1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Data Path Soft Parser
4 *
5 * Copyright 2018, 2023 NXP
6 */
7#include <fsl-mc/fsl_mc_sys.h>
8#include <fsl-mc/fsl_mc_cmd.h>
9#include <fsl-mc/fsl_dpsparser.h>
10
11/**
12 * dpsparser_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 * @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 dpsparser_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 */
27int dpsparser_open(struct fsl_mc_io *mc_io, u32 cmd_flags, u16 *token)
28{
29	struct mc_command cmd = { 0 };
30	int err;
31
32	/* prepare command */
33	cmd.header = mc_encode_cmd_header(DPSPARSER_CMDID_OPEN,
34					  cmd_flags,
35					  0);
36
37	/* send command to mc*/
38	err = mc_send_command(mc_io, &cmd);
39	if (err)
40		return err;
41
42	/* retrieve response parameters */
43	*token = mc_cmd_hdr_read_token(&cmd);
44
45	return err;
46}
47
48/**
49 * dpsparser_close() - Close the control session of the object
50 * @mc_io:	Pointer to MC portal's I/O object
51 * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
52 * @token:	Token of DPSPARSER object
53 *
54 * After this function is called, no further operations are
55 * allowed on the object without opening a new control session.
56 *
57 * Return:	'0' on Success; Error code otherwise.
58 */
59int dpsparser_close(struct fsl_mc_io *mc_io, u32 cmd_flags, u16 token)
60{
61	struct mc_command cmd = { 0 };
62
63	/* prepare command */
64	cmd.header = mc_encode_cmd_header(DPSPARSER_CMDID_CLOSE, cmd_flags,
65					  token);
66
67	/* send command to mc*/
68	return mc_send_command(mc_io, &cmd);
69}
70
71/**
72 * dpsparser_create() - Create the DPSPARSER object.
73 * @mc_io:	Pointer to MC portal's I/O object
74 * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
75 * @token:	Returned token; use in subsequent API calls
76 *
77 * Create the DPSPARSER object, allocate required resources and
78 * perform required initialization.
79 *
80 * The object can be created either by declaring it in the
81 * DPL file, or by calling this function.
82 * This function returns a unique authentication token,
83 * associated with the specific object ID and the specific MC
84 * portal; this token must be used in all subsequent calls to
85 * this specific object. For objects that are created using the
86 * DPL file, call dpsparser_open function to get an authentication
87 * token first.
88 *
89 * Return:	'0' on Success; Error code otherwise.
90 */
91int dpsparser_create(struct fsl_mc_io *mc_io, u16 token, u32 cmd_flags,
92		     u32 *obj_id)
93{
94	struct mc_command cmd = { 0 };
95	int err;
96
97	/* prepare command */
98	cmd.header = mc_encode_cmd_header(DPSPARSER_CMDID_CREATE,
99					  cmd_flags,
100					  token);
101
102	/* send command to mc*/
103	err = mc_send_command(mc_io, &cmd);
104	if (err)
105		return err;
106
107	/* retrieve response parameters */
108	*obj_id = mc_cmd_read_object_id(&cmd);
109
110	return 0;
111}
112
113/**
114 * dpsparser_destroy() - Destroy the DPSPARSER object and release all its resources.
115 * @mc_io:	Pointer to MC portal's I/O object
116 * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
117 * @token:	Token of DPSPARSER object
118 *
119 * Return:	'0' on Success; error code otherwise.
120 */
121int dpsparser_destroy(struct fsl_mc_io *mc_io, u16 token, u32 cmd_flags,
122		      u32 obj_id)
123{
124	struct dpsparser_cmd_destroy *cmd_params;
125	struct mc_command cmd = { 0 };
126
127	/* prepare command */
128	cmd.header = mc_encode_cmd_header(DPSPARSER_CMDID_DESTROY,
129					  cmd_flags,
130					  token);
131	cmd_params = (struct dpsparser_cmd_destroy *)cmd.params;
132	cmd_params->dpsparser_id = cpu_to_le32(obj_id);
133
134	/* send command to mc*/
135	return mc_send_command(mc_io, &cmd);
136}
137
138/**
139 * dpsparser_apply_spb() - Applies the Soft Parser Blob loaded at specified address.
140 * @mc_io:	Pointer to MC portal's I/O object
141 * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
142 * @token:	Token of DPSPARSER object
143 * @blob_addr:	Blob loading address
144 * @error: Error reported by MC related to SP Blob parsing and apply
145 *
146 * Return:	'0' on Success; error code otherwise.
147 */
148int dpsparser_apply_spb(struct fsl_mc_io *mc_io, u32 cmd_flags, u16 token,
149			u64 blob_addr, u16 *error)
150{
151	struct dpsparser_rsp_blob_report_error *rsp_params;
152	struct dpsparser_cmd_blob_set_address *cmd_params;
153	struct mc_command cmd = { 0 };
154	int err;
155
156	/* prepare command */
157	cmd.header = mc_encode_cmd_header(DPSPARSER_CMDID_APPLY_SPB,
158					  cmd_flags,
159					  token);
160	cmd_params = (struct dpsparser_cmd_blob_set_address *)cmd.params;
161	cmd_params->blob_addr = cpu_to_le64(blob_addr);
162
163	/* send command to mc*/
164	err = mc_send_command(mc_io, &cmd);
165	if (err)
166		return err;
167
168	/* retrieve response parameters: MC error code */
169	rsp_params = (struct dpsparser_rsp_blob_report_error *)cmd.params;
170	*error = le16_to_cpu(rsp_params->error);
171
172	return 0;
173}
174
175/**
176 * dpsparser_get_api_version - Retrieve DPSPARSER Major and Minor version info.
177 *
178 * @mc_io:	Pointer to MC portal's I/O object
179 * @cmd_flags:	Command flags; one or more of 'MC_CMD_FLAG_'
180 * @major_ver:	DPSPARSER major version
181 * @minor_ver:	DPSPARSER minor version
182 *
183 * Return:	'0' on Success; Error code otherwise.
184 */
185int dpsparser_get_api_version(struct fsl_mc_io *mc_io, u32 cmd_flags,
186			      u16 *major_ver, u16 *minor_ver)
187{
188	struct mc_command cmd = { 0 };
189	int err;
190
191	/* prepare command */
192	cmd.header = mc_encode_cmd_header(DPSPARSER_CMDID_GET_API_VERSION,
193					  cmd_flags, 0);
194
195	/* send command to mc */
196	err = mc_send_command(mc_io, &cmd);
197	if (err)
198		return err;
199
200	/* retrieve response parameters */
201	mc_cmd_read_api_version(&cmd, major_ver, minor_ver);
202
203	return 0;
204}
205