Linux Audio

Check our new training course

Loading...
v6.8
   1// SPDX-License-Identifier: ISC
   2/*
   3 * Copyright (c) 2005-2011 Atheros Communications Inc.
   4 * Copyright (c) 2011-2017 Qualcomm Atheros, Inc.
   5 * Copyright (c) 2018 The Linux Foundation. All rights reserved.
   6 * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
   7 */
   8
   9#include "hif.h"
  10#include "ce.h"
  11#include "debug.h"
  12
  13/*
  14 * Support for Copy Engine hardware, which is mainly used for
  15 * communication between Host and Target over a PCIe interconnect.
  16 */
  17
  18/*
  19 * A single CopyEngine (CE) comprises two "rings":
  20 *   a source ring
  21 *   a destination ring
  22 *
  23 * Each ring consists of a number of descriptors which specify
  24 * an address, length, and meta-data.
  25 *
  26 * Typically, one side of the PCIe/AHB/SNOC interconnect (Host or Target)
  27 * controls one ring and the other side controls the other ring.
  28 * The source side chooses when to initiate a transfer and it
  29 * chooses what to send (buffer address, length). The destination
  30 * side keeps a supply of "anonymous receive buffers" available and
  31 * it handles incoming data as it arrives (when the destination
  32 * receives an interrupt).
  33 *
  34 * The sender may send a simple buffer (address/length) or it may
  35 * send a small list of buffers.  When a small list is sent, hardware
  36 * "gathers" these and they end up in a single destination buffer
  37 * with a single interrupt.
  38 *
  39 * There are several "contexts" managed by this layer -- more, it
  40 * may seem -- than should be needed. These are provided mainly for
  41 * maximum flexibility and especially to facilitate a simpler HIF
  42 * implementation. There are per-CopyEngine recv, send, and watermark
  43 * contexts. These are supplied by the caller when a recv, send,
  44 * or watermark handler is established and they are echoed back to
  45 * the caller when the respective callbacks are invoked. There is
  46 * also a per-transfer context supplied by the caller when a buffer
  47 * (or sendlist) is sent and when a buffer is enqueued for recv.
  48 * These per-transfer contexts are echoed back to the caller when
  49 * the buffer is sent/received.
  50 */
  51
  52static inline u32 shadow_sr_wr_ind_addr(struct ath10k *ar,
  53					struct ath10k_ce_pipe *ce_state)
  54{
  55	u32 ce_id = ce_state->id;
  56	u32 addr = 0;
  57
  58	switch (ce_id) {
  59	case 0:
  60		addr = 0x00032000;
  61		break;
  62	case 3:
  63		addr = 0x0003200C;
  64		break;
  65	case 4:
  66		addr = 0x00032010;
  67		break;
  68	case 5:
  69		addr = 0x00032014;
  70		break;
  71	case 7:
  72		addr = 0x0003201C;
  73		break;
  74	default:
  75		ath10k_warn(ar, "invalid CE id: %d", ce_id);
  76		break;
  77	}
  78	return addr;
  79}
  80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  81static inline unsigned int
  82ath10k_set_ring_byte(unsigned int offset,
  83		     struct ath10k_hw_ce_regs_addr_map *addr_map)
  84{
  85	return ((offset << addr_map->lsb) & addr_map->mask);
  86}
  87
 
 
 
 
 
 
 
  88static inline u32 ath10k_ce_read32(struct ath10k *ar, u32 offset)
  89{
  90	struct ath10k_ce *ce = ath10k_ce_priv(ar);
  91
  92	return ce->bus_ops->read32(ar, offset);
  93}
  94
  95static inline void ath10k_ce_write32(struct ath10k *ar, u32 offset, u32 value)
  96{
  97	struct ath10k_ce *ce = ath10k_ce_priv(ar);
  98
  99	ce->bus_ops->write32(ar, offset, value);
 100}
 101
 102static inline void ath10k_ce_dest_ring_write_index_set(struct ath10k *ar,
 103						       u32 ce_ctrl_addr,
 104						       unsigned int n)
 105{
 106	ath10k_ce_write32(ar, ce_ctrl_addr +
 107			  ar->hw_ce_regs->dst_wr_index_addr, n);
 108}
 109
 110static inline u32 ath10k_ce_dest_ring_write_index_get(struct ath10k *ar,
 111						      u32 ce_ctrl_addr)
 112{
 113	return ath10k_ce_read32(ar, ce_ctrl_addr +
 114				ar->hw_ce_regs->dst_wr_index_addr);
 115}
 116
 117static inline void ath10k_ce_src_ring_write_index_set(struct ath10k *ar,
 118						      u32 ce_ctrl_addr,
 119						      unsigned int n)
 120{
 121	ath10k_ce_write32(ar, ce_ctrl_addr +
 122			  ar->hw_ce_regs->sr_wr_index_addr, n);
 123}
 124
 125static inline u32 ath10k_ce_src_ring_write_index_get(struct ath10k *ar,
 126						     u32 ce_ctrl_addr)
 127{
 128	return ath10k_ce_read32(ar, ce_ctrl_addr +
 129				ar->hw_ce_regs->sr_wr_index_addr);
 130}
 131
 132static inline u32 ath10k_ce_src_ring_read_index_from_ddr(struct ath10k *ar,
 133							 u32 ce_id)
 134{
 135	struct ath10k_ce *ce = ath10k_ce_priv(ar);
 136
 137	return ce->vaddr_rri[ce_id] & CE_DDR_RRI_MASK;
 138}
 139
 140static inline u32 ath10k_ce_src_ring_read_index_get(struct ath10k *ar,
 141						    u32 ce_ctrl_addr)
 142{
 143	struct ath10k_ce *ce = ath10k_ce_priv(ar);
 144	u32 ce_id = COPY_ENGINE_ID(ce_ctrl_addr);
 145	struct ath10k_ce_pipe *ce_state = &ce->ce_states[ce_id];
 146	u32 index;
 147
 148	if (ar->hw_params.rri_on_ddr &&
 149	    (ce_state->attr_flags & CE_ATTR_DIS_INTR))
 150		index = ath10k_ce_src_ring_read_index_from_ddr(ar, ce_id);
 151	else
 152		index = ath10k_ce_read32(ar, ce_ctrl_addr +
 153					 ar->hw_ce_regs->current_srri_addr);
 154
 155	return index;
 156}
 157
 158static inline void
 159ath10k_ce_shadow_src_ring_write_index_set(struct ath10k *ar,
 160					  struct ath10k_ce_pipe *ce_state,
 161					  unsigned int value)
 162{
 163	ath10k_ce_write32(ar, shadow_sr_wr_ind_addr(ar, ce_state), value);
 164}
 165
 
 
 
 
 
 
 
 
 166static inline void ath10k_ce_src_ring_base_addr_set(struct ath10k *ar,
 167						    u32 ce_id,
 168						    u64 addr)
 169{
 170	struct ath10k_ce *ce = ath10k_ce_priv(ar);
 171	struct ath10k_ce_pipe *ce_state = &ce->ce_states[ce_id];
 172	u32 ce_ctrl_addr = ath10k_ce_base_address(ar, ce_id);
 173	u32 addr_lo = lower_32_bits(addr);
 174
 175	ath10k_ce_write32(ar, ce_ctrl_addr +
 176			  ar->hw_ce_regs->sr_base_addr_lo, addr_lo);
 177
 178	if (ce_state->ops->ce_set_src_ring_base_addr_hi) {
 179		ce_state->ops->ce_set_src_ring_base_addr_hi(ar, ce_ctrl_addr,
 180							    addr);
 181	}
 182}
 183
 184static void ath10k_ce_set_src_ring_base_addr_hi(struct ath10k *ar,
 185						u32 ce_ctrl_addr,
 186						u64 addr)
 187{
 188	u32 addr_hi = upper_32_bits(addr) & CE_DESC_ADDR_HI_MASK;
 189
 190	ath10k_ce_write32(ar, ce_ctrl_addr +
 191			  ar->hw_ce_regs->sr_base_addr_hi, addr_hi);
 192}
 193
 194static inline void ath10k_ce_src_ring_size_set(struct ath10k *ar,
 195					       u32 ce_ctrl_addr,
 196					       unsigned int n)
 197{
 198	ath10k_ce_write32(ar, ce_ctrl_addr +
 199			  ar->hw_ce_regs->sr_size_addr, n);
 200}
 201
 202static inline void ath10k_ce_src_ring_dmax_set(struct ath10k *ar,
 203					       u32 ce_ctrl_addr,
 204					       unsigned int n)
 205{
 206	struct ath10k_hw_ce_ctrl1 *ctrl_regs = ar->hw_ce_regs->ctrl1_regs;
 207
 208	u32 ctrl1_addr = ath10k_ce_read32(ar, ce_ctrl_addr +
 209					  ctrl_regs->addr);
 210
 211	ath10k_ce_write32(ar, ce_ctrl_addr + ctrl_regs->addr,
 212			  (ctrl1_addr &  ~(ctrl_regs->dmax->mask)) |
 213			  ath10k_set_ring_byte(n, ctrl_regs->dmax));
 214}
 215
 216static inline void ath10k_ce_src_ring_byte_swap_set(struct ath10k *ar,
 217						    u32 ce_ctrl_addr,
 218						    unsigned int n)
 219{
 220	struct ath10k_hw_ce_ctrl1 *ctrl_regs = ar->hw_ce_regs->ctrl1_regs;
 221
 222	u32 ctrl1_addr = ath10k_ce_read32(ar, ce_ctrl_addr +
 223					  ctrl_regs->addr);
 224
 225	ath10k_ce_write32(ar, ce_ctrl_addr + ctrl_regs->addr,
 226			  (ctrl1_addr & ~(ctrl_regs->src_ring->mask)) |
 227			  ath10k_set_ring_byte(n, ctrl_regs->src_ring));
 228}
 229
 230static inline void ath10k_ce_dest_ring_byte_swap_set(struct ath10k *ar,
 231						     u32 ce_ctrl_addr,
 232						     unsigned int n)
 233{
 234	struct ath10k_hw_ce_ctrl1 *ctrl_regs = ar->hw_ce_regs->ctrl1_regs;
 235
 236	u32 ctrl1_addr = ath10k_ce_read32(ar, ce_ctrl_addr +
 237					  ctrl_regs->addr);
 238
 239	ath10k_ce_write32(ar, ce_ctrl_addr + ctrl_regs->addr,
 240			  (ctrl1_addr & ~(ctrl_regs->dst_ring->mask)) |
 241			  ath10k_set_ring_byte(n, ctrl_regs->dst_ring));
 242}
 243
 244static inline
 245	u32 ath10k_ce_dest_ring_read_index_from_ddr(struct ath10k *ar, u32 ce_id)
 246{
 247	struct ath10k_ce *ce = ath10k_ce_priv(ar);
 248
 249	return (ce->vaddr_rri[ce_id] >> CE_DDR_DRRI_SHIFT) &
 250		CE_DDR_RRI_MASK;
 251}
 252
 253static inline u32 ath10k_ce_dest_ring_read_index_get(struct ath10k *ar,
 254						     u32 ce_ctrl_addr)
 255{
 256	struct ath10k_ce *ce = ath10k_ce_priv(ar);
 257	u32 ce_id = COPY_ENGINE_ID(ce_ctrl_addr);
 258	struct ath10k_ce_pipe *ce_state = &ce->ce_states[ce_id];
 259	u32 index;
 260
 261	if (ar->hw_params.rri_on_ddr &&
 262	    (ce_state->attr_flags & CE_ATTR_DIS_INTR))
 263		index = ath10k_ce_dest_ring_read_index_from_ddr(ar, ce_id);
 264	else
 265		index = ath10k_ce_read32(ar, ce_ctrl_addr +
 266					 ar->hw_ce_regs->current_drri_addr);
 267
 268	return index;
 269}
 270
 271static inline void ath10k_ce_dest_ring_base_addr_set(struct ath10k *ar,
 272						     u32 ce_id,
 273						     u64 addr)
 274{
 275	struct ath10k_ce *ce = ath10k_ce_priv(ar);
 276	struct ath10k_ce_pipe *ce_state = &ce->ce_states[ce_id];
 277	u32 ce_ctrl_addr = ath10k_ce_base_address(ar, ce_id);
 278	u32 addr_lo = lower_32_bits(addr);
 279
 280	ath10k_ce_write32(ar, ce_ctrl_addr +
 281			  ar->hw_ce_regs->dr_base_addr_lo, addr_lo);
 282
 283	if (ce_state->ops->ce_set_dest_ring_base_addr_hi) {
 284		ce_state->ops->ce_set_dest_ring_base_addr_hi(ar, ce_ctrl_addr,
 285							     addr);
 286	}
 287}
 288
 289static void ath10k_ce_set_dest_ring_base_addr_hi(struct ath10k *ar,
 290						 u32 ce_ctrl_addr,
 291						 u64 addr)
 292{
 293	u32 addr_hi = upper_32_bits(addr) & CE_DESC_ADDR_HI_MASK;
 294	u32 reg_value;
 295
 296	reg_value = ath10k_ce_read32(ar, ce_ctrl_addr +
 297				     ar->hw_ce_regs->dr_base_addr_hi);
 298	reg_value &= ~CE_DESC_ADDR_HI_MASK;
 299	reg_value |= addr_hi;
 300	ath10k_ce_write32(ar, ce_ctrl_addr +
 301			  ar->hw_ce_regs->dr_base_addr_hi, reg_value);
 302}
 303
 304static inline void ath10k_ce_dest_ring_size_set(struct ath10k *ar,
 305						u32 ce_ctrl_addr,
 306						unsigned int n)
 307{
 308	ath10k_ce_write32(ar, ce_ctrl_addr +
 309			  ar->hw_ce_regs->dr_size_addr, n);
 310}
 311
 312static inline void ath10k_ce_src_ring_highmark_set(struct ath10k *ar,
 313						   u32 ce_ctrl_addr,
 314						   unsigned int n)
 315{
 316	struct ath10k_hw_ce_dst_src_wm_regs *srcr_wm = ar->hw_ce_regs->wm_srcr;
 317	u32 addr = ath10k_ce_read32(ar, ce_ctrl_addr + srcr_wm->addr);
 318
 319	ath10k_ce_write32(ar, ce_ctrl_addr + srcr_wm->addr,
 320			  (addr & ~(srcr_wm->wm_high->mask)) |
 321			  (ath10k_set_ring_byte(n, srcr_wm->wm_high)));
 322}
 323
 324static inline void ath10k_ce_src_ring_lowmark_set(struct ath10k *ar,
 325						  u32 ce_ctrl_addr,
 326						  unsigned int n)
 327{
 328	struct ath10k_hw_ce_dst_src_wm_regs *srcr_wm = ar->hw_ce_regs->wm_srcr;
 329	u32 addr = ath10k_ce_read32(ar, ce_ctrl_addr + srcr_wm->addr);
 330
 331	ath10k_ce_write32(ar, ce_ctrl_addr + srcr_wm->addr,
 332			  (addr & ~(srcr_wm->wm_low->mask)) |
 333			  (ath10k_set_ring_byte(n, srcr_wm->wm_low)));
 334}
 335
 336static inline void ath10k_ce_dest_ring_highmark_set(struct ath10k *ar,
 337						    u32 ce_ctrl_addr,
 338						    unsigned int n)
 339{
 340	struct ath10k_hw_ce_dst_src_wm_regs *dstr_wm = ar->hw_ce_regs->wm_dstr;
 341	u32 addr = ath10k_ce_read32(ar, ce_ctrl_addr + dstr_wm->addr);
 342
 343	ath10k_ce_write32(ar, ce_ctrl_addr + dstr_wm->addr,
 344			  (addr & ~(dstr_wm->wm_high->mask)) |
 345			  (ath10k_set_ring_byte(n, dstr_wm->wm_high)));
 346}
 347
 348static inline void ath10k_ce_dest_ring_lowmark_set(struct ath10k *ar,
 349						   u32 ce_ctrl_addr,
 350						   unsigned int n)
 351{
 352	struct ath10k_hw_ce_dst_src_wm_regs *dstr_wm = ar->hw_ce_regs->wm_dstr;
 353	u32 addr = ath10k_ce_read32(ar, ce_ctrl_addr + dstr_wm->addr);
 354
 355	ath10k_ce_write32(ar, ce_ctrl_addr + dstr_wm->addr,
 356			  (addr & ~(dstr_wm->wm_low->mask)) |
 357			  (ath10k_set_ring_byte(n, dstr_wm->wm_low)));
 358}
 359
 360static inline void ath10k_ce_copy_complete_inter_enable(struct ath10k *ar,
 361							u32 ce_ctrl_addr)
 362{
 363	struct ath10k_hw_ce_host_ie *host_ie = ar->hw_ce_regs->host_ie;
 364
 365	u32 host_ie_addr = ath10k_ce_read32(ar, ce_ctrl_addr +
 366					    ar->hw_ce_regs->host_ie_addr);
 367
 368	ath10k_ce_write32(ar, ce_ctrl_addr + ar->hw_ce_regs->host_ie_addr,
 369			  host_ie_addr | host_ie->copy_complete->mask);
 370}
 371
 372static inline void ath10k_ce_copy_complete_intr_disable(struct ath10k *ar,
 373							u32 ce_ctrl_addr)
 374{
 375	struct ath10k_hw_ce_host_ie *host_ie = ar->hw_ce_regs->host_ie;
 376
 377	u32 host_ie_addr = ath10k_ce_read32(ar, ce_ctrl_addr +
 378					    ar->hw_ce_regs->host_ie_addr);
 379
 380	ath10k_ce_write32(ar, ce_ctrl_addr + ar->hw_ce_regs->host_ie_addr,
 381			  host_ie_addr & ~(host_ie->copy_complete->mask));
 382}
 383
 384static inline void ath10k_ce_watermark_intr_disable(struct ath10k *ar,
 385						    u32 ce_ctrl_addr)
 386{
 387	struct ath10k_hw_ce_host_wm_regs *wm_regs = ar->hw_ce_regs->wm_regs;
 388
 389	u32 host_ie_addr = ath10k_ce_read32(ar, ce_ctrl_addr +
 390					    ar->hw_ce_regs->host_ie_addr);
 391
 392	ath10k_ce_write32(ar, ce_ctrl_addr + ar->hw_ce_regs->host_ie_addr,
 393			  host_ie_addr & ~(wm_regs->wm_mask));
 394}
 395
 
 
 
 
 
 
 
 
 
 
 
 
 
 396static inline void ath10k_ce_error_intr_disable(struct ath10k *ar,
 397						u32 ce_ctrl_addr)
 398{
 399	struct ath10k_hw_ce_misc_regs *misc_regs = ar->hw_ce_regs->misc_regs;
 400
 401	u32 misc_ie_addr = ath10k_ce_read32(ar,
 402			ce_ctrl_addr + ar->hw_ce_regs->misc_ie_addr);
 403
 404	ath10k_ce_write32(ar,
 405			  ce_ctrl_addr + ar->hw_ce_regs->misc_ie_addr,
 406			  misc_ie_addr & ~(misc_regs->err_mask));
 407}
 408
 409static inline void ath10k_ce_engine_int_status_clear(struct ath10k *ar,
 410						     u32 ce_ctrl_addr,
 411						     unsigned int mask)
 412{
 413	struct ath10k_hw_ce_host_wm_regs *wm_regs = ar->hw_ce_regs->wm_regs;
 414
 415	ath10k_ce_write32(ar, ce_ctrl_addr + wm_regs->addr, mask);
 416}
 417
 418/*
 419 * Guts of ath10k_ce_send.
 420 * The caller takes responsibility for any needed locking.
 421 */
 422static int _ath10k_ce_send_nolock(struct ath10k_ce_pipe *ce_state,
 423				  void *per_transfer_context,
 424				  dma_addr_t buffer,
 425				  unsigned int nbytes,
 426				  unsigned int transfer_id,
 427				  unsigned int flags)
 428{
 429	struct ath10k *ar = ce_state->ar;
 430	struct ath10k_ce_ring *src_ring = ce_state->src_ring;
 431	struct ce_desc *desc, sdesc;
 432	unsigned int nentries_mask = src_ring->nentries_mask;
 433	unsigned int sw_index = src_ring->sw_index;
 434	unsigned int write_index = src_ring->write_index;
 435	u32 ctrl_addr = ce_state->ctrl_addr;
 436	u32 desc_flags = 0;
 437	int ret = 0;
 438
 439	if (nbytes > ce_state->src_sz_max)
 440		ath10k_warn(ar, "%s: send more we can (nbytes: %d, max: %d)\n",
 441			    __func__, nbytes, ce_state->src_sz_max);
 442
 443	if (unlikely(CE_RING_DELTA(nentries_mask,
 444				   write_index, sw_index - 1) <= 0)) {
 445		ret = -ENOSR;
 446		goto exit;
 447	}
 448
 449	desc = CE_SRC_RING_TO_DESC(src_ring->base_addr_owner_space,
 450				   write_index);
 451
 452	desc_flags |= SM(transfer_id, CE_DESC_FLAGS_META_DATA);
 453
 454	if (flags & CE_SEND_FLAG_GATHER)
 455		desc_flags |= CE_DESC_FLAGS_GATHER;
 456	if (flags & CE_SEND_FLAG_BYTE_SWAP)
 457		desc_flags |= CE_DESC_FLAGS_BYTE_SWAP;
 458
 459	sdesc.addr   = __cpu_to_le32(buffer);
 460	sdesc.nbytes = __cpu_to_le16(nbytes);
 461	sdesc.flags  = __cpu_to_le16(desc_flags);
 462
 463	*desc = sdesc;
 464
 465	src_ring->per_transfer_context[write_index] = per_transfer_context;
 466
 467	/* Update Source Ring Write Index */
 468	write_index = CE_RING_IDX_INCR(nentries_mask, write_index);
 469
 470	/* WORKAROUND */
 471	if (!(flags & CE_SEND_FLAG_GATHER))
 472		ath10k_ce_src_ring_write_index_set(ar, ctrl_addr, write_index);
 473
 474	src_ring->write_index = write_index;
 475exit:
 476	return ret;
 477}
 478
 479static int _ath10k_ce_send_nolock_64(struct ath10k_ce_pipe *ce_state,
 480				     void *per_transfer_context,
 481				     dma_addr_t buffer,
 482				     unsigned int nbytes,
 483				     unsigned int transfer_id,
 484				     unsigned int flags)
 485{
 486	struct ath10k *ar = ce_state->ar;
 487	struct ath10k_ce_ring *src_ring = ce_state->src_ring;
 488	struct ce_desc_64 *desc, sdesc;
 489	unsigned int nentries_mask = src_ring->nentries_mask;
 490	unsigned int sw_index;
 491	unsigned int write_index = src_ring->write_index;
 492	u32 ctrl_addr = ce_state->ctrl_addr;
 493	__le32 *addr;
 494	u32 desc_flags = 0;
 495	int ret = 0;
 496
 497	if (test_bit(ATH10K_FLAG_CRASH_FLUSH, &ar->dev_flags))
 498		return -ESHUTDOWN;
 499
 500	if (nbytes > ce_state->src_sz_max)
 501		ath10k_warn(ar, "%s: send more we can (nbytes: %d, max: %d)\n",
 502			    __func__, nbytes, ce_state->src_sz_max);
 503
 504	if (ar->hw_params.rri_on_ddr)
 505		sw_index = ath10k_ce_src_ring_read_index_from_ddr(ar, ce_state->id);
 506	else
 507		sw_index = src_ring->sw_index;
 508
 509	if (unlikely(CE_RING_DELTA(nentries_mask,
 510				   write_index, sw_index - 1) <= 0)) {
 511		ret = -ENOSR;
 512		goto exit;
 513	}
 514
 515	desc = CE_SRC_RING_TO_DESC_64(src_ring->base_addr_owner_space,
 516				      write_index);
 517
 518	desc_flags |= SM(transfer_id, CE_DESC_FLAGS_META_DATA);
 519
 520	if (flags & CE_SEND_FLAG_GATHER)
 521		desc_flags |= CE_DESC_FLAGS_GATHER;
 522
 523	if (flags & CE_SEND_FLAG_BYTE_SWAP)
 524		desc_flags |= CE_DESC_FLAGS_BYTE_SWAP;
 525
 526	addr = (__le32 *)&sdesc.addr;
 527
 528	flags |= upper_32_bits(buffer) & CE_DESC_ADDR_HI_MASK;
 529	addr[0] = __cpu_to_le32(buffer);
 530	addr[1] = __cpu_to_le32(flags);
 531	if (flags & CE_SEND_FLAG_GATHER)
 532		addr[1] |= __cpu_to_le32(CE_WCN3990_DESC_FLAGS_GATHER);
 533	else
 534		addr[1] &= ~(__cpu_to_le32(CE_WCN3990_DESC_FLAGS_GATHER));
 535
 536	sdesc.nbytes = __cpu_to_le16(nbytes);
 537	sdesc.flags  = __cpu_to_le16(desc_flags);
 538
 539	*desc = sdesc;
 540
 541	src_ring->per_transfer_context[write_index] = per_transfer_context;
 542
 543	/* Update Source Ring Write Index */
 544	write_index = CE_RING_IDX_INCR(nentries_mask, write_index);
 545
 546	if (!(flags & CE_SEND_FLAG_GATHER)) {
 547		if (ar->hw_params.shadow_reg_support)
 548			ath10k_ce_shadow_src_ring_write_index_set(ar, ce_state,
 549								  write_index);
 550		else
 551			ath10k_ce_src_ring_write_index_set(ar, ctrl_addr,
 552							   write_index);
 553	}
 554
 555	src_ring->write_index = write_index;
 556exit:
 557	return ret;
 558}
 559
 560int ath10k_ce_send_nolock(struct ath10k_ce_pipe *ce_state,
 561			  void *per_transfer_context,
 562			  dma_addr_t buffer,
 563			  unsigned int nbytes,
 564			  unsigned int transfer_id,
 565			  unsigned int flags)
 566{
 567	return ce_state->ops->ce_send_nolock(ce_state, per_transfer_context,
 568				    buffer, nbytes, transfer_id, flags);
 569}
 570EXPORT_SYMBOL(ath10k_ce_send_nolock);
 571
 572void __ath10k_ce_send_revert(struct ath10k_ce_pipe *pipe)
 573{
 574	struct ath10k *ar = pipe->ar;
 575	struct ath10k_ce *ce = ath10k_ce_priv(ar);
 576	struct ath10k_ce_ring *src_ring = pipe->src_ring;
 577	u32 ctrl_addr = pipe->ctrl_addr;
 578
 579	lockdep_assert_held(&ce->ce_lock);
 580
 581	/*
 582	 * This function must be called only if there is an incomplete
 583	 * scatter-gather transfer (before index register is updated)
 584	 * that needs to be cleaned up.
 585	 */
 586	if (WARN_ON_ONCE(src_ring->write_index == src_ring->sw_index))
 587		return;
 588
 589	if (WARN_ON_ONCE(src_ring->write_index ==
 590			 ath10k_ce_src_ring_write_index_get(ar, ctrl_addr)))
 591		return;
 592
 593	src_ring->write_index--;
 594	src_ring->write_index &= src_ring->nentries_mask;
 595
 596	src_ring->per_transfer_context[src_ring->write_index] = NULL;
 597}
 598EXPORT_SYMBOL(__ath10k_ce_send_revert);
 599
 600int ath10k_ce_send(struct ath10k_ce_pipe *ce_state,
 601		   void *per_transfer_context,
 602		   dma_addr_t buffer,
 603		   unsigned int nbytes,
 604		   unsigned int transfer_id,
 605		   unsigned int flags)
 606{
 607	struct ath10k *ar = ce_state->ar;
 608	struct ath10k_ce *ce = ath10k_ce_priv(ar);
 609	int ret;
 610
 611	spin_lock_bh(&ce->ce_lock);
 612	ret = ath10k_ce_send_nolock(ce_state, per_transfer_context,
 613				    buffer, nbytes, transfer_id, flags);
 614	spin_unlock_bh(&ce->ce_lock);
 615
 616	return ret;
 617}
 618EXPORT_SYMBOL(ath10k_ce_send);
 619
 620int ath10k_ce_num_free_src_entries(struct ath10k_ce_pipe *pipe)
 621{
 622	struct ath10k *ar = pipe->ar;
 623	struct ath10k_ce *ce = ath10k_ce_priv(ar);
 624	int delta;
 625
 626	spin_lock_bh(&ce->ce_lock);
 627	delta = CE_RING_DELTA(pipe->src_ring->nentries_mask,
 628			      pipe->src_ring->write_index,
 629			      pipe->src_ring->sw_index - 1);
 630	spin_unlock_bh(&ce->ce_lock);
 631
 632	return delta;
 633}
 634EXPORT_SYMBOL(ath10k_ce_num_free_src_entries);
 635
 636int __ath10k_ce_rx_num_free_bufs(struct ath10k_ce_pipe *pipe)
 637{
 638	struct ath10k *ar = pipe->ar;
 639	struct ath10k_ce *ce = ath10k_ce_priv(ar);
 640	struct ath10k_ce_ring *dest_ring = pipe->dest_ring;
 641	unsigned int nentries_mask = dest_ring->nentries_mask;
 642	unsigned int write_index = dest_ring->write_index;
 643	unsigned int sw_index = dest_ring->sw_index;
 644
 645	lockdep_assert_held(&ce->ce_lock);
 646
 647	return CE_RING_DELTA(nentries_mask, write_index, sw_index - 1);
 648}
 649EXPORT_SYMBOL(__ath10k_ce_rx_num_free_bufs);
 650
 651static int __ath10k_ce_rx_post_buf(struct ath10k_ce_pipe *pipe, void *ctx,
 652				   dma_addr_t paddr)
 653{
 654	struct ath10k *ar = pipe->ar;
 655	struct ath10k_ce *ce = ath10k_ce_priv(ar);
 656	struct ath10k_ce_ring *dest_ring = pipe->dest_ring;
 657	unsigned int nentries_mask = dest_ring->nentries_mask;
 658	unsigned int write_index = dest_ring->write_index;
 659	unsigned int sw_index = dest_ring->sw_index;
 660	struct ce_desc *base = dest_ring->base_addr_owner_space;
 661	struct ce_desc *desc = CE_DEST_RING_TO_DESC(base, write_index);
 662	u32 ctrl_addr = pipe->ctrl_addr;
 663
 664	lockdep_assert_held(&ce->ce_lock);
 665
 666	if ((pipe->id != 5) &&
 667	    CE_RING_DELTA(nentries_mask, write_index, sw_index - 1) == 0)
 668		return -ENOSPC;
 669
 670	desc->addr = __cpu_to_le32(paddr);
 671	desc->nbytes = 0;
 672
 673	dest_ring->per_transfer_context[write_index] = ctx;
 674	write_index = CE_RING_IDX_INCR(nentries_mask, write_index);
 675	ath10k_ce_dest_ring_write_index_set(ar, ctrl_addr, write_index);
 676	dest_ring->write_index = write_index;
 677
 678	return 0;
 679}
 680
 681static int __ath10k_ce_rx_post_buf_64(struct ath10k_ce_pipe *pipe,
 682				      void *ctx,
 683				      dma_addr_t paddr)
 684{
 685	struct ath10k *ar = pipe->ar;
 686	struct ath10k_ce *ce = ath10k_ce_priv(ar);
 687	struct ath10k_ce_ring *dest_ring = pipe->dest_ring;
 688	unsigned int nentries_mask = dest_ring->nentries_mask;
 689	unsigned int write_index = dest_ring->write_index;
 690	unsigned int sw_index = dest_ring->sw_index;
 691	struct ce_desc_64 *base = dest_ring->base_addr_owner_space;
 692	struct ce_desc_64 *desc =
 693			CE_DEST_RING_TO_DESC_64(base, write_index);
 694	u32 ctrl_addr = pipe->ctrl_addr;
 695
 696	lockdep_assert_held(&ce->ce_lock);
 697
 698	if (CE_RING_DELTA(nentries_mask, write_index, sw_index - 1) == 0)
 699		return -ENOSPC;
 700
 701	desc->addr = __cpu_to_le64(paddr);
 702	desc->addr &= __cpu_to_le64(CE_DESC_ADDR_MASK);
 703
 704	desc->nbytes = 0;
 705
 706	dest_ring->per_transfer_context[write_index] = ctx;
 707	write_index = CE_RING_IDX_INCR(nentries_mask, write_index);
 708	ath10k_ce_dest_ring_write_index_set(ar, ctrl_addr, write_index);
 709	dest_ring->write_index = write_index;
 710
 711	return 0;
 712}
 713
 714void ath10k_ce_rx_update_write_idx(struct ath10k_ce_pipe *pipe, u32 nentries)
 715{
 716	struct ath10k *ar = pipe->ar;
 717	struct ath10k_ce_ring *dest_ring = pipe->dest_ring;
 718	unsigned int nentries_mask = dest_ring->nentries_mask;
 719	unsigned int write_index = dest_ring->write_index;
 720	u32 ctrl_addr = pipe->ctrl_addr;
 721	u32 cur_write_idx = ath10k_ce_dest_ring_write_index_get(ar, ctrl_addr);
 722
 723	/* Prevent CE ring stuck issue that will occur when ring is full.
 724	 * Make sure that write index is 1 less than read index.
 725	 */
 726	if (((cur_write_idx + nentries) & nentries_mask) == dest_ring->sw_index)
 727		nentries -= 1;
 728
 729	write_index = CE_RING_IDX_ADD(nentries_mask, write_index, nentries);
 730	ath10k_ce_dest_ring_write_index_set(ar, ctrl_addr, write_index);
 731	dest_ring->write_index = write_index;
 732}
 733EXPORT_SYMBOL(ath10k_ce_rx_update_write_idx);
 734
 735int ath10k_ce_rx_post_buf(struct ath10k_ce_pipe *pipe, void *ctx,
 736			  dma_addr_t paddr)
 737{
 738	struct ath10k *ar = pipe->ar;
 739	struct ath10k_ce *ce = ath10k_ce_priv(ar);
 740	int ret;
 741
 742	spin_lock_bh(&ce->ce_lock);
 743	ret = pipe->ops->ce_rx_post_buf(pipe, ctx, paddr);
 744	spin_unlock_bh(&ce->ce_lock);
 745
 746	return ret;
 747}
 748EXPORT_SYMBOL(ath10k_ce_rx_post_buf);
 749
 750/*
 751 * Guts of ath10k_ce_completed_recv_next.
 752 * The caller takes responsibility for any necessary locking.
 753 */
 754static int
 755	 _ath10k_ce_completed_recv_next_nolock(struct ath10k_ce_pipe *ce_state,
 756					       void **per_transfer_contextp,
 757					       unsigned int *nbytesp)
 758{
 759	struct ath10k_ce_ring *dest_ring = ce_state->dest_ring;
 760	unsigned int nentries_mask = dest_ring->nentries_mask;
 761	unsigned int sw_index = dest_ring->sw_index;
 762
 763	struct ce_desc *base = dest_ring->base_addr_owner_space;
 764	struct ce_desc *desc = CE_DEST_RING_TO_DESC(base, sw_index);
 765	struct ce_desc sdesc;
 766	u16 nbytes;
 767
 768	/* Copy in one go for performance reasons */
 769	sdesc = *desc;
 770
 771	nbytes = __le16_to_cpu(sdesc.nbytes);
 772	if (nbytes == 0) {
 773		/*
 774		 * This closes a relatively unusual race where the Host
 775		 * sees the updated DRRI before the update to the
 776		 * corresponding descriptor has completed. We treat this
 777		 * as a descriptor that is not yet done.
 778		 */
 779		return -EIO;
 780	}
 781
 782	desc->nbytes = 0;
 783
 784	/* Return data from completed destination descriptor */
 785	*nbytesp = nbytes;
 786
 787	if (per_transfer_contextp)
 788		*per_transfer_contextp =
 789			dest_ring->per_transfer_context[sw_index];
 790
 791	/* Copy engine 5 (HTT Rx) will reuse the same transfer context.
 792	 * So update transfer context all CEs except CE5.
 793	 */
 794	if (ce_state->id != 5)
 795		dest_ring->per_transfer_context[sw_index] = NULL;
 796
 797	/* Update sw_index */
 798	sw_index = CE_RING_IDX_INCR(nentries_mask, sw_index);
 799	dest_ring->sw_index = sw_index;
 800
 801	return 0;
 802}
 803
 804static int
 805_ath10k_ce_completed_recv_next_nolock_64(struct ath10k_ce_pipe *ce_state,
 806					 void **per_transfer_contextp,
 807					 unsigned int *nbytesp)
 808{
 809	struct ath10k_ce_ring *dest_ring = ce_state->dest_ring;
 810	unsigned int nentries_mask = dest_ring->nentries_mask;
 811	unsigned int sw_index = dest_ring->sw_index;
 812	struct ce_desc_64 *base = dest_ring->base_addr_owner_space;
 813	struct ce_desc_64 *desc =
 814		CE_DEST_RING_TO_DESC_64(base, sw_index);
 815	struct ce_desc_64 sdesc;
 816	u16 nbytes;
 817
 818	/* Copy in one go for performance reasons */
 819	sdesc = *desc;
 820
 821	nbytes = __le16_to_cpu(sdesc.nbytes);
 822	if (nbytes == 0) {
 823		/* This closes a relatively unusual race where the Host
 824		 * sees the updated DRRI before the update to the
 825		 * corresponding descriptor has completed. We treat this
 826		 * as a descriptor that is not yet done.
 827		 */
 828		return -EIO;
 829	}
 830
 831	desc->nbytes = 0;
 832
 833	/* Return data from completed destination descriptor */
 834	*nbytesp = nbytes;
 835
 836	if (per_transfer_contextp)
 837		*per_transfer_contextp =
 838			dest_ring->per_transfer_context[sw_index];
 839
 840	/* Copy engine 5 (HTT Rx) will reuse the same transfer context.
 841	 * So update transfer context all CEs except CE5.
 842	 */
 843	if (ce_state->id != 5)
 844		dest_ring->per_transfer_context[sw_index] = NULL;
 845
 846	/* Update sw_index */
 847	sw_index = CE_RING_IDX_INCR(nentries_mask, sw_index);
 848	dest_ring->sw_index = sw_index;
 849
 850	return 0;
 851}
 852
 853int ath10k_ce_completed_recv_next_nolock(struct ath10k_ce_pipe *ce_state,
 854					 void **per_transfer_ctx,
 855					 unsigned int *nbytesp)
 856{
 857	return ce_state->ops->ce_completed_recv_next_nolock(ce_state,
 858							    per_transfer_ctx,
 859							    nbytesp);
 860}
 861EXPORT_SYMBOL(ath10k_ce_completed_recv_next_nolock);
 862
 863int ath10k_ce_completed_recv_next(struct ath10k_ce_pipe *ce_state,
 864				  void **per_transfer_contextp,
 865				  unsigned int *nbytesp)
 866{
 867	struct ath10k *ar = ce_state->ar;
 868	struct ath10k_ce *ce = ath10k_ce_priv(ar);
 869	int ret;
 870
 871	spin_lock_bh(&ce->ce_lock);
 872	ret = ce_state->ops->ce_completed_recv_next_nolock(ce_state,
 873						   per_transfer_contextp,
 874						   nbytesp);
 875
 876	spin_unlock_bh(&ce->ce_lock);
 877
 878	return ret;
 879}
 880EXPORT_SYMBOL(ath10k_ce_completed_recv_next);
 881
 882static int _ath10k_ce_revoke_recv_next(struct ath10k_ce_pipe *ce_state,
 883				       void **per_transfer_contextp,
 884				       dma_addr_t *bufferp)
 885{
 886	struct ath10k_ce_ring *dest_ring;
 887	unsigned int nentries_mask;
 888	unsigned int sw_index;
 889	unsigned int write_index;
 890	int ret;
 891	struct ath10k *ar;
 892	struct ath10k_ce *ce;
 893
 894	dest_ring = ce_state->dest_ring;
 895
 896	if (!dest_ring)
 897		return -EIO;
 898
 899	ar = ce_state->ar;
 900	ce = ath10k_ce_priv(ar);
 901
 902	spin_lock_bh(&ce->ce_lock);
 903
 904	nentries_mask = dest_ring->nentries_mask;
 905	sw_index = dest_ring->sw_index;
 906	write_index = dest_ring->write_index;
 907	if (write_index != sw_index) {
 908		struct ce_desc *base = dest_ring->base_addr_owner_space;
 909		struct ce_desc *desc = CE_DEST_RING_TO_DESC(base, sw_index);
 910
 911		/* Return data from completed destination descriptor */
 912		*bufferp = __le32_to_cpu(desc->addr);
 913
 914		if (per_transfer_contextp)
 915			*per_transfer_contextp =
 916				dest_ring->per_transfer_context[sw_index];
 917
 918		/* sanity */
 919		dest_ring->per_transfer_context[sw_index] = NULL;
 920		desc->nbytes = 0;
 921
 922		/* Update sw_index */
 923		sw_index = CE_RING_IDX_INCR(nentries_mask, sw_index);
 924		dest_ring->sw_index = sw_index;
 925		ret = 0;
 926	} else {
 927		ret = -EIO;
 928	}
 929
 930	spin_unlock_bh(&ce->ce_lock);
 931
 932	return ret;
 933}
 934
 935static int _ath10k_ce_revoke_recv_next_64(struct ath10k_ce_pipe *ce_state,
 936					  void **per_transfer_contextp,
 937					  dma_addr_t *bufferp)
 938{
 939	struct ath10k_ce_ring *dest_ring;
 940	unsigned int nentries_mask;
 941	unsigned int sw_index;
 942	unsigned int write_index;
 943	int ret;
 944	struct ath10k *ar;
 945	struct ath10k_ce *ce;
 946
 947	dest_ring = ce_state->dest_ring;
 948
 949	if (!dest_ring)
 950		return -EIO;
 951
 952	ar = ce_state->ar;
 953	ce = ath10k_ce_priv(ar);
 954
 955	spin_lock_bh(&ce->ce_lock);
 956
 957	nentries_mask = dest_ring->nentries_mask;
 958	sw_index = dest_ring->sw_index;
 959	write_index = dest_ring->write_index;
 960	if (write_index != sw_index) {
 961		struct ce_desc_64 *base = dest_ring->base_addr_owner_space;
 962		struct ce_desc_64 *desc =
 963			CE_DEST_RING_TO_DESC_64(base, sw_index);
 964
 965		/* Return data from completed destination descriptor */
 966		*bufferp = __le64_to_cpu(desc->addr);
 967
 968		if (per_transfer_contextp)
 969			*per_transfer_contextp =
 970				dest_ring->per_transfer_context[sw_index];
 971
 972		/* sanity */
 973		dest_ring->per_transfer_context[sw_index] = NULL;
 974		desc->nbytes = 0;
 975
 976		/* Update sw_index */
 977		sw_index = CE_RING_IDX_INCR(nentries_mask, sw_index);
 978		dest_ring->sw_index = sw_index;
 979		ret = 0;
 980	} else {
 981		ret = -EIO;
 982	}
 983
 984	spin_unlock_bh(&ce->ce_lock);
 985
 986	return ret;
 987}
 988
 989int ath10k_ce_revoke_recv_next(struct ath10k_ce_pipe *ce_state,
 990			       void **per_transfer_contextp,
 991			       dma_addr_t *bufferp)
 992{
 993	return ce_state->ops->ce_revoke_recv_next(ce_state,
 994						  per_transfer_contextp,
 995						  bufferp);
 996}
 997EXPORT_SYMBOL(ath10k_ce_revoke_recv_next);
 998
 999/*
1000 * Guts of ath10k_ce_completed_send_next.
1001 * The caller takes responsibility for any necessary locking.
1002 */
1003static int _ath10k_ce_completed_send_next_nolock(struct ath10k_ce_pipe *ce_state,
1004						 void **per_transfer_contextp)
1005{
1006	struct ath10k_ce_ring *src_ring = ce_state->src_ring;
1007	u32 ctrl_addr = ce_state->ctrl_addr;
1008	struct ath10k *ar = ce_state->ar;
1009	unsigned int nentries_mask = src_ring->nentries_mask;
1010	unsigned int sw_index = src_ring->sw_index;
1011	unsigned int read_index;
1012	struct ce_desc *desc;
1013
1014	if (src_ring->hw_index == sw_index) {
1015		/*
1016		 * The SW completion index has caught up with the cached
1017		 * version of the HW completion index.
1018		 * Update the cached HW completion index to see whether
1019		 * the SW has really caught up to the HW, or if the cached
1020		 * value of the HW index has become stale.
1021		 */
1022
1023		read_index = ath10k_ce_src_ring_read_index_get(ar, ctrl_addr);
1024		if (read_index == 0xffffffff)
1025			return -ENODEV;
1026
1027		read_index &= nentries_mask;
1028		src_ring->hw_index = read_index;
1029	}
1030
1031	if (ar->hw_params.rri_on_ddr)
1032		read_index = ath10k_ce_src_ring_read_index_get(ar, ctrl_addr);
1033	else
1034		read_index = src_ring->hw_index;
1035
1036	if (read_index == sw_index)
1037		return -EIO;
1038
1039	if (per_transfer_contextp)
1040		*per_transfer_contextp =
1041			src_ring->per_transfer_context[sw_index];
1042
1043	/* sanity */
1044	src_ring->per_transfer_context[sw_index] = NULL;
1045	desc = CE_SRC_RING_TO_DESC(src_ring->base_addr_owner_space,
1046				   sw_index);
1047	desc->nbytes = 0;
1048
1049	/* Update sw_index */
1050	sw_index = CE_RING_IDX_INCR(nentries_mask, sw_index);
1051	src_ring->sw_index = sw_index;
1052
1053	return 0;
1054}
1055
1056static int _ath10k_ce_completed_send_next_nolock_64(struct ath10k_ce_pipe *ce_state,
1057						    void **per_transfer_contextp)
1058{
1059	struct ath10k_ce_ring *src_ring = ce_state->src_ring;
1060	u32 ctrl_addr = ce_state->ctrl_addr;
1061	struct ath10k *ar = ce_state->ar;
1062	unsigned int nentries_mask = src_ring->nentries_mask;
1063	unsigned int sw_index = src_ring->sw_index;
1064	unsigned int read_index;
1065	struct ce_desc_64 *desc;
1066
1067	if (src_ring->hw_index == sw_index) {
1068		/*
1069		 * The SW completion index has caught up with the cached
1070		 * version of the HW completion index.
1071		 * Update the cached HW completion index to see whether
1072		 * the SW has really caught up to the HW, or if the cached
1073		 * value of the HW index has become stale.
1074		 */
1075
1076		read_index = ath10k_ce_src_ring_read_index_get(ar, ctrl_addr);
1077		if (read_index == 0xffffffff)
1078			return -ENODEV;
1079
1080		read_index &= nentries_mask;
1081		src_ring->hw_index = read_index;
1082	}
1083
1084	if (ar->hw_params.rri_on_ddr)
1085		read_index = ath10k_ce_src_ring_read_index_get(ar, ctrl_addr);
1086	else
1087		read_index = src_ring->hw_index;
1088
1089	if (read_index == sw_index)
1090		return -EIO;
1091
1092	if (per_transfer_contextp)
1093		*per_transfer_contextp =
1094			src_ring->per_transfer_context[sw_index];
1095
1096	/* sanity */
1097	src_ring->per_transfer_context[sw_index] = NULL;
1098	desc = CE_SRC_RING_TO_DESC_64(src_ring->base_addr_owner_space,
1099				      sw_index);
1100	desc->nbytes = 0;
1101
1102	/* Update sw_index */
1103	sw_index = CE_RING_IDX_INCR(nentries_mask, sw_index);
1104	src_ring->sw_index = sw_index;
1105
1106	return 0;
1107}
1108
1109int ath10k_ce_completed_send_next_nolock(struct ath10k_ce_pipe *ce_state,
1110					 void **per_transfer_contextp)
1111{
1112	return ce_state->ops->ce_completed_send_next_nolock(ce_state,
1113							    per_transfer_contextp);
1114}
1115EXPORT_SYMBOL(ath10k_ce_completed_send_next_nolock);
1116
1117static void ath10k_ce_extract_desc_data(struct ath10k *ar,
1118					struct ath10k_ce_ring *src_ring,
1119					u32 sw_index,
1120					dma_addr_t *bufferp,
1121					u32 *nbytesp,
1122					u32 *transfer_idp)
1123{
1124		struct ce_desc *base = src_ring->base_addr_owner_space;
1125		struct ce_desc *desc = CE_SRC_RING_TO_DESC(base, sw_index);
1126
1127		/* Return data from completed source descriptor */
1128		*bufferp = __le32_to_cpu(desc->addr);
1129		*nbytesp = __le16_to_cpu(desc->nbytes);
1130		*transfer_idp = MS(__le16_to_cpu(desc->flags),
1131				   CE_DESC_FLAGS_META_DATA);
1132}
1133
1134static void ath10k_ce_extract_desc_data_64(struct ath10k *ar,
1135					   struct ath10k_ce_ring *src_ring,
1136					   u32 sw_index,
1137					   dma_addr_t *bufferp,
1138					   u32 *nbytesp,
1139					   u32 *transfer_idp)
1140{
1141		struct ce_desc_64 *base = src_ring->base_addr_owner_space;
1142		struct ce_desc_64 *desc =
1143			CE_SRC_RING_TO_DESC_64(base, sw_index);
1144
1145		/* Return data from completed source descriptor */
1146		*bufferp = __le64_to_cpu(desc->addr);
1147		*nbytesp = __le16_to_cpu(desc->nbytes);
1148		*transfer_idp = MS(__le16_to_cpu(desc->flags),
1149				   CE_DESC_FLAGS_META_DATA);
1150}
1151
1152/* NB: Modeled after ath10k_ce_completed_send_next */
1153int ath10k_ce_cancel_send_next(struct ath10k_ce_pipe *ce_state,
1154			       void **per_transfer_contextp,
1155			       dma_addr_t *bufferp,
1156			       unsigned int *nbytesp,
1157			       unsigned int *transfer_idp)
1158{
1159	struct ath10k_ce_ring *src_ring;
1160	unsigned int nentries_mask;
1161	unsigned int sw_index;
1162	unsigned int write_index;
1163	int ret;
1164	struct ath10k *ar;
1165	struct ath10k_ce *ce;
1166
1167	src_ring = ce_state->src_ring;
1168
1169	if (!src_ring)
1170		return -EIO;
1171
1172	ar = ce_state->ar;
1173	ce = ath10k_ce_priv(ar);
1174
1175	spin_lock_bh(&ce->ce_lock);
1176
1177	nentries_mask = src_ring->nentries_mask;
1178	sw_index = src_ring->sw_index;
1179	write_index = src_ring->write_index;
1180
1181	if (write_index != sw_index) {
1182		ce_state->ops->ce_extract_desc_data(ar, src_ring, sw_index,
1183						    bufferp, nbytesp,
1184						    transfer_idp);
1185
1186		if (per_transfer_contextp)
1187			*per_transfer_contextp =
1188				src_ring->per_transfer_context[sw_index];
1189
1190		/* sanity */
1191		src_ring->per_transfer_context[sw_index] = NULL;
1192
1193		/* Update sw_index */
1194		sw_index = CE_RING_IDX_INCR(nentries_mask, sw_index);
1195		src_ring->sw_index = sw_index;
1196		ret = 0;
1197	} else {
1198		ret = -EIO;
1199	}
1200
1201	spin_unlock_bh(&ce->ce_lock);
1202
1203	return ret;
1204}
1205EXPORT_SYMBOL(ath10k_ce_cancel_send_next);
1206
1207int ath10k_ce_completed_send_next(struct ath10k_ce_pipe *ce_state,
1208				  void **per_transfer_contextp)
1209{
1210	struct ath10k *ar = ce_state->ar;
1211	struct ath10k_ce *ce = ath10k_ce_priv(ar);
1212	int ret;
1213
1214	spin_lock_bh(&ce->ce_lock);
1215	ret = ath10k_ce_completed_send_next_nolock(ce_state,
1216						   per_transfer_contextp);
1217	spin_unlock_bh(&ce->ce_lock);
1218
1219	return ret;
1220}
1221EXPORT_SYMBOL(ath10k_ce_completed_send_next);
1222
1223/*
1224 * Guts of interrupt handler for per-engine interrupts on a particular CE.
1225 *
1226 * Invokes registered callbacks for recv_complete,
1227 * send_complete, and watermarks.
1228 */
1229void ath10k_ce_per_engine_service(struct ath10k *ar, unsigned int ce_id)
1230{
1231	struct ath10k_ce *ce = ath10k_ce_priv(ar);
1232	struct ath10k_ce_pipe *ce_state = &ce->ce_states[ce_id];
1233	struct ath10k_hw_ce_host_wm_regs *wm_regs = ar->hw_ce_regs->wm_regs;
1234	u32 ctrl_addr = ce_state->ctrl_addr;
1235
1236	/*
1237	 * Clear before handling
1238	 *
1239	 * Misc CE interrupts are not being handled, but still need
1240	 * to be cleared.
1241	 *
1242	 * NOTE: When the last copy engine interrupt is cleared the
1243	 * hardware will go to sleep.  Once this happens any access to
1244	 * the CE registers can cause a hardware fault.
1245	 */
1246	ath10k_ce_engine_int_status_clear(ar, ctrl_addr,
1247					  wm_regs->cc_mask | wm_regs->wm_mask);
 
 
1248
1249	if (ce_state->recv_cb)
1250		ce_state->recv_cb(ce_state);
1251
1252	if (ce_state->send_cb)
1253		ce_state->send_cb(ce_state);
 
 
 
 
 
 
 
 
 
 
1254}
1255EXPORT_SYMBOL(ath10k_ce_per_engine_service);
1256
1257/*
1258 * Handler for per-engine interrupts on ALL active CEs.
1259 * This is used in cases where the system is sharing a
1260 * single interrupt for all CEs
1261 */
1262
1263void ath10k_ce_per_engine_service_any(struct ath10k *ar)
1264{
1265	int ce_id;
1266	u32 intr_summary;
1267
1268	intr_summary = ath10k_ce_interrupt_summary(ar);
1269
1270	for (ce_id = 0; intr_summary && (ce_id < CE_COUNT); ce_id++) {
1271		if (intr_summary & (1 << ce_id))
1272			intr_summary &= ~(1 << ce_id);
1273		else
1274			/* no intr pending on this CE */
1275			continue;
1276
1277		ath10k_ce_per_engine_service(ar, ce_id);
1278	}
1279}
1280EXPORT_SYMBOL(ath10k_ce_per_engine_service_any);
1281
1282/*
1283 * Adjust interrupts for the copy complete handler.
1284 * If it's needed for either send or recv, then unmask
1285 * this interrupt; otherwise, mask it.
1286 *
1287 * Called with ce_lock held.
1288 */
1289static void ath10k_ce_per_engine_handler_adjust(struct ath10k_ce_pipe *ce_state)
1290{
1291	u32 ctrl_addr = ce_state->ctrl_addr;
1292	struct ath10k *ar = ce_state->ar;
1293	bool disable_copy_compl_intr = ce_state->attr_flags & CE_ATTR_DIS_INTR;
1294
1295	if ((!disable_copy_compl_intr) &&
1296	    (ce_state->send_cb || ce_state->recv_cb))
1297		ath10k_ce_copy_complete_inter_enable(ar, ctrl_addr);
1298	else
1299		ath10k_ce_copy_complete_intr_disable(ar, ctrl_addr);
1300
1301	ath10k_ce_watermark_intr_disable(ar, ctrl_addr);
1302}
1303
1304void ath10k_ce_disable_interrupt(struct ath10k *ar, int ce_id)
1305{
1306	struct ath10k_ce *ce = ath10k_ce_priv(ar);
1307	struct ath10k_ce_pipe *ce_state;
1308	u32 ctrl_addr;
1309
1310	ce_state  = &ce->ce_states[ce_id];
1311	if (ce_state->attr_flags & CE_ATTR_POLL)
1312		return;
1313
1314	ctrl_addr = ath10k_ce_base_address(ar, ce_id);
1315
1316	ath10k_ce_copy_complete_intr_disable(ar, ctrl_addr);
1317	ath10k_ce_error_intr_disable(ar, ctrl_addr);
1318	ath10k_ce_watermark_intr_disable(ar, ctrl_addr);
1319}
1320EXPORT_SYMBOL(ath10k_ce_disable_interrupt);
1321
1322void ath10k_ce_disable_interrupts(struct ath10k *ar)
1323{
1324	int ce_id;
1325
1326	for (ce_id = 0; ce_id < CE_COUNT; ce_id++)
1327		ath10k_ce_disable_interrupt(ar, ce_id);
1328}
1329EXPORT_SYMBOL(ath10k_ce_disable_interrupts);
1330
1331void ath10k_ce_enable_interrupt(struct ath10k *ar, int ce_id)
1332{
1333	struct ath10k_ce *ce = ath10k_ce_priv(ar);
1334	struct ath10k_ce_pipe *ce_state;
1335
1336	ce_state  = &ce->ce_states[ce_id];
1337	if (ce_state->attr_flags & CE_ATTR_POLL)
1338		return;
 
1339
1340	ath10k_ce_per_engine_handler_adjust(ce_state);
1341}
1342EXPORT_SYMBOL(ath10k_ce_enable_interrupt);
1343
1344void ath10k_ce_enable_interrupts(struct ath10k *ar)
1345{
 
1346	int ce_id;
 
1347
1348	/* Enable interrupts for copy engine that
1349	 * are not using polling mode.
1350	 */
1351	for (ce_id = 0; ce_id < CE_COUNT; ce_id++)
1352		ath10k_ce_enable_interrupt(ar, ce_id);
 
 
 
 
 
1353}
1354EXPORT_SYMBOL(ath10k_ce_enable_interrupts);
1355
1356static int ath10k_ce_init_src_ring(struct ath10k *ar,
1357				   unsigned int ce_id,
1358				   const struct ce_attr *attr)
1359{
1360	struct ath10k_ce *ce = ath10k_ce_priv(ar);
1361	struct ath10k_ce_pipe *ce_state = &ce->ce_states[ce_id];
1362	struct ath10k_ce_ring *src_ring = ce_state->src_ring;
1363	u32 nentries, ctrl_addr = ath10k_ce_base_address(ar, ce_id);
1364
1365	nentries = roundup_pow_of_two(attr->src_nentries);
1366
1367	if (ar->hw_params.target_64bit)
1368		memset(src_ring->base_addr_owner_space, 0,
1369		       nentries * sizeof(struct ce_desc_64));
1370	else
1371		memset(src_ring->base_addr_owner_space, 0,
1372		       nentries * sizeof(struct ce_desc));
1373
1374	src_ring->sw_index = ath10k_ce_src_ring_read_index_get(ar, ctrl_addr);
1375	src_ring->sw_index &= src_ring->nentries_mask;
1376	src_ring->hw_index = src_ring->sw_index;
1377
1378	src_ring->write_index =
1379		ath10k_ce_src_ring_write_index_get(ar, ctrl_addr);
1380	src_ring->write_index &= src_ring->nentries_mask;
1381
1382	ath10k_ce_src_ring_base_addr_set(ar, ce_id,
1383					 src_ring->base_addr_ce_space);
1384	ath10k_ce_src_ring_size_set(ar, ctrl_addr, nentries);
1385	ath10k_ce_src_ring_dmax_set(ar, ctrl_addr, attr->src_sz_max);
1386	ath10k_ce_src_ring_byte_swap_set(ar, ctrl_addr, 0);
1387	ath10k_ce_src_ring_lowmark_set(ar, ctrl_addr, 0);
1388	ath10k_ce_src_ring_highmark_set(ar, ctrl_addr, nentries);
1389
1390	ath10k_dbg(ar, ATH10K_DBG_BOOT,
1391		   "boot init ce src ring id %d entries %d base_addr %pK\n",
1392		   ce_id, nentries, src_ring->base_addr_owner_space);
1393
1394	return 0;
1395}
1396
1397static int ath10k_ce_init_dest_ring(struct ath10k *ar,
1398				    unsigned int ce_id,
1399				    const struct ce_attr *attr)
1400{
1401	struct ath10k_ce *ce = ath10k_ce_priv(ar);
1402	struct ath10k_ce_pipe *ce_state = &ce->ce_states[ce_id];
1403	struct ath10k_ce_ring *dest_ring = ce_state->dest_ring;
1404	u32 nentries, ctrl_addr = ath10k_ce_base_address(ar, ce_id);
1405
1406	nentries = roundup_pow_of_two(attr->dest_nentries);
1407
1408	if (ar->hw_params.target_64bit)
1409		memset(dest_ring->base_addr_owner_space, 0,
1410		       nentries * sizeof(struct ce_desc_64));
1411	else
1412		memset(dest_ring->base_addr_owner_space, 0,
1413		       nentries * sizeof(struct ce_desc));
1414
1415	dest_ring->sw_index = ath10k_ce_dest_ring_read_index_get(ar, ctrl_addr);
1416	dest_ring->sw_index &= dest_ring->nentries_mask;
1417	dest_ring->write_index =
1418		ath10k_ce_dest_ring_write_index_get(ar, ctrl_addr);
1419	dest_ring->write_index &= dest_ring->nentries_mask;
1420
1421	ath10k_ce_dest_ring_base_addr_set(ar, ce_id,
1422					  dest_ring->base_addr_ce_space);
1423	ath10k_ce_dest_ring_size_set(ar, ctrl_addr, nentries);
1424	ath10k_ce_dest_ring_byte_swap_set(ar, ctrl_addr, 0);
1425	ath10k_ce_dest_ring_lowmark_set(ar, ctrl_addr, 0);
1426	ath10k_ce_dest_ring_highmark_set(ar, ctrl_addr, nentries);
1427
1428	ath10k_dbg(ar, ATH10K_DBG_BOOT,
1429		   "boot ce dest ring id %d entries %d base_addr %pK\n",
1430		   ce_id, nentries, dest_ring->base_addr_owner_space);
1431
1432	return 0;
1433}
1434
1435static int ath10k_ce_alloc_shadow_base(struct ath10k *ar,
1436				       struct ath10k_ce_ring *src_ring,
1437				       u32 nentries)
1438{
1439	src_ring->shadow_base_unaligned = kcalloc(nentries,
1440						  sizeof(struct ce_desc_64),
1441						  GFP_KERNEL);
1442	if (!src_ring->shadow_base_unaligned)
1443		return -ENOMEM;
1444
1445	src_ring->shadow_base = (struct ce_desc_64 *)
1446			PTR_ALIGN(src_ring->shadow_base_unaligned,
1447				  CE_DESC_RING_ALIGN);
1448	return 0;
1449}
1450
1451static struct ath10k_ce_ring *
1452ath10k_ce_alloc_src_ring(struct ath10k *ar, unsigned int ce_id,
1453			 const struct ce_attr *attr)
1454{
1455	struct ath10k_ce_ring *src_ring;
1456	u32 nentries = attr->src_nentries;
1457	dma_addr_t base_addr;
1458	int ret;
1459
1460	nentries = roundup_pow_of_two(nentries);
1461
1462	src_ring = kzalloc(struct_size(src_ring, per_transfer_context,
1463				       nentries), GFP_KERNEL);
1464	if (src_ring == NULL)
1465		return ERR_PTR(-ENOMEM);
1466
1467	src_ring->nentries = nentries;
1468	src_ring->nentries_mask = nentries - 1;
1469
1470	/*
1471	 * Legacy platforms that do not support cache
1472	 * coherent DMA are unsupported
1473	 */
1474	src_ring->base_addr_owner_space_unaligned =
1475		dma_alloc_coherent(ar->dev,
1476				   (nentries * sizeof(struct ce_desc) +
1477				    CE_DESC_RING_ALIGN),
1478				   &base_addr, GFP_KERNEL);
1479	if (!src_ring->base_addr_owner_space_unaligned) {
1480		kfree(src_ring);
1481		return ERR_PTR(-ENOMEM);
1482	}
1483
1484	src_ring->base_addr_ce_space_unaligned = base_addr;
1485
1486	src_ring->base_addr_owner_space =
1487			PTR_ALIGN(src_ring->base_addr_owner_space_unaligned,
1488				  CE_DESC_RING_ALIGN);
1489	src_ring->base_addr_ce_space =
1490			ALIGN(src_ring->base_addr_ce_space_unaligned,
1491			      CE_DESC_RING_ALIGN);
1492
1493	if (ar->hw_params.shadow_reg_support) {
1494		ret = ath10k_ce_alloc_shadow_base(ar, src_ring, nentries);
1495		if (ret) {
1496			dma_free_coherent(ar->dev,
1497					  (nentries * sizeof(struct ce_desc) +
1498					   CE_DESC_RING_ALIGN),
1499					  src_ring->base_addr_owner_space_unaligned,
1500					  base_addr);
1501			kfree(src_ring);
1502			return ERR_PTR(ret);
1503		}
1504	}
1505
1506	return src_ring;
1507}
1508
1509static struct ath10k_ce_ring *
1510ath10k_ce_alloc_src_ring_64(struct ath10k *ar, unsigned int ce_id,
1511			    const struct ce_attr *attr)
1512{
1513	struct ath10k_ce_ring *src_ring;
1514	u32 nentries = attr->src_nentries;
1515	dma_addr_t base_addr;
1516	int ret;
1517
1518	nentries = roundup_pow_of_two(nentries);
1519
1520	src_ring = kzalloc(struct_size(src_ring, per_transfer_context,
1521				       nentries), GFP_KERNEL);
1522	if (!src_ring)
1523		return ERR_PTR(-ENOMEM);
1524
1525	src_ring->nentries = nentries;
1526	src_ring->nentries_mask = nentries - 1;
1527
1528	/* Legacy platforms that do not support cache
1529	 * coherent DMA are unsupported
1530	 */
1531	src_ring->base_addr_owner_space_unaligned =
1532		dma_alloc_coherent(ar->dev,
1533				   (nentries * sizeof(struct ce_desc_64) +
1534				    CE_DESC_RING_ALIGN),
1535				   &base_addr, GFP_KERNEL);
1536	if (!src_ring->base_addr_owner_space_unaligned) {
1537		kfree(src_ring);
1538		return ERR_PTR(-ENOMEM);
1539	}
1540
1541	src_ring->base_addr_ce_space_unaligned = base_addr;
1542
1543	src_ring->base_addr_owner_space =
1544			PTR_ALIGN(src_ring->base_addr_owner_space_unaligned,
1545				  CE_DESC_RING_ALIGN);
1546	src_ring->base_addr_ce_space =
1547			ALIGN(src_ring->base_addr_ce_space_unaligned,
1548			      CE_DESC_RING_ALIGN);
1549
1550	if (ar->hw_params.shadow_reg_support) {
1551		ret = ath10k_ce_alloc_shadow_base(ar, src_ring, nentries);
1552		if (ret) {
1553			dma_free_coherent(ar->dev,
1554					  (nentries * sizeof(struct ce_desc_64) +
1555					   CE_DESC_RING_ALIGN),
1556					  src_ring->base_addr_owner_space_unaligned,
1557					  base_addr);
1558			kfree(src_ring);
1559			return ERR_PTR(ret);
1560		}
1561	}
1562
1563	return src_ring;
1564}
1565
1566static struct ath10k_ce_ring *
1567ath10k_ce_alloc_dest_ring(struct ath10k *ar, unsigned int ce_id,
1568			  const struct ce_attr *attr)
1569{
1570	struct ath10k_ce_ring *dest_ring;
1571	u32 nentries;
1572	dma_addr_t base_addr;
1573
1574	nentries = roundup_pow_of_two(attr->dest_nentries);
1575
1576	dest_ring = kzalloc(struct_size(dest_ring, per_transfer_context,
1577					nentries), GFP_KERNEL);
1578	if (dest_ring == NULL)
1579		return ERR_PTR(-ENOMEM);
1580
1581	dest_ring->nentries = nentries;
1582	dest_ring->nentries_mask = nentries - 1;
1583
1584	/*
1585	 * Legacy platforms that do not support cache
1586	 * coherent DMA are unsupported
1587	 */
1588	dest_ring->base_addr_owner_space_unaligned =
1589		dma_alloc_coherent(ar->dev,
1590				   (nentries * sizeof(struct ce_desc) +
1591				    CE_DESC_RING_ALIGN),
1592				   &base_addr, GFP_KERNEL);
1593	if (!dest_ring->base_addr_owner_space_unaligned) {
1594		kfree(dest_ring);
1595		return ERR_PTR(-ENOMEM);
1596	}
1597
1598	dest_ring->base_addr_ce_space_unaligned = base_addr;
1599
1600	dest_ring->base_addr_owner_space =
1601			PTR_ALIGN(dest_ring->base_addr_owner_space_unaligned,
1602				  CE_DESC_RING_ALIGN);
1603	dest_ring->base_addr_ce_space =
1604				ALIGN(dest_ring->base_addr_ce_space_unaligned,
1605				      CE_DESC_RING_ALIGN);
1606
1607	return dest_ring;
1608}
1609
1610static struct ath10k_ce_ring *
1611ath10k_ce_alloc_dest_ring_64(struct ath10k *ar, unsigned int ce_id,
1612			     const struct ce_attr *attr)
1613{
1614	struct ath10k_ce_ring *dest_ring;
1615	u32 nentries;
1616	dma_addr_t base_addr;
1617
1618	nentries = roundup_pow_of_two(attr->dest_nentries);
1619
1620	dest_ring = kzalloc(struct_size(dest_ring, per_transfer_context,
1621					nentries), GFP_KERNEL);
1622	if (!dest_ring)
1623		return ERR_PTR(-ENOMEM);
1624
1625	dest_ring->nentries = nentries;
1626	dest_ring->nentries_mask = nentries - 1;
1627
1628	/* Legacy platforms that do not support cache
1629	 * coherent DMA are unsupported
1630	 */
1631	dest_ring->base_addr_owner_space_unaligned =
1632		dma_alloc_coherent(ar->dev,
1633				   (nentries * sizeof(struct ce_desc_64) +
1634				    CE_DESC_RING_ALIGN),
1635				   &base_addr, GFP_KERNEL);
1636	if (!dest_ring->base_addr_owner_space_unaligned) {
1637		kfree(dest_ring);
1638		return ERR_PTR(-ENOMEM);
1639	}
1640
1641	dest_ring->base_addr_ce_space_unaligned = base_addr;
1642
1643	/* Correctly initialize memory to 0 to prevent garbage
1644	 * data crashing system when download firmware
1645	 */
1646	dest_ring->base_addr_owner_space =
1647			PTR_ALIGN(dest_ring->base_addr_owner_space_unaligned,
1648				  CE_DESC_RING_ALIGN);
1649	dest_ring->base_addr_ce_space =
1650			ALIGN(dest_ring->base_addr_ce_space_unaligned,
1651			      CE_DESC_RING_ALIGN);
1652
1653	return dest_ring;
1654}
1655
1656/*
1657 * Initialize a Copy Engine based on caller-supplied attributes.
1658 * This may be called once to initialize both source and destination
1659 * rings or it may be called twice for separate source and destination
1660 * initialization. It may be that only one side or the other is
1661 * initialized by software/firmware.
1662 */
1663int ath10k_ce_init_pipe(struct ath10k *ar, unsigned int ce_id,
1664			const struct ce_attr *attr)
1665{
1666	int ret;
1667
1668	if (attr->src_nentries) {
1669		ret = ath10k_ce_init_src_ring(ar, ce_id, attr);
1670		if (ret) {
1671			ath10k_err(ar, "Failed to initialize CE src ring for ID: %d (%d)\n",
1672				   ce_id, ret);
1673			return ret;
1674		}
1675	}
1676
1677	if (attr->dest_nentries) {
1678		ret = ath10k_ce_init_dest_ring(ar, ce_id, attr);
1679		if (ret) {
1680			ath10k_err(ar, "Failed to initialize CE dest ring for ID: %d (%d)\n",
1681				   ce_id, ret);
1682			return ret;
1683		}
1684	}
1685
1686	return 0;
1687}
1688EXPORT_SYMBOL(ath10k_ce_init_pipe);
1689
1690static void ath10k_ce_deinit_src_ring(struct ath10k *ar, unsigned int ce_id)
1691{
1692	u32 ctrl_addr = ath10k_ce_base_address(ar, ce_id);
1693
1694	ath10k_ce_src_ring_base_addr_set(ar, ce_id, 0);
1695	ath10k_ce_src_ring_size_set(ar, ctrl_addr, 0);
1696	ath10k_ce_src_ring_dmax_set(ar, ctrl_addr, 0);
1697	ath10k_ce_src_ring_highmark_set(ar, ctrl_addr, 0);
1698}
1699
1700static void ath10k_ce_deinit_dest_ring(struct ath10k *ar, unsigned int ce_id)
1701{
1702	u32 ctrl_addr = ath10k_ce_base_address(ar, ce_id);
1703
1704	ath10k_ce_dest_ring_base_addr_set(ar, ce_id, 0);
1705	ath10k_ce_dest_ring_size_set(ar, ctrl_addr, 0);
1706	ath10k_ce_dest_ring_highmark_set(ar, ctrl_addr, 0);
1707}
1708
1709void ath10k_ce_deinit_pipe(struct ath10k *ar, unsigned int ce_id)
1710{
1711	ath10k_ce_deinit_src_ring(ar, ce_id);
1712	ath10k_ce_deinit_dest_ring(ar, ce_id);
1713}
1714EXPORT_SYMBOL(ath10k_ce_deinit_pipe);
1715
1716static void _ath10k_ce_free_pipe(struct ath10k *ar, int ce_id)
1717{
1718	struct ath10k_ce *ce = ath10k_ce_priv(ar);
1719	struct ath10k_ce_pipe *ce_state = &ce->ce_states[ce_id];
1720
1721	if (ce_state->src_ring) {
1722		if (ar->hw_params.shadow_reg_support)
1723			kfree(ce_state->src_ring->shadow_base_unaligned);
1724		dma_free_coherent(ar->dev,
1725				  (ce_state->src_ring->nentries *
1726				   sizeof(struct ce_desc) +
1727				   CE_DESC_RING_ALIGN),
1728				  ce_state->src_ring->base_addr_owner_space,
1729				  ce_state->src_ring->base_addr_ce_space);
1730		kfree(ce_state->src_ring);
1731	}
1732
1733	if (ce_state->dest_ring) {
1734		dma_free_coherent(ar->dev,
1735				  (ce_state->dest_ring->nentries *
1736				   sizeof(struct ce_desc) +
1737				   CE_DESC_RING_ALIGN),
1738				  ce_state->dest_ring->base_addr_owner_space,
1739				  ce_state->dest_ring->base_addr_ce_space);
1740		kfree(ce_state->dest_ring);
1741	}
1742
1743	ce_state->src_ring = NULL;
1744	ce_state->dest_ring = NULL;
1745}
1746
1747static void _ath10k_ce_free_pipe_64(struct ath10k *ar, int ce_id)
1748{
1749	struct ath10k_ce *ce = ath10k_ce_priv(ar);
1750	struct ath10k_ce_pipe *ce_state = &ce->ce_states[ce_id];
1751
1752	if (ce_state->src_ring) {
1753		if (ar->hw_params.shadow_reg_support)
1754			kfree(ce_state->src_ring->shadow_base_unaligned);
1755		dma_free_coherent(ar->dev,
1756				  (ce_state->src_ring->nentries *
1757				   sizeof(struct ce_desc_64) +
1758				   CE_DESC_RING_ALIGN),
1759				  ce_state->src_ring->base_addr_owner_space,
1760				  ce_state->src_ring->base_addr_ce_space);
1761		kfree(ce_state->src_ring);
1762	}
1763
1764	if (ce_state->dest_ring) {
1765		dma_free_coherent(ar->dev,
1766				  (ce_state->dest_ring->nentries *
1767				   sizeof(struct ce_desc_64) +
1768				   CE_DESC_RING_ALIGN),
1769				  ce_state->dest_ring->base_addr_owner_space,
1770				  ce_state->dest_ring->base_addr_ce_space);
1771		kfree(ce_state->dest_ring);
1772	}
1773
1774	ce_state->src_ring = NULL;
1775	ce_state->dest_ring = NULL;
1776}
1777
1778void ath10k_ce_free_pipe(struct ath10k *ar, int ce_id)
1779{
1780	struct ath10k_ce *ce = ath10k_ce_priv(ar);
1781	struct ath10k_ce_pipe *ce_state = &ce->ce_states[ce_id];
1782
1783	ce_state->ops->ce_free_pipe(ar, ce_id);
1784}
1785EXPORT_SYMBOL(ath10k_ce_free_pipe);
1786
1787void ath10k_ce_dump_registers(struct ath10k *ar,
1788			      struct ath10k_fw_crash_data *crash_data)
1789{
1790	struct ath10k_ce *ce = ath10k_ce_priv(ar);
1791	struct ath10k_ce_crash_data ce_data;
1792	u32 addr, id;
1793
1794	lockdep_assert_held(&ar->dump_mutex);
1795
1796	ath10k_err(ar, "Copy Engine register dump:\n");
1797
1798	spin_lock_bh(&ce->ce_lock);
1799	for (id = 0; id < CE_COUNT; id++) {
1800		addr = ath10k_ce_base_address(ar, id);
1801		ce_data.base_addr = cpu_to_le32(addr);
1802
1803		ce_data.src_wr_idx =
1804			cpu_to_le32(ath10k_ce_src_ring_write_index_get(ar, addr));
1805		ce_data.src_r_idx =
1806			cpu_to_le32(ath10k_ce_src_ring_read_index_get(ar, addr));
1807		ce_data.dst_wr_idx =
1808			cpu_to_le32(ath10k_ce_dest_ring_write_index_get(ar, addr));
1809		ce_data.dst_r_idx =
1810			cpu_to_le32(ath10k_ce_dest_ring_read_index_get(ar, addr));
1811
1812		if (crash_data)
1813			crash_data->ce_crash_data[id] = ce_data;
1814
1815		ath10k_err(ar, "[%02d]: 0x%08x %3u %3u %3u %3u", id,
1816			   le32_to_cpu(ce_data.base_addr),
1817			   le32_to_cpu(ce_data.src_wr_idx),
1818			   le32_to_cpu(ce_data.src_r_idx),
1819			   le32_to_cpu(ce_data.dst_wr_idx),
1820			   le32_to_cpu(ce_data.dst_r_idx));
1821	}
1822
1823	spin_unlock_bh(&ce->ce_lock);
1824}
1825EXPORT_SYMBOL(ath10k_ce_dump_registers);
1826
1827static const struct ath10k_ce_ops ce_ops = {
1828	.ce_alloc_src_ring = ath10k_ce_alloc_src_ring,
1829	.ce_alloc_dst_ring = ath10k_ce_alloc_dest_ring,
1830	.ce_rx_post_buf = __ath10k_ce_rx_post_buf,
1831	.ce_completed_recv_next_nolock = _ath10k_ce_completed_recv_next_nolock,
1832	.ce_revoke_recv_next = _ath10k_ce_revoke_recv_next,
1833	.ce_extract_desc_data = ath10k_ce_extract_desc_data,
1834	.ce_free_pipe = _ath10k_ce_free_pipe,
1835	.ce_send_nolock = _ath10k_ce_send_nolock,
1836	.ce_set_src_ring_base_addr_hi = NULL,
1837	.ce_set_dest_ring_base_addr_hi = NULL,
1838	.ce_completed_send_next_nolock = _ath10k_ce_completed_send_next_nolock,
1839};
1840
1841static const struct ath10k_ce_ops ce_64_ops = {
1842	.ce_alloc_src_ring = ath10k_ce_alloc_src_ring_64,
1843	.ce_alloc_dst_ring = ath10k_ce_alloc_dest_ring_64,
1844	.ce_rx_post_buf = __ath10k_ce_rx_post_buf_64,
1845	.ce_completed_recv_next_nolock =
1846				_ath10k_ce_completed_recv_next_nolock_64,
1847	.ce_revoke_recv_next = _ath10k_ce_revoke_recv_next_64,
1848	.ce_extract_desc_data = ath10k_ce_extract_desc_data_64,
1849	.ce_free_pipe = _ath10k_ce_free_pipe_64,
1850	.ce_send_nolock = _ath10k_ce_send_nolock_64,
1851	.ce_set_src_ring_base_addr_hi = ath10k_ce_set_src_ring_base_addr_hi,
1852	.ce_set_dest_ring_base_addr_hi = ath10k_ce_set_dest_ring_base_addr_hi,
1853	.ce_completed_send_next_nolock = _ath10k_ce_completed_send_next_nolock_64,
1854};
1855
1856static void ath10k_ce_set_ops(struct ath10k *ar,
1857			      struct ath10k_ce_pipe *ce_state)
1858{
1859	switch (ar->hw_rev) {
1860	case ATH10K_HW_WCN3990:
1861		ce_state->ops = &ce_64_ops;
1862		break;
1863	default:
1864		ce_state->ops = &ce_ops;
1865		break;
1866	}
1867}
1868
1869int ath10k_ce_alloc_pipe(struct ath10k *ar, int ce_id,
1870			 const struct ce_attr *attr)
1871{
1872	struct ath10k_ce *ce = ath10k_ce_priv(ar);
1873	struct ath10k_ce_pipe *ce_state = &ce->ce_states[ce_id];
1874	int ret;
1875
1876	ath10k_ce_set_ops(ar, ce_state);
1877	/* Make sure there's enough CE ringbuffer entries for HTT TX to avoid
1878	 * additional TX locking checks.
1879	 *
1880	 * For the lack of a better place do the check here.
1881	 */
1882	BUILD_BUG_ON(2 * TARGET_NUM_MSDU_DESC >
1883		     (CE_HTT_H2T_MSG_SRC_NENTRIES - 1));
1884	BUILD_BUG_ON(2 * TARGET_10_4_NUM_MSDU_DESC_PFC >
1885		     (CE_HTT_H2T_MSG_SRC_NENTRIES - 1));
1886	BUILD_BUG_ON(2 * TARGET_TLV_NUM_MSDU_DESC >
1887		     (CE_HTT_H2T_MSG_SRC_NENTRIES - 1));
1888
1889	ce_state->ar = ar;
1890	ce_state->id = ce_id;
1891	ce_state->ctrl_addr = ath10k_ce_base_address(ar, ce_id);
1892	ce_state->attr_flags = attr->flags;
1893	ce_state->src_sz_max = attr->src_sz_max;
1894
1895	if (attr->src_nentries)
1896		ce_state->send_cb = attr->send_cb;
1897
1898	if (attr->dest_nentries)
1899		ce_state->recv_cb = attr->recv_cb;
1900
1901	if (attr->src_nentries) {
1902		ce_state->src_ring =
1903			ce_state->ops->ce_alloc_src_ring(ar, ce_id, attr);
1904		if (IS_ERR(ce_state->src_ring)) {
1905			ret = PTR_ERR(ce_state->src_ring);
1906			ath10k_err(ar, "failed to alloc CE src ring %d: %d\n",
1907				   ce_id, ret);
1908			ce_state->src_ring = NULL;
1909			return ret;
1910		}
1911	}
1912
1913	if (attr->dest_nentries) {
1914		ce_state->dest_ring = ce_state->ops->ce_alloc_dst_ring(ar,
1915									ce_id,
1916									attr);
1917		if (IS_ERR(ce_state->dest_ring)) {
1918			ret = PTR_ERR(ce_state->dest_ring);
1919			ath10k_err(ar, "failed to alloc CE dest ring %d: %d\n",
1920				   ce_id, ret);
1921			ce_state->dest_ring = NULL;
1922			return ret;
1923		}
1924	}
1925
1926	return 0;
1927}
1928EXPORT_SYMBOL(ath10k_ce_alloc_pipe);
1929
1930void ath10k_ce_alloc_rri(struct ath10k *ar)
1931{
1932	int i;
1933	u32 value;
1934	u32 ctrl1_regs;
1935	u32 ce_base_addr;
1936	struct ath10k_ce *ce = ath10k_ce_priv(ar);
1937
1938	ce->vaddr_rri = dma_alloc_coherent(ar->dev,
1939					   (CE_COUNT * sizeof(u32)),
1940					   &ce->paddr_rri, GFP_KERNEL);
1941
1942	if (!ce->vaddr_rri)
1943		return;
1944
1945	ath10k_ce_write32(ar, ar->hw_ce_regs->ce_rri_low,
1946			  lower_32_bits(ce->paddr_rri));
1947	ath10k_ce_write32(ar, ar->hw_ce_regs->ce_rri_high,
1948			  (upper_32_bits(ce->paddr_rri) &
1949			  CE_DESC_ADDR_HI_MASK));
1950
1951	for (i = 0; i < CE_COUNT; i++) {
1952		ctrl1_regs = ar->hw_ce_regs->ctrl1_regs->addr;
1953		ce_base_addr = ath10k_ce_base_address(ar, i);
1954		value = ath10k_ce_read32(ar, ce_base_addr + ctrl1_regs);
1955		value |= ar->hw_ce_regs->upd->mask;
1956		ath10k_ce_write32(ar, ce_base_addr + ctrl1_regs, value);
1957	}
1958}
1959EXPORT_SYMBOL(ath10k_ce_alloc_rri);
1960
1961void ath10k_ce_free_rri(struct ath10k *ar)
1962{
1963	struct ath10k_ce *ce = ath10k_ce_priv(ar);
1964
1965	dma_free_coherent(ar->dev, (CE_COUNT * sizeof(u32)),
1966			  ce->vaddr_rri,
1967			  ce->paddr_rri);
1968}
1969EXPORT_SYMBOL(ath10k_ce_free_rri);
v5.9
   1// SPDX-License-Identifier: ISC
   2/*
   3 * Copyright (c) 2005-2011 Atheros Communications Inc.
   4 * Copyright (c) 2011-2017 Qualcomm Atheros, Inc.
   5 * Copyright (c) 2018 The Linux Foundation. All rights reserved.
 
   6 */
   7
   8#include "hif.h"
   9#include "ce.h"
  10#include "debug.h"
  11
  12/*
  13 * Support for Copy Engine hardware, which is mainly used for
  14 * communication between Host and Target over a PCIe interconnect.
  15 */
  16
  17/*
  18 * A single CopyEngine (CE) comprises two "rings":
  19 *   a source ring
  20 *   a destination ring
  21 *
  22 * Each ring consists of a number of descriptors which specify
  23 * an address, length, and meta-data.
  24 *
  25 * Typically, one side of the PCIe/AHB/SNOC interconnect (Host or Target)
  26 * controls one ring and the other side controls the other ring.
  27 * The source side chooses when to initiate a transfer and it
  28 * chooses what to send (buffer address, length). The destination
  29 * side keeps a supply of "anonymous receive buffers" available and
  30 * it handles incoming data as it arrives (when the destination
  31 * receives an interrupt).
  32 *
  33 * The sender may send a simple buffer (address/length) or it may
  34 * send a small list of buffers.  When a small list is sent, hardware
  35 * "gathers" these and they end up in a single destination buffer
  36 * with a single interrupt.
  37 *
  38 * There are several "contexts" managed by this layer -- more, it
  39 * may seem -- than should be needed. These are provided mainly for
  40 * maximum flexibility and especially to facilitate a simpler HIF
  41 * implementation. There are per-CopyEngine recv, send, and watermark
  42 * contexts. These are supplied by the caller when a recv, send,
  43 * or watermark handler is established and they are echoed back to
  44 * the caller when the respective callbacks are invoked. There is
  45 * also a per-transfer context supplied by the caller when a buffer
  46 * (or sendlist) is sent and when a buffer is enqueued for recv.
  47 * These per-transfer contexts are echoed back to the caller when
  48 * the buffer is sent/received.
  49 */
  50
  51static inline u32 shadow_sr_wr_ind_addr(struct ath10k *ar,
  52					struct ath10k_ce_pipe *ce_state)
  53{
  54	u32 ce_id = ce_state->id;
  55	u32 addr = 0;
  56
  57	switch (ce_id) {
  58	case 0:
  59		addr = 0x00032000;
  60		break;
  61	case 3:
  62		addr = 0x0003200C;
  63		break;
  64	case 4:
  65		addr = 0x00032010;
  66		break;
  67	case 5:
  68		addr = 0x00032014;
  69		break;
  70	case 7:
  71		addr = 0x0003201C;
  72		break;
  73	default:
  74		ath10k_warn(ar, "invalid CE id: %d", ce_id);
  75		break;
  76	}
  77	return addr;
  78}
  79
  80static inline u32 shadow_dst_wr_ind_addr(struct ath10k *ar,
  81					 struct ath10k_ce_pipe *ce_state)
  82{
  83	u32 ce_id = ce_state->id;
  84	u32 addr = 0;
  85
  86	switch (ce_id) {
  87	case 1:
  88		addr = 0x00032034;
  89		break;
  90	case 2:
  91		addr = 0x00032038;
  92		break;
  93	case 5:
  94		addr = 0x00032044;
  95		break;
  96	case 7:
  97		addr = 0x0003204C;
  98		break;
  99	case 8:
 100		addr = 0x00032050;
 101		break;
 102	case 9:
 103		addr = 0x00032054;
 104		break;
 105	case 10:
 106		addr = 0x00032058;
 107		break;
 108	case 11:
 109		addr = 0x0003205C;
 110		break;
 111	default:
 112		ath10k_warn(ar, "invalid CE id: %d", ce_id);
 113		break;
 114	}
 115
 116	return addr;
 117}
 118
 119static inline unsigned int
 120ath10k_set_ring_byte(unsigned int offset,
 121		     struct ath10k_hw_ce_regs_addr_map *addr_map)
 122{
 123	return ((offset << addr_map->lsb) & addr_map->mask);
 124}
 125
 126static inline unsigned int
 127ath10k_get_ring_byte(unsigned int offset,
 128		     struct ath10k_hw_ce_regs_addr_map *addr_map)
 129{
 130	return ((offset & addr_map->mask) >> (addr_map->lsb));
 131}
 132
 133static inline u32 ath10k_ce_read32(struct ath10k *ar, u32 offset)
 134{
 135	struct ath10k_ce *ce = ath10k_ce_priv(ar);
 136
 137	return ce->bus_ops->read32(ar, offset);
 138}
 139
 140static inline void ath10k_ce_write32(struct ath10k *ar, u32 offset, u32 value)
 141{
 142	struct ath10k_ce *ce = ath10k_ce_priv(ar);
 143
 144	ce->bus_ops->write32(ar, offset, value);
 145}
 146
 147static inline void ath10k_ce_dest_ring_write_index_set(struct ath10k *ar,
 148						       u32 ce_ctrl_addr,
 149						       unsigned int n)
 150{
 151	ath10k_ce_write32(ar, ce_ctrl_addr +
 152			  ar->hw_ce_regs->dst_wr_index_addr, n);
 153}
 154
 155static inline u32 ath10k_ce_dest_ring_write_index_get(struct ath10k *ar,
 156						      u32 ce_ctrl_addr)
 157{
 158	return ath10k_ce_read32(ar, ce_ctrl_addr +
 159				ar->hw_ce_regs->dst_wr_index_addr);
 160}
 161
 162static inline void ath10k_ce_src_ring_write_index_set(struct ath10k *ar,
 163						      u32 ce_ctrl_addr,
 164						      unsigned int n)
 165{
 166	ath10k_ce_write32(ar, ce_ctrl_addr +
 167			  ar->hw_ce_regs->sr_wr_index_addr, n);
 168}
 169
 170static inline u32 ath10k_ce_src_ring_write_index_get(struct ath10k *ar,
 171						     u32 ce_ctrl_addr)
 172{
 173	return ath10k_ce_read32(ar, ce_ctrl_addr +
 174				ar->hw_ce_regs->sr_wr_index_addr);
 175}
 176
 177static inline u32 ath10k_ce_src_ring_read_index_from_ddr(struct ath10k *ar,
 178							 u32 ce_id)
 179{
 180	struct ath10k_ce *ce = ath10k_ce_priv(ar);
 181
 182	return ce->vaddr_rri[ce_id] & CE_DDR_RRI_MASK;
 183}
 184
 185static inline u32 ath10k_ce_src_ring_read_index_get(struct ath10k *ar,
 186						    u32 ce_ctrl_addr)
 187{
 188	struct ath10k_ce *ce = ath10k_ce_priv(ar);
 189	u32 ce_id = COPY_ENGINE_ID(ce_ctrl_addr);
 190	struct ath10k_ce_pipe *ce_state = &ce->ce_states[ce_id];
 191	u32 index;
 192
 193	if (ar->hw_params.rri_on_ddr &&
 194	    (ce_state->attr_flags & CE_ATTR_DIS_INTR))
 195		index = ath10k_ce_src_ring_read_index_from_ddr(ar, ce_id);
 196	else
 197		index = ath10k_ce_read32(ar, ce_ctrl_addr +
 198					 ar->hw_ce_regs->current_srri_addr);
 199
 200	return index;
 201}
 202
 203static inline void
 204ath10k_ce_shadow_src_ring_write_index_set(struct ath10k *ar,
 205					  struct ath10k_ce_pipe *ce_state,
 206					  unsigned int value)
 207{
 208	ath10k_ce_write32(ar, shadow_sr_wr_ind_addr(ar, ce_state), value);
 209}
 210
 211static inline void
 212ath10k_ce_shadow_dest_ring_write_index_set(struct ath10k *ar,
 213					   struct ath10k_ce_pipe *ce_state,
 214					   unsigned int value)
 215{
 216	ath10k_ce_write32(ar, shadow_dst_wr_ind_addr(ar, ce_state), value);
 217}
 218
 219static inline void ath10k_ce_src_ring_base_addr_set(struct ath10k *ar,
 220						    u32 ce_id,
 221						    u64 addr)
 222{
 223	struct ath10k_ce *ce = ath10k_ce_priv(ar);
 224	struct ath10k_ce_pipe *ce_state = &ce->ce_states[ce_id];
 225	u32 ce_ctrl_addr = ath10k_ce_base_address(ar, ce_id);
 226	u32 addr_lo = lower_32_bits(addr);
 227
 228	ath10k_ce_write32(ar, ce_ctrl_addr +
 229			  ar->hw_ce_regs->sr_base_addr_lo, addr_lo);
 230
 231	if (ce_state->ops->ce_set_src_ring_base_addr_hi) {
 232		ce_state->ops->ce_set_src_ring_base_addr_hi(ar, ce_ctrl_addr,
 233							    addr);
 234	}
 235}
 236
 237static void ath10k_ce_set_src_ring_base_addr_hi(struct ath10k *ar,
 238						u32 ce_ctrl_addr,
 239						u64 addr)
 240{
 241	u32 addr_hi = upper_32_bits(addr) & CE_DESC_ADDR_HI_MASK;
 242
 243	ath10k_ce_write32(ar, ce_ctrl_addr +
 244			  ar->hw_ce_regs->sr_base_addr_hi, addr_hi);
 245}
 246
 247static inline void ath10k_ce_src_ring_size_set(struct ath10k *ar,
 248					       u32 ce_ctrl_addr,
 249					       unsigned int n)
 250{
 251	ath10k_ce_write32(ar, ce_ctrl_addr +
 252			  ar->hw_ce_regs->sr_size_addr, n);
 253}
 254
 255static inline void ath10k_ce_src_ring_dmax_set(struct ath10k *ar,
 256					       u32 ce_ctrl_addr,
 257					       unsigned int n)
 258{
 259	struct ath10k_hw_ce_ctrl1 *ctrl_regs = ar->hw_ce_regs->ctrl1_regs;
 260
 261	u32 ctrl1_addr = ath10k_ce_read32(ar, ce_ctrl_addr +
 262					  ctrl_regs->addr);
 263
 264	ath10k_ce_write32(ar, ce_ctrl_addr + ctrl_regs->addr,
 265			  (ctrl1_addr &  ~(ctrl_regs->dmax->mask)) |
 266			  ath10k_set_ring_byte(n, ctrl_regs->dmax));
 267}
 268
 269static inline void ath10k_ce_src_ring_byte_swap_set(struct ath10k *ar,
 270						    u32 ce_ctrl_addr,
 271						    unsigned int n)
 272{
 273	struct ath10k_hw_ce_ctrl1 *ctrl_regs = ar->hw_ce_regs->ctrl1_regs;
 274
 275	u32 ctrl1_addr = ath10k_ce_read32(ar, ce_ctrl_addr +
 276					  ctrl_regs->addr);
 277
 278	ath10k_ce_write32(ar, ce_ctrl_addr + ctrl_regs->addr,
 279			  (ctrl1_addr & ~(ctrl_regs->src_ring->mask)) |
 280			  ath10k_set_ring_byte(n, ctrl_regs->src_ring));
 281}
 282
 283static inline void ath10k_ce_dest_ring_byte_swap_set(struct ath10k *ar,
 284						     u32 ce_ctrl_addr,
 285						     unsigned int n)
 286{
 287	struct ath10k_hw_ce_ctrl1 *ctrl_regs = ar->hw_ce_regs->ctrl1_regs;
 288
 289	u32 ctrl1_addr = ath10k_ce_read32(ar, ce_ctrl_addr +
 290					  ctrl_regs->addr);
 291
 292	ath10k_ce_write32(ar, ce_ctrl_addr + ctrl_regs->addr,
 293			  (ctrl1_addr & ~(ctrl_regs->dst_ring->mask)) |
 294			  ath10k_set_ring_byte(n, ctrl_regs->dst_ring));
 295}
 296
 297static inline
 298	u32 ath10k_ce_dest_ring_read_index_from_ddr(struct ath10k *ar, u32 ce_id)
 299{
 300	struct ath10k_ce *ce = ath10k_ce_priv(ar);
 301
 302	return (ce->vaddr_rri[ce_id] >> CE_DDR_DRRI_SHIFT) &
 303		CE_DDR_RRI_MASK;
 304}
 305
 306static inline u32 ath10k_ce_dest_ring_read_index_get(struct ath10k *ar,
 307						     u32 ce_ctrl_addr)
 308{
 309	struct ath10k_ce *ce = ath10k_ce_priv(ar);
 310	u32 ce_id = COPY_ENGINE_ID(ce_ctrl_addr);
 311	struct ath10k_ce_pipe *ce_state = &ce->ce_states[ce_id];
 312	u32 index;
 313
 314	if (ar->hw_params.rri_on_ddr &&
 315	    (ce_state->attr_flags & CE_ATTR_DIS_INTR))
 316		index = ath10k_ce_dest_ring_read_index_from_ddr(ar, ce_id);
 317	else
 318		index = ath10k_ce_read32(ar, ce_ctrl_addr +
 319					 ar->hw_ce_regs->current_drri_addr);
 320
 321	return index;
 322}
 323
 324static inline void ath10k_ce_dest_ring_base_addr_set(struct ath10k *ar,
 325						     u32 ce_id,
 326						     u64 addr)
 327{
 328	struct ath10k_ce *ce = ath10k_ce_priv(ar);
 329	struct ath10k_ce_pipe *ce_state = &ce->ce_states[ce_id];
 330	u32 ce_ctrl_addr = ath10k_ce_base_address(ar, ce_id);
 331	u32 addr_lo = lower_32_bits(addr);
 332
 333	ath10k_ce_write32(ar, ce_ctrl_addr +
 334			  ar->hw_ce_regs->dr_base_addr_lo, addr_lo);
 335
 336	if (ce_state->ops->ce_set_dest_ring_base_addr_hi) {
 337		ce_state->ops->ce_set_dest_ring_base_addr_hi(ar, ce_ctrl_addr,
 338							     addr);
 339	}
 340}
 341
 342static void ath10k_ce_set_dest_ring_base_addr_hi(struct ath10k *ar,
 343						 u32 ce_ctrl_addr,
 344						 u64 addr)
 345{
 346	u32 addr_hi = upper_32_bits(addr) & CE_DESC_ADDR_HI_MASK;
 347	u32 reg_value;
 348
 349	reg_value = ath10k_ce_read32(ar, ce_ctrl_addr +
 350				     ar->hw_ce_regs->dr_base_addr_hi);
 351	reg_value &= ~CE_DESC_ADDR_HI_MASK;
 352	reg_value |= addr_hi;
 353	ath10k_ce_write32(ar, ce_ctrl_addr +
 354			  ar->hw_ce_regs->dr_base_addr_hi, reg_value);
 355}
 356
 357static inline void ath10k_ce_dest_ring_size_set(struct ath10k *ar,
 358						u32 ce_ctrl_addr,
 359						unsigned int n)
 360{
 361	ath10k_ce_write32(ar, ce_ctrl_addr +
 362			  ar->hw_ce_regs->dr_size_addr, n);
 363}
 364
 365static inline void ath10k_ce_src_ring_highmark_set(struct ath10k *ar,
 366						   u32 ce_ctrl_addr,
 367						   unsigned int n)
 368{
 369	struct ath10k_hw_ce_dst_src_wm_regs *srcr_wm = ar->hw_ce_regs->wm_srcr;
 370	u32 addr = ath10k_ce_read32(ar, ce_ctrl_addr + srcr_wm->addr);
 371
 372	ath10k_ce_write32(ar, ce_ctrl_addr + srcr_wm->addr,
 373			  (addr & ~(srcr_wm->wm_high->mask)) |
 374			  (ath10k_set_ring_byte(n, srcr_wm->wm_high)));
 375}
 376
 377static inline void ath10k_ce_src_ring_lowmark_set(struct ath10k *ar,
 378						  u32 ce_ctrl_addr,
 379						  unsigned int n)
 380{
 381	struct ath10k_hw_ce_dst_src_wm_regs *srcr_wm = ar->hw_ce_regs->wm_srcr;
 382	u32 addr = ath10k_ce_read32(ar, ce_ctrl_addr + srcr_wm->addr);
 383
 384	ath10k_ce_write32(ar, ce_ctrl_addr + srcr_wm->addr,
 385			  (addr & ~(srcr_wm->wm_low->mask)) |
 386			  (ath10k_set_ring_byte(n, srcr_wm->wm_low)));
 387}
 388
 389static inline void ath10k_ce_dest_ring_highmark_set(struct ath10k *ar,
 390						    u32 ce_ctrl_addr,
 391						    unsigned int n)
 392{
 393	struct ath10k_hw_ce_dst_src_wm_regs *dstr_wm = ar->hw_ce_regs->wm_dstr;
 394	u32 addr = ath10k_ce_read32(ar, ce_ctrl_addr + dstr_wm->addr);
 395
 396	ath10k_ce_write32(ar, ce_ctrl_addr + dstr_wm->addr,
 397			  (addr & ~(dstr_wm->wm_high->mask)) |
 398			  (ath10k_set_ring_byte(n, dstr_wm->wm_high)));
 399}
 400
 401static inline void ath10k_ce_dest_ring_lowmark_set(struct ath10k *ar,
 402						   u32 ce_ctrl_addr,
 403						   unsigned int n)
 404{
 405	struct ath10k_hw_ce_dst_src_wm_regs *dstr_wm = ar->hw_ce_regs->wm_dstr;
 406	u32 addr = ath10k_ce_read32(ar, ce_ctrl_addr + dstr_wm->addr);
 407
 408	ath10k_ce_write32(ar, ce_ctrl_addr + dstr_wm->addr,
 409			  (addr & ~(dstr_wm->wm_low->mask)) |
 410			  (ath10k_set_ring_byte(n, dstr_wm->wm_low)));
 411}
 412
 413static inline void ath10k_ce_copy_complete_inter_enable(struct ath10k *ar,
 414							u32 ce_ctrl_addr)
 415{
 416	struct ath10k_hw_ce_host_ie *host_ie = ar->hw_ce_regs->host_ie;
 417
 418	u32 host_ie_addr = ath10k_ce_read32(ar, ce_ctrl_addr +
 419					    ar->hw_ce_regs->host_ie_addr);
 420
 421	ath10k_ce_write32(ar, ce_ctrl_addr + ar->hw_ce_regs->host_ie_addr,
 422			  host_ie_addr | host_ie->copy_complete->mask);
 423}
 424
 425static inline void ath10k_ce_copy_complete_intr_disable(struct ath10k *ar,
 426							u32 ce_ctrl_addr)
 427{
 428	struct ath10k_hw_ce_host_ie *host_ie = ar->hw_ce_regs->host_ie;
 429
 430	u32 host_ie_addr = ath10k_ce_read32(ar, ce_ctrl_addr +
 431					    ar->hw_ce_regs->host_ie_addr);
 432
 433	ath10k_ce_write32(ar, ce_ctrl_addr + ar->hw_ce_regs->host_ie_addr,
 434			  host_ie_addr & ~(host_ie->copy_complete->mask));
 435}
 436
 437static inline void ath10k_ce_watermark_intr_disable(struct ath10k *ar,
 438						    u32 ce_ctrl_addr)
 439{
 440	struct ath10k_hw_ce_host_wm_regs *wm_regs = ar->hw_ce_regs->wm_regs;
 441
 442	u32 host_ie_addr = ath10k_ce_read32(ar, ce_ctrl_addr +
 443					    ar->hw_ce_regs->host_ie_addr);
 444
 445	ath10k_ce_write32(ar, ce_ctrl_addr + ar->hw_ce_regs->host_ie_addr,
 446			  host_ie_addr & ~(wm_regs->wm_mask));
 447}
 448
 449static inline void ath10k_ce_error_intr_enable(struct ath10k *ar,
 450					       u32 ce_ctrl_addr)
 451{
 452	struct ath10k_hw_ce_misc_regs *misc_regs = ar->hw_ce_regs->misc_regs;
 453
 454	u32 misc_ie_addr = ath10k_ce_read32(ar, ce_ctrl_addr +
 455					    ar->hw_ce_regs->misc_ie_addr);
 456
 457	ath10k_ce_write32(ar,
 458			  ce_ctrl_addr + ar->hw_ce_regs->misc_ie_addr,
 459			  misc_ie_addr | misc_regs->err_mask);
 460}
 461
 462static inline void ath10k_ce_error_intr_disable(struct ath10k *ar,
 463						u32 ce_ctrl_addr)
 464{
 465	struct ath10k_hw_ce_misc_regs *misc_regs = ar->hw_ce_regs->misc_regs;
 466
 467	u32 misc_ie_addr = ath10k_ce_read32(ar,
 468			ce_ctrl_addr + ar->hw_ce_regs->misc_ie_addr);
 469
 470	ath10k_ce_write32(ar,
 471			  ce_ctrl_addr + ar->hw_ce_regs->misc_ie_addr,
 472			  misc_ie_addr & ~(misc_regs->err_mask));
 473}
 474
 475static inline void ath10k_ce_engine_int_status_clear(struct ath10k *ar,
 476						     u32 ce_ctrl_addr,
 477						     unsigned int mask)
 478{
 479	struct ath10k_hw_ce_host_wm_regs *wm_regs = ar->hw_ce_regs->wm_regs;
 480
 481	ath10k_ce_write32(ar, ce_ctrl_addr + wm_regs->addr, mask);
 482}
 483
 484/*
 485 * Guts of ath10k_ce_send.
 486 * The caller takes responsibility for any needed locking.
 487 */
 488static int _ath10k_ce_send_nolock(struct ath10k_ce_pipe *ce_state,
 489				  void *per_transfer_context,
 490				  dma_addr_t buffer,
 491				  unsigned int nbytes,
 492				  unsigned int transfer_id,
 493				  unsigned int flags)
 494{
 495	struct ath10k *ar = ce_state->ar;
 496	struct ath10k_ce_ring *src_ring = ce_state->src_ring;
 497	struct ce_desc *desc, sdesc;
 498	unsigned int nentries_mask = src_ring->nentries_mask;
 499	unsigned int sw_index = src_ring->sw_index;
 500	unsigned int write_index = src_ring->write_index;
 501	u32 ctrl_addr = ce_state->ctrl_addr;
 502	u32 desc_flags = 0;
 503	int ret = 0;
 504
 505	if (nbytes > ce_state->src_sz_max)
 506		ath10k_warn(ar, "%s: send more we can (nbytes: %d, max: %d)\n",
 507			    __func__, nbytes, ce_state->src_sz_max);
 508
 509	if (unlikely(CE_RING_DELTA(nentries_mask,
 510				   write_index, sw_index - 1) <= 0)) {
 511		ret = -ENOSR;
 512		goto exit;
 513	}
 514
 515	desc = CE_SRC_RING_TO_DESC(src_ring->base_addr_owner_space,
 516				   write_index);
 517
 518	desc_flags |= SM(transfer_id, CE_DESC_FLAGS_META_DATA);
 519
 520	if (flags & CE_SEND_FLAG_GATHER)
 521		desc_flags |= CE_DESC_FLAGS_GATHER;
 522	if (flags & CE_SEND_FLAG_BYTE_SWAP)
 523		desc_flags |= CE_DESC_FLAGS_BYTE_SWAP;
 524
 525	sdesc.addr   = __cpu_to_le32(buffer);
 526	sdesc.nbytes = __cpu_to_le16(nbytes);
 527	sdesc.flags  = __cpu_to_le16(desc_flags);
 528
 529	*desc = sdesc;
 530
 531	src_ring->per_transfer_context[write_index] = per_transfer_context;
 532
 533	/* Update Source Ring Write Index */
 534	write_index = CE_RING_IDX_INCR(nentries_mask, write_index);
 535
 536	/* WORKAROUND */
 537	if (!(flags & CE_SEND_FLAG_GATHER))
 538		ath10k_ce_src_ring_write_index_set(ar, ctrl_addr, write_index);
 539
 540	src_ring->write_index = write_index;
 541exit:
 542	return ret;
 543}
 544
 545static int _ath10k_ce_send_nolock_64(struct ath10k_ce_pipe *ce_state,
 546				     void *per_transfer_context,
 547				     dma_addr_t buffer,
 548				     unsigned int nbytes,
 549				     unsigned int transfer_id,
 550				     unsigned int flags)
 551{
 552	struct ath10k *ar = ce_state->ar;
 553	struct ath10k_ce_ring *src_ring = ce_state->src_ring;
 554	struct ce_desc_64 *desc, sdesc;
 555	unsigned int nentries_mask = src_ring->nentries_mask;
 556	unsigned int sw_index;
 557	unsigned int write_index = src_ring->write_index;
 558	u32 ctrl_addr = ce_state->ctrl_addr;
 559	__le32 *addr;
 560	u32 desc_flags = 0;
 561	int ret = 0;
 562
 563	if (test_bit(ATH10K_FLAG_CRASH_FLUSH, &ar->dev_flags))
 564		return -ESHUTDOWN;
 565
 566	if (nbytes > ce_state->src_sz_max)
 567		ath10k_warn(ar, "%s: send more we can (nbytes: %d, max: %d)\n",
 568			    __func__, nbytes, ce_state->src_sz_max);
 569
 570	if (ar->hw_params.rri_on_ddr)
 571		sw_index = ath10k_ce_src_ring_read_index_from_ddr(ar, ce_state->id);
 572	else
 573		sw_index = src_ring->sw_index;
 574
 575	if (unlikely(CE_RING_DELTA(nentries_mask,
 576				   write_index, sw_index - 1) <= 0)) {
 577		ret = -ENOSR;
 578		goto exit;
 579	}
 580
 581	desc = CE_SRC_RING_TO_DESC_64(src_ring->base_addr_owner_space,
 582				      write_index);
 583
 584	desc_flags |= SM(transfer_id, CE_DESC_FLAGS_META_DATA);
 585
 586	if (flags & CE_SEND_FLAG_GATHER)
 587		desc_flags |= CE_DESC_FLAGS_GATHER;
 588
 589	if (flags & CE_SEND_FLAG_BYTE_SWAP)
 590		desc_flags |= CE_DESC_FLAGS_BYTE_SWAP;
 591
 592	addr = (__le32 *)&sdesc.addr;
 593
 594	flags |= upper_32_bits(buffer) & CE_DESC_ADDR_HI_MASK;
 595	addr[0] = __cpu_to_le32(buffer);
 596	addr[1] = __cpu_to_le32(flags);
 597	if (flags & CE_SEND_FLAG_GATHER)
 598		addr[1] |= __cpu_to_le32(CE_WCN3990_DESC_FLAGS_GATHER);
 599	else
 600		addr[1] &= ~(__cpu_to_le32(CE_WCN3990_DESC_FLAGS_GATHER));
 601
 602	sdesc.nbytes = __cpu_to_le16(nbytes);
 603	sdesc.flags  = __cpu_to_le16(desc_flags);
 604
 605	*desc = sdesc;
 606
 607	src_ring->per_transfer_context[write_index] = per_transfer_context;
 608
 609	/* Update Source Ring Write Index */
 610	write_index = CE_RING_IDX_INCR(nentries_mask, write_index);
 611
 612	if (!(flags & CE_SEND_FLAG_GATHER)) {
 613		if (ar->hw_params.shadow_reg_support)
 614			ath10k_ce_shadow_src_ring_write_index_set(ar, ce_state,
 615								  write_index);
 616		else
 617			ath10k_ce_src_ring_write_index_set(ar, ctrl_addr,
 618							   write_index);
 619	}
 620
 621	src_ring->write_index = write_index;
 622exit:
 623	return ret;
 624}
 625
 626int ath10k_ce_send_nolock(struct ath10k_ce_pipe *ce_state,
 627			  void *per_transfer_context,
 628			  dma_addr_t buffer,
 629			  unsigned int nbytes,
 630			  unsigned int transfer_id,
 631			  unsigned int flags)
 632{
 633	return ce_state->ops->ce_send_nolock(ce_state, per_transfer_context,
 634				    buffer, nbytes, transfer_id, flags);
 635}
 636EXPORT_SYMBOL(ath10k_ce_send_nolock);
 637
 638void __ath10k_ce_send_revert(struct ath10k_ce_pipe *pipe)
 639{
 640	struct ath10k *ar = pipe->ar;
 641	struct ath10k_ce *ce = ath10k_ce_priv(ar);
 642	struct ath10k_ce_ring *src_ring = pipe->src_ring;
 643	u32 ctrl_addr = pipe->ctrl_addr;
 644
 645	lockdep_assert_held(&ce->ce_lock);
 646
 647	/*
 648	 * This function must be called only if there is an incomplete
 649	 * scatter-gather transfer (before index register is updated)
 650	 * that needs to be cleaned up.
 651	 */
 652	if (WARN_ON_ONCE(src_ring->write_index == src_ring->sw_index))
 653		return;
 654
 655	if (WARN_ON_ONCE(src_ring->write_index ==
 656			 ath10k_ce_src_ring_write_index_get(ar, ctrl_addr)))
 657		return;
 658
 659	src_ring->write_index--;
 660	src_ring->write_index &= src_ring->nentries_mask;
 661
 662	src_ring->per_transfer_context[src_ring->write_index] = NULL;
 663}
 664EXPORT_SYMBOL(__ath10k_ce_send_revert);
 665
 666int ath10k_ce_send(struct ath10k_ce_pipe *ce_state,
 667		   void *per_transfer_context,
 668		   dma_addr_t buffer,
 669		   unsigned int nbytes,
 670		   unsigned int transfer_id,
 671		   unsigned int flags)
 672{
 673	struct ath10k *ar = ce_state->ar;
 674	struct ath10k_ce *ce = ath10k_ce_priv(ar);
 675	int ret;
 676
 677	spin_lock_bh(&ce->ce_lock);
 678	ret = ath10k_ce_send_nolock(ce_state, per_transfer_context,
 679				    buffer, nbytes, transfer_id, flags);
 680	spin_unlock_bh(&ce->ce_lock);
 681
 682	return ret;
 683}
 684EXPORT_SYMBOL(ath10k_ce_send);
 685
 686int ath10k_ce_num_free_src_entries(struct ath10k_ce_pipe *pipe)
 687{
 688	struct ath10k *ar = pipe->ar;
 689	struct ath10k_ce *ce = ath10k_ce_priv(ar);
 690	int delta;
 691
 692	spin_lock_bh(&ce->ce_lock);
 693	delta = CE_RING_DELTA(pipe->src_ring->nentries_mask,
 694			      pipe->src_ring->write_index,
 695			      pipe->src_ring->sw_index - 1);
 696	spin_unlock_bh(&ce->ce_lock);
 697
 698	return delta;
 699}
 700EXPORT_SYMBOL(ath10k_ce_num_free_src_entries);
 701
 702int __ath10k_ce_rx_num_free_bufs(struct ath10k_ce_pipe *pipe)
 703{
 704	struct ath10k *ar = pipe->ar;
 705	struct ath10k_ce *ce = ath10k_ce_priv(ar);
 706	struct ath10k_ce_ring *dest_ring = pipe->dest_ring;
 707	unsigned int nentries_mask = dest_ring->nentries_mask;
 708	unsigned int write_index = dest_ring->write_index;
 709	unsigned int sw_index = dest_ring->sw_index;
 710
 711	lockdep_assert_held(&ce->ce_lock);
 712
 713	return CE_RING_DELTA(nentries_mask, write_index, sw_index - 1);
 714}
 715EXPORT_SYMBOL(__ath10k_ce_rx_num_free_bufs);
 716
 717static int __ath10k_ce_rx_post_buf(struct ath10k_ce_pipe *pipe, void *ctx,
 718				   dma_addr_t paddr)
 719{
 720	struct ath10k *ar = pipe->ar;
 721	struct ath10k_ce *ce = ath10k_ce_priv(ar);
 722	struct ath10k_ce_ring *dest_ring = pipe->dest_ring;
 723	unsigned int nentries_mask = dest_ring->nentries_mask;
 724	unsigned int write_index = dest_ring->write_index;
 725	unsigned int sw_index = dest_ring->sw_index;
 726	struct ce_desc *base = dest_ring->base_addr_owner_space;
 727	struct ce_desc *desc = CE_DEST_RING_TO_DESC(base, write_index);
 728	u32 ctrl_addr = pipe->ctrl_addr;
 729
 730	lockdep_assert_held(&ce->ce_lock);
 731
 732	if ((pipe->id != 5) &&
 733	    CE_RING_DELTA(nentries_mask, write_index, sw_index - 1) == 0)
 734		return -ENOSPC;
 735
 736	desc->addr = __cpu_to_le32(paddr);
 737	desc->nbytes = 0;
 738
 739	dest_ring->per_transfer_context[write_index] = ctx;
 740	write_index = CE_RING_IDX_INCR(nentries_mask, write_index);
 741	ath10k_ce_dest_ring_write_index_set(ar, ctrl_addr, write_index);
 742	dest_ring->write_index = write_index;
 743
 744	return 0;
 745}
 746
 747static int __ath10k_ce_rx_post_buf_64(struct ath10k_ce_pipe *pipe,
 748				      void *ctx,
 749				      dma_addr_t paddr)
 750{
 751	struct ath10k *ar = pipe->ar;
 752	struct ath10k_ce *ce = ath10k_ce_priv(ar);
 753	struct ath10k_ce_ring *dest_ring = pipe->dest_ring;
 754	unsigned int nentries_mask = dest_ring->nentries_mask;
 755	unsigned int write_index = dest_ring->write_index;
 756	unsigned int sw_index = dest_ring->sw_index;
 757	struct ce_desc_64 *base = dest_ring->base_addr_owner_space;
 758	struct ce_desc_64 *desc =
 759			CE_DEST_RING_TO_DESC_64(base, write_index);
 760	u32 ctrl_addr = pipe->ctrl_addr;
 761
 762	lockdep_assert_held(&ce->ce_lock);
 763
 764	if (CE_RING_DELTA(nentries_mask, write_index, sw_index - 1) == 0)
 765		return -ENOSPC;
 766
 767	desc->addr = __cpu_to_le64(paddr);
 768	desc->addr &= __cpu_to_le64(CE_DESC_ADDR_MASK);
 769
 770	desc->nbytes = 0;
 771
 772	dest_ring->per_transfer_context[write_index] = ctx;
 773	write_index = CE_RING_IDX_INCR(nentries_mask, write_index);
 774	ath10k_ce_dest_ring_write_index_set(ar, ctrl_addr, write_index);
 775	dest_ring->write_index = write_index;
 776
 777	return 0;
 778}
 779
 780void ath10k_ce_rx_update_write_idx(struct ath10k_ce_pipe *pipe, u32 nentries)
 781{
 782	struct ath10k *ar = pipe->ar;
 783	struct ath10k_ce_ring *dest_ring = pipe->dest_ring;
 784	unsigned int nentries_mask = dest_ring->nentries_mask;
 785	unsigned int write_index = dest_ring->write_index;
 786	u32 ctrl_addr = pipe->ctrl_addr;
 787	u32 cur_write_idx = ath10k_ce_dest_ring_write_index_get(ar, ctrl_addr);
 788
 789	/* Prevent CE ring stuck issue that will occur when ring is full.
 790	 * Make sure that write index is 1 less than read index.
 791	 */
 792	if (((cur_write_idx + nentries) & nentries_mask) == dest_ring->sw_index)
 793		nentries -= 1;
 794
 795	write_index = CE_RING_IDX_ADD(nentries_mask, write_index, nentries);
 796	ath10k_ce_dest_ring_write_index_set(ar, ctrl_addr, write_index);
 797	dest_ring->write_index = write_index;
 798}
 799EXPORT_SYMBOL(ath10k_ce_rx_update_write_idx);
 800
 801int ath10k_ce_rx_post_buf(struct ath10k_ce_pipe *pipe, void *ctx,
 802			  dma_addr_t paddr)
 803{
 804	struct ath10k *ar = pipe->ar;
 805	struct ath10k_ce *ce = ath10k_ce_priv(ar);
 806	int ret;
 807
 808	spin_lock_bh(&ce->ce_lock);
 809	ret = pipe->ops->ce_rx_post_buf(pipe, ctx, paddr);
 810	spin_unlock_bh(&ce->ce_lock);
 811
 812	return ret;
 813}
 814EXPORT_SYMBOL(ath10k_ce_rx_post_buf);
 815
 816/*
 817 * Guts of ath10k_ce_completed_recv_next.
 818 * The caller takes responsibility for any necessary locking.
 819 */
 820static int
 821	 _ath10k_ce_completed_recv_next_nolock(struct ath10k_ce_pipe *ce_state,
 822					       void **per_transfer_contextp,
 823					       unsigned int *nbytesp)
 824{
 825	struct ath10k_ce_ring *dest_ring = ce_state->dest_ring;
 826	unsigned int nentries_mask = dest_ring->nentries_mask;
 827	unsigned int sw_index = dest_ring->sw_index;
 828
 829	struct ce_desc *base = dest_ring->base_addr_owner_space;
 830	struct ce_desc *desc = CE_DEST_RING_TO_DESC(base, sw_index);
 831	struct ce_desc sdesc;
 832	u16 nbytes;
 833
 834	/* Copy in one go for performance reasons */
 835	sdesc = *desc;
 836
 837	nbytes = __le16_to_cpu(sdesc.nbytes);
 838	if (nbytes == 0) {
 839		/*
 840		 * This closes a relatively unusual race where the Host
 841		 * sees the updated DRRI before the update to the
 842		 * corresponding descriptor has completed. We treat this
 843		 * as a descriptor that is not yet done.
 844		 */
 845		return -EIO;
 846	}
 847
 848	desc->nbytes = 0;
 849
 850	/* Return data from completed destination descriptor */
 851	*nbytesp = nbytes;
 852
 853	if (per_transfer_contextp)
 854		*per_transfer_contextp =
 855			dest_ring->per_transfer_context[sw_index];
 856
 857	/* Copy engine 5 (HTT Rx) will reuse the same transfer context.
 858	 * So update transfer context all CEs except CE5.
 859	 */
 860	if (ce_state->id != 5)
 861		dest_ring->per_transfer_context[sw_index] = NULL;
 862
 863	/* Update sw_index */
 864	sw_index = CE_RING_IDX_INCR(nentries_mask, sw_index);
 865	dest_ring->sw_index = sw_index;
 866
 867	return 0;
 868}
 869
 870static int
 871_ath10k_ce_completed_recv_next_nolock_64(struct ath10k_ce_pipe *ce_state,
 872					 void **per_transfer_contextp,
 873					 unsigned int *nbytesp)
 874{
 875	struct ath10k_ce_ring *dest_ring = ce_state->dest_ring;
 876	unsigned int nentries_mask = dest_ring->nentries_mask;
 877	unsigned int sw_index = dest_ring->sw_index;
 878	struct ce_desc_64 *base = dest_ring->base_addr_owner_space;
 879	struct ce_desc_64 *desc =
 880		CE_DEST_RING_TO_DESC_64(base, sw_index);
 881	struct ce_desc_64 sdesc;
 882	u16 nbytes;
 883
 884	/* Copy in one go for performance reasons */
 885	sdesc = *desc;
 886
 887	nbytes = __le16_to_cpu(sdesc.nbytes);
 888	if (nbytes == 0) {
 889		/* This closes a relatively unusual race where the Host
 890		 * sees the updated DRRI before the update to the
 891		 * corresponding descriptor has completed. We treat this
 892		 * as a descriptor that is not yet done.
 893		 */
 894		return -EIO;
 895	}
 896
 897	desc->nbytes = 0;
 898
 899	/* Return data from completed destination descriptor */
 900	*nbytesp = nbytes;
 901
 902	if (per_transfer_contextp)
 903		*per_transfer_contextp =
 904			dest_ring->per_transfer_context[sw_index];
 905
 906	/* Copy engine 5 (HTT Rx) will reuse the same transfer context.
 907	 * So update transfer context all CEs except CE5.
 908	 */
 909	if (ce_state->id != 5)
 910		dest_ring->per_transfer_context[sw_index] = NULL;
 911
 912	/* Update sw_index */
 913	sw_index = CE_RING_IDX_INCR(nentries_mask, sw_index);
 914	dest_ring->sw_index = sw_index;
 915
 916	return 0;
 917}
 918
 919int ath10k_ce_completed_recv_next_nolock(struct ath10k_ce_pipe *ce_state,
 920					 void **per_transfer_ctx,
 921					 unsigned int *nbytesp)
 922{
 923	return ce_state->ops->ce_completed_recv_next_nolock(ce_state,
 924							    per_transfer_ctx,
 925							    nbytesp);
 926}
 927EXPORT_SYMBOL(ath10k_ce_completed_recv_next_nolock);
 928
 929int ath10k_ce_completed_recv_next(struct ath10k_ce_pipe *ce_state,
 930				  void **per_transfer_contextp,
 931				  unsigned int *nbytesp)
 932{
 933	struct ath10k *ar = ce_state->ar;
 934	struct ath10k_ce *ce = ath10k_ce_priv(ar);
 935	int ret;
 936
 937	spin_lock_bh(&ce->ce_lock);
 938	ret = ce_state->ops->ce_completed_recv_next_nolock(ce_state,
 939						   per_transfer_contextp,
 940						   nbytesp);
 941
 942	spin_unlock_bh(&ce->ce_lock);
 943
 944	return ret;
 945}
 946EXPORT_SYMBOL(ath10k_ce_completed_recv_next);
 947
 948static int _ath10k_ce_revoke_recv_next(struct ath10k_ce_pipe *ce_state,
 949				       void **per_transfer_contextp,
 950				       dma_addr_t *bufferp)
 951{
 952	struct ath10k_ce_ring *dest_ring;
 953	unsigned int nentries_mask;
 954	unsigned int sw_index;
 955	unsigned int write_index;
 956	int ret;
 957	struct ath10k *ar;
 958	struct ath10k_ce *ce;
 959
 960	dest_ring = ce_state->dest_ring;
 961
 962	if (!dest_ring)
 963		return -EIO;
 964
 965	ar = ce_state->ar;
 966	ce = ath10k_ce_priv(ar);
 967
 968	spin_lock_bh(&ce->ce_lock);
 969
 970	nentries_mask = dest_ring->nentries_mask;
 971	sw_index = dest_ring->sw_index;
 972	write_index = dest_ring->write_index;
 973	if (write_index != sw_index) {
 974		struct ce_desc *base = dest_ring->base_addr_owner_space;
 975		struct ce_desc *desc = CE_DEST_RING_TO_DESC(base, sw_index);
 976
 977		/* Return data from completed destination descriptor */
 978		*bufferp = __le32_to_cpu(desc->addr);
 979
 980		if (per_transfer_contextp)
 981			*per_transfer_contextp =
 982				dest_ring->per_transfer_context[sw_index];
 983
 984		/* sanity */
 985		dest_ring->per_transfer_context[sw_index] = NULL;
 986		desc->nbytes = 0;
 987
 988		/* Update sw_index */
 989		sw_index = CE_RING_IDX_INCR(nentries_mask, sw_index);
 990		dest_ring->sw_index = sw_index;
 991		ret = 0;
 992	} else {
 993		ret = -EIO;
 994	}
 995
 996	spin_unlock_bh(&ce->ce_lock);
 997
 998	return ret;
 999}
1000
1001static int _ath10k_ce_revoke_recv_next_64(struct ath10k_ce_pipe *ce_state,
1002					  void **per_transfer_contextp,
1003					  dma_addr_t *bufferp)
1004{
1005	struct ath10k_ce_ring *dest_ring;
1006	unsigned int nentries_mask;
1007	unsigned int sw_index;
1008	unsigned int write_index;
1009	int ret;
1010	struct ath10k *ar;
1011	struct ath10k_ce *ce;
1012
1013	dest_ring = ce_state->dest_ring;
1014
1015	if (!dest_ring)
1016		return -EIO;
1017
1018	ar = ce_state->ar;
1019	ce = ath10k_ce_priv(ar);
1020
1021	spin_lock_bh(&ce->ce_lock);
1022
1023	nentries_mask = dest_ring->nentries_mask;
1024	sw_index = dest_ring->sw_index;
1025	write_index = dest_ring->write_index;
1026	if (write_index != sw_index) {
1027		struct ce_desc_64 *base = dest_ring->base_addr_owner_space;
1028		struct ce_desc_64 *desc =
1029			CE_DEST_RING_TO_DESC_64(base, sw_index);
1030
1031		/* Return data from completed destination descriptor */
1032		*bufferp = __le64_to_cpu(desc->addr);
1033
1034		if (per_transfer_contextp)
1035			*per_transfer_contextp =
1036				dest_ring->per_transfer_context[sw_index];
1037
1038		/* sanity */
1039		dest_ring->per_transfer_context[sw_index] = NULL;
1040		desc->nbytes = 0;
1041
1042		/* Update sw_index */
1043		sw_index = CE_RING_IDX_INCR(nentries_mask, sw_index);
1044		dest_ring->sw_index = sw_index;
1045		ret = 0;
1046	} else {
1047		ret = -EIO;
1048	}
1049
1050	spin_unlock_bh(&ce->ce_lock);
1051
1052	return ret;
1053}
1054
1055int ath10k_ce_revoke_recv_next(struct ath10k_ce_pipe *ce_state,
1056			       void **per_transfer_contextp,
1057			       dma_addr_t *bufferp)
1058{
1059	return ce_state->ops->ce_revoke_recv_next(ce_state,
1060						  per_transfer_contextp,
1061						  bufferp);
1062}
1063EXPORT_SYMBOL(ath10k_ce_revoke_recv_next);
1064
1065/*
1066 * Guts of ath10k_ce_completed_send_next.
1067 * The caller takes responsibility for any necessary locking.
1068 */
1069static int _ath10k_ce_completed_send_next_nolock(struct ath10k_ce_pipe *ce_state,
1070						 void **per_transfer_contextp)
1071{
1072	struct ath10k_ce_ring *src_ring = ce_state->src_ring;
1073	u32 ctrl_addr = ce_state->ctrl_addr;
1074	struct ath10k *ar = ce_state->ar;
1075	unsigned int nentries_mask = src_ring->nentries_mask;
1076	unsigned int sw_index = src_ring->sw_index;
1077	unsigned int read_index;
1078	struct ce_desc *desc;
1079
1080	if (src_ring->hw_index == sw_index) {
1081		/*
1082		 * The SW completion index has caught up with the cached
1083		 * version of the HW completion index.
1084		 * Update the cached HW completion index to see whether
1085		 * the SW has really caught up to the HW, or if the cached
1086		 * value of the HW index has become stale.
1087		 */
1088
1089		read_index = ath10k_ce_src_ring_read_index_get(ar, ctrl_addr);
1090		if (read_index == 0xffffffff)
1091			return -ENODEV;
1092
1093		read_index &= nentries_mask;
1094		src_ring->hw_index = read_index;
1095	}
1096
1097	if (ar->hw_params.rri_on_ddr)
1098		read_index = ath10k_ce_src_ring_read_index_get(ar, ctrl_addr);
1099	else
1100		read_index = src_ring->hw_index;
1101
1102	if (read_index == sw_index)
1103		return -EIO;
1104
1105	if (per_transfer_contextp)
1106		*per_transfer_contextp =
1107			src_ring->per_transfer_context[sw_index];
1108
1109	/* sanity */
1110	src_ring->per_transfer_context[sw_index] = NULL;
1111	desc = CE_SRC_RING_TO_DESC(src_ring->base_addr_owner_space,
1112				   sw_index);
1113	desc->nbytes = 0;
1114
1115	/* Update sw_index */
1116	sw_index = CE_RING_IDX_INCR(nentries_mask, sw_index);
1117	src_ring->sw_index = sw_index;
1118
1119	return 0;
1120}
1121
1122static int _ath10k_ce_completed_send_next_nolock_64(struct ath10k_ce_pipe *ce_state,
1123						    void **per_transfer_contextp)
1124{
1125	struct ath10k_ce_ring *src_ring = ce_state->src_ring;
1126	u32 ctrl_addr = ce_state->ctrl_addr;
1127	struct ath10k *ar = ce_state->ar;
1128	unsigned int nentries_mask = src_ring->nentries_mask;
1129	unsigned int sw_index = src_ring->sw_index;
1130	unsigned int read_index;
1131	struct ce_desc_64 *desc;
1132
1133	if (src_ring->hw_index == sw_index) {
1134		/*
1135		 * The SW completion index has caught up with the cached
1136		 * version of the HW completion index.
1137		 * Update the cached HW completion index to see whether
1138		 * the SW has really caught up to the HW, or if the cached
1139		 * value of the HW index has become stale.
1140		 */
1141
1142		read_index = ath10k_ce_src_ring_read_index_get(ar, ctrl_addr);
1143		if (read_index == 0xffffffff)
1144			return -ENODEV;
1145
1146		read_index &= nentries_mask;
1147		src_ring->hw_index = read_index;
1148	}
1149
1150	if (ar->hw_params.rri_on_ddr)
1151		read_index = ath10k_ce_src_ring_read_index_get(ar, ctrl_addr);
1152	else
1153		read_index = src_ring->hw_index;
1154
1155	if (read_index == sw_index)
1156		return -EIO;
1157
1158	if (per_transfer_contextp)
1159		*per_transfer_contextp =
1160			src_ring->per_transfer_context[sw_index];
1161
1162	/* sanity */
1163	src_ring->per_transfer_context[sw_index] = NULL;
1164	desc = CE_SRC_RING_TO_DESC_64(src_ring->base_addr_owner_space,
1165				      sw_index);
1166	desc->nbytes = 0;
1167
1168	/* Update sw_index */
1169	sw_index = CE_RING_IDX_INCR(nentries_mask, sw_index);
1170	src_ring->sw_index = sw_index;
1171
1172	return 0;
1173}
1174
1175int ath10k_ce_completed_send_next_nolock(struct ath10k_ce_pipe *ce_state,
1176					 void **per_transfer_contextp)
1177{
1178	return ce_state->ops->ce_completed_send_next_nolock(ce_state,
1179							    per_transfer_contextp);
1180}
1181EXPORT_SYMBOL(ath10k_ce_completed_send_next_nolock);
1182
1183static void ath10k_ce_extract_desc_data(struct ath10k *ar,
1184					struct ath10k_ce_ring *src_ring,
1185					u32 sw_index,
1186					dma_addr_t *bufferp,
1187					u32 *nbytesp,
1188					u32 *transfer_idp)
1189{
1190		struct ce_desc *base = src_ring->base_addr_owner_space;
1191		struct ce_desc *desc = CE_SRC_RING_TO_DESC(base, sw_index);
1192
1193		/* Return data from completed source descriptor */
1194		*bufferp = __le32_to_cpu(desc->addr);
1195		*nbytesp = __le16_to_cpu(desc->nbytes);
1196		*transfer_idp = MS(__le16_to_cpu(desc->flags),
1197				   CE_DESC_FLAGS_META_DATA);
1198}
1199
1200static void ath10k_ce_extract_desc_data_64(struct ath10k *ar,
1201					   struct ath10k_ce_ring *src_ring,
1202					   u32 sw_index,
1203					   dma_addr_t *bufferp,
1204					   u32 *nbytesp,
1205					   u32 *transfer_idp)
1206{
1207		struct ce_desc_64 *base = src_ring->base_addr_owner_space;
1208		struct ce_desc_64 *desc =
1209			CE_SRC_RING_TO_DESC_64(base, sw_index);
1210
1211		/* Return data from completed source descriptor */
1212		*bufferp = __le64_to_cpu(desc->addr);
1213		*nbytesp = __le16_to_cpu(desc->nbytes);
1214		*transfer_idp = MS(__le16_to_cpu(desc->flags),
1215				   CE_DESC_FLAGS_META_DATA);
1216}
1217
1218/* NB: Modeled after ath10k_ce_completed_send_next */
1219int ath10k_ce_cancel_send_next(struct ath10k_ce_pipe *ce_state,
1220			       void **per_transfer_contextp,
1221			       dma_addr_t *bufferp,
1222			       unsigned int *nbytesp,
1223			       unsigned int *transfer_idp)
1224{
1225	struct ath10k_ce_ring *src_ring;
1226	unsigned int nentries_mask;
1227	unsigned int sw_index;
1228	unsigned int write_index;
1229	int ret;
1230	struct ath10k *ar;
1231	struct ath10k_ce *ce;
1232
1233	src_ring = ce_state->src_ring;
1234
1235	if (!src_ring)
1236		return -EIO;
1237
1238	ar = ce_state->ar;
1239	ce = ath10k_ce_priv(ar);
1240
1241	spin_lock_bh(&ce->ce_lock);
1242
1243	nentries_mask = src_ring->nentries_mask;
1244	sw_index = src_ring->sw_index;
1245	write_index = src_ring->write_index;
1246
1247	if (write_index != sw_index) {
1248		ce_state->ops->ce_extract_desc_data(ar, src_ring, sw_index,
1249						    bufferp, nbytesp,
1250						    transfer_idp);
1251
1252		if (per_transfer_contextp)
1253			*per_transfer_contextp =
1254				src_ring->per_transfer_context[sw_index];
1255
1256		/* sanity */
1257		src_ring->per_transfer_context[sw_index] = NULL;
1258
1259		/* Update sw_index */
1260		sw_index = CE_RING_IDX_INCR(nentries_mask, sw_index);
1261		src_ring->sw_index = sw_index;
1262		ret = 0;
1263	} else {
1264		ret = -EIO;
1265	}
1266
1267	spin_unlock_bh(&ce->ce_lock);
1268
1269	return ret;
1270}
1271EXPORT_SYMBOL(ath10k_ce_cancel_send_next);
1272
1273int ath10k_ce_completed_send_next(struct ath10k_ce_pipe *ce_state,
1274				  void **per_transfer_contextp)
1275{
1276	struct ath10k *ar = ce_state->ar;
1277	struct ath10k_ce *ce = ath10k_ce_priv(ar);
1278	int ret;
1279
1280	spin_lock_bh(&ce->ce_lock);
1281	ret = ath10k_ce_completed_send_next_nolock(ce_state,
1282						   per_transfer_contextp);
1283	spin_unlock_bh(&ce->ce_lock);
1284
1285	return ret;
1286}
1287EXPORT_SYMBOL(ath10k_ce_completed_send_next);
1288
1289/*
1290 * Guts of interrupt handler for per-engine interrupts on a particular CE.
1291 *
1292 * Invokes registered callbacks for recv_complete,
1293 * send_complete, and watermarks.
1294 */
1295void ath10k_ce_per_engine_service(struct ath10k *ar, unsigned int ce_id)
1296{
1297	struct ath10k_ce *ce = ath10k_ce_priv(ar);
1298	struct ath10k_ce_pipe *ce_state = &ce->ce_states[ce_id];
1299	struct ath10k_hw_ce_host_wm_regs *wm_regs = ar->hw_ce_regs->wm_regs;
1300	u32 ctrl_addr = ce_state->ctrl_addr;
1301
1302	spin_lock_bh(&ce->ce_lock);
1303
1304	/* Clear the copy-complete interrupts that will be handled here. */
 
 
 
 
 
 
 
1305	ath10k_ce_engine_int_status_clear(ar, ctrl_addr,
1306					  wm_regs->cc_mask);
1307
1308	spin_unlock_bh(&ce->ce_lock);
1309
1310	if (ce_state->recv_cb)
1311		ce_state->recv_cb(ce_state);
1312
1313	if (ce_state->send_cb)
1314		ce_state->send_cb(ce_state);
1315
1316	spin_lock_bh(&ce->ce_lock);
1317
1318	/*
1319	 * Misc CE interrupts are not being handled, but still need
1320	 * to be cleared.
1321	 */
1322	ath10k_ce_engine_int_status_clear(ar, ctrl_addr, wm_regs->wm_mask);
1323
1324	spin_unlock_bh(&ce->ce_lock);
1325}
1326EXPORT_SYMBOL(ath10k_ce_per_engine_service);
1327
1328/*
1329 * Handler for per-engine interrupts on ALL active CEs.
1330 * This is used in cases where the system is sharing a
1331 * single interrput for all CEs
1332 */
1333
1334void ath10k_ce_per_engine_service_any(struct ath10k *ar)
1335{
1336	int ce_id;
1337	u32 intr_summary;
1338
1339	intr_summary = ath10k_ce_interrupt_summary(ar);
1340
1341	for (ce_id = 0; intr_summary && (ce_id < CE_COUNT); ce_id++) {
1342		if (intr_summary & (1 << ce_id))
1343			intr_summary &= ~(1 << ce_id);
1344		else
1345			/* no intr pending on this CE */
1346			continue;
1347
1348		ath10k_ce_per_engine_service(ar, ce_id);
1349	}
1350}
1351EXPORT_SYMBOL(ath10k_ce_per_engine_service_any);
1352
1353/*
1354 * Adjust interrupts for the copy complete handler.
1355 * If it's needed for either send or recv, then unmask
1356 * this interrupt; otherwise, mask it.
1357 *
1358 * Called with ce_lock held.
1359 */
1360static void ath10k_ce_per_engine_handler_adjust(struct ath10k_ce_pipe *ce_state)
1361{
1362	u32 ctrl_addr = ce_state->ctrl_addr;
1363	struct ath10k *ar = ce_state->ar;
1364	bool disable_copy_compl_intr = ce_state->attr_flags & CE_ATTR_DIS_INTR;
1365
1366	if ((!disable_copy_compl_intr) &&
1367	    (ce_state->send_cb || ce_state->recv_cb))
1368		ath10k_ce_copy_complete_inter_enable(ar, ctrl_addr);
1369	else
1370		ath10k_ce_copy_complete_intr_disable(ar, ctrl_addr);
1371
1372	ath10k_ce_watermark_intr_disable(ar, ctrl_addr);
1373}
1374
1375int ath10k_ce_disable_interrupts(struct ath10k *ar)
1376{
1377	struct ath10k_ce *ce = ath10k_ce_priv(ar);
1378	struct ath10k_ce_pipe *ce_state;
1379	u32 ctrl_addr;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1380	int ce_id;
1381
1382	for (ce_id = 0; ce_id < CE_COUNT; ce_id++) {
1383		ce_state  = &ce->ce_states[ce_id];
1384		if (ce_state->attr_flags & CE_ATTR_POLL)
1385			continue;
1386
1387		ctrl_addr = ath10k_ce_base_address(ar, ce_id);
 
 
 
1388
1389		ath10k_ce_copy_complete_intr_disable(ar, ctrl_addr);
1390		ath10k_ce_error_intr_disable(ar, ctrl_addr);
1391		ath10k_ce_watermark_intr_disable(ar, ctrl_addr);
1392	}
1393
1394	return 0;
1395}
1396EXPORT_SYMBOL(ath10k_ce_disable_interrupts);
1397
1398void ath10k_ce_enable_interrupts(struct ath10k *ar)
1399{
1400	struct ath10k_ce *ce = ath10k_ce_priv(ar);
1401	int ce_id;
1402	struct ath10k_ce_pipe *ce_state;
1403
1404	/* Enable interrupts for copy engine that
1405	 * are not using polling mode.
1406	 */
1407	for (ce_id = 0; ce_id < CE_COUNT; ce_id++) {
1408		ce_state  = &ce->ce_states[ce_id];
1409		if (ce_state->attr_flags & CE_ATTR_POLL)
1410			continue;
1411
1412		ath10k_ce_per_engine_handler_adjust(ce_state);
1413	}
1414}
1415EXPORT_SYMBOL(ath10k_ce_enable_interrupts);
1416
1417static int ath10k_ce_init_src_ring(struct ath10k *ar,
1418				   unsigned int ce_id,
1419				   const struct ce_attr *attr)
1420{
1421	struct ath10k_ce *ce = ath10k_ce_priv(ar);
1422	struct ath10k_ce_pipe *ce_state = &ce->ce_states[ce_id];
1423	struct ath10k_ce_ring *src_ring = ce_state->src_ring;
1424	u32 nentries, ctrl_addr = ath10k_ce_base_address(ar, ce_id);
1425
1426	nentries = roundup_pow_of_two(attr->src_nentries);
1427
1428	if (ar->hw_params.target_64bit)
1429		memset(src_ring->base_addr_owner_space, 0,
1430		       nentries * sizeof(struct ce_desc_64));
1431	else
1432		memset(src_ring->base_addr_owner_space, 0,
1433		       nentries * sizeof(struct ce_desc));
1434
1435	src_ring->sw_index = ath10k_ce_src_ring_read_index_get(ar, ctrl_addr);
1436	src_ring->sw_index &= src_ring->nentries_mask;
1437	src_ring->hw_index = src_ring->sw_index;
1438
1439	src_ring->write_index =
1440		ath10k_ce_src_ring_write_index_get(ar, ctrl_addr);
1441	src_ring->write_index &= src_ring->nentries_mask;
1442
1443	ath10k_ce_src_ring_base_addr_set(ar, ce_id,
1444					 src_ring->base_addr_ce_space);
1445	ath10k_ce_src_ring_size_set(ar, ctrl_addr, nentries);
1446	ath10k_ce_src_ring_dmax_set(ar, ctrl_addr, attr->src_sz_max);
1447	ath10k_ce_src_ring_byte_swap_set(ar, ctrl_addr, 0);
1448	ath10k_ce_src_ring_lowmark_set(ar, ctrl_addr, 0);
1449	ath10k_ce_src_ring_highmark_set(ar, ctrl_addr, nentries);
1450
1451	ath10k_dbg(ar, ATH10K_DBG_BOOT,
1452		   "boot init ce src ring id %d entries %d base_addr %pK\n",
1453		   ce_id, nentries, src_ring->base_addr_owner_space);
1454
1455	return 0;
1456}
1457
1458static int ath10k_ce_init_dest_ring(struct ath10k *ar,
1459				    unsigned int ce_id,
1460				    const struct ce_attr *attr)
1461{
1462	struct ath10k_ce *ce = ath10k_ce_priv(ar);
1463	struct ath10k_ce_pipe *ce_state = &ce->ce_states[ce_id];
1464	struct ath10k_ce_ring *dest_ring = ce_state->dest_ring;
1465	u32 nentries, ctrl_addr = ath10k_ce_base_address(ar, ce_id);
1466
1467	nentries = roundup_pow_of_two(attr->dest_nentries);
1468
1469	if (ar->hw_params.target_64bit)
1470		memset(dest_ring->base_addr_owner_space, 0,
1471		       nentries * sizeof(struct ce_desc_64));
1472	else
1473		memset(dest_ring->base_addr_owner_space, 0,
1474		       nentries * sizeof(struct ce_desc));
1475
1476	dest_ring->sw_index = ath10k_ce_dest_ring_read_index_get(ar, ctrl_addr);
1477	dest_ring->sw_index &= dest_ring->nentries_mask;
1478	dest_ring->write_index =
1479		ath10k_ce_dest_ring_write_index_get(ar, ctrl_addr);
1480	dest_ring->write_index &= dest_ring->nentries_mask;
1481
1482	ath10k_ce_dest_ring_base_addr_set(ar, ce_id,
1483					  dest_ring->base_addr_ce_space);
1484	ath10k_ce_dest_ring_size_set(ar, ctrl_addr, nentries);
1485	ath10k_ce_dest_ring_byte_swap_set(ar, ctrl_addr, 0);
1486	ath10k_ce_dest_ring_lowmark_set(ar, ctrl_addr, 0);
1487	ath10k_ce_dest_ring_highmark_set(ar, ctrl_addr, nentries);
1488
1489	ath10k_dbg(ar, ATH10K_DBG_BOOT,
1490		   "boot ce dest ring id %d entries %d base_addr %pK\n",
1491		   ce_id, nentries, dest_ring->base_addr_owner_space);
1492
1493	return 0;
1494}
1495
1496static int ath10k_ce_alloc_shadow_base(struct ath10k *ar,
1497				       struct ath10k_ce_ring *src_ring,
1498				       u32 nentries)
1499{
1500	src_ring->shadow_base_unaligned = kcalloc(nentries,
1501						  sizeof(struct ce_desc_64),
1502						  GFP_KERNEL);
1503	if (!src_ring->shadow_base_unaligned)
1504		return -ENOMEM;
1505
1506	src_ring->shadow_base = (struct ce_desc_64 *)
1507			PTR_ALIGN(src_ring->shadow_base_unaligned,
1508				  CE_DESC_RING_ALIGN);
1509	return 0;
1510}
1511
1512static struct ath10k_ce_ring *
1513ath10k_ce_alloc_src_ring(struct ath10k *ar, unsigned int ce_id,
1514			 const struct ce_attr *attr)
1515{
1516	struct ath10k_ce_ring *src_ring;
1517	u32 nentries = attr->src_nentries;
1518	dma_addr_t base_addr;
1519	int ret;
1520
1521	nentries = roundup_pow_of_two(nentries);
1522
1523	src_ring = kzalloc(struct_size(src_ring, per_transfer_context,
1524				       nentries), GFP_KERNEL);
1525	if (src_ring == NULL)
1526		return ERR_PTR(-ENOMEM);
1527
1528	src_ring->nentries = nentries;
1529	src_ring->nentries_mask = nentries - 1;
1530
1531	/*
1532	 * Legacy platforms that do not support cache
1533	 * coherent DMA are unsupported
1534	 */
1535	src_ring->base_addr_owner_space_unaligned =
1536		dma_alloc_coherent(ar->dev,
1537				   (nentries * sizeof(struct ce_desc) +
1538				    CE_DESC_RING_ALIGN),
1539				   &base_addr, GFP_KERNEL);
1540	if (!src_ring->base_addr_owner_space_unaligned) {
1541		kfree(src_ring);
1542		return ERR_PTR(-ENOMEM);
1543	}
1544
1545	src_ring->base_addr_ce_space_unaligned = base_addr;
1546
1547	src_ring->base_addr_owner_space =
1548			PTR_ALIGN(src_ring->base_addr_owner_space_unaligned,
1549				  CE_DESC_RING_ALIGN);
1550	src_ring->base_addr_ce_space =
1551			ALIGN(src_ring->base_addr_ce_space_unaligned,
1552			      CE_DESC_RING_ALIGN);
1553
1554	if (ar->hw_params.shadow_reg_support) {
1555		ret = ath10k_ce_alloc_shadow_base(ar, src_ring, nentries);
1556		if (ret) {
1557			dma_free_coherent(ar->dev,
1558					  (nentries * sizeof(struct ce_desc_64) +
1559					   CE_DESC_RING_ALIGN),
1560					  src_ring->base_addr_owner_space_unaligned,
1561					  base_addr);
1562			kfree(src_ring);
1563			return ERR_PTR(ret);
1564		}
1565	}
1566
1567	return src_ring;
1568}
1569
1570static struct ath10k_ce_ring *
1571ath10k_ce_alloc_src_ring_64(struct ath10k *ar, unsigned int ce_id,
1572			    const struct ce_attr *attr)
1573{
1574	struct ath10k_ce_ring *src_ring;
1575	u32 nentries = attr->src_nentries;
1576	dma_addr_t base_addr;
1577	int ret;
1578
1579	nentries = roundup_pow_of_two(nentries);
1580
1581	src_ring = kzalloc(struct_size(src_ring, per_transfer_context,
1582				       nentries), GFP_KERNEL);
1583	if (!src_ring)
1584		return ERR_PTR(-ENOMEM);
1585
1586	src_ring->nentries = nentries;
1587	src_ring->nentries_mask = nentries - 1;
1588
1589	/* Legacy platforms that do not support cache
1590	 * coherent DMA are unsupported
1591	 */
1592	src_ring->base_addr_owner_space_unaligned =
1593		dma_alloc_coherent(ar->dev,
1594				   (nentries * sizeof(struct ce_desc_64) +
1595				    CE_DESC_RING_ALIGN),
1596				   &base_addr, GFP_KERNEL);
1597	if (!src_ring->base_addr_owner_space_unaligned) {
1598		kfree(src_ring);
1599		return ERR_PTR(-ENOMEM);
1600	}
1601
1602	src_ring->base_addr_ce_space_unaligned = base_addr;
1603
1604	src_ring->base_addr_owner_space =
1605			PTR_ALIGN(src_ring->base_addr_owner_space_unaligned,
1606				  CE_DESC_RING_ALIGN);
1607	src_ring->base_addr_ce_space =
1608			ALIGN(src_ring->base_addr_ce_space_unaligned,
1609			      CE_DESC_RING_ALIGN);
1610
1611	if (ar->hw_params.shadow_reg_support) {
1612		ret = ath10k_ce_alloc_shadow_base(ar, src_ring, nentries);
1613		if (ret) {
1614			dma_free_coherent(ar->dev,
1615					  (nentries * sizeof(struct ce_desc_64) +
1616					   CE_DESC_RING_ALIGN),
1617					  src_ring->base_addr_owner_space_unaligned,
1618					  base_addr);
1619			kfree(src_ring);
1620			return ERR_PTR(ret);
1621		}
1622	}
1623
1624	return src_ring;
1625}
1626
1627static struct ath10k_ce_ring *
1628ath10k_ce_alloc_dest_ring(struct ath10k *ar, unsigned int ce_id,
1629			  const struct ce_attr *attr)
1630{
1631	struct ath10k_ce_ring *dest_ring;
1632	u32 nentries;
1633	dma_addr_t base_addr;
1634
1635	nentries = roundup_pow_of_two(attr->dest_nentries);
1636
1637	dest_ring = kzalloc(struct_size(dest_ring, per_transfer_context,
1638					nentries), GFP_KERNEL);
1639	if (dest_ring == NULL)
1640		return ERR_PTR(-ENOMEM);
1641
1642	dest_ring->nentries = nentries;
1643	dest_ring->nentries_mask = nentries - 1;
1644
1645	/*
1646	 * Legacy platforms that do not support cache
1647	 * coherent DMA are unsupported
1648	 */
1649	dest_ring->base_addr_owner_space_unaligned =
1650		dma_alloc_coherent(ar->dev,
1651				   (nentries * sizeof(struct ce_desc) +
1652				    CE_DESC_RING_ALIGN),
1653				   &base_addr, GFP_KERNEL);
1654	if (!dest_ring->base_addr_owner_space_unaligned) {
1655		kfree(dest_ring);
1656		return ERR_PTR(-ENOMEM);
1657	}
1658
1659	dest_ring->base_addr_ce_space_unaligned = base_addr;
1660
1661	dest_ring->base_addr_owner_space =
1662			PTR_ALIGN(dest_ring->base_addr_owner_space_unaligned,
1663				  CE_DESC_RING_ALIGN);
1664	dest_ring->base_addr_ce_space =
1665				ALIGN(dest_ring->base_addr_ce_space_unaligned,
1666				      CE_DESC_RING_ALIGN);
1667
1668	return dest_ring;
1669}
1670
1671static struct ath10k_ce_ring *
1672ath10k_ce_alloc_dest_ring_64(struct ath10k *ar, unsigned int ce_id,
1673			     const struct ce_attr *attr)
1674{
1675	struct ath10k_ce_ring *dest_ring;
1676	u32 nentries;
1677	dma_addr_t base_addr;
1678
1679	nentries = roundup_pow_of_two(attr->dest_nentries);
1680
1681	dest_ring = kzalloc(struct_size(dest_ring, per_transfer_context,
1682					nentries), GFP_KERNEL);
1683	if (!dest_ring)
1684		return ERR_PTR(-ENOMEM);
1685
1686	dest_ring->nentries = nentries;
1687	dest_ring->nentries_mask = nentries - 1;
1688
1689	/* Legacy platforms that do not support cache
1690	 * coherent DMA are unsupported
1691	 */
1692	dest_ring->base_addr_owner_space_unaligned =
1693		dma_alloc_coherent(ar->dev,
1694				   (nentries * sizeof(struct ce_desc_64) +
1695				    CE_DESC_RING_ALIGN),
1696				   &base_addr, GFP_KERNEL);
1697	if (!dest_ring->base_addr_owner_space_unaligned) {
1698		kfree(dest_ring);
1699		return ERR_PTR(-ENOMEM);
1700	}
1701
1702	dest_ring->base_addr_ce_space_unaligned = base_addr;
1703
1704	/* Correctly initialize memory to 0 to prevent garbage
1705	 * data crashing system when download firmware
1706	 */
1707	dest_ring->base_addr_owner_space =
1708			PTR_ALIGN(dest_ring->base_addr_owner_space_unaligned,
1709				  CE_DESC_RING_ALIGN);
1710	dest_ring->base_addr_ce_space =
1711			ALIGN(dest_ring->base_addr_ce_space_unaligned,
1712			      CE_DESC_RING_ALIGN);
1713
1714	return dest_ring;
1715}
1716
1717/*
1718 * Initialize a Copy Engine based on caller-supplied attributes.
1719 * This may be called once to initialize both source and destination
1720 * rings or it may be called twice for separate source and destination
1721 * initialization. It may be that only one side or the other is
1722 * initialized by software/firmware.
1723 */
1724int ath10k_ce_init_pipe(struct ath10k *ar, unsigned int ce_id,
1725			const struct ce_attr *attr)
1726{
1727	int ret;
1728
1729	if (attr->src_nentries) {
1730		ret = ath10k_ce_init_src_ring(ar, ce_id, attr);
1731		if (ret) {
1732			ath10k_err(ar, "Failed to initialize CE src ring for ID: %d (%d)\n",
1733				   ce_id, ret);
1734			return ret;
1735		}
1736	}
1737
1738	if (attr->dest_nentries) {
1739		ret = ath10k_ce_init_dest_ring(ar, ce_id, attr);
1740		if (ret) {
1741			ath10k_err(ar, "Failed to initialize CE dest ring for ID: %d (%d)\n",
1742				   ce_id, ret);
1743			return ret;
1744		}
1745	}
1746
1747	return 0;
1748}
1749EXPORT_SYMBOL(ath10k_ce_init_pipe);
1750
1751static void ath10k_ce_deinit_src_ring(struct ath10k *ar, unsigned int ce_id)
1752{
1753	u32 ctrl_addr = ath10k_ce_base_address(ar, ce_id);
1754
1755	ath10k_ce_src_ring_base_addr_set(ar, ce_id, 0);
1756	ath10k_ce_src_ring_size_set(ar, ctrl_addr, 0);
1757	ath10k_ce_src_ring_dmax_set(ar, ctrl_addr, 0);
1758	ath10k_ce_src_ring_highmark_set(ar, ctrl_addr, 0);
1759}
1760
1761static void ath10k_ce_deinit_dest_ring(struct ath10k *ar, unsigned int ce_id)
1762{
1763	u32 ctrl_addr = ath10k_ce_base_address(ar, ce_id);
1764
1765	ath10k_ce_dest_ring_base_addr_set(ar, ce_id, 0);
1766	ath10k_ce_dest_ring_size_set(ar, ctrl_addr, 0);
1767	ath10k_ce_dest_ring_highmark_set(ar, ctrl_addr, 0);
1768}
1769
1770void ath10k_ce_deinit_pipe(struct ath10k *ar, unsigned int ce_id)
1771{
1772	ath10k_ce_deinit_src_ring(ar, ce_id);
1773	ath10k_ce_deinit_dest_ring(ar, ce_id);
1774}
1775EXPORT_SYMBOL(ath10k_ce_deinit_pipe);
1776
1777static void _ath10k_ce_free_pipe(struct ath10k *ar, int ce_id)
1778{
1779	struct ath10k_ce *ce = ath10k_ce_priv(ar);
1780	struct ath10k_ce_pipe *ce_state = &ce->ce_states[ce_id];
1781
1782	if (ce_state->src_ring) {
1783		if (ar->hw_params.shadow_reg_support)
1784			kfree(ce_state->src_ring->shadow_base_unaligned);
1785		dma_free_coherent(ar->dev,
1786				  (ce_state->src_ring->nentries *
1787				   sizeof(struct ce_desc) +
1788				   CE_DESC_RING_ALIGN),
1789				  ce_state->src_ring->base_addr_owner_space,
1790				  ce_state->src_ring->base_addr_ce_space);
1791		kfree(ce_state->src_ring);
1792	}
1793
1794	if (ce_state->dest_ring) {
1795		dma_free_coherent(ar->dev,
1796				  (ce_state->dest_ring->nentries *
1797				   sizeof(struct ce_desc) +
1798				   CE_DESC_RING_ALIGN),
1799				  ce_state->dest_ring->base_addr_owner_space,
1800				  ce_state->dest_ring->base_addr_ce_space);
1801		kfree(ce_state->dest_ring);
1802	}
1803
1804	ce_state->src_ring = NULL;
1805	ce_state->dest_ring = NULL;
1806}
1807
1808static void _ath10k_ce_free_pipe_64(struct ath10k *ar, int ce_id)
1809{
1810	struct ath10k_ce *ce = ath10k_ce_priv(ar);
1811	struct ath10k_ce_pipe *ce_state = &ce->ce_states[ce_id];
1812
1813	if (ce_state->src_ring) {
1814		if (ar->hw_params.shadow_reg_support)
1815			kfree(ce_state->src_ring->shadow_base_unaligned);
1816		dma_free_coherent(ar->dev,
1817				  (ce_state->src_ring->nentries *
1818				   sizeof(struct ce_desc_64) +
1819				   CE_DESC_RING_ALIGN),
1820				  ce_state->src_ring->base_addr_owner_space,
1821				  ce_state->src_ring->base_addr_ce_space);
1822		kfree(ce_state->src_ring);
1823	}
1824
1825	if (ce_state->dest_ring) {
1826		dma_free_coherent(ar->dev,
1827				  (ce_state->dest_ring->nentries *
1828				   sizeof(struct ce_desc_64) +
1829				   CE_DESC_RING_ALIGN),
1830				  ce_state->dest_ring->base_addr_owner_space,
1831				  ce_state->dest_ring->base_addr_ce_space);
1832		kfree(ce_state->dest_ring);
1833	}
1834
1835	ce_state->src_ring = NULL;
1836	ce_state->dest_ring = NULL;
1837}
1838
1839void ath10k_ce_free_pipe(struct ath10k *ar, int ce_id)
1840{
1841	struct ath10k_ce *ce = ath10k_ce_priv(ar);
1842	struct ath10k_ce_pipe *ce_state = &ce->ce_states[ce_id];
1843
1844	ce_state->ops->ce_free_pipe(ar, ce_id);
1845}
1846EXPORT_SYMBOL(ath10k_ce_free_pipe);
1847
1848void ath10k_ce_dump_registers(struct ath10k *ar,
1849			      struct ath10k_fw_crash_data *crash_data)
1850{
1851	struct ath10k_ce *ce = ath10k_ce_priv(ar);
1852	struct ath10k_ce_crash_data ce_data;
1853	u32 addr, id;
1854
1855	lockdep_assert_held(&ar->dump_mutex);
1856
1857	ath10k_err(ar, "Copy Engine register dump:\n");
1858
1859	spin_lock_bh(&ce->ce_lock);
1860	for (id = 0; id < CE_COUNT; id++) {
1861		addr = ath10k_ce_base_address(ar, id);
1862		ce_data.base_addr = cpu_to_le32(addr);
1863
1864		ce_data.src_wr_idx =
1865			cpu_to_le32(ath10k_ce_src_ring_write_index_get(ar, addr));
1866		ce_data.src_r_idx =
1867			cpu_to_le32(ath10k_ce_src_ring_read_index_get(ar, addr));
1868		ce_data.dst_wr_idx =
1869			cpu_to_le32(ath10k_ce_dest_ring_write_index_get(ar, addr));
1870		ce_data.dst_r_idx =
1871			cpu_to_le32(ath10k_ce_dest_ring_read_index_get(ar, addr));
1872
1873		if (crash_data)
1874			crash_data->ce_crash_data[id] = ce_data;
1875
1876		ath10k_err(ar, "[%02d]: 0x%08x %3u %3u %3u %3u", id,
1877			   le32_to_cpu(ce_data.base_addr),
1878			   le32_to_cpu(ce_data.src_wr_idx),
1879			   le32_to_cpu(ce_data.src_r_idx),
1880			   le32_to_cpu(ce_data.dst_wr_idx),
1881			   le32_to_cpu(ce_data.dst_r_idx));
1882	}
1883
1884	spin_unlock_bh(&ce->ce_lock);
1885}
1886EXPORT_SYMBOL(ath10k_ce_dump_registers);
1887
1888static const struct ath10k_ce_ops ce_ops = {
1889	.ce_alloc_src_ring = ath10k_ce_alloc_src_ring,
1890	.ce_alloc_dst_ring = ath10k_ce_alloc_dest_ring,
1891	.ce_rx_post_buf = __ath10k_ce_rx_post_buf,
1892	.ce_completed_recv_next_nolock = _ath10k_ce_completed_recv_next_nolock,
1893	.ce_revoke_recv_next = _ath10k_ce_revoke_recv_next,
1894	.ce_extract_desc_data = ath10k_ce_extract_desc_data,
1895	.ce_free_pipe = _ath10k_ce_free_pipe,
1896	.ce_send_nolock = _ath10k_ce_send_nolock,
1897	.ce_set_src_ring_base_addr_hi = NULL,
1898	.ce_set_dest_ring_base_addr_hi = NULL,
1899	.ce_completed_send_next_nolock = _ath10k_ce_completed_send_next_nolock,
1900};
1901
1902static const struct ath10k_ce_ops ce_64_ops = {
1903	.ce_alloc_src_ring = ath10k_ce_alloc_src_ring_64,
1904	.ce_alloc_dst_ring = ath10k_ce_alloc_dest_ring_64,
1905	.ce_rx_post_buf = __ath10k_ce_rx_post_buf_64,
1906	.ce_completed_recv_next_nolock =
1907				_ath10k_ce_completed_recv_next_nolock_64,
1908	.ce_revoke_recv_next = _ath10k_ce_revoke_recv_next_64,
1909	.ce_extract_desc_data = ath10k_ce_extract_desc_data_64,
1910	.ce_free_pipe = _ath10k_ce_free_pipe_64,
1911	.ce_send_nolock = _ath10k_ce_send_nolock_64,
1912	.ce_set_src_ring_base_addr_hi = ath10k_ce_set_src_ring_base_addr_hi,
1913	.ce_set_dest_ring_base_addr_hi = ath10k_ce_set_dest_ring_base_addr_hi,
1914	.ce_completed_send_next_nolock = _ath10k_ce_completed_send_next_nolock_64,
1915};
1916
1917static void ath10k_ce_set_ops(struct ath10k *ar,
1918			      struct ath10k_ce_pipe *ce_state)
1919{
1920	switch (ar->hw_rev) {
1921	case ATH10K_HW_WCN3990:
1922		ce_state->ops = &ce_64_ops;
1923		break;
1924	default:
1925		ce_state->ops = &ce_ops;
1926		break;
1927	}
1928}
1929
1930int ath10k_ce_alloc_pipe(struct ath10k *ar, int ce_id,
1931			 const struct ce_attr *attr)
1932{
1933	struct ath10k_ce *ce = ath10k_ce_priv(ar);
1934	struct ath10k_ce_pipe *ce_state = &ce->ce_states[ce_id];
1935	int ret;
1936
1937	ath10k_ce_set_ops(ar, ce_state);
1938	/* Make sure there's enough CE ringbuffer entries for HTT TX to avoid
1939	 * additional TX locking checks.
1940	 *
1941	 * For the lack of a better place do the check here.
1942	 */
1943	BUILD_BUG_ON(2 * TARGET_NUM_MSDU_DESC >
1944		     (CE_HTT_H2T_MSG_SRC_NENTRIES - 1));
1945	BUILD_BUG_ON(2 * TARGET_10_4_NUM_MSDU_DESC_PFC >
1946		     (CE_HTT_H2T_MSG_SRC_NENTRIES - 1));
1947	BUILD_BUG_ON(2 * TARGET_TLV_NUM_MSDU_DESC >
1948		     (CE_HTT_H2T_MSG_SRC_NENTRIES - 1));
1949
1950	ce_state->ar = ar;
1951	ce_state->id = ce_id;
1952	ce_state->ctrl_addr = ath10k_ce_base_address(ar, ce_id);
1953	ce_state->attr_flags = attr->flags;
1954	ce_state->src_sz_max = attr->src_sz_max;
1955
1956	if (attr->src_nentries)
1957		ce_state->send_cb = attr->send_cb;
1958
1959	if (attr->dest_nentries)
1960		ce_state->recv_cb = attr->recv_cb;
1961
1962	if (attr->src_nentries) {
1963		ce_state->src_ring =
1964			ce_state->ops->ce_alloc_src_ring(ar, ce_id, attr);
1965		if (IS_ERR(ce_state->src_ring)) {
1966			ret = PTR_ERR(ce_state->src_ring);
1967			ath10k_err(ar, "failed to alloc CE src ring %d: %d\n",
1968				   ce_id, ret);
1969			ce_state->src_ring = NULL;
1970			return ret;
1971		}
1972	}
1973
1974	if (attr->dest_nentries) {
1975		ce_state->dest_ring = ce_state->ops->ce_alloc_dst_ring(ar,
1976									ce_id,
1977									attr);
1978		if (IS_ERR(ce_state->dest_ring)) {
1979			ret = PTR_ERR(ce_state->dest_ring);
1980			ath10k_err(ar, "failed to alloc CE dest ring %d: %d\n",
1981				   ce_id, ret);
1982			ce_state->dest_ring = NULL;
1983			return ret;
1984		}
1985	}
1986
1987	return 0;
1988}
1989EXPORT_SYMBOL(ath10k_ce_alloc_pipe);
1990
1991void ath10k_ce_alloc_rri(struct ath10k *ar)
1992{
1993	int i;
1994	u32 value;
1995	u32 ctrl1_regs;
1996	u32 ce_base_addr;
1997	struct ath10k_ce *ce = ath10k_ce_priv(ar);
1998
1999	ce->vaddr_rri = dma_alloc_coherent(ar->dev,
2000					   (CE_COUNT * sizeof(u32)),
2001					   &ce->paddr_rri, GFP_KERNEL);
2002
2003	if (!ce->vaddr_rri)
2004		return;
2005
2006	ath10k_ce_write32(ar, ar->hw_ce_regs->ce_rri_low,
2007			  lower_32_bits(ce->paddr_rri));
2008	ath10k_ce_write32(ar, ar->hw_ce_regs->ce_rri_high,
2009			  (upper_32_bits(ce->paddr_rri) &
2010			  CE_DESC_ADDR_HI_MASK));
2011
2012	for (i = 0; i < CE_COUNT; i++) {
2013		ctrl1_regs = ar->hw_ce_regs->ctrl1_regs->addr;
2014		ce_base_addr = ath10k_ce_base_address(ar, i);
2015		value = ath10k_ce_read32(ar, ce_base_addr + ctrl1_regs);
2016		value |= ar->hw_ce_regs->upd->mask;
2017		ath10k_ce_write32(ar, ce_base_addr + ctrl1_regs, value);
2018	}
2019}
2020EXPORT_SYMBOL(ath10k_ce_alloc_rri);
2021
2022void ath10k_ce_free_rri(struct ath10k *ar)
2023{
2024	struct ath10k_ce *ce = ath10k_ce_priv(ar);
2025
2026	dma_free_coherent(ar->dev, (CE_COUNT * sizeof(u32)),
2027			  ce->vaddr_rri,
2028			  ce->paddr_rri);
2029}
2030EXPORT_SYMBOL(ath10k_ce_free_rri);