main.c 21 KB

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