core.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  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_get_mac_chan_info(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. qtnf_band_setup_htvht_caps(&mac->macinfo, wiphy->bands[band]);
  151. return 0;
  152. }
  153. static int qtnf_mac_init_bands(struct qtnf_wmac *mac)
  154. {
  155. struct wiphy *wiphy = priv_to_wiphy(mac);
  156. int ret = 0;
  157. if (mac->macinfo.bands_cap & QLINK_BAND_2GHZ) {
  158. ret = qtnf_mac_init_single_band(wiphy, mac, NL80211_BAND_2GHZ);
  159. if (ret)
  160. goto out;
  161. }
  162. if (mac->macinfo.bands_cap & QLINK_BAND_5GHZ) {
  163. ret = qtnf_mac_init_single_band(wiphy, mac, NL80211_BAND_5GHZ);
  164. if (ret)
  165. goto out;
  166. }
  167. if (mac->macinfo.bands_cap & QLINK_BAND_60GHZ)
  168. ret = qtnf_mac_init_single_band(wiphy, mac, NL80211_BAND_60GHZ);
  169. out:
  170. return ret;
  171. }
  172. struct qtnf_vif *qtnf_mac_get_free_vif(struct qtnf_wmac *mac)
  173. {
  174. struct qtnf_vif *vif;
  175. int i;
  176. for (i = 0; i < QTNF_MAX_INTF; i++) {
  177. vif = &mac->iflist[i];
  178. if (vif->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED)
  179. return vif;
  180. }
  181. return NULL;
  182. }
  183. struct qtnf_vif *qtnf_mac_get_base_vif(struct qtnf_wmac *mac)
  184. {
  185. struct qtnf_vif *vif;
  186. vif = &mac->iflist[QTNF_PRIMARY_VIF_IDX];
  187. if (vif->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED)
  188. return NULL;
  189. return vif;
  190. }
  191. static void qtnf_vif_reset_handler(struct work_struct *work)
  192. {
  193. struct qtnf_vif *vif = container_of(work, struct qtnf_vif, reset_work);
  194. rtnl_lock();
  195. if (vif->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED) {
  196. rtnl_unlock();
  197. return;
  198. }
  199. /* stop tx completely */
  200. netif_tx_stop_all_queues(vif->netdev);
  201. if (netif_carrier_ok(vif->netdev))
  202. netif_carrier_off(vif->netdev);
  203. qtnf_cfg80211_vif_reset(vif);
  204. rtnl_unlock();
  205. }
  206. static void qtnf_mac_init_primary_intf(struct qtnf_wmac *mac)
  207. {
  208. struct qtnf_vif *vif = &mac->iflist[QTNF_PRIMARY_VIF_IDX];
  209. vif->wdev.iftype = NL80211_IFTYPE_AP;
  210. vif->bss_priority = QTNF_DEF_BSS_PRIORITY;
  211. vif->wdev.wiphy = priv_to_wiphy(mac);
  212. INIT_WORK(&vif->reset_work, qtnf_vif_reset_handler);
  213. vif->cons_tx_timeout_cnt = 0;
  214. }
  215. static struct qtnf_wmac *qtnf_core_mac_alloc(struct qtnf_bus *bus,
  216. unsigned int macid)
  217. {
  218. struct wiphy *wiphy;
  219. struct qtnf_wmac *mac;
  220. unsigned int i;
  221. wiphy = qtnf_wiphy_allocate(bus);
  222. if (!wiphy)
  223. return ERR_PTR(-ENOMEM);
  224. mac = wiphy_priv(wiphy);
  225. mac->macid = macid;
  226. mac->bus = bus;
  227. for (i = 0; i < QTNF_MAX_INTF; i++) {
  228. memset(&mac->iflist[i], 0, sizeof(struct qtnf_vif));
  229. mac->iflist[i].wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
  230. mac->iflist[i].mac = mac;
  231. mac->iflist[i].vifid = i;
  232. qtnf_sta_list_init(&mac->iflist[i].sta_list);
  233. mutex_init(&mac->mac_lock);
  234. init_timer(&mac->scan_timeout);
  235. }
  236. qtnf_mac_init_primary_intf(mac);
  237. bus->mac[macid] = mac;
  238. return mac;
  239. }
  240. int qtnf_core_net_attach(struct qtnf_wmac *mac, struct qtnf_vif *vif,
  241. const char *name, unsigned char name_assign_type,
  242. enum nl80211_iftype iftype)
  243. {
  244. struct wiphy *wiphy = priv_to_wiphy(mac);
  245. struct net_device *dev;
  246. void *qdev_vif;
  247. int ret;
  248. dev = alloc_netdev_mqs(sizeof(struct qtnf_vif *), name,
  249. name_assign_type, ether_setup, 1, 1);
  250. if (!dev) {
  251. memset(&vif->wdev, 0, sizeof(vif->wdev));
  252. vif->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
  253. return -ENOMEM;
  254. }
  255. vif->netdev = dev;
  256. dev->netdev_ops = &qtnf_netdev_ops;
  257. dev->needs_free_netdev = true;
  258. dev_net_set(dev, wiphy_net(wiphy));
  259. dev->ieee80211_ptr = &vif->wdev;
  260. dev->ieee80211_ptr->iftype = iftype;
  261. ether_addr_copy(dev->dev_addr, vif->mac_addr);
  262. SET_NETDEV_DEV(dev, wiphy_dev(wiphy));
  263. dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
  264. dev->watchdog_timeo = QTNF_DEF_WDOG_TIMEOUT;
  265. dev->tx_queue_len = 100;
  266. qdev_vif = netdev_priv(dev);
  267. *((void **)qdev_vif) = vif;
  268. SET_NETDEV_DEV(dev, mac->bus->dev);
  269. ret = register_netdevice(dev);
  270. if (ret) {
  271. free_netdev(dev);
  272. vif->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
  273. }
  274. return ret;
  275. }
  276. static void qtnf_core_mac_detach(struct qtnf_bus *bus, unsigned int macid)
  277. {
  278. struct qtnf_wmac *mac;
  279. struct wiphy *wiphy;
  280. struct qtnf_vif *vif;
  281. unsigned int i;
  282. enum nl80211_band band;
  283. mac = bus->mac[macid];
  284. if (!mac)
  285. return;
  286. wiphy = priv_to_wiphy(mac);
  287. for (i = 0; i < QTNF_MAX_INTF; i++) {
  288. vif = &mac->iflist[i];
  289. rtnl_lock();
  290. if (vif->netdev &&
  291. vif->wdev.iftype != NL80211_IFTYPE_UNSPECIFIED) {
  292. qtnf_virtual_intf_cleanup(vif->netdev);
  293. qtnf_del_virtual_intf(wiphy, &vif->wdev);
  294. }
  295. rtnl_unlock();
  296. qtnf_sta_list_free(&vif->sta_list);
  297. }
  298. if (mac->wiphy_registered)
  299. wiphy_unregister(wiphy);
  300. for (band = NL80211_BAND_2GHZ; band < NUM_NL80211_BANDS; ++band) {
  301. if (!wiphy->bands[band])
  302. continue;
  303. kfree(wiphy->bands[band]->channels);
  304. wiphy->bands[band]->n_channels = 0;
  305. kfree(wiphy->bands[band]);
  306. wiphy->bands[band] = NULL;
  307. }
  308. kfree(mac->macinfo.limits);
  309. kfree(wiphy->iface_combinations);
  310. wiphy_free(wiphy);
  311. bus->mac[macid] = NULL;
  312. }
  313. static int qtnf_core_mac_attach(struct qtnf_bus *bus, unsigned int macid)
  314. {
  315. struct qtnf_wmac *mac;
  316. struct qtnf_vif *vif;
  317. int ret;
  318. if (!(bus->hw_info.mac_bitmap & BIT(macid))) {
  319. pr_info("MAC%u is not active in FW\n", macid);
  320. return 0;
  321. }
  322. mac = qtnf_core_mac_alloc(bus, macid);
  323. if (IS_ERR(mac)) {
  324. pr_err("MAC%u allocation failed\n", macid);
  325. return PTR_ERR(mac);
  326. }
  327. ret = qtnf_cmd_get_mac_info(mac);
  328. if (ret) {
  329. pr_err("MAC%u: failed to get info\n", macid);
  330. goto error;
  331. }
  332. vif = qtnf_mac_get_base_vif(mac);
  333. if (!vif) {
  334. pr_err("MAC%u: primary VIF is not ready\n", macid);
  335. ret = -EFAULT;
  336. goto error;
  337. }
  338. ret = qtnf_cmd_send_add_intf(vif, NL80211_IFTYPE_AP, vif->mac_addr);
  339. if (ret) {
  340. pr_err("MAC%u: failed to add VIF\n", macid);
  341. goto error;
  342. }
  343. ret = qtnf_cmd_send_get_phy_params(mac);
  344. if (ret) {
  345. pr_err("MAC%u: failed to get PHY settings\n", macid);
  346. goto error;
  347. }
  348. ret = qtnf_mac_init_bands(mac);
  349. if (ret) {
  350. pr_err("MAC%u: failed to init bands\n", macid);
  351. goto error;
  352. }
  353. ret = qtnf_wiphy_register(&bus->hw_info, mac);
  354. if (ret) {
  355. pr_err("MAC%u: wiphy registration failed\n", macid);
  356. goto error;
  357. }
  358. mac->wiphy_registered = 1;
  359. rtnl_lock();
  360. ret = qtnf_core_net_attach(mac, vif, "wlan%d", NET_NAME_ENUM,
  361. NL80211_IFTYPE_AP);
  362. rtnl_unlock();
  363. if (ret) {
  364. pr_err("MAC%u: failed to attach netdev\n", macid);
  365. vif->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
  366. vif->netdev = NULL;
  367. goto error;
  368. }
  369. pr_debug("MAC%u initialized\n", macid);
  370. return 0;
  371. error:
  372. qtnf_core_mac_detach(bus, macid);
  373. return ret;
  374. }
  375. int qtnf_core_attach(struct qtnf_bus *bus)
  376. {
  377. unsigned int i;
  378. int ret;
  379. qtnf_trans_init(bus);
  380. bus->fw_state = QTNF_FW_STATE_BOOT_DONE;
  381. qtnf_bus_data_rx_start(bus);
  382. bus->workqueue = alloc_ordered_workqueue("QTNF_BUS", 0);
  383. if (!bus->workqueue) {
  384. pr_err("failed to alloc main workqueue\n");
  385. ret = -ENOMEM;
  386. goto error;
  387. }
  388. INIT_WORK(&bus->event_work, qtnf_event_work_handler);
  389. ret = qtnf_cmd_send_init_fw(bus);
  390. if (ret) {
  391. pr_err("failed to init FW: %d\n", ret);
  392. goto error;
  393. }
  394. bus->fw_state = QTNF_FW_STATE_ACTIVE;
  395. ret = qtnf_cmd_get_hw_info(bus);
  396. if (ret) {
  397. pr_err("failed to get HW info: %d\n", ret);
  398. goto error;
  399. }
  400. if (bus->hw_info.ql_proto_ver != QLINK_PROTO_VER) {
  401. pr_err("qlink version mismatch %u != %u\n",
  402. QLINK_PROTO_VER, bus->hw_info.ql_proto_ver);
  403. ret = -EPROTONOSUPPORT;
  404. goto error;
  405. }
  406. if (bus->hw_info.num_mac > QTNF_MAX_MAC) {
  407. pr_err("no support for number of MACs=%u\n",
  408. bus->hw_info.num_mac);
  409. ret = -ERANGE;
  410. goto error;
  411. }
  412. for (i = 0; i < bus->hw_info.num_mac; i++) {
  413. ret = qtnf_core_mac_attach(bus, i);
  414. if (ret) {
  415. pr_err("MAC%u: attach failed: %d\n", i, ret);
  416. goto error;
  417. }
  418. }
  419. return 0;
  420. error:
  421. qtnf_core_detach(bus);
  422. return ret;
  423. }
  424. EXPORT_SYMBOL_GPL(qtnf_core_attach);
  425. void qtnf_core_detach(struct qtnf_bus *bus)
  426. {
  427. unsigned int macid;
  428. qtnf_bus_data_rx_stop(bus);
  429. for (macid = 0; macid < QTNF_MAX_MAC; macid++)
  430. qtnf_core_mac_detach(bus, macid);
  431. if (bus->fw_state == QTNF_FW_STATE_ACTIVE)
  432. qtnf_cmd_send_deinit_fw(bus);
  433. bus->fw_state = QTNF_FW_STATE_DEAD;
  434. if (bus->workqueue) {
  435. flush_workqueue(bus->workqueue);
  436. destroy_workqueue(bus->workqueue);
  437. }
  438. kfree(bus->hw_info.rd);
  439. bus->hw_info.rd = NULL;
  440. qtnf_trans_free(bus);
  441. }
  442. EXPORT_SYMBOL_GPL(qtnf_core_detach);
  443. static inline int qtnf_is_frame_meta_magic_valid(struct qtnf_frame_meta_info *m)
  444. {
  445. return m->magic_s == 0xAB && m->magic_e == 0xBA;
  446. }
  447. struct net_device *qtnf_classify_skb(struct qtnf_bus *bus, struct sk_buff *skb)
  448. {
  449. struct qtnf_frame_meta_info *meta;
  450. struct net_device *ndev = NULL;
  451. struct qtnf_wmac *mac;
  452. struct qtnf_vif *vif;
  453. meta = (struct qtnf_frame_meta_info *)
  454. (skb_tail_pointer(skb) - sizeof(*meta));
  455. if (unlikely(!qtnf_is_frame_meta_magic_valid(meta))) {
  456. pr_err_ratelimited("invalid magic 0x%x:0x%x\n",
  457. meta->magic_s, meta->magic_e);
  458. goto out;
  459. }
  460. if (unlikely(meta->macid >= QTNF_MAX_MAC)) {
  461. pr_err_ratelimited("invalid mac(%u)\n", meta->macid);
  462. goto out;
  463. }
  464. if (unlikely(meta->ifidx >= QTNF_MAX_INTF)) {
  465. pr_err_ratelimited("invalid vif(%u)\n", meta->ifidx);
  466. goto out;
  467. }
  468. mac = bus->mac[meta->macid];
  469. if (unlikely(!mac)) {
  470. pr_err_ratelimited("mac(%d) does not exist\n", meta->macid);
  471. goto out;
  472. }
  473. vif = &mac->iflist[meta->ifidx];
  474. if (unlikely(vif->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED)) {
  475. pr_err_ratelimited("vif(%u) does not exists\n", meta->ifidx);
  476. goto out;
  477. }
  478. ndev = vif->netdev;
  479. if (unlikely(!ndev)) {
  480. pr_err_ratelimited("netdev for wlan%u.%u does not exists\n",
  481. meta->macid, meta->ifidx);
  482. goto out;
  483. }
  484. __skb_trim(skb, skb->len - sizeof(*meta));
  485. out:
  486. return ndev;
  487. }
  488. EXPORT_SYMBOL_GPL(qtnf_classify_skb);
  489. MODULE_AUTHOR("Quantenna Communications");
  490. MODULE_DESCRIPTION("Quantenna 802.11 wireless LAN FullMAC driver.");
  491. MODULE_LICENSE("GPL");