2 * Copyright (c) 2015-2016 Quantenna Communications, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/if_ether.h>
29 #define QTNF_DMP_MAX_LEN 48
30 #define QTNF_PRIMARY_VIF_IDX 0
32 struct qtnf_frame_meta_info {
39 struct qtnf_wmac *qtnf_core_get_mac(const struct qtnf_bus *bus, u8 macid)
41 struct qtnf_wmac *mac = NULL;
43 if (unlikely(macid >= QTNF_MAX_MAC)) {
44 pr_err("invalid MAC index %u\n", macid);
48 mac = bus->mac[macid];
51 pr_err("MAC%u: not initialized\n", macid);
58 /* Netdev handler for open.
60 static int qtnf_netdev_open(struct net_device *ndev)
62 netif_carrier_off(ndev);
63 qtnf_netdev_updown(ndev, 1);
67 /* Netdev handler for close.
69 static int qtnf_netdev_close(struct net_device *ndev)
71 netif_carrier_off(ndev);
72 qtnf_virtual_intf_cleanup(ndev);
73 qtnf_netdev_updown(ndev, 0);
77 /* Netdev handler for data transmission.
80 qtnf_netdev_hard_start_xmit(struct sk_buff *skb, struct net_device *ndev)
83 struct qtnf_wmac *mac;
85 vif = qtnf_netdev_get_priv(ndev);
87 if (unlikely(skb->dev != ndev)) {
88 pr_err_ratelimited("invalid skb->dev");
89 dev_kfree_skb_any(skb);
93 if (unlikely(vif->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED)) {
94 pr_err_ratelimited("%s: VIF not initialized\n", ndev->name);
95 dev_kfree_skb_any(skb);
100 if (unlikely(!mac)) {
101 pr_err_ratelimited("%s: NULL mac pointer", ndev->name);
102 dev_kfree_skb_any(skb);
106 if (!skb->len || (skb->len > ETH_FRAME_LEN)) {
107 pr_err_ratelimited("%s: invalid skb len %d\n", ndev->name,
109 dev_kfree_skb_any(skb);
110 ndev->stats.tx_dropped++;
114 /* tx path is enabled: reset vif timeout */
115 vif->cons_tx_timeout_cnt = 0;
117 return qtnf_bus_data_tx(mac->bus, skb);
120 /* Netdev handler for getting stats.
122 static void qtnf_netdev_get_stats64(struct net_device *ndev,
123 struct rtnl_link_stats64 *stats)
125 struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
129 netdev_stats_to_stats64(stats, &ndev->stats);
134 for_each_possible_cpu(cpu) {
135 struct pcpu_sw_netstats *stats64;
136 u64 rx_packets, rx_bytes;
137 u64 tx_packets, tx_bytes;
139 stats64 = per_cpu_ptr(vif->stats64, cpu);
142 start = u64_stats_fetch_begin_irq(&stats64->syncp);
143 rx_packets = stats64->rx_packets;
144 rx_bytes = stats64->rx_bytes;
145 tx_packets = stats64->tx_packets;
146 tx_bytes = stats64->tx_bytes;
147 } while (u64_stats_fetch_retry_irq(&stats64->syncp, start));
149 stats->rx_packets += rx_packets;
150 stats->rx_bytes += rx_bytes;
151 stats->tx_packets += tx_packets;
152 stats->tx_bytes += tx_bytes;
156 /* Netdev handler for transmission timeout.
158 static void qtnf_netdev_tx_timeout(struct net_device *ndev)
160 struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
161 struct qtnf_wmac *mac;
162 struct qtnf_bus *bus;
164 if (unlikely(!vif || !vif->mac || !vif->mac->bus))
170 pr_warn("VIF%u.%u: Tx timeout- %lu\n", mac->macid, vif->vifid, jiffies);
172 qtnf_bus_data_tx_timeout(bus, ndev);
173 ndev->stats.tx_errors++;
175 if (++vif->cons_tx_timeout_cnt > QTNF_TX_TIMEOUT_TRSHLD) {
176 pr_err("Tx timeout threshold exceeded !\n");
177 pr_err("schedule interface %s reset !\n", netdev_name(ndev));
178 queue_work(bus->workqueue, &vif->reset_work);
182 static int qtnf_netdev_set_mac_address(struct net_device *ndev, void *addr)
184 struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
185 struct sockaddr *sa = addr;
187 unsigned char old_addr[ETH_ALEN];
189 memcpy(old_addr, sa->sa_data, sizeof(old_addr));
191 ret = eth_mac_addr(ndev, sa);
195 qtnf_scan_done(vif->mac, true);
197 ret = qtnf_cmd_send_change_intf_type(vif, vif->wdev.iftype,
201 memcpy(ndev->dev_addr, old_addr, ETH_ALEN);
206 /* Network device ops handlers */
207 const struct net_device_ops qtnf_netdev_ops = {
208 .ndo_open = qtnf_netdev_open,
209 .ndo_stop = qtnf_netdev_close,
210 .ndo_start_xmit = qtnf_netdev_hard_start_xmit,
211 .ndo_tx_timeout = qtnf_netdev_tx_timeout,
212 .ndo_get_stats64 = qtnf_netdev_get_stats64,
213 .ndo_set_mac_address = qtnf_netdev_set_mac_address,
216 static int qtnf_mac_init_single_band(struct wiphy *wiphy,
217 struct qtnf_wmac *mac,
218 enum nl80211_band band)
222 wiphy->bands[band] = kzalloc(sizeof(*wiphy->bands[band]), GFP_KERNEL);
223 if (!wiphy->bands[band])
226 wiphy->bands[band]->band = band;
228 ret = qtnf_cmd_band_info_get(mac, wiphy->bands[band]);
230 pr_err("MAC%u: band %u: failed to get chans info: %d\n",
231 mac->macid, band, ret);
235 qtnf_band_init_rates(wiphy->bands[band]);
240 static int qtnf_mac_init_bands(struct qtnf_wmac *mac)
242 struct wiphy *wiphy = priv_to_wiphy(mac);
245 if (mac->macinfo.bands_cap & QLINK_BAND_2GHZ) {
246 ret = qtnf_mac_init_single_band(wiphy, mac, NL80211_BAND_2GHZ);
251 if (mac->macinfo.bands_cap & QLINK_BAND_5GHZ) {
252 ret = qtnf_mac_init_single_band(wiphy, mac, NL80211_BAND_5GHZ);
257 if (mac->macinfo.bands_cap & QLINK_BAND_60GHZ)
258 ret = qtnf_mac_init_single_band(wiphy, mac, NL80211_BAND_60GHZ);
264 struct qtnf_vif *qtnf_mac_get_free_vif(struct qtnf_wmac *mac)
266 struct qtnf_vif *vif;
269 for (i = 0; i < QTNF_MAX_INTF; i++) {
270 vif = &mac->iflist[i];
271 if (vif->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED)
278 struct qtnf_vif *qtnf_mac_get_base_vif(struct qtnf_wmac *mac)
280 struct qtnf_vif *vif;
282 vif = &mac->iflist[QTNF_PRIMARY_VIF_IDX];
284 if (vif->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED)
290 void qtnf_mac_iface_comb_free(struct qtnf_wmac *mac)
292 struct ieee80211_iface_combination *comb;
295 if (mac->macinfo.if_comb) {
296 for (i = 0; i < mac->macinfo.n_if_comb; i++) {
297 comb = &mac->macinfo.if_comb[i];
302 kfree(mac->macinfo.if_comb);
303 mac->macinfo.if_comb = NULL;
307 void qtnf_mac_ext_caps_free(struct qtnf_wmac *mac)
309 if (mac->macinfo.extended_capabilities_len) {
310 kfree(mac->macinfo.extended_capabilities);
311 mac->macinfo.extended_capabilities = NULL;
313 kfree(mac->macinfo.extended_capabilities_mask);
314 mac->macinfo.extended_capabilities_mask = NULL;
316 mac->macinfo.extended_capabilities_len = 0;
320 static void qtnf_vif_reset_handler(struct work_struct *work)
322 struct qtnf_vif *vif = container_of(work, struct qtnf_vif, reset_work);
326 if (vif->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED) {
331 /* stop tx completely */
332 netif_tx_stop_all_queues(vif->netdev);
333 if (netif_carrier_ok(vif->netdev))
334 netif_carrier_off(vif->netdev);
336 qtnf_cfg80211_vif_reset(vif);
341 static void qtnf_mac_init_primary_intf(struct qtnf_wmac *mac)
343 struct qtnf_vif *vif = &mac->iflist[QTNF_PRIMARY_VIF_IDX];
345 vif->wdev.iftype = NL80211_IFTYPE_STATION;
346 vif->bss_priority = QTNF_DEF_BSS_PRIORITY;
347 vif->wdev.wiphy = priv_to_wiphy(mac);
348 INIT_WORK(&vif->reset_work, qtnf_vif_reset_handler);
349 vif->cons_tx_timeout_cnt = 0;
352 static void qtnf_mac_scan_finish(struct qtnf_wmac *mac, bool aborted)
354 struct cfg80211_scan_info info = {
358 mutex_lock(&mac->mac_lock);
361 cfg80211_scan_done(mac->scan_req, &info);
362 mac->scan_req = NULL;
365 mutex_unlock(&mac->mac_lock);
368 void qtnf_scan_done(struct qtnf_wmac *mac, bool aborted)
370 cancel_delayed_work_sync(&mac->scan_timeout);
371 qtnf_mac_scan_finish(mac, aborted);
374 static void qtnf_mac_scan_timeout(struct work_struct *work)
376 struct qtnf_wmac *mac =
377 container_of(work, struct qtnf_wmac, scan_timeout.work);
379 pr_warn("MAC%d: scan timed out\n", mac->macid);
380 qtnf_mac_scan_finish(mac, true);
383 static struct qtnf_wmac *qtnf_core_mac_alloc(struct qtnf_bus *bus,
387 struct qtnf_wmac *mac;
390 wiphy = qtnf_wiphy_allocate(bus);
392 return ERR_PTR(-ENOMEM);
394 mac = wiphy_priv(wiphy);
399 for (i = 0; i < QTNF_MAX_INTF; i++) {
400 memset(&mac->iflist[i], 0, sizeof(struct qtnf_vif));
401 mac->iflist[i].wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
402 mac->iflist[i].mac = mac;
403 mac->iflist[i].vifid = i;
404 qtnf_sta_list_init(&mac->iflist[i].sta_list);
405 mutex_init(&mac->mac_lock);
406 INIT_DELAYED_WORK(&mac->scan_timeout, qtnf_mac_scan_timeout);
407 mac->iflist[i].stats64 =
408 netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
409 if (!mac->iflist[i].stats64)
410 pr_warn("VIF%u.%u: per cpu stats allocation failed\n",
414 qtnf_mac_init_primary_intf(mac);
415 bus->mac[macid] = mac;
420 static const struct ethtool_ops qtnf_ethtool_ops = {
421 .get_drvinfo = cfg80211_get_drvinfo,
424 int qtnf_core_net_attach(struct qtnf_wmac *mac, struct qtnf_vif *vif,
425 const char *name, unsigned char name_assign_type)
427 struct wiphy *wiphy = priv_to_wiphy(mac);
428 struct net_device *dev;
432 dev = alloc_netdev_mqs(sizeof(struct qtnf_vif *), name,
433 name_assign_type, ether_setup, 1, 1);
435 vif->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
441 dev->netdev_ops = &qtnf_netdev_ops;
442 dev->needs_free_netdev = true;
443 dev_net_set(dev, wiphy_net(wiphy));
444 dev->ieee80211_ptr = &vif->wdev;
445 ether_addr_copy(dev->dev_addr, vif->mac_addr);
446 SET_NETDEV_DEV(dev, wiphy_dev(wiphy));
447 dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
448 dev->watchdog_timeo = QTNF_DEF_WDOG_TIMEOUT;
449 dev->tx_queue_len = 100;
450 dev->ethtool_ops = &qtnf_ethtool_ops;
452 qdev_vif = netdev_priv(dev);
453 *((void **)qdev_vif) = vif;
455 SET_NETDEV_DEV(dev, mac->bus->dev);
457 ret = register_netdevice(dev);
460 vif->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
466 static void qtnf_core_mac_detach(struct qtnf_bus *bus, unsigned int macid)
468 struct qtnf_wmac *mac;
470 struct qtnf_vif *vif;
472 enum nl80211_band band;
474 mac = bus->mac[macid];
479 wiphy = priv_to_wiphy(mac);
481 for (i = 0; i < QTNF_MAX_INTF; i++) {
482 vif = &mac->iflist[i];
485 vif->wdev.iftype != NL80211_IFTYPE_UNSPECIFIED) {
486 qtnf_virtual_intf_cleanup(vif->netdev);
487 qtnf_del_virtual_intf(wiphy, &vif->wdev);
490 qtnf_sta_list_free(&vif->sta_list);
491 free_percpu(vif->stats64);
494 if (mac->wiphy_registered)
495 wiphy_unregister(wiphy);
497 for (band = NL80211_BAND_2GHZ; band < NUM_NL80211_BANDS; ++band) {
498 if (!wiphy->bands[band])
501 kfree(wiphy->bands[band]->channels);
502 wiphy->bands[band]->n_channels = 0;
504 kfree(wiphy->bands[band]);
505 wiphy->bands[band] = NULL;
508 qtnf_mac_iface_comb_free(mac);
509 qtnf_mac_ext_caps_free(mac);
510 kfree(mac->macinfo.wowlan);
512 bus->mac[macid] = NULL;
515 static int qtnf_core_mac_attach(struct qtnf_bus *bus, unsigned int macid)
517 struct qtnf_wmac *mac;
518 struct qtnf_vif *vif;
521 if (!(bus->hw_info.mac_bitmap & BIT(macid))) {
522 pr_info("MAC%u is not active in FW\n", macid);
526 mac = qtnf_core_mac_alloc(bus, macid);
528 pr_err("MAC%u allocation failed\n", macid);
532 ret = qtnf_cmd_get_mac_info(mac);
534 pr_err("MAC%u: failed to get info\n", macid);
538 vif = qtnf_mac_get_base_vif(mac);
540 pr_err("MAC%u: primary VIF is not ready\n", macid);
545 ret = qtnf_cmd_send_add_intf(vif, vif->wdev.iftype, vif->mac_addr);
547 pr_err("MAC%u: failed to add VIF\n", macid);
551 ret = qtnf_cmd_send_get_phy_params(mac);
553 pr_err("MAC%u: failed to get PHY settings\n", macid);
557 ret = qtnf_mac_init_bands(mac);
559 pr_err("MAC%u: failed to init bands\n", macid);
563 ret = qtnf_wiphy_register(&bus->hw_info, mac);
565 pr_err("MAC%u: wiphy registration failed\n", macid);
569 mac->wiphy_registered = 1;
573 ret = qtnf_core_net_attach(mac, vif, "wlan%d", NET_NAME_ENUM);
577 pr_err("MAC%u: failed to attach netdev\n", macid);
578 vif->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
583 pr_debug("MAC%u initialized\n", macid);
588 qtnf_core_mac_detach(bus, macid);
592 int qtnf_core_attach(struct qtnf_bus *bus)
597 qtnf_trans_init(bus);
599 bus->fw_state = QTNF_FW_STATE_BOOT_DONE;
600 qtnf_bus_data_rx_start(bus);
602 bus->workqueue = alloc_ordered_workqueue("QTNF_BUS", 0);
603 if (!bus->workqueue) {
604 pr_err("failed to alloc main workqueue\n");
609 INIT_WORK(&bus->event_work, qtnf_event_work_handler);
611 ret = qtnf_cmd_send_init_fw(bus);
613 pr_err("failed to init FW: %d\n", ret);
617 bus->fw_state = QTNF_FW_STATE_ACTIVE;
619 ret = qtnf_cmd_get_hw_info(bus);
621 pr_err("failed to get HW info: %d\n", ret);
625 if (bus->hw_info.ql_proto_ver != QLINK_PROTO_VER) {
626 pr_err("qlink version mismatch %u != %u\n",
627 QLINK_PROTO_VER, bus->hw_info.ql_proto_ver);
628 ret = -EPROTONOSUPPORT;
632 if (bus->hw_info.num_mac > QTNF_MAX_MAC) {
633 pr_err("no support for number of MACs=%u\n",
634 bus->hw_info.num_mac);
639 for (i = 0; i < bus->hw_info.num_mac; i++) {
640 ret = qtnf_core_mac_attach(bus, i);
643 pr_err("MAC%u: attach failed: %d\n", i, ret);
651 qtnf_core_detach(bus);
655 EXPORT_SYMBOL_GPL(qtnf_core_attach);
657 void qtnf_core_detach(struct qtnf_bus *bus)
661 qtnf_bus_data_rx_stop(bus);
663 for (macid = 0; macid < QTNF_MAX_MAC; macid++)
664 qtnf_core_mac_detach(bus, macid);
666 if (bus->fw_state == QTNF_FW_STATE_ACTIVE)
667 qtnf_cmd_send_deinit_fw(bus);
669 bus->fw_state = QTNF_FW_STATE_DETACHED;
671 if (bus->workqueue) {
672 flush_workqueue(bus->workqueue);
673 destroy_workqueue(bus->workqueue);
676 kfree(bus->hw_info.rd);
677 bus->hw_info.rd = NULL;
679 qtnf_trans_free(bus);
681 EXPORT_SYMBOL_GPL(qtnf_core_detach);
683 static inline int qtnf_is_frame_meta_magic_valid(struct qtnf_frame_meta_info *m)
685 return m->magic_s == 0xAB && m->magic_e == 0xBA;
688 struct net_device *qtnf_classify_skb(struct qtnf_bus *bus, struct sk_buff *skb)
690 struct qtnf_frame_meta_info *meta;
691 struct net_device *ndev = NULL;
692 struct qtnf_wmac *mac;
693 struct qtnf_vif *vif;
695 meta = (struct qtnf_frame_meta_info *)
696 (skb_tail_pointer(skb) - sizeof(*meta));
698 if (unlikely(!qtnf_is_frame_meta_magic_valid(meta))) {
699 pr_err_ratelimited("invalid magic 0x%x:0x%x\n",
700 meta->magic_s, meta->magic_e);
704 if (unlikely(meta->macid >= QTNF_MAX_MAC)) {
705 pr_err_ratelimited("invalid mac(%u)\n", meta->macid);
709 if (unlikely(meta->ifidx >= QTNF_MAX_INTF)) {
710 pr_err_ratelimited("invalid vif(%u)\n", meta->ifidx);
714 mac = bus->mac[meta->macid];
716 if (unlikely(!mac)) {
717 pr_err_ratelimited("mac(%d) does not exist\n", meta->macid);
721 vif = &mac->iflist[meta->ifidx];
723 if (unlikely(vif->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED)) {
724 pr_err_ratelimited("vif(%u) does not exists\n", meta->ifidx);
730 if (unlikely(!ndev)) {
731 pr_err_ratelimited("netdev for wlan%u.%u does not exists\n",
732 meta->macid, meta->ifidx);
736 __skb_trim(skb, skb->len - sizeof(*meta));
741 EXPORT_SYMBOL_GPL(qtnf_classify_skb);
743 void qtnf_wake_all_queues(struct net_device *ndev)
745 struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
746 struct qtnf_wmac *mac;
747 struct qtnf_bus *bus;
751 if (unlikely(!vif || !vif->mac || !vif->mac->bus))
756 for (macid = 0; macid < QTNF_MAX_MAC; macid++) {
757 if (!(bus->hw_info.mac_bitmap & BIT(macid)))
760 mac = bus->mac[macid];
761 for (i = 0; i < QTNF_MAX_INTF; i++) {
762 vif = &mac->iflist[i];
763 if (vif->netdev && netif_queue_stopped(vif->netdev))
764 netif_tx_wake_all_queues(vif->netdev);
768 EXPORT_SYMBOL_GPL(qtnf_wake_all_queues);
770 void qtnf_update_rx_stats(struct net_device *ndev, const struct sk_buff *skb)
772 struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
773 struct pcpu_sw_netstats *stats64;
775 if (unlikely(!vif || !vif->stats64)) {
776 ndev->stats.rx_packets++;
777 ndev->stats.rx_bytes += skb->len;
781 stats64 = this_cpu_ptr(vif->stats64);
783 u64_stats_update_begin(&stats64->syncp);
784 stats64->rx_packets++;
785 stats64->rx_bytes += skb->len;
786 u64_stats_update_end(&stats64->syncp);
788 EXPORT_SYMBOL_GPL(qtnf_update_rx_stats);
790 void qtnf_update_tx_stats(struct net_device *ndev, const struct sk_buff *skb)
792 struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
793 struct pcpu_sw_netstats *stats64;
795 if (unlikely(!vif || !vif->stats64)) {
796 ndev->stats.tx_packets++;
797 ndev->stats.tx_bytes += skb->len;
801 stats64 = this_cpu_ptr(vif->stats64);
803 u64_stats_update_begin(&stats64->syncp);
804 stats64->tx_packets++;
805 stats64->tx_bytes += skb->len;
806 u64_stats_update_end(&stats64->syncp);
808 EXPORT_SYMBOL_GPL(qtnf_update_tx_stats);
810 MODULE_AUTHOR("Quantenna Communications");
811 MODULE_DESCRIPTION("Quantenna 802.11 wireless LAN FullMAC driver.");
812 MODULE_LICENSE("GPL");