1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2008, cozybit Inc.
4 * Copyright (C) 2003-2006, Marvell International Ltd.
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 #include <linux/hardirq.h>
9 #include <linux/slab.h>
11 #include <linux/etherdevice.h>
12 #include <linux/module.h>
13 #include "libertas_tf.h"
15 /* thinfirm version: 5.132.X.pX */
16 #define LBTF_FW_VER_MIN 0x05840300
17 #define LBTF_FW_VER_MAX 0x0584ffff
19 /* Module parameters */
20 unsigned int lbtf_debug;
21 EXPORT_SYMBOL_GPL(lbtf_debug);
22 module_param_named(libertas_tf_debug, lbtf_debug, int, 0644);
24 struct workqueue_struct *lbtf_wq;
26 static const struct ieee80211_channel lbtf_channels[] = {
27 { .center_freq = 2412, .hw_value = 1 },
28 { .center_freq = 2417, .hw_value = 2 },
29 { .center_freq = 2422, .hw_value = 3 },
30 { .center_freq = 2427, .hw_value = 4 },
31 { .center_freq = 2432, .hw_value = 5 },
32 { .center_freq = 2437, .hw_value = 6 },
33 { .center_freq = 2442, .hw_value = 7 },
34 { .center_freq = 2447, .hw_value = 8 },
35 { .center_freq = 2452, .hw_value = 9 },
36 { .center_freq = 2457, .hw_value = 10 },
37 { .center_freq = 2462, .hw_value = 11 },
38 { .center_freq = 2467, .hw_value = 12 },
39 { .center_freq = 2472, .hw_value = 13 },
40 { .center_freq = 2484, .hw_value = 14 },
43 /* This table contains the hardware specific values for the modulation rates. */
44 static const struct ieee80211_rate lbtf_rates[] = {
49 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
52 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
55 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
82 static void lbtf_cmd_work(struct work_struct *work)
84 struct lbtf_private *priv = container_of(work, struct lbtf_private,
87 lbtf_deb_enter(LBTF_DEB_CMD);
89 spin_lock_irq(&priv->driver_lock);
90 /* command response? */
91 if (priv->cmd_response_rxed) {
92 priv->cmd_response_rxed = 0;
93 spin_unlock_irq(&priv->driver_lock);
94 lbtf_process_rx_command(priv);
95 spin_lock_irq(&priv->driver_lock);
98 if (priv->cmd_timed_out && priv->cur_cmd) {
99 struct cmd_ctrl_node *cmdnode = priv->cur_cmd;
101 if (++priv->nr_retries > 10) {
102 lbtf_complete_command(priv, cmdnode,
104 priv->nr_retries = 0;
106 priv->cur_cmd = NULL;
108 /* Stick it back at the _top_ of the pending
109 * queue for immediate resubmission */
110 list_add(&cmdnode->list, &priv->cmdpendingq);
113 priv->cmd_timed_out = 0;
114 spin_unlock_irq(&priv->driver_lock);
116 /* Execute the next command */
118 lbtf_execute_next_command(priv);
120 lbtf_deb_leave(LBTF_DEB_CMD);
124 * This function handles the timeout of command sending.
125 * It will re-send the same command again.
127 static void command_timer_fn(struct timer_list *t)
129 struct lbtf_private *priv = from_timer(priv, t, command_timer);
131 lbtf_deb_enter(LBTF_DEB_CMD);
133 spin_lock_irqsave(&priv->driver_lock, flags);
135 if (!priv->cur_cmd) {
136 printk(KERN_DEBUG "libertastf: command timer expired; "
137 "no pending command\n");
141 printk(KERN_DEBUG "libertas: command %x timed out\n",
142 le16_to_cpu(priv->cur_cmd->cmdbuf->command));
144 priv->cmd_timed_out = 1;
145 queue_work(lbtf_wq, &priv->cmd_work);
147 spin_unlock_irqrestore(&priv->driver_lock, flags);
148 lbtf_deb_leave(LBTF_DEB_CMD);
151 static int lbtf_init_adapter(struct lbtf_private *priv)
153 lbtf_deb_enter(LBTF_DEB_MAIN);
154 eth_broadcast_addr(priv->current_addr);
155 mutex_init(&priv->lock);
158 timer_setup(&priv->command_timer, command_timer_fn, 0);
160 INIT_LIST_HEAD(&priv->cmdfreeq);
161 INIT_LIST_HEAD(&priv->cmdpendingq);
163 spin_lock_init(&priv->driver_lock);
165 /* Allocate the command buffers */
166 if (lbtf_allocate_cmd_buffer(priv))
169 lbtf_deb_leave(LBTF_DEB_MAIN);
173 static void lbtf_free_adapter(struct lbtf_private *priv)
175 lbtf_deb_enter(LBTF_DEB_MAIN);
176 lbtf_free_cmd_buffer(priv);
177 del_timer(&priv->command_timer);
178 lbtf_deb_leave(LBTF_DEB_MAIN);
181 static void lbtf_op_tx(struct ieee80211_hw *hw,
182 struct ieee80211_tx_control *control,
185 struct lbtf_private *priv = hw->priv;
187 priv->skb_to_tx = skb;
188 queue_work(lbtf_wq, &priv->tx_work);
190 * queue will be restarted when we receive transmission feedback if
191 * there are no buffered multicast frames to send
193 ieee80211_stop_queues(priv->hw);
196 static void lbtf_tx_work(struct work_struct *work)
198 struct lbtf_private *priv = container_of(work, struct lbtf_private,
201 struct ieee80211_tx_info *info;
203 struct sk_buff *skb = NULL;
206 lbtf_deb_enter(LBTF_DEB_MACOPS | LBTF_DEB_TX);
208 if ((priv->vif->type == NL80211_IFTYPE_AP) &&
209 (!skb_queue_empty(&priv->bc_ps_buf)))
210 skb = skb_dequeue(&priv->bc_ps_buf);
211 else if (priv->skb_to_tx) {
212 skb = priv->skb_to_tx;
213 priv->skb_to_tx = NULL;
215 lbtf_deb_leave(LBTF_DEB_MACOPS | LBTF_DEB_TX);
220 info = IEEE80211_SKB_CB(skb);
221 txpd = skb_push(skb, sizeof(struct txpd));
223 if (priv->surpriseremoved) {
224 dev_kfree_skb_any(skb);
225 lbtf_deb_leave(LBTF_DEB_MACOPS | LBTF_DEB_TX);
229 memset(txpd, 0, sizeof(struct txpd));
230 /* Activate per-packet rate selection */
231 txpd->tx_control |= cpu_to_le32(MRVL_PER_PACKET_RATE |
232 ieee80211_get_tx_rate(priv->hw, info)->hw_value);
234 /* copy destination address from 802.11 header */
235 BUILD_BUG_ON(sizeof(txpd->tx_dest_addr) != ETH_ALEN);
236 memcpy(&txpd->tx_dest_addr, skb->data + sizeof(struct txpd) + 4,
238 txpd->tx_packet_length = cpu_to_le16(len);
239 txpd->tx_packet_location = cpu_to_le32(sizeof(struct txpd));
240 lbtf_deb_hex(LBTF_DEB_TX, "TX Data", skb->data, min_t(unsigned int, skb->len, 100));
241 BUG_ON(priv->tx_skb);
242 spin_lock_irq(&priv->driver_lock);
244 err = priv->ops->hw_host_to_card(priv, MVMS_DAT, skb->data, skb->len);
245 spin_unlock_irq(&priv->driver_lock);
247 dev_kfree_skb_any(skb);
249 pr_err("TX error: %d", err);
251 lbtf_deb_leave(LBTF_DEB_MACOPS | LBTF_DEB_TX);
254 static int lbtf_op_start(struct ieee80211_hw *hw)
256 struct lbtf_private *priv = hw->priv;
258 lbtf_deb_enter(LBTF_DEB_MACOPS);
260 priv->capability = WLAN_CAPABILITY_SHORT_PREAMBLE;
261 priv->radioon = RADIO_ON;
262 priv->mac_control = CMD_ACT_MAC_RX_ON | CMD_ACT_MAC_TX_ON;
263 lbtf_set_mac_control(priv);
264 lbtf_set_radio_control(priv);
266 lbtf_deb_leave(LBTF_DEB_MACOPS);
270 static void lbtf_op_stop(struct ieee80211_hw *hw)
272 struct lbtf_private *priv = hw->priv;
276 struct cmd_ctrl_node *cmdnode;
278 lbtf_deb_enter(LBTF_DEB_MACOPS);
280 /* Flush pending command nodes */
281 spin_lock_irqsave(&priv->driver_lock, flags);
282 list_for_each_entry(cmdnode, &priv->cmdpendingq, list) {
283 cmdnode->result = -ENOENT;
284 cmdnode->cmdwaitqwoken = 1;
285 wake_up_interruptible(&cmdnode->cmdwait_q);
288 spin_unlock_irqrestore(&priv->driver_lock, flags);
289 cancel_work_sync(&priv->cmd_work);
290 cancel_work_sync(&priv->tx_work);
291 while ((skb = skb_dequeue(&priv->bc_ps_buf)))
292 dev_kfree_skb_any(skb);
293 priv->radioon = RADIO_OFF;
294 lbtf_set_radio_control(priv);
296 lbtf_deb_leave(LBTF_DEB_MACOPS);
299 static int lbtf_op_add_interface(struct ieee80211_hw *hw,
300 struct ieee80211_vif *vif)
302 struct lbtf_private *priv = hw->priv;
303 lbtf_deb_enter(LBTF_DEB_MACOPS);
304 if (priv->vif != NULL)
309 case NL80211_IFTYPE_MESH_POINT:
310 case NL80211_IFTYPE_AP:
311 lbtf_set_mode(priv, LBTF_AP_MODE);
313 case NL80211_IFTYPE_STATION:
314 lbtf_set_mode(priv, LBTF_STA_MODE);
320 lbtf_set_mac_address(priv, (u8 *) vif->addr);
321 lbtf_deb_leave(LBTF_DEB_MACOPS);
325 static void lbtf_op_remove_interface(struct ieee80211_hw *hw,
326 struct ieee80211_vif *vif)
328 struct lbtf_private *priv = hw->priv;
329 lbtf_deb_enter(LBTF_DEB_MACOPS);
331 if (priv->vif->type == NL80211_IFTYPE_AP ||
332 priv->vif->type == NL80211_IFTYPE_MESH_POINT)
333 lbtf_beacon_ctrl(priv, 0, 0);
334 lbtf_set_mode(priv, LBTF_PASSIVE_MODE);
335 lbtf_set_bssid(priv, 0, NULL);
337 lbtf_deb_leave(LBTF_DEB_MACOPS);
340 static int lbtf_op_config(struct ieee80211_hw *hw, u32 changed)
342 struct lbtf_private *priv = hw->priv;
343 struct ieee80211_conf *conf = &hw->conf;
344 lbtf_deb_enter(LBTF_DEB_MACOPS);
346 if (conf->chandef.chan->center_freq != priv->cur_freq) {
347 priv->cur_freq = conf->chandef.chan->center_freq;
348 lbtf_set_channel(priv, conf->chandef.chan->hw_value);
350 lbtf_deb_leave(LBTF_DEB_MACOPS);
354 static u64 lbtf_op_prepare_multicast(struct ieee80211_hw *hw,
355 struct netdev_hw_addr_list *mc_list)
357 struct lbtf_private *priv = hw->priv;
359 struct netdev_hw_addr *ha;
360 int mc_count = netdev_hw_addr_list_count(mc_list);
362 if (!mc_count || mc_count > MRVDRV_MAX_MULTICAST_LIST_SIZE)
365 priv->nr_of_multicastmacaddr = mc_count;
367 netdev_hw_addr_list_for_each(ha, mc_list)
368 memcpy(&priv->multicastlist[i++], ha->addr, ETH_ALEN);
373 #define SUPPORTED_FIF_FLAGS FIF_ALLMULTI
374 static void lbtf_op_configure_filter(struct ieee80211_hw *hw,
375 unsigned int changed_flags,
376 unsigned int *new_flags,
379 struct lbtf_private *priv = hw->priv;
380 int old_mac_control = priv->mac_control;
382 lbtf_deb_enter(LBTF_DEB_MACOPS);
384 changed_flags &= SUPPORTED_FIF_FLAGS;
385 *new_flags &= SUPPORTED_FIF_FLAGS;
387 if (!changed_flags) {
388 lbtf_deb_leave(LBTF_DEB_MACOPS);
392 priv->mac_control &= ~CMD_ACT_MAC_PROMISCUOUS_ENABLE;
393 if (*new_flags & (FIF_ALLMULTI) ||
394 multicast > MRVDRV_MAX_MULTICAST_LIST_SIZE) {
395 priv->mac_control |= CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
396 priv->mac_control &= ~CMD_ACT_MAC_MULTICAST_ENABLE;
397 } else if (multicast) {
398 priv->mac_control |= CMD_ACT_MAC_MULTICAST_ENABLE;
399 priv->mac_control &= ~CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
400 lbtf_cmd_set_mac_multicast_addr(priv);
402 priv->mac_control &= ~(CMD_ACT_MAC_MULTICAST_ENABLE |
403 CMD_ACT_MAC_ALL_MULTICAST_ENABLE);
404 if (priv->nr_of_multicastmacaddr) {
405 priv->nr_of_multicastmacaddr = 0;
406 lbtf_cmd_set_mac_multicast_addr(priv);
411 if (priv->mac_control != old_mac_control)
412 lbtf_set_mac_control(priv);
414 lbtf_deb_leave(LBTF_DEB_MACOPS);
417 static void lbtf_op_bss_info_changed(struct ieee80211_hw *hw,
418 struct ieee80211_vif *vif,
419 struct ieee80211_bss_conf *bss_conf,
422 struct lbtf_private *priv = hw->priv;
423 struct sk_buff *beacon;
424 lbtf_deb_enter(LBTF_DEB_MACOPS);
426 if (changes & (BSS_CHANGED_BEACON | BSS_CHANGED_BEACON_INT)) {
427 switch (priv->vif->type) {
428 case NL80211_IFTYPE_AP:
429 case NL80211_IFTYPE_MESH_POINT:
430 beacon = ieee80211_beacon_get(hw, vif);
432 lbtf_beacon_set(priv, beacon);
434 lbtf_beacon_ctrl(priv, 1,
435 bss_conf->beacon_int);
443 if (changes & BSS_CHANGED_BSSID) {
444 bool activate = !is_zero_ether_addr(bss_conf->bssid);
445 lbtf_set_bssid(priv, activate, bss_conf->bssid);
448 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
449 if (bss_conf->use_short_preamble)
450 priv->preamble = CMD_TYPE_SHORT_PREAMBLE;
452 priv->preamble = CMD_TYPE_LONG_PREAMBLE;
453 lbtf_set_radio_control(priv);
456 lbtf_deb_leave(LBTF_DEB_MACOPS);
459 static int lbtf_op_get_survey(struct ieee80211_hw *hw, int idx,
460 struct survey_info *survey)
462 struct lbtf_private *priv = hw->priv;
463 struct ieee80211_conf *conf = &hw->conf;
468 survey->channel = conf->chandef.chan;
469 survey->filled = SURVEY_INFO_NOISE_DBM;
470 survey->noise = priv->noise;
475 static const struct ieee80211_ops lbtf_ops = {
477 .start = lbtf_op_start,
478 .stop = lbtf_op_stop,
479 .add_interface = lbtf_op_add_interface,
480 .remove_interface = lbtf_op_remove_interface,
481 .config = lbtf_op_config,
482 .prepare_multicast = lbtf_op_prepare_multicast,
483 .configure_filter = lbtf_op_configure_filter,
484 .bss_info_changed = lbtf_op_bss_info_changed,
485 .get_survey = lbtf_op_get_survey,
488 int lbtf_rx(struct lbtf_private *priv, struct sk_buff *skb)
490 struct ieee80211_rx_status stats;
493 struct ieee80211_hdr *hdr;
495 lbtf_deb_enter(LBTF_DEB_RX);
497 if (priv->radioon != RADIO_ON) {
498 lbtf_deb_rx("rx before we turned on the radio");
502 prxpd = (struct rxpd *) skb->data;
504 memset(&stats, 0, sizeof(stats));
505 if (!(prxpd->status & cpu_to_le16(MRVDRV_RXPD_STATUS_OK)))
506 stats.flag |= RX_FLAG_FAILED_FCS_CRC;
507 stats.freq = priv->cur_freq;
508 stats.band = NL80211_BAND_2GHZ;
509 stats.signal = prxpd->snr - prxpd->nf;
510 priv->noise = prxpd->nf;
511 /* Marvell rate index has a hole at value 4 */
512 if (prxpd->rx_rate > 4)
514 stats.rate_idx = prxpd->rx_rate;
515 skb_pull(skb, sizeof(struct rxpd));
517 hdr = (struct ieee80211_hdr *)skb->data;
519 need_padding = ieee80211_is_data_qos(hdr->frame_control);
520 need_padding ^= ieee80211_has_a4(hdr->frame_control);
521 need_padding ^= ieee80211_is_data_qos(hdr->frame_control) &&
522 (*ieee80211_get_qos_ctl(hdr) &
523 IEEE80211_QOS_CTL_A_MSDU_PRESENT);
526 memmove(skb->data + 2, skb->data, skb->len);
530 memcpy(IEEE80211_SKB_RXCB(skb), &stats, sizeof(stats));
532 lbtf_deb_rx("rx data: skb->len-sizeof(RxPd) = %d-%zd = %zd\n",
533 skb->len, sizeof(struct rxpd), skb->len - sizeof(struct rxpd));
534 lbtf_deb_hex(LBTF_DEB_RX, "RX Data", skb->data,
535 min_t(unsigned int, skb->len, 100));
537 ieee80211_rx_irqsafe(priv->hw, skb);
540 lbtf_deb_leave(LBTF_DEB_RX);
543 EXPORT_SYMBOL_GPL(lbtf_rx);
546 * lbtf_add_card: Add and initialize the card.
548 * Returns: pointer to struct lbtf_priv.
550 struct lbtf_private *lbtf_add_card(void *card, struct device *dmdev,
551 const struct lbtf_ops *ops)
553 struct ieee80211_hw *hw;
554 struct lbtf_private *priv = NULL;
556 lbtf_deb_enter(LBTF_DEB_MAIN);
558 hw = ieee80211_alloc_hw(sizeof(struct lbtf_private), &lbtf_ops);
563 if (lbtf_init_adapter(priv))
564 goto err_init_adapter;
570 priv->radioon = RADIO_OFF;
573 ieee80211_hw_set(hw, HOST_BROADCAST_PS_BUFFERING);
574 ieee80211_hw_set(hw, SIGNAL_DBM);
575 hw->extra_tx_headroom = sizeof(struct txpd);
576 memcpy(priv->channels, lbtf_channels, sizeof(lbtf_channels));
577 memcpy(priv->rates, lbtf_rates, sizeof(lbtf_rates));
578 priv->band.n_bitrates = ARRAY_SIZE(lbtf_rates);
579 priv->band.bitrates = priv->rates;
580 priv->band.n_channels = ARRAY_SIZE(lbtf_channels);
581 priv->band.channels = priv->channels;
582 hw->wiphy->bands[NL80211_BAND_2GHZ] = &priv->band;
583 hw->wiphy->interface_modes =
584 BIT(NL80211_IFTYPE_STATION) |
585 BIT(NL80211_IFTYPE_ADHOC);
586 skb_queue_head_init(&priv->bc_ps_buf);
588 wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
590 SET_IEEE80211_DEV(hw, dmdev);
592 INIT_WORK(&priv->cmd_work, lbtf_cmd_work);
593 INIT_WORK(&priv->tx_work, lbtf_tx_work);
595 if (priv->ops->hw_prog_firmware(priv)) {
596 lbtf_deb_usbd(dmdev, "Error programming the firmware\n");
597 priv->ops->hw_reset_device(priv);
598 goto err_init_adapter;
601 eth_broadcast_addr(priv->current_addr);
602 if (lbtf_update_hw_spec(priv))
603 goto err_init_adapter;
605 if (priv->fwrelease < LBTF_FW_VER_MIN ||
606 priv->fwrelease > LBTF_FW_VER_MAX) {
607 goto err_init_adapter;
610 /* The firmware seems to start with the radio enabled. Turn it
611 * off before an actual mac80211 start callback is invoked.
613 lbtf_set_radio_control(priv);
615 if (ieee80211_register_hw(hw))
616 goto err_init_adapter;
618 dev_info(dmdev, "libertastf: Marvell WLAN 802.11 thinfirm adapter\n");
622 lbtf_free_adapter(priv);
623 ieee80211_free_hw(hw);
627 lbtf_deb_leave_args(LBTF_DEB_MAIN, "priv %p", priv);
630 EXPORT_SYMBOL_GPL(lbtf_add_card);
633 int lbtf_remove_card(struct lbtf_private *priv)
635 struct ieee80211_hw *hw = priv->hw;
637 lbtf_deb_enter(LBTF_DEB_MAIN);
639 priv->surpriseremoved = 1;
640 del_timer(&priv->command_timer);
641 lbtf_free_adapter(priv);
643 ieee80211_unregister_hw(hw);
644 ieee80211_free_hw(hw);
646 lbtf_deb_leave(LBTF_DEB_MAIN);
649 EXPORT_SYMBOL_GPL(lbtf_remove_card);
651 void lbtf_send_tx_feedback(struct lbtf_private *priv, u8 retrycnt, u8 fail)
653 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(priv->tx_skb);
655 ieee80211_tx_info_clear_status(info);
657 * Commented out, otherwise we never go beyond 1Mbit/s using mac80211
658 * default pid rc algorithm.
660 * info->status.retry_count = MRVL_DEFAULT_RETRIES - retrycnt;
662 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK) && !fail)
663 info->flags |= IEEE80211_TX_STAT_ACK;
664 skb_pull(priv->tx_skb, sizeof(struct txpd));
665 ieee80211_tx_status_irqsafe(priv->hw, priv->tx_skb);
667 if (!priv->skb_to_tx && skb_queue_empty(&priv->bc_ps_buf))
668 ieee80211_wake_queues(priv->hw);
670 queue_work(lbtf_wq, &priv->tx_work);
672 EXPORT_SYMBOL_GPL(lbtf_send_tx_feedback);
674 void lbtf_bcn_sent(struct lbtf_private *priv)
676 struct sk_buff *skb = NULL;
678 if (priv->vif->type != NL80211_IFTYPE_AP)
681 if (skb_queue_empty(&priv->bc_ps_buf)) {
682 bool tx_buff_bc = false;
684 while ((skb = ieee80211_get_buffered_bc(priv->hw, priv->vif))) {
685 skb_queue_tail(&priv->bc_ps_buf, skb);
689 ieee80211_stop_queues(priv->hw);
690 queue_work(lbtf_wq, &priv->tx_work);
694 skb = ieee80211_beacon_get(priv->hw, priv->vif);
697 lbtf_beacon_set(priv, skb);
701 EXPORT_SYMBOL_GPL(lbtf_bcn_sent);
703 static int __init lbtf_init_module(void)
705 lbtf_deb_enter(LBTF_DEB_MAIN);
706 lbtf_wq = alloc_workqueue("libertastf", WQ_MEM_RECLAIM, 0);
707 if (lbtf_wq == NULL) {
708 printk(KERN_ERR "libertastf: couldn't create workqueue\n");
711 lbtf_deb_leave(LBTF_DEB_MAIN);
715 static void __exit lbtf_exit_module(void)
717 lbtf_deb_enter(LBTF_DEB_MAIN);
718 destroy_workqueue(lbtf_wq);
719 lbtf_deb_leave(LBTF_DEB_MAIN);
722 module_init(lbtf_init_module);
723 module_exit(lbtf_exit_module);
725 MODULE_DESCRIPTION("Libertas WLAN Thinfirm Driver Library");
726 MODULE_AUTHOR("Cozybit Inc.");
727 MODULE_LICENSE("GPL");