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