core.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. /*
  2. * Copyright (c) 2015-2016 Quantenna Communications, Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/if_ether.h>
  19. #include "core.h"
  20. #include "bus.h"
  21. #include "trans.h"
  22. #include "commands.h"
  23. #include "cfg80211.h"
  24. #include "event.h"
  25. #include "util.h"
  26. #define QTNF_DMP_MAX_LEN 48
  27. #define QTNF_PRIMARY_VIF_IDX 0
  28. struct qtnf_frame_meta_info {
  29. u8 magic_s;
  30. u8 ifidx;
  31. u8 macid;
  32. u8 magic_e;
  33. } __packed;
  34. struct qtnf_wmac *qtnf_core_get_mac(const struct qtnf_bus *bus, u8 macid)
  35. {
  36. struct qtnf_wmac *mac = NULL;
  37. if (unlikely(macid >= QTNF_MAX_MAC)) {
  38. pr_err("invalid MAC index %u\n", macid);
  39. return NULL;
  40. }
  41. mac = bus->mac[macid];
  42. if (unlikely(!mac)) {
  43. pr_err("MAC%u: not initialized\n", macid);
  44. return NULL;
  45. }
  46. return mac;
  47. }
  48. /* Netdev handler for open.
  49. */
  50. static int qtnf_netdev_open(struct net_device *ndev)
  51. {
  52. netif_carrier_off(ndev);
  53. qtnf_netdev_updown(ndev, 1);
  54. return 0;
  55. }
  56. /* Netdev handler for close.
  57. */
  58. static int qtnf_netdev_close(struct net_device *ndev)
  59. {
  60. netif_carrier_off(ndev);
  61. qtnf_virtual_intf_cleanup(ndev);
  62. qtnf_netdev_updown(ndev, 0);
  63. return 0;
  64. }
  65. /* Netdev handler for data transmission.
  66. */
  67. static int
  68. qtnf_netdev_hard_start_xmit(struct sk_buff *skb, struct net_device *ndev)
  69. {
  70. struct qtnf_vif *vif;
  71. struct qtnf_wmac *mac;
  72. vif = qtnf_netdev_get_priv(ndev);
  73. if (unlikely(skb->dev != ndev)) {
  74. pr_err_ratelimited("invalid skb->dev");
  75. dev_kfree_skb_any(skb);
  76. return 0;
  77. }
  78. if (unlikely(vif->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED)) {
  79. pr_err_ratelimited("%s: VIF not initialized\n", ndev->name);
  80. dev_kfree_skb_any(skb);
  81. return 0;
  82. }
  83. mac = vif->mac;
  84. if (unlikely(!mac)) {
  85. pr_err_ratelimited("%s: NULL mac pointer", ndev->name);
  86. dev_kfree_skb_any(skb);
  87. return 0;
  88. }
  89. if (!skb->len || (skb->len > ETH_FRAME_LEN)) {
  90. pr_err_ratelimited("%s: invalid skb len %d\n", ndev->name,
  91. skb->len);
  92. dev_kfree_skb_any(skb);
  93. ndev->stats.tx_dropped++;
  94. return 0;
  95. }
  96. /* tx path is enabled: reset vif timeout */
  97. vif->cons_tx_timeout_cnt = 0;
  98. return qtnf_bus_data_tx(mac->bus, skb);
  99. }
  100. /* Netdev handler for getting stats.
  101. */
  102. static struct net_device_stats *qtnf_netdev_get_stats(struct net_device *dev)
  103. {
  104. return &dev->stats;
  105. }
  106. /* Netdev handler for transmission timeout.
  107. */
  108. static void qtnf_netdev_tx_timeout(struct net_device *ndev)
  109. {
  110. struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
  111. struct qtnf_wmac *mac;
  112. struct qtnf_bus *bus;
  113. if (unlikely(!vif || !vif->mac || !vif->mac->bus))
  114. return;
  115. mac = vif->mac;
  116. bus = mac->bus;
  117. pr_warn("VIF%u.%u: Tx timeout- %lu\n", mac->macid, vif->vifid, jiffies);
  118. qtnf_bus_data_tx_timeout(bus, ndev);
  119. ndev->stats.tx_errors++;
  120. if (++vif->cons_tx_timeout_cnt > QTNF_TX_TIMEOUT_TRSHLD) {
  121. pr_err("Tx timeout threshold exceeded !\n");
  122. pr_err("schedule interface %s reset !\n", netdev_name(ndev));
  123. queue_work(bus->workqueue, &vif->reset_work);
  124. }
  125. }
  126. /* Network device ops handlers */
  127. const struct net_device_ops qtnf_netdev_ops = {
  128. .ndo_open = qtnf_netdev_open,
  129. .ndo_stop = qtnf_netdev_close,
  130. .ndo_start_xmit = qtnf_netdev_hard_start_xmit,
  131. .ndo_tx_timeout = qtnf_netdev_tx_timeout,
  132. .ndo_get_stats = qtnf_netdev_get_stats,
  133. };
  134. static int qtnf_mac_init_single_band(struct wiphy *wiphy,
  135. struct qtnf_wmac *mac,
  136. enum nl80211_band band)
  137. {
  138. int ret;
  139. wiphy->bands[band] = kzalloc(sizeof(*wiphy->bands[band]), GFP_KERNEL);
  140. if (!wiphy->bands[band])
  141. return -ENOMEM;
  142. wiphy->bands[band]->band = band;
  143. ret = qtnf_cmd_band_info_get(mac, wiphy->bands[band]);
  144. if (ret) {
  145. pr_err("MAC%u: band %u: failed to get chans info: %d\n",
  146. mac->macid, band, ret);
  147. return ret;
  148. }
  149. qtnf_band_init_rates(wiphy->bands[band]);
  150. return 0;
  151. }
  152. static int qtnf_mac_init_bands(struct qtnf_wmac *mac)
  153. {
  154. struct wiphy *wiphy = priv_to_wiphy(mac);
  155. int ret = 0;
  156. if (mac->macinfo.bands_cap & QLINK_BAND_2GHZ) {
  157. ret = qtnf_mac_init_single_band(wiphy, mac, NL80211_BAND_2GHZ);
  158. if (ret)
  159. goto out;
  160. }
  161. if (mac->macinfo.bands_cap & QLINK_BAND_5GHZ) {
  162. ret = qtnf_mac_init_single_band(wiphy, mac, NL80211_BAND_5GHZ);
  163. if (ret)
  164. goto out;
  165. }
  166. if (mac->macinfo.bands_cap & QLINK_BAND_60GHZ)
  167. ret = qtnf_mac_init_single_band(wiphy, mac, NL80211_BAND_60GHZ);
  168. out:
  169. return ret;
  170. }
  171. struct qtnf_vif *qtnf_mac_get_free_vif(struct qtnf_wmac *mac)
  172. {
  173. struct qtnf_vif *vif;
  174. int i;
  175. for (i = 0; i < QTNF_MAX_INTF; i++) {
  176. vif = &mac->iflist[i];
  177. if (vif->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED)
  178. return vif;
  179. }
  180. return NULL;
  181. }
  182. struct qtnf_vif *qtnf_mac_get_base_vif(struct qtnf_wmac *mac)
  183. {
  184. struct qtnf_vif *vif;
  185. vif = &mac->iflist[QTNF_PRIMARY_VIF_IDX];
  186. if (vif->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED)
  187. return NULL;
  188. return vif;
  189. }
  190. static void qtnf_vif_reset_handler(struct work_struct *work)
  191. {
  192. struct qtnf_vif *vif = container_of(work, struct qtnf_vif, reset_work);
  193. rtnl_lock();
  194. if (vif->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED) {
  195. rtnl_unlock();
  196. return;
  197. }
  198. /* stop tx completely */
  199. netif_tx_stop_all_queues(vif->netdev);
  200. if (netif_carrier_ok(vif->netdev))
  201. netif_carrier_off(vif->netdev);
  202. qtnf_cfg80211_vif_reset(vif);
  203. rtnl_unlock();
  204. }
  205. static void qtnf_mac_init_primary_intf(struct qtnf_wmac *mac)
  206. {
  207. struct qtnf_vif *vif = &mac->iflist[QTNF_PRIMARY_VIF_IDX];
  208. vif->wdev.iftype = NL80211_IFTYPE_AP;
  209. vif->bss_priority = QTNF_DEF_BSS_PRIORITY;
  210. vif->wdev.wiphy = priv_to_wiphy(mac);
  211. INIT_WORK(&vif->reset_work, qtnf_vif_reset_handler);
  212. vif->cons_tx_timeout_cnt = 0;
  213. }
  214. static struct qtnf_wmac *qtnf_core_mac_alloc(struct qtnf_bus *bus,
  215. unsigned int macid)
  216. {
  217. struct wiphy *wiphy;
  218. struct qtnf_wmac *mac;
  219. unsigned int i;
  220. wiphy = qtnf_wiphy_allocate(bus);
  221. if (!wiphy)
  222. return ERR_PTR(-ENOMEM);
  223. mac = wiphy_priv(wiphy);
  224. mac->macid = macid;
  225. mac->bus = bus;
  226. for (i = 0; i < QTNF_MAX_INTF; i++) {
  227. memset(&mac->iflist[i], 0, sizeof(struct qtnf_vif));
  228. mac->iflist[i].wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
  229. mac->iflist[i].mac = mac;
  230. mac->iflist[i].vifid = i;
  231. qtnf_sta_list_init(&mac->iflist[i].sta_list);
  232. mutex_init(&mac->mac_lock);
  233. setup_timer(&mac->scan_timeout, NULL, 0);
  234. }
  235. qtnf_mac_init_primary_intf(mac);
  236. bus->mac[macid] = mac;
  237. return mac;
  238. }
  239. int qtnf_core_net_attach(struct qtnf_wmac *mac, struct qtnf_vif *vif,
  240. const char *name, unsigned char name_assign_type,
  241. enum nl80211_iftype iftype)
  242. {
  243. struct wiphy *wiphy = priv_to_wiphy(mac);
  244. struct net_device *dev;
  245. void *qdev_vif;
  246. int ret;
  247. dev = alloc_netdev_mqs(sizeof(struct qtnf_vif *), name,
  248. name_assign_type, ether_setup, 1, 1);
  249. if (!dev) {
  250. memset(&vif->wdev, 0, sizeof(vif->wdev));
  251. vif->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
  252. return -ENOMEM;
  253. }
  254. vif->netdev = dev;
  255. dev->netdev_ops = &qtnf_netdev_ops;
  256. dev->needs_free_netdev = true;
  257. dev_net_set(dev, wiphy_net(wiphy));
  258. dev->ieee80211_ptr = &vif->wdev;
  259. dev->ieee80211_ptr->iftype = iftype;
  260. ether_addr_copy(dev->dev_addr, vif->mac_addr);
  261. SET_NETDEV_DEV(dev, wiphy_dev(wiphy));
  262. dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
  263. dev->watchdog_timeo = QTNF_DEF_WDOG_TIMEOUT;
  264. dev->tx_queue_len = 100;
  265. qdev_vif = netdev_priv(dev);
  266. *((void **)qdev_vif) = vif;
  267. SET_NETDEV_DEV(dev, mac->bus->dev);
  268. ret = register_netdevice(dev);
  269. if (ret) {
  270. free_netdev(dev);
  271. vif->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
  272. }
  273. return ret;
  274. }
  275. static void qtnf_core_mac_detach(struct qtnf_bus *bus, unsigned int macid)
  276. {
  277. struct qtnf_wmac *mac;
  278. struct wiphy *wiphy;
  279. struct qtnf_vif *vif;
  280. unsigned int i;
  281. enum nl80211_band band;
  282. mac = bus->mac[macid];
  283. if (!mac)
  284. return;
  285. wiphy = priv_to_wiphy(mac);
  286. for (i = 0; i < QTNF_MAX_INTF; i++) {
  287. vif = &mac->iflist[i];
  288. rtnl_lock();
  289. if (vif->netdev &&
  290. vif->wdev.iftype != NL80211_IFTYPE_UNSPECIFIED) {
  291. qtnf_virtual_intf_cleanup(vif->netdev);
  292. qtnf_del_virtual_intf(wiphy, &vif->wdev);
  293. }
  294. rtnl_unlock();
  295. qtnf_sta_list_free(&vif->sta_list);
  296. }
  297. if (mac->wiphy_registered)
  298. wiphy_unregister(wiphy);
  299. for (band = NL80211_BAND_2GHZ; band < NUM_NL80211_BANDS; ++band) {
  300. if (!wiphy->bands[band])
  301. continue;
  302. kfree(wiphy->bands[band]->channels);
  303. wiphy->bands[band]->n_channels = 0;
  304. kfree(wiphy->bands[band]);
  305. wiphy->bands[band] = NULL;
  306. }
  307. kfree(mac->macinfo.limits);
  308. kfree(wiphy->iface_combinations);
  309. wiphy_free(wiphy);
  310. bus->mac[macid] = NULL;
  311. }
  312. static int qtnf_core_mac_attach(struct qtnf_bus *bus, unsigned int macid)
  313. {
  314. struct qtnf_wmac *mac;
  315. struct qtnf_vif *vif;
  316. int ret;
  317. if (!(bus->hw_info.mac_bitmap & BIT(macid))) {
  318. pr_info("MAC%u is not active in FW\n", macid);
  319. return 0;
  320. }
  321. mac = qtnf_core_mac_alloc(bus, macid);
  322. if (IS_ERR(mac)) {
  323. pr_err("MAC%u allocation failed\n", macid);
  324. return PTR_ERR(mac);
  325. }
  326. ret = qtnf_cmd_get_mac_info(mac);
  327. if (ret) {
  328. pr_err("MAC%u: failed to get info\n", macid);
  329. goto error;
  330. }
  331. vif = qtnf_mac_get_base_vif(mac);
  332. if (!vif) {
  333. pr_err("MAC%u: primary VIF is not ready\n", macid);
  334. ret = -EFAULT;
  335. goto error;
  336. }
  337. ret = qtnf_cmd_send_add_intf(vif, NL80211_IFTYPE_AP, vif->mac_addr);
  338. if (ret) {
  339. pr_err("MAC%u: failed to add VIF\n", macid);
  340. goto error;
  341. }
  342. ret = qtnf_cmd_send_get_phy_params(mac);
  343. if (ret) {
  344. pr_err("MAC%u: failed to get PHY settings\n", macid);
  345. goto error;
  346. }
  347. ret = qtnf_mac_init_bands(mac);
  348. if (ret) {
  349. pr_err("MAC%u: failed to init bands\n", macid);
  350. goto error;
  351. }
  352. ret = qtnf_wiphy_register(&bus->hw_info, mac);
  353. if (ret) {
  354. pr_err("MAC%u: wiphy registration failed\n", macid);
  355. goto error;
  356. }
  357. mac->wiphy_registered = 1;
  358. rtnl_lock();
  359. ret = qtnf_core_net_attach(mac, vif, "wlan%d", NET_NAME_ENUM,
  360. NL80211_IFTYPE_AP);
  361. rtnl_unlock();
  362. if (ret) {
  363. pr_err("MAC%u: failed to attach netdev\n", macid);
  364. vif->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
  365. vif->netdev = NULL;
  366. goto error;
  367. }
  368. pr_debug("MAC%u initialized\n", macid);
  369. return 0;
  370. error:
  371. qtnf_core_mac_detach(bus, macid);
  372. return ret;
  373. }
  374. int qtnf_core_attach(struct qtnf_bus *bus)
  375. {
  376. unsigned int i;
  377. int ret;
  378. qtnf_trans_init(bus);
  379. bus->fw_state = QTNF_FW_STATE_BOOT_DONE;
  380. qtnf_bus_data_rx_start(bus);
  381. bus->workqueue = alloc_ordered_workqueue("QTNF_BUS", 0);
  382. if (!bus->workqueue) {
  383. pr_err("failed to alloc main workqueue\n");
  384. ret = -ENOMEM;
  385. goto error;
  386. }
  387. INIT_WORK(&bus->event_work, qtnf_event_work_handler);
  388. ret = qtnf_cmd_send_init_fw(bus);
  389. if (ret) {
  390. pr_err("failed to init FW: %d\n", ret);
  391. goto error;
  392. }
  393. bus->fw_state = QTNF_FW_STATE_ACTIVE;
  394. ret = qtnf_cmd_get_hw_info(bus);
  395. if (ret) {
  396. pr_err("failed to get HW info: %d\n", ret);
  397. goto error;
  398. }
  399. if (bus->hw_info.ql_proto_ver != QLINK_PROTO_VER) {
  400. pr_err("qlink version mismatch %u != %u\n",
  401. QLINK_PROTO_VER, bus->hw_info.ql_proto_ver);
  402. ret = -EPROTONOSUPPORT;
  403. goto error;
  404. }
  405. if (bus->hw_info.num_mac > QTNF_MAX_MAC) {
  406. pr_err("no support for number of MACs=%u\n",
  407. bus->hw_info.num_mac);
  408. ret = -ERANGE;
  409. goto error;
  410. }
  411. for (i = 0; i < bus->hw_info.num_mac; i++) {
  412. ret = qtnf_core_mac_attach(bus, i);
  413. if (ret) {
  414. pr_err("MAC%u: attach failed: %d\n", i, ret);
  415. goto error;
  416. }
  417. }
  418. return 0;
  419. error:
  420. qtnf_core_detach(bus);
  421. return ret;
  422. }
  423. EXPORT_SYMBOL_GPL(qtnf_core_attach);
  424. void qtnf_core_detach(struct qtnf_bus *bus)
  425. {
  426. unsigned int macid;
  427. qtnf_bus_data_rx_stop(bus);
  428. for (macid = 0; macid < QTNF_MAX_MAC; macid++)
  429. qtnf_core_mac_detach(bus, macid);
  430. if (bus->fw_state == QTNF_FW_STATE_ACTIVE)
  431. qtnf_cmd_send_deinit_fw(bus);
  432. bus->fw_state = QTNF_FW_STATE_DEAD;
  433. if (bus->workqueue) {
  434. flush_workqueue(bus->workqueue);
  435. destroy_workqueue(bus->workqueue);
  436. }
  437. kfree(bus->hw_info.rd);
  438. bus->hw_info.rd = NULL;
  439. qtnf_trans_free(bus);
  440. }
  441. EXPORT_SYMBOL_GPL(qtnf_core_detach);
  442. static inline int qtnf_is_frame_meta_magic_valid(struct qtnf_frame_meta_info *m)
  443. {
  444. return m->magic_s == 0xAB && m->magic_e == 0xBA;
  445. }
  446. struct net_device *qtnf_classify_skb(struct qtnf_bus *bus, struct sk_buff *skb)
  447. {
  448. struct qtnf_frame_meta_info *meta;
  449. struct net_device *ndev = NULL;
  450. struct qtnf_wmac *mac;
  451. struct qtnf_vif *vif;
  452. meta = (struct qtnf_frame_meta_info *)
  453. (skb_tail_pointer(skb) - sizeof(*meta));
  454. if (unlikely(!qtnf_is_frame_meta_magic_valid(meta))) {
  455. pr_err_ratelimited("invalid magic 0x%x:0x%x\n",
  456. meta->magic_s, meta->magic_e);
  457. goto out;
  458. }
  459. if (unlikely(meta->macid >= QTNF_MAX_MAC)) {
  460. pr_err_ratelimited("invalid mac(%u)\n", meta->macid);
  461. goto out;
  462. }
  463. if (unlikely(meta->ifidx >= QTNF_MAX_INTF)) {
  464. pr_err_ratelimited("invalid vif(%u)\n", meta->ifidx);
  465. goto out;
  466. }
  467. mac = bus->mac[meta->macid];
  468. if (unlikely(!mac)) {
  469. pr_err_ratelimited("mac(%d) does not exist\n", meta->macid);
  470. goto out;
  471. }
  472. vif = &mac->iflist[meta->ifidx];
  473. if (unlikely(vif->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED)) {
  474. pr_err_ratelimited("vif(%u) does not exists\n", meta->ifidx);
  475. goto out;
  476. }
  477. ndev = vif->netdev;
  478. if (unlikely(!ndev)) {
  479. pr_err_ratelimited("netdev for wlan%u.%u does not exists\n",
  480. meta->macid, meta->ifidx);
  481. goto out;
  482. }
  483. __skb_trim(skb, skb->len - sizeof(*meta));
  484. out:
  485. return ndev;
  486. }
  487. EXPORT_SYMBOL_GPL(qtnf_classify_skb);
  488. void qtnf_wake_all_queues(struct net_device *ndev)
  489. {
  490. struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
  491. struct qtnf_wmac *mac;
  492. struct qtnf_bus *bus;
  493. int macid;
  494. int i;
  495. if (unlikely(!vif || !vif->mac || !vif->mac->bus))
  496. return;
  497. bus = vif->mac->bus;
  498. for (macid = 0; macid < QTNF_MAX_MAC; macid++) {
  499. if (!(bus->hw_info.mac_bitmap & BIT(macid)))
  500. continue;
  501. mac = bus->mac[macid];
  502. for (i = 0; i < QTNF_MAX_INTF; i++) {
  503. vif = &mac->iflist[i];
  504. if (vif->netdev && netif_queue_stopped(vif->netdev))
  505. netif_tx_wake_all_queues(vif->netdev);
  506. }
  507. }
  508. }
  509. EXPORT_SYMBOL_GPL(qtnf_wake_all_queues);
  510. MODULE_AUTHOR("Quantenna Communications");
  511. MODULE_DESCRIPTION("Quantenna 802.11 wireless LAN FullMAC driver.");
  512. MODULE_LICENSE("GPL");