xref: /OK3568_Linux_fs/kernel/drivers/net/ipa/gsi_trans.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun 
3*4882a593Smuzhiyun /* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
4*4882a593Smuzhiyun  * Copyright (C) 2019-2020 Linaro Ltd.
5*4882a593Smuzhiyun  */
6*4882a593Smuzhiyun #ifndef _GSI_TRANS_H_
7*4882a593Smuzhiyun #define _GSI_TRANS_H_
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun #include <linux/types.h>
10*4882a593Smuzhiyun #include <linux/refcount.h>
11*4882a593Smuzhiyun #include <linux/completion.h>
12*4882a593Smuzhiyun #include <linux/dma-direction.h>
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun #include "ipa_cmd.h"
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun struct scatterlist;
17*4882a593Smuzhiyun struct device;
18*4882a593Smuzhiyun struct sk_buff;
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun struct gsi;
21*4882a593Smuzhiyun struct gsi_trans;
22*4882a593Smuzhiyun struct gsi_trans_pool;
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun /**
25*4882a593Smuzhiyun  * struct gsi_trans - a GSI transaction
26*4882a593Smuzhiyun  *
27*4882a593Smuzhiyun  * Most fields in this structure for internal use by the transaction core code:
28*4882a593Smuzhiyun  * @links:	Links for channel transaction lists by state
29*4882a593Smuzhiyun  * @gsi:	GSI pointer
30*4882a593Smuzhiyun  * @channel_id: Channel number transaction is associated with
31*4882a593Smuzhiyun  * @cancelled:	If set by the core code, transaction was cancelled
32*4882a593Smuzhiyun  * @tre_count:	Number of TREs reserved for this transaction
33*4882a593Smuzhiyun  * @used:	Number of TREs *used* (could be less than tre_count)
34*4882a593Smuzhiyun  * @len:	Total # of transfer bytes represented in sgl[] (set by core)
35*4882a593Smuzhiyun  * @data:	Preserved but not touched by the core transaction code
36*4882a593Smuzhiyun  * @sgl:	An array of scatter/gather entries managed by core code
37*4882a593Smuzhiyun  * @info:	Array of command information structures (command channel)
38*4882a593Smuzhiyun  * @direction:	DMA transfer direction (DMA_NONE for commands)
39*4882a593Smuzhiyun  * @refcount:	Reference count used for destruction
40*4882a593Smuzhiyun  * @completion:	Completed when the transaction completes
41*4882a593Smuzhiyun  * @byte_count:	TX channel byte count recorded when transaction committed
42*4882a593Smuzhiyun  * @trans_count: Channel transaction count when committed (for BQL accounting)
43*4882a593Smuzhiyun  *
44*4882a593Smuzhiyun  * The size used for some fields in this structure were chosen to ensure
45*4882a593Smuzhiyun  * the full structure size is no larger than 128 bytes.
46*4882a593Smuzhiyun  */
47*4882a593Smuzhiyun struct gsi_trans {
48*4882a593Smuzhiyun 	struct list_head links;		/* gsi_channel lists */
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun 	struct gsi *gsi;
51*4882a593Smuzhiyun 	u8 channel_id;
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun 	bool cancelled;			/* true if transaction was cancelled */
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun 	u8 tre_count;			/* # TREs requested */
56*4882a593Smuzhiyun 	u8 used;			/* # entries used in sgl[] */
57*4882a593Smuzhiyun 	u32 len;			/* total # bytes across sgl[] */
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun 	void *data;
60*4882a593Smuzhiyun 	struct scatterlist *sgl;
61*4882a593Smuzhiyun 	struct ipa_cmd_info *info;	/* array of entries, or null */
62*4882a593Smuzhiyun 	enum dma_data_direction direction;
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun 	refcount_t refcount;
65*4882a593Smuzhiyun 	struct completion completion;
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun 	u64 byte_count;			/* channel byte_count when committed */
68*4882a593Smuzhiyun 	u64 trans_count;		/* channel trans_count when committed */
69*4882a593Smuzhiyun };
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun /**
72*4882a593Smuzhiyun  * gsi_trans_pool_init() - Initialize a pool of structures for transactions
73*4882a593Smuzhiyun  * @gsi:	GSI pointer
74*4882a593Smuzhiyun  * @size:	Size of elements in the pool
75*4882a593Smuzhiyun  * @count:	Minimum number of elements in the pool
76*4882a593Smuzhiyun  * @max_alloc:	Maximum number of elements allocated at a time from pool
77*4882a593Smuzhiyun  *
78*4882a593Smuzhiyun  * Return:	0 if successful, or a negative error code
79*4882a593Smuzhiyun  */
80*4882a593Smuzhiyun int gsi_trans_pool_init(struct gsi_trans_pool *pool, size_t size, u32 count,
81*4882a593Smuzhiyun 			u32 max_alloc);
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun /**
84*4882a593Smuzhiyun  * gsi_trans_pool_alloc() - Allocate one or more elements from a pool
85*4882a593Smuzhiyun  * @pool:	Pool pointer
86*4882a593Smuzhiyun  * @count:	Number of elements to allocate from the pool
87*4882a593Smuzhiyun  *
88*4882a593Smuzhiyun  * Return:	Virtual address of element(s) allocated from the pool
89*4882a593Smuzhiyun  */
90*4882a593Smuzhiyun void *gsi_trans_pool_alloc(struct gsi_trans_pool *pool, u32 count);
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun /**
93*4882a593Smuzhiyun  * gsi_trans_pool_exit() - Inverse of gsi_trans_pool_init()
94*4882a593Smuzhiyun  * @pool:	Pool pointer
95*4882a593Smuzhiyun  */
96*4882a593Smuzhiyun void gsi_trans_pool_exit(struct gsi_trans_pool *pool);
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun /**
99*4882a593Smuzhiyun  * gsi_trans_pool_init_dma() - Initialize a pool of DMA-able structures
100*4882a593Smuzhiyun  * @dev:	Device used for DMA
101*4882a593Smuzhiyun  * @pool:	Pool pointer
102*4882a593Smuzhiyun  * @size:	Size of elements in the pool
103*4882a593Smuzhiyun  * @count:	Minimum number of elements in the pool
104*4882a593Smuzhiyun  * @max_alloc:	Maximum number of elements allocated at a time from pool
105*4882a593Smuzhiyun  *
106*4882a593Smuzhiyun  * Return:	0 if successful, or a negative error code
107*4882a593Smuzhiyun  *
108*4882a593Smuzhiyun  * Structures in this pool reside in DMA-coherent memory.
109*4882a593Smuzhiyun  */
110*4882a593Smuzhiyun int gsi_trans_pool_init_dma(struct device *dev, struct gsi_trans_pool *pool,
111*4882a593Smuzhiyun 			    size_t size, u32 count, u32 max_alloc);
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun /**
114*4882a593Smuzhiyun  * gsi_trans_pool_alloc_dma() - Allocate an element from a DMA pool
115*4882a593Smuzhiyun  * @pool:	DMA pool pointer
116*4882a593Smuzhiyun  * @addr:	DMA address "handle" associated with the allocation
117*4882a593Smuzhiyun  *
118*4882a593Smuzhiyun  * Return:	Virtual address of element allocated from the pool
119*4882a593Smuzhiyun  *
120*4882a593Smuzhiyun  * Only one element at a time may be allocated from a DMA pool.
121*4882a593Smuzhiyun  */
122*4882a593Smuzhiyun void *gsi_trans_pool_alloc_dma(struct gsi_trans_pool *pool, dma_addr_t *addr);
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun /**
125*4882a593Smuzhiyun  * gsi_trans_pool_exit() - Inverse of gsi_trans_pool_init()
126*4882a593Smuzhiyun  * @pool:	Pool pointer
127*4882a593Smuzhiyun  */
128*4882a593Smuzhiyun void gsi_trans_pool_exit_dma(struct device *dev, struct gsi_trans_pool *pool);
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun /**
131*4882a593Smuzhiyun  * gsi_channel_trans_alloc() - Allocate a GSI transaction on a channel
132*4882a593Smuzhiyun  * @gsi:	GSI pointer
133*4882a593Smuzhiyun  * @channel_id:	Channel the transaction is associated with
134*4882a593Smuzhiyun  * @tre_count:	Number of elements in the transaction
135*4882a593Smuzhiyun  * @direction:	DMA direction for entire SGL (or DMA_NONE)
136*4882a593Smuzhiyun  *
137*4882a593Smuzhiyun  * Return:	A GSI transaction structure, or a null pointer if all
138*4882a593Smuzhiyun  *		available transactions are in use
139*4882a593Smuzhiyun  */
140*4882a593Smuzhiyun struct gsi_trans *gsi_channel_trans_alloc(struct gsi *gsi, u32 channel_id,
141*4882a593Smuzhiyun 					  u32 tre_count,
142*4882a593Smuzhiyun 					  enum dma_data_direction direction);
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun /**
145*4882a593Smuzhiyun  * gsi_trans_free() - Free a previously-allocated GSI transaction
146*4882a593Smuzhiyun  * @trans:	Transaction to be freed
147*4882a593Smuzhiyun  */
148*4882a593Smuzhiyun void gsi_trans_free(struct gsi_trans *trans);
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun /**
151*4882a593Smuzhiyun  * gsi_trans_cmd_add() - Add an immediate command to a transaction
152*4882a593Smuzhiyun  * @trans:	Transaction
153*4882a593Smuzhiyun  * @buf:	Buffer pointer for command payload
154*4882a593Smuzhiyun  * @size:	Number of bytes in buffer
155*4882a593Smuzhiyun  * @addr:	DMA address for payload
156*4882a593Smuzhiyun  * @direction:	Direction of DMA transfer (or DMA_NONE if none required)
157*4882a593Smuzhiyun  * @opcode:	IPA immediate command opcode
158*4882a593Smuzhiyun  */
159*4882a593Smuzhiyun void gsi_trans_cmd_add(struct gsi_trans *trans, void *buf, u32 size,
160*4882a593Smuzhiyun 		       dma_addr_t addr, enum dma_data_direction direction,
161*4882a593Smuzhiyun 		       enum ipa_cmd_opcode opcode);
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun /**
164*4882a593Smuzhiyun  * gsi_trans_page_add() - Add a page transfer to a transaction
165*4882a593Smuzhiyun  * @trans:	Transaction
166*4882a593Smuzhiyun  * @page:	Page pointer
167*4882a593Smuzhiyun  * @size:	Number of bytes (starting at offset) to transfer
168*4882a593Smuzhiyun  * @offset:	Offset within page for start of transfer
169*4882a593Smuzhiyun  */
170*4882a593Smuzhiyun int gsi_trans_page_add(struct gsi_trans *trans, struct page *page, u32 size,
171*4882a593Smuzhiyun 		       u32 offset);
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun /**
174*4882a593Smuzhiyun  * gsi_trans_skb_add() - Add a socket transfer to a transaction
175*4882a593Smuzhiyun  * @trans:	Transaction
176*4882a593Smuzhiyun  * @skb:	Socket buffer for transfer (outbound)
177*4882a593Smuzhiyun  *
178*4882a593Smuzhiyun  * Return:	0, or -EMSGSIZE if socket data won't fit in transaction.
179*4882a593Smuzhiyun  */
180*4882a593Smuzhiyun int gsi_trans_skb_add(struct gsi_trans *trans, struct sk_buff *skb);
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun /**
183*4882a593Smuzhiyun  * gsi_trans_commit() - Commit a GSI transaction
184*4882a593Smuzhiyun  * @trans:	Transaction to commit
185*4882a593Smuzhiyun  * @ring_db:	Whether to tell the hardware about these queued transfers
186*4882a593Smuzhiyun  */
187*4882a593Smuzhiyun void gsi_trans_commit(struct gsi_trans *trans, bool ring_db);
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun /**
190*4882a593Smuzhiyun  * gsi_trans_commit_wait() - Commit a GSI transaction and wait for it
191*4882a593Smuzhiyun  *			     to complete
192*4882a593Smuzhiyun  * @trans:	Transaction to commit
193*4882a593Smuzhiyun  */
194*4882a593Smuzhiyun void gsi_trans_commit_wait(struct gsi_trans *trans);
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun /**
197*4882a593Smuzhiyun  * gsi_trans_commit_wait_timeout() - Commit a GSI transaction and wait for
198*4882a593Smuzhiyun  *				     it to complete, with timeout
199*4882a593Smuzhiyun  * @trans:	Transaction to commit
200*4882a593Smuzhiyun  * @timeout:	Timeout period (in milliseconds)
201*4882a593Smuzhiyun  */
202*4882a593Smuzhiyun int gsi_trans_commit_wait_timeout(struct gsi_trans *trans,
203*4882a593Smuzhiyun 				  unsigned long timeout);
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun /**
206*4882a593Smuzhiyun  * gsi_trans_read_byte() - Issue a single byte read TRE on a channel
207*4882a593Smuzhiyun  * @gsi:	GSI pointer
208*4882a593Smuzhiyun  * @channel_id:	Channel on which to read a byte
209*4882a593Smuzhiyun  * @addr:	DMA address into which to transfer the one byte
210*4882a593Smuzhiyun  *
211*4882a593Smuzhiyun  * This is not a transaction operation at all.  It's defined here because
212*4882a593Smuzhiyun  * it needs to be done in coordination with other transaction activity.
213*4882a593Smuzhiyun  */
214*4882a593Smuzhiyun int gsi_trans_read_byte(struct gsi *gsi, u32 channel_id, dma_addr_t addr);
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun /**
217*4882a593Smuzhiyun  * gsi_trans_read_byte_done() - Clean up after a single byte read TRE
218*4882a593Smuzhiyun  * @gsi:	GSI pointer
219*4882a593Smuzhiyun  * @channel_id:	Channel on which byte was read
220*4882a593Smuzhiyun  *
221*4882a593Smuzhiyun  * This function needs to be called to signal that the work related
222*4882a593Smuzhiyun  * to reading a byte initiated by gsi_trans_read_byte() is complete.
223*4882a593Smuzhiyun  */
224*4882a593Smuzhiyun void gsi_trans_read_byte_done(struct gsi *gsi, u32 channel_id);
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun #endif /* _GSI_TRANS_H_ */
227