xref: /wlan-dirver/qca-wifi-host-cmn/hif/src/hif_napi.c (revision 2f4b444fb7e689b83a4ab0e7b3b38f0bf4def8e0)
1 /*
2  * Copyright (c) 2015-2021 The Linux Foundation. All rights reserved.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for
5  * any purpose with or without fee is hereby granted, provided that the
6  * above copyright notice and this permission notice appear in all
7  * copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
12  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
13  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
14  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
15  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16  * PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 /**
20  * DOC: hif_napi.c
21  *
22  * HIF NAPI interface implementation
23  */
24 
25 #include <linux/string.h> /* memset */
26 
27 /* Linux headers */
28 #include <linux/cpumask.h>
29 #include <linux/cpufreq.h>
30 #include <linux/cpu.h>
31 #include <linux/topology.h>
32 #include <linux/interrupt.h>
33 #ifdef CONFIG_SCHED_CORE_CTL
34 #include <linux/sched/core_ctl.h>
35 #endif
36 #include <pld_common.h>
37 #include <linux/pm.h>
38 
39 /* Driver headers */
40 #include <hif_napi.h>
41 #include <hif_debug.h>
42 #include <hif_io32.h>
43 #include <ce_api.h>
44 #include <ce_internal.h>
45 #include <hif_irq_affinity.h>
46 #include "qdf_cpuhp.h"
47 #include "qdf_module.h"
48 #include "qdf_net_if.h"
49 #include "qdf_dev.h"
50 #include "qdf_irq.h"
51 
52 enum napi_decision_vector {
53 	HIF_NAPI_NOEVENT = 0,
54 	HIF_NAPI_INITED  = 1,
55 	HIF_NAPI_CONF_UP = 2
56 };
57 #define ENABLE_NAPI_MASK (HIF_NAPI_INITED | HIF_NAPI_CONF_UP)
58 
59 #ifdef RECEIVE_OFFLOAD
60 /**
61  * hif_rxthread_napi_poll() - dummy napi poll for rx_thread NAPI
62  * @napi: Rx_thread NAPI
63  * @budget: NAPI BUDGET
64  *
65  * Return: 0 as it is not supposed to be polled at all as it is not scheduled.
66  */
67 static int hif_rxthread_napi_poll(struct napi_struct *napi, int budget)
68 {
69 	hif_err("This napi_poll should not be polled as we don't schedule it");
70 	QDF_ASSERT(0);
71 	return 0;
72 }
73 
74 /**
75  * hif_init_rx_thread_napi() - Initialize dummy Rx_thread NAPI
76  * @napii: Handle to napi_info holding rx_thread napi
77  *
78  * Return: None
79  */
80 static void hif_init_rx_thread_napi(struct qca_napi_info *napii)
81 {
82 	struct qdf_net_if *nd = (struct qdf_net_if *)&napii->rx_thread_netdev;
83 
84 	qdf_net_if_create_dummy_if(nd);
85 	netif_napi_add(&napii->rx_thread_netdev, &napii->rx_thread_napi,
86 		       hif_rxthread_napi_poll, 64);
87 	napi_enable(&napii->rx_thread_napi);
88 }
89 
90 /**
91  * hif_deinit_rx_thread_napi() - Deinitialize dummy Rx_thread NAPI
92  * @napii: Handle to napi_info holding rx_thread napi
93  *
94  * Return: None
95  */
96 static void hif_deinit_rx_thread_napi(struct qca_napi_info *napii)
97 {
98 	netif_napi_del(&napii->rx_thread_napi);
99 }
100 #else /* RECEIVE_OFFLOAD */
101 static void hif_init_rx_thread_napi(struct qca_napi_info *napii)
102 {
103 }
104 
105 static void hif_deinit_rx_thread_napi(struct qca_napi_info *napii)
106 {
107 }
108 #endif
109 
110 /**
111  * hif_napi_create() - creates the NAPI structures for a given CE
112  * @hif    : pointer to hif context
113  * @pipe_id: the CE id on which the instance will be created
114  * @poll   : poll function to be used for this NAPI instance
115  * @budget : budget to be registered with the NAPI instance
116  * @scale  : scale factor on the weight (to scaler budget to 1000)
117  * @flags  : feature flags
118  *
119  * Description:
120  *    Creates NAPI instances. This function is called
121  *    unconditionally during initialization. It creates
122  *    napi structures through the proper HTC/HIF calls.
123  *    The structures are disabled on creation.
124  *    Note that for each NAPI instance a separate dummy netdev is used
125  *
126  * Return:
127  * < 0: error
128  * = 0: <should never happen>
129  * > 0: id of the created object (for multi-NAPI, number of objects created)
130  */
131 int hif_napi_create(struct hif_opaque_softc   *hif_ctx,
132 		    int (*poll)(struct napi_struct *, int),
133 		    int                budget,
134 		    int                scale,
135 		    uint8_t            flags)
136 {
137 	int i;
138 	struct qca_napi_data *napid;
139 	struct qca_napi_info *napii;
140 	struct CE_state      *ce_state;
141 	struct hif_softc *hif = HIF_GET_SOFTC(hif_ctx);
142 	int    rc = 0;
143 
144 	NAPI_DEBUG("-->(budget=%d, scale=%d)",
145 		   budget, scale);
146 	NAPI_DEBUG("hif->napi_data.state = 0x%08x",
147 		   hif->napi_data.state);
148 	NAPI_DEBUG("hif->napi_data.ce_map = 0x%08x",
149 		   hif->napi_data.ce_map);
150 
151 	napid = &(hif->napi_data);
152 	if (0 == (napid->state &  HIF_NAPI_INITED)) {
153 		memset(napid, 0, sizeof(struct qca_napi_data));
154 		qdf_spinlock_create(&(napid->lock));
155 
156 		napid->state |= HIF_NAPI_INITED;
157 		napid->flags = flags;
158 
159 		rc = hif_napi_cpu_init(hif_ctx);
160 		if (rc != 0 && rc != -EALREADY) {
161 			hif_err("NAPI_initialization failed(rc=%d)", rc);
162 			rc = napid->ce_map;
163 			goto hnc_err;
164 		} else
165 			rc = 0;
166 
167 		hif_debug("NAPI structures initialized, rc=%d", rc);
168 	}
169 	for (i = 0; i < hif->ce_count; i++) {
170 		ce_state = hif->ce_id_to_state[i];
171 		NAPI_DEBUG("ce %d: htt_rx=%d htt_tx=%d",
172 			   i, ce_state->htt_rx_data,
173 			   ce_state->htt_tx_data);
174 		if (ce_srng_based(hif))
175 			continue;
176 
177 		if (!ce_state->htt_rx_data)
178 			continue;
179 
180 		/* Now this is a CE where we need NAPI on */
181 		NAPI_DEBUG("Creating NAPI on pipe %d", i);
182 		napii = qdf_mem_malloc(sizeof(*napii));
183 		napid->napis[i] = napii;
184 		if (!napii) {
185 			rc = -ENOMEM;
186 			goto napii_free;
187 		}
188 	}
189 
190 	for (i = 0; i < hif->ce_count; i++) {
191 		napii = napid->napis[i];
192 		if (!napii)
193 			continue;
194 
195 		NAPI_DEBUG("initializing NAPI for pipe %d", i);
196 		memset(napii, 0, sizeof(struct qca_napi_info));
197 		napii->scale = scale;
198 		napii->id    = NAPI_PIPE2ID(i);
199 		napii->hif_ctx = hif_ctx;
200 		napii->irq   = pld_get_irq(hif->qdf_dev->dev, i);
201 
202 		if (napii->irq < 0)
203 			hif_warn("bad IRQ value for CE %d: %d", i, napii->irq);
204 
205 		qdf_net_if_create_dummy_if((struct qdf_net_if *)&napii->netdev);
206 
207 		NAPI_DEBUG("adding napi=%pK to netdev=%pK (poll=%pK, bdgt=%d)",
208 			   &(napii->napi), &(napii->netdev), poll, budget);
209 		netif_napi_add(&(napii->netdev), &(napii->napi), poll, budget);
210 
211 		NAPI_DEBUG("after napi_add");
212 		NAPI_DEBUG("napi=0x%pK, netdev=0x%pK",
213 			   &(napii->napi), &(napii->netdev));
214 		NAPI_DEBUG("napi.dev_list.prev=0x%pK, next=0x%pK",
215 			   napii->napi.dev_list.prev,
216 			   napii->napi.dev_list.next);
217 		NAPI_DEBUG("dev.napi_list.prev=0x%pK, next=0x%pK",
218 			   napii->netdev.napi_list.prev,
219 			   napii->netdev.napi_list.next);
220 
221 		hif_init_rx_thread_napi(napii);
222 		napii->lro_ctx = qdf_lro_init();
223 		NAPI_DEBUG("Registering LRO for ce_id %d NAPI callback for %d lro_ctx %pK\n",
224 				i, napii->id, napii->lro_ctx);
225 
226 		/* It is OK to change the state variable below without
227 		 * protection as there should be no-one around yet
228 		 */
229 		napid->ce_map |= (0x01 << i);
230 		hif_debug("NAPI id %d created for pipe %d", napii->id, i);
231 	}
232 
233 	/* no ces registered with the napi */
234 	if (!ce_srng_based(hif) && napid->ce_map == 0) {
235 		hif_warn("no napis created for copy engines");
236 		rc = -EFAULT;
237 		goto napii_free;
238 	}
239 
240 	NAPI_DEBUG("napi map = %x", napid->ce_map);
241 	NAPI_DEBUG("NAPI ids created for all applicable pipes");
242 	return napid->ce_map;
243 
244 napii_free:
245 	for (i = 0; i < hif->ce_count; i++) {
246 		napii = napid->napis[i];
247 		napid->napis[i] = NULL;
248 		if (napii)
249 			qdf_mem_free(napii);
250 	}
251 
252 hnc_err:
253 	NAPI_DEBUG("<--napi_instances_map=%x]", napid->ce_map);
254 	return rc;
255 }
256 qdf_export_symbol(hif_napi_create);
257 
258 #ifdef RECEIVE_OFFLOAD
259 void hif_napi_rx_offld_flush_cb_register(struct hif_opaque_softc *hif_hdl,
260 					 void (offld_flush_handler)(void *))
261 {
262 	int i;
263 	struct CE_state *ce_state;
264 	struct hif_softc *scn = HIF_GET_SOFTC(hif_hdl);
265 	struct qca_napi_data *napid;
266 	struct qca_napi_info *napii;
267 
268 	if (!scn) {
269 		hif_err("hif_state NULL!");
270 		QDF_ASSERT(0);
271 		return;
272 	}
273 
274 	napid = hif_napi_get_all(hif_hdl);
275 	for (i = 0; i < scn->ce_count; i++) {
276 		ce_state = scn->ce_id_to_state[i];
277 		if (ce_state && (ce_state->htt_rx_data)) {
278 			napii = napid->napis[i];
279 			napii->offld_flush_cb = offld_flush_handler;
280 			hif_debug("Registering offload for ce_id %d NAPI callback for %d flush_cb %pK",
281 				i, napii->id, napii->offld_flush_cb);
282 		}
283 	}
284 }
285 
286 void hif_napi_rx_offld_flush_cb_deregister(struct hif_opaque_softc *hif_hdl)
287 {
288 	int i;
289 	struct CE_state *ce_state;
290 	struct hif_softc *scn = HIF_GET_SOFTC(hif_hdl);
291 	struct qca_napi_data *napid;
292 	struct qca_napi_info *napii;
293 
294 	if (!scn) {
295 		hif_err("hif_state NULL!");
296 		QDF_ASSERT(0);
297 		return;
298 	}
299 
300 	napid = hif_napi_get_all(hif_hdl);
301 	for (i = 0; i < scn->ce_count; i++) {
302 		ce_state = scn->ce_id_to_state[i];
303 		if (ce_state && (ce_state->htt_rx_data)) {
304 			napii = napid->napis[i];
305 			hif_debug("deRegistering offld for ce_id %d NAPI callback for %d flush_cb %pK",
306 				 i, napii->id, napii->offld_flush_cb);
307 			/* Not required */
308 			napii->offld_flush_cb = NULL;
309 		}
310 	}
311 }
312 #endif /* RECEIVE_OFFLOAD */
313 
314 /**
315  *
316  * hif_napi_destroy() - destroys the NAPI structures for a given instance
317  * @hif   : pointer to hif context
318  * @ce_id : the CE id whose napi instance will be destroyed
319  * @force : if set, will destroy even if entry is active (de-activates)
320  *
321  * Description:
322  *    Destroy a given NAPI instance. This function is called
323  *    unconditionally during cleanup.
324  *    Refuses to destroy an entry of it is still enabled (unless force=1)
325  *    Marks the whole napi_data invalid if all instances are destroyed.
326  *
327  * Return:
328  * -EINVAL: specific entry has not been created
329  * -EPERM : specific entry is still active
330  * 0 <    : error
331  * 0 =    : success
332  */
333 int hif_napi_destroy(struct hif_opaque_softc *hif_ctx,
334 		     uint8_t          id,
335 		     int              force)
336 {
337 	uint8_t ce = NAPI_ID2PIPE(id);
338 	int rc = 0;
339 	struct hif_softc *hif = HIF_GET_SOFTC(hif_ctx);
340 
341 	NAPI_DEBUG("-->(id=%d, force=%d)", id, force);
342 
343 	if (0 == (hif->napi_data.state & HIF_NAPI_INITED)) {
344 		hif_err("NAPI not initialized or entry %d not created", id);
345 		rc = -EINVAL;
346 	} else if (0 == (hif->napi_data.ce_map & (0x01 << ce))) {
347 		hif_err("NAPI instance %d (pipe %d) not created", id, ce);
348 		if (hif->napi_data.napis[ce])
349 			hif_err("memory allocated but ce_map not set %d (pipe %d)",
350 				id, ce);
351 		rc = -EINVAL;
352 	} else {
353 		struct qca_napi_data *napid;
354 		struct qca_napi_info *napii;
355 
356 		napid = &(hif->napi_data);
357 		napii = napid->napis[ce];
358 		if (!napii) {
359 			if (napid->ce_map & (0x01 << ce))
360 				hif_err("napii & ce_map out of sync(ce %d)", ce);
361 			return -EINVAL;
362 		}
363 
364 
365 		if (hif->napi_data.state == HIF_NAPI_CONF_UP) {
366 			if (force) {
367 				napi_disable(&(napii->napi));
368 				hif_debug("NAPI entry %d force disabled", id);
369 				NAPI_DEBUG("NAPI %d force disabled", id);
370 			} else {
371 				hif_err("Cannot destroy active NAPI %d", id);
372 				rc = -EPERM;
373 			}
374 		}
375 		if (0 == rc) {
376 			NAPI_DEBUG("before napi_del");
377 			NAPI_DEBUG("napi.dlist.prv=0x%pK, next=0x%pK",
378 				  napii->napi.dev_list.prev,
379 				  napii->napi.dev_list.next);
380 			NAPI_DEBUG("dev.napi_l.prv=0x%pK, next=0x%pK",
381 				   napii->netdev.napi_list.prev,
382 				   napii->netdev.napi_list.next);
383 
384 			qdf_lro_deinit(napii->lro_ctx);
385 			netif_napi_del(&(napii->napi));
386 			hif_deinit_rx_thread_napi(napii);
387 
388 			napid->ce_map &= ~(0x01 << ce);
389 			napid->napis[ce] = NULL;
390 			napii->scale  = 0;
391 			qdf_mem_free(napii);
392 			hif_debug("NAPI %d destroyed", id);
393 
394 			/* if there are no active instances and
395 			 * if they are all destroyed,
396 			 * set the whole structure to uninitialized state
397 			 */
398 			if (napid->ce_map == 0) {
399 				rc = hif_napi_cpu_deinit(hif_ctx);
400 				/* caller is tolerant to receiving !=0 rc */
401 
402 				qdf_spinlock_destroy(&(napid->lock));
403 				memset(napid,
404 				       0, sizeof(struct qca_napi_data));
405 				hif_debug("no NAPI instances. Zapped");
406 			}
407 		}
408 	}
409 
410 	return rc;
411 }
412 qdf_export_symbol(hif_napi_destroy);
413 
414 #ifdef FEATURE_LRO
415 void *hif_napi_get_lro_info(struct hif_opaque_softc *hif_hdl, int napi_id)
416 {
417 	struct hif_softc *scn = HIF_GET_SOFTC(hif_hdl);
418 	struct qca_napi_data *napid;
419 	struct qca_napi_info *napii;
420 
421 	napid = &(scn->napi_data);
422 	napii = napid->napis[NAPI_ID2PIPE(napi_id)];
423 
424 	if (napii)
425 		return napii->lro_ctx;
426 	return 0;
427 }
428 #endif
429 
430 /**
431  *
432  * hif_napi_get_all() - returns the address of the whole HIF NAPI structure
433  * @hif: pointer to hif context
434  *
435  * Description:
436  *    Returns the address of the whole structure
437  *
438  * Return:
439  *  <addr>: address of the whole HIF NAPI structure
440  */
441 inline struct qca_napi_data *hif_napi_get_all(struct hif_opaque_softc *hif_ctx)
442 {
443 	struct hif_softc *hif = HIF_GET_SOFTC(hif_ctx);
444 
445 	return &(hif->napi_data);
446 }
447 
448 struct qca_napi_info *hif_get_napi(int napi_id, struct qca_napi_data *napid)
449 {
450 	int id = NAPI_ID2PIPE(napi_id);
451 
452 	return napid->napis[id];
453 }
454 
455 /**
456  *
457  * hif_napi_event() - reacts to events that impact NAPI
458  * @hif : pointer to hif context
459  * @evnt: event that has been detected
460  * @data: more data regarding the event
461  *
462  * Description:
463  *   This function handles two types of events:
464  *   1- Events that change the state of NAPI (enabled/disabled):
465  *      {NAPI_EVT_INI_FILE, NAPI_EVT_CMD_STATE}
466  *      The state is retrievable by "hdd_napi_enabled(-1)"
467  *    - NAPI will be on if either INI file is on and it has not been disabled
468  *                                by a subsequent vendor CMD,
469  *                         or     it has been enabled by a vendor CMD.
470  *   2- Events that change the CPU affinity of a NAPI instance/IRQ:
471  *      {NAPI_EVT_TPUT_STATE, NAPI_EVT_CPU_STATE}
472  *    - NAPI will support a throughput mode (HI/LO), kept at napid->napi_mode
473  *    - NAPI will switch throughput mode based on hdd_napi_throughput_policy()
474  *    - In LO tput mode, NAPI will yield control if its interrupts to the system
475  *      management functions. However in HI throughput mode, NAPI will actively
476  *      manage its interrupts/instances (by trying to disperse them out to
477  *      separate performance cores).
478  *    - CPU eligibility is kept up-to-date by NAPI_EVT_CPU_STATE events.
479  *
480  *    + In some cases (roaming peer management is the only case so far), a
481  *      a client can trigger a "SERIALIZE" event. Basically, this means that the
482  *      users is asking NAPI to go into a truly single execution context state.
483  *      So, NAPI indicates to msm-irqbalancer that it wants to be blacklisted,
484  *      (if called for the first time) and then moves all IRQs (for NAPI
485  *      instances) to be collapsed to a single core. If called multiple times,
486  *      it will just re-collapse the CPUs. This is because blacklist-on() API
487  *      is reference-counted, and because the API has already been called.
488  *
489  *      Such a user, should call "DESERIALIZE" (NORMAL) event, to set NAPI to go
490  *      to its "normal" operation. Optionally, they can give a timeout value (in
491  *      multiples of BusBandwidthCheckPeriod -- 100 msecs by default). In this
492  *      case, NAPI will just set the current throughput state to uninitialized
493  *      and set the delay period. Once policy handler is called, it would skip
494  *      applying the policy delay period times, and otherwise apply the policy.
495  *
496  * Return:
497  *  < 0: some error
498  *  = 0: event handled successfully
499  */
500 int hif_napi_event(struct hif_opaque_softc *hif_ctx, enum qca_napi_event event,
501 		   void *data)
502 {
503 	int      rc = 0;
504 	uint32_t prev_state;
505 	int      i;
506 	bool state_changed;
507 	struct napi_struct *napi;
508 	struct hif_softc *hif = HIF_GET_SOFTC(hif_ctx);
509 	struct qca_napi_data *napid = &(hif->napi_data);
510 	enum qca_napi_tput_state tput_mode = QCA_NAPI_TPUT_UNINITIALIZED;
511 	enum {
512 		BLACKLIST_NOT_PENDING,
513 		BLACKLIST_ON_PENDING,
514 		BLACKLIST_OFF_PENDING
515 	     } blacklist_pending = BLACKLIST_NOT_PENDING;
516 
517 	NAPI_DEBUG("%s: -->(event=%d, aux=%pK)", __func__, event, data);
518 
519 	if (ce_srng_based(hif))
520 		return hif_exec_event(hif_ctx, event, data);
521 
522 	if ((napid->state & HIF_NAPI_INITED) == 0) {
523 		NAPI_DEBUG("%s: got event when NAPI not initialized",
524 			   __func__);
525 		return -EINVAL;
526 	}
527 	qdf_spin_lock_bh(&(napid->lock));
528 	prev_state = napid->state;
529 	switch (event) {
530 	case NAPI_EVT_INI_FILE:
531 	case NAPI_EVT_CMD_STATE:
532 	case NAPI_EVT_INT_STATE: {
533 		int on = (data != ((void *)0));
534 
535 		hif_debug("recved evnt: STATE_CMD %d; v = %d (state=0x%0x)",
536 			 event, on, prev_state);
537 		if (on)
538 			if (prev_state & HIF_NAPI_CONF_UP) {
539 				hif_debug("Duplicate NAPI conf ON msg");
540 			} else {
541 				hif_debug("Setting state to ON");
542 				napid->state |= HIF_NAPI_CONF_UP;
543 			}
544 		else /* off request */
545 			if (prev_state & HIF_NAPI_CONF_UP) {
546 				hif_debug("Setting state to OFF");
547 				napid->state &= ~HIF_NAPI_CONF_UP;
548 			} else {
549 				hif_debug("Duplicate NAPI conf OFF msg");
550 			}
551 		break;
552 	}
553 	/* case NAPI_INIT_FILE/CMD_STATE */
554 
555 	case NAPI_EVT_CPU_STATE: {
556 		int cpu = ((unsigned long int)data >> 16);
557 		int val = ((unsigned long int)data & 0x0ff);
558 
559 		NAPI_DEBUG("%s: evt=CPU_STATE on CPU %d value=%d",
560 			   __func__, cpu, val);
561 
562 		/* state has already been set by hnc_cpu_notify_cb */
563 		if ((val == QCA_NAPI_CPU_DOWN) &&
564 		    (napid->napi_mode == QCA_NAPI_TPUT_HI) && /* we manage */
565 		    (napid->napi_cpu[cpu].napis != 0)) {
566 			NAPI_DEBUG("%s: Migrating NAPIs out of cpu %d",
567 				   __func__, cpu);
568 			rc = hif_napi_cpu_migrate(napid,
569 						  cpu,
570 						  HNC_ACT_RELOCATE);
571 			napid->napi_cpu[cpu].napis = 0;
572 		}
573 		/* in QCA_NAPI_TPUT_LO case, napis MUST == 0 */
574 		break;
575 	}
576 
577 	case NAPI_EVT_TPUT_STATE: {
578 		tput_mode = (enum qca_napi_tput_state)data;
579 		if (tput_mode == QCA_NAPI_TPUT_LO) {
580 			/* from TPUT_HI -> TPUT_LO */
581 			NAPI_DEBUG("%s: Moving to napi_tput_LO state",
582 				   __func__);
583 			blacklist_pending = BLACKLIST_OFF_PENDING;
584 			/*
585 			 * Ideally we should "collapse" interrupts here, since
586 			 * we are "dispersing" interrupts in the "else" case.
587 			 * This allows the possibility that our interrupts may
588 			 * still be on the perf cluster the next time we enter
589 			 * high tput mode. However, the irq_balancer is free
590 			 * to move our interrupts to power cluster once
591 			 * blacklisting has been turned off in the "else" case.
592 			 */
593 		} else {
594 			/* from TPUT_LO -> TPUT->HI */
595 			NAPI_DEBUG("%s: Moving to napi_tput_HI state",
596 				   __func__);
597 			rc = hif_napi_cpu_migrate(napid,
598 						  HNC_ANY_CPU,
599 						  HNC_ACT_DISPERSE);
600 
601 			blacklist_pending = BLACKLIST_ON_PENDING;
602 		}
603 		napid->napi_mode = tput_mode;
604 		break;
605 	}
606 
607 	case NAPI_EVT_USR_SERIAL: {
608 		unsigned long users = (unsigned long)data;
609 
610 		NAPI_DEBUG("%s: User forced SERIALIZATION; users=%ld",
611 			   __func__, users);
612 
613 		rc = hif_napi_cpu_migrate(napid,
614 					  HNC_ANY_CPU,
615 					  HNC_ACT_COLLAPSE);
616 		if ((users == 0) && (rc == 0))
617 			blacklist_pending = BLACKLIST_ON_PENDING;
618 		break;
619 	}
620 	case NAPI_EVT_USR_NORMAL: {
621 		NAPI_DEBUG("%s: User forced DE-SERIALIZATION", __func__);
622 		if (!napid->user_cpu_affin_mask)
623 			blacklist_pending = BLACKLIST_OFF_PENDING;
624 		/*
625 		 * Deserialization timeout is handled at hdd layer;
626 		 * just mark current mode to uninitialized to ensure
627 		 * it will be set when the delay is over
628 		 */
629 		napid->napi_mode = QCA_NAPI_TPUT_UNINITIALIZED;
630 		break;
631 	}
632 	default: {
633 		hif_err("Unknown event: %d (data=0x%0lx)",
634 			event, (unsigned long) data);
635 		break;
636 	} /* default */
637 	}; /* switch */
638 
639 
640 	switch (blacklist_pending) {
641 	case BLACKLIST_ON_PENDING:
642 		/* assume the control of WLAN IRQs */
643 		hif_napi_cpu_blacklist(napid, BLACKLIST_ON);
644 		break;
645 	case BLACKLIST_OFF_PENDING:
646 		/* yield the control of WLAN IRQs */
647 		hif_napi_cpu_blacklist(napid, BLACKLIST_OFF);
648 		break;
649 	default: /* nothing to do */
650 		break;
651 	} /* switch blacklist_pending */
652 
653 	/* we want to perform the comparison in lock:
654 	 * there is a possiblity of hif_napi_event get called
655 	 * from two different contexts (driver unload and cpu hotplug
656 	 * notification) and napid->state get changed
657 	 * in driver unload context and can lead to race condition
658 	 * in cpu hotplug context. Therefore, perform the napid->state
659 	 * comparison before releasing lock.
660 	 */
661 	state_changed = (prev_state != napid->state);
662 	qdf_spin_unlock_bh(&(napid->lock));
663 
664 	if (state_changed) {
665 		if (napid->state == ENABLE_NAPI_MASK) {
666 			rc = 1;
667 			for (i = 0; i < CE_COUNT_MAX; i++) {
668 				struct qca_napi_info *napii = napid->napis[i];
669 				if (napii) {
670 					napi = &(napii->napi);
671 					NAPI_DEBUG("%s: enabling NAPI %d",
672 						   __func__, i);
673 					napi_enable(napi);
674 				}
675 			}
676 		} else {
677 			rc = 0;
678 			for (i = 0; i < CE_COUNT_MAX; i++) {
679 				struct qca_napi_info *napii = napid->napis[i];
680 				if (napii) {
681 					napi = &(napii->napi);
682 					NAPI_DEBUG("%s: disabling NAPI %d",
683 						   __func__, i);
684 					napi_disable(napi);
685 					/* in case it is affined, remove it */
686 					qdf_dev_set_irq_affinity(napii->irq,
687 								 NULL);
688 				}
689 			}
690 		}
691 	} else {
692 		hif_debug("no change in hif napi state (still %d)", prev_state);
693 	}
694 
695 	NAPI_DEBUG("<--[rc=%d]", rc);
696 	return rc;
697 }
698 qdf_export_symbol(hif_napi_event);
699 
700 /**
701  * hif_napi_enabled() - checks whether NAPI is enabled for given ce or not
702  * @hif: hif context
703  * @ce : CE instance (or -1, to check if any CEs are enabled)
704  *
705  * Return: bool
706  */
707 int hif_napi_enabled(struct hif_opaque_softc *hif_ctx, int ce)
708 {
709 	int rc;
710 	struct hif_softc *hif = HIF_GET_SOFTC(hif_ctx);
711 
712 	if (-1 == ce)
713 		rc = ((hif->napi_data.state == ENABLE_NAPI_MASK));
714 	else
715 		rc = ((hif->napi_data.state == ENABLE_NAPI_MASK) &&
716 		      (hif->napi_data.ce_map & (0x01 << ce)));
717 	return rc;
718 }
719 qdf_export_symbol(hif_napi_enabled);
720 
721 /**
722  * hif_napi_created() - checks whether NAPI is created for given ce or not
723  * @hif: hif context
724  * @ce : CE instance
725  *
726  * Return: bool
727  */
728 bool hif_napi_created(struct hif_opaque_softc *hif_ctx, int ce)
729 {
730 	int rc;
731 	struct hif_softc *hif = HIF_GET_SOFTC(hif_ctx);
732 
733 	rc = (hif->napi_data.ce_map & (0x01 << ce));
734 
735 	return !!rc;
736 }
737 qdf_export_symbol(hif_napi_created);
738 
739 /**
740  * hif_napi_enable_irq() - enables bus interrupts after napi_complete
741  *
742  * @hif: hif context
743  * @id : id of NAPI instance calling this (used to determine the CE)
744  *
745  * Return: void
746  */
747 inline void hif_napi_enable_irq(struct hif_opaque_softc *hif, int id)
748 {
749 	struct hif_softc *scn = HIF_GET_SOFTC(hif);
750 
751 	hif_irq_enable(scn, NAPI_ID2PIPE(id));
752 }
753 
754 
755 /**
756  * hif_napi_schedule() - schedules napi, updates stats
757  * @scn:  hif context
758  * @ce_id: index of napi instance
759  *
760  * Return: false if napi didn't enable or already scheduled, otherwise true
761  */
762 bool hif_napi_schedule(struct hif_opaque_softc *hif_ctx, int ce_id)
763 {
764 	int cpu = smp_processor_id();
765 	struct hif_softc *scn = HIF_GET_SOFTC(hif_ctx);
766 	struct qca_napi_info *napii;
767 
768 	napii = scn->napi_data.napis[ce_id];
769 	if (qdf_unlikely(!napii)) {
770 		hif_err("scheduling unallocated napi (ce:%d)", ce_id);
771 		qdf_atomic_dec(&scn->active_tasklet_cnt);
772 		return false;
773 	}
774 
775 	if (test_bit(NAPI_STATE_SCHED, &napii->napi.state)) {
776 		NAPI_DEBUG("napi scheduled, return");
777 		qdf_atomic_dec(&scn->active_tasklet_cnt);
778 		return false;
779 	}
780 
781 	hif_record_ce_desc_event(scn,  ce_id, NAPI_SCHEDULE,
782 				 NULL, NULL, 0, 0);
783 	napii->stats[cpu].napi_schedules++;
784 	NAPI_DEBUG("scheduling napi %d (ce:%d)", napii->id, ce_id);
785 	napi_schedule(&(napii->napi));
786 
787 	return true;
788 }
789 qdf_export_symbol(hif_napi_schedule);
790 
791 /**
792  * hif_napi_correct_cpu() - correct the interrupt affinity for napi if needed
793  * @napi_info: pointer to qca_napi_info for the napi instance
794  *
795  * Return: true  => interrupt already on correct cpu, no correction needed
796  *         false => interrupt on wrong cpu, correction done for cpu affinity
797  *                   of the interrupt
798  */
799 static inline
800 bool hif_napi_correct_cpu(struct qca_napi_info *napi_info)
801 {
802 	bool right_cpu = true;
803 	int rc = 0;
804 	int cpu;
805 	struct qca_napi_data *napid;
806 	QDF_STATUS ret;
807 
808 	napid = hif_napi_get_all(GET_HIF_OPAQUE_HDL(napi_info->hif_ctx));
809 
810 	if (napid->flags & QCA_NAPI_FEATURE_CPU_CORRECTION) {
811 
812 		cpu = qdf_get_cpu();
813 		if (unlikely((hif_napi_cpu_blacklist(napid,
814 						BLACKLIST_QUERY) > 0) &&
815 						(cpu != napi_info->cpu))) {
816 			right_cpu = false;
817 
818 			NAPI_DEBUG("interrupt on wrong CPU, correcting");
819 			napi_info->cpumask.bits[0] = (0x01 << napi_info->cpu);
820 
821 			qdf_dev_modify_irq_status(napi_info->irq,
822 						  QDF_IRQ_NO_BALANCING, 0);
823 			ret = qdf_dev_set_irq_affinity(napi_info->irq,
824 						       (struct qdf_cpu_mask *)
825 						       &napi_info->cpumask);
826 			rc = qdf_status_to_os_return(ret);
827 			qdf_dev_modify_irq_status(napi_info->irq, 0,
828 						  QDF_IRQ_NO_BALANCING);
829 
830 			if (rc)
831 				hif_err("Setting irq affinity hint: %d", rc);
832 			else
833 				napi_info->stats[cpu].cpu_corrected++;
834 		}
835 	}
836 	return right_cpu;
837 }
838 
839 #ifdef RECEIVE_OFFLOAD
840 /**
841  * hif_napi_offld_flush_cb() - Call upper layer flush callback
842  * @napi_info: Handle to hif_napi_info
843  *
844  * Return: None
845  */
846 static void hif_napi_offld_flush_cb(struct qca_napi_info *napi_info)
847 {
848 	if (napi_info->offld_flush_cb)
849 		napi_info->offld_flush_cb(napi_info);
850 }
851 #else
852 static void hif_napi_offld_flush_cb(struct qca_napi_info *napi_info)
853 {
854 }
855 #endif
856 
857 /**
858  * hif_napi_poll() - NAPI poll routine
859  * @napi  : pointer to NAPI struct as kernel holds it
860  * @budget:
861  *
862  * This is the body of the poll function.
863  * The poll function is called by kernel. So, there is a wrapper
864  * function in HDD, which in turn calls this function.
865  * Two main reasons why the whole thing is not implemented in HDD:
866  * a) references to things like ce_service that HDD is not aware of
867  * b) proximity to the implementation of ce_tasklet, which the body
868  *    of this function should be very close to.
869  *
870  * NOTE TO THE MAINTAINER:
871  *  Consider this function and ce_tasklet very tightly coupled pairs.
872  *  Any changes to ce_tasklet or this function may likely need to be
873  *  reflected in the counterpart.
874  *
875  * Returns:
876  *  int: the amount of work done in this poll (<= budget)
877  */
878 int hif_napi_poll(struct hif_opaque_softc *hif_ctx,
879 		  struct napi_struct *napi,
880 		  int budget)
881 {
882 	int    rc = 0; /* default: no work done, also takes care of error */
883 	int    normalized = 0;
884 	int    bucket;
885 	int    cpu = smp_processor_id();
886 	bool poll_on_right_cpu;
887 	struct hif_softc      *hif = HIF_GET_SOFTC(hif_ctx);
888 	struct qca_napi_info *napi_info;
889 	struct CE_state *ce_state = NULL;
890 
891 	if (unlikely(!hif)) {
892 		hif_err("hif context is NULL");
893 		QDF_ASSERT(0);
894 		goto out;
895 	}
896 
897 	napi_info = (struct qca_napi_info *)
898 		container_of(napi, struct qca_napi_info, napi);
899 
900 	NAPI_DEBUG("%s -->(napi(%d, irq=%d), budget=%d)",
901 		   __func__, napi_info->id, napi_info->irq, budget);
902 
903 	napi_info->stats[cpu].napi_polls++;
904 
905 	hif_record_ce_desc_event(hif, NAPI_ID2PIPE(napi_info->id),
906 				 NAPI_POLL_ENTER, NULL, NULL, cpu, 0);
907 
908 	rc = ce_per_engine_service(hif, NAPI_ID2PIPE(napi_info->id));
909 	NAPI_DEBUG("%s: ce_per_engine_service processed %d msgs",
910 		    __func__, rc);
911 
912 	hif_napi_offld_flush_cb(napi_info);
913 
914 	/* do not return 0, if there was some work done,
915 	 * even if it is below the scale
916 	 */
917 	if (rc) {
918 		napi_info->stats[cpu].napi_workdone += rc;
919 		normalized = (rc / napi_info->scale);
920 		if (normalized == 0)
921 			normalized++;
922 		bucket = (normalized - 1) /
923 				(QCA_NAPI_BUDGET / QCA_NAPI_NUM_BUCKETS);
924 		if (bucket >= QCA_NAPI_NUM_BUCKETS) {
925 			bucket = QCA_NAPI_NUM_BUCKETS - 1;
926 			hif_err("Bad bucket#(%d) > QCA_NAPI_NUM_BUCKETS(%d)"
927 				" normalized %d, napi budget %d",
928 				bucket, QCA_NAPI_NUM_BUCKETS,
929 				normalized, QCA_NAPI_BUDGET);
930 		}
931 		napi_info->stats[cpu].napi_budget_uses[bucket]++;
932 	} else {
933 	/* if ce_per engine reports 0, then poll should be terminated */
934 		NAPI_DEBUG("%s:%d: nothing processed by CE. Completing NAPI",
935 			   __func__, __LINE__);
936 	}
937 
938 	ce_state = hif->ce_id_to_state[NAPI_ID2PIPE(napi_info->id)];
939 
940 	/*
941 	 * Not using the API hif_napi_correct_cpu directly in the if statement
942 	 * below since the API may not get evaluated if put at the end if any
943 	 * prior condition would evaluate to be true. The CPU correction
944 	 * check should kick in every poll.
945 	 */
946 #ifdef NAPI_YIELD_BUDGET_BASED
947 	if (ce_state && (ce_state->force_break || 0 == rc)) {
948 #else
949 	poll_on_right_cpu = hif_napi_correct_cpu(napi_info);
950 	if ((ce_state) &&
951 	    (!ce_check_rx_pending(ce_state) || (0 == rc) ||
952 	     !poll_on_right_cpu)) {
953 #endif
954 		napi_info->stats[cpu].napi_completes++;
955 #ifdef NAPI_YIELD_BUDGET_BASED
956 		ce_state->force_break = 0;
957 #endif
958 
959 		hif_record_ce_desc_event(hif, ce_state->id, NAPI_COMPLETE,
960 					 NULL, NULL, 0, 0);
961 		if (normalized >= budget)
962 			normalized = budget - 1;
963 
964 		napi_complete(napi);
965 		/* enable interrupts */
966 		hif_napi_enable_irq(hif_ctx, napi_info->id);
967 		/* support suspend/resume */
968 		qdf_atomic_dec(&(hif->active_tasklet_cnt));
969 
970 		NAPI_DEBUG("%s:%d: napi_complete + enabling the interrupts",
971 			   __func__, __LINE__);
972 	} else {
973 		/* 4.4 kernel NAPI implementation requires drivers to
974 		 * return full work when they ask to be re-scheduled,
975 		 * or napi_complete and re-start with a fresh interrupt
976 		 */
977 		normalized = budget;
978 	}
979 
980 	hif_record_ce_desc_event(hif, NAPI_ID2PIPE(napi_info->id),
981 				 NAPI_POLL_EXIT, NULL, NULL, normalized, 0);
982 
983 	NAPI_DEBUG("%s <--[normalized=%d]", __func__, normalized);
984 	return normalized;
985 out:
986 	return rc;
987 }
988 qdf_export_symbol(hif_napi_poll);
989 
990 void hif_update_napi_max_poll_time(struct CE_state *ce_state,
991 				   int ce_id,
992 				   int cpu_id)
993 {
994 	struct hif_softc *hif;
995 	struct qca_napi_info *napi_info;
996 	unsigned long long napi_poll_time = qdf_time_sched_clock() -
997 					ce_state->ce_service_start_time;
998 
999 	hif = ce_state->scn;
1000 	napi_info = hif->napi_data.napis[ce_id];
1001 	if (napi_poll_time >
1002 			napi_info->stats[cpu_id].napi_max_poll_time)
1003 		napi_info->stats[cpu_id].napi_max_poll_time = napi_poll_time;
1004 }
1005 qdf_export_symbol(hif_update_napi_max_poll_time);
1006 
1007 #ifdef HIF_IRQ_AFFINITY
1008 /**
1009  *
1010  * hif_napi_update_yield_stats() - update NAPI yield related stats
1011  * @cpu_id: CPU ID for which stats needs to be updates
1012  * @ce_id: Copy Engine ID for which yield stats needs to be updates
1013  * @time_limit_reached: indicates whether the time limit was reached
1014  * @rxpkt_thresh_reached: indicates whether rx packet threshold was reached
1015  *
1016  * Return: None
1017  */
1018 void hif_napi_update_yield_stats(struct CE_state *ce_state,
1019 				 bool time_limit_reached,
1020 				 bool rxpkt_thresh_reached)
1021 {
1022 	struct hif_softc *hif;
1023 	struct qca_napi_data *napi_data = NULL;
1024 	int ce_id = 0;
1025 	int cpu_id = 0;
1026 
1027 	if (unlikely(!ce_state)) {
1028 		QDF_ASSERT(ce_state);
1029 		return;
1030 	}
1031 
1032 	hif = ce_state->scn;
1033 
1034 	if (unlikely(!hif)) {
1035 		QDF_ASSERT(hif);
1036 		return;
1037 	}
1038 	napi_data = &(hif->napi_data);
1039 	if (unlikely(!napi_data)) {
1040 		QDF_ASSERT(napi_data);
1041 		return;
1042 	}
1043 
1044 	ce_id = ce_state->id;
1045 	cpu_id = qdf_get_cpu();
1046 
1047 	if (unlikely(!napi_data->napis[ce_id])) {
1048 		return;
1049 	}
1050 
1051 	if (time_limit_reached)
1052 		napi_data->napis[ce_id]->stats[cpu_id].time_limit_reached++;
1053 	else
1054 		napi_data->napis[ce_id]->stats[cpu_id].rxpkt_thresh_reached++;
1055 
1056 	hif_update_napi_max_poll_time(ce_state, ce_id,
1057 				      cpu_id);
1058 }
1059 
1060 /**
1061  *
1062  * hif_napi_stats() - display NAPI CPU statistics
1063  * @napid: pointer to qca_napi_data
1064  *
1065  * Description:
1066  *    Prints the various CPU cores on which the NAPI instances /CEs interrupts
1067  *    are being executed. Can be called from outside NAPI layer.
1068  *
1069  * Return: None
1070  */
1071 void hif_napi_stats(struct qca_napi_data *napid)
1072 {
1073 	int i;
1074 	struct qca_napi_cpu *cpu;
1075 
1076 	if (!napid) {
1077 		qdf_debug("%s: napiid struct is null", __func__);
1078 		return;
1079 	}
1080 
1081 	cpu = napid->napi_cpu;
1082 	qdf_debug("NAPI CPU TABLE");
1083 	qdf_debug("lilclhead=%d, bigclhead=%d",
1084 		  napid->lilcl_head, napid->bigcl_head);
1085 	for (i = 0; i < NR_CPUS; i++) {
1086 		qdf_debug("CPU[%02d]: state:%d crid=%02d clid=%02d crmk:0x%0lx thmk:0x%0lx frq:%d napi = 0x%08x lnk:%d",
1087 			  i,
1088 			  cpu[i].state, cpu[i].core_id, cpu[i].cluster_id,
1089 			  cpu[i].core_mask.bits[0],
1090 			  cpu[i].thread_mask.bits[0],
1091 			  cpu[i].max_freq, cpu[i].napis,
1092 			  cpu[i].cluster_nxt);
1093 	}
1094 }
1095 
1096 #ifdef FEATURE_NAPI_DEBUG
1097 /*
1098  * Local functions
1099  * - no argument checks, all internal/trusted callers
1100  */
1101 static void hnc_dump_cpus(struct qca_napi_data *napid)
1102 {
1103 	hif_napi_stats(napid);
1104 }
1105 #else
1106 static void hnc_dump_cpus(struct qca_napi_data *napid) { /* no-op */ };
1107 #endif /* FEATURE_NAPI_DEBUG */
1108 /**
1109  * hnc_link_clusters() - partitions to cpu table into clusters
1110  * @napid: pointer to NAPI data
1111  *
1112  * Takes in a CPU topology table and builds two linked lists
1113  * (big cluster cores, list-head at bigcl_head, and little cluster
1114  * cores, list-head at lilcl_head) out of it.
1115  *
1116  * If there are more than two clusters:
1117  * - bigcl_head and lilcl_head will be different,
1118  * - the cluster with highest cpufreq will be considered the "big" cluster.
1119  *   If there are more than one with the highest frequency, the *last* of such
1120  *   clusters will be designated as the "big cluster"
1121  * - the cluster with lowest cpufreq will be considered the "li'l" cluster.
1122  *   If there are more than one clusters with the lowest cpu freq, the *first*
1123  *   of such clusters will be designated as the "little cluster"
1124  * - We only support up to 32 clusters
1125  * Return: 0 : OK
1126  *         !0: error (at least one of lil/big clusters could not be found)
1127  */
1128 #define HNC_MIN_CLUSTER 0
1129 #define HNC_MAX_CLUSTER 1
1130 static int hnc_link_clusters(struct qca_napi_data *napid)
1131 {
1132 	int rc = 0;
1133 
1134 	int i;
1135 	int it = 0;
1136 	uint32_t cl_done = 0x0;
1137 	int cl, curcl, curclhead = 0;
1138 	int more;
1139 	unsigned int lilfrq = INT_MAX;
1140 	unsigned int bigfrq = 0;
1141 	unsigned int clfrq = 0;
1142 	int prev = 0;
1143 	struct qca_napi_cpu *cpus = napid->napi_cpu;
1144 
1145 	napid->lilcl_head = napid->bigcl_head = -1;
1146 
1147 	do {
1148 		more = 0;
1149 		it++; curcl = -1;
1150 		for (i = 0; i < NR_CPUS; i++) {
1151 			cl = cpus[i].cluster_id;
1152 			NAPI_DEBUG("Processing cpu[%d], cluster=%d\n",
1153 				   i, cl);
1154 			if ((cl < HNC_MIN_CLUSTER) || (cl > HNC_MAX_CLUSTER)) {
1155 				NAPI_DEBUG("Bad cluster (%d). SKIPPED\n", cl);
1156 				/* continue if ASSERTs are disabled */
1157 				continue;
1158 			};
1159 			if (cpumask_weight(&(cpus[i].core_mask)) == 0) {
1160 				NAPI_DEBUG("Core mask 0. SKIPPED\n");
1161 				continue;
1162 			}
1163 			if (cl_done & (0x01 << cl)) {
1164 				NAPI_DEBUG("Cluster already processed. SKIPPED\n");
1165 				continue;
1166 			} else {
1167 				if (more == 0) {
1168 					more = 1;
1169 					curcl = cl;
1170 					curclhead = i; /* row */
1171 					clfrq = cpus[i].max_freq;
1172 					prev = -1;
1173 				};
1174 				if ((curcl >= 0) && (curcl != cl)) {
1175 					NAPI_DEBUG("Entry cl(%d) != curcl(%d). SKIPPED\n",
1176 						   cl, curcl);
1177 					continue;
1178 				}
1179 				if (cpus[i].max_freq != clfrq)
1180 					NAPI_DEBUG("WARN: frq(%d)!=clfrq(%d)\n",
1181 						   cpus[i].max_freq, clfrq);
1182 				if (clfrq >= bigfrq) {
1183 					bigfrq = clfrq;
1184 					napid->bigcl_head  = curclhead;
1185 					NAPI_DEBUG("bigcl=%d\n", curclhead);
1186 				}
1187 				if (clfrq < lilfrq) {
1188 					lilfrq = clfrq;
1189 					napid->lilcl_head = curclhead;
1190 					NAPI_DEBUG("lilcl=%d\n", curclhead);
1191 				}
1192 				if (prev != -1)
1193 					cpus[prev].cluster_nxt = i;
1194 
1195 				prev = i;
1196 			}
1197 		}
1198 		if (curcl >= 0)
1199 			cl_done |= (0x01 << curcl);
1200 
1201 	} while (more);
1202 
1203 	if (qdf_unlikely((napid->lilcl_head < 0) && (napid->bigcl_head < 0)))
1204 		rc = -EFAULT;
1205 
1206 	hnc_dump_cpus(napid); /* if NAPI_DEBUG */
1207 	return rc;
1208 }
1209 #undef HNC_MIN_CLUSTER
1210 #undef HNC_MAX_CLUSTER
1211 
1212 /*
1213  * hotplug function group
1214  */
1215 
1216 /**
1217  * hnc_cpu_online_cb() - handles CPU hotplug "up" events
1218  * @context: the associated HIF context
1219  * @cpu: the CPU Id of the CPU the event happened on
1220  *
1221  * Return: None
1222  */
1223 static void hnc_cpu_online_cb(void *context, uint32_t cpu)
1224 {
1225 	struct hif_softc *hif = context;
1226 	struct qca_napi_data *napid = &hif->napi_data;
1227 
1228 	if (cpu >= NR_CPUS)
1229 		return;
1230 
1231 	NAPI_DEBUG("-->%s(act=online, cpu=%u)", __func__, cpu);
1232 
1233 	napid->napi_cpu[cpu].state = QCA_NAPI_CPU_UP;
1234 	NAPI_DEBUG("%s: CPU %u marked %d",
1235 		   __func__, cpu, napid->napi_cpu[cpu].state);
1236 
1237 	NAPI_DEBUG("<--%s", __func__);
1238 }
1239 
1240 /**
1241  * hnc_cpu_before_offline_cb() - handles CPU hotplug "prepare down" events
1242  * @context: the associated HIF context
1243  * @cpu: the CPU Id of the CPU the event happened on
1244  *
1245  * On transtion to offline, we act on PREP events, because we may need to move
1246  * the irqs/NAPIs to another CPU before it is actually off-lined.
1247  *
1248  * Return: None
1249  */
1250 static void hnc_cpu_before_offline_cb(void *context, uint32_t cpu)
1251 {
1252 	struct hif_softc *hif = context;
1253 	struct qca_napi_data *napid = &hif->napi_data;
1254 
1255 	if (cpu >= NR_CPUS)
1256 		return;
1257 
1258 	NAPI_DEBUG("-->%s(act=before_offline, cpu=%u)", __func__, cpu);
1259 
1260 	napid->napi_cpu[cpu].state = QCA_NAPI_CPU_DOWN;
1261 
1262 	NAPI_DEBUG("%s: CPU %u marked %d; updating affinity",
1263 		   __func__, cpu, napid->napi_cpu[cpu].state);
1264 
1265 	/**
1266 	 * we need to move any NAPIs on this CPU out.
1267 	 * if we are in LO throughput mode, then this is valid
1268 	 * if the CPU is the the low designated CPU.
1269 	 */
1270 	hif_napi_event(GET_HIF_OPAQUE_HDL(hif),
1271 		       NAPI_EVT_CPU_STATE,
1272 		       (void *)
1273 		       ((size_t)cpu << 16 | napid->napi_cpu[cpu].state));
1274 
1275 	NAPI_DEBUG("<--%s", __func__);
1276 }
1277 
1278 static int hnc_hotplug_register(struct hif_softc *hif_sc)
1279 {
1280 	QDF_STATUS status;
1281 
1282 	NAPI_DEBUG("-->%s", __func__);
1283 
1284 	status = qdf_cpuhp_register(&hif_sc->napi_data.cpuhp_handler,
1285 				    hif_sc,
1286 				    hnc_cpu_online_cb,
1287 				    hnc_cpu_before_offline_cb);
1288 
1289 	NAPI_DEBUG("<--%s [%d]", __func__, status);
1290 
1291 	return qdf_status_to_os_return(status);
1292 }
1293 
1294 static void hnc_hotplug_unregister(struct hif_softc *hif_sc)
1295 {
1296 	NAPI_DEBUG("-->%s", __func__);
1297 
1298 	if (hif_sc->napi_data.cpuhp_handler)
1299 		qdf_cpuhp_unregister(&hif_sc->napi_data.cpuhp_handler);
1300 
1301 	NAPI_DEBUG("<--%s", __func__);
1302 }
1303 
1304 /**
1305  * hnc_install_tput() - installs a callback in the throughput detector
1306  * @register: !0 => register; =0: unregister
1307  *
1308  * installs a callback to be called when wifi driver throughput (tx+rx)
1309  * crosses a threshold. Currently, we are using the same criteria as
1310  * TCP ack suppression (500 packets/100ms by default).
1311  *
1312  * Return: 0 : success
1313  *         <0: failure
1314  */
1315 
1316 static int hnc_tput_hook(int install)
1317 {
1318 	int rc = 0;
1319 
1320 	/*
1321 	 * Nothing, until the bw_calculation accepts registration
1322 	 * it is now hardcoded in the wlan_hdd_main.c::hdd_bus_bw_compute_cbk
1323 	 *   hdd_napi_throughput_policy(...)
1324 	 */
1325 	return rc;
1326 }
1327 
1328 /*
1329  * Implementation of hif_napi_cpu API
1330  */
1331 
1332 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0))
1333 static inline void record_sibling_cpumask(struct qca_napi_cpu *cpus, int i)
1334 {
1335 	cpumask_copy(&(cpus[i].thread_mask),
1336 			     topology_sibling_cpumask(i));
1337 }
1338 #else
1339 static inline void record_sibling_cpumask(struct qca_napi_cpu *cpus, int i)
1340 {
1341 }
1342 #endif
1343 
1344 
1345 /**
1346  * hif_napi_cpu_init() - initialization of irq affinity block
1347  * @ctx: pointer to qca_napi_data
1348  *
1349  * called by hif_napi_create, after the first instance is called
1350  * - builds napi_rss_cpus table from cpu topology
1351  * - links cores of the same clusters together
1352  * - installs hot-plug notifier
1353  * - installs throughput trigger notifier (when such mechanism exists)
1354  *
1355  * Return: 0: OK
1356  *         <0: error code
1357  */
1358 int hif_napi_cpu_init(struct hif_opaque_softc *hif)
1359 {
1360 	int rc = 0;
1361 	int i;
1362 	struct qca_napi_data *napid = &HIF_GET_SOFTC(hif)->napi_data;
1363 	struct qca_napi_cpu *cpus = napid->napi_cpu;
1364 
1365 	NAPI_DEBUG("--> ");
1366 
1367 	if (cpus[0].state != QCA_NAPI_CPU_UNINITIALIZED) {
1368 		NAPI_DEBUG("NAPI RSS table already initialized.\n");
1369 		rc = -EALREADY;
1370 		goto lab_rss_init;
1371 	}
1372 
1373 	/* build CPU topology table */
1374 	for_each_possible_cpu(i) {
1375 		cpus[i].state       = ((cpumask_test_cpu(i, cpu_online_mask)
1376 					? QCA_NAPI_CPU_UP
1377 					: QCA_NAPI_CPU_DOWN));
1378 		cpus[i].core_id     = topology_core_id(i);
1379 		cpus[i].cluster_id  = topology_physical_package_id(i);
1380 		cpumask_copy(&(cpus[i].core_mask),
1381 			     topology_core_cpumask(i));
1382 		record_sibling_cpumask(cpus, i);
1383 		cpus[i].max_freq    = cpufreq_quick_get_max(i);
1384 		cpus[i].napis       = 0x0;
1385 		cpus[i].cluster_nxt = -1; /* invalid */
1386 	}
1387 
1388 	/* link clusters together */
1389 	rc = hnc_link_clusters(napid);
1390 	if (0 != rc)
1391 		goto lab_err_topology;
1392 
1393 	/* install hotplug notifier */
1394 	rc = hnc_hotplug_register(HIF_GET_SOFTC(hif));
1395 	if (0 != rc)
1396 		goto lab_err_hotplug;
1397 
1398 	/* install throughput notifier */
1399 	rc = hnc_tput_hook(1);
1400 	if (0 == rc)
1401 		goto lab_rss_init;
1402 
1403 lab_err_hotplug:
1404 	hnc_tput_hook(0);
1405 	hnc_hotplug_unregister(HIF_GET_SOFTC(hif));
1406 lab_err_topology:
1407 	memset(napid->napi_cpu, 0, sizeof(struct qca_napi_cpu) * NR_CPUS);
1408 lab_rss_init:
1409 	NAPI_DEBUG("<-- [rc=%d]", rc);
1410 	return rc;
1411 }
1412 
1413 /**
1414  * hif_napi_cpu_deinit() - clean-up of irq affinity block
1415  *
1416  * called by hif_napi_destroy, when the last instance is removed
1417  * - uninstalls throughput and hotplug notifiers
1418  * - clears cpu topology table
1419  * Return: 0: OK
1420  */
1421 int hif_napi_cpu_deinit(struct hif_opaque_softc *hif)
1422 {
1423 	int rc = 0;
1424 	struct qca_napi_data *napid = &HIF_GET_SOFTC(hif)->napi_data;
1425 
1426 	NAPI_DEBUG("-->%s(...)", __func__);
1427 
1428 	/* uninstall tput notifier */
1429 	rc = hnc_tput_hook(0);
1430 
1431 	/* uninstall hotplug notifier */
1432 	hnc_hotplug_unregister(HIF_GET_SOFTC(hif));
1433 
1434 	/* clear the topology table */
1435 	memset(napid->napi_cpu, 0, sizeof(struct qca_napi_cpu) * NR_CPUS);
1436 
1437 	NAPI_DEBUG("<--%s[rc=%d]", __func__, rc);
1438 
1439 	return rc;
1440 }
1441 
1442 /**
1443  * hncm_migrate_to() - migrates a NAPI to a CPU
1444  * @napid: pointer to NAPI block
1445  * @ce_id: CE_id of the NAPI instance
1446  * @didx : index in the CPU topology table for the CPU to migrate to
1447  *
1448  * Migrates NAPI (identified by the CE_id) to the destination core
1449  * Updates the napi_map of the destination entry
1450  *
1451  * Return:
1452  *  =0 : success
1453  *  <0 : error
1454  */
1455 static int hncm_migrate_to(struct qca_napi_data *napid,
1456 			   int                   napi_ce,
1457 			   int                   didx)
1458 {
1459 	int rc = 0;
1460 	QDF_STATUS status;
1461 
1462 	NAPI_DEBUG("-->%s(napi_cd=%d, didx=%d)", __func__, napi_ce, didx);
1463 
1464 	if (!napid->napis[napi_ce])
1465 		return -EINVAL;
1466 
1467 	napid->napis[napi_ce]->cpumask.bits[0] = (1 << didx);
1468 
1469 	qdf_dev_modify_irq_status(napid->napis[napi_ce]->irq,
1470 				  QDF_IRQ_NO_BALANCING, 0);
1471 	status = qdf_dev_set_irq_affinity(napid->napis[napi_ce]->irq,
1472 					  (struct qdf_cpu_mask *)
1473 					  &napid->napis[napi_ce]->cpumask);
1474 	rc = qdf_status_to_os_return(status);
1475 
1476 	/* unmark the napis bitmap in the cpu table */
1477 	napid->napi_cpu[napid->napis[napi_ce]->cpu].napis &= ~(0x01 << napi_ce);
1478 	/* mark the napis bitmap for the new designated cpu */
1479 	napid->napi_cpu[didx].napis |= (0x01 << napi_ce);
1480 	napid->napis[napi_ce]->cpu = didx;
1481 
1482 	NAPI_DEBUG("<--%s[%d]", __func__, rc);
1483 	return rc;
1484 }
1485 /**
1486  * hncm_dest_cpu() - finds a destination CPU for NAPI
1487  * @napid: pointer to NAPI block
1488  * @act  : RELOCATE | COLLAPSE | DISPERSE
1489  *
1490  * Finds the designated destionation for the next IRQ.
1491  * RELOCATE: translated to either COLLAPSE or DISPERSE based
1492  *           on napid->napi_mode (throughput state)
1493  * COLLAPSE: All have the same destination: the first online CPU in lilcl
1494  * DISPERSE: One of the CPU in bigcl, which has the smallest number of
1495  *           NAPIs on it
1496  *
1497  * Return: >=0 : index in the cpu topology table
1498  *       : < 0 : error
1499  */
1500 static int hncm_dest_cpu(struct qca_napi_data *napid, int act)
1501 {
1502 	int destidx = -1;
1503 	int head, i;
1504 
1505 	NAPI_DEBUG("-->%s(act=%d)", __func__, act);
1506 	if (act == HNC_ACT_RELOCATE) {
1507 		if (napid->napi_mode == QCA_NAPI_TPUT_LO)
1508 			act = HNC_ACT_COLLAPSE;
1509 		else
1510 			act = HNC_ACT_DISPERSE;
1511 		NAPI_DEBUG("%s: act changed from HNC_ACT_RELOCATE to %d",
1512 			   __func__, act);
1513 	}
1514 	if (act == HNC_ACT_COLLAPSE) {
1515 		head = i = napid->lilcl_head;
1516 retry_collapse:
1517 		while (i >= 0) {
1518 			if (napid->napi_cpu[i].state == QCA_NAPI_CPU_UP) {
1519 				destidx = i;
1520 				break;
1521 			}
1522 			i = napid->napi_cpu[i].cluster_nxt;
1523 		}
1524 		if ((destidx < 0) && (head == napid->lilcl_head)) {
1525 			NAPI_DEBUG("%s: COLLAPSE: no lilcl dest, try bigcl",
1526 				__func__);
1527 			head = i = napid->bigcl_head;
1528 			goto retry_collapse;
1529 		}
1530 	} else { /* HNC_ACT_DISPERSE */
1531 		int smallest = 99; /* all 32 bits full */
1532 		int smallidx = -1;
1533 
1534 		head = i = napid->bigcl_head;
1535 retry_disperse:
1536 		while (i >= 0) {
1537 			if ((napid->napi_cpu[i].state == QCA_NAPI_CPU_UP) &&
1538 			    (hweight32(napid->napi_cpu[i].napis) <= smallest)) {
1539 				smallest = napid->napi_cpu[i].napis;
1540 				smallidx = i;
1541 			}
1542 			i = napid->napi_cpu[i].cluster_nxt;
1543 		}
1544 		/* Check if matches with user sepecified CPU mask */
1545 		smallidx = ((1 << smallidx) & napid->user_cpu_affin_mask) ?
1546 								smallidx : -1;
1547 
1548 		if ((smallidx < 0) && (head == napid->bigcl_head)) {
1549 			NAPI_DEBUG("%s: DISPERSE: no bigcl dest, try lilcl",
1550 				__func__);
1551 			head = i = napid->lilcl_head;
1552 			goto retry_disperse;
1553 		}
1554 		destidx = smallidx;
1555 	}
1556 	NAPI_DEBUG("<--%s[dest=%d]", __func__, destidx);
1557 	return destidx;
1558 }
1559 /**
1560  * hif_napi_cpu_migrate() - migrate IRQs away
1561  * @cpu: -1: all CPUs <n> specific CPU
1562  * @act: COLLAPSE | DISPERSE
1563  *
1564  * Moves IRQs/NAPIs from specific or all CPUs (specified by @cpu) to eligible
1565  * cores. Eligible cores are:
1566  * act=COLLAPSE -> the first online core of the little cluster
1567  * act=DISPERSE -> separate cores of the big cluster, so that each core will
1568  *                 host minimum number of NAPIs/IRQs (napid->cpus[cpu].napis)
1569  *
1570  * Note that this function is called with a spinlock acquired already.
1571  *
1572  * Return: =0: success
1573  *         <0: error
1574  */
1575 
1576 int hif_napi_cpu_migrate(struct qca_napi_data *napid, int cpu, int action)
1577 {
1578 	int      rc = 0;
1579 	struct qca_napi_cpu *cpup;
1580 	int      i, dind;
1581 	uint32_t napis;
1582 
1583 	NAPI_DEBUG("-->%s(.., cpu=%d, act=%d)",
1584 		   __func__, cpu, action);
1585 	/* the following is really: hif_napi_enabled() with less overhead */
1586 	if (napid->ce_map == 0) {
1587 		NAPI_DEBUG("%s: NAPI disabled. Not migrating.", __func__);
1588 		goto hncm_return;
1589 	}
1590 
1591 	cpup = napid->napi_cpu;
1592 
1593 	switch (action) {
1594 	case HNC_ACT_RELOCATE:
1595 	case HNC_ACT_DISPERSE:
1596 	case HNC_ACT_COLLAPSE: {
1597 		/* first find the src napi set */
1598 		if (cpu == HNC_ANY_CPU)
1599 			napis = napid->ce_map;
1600 		else
1601 			napis = cpup[cpu].napis;
1602 		/* then clear the napi bitmap on each CPU */
1603 		for (i = 0; i < NR_CPUS; i++)
1604 			cpup[i].napis = 0;
1605 		/* then for each of the NAPIs to disperse: */
1606 		for (i = 0; i < CE_COUNT_MAX; i++)
1607 			if (napis & (1 << i)) {
1608 				/* find a destination CPU */
1609 				dind = hncm_dest_cpu(napid, action);
1610 				if (dind >= 0) {
1611 					NAPI_DEBUG("Migrating NAPI ce%d to %d",
1612 						   i, dind);
1613 					rc = hncm_migrate_to(napid, i, dind);
1614 				} else {
1615 					NAPI_DEBUG("No dest for NAPI ce%d", i);
1616 					hnc_dump_cpus(napid);
1617 					rc = -1;
1618 				}
1619 			}
1620 		break;
1621 	}
1622 	default: {
1623 		NAPI_DEBUG("%s: bad action: %d\n", __func__, action);
1624 		QDF_BUG(0);
1625 		break;
1626 	}
1627 	} /* switch action */
1628 
1629 hncm_return:
1630 	hnc_dump_cpus(napid);
1631 	return rc;
1632 }
1633 
1634 
1635 /**
1636  * hif_napi_bl_irq() - calls irq_modify_status to enable/disable blacklisting
1637  * @napid: pointer to qca_napi_data structure
1638  * @bl_flag: blacklist flag to enable/disable blacklisting
1639  *
1640  * The function enables/disables blacklisting for all the copy engine
1641  * interrupts on which NAPI is enabled.
1642  *
1643  * Return: None
1644  */
1645 static inline void hif_napi_bl_irq(struct qca_napi_data *napid, bool bl_flag)
1646 {
1647 	int i;
1648 	struct qca_napi_info *napii;
1649 
1650 	for (i = 0; i < CE_COUNT_MAX; i++) {
1651 		/* check if NAPI is enabled on the CE */
1652 		if (!(napid->ce_map & (0x01 << i)))
1653 			continue;
1654 
1655 		/*double check that NAPI is allocated for the CE */
1656 		napii = napid->napis[i];
1657 		if (!(napii))
1658 			continue;
1659 
1660 		if (bl_flag == true)
1661 			qdf_dev_modify_irq_status(napii->irq,
1662 						  0, QDF_IRQ_NO_BALANCING);
1663 		else
1664 			qdf_dev_modify_irq_status(napii->irq,
1665 						  QDF_IRQ_NO_BALANCING, 0);
1666 		hif_debug("bl_flag %d CE %d", bl_flag, i);
1667 	}
1668 }
1669 
1670 /**
1671  * hif_napi_cpu_blacklist() - en(dis)ables blacklisting for NAPI RX interrupts.
1672  * @napid: pointer to qca_napi_data structure
1673  * @op: blacklist operation to perform
1674  *
1675  * The function enables/disables/queries blacklisting for all CE RX
1676  * interrupts with NAPI enabled. Besides blacklisting, it also enables/disables
1677  * core_ctl_set_boost.
1678  * Once blacklisting is enabled, the interrupts will not be managed by the IRQ
1679  * balancer.
1680  *
1681  * Return: -EINVAL, in case IRQ_BLACKLISTING and CORE_CTL_BOOST is not enabled
1682  *         for BLACKLIST_QUERY op - blacklist refcount
1683  *         for BLACKLIST_ON op    - return value from core_ctl_set_boost API
1684  *         for BLACKLIST_OFF op   - return value from core_ctl_set_boost API
1685  */
1686 int hif_napi_cpu_blacklist(struct qca_napi_data *napid,
1687 			   enum qca_blacklist_op op)
1688 {
1689 	int rc = 0;
1690 	static int ref_count; /* = 0 by the compiler */
1691 	uint8_t flags = napid->flags;
1692 	bool bl_en = flags & QCA_NAPI_FEATURE_IRQ_BLACKLISTING;
1693 	bool ccb_en = flags & QCA_NAPI_FEATURE_CORE_CTL_BOOST;
1694 
1695 	NAPI_DEBUG("-->%s(%d %d)", __func__, flags, op);
1696 
1697 	if (!(bl_en && ccb_en)) {
1698 		rc = -EINVAL;
1699 		goto out;
1700 	}
1701 
1702 	switch (op) {
1703 	case BLACKLIST_QUERY:
1704 		rc = ref_count;
1705 		break;
1706 	case BLACKLIST_ON:
1707 		ref_count++;
1708 		rc = 0;
1709 		if (ref_count == 1) {
1710 			rc = hif_napi_core_ctl_set_boost(true);
1711 			NAPI_DEBUG("boost_on() returns %d - refcnt=%d",
1712 				rc, ref_count);
1713 			hif_napi_bl_irq(napid, true);
1714 		}
1715 		break;
1716 	case BLACKLIST_OFF:
1717 		if (ref_count) {
1718 			ref_count--;
1719 			rc = 0;
1720 			if (ref_count == 0) {
1721 				rc = hif_napi_core_ctl_set_boost(false);
1722 				NAPI_DEBUG("boost_off() returns %d - refcnt=%d",
1723 					   rc, ref_count);
1724 				hif_napi_bl_irq(napid, false);
1725 			}
1726 		}
1727 		break;
1728 	default:
1729 		NAPI_DEBUG("Invalid blacklist op: %d", op);
1730 		rc = -EINVAL;
1731 	} /* switch */
1732 out:
1733 	NAPI_DEBUG("<--%s[%d]", __func__, rc);
1734 	return rc;
1735 }
1736 
1737 /**
1738  * hif_napi_serialize() - [de-]serialize NAPI operations
1739  * @hif:   context
1740  * @is_on: 1: serialize, 0: deserialize
1741  *
1742  * hif_napi_serialize(hif, 1) can be called multiple times. It will perform the
1743  * following steps (see hif_napi_event for code):
1744  * - put irqs of all NAPI instances on the same CPU
1745  * - only for the first serialize call: blacklist
1746  *
1747  * hif_napi_serialize(hif, 0):
1748  * - start a timer (multiple of BusBandwidthTimer -- default: 100 msec)
1749  * - at the end of the timer, check the current throughput state and
1750  *   implement it.
1751  */
1752 static unsigned long napi_serialize_reqs;
1753 int hif_napi_serialize(struct hif_opaque_softc *hif, int is_on)
1754 {
1755 	int rc = -EINVAL;
1756 
1757 	if (hif)
1758 		switch (is_on) {
1759 		case 0: { /* de-serialize */
1760 			rc = hif_napi_event(hif, NAPI_EVT_USR_NORMAL,
1761 					    (void *) 0);
1762 			napi_serialize_reqs = 0;
1763 			break;
1764 		} /* end de-serialize */
1765 		case 1: { /* serialize */
1766 			rc = hif_napi_event(hif, NAPI_EVT_USR_SERIAL,
1767 					    (void *)napi_serialize_reqs++);
1768 			break;
1769 		} /* end serialize */
1770 		default:
1771 			break; /* no-op */
1772 		} /* switch */
1773 	return rc;
1774 }
1775 
1776 #endif /* ifdef HIF_IRQ_AFFINITY */
1777