1 /*
2  * Copyright (c) 2012-2017 Qualcomm Atheros, Inc.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #include <linux/interrupt.h>
18 
19 #include "wil6210.h"
20 #include "trace.h"
21 
22 /**
23  * Theory of operation:
24  *
25  * There is ISR pseudo-cause register,
26  * dma_rgf->DMA_RGF.PSEUDO_CAUSE.PSEUDO_CAUSE
27  * Its bits represents OR'ed bits from 3 real ISR registers:
28  * TX, RX, and MISC.
29  *
30  * Registers may be configured to either "write 1 to clear" or
31  * "clear on read" mode
32  *
33  * When handling interrupt, one have to mask/unmask interrupts for the
34  * real ISR registers, or hardware may malfunction.
35  *
36  */
37 
38 #define WIL6210_IRQ_DISABLE		(0xFFFFFFFFUL)
39 #define WIL6210_IRQ_DISABLE_NO_HALP	(0xF7FFFFFFUL)
40 #define WIL6210_IMC_RX		(BIT_DMA_EP_RX_ICR_RX_DONE | \
41 				 BIT_DMA_EP_RX_ICR_RX_HTRSH)
42 #define WIL6210_IMC_RX_NO_RX_HTRSH (WIL6210_IMC_RX & \
43 				    (~(BIT_DMA_EP_RX_ICR_RX_HTRSH)))
44 #define WIL6210_IMC_TX		(BIT_DMA_EP_TX_ICR_TX_DONE | \
45 				BIT_DMA_EP_TX_ICR_TX_DONE_N(0))
46 #define WIL6210_IMC_MISC_NO_HALP	(ISR_MISC_FW_READY | \
47 					 ISR_MISC_MBOX_EVT | \
48 					 ISR_MISC_FW_ERROR)
49 #define WIL6210_IMC_MISC		(WIL6210_IMC_MISC_NO_HALP | \
50 					 BIT_DMA_EP_MISC_ICR_HALP)
51 #define WIL6210_IRQ_PSEUDO_MASK (u32)(~(BIT_DMA_PSEUDO_CAUSE_RX | \
52 					BIT_DMA_PSEUDO_CAUSE_TX | \
53 					BIT_DMA_PSEUDO_CAUSE_MISC))
54 
55 #if defined(CONFIG_WIL6210_ISR_COR)
56 /* configure to Clear-On-Read mode */
57 #define WIL_ICR_ICC_VALUE	(0xFFFFFFFFUL)
58 #define WIL_ICR_ICC_MISC_VALUE	(0xF7FFFFFFUL)
59 
60 static inline void wil_icr_clear(u32 x, void __iomem *addr)
61 {
62 }
63 #else /* defined(CONFIG_WIL6210_ISR_COR) */
64 /* configure to Write-1-to-Clear mode */
65 #define WIL_ICR_ICC_VALUE	(0UL)
66 #define WIL_ICR_ICC_MISC_VALUE	(0UL)
67 
68 static inline void wil_icr_clear(u32 x, void __iomem *addr)
69 {
70 	writel(x, addr);
71 }
72 #endif /* defined(CONFIG_WIL6210_ISR_COR) */
73 
74 static inline u32 wil_ioread32_and_clear(void __iomem *addr)
75 {
76 	u32 x = readl(addr);
77 
78 	wil_icr_clear(x, addr);
79 
80 	return x;
81 }
82 
83 static void wil6210_mask_irq_tx(struct wil6210_priv *wil)
84 {
85 	wil_w(wil, RGF_DMA_EP_TX_ICR + offsetof(struct RGF_ICR, IMS),
86 	      WIL6210_IRQ_DISABLE);
87 }
88 
89 static void wil6210_mask_irq_rx(struct wil6210_priv *wil)
90 {
91 	wil_w(wil, RGF_DMA_EP_RX_ICR + offsetof(struct RGF_ICR, IMS),
92 	      WIL6210_IRQ_DISABLE);
93 }
94 
95 static void wil6210_mask_irq_misc(struct wil6210_priv *wil, bool mask_halp)
96 {
97 	wil_dbg_irq(wil, "mask_irq_misc: mask_halp(%s)\n",
98 		    mask_halp ? "true" : "false");
99 
100 	wil_w(wil, RGF_DMA_EP_MISC_ICR + offsetof(struct RGF_ICR, IMS),
101 	      mask_halp ? WIL6210_IRQ_DISABLE : WIL6210_IRQ_DISABLE_NO_HALP);
102 }
103 
104 void wil6210_mask_halp(struct wil6210_priv *wil)
105 {
106 	wil_dbg_irq(wil, "mask_halp\n");
107 
108 	wil_w(wil, RGF_DMA_EP_MISC_ICR + offsetof(struct RGF_ICR, IMS),
109 	      BIT_DMA_EP_MISC_ICR_HALP);
110 }
111 
112 static void wil6210_mask_irq_pseudo(struct wil6210_priv *wil)
113 {
114 	wil_dbg_irq(wil, "mask_irq_pseudo\n");
115 
116 	wil_w(wil, RGF_DMA_PSEUDO_CAUSE_MASK_SW, WIL6210_IRQ_DISABLE);
117 
118 	clear_bit(wil_status_irqen, wil->status);
119 }
120 
121 void wil6210_unmask_irq_tx(struct wil6210_priv *wil)
122 {
123 	wil_w(wil, RGF_DMA_EP_TX_ICR + offsetof(struct RGF_ICR, IMC),
124 	      WIL6210_IMC_TX);
125 }
126 
127 void wil6210_unmask_irq_rx(struct wil6210_priv *wil)
128 {
129 	bool unmask_rx_htrsh = test_bit(wil_status_fwconnected, wil->status);
130 
131 	wil_w(wil, RGF_DMA_EP_RX_ICR + offsetof(struct RGF_ICR, IMC),
132 	      unmask_rx_htrsh ? WIL6210_IMC_RX : WIL6210_IMC_RX_NO_RX_HTRSH);
133 }
134 
135 static void wil6210_unmask_irq_misc(struct wil6210_priv *wil, bool unmask_halp)
136 {
137 	wil_dbg_irq(wil, "unmask_irq_misc: unmask_halp(%s)\n",
138 		    unmask_halp ? "true" : "false");
139 
140 	wil_w(wil, RGF_DMA_EP_MISC_ICR + offsetof(struct RGF_ICR, IMC),
141 	      unmask_halp ? WIL6210_IMC_MISC : WIL6210_IMC_MISC_NO_HALP);
142 }
143 
144 static void wil6210_unmask_halp(struct wil6210_priv *wil)
145 {
146 	wil_dbg_irq(wil, "unmask_halp\n");
147 
148 	wil_w(wil, RGF_DMA_EP_MISC_ICR + offsetof(struct RGF_ICR, IMC),
149 	      BIT_DMA_EP_MISC_ICR_HALP);
150 }
151 
152 static void wil6210_unmask_irq_pseudo(struct wil6210_priv *wil)
153 {
154 	wil_dbg_irq(wil, "unmask_irq_pseudo\n");
155 
156 	set_bit(wil_status_irqen, wil->status);
157 
158 	wil_w(wil, RGF_DMA_PSEUDO_CAUSE_MASK_SW, WIL6210_IRQ_PSEUDO_MASK);
159 }
160 
161 void wil_mask_irq(struct wil6210_priv *wil)
162 {
163 	wil_dbg_irq(wil, "mask_irq\n");
164 
165 	wil6210_mask_irq_tx(wil);
166 	wil6210_mask_irq_rx(wil);
167 	wil6210_mask_irq_misc(wil, true);
168 	wil6210_mask_irq_pseudo(wil);
169 }
170 
171 void wil_unmask_irq(struct wil6210_priv *wil)
172 {
173 	wil_dbg_irq(wil, "unmask_irq\n");
174 
175 	wil_w(wil, RGF_DMA_EP_RX_ICR + offsetof(struct RGF_ICR, ICC),
176 	      WIL_ICR_ICC_VALUE);
177 	wil_w(wil, RGF_DMA_EP_TX_ICR + offsetof(struct RGF_ICR, ICC),
178 	      WIL_ICR_ICC_VALUE);
179 	wil_w(wil, RGF_DMA_EP_MISC_ICR + offsetof(struct RGF_ICR, ICC),
180 	      WIL_ICR_ICC_MISC_VALUE);
181 
182 	wil6210_unmask_irq_pseudo(wil);
183 	wil6210_unmask_irq_tx(wil);
184 	wil6210_unmask_irq_rx(wil);
185 	wil6210_unmask_irq_misc(wil, true);
186 }
187 
188 void wil_configure_interrupt_moderation(struct wil6210_priv *wil)
189 {
190 	wil_dbg_irq(wil, "configure_interrupt_moderation\n");
191 
192 	/* disable interrupt moderation for monitor
193 	 * to get better timestamp precision
194 	 */
195 	if (wil->wdev->iftype == NL80211_IFTYPE_MONITOR)
196 		return;
197 
198 	/* Disable and clear tx counter before (re)configuration */
199 	wil_w(wil, RGF_DMA_ITR_TX_CNT_CTL, BIT_DMA_ITR_TX_CNT_CTL_CLR);
200 	wil_w(wil, RGF_DMA_ITR_TX_CNT_TRSH, wil->tx_max_burst_duration);
201 	wil_info(wil, "set ITR_TX_CNT_TRSH = %d usec\n",
202 		 wil->tx_max_burst_duration);
203 	/* Configure TX max burst duration timer to use usec units */
204 	wil_w(wil, RGF_DMA_ITR_TX_CNT_CTL,
205 	      BIT_DMA_ITR_TX_CNT_CTL_EN | BIT_DMA_ITR_TX_CNT_CTL_EXT_TIC_SEL);
206 
207 	/* Disable and clear tx idle counter before (re)configuration */
208 	wil_w(wil, RGF_DMA_ITR_TX_IDL_CNT_CTL, BIT_DMA_ITR_TX_IDL_CNT_CTL_CLR);
209 	wil_w(wil, RGF_DMA_ITR_TX_IDL_CNT_TRSH, wil->tx_interframe_timeout);
210 	wil_info(wil, "set ITR_TX_IDL_CNT_TRSH = %d usec\n",
211 		 wil->tx_interframe_timeout);
212 	/* Configure TX max burst duration timer to use usec units */
213 	wil_w(wil, RGF_DMA_ITR_TX_IDL_CNT_CTL, BIT_DMA_ITR_TX_IDL_CNT_CTL_EN |
214 	      BIT_DMA_ITR_TX_IDL_CNT_CTL_EXT_TIC_SEL);
215 
216 	/* Disable and clear rx counter before (re)configuration */
217 	wil_w(wil, RGF_DMA_ITR_RX_CNT_CTL, BIT_DMA_ITR_RX_CNT_CTL_CLR);
218 	wil_w(wil, RGF_DMA_ITR_RX_CNT_TRSH, wil->rx_max_burst_duration);
219 	wil_info(wil, "set ITR_RX_CNT_TRSH = %d usec\n",
220 		 wil->rx_max_burst_duration);
221 	/* Configure TX max burst duration timer to use usec units */
222 	wil_w(wil, RGF_DMA_ITR_RX_CNT_CTL,
223 	      BIT_DMA_ITR_RX_CNT_CTL_EN | BIT_DMA_ITR_RX_CNT_CTL_EXT_TIC_SEL);
224 
225 	/* Disable and clear rx idle counter before (re)configuration */
226 	wil_w(wil, RGF_DMA_ITR_RX_IDL_CNT_CTL, BIT_DMA_ITR_RX_IDL_CNT_CTL_CLR);
227 	wil_w(wil, RGF_DMA_ITR_RX_IDL_CNT_TRSH, wil->rx_interframe_timeout);
228 	wil_info(wil, "set ITR_RX_IDL_CNT_TRSH = %d usec\n",
229 		 wil->rx_interframe_timeout);
230 	/* Configure TX max burst duration timer to use usec units */
231 	wil_w(wil, RGF_DMA_ITR_RX_IDL_CNT_CTL, BIT_DMA_ITR_RX_IDL_CNT_CTL_EN |
232 	      BIT_DMA_ITR_RX_IDL_CNT_CTL_EXT_TIC_SEL);
233 }
234 
235 static irqreturn_t wil6210_irq_rx(int irq, void *cookie)
236 {
237 	struct wil6210_priv *wil = cookie;
238 	u32 isr = wil_ioread32_and_clear(wil->csr +
239 					 HOSTADDR(RGF_DMA_EP_RX_ICR) +
240 					 offsetof(struct RGF_ICR, ICR));
241 	bool need_unmask = true;
242 
243 	trace_wil6210_irq_rx(isr);
244 	wil_dbg_irq(wil, "ISR RX 0x%08x\n", isr);
245 
246 	if (unlikely(!isr)) {
247 		wil_err_ratelimited(wil, "spurious IRQ: RX\n");
248 		return IRQ_NONE;
249 	}
250 
251 	wil6210_mask_irq_rx(wil);
252 
253 	/* RX_DONE and RX_HTRSH interrupts are the same if interrupt
254 	 * moderation is not used. Interrupt moderation may cause RX
255 	 * buffer overflow while RX_DONE is delayed. The required
256 	 * action is always the same - should empty the accumulated
257 	 * packets from the RX ring.
258 	 */
259 	if (likely(isr & (BIT_DMA_EP_RX_ICR_RX_DONE |
260 			  BIT_DMA_EP_RX_ICR_RX_HTRSH))) {
261 		wil_dbg_irq(wil, "RX done / RX_HTRSH received, ISR (0x%x)\n",
262 			    isr);
263 
264 		isr &= ~(BIT_DMA_EP_RX_ICR_RX_DONE |
265 			 BIT_DMA_EP_RX_ICR_RX_HTRSH);
266 		if (likely(test_bit(wil_status_fwready, wil->status))) {
267 			if (likely(test_bit(wil_status_napi_en, wil->status))) {
268 				wil_dbg_txrx(wil, "NAPI(Rx) schedule\n");
269 				need_unmask = false;
270 				napi_schedule(&wil->napi_rx);
271 			} else {
272 				wil_err_ratelimited(
273 					wil,
274 					"Got Rx interrupt while stopping interface\n");
275 			}
276 		} else {
277 			wil_err_ratelimited(wil, "Got Rx interrupt while in reset\n");
278 		}
279 	}
280 
281 	if (unlikely(isr))
282 		wil_err(wil, "un-handled RX ISR bits 0x%08x\n", isr);
283 
284 	/* Rx IRQ will be enabled when NAPI processing finished */
285 
286 	atomic_inc(&wil->isr_count_rx);
287 
288 	if (unlikely(need_unmask))
289 		wil6210_unmask_irq_rx(wil);
290 
291 	return IRQ_HANDLED;
292 }
293 
294 static irqreturn_t wil6210_irq_tx(int irq, void *cookie)
295 {
296 	struct wil6210_priv *wil = cookie;
297 	u32 isr = wil_ioread32_and_clear(wil->csr +
298 					 HOSTADDR(RGF_DMA_EP_TX_ICR) +
299 					 offsetof(struct RGF_ICR, ICR));
300 	bool need_unmask = true;
301 
302 	trace_wil6210_irq_tx(isr);
303 	wil_dbg_irq(wil, "ISR TX 0x%08x\n", isr);
304 
305 	if (unlikely(!isr)) {
306 		wil_err_ratelimited(wil, "spurious IRQ: TX\n");
307 		return IRQ_NONE;
308 	}
309 
310 	wil6210_mask_irq_tx(wil);
311 
312 	if (likely(isr & BIT_DMA_EP_TX_ICR_TX_DONE)) {
313 		wil_dbg_irq(wil, "TX done\n");
314 		isr &= ~BIT_DMA_EP_TX_ICR_TX_DONE;
315 		/* clear also all VRING interrupts */
316 		isr &= ~(BIT(25) - 1UL);
317 		if (likely(test_bit(wil_status_fwready, wil->status))) {
318 			wil_dbg_txrx(wil, "NAPI(Tx) schedule\n");
319 			need_unmask = false;
320 			napi_schedule(&wil->napi_tx);
321 		} else {
322 			wil_err_ratelimited(wil, "Got Tx interrupt while in reset\n");
323 		}
324 	}
325 
326 	if (unlikely(isr))
327 		wil_err_ratelimited(wil, "un-handled TX ISR bits 0x%08x\n",
328 				    isr);
329 
330 	/* Tx IRQ will be enabled when NAPI processing finished */
331 
332 	atomic_inc(&wil->isr_count_tx);
333 
334 	if (unlikely(need_unmask))
335 		wil6210_unmask_irq_tx(wil);
336 
337 	return IRQ_HANDLED;
338 }
339 
340 static void wil_notify_fw_error(struct wil6210_priv *wil)
341 {
342 	struct device *dev = &wil_to_ndev(wil)->dev;
343 	char *envp[3] = {
344 		[0] = "SOURCE=wil6210",
345 		[1] = "EVENT=FW_ERROR",
346 		[2] = NULL,
347 	};
348 	wil_err(wil, "Notify about firmware error\n");
349 	kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
350 }
351 
352 static void wil_cache_mbox_regs(struct wil6210_priv *wil)
353 {
354 	/* make shadow copy of registers that should not change on run time */
355 	wil_memcpy_fromio_32(&wil->mbox_ctl, wil->csr + HOST_MBOX,
356 			     sizeof(struct wil6210_mbox_ctl));
357 	wil_mbox_ring_le2cpus(&wil->mbox_ctl.rx);
358 	wil_mbox_ring_le2cpus(&wil->mbox_ctl.tx);
359 }
360 
361 static irqreturn_t wil6210_irq_misc(int irq, void *cookie)
362 {
363 	struct wil6210_priv *wil = cookie;
364 	u32 isr = wil_ioread32_and_clear(wil->csr +
365 					 HOSTADDR(RGF_DMA_EP_MISC_ICR) +
366 					 offsetof(struct RGF_ICR, ICR));
367 
368 	trace_wil6210_irq_misc(isr);
369 	wil_dbg_irq(wil, "ISR MISC 0x%08x\n", isr);
370 
371 	if (!isr) {
372 		wil_err(wil, "spurious IRQ: MISC\n");
373 		return IRQ_NONE;
374 	}
375 
376 	wil6210_mask_irq_misc(wil, false);
377 
378 	if (isr & ISR_MISC_FW_ERROR) {
379 		u32 fw_assert_code = wil_r(wil, RGF_FW_ASSERT_CODE);
380 		u32 ucode_assert_code = wil_r(wil, RGF_UCODE_ASSERT_CODE);
381 
382 		wil_err(wil,
383 			"Firmware error detected, assert codes FW 0x%08x, UCODE 0x%08x\n",
384 			fw_assert_code, ucode_assert_code);
385 		clear_bit(wil_status_fwready, wil->status);
386 		/*
387 		 * do not clear @isr here - we do 2-nd part in thread
388 		 * there, user space get notified, and it should be done
389 		 * in non-atomic context
390 		 */
391 	}
392 
393 	if (isr & ISR_MISC_FW_READY) {
394 		wil_dbg_irq(wil, "IRQ: FW ready\n");
395 		wil_cache_mbox_regs(wil);
396 		set_bit(wil_status_mbox_ready, wil->status);
397 		/**
398 		 * Actual FW ready indicated by the
399 		 * WMI_FW_READY_EVENTID
400 		 */
401 		isr &= ~ISR_MISC_FW_READY;
402 	}
403 
404 	if (isr & BIT_DMA_EP_MISC_ICR_HALP) {
405 		wil_dbg_irq(wil, "irq_misc: HALP IRQ invoked\n");
406 		wil6210_mask_halp(wil);
407 		isr &= ~BIT_DMA_EP_MISC_ICR_HALP;
408 		complete(&wil->halp.comp);
409 	}
410 
411 	wil->isr_misc = isr;
412 
413 	if (isr) {
414 		return IRQ_WAKE_THREAD;
415 	} else {
416 		wil6210_unmask_irq_misc(wil, false);
417 		return IRQ_HANDLED;
418 	}
419 }
420 
421 static irqreturn_t wil6210_irq_misc_thread(int irq, void *cookie)
422 {
423 	struct wil6210_priv *wil = cookie;
424 	u32 isr = wil->isr_misc;
425 
426 	trace_wil6210_irq_misc_thread(isr);
427 	wil_dbg_irq(wil, "Thread ISR MISC 0x%08x\n", isr);
428 
429 	if (isr & ISR_MISC_FW_ERROR) {
430 		wil->recovery_state = fw_recovery_pending;
431 		wil_fw_core_dump(wil);
432 		wil_notify_fw_error(wil);
433 		isr &= ~ISR_MISC_FW_ERROR;
434 		if (wil->platform_ops.notify) {
435 			wil_err(wil, "notify platform driver about FW crash");
436 			wil->platform_ops.notify(wil->platform_handle,
437 						 WIL_PLATFORM_EVT_FW_CRASH);
438 		} else {
439 			wil_fw_error_recovery(wil);
440 		}
441 	}
442 	if (isr & ISR_MISC_MBOX_EVT) {
443 		wil_dbg_irq(wil, "MBOX event\n");
444 		wmi_recv_cmd(wil);
445 		isr &= ~ISR_MISC_MBOX_EVT;
446 	}
447 
448 	if (isr)
449 		wil_dbg_irq(wil, "un-handled MISC ISR bits 0x%08x\n", isr);
450 
451 	wil->isr_misc = 0;
452 
453 	wil6210_unmask_irq_misc(wil, false);
454 
455 	return IRQ_HANDLED;
456 }
457 
458 /**
459  * thread IRQ handler
460  */
461 static irqreturn_t wil6210_thread_irq(int irq, void *cookie)
462 {
463 	struct wil6210_priv *wil = cookie;
464 
465 	wil_dbg_irq(wil, "Thread IRQ\n");
466 	/* Discover real IRQ cause */
467 	if (wil->isr_misc)
468 		wil6210_irq_misc_thread(irq, cookie);
469 
470 	wil6210_unmask_irq_pseudo(wil);
471 
472 	if (wil->suspend_resp_rcvd) {
473 		wil_dbg_irq(wil, "set suspend_resp_comp to true\n");
474 		wil->suspend_resp_comp = true;
475 		wake_up_interruptible(&wil->wq);
476 	}
477 
478 	return IRQ_HANDLED;
479 }
480 
481 /* DEBUG
482  * There is subtle bug in hardware that causes IRQ to raise when it should be
483  * masked. It is quite rare and hard to debug.
484  *
485  * Catch irq issue if it happens and print all I can.
486  */
487 static int wil6210_debug_irq_mask(struct wil6210_priv *wil, u32 pseudo_cause)
488 {
489 	if (!test_bit(wil_status_irqen, wil->status)) {
490 		u32 icm_rx = wil_ioread32_and_clear(wil->csr +
491 				HOSTADDR(RGF_DMA_EP_RX_ICR) +
492 				offsetof(struct RGF_ICR, ICM));
493 		u32 icr_rx = wil_ioread32_and_clear(wil->csr +
494 				HOSTADDR(RGF_DMA_EP_RX_ICR) +
495 				offsetof(struct RGF_ICR, ICR));
496 		u32 imv_rx = wil_r(wil, RGF_DMA_EP_RX_ICR +
497 				   offsetof(struct RGF_ICR, IMV));
498 		u32 icm_tx = wil_ioread32_and_clear(wil->csr +
499 				HOSTADDR(RGF_DMA_EP_TX_ICR) +
500 				offsetof(struct RGF_ICR, ICM));
501 		u32 icr_tx = wil_ioread32_and_clear(wil->csr +
502 				HOSTADDR(RGF_DMA_EP_TX_ICR) +
503 				offsetof(struct RGF_ICR, ICR));
504 		u32 imv_tx = wil_r(wil, RGF_DMA_EP_TX_ICR +
505 				   offsetof(struct RGF_ICR, IMV));
506 		u32 icm_misc = wil_ioread32_and_clear(wil->csr +
507 				HOSTADDR(RGF_DMA_EP_MISC_ICR) +
508 				offsetof(struct RGF_ICR, ICM));
509 		u32 icr_misc = wil_ioread32_and_clear(wil->csr +
510 				HOSTADDR(RGF_DMA_EP_MISC_ICR) +
511 				offsetof(struct RGF_ICR, ICR));
512 		u32 imv_misc = wil_r(wil, RGF_DMA_EP_MISC_ICR +
513 				     offsetof(struct RGF_ICR, IMV));
514 
515 		/* HALP interrupt can be unmasked when misc interrupts are
516 		 * masked
517 		 */
518 		if (icr_misc & BIT_DMA_EP_MISC_ICR_HALP)
519 			return 0;
520 
521 		wil_err(wil, "IRQ when it should be masked: pseudo 0x%08x\n"
522 				"Rx   icm:icr:imv 0x%08x 0x%08x 0x%08x\n"
523 				"Tx   icm:icr:imv 0x%08x 0x%08x 0x%08x\n"
524 				"Misc icm:icr:imv 0x%08x 0x%08x 0x%08x\n",
525 				pseudo_cause,
526 				icm_rx, icr_rx, imv_rx,
527 				icm_tx, icr_tx, imv_tx,
528 				icm_misc, icr_misc, imv_misc);
529 
530 		return -EINVAL;
531 	}
532 
533 	return 0;
534 }
535 
536 static irqreturn_t wil6210_hardirq(int irq, void *cookie)
537 {
538 	irqreturn_t rc = IRQ_HANDLED;
539 	struct wil6210_priv *wil = cookie;
540 	u32 pseudo_cause = wil_r(wil, RGF_DMA_PSEUDO_CAUSE);
541 
542 	/**
543 	 * pseudo_cause is Clear-On-Read, no need to ACK
544 	 */
545 	if (unlikely((pseudo_cause == 0) || ((pseudo_cause & 0xff) == 0xff)))
546 		return IRQ_NONE;
547 
548 	/* FIXME: IRQ mask debug */
549 	if (unlikely(wil6210_debug_irq_mask(wil, pseudo_cause)))
550 		return IRQ_NONE;
551 
552 	trace_wil6210_irq_pseudo(pseudo_cause);
553 	wil_dbg_irq(wil, "Pseudo IRQ 0x%08x\n", pseudo_cause);
554 
555 	wil6210_mask_irq_pseudo(wil);
556 
557 	/* Discover real IRQ cause
558 	 * There are 2 possible phases for every IRQ:
559 	 * - hard IRQ handler called right here
560 	 * - threaded handler called later
561 	 *
562 	 * Hard IRQ handler reads and clears ISR.
563 	 *
564 	 * If threaded handler requested, hard IRQ handler
565 	 * returns IRQ_WAKE_THREAD and saves ISR register value
566 	 * for the threaded handler use.
567 	 *
568 	 * voting for wake thread - need at least 1 vote
569 	 */
570 	if ((pseudo_cause & BIT_DMA_PSEUDO_CAUSE_RX) &&
571 	    (wil6210_irq_rx(irq, cookie) == IRQ_WAKE_THREAD))
572 		rc = IRQ_WAKE_THREAD;
573 
574 	if ((pseudo_cause & BIT_DMA_PSEUDO_CAUSE_TX) &&
575 	    (wil6210_irq_tx(irq, cookie) == IRQ_WAKE_THREAD))
576 		rc = IRQ_WAKE_THREAD;
577 
578 	if ((pseudo_cause & BIT_DMA_PSEUDO_CAUSE_MISC) &&
579 	    (wil6210_irq_misc(irq, cookie) == IRQ_WAKE_THREAD))
580 		rc = IRQ_WAKE_THREAD;
581 
582 	/* if thread is requested, it will unmask IRQ */
583 	if (rc != IRQ_WAKE_THREAD)
584 		wil6210_unmask_irq_pseudo(wil);
585 
586 	return rc;
587 }
588 
589 /* can't use wil_ioread32_and_clear because ICC value is not set yet */
590 static inline void wil_clear32(void __iomem *addr)
591 {
592 	u32 x = readl(addr);
593 
594 	writel(x, addr);
595 }
596 
597 void wil6210_clear_irq(struct wil6210_priv *wil)
598 {
599 	wil_clear32(wil->csr + HOSTADDR(RGF_DMA_EP_RX_ICR) +
600 		    offsetof(struct RGF_ICR, ICR));
601 	wil_clear32(wil->csr + HOSTADDR(RGF_DMA_EP_TX_ICR) +
602 		    offsetof(struct RGF_ICR, ICR));
603 	wil_clear32(wil->csr + HOSTADDR(RGF_DMA_EP_MISC_ICR) +
604 		    offsetof(struct RGF_ICR, ICR));
605 	wmb(); /* make sure write completed */
606 }
607 
608 void wil6210_set_halp(struct wil6210_priv *wil)
609 {
610 	wil_dbg_irq(wil, "set_halp\n");
611 
612 	wil_w(wil, RGF_DMA_EP_MISC_ICR + offsetof(struct RGF_ICR, ICS),
613 	      BIT_DMA_EP_MISC_ICR_HALP);
614 }
615 
616 void wil6210_clear_halp(struct wil6210_priv *wil)
617 {
618 	wil_dbg_irq(wil, "clear_halp\n");
619 
620 	wil_w(wil, RGF_DMA_EP_MISC_ICR + offsetof(struct RGF_ICR, ICR),
621 	      BIT_DMA_EP_MISC_ICR_HALP);
622 	wil6210_unmask_halp(wil);
623 }
624 
625 int wil6210_init_irq(struct wil6210_priv *wil, int irq, bool use_msi)
626 {
627 	int rc;
628 
629 	wil_dbg_misc(wil, "init_irq: %s\n", use_msi ? "MSI" : "INTx");
630 
631 	rc = request_threaded_irq(irq, wil6210_hardirq,
632 				  wil6210_thread_irq,
633 				  use_msi ? 0 : IRQF_SHARED,
634 				  WIL_NAME, wil);
635 	return rc;
636 }
637 
638 void wil6210_fini_irq(struct wil6210_priv *wil, int irq)
639 {
640 	wil_dbg_misc(wil, "fini_irq:\n");
641 
642 	wil_mask_irq(wil);
643 	free_irq(irq, wil);
644 }
645