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