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