1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2013-2016 Freescale Semiconductor, Inc.
4  * Copyright 2017, 2023 NXP
5  */
6 
7 #include <fsl-mc/fsl_mc_sys.h>
8 #include <fsl-mc/fsl_mc_cmd.h>
9 #include <fsl-mc/fsl_dpni.h>
10 
11 /**
12  * dpni_open() - Open a control session for the specified object
13  * @mc_io:	Pointer to MC portal's I/O object
14  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
15  * @dpni_id:	DPNI unique ID
16  * @token:	Returned token; use in subsequent API calls
17  *
18  * This function can be used to open a control session for an
19  * already created object; an object may have been declared in
20  * the DPL or by calling the dpni_create() function.
21  * This function returns a unique authentication token,
22  * associated with the specific object ID and the specific MC
23  * portal; this token must be used in all subsequent commands for
24  * this specific object.
25  *
26  * Return:	'0' on Success; Error code otherwise.
27  */
dpni_open(struct fsl_mc_io * mc_io,u32 cmd_flags,int dpni_id,u16 * token)28 int dpni_open(struct fsl_mc_io *mc_io, u32 cmd_flags, int dpni_id, u16 *token)
29 {
30 	struct dpni_cmd_open *cmd_params;
31 	struct mc_command cmd = { 0 };
32 
33 	int err;
34 
35 	/* prepare command */
36 	cmd.header = mc_encode_cmd_header(DPNI_CMDID_OPEN,
37 					  cmd_flags,
38 					  0);
39 	cmd_params = (struct dpni_cmd_open *)cmd.params;
40 	cmd_params->dpni_id = cpu_to_le32(dpni_id);
41 
42 	/* send command to mc*/
43 	err = mc_send_command(mc_io, &cmd);
44 	if (err)
45 		return err;
46 
47 	/* retrieve response parameters */
48 	*token = mc_cmd_hdr_read_token(&cmd);
49 
50 	return 0;
51 }
52 
53 /**
54  * dpni_close() - Close the control session of the object
55  * @mc_io:	Pointer to MC portal's I/O object
56  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
57  * @token:	Token of DPNI object
58  *
59  * After this function is called, no further operations are
60  * allowed on the object without opening a new control session.
61  *
62  * Return:	'0' on Success; Error code otherwise.
63  */
dpni_close(struct fsl_mc_io * mc_io,u32 cmd_flags,u16 token)64 int dpni_close(struct fsl_mc_io *mc_io, u32 cmd_flags, u16 token)
65 {
66 	struct mc_command cmd = { 0 };
67 
68 	/* prepare command */
69 	cmd.header = mc_encode_cmd_header(DPNI_CMDID_CLOSE,
70 					  cmd_flags,
71 					  token);
72 
73 	/* send command to mc*/
74 	return mc_send_command(mc_io, &cmd);
75 }
76 
77 /**
78  * dpni_create() - Create the DPNI object
79  * @mc_io:	Pointer to MC portal's I/O object
80  * @dprc_token:	Parent container token; '0' for default container
81  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
82  * @cfg:	Configuration structure
83  * @obj_id:	Returned object id
84  *
85  * Create the DPNI object, allocate required resources and
86  * perform required initialization.
87  *
88  * The object can be created either by declaring it in the
89  * DPL file, or by calling this function.
90  *
91  * The function accepts an authentication token of a parent
92  * container that this object should be assigned to. The token
93  * can be '0' so the object will be assigned to the default container.
94  * The newly created object can be opened with the returned
95  * object id and using the container's associated tokens and MC portals.
96  *
97  * Return:	'0' on Success; Error code otherwise.
98  */
dpni_create(struct fsl_mc_io * mc_io,u16 dprc_token,u32 cmd_flags,const struct dpni_cfg * cfg,u32 * obj_id)99 int dpni_create(struct fsl_mc_io *mc_io, u16 dprc_token, u32 cmd_flags,
100 		const struct dpni_cfg *cfg, u32 *obj_id)
101 {
102 	struct dpni_cmd_create *cmd_params;
103 	struct mc_command cmd = { 0 };
104 	int err;
105 
106 	/* prepare command */
107 	cmd.header = mc_encode_cmd_header(DPNI_CMDID_CREATE,
108 					  cmd_flags,
109 					  dprc_token);
110 	cmd_params = (struct dpni_cmd_create *)cmd.params;
111 	cmd_params->options = cpu_to_le32(cfg->options);
112 	cmd_params->num_queues = cfg->num_queues;
113 	cmd_params->num_tcs = cfg->num_tcs;
114 	cmd_params->mac_filter_entries = cfg->mac_filter_entries;
115 	cmd_params->num_rx_tcs = cfg->num_rx_tcs;
116 	cmd_params->vlan_filter_entries =  cfg->vlan_filter_entries;
117 	cmd_params->qos_entries = cfg->qos_entries;
118 	cmd_params->fs_entries = cpu_to_le16(cfg->fs_entries);
119 	cmd_params->num_cgs = cfg->num_cgs;
120 	cmd_params->num_opr = cfg->num_opr;
121 	cmd_params->dist_key_size = cfg->dist_key_size;
122 	cmd_params->num_channels = cfg->num_channels;
123 
124 	/* send command to mc*/
125 	err = mc_send_command(mc_io, &cmd);
126 	if (err)
127 		return err;
128 
129 	/* retrieve response parameters */
130 	*obj_id = mc_cmd_read_object_id(&cmd);
131 
132 	return 0;
133 }
134 
135 /**
136  * dpni_destroy() - Destroy the DPNI object and release all its resources.
137  * @mc_io:	Pointer to MC portal's I/O object
138  * @dprc_token: Parent container token; '0' for default container
139  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
140  * @object_id:	The object id; it must be a valid id within the container that
141  * created this object;
142  *
143  * The function accepts the authentication token of the parent container that
144  * created the object (not the one that currently owns the object). The object
145  * is searched within parent using the provided 'object_id'.
146  * All tokens to the object must be closed before calling destroy.
147  *
148  * Return:	'0' on Success; error code otherwise.
149  */
dpni_destroy(struct fsl_mc_io * mc_io,u16 dprc_token,u32 cmd_flags,u32 object_id)150 int dpni_destroy(struct fsl_mc_io *mc_io, u16 dprc_token, u32 cmd_flags,
151 		 u32 object_id)
152 {
153 	struct dpni_cmd_destroy *cmd_params;
154 	struct mc_command cmd = { 0 };
155 
156 	/* prepare command */
157 	cmd.header = mc_encode_cmd_header(DPNI_CMDID_DESTROY,
158 					  cmd_flags,
159 					  dprc_token);
160 	/* set object id to destroy */
161 	cmd_params = (struct dpni_cmd_destroy *)cmd.params;
162 	cmd_params->dpni_id = cpu_to_le32(object_id);
163 
164 	/* send command to mc*/
165 	return mc_send_command(mc_io, &cmd);
166 }
167 
168 /**
169  * dpni_set_pools() - Set buffer pools configuration
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 DPNI object
173  * @cfg:	Buffer pools configuration
174  *
175  * mandatory for DPNI operation
176  * warning:Allowed only when DPNI is disabled
177  *
178  * Return:	'0' on Success; Error code otherwise.
179  */
dpni_set_pools(struct fsl_mc_io * mc_io,u32 cmd_flags,u16 token,const struct dpni_pools_cfg * cfg)180 int dpni_set_pools(struct fsl_mc_io *mc_io, u32 cmd_flags, u16 token,
181 		   const struct dpni_pools_cfg *cfg)
182 {
183 	struct mc_command cmd = { 0 };
184 	struct dpni_cmd_set_pools *cmd_params;
185 	int i;
186 
187 	/* prepare command */
188 	cmd.header = mc_encode_cmd_header(DPNI_CMDID_SET_POOLS,
189 					  cmd_flags,
190 					  token);
191 	cmd_params = (struct dpni_cmd_set_pools *)cmd.params;
192 	cmd_params->num_dpbp = cfg->num_dpbp;
193 	cmd_params->pool_options = cfg->pool_options;
194 	for (i = 0; i < DPNI_MAX_DPBP; i++) {
195 		cmd_params->pool[i].dpbp_id =
196 			cpu_to_le16(cfg->pools[i].dpbp_id);
197 		cmd_params->pool[i].priority_mask =
198 			cfg->pools[i].priority_mask;
199 		cmd_params->buffer_size[i] =
200 			cpu_to_le16(cfg->pools[i].buffer_size);
201 		cmd_params->backup_pool_mask |=
202 			DPNI_BACKUP_POOL(cfg->pools[i].backup_pool, i);
203 	}
204 
205 	/* send command to mc*/
206 	return mc_send_command(mc_io, &cmd);
207 }
208 
209 /**
210  * dpni_enable() - Enable the DPNI, allow sending and receiving frames.
211  * @mc_io:	Pointer to MC portal's I/O object
212  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
213  * @token:	Token of DPNI object
214  *
215  * Return:	'0' on Success; Error code otherwise.
216  */
dpni_enable(struct fsl_mc_io * mc_io,u32 cmd_flags,u16 token)217 int dpni_enable(struct fsl_mc_io *mc_io, u32 cmd_flags, u16 token)
218 {
219 	struct mc_command cmd = { 0 };
220 
221 	/* prepare command */
222 	cmd.header = mc_encode_cmd_header(DPNI_CMDID_ENABLE,
223 					  cmd_flags,
224 					  token);
225 
226 	/* send command to mc*/
227 	return mc_send_command(mc_io, &cmd);
228 }
229 
230 /**
231  * dpni_disable() - Disable the DPNI, stop sending and receiving frames.
232  * @mc_io:	Pointer to MC portal's I/O object
233  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
234  * @token:	Token of DPNI object
235  *
236  * Return:	'0' on Success; Error code otherwise.
237  */
dpni_disable(struct fsl_mc_io * mc_io,u32 cmd_flags,u16 token)238 int dpni_disable(struct fsl_mc_io *mc_io, u32 cmd_flags, u16 token)
239 {
240 	struct mc_command cmd = { 0 };
241 
242 	/* prepare command */
243 	cmd.header = mc_encode_cmd_header(DPNI_CMDID_DISABLE,
244 					  cmd_flags,
245 					  token);
246 
247 	/* send command to mc*/
248 	return mc_send_command(mc_io, &cmd);
249 }
250 
251 /**
252  * dpni_reset() - Reset the DPNI, returns the object to initial state.
253  * @mc_io:	Pointer to MC portal's I/O object
254  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
255  * @token:	Token of DPNI object
256  *
257  * Return:	'0' on Success; Error code otherwise.
258  */
dpni_reset(struct fsl_mc_io * mc_io,u32 cmd_flags,u16 token)259 int dpni_reset(struct fsl_mc_io *mc_io, u32 cmd_flags, u16 token)
260 {
261 	struct mc_command cmd = { 0 };
262 
263 	/* prepare command */
264 	cmd.header = mc_encode_cmd_header(DPNI_CMDID_RESET,
265 					  cmd_flags,
266 					  token);
267 
268 	/* send command to mc*/
269 	return mc_send_command(mc_io, &cmd);
270 }
271 
272 /**
273  * dpni_get_attributes() - Retrieve DPNI attributes.
274  * @mc_io:	Pointer to MC portal's I/O object
275  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
276  * @token:	Token of DPNI object
277  * @attr:	Object's attributes
278  *
279  * Return:	'0' on Success; Error code otherwise.
280  */
dpni_get_attributes(struct fsl_mc_io * mc_io,u32 cmd_flags,u16 token,struct dpni_attr * attr)281 int dpni_get_attributes(struct fsl_mc_io *mc_io, u32 cmd_flags, u16 token,
282 			struct dpni_attr *attr)
283 {
284 	struct mc_command cmd = { 0 };
285 	struct dpni_rsp_get_attr *rsp_params;
286 
287 	int err;
288 
289 	/* prepare command */
290 	cmd.header = mc_encode_cmd_header(DPNI_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 dpni_rsp_get_attr *)cmd.params;
301 	attr->options = le32_to_cpu(rsp_params->options);
302 	attr->num_queues = rsp_params->num_queues;
303 	attr->num_rx_tcs = rsp_params->num_rx_tcs;
304 	attr->num_tx_tcs = rsp_params->num_tx_tcs;
305 	attr->mac_filter_entries = rsp_params->mac_filter_entries;
306 	attr->vlan_filter_entries = rsp_params->vlan_filter_entries;
307 	attr->num_channels = rsp_params->num_channels;
308 	attr->qos_entries = rsp_params->qos_entries;
309 	attr->fs_entries = le16_to_cpu(rsp_params->fs_entries);
310 	attr->num_opr = le16_to_cpu(rsp_params->num_opr);
311 	attr->qos_key_size = rsp_params->qos_key_size;
312 	attr->fs_key_size = rsp_params->fs_key_size;
313 	attr->wriop_version = le16_to_cpu(rsp_params->wriop_version);
314 	attr->num_cgs = rsp_params->num_cgs;
315 
316 	return 0;
317 }
318 
319 /**
320  * dpni_set_buffer_layout() - Set buffer layout configuration.
321  * @mc_io:	Pointer to MC portal's I/O object
322  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
323  * @token:	Token of DPNI object
324  * @qtype:	Type of queue this configuration applies to
325  * @layout:	Buffer layout configuration
326  *
327  * Return:	'0' on Success; Error code otherwise.
328  *
329  * @warning	Allowed only when DPNI is disabled
330  */
dpni_set_buffer_layout(struct fsl_mc_io * mc_io,u32 cmd_flags,u16 token,enum dpni_queue_type qtype,const struct dpni_buffer_layout * layout)331 int dpni_set_buffer_layout(struct fsl_mc_io *mc_io, u32 cmd_flags, u16 token,
332 			   enum dpni_queue_type qtype,
333 			   const struct dpni_buffer_layout *layout)
334 {
335 	struct dpni_cmd_set_buffer_layout *cmd_params;
336 	struct mc_command cmd = { 0 };
337 
338 	/* prepare command */
339 	cmd.header = mc_encode_cmd_header(DPNI_CMDID_SET_BUFFER_LAYOUT,
340 					  cmd_flags,
341 					  token);
342 	cmd_params = (struct dpni_cmd_set_buffer_layout *)cmd.params;
343 	cmd_params->qtype = qtype;
344 	cmd_params->options = cpu_to_le16((u16)layout->options);
345 	dpni_set_field(cmd_params->flags, PASS_TS, layout->pass_timestamp);
346 	dpni_set_field(cmd_params->flags, PASS_PR, layout->pass_parser_result);
347 	dpni_set_field(cmd_params->flags, PASS_FS, layout->pass_frame_status);
348 	dpni_set_field(cmd_params->flags, PASS_SWO, layout->pass_sw_opaque);
349 	cmd_params->private_data_size = cpu_to_le16(layout->private_data_size);
350 	cmd_params->data_align = cpu_to_le16(layout->data_align);
351 	cmd_params->head_room = cpu_to_le16(layout->data_head_room);
352 	cmd_params->tail_room = cpu_to_le16(layout->data_tail_room);
353 
354 	/* send command to mc*/
355 	return mc_send_command(mc_io, &cmd);
356 }
357 
358 /**
359  * dpni_get_qdid() - Get the Queuing Destination ID (QDID) that should be used
360  *			for enqueue operations
361  * @mc_io:	Pointer to MC portal's I/O object
362  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
363  * @token:	Token of DPNI object
364  * @qtype:	Type of queue to receive QDID for
365  * @qdid:	Returned virtual QDID value that should be used as an argument
366  *			in all enqueue operations
367  *
368  * Return:	'0' on Success; Error code otherwise.
369  *
370  * If dpni object is created using multiple Tc channels this function will return
371  * qdid value for the first channel
372  */
dpni_get_qdid(struct fsl_mc_io * mc_io,u32 cmd_flags,u16 token,enum dpni_queue_type qtype,u16 * qdid)373 int dpni_get_qdid(struct fsl_mc_io *mc_io, u32 cmd_flags, u16 token,
374 		  enum dpni_queue_type qtype, u16 *qdid)
375 {
376 	struct mc_command cmd = { 0 };
377 	struct dpni_cmd_get_qdid *cmd_params;
378 	struct dpni_rsp_get_qdid *rsp_params;
379 	int err;
380 
381 	/* prepare command */
382 	cmd.header = mc_encode_cmd_header(DPNI_CMDID_GET_QDID,
383 					  cmd_flags,
384 					  token);
385 	cmd_params = (struct dpni_cmd_get_qdid *)cmd.params;
386 	cmd_params->qtype = qtype;
387 
388 	/* send command to mc*/
389 	err = mc_send_command(mc_io, &cmd);
390 	if (err)
391 		return err;
392 
393 	/* retrieve response parameters */
394 	rsp_params = (struct dpni_rsp_get_qdid *)cmd.params;
395 	*qdid = le16_to_cpu(rsp_params->qdid);
396 
397 	return 0;
398 }
399 
400 /**
401  * dpni_get_tx_data_offset() - Get the Tx data offset (from start of buffer)
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 DPNI object
405  * @data_offset: Tx data offset (from start of buffer)
406  *
407  * Return:	'0' on Success; Error code otherwise.
408  */
dpni_get_tx_data_offset(struct fsl_mc_io * mc_io,u32 cmd_flags,u16 token,u16 * data_offset)409 int dpni_get_tx_data_offset(struct fsl_mc_io *mc_io, u32 cmd_flags, u16 token,
410 			    u16 *data_offset)
411 {
412 	struct mc_command cmd = { 0 };
413 	struct dpni_rsp_get_tx_data_offset *rsp_params;
414 	int err;
415 
416 	/* prepare command */
417 	cmd.header = mc_encode_cmd_header(DPNI_CMDID_GET_TX_DATA_OFFSET,
418 					  cmd_flags,
419 					  token);
420 
421 	/* send command to mc*/
422 	err = mc_send_command(mc_io, &cmd);
423 	if (err)
424 		return err;
425 
426 	/* retrieve response parameters */
427 	rsp_params = (struct dpni_rsp_get_tx_data_offset *)cmd.params;
428 	*data_offset = le16_to_cpu(rsp_params->data_offset);
429 
430 	return 0;
431 }
432 
433 /**
434  * dpni_set_link_cfg() - set the link configuration.
435  * @mc_io:	Pointer to MC portal's I/O object
436  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
437  * @token:	Token of DPNI object
438  * @cfg:	Link configuration
439  *
440  * Return:	'0' on Success; Error code otherwise.
441  */
dpni_set_link_cfg(struct fsl_mc_io * mc_io,u32 cmd_flags,u16 token,const struct dpni_link_cfg * cfg)442 int dpni_set_link_cfg(struct fsl_mc_io *mc_io, u32 cmd_flags, u16 token,
443 		      const struct dpni_link_cfg *cfg)
444 {
445 	struct mc_command cmd = { 0 };
446 	struct dpni_cmd_set_link_cfg *cmd_params;
447 
448 	/* prepare command */
449 	cmd.header = mc_encode_cmd_header(DPNI_CMDID_SET_LINK_CFG,
450 					  cmd_flags,
451 					  token);
452 	cmd_params = (struct dpni_cmd_set_link_cfg *)cmd.params;
453 	cmd_params->rate = cpu_to_le32(cfg->rate);
454 	cmd_params->options = cpu_to_le64(cfg->options);
455 	cmd_params->advertising = cpu_to_le64(cfg->advertising);
456 
457 	/* send command to mc*/
458 	return mc_send_command(mc_io, &cmd);
459 }
460 
461 /**
462  * dpni_get_link_state() - Return the link state (either up or down)
463  * @mc_io:	Pointer to MC portal's I/O object
464  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
465  * @token:	Token of DPNI object
466  * @state:	Returned link state;
467  *
468  * Return:	'0' on Success; Error code otherwise.
469  */
dpni_get_link_state(struct fsl_mc_io * mc_io,u32 cmd_flags,u16 token,struct dpni_link_state * state)470 int dpni_get_link_state(struct fsl_mc_io *mc_io, u32 cmd_flags, u16 token,
471 			struct dpni_link_state *state)
472 {
473 	struct mc_command cmd = { 0 };
474 	struct dpni_rsp_get_link_state *rsp_params;
475 	int err;
476 
477 	/* prepare command */
478 	cmd.header = mc_encode_cmd_header(DPNI_CMDID_GET_LINK_STATE,
479 					  cmd_flags,
480 					  token);
481 
482 	/* send command to mc*/
483 	err = mc_send_command(mc_io, &cmd);
484 	if (err)
485 		return err;
486 
487 	/* retrieve response parameters */
488 	rsp_params = (struct dpni_rsp_get_link_state *)cmd.params;
489 	state->up = dpni_get_field(rsp_params->flags, LINK_STATE);
490 	state->state_valid = dpni_get_field(rsp_params->flags, STATE_VALID);
491 	state->rate = le32_to_cpu(rsp_params->rate);
492 	state->options = le64_to_cpu(rsp_params->options);
493 	state->supported = le64_to_cpu(rsp_params->supported);
494 	state->advertising = le64_to_cpu(rsp_params->advertising);
495 
496 	return 0;
497 }
498 
499 /**
500  * dpni_add_mac_addr() - Add MAC address filter
501  * @mc_io:	Pointer to MC portal's I/O object
502  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
503  * @token:	Token of DPNI object
504  * @mac_addr:	MAC address to add
505  * @flags :0 - tc_id and flow_id will be ignored.
506  *				 Pkt with this mac_id will be passed to the next
507  *				 classification stages
508  *          DPNI_MAC_SET_QUEUE_ACTION
509  *				 Pkt with this mac will be forward directly to
510  *				 queue defined by the tc_id and flow_id
511  * @tc_id : Traffic class selection (0-7)
512  * @flow_id : Selects the specific queue out of the set allocated for the
513  *            same as tc_id. Value must be in range 0 to NUM_QUEUES - 1
514  * Return:	'0' on Success; Error code otherwise.
515  */
dpni_add_mac_addr(struct fsl_mc_io * mc_io,u32 cmd_flags,u16 token,const u8 mac_addr[6],u8 flags,u8 tc_id,u8 flow_id)516 int dpni_add_mac_addr(struct fsl_mc_io *mc_io, u32 cmd_flags, u16 token,
517 		      const u8 mac_addr[6], u8 flags,
518 		      u8 tc_id, u8 flow_id)
519 {
520 	struct dpni_cmd_add_mac_addr *cmd_params;
521 	struct mc_command cmd = { 0 };
522 	int i;
523 
524 	/* prepare command */
525 	cmd.header = mc_encode_cmd_header(DPNI_CMDID_ADD_MAC_ADDR,
526 					  cmd_flags,
527 					  token);
528 	cmd_params = (struct dpni_cmd_add_mac_addr *)cmd.params;
529 	cmd_params->flags = flags;
530 	cmd_params->tc_id = tc_id;
531 	cmd_params->fq_id = flow_id;
532 
533 	for (i = 0; i < 6; i++)
534 		cmd_params->mac_addr[i] = mac_addr[5 - i];
535 
536 	/* send command to mc*/
537 	return mc_send_command(mc_io, &cmd);
538 }
539 
540 /**
541  * dpni_get_api_version() - Get Data Path Network Interface API version
542  * @mc_io:  Pointer to MC portal's I/O object
543  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
544  * @major_ver:	Major version of data path network interface API
545  * @minor_ver:	Minor version of data path network interface API
546  *
547  * Return:  '0' on Success; Error code otherwise.
548  */
dpni_get_api_version(struct fsl_mc_io * mc_io,u32 cmd_flags,u16 * major_ver,u16 * minor_ver)549 int dpni_get_api_version(struct fsl_mc_io *mc_io, u32 cmd_flags,
550 			 u16 *major_ver, u16 *minor_ver)
551 {
552 	struct mc_command cmd = { 0 };
553 	int err;
554 
555 	cmd.header = mc_encode_cmd_header(DPNI_CMDID_GET_API_VERSION,
556 					cmd_flags,
557 					0);
558 
559 	err = mc_send_command(mc_io, &cmd);
560 	if (err)
561 		return err;
562 
563 	mc_cmd_read_api_version(&cmd, major_ver, minor_ver);
564 
565 	return 0;
566 }
567 
568 /**
569  * dpni_set_queue() - Set queue parameters
570  * @mc_io:	Pointer to MC portal's I/O object
571  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
572  * @token:	Token of DPNI object
573  * @qtype:	Type of queue - all queue types are supported, although
574  *		the command is ignored for Tx
575  * @tc:		Traffic class, in range 0 to NUM_TCS - 1
576  * @index:	Selects the specific queue out of the set allocated for the
577  *		same TC. Value must be in range 0 to NUM_QUEUES - 1
578  * @options:	A combination of DPNI_QUEUE_OPT_ values that control what
579  *		configuration options are set on the queue
580  * @queue:	Queue structure
581  *
582  * Return:	'0' on Success; Error code otherwise.
583  */
dpni_set_queue(struct fsl_mc_io * mc_io,u32 cmd_flags,u16 token,enum dpni_queue_type qtype,u16 param,u8 index,u8 options,const struct dpni_queue * queue)584 int dpni_set_queue(struct fsl_mc_io *mc_io, u32 cmd_flags, u16 token,
585 		   enum dpni_queue_type qtype, u16 param, u8 index,
586 		   u8 options, const struct dpni_queue *queue)
587 {
588 	struct mc_command cmd = { 0 };
589 	struct dpni_cmd_set_queue *cmd_params;
590 
591 	/* prepare command */
592 	cmd.header = mc_encode_cmd_header(DPNI_CMDID_SET_QUEUE,
593 					  cmd_flags,
594 					  token);
595 	cmd_params = (struct dpni_cmd_set_queue *)cmd.params;
596 	cmd_params->qtype = qtype;
597 	cmd_params->tc = (u8)(param & 0xff);
598 	cmd_params->channel_id = (u8)((param >> 8) & 0xff);
599 	cmd_params->index = index;
600 	cmd_params->options = options;
601 	cmd_params->dest_id = cpu_to_le32(queue->destination.id);
602 	cmd_params->dest_prio = queue->destination.priority;
603 	dpni_set_field(cmd_params->flags, DEST_TYPE, queue->destination.type);
604 	dpni_set_field(cmd_params->flags, STASH_CTRL, queue->flc.stash_control);
605 	dpni_set_field(cmd_params->flags, HOLD_ACTIVE,
606 		       queue->destination.hold_active);
607 	cmd_params->flc = cpu_to_le64(queue->flc.value);
608 	cmd_params->user_context = cpu_to_le64(queue->user_context);
609 	cmd_params->cgid = queue->cgid;
610 
611 	/* send command to mc */
612 	return mc_send_command(mc_io, &cmd);
613 }
614 
615 /**
616  * dpni_get_queue() - Get queue parameters
617  * @mc_io:	Pointer to MC portal's I/O object
618  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
619  * @token:	Token of DPNI object
620  * @qtype:	Type of queue - all queue types are supported
621  * @param:	Traffic class and channel ID.
622  *			MSB - channel id; used only for DPNI_QUEUE_TX and
623  *			DPNI_QUEUE_TX_CONFIRM, ignored for the rest
624  *			LSB - traffic class
625  *			Use macro DPNI_BUILD_PARAM() to build correct value.
626  *			If dpni uses a single channel (uses only channel zero)
627  *			the parameter can receive traffic class directly.
628  * @index:	Selects the specific queue out of the set allocated for the
629  *		same TC. Value must be in range 0 to NUM_QUEUES - 1
630  * @queue:	Queue configuration structure
631  * @qid:	Queue identification
632  *
633  * Return:	'0' on Success; Error code otherwise.
634  */
dpni_get_queue(struct fsl_mc_io * mc_io,u32 cmd_flags,u16 token,enum dpni_queue_type qtype,u16 param,u8 index,struct dpni_queue * queue,struct dpni_queue_id * qid)635 int dpni_get_queue(struct fsl_mc_io *mc_io, u32 cmd_flags, u16 token,
636 		   enum dpni_queue_type qtype, u16 param, u8 index,
637 		   struct dpni_queue *queue, struct dpni_queue_id *qid)
638 {
639 	struct mc_command cmd = { 0 };
640 	struct dpni_cmd_get_queue *cmd_params;
641 	struct dpni_rsp_get_queue *rsp_params;
642 	int err;
643 
644 	/* prepare command */
645 	cmd.header = mc_encode_cmd_header(DPNI_CMDID_GET_QUEUE,
646 					  cmd_flags,
647 					  token);
648 	cmd_params = (struct dpni_cmd_get_queue *)cmd.params;
649 	cmd_params->qtype = qtype;
650 	cmd_params->tc = (u8)(param & 0xff);
651 	cmd_params->index = index;
652 	cmd_params->channel_id = (u8)((param >> 8) & 0xff);
653 
654 	/* send command to mc */
655 	err = mc_send_command(mc_io, &cmd);
656 	if (err)
657 		return err;
658 
659 	/* retrieve response parameters */
660 	rsp_params = (struct dpni_rsp_get_queue *)cmd.params;
661 	queue->destination.id = le32_to_cpu(rsp_params->dest_id);
662 	queue->destination.priority = rsp_params->dest_prio;
663 	queue->destination.type = dpni_get_field(rsp_params->flags, DEST_TYPE);
664 	queue->flc.stash_control = dpni_get_field(rsp_params->flags, STASH_CTRL);
665 	queue->destination.hold_active = dpni_get_field(rsp_params->flags, HOLD_ACTIVE);
666 	queue->flc.value = le64_to_cpu(rsp_params->flc);
667 	queue->user_context = le64_to_cpu(rsp_params->user_context);
668 	qid->fqid = le32_to_cpu(rsp_params->fqid);
669 	qid->qdbin = le16_to_cpu(rsp_params->qdbin);
670 	if (dpni_get_field(rsp_params->flags, CGID_VALID))
671 		queue->cgid = rsp_params->cgid;
672 	else
673 		queue->cgid = -1;
674 
675 	return 0;
676 }
677 
678 /**
679  * dpni_set_tx_confirmation_mode() - Tx confirmation mode
680  * @mc_io:	Pointer to MC portal's I/O object
681  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
682  * @token:	Token of DPNI object
683  * @ceetm_ch_idx:	ceetm channel index
684  * @mode:	Tx confirmation mode
685  *
686  * This function is useful only when 'DPNI_OPT_TX_CONF_DISABLED' is not
687  * selected at DPNI creation.
688  * Calling this function with 'mode' set to DPNI_CONF_DISABLE disables all
689  * transmit confirmation (including the private confirmation queues), regardless
690  * of previous settings; Note that in this case, Tx error frames are still
691  * enqueued to the general transmit errors queue.
692  * Calling this function with 'mode' set to DPNI_CONF_SINGLE switches all
693  * Tx confirmations to a shared Tx conf queue. 'index' field in dpni_get_queue
694  * command will be ignored.
695  *
696  * Return:	'0' on Success; Error code otherwise.
697  */
dpni_set_tx_confirmation_mode(struct fsl_mc_io * mc_io,u32 cmd_flags,u16 token,u8 ceetm_ch_idx,enum dpni_confirmation_mode mode)698 int dpni_set_tx_confirmation_mode(struct fsl_mc_io *mc_io, u32 cmd_flags, u16 token,
699 				  u8 ceetm_ch_idx, enum dpni_confirmation_mode mode)
700 {
701 	struct dpni_tx_confirmation_mode *cmd_params;
702 	struct mc_command cmd = { 0 };
703 
704 	/* prepare command */
705 	cmd.header = mc_encode_cmd_header(DPNI_CMDID_SET_TX_CONFIRMATION_MODE,
706 					  cmd_flags, token);
707 	cmd_params = (struct dpni_tx_confirmation_mode *)cmd.params;
708 	cmd_params->ceetm_ch_idx = ceetm_ch_idx;
709 	cmd_params->confirmation_mode = mode;
710 
711 	/* send command to mc*/
712 	return mc_send_command(mc_io, &cmd);
713 }
714 
715 /**
716  * dpni_get_statistics() - Get DPNI statistics
717  * @mc_io:	Pointer to MC portal's I/O object
718  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
719  * @token:	Token of DPNI object
720  * @page:	Selects the statistics page to retrieve, see
721  *		DPNI_GET_STATISTICS output. Pages are numbered 0 to 6.
722  * @param:  Custom parameter for some pages used to select
723  *		 a certain statistic source, for example the TC.
724  *		 - page_0: not used
725  *		 - page_1: not used
726  *		 - page_2: not used
727  *		 - page_3: high_byte - channel_id, low_byte - traffic class
728  *		 - page_4: high_byte - queue_index have meaning only if dpni is
729  *		 created using option DPNI_OPT_CUSTOM_CG, low_byte - traffic class
730  *		 - page_5: not used
731  *		 - page_6: not used
732  * @stat:	Structure containing the statistics
733  *
734  * Return:	'0' on Success; Error code otherwise.
735  */
dpni_get_statistics(struct fsl_mc_io * mc_io,u32 cmd_flags,u16 token,u8 page,u16 param,union dpni_statistics * stat)736 int dpni_get_statistics(struct fsl_mc_io *mc_io, u32 cmd_flags, u16 token,
737 			u8 page, u16 param, union dpni_statistics *stat)
738 {
739 	struct dpni_cmd_get_statistics *cmd_params;
740 	struct dpni_rsp_get_statistics *rsp_params;
741 	struct mc_command cmd = { 0 };
742 	int i, err;
743 
744 	/* prepare command */
745 	cmd.header = mc_encode_cmd_header(DPNI_CMDID_GET_STATISTICS,
746 					  cmd_flags,
747 					  token);
748 	cmd_params = (struct dpni_cmd_get_statistics *)cmd.params;
749 	cmd_params->page_number = page;
750 	cmd_params->param = param;
751 
752 	/* send command to mc */
753 	err = mc_send_command(mc_io, &cmd);
754 	if (err)
755 		return err;
756 
757 	/* retrieve response parameters */
758 	rsp_params = (struct dpni_rsp_get_statistics *)cmd.params;
759 	for (i = 0; i < DPNI_STATISTICS_CNT; i++)
760 		stat->raw.counter[i] = le64_to_cpu(rsp_params->counter[i]);
761 
762 	return 0;
763 }
764