New upstream version 18.02
[deb_dpdk.git] / drivers / bus / fslmc / mc / dpbp.c
1 /* SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0)
2  *
3  * Copyright 2013-2016 Freescale Semiconductor Inc.
4  * Copyright 2016-2017 NXP
5  *
6  */
7 #include <fsl_mc_sys.h>
8 #include <fsl_mc_cmd.h>
9 #include <fsl_dpbp.h>
10 #include <fsl_dpbp_cmd.h>
11
12 /**
13  * dpbp_open() - Open a control session for the specified object.
14  * @mc_io:      Pointer to MC portal's I/O object
15  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
16  * @dpbp_id:    DPBP unique ID
17  * @token:      Returned token; use in subsequent API calls
18  *
19  * This function can be used to open a control session for an
20  * already created object; an object may have been declared in
21  * the DPL or by calling the dpbp_create function.
22  * This function returns a unique authentication token,
23  * associated with the specific object ID and the specific MC
24  * portal; this token must be used in all subsequent commands for
25  * this specific object
26  *
27  * Return:      '0' on Success; Error code otherwise.
28  */
29 int dpbp_open(struct fsl_mc_io *mc_io,
30               uint32_t cmd_flags,
31               int dpbp_id,
32               uint16_t *token)
33 {
34         struct dpbp_cmd_open *cmd_params;
35         struct mc_command cmd = { 0 };
36         int err;
37
38         /* prepare command */
39         cmd.header = mc_encode_cmd_header(DPBP_CMDID_OPEN,
40                                           cmd_flags, 0);
41         cmd_params = (struct dpbp_cmd_open *)cmd.params;
42         cmd_params->dpbp_id = cpu_to_le32(dpbp_id);
43
44         /* send command to mc*/
45         err = mc_send_command(mc_io, &cmd);
46         if (err)
47                 return err;
48
49         /* retrieve response parameters */
50         *token = mc_cmd_hdr_read_token(&cmd);
51
52         return err;
53 }
54
55 /**
56  * dpbp_close() - Close the control session of the object
57  * @mc_io:      Pointer to MC portal's I/O object
58  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
59  * @token:      Token of DPBP object
60  *
61  * After this function is called, no further operations are
62  * allowed on the object without opening a new control session.
63  *
64  * Return:      '0' on Success; Error code otherwise.
65  */
66 int dpbp_close(struct fsl_mc_io *mc_io,
67                uint32_t cmd_flags,
68                uint16_t token)
69 {
70         struct mc_command cmd = { 0 };
71
72         /* prepare command */
73         cmd.header = mc_encode_cmd_header(DPBP_CMDID_CLOSE, cmd_flags,
74                                           token);
75
76         /* send command to mc*/
77         return mc_send_command(mc_io, &cmd);
78 }
79
80 /**
81  * dpbp_create() - Create the DPBP object.
82  * @mc_io:      Pointer to MC portal's I/O object
83  * @dprc_token: Parent container token; '0' for default container
84  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
85  * @cfg:        Configuration structure
86  * @obj_id:     Returned object id; use in subsequent API calls
87  *
88  * Create the DPBP object, allocate required resources and
89  * perform required initialization.
90  *
91  * This function accepts an authentication token of a parent
92  * container that this object should be assigned to and returns
93  * an object id. This object_id will be used in all subsequent calls to
94  * this specific object.
95  *
96  * Return:      '0' on Success; Error code otherwise.
97  */
98 int dpbp_create(struct fsl_mc_io *mc_io,
99                 uint16_t dprc_token,
100                 uint32_t cmd_flags,
101                 const struct dpbp_cfg *cfg,
102                 uint32_t *obj_id)
103 {
104         struct mc_command cmd = { 0 };
105         int err;
106
107         (void)(cfg); /* unused */
108
109         /* prepare command */
110         cmd.header = mc_encode_cmd_header(DPBP_CMDID_CREATE,
111                                           cmd_flags, dprc_token);
112
113         /* send command to mc*/
114         err = mc_send_command(mc_io, &cmd);
115         if (err)
116                 return err;
117
118         /* retrieve response parameters */
119         *obj_id = mc_cmd_read_object_id(&cmd);
120
121         return 0;
122 }
123
124 /**
125  * dpbp_destroy() - Destroy the DPBP object and release all its resources.
126  * @mc_io:      Pointer to MC portal's I/O object
127  * @dprc_token: Parent container token; '0' for default container
128  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
129  * @obj_id:     ID of DPBP object
130  *
131  * Return:      '0' on Success; error code otherwise.
132  */
133 int dpbp_destroy(struct fsl_mc_io *mc_io,
134                  uint16_t dprc_token,
135                  uint32_t cmd_flags,
136                  uint32_t obj_id)
137 {
138         struct dpbp_cmd_destroy *cmd_params;
139         struct mc_command cmd = { 0 };
140
141         /* prepare command */
142         cmd.header = mc_encode_cmd_header(DPBP_CMDID_DESTROY,
143                                           cmd_flags, dprc_token);
144
145         cmd_params = (struct dpbp_cmd_destroy *)cmd.params;
146         cmd_params->object_id = cpu_to_le32(obj_id);
147
148         /* send command to mc*/
149         return mc_send_command(mc_io, &cmd);
150 }
151
152 /**
153  * dpbp_enable() - Enable the DPBP.
154  * @mc_io:      Pointer to MC portal's I/O object
155  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
156  * @token:      Token of DPBP object
157  *
158  * Return:      '0' on Success; Error code otherwise.
159  */
160 int dpbp_enable(struct fsl_mc_io *mc_io,
161                 uint32_t cmd_flags,
162                 uint16_t token)
163 {
164         struct mc_command cmd = { 0 };
165
166         /* prepare command */
167         cmd.header = mc_encode_cmd_header(DPBP_CMDID_ENABLE, cmd_flags,
168                                           token);
169
170         /* send command to mc*/
171         return mc_send_command(mc_io, &cmd);
172 }
173
174 /**
175  * dpbp_disable() - Disable the DPBP.
176  * @mc_io:      Pointer to MC portal's I/O object
177  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
178  * @token:      Token of DPBP object
179  *
180  * Return:      '0' on Success; Error code otherwise.
181  */
182 int dpbp_disable(struct fsl_mc_io *mc_io,
183                  uint32_t cmd_flags,
184                  uint16_t token)
185 {
186         struct mc_command cmd = { 0 };
187
188         /* prepare command */
189         cmd.header = mc_encode_cmd_header(DPBP_CMDID_DISABLE,
190                                           cmd_flags, token);
191
192         /* send command to mc*/
193         return mc_send_command(mc_io, &cmd);
194 }
195
196 /**
197  * dpbp_is_enabled() - Check if the DPBP is enabled.
198  * @mc_io:      Pointer to MC portal's I/O object
199  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
200  * @token:      Token of DPBP object
201  * @en:         Returns '1' if object is enabled; '0' otherwise
202  *
203  * Return:      '0' on Success; Error code otherwise.
204  */
205 int dpbp_is_enabled(struct fsl_mc_io *mc_io,
206                     uint32_t cmd_flags,
207                     uint16_t token,
208                     int *en)
209 {
210         struct dpbp_rsp_is_enabled *rsp_params;
211         struct mc_command cmd = { 0 };
212         int err;
213
214         /* prepare command */
215         cmd.header = mc_encode_cmd_header(DPBP_CMDID_IS_ENABLED, cmd_flags,
216                                           token);
217
218         /* send command to mc*/
219         err = mc_send_command(mc_io, &cmd);
220         if (err)
221                 return err;
222
223         /* retrieve response parameters */
224         rsp_params = (struct dpbp_rsp_is_enabled *)cmd.params;
225         *en = rsp_params->enabled & DPBP_ENABLE;
226
227         return 0;
228 }
229
230 /**
231  * dpbp_reset() - Reset the DPBP, returns the object to initial state.
232  * @mc_io:      Pointer to MC portal's I/O object
233  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
234  * @token:      Token of DPBP object
235  *
236  * Return:      '0' on Success; Error code otherwise.
237  */
238 int dpbp_reset(struct fsl_mc_io *mc_io,
239                uint32_t cmd_flags,
240                uint16_t token)
241 {
242         struct mc_command cmd = { 0 };
243
244         /* prepare command */
245         cmd.header = mc_encode_cmd_header(DPBP_CMDID_RESET,
246                                           cmd_flags, token);
247
248         /* send command to mc*/
249         return mc_send_command(mc_io, &cmd);
250 }
251 int dpbp_get_attributes(struct fsl_mc_io *mc_io,
252                         uint32_t cmd_flags,
253                         uint16_t token,
254                         struct dpbp_attr *attr)
255 {
256         struct dpbp_rsp_get_attributes *rsp_params;
257         struct mc_command cmd = { 0 };
258         int err;
259
260         /* prepare command */
261         cmd.header = mc_encode_cmd_header(DPBP_CMDID_GET_ATTR,
262                                           cmd_flags, token);
263
264         /* send command to mc*/
265         err = mc_send_command(mc_io, &cmd);
266         if (err)
267                 return err;
268
269         /* retrieve response parameters */
270         rsp_params = (struct dpbp_rsp_get_attributes *)cmd.params;
271         attr->bpid = le16_to_cpu(rsp_params->bpid);
272         attr->id = le32_to_cpu(rsp_params->id);
273
274         return 0;
275 }
276
277 /**
278  * dpbp_get_api_version - Get Data Path Buffer Pool API version
279  * @mc_io:      Pointer to Mc portal's I/O object
280  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
281  * @major_ver:  Major version of Buffer Pool API
282  * @minor_ver:  Minor version of Buffer Pool API
283  *
284  * Return:      '0' on Success; Error code otherwise.
285  */
286 int dpbp_get_api_version(struct fsl_mc_io *mc_io,
287                          uint32_t cmd_flags,
288                          uint16_t *major_ver,
289                          uint16_t *minor_ver)
290 {
291         struct dpbp_rsp_get_api_version *rsp_params;
292         struct mc_command cmd = { 0 };
293         int err;
294
295         /* prepare command */
296         cmd.header = mc_encode_cmd_header(DPBP_CMDID_GET_API_VERSION,
297                                           cmd_flags, 0);
298
299         /* send command to mc */
300         err = mc_send_command(mc_io, &cmd);
301         if (err)
302                 return err;
303
304         /* retrieve response parameters */
305         rsp_params = (struct dpbp_rsp_get_api_version *)cmd.params;
306         *major_ver = le16_to_cpu(rsp_params->major);
307         *minor_ver = le16_to_cpu(rsp_params->minor);
308
309         return 0;
310 }
311
312 /**
313  * dpbp_get_num_free_bufs() - Get number of free buffers in the buffer pool
314  * @mc_io:  Pointer to MC portal's I/O object
315  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
316  * @token:      Token of DPBP object
317  * @num_free_bufs:      Number of free buffers
318  *
319  * Return:  '0' on Success; Error code otherwise.
320  */
321
322 int dpbp_get_num_free_bufs(struct fsl_mc_io *mc_io,
323                            uint32_t cmd_flags,
324                            uint16_t token,
325                            uint32_t *num_free_bufs)
326 {
327         struct dpbp_rsp_get_num_free_bufs *rsp_params;
328         struct mc_command cmd = { 0 };
329         int err;
330
331         /* prepare command */
332         cmd.header = mc_encode_cmd_header(DPBP_CMDID_GET_FREE_BUFFERS_NUM,
333                                           cmd_flags,
334                                           token);
335
336         /* send command to mc*/
337         err = mc_send_command(mc_io, &cmd);
338         if (err)
339                 return err;
340
341         /* retrieve response parameters */
342         rsp_params = (struct dpbp_rsp_get_num_free_bufs *)cmd.params;
343         *num_free_bufs =  le32_to_cpu(rsp_params->num_free_bufs);
344
345         return 0;
346 }