]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/net/wireless/quantenna/qtnfmac/core.c
qtnfmac: introduce new FullMAC driver for Quantenna chipsets
[mirror_ubuntu-bionic-kernel.git] / drivers / net / wireless / quantenna / qtnfmac / core.c
CommitLineData
98f44cb0
IM
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
17#include <linux/kernel.h>
18#include <linux/module.h>
19#include <linux/if_ether.h>
20
21#include "core.h"
22#include "bus.h"
23#include "trans.h"
24#include "commands.h"
25#include "cfg80211.h"
26#include "event.h"
27#include "util.h"
28
29#define QTNF_DMP_MAX_LEN 48
30#define QTNF_PRIMARY_VIF_IDX 0
31
32struct qtnf_frame_meta_info {
33 u8 magic_s;
34 u8 ifidx;
35 u8 macid;
36 u8 magic_e;
37} __packed;
38
39struct qtnf_wmac *qtnf_core_get_mac(const struct qtnf_bus *bus, u8 macid)
40{
41 struct qtnf_wmac *mac = NULL;
42
43 if (unlikely(macid >= QTNF_MAX_MAC)) {
44 pr_err("invalid MAC index %u\n", macid);
45 return NULL;
46 }
47
48 mac = bus->mac[macid];
49
50 if (unlikely(!mac)) {
51 pr_err("MAC%u: not initialized\n", macid);
52 return NULL;
53 }
54
55 return mac;
56}
57
58/* Netdev handler for open.
59 */
60static int qtnf_netdev_open(struct net_device *ndev)
61{
62 netif_carrier_off(ndev);
63 qtnf_netdev_updown(ndev, 1);
64 return 0;
65}
66
67/* Netdev handler for close.
68 */
69static int qtnf_netdev_close(struct net_device *ndev)
70{
71 netif_carrier_off(ndev);
72 qtnf_virtual_intf_cleanup(ndev);
73 qtnf_netdev_updown(ndev, 0);
74 return 0;
75}
76
77/* Netdev handler for data transmission.
78 */
79static int
80qtnf_netdev_hard_start_xmit(struct sk_buff *skb, struct net_device *ndev)
81{
82 struct qtnf_vif *vif;
83 struct qtnf_wmac *mac;
84
85 vif = qtnf_netdev_get_priv(ndev);
86
87 if (unlikely(skb->dev != ndev)) {
88 pr_err_ratelimited("invalid skb->dev");
89 dev_kfree_skb_any(skb);
90 return 0;
91 }
92
93 if (unlikely(vif->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED)) {
94 pr_err_ratelimited("%s: VIF not initialized\n", ndev->name);
95 dev_kfree_skb_any(skb);
96 return 0;
97 }
98
99 mac = vif->mac;
100 if (unlikely(!mac)) {
101 pr_err_ratelimited("%s: NULL mac pointer", ndev->name);
102 dev_kfree_skb_any(skb);
103 return 0;
104 }
105
106 if (!skb->len || (skb->len > ETH_FRAME_LEN)) {
107 pr_err_ratelimited("%s: invalid skb len %d\n", ndev->name,
108 skb->len);
109 dev_kfree_skb_any(skb);
110 ndev->stats.tx_dropped++;
111 return 0;
112 }
113
114 /* tx path is enabled: reset vif timeout */
115 vif->cons_tx_timeout_cnt = 0;
116
117 return qtnf_bus_data_tx(mac->bus, skb);
118}
119
120/* Netdev handler for getting stats.
121 */
122static struct net_device_stats *qtnf_netdev_get_stats(struct net_device *dev)
123{
124 return &dev->stats;
125}
126
127/* Netdev handler for transmission timeout.
128 */
129static void qtnf_netdev_tx_timeout(struct net_device *ndev)
130{
131 struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
132 struct qtnf_wmac *mac;
133 struct qtnf_bus *bus;
134
135 if (unlikely(!vif || !vif->mac || !vif->mac->bus))
136 return;
137
138 mac = vif->mac;
139 bus = mac->bus;
140
141 pr_warn("VIF%u.%u: Tx timeout- %lu\n", mac->macid, vif->vifid, jiffies);
142
143 qtnf_bus_data_tx_timeout(bus, ndev);
144 ndev->stats.tx_errors++;
145
146 if (++vif->cons_tx_timeout_cnt > QTNF_TX_TIMEOUT_TRSHLD) {
147 pr_err("Tx timeout threshold exceeded !\n");
148 pr_err("schedule interface %s reset !\n", netdev_name(ndev));
149 queue_work(bus->workqueue, &vif->reset_work);
150 }
151}
152
153/* Network device ops handlers */
154const struct net_device_ops qtnf_netdev_ops = {
155 .ndo_open = qtnf_netdev_open,
156 .ndo_stop = qtnf_netdev_close,
157 .ndo_start_xmit = qtnf_netdev_hard_start_xmit,
158 .ndo_tx_timeout = qtnf_netdev_tx_timeout,
159 .ndo_get_stats = qtnf_netdev_get_stats,
160};
161
162static int qtnf_mac_init_single_band(struct wiphy *wiphy,
163 struct qtnf_wmac *mac,
164 enum nl80211_band band)
165{
166 int ret;
167
168 wiphy->bands[band] = kzalloc(sizeof(*wiphy->bands[band]), GFP_KERNEL);
169 if (!wiphy->bands[band])
170 return -ENOMEM;
171
172 wiphy->bands[band]->band = band;
173
174 ret = qtnf_cmd_get_mac_chan_info(mac, wiphy->bands[band]);
175 if (ret) {
176 pr_err("MAC%u: band %u: failed to get chans info: %d\n",
177 mac->macid, band, ret);
178 return ret;
179 }
180
181 qtnf_band_init_rates(wiphy->bands[band]);
182 qtnf_band_setup_htvht_caps(&mac->macinfo, wiphy->bands[band]);
183
184 return 0;
185}
186
187static int qtnf_mac_init_bands(struct qtnf_wmac *mac)
188{
189 struct wiphy *wiphy = priv_to_wiphy(mac);
190 int ret = 0;
191
192 if (mac->macinfo.bands_cap & QLINK_BAND_2GHZ) {
193 ret = qtnf_mac_init_single_band(wiphy, mac, NL80211_BAND_2GHZ);
194 if (ret)
195 goto out;
196 }
197
198 if (mac->macinfo.bands_cap & QLINK_BAND_5GHZ) {
199 ret = qtnf_mac_init_single_band(wiphy, mac, NL80211_BAND_5GHZ);
200 if (ret)
201 goto out;
202 }
203
204 if (mac->macinfo.bands_cap & QLINK_BAND_60GHZ)
205 ret = qtnf_mac_init_single_band(wiphy, mac, NL80211_BAND_60GHZ);
206
207out:
208 return ret;
209}
210
211struct qtnf_vif *qtnf_mac_get_free_vif(struct qtnf_wmac *mac)
212{
213 struct qtnf_vif *vif;
214 int i;
215
216 for (i = 0; i < QTNF_MAX_INTF; i++) {
217 vif = &mac->iflist[i];
218 if (vif->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED)
219 return vif;
220 }
221
222 return NULL;
223}
224
225struct qtnf_vif *qtnf_mac_get_base_vif(struct qtnf_wmac *mac)
226{
227 struct qtnf_vif *vif;
228
229 vif = &mac->iflist[QTNF_PRIMARY_VIF_IDX];
230
231 if (vif->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED)
232 return NULL;
233
234 return vif;
235}
236
237static void qtnf_vif_reset_handler(struct work_struct *work)
238{
239 struct qtnf_vif *vif = container_of(work, struct qtnf_vif, reset_work);
240
241 rtnl_lock();
242
243 if (vif->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED) {
244 rtnl_unlock();
245 return;
246 }
247
248 /* stop tx completely */
249 netif_tx_stop_all_queues(vif->netdev);
250 if (netif_carrier_ok(vif->netdev))
251 netif_carrier_off(vif->netdev);
252
253 qtnf_cfg80211_vif_reset(vif);
254
255 rtnl_unlock();
256}
257
258static void qtnf_mac_init_primary_intf(struct qtnf_wmac *mac)
259{
260 struct qtnf_vif *vif = &mac->iflist[QTNF_PRIMARY_VIF_IDX];
261
262 vif->wdev.iftype = NL80211_IFTYPE_AP;
263 vif->bss_priority = QTNF_DEF_BSS_PRIORITY;
264 vif->wdev.wiphy = priv_to_wiphy(mac);
265 INIT_WORK(&vif->reset_work, qtnf_vif_reset_handler);
266 vif->cons_tx_timeout_cnt = 0;
267}
268
269static struct qtnf_wmac *qtnf_core_mac_alloc(struct qtnf_bus *bus,
270 unsigned int macid)
271{
272 struct wiphy *wiphy;
273 struct qtnf_wmac *mac;
274 unsigned int i;
275
276 wiphy = qtnf_wiphy_allocate(bus);
277 if (!wiphy)
278 return ERR_PTR(-ENOMEM);
279
280 mac = wiphy_priv(wiphy);
281
282 mac->macid = macid;
283 mac->bus = bus;
284
285 for (i = 0; i < QTNF_MAX_INTF; i++) {
286 memset(&mac->iflist[i], 0, sizeof(struct qtnf_vif));
287 mac->iflist[i].wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
288 mac->iflist[i].mac = mac;
289 mac->iflist[i].vifid = i;
290 qtnf_sta_list_init(&mac->iflist[i].sta_list);
291 }
292
293 qtnf_mac_init_primary_intf(mac);
294 bus->mac[macid] = mac;
295
296 return mac;
297}
298
299int qtnf_core_net_attach(struct qtnf_wmac *mac, struct qtnf_vif *vif,
300 const char *name, unsigned char name_assign_type,
301 enum nl80211_iftype iftype)
302{
303 struct wiphy *wiphy = priv_to_wiphy(mac);
304 struct net_device *dev;
305 void *qdev_vif;
306 int ret;
307
308 dev = alloc_netdev_mqs(sizeof(struct qtnf_vif *), name,
309 name_assign_type, ether_setup, 1, 1);
310 if (!dev) {
311 memset(&vif->wdev, 0, sizeof(vif->wdev));
312 vif->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
313 return -ENOMEM;
314 }
315
316 vif->netdev = dev;
317
318 dev->netdev_ops = &qtnf_netdev_ops;
319 dev->destructor = free_netdev;
320 dev_net_set(dev, wiphy_net(wiphy));
321 dev->ieee80211_ptr = &vif->wdev;
322 dev->ieee80211_ptr->iftype = iftype;
323 ether_addr_copy(dev->dev_addr, vif->mac_addr);
324 SET_NETDEV_DEV(dev, wiphy_dev(wiphy));
325 dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
326 dev->watchdog_timeo = QTNF_DEF_WDOG_TIMEOUT;
327 dev->tx_queue_len = 100;
328
329 qdev_vif = netdev_priv(dev);
330 *((void **)qdev_vif) = vif;
331
332 SET_NETDEV_DEV(dev, mac->bus->dev);
333
334 ret = register_netdevice(dev);
335 if (ret) {
336 free_netdev(dev);
337 vif->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
338 }
339
340 return ret;
341}
342
343static void qtnf_core_mac_detach(struct qtnf_bus *bus, unsigned int macid)
344{
345 struct qtnf_wmac *mac;
346 struct wiphy *wiphy;
347 struct qtnf_vif *vif;
348 unsigned int i;
349 enum nl80211_band band;
350
351 mac = bus->mac[macid];
352
353 if (!mac)
354 return;
355
356 wiphy = priv_to_wiphy(mac);
357
358 for (i = 0; i < QTNF_MAX_INTF; i++) {
359 vif = &mac->iflist[i];
360 rtnl_lock();
361 if (vif->netdev &&
362 vif->wdev.iftype != NL80211_IFTYPE_UNSPECIFIED) {
363 qtnf_virtual_intf_cleanup(vif->netdev);
364 qtnf_del_virtual_intf(wiphy, &vif->wdev);
365 }
366 rtnl_unlock();
367 qtnf_sta_list_free(&vif->sta_list);
368 }
369
370 if (mac->wiphy_registered)
371 wiphy_unregister(wiphy);
372
373 for (band = NL80211_BAND_2GHZ; band < NUM_NL80211_BANDS; ++band) {
374 if (!wiphy->bands[band])
375 continue;
376
377 kfree(wiphy->bands[band]->channels);
378 wiphy->bands[band]->n_channels = 0;
379
380 kfree(wiphy->bands[band]);
381 wiphy->bands[band] = NULL;
382 }
383
384 kfree(mac->macinfo.limits);
385 kfree(wiphy->iface_combinations);
386 wiphy_free(wiphy);
387 bus->mac[macid] = NULL;
388}
389
390static int qtnf_core_mac_attach(struct qtnf_bus *bus, unsigned int macid)
391{
392 struct qtnf_wmac *mac;
393 struct qtnf_vif *vif;
394 int ret;
395
396 if (!(bus->hw_info.mac_bitmap & BIT(macid))) {
397 pr_info("MAC%u is not active in FW\n", macid);
398 return 0;
399 }
400
401 mac = qtnf_core_mac_alloc(bus, macid);
402 if (IS_ERR(mac)) {
403 pr_err("MAC%u allocation failed\n", macid);
404 return PTR_ERR(mac);
405 }
406
407 ret = qtnf_cmd_get_mac_info(mac);
408 if (ret) {
409 pr_err("MAC%u: failed to get info\n", macid);
410 goto error;
411 }
412
413 vif = qtnf_mac_get_base_vif(mac);
414 if (!vif) {
415 pr_err("MAC%u: primary VIF is not ready\n", macid);
416 ret = -EFAULT;
417 goto error;
418 }
419
420 ret = qtnf_cmd_send_add_intf(vif, NL80211_IFTYPE_AP, vif->mac_addr);
421 if (ret) {
422 pr_err("MAC%u: failed to add VIF\n", macid);
423 goto error;
424 }
425
426 ret = qtnf_cmd_send_get_phy_params(mac);
427 if (ret) {
428 pr_err("MAC%u: failed to get PHY settings\n", macid);
429 goto error;
430 }
431
432 ret = qtnf_mac_init_bands(mac);
433 if (ret) {
434 pr_err("MAC%u: failed to init bands\n", macid);
435 goto error;
436 }
437
438 ret = qtnf_wiphy_register(&bus->hw_info, mac);
439 if (ret) {
440 pr_err("MAC%u: wiphy registration failed\n", macid);
441 goto error;
442 }
443
444 mac->wiphy_registered = 1;
445
446 rtnl_lock();
447
448 ret = qtnf_core_net_attach(mac, vif, "wlan%d", NET_NAME_ENUM,
449 NL80211_IFTYPE_AP);
450 rtnl_unlock();
451
452 if (ret) {
453 pr_err("MAC%u: failed to attach netdev\n", macid);
454 vif->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
455 vif->netdev = NULL;
456 goto error;
457 }
458
459 pr_debug("MAC%u initialized\n", macid);
460
461 return 0;
462
463error:
464 qtnf_core_mac_detach(bus, macid);
465 return ret;
466}
467
468int qtnf_core_attach(struct qtnf_bus *bus)
469{
470 unsigned int i;
471 int ret;
472
473 qtnf_trans_init(bus);
474
475 bus->fw_state = QTNF_FW_STATE_BOOT_DONE;
476 qtnf_bus_data_rx_start(bus);
477
478 bus->workqueue = alloc_ordered_workqueue("QTNF_BUS", 0);
479 if (!bus->workqueue) {
480 pr_err("failed to alloc main workqueue\n");
481 ret = -ENOMEM;
482 goto error;
483 }
484
485 INIT_WORK(&bus->event_work, qtnf_event_work_handler);
486
487 ret = qtnf_cmd_send_init_fw(bus);
488 if (ret) {
489 pr_err("failed to init FW: %d\n", ret);
490 goto error;
491 }
492
493 bus->fw_state = QTNF_FW_STATE_ACTIVE;
494
495 ret = qtnf_cmd_get_hw_info(bus);
496 if (ret) {
497 pr_err("failed to get HW info: %d\n", ret);
498 goto error;
499 }
500
501 if (bus->hw_info.ql_proto_ver != QLINK_PROTO_VER) {
502 pr_err("qlink version mismatch %u != %u\n",
503 QLINK_PROTO_VER, bus->hw_info.ql_proto_ver);
504 ret = -EPROTONOSUPPORT;
505 goto error;
506 }
507
508 if (bus->hw_info.num_mac > QTNF_MAX_MAC) {
509 pr_err("no support for number of MACs=%u\n",
510 bus->hw_info.num_mac);
511 ret = -ERANGE;
512 goto error;
513 }
514
515 for (i = 0; i < bus->hw_info.num_mac; i++) {
516 ret = qtnf_core_mac_attach(bus, i);
517
518 if (ret) {
519 pr_err("MAC%u: attach failed: %d\n", i, ret);
520 goto error;
521 }
522 }
523
524 return 0;
525
526error:
527 qtnf_core_detach(bus);
528
529 return ret;
530}
531EXPORT_SYMBOL_GPL(qtnf_core_attach);
532
533void qtnf_core_detach(struct qtnf_bus *bus)
534{
535 unsigned int macid;
536
537 qtnf_bus_data_rx_stop(bus);
538
539 for (macid = 0; macid < QTNF_MAX_MAC; macid++)
540 qtnf_core_mac_detach(bus, macid);
541
542 if (bus->fw_state == QTNF_FW_STATE_ACTIVE)
543 qtnf_cmd_send_deinit_fw(bus);
544
545 bus->fw_state = QTNF_FW_STATE_DEAD;
546
547 if (bus->workqueue) {
548 flush_workqueue(bus->workqueue);
549 destroy_workqueue(bus->workqueue);
550 }
551
552 qtnf_trans_free(bus);
553}
554EXPORT_SYMBOL_GPL(qtnf_core_detach);
555
556static inline int qtnf_is_frame_meta_magic_valid(struct qtnf_frame_meta_info *m)
557{
558 return m->magic_s == 0xAB && m->magic_e == 0xBA;
559}
560
561struct net_device *qtnf_classify_skb(struct qtnf_bus *bus, struct sk_buff *skb)
562{
563 struct qtnf_frame_meta_info *meta;
564 struct net_device *ndev = NULL;
565 struct qtnf_wmac *mac;
566 struct qtnf_vif *vif;
567
568 meta = (struct qtnf_frame_meta_info *)
569 (skb_tail_pointer(skb) - sizeof(*meta));
570
571 if (unlikely(!qtnf_is_frame_meta_magic_valid(meta))) {
572 pr_err_ratelimited("invalid magic 0x%x:0x%x\n",
573 meta->magic_s, meta->magic_e);
574 goto out;
575 }
576
577 if (unlikely(meta->macid >= QTNF_MAX_MAC)) {
578 pr_err_ratelimited("invalid mac(%u)\n", meta->macid);
579 goto out;
580 }
581
582 if (unlikely(meta->ifidx >= QTNF_MAX_INTF)) {
583 pr_err_ratelimited("invalid vif(%u)\n", meta->ifidx);
584 goto out;
585 }
586
587 mac = bus->mac[meta->macid];
588
589 if (unlikely(!mac)) {
590 pr_err_ratelimited("mac(%d) does not exist\n", meta->macid);
591 goto out;
592 }
593
594 vif = &mac->iflist[meta->ifidx];
595
596 if (unlikely(vif->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED)) {
597 pr_err_ratelimited("vif(%u) does not exists\n", meta->ifidx);
598 goto out;
599 }
600
601 ndev = vif->netdev;
602
603 if (unlikely(!ndev)) {
604 pr_err_ratelimited("netdev for wlan%u.%u does not exists\n",
605 meta->macid, meta->ifidx);
606 goto out;
607 }
608
609 __skb_trim(skb, skb->len - sizeof(*meta));
610
611out:
612 return ndev;
613}
614EXPORT_SYMBOL_GPL(qtnf_classify_skb);
615
616MODULE_AUTHOR("Quantenna Communications");
617MODULE_DESCRIPTION("Quantenna 802.11 wireless LAN FullMAC driver.");
618MODULE_LICENSE("GPL");