xref: /openbmc/linux/drivers/net/ipa/gsi_trans.c (revision 11902b41)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 /* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
4  * Copyright (C) 2019-2020 Linaro Ltd.
5  */
6 
7 #include <linux/types.h>
8 #include <linux/bits.h>
9 #include <linux/bitfield.h>
10 #include <linux/refcount.h>
11 #include <linux/scatterlist.h>
12 #include <linux/dma-direction.h>
13 
14 #include "gsi.h"
15 #include "gsi_private.h"
16 #include "gsi_trans.h"
17 #include "ipa_gsi.h"
18 #include "ipa_data.h"
19 #include "ipa_cmd.h"
20 
21 /**
22  * DOC: GSI Transactions
23  *
24  * A GSI transaction abstracts the behavior of a GSI channel by representing
25  * everything about a related group of IPA commands in a single structure.
26  * (A "command" in this sense is either a data transfer or an IPA immediate
27  * command.)  Most details of interaction with the GSI hardware are managed
28  * by the GSI transaction core, allowing users to simply describe commands
29  * to be performed.  When a transaction has completed a callback function
30  * (dependent on the type of endpoint associated with the channel) allows
31  * cleanup of resources associated with the transaction.
32  *
33  * To perform a command (or set of them), a user of the GSI transaction
34  * interface allocates a transaction, indicating the number of TREs required
35  * (one per command).  If sufficient TREs are available, they are reserved
36  * for use in the transaction and the allocation succeeds.  This way
37  * exhaustion of the available TREs in a channel ring is detected
38  * as early as possible.  All resources required to complete a transaction
39  * are allocated at transaction allocation time.
40  *
41  * Commands performed as part of a transaction are represented in an array
42  * of Linux scatterlist structures.  This array is allocated with the
43  * transaction, and its entries are initialized using standard scatterlist
44  * functions (such as sg_set_buf() or skb_to_sgvec()).
45  *
46  * Once a transaction's scatterlist structures have been initialized, the
47  * transaction is committed.  The caller is responsible for mapping buffers
48  * for DMA if necessary, and this should be done *before* allocating
49  * the transaction.  Between a successful allocation and commit of a
50  * transaction no errors should occur.
51  *
52  * Committing transfers ownership of the entire transaction to the GSI
53  * transaction core.  The GSI transaction code formats the content of
54  * the scatterlist array into the channel ring buffer and informs the
55  * hardware that new TREs are available to process.
56  *
57  * The last TRE in each transaction is marked to interrupt the AP when the
58  * GSI hardware has completed it.  Because transfers described by TREs are
59  * performed strictly in order, signaling the completion of just the last
60  * TRE in the transaction is sufficient to indicate the full transaction
61  * is complete.
62  *
63  * When a transaction is complete, ipa_gsi_trans_complete() is called by the
64  * GSI code into the IPA layer, allowing it to perform any final cleanup
65  * required before the transaction is freed.
66  */
67 
68 /* Hardware values representing a transfer element type */
69 enum gsi_tre_type {
70 	GSI_RE_XFER	= 0x2,
71 	GSI_RE_IMMD_CMD	= 0x3,
72 };
73 
74 /* An entry in a channel ring */
75 struct gsi_tre {
76 	__le64 addr;		/* DMA address */
77 	__le16 len_opcode;	/* length in bytes or enum IPA_CMD_* */
78 	__le16 reserved;
79 	__le32 flags;		/* TRE_FLAGS_* */
80 };
81 
82 /* gsi_tre->flags mask values (in CPU byte order) */
83 #define TRE_FLAGS_CHAIN_FMASK	GENMASK(0, 0)
84 #define TRE_FLAGS_IEOT_FMASK	GENMASK(9, 9)
85 #define TRE_FLAGS_BEI_FMASK	GENMASK(10, 10)
86 #define TRE_FLAGS_TYPE_FMASK	GENMASK(23, 16)
87 
88 int gsi_trans_pool_init(struct gsi_trans_pool *pool, size_t size, u32 count,
89 			u32 max_alloc)
90 {
91 	void *virt;
92 
93 	if (!size)
94 		return -EINVAL;
95 	if (count < max_alloc)
96 		return -EINVAL;
97 	if (!max_alloc)
98 		return -EINVAL;
99 
100 	/* By allocating a few extra entries in our pool (one less
101 	 * than the maximum number that will be requested in a
102 	 * single allocation), we can always satisfy requests without
103 	 * ever worrying about straddling the end of the pool array.
104 	 * If there aren't enough entries starting at the free index,
105 	 * we just allocate free entries from the beginning of the pool.
106 	 */
107 	virt = kcalloc(count + max_alloc - 1, size, GFP_KERNEL);
108 	if (!virt)
109 		return -ENOMEM;
110 
111 	pool->base = virt;
112 	/* If the allocator gave us any extra memory, use it */
113 	pool->count = ksize(pool->base) / size;
114 	pool->free = 0;
115 	pool->max_alloc = max_alloc;
116 	pool->size = size;
117 	pool->addr = 0;		/* Only used for DMA pools */
118 
119 	return 0;
120 }
121 
122 void gsi_trans_pool_exit(struct gsi_trans_pool *pool)
123 {
124 	kfree(pool->base);
125 	memset(pool, 0, sizeof(*pool));
126 }
127 
128 /* Allocate the requested number of (zeroed) entries from the pool */
129 /* Home-grown DMA pool.  This way we can preallocate and use the tre_count
130  * to guarantee allocations will succeed.  Even though we specify max_alloc
131  * (and it can be more than one), we only allow allocation of a single
132  * element from a DMA pool.
133  */
134 int gsi_trans_pool_init_dma(struct device *dev, struct gsi_trans_pool *pool,
135 			    size_t size, u32 count, u32 max_alloc)
136 {
137 	size_t total_size;
138 	dma_addr_t addr;
139 	void *virt;
140 
141 	if (!size)
142 		return -EINVAL;
143 	if (count < max_alloc)
144 		return -EINVAL;
145 	if (!max_alloc)
146 		return -EINVAL;
147 
148 	/* Don't let allocations cross a power-of-two boundary */
149 	size = __roundup_pow_of_two(size);
150 	total_size = (count + max_alloc - 1) * size;
151 
152 	/* The allocator will give us a power-of-2 number of pages
153 	 * sufficient to satisfy our request.  Round up our requested
154 	 * size to avoid any unused space in the allocation.  This way
155 	 * gsi_trans_pool_exit_dma() can assume the total allocated
156 	 * size is exactly (count * size).
157 	 */
158 	total_size = get_order(total_size) << PAGE_SHIFT;
159 
160 	virt = dma_alloc_coherent(dev, total_size, &addr, GFP_KERNEL);
161 	if (!virt)
162 		return -ENOMEM;
163 
164 	pool->base = virt;
165 	pool->count = total_size / size;
166 	pool->free = 0;
167 	pool->size = size;
168 	pool->max_alloc = max_alloc;
169 	pool->addr = addr;
170 
171 	return 0;
172 }
173 
174 void gsi_trans_pool_exit_dma(struct device *dev, struct gsi_trans_pool *pool)
175 {
176 	size_t total_size = pool->count * pool->size;
177 
178 	dma_free_coherent(dev, total_size, pool->base, pool->addr);
179 	memset(pool, 0, sizeof(*pool));
180 }
181 
182 /* Return the byte offset of the next free entry in the pool */
183 static u32 gsi_trans_pool_alloc_common(struct gsi_trans_pool *pool, u32 count)
184 {
185 	u32 offset;
186 
187 	WARN_ON(!count);
188 	WARN_ON(count > pool->max_alloc);
189 
190 	/* Allocate from beginning if wrap would occur */
191 	if (count > pool->count - pool->free)
192 		pool->free = 0;
193 
194 	offset = pool->free * pool->size;
195 	pool->free += count;
196 	memset(pool->base + offset, 0, count * pool->size);
197 
198 	return offset;
199 }
200 
201 /* Allocate a contiguous block of zeroed entries from a pool */
202 void *gsi_trans_pool_alloc(struct gsi_trans_pool *pool, u32 count)
203 {
204 	return pool->base + gsi_trans_pool_alloc_common(pool, count);
205 }
206 
207 /* Allocate a single zeroed entry from a DMA pool */
208 void *gsi_trans_pool_alloc_dma(struct gsi_trans_pool *pool, dma_addr_t *addr)
209 {
210 	u32 offset = gsi_trans_pool_alloc_common(pool, 1);
211 
212 	*addr = pool->addr + offset;
213 
214 	return pool->base + offset;
215 }
216 
217 /* Map a TRE ring entry index to the transaction it is associated with */
218 static void gsi_trans_map(struct gsi_trans *trans, u32 index)
219 {
220 	struct gsi_channel *channel = &trans->gsi->channel[trans->channel_id];
221 
222 	/* The completion event will indicate the last TRE used */
223 	index += trans->used_count - 1;
224 
225 	/* Note: index *must* be used modulo the ring count here */
226 	channel->trans_info.map[index % channel->tre_ring.count] = trans;
227 }
228 
229 /* Return the transaction mapped to a given ring entry */
230 struct gsi_trans *
231 gsi_channel_trans_mapped(struct gsi_channel *channel, u32 index)
232 {
233 	/* Note: index *must* be used modulo the ring count here */
234 	return channel->trans_info.map[index % channel->tre_ring.count];
235 }
236 
237 /* Return the oldest completed transaction for a channel (or null) */
238 struct gsi_trans *gsi_channel_trans_complete(struct gsi_channel *channel)
239 {
240 	struct gsi_trans_info *trans_info = &channel->trans_info;
241 	u16 trans_id = trans_info->completed_id;
242 
243 	if (trans_id == trans_info->pending_id)
244 		return NULL;
245 
246 	return &trans_info->trans[trans_id %= channel->tre_count];
247 }
248 
249 /* Move a transaction from allocated to committed state */
250 static void gsi_trans_move_committed(struct gsi_trans *trans)
251 {
252 	struct gsi_channel *channel = &trans->gsi->channel[trans->channel_id];
253 	struct gsi_trans_info *trans_info = &channel->trans_info;
254 
255 	spin_lock_bh(&trans_info->spinlock);
256 
257 	list_add_tail(&trans->links, &trans_info->committed);
258 
259 	spin_unlock_bh(&trans_info->spinlock);
260 
261 	/* This allocated transaction is now committed */
262 	trans_info->allocated_id++;
263 }
264 
265 /* Move transactions from the committed list to the pending list */
266 static void gsi_trans_move_pending(struct gsi_trans *trans)
267 {
268 	struct gsi_channel *channel = &trans->gsi->channel[trans->channel_id];
269 	struct gsi_trans_info *trans_info = &channel->trans_info;
270 	u16 trans_index = trans - &trans_info->trans[0];
271 	struct list_head list;
272 	u16 delta;
273 
274 	spin_lock_bh(&trans_info->spinlock);
275 
276 	/* Move this transaction and all predecessors to the pending list */
277 	list_cut_position(&list, &trans_info->committed, &trans->links);
278 	list_splice_tail(&list, &trans_info->pending);
279 
280 	spin_unlock_bh(&trans_info->spinlock);
281 
282 	/* These committed transactions are now pending */
283 	delta = trans_index - trans_info->committed_id + 1;
284 	trans_info->committed_id += delta % channel->tre_count;
285 }
286 
287 /* Move a transaction and all of its predecessors from the pending list
288  * to the completed list.
289  */
290 void gsi_trans_move_complete(struct gsi_trans *trans)
291 {
292 	struct gsi_channel *channel = &trans->gsi->channel[trans->channel_id];
293 	struct gsi_trans_info *trans_info = &channel->trans_info;
294 	u16 trans_index = trans - trans_info->trans;
295 	struct list_head list;
296 	u16 delta;
297 
298 	spin_lock_bh(&trans_info->spinlock);
299 
300 	/* Move this transaction and all predecessors to completed list */
301 	list_cut_position(&list, &trans_info->pending, &trans->links);
302 	list_splice_tail(&list, &trans_info->complete);
303 
304 	spin_unlock_bh(&trans_info->spinlock);
305 
306 	/* These pending transactions are now completed */
307 	delta = trans_index - trans_info->pending_id + 1;
308 	delta %= channel->tre_count;
309 	trans_info->pending_id += delta;
310 }
311 
312 /* Move a transaction from the completed list to the polled list */
313 void gsi_trans_move_polled(struct gsi_trans *trans)
314 {
315 	struct gsi_channel *channel = &trans->gsi->channel[trans->channel_id];
316 	struct gsi_trans_info *trans_info = &channel->trans_info;
317 
318 	spin_lock_bh(&trans_info->spinlock);
319 
320 	list_move_tail(&trans->links, &trans_info->polled);
321 
322 	spin_unlock_bh(&trans_info->spinlock);
323 
324 	/* This completed transaction is now polled */
325 	trans_info->completed_id++;
326 }
327 
328 /* Reserve some number of TREs on a channel.  Returns true if successful */
329 static bool
330 gsi_trans_tre_reserve(struct gsi_trans_info *trans_info, u32 tre_count)
331 {
332 	int avail = atomic_read(&trans_info->tre_avail);
333 	int new;
334 
335 	do {
336 		new = avail - (int)tre_count;
337 		if (unlikely(new < 0))
338 			return false;
339 	} while (!atomic_try_cmpxchg(&trans_info->tre_avail, &avail, new));
340 
341 	return true;
342 }
343 
344 /* Release previously-reserved TRE entries to a channel */
345 static void
346 gsi_trans_tre_release(struct gsi_trans_info *trans_info, u32 tre_count)
347 {
348 	atomic_add(tre_count, &trans_info->tre_avail);
349 }
350 
351 /* Return true if no transactions are allocated, false otherwise */
352 bool gsi_channel_trans_idle(struct gsi *gsi, u32 channel_id)
353 {
354 	u32 tre_max = gsi_channel_tre_max(gsi, channel_id);
355 	struct gsi_trans_info *trans_info;
356 
357 	trans_info = &gsi->channel[channel_id].trans_info;
358 
359 	return atomic_read(&trans_info->tre_avail) == tre_max;
360 }
361 
362 /* Allocate a GSI transaction on a channel */
363 struct gsi_trans *gsi_channel_trans_alloc(struct gsi *gsi, u32 channel_id,
364 					  u32 tre_count,
365 					  enum dma_data_direction direction)
366 {
367 	struct gsi_channel *channel = &gsi->channel[channel_id];
368 	struct gsi_trans_info *trans_info;
369 	struct gsi_trans *trans;
370 	u16 trans_index;
371 
372 	if (WARN_ON(tre_count > channel->trans_tre_max))
373 		return NULL;
374 
375 	trans_info = &channel->trans_info;
376 
377 	/* If we can't reserve the TREs for the transaction, we're done */
378 	if (!gsi_trans_tre_reserve(trans_info, tre_count))
379 		return NULL;
380 
381 	trans_index = trans_info->free_id % channel->tre_count;
382 	trans = &trans_info->trans[trans_index];
383 	memset(trans, 0, sizeof(*trans));
384 
385 	/* Initialize non-zero fields in the transaction */
386 	INIT_LIST_HEAD(&trans->links);
387 	trans->gsi = gsi;
388 	trans->channel_id = channel_id;
389 	trans->rsvd_count = tre_count;
390 	init_completion(&trans->completion);
391 
392 	/* Allocate the scatterlist */
393 	trans->sgl = gsi_trans_pool_alloc(&trans_info->sg_pool, tre_count);
394 	sg_init_marker(trans->sgl, tre_count);
395 
396 	trans->direction = direction;
397 	refcount_set(&trans->refcount, 1);
398 
399 	/* This free transaction will now be allocated */
400 	trans_info->free_id++;
401 
402 	return trans;
403 }
404 
405 /* Free a previously-allocated transaction */
406 void gsi_trans_free(struct gsi_trans *trans)
407 {
408 	refcount_t *refcount = &trans->refcount;
409 	struct gsi_trans_info *trans_info;
410 	bool last;
411 
412 	/* We must hold the lock to release the last reference */
413 	if (refcount_dec_not_one(refcount))
414 		return;
415 
416 	trans_info = &trans->gsi->channel[trans->channel_id].trans_info;
417 
418 	spin_lock_bh(&trans_info->spinlock);
419 
420 	/* Reference might have been added before we got the lock */
421 	last = refcount_dec_and_test(refcount);
422 	if (last)
423 		list_del(&trans->links);
424 
425 	spin_unlock_bh(&trans_info->spinlock);
426 
427 	if (!last)
428 		return;
429 
430 	/* Unused transactions are allocated but never committed, pending,
431 	 * completed, or polled.
432 	 */
433 	if (!trans->used_count) {
434 		trans_info->allocated_id++;
435 		trans_info->committed_id++;
436 		trans_info->pending_id++;
437 		trans_info->completed_id++;
438 	} else {
439 		ipa_gsi_trans_release(trans);
440 	}
441 
442 	/* This transaction is now free */
443 	trans_info->polled_id++;
444 
445 	/* Releasing the reserved TREs implicitly frees the sgl[] and
446 	 * (if present) info[] arrays, plus the transaction itself.
447 	 */
448 	gsi_trans_tre_release(trans_info, trans->rsvd_count);
449 }
450 
451 /* Add an immediate command to a transaction */
452 void gsi_trans_cmd_add(struct gsi_trans *trans, void *buf, u32 size,
453 		       dma_addr_t addr, enum ipa_cmd_opcode opcode)
454 {
455 	u32 which = trans->used_count++;
456 	struct scatterlist *sg;
457 
458 	WARN_ON(which >= trans->rsvd_count);
459 
460 	/* Commands are quite different from data transfer requests.
461 	 * Their payloads come from a pool whose memory is allocated
462 	 * using dma_alloc_coherent().  We therefore do *not* map them
463 	 * for DMA (unlike what we do for pages and skbs).
464 	 *
465 	 * When a transaction completes, the SGL is normally unmapped.
466 	 * A command transaction has direction DMA_NONE, which tells
467 	 * gsi_trans_complete() to skip the unmapping step.
468 	 *
469 	 * The only things we use directly in a command scatter/gather
470 	 * entry are the DMA address and length.  We still need the SG
471 	 * table flags to be maintained though, so assign a NULL page
472 	 * pointer for that purpose.
473 	 */
474 	sg = &trans->sgl[which];
475 	sg_assign_page(sg, NULL);
476 	sg_dma_address(sg) = addr;
477 	sg_dma_len(sg) = size;
478 
479 	trans->cmd_opcode[which] = opcode;
480 }
481 
482 /* Add a page transfer to a transaction.  It will fill the only TRE. */
483 int gsi_trans_page_add(struct gsi_trans *trans, struct page *page, u32 size,
484 		       u32 offset)
485 {
486 	struct scatterlist *sg = &trans->sgl[0];
487 	int ret;
488 
489 	if (WARN_ON(trans->rsvd_count != 1))
490 		return -EINVAL;
491 	if (WARN_ON(trans->used_count))
492 		return -EINVAL;
493 
494 	sg_set_page(sg, page, size, offset);
495 	ret = dma_map_sg(trans->gsi->dev, sg, 1, trans->direction);
496 	if (!ret)
497 		return -ENOMEM;
498 
499 	trans->used_count++;	/* Transaction now owns the (DMA mapped) page */
500 
501 	return 0;
502 }
503 
504 /* Add an SKB transfer to a transaction.  No other TREs will be used. */
505 int gsi_trans_skb_add(struct gsi_trans *trans, struct sk_buff *skb)
506 {
507 	struct scatterlist *sg = &trans->sgl[0];
508 	u32 used_count;
509 	int ret;
510 
511 	if (WARN_ON(trans->rsvd_count != 1))
512 		return -EINVAL;
513 	if (WARN_ON(trans->used_count))
514 		return -EINVAL;
515 
516 	/* skb->len will not be 0 (checked early) */
517 	ret = skb_to_sgvec(skb, sg, 0, skb->len);
518 	if (ret < 0)
519 		return ret;
520 	used_count = ret;
521 
522 	ret = dma_map_sg(trans->gsi->dev, sg, used_count, trans->direction);
523 	if (!ret)
524 		return -ENOMEM;
525 
526 	/* Transaction now owns the (DMA mapped) skb */
527 	trans->used_count += used_count;
528 
529 	return 0;
530 }
531 
532 /* Compute the length/opcode value to use for a TRE */
533 static __le16 gsi_tre_len_opcode(enum ipa_cmd_opcode opcode, u32 len)
534 {
535 	return opcode == IPA_CMD_NONE ? cpu_to_le16((u16)len)
536 				      : cpu_to_le16((u16)opcode);
537 }
538 
539 /* Compute the flags value to use for a given TRE */
540 static __le32 gsi_tre_flags(bool last_tre, bool bei, enum ipa_cmd_opcode opcode)
541 {
542 	enum gsi_tre_type tre_type;
543 	u32 tre_flags;
544 
545 	tre_type = opcode == IPA_CMD_NONE ? GSI_RE_XFER : GSI_RE_IMMD_CMD;
546 	tre_flags = u32_encode_bits(tre_type, TRE_FLAGS_TYPE_FMASK);
547 
548 	/* Last TRE contains interrupt flags */
549 	if (last_tre) {
550 		/* All transactions end in a transfer completion interrupt */
551 		tre_flags |= TRE_FLAGS_IEOT_FMASK;
552 		/* Don't interrupt when outbound commands are acknowledged */
553 		if (bei)
554 			tre_flags |= TRE_FLAGS_BEI_FMASK;
555 	} else {	/* All others indicate there's more to come */
556 		tre_flags |= TRE_FLAGS_CHAIN_FMASK;
557 	}
558 
559 	return cpu_to_le32(tre_flags);
560 }
561 
562 static void gsi_trans_tre_fill(struct gsi_tre *dest_tre, dma_addr_t addr,
563 			       u32 len, bool last_tre, bool bei,
564 			       enum ipa_cmd_opcode opcode)
565 {
566 	struct gsi_tre tre;
567 
568 	tre.addr = cpu_to_le64(addr);
569 	tre.len_opcode = gsi_tre_len_opcode(opcode, len);
570 	tre.reserved = 0;
571 	tre.flags = gsi_tre_flags(last_tre, bei, opcode);
572 
573 	/* ARM64 can write 16 bytes as a unit with a single instruction.
574 	 * Doing the assignment this way is an attempt to make that happen.
575 	 */
576 	*dest_tre = tre;
577 }
578 
579 /**
580  * __gsi_trans_commit() - Common GSI transaction commit code
581  * @trans:	Transaction to commit
582  * @ring_db:	Whether to tell the hardware about these queued transfers
583  *
584  * Formats channel ring TRE entries based on the content of the scatterlist.
585  * Maps a transaction pointer to the last ring entry used for the transaction,
586  * so it can be recovered when it completes.  Moves the transaction to the
587  * pending list.  Finally, updates the channel ring pointer and optionally
588  * rings the doorbell.
589  */
590 static void __gsi_trans_commit(struct gsi_trans *trans, bool ring_db)
591 {
592 	struct gsi_channel *channel = &trans->gsi->channel[trans->channel_id];
593 	struct gsi_ring *tre_ring = &channel->tre_ring;
594 	enum ipa_cmd_opcode opcode = IPA_CMD_NONE;
595 	bool bei = channel->toward_ipa;
596 	struct gsi_tre *dest_tre;
597 	struct scatterlist *sg;
598 	u32 byte_count = 0;
599 	u8 *cmd_opcode;
600 	u32 avail;
601 	u32 i;
602 
603 	WARN_ON(!trans->used_count);
604 
605 	/* Consume the entries.  If we cross the end of the ring while
606 	 * filling them we'll switch to the beginning to finish.
607 	 * If there is no info array we're doing a simple data
608 	 * transfer request, whose opcode is IPA_CMD_NONE.
609 	 */
610 	cmd_opcode = channel->command ? &trans->cmd_opcode[0] : NULL;
611 	avail = tre_ring->count - tre_ring->index % tre_ring->count;
612 	dest_tre = gsi_ring_virt(tre_ring, tre_ring->index);
613 	for_each_sg(trans->sgl, sg, trans->used_count, i) {
614 		bool last_tre = i == trans->used_count - 1;
615 		dma_addr_t addr = sg_dma_address(sg);
616 		u32 len = sg_dma_len(sg);
617 
618 		byte_count += len;
619 		if (!avail--)
620 			dest_tre = gsi_ring_virt(tre_ring, 0);
621 		if (cmd_opcode)
622 			opcode = *cmd_opcode++;
623 
624 		gsi_trans_tre_fill(dest_tre, addr, len, last_tre, bei, opcode);
625 		dest_tre++;
626 	}
627 	/* Associate the TRE with the transaction */
628 	gsi_trans_map(trans, tre_ring->index);
629 
630 	tre_ring->index += trans->used_count;
631 
632 	trans->len = byte_count;
633 	if (channel->toward_ipa)
634 		gsi_trans_tx_committed(trans);
635 
636 	gsi_trans_move_committed(trans);
637 
638 	/* Ring doorbell if requested, or if all TREs are allocated */
639 	if (ring_db || !atomic_read(&channel->trans_info.tre_avail)) {
640 		/* Report what we're handing off to hardware for TX channels */
641 		if (channel->toward_ipa)
642 			gsi_trans_tx_queued(trans);
643 		gsi_trans_move_pending(trans);
644 		gsi_channel_doorbell(channel);
645 	}
646 }
647 
648 /* Commit a GSI transaction */
649 void gsi_trans_commit(struct gsi_trans *trans, bool ring_db)
650 {
651 	if (trans->used_count)
652 		__gsi_trans_commit(trans, ring_db);
653 	else
654 		gsi_trans_free(trans);
655 }
656 
657 /* Commit a GSI transaction and wait for it to complete */
658 void gsi_trans_commit_wait(struct gsi_trans *trans)
659 {
660 	if (!trans->used_count)
661 		goto out_trans_free;
662 
663 	refcount_inc(&trans->refcount);
664 
665 	__gsi_trans_commit(trans, true);
666 
667 	wait_for_completion(&trans->completion);
668 
669 out_trans_free:
670 	gsi_trans_free(trans);
671 }
672 
673 /* Process the completion of a transaction; called while polling */
674 void gsi_trans_complete(struct gsi_trans *trans)
675 {
676 	/* If the entire SGL was mapped when added, unmap it now */
677 	if (trans->direction != DMA_NONE)
678 		dma_unmap_sg(trans->gsi->dev, trans->sgl, trans->used_count,
679 			     trans->direction);
680 
681 	ipa_gsi_trans_complete(trans);
682 
683 	complete(&trans->completion);
684 
685 	gsi_trans_free(trans);
686 }
687 
688 /* Cancel a channel's pending transactions */
689 void gsi_channel_trans_cancel_pending(struct gsi_channel *channel)
690 {
691 	struct gsi_trans_info *trans_info = &channel->trans_info;
692 	u16 trans_id = trans_info->pending_id;
693 
694 	/* channel->gsi->mutex is held by caller */
695 	spin_lock_bh(&trans_info->spinlock);
696 
697 	list_splice_tail_init(&trans_info->pending, &trans_info->complete);
698 
699 	spin_unlock_bh(&trans_info->spinlock);
700 
701 	/* If there are no pending transactions, we're done */
702 	if (trans_id == trans_info->committed_id)
703 		return;
704 
705 	/* Mark all pending transactions cancelled */
706 	do {
707 		struct gsi_trans *trans;
708 
709 		trans = &trans_info->trans[trans_id % channel->tre_count];
710 		trans->cancelled = true;
711 	} while (++trans_id != trans_info->committed_id);
712 
713 	/* All pending transactions are now completed */
714 	trans_info->pending_id = trans_info->committed_id;
715 
716 	/* Schedule NAPI polling to complete the cancelled transactions */
717 	napi_schedule(&channel->napi);
718 }
719 
720 /* Issue a command to read a single byte from a channel */
721 int gsi_trans_read_byte(struct gsi *gsi, u32 channel_id, dma_addr_t addr)
722 {
723 	struct gsi_channel *channel = &gsi->channel[channel_id];
724 	struct gsi_ring *tre_ring = &channel->tre_ring;
725 	struct gsi_trans_info *trans_info;
726 	struct gsi_tre *dest_tre;
727 
728 	trans_info = &channel->trans_info;
729 
730 	/* First reserve the TRE, if possible */
731 	if (!gsi_trans_tre_reserve(trans_info, 1))
732 		return -EBUSY;
733 
734 	/* Now fill the reserved TRE and tell the hardware */
735 
736 	dest_tre = gsi_ring_virt(tre_ring, tre_ring->index);
737 	gsi_trans_tre_fill(dest_tre, addr, 1, true, false, IPA_CMD_NONE);
738 
739 	tre_ring->index++;
740 	gsi_channel_doorbell(channel);
741 
742 	return 0;
743 }
744 
745 /* Mark a gsi_trans_read_byte() request done */
746 void gsi_trans_read_byte_done(struct gsi *gsi, u32 channel_id)
747 {
748 	struct gsi_channel *channel = &gsi->channel[channel_id];
749 
750 	gsi_trans_tre_release(&channel->trans_info, 1);
751 }
752 
753 /* Initialize a channel's GSI transaction info */
754 int gsi_channel_trans_init(struct gsi *gsi, u32 channel_id)
755 {
756 	struct gsi_channel *channel = &gsi->channel[channel_id];
757 	u32 tre_count = channel->tre_count;
758 	struct gsi_trans_info *trans_info;
759 	u32 tre_max;
760 	int ret;
761 
762 	/* Ensure the size of a channel element is what's expected */
763 	BUILD_BUG_ON(sizeof(struct gsi_tre) != GSI_RING_ELEMENT_SIZE);
764 
765 	trans_info = &channel->trans_info;
766 
767 	/* The tre_avail field is what ultimately limits the number of
768 	 * outstanding transactions and their resources.  A transaction
769 	 * allocation succeeds only if the TREs available are sufficient
770 	 * for what the transaction might need.
771 	 */
772 	tre_max = gsi_channel_tre_max(channel->gsi, channel_id);
773 	atomic_set(&trans_info->tre_avail, tre_max);
774 
775 	/* We can't use more TREs than the number available in the ring.
776 	 * This limits the number of transactions that can be outstanding.
777 	 * Worst case is one TRE per transaction (but we actually limit
778 	 * it to something a little less than that).  By allocating a
779 	 * power-of-two number of transactions we can use an index
780 	 * modulo that number to determine the next one that's free.
781 	 * Transactions are allocated one at a time.
782 	 */
783 	trans_info->trans = kcalloc(tre_count, sizeof(*trans_info->trans),
784 				    GFP_KERNEL);
785 	if (!trans_info->trans)
786 		return -ENOMEM;
787 	trans_info->free_id = 0;	/* all modulo channel->tre_count */
788 	trans_info->allocated_id = 0;
789 	trans_info->committed_id = 0;
790 	trans_info->pending_id = 0;
791 	trans_info->completed_id = 0;
792 	trans_info->polled_id = 0;
793 
794 	/* A completion event contains a pointer to the TRE that caused
795 	 * the event (which will be the last one used by the transaction).
796 	 * Each entry in this map records the transaction associated
797 	 * with a corresponding completed TRE.
798 	 */
799 	trans_info->map = kcalloc(tre_count, sizeof(*trans_info->map),
800 				  GFP_KERNEL);
801 	if (!trans_info->map) {
802 		ret = -ENOMEM;
803 		goto err_trans_free;
804 	}
805 
806 	/* A transaction uses a scatterlist array to represent the data
807 	 * transfers implemented by the transaction.  Each scatterlist
808 	 * element is used to fill a single TRE when the transaction is
809 	 * committed.  So we need as many scatterlist elements as the
810 	 * maximum number of TREs that can be outstanding.
811 	 */
812 	ret = gsi_trans_pool_init(&trans_info->sg_pool,
813 				  sizeof(struct scatterlist),
814 				  tre_max, channel->trans_tre_max);
815 	if (ret)
816 		goto err_map_free;
817 
818 	spin_lock_init(&trans_info->spinlock);
819 	INIT_LIST_HEAD(&trans_info->committed);
820 	INIT_LIST_HEAD(&trans_info->pending);
821 	INIT_LIST_HEAD(&trans_info->complete);
822 	INIT_LIST_HEAD(&trans_info->polled);
823 
824 	return 0;
825 
826 err_map_free:
827 	kfree(trans_info->map);
828 err_trans_free:
829 	kfree(trans_info->trans);
830 
831 	dev_err(gsi->dev, "error %d initializing channel %u transactions\n",
832 		ret, channel_id);
833 
834 	return ret;
835 }
836 
837 /* Inverse of gsi_channel_trans_init() */
838 void gsi_channel_trans_exit(struct gsi_channel *channel)
839 {
840 	struct gsi_trans_info *trans_info = &channel->trans_info;
841 
842 	gsi_trans_pool_exit(&trans_info->sg_pool);
843 	kfree(trans_info->trans);
844 	kfree(trans_info->map);
845 }
846