Linux Audio

Check our new training course

Loading...
v6.8
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * Copyright (c) 2012 - 2018 Microchip Technology Inc., and its subsidiaries.
   4 * All rights reserved.
   5 */
   6
   7#include <linux/irq.h>
   8#include <linux/kthread.h>
   9#include <linux/firmware.h>
  10#include <linux/netdevice.h>
  11#include <linux/inetdevice.h>
  12
  13#include "cfg80211.h"
  14#include "wlan_cfg.h"
  15
  16#define WILC_MULTICAST_TABLE_SIZE	8
  17#define WILC_MAX_FW_VERSION_STR_SIZE	50
  18
  19/* latest API version supported */
  20#define WILC1000_API_VER		1
  21
  22#define WILC1000_FW_PREFIX		"atmel/wilc1000_wifi_firmware-"
  23#define __WILC1000_FW(api)		WILC1000_FW_PREFIX #api ".bin"
  24#define WILC1000_FW(api)		__WILC1000_FW(api)
  25
  26static irqreturn_t isr_uh_routine(int irq, void *user_data)
  27{
  28	struct wilc *wilc = user_data;
 
 
  29
  30	if (wilc->close) {
  31		pr_err("Can't handle UH interrupt\n");
  32		return IRQ_HANDLED;
  33	}
  34	return IRQ_WAKE_THREAD;
  35}
  36
  37static irqreturn_t isr_bh_routine(int irq, void *userdata)
  38{
  39	struct wilc *wilc = userdata;
 
 
  40
  41	if (wilc->close) {
  42		pr_err("Can't handle BH interrupt\n");
  43		return IRQ_HANDLED;
  44	}
  45
  46	wilc_handle_isr(wilc);
  47
  48	return IRQ_HANDLED;
  49}
  50
  51static int init_irq(struct net_device *dev)
  52{
  53	struct wilc_vif *vif = netdev_priv(dev);
  54	struct wilc *wl = vif->wilc;
  55	int ret;
  56
  57	ret = request_threaded_irq(wl->dev_irq_num, isr_uh_routine,
  58				   isr_bh_routine,
  59				   IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  60				   dev->name, wl);
  61	if (ret) {
  62		netdev_err(dev, "Failed to request IRQ [%d]\n", ret);
  63		return ret;
  64	}
  65	netdev_dbg(dev, "IRQ request succeeded IRQ-NUM= %d\n", wl->dev_irq_num);
  66
  67	return 0;
  68}
  69
  70static void deinit_irq(struct net_device *dev)
  71{
  72	struct wilc_vif *vif = netdev_priv(dev);
  73	struct wilc *wilc = vif->wilc;
  74
  75	/* Deinitialize IRQ */
  76	if (wilc->dev_irq_num)
  77		free_irq(wilc->dev_irq_num, wilc);
  78}
  79
  80void wilc_mac_indicate(struct wilc *wilc)
  81{
  82	s8 status;
  83
  84	wilc_wlan_cfg_get_val(wilc, WID_STATUS, &status, 1);
  85	if (wilc->mac_status == WILC_MAC_STATUS_INIT) {
  86		wilc->mac_status = status;
  87		complete(&wilc->sync_event);
  88	} else {
  89		wilc->mac_status = status;
  90	}
  91}
  92
  93static struct net_device *get_if_handler(struct wilc *wilc, u8 *mac_header)
  94{
  95	struct net_device *ndev = NULL;
  96	struct wilc_vif *vif;
  97	struct ieee80211_hdr *h = (struct ieee80211_hdr *)mac_header;
  98
  99	list_for_each_entry_rcu(vif, &wilc->vif_list, list) {
 100		if (vif->iftype == WILC_STATION_MODE)
 101			if (ether_addr_equal_unaligned(h->addr2, vif->bssid)) {
 102				ndev = vif->ndev;
 103				goto out;
 104			}
 105		if (vif->iftype == WILC_AP_MODE)
 106			if (ether_addr_equal_unaligned(h->addr1, vif->bssid)) {
 107				ndev = vif->ndev;
 108				goto out;
 109			}
 110	}
 111out:
 112	return ndev;
 113}
 114
 115void wilc_wlan_set_bssid(struct net_device *wilc_netdev, const u8 *bssid,
 116			 u8 mode)
 117{
 118	struct wilc_vif *vif = netdev_priv(wilc_netdev);
 119
 120	if (bssid)
 121		ether_addr_copy(vif->bssid, bssid);
 122	else
 123		eth_zero_addr(vif->bssid);
 124
 125	vif->iftype = mode;
 126}
 127
 128int wilc_wlan_get_num_conn_ifcs(struct wilc *wilc)
 129{
 130	int srcu_idx;
 131	u8 ret_val = 0;
 132	struct wilc_vif *vif;
 133
 134	srcu_idx = srcu_read_lock(&wilc->srcu);
 135	list_for_each_entry_rcu(vif, &wilc->vif_list, list) {
 136		if (!is_zero_ether_addr(vif->bssid))
 137			ret_val++;
 138	}
 139	srcu_read_unlock(&wilc->srcu, srcu_idx);
 140	return ret_val;
 141}
 142
 143static int wilc_txq_task(void *vp)
 144{
 145	int ret;
 146	u32 txq_count;
 147	struct wilc *wl = vp;
 148
 149	complete(&wl->txq_thread_started);
 150	while (1) {
 151		if (wait_for_completion_interruptible(&wl->txq_event))
 152			continue;
 153		if (wl->close) {
 154			complete(&wl->txq_thread_started);
 155
 156			while (!kthread_should_stop())
 157				schedule();
 158			break;
 159		}
 160		do {
 161			ret = wilc_wlan_handle_txq(wl, &txq_count);
 162			if (txq_count < FLOW_CONTROL_LOWER_THRESHOLD) {
 163				int srcu_idx;
 164				struct wilc_vif *ifc;
 165
 166				srcu_idx = srcu_read_lock(&wl->srcu);
 167				list_for_each_entry_rcu(ifc, &wl->vif_list,
 168							list) {
 169					if (ifc->mac_opened &&
 170					    netif_queue_stopped(ifc->ndev))
 171						netif_wake_queue(ifc->ndev);
 172				}
 173				srcu_read_unlock(&wl->srcu, srcu_idx);
 174			}
 175			if (ret != WILC_VMM_ENTRY_FULL_RETRY)
 176				break;
 177			/* Back off TX task from sending packets for some time.
 178			 * msleep_interruptible will allow RX task to run and
 179			 * free buffers. TX task will be in TASK_INTERRUPTIBLE
 180			 * state which will put the thread back to CPU running
 181			 * queue when it's signaled even if the timeout isn't
 182			 * elapsed. This gives faster chance for reserved SK
 183			 * buffers to be free.
 184			 */
 185			msleep_interruptible(TX_BACKOFF_WEIGHT_MS);
 186		} while (!wl->close);
 187	}
 188	return 0;
 189}
 190
 191static int wilc_wlan_get_firmware(struct net_device *dev)
 192{
 193	struct wilc_vif *vif = netdev_priv(dev);
 194	struct wilc *wilc = vif->wilc;
 195	int chip_id;
 196	const struct firmware *wilc_fw;
 197	int ret;
 198
 199	chip_id = wilc_get_chipid(wilc, false);
 200
 201	netdev_info(dev, "ChipID [%x] loading firmware [%s]\n", chip_id,
 202		    WILC1000_FW(WILC1000_API_VER));
 203
 204	ret = request_firmware(&wilc_fw, WILC1000_FW(WILC1000_API_VER),
 205			       wilc->dev);
 206	if (ret != 0) {
 207		netdev_err(dev, "%s - firmware not available\n",
 208			   WILC1000_FW(WILC1000_API_VER));
 209		return -EINVAL;
 210	}
 211	wilc->firmware = wilc_fw;
 212
 213	return 0;
 214}
 215
 216static int wilc_start_firmware(struct net_device *dev)
 217{
 218	struct wilc_vif *vif = netdev_priv(dev);
 219	struct wilc *wilc = vif->wilc;
 220	int ret = 0;
 221
 222	ret = wilc_wlan_start(wilc);
 223	if (ret)
 224		return ret;
 225
 226	if (!wait_for_completion_timeout(&wilc->sync_event,
 227					 msecs_to_jiffies(5000)))
 228		return -ETIME;
 229
 230	return 0;
 231}
 232
 233static int wilc1000_firmware_download(struct net_device *dev)
 234{
 235	struct wilc_vif *vif = netdev_priv(dev);
 236	struct wilc *wilc = vif->wilc;
 237	int ret = 0;
 238
 239	if (!wilc->firmware) {
 240		netdev_err(dev, "Firmware buffer is NULL\n");
 241		return -ENOBUFS;
 242	}
 243
 244	ret = wilc_wlan_firmware_download(wilc, wilc->firmware->data,
 245					  wilc->firmware->size);
 246	if (ret)
 247		return ret;
 248
 249	release_firmware(wilc->firmware);
 250	wilc->firmware = NULL;
 251
 252	netdev_dbg(dev, "Download Succeeded\n");
 253
 254	return 0;
 255}
 256
 257static int wilc_init_fw_config(struct net_device *dev, struct wilc_vif *vif)
 258{
 259	struct wilc_priv *priv = &vif->priv;
 260	struct host_if_drv *hif_drv;
 261	u8 b;
 262	u16 hw;
 263	u32 w;
 264
 265	netdev_dbg(dev, "Start configuring Firmware\n");
 266	hif_drv = (struct host_if_drv *)priv->hif_drv;
 267	netdev_dbg(dev, "Host = %p\n", hif_drv);
 268
 269	w = vif->iftype;
 270	cpu_to_le32s(&w);
 271	if (!wilc_wlan_cfg_set(vif, 1, WID_SET_OPERATION_MODE, (u8 *)&w, 4,
 272			       0, 0))
 273		goto fail;
 274
 275	b = WILC_FW_BSS_TYPE_INFRA;
 276	if (!wilc_wlan_cfg_set(vif, 0, WID_BSS_TYPE, &b, 1, 0, 0))
 277		goto fail;
 278
 279	b = WILC_FW_TX_RATE_AUTO;
 280	if (!wilc_wlan_cfg_set(vif, 0, WID_CURRENT_TX_RATE, &b, 1, 0, 0))
 281		goto fail;
 282
 283	b = WILC_FW_OPER_MODE_G_MIXED_11B_2;
 284	if (!wilc_wlan_cfg_set(vif, 0, WID_11G_OPERATING_MODE, &b, 1, 0, 0))
 285		goto fail;
 286
 287	b = WILC_FW_PREAMBLE_SHORT;
 288	if (!wilc_wlan_cfg_set(vif, 0, WID_PREAMBLE, &b, 1, 0, 0))
 289		goto fail;
 290
 291	b = WILC_FW_11N_PROT_AUTO;
 292	if (!wilc_wlan_cfg_set(vif, 0, WID_11N_PROT_MECH, &b, 1, 0, 0))
 293		goto fail;
 294
 295	b = WILC_FW_ACTIVE_SCAN;
 296	if (!wilc_wlan_cfg_set(vif, 0, WID_SCAN_TYPE, &b, 1, 0, 0))
 297		goto fail;
 298
 299	b = WILC_FW_SITE_SURVEY_OFF;
 300	if (!wilc_wlan_cfg_set(vif, 0, WID_SITE_SURVEY, &b, 1, 0, 0))
 301		goto fail;
 302
 303	hw = 0xffff;
 304	cpu_to_le16s(&hw);
 305	if (!wilc_wlan_cfg_set(vif, 0, WID_RTS_THRESHOLD, (u8 *)&hw, 2, 0, 0))
 306		goto fail;
 307
 308	hw = 2346;
 309	cpu_to_le16s(&hw);
 310	if (!wilc_wlan_cfg_set(vif, 0, WID_FRAG_THRESHOLD, (u8 *)&hw, 2, 0, 0))
 311		goto fail;
 312
 313	b = 0;
 314	if (!wilc_wlan_cfg_set(vif, 0, WID_BCAST_SSID, &b, 1, 0, 0))
 315		goto fail;
 316
 317	b = 1;
 318	if (!wilc_wlan_cfg_set(vif, 0, WID_QOS_ENABLE, &b, 1, 0, 0))
 319		goto fail;
 320
 321	b = WILC_FW_NO_POWERSAVE;
 322	if (!wilc_wlan_cfg_set(vif, 0, WID_POWER_MANAGEMENT, &b, 1, 0, 0))
 323		goto fail;
 324
 325	b = WILC_FW_SEC_NO;
 326	if (!wilc_wlan_cfg_set(vif, 0, WID_11I_MODE, &b, 1, 0, 0))
 327		goto fail;
 328
 329	b = WILC_FW_AUTH_OPEN_SYSTEM;
 330	if (!wilc_wlan_cfg_set(vif, 0, WID_AUTH_TYPE, &b, 1, 0, 0))
 331		goto fail;
 332
 333	b = 3;
 334	if (!wilc_wlan_cfg_set(vif, 0, WID_LISTEN_INTERVAL, &b, 1, 0, 0))
 335		goto fail;
 336
 337	b = 3;
 338	if (!wilc_wlan_cfg_set(vif, 0, WID_DTIM_PERIOD, &b, 1, 0, 0))
 339		goto fail;
 340
 341	b = WILC_FW_ACK_POLICY_NORMAL;
 342	if (!wilc_wlan_cfg_set(vif, 0, WID_ACK_POLICY, &b, 1, 0, 0))
 343		goto fail;
 344
 345	b = 0;
 346	if (!wilc_wlan_cfg_set(vif, 0, WID_USER_CONTROL_ON_TX_POWER, &b, 1,
 347			       0, 0))
 348		goto fail;
 349
 350	b = 48;
 351	if (!wilc_wlan_cfg_set(vif, 0, WID_TX_POWER_LEVEL_11A, &b, 1, 0, 0))
 352		goto fail;
 353
 354	b = 28;
 355	if (!wilc_wlan_cfg_set(vif, 0, WID_TX_POWER_LEVEL_11B, &b, 1, 0, 0))
 356		goto fail;
 357
 358	hw = 100;
 359	cpu_to_le16s(&hw);
 360	if (!wilc_wlan_cfg_set(vif, 0, WID_BEACON_INTERVAL, (u8 *)&hw, 2, 0, 0))
 361		goto fail;
 362
 363	b = WILC_FW_REKEY_POLICY_DISABLE;
 364	if (!wilc_wlan_cfg_set(vif, 0, WID_REKEY_POLICY, &b, 1, 0, 0))
 365		goto fail;
 366
 367	w = 84600;
 368	cpu_to_le32s(&w);
 369	if (!wilc_wlan_cfg_set(vif, 0, WID_REKEY_PERIOD, (u8 *)&w, 4, 0, 0))
 370		goto fail;
 371
 372	w = 500;
 373	cpu_to_le32s(&w);
 374	if (!wilc_wlan_cfg_set(vif, 0, WID_REKEY_PACKET_COUNT, (u8 *)&w, 4, 0,
 375			       0))
 376		goto fail;
 377
 378	b = 1;
 379	if (!wilc_wlan_cfg_set(vif, 0, WID_SHORT_SLOT_ALLOWED, &b, 1, 0,
 380			       0))
 381		goto fail;
 382
 383	b = WILC_FW_ERP_PROT_SELF_CTS;
 384	if (!wilc_wlan_cfg_set(vif, 0, WID_11N_ERP_PROT_TYPE, &b, 1, 0, 0))
 385		goto fail;
 386
 387	b = 1;
 388	if (!wilc_wlan_cfg_set(vif, 0, WID_11N_ENABLE, &b, 1, 0, 0))
 389		goto fail;
 390
 391	b = WILC_FW_11N_OP_MODE_HT_MIXED;
 392	if (!wilc_wlan_cfg_set(vif, 0, WID_11N_OPERATING_MODE, &b, 1, 0, 0))
 393		goto fail;
 394
 395	b = 1;
 396	if (!wilc_wlan_cfg_set(vif, 0, WID_11N_TXOP_PROT_DISABLE, &b, 1, 0, 0))
 397		goto fail;
 398
 399	b = WILC_FW_OBBS_NONHT_DETECT_PROTECT_REPORT;
 400	if (!wilc_wlan_cfg_set(vif, 0, WID_11N_OBSS_NONHT_DETECTION, &b, 1,
 401			       0, 0))
 402		goto fail;
 403
 404	b = WILC_FW_HT_PROT_RTS_CTS_NONHT;
 405	if (!wilc_wlan_cfg_set(vif, 0, WID_11N_HT_PROT_TYPE, &b, 1, 0, 0))
 406		goto fail;
 407
 408	b = 0;
 409	if (!wilc_wlan_cfg_set(vif, 0, WID_11N_RIFS_PROT_ENABLE, &b, 1, 0,
 410			       0))
 411		goto fail;
 412
 413	b = 7;
 414	if (!wilc_wlan_cfg_set(vif, 0, WID_11N_CURRENT_TX_MCS, &b, 1, 0, 0))
 415		goto fail;
 416
 417	b = 1;
 418	if (!wilc_wlan_cfg_set(vif, 0, WID_11N_IMMEDIATE_BA_ENABLED, &b, 1,
 419			       1, 1))
 420		goto fail;
 421
 422	return 0;
 423
 424fail:
 425	return -EINVAL;
 426}
 427
 428static void wlan_deinitialize_threads(struct net_device *dev)
 429{
 430	struct wilc_vif *vif = netdev_priv(dev);
 431	struct wilc *wl = vif->wilc;
 432
 433	wl->close = 1;
 434
 435	complete(&wl->txq_event);
 436
 437	if (wl->txq_thread) {
 438		kthread_stop(wl->txq_thread);
 439		wl->txq_thread = NULL;
 440	}
 441}
 442
 443static void wilc_wlan_deinitialize(struct net_device *dev)
 444{
 445	struct wilc_vif *vif = netdev_priv(dev);
 446	struct wilc *wl = vif->wilc;
 447
 448	if (!wl) {
 449		netdev_err(dev, "wl is NULL\n");
 450		return;
 451	}
 452
 453	if (wl->initialized) {
 454		netdev_info(dev, "Deinitializing wilc1000...\n");
 455
 456		if (!wl->dev_irq_num &&
 457		    wl->hif_func->disable_interrupt) {
 458			mutex_lock(&wl->hif_cs);
 459			wl->hif_func->disable_interrupt(wl);
 460			mutex_unlock(&wl->hif_cs);
 461		}
 462		complete(&wl->txq_event);
 463
 464		wlan_deinitialize_threads(dev);
 465		deinit_irq(dev);
 466
 467		wilc_wlan_stop(wl, vif);
 468		wilc_wlan_cleanup(dev);
 469
 470		wl->initialized = false;
 471
 472		netdev_dbg(dev, "wilc1000 deinitialization Done\n");
 473	} else {
 474		netdev_dbg(dev, "wilc1000 is not initialized\n");
 475	}
 476}
 477
 478static int wlan_initialize_threads(struct net_device *dev)
 479{
 480	struct wilc_vif *vif = netdev_priv(dev);
 481	struct wilc *wilc = vif->wilc;
 482
 483	wilc->txq_thread = kthread_run(wilc_txq_task, (void *)wilc,
 484				       "%s-tx", dev->name);
 485	if (IS_ERR(wilc->txq_thread)) {
 486		netdev_err(dev, "couldn't create TXQ thread\n");
 487		wilc->close = 1;
 488		return PTR_ERR(wilc->txq_thread);
 489	}
 490	wait_for_completion(&wilc->txq_thread_started);
 491
 492	return 0;
 493}
 494
 495static int wilc_wlan_initialize(struct net_device *dev, struct wilc_vif *vif)
 496{
 497	int ret = 0;
 498	struct wilc *wl = vif->wilc;
 499
 500	if (!wl->initialized) {
 501		wl->mac_status = WILC_MAC_STATUS_INIT;
 502		wl->close = 0;
 503
 504		ret = wilc_wlan_init(dev);
 505		if (ret)
 506			return ret;
 507
 508		ret = wlan_initialize_threads(dev);
 509		if (ret)
 510			goto fail_wilc_wlan;
 511
 512		if (wl->dev_irq_num && init_irq(dev)) {
 513			ret = -EIO;
 514			goto fail_threads;
 515		}
 516
 517		if (!wl->dev_irq_num &&
 518		    wl->hif_func->enable_interrupt &&
 519		    wl->hif_func->enable_interrupt(wl)) {
 520			ret = -EIO;
 521			goto fail_irq_init;
 522		}
 523
 524		ret = wilc_wlan_get_firmware(dev);
 525		if (ret)
 526			goto fail_irq_enable;
 527
 528		ret = wilc1000_firmware_download(dev);
 529		if (ret)
 530			goto fail_irq_enable;
 531
 532		ret = wilc_start_firmware(dev);
 533		if (ret)
 534			goto fail_irq_enable;
 535
 536		if (wilc_wlan_cfg_get(vif, 1, WID_FIRMWARE_VERSION, 1, 0)) {
 537			int size;
 538			char firmware_ver[WILC_MAX_FW_VERSION_STR_SIZE];
 539
 540			size = wilc_wlan_cfg_get_val(wl, WID_FIRMWARE_VERSION,
 541						     firmware_ver,
 542						     sizeof(firmware_ver));
 543			firmware_ver[size] = '\0';
 544			netdev_dbg(dev, "Firmware Ver = %s\n", firmware_ver);
 545		}
 546
 547		ret = wilc_init_fw_config(dev, vif);
 548		if (ret) {
 549			netdev_err(dev, "Failed to configure firmware\n");
 550			goto fail_fw_start;
 551		}
 552		wl->initialized = true;
 553		return 0;
 554
 555fail_fw_start:
 556		wilc_wlan_stop(wl, vif);
 557
 558fail_irq_enable:
 559		if (!wl->dev_irq_num &&
 560		    wl->hif_func->disable_interrupt)
 561			wl->hif_func->disable_interrupt(wl);
 562fail_irq_init:
 563		if (wl->dev_irq_num)
 564			deinit_irq(dev);
 565fail_threads:
 566		wlan_deinitialize_threads(dev);
 567fail_wilc_wlan:
 568		wilc_wlan_cleanup(dev);
 569		netdev_err(dev, "WLAN initialization FAILED\n");
 570	} else {
 571		netdev_dbg(dev, "wilc1000 already initialized\n");
 572	}
 573	return ret;
 574}
 575
 576static int mac_init_fn(struct net_device *ndev)
 577{
 578	netif_start_queue(ndev);
 579	netif_stop_queue(ndev);
 580
 581	return 0;
 582}
 583
 584static int wilc_mac_open(struct net_device *ndev)
 585{
 586	struct wilc_vif *vif = netdev_priv(ndev);
 587	struct wilc *wl = vif->wilc;
 
 588	int ret = 0;
 589	struct mgmt_frame_regs mgmt_regs = {};
 590	u8 addr[ETH_ALEN] __aligned(2);
 591
 592	if (!wl || !wl->dev) {
 593		netdev_err(ndev, "device not ready\n");
 594		return -ENODEV;
 595	}
 596
 597	netdev_dbg(ndev, "MAC OPEN[%p]\n", ndev);
 598
 599	ret = wilc_init_host_int(ndev);
 600	if (ret)
 601		return ret;
 602
 603	ret = wilc_wlan_initialize(ndev, vif);
 604	if (ret) {
 605		wilc_deinit_host_int(ndev);
 606		return ret;
 607	}
 608
 609	wilc_set_operation_mode(vif, wilc_get_vif_idx(vif), vif->iftype,
 610				vif->idx);
 611
 612	if (is_valid_ether_addr(ndev->dev_addr)) {
 613		ether_addr_copy(addr, ndev->dev_addr);
 614		wilc_set_mac_address(vif, addr);
 615	} else {
 616		wilc_get_mac_address(vif, addr);
 617		eth_hw_addr_set(ndev, addr);
 618	}
 619	netdev_dbg(ndev, "Mac address: %pM\n", ndev->dev_addr);
 620
 621	if (!is_valid_ether_addr(ndev->dev_addr)) {
 622		netdev_err(ndev, "Wrong MAC address\n");
 623		wilc_deinit_host_int(ndev);
 624		wilc_wlan_deinitialize(ndev);
 625		return -EINVAL;
 626	}
 627
 628	mgmt_regs.interface_stypes = vif->mgmt_reg_stypes;
 629	/* so we detect a change */
 630	vif->mgmt_reg_stypes = 0;
 631	wilc_update_mgmt_frame_registrations(vif->ndev->ieee80211_ptr->wiphy,
 632					     vif->ndev->ieee80211_ptr,
 633					     &mgmt_regs);
 634	netif_wake_queue(ndev);
 635	wl->open_ifcs++;
 636	vif->mac_opened = 1;
 637	return 0;
 638}
 639
 640static struct net_device_stats *mac_stats(struct net_device *dev)
 641{
 642	struct wilc_vif *vif = netdev_priv(dev);
 643
 644	return &vif->netstats;
 645}
 646
 647static int wilc_set_mac_addr(struct net_device *dev, void *p)
 648{
 649	int result;
 650	struct wilc_vif *vif = netdev_priv(dev);
 651	struct wilc *wilc = vif->wilc;
 652	struct sockaddr *addr = (struct sockaddr *)p;
 653	unsigned char mac_addr[ETH_ALEN];
 654	struct wilc_vif *tmp_vif;
 655	int srcu_idx;
 656
 657	if (!is_valid_ether_addr(addr->sa_data))
 658		return -EADDRNOTAVAIL;
 659
 660	if (!vif->mac_opened) {
 661		eth_commit_mac_addr_change(dev, p);
 662		return 0;
 663	}
 664
 665	/* Verify MAC Address is not already in use: */
 666
 667	srcu_idx = srcu_read_lock(&wilc->srcu);
 668	list_for_each_entry_rcu(tmp_vif, &wilc->vif_list, list) {
 669		wilc_get_mac_address(tmp_vif, mac_addr);
 670		if (ether_addr_equal(addr->sa_data, mac_addr)) {
 671			if (vif != tmp_vif) {
 672				srcu_read_unlock(&wilc->srcu, srcu_idx);
 673				return -EADDRNOTAVAIL;
 674			}
 675			srcu_read_unlock(&wilc->srcu, srcu_idx);
 676			return 0;
 677		}
 678	}
 679	srcu_read_unlock(&wilc->srcu, srcu_idx);
 680
 681	result = wilc_set_mac_address(vif, (u8 *)addr->sa_data);
 682	if (result)
 683		return result;
 684
 685	eth_commit_mac_addr_change(dev, p);
 686	return result;
 687}
 688
 689static void wilc_set_multicast_list(struct net_device *dev)
 690{
 691	struct netdev_hw_addr *ha;
 692	struct wilc_vif *vif = netdev_priv(dev);
 693	int i;
 694	u8 *mc_list;
 695	u8 *cur_mc;
 696
 697	if (dev->flags & IFF_PROMISC)
 698		return;
 699
 700	if (dev->flags & IFF_ALLMULTI ||
 701	    dev->mc.count > WILC_MULTICAST_TABLE_SIZE) {
 702		wilc_setup_multicast_filter(vif, 0, 0, NULL);
 703		return;
 704	}
 705
 706	if (dev->mc.count == 0) {
 707		wilc_setup_multicast_filter(vif, 1, 0, NULL);
 708		return;
 709	}
 710
 711	mc_list = kmalloc_array(dev->mc.count, ETH_ALEN, GFP_ATOMIC);
 712	if (!mc_list)
 713		return;
 714
 715	cur_mc = mc_list;
 716	i = 0;
 717	netdev_for_each_mc_addr(ha, dev) {
 718		memcpy(cur_mc, ha->addr, ETH_ALEN);
 719		netdev_dbg(dev, "Entry[%d]: %pM\n", i, cur_mc);
 720		i++;
 721		cur_mc += ETH_ALEN;
 722	}
 723
 724	if (wilc_setup_multicast_filter(vif, 1, dev->mc.count, mc_list))
 725		kfree(mc_list);
 726}
 727
 728static void wilc_tx_complete(void *priv, int status)
 729{
 730	struct tx_complete_data *pv_data = priv;
 731
 732	dev_kfree_skb(pv_data->skb);
 733	kfree(pv_data);
 734}
 735
 736netdev_tx_t wilc_mac_xmit(struct sk_buff *skb, struct net_device *ndev)
 737{
 738	struct wilc_vif *vif = netdev_priv(ndev);
 739	struct wilc *wilc = vif->wilc;
 740	struct tx_complete_data *tx_data = NULL;
 741	int queue_count;
 742
 743	if (skb->dev != ndev) {
 744		netdev_err(ndev, "Packet not destined to this device\n");
 745		dev_kfree_skb(skb);
 746		return NETDEV_TX_OK;
 747	}
 748
 749	tx_data = kmalloc(sizeof(*tx_data), GFP_ATOMIC);
 750	if (!tx_data) {
 751		dev_kfree_skb(skb);
 752		netif_wake_queue(ndev);
 753		return NETDEV_TX_OK;
 754	}
 755
 756	tx_data->buff = skb->data;
 757	tx_data->size = skb->len;
 758	tx_data->skb  = skb;
 759
 760	vif->netstats.tx_packets++;
 761	vif->netstats.tx_bytes += tx_data->size;
 762	queue_count = wilc_wlan_txq_add_net_pkt(ndev, tx_data,
 763						tx_data->buff, tx_data->size,
 764						wilc_tx_complete);
 765
 766	if (queue_count > FLOW_CONTROL_UPPER_THRESHOLD) {
 767		int srcu_idx;
 768		struct wilc_vif *vif;
 769
 770		srcu_idx = srcu_read_lock(&wilc->srcu);
 771		list_for_each_entry_rcu(vif, &wilc->vif_list, list) {
 772			if (vif->mac_opened)
 773				netif_stop_queue(vif->ndev);
 774		}
 775		srcu_read_unlock(&wilc->srcu, srcu_idx);
 776	}
 777
 778	return NETDEV_TX_OK;
 779}
 780
 781static int wilc_mac_close(struct net_device *ndev)
 782{
 783	struct wilc_vif *vif = netdev_priv(ndev);
 784	struct wilc *wl = vif->wilc;
 785
 786	netdev_dbg(ndev, "Mac close\n");
 787
 788	if (wl->open_ifcs > 0)
 789		wl->open_ifcs--;
 790	else
 791		return 0;
 792
 793	if (vif->ndev) {
 794		netif_stop_queue(vif->ndev);
 795
 796		wilc_handle_disconnect(vif);
 797		wilc_deinit_host_int(vif->ndev);
 798	}
 799
 800	if (wl->open_ifcs == 0) {
 801		netdev_dbg(ndev, "Deinitializing wilc1000\n");
 802		wl->close = 1;
 803		wilc_wlan_deinitialize(ndev);
 804	}
 805
 806	vif->mac_opened = 0;
 807
 808	return 0;
 809}
 810
 811void wilc_frmw_to_host(struct wilc *wilc, u8 *buff, u32 size,
 812		       u32 pkt_offset)
 813{
 814	unsigned int frame_len = 0;
 815	int stats;
 816	unsigned char *buff_to_send = NULL;
 817	struct sk_buff *skb;
 818	struct net_device *wilc_netdev;
 819	struct wilc_vif *vif;
 820
 821	if (!wilc)
 822		return;
 823
 824	wilc_netdev = get_if_handler(wilc, buff);
 825	if (!wilc_netdev)
 826		return;
 827
 828	buff += pkt_offset;
 829	vif = netdev_priv(wilc_netdev);
 830
 831	if (size > 0) {
 832		frame_len = size;
 833		buff_to_send = buff;
 834
 835		skb = dev_alloc_skb(frame_len);
 836		if (!skb)
 837			return;
 838
 839		skb->dev = wilc_netdev;
 840
 841		skb_put_data(skb, buff_to_send, frame_len);
 842
 843		skb->protocol = eth_type_trans(skb, wilc_netdev);
 844		vif->netstats.rx_packets++;
 845		vif->netstats.rx_bytes += frame_len;
 846		skb->ip_summed = CHECKSUM_UNNECESSARY;
 847		stats = netif_rx(skb);
 848		netdev_dbg(wilc_netdev, "netif_rx ret value is: %d\n", stats);
 849	}
 850}
 851
 852void wilc_wfi_mgmt_rx(struct wilc *wilc, u8 *buff, u32 size, bool is_auth)
 853{
 854	int srcu_idx;
 855	struct wilc_vif *vif;
 856
 857	srcu_idx = srcu_read_lock(&wilc->srcu);
 858	list_for_each_entry_rcu(vif, &wilc->vif_list, list) {
 859		struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buff;
 860		u16 type = le16_to_cpup((__le16 *)buff);
 861		u32 type_bit = BIT(type >> 4);
 862		u32 auth_bit = BIT(IEEE80211_STYPE_AUTH >> 4);
 863
 864		if ((vif->mgmt_reg_stypes & auth_bit &&
 865		     ieee80211_is_auth(mgmt->frame_control)) &&
 866		    vif->iftype == WILC_STATION_MODE && is_auth) {
 867			wilc_wfi_mgmt_frame_rx(vif, buff, size);
 868			break;
 869		}
 870
 871		if (vif->priv.p2p_listen_state &&
 872		    vif->mgmt_reg_stypes & type_bit)
 873			wilc_wfi_p2p_rx(vif, buff, size);
 874
 875		if (vif->monitor_flag)
 876			wilc_wfi_monitor_rx(wilc->monitor_dev, buff, size);
 877	}
 878	srcu_read_unlock(&wilc->srcu, srcu_idx);
 879}
 880
 881static const struct net_device_ops wilc_netdev_ops = {
 882	.ndo_init = mac_init_fn,
 883	.ndo_open = wilc_mac_open,
 884	.ndo_stop = wilc_mac_close,
 885	.ndo_set_mac_address = wilc_set_mac_addr,
 886	.ndo_start_xmit = wilc_mac_xmit,
 887	.ndo_get_stats = mac_stats,
 888	.ndo_set_rx_mode  = wilc_set_multicast_list,
 889};
 890
 891void wilc_netdev_cleanup(struct wilc *wilc)
 892{
 893	struct wilc_vif *vif;
 894	int srcu_idx, ifc_cnt = 0;
 895
 896	if (!wilc)
 897		return;
 898
 899	if (wilc->firmware) {
 900		release_firmware(wilc->firmware);
 901		wilc->firmware = NULL;
 902	}
 903
 904	srcu_idx = srcu_read_lock(&wilc->srcu);
 905	list_for_each_entry_rcu(vif, &wilc->vif_list, list) {
 906		if (vif->ndev)
 907			unregister_netdev(vif->ndev);
 908	}
 909	srcu_read_unlock(&wilc->srcu, srcu_idx);
 910
 911	wilc_wfi_deinit_mon_interface(wilc, false);
 
 912	destroy_workqueue(wilc->hif_workqueue);
 913
 914	while (ifc_cnt < WILC_NUM_CONCURRENT_IFC) {
 915		mutex_lock(&wilc->vif_mutex);
 916		if (wilc->vif_num <= 0) {
 917			mutex_unlock(&wilc->vif_mutex);
 918			break;
 919		}
 920		vif = wilc_get_wl_to_vif(wilc);
 921		if (!IS_ERR(vif))
 922			list_del_rcu(&vif->list);
 923
 924		wilc->vif_num--;
 925		mutex_unlock(&wilc->vif_mutex);
 926		synchronize_srcu(&wilc->srcu);
 927		ifc_cnt++;
 928	}
 929
 930	wilc_wlan_cfg_deinit(wilc);
 931	wlan_deinit_locks(wilc);
 
 932	wiphy_unregister(wilc->wiphy);
 933	wiphy_free(wilc->wiphy);
 934}
 935EXPORT_SYMBOL_GPL(wilc_netdev_cleanup);
 936
 937static u8 wilc_get_available_idx(struct wilc *wl)
 938{
 939	int idx = 0;
 940	struct wilc_vif *vif;
 941	int srcu_idx;
 942
 943	srcu_idx = srcu_read_lock(&wl->srcu);
 944	list_for_each_entry_rcu(vif, &wl->vif_list, list) {
 945		if (vif->idx == 0)
 946			idx = 1;
 947		else
 948			idx = 0;
 949	}
 950	srcu_read_unlock(&wl->srcu, srcu_idx);
 951	return idx;
 952}
 953
 954struct wilc_vif *wilc_netdev_ifc_init(struct wilc *wl, const char *name,
 955				      int vif_type, enum nl80211_iftype type,
 956				      bool rtnl_locked)
 957{
 958	struct net_device *ndev;
 959	struct wilc_vif *vif;
 960	int ret;
 961
 962	ndev = alloc_etherdev(sizeof(*vif));
 963	if (!ndev)
 964		return ERR_PTR(-ENOMEM);
 965
 966	vif = netdev_priv(ndev);
 967	ndev->ieee80211_ptr = &vif->priv.wdev;
 968	strcpy(ndev->name, name);
 969	vif->wilc = wl;
 970	vif->ndev = ndev;
 971	ndev->ml_priv = vif;
 972
 973	ndev->netdev_ops = &wilc_netdev_ops;
 974
 975	SET_NETDEV_DEV(ndev, wiphy_dev(wl->wiphy));
 976
 977	vif->priv.wdev.wiphy = wl->wiphy;
 978	vif->priv.wdev.netdev = ndev;
 979	vif->priv.wdev.iftype = type;
 980	vif->priv.dev = ndev;
 981
 982	if (rtnl_locked)
 983		ret = cfg80211_register_netdevice(ndev);
 984	else
 985		ret = register_netdev(ndev);
 986
 987	if (ret) {
 988		ret = -EFAULT;
 989		goto error;
 990	}
 991
 992	wl->hif_workqueue = alloc_ordered_workqueue("%s-wq", WQ_MEM_RECLAIM,
 993						    ndev->name);
 994	if (!wl->hif_workqueue) {
 995		ret = -ENOMEM;
 996		goto unregister_netdev;
 997	}
 998
 999	ndev->needs_free_netdev = true;
1000	vif->iftype = vif_type;
1001	vif->idx = wilc_get_available_idx(wl);
1002	vif->mac_opened = 0;
1003	mutex_lock(&wl->vif_mutex);
1004	list_add_tail_rcu(&vif->list, &wl->vif_list);
1005	wl->vif_num += 1;
1006	mutex_unlock(&wl->vif_mutex);
1007	synchronize_srcu(&wl->srcu);
1008
1009	return vif;
1010
1011unregister_netdev:
1012	if (rtnl_locked)
1013		cfg80211_unregister_netdevice(ndev);
1014	else
1015		unregister_netdev(ndev);
1016  error:
1017	free_netdev(ndev);
1018	return ERR_PTR(ret);
1019}
1020
1021MODULE_DESCRIPTION("Atmel WILC1000 core wireless driver");
1022MODULE_LICENSE("GPL");
1023MODULE_FIRMWARE(WILC1000_FW(WILC1000_API_VER));
v5.9
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Copyright (c) 2012 - 2018 Microchip Technology Inc., and its subsidiaries.
  4 * All rights reserved.
  5 */
  6
  7#include <linux/irq.h>
  8#include <linux/kthread.h>
  9#include <linux/firmware.h>
 10#include <linux/netdevice.h>
 11#include <linux/inetdevice.h>
 12
 13#include "cfg80211.h"
 14#include "wlan_cfg.h"
 15
 16#define WILC_MULTICAST_TABLE_SIZE	8
 
 17
 18/* latest API version supported */
 19#define WILC1000_API_VER		1
 20
 21#define WILC1000_FW_PREFIX		"atmel/wilc1000_wifi_firmware-"
 22#define __WILC1000_FW(api)		WILC1000_FW_PREFIX #api ".bin"
 23#define WILC1000_FW(api)		__WILC1000_FW(api)
 24
 25static irqreturn_t isr_uh_routine(int irq, void *user_data)
 26{
 27	struct net_device *dev = user_data;
 28	struct wilc_vif *vif = netdev_priv(dev);
 29	struct wilc *wilc = vif->wilc;
 30
 31	if (wilc->close) {
 32		netdev_err(dev, "Can't handle UH interrupt\n");
 33		return IRQ_HANDLED;
 34	}
 35	return IRQ_WAKE_THREAD;
 36}
 37
 38static irqreturn_t isr_bh_routine(int irq, void *userdata)
 39{
 40	struct net_device *dev = userdata;
 41	struct wilc_vif *vif = netdev_priv(userdata);
 42	struct wilc *wilc = vif->wilc;
 43
 44	if (wilc->close) {
 45		netdev_err(dev, "Can't handle BH interrupt\n");
 46		return IRQ_HANDLED;
 47	}
 48
 49	wilc_handle_isr(wilc);
 50
 51	return IRQ_HANDLED;
 52}
 53
 54static int init_irq(struct net_device *dev)
 55{
 56	struct wilc_vif *vif = netdev_priv(dev);
 57	struct wilc *wl = vif->wilc;
 58	int ret;
 59
 60	ret = request_threaded_irq(wl->dev_irq_num, isr_uh_routine,
 61				   isr_bh_routine,
 62				   IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
 63				   "WILC_IRQ", dev);
 64	if (ret) {
 65		netdev_err(dev, "Failed to request IRQ [%d]\n", ret);
 66		return ret;
 67	}
 68	netdev_dbg(dev, "IRQ request succeeded IRQ-NUM= %d\n", wl->dev_irq_num);
 69
 70	return 0;
 71}
 72
 73static void deinit_irq(struct net_device *dev)
 74{
 75	struct wilc_vif *vif = netdev_priv(dev);
 76	struct wilc *wilc = vif->wilc;
 77
 78	/* Deinitialize IRQ */
 79	if (wilc->dev_irq_num)
 80		free_irq(wilc->dev_irq_num, wilc);
 81}
 82
 83void wilc_mac_indicate(struct wilc *wilc)
 84{
 85	s8 status;
 86
 87	wilc_wlan_cfg_get_val(wilc, WID_STATUS, &status, 1);
 88	if (wilc->mac_status == WILC_MAC_STATUS_INIT) {
 89		wilc->mac_status = status;
 90		complete(&wilc->sync_event);
 91	} else {
 92		wilc->mac_status = status;
 93	}
 94}
 95
 96static struct net_device *get_if_handler(struct wilc *wilc, u8 *mac_header)
 97{
 98	struct net_device *ndev = NULL;
 99	struct wilc_vif *vif;
100	struct ieee80211_hdr *h = (struct ieee80211_hdr *)mac_header;
101
102	list_for_each_entry_rcu(vif, &wilc->vif_list, list) {
103		if (vif->mode == WILC_STATION_MODE)
104			if (ether_addr_equal_unaligned(h->addr2, vif->bssid)) {
105				ndev = vif->ndev;
106				goto out;
107			}
108		if (vif->mode == WILC_AP_MODE)
109			if (ether_addr_equal_unaligned(h->addr1, vif->bssid)) {
110				ndev = vif->ndev;
111				goto out;
112			}
113	}
114out:
115	return ndev;
116}
117
118void wilc_wlan_set_bssid(struct net_device *wilc_netdev, u8 *bssid, u8 mode)
 
119{
120	struct wilc_vif *vif = netdev_priv(wilc_netdev);
121
122	if (bssid)
123		ether_addr_copy(vif->bssid, bssid);
124	else
125		eth_zero_addr(vif->bssid);
126
127	vif->mode = mode;
128}
129
130int wilc_wlan_get_num_conn_ifcs(struct wilc *wilc)
131{
132	int srcu_idx;
133	u8 ret_val = 0;
134	struct wilc_vif *vif;
135
136	srcu_idx = srcu_read_lock(&wilc->srcu);
137	list_for_each_entry_rcu(vif, &wilc->vif_list, list) {
138		if (!is_zero_ether_addr(vif->bssid))
139			ret_val++;
140	}
141	srcu_read_unlock(&wilc->srcu, srcu_idx);
142	return ret_val;
143}
144
145static int wilc_txq_task(void *vp)
146{
147	int ret;
148	u32 txq_count;
149	struct wilc *wl = vp;
150
151	complete(&wl->txq_thread_started);
152	while (1) {
153		wait_for_completion(&wl->txq_event);
154
155		if (wl->close) {
156			complete(&wl->txq_thread_started);
157
158			while (!kthread_should_stop())
159				schedule();
160			break;
161		}
162		do {
163			ret = wilc_wlan_handle_txq(wl, &txq_count);
164			if (txq_count < FLOW_CONTROL_LOWER_THRESHOLD) {
165				int srcu_idx;
166				struct wilc_vif *ifc;
167
168				srcu_idx = srcu_read_lock(&wl->srcu);
169				list_for_each_entry_rcu(ifc, &wl->vif_list,
170							list) {
171					if (ifc->mac_opened && ifc->ndev)
 
172						netif_wake_queue(ifc->ndev);
173				}
174				srcu_read_unlock(&wl->srcu, srcu_idx);
175			}
176		} while (ret == WILC_VMM_ENTRY_FULL_RETRY && !wl->close);
 
 
 
 
 
 
 
 
 
 
 
177	}
178	return 0;
179}
180
181static int wilc_wlan_get_firmware(struct net_device *dev)
182{
183	struct wilc_vif *vif = netdev_priv(dev);
184	struct wilc *wilc = vif->wilc;
185	int chip_id;
186	const struct firmware *wilc_fw;
187	int ret;
188
189	chip_id = wilc_get_chipid(wilc, false);
190
191	netdev_info(dev, "ChipID [%x] loading firmware [%s]\n", chip_id,
192		    WILC1000_FW(WILC1000_API_VER));
193
194	ret = request_firmware(&wilc_fw, WILC1000_FW(WILC1000_API_VER),
195			       wilc->dev);
196	if (ret != 0) {
197		netdev_err(dev, "%s - firmware not available\n",
198			   WILC1000_FW(WILC1000_API_VER));
199		return -EINVAL;
200	}
201	wilc->firmware = wilc_fw;
202
203	return 0;
204}
205
206static int wilc_start_firmware(struct net_device *dev)
207{
208	struct wilc_vif *vif = netdev_priv(dev);
209	struct wilc *wilc = vif->wilc;
210	int ret = 0;
211
212	ret = wilc_wlan_start(wilc);
213	if (ret)
214		return ret;
215
216	if (!wait_for_completion_timeout(&wilc->sync_event,
217					 msecs_to_jiffies(5000)))
218		return -ETIME;
219
220	return 0;
221}
222
223static int wilc1000_firmware_download(struct net_device *dev)
224{
225	struct wilc_vif *vif = netdev_priv(dev);
226	struct wilc *wilc = vif->wilc;
227	int ret = 0;
228
229	if (!wilc->firmware) {
230		netdev_err(dev, "Firmware buffer is NULL\n");
231		return -ENOBUFS;
232	}
233
234	ret = wilc_wlan_firmware_download(wilc, wilc->firmware->data,
235					  wilc->firmware->size);
236	if (ret)
237		return ret;
238
239	release_firmware(wilc->firmware);
240	wilc->firmware = NULL;
241
242	netdev_dbg(dev, "Download Succeeded\n");
243
244	return 0;
245}
246
247static int wilc_init_fw_config(struct net_device *dev, struct wilc_vif *vif)
248{
249	struct wilc_priv *priv = &vif->priv;
250	struct host_if_drv *hif_drv;
251	u8 b;
252	u16 hw;
253	u32 w;
254
255	netdev_dbg(dev, "Start configuring Firmware\n");
256	hif_drv = (struct host_if_drv *)priv->hif_drv;
257	netdev_dbg(dev, "Host = %p\n", hif_drv);
258
259	w = vif->iftype;
260	cpu_to_le32s(&w);
261	if (!wilc_wlan_cfg_set(vif, 1, WID_SET_OPERATION_MODE, (u8 *)&w, 4,
262			       0, 0))
263		goto fail;
264
265	b = WILC_FW_BSS_TYPE_INFRA;
266	if (!wilc_wlan_cfg_set(vif, 0, WID_BSS_TYPE, &b, 1, 0, 0))
267		goto fail;
268
269	b = WILC_FW_TX_RATE_AUTO;
270	if (!wilc_wlan_cfg_set(vif, 0, WID_CURRENT_TX_RATE, &b, 1, 0, 0))
271		goto fail;
272
273	b = WILC_FW_OPER_MODE_G_MIXED_11B_2;
274	if (!wilc_wlan_cfg_set(vif, 0, WID_11G_OPERATING_MODE, &b, 1, 0, 0))
275		goto fail;
276
277	b = WILC_FW_PREAMBLE_SHORT;
278	if (!wilc_wlan_cfg_set(vif, 0, WID_PREAMBLE, &b, 1, 0, 0))
279		goto fail;
280
281	b = WILC_FW_11N_PROT_AUTO;
282	if (!wilc_wlan_cfg_set(vif, 0, WID_11N_PROT_MECH, &b, 1, 0, 0))
283		goto fail;
284
285	b = WILC_FW_ACTIVE_SCAN;
286	if (!wilc_wlan_cfg_set(vif, 0, WID_SCAN_TYPE, &b, 1, 0, 0))
287		goto fail;
288
289	b = WILC_FW_SITE_SURVEY_OFF;
290	if (!wilc_wlan_cfg_set(vif, 0, WID_SITE_SURVEY, &b, 1, 0, 0))
291		goto fail;
292
293	hw = 0xffff;
294	cpu_to_le16s(&hw);
295	if (!wilc_wlan_cfg_set(vif, 0, WID_RTS_THRESHOLD, (u8 *)&hw, 2, 0, 0))
296		goto fail;
297
298	hw = 2346;
299	cpu_to_le16s(&hw);
300	if (!wilc_wlan_cfg_set(vif, 0, WID_FRAG_THRESHOLD, (u8 *)&hw, 2, 0, 0))
301		goto fail;
302
303	b = 0;
304	if (!wilc_wlan_cfg_set(vif, 0, WID_BCAST_SSID, &b, 1, 0, 0))
305		goto fail;
306
307	b = 1;
308	if (!wilc_wlan_cfg_set(vif, 0, WID_QOS_ENABLE, &b, 1, 0, 0))
309		goto fail;
310
311	b = WILC_FW_NO_POWERSAVE;
312	if (!wilc_wlan_cfg_set(vif, 0, WID_POWER_MANAGEMENT, &b, 1, 0, 0))
313		goto fail;
314
315	b = WILC_FW_SEC_NO;
316	if (!wilc_wlan_cfg_set(vif, 0, WID_11I_MODE, &b, 1, 0, 0))
317		goto fail;
318
319	b = WILC_FW_AUTH_OPEN_SYSTEM;
320	if (!wilc_wlan_cfg_set(vif, 0, WID_AUTH_TYPE, &b, 1, 0, 0))
321		goto fail;
322
323	b = 3;
324	if (!wilc_wlan_cfg_set(vif, 0, WID_LISTEN_INTERVAL, &b, 1, 0, 0))
325		goto fail;
326
327	b = 3;
328	if (!wilc_wlan_cfg_set(vif, 0, WID_DTIM_PERIOD, &b, 1, 0, 0))
329		goto fail;
330
331	b = WILC_FW_ACK_POLICY_NORMAL;
332	if (!wilc_wlan_cfg_set(vif, 0, WID_ACK_POLICY, &b, 1, 0, 0))
333		goto fail;
334
335	b = 0;
336	if (!wilc_wlan_cfg_set(vif, 0, WID_USER_CONTROL_ON_TX_POWER, &b, 1,
337			       0, 0))
338		goto fail;
339
340	b = 48;
341	if (!wilc_wlan_cfg_set(vif, 0, WID_TX_POWER_LEVEL_11A, &b, 1, 0, 0))
342		goto fail;
343
344	b = 28;
345	if (!wilc_wlan_cfg_set(vif, 0, WID_TX_POWER_LEVEL_11B, &b, 1, 0, 0))
346		goto fail;
347
348	hw = 100;
349	cpu_to_le16s(&hw);
350	if (!wilc_wlan_cfg_set(vif, 0, WID_BEACON_INTERVAL, (u8 *)&hw, 2, 0, 0))
351		goto fail;
352
353	b = WILC_FW_REKEY_POLICY_DISABLE;
354	if (!wilc_wlan_cfg_set(vif, 0, WID_REKEY_POLICY, &b, 1, 0, 0))
355		goto fail;
356
357	w = 84600;
358	cpu_to_le32s(&w);
359	if (!wilc_wlan_cfg_set(vif, 0, WID_REKEY_PERIOD, (u8 *)&w, 4, 0, 0))
360		goto fail;
361
362	w = 500;
363	cpu_to_le32s(&w);
364	if (!wilc_wlan_cfg_set(vif, 0, WID_REKEY_PACKET_COUNT, (u8 *)&w, 4, 0,
365			       0))
366		goto fail;
367
368	b = 1;
369	if (!wilc_wlan_cfg_set(vif, 0, WID_SHORT_SLOT_ALLOWED, &b, 1, 0,
370			       0))
371		goto fail;
372
373	b = WILC_FW_ERP_PROT_SELF_CTS;
374	if (!wilc_wlan_cfg_set(vif, 0, WID_11N_ERP_PROT_TYPE, &b, 1, 0, 0))
375		goto fail;
376
377	b = 1;
378	if (!wilc_wlan_cfg_set(vif, 0, WID_11N_ENABLE, &b, 1, 0, 0))
379		goto fail;
380
381	b = WILC_FW_11N_OP_MODE_HT_MIXED;
382	if (!wilc_wlan_cfg_set(vif, 0, WID_11N_OPERATING_MODE, &b, 1, 0, 0))
383		goto fail;
384
385	b = 1;
386	if (!wilc_wlan_cfg_set(vif, 0, WID_11N_TXOP_PROT_DISABLE, &b, 1, 0, 0))
387		goto fail;
388
389	b = WILC_FW_OBBS_NONHT_DETECT_PROTECT_REPORT;
390	if (!wilc_wlan_cfg_set(vif, 0, WID_11N_OBSS_NONHT_DETECTION, &b, 1,
391			       0, 0))
392		goto fail;
393
394	b = WILC_FW_HT_PROT_RTS_CTS_NONHT;
395	if (!wilc_wlan_cfg_set(vif, 0, WID_11N_HT_PROT_TYPE, &b, 1, 0, 0))
396		goto fail;
397
398	b = 0;
399	if (!wilc_wlan_cfg_set(vif, 0, WID_11N_RIFS_PROT_ENABLE, &b, 1, 0,
400			       0))
401		goto fail;
402
403	b = 7;
404	if (!wilc_wlan_cfg_set(vif, 0, WID_11N_CURRENT_TX_MCS, &b, 1, 0, 0))
405		goto fail;
406
407	b = 1;
408	if (!wilc_wlan_cfg_set(vif, 0, WID_11N_IMMEDIATE_BA_ENABLED, &b, 1,
409			       1, 1))
410		goto fail;
411
412	return 0;
413
414fail:
415	return -EINVAL;
416}
417
418static void wlan_deinitialize_threads(struct net_device *dev)
419{
420	struct wilc_vif *vif = netdev_priv(dev);
421	struct wilc *wl = vif->wilc;
422
423	wl->close = 1;
424
425	complete(&wl->txq_event);
426
427	if (wl->txq_thread) {
428		kthread_stop(wl->txq_thread);
429		wl->txq_thread = NULL;
430	}
431}
432
433static void wilc_wlan_deinitialize(struct net_device *dev)
434{
435	struct wilc_vif *vif = netdev_priv(dev);
436	struct wilc *wl = vif->wilc;
437
438	if (!wl) {
439		netdev_err(dev, "wl is NULL\n");
440		return;
441	}
442
443	if (wl->initialized) {
444		netdev_info(dev, "Deinitializing wilc1000...\n");
445
446		if (!wl->dev_irq_num &&
447		    wl->hif_func->disable_interrupt) {
448			mutex_lock(&wl->hif_cs);
449			wl->hif_func->disable_interrupt(wl);
450			mutex_unlock(&wl->hif_cs);
451		}
452		complete(&wl->txq_event);
453
454		wlan_deinitialize_threads(dev);
455		deinit_irq(dev);
456
457		wilc_wlan_stop(wl, vif);
458		wilc_wlan_cleanup(dev);
459
460		wl->initialized = false;
461
462		netdev_dbg(dev, "wilc1000 deinitialization Done\n");
463	} else {
464		netdev_dbg(dev, "wilc1000 is not initialized\n");
465	}
466}
467
468static int wlan_initialize_threads(struct net_device *dev)
469{
470	struct wilc_vif *vif = netdev_priv(dev);
471	struct wilc *wilc = vif->wilc;
472
473	wilc->txq_thread = kthread_run(wilc_txq_task, (void *)wilc,
474				       "K_TXQ_TASK");
475	if (IS_ERR(wilc->txq_thread)) {
476		netdev_err(dev, "couldn't create TXQ thread\n");
477		wilc->close = 0;
478		return PTR_ERR(wilc->txq_thread);
479	}
480	wait_for_completion(&wilc->txq_thread_started);
481
482	return 0;
483}
484
485static int wilc_wlan_initialize(struct net_device *dev, struct wilc_vif *vif)
486{
487	int ret = 0;
488	struct wilc *wl = vif->wilc;
489
490	if (!wl->initialized) {
491		wl->mac_status = WILC_MAC_STATUS_INIT;
492		wl->close = 0;
493
494		ret = wilc_wlan_init(dev);
495		if (ret)
496			return ret;
497
498		ret = wlan_initialize_threads(dev);
499		if (ret)
500			goto fail_wilc_wlan;
501
502		if (wl->dev_irq_num && init_irq(dev)) {
503			ret = -EIO;
504			goto fail_threads;
505		}
506
507		if (!wl->dev_irq_num &&
508		    wl->hif_func->enable_interrupt &&
509		    wl->hif_func->enable_interrupt(wl)) {
510			ret = -EIO;
511			goto fail_irq_init;
512		}
513
514		ret = wilc_wlan_get_firmware(dev);
515		if (ret)
516			goto fail_irq_enable;
517
518		ret = wilc1000_firmware_download(dev);
519		if (ret)
520			goto fail_irq_enable;
521
522		ret = wilc_start_firmware(dev);
523		if (ret)
524			goto fail_irq_enable;
525
526		if (wilc_wlan_cfg_get(vif, 1, WID_FIRMWARE_VERSION, 1, 0)) {
527			int size;
528			char firmware_ver[20];
529
530			size = wilc_wlan_cfg_get_val(wl, WID_FIRMWARE_VERSION,
531						     firmware_ver,
532						     sizeof(firmware_ver));
533			firmware_ver[size] = '\0';
534			netdev_dbg(dev, "Firmware Ver = %s\n", firmware_ver);
535		}
536
537		ret = wilc_init_fw_config(dev, vif);
538		if (ret) {
539			netdev_err(dev, "Failed to configure firmware\n");
540			goto fail_fw_start;
541		}
542		wl->initialized = true;
543		return 0;
544
545fail_fw_start:
546		wilc_wlan_stop(wl, vif);
547
548fail_irq_enable:
549		if (!wl->dev_irq_num &&
550		    wl->hif_func->disable_interrupt)
551			wl->hif_func->disable_interrupt(wl);
552fail_irq_init:
553		if (wl->dev_irq_num)
554			deinit_irq(dev);
555fail_threads:
556		wlan_deinitialize_threads(dev);
557fail_wilc_wlan:
558		wilc_wlan_cleanup(dev);
559		netdev_err(dev, "WLAN initialization FAILED\n");
560	} else {
561		netdev_dbg(dev, "wilc1000 already initialized\n");
562	}
563	return ret;
564}
565
566static int mac_init_fn(struct net_device *ndev)
567{
568	netif_start_queue(ndev);
569	netif_stop_queue(ndev);
570
571	return 0;
572}
573
574static int wilc_mac_open(struct net_device *ndev)
575{
576	struct wilc_vif *vif = netdev_priv(ndev);
577	struct wilc *wl = vif->wilc;
578	unsigned char mac_add[ETH_ALEN] = {0};
579	int ret = 0;
580	struct mgmt_frame_regs mgmt_regs = {};
 
581
582	if (!wl || !wl->dev) {
583		netdev_err(ndev, "device not ready\n");
584		return -ENODEV;
585	}
586
587	netdev_dbg(ndev, "MAC OPEN[%p]\n", ndev);
588
589	ret = wilc_init_host_int(ndev);
590	if (ret)
591		return ret;
592
593	ret = wilc_wlan_initialize(ndev, vif);
594	if (ret) {
595		wilc_deinit_host_int(ndev);
596		return ret;
597	}
598
599	wilc_set_operation_mode(vif, wilc_get_vif_idx(vif), vif->iftype,
600				vif->idx);
601	wilc_get_mac_address(vif, mac_add);
602	netdev_dbg(ndev, "Mac address: %pM\n", mac_add);
603	ether_addr_copy(ndev->dev_addr, mac_add);
 
 
 
 
 
 
604
605	if (!is_valid_ether_addr(ndev->dev_addr)) {
606		netdev_err(ndev, "Wrong MAC address\n");
607		wilc_deinit_host_int(ndev);
608		wilc_wlan_deinitialize(ndev);
609		return -EINVAL;
610	}
611
612	mgmt_regs.interface_stypes = vif->mgmt_reg_stypes;
613	/* so we detect a change */
614	vif->mgmt_reg_stypes = 0;
615	wilc_update_mgmt_frame_registrations(vif->ndev->ieee80211_ptr->wiphy,
616					     vif->ndev->ieee80211_ptr,
617					     &mgmt_regs);
618	netif_wake_queue(ndev);
619	wl->open_ifcs++;
620	vif->mac_opened = 1;
621	return 0;
622}
623
624static struct net_device_stats *mac_stats(struct net_device *dev)
625{
626	struct wilc_vif *vif = netdev_priv(dev);
627
628	return &vif->netstats;
629}
630
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
631static void wilc_set_multicast_list(struct net_device *dev)
632{
633	struct netdev_hw_addr *ha;
634	struct wilc_vif *vif = netdev_priv(dev);
635	int i;
636	u8 *mc_list;
637	u8 *cur_mc;
638
639	if (dev->flags & IFF_PROMISC)
640		return;
641
642	if (dev->flags & IFF_ALLMULTI ||
643	    dev->mc.count > WILC_MULTICAST_TABLE_SIZE) {
644		wilc_setup_multicast_filter(vif, 0, 0, NULL);
645		return;
646	}
647
648	if (dev->mc.count == 0) {
649		wilc_setup_multicast_filter(vif, 1, 0, NULL);
650		return;
651	}
652
653	mc_list = kmalloc_array(dev->mc.count, ETH_ALEN, GFP_ATOMIC);
654	if (!mc_list)
655		return;
656
657	cur_mc = mc_list;
658	i = 0;
659	netdev_for_each_mc_addr(ha, dev) {
660		memcpy(cur_mc, ha->addr, ETH_ALEN);
661		netdev_dbg(dev, "Entry[%d]: %pM\n", i, cur_mc);
662		i++;
663		cur_mc += ETH_ALEN;
664	}
665
666	if (wilc_setup_multicast_filter(vif, 1, dev->mc.count, mc_list))
667		kfree(mc_list);
668}
669
670static void wilc_tx_complete(void *priv, int status)
671{
672	struct tx_complete_data *pv_data = priv;
673
674	dev_kfree_skb(pv_data->skb);
675	kfree(pv_data);
676}
677
678netdev_tx_t wilc_mac_xmit(struct sk_buff *skb, struct net_device *ndev)
679{
680	struct wilc_vif *vif = netdev_priv(ndev);
681	struct wilc *wilc = vif->wilc;
682	struct tx_complete_data *tx_data = NULL;
683	int queue_count;
684
685	if (skb->dev != ndev) {
686		netdev_err(ndev, "Packet not destined to this device\n");
 
687		return NETDEV_TX_OK;
688	}
689
690	tx_data = kmalloc(sizeof(*tx_data), GFP_ATOMIC);
691	if (!tx_data) {
692		dev_kfree_skb(skb);
693		netif_wake_queue(ndev);
694		return NETDEV_TX_OK;
695	}
696
697	tx_data->buff = skb->data;
698	tx_data->size = skb->len;
699	tx_data->skb  = skb;
700
701	vif->netstats.tx_packets++;
702	vif->netstats.tx_bytes += tx_data->size;
703	queue_count = wilc_wlan_txq_add_net_pkt(ndev, (void *)tx_data,
704						tx_data->buff, tx_data->size,
705						wilc_tx_complete);
706
707	if (queue_count > FLOW_CONTROL_UPPER_THRESHOLD) {
708		int srcu_idx;
709		struct wilc_vif *vif;
710
711		srcu_idx = srcu_read_lock(&wilc->srcu);
712		list_for_each_entry_rcu(vif, &wilc->vif_list, list) {
713			if (vif->mac_opened)
714				netif_stop_queue(vif->ndev);
715		}
716		srcu_read_unlock(&wilc->srcu, srcu_idx);
717	}
718
719	return NETDEV_TX_OK;
720}
721
722static int wilc_mac_close(struct net_device *ndev)
723{
724	struct wilc_vif *vif = netdev_priv(ndev);
725	struct wilc *wl = vif->wilc;
726
727	netdev_dbg(ndev, "Mac close\n");
728
729	if (wl->open_ifcs > 0)
730		wl->open_ifcs--;
731	else
732		return 0;
733
734	if (vif->ndev) {
735		netif_stop_queue(vif->ndev);
736
 
737		wilc_deinit_host_int(vif->ndev);
738	}
739
740	if (wl->open_ifcs == 0) {
741		netdev_dbg(ndev, "Deinitializing wilc1000\n");
742		wl->close = 1;
743		wilc_wlan_deinitialize(ndev);
744	}
745
746	vif->mac_opened = 0;
747
748	return 0;
749}
750
751void wilc_frmw_to_host(struct wilc *wilc, u8 *buff, u32 size,
752		       u32 pkt_offset)
753{
754	unsigned int frame_len = 0;
755	int stats;
756	unsigned char *buff_to_send = NULL;
757	struct sk_buff *skb;
758	struct net_device *wilc_netdev;
759	struct wilc_vif *vif;
760
761	if (!wilc)
762		return;
763
764	wilc_netdev = get_if_handler(wilc, buff);
765	if (!wilc_netdev)
766		return;
767
768	buff += pkt_offset;
769	vif = netdev_priv(wilc_netdev);
770
771	if (size > 0) {
772		frame_len = size;
773		buff_to_send = buff;
774
775		skb = dev_alloc_skb(frame_len);
776		if (!skb)
777			return;
778
779		skb->dev = wilc_netdev;
780
781		skb_put_data(skb, buff_to_send, frame_len);
782
783		skb->protocol = eth_type_trans(skb, wilc_netdev);
784		vif->netstats.rx_packets++;
785		vif->netstats.rx_bytes += frame_len;
786		skb->ip_summed = CHECKSUM_UNNECESSARY;
787		stats = netif_rx(skb);
788		netdev_dbg(wilc_netdev, "netif_rx ret value is: %d\n", stats);
789	}
790}
791
792void wilc_wfi_mgmt_rx(struct wilc *wilc, u8 *buff, u32 size)
793{
794	int srcu_idx;
795	struct wilc_vif *vif;
796
797	srcu_idx = srcu_read_lock(&wilc->srcu);
798	list_for_each_entry_rcu(vif, &wilc->vif_list, list) {
 
799		u16 type = le16_to_cpup((__le16 *)buff);
800		u32 type_bit = BIT(type >> 4);
 
 
 
 
 
 
 
 
801
802		if (vif->priv.p2p_listen_state &&
803		    vif->mgmt_reg_stypes & type_bit)
804			wilc_wfi_p2p_rx(vif, buff, size);
805
806		if (vif->monitor_flag)
807			wilc_wfi_monitor_rx(wilc->monitor_dev, buff, size);
808	}
809	srcu_read_unlock(&wilc->srcu, srcu_idx);
810}
811
812static const struct net_device_ops wilc_netdev_ops = {
813	.ndo_init = mac_init_fn,
814	.ndo_open = wilc_mac_open,
815	.ndo_stop = wilc_mac_close,
 
816	.ndo_start_xmit = wilc_mac_xmit,
817	.ndo_get_stats = mac_stats,
818	.ndo_set_rx_mode  = wilc_set_multicast_list,
819};
820
821void wilc_netdev_cleanup(struct wilc *wilc)
822{
823	struct wilc_vif *vif;
824	int srcu_idx, ifc_cnt = 0;
825
826	if (!wilc)
827		return;
828
829	if (wilc->firmware) {
830		release_firmware(wilc->firmware);
831		wilc->firmware = NULL;
832	}
833
834	srcu_idx = srcu_read_lock(&wilc->srcu);
835	list_for_each_entry_rcu(vif, &wilc->vif_list, list) {
836		if (vif->ndev)
837			unregister_netdev(vif->ndev);
838	}
839	srcu_read_unlock(&wilc->srcu, srcu_idx);
840
841	wilc_wfi_deinit_mon_interface(wilc, false);
842	flush_workqueue(wilc->hif_workqueue);
843	destroy_workqueue(wilc->hif_workqueue);
844
845	while (ifc_cnt < WILC_NUM_CONCURRENT_IFC) {
846		mutex_lock(&wilc->vif_mutex);
847		if (wilc->vif_num <= 0) {
848			mutex_unlock(&wilc->vif_mutex);
849			break;
850		}
851		vif = wilc_get_wl_to_vif(wilc);
852		if (!IS_ERR(vif))
853			list_del_rcu(&vif->list);
854
855		wilc->vif_num--;
856		mutex_unlock(&wilc->vif_mutex);
857		synchronize_srcu(&wilc->srcu);
858		ifc_cnt++;
859	}
860
861	wilc_wlan_cfg_deinit(wilc);
862	wlan_deinit_locks(wilc);
863	kfree(wilc->bus_data);
864	wiphy_unregister(wilc->wiphy);
865	wiphy_free(wilc->wiphy);
866}
867EXPORT_SYMBOL_GPL(wilc_netdev_cleanup);
868
869static u8 wilc_get_available_idx(struct wilc *wl)
870{
871	int idx = 0;
872	struct wilc_vif *vif;
873	int srcu_idx;
874
875	srcu_idx = srcu_read_lock(&wl->srcu);
876	list_for_each_entry_rcu(vif, &wl->vif_list, list) {
877		if (vif->idx == 0)
878			idx = 1;
879		else
880			idx = 0;
881	}
882	srcu_read_unlock(&wl->srcu, srcu_idx);
883	return idx;
884}
885
886struct wilc_vif *wilc_netdev_ifc_init(struct wilc *wl, const char *name,
887				      int vif_type, enum nl80211_iftype type,
888				      bool rtnl_locked)
889{
890	struct net_device *ndev;
891	struct wilc_vif *vif;
892	int ret;
893
894	ndev = alloc_etherdev(sizeof(*vif));
895	if (!ndev)
896		return ERR_PTR(-ENOMEM);
897
898	vif = netdev_priv(ndev);
899	ndev->ieee80211_ptr = &vif->priv.wdev;
900	strcpy(ndev->name, name);
901	vif->wilc = wl;
902	vif->ndev = ndev;
903	ndev->ml_priv = vif;
904
905	ndev->netdev_ops = &wilc_netdev_ops;
906
907	SET_NETDEV_DEV(ndev, wiphy_dev(wl->wiphy));
908
909	vif->priv.wdev.wiphy = wl->wiphy;
910	vif->priv.wdev.netdev = ndev;
911	vif->priv.wdev.iftype = type;
912	vif->priv.dev = ndev;
913
914	if (rtnl_locked)
915		ret = register_netdevice(ndev);
916	else
917		ret = register_netdev(ndev);
918
919	if (ret) {
920		free_netdev(ndev);
921		return ERR_PTR(-EFAULT);
 
 
 
 
 
 
 
922	}
923
924	ndev->needs_free_netdev = true;
925	vif->iftype = vif_type;
926	vif->idx = wilc_get_available_idx(wl);
927	vif->mac_opened = 0;
928	mutex_lock(&wl->vif_mutex);
929	list_add_tail_rcu(&vif->list, &wl->vif_list);
930	wl->vif_num += 1;
931	mutex_unlock(&wl->vif_mutex);
932	synchronize_srcu(&wl->srcu);
933
934	return vif;
 
 
 
 
 
 
 
 
 
935}
936
 
937MODULE_LICENSE("GPL");
938MODULE_FIRMWARE(WILC1000_FW(WILC1000_API_VER));