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