ce.c 34.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
/*
 * Copyright (c) 2005-2011 Atheros Communications Inc.
 * Copyright (c) 2011-2013 Qualcomm Atheros, Inc.
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include "hif.h"
#include "ce.h"
#include "debug.h"

/*
 * Support for Copy Engine hardware, which is mainly used for
 * communication between Host and Target over a PCIe interconnect.
 */

/*
 * A single CopyEngine (CE) comprises two "rings":
 *   a source ring
 *   a destination ring
 *
 * Each ring consists of a number of descriptors which specify
 * an address, length, and meta-data.
 *
35
 * Typically, one side of the PCIe/AHB/SNOC interconnect (Host or Target)
36 37 38 39 40
 * controls one ring and the other side controls the other ring.
 * The source side chooses when to initiate a transfer and it
 * chooses what to send (buffer address, length). The destination
 * side keeps a supply of "anonymous receive buffers" available and
 * it handles incoming data as it arrives (when the destination
41
 * receives an interrupt).
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
 *
 * The sender may send a simple buffer (address/length) or it may
 * send a small list of buffers.  When a small list is sent, hardware
 * "gathers" these and they end up in a single destination buffer
 * with a single interrupt.
 *
 * There are several "contexts" managed by this layer -- more, it
 * may seem -- than should be needed. These are provided mainly for
 * maximum flexibility and especially to facilitate a simpler HIF
 * implementation. There are per-CopyEngine recv, send, and watermark
 * contexts. These are supplied by the caller when a recv, send,
 * or watermark handler is established and they are echoed back to
 * the caller when the respective callbacks are invoked. There is
 * also a per-transfer context supplied by the caller when a buffer
 * (or sendlist) is sent and when a buffer is enqueued for recv.
 * These per-transfer contexts are echoed back to the caller when
 * the buffer is sent/received.
 */

61 62 63 64 65 66 67 68 69 70 71 72 73 74
static inline unsigned int
ath10k_set_ring_byte(unsigned int offset,
		     struct ath10k_hw_ce_regs_addr_map *addr_map)
{
	return ((offset << addr_map->lsb) & addr_map->mask);
}

static inline unsigned int
ath10k_get_ring_byte(unsigned int offset,
		     struct ath10k_hw_ce_regs_addr_map *addr_map)
{
	return ((offset & addr_map->mask) >> (addr_map->lsb));
}

75 76 77 78 79 80 81 82 83 84 85 86 87 88
static inline u32 ath10k_ce_read32(struct ath10k *ar, u32 offset)
{
	struct ath10k_ce *ce = ath10k_ce_priv(ar);

	return ce->bus_ops->read32(ar, offset);
}

static inline void ath10k_ce_write32(struct ath10k *ar, u32 offset, u32 value)
{
	struct ath10k_ce *ce = ath10k_ce_priv(ar);

	ce->bus_ops->write32(ar, offset, value);
}

89 90 91 92
static inline void ath10k_ce_dest_ring_write_index_set(struct ath10k *ar,
						       u32 ce_ctrl_addr,
						       unsigned int n)
{
93 94
	ath10k_ce_write32(ar, ce_ctrl_addr +
			  ar->hw_ce_regs->dst_wr_index_addr, n);
95 96 97 98 99
}

static inline u32 ath10k_ce_dest_ring_write_index_get(struct ath10k *ar,
						      u32 ce_ctrl_addr)
{
100 101
	return ath10k_ce_read32(ar, ce_ctrl_addr +
				ar->hw_ce_regs->dst_wr_index_addr);
102 103 104 105 106 107
}

static inline void ath10k_ce_src_ring_write_index_set(struct ath10k *ar,
						      u32 ce_ctrl_addr,
						      unsigned int n)
{
108 109
	ath10k_ce_write32(ar, ce_ctrl_addr +
			  ar->hw_ce_regs->sr_wr_index_addr, n);
110 111 112 113 114
}

static inline u32 ath10k_ce_src_ring_write_index_get(struct ath10k *ar,
						     u32 ce_ctrl_addr)
{
115 116
	return ath10k_ce_read32(ar, ce_ctrl_addr +
				ar->hw_ce_regs->sr_wr_index_addr);
117 118 119 120 121
}

static inline u32 ath10k_ce_src_ring_read_index_get(struct ath10k *ar,
						    u32 ce_ctrl_addr)
{
122 123
	return ath10k_ce_read32(ar, ce_ctrl_addr +
				ar->hw_ce_regs->current_srri_addr);
124 125 126 127 128 129
}

static inline void ath10k_ce_src_ring_base_addr_set(struct ath10k *ar,
						    u32 ce_ctrl_addr,
						    unsigned int addr)
{
130 131
	ath10k_ce_write32(ar, ce_ctrl_addr +
			  ar->hw_ce_regs->sr_base_addr, addr);
132 133 134 135 136 137
}

static inline void ath10k_ce_src_ring_size_set(struct ath10k *ar,
					       u32 ce_ctrl_addr,
					       unsigned int n)
{
138 139
	ath10k_ce_write32(ar, ce_ctrl_addr +
			  ar->hw_ce_regs->sr_size_addr, n);
140 141 142 143 144 145
}

static inline void ath10k_ce_src_ring_dmax_set(struct ath10k *ar,
					       u32 ce_ctrl_addr,
					       unsigned int n)
{
146
	struct ath10k_hw_ce_ctrl1 *ctrl_regs = ar->hw_ce_regs->ctrl1_regs;
147

148 149 150 151 152 153
	u32 ctrl1_addr = ath10k_ce_read32(ar, ce_ctrl_addr +
					  ctrl_regs->addr);

	ath10k_ce_write32(ar, ce_ctrl_addr + ctrl_regs->addr,
			  (ctrl1_addr &  ~(ctrl_regs->dmax->mask)) |
			  ath10k_set_ring_byte(n, ctrl_regs->dmax));
154 155 156 157 158 159
}

static inline void ath10k_ce_src_ring_byte_swap_set(struct ath10k *ar,
						    u32 ce_ctrl_addr,
						    unsigned int n)
{
160
	struct ath10k_hw_ce_ctrl1 *ctrl_regs = ar->hw_ce_regs->ctrl1_regs;
161

162 163 164 165 166 167
	u32 ctrl1_addr = ath10k_ce_read32(ar, ce_ctrl_addr +
					  ctrl_regs->addr);

	ath10k_ce_write32(ar, ce_ctrl_addr + ctrl_regs->addr,
			  (ctrl1_addr & ~(ctrl_regs->src_ring->mask)) |
			  ath10k_set_ring_byte(n, ctrl_regs->src_ring));
168 169 170 171 172 173
}

static inline void ath10k_ce_dest_ring_byte_swap_set(struct ath10k *ar,
						     u32 ce_ctrl_addr,
						     unsigned int n)
{
174
	struct ath10k_hw_ce_ctrl1 *ctrl_regs = ar->hw_ce_regs->ctrl1_regs;
175

176 177 178 179 180 181
	u32 ctrl1_addr = ath10k_ce_read32(ar, ce_ctrl_addr +
					  ctrl_regs->addr);

	ath10k_ce_write32(ar, ce_ctrl_addr + ctrl_regs->addr,
			  (ctrl1_addr & ~(ctrl_regs->dst_ring->mask)) |
			  ath10k_set_ring_byte(n, ctrl_regs->dst_ring));
182 183 184 185 186
}

static inline u32 ath10k_ce_dest_ring_read_index_get(struct ath10k *ar,
						     u32 ce_ctrl_addr)
{
187 188
	return ath10k_ce_read32(ar, ce_ctrl_addr +
				ar->hw_ce_regs->current_drri_addr);
189 190 191 192 193 194
}

static inline void ath10k_ce_dest_ring_base_addr_set(struct ath10k *ar,
						     u32 ce_ctrl_addr,
						     u32 addr)
{
195 196
	ath10k_ce_write32(ar, ce_ctrl_addr +
			  ar->hw_ce_regs->dr_base_addr, addr);
197 198 199 200 201 202
}

static inline void ath10k_ce_dest_ring_size_set(struct ath10k *ar,
						u32 ce_ctrl_addr,
						unsigned int n)
{
203 204
	ath10k_ce_write32(ar, ce_ctrl_addr +
			  ar->hw_ce_regs->dr_size_addr, n);
205 206 207 208 209 210
}

static inline void ath10k_ce_src_ring_highmark_set(struct ath10k *ar,
						   u32 ce_ctrl_addr,
						   unsigned int n)
{
211
	struct ath10k_hw_ce_dst_src_wm_regs *srcr_wm = ar->hw_ce_regs->wm_srcr;
212
	u32 addr = ath10k_ce_read32(ar, ce_ctrl_addr + srcr_wm->addr);
213

214 215 216
	ath10k_ce_write32(ar, ce_ctrl_addr + srcr_wm->addr,
			  (addr & ~(srcr_wm->wm_high->mask)) |
			  (ath10k_set_ring_byte(n, srcr_wm->wm_high)));
217 218 219 220 221 222
}

static inline void ath10k_ce_src_ring_lowmark_set(struct ath10k *ar,
						  u32 ce_ctrl_addr,
						  unsigned int n)
{
223
	struct ath10k_hw_ce_dst_src_wm_regs *srcr_wm = ar->hw_ce_regs->wm_srcr;
224
	u32 addr = ath10k_ce_read32(ar, ce_ctrl_addr + srcr_wm->addr);
225

226 227 228
	ath10k_ce_write32(ar, ce_ctrl_addr + srcr_wm->addr,
			  (addr & ~(srcr_wm->wm_low->mask)) |
			  (ath10k_set_ring_byte(n, srcr_wm->wm_low)));
229 230 231 232 233 234
}

static inline void ath10k_ce_dest_ring_highmark_set(struct ath10k *ar,
						    u32 ce_ctrl_addr,
						    unsigned int n)
{
235
	struct ath10k_hw_ce_dst_src_wm_regs *dstr_wm = ar->hw_ce_regs->wm_dstr;
236
	u32 addr = ath10k_ce_read32(ar, ce_ctrl_addr + dstr_wm->addr);
237

238 239 240
	ath10k_ce_write32(ar, ce_ctrl_addr + dstr_wm->addr,
			  (addr & ~(dstr_wm->wm_high->mask)) |
			  (ath10k_set_ring_byte(n, dstr_wm->wm_high)));
241 242 243 244 245 246
}

static inline void ath10k_ce_dest_ring_lowmark_set(struct ath10k *ar,
						   u32 ce_ctrl_addr,
						   unsigned int n)
{
247
	struct ath10k_hw_ce_dst_src_wm_regs *dstr_wm = ar->hw_ce_regs->wm_dstr;
248
	u32 addr = ath10k_ce_read32(ar, ce_ctrl_addr + dstr_wm->addr);
249

250 251 252
	ath10k_ce_write32(ar, ce_ctrl_addr + dstr_wm->addr,
			  (addr & ~(dstr_wm->wm_low->mask)) |
			  (ath10k_set_ring_byte(n, dstr_wm->wm_low)));
253 254 255 256 257
}

static inline void ath10k_ce_copy_complete_inter_enable(struct ath10k *ar,
							u32 ce_ctrl_addr)
{
258
	struct ath10k_hw_ce_host_ie *host_ie = ar->hw_ce_regs->host_ie;
259

260 261 262 263 264
	u32 host_ie_addr = ath10k_ce_read32(ar, ce_ctrl_addr +
					    ar->hw_ce_regs->host_ie_addr);

	ath10k_ce_write32(ar, ce_ctrl_addr + ar->hw_ce_regs->host_ie_addr,
			  host_ie_addr | host_ie->copy_complete->mask);
265 266 267 268 269
}

static inline void ath10k_ce_copy_complete_intr_disable(struct ath10k *ar,
							u32 ce_ctrl_addr)
{
270
	struct ath10k_hw_ce_host_ie *host_ie = ar->hw_ce_regs->host_ie;
271

272 273 274 275 276
	u32 host_ie_addr = ath10k_ce_read32(ar, ce_ctrl_addr +
					    ar->hw_ce_regs->host_ie_addr);

	ath10k_ce_write32(ar, ce_ctrl_addr + ar->hw_ce_regs->host_ie_addr,
			  host_ie_addr & ~(host_ie->copy_complete->mask));
277 278 279 280 281
}

static inline void ath10k_ce_watermark_intr_disable(struct ath10k *ar,
						    u32 ce_ctrl_addr)
{
282
	struct ath10k_hw_ce_host_wm_regs *wm_regs = ar->hw_ce_regs->wm_regs;
283

284 285 286 287 288
	u32 host_ie_addr = ath10k_ce_read32(ar, ce_ctrl_addr +
					    ar->hw_ce_regs->host_ie_addr);

	ath10k_ce_write32(ar, ce_ctrl_addr + ar->hw_ce_regs->host_ie_addr,
			  host_ie_addr & ~(wm_regs->wm_mask));
289 290 291 292 293
}

static inline void ath10k_ce_error_intr_enable(struct ath10k *ar,
					       u32 ce_ctrl_addr)
{
294
	struct ath10k_hw_ce_misc_regs *misc_regs = ar->hw_ce_regs->misc_regs;
295

296 297 298 299 300 301
	u32 misc_ie_addr = ath10k_ce_read32(ar, ce_ctrl_addr +
					    ar->hw_ce_regs->misc_ie_addr);

	ath10k_ce_write32(ar,
			  ce_ctrl_addr + ar->hw_ce_regs->misc_ie_addr,
			  misc_ie_addr | misc_regs->err_mask);
302 303
}

304 305 306
static inline void ath10k_ce_error_intr_disable(struct ath10k *ar,
						u32 ce_ctrl_addr)
{
307
	struct ath10k_hw_ce_misc_regs *misc_regs = ar->hw_ce_regs->misc_regs;
308

309 310 311 312 313 314
	u32 misc_ie_addr = ath10k_ce_read32(ar,
			ce_ctrl_addr + ar->hw_ce_regs->misc_ie_addr);

	ath10k_ce_write32(ar,
			  ce_ctrl_addr + ar->hw_ce_regs->misc_ie_addr,
			  misc_ie_addr & ~(misc_regs->err_mask));
315 316
}

317 318 319 320
static inline void ath10k_ce_engine_int_status_clear(struct ath10k *ar,
						     u32 ce_ctrl_addr,
						     unsigned int mask)
{
321 322
	struct ath10k_hw_ce_host_wm_regs *wm_regs = ar->hw_ce_regs->wm_regs;

323
	ath10k_ce_write32(ar, ce_ctrl_addr + wm_regs->addr, mask);
324 325 326
}

/*
327
 * Guts of ath10k_ce_send.
328 329
 * The caller takes responsibility for any needed locking.
 */
330 331 332 333 334 335
int ath10k_ce_send_nolock(struct ath10k_ce_pipe *ce_state,
			  void *per_transfer_context,
			  u32 buffer,
			  unsigned int nbytes,
			  unsigned int transfer_id,
			  unsigned int flags)
336 337
{
	struct ath10k *ar = ce_state->ar;
338
	struct ath10k_ce_ring *src_ring = ce_state->src_ring;
339
	struct ce_desc *desc, sdesc;
340 341 342 343 344 345 346 347
	unsigned int nentries_mask = src_ring->nentries_mask;
	unsigned int sw_index = src_ring->sw_index;
	unsigned int write_index = src_ring->write_index;
	u32 ctrl_addr = ce_state->ctrl_addr;
	u32 desc_flags = 0;
	int ret = 0;

	if (nbytes > ce_state->src_sz_max)
348
		ath10k_warn(ar, "%s: send more we can (nbytes: %d, max: %d)\n",
349 350 351 352
			    __func__, nbytes, ce_state->src_sz_max);

	if (unlikely(CE_RING_DELTA(nentries_mask,
				   write_index, sw_index - 1) <= 0)) {
M
Michal Kazior 已提交
353
		ret = -ENOSR;
354 355 356 357 358 359 360 361 362 363 364 365 366
		goto exit;
	}

	desc = CE_SRC_RING_TO_DESC(src_ring->base_addr_owner_space,
				   write_index);

	desc_flags |= SM(transfer_id, CE_DESC_FLAGS_META_DATA);

	if (flags & CE_SEND_FLAG_GATHER)
		desc_flags |= CE_DESC_FLAGS_GATHER;
	if (flags & CE_SEND_FLAG_BYTE_SWAP)
		desc_flags |= CE_DESC_FLAGS_BYTE_SWAP;

367 368 369
	sdesc.addr   = __cpu_to_le32(buffer);
	sdesc.nbytes = __cpu_to_le16(nbytes);
	sdesc.flags  = __cpu_to_le16(desc_flags);
370

371
	*desc = sdesc;
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386

	src_ring->per_transfer_context[write_index] = per_transfer_context;

	/* Update Source Ring Write Index */
	write_index = CE_RING_IDX_INCR(nentries_mask, write_index);

	/* WORKAROUND */
	if (!(flags & CE_SEND_FLAG_GATHER))
		ath10k_ce_src_ring_write_index_set(ar, ctrl_addr, write_index);

	src_ring->write_index = write_index;
exit:
	return ret;
}

387 388 389
void __ath10k_ce_send_revert(struct ath10k_ce_pipe *pipe)
{
	struct ath10k *ar = pipe->ar;
390
	struct ath10k_ce *ce = ath10k_ce_priv(ar);
391 392 393
	struct ath10k_ce_ring *src_ring = pipe->src_ring;
	u32 ctrl_addr = pipe->ctrl_addr;

394
	lockdep_assert_held(&ce->ce_lock);
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413

	/*
	 * This function must be called only if there is an incomplete
	 * scatter-gather transfer (before index register is updated)
	 * that needs to be cleaned up.
	 */
	if (WARN_ON_ONCE(src_ring->write_index == src_ring->sw_index))
		return;

	if (WARN_ON_ONCE(src_ring->write_index ==
			 ath10k_ce_src_ring_write_index_get(ar, ctrl_addr)))
		return;

	src_ring->write_index--;
	src_ring->write_index &= src_ring->nentries_mask;

	src_ring->per_transfer_context[src_ring->write_index] = NULL;
}

414
int ath10k_ce_send(struct ath10k_ce_pipe *ce_state,
415 416 417 418 419 420 421
		   void *per_transfer_context,
		   u32 buffer,
		   unsigned int nbytes,
		   unsigned int transfer_id,
		   unsigned int flags)
{
	struct ath10k *ar = ce_state->ar;
422
	struct ath10k_ce *ce = ath10k_ce_priv(ar);
423 424
	int ret;

425
	spin_lock_bh(&ce->ce_lock);
426 427
	ret = ath10k_ce_send_nolock(ce_state, per_transfer_context,
				    buffer, nbytes, transfer_id, flags);
428
	spin_unlock_bh(&ce->ce_lock);
429 430 431 432

	return ret;
}

M
Michal Kazior 已提交
433 434 435
int ath10k_ce_num_free_src_entries(struct ath10k_ce_pipe *pipe)
{
	struct ath10k *ar = pipe->ar;
436
	struct ath10k_ce *ce = ath10k_ce_priv(ar);
M
Michal Kazior 已提交
437 438
	int delta;

439
	spin_lock_bh(&ce->ce_lock);
M
Michal Kazior 已提交
440 441 442
	delta = CE_RING_DELTA(pipe->src_ring->nentries_mask,
			      pipe->src_ring->write_index,
			      pipe->src_ring->sw_index - 1);
443
	spin_unlock_bh(&ce->ce_lock);
M
Michal Kazior 已提交
444 445 446 447

	return delta;
}

448
int __ath10k_ce_rx_num_free_bufs(struct ath10k_ce_pipe *pipe)
449
{
450
	struct ath10k *ar = pipe->ar;
451
	struct ath10k_ce *ce = ath10k_ce_priv(ar);
452
	struct ath10k_ce_ring *dest_ring = pipe->dest_ring;
453
	unsigned int nentries_mask = dest_ring->nentries_mask;
454 455 456
	unsigned int write_index = dest_ring->write_index;
	unsigned int sw_index = dest_ring->sw_index;

457
	lockdep_assert_held(&ce->ce_lock);
458 459 460 461 462 463 464

	return CE_RING_DELTA(nentries_mask, write_index, sw_index - 1);
}

int __ath10k_ce_rx_post_buf(struct ath10k_ce_pipe *pipe, void *ctx, u32 paddr)
{
	struct ath10k *ar = pipe->ar;
465
	struct ath10k_ce *ce = ath10k_ce_priv(ar);
466 467 468 469 470 471 472 473
	struct ath10k_ce_ring *dest_ring = pipe->dest_ring;
	unsigned int nentries_mask = dest_ring->nentries_mask;
	unsigned int write_index = dest_ring->write_index;
	unsigned int sw_index = dest_ring->sw_index;
	struct ce_desc *base = dest_ring->base_addr_owner_space;
	struct ce_desc *desc = CE_DEST_RING_TO_DESC(base, write_index);
	u32 ctrl_addr = pipe->ctrl_addr;

474
	lockdep_assert_held(&ce->ce_lock);
475

476 477
	if ((pipe->id != 5) &&
	    CE_RING_DELTA(nentries_mask, write_index, sw_index - 1) == 0)
478
		return -ENOSPC;
479 480 481 482 483 484 485 486 487 488 489 490

	desc->addr = __cpu_to_le32(paddr);
	desc->nbytes = 0;

	dest_ring->per_transfer_context[write_index] = ctx;
	write_index = CE_RING_IDX_INCR(nentries_mask, write_index);
	ath10k_ce_dest_ring_write_index_set(ar, ctrl_addr, write_index);
	dest_ring->write_index = write_index;

	return 0;
}

491 492 493 494 495 496 497
void ath10k_ce_rx_update_write_idx(struct ath10k_ce_pipe *pipe, u32 nentries)
{
	struct ath10k *ar = pipe->ar;
	struct ath10k_ce_ring *dest_ring = pipe->dest_ring;
	unsigned int nentries_mask = dest_ring->nentries_mask;
	unsigned int write_index = dest_ring->write_index;
	u32 ctrl_addr = pipe->ctrl_addr;
498 499 500 501 502 503 504
	u32 cur_write_idx = ath10k_ce_dest_ring_write_index_get(ar, ctrl_addr);

	/* Prevent CE ring stuck issue that will occur when ring is full.
	 * Make sure that write index is 1 less than read index.
	 */
	if ((cur_write_idx + nentries)  == dest_ring->sw_index)
		nentries -= 1;
505 506 507 508 509 510

	write_index = CE_RING_IDX_ADD(nentries_mask, write_index, nentries);
	ath10k_ce_dest_ring_write_index_set(ar, ctrl_addr, write_index);
	dest_ring->write_index = write_index;
}

511 512 513
int ath10k_ce_rx_post_buf(struct ath10k_ce_pipe *pipe, void *ctx, u32 paddr)
{
	struct ath10k *ar = pipe->ar;
514
	struct ath10k_ce *ce = ath10k_ce_priv(ar);
515 516
	int ret;

517
	spin_lock_bh(&ce->ce_lock);
518
	ret = __ath10k_ce_rx_post_buf(pipe, ctx, paddr);
519
	spin_unlock_bh(&ce->ce_lock);
520

521 522 523 524 525 526 527
	return ret;
}

/*
 * Guts of ath10k_ce_completed_recv_next.
 * The caller takes responsibility for any necessary locking.
 */
K
Kalle Valo 已提交
528 529
int ath10k_ce_completed_recv_next_nolock(struct ath10k_ce_pipe *ce_state,
					 void **per_transfer_contextp,
530
					 unsigned int *nbytesp)
531
{
532
	struct ath10k_ce_ring *dest_ring = ce_state->dest_ring;
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
	unsigned int nentries_mask = dest_ring->nentries_mask;
	unsigned int sw_index = dest_ring->sw_index;

	struct ce_desc *base = dest_ring->base_addr_owner_space;
	struct ce_desc *desc = CE_DEST_RING_TO_DESC(base, sw_index);
	struct ce_desc sdesc;
	u16 nbytes;

	/* Copy in one go for performance reasons */
	sdesc = *desc;

	nbytes = __le16_to_cpu(sdesc.nbytes);
	if (nbytes == 0) {
		/*
		 * This closes a relatively unusual race where the Host
		 * sees the updated DRRI before the update to the
		 * corresponding descriptor has completed. We treat this
		 * as a descriptor that is not yet done.
		 */
		return -EIO;
	}

	desc->nbytes = 0;

	/* Return data from completed destination descriptor */
	*nbytesp = nbytes;

	if (per_transfer_contextp)
		*per_transfer_contextp =
			dest_ring->per_transfer_context[sw_index];

564 565 566 567 568
	/* Copy engine 5 (HTT Rx) will reuse the same transfer context.
	 * So update transfer context all CEs except CE5.
	 */
	if (ce_state->id != 5)
		dest_ring->per_transfer_context[sw_index] = NULL;
569 570 571 572 573 574 575 576

	/* Update sw_index */
	sw_index = CE_RING_IDX_INCR(nentries_mask, sw_index);
	dest_ring->sw_index = sw_index;

	return 0;
}

577
int ath10k_ce_completed_recv_next(struct ath10k_ce_pipe *ce_state,
578
				  void **per_transfer_contextp,
579
				  unsigned int *nbytesp)
580 581
{
	struct ath10k *ar = ce_state->ar;
582
	struct ath10k_ce *ce = ath10k_ce_priv(ar);
583 584
	int ret;

585
	spin_lock_bh(&ce->ce_lock);
586 587
	ret = ath10k_ce_completed_recv_next_nolock(ce_state,
						   per_transfer_contextp,
588
						   nbytesp);
589
	spin_unlock_bh(&ce->ce_lock);
590 591 592 593

	return ret;
}

594
int ath10k_ce_revoke_recv_next(struct ath10k_ce_pipe *ce_state,
595 596 597
			       void **per_transfer_contextp,
			       u32 *bufferp)
{
598
	struct ath10k_ce_ring *dest_ring;
599 600 601 602 603
	unsigned int nentries_mask;
	unsigned int sw_index;
	unsigned int write_index;
	int ret;
	struct ath10k *ar;
604
	struct ath10k_ce *ce;
605 606 607 608 609 610 611

	dest_ring = ce_state->dest_ring;

	if (!dest_ring)
		return -EIO;

	ar = ce_state->ar;
612
	ce = ath10k_ce_priv(ar);
613

614
	spin_lock_bh(&ce->ce_lock);
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631

	nentries_mask = dest_ring->nentries_mask;
	sw_index = dest_ring->sw_index;
	write_index = dest_ring->write_index;
	if (write_index != sw_index) {
		struct ce_desc *base = dest_ring->base_addr_owner_space;
		struct ce_desc *desc = CE_DEST_RING_TO_DESC(base, sw_index);

		/* Return data from completed destination descriptor */
		*bufferp = __le32_to_cpu(desc->addr);

		if (per_transfer_contextp)
			*per_transfer_contextp =
				dest_ring->per_transfer_context[sw_index];

		/* sanity */
		dest_ring->per_transfer_context[sw_index] = NULL;
M
Michal Kazior 已提交
632
		desc->nbytes = 0;
633 634 635 636 637 638 639 640 641

		/* Update sw_index */
		sw_index = CE_RING_IDX_INCR(nentries_mask, sw_index);
		dest_ring->sw_index = sw_index;
		ret = 0;
	} else {
		ret = -EIO;
	}

642
	spin_unlock_bh(&ce->ce_lock);
643 644 645 646 647 648 649 650

	return ret;
}

/*
 * Guts of ath10k_ce_completed_send_next.
 * The caller takes responsibility for any necessary locking.
 */
K
Kalle Valo 已提交
651
int ath10k_ce_completed_send_next_nolock(struct ath10k_ce_pipe *ce_state,
652
					 void **per_transfer_contextp)
653
{
654
	struct ath10k_ce_ring *src_ring = ce_state->src_ring;
655 656 657 658 659
	u32 ctrl_addr = ce_state->ctrl_addr;
	struct ath10k *ar = ce_state->ar;
	unsigned int nentries_mask = src_ring->nentries_mask;
	unsigned int sw_index = src_ring->sw_index;
	unsigned int read_index;
B
Ben Greear 已提交
660
	struct ce_desc *desc;
661 662 663 664 665 666 667 668 669

	if (src_ring->hw_index == sw_index) {
		/*
		 * The SW completion index has caught up with the cached
		 * version of the HW completion index.
		 * Update the cached HW completion index to see whether
		 * the SW has really caught up to the HW, or if the cached
		 * value of the HW index has become stale.
		 */
670

671 672 673 674 675 676
		read_index = ath10k_ce_src_ring_read_index_get(ar, ctrl_addr);
		if (read_index == 0xffffffff)
			return -ENODEV;

		read_index &= nentries_mask;
		src_ring->hw_index = read_index;
677
	}
678

679 680
	read_index = src_ring->hw_index;

681
	if (read_index == sw_index)
682
		return -EIO;
683

684 685 686
	if (per_transfer_contextp)
		*per_transfer_contextp =
			src_ring->per_transfer_context[sw_index];
687

688 689
	/* sanity */
	src_ring->per_transfer_context[sw_index] = NULL;
B
Ben Greear 已提交
690 691 692
	desc = CE_SRC_RING_TO_DESC(src_ring->base_addr_owner_space,
				   sw_index);
	desc->nbytes = 0;
693

694 695 696 697 698
	/* Update sw_index */
	sw_index = CE_RING_IDX_INCR(nentries_mask, sw_index);
	src_ring->sw_index = sw_index;

	return 0;
699 700 701
}

/* NB: Modeled after ath10k_ce_completed_send_next */
702
int ath10k_ce_cancel_send_next(struct ath10k_ce_pipe *ce_state,
703 704 705 706 707
			       void **per_transfer_contextp,
			       u32 *bufferp,
			       unsigned int *nbytesp,
			       unsigned int *transfer_idp)
{
708
	struct ath10k_ce_ring *src_ring;
709 710 711 712 713
	unsigned int nentries_mask;
	unsigned int sw_index;
	unsigned int write_index;
	int ret;
	struct ath10k *ar;
714
	struct ath10k_ce *ce;
715 716 717 718 719 720 721

	src_ring = ce_state->src_ring;

	if (!src_ring)
		return -EIO;

	ar = ce_state->ar;
722
	ce = ath10k_ce_priv(ar);
723

724
	spin_lock_bh(&ce->ce_lock);
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754

	nentries_mask = src_ring->nentries_mask;
	sw_index = src_ring->sw_index;
	write_index = src_ring->write_index;

	if (write_index != sw_index) {
		struct ce_desc *base = src_ring->base_addr_owner_space;
		struct ce_desc *desc = CE_SRC_RING_TO_DESC(base, sw_index);

		/* Return data from completed source descriptor */
		*bufferp = __le32_to_cpu(desc->addr);
		*nbytesp = __le16_to_cpu(desc->nbytes);
		*transfer_idp = MS(__le16_to_cpu(desc->flags),
						CE_DESC_FLAGS_META_DATA);

		if (per_transfer_contextp)
			*per_transfer_contextp =
				src_ring->per_transfer_context[sw_index];

		/* sanity */
		src_ring->per_transfer_context[sw_index] = NULL;

		/* Update sw_index */
		sw_index = CE_RING_IDX_INCR(nentries_mask, sw_index);
		src_ring->sw_index = sw_index;
		ret = 0;
	} else {
		ret = -EIO;
	}

755
	spin_unlock_bh(&ce->ce_lock);
756 757 758 759

	return ret;
}

760
int ath10k_ce_completed_send_next(struct ath10k_ce_pipe *ce_state,
761
				  void **per_transfer_contextp)
762 763
{
	struct ath10k *ar = ce_state->ar;
764
	struct ath10k_ce *ce = ath10k_ce_priv(ar);
765 766
	int ret;

767
	spin_lock_bh(&ce->ce_lock);
768
	ret = ath10k_ce_completed_send_next_nolock(ce_state,
769
						   per_transfer_contextp);
770
	spin_unlock_bh(&ce->ce_lock);
771 772 773 774 775 776 777 778 779 780 781 782

	return ret;
}

/*
 * Guts of interrupt handler for per-engine interrupts on a particular CE.
 *
 * Invokes registered callbacks for recv_complete,
 * send_complete, and watermarks.
 */
void ath10k_ce_per_engine_service(struct ath10k *ar, unsigned int ce_id)
{
783 784
	struct ath10k_ce *ce = ath10k_ce_priv(ar);
	struct ath10k_ce_pipe *ce_state = &ce->ce_states[ce_id];
785
	struct ath10k_hw_ce_host_wm_regs *wm_regs = ar->hw_ce_regs->wm_regs;
786 787
	u32 ctrl_addr = ce_state->ctrl_addr;

788
	spin_lock_bh(&ce->ce_lock);
789 790

	/* Clear the copy-complete interrupts that will be handled here. */
791 792
	ath10k_ce_engine_int_status_clear(ar, ctrl_addr,
					  wm_regs->cc_mask);
793

794
	spin_unlock_bh(&ce->ce_lock);
795

796 797 798 799 800 801
	if (ce_state->recv_cb)
		ce_state->recv_cb(ce_state);

	if (ce_state->send_cb)
		ce_state->send_cb(ce_state);

802
	spin_lock_bh(&ce->ce_lock);
803 804 805 806 807

	/*
	 * Misc CE interrupts are not being handled, but still need
	 * to be cleared.
	 */
808
	ath10k_ce_engine_int_status_clear(ar, ctrl_addr, wm_regs->wm_mask);
809

810
	spin_unlock_bh(&ce->ce_lock);
811 812 813 814 815 816 817 818 819 820
}

/*
 * Handler for per-engine interrupts on ALL active CEs.
 * This is used in cases where the system is sharing a
 * single interrput for all CEs
 */

void ath10k_ce_per_engine_service_any(struct ath10k *ar)
{
821
	int ce_id;
822 823
	u32 intr_summary;

824
	intr_summary = ath10k_ce_interrupt_summary(ar);
825

M
Michal Kazior 已提交
826
	for (ce_id = 0; intr_summary && (ce_id < CE_COUNT); ce_id++) {
827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
		if (intr_summary & (1 << ce_id))
			intr_summary &= ~(1 << ce_id);
		else
			/* no intr pending on this CE */
			continue;

		ath10k_ce_per_engine_service(ar, ce_id);
	}
}

/*
 * Adjust interrupts for the copy complete handler.
 * If it's needed for either send or recv, then unmask
 * this interrupt; otherwise, mask it.
 *
 * Called with ce_lock held.
 */
844
static void ath10k_ce_per_engine_handler_adjust(struct ath10k_ce_pipe *ce_state)
845 846 847
{
	u32 ctrl_addr = ce_state->ctrl_addr;
	struct ath10k *ar = ce_state->ar;
848
	bool disable_copy_compl_intr = ce_state->attr_flags & CE_ATTR_DIS_INTR;
849 850 851 852 853 854 855 856 857 858

	if ((!disable_copy_compl_intr) &&
	    (ce_state->send_cb || ce_state->recv_cb))
		ath10k_ce_copy_complete_inter_enable(ar, ctrl_addr);
	else
		ath10k_ce_copy_complete_intr_disable(ar, ctrl_addr);

	ath10k_ce_watermark_intr_disable(ar, ctrl_addr);
}

859
int ath10k_ce_disable_interrupts(struct ath10k *ar)
860
{
861
	int ce_id;
862

M
Michal Kazior 已提交
863
	for (ce_id = 0; ce_id < CE_COUNT; ce_id++) {
M
Michal Kazior 已提交
864
		u32 ctrl_addr = ath10k_ce_base_address(ar, ce_id);
865 866

		ath10k_ce_copy_complete_intr_disable(ar, ctrl_addr);
867 868
		ath10k_ce_error_intr_disable(ar, ctrl_addr);
		ath10k_ce_watermark_intr_disable(ar, ctrl_addr);
869
	}
870 871

	return 0;
872 873
}

874
void ath10k_ce_enable_interrupts(struct ath10k *ar)
875
{
876
	struct ath10k_ce *ce = ath10k_ce_priv(ar);
877
	int ce_id;
878
	struct ath10k_ce_pipe *ce_state;
879

880 881 882
	/* Skip the last copy engine, CE7 the diagnostic window, as that
	 * uses polling and isn't initialized for interrupts.
	 */
883 884 885 886
	for (ce_id = 0; ce_id < CE_COUNT - 1; ce_id++) {
		ce_state  = &ce->ce_states[ce_id];
		ath10k_ce_per_engine_handler_adjust(ce_state);
	}
887 888 889 890 891 892
}

static int ath10k_ce_init_src_ring(struct ath10k *ar,
				   unsigned int ce_id,
				   const struct ce_attr *attr)
{
893 894
	struct ath10k_ce *ce = ath10k_ce_priv(ar);
	struct ath10k_ce_pipe *ce_state = &ce->ce_states[ce_id];
895
	struct ath10k_ce_ring *src_ring = ce_state->src_ring;
M
Michal Kazior 已提交
896
	u32 nentries, ctrl_addr = ath10k_ce_base_address(ar, ce_id);
897

898
	nentries = roundup_pow_of_two(attr->src_nentries);
899

M
Michal Kazior 已提交
900 901 902
	memset(src_ring->base_addr_owner_space, 0,
	       nentries * sizeof(struct ce_desc));

903
	src_ring->sw_index = ath10k_ce_src_ring_read_index_get(ar, ctrl_addr);
904
	src_ring->sw_index &= src_ring->nentries_mask;
905 906 907 908
	src_ring->hw_index = src_ring->sw_index;

	src_ring->write_index =
		ath10k_ce_src_ring_write_index_get(ar, ctrl_addr);
909
	src_ring->write_index &= src_ring->nentries_mask;
910

911 912 913 914 915 916 917 918
	ath10k_ce_src_ring_base_addr_set(ar, ctrl_addr,
					 src_ring->base_addr_ce_space);
	ath10k_ce_src_ring_size_set(ar, ctrl_addr, nentries);
	ath10k_ce_src_ring_dmax_set(ar, ctrl_addr, attr->src_sz_max);
	ath10k_ce_src_ring_byte_swap_set(ar, ctrl_addr, 0);
	ath10k_ce_src_ring_lowmark_set(ar, ctrl_addr, 0);
	ath10k_ce_src_ring_highmark_set(ar, ctrl_addr, nentries);

919
	ath10k_dbg(ar, ATH10K_DBG_BOOT,
920
		   "boot init ce src ring id %d entries %d base_addr %pK\n",
921 922 923 924 925 926 927 928 929
		   ce_id, nentries, src_ring->base_addr_owner_space);

	return 0;
}

static int ath10k_ce_init_dest_ring(struct ath10k *ar,
				    unsigned int ce_id,
				    const struct ce_attr *attr)
{
930 931
	struct ath10k_ce *ce = ath10k_ce_priv(ar);
	struct ath10k_ce_pipe *ce_state = &ce->ce_states[ce_id];
932
	struct ath10k_ce_ring *dest_ring = ce_state->dest_ring;
M
Michal Kazior 已提交
933
	u32 nentries, ctrl_addr = ath10k_ce_base_address(ar, ce_id);
934 935 936

	nentries = roundup_pow_of_two(attr->dest_nentries);

M
Michal Kazior 已提交
937 938 939
	memset(dest_ring->base_addr_owner_space, 0,
	       nentries * sizeof(struct ce_desc));

940 941 942 943 944 945 946 947 948 949 950 951 952
	dest_ring->sw_index = ath10k_ce_dest_ring_read_index_get(ar, ctrl_addr);
	dest_ring->sw_index &= dest_ring->nentries_mask;
	dest_ring->write_index =
		ath10k_ce_dest_ring_write_index_get(ar, ctrl_addr);
	dest_ring->write_index &= dest_ring->nentries_mask;

	ath10k_ce_dest_ring_base_addr_set(ar, ctrl_addr,
					  dest_ring->base_addr_ce_space);
	ath10k_ce_dest_ring_size_set(ar, ctrl_addr, nentries);
	ath10k_ce_dest_ring_byte_swap_set(ar, ctrl_addr, 0);
	ath10k_ce_dest_ring_lowmark_set(ar, ctrl_addr, 0);
	ath10k_ce_dest_ring_highmark_set(ar, ctrl_addr, nentries);

953
	ath10k_dbg(ar, ATH10K_DBG_BOOT,
954
		   "boot ce dest ring id %d entries %d base_addr %pK\n",
955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978
		   ce_id, nentries, dest_ring->base_addr_owner_space);

	return 0;
}

static struct ath10k_ce_ring *
ath10k_ce_alloc_src_ring(struct ath10k *ar, unsigned int ce_id,
			 const struct ce_attr *attr)
{
	struct ath10k_ce_ring *src_ring;
	u32 nentries = attr->src_nentries;
	dma_addr_t base_addr;

	nentries = roundup_pow_of_two(nentries);

	src_ring = kzalloc(sizeof(*src_ring) +
			   (nentries *
			    sizeof(*src_ring->per_transfer_context)),
			   GFP_KERNEL);
	if (src_ring == NULL)
		return ERR_PTR(-ENOMEM);

	src_ring->nentries = nentries;
	src_ring->nentries_mask = nentries - 1;
979 980 981 982 983 984

	/*
	 * Legacy platforms that do not support cache
	 * coherent DMA are unsupported
	 */
	src_ring->base_addr_owner_space_unaligned =
985 986 987 988
		dma_alloc_coherent(ar->dev,
				   (nentries * sizeof(struct ce_desc) +
				    CE_DESC_RING_ALIGN),
				   &base_addr, GFP_KERNEL);
989
	if (!src_ring->base_addr_owner_space_unaligned) {
990 991
		kfree(src_ring);
		return ERR_PTR(-ENOMEM);
992 993
	}

994 995 996 997 998 999 1000 1001 1002
	src_ring->base_addr_ce_space_unaligned = base_addr;

	src_ring->base_addr_owner_space = PTR_ALIGN(
			src_ring->base_addr_owner_space_unaligned,
			CE_DESC_RING_ALIGN);
	src_ring->base_addr_ce_space = ALIGN(
			src_ring->base_addr_ce_space_unaligned,
			CE_DESC_RING_ALIGN);

1003
	return src_ring;
1004 1005
}

1006 1007 1008
static struct ath10k_ce_ring *
ath10k_ce_alloc_dest_ring(struct ath10k *ar, unsigned int ce_id,
			  const struct ce_attr *attr)
1009
{
1010
	struct ath10k_ce_ring *dest_ring;
1011
	u32 nentries;
1012 1013
	dma_addr_t base_addr;

1014
	nentries = roundup_pow_of_two(attr->dest_nentries);
1015

1016 1017 1018 1019 1020 1021
	dest_ring = kzalloc(sizeof(*dest_ring) +
			    (nentries *
			     sizeof(*dest_ring->per_transfer_context)),
			    GFP_KERNEL);
	if (dest_ring == NULL)
		return ERR_PTR(-ENOMEM);
1022 1023 1024 1025 1026 1027 1028 1029 1030

	dest_ring->nentries = nentries;
	dest_ring->nentries_mask = nentries - 1;

	/*
	 * Legacy platforms that do not support cache
	 * coherent DMA are unsupported
	 */
	dest_ring->base_addr_owner_space_unaligned =
1031
		dma_zalloc_coherent(ar->dev,
K
Kalle Valo 已提交
1032 1033 1034
				    (nentries * sizeof(struct ce_desc) +
				     CE_DESC_RING_ALIGN),
				    &base_addr, GFP_KERNEL);
1035
	if (!dest_ring->base_addr_owner_space_unaligned) {
1036 1037
		kfree(dest_ring);
		return ERR_PTR(-ENOMEM);
1038 1039
	}

1040 1041 1042 1043 1044 1045 1046 1047 1048
	dest_ring->base_addr_ce_space_unaligned = base_addr;

	dest_ring->base_addr_owner_space = PTR_ALIGN(
			dest_ring->base_addr_owner_space_unaligned,
			CE_DESC_RING_ALIGN);
	dest_ring->base_addr_ce_space = ALIGN(
			dest_ring->base_addr_ce_space_unaligned,
			CE_DESC_RING_ALIGN);

1049
	return dest_ring;
1050 1051 1052 1053 1054 1055 1056 1057 1058
}

/*
 * Initialize a Copy Engine based on caller-supplied attributes.
 * This may be called once to initialize both source and destination
 * rings or it may be called twice for separate source and destination
 * initialization. It may be that only one side or the other is
 * initialized by software/firmware.
 */
1059
int ath10k_ce_init_pipe(struct ath10k *ar, unsigned int ce_id,
1060
			const struct ce_attr *attr)
1061
{
1062
	int ret;
1063 1064

	if (attr->src_nentries) {
1065
		ret = ath10k_ce_init_src_ring(ar, ce_id, attr);
1066
		if (ret) {
1067
			ath10k_err(ar, "Failed to initialize CE src ring for ID: %d (%d)\n",
1068
				   ce_id, ret);
1069
			return ret;
1070 1071 1072 1073
		}
	}

	if (attr->dest_nentries) {
1074
		ret = ath10k_ce_init_dest_ring(ar, ce_id, attr);
1075
		if (ret) {
1076
			ath10k_err(ar, "Failed to initialize CE dest ring for ID: %d (%d)\n",
1077
				   ce_id, ret);
1078
			return ret;
1079 1080 1081
		}
	}

1082
	return 0;
1083 1084
}

1085
static void ath10k_ce_deinit_src_ring(struct ath10k *ar, unsigned int ce_id)
1086
{
M
Michal Kazior 已提交
1087
	u32 ctrl_addr = ath10k_ce_base_address(ar, ce_id);
1088 1089 1090 1091 1092 1093 1094 1095 1096

	ath10k_ce_src_ring_base_addr_set(ar, ctrl_addr, 0);
	ath10k_ce_src_ring_size_set(ar, ctrl_addr, 0);
	ath10k_ce_src_ring_dmax_set(ar, ctrl_addr, 0);
	ath10k_ce_src_ring_highmark_set(ar, ctrl_addr, 0);
}

static void ath10k_ce_deinit_dest_ring(struct ath10k *ar, unsigned int ce_id)
{
M
Michal Kazior 已提交
1097
	u32 ctrl_addr = ath10k_ce_base_address(ar, ce_id);
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110

	ath10k_ce_dest_ring_base_addr_set(ar, ctrl_addr, 0);
	ath10k_ce_dest_ring_size_set(ar, ctrl_addr, 0);
	ath10k_ce_dest_ring_highmark_set(ar, ctrl_addr, 0);
}

void ath10k_ce_deinit_pipe(struct ath10k *ar, unsigned int ce_id)
{
	ath10k_ce_deinit_src_ring(ar, ce_id);
	ath10k_ce_deinit_dest_ring(ar, ce_id);
}

int ath10k_ce_alloc_pipe(struct ath10k *ar, int ce_id,
1111
			 const struct ce_attr *attr)
1112
{
1113 1114
	struct ath10k_ce *ce = ath10k_ce_priv(ar);
	struct ath10k_ce_pipe *ce_state = &ce->ce_states[ce_id];
1115 1116
	int ret;

1117 1118 1119 1120 1121 1122
	/*
	 * Make sure there's enough CE ringbuffer entries for HTT TX to avoid
	 * additional TX locking checks.
	 *
	 * For the lack of a better place do the check here.
	 */
1123
	BUILD_BUG_ON(2 * TARGET_NUM_MSDU_DESC >
1124
		     (CE_HTT_H2T_MSG_SRC_NENTRIES - 1));
1125
	BUILD_BUG_ON(2 * TARGET_10_4_NUM_MSDU_DESC_PFC >
1126
		     (CE_HTT_H2T_MSG_SRC_NENTRIES - 1));
1127
	BUILD_BUG_ON(2 * TARGET_TLV_NUM_MSDU_DESC >
M
Michal Kazior 已提交
1128
		     (CE_HTT_H2T_MSG_SRC_NENTRIES - 1));
1129 1130 1131

	ce_state->ar = ar;
	ce_state->id = ce_id;
M
Michal Kazior 已提交
1132
	ce_state->ctrl_addr = ath10k_ce_base_address(ar, ce_id);
1133 1134 1135 1136
	ce_state->attr_flags = attr->flags;
	ce_state->src_sz_max = attr->src_sz_max;

	if (attr->src_nentries)
1137
		ce_state->send_cb = attr->send_cb;
1138 1139

	if (attr->dest_nentries)
1140
		ce_state->recv_cb = attr->recv_cb;
1141

1142 1143 1144 1145
	if (attr->src_nentries) {
		ce_state->src_ring = ath10k_ce_alloc_src_ring(ar, ce_id, attr);
		if (IS_ERR(ce_state->src_ring)) {
			ret = PTR_ERR(ce_state->src_ring);
1146
			ath10k_err(ar, "failed to allocate copy engine source ring %d: %d\n",
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
				   ce_id, ret);
			ce_state->src_ring = NULL;
			return ret;
		}
	}

	if (attr->dest_nentries) {
		ce_state->dest_ring = ath10k_ce_alloc_dest_ring(ar, ce_id,
								attr);
		if (IS_ERR(ce_state->dest_ring)) {
			ret = PTR_ERR(ce_state->dest_ring);
1158
			ath10k_err(ar, "failed to allocate copy engine destination ring %d: %d\n",
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169
				   ce_id, ret);
			ce_state->dest_ring = NULL;
			return ret;
		}
	}

	return 0;
}

void ath10k_ce_free_pipe(struct ath10k *ar, int ce_id)
{
1170 1171
	struct ath10k_ce *ce = ath10k_ce_priv(ar);
	struct ath10k_ce_pipe *ce_state = &ce->ce_states[ce_id];
1172 1173

	if (ce_state->src_ring) {
1174 1175 1176 1177 1178 1179
		dma_free_coherent(ar->dev,
				  (ce_state->src_ring->nentries *
				   sizeof(struct ce_desc) +
				   CE_DESC_RING_ALIGN),
				  ce_state->src_ring->base_addr_owner_space,
				  ce_state->src_ring->base_addr_ce_space);
1180 1181 1182 1183
		kfree(ce_state->src_ring);
	}

	if (ce_state->dest_ring) {
1184 1185 1186 1187 1188 1189
		dma_free_coherent(ar->dev,
				  (ce_state->dest_ring->nentries *
				   sizeof(struct ce_desc) +
				   CE_DESC_RING_ALIGN),
				  ce_state->dest_ring->base_addr_owner_space,
				  ce_state->dest_ring->base_addr_ce_space);
1190 1191
		kfree(ce_state->dest_ring);
	}
1192 1193 1194

	ce_state->src_ring = NULL;
	ce_state->dest_ring = NULL;
1195
}
1196 1197 1198 1199

void ath10k_ce_dump_registers(struct ath10k *ar,
			      struct ath10k_fw_crash_data *crash_data)
{
1200 1201
	struct ath10k_ce *ce = ath10k_ce_priv(ar);
	struct ath10k_ce_crash_data ce_data;
1202 1203 1204 1205 1206 1207
	u32 addr, id;

	lockdep_assert_held(&ar->data_lock);

	ath10k_err(ar, "Copy Engine register dump:\n");

1208
	spin_lock_bh(&ce->ce_lock);
1209 1210
	for (id = 0; id < CE_COUNT; id++) {
		addr = ath10k_ce_base_address(ar, id);
1211
		ce_data.base_addr = cpu_to_le32(addr);
1212

1213
		ce_data.src_wr_idx =
1214
			cpu_to_le32(ath10k_ce_src_ring_write_index_get(ar, addr));
1215
		ce_data.src_r_idx =
1216
			cpu_to_le32(ath10k_ce_src_ring_read_index_get(ar, addr));
1217
		ce_data.dst_wr_idx =
1218
			cpu_to_le32(ath10k_ce_dest_ring_write_index_get(ar, addr));
1219
		ce_data.dst_r_idx =
1220 1221 1222
			cpu_to_le32(ath10k_ce_dest_ring_read_index_get(ar, addr));

		if (crash_data)
1223
			crash_data->ce_crash_data[id] = ce_data;
1224 1225

		ath10k_err(ar, "[%02d]: 0x%08x %3u %3u %3u %3u", id,
1226 1227 1228 1229 1230
			   le32_to_cpu(ce_data.base_addr),
			   le32_to_cpu(ce_data.src_wr_idx),
			   le32_to_cpu(ce_data.src_r_idx),
			   le32_to_cpu(ce_data.dst_wr_idx),
			   le32_to_cpu(ce_data.dst_r_idx));
1231 1232
	}

1233
	spin_unlock_bh(&ce->ce_lock);
1234
}