1 /*
2 * Copyright 2014-2016 Freescale Semiconductor Inc.
3 * Copyright 2016 NXP
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of Freescale Semiconductor nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * ALTERNATIVELY, this software may be distributed under the terms of the
17 * GNU General Public License ("GPL") as published by the Free Software
18 * Foundation, either version 2 of that License or (at your option) any
19 * later version.
20 *
21 * THIS SOFTWARE IS PROVIDED BY Freescale Semiconductor ``AS IS'' AND ANY
22 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL Freescale Semiconductor BE LIABLE FOR ANY
25 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32 #ifndef __FSL_DPAA2_FD_H
33 #define __FSL_DPAA2_FD_H
34
35 #include <linux/kernel.h>
36
37 /**
38 * DOC: DPAA2 FD - Frame Descriptor APIs for DPAA2
39 *
40 * Frame Descriptors (FDs) are used to describe frame data in the DPAA2.
41 * Frames can be enqueued and dequeued to Frame Queues (FQs) which are consumed
42 * by the various DPAA accelerators (WRIOP, SEC, PME, DCE)
43 *
44 * There are three types of frames: single, scatter gather, and frame lists.
45 *
46 * The set of APIs in this file must be used to create, manipulate and
47 * query Frame Descriptors.
48 */
49
50 /**
51 * struct dpaa2_fd - Struct describing FDs
52 * @words: for easier/faster copying the whole FD structure
53 * @addr: address in the FD
54 * @len: length in the FD
55 * @bpid: buffer pool ID
56 * @format_offset: format, offset, and short-length fields
57 * @frc: frame context
58 * @ctrl: control bits...including dd, sc, va, err, etc
59 * @flc: flow context address
60 *
61 * This structure represents the basic Frame Descriptor used in the system.
62 */
63 struct dpaa2_fd {
64 union {
65 u32 words[8];
66 struct dpaa2_fd_simple {
67 __le64 addr;
68 __le32 len;
69 __le16 bpid;
70 __le16 format_offset;
71 __le32 frc;
72 __le32 ctrl;
73 __le64 flc;
74 } simple;
75 };
76 };
77
78 #define FD_SHORT_LEN_FLAG_MASK 0x1
79 #define FD_SHORT_LEN_FLAG_SHIFT 14
80 #define FD_SHORT_LEN_MASK 0x3FFFF
81 #define FD_OFFSET_MASK 0x0FFF
82 #define FD_FORMAT_MASK 0x3
83 #define FD_FORMAT_SHIFT 12
84 #define FD_BPID_MASK 0x3FFF
85 #define SG_SHORT_LEN_FLAG_MASK 0x1
86 #define SG_SHORT_LEN_FLAG_SHIFT 14
87 #define SG_SHORT_LEN_MASK 0x1FFFF
88 #define SG_OFFSET_MASK 0x0FFF
89 #define SG_FORMAT_MASK 0x3
90 #define SG_FORMAT_SHIFT 12
91 #define SG_BPID_MASK 0x3FFF
92 #define SG_FINAL_FLAG_MASK 0x1
93 #define SG_FINAL_FLAG_SHIFT 15
94
95 enum dpaa2_fd_format {
96 dpaa2_fd_single = 0,
97 dpaa2_fd_list,
98 dpaa2_fd_sg
99 };
100
101 /**
102 * dpaa2_fd_get_addr() - get the addr field of frame descriptor
103 * @fd: the given frame descriptor
104 *
105 * Return the address in the frame descriptor.
106 */
dpaa2_fd_get_addr(const struct dpaa2_fd * fd)107 static inline dma_addr_t dpaa2_fd_get_addr(const struct dpaa2_fd *fd)
108 {
109 return (dma_addr_t)le64_to_cpu(fd->simple.addr);
110 }
111
112 /**
113 * dpaa2_fd_set_addr() - Set the addr field of frame descriptor
114 * @fd: the given frame descriptor
115 * @addr: the address needs to be set in frame descriptor
116 */
dpaa2_fd_set_addr(struct dpaa2_fd * fd,dma_addr_t addr)117 static inline void dpaa2_fd_set_addr(struct dpaa2_fd *fd, dma_addr_t addr)
118 {
119 fd->simple.addr = cpu_to_le64(addr);
120 }
121
122 /**
123 * dpaa2_fd_get_frc() - Get the frame context in the frame descriptor
124 * @fd: the given frame descriptor
125 *
126 * Return the frame context field in the frame descriptor.
127 */
dpaa2_fd_get_frc(const struct dpaa2_fd * fd)128 static inline u32 dpaa2_fd_get_frc(const struct dpaa2_fd *fd)
129 {
130 return le32_to_cpu(fd->simple.frc);
131 }
132
133 /**
134 * dpaa2_fd_set_frc() - Set the frame context in the frame descriptor
135 * @fd: the given frame descriptor
136 * @frc: the frame context needs to be set in frame descriptor
137 */
dpaa2_fd_set_frc(struct dpaa2_fd * fd,u32 frc)138 static inline void dpaa2_fd_set_frc(struct dpaa2_fd *fd, u32 frc)
139 {
140 fd->simple.frc = cpu_to_le32(frc);
141 }
142
143 /**
144 * dpaa2_fd_get_ctrl() - Get the control bits in the frame descriptor
145 * @fd: the given frame descriptor
146 *
147 * Return the control bits field in the frame descriptor.
148 */
dpaa2_fd_get_ctrl(const struct dpaa2_fd * fd)149 static inline u32 dpaa2_fd_get_ctrl(const struct dpaa2_fd *fd)
150 {
151 return le32_to_cpu(fd->simple.ctrl);
152 }
153
154 /**
155 * dpaa2_fd_set_ctrl() - Set the control bits in the frame descriptor
156 * @fd: the given frame descriptor
157 * @ctrl: the control bits to be set in the frame descriptor
158 */
dpaa2_fd_set_ctrl(struct dpaa2_fd * fd,u32 ctrl)159 static inline void dpaa2_fd_set_ctrl(struct dpaa2_fd *fd, u32 ctrl)
160 {
161 fd->simple.ctrl = cpu_to_le32(ctrl);
162 }
163
164 /**
165 * dpaa2_fd_get_flc() - Get the flow context in the frame descriptor
166 * @fd: the given frame descriptor
167 *
168 * Return the flow context in the frame descriptor.
169 */
dpaa2_fd_get_flc(const struct dpaa2_fd * fd)170 static inline dma_addr_t dpaa2_fd_get_flc(const struct dpaa2_fd *fd)
171 {
172 return (dma_addr_t)le64_to_cpu(fd->simple.flc);
173 }
174
175 /**
176 * dpaa2_fd_set_flc() - Set the flow context field of frame descriptor
177 * @fd: the given frame descriptor
178 * @flc_addr: the flow context needs to be set in frame descriptor
179 */
dpaa2_fd_set_flc(struct dpaa2_fd * fd,dma_addr_t flc_addr)180 static inline void dpaa2_fd_set_flc(struct dpaa2_fd *fd, dma_addr_t flc_addr)
181 {
182 fd->simple.flc = cpu_to_le64(flc_addr);
183 }
184
dpaa2_fd_short_len(const struct dpaa2_fd * fd)185 static inline bool dpaa2_fd_short_len(const struct dpaa2_fd *fd)
186 {
187 return !!((le16_to_cpu(fd->simple.format_offset) >>
188 FD_SHORT_LEN_FLAG_SHIFT) & FD_SHORT_LEN_FLAG_MASK);
189 }
190
191 /**
192 * dpaa2_fd_get_len() - Get the length in the frame descriptor
193 * @fd: the given frame descriptor
194 *
195 * Return the length field in the frame descriptor.
196 */
dpaa2_fd_get_len(const struct dpaa2_fd * fd)197 static inline u32 dpaa2_fd_get_len(const struct dpaa2_fd *fd)
198 {
199 if (dpaa2_fd_short_len(fd))
200 return le32_to_cpu(fd->simple.len) & FD_SHORT_LEN_MASK;
201
202 return le32_to_cpu(fd->simple.len);
203 }
204
205 /**
206 * dpaa2_fd_set_len() - Set the length field of frame descriptor
207 * @fd: the given frame descriptor
208 * @len: the length needs to be set in frame descriptor
209 */
dpaa2_fd_set_len(struct dpaa2_fd * fd,u32 len)210 static inline void dpaa2_fd_set_len(struct dpaa2_fd *fd, u32 len)
211 {
212 fd->simple.len = cpu_to_le32(len);
213 }
214
215 /**
216 * dpaa2_fd_get_offset() - Get the offset field in the frame descriptor
217 * @fd: the given frame descriptor
218 *
219 * Return the offset.
220 */
dpaa2_fd_get_offset(const struct dpaa2_fd * fd)221 static inline uint16_t dpaa2_fd_get_offset(const struct dpaa2_fd *fd)
222 {
223 return le16_to_cpu(fd->simple.format_offset) & FD_OFFSET_MASK;
224 }
225
226 /**
227 * dpaa2_fd_set_offset() - Set the offset field of frame descriptor
228 * @fd: the given frame descriptor
229 * @offset: the offset needs to be set in frame descriptor
230 */
dpaa2_fd_set_offset(struct dpaa2_fd * fd,uint16_t offset)231 static inline void dpaa2_fd_set_offset(struct dpaa2_fd *fd, uint16_t offset)
232 {
233 fd->simple.format_offset &= cpu_to_le16(~FD_OFFSET_MASK);
234 fd->simple.format_offset |= cpu_to_le16(offset);
235 }
236
237 /**
238 * dpaa2_fd_get_format() - Get the format field in the frame descriptor
239 * @fd: the given frame descriptor
240 *
241 * Return the format.
242 */
dpaa2_fd_get_format(const struct dpaa2_fd * fd)243 static inline enum dpaa2_fd_format dpaa2_fd_get_format(
244 const struct dpaa2_fd *fd)
245 {
246 return (enum dpaa2_fd_format)((le16_to_cpu(fd->simple.format_offset)
247 >> FD_FORMAT_SHIFT) & FD_FORMAT_MASK);
248 }
249
250 /**
251 * dpaa2_fd_set_format() - Set the format field of frame descriptor
252 * @fd: the given frame descriptor
253 * @format: the format needs to be set in frame descriptor
254 */
dpaa2_fd_set_format(struct dpaa2_fd * fd,enum dpaa2_fd_format format)255 static inline void dpaa2_fd_set_format(struct dpaa2_fd *fd,
256 enum dpaa2_fd_format format)
257 {
258 fd->simple.format_offset &=
259 cpu_to_le16(~(FD_FORMAT_MASK << FD_FORMAT_SHIFT));
260 fd->simple.format_offset |= cpu_to_le16(format << FD_FORMAT_SHIFT);
261 }
262
263 /**
264 * dpaa2_fd_get_bpid() - Get the bpid field in the frame descriptor
265 * @fd: the given frame descriptor
266 *
267 * Return the buffer pool id.
268 */
dpaa2_fd_get_bpid(const struct dpaa2_fd * fd)269 static inline uint16_t dpaa2_fd_get_bpid(const struct dpaa2_fd *fd)
270 {
271 return le16_to_cpu(fd->simple.bpid) & FD_BPID_MASK;
272 }
273
274 /**
275 * dpaa2_fd_set_bpid() - Set the bpid field of frame descriptor
276 * @fd: the given frame descriptor
277 * @bpid: buffer pool id to be set
278 */
dpaa2_fd_set_bpid(struct dpaa2_fd * fd,uint16_t bpid)279 static inline void dpaa2_fd_set_bpid(struct dpaa2_fd *fd, uint16_t bpid)
280 {
281 fd->simple.bpid &= cpu_to_le16(~(FD_BPID_MASK));
282 fd->simple.bpid |= cpu_to_le16(bpid);
283 }
284
285 /**
286 * struct dpaa2_sg_entry - the scatter-gathering structure
287 * @addr: address of the sg entry
288 * @len: length in this sg entry
289 * @bpid: buffer pool id
290 * @format_offset: format and offset fields
291 */
292 struct dpaa2_sg_entry {
293 __le64 addr;
294 __le32 len;
295 __le16 bpid;
296 __le16 format_offset;
297 };
298
299 enum dpaa2_sg_format {
300 dpaa2_sg_single = 0,
301 dpaa2_sg_frame_data,
302 dpaa2_sg_sgt_ext
303 };
304
305 /* Accessors for SG entry fields */
306
307 /**
308 * dpaa2_sg_get_addr() - Get the address from SG entry
309 * @sg: the given scatter-gathering object
310 *
311 * Return the address.
312 */
dpaa2_sg_get_addr(const struct dpaa2_sg_entry * sg)313 static inline dma_addr_t dpaa2_sg_get_addr(const struct dpaa2_sg_entry *sg)
314 {
315 return le64_to_cpu((dma_addr_t)sg->addr);
316 }
317
318 /**
319 * dpaa2_sg_set_addr() - Set the address in SG entry
320 * @sg: the given scatter-gathering object
321 * @addr: the address to be set
322 */
dpaa2_sg_set_addr(struct dpaa2_sg_entry * sg,dma_addr_t addr)323 static inline void dpaa2_sg_set_addr(struct dpaa2_sg_entry *sg, dma_addr_t addr)
324 {
325 sg->addr = cpu_to_le64(addr);
326 }
327
dpaa2_sg_short_len(const struct dpaa2_sg_entry * sg)328 static inline bool dpaa2_sg_short_len(const struct dpaa2_sg_entry *sg)
329 {
330 return !!((le16_to_cpu(sg->format_offset) >> SG_SHORT_LEN_FLAG_SHIFT)
331 & SG_SHORT_LEN_FLAG_MASK);
332 }
333
334 /**
335 * dpaa2_sg_get_len() - Get the length in SG entry
336 * @sg: the given scatter-gathering object
337 *
338 * Return the length.
339 */
dpaa2_sg_get_len(const struct dpaa2_sg_entry * sg)340 static inline u32 dpaa2_sg_get_len(const struct dpaa2_sg_entry *sg)
341 {
342 if (dpaa2_sg_short_len(sg))
343 return le32_to_cpu(sg->len) & SG_SHORT_LEN_MASK;
344
345 return le32_to_cpu(sg->len);
346 }
347
348 /**
349 * dpaa2_sg_set_len() - Set the length in SG entry
350 * @sg: the given scatter-gathering object
351 * @len: the length to be set
352 */
dpaa2_sg_set_len(struct dpaa2_sg_entry * sg,u32 len)353 static inline void dpaa2_sg_set_len(struct dpaa2_sg_entry *sg, u32 len)
354 {
355 sg->len = cpu_to_le32(len);
356 }
357
358 /**
359 * dpaa2_sg_get_offset() - Get the offset in SG entry
360 * @sg: the given scatter-gathering object
361 *
362 * Return the offset.
363 */
dpaa2_sg_get_offset(const struct dpaa2_sg_entry * sg)364 static inline u16 dpaa2_sg_get_offset(const struct dpaa2_sg_entry *sg)
365 {
366 return le16_to_cpu(sg->format_offset) & SG_OFFSET_MASK;
367 }
368
369 /**
370 * dpaa2_sg_set_offset() - Set the offset in SG entry
371 * @sg: the given scatter-gathering object
372 * @offset: the offset to be set
373 */
dpaa2_sg_set_offset(struct dpaa2_sg_entry * sg,u16 offset)374 static inline void dpaa2_sg_set_offset(struct dpaa2_sg_entry *sg,
375 u16 offset)
376 {
377 sg->format_offset &= cpu_to_le16(~SG_OFFSET_MASK);
378 sg->format_offset |= cpu_to_le16(offset);
379 }
380
381 /**
382 * dpaa2_sg_get_format() - Get the SG format in SG entry
383 * @sg: the given scatter-gathering object
384 *
385 * Return the format.
386 */
387 static inline enum dpaa2_sg_format
dpaa2_sg_get_format(const struct dpaa2_sg_entry * sg)388 dpaa2_sg_get_format(const struct dpaa2_sg_entry *sg)
389 {
390 return (enum dpaa2_sg_format)((le16_to_cpu(sg->format_offset)
391 >> SG_FORMAT_SHIFT) & SG_FORMAT_MASK);
392 }
393
394 /**
395 * dpaa2_sg_set_format() - Set the SG format in SG entry
396 * @sg: the given scatter-gathering object
397 * @format: the format to be set
398 */
dpaa2_sg_set_format(struct dpaa2_sg_entry * sg,enum dpaa2_sg_format format)399 static inline void dpaa2_sg_set_format(struct dpaa2_sg_entry *sg,
400 enum dpaa2_sg_format format)
401 {
402 sg->format_offset &= cpu_to_le16(~(SG_FORMAT_MASK << SG_FORMAT_SHIFT));
403 sg->format_offset |= cpu_to_le16(format << SG_FORMAT_SHIFT);
404 }
405
406 /**
407 * dpaa2_sg_get_bpid() - Get the buffer pool id in SG entry
408 * @sg: the given scatter-gathering object
409 *
410 * Return the bpid.
411 */
dpaa2_sg_get_bpid(const struct dpaa2_sg_entry * sg)412 static inline u16 dpaa2_sg_get_bpid(const struct dpaa2_sg_entry *sg)
413 {
414 return le16_to_cpu(sg->bpid) & SG_BPID_MASK;
415 }
416
417 /**
418 * dpaa2_sg_set_bpid() - Set the buffer pool id in SG entry
419 * @sg: the given scatter-gathering object
420 * @bpid: the bpid to be set
421 */
dpaa2_sg_set_bpid(struct dpaa2_sg_entry * sg,u16 bpid)422 static inline void dpaa2_sg_set_bpid(struct dpaa2_sg_entry *sg, u16 bpid)
423 {
424 sg->bpid &= cpu_to_le16(~(SG_BPID_MASK));
425 sg->bpid |= cpu_to_le16(bpid);
426 }
427
428 /**
429 * dpaa2_sg_is_final() - Check final bit in SG entry
430 * @sg: the given scatter-gathering object
431 *
432 * Return bool.
433 */
dpaa2_sg_is_final(const struct dpaa2_sg_entry * sg)434 static inline bool dpaa2_sg_is_final(const struct dpaa2_sg_entry *sg)
435 {
436 return !!(le16_to_cpu(sg->format_offset) >> SG_FINAL_FLAG_SHIFT);
437 }
438
439 /**
440 * dpaa2_sg_set_final() - Set the final bit in SG entry
441 * @sg: the given scatter-gathering object
442 * @final: the final boolean to be set
443 */
dpaa2_sg_set_final(struct dpaa2_sg_entry * sg,bool final)444 static inline void dpaa2_sg_set_final(struct dpaa2_sg_entry *sg, bool final)
445 {
446 sg->format_offset &= cpu_to_le16(~(SG_FINAL_FLAG_MASK
447 << SG_FINAL_FLAG_SHIFT));
448 sg->format_offset |= cpu_to_le16(final << SG_FINAL_FLAG_SHIFT);
449 }
450
451 #endif /* __FSL_DPAA2_FD_H */
452