rt2x00mac.c 15 KB

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