rt2x00mac.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. /*
  2. Copyright (C) 2004 - 2008 rt2x00 SourceForge Project
  3. <http://rt2x00.serialmonkey.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the
  14. Free Software Foundation, Inc.,
  15. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. */
  17. /*
  18. Module: rt2x00mac
  19. Abstract: rt2x00 generic mac80211 routines.
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include "rt2x00.h"
  24. #include "rt2x00lib.h"
  25. static int rt2x00mac_tx_rts_cts(struct rt2x00_dev *rt2x00dev,
  26. struct data_queue *queue,
  27. struct sk_buff *frag_skb)
  28. {
  29. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(frag_skb);
  30. struct skb_frame_desc *skbdesc;
  31. struct ieee80211_tx_info *rts_info;
  32. struct sk_buff *skb;
  33. int size;
  34. if (tx_info->flags & IEEE80211_TX_CTL_USE_CTS_PROTECT)
  35. size = sizeof(struct ieee80211_cts);
  36. else
  37. size = sizeof(struct ieee80211_rts);
  38. skb = dev_alloc_skb(size + rt2x00dev->hw->extra_tx_headroom);
  39. if (!skb) {
  40. WARNING(rt2x00dev, "Failed to create RTS/CTS frame.\n");
  41. return NETDEV_TX_BUSY;
  42. }
  43. skb_reserve(skb, rt2x00dev->hw->extra_tx_headroom);
  44. skb_put(skb, size);
  45. /*
  46. * Copy TX information over from original frame to
  47. * RTS/CTS frame. Note that we set the no encryption flag
  48. * since we don't want this frame to be encrypted.
  49. * RTS frames should be acked, while CTS-to-self frames
  50. * should not. The ready for TX flag is cleared to prevent
  51. * it being automatically send when the descriptor is
  52. * written to the hardware.
  53. */
  54. memcpy(skb->cb, frag_skb->cb, sizeof(skb->cb));
  55. rts_info = IEEE80211_SKB_CB(skb);
  56. rts_info->flags |= IEEE80211_TX_CTL_DO_NOT_ENCRYPT;
  57. rts_info->flags &= ~IEEE80211_TX_CTL_USE_CTS_PROTECT;
  58. rts_info->flags &= ~IEEE80211_TX_CTL_REQ_TX_STATUS;
  59. if (tx_info->flags & IEEE80211_TX_CTL_USE_CTS_PROTECT)
  60. rts_info->flags |= IEEE80211_TX_CTL_NO_ACK;
  61. else
  62. rts_info->flags &= ~IEEE80211_TX_CTL_NO_ACK;
  63. if (tx_info->flags & IEEE80211_TX_CTL_USE_CTS_PROTECT)
  64. ieee80211_ctstoself_get(rt2x00dev->hw, tx_info->control.vif,
  65. frag_skb->data, size, tx_info,
  66. (struct ieee80211_cts *)(skb->data));
  67. else
  68. ieee80211_rts_get(rt2x00dev->hw, tx_info->control.vif,
  69. frag_skb->data, size, tx_info,
  70. (struct ieee80211_rts *)(skb->data));
  71. /*
  72. * Initialize skb descriptor
  73. */
  74. skbdesc = get_skb_frame_desc(skb);
  75. memset(skbdesc, 0, sizeof(*skbdesc));
  76. skbdesc->flags |= FRAME_DESC_DRIVER_GENERATED;
  77. if (rt2x00queue_write_tx_frame(queue, skb)) {
  78. WARNING(rt2x00dev, "Failed to send RTS/CTS frame.\n");
  79. return NETDEV_TX_BUSY;
  80. }
  81. return NETDEV_TX_OK;
  82. }
  83. int rt2x00mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
  84. {
  85. struct rt2x00_dev *rt2x00dev = hw->priv;
  86. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  87. struct ieee80211_hdr *ieee80211hdr = (struct ieee80211_hdr *)skb->data;
  88. enum data_queue_qid qid = skb_get_queue_mapping(skb);
  89. struct data_queue *queue;
  90. u16 frame_control;
  91. /*
  92. * Mac80211 might be calling this function while we are trying
  93. * to remove the device or perhaps suspending it.
  94. * Note that we can only stop the TX queues inside the TX path
  95. * due to possible race conditions in mac80211.
  96. */
  97. if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags)) {
  98. ieee80211_stop_queues(hw);
  99. dev_kfree_skb_any(skb);
  100. return NETDEV_TX_OK;
  101. }
  102. /*
  103. * Determine which queue to put packet on.
  104. */
  105. if (tx_info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM &&
  106. test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags))
  107. queue = rt2x00queue_get_queue(rt2x00dev, QID_ATIM);
  108. else
  109. queue = rt2x00queue_get_queue(rt2x00dev, qid);
  110. if (unlikely(!queue)) {
  111. ERROR(rt2x00dev,
  112. "Attempt to send packet over invalid queue %d.\n"
  113. "Please file bug report to %s.\n", qid, DRV_PROJECT);
  114. dev_kfree_skb_any(skb);
  115. return NETDEV_TX_OK;
  116. }
  117. /*
  118. * If CTS/RTS is required. create and queue that frame first.
  119. * Make sure we have at least enough entries available to send
  120. * this CTS/RTS frame as well as the data frame.
  121. * Note that when the driver has set the set_rts_threshold()
  122. * callback function it doesn't need software generation of
  123. * either RTS or CTS-to-self frame and handles everything
  124. * inside the hardware.
  125. */
  126. frame_control = le16_to_cpu(ieee80211hdr->frame_control);
  127. if ((tx_info->flags & (IEEE80211_TX_CTL_USE_RTS_CTS |
  128. IEEE80211_TX_CTL_USE_CTS_PROTECT)) &&
  129. !rt2x00dev->ops->hw->set_rts_threshold) {
  130. if (rt2x00queue_available(queue) <= 1) {
  131. ieee80211_stop_queue(rt2x00dev->hw, qid);
  132. return NETDEV_TX_BUSY;
  133. }
  134. if (rt2x00mac_tx_rts_cts(rt2x00dev, queue, skb)) {
  135. ieee80211_stop_queue(rt2x00dev->hw, qid);
  136. return NETDEV_TX_BUSY;
  137. }
  138. }
  139. if (rt2x00queue_write_tx_frame(queue, skb)) {
  140. ieee80211_stop_queue(rt2x00dev->hw, qid);
  141. return NETDEV_TX_BUSY;
  142. }
  143. if (rt2x00queue_full(queue))
  144. ieee80211_stop_queue(rt2x00dev->hw, qid);
  145. return NETDEV_TX_OK;
  146. }
  147. EXPORT_SYMBOL_GPL(rt2x00mac_tx);
  148. int rt2x00mac_start(struct ieee80211_hw *hw)
  149. {
  150. struct rt2x00_dev *rt2x00dev = hw->priv;
  151. if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags))
  152. return 0;
  153. return rt2x00lib_start(rt2x00dev);
  154. }
  155. EXPORT_SYMBOL_GPL(rt2x00mac_start);
  156. void rt2x00mac_stop(struct ieee80211_hw *hw)
  157. {
  158. struct rt2x00_dev *rt2x00dev = hw->priv;
  159. if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags))
  160. return;
  161. rt2x00lib_stop(rt2x00dev);
  162. }
  163. EXPORT_SYMBOL_GPL(rt2x00mac_stop);
  164. int rt2x00mac_add_interface(struct ieee80211_hw *hw,
  165. struct ieee80211_if_init_conf *conf)
  166. {
  167. struct rt2x00_dev *rt2x00dev = hw->priv;
  168. struct rt2x00_intf *intf = vif_to_intf(conf->vif);
  169. struct data_queue *queue = rt2x00queue_get_queue(rt2x00dev, QID_BEACON);
  170. struct queue_entry *entry = NULL;
  171. unsigned int i;
  172. /*
  173. * Don't allow interfaces to be added
  174. * the device has disappeared.
  175. */
  176. if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags) ||
  177. !test_bit(DEVICE_STARTED, &rt2x00dev->flags))
  178. return -ENODEV;
  179. /*
  180. * We don't support mixed combinations of sta and ap virtual
  181. * interfaces. We can only add this interface when the rival
  182. * interface count is 0.
  183. */
  184. if ((conf->type == IEEE80211_IF_TYPE_AP && rt2x00dev->intf_sta_count) ||
  185. (conf->type != IEEE80211_IF_TYPE_AP && rt2x00dev->intf_ap_count))
  186. return -ENOBUFS;
  187. /*
  188. * Check if we exceeded the maximum amount of supported interfaces.
  189. */
  190. if ((conf->type == IEEE80211_IF_TYPE_AP &&
  191. rt2x00dev->intf_ap_count >= rt2x00dev->ops->max_ap_intf) ||
  192. (conf->type != IEEE80211_IF_TYPE_AP &&
  193. rt2x00dev->intf_sta_count >= rt2x00dev->ops->max_sta_intf))
  194. return -ENOBUFS;
  195. /*
  196. * Loop through all beacon queues to find a free
  197. * entry. Since there are as much beacon entries
  198. * as the maximum interfaces, this search shouldn't
  199. * fail.
  200. */
  201. for (i = 0; i < queue->limit; i++) {
  202. entry = &queue->entries[i];
  203. if (!__test_and_set_bit(ENTRY_BCN_ASSIGNED, &entry->flags))
  204. break;
  205. }
  206. if (unlikely(i == queue->limit))
  207. return -ENOBUFS;
  208. /*
  209. * We are now absolutely sure the interface can be created,
  210. * increase interface count and start initialization.
  211. */
  212. if (conf->type == IEEE80211_IF_TYPE_AP)
  213. rt2x00dev->intf_ap_count++;
  214. else
  215. rt2x00dev->intf_sta_count++;
  216. spin_lock_init(&intf->lock);
  217. intf->beacon = entry;
  218. if (conf->type == IEEE80211_IF_TYPE_AP)
  219. memcpy(&intf->bssid, conf->mac_addr, ETH_ALEN);
  220. memcpy(&intf->mac, conf->mac_addr, ETH_ALEN);
  221. /*
  222. * The MAC adddress must be configured after the device
  223. * has been initialized. Otherwise the device can reset
  224. * the MAC registers.
  225. */
  226. rt2x00lib_config_intf(rt2x00dev, intf, conf->type, intf->mac, NULL);
  227. /*
  228. * Some filters depend on the current working mode. We can force
  229. * an update during the next configure_filter() run by mac80211 by
  230. * resetting the current packet_filter state.
  231. */
  232. rt2x00dev->packet_filter = 0;
  233. return 0;
  234. }
  235. EXPORT_SYMBOL_GPL(rt2x00mac_add_interface);
  236. void rt2x00mac_remove_interface(struct ieee80211_hw *hw,
  237. struct ieee80211_if_init_conf *conf)
  238. {
  239. struct rt2x00_dev *rt2x00dev = hw->priv;
  240. struct rt2x00_intf *intf = vif_to_intf(conf->vif);
  241. /*
  242. * Don't allow interfaces to be remove while
  243. * either the device has disappeared or when
  244. * no interface is present.
  245. */
  246. if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags) ||
  247. (conf->type == IEEE80211_IF_TYPE_AP && !rt2x00dev->intf_ap_count) ||
  248. (conf->type != IEEE80211_IF_TYPE_AP && !rt2x00dev->intf_sta_count))
  249. return;
  250. if (conf->type == IEEE80211_IF_TYPE_AP)
  251. rt2x00dev->intf_ap_count--;
  252. else
  253. rt2x00dev->intf_sta_count--;
  254. /*
  255. * Release beacon entry so it is available for
  256. * new interfaces again.
  257. */
  258. __clear_bit(ENTRY_BCN_ASSIGNED, &intf->beacon->flags);
  259. /*
  260. * Make sure the bssid and mac address registers
  261. * are cleared to prevent false ACKing of frames.
  262. */
  263. rt2x00lib_config_intf(rt2x00dev, intf,
  264. IEEE80211_IF_TYPE_INVALID, NULL, NULL);
  265. }
  266. EXPORT_SYMBOL_GPL(rt2x00mac_remove_interface);
  267. int rt2x00mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
  268. {
  269. struct rt2x00_dev *rt2x00dev = hw->priv;
  270. /*
  271. * Mac80211 might be calling this function while we are trying
  272. * to remove the device or perhaps suspending it.
  273. */
  274. if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags))
  275. return 0;
  276. /*
  277. * Check if we need to disable the radio,
  278. * if this is not the case, at least the RX must be disabled.
  279. */
  280. if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags)) {
  281. if (!conf->radio_enabled)
  282. rt2x00lib_disable_radio(rt2x00dev);
  283. else
  284. rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_OFF);
  285. }
  286. rt2x00lib_config(rt2x00dev, conf, 0);
  287. /*
  288. * Reenable RX only if the radio should be on.
  289. */
  290. if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
  291. rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_ON);
  292. else if (conf->radio_enabled)
  293. return rt2x00lib_enable_radio(rt2x00dev);
  294. return 0;
  295. }
  296. EXPORT_SYMBOL_GPL(rt2x00mac_config);
  297. int rt2x00mac_config_interface(struct ieee80211_hw *hw,
  298. struct ieee80211_vif *vif,
  299. struct ieee80211_if_conf *conf)
  300. {
  301. struct rt2x00_dev *rt2x00dev = hw->priv;
  302. struct rt2x00_intf *intf = vif_to_intf(vif);
  303. int status;
  304. /*
  305. * Mac80211 might be calling this function while we are trying
  306. * to remove the device or perhaps suspending it.
  307. */
  308. if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags))
  309. return 0;
  310. spin_lock(&intf->lock);
  311. /*
  312. * If the interface does not work in master mode,
  313. * then the bssid value in the interface structure
  314. * should now be set.
  315. */
  316. if (conf->type != IEEE80211_IF_TYPE_AP)
  317. memcpy(&intf->bssid, conf->bssid, ETH_ALEN);
  318. spin_unlock(&intf->lock);
  319. /*
  320. * Call rt2x00_config_intf() outside of the spinlock context since
  321. * the call will sleep for USB drivers. By using the ieee80211_if_conf
  322. * values as arguments we make keep access to rt2x00_intf thread safe
  323. * even without the lock.
  324. */
  325. rt2x00lib_config_intf(rt2x00dev, intf, conf->type, NULL, conf->bssid);
  326. /*
  327. * We only need to initialize the beacon when master mode is enabled.
  328. */
  329. if (conf->type != IEEE80211_IF_TYPE_AP || !conf->beacon)
  330. return 0;
  331. status = rt2x00dev->ops->hw->beacon_update(rt2x00dev->hw, conf->beacon);
  332. if (status)
  333. dev_kfree_skb(conf->beacon);
  334. return status;
  335. }
  336. EXPORT_SYMBOL_GPL(rt2x00mac_config_interface);
  337. void rt2x00mac_configure_filter(struct ieee80211_hw *hw,
  338. unsigned int changed_flags,
  339. unsigned int *total_flags,
  340. int mc_count, struct dev_addr_list *mc_list)
  341. {
  342. struct rt2x00_dev *rt2x00dev = hw->priv;
  343. /*
  344. * Mask off any flags we are going to ignore
  345. * from the total_flags field.
  346. */
  347. *total_flags &=
  348. FIF_ALLMULTI |
  349. FIF_FCSFAIL |
  350. FIF_PLCPFAIL |
  351. FIF_CONTROL |
  352. FIF_OTHER_BSS |
  353. FIF_PROMISC_IN_BSS;
  354. /*
  355. * Apply some rules to the filters:
  356. * - Some filters imply different filters to be set.
  357. * - Some things we can't filter out at all.
  358. * - Multicast filter seems to kill broadcast traffic so never use it.
  359. */
  360. *total_flags |= FIF_ALLMULTI;
  361. if (*total_flags & FIF_OTHER_BSS ||
  362. *total_flags & FIF_PROMISC_IN_BSS)
  363. *total_flags |= FIF_PROMISC_IN_BSS | FIF_OTHER_BSS;
  364. /*
  365. * Check if there is any work left for us.
  366. */
  367. if (rt2x00dev->packet_filter == *total_flags)
  368. return;
  369. rt2x00dev->packet_filter = *total_flags;
  370. if (!test_bit(DRIVER_REQUIRE_SCHEDULED, &rt2x00dev->flags))
  371. rt2x00dev->ops->lib->config_filter(rt2x00dev, *total_flags);
  372. else
  373. queue_work(rt2x00dev->hw->workqueue, &rt2x00dev->filter_work);
  374. }
  375. EXPORT_SYMBOL_GPL(rt2x00mac_configure_filter);
  376. int rt2x00mac_get_stats(struct ieee80211_hw *hw,
  377. struct ieee80211_low_level_stats *stats)
  378. {
  379. struct rt2x00_dev *rt2x00dev = hw->priv;
  380. /*
  381. * The dot11ACKFailureCount, dot11RTSFailureCount and
  382. * dot11RTSSuccessCount are updated in interrupt time.
  383. * dot11FCSErrorCount is updated in the link tuner.
  384. */
  385. memcpy(stats, &rt2x00dev->low_level_stats, sizeof(*stats));
  386. return 0;
  387. }
  388. EXPORT_SYMBOL_GPL(rt2x00mac_get_stats);
  389. int rt2x00mac_get_tx_stats(struct ieee80211_hw *hw,
  390. struct ieee80211_tx_queue_stats *stats)
  391. {
  392. struct rt2x00_dev *rt2x00dev = hw->priv;
  393. unsigned int i;
  394. for (i = 0; i < rt2x00dev->ops->tx_queues; i++) {
  395. stats[i].len = rt2x00dev->tx[i].length;
  396. stats[i].limit = rt2x00dev->tx[i].limit;
  397. stats[i].count = rt2x00dev->tx[i].count;
  398. }
  399. return 0;
  400. }
  401. EXPORT_SYMBOL_GPL(rt2x00mac_get_tx_stats);
  402. void rt2x00mac_bss_info_changed(struct ieee80211_hw *hw,
  403. struct ieee80211_vif *vif,
  404. struct ieee80211_bss_conf *bss_conf,
  405. u32 changes)
  406. {
  407. struct rt2x00_dev *rt2x00dev = hw->priv;
  408. struct rt2x00_intf *intf = vif_to_intf(vif);
  409. unsigned int delayed = 0;
  410. /*
  411. * When the association status has changed we must reset the link
  412. * tuner counter. This is because some drivers determine if they
  413. * should perform link tuning based on the number of seconds
  414. * while associated or not associated.
  415. */
  416. if (changes & BSS_CHANGED_ASSOC) {
  417. rt2x00dev->link.count = 0;
  418. if (bss_conf->assoc)
  419. rt2x00dev->intf_associated++;
  420. else
  421. rt2x00dev->intf_associated--;
  422. if (!test_bit(DRIVER_REQUIRE_SCHEDULED, &rt2x00dev->flags))
  423. rt2x00leds_led_assoc(rt2x00dev,
  424. !!rt2x00dev->intf_associated);
  425. else
  426. delayed |= DELAYED_LED_ASSOC;
  427. }
  428. /*
  429. * When the erp information has changed, we should perform
  430. * additional configuration steps. For all other changes we are done.
  431. */
  432. if (changes & BSS_CHANGED_ERP_PREAMBLE) {
  433. if (!test_bit(DRIVER_REQUIRE_SCHEDULED, &rt2x00dev->flags))
  434. rt2x00lib_config_erp(rt2x00dev, intf, bss_conf);
  435. else
  436. delayed |= DELAYED_CONFIG_ERP;
  437. }
  438. spin_lock(&intf->lock);
  439. memcpy(&intf->conf, bss_conf, sizeof(*bss_conf));
  440. if (delayed) {
  441. intf->delayed_flags |= delayed;
  442. queue_work(rt2x00dev->hw->workqueue, &rt2x00dev->intf_work);
  443. }
  444. spin_unlock(&intf->lock);
  445. }
  446. EXPORT_SYMBOL_GPL(rt2x00mac_bss_info_changed);
  447. int rt2x00mac_conf_tx(struct ieee80211_hw *hw, u16 queue_idx,
  448. const struct ieee80211_tx_queue_params *params)
  449. {
  450. struct rt2x00_dev *rt2x00dev = hw->priv;
  451. struct data_queue *queue;
  452. queue = rt2x00queue_get_queue(rt2x00dev, queue_idx);
  453. if (unlikely(!queue))
  454. return -EINVAL;
  455. /*
  456. * The passed variables are stored as real value ((2^n)-1).
  457. * Ralink registers require to know the bit number 'n'.
  458. */
  459. if (params->cw_min > 0)
  460. queue->cw_min = fls(params->cw_min);
  461. else
  462. queue->cw_min = 5; /* cw_min: 2^5 = 32. */
  463. if (params->cw_max > 0)
  464. queue->cw_max = fls(params->cw_max);
  465. else
  466. queue->cw_max = 10; /* cw_min: 2^10 = 1024. */
  467. if (params->aifs >= 0)
  468. queue->aifs = params->aifs;
  469. else
  470. queue->aifs = 2;
  471. INFO(rt2x00dev,
  472. "Configured TX queue %d - CWmin: %d, CWmax: %d, Aifs: %d.\n",
  473. queue_idx, queue->cw_min, queue->cw_max, queue->aifs);
  474. return 0;
  475. }
  476. EXPORT_SYMBOL_GPL(rt2x00mac_conf_tx);