1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Freescale Layerscape MC I/O wrapper
4  *
5  * Copyright 2013-2016 Freescale Semiconductor, Inc.
6  * Copyright 2017-2023 NXP
7  */
8 #include <fsl-mc/fsl_mc_sys.h>
9 #include <fsl-mc/fsl_mc_cmd.h>
10 #include <fsl-mc/fsl_dpbp.h>
11 
12 /**
13  * dpbp_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  * @dpbp_id:	DPBP 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 dpbp_create function.
22  * This function returns a unique authentication token,
23  * associated with the specific object ID and the specific MC
24  * portal; this token must be used in all subsequent commands for
25  * this specific object
26  *
27  * Return:	'0' on Success; Error code otherwise.
28  */
dpbp_open(struct fsl_mc_io * mc_io,u32 cmd_flags,int dpbp_id,u16 * token)29 int dpbp_open(struct fsl_mc_io *mc_io, u32 cmd_flags, int dpbp_id, u16 *token)
30 {
31 	struct dpbp_cmd_open *cmd_params;
32 	struct mc_command cmd = { 0 };
33 	int err;
34 
35 	/* prepare command */
36 	cmd.header = mc_encode_cmd_header(DPBP_CMDID_OPEN,
37 					  cmd_flags, 0);
38 	cmd_params = (struct dpbp_cmd_open *)cmd.params;
39 	cmd_params->dpbp_id = cpu_to_le32(dpbp_id);
40 
41 	/* send command to mc*/
42 	err = mc_send_command(mc_io, &cmd);
43 	if (err)
44 		return err;
45 
46 	/* retrieve response parameters */
47 	*token = mc_cmd_hdr_read_token(&cmd);
48 
49 	return err;
50 }
51 
52 /**
53  * dpbp_close() - Close the control session of the object
54  * @mc_io:	Pointer to MC portal's I/O object
55  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
56  * @token:	Token of DPBP object
57  *
58  * After this function is called, no further operations are
59  * allowed on the object without opening a new control session.
60  *
61  * Return:	'0' on Success; Error code otherwise.
62  */
dpbp_close(struct fsl_mc_io * mc_io,u32 cmd_flags,u16 token)63 int dpbp_close(struct fsl_mc_io *mc_io, u32 cmd_flags, u16 token)
64 {
65 	struct mc_command cmd = { 0 };
66 
67 	/* prepare command */
68 	cmd.header = mc_encode_cmd_header(DPBP_CMDID_CLOSE, cmd_flags,
69 					  token);
70 
71 	/* send command to mc*/
72 	return mc_send_command(mc_io, &cmd);
73 }
74 
75 /**
76  * dpbp_create() - Create the DPBP object.
77  * @mc_io:	Pointer to MC portal's I/O object
78  * @dprc_token:	Parent container token; '0' for default container
79  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
80  * @cfg:	Configuration structure
81  * @obj_id:	Returned object id; use in subsequent API calls
82  *
83  * Create the DPBP object, allocate required resources and
84  * perform required initialization.
85  *
86  * This function accepts an authentication token of a parent
87  * container that this object should be assigned to and returns
88  * an object id. This object_id will be used in all subsequent calls to
89  * this specific object.
90  *
91  * Return:	'0' on Success; Error code otherwise.
92  */
dpbp_create(struct fsl_mc_io * mc_io,u16 dprc_token,u32 cmd_flags,const struct dpbp_cfg * cfg,u32 * obj_id)93 int dpbp_create(struct fsl_mc_io *mc_io, u16 dprc_token, u32 cmd_flags,
94 		const struct dpbp_cfg *cfg, u32 *obj_id)
95 {
96 	struct mc_command cmd = { 0 };
97 	int err;
98 
99 	(void)(cfg); /* unused */
100 
101 	/* prepare command */
102 	cmd.header = mc_encode_cmd_header(DPBP_CMDID_CREATE,
103 					  cmd_flags, dprc_token);
104 
105 	/* send command to mc*/
106 	err = mc_send_command(mc_io, &cmd);
107 	if (err)
108 		return err;
109 
110 	/* retrieve response parameters */
111 	*obj_id = mc_cmd_read_object_id(&cmd);
112 
113 	return 0;
114 }
115 
116 /**
117  * dpbp_destroy() - Destroy the DPBP object and release all its resources.
118  * @mc_io:	Pointer to MC portal's I/O object
119  * @dprc_token:	Parent container token; '0' for default container
120  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
121  * @obj_id:	ID of DPBP object
122  *
123  * Return:	'0' on Success; error code otherwise.
124  */
dpbp_destroy(struct fsl_mc_io * mc_io,u16 dprc_token,u32 cmd_flags,u32 obj_id)125 int dpbp_destroy(struct fsl_mc_io *mc_io, u16 dprc_token, u32 cmd_flags,
126 		 u32 obj_id)
127 {
128 	struct dpbp_cmd_destroy *cmd_params;
129 	struct mc_command cmd = { 0 };
130 
131 	/* prepare command */
132 	cmd.header = mc_encode_cmd_header(DPBP_CMDID_DESTROY,
133 					  cmd_flags, dprc_token);
134 
135 	cmd_params = (struct dpbp_cmd_destroy *)cmd.params;
136 	cmd_params->object_id = cpu_to_le32(obj_id);
137 
138 	/* send command to mc*/
139 	return mc_send_command(mc_io, &cmd);
140 }
141 
142 /**
143  * dpbp_enable() - Enable the DPBP.
144  * @mc_io:	Pointer to MC portal's I/O object
145  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
146  * @token:	Token of DPBP object
147  *
148  * Return:	'0' on Success; Error code otherwise.
149  */
dpbp_enable(struct fsl_mc_io * mc_io,u32 cmd_flags,u16 token)150 int dpbp_enable(struct fsl_mc_io *mc_io, u32 cmd_flags, u16 token)
151 {
152 	struct mc_command cmd = { 0 };
153 
154 	/* prepare command */
155 	cmd.header = mc_encode_cmd_header(DPBP_CMDID_ENABLE, cmd_flags,
156 					  token);
157 
158 	/* send command to mc*/
159 	return mc_send_command(mc_io, &cmd);
160 }
161 
162 /**
163  * dpbp_disable() - Disable the DPBP.
164  * @mc_io:	Pointer to MC portal's I/O object
165  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
166  * @token:	Token of DPBP object
167  *
168  * Return:	'0' on Success; Error code otherwise.
169  */
dpbp_disable(struct fsl_mc_io * mc_io,u32 cmd_flags,u16 token)170 int dpbp_disable(struct fsl_mc_io *mc_io, u32 cmd_flags, u16 token)
171 {
172 	struct mc_command cmd = { 0 };
173 
174 	/* prepare command */
175 	cmd.header = mc_encode_cmd_header(DPBP_CMDID_DISABLE,
176 					  cmd_flags, token);
177 
178 	/* send command to mc*/
179 	return mc_send_command(mc_io, &cmd);
180 }
181 
182 /**
183  * dpbp_reset() - Reset the DPBP, returns the object to initial state.
184  * @mc_io:	Pointer to MC portal's I/O object
185  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
186  * @token:	Token of DPBP object
187  *
188  * Return:	'0' on Success; Error code otherwise.
189  */
dpbp_reset(struct fsl_mc_io * mc_io,u32 cmd_flags,u16 token)190 int dpbp_reset(struct fsl_mc_io *mc_io, u32 cmd_flags, u16 token)
191 {
192 	struct mc_command cmd = { 0 };
193 
194 	/* prepare command */
195 	cmd.header = mc_encode_cmd_header(DPBP_CMDID_RESET,
196 					  cmd_flags, token);
197 
198 	/* send command to mc*/
199 	return mc_send_command(mc_io, &cmd);
200 }
201 
202 /**
203  * dpbp_get_attributes - Retrieve DPBP attributes.
204  *
205  * @mc_io:	Pointer to MC portal's I/O object
206  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
207  * @token:	Token of DPBP object
208  * @attr:	Returned object's attributes
209  *
210  * Return:	'0' on Success; Error code otherwise.
211  */
dpbp_get_attributes(struct fsl_mc_io * mc_io,u32 cmd_flags,u16 token,struct dpbp_attr * attr)212 int dpbp_get_attributes(struct fsl_mc_io *mc_io, u32 cmd_flags, u16 token,
213 			struct dpbp_attr *attr)
214 {
215 	struct dpbp_rsp_get_attributes *rsp_params;
216 	struct mc_command cmd = { 0 };
217 	int err;
218 
219 	/* prepare command */
220 	cmd.header = mc_encode_cmd_header(DPBP_CMDID_GET_ATTR,
221 					  cmd_flags, token);
222 
223 	/* send command to mc*/
224 	err = mc_send_command(mc_io, &cmd);
225 	if (err)
226 		return err;
227 
228 	/* retrieve response parameters */
229 	rsp_params = (struct dpbp_rsp_get_attributes *)cmd.params;
230 	attr->bpid = le16_to_cpu(rsp_params->bpid);
231 	attr->id = le32_to_cpu(rsp_params->id);
232 
233 	return 0;
234 }
235 
236 /**
237  * dpbp_get_api_version - Get Data Path Buffer Pool API version
238  * @mc_io:	Pointer to Mc portal's I/O object
239  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
240  * @major_ver:	Major version of Buffer Pool API
241  * @minor_ver:	Minor version of Buffer Pool API
242  *
243  * Return:	'0' on Success; Error code otherwise.
244  */
dpbp_get_api_version(struct fsl_mc_io * mc_io,u32 cmd_flags,u16 * major_ver,u16 * minor_ver)245 int dpbp_get_api_version(struct fsl_mc_io *mc_io, u32 cmd_flags,
246 			 u16 *major_ver, u16 *minor_ver)
247 {
248 	struct mc_command cmd = { 0 };
249 	int err;
250 
251 	/* prepare command */
252 	cmd.header = mc_encode_cmd_header(DPBP_CMDID_GET_API_VERSION,
253 					  cmd_flags, 0);
254 
255 	/* send command to mc */
256 	err = mc_send_command(mc_io, &cmd);
257 	if (err)
258 		return err;
259 
260 	/* retrieve response parameters */
261 	mc_cmd_read_api_version(&cmd, major_ver, minor_ver);
262 
263 	return 0;
264 }
265