New upstream version 18.02
[deb_dpdk.git] / drivers / bus / fslmc / mc / dpio.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_dpio.h>
10 #include <fsl_dpio_cmd.h>
11
12 /**
13  * dpio_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  * @dpio_id:    DPIO 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 dpio_create() function.
22  * This function returns a unique authentication token,
23  * associated with the specific object ID and any MC portals
24  * assigned to the parent container; this token must be used in
25  * all subsequent commands for this specific object.
26  *
27  * Return:      '0' on Success; Error code otherwise.
28  */
29 int dpio_open(struct fsl_mc_io *mc_io,
30               uint32_t cmd_flags,
31               int dpio_id,
32               uint16_t *token)
33 {
34         struct dpio_cmd_open *cmd_params;
35         struct mc_command cmd = { 0 };
36         int err;
37
38         /* prepare command */
39         cmd.header = mc_encode_cmd_header(DPIO_CMDID_OPEN,
40                                           cmd_flags,
41                                           0);
42         cmd_params = (struct dpio_cmd_open *)cmd.params;
43         cmd_params->dpio_id = cpu_to_le32(dpio_id);
44
45         /* send command to mc*/
46         err = mc_send_command(mc_io, &cmd);
47         if (err)
48                 return err;
49
50         /* retrieve response parameters */
51         *token = mc_cmd_hdr_read_token(&cmd);
52
53         return 0;
54 }
55
56 /**
57  * dpio_close() - Close the control session of the object
58  * @mc_io:      Pointer to MC portal's I/O object
59  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
60  * @token:      Token of DPIO object
61  *
62  * Return:      '0' on Success; Error code otherwise.
63  */
64 int dpio_close(struct fsl_mc_io *mc_io,
65                uint32_t cmd_flags,
66                uint16_t token)
67 {
68         struct mc_command cmd = { 0 };
69
70         /* prepare command */
71         cmd.header = mc_encode_cmd_header(DPIO_CMDID_CLOSE,
72                                           cmd_flags,
73                                           token);
74
75         /* send command to mc*/
76         return mc_send_command(mc_io, &cmd);
77 }
78
79 /**
80  * dpio_create() - Create the DPIO 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 DPIO 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 dpio_create(struct fsl_mc_io *mc_io,
102                 uint16_t dprc_token,
103                 uint32_t cmd_flags,
104                 const struct dpio_cfg *cfg,
105                 uint32_t *obj_id)
106 {
107         struct dpio_cmd_create *cmd_params;
108         struct mc_command cmd = { 0 };
109         int err;
110
111         /* prepare command */
112         cmd.header = mc_encode_cmd_header(DPIO_CMDID_CREATE,
113                                           cmd_flags,
114                                           dprc_token);
115         cmd_params = (struct dpio_cmd_create *)cmd.params;
116         cmd_params->num_priorities = cfg->num_priorities;
117         dpio_set_field(cmd_params->channel_mode,
118                        CHANNEL_MODE,
119                        cfg->channel_mode);
120
121         /* send command to mc*/
122         err = mc_send_command(mc_io, &cmd);
123         if (err)
124                 return err;
125
126         /* retrieve response parameters */
127         *obj_id = mc_cmd_read_object_id(&cmd);
128
129         return 0;
130 }
131
132 /**
133  * dpio_destroy() - Destroy the DPIO object and release all its resources.
134  * @mc_io:      Pointer to MC portal's I/O object
135  * @dprc_token: Parent container token; '0' for default container
136  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
137  * @object_id:  The object id; it must be a valid id within the container that
138  *              created this object;
139  *
140  * The function accepts the authentication token of the parent container that
141  * created the object (not the one that currently owns the object). The object
142  * is searched within parent using the provided 'object_id'.
143  * All tokens to the object must be closed before calling destroy.
144  *
145  * Return:      '0' on Success; Error code otherwise
146  */
147 int dpio_destroy(struct fsl_mc_io *mc_io,
148                  uint16_t dprc_token,
149                  uint32_t cmd_flags,
150                  uint32_t object_id)
151 {
152         struct dpio_cmd_destroy *cmd_params;
153         struct mc_command cmd = { 0 };
154
155         /* prepare command */
156         cmd.header = mc_encode_cmd_header(DPIO_CMDID_DESTROY,
157                         cmd_flags,
158                         dprc_token);
159
160         /* set object id to destroy */
161         cmd_params = (struct dpio_cmd_destroy *)cmd.params;
162         cmd_params->dpio_id = cpu_to_le32(object_id);
163
164         /* send command to mc*/
165         return mc_send_command(mc_io, &cmd);
166 }
167
168 /**
169  * dpio_enable() - Enable the DPIO, allow I/O portal operations.
170  * @mc_io:      Pointer to MC portal's I/O object
171  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
172  * @token:      Token of DPIO object
173  *
174  * Return:      '0' on Success; Error code otherwise
175  */
176 int dpio_enable(struct fsl_mc_io *mc_io,
177                 uint32_t cmd_flags,
178                 uint16_t token)
179 {
180         struct mc_command cmd = { 0 };
181
182         /* prepare command */
183         cmd.header = mc_encode_cmd_header(DPIO_CMDID_ENABLE,
184                                           cmd_flags,
185                                           token);
186
187         /* send command to mc*/
188         return mc_send_command(mc_io, &cmd);
189 }
190
191 /**
192  * dpio_disable() - Disable the DPIO, stop any I/O portal operation.
193  * @mc_io:      Pointer to MC portal's I/O object
194  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
195  * @token:      Token of DPIO object
196  *
197  * Return:      '0' on Success; Error code otherwise
198  */
199 int dpio_disable(struct fsl_mc_io *mc_io,
200                  uint32_t cmd_flags,
201                  uint16_t token)
202 {
203         struct mc_command cmd = { 0 };
204
205         /* prepare command */
206         cmd.header = mc_encode_cmd_header(DPIO_CMDID_DISABLE,
207                                           cmd_flags,
208                                           token);
209
210         /* send command to mc*/
211         return mc_send_command(mc_io, &cmd);
212 }
213
214 /**
215  * dpio_is_enabled() - Check if the DPIO is enabled.
216  * @mc_io:      Pointer to MC portal's I/O object
217  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
218  * @token:      Token of DPIO object
219  * @en:         Returns '1' if object is enabled; '0' otherwise
220  *
221  * Return:      '0' on Success; Error code otherwise.
222  */
223 int dpio_is_enabled(struct fsl_mc_io *mc_io,
224                     uint32_t cmd_flags,
225                     uint16_t token,
226                     int *en)
227 {
228         struct dpio_rsp_is_enabled *rsp_params;
229         struct mc_command cmd = { 0 };
230         int err;
231
232         /* prepare command */
233         cmd.header = mc_encode_cmd_header(DPIO_CMDID_IS_ENABLED, cmd_flags,
234                                           token);
235
236         /* send command to mc*/
237         err = mc_send_command(mc_io, &cmd);
238         if (err)
239                 return err;
240
241         /* retrieve response parameters */
242         rsp_params = (struct dpio_rsp_is_enabled *)cmd.params;
243         *en = dpio_get_field(rsp_params->en, ENABLE);
244
245         return 0;
246 }
247
248 /**
249  * dpio_reset() - Reset the DPIO, returns the object to initial state.
250  * @mc_io:      Pointer to MC portal's I/O object
251  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
252  * @token:      Token of DPIO object
253  *
254  * Return:      '0' on Success; Error code otherwise.
255  */
256 int dpio_reset(struct fsl_mc_io *mc_io,
257                uint32_t cmd_flags,
258                uint16_t token)
259 {
260         struct mc_command cmd = { 0 };
261
262         /* prepare command */
263         cmd.header = mc_encode_cmd_header(DPIO_CMDID_RESET,
264                                           cmd_flags,
265                                           token);
266
267         /* send command to mc*/
268         return mc_send_command(mc_io, &cmd);
269 }
270
271 int dpio_get_attributes(struct fsl_mc_io *mc_io,
272                         uint32_t cmd_flags,
273                         uint16_t token,
274                         struct dpio_attr *attr)
275 {
276         struct dpio_rsp_get_attr *rsp_params;
277         struct mc_command cmd = { 0 };
278         int err;
279
280         /* prepare command */
281         cmd.header = mc_encode_cmd_header(DPIO_CMDID_GET_ATTR,
282                                           cmd_flags,
283                                           token);
284
285         /* send command to mc*/
286         err = mc_send_command(mc_io, &cmd);
287         if (err)
288                 return err;
289
290         /* retrieve response parameters */
291         rsp_params = (struct dpio_rsp_get_attr *)cmd.params;
292         attr->id = le32_to_cpu(rsp_params->id);
293         attr->qbman_portal_id = le16_to_cpu(rsp_params->qbman_portal_id);
294         attr->num_priorities = rsp_params->num_priorities;
295         attr->qbman_portal_ce_offset =
296                                 le64_to_cpu(rsp_params->qbman_portal_ce_offset);
297         attr->qbman_portal_ci_offset =
298                                 le64_to_cpu(rsp_params->qbman_portal_ci_offset);
299         attr->qbman_version = le32_to_cpu(rsp_params->qbman_version);
300         attr->clk = le32_to_cpu(rsp_params->clk);
301         attr->channel_mode = dpio_get_field(rsp_params->channel_mode,
302                                             ATTR_CHANNEL_MODE);
303
304         return 0;
305 }
306
307 /**
308  * dpio_set_stashing_destination() - Set the stashing destination.
309  * @mc_io:      Pointer to MC portal's I/O object
310  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
311  * @token:      Token of DPIO object
312  * @sdest:      Stashing destination value
313  *
314  * Return:      '0' on Success; Error code otherwise.
315  */
316 int dpio_set_stashing_destination(struct fsl_mc_io *mc_io,
317                                   uint32_t cmd_flags,
318                                   uint16_t token,
319                                   uint8_t sdest)
320 {
321         struct dpio_stashing_dest *cmd_params;
322         struct mc_command cmd = { 0 };
323
324         /* prepare command */
325         cmd.header = mc_encode_cmd_header(DPIO_CMDID_SET_STASHING_DEST,
326                                           cmd_flags,
327                                           token);
328         cmd_params = (struct dpio_stashing_dest *)cmd.params;
329         cmd_params->sdest = sdest;
330
331         /* send command to mc*/
332         return mc_send_command(mc_io, &cmd);
333 }
334
335 /**
336  * dpio_get_stashing_destination() - Get the stashing destination..
337  * @mc_io:      Pointer to MC portal's I/O object
338  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
339  * @token:      Token of DPIO object
340  * @sdest:      Returns the stashing destination value
341  *
342  * Return:      '0' on Success; Error code otherwise.
343  */
344 int dpio_get_stashing_destination(struct fsl_mc_io *mc_io,
345                                   uint32_t cmd_flags,
346                                   uint16_t token,
347                                   uint8_t *sdest)
348 {
349         struct dpio_stashing_dest *rsp_params;
350         struct mc_command cmd = { 0 };
351         int err;
352
353         /* prepare command */
354         cmd.header = mc_encode_cmd_header(DPIO_CMDID_GET_STASHING_DEST,
355                                           cmd_flags,
356                                           token);
357
358         /* send command to mc*/
359         err = mc_send_command(mc_io, &cmd);
360         if (err)
361                 return err;
362
363         /* retrieve response parameters */
364         rsp_params = (struct dpio_stashing_dest *)cmd.params;
365         *sdest = rsp_params->sdest;
366
367         return 0;
368 }
369
370 /**
371  * dpio_add_static_dequeue_channel() - Add a static dequeue channel.
372  * @mc_io:              Pointer to MC portal's I/O object
373  * @cmd_flags:          Command flags; one or more of 'MC_CMD_FLAG_'
374  * @token:              Token of DPIO object
375  * @dpcon_id:           DPCON object ID
376  * @channel_index:      Returned channel index to be used in qbman API
377  *
378  * Return:      '0' on Success; Error code otherwise.
379  */
380 int dpio_add_static_dequeue_channel(struct fsl_mc_io *mc_io,
381                                     uint32_t cmd_flags,
382                                     uint16_t token,
383                                     int dpcon_id,
384                                     uint8_t *channel_index)
385 {
386         struct dpio_rsp_add_static_dequeue_channel *rsp_params;
387         struct dpio_cmd_static_dequeue_channel *cmd_params;
388         struct mc_command cmd = { 0 };
389         int err;
390
391         /* prepare command */
392         cmd.header = mc_encode_cmd_header(DPIO_CMDID_ADD_STATIC_DEQUEUE_CHANNEL,
393                                           cmd_flags,
394                                           token);
395         cmd_params = (struct dpio_cmd_static_dequeue_channel *)cmd.params;
396         cmd_params->dpcon_id = cpu_to_le32(dpcon_id);
397
398         /* send command to mc*/
399         err = mc_send_command(mc_io, &cmd);
400         if (err)
401                 return err;
402
403         /* retrieve response parameters */
404         rsp_params = (struct dpio_rsp_add_static_dequeue_channel *)cmd.params;
405         *channel_index = rsp_params->channel_index;
406
407         return 0;
408 }
409
410 /**
411  * dpio_remove_static_dequeue_channel() - Remove a static dequeue channel.
412  * @mc_io:      Pointer to MC portal's I/O object
413  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
414  * @token:      Token of DPIO object
415  * @dpcon_id:   DPCON object ID
416  *
417  * Return:      '0' on Success; Error code otherwise.
418  */
419 int dpio_remove_static_dequeue_channel(struct fsl_mc_io *mc_io,
420                                        uint32_t cmd_flags,
421                                        uint16_t token,
422                                        int dpcon_id)
423 {
424         struct dpio_cmd_static_dequeue_channel *cmd_params;
425         struct mc_command cmd = { 0 };
426
427         /* prepare command */
428         cmd.header = mc_encode_cmd_header(
429                                 DPIO_CMDID_REMOVE_STATIC_DEQUEUE_CHANNEL,
430                                 cmd_flags,
431                                 token);
432         cmd_params = (struct dpio_cmd_static_dequeue_channel *)cmd.params;
433         cmd_params->dpcon_id = cpu_to_le32(dpcon_id);
434
435         /* send command to mc*/
436         return mc_send_command(mc_io, &cmd);
437 }
438
439 /**
440  * dpio_get_api_version() - Get Data Path I/O API version
441  * @mc_io:      Pointer to MC portal's I/O object
442  * @cmd_flags:  Command flags; one or more of 'MC_CMD_FLAG_'
443  * @major_ver:  Major version of data path i/o API
444  * @minor_ver:  Minor version of data path i/o API
445  *
446  * Return:  '0' on Success; Error code otherwise.
447  */
448 int dpio_get_api_version(struct fsl_mc_io *mc_io,
449                          uint32_t cmd_flags,
450                          uint16_t *major_ver,
451                          uint16_t *minor_ver)
452 {
453         struct dpio_rsp_get_api_version *rsp_params;
454         struct mc_command cmd = { 0 };
455         int err;
456
457         cmd.header = mc_encode_cmd_header(DPIO_CMDID_GET_API_VERSION,
458                                         cmd_flags,
459                                         0);
460
461         err = mc_send_command(mc_io, &cmd);
462         if (err)
463                 return err;
464
465         rsp_params = (struct dpio_rsp_get_api_version *)cmd.params;
466         *major_ver = le16_to_cpu(rsp_params->major);
467         *minor_ver = le16_to_cpu(rsp_params->minor);
468
469         return 0;
470 }