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