rt2x00dev.c 41 KB

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