xref: /wlan-dirver/qca-wifi-host-cmn/hif/src/ce/ce_service.c (revision 45a38684b07295822dc8eba39e293408f203eec8)
1 /*
2  * Copyright (c) 2013-2020 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 #include "hif.h"
20 #include "hif_io32.h"
21 #include "ce_api.h"
22 #include "ce_main.h"
23 #include "ce_internal.h"
24 #include "ce_reg.h"
25 #include "qdf_lock.h"
26 #include "regtable.h"
27 #include "hif_main.h"
28 #include "hif_debug.h"
29 #include "hif_napi.h"
30 #include "qdf_module.h"
31 
32 #ifdef IPA_OFFLOAD
33 #ifdef QCA_WIFI_3_0
34 #define CE_IPA_RING_INIT(ce_desc)                       \
35 	do {                                            \
36 		ce_desc->gather = 0;                    \
37 		ce_desc->enable_11h = 0;                \
38 		ce_desc->meta_data_low = 0;             \
39 		ce_desc->packet_result_offset = 64;     \
40 		ce_desc->toeplitz_hash_enable = 0;      \
41 		ce_desc->addr_y_search_disable = 0;     \
42 		ce_desc->addr_x_search_disable = 0;     \
43 		ce_desc->misc_int_disable = 0;          \
44 		ce_desc->target_int_disable = 0;        \
45 		ce_desc->host_int_disable = 0;          \
46 		ce_desc->dest_byte_swap = 0;            \
47 		ce_desc->byte_swap = 0;                 \
48 		ce_desc->type = 2;                      \
49 		ce_desc->tx_classify = 1;               \
50 		ce_desc->buffer_addr_hi = 0;            \
51 		ce_desc->meta_data = 0;                 \
52 		ce_desc->nbytes = 128;                  \
53 	} while (0)
54 #else
55 #define CE_IPA_RING_INIT(ce_desc)                       \
56 	do {                                            \
57 		ce_desc->byte_swap = 0;                 \
58 		ce_desc->nbytes = 60;                   \
59 		ce_desc->gather = 0;                    \
60 	} while (0)
61 #endif /* QCA_WIFI_3_0 */
62 #endif /* IPA_OFFLOAD */
63 
64 static int war1_allow_sleep;
65 /* io32 write workaround */
66 static int hif_ce_war1;
67 
68 /**
69  * hif_ce_war_disable() - disable ce war gobally
70  */
71 void hif_ce_war_disable(void)
72 {
73 	hif_ce_war1 = 0;
74 }
75 
76 /**
77  * hif_ce_war_enable() - enable ce war gobally
78  */
79 void hif_ce_war_enable(void)
80 {
81 	hif_ce_war1 = 1;
82 }
83 
84 /*
85  * Note: For MCL, #if defined (HIF_CONFIG_SLUB_DEBUG_ON) needs to be checked
86  * for defined here
87  */
88 #if defined(HIF_CONFIG_SLUB_DEBUG_ON) || defined(HIF_CE_DEBUG_DATA_BUF)
89 
90 #define CE_DEBUG_PRINT_BUF_SIZE(x) (((x) * 3) - 1)
91 #define CE_DEBUG_DATA_PER_ROW 16
92 
93 static const char *ce_event_type_to_str(enum hif_ce_event_type type);
94 
95 int get_next_record_index(qdf_atomic_t *table_index, int array_size)
96 {
97 	int record_index = qdf_atomic_inc_return(table_index);
98 
99 	if (record_index == array_size)
100 		qdf_atomic_sub(array_size, table_index);
101 
102 	while (record_index >= array_size)
103 		record_index -= array_size;
104 
105 	return record_index;
106 }
107 
108 qdf_export_symbol(get_next_record_index);
109 
110 #ifdef HIF_CE_DEBUG_DATA_BUF
111 void hif_ce_desc_data_record(struct hif_ce_desc_event *event, int len)
112 {
113 	uint8_t *data = NULL;
114 
115 	if (!event->data) {
116 		hif_err_rl("No ce debug memory allocated");
117 		return;
118 	}
119 
120 	if (event->memory && len > 0)
121 		data = qdf_nbuf_data((qdf_nbuf_t)event->memory);
122 
123 	event->actual_data_len = 0;
124 	qdf_mem_zero(event->data, CE_DEBUG_MAX_DATA_BUF_SIZE);
125 
126 	if (data && len > 0) {
127 		qdf_mem_copy(event->data, data,
128 				((len < CE_DEBUG_MAX_DATA_BUF_SIZE) ?
129 				 len : CE_DEBUG_MAX_DATA_BUF_SIZE));
130 		event->actual_data_len = len;
131 	}
132 }
133 
134 qdf_export_symbol(hif_ce_desc_data_record);
135 
136 void hif_clear_ce_desc_debug_data(struct hif_ce_desc_event *event)
137 {
138 	qdf_mem_zero(event,
139 		     offsetof(struct hif_ce_desc_event, data));
140 }
141 
142 qdf_export_symbol(hif_clear_ce_desc_debug_data);
143 #else
144 void hif_clear_ce_desc_debug_data(struct hif_ce_desc_event *event)
145 {
146 	qdf_mem_zero(event, sizeof(struct hif_ce_desc_event));
147 }
148 
149 qdf_export_symbol(hif_clear_ce_desc_debug_data);
150 #endif /* HIF_CE_DEBUG_DATA_BUF */
151 
152 #if defined(HIF_RECORD_PADDR)
153 void hif_ce_desc_record_rx_paddr(struct hif_softc *scn,
154 				 struct hif_ce_desc_event *event,
155 				 qdf_nbuf_t memory)
156 {
157 	if (memory) {
158 		event->dma_addr = QDF_NBUF_CB_PADDR(memory);
159 		event->dma_to_phy = qdf_mem_paddr_from_dmaaddr(
160 					scn->qdf_dev,
161 					event->dma_addr);
162 
163 		event->virt_to_phy =
164 			virt_to_phys(qdf_nbuf_data(memory));
165 	}
166 }
167 #endif /* HIF_RECORD_RX_PADDR */
168 
169 /**
170  * hif_record_ce_desc_event() - record ce descriptor events
171  * @scn: hif_softc
172  * @ce_id: which ce is the event occurring on
173  * @type: what happened
174  * @descriptor: pointer to the descriptor posted/completed
175  * @memory: virtual address of buffer related to the descriptor
176  * @index: index that the descriptor was/will be at.
177  */
178 void hif_record_ce_desc_event(struct hif_softc *scn, int ce_id,
179 				enum hif_ce_event_type type,
180 				union ce_desc *descriptor,
181 				void *memory, int index,
182 				int len)
183 {
184 	int record_index;
185 	struct hif_ce_desc_event *event;
186 
187 	struct ce_desc_hist *ce_hist = &scn->hif_ce_desc_hist;
188 	struct hif_ce_desc_event *hist_ev = NULL;
189 
190 	if (ce_id < CE_COUNT_MAX)
191 		hist_ev = (struct hif_ce_desc_event *)ce_hist->hist_ev[ce_id];
192 	else
193 		return;
194 
195 	if (ce_id >= CE_COUNT_MAX)
196 		return;
197 
198 	if (!ce_hist->enable[ce_id])
199 		return;
200 
201 	if (!hist_ev)
202 		return;
203 
204 	record_index = get_next_record_index(
205 			&ce_hist->history_index[ce_id], HIF_CE_HISTORY_MAX);
206 
207 	event = &hist_ev[record_index];
208 
209 	hif_clear_ce_desc_debug_data(event);
210 
211 	event->type = type;
212 	event->time = qdf_get_log_timestamp();
213 
214 	if (descriptor)
215 		qdf_mem_copy(&event->descriptor, descriptor,
216 			     sizeof(union ce_desc));
217 
218 	event->memory = memory;
219 	event->index = index;
220 
221 	if (event->type == HIF_RX_DESC_POST ||
222 	    event->type == HIF_RX_DESC_COMPLETION)
223 		hif_ce_desc_record_rx_paddr(scn, event, memory);
224 
225 	if (ce_hist->data_enable[ce_id])
226 		hif_ce_desc_data_record(event, len);
227 }
228 qdf_export_symbol(hif_record_ce_desc_event);
229 
230 /**
231  * ce_init_ce_desc_event_log() - initialize the ce event log
232  * @ce_id: copy engine id for which we are initializing the log
233  * @size: size of array to dedicate
234  *
235  * Currently the passed size is ignored in favor of a precompiled value.
236  */
237 void ce_init_ce_desc_event_log(struct hif_softc *scn, int ce_id, int size)
238 {
239 	struct ce_desc_hist *ce_hist = &scn->hif_ce_desc_hist;
240 	qdf_atomic_init(&ce_hist->history_index[ce_id]);
241 	qdf_mutex_create(&ce_hist->ce_dbg_datamem_lock[ce_id]);
242 }
243 
244 /**
245  * ce_deinit_ce_desc_event_log() - deinitialize the ce event log
246  * @ce_id: copy engine id for which we are deinitializing the log
247  *
248  */
249 inline void ce_deinit_ce_desc_event_log(struct hif_softc *scn, int ce_id)
250 {
251 	struct ce_desc_hist *ce_hist = &scn->hif_ce_desc_hist;
252 
253 	qdf_mutex_destroy(&ce_hist->ce_dbg_datamem_lock[ce_id]);
254 }
255 
256 #else /* (HIF_CONFIG_SLUB_DEBUG_ON) || defined(HIF_CE_DEBUG_DATA_BUF) */
257 void hif_record_ce_desc_event(struct hif_softc *scn,
258 		int ce_id, enum hif_ce_event_type type,
259 		union ce_desc *descriptor, void *memory,
260 		int index, int len)
261 {
262 }
263 qdf_export_symbol(hif_record_ce_desc_event);
264 
265 inline void ce_init_ce_desc_event_log(struct hif_softc *scn, int ce_id,
266 					int size)
267 {
268 }
269 
270 void ce_deinit_ce_desc_event_log(struct hif_softc *scn, int ce_id)
271 {
272 }
273 #endif /*defined(HIF_CONFIG_SLUB_DEBUG_ON) || defined(HIF_CE_DEBUG_DATA_BUF) */
274 
275 #ifdef NAPI_YIELD_BUDGET_BASED
276 bool hif_ce_service_should_yield(struct hif_softc *scn,
277 				 struct CE_state *ce_state)
278 {
279 	bool yield =  hif_max_num_receives_reached(scn, ce_state->receive_count);
280 
281 	/* Setting receive_count to MAX_NUM_OF_RECEIVES when this count goes
282 	 * beyond MAX_NUM_OF_RECEIVES for NAPI backet calulation issue. This
283 	 * can happen in fast path handling as processing is happenning in
284 	 * batches.
285 	 */
286 	if (yield)
287 		ce_state->receive_count = MAX_NUM_OF_RECEIVES;
288 
289 	return yield;
290 }
291 #else
292 /**
293  * hif_ce_service_should_yield() - return true if the service is hogging the cpu
294  * @scn: hif context
295  * @ce_state: context of the copy engine being serviced
296  *
297  * Return: true if the service should yield
298  */
299 bool hif_ce_service_should_yield(struct hif_softc *scn,
300 				 struct CE_state *ce_state)
301 {
302 	bool yield, time_limit_reached, rxpkt_thresh_reached = 0;
303 
304 	time_limit_reached =
305 		sched_clock() > ce_state->ce_service_yield_time ? 1 : 0;
306 
307 	if (!time_limit_reached)
308 		rxpkt_thresh_reached = hif_max_num_receives_reached
309 					(scn, ce_state->receive_count);
310 
311 	/* Setting receive_count to MAX_NUM_OF_RECEIVES when this count goes
312 	 * beyond MAX_NUM_OF_RECEIVES for NAPI backet calulation issue. This
313 	 * can happen in fast path handling as processing is happenning in
314 	 * batches.
315 	 */
316 	if (rxpkt_thresh_reached)
317 		ce_state->receive_count = MAX_NUM_OF_RECEIVES;
318 
319 	yield =  time_limit_reached || rxpkt_thresh_reached;
320 
321 	if (yield &&
322 	    ce_state->htt_rx_data &&
323 	    hif_napi_enabled(GET_HIF_OPAQUE_HDL(scn), ce_state->id)) {
324 		hif_napi_update_yield_stats(ce_state,
325 					    time_limit_reached,
326 					    rxpkt_thresh_reached);
327 	}
328 
329 	return yield;
330 }
331 qdf_export_symbol(hif_ce_service_should_yield);
332 #endif
333 
334 /*
335  * Guts of ce_send, used by both ce_send and ce_sendlist_send.
336  * The caller takes responsibility for any needed locking.
337  */
338 
339 void war_ce_src_ring_write_idx_set(struct hif_softc *scn,
340 				   u32 ctrl_addr, unsigned int write_index)
341 {
342 	if (hif_ce_war1) {
343 		void __iomem *indicator_addr;
344 
345 		indicator_addr = scn->mem + ctrl_addr + DST_WATERMARK_ADDRESS;
346 
347 		if (!war1_allow_sleep
348 		    && ctrl_addr == CE_BASE_ADDRESS(CDC_WAR_DATA_CE)) {
349 			hif_write32_mb(scn, indicator_addr,
350 				       (CDC_WAR_MAGIC_STR | write_index));
351 		} else {
352 			unsigned long irq_flags;
353 
354 			local_irq_save(irq_flags);
355 			hif_write32_mb(scn, indicator_addr, 1);
356 
357 			/*
358 			 * PCIE write waits for ACK in IPQ8K, there is no
359 			 * need to read back value.
360 			 */
361 			(void)hif_read32_mb(scn, indicator_addr);
362 			/* conservative */
363 			(void)hif_read32_mb(scn, indicator_addr);
364 
365 			CE_SRC_RING_WRITE_IDX_SET(scn,
366 						  ctrl_addr, write_index);
367 
368 			hif_write32_mb(scn, indicator_addr, 0);
369 			local_irq_restore(irq_flags);
370 		}
371 	} else {
372 		CE_SRC_RING_WRITE_IDX_SET(scn, ctrl_addr, write_index);
373 	}
374 }
375 
376 qdf_export_symbol(war_ce_src_ring_write_idx_set);
377 
378 QDF_STATUS
379 ce_send(struct CE_handle *copyeng,
380 		void *per_transfer_context,
381 		qdf_dma_addr_t buffer,
382 		uint32_t nbytes,
383 		uint32_t transfer_id,
384 		uint32_t flags,
385 		uint32_t user_flag)
386 {
387 	struct CE_state *CE_state = (struct CE_state *)copyeng;
388 	QDF_STATUS status;
389 	struct HIF_CE_state *hif_state = HIF_GET_CE_STATE(CE_state->scn);
390 
391 	qdf_spin_lock_bh(&CE_state->ce_index_lock);
392 	status = hif_state->ce_services->ce_send_nolock(copyeng,
393 			per_transfer_context, buffer, nbytes,
394 			transfer_id, flags, user_flag);
395 	qdf_spin_unlock_bh(&CE_state->ce_index_lock);
396 
397 	return status;
398 }
399 qdf_export_symbol(ce_send);
400 
401 unsigned int ce_sendlist_sizeof(void)
402 {
403 	return sizeof(struct ce_sendlist);
404 }
405 
406 void ce_sendlist_init(struct ce_sendlist *sendlist)
407 {
408 	struct ce_sendlist_s *sl = (struct ce_sendlist_s *)sendlist;
409 
410 	sl->num_items = 0;
411 }
412 
413 QDF_STATUS
414 ce_sendlist_buf_add(struct ce_sendlist *sendlist,
415 					qdf_dma_addr_t buffer,
416 					uint32_t nbytes,
417 					uint32_t flags,
418 					uint32_t user_flags)
419 {
420 	struct ce_sendlist_s *sl = (struct ce_sendlist_s *)sendlist;
421 	unsigned int num_items = sl->num_items;
422 	struct ce_sendlist_item *item;
423 
424 	if (num_items >= CE_SENDLIST_ITEMS_MAX) {
425 		QDF_ASSERT(num_items < CE_SENDLIST_ITEMS_MAX);
426 		return QDF_STATUS_E_RESOURCES;
427 	}
428 
429 	item = &sl->item[num_items];
430 	item->send_type = CE_SIMPLE_BUFFER_TYPE;
431 	item->data = buffer;
432 	item->u.nbytes = nbytes;
433 	item->flags = flags;
434 	item->user_flags = user_flags;
435 	sl->num_items = num_items + 1;
436 	return QDF_STATUS_SUCCESS;
437 }
438 
439 QDF_STATUS
440 ce_sendlist_send(struct CE_handle *copyeng,
441 		 void *per_transfer_context,
442 		 struct ce_sendlist *sendlist, unsigned int transfer_id)
443 {
444 	struct CE_state *CE_state = (struct CE_state *)copyeng;
445 	struct HIF_CE_state *hif_state = HIF_GET_CE_STATE(CE_state->scn);
446 
447 	return hif_state->ce_services->ce_sendlist_send(copyeng,
448 			per_transfer_context, sendlist, transfer_id);
449 }
450 
451 #ifndef AH_NEED_TX_DATA_SWAP
452 #define AH_NEED_TX_DATA_SWAP 0
453 #endif
454 
455 /**
456  * ce_batch_send() - sends bunch of msdus at once
457  * @ce_tx_hdl : pointer to CE handle
458  * @msdu : list of msdus to be sent
459  * @transfer_id : transfer id
460  * @len : Downloaded length
461  * @sendhead : sendhead
462  *
463  * Assumption : Called with an array of MSDU's
464  * Function:
465  * For each msdu in the array
466  * 1. Send each msdu
467  * 2. Increment write index accordinlgy.
468  *
469  * Return: list of msds not sent
470  */
471 qdf_nbuf_t ce_batch_send(struct CE_handle *ce_tx_hdl,  qdf_nbuf_t msdu,
472 		uint32_t transfer_id, u_int32_t len, uint32_t sendhead)
473 {
474 	struct CE_state *ce_state = (struct CE_state *)ce_tx_hdl;
475 	struct hif_softc *scn = ce_state->scn;
476 	struct CE_ring_state *src_ring = ce_state->src_ring;
477 	u_int32_t ctrl_addr = ce_state->ctrl_addr;
478 	/*  A_target_id_t targid = TARGID(scn);*/
479 
480 	uint32_t nentries_mask = src_ring->nentries_mask;
481 	uint32_t sw_index, write_index;
482 
483 	struct CE_src_desc *src_desc_base =
484 		(struct CE_src_desc *)src_ring->base_addr_owner_space;
485 	uint32_t *src_desc;
486 
487 	struct CE_src_desc lsrc_desc = {0};
488 	int deltacount = 0;
489 	qdf_nbuf_t freelist = NULL, hfreelist = NULL, tempnext;
490 
491 	DATA_CE_UPDATE_SWINDEX(src_ring->sw_index, scn, ctrl_addr);
492 	sw_index = src_ring->sw_index;
493 	write_index = src_ring->write_index;
494 
495 	deltacount = CE_RING_DELTA(nentries_mask, write_index, sw_index-1);
496 
497 	while (msdu) {
498 		tempnext = qdf_nbuf_next(msdu);
499 
500 		if (deltacount < 2) {
501 			if (sendhead)
502 				return msdu;
503 			HIF_ERROR("%s: Out of descriptors", __func__);
504 			src_ring->write_index = write_index;
505 			war_ce_src_ring_write_idx_set(scn, ctrl_addr,
506 					write_index);
507 
508 			sw_index = src_ring->sw_index;
509 			write_index = src_ring->write_index;
510 
511 			deltacount = CE_RING_DELTA(nentries_mask, write_index,
512 					sw_index-1);
513 			if (!freelist) {
514 				freelist = msdu;
515 				hfreelist = msdu;
516 			} else {
517 				qdf_nbuf_set_next(freelist, msdu);
518 				freelist = msdu;
519 			}
520 			qdf_nbuf_set_next(msdu, NULL);
521 			msdu = tempnext;
522 			continue;
523 		}
524 
525 		src_desc = (uint32_t *)CE_SRC_RING_TO_DESC(src_desc_base,
526 				write_index);
527 
528 		src_desc[0]   = qdf_nbuf_get_frag_paddr(msdu, 0);
529 
530 		lsrc_desc.meta_data = transfer_id;
531 		if (len  > msdu->len)
532 			len =  msdu->len;
533 		lsrc_desc.nbytes = len;
534 		/*  Data packet is a byte stream, so disable byte swap */
535 		lsrc_desc.byte_swap = AH_NEED_TX_DATA_SWAP;
536 		lsrc_desc.gather    = 0; /*For the last one, gather is not set*/
537 
538 		src_desc[1] = ((uint32_t *)&lsrc_desc)[1];
539 
540 
541 		src_ring->per_transfer_context[write_index] = msdu;
542 		write_index = CE_RING_IDX_INCR(nentries_mask, write_index);
543 
544 		if (sendhead)
545 			break;
546 		qdf_nbuf_set_next(msdu, NULL);
547 		msdu = tempnext;
548 
549 	}
550 
551 
552 	src_ring->write_index = write_index;
553 	war_ce_src_ring_write_idx_set(scn, ctrl_addr, write_index);
554 
555 	return hfreelist;
556 }
557 
558 /**
559  * ce_update_tx_ring() - Advance sw index.
560  * @ce_tx_hdl : pointer to CE handle
561  * @num_htt_cmpls : htt completions received.
562  *
563  * Function:
564  * Increment the value of sw index of src ring
565  * according to number of htt completions
566  * received.
567  *
568  * Return: void
569  */
570 #ifdef DATA_CE_SW_INDEX_NO_INLINE_UPDATE
571 void ce_update_tx_ring(struct CE_handle *ce_tx_hdl, uint32_t num_htt_cmpls)
572 {
573 	struct CE_state *ce_state = (struct CE_state *)ce_tx_hdl;
574 	struct CE_ring_state *src_ring = ce_state->src_ring;
575 	uint32_t nentries_mask = src_ring->nentries_mask;
576 	/*
577 	 * Advance the s/w index:
578 	 * This effectively simulates completing the CE ring descriptors
579 	 */
580 	src_ring->sw_index =
581 		CE_RING_IDX_ADD(nentries_mask, src_ring->sw_index,
582 				num_htt_cmpls);
583 }
584 #else
585 void ce_update_tx_ring(struct CE_handle *ce_tx_hdl, uint32_t num_htt_cmpls)
586 {}
587 #endif
588 
589 /**
590  * ce_send_single() - sends
591  * @ce_tx_hdl : pointer to CE handle
592  * @msdu : msdu to be sent
593  * @transfer_id : transfer id
594  * @len : Downloaded length
595  *
596  * Function:
597  * 1. Send one msdu
598  * 2. Increment write index of src ring accordinlgy.
599  *
600  * Return: QDF_STATUS: CE sent status
601  */
602 QDF_STATUS ce_send_single(struct CE_handle *ce_tx_hdl, qdf_nbuf_t msdu,
603 			  uint32_t transfer_id, u_int32_t len)
604 {
605 	struct CE_state *ce_state = (struct CE_state *)ce_tx_hdl;
606 	struct hif_softc *scn = ce_state->scn;
607 	struct CE_ring_state *src_ring = ce_state->src_ring;
608 	uint32_t ctrl_addr = ce_state->ctrl_addr;
609 	/*A_target_id_t targid = TARGID(scn);*/
610 
611 	uint32_t nentries_mask = src_ring->nentries_mask;
612 	uint32_t sw_index, write_index;
613 
614 	struct CE_src_desc *src_desc_base =
615 		(struct CE_src_desc *)src_ring->base_addr_owner_space;
616 	uint32_t *src_desc;
617 
618 	struct CE_src_desc lsrc_desc = {0};
619 	enum hif_ce_event_type event_type;
620 
621 	DATA_CE_UPDATE_SWINDEX(src_ring->sw_index, scn, ctrl_addr);
622 	sw_index = src_ring->sw_index;
623 	write_index = src_ring->write_index;
624 
625 	if (qdf_unlikely(CE_RING_DELTA(nentries_mask, write_index,
626 					sw_index-1) < 1)) {
627 		/* ol_tx_stats_inc_ring_error(sc->scn->pdev_txrx_handle, 1); */
628 		HIF_ERROR("%s: ce send fail %d %d %d", __func__, nentries_mask,
629 			  write_index, sw_index);
630 		return QDF_STATUS_E_RESOURCES;
631 	}
632 
633 	src_desc = (uint32_t *)CE_SRC_RING_TO_DESC(src_desc_base, write_index);
634 
635 	src_desc[0] = qdf_nbuf_get_frag_paddr(msdu, 0);
636 
637 	lsrc_desc.meta_data = transfer_id;
638 	lsrc_desc.nbytes = len;
639 	/*  Data packet is a byte stream, so disable byte swap */
640 	lsrc_desc.byte_swap = AH_NEED_TX_DATA_SWAP;
641 	lsrc_desc.gather    = 0; /* For the last one, gather is not set */
642 
643 	src_desc[1] = ((uint32_t *)&lsrc_desc)[1];
644 
645 
646 	src_ring->per_transfer_context[write_index] = msdu;
647 
648 	if (((struct CE_src_desc *)src_desc)->gather)
649 		event_type = HIF_TX_GATHER_DESC_POST;
650 	else if (qdf_unlikely(ce_state->state != CE_RUNNING))
651 		event_type = HIF_TX_DESC_SOFTWARE_POST;
652 	else
653 		event_type = HIF_TX_DESC_POST;
654 
655 	hif_record_ce_desc_event(scn, ce_state->id, event_type,
656 				(union ce_desc *)src_desc, msdu,
657 				write_index, len);
658 
659 	write_index = CE_RING_IDX_INCR(nentries_mask, write_index);
660 
661 	src_ring->write_index = write_index;
662 
663 	war_ce_src_ring_write_idx_set(scn, ctrl_addr, write_index);
664 
665 	return QDF_STATUS_SUCCESS;
666 }
667 
668 /**
669  * ce_recv_buf_enqueue() - enqueue a recv buffer into a copy engine
670  * @coyeng: copy engine handle
671  * @per_recv_context: virtual address of the nbuf
672  * @buffer: physical address of the nbuf
673  *
674  * Return: QDF_STATUS_SUCCESS if the buffer is enqueued
675  */
676 QDF_STATUS
677 ce_recv_buf_enqueue(struct CE_handle *copyeng,
678 		    void *per_recv_context, qdf_dma_addr_t buffer)
679 {
680 	struct CE_state *CE_state = (struct CE_state *)copyeng;
681 	struct HIF_CE_state *hif_state = HIF_GET_CE_STATE(CE_state->scn);
682 
683 	return hif_state->ce_services->ce_recv_buf_enqueue(copyeng,
684 			per_recv_context, buffer);
685 }
686 qdf_export_symbol(ce_recv_buf_enqueue);
687 
688 void
689 ce_send_watermarks_set(struct CE_handle *copyeng,
690 		       unsigned int low_alert_nentries,
691 		       unsigned int high_alert_nentries)
692 {
693 	struct CE_state *CE_state = (struct CE_state *)copyeng;
694 	uint32_t ctrl_addr = CE_state->ctrl_addr;
695 	struct hif_softc *scn = CE_state->scn;
696 
697 	CE_SRC_RING_LOWMARK_SET(scn, ctrl_addr, low_alert_nentries);
698 	CE_SRC_RING_HIGHMARK_SET(scn, ctrl_addr, high_alert_nentries);
699 }
700 
701 void
702 ce_recv_watermarks_set(struct CE_handle *copyeng,
703 		       unsigned int low_alert_nentries,
704 		       unsigned int high_alert_nentries)
705 {
706 	struct CE_state *CE_state = (struct CE_state *)copyeng;
707 	uint32_t ctrl_addr = CE_state->ctrl_addr;
708 	struct hif_softc *scn = CE_state->scn;
709 
710 	CE_DEST_RING_LOWMARK_SET(scn, ctrl_addr,
711 				low_alert_nentries);
712 	CE_DEST_RING_HIGHMARK_SET(scn, ctrl_addr,
713 				high_alert_nentries);
714 }
715 
716 unsigned int ce_send_entries_avail(struct CE_handle *copyeng)
717 {
718 	struct CE_state *CE_state = (struct CE_state *)copyeng;
719 	struct CE_ring_state *src_ring = CE_state->src_ring;
720 	unsigned int nentries_mask = src_ring->nentries_mask;
721 	unsigned int sw_index;
722 	unsigned int write_index;
723 
724 	qdf_spin_lock(&CE_state->ce_index_lock);
725 	sw_index = src_ring->sw_index;
726 	write_index = src_ring->write_index;
727 	qdf_spin_unlock(&CE_state->ce_index_lock);
728 
729 	return CE_RING_DELTA(nentries_mask, write_index, sw_index - 1);
730 }
731 
732 unsigned int ce_recv_entries_avail(struct CE_handle *copyeng)
733 {
734 	struct CE_state *CE_state = (struct CE_state *)copyeng;
735 	struct CE_ring_state *dest_ring = CE_state->dest_ring;
736 	unsigned int nentries_mask = dest_ring->nentries_mask;
737 	unsigned int sw_index;
738 	unsigned int write_index;
739 
740 	qdf_spin_lock(&CE_state->ce_index_lock);
741 	sw_index = dest_ring->sw_index;
742 	write_index = dest_ring->write_index;
743 	qdf_spin_unlock(&CE_state->ce_index_lock);
744 
745 	return CE_RING_DELTA(nentries_mask, write_index, sw_index - 1);
746 }
747 
748 /*
749  * Guts of ce_completed_recv_next.
750  * The caller takes responsibility for any necessary locking.
751  */
752 QDF_STATUS
753 ce_completed_recv_next(struct CE_handle *copyeng,
754 		       void **per_CE_contextp,
755 		       void **per_transfer_contextp,
756 		       qdf_dma_addr_t *bufferp,
757 		       unsigned int *nbytesp,
758 		       unsigned int *transfer_idp, unsigned int *flagsp)
759 {
760 	struct CE_state *CE_state = (struct CE_state *)copyeng;
761 	QDF_STATUS status;
762 	struct hif_softc *scn = CE_state->scn;
763 	struct HIF_CE_state *hif_state = HIF_GET_CE_STATE(scn);
764 	struct ce_ops *ce_services;
765 
766 	ce_services = hif_state->ce_services;
767 	qdf_spin_lock_bh(&CE_state->ce_index_lock);
768 	status =
769 		ce_services->ce_completed_recv_next_nolock(CE_state,
770 				per_CE_contextp, per_transfer_contextp, bufferp,
771 					      nbytesp, transfer_idp, flagsp);
772 	qdf_spin_unlock_bh(&CE_state->ce_index_lock);
773 
774 	return status;
775 }
776 
777 QDF_STATUS
778 ce_revoke_recv_next(struct CE_handle *copyeng,
779 		    void **per_CE_contextp,
780 		    void **per_transfer_contextp, qdf_dma_addr_t *bufferp)
781 {
782 	struct CE_state *CE_state = (struct CE_state *)copyeng;
783 	struct HIF_CE_state *hif_state = HIF_GET_CE_STATE(CE_state->scn);
784 
785 	return hif_state->ce_services->ce_revoke_recv_next(copyeng,
786 			per_CE_contextp, per_transfer_contextp, bufferp);
787 }
788 
789 QDF_STATUS
790 ce_cancel_send_next(struct CE_handle *copyeng,
791 		void **per_CE_contextp,
792 		void **per_transfer_contextp,
793 		qdf_dma_addr_t *bufferp,
794 		unsigned int *nbytesp,
795 		unsigned int *transfer_idp,
796 		uint32_t *toeplitz_hash_result)
797 {
798 	struct CE_state *CE_state = (struct CE_state *)copyeng;
799 	struct HIF_CE_state *hif_state = HIF_GET_CE_STATE(CE_state->scn);
800 
801 	return hif_state->ce_services->ce_cancel_send_next
802 		(copyeng, per_CE_contextp, per_transfer_contextp,
803 		 bufferp, nbytesp, transfer_idp, toeplitz_hash_result);
804 }
805 qdf_export_symbol(ce_cancel_send_next);
806 
807 QDF_STATUS
808 ce_completed_send_next(struct CE_handle *copyeng,
809 		       void **per_CE_contextp,
810 		       void **per_transfer_contextp,
811 		       qdf_dma_addr_t *bufferp,
812 		       unsigned int *nbytesp,
813 		       unsigned int *transfer_idp,
814 		       unsigned int *sw_idx,
815 		       unsigned int *hw_idx,
816 		       unsigned int *toeplitz_hash_result)
817 {
818 	struct CE_state *CE_state = (struct CE_state *)copyeng;
819 	struct hif_softc *scn = CE_state->scn;
820 	struct HIF_CE_state *hif_state = HIF_GET_CE_STATE(scn);
821 	struct ce_ops *ce_services;
822 	QDF_STATUS status;
823 
824 	ce_services = hif_state->ce_services;
825 	qdf_spin_lock_bh(&CE_state->ce_index_lock);
826 	status =
827 		ce_services->ce_completed_send_next_nolock(CE_state,
828 					per_CE_contextp, per_transfer_contextp,
829 					bufferp, nbytesp, transfer_idp, sw_idx,
830 					      hw_idx, toeplitz_hash_result);
831 	qdf_spin_unlock_bh(&CE_state->ce_index_lock);
832 
833 	return status;
834 }
835 
836 #ifdef ATH_11AC_TXCOMPACT
837 /* CE engine descriptor reap
838  * Similar to ce_per_engine_service , Only difference is ce_per_engine_service
839  * does receive and reaping of completed descriptor ,
840  * This function only handles reaping of Tx complete descriptor.
841  * The Function is called from threshold reap  poll routine
842  * hif_send_complete_check so should not countain receive functionality
843  * within it .
844  */
845 
846 void ce_per_engine_servicereap(struct hif_softc *scn, unsigned int ce_id)
847 {
848 	void *CE_context;
849 	void *transfer_context;
850 	qdf_dma_addr_t buf;
851 	unsigned int nbytes;
852 	unsigned int id;
853 	unsigned int sw_idx, hw_idx;
854 	uint32_t toeplitz_hash_result;
855 	struct CE_state *CE_state = scn->ce_id_to_state[ce_id];
856 	struct HIF_CE_state *hif_state = HIF_GET_CE_STATE(scn);
857 
858 	if (Q_TARGET_ACCESS_BEGIN(scn) < 0)
859 		return;
860 
861 	hif_record_ce_desc_event(scn, ce_id, HIF_CE_REAP_ENTRY,
862 			NULL, NULL, 0, 0);
863 
864 	/* Since this function is called from both user context and
865 	 * tasklet context the spinlock has to lock the bottom halves.
866 	 * This fix assumes that ATH_11AC_TXCOMPACT flag is always
867 	 * enabled in TX polling mode. If this is not the case, more
868 	 * bottom halve spin lock changes are needed. Due to data path
869 	 * performance concern, after internal discussion we've decided
870 	 * to make minimum change, i.e., only address the issue occurred
871 	 * in this function. The possible negative effect of this minimum
872 	 * change is that, in the future, if some other function will also
873 	 * be opened to let the user context to use, those cases need to be
874 	 * addressed by change spin_lock to spin_lock_bh also.
875 	 */
876 
877 	qdf_spin_lock_bh(&CE_state->ce_index_lock);
878 
879 	if (CE_state->send_cb) {
880 		{
881 			struct ce_ops *ce_services = hif_state->ce_services;
882 			/* Pop completed send buffers and call the
883 			 * registered send callback for each
884 			 */
885 			while (ce_services->ce_completed_send_next_nolock
886 				 (CE_state, &CE_context,
887 				  &transfer_context, &buf,
888 				  &nbytes, &id, &sw_idx, &hw_idx,
889 				  &toeplitz_hash_result) ==
890 				  QDF_STATUS_SUCCESS) {
891 				if (ce_id != CE_HTT_H2T_MSG) {
892 					qdf_spin_unlock_bh(
893 						&CE_state->ce_index_lock);
894 					CE_state->send_cb(
895 						(struct CE_handle *)
896 						CE_state, CE_context,
897 						transfer_context, buf,
898 						nbytes, id, sw_idx, hw_idx,
899 						toeplitz_hash_result);
900 					qdf_spin_lock_bh(
901 						&CE_state->ce_index_lock);
902 				} else {
903 					struct HIF_CE_pipe_info *pipe_info =
904 						(struct HIF_CE_pipe_info *)
905 						CE_context;
906 
907 					qdf_spin_lock_bh(&pipe_info->
908 						 completion_freeq_lock);
909 					pipe_info->num_sends_allowed++;
910 					qdf_spin_unlock_bh(&pipe_info->
911 						   completion_freeq_lock);
912 				}
913 			}
914 		}
915 	}
916 
917 	qdf_spin_unlock_bh(&CE_state->ce_index_lock);
918 
919 	hif_record_ce_desc_event(scn, ce_id, HIF_CE_REAP_EXIT,
920 			NULL, NULL, 0, 0);
921 	Q_TARGET_ACCESS_END(scn);
922 }
923 
924 #endif /*ATH_11AC_TXCOMPACT */
925 
926 /*
927  * ce_engine_service_reg:
928  *
929  * Called from ce_per_engine_service and goes through the regular interrupt
930  * handling that does not involve the WLAN fast path feature.
931  *
932  * Returns void
933  */
934 void ce_engine_service_reg(struct hif_softc *scn, int CE_id)
935 {
936 	struct CE_state *CE_state = scn->ce_id_to_state[CE_id];
937 	uint32_t ctrl_addr = CE_state->ctrl_addr;
938 	struct HIF_CE_state *hif_state = HIF_GET_CE_STATE(scn);
939 	void *CE_context;
940 	void *transfer_context;
941 	qdf_dma_addr_t buf;
942 	unsigned int nbytes;
943 	unsigned int id;
944 	unsigned int flags;
945 	unsigned int more_comp_cnt = 0;
946 	unsigned int more_snd_comp_cnt = 0;
947 	unsigned int sw_idx, hw_idx;
948 	uint32_t toeplitz_hash_result;
949 	uint32_t mode = hif_get_conparam(scn);
950 
951 more_completions:
952 	if (CE_state->recv_cb) {
953 
954 		/* Pop completed recv buffers and call
955 		 * the registered recv callback for each
956 		 */
957 		while (hif_state->ce_services->ce_completed_recv_next_nolock
958 				(CE_state, &CE_context, &transfer_context,
959 				&buf, &nbytes, &id, &flags) ==
960 				QDF_STATUS_SUCCESS) {
961 			qdf_spin_unlock(&CE_state->ce_index_lock);
962 			CE_state->recv_cb((struct CE_handle *)CE_state,
963 					  CE_context, transfer_context, buf,
964 					  nbytes, id, flags);
965 
966 			qdf_spin_lock(&CE_state->ce_index_lock);
967 			/*
968 			 * EV #112693 -
969 			 * [Peregrine][ES1][WB342][Win8x86][Performance]
970 			 * BSoD_0x133 occurred in VHT80 UDP_DL
971 			 * Break out DPC by force if number of loops in
972 			 * hif_pci_ce_recv_data reaches MAX_NUM_OF_RECEIVES
973 			 * to avoid spending too long time in
974 			 * DPC for each interrupt handling. Schedule another
975 			 * DPC to avoid data loss if we had taken
976 			 * force-break action before apply to Windows OS
977 			 * only currently, Linux/MAC os can expand to their
978 			 * platform if necessary
979 			 */
980 
981 			/* Break the receive processes by
982 			 * force if force_break set up
983 			 */
984 			if (qdf_unlikely(CE_state->force_break)) {
985 				qdf_atomic_set(&CE_state->rx_pending, 1);
986 				return;
987 			}
988 		}
989 	}
990 
991 	/*
992 	 * Attention: We may experience potential infinite loop for below
993 	 * While Loop during Sending Stress test.
994 	 * Resolve the same way as Receive Case (Refer to EV #112693)
995 	 */
996 
997 	if (CE_state->send_cb) {
998 		/* Pop completed send buffers and call
999 		 * the registered send callback for each
1000 		 */
1001 
1002 #ifdef ATH_11AC_TXCOMPACT
1003 		while (hif_state->ce_services->ce_completed_send_next_nolock
1004 			 (CE_state, &CE_context,
1005 			 &transfer_context, &buf, &nbytes,
1006 			 &id, &sw_idx, &hw_idx,
1007 			 &toeplitz_hash_result) == QDF_STATUS_SUCCESS) {
1008 
1009 			if (CE_id != CE_HTT_H2T_MSG ||
1010 			    QDF_IS_EPPING_ENABLED(mode)) {
1011 				qdf_spin_unlock(&CE_state->ce_index_lock);
1012 				CE_state->send_cb((struct CE_handle *)CE_state,
1013 						  CE_context, transfer_context,
1014 						  buf, nbytes, id, sw_idx,
1015 						  hw_idx, toeplitz_hash_result);
1016 				qdf_spin_lock(&CE_state->ce_index_lock);
1017 			} else {
1018 				struct HIF_CE_pipe_info *pipe_info =
1019 					(struct HIF_CE_pipe_info *)CE_context;
1020 
1021 				qdf_spin_lock_bh(&pipe_info->
1022 					      completion_freeq_lock);
1023 				pipe_info->num_sends_allowed++;
1024 				qdf_spin_unlock_bh(&pipe_info->
1025 						completion_freeq_lock);
1026 			}
1027 		}
1028 #else                           /*ATH_11AC_TXCOMPACT */
1029 		while (hif_state->ce_services->ce_completed_send_next_nolock
1030 			 (CE_state, &CE_context,
1031 			  &transfer_context, &buf, &nbytes,
1032 			  &id, &sw_idx, &hw_idx,
1033 			  &toeplitz_hash_result) == QDF_STATUS_SUCCESS) {
1034 			qdf_spin_unlock(&CE_state->ce_index_lock);
1035 			CE_state->send_cb((struct CE_handle *)CE_state,
1036 				  CE_context, transfer_context, buf,
1037 				  nbytes, id, sw_idx, hw_idx,
1038 				  toeplitz_hash_result);
1039 			qdf_spin_lock(&CE_state->ce_index_lock);
1040 		}
1041 #endif /*ATH_11AC_TXCOMPACT */
1042 	}
1043 
1044 more_watermarks:
1045 	if (CE_state->misc_cbs) {
1046 		if (CE_state->watermark_cb &&
1047 				hif_state->ce_services->watermark_int(CE_state,
1048 					&flags)) {
1049 			qdf_spin_unlock(&CE_state->ce_index_lock);
1050 			/* Convert HW IS bits to software flags */
1051 			CE_state->watermark_cb((struct CE_handle *)CE_state,
1052 					CE_state->wm_context, flags);
1053 			qdf_spin_lock(&CE_state->ce_index_lock);
1054 		}
1055 	}
1056 
1057 	/*
1058 	 * Clear the misc interrupts (watermark) that were handled above,
1059 	 * and that will be checked again below.
1060 	 * Clear and check for copy-complete interrupts again, just in case
1061 	 * more copy completions happened while the misc interrupts were being
1062 	 * handled.
1063 	 */
1064 	if (!ce_srng_based(scn)) {
1065 		if (TARGET_REGISTER_ACCESS_ALLOWED(scn)) {
1066 			CE_ENGINE_INT_STATUS_CLEAR(scn, ctrl_addr,
1067 					   CE_WATERMARK_MASK |
1068 					   HOST_IS_COPY_COMPLETE_MASK);
1069 		} else {
1070 			qdf_atomic_set(&CE_state->rx_pending, 0);
1071 			hif_err_rl("%s: target access is not allowed",
1072 				   __func__);
1073 			return;
1074 		}
1075 	}
1076 
1077 	/*
1078 	 * Now that per-engine interrupts are cleared, verify that
1079 	 * no recv interrupts arrive while processing send interrupts,
1080 	 * and no recv or send interrupts happened while processing
1081 	 * misc interrupts.Go back and check again.Keep checking until
1082 	 * we find no more events to process.
1083 	 */
1084 	if (CE_state->recv_cb &&
1085 		hif_state->ce_services->ce_recv_entries_done_nolock(scn,
1086 				CE_state)) {
1087 		if (QDF_IS_EPPING_ENABLED(mode) ||
1088 		    more_comp_cnt++ < CE_TXRX_COMP_CHECK_THRESHOLD) {
1089 			goto more_completions;
1090 		} else {
1091 			if (!ce_srng_based(scn)) {
1092 				HIF_ERROR(
1093 					"%s:Potential infinite loop detected during Rx processing nentries_mask:0x%x sw read_idx:0x%x hw read_idx:0x%x",
1094 					__func__,
1095 					CE_state->dest_ring->nentries_mask,
1096 					CE_state->dest_ring->sw_index,
1097 					CE_DEST_RING_READ_IDX_GET(scn,
1098 							  CE_state->ctrl_addr));
1099 			}
1100 		}
1101 	}
1102 
1103 	if (CE_state->send_cb &&
1104 		hif_state->ce_services->ce_send_entries_done_nolock(scn,
1105 				CE_state)) {
1106 		if (QDF_IS_EPPING_ENABLED(mode) ||
1107 		    more_snd_comp_cnt++ < CE_TXRX_COMP_CHECK_THRESHOLD) {
1108 			goto more_completions;
1109 		} else {
1110 			if (!ce_srng_based(scn)) {
1111 				HIF_ERROR(
1112 					"%s:Potential infinite loop detected during send completion nentries_mask:0x%x sw read_idx:0x%x hw read_idx:0x%x",
1113 					__func__,
1114 					CE_state->src_ring->nentries_mask,
1115 					CE_state->src_ring->sw_index,
1116 					CE_SRC_RING_READ_IDX_GET(scn,
1117 							 CE_state->ctrl_addr));
1118 			}
1119 		}
1120 	}
1121 
1122 	if (CE_state->misc_cbs && CE_state->watermark_cb) {
1123 		if (hif_state->ce_services->watermark_int(CE_state, &flags))
1124 			goto more_watermarks;
1125 	}
1126 
1127 	qdf_atomic_set(&CE_state->rx_pending, 0);
1128 }
1129 
1130 /*
1131  * Guts of interrupt handler for per-engine interrupts on a particular CE.
1132  *
1133  * Invokes registered callbacks for recv_complete,
1134  * send_complete, and watermarks.
1135  *
1136  * Returns: number of messages processed
1137  */
1138 int ce_per_engine_service(struct hif_softc *scn, unsigned int CE_id)
1139 {
1140 	struct CE_state *CE_state = scn->ce_id_to_state[CE_id];
1141 
1142 	if (hif_is_nss_wifi_enabled(scn) && (CE_state->htt_rx_data))
1143 		return CE_state->receive_count;
1144 
1145 	if (Q_TARGET_ACCESS_BEGIN(scn) < 0) {
1146 		HIF_ERROR("[premature rc=0]");
1147 		return 0; /* no work done */
1148 	}
1149 
1150 	/* Clear force_break flag and re-initialize receive_count to 0 */
1151 	CE_state->receive_count = 0;
1152 	CE_state->force_break = 0;
1153 	CE_state->ce_service_start_time = sched_clock();
1154 	CE_state->ce_service_yield_time =
1155 		CE_state->ce_service_start_time +
1156 		hif_get_ce_service_max_yield_time(
1157 			(struct hif_opaque_softc *)scn);
1158 
1159 	qdf_spin_lock(&CE_state->ce_index_lock);
1160 
1161 	CE_state->service(scn, CE_id);
1162 
1163 	qdf_spin_unlock(&CE_state->ce_index_lock);
1164 
1165 	if (Q_TARGET_ACCESS_END(scn) < 0)
1166 		HIF_ERROR("<--[premature rc=%d]", CE_state->receive_count);
1167 	return CE_state->receive_count;
1168 }
1169 qdf_export_symbol(ce_per_engine_service);
1170 
1171 /*
1172  * Handler for per-engine interrupts on ALL active CEs.
1173  * This is used in cases where the system is sharing a
1174  * single interrput for all CEs
1175  */
1176 
1177 void ce_per_engine_service_any(int irq, struct hif_softc *scn)
1178 {
1179 	int CE_id;
1180 	uint32_t intr_summary;
1181 
1182 	if (Q_TARGET_ACCESS_BEGIN(scn) < 0)
1183 		return;
1184 
1185 	if (!qdf_atomic_read(&scn->tasklet_from_intr)) {
1186 		for (CE_id = 0; CE_id < scn->ce_count; CE_id++) {
1187 			struct CE_state *CE_state = scn->ce_id_to_state[CE_id];
1188 
1189 			if (qdf_atomic_read(&CE_state->rx_pending)) {
1190 				qdf_atomic_set(&CE_state->rx_pending, 0);
1191 				ce_per_engine_service(scn, CE_id);
1192 			}
1193 		}
1194 
1195 		Q_TARGET_ACCESS_END(scn);
1196 		return;
1197 	}
1198 
1199 	intr_summary = CE_INTERRUPT_SUMMARY(scn);
1200 
1201 	for (CE_id = 0; intr_summary && (CE_id < scn->ce_count); CE_id++) {
1202 		if (intr_summary & (1 << CE_id))
1203 			intr_summary &= ~(1 << CE_id);
1204 		else
1205 			continue;       /* no intr pending on this CE */
1206 
1207 		ce_per_engine_service(scn, CE_id);
1208 	}
1209 
1210 	Q_TARGET_ACCESS_END(scn);
1211 }
1212 
1213 /*Iterate the CE_state list and disable the compl interrupt
1214  * if it has been registered already.
1215  */
1216 void ce_disable_any_copy_compl_intr_nolock(struct hif_softc *scn)
1217 {
1218 	int CE_id;
1219 
1220 	if (Q_TARGET_ACCESS_BEGIN(scn) < 0)
1221 		return;
1222 
1223 	for (CE_id = 0; CE_id < scn->ce_count; CE_id++) {
1224 		struct CE_state *CE_state = scn->ce_id_to_state[CE_id];
1225 		uint32_t ctrl_addr = CE_state->ctrl_addr;
1226 
1227 		/* if the interrupt is currently enabled, disable it */
1228 		if (!CE_state->disable_copy_compl_intr
1229 		    && (CE_state->send_cb || CE_state->recv_cb))
1230 			CE_COPY_COMPLETE_INTR_DISABLE(scn, ctrl_addr);
1231 
1232 		if (CE_state->watermark_cb)
1233 			CE_WATERMARK_INTR_DISABLE(scn, ctrl_addr);
1234 	}
1235 	Q_TARGET_ACCESS_END(scn);
1236 }
1237 
1238 void ce_enable_any_copy_compl_intr_nolock(struct hif_softc *scn)
1239 {
1240 	int CE_id;
1241 
1242 	if (Q_TARGET_ACCESS_BEGIN(scn) < 0)
1243 		return;
1244 
1245 	for (CE_id = 0; CE_id < scn->ce_count; CE_id++) {
1246 		struct CE_state *CE_state = scn->ce_id_to_state[CE_id];
1247 		uint32_t ctrl_addr = CE_state->ctrl_addr;
1248 
1249 		/*
1250 		 * If the CE is supposed to have copy complete interrupts
1251 		 * enabled (i.e. there a callback registered, and the
1252 		 * "disable" flag is not set), then re-enable the interrupt.
1253 		 */
1254 		if (!CE_state->disable_copy_compl_intr
1255 		    && (CE_state->send_cb || CE_state->recv_cb))
1256 			CE_COPY_COMPLETE_INTR_ENABLE(scn, ctrl_addr);
1257 
1258 		if (CE_state->watermark_cb)
1259 			CE_WATERMARK_INTR_ENABLE(scn, ctrl_addr);
1260 	}
1261 	Q_TARGET_ACCESS_END(scn);
1262 }
1263 
1264 /**
1265  * ce_send_cb_register(): register completion handler
1266  * @copyeng: CE_state representing the ce we are adding the behavior to
1267  * @fn_ptr: callback that the ce should use when processing tx completions
1268  * @disable_interrupts: if the interupts should be enabled or not.
1269  *
1270  * Caller should guarantee that no transactions are in progress before
1271  * switching the callback function.
1272  *
1273  * Registers the send context before the fn pointer so that if the cb is valid
1274  * the context should be valid.
1275  *
1276  * Beware that currently this function will enable completion interrupts.
1277  */
1278 void
1279 ce_send_cb_register(struct CE_handle *copyeng,
1280 		    ce_send_cb fn_ptr,
1281 		    void *ce_send_context, int disable_interrupts)
1282 {
1283 	struct CE_state *CE_state = (struct CE_state *)copyeng;
1284 	struct hif_softc *scn;
1285 	struct HIF_CE_state *hif_state;
1286 
1287 	if (!CE_state) {
1288 		HIF_ERROR("%s: Error CE state = NULL", __func__);
1289 		return;
1290 	}
1291 	scn = CE_state->scn;
1292 	hif_state = HIF_GET_CE_STATE(scn);
1293 	if (!hif_state) {
1294 		HIF_ERROR("%s: Error HIF state = NULL", __func__);
1295 		return;
1296 	}
1297 	CE_state->send_context = ce_send_context;
1298 	CE_state->send_cb = fn_ptr;
1299 	hif_state->ce_services->ce_per_engine_handler_adjust(CE_state,
1300 							disable_interrupts);
1301 }
1302 qdf_export_symbol(ce_send_cb_register);
1303 
1304 /**
1305  * ce_recv_cb_register(): register completion handler
1306  * @copyeng: CE_state representing the ce we are adding the behavior to
1307  * @fn_ptr: callback that the ce should use when processing rx completions
1308  * @disable_interrupts: if the interupts should be enabled or not.
1309  *
1310  * Registers the send context before the fn pointer so that if the cb is valid
1311  * the context should be valid.
1312  *
1313  * Caller should guarantee that no transactions are in progress before
1314  * switching the callback function.
1315  */
1316 void
1317 ce_recv_cb_register(struct CE_handle *copyeng,
1318 		    CE_recv_cb fn_ptr,
1319 		    void *CE_recv_context, int disable_interrupts)
1320 {
1321 	struct CE_state *CE_state = (struct CE_state *)copyeng;
1322 	struct hif_softc *scn;
1323 	struct HIF_CE_state *hif_state;
1324 
1325 	if (!CE_state) {
1326 		HIF_ERROR("%s: ERROR CE state = NULL", __func__);
1327 		return;
1328 	}
1329 	scn = CE_state->scn;
1330 	hif_state = HIF_GET_CE_STATE(scn);
1331 	if (!hif_state) {
1332 		HIF_ERROR("%s: Error HIF state = NULL", __func__);
1333 		return;
1334 	}
1335 	CE_state->recv_context = CE_recv_context;
1336 	CE_state->recv_cb = fn_ptr;
1337 	hif_state->ce_services->ce_per_engine_handler_adjust(CE_state,
1338 							disable_interrupts);
1339 }
1340 qdf_export_symbol(ce_recv_cb_register);
1341 
1342 /**
1343  * ce_watermark_cb_register(): register completion handler
1344  * @copyeng: CE_state representing the ce we are adding the behavior to
1345  * @fn_ptr: callback that the ce should use when processing watermark events
1346  *
1347  * Caller should guarantee that no watermark events are being processed before
1348  * switching the callback function.
1349  */
1350 void
1351 ce_watermark_cb_register(struct CE_handle *copyeng,
1352 			 CE_watermark_cb fn_ptr, void *CE_wm_context)
1353 {
1354 	struct CE_state *CE_state = (struct CE_state *)copyeng;
1355 	struct hif_softc *scn = CE_state->scn;
1356 	struct HIF_CE_state *hif_state = HIF_GET_CE_STATE(scn);
1357 
1358 	CE_state->watermark_cb = fn_ptr;
1359 	CE_state->wm_context = CE_wm_context;
1360 	hif_state->ce_services->ce_per_engine_handler_adjust(CE_state,
1361 							0);
1362 	if (fn_ptr)
1363 		CE_state->misc_cbs = 1;
1364 }
1365 
1366 bool ce_get_rx_pending(struct hif_softc *scn)
1367 {
1368 	int CE_id;
1369 
1370 	for (CE_id = 0; CE_id < scn->ce_count; CE_id++) {
1371 		struct CE_state *CE_state = scn->ce_id_to_state[CE_id];
1372 
1373 		if (qdf_atomic_read(&CE_state->rx_pending))
1374 			return true;
1375 	}
1376 
1377 	return false;
1378 }
1379 
1380 /**
1381  * ce_check_rx_pending() - ce_check_rx_pending
1382  * @CE_state: context of the copy engine to check
1383  *
1384  * Return: true if there per_engine_service
1385  *	didn't process all the rx descriptors.
1386  */
1387 bool ce_check_rx_pending(struct CE_state *CE_state)
1388 {
1389 	if (qdf_atomic_read(&CE_state->rx_pending))
1390 		return true;
1391 	else
1392 		return false;
1393 }
1394 qdf_export_symbol(ce_check_rx_pending);
1395 
1396 #ifdef IPA_OFFLOAD
1397 #ifdef QCN7605_SUPPORT
1398 static qdf_dma_addr_t ce_ipa_get_wr_index_addr(struct CE_state *CE_state)
1399 {
1400 	u_int32_t ctrl_addr = CE_state->ctrl_addr;
1401 	struct hif_softc *scn = CE_state->scn;
1402 	qdf_dma_addr_t wr_index_addr;
1403 
1404 	wr_index_addr = shadow_sr_wr_ind_addr(scn, ctrl_addr);
1405 	return wr_index_addr;
1406 }
1407 #else
1408 static qdf_dma_addr_t ce_ipa_get_wr_index_addr(struct CE_state *CE_state)
1409 {
1410 	struct hif_softc *scn = CE_state->scn;
1411 	qdf_dma_addr_t wr_index_addr;
1412 
1413 	wr_index_addr = CE_BASE_ADDRESS(CE_state->id) +
1414 			SR_WR_INDEX_ADDRESS;
1415 	return wr_index_addr;
1416 }
1417 #endif
1418 
1419 /**
1420  * ce_ipa_get_resource() - get uc resource on copyengine
1421  * @ce: copyengine context
1422  * @ce_sr: copyengine source ring resource info
1423  * @ce_sr_ring_size: copyengine source ring size
1424  * @ce_reg_paddr: copyengine register physical address
1425  *
1426  * Copy engine should release resource to micro controller
1427  * Micro controller needs
1428  *  - Copy engine source descriptor base address
1429  *  - Copy engine source descriptor size
1430  *  - PCI BAR address to access copy engine regiser
1431  *
1432  * Return: None
1433  */
1434 void ce_ipa_get_resource(struct CE_handle *ce,
1435 			 qdf_shared_mem_t **ce_sr,
1436 			 uint32_t *ce_sr_ring_size,
1437 			 qdf_dma_addr_t *ce_reg_paddr)
1438 {
1439 	struct CE_state *CE_state = (struct CE_state *)ce;
1440 	uint32_t ring_loop;
1441 	struct CE_src_desc *ce_desc;
1442 	qdf_dma_addr_t phy_mem_base;
1443 	struct hif_softc *scn = CE_state->scn;
1444 
1445 	if (CE_UNUSED == CE_state->state) {
1446 		*qdf_mem_get_dma_addr_ptr(scn->qdf_dev,
1447 			&CE_state->scn->ipa_ce_ring->mem_info) = 0;
1448 		*ce_sr_ring_size = 0;
1449 		return;
1450 	}
1451 
1452 	/* Update default value for descriptor */
1453 	for (ring_loop = 0; ring_loop < CE_state->src_ring->nentries;
1454 	     ring_loop++) {
1455 		ce_desc = (struct CE_src_desc *)
1456 			  ((char *)CE_state->src_ring->base_addr_owner_space +
1457 			   ring_loop * (sizeof(struct CE_src_desc)));
1458 		CE_IPA_RING_INIT(ce_desc);
1459 	}
1460 
1461 	/* Get BAR address */
1462 	hif_read_phy_mem_base(CE_state->scn, &phy_mem_base);
1463 
1464 	*ce_sr = CE_state->scn->ipa_ce_ring;
1465 	*ce_sr_ring_size = (uint32_t)(CE_state->src_ring->nentries *
1466 		sizeof(struct CE_src_desc));
1467 	*ce_reg_paddr = phy_mem_base + ce_ipa_get_wr_index_addr(CE_state);
1468 
1469 }
1470 
1471 #endif /* IPA_OFFLOAD */
1472 
1473 #ifdef HIF_CE_DEBUG_DATA_BUF
1474 /**
1475  * hif_dump_desc_data_buf() - record ce descriptor events
1476  * @buf: buffer to copy to
1477  * @pos: Current position till which the buf is filled
1478  * @data: Data to be copied
1479  * @data_len: Length of the data to be copied
1480  */
1481 static uint32_t hif_dump_desc_data_buf(uint8_t *buf, ssize_t pos,
1482 					uint8_t *data, uint32_t data_len)
1483 {
1484 	pos += snprintf(buf + pos, PAGE_SIZE - pos, "Data:(Max%dBytes)\n",
1485 			CE_DEBUG_MAX_DATA_BUF_SIZE);
1486 
1487 	if ((data_len > 0) && data) {
1488 		if (data_len < 16) {
1489 			hex_dump_to_buffer(data,
1490 						CE_DEBUG_DATA_PER_ROW,
1491 						16, 1, buf + pos,
1492 						(ssize_t)PAGE_SIZE - pos,
1493 						false);
1494 			pos += CE_DEBUG_PRINT_BUF_SIZE(data_len);
1495 			pos += snprintf(buf + pos, PAGE_SIZE - pos, "\n");
1496 		} else {
1497 			uint32_t rows = (data_len / 16) + 1;
1498 			uint32_t row = 0;
1499 
1500 			for (row = 0; row < rows; row++) {
1501 				hex_dump_to_buffer(data + (row * 16),
1502 							CE_DEBUG_DATA_PER_ROW,
1503 							16, 1, buf + pos,
1504 							(ssize_t)PAGE_SIZE
1505 							- pos, false);
1506 				pos +=
1507 				CE_DEBUG_PRINT_BUF_SIZE(CE_DEBUG_DATA_PER_ROW);
1508 				pos += snprintf(buf + pos, PAGE_SIZE - pos,
1509 						"\n");
1510 			}
1511 		}
1512 	}
1513 
1514 	return pos;
1515 }
1516 #endif
1517 
1518 /*
1519  * Note: For MCL, #if defined (HIF_CONFIG_SLUB_DEBUG_ON) needs to be checked
1520  * for defined here
1521  */
1522 #if defined(HIF_CONFIG_SLUB_DEBUG_ON) || defined(HIF_CE_DEBUG_DATA_BUF)
1523 static const char *ce_event_type_to_str(enum hif_ce_event_type type)
1524 {
1525 	switch (type) {
1526 	case HIF_RX_DESC_POST:
1527 		return "HIF_RX_DESC_POST";
1528 	case HIF_RX_DESC_COMPLETION:
1529 		return "HIF_RX_DESC_COMPLETION";
1530 	case HIF_TX_GATHER_DESC_POST:
1531 		return "HIF_TX_GATHER_DESC_POST";
1532 	case HIF_TX_DESC_POST:
1533 		return "HIF_TX_DESC_POST";
1534 	case HIF_TX_DESC_SOFTWARE_POST:
1535 		return "HIF_TX_DESC_SOFTWARE_POST";
1536 	case HIF_TX_DESC_COMPLETION:
1537 		return "HIF_TX_DESC_COMPLETION";
1538 	case FAST_RX_WRITE_INDEX_UPDATE:
1539 		return "FAST_RX_WRITE_INDEX_UPDATE";
1540 	case FAST_RX_SOFTWARE_INDEX_UPDATE:
1541 		return "FAST_RX_SOFTWARE_INDEX_UPDATE";
1542 	case FAST_TX_WRITE_INDEX_UPDATE:
1543 		return "FAST_TX_WRITE_INDEX_UPDATE";
1544 	case FAST_TX_WRITE_INDEX_SOFTWARE_UPDATE:
1545 		return "FAST_TX_WRITE_INDEX_SOFTWARE_UPDATE";
1546 	case FAST_TX_SOFTWARE_INDEX_UPDATE:
1547 		return "FAST_TX_SOFTWARE_INDEX_UPDATE";
1548 	case RESUME_WRITE_INDEX_UPDATE:
1549 		return "RESUME_WRITE_INDEX_UPDATE";
1550 	case HIF_IRQ_EVENT:
1551 		return "HIF_IRQ_EVENT";
1552 	case HIF_CE_TASKLET_ENTRY:
1553 		return "HIF_CE_TASKLET_ENTRY";
1554 	case HIF_CE_TASKLET_RESCHEDULE:
1555 		return "HIF_CE_TASKLET_RESCHEDULE";
1556 	case HIF_CE_TASKLET_EXIT:
1557 		return "HIF_CE_TASKLET_EXIT";
1558 	case HIF_CE_REAP_ENTRY:
1559 		return "HIF_CE_REAP_ENTRY";
1560 	case HIF_CE_REAP_EXIT:
1561 		return "HIF_CE_REAP_EXIT";
1562 	case NAPI_SCHEDULE:
1563 		return "NAPI_SCHEDULE";
1564 	case NAPI_POLL_ENTER:
1565 		return "NAPI_POLL_ENTER";
1566 	case NAPI_COMPLETE:
1567 		return "NAPI_COMPLETE";
1568 	case NAPI_POLL_EXIT:
1569 		return "NAPI_POLL_EXIT";
1570 	case HIF_RX_NBUF_ALLOC_FAILURE:
1571 		return "HIF_RX_NBUF_ALLOC_FAILURE";
1572 	case HIF_RX_NBUF_MAP_FAILURE:
1573 		return "HIF_RX_NBUF_MAP_FAILURE";
1574 	case HIF_RX_NBUF_ENQUEUE_FAILURE:
1575 		return "HIF_RX_NBUF_ENQUEUE_FAILURE";
1576 	default:
1577 		return "invalid";
1578 	}
1579 }
1580 
1581 /**
1582  * hif_dump_desc_event() - record ce descriptor events
1583  * @buf: Buffer to which to be copied
1584  * @ce_id: which ce is the event occurring on
1585  * @index: index that the descriptor was/will be at.
1586  */
1587 ssize_t hif_dump_desc_event(struct hif_softc *scn, char *buf)
1588 {
1589 	struct hif_ce_desc_event *event;
1590 	uint64_t secs, usecs;
1591 	ssize_t len = 0;
1592 	struct ce_desc_hist *ce_hist = NULL;
1593 	struct hif_ce_desc_event *hist_ev = NULL;
1594 
1595 	if (!scn)
1596 		return -EINVAL;
1597 
1598 	ce_hist = &scn->hif_ce_desc_hist;
1599 
1600 	if (ce_hist->hist_id >= CE_COUNT_MAX ||
1601 	    ce_hist->hist_index >= HIF_CE_HISTORY_MAX) {
1602 		qdf_print("Invalid values");
1603 		return -EINVAL;
1604 	}
1605 
1606 	hist_ev =
1607 		(struct hif_ce_desc_event *)ce_hist->hist_ev[ce_hist->hist_id];
1608 
1609 	if (!hist_ev) {
1610 		qdf_print("Low Memory");
1611 		return -EINVAL;
1612 	}
1613 
1614 	event = &hist_ev[ce_hist->hist_index];
1615 
1616 	qdf_log_timestamp_to_secs(event->time, &secs, &usecs);
1617 
1618 	len += snprintf(buf, PAGE_SIZE - len,
1619 			"\nTime:%lld.%06lld, CE:%d, EventType: %s, EventIndex: %d\nDataAddr=%pK",
1620 			secs, usecs, ce_hist->hist_id,
1621 			ce_event_type_to_str(event->type),
1622 			event->index, event->memory);
1623 #ifdef HIF_CE_DEBUG_DATA_BUF
1624 	len += snprintf(buf + len, PAGE_SIZE - len, ", Data len=%zu",
1625 			event->actual_data_len);
1626 #endif
1627 
1628 	len += snprintf(buf + len, PAGE_SIZE - len, "\nCE descriptor: ");
1629 
1630 	hex_dump_to_buffer(&event->descriptor, sizeof(union ce_desc),
1631 				16, 1, buf + len,
1632 				(ssize_t)PAGE_SIZE - len, false);
1633 	len += CE_DEBUG_PRINT_BUF_SIZE(sizeof(union ce_desc));
1634 	len += snprintf(buf + len, PAGE_SIZE - len, "\n");
1635 
1636 #ifdef HIF_CE_DEBUG_DATA_BUF
1637 	if (ce_hist->data_enable[ce_hist->hist_id])
1638 		len = hif_dump_desc_data_buf(buf, len, event->data,
1639 						(event->actual_data_len <
1640 						 CE_DEBUG_MAX_DATA_BUF_SIZE) ?
1641 						event->actual_data_len :
1642 						CE_DEBUG_MAX_DATA_BUF_SIZE);
1643 #endif /*HIF_CE_DEBUG_DATA_BUF*/
1644 
1645 	len += snprintf(buf + len, PAGE_SIZE - len, "END\n");
1646 
1647 	return len;
1648 }
1649 
1650 /*
1651  * hif_store_desc_trace_buf_index() -
1652  * API to get the CE id and CE debug storage buffer index
1653  *
1654  * @dev: network device
1655  * @attr: sysfs attribute
1656  * @buf: data got from the user
1657  *
1658  * Return total length
1659  */
1660 ssize_t hif_input_desc_trace_buf_index(struct hif_softc *scn,
1661 					const char *buf, size_t size)
1662 {
1663 	struct ce_desc_hist *ce_hist = NULL;
1664 
1665 	if (!scn)
1666 		return -EINVAL;
1667 
1668 	ce_hist = &scn->hif_ce_desc_hist;
1669 
1670 	if (!size) {
1671 		qdf_nofl_err("%s: Invalid input buffer.", __func__);
1672 		return -EINVAL;
1673 	}
1674 
1675 	if (sscanf(buf, "%u %u", (unsigned int *)&ce_hist->hist_id,
1676 		   (unsigned int *)&ce_hist->hist_index) != 2) {
1677 		qdf_nofl_err("%s: Invalid input value.", __func__);
1678 		return -EINVAL;
1679 	}
1680 	if ((ce_hist->hist_id >= CE_COUNT_MAX) ||
1681 	   (ce_hist->hist_index >= HIF_CE_HISTORY_MAX)) {
1682 		qdf_print("Invalid values");
1683 		return -EINVAL;
1684 	}
1685 
1686 	return size;
1687 }
1688 
1689 #endif /*defined(HIF_CONFIG_SLUB_DEBUG_ON) || defined(HIF_CE_DEBUG_DATA_BUF) */
1690 
1691 #ifdef HIF_CE_DEBUG_DATA_BUF
1692 /*
1693  * hif_ce_en_desc_hist() -
1694  * API to enable recording the CE desc history
1695  *
1696  * @dev: network device
1697  * @attr: sysfs attribute
1698  * @buf: buffer to copy the data.
1699  *
1700  * Starts recording the ce desc history
1701  *
1702  * Return total length copied
1703  */
1704 ssize_t hif_ce_en_desc_hist(struct hif_softc *scn, const char *buf, size_t size)
1705 {
1706 	struct ce_desc_hist *ce_hist = NULL;
1707 	uint32_t cfg = 0;
1708 	uint32_t ce_id = 0;
1709 
1710 	if (!scn)
1711 		return -EINVAL;
1712 
1713 	ce_hist = &scn->hif_ce_desc_hist;
1714 
1715 	if (!size) {
1716 		qdf_nofl_err("%s: Invalid input buffer.", __func__);
1717 		return -EINVAL;
1718 	}
1719 
1720 	if (sscanf(buf, "%u %u", (unsigned int *)&ce_id,
1721 		   (unsigned int *)&cfg) != 2) {
1722 		qdf_nofl_err("%s: Invalid input: Enter CE Id<sp><1/0>.",
1723 			     __func__);
1724 		return -EINVAL;
1725 	}
1726 	if (ce_id >= CE_COUNT_MAX) {
1727 		qdf_print("Invalid value CE Id");
1728 		return -EINVAL;
1729 	}
1730 
1731 	if ((cfg > 1 || cfg < 0)) {
1732 		qdf_print("Invalid values: enter 0 or 1");
1733 		return -EINVAL;
1734 	}
1735 
1736 	if (!ce_hist->hist_ev[ce_id])
1737 		return -EINVAL;
1738 
1739 	qdf_mutex_acquire(&ce_hist->ce_dbg_datamem_lock[ce_id]);
1740 	if (cfg == 1) {
1741 		if (ce_hist->data_enable[ce_id] == 1) {
1742 			qdf_debug("Already Enabled");
1743 		} else {
1744 			if (alloc_mem_ce_debug_hist_data(scn, ce_id)
1745 							== QDF_STATUS_E_NOMEM){
1746 				ce_hist->data_enable[ce_id] = 0;
1747 				qdf_err("%s:Memory Alloc failed", __func__);
1748 			} else
1749 				ce_hist->data_enable[ce_id] = 1;
1750 		}
1751 	} else if (cfg == 0) {
1752 		if (ce_hist->data_enable[ce_id] == 0) {
1753 			qdf_debug("Already Disabled");
1754 		} else {
1755 			ce_hist->data_enable[ce_id] = 0;
1756 				free_mem_ce_debug_hist_data(scn, ce_id);
1757 		}
1758 	}
1759 	qdf_mutex_release(&ce_hist->ce_dbg_datamem_lock[ce_id]);
1760 
1761 	return size;
1762 }
1763 
1764 /*
1765  * hif_disp_ce_enable_desc_data_hist() -
1766  * API to display value of data_enable
1767  *
1768  * @dev: network device
1769  * @attr: sysfs attribute
1770  * @buf: buffer to copy the data.
1771  *
1772  * Return total length copied
1773  */
1774 ssize_t hif_disp_ce_enable_desc_data_hist(struct hif_softc *scn, char *buf)
1775 {
1776 	ssize_t len = 0;
1777 	uint32_t ce_id = 0;
1778 	struct ce_desc_hist *ce_hist = NULL;
1779 
1780 	if (!scn)
1781 		return -EINVAL;
1782 
1783 	ce_hist = &scn->hif_ce_desc_hist;
1784 
1785 	for (ce_id = 0; ce_id < CE_COUNT_MAX; ce_id++) {
1786 		len += snprintf(buf + len, PAGE_SIZE - len, " CE%d: %d\n",
1787 				ce_id, ce_hist->data_enable[ce_id]);
1788 	}
1789 
1790 	return len;
1791 }
1792 #endif /* HIF_CE_DEBUG_DATA_BUF */
1793 
1794 #ifdef OL_ATH_SMART_LOGGING
1795 #define GUARD_SPACE 10
1796 #define LOG_ID_SZ 4
1797 /*
1798  * hif_log_src_ce_dump() - Copy all the CE SRC ring to buf
1799  * @src_ring: SRC ring state
1800  * @buf_cur: Current pointer in ring buffer
1801  * @buf_init:Start of the ring buffer
1802  * @buf_sz: Size of the ring buffer
1803  * @skb_sz: Max size of the SKB buffer to be copied
1804  *
1805  * Dumps all the CE SRC ring descriptors and buffers pointed by them in to
1806  * the given buf, skb_sz is the max buffer size to be copied
1807  *
1808  * Return: Current pointer in ring buffer
1809  */
1810 static uint8_t *hif_log_src_ce_dump(struct CE_ring_state *src_ring,
1811 				    uint8_t *buf_cur, uint8_t *buf_init,
1812 				    uint32_t buf_sz, uint32_t skb_sz)
1813 {
1814 	struct CE_src_desc *src_ring_base;
1815 	uint32_t len, entry;
1816 	struct CE_src_desc  *src_desc;
1817 	qdf_nbuf_t nbuf;
1818 	uint32_t available_buf;
1819 
1820 	src_ring_base = (struct CE_src_desc *)src_ring->base_addr_owner_space;
1821 	len = sizeof(struct CE_ring_state);
1822 	available_buf = buf_sz - (buf_cur - buf_init);
1823 	if (available_buf < (len + GUARD_SPACE)) {
1824 		buf_cur = buf_init;
1825 	}
1826 
1827 	qdf_mem_copy(buf_cur, src_ring, sizeof(struct CE_ring_state));
1828 	buf_cur += sizeof(struct CE_ring_state);
1829 
1830 	for (entry = 0; entry < src_ring->nentries; entry++) {
1831 		src_desc = CE_SRC_RING_TO_DESC(src_ring_base, entry);
1832 		nbuf = src_ring->per_transfer_context[entry];
1833 		if (nbuf) {
1834 			uint32_t skb_len  = qdf_nbuf_len(nbuf);
1835 			uint32_t skb_cp_len = qdf_min(skb_len, skb_sz);
1836 
1837 			len = sizeof(struct CE_src_desc) + skb_cp_len
1838 				+ LOG_ID_SZ + sizeof(skb_cp_len);
1839 			available_buf = buf_sz - (buf_cur - buf_init);
1840 			if (available_buf < (len + GUARD_SPACE)) {
1841 				buf_cur = buf_init;
1842 			}
1843 			qdf_mem_copy(buf_cur, src_desc,
1844 				     sizeof(struct CE_src_desc));
1845 			buf_cur += sizeof(struct CE_src_desc);
1846 
1847 			available_buf = buf_sz - (buf_cur - buf_init);
1848 			buf_cur += snprintf(buf_cur, available_buf, "SKB%d",
1849 						skb_cp_len);
1850 
1851 			if (skb_cp_len) {
1852 				qdf_mem_copy(buf_cur, qdf_nbuf_data(nbuf),
1853 					     skb_cp_len);
1854 				buf_cur += skb_cp_len;
1855 			}
1856 		} else {
1857 			len = sizeof(struct CE_src_desc) + LOG_ID_SZ;
1858 			available_buf = buf_sz - (buf_cur - buf_init);
1859 			if (available_buf < (len + GUARD_SPACE)) {
1860 				buf_cur = buf_init;
1861 			}
1862 			qdf_mem_copy(buf_cur, src_desc,
1863 				     sizeof(struct CE_src_desc));
1864 			buf_cur += sizeof(struct CE_src_desc);
1865 			available_buf = buf_sz - (buf_cur - buf_init);
1866 			buf_cur += snprintf(buf_cur, available_buf, "NUL");
1867 		}
1868 	}
1869 
1870 	return buf_cur;
1871 }
1872 
1873 /*
1874  * hif_log_dest_ce_dump() - Copy all the CE DEST ring to buf
1875  * @dest_ring: SRC ring state
1876  * @buf_cur: Current pointer in ring buffer
1877  * @buf_init:Start of the ring buffer
1878  * @buf_sz: Size of the ring buffer
1879  * @skb_sz: Max size of the SKB buffer to be copied
1880  *
1881  * Dumps all the CE SRC ring descriptors and buffers pointed by them in to
1882  * the given buf, skb_sz is the max buffer size to be copied
1883  *
1884  * Return: Current pointer in ring buffer
1885  */
1886 static uint8_t *hif_log_dest_ce_dump(struct CE_ring_state *dest_ring,
1887 				     uint8_t *buf_cur, uint8_t *buf_init,
1888 				     uint32_t buf_sz, uint32_t skb_sz)
1889 {
1890 	struct CE_dest_desc *dest_ring_base;
1891 	uint32_t len, entry;
1892 	struct CE_dest_desc  *dest_desc;
1893 	qdf_nbuf_t nbuf;
1894 	uint32_t available_buf;
1895 
1896 	dest_ring_base =
1897 		(struct CE_dest_desc *)dest_ring->base_addr_owner_space;
1898 
1899 	len = sizeof(struct CE_ring_state);
1900 	available_buf = buf_sz - (buf_cur - buf_init);
1901 	if (available_buf < (len + GUARD_SPACE)) {
1902 		buf_cur = buf_init;
1903 	}
1904 
1905 	qdf_mem_copy(buf_cur, dest_ring, sizeof(struct CE_ring_state));
1906 	buf_cur += sizeof(struct CE_ring_state);
1907 
1908 	for (entry = 0; entry < dest_ring->nentries; entry++) {
1909 		dest_desc = CE_DEST_RING_TO_DESC(dest_ring_base, entry);
1910 
1911 		nbuf = dest_ring->per_transfer_context[entry];
1912 		if (nbuf) {
1913 			uint32_t skb_len  = qdf_nbuf_len(nbuf);
1914 			uint32_t skb_cp_len = qdf_min(skb_len, skb_sz);
1915 
1916 			len = sizeof(struct CE_dest_desc) + skb_cp_len
1917 				+ LOG_ID_SZ + sizeof(skb_cp_len);
1918 
1919 			available_buf = buf_sz - (buf_cur - buf_init);
1920 			if (available_buf < (len + GUARD_SPACE)) {
1921 				buf_cur = buf_init;
1922 			}
1923 
1924 			qdf_mem_copy(buf_cur, dest_desc,
1925 				     sizeof(struct CE_dest_desc));
1926 			buf_cur += sizeof(struct CE_dest_desc);
1927 			available_buf = buf_sz - (buf_cur - buf_init);
1928 			buf_cur += snprintf(buf_cur, available_buf, "SKB%d",
1929 						skb_cp_len);
1930 			if (skb_cp_len) {
1931 				qdf_mem_copy(buf_cur, qdf_nbuf_data(nbuf),
1932 					     skb_cp_len);
1933 				buf_cur += skb_cp_len;
1934 			}
1935 		} else {
1936 			len = sizeof(struct CE_dest_desc) + LOG_ID_SZ;
1937 			available_buf = buf_sz - (buf_cur - buf_init);
1938 			if (available_buf < (len + GUARD_SPACE)) {
1939 				buf_cur = buf_init;
1940 			}
1941 			qdf_mem_copy(buf_cur, dest_desc,
1942 				     sizeof(struct CE_dest_desc));
1943 			buf_cur += sizeof(struct CE_dest_desc);
1944 			available_buf = buf_sz - (buf_cur - buf_init);
1945 			buf_cur += snprintf(buf_cur, available_buf, "NUL");
1946 		}
1947 	}
1948 	return buf_cur;
1949 }
1950 
1951 /**
1952  * hif_log_ce_dump() - Copy all the CE DEST ring to buf
1953  * Calls the respective function to dump all the CE SRC/DEST ring descriptors
1954  * and buffers pointed by them in to the given buf
1955  */
1956 uint8_t *hif_log_dump_ce(struct hif_softc *scn, uint8_t *buf_cur,
1957 			 uint8_t *buf_init, uint32_t buf_sz,
1958 			 uint32_t ce, uint32_t skb_sz)
1959 {
1960 	struct CE_state *ce_state;
1961 	struct CE_ring_state *src_ring;
1962 	struct CE_ring_state *dest_ring;
1963 
1964 	ce_state = scn->ce_id_to_state[ce];
1965 	src_ring = ce_state->src_ring;
1966 	dest_ring = ce_state->dest_ring;
1967 
1968 	if (src_ring) {
1969 		buf_cur = hif_log_src_ce_dump(src_ring, buf_cur,
1970 					      buf_init, buf_sz, skb_sz);
1971 	} else if (dest_ring) {
1972 		buf_cur = hif_log_dest_ce_dump(dest_ring, buf_cur,
1973 					       buf_init, buf_sz, skb_sz);
1974 	}
1975 
1976 	return buf_cur;
1977 }
1978 
1979 qdf_export_symbol(hif_log_dump_ce);
1980 #endif /* OL_ATH_SMART_LOGGING */
1981 
1982