1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#include <linux/kernel.h>
18#include <linux/fs.h>
19#include <linux/errno.h>
20#include <linux/types.h>
21#include <linux/fcntl.h>
22#include <linux/ioctl.h>
23#include <linux/cdev.h>
24#include <linux/list.h>
25#include <linux/delay.h>
26#include <linux/sched.h>
27#include <linux/uuid.h>
28#include <linux/jiffies.h>
29#include <linux/uaccess.h>
30#include <linux/slab.h>
31
32#include <linux/mei.h>
33
34#include "mei_dev.h"
35#include "hbm.h"
36#include "client.h"
37
38const uuid_le mei_amthif_guid = UUID_LE(0x12f80028, 0xb4b7, 0x4b2d,
39 0xac, 0xa8, 0x46, 0xe0,
40 0xff, 0x65, 0x81, 0x4c);
41
42
43
44
45
46
47void mei_amthif_reset_params(struct mei_device *dev)
48{
49
50 dev->iamthif_current_cb = NULL;
51 dev->iamthif_canceled = false;
52 dev->iamthif_state = MEI_IAMTHIF_IDLE;
53 dev->iamthif_stall_timer = 0;
54 dev->iamthif_open_count = 0;
55}
56
57
58
59
60
61
62
63
64
65int mei_amthif_host_init(struct mei_device *dev, struct mei_me_client *me_cl)
66{
67 struct mei_cl *cl = &dev->iamthif_cl;
68 int ret;
69
70 if (mei_cl_is_connected(cl))
71 return 0;
72
73 dev->iamthif_state = MEI_IAMTHIF_IDLE;
74
75 mei_cl_init(cl, dev);
76
77 ret = mei_cl_link(cl);
78 if (ret < 0) {
79 dev_err(dev->dev, "amthif: failed cl_link %d\n", ret);
80 return ret;
81 }
82
83 ret = mei_cl_connect(cl, me_cl, NULL);
84
85 return ret;
86}
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104int mei_amthif_read(struct mei_device *dev, struct file *file,
105 char __user *ubuf, size_t length, loff_t *offset)
106{
107 struct mei_cl *cl = file->private_data;
108 struct mei_cl_cb *cb;
109 int rets;
110 int wait_ret;
111
112 dev_dbg(dev->dev, "checking amthif data\n");
113 cb = mei_cl_read_cb(cl, file);
114
115
116 if (cb == NULL && file->f_flags & O_NONBLOCK)
117 return -EAGAIN;
118
119
120 dev_dbg(dev->dev, "waiting for amthif data\n");
121 while (cb == NULL) {
122
123 mutex_unlock(&dev->device_lock);
124
125 wait_ret = wait_event_interruptible(cl->rx_wait,
126 !list_empty(&cl->rd_completed) ||
127 !mei_cl_is_connected(cl));
128
129
130 mutex_lock(&dev->device_lock);
131
132 if (wait_ret)
133 return -ERESTARTSYS;
134
135 if (!mei_cl_is_connected(cl)) {
136 rets = -EBUSY;
137 goto out;
138 }
139
140 cb = mei_cl_read_cb(cl, file);
141 }
142
143 if (cb->status) {
144 rets = cb->status;
145 dev_dbg(dev->dev, "read operation failed %d\n", rets);
146 goto free;
147 }
148
149 dev_dbg(dev->dev, "Got amthif data\n");
150
151 if (cb->buf_idx >= *offset && length >= (cb->buf_idx - *offset))
152 list_del_init(&cb->list);
153 else if (cb->buf_idx <= *offset) {
154
155 list_del_init(&cb->list);
156 rets = 0;
157 goto free;
158 }
159
160
161
162
163 dev_dbg(dev->dev, "amthif cb->buf.size - %zu cb->buf_idx - %zu\n",
164 cb->buf.size, cb->buf_idx);
165
166
167
168 length = min_t(size_t, length, (cb->buf_idx - *offset));
169
170 if (copy_to_user(ubuf, cb->buf.data + *offset, length)) {
171 dev_dbg(dev->dev, "failed to copy data to userland\n");
172 rets = -EFAULT;
173 } else {
174 rets = length;
175 if ((*offset + length) < cb->buf_idx) {
176 *offset += length;
177 goto out;
178 }
179 }
180free:
181 dev_dbg(dev->dev, "free amthif cb memory.\n");
182 *offset = 0;
183 mei_io_cb_free(cb);
184out:
185 return rets;
186}
187
188
189
190
191
192
193
194
195
196static int mei_amthif_read_start(struct mei_cl *cl, const struct file *file)
197{
198 struct mei_device *dev = cl->dev;
199 struct mei_cl_cb *cb;
200 int rets;
201
202 cb = mei_io_cb_init(cl, MEI_FOP_READ, file);
203 if (!cb) {
204 rets = -ENOMEM;
205 goto err;
206 }
207
208 rets = mei_io_cb_alloc_buf(cb, mei_cl_mtu(cl));
209 if (rets)
210 goto err;
211
212 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
213
214 dev->iamthif_state = MEI_IAMTHIF_READING;
215 dev->iamthif_fp = cb->fp;
216 dev->iamthif_current_cb = cb;
217
218 return 0;
219err:
220 mei_io_cb_free(cb);
221 return rets;
222}
223
224
225
226
227
228
229
230
231
232static int mei_amthif_send_cmd(struct mei_cl *cl, struct mei_cl_cb *cb)
233{
234 struct mei_device *dev;
235 int ret;
236
237 if (!cl->dev || !cb)
238 return -ENODEV;
239
240 dev = cl->dev;
241
242 dev->iamthif_state = MEI_IAMTHIF_WRITING;
243 dev->iamthif_current_cb = cb;
244 dev->iamthif_fp = cb->fp;
245 dev->iamthif_canceled = false;
246
247 ret = mei_cl_write(cl, cb, false);
248 if (ret < 0)
249 return ret;
250
251 if (cb->completed)
252 cb->status = mei_amthif_read_start(cl, cb->fp);
253
254 return 0;
255}
256
257
258
259
260
261
262
263
264int mei_amthif_run_next_cmd(struct mei_device *dev)
265{
266 struct mei_cl *cl = &dev->iamthif_cl;
267 struct mei_cl_cb *cb;
268
269 dev->iamthif_canceled = false;
270 dev->iamthif_state = MEI_IAMTHIF_IDLE;
271 dev->iamthif_fp = NULL;
272
273 dev_dbg(dev->dev, "complete amthif cmd_list cb.\n");
274
275 cb = list_first_entry_or_null(&dev->amthif_cmd_list.list,
276 typeof(*cb), list);
277 if (!cb)
278 return 0;
279
280 list_del_init(&cb->list);
281 return mei_amthif_send_cmd(cl, cb);
282}
283
284
285
286
287
288
289
290
291
292int mei_amthif_write(struct mei_cl *cl, struct mei_cl_cb *cb)
293{
294
295 struct mei_device *dev = cl->dev;
296
297 list_add_tail(&cb->list, &dev->amthif_cmd_list.list);
298
299
300
301
302 if (dev->iamthif_state > MEI_IAMTHIF_IDLE &&
303 dev->iamthif_state < MEI_IAMTHIF_READ_COMPLETE)
304 return 0;
305
306 return mei_amthif_run_next_cmd(dev);
307}
308
309
310
311
312
313
314
315
316
317
318
319
320
321unsigned int mei_amthif_poll(struct mei_device *dev,
322 struct file *file, poll_table *wait)
323{
324 unsigned int mask = 0;
325
326 poll_wait(file, &dev->iamthif_cl.rx_wait, wait);
327
328 if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE &&
329 dev->iamthif_fp == file) {
330
331 mask |= POLLIN | POLLRDNORM;
332 mei_amthif_run_next_cmd(dev);
333 }
334
335 return mask;
336}
337
338
339
340
341
342
343
344
345
346
347
348
349int mei_amthif_irq_write(struct mei_cl *cl, struct mei_cl_cb *cb,
350 struct mei_cl_cb *cmpl_list)
351{
352 int ret;
353
354 ret = mei_cl_irq_write(cl, cb, cmpl_list);
355 if (ret)
356 return ret;
357
358 if (cb->completed)
359 cb->status = mei_amthif_read_start(cl, cb->fp);
360
361 return 0;
362}
363
364
365
366
367
368
369
370
371
372
373
374int mei_amthif_irq_read_msg(struct mei_cl *cl,
375 struct mei_msg_hdr *mei_hdr,
376 struct mei_cl_cb *cmpl_list)
377{
378 struct mei_device *dev;
379 int ret;
380
381 dev = cl->dev;
382
383 if (dev->iamthif_state != MEI_IAMTHIF_READING) {
384 mei_irq_discard_msg(dev, mei_hdr);
385 return 0;
386 }
387
388 ret = mei_cl_irq_read_msg(cl, mei_hdr, cmpl_list);
389 if (ret)
390 return ret;
391
392 if (!mei_hdr->msg_complete)
393 return 0;
394
395 dev_dbg(dev->dev, "completed amthif read.\n ");
396 dev->iamthif_current_cb = NULL;
397 dev->iamthif_stall_timer = 0;
398
399 return 0;
400}
401
402
403
404
405
406
407
408void mei_amthif_complete(struct mei_cl *cl, struct mei_cl_cb *cb)
409{
410 struct mei_device *dev = cl->dev;
411
412 if (cb->fop_type == MEI_FOP_WRITE) {
413 if (!cb->status) {
414 dev->iamthif_stall_timer = MEI_IAMTHIF_STALL_TIMER;
415 mei_io_cb_free(cb);
416 return;
417 }
418
419
420
421
422 list_add_tail(&cb->list, &cl->rd_completed);
423 wake_up_interruptible(&cl->rx_wait);
424 return;
425 }
426
427 if (!dev->iamthif_canceled) {
428 dev->iamthif_state = MEI_IAMTHIF_READ_COMPLETE;
429 dev->iamthif_stall_timer = 0;
430 list_add_tail(&cb->list, &cl->rd_completed);
431 dev_dbg(dev->dev, "amthif read completed\n");
432 } else {
433 mei_amthif_run_next_cmd(dev);
434 }
435
436 dev_dbg(dev->dev, "completing amthif call back.\n");
437 wake_up_interruptible(&cl->rx_wait);
438}
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453static bool mei_clear_list(struct mei_device *dev,
454 const struct file *file, struct list_head *mei_cb_list)
455{
456 struct mei_cl *cl = &dev->iamthif_cl;
457 struct mei_cl_cb *cb, *next;
458 bool removed = false;
459
460
461 list_for_each_entry_safe(cb, next, mei_cb_list, list) {
462
463 if (file == cb->fp) {
464
465 if (dev->iamthif_current_cb == cb) {
466 dev->iamthif_current_cb = NULL;
467
468 mei_hbm_cl_flow_control_req(dev, cl);
469 }
470
471 mei_io_cb_free(cb);
472 removed = true;
473 }
474 }
475 return removed;
476}
477
478
479
480
481
482
483
484
485
486
487
488
489static bool mei_clear_lists(struct mei_device *dev, const struct file *file)
490{
491 bool removed = false;
492 struct mei_cl *cl = &dev->iamthif_cl;
493
494
495 mei_clear_list(dev, file, &dev->amthif_cmd_list.list);
496 if (mei_clear_list(dev, file, &cl->rd_completed))
497 removed = true;
498
499 mei_clear_list(dev, file, &dev->ctrl_rd_list.list);
500
501 if (mei_clear_list(dev, file, &dev->ctrl_wr_list.list))
502 removed = true;
503
504 if (mei_clear_list(dev, file, &dev->write_waiting_list.list))
505 removed = true;
506
507 if (mei_clear_list(dev, file, &dev->write_list.list))
508 removed = true;
509
510
511 if (dev->iamthif_current_cb && !removed) {
512
513 if (dev->iamthif_current_cb->fp == file) {
514
515 mei_io_cb_free(dev->iamthif_current_cb);
516 dev->iamthif_current_cb = NULL;
517 removed = true;
518 }
519 }
520 return removed;
521}
522
523
524
525
526
527
528
529
530
531int mei_amthif_release(struct mei_device *dev, struct file *file)
532{
533 if (dev->iamthif_open_count > 0)
534 dev->iamthif_open_count--;
535
536 if (dev->iamthif_fp == file &&
537 dev->iamthif_state != MEI_IAMTHIF_IDLE) {
538
539 dev_dbg(dev->dev, "amthif canceled iamthif state %d\n",
540 dev->iamthif_state);
541 dev->iamthif_canceled = true;
542 if (dev->iamthif_state == MEI_IAMTHIF_READ_COMPLETE) {
543 dev_dbg(dev->dev, "run next amthif iamthif cb\n");
544 mei_amthif_run_next_cmd(dev);
545 }
546 }
547
548 if (mei_clear_lists(dev, file))
549 dev->iamthif_state = MEI_IAMTHIF_IDLE;
550
551 return 0;
552}
553