rt2x00mac.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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 (rt2x00dev->ops->lib->write_tx_data(rt2x00dev, 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. return NETDEV_TX_OK;
  100. }
  101. /*
  102. * Determine which queue to put packet on.
  103. */
  104. if (tx_info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM &&
  105. test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags))
  106. queue = rt2x00queue_get_queue(rt2x00dev, QID_ATIM);
  107. else
  108. queue = rt2x00queue_get_queue(rt2x00dev, qid);
  109. if (unlikely(!queue)) {
  110. ERROR(rt2x00dev,
  111. "Attempt to send packet over invalid queue %d.\n"
  112. "Please file bug report to %s.\n", qid, DRV_PROJECT);
  113. dev_kfree_skb_any(skb);
  114. return NETDEV_TX_OK;
  115. }
  116. /*
  117. * If CTS/RTS is required. and this frame is not CTS or RTS,
  118. * create and queue that frame first. But make sure we have
  119. * at least enough entries available to send this CTS/RTS
  120. * 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. * neither RTS or CTS-to-self frames and handles everything
  124. * inside the hardware.
  125. */
  126. frame_control = le16_to_cpu(ieee80211hdr->frame_control);
  127. if (!is_rts_frame(frame_control) && !is_cts_frame(frame_control) &&
  128. (tx_info->flags & (IEEE80211_TX_CTL_USE_RTS_CTS |
  129. IEEE80211_TX_CTL_USE_CTS_PROTECT)) &&
  130. !rt2x00dev->ops->hw->set_rts_threshold) {
  131. if (rt2x00queue_available(queue) <= 1) {
  132. ieee80211_stop_queue(rt2x00dev->hw, qid);
  133. return NETDEV_TX_BUSY;
  134. }
  135. if (rt2x00mac_tx_rts_cts(rt2x00dev, queue, skb)) {
  136. ieee80211_stop_queue(rt2x00dev->hw, qid);
  137. return NETDEV_TX_BUSY;
  138. }
  139. }
  140. if (rt2x00dev->ops->lib->write_tx_data(rt2x00dev, queue, skb)) {
  141. ieee80211_stop_queue(rt2x00dev->hw, qid);
  142. return NETDEV_TX_BUSY;
  143. }
  144. if (rt2x00queue_full(queue))
  145. ieee80211_stop_queue(rt2x00dev->hw, qid);
  146. if (rt2x00dev->ops->lib->kick_tx_queue)
  147. rt2x00dev->ops->lib->kick_tx_queue(rt2x00dev, qid);
  148. return NETDEV_TX_OK;
  149. }
  150. EXPORT_SYMBOL_GPL(rt2x00mac_tx);
  151. int rt2x00mac_start(struct ieee80211_hw *hw)
  152. {
  153. struct rt2x00_dev *rt2x00dev = hw->priv;
  154. if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags))
  155. return 0;
  156. return rt2x00lib_start(rt2x00dev);
  157. }
  158. EXPORT_SYMBOL_GPL(rt2x00mac_start);
  159. void rt2x00mac_stop(struct ieee80211_hw *hw)
  160. {
  161. struct rt2x00_dev *rt2x00dev = hw->priv;
  162. if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags))
  163. return;
  164. rt2x00lib_stop(rt2x00dev);
  165. }
  166. EXPORT_SYMBOL_GPL(rt2x00mac_stop);
  167. int rt2x00mac_add_interface(struct ieee80211_hw *hw,
  168. struct ieee80211_if_init_conf *conf)
  169. {
  170. struct rt2x00_dev *rt2x00dev = hw->priv;
  171. struct rt2x00_intf *intf = vif_to_intf(conf->vif);
  172. struct data_queue *queue = rt2x00queue_get_queue(rt2x00dev, QID_BEACON);
  173. struct queue_entry *entry = NULL;
  174. unsigned int i;
  175. /*
  176. * Don't allow interfaces to be added
  177. * the device has disappeared.
  178. */
  179. if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags) ||
  180. !test_bit(DEVICE_STARTED, &rt2x00dev->flags))
  181. return -ENODEV;
  182. /*
  183. * We don't support mixed combinations of sta and ap virtual
  184. * interfaces. We can only add this interface when the rival
  185. * interface count is 0.
  186. */
  187. if ((conf->type == IEEE80211_IF_TYPE_AP && rt2x00dev->intf_sta_count) ||
  188. (conf->type != IEEE80211_IF_TYPE_AP && rt2x00dev->intf_ap_count))
  189. return -ENOBUFS;
  190. /*
  191. * Check if we exceeded the maximum amount of supported interfaces.
  192. */
  193. if ((conf->type == IEEE80211_IF_TYPE_AP &&
  194. rt2x00dev->intf_ap_count >= rt2x00dev->ops->max_ap_intf) ||
  195. (conf->type != IEEE80211_IF_TYPE_AP &&
  196. rt2x00dev->intf_sta_count >= rt2x00dev->ops->max_sta_intf))
  197. return -ENOBUFS;
  198. /*
  199. * Loop through all beacon queues to find a free
  200. * entry. Since there are as much beacon entries
  201. * as the maximum interfaces, this search shouldn't
  202. * fail.
  203. */
  204. for (i = 0; i < queue->limit; i++) {
  205. entry = &queue->entries[i];
  206. if (!__test_and_set_bit(ENTRY_BCN_ASSIGNED, &entry->flags))
  207. break;
  208. }
  209. if (unlikely(i == queue->limit))
  210. return -ENOBUFS;
  211. /*
  212. * We are now absolutely sure the interface can be created,
  213. * increase interface count and start initialization.
  214. */
  215. if (conf->type == IEEE80211_IF_TYPE_AP)
  216. rt2x00dev->intf_ap_count++;
  217. else
  218. rt2x00dev->intf_sta_count++;
  219. spin_lock_init(&intf->lock);
  220. intf->beacon = entry;
  221. if (conf->type == IEEE80211_IF_TYPE_AP)
  222. memcpy(&intf->bssid, conf->mac_addr, ETH_ALEN);
  223. memcpy(&intf->mac, conf->mac_addr, ETH_ALEN);
  224. /*
  225. * The MAC adddress must be configured after the device
  226. * has been initialized. Otherwise the device can reset
  227. * the MAC registers.
  228. */
  229. rt2x00lib_config_intf(rt2x00dev, intf, conf->type, intf->mac, NULL);
  230. /*
  231. * Some filters depend on the current working mode. We can force
  232. * an update during the next configure_filter() run by mac80211 by
  233. * resetting the current packet_filter state.
  234. */
  235. rt2x00dev->packet_filter = 0;
  236. return 0;
  237. }
  238. EXPORT_SYMBOL_GPL(rt2x00mac_add_interface);
  239. void rt2x00mac_remove_interface(struct ieee80211_hw *hw,
  240. struct ieee80211_if_init_conf *conf)
  241. {
  242. struct rt2x00_dev *rt2x00dev = hw->priv;
  243. struct rt2x00_intf *intf = vif_to_intf(conf->vif);
  244. /*
  245. * Don't allow interfaces to be remove while
  246. * either the device has disappeared or when
  247. * no interface is present.
  248. */
  249. if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags) ||
  250. (conf->type == IEEE80211_IF_TYPE_AP && !rt2x00dev->intf_ap_count) ||
  251. (conf->type != IEEE80211_IF_TYPE_AP && !rt2x00dev->intf_sta_count))
  252. return;
  253. if (conf->type == IEEE80211_IF_TYPE_AP)
  254. rt2x00dev->intf_ap_count--;
  255. else
  256. rt2x00dev->intf_sta_count--;
  257. /*
  258. * Release beacon entry so it is available for
  259. * new interfaces again.
  260. */
  261. __clear_bit(ENTRY_BCN_ASSIGNED, &intf->beacon->flags);
  262. /*
  263. * Make sure the bssid and mac address registers
  264. * are cleared to prevent false ACKing of frames.
  265. */
  266. rt2x00lib_config_intf(rt2x00dev, intf,
  267. IEEE80211_IF_TYPE_INVALID, NULL, NULL);
  268. }
  269. EXPORT_SYMBOL_GPL(rt2x00mac_remove_interface);
  270. int rt2x00mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
  271. {
  272. struct rt2x00_dev *rt2x00dev = hw->priv;
  273. /*
  274. * Mac80211 might be calling this function while we are trying
  275. * to remove the device or perhaps suspending it.
  276. */
  277. if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags))
  278. return 0;
  279. /*
  280. * Check if we need to disable the radio,
  281. * if this is not the case, at least the RX must be disabled.
  282. */
  283. if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags)) {
  284. if (!conf->radio_enabled)
  285. rt2x00lib_disable_radio(rt2x00dev);
  286. else
  287. rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_OFF);
  288. }
  289. rt2x00lib_config(rt2x00dev, conf, 0);
  290. /*
  291. * Reenable RX only if the radio should be on.
  292. */
  293. if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
  294. rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_ON);
  295. else if (conf->radio_enabled)
  296. return rt2x00lib_enable_radio(rt2x00dev);
  297. return 0;
  298. }
  299. EXPORT_SYMBOL_GPL(rt2x00mac_config);
  300. int rt2x00mac_config_interface(struct ieee80211_hw *hw,
  301. struct ieee80211_vif *vif,
  302. struct ieee80211_if_conf *conf)
  303. {
  304. struct rt2x00_dev *rt2x00dev = hw->priv;
  305. struct rt2x00_intf *intf = vif_to_intf(vif);
  306. int status;
  307. /*
  308. * Mac80211 might be calling this function while we are trying
  309. * to remove the device or perhaps suspending it.
  310. */
  311. if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags))
  312. return 0;
  313. spin_lock(&intf->lock);
  314. /*
  315. * If the interface does not work in master mode,
  316. * then the bssid value in the interface structure
  317. * should now be set.
  318. */
  319. if (conf->type != IEEE80211_IF_TYPE_AP)
  320. memcpy(&intf->bssid, conf->bssid, ETH_ALEN);
  321. spin_unlock(&intf->lock);
  322. /*
  323. * Call rt2x00_config_intf() outside of the spinlock context since
  324. * the call will sleep for USB drivers. By using the ieee80211_if_conf
  325. * values as arguments we make keep access to rt2x00_intf thread safe
  326. * even without the lock.
  327. */
  328. rt2x00lib_config_intf(rt2x00dev, intf, conf->type, NULL, conf->bssid);
  329. /*
  330. * We only need to initialize the beacon when master mode is enabled.
  331. */
  332. if (conf->type != IEEE80211_IF_TYPE_AP || !conf->beacon)
  333. return 0;
  334. status = rt2x00dev->ops->hw->beacon_update(rt2x00dev->hw, conf->beacon);
  335. if (status)
  336. dev_kfree_skb(conf->beacon);
  337. return status;
  338. }
  339. EXPORT_SYMBOL_GPL(rt2x00mac_config_interface);
  340. void rt2x00mac_configure_filter(struct ieee80211_hw *hw,
  341. unsigned int changed_flags,
  342. unsigned int *total_flags,
  343. int mc_count, struct dev_addr_list *mc_list)
  344. {
  345. struct rt2x00_dev *rt2x00dev = hw->priv;
  346. /*
  347. * Mask off any flags we are going to ignore
  348. * from the total_flags field.
  349. */
  350. *total_flags &=
  351. FIF_ALLMULTI |
  352. FIF_FCSFAIL |
  353. FIF_PLCPFAIL |
  354. FIF_CONTROL |
  355. FIF_OTHER_BSS |
  356. FIF_PROMISC_IN_BSS;
  357. /*
  358. * Apply some rules to the filters:
  359. * - Some filters imply different filters to be set.
  360. * - Some things we can't filter out at all.
  361. * - Multicast filter seems to kill broadcast traffic so never use it.
  362. */
  363. *total_flags |= FIF_ALLMULTI;
  364. if (*total_flags & FIF_OTHER_BSS ||
  365. *total_flags & FIF_PROMISC_IN_BSS)
  366. *total_flags |= FIF_PROMISC_IN_BSS | FIF_OTHER_BSS;
  367. /*
  368. * Check if there is any work left for us.
  369. */
  370. if (rt2x00dev->packet_filter == *total_flags)
  371. return;
  372. rt2x00dev->packet_filter = *total_flags;
  373. if (!test_bit(DRIVER_REQUIRE_SCHEDULED, &rt2x00dev->flags))
  374. rt2x00dev->ops->lib->config_filter(rt2x00dev, *total_flags);
  375. else
  376. queue_work(rt2x00dev->hw->workqueue, &rt2x00dev->filter_work);
  377. }
  378. EXPORT_SYMBOL_GPL(rt2x00mac_configure_filter);
  379. int rt2x00mac_get_stats(struct ieee80211_hw *hw,
  380. struct ieee80211_low_level_stats *stats)
  381. {
  382. struct rt2x00_dev *rt2x00dev = hw->priv;
  383. /*
  384. * The dot11ACKFailureCount, dot11RTSFailureCount and
  385. * dot11RTSSuccessCount are updated in interrupt time.
  386. * dot11FCSErrorCount is updated in the link tuner.
  387. */
  388. memcpy(stats, &rt2x00dev->low_level_stats, sizeof(*stats));
  389. return 0;
  390. }
  391. EXPORT_SYMBOL_GPL(rt2x00mac_get_stats);
  392. int rt2x00mac_get_tx_stats(struct ieee80211_hw *hw,
  393. struct ieee80211_tx_queue_stats *stats)
  394. {
  395. struct rt2x00_dev *rt2x00dev = hw->priv;
  396. unsigned int i;
  397. for (i = 0; i < rt2x00dev->ops->tx_queues; i++) {
  398. stats[i].len = rt2x00dev->tx[i].length;
  399. stats[i].limit = rt2x00dev->tx[i].limit;
  400. stats[i].count = rt2x00dev->tx[i].count;
  401. }
  402. return 0;
  403. }
  404. EXPORT_SYMBOL_GPL(rt2x00mac_get_tx_stats);
  405. void rt2x00mac_bss_info_changed(struct ieee80211_hw *hw,
  406. struct ieee80211_vif *vif,
  407. struct ieee80211_bss_conf *bss_conf,
  408. u32 changes)
  409. {
  410. struct rt2x00_dev *rt2x00dev = hw->priv;
  411. struct rt2x00_intf *intf = vif_to_intf(vif);
  412. unsigned int delayed = 0;
  413. /*
  414. * When the association status has changed we must reset the link
  415. * tuner counter. This is because some drivers determine if they
  416. * should perform link tuning based on the number of seconds
  417. * while associated or not associated.
  418. */
  419. if (changes & BSS_CHANGED_ASSOC) {
  420. rt2x00dev->link.count = 0;
  421. if (bss_conf->assoc)
  422. rt2x00dev->intf_associated++;
  423. else
  424. rt2x00dev->intf_associated--;
  425. if (!test_bit(DRIVER_REQUIRE_SCHEDULED, &rt2x00dev->flags))
  426. rt2x00leds_led_assoc(rt2x00dev,
  427. !!rt2x00dev->intf_associated);
  428. else
  429. delayed |= DELAYED_LED_ASSOC;
  430. }
  431. /*
  432. * When the erp information has changed, we should perform
  433. * additional configuration steps. For all other changes we are done.
  434. */
  435. if (changes & BSS_CHANGED_ERP_PREAMBLE) {
  436. if (!test_bit(DRIVER_REQUIRE_SCHEDULED, &rt2x00dev->flags))
  437. rt2x00lib_config_erp(rt2x00dev, intf, bss_conf);
  438. else
  439. delayed |= DELAYED_CONFIG_ERP;
  440. }
  441. spin_lock(&intf->lock);
  442. memcpy(&intf->conf, bss_conf, sizeof(*bss_conf));
  443. if (delayed) {
  444. intf->delayed_flags |= delayed;
  445. queue_work(rt2x00dev->hw->workqueue, &rt2x00dev->intf_work);
  446. }
  447. spin_unlock(&intf->lock);
  448. }
  449. EXPORT_SYMBOL_GPL(rt2x00mac_bss_info_changed);
  450. int rt2x00mac_conf_tx(struct ieee80211_hw *hw, u16 queue_idx,
  451. const struct ieee80211_tx_queue_params *params)
  452. {
  453. struct rt2x00_dev *rt2x00dev = hw->priv;
  454. struct data_queue *queue;
  455. queue = rt2x00queue_get_queue(rt2x00dev, queue_idx);
  456. if (unlikely(!queue))
  457. return -EINVAL;
  458. /*
  459. * The passed variables are stored as real value ((2^n)-1).
  460. * Ralink registers require to know the bit number 'n'.
  461. */
  462. if (params->cw_min > 0)
  463. queue->cw_min = fls(params->cw_min);
  464. else
  465. queue->cw_min = 5; /* cw_min: 2^5 = 32. */
  466. if (params->cw_max > 0)
  467. queue->cw_max = fls(params->cw_max);
  468. else
  469. queue->cw_max = 10; /* cw_min: 2^10 = 1024. */
  470. if (params->aifs >= 0)
  471. queue->aifs = params->aifs;
  472. else
  473. queue->aifs = 2;
  474. INFO(rt2x00dev,
  475. "Configured TX queue %d - CWmin: %d, CWmax: %d, Aifs: %d.\n",
  476. queue_idx, queue->cw_min, queue->cw_max, queue->aifs);
  477. return 0;
  478. }
  479. EXPORT_SYMBOL_GPL(rt2x00mac_conf_tx);