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 struct net_device_stats *qtnf_netdev_get_stats(struct net_device *dev)
127 /* Netdev handler for transmission timeout.
129 static void qtnf_netdev_tx_timeout(struct net_device *ndev)
131 struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
132 struct qtnf_wmac *mac;
133 struct qtnf_bus *bus;
135 if (unlikely(!vif || !vif->mac || !vif->mac->bus))
141 pr_warn("VIF%u.%u: Tx timeout- %lu\n", mac->macid, vif->vifid, jiffies);
143 qtnf_bus_data_tx_timeout(bus, ndev);
144 ndev->stats.tx_errors++;
146 if (++vif->cons_tx_timeout_cnt > QTNF_TX_TIMEOUT_TRSHLD) {
147 pr_err("Tx timeout threshold exceeded !\n");
148 pr_err("schedule interface %s reset !\n", netdev_name(ndev));
149 queue_work(bus->workqueue, &vif->reset_work);
153 /* Network device ops handlers */
154 const struct net_device_ops qtnf_netdev_ops = {
155 .ndo_open = qtnf_netdev_open,
156 .ndo_stop = qtnf_netdev_close,
157 .ndo_start_xmit = qtnf_netdev_hard_start_xmit,
158 .ndo_tx_timeout = qtnf_netdev_tx_timeout,
159 .ndo_get_stats = qtnf_netdev_get_stats,
162 static int qtnf_mac_init_single_band(struct wiphy *wiphy,
163 struct qtnf_wmac *mac,
164 enum nl80211_band band)
168 wiphy->bands[band] = kzalloc(sizeof(*wiphy->bands[band]), GFP_KERNEL);
169 if (!wiphy->bands[band])
172 wiphy->bands[band]->band = band;
174 ret = qtnf_cmd_get_mac_chan_info(mac, wiphy->bands[band]);
176 pr_err("MAC%u: band %u: failed to get chans info: %d\n",
177 mac->macid, band, ret);
181 qtnf_band_init_rates(wiphy->bands[band]);
182 qtnf_band_setup_htvht_caps(&mac->macinfo, wiphy->bands[band]);
187 static int qtnf_mac_init_bands(struct qtnf_wmac *mac)
189 struct wiphy *wiphy = priv_to_wiphy(mac);
192 if (mac->macinfo.bands_cap & QLINK_BAND_2GHZ) {
193 ret = qtnf_mac_init_single_band(wiphy, mac, NL80211_BAND_2GHZ);
198 if (mac->macinfo.bands_cap & QLINK_BAND_5GHZ) {
199 ret = qtnf_mac_init_single_band(wiphy, mac, NL80211_BAND_5GHZ);
204 if (mac->macinfo.bands_cap & QLINK_BAND_60GHZ)
205 ret = qtnf_mac_init_single_band(wiphy, mac, NL80211_BAND_60GHZ);
211 struct qtnf_vif *qtnf_mac_get_free_vif(struct qtnf_wmac *mac)
213 struct qtnf_vif *vif;
216 for (i = 0; i < QTNF_MAX_INTF; i++) {
217 vif = &mac->iflist[i];
218 if (vif->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED)
225 struct qtnf_vif *qtnf_mac_get_base_vif(struct qtnf_wmac *mac)
227 struct qtnf_vif *vif;
229 vif = &mac->iflist[QTNF_PRIMARY_VIF_IDX];
231 if (vif->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED)
237 static void qtnf_vif_reset_handler(struct work_struct *work)
239 struct qtnf_vif *vif = container_of(work, struct qtnf_vif, reset_work);
243 if (vif->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED) {
248 /* stop tx completely */
249 netif_tx_stop_all_queues(vif->netdev);
250 if (netif_carrier_ok(vif->netdev))
251 netif_carrier_off(vif->netdev);
253 qtnf_cfg80211_vif_reset(vif);
258 static void qtnf_mac_init_primary_intf(struct qtnf_wmac *mac)
260 struct qtnf_vif *vif = &mac->iflist[QTNF_PRIMARY_VIF_IDX];
262 vif->wdev.iftype = NL80211_IFTYPE_AP;
263 vif->bss_priority = QTNF_DEF_BSS_PRIORITY;
264 vif->wdev.wiphy = priv_to_wiphy(mac);
265 INIT_WORK(&vif->reset_work, qtnf_vif_reset_handler);
266 vif->cons_tx_timeout_cnt = 0;
269 static struct qtnf_wmac *qtnf_core_mac_alloc(struct qtnf_bus *bus,
273 struct qtnf_wmac *mac;
276 wiphy = qtnf_wiphy_allocate(bus);
278 return ERR_PTR(-ENOMEM);
280 mac = wiphy_priv(wiphy);
285 for (i = 0; i < QTNF_MAX_INTF; i++) {
286 memset(&mac->iflist[i], 0, sizeof(struct qtnf_vif));
287 mac->iflist[i].wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
288 mac->iflist[i].mac = mac;
289 mac->iflist[i].vifid = i;
290 qtnf_sta_list_init(&mac->iflist[i].sta_list);
291 mutex_init(&mac->mac_lock);
292 init_timer(&mac->scan_timeout);
295 qtnf_mac_init_primary_intf(mac);
296 bus->mac[macid] = mac;
301 int qtnf_core_net_attach(struct qtnf_wmac *mac, struct qtnf_vif *vif,
302 const char *name, unsigned char name_assign_type,
303 enum nl80211_iftype iftype)
305 struct wiphy *wiphy = priv_to_wiphy(mac);
306 struct net_device *dev;
310 dev = alloc_netdev_mqs(sizeof(struct qtnf_vif *), name,
311 name_assign_type, ether_setup, 1, 1);
313 memset(&vif->wdev, 0, sizeof(vif->wdev));
314 vif->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
320 dev->netdev_ops = &qtnf_netdev_ops;
321 dev->needs_free_netdev = true;
322 dev_net_set(dev, wiphy_net(wiphy));
323 dev->ieee80211_ptr = &vif->wdev;
324 dev->ieee80211_ptr->iftype = iftype;
325 ether_addr_copy(dev->dev_addr, vif->mac_addr);
326 SET_NETDEV_DEV(dev, wiphy_dev(wiphy));
327 dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
328 dev->watchdog_timeo = QTNF_DEF_WDOG_TIMEOUT;
329 dev->tx_queue_len = 100;
331 qdev_vif = netdev_priv(dev);
332 *((void **)qdev_vif) = vif;
334 SET_NETDEV_DEV(dev, mac->bus->dev);
336 ret = register_netdevice(dev);
339 vif->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
345 static void qtnf_core_mac_detach(struct qtnf_bus *bus, unsigned int macid)
347 struct qtnf_wmac *mac;
349 struct qtnf_vif *vif;
351 enum nl80211_band band;
353 mac = bus->mac[macid];
358 wiphy = priv_to_wiphy(mac);
360 for (i = 0; i < QTNF_MAX_INTF; i++) {
361 vif = &mac->iflist[i];
364 vif->wdev.iftype != NL80211_IFTYPE_UNSPECIFIED) {
365 qtnf_virtual_intf_cleanup(vif->netdev);
366 qtnf_del_virtual_intf(wiphy, &vif->wdev);
369 qtnf_sta_list_free(&vif->sta_list);
372 if (mac->wiphy_registered)
373 wiphy_unregister(wiphy);
375 for (band = NL80211_BAND_2GHZ; band < NUM_NL80211_BANDS; ++band) {
376 if (!wiphy->bands[band])
379 kfree(wiphy->bands[band]->channels);
380 wiphy->bands[band]->n_channels = 0;
382 kfree(wiphy->bands[band]);
383 wiphy->bands[band] = NULL;
386 kfree(mac->macinfo.limits);
387 kfree(wiphy->iface_combinations);
389 bus->mac[macid] = NULL;
392 static int qtnf_core_mac_attach(struct qtnf_bus *bus, unsigned int macid)
394 struct qtnf_wmac *mac;
395 struct qtnf_vif *vif;
398 if (!(bus->hw_info.mac_bitmap & BIT(macid))) {
399 pr_info("MAC%u is not active in FW\n", macid);
403 mac = qtnf_core_mac_alloc(bus, macid);
405 pr_err("MAC%u allocation failed\n", macid);
409 ret = qtnf_cmd_get_mac_info(mac);
411 pr_err("MAC%u: failed to get info\n", macid);
415 vif = qtnf_mac_get_base_vif(mac);
417 pr_err("MAC%u: primary VIF is not ready\n", macid);
422 ret = qtnf_cmd_send_add_intf(vif, NL80211_IFTYPE_AP, vif->mac_addr);
424 pr_err("MAC%u: failed to add VIF\n", macid);
428 ret = qtnf_cmd_send_get_phy_params(mac);
430 pr_err("MAC%u: failed to get PHY settings\n", macid);
434 ret = qtnf_mac_init_bands(mac);
436 pr_err("MAC%u: failed to init bands\n", macid);
440 ret = qtnf_wiphy_register(&bus->hw_info, mac);
442 pr_err("MAC%u: wiphy registration failed\n", macid);
446 mac->wiphy_registered = 1;
450 ret = qtnf_core_net_attach(mac, vif, "wlan%d", NET_NAME_ENUM,
455 pr_err("MAC%u: failed to attach netdev\n", macid);
456 vif->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
461 pr_debug("MAC%u initialized\n", macid);
466 qtnf_core_mac_detach(bus, macid);
470 int qtnf_core_attach(struct qtnf_bus *bus)
475 qtnf_trans_init(bus);
477 bus->fw_state = QTNF_FW_STATE_BOOT_DONE;
478 qtnf_bus_data_rx_start(bus);
480 bus->workqueue = alloc_ordered_workqueue("QTNF_BUS", 0);
481 if (!bus->workqueue) {
482 pr_err("failed to alloc main workqueue\n");
487 INIT_WORK(&bus->event_work, qtnf_event_work_handler);
489 ret = qtnf_cmd_send_init_fw(bus);
491 pr_err("failed to init FW: %d\n", ret);
495 bus->fw_state = QTNF_FW_STATE_ACTIVE;
497 ret = qtnf_cmd_get_hw_info(bus);
499 pr_err("failed to get HW info: %d\n", ret);
503 if (bus->hw_info.ql_proto_ver != QLINK_PROTO_VER) {
504 pr_err("qlink version mismatch %u != %u\n",
505 QLINK_PROTO_VER, bus->hw_info.ql_proto_ver);
506 ret = -EPROTONOSUPPORT;
510 if (bus->hw_info.num_mac > QTNF_MAX_MAC) {
511 pr_err("no support for number of MACs=%u\n",
512 bus->hw_info.num_mac);
517 for (i = 0; i < bus->hw_info.num_mac; i++) {
518 ret = qtnf_core_mac_attach(bus, i);
521 pr_err("MAC%u: attach failed: %d\n", i, ret);
529 qtnf_core_detach(bus);
533 EXPORT_SYMBOL_GPL(qtnf_core_attach);
535 void qtnf_core_detach(struct qtnf_bus *bus)
539 qtnf_bus_data_rx_stop(bus);
541 for (macid = 0; macid < QTNF_MAX_MAC; macid++)
542 qtnf_core_mac_detach(bus, macid);
544 if (bus->fw_state == QTNF_FW_STATE_ACTIVE)
545 qtnf_cmd_send_deinit_fw(bus);
547 bus->fw_state = QTNF_FW_STATE_DEAD;
549 if (bus->workqueue) {
550 flush_workqueue(bus->workqueue);
551 destroy_workqueue(bus->workqueue);
554 kfree(bus->hw_info.rd);
555 bus->hw_info.rd = NULL;
557 qtnf_trans_free(bus);
559 EXPORT_SYMBOL_GPL(qtnf_core_detach);
561 static inline int qtnf_is_frame_meta_magic_valid(struct qtnf_frame_meta_info *m)
563 return m->magic_s == 0xAB && m->magic_e == 0xBA;
566 struct net_device *qtnf_classify_skb(struct qtnf_bus *bus, struct sk_buff *skb)
568 struct qtnf_frame_meta_info *meta;
569 struct net_device *ndev = NULL;
570 struct qtnf_wmac *mac;
571 struct qtnf_vif *vif;
573 meta = (struct qtnf_frame_meta_info *)
574 (skb_tail_pointer(skb) - sizeof(*meta));
576 if (unlikely(!qtnf_is_frame_meta_magic_valid(meta))) {
577 pr_err_ratelimited("invalid magic 0x%x:0x%x\n",
578 meta->magic_s, meta->magic_e);
582 if (unlikely(meta->macid >= QTNF_MAX_MAC)) {
583 pr_err_ratelimited("invalid mac(%u)\n", meta->macid);
587 if (unlikely(meta->ifidx >= QTNF_MAX_INTF)) {
588 pr_err_ratelimited("invalid vif(%u)\n", meta->ifidx);
592 mac = bus->mac[meta->macid];
594 if (unlikely(!mac)) {
595 pr_err_ratelimited("mac(%d) does not exist\n", meta->macid);
599 vif = &mac->iflist[meta->ifidx];
601 if (unlikely(vif->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED)) {
602 pr_err_ratelimited("vif(%u) does not exists\n", meta->ifidx);
608 if (unlikely(!ndev)) {
609 pr_err_ratelimited("netdev for wlan%u.%u does not exists\n",
610 meta->macid, meta->ifidx);
614 __skb_trim(skb, skb->len - sizeof(*meta));
619 EXPORT_SYMBOL_GPL(qtnf_classify_skb);
621 MODULE_AUTHOR("Quantenna Communications");
622 MODULE_DESCRIPTION("Quantenna 802.11 wireless LAN FullMAC driver.");
623 MODULE_LICENSE("GPL");