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 
32 struct qtnf_frame_meta_info {
33 	u8 magic_s;
34 	u8 ifidx;
35 	u8 macid;
36 	u8 magic_e;
37 } __packed;
38 
39 struct 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  */
60 static 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  */
69 static 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  */
79 static int
80 qtnf_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  */
122 static 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  */
129 static 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 */
154 const 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 
162 static 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 
187 static 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 
207 out:
208 	return ret;
209 }
210 
211 struct 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 
225 struct 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 
237 static 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 
258 static 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 
269 static 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 		mutex_init(&mac->mac_lock);
292 		init_timer(&mac->scan_timeout);
293 	}
294 
295 	qtnf_mac_init_primary_intf(mac);
296 	bus->mac[macid] = mac;
297 
298 	return mac;
299 }
300 
301 int qtnf_core_net_attach(struct qtnf_wmac *mac, struct qtnf_vif *vif,
302 			 const char *name, unsigned char name_assign_type,
303 			 enum nl80211_iftype iftype)
304 {
305 	struct wiphy *wiphy = priv_to_wiphy(mac);
306 	struct net_device *dev;
307 	void *qdev_vif;
308 	int ret;
309 
310 	dev = alloc_netdev_mqs(sizeof(struct qtnf_vif *), name,
311 			       name_assign_type, ether_setup, 1, 1);
312 	if (!dev) {
313 		memset(&vif->wdev, 0, sizeof(vif->wdev));
314 		vif->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
315 		return -ENOMEM;
316 	}
317 
318 	vif->netdev = dev;
319 
320 	dev->netdev_ops = &qtnf_netdev_ops;
321 	dev->needs_free_netdev = true;
322 	dev_net_set(dev, wiphy_net(wiphy));
323 	dev->ieee80211_ptr = &vif->wdev;
324 	dev->ieee80211_ptr->iftype = iftype;
325 	ether_addr_copy(dev->dev_addr, vif->mac_addr);
326 	SET_NETDEV_DEV(dev, wiphy_dev(wiphy));
327 	dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
328 	dev->watchdog_timeo = QTNF_DEF_WDOG_TIMEOUT;
329 	dev->tx_queue_len = 100;
330 
331 	qdev_vif = netdev_priv(dev);
332 	*((void **)qdev_vif) = vif;
333 
334 	SET_NETDEV_DEV(dev, mac->bus->dev);
335 
336 	ret = register_netdevice(dev);
337 	if (ret) {
338 		free_netdev(dev);
339 		vif->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
340 	}
341 
342 	return ret;
343 }
344 
345 static void qtnf_core_mac_detach(struct qtnf_bus *bus, unsigned int macid)
346 {
347 	struct qtnf_wmac *mac;
348 	struct wiphy *wiphy;
349 	struct qtnf_vif *vif;
350 	unsigned int i;
351 	enum nl80211_band band;
352 
353 	mac = bus->mac[macid];
354 
355 	if (!mac)
356 		return;
357 
358 	wiphy = priv_to_wiphy(mac);
359 
360 	for (i = 0; i < QTNF_MAX_INTF; i++) {
361 		vif = &mac->iflist[i];
362 		rtnl_lock();
363 		if (vif->netdev &&
364 		    vif->wdev.iftype != NL80211_IFTYPE_UNSPECIFIED) {
365 			qtnf_virtual_intf_cleanup(vif->netdev);
366 			qtnf_del_virtual_intf(wiphy, &vif->wdev);
367 		}
368 		rtnl_unlock();
369 		qtnf_sta_list_free(&vif->sta_list);
370 	}
371 
372 	if (mac->wiphy_registered)
373 		wiphy_unregister(wiphy);
374 
375 	for (band = NL80211_BAND_2GHZ; band < NUM_NL80211_BANDS; ++band) {
376 		if (!wiphy->bands[band])
377 			continue;
378 
379 		kfree(wiphy->bands[band]->channels);
380 		wiphy->bands[band]->n_channels = 0;
381 
382 		kfree(wiphy->bands[band]);
383 		wiphy->bands[band] = NULL;
384 	}
385 
386 	kfree(mac->macinfo.limits);
387 	kfree(wiphy->iface_combinations);
388 	wiphy_free(wiphy);
389 	bus->mac[macid] = NULL;
390 }
391 
392 static int qtnf_core_mac_attach(struct qtnf_bus *bus, unsigned int macid)
393 {
394 	struct qtnf_wmac *mac;
395 	struct qtnf_vif *vif;
396 	int ret;
397 
398 	if (!(bus->hw_info.mac_bitmap & BIT(macid))) {
399 		pr_info("MAC%u is not active in FW\n", macid);
400 		return 0;
401 	}
402 
403 	mac = qtnf_core_mac_alloc(bus, macid);
404 	if (IS_ERR(mac)) {
405 		pr_err("MAC%u allocation failed\n", macid);
406 		return PTR_ERR(mac);
407 	}
408 
409 	ret = qtnf_cmd_get_mac_info(mac);
410 	if (ret) {
411 		pr_err("MAC%u: failed to get info\n", macid);
412 		goto error;
413 	}
414 
415 	vif = qtnf_mac_get_base_vif(mac);
416 	if (!vif) {
417 		pr_err("MAC%u: primary VIF is not ready\n", macid);
418 		ret = -EFAULT;
419 		goto error;
420 	}
421 
422 	ret = qtnf_cmd_send_add_intf(vif, NL80211_IFTYPE_AP, vif->mac_addr);
423 	if (ret) {
424 		pr_err("MAC%u: failed to add VIF\n", macid);
425 		goto error;
426 	}
427 
428 	ret = qtnf_cmd_send_get_phy_params(mac);
429 	if (ret) {
430 		pr_err("MAC%u: failed to get PHY settings\n", macid);
431 		goto error;
432 	}
433 
434 	ret = qtnf_mac_init_bands(mac);
435 	if (ret) {
436 		pr_err("MAC%u: failed to init bands\n", macid);
437 		goto error;
438 	}
439 
440 	ret = qtnf_wiphy_register(&bus->hw_info, mac);
441 	if (ret) {
442 		pr_err("MAC%u: wiphy registration failed\n", macid);
443 		goto error;
444 	}
445 
446 	mac->wiphy_registered = 1;
447 
448 	rtnl_lock();
449 
450 	ret = qtnf_core_net_attach(mac, vif, "wlan%d", NET_NAME_ENUM,
451 				   NL80211_IFTYPE_AP);
452 	rtnl_unlock();
453 
454 	if (ret) {
455 		pr_err("MAC%u: failed to attach netdev\n", macid);
456 		vif->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
457 		vif->netdev = NULL;
458 		goto error;
459 	}
460 
461 	pr_debug("MAC%u initialized\n", macid);
462 
463 	return 0;
464 
465 error:
466 	qtnf_core_mac_detach(bus, macid);
467 	return ret;
468 }
469 
470 int qtnf_core_attach(struct qtnf_bus *bus)
471 {
472 	unsigned int i;
473 	int ret;
474 
475 	qtnf_trans_init(bus);
476 
477 	bus->fw_state = QTNF_FW_STATE_BOOT_DONE;
478 	qtnf_bus_data_rx_start(bus);
479 
480 	bus->workqueue = alloc_ordered_workqueue("QTNF_BUS", 0);
481 	if (!bus->workqueue) {
482 		pr_err("failed to alloc main workqueue\n");
483 		ret = -ENOMEM;
484 		goto error;
485 	}
486 
487 	INIT_WORK(&bus->event_work, qtnf_event_work_handler);
488 
489 	ret = qtnf_cmd_send_init_fw(bus);
490 	if (ret) {
491 		pr_err("failed to init FW: %d\n", ret);
492 		goto error;
493 	}
494 
495 	bus->fw_state = QTNF_FW_STATE_ACTIVE;
496 
497 	ret = qtnf_cmd_get_hw_info(bus);
498 	if (ret) {
499 		pr_err("failed to get HW info: %d\n", ret);
500 		goto error;
501 	}
502 
503 	if (bus->hw_info.ql_proto_ver != QLINK_PROTO_VER) {
504 		pr_err("qlink version mismatch %u != %u\n",
505 		       QLINK_PROTO_VER, bus->hw_info.ql_proto_ver);
506 		ret = -EPROTONOSUPPORT;
507 		goto error;
508 	}
509 
510 	if (bus->hw_info.num_mac > QTNF_MAX_MAC) {
511 		pr_err("no support for number of MACs=%u\n",
512 		       bus->hw_info.num_mac);
513 		ret = -ERANGE;
514 		goto error;
515 	}
516 
517 	for (i = 0; i < bus->hw_info.num_mac; i++) {
518 		ret = qtnf_core_mac_attach(bus, i);
519 
520 		if (ret) {
521 			pr_err("MAC%u: attach failed: %d\n", i, ret);
522 			goto error;
523 		}
524 	}
525 
526 	return 0;
527 
528 error:
529 	qtnf_core_detach(bus);
530 
531 	return ret;
532 }
533 EXPORT_SYMBOL_GPL(qtnf_core_attach);
534 
535 void qtnf_core_detach(struct qtnf_bus *bus)
536 {
537 	unsigned int macid;
538 
539 	qtnf_bus_data_rx_stop(bus);
540 
541 	for (macid = 0; macid < QTNF_MAX_MAC; macid++)
542 		qtnf_core_mac_detach(bus, macid);
543 
544 	if (bus->fw_state == QTNF_FW_STATE_ACTIVE)
545 		qtnf_cmd_send_deinit_fw(bus);
546 
547 	bus->fw_state = QTNF_FW_STATE_DEAD;
548 
549 	if (bus->workqueue) {
550 		flush_workqueue(bus->workqueue);
551 		destroy_workqueue(bus->workqueue);
552 	}
553 
554 	kfree(bus->hw_info.rd);
555 	bus->hw_info.rd = NULL;
556 
557 	qtnf_trans_free(bus);
558 }
559 EXPORT_SYMBOL_GPL(qtnf_core_detach);
560 
561 static inline int qtnf_is_frame_meta_magic_valid(struct qtnf_frame_meta_info *m)
562 {
563 	return m->magic_s == 0xAB && m->magic_e == 0xBA;
564 }
565 
566 struct net_device *qtnf_classify_skb(struct qtnf_bus *bus, struct sk_buff *skb)
567 {
568 	struct qtnf_frame_meta_info *meta;
569 	struct net_device *ndev = NULL;
570 	struct qtnf_wmac *mac;
571 	struct qtnf_vif *vif;
572 
573 	meta = (struct qtnf_frame_meta_info *)
574 		(skb_tail_pointer(skb) - sizeof(*meta));
575 
576 	if (unlikely(!qtnf_is_frame_meta_magic_valid(meta))) {
577 		pr_err_ratelimited("invalid magic 0x%x:0x%x\n",
578 				   meta->magic_s, meta->magic_e);
579 		goto out;
580 	}
581 
582 	if (unlikely(meta->macid >= QTNF_MAX_MAC)) {
583 		pr_err_ratelimited("invalid mac(%u)\n", meta->macid);
584 		goto out;
585 	}
586 
587 	if (unlikely(meta->ifidx >= QTNF_MAX_INTF)) {
588 		pr_err_ratelimited("invalid vif(%u)\n", meta->ifidx);
589 		goto out;
590 	}
591 
592 	mac = bus->mac[meta->macid];
593 
594 	if (unlikely(!mac)) {
595 		pr_err_ratelimited("mac(%d) does not exist\n", meta->macid);
596 		goto out;
597 	}
598 
599 	vif = &mac->iflist[meta->ifidx];
600 
601 	if (unlikely(vif->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED)) {
602 		pr_err_ratelimited("vif(%u) does not exists\n", meta->ifidx);
603 		goto out;
604 	}
605 
606 	ndev = vif->netdev;
607 
608 	if (unlikely(!ndev)) {
609 		pr_err_ratelimited("netdev for wlan%u.%u does not exists\n",
610 				   meta->macid, meta->ifidx);
611 		goto out;
612 	}
613 
614 	__skb_trim(skb, skb->len - sizeof(*meta));
615 
616 out:
617 	return ndev;
618 }
619 EXPORT_SYMBOL_GPL(qtnf_classify_skb);
620 
621 MODULE_AUTHOR("Quantenna Communications");
622 MODULE_DESCRIPTION("Quantenna 802.11 wireless LAN FullMAC driver.");
623 MODULE_LICENSE("GPL");
624