a846245a71317f5a45d4d3276882dcd754254fd6
[deb_dpdk.git] / drivers / bus / fslmc / mc / dpbp.c
1 /*-
2  * This file is provided under a dual BSD/GPLv2 license. When using or
3  * redistributing this file, you may do so under either license.
4  *
5  *   BSD LICENSE
6  *
7  * Copyright 2013-2016 Freescale Semiconductor Inc.
8  * Copyright 2016-2017 NXP.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are met:
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the distribution.
17  * * Neither the name of the above-listed copyright holders nor the
18  * names of any contributors may be used to endorse or promote products
19  * derived from this software without specific prior written permission.
20  *
21  *   GPL LICENSE SUMMARY
22  *
23  * ALTERNATIVELY, this software may be distributed under the terms of the
24  * GNU General Public License ("GPL") as published by the Free Software
25  * Foundation, either version 2 of that License or (at your option) any
26  * later version.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
29  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
32  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGE.
39  */
40 #include <fsl_mc_sys.h>
41 #include <fsl_mc_cmd.h>
42 #include <fsl_dpbp.h>
43 #include <fsl_dpbp_cmd.h>
44
45 /**
46  * dpbp_open() - Open a control session for the specified object.
47  * @mc_io:      Pointer to MC portal's I/O object
48  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
49  * @dpbp_id:    DPBP unique ID
50  * @token:      Returned token; use in subsequent API calls
51  *
52  * This function can be used to open a control session for an
53  * already created object; an object may have been declared in
54  * the DPL or by calling the dpbp_create function.
55  * This function returns a unique authentication token,
56  * associated with the specific object ID and the specific MC
57  * portal; this token must be used in all subsequent commands for
58  * this specific object
59  *
60  * Return:      '0' on Success; Error code otherwise.
61  */
62 int dpbp_open(struct fsl_mc_io *mc_io,
63               uint32_t cmd_flags,
64               int dpbp_id,
65               uint16_t *token)
66 {
67         struct dpbp_cmd_open *cmd_params;
68         struct mc_command cmd = { 0 };
69         int err;
70
71         /* prepare command */
72         cmd.header = mc_encode_cmd_header(DPBP_CMDID_OPEN,
73                                           cmd_flags, 0);
74         cmd_params = (struct dpbp_cmd_open *)cmd.params;
75         cmd_params->dpbp_id = cpu_to_le32(dpbp_id);
76
77         /* send command to mc*/
78         err = mc_send_command(mc_io, &cmd);
79         if (err)
80                 return err;
81
82         /* retrieve response parameters */
83         *token = mc_cmd_hdr_read_token(&cmd);
84
85         return err;
86 }
87
88 /**
89  * dpbp_close() - Close the control session of the object
90  * @mc_io:      Pointer to MC portal's I/O object
91  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
92  * @token:      Token of DPBP object
93  *
94  * After this function is called, no further operations are
95  * allowed on the object without opening a new control session.
96  *
97  * Return:      '0' on Success; Error code otherwise.
98  */
99 int dpbp_close(struct fsl_mc_io *mc_io,
100                uint32_t cmd_flags,
101                uint16_t token)
102 {
103         struct mc_command cmd = { 0 };
104
105         /* prepare command */
106         cmd.header = mc_encode_cmd_header(DPBP_CMDID_CLOSE, cmd_flags,
107                                           token);
108
109         /* send command to mc*/
110         return mc_send_command(mc_io, &cmd);
111 }
112
113 /**
114  * dpbp_create() - Create the DPBP object.
115  * @mc_io:      Pointer to MC portal's I/O object
116  * @dprc_token: Parent container token; '0' for default container
117  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
118  * @cfg:        Configuration structure
119  * @obj_id:     Returned object id; use in subsequent API calls
120  *
121  * Create the DPBP object, allocate required resources and
122  * perform required initialization.
123  *
124  * This function accepts an authentication token of a parent
125  * container that this object should be assigned to and returns
126  * an object id. This object_id will be used in all subsequent calls to
127  * this specific object.
128  *
129  * Return:      '0' on Success; Error code otherwise.
130  */
131 int dpbp_create(struct fsl_mc_io *mc_io,
132                 uint16_t dprc_token,
133                 uint32_t cmd_flags,
134                 const struct dpbp_cfg *cfg,
135                 uint32_t *obj_id)
136 {
137         struct mc_command cmd = { 0 };
138         int err;
139
140         (void)(cfg); /* unused */
141
142         /* prepare command */
143         cmd.header = mc_encode_cmd_header(DPBP_CMDID_CREATE,
144                                           cmd_flags, dprc_token);
145
146         /* send command to mc*/
147         err = mc_send_command(mc_io, &cmd);
148         if (err)
149                 return err;
150
151         /* retrieve response parameters */
152         *obj_id = mc_cmd_read_object_id(&cmd);
153
154         return 0;
155 }
156
157 /**
158  * dpbp_destroy() - Destroy the DPBP object and release all its resources.
159  * @mc_io:      Pointer to MC portal's I/O object
160  * @dprc_token: Parent container token; '0' for default container
161  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
162  * @obj_id:     ID of DPBP object
163  *
164  * Return:      '0' on Success; error code otherwise.
165  */
166 int dpbp_destroy(struct fsl_mc_io *mc_io,
167                  uint16_t dprc_token,
168                  uint32_t cmd_flags,
169                  uint32_t obj_id)
170 {
171         struct dpbp_cmd_destroy *cmd_params;
172         struct mc_command cmd = { 0 };
173
174         /* prepare command */
175         cmd.header = mc_encode_cmd_header(DPBP_CMDID_DESTROY,
176                                           cmd_flags, dprc_token);
177
178         cmd_params = (struct dpbp_cmd_destroy *)cmd.params;
179         cmd_params->object_id = cpu_to_le32(obj_id);
180
181         /* send command to mc*/
182         return mc_send_command(mc_io, &cmd);
183 }
184
185 /**
186  * dpbp_enable() - Enable the DPBP.
187  * @mc_io:      Pointer to MC portal's I/O object
188  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
189  * @token:      Token of DPBP object
190  *
191  * Return:      '0' on Success; Error code otherwise.
192  */
193 int dpbp_enable(struct fsl_mc_io *mc_io,
194                 uint32_t cmd_flags,
195                 uint16_t token)
196 {
197         struct mc_command cmd = { 0 };
198
199         /* prepare command */
200         cmd.header = mc_encode_cmd_header(DPBP_CMDID_ENABLE, cmd_flags,
201                                           token);
202
203         /* send command to mc*/
204         return mc_send_command(mc_io, &cmd);
205 }
206
207 /**
208  * dpbp_disable() - Disable the DPBP.
209  * @mc_io:      Pointer to MC portal's I/O object
210  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
211  * @token:      Token of DPBP object
212  *
213  * Return:      '0' on Success; Error code otherwise.
214  */
215 int dpbp_disable(struct fsl_mc_io *mc_io,
216                  uint32_t cmd_flags,
217                  uint16_t token)
218 {
219         struct mc_command cmd = { 0 };
220
221         /* prepare command */
222         cmd.header = mc_encode_cmd_header(DPBP_CMDID_DISABLE,
223                                           cmd_flags, token);
224
225         /* send command to mc*/
226         return mc_send_command(mc_io, &cmd);
227 }
228
229 /**
230  * dpbp_is_enabled() - Check if the DPBP is enabled.
231  * @mc_io:      Pointer to MC portal's I/O object
232  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
233  * @token:      Token of DPBP object
234  * @en:         Returns '1' if object is enabled; '0' otherwise
235  *
236  * Return:      '0' on Success; Error code otherwise.
237  */
238 int dpbp_is_enabled(struct fsl_mc_io *mc_io,
239                     uint32_t cmd_flags,
240                     uint16_t token,
241                     int *en)
242 {
243         struct dpbp_rsp_is_enabled *rsp_params;
244         struct mc_command cmd = { 0 };
245         int err;
246
247         /* prepare command */
248         cmd.header = mc_encode_cmd_header(DPBP_CMDID_IS_ENABLED, cmd_flags,
249                                           token);
250
251         /* send command to mc*/
252         err = mc_send_command(mc_io, &cmd);
253         if (err)
254                 return err;
255
256         /* retrieve response parameters */
257         rsp_params = (struct dpbp_rsp_is_enabled *)cmd.params;
258         *en = rsp_params->enabled & DPBP_ENABLE;
259
260         return 0;
261 }
262
263 /**
264  * dpbp_reset() - Reset the DPBP, returns the object to initial state.
265  * @mc_io:      Pointer to MC portal's I/O object
266  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
267  * @token:      Token of DPBP object
268  *
269  * Return:      '0' on Success; Error code otherwise.
270  */
271 int dpbp_reset(struct fsl_mc_io *mc_io,
272                uint32_t cmd_flags,
273                uint16_t token)
274 {
275         struct mc_command cmd = { 0 };
276
277         /* prepare command */
278         cmd.header = mc_encode_cmd_header(DPBP_CMDID_RESET,
279                                           cmd_flags, token);
280
281         /* send command to mc*/
282         return mc_send_command(mc_io, &cmd);
283 }
284 int dpbp_get_attributes(struct fsl_mc_io *mc_io,
285                         uint32_t cmd_flags,
286                         uint16_t token,
287                         struct dpbp_attr *attr)
288 {
289         struct dpbp_rsp_get_attributes *rsp_params;
290         struct mc_command cmd = { 0 };
291         int err;
292
293         /* prepare command */
294         cmd.header = mc_encode_cmd_header(DPBP_CMDID_GET_ATTR,
295                                           cmd_flags, token);
296
297         /* send command to mc*/
298         err = mc_send_command(mc_io, &cmd);
299         if (err)
300                 return err;
301
302         /* retrieve response parameters */
303         rsp_params = (struct dpbp_rsp_get_attributes *)cmd.params;
304         attr->bpid = le16_to_cpu(rsp_params->bpid);
305         attr->id = le32_to_cpu(rsp_params->id);
306
307         return 0;
308 }
309
310 /**
311  * dpbp_get_api_version - Get Data Path Buffer Pool API version
312  * @mc_io:      Pointer to Mc portal's I/O object
313  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
314  * @major_ver:  Major version of Buffer Pool API
315  * @minor_ver:  Minor version of Buffer Pool API
316  *
317  * Return:      '0' on Success; Error code otherwise.
318  */
319 int dpbp_get_api_version(struct fsl_mc_io *mc_io,
320                          uint32_t cmd_flags,
321                          uint16_t *major_ver,
322                          uint16_t *minor_ver)
323 {
324         struct dpbp_rsp_get_api_version *rsp_params;
325         struct mc_command cmd = { 0 };
326         int err;
327
328         /* prepare command */
329         cmd.header = mc_encode_cmd_header(DPBP_CMDID_GET_API_VERSION,
330                                           cmd_flags, 0);
331
332         /* send command to mc */
333         err = mc_send_command(mc_io, &cmd);
334         if (err)
335                 return err;
336
337         /* retrieve response parameters */
338         rsp_params = (struct dpbp_rsp_get_api_version *)cmd.params;
339         *major_ver = le16_to_cpu(rsp_params->major);
340         *minor_ver = le16_to_cpu(rsp_params->minor);
341
342         return 0;
343 }
344
345 /**
346  * dpbp_get_num_free_bufs() - Get number of free buffers in the buffer pool
347  * @mc_io:  Pointer to MC portal's I/O object
348  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
349  * @token:      Token of DPBP object
350  * @num_free_bufs:      Number of free buffers
351  *
352  * Return:  '0' on Success; Error code otherwise.
353  */
354
355 int dpbp_get_num_free_bufs(struct fsl_mc_io *mc_io,
356                            uint32_t cmd_flags,
357                            uint16_t token,
358                            uint32_t *num_free_bufs)
359 {
360         struct dpbp_rsp_get_num_free_bufs *rsp_params;
361         struct mc_command cmd = { 0 };
362         int err;
363
364         /* prepare command */
365         cmd.header = mc_encode_cmd_header(DPBP_CMDID_GET_FREE_BUFFERS_NUM,
366                                           cmd_flags,
367                                           token);
368
369         /* send command to mc*/
370         err = mc_send_command(mc_io, &cmd);
371         if (err)
372                 return err;
373
374         /* retrieve response parameters */
375         rsp_params = (struct dpbp_rsp_get_num_free_bufs *)cmd.params;
376         *num_free_bufs =  le32_to_cpu(rsp_params->num_free_bufs);
377
378         return 0;
379 }