rt2x00dev.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. Copyright (C) 2010 Willow Garage <http://www.willowgarage.com>
  4. Copyright (C) 2004 - 2010 Ivo van Doorn <IvDoorn@gmail.com>
  5. <http://rt2x00.serialmonkey.com>
  6. */
  7. /*
  8. Module: rt2x00lib
  9. Abstract: rt2x00 generic device routines.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/slab.h>
  14. #include <linux/log2.h>
  15. #include <linux/of.h>
  16. #include <linux/of_net.h>
  17. #include "rt2x00.h"
  18. #include "rt2x00lib.h"
  19. /*
  20. * Utility functions.
  21. */
  22. u32 rt2x00lib_get_bssidx(struct rt2x00_dev *rt2x00dev,
  23. struct ieee80211_vif *vif)
  24. {
  25. /*
  26. * When in STA mode, bssidx is always 0 otherwise local_address[5]
  27. * contains the bss number, see BSS_ID_MASK comments for details.
  28. */
  29. if (rt2x00dev->intf_sta_count)
  30. return 0;
  31. return vif->addr[5] & (rt2x00dev->ops->max_ap_intf - 1);
  32. }
  33. EXPORT_SYMBOL_GPL(rt2x00lib_get_bssidx);
  34. /*
  35. * Radio control handlers.
  36. */
  37. int rt2x00lib_enable_radio(struct rt2x00_dev *rt2x00dev)
  38. {
  39. int status;
  40. /*
  41. * Don't enable the radio twice.
  42. * And check if the hardware button has been disabled.
  43. */
  44. if (test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  45. return 0;
  46. /*
  47. * Initialize all data queues.
  48. */
  49. rt2x00queue_init_queues(rt2x00dev);
  50. /*
  51. * Enable radio.
  52. */
  53. status =
  54. rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_ON);
  55. if (status)
  56. return status;
  57. rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_IRQ_ON);
  58. rt2x00leds_led_radio(rt2x00dev, true);
  59. rt2x00led_led_activity(rt2x00dev, true);
  60. set_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags);
  61. /*
  62. * Enable queues.
  63. */
  64. rt2x00queue_start_queues(rt2x00dev);
  65. rt2x00link_start_tuner(rt2x00dev);
  66. /*
  67. * Start watchdog monitoring.
  68. */
  69. rt2x00link_start_watchdog(rt2x00dev);
  70. return 0;
  71. }
  72. void rt2x00lib_disable_radio(struct rt2x00_dev *rt2x00dev)
  73. {
  74. if (!test_and_clear_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  75. return;
  76. /*
  77. * Stop watchdog monitoring.
  78. */
  79. rt2x00link_stop_watchdog(rt2x00dev);
  80. /*
  81. * Stop all queues
  82. */
  83. rt2x00link_stop_tuner(rt2x00dev);
  84. rt2x00queue_stop_queues(rt2x00dev);
  85. rt2x00queue_flush_queues(rt2x00dev, true);
  86. rt2x00queue_stop_queue(rt2x00dev->bcn);
  87. /*
  88. * Disable radio.
  89. */
  90. rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_OFF);
  91. rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_IRQ_OFF);
  92. rt2x00led_led_activity(rt2x00dev, false);
  93. rt2x00leds_led_radio(rt2x00dev, false);
  94. }
  95. static void rt2x00lib_intf_scheduled_iter(void *data, u8 *mac,
  96. struct ieee80211_vif *vif)
  97. {
  98. struct rt2x00_dev *rt2x00dev = data;
  99. struct rt2x00_intf *intf = vif_to_intf(vif);
  100. /*
  101. * It is possible the radio was disabled while the work had been
  102. * scheduled. If that happens we should return here immediately,
  103. * note that in the spinlock protected area above the delayed_flags
  104. * have been cleared correctly.
  105. */
  106. if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  107. return;
  108. if (test_and_clear_bit(DELAYED_UPDATE_BEACON, &intf->delayed_flags)) {
  109. mutex_lock(&intf->beacon_skb_mutex);
  110. rt2x00queue_update_beacon(rt2x00dev, vif);
  111. mutex_unlock(&intf->beacon_skb_mutex);
  112. }
  113. }
  114. static void rt2x00lib_intf_scheduled(struct work_struct *work)
  115. {
  116. struct rt2x00_dev *rt2x00dev =
  117. container_of(work, struct rt2x00_dev, intf_work);
  118. /*
  119. * Iterate over each interface and perform the
  120. * requested configurations.
  121. */
  122. ieee80211_iterate_active_interfaces(rt2x00dev->hw,
  123. IEEE80211_IFACE_ITER_RESUME_ALL,
  124. rt2x00lib_intf_scheduled_iter,
  125. rt2x00dev);
  126. }
  127. static void rt2x00lib_autowakeup(struct work_struct *work)
  128. {
  129. struct rt2x00_dev *rt2x00dev =
  130. container_of(work, struct rt2x00_dev, autowakeup_work.work);
  131. if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
  132. return;
  133. if (rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_AWAKE))
  134. rt2x00_err(rt2x00dev, "Device failed to wakeup\n");
  135. clear_bit(CONFIG_POWERSAVING, &rt2x00dev->flags);
  136. }
  137. /*
  138. * Interrupt context handlers.
  139. */
  140. static void rt2x00lib_bc_buffer_iter(void *data, u8 *mac,
  141. struct ieee80211_vif *vif)
  142. {
  143. struct ieee80211_tx_control control = {};
  144. struct rt2x00_dev *rt2x00dev = data;
  145. struct sk_buff *skb;
  146. /*
  147. * Only AP mode interfaces do broad- and multicast buffering
  148. */
  149. if (vif->type != NL80211_IFTYPE_AP)
  150. return;
  151. /*
  152. * Send out buffered broad- and multicast frames
  153. */
  154. skb = ieee80211_get_buffered_bc(rt2x00dev->hw, vif);
  155. while (skb) {
  156. rt2x00mac_tx(rt2x00dev->hw, &control, skb);
  157. skb = ieee80211_get_buffered_bc(rt2x00dev->hw, vif);
  158. }
  159. }
  160. static void rt2x00lib_beaconupdate_iter(void *data, u8 *mac,
  161. struct ieee80211_vif *vif)
  162. {
  163. struct rt2x00_dev *rt2x00dev = data;
  164. if (vif->type != NL80211_IFTYPE_AP &&
  165. vif->type != NL80211_IFTYPE_ADHOC &&
  166. vif->type != NL80211_IFTYPE_MESH_POINT)
  167. return;
  168. /*
  169. * Update the beacon without locking. This is safe on PCI devices
  170. * as they only update the beacon periodically here. This should
  171. * never be called for USB devices.
  172. */
  173. WARN_ON(rt2x00_is_usb(rt2x00dev));
  174. rt2x00queue_update_beacon(rt2x00dev, vif);
  175. }
  176. void rt2x00lib_beacondone(struct rt2x00_dev *rt2x00dev)
  177. {
  178. if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  179. return;
  180. /* send buffered bc/mc frames out for every bssid */
  181. ieee80211_iterate_active_interfaces_atomic(
  182. rt2x00dev->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
  183. rt2x00lib_bc_buffer_iter, rt2x00dev);
  184. /*
  185. * Devices with pre tbtt interrupt don't need to update the beacon
  186. * here as they will fetch the next beacon directly prior to
  187. * transmission.
  188. */
  189. if (rt2x00_has_cap_pre_tbtt_interrupt(rt2x00dev))
  190. return;
  191. /* fetch next beacon */
  192. ieee80211_iterate_active_interfaces_atomic(
  193. rt2x00dev->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
  194. rt2x00lib_beaconupdate_iter, rt2x00dev);
  195. }
  196. EXPORT_SYMBOL_GPL(rt2x00lib_beacondone);
  197. void rt2x00lib_pretbtt(struct rt2x00_dev *rt2x00dev)
  198. {
  199. if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  200. return;
  201. /* fetch next beacon */
  202. ieee80211_iterate_active_interfaces_atomic(
  203. rt2x00dev->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
  204. rt2x00lib_beaconupdate_iter, rt2x00dev);
  205. }
  206. EXPORT_SYMBOL_GPL(rt2x00lib_pretbtt);
  207. void rt2x00lib_dmastart(struct queue_entry *entry)
  208. {
  209. set_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
  210. rt2x00queue_index_inc(entry, Q_INDEX);
  211. }
  212. EXPORT_SYMBOL_GPL(rt2x00lib_dmastart);
  213. void rt2x00lib_dmadone(struct queue_entry *entry)
  214. {
  215. set_bit(ENTRY_DATA_STATUS_PENDING, &entry->flags);
  216. clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
  217. rt2x00queue_index_inc(entry, Q_INDEX_DMA_DONE);
  218. }
  219. EXPORT_SYMBOL_GPL(rt2x00lib_dmadone);
  220. static inline int rt2x00lib_txdone_bar_status(struct queue_entry *entry)
  221. {
  222. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  223. struct ieee80211_bar *bar = (void *) entry->skb->data;
  224. struct rt2x00_bar_list_entry *bar_entry;
  225. int ret;
  226. if (likely(!ieee80211_is_back_req(bar->frame_control)))
  227. return 0;
  228. /*
  229. * Unlike all other frames, the status report for BARs does
  230. * not directly come from the hardware as it is incapable of
  231. * matching a BA to a previously send BAR. The hardware will
  232. * report all BARs as if they weren't acked at all.
  233. *
  234. * Instead the RX-path will scan for incoming BAs and set the
  235. * block_acked flag if it sees one that was likely caused by
  236. * a BAR from us.
  237. *
  238. * Remove remaining BARs here and return their status for
  239. * TX done processing.
  240. */
  241. ret = 0;
  242. rcu_read_lock();
  243. list_for_each_entry_rcu(bar_entry, &rt2x00dev->bar_list, list) {
  244. if (bar_entry->entry != entry)
  245. continue;
  246. spin_lock_bh(&rt2x00dev->bar_list_lock);
  247. /* Return whether this BAR was blockacked or not */
  248. ret = bar_entry->block_acked;
  249. /* Remove the BAR from our checklist */
  250. list_del_rcu(&bar_entry->list);
  251. spin_unlock_bh(&rt2x00dev->bar_list_lock);
  252. kfree_rcu(bar_entry, head);
  253. break;
  254. }
  255. rcu_read_unlock();
  256. return ret;
  257. }
  258. static void rt2x00lib_fill_tx_status(struct rt2x00_dev *rt2x00dev,
  259. struct ieee80211_tx_info *tx_info,
  260. struct skb_frame_desc *skbdesc,
  261. struct txdone_entry_desc *txdesc,
  262. bool success)
  263. {
  264. u8 rate_idx, rate_flags, retry_rates;
  265. int i;
  266. rate_idx = skbdesc->tx_rate_idx;
  267. rate_flags = skbdesc->tx_rate_flags;
  268. retry_rates = test_bit(TXDONE_FALLBACK, &txdesc->flags) ?
  269. (txdesc->retry + 1) : 1;
  270. /*
  271. * Initialize TX status
  272. */
  273. memset(&tx_info->status, 0, sizeof(tx_info->status));
  274. tx_info->status.ack_signal = 0;
  275. /*
  276. * Frame was send with retries, hardware tried
  277. * different rates to send out the frame, at each
  278. * retry it lowered the rate 1 step except when the
  279. * lowest rate was used.
  280. */
  281. for (i = 0; i < retry_rates && i < IEEE80211_TX_MAX_RATES; i++) {
  282. tx_info->status.rates[i].idx = rate_idx - i;
  283. tx_info->status.rates[i].flags = rate_flags;
  284. if (rate_idx - i == 0) {
  285. /*
  286. * The lowest rate (index 0) was used until the
  287. * number of max retries was reached.
  288. */
  289. tx_info->status.rates[i].count = retry_rates - i;
  290. i++;
  291. break;
  292. }
  293. tx_info->status.rates[i].count = 1;
  294. }
  295. if (i < (IEEE80211_TX_MAX_RATES - 1))
  296. tx_info->status.rates[i].idx = -1; /* terminate */
  297. if (test_bit(TXDONE_NO_ACK_REQ, &txdesc->flags))
  298. tx_info->flags |= IEEE80211_TX_CTL_NO_ACK;
  299. if (!(tx_info->flags & IEEE80211_TX_CTL_NO_ACK)) {
  300. if (success)
  301. tx_info->flags |= IEEE80211_TX_STAT_ACK;
  302. else
  303. rt2x00dev->low_level_stats.dot11ACKFailureCount++;
  304. }
  305. /*
  306. * Every single frame has its own tx status, hence report
  307. * every frame as ampdu of size 1.
  308. *
  309. * TODO: if we can find out how many frames were aggregated
  310. * by the hw we could provide the real ampdu_len to mac80211
  311. * which would allow the rc algorithm to better decide on
  312. * which rates are suitable.
  313. */
  314. if (test_bit(TXDONE_AMPDU, &txdesc->flags) ||
  315. tx_info->flags & IEEE80211_TX_CTL_AMPDU) {
  316. tx_info->flags |= IEEE80211_TX_STAT_AMPDU |
  317. IEEE80211_TX_CTL_AMPDU;
  318. tx_info->status.ampdu_len = 1;
  319. tx_info->status.ampdu_ack_len = success ? 1 : 0;
  320. }
  321. if (rate_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
  322. if (success)
  323. rt2x00dev->low_level_stats.dot11RTSSuccessCount++;
  324. else
  325. rt2x00dev->low_level_stats.dot11RTSFailureCount++;
  326. }
  327. }
  328. static void rt2x00lib_clear_entry(struct rt2x00_dev *rt2x00dev,
  329. struct queue_entry *entry)
  330. {
  331. /*
  332. * Make this entry available for reuse.
  333. */
  334. entry->skb = NULL;
  335. entry->flags = 0;
  336. rt2x00dev->ops->lib->clear_entry(entry);
  337. rt2x00queue_index_inc(entry, Q_INDEX_DONE);
  338. /*
  339. * If the data queue was below the threshold before the txdone
  340. * handler we must make sure the packet queue in the mac80211 stack
  341. * is reenabled when the txdone handler has finished. This has to be
  342. * serialized with rt2x00mac_tx(), otherwise we can wake up queue
  343. * before it was stopped.
  344. */
  345. spin_lock_bh(&entry->queue->tx_lock);
  346. if (!rt2x00queue_threshold(entry->queue))
  347. rt2x00queue_unpause_queue(entry->queue);
  348. spin_unlock_bh(&entry->queue->tx_lock);
  349. }
  350. void rt2x00lib_txdone_nomatch(struct queue_entry *entry,
  351. struct txdone_entry_desc *txdesc)
  352. {
  353. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  354. struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
  355. struct ieee80211_tx_info txinfo = {};
  356. bool success;
  357. /*
  358. * Unmap the skb.
  359. */
  360. rt2x00queue_unmap_skb(entry);
  361. /*
  362. * Signal that the TX descriptor is no longer in the skb.
  363. */
  364. skbdesc->flags &= ~SKBDESC_DESC_IN_SKB;
  365. /*
  366. * Send frame to debugfs immediately, after this call is completed
  367. * we are going to overwrite the skb->cb array.
  368. */
  369. rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_TXDONE, entry);
  370. /*
  371. * Determine if the frame has been successfully transmitted and
  372. * remove BARs from our check list while checking for their
  373. * TX status.
  374. */
  375. success =
  376. rt2x00lib_txdone_bar_status(entry) ||
  377. test_bit(TXDONE_SUCCESS, &txdesc->flags);
  378. if (!test_bit(TXDONE_UNKNOWN, &txdesc->flags)) {
  379. /*
  380. * Update TX statistics.
  381. */
  382. rt2x00dev->link.qual.tx_success += success;
  383. rt2x00dev->link.qual.tx_failed += !success;
  384. rt2x00lib_fill_tx_status(rt2x00dev, &txinfo, skbdesc, txdesc,
  385. success);
  386. ieee80211_tx_status_noskb(rt2x00dev->hw, skbdesc->sta, &txinfo);
  387. }
  388. dev_kfree_skb_any(entry->skb);
  389. rt2x00lib_clear_entry(rt2x00dev, entry);
  390. }
  391. EXPORT_SYMBOL_GPL(rt2x00lib_txdone_nomatch);
  392. void rt2x00lib_txdone(struct queue_entry *entry,
  393. struct txdone_entry_desc *txdesc)
  394. {
  395. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  396. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(entry->skb);
  397. struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
  398. u8 skbdesc_flags = skbdesc->flags;
  399. unsigned int header_length;
  400. bool success;
  401. /*
  402. * Unmap the skb.
  403. */
  404. rt2x00queue_unmap_skb(entry);
  405. /*
  406. * Remove the extra tx headroom from the skb.
  407. */
  408. skb_pull(entry->skb, rt2x00dev->extra_tx_headroom);
  409. /*
  410. * Signal that the TX descriptor is no longer in the skb.
  411. */
  412. skbdesc->flags &= ~SKBDESC_DESC_IN_SKB;
  413. /*
  414. * Determine the length of 802.11 header.
  415. */
  416. header_length = ieee80211_get_hdrlen_from_skb(entry->skb);
  417. /*
  418. * Remove L2 padding which was added during
  419. */
  420. if (rt2x00_has_cap_flag(rt2x00dev, REQUIRE_L2PAD))
  421. rt2x00queue_remove_l2pad(entry->skb, header_length);
  422. /*
  423. * If the IV/EIV data was stripped from the frame before it was
  424. * passed to the hardware, we should now reinsert it again because
  425. * mac80211 will expect the same data to be present in the
  426. * frame as it was passed to us.
  427. */
  428. if (rt2x00_has_cap_hw_crypto(rt2x00dev))
  429. rt2x00crypto_tx_insert_iv(entry->skb, header_length);
  430. /*
  431. * Send frame to debugfs immediately, after this call is completed
  432. * we are going to overwrite the skb->cb array.
  433. */
  434. rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_TXDONE, entry);
  435. /*
  436. * Determine if the frame has been successfully transmitted and
  437. * remove BARs from our check list while checking for their
  438. * TX status.
  439. */
  440. success =
  441. rt2x00lib_txdone_bar_status(entry) ||
  442. test_bit(TXDONE_SUCCESS, &txdesc->flags) ||
  443. test_bit(TXDONE_UNKNOWN, &txdesc->flags);
  444. /*
  445. * Update TX statistics.
  446. */
  447. rt2x00dev->link.qual.tx_success += success;
  448. rt2x00dev->link.qual.tx_failed += !success;
  449. rt2x00lib_fill_tx_status(rt2x00dev, tx_info, skbdesc, txdesc, success);
  450. /*
  451. * Only send the status report to mac80211 when it's a frame
  452. * that originated in mac80211. If this was a extra frame coming
  453. * through a mac80211 library call (RTS/CTS) then we should not
  454. * send the status report back.
  455. */
  456. if (!(skbdesc_flags & SKBDESC_NOT_MAC80211)) {
  457. if (rt2x00_has_cap_flag(rt2x00dev, REQUIRE_TASKLET_CONTEXT))
  458. ieee80211_tx_status_skb(rt2x00dev->hw, entry->skb);
  459. else
  460. ieee80211_tx_status_ni(rt2x00dev->hw, entry->skb);
  461. } else {
  462. dev_kfree_skb_any(entry->skb);
  463. }
  464. rt2x00lib_clear_entry(rt2x00dev, entry);
  465. }
  466. EXPORT_SYMBOL_GPL(rt2x00lib_txdone);
  467. void rt2x00lib_txdone_noinfo(struct queue_entry *entry, u32 status)
  468. {
  469. struct txdone_entry_desc txdesc;
  470. txdesc.flags = 0;
  471. __set_bit(status, &txdesc.flags);
  472. txdesc.retry = 0;
  473. rt2x00lib_txdone(entry, &txdesc);
  474. }
  475. EXPORT_SYMBOL_GPL(rt2x00lib_txdone_noinfo);
  476. static u8 *rt2x00lib_find_ie(u8 *data, unsigned int len, u8 ie)
  477. {
  478. struct ieee80211_mgmt *mgmt = (void *)data;
  479. u8 *pos, *end;
  480. pos = (u8 *)mgmt->u.beacon.variable;
  481. end = data + len;
  482. while (pos < end) {
  483. if (pos + 2 + pos[1] > end)
  484. return NULL;
  485. if (pos[0] == ie)
  486. return pos;
  487. pos += 2 + pos[1];
  488. }
  489. return NULL;
  490. }
  491. static void rt2x00lib_sleep(struct work_struct *work)
  492. {
  493. struct rt2x00_dev *rt2x00dev =
  494. container_of(work, struct rt2x00_dev, sleep_work);
  495. if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
  496. return;
  497. /*
  498. * Check again is powersaving is enabled, to prevent races from delayed
  499. * work execution.
  500. */
  501. if (!test_bit(CONFIG_POWERSAVING, &rt2x00dev->flags))
  502. rt2x00lib_config(rt2x00dev, &rt2x00dev->hw->conf,
  503. IEEE80211_CONF_CHANGE_PS);
  504. }
  505. static void rt2x00lib_rxdone_check_ba(struct rt2x00_dev *rt2x00dev,
  506. struct sk_buff *skb,
  507. struct rxdone_entry_desc *rxdesc)
  508. {
  509. struct rt2x00_bar_list_entry *entry;
  510. struct ieee80211_bar *ba = (void *)skb->data;
  511. if (likely(!ieee80211_is_back(ba->frame_control)))
  512. return;
  513. if (rxdesc->size < sizeof(*ba) + FCS_LEN)
  514. return;
  515. rcu_read_lock();
  516. list_for_each_entry_rcu(entry, &rt2x00dev->bar_list, list) {
  517. if (ba->start_seq_num != entry->start_seq_num)
  518. continue;
  519. #define TID_CHECK(a, b) ( \
  520. ((a) & cpu_to_le16(IEEE80211_BAR_CTRL_TID_INFO_MASK)) == \
  521. ((b) & cpu_to_le16(IEEE80211_BAR_CTRL_TID_INFO_MASK))) \
  522. if (!TID_CHECK(ba->control, entry->control))
  523. continue;
  524. #undef TID_CHECK
  525. if (!ether_addr_equal_64bits(ba->ra, entry->ta))
  526. continue;
  527. if (!ether_addr_equal_64bits(ba->ta, entry->ra))
  528. continue;
  529. /* Mark BAR since we received the according BA */
  530. spin_lock_bh(&rt2x00dev->bar_list_lock);
  531. entry->block_acked = 1;
  532. spin_unlock_bh(&rt2x00dev->bar_list_lock);
  533. break;
  534. }
  535. rcu_read_unlock();
  536. }
  537. static void rt2x00lib_rxdone_check_ps(struct rt2x00_dev *rt2x00dev,
  538. struct sk_buff *skb,
  539. struct rxdone_entry_desc *rxdesc)
  540. {
  541. struct ieee80211_hdr *hdr = (void *) skb->data;
  542. struct ieee80211_tim_ie *tim_ie;
  543. u8 *tim;
  544. u8 tim_len;
  545. bool cam;
  546. /* If this is not a beacon, or if mac80211 has no powersaving
  547. * configured, or if the device is already in powersaving mode
  548. * we can exit now. */
  549. if (likely(!ieee80211_is_beacon(hdr->frame_control) ||
  550. !(rt2x00dev->hw->conf.flags & IEEE80211_CONF_PS)))
  551. return;
  552. /* min. beacon length + FCS_LEN */
  553. if (skb->len <= 40 + FCS_LEN)
  554. return;
  555. /* and only beacons from the associated BSSID, please */
  556. if (!(rxdesc->dev_flags & RXDONE_MY_BSS) ||
  557. !rt2x00dev->aid)
  558. return;
  559. rt2x00dev->last_beacon = jiffies;
  560. tim = rt2x00lib_find_ie(skb->data, skb->len - FCS_LEN, WLAN_EID_TIM);
  561. if (!tim)
  562. return;
  563. if (tim[1] < sizeof(*tim_ie))
  564. return;
  565. tim_len = tim[1];
  566. tim_ie = (struct ieee80211_tim_ie *) &tim[2];
  567. /* Check whenever the PHY can be turned off again. */
  568. /* 1. What about buffered unicast traffic for our AID? */
  569. cam = ieee80211_check_tim(tim_ie, tim_len, rt2x00dev->aid, false);
  570. /* 2. Maybe the AP wants to send multicast/broadcast data? */
  571. cam |= (tim_ie->bitmap_ctrl & 0x01);
  572. if (!cam && !test_bit(CONFIG_POWERSAVING, &rt2x00dev->flags))
  573. queue_work(rt2x00dev->workqueue, &rt2x00dev->sleep_work);
  574. }
  575. static int rt2x00lib_rxdone_read_signal(struct rt2x00_dev *rt2x00dev,
  576. struct rxdone_entry_desc *rxdesc)
  577. {
  578. struct ieee80211_supported_band *sband;
  579. const struct rt2x00_rate *rate;
  580. unsigned int i;
  581. int signal = rxdesc->signal;
  582. int type = (rxdesc->dev_flags & RXDONE_SIGNAL_MASK);
  583. switch (rxdesc->rate_mode) {
  584. case RATE_MODE_CCK:
  585. case RATE_MODE_OFDM:
  586. /*
  587. * For non-HT rates the MCS value needs to contain the
  588. * actually used rate modulation (CCK or OFDM).
  589. */
  590. if (rxdesc->dev_flags & RXDONE_SIGNAL_MCS)
  591. signal = RATE_MCS(rxdesc->rate_mode, signal);
  592. sband = &rt2x00dev->bands[rt2x00dev->curr_band];
  593. for (i = 0; i < sband->n_bitrates; i++) {
  594. rate = rt2x00_get_rate(sband->bitrates[i].hw_value);
  595. if (((type == RXDONE_SIGNAL_PLCP) &&
  596. (rate->plcp == signal)) ||
  597. ((type == RXDONE_SIGNAL_BITRATE) &&
  598. (rate->bitrate == signal)) ||
  599. ((type == RXDONE_SIGNAL_MCS) &&
  600. (rate->mcs == signal))) {
  601. return i;
  602. }
  603. }
  604. break;
  605. case RATE_MODE_HT_MIX:
  606. case RATE_MODE_HT_GREENFIELD:
  607. if (signal >= 0 && signal <= 76)
  608. return signal;
  609. break;
  610. default:
  611. break;
  612. }
  613. rt2x00_warn(rt2x00dev, "Frame received with unrecognized signal, mode=0x%.4x, signal=0x%.4x, type=%d\n",
  614. rxdesc->rate_mode, signal, type);
  615. return 0;
  616. }
  617. void rt2x00lib_rxdone(struct queue_entry *entry, gfp_t gfp)
  618. {
  619. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  620. struct rxdone_entry_desc rxdesc;
  621. struct sk_buff *skb;
  622. struct ieee80211_rx_status *rx_status;
  623. unsigned int header_length;
  624. int rate_idx;
  625. if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags) ||
  626. !test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  627. goto submit_entry;
  628. if (test_bit(ENTRY_DATA_IO_FAILED, &entry->flags))
  629. goto submit_entry;
  630. /*
  631. * Allocate a new sk_buffer. If no new buffer available, drop the
  632. * received frame and reuse the existing buffer.
  633. */
  634. skb = rt2x00queue_alloc_rxskb(entry, gfp);
  635. if (!skb)
  636. goto submit_entry;
  637. /*
  638. * Unmap the skb.
  639. */
  640. rt2x00queue_unmap_skb(entry);
  641. /*
  642. * Extract the RXD details.
  643. */
  644. memset(&rxdesc, 0, sizeof(rxdesc));
  645. rt2x00dev->ops->lib->fill_rxdone(entry, &rxdesc);
  646. /*
  647. * Check for valid size in case we get corrupted descriptor from
  648. * hardware.
  649. */
  650. if (unlikely(rxdesc.size == 0 ||
  651. rxdesc.size > entry->queue->data_size)) {
  652. rt2x00_err(rt2x00dev, "Wrong frame size %d max %d\n",
  653. rxdesc.size, entry->queue->data_size);
  654. dev_kfree_skb(entry->skb);
  655. goto renew_skb;
  656. }
  657. /*
  658. * The data behind the ieee80211 header must be
  659. * aligned on a 4 byte boundary.
  660. */
  661. header_length = ieee80211_get_hdrlen_from_skb(entry->skb);
  662. /*
  663. * Hardware might have stripped the IV/EIV/ICV data,
  664. * in that case it is possible that the data was
  665. * provided separately (through hardware descriptor)
  666. * in which case we should reinsert the data into the frame.
  667. */
  668. if ((rxdesc.dev_flags & RXDONE_CRYPTO_IV) &&
  669. (rxdesc.flags & RX_FLAG_IV_STRIPPED))
  670. rt2x00crypto_rx_insert_iv(entry->skb, header_length,
  671. &rxdesc);
  672. else if (header_length &&
  673. (rxdesc.size > header_length) &&
  674. (rxdesc.dev_flags & RXDONE_L2PAD))
  675. rt2x00queue_remove_l2pad(entry->skb, header_length);
  676. /* Trim buffer to correct size */
  677. skb_trim(entry->skb, rxdesc.size);
  678. /*
  679. * Translate the signal to the correct bitrate index.
  680. */
  681. rate_idx = rt2x00lib_rxdone_read_signal(rt2x00dev, &rxdesc);
  682. if (rxdesc.rate_mode == RATE_MODE_HT_MIX ||
  683. rxdesc.rate_mode == RATE_MODE_HT_GREENFIELD)
  684. rxdesc.encoding = RX_ENC_HT;
  685. /*
  686. * Check if this is a beacon, and more frames have been
  687. * buffered while we were in powersaving mode.
  688. */
  689. rt2x00lib_rxdone_check_ps(rt2x00dev, entry->skb, &rxdesc);
  690. /*
  691. * Check for incoming BlockAcks to match to the BlockAckReqs
  692. * we've send out.
  693. */
  694. rt2x00lib_rxdone_check_ba(rt2x00dev, entry->skb, &rxdesc);
  695. /*
  696. * Update extra components
  697. */
  698. rt2x00link_update_stats(rt2x00dev, entry->skb, &rxdesc);
  699. rt2x00debug_update_crypto(rt2x00dev, &rxdesc);
  700. rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_RXDONE, entry);
  701. /*
  702. * Initialize RX status information, and send frame
  703. * to mac80211.
  704. */
  705. rx_status = IEEE80211_SKB_RXCB(entry->skb);
  706. /* Ensure that all fields of rx_status are initialized
  707. * properly. The skb->cb array was used for driver
  708. * specific informations, so rx_status might contain
  709. * garbage.
  710. */
  711. memset(rx_status, 0, sizeof(*rx_status));
  712. rx_status->mactime = rxdesc.timestamp;
  713. rx_status->band = rt2x00dev->curr_band;
  714. rx_status->freq = rt2x00dev->curr_freq;
  715. rx_status->rate_idx = rate_idx;
  716. rx_status->signal = rxdesc.rssi;
  717. rx_status->flag = rxdesc.flags;
  718. rx_status->enc_flags = rxdesc.enc_flags;
  719. rx_status->encoding = rxdesc.encoding;
  720. rx_status->bw = rxdesc.bw;
  721. rx_status->antenna = rt2x00dev->link.ant.active.rx;
  722. ieee80211_rx_ni(rt2x00dev->hw, entry->skb);
  723. renew_skb:
  724. /*
  725. * Replace the skb with the freshly allocated one.
  726. */
  727. entry->skb = skb;
  728. submit_entry:
  729. entry->flags = 0;
  730. rt2x00queue_index_inc(entry, Q_INDEX_DONE);
  731. if (test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags) &&
  732. test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  733. rt2x00dev->ops->lib->clear_entry(entry);
  734. }
  735. EXPORT_SYMBOL_GPL(rt2x00lib_rxdone);
  736. /*
  737. * Driver initialization handlers.
  738. */
  739. const struct rt2x00_rate rt2x00_supported_rates[12] = {
  740. {
  741. .flags = DEV_RATE_CCK,
  742. .bitrate = 10,
  743. .ratemask = BIT(0),
  744. .plcp = 0x00,
  745. .mcs = RATE_MCS(RATE_MODE_CCK, 0),
  746. },
  747. {
  748. .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE,
  749. .bitrate = 20,
  750. .ratemask = BIT(1),
  751. .plcp = 0x01,
  752. .mcs = RATE_MCS(RATE_MODE_CCK, 1),
  753. },
  754. {
  755. .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE,
  756. .bitrate = 55,
  757. .ratemask = BIT(2),
  758. .plcp = 0x02,
  759. .mcs = RATE_MCS(RATE_MODE_CCK, 2),
  760. },
  761. {
  762. .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE,
  763. .bitrate = 110,
  764. .ratemask = BIT(3),
  765. .plcp = 0x03,
  766. .mcs = RATE_MCS(RATE_MODE_CCK, 3),
  767. },
  768. {
  769. .flags = DEV_RATE_OFDM,
  770. .bitrate = 60,
  771. .ratemask = BIT(4),
  772. .plcp = 0x0b,
  773. .mcs = RATE_MCS(RATE_MODE_OFDM, 0),
  774. },
  775. {
  776. .flags = DEV_RATE_OFDM,
  777. .bitrate = 90,
  778. .ratemask = BIT(5),
  779. .plcp = 0x0f,
  780. .mcs = RATE_MCS(RATE_MODE_OFDM, 1),
  781. },
  782. {
  783. .flags = DEV_RATE_OFDM,
  784. .bitrate = 120,
  785. .ratemask = BIT(6),
  786. .plcp = 0x0a,
  787. .mcs = RATE_MCS(RATE_MODE_OFDM, 2),
  788. },
  789. {
  790. .flags = DEV_RATE_OFDM,
  791. .bitrate = 180,
  792. .ratemask = BIT(7),
  793. .plcp = 0x0e,
  794. .mcs = RATE_MCS(RATE_MODE_OFDM, 3),
  795. },
  796. {
  797. .flags = DEV_RATE_OFDM,
  798. .bitrate = 240,
  799. .ratemask = BIT(8),
  800. .plcp = 0x09,
  801. .mcs = RATE_MCS(RATE_MODE_OFDM, 4),
  802. },
  803. {
  804. .flags = DEV_RATE_OFDM,
  805. .bitrate = 360,
  806. .ratemask = BIT(9),
  807. .plcp = 0x0d,
  808. .mcs = RATE_MCS(RATE_MODE_OFDM, 5),
  809. },
  810. {
  811. .flags = DEV_RATE_OFDM,
  812. .bitrate = 480,
  813. .ratemask = BIT(10),
  814. .plcp = 0x08,
  815. .mcs = RATE_MCS(RATE_MODE_OFDM, 6),
  816. },
  817. {
  818. .flags = DEV_RATE_OFDM,
  819. .bitrate = 540,
  820. .ratemask = BIT(11),
  821. .plcp = 0x0c,
  822. .mcs = RATE_MCS(RATE_MODE_OFDM, 7),
  823. },
  824. };
  825. static void rt2x00lib_channel(struct ieee80211_channel *entry,
  826. const int channel, const int tx_power,
  827. const int value)
  828. {
  829. /* XXX: this assumption about the band is wrong for 802.11j */
  830. entry->band = channel <= 14 ? NL80211_BAND_2GHZ : NL80211_BAND_5GHZ;
  831. entry->center_freq = ieee80211_channel_to_frequency(channel,
  832. entry->band);
  833. entry->hw_value = value;
  834. entry->max_power = tx_power;
  835. entry->max_antenna_gain = 0xff;
  836. }
  837. static void rt2x00lib_rate(struct ieee80211_rate *entry,
  838. const u16 index, const struct rt2x00_rate *rate)
  839. {
  840. entry->flags = 0;
  841. entry->bitrate = rate->bitrate;
  842. entry->hw_value = index;
  843. entry->hw_value_short = index;
  844. if (rate->flags & DEV_RATE_SHORT_PREAMBLE)
  845. entry->flags |= IEEE80211_RATE_SHORT_PREAMBLE;
  846. }
  847. int rt2x00lib_set_mac_address(struct rt2x00_dev *rt2x00dev, u8 *eeprom_mac_addr)
  848. {
  849. int ret;
  850. ret = of_get_mac_address(rt2x00dev->dev->of_node, eeprom_mac_addr);
  851. if (ret == -EPROBE_DEFER)
  852. return ret;
  853. if (!is_valid_ether_addr(eeprom_mac_addr)) {
  854. eth_random_addr(eeprom_mac_addr);
  855. rt2x00_eeprom_dbg(rt2x00dev, "MAC: %pM\n", eeprom_mac_addr);
  856. }
  857. return 0;
  858. }
  859. EXPORT_SYMBOL_GPL(rt2x00lib_set_mac_address);
  860. static int rt2x00lib_probe_hw_modes(struct rt2x00_dev *rt2x00dev,
  861. struct hw_mode_spec *spec)
  862. {
  863. struct ieee80211_hw *hw = rt2x00dev->hw;
  864. struct ieee80211_channel *channels;
  865. struct ieee80211_rate *rates;
  866. unsigned int num_rates;
  867. unsigned int i;
  868. num_rates = 0;
  869. if (spec->supported_rates & SUPPORT_RATE_CCK)
  870. num_rates += 4;
  871. if (spec->supported_rates & SUPPORT_RATE_OFDM)
  872. num_rates += 8;
  873. channels = kzalloc_objs(*channels, spec->num_channels);
  874. if (!channels)
  875. return -ENOMEM;
  876. rates = kzalloc_objs(*rates, num_rates);
  877. if (!rates)
  878. goto exit_free_channels;
  879. /*
  880. * Initialize Rate list.
  881. */
  882. for (i = 0; i < num_rates; i++)
  883. rt2x00lib_rate(&rates[i], i, rt2x00_get_rate(i));
  884. /*
  885. * Initialize Channel list.
  886. */
  887. for (i = 0; i < spec->num_channels; i++) {
  888. rt2x00lib_channel(&channels[i],
  889. spec->channels[i].channel,
  890. spec->channels_info[i].max_power, i);
  891. }
  892. /*
  893. * Intitialize 802.11b, 802.11g
  894. * Rates: CCK, OFDM.
  895. * Channels: 2.4 GHz
  896. */
  897. if (spec->supported_bands & SUPPORT_BAND_2GHZ) {
  898. rt2x00dev->bands[NL80211_BAND_2GHZ].n_channels = 14;
  899. rt2x00dev->bands[NL80211_BAND_2GHZ].n_bitrates = num_rates;
  900. rt2x00dev->bands[NL80211_BAND_2GHZ].channels = channels;
  901. rt2x00dev->bands[NL80211_BAND_2GHZ].bitrates = rates;
  902. hw->wiphy->bands[NL80211_BAND_2GHZ] =
  903. &rt2x00dev->bands[NL80211_BAND_2GHZ];
  904. memcpy(&rt2x00dev->bands[NL80211_BAND_2GHZ].ht_cap,
  905. &spec->ht, sizeof(spec->ht));
  906. }
  907. /*
  908. * Intitialize 802.11a
  909. * Rates: OFDM.
  910. * Channels: OFDM, UNII, HiperLAN2.
  911. */
  912. if (spec->supported_bands & SUPPORT_BAND_5GHZ) {
  913. rt2x00dev->bands[NL80211_BAND_5GHZ].n_channels =
  914. spec->num_channels - 14;
  915. rt2x00dev->bands[NL80211_BAND_5GHZ].n_bitrates =
  916. num_rates - 4;
  917. rt2x00dev->bands[NL80211_BAND_5GHZ].channels = &channels[14];
  918. rt2x00dev->bands[NL80211_BAND_5GHZ].bitrates = &rates[4];
  919. hw->wiphy->bands[NL80211_BAND_5GHZ] =
  920. &rt2x00dev->bands[NL80211_BAND_5GHZ];
  921. memcpy(&rt2x00dev->bands[NL80211_BAND_5GHZ].ht_cap,
  922. &spec->ht, sizeof(spec->ht));
  923. }
  924. return 0;
  925. exit_free_channels:
  926. kfree(channels);
  927. rt2x00_err(rt2x00dev, "Allocation ieee80211 modes failed\n");
  928. return -ENOMEM;
  929. }
  930. static void rt2x00lib_remove_hw(struct rt2x00_dev *rt2x00dev)
  931. {
  932. if (test_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags))
  933. ieee80211_unregister_hw(rt2x00dev->hw);
  934. if (likely(rt2x00dev->hw->wiphy->bands[NL80211_BAND_2GHZ])) {
  935. kfree(rt2x00dev->hw->wiphy->bands[NL80211_BAND_2GHZ]->channels);
  936. kfree(rt2x00dev->hw->wiphy->bands[NL80211_BAND_2GHZ]->bitrates);
  937. rt2x00dev->hw->wiphy->bands[NL80211_BAND_2GHZ] = NULL;
  938. rt2x00dev->hw->wiphy->bands[NL80211_BAND_5GHZ] = NULL;
  939. }
  940. kfree(rt2x00dev->spec.channels_info);
  941. kfree(rt2x00dev->chan_survey);
  942. }
  943. static const struct ieee80211_tpt_blink rt2x00_tpt_blink[] = {
  944. { .throughput = 0 * 1024, .blink_time = 334 },
  945. { .throughput = 1 * 1024, .blink_time = 260 },
  946. { .throughput = 2 * 1024, .blink_time = 220 },
  947. { .throughput = 5 * 1024, .blink_time = 190 },
  948. { .throughput = 10 * 1024, .blink_time = 170 },
  949. { .throughput = 25 * 1024, .blink_time = 150 },
  950. { .throughput = 54 * 1024, .blink_time = 130 },
  951. { .throughput = 120 * 1024, .blink_time = 110 },
  952. { .throughput = 265 * 1024, .blink_time = 80 },
  953. { .throughput = 586 * 1024, .blink_time = 50 },
  954. };
  955. static int rt2x00lib_probe_hw(struct rt2x00_dev *rt2x00dev)
  956. {
  957. struct hw_mode_spec *spec = &rt2x00dev->spec;
  958. int status;
  959. if (test_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags))
  960. return 0;
  961. /*
  962. * Initialize HW modes.
  963. */
  964. status = rt2x00lib_probe_hw_modes(rt2x00dev, spec);
  965. if (status)
  966. return status;
  967. /*
  968. * Initialize HW fields.
  969. */
  970. rt2x00dev->hw->queues = rt2x00dev->ops->tx_queues;
  971. /*
  972. * Initialize extra TX headroom required.
  973. */
  974. rt2x00dev->hw->extra_tx_headroom =
  975. max_t(unsigned int, IEEE80211_TX_STATUS_HEADROOM,
  976. rt2x00dev->extra_tx_headroom);
  977. /*
  978. * Take TX headroom required for alignment into account.
  979. */
  980. if (rt2x00_has_cap_flag(rt2x00dev, REQUIRE_L2PAD))
  981. rt2x00dev->hw->extra_tx_headroom += RT2X00_L2PAD_SIZE;
  982. else if (rt2x00_has_cap_flag(rt2x00dev, REQUIRE_DMA))
  983. rt2x00dev->hw->extra_tx_headroom += RT2X00_ALIGN_SIZE;
  984. /*
  985. * Tell mac80211 about the size of our private STA structure.
  986. */
  987. rt2x00dev->hw->sta_data_size = sizeof(struct rt2x00_sta);
  988. /*
  989. * Allocate tx status FIFO for driver use.
  990. */
  991. if (rt2x00_has_cap_flag(rt2x00dev, REQUIRE_TXSTATUS_FIFO)) {
  992. /*
  993. * Allocate the txstatus fifo. In the worst case the tx
  994. * status fifo has to hold the tx status of all entries
  995. * in all tx queues. Hence, calculate the kfifo size as
  996. * tx_queues * entry_num and round up to the nearest
  997. * power of 2.
  998. */
  999. int kfifo_size =
  1000. roundup_pow_of_two(rt2x00dev->ops->tx_queues *
  1001. rt2x00dev->tx->limit *
  1002. sizeof(u32));
  1003. status = kfifo_alloc(&rt2x00dev->txstatus_fifo, kfifo_size,
  1004. GFP_KERNEL);
  1005. if (status)
  1006. return status;
  1007. }
  1008. /*
  1009. * Initialize tasklets if used by the driver. Tasklets are
  1010. * disabled until the interrupts are turned on. The driver
  1011. * has to handle that.
  1012. */
  1013. #define RT2X00_TASKLET_INIT(taskletname) \
  1014. if (rt2x00dev->ops->lib->taskletname) { \
  1015. tasklet_setup(&rt2x00dev->taskletname, \
  1016. rt2x00dev->ops->lib->taskletname); \
  1017. }
  1018. RT2X00_TASKLET_INIT(txstatus_tasklet);
  1019. RT2X00_TASKLET_INIT(pretbtt_tasklet);
  1020. RT2X00_TASKLET_INIT(tbtt_tasklet);
  1021. RT2X00_TASKLET_INIT(rxdone_tasklet);
  1022. RT2X00_TASKLET_INIT(autowake_tasklet);
  1023. #undef RT2X00_TASKLET_INIT
  1024. ieee80211_create_tpt_led_trigger(rt2x00dev->hw,
  1025. IEEE80211_TPT_LEDTRIG_FL_RADIO,
  1026. rt2x00_tpt_blink,
  1027. ARRAY_SIZE(rt2x00_tpt_blink));
  1028. /*
  1029. * Register HW.
  1030. */
  1031. status = ieee80211_register_hw(rt2x00dev->hw);
  1032. if (status)
  1033. return status;
  1034. set_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags);
  1035. return 0;
  1036. }
  1037. /*
  1038. * Initialization/uninitialization handlers.
  1039. */
  1040. static void rt2x00lib_uninitialize(struct rt2x00_dev *rt2x00dev)
  1041. {
  1042. if (!test_and_clear_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags))
  1043. return;
  1044. /*
  1045. * Stop rfkill polling.
  1046. */
  1047. if (rt2x00_has_cap_flag(rt2x00dev, REQUIRE_DELAYED_RFKILL))
  1048. rt2x00rfkill_unregister(rt2x00dev);
  1049. /*
  1050. * Allow the HW to uninitialize.
  1051. */
  1052. rt2x00dev->ops->lib->uninitialize(rt2x00dev);
  1053. /*
  1054. * Free allocated queue entries.
  1055. */
  1056. rt2x00queue_uninitialize(rt2x00dev);
  1057. }
  1058. static int rt2x00lib_initialize(struct rt2x00_dev *rt2x00dev)
  1059. {
  1060. int status;
  1061. if (test_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags))
  1062. return 0;
  1063. /*
  1064. * Allocate all queue entries.
  1065. */
  1066. status = rt2x00queue_initialize(rt2x00dev);
  1067. if (status)
  1068. return status;
  1069. /*
  1070. * Initialize the device.
  1071. */
  1072. status = rt2x00dev->ops->lib->initialize(rt2x00dev);
  1073. if (status) {
  1074. rt2x00queue_uninitialize(rt2x00dev);
  1075. return status;
  1076. }
  1077. set_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags);
  1078. /*
  1079. * Start rfkill polling.
  1080. */
  1081. if (rt2x00_has_cap_flag(rt2x00dev, REQUIRE_DELAYED_RFKILL))
  1082. rt2x00rfkill_register(rt2x00dev);
  1083. return 0;
  1084. }
  1085. int rt2x00lib_start(struct rt2x00_dev *rt2x00dev)
  1086. {
  1087. int retval = 0;
  1088. /*
  1089. * If this is the first interface which is added,
  1090. * we should load the firmware now.
  1091. */
  1092. retval = rt2x00lib_load_firmware(rt2x00dev);
  1093. if (retval)
  1094. goto out;
  1095. /*
  1096. * Initialize the device.
  1097. */
  1098. retval = rt2x00lib_initialize(rt2x00dev);
  1099. if (retval)
  1100. goto out;
  1101. rt2x00dev->intf_ap_count = 0;
  1102. rt2x00dev->intf_sta_count = 0;
  1103. rt2x00dev->intf_associated = 0;
  1104. rt2x00dev->intf_beaconing = 0;
  1105. /* Enable the radio */
  1106. retval = rt2x00lib_enable_radio(rt2x00dev);
  1107. if (retval)
  1108. goto out;
  1109. set_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags);
  1110. out:
  1111. return retval;
  1112. }
  1113. void rt2x00lib_stop(struct rt2x00_dev *rt2x00dev)
  1114. {
  1115. if (!test_and_clear_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags))
  1116. return;
  1117. /*
  1118. * Perhaps we can add something smarter here,
  1119. * but for now just disabling the radio should do.
  1120. */
  1121. rt2x00lib_disable_radio(rt2x00dev);
  1122. rt2x00dev->intf_ap_count = 0;
  1123. rt2x00dev->intf_sta_count = 0;
  1124. rt2x00dev->intf_associated = 0;
  1125. rt2x00dev->intf_beaconing = 0;
  1126. }
  1127. static inline void rt2x00lib_set_if_combinations(struct rt2x00_dev *rt2x00dev)
  1128. {
  1129. struct ieee80211_iface_limit *if_limit;
  1130. struct ieee80211_iface_combination *if_combination;
  1131. if (rt2x00dev->ops->max_ap_intf < 2)
  1132. return;
  1133. /*
  1134. * Build up AP interface limits structure.
  1135. */
  1136. if_limit = &rt2x00dev->if_limits_ap;
  1137. if_limit->max = rt2x00dev->ops->max_ap_intf;
  1138. if_limit->types = BIT(NL80211_IFTYPE_AP);
  1139. #ifdef CONFIG_MAC80211_MESH
  1140. if_limit->types |= BIT(NL80211_IFTYPE_MESH_POINT);
  1141. #endif
  1142. /*
  1143. * Build up AP interface combinations structure.
  1144. */
  1145. if_combination = &rt2x00dev->if_combinations[IF_COMB_AP];
  1146. if_combination->limits = if_limit;
  1147. if_combination->n_limits = 1;
  1148. if_combination->max_interfaces = if_limit->max;
  1149. if_combination->num_different_channels = 1;
  1150. /*
  1151. * Finally, specify the possible combinations to mac80211.
  1152. */
  1153. rt2x00dev->hw->wiphy->iface_combinations = rt2x00dev->if_combinations;
  1154. rt2x00dev->hw->wiphy->n_iface_combinations = 1;
  1155. }
  1156. static unsigned int rt2x00dev_extra_tx_headroom(struct rt2x00_dev *rt2x00dev)
  1157. {
  1158. if (WARN_ON(!rt2x00dev->tx))
  1159. return 0;
  1160. if (rt2x00_is_usb(rt2x00dev))
  1161. return rt2x00dev->tx[0].winfo_size + rt2x00dev->tx[0].desc_size;
  1162. return rt2x00dev->tx[0].winfo_size;
  1163. }
  1164. /*
  1165. * driver allocation handlers.
  1166. */
  1167. int rt2x00lib_probe_dev(struct rt2x00_dev *rt2x00dev)
  1168. {
  1169. int retval = -ENOMEM;
  1170. /*
  1171. * Set possible interface combinations.
  1172. */
  1173. rt2x00lib_set_if_combinations(rt2x00dev);
  1174. /*
  1175. * Allocate the driver data memory, if necessary.
  1176. */
  1177. if (rt2x00dev->ops->drv_data_size > 0) {
  1178. rt2x00dev->drv_data = kzalloc(rt2x00dev->ops->drv_data_size,
  1179. GFP_KERNEL);
  1180. if (!rt2x00dev->drv_data) {
  1181. retval = -ENOMEM;
  1182. goto exit;
  1183. }
  1184. }
  1185. spin_lock_init(&rt2x00dev->irqmask_lock);
  1186. mutex_init(&rt2x00dev->csr_mutex);
  1187. mutex_init(&rt2x00dev->conf_mutex);
  1188. INIT_LIST_HEAD(&rt2x00dev->bar_list);
  1189. spin_lock_init(&rt2x00dev->bar_list_lock);
  1190. hrtimer_setup(&rt2x00dev->txstatus_timer, hrtimer_dummy_timeout, CLOCK_MONOTONIC,
  1191. HRTIMER_MODE_REL);
  1192. set_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
  1193. /*
  1194. * Make room for rt2x00_intf inside the per-interface
  1195. * structure ieee80211_vif.
  1196. */
  1197. rt2x00dev->hw->vif_data_size = sizeof(struct rt2x00_intf);
  1198. /*
  1199. * rt2x00 devices can only use the last n bits of the MAC address
  1200. * for virtual interfaces.
  1201. */
  1202. rt2x00dev->hw->wiphy->addr_mask[ETH_ALEN - 1] =
  1203. (rt2x00dev->ops->max_ap_intf - 1);
  1204. /*
  1205. * Initialize work.
  1206. */
  1207. rt2x00dev->workqueue =
  1208. alloc_ordered_workqueue("%s", 0, wiphy_name(rt2x00dev->hw->wiphy));
  1209. if (!rt2x00dev->workqueue) {
  1210. retval = -ENOMEM;
  1211. goto exit;
  1212. }
  1213. INIT_WORK(&rt2x00dev->intf_work, rt2x00lib_intf_scheduled);
  1214. INIT_DELAYED_WORK(&rt2x00dev->autowakeup_work, rt2x00lib_autowakeup);
  1215. INIT_WORK(&rt2x00dev->sleep_work, rt2x00lib_sleep);
  1216. /*
  1217. * Let the driver probe the device to detect the capabilities.
  1218. */
  1219. retval = rt2x00dev->ops->lib->probe_hw(rt2x00dev);
  1220. if (retval) {
  1221. rt2x00_err(rt2x00dev, "Failed to allocate device\n");
  1222. goto exit;
  1223. }
  1224. /*
  1225. * Allocate queue array.
  1226. */
  1227. retval = rt2x00queue_allocate(rt2x00dev);
  1228. if (retval)
  1229. goto exit;
  1230. /* Cache TX headroom value */
  1231. rt2x00dev->extra_tx_headroom = rt2x00dev_extra_tx_headroom(rt2x00dev);
  1232. /*
  1233. * Determine which operating modes are supported, all modes
  1234. * which require beaconing, depend on the availability of
  1235. * beacon entries.
  1236. */
  1237. rt2x00dev->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
  1238. if (rt2x00dev->bcn->limit > 0)
  1239. rt2x00dev->hw->wiphy->interface_modes |=
  1240. BIT(NL80211_IFTYPE_ADHOC) |
  1241. #ifdef CONFIG_MAC80211_MESH
  1242. BIT(NL80211_IFTYPE_MESH_POINT) |
  1243. #endif
  1244. BIT(NL80211_IFTYPE_AP);
  1245. rt2x00dev->hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
  1246. wiphy_ext_feature_set(rt2x00dev->hw->wiphy,
  1247. NL80211_EXT_FEATURE_CQM_RSSI_LIST);
  1248. /*
  1249. * Initialize ieee80211 structure.
  1250. */
  1251. retval = rt2x00lib_probe_hw(rt2x00dev);
  1252. if (retval) {
  1253. rt2x00_err(rt2x00dev, "Failed to initialize hw\n");
  1254. goto exit;
  1255. }
  1256. /*
  1257. * Register extra components.
  1258. */
  1259. rt2x00link_register(rt2x00dev);
  1260. rt2x00leds_register(rt2x00dev);
  1261. rt2x00debug_register(rt2x00dev);
  1262. /*
  1263. * Start rfkill polling.
  1264. */
  1265. if (!rt2x00_has_cap_flag(rt2x00dev, REQUIRE_DELAYED_RFKILL))
  1266. rt2x00rfkill_register(rt2x00dev);
  1267. return 0;
  1268. exit:
  1269. rt2x00lib_remove_dev(rt2x00dev);
  1270. return retval;
  1271. }
  1272. EXPORT_SYMBOL_GPL(rt2x00lib_probe_dev);
  1273. void rt2x00lib_remove_dev(struct rt2x00_dev *rt2x00dev)
  1274. {
  1275. clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
  1276. /*
  1277. * Stop rfkill polling.
  1278. */
  1279. if (!rt2x00_has_cap_flag(rt2x00dev, REQUIRE_DELAYED_RFKILL))
  1280. rt2x00rfkill_unregister(rt2x00dev);
  1281. /*
  1282. * Disable radio.
  1283. */
  1284. rt2x00lib_disable_radio(rt2x00dev);
  1285. /*
  1286. * Stop all work.
  1287. */
  1288. cancel_work_sync(&rt2x00dev->intf_work);
  1289. cancel_delayed_work_sync(&rt2x00dev->autowakeup_work);
  1290. cancel_work_sync(&rt2x00dev->sleep_work);
  1291. hrtimer_cancel(&rt2x00dev->txstatus_timer);
  1292. /*
  1293. * Kill the tx status tasklet.
  1294. */
  1295. tasklet_kill(&rt2x00dev->txstatus_tasklet);
  1296. tasklet_kill(&rt2x00dev->pretbtt_tasklet);
  1297. tasklet_kill(&rt2x00dev->tbtt_tasklet);
  1298. tasklet_kill(&rt2x00dev->rxdone_tasklet);
  1299. tasklet_kill(&rt2x00dev->autowake_tasklet);
  1300. /*
  1301. * Uninitialize device.
  1302. */
  1303. rt2x00lib_uninitialize(rt2x00dev);
  1304. if (rt2x00dev->workqueue)
  1305. destroy_workqueue(rt2x00dev->workqueue);
  1306. /*
  1307. * Free the tx status fifo.
  1308. */
  1309. kfifo_free(&rt2x00dev->txstatus_fifo);
  1310. /*
  1311. * Free extra components
  1312. */
  1313. rt2x00debug_deregister(rt2x00dev);
  1314. rt2x00leds_unregister(rt2x00dev);
  1315. /*
  1316. * Free ieee80211_hw memory.
  1317. */
  1318. rt2x00lib_remove_hw(rt2x00dev);
  1319. /*
  1320. * Free firmware image.
  1321. */
  1322. rt2x00lib_free_firmware(rt2x00dev);
  1323. /*
  1324. * Free queue structures.
  1325. */
  1326. rt2x00queue_free(rt2x00dev);
  1327. /*
  1328. * Free the driver data.
  1329. */
  1330. kfree(rt2x00dev->drv_data);
  1331. }
  1332. EXPORT_SYMBOL_GPL(rt2x00lib_remove_dev);
  1333. /*
  1334. * Device state handlers
  1335. */
  1336. int rt2x00lib_suspend(struct rt2x00_dev *rt2x00dev)
  1337. {
  1338. rt2x00_dbg(rt2x00dev, "Going to sleep\n");
  1339. /*
  1340. * Prevent mac80211 from accessing driver while suspended.
  1341. */
  1342. if (!test_and_clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
  1343. return 0;
  1344. /*
  1345. * Cleanup as much as possible.
  1346. */
  1347. rt2x00lib_uninitialize(rt2x00dev);
  1348. /*
  1349. * Suspend/disable extra components.
  1350. */
  1351. rt2x00leds_suspend(rt2x00dev);
  1352. rt2x00debug_deregister(rt2x00dev);
  1353. /*
  1354. * Set device mode to sleep for power management,
  1355. * on some hardware this call seems to consistently fail.
  1356. * From the specifications it is hard to tell why it fails,
  1357. * and if this is a "bad thing".
  1358. * Overall it is safe to just ignore the failure and
  1359. * continue suspending. The only downside is that the
  1360. * device will not be in optimal power save mode, but with
  1361. * the radio and the other components already disabled the
  1362. * device is as good as disabled.
  1363. */
  1364. if (rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_SLEEP))
  1365. rt2x00_warn(rt2x00dev, "Device failed to enter sleep state, continue suspending\n");
  1366. return 0;
  1367. }
  1368. EXPORT_SYMBOL_GPL(rt2x00lib_suspend);
  1369. int rt2x00lib_resume(struct rt2x00_dev *rt2x00dev)
  1370. {
  1371. rt2x00_dbg(rt2x00dev, "Waking up\n");
  1372. /*
  1373. * Restore/enable extra components.
  1374. */
  1375. rt2x00debug_register(rt2x00dev);
  1376. rt2x00leds_resume(rt2x00dev);
  1377. /*
  1378. * We are ready again to receive requests from mac80211.
  1379. */
  1380. set_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
  1381. return 0;
  1382. }
  1383. EXPORT_SYMBOL_GPL(rt2x00lib_resume);
  1384. /*
  1385. * rt2x00lib module information.
  1386. */
  1387. MODULE_AUTHOR(DRV_PROJECT);
  1388. MODULE_VERSION(DRV_VERSION);
  1389. MODULE_DESCRIPTION("rt2x00 library");
  1390. MODULE_LICENSE("GPL");