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 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" 15 #include "switchdev.h" 16 17 #define QTNF_DMP_MAX_LEN 48 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 struct dentry *qtnf_debugfs_dir; 25 26 struct 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 */ 47 static 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 */ 56 static 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 64 static 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 72 /* Netdev handler for data transmission. 73 */ 74 static netdev_tx_t 75 qtnf_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 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 118 return qtnf_bus_data_tx(mac->bus, skb, mac->macid, vif->vifid); 119 } 120 121 /* Netdev handler for getting stats. 122 */ 123 static void qtnf_netdev_get_stats64(struct net_device *ndev, 124 struct rtnl_link_stats64 *stats) 125 { 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 } 155 } 156 157 /* Netdev handler for transmission timeout. 158 */ 159 static 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 183 static 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, 199 vif->wdev.use_4addr, 200 sa->sa_data); 201 202 if (ret) 203 memcpy(ndev->dev_addr, old_addr, ETH_ALEN); 204 205 return ret; 206 } 207 208 static 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 223 /* Network device ops handlers */ 224 const 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, 229 .ndo_get_stats64 = qtnf_netdev_get_stats64, 230 .ndo_set_mac_address = qtnf_netdev_set_mac_address, 231 .ndo_get_port_parent_id = qtnf_netdev_port_parent_id, 232 }; 233 234 static 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 246 ret = qtnf_cmd_band_info_get(mac, wiphy->bands[band]); 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]); 254 255 return 0; 256 } 257 258 static 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 278 out: 279 return ret; 280 } 281 282 struct 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 296 struct 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 308 void 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 325 void 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 338 static 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 359 static void qtnf_mac_init_primary_intf(struct qtnf_wmac *mac) 360 { 361 struct qtnf_vif *vif = &mac->iflist[QTNF_PRIMARY_VIF_IDX]; 362 363 vif->wdev.iftype = NL80211_IFTYPE_STATION; 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 370 static 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 386 void 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 392 static 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 401 static 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 418 static struct qtnf_wmac *qtnf_core_mac_alloc(struct qtnf_bus *bus, 419 unsigned int macid) 420 { 421 struct qtnf_vif *vif; 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; 434 mutex_init(&mac->mac_lock); 435 INIT_DELAYED_WORK(&mac->scan_timeout, qtnf_mac_scan_timeout); 436 437 for (i = 0; i < QTNF_MAX_INTF; i++) { 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); 445 INIT_WORK(&vif->high_pri_tx_work, qtnf_vif_send_data_high_pri); 446 skb_queue_head_init(&vif->high_pri_tx_queue); 447 vif->stats64 = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats); 448 if (!vif->stats64) 449 pr_warn("VIF%u.%u: per cpu stats allocation failed\n", 450 macid, i); 451 } 452 453 qtnf_mac_init_primary_intf(mac); 454 bus->mac[macid] = mac; 455 456 return mac; 457 } 458 459 bool qtnf_mac_slave_radar_get(struct wiphy *wiphy) 460 { 461 return slave_radar; 462 } 463 464 static const struct ethtool_ops qtnf_ethtool_ops = { 465 .get_drvinfo = cfg80211_get_drvinfo, 466 }; 467 468 int qtnf_core_net_attach(struct qtnf_wmac *mac, struct qtnf_vif *vif, 469 const char *name, unsigned char name_assign_type) 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); 478 if (!dev) 479 return -ENOMEM; 480 481 vif->netdev = dev; 482 483 dev->netdev_ops = &qtnf_netdev_ops; 484 dev->needs_free_netdev = true; 485 dev_net_set(dev, wiphy_net(wiphy)); 486 dev->ieee80211_ptr = &vif->wdev; 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; 492 dev->ethtool_ops = &qtnf_ethtool_ops; 493 494 if (mac->bus->hw_info.hw_capab & QLINK_HW_CAPAB_HW_BRIDGE) 495 dev->needed_tailroom = sizeof(struct qtnf_frame_meta_info); 496 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); 505 vif->netdev = NULL; 506 } 507 508 return ret; 509 } 510 511 static 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); 536 free_percpu(vif->stats64); 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 546 kfree(wiphy->bands[band]->iftype_data); 547 wiphy->bands[band]->n_iftype_data = 0; 548 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 556 qtnf_mac_iface_comb_free(mac); 557 qtnf_mac_ext_caps_free(mac); 558 kfree(mac->macinfo.wowlan); 559 kfree(mac->rd); 560 mac->rd = NULL; 561 wiphy_free(wiphy); 562 bus->mac[macid] = NULL; 563 } 564 565 static 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 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 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 599 ret = qtnf_cmd_send_add_intf(vif, vif->wdev.iftype, 600 vif->wdev.use_4addr, vif->mac_addr); 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); 609 goto error_del_vif; 610 } 611 612 ret = qtnf_mac_init_bands(mac); 613 if (ret) { 614 pr_err("MAC%u: failed to init bands\n", macid); 615 goto error_del_vif; 616 } 617 618 ret = qtnf_wiphy_register(&bus->hw_info, mac); 619 if (ret) { 620 pr_err("MAC%u: wiphy registration failed\n", macid); 621 goto error_del_vif; 622 } 623 624 mac->wiphy_registered = 1; 625 626 rtnl_lock(); 627 628 ret = qtnf_core_net_attach(mac, vif, "wlan%d", NET_NAME_ENUM); 629 rtnl_unlock(); 630 631 if (ret) { 632 pr_err("MAC%u: failed to attach netdev\n", macid); 633 goto error_del_vif; 634 } 635 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 642 pr_debug("MAC%u initialized\n", macid); 643 644 return 0; 645 646 error_del_vif: 647 qtnf_cmd_send_del_intf(vif); 648 vif->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED; 649 error: 650 qtnf_core_mac_detach(bus, macid); 651 return ret; 652 } 653 654 bool qtnf_netdev_is_qtn(const struct net_device *ndev) 655 { 656 return ndev->netdev_ops == &qtnf_netdev_ops; 657 } 658 659 static 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 702 int qtnf_core_attach(struct qtnf_bus *bus) 703 { 704 unsigned int i; 705 int ret; 706 707 qtnf_trans_init(bus); 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 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 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; 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 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 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 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 775 bus->fw_state = QTNF_FW_STATE_RUNNING; 776 return 0; 777 778 error: 779 qtnf_core_detach(bus); 780 return ret; 781 } 782 EXPORT_SYMBOL_GPL(qtnf_core_attach); 783 784 void qtnf_core_detach(struct qtnf_bus *bus) 785 { 786 unsigned int macid; 787 788 unregister_netdevice_notifier(&bus->netdev_nb); 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 794 if (qtnf_fw_is_up(bus)) 795 qtnf_cmd_send_deinit_fw(bus); 796 797 bus->fw_state = QTNF_FW_STATE_DETACHED; 798 799 if (bus->workqueue) { 800 flush_workqueue(bus->workqueue); 801 destroy_workqueue(bus->workqueue); 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; 809 } 810 811 qtnf_trans_free(bus); 812 } 813 EXPORT_SYMBOL_GPL(qtnf_core_detach); 814 815 static inline int qtnf_is_frame_meta_magic_valid(struct qtnf_frame_meta_info *m) 816 { 817 return m->magic_s == HBM_FRAME_META_MAGIC_PATTERN_S && 818 m->magic_e == HBM_FRAME_META_MAGIC_PATTERN_E; 819 } 820 821 struct 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 828 if (unlikely(bus->fw_state != QTNF_FW_STATE_RUNNING)) 829 return NULL; 830 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)); 873 /* Firmware always handles packets that require flooding */ 874 qtnfmac_switch_mark_skb_flooded(skb); 875 876 out: 877 return ndev; 878 } 879 EXPORT_SYMBOL_GPL(qtnf_classify_skb); 880 881 void 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 } 906 EXPORT_SYMBOL_GPL(qtnf_wake_all_queues); 907 908 void 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 } 926 EXPORT_SYMBOL_GPL(qtnf_update_rx_stats); 927 928 void 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 } 946 EXPORT_SYMBOL_GPL(qtnf_update_tx_stats); 947 948 struct dentry *qtnf_get_debugfs_dir(void) 949 { 950 return qtnf_debugfs_dir; 951 } 952 EXPORT_SYMBOL_GPL(qtnf_get_debugfs_dir); 953 954 static 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 964 static void __exit qtnf_core_exit(void) 965 { 966 debugfs_remove(qtnf_debugfs_dir); 967 } 968 969 module_init(qtnf_core_register); 970 module_exit(qtnf_core_exit); 971 972 MODULE_AUTHOR("Quantenna Communications"); 973 MODULE_DESCRIPTION("Quantenna 802.11 wireless LAN FullMAC driver."); 974 MODULE_LICENSE("GPL"); 975