main.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * mac80211 glue code for mac80211 Prism54 drivers
  4. *
  5. * Copyright (c) 2006, Michael Wu <[email protected]>
  6. * Copyright (c) 2007-2009, Christian Lamparter <[email protected]>
  7. * Copyright 2008, Johannes Berg <[email protected]>
  8. *
  9. * Based on:
  10. * - the islsm (softmac prism54) driver, which is:
  11. * Copyright 2004-2006 Jean-Baptiste Note <[email protected]>, et al.
  12. * - stlc45xx driver
  13. * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
  14. */
  15. #include <linux/slab.h>
  16. #include <linux/firmware.h>
  17. #include <linux/etherdevice.h>
  18. #include <linux/module.h>
  19. #include <net/mac80211.h>
  20. #include "p54.h"
  21. #include "lmac.h"
  22. static bool modparam_nohwcrypt;
  23. module_param_named(nohwcrypt, modparam_nohwcrypt, bool, 0444);
  24. MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption.");
  25. MODULE_AUTHOR("Michael Wu <[email protected]>");
  26. MODULE_DESCRIPTION("Softmac Prism54 common code");
  27. MODULE_LICENSE("GPL");
  28. MODULE_ALIAS("prism54common");
  29. static int p54_sta_add_remove(struct ieee80211_hw *hw,
  30. struct ieee80211_vif *vif,
  31. struct ieee80211_sta *sta)
  32. {
  33. struct p54_common *priv = hw->priv;
  34. /*
  35. * Notify the firmware that we don't want or we don't
  36. * need to buffer frames for this station anymore.
  37. */
  38. p54_sta_unlock(priv, sta->addr);
  39. return 0;
  40. }
  41. static void p54_sta_notify(struct ieee80211_hw *dev, struct ieee80211_vif *vif,
  42. enum sta_notify_cmd notify_cmd,
  43. struct ieee80211_sta *sta)
  44. {
  45. struct p54_common *priv = dev->priv;
  46. switch (notify_cmd) {
  47. case STA_NOTIFY_AWAKE:
  48. /* update the firmware's filter table */
  49. p54_sta_unlock(priv, sta->addr);
  50. break;
  51. default:
  52. break;
  53. }
  54. }
  55. static int p54_set_tim(struct ieee80211_hw *dev, struct ieee80211_sta *sta,
  56. bool set)
  57. {
  58. struct p54_common *priv = dev->priv;
  59. return p54_update_beacon_tim(priv, sta->aid, set);
  60. }
  61. u8 *p54_find_ie(struct sk_buff *skb, u8 ie)
  62. {
  63. struct ieee80211_mgmt *mgmt = (void *)skb->data;
  64. u8 *pos, *end;
  65. if (skb->len <= sizeof(mgmt))
  66. return NULL;
  67. pos = (u8 *)mgmt->u.beacon.variable;
  68. end = skb->data + skb->len;
  69. while (pos < end) {
  70. if (pos + 2 + pos[1] > end)
  71. return NULL;
  72. if (pos[0] == ie)
  73. return pos;
  74. pos += 2 + pos[1];
  75. }
  76. return NULL;
  77. }
  78. static int p54_beacon_format_ie_tim(struct sk_buff *skb)
  79. {
  80. /*
  81. * the good excuse for this mess is ... the firmware.
  82. * The dummy TIM MUST be at the end of the beacon frame,
  83. * because it'll be overwritten!
  84. */
  85. u8 *tim;
  86. u8 dtim_len;
  87. u8 dtim_period;
  88. u8 *next;
  89. tim = p54_find_ie(skb, WLAN_EID_TIM);
  90. if (!tim)
  91. return 0;
  92. dtim_len = tim[1];
  93. dtim_period = tim[3];
  94. next = tim + 2 + dtim_len;
  95. if (dtim_len < 3)
  96. return -EINVAL;
  97. memmove(tim, next, skb_tail_pointer(skb) - next);
  98. tim = skb_tail_pointer(skb) - (dtim_len + 2);
  99. /* add the dummy at the end */
  100. tim[0] = WLAN_EID_TIM;
  101. tim[1] = 3;
  102. tim[2] = 0;
  103. tim[3] = dtim_period;
  104. tim[4] = 0;
  105. if (dtim_len > 3)
  106. skb_trim(skb, skb->len - (dtim_len - 3));
  107. return 0;
  108. }
  109. static int p54_beacon_update(struct p54_common *priv,
  110. struct ieee80211_vif *vif)
  111. {
  112. struct ieee80211_tx_control control = { };
  113. struct sk_buff *beacon;
  114. int ret;
  115. beacon = ieee80211_beacon_get(priv->hw, vif, 0);
  116. if (!beacon)
  117. return -ENOMEM;
  118. ret = p54_beacon_format_ie_tim(beacon);
  119. if (ret)
  120. return ret;
  121. /*
  122. * During operation, the firmware takes care of beaconing.
  123. * The driver only needs to upload a new beacon template, once
  124. * the template was changed by the stack or userspace.
  125. *
  126. * LMAC API 3.2.2 also specifies that the driver does not need
  127. * to cancel the old beacon template by hand, instead the firmware
  128. * will release the previous one through the feedback mechanism.
  129. */
  130. p54_tx_80211(priv->hw, &control, beacon);
  131. priv->tsf_high32 = 0;
  132. priv->tsf_low32 = 0;
  133. return 0;
  134. }
  135. static int p54_start(struct ieee80211_hw *dev)
  136. {
  137. struct p54_common *priv = dev->priv;
  138. int err;
  139. mutex_lock(&priv->conf_mutex);
  140. err = priv->open(dev);
  141. if (err)
  142. goto out;
  143. P54_SET_QUEUE(priv->qos_params[0], 0x0002, 0x0003, 0x0007, 47);
  144. P54_SET_QUEUE(priv->qos_params[1], 0x0002, 0x0007, 0x000f, 94);
  145. P54_SET_QUEUE(priv->qos_params[2], 0x0003, 0x000f, 0x03ff, 0);
  146. P54_SET_QUEUE(priv->qos_params[3], 0x0007, 0x000f, 0x03ff, 0);
  147. err = p54_set_edcf(priv);
  148. if (err)
  149. goto out;
  150. eth_broadcast_addr(priv->bssid);
  151. priv->mode = NL80211_IFTYPE_MONITOR;
  152. err = p54_setup_mac(priv);
  153. if (err) {
  154. priv->mode = NL80211_IFTYPE_UNSPECIFIED;
  155. goto out;
  156. }
  157. ieee80211_queue_delayed_work(dev, &priv->work, 0);
  158. priv->softled_state = 0;
  159. err = p54_set_leds(priv);
  160. out:
  161. mutex_unlock(&priv->conf_mutex);
  162. return err;
  163. }
  164. static void p54_stop(struct ieee80211_hw *dev)
  165. {
  166. struct p54_common *priv = dev->priv;
  167. int i;
  168. priv->mode = NL80211_IFTYPE_UNSPECIFIED;
  169. priv->softled_state = 0;
  170. cancel_delayed_work_sync(&priv->work);
  171. mutex_lock(&priv->conf_mutex);
  172. p54_set_leds(priv);
  173. priv->stop(dev);
  174. skb_queue_purge(&priv->tx_pending);
  175. skb_queue_purge(&priv->tx_queue);
  176. for (i = 0; i < P54_QUEUE_NUM; i++) {
  177. priv->tx_stats[i].count = 0;
  178. priv->tx_stats[i].len = 0;
  179. }
  180. priv->beacon_req_id = cpu_to_le32(0);
  181. priv->tsf_high32 = priv->tsf_low32 = 0;
  182. mutex_unlock(&priv->conf_mutex);
  183. }
  184. static int p54_add_interface(struct ieee80211_hw *dev,
  185. struct ieee80211_vif *vif)
  186. {
  187. struct p54_common *priv = dev->priv;
  188. int err;
  189. vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER;
  190. mutex_lock(&priv->conf_mutex);
  191. if (priv->mode != NL80211_IFTYPE_MONITOR) {
  192. mutex_unlock(&priv->conf_mutex);
  193. return -EOPNOTSUPP;
  194. }
  195. priv->vif = vif;
  196. switch (vif->type) {
  197. case NL80211_IFTYPE_STATION:
  198. case NL80211_IFTYPE_ADHOC:
  199. case NL80211_IFTYPE_AP:
  200. case NL80211_IFTYPE_MESH_POINT:
  201. priv->mode = vif->type;
  202. break;
  203. default:
  204. mutex_unlock(&priv->conf_mutex);
  205. return -EOPNOTSUPP;
  206. }
  207. memcpy(priv->mac_addr, vif->addr, ETH_ALEN);
  208. err = p54_setup_mac(priv);
  209. mutex_unlock(&priv->conf_mutex);
  210. return err;
  211. }
  212. static void p54_remove_interface(struct ieee80211_hw *dev,
  213. struct ieee80211_vif *vif)
  214. {
  215. struct p54_common *priv = dev->priv;
  216. mutex_lock(&priv->conf_mutex);
  217. priv->vif = NULL;
  218. /*
  219. * LMAC API 3.2.2 states that any active beacon template must be
  220. * canceled by the driver before attempting a mode transition.
  221. */
  222. if (le32_to_cpu(priv->beacon_req_id) != 0) {
  223. p54_tx_cancel(priv, priv->beacon_req_id);
  224. wait_for_completion_interruptible_timeout(&priv->beacon_comp, HZ);
  225. }
  226. priv->mode = NL80211_IFTYPE_MONITOR;
  227. eth_zero_addr(priv->mac_addr);
  228. eth_zero_addr(priv->bssid);
  229. p54_setup_mac(priv);
  230. mutex_unlock(&priv->conf_mutex);
  231. }
  232. static int p54_wait_for_stats(struct ieee80211_hw *dev)
  233. {
  234. struct p54_common *priv = dev->priv;
  235. int ret;
  236. priv->update_stats = true;
  237. ret = p54_fetch_statistics(priv);
  238. if (ret)
  239. return ret;
  240. ret = wait_for_completion_interruptible_timeout(&priv->stat_comp, HZ);
  241. if (ret == 0)
  242. return -ETIMEDOUT;
  243. return 0;
  244. }
  245. static void p54_reset_stats(struct p54_common *priv)
  246. {
  247. struct ieee80211_channel *chan = priv->curchan;
  248. if (chan) {
  249. struct survey_info *info = &priv->survey[chan->hw_value];
  250. /* only reset channel statistics, don't touch .filled, etc. */
  251. info->time = 0;
  252. info->time_busy = 0;
  253. info->time_tx = 0;
  254. }
  255. priv->update_stats = true;
  256. priv->survey_raw.active = 0;
  257. priv->survey_raw.cca = 0;
  258. priv->survey_raw.tx = 0;
  259. }
  260. static int p54_config(struct ieee80211_hw *dev, u32 changed)
  261. {
  262. int ret = 0;
  263. struct p54_common *priv = dev->priv;
  264. struct ieee80211_conf *conf = &dev->conf;
  265. mutex_lock(&priv->conf_mutex);
  266. if (changed & IEEE80211_CONF_CHANGE_POWER)
  267. priv->output_power = conf->power_level << 2;
  268. if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
  269. struct ieee80211_channel *oldchan;
  270. WARN_ON(p54_wait_for_stats(dev));
  271. oldchan = priv->curchan;
  272. priv->curchan = NULL;
  273. ret = p54_scan(priv, P54_SCAN_EXIT, 0);
  274. if (ret) {
  275. priv->curchan = oldchan;
  276. goto out;
  277. }
  278. /*
  279. * TODO: Use the LM_SCAN_TRAP to determine the current
  280. * operating channel.
  281. */
  282. priv->curchan = priv->hw->conf.chandef.chan;
  283. p54_reset_stats(priv);
  284. WARN_ON(p54_fetch_statistics(priv));
  285. }
  286. if (changed & IEEE80211_CONF_CHANGE_PS) {
  287. WARN_ON(p54_wait_for_stats(dev));
  288. ret = p54_set_ps(priv);
  289. if (ret)
  290. goto out;
  291. WARN_ON(p54_wait_for_stats(dev));
  292. }
  293. if (changed & IEEE80211_CONF_CHANGE_IDLE) {
  294. WARN_ON(p54_wait_for_stats(dev));
  295. ret = p54_setup_mac(priv);
  296. if (ret)
  297. goto out;
  298. WARN_ON(p54_wait_for_stats(dev));
  299. }
  300. out:
  301. mutex_unlock(&priv->conf_mutex);
  302. return ret;
  303. }
  304. static u64 p54_prepare_multicast(struct ieee80211_hw *dev,
  305. struct netdev_hw_addr_list *mc_list)
  306. {
  307. struct p54_common *priv = dev->priv;
  308. struct netdev_hw_addr *ha;
  309. int i;
  310. BUILD_BUG_ON(ARRAY_SIZE(priv->mc_maclist) !=
  311. ARRAY_SIZE(((struct p54_group_address_table *)NULL)->mac_list));
  312. /*
  313. * The first entry is reserved for the global broadcast MAC.
  314. * Otherwise the firmware will drop it and ARP will no longer work.
  315. */
  316. i = 1;
  317. priv->mc_maclist_num = netdev_hw_addr_list_count(mc_list) + i;
  318. netdev_hw_addr_list_for_each(ha, mc_list) {
  319. memcpy(&priv->mc_maclist[i], ha->addr, ETH_ALEN);
  320. i++;
  321. if (i >= ARRAY_SIZE(priv->mc_maclist))
  322. break;
  323. }
  324. return 1; /* update */
  325. }
  326. static void p54_configure_filter(struct ieee80211_hw *dev,
  327. unsigned int changed_flags,
  328. unsigned int *total_flags,
  329. u64 multicast)
  330. {
  331. struct p54_common *priv = dev->priv;
  332. *total_flags &= FIF_ALLMULTI | FIF_OTHER_BSS;
  333. priv->filter_flags = *total_flags;
  334. if (changed_flags & FIF_OTHER_BSS)
  335. p54_setup_mac(priv);
  336. if (changed_flags & FIF_ALLMULTI || multicast)
  337. p54_set_groupfilter(priv);
  338. }
  339. static int p54_conf_tx(struct ieee80211_hw *dev,
  340. struct ieee80211_vif *vif,
  341. unsigned int link_id, u16 queue,
  342. const struct ieee80211_tx_queue_params *params)
  343. {
  344. struct p54_common *priv = dev->priv;
  345. int ret;
  346. mutex_lock(&priv->conf_mutex);
  347. P54_SET_QUEUE(priv->qos_params[queue], params->aifs,
  348. params->cw_min, params->cw_max, params->txop);
  349. ret = p54_set_edcf(priv);
  350. mutex_unlock(&priv->conf_mutex);
  351. return ret;
  352. }
  353. static void p54_work(struct work_struct *work)
  354. {
  355. struct p54_common *priv = container_of(work, struct p54_common,
  356. work.work);
  357. if (unlikely(priv->mode == NL80211_IFTYPE_UNSPECIFIED))
  358. return ;
  359. /*
  360. * TODO: walk through tx_queue and do the following tasks
  361. * 1. initiate bursts.
  362. * 2. cancel stuck frames / reset the device if necessary.
  363. */
  364. mutex_lock(&priv->conf_mutex);
  365. WARN_ON_ONCE(p54_fetch_statistics(priv));
  366. mutex_unlock(&priv->conf_mutex);
  367. }
  368. static int p54_get_stats(struct ieee80211_hw *dev,
  369. struct ieee80211_low_level_stats *stats)
  370. {
  371. struct p54_common *priv = dev->priv;
  372. memcpy(stats, &priv->stats, sizeof(*stats));
  373. return 0;
  374. }
  375. static void p54_bss_info_changed(struct ieee80211_hw *dev,
  376. struct ieee80211_vif *vif,
  377. struct ieee80211_bss_conf *info,
  378. u64 changed)
  379. {
  380. struct p54_common *priv = dev->priv;
  381. mutex_lock(&priv->conf_mutex);
  382. if (changed & BSS_CHANGED_BSSID) {
  383. memcpy(priv->bssid, info->bssid, ETH_ALEN);
  384. p54_setup_mac(priv);
  385. }
  386. if (changed & BSS_CHANGED_BEACON) {
  387. p54_scan(priv, P54_SCAN_EXIT, 0);
  388. p54_setup_mac(priv);
  389. p54_beacon_update(priv, vif);
  390. p54_set_edcf(priv);
  391. }
  392. if (changed & (BSS_CHANGED_ERP_SLOT | BSS_CHANGED_BEACON)) {
  393. priv->use_short_slot = info->use_short_slot;
  394. p54_set_edcf(priv);
  395. }
  396. if (changed & BSS_CHANGED_BASIC_RATES) {
  397. if (dev->conf.chandef.chan->band == NL80211_BAND_5GHZ)
  398. priv->basic_rate_mask = (info->basic_rates << 4);
  399. else
  400. priv->basic_rate_mask = info->basic_rates;
  401. p54_setup_mac(priv);
  402. if (priv->fw_var >= 0x500)
  403. p54_scan(priv, P54_SCAN_EXIT, 0);
  404. }
  405. if (changed & BSS_CHANGED_ASSOC) {
  406. if (vif->cfg.assoc) {
  407. priv->aid = vif->cfg.aid;
  408. priv->wakeup_timer = info->beacon_int *
  409. info->dtim_period * 5;
  410. p54_setup_mac(priv);
  411. } else {
  412. priv->wakeup_timer = 500;
  413. priv->aid = 0;
  414. }
  415. }
  416. mutex_unlock(&priv->conf_mutex);
  417. }
  418. static int p54_set_key(struct ieee80211_hw *dev, enum set_key_cmd cmd,
  419. struct ieee80211_vif *vif, struct ieee80211_sta *sta,
  420. struct ieee80211_key_conf *key)
  421. {
  422. struct p54_common *priv = dev->priv;
  423. int slot, ret = 0;
  424. u8 algo = 0;
  425. u8 *addr = NULL;
  426. if (modparam_nohwcrypt)
  427. return -EOPNOTSUPP;
  428. if (key->flags & IEEE80211_KEY_FLAG_RX_MGMT) {
  429. /*
  430. * Unfortunately most/all firmwares are trying to decrypt
  431. * incoming management frames if a suitable key can be found.
  432. * However, in doing so the data in these frames gets
  433. * corrupted. So, we can't have firmware supported crypto
  434. * offload in this case.
  435. */
  436. return -EOPNOTSUPP;
  437. }
  438. mutex_lock(&priv->conf_mutex);
  439. if (cmd == SET_KEY) {
  440. switch (key->cipher) {
  441. case WLAN_CIPHER_SUITE_TKIP:
  442. if (!(priv->privacy_caps & (BR_DESC_PRIV_CAP_MICHAEL |
  443. BR_DESC_PRIV_CAP_TKIP))) {
  444. ret = -EOPNOTSUPP;
  445. goto out_unlock;
  446. }
  447. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  448. algo = P54_CRYPTO_TKIPMICHAEL;
  449. break;
  450. case WLAN_CIPHER_SUITE_WEP40:
  451. case WLAN_CIPHER_SUITE_WEP104:
  452. if (!(priv->privacy_caps & BR_DESC_PRIV_CAP_WEP)) {
  453. ret = -EOPNOTSUPP;
  454. goto out_unlock;
  455. }
  456. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  457. algo = P54_CRYPTO_WEP;
  458. break;
  459. case WLAN_CIPHER_SUITE_CCMP:
  460. if (!(priv->privacy_caps & BR_DESC_PRIV_CAP_AESCCMP)) {
  461. ret = -EOPNOTSUPP;
  462. goto out_unlock;
  463. }
  464. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  465. algo = P54_CRYPTO_AESCCMP;
  466. break;
  467. default:
  468. ret = -EOPNOTSUPP;
  469. goto out_unlock;
  470. }
  471. slot = bitmap_find_free_region(priv->used_rxkeys,
  472. priv->rx_keycache_size, 0);
  473. if (slot < 0) {
  474. /*
  475. * The device supports the chosen algorithm, but the
  476. * firmware does not provide enough key slots to store
  477. * all of them.
  478. * But encryption offload for outgoing frames is always
  479. * possible, so we just pretend that the upload was
  480. * successful and do the decryption in software.
  481. */
  482. /* mark the key as invalid. */
  483. key->hw_key_idx = 0xff;
  484. goto out_unlock;
  485. }
  486. key->flags |= IEEE80211_KEY_FLAG_RESERVE_TAILROOM;
  487. } else {
  488. slot = key->hw_key_idx;
  489. if (slot == 0xff) {
  490. /* This key was not uploaded into the rx key cache. */
  491. goto out_unlock;
  492. }
  493. bitmap_release_region(priv->used_rxkeys, slot, 0);
  494. algo = 0;
  495. }
  496. if (sta)
  497. addr = sta->addr;
  498. ret = p54_upload_key(priv, algo, slot, key->keyidx,
  499. key->keylen, addr, key->key);
  500. if (ret) {
  501. bitmap_release_region(priv->used_rxkeys, slot, 0);
  502. ret = -EOPNOTSUPP;
  503. goto out_unlock;
  504. }
  505. key->hw_key_idx = slot;
  506. out_unlock:
  507. mutex_unlock(&priv->conf_mutex);
  508. return ret;
  509. }
  510. static int p54_get_survey(struct ieee80211_hw *dev, int idx,
  511. struct survey_info *survey)
  512. {
  513. struct p54_common *priv = dev->priv;
  514. struct ieee80211_channel *chan;
  515. int err, tries;
  516. bool in_use = false;
  517. if (idx >= priv->chan_num)
  518. return -ENOENT;
  519. #define MAX_TRIES 1
  520. for (tries = 0; tries < MAX_TRIES; tries++) {
  521. chan = priv->curchan;
  522. if (chan && chan->hw_value == idx) {
  523. mutex_lock(&priv->conf_mutex);
  524. err = p54_wait_for_stats(dev);
  525. mutex_unlock(&priv->conf_mutex);
  526. if (err)
  527. return err;
  528. in_use = true;
  529. }
  530. memcpy(survey, &priv->survey[idx], sizeof(*survey));
  531. if (in_use) {
  532. /* test if the reported statistics are valid. */
  533. if (survey->time != 0) {
  534. survey->filled |= SURVEY_INFO_IN_USE;
  535. } else {
  536. /*
  537. * hw/fw has not accumulated enough sample sets.
  538. * Wait for 100ms, this ought to be enough to
  539. * get at least one non-null set of channel
  540. * usage statistics.
  541. */
  542. msleep(100);
  543. continue;
  544. }
  545. }
  546. return 0;
  547. }
  548. return -ETIMEDOUT;
  549. #undef MAX_TRIES
  550. }
  551. static unsigned int p54_flush_count(struct p54_common *priv)
  552. {
  553. unsigned int total = 0, i;
  554. BUILD_BUG_ON(P54_QUEUE_NUM > ARRAY_SIZE(priv->tx_stats));
  555. /*
  556. * Because the firmware has the sole control over any frames
  557. * in the P54_QUEUE_BEACON or P54_QUEUE_SCAN queues, they
  558. * don't really count as pending or active.
  559. */
  560. for (i = P54_QUEUE_MGMT; i < P54_QUEUE_NUM; i++)
  561. total += priv->tx_stats[i].len;
  562. return total;
  563. }
  564. static void p54_flush(struct ieee80211_hw *dev, struct ieee80211_vif *vif,
  565. u32 queues, bool drop)
  566. {
  567. struct p54_common *priv = dev->priv;
  568. unsigned int total, i;
  569. /*
  570. * Currently, it wouldn't really matter if we wait for one second
  571. * or 15 minutes. But once someone gets around and completes the
  572. * TODOs [ancel stuck frames / reset device] in p54_work, it will
  573. * suddenly make sense to wait that long.
  574. */
  575. i = P54_STATISTICS_UPDATE * 2 / 20;
  576. /*
  577. * In this case no locking is required because as we speak the
  578. * queues have already been stopped and no new frames can sneak
  579. * up from behind.
  580. */
  581. while ((total = p54_flush_count(priv)) && i--) {
  582. /* waste time */
  583. msleep(20);
  584. }
  585. WARN(total, "tx flush timeout, unresponsive firmware");
  586. }
  587. static void p54_set_coverage_class(struct ieee80211_hw *dev,
  588. s16 coverage_class)
  589. {
  590. struct p54_common *priv = dev->priv;
  591. mutex_lock(&priv->conf_mutex);
  592. /* support all coverage class values as in 802.11-2007 Table 7-27 */
  593. priv->coverage_class = clamp_t(u8, coverage_class, 0, 31);
  594. p54_set_edcf(priv);
  595. mutex_unlock(&priv->conf_mutex);
  596. }
  597. static const struct ieee80211_ops p54_ops = {
  598. .tx = p54_tx_80211,
  599. .start = p54_start,
  600. .stop = p54_stop,
  601. .add_interface = p54_add_interface,
  602. .remove_interface = p54_remove_interface,
  603. .set_tim = p54_set_tim,
  604. .sta_notify = p54_sta_notify,
  605. .sta_add = p54_sta_add_remove,
  606. .sta_remove = p54_sta_add_remove,
  607. .set_key = p54_set_key,
  608. .config = p54_config,
  609. .flush = p54_flush,
  610. .bss_info_changed = p54_bss_info_changed,
  611. .prepare_multicast = p54_prepare_multicast,
  612. .configure_filter = p54_configure_filter,
  613. .conf_tx = p54_conf_tx,
  614. .get_stats = p54_get_stats,
  615. .get_survey = p54_get_survey,
  616. .set_coverage_class = p54_set_coverage_class,
  617. };
  618. struct ieee80211_hw *p54_init_common(size_t priv_data_len)
  619. {
  620. struct ieee80211_hw *dev;
  621. struct p54_common *priv;
  622. dev = ieee80211_alloc_hw(priv_data_len, &p54_ops);
  623. if (!dev)
  624. return NULL;
  625. priv = dev->priv;
  626. priv->hw = dev;
  627. priv->mode = NL80211_IFTYPE_UNSPECIFIED;
  628. priv->basic_rate_mask = 0x15f;
  629. spin_lock_init(&priv->tx_stats_lock);
  630. skb_queue_head_init(&priv->tx_queue);
  631. skb_queue_head_init(&priv->tx_pending);
  632. ieee80211_hw_set(dev, REPORTS_TX_ACK_STATUS);
  633. ieee80211_hw_set(dev, MFP_CAPABLE);
  634. ieee80211_hw_set(dev, PS_NULLFUNC_STACK);
  635. ieee80211_hw_set(dev, SUPPORTS_PS);
  636. ieee80211_hw_set(dev, RX_INCLUDES_FCS);
  637. ieee80211_hw_set(dev, SIGNAL_DBM);
  638. dev->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
  639. BIT(NL80211_IFTYPE_ADHOC) |
  640. BIT(NL80211_IFTYPE_AP) |
  641. BIT(NL80211_IFTYPE_MESH_POINT);
  642. priv->beacon_req_id = cpu_to_le32(0);
  643. priv->tx_stats[P54_QUEUE_BEACON].limit = 1;
  644. priv->tx_stats[P54_QUEUE_FWSCAN].limit = 1;
  645. priv->tx_stats[P54_QUEUE_MGMT].limit = 3;
  646. priv->tx_stats[P54_QUEUE_CAB].limit = 3;
  647. priv->tx_stats[P54_QUEUE_DATA].limit = 5;
  648. dev->queues = 1;
  649. priv->noise = -94;
  650. /*
  651. * We support at most 8 tries no matter which rate they're at,
  652. * we cannot support max_rates * max_rate_tries as we set it
  653. * here, but setting it correctly to 4/2 or so would limit us
  654. * artificially if the RC algorithm wants just two rates, so
  655. * let's say 4/7, we'll redistribute it at TX time, see the
  656. * comments there.
  657. */
  658. dev->max_rates = 4;
  659. dev->max_rate_tries = 7;
  660. dev->extra_tx_headroom = sizeof(struct p54_hdr) + 4 +
  661. sizeof(struct p54_tx_data);
  662. /*
  663. * For now, disable PS by default because it affects
  664. * link stability significantly.
  665. */
  666. dev->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
  667. mutex_init(&priv->conf_mutex);
  668. mutex_init(&priv->eeprom_mutex);
  669. init_completion(&priv->stat_comp);
  670. init_completion(&priv->eeprom_comp);
  671. init_completion(&priv->beacon_comp);
  672. INIT_DELAYED_WORK(&priv->work, p54_work);
  673. eth_broadcast_addr(priv->mc_maclist[0]);
  674. priv->curchan = NULL;
  675. p54_reset_stats(priv);
  676. return dev;
  677. }
  678. EXPORT_SYMBOL_GPL(p54_init_common);
  679. int p54_register_common(struct ieee80211_hw *dev, struct device *pdev)
  680. {
  681. struct p54_common __maybe_unused *priv = dev->priv;
  682. int err;
  683. err = ieee80211_register_hw(dev);
  684. if (err) {
  685. dev_err(pdev, "Cannot register device (%d).\n", err);
  686. return err;
  687. }
  688. priv->registered = true;
  689. #ifdef CONFIG_P54_LEDS
  690. err = p54_init_leds(priv);
  691. if (err) {
  692. p54_unregister_common(dev);
  693. return err;
  694. }
  695. #endif /* CONFIG_P54_LEDS */
  696. dev_info(pdev, "is registered as '%s'\n", wiphy_name(dev->wiphy));
  697. return 0;
  698. }
  699. EXPORT_SYMBOL_GPL(p54_register_common);
  700. void p54_free_common(struct ieee80211_hw *dev)
  701. {
  702. struct p54_common *priv = dev->priv;
  703. unsigned int i;
  704. for (i = 0; i < NUM_NL80211_BANDS; i++)
  705. kfree(priv->band_table[i]);
  706. kfree(priv->iq_autocal);
  707. kfree(priv->output_limit);
  708. kfree(priv->curve_data);
  709. kfree(priv->rssi_db);
  710. bitmap_free(priv->used_rxkeys);
  711. kfree(priv->survey);
  712. priv->iq_autocal = NULL;
  713. priv->output_limit = NULL;
  714. priv->curve_data = NULL;
  715. priv->rssi_db = NULL;
  716. priv->used_rxkeys = NULL;
  717. priv->survey = NULL;
  718. ieee80211_free_hw(dev);
  719. }
  720. EXPORT_SYMBOL_GPL(p54_free_common);
  721. void p54_unregister_common(struct ieee80211_hw *dev)
  722. {
  723. struct p54_common *priv = dev->priv;
  724. if (priv->registered) {
  725. priv->registered = false;
  726. #ifdef CONFIG_P54_LEDS
  727. p54_unregister_leds(priv);
  728. #endif /* CONFIG_P54_LEDS */
  729. ieee80211_unregister_hw(dev);
  730. }
  731. mutex_destroy(&priv->conf_mutex);
  732. mutex_destroy(&priv->eeprom_mutex);
  733. }
  734. EXPORT_SYMBOL_GPL(p54_unregister_common);