New upstream version 18.11-rc1
[deb_dpdk.git] / drivers / bus / fslmc / mc / dpcon.c
1 /* SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0)
2  *
3  * Copyright 2013-2016 Freescale Semiconductor Inc.
4  *
5  */
6 #include <fsl_mc_sys.h>
7 #include <fsl_mc_cmd.h>
8 #include <fsl_dpcon.h>
9 #include <fsl_dpcon_cmd.h>
10
11 /**
12  * dpcon_open() - Open a control session for the specified object
13  * @mc_io:      Pointer to MC portal's I/O object
14  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
15  * @dpcon_id:   DPCON unique ID
16  * @token:      Returned token; use in subsequent API calls
17  *
18  * This function can be used to open a control session for an
19  * already created object; an object may have been declared in
20  * the DPL or by calling the dpcon_create() function.
21  * This function returns a unique authentication token,
22  * associated with the specific object ID and the specific MC
23  * portal; this token must be used in all subsequent commands for
24  * this specific object.
25  *
26  * Return:      '0' on Success; Error code otherwise.
27  */
28 int dpcon_open(struct fsl_mc_io *mc_io,
29                uint32_t cmd_flags,
30                int dpcon_id,
31                uint16_t *token)
32 {
33         struct mc_command cmd = { 0 };
34         struct dpcon_cmd_open *dpcon_cmd;
35         int err;
36
37         /* prepare command */
38         cmd.header = mc_encode_cmd_header(DPCON_CMDID_OPEN,
39                                           cmd_flags,
40                                           0);
41         dpcon_cmd = (struct dpcon_cmd_open *)cmd.params;
42         dpcon_cmd->dpcon_id = cpu_to_le32(dpcon_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 0;
53 }
54
55 /**
56  * dpcon_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 DPCON 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 dpcon_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(DPCON_CMDID_CLOSE,
74                                           cmd_flags,
75                                           token);
76
77         /* send command to mc*/
78         return mc_send_command(mc_io, &cmd);
79 }
80
81 /**
82  * dpcon_create() - Create the DPCON object.
83  * @mc_io:      Pointer to MC portal's I/O object
84  * @dprc_token: Parent container token; '0' for default container
85  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
86  * @cfg:        Configuration structure
87  * @obj_id:     Returned object id; use in subsequent API calls
88  *
89  * Create the DPCON object, allocate required resources and
90  * perform required initialization.
91  *
92  * The object can be created either by declaring it in the
93  * DPL file, or by calling this function.
94  *
95  * This function accepts an authentication token of a parent
96  * container that this object should be assigned to and returns
97  * an object id. This object_id will be used in all subsequent calls to
98  * this specific object.
99  *
100  * Return:      '0' on Success; Error code otherwise.
101  */
102 int dpcon_create(struct fsl_mc_io *mc_io,
103                  uint16_t dprc_token,
104                  uint32_t cmd_flags,
105                  const struct dpcon_cfg *cfg,
106                  uint32_t *obj_id)
107 {
108         struct dpcon_cmd_create *dpcon_cmd;
109         struct mc_command cmd = { 0 };
110         int err;
111
112         /* prepare command */
113         cmd.header = mc_encode_cmd_header(DPCON_CMDID_CREATE,
114                                           cmd_flags,
115                                           dprc_token);
116         dpcon_cmd = (struct dpcon_cmd_create *)cmd.params;
117         dpcon_cmd->num_priorities = cfg->num_priorities;
118
119         /* send command to mc*/
120         err = mc_send_command(mc_io, &cmd);
121         if (err)
122                 return err;
123
124         /* retrieve response parameters */
125         *obj_id = mc_cmd_read_object_id(&cmd);
126
127         return 0;
128 }
129
130 /**
131  * dpcon_destroy() - Destroy the DPCON object and release all its resources.
132  * @mc_io:      Pointer to MC portal's I/O object
133  * @dprc_token: Parent container token; '0' for default container
134  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
135  * @obj_id:     ID of DPCON object
136  *
137  * Return:      '0' on Success; error code otherwise.
138  */
139 int dpcon_destroy(struct fsl_mc_io *mc_io,
140                   uint16_t dprc_token,
141                   uint32_t cmd_flags,
142                   uint32_t obj_id)
143 {
144         struct dpcon_cmd_destroy *cmd_params;
145         struct mc_command cmd = { 0 };
146
147         /* prepare command */
148         cmd.header = mc_encode_cmd_header(DPCON_CMDID_DESTROY,
149                                           cmd_flags,
150                                           dprc_token);
151         cmd_params = (struct dpcon_cmd_destroy *)cmd.params;
152         cmd_params->object_id = cpu_to_le32(obj_id);
153
154         /* send command to mc*/
155         return mc_send_command(mc_io, &cmd);
156 }
157
158 /**
159  * dpcon_enable() - Enable the DPCON
160  * @mc_io:      Pointer to MC portal's I/O object
161  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
162  * @token:      Token of DPCON object
163  *
164  * Return:      '0' on Success; Error code otherwise
165  */
166 int dpcon_enable(struct fsl_mc_io *mc_io,
167                  uint32_t cmd_flags,
168                  uint16_t token)
169 {
170         struct mc_command cmd = { 0 };
171
172         /* prepare command */
173         cmd.header = mc_encode_cmd_header(DPCON_CMDID_ENABLE,
174                                           cmd_flags,
175                                           token);
176
177         /* send command to mc*/
178         return mc_send_command(mc_io, &cmd);
179 }
180
181 /**
182  * dpcon_disable() - Disable the DPCON
183  * @mc_io:      Pointer to MC portal's I/O object
184  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
185  * @token:      Token of DPCON object
186  *
187  * Return:      '0' on Success; Error code otherwise
188  */
189 int dpcon_disable(struct fsl_mc_io *mc_io,
190                   uint32_t cmd_flags,
191                   uint16_t token)
192 {
193         struct mc_command cmd = { 0 };
194
195         /* prepare command */
196         cmd.header = mc_encode_cmd_header(DPCON_CMDID_DISABLE,
197                                           cmd_flags,
198                                           token);
199
200         /* send command to mc*/
201         return mc_send_command(mc_io, &cmd);
202 }
203
204 /**
205  * dpcon_is_enabled() - Check if the DPCON is enabled.
206  * @mc_io:      Pointer to MC portal's I/O object
207  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
208  * @token:      Token of DPCON object
209  * @en:         Returns '1' if object is enabled; '0' otherwise
210  *
211  * Return:      '0' on Success; Error code otherwise.
212  */
213 int dpcon_is_enabled(struct fsl_mc_io *mc_io,
214                      uint32_t cmd_flags,
215                      uint16_t token,
216                      int *en)
217 {
218         struct dpcon_rsp_is_enabled *dpcon_rsp;
219         struct mc_command cmd = { 0 };
220         int err;
221
222         /* prepare command */
223         cmd.header = mc_encode_cmd_header(DPCON_CMDID_IS_ENABLED,
224                                           cmd_flags,
225                                           token);
226
227         /* send command to mc*/
228         err = mc_send_command(mc_io, &cmd);
229         if (err)
230                 return err;
231
232         /* retrieve response parameters */
233         dpcon_rsp = (struct dpcon_rsp_is_enabled *)cmd.params;
234         *en = dpcon_rsp->enabled & DPCON_ENABLE;
235
236         return 0;
237 }
238
239 /**
240  * dpcon_reset() - Reset the DPCON, returns the object to initial state.
241  * @mc_io:      Pointer to MC portal's I/O object
242  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
243  * @token:      Token of DPCON object
244  *
245  * Return:      '0' on Success; Error code otherwise.
246  */
247 int dpcon_reset(struct fsl_mc_io *mc_io,
248                 uint32_t cmd_flags,
249                 uint16_t token)
250 {
251         struct mc_command cmd = { 0 };
252
253         /* prepare command */
254         cmd.header = mc_encode_cmd_header(DPCON_CMDID_RESET,
255                                           cmd_flags, token);
256
257         /* send command to mc*/
258         return mc_send_command(mc_io, &cmd);
259 }
260
261 /**
262  * dpcon_get_attributes() - Retrieve DPCON attributes.
263  * @mc_io:      Pointer to MC portal's I/O object
264  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
265  * @token:      Token of DPCON object
266  * @attr:       Object's attributes
267  *
268  * Return:      '0' on Success; Error code otherwise.
269  */
270 int dpcon_get_attributes(struct fsl_mc_io *mc_io,
271                          uint32_t cmd_flags,
272                          uint16_t token,
273                          struct dpcon_attr *attr)
274 {
275         struct dpcon_rsp_get_attr *dpcon_rsp;
276         struct mc_command cmd = { 0 };
277         int err;
278
279         /* prepare command */
280         cmd.header = mc_encode_cmd_header(DPCON_CMDID_GET_ATTR,
281                                           cmd_flags,
282                                           token);
283
284         /* send command to mc*/
285         err = mc_send_command(mc_io, &cmd);
286         if (err)
287                 return err;
288
289         /* retrieve response parameters */
290         dpcon_rsp = (struct dpcon_rsp_get_attr *)cmd.params;
291         attr->id = le32_to_cpu(dpcon_rsp->id);
292         attr->qbman_ch_id = le16_to_cpu(dpcon_rsp->qbman_ch_id);
293         attr->num_priorities = dpcon_rsp->num_priorities;
294
295         return 0;
296 }
297
298 /**
299  * dpcon_set_notification() - Set DPCON notification destination
300  * @mc_io:      Pointer to MC portal's I/O object
301  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
302  * @token:      Token of DPCON object
303  * @cfg:        Notification parameters
304  *
305  * Return:      '0' on Success; Error code otherwise
306  */
307 int dpcon_set_notification(struct fsl_mc_io *mc_io,
308                            uint32_t cmd_flags,
309                            uint16_t token,
310                            struct dpcon_notification_cfg *cfg)
311 {
312         struct dpcon_cmd_set_notification *dpcon_cmd;
313         struct mc_command cmd = { 0 };
314
315         /* prepare command */
316         cmd.header = mc_encode_cmd_header(DPCON_CMDID_SET_NOTIFICATION,
317                                           cmd_flags,
318                                           token);
319         dpcon_cmd = (struct dpcon_cmd_set_notification *)cmd.params;
320         dpcon_cmd->dpio_id = cpu_to_le32(cfg->dpio_id);
321         dpcon_cmd->priority = cfg->priority;
322         dpcon_cmd->user_ctx = cpu_to_le64(cfg->user_ctx);
323
324         /* send command to mc*/
325         return mc_send_command(mc_io, &cmd);
326 }
327
328 /**
329  * dpcon_get_api_version - Get Data Path Concentrator API version
330  * @mc_io:      Pointer to MC portal's DPCON object
331  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
332  * @major_ver:  Major version of DPCON API
333  * @minor_ver:  Minor version of DPCON API
334  *
335  * Return:      '0' on Success; Error code otherwise
336  */
337 int dpcon_get_api_version(struct fsl_mc_io *mc_io,
338                           uint32_t cmd_flags,
339                           uint16_t *major_ver,
340                           uint16_t *minor_ver)
341 {
342         struct dpcon_rsp_get_api_version *rsp_params;
343         struct mc_command cmd = { 0 };
344         int err;
345
346         /* prepare command */
347         cmd.header = mc_encode_cmd_header(DPCON_CMDID_GET_API_VERSION,
348                                           cmd_flags, 0);
349
350         /* send command to mc */
351         err = mc_send_command(mc_io, &cmd);
352         if (err)
353                 return err;
354
355         /* retrieve response parameters */
356         rsp_params = (struct dpcon_rsp_get_api_version *)cmd.params;
357         *major_ver = le16_to_cpu(rsp_params->major);
358         *minor_ver = le16_to_cpu(rsp_params->minor);
359
360         return 0;
361 }