xref: /f-stack/dpdk/drivers/bus/fslmc/mc/dpio.c (revision d30ea906)
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  */
dpio_open(struct fsl_mc_io * mc_io,uint32_t cmd_flags,int dpio_id,uint16_t * token)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  */
dpio_close(struct fsl_mc_io * mc_io,uint32_t cmd_flags,uint16_t token)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  */
dpio_create(struct fsl_mc_io * mc_io,uint16_t dprc_token,uint32_t cmd_flags,const struct dpio_cfg * cfg,uint32_t * obj_id)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  */
dpio_destroy(struct fsl_mc_io * mc_io,uint16_t dprc_token,uint32_t cmd_flags,uint32_t object_id)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  */
dpio_enable(struct fsl_mc_io * mc_io,uint32_t cmd_flags,uint16_t token)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  */
dpio_disable(struct fsl_mc_io * mc_io,uint32_t cmd_flags,uint16_t token)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  */
dpio_is_enabled(struct fsl_mc_io * mc_io,uint32_t cmd_flags,uint16_t token,int * en)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  */
dpio_reset(struct fsl_mc_io * mc_io,uint32_t cmd_flags,uint16_t token)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 /**
272  * dpio_get_attributes() - Retrieve DPIO attributes
273  * @mc_io:	Pointer to MC portal's I/O object
274  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
275  * @token:	Token of DPIO object
276  * @attr:	Returned object's attributes
277  *
278  * Return:	'0' on Success; Error code otherwise
279  */
dpio_get_attributes(struct fsl_mc_io * mc_io,uint32_t cmd_flags,uint16_t token,struct dpio_attr * attr)280 int dpio_get_attributes(struct fsl_mc_io *mc_io,
281 			uint32_t cmd_flags,
282 			uint16_t token,
283 			struct dpio_attr *attr)
284 {
285 	struct dpio_rsp_get_attr *rsp_params;
286 	struct mc_command cmd = { 0 };
287 	int err;
288 
289 	/* prepare command */
290 	cmd.header = mc_encode_cmd_header(DPIO_CMDID_GET_ATTR,
291 					  cmd_flags,
292 					  token);
293 
294 	/* send command to mc*/
295 	err = mc_send_command(mc_io, &cmd);
296 	if (err)
297 		return err;
298 
299 	/* retrieve response parameters */
300 	rsp_params = (struct dpio_rsp_get_attr *)cmd.params;
301 	attr->id = le32_to_cpu(rsp_params->id);
302 	attr->qbman_portal_id = le16_to_cpu(rsp_params->qbman_portal_id);
303 	attr->num_priorities = rsp_params->num_priorities;
304 	attr->qbman_portal_ce_offset =
305 				le64_to_cpu(rsp_params->qbman_portal_ce_offset);
306 	attr->qbman_portal_ci_offset =
307 				le64_to_cpu(rsp_params->qbman_portal_ci_offset);
308 	attr->qbman_version = le32_to_cpu(rsp_params->qbman_version);
309 	attr->clk = le32_to_cpu(rsp_params->clk);
310 	attr->channel_mode = dpio_get_field(rsp_params->channel_mode,
311 					    ATTR_CHANNEL_MODE);
312 
313 	return 0;
314 }
315 
316 /**
317  * dpio_set_stashing_destination() - Set the stashing destination.
318  * @mc_io:	Pointer to MC portal's I/O object
319  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
320  * @token:	Token of DPIO object
321  * @sdest:	Stashing destination value
322  *
323  * Return:	'0' on Success; Error code otherwise.
324  */
dpio_set_stashing_destination(struct fsl_mc_io * mc_io,uint32_t cmd_flags,uint16_t token,uint8_t sdest)325 int dpio_set_stashing_destination(struct fsl_mc_io *mc_io,
326 				  uint32_t cmd_flags,
327 				  uint16_t token,
328 				  uint8_t sdest)
329 {
330 	struct dpio_stashing_dest *cmd_params;
331 	struct mc_command cmd = { 0 };
332 
333 	/* prepare command */
334 	cmd.header = mc_encode_cmd_header(DPIO_CMDID_SET_STASHING_DEST,
335 					  cmd_flags,
336 					  token);
337 	cmd_params = (struct dpio_stashing_dest *)cmd.params;
338 	cmd_params->sdest = sdest;
339 
340 	/* send command to mc*/
341 	return mc_send_command(mc_io, &cmd);
342 }
343 
344 /**
345  * dpio_get_stashing_destination() - Get the stashing destination..
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 DPIO object
349  * @sdest:	Returns the stashing destination value
350  *
351  * Return:	'0' on Success; Error code otherwise.
352  */
dpio_get_stashing_destination(struct fsl_mc_io * mc_io,uint32_t cmd_flags,uint16_t token,uint8_t * sdest)353 int dpio_get_stashing_destination(struct fsl_mc_io *mc_io,
354 				  uint32_t cmd_flags,
355 				  uint16_t token,
356 				  uint8_t *sdest)
357 {
358 	struct dpio_stashing_dest *rsp_params;
359 	struct mc_command cmd = { 0 };
360 	int err;
361 
362 	/* prepare command */
363 	cmd.header = mc_encode_cmd_header(DPIO_CMDID_GET_STASHING_DEST,
364 					  cmd_flags,
365 					  token);
366 
367 	/* send command to mc*/
368 	err = mc_send_command(mc_io, &cmd);
369 	if (err)
370 		return err;
371 
372 	/* retrieve response parameters */
373 	rsp_params = (struct dpio_stashing_dest *)cmd.params;
374 	*sdest = rsp_params->sdest;
375 
376 	return 0;
377 }
378 
379 /**
380  * dpio_add_static_dequeue_channel() - Add a static dequeue channel.
381  * @mc_io:		Pointer to MC portal's I/O object
382  * @cmd_flags:		Command flags; one or more of 'MC_CMD_FLAG_'
383  * @token:		Token of DPIO object
384  * @dpcon_id:		DPCON object ID
385  * @channel_index:	Returned channel index to be used in qbman API
386  *
387  * Return:	'0' on Success; Error code otherwise.
388  */
dpio_add_static_dequeue_channel(struct fsl_mc_io * mc_io,uint32_t cmd_flags,uint16_t token,int dpcon_id,uint8_t * channel_index)389 int dpio_add_static_dequeue_channel(struct fsl_mc_io *mc_io,
390 				    uint32_t cmd_flags,
391 				    uint16_t token,
392 				    int dpcon_id,
393 				    uint8_t *channel_index)
394 {
395 	struct dpio_rsp_add_static_dequeue_channel *rsp_params;
396 	struct dpio_cmd_static_dequeue_channel *cmd_params;
397 	struct mc_command cmd = { 0 };
398 	int err;
399 
400 	/* prepare command */
401 	cmd.header = mc_encode_cmd_header(DPIO_CMDID_ADD_STATIC_DEQUEUE_CHANNEL,
402 					  cmd_flags,
403 					  token);
404 	cmd_params = (struct dpio_cmd_static_dequeue_channel *)cmd.params;
405 	cmd_params->dpcon_id = cpu_to_le32(dpcon_id);
406 
407 	/* send command to mc*/
408 	err = mc_send_command(mc_io, &cmd);
409 	if (err)
410 		return err;
411 
412 	/* retrieve response parameters */
413 	rsp_params = (struct dpio_rsp_add_static_dequeue_channel *)cmd.params;
414 	*channel_index = rsp_params->channel_index;
415 
416 	return 0;
417 }
418 
419 /**
420  * dpio_remove_static_dequeue_channel() - Remove a static dequeue channel.
421  * @mc_io:	Pointer to MC portal's I/O object
422  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
423  * @token:	Token of DPIO object
424  * @dpcon_id:	DPCON object ID
425  *
426  * Return:	'0' on Success; Error code otherwise.
427  */
dpio_remove_static_dequeue_channel(struct fsl_mc_io * mc_io,uint32_t cmd_flags,uint16_t token,int dpcon_id)428 int dpio_remove_static_dequeue_channel(struct fsl_mc_io *mc_io,
429 				       uint32_t cmd_flags,
430 				       uint16_t token,
431 				       int dpcon_id)
432 {
433 	struct dpio_cmd_static_dequeue_channel *cmd_params;
434 	struct mc_command cmd = { 0 };
435 
436 	/* prepare command */
437 	cmd.header = mc_encode_cmd_header(
438 				DPIO_CMDID_REMOVE_STATIC_DEQUEUE_CHANNEL,
439 				cmd_flags,
440 				token);
441 	cmd_params = (struct dpio_cmd_static_dequeue_channel *)cmd.params;
442 	cmd_params->dpcon_id = cpu_to_le32(dpcon_id);
443 
444 	/* send command to mc*/
445 	return mc_send_command(mc_io, &cmd);
446 }
447 
448 /**
449  * dpio_get_api_version() - Get Data Path I/O API version
450  * @mc_io:	Pointer to MC portal's I/O object
451  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
452  * @major_ver:	Major version of data path i/o API
453  * @minor_ver:	Minor version of data path i/o API
454  *
455  * Return:  '0' on Success; Error code otherwise.
456  */
dpio_get_api_version(struct fsl_mc_io * mc_io,uint32_t cmd_flags,uint16_t * major_ver,uint16_t * minor_ver)457 int dpio_get_api_version(struct fsl_mc_io *mc_io,
458 			 uint32_t cmd_flags,
459 			 uint16_t *major_ver,
460 			 uint16_t *minor_ver)
461 {
462 	struct dpio_rsp_get_api_version *rsp_params;
463 	struct mc_command cmd = { 0 };
464 	int err;
465 
466 	cmd.header = mc_encode_cmd_header(DPIO_CMDID_GET_API_VERSION,
467 					cmd_flags,
468 					0);
469 
470 	err = mc_send_command(mc_io, &cmd);
471 	if (err)
472 		return err;
473 
474 	rsp_params = (struct dpio_rsp_get_api_version *)cmd.params;
475 	*major_ver = le16_to_cpu(rsp_params->major);
476 	*minor_ver = le16_to_cpu(rsp_params->minor);
477 
478 	return 0;
479 }
480