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_band_info_get(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
]);
186 static int qtnf_mac_init_bands(struct qtnf_wmac
*mac
)
188 struct wiphy
*wiphy
= priv_to_wiphy(mac
);
191 if (mac
->macinfo
.bands_cap
& QLINK_BAND_2GHZ
) {
192 ret
= qtnf_mac_init_single_band(wiphy
, mac
, NL80211_BAND_2GHZ
);
197 if (mac
->macinfo
.bands_cap
& QLINK_BAND_5GHZ
) {
198 ret
= qtnf_mac_init_single_band(wiphy
, mac
, NL80211_BAND_5GHZ
);
203 if (mac
->macinfo
.bands_cap
& QLINK_BAND_60GHZ
)
204 ret
= qtnf_mac_init_single_band(wiphy
, mac
, NL80211_BAND_60GHZ
);
210 struct qtnf_vif
*qtnf_mac_get_free_vif(struct qtnf_wmac
*mac
)
212 struct qtnf_vif
*vif
;
215 for (i
= 0; i
< QTNF_MAX_INTF
; i
++) {
216 vif
= &mac
->iflist
[i
];
217 if (vif
->wdev
.iftype
== NL80211_IFTYPE_UNSPECIFIED
)
224 struct qtnf_vif
*qtnf_mac_get_base_vif(struct qtnf_wmac
*mac
)
226 struct qtnf_vif
*vif
;
228 vif
= &mac
->iflist
[QTNF_PRIMARY_VIF_IDX
];
230 if (vif
->wdev
.iftype
== NL80211_IFTYPE_UNSPECIFIED
)
236 static void qtnf_vif_reset_handler(struct work_struct
*work
)
238 struct qtnf_vif
*vif
= container_of(work
, struct qtnf_vif
, reset_work
);
242 if (vif
->wdev
.iftype
== NL80211_IFTYPE_UNSPECIFIED
) {
247 /* stop tx completely */
248 netif_tx_stop_all_queues(vif
->netdev
);
249 if (netif_carrier_ok(vif
->netdev
))
250 netif_carrier_off(vif
->netdev
);
252 qtnf_cfg80211_vif_reset(vif
);
257 static void qtnf_mac_init_primary_intf(struct qtnf_wmac
*mac
)
259 struct qtnf_vif
*vif
= &mac
->iflist
[QTNF_PRIMARY_VIF_IDX
];
261 vif
->wdev
.iftype
= NL80211_IFTYPE_AP
;
262 vif
->bss_priority
= QTNF_DEF_BSS_PRIORITY
;
263 vif
->wdev
.wiphy
= priv_to_wiphy(mac
);
264 INIT_WORK(&vif
->reset_work
, qtnf_vif_reset_handler
);
265 vif
->cons_tx_timeout_cnt
= 0;
268 static struct qtnf_wmac
*qtnf_core_mac_alloc(struct qtnf_bus
*bus
,
272 struct qtnf_wmac
*mac
;
275 wiphy
= qtnf_wiphy_allocate(bus
);
277 return ERR_PTR(-ENOMEM
);
279 mac
= wiphy_priv(wiphy
);
284 for (i
= 0; i
< QTNF_MAX_INTF
; i
++) {
285 memset(&mac
->iflist
[i
], 0, sizeof(struct qtnf_vif
));
286 mac
->iflist
[i
].wdev
.iftype
= NL80211_IFTYPE_UNSPECIFIED
;
287 mac
->iflist
[i
].mac
= mac
;
288 mac
->iflist
[i
].vifid
= i
;
289 qtnf_sta_list_init(&mac
->iflist
[i
].sta_list
);
290 mutex_init(&mac
->mac_lock
);
291 timer_setup(&mac
->scan_timeout
, NULL
, 0);
294 qtnf_mac_init_primary_intf(mac
);
295 bus
->mac
[macid
] = mac
;
300 int qtnf_core_net_attach(struct qtnf_wmac
*mac
, struct qtnf_vif
*vif
,
301 const char *name
, unsigned char name_assign_type
,
302 enum nl80211_iftype iftype
)
304 struct wiphy
*wiphy
= priv_to_wiphy(mac
);
305 struct net_device
*dev
;
309 dev
= alloc_netdev_mqs(sizeof(struct qtnf_vif
*), name
,
310 name_assign_type
, ether_setup
, 1, 1);
312 memset(&vif
->wdev
, 0, sizeof(vif
->wdev
));
313 vif
->wdev
.iftype
= NL80211_IFTYPE_UNSPECIFIED
;
319 dev
->netdev_ops
= &qtnf_netdev_ops
;
320 dev
->needs_free_netdev
= true;
321 dev_net_set(dev
, wiphy_net(wiphy
));
322 dev
->ieee80211_ptr
= &vif
->wdev
;
323 dev
->ieee80211_ptr
->iftype
= iftype
;
324 ether_addr_copy(dev
->dev_addr
, vif
->mac_addr
);
325 SET_NETDEV_DEV(dev
, wiphy_dev(wiphy
));
326 dev
->flags
|= IFF_BROADCAST
| IFF_MULTICAST
;
327 dev
->watchdog_timeo
= QTNF_DEF_WDOG_TIMEOUT
;
328 dev
->tx_queue_len
= 100;
330 qdev_vif
= netdev_priv(dev
);
331 *((void **)qdev_vif
) = vif
;
333 SET_NETDEV_DEV(dev
, mac
->bus
->dev
);
335 ret
= register_netdevice(dev
);
338 vif
->wdev
.iftype
= NL80211_IFTYPE_UNSPECIFIED
;
344 static void qtnf_core_mac_detach(struct qtnf_bus
*bus
, unsigned int macid
)
346 struct qtnf_wmac
*mac
;
348 struct qtnf_vif
*vif
;
350 enum nl80211_band band
;
352 mac
= bus
->mac
[macid
];
357 wiphy
= priv_to_wiphy(mac
);
359 for (i
= 0; i
< QTNF_MAX_INTF
; i
++) {
360 vif
= &mac
->iflist
[i
];
363 vif
->wdev
.iftype
!= NL80211_IFTYPE_UNSPECIFIED
) {
364 qtnf_virtual_intf_cleanup(vif
->netdev
);
365 qtnf_del_virtual_intf(wiphy
, &vif
->wdev
);
368 qtnf_sta_list_free(&vif
->sta_list
);
371 if (mac
->wiphy_registered
)
372 wiphy_unregister(wiphy
);
374 for (band
= NL80211_BAND_2GHZ
; band
< NUM_NL80211_BANDS
; ++band
) {
375 if (!wiphy
->bands
[band
])
378 kfree(wiphy
->bands
[band
]->channels
);
379 wiphy
->bands
[band
]->n_channels
= 0;
381 kfree(wiphy
->bands
[band
]);
382 wiphy
->bands
[band
] = NULL
;
385 kfree(mac
->macinfo
.limits
);
386 kfree(wiphy
->iface_combinations
);
388 bus
->mac
[macid
] = NULL
;
391 static int qtnf_core_mac_attach(struct qtnf_bus
*bus
, unsigned int macid
)
393 struct qtnf_wmac
*mac
;
394 struct qtnf_vif
*vif
;
397 if (!(bus
->hw_info
.mac_bitmap
& BIT(macid
))) {
398 pr_info("MAC%u is not active in FW\n", macid
);
402 mac
= qtnf_core_mac_alloc(bus
, macid
);
404 pr_err("MAC%u allocation failed\n", macid
);
408 ret
= qtnf_cmd_get_mac_info(mac
);
410 pr_err("MAC%u: failed to get info\n", macid
);
414 vif
= qtnf_mac_get_base_vif(mac
);
416 pr_err("MAC%u: primary VIF is not ready\n", macid
);
421 ret
= qtnf_cmd_send_add_intf(vif
, NL80211_IFTYPE_AP
, vif
->mac_addr
);
423 pr_err("MAC%u: failed to add VIF\n", macid
);
427 ret
= qtnf_cmd_send_get_phy_params(mac
);
429 pr_err("MAC%u: failed to get PHY settings\n", macid
);
433 ret
= qtnf_mac_init_bands(mac
);
435 pr_err("MAC%u: failed to init bands\n", macid
);
439 ret
= qtnf_wiphy_register(&bus
->hw_info
, mac
);
441 pr_err("MAC%u: wiphy registration failed\n", macid
);
445 mac
->wiphy_registered
= 1;
449 ret
= qtnf_core_net_attach(mac
, vif
, "wlan%d", NET_NAME_ENUM
,
454 pr_err("MAC%u: failed to attach netdev\n", macid
);
455 vif
->wdev
.iftype
= NL80211_IFTYPE_UNSPECIFIED
;
460 pr_debug("MAC%u initialized\n", macid
);
465 qtnf_core_mac_detach(bus
, macid
);
469 int qtnf_core_attach(struct qtnf_bus
*bus
)
474 qtnf_trans_init(bus
);
476 bus
->fw_state
= QTNF_FW_STATE_BOOT_DONE
;
477 qtnf_bus_data_rx_start(bus
);
479 bus
->workqueue
= alloc_ordered_workqueue("QTNF_BUS", 0);
480 if (!bus
->workqueue
) {
481 pr_err("failed to alloc main workqueue\n");
486 INIT_WORK(&bus
->event_work
, qtnf_event_work_handler
);
488 ret
= qtnf_cmd_send_init_fw(bus
);
490 pr_err("failed to init FW: %d\n", ret
);
494 bus
->fw_state
= QTNF_FW_STATE_ACTIVE
;
496 ret
= qtnf_cmd_get_hw_info(bus
);
498 pr_err("failed to get HW info: %d\n", ret
);
502 if (bus
->hw_info
.ql_proto_ver
!= QLINK_PROTO_VER
) {
503 pr_err("qlink version mismatch %u != %u\n",
504 QLINK_PROTO_VER
, bus
->hw_info
.ql_proto_ver
);
505 ret
= -EPROTONOSUPPORT
;
509 if (bus
->hw_info
.num_mac
> QTNF_MAX_MAC
) {
510 pr_err("no support for number of MACs=%u\n",
511 bus
->hw_info
.num_mac
);
516 for (i
= 0; i
< bus
->hw_info
.num_mac
; i
++) {
517 ret
= qtnf_core_mac_attach(bus
, i
);
520 pr_err("MAC%u: attach failed: %d\n", i
, ret
);
528 qtnf_core_detach(bus
);
532 EXPORT_SYMBOL_GPL(qtnf_core_attach
);
534 void qtnf_core_detach(struct qtnf_bus
*bus
)
538 qtnf_bus_data_rx_stop(bus
);
540 for (macid
= 0; macid
< QTNF_MAX_MAC
; macid
++)
541 qtnf_core_mac_detach(bus
, macid
);
543 if (bus
->fw_state
== QTNF_FW_STATE_ACTIVE
)
544 qtnf_cmd_send_deinit_fw(bus
);
546 bus
->fw_state
= QTNF_FW_STATE_DEAD
;
548 if (bus
->workqueue
) {
549 flush_workqueue(bus
->workqueue
);
550 destroy_workqueue(bus
->workqueue
);
553 kfree(bus
->hw_info
.rd
);
554 bus
->hw_info
.rd
= NULL
;
556 qtnf_trans_free(bus
);
558 EXPORT_SYMBOL_GPL(qtnf_core_detach
);
560 static inline int qtnf_is_frame_meta_magic_valid(struct qtnf_frame_meta_info
*m
)
562 return m
->magic_s
== 0xAB && m
->magic_e
== 0xBA;
565 struct net_device
*qtnf_classify_skb(struct qtnf_bus
*bus
, struct sk_buff
*skb
)
567 struct qtnf_frame_meta_info
*meta
;
568 struct net_device
*ndev
= NULL
;
569 struct qtnf_wmac
*mac
;
570 struct qtnf_vif
*vif
;
572 meta
= (struct qtnf_frame_meta_info
*)
573 (skb_tail_pointer(skb
) - sizeof(*meta
));
575 if (unlikely(!qtnf_is_frame_meta_magic_valid(meta
))) {
576 pr_err_ratelimited("invalid magic 0x%x:0x%x\n",
577 meta
->magic_s
, meta
->magic_e
);
581 if (unlikely(meta
->macid
>= QTNF_MAX_MAC
)) {
582 pr_err_ratelimited("invalid mac(%u)\n", meta
->macid
);
586 if (unlikely(meta
->ifidx
>= QTNF_MAX_INTF
)) {
587 pr_err_ratelimited("invalid vif(%u)\n", meta
->ifidx
);
591 mac
= bus
->mac
[meta
->macid
];
593 if (unlikely(!mac
)) {
594 pr_err_ratelimited("mac(%d) does not exist\n", meta
->macid
);
598 vif
= &mac
->iflist
[meta
->ifidx
];
600 if (unlikely(vif
->wdev
.iftype
== NL80211_IFTYPE_UNSPECIFIED
)) {
601 pr_err_ratelimited("vif(%u) does not exists\n", meta
->ifidx
);
607 if (unlikely(!ndev
)) {
608 pr_err_ratelimited("netdev for wlan%u.%u does not exists\n",
609 meta
->macid
, meta
->ifidx
);
613 __skb_trim(skb
, skb
->len
- sizeof(*meta
));
618 EXPORT_SYMBOL_GPL(qtnf_classify_skb
);
620 void qtnf_wake_all_queues(struct net_device
*ndev
)
622 struct qtnf_vif
*vif
= qtnf_netdev_get_priv(ndev
);
623 struct qtnf_wmac
*mac
;
624 struct qtnf_bus
*bus
;
628 if (unlikely(!vif
|| !vif
->mac
|| !vif
->mac
->bus
))
633 for (macid
= 0; macid
< QTNF_MAX_MAC
; macid
++) {
634 if (!(bus
->hw_info
.mac_bitmap
& BIT(macid
)))
637 mac
= bus
->mac
[macid
];
638 for (i
= 0; i
< QTNF_MAX_INTF
; i
++) {
639 vif
= &mac
->iflist
[i
];
640 if (vif
->netdev
&& netif_queue_stopped(vif
->netdev
))
641 netif_tx_wake_all_queues(vif
->netdev
);
645 EXPORT_SYMBOL_GPL(qtnf_wake_all_queues
);
647 MODULE_AUTHOR("Quantenna Communications");
648 MODULE_DESCRIPTION("Quantenna 802.11 wireless LAN FullMAC driver.");
649 MODULE_LICENSE("GPL");