1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35#include <linux/delay.h>
36#include <linux/slab.h>
37#include <linux/ioport.h>
38#include <linux/device.h>
39#include <linux/io.h>
40#include <linux/io-64-nonatomic-hi-lo.h>
41#include "../include/mc.h"
42
43#include "dpmcp.h"
44
45
46
47
48#define MC_CMD_COMPLETION_TIMEOUT_MS 500
49
50
51
52
53
54#define MC_CMD_COMPLETION_POLLING_MIN_SLEEP_USECS 10
55#define MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS 500
56
57static enum mc_cmd_status mc_cmd_hdr_read_status(struct mc_command *cmd)
58{
59 struct mc_cmd_header *hdr = (struct mc_cmd_header *)&cmd->header;
60
61 return (enum mc_cmd_status)hdr->status;
62}
63
64static u16 mc_cmd_hdr_read_cmdid(struct mc_command *cmd)
65{
66 struct mc_cmd_header *hdr = (struct mc_cmd_header *)&cmd->header;
67 u16 cmd_id = le16_to_cpu(hdr->cmd_id);
68
69 return cmd_id;
70}
71
72static int mc_status_to_error(enum mc_cmd_status status)
73{
74 static const int mc_status_to_error_map[] = {
75 [MC_CMD_STATUS_OK] = 0,
76 [MC_CMD_STATUS_AUTH_ERR] = -EACCES,
77 [MC_CMD_STATUS_NO_PRIVILEGE] = -EPERM,
78 [MC_CMD_STATUS_DMA_ERR] = -EIO,
79 [MC_CMD_STATUS_CONFIG_ERR] = -ENXIO,
80 [MC_CMD_STATUS_TIMEOUT] = -ETIMEDOUT,
81 [MC_CMD_STATUS_NO_RESOURCE] = -ENAVAIL,
82 [MC_CMD_STATUS_NO_MEMORY] = -ENOMEM,
83 [MC_CMD_STATUS_BUSY] = -EBUSY,
84 [MC_CMD_STATUS_UNSUPPORTED_OP] = -ENOTSUPP,
85 [MC_CMD_STATUS_INVALID_STATE] = -ENODEV,
86 };
87
88 if (WARN_ON((u32)status >= ARRAY_SIZE(mc_status_to_error_map)))
89 return -EINVAL;
90
91 return mc_status_to_error_map[status];
92}
93
94static const char *mc_status_to_string(enum mc_cmd_status status)
95{
96 static const char *const status_strings[] = {
97 [MC_CMD_STATUS_OK] = "Command completed successfully",
98 [MC_CMD_STATUS_READY] = "Command ready to be processed",
99 [MC_CMD_STATUS_AUTH_ERR] = "Authentication error",
100 [MC_CMD_STATUS_NO_PRIVILEGE] = "No privilege",
101 [MC_CMD_STATUS_DMA_ERR] = "DMA or I/O error",
102 [MC_CMD_STATUS_CONFIG_ERR] = "Configuration error",
103 [MC_CMD_STATUS_TIMEOUT] = "Operation timed out",
104 [MC_CMD_STATUS_NO_RESOURCE] = "No resources",
105 [MC_CMD_STATUS_NO_MEMORY] = "No memory available",
106 [MC_CMD_STATUS_BUSY] = "Device is busy",
107 [MC_CMD_STATUS_UNSUPPORTED_OP] = "Unsupported operation",
108 [MC_CMD_STATUS_INVALID_STATE] = "Invalid state"
109 };
110
111 if ((unsigned int)status >= ARRAY_SIZE(status_strings))
112 return "Unknown MC error";
113
114 return status_strings[status];
115}
116
117
118
119
120
121
122
123static inline void mc_write_command(struct mc_command __iomem *portal,
124 struct mc_command *cmd)
125{
126 int i;
127
128
129 for (i = 0; i < MC_CMD_NUM_OF_PARAMS; i++)
130
131
132
133
134
135 writeq_relaxed(le64_to_cpu(cmd->params[i]), &portal->params[i]);
136
137
138 writeq(le64_to_cpu(cmd->header), &portal->header);
139}
140
141
142
143
144
145
146
147
148
149
150static inline enum mc_cmd_status mc_read_response(struct mc_command __iomem *
151 portal,
152 struct mc_command *resp)
153{
154 int i;
155 enum mc_cmd_status status;
156
157
158 resp->header = cpu_to_le64(readq_relaxed(&portal->header));
159 status = mc_cmd_hdr_read_status(resp);
160 if (status != MC_CMD_STATUS_OK)
161 return status;
162
163
164 for (i = 0; i < MC_CMD_NUM_OF_PARAMS; i++)
165
166
167
168
169
170 resp->params[i] =
171 cpu_to_le64(readq_relaxed(&portal->params[i]));
172
173 return status;
174}
175
176
177
178
179
180
181
182
183
184static int mc_polling_wait_preemptible(struct fsl_mc_io *mc_io,
185 struct mc_command *cmd,
186 enum mc_cmd_status *mc_status)
187{
188 enum mc_cmd_status status;
189 unsigned long jiffies_until_timeout =
190 jiffies + msecs_to_jiffies(MC_CMD_COMPLETION_TIMEOUT_MS);
191
192
193
194
195 for (;;) {
196 status = mc_read_response(mc_io->portal_virt_addr, cmd);
197 if (status != MC_CMD_STATUS_READY)
198 break;
199
200
201
202
203
204 usleep_range(MC_CMD_COMPLETION_POLLING_MIN_SLEEP_USECS,
205 MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS);
206
207 if (time_after_eq(jiffies, jiffies_until_timeout)) {
208 dev_dbg(mc_io->dev,
209 "MC command timed out (portal: %pa, dprc handle: %#x, command: %#x)\n",
210 &mc_io->portal_phys_addr,
211 (unsigned int)mc_cmd_hdr_read_token(cmd),
212 (unsigned int)mc_cmd_hdr_read_cmdid(cmd));
213
214 return -ETIMEDOUT;
215 }
216 }
217
218 *mc_status = status;
219 return 0;
220}
221
222
223
224
225
226
227
228
229
230static int mc_polling_wait_atomic(struct fsl_mc_io *mc_io,
231 struct mc_command *cmd,
232 enum mc_cmd_status *mc_status)
233{
234 enum mc_cmd_status status;
235 unsigned long timeout_usecs = MC_CMD_COMPLETION_TIMEOUT_MS * 1000;
236
237 BUILD_BUG_ON((MC_CMD_COMPLETION_TIMEOUT_MS * 1000) %
238 MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS != 0);
239
240 for (;;) {
241 status = mc_read_response(mc_io->portal_virt_addr, cmd);
242 if (status != MC_CMD_STATUS_READY)
243 break;
244
245 udelay(MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS);
246 timeout_usecs -= MC_CMD_COMPLETION_POLLING_MAX_SLEEP_USECS;
247 if (timeout_usecs == 0) {
248 dev_dbg(mc_io->dev,
249 "MC command timed out (portal: %pa, dprc handle: %#x, command: %#x)\n",
250 &mc_io->portal_phys_addr,
251 (unsigned int)mc_cmd_hdr_read_token(cmd),
252 (unsigned int)mc_cmd_hdr_read_cmdid(cmd));
253
254 return -ETIMEDOUT;
255 }
256 }
257
258 *mc_status = status;
259 return 0;
260}
261
262
263
264
265
266
267
268
269
270int mc_send_command(struct fsl_mc_io *mc_io, struct mc_command *cmd)
271{
272 int error;
273 enum mc_cmd_status status;
274 unsigned long irq_flags = 0;
275
276 if (WARN_ON(in_irq() &&
277 !(mc_io->flags & FSL_MC_IO_ATOMIC_CONTEXT_PORTAL)))
278 return -EINVAL;
279
280 if (mc_io->flags & FSL_MC_IO_ATOMIC_CONTEXT_PORTAL)
281 spin_lock_irqsave(&mc_io->spinlock, irq_flags);
282 else
283 mutex_lock(&mc_io->mutex);
284
285
286
287
288 mc_write_command(mc_io->portal_virt_addr, cmd);
289
290
291
292
293 if (!(mc_io->flags & FSL_MC_IO_ATOMIC_CONTEXT_PORTAL))
294 error = mc_polling_wait_preemptible(mc_io, cmd, &status);
295 else
296 error = mc_polling_wait_atomic(mc_io, cmd, &status);
297
298 if (error < 0)
299 goto common_exit;
300
301 if (status != MC_CMD_STATUS_OK) {
302 dev_dbg(mc_io->dev,
303 "MC command failed: portal: %pa, dprc handle: %#x, command: %#x, status: %s (%#x)\n",
304 &mc_io->portal_phys_addr,
305 (unsigned int)mc_cmd_hdr_read_token(cmd),
306 (unsigned int)mc_cmd_hdr_read_cmdid(cmd),
307 mc_status_to_string(status),
308 (unsigned int)status);
309
310 error = mc_status_to_error(status);
311 goto common_exit;
312 }
313
314 error = 0;
315common_exit:
316 if (mc_io->flags & FSL_MC_IO_ATOMIC_CONTEXT_PORTAL)
317 spin_unlock_irqrestore(&mc_io->spinlock, irq_flags);
318 else
319 mutex_unlock(&mc_io->mutex);
320
321 return error;
322}
323EXPORT_SYMBOL(mc_send_command);
324