blob: 0574866a6725bfb66c09af13664bf7a44a380988 [file] [log] [blame]
Prakash Dhavali7090c5f2015-11-02 17:55:19 -08001/*
Komal Seelamf8600682016-02-02 18:17:13 +05302 * Copyright (c) 2015-2016 The Linux Foundation. All rights reserved.
Prakash Dhavali7090c5f2015-11-02 17:55:19 -08003 *
4 * Previously licensed under the ISC license by Qualcomm Atheros, Inc.
5 *
6 *
7 * Permission to use, copy, modify, and/or distribute this software for
8 * any purpose with or without fee is hereby granted, provided that the
9 * above copyright notice and this permission notice appear in all
10 * copies.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
13 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
14 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
15 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
16 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
17 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
18 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
19 * PERFORMANCE OF THIS SOFTWARE.
20 */
21
22/*
23 * This file was originally distributed by Qualcomm Atheros, Inc.
24 * under proprietary terms before Copyright ownership was assigned
25 * to the Linux Foundation.
26 */
27
28/**
29 * DOC: hif_napi.c
30 *
31 * HIF NAPI interface implementation
32 */
33
Orhan K AKYILDIZ7ce54e72016-05-16 12:36:32 -070034#include <string.h> /* memset */
35
36/* Linux headers */
37#include <linux/cpumask.h>
38#include <linux/cpufreq.h>
39#include <linux/cpu.h>
40#include <linux/topology.h>
41#include <linux/interrupt.h>
Manjunathappa Prakash21196d22016-09-12 13:39:41 -070042#ifdef HELIUMPLUS
Orhan K AKYILDIZ7ce54e72016-05-16 12:36:32 -070043#include <soc/qcom/irq-helper.h>
Orhan K AKYILDIZ6d9b7572016-09-25 21:31:50 -070044#include <pld_snoc.h>
Manjunathappa Prakash21196d22016-09-12 13:39:41 -070045#endif
46#include <linux/pm.h>
Orhan K AKYILDIZ7ce54e72016-05-16 12:36:32 -070047
48/* Driver headers */
49#include <cds_api.h>
Prakash Dhavali7090c5f2015-11-02 17:55:19 -080050#include <hif_napi.h>
51#include <hif_debug.h>
52#include <hif_io32.h>
53#include <ce_api.h>
Dhanashri Atre8d978172015-10-30 15:12:03 -070054#include <ce_internal.h>
Prakash Dhavali7090c5f2015-11-02 17:55:19 -080055
56enum napi_decision_vector {
57 HIF_NAPI_NOEVENT = 0,
58 HIF_NAPI_INITED = 1,
59 HIF_NAPI_CONF_UP = 2
60};
61#define ENABLE_NAPI_MASK (HIF_NAPI_INITED | HIF_NAPI_CONF_UP)
62
Manjunathappa Prakash21196d22016-09-12 13:39:41 -070063#ifdef HELIUMPLUS
64static inline int hif_get_irq_for_ce(int ce_id)
65{
Orhan K AKYILDIZ6d9b7572016-09-25 21:31:50 -070066 return pld_snoc_get_irq(ce_id);
Manjunathappa Prakash21196d22016-09-12 13:39:41 -070067}
68#else /* HELIUMPLUS */
69static inline int hif_get_irq_for_ce(int ce_id)
70{
71 return -EINVAL;
72}
73static int hif_napi_cpu_migrate(struct qca_napi_data *napid, int cpu,
74 int action)
75{
76 return 0;
77}
78
79int hif_napi_cpu_blacklist(bool is_on) { return 0; }
80#endif /* HELIUMPLUS */
81
Prakash Dhavali7090c5f2015-11-02 17:55:19 -080082/**
83 * hif_napi_create() - creates the NAPI structures for a given CE
84 * @hif : pointer to hif context
85 * @pipe_id: the CE id on which the instance will be created
86 * @poll : poll function to be used for this NAPI instance
87 * @budget : budget to be registered with the NAPI instance
88 * @scale : scale factor on the weight (to scaler budget to 1000)
89 *
90 * Description:
91 * Creates NAPI instances. This function is called
92 * unconditionally during initialization. It creates
93 * napi structures through the proper HTC/HIF calls.
94 * The structures are disabled on creation.
95 * Note that for each NAPI instance a separate dummy netdev is used
96 *
97 * Return:
98 * < 0: error
99 * = 0: <should never happen>
100 * > 0: id of the created object (for multi-NAPI, number of objects created)
101 */
Komal Seelam5584a7c2016-02-24 19:22:48 +0530102int hif_napi_create(struct hif_opaque_softc *hif_ctx,
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800103 int (*poll)(struct napi_struct *, int),
104 int budget,
105 int scale)
106{
Houston Hoffmanc7d54292016-04-13 18:55:37 -0700107 int i;
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800108 struct qca_napi_data *napid;
109 struct qca_napi_info *napii;
Houston Hoffmanc7d54292016-04-13 18:55:37 -0700110 struct CE_state *ce_state;
Komal Seelam644263d2016-02-22 20:45:49 +0530111 struct hif_softc *hif = HIF_GET_SOFTC(hif_ctx);
Orhan K AKYILDIZ7ce54e72016-05-16 12:36:32 -0700112 int rc = 0;
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800113
Houston Hoffmanc7d54292016-04-13 18:55:37 -0700114 NAPI_DEBUG("-->(budget=%d, scale=%d)",
115 budget, scale);
Houston Hoffman56936832016-03-16 12:16:24 -0700116 NAPI_DEBUG("hif->napi_data.state = 0x%08x",
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800117 hif->napi_data.state);
Houston Hoffman56936832016-03-16 12:16:24 -0700118 NAPI_DEBUG("hif->napi_data.ce_map = 0x%08x",
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800119 hif->napi_data.ce_map);
120
121 napid = &(hif->napi_data);
122 if (0 == (napid->state & HIF_NAPI_INITED)) {
123 memset(napid, 0, sizeof(struct qca_napi_data));
Orhan K AKYILDIZ7ce54e72016-05-16 12:36:32 -0700124 spin_lock_init(&(napid->lock));
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800125
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800126 napid->state |= HIF_NAPI_INITED;
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800127
Orhan K AKYILDIZ7ce54e72016-05-16 12:36:32 -0700128 rc = hif_napi_cpu_init(napid);
129 if (rc != 0) {
130 HIF_ERROR("NAPI_initialization failed,. %d", rc);
131 goto hnc_err;
132 }
133
134 HIF_INFO("%s: NAPI structures initialized, rc=%d",
135 __func__, rc);
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800136 }
Houston Hoffmand6f946c2016-04-06 15:16:00 -0700137 for (i = 0; i < hif->ce_count; i++) {
Houston Hoffmanc7d54292016-04-13 18:55:37 -0700138 ce_state = hif->ce_id_to_state[i];
Venkateswara Swamy Bandaru16334362016-08-23 15:38:10 +0530139 NAPI_DEBUG("ce %d: htt_rx=%d htt_tx=%d",
Houston Hoffmanc7d54292016-04-13 18:55:37 -0700140 i, ce_state->htt_rx_data,
141 ce_state->htt_tx_data);
142 if (!ce_state->htt_rx_data)
143 continue;
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800144
Houston Hoffmanc7d54292016-04-13 18:55:37 -0700145 /* Now this is a CE where we need NAPI on */
146 NAPI_DEBUG("Creating NAPI on pipe %d", i);
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800147
Houston Hoffmanc7d54292016-04-13 18:55:37 -0700148 napii = &(napid->napis[i]);
149 memset(napii, 0, sizeof(struct qca_napi_info));
150 napii->scale = scale;
151 napii->id = NAPI_PIPE2ID(i);
Venkateswara Swamy Bandaru16334362016-08-23 15:38:10 +0530152 napii->hif_ctx = hif_ctx;
Manjunathappa Prakash21196d22016-09-12 13:39:41 -0700153 napii->irq = hif_get_irq_for_ce(i);
Orhan K AKYILDIZ7ce54e72016-05-16 12:36:32 -0700154 if (napii->irq < 0)
155 HIF_WARN("%s: bad IRQ value for CE %d: %d",
156 __func__, i, napii->irq);
157
Manjunathappa Prakash2146da32016-10-13 14:47:47 -0700158 qdf_spinlock_create(&napii->lro_unloading_lock);
Houston Hoffmanc7d54292016-04-13 18:55:37 -0700159 init_dummy_netdev(&(napii->netdev));
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800160
Houston Hoffmanc7d54292016-04-13 18:55:37 -0700161 NAPI_DEBUG("adding napi=%p to netdev=%p (poll=%p, bdgt=%d)",
162 &(napii->napi), &(napii->netdev), poll, budget);
163 netif_napi_add(&(napii->netdev), &(napii->napi), poll, budget);
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800164
Houston Hoffmanc7d54292016-04-13 18:55:37 -0700165 NAPI_DEBUG("after napi_add");
166 NAPI_DEBUG("napi=0x%p, netdev=0x%p",
167 &(napii->napi), &(napii->netdev));
168 NAPI_DEBUG("napi.dev_list.prev=0x%p, next=0x%p",
169 napii->napi.dev_list.prev,
170 napii->napi.dev_list.next);
171 NAPI_DEBUG("dev.napi_list.prev=0x%p, next=0x%p",
172 napii->netdev.napi_list.prev,
173 napii->netdev.napi_list.next);
174
175 /* It is OK to change the state variable below without
176 * protection as there should be no-one around yet
177 */
178 napid->ce_map |= (0x01 << i);
179 HIF_INFO("%s: NAPI id %d created for pipe %d", __func__,
180 napii->id, i);
181 }
182 NAPI_DEBUG("NAPI idscreated for pipe all applicable pipes");
Orhan K AKYILDIZ7ce54e72016-05-16 12:36:32 -0700183hnc_err:
Houston Hoffmanc7d54292016-04-13 18:55:37 -0700184 NAPI_DEBUG("<--napi_instances_map=%x]", napid->ce_map);
185 return napid->ce_map;
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800186}
187
188/**
189 *
190 * hif_napi_destroy() - destroys the NAPI structures for a given instance
191 * @hif : pointer to hif context
192 * @ce_id : the CE id whose napi instance will be destroyed
193 * @force : if set, will destroy even if entry is active (de-activates)
194 *
195 * Description:
196 * Destroy a given NAPI instance. This function is called
197 * unconditionally during cleanup.
198 * Refuses to destroy an entry of it is still enabled (unless force=1)
199 * Marks the whole napi_data invalid if all instances are destroyed.
200 *
201 * Return:
202 * -EINVAL: specific entry has not been created
203 * -EPERM : specific entry is still active
204 * 0 < : error
205 * 0 = : success
206 */
Komal Seelam5584a7c2016-02-24 19:22:48 +0530207int hif_napi_destroy(struct hif_opaque_softc *hif_ctx,
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800208 uint8_t id,
209 int force)
210{
211 uint8_t ce = NAPI_ID2PIPE(id);
212 int rc = 0;
Komal Seelam644263d2016-02-22 20:45:49 +0530213 struct hif_softc *hif = HIF_GET_SOFTC(hif_ctx);
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800214
Houston Hoffman56936832016-03-16 12:16:24 -0700215 NAPI_DEBUG("-->(id=%d, force=%d)", id, force);
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800216
217 if (0 == (hif->napi_data.state & HIF_NAPI_INITED)) {
Houston Hoffmanc50572b2016-06-08 19:49:46 -0700218 HIF_ERROR("%s: NAPI not initialized or entry %d not created",
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800219 __func__, id);
220 rc = -EINVAL;
221 } else if (0 == (hif->napi_data.ce_map & (0x01 << ce))) {
Houston Hoffmanc50572b2016-06-08 19:49:46 -0700222 HIF_ERROR("%s: NAPI instance %d (pipe %d) not created",
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800223 __func__, id, ce);
224 rc = -EINVAL;
225 } else {
226 struct qca_napi_data *napid;
227 struct qca_napi_info *napii;
Orhan K AKYILDIZc4094612015-11-11 18:01:15 -0800228
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800229 napid = &(hif->napi_data);
230 napii = &(napid->napis[ce]);
231
232 if (hif->napi_data.state == HIF_NAPI_CONF_UP) {
233 if (force) {
234 napi_disable(&(napii->napi));
Houston Hoffmanc50572b2016-06-08 19:49:46 -0700235 HIF_INFO("%s: NAPI entry %d force disabled",
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800236 __func__, id);
Houston Hoffman56936832016-03-16 12:16:24 -0700237 NAPI_DEBUG("NAPI %d force disabled", id);
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800238 } else {
Houston Hoffmanc50572b2016-06-08 19:49:46 -0700239 HIF_ERROR("%s: Cannot destroy active NAPI %d",
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800240 __func__, id);
241 rc = -EPERM;
242 }
243 }
244 if (0 == rc) {
Houston Hoffman56936832016-03-16 12:16:24 -0700245 NAPI_DEBUG("before napi_del");
246 NAPI_DEBUG("napi.dlist.prv=0x%p, next=0x%p",
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800247 napii->napi.dev_list.prev,
248 napii->napi.dev_list.next);
Houston Hoffman56936832016-03-16 12:16:24 -0700249 NAPI_DEBUG("dev.napi_l.prv=0x%p, next=0x%p",
Houston Hoffmanc7d54292016-04-13 18:55:37 -0700250 napii->netdev.napi_list.prev,
251 napii->netdev.napi_list.next);
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800252
253 netif_napi_del(&(napii->napi));
254
255 napid->ce_map &= ~(0x01 << ce);
256 napii->scale = 0;
257 HIF_INFO("%s: NAPI %d destroyed\n", __func__, id);
258
259 /* if there are no active instances and
Orhan K AKYILDIZc4094612015-11-11 18:01:15 -0800260 * if they are all destroyed,
261 * set the whole structure to uninitialized state
262 */
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800263 if (napid->ce_map == 0) {
Orhan K AKYILDIZ7ce54e72016-05-16 12:36:32 -0700264 rc = hif_napi_cpu_deinit(napid);
265 /* caller is tolerant to receiving !=0 rc */
266
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800267 memset(napid,
268 0, sizeof(struct qca_napi_data));
Houston Hoffmanc50572b2016-06-08 19:49:46 -0700269 HIF_INFO("%s: no NAPI instances. Zapped.",
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800270 __func__);
271 }
272 }
273 }
274
275 return rc;
276}
277
278/**
Manjunathappa Prakash2146da32016-10-13 14:47:47 -0700279 * hif_napi_lro_flush_cb_register() - init and register flush callback for LRO
280 * @hif_hdl: pointer to hif context
281 * @lro_flush_handler: register LRO flush callback
282 * @lro_init_handler: Callback for initializing LRO
283 *
284 * Return: positive value on success and 0 on failure
285 */
286int hif_napi_lro_flush_cb_register(struct hif_opaque_softc *hif_hdl,
287 void (lro_flush_handler)(void *),
288 void *(lro_init_handler)(void))
289{
290 int rc = 0;
291 int i;
292 struct CE_state *ce_state;
293 struct hif_softc *scn = HIF_GET_SOFTC(hif_hdl);
294 void *data = NULL;
295 struct qca_napi_data *napid;
296 struct qca_napi_info *napii;
297
298 QDF_ASSERT(scn != NULL);
299
300 napid = hif_napi_get_all(hif_hdl);
301 if (scn != NULL) {
302 for (i = 0; i < scn->ce_count; i++) {
303 ce_state = scn->ce_id_to_state[i];
304 if ((ce_state != NULL) && (ce_state->htt_rx_data)) {
305 data = lro_init_handler();
306 if (data == NULL) {
307 HIF_ERROR("%s: Failed to init LRO for CE %d",
308 __func__, i);
309 continue;
310 }
311 napii = &(napid->napis[i]);
312 napii->lro_flush_cb = lro_flush_handler;
313 napii->lro_ctx = data;
314 HIF_ERROR("Registering LRO for ce_id %d NAPI callback for %d flush_cb %p, lro_data %p\n",
315 i, napii->id, napii->lro_flush_cb,
316 napii->lro_ctx);
317 rc++;
318 }
319 }
320 } else {
321 HIF_ERROR("%s: hif_state NULL!", __func__);
322 }
323 return rc;
324}
325
326/**
327 * hif_napi_lro_flush_cb_deregister() - Degregister and free LRO.
328 * @hif: pointer to hif context
329 * @lro_deinit_cb: LRO deinit callback
330 *
331 * Return: NONE
332 */
333void hif_napi_lro_flush_cb_deregister(struct hif_opaque_softc *hif_hdl,
334 void (lro_deinit_cb)(void *))
335{
336 int i;
337 struct CE_state *ce_state;
338 struct hif_softc *scn = HIF_GET_SOFTC(hif_hdl);
339 struct qca_napi_data *napid;
340 struct qca_napi_info *napii;
341
342 QDF_ASSERT(scn != NULL);
343
344 napid = hif_napi_get_all(hif_hdl);
345 if (scn != NULL) {
346 for (i = 0; i < scn->ce_count; i++) {
347 ce_state = scn->ce_id_to_state[i];
348 if ((ce_state != NULL) && (ce_state->htt_rx_data)) {
349 napii = &(napid->napis[i]);
350 HIF_ERROR("deRegistering LRO for ce_id %d NAPI callback for %d flush_cb %p, lro_data %p\n",
351 i, napii->id, napii->lro_flush_cb,
352 napii->lro_ctx);
353 qdf_spin_lock_bh(&napii->lro_unloading_lock);
354 napii->lro_flush_cb = NULL;
355 lro_deinit_cb(napii->lro_ctx);
356 napii->lro_ctx = NULL;
357 qdf_spin_unlock_bh(
358 &napii->lro_unloading_lock);
359 qdf_spinlock_destroy(
360 &napii->lro_unloading_lock);
361 }
362 }
363 } else {
364 HIF_ERROR("%s: hif_state NULL!", __func__);
365 }
366}
367
368/**
369 * hif_napi_get_lro_info() - returns the address LRO data for napi_id
370 * @hif: pointer to hif context
371 * @napi_id: napi instance
372 *
373 * Description:
374 * Returns the address of the LRO structure
375 *
376 * Return:
377 * <addr>: address of the LRO structure
378 */
379void *hif_napi_get_lro_info(struct hif_opaque_softc *hif_hdl, int napi_id)
380{
381 struct hif_softc *scn = HIF_GET_SOFTC(hif_hdl);
382 struct qca_napi_data *napid;
383 struct qca_napi_info *napii;
384
385 napid = &(scn->napi_data);
386 napii = &(napid->napis[NAPI_ID2PIPE(napi_id)]);
387
388 return napii->lro_ctx;
389}
390
391/**
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800392 *
393 * hif_napi_get_all() - returns the address of the whole HIF NAPI structure
394 * @hif: pointer to hif context
395 *
396 * Description:
397 * Returns the address of the whole structure
398 *
399 * Return:
400 * <addr>: address of the whole HIF NAPI structure
401 */
Komal Seelam5584a7c2016-02-24 19:22:48 +0530402inline struct qca_napi_data *hif_napi_get_all(struct hif_opaque_softc *hif_ctx)
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800403{
Komal Seelam644263d2016-02-22 20:45:49 +0530404 struct hif_softc *hif = HIF_GET_SOFTC(hif_ctx);
405
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800406 return &(hif->napi_data);
407}
408
409/**
410 *
Orhan K AKYILDIZ7ce54e72016-05-16 12:36:32 -0700411 * hif_napi_event() - reacts to events that impact NAPI
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800412 * @hif : pointer to hif context
413 * @evnt: event that has been detected
414 * @data: more data regarding the event
415 *
416 * Description:
Orhan K AKYILDIZ7ce54e72016-05-16 12:36:32 -0700417 * This function handles two types of events:
418 * 1- Events that change the state of NAPI (enabled/disabled):
419 * {NAPI_EVT_INI_FILE, NAPI_EVT_CMD_STATE}
420 * The state is retrievable by "hdd_napi_enabled(-1)"
421 * - NAPI will be on if either INI file is on and it has not been disabled
422 * by a subsequent vendor CMD,
423 * or it has been enabled by a vendor CMD.
424 * 2- Events that change the CPU affinity of a NAPI instance/IRQ:
425 * {NAPI_EVT_TPUT_STATE, NAPI_EVT_CPU_STATE}
426 * - NAPI will support a throughput mode (HI/LO), kept at napid->napi_mode
427 * - NAPI will switch throughput mode based on hdd_napi_throughput_policy()
428 * - In LO tput mode, NAPI will yield control if its interrupts to the system
429 * management functions. However in HI throughput mode, NAPI will actively
430 * manage its interrupts/instances (by trying to disperse them out to
431 * separate performance cores).
432 * - CPU eligibility is kept up-to-date by NAPI_EVT_CPU_STATE events.
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800433 *
Orhan K AKYILDIZ458fefc2016-09-16 00:43:38 -0700434 * + In some cases (roaming peer management is the only case so far), a
435 * a client can trigger a "SERIALIZE" event. Basically, this means that the
436 * users is asking NAPI to go into a truly single execution context state.
437 * So, NAPI indicates to msm-irqbalancer that it wants to be blacklisted,
438 * (if called for the first time) and then moves all IRQs (for NAPI
439 * instances) to be collapsed to a single core. If called multiple times,
440 * it will just re-collapse the CPUs. This is because blacklist-on() API
441 * is reference-counted, and because the API has already been called.
442 *
443 * Such a user, should call "DESERIALIZE" (NORMAL) event, to set NAPI to go
444 * to its "normal" operation. Optionally, they can give a timeout value (in
445 * multiples of BusBandwidthCheckPeriod -- 100 msecs by default). In this
446 * case, NAPI will just set the current throughput state to uninitialized
447 * and set the delay period. Once policy handler is called, it would skip
448 * applying the policy delay period times, and otherwise apply the policy.
449 *
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800450 * Return:
451 * < 0: some error
Orhan K AKYILDIZ7ce54e72016-05-16 12:36:32 -0700452 * = 0: event handled successfully
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800453 */
Komal Seelam5584a7c2016-02-24 19:22:48 +0530454int hif_napi_event(struct hif_opaque_softc *hif_ctx, enum qca_napi_event event,
Komal Seelam644263d2016-02-22 20:45:49 +0530455 void *data)
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800456{
Orhan K AKYILDIZ7ce54e72016-05-16 12:36:32 -0700457 int rc = 0;
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800458 uint32_t prev_state;
459 int i;
460 struct napi_struct *napi;
Komal Seelam644263d2016-02-22 20:45:49 +0530461 struct hif_softc *hif = HIF_GET_SOFTC(hif_ctx);
Orhan K AKYILDIZ7ce54e72016-05-16 12:36:32 -0700462 struct qca_napi_data *napid = &(hif->napi_data);
463 enum qca_napi_tput_state tput_mode = QCA_NAPI_TPUT_UNINITIALIZED;
Orhan K AKYILDIZ458fefc2016-09-16 00:43:38 -0700464 enum {
465 BLACKLIST_NOT_PENDING,
466 BLACKLIST_ON_PENDING,
467 BLACKLIST_OFF_PENDING
468 } blacklist_pending = BLACKLIST_NOT_PENDING;
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800469
Orhan K AKYILDIZ7ce54e72016-05-16 12:36:32 -0700470 NAPI_DEBUG("%s: -->(event=%d, aux=%p)", __func__, event, data);
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800471
Orhan K AKYILDIZ7ce54e72016-05-16 12:36:32 -0700472 spin_lock_bh(&(napid->lock));
473 prev_state = napid->state;
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800474 switch (event) {
475 case NAPI_EVT_INI_FILE:
Orhan K AKYILDIZf006e932016-11-14 00:35:44 -0800476 case NAPI_EVT_CMD_STATE:
477 case NAPI_EVT_INT_STATE: {
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800478 int on = (data != ((void *)0));
Orhan K AKYILDIZc4094612015-11-11 18:01:15 -0800479
Orhan K AKYILDIZf006e932016-11-14 00:35:44 -0800480 HIF_INFO("%s: recved evnt: STATE_CMD %d; v = %d (state=0x%0x)",
481 __func__, event,
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800482 on, prev_state);
483 if (on)
484 if (prev_state & HIF_NAPI_CONF_UP) {
Houston Hoffmanc50572b2016-06-08 19:49:46 -0700485 HIF_INFO("%s: duplicate NAPI conf ON msg",
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800486 __func__);
487 } else {
Orhan K AKYILDIZf006e932016-11-14 00:35:44 -0800488 HIF_INFO("%s: setting state to ON",
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800489 __func__);
Orhan K AKYILDIZ7ce54e72016-05-16 12:36:32 -0700490 napid->state |= HIF_NAPI_CONF_UP;
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800491 }
492 else /* off request */
493 if (prev_state & HIF_NAPI_CONF_UP) {
Orhan K AKYILDIZf006e932016-11-14 00:35:44 -0800494 HIF_INFO("%s: setting state to OFF",
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800495 __func__);
Orhan K AKYILDIZ7ce54e72016-05-16 12:36:32 -0700496 napid->state &= ~HIF_NAPI_CONF_UP;
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800497 } else {
Houston Hoffmanc50572b2016-06-08 19:49:46 -0700498 HIF_INFO("%s: duplicate NAPI conf OFF msg",
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800499 __func__);
500 }
501 break;
502 }
503 /* case NAPI_INIT_FILE/CMD_STATE */
Orhan K AKYILDIZ7ce54e72016-05-16 12:36:32 -0700504
505 case NAPI_EVT_CPU_STATE: {
506 int cpu = ((unsigned long int)data >> 16);
507 int val = ((unsigned long int)data & 0x0ff);
508
509 NAPI_DEBUG("%s: evt=CPU_STATE on CPU %d value=%d",
510 __func__, cpu, val);
511
512 /* state has already been set by hnc_cpu_notify_cb */
513 if ((val == QCA_NAPI_CPU_DOWN) &&
514 (napid->napi_mode == QCA_NAPI_TPUT_HI) && /* we manage */
515 (napid->napi_cpu[cpu].napis != 0)) {
516 NAPI_DEBUG("%s: Migrating NAPIs out of cpu %d",
517 __func__, cpu);
518 rc = hif_napi_cpu_migrate(napid,
519 cpu,
520 HNC_ACT_RELOCATE);
521 napid->napi_cpu[cpu].napis = 0;
522 }
523 /* in QCA_NAPI_TPUT_LO case, napis MUST == 0 */
524 break;
525 }
526 case NAPI_EVT_TPUT_STATE: {
527
528 tput_mode = (enum qca_napi_tput_state)data;
529 if (tput_mode == QCA_NAPI_TPUT_LO) {
530 /* from TPUT_HI -> TPUT_LO */
531 NAPI_DEBUG("%s: Moving to napi_tput_LO state",
532 __func__);
533
534 rc = hif_napi_cpu_migrate(napid,
535 HNC_ANY_CPU,
536 HNC_ACT_COLLAPSE);
Orhan K AKYILDIZ458fefc2016-09-16 00:43:38 -0700537 blacklist_pending = BLACKLIST_OFF_PENDING;
Orhan K AKYILDIZ7ce54e72016-05-16 12:36:32 -0700538 } else {
539 /* from TPUT_LO -> TPUT->HI */
540 NAPI_DEBUG("%s: Moving to napi_tput_HI state",
541 __func__);
542
543 rc = hif_napi_cpu_migrate(napid,
544 HNC_ANY_CPU,
545 HNC_ACT_DISPERSE);
Orhan K AKYILDIZ458fefc2016-09-16 00:43:38 -0700546 blacklist_pending = BLACKLIST_ON_PENDING;
Orhan K AKYILDIZ7ce54e72016-05-16 12:36:32 -0700547 }
548 napid->napi_mode = tput_mode;
549
550 break;
551 }
Orhan K AKYILDIZ458fefc2016-09-16 00:43:38 -0700552 case NAPI_EVT_USR_SERIAL: {
553 unsigned long users = (unsigned long)data;
Orhan K AKYILDIZ7ce54e72016-05-16 12:36:32 -0700554
Orhan K AKYILDIZ458fefc2016-09-16 00:43:38 -0700555 NAPI_DEBUG("%s: User forced SERIALIZATION; users=%ld",
556 __func__, users);
557
558 rc = hif_napi_cpu_migrate(napid,
559 HNC_ANY_CPU,
560 HNC_ACT_COLLAPSE);
561 if ((users == 0) && (rc == 0))
562 blacklist_pending = BLACKLIST_ON_PENDING;
563 break;
564 }
565 case NAPI_EVT_USR_NORMAL: {
566 NAPI_DEBUG("%s: User forced DE-SERIALIZATION", __func__);
567 /*
568 * Deserialization timeout is handled at hdd layer;
569 * just mark current mode to uninitialized to ensure
570 * it will be set when the delay is over
571 */
572 napid->napi_mode = QCA_NAPI_TPUT_UNINITIALIZED;
573 break;
574 }
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800575 default: {
Houston Hoffmanc50572b2016-06-08 19:49:46 -0700576 HIF_ERROR("%s: unknown event: %d (data=0x%0lx)",
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800577 __func__, event, (unsigned long) data);
578 break;
579 } /* default */
580 }; /* switch */
581
582
Orhan K AKYILDIZ7ce54e72016-05-16 12:36:32 -0700583 spin_unlock_bh(&(napid->lock));
584
585 /* Call this API without spin_locks hif_napi_cpu_blacklist */
Orhan K AKYILDIZ458fefc2016-09-16 00:43:38 -0700586 switch (blacklist_pending) {
587 case BLACKLIST_ON_PENDING:
588 /* assume the control of WLAN IRQs */
Orhan K AKYILDIZ7ce54e72016-05-16 12:36:32 -0700589 hif_napi_cpu_blacklist(true);
Orhan K AKYILDIZ458fefc2016-09-16 00:43:38 -0700590 break;
591 case BLACKLIST_OFF_PENDING:
592 /* yield the control of WLAN IRQs */
593 hif_napi_cpu_blacklist(false);
594 break;
595 default: /* nothing to do */
596 break;
597 } /* switch blacklist_pending */
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800598
Orhan K AKYILDIZf006e932016-11-14 00:35:44 -0800599 if (prev_state != napid->state) {
600 if (napid->state == ENABLE_NAPI_MASK) {
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800601 rc = 1;
602 for (i = 0; i < CE_COUNT_MAX; i++)
Orhan K AKYILDIZf006e932016-11-14 00:35:44 -0800603 if ((napid->ce_map & (0x01 << i))) {
604 napi = &(napid->napis[i].napi);
Orhan K AKYILDIZ7ce54e72016-05-16 12:36:32 -0700605 NAPI_DEBUG("%s: enabling NAPI %d",
606 __func__, i);
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800607 napi_enable(napi);
608 }
609 } else {
610 rc = 0;
611 for (i = 0; i < CE_COUNT_MAX; i++)
Orhan K AKYILDIZf006e932016-11-14 00:35:44 -0800612 if (napid->ce_map & (0x01 << i)) {
613 napi = &(napid->napis[i].napi);
Orhan K AKYILDIZ7ce54e72016-05-16 12:36:32 -0700614 NAPI_DEBUG("%s: disabling NAPI %d",
615 __func__, i);
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800616 napi_disable(napi);
Orhan K AKYILDIZf006e932016-11-14 00:35:44 -0800617 /* in case it is affined, remove it */
618 irq_set_affinity_hint(
619 napid->napis[i].irq,
620 NULL);
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800621 }
622 }
623 } else {
Houston Hoffmanc50572b2016-06-08 19:49:46 -0700624 HIF_INFO("%s: no change in hif napi state (still %d)",
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800625 __func__, prev_state);
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800626 }
627
Houston Hoffman56936832016-03-16 12:16:24 -0700628 NAPI_DEBUG("<--[rc=%d]", rc);
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800629 return rc;
630}
631
632/**
633 * hif_napi_enabled() - checks whether NAPI is enabled for given ce or not
634 * @hif: hif context
635 * @ce : CE instance (or -1, to check if any CEs are enabled)
636 *
637 * Return: bool
638 */
Komal Seelam5584a7c2016-02-24 19:22:48 +0530639int hif_napi_enabled(struct hif_opaque_softc *hif_ctx, int ce)
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800640{
641 int rc;
Komal Seelam644263d2016-02-22 20:45:49 +0530642 struct hif_softc *hif = HIF_GET_SOFTC(hif_ctx);
Orhan K AKYILDIZc4094612015-11-11 18:01:15 -0800643
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800644 if (-1 == ce)
645 rc = ((hif->napi_data.state == ENABLE_NAPI_MASK));
646 else
647 rc = ((hif->napi_data.state == ENABLE_NAPI_MASK) &&
648 (hif->napi_data.ce_map & (0x01 << ce)));
649 return rc;
650};
651
652/**
653 * hif_napi_enable_irq() - enables bus interrupts after napi_complete
654 *
655 * @hif: hif context
656 * @id : id of NAPI instance calling this (used to determine the CE)
657 *
658 * Return: void
659 */
Komal Seelam5584a7c2016-02-24 19:22:48 +0530660inline void hif_napi_enable_irq(struct hif_opaque_softc *hif, int id)
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800661{
Komal Seelam644263d2016-02-22 20:45:49 +0530662 struct hif_softc *scn = HIF_GET_SOFTC(hif);
663
Houston Hoffman8f239f62016-03-14 21:12:05 -0700664 hif_irq_enable(scn, NAPI_ID2PIPE(id));
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800665}
666
667
668/**
669 * hif_napi_schedule() - schedules napi, updates stats
670 * @scn: hif context
671 * @ce_id: index of napi instance
672 *
673 * Return: void
674 */
Komal Seelam5584a7c2016-02-24 19:22:48 +0530675int hif_napi_schedule(struct hif_opaque_softc *hif_ctx, int ce_id)
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800676{
677 int cpu = smp_processor_id();
Komal Seelam644263d2016-02-22 20:45:49 +0530678 struct hif_softc *scn = HIF_GET_SOFTC(hif_ctx);
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800679
Houston Hoffmanfa260aa2016-04-26 16:14:13 -0700680 hif_record_ce_desc_event(scn, ce_id, NAPI_SCHEDULE,
681 NULL, NULL, 0);
682
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800683 scn->napi_data.napis[ce_id].stats[cpu].napi_schedules++;
Houston Hoffman56936832016-03-16 12:16:24 -0700684 NAPI_DEBUG("scheduling napi %d (ce:%d)",
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800685 scn->napi_data.napis[ce_id].id, ce_id);
686 napi_schedule(&(scn->napi_data.napis[ce_id].napi));
687
688 return true;
689}
690
691/**
692 * hif_napi_poll() - NAPI poll routine
693 * @napi : pointer to NAPI struct as kernel holds it
694 * @budget:
695 *
696 * This is the body of the poll function.
697 * The poll function is called by kernel. So, there is a wrapper
698 * function in HDD, which in turn calls this function.
699 * Two main reasons why the whole thing is not implemented in HDD:
700 * a) references to things like ce_service that HDD is not aware of
701 * b) proximity to the implementation of ce_tasklet, which the body
702 * of this function should be very close to.
703 *
704 * NOTE TO THE MAINTAINER:
705 * Consider this function and ce_tasklet very tightly coupled pairs.
706 * Any changes to ce_tasklet or this function may likely need to be
707 * reflected in the counterpart.
708 *
709 * Returns:
710 * int: the amount of work done in this poll ( <= budget)
711 */
Komal Seelam5584a7c2016-02-24 19:22:48 +0530712int hif_napi_poll(struct hif_opaque_softc *hif_ctx, struct napi_struct *napi,
713 int budget)
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800714{
Orhan K AKYILDIZc4094612015-11-11 18:01:15 -0800715 int rc = 0; /* default: no work done, also takes care of error */
716 int normalized, bucket;
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800717 int cpu = smp_processor_id();
Komal Seelam644263d2016-02-22 20:45:49 +0530718 struct hif_softc *hif = HIF_GET_SOFTC(hif_ctx);
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800719 struct qca_napi_info *napi_info;
Houston Hoffmaneb2516c2016-04-01 12:53:50 -0700720 struct CE_state *ce_state = NULL;
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800721
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800722 napi_info = (struct qca_napi_info *)
723 container_of(napi, struct qca_napi_info, napi);
Orhan K AKYILDIZ7ce54e72016-05-16 12:36:32 -0700724
725 NAPI_DEBUG("%s -->(napi(%d, irq=%d), budget=%d)",
726 __func__, napi_info->id, napi_info->irq, budget);
727
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800728 napi_info->stats[cpu].napi_polls++;
729
Houston Hoffmanfa260aa2016-04-26 16:14:13 -0700730 hif_record_ce_desc_event(hif, NAPI_ID2PIPE(napi_info->id),
731 NAPI_POLL_ENTER, NULL, NULL, cpu);
732
Manjunathappa Prakash2146da32016-10-13 14:47:47 -0700733 qdf_spin_lock_bh(&napi_info->lro_unloading_lock);
734 if (unlikely(NULL == hif)) {
735 HIF_ERROR("%s: hif context is NULL", __func__);
736 QDF_ASSERT(0); /* emit a warning if hif NULL */
737 goto out;
738 } else {
Orhan K AKYILDIZc4094612015-11-11 18:01:15 -0800739 rc = ce_per_engine_service(hif, NAPI_ID2PIPE(napi_info->id));
Rajeev Kumarfebbf6b2016-03-23 15:05:42 -0700740 NAPI_DEBUG("%s: ce_per_engine_service processed %d msgs",
Orhan K AKYILDIZc4094612015-11-11 18:01:15 -0800741 __func__, rc);
742 }
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800743 napi_info->stats[cpu].napi_workdone += rc;
744 normalized = (rc / napi_info->scale);
Dhanashri Atre8d978172015-10-30 15:12:03 -0700745
Manjunathappa Prakash2146da32016-10-13 14:47:47 -0700746 ce_state = hif->ce_id_to_state[NAPI_ID2PIPE(napi_info->id)];
747
748 if (napi_info->lro_flush_cb)
749 napi_info->lro_flush_cb(napi_info->lro_ctx);
750 qdf_spin_unlock_bh(&napi_info->lro_unloading_lock);
Dhanashri Atre8d978172015-10-30 15:12:03 -0700751
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800752 /* do not return 0, if there was some work done,
Orhan K AKYILDIZc4094612015-11-11 18:01:15 -0800753 * even if it is below the scale
754 */
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800755 if (rc)
756 normalized++;
757 bucket = (normalized / QCA_NAPI_DEF_SCALE);
758 napi_info->stats[cpu].napi_budget_uses[bucket]++;
759
Orhan K AKYILDIZc4094612015-11-11 18:01:15 -0800760 /* if ce_per engine reports 0, then poll should be terminated */
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800761 if (0 == rc)
Houston Hoffman56936832016-03-16 12:16:24 -0700762 NAPI_DEBUG("%s:%d: nothing processed by CE. Completing NAPI",
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800763 __func__, __LINE__);
764
Himanshu Agarwal2a924592016-06-30 18:04:14 +0530765 if (ce_state && (!ce_check_rx_pending(ce_state) || 0 == rc)) {
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800766 napi_info->stats[cpu].napi_completes++;
Houston Hoffmanfa260aa2016-04-26 16:14:13 -0700767
768 hif_record_ce_desc_event(hif, ce_state->id, NAPI_COMPLETE,
769 NULL, NULL, 0);
Houston Hoffmana757eda2016-05-12 21:19:50 -0700770 if (normalized >= budget)
771 normalized = budget - 1;
772
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800773 /* enable interrupts */
774 napi_complete(napi);
Orhan K AKYILDIZc4094612015-11-11 18:01:15 -0800775 if (NULL != hif) {
Komal Seelam644263d2016-02-22 20:45:49 +0530776 hif_napi_enable_irq(hif_ctx, napi_info->id);
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800777
Orhan K AKYILDIZc4094612015-11-11 18:01:15 -0800778 /* support suspend/resume */
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +0530779 qdf_atomic_dec(&(hif->active_tasklet_cnt));
Orhan K AKYILDIZc4094612015-11-11 18:01:15 -0800780 }
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800781
Houston Hoffman56936832016-03-16 12:16:24 -0700782 NAPI_DEBUG("%s:%d: napi_complete + enabling the interrupts",
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800783 __func__, __LINE__);
Houston Hoffmana757eda2016-05-12 21:19:50 -0700784 } else {
785 /* 4.4 kernel NAPI implementation requires drivers to
786 * return full work when they ask to be re-scheduled,
787 * or napi_complete and re-start with a fresh interrupt
788 */
789 normalized = budget;
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800790 }
791
Houston Hoffmanfa260aa2016-04-26 16:14:13 -0700792 hif_record_ce_desc_event(hif, NAPI_ID2PIPE(napi_info->id),
793 NAPI_POLL_EXIT, NULL, NULL, normalized);
794
Orhan K AKYILDIZ7ce54e72016-05-16 12:36:32 -0700795 NAPI_DEBUG("%s <--[normalized=%d]", __func__, normalized);
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800796 return normalized;
Manjunathappa Prakash2146da32016-10-13 14:47:47 -0700797out:
798 return rc;
Prakash Dhavali7090c5f2015-11-02 17:55:19 -0800799}
Orhan K AKYILDIZ7ce54e72016-05-16 12:36:32 -0700800
801#ifdef HELIUMPLUS
802/*
803 * Local functions
804 * - no argument checks, all internal/trusted callers
805 */
806
807#ifdef FEATURE_NAPI_DEBUG
808static void hnc_dump_cpus(struct qca_napi_data *napid)
809{
810 int i;
811 struct qca_napi_cpu *cpu = napid->napi_cpu;
812
813 NAPI_DEBUG("%s: NAPI CPU TABLE", __func__);
814 NAPI_DEBUG("lilclhead=%d, bigclhead=%d",
815 napid->lilcl_head, napid->bigcl_head);
816 for (i = 0; i < NR_CPUS; i++) {
817 NAPI_DEBUG("CPU[%02d]: state:%d crid=%02d clid=%02d "
Govind Singh861d5da2016-09-27 23:04:17 +0530818 "crmk:0x%0lx thmk:0x%0lx frq:%d "
Orhan K AKYILDIZ7ce54e72016-05-16 12:36:32 -0700819 "napi = 0x%08x lnk:%d",
820 i,
821 cpu[i].state, cpu[i].core_id, cpu[i].cluster_id,
822 cpu[i].core_mask.bits[0],
823 cpu[i].thread_mask.bits[0],
Govind Singh861d5da2016-09-27 23:04:17 +0530824 cpu[i].max_freq, cpu[i].napis,
Orhan K AKYILDIZ7ce54e72016-05-16 12:36:32 -0700825 cpu[i].cluster_nxt);
826 }
827 /* return; -- Linus does not like it, I do. */
828}
829#else
830static void hnc_dump_cpus(struct qca_napi_data *napid) { /* no-op */ };
831#endif /* FEATURE_NAPI_DEBUG */
832/**
833 * hnc_link_clusters() - partitions to cpu table into clusters
834 * @napid: pointer to NAPI data
835 *
836 * Takes in a CPU topology table and builds two linked lists
837 * (big cluster cores, list-head at bigcl_head, and little cluster
838 * cores, list-head at lilcl_head) out of it.
839 *
840 * If there are more than two clusters:
841 * - bigcl_head and lilcl_head will be different,
842 * - the cluster with highest cpufreq will be considered the "big" cluster.
843 * If there are more than one with the highest frequency, the *last* of such
844 * clusters will be designated as the "big cluster"
845 * - the cluster with lowest cpufreq will be considered the "li'l" cluster.
846 * If there are more than one clusters with the lowest cpu freq, the *first*
847 * of such clusters will be designated as the "little cluster"
848 * - We only support up to 32 clusters
849 * Return: 0 : OK
850 * !0: error (at least one of lil/big clusters could not be found)
851 */
852#define HNC_MIN_CLUSTER 0
853#define HNC_MAX_CLUSTER 31
854static int hnc_link_clusters(struct qca_napi_data *napid)
855{
856 int rc = 0;
857
858 int i;
859 int it = 0;
860 uint32_t cl_done = 0x0;
861 int cl, curcl, curclhead;
862 int more;
863 unsigned int lilfrq = INT_MAX;
864 unsigned int bigfrq = 0;
865 unsigned int clfrq;
Orhan K AKYILDIZ7ce54e72016-05-16 12:36:32 -0700866 int prev;
867 struct qca_napi_cpu *cpus = napid->napi_cpu;
868
869 napid->lilcl_head = napid->bigcl_head = -1;
870
871 do {
872 more = 0;
873 it++; curcl = -1;
874 for (i = 0; i < NR_CPUS; i++) {
875 cl = cpus[i].cluster_id;
876 NAPI_DEBUG("Processing cpu[%d], cluster=%d\n",
877 i, cl);
878 if ((cl < HNC_MIN_CLUSTER) || (cl > HNC_MAX_CLUSTER)) {
879 NAPI_DEBUG("Bad cluster (%d). SKIPPED\n", cl);
880 QDF_ASSERT(0);
881 /* continue if ASSERTs are disabled */
882 continue;
883 };
884 if (cpumask_weight(&(cpus[i].core_mask)) == 0) {
885 NAPI_DEBUG("Core mask 0. SKIPPED\n");
886 continue;
887 }
888 if (cl_done & (0x01 << cl)) {
889 NAPI_DEBUG("Cluster already processed. "
890 "SKIPPED\n");
891 continue;
892 } else {
893 if (more == 0) {
894 more = 1;
895 curcl = cl;
896 curclhead = i; /* row */
897 clfrq = cpus[i].max_freq;
Orhan K AKYILDIZ7ce54e72016-05-16 12:36:32 -0700898 prev = -1;
899 };
900 if ((curcl >= 0) && (curcl != cl)) {
901 NAPI_DEBUG("Entry cl(%d) != curcl(%d). "
902 "SKIPPED\n",
903 cl, curcl);
904 continue;
905 }
Orhan K AKYILDIZ7ce54e72016-05-16 12:36:32 -0700906 if (cpus[i].max_freq != clfrq)
907 NAPI_DEBUG("WARN: frq(%d)!=clfrq(%d)\n",
908 cpus[i].max_freq, clfrq);
909 if (clfrq >= bigfrq) {
910 bigfrq = clfrq;
911 napid->bigcl_head = curclhead;
912 NAPI_DEBUG("bigcl=%d\n", curclhead);
913 }
914 if (clfrq < lilfrq) {
915 lilfrq = clfrq;
916 napid->lilcl_head = curclhead;
917 NAPI_DEBUG("lilcl=%d\n", curclhead);
918 }
919 if (prev != -1)
920 cpus[prev].cluster_nxt = i;
921
922 prev = i;
923 }
924 }
925 if (curcl >= 0)
926 cl_done |= (0x01 << curcl);
927
928 } while (more);
929
930 if (qdf_unlikely((napid->lilcl_head < 0) && (napid->bigcl_head < 0)))
931 rc = -EFAULT;
932
933 hnc_dump_cpus(napid); /* if NAPI_DEBUG */
934 return rc;
935}
936#undef HNC_MIN_CLUSTER
937#undef HNC_MAX_CLUSTER
938
939/*
940 * hotplug function group
941 */
942
943/**
944 * hnc_cpu_notify_cb() - handles CPU hotplug events
945 *
946 * On transitions to online, we onlu handle the ONLINE event,
947 * and ignore the PREP events, because we dont want to act too
948 * early.
949 * On transtion to offline, we act on PREP events, because
950 * we may need to move the irqs/NAPIs to another CPU before
951 * it is actually off-lined.
952 *
953 * Return: NOTIFY_OK (dont block action)
954 */
955static int hnc_cpu_notify_cb(struct notifier_block *nb,
956 unsigned long action,
957 void *hcpu)
958{
959 int rc = NOTIFY_OK;
960 unsigned long cpu = (unsigned long)hcpu;
961 struct hif_opaque_softc *hif;
962 struct qca_napi_data *napid = NULL;
963
964 NAPI_DEBUG("-->%s(act=%ld, cpu=%ld)", __func__, action, cpu);
965
966 hif = (struct hif_opaque_softc *)cds_get_context(QDF_MODULE_ID_HIF);
967 if (qdf_likely(hif != NULL))
968 napid = hif_napi_get_all(hif);
969 if (qdf_unlikely(napid == NULL)) {
970 NAPI_DEBUG("%s: hif/napid NULL (%p/%p)",
971 __func__, hif, napid);
972 goto lab_hnc_notify;
973 }
974 switch (action) {
975 case CPU_ONLINE:
976 napid->napi_cpu[cpu].state = QCA_NAPI_CPU_UP;
977 NAPI_DEBUG("%s: CPU %ld marked %d",
978 __func__, cpu, napid->napi_cpu[cpu].state);
979 break;
980 case CPU_DEAD: /* already dead; we have marked it before, but ... */
981 case CPU_DEAD_FROZEN:
982 napid->napi_cpu[cpu].state = QCA_NAPI_CPU_DOWN;
983 NAPI_DEBUG("%s: CPU %ld marked %d",
984 __func__, cpu, napid->napi_cpu[cpu].state);
985 break;
986 case CPU_DOWN_PREPARE:
987 case CPU_DOWN_PREPARE_FROZEN:
988 napid->napi_cpu[cpu].state = QCA_NAPI_CPU_DOWN;
989
990 NAPI_DEBUG("%s: CPU %ld marked %d; updating affinity",
991 __func__, cpu, napid->napi_cpu[cpu].state);
992
993 /**
994 * we need to move any NAPIs on this CPU out.
995 * if we are in LO throughput mode, then this is valid
996 * if the CPU is the the low designated CPU.
997 */
998 hif_napi_event(hif,
999 NAPI_EVT_CPU_STATE,
1000 (void *)
1001 ((cpu << 16) | napid->napi_cpu[cpu].state));
1002 break;
1003 default:
1004 NAPI_DEBUG("%s: ignored. action: %ld", __func__, action);
1005 break;
1006 } /* switch */
1007lab_hnc_notify:
1008 NAPI_DEBUG("<--%s [%d]", __func__, rc);
1009 return rc;
1010}
1011
1012/**
1013 * hnc_hotplug_hook() - installs a hotplug notifier
1014 * @register: !0 => register , =0 => deregister
1015 * Note that this is different from the cpu notifier used by
1016 * rx_thread (cds_schedule.c).
1017 * We may consider combining these modifiers in the future.
1018 *
1019 * Return: 0: success
1020 * <0: error
1021 */
1022static struct notifier_block hnc_cpu_notifier = {
1023 .notifier_call = hnc_cpu_notify_cb,
1024};
1025static int hnc_hotplug_hook(int install)
1026{
1027 int rc = 0;
1028
1029 NAPI_DEBUG("-->%s(%d)", __func__, install);
1030
1031 if (install)
1032 rc = register_hotcpu_notifier(&hnc_cpu_notifier);
1033 else
1034 unregister_hotcpu_notifier(&hnc_cpu_notifier);
1035
1036 NAPI_DEBUG("<--%s()[%d]", __func__, rc);
1037 return rc;
1038}
1039
1040/**
1041 * hnc_install_tput() - installs a callback in the throughput detector
1042 * @register: !0 => register; =0: unregister
1043 *
1044 * installs a callback to be called when wifi driver throughput (tx+rx)
1045 * crosses a threshold. Currently, we are using the same criteria as
1046 * TCP ack suppression (500 packets/100ms by default).
1047 *
1048 * Return: 0 : success
1049 * <0: failure
1050 */
1051
1052static int hnc_tput_hook(int install)
1053{
1054 int rc = 0;
1055
1056 /*
1057 * Nothing, until the bw_calculation accepts registration
1058 * it is now hardcoded in the wlan_hdd_main.c::hdd_bus_bw_compute_cbk
1059 * hdd_napi_throughput_policy(...)
1060 */
1061 return rc;
1062}
1063
1064/*
1065 * Implementation of hif_napi_cpu API
1066 */
1067
1068/**
1069 * hif_napi_cpu_init() - initialization of irq affinity block
1070 * @ctx: pointer to qca_napi_data
1071 *
1072 * called by hif_napi_create, after the first instance is called
1073 * - builds napi_rss_cpus table from cpu topology
1074 * - links cores of the same clusters together
1075 * - installs hot-plug notifier
1076 * - installs throughput trigger notifier (when such mechanism exists)
1077 *
1078 * Return: 0: OK
1079 * <0: error code
1080 */
1081int hif_napi_cpu_init(void *ctx)
1082{
1083 int rc = 0;
1084 int i;
1085 struct qca_napi_data *napid = (struct qca_napi_data *)ctx;
1086 struct qca_napi_cpu *cpus = napid->napi_cpu;
1087
1088 NAPI_DEBUG("--> ");
1089
1090 if (cpus[0].state != QCA_NAPI_CPU_UNINITIALIZED) {
1091 NAPI_DEBUG("NAPI RSS table already initialized.\n");
1092 rc = -EALREADY;
1093 goto lab_rss_init;
1094 }
1095
1096 /* build CPU topology table */
1097 for_each_possible_cpu(i) {
1098 cpus[i].state = ((cpumask_test_cpu(i, cpu_online_mask)
1099 ? QCA_NAPI_CPU_UP
1100 : QCA_NAPI_CPU_DOWN));
1101 cpus[i].core_id = topology_core_id(i);
1102 cpus[i].cluster_id = topology_physical_package_id(i);
1103 cpumask_copy(&(cpus[i].core_mask),
1104 topology_core_cpumask(i));
1105 cpumask_copy(&(cpus[i].thread_mask),
1106 topology_sibling_cpumask(i));
1107 cpus[i].max_freq = cpufreq_quick_get_max(i);
Orhan K AKYILDIZ7ce54e72016-05-16 12:36:32 -07001108 cpus[i].napis = 0x0;
1109 cpus[i].cluster_nxt = -1; /* invalid */
1110 }
1111
1112 /* link clusters together */
1113 rc = hnc_link_clusters(napid);
1114 if (0 != rc)
1115 goto lab_err_topology;
1116
1117 /* install hotplug notifier */
1118 rc = hnc_hotplug_hook(1);
1119 if (0 != rc)
1120 goto lab_err_hotplug;
1121
1122 /* install throughput notifier */
1123 rc = hnc_tput_hook(1);
1124 if (0 == rc)
1125 goto lab_rss_init;
1126
1127lab_err_hotplug:
1128 hnc_tput_hook(0);
1129 hnc_hotplug_hook(0);
1130lab_err_topology:
Orhan K AKYILDIZ84deb972016-10-25 12:54:21 -07001131 memset(napid->napi_cpu, 0, sizeof(struct qca_napi_cpu) * NR_CPUS);
Orhan K AKYILDIZ7ce54e72016-05-16 12:36:32 -07001132lab_rss_init:
1133 NAPI_DEBUG("<-- [rc=%d]", rc);
1134 return rc;
1135}
1136
1137/**
1138 * hif_napi_cpu_deinit() - clean-up of irq affinity block
1139 *
1140 * called by hif_napi_destroy, when the last instance is removed
1141 * - uninstalls throughput and hotplug notifiers
1142 * - clears cpu topology table
1143 * Return: 0: OK
1144 */
1145int hif_napi_cpu_deinit(void *ctx)
1146{
1147 int rc = 0;
1148 struct qca_napi_data *napid = (struct qca_napi_data *)ctx;
1149
1150 NAPI_DEBUG("-->%s(...)", __func__);
1151
1152 /* uninstall tput notifier */
1153 rc = hnc_tput_hook(0);
1154
1155 /* uninstall hotplug notifier */
1156 rc = hnc_hotplug_hook(0);
1157
1158 /* clear the topology table */
Orhan K AKYILDIZ84deb972016-10-25 12:54:21 -07001159 memset(napid->napi_cpu, 0, sizeof(struct qca_napi_cpu) * NR_CPUS);
Orhan K AKYILDIZ7ce54e72016-05-16 12:36:32 -07001160
1161 NAPI_DEBUG("<--%s[rc=%d]", __func__, rc);
1162
1163 return rc;
1164}
1165
1166/**
1167 * hncm_migrate_to() - migrates a NAPI to a CPU
1168 * @napid: pointer to NAPI block
1169 * @ce_id: CE_id of the NAPI instance
1170 * @didx : index in the CPU topology table for the CPU to migrate to
1171 *
1172 * Migrates NAPI (identified by the CE_id) to the destination core
1173 * Updates the napi_map of the destination entry
1174 *
1175 * Return:
1176 * =0 : success
1177 * <0 : error
1178 */
1179int hncm_migrate_to(struct qca_napi_data *napid,
1180 int napi_ce,
1181 int didx)
1182{
1183 int rc = 0;
1184 cpumask_t cpumask;
1185
1186 NAPI_DEBUG("-->%s(napi_cd=%d, didx=%d)", __func__, napi_ce, didx);
1187
Mohit Khanna865d8ff2016-10-06 19:58:02 -07001188 cpumask.bits[0] = (1 << didx);
Orhan K AKYILDIZ7ce54e72016-05-16 12:36:32 -07001189 rc = irq_set_affinity_hint(napid->napis[napi_ce].irq, &cpumask);
1190 napid->napi_cpu[didx].napis |= (1 << napi_ce);
1191
1192 NAPI_DEBUG("<--%s[%d]", __func__, rc);
1193 return rc;
1194}
1195/**
1196 * hncm_dest_cpu() - finds a destination CPU for NAPI
1197 * @napid: pointer to NAPI block
1198 * @act : RELOCATE | COLLAPSE | DISPERSE
1199 *
1200 * Finds the designated destionation for the next IRQ.
1201 * RELOCATE: translated to either COLLAPSE or DISPERSE based
1202 * on napid->napi_mode (throughput state)
1203 * COLLAPSE: All have the same destination: the first online CPU in lilcl
1204 * DISPERSE: One of the CPU in bigcl, which has the smallest number of
1205 * NAPIs on it
1206 *
1207 * Return: >=0 : index in the cpu topology table
1208 * : < 0 : error
1209 */
1210int hncm_dest_cpu(struct qca_napi_data *napid, int act)
1211{
1212 int destidx = -1;
1213 int head, i;
1214
1215 NAPI_DEBUG("-->%s(act=%d)", __func__, act);
1216 if (act == HNC_ACT_RELOCATE) {
1217 if (napid->napi_mode == QCA_NAPI_TPUT_LO)
1218 act = HNC_ACT_COLLAPSE;
1219 else
1220 act = HNC_ACT_DISPERSE;
1221 NAPI_DEBUG("%s: act changed from HNC_ACT_RELOCATE to %d",
1222 __func__, act);
1223 }
1224 if (act == HNC_ACT_COLLAPSE) {
1225 head = i = napid->lilcl_head;
1226retry_collapse:
1227 while (i >= 0) {
1228 if (napid->napi_cpu[i].state == QCA_NAPI_CPU_UP) {
1229 destidx = i;
1230 break;
1231 } else {
1232 i = napid->napi_cpu[i].cluster_nxt;
1233 }
1234 }
1235 if ((destidx < 0) && (head == napid->lilcl_head)) {
1236 NAPI_DEBUG("%s: COLLAPSE: no lilcl dest, try bigcl",
1237 __func__);
1238 head = i = napid->bigcl_head;
1239 goto retry_collapse;
1240 }
1241 } else { /* HNC_ACT_DISPERSE */
1242 int smallest = 99; /* all 32 bits full */
1243 int smallidx = -1;
1244
1245 head = i = napid->bigcl_head;
1246retry_disperse:
1247 while (i >= 0) {
1248 if ((napid->napi_cpu[i].state == QCA_NAPI_CPU_UP) &&
Orhan K AKYILDIZ6d9b7572016-09-25 21:31:50 -07001249 (hweight32(napid->napi_cpu[i].napis) <= smallest)) {
Orhan K AKYILDIZ7ce54e72016-05-16 12:36:32 -07001250 smallest = napid->napi_cpu[i].napis;
1251 smallidx = i;
1252 }
1253 i = napid->napi_cpu[i].cluster_nxt;
1254 }
1255 destidx = smallidx;
1256 if ((destidx < 0) && (head == napid->bigcl_head)) {
1257 NAPI_DEBUG("%s: DISPERSE: no bigcl dest, try lilcl",
1258 __func__);
1259 head = i = napid->lilcl_head;
1260 goto retry_disperse;
1261 }
1262 }
1263 NAPI_DEBUG("<--%s[dest=%d]", __func__, destidx);
1264 return destidx;
1265}
1266/**
1267 * hif_napi_cpu_migrate() - migrate IRQs away
1268 * @cpu: -1: all CPUs <n> specific CPU
1269 * @act: COLLAPSE | DISPERSE
1270 *
1271 * Moves IRQs/NAPIs from specific or all CPUs (specified by @cpu) to eligible
1272 * cores. Eligible cores are:
1273 * act=COLLAPSE -> the first online core of the little cluster
1274 * act=DISPERSE -> separate cores of the big cluster, so that each core will
1275 * host minimum number of NAPIs/IRQs (napid->cpus[cpu].napis)
1276 *
1277 * Note that this function is called with a spinlock acquired already.
1278 *
1279 * Return: =0: success
1280 * <0: error
1281 */
1282
1283int hif_napi_cpu_migrate(struct qca_napi_data *napid, int cpu, int action)
1284{
1285 int rc = 0;
1286 struct qca_napi_cpu *cpup;
1287 int i, dind;
1288 uint32_t napis;
1289
1290 NAPI_DEBUG("-->%s(.., cpu=%d, act=%d)",
1291 __func__, cpu, action);
1292 /* the following is really: hif_napi_enabled() with less overhead */
1293 if (napid->ce_map == 0) {
1294 NAPI_DEBUG("%s: NAPI disabled. Not migrating.", __func__);
1295 goto hncm_return;
1296 }
1297
1298 cpup = napid->napi_cpu;
1299
1300 switch (action) {
1301 case HNC_ACT_RELOCATE:
1302 case HNC_ACT_DISPERSE:
1303 case HNC_ACT_COLLAPSE: {
1304 /* first find the src napi set */
1305 if (cpu == HNC_ANY_CPU)
1306 napis = napid->ce_map;
1307 else
1308 napis = cpup[cpu].napis;
1309 /* then clear the napi bitmap on each CPU */
1310 for (i = 0; i < NR_CPUS; i++)
1311 cpup[i].napis = 0;
1312 /* then for each of the NAPIs to disperse: */
1313 for (i = 0; i < CE_COUNT_MAX; i++)
1314 if (napis & (1 << i)) {
1315 /* find a destination CPU */
1316 dind = hncm_dest_cpu(napid, action);
1317 if (dind >= 0) {
1318 NAPI_DEBUG("Migrating NAPI ce%d to %d",
1319 i, dind);
1320 rc = hncm_migrate_to(napid, i, dind);
1321 } else {
1322 NAPI_DEBUG("No dest for NAPI ce%d", i);
1323 hnc_dump_cpus(napid);
1324 rc = -1;
1325 }
1326 }
1327 break;
1328 }
1329 default: {
1330 NAPI_DEBUG("%s: bad action: %d\n", __func__, action);
1331 QDF_BUG(0);
1332 break;
1333 }
1334 } /* switch action */
1335
1336hncm_return:
1337 hnc_dump_cpus(napid);
1338 return rc;
1339}
1340
1341/**
1342 * hif_napi_cpu_blacklist() - calls kernel API to enable/disable blacklisting
1343 *
1344 * Return: from the API
1345 */
1346int hif_napi_cpu_blacklist(bool is_on)
1347{
1348 int rc = 0;
Orhan K AKYILDIZ458fefc2016-09-16 00:43:38 -07001349 static int ref_count; /* = 0 by the compiler */
Orhan K AKYILDIZ7ce54e72016-05-16 12:36:32 -07001350
1351 NAPI_DEBUG("-->%s(%d)", __func__, is_on);
Orhan K AKYILDIZ458fefc2016-09-16 00:43:38 -07001352 if (is_on) {
1353 ref_count++;
Orhan K AKYILDIZ7ce54e72016-05-16 12:36:32 -07001354 rc = irq_blacklist_on();
Orhan K AKYILDIZ458fefc2016-09-16 00:43:38 -07001355 } else {
1356 do {
1357 rc = irq_blacklist_off();
1358 ref_count--;
1359 } while (ref_count > 0);
1360 }
Orhan K AKYILDIZ7ce54e72016-05-16 12:36:32 -07001361
1362 NAPI_DEBUG("<--%s[%d]", __func__, rc);
1363 return rc;
1364}
1365
Orhan K AKYILDIZ458fefc2016-09-16 00:43:38 -07001366/**
1367 * hif_napi_serialize() - [de-]serialize NAPI operations
1368 * @hif: context
1369 * @is_on: 1: serialize, 0: deserialize
1370 *
1371 * hif_napi_serialize(hif, 1) can be called multiple times. It will perform the
1372 * following steps (see hif_napi_event for code):
1373 * - put irqs of all NAPI instances on the same CPU
1374 * - only for the first serialize call: blacklist
1375 *
1376 * hif_napi_serialize(hif, 0):
1377 * - start a timer (multiple of BusBandwidthTimer -- default: 100 msec)
1378 * - at the end of the timer, check the current throughput state and
1379 * implement it.
1380 */
1381static unsigned long napi_serialize_reqs;
1382int hif_napi_serialize(struct hif_opaque_softc *hif, int is_on)
1383{
1384 int rc = -EINVAL;
1385
1386 if (hif != NULL)
1387 switch (is_on) {
1388 case 0: { /* de-serialize */
1389 rc = hif_napi_event(hif, NAPI_EVT_USR_NORMAL,
1390 (void *) 0);
1391 napi_serialize_reqs = 0;
1392 break;
1393 } /* end de-serialize */
1394 case 1: { /* serialize */
1395 rc = hif_napi_event(hif, NAPI_EVT_USR_SERIAL,
1396 (void *)napi_serialize_reqs++);
1397 break;
1398 } /* end serialize */
1399 default:
1400 break; /* no-op */
1401 } /* switch */
1402 return rc;
1403}
1404
Orhan K AKYILDIZ7ce54e72016-05-16 12:36:32 -07001405#endif /* ifdef HELIUMPLUS */