New upstream version 18.08
[deb_dpdk.git] / drivers / bus / fslmc / mc / dpdmai.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2018 NXP
3  */
4
5 #include <fsl_mc_sys.h>
6 #include <fsl_mc_cmd.h>
7 #include <fsl_dpdmai.h>
8 #include <fsl_dpdmai_cmd.h>
9
10 /**
11  * dpdmai_open() - Open a control session for the specified object
12  * @mc_io:      Pointer to MC portal's I/O object
13  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
14  * @dpdmai_id:  DPDMAI unique ID
15  * @token:      Returned token; use in subsequent API calls
16  *
17  * This function can be used to open a control session for an
18  * already created object; an object may have been declared in
19  * the DPL or by calling the dpdmai_create() function.
20  * This function returns a unique authentication token,
21  * associated with the specific object ID and the specific MC
22  * portal; this token must be used in all subsequent commands for
23  * this specific object.
24  *
25  * Return:      '0' on Success; Error code otherwise.
26  */
27 int dpdmai_open(struct fsl_mc_io *mc_io,
28                 uint32_t cmd_flags,
29                 int dpdmai_id,
30                 uint16_t *token)
31 {
32         struct dpdmai_cmd_open *cmd_params;
33         struct mc_command cmd = { 0 };
34         int err;
35
36         /* prepare command */
37         cmd.header = mc_encode_cmd_header(DPDMAI_CMDID_OPEN,
38                                           cmd_flags,
39                                           0);
40         cmd_params = (struct dpdmai_cmd_open *)cmd.params;
41         cmd_params->dpdmai_id = cpu_to_le32(dpdmai_id);
42
43         /* send command to mc*/
44         err = mc_send_command(mc_io, &cmd);
45         if (err)
46                 return err;
47
48         /* retrieve response parameters */
49         *token = mc_cmd_hdr_read_token(&cmd);
50
51         return 0;
52 }
53
54 /**
55  * dpdmai_close() - Close the control session of the object
56  * @mc_io:      Pointer to MC portal's I/O object
57  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
58  * @token:      Token of DPDMAI object
59  *
60  * After this function is called, no further operations are
61  * allowed on the object without opening a new control session.
62  *
63  * Return:      '0' on Success; Error code otherwise.
64  */
65 int dpdmai_close(struct fsl_mc_io *mc_io,
66                  uint32_t cmd_flags,
67                  uint16_t token)
68 {
69         struct mc_command cmd = { 0 };
70
71         /* prepare command */
72         cmd.header = mc_encode_cmd_header(DPDMAI_CMDID_CLOSE,
73                                           cmd_flags, token);
74
75         /* send command to mc*/
76         return mc_send_command(mc_io, &cmd);
77 }
78
79 /**
80  * dpdmai_create() - Create the DPDMAI object
81  * @mc_io:      Pointer to MC portal's I/O object
82  * @dprc_token: Parent container token; '0' for default container
83  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
84  * @cfg:        Configuration structure
85  * @obj_id:     Returned object id
86  *
87  * Create the DPDMAI object, allocate required resources and
88  * perform required initialization.
89  *
90  * The object can be created either by declaring it in the
91  * DPL file, or by calling this function.
92  *
93  * The function accepts an authentication token of a parent
94  * container that this object should be assigned to. The token
95  * can be '0' so the object will be assigned to the default container.
96  * The newly created object can be opened with the returned
97  * object id and using the container's associated tokens and MC portals.
98  *
99  * Return:      '0' on Success; Error code otherwise.
100  */
101 int dpdmai_create(struct fsl_mc_io *mc_io,
102                   uint16_t dprc_token,
103                   uint32_t cmd_flags,
104                   const struct dpdmai_cfg *cfg,
105                   uint32_t *obj_id)
106 {
107         struct dpdmai_cmd_create *cmd_params;
108         struct mc_command cmd = { 0 };
109         int err;
110
111         /* prepare command */
112         cmd.header = mc_encode_cmd_header(DPDMAI_CMDID_CREATE,
113                                           cmd_flags,
114                                           dprc_token);
115         cmd_params = (struct dpdmai_cmd_create *)cmd.params;
116         cmd_params->priorities[0] = cfg->priorities[0];
117         cmd_params->priorities[1] = cfg->priorities[1];
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  * dpdmai_destroy() - Destroy the DPDMAI 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  * @object_id:  The object id; it must be a valid id within the container that
136  *              created this object;
137  *
138  * The function accepts the authentication token of the parent container that
139  * created the object (not the one that currently owns the object). The object
140  * is searched within parent using the provided 'object_id'.
141  * All tokens to the object must be closed before calling destroy.
142  *
143  * Return:      '0' on Success; error code otherwise.
144  */
145 int dpdmai_destroy(struct fsl_mc_io *mc_io,
146                    uint16_t dprc_token,
147                    uint32_t cmd_flags,
148                    uint32_t object_id)
149 {
150         struct dpdmai_cmd_destroy *cmd_params;
151         struct mc_command cmd = { 0 };
152
153         /* prepare command */
154         cmd.header = mc_encode_cmd_header(DPDMAI_CMDID_DESTROY,
155                                           cmd_flags,
156                                           dprc_token);
157         cmd_params = (struct dpdmai_cmd_destroy *)cmd.params;
158         cmd_params->dpdmai_id = cpu_to_le32(object_id);
159
160         /* send command to mc*/
161         return mc_send_command(mc_io, &cmd);
162 }
163
164 /**
165  * dpdmai_enable() - Enable the DPDMAI, allow sending and receiving frames.
166  * @mc_io:      Pointer to MC portal's I/O object
167  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
168  * @token:      Token of DPDMAI object
169  *
170  * Return:      '0' on Success; Error code otherwise.
171  */
172 int dpdmai_enable(struct fsl_mc_io *mc_io,
173                   uint32_t cmd_flags,
174                   uint16_t token)
175 {
176         struct mc_command cmd = { 0 };
177
178         /* prepare command */
179         cmd.header = mc_encode_cmd_header(DPDMAI_CMDID_ENABLE,
180                                           cmd_flags,
181                                           token);
182
183         /* send command to mc*/
184         return mc_send_command(mc_io, &cmd);
185 }
186
187 /**
188  * dpdmai_disable() - Disable the DPDMAI, stop sending and receiving frames.
189  * @mc_io:      Pointer to MC portal's I/O object
190  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
191  * @token:      Token of DPDMAI object
192  *
193  * Return:      '0' on Success; Error code otherwise.
194  */
195 int dpdmai_disable(struct fsl_mc_io *mc_io,
196                    uint32_t cmd_flags,
197                    uint16_t token)
198 {
199         struct mc_command cmd = { 0 };
200
201         /* prepare command */
202         cmd.header = mc_encode_cmd_header(DPDMAI_CMDID_DISABLE,
203                                           cmd_flags,
204                                           token);
205
206         /* send command to mc*/
207         return mc_send_command(mc_io, &cmd);
208 }
209
210 /**
211  * dpdmai_is_enabled() - Check if the DPDMAI is enabled.
212  * @mc_io:      Pointer to MC portal's I/O object
213  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
214  * @token:      Token of DPDMAI object
215  * @en:         Returns '1' if object is enabled; '0' otherwise
216  *
217  * Return:      '0' on Success; Error code otherwise.
218  */
219 int dpdmai_is_enabled(struct fsl_mc_io *mc_io,
220                       uint32_t cmd_flags,
221                       uint16_t token,
222                       int *en)
223 {
224         struct dpdmai_rsp_is_enabled *rsp_params;
225         struct mc_command cmd = { 0 };
226         int err;
227
228         /* prepare command */
229         cmd.header = mc_encode_cmd_header(DPDMAI_CMDID_IS_ENABLED,
230                                           cmd_flags,
231                                           token);
232
233         /* send command to mc*/
234         err = mc_send_command(mc_io, &cmd);
235         if (err)
236                 return err;
237
238         /* retrieve response parameters */
239         rsp_params = (struct dpdmai_rsp_is_enabled *)cmd.params;
240         *en = dpdmai_get_field(rsp_params->en, ENABLE);
241
242         return 0;
243 }
244
245 /**
246  * dpdmai_reset() - Reset the DPDMAI, returns the object to initial state.
247  * @mc_io:      Pointer to MC portal's I/O object
248  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
249  * @token:      Token of DPDMAI object
250  *
251  * Return:      '0' on Success; Error code otherwise.
252  */
253 int dpdmai_reset(struct fsl_mc_io *mc_io,
254                  uint32_t cmd_flags,
255                  uint16_t token)
256 {
257         struct mc_command cmd = { 0 };
258
259         /* prepare command */
260         cmd.header = mc_encode_cmd_header(DPDMAI_CMDID_RESET,
261                                           cmd_flags,
262                                           token);
263
264         /* send command to mc*/
265         return mc_send_command(mc_io, &cmd);
266 }
267
268 /**
269  * dpdmai_get_attributes() - Retrieve DPDMAI attributes.
270  * @mc_io:      Pointer to MC portal's I/O object
271  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
272  * @token:      Token of DPDMAI object
273  * @attr:       Returned object's attributes
274  *
275  * Return:      '0' on Success; Error code otherwise.
276  */
277 int dpdmai_get_attributes(struct fsl_mc_io *mc_io,
278                           uint32_t cmd_flags,
279                           uint16_t token,
280                           struct dpdmai_attr *attr)
281 {
282         struct dpdmai_rsp_get_attr *rsp_params;
283         struct mc_command cmd = { 0 };
284         int err;
285
286         /* prepare command */
287         cmd.header = mc_encode_cmd_header(DPDMAI_CMDID_GET_ATTR,
288                                           cmd_flags,
289                                           token);
290
291         /* send command to mc*/
292         err = mc_send_command(mc_io, &cmd);
293         if (err)
294                 return err;
295
296         /* retrieve response parameters */
297         rsp_params = (struct dpdmai_rsp_get_attr *)cmd.params;
298         attr->id = le32_to_cpu(rsp_params->id);
299         attr->num_of_priorities = rsp_params->num_of_priorities;
300
301         return 0;
302 }
303
304 /**
305  * dpdmai_set_rx_queue() - Set Rx queue configuration
306  * @mc_io:      Pointer to MC portal's I/O object
307  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
308  * @token:      Token of DPDMAI object
309  * @priority:   Select the queue relative to number of
310  *              priorities configured at DPDMAI creation; use
311  *              DPDMAI_ALL_QUEUES to configure all Rx queues
312  *              identically.
313  * @cfg:        Rx queue configuration
314  *
315  * Return:      '0' on Success; Error code otherwise.
316  */
317 int dpdmai_set_rx_queue(struct fsl_mc_io *mc_io,
318                         uint32_t cmd_flags,
319                         uint16_t token,
320                         uint8_t priority,
321                         const struct dpdmai_rx_queue_cfg *cfg)
322 {
323         struct dpdmai_cmd_set_rx_queue *cmd_params;
324         struct mc_command cmd = { 0 };
325
326         /* prepare command */
327         cmd.header = mc_encode_cmd_header(DPDMAI_CMDID_SET_RX_QUEUE,
328                                           cmd_flags,
329                                           token);
330         cmd_params = (struct dpdmai_cmd_set_rx_queue *)cmd.params;
331         cmd_params->dest_id = cpu_to_le32(cfg->dest_cfg.dest_id);
332         cmd_params->dest_priority = cfg->dest_cfg.priority;
333         cmd_params->priority = priority;
334         cmd_params->user_ctx = cpu_to_le64(cfg->user_ctx);
335         cmd_params->options = cpu_to_le32(cfg->options);
336         dpdmai_set_field(cmd_params->dest_type,
337                          DEST_TYPE,
338                          cfg->dest_cfg.dest_type);
339
340         /* send command to mc*/
341         return mc_send_command(mc_io, &cmd);
342 }
343
344 /**
345  * dpdmai_get_rx_queue() - Retrieve Rx queue attributes.
346  * @mc_io:      Pointer to MC portal's I/O object
347  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
348  * @token:      Token of DPDMAI object
349  * @priority:   Select the queue relative to number of
350  *              priorities configured at DPDMAI creation
351  * @attr:       Returned Rx queue attributes
352  *
353  * Return:      '0' on Success; Error code otherwise.
354  */
355 int dpdmai_get_rx_queue(struct fsl_mc_io *mc_io,
356                         uint32_t cmd_flags,
357                         uint16_t token,
358                         uint8_t priority,
359                         struct dpdmai_rx_queue_attr *attr)
360 {
361         struct dpdmai_cmd_get_queue *cmd_params;
362         struct dpdmai_rsp_get_rx_queue *rsp_params;
363         struct mc_command cmd = { 0 };
364         int err;
365
366         /* prepare command */
367         cmd.header = mc_encode_cmd_header(DPDMAI_CMDID_GET_RX_QUEUE,
368                                           cmd_flags,
369                                           token);
370         cmd_params = (struct dpdmai_cmd_get_queue *)cmd.params;
371         cmd_params->priority = priority;
372
373         /* send command to mc*/
374         err = mc_send_command(mc_io, &cmd);
375         if (err)
376                 return err;
377
378         /* retrieve response parameters */
379         rsp_params = (struct dpdmai_rsp_get_rx_queue *)cmd.params;
380         attr->user_ctx = le64_to_cpu(rsp_params->user_ctx);
381         attr->fqid = le32_to_cpu(rsp_params->fqid);
382         attr->dest_cfg.dest_id = le32_to_cpu(rsp_params->dest_id);
383         attr->dest_cfg.priority = le32_to_cpu(rsp_params->dest_priority);
384         attr->dest_cfg.dest_type = dpdmai_get_field(rsp_params->dest_type,
385                                                     DEST_TYPE);
386
387         return 0;
388 }
389
390 /**
391  * dpdmai_get_tx_queue() - Retrieve Tx queue attributes.
392  * @mc_io:      Pointer to MC portal's I/O object
393  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
394  * @token:      Token of DPDMAI object
395  * @priority:   Select the queue relative to number of
396  *              priorities configured at DPDMAI creation
397  * @attr:       Returned Tx queue attributes
398  *
399  * Return:      '0' on Success; Error code otherwise.
400  */
401 int dpdmai_get_tx_queue(struct fsl_mc_io *mc_io,
402                         uint32_t cmd_flags,
403                         uint16_t token,
404                         uint8_t priority,
405                         struct dpdmai_tx_queue_attr *attr)
406 {
407         struct dpdmai_cmd_get_queue *cmd_params;
408         struct dpdmai_rsp_get_tx_queue *rsp_params;
409         struct mc_command cmd = { 0 };
410         int err;
411
412         /* prepare command */
413         cmd.header = mc_encode_cmd_header(DPDMAI_CMDID_GET_TX_QUEUE,
414                                           cmd_flags,
415                                           token);
416         cmd_params = (struct dpdmai_cmd_get_queue *)cmd.params;
417         cmd_params->priority = priority;
418
419         /* send command to mc*/
420         err = mc_send_command(mc_io, &cmd);
421         if (err)
422                 return err;
423
424         /* retrieve response parameters */
425         rsp_params = (struct dpdmai_rsp_get_tx_queue *)cmd.params;
426         attr->fqid = le32_to_cpu(rsp_params->fqid);
427
428         return 0;
429 }