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