rt2x00dev.c 41 KB

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