Merge branch 'x86-pti-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-block.git] / drivers / net / wireless / quantenna / qtnfmac / core.c
CommitLineData
ff233cb5
SM
1// SPDX-License-Identifier: GPL-2.0+
2/* Copyright (c) 2015-2016 Quantenna Communications. All rights reserved. */
98f44cb0
IM
3
4#include <linux/kernel.h>
5#include <linux/module.h>
6#include <linux/if_ether.h>
7
8#include "core.h"
9#include "bus.h"
10#include "trans.h"
11#include "commands.h"
12#include "cfg80211.h"
13#include "event.h"
14#include "util.h"
1db35994 15#include "switchdev.h"
98f44cb0
IM
16
17#define QTNF_DMP_MAX_LEN 48
18#define QTNF_PRIMARY_VIF_IDX 0
19
888f1564
IM
20static bool slave_radar = true;
21module_param(slave_radar, bool, 0644);
22MODULE_PARM_DESC(slave_radar, "set 0 to disable radar detection in slave mode");
23
0b68fe10
SM
24static struct dentry *qtnf_debugfs_dir;
25
98f44cb0
IM
26struct qtnf_wmac *qtnf_core_get_mac(const struct qtnf_bus *bus, u8 macid)
27{
28 struct qtnf_wmac *mac = NULL;
29
30 if (unlikely(macid >= QTNF_MAX_MAC)) {
31 pr_err("invalid MAC index %u\n", macid);
32 return NULL;
33 }
34
35 mac = bus->mac[macid];
36
37 if (unlikely(!mac)) {
38 pr_err("MAC%u: not initialized\n", macid);
39 return NULL;
40 }
41
42 return mac;
43}
44
45/* Netdev handler for open.
46 */
47static int qtnf_netdev_open(struct net_device *ndev)
48{
49 netif_carrier_off(ndev);
50 qtnf_netdev_updown(ndev, 1);
51 return 0;
52}
53
54/* Netdev handler for close.
55 */
56static int qtnf_netdev_close(struct net_device *ndev)
57{
58 netif_carrier_off(ndev);
59 qtnf_virtual_intf_cleanup(ndev);
60 qtnf_netdev_updown(ndev, 0);
61 return 0;
62}
63
46d55fce
SM
64static void qtnf_packet_send_hi_pri(struct sk_buff *skb)
65{
66 struct qtnf_vif *vif = qtnf_netdev_get_priv(skb->dev);
67
68 skb_queue_tail(&vif->high_pri_tx_queue, skb);
69 queue_work(vif->mac->bus->hprio_workqueue, &vif->high_pri_tx_work);
70}
71
98f44cb0
IM
72/* Netdev handler for data transmission.
73 */
3ff6ee28 74static netdev_tx_t
98f44cb0
IM
75qtnf_netdev_hard_start_xmit(struct sk_buff *skb, struct net_device *ndev)
76{
77 struct qtnf_vif *vif;
78 struct qtnf_wmac *mac;
79
80 vif = qtnf_netdev_get_priv(ndev);
81
82 if (unlikely(skb->dev != ndev)) {
83 pr_err_ratelimited("invalid skb->dev");
84 dev_kfree_skb_any(skb);
85 return 0;
86 }
87
88 if (unlikely(vif->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED)) {
89 pr_err_ratelimited("%s: VIF not initialized\n", ndev->name);
90 dev_kfree_skb_any(skb);
91 return 0;
92 }
93
94 mac = vif->mac;
95 if (unlikely(!mac)) {
96 pr_err_ratelimited("%s: NULL mac pointer", ndev->name);
97 dev_kfree_skb_any(skb);
98 return 0;
99 }
100
101 if (!skb->len || (skb->len > ETH_FRAME_LEN)) {
102 pr_err_ratelimited("%s: invalid skb len %d\n", ndev->name,
103 skb->len);
104 dev_kfree_skb_any(skb);
105 ndev->stats.tx_dropped++;
106 return 0;
107 }
108
109 /* tx path is enabled: reset vif timeout */
110 vif->cons_tx_timeout_cnt = 0;
111
46d55fce
SM
112 if (unlikely(skb->protocol == htons(ETH_P_PAE))) {
113 qtnf_packet_send_hi_pri(skb);
114 qtnf_update_tx_stats(ndev, skb);
115 return NETDEV_TX_OK;
116 }
117
904628d3 118 return qtnf_bus_data_tx(mac->bus, skb, mac->macid, vif->vifid);
98f44cb0
IM
119}
120
121/* Netdev handler for getting stats.
122 */
04b01aff
VU
123static void qtnf_netdev_get_stats64(struct net_device *ndev,
124 struct rtnl_link_stats64 *stats)
98f44cb0 125{
04b01aff
VU
126 struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
127 unsigned int start;
128 int cpu;
129
130 netdev_stats_to_stats64(stats, &ndev->stats);
131
132 if (!vif->stats64)
133 return;
134
135 for_each_possible_cpu(cpu) {
136 struct pcpu_sw_netstats *stats64;
137 u64 rx_packets, rx_bytes;
138 u64 tx_packets, tx_bytes;
139
140 stats64 = per_cpu_ptr(vif->stats64, cpu);
141
142 do {
143 start = u64_stats_fetch_begin_irq(&stats64->syncp);
144 rx_packets = stats64->rx_packets;
145 rx_bytes = stats64->rx_bytes;
146 tx_packets = stats64->tx_packets;
147 tx_bytes = stats64->tx_bytes;
148 } while (u64_stats_fetch_retry_irq(&stats64->syncp, start));
149
150 stats->rx_packets += rx_packets;
151 stats->rx_bytes += rx_bytes;
152 stats->tx_packets += tx_packets;
153 stats->tx_bytes += tx_bytes;
154 }
98f44cb0
IM
155}
156
157/* Netdev handler for transmission timeout.
158 */
159static void qtnf_netdev_tx_timeout(struct net_device *ndev)
160{
161 struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
162 struct qtnf_wmac *mac;
163 struct qtnf_bus *bus;
164
165 if (unlikely(!vif || !vif->mac || !vif->mac->bus))
166 return;
167
168 mac = vif->mac;
169 bus = mac->bus;
170
171 pr_warn("VIF%u.%u: Tx timeout- %lu\n", mac->macid, vif->vifid, jiffies);
172
173 qtnf_bus_data_tx_timeout(bus, ndev);
174 ndev->stats.tx_errors++;
175
176 if (++vif->cons_tx_timeout_cnt > QTNF_TX_TIMEOUT_TRSHLD) {
177 pr_err("Tx timeout threshold exceeded !\n");
178 pr_err("schedule interface %s reset !\n", netdev_name(ndev));
179 queue_work(bus->workqueue, &vif->reset_work);
180 }
181}
182
ed9f34bb
IM
183static int qtnf_netdev_set_mac_address(struct net_device *ndev, void *addr)
184{
185 struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
186 struct sockaddr *sa = addr;
187 int ret;
188 unsigned char old_addr[ETH_ALEN];
189
190 memcpy(old_addr, sa->sa_data, sizeof(old_addr));
191
192 ret = eth_mac_addr(ndev, sa);
193 if (ret)
194 return ret;
195
196 qtnf_scan_done(vif->mac, true);
197
198 ret = qtnf_cmd_send_change_intf_type(vif, vif->wdev.iftype,
de624a35 199 vif->wdev.use_4addr,
ed9f34bb
IM
200 sa->sa_data);
201
202 if (ret)
203 memcpy(ndev->dev_addr, old_addr, ETH_ALEN);
204
205 return ret;
206}
207
4e14e76c
IM
208static int qtnf_netdev_port_parent_id(struct net_device *ndev,
209 struct netdev_phys_item_id *ppid)
210{
211 const struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
212 const struct qtnf_bus *bus = vif->mac->bus;
213
214 if (!(bus->hw_info.hw_capab & QLINK_HW_CAPAB_HW_BRIDGE))
215 return -EOPNOTSUPP;
216
217 ppid->id_len = sizeof(bus->hw_id);
218 memcpy(&ppid->id, bus->hw_id, ppid->id_len);
219
220 return 0;
221}
222
98f44cb0
IM
223/* Network device ops handlers */
224const struct net_device_ops qtnf_netdev_ops = {
225 .ndo_open = qtnf_netdev_open,
226 .ndo_stop = qtnf_netdev_close,
227 .ndo_start_xmit = qtnf_netdev_hard_start_xmit,
228 .ndo_tx_timeout = qtnf_netdev_tx_timeout,
04b01aff 229 .ndo_get_stats64 = qtnf_netdev_get_stats64,
ed9f34bb 230 .ndo_set_mac_address = qtnf_netdev_set_mac_address,
4e14e76c 231 .ndo_get_port_parent_id = qtnf_netdev_port_parent_id,
98f44cb0
IM
232};
233
234static int qtnf_mac_init_single_band(struct wiphy *wiphy,
235 struct qtnf_wmac *mac,
236 enum nl80211_band band)
237{
238 int ret;
239
240 wiphy->bands[band] = kzalloc(sizeof(*wiphy->bands[band]), GFP_KERNEL);
241 if (!wiphy->bands[band])
242 return -ENOMEM;
243
244 wiphy->bands[band]->band = band;
245
e294cbfd 246 ret = qtnf_cmd_band_info_get(mac, wiphy->bands[band]);
98f44cb0
IM
247 if (ret) {
248 pr_err("MAC%u: band %u: failed to get chans info: %d\n",
249 mac->macid, band, ret);
250 return ret;
251 }
252
253 qtnf_band_init_rates(wiphy->bands[band]);
98f44cb0
IM
254
255 return 0;
256}
257
258static int qtnf_mac_init_bands(struct qtnf_wmac *mac)
259{
260 struct wiphy *wiphy = priv_to_wiphy(mac);
261 int ret = 0;
262
263 if (mac->macinfo.bands_cap & QLINK_BAND_2GHZ) {
264 ret = qtnf_mac_init_single_band(wiphy, mac, NL80211_BAND_2GHZ);
265 if (ret)
266 goto out;
267 }
268
269 if (mac->macinfo.bands_cap & QLINK_BAND_5GHZ) {
270 ret = qtnf_mac_init_single_band(wiphy, mac, NL80211_BAND_5GHZ);
271 if (ret)
272 goto out;
273 }
274
275 if (mac->macinfo.bands_cap & QLINK_BAND_60GHZ)
276 ret = qtnf_mac_init_single_band(wiphy, mac, NL80211_BAND_60GHZ);
277
278out:
279 return ret;
280}
281
282struct qtnf_vif *qtnf_mac_get_free_vif(struct qtnf_wmac *mac)
283{
284 struct qtnf_vif *vif;
285 int i;
286
287 for (i = 0; i < QTNF_MAX_INTF; i++) {
288 vif = &mac->iflist[i];
289 if (vif->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED)
290 return vif;
291 }
292
293 return NULL;
294}
295
296struct qtnf_vif *qtnf_mac_get_base_vif(struct qtnf_wmac *mac)
297{
298 struct qtnf_vif *vif;
299
300 vif = &mac->iflist[QTNF_PRIMARY_VIF_IDX];
301
302 if (vif->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED)
303 return NULL;
304
305 return vif;
306}
307
537faf26
SM
308void qtnf_mac_iface_comb_free(struct qtnf_wmac *mac)
309{
310 struct ieee80211_iface_combination *comb;
311 int i;
312
313 if (mac->macinfo.if_comb) {
314 for (i = 0; i < mac->macinfo.n_if_comb; i++) {
315 comb = &mac->macinfo.if_comb[i];
316 kfree(comb->limits);
317 comb->limits = NULL;
318 }
319
320 kfree(mac->macinfo.if_comb);
321 mac->macinfo.if_comb = NULL;
322 }
323}
324
ab1c64a1
SM
325void qtnf_mac_ext_caps_free(struct qtnf_wmac *mac)
326{
327 if (mac->macinfo.extended_capabilities_len) {
328 kfree(mac->macinfo.extended_capabilities);
329 mac->macinfo.extended_capabilities = NULL;
330
331 kfree(mac->macinfo.extended_capabilities_mask);
332 mac->macinfo.extended_capabilities_mask = NULL;
333
334 mac->macinfo.extended_capabilities_len = 0;
335 }
336}
337
98f44cb0
IM
338static void qtnf_vif_reset_handler(struct work_struct *work)
339{
340 struct qtnf_vif *vif = container_of(work, struct qtnf_vif, reset_work);
341
342 rtnl_lock();
343
344 if (vif->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED) {
345 rtnl_unlock();
346 return;
347 }
348
349 /* stop tx completely */
350 netif_tx_stop_all_queues(vif->netdev);
351 if (netif_carrier_ok(vif->netdev))
352 netif_carrier_off(vif->netdev);
353
354 qtnf_cfg80211_vif_reset(vif);
355
356 rtnl_unlock();
357}
358
359static void qtnf_mac_init_primary_intf(struct qtnf_wmac *mac)
360{
361 struct qtnf_vif *vif = &mac->iflist[QTNF_PRIMARY_VIF_IDX];
362
e6ef8cd0 363 vif->wdev.iftype = NL80211_IFTYPE_STATION;
98f44cb0
IM
364 vif->bss_priority = QTNF_DEF_BSS_PRIORITY;
365 vif->wdev.wiphy = priv_to_wiphy(mac);
366 INIT_WORK(&vif->reset_work, qtnf_vif_reset_handler);
367 vif->cons_tx_timeout_cnt = 0;
368}
369
f2cddd54
IM
370static void qtnf_mac_scan_finish(struct qtnf_wmac *mac, bool aborted)
371{
372 struct cfg80211_scan_info info = {
373 .aborted = aborted,
374 };
375
376 mutex_lock(&mac->mac_lock);
377
378 if (mac->scan_req) {
379 cfg80211_scan_done(mac->scan_req, &info);
380 mac->scan_req = NULL;
381 }
382
383 mutex_unlock(&mac->mac_lock);
384}
385
386void qtnf_scan_done(struct qtnf_wmac *mac, bool aborted)
387{
388 cancel_delayed_work_sync(&mac->scan_timeout);
389 qtnf_mac_scan_finish(mac, aborted);
390}
391
392static void qtnf_mac_scan_timeout(struct work_struct *work)
393{
394 struct qtnf_wmac *mac =
395 container_of(work, struct qtnf_wmac, scan_timeout.work);
396
397 pr_warn("MAC%d: scan timed out\n", mac->macid);
398 qtnf_mac_scan_finish(mac, true);
399}
400
bc70732f
IM
401static void qtnf_vif_send_data_high_pri(struct work_struct *work)
402{
403 struct qtnf_vif *vif =
404 container_of(work, struct qtnf_vif, high_pri_tx_work);
405 struct sk_buff *skb;
406
407 if (!vif->netdev ||
408 vif->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED)
409 return;
410
411 while ((skb = skb_dequeue(&vif->high_pri_tx_queue))) {
412 qtnf_cmd_send_frame(vif, 0, QLINK_FRAME_TX_FLAG_8023,
413 0, skb->data, skb->len);
414 dev_kfree_skb_any(skb);
415 }
416}
417
98f44cb0
IM
418static struct qtnf_wmac *qtnf_core_mac_alloc(struct qtnf_bus *bus,
419 unsigned int macid)
420{
75001bbc 421 struct qtnf_vif *vif;
98f44cb0
IM
422 struct wiphy *wiphy;
423 struct qtnf_wmac *mac;
424 unsigned int i;
425
426 wiphy = qtnf_wiphy_allocate(bus);
427 if (!wiphy)
428 return ERR_PTR(-ENOMEM);
429
430 mac = wiphy_priv(wiphy);
431
432 mac->macid = macid;
433 mac->bus = bus;
75001bbc
IM
434 mutex_init(&mac->mac_lock);
435 INIT_DELAYED_WORK(&mac->scan_timeout, qtnf_mac_scan_timeout);
98f44cb0
IM
436
437 for (i = 0; i < QTNF_MAX_INTF; i++) {
75001bbc
IM
438 vif = &mac->iflist[i];
439
440 memset(vif, 0, sizeof(*vif));
441 vif->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
442 vif->mac = mac;
443 vif->vifid = i;
444 qtnf_sta_list_init(&vif->sta_list);
bc70732f
IM
445 INIT_WORK(&vif->high_pri_tx_work, qtnf_vif_send_data_high_pri);
446 skb_queue_head_init(&vif->high_pri_tx_queue);
75001bbc
IM
447 vif->stats64 = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
448 if (!vif->stats64)
04b01aff
VU
449 pr_warn("VIF%u.%u: per cpu stats allocation failed\n",
450 macid, i);
98f44cb0
IM
451 }
452
453 qtnf_mac_init_primary_intf(mac);
454 bus->mac[macid] = mac;
455
456 return mac;
457}
458
888f1564
IM
459bool qtnf_mac_slave_radar_get(struct wiphy *wiphy)
460{
461 return slave_radar;
462}
463
0b419d01
VU
464static const struct ethtool_ops qtnf_ethtool_ops = {
465 .get_drvinfo = cfg80211_get_drvinfo,
466};
467
98f44cb0 468int qtnf_core_net_attach(struct qtnf_wmac *mac, struct qtnf_vif *vif,
e6ef8cd0 469 const char *name, unsigned char name_assign_type)
98f44cb0
IM
470{
471 struct wiphy *wiphy = priv_to_wiphy(mac);
472 struct net_device *dev;
473 void *qdev_vif;
474 int ret;
475
476 dev = alloc_netdev_mqs(sizeof(struct qtnf_vif *), name,
477 name_assign_type, ether_setup, 1, 1);
45028223 478 if (!dev)
98f44cb0 479 return -ENOMEM;
98f44cb0
IM
480
481 vif->netdev = dev;
482
483 dev->netdev_ops = &qtnf_netdev_ops;
0ddead90 484 dev->needs_free_netdev = true;
98f44cb0
IM
485 dev_net_set(dev, wiphy_net(wiphy));
486 dev->ieee80211_ptr = &vif->wdev;
98f44cb0
IM
487 ether_addr_copy(dev->dev_addr, vif->mac_addr);
488 SET_NETDEV_DEV(dev, wiphy_dev(wiphy));
489 dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
490 dev->watchdog_timeo = QTNF_DEF_WDOG_TIMEOUT;
491 dev->tx_queue_len = 100;
0b419d01 492 dev->ethtool_ops = &qtnf_ethtool_ops;
98f44cb0 493
904628d3
IM
494 if (mac->bus->hw_info.hw_capab & QLINK_HW_CAPAB_HW_BRIDGE)
495 dev->needed_tailroom = sizeof(struct qtnf_frame_meta_info);
496
98f44cb0
IM
497 qdev_vif = netdev_priv(dev);
498 *((void **)qdev_vif) = vif;
499
500 SET_NETDEV_DEV(dev, mac->bus->dev);
501
502 ret = register_netdevice(dev);
503 if (ret) {
504 free_netdev(dev);
45028223 505 vif->netdev = NULL;
98f44cb0
IM
506 }
507
508 return ret;
509}
510
511static void qtnf_core_mac_detach(struct qtnf_bus *bus, unsigned int macid)
512{
513 struct qtnf_wmac *mac;
514 struct wiphy *wiphy;
515 struct qtnf_vif *vif;
516 unsigned int i;
517 enum nl80211_band band;
518
519 mac = bus->mac[macid];
520
521 if (!mac)
522 return;
523
524 wiphy = priv_to_wiphy(mac);
525
526 for (i = 0; i < QTNF_MAX_INTF; i++) {
527 vif = &mac->iflist[i];
528 rtnl_lock();
529 if (vif->netdev &&
530 vif->wdev.iftype != NL80211_IFTYPE_UNSPECIFIED) {
531 qtnf_virtual_intf_cleanup(vif->netdev);
532 qtnf_del_virtual_intf(wiphy, &vif->wdev);
533 }
534 rtnl_unlock();
535 qtnf_sta_list_free(&vif->sta_list);
04b01aff 536 free_percpu(vif->stats64);
98f44cb0
IM
537 }
538
539 if (mac->wiphy_registered)
540 wiphy_unregister(wiphy);
541
542 for (band = NL80211_BAND_2GHZ; band < NUM_NL80211_BANDS; ++band) {
543 if (!wiphy->bands[band])
544 continue;
545
df0af4c7
MK
546 kfree(wiphy->bands[band]->iftype_data);
547 wiphy->bands[band]->n_iftype_data = 0;
548
98f44cb0
IM
549 kfree(wiphy->bands[band]->channels);
550 wiphy->bands[band]->n_channels = 0;
551
552 kfree(wiphy->bands[band]);
553 wiphy->bands[band] = NULL;
554 }
555
537faf26 556 qtnf_mac_iface_comb_free(mac);
ab1c64a1 557 qtnf_mac_ext_caps_free(mac);
28b91884 558 kfree(mac->macinfo.wowlan);
c698bce0
IM
559 kfree(mac->rd);
560 mac->rd = NULL;
98f44cb0
IM
561 wiphy_free(wiphy);
562 bus->mac[macid] = NULL;
563}
564
565static int qtnf_core_mac_attach(struct qtnf_bus *bus, unsigned int macid)
566{
567 struct qtnf_wmac *mac;
568 struct qtnf_vif *vif;
569 int ret;
570
571 if (!(bus->hw_info.mac_bitmap & BIT(macid))) {
572 pr_info("MAC%u is not active in FW\n", macid);
573 return 0;
574 }
575
576 mac = qtnf_core_mac_alloc(bus, macid);
577 if (IS_ERR(mac)) {
578 pr_err("MAC%u allocation failed\n", macid);
579 return PTR_ERR(mac);
580 }
581
582 ret = qtnf_cmd_get_mac_info(mac);
583 if (ret) {
584 pr_err("MAC%u: failed to get info\n", macid);
585 goto error;
586 }
587
4e14e76c
IM
588 /* Use MAC address of the first active radio as a unique device ID */
589 if (is_zero_ether_addr(mac->bus->hw_id))
590 ether_addr_copy(mac->bus->hw_id, mac->macaddr);
591
98f44cb0
IM
592 vif = qtnf_mac_get_base_vif(mac);
593 if (!vif) {
594 pr_err("MAC%u: primary VIF is not ready\n", macid);
595 ret = -EFAULT;
596 goto error;
597 }
598
de624a35
SM
599 ret = qtnf_cmd_send_add_intf(vif, vif->wdev.iftype,
600 vif->wdev.use_4addr, vif->mac_addr);
98f44cb0
IM
601 if (ret) {
602 pr_err("MAC%u: failed to add VIF\n", macid);
603 goto error;
604 }
605
606 ret = qtnf_cmd_send_get_phy_params(mac);
607 if (ret) {
608 pr_err("MAC%u: failed to get PHY settings\n", macid);
45028223 609 goto error_del_vif;
98f44cb0
IM
610 }
611
612 ret = qtnf_mac_init_bands(mac);
613 if (ret) {
614 pr_err("MAC%u: failed to init bands\n", macid);
45028223 615 goto error_del_vif;
98f44cb0
IM
616 }
617
618 ret = qtnf_wiphy_register(&bus->hw_info, mac);
619 if (ret) {
620 pr_err("MAC%u: wiphy registration failed\n", macid);
45028223 621 goto error_del_vif;
98f44cb0
IM
622 }
623
624 mac->wiphy_registered = 1;
625
626 rtnl_lock();
627
e6ef8cd0 628 ret = qtnf_core_net_attach(mac, vif, "wlan%d", NET_NAME_ENUM);
98f44cb0
IM
629 rtnl_unlock();
630
631 if (ret) {
632 pr_err("MAC%u: failed to attach netdev\n", macid);
45028223 633 goto error_del_vif;
98f44cb0
IM
634 }
635
decfc5c7
IM
636 if (bus->hw_info.hw_capab & QLINK_HW_CAPAB_HW_BRIDGE) {
637 ret = qtnf_cmd_netdev_changeupper(vif, vif->netdev->ifindex);
638 if (ret)
639 goto error;
640 }
641
98f44cb0
IM
642 pr_debug("MAC%u initialized\n", macid);
643
644 return 0;
645
45028223
IM
646error_del_vif:
647 qtnf_cmd_send_del_intf(vif);
648 vif->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
98f44cb0
IM
649error:
650 qtnf_core_mac_detach(bus, macid);
651 return ret;
652}
653
decfc5c7
IM
654bool qtnf_netdev_is_qtn(const struct net_device *ndev)
655{
656 return ndev->netdev_ops == &qtnf_netdev_ops;
657}
658
659static int qtnf_core_netdevice_event(struct notifier_block *nb,
660 unsigned long event, void *ptr)
661{
662 struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
663 const struct netdev_notifier_changeupper_info *info;
664 struct qtnf_vif *vif;
665 int br_domain;
666 int ret = 0;
667
668 if (!qtnf_netdev_is_qtn(ndev))
669 return NOTIFY_DONE;
670
671 if (!net_eq(dev_net(ndev), &init_net))
672 return NOTIFY_OK;
673
674 vif = qtnf_netdev_get_priv(ndev);
675
676 switch (event) {
677 case NETDEV_CHANGEUPPER:
678 info = ptr;
679
680 if (!netif_is_bridge_master(info->upper_dev))
681 break;
682
683 pr_debug("[VIF%u.%u] change bridge: %s %s\n",
684 vif->mac->macid, vif->vifid,
685 netdev_name(info->upper_dev),
686 info->linking ? "add" : "del");
687
688 if (info->linking)
689 br_domain = info->upper_dev->ifindex;
690 else
691 br_domain = ndev->ifindex;
692
693 ret = qtnf_cmd_netdev_changeupper(vif, br_domain);
694 break;
695 default:
696 break;
697 }
698
699 return notifier_from_errno(ret);
700}
701
98f44cb0
IM
702int qtnf_core_attach(struct qtnf_bus *bus)
703{
704 unsigned int i;
705 int ret;
706
707 qtnf_trans_init(bus);
98f44cb0
IM
708 qtnf_bus_data_rx_start(bus);
709
710 bus->workqueue = alloc_ordered_workqueue("QTNF_BUS", 0);
711 if (!bus->workqueue) {
712 pr_err("failed to alloc main workqueue\n");
713 ret = -ENOMEM;
714 goto error;
715 }
716
bc70732f
IM
717 bus->hprio_workqueue = alloc_workqueue("QTNF_HPRI", WQ_HIGHPRI, 0);
718 if (!bus->hprio_workqueue) {
719 pr_err("failed to alloc high prio workqueue\n");
720 ret = -ENOMEM;
721 goto error;
722 }
723
98f44cb0
IM
724 INIT_WORK(&bus->event_work, qtnf_event_work_handler);
725
726 ret = qtnf_cmd_send_init_fw(bus);
727 if (ret) {
728 pr_err("failed to init FW: %d\n", ret);
729 goto error;
730 }
731
732 bus->fw_state = QTNF_FW_STATE_ACTIVE;
98f44cb0
IM
733 ret = qtnf_cmd_get_hw_info(bus);
734 if (ret) {
735 pr_err("failed to get HW info: %d\n", ret);
736 goto error;
737 }
738
739 if (bus->hw_info.ql_proto_ver != QLINK_PROTO_VER) {
740 pr_err("qlink version mismatch %u != %u\n",
741 QLINK_PROTO_VER, bus->hw_info.ql_proto_ver);
742 ret = -EPROTONOSUPPORT;
743 goto error;
744 }
745
904628d3
IM
746 if ((bus->hw_info.hw_capab & QLINK_HW_CAPAB_HW_BRIDGE) &&
747 bus->bus_ops->data_tx_use_meta_set)
748 bus->bus_ops->data_tx_use_meta_set(bus, true);
749
98f44cb0
IM
750 if (bus->hw_info.num_mac > QTNF_MAX_MAC) {
751 pr_err("no support for number of MACs=%u\n",
752 bus->hw_info.num_mac);
753 ret = -ERANGE;
754 goto error;
755 }
756
757 for (i = 0; i < bus->hw_info.num_mac; i++) {
758 ret = qtnf_core_mac_attach(bus, i);
759
760 if (ret) {
761 pr_err("MAC%u: attach failed: %d\n", i, ret);
762 goto error;
763 }
764 }
765
decfc5c7
IM
766 if (bus->hw_info.hw_capab & QLINK_HW_CAPAB_HW_BRIDGE) {
767 bus->netdev_nb.notifier_call = qtnf_core_netdevice_event;
768 ret = register_netdevice_notifier(&bus->netdev_nb);
769 if (ret) {
770 pr_err("failed to register netdev notifier: %d\n", ret);
771 goto error;
772 }
773 }
774
83b00f6e 775 bus->fw_state = QTNF_FW_STATE_RUNNING;
98f44cb0
IM
776 return 0;
777
778error:
779 qtnf_core_detach(bus);
98f44cb0
IM
780 return ret;
781}
782EXPORT_SYMBOL_GPL(qtnf_core_attach);
783
784void qtnf_core_detach(struct qtnf_bus *bus)
785{
786 unsigned int macid;
787
decfc5c7 788 unregister_netdevice_notifier(&bus->netdev_nb);
98f44cb0
IM
789 qtnf_bus_data_rx_stop(bus);
790
791 for (macid = 0; macid < QTNF_MAX_MAC; macid++)
792 qtnf_core_mac_detach(bus, macid);
793
83b00f6e 794 if (qtnf_fw_is_up(bus))
98f44cb0
IM
795 qtnf_cmd_send_deinit_fw(bus);
796
9e33e7fb 797 bus->fw_state = QTNF_FW_STATE_DETACHED;
98f44cb0
IM
798
799 if (bus->workqueue) {
800 flush_workqueue(bus->workqueue);
801 destroy_workqueue(bus->workqueue);
bc70732f
IM
802 bus->workqueue = NULL;
803 }
804
805 if (bus->hprio_workqueue) {
806 flush_workqueue(bus->hprio_workqueue);
807 destroy_workqueue(bus->hprio_workqueue);
808 bus->hprio_workqueue = NULL;
98f44cb0
IM
809 }
810
811 qtnf_trans_free(bus);
812}
813EXPORT_SYMBOL_GPL(qtnf_core_detach);
814
815static inline int qtnf_is_frame_meta_magic_valid(struct qtnf_frame_meta_info *m)
816{
904628d3
IM
817 return m->magic_s == HBM_FRAME_META_MAGIC_PATTERN_S &&
818 m->magic_e == HBM_FRAME_META_MAGIC_PATTERN_E;
98f44cb0
IM
819}
820
821struct net_device *qtnf_classify_skb(struct qtnf_bus *bus, struct sk_buff *skb)
822{
823 struct qtnf_frame_meta_info *meta;
824 struct net_device *ndev = NULL;
825 struct qtnf_wmac *mac;
826 struct qtnf_vif *vif;
827
83b00f6e
SM
828 if (unlikely(bus->fw_state != QTNF_FW_STATE_RUNNING))
829 return NULL;
830
98f44cb0
IM
831 meta = (struct qtnf_frame_meta_info *)
832 (skb_tail_pointer(skb) - sizeof(*meta));
833
834 if (unlikely(!qtnf_is_frame_meta_magic_valid(meta))) {
835 pr_err_ratelimited("invalid magic 0x%x:0x%x\n",
836 meta->magic_s, meta->magic_e);
837 goto out;
838 }
839
840 if (unlikely(meta->macid >= QTNF_MAX_MAC)) {
841 pr_err_ratelimited("invalid mac(%u)\n", meta->macid);
842 goto out;
843 }
844
845 if (unlikely(meta->ifidx >= QTNF_MAX_INTF)) {
846 pr_err_ratelimited("invalid vif(%u)\n", meta->ifidx);
847 goto out;
848 }
849
850 mac = bus->mac[meta->macid];
851
852 if (unlikely(!mac)) {
853 pr_err_ratelimited("mac(%d) does not exist\n", meta->macid);
854 goto out;
855 }
856
857 vif = &mac->iflist[meta->ifidx];
858
859 if (unlikely(vif->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED)) {
860 pr_err_ratelimited("vif(%u) does not exists\n", meta->ifidx);
861 goto out;
862 }
863
864 ndev = vif->netdev;
865
866 if (unlikely(!ndev)) {
867 pr_err_ratelimited("netdev for wlan%u.%u does not exists\n",
868 meta->macid, meta->ifidx);
869 goto out;
870 }
871
872 __skb_trim(skb, skb->len - sizeof(*meta));
1db35994
IM
873 /* Firmware always handles packets that require flooding */
874 qtnfmac_switch_mark_skb_flooded(skb);
98f44cb0
IM
875
876out:
877 return ndev;
878}
879EXPORT_SYMBOL_GPL(qtnf_classify_skb);
880
c35c0d54
SM
881void qtnf_wake_all_queues(struct net_device *ndev)
882{
883 struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
884 struct qtnf_wmac *mac;
885 struct qtnf_bus *bus;
886 int macid;
887 int i;
888
889 if (unlikely(!vif || !vif->mac || !vif->mac->bus))
890 return;
891
892 bus = vif->mac->bus;
893
894 for (macid = 0; macid < QTNF_MAX_MAC; macid++) {
895 if (!(bus->hw_info.mac_bitmap & BIT(macid)))
896 continue;
897
898 mac = bus->mac[macid];
899 for (i = 0; i < QTNF_MAX_INTF; i++) {
900 vif = &mac->iflist[i];
901 if (vif->netdev && netif_queue_stopped(vif->netdev))
902 netif_tx_wake_all_queues(vif->netdev);
903 }
904 }
905}
906EXPORT_SYMBOL_GPL(qtnf_wake_all_queues);
907
04b01aff
VU
908void qtnf_update_rx_stats(struct net_device *ndev, const struct sk_buff *skb)
909{
910 struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
911 struct pcpu_sw_netstats *stats64;
912
913 if (unlikely(!vif || !vif->stats64)) {
914 ndev->stats.rx_packets++;
915 ndev->stats.rx_bytes += skb->len;
916 return;
917 }
918
919 stats64 = this_cpu_ptr(vif->stats64);
920
921 u64_stats_update_begin(&stats64->syncp);
922 stats64->rx_packets++;
923 stats64->rx_bytes += skb->len;
924 u64_stats_update_end(&stats64->syncp);
925}
926EXPORT_SYMBOL_GPL(qtnf_update_rx_stats);
927
928void qtnf_update_tx_stats(struct net_device *ndev, const struct sk_buff *skb)
929{
930 struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
931 struct pcpu_sw_netstats *stats64;
932
933 if (unlikely(!vif || !vif->stats64)) {
934 ndev->stats.tx_packets++;
935 ndev->stats.tx_bytes += skb->len;
936 return;
937 }
938
939 stats64 = this_cpu_ptr(vif->stats64);
940
941 u64_stats_update_begin(&stats64->syncp);
942 stats64->tx_packets++;
943 stats64->tx_bytes += skb->len;
944 u64_stats_update_end(&stats64->syncp);
945}
946EXPORT_SYMBOL_GPL(qtnf_update_tx_stats);
947
0b68fe10
SM
948struct dentry *qtnf_get_debugfs_dir(void)
949{
950 return qtnf_debugfs_dir;
951}
952EXPORT_SYMBOL_GPL(qtnf_get_debugfs_dir);
953
954static int __init qtnf_core_register(void)
955{
956 qtnf_debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
957
958 if (IS_ERR(qtnf_debugfs_dir))
959 qtnf_debugfs_dir = NULL;
960
961 return 0;
962}
963
964static void __exit qtnf_core_exit(void)
965{
966 debugfs_remove(qtnf_debugfs_dir);
967}
968
969module_init(qtnf_core_register);
970module_exit(qtnf_core_exit);
971
98f44cb0
IM
972MODULE_AUTHOR("Quantenna Communications");
973MODULE_DESCRIPTION("Quantenna 802.11 wireless LAN FullMAC driver.");
974MODULE_LICENSE("GPL");