xref: /dpdk/drivers/bus/fslmc/mc/dpdmai.c (revision 2cb2abf304fcecc0e2804b8da61760d4f2cb9a7e)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2018-2021 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  */
dpdmai_open(struct fsl_mc_io * mc_io,uint32_t cmd_flags,int dpdmai_id,uint16_t * token)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  */
dpdmai_close(struct fsl_mc_io * mc_io,uint32_t cmd_flags,uint16_t token)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  */
dpdmai_create(struct fsl_mc_io * mc_io,uint16_t dprc_token,uint32_t cmd_flags,const struct dpdmai_cfg * cfg,uint32_t * obj_id)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->num_queues = cfg->num_queues;
117 	cmd_params->priorities[0] = cfg->priorities[0];
118 	cmd_params->priorities[1] = cfg->priorities[1];
119 	cmd_params->options = cpu_to_le32(cfg->adv.options);
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  * dpdmai_destroy() - Destroy the DPDMAI 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  */
dpdmai_destroy(struct fsl_mc_io * mc_io,uint16_t dprc_token,uint32_t cmd_flags,uint32_t object_id)147 int dpdmai_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 dpdmai_cmd_destroy *cmd_params;
153 	struct mc_command cmd = { 0 };
154 
155 	/* prepare command */
156 	cmd.header = mc_encode_cmd_header(DPDMAI_CMDID_DESTROY,
157 					  cmd_flags,
158 					  dprc_token);
159 	cmd_params = (struct dpdmai_cmd_destroy *)cmd.params;
160 	cmd_params->dpdmai_id = cpu_to_le32(object_id);
161 
162 	/* send command to mc*/
163 	return mc_send_command(mc_io, &cmd);
164 }
165 
166 /**
167  * dpdmai_enable() - Enable the DPDMAI, allow sending and receiving frames.
168  * @mc_io:	Pointer to MC portal's I/O object
169  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
170  * @token:	Token of DPDMAI object
171  *
172  * Return:	'0' on Success; Error code otherwise.
173  */
dpdmai_enable(struct fsl_mc_io * mc_io,uint32_t cmd_flags,uint16_t token)174 int dpdmai_enable(struct fsl_mc_io *mc_io,
175 		  uint32_t cmd_flags,
176 		  uint16_t token)
177 {
178 	struct mc_command cmd = { 0 };
179 
180 	/* prepare command */
181 	cmd.header = mc_encode_cmd_header(DPDMAI_CMDID_ENABLE,
182 					  cmd_flags,
183 					  token);
184 
185 	/* send command to mc*/
186 	return mc_send_command(mc_io, &cmd);
187 }
188 
189 /**
190  * dpdmai_disable() - Disable the DPDMAI, stop sending and receiving frames.
191  * @mc_io:	Pointer to MC portal's I/O object
192  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
193  * @token:	Token of DPDMAI object
194  *
195  * Return:	'0' on Success; Error code otherwise.
196  */
dpdmai_disable(struct fsl_mc_io * mc_io,uint32_t cmd_flags,uint16_t token)197 int dpdmai_disable(struct fsl_mc_io *mc_io,
198 		   uint32_t cmd_flags,
199 		   uint16_t token)
200 {
201 	struct mc_command cmd = { 0 };
202 
203 	/* prepare command */
204 	cmd.header = mc_encode_cmd_header(DPDMAI_CMDID_DISABLE,
205 					  cmd_flags,
206 					  token);
207 
208 	/* send command to mc*/
209 	return mc_send_command(mc_io, &cmd);
210 }
211 
212 /**
213  * dpdmai_is_enabled() - Check if the DPDMAI is enabled.
214  * @mc_io:	Pointer to MC portal's I/O object
215  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
216  * @token:	Token of DPDMAI object
217  * @en:		Returns '1' if object is enabled; '0' otherwise
218  *
219  * Return:	'0' on Success; Error code otherwise.
220  */
dpdmai_is_enabled(struct fsl_mc_io * mc_io,uint32_t cmd_flags,uint16_t token,int * en)221 int dpdmai_is_enabled(struct fsl_mc_io *mc_io,
222 		      uint32_t cmd_flags,
223 		      uint16_t token,
224 		      int *en)
225 {
226 	struct dpdmai_rsp_is_enabled *rsp_params;
227 	struct mc_command cmd = { 0 };
228 	int err;
229 
230 	/* prepare command */
231 	cmd.header = mc_encode_cmd_header(DPDMAI_CMDID_IS_ENABLED,
232 					  cmd_flags,
233 					  token);
234 
235 	/* send command to mc*/
236 	err = mc_send_command(mc_io, &cmd);
237 	if (err)
238 		return err;
239 
240 	/* retrieve response parameters */
241 	rsp_params = (struct dpdmai_rsp_is_enabled *)cmd.params;
242 	*en = dpdmai_get_field(rsp_params->en, ENABLE);
243 
244 	return 0;
245 }
246 
247 /**
248  * dpdmai_reset() - Reset the DPDMAI, returns the object to initial state.
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 DPDMAI object
252  *
253  * Return:	'0' on Success; Error code otherwise.
254  */
dpdmai_reset(struct fsl_mc_io * mc_io,uint32_t cmd_flags,uint16_t token)255 int dpdmai_reset(struct fsl_mc_io *mc_io,
256 		 uint32_t cmd_flags,
257 		 uint16_t token)
258 {
259 	struct mc_command cmd = { 0 };
260 
261 	/* prepare command */
262 	cmd.header = mc_encode_cmd_header(DPDMAI_CMDID_RESET,
263 					  cmd_flags,
264 					  token);
265 
266 	/* send command to mc*/
267 	return mc_send_command(mc_io, &cmd);
268 }
269 
270 /**
271  * dpdmai_get_attributes() - Retrieve DPDMAI attributes.
272  * @mc_io:	Pointer to MC portal's I/O object
273  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
274  * @token:	Token of DPDMAI object
275  * @attr:	Returned object's attributes
276  *
277  * Return:	'0' on Success; Error code otherwise.
278  */
dpdmai_get_attributes(struct fsl_mc_io * mc_io,uint32_t cmd_flags,uint16_t token,struct dpdmai_attr * attr)279 int dpdmai_get_attributes(struct fsl_mc_io *mc_io,
280 			  uint32_t cmd_flags,
281 			  uint16_t token,
282 			  struct dpdmai_attr *attr)
283 {
284 	struct dpdmai_rsp_get_attr *rsp_params;
285 	struct mc_command cmd = { 0 };
286 	int err;
287 
288 	/* prepare command */
289 	cmd.header = mc_encode_cmd_header(DPDMAI_CMDID_GET_ATTR,
290 					  cmd_flags,
291 					  token);
292 
293 	/* send command to mc*/
294 	err = mc_send_command(mc_io, &cmd);
295 	if (err)
296 		return err;
297 
298 	/* retrieve response parameters */
299 	rsp_params = (struct dpdmai_rsp_get_attr *)cmd.params;
300 	attr->id = le32_to_cpu(rsp_params->id);
301 	attr->num_of_priorities = rsp_params->num_of_priorities;
302 	attr->num_of_queues = rsp_params->num_of_queues;
303 	attr->options = le32_to_cpu(rsp_params->options);
304 
305 	return 0;
306 }
307 
308 /**
309  * dpdmai_set_rx_queue() - Set Rx queue configuration
310  * @mc_io:	Pointer to MC portal's I/O object
311  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
312  * @token:	Token of DPDMAI object
313  * @queue_idx: Rx queue index. Accepted values are form 0 to num_queues
314  *		parameter provided in dpdmai_create
315  * @priority:	Select the queue relative to number of
316  *		priorities configured at DPDMAI creation; use
317  *		DPDMAI_ALL_QUEUES to configure all Rx queues
318  *		identically.
319  * @cfg:	Rx queue configuration
320  *
321  * Return:	'0' on Success; Error code otherwise.
322  */
dpdmai_set_rx_queue(struct fsl_mc_io * mc_io,uint32_t cmd_flags,uint16_t token,uint8_t queue_idx,uint8_t priority,const struct dpdmai_rx_queue_cfg * cfg)323 int dpdmai_set_rx_queue(struct fsl_mc_io *mc_io,
324 			uint32_t cmd_flags,
325 			uint16_t token,
326 			uint8_t queue_idx,
327 			uint8_t priority,
328 			const struct dpdmai_rx_queue_cfg *cfg)
329 {
330 	struct dpdmai_cmd_set_rx_queue *cmd_params;
331 	struct mc_command cmd = { 0 };
332 
333 	/* prepare command */
334 	cmd.header = mc_encode_cmd_header(DPDMAI_CMDID_SET_RX_QUEUE,
335 					  cmd_flags,
336 					  token);
337 	cmd_params = (struct dpdmai_cmd_set_rx_queue *)cmd.params;
338 	cmd_params->dest_id = cpu_to_le32(cfg->dest_cfg.dest_id);
339 	cmd_params->dest_priority = cfg->dest_cfg.priority;
340 	cmd_params->priority = priority;
341 	cmd_params->queue_idx = queue_idx;
342 	cmd_params->user_ctx = cpu_to_le64(cfg->user_ctx);
343 	cmd_params->options = cpu_to_le32(cfg->options);
344 	dpdmai_set_field(cmd_params->dest_type,
345 			 DEST_TYPE,
346 			 cfg->dest_cfg.dest_type);
347 
348 	/* send command to mc*/
349 	return mc_send_command(mc_io, &cmd);
350 }
351 
352 /**
353  * dpdmai_get_rx_queue() - Retrieve Rx queue attributes.
354  * @mc_io:	Pointer to MC portal's I/O object
355  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
356  * @token:	Token of DPDMAI object
357  * @queue_idx: Rx queue index. Accepted values are form 0 to num_queues
358  *		parameter provided in dpdmai_create
359  * @priority:	Select the queue relative to number of
360  *		priorities configured at DPDMAI creation
361  * @attr:	Returned Rx queue attributes
362  *
363  * Return:	'0' on Success; Error code otherwise.
364  */
dpdmai_get_rx_queue(struct fsl_mc_io * mc_io,uint32_t cmd_flags,uint16_t token,uint8_t queue_idx,uint8_t priority,struct dpdmai_rx_queue_attr * attr)365 int dpdmai_get_rx_queue(struct fsl_mc_io *mc_io,
366 			uint32_t cmd_flags,
367 			uint16_t token,
368 			uint8_t queue_idx,
369 			uint8_t priority,
370 			struct dpdmai_rx_queue_attr *attr)
371 {
372 	struct dpdmai_cmd_get_queue *cmd_params;
373 	struct dpdmai_rsp_get_rx_queue *rsp_params;
374 	struct mc_command cmd = { 0 };
375 	int err;
376 
377 	/* prepare command */
378 	cmd.header = mc_encode_cmd_header(DPDMAI_CMDID_GET_RX_QUEUE,
379 					  cmd_flags,
380 					  token);
381 	cmd_params = (struct dpdmai_cmd_get_queue *)cmd.params;
382 	cmd_params->priority = priority;
383 	cmd_params->queue_idx = queue_idx;
384 
385 	/* send command to mc*/
386 	err = mc_send_command(mc_io, &cmd);
387 	if (err)
388 		return err;
389 
390 	/* retrieve response parameters */
391 	rsp_params = (struct dpdmai_rsp_get_rx_queue *)cmd.params;
392 	attr->user_ctx = le64_to_cpu(rsp_params->user_ctx);
393 	attr->fqid = le32_to_cpu(rsp_params->fqid);
394 	attr->dest_cfg.dest_id = le32_to_cpu(rsp_params->dest_id);
395 	attr->dest_cfg.priority = le32_to_cpu(rsp_params->dest_priority);
396 	attr->dest_cfg.dest_type = dpdmai_get_field(rsp_params->dest_type,
397 						    DEST_TYPE);
398 
399 	return 0;
400 }
401 
402 /**
403  * dpdmai_get_tx_queue() - Retrieve Tx queue attributes.
404  * @mc_io:	Pointer to MC portal's I/O object
405  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
406  * @token:	Token of DPDMAI object
407  * @queue_idx: Tx queue index. Accepted values are form 0 to num_queues
408  *		parameter provided in dpdmai_create
409  * @priority:	Select the queue relative to number of
410  *		priorities configured at DPDMAI creation
411  * @attr:	Returned Tx queue attributes
412  *
413  * Return:	'0' on Success; Error code otherwise.
414  */
dpdmai_get_tx_queue(struct fsl_mc_io * mc_io,uint32_t cmd_flags,uint16_t token,uint8_t queue_idx,uint8_t priority,struct dpdmai_tx_queue_attr * attr)415 int dpdmai_get_tx_queue(struct fsl_mc_io *mc_io,
416 			uint32_t cmd_flags,
417 			uint16_t token,
418 			uint8_t queue_idx,
419 			uint8_t priority,
420 			struct dpdmai_tx_queue_attr *attr)
421 {
422 	struct dpdmai_cmd_get_queue *cmd_params;
423 	struct dpdmai_rsp_get_tx_queue *rsp_params;
424 	struct mc_command cmd = { 0 };
425 	int err;
426 
427 	/* prepare command */
428 	cmd.header = mc_encode_cmd_header(DPDMAI_CMDID_GET_TX_QUEUE,
429 					  cmd_flags,
430 					  token);
431 	cmd_params = (struct dpdmai_cmd_get_queue *)cmd.params;
432 	cmd_params->priority = priority;
433 	cmd_params->queue_idx = queue_idx;
434 
435 	/* send command to mc*/
436 	err = mc_send_command(mc_io, &cmd);
437 	if (err)
438 		return err;
439 
440 	/* retrieve response parameters */
441 	rsp_params = (struct dpdmai_rsp_get_tx_queue *)cmd.params;
442 	attr->fqid = le32_to_cpu(rsp_params->fqid);
443 
444 	return 0;
445 }
446