1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright 2019 NXP
3 
4 #include <linux/module.h>
5 #include <linux/types.h>
6 #include <linux/io.h>
7 #include <linux/fsl/mc.h>
8 #include "dpdmai.h"
9 
10 struct dpdmai_rsp_get_attributes {
11 	__le32 id;
12 	u8 num_of_priorities;
13 	u8 pad0[3];
14 	__le16 major;
15 	__le16 minor;
16 };
17 
18 struct dpdmai_cmd_queue {
19 	__le32 dest_id;
20 	u8 priority;
21 	u8 queue;
22 	u8 dest_type;
23 	u8 pad;
24 	__le64 user_ctx;
25 	union {
26 		__le32 options;
27 		__le32 fqid;
28 	};
29 };
30 
31 struct dpdmai_rsp_get_tx_queue {
32 	__le64 pad;
33 	__le32 fqid;
34 };
35 
36 #define MC_CMD_OP(_cmd, _param, _offset, _width, _type, _arg) \
37 	((_cmd).params[_param] |= mc_enc((_offset), (_width), _arg))
38 
39 /* cmd, param, offset, width, type, arg_name */
40 #define DPDMAI_CMD_CREATE(cmd, cfg) \
41 do { \
42 	MC_CMD_OP(cmd, 0, 8,  8,  u8,  (cfg)->priorities[0]);\
43 	MC_CMD_OP(cmd, 0, 16, 8,  u8,  (cfg)->priorities[1]);\
44 } while (0)
45 
46 static inline u64 mc_enc(int lsoffset, int width, u64 val)
47 {
48 	return (val & MAKE_UMASK64(width)) << lsoffset;
49 }
50 
51 /**
52  * dpdmai_open() - Open a control session for the specified object
53  * @mc_io:	Pointer to MC portal's I/O object
54  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
55  * @dpdmai_id:	DPDMAI unique ID
56  * @token:	Returned token; use in subsequent API calls
57  *
58  * This function can be used to open a control session for an
59  * already created object; an object may have been declared in
60  * the DPL or by calling the dpdmai_create() function.
61  * This function returns a unique authentication token,
62  * associated with the specific object ID and the specific MC
63  * portal; this token must be used in all subsequent commands for
64  * this specific object.
65  *
66  * Return:	'0' on Success; Error code otherwise.
67  */
68 int dpdmai_open(struct fsl_mc_io *mc_io, u32 cmd_flags,
69 		int dpdmai_id, u16 *token)
70 {
71 	struct fsl_mc_command cmd = { 0 };
72 	__le64 *cmd_dpdmai_id;
73 	int err;
74 
75 	/* prepare command */
76 	cmd.header = mc_encode_cmd_header(DPDMAI_CMDID_OPEN,
77 					  cmd_flags, 0);
78 
79 	cmd_dpdmai_id = cmd.params;
80 	*cmd_dpdmai_id = cpu_to_le32(dpdmai_id);
81 
82 	/* send command to mc*/
83 	err = mc_send_command(mc_io, &cmd);
84 	if (err)
85 		return err;
86 
87 	/* retrieve response parameters */
88 	*token = mc_cmd_hdr_read_token(&cmd);
89 
90 	return 0;
91 }
92 EXPORT_SYMBOL_GPL(dpdmai_open);
93 
94 /**
95  * dpdmai_close() - Close the control session of the object
96  * @mc_io:	Pointer to MC portal's I/O object
97  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
98  * @token:	Token of DPDMAI object
99  *
100  * After this function is called, no further operations are
101  * allowed on the object without opening a new control session.
102  *
103  * Return:	'0' on Success; Error code otherwise.
104  */
105 int dpdmai_close(struct fsl_mc_io *mc_io, u32 cmd_flags, u16 token)
106 {
107 	struct fsl_mc_command cmd = { 0 };
108 
109 	/* prepare command */
110 	cmd.header = mc_encode_cmd_header(DPDMAI_CMDID_CLOSE,
111 					  cmd_flags, token);
112 
113 	/* send command to mc*/
114 	return mc_send_command(mc_io, &cmd);
115 }
116 EXPORT_SYMBOL_GPL(dpdmai_close);
117 
118 /**
119  * dpdmai_create() - Create the DPDMAI object
120  * @mc_io:	Pointer to MC portal's I/O object
121  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
122  * @cfg:	Configuration structure
123  * @token:	Returned token; use in subsequent API calls
124  *
125  * Create the DPDMAI object, allocate required resources and
126  * perform required initialization.
127  *
128  * The object can be created either by declaring it in the
129  * DPL file, or by calling this function.
130  *
131  * This function returns a unique authentication token,
132  * associated with the specific object ID and the specific MC
133  * portal; this token must be used in all subsequent calls to
134  * this specific object. For objects that are created using the
135  * DPL file, call dpdmai_open() function to get an authentication
136  * token first.
137  *
138  * Return:	'0' on Success; Error code otherwise.
139  */
140 int dpdmai_create(struct fsl_mc_io *mc_io, u32 cmd_flags,
141 		  const struct dpdmai_cfg *cfg, u16 *token)
142 {
143 	struct fsl_mc_command cmd = { 0 };
144 	int err;
145 
146 	/* prepare command */
147 	cmd.header = mc_encode_cmd_header(DPDMAI_CMDID_CREATE,
148 					  cmd_flags, 0);
149 	DPDMAI_CMD_CREATE(cmd, cfg);
150 
151 	/* send command to mc*/
152 	err = mc_send_command(mc_io, &cmd);
153 	if (err)
154 		return err;
155 
156 	/* retrieve response parameters */
157 	*token = mc_cmd_hdr_read_token(&cmd);
158 
159 	return 0;
160 }
161 
162 /**
163  * dpdmai_enable() - Enable the DPDMAI, allow sending and receiving frames.
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 DPDMAI object
167  *
168  * Return:	'0' on Success; Error code otherwise.
169  */
170 int dpdmai_enable(struct fsl_mc_io *mc_io, u32 cmd_flags, u16 token)
171 {
172 	struct fsl_mc_command cmd = { 0 };
173 
174 	/* prepare command */
175 	cmd.header = mc_encode_cmd_header(DPDMAI_CMDID_ENABLE,
176 					  cmd_flags, token);
177 
178 	/* send command to mc*/
179 	return mc_send_command(mc_io, &cmd);
180 }
181 EXPORT_SYMBOL_GPL(dpdmai_enable);
182 
183 /**
184  * dpdmai_disable() - Disable the DPDMAI, stop sending and receiving frames.
185  * @mc_io:	Pointer to MC portal's I/O object
186  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
187  * @token:	Token of DPDMAI object
188  *
189  * Return:	'0' on Success; Error code otherwise.
190  */
191 int dpdmai_disable(struct fsl_mc_io *mc_io, u32 cmd_flags, u16 token)
192 {
193 	struct fsl_mc_command cmd = { 0 };
194 
195 	/* prepare command */
196 	cmd.header = mc_encode_cmd_header(DPDMAI_CMDID_DISABLE,
197 					  cmd_flags, token);
198 
199 	/* send command to mc*/
200 	return mc_send_command(mc_io, &cmd);
201 }
202 EXPORT_SYMBOL_GPL(dpdmai_disable);
203 
204 /**
205  * dpdmai_reset() - Reset the DPDMAI, returns the object to initial state.
206  * @mc_io:	Pointer to MC portal's I/O object
207  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
208  * @token:	Token of DPDMAI object
209  *
210  * Return:	'0' on Success; Error code otherwise.
211  */
212 int dpdmai_reset(struct fsl_mc_io *mc_io, u32 cmd_flags, u16 token)
213 {
214 	struct fsl_mc_command cmd = { 0 };
215 
216 	/* prepare command */
217 	cmd.header = mc_encode_cmd_header(DPDMAI_CMDID_RESET,
218 					  cmd_flags, token);
219 
220 	/* send command to mc*/
221 	return mc_send_command(mc_io, &cmd);
222 }
223 EXPORT_SYMBOL_GPL(dpdmai_reset);
224 
225 /**
226  * dpdmai_get_attributes() - Retrieve DPDMAI attributes.
227  * @mc_io:	Pointer to MC portal's I/O object
228  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
229  * @token:	Token of DPDMAI object
230  * @attr:	Returned object's attributes
231  *
232  * Return:	'0' on Success; Error code otherwise.
233  */
234 int dpdmai_get_attributes(struct fsl_mc_io *mc_io, u32 cmd_flags,
235 			  u16 token, struct dpdmai_attr *attr)
236 {
237 	struct dpdmai_rsp_get_attributes *rsp_params;
238 	struct fsl_mc_command cmd = { 0 };
239 	int err;
240 
241 	/* prepare command */
242 	cmd.header = mc_encode_cmd_header(DPDMAI_CMDID_GET_ATTR,
243 					  cmd_flags, token);
244 
245 	/* send command to mc*/
246 	err = mc_send_command(mc_io, &cmd);
247 	if (err)
248 		return err;
249 
250 	/* retrieve response parameters */
251 	rsp_params = (struct dpdmai_rsp_get_attributes *)cmd.params;
252 	attr->id = le32_to_cpu(rsp_params->id);
253 	attr->version.major = le16_to_cpu(rsp_params->major);
254 	attr->version.minor = le16_to_cpu(rsp_params->minor);
255 	attr->num_of_priorities = rsp_params->num_of_priorities;
256 
257 	return 0;
258 }
259 EXPORT_SYMBOL_GPL(dpdmai_get_attributes);
260 
261 /**
262  * dpdmai_set_rx_queue() - Set Rx queue configuration
263  * @mc_io:	Pointer to MC portal's I/O object
264  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
265  * @token:	Token of DPDMAI object
266  * @priority:	Select the queue relative to number of
267  *		priorities configured at DPDMAI creation
268  * @cfg:	Rx queue configuration
269  *
270  * Return:	'0' on Success; Error code otherwise.
271  */
272 int dpdmai_set_rx_queue(struct fsl_mc_io *mc_io, u32 cmd_flags, u16 token,
273 			u8 priority, const struct dpdmai_rx_queue_cfg *cfg)
274 {
275 	struct dpdmai_cmd_queue *cmd_params;
276 	struct fsl_mc_command cmd = { 0 };
277 
278 	/* prepare command */
279 	cmd.header = mc_encode_cmd_header(DPDMAI_CMDID_SET_RX_QUEUE,
280 					  cmd_flags, token);
281 
282 	cmd_params = (struct dpdmai_cmd_queue *)cmd.params;
283 	cmd_params->dest_id = cpu_to_le32(cfg->dest_cfg.dest_id);
284 	cmd_params->priority = cfg->dest_cfg.priority;
285 	cmd_params->queue = priority;
286 	cmd_params->dest_type = cfg->dest_cfg.dest_type;
287 	cmd_params->user_ctx = cpu_to_le64(cfg->user_ctx);
288 	cmd_params->options = cpu_to_le32(cfg->options);
289 
290 	/* send command to mc*/
291 	return mc_send_command(mc_io, &cmd);
292 }
293 EXPORT_SYMBOL_GPL(dpdmai_set_rx_queue);
294 
295 /**
296  * dpdmai_get_rx_queue() - Retrieve Rx queue attributes.
297  * @mc_io:	Pointer to MC portal's I/O object
298  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
299  * @token:	Token of DPDMAI object
300  * @priority:	Select the queue relative to number of
301  *				priorities configured at DPDMAI creation
302  * @attr:	Returned Rx queue attributes
303  *
304  * Return:	'0' on Success; Error code otherwise.
305  */
306 int dpdmai_get_rx_queue(struct fsl_mc_io *mc_io, u32 cmd_flags, u16 token,
307 			u8 priority, struct dpdmai_rx_queue_attr *attr)
308 {
309 	struct dpdmai_cmd_queue *cmd_params;
310 	struct fsl_mc_command cmd = { 0 };
311 	int err;
312 
313 	/* prepare command */
314 	cmd.header = mc_encode_cmd_header(DPDMAI_CMDID_GET_RX_QUEUE,
315 					  cmd_flags, token);
316 
317 	cmd_params = (struct dpdmai_cmd_queue *)cmd.params;
318 	cmd_params->queue = priority;
319 
320 	/* send command to mc*/
321 	err = mc_send_command(mc_io, &cmd);
322 	if (err)
323 		return err;
324 
325 	/* retrieve response parameters */
326 	attr->dest_cfg.dest_id = le32_to_cpu(cmd_params->dest_id);
327 	attr->dest_cfg.priority = cmd_params->priority;
328 	attr->dest_cfg.dest_type = cmd_params->dest_type;
329 	attr->user_ctx = le64_to_cpu(cmd_params->user_ctx);
330 	attr->fqid = le32_to_cpu(cmd_params->fqid);
331 
332 	return 0;
333 }
334 EXPORT_SYMBOL_GPL(dpdmai_get_rx_queue);
335 
336 /**
337  * dpdmai_get_tx_queue() - Retrieve Tx queue attributes.
338  * @mc_io:	Pointer to MC portal's I/O object
339  * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
340  * @token:	Token of DPDMAI object
341  * @priority:	Select the queue relative to number of
342  *			priorities configured at DPDMAI creation
343  * @fqid:	Returned Tx queue
344  *
345  * Return:	'0' on Success; Error code otherwise.
346  */
347 int dpdmai_get_tx_queue(struct fsl_mc_io *mc_io, u32 cmd_flags,
348 			u16 token, u8 priority, u32 *fqid)
349 {
350 	struct dpdmai_rsp_get_tx_queue *rsp_params;
351 	struct dpdmai_cmd_queue *cmd_params;
352 	struct fsl_mc_command cmd = { 0 };
353 	int err;
354 
355 	/* prepare command */
356 	cmd.header = mc_encode_cmd_header(DPDMAI_CMDID_GET_TX_QUEUE,
357 					  cmd_flags, token);
358 
359 	cmd_params = (struct dpdmai_cmd_queue *)cmd.params;
360 	cmd_params->queue = priority;
361 
362 	/* send command to mc*/
363 	err = mc_send_command(mc_io, &cmd);
364 	if (err)
365 		return err;
366 
367 	/* retrieve response parameters */
368 
369 	rsp_params = (struct dpdmai_rsp_get_tx_queue *)cmd.params;
370 	*fqid = le32_to_cpu(rsp_params->fqid);
371 
372 	return 0;
373 }
374 EXPORT_SYMBOL_GPL(dpdmai_get_tx_queue);
375 
376 MODULE_LICENSE("GPL v2");
377