blob: 9bb2759499fc08e08fd3956a8b3ef60448d71f66 [file] [log] [blame]
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001/*
Komal Seelam644263d2016-02-22 20:45:49 +05302 * Copyright (c) 2013-2016 The Linux Foundation. All rights reserved.
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -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
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -080028#include "hif.h"
29#include "hif_io32.h"
30#include "ce_api.h"
31#include "ce_main.h"
32#include "ce_internal.h"
33#include "ce_reg.h"
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +053034#include "qdf_lock.h"
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -080035#include "regtable.h"
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -080036#include "hif_main.h"
37#include "hif_debug.h"
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -080038
39#ifdef IPA_OFFLOAD
40#ifdef QCA_WIFI_3_0
41#define CE_IPA_RING_INIT(ce_desc) \
42 do { \
43 ce_desc->gather = 0; \
44 ce_desc->enable_11h = 0; \
45 ce_desc->meta_data_low = 0; \
46 ce_desc->packet_result_offset = 64; \
47 ce_desc->toeplitz_hash_enable = 0; \
48 ce_desc->addr_y_search_disable = 0; \
49 ce_desc->addr_x_search_disable = 0; \
50 ce_desc->misc_int_disable = 0; \
51 ce_desc->target_int_disable = 0; \
52 ce_desc->host_int_disable = 0; \
53 ce_desc->dest_byte_swap = 0; \
54 ce_desc->byte_swap = 0; \
55 ce_desc->type = 2; \
56 ce_desc->tx_classify = 1; \
57 ce_desc->buffer_addr_hi = 0; \
58 ce_desc->meta_data = 0; \
59 ce_desc->nbytes = 128; \
60 } while (0)
61#else
62#define CE_IPA_RING_INIT(ce_desc) \
63 do { \
64 ce_desc->byte_swap = 0; \
65 ce_desc->nbytes = 60; \
66 ce_desc->gather = 0; \
67 } while (0)
68#endif /* QCA_WIFI_3_0 */
69#endif /* IPA_OFFLOAD */
70
71static int war1_allow_sleep;
72/* io32 write workaround */
73static int hif_ce_war1;
74
Houston Hoffman68e837e2015-12-04 12:57:24 -080075#ifdef CONFIG_SLUB_DEBUG_ON
76
77/**
78 * struct hif_ce_event - structure for detailing a ce event
79 * @type: what the event was
80 * @time: when it happened
81 * @descriptor: descriptor enqueued or dequeued
82 * @memory: virtual address that was used
83 * @index: location of the descriptor in the ce ring;
84 */
85struct hif_ce_desc_event {
86 uint16_t index;
87 enum hif_ce_event_type type;
88 uint64_t time;
89 union ce_desc descriptor;
90 void *memory;
91};
92
93/* max history to record per copy engine */
94#define HIF_CE_HISTORY_MAX 512
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +053095qdf_atomic_t hif_ce_desc_history_index[CE_COUNT_MAX];
Houston Hoffman68e837e2015-12-04 12:57:24 -080096struct hif_ce_desc_event hif_ce_desc_history[CE_COUNT_MAX][HIF_CE_HISTORY_MAX];
97
Houston Hoffman4275ba22015-12-06 21:02:11 -080098
Houston Hoffman68e837e2015-12-04 12:57:24 -080099/**
100 * get_next_record_index() - get the next record index
101 * @table_index: atomic index variable to increment
102 * @array_size: array size of the circular buffer
103 *
104 * Increment the atomic index and reserve the value.
105 * Takes care of buffer wrap.
106 * Guaranteed to be thread safe as long as fewer than array_size contexts
107 * try to access the array. If there are more than array_size contexts
108 * trying to access the array, full locking of the recording process would
109 * be needed to have sane logging.
110 */
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +0530111static int get_next_record_index(qdf_atomic_t *table_index, int array_size)
Houston Hoffman68e837e2015-12-04 12:57:24 -0800112{
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +0530113 int record_index = qdf_atomic_inc_return(table_index);
Houston Hoffman68e837e2015-12-04 12:57:24 -0800114 if (record_index == array_size)
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +0530115 qdf_atomic_sub(array_size, table_index);
Houston Hoffman68e837e2015-12-04 12:57:24 -0800116
117 while (record_index >= array_size)
118 record_index -= array_size;
119 return record_index;
120}
121
122/**
123 * hif_record_ce_desc_event() - record ce descriptor events
Komal Seelambd7c51d2016-02-24 10:27:30 +0530124 * @scn: hif_softc
Houston Hoffman68e837e2015-12-04 12:57:24 -0800125 * @ce_id: which ce is the event occuring on
126 * @type: what happened
127 * @descriptor: pointer to the descriptor posted/completed
128 * @memory: virtual address of buffer related to the descriptor
129 * @index: index that the descriptor was/will be at.
130 */
Komal Seelambd7c51d2016-02-24 10:27:30 +0530131void hif_record_ce_desc_event(struct hif_softc *scn, int ce_id,
132 enum hif_ce_event_type type,
133 union ce_desc *descriptor,
134 void *memory, int index)
Houston Hoffman68e837e2015-12-04 12:57:24 -0800135{
136 int record_index = get_next_record_index(
137 &hif_ce_desc_history_index[ce_id], HIF_CE_HISTORY_MAX);
138
139 struct hif_ce_desc_event *event =
140 &hif_ce_desc_history[ce_id][record_index];
141 event->type = type;
Komal Seelam75080122016-03-02 15:18:25 +0530142 event->time = qdf_get_monotonic_boottime();
Komal Seelambd7c51d2016-02-24 10:27:30 +0530143
Houston Hoffman4275ba22015-12-06 21:02:11 -0800144 if (descriptor != NULL)
145 event->descriptor = *descriptor;
146 else
147 memset(&event->descriptor, 0, sizeof(union ce_desc));
Houston Hoffman68e837e2015-12-04 12:57:24 -0800148 event->memory = memory;
149 event->index = index;
150}
151
152/**
153 * ce_init_ce_desc_event_log() - initialize the ce event log
154 * @ce_id: copy engine id for which we are initializing the log
155 * @size: size of array to dedicate
156 *
157 * Currently the passed size is ignored in favor of a precompiled value.
158 */
159void ce_init_ce_desc_event_log(int ce_id, int size)
160{
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +0530161 qdf_atomic_init(&hif_ce_desc_history_index[ce_id]);
Houston Hoffman68e837e2015-12-04 12:57:24 -0800162}
163#else
Komal Seelambd7c51d2016-02-24 10:27:30 +0530164void hif_record_ce_desc_event(struct hif_softc *scn,
Houston Hoffman68e837e2015-12-04 12:57:24 -0800165 int ce_id, enum hif_ce_event_type type,
166 union ce_desc *descriptor, void *memory,
167 int index)
168{
169}
170
Houston Hoffman5cc292b2015-12-22 11:33:14 -0800171inline void ce_init_ce_desc_event_log(int ce_id, int size)
Houston Hoffman68e837e2015-12-04 12:57:24 -0800172{
173}
174#endif
175
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800176/*
177 * Support for Copy Engine hardware, which is mainly used for
178 * communication between Host and Target over a PCIe interconnect.
179 */
180
181/*
182 * A single CopyEngine (CE) comprises two "rings":
183 * a source ring
184 * a destination ring
185 *
186 * Each ring consists of a number of descriptors which specify
187 * an address, length, and meta-data.
188 *
189 * Typically, one side of the PCIe interconnect (Host or Target)
190 * controls one ring and the other side controls the other ring.
191 * The source side chooses when to initiate a transfer and it
192 * chooses what to send (buffer address, length). The destination
193 * side keeps a supply of "anonymous receive buffers" available and
194 * it handles incoming data as it arrives (when the destination
195 * recieves an interrupt).
196 *
197 * The sender may send a simple buffer (address/length) or it may
198 * send a small list of buffers. When a small list is sent, hardware
199 * "gathers" these and they end up in a single destination buffer
200 * with a single interrupt.
201 *
202 * There are several "contexts" managed by this layer -- more, it
203 * may seem -- than should be needed. These are provided mainly for
204 * maximum flexibility and especially to facilitate a simpler HIF
205 * implementation. There are per-CopyEngine recv, send, and watermark
206 * contexts. These are supplied by the caller when a recv, send,
207 * or watermark handler is established and they are echoed back to
208 * the caller when the respective callbacks are invoked. There is
209 * also a per-transfer context supplied by the caller when a buffer
210 * (or sendlist) is sent and when a buffer is enqueued for recv.
211 * These per-transfer contexts are echoed back to the caller when
212 * the buffer is sent/received.
213 * Target TX harsh result toeplitz_hash_result
214 */
215
216/*
217 * Guts of ce_send, used by both ce_send and ce_sendlist_send.
218 * The caller takes responsibility for any needed locking.
219 */
220int
221ce_completed_send_next_nolock(struct CE_state *CE_state,
222 void **per_CE_contextp,
223 void **per_transfer_contextp,
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +0530224 qdf_dma_addr_t *bufferp,
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800225 unsigned int *nbytesp,
226 unsigned int *transfer_idp,
227 unsigned int *sw_idx, unsigned int *hw_idx,
228 uint32_t *toeplitz_hash_result);
229
Komal Seelam644263d2016-02-22 20:45:49 +0530230void war_ce_src_ring_write_idx_set(struct hif_softc *scn,
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800231 u32 ctrl_addr, unsigned int write_index)
232{
233 if (hif_ce_war1) {
234 void __iomem *indicator_addr;
235
236 indicator_addr = scn->mem + ctrl_addr + DST_WATERMARK_ADDRESS;
237
238 if (!war1_allow_sleep
239 && ctrl_addr == CE_BASE_ADDRESS(CDC_WAR_DATA_CE)) {
240 hif_write32_mb(indicator_addr,
241 (CDC_WAR_MAGIC_STR | write_index));
242 } else {
243 unsigned long irq_flags;
244 local_irq_save(irq_flags);
245 hif_write32_mb(indicator_addr, 1);
246
247 /*
248 * PCIE write waits for ACK in IPQ8K, there is no
249 * need to read back value.
250 */
251 (void)hif_read32_mb(indicator_addr);
252 (void)hif_read32_mb(indicator_addr); /* conservative */
253
254 CE_SRC_RING_WRITE_IDX_SET(scn,
255 ctrl_addr, write_index);
256
257 hif_write32_mb(indicator_addr, 0);
258 local_irq_restore(irq_flags);
259 }
260 } else
261 CE_SRC_RING_WRITE_IDX_SET(scn, ctrl_addr, write_index);
262}
263
264int
265ce_send_nolock(struct CE_handle *copyeng,
266 void *per_transfer_context,
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +0530267 qdf_dma_addr_t buffer,
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800268 uint32_t nbytes,
269 uint32_t transfer_id,
270 uint32_t flags,
271 uint32_t user_flags)
272{
273 int status;
274 struct CE_state *CE_state = (struct CE_state *)copyeng;
275 struct CE_ring_state *src_ring = CE_state->src_ring;
276 uint32_t ctrl_addr = CE_state->ctrl_addr;
277 unsigned int nentries_mask = src_ring->nentries_mask;
278 unsigned int sw_index = src_ring->sw_index;
279 unsigned int write_index = src_ring->write_index;
280 uint64_t dma_addr = buffer;
Komal Seelam644263d2016-02-22 20:45:49 +0530281 struct hif_softc *scn = CE_state->scn;
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800282
Houston Hoffman2c32cf62016-03-14 21:12:00 -0700283 if (Q_TARGET_ACCESS_BEGIN(scn) < 0)
Houston Hoffman987ab442016-03-14 21:12:02 -0700284 return QDF_STATUS_E_FAILURE;
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800285 if (unlikely(CE_RING_DELTA(nentries_mask,
286 write_index, sw_index - 1) <= 0)) {
287 OL_ATH_CE_PKT_ERROR_COUNT_INCR(scn, CE_RING_DELTA_FAIL);
Houston Hoffman987ab442016-03-14 21:12:02 -0700288 Q_TARGET_ACCESS_END(scn);
289 return QDF_STATUS_E_FAILURE;
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800290 }
291 {
Houston Hoffman68e837e2015-12-04 12:57:24 -0800292 enum hif_ce_event_type event_type = HIF_TX_GATHER_DESC_POST;
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800293 struct CE_src_desc *src_ring_base =
294 (struct CE_src_desc *)src_ring->base_addr_owner_space;
295 struct CE_src_desc *shadow_base =
296 (struct CE_src_desc *)src_ring->shadow_base;
297 struct CE_src_desc *src_desc =
298 CE_SRC_RING_TO_DESC(src_ring_base, write_index);
299 struct CE_src_desc *shadow_src_desc =
300 CE_SRC_RING_TO_DESC(shadow_base, write_index);
301
302 /* Update low 32 bits source descriptor address */
303 shadow_src_desc->buffer_addr =
304 (uint32_t)(dma_addr & 0xFFFFFFFF);
305#ifdef QCA_WIFI_3_0
306 shadow_src_desc->buffer_addr_hi =
307 (uint32_t)((dma_addr >> 32) & 0x1F);
308 user_flags |= shadow_src_desc->buffer_addr_hi;
309 memcpy(&(((uint32_t *)shadow_src_desc)[1]), &user_flags,
310 sizeof(uint32_t));
311#endif
312 shadow_src_desc->meta_data = transfer_id;
313
314 /*
315 * Set the swap bit if:
316 * typical sends on this CE are swapped (host is big-endian)
317 * and this send doesn't disable the swapping
318 * (data is not bytestream)
319 */
320 shadow_src_desc->byte_swap =
321 (((CE_state->attr_flags & CE_ATTR_BYTE_SWAP_DATA)
322 != 0) & ((flags & CE_SEND_FLAG_SWAP_DISABLE) == 0));
323 shadow_src_desc->gather = ((flags & CE_SEND_FLAG_GATHER) != 0);
324 shadow_src_desc->nbytes = nbytes;
325
326 *src_desc = *shadow_src_desc;
327
328 src_ring->per_transfer_context[write_index] =
329 per_transfer_context;
330
331 /* Update Source Ring Write Index */
332 write_index = CE_RING_IDX_INCR(nentries_mask, write_index);
333
334 /* WORKAROUND */
335 if (!shadow_src_desc->gather) {
Houston Hoffman68e837e2015-12-04 12:57:24 -0800336 event_type = HIF_TX_DESC_POST;
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800337 war_ce_src_ring_write_idx_set(scn, ctrl_addr,
338 write_index);
339 }
340
Houston Hoffman68e837e2015-12-04 12:57:24 -0800341 /* src_ring->write index hasn't been updated event though
342 * the register has allready been written to.
343 */
Komal Seelambd7c51d2016-02-24 10:27:30 +0530344 hif_record_ce_desc_event(scn, CE_state->id, event_type,
Houston Hoffman68e837e2015-12-04 12:57:24 -0800345 (union ce_desc *) shadow_src_desc, per_transfer_context,
346 src_ring->write_index);
347
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800348 src_ring->write_index = write_index;
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +0530349 status = QDF_STATUS_SUCCESS;
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800350 }
Houston Hoffman987ab442016-03-14 21:12:02 -0700351 Q_TARGET_ACCESS_END(scn);
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800352 return status;
353}
354
355int
356ce_send(struct CE_handle *copyeng,
357 void *per_transfer_context,
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +0530358 qdf_dma_addr_t buffer,
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800359 uint32_t nbytes,
360 uint32_t transfer_id,
361 uint32_t flags,
362 uint32_t user_flag)
363{
364 struct CE_state *CE_state = (struct CE_state *)copyeng;
365 int status;
366
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +0530367 qdf_spin_lock_bh(&CE_state->ce_index_lock);
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800368 status = ce_send_nolock(copyeng, per_transfer_context, buffer, nbytes,
369 transfer_id, flags, user_flag);
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +0530370 qdf_spin_unlock_bh(&CE_state->ce_index_lock);
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800371
372 return status;
373}
374
375unsigned int ce_sendlist_sizeof(void)
376{
377 return sizeof(struct ce_sendlist);
378}
379
380void ce_sendlist_init(struct ce_sendlist *sendlist)
381{
382 struct ce_sendlist_s *sl = (struct ce_sendlist_s *)sendlist;
383 sl->num_items = 0;
384}
385
386int
387ce_sendlist_buf_add(struct ce_sendlist *sendlist,
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +0530388 qdf_dma_addr_t buffer,
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800389 uint32_t nbytes,
390 uint32_t flags,
391 uint32_t user_flags)
392{
393 struct ce_sendlist_s *sl = (struct ce_sendlist_s *)sendlist;
394 unsigned int num_items = sl->num_items;
395 struct ce_sendlist_item *item;
396
397 if (num_items >= CE_SENDLIST_ITEMS_MAX) {
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +0530398 QDF_ASSERT(num_items < CE_SENDLIST_ITEMS_MAX);
399 return QDF_STATUS_E_RESOURCES;
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800400 }
401
402 item = &sl->item[num_items];
403 item->send_type = CE_SIMPLE_BUFFER_TYPE;
404 item->data = buffer;
405 item->u.nbytes = nbytes;
406 item->flags = flags;
407 item->user_flags = user_flags;
408 sl->num_items = num_items + 1;
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +0530409 return QDF_STATUS_SUCCESS;
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800410}
411
412int
413ce_sendlist_send(struct CE_handle *copyeng,
414 void *per_transfer_context,
415 struct ce_sendlist *sendlist, unsigned int transfer_id)
416{
417 int status = -ENOMEM;
418 struct ce_sendlist_s *sl = (struct ce_sendlist_s *)sendlist;
419 struct CE_state *CE_state = (struct CE_state *)copyeng;
420 struct CE_ring_state *src_ring = CE_state->src_ring;
421 unsigned int nentries_mask = src_ring->nentries_mask;
422 unsigned int num_items = sl->num_items;
423 unsigned int sw_index;
424 unsigned int write_index;
425
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +0530426 QDF_ASSERT((num_items > 0) && (num_items < src_ring->nentries));
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800427
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +0530428 qdf_spin_lock_bh(&CE_state->ce_index_lock);
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800429 sw_index = src_ring->sw_index;
430 write_index = src_ring->write_index;
431
432 if (CE_RING_DELTA(nentries_mask, write_index, sw_index - 1) >=
433 num_items) {
434 struct ce_sendlist_item *item;
435 int i;
436
437 /* handle all but the last item uniformly */
438 for (i = 0; i < num_items - 1; i++) {
439 item = &sl->item[i];
440 /* TBDXXX: Support extensible sendlist_types? */
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +0530441 QDF_ASSERT(item->send_type == CE_SIMPLE_BUFFER_TYPE);
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800442 status = ce_send_nolock(copyeng, CE_SENDLIST_ITEM_CTXT,
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +0530443 (qdf_dma_addr_t) item->data,
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800444 item->u.nbytes, transfer_id,
445 item->flags | CE_SEND_FLAG_GATHER,
446 item->user_flags);
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +0530447 QDF_ASSERT(status == QDF_STATUS_SUCCESS);
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800448 }
449 /* provide valid context pointer for final item */
450 item = &sl->item[i];
451 /* TBDXXX: Support extensible sendlist_types? */
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +0530452 QDF_ASSERT(item->send_type == CE_SIMPLE_BUFFER_TYPE);
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800453 status = ce_send_nolock(copyeng, per_transfer_context,
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +0530454 (qdf_dma_addr_t) item->data,
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800455 item->u.nbytes,
456 transfer_id, item->flags,
457 item->user_flags);
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +0530458 QDF_ASSERT(status == QDF_STATUS_SUCCESS);
Vishwajith Upendra70f8b6e2016-03-01 16:28:23 +0530459 QDF_NBUF_UPDATE_TX_PKT_COUNT((qdf_nbuf_t)per_transfer_context,
460 QDF_NBUF_TX_PKT_CE);
461 DPTRACE(qdf_dp_trace((qdf_nbuf_t)per_transfer_context,
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +0530462 QDF_DP_TRACE_CE_PACKET_PTR_RECORD,
Vishwajith Upendra70f8b6e2016-03-01 16:28:23 +0530463 (uint8_t *)(((qdf_nbuf_t)per_transfer_context)->data),
464 sizeof(((qdf_nbuf_t)per_transfer_context)->data)));
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800465 } else {
466 /*
467 * Probably not worth the additional complexity to support
468 * partial sends with continuation or notification. We expect
469 * to use large rings and small sendlists. If we can't handle
470 * the entire request at once, punt it back to the caller.
471 */
472 }
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +0530473 qdf_spin_unlock_bh(&CE_state->ce_index_lock);
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800474
475 return status;
476}
477
478#ifdef WLAN_FEATURE_FASTPATH
479#ifdef QCA_WIFI_3_0
480static inline void
481ce_buffer_addr_hi_set(struct CE_src_desc *shadow_src_desc,
482 uint64_t dma_addr,
483 uint32_t user_flags)
484{
485 shadow_src_desc->buffer_addr_hi =
486 (uint32_t)((dma_addr >> 32) & 0x1F);
487 user_flags |= shadow_src_desc->buffer_addr_hi;
488 memcpy(&(((uint32_t *)shadow_src_desc)[1]), &user_flags,
489 sizeof(uint32_t));
490}
491#else
492static inline void
493ce_buffer_addr_hi_set(struct CE_src_desc *shadow_src_desc,
494 uint64_t dma_addr,
495 uint32_t user_flags)
496{
497}
498#endif
499
500/**
501 * ce_send_fast() CE layer Tx buffer posting function
502 * @copyeng: copy engine handle
503 * @msdus: iarray of msdu to be sent
504 * @num_msdus: number of msdus in an array
505 * @transfer_id: transfer_id
506 *
507 * Assumption : Called with an array of MSDU's
508 * Function:
509 * For each msdu in the array
510 * 1. Check no. of available entries
511 * 2. Create src ring entries (allocated in consistent memory
512 * 3. Write index to h/w
513 *
514 * Return: No. of packets that could be sent
515 */
516
Vishwajith Upendra70f8b6e2016-03-01 16:28:23 +0530517int ce_send_fast(struct CE_handle *copyeng, qdf_nbuf_t *msdus,
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800518 unsigned int num_msdus, unsigned int transfer_id)
519{
520 struct CE_state *ce_state = (struct CE_state *)copyeng;
Komal Seelam644263d2016-02-22 20:45:49 +0530521 struct hif_softc *scn = ce_state->scn;
Komal Seelam5584a7c2016-02-24 19:22:48 +0530522 struct hif_opaque_softc *hif_hdl = GET_HIF_OPAQUE_HDL(scn);
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800523 struct CE_ring_state *src_ring = ce_state->src_ring;
524 u_int32_t ctrl_addr = ce_state->ctrl_addr;
525 unsigned int nentries_mask = src_ring->nentries_mask;
526 unsigned int write_index;
Manjunathappa Prakash4a9c3a82016-04-14 01:12:14 -0700527 unsigned int sw_index;
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800528 unsigned int frag_len;
Vishwajith Upendra70f8b6e2016-03-01 16:28:23 +0530529 qdf_nbuf_t msdu;
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800530 int i;
531 uint64_t dma_addr;
Manjunathappa Prakash4a9c3a82016-04-14 01:12:14 -0700532 uint32_t user_flags;
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800533
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +0530534 qdf_spin_lock_bh(&ce_state->ce_index_lock);
Manjunathappa Prakash4a9c3a82016-04-14 01:12:14 -0700535 Q_TARGET_ACCESS_BEGIN(scn);
536
537 src_ring->sw_index = CE_SRC_RING_READ_IDX_GET_FROM_DDR(scn, ctrl_addr);
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800538 write_index = src_ring->write_index;
Manjunathappa Prakash4a9c3a82016-04-14 01:12:14 -0700539 sw_index = src_ring->sw_index;
540
Houston Hoffmanfa260aa2016-04-26 16:14:13 -0700541 hif_record_ce_desc_event(scn, ce_state->id,
542 FAST_TX_SOFTWARE_INDEX_UPDATE,
543 NULL, NULL, write_index);
544
Manjunathappa Prakash4a9c3a82016-04-14 01:12:14 -0700545 if (qdf_unlikely(CE_RING_DELTA(nentries_mask, write_index, sw_index - 1)
546 < (SLOTS_PER_DATAPATH_TX * num_msdus))) {
547 HIF_ERROR("Source ring full, required %d, available %d",
548 (SLOTS_PER_DATAPATH_TX * num_msdus),
549 CE_RING_DELTA(nentries_mask, write_index, sw_index - 1));
550 OL_ATH_CE_PKT_ERROR_COUNT_INCR(scn, CE_RING_DELTA_FAIL);
551 Q_TARGET_ACCESS_END(scn);
552 qdf_spin_unlock_bh(&ce_state->ce_index_lock);
553 return 0;
554 }
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800555
556 /* 2 msdus per packet */
557 for (i = 0; i < num_msdus; i++) {
558 struct CE_src_desc *src_ring_base =
559 (struct CE_src_desc *)src_ring->base_addr_owner_space;
560 struct CE_src_desc *shadow_base =
561 (struct CE_src_desc *)src_ring->shadow_base;
562 struct CE_src_desc *src_desc =
563 CE_SRC_RING_TO_DESC(src_ring_base, write_index);
564 struct CE_src_desc *shadow_src_desc =
565 CE_SRC_RING_TO_DESC(shadow_base, write_index);
566
Komal Seelam644263d2016-02-22 20:45:49 +0530567 hif_pm_runtime_get_noresume(hif_hdl);
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800568 msdu = msdus[i];
569
570 /*
571 * First fill out the ring descriptor for the HTC HTT frame
572 * header. These are uncached writes. Should we use a local
573 * structure instead?
574 */
575 /* HTT/HTC header can be passed as a argument */
Vishwajith Upendra70f8b6e2016-03-01 16:28:23 +0530576 dma_addr = qdf_nbuf_get_frag_paddr(msdu, 0);
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800577 shadow_src_desc->buffer_addr = (uint32_t)(dma_addr &
578 0xFFFFFFFF);
Vishwajith Upendra70f8b6e2016-03-01 16:28:23 +0530579 user_flags = qdf_nbuf_data_attr_get(msdu) & DESC_DATA_FLAG_MASK;
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800580 ce_buffer_addr_hi_set(shadow_src_desc, dma_addr, user_flags);
581
582 shadow_src_desc->meta_data = transfer_id;
Vishwajith Upendra70f8b6e2016-03-01 16:28:23 +0530583 shadow_src_desc->nbytes = qdf_nbuf_get_frag_len(msdu, 0);
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800584
585 /*
586 * HTC HTT header is a word stream, so byte swap if CE byte
587 * swap enabled
588 */
589 shadow_src_desc->byte_swap = ((ce_state->attr_flags &
590 CE_ATTR_BYTE_SWAP_DATA) != 0);
591 /* For the first one, it still does not need to write */
592 shadow_src_desc->gather = 1;
593 *src_desc = *shadow_src_desc;
594
595 /* By default we could initialize the transfer context to this
596 * value
597 */
598 src_ring->per_transfer_context[write_index] =
599 CE_SENDLIST_ITEM_CTXT;
600
601 write_index = CE_RING_IDX_INCR(nentries_mask, write_index);
602
603 src_desc = CE_SRC_RING_TO_DESC(src_ring_base, write_index);
604 shadow_src_desc = CE_SRC_RING_TO_DESC(shadow_base, write_index);
605 /*
606 * Now fill out the ring descriptor for the actual data
607 * packet
608 */
Vishwajith Upendra70f8b6e2016-03-01 16:28:23 +0530609 dma_addr = qdf_nbuf_get_frag_paddr(msdu, 1);
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800610 shadow_src_desc->buffer_addr = (uint32_t)(dma_addr &
611 0xFFFFFFFF);
612 /*
613 * Clear packet offset for all but the first CE desc.
614 */
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +0530615 user_flags &= ~QDF_CE_TX_PKT_OFFSET_BIT_M;
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800616 ce_buffer_addr_hi_set(shadow_src_desc, dma_addr, user_flags);
617 shadow_src_desc->meta_data = transfer_id;
618
619 /* get actual packet length */
Vishwajith Upendra70f8b6e2016-03-01 16:28:23 +0530620 frag_len = qdf_nbuf_get_frag_len(msdu, 1);
Houston Hoffmana5e74c12015-09-02 18:06:28 -0700621
622 /* only read download_len once */
623 shadow_src_desc->nbytes = ce_state->download_len;
624 if (shadow_src_desc->nbytes > frag_len)
625 shadow_src_desc->nbytes = frag_len;
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800626
627 /* Data packet is a byte stream, so disable byte swap */
628 shadow_src_desc->byte_swap = 0;
629 /* For the last one, gather is not set */
630 shadow_src_desc->gather = 0;
631 *src_desc = *shadow_src_desc;
632 src_ring->per_transfer_context[write_index] = msdu;
633 write_index = CE_RING_IDX_INCR(nentries_mask, write_index);
634 }
635
636 /* Write the final index to h/w one-shot */
637 if (i) {
638 src_ring->write_index = write_index;
Houston Hoffmanf4607852015-12-17 17:14:40 -0800639
Komal Seelam644263d2016-02-22 20:45:49 +0530640 if (hif_pm_runtime_get(hif_hdl) == 0) {
Houston Hoffmanfa260aa2016-04-26 16:14:13 -0700641 hif_record_ce_desc_event(scn, ce_state->id,
642 FAST_TX_WRITE_INDEX_UPDATE,
643 NULL, NULL, write_index);
644
Houston Hoffmanf4607852015-12-17 17:14:40 -0800645 /* Don't call WAR_XXX from here
646 * Just call XXX instead, that has the reqd. intel
647 */
648 war_ce_src_ring_write_idx_set(scn, ctrl_addr,
649 write_index);
Komal Seelam644263d2016-02-22 20:45:49 +0530650 hif_pm_runtime_put(hif_hdl);
Houston Hoffmanf4607852015-12-17 17:14:40 -0800651 }
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800652 }
653
Manjunathappa Prakash4a9c3a82016-04-14 01:12:14 -0700654 Q_TARGET_ACCESS_END(scn);
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +0530655 qdf_spin_unlock_bh(&ce_state->ce_index_lock);
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800656
657 /*
658 * If all packets in the array are transmitted,
659 * i = num_msdus
660 * Temporarily add an ASSERT
661 */
662 ASSERT(i == num_msdus);
663 return i;
664}
Manjunathappa Prakash4a9c3a82016-04-14 01:12:14 -0700665
666/**
667 * ce_is_fastpath_enabled() - returns true if fastpath mode is enabled
668 * @scn: Handle to HIF context
669 *
670 * Return: true if fastpath is enabled else false.
671 */
672static bool ce_is_fastpath_enabled(struct hif_softc *scn)
673{
674 return scn->fastpath_mode_on;
675}
676
677/**
678 * ce_is_fastpath_handler_registered() - return true for datapath CEs and if
679 * fastpath is enabled.
680 * @ce_state: handle to copy engine
681 *
682 * Return: true if fastpath handler is registered for datapath CE.
683 */
684static bool ce_is_fastpath_handler_registered(struct CE_state *ce_state)
685{
686 if (ce_state->fastpath_handler)
687 return true;
688 else
689 return false;
690}
691
692
693#else
694static inline bool ce_is_fastpath_enabled(struct hif_softc *scn)
695{
696 return false;
697}
698
699static inline bool ce_is_fastpath_handler_registered(struct CE_state *ce_state)
700{
701 return false;
702}
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800703#endif /* WLAN_FEATURE_FASTPATH */
704
Houston Hoffman4411ad42016-03-14 21:12:04 -0700705/**
706 * ce_recv_buf_enqueue() - enqueue a recv buffer into a copy engine
707 * @coyeng: copy engine handle
708 * @per_recv_context: virtual address of the nbuf
709 * @buffer: physical address of the nbuf
710 *
711 * Return: 0 if the buffer is enqueued
712 */
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800713int
714ce_recv_buf_enqueue(struct CE_handle *copyeng,
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +0530715 void *per_recv_context, qdf_dma_addr_t buffer)
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800716{
717 int status;
718 struct CE_state *CE_state = (struct CE_state *)copyeng;
719 struct CE_ring_state *dest_ring = CE_state->dest_ring;
720 uint32_t ctrl_addr = CE_state->ctrl_addr;
721 unsigned int nentries_mask = dest_ring->nentries_mask;
722 unsigned int write_index;
723 unsigned int sw_index;
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800724 uint64_t dma_addr = buffer;
Komal Seelam644263d2016-02-22 20:45:49 +0530725 struct hif_softc *scn = CE_state->scn;
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800726
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +0530727 qdf_spin_lock_bh(&CE_state->ce_index_lock);
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800728 write_index = dest_ring->write_index;
729 sw_index = dest_ring->sw_index;
730
Houston Hoffman4411ad42016-03-14 21:12:04 -0700731 if (Q_TARGET_ACCESS_BEGIN(scn) < 0) {
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +0530732 qdf_spin_unlock_bh(&CE_state->ce_index_lock);
Houston Hoffman4411ad42016-03-14 21:12:04 -0700733 return -EIO;
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800734 }
735
Manjunathappa Prakash7399f142016-04-13 23:38:16 -0700736 if ((CE_RING_DELTA(nentries_mask, write_index, sw_index - 1) > 0) ||
Manjunathappa Prakash4a9c3a82016-04-14 01:12:14 -0700737 (ce_is_fastpath_enabled(scn) && CE_state->htt_rx_data)) {
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800738 struct CE_dest_desc *dest_ring_base =
Manjunathappa Prakash7399f142016-04-13 23:38:16 -0700739 (struct CE_dest_desc *)dest_ring->base_addr_owner_space;
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800740 struct CE_dest_desc *dest_desc =
741 CE_DEST_RING_TO_DESC(dest_ring_base, write_index);
742
743 /* Update low 32 bit destination descriptor */
744 dest_desc->buffer_addr = (uint32_t)(dma_addr & 0xFFFFFFFF);
745#ifdef QCA_WIFI_3_0
746 dest_desc->buffer_addr_hi =
747 (uint32_t)((dma_addr >> 32) & 0x1F);
748#endif
749 dest_desc->nbytes = 0;
750
751 dest_ring->per_transfer_context[write_index] =
752 per_recv_context;
753
Komal Seelambd7c51d2016-02-24 10:27:30 +0530754 hif_record_ce_desc_event(scn, CE_state->id, HIF_RX_DESC_POST,
Houston Hoffman68e837e2015-12-04 12:57:24 -0800755 (union ce_desc *) dest_desc, per_recv_context,
756 write_index);
757
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800758 /* Update Destination Ring Write Index */
759 write_index = CE_RING_IDX_INCR(nentries_mask, write_index);
Manjunathappa Prakash7399f142016-04-13 23:38:16 -0700760 if (write_index != sw_index) {
761 CE_DEST_RING_WRITE_IDX_SET(scn, ctrl_addr, write_index);
762 dest_ring->write_index = write_index;
763 }
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +0530764 status = QDF_STATUS_SUCCESS;
Manjunathappa Prakash7399f142016-04-13 23:38:16 -0700765 } else
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +0530766 status = QDF_STATUS_E_FAILURE;
Manjunathappa Prakash7399f142016-04-13 23:38:16 -0700767
Houston Hoffman4411ad42016-03-14 21:12:04 -0700768 Q_TARGET_ACCESS_END(scn);
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +0530769 qdf_spin_unlock_bh(&CE_state->ce_index_lock);
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800770 return status;
771}
772
773void
774ce_send_watermarks_set(struct CE_handle *copyeng,
775 unsigned int low_alert_nentries,
776 unsigned int high_alert_nentries)
777{
778 struct CE_state *CE_state = (struct CE_state *)copyeng;
779 uint32_t ctrl_addr = CE_state->ctrl_addr;
Komal Seelam644263d2016-02-22 20:45:49 +0530780 struct hif_softc *scn = CE_state->scn;
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800781
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800782 CE_SRC_RING_LOWMARK_SET(scn, ctrl_addr, low_alert_nentries);
783 CE_SRC_RING_HIGHMARK_SET(scn, ctrl_addr, high_alert_nentries);
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800784}
785
786void
787ce_recv_watermarks_set(struct CE_handle *copyeng,
788 unsigned int low_alert_nentries,
789 unsigned int high_alert_nentries)
790{
791 struct CE_state *CE_state = (struct CE_state *)copyeng;
792 uint32_t ctrl_addr = CE_state->ctrl_addr;
Komal Seelam644263d2016-02-22 20:45:49 +0530793 struct hif_softc *scn = CE_state->scn;
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800794
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800795 CE_DEST_RING_LOWMARK_SET(scn, ctrl_addr,
796 low_alert_nentries);
797 CE_DEST_RING_HIGHMARK_SET(scn, ctrl_addr,
798 high_alert_nentries);
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800799}
800
801unsigned int ce_send_entries_avail(struct CE_handle *copyeng)
802{
803 struct CE_state *CE_state = (struct CE_state *)copyeng;
804 struct CE_ring_state *src_ring = CE_state->src_ring;
805 unsigned int nentries_mask = src_ring->nentries_mask;
806 unsigned int sw_index;
807 unsigned int write_index;
808
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +0530809 qdf_spin_lock(&CE_state->ce_index_lock);
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800810 sw_index = src_ring->sw_index;
811 write_index = src_ring->write_index;
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +0530812 qdf_spin_unlock(&CE_state->ce_index_lock);
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800813
814 return CE_RING_DELTA(nentries_mask, write_index, sw_index - 1);
815}
816
817unsigned int ce_recv_entries_avail(struct CE_handle *copyeng)
818{
819 struct CE_state *CE_state = (struct CE_state *)copyeng;
820 struct CE_ring_state *dest_ring = CE_state->dest_ring;
821 unsigned int nentries_mask = dest_ring->nentries_mask;
822 unsigned int sw_index;
823 unsigned int write_index;
824
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +0530825 qdf_spin_lock(&CE_state->ce_index_lock);
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800826 sw_index = dest_ring->sw_index;
827 write_index = dest_ring->write_index;
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +0530828 qdf_spin_unlock(&CE_state->ce_index_lock);
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800829
830 return CE_RING_DELTA(nentries_mask, write_index, sw_index - 1);
831}
832
833/*
834 * Guts of ce_send_entries_done.
835 * The caller takes responsibility for any necessary locking.
836 */
837unsigned int
Komal Seelam644263d2016-02-22 20:45:49 +0530838ce_send_entries_done_nolock(struct hif_softc *scn,
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800839 struct CE_state *CE_state)
840{
841 struct CE_ring_state *src_ring = CE_state->src_ring;
842 uint32_t ctrl_addr = CE_state->ctrl_addr;
843 unsigned int nentries_mask = src_ring->nentries_mask;
844 unsigned int sw_index;
845 unsigned int read_index;
846
847 sw_index = src_ring->sw_index;
848 read_index = CE_SRC_RING_READ_IDX_GET(scn, ctrl_addr);
849
850 return CE_RING_DELTA(nentries_mask, sw_index, read_index);
851}
852
853unsigned int ce_send_entries_done(struct CE_handle *copyeng)
854{
855 struct CE_state *CE_state = (struct CE_state *)copyeng;
856 unsigned int nentries;
857
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +0530858 qdf_spin_lock(&CE_state->ce_index_lock);
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800859 nentries = ce_send_entries_done_nolock(CE_state->scn, CE_state);
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +0530860 qdf_spin_unlock(&CE_state->ce_index_lock);
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800861
862 return nentries;
863}
864
865/*
866 * Guts of ce_recv_entries_done.
867 * The caller takes responsibility for any necessary locking.
868 */
869unsigned int
Komal Seelam644263d2016-02-22 20:45:49 +0530870ce_recv_entries_done_nolock(struct hif_softc *scn,
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800871 struct CE_state *CE_state)
872{
873 struct CE_ring_state *dest_ring = CE_state->dest_ring;
874 uint32_t ctrl_addr = CE_state->ctrl_addr;
875 unsigned int nentries_mask = dest_ring->nentries_mask;
876 unsigned int sw_index;
877 unsigned int read_index;
878
879 sw_index = dest_ring->sw_index;
880 read_index = CE_DEST_RING_READ_IDX_GET(scn, ctrl_addr);
881
882 return CE_RING_DELTA(nentries_mask, sw_index, read_index);
883}
884
885unsigned int ce_recv_entries_done(struct CE_handle *copyeng)
886{
887 struct CE_state *CE_state = (struct CE_state *)copyeng;
888 unsigned int nentries;
889
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +0530890 qdf_spin_lock(&CE_state->ce_index_lock);
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800891 nentries = ce_recv_entries_done_nolock(CE_state->scn, CE_state);
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +0530892 qdf_spin_unlock(&CE_state->ce_index_lock);
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800893
894 return nentries;
895}
896
897/* Debug support */
898void *ce_debug_cmplrn_context; /* completed recv next context */
899void *ce_debug_cnclsn_context; /* cancel send next context */
900void *ce_debug_rvkrn_context; /* revoke receive next context */
901void *ce_debug_cmplsn_context; /* completed send next context */
902
903/*
904 * Guts of ce_completed_recv_next.
905 * The caller takes responsibility for any necessary locking.
906 */
907int
908ce_completed_recv_next_nolock(struct CE_state *CE_state,
909 void **per_CE_contextp,
910 void **per_transfer_contextp,
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +0530911 qdf_dma_addr_t *bufferp,
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800912 unsigned int *nbytesp,
913 unsigned int *transfer_idp,
914 unsigned int *flagsp)
915{
916 int status;
917 struct CE_ring_state *dest_ring = CE_state->dest_ring;
918 unsigned int nentries_mask = dest_ring->nentries_mask;
919 unsigned int sw_index = dest_ring->sw_index;
Komal Seelambd7c51d2016-02-24 10:27:30 +0530920 struct hif_softc *scn = CE_state->scn;
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800921 struct CE_dest_desc *dest_ring_base =
922 (struct CE_dest_desc *)dest_ring->base_addr_owner_space;
923 struct CE_dest_desc *dest_desc =
924 CE_DEST_RING_TO_DESC(dest_ring_base, sw_index);
925 int nbytes;
926 struct CE_dest_desc dest_desc_info;
927 /*
928 * By copying the dest_desc_info element to local memory, we could
929 * avoid extra memory read from non-cachable memory.
930 */
931 dest_desc_info = *dest_desc;
932 nbytes = dest_desc_info.nbytes;
933 if (nbytes == 0) {
934 /*
935 * This closes a relatively unusual race where the Host
936 * sees the updated DRRI before the update to the
937 * corresponding descriptor has completed. We treat this
938 * as a descriptor that is not yet done.
939 */
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +0530940 status = QDF_STATUS_E_FAILURE;
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800941 goto done;
942 }
943
Komal Seelambd7c51d2016-02-24 10:27:30 +0530944 hif_record_ce_desc_event(scn, CE_state->id, HIF_RX_DESC_COMPLETION,
Houston Hoffman68e837e2015-12-04 12:57:24 -0800945 (union ce_desc *) dest_desc,
946 dest_ring->per_transfer_context[sw_index],
947 sw_index);
948
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800949 dest_desc->nbytes = 0;
950
951 /* Return data from completed destination descriptor */
952 *bufferp = HIF_CE_DESC_ADDR_TO_DMA(&dest_desc_info);
953 *nbytesp = nbytes;
954 *transfer_idp = dest_desc_info.meta_data;
955 *flagsp = (dest_desc_info.byte_swap) ? CE_RECV_FLAG_SWAPPED : 0;
956
957 if (per_CE_contextp) {
958 *per_CE_contextp = CE_state->recv_context;
959 }
960
961 ce_debug_cmplrn_context = dest_ring->per_transfer_context[sw_index];
962 if (per_transfer_contextp) {
963 *per_transfer_contextp = ce_debug_cmplrn_context;
964 }
965 dest_ring->per_transfer_context[sw_index] = 0; /* sanity */
966
967 /* Update sw_index */
968 sw_index = CE_RING_IDX_INCR(nentries_mask, sw_index);
969 dest_ring->sw_index = sw_index;
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +0530970 status = QDF_STATUS_SUCCESS;
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800971
972done:
973 return status;
974}
975
976int
977ce_completed_recv_next(struct CE_handle *copyeng,
978 void **per_CE_contextp,
979 void **per_transfer_contextp,
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +0530980 qdf_dma_addr_t *bufferp,
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800981 unsigned int *nbytesp,
982 unsigned int *transfer_idp, unsigned int *flagsp)
983{
984 struct CE_state *CE_state = (struct CE_state *)copyeng;
985 int status;
986
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +0530987 qdf_spin_lock_bh(&CE_state->ce_index_lock);
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800988 status =
989 ce_completed_recv_next_nolock(CE_state, per_CE_contextp,
990 per_transfer_contextp, bufferp,
991 nbytesp, transfer_idp, flagsp);
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +0530992 qdf_spin_unlock_bh(&CE_state->ce_index_lock);
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800993
994 return status;
995}
996
997/* NB: Modeled after ce_completed_recv_next_nolock */
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +0530998QDF_STATUS
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -0800999ce_revoke_recv_next(struct CE_handle *copyeng,
1000 void **per_CE_contextp,
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +05301001 void **per_transfer_contextp, qdf_dma_addr_t *bufferp)
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001002{
1003 struct CE_state *CE_state;
1004 struct CE_ring_state *dest_ring;
1005 unsigned int nentries_mask;
1006 unsigned int sw_index;
1007 unsigned int write_index;
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +05301008 QDF_STATUS status;
Komal Seelam644263d2016-02-22 20:45:49 +05301009 struct hif_softc *scn;
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001010
1011 CE_state = (struct CE_state *)copyeng;
1012 dest_ring = CE_state->dest_ring;
1013 if (!dest_ring) {
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +05301014 return QDF_STATUS_E_FAILURE;
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001015 }
1016
1017 scn = CE_state->scn;
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +05301018 qdf_spin_lock(&CE_state->ce_index_lock);
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001019 nentries_mask = dest_ring->nentries_mask;
1020 sw_index = dest_ring->sw_index;
1021 write_index = dest_ring->write_index;
1022 if (write_index != sw_index) {
1023 struct CE_dest_desc *dest_ring_base =
1024 (struct CE_dest_desc *)dest_ring->
1025 base_addr_owner_space;
1026 struct CE_dest_desc *dest_desc =
1027 CE_DEST_RING_TO_DESC(dest_ring_base, sw_index);
1028
1029 /* Return data from completed destination descriptor */
1030 *bufferp = HIF_CE_DESC_ADDR_TO_DMA(dest_desc);
1031
1032 if (per_CE_contextp) {
1033 *per_CE_contextp = CE_state->recv_context;
1034 }
1035
1036 ce_debug_rvkrn_context =
1037 dest_ring->per_transfer_context[sw_index];
1038 if (per_transfer_contextp) {
1039 *per_transfer_contextp = ce_debug_rvkrn_context;
1040 }
1041 dest_ring->per_transfer_context[sw_index] = 0; /* sanity */
1042
1043 /* Update sw_index */
1044 sw_index = CE_RING_IDX_INCR(nentries_mask, sw_index);
1045 dest_ring->sw_index = sw_index;
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +05301046 status = QDF_STATUS_SUCCESS;
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001047 } else {
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +05301048 status = QDF_STATUS_E_FAILURE;
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001049 }
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +05301050 qdf_spin_unlock(&CE_state->ce_index_lock);
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001051
1052 return status;
1053}
1054
1055/*
1056 * Guts of ce_completed_send_next.
1057 * The caller takes responsibility for any necessary locking.
1058 */
1059int
1060ce_completed_send_next_nolock(struct CE_state *CE_state,
1061 void **per_CE_contextp,
1062 void **per_transfer_contextp,
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +05301063 qdf_dma_addr_t *bufferp,
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001064 unsigned int *nbytesp,
1065 unsigned int *transfer_idp,
1066 unsigned int *sw_idx,
1067 unsigned int *hw_idx,
1068 uint32_t *toeplitz_hash_result)
1069{
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +05301070 int status = QDF_STATUS_E_FAILURE;
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001071 struct CE_ring_state *src_ring = CE_state->src_ring;
1072 uint32_t ctrl_addr = CE_state->ctrl_addr;
1073 unsigned int nentries_mask = src_ring->nentries_mask;
1074 unsigned int sw_index = src_ring->sw_index;
1075 unsigned int read_index;
Komal Seelam644263d2016-02-22 20:45:49 +05301076 struct hif_softc *scn = CE_state->scn;
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001077
1078 if (src_ring->hw_index == sw_index) {
1079 /*
1080 * The SW completion index has caught up with the cached
1081 * version of the HW completion index.
1082 * Update the cached HW completion index to see whether
1083 * the SW has really caught up to the HW, or if the cached
1084 * value of the HW index has become stale.
1085 */
Houston Hoffman2c32cf62016-03-14 21:12:00 -07001086 if (Q_TARGET_ACCESS_BEGIN(scn) < 0)
Houston Hoffman987ab442016-03-14 21:12:02 -07001087 return QDF_STATUS_E_FAILURE;
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001088 src_ring->hw_index =
Houston Hoffman3d0cda82015-12-03 13:25:05 -08001089 CE_SRC_RING_READ_IDX_GET_FROM_DDR(scn, ctrl_addr);
Houston Hoffman2c32cf62016-03-14 21:12:00 -07001090 if (Q_TARGET_ACCESS_END(scn) < 0)
Houston Hoffman987ab442016-03-14 21:12:02 -07001091 return QDF_STATUS_E_FAILURE;
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001092 }
1093 read_index = src_ring->hw_index;
1094
1095 if (sw_idx)
1096 *sw_idx = sw_index;
1097
1098 if (hw_idx)
1099 *hw_idx = read_index;
1100
1101 if ((read_index != sw_index) && (read_index != 0xffffffff)) {
1102 struct CE_src_desc *shadow_base =
1103 (struct CE_src_desc *)src_ring->shadow_base;
1104 struct CE_src_desc *shadow_src_desc =
1105 CE_SRC_RING_TO_DESC(shadow_base, sw_index);
1106#ifdef QCA_WIFI_3_0
1107 struct CE_src_desc *src_ring_base =
1108 (struct CE_src_desc *)src_ring->base_addr_owner_space;
1109 struct CE_src_desc *src_desc =
1110 CE_SRC_RING_TO_DESC(src_ring_base, sw_index);
1111#endif
Komal Seelambd7c51d2016-02-24 10:27:30 +05301112 hif_record_ce_desc_event(scn, CE_state->id,
1113 HIF_TX_DESC_COMPLETION,
Houston Hoffman68e837e2015-12-04 12:57:24 -08001114 (union ce_desc *) shadow_src_desc,
1115 src_ring->per_transfer_context[sw_index],
1116 sw_index);
1117
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001118 /* Return data from completed source descriptor */
1119 *bufferp = HIF_CE_DESC_ADDR_TO_DMA(shadow_src_desc);
1120 *nbytesp = shadow_src_desc->nbytes;
1121 *transfer_idp = shadow_src_desc->meta_data;
1122#ifdef QCA_WIFI_3_0
1123 *toeplitz_hash_result = src_desc->toeplitz_hash_result;
1124#else
1125 *toeplitz_hash_result = 0;
1126#endif
1127 if (per_CE_contextp) {
1128 *per_CE_contextp = CE_state->send_context;
1129 }
1130
1131 ce_debug_cmplsn_context =
1132 src_ring->per_transfer_context[sw_index];
1133 if (per_transfer_contextp) {
1134 *per_transfer_contextp = ce_debug_cmplsn_context;
1135 }
1136 src_ring->per_transfer_context[sw_index] = 0; /* sanity */
1137
1138 /* Update sw_index */
1139 sw_index = CE_RING_IDX_INCR(nentries_mask, sw_index);
1140 src_ring->sw_index = sw_index;
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +05301141 status = QDF_STATUS_SUCCESS;
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001142 }
1143
1144 return status;
1145}
1146
1147/* NB: Modeled after ce_completed_send_next */
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +05301148QDF_STATUS
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001149ce_cancel_send_next(struct CE_handle *copyeng,
1150 void **per_CE_contextp,
1151 void **per_transfer_contextp,
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +05301152 qdf_dma_addr_t *bufferp,
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001153 unsigned int *nbytesp,
1154 unsigned int *transfer_idp,
1155 uint32_t *toeplitz_hash_result)
1156{
1157 struct CE_state *CE_state;
1158 struct CE_ring_state *src_ring;
1159 unsigned int nentries_mask;
1160 unsigned int sw_index;
1161 unsigned int write_index;
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +05301162 QDF_STATUS status;
Komal Seelam644263d2016-02-22 20:45:49 +05301163 struct hif_softc *scn;
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001164
1165 CE_state = (struct CE_state *)copyeng;
1166 src_ring = CE_state->src_ring;
1167 if (!src_ring) {
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +05301168 return QDF_STATUS_E_FAILURE;
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001169 }
1170
1171 scn = CE_state->scn;
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +05301172 qdf_spin_lock(&CE_state->ce_index_lock);
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001173 nentries_mask = src_ring->nentries_mask;
1174 sw_index = src_ring->sw_index;
1175 write_index = src_ring->write_index;
1176
1177 if (write_index != sw_index) {
1178 struct CE_src_desc *src_ring_base =
1179 (struct CE_src_desc *)src_ring->base_addr_owner_space;
1180 struct CE_src_desc *src_desc =
1181 CE_SRC_RING_TO_DESC(src_ring_base, sw_index);
1182
1183 /* Return data from completed source descriptor */
1184 *bufferp = HIF_CE_DESC_ADDR_TO_DMA(src_desc);
1185 *nbytesp = src_desc->nbytes;
1186 *transfer_idp = src_desc->meta_data;
1187#ifdef QCA_WIFI_3_0
1188 *toeplitz_hash_result = src_desc->toeplitz_hash_result;
1189#else
1190 *toeplitz_hash_result = 0;
1191#endif
1192
1193 if (per_CE_contextp) {
1194 *per_CE_contextp = CE_state->send_context;
1195 }
1196
1197 ce_debug_cnclsn_context =
1198 src_ring->per_transfer_context[sw_index];
1199 if (per_transfer_contextp) {
1200 *per_transfer_contextp = ce_debug_cnclsn_context;
1201 }
1202 src_ring->per_transfer_context[sw_index] = 0; /* sanity */
1203
1204 /* Update sw_index */
1205 sw_index = CE_RING_IDX_INCR(nentries_mask, sw_index);
1206 src_ring->sw_index = sw_index;
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +05301207 status = QDF_STATUS_SUCCESS;
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001208 } else {
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +05301209 status = QDF_STATUS_E_FAILURE;
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001210 }
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +05301211 qdf_spin_unlock(&CE_state->ce_index_lock);
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001212
1213 return status;
1214}
1215
1216/* Shift bits to convert IS_*_RING_*_WATERMARK_MASK to CE_WM_FLAG_*_* */
1217#define CE_WM_SHFT 1
1218
1219int
1220ce_completed_send_next(struct CE_handle *copyeng,
1221 void **per_CE_contextp,
1222 void **per_transfer_contextp,
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +05301223 qdf_dma_addr_t *bufferp,
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001224 unsigned int *nbytesp,
1225 unsigned int *transfer_idp,
1226 unsigned int *sw_idx,
1227 unsigned int *hw_idx,
1228 unsigned int *toeplitz_hash_result)
1229{
1230 struct CE_state *CE_state = (struct CE_state *)copyeng;
1231 int status;
1232
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +05301233 qdf_spin_lock_bh(&CE_state->ce_index_lock);
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001234 status =
1235 ce_completed_send_next_nolock(CE_state, per_CE_contextp,
1236 per_transfer_contextp, bufferp,
1237 nbytesp, transfer_idp, sw_idx,
1238 hw_idx, toeplitz_hash_result);
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +05301239 qdf_spin_unlock_bh(&CE_state->ce_index_lock);
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001240
1241 return status;
1242}
1243
1244#ifdef ATH_11AC_TXCOMPACT
1245/* CE engine descriptor reap
1246 * Similar to ce_per_engine_service , Only difference is ce_per_engine_service
1247 * does recieve and reaping of completed descriptor ,
1248 * This function only handles reaping of Tx complete descriptor.
1249 * The Function is called from threshold reap poll routine
1250 * hif_send_complete_check so should not countain recieve functionality
1251 * within it .
1252 */
1253
Komal Seelam644263d2016-02-22 20:45:49 +05301254void ce_per_engine_servicereap(struct hif_softc *scn, unsigned int ce_id)
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001255{
1256 void *CE_context;
1257 void *transfer_context;
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +05301258 qdf_dma_addr_t buf;
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001259 unsigned int nbytes;
1260 unsigned int id;
1261 unsigned int sw_idx, hw_idx;
1262 uint32_t toeplitz_hash_result;
Houston Hoffmana575ec22015-12-14 16:35:15 -08001263 struct CE_state *CE_state = scn->ce_id_to_state[ce_id];
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001264
Houston Hoffmanbac94542016-03-14 21:11:59 -07001265 if (Q_TARGET_ACCESS_BEGIN(scn) < 0)
1266 return;
1267
Komal Seelambd7c51d2016-02-24 10:27:30 +05301268 hif_record_ce_desc_event(scn, ce_id, HIF_CE_REAP_ENTRY,
Houston Hoffmana575ec22015-12-14 16:35:15 -08001269 NULL, NULL, 0);
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001270
1271 /* Since this function is called from both user context and
1272 * tasklet context the spinlock has to lock the bottom halves.
1273 * This fix assumes that ATH_11AC_TXCOMPACT flag is always
1274 * enabled in TX polling mode. If this is not the case, more
1275 * bottom halve spin lock changes are needed. Due to data path
1276 * performance concern, after internal discussion we've decided
1277 * to make minimum change, i.e., only address the issue occured
1278 * in this function. The possible negative effect of this minimum
1279 * change is that, in the future, if some other function will also
1280 * be opened to let the user context to use, those cases need to be
1281 * addressed by change spin_lock to spin_lock_bh also.
1282 */
1283
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +05301284 qdf_spin_lock_bh(&CE_state->ce_index_lock);
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001285
1286 if (CE_state->send_cb) {
1287 {
1288 /* Pop completed send buffers and call the
1289 * registered send callback for each
1290 */
1291 while (ce_completed_send_next_nolock
1292 (CE_state, &CE_context,
1293 &transfer_context, &buf,
1294 &nbytes, &id, &sw_idx, &hw_idx,
1295 &toeplitz_hash_result) ==
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +05301296 QDF_STATUS_SUCCESS) {
Houston Hoffmana575ec22015-12-14 16:35:15 -08001297 if (ce_id != CE_HTT_H2T_MSG) {
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +05301298 qdf_spin_unlock_bh(
Houston Hoffman44b7e4a2015-09-03 17:01:22 -07001299 &CE_state->ce_index_lock);
1300 CE_state->send_cb(
1301 (struct CE_handle *)
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001302 CE_state, CE_context,
1303 transfer_context, buf,
1304 nbytes, id, sw_idx, hw_idx,
1305 toeplitz_hash_result);
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +05301306 qdf_spin_lock_bh(
Houston Hoffman44b7e4a2015-09-03 17:01:22 -07001307 &CE_state->ce_index_lock);
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001308 } else {
1309 struct HIF_CE_pipe_info *pipe_info =
1310 (struct HIF_CE_pipe_info *)
1311 CE_context;
1312
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +05301313 qdf_spin_lock_bh(&pipe_info->
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001314 completion_freeq_lock);
1315 pipe_info->num_sends_allowed++;
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +05301316 qdf_spin_unlock_bh(&pipe_info->
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001317 completion_freeq_lock);
1318 }
1319 }
1320 }
1321 }
1322
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +05301323 qdf_spin_unlock_bh(&CE_state->ce_index_lock);
Houston Hoffmana575ec22015-12-14 16:35:15 -08001324
Komal Seelambd7c51d2016-02-24 10:27:30 +05301325 hif_record_ce_desc_event(scn, ce_id, HIF_CE_REAP_EXIT,
Houston Hoffmana575ec22015-12-14 16:35:15 -08001326 NULL, NULL, 0);
Houston Hoffmanbac94542016-03-14 21:11:59 -07001327 Q_TARGET_ACCESS_END(scn);
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001328}
1329
1330#endif /*ATH_11AC_TXCOMPACT */
1331
Manjunathappa Prakash4a9c3a82016-04-14 01:12:14 -07001332/*
1333 * Number of times to check for any pending tx/rx completion on
1334 * a copy engine, this count should be big enough. Once we hit
1335 * this threashold we'll not check for any Tx/Rx comlpetion in same
1336 * interrupt handling. Note that this threashold is only used for
1337 * Rx interrupt processing, this can be used tor Tx as well if we
1338 * suspect any infinite loop in checking for pending Tx completion.
Manjunathappa Prakash7399f142016-04-13 23:38:16 -07001339 */
Manjunathappa Prakash4a9c3a82016-04-14 01:12:14 -07001340#define CE_TXRX_COMP_CHECK_THRESHOLD 20
Manjunathappa Prakash7399f142016-04-13 23:38:16 -07001341
Manjunathappa Prakash4a9c3a82016-04-14 01:12:14 -07001342#ifdef WLAN_FEATURE_FASTPATH
Manjunathappa Prakash7399f142016-04-13 23:38:16 -07001343/**
1344 * ce_fastpath_rx_handle() - Updates write_index and calls fastpath msg handler
1345 * @ce_state: handle to copy engine state
1346 * @cmpl_msdus: Rx msdus
1347 * @num_cmpls: number of Rx msdus
1348 * @ctrl_addr: CE control address
1349 *
1350 * Return: None
1351 */
1352static void ce_fastpath_rx_handle(struct CE_state *ce_state,
1353 qdf_nbuf_t *cmpl_msdus, uint32_t num_cmpls,
1354 uint32_t ctrl_addr)
1355{
1356 struct hif_softc *scn = ce_state->scn;
1357 struct CE_ring_state *dest_ring = ce_state->dest_ring;
Manjunathappa Prakash7399f142016-04-13 23:38:16 -07001358 uint32_t nentries_mask = dest_ring->nentries_mask;
Manjunathappa Prakash7399f142016-04-13 23:38:16 -07001359 uint32_t write_index;
1360
Manjunathappa Prakash4a9c3a82016-04-14 01:12:14 -07001361 (ce_state->fastpath_handler)(ce_state->context, cmpl_msdus, num_cmpls);
Manjunathappa Prakash7399f142016-04-13 23:38:16 -07001362
1363 /* Update Destination Ring Write Index */
1364 write_index = dest_ring->write_index;
1365 write_index = CE_RING_IDX_ADD(nentries_mask, write_index, num_cmpls);
Houston Hoffmanfa260aa2016-04-26 16:14:13 -07001366
1367 hif_record_ce_desc_event(scn, ce_state->id,
1368 FAST_RX_WRITE_INDEX_UPDATE,
1369 NULL, NULL, write_index);
1370
Manjunathappa Prakash7399f142016-04-13 23:38:16 -07001371 CE_DEST_RING_WRITE_IDX_SET(scn, ctrl_addr, write_index);
1372 dest_ring->write_index = write_index;
Manjunathappa Prakash7399f142016-04-13 23:38:16 -07001373}
1374
Manjunathappa Prakash4a9c3a82016-04-14 01:12:14 -07001375#define MSG_FLUSH_NUM 6
Manjunathappa Prakash7399f142016-04-13 23:38:16 -07001376/**
1377 * ce_per_engine_service_fast() - CE handler routine to service fastpath messages
1378 * @scn: hif_context
Manjunathappa Prakash4a9c3a82016-04-14 01:12:14 -07001379 * @ce_id: Copy engine ID
Manjunathappa Prakash7399f142016-04-13 23:38:16 -07001380 * 1) Go through the CE ring, and find the completions
1381 * 2) For valid completions retrieve context (nbuf) for per_transfer_context[]
1382 * 3) Unmap buffer & accumulate in an array.
1383 * 4) Call message handler when array is full or when exiting the handler
1384 *
1385 * Return: void
1386 */
1387
Manjunathappa Prakash4a9c3a82016-04-14 01:12:14 -07001388static void ce_per_engine_service_fast(struct hif_softc *scn, int ce_id)
Manjunathappa Prakash7399f142016-04-13 23:38:16 -07001389{
1390 struct CE_state *ce_state = scn->ce_id_to_state[ce_id];
1391 struct CE_ring_state *dest_ring = ce_state->dest_ring;
1392 struct CE_dest_desc *dest_ring_base =
1393 (struct CE_dest_desc *)dest_ring->base_addr_owner_space;
1394
1395 uint32_t nentries_mask = dest_ring->nentries_mask;
1396 uint32_t sw_index = dest_ring->sw_index;
1397 uint32_t nbytes;
1398 qdf_nbuf_t nbuf;
1399 uint32_t paddr_lo;
1400 struct CE_dest_desc *dest_desc;
1401 uint32_t ce_int_status = (1 << ce_id);
1402 qdf_nbuf_t cmpl_msdus[MSG_FLUSH_NUM];
1403 uint32_t ctrl_addr = ce_state->ctrl_addr;
1404 uint32_t nbuf_cmpl_idx = 0;
Manjunathappa Prakash4a9c3a82016-04-14 01:12:14 -07001405 unsigned int more_comp_cnt = 0;
Manjunathappa Prakash7399f142016-04-13 23:38:16 -07001406
1407more_data:
1408 if (ce_int_status == (1 << ce_id)) {
1409 for (;;) {
1410
1411 dest_desc = CE_DEST_RING_TO_DESC(dest_ring_base,
1412 sw_index);
1413
1414 /*
1415 * The following 2 reads are from non-cached memory
1416 */
1417 nbytes = dest_desc->nbytes;
1418
1419 /* If completion is invalid, break */
1420 if (qdf_unlikely(nbytes == 0))
1421 break;
1422
1423
1424 /*
1425 * Build the nbuf list from valid completions
1426 */
1427 nbuf = dest_ring->per_transfer_context[sw_index];
1428
1429 /*
1430 * No lock is needed here, since this is the only thread
1431 * that accesses the sw_index
1432 */
1433 sw_index = CE_RING_IDX_INCR(nentries_mask, sw_index);
1434
1435 /*
1436 * CAREFUL : Uncached write, but still less expensive,
1437 * since most modern caches use "write-combining" to
1438 * flush multiple cache-writes all at once.
1439 */
1440 dest_desc->nbytes = 0;
1441
1442 /*
1443 * Per our understanding this is not required on our
1444 * since we are doing the same cache invalidation
1445 * operation on the same buffer twice in succession,
1446 * without any modifiication to this buffer by CPU in
1447 * between.
1448 * However, this code with 2 syncs in succession has
1449 * been undergoing some testing at a customer site,
1450 * and seemed to be showing no problems so far. Would
1451 * like to validate from the customer, that this line
1452 * is really not required, before we remove this line
1453 * completely.
1454 */
1455 paddr_lo = QDF_NBUF_CB_PADDR(nbuf);
1456
Manjunathappa Prakash4a9c3a82016-04-14 01:12:14 -07001457 qdf_mem_dma_sync_single_for_cpu(scn->qdf_dev,
1458 paddr_lo,
1459 (skb_end_pointer(nbuf) - (nbuf)->data),
1460 DMA_FROM_DEVICE);
Manjunathappa Prakash7399f142016-04-13 23:38:16 -07001461 qdf_nbuf_put_tail(nbuf, nbytes);
1462
1463 qdf_assert_always(nbuf->data != NULL);
1464
1465 cmpl_msdus[nbuf_cmpl_idx++] = nbuf;
1466
1467 /*
1468 * we are not posting the buffers back instead
1469 * reusing the buffers
1470 */
1471 if (nbuf_cmpl_idx == MSG_FLUSH_NUM) {
Houston Hoffmanfa260aa2016-04-26 16:14:13 -07001472 hif_record_ce_desc_event(scn, ce_state->id,
1473 FAST_RX_SOFTWARE_INDEX_UPDATE,
1474 NULL, NULL, sw_index);
1475 dest_ring->sw_index = sw_index;
1476
Manjunathappa Prakash7399f142016-04-13 23:38:16 -07001477 qdf_spin_unlock(&ce_state->ce_index_lock);
1478 ce_fastpath_rx_handle(ce_state, cmpl_msdus,
1479 MSG_FLUSH_NUM, ctrl_addr);
1480 qdf_spin_lock(&ce_state->ce_index_lock);
1481 nbuf_cmpl_idx = 0;
1482 }
1483
1484 }
1485
Houston Hoffmanfa260aa2016-04-26 16:14:13 -07001486 hif_record_ce_desc_event(scn, ce_state->id,
1487 FAST_RX_SOFTWARE_INDEX_UPDATE,
1488 NULL, NULL, sw_index);
1489
1490 dest_ring->sw_index = sw_index;
1491
Manjunathappa Prakash7399f142016-04-13 23:38:16 -07001492 /*
1493 * If there are not enough completions to fill the array,
1494 * just call the message handler here
1495 */
1496 if (nbuf_cmpl_idx) {
1497 qdf_spin_unlock(&ce_state->ce_index_lock);
1498 ce_fastpath_rx_handle(ce_state, cmpl_msdus,
1499 nbuf_cmpl_idx, ctrl_addr);
1500 qdf_spin_lock(&ce_state->ce_index_lock);
1501 nbuf_cmpl_idx = 0;
1502 }
1503 qdf_atomic_set(&ce_state->rx_pending, 0);
Manjunathappa Prakash7399f142016-04-13 23:38:16 -07001504 CE_ENGINE_INT_STATUS_CLEAR(scn, ctrl_addr,
1505 HOST_IS_COPY_COMPLETE_MASK);
1506 }
Manjunathappa Prakash4a9c3a82016-04-14 01:12:14 -07001507 if (ce_recv_entries_done_nolock(scn, ce_state)) {
1508 if (more_comp_cnt++ < CE_TXRX_COMP_CHECK_THRESHOLD) {
1509 goto more_data;
1510 } else {
1511 HIF_ERROR("%s:Potential infinite loop detected during Rx processing nentries_mask:0x%x sw read_idx:0x%x hw read_idx:0x%x",
1512 __func__, nentries_mask,
1513 ce_state->dest_ring->sw_index,
1514 CE_DEST_RING_READ_IDX_GET(scn, ctrl_addr));
1515 }
1516 }
Manjunathappa Prakash7399f142016-04-13 23:38:16 -07001517}
1518
1519#else
Manjunathappa Prakash4a9c3a82016-04-14 01:12:14 -07001520static void ce_per_engine_service_fast(struct hif_softc *scn, int ce_id)
Manjunathappa Prakash7399f142016-04-13 23:38:16 -07001521{
Manjunathappa Prakash7399f142016-04-13 23:38:16 -07001522}
1523#endif /* WLAN_FEATURE_FASTPATH */
1524
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001525/*
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001526 * Guts of interrupt handler for per-engine interrupts on a particular CE.
1527 *
1528 * Invokes registered callbacks for recv_complete,
1529 * send_complete, and watermarks.
1530 *
1531 * Returns: number of messages processed
1532 */
1533
Komal Seelam644263d2016-02-22 20:45:49 +05301534int ce_per_engine_service(struct hif_softc *scn, unsigned int CE_id)
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001535{
1536 struct CE_state *CE_state = scn->ce_id_to_state[CE_id];
1537 uint32_t ctrl_addr = CE_state->ctrl_addr;
1538 void *CE_context;
1539 void *transfer_context;
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +05301540 qdf_dma_addr_t buf;
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001541 unsigned int nbytes;
1542 unsigned int id;
1543 unsigned int flags;
1544 uint32_t CE_int_status;
1545 unsigned int more_comp_cnt = 0;
1546 unsigned int more_snd_comp_cnt = 0;
1547 unsigned int sw_idx, hw_idx;
1548 uint32_t toeplitz_hash_result;
Komal Seelambd7c51d2016-02-24 10:27:30 +05301549 uint32_t mode = hif_get_conparam(scn);
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001550
1551 if (Q_TARGET_ACCESS_BEGIN(scn) < 0) {
1552 HIF_ERROR("[premature rc=0]\n");
1553 return 0; /* no work done */
1554 }
1555
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +05301556 qdf_spin_lock(&CE_state->ce_index_lock);
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001557
Manjunathappa Prakash7399f142016-04-13 23:38:16 -07001558 /*
1559 * With below check we make sure CE we are handling is datapath CE and
1560 * fastpath is enabled.
1561 */
Manjunathappa Prakash4a9c3a82016-04-14 01:12:14 -07001562 if (ce_is_fastpath_handler_registered(CE_state)) {
Manjunathappa Prakash7399f142016-04-13 23:38:16 -07001563 /* For datapath only Rx CEs */
Manjunathappa Prakash4a9c3a82016-04-14 01:12:14 -07001564 ce_per_engine_service_fast(scn, CE_id);
1565 qdf_spin_unlock(&CE_state->ce_index_lock);
1566 return CE_state->receive_count;
1567 }
Manjunathappa Prakash7399f142016-04-13 23:38:16 -07001568
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001569 /* Clear force_break flag and re-initialize receive_count to 0 */
1570
1571 /* NAPI: scn variables- thread/multi-processing safety? */
Houston Hoffman5bf441a2015-09-02 11:52:10 -07001572 CE_state->receive_count = 0;
Houston Hoffman18c7fc52015-09-02 11:44:42 -07001573 CE_state->force_break = 0;
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001574more_completions:
1575 if (CE_state->recv_cb) {
1576
1577 /* Pop completed recv buffers and call
1578 * the registered recv callback for each
1579 */
1580 while (ce_completed_recv_next_nolock
1581 (CE_state, &CE_context, &transfer_context,
1582 &buf, &nbytes, &id, &flags) ==
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +05301583 QDF_STATUS_SUCCESS) {
1584 qdf_spin_unlock(&CE_state->ce_index_lock);
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001585 CE_state->recv_cb((struct CE_handle *)CE_state,
1586 CE_context, transfer_context, buf,
1587 nbytes, id, flags);
1588
1589 /*
1590 * EV #112693 -
1591 * [Peregrine][ES1][WB342][Win8x86][Performance]
1592 * BSoD_0x133 occurred in VHT80 UDP_DL
1593 * Break out DPC by force if number of loops in
1594 * hif_pci_ce_recv_data reaches MAX_NUM_OF_RECEIVES
1595 * to avoid spending too long time in
1596 * DPC for each interrupt handling. Schedule another
1597 * DPC to avoid data loss if we had taken
1598 * force-break action before apply to Windows OS
1599 * only currently, Linux/MAC os can expand to their
1600 * platform if necessary
1601 */
1602
1603 /* Break the receive processes by
1604 * force if force_break set up
1605 */
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +05301606 if (qdf_unlikely(CE_state->force_break)) {
1607 qdf_atomic_set(&CE_state->rx_pending, 1);
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001608 if (Q_TARGET_ACCESS_END(scn) < 0)
1609 HIF_ERROR("<--[premature rc=%d]\n",
Houston Hoffman5bf441a2015-09-02 11:52:10 -07001610 CE_state->receive_count);
1611 return CE_state->receive_count;
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001612 }
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +05301613 qdf_spin_lock(&CE_state->ce_index_lock);
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001614 }
1615 }
1616
1617 /*
1618 * Attention: We may experience potential infinite loop for below
1619 * While Loop during Sending Stress test.
1620 * Resolve the same way as Receive Case (Refer to EV #112693)
1621 */
1622
1623 if (CE_state->send_cb) {
1624 /* Pop completed send buffers and call
1625 * the registered send callback for each
1626 */
1627
1628#ifdef ATH_11AC_TXCOMPACT
1629 while (ce_completed_send_next_nolock
1630 (CE_state, &CE_context,
1631 &transfer_context, &buf, &nbytes,
1632 &id, &sw_idx, &hw_idx,
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +05301633 &toeplitz_hash_result) == QDF_STATUS_SUCCESS) {
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001634
1635 if (CE_id != CE_HTT_H2T_MSG ||
Houston Hoffman75ef5a52016-04-14 17:15:49 -07001636 QDF_IS_EPPING_ENABLED(mode)) {
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +05301637 qdf_spin_unlock(&CE_state->ce_index_lock);
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001638 CE_state->send_cb((struct CE_handle *)CE_state,
1639 CE_context, transfer_context,
1640 buf, nbytes, id, sw_idx,
1641 hw_idx, toeplitz_hash_result);
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +05301642 qdf_spin_lock(&CE_state->ce_index_lock);
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001643 } else {
1644 struct HIF_CE_pipe_info *pipe_info =
1645 (struct HIF_CE_pipe_info *)CE_context;
1646
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +05301647 qdf_spin_lock(&pipe_info->
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001648 completion_freeq_lock);
1649 pipe_info->num_sends_allowed++;
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +05301650 qdf_spin_unlock(&pipe_info->
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001651 completion_freeq_lock);
1652 }
1653 }
1654#else /*ATH_11AC_TXCOMPACT */
1655 while (ce_completed_send_next_nolock
1656 (CE_state, &CE_context,
1657 &transfer_context, &buf, &nbytes,
1658 &id, &sw_idx, &hw_idx,
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +05301659 &toeplitz_hash_result) == QDF_STATUS_SUCCESS) {
1660 qdf_spin_unlock(&CE_state->ce_index_lock);
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001661 CE_state->send_cb((struct CE_handle *)CE_state,
1662 CE_context, transfer_context, buf,
1663 nbytes, id, sw_idx, hw_idx,
1664 toeplitz_hash_result);
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +05301665 qdf_spin_lock(&CE_state->ce_index_lock);
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001666 }
1667#endif /*ATH_11AC_TXCOMPACT */
1668 }
1669
1670more_watermarks:
1671 if (CE_state->misc_cbs) {
1672 CE_int_status = CE_ENGINE_INT_STATUS_GET(scn, ctrl_addr);
1673 if (CE_int_status & CE_WATERMARK_MASK) {
1674 if (CE_state->watermark_cb) {
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +05301675 qdf_spin_unlock(&CE_state->ce_index_lock);
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001676 /* Convert HW IS bits to software flags */
1677 flags =
1678 (CE_int_status & CE_WATERMARK_MASK) >>
1679 CE_WM_SHFT;
1680
1681 CE_state->
1682 watermark_cb((struct CE_handle *)CE_state,
1683 CE_state->wm_context, flags);
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +05301684 qdf_spin_lock(&CE_state->ce_index_lock);
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001685 }
1686 }
1687 }
1688
1689 /*
1690 * Clear the misc interrupts (watermark) that were handled above,
1691 * and that will be checked again below.
1692 * Clear and check for copy-complete interrupts again, just in case
1693 * more copy completions happened while the misc interrupts were being
1694 * handled.
1695 */
1696 CE_ENGINE_INT_STATUS_CLEAR(scn, ctrl_addr,
1697 CE_WATERMARK_MASK |
1698 HOST_IS_COPY_COMPLETE_MASK);
1699
1700 /*
1701 * Now that per-engine interrupts are cleared, verify that
1702 * no recv interrupts arrive while processing send interrupts,
1703 * and no recv or send interrupts happened while processing
1704 * misc interrupts.Go back and check again.Keep checking until
1705 * we find no more events to process.
1706 */
1707 if (CE_state->recv_cb && ce_recv_entries_done_nolock(scn, CE_state)) {
Houston Hoffman75ef5a52016-04-14 17:15:49 -07001708 if (QDF_IS_EPPING_ENABLED(mode) ||
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001709 more_comp_cnt++ < CE_TXRX_COMP_CHECK_THRESHOLD) {
1710 goto more_completions;
1711 } else {
1712 HIF_ERROR(
1713 "%s:Potential infinite loop detected during Rx processing nentries_mask:0x%x sw read_idx:0x%x hw read_idx:0x%x",
1714 __func__, CE_state->dest_ring->nentries_mask,
1715 CE_state->dest_ring->sw_index,
1716 CE_DEST_RING_READ_IDX_GET(scn,
1717 CE_state->ctrl_addr));
1718 }
1719 }
1720
1721 if (CE_state->send_cb && ce_send_entries_done_nolock(scn, CE_state)) {
Houston Hoffman75ef5a52016-04-14 17:15:49 -07001722 if (QDF_IS_EPPING_ENABLED(mode) ||
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001723 more_snd_comp_cnt++ < CE_TXRX_COMP_CHECK_THRESHOLD) {
1724 goto more_completions;
1725 } else {
1726 HIF_ERROR(
1727 "%s:Potential infinite loop detected during send completion nentries_mask:0x%x sw read_idx:0x%x hw read_idx:0x%x",
1728 __func__, CE_state->src_ring->nentries_mask,
1729 CE_state->src_ring->sw_index,
1730 CE_SRC_RING_READ_IDX_GET(scn,
1731 CE_state->ctrl_addr));
1732 }
1733 }
1734
1735 if (CE_state->misc_cbs) {
1736 CE_int_status = CE_ENGINE_INT_STATUS_GET(scn, ctrl_addr);
1737 if (CE_int_status & CE_WATERMARK_MASK) {
1738 if (CE_state->watermark_cb) {
1739 goto more_watermarks;
1740 }
1741 }
1742 }
1743
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +05301744 qdf_spin_unlock(&CE_state->ce_index_lock);
1745 qdf_atomic_set(&CE_state->rx_pending, 0);
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001746
1747 if (Q_TARGET_ACCESS_END(scn) < 0)
Houston Hoffman5bf441a2015-09-02 11:52:10 -07001748 HIF_ERROR("<--[premature rc=%d]\n", CE_state->receive_count);
1749 return CE_state->receive_count;
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001750}
1751
1752/*
1753 * Handler for per-engine interrupts on ALL active CEs.
1754 * This is used in cases where the system is sharing a
1755 * single interrput for all CEs
1756 */
1757
Komal Seelam644263d2016-02-22 20:45:49 +05301758void ce_per_engine_service_any(int irq, struct hif_softc *scn)
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001759{
1760 int CE_id;
1761 uint32_t intr_summary;
1762
Houston Hoffmanbac94542016-03-14 21:11:59 -07001763 if (Q_TARGET_ACCESS_BEGIN(scn) < 0)
1764 return;
1765
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +05301766 if (!qdf_atomic_read(&scn->tasklet_from_intr)) {
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001767 for (CE_id = 0; CE_id < scn->ce_count; CE_id++) {
1768 struct CE_state *CE_state = scn->ce_id_to_state[CE_id];
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +05301769 if (qdf_atomic_read(&CE_state->rx_pending)) {
1770 qdf_atomic_set(&CE_state->rx_pending, 0);
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001771 ce_per_engine_service(scn, CE_id);
1772 }
1773 }
1774
Houston Hoffmanbac94542016-03-14 21:11:59 -07001775 Q_TARGET_ACCESS_END(scn);
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001776 return;
1777 }
1778
1779 intr_summary = CE_INTERRUPT_SUMMARY(scn);
1780
1781 for (CE_id = 0; intr_summary && (CE_id < scn->ce_count); CE_id++) {
1782 if (intr_summary & (1 << CE_id)) {
1783 intr_summary &= ~(1 << CE_id);
1784 } else {
1785 continue; /* no intr pending on this CE */
1786 }
1787
1788 ce_per_engine_service(scn, CE_id);
1789 }
1790
Houston Hoffmanbac94542016-03-14 21:11:59 -07001791 Q_TARGET_ACCESS_END(scn);
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001792}
1793
1794/*
1795 * Adjust interrupts for the copy complete handler.
1796 * If it's needed for either send or recv, then unmask
1797 * this interrupt; otherwise, mask it.
1798 *
1799 * Called with target_lock held.
1800 */
1801static void
1802ce_per_engine_handler_adjust(struct CE_state *CE_state,
1803 int disable_copy_compl_intr)
1804{
1805 uint32_t ctrl_addr = CE_state->ctrl_addr;
Komal Seelam644263d2016-02-22 20:45:49 +05301806 struct hif_softc *scn = CE_state->scn;
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001807
1808 CE_state->disable_copy_compl_intr = disable_copy_compl_intr;
Houston Hoffmanbac94542016-03-14 21:11:59 -07001809
1810 if (Q_TARGET_ACCESS_BEGIN(scn) < 0)
1811 return;
1812
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001813 if ((!disable_copy_compl_intr) &&
1814 (CE_state->send_cb || CE_state->recv_cb)) {
1815 CE_COPY_COMPLETE_INTR_ENABLE(scn, ctrl_addr);
1816 } else {
1817 CE_COPY_COMPLETE_INTR_DISABLE(scn, ctrl_addr);
1818 }
1819
1820 if (CE_state->watermark_cb) {
1821 CE_WATERMARK_INTR_ENABLE(scn, ctrl_addr);
1822 } else {
1823 CE_WATERMARK_INTR_DISABLE(scn, ctrl_addr);
1824 }
Houston Hoffmanbac94542016-03-14 21:11:59 -07001825 Q_TARGET_ACCESS_END(scn);
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001826}
1827
1828/*Iterate the CE_state list and disable the compl interrupt
1829 * if it has been registered already.
1830 */
Komal Seelam644263d2016-02-22 20:45:49 +05301831void ce_disable_any_copy_compl_intr_nolock(struct hif_softc *scn)
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001832{
1833 int CE_id;
1834
Houston Hoffmanbac94542016-03-14 21:11:59 -07001835 if (Q_TARGET_ACCESS_BEGIN(scn) < 0)
1836 return;
1837
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001838 for (CE_id = 0; CE_id < scn->ce_count; CE_id++) {
1839 struct CE_state *CE_state = scn->ce_id_to_state[CE_id];
1840 uint32_t ctrl_addr = CE_state->ctrl_addr;
1841
1842 /* if the interrupt is currently enabled, disable it */
1843 if (!CE_state->disable_copy_compl_intr
1844 && (CE_state->send_cb || CE_state->recv_cb)) {
1845 CE_COPY_COMPLETE_INTR_DISABLE(scn, ctrl_addr);
1846 }
1847
1848 if (CE_state->watermark_cb) {
1849 CE_WATERMARK_INTR_DISABLE(scn, ctrl_addr);
1850 }
1851 }
Houston Hoffmanbac94542016-03-14 21:11:59 -07001852 Q_TARGET_ACCESS_END(scn);
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001853}
1854
Komal Seelam644263d2016-02-22 20:45:49 +05301855void ce_enable_any_copy_compl_intr_nolock(struct hif_softc *scn)
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001856{
1857 int CE_id;
1858
Houston Hoffmanbac94542016-03-14 21:11:59 -07001859 if (Q_TARGET_ACCESS_BEGIN(scn) < 0)
1860 return;
1861
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001862 for (CE_id = 0; CE_id < scn->ce_count; CE_id++) {
1863 struct CE_state *CE_state = scn->ce_id_to_state[CE_id];
1864 uint32_t ctrl_addr = CE_state->ctrl_addr;
1865
1866 /*
1867 * If the CE is supposed to have copy complete interrupts
1868 * enabled (i.e. there a callback registered, and the
1869 * "disable" flag is not set), then re-enable the interrupt.
1870 */
1871 if (!CE_state->disable_copy_compl_intr
1872 && (CE_state->send_cb || CE_state->recv_cb)) {
1873 CE_COPY_COMPLETE_INTR_ENABLE(scn, ctrl_addr);
1874 }
1875
1876 if (CE_state->watermark_cb) {
1877 CE_WATERMARK_INTR_ENABLE(scn, ctrl_addr);
1878 }
1879 }
Houston Hoffmanbac94542016-03-14 21:11:59 -07001880 Q_TARGET_ACCESS_END(scn);
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001881}
1882
Houston Hoffmana837c9a2015-09-03 12:47:01 -07001883/**
1884 * ce_send_cb_register(): register completion handler
1885 * @copyeng: CE_state representing the ce we are adding the behavior to
1886 * @fn_ptr: callback that the ce should use when processing tx completions
1887 * @disable_interrupts: if the interupts should be enabled or not.
1888 *
1889 * Caller should guarantee that no transactions are in progress before
1890 * switching the callback function.
1891 *
1892 * Registers the send context before the fn pointer so that if the cb is valid
1893 * the context should be valid.
1894 *
1895 * Beware that currently this function will enable completion interrupts.
1896 */
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001897void
1898ce_send_cb_register(struct CE_handle *copyeng,
1899 ce_send_cb fn_ptr,
1900 void *ce_send_context, int disable_interrupts)
1901{
1902 struct CE_state *CE_state = (struct CE_state *)copyeng;
1903
Sanjay Devnani9ce15772015-11-12 14:08:57 -08001904 if (CE_state == NULL) {
1905 pr_err("%s: Error CE state = NULL\n", __func__);
1906 return;
1907 }
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001908 CE_state->send_context = ce_send_context;
Houston Hoffmana837c9a2015-09-03 12:47:01 -07001909 CE_state->send_cb = fn_ptr;
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001910 ce_per_engine_handler_adjust(CE_state, disable_interrupts);
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001911}
1912
Houston Hoffmana837c9a2015-09-03 12:47:01 -07001913/**
1914 * ce_recv_cb_register(): register completion handler
1915 * @copyeng: CE_state representing the ce we are adding the behavior to
1916 * @fn_ptr: callback that the ce should use when processing rx completions
1917 * @disable_interrupts: if the interupts should be enabled or not.
1918 *
1919 * Registers the send context before the fn pointer so that if the cb is valid
1920 * the context should be valid.
1921 *
1922 * Caller should guarantee that no transactions are in progress before
1923 * switching the callback function.
1924 */
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001925void
1926ce_recv_cb_register(struct CE_handle *copyeng,
1927 CE_recv_cb fn_ptr,
1928 void *CE_recv_context, int disable_interrupts)
1929{
1930 struct CE_state *CE_state = (struct CE_state *)copyeng;
1931
Sanjay Devnani9ce15772015-11-12 14:08:57 -08001932 if (CE_state == NULL) {
1933 pr_err("%s: ERROR CE state = NULL\n", __func__);
1934 return;
1935 }
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001936 CE_state->recv_context = CE_recv_context;
Houston Hoffmana837c9a2015-09-03 12:47:01 -07001937 CE_state->recv_cb = fn_ptr;
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001938 ce_per_engine_handler_adjust(CE_state, disable_interrupts);
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001939}
1940
Houston Hoffmana837c9a2015-09-03 12:47:01 -07001941/**
1942 * ce_watermark_cb_register(): register completion handler
1943 * @copyeng: CE_state representing the ce we are adding the behavior to
1944 * @fn_ptr: callback that the ce should use when processing watermark events
1945 *
1946 * Caller should guarantee that no watermark events are being processed before
1947 * switching the callback function.
1948 */
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001949void
1950ce_watermark_cb_register(struct CE_handle *copyeng,
1951 CE_watermark_cb fn_ptr, void *CE_wm_context)
1952{
1953 struct CE_state *CE_state = (struct CE_state *)copyeng;
1954
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001955 CE_state->watermark_cb = fn_ptr;
1956 CE_state->wm_context = CE_wm_context;
1957 ce_per_engine_handler_adjust(CE_state, 0);
1958 if (fn_ptr) {
1959 CE_state->misc_cbs = 1;
1960 }
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001961}
1962
1963#ifdef WLAN_FEATURE_FASTPATH
1964/**
1965 * ce_pkt_dl_len_set() set the HTT packet download length
1966 * @hif_sc: HIF context
1967 * @pkt_download_len: download length
1968 *
1969 * Return: None
1970 */
1971void ce_pkt_dl_len_set(void *hif_sc, u_int32_t pkt_download_len)
1972{
Komal Seelam644263d2016-02-22 20:45:49 +05301973 struct hif_softc *sc = (struct hif_softc *)(hif_sc);
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001974 struct CE_state *ce_state = sc->ce_id_to_state[CE_HTT_H2T_MSG];
1975
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +05301976 qdf_assert_always(ce_state);
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001977
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001978 ce_state->download_len = pkt_download_len;
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001979
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +05301980 qdf_print("%s CE %d Pkt download length %d", __func__,
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001981 ce_state->id, ce_state->download_len);
1982}
1983#else
1984void ce_pkt_dl_len_set(void *hif_sc, u_int32_t pkt_download_len)
1985{
1986}
1987#endif /* WLAN_FEATURE_FASTPATH */
1988
Komal Seelam644263d2016-02-22 20:45:49 +05301989bool ce_get_rx_pending(struct hif_softc *scn)
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001990{
1991 int CE_id;
1992
1993 for (CE_id = 0; CE_id < scn->ce_count; CE_id++) {
1994 struct CE_state *CE_state = scn->ce_id_to_state[CE_id];
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +05301995 if (qdf_atomic_read(&CE_state->rx_pending))
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08001996 return true;
1997 }
1998
1999 return false;
2000}
2001
2002/**
2003 * ce_check_rx_pending() - ce_check_rx_pending
Houston Hoffmaneb2516c2016-04-01 12:53:50 -07002004 * @CE_state: context of the copy engine to check
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08002005 *
Houston Hoffmaneb2516c2016-04-01 12:53:50 -07002006 * Return: true if there per_engine_service
2007 * didn't process all the rx descriptors.
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08002008 */
Houston Hoffmaneb2516c2016-04-01 12:53:50 -07002009bool ce_check_rx_pending(struct CE_state *CE_state)
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08002010{
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +05302011 if (qdf_atomic_read(&CE_state->rx_pending))
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08002012 return true;
2013 else
2014 return false;
2015}
Houston Hoffman8ed92e52015-09-02 14:49:48 -07002016
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08002017#ifdef IPA_OFFLOAD
Leo Changd85f78d2015-11-13 10:55:34 -08002018/**
2019 * ce_ipa_get_resource() - get uc resource on copyengine
2020 * @ce: copyengine context
2021 * @ce_sr_base_paddr: copyengine source ring base physical address
2022 * @ce_sr_ring_size: copyengine source ring size
2023 * @ce_reg_paddr: copyengine register physical address
2024 *
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08002025 * Copy engine should release resource to micro controller
2026 * Micro controller needs
Leo Changd85f78d2015-11-13 10:55:34 -08002027 * - Copy engine source descriptor base address
2028 * - Copy engine source descriptor size
2029 * - PCI BAR address to access copy engine regiser
2030 *
2031 * Return: None
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08002032 */
2033void ce_ipa_get_resource(struct CE_handle *ce,
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +05302034 qdf_dma_addr_t *ce_sr_base_paddr,
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08002035 uint32_t *ce_sr_ring_size,
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +05302036 qdf_dma_addr_t *ce_reg_paddr)
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08002037{
2038 struct CE_state *CE_state = (struct CE_state *)ce;
2039 uint32_t ring_loop;
2040 struct CE_src_desc *ce_desc;
Chouhan, Anuragfc06aa92016-03-03 19:05:05 +05302041 qdf_dma_addr_t phy_mem_base;
Komal Seelam644263d2016-02-22 20:45:49 +05302042 struct hif_softc *scn = CE_state->scn;
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08002043
2044 if (CE_RUNNING != CE_state->state) {
2045 *ce_sr_base_paddr = 0;
2046 *ce_sr_ring_size = 0;
2047 return;
2048 }
2049
2050 /* Update default value for descriptor */
2051 for (ring_loop = 0; ring_loop < CE_state->src_ring->nentries;
2052 ring_loop++) {
2053 ce_desc = (struct CE_src_desc *)
2054 ((char *)CE_state->src_ring->base_addr_owner_space +
2055 ring_loop * (sizeof(struct CE_src_desc)));
2056 CE_IPA_RING_INIT(ce_desc);
2057 }
2058
2059 /* Get BAR address */
2060 hif_read_phy_mem_base(CE_state->scn, &phy_mem_base);
2061
Leo Changd85f78d2015-11-13 10:55:34 -08002062 *ce_sr_base_paddr = CE_state->src_ring->base_addr_CE_space;
2063 *ce_sr_ring_size = (uint32_t) (CE_state->src_ring->nentries *
2064 sizeof(struct CE_src_desc));
Prakash Dhavalid5c9f1c2015-11-08 19:04:44 -08002065 *ce_reg_paddr = phy_mem_base + CE_BASE_ADDRESS(CE_state->id) +
2066 SR_WR_INDEX_ADDRESS;
2067 return;
2068}
2069#endif /* IPA_OFFLOAD */
2070