main.c 21.5 KB
Newer Older
1
/*
2
 * Copyright (c) 2012-2014 Qualcomm Atheros, Inc.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <linux/moduleparam.h>
#include <linux/if_arp.h>
19
#include <linux/etherdevice.h>
20 21

#include "wil6210.h"
V
Vladimir Kondratiev 已提交
22
#include "txrx.h"
23 24 25 26
#include "wmi.h"

#define WAIT_FOR_DISCONNECT_TIMEOUT_MS 2000
#define WAIT_FOR_DISCONNECT_INTERVAL_MS 10
27

28
bool no_fw_recovery;
V
Vladimir Kondratiev 已提交
29
module_param(no_fw_recovery, bool, S_IRUGO | S_IWUSR);
30
MODULE_PARM_DESC(no_fw_recovery, " disable automatic FW error recovery");
V
Vladimir Kondratiev 已提交
31

V
Vladimir Kondratiev 已提交
32 33 34 35
static bool no_fw_load = true;
module_param(no_fw_load, bool, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(no_fw_load, " do not download FW, use one in on-card flash.");

V
Vladimir Kondratiev 已提交
36 37 38 39 40
static unsigned int itr_trsh = WIL6210_ITR_TRSH_DEFAULT;

module_param(itr_trsh, uint, S_IRUGO);
MODULE_PARM_DESC(itr_trsh, " Interrupt moderation threshold, usecs.");

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
/* We allow allocation of more than 1 page buffers to support large packets.
 * It is suboptimal behavior performance wise in case MTU above page size.
 */
unsigned int mtu_max = TXRX_BUF_LEN_DEFAULT - ETH_HLEN;
static int mtu_max_set(const char *val, const struct kernel_param *kp)
{
	int ret;

	/* sets mtu_max directly. no need to restore it in case of
	 * illegal value since we assume this will fail insmod
	 */
	ret = param_set_uint(val, kp);
	if (ret)
		return ret;

	if (mtu_max < 68 || mtu_max > IEEE80211_MAX_DATA_LEN_DMG)
		ret = -EINVAL;

	return ret;
}

static struct kernel_param_ops mtu_max_ops = {
	.set = mtu_max_set,
	.get = param_get_uint,
};

module_param_cb(mtu_max, &mtu_max_ops, &mtu_max, S_IRUGO);
MODULE_PARM_DESC(mtu_max, " Max MTU value.");

70 71 72
#define RST_DELAY (20) /* msec, for loop in @wil_target_reset */
#define RST_COUNT (1 + 1000/RST_DELAY) /* round up to be above 1 sec total */

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
/*
 * Due to a hardware issue,
 * one has to read/write to/from NIC in 32-bit chunks;
 * regular memcpy_fromio and siblings will
 * not work on 64-bit platform - it uses 64-bit transactions
 *
 * Force 32-bit transactions to enable NIC on 64-bit platforms
 *
 * To avoid byte swap on big endian host, __raw_{read|write}l
 * should be used - {read|write}l would swap bytes to provide
 * little endian on PCI value in host endianness.
 */
void wil_memcpy_fromio_32(void *dst, const volatile void __iomem *src,
			  size_t count)
{
	u32 *d = dst;
	const volatile u32 __iomem *s = src;

	/* size_t is unsigned, if (count%4 != 0) it will wrap */
	for (count += 4; count > 4; count -= 4)
		*d++ = __raw_readl(s++);
}

void wil_memcpy_toio_32(volatile void __iomem *dst, const void *src,
			size_t count)
{
	volatile u32 __iomem *d = dst;
	const u32 *s = src;

	for (count += 4; count > 4; count -= 4)
		__raw_writel(*s++, d++);
}

106
static void wil_disconnect_cid(struct wil6210_priv *wil, int cid,
107
			       u16 reason_code, bool from_event)
108 109
{
	uint i;
110 111
	struct net_device *ndev = wil_to_ndev(wil);
	struct wireless_dev *wdev = wil->wdev;
112
	struct wil_sta_info *sta = &wil->sta[cid];
113

114 115
	wil_dbg_misc(wil, "%s(CID %d, status %d)\n", __func__, cid,
		     sta->status);
116

117
	sta->data_port_open = false;
118
	if (sta->status != wil_sta_unused) {
119
		if (!from_event)
120
			wmi_disconnect_sta(wil, sta->addr, reason_code);
121

122 123 124 125 126 127 128 129 130
		switch (wdev->iftype) {
		case NL80211_IFTYPE_AP:
		case NL80211_IFTYPE_P2P_GO:
			/* AP-like interface */
			cfg80211_del_sta(ndev, sta->addr, GFP_KERNEL);
			break;
		default:
			break;
		}
131 132 133
		sta->status = wil_sta_unused;
	}

134
	for (i = 0; i < WIL_STA_TID_NUM; i++) {
135 136 137 138 139 140
		struct wil_tid_ampdu_rx *r;
		unsigned long flags;

		spin_lock_irqsave(&sta->tid_rx_lock, flags);

		r = sta->tid_rx[i];
141 142
		sta->tid_rx[i] = NULL;
		wil_tid_ampdu_rx_free(wil, r);
143 144

		spin_unlock_irqrestore(&sta->tid_rx_lock, flags);
145 146 147 148 149 150 151 152
	}
	for (i = 0; i < ARRAY_SIZE(wil->vring_tx); i++) {
		if (wil->vring2cid_tid[i][0] == cid)
			wil_vring_fini_tx(wil, i);
	}
	memset(&sta->stats, 0, sizeof(sta->stats));
}

153
static void _wil6210_disconnect(struct wil6210_priv *wil, const u8 *bssid,
154
				u16 reason_code, bool from_event)
155
{
156
	int cid = -ENOENT;
157
	struct net_device *ndev = wil_to_ndev(wil);
158 159 160 161 162 163 164 165
	struct wireless_dev *wdev = wil->wdev;

	might_sleep();
	if (bssid) {
		cid = wil_find_cid(wil, bssid);
		wil_dbg_misc(wil, "%s(%pM, CID %d)\n", __func__, bssid, cid);
	} else {
		wil_dbg_misc(wil, "%s(all)\n", __func__);
V
Vladimir Kondratiev 已提交
166 167
	}

168
	if (cid >= 0) /* disconnect 1 peer */
169
		wil_disconnect_cid(wil, cid, reason_code, from_event);
170 171
	else /* disconnect all */
		for (cid = 0; cid < WIL6210_MAX_CID; cid++)
172
			wil_disconnect_cid(wil, cid, reason_code, from_event);
173 174 175 176 177 178 179 180

	/* link state */
	switch (wdev->iftype) {
	case NL80211_IFTYPE_STATION:
	case NL80211_IFTYPE_P2P_CLIENT:
		wil_link_off(wil);
		if (test_bit(wil_status_fwconnected, &wil->status)) {
			clear_bit(wil_status_fwconnected, &wil->status);
181
			cfg80211_disconnected(ndev, reason_code,
182 183 184 185 186 187 188 189 190 191
					      NULL, 0, GFP_KERNEL);
		} else if (test_bit(wil_status_fwconnecting, &wil->status)) {
			cfg80211_connect_result(ndev, bssid, NULL, 0, NULL, 0,
						WLAN_STATUS_UNSPECIFIED_FAILURE,
						GFP_KERNEL);
		}
		clear_bit(wil_status_fwconnecting, &wil->status);
		break;
	default:
		break;
192 193 194 195 196 197 198 199
	}
}

static void wil_disconnect_worker(struct work_struct *work)
{
	struct wil6210_priv *wil = container_of(work,
			struct wil6210_priv, disconnect_worker);

200
	mutex_lock(&wil->mutex);
201
	_wil6210_disconnect(wil, NULL, WLAN_REASON_UNSPECIFIED, false);
202
	mutex_unlock(&wil->mutex);
203 204 205 206 207 208
}

static void wil_connect_timer_fn(ulong x)
{
	struct wil6210_priv *wil = (void *)x;

209
	wil_dbg_misc(wil, "Connect timeout\n");
210 211 212 213 214 215 216

	/* reschedule to thread context - disconnect won't
	 * run from atomic context
	 */
	schedule_work(&wil->disconnect_worker);
}

217 218 219 220 221 222 223 224 225
static void wil_scan_timer_fn(ulong x)
{
	struct wil6210_priv *wil = (void *)x;

	clear_bit(wil_status_fwready, &wil->status);
	wil_err(wil, "Scan timeout detected, start fw error recovery\n");
	schedule_work(&wil->fw_error_worker);
}

226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
static int wil_wait_for_recovery(struct wil6210_priv *wil)
{
	if (wait_event_interruptible(wil->wq, wil->recovery_state !=
				     fw_recovery_pending)) {
		wil_err(wil, "Interrupt, canceling recovery\n");
		return -ERESTARTSYS;
	}
	if (wil->recovery_state != fw_recovery_running) {
		wil_info(wil, "Recovery cancelled\n");
		return -EINTR;
	}
	wil_info(wil, "Proceed with recovery\n");
	return 0;
}

void wil_set_recovery_state(struct wil6210_priv *wil, int state)
{
	wil_dbg_misc(wil, "%s(%d -> %d)\n", __func__,
		     wil->recovery_state, state);

	wil->recovery_state = state;
	wake_up_interruptible(&wil->wq);
}

V
Vladimir Kondratiev 已提交
250 251
static void wil_fw_error_worker(struct work_struct *work)
{
252 253
	struct wil6210_priv *wil = container_of(work, struct wil6210_priv,
						fw_error_worker);
V
Vladimir Kondratiev 已提交
254 255 256 257
	struct wireless_dev *wdev = wil->wdev;

	wil_dbg_misc(wil, "fw error worker\n");

258 259 260 261 262
	if (!netif_running(wil_to_ndev(wil))) {
		wil_info(wil, "No recovery - interface is down\n");
		return;
	}

263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
	/* increment @recovery_count if less then WIL6210_FW_RECOVERY_TO
	 * passed since last recovery attempt
	 */
	if (time_is_after_jiffies(wil->last_fw_recovery +
				  WIL6210_FW_RECOVERY_TO))
		wil->recovery_count++;
	else
		wil->recovery_count = 1; /* fw was alive for a long time */

	if (wil->recovery_count > WIL6210_FW_RECOVERY_RETRIES) {
		wil_err(wil, "too many recovery attempts (%d), giving up\n",
			wil->recovery_count);
		return;
	}

	wil->last_fw_recovery = jiffies;

280
	mutex_lock(&wil->mutex);
V
Vladimir Kondratiev 已提交
281 282 283 284
	switch (wdev->iftype) {
	case NL80211_IFTYPE_STATION:
	case NL80211_IFTYPE_P2P_CLIENT:
	case NL80211_IFTYPE_MONITOR:
285
		wil_info(wil, "fw error recovery requested (try %d)...\n",
286
			 wil->recovery_count);
287 288 289 290 291
		if (!no_fw_recovery)
			wil->recovery_state = fw_recovery_running;
		if (0 != wil_wait_for_recovery(wil))
			break;

292 293
		__wil_down(wil);
		__wil_up(wil);
V
Vladimir Kondratiev 已提交
294 295 296
		break;
	case NL80211_IFTYPE_AP:
	case NL80211_IFTYPE_P2P_GO:
297
		wil_info(wil, "No recovery for AP-like interface\n");
V
Vladimir Kondratiev 已提交
298 299 300
		/* recovery in these modes is done by upper layers */
		break;
	default:
301 302
		wil_err(wil, "No recovery - unknown interface type %d\n",
			wdev->iftype);
V
Vladimir Kondratiev 已提交
303 304
		break;
	}
305
	mutex_unlock(&wil->mutex);
V
Vladimir Kondratiev 已提交
306 307
}

308 309 310
static int wil_find_free_vring(struct wil6210_priv *wil)
{
	int i;
311

312 313 314 315 316 317 318
	for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
		if (!wil->vring_tx[i].va)
			return i;
	}
	return -EINVAL;
}

319 320 321 322 323 324
static void wil_connect_worker(struct work_struct *work)
{
	int rc;
	struct wil6210_priv *wil = container_of(work, struct wil6210_priv,
						connect_worker);
	int cid = wil->pending_connect_cid;
325
	int ringid = wil_find_free_vring(wil);
326 327 328 329 330 331 332 333

	if (cid < 0) {
		wil_err(wil, "No connection pending\n");
		return;
	}

	wil_dbg_wmi(wil, "Configure for connection CID %d\n", cid);

334
	rc = wil_vring_init_tx(wil, ringid, WIL6210_TX_RING_SIZE, cid, 0);
335
	wil->pending_connect_cid = -1;
336 337
	if (rc == 0) {
		wil->sta[cid].status = wil_sta_connected;
338
		wil_link_on(wil);
339 340 341
	} else {
		wil->sta[cid].status = wil_sta_unused;
	}
342 343
}

344 345
int wil_priv_init(struct wil6210_priv *wil)
{
346 347
	uint i;

348
	wil_dbg_misc(wil, "%s()\n", __func__);
349

350
	memset(wil->sta, 0, sizeof(wil->sta));
351 352
	for (i = 0; i < WIL6210_MAX_CID; i++)
		spin_lock_init(&wil->sta[i].tid_rx_lock);
353

354 355 356 357
	mutex_init(&wil->mutex);
	mutex_init(&wil->wmi_mutex);

	init_completion(&wil->wmi_ready);
358
	init_completion(&wil->wmi_call);
359 360 361

	wil->pending_connect_cid = -1;
	setup_timer(&wil->connect_timer, wil_connect_timer_fn, (ulong)wil);
362
	setup_timer(&wil->scan_timer, wil_scan_timer_fn, (ulong)wil);
363

364
	INIT_WORK(&wil->connect_worker, wil_connect_worker);
365 366
	INIT_WORK(&wil->disconnect_worker, wil_disconnect_worker);
	INIT_WORK(&wil->wmi_event_worker, wmi_event_worker);
V
Vladimir Kondratiev 已提交
367
	INIT_WORK(&wil->fw_error_worker, wil_fw_error_worker);
368 369 370

	INIT_LIST_HEAD(&wil->pending_wmi_ev);
	spin_lock_init(&wil->wmi_ev_lock);
371
	init_waitqueue_head(&wil->wq);
372 373 374 375 376 377 378 379 380 381 382

	wil->wmi_wq = create_singlethread_workqueue(WIL_NAME"_wmi");
	if (!wil->wmi_wq)
		return -EAGAIN;

	wil->wmi_wq_conn = create_singlethread_workqueue(WIL_NAME"_connect");
	if (!wil->wmi_wq_conn) {
		destroy_workqueue(wil->wmi_wq);
		return -EAGAIN;
	}

383
	wil->last_fw_recovery = jiffies;
V
Vladimir Kondratiev 已提交
384
	wil->itr_trsh = itr_trsh;
385

386 387 388
	return 0;
}

389 390 391 392
/**
 * wil6210_disconnect - disconnect one connection
 * @wil: driver context
 * @bssid: peer to disconnect, NULL to disconnect all
393
 * @reason_code: Reason code for the Disassociation frame
394 395 396 397 398 399
 * @from_event: whether is invoked from FW event handler
 *
 * Disconnect and release associated resources. If invoked not from the
 * FW event handler, issue WMI command(s) to trigger MAC disconnect.
 */
void wil6210_disconnect(struct wil6210_priv *wil, const u8 *bssid,
400
			u16 reason_code, bool from_event)
401
{
402 403
	wil_dbg_misc(wil, "%s()\n", __func__);

404
	del_timer_sync(&wil->connect_timer);
405
	_wil6210_disconnect(wil, bssid, reason_code, from_event);
406 407 408 409
}

void wil_priv_deinit(struct wil6210_priv *wil)
{
410 411
	wil_dbg_misc(wil, "%s()\n", __func__);

412
	wil_set_recovery_state(wil, fw_recovery_idle);
413
	del_timer_sync(&wil->scan_timer);
414
	cancel_work_sync(&wil->disconnect_worker);
V
Vladimir Kondratiev 已提交
415
	cancel_work_sync(&wil->fw_error_worker);
416
	mutex_lock(&wil->mutex);
417
	wil6210_disconnect(wil, NULL, WLAN_REASON_DEAUTH_LEAVING, false);
418
	mutex_unlock(&wil->mutex);
419 420 421 422 423
	wmi_event_flush(wil);
	destroy_workqueue(wil->wmi_wq_conn);
	destroy_workqueue(wil->wmi_wq);
}

V
Vladimir Kondratiev 已提交
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
/* target operations */
/* register read */
#define R(a) ioread32(wil->csr + HOSTADDR(a))
/* register write. wmb() to make sure it is completed */
#define W(a, v) do { iowrite32(v, wil->csr + HOSTADDR(a)); wmb(); } while (0)
/* register set = read, OR, write */
#define S(a, v) W(a, R(a) | v)
/* register clear = read, AND with inverted, write */
#define C(a, v) W(a, R(a) & ~v)

static inline void wil_halt_cpu(struct wil6210_priv *wil)
{
	W(RGF_USER_USER_CPU_0, BIT_USER_USER_CPU_MAN_RST);
	W(RGF_USER_MAC_CPU_0,  BIT_USER_MAC_CPU_MAN_RST);
}

static inline void wil_release_cpu(struct wil6210_priv *wil)
{
	/* Start CPU */
	W(RGF_USER_USER_CPU_0, 1);
}

446
static int wil_target_reset(struct wil6210_priv *wil)
447
{
V
Vladimir Kondratiev 已提交
448
	int delay = 0;
V
Vladimir Kondratiev 已提交
449
	u32 x;
450
	u32 rev_id;
451
	bool is_sparrow = (wil->board->board == WIL_BOARD_SPARROW);
452

453
	wil_dbg_misc(wil, "Resetting \"%s\"...\n", wil->board->name);
454

455
	wil->hw_version = R(RGF_USER_FW_REV_ID);
456
	rev_id = wil->hw_version & 0xff;
457 458 459

	/* Clear MAC link up */
	S(RGF_HP_CTRL, BIT(15));
V
Vladimir Kondratiev 已提交
460 461 462 463
	S(RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT_HPAL_PERST_FROM_PAD);
	S(RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT_CAR_PERST_RST);

	wil_halt_cpu(wil);
464

465
	if (is_sparrow) {
V
Vladimir Kondratiev 已提交
466 467 468 469 470 471 472 473 474 475 476 477 478 479
		S(RGF_CAF_OSC_CONTROL, BIT_CAF_OSC_XTAL_EN);
		/* XTAL stabilization should take about 3ms */
		usleep_range(5000, 7000);
		x = R(RGF_CAF_PLL_LOCK_STATUS);
		if (!(x & BIT_CAF_OSC_DIG_XTAL_STABLE)) {
			wil_err(wil, "Xtal stabilization timeout\n"
				"RGF_CAF_PLL_LOCK_STATUS = 0x%08x\n", x);
			return -ETIME;
		}
		/* switch 10k to XTAL*/
		C(RGF_USER_SPARROW_M_4, BIT_SPARROW_M_4_SEL_SLEEP_OR_REF);
		/* 40 MHz */
		C(RGF_USER_CLKS_CTL_0, BIT_USER_CLKS_CAR_AHB_SW_SEL);

480
		W(RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_0, 0x3ff81f);
V
Vladimir Kondratiev 已提交
481
		W(RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_1, 0xf);
482 483
	}

484 485
	W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0xFE000000);
	W(RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0x0000003F);
V
Vladimir Kondratiev 已提交
486 487
	W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, is_sparrow ? 0x000000f0 : 0x00000170);
	W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0xFFE7FE00);
488

489 490
	if (is_sparrow) {
		W(RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_0, 0x0);
V
Vladimir Kondratiev 已提交
491
		W(RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_1, 0x0);
492 493
	}

494 495 496 497 498
	W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0);
	W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0);
	W(RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0);
	W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);

499 500 501
	if (is_sparrow) {
		W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x00000003);
		/* reset A2 PCIE AHB */
502
		W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0x00008000);
503 504 505 506 507 508 509 510 511
	} else {
		W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x00000001);
		if (rev_id == 1) {
			/* reset A1 BOTH PCIE AHB & PCIE RGF */
			W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0x00000080);
		} else {
			W(RGF_PCIE_LOS_COUNTER_CTL, BIT(6) | BIT(8));
			W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0x00008000);
		}
512
	}
513 514

	/* TODO: check order here!!! Erez code is different */
515 516
	W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);

517
	/* wait until device ready. typical time is 200..250 msec */
518
	do {
519
		msleep(RST_DELAY);
V
Vladimir Kondratiev 已提交
520
		x = R(RGF_USER_HW_MACHINE_STATE);
521
		if (delay++ > RST_COUNT) {
522
			wil_err(wil, "Reset not completed, hw_state 0x%08x\n",
V
Vladimir Kondratiev 已提交
523
				x);
524
			return -ETIME;
525
		}
V
Vladimir Kondratiev 已提交
526
	} while (x != HW_MACHINE_BOOT_DONE);
527

528 529
	/* TODO: Erez check rev_id != 1 */
	if (!is_sparrow && (rev_id != 1))
530
		W(RGF_PCIE_LOS_COUNTER_CTL, BIT(8));
531

532 533
	C(RGF_USER_CLKS_CTL_0, BIT_USER_CLKS_RST_PWGD);

534
	wil_dbg_misc(wil, "Reset completed in %d ms\n", delay * RST_DELAY);
535
	return 0;
V
Vladimir Kondratiev 已提交
536
}
537

V
Vladimir Kondratiev 已提交
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
/**
 * wil_set_itr_trsh: - apply interrupt coalescing params
 */
void wil_set_itr_trsh(struct wil6210_priv *wil)
{
	/* disable, use usec resolution */
	W(RGF_DMA_ITR_CNT_CRL, BIT_DMA_ITR_CNT_CRL_EXT_TICK);

	/* disable interrupt moderation for monitor
	 * to get better timestamp precision
	 */
	if (wil->wdev->iftype == NL80211_IFTYPE_MONITOR)
		return;

	wil_info(wil, "set ITR_TRSH = %d usec\n", wil->itr_trsh);
	W(RGF_DMA_ITR_CNT_TRSH, wil->itr_trsh);
	W(RGF_DMA_ITR_CNT_CRL, BIT_DMA_ITR_CNT_CRL_EN |
	  BIT_DMA_ITR_CNT_CRL_EXT_TICK); /* start it */
}

558
#undef R
559 560
#undef W
#undef S
561
#undef C
562 563 564 565 566 567 568 569 570 571 572 573 574 575

void wil_mbox_ring_le2cpus(struct wil6210_mbox_ring *r)
{
	le32_to_cpus(&r->base);
	le16_to_cpus(&r->entry_size);
	le16_to_cpus(&r->size);
	le32_to_cpus(&r->tail);
	le32_to_cpus(&r->head);
}

static int wil_wait_for_fw_ready(struct wil6210_priv *wil)
{
	ulong to = msecs_to_jiffies(1000);
	ulong left = wait_for_completion_timeout(&wil->wmi_ready, to);
576

577 578 579 580
	if (0 == left) {
		wil_err(wil, "Firmware not ready\n");
		return -ETIME;
	} else {
581 582
		wil_info(wil, "FW ready after %d ms. HW version 0x%08x\n",
			 jiffies_to_msecs(to-left), wil->hw_version);
583 584 585 586 587 588 589 590 591 592 593 594 595
	}
	return 0;
}

/*
 * We reset all the structures, and we reset the UMAC.
 * After calling this routine, you're expected to reload
 * the firmware.
 */
int wil_reset(struct wil6210_priv *wil)
{
	int rc;

596 597
	wil_dbg_misc(wil, "%s()\n", __func__);

598
	WARN_ON(!mutex_is_locked(&wil->mutex));
599
	WARN_ON(test_bit(wil_status_napi_en, &wil->status));
600 601

	cancel_work_sync(&wil->disconnect_worker);
602
	wil6210_disconnect(wil, NULL, WLAN_REASON_DEAUTH_LEAVING, false);
603

604 605
	wil->status = 0; /* prevent NAPI from being scheduled */

V
Vladimir Kondratiev 已提交
606 607 608
	if (wil->scan_request) {
		wil_dbg_misc(wil, "Abort scan_request 0x%p\n",
			     wil->scan_request);
609
		del_timer_sync(&wil->scan_timer);
V
Vladimir Kondratiev 已提交
610 611 612 613
		cfg80211_scan_done(wil->scan_request, true);
		wil->scan_request = NULL;
	}

614
	wil_mask_irq(wil);
615

616 617 618
	wmi_event_flush(wil);

	flush_workqueue(wil->wmi_wq_conn);
619
	flush_workqueue(wil->wmi_wq);
620

621
	rc = wil_target_reset(wil);
622
	wil_rx_fini(wil);
623 624 625
	if (rc)
		return rc;

V
Vladimir Kondratiev 已提交
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
	if (!no_fw_load) {
		wil_info(wil, "Use firmware <%s>\n", WIL_FW_NAME);
		wil_halt_cpu(wil);
		/* Loading f/w from the file */
		rc = wil_request_firmware(wil, WIL_FW_NAME);
		if (rc)
			return rc;

		/* clear any interrupts which on-card-firmware may have set */
		wil6210_clear_irq(wil);
		{ /* CAF_ICR - clear and mask */
			u32 a = HOSTADDR(RGF_CAF_ICR) +
				offsetof(struct RGF_ICR, ICR);
			u32 m = HOSTADDR(RGF_CAF_ICR) +
				offsetof(struct RGF_ICR, IMV);
			u32 icr = ioread32(wil->csr + a);

			iowrite32(icr, wil->csr + a); /* W1C */
			iowrite32(~0, wil->csr + m);
			wmb(); /* wait for completion */
		}
		wil_release_cpu(wil);
	} else {
		wil_info(wil, "Use firmware from on-card flash\n");
	}
651

652 653
	/* init after reset */
	wil->pending_connect_cid = -1;
654
	reinit_completion(&wil->wmi_ready);
655
	reinit_completion(&wil->wmi_call);
656

657
	wil_unmask_irq(wil);
658 659 660 661 662 663 664

	/* we just started MAC, wait for FW ready */
	rc = wil_wait_for_fw_ready(wil);

	return rc;
}

V
Vladimir Kondratiev 已提交
665 666 667
void wil_fw_error_recovery(struct wil6210_priv *wil)
{
	wil_dbg_misc(wil, "starting fw error recovery\n");
668
	wil->recovery_state = fw_recovery_pending;
V
Vladimir Kondratiev 已提交
669 670
	schedule_work(&wil->fw_error_worker);
}
671 672 673 674 675

void wil_link_on(struct wil6210_priv *wil)
{
	struct net_device *ndev = wil_to_ndev(wil);

676
	wil_dbg_misc(wil, "%s()\n", __func__);
677 678

	netif_carrier_on(ndev);
679
	wil_dbg_misc(wil, "netif_tx_wake : link on\n");
680 681 682 683 684 685 686
	netif_tx_wake_all_queues(ndev);
}

void wil_link_off(struct wil6210_priv *wil)
{
	struct net_device *ndev = wil_to_ndev(wil);

687
	wil_dbg_misc(wil, "%s()\n", __func__);
688 689

	netif_tx_stop_all_queues(ndev);
690
	wil_dbg_misc(wil, "netif_tx_stop : link off\n");
691 692 693
	netif_carrier_off(ndev);
}

694
int __wil_up(struct wil6210_priv *wil)
695 696 697 698 699
{
	struct net_device *ndev = wil_to_ndev(wil);
	struct wireless_dev *wdev = wil->wdev;
	int rc;

700 701
	WARN_ON(!mutex_is_locked(&wil->mutex));

702 703 704 705
	rc = wil_reset(wil);
	if (rc)
		return rc;

706 707 708 709 710
	/* Rx VRING. After MAC and beacon */
	rc = wil_rx_init(wil);
	if (rc)
		return rc;

711 712
	switch (wdev->iftype) {
	case NL80211_IFTYPE_STATION:
713
		wil_dbg_misc(wil, "type: STATION\n");
714 715 716
		ndev->type = ARPHRD_ETHER;
		break;
	case NL80211_IFTYPE_AP:
717
		wil_dbg_misc(wil, "type: AP\n");
718 719 720
		ndev->type = ARPHRD_ETHER;
		break;
	case NL80211_IFTYPE_P2P_CLIENT:
721
		wil_dbg_misc(wil, "type: P2P_CLIENT\n");
722 723 724
		ndev->type = ARPHRD_ETHER;
		break;
	case NL80211_IFTYPE_P2P_GO:
725
		wil_dbg_misc(wil, "type: P2P_GO\n");
726 727 728
		ndev->type = ARPHRD_ETHER;
		break;
	case NL80211_IFTYPE_MONITOR:
729
		wil_dbg_misc(wil, "type: Monitor\n");
730 731 732 733 734 735 736 737 738 739
		ndev->type = ARPHRD_IEEE80211_RADIOTAP;
		/* ARPHRD_IEEE80211 or ARPHRD_IEEE80211_RADIOTAP ? */
		break;
	default:
		return -EOPNOTSUPP;
	}

	/* MAC address - pre-requisite for other commands */
	wmi_set_mac_address(wil, ndev->dev_addr);

740
	wil_dbg_misc(wil, "NAPI enable\n");
V
Vladimir Kondratiev 已提交
741 742
	napi_enable(&wil->napi_rx);
	napi_enable(&wil->napi_tx);
743
	set_bit(wil_status_napi_en, &wil->status);
V
Vladimir Kondratiev 已提交
744

745 746 747 748
	if (wil->platform_ops.bus_request)
		wil->platform_ops.bus_request(wil->platform_handle,
					      WIL_MAX_BUS_REQUEST_KBPS);

749 750 751 752 753 754 755
	return 0;
}

int wil_up(struct wil6210_priv *wil)
{
	int rc;

756 757
	wil_dbg_misc(wil, "%s()\n", __func__);

758 759 760 761 762 763 764
	mutex_lock(&wil->mutex);
	rc = __wil_up(wil);
	mutex_unlock(&wil->mutex);

	return rc;
}

765
int __wil_down(struct wil6210_priv *wil)
766
{
767 768 769
	int iter = WAIT_FOR_DISCONNECT_TIMEOUT_MS /
			WAIT_FOR_DISCONNECT_INTERVAL_MS;

770 771
	WARN_ON(!mutex_is_locked(&wil->mutex));

772 773 774
	if (wil->platform_ops.bus_request)
		wil->platform_ops.bus_request(wil->platform_handle, 0);

775 776 777 778 779 780 781
	wil_disable_irq(wil);
	if (test_and_clear_bit(wil_status_napi_en, &wil->status)) {
		napi_disable(&wil->napi_rx);
		napi_disable(&wil->napi_tx);
		wil_dbg_misc(wil, "NAPI disable\n");
	}
	wil_enable_irq(wil);
V
Vladimir Kondratiev 已提交
782

783
	if (wil->scan_request) {
784 785
		wil_dbg_misc(wil, "Abort scan_request 0x%p\n",
			     wil->scan_request);
786
		del_timer_sync(&wil->scan_timer);
787 788 789 790
		cfg80211_scan_done(wil->scan_request, true);
		wil->scan_request = NULL;
	}

791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
	if (test_bit(wil_status_fwconnected, &wil->status) ||
	    test_bit(wil_status_fwconnecting, &wil->status))
		wmi_send(wil, WMI_DISCONNECT_CMDID, NULL, 0);

	/* make sure wil is idle (not connected) */
	mutex_unlock(&wil->mutex);
	while (iter--) {
		int idle = !test_bit(wil_status_fwconnected, &wil->status) &&
			   !test_bit(wil_status_fwconnecting, &wil->status);
		if (idle)
			break;
		msleep(WAIT_FOR_DISCONNECT_INTERVAL_MS);
	}
	mutex_lock(&wil->mutex);

	if (!iter)
		wil_err(wil, "timeout waiting for idle FW/HW\n");

809 810 811 812 813 814 815 816 817
	wil_rx_fini(wil);

	return 0;
}

int wil_down(struct wil6210_priv *wil)
{
	int rc;

818 819
	wil_dbg_misc(wil, "%s()\n", __func__);

820
	wil_set_recovery_state(wil, fw_recovery_idle);
821 822 823 824 825 826
	mutex_lock(&wil->mutex);
	rc = __wil_down(wil);
	mutex_unlock(&wil->mutex);

	return rc;
}
827 828 829 830 831 832 833 834

int wil_find_cid(struct wil6210_priv *wil, const u8 *mac)
{
	int i;
	int rc = -ENOENT;

	for (i = 0; i < ARRAY_SIZE(wil->sta); i++) {
		if ((wil->sta[i].status != wil_sta_unused) &&
835
		    ether_addr_equal(wil->sta[i].addr, mac)) {
836 837 838 839 840 841 842
			rc = i;
			break;
		}
	}

	return rc;
}