1 /*
2 *
3 * Intel Management Engine Interface (Intel MEI) Linux driver
4 * Copyright (c) 2003-2012, Intel Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 */
16
17 #include <linux/sched.h>
18 #include <linux/wait.h>
19 #include <linux/delay.h>
20 #include <linux/slab.h>
21 #include <linux/pm_runtime.h>
22
23 #include <linux/mei.h>
24
25 #include "mei_dev.h"
26 #include "hbm.h"
27 #include "client.h"
28
29 /**
30 * mei_me_cl_init - initialize me client
31 *
32 * @me_cl: me client
33 */
mei_me_cl_init(struct mei_me_client * me_cl)34 void mei_me_cl_init(struct mei_me_client *me_cl)
35 {
36 INIT_LIST_HEAD(&me_cl->list);
37 kref_init(&me_cl->refcnt);
38 }
39
40 /**
41 * mei_me_cl_get - increases me client refcount
42 *
43 * @me_cl: me client
44 *
45 * Locking: called under "dev->device_lock" lock
46 *
47 * Return: me client or NULL
48 */
mei_me_cl_get(struct mei_me_client * me_cl)49 struct mei_me_client *mei_me_cl_get(struct mei_me_client *me_cl)
50 {
51 if (me_cl && kref_get_unless_zero(&me_cl->refcnt))
52 return me_cl;
53
54 return NULL;
55 }
56
57 /**
58 * mei_me_cl_release - free me client
59 *
60 * Locking: called under "dev->device_lock" lock
61 *
62 * @ref: me_client refcount
63 */
mei_me_cl_release(struct kref * ref)64 static void mei_me_cl_release(struct kref *ref)
65 {
66 struct mei_me_client *me_cl =
67 container_of(ref, struct mei_me_client, refcnt);
68
69 kfree(me_cl);
70 }
71
72 /**
73 * mei_me_cl_put - decrease me client refcount and free client if necessary
74 *
75 * Locking: called under "dev->device_lock" lock
76 *
77 * @me_cl: me client
78 */
mei_me_cl_put(struct mei_me_client * me_cl)79 void mei_me_cl_put(struct mei_me_client *me_cl)
80 {
81 if (me_cl)
82 kref_put(&me_cl->refcnt, mei_me_cl_release);
83 }
84
85 /**
86 * __mei_me_cl_del - delete me client form the list and decrease
87 * reference counter
88 *
89 * @dev: mei device
90 * @me_cl: me client
91 *
92 * Locking: dev->me_clients_rwsem
93 */
__mei_me_cl_del(struct mei_device * dev,struct mei_me_client * me_cl)94 static void __mei_me_cl_del(struct mei_device *dev, struct mei_me_client *me_cl)
95 {
96 if (!me_cl)
97 return;
98
99 list_del(&me_cl->list);
100 mei_me_cl_put(me_cl);
101 }
102
103 /**
104 * mei_me_cl_add - add me client to the list
105 *
106 * @dev: mei device
107 * @me_cl: me client
108 */
mei_me_cl_add(struct mei_device * dev,struct mei_me_client * me_cl)109 void mei_me_cl_add(struct mei_device *dev, struct mei_me_client *me_cl)
110 {
111 down_write(&dev->me_clients_rwsem);
112 list_add(&me_cl->list, &dev->me_clients);
113 up_write(&dev->me_clients_rwsem);
114 }
115
116 /**
117 * __mei_me_cl_by_uuid - locate me client by uuid
118 * increases ref count
119 *
120 * @dev: mei device
121 * @uuid: me client uuid
122 *
123 * Return: me client or NULL if not found
124 *
125 * Locking: dev->me_clients_rwsem
126 */
__mei_me_cl_by_uuid(struct mei_device * dev,const uuid_le * uuid)127 static struct mei_me_client *__mei_me_cl_by_uuid(struct mei_device *dev,
128 const uuid_le *uuid)
129 {
130 struct mei_me_client *me_cl;
131 const uuid_le *pn;
132
133 WARN_ON(!rwsem_is_locked(&dev->me_clients_rwsem));
134
135 list_for_each_entry(me_cl, &dev->me_clients, list) {
136 pn = &me_cl->props.protocol_name;
137 if (uuid_le_cmp(*uuid, *pn) == 0)
138 return mei_me_cl_get(me_cl);
139 }
140
141 return NULL;
142 }
143
144 /**
145 * mei_me_cl_by_uuid - locate me client by uuid
146 * increases ref count
147 *
148 * @dev: mei device
149 * @uuid: me client uuid
150 *
151 * Return: me client or NULL if not found
152 *
153 * Locking: dev->me_clients_rwsem
154 */
mei_me_cl_by_uuid(struct mei_device * dev,const uuid_le * uuid)155 struct mei_me_client *mei_me_cl_by_uuid(struct mei_device *dev,
156 const uuid_le *uuid)
157 {
158 struct mei_me_client *me_cl;
159
160 down_read(&dev->me_clients_rwsem);
161 me_cl = __mei_me_cl_by_uuid(dev, uuid);
162 up_read(&dev->me_clients_rwsem);
163
164 return me_cl;
165 }
166
167 /**
168 * mei_me_cl_by_id - locate me client by client id
169 * increases ref count
170 *
171 * @dev: the device structure
172 * @client_id: me client id
173 *
174 * Return: me client or NULL if not found
175 *
176 * Locking: dev->me_clients_rwsem
177 */
mei_me_cl_by_id(struct mei_device * dev,u8 client_id)178 struct mei_me_client *mei_me_cl_by_id(struct mei_device *dev, u8 client_id)
179 {
180
181 struct mei_me_client *__me_cl, *me_cl = NULL;
182
183 down_read(&dev->me_clients_rwsem);
184 list_for_each_entry(__me_cl, &dev->me_clients, list) {
185 if (__me_cl->client_id == client_id) {
186 me_cl = mei_me_cl_get(__me_cl);
187 break;
188 }
189 }
190 up_read(&dev->me_clients_rwsem);
191
192 return me_cl;
193 }
194
195 /**
196 * __mei_me_cl_by_uuid_id - locate me client by client id and uuid
197 * increases ref count
198 *
199 * @dev: the device structure
200 * @uuid: me client uuid
201 * @client_id: me client id
202 *
203 * Return: me client or null if not found
204 *
205 * Locking: dev->me_clients_rwsem
206 */
__mei_me_cl_by_uuid_id(struct mei_device * dev,const uuid_le * uuid,u8 client_id)207 static struct mei_me_client *__mei_me_cl_by_uuid_id(struct mei_device *dev,
208 const uuid_le *uuid, u8 client_id)
209 {
210 struct mei_me_client *me_cl;
211 const uuid_le *pn;
212
213 WARN_ON(!rwsem_is_locked(&dev->me_clients_rwsem));
214
215 list_for_each_entry(me_cl, &dev->me_clients, list) {
216 pn = &me_cl->props.protocol_name;
217 if (uuid_le_cmp(*uuid, *pn) == 0 &&
218 me_cl->client_id == client_id)
219 return mei_me_cl_get(me_cl);
220 }
221
222 return NULL;
223 }
224
225
226 /**
227 * mei_me_cl_by_uuid_id - locate me client by client id and uuid
228 * increases ref count
229 *
230 * @dev: the device structure
231 * @uuid: me client uuid
232 * @client_id: me client id
233 *
234 * Return: me client or null if not found
235 */
mei_me_cl_by_uuid_id(struct mei_device * dev,const uuid_le * uuid,u8 client_id)236 struct mei_me_client *mei_me_cl_by_uuid_id(struct mei_device *dev,
237 const uuid_le *uuid, u8 client_id)
238 {
239 struct mei_me_client *me_cl;
240
241 down_read(&dev->me_clients_rwsem);
242 me_cl = __mei_me_cl_by_uuid_id(dev, uuid, client_id);
243 up_read(&dev->me_clients_rwsem);
244
245 return me_cl;
246 }
247
248 /**
249 * mei_me_cl_rm_by_uuid - remove all me clients matching uuid
250 *
251 * @dev: the device structure
252 * @uuid: me client uuid
253 *
254 * Locking: called under "dev->device_lock" lock
255 */
mei_me_cl_rm_by_uuid(struct mei_device * dev,const uuid_le * uuid)256 void mei_me_cl_rm_by_uuid(struct mei_device *dev, const uuid_le *uuid)
257 {
258 struct mei_me_client *me_cl;
259
260 dev_dbg(dev->dev, "remove %pUl\n", uuid);
261
262 down_write(&dev->me_clients_rwsem);
263 me_cl = __mei_me_cl_by_uuid(dev, uuid);
264 __mei_me_cl_del(dev, me_cl);
265 up_write(&dev->me_clients_rwsem);
266 }
267
268 /**
269 * mei_me_cl_rm_by_uuid_id - remove all me clients matching client id
270 *
271 * @dev: the device structure
272 * @uuid: me client uuid
273 * @id: me client id
274 *
275 * Locking: called under "dev->device_lock" lock
276 */
mei_me_cl_rm_by_uuid_id(struct mei_device * dev,const uuid_le * uuid,u8 id)277 void mei_me_cl_rm_by_uuid_id(struct mei_device *dev, const uuid_le *uuid, u8 id)
278 {
279 struct mei_me_client *me_cl;
280
281 dev_dbg(dev->dev, "remove %pUl %d\n", uuid, id);
282
283 down_write(&dev->me_clients_rwsem);
284 me_cl = __mei_me_cl_by_uuid_id(dev, uuid, id);
285 __mei_me_cl_del(dev, me_cl);
286 up_write(&dev->me_clients_rwsem);
287 }
288
289 /**
290 * mei_me_cl_rm_all - remove all me clients
291 *
292 * @dev: the device structure
293 *
294 * Locking: called under "dev->device_lock" lock
295 */
mei_me_cl_rm_all(struct mei_device * dev)296 void mei_me_cl_rm_all(struct mei_device *dev)
297 {
298 struct mei_me_client *me_cl, *next;
299
300 down_write(&dev->me_clients_rwsem);
301 list_for_each_entry_safe(me_cl, next, &dev->me_clients, list)
302 __mei_me_cl_del(dev, me_cl);
303 up_write(&dev->me_clients_rwsem);
304 }
305
306 /**
307 * mei_cl_cmp_id - tells if the clients are the same
308 *
309 * @cl1: host client 1
310 * @cl2: host client 2
311 *
312 * Return: true - if the clients has same host and me ids
313 * false - otherwise
314 */
mei_cl_cmp_id(const struct mei_cl * cl1,const struct mei_cl * cl2)315 static inline bool mei_cl_cmp_id(const struct mei_cl *cl1,
316 const struct mei_cl *cl2)
317 {
318 return cl1 && cl2 &&
319 (cl1->host_client_id == cl2->host_client_id) &&
320 (cl1->me_client_id == cl2->me_client_id);
321 }
322
323 /**
324 * mei_io_cb_free - free mei_cb_private related memory
325 *
326 * @cb: mei callback struct
327 */
mei_io_cb_free(struct mei_cl_cb * cb)328 void mei_io_cb_free(struct mei_cl_cb *cb)
329 {
330 if (cb == NULL)
331 return;
332
333 list_del(&cb->list);
334 kfree(cb->buf.data);
335 kfree(cb);
336 }
337
338 /**
339 * mei_io_cb_init - allocate and initialize io callback
340 *
341 * @cl: mei client
342 * @type: operation type
343 * @fp: pointer to file structure
344 *
345 * Return: mei_cl_cb pointer or NULL;
346 */
mei_io_cb_init(struct mei_cl * cl,enum mei_cb_file_ops type,struct file * fp)347 struct mei_cl_cb *mei_io_cb_init(struct mei_cl *cl, enum mei_cb_file_ops type,
348 struct file *fp)
349 {
350 struct mei_cl_cb *cb;
351
352 cb = kzalloc(sizeof(struct mei_cl_cb), GFP_KERNEL);
353 if (!cb)
354 return NULL;
355
356 INIT_LIST_HEAD(&cb->list);
357 cb->file_object = fp;
358 cb->cl = cl;
359 cb->buf_idx = 0;
360 cb->fop_type = type;
361 return cb;
362 }
363
364 /**
365 * __mei_io_list_flush - removes and frees cbs belonging to cl.
366 *
367 * @list: an instance of our list structure
368 * @cl: host client, can be NULL for flushing the whole list
369 * @free: whether to free the cbs
370 */
__mei_io_list_flush(struct mei_cl_cb * list,struct mei_cl * cl,bool free)371 static void __mei_io_list_flush(struct mei_cl_cb *list,
372 struct mei_cl *cl, bool free)
373 {
374 struct mei_cl_cb *cb, *next;
375
376 /* enable removing everything if no cl is specified */
377 list_for_each_entry_safe(cb, next, &list->list, list) {
378 if (!cl || mei_cl_cmp_id(cl, cb->cl)) {
379 list_del_init(&cb->list);
380 if (free)
381 mei_io_cb_free(cb);
382 }
383 }
384 }
385
386 /**
387 * mei_io_list_flush - removes list entry belonging to cl.
388 *
389 * @list: An instance of our list structure
390 * @cl: host client
391 */
mei_io_list_flush(struct mei_cl_cb * list,struct mei_cl * cl)392 void mei_io_list_flush(struct mei_cl_cb *list, struct mei_cl *cl)
393 {
394 __mei_io_list_flush(list, cl, false);
395 }
396
397 /**
398 * mei_io_list_free - removes cb belonging to cl and free them
399 *
400 * @list: An instance of our list structure
401 * @cl: host client
402 */
mei_io_list_free(struct mei_cl_cb * list,struct mei_cl * cl)403 static inline void mei_io_list_free(struct mei_cl_cb *list, struct mei_cl *cl)
404 {
405 __mei_io_list_flush(list, cl, true);
406 }
407
408 /**
409 * mei_io_cb_alloc_buf - allocate callback buffer
410 *
411 * @cb: io callback structure
412 * @length: size of the buffer
413 *
414 * Return: 0 on success
415 * -EINVAL if cb is NULL
416 * -ENOMEM if allocation failed
417 */
mei_io_cb_alloc_buf(struct mei_cl_cb * cb,size_t length)418 int mei_io_cb_alloc_buf(struct mei_cl_cb *cb, size_t length)
419 {
420 if (!cb)
421 return -EINVAL;
422
423 if (length == 0)
424 return 0;
425
426 cb->buf.data = kmalloc(length, GFP_KERNEL);
427 if (!cb->buf.data)
428 return -ENOMEM;
429 cb->buf.size = length;
430 return 0;
431 }
432
433 /**
434 * mei_cl_alloc_cb - a convenient wrapper for allocating read cb
435 *
436 * @cl: host client
437 * @length: size of the buffer
438 * @type: operation type
439 * @fp: associated file pointer (might be NULL)
440 *
441 * Return: cb on success and NULL on failure
442 */
mei_cl_alloc_cb(struct mei_cl * cl,size_t length,enum mei_cb_file_ops type,struct file * fp)443 struct mei_cl_cb *mei_cl_alloc_cb(struct mei_cl *cl, size_t length,
444 enum mei_cb_file_ops type, struct file *fp)
445 {
446 struct mei_cl_cb *cb;
447
448 cb = mei_io_cb_init(cl, type, fp);
449 if (!cb)
450 return NULL;
451
452 if (mei_io_cb_alloc_buf(cb, length)) {
453 mei_io_cb_free(cb);
454 return NULL;
455 }
456
457 return cb;
458 }
459
460 /**
461 * mei_cl_read_cb - find this cl's callback in the read list
462 * for a specific file
463 *
464 * @cl: host client
465 * @fp: file pointer (matching cb file object), may be NULL
466 *
467 * Return: cb on success, NULL if cb is not found
468 */
mei_cl_read_cb(const struct mei_cl * cl,const struct file * fp)469 struct mei_cl_cb *mei_cl_read_cb(const struct mei_cl *cl, const struct file *fp)
470 {
471 struct mei_cl_cb *cb;
472
473 list_for_each_entry(cb, &cl->rd_completed, list)
474 if (!fp || fp == cb->file_object)
475 return cb;
476
477 return NULL;
478 }
479
480 /**
481 * mei_cl_read_cb_flush - free client's read pending and completed cbs
482 * for a specific file
483 *
484 * @cl: host client
485 * @fp: file pointer (matching cb file object), may be NULL
486 */
mei_cl_read_cb_flush(const struct mei_cl * cl,const struct file * fp)487 void mei_cl_read_cb_flush(const struct mei_cl *cl, const struct file *fp)
488 {
489 struct mei_cl_cb *cb, *next;
490
491 list_for_each_entry_safe(cb, next, &cl->rd_completed, list)
492 if (!fp || fp == cb->file_object)
493 mei_io_cb_free(cb);
494
495
496 list_for_each_entry_safe(cb, next, &cl->rd_pending, list)
497 if (!fp || fp == cb->file_object)
498 mei_io_cb_free(cb);
499 }
500
501 /**
502 * mei_cl_flush_queues - flushes queue lists belonging to cl.
503 *
504 * @cl: host client
505 * @fp: file pointer (matching cb file object), may be NULL
506 *
507 * Return: 0 on success, -EINVAL if cl or cl->dev is NULL.
508 */
mei_cl_flush_queues(struct mei_cl * cl,const struct file * fp)509 int mei_cl_flush_queues(struct mei_cl *cl, const struct file *fp)
510 {
511 struct mei_device *dev;
512
513 if (WARN_ON(!cl || !cl->dev))
514 return -EINVAL;
515
516 dev = cl->dev;
517
518 cl_dbg(dev, cl, "remove list entry belonging to cl\n");
519 mei_io_list_free(&cl->dev->write_list, cl);
520 mei_io_list_free(&cl->dev->write_waiting_list, cl);
521 mei_io_list_flush(&cl->dev->ctrl_wr_list, cl);
522 mei_io_list_flush(&cl->dev->ctrl_rd_list, cl);
523 mei_io_list_flush(&cl->dev->amthif_cmd_list, cl);
524 mei_io_list_flush(&cl->dev->amthif_rd_complete_list, cl);
525
526 mei_cl_read_cb_flush(cl, fp);
527
528 return 0;
529 }
530
531
532 /**
533 * mei_cl_init - initializes cl.
534 *
535 * @cl: host client to be initialized
536 * @dev: mei device
537 */
mei_cl_init(struct mei_cl * cl,struct mei_device * dev)538 void mei_cl_init(struct mei_cl *cl, struct mei_device *dev)
539 {
540 memset(cl, 0, sizeof(struct mei_cl));
541 init_waitqueue_head(&cl->wait);
542 init_waitqueue_head(&cl->rx_wait);
543 init_waitqueue_head(&cl->tx_wait);
544 INIT_LIST_HEAD(&cl->rd_completed);
545 INIT_LIST_HEAD(&cl->rd_pending);
546 INIT_LIST_HEAD(&cl->link);
547 INIT_LIST_HEAD(&cl->device_link);
548 cl->writing_state = MEI_IDLE;
549 cl->dev = dev;
550 }
551
552 /**
553 * mei_cl_allocate - allocates cl structure and sets it up.
554 *
555 * @dev: mei device
556 * Return: The allocated file or NULL on failure
557 */
mei_cl_allocate(struct mei_device * dev)558 struct mei_cl *mei_cl_allocate(struct mei_device *dev)
559 {
560 struct mei_cl *cl;
561
562 cl = kmalloc(sizeof(struct mei_cl), GFP_KERNEL);
563 if (!cl)
564 return NULL;
565
566 mei_cl_init(cl, dev);
567
568 return cl;
569 }
570
571 /**
572 * mei_cl_link - allocate host id in the host map
573 *
574 * @cl: host client
575 * @id: fixed host id or MEI_HOST_CLIENT_ID_ANY (-1) for generic one
576 *
577 * Return: 0 on success
578 * -EINVAL on incorrect values
579 * -EMFILE if open count exceeded.
580 */
mei_cl_link(struct mei_cl * cl,int id)581 int mei_cl_link(struct mei_cl *cl, int id)
582 {
583 struct mei_device *dev;
584 long open_handle_count;
585
586 if (WARN_ON(!cl || !cl->dev))
587 return -EINVAL;
588
589 dev = cl->dev;
590
591 /* If Id is not assigned get one*/
592 if (id == MEI_HOST_CLIENT_ID_ANY)
593 id = find_first_zero_bit(dev->host_clients_map,
594 MEI_CLIENTS_MAX);
595
596 if (id >= MEI_CLIENTS_MAX) {
597 dev_err(dev->dev, "id exceeded %d", MEI_CLIENTS_MAX);
598 return -EMFILE;
599 }
600
601 open_handle_count = dev->open_handle_count + dev->iamthif_open_count;
602 if (open_handle_count >= MEI_MAX_OPEN_HANDLE_COUNT) {
603 dev_err(dev->dev, "open_handle_count exceeded %d",
604 MEI_MAX_OPEN_HANDLE_COUNT);
605 return -EMFILE;
606 }
607
608 dev->open_handle_count++;
609
610 cl->host_client_id = id;
611 list_add_tail(&cl->link, &dev->file_list);
612
613 set_bit(id, dev->host_clients_map);
614
615 cl->state = MEI_FILE_INITIALIZING;
616
617 cl_dbg(dev, cl, "link cl\n");
618 return 0;
619 }
620
621 /**
622 * mei_cl_unlink - remove me_cl from the list
623 *
624 * @cl: host client
625 *
626 * Return: always 0
627 */
mei_cl_unlink(struct mei_cl * cl)628 int mei_cl_unlink(struct mei_cl *cl)
629 {
630 struct mei_device *dev;
631
632 /* don't shout on error exit path */
633 if (!cl)
634 return 0;
635
636 /* wd and amthif might not be initialized */
637 if (!cl->dev)
638 return 0;
639
640 dev = cl->dev;
641
642 cl_dbg(dev, cl, "unlink client");
643
644 if (dev->open_handle_count > 0)
645 dev->open_handle_count--;
646
647 /* never clear the 0 bit */
648 if (cl->host_client_id)
649 clear_bit(cl->host_client_id, dev->host_clients_map);
650
651 list_del_init(&cl->link);
652
653 cl->state = MEI_FILE_INITIALIZING;
654
655 return 0;
656 }
657
658
mei_host_client_init(struct work_struct * work)659 void mei_host_client_init(struct work_struct *work)
660 {
661 struct mei_device *dev =
662 container_of(work, struct mei_device, init_work);
663 struct mei_me_client *me_cl;
664
665 mutex_lock(&dev->device_lock);
666
667
668 me_cl = mei_me_cl_by_uuid(dev, &mei_amthif_guid);
669 if (me_cl)
670 mei_amthif_host_init(dev);
671 mei_me_cl_put(me_cl);
672
673 me_cl = mei_me_cl_by_uuid(dev, &mei_wd_guid);
674 if (me_cl)
675 mei_wd_host_init(dev);
676 mei_me_cl_put(me_cl);
677
678 me_cl = mei_me_cl_by_uuid(dev, &mei_nfc_guid);
679 if (me_cl)
680 mei_nfc_host_init(dev);
681 mei_me_cl_put(me_cl);
682
683
684 dev->dev_state = MEI_DEV_ENABLED;
685 dev->reset_count = 0;
686 mutex_unlock(&dev->device_lock);
687
688 pm_runtime_mark_last_busy(dev->dev);
689 dev_dbg(dev->dev, "rpm: autosuspend\n");
690 pm_runtime_autosuspend(dev->dev);
691 }
692
693 /**
694 * mei_hbuf_acquire - try to acquire host buffer
695 *
696 * @dev: the device structure
697 * Return: true if host buffer was acquired
698 */
mei_hbuf_acquire(struct mei_device * dev)699 bool mei_hbuf_acquire(struct mei_device *dev)
700 {
701 if (mei_pg_state(dev) == MEI_PG_ON ||
702 mei_pg_in_transition(dev)) {
703 dev_dbg(dev->dev, "device is in pg\n");
704 return false;
705 }
706
707 if (!dev->hbuf_is_ready) {
708 dev_dbg(dev->dev, "hbuf is not ready\n");
709 return false;
710 }
711
712 dev->hbuf_is_ready = false;
713
714 return true;
715 }
716
717 /**
718 * mei_cl_disconnect - disconnect host client from the me one
719 *
720 * @cl: host client
721 *
722 * Locking: called under "dev->device_lock" lock
723 *
724 * Return: 0 on success, <0 on failure.
725 */
mei_cl_disconnect(struct mei_cl * cl)726 int mei_cl_disconnect(struct mei_cl *cl)
727 {
728 struct mei_device *dev;
729 struct mei_cl_cb *cb;
730 int rets;
731
732 if (WARN_ON(!cl || !cl->dev))
733 return -ENODEV;
734
735 dev = cl->dev;
736
737 cl_dbg(dev, cl, "disconnecting");
738
739 if (cl->state != MEI_FILE_DISCONNECTING)
740 return 0;
741
742 rets = pm_runtime_get(dev->dev);
743 if (rets < 0 && rets != -EINPROGRESS) {
744 pm_runtime_put_noidle(dev->dev);
745 cl_err(dev, cl, "rpm: get failed %d\n", rets);
746 return rets;
747 }
748
749 cb = mei_io_cb_init(cl, MEI_FOP_DISCONNECT, NULL);
750 rets = cb ? 0 : -ENOMEM;
751 if (rets)
752 goto free;
753
754 if (mei_hbuf_acquire(dev)) {
755 if (mei_hbm_cl_disconnect_req(dev, cl)) {
756 rets = -ENODEV;
757 cl_err(dev, cl, "failed to disconnect.\n");
758 goto free;
759 }
760 cl->timer_count = MEI_CONNECT_TIMEOUT;
761 mdelay(10); /* Wait for hardware disconnection ready */
762 list_add_tail(&cb->list, &dev->ctrl_rd_list.list);
763 } else {
764 cl_dbg(dev, cl, "add disconnect cb to control write list\n");
765 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
766
767 }
768 mutex_unlock(&dev->device_lock);
769
770 wait_event_timeout(cl->wait,
771 MEI_FILE_DISCONNECTED == cl->state,
772 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
773
774 mutex_lock(&dev->device_lock);
775
776 if (MEI_FILE_DISCONNECTED == cl->state) {
777 rets = 0;
778 cl_dbg(dev, cl, "successfully disconnected from FW client.\n");
779 } else {
780 cl_dbg(dev, cl, "timeout on disconnect from FW client.\n");
781 rets = -ETIME;
782 }
783
784 mei_io_list_flush(&dev->ctrl_rd_list, cl);
785 mei_io_list_flush(&dev->ctrl_wr_list, cl);
786 free:
787 cl_dbg(dev, cl, "rpm: autosuspend\n");
788 pm_runtime_mark_last_busy(dev->dev);
789 pm_runtime_put_autosuspend(dev->dev);
790
791 mei_io_cb_free(cb);
792 return rets;
793 }
794
795
796 /**
797 * mei_cl_is_other_connecting - checks if other
798 * client with the same me client id is connecting
799 *
800 * @cl: private data of the file object
801 *
802 * Return: true if other client is connected, false - otherwise.
803 */
mei_cl_is_other_connecting(struct mei_cl * cl)804 bool mei_cl_is_other_connecting(struct mei_cl *cl)
805 {
806 struct mei_device *dev;
807 struct mei_cl *ocl; /* the other client */
808
809 if (WARN_ON(!cl || !cl->dev))
810 return false;
811
812 dev = cl->dev;
813
814 list_for_each_entry(ocl, &dev->file_list, link) {
815 if (ocl->state == MEI_FILE_CONNECTING &&
816 ocl != cl &&
817 cl->me_client_id == ocl->me_client_id)
818 return true;
819
820 }
821
822 return false;
823 }
824
825 /**
826 * mei_cl_connect - connect host client to the me one
827 *
828 * @cl: host client
829 * @file: pointer to file structure
830 *
831 * Locking: called under "dev->device_lock" lock
832 *
833 * Return: 0 on success, <0 on failure.
834 */
mei_cl_connect(struct mei_cl * cl,struct file * file)835 int mei_cl_connect(struct mei_cl *cl, struct file *file)
836 {
837 struct mei_device *dev;
838 struct mei_cl_cb *cb;
839 int rets;
840
841 if (WARN_ON(!cl || !cl->dev))
842 return -ENODEV;
843
844 dev = cl->dev;
845
846 rets = pm_runtime_get(dev->dev);
847 if (rets < 0 && rets != -EINPROGRESS) {
848 pm_runtime_put_noidle(dev->dev);
849 cl_err(dev, cl, "rpm: get failed %d\n", rets);
850 return rets;
851 }
852
853 cb = mei_io_cb_init(cl, MEI_FOP_CONNECT, file);
854 rets = cb ? 0 : -ENOMEM;
855 if (rets)
856 goto out;
857
858 /* run hbuf acquire last so we don't have to undo */
859 if (!mei_cl_is_other_connecting(cl) && mei_hbuf_acquire(dev)) {
860 cl->state = MEI_FILE_CONNECTING;
861 if (mei_hbm_cl_connect_req(dev, cl)) {
862 rets = -ENODEV;
863 goto out;
864 }
865 cl->timer_count = MEI_CONNECT_TIMEOUT;
866 list_add_tail(&cb->list, &dev->ctrl_rd_list.list);
867 } else {
868 cl->state = MEI_FILE_INITIALIZING;
869 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
870 }
871
872 mutex_unlock(&dev->device_lock);
873 wait_event_timeout(cl->wait,
874 (cl->state == MEI_FILE_CONNECTED ||
875 cl->state == MEI_FILE_DISCONNECTED),
876 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
877 mutex_lock(&dev->device_lock);
878
879 if (!mei_cl_is_connected(cl)) {
880 cl->state = MEI_FILE_DISCONNECTED;
881 /* something went really wrong */
882 if (!cl->status)
883 cl->status = -EFAULT;
884
885 mei_io_list_flush(&dev->ctrl_rd_list, cl);
886 mei_io_list_flush(&dev->ctrl_wr_list, cl);
887 }
888
889 rets = cl->status;
890
891 out:
892 cl_dbg(dev, cl, "rpm: autosuspend\n");
893 pm_runtime_mark_last_busy(dev->dev);
894 pm_runtime_put_autosuspend(dev->dev);
895
896 mei_io_cb_free(cb);
897 return rets;
898 }
899
900 /**
901 * mei_cl_alloc_linked - allocate and link host client
902 *
903 * @dev: the device structure
904 * @id: fixed host id or MEI_HOST_CLIENT_ID_ANY (-1) for generic one
905 *
906 * Return: cl on success ERR_PTR on failure
907 */
mei_cl_alloc_linked(struct mei_device * dev,int id)908 struct mei_cl *mei_cl_alloc_linked(struct mei_device *dev, int id)
909 {
910 struct mei_cl *cl;
911 int ret;
912
913 cl = mei_cl_allocate(dev);
914 if (!cl) {
915 ret = -ENOMEM;
916 goto err;
917 }
918
919 ret = mei_cl_link(cl, id);
920 if (ret)
921 goto err;
922
923 return cl;
924 err:
925 kfree(cl);
926 return ERR_PTR(ret);
927 }
928
929
930
931 /**
932 * mei_cl_flow_ctrl_creds - checks flow_control credits for cl.
933 *
934 * @cl: private data of the file object
935 *
936 * Return: 1 if mei_flow_ctrl_creds >0, 0 - otherwise.
937 * -ENOENT if mei_cl is not present
938 * -EINVAL if single_recv_buf == 0
939 */
mei_cl_flow_ctrl_creds(struct mei_cl * cl)940 int mei_cl_flow_ctrl_creds(struct mei_cl *cl)
941 {
942 struct mei_device *dev;
943 struct mei_me_client *me_cl;
944 int rets = 0;
945
946 if (WARN_ON(!cl || !cl->dev))
947 return -EINVAL;
948
949 dev = cl->dev;
950
951 if (cl->mei_flow_ctrl_creds > 0)
952 return 1;
953
954 me_cl = mei_me_cl_by_uuid_id(dev, &cl->cl_uuid, cl->me_client_id);
955 if (!me_cl) {
956 cl_err(dev, cl, "no such me client %d\n", cl->me_client_id);
957 return -ENOENT;
958 }
959
960 if (me_cl->mei_flow_ctrl_creds > 0) {
961 rets = 1;
962 if (WARN_ON(me_cl->props.single_recv_buf == 0))
963 rets = -EINVAL;
964 }
965 mei_me_cl_put(me_cl);
966 return rets;
967 }
968
969 /**
970 * mei_cl_flow_ctrl_reduce - reduces flow_control.
971 *
972 * @cl: private data of the file object
973 *
974 * Return:
975 * 0 on success
976 * -ENOENT when me client is not found
977 * -EINVAL when ctrl credits are <= 0
978 */
mei_cl_flow_ctrl_reduce(struct mei_cl * cl)979 int mei_cl_flow_ctrl_reduce(struct mei_cl *cl)
980 {
981 struct mei_device *dev;
982 struct mei_me_client *me_cl;
983 int rets;
984
985 if (WARN_ON(!cl || !cl->dev))
986 return -EINVAL;
987
988 dev = cl->dev;
989
990 me_cl = mei_me_cl_by_uuid_id(dev, &cl->cl_uuid, cl->me_client_id);
991 if (!me_cl) {
992 cl_err(dev, cl, "no such me client %d\n", cl->me_client_id);
993 return -ENOENT;
994 }
995
996 if (me_cl->props.single_recv_buf) {
997 if (WARN_ON(me_cl->mei_flow_ctrl_creds <= 0)) {
998 rets = -EINVAL;
999 goto out;
1000 }
1001 me_cl->mei_flow_ctrl_creds--;
1002 } else {
1003 if (WARN_ON(cl->mei_flow_ctrl_creds <= 0)) {
1004 rets = -EINVAL;
1005 goto out;
1006 }
1007 cl->mei_flow_ctrl_creds--;
1008 }
1009 rets = 0;
1010 out:
1011 mei_me_cl_put(me_cl);
1012 return rets;
1013 }
1014
1015 /**
1016 * mei_cl_read_start - the start read client message function.
1017 *
1018 * @cl: host client
1019 * @length: number of bytes to read
1020 * @fp: pointer to file structure
1021 *
1022 * Return: 0 on success, <0 on failure.
1023 */
mei_cl_read_start(struct mei_cl * cl,size_t length,struct file * fp)1024 int mei_cl_read_start(struct mei_cl *cl, size_t length, struct file *fp)
1025 {
1026 struct mei_device *dev;
1027 struct mei_cl_cb *cb;
1028 struct mei_me_client *me_cl;
1029 int rets;
1030
1031 if (WARN_ON(!cl || !cl->dev))
1032 return -ENODEV;
1033
1034 dev = cl->dev;
1035
1036 if (!mei_cl_is_connected(cl))
1037 return -ENODEV;
1038
1039 /* HW currently supports only one pending read */
1040 if (!list_empty(&cl->rd_pending))
1041 return -EBUSY;
1042
1043 me_cl = mei_me_cl_by_uuid_id(dev, &cl->cl_uuid, cl->me_client_id);
1044 if (!me_cl) {
1045 cl_err(dev, cl, "no such me client %d\n", cl->me_client_id);
1046 return -ENOTTY;
1047 }
1048 /* always allocate at least client max message */
1049 length = max_t(size_t, length, me_cl->props.max_msg_length);
1050 mei_me_cl_put(me_cl);
1051
1052 rets = pm_runtime_get(dev->dev);
1053 if (rets < 0 && rets != -EINPROGRESS) {
1054 pm_runtime_put_noidle(dev->dev);
1055 cl_err(dev, cl, "rpm: get failed %d\n", rets);
1056 return rets;
1057 }
1058
1059 cb = mei_cl_alloc_cb(cl, length, MEI_FOP_READ, fp);
1060 rets = cb ? 0 : -ENOMEM;
1061 if (rets)
1062 goto out;
1063
1064 if (mei_hbuf_acquire(dev)) {
1065 rets = mei_hbm_cl_flow_control_req(dev, cl);
1066 if (rets < 0)
1067 goto out;
1068
1069 list_add_tail(&cb->list, &cl->rd_pending);
1070 } else {
1071 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
1072 }
1073
1074 out:
1075 cl_dbg(dev, cl, "rpm: autosuspend\n");
1076 pm_runtime_mark_last_busy(dev->dev);
1077 pm_runtime_put_autosuspend(dev->dev);
1078
1079 if (rets)
1080 mei_io_cb_free(cb);
1081
1082 return rets;
1083 }
1084
1085 /**
1086 * mei_cl_irq_write - write a message to device
1087 * from the interrupt thread context
1088 *
1089 * @cl: client
1090 * @cb: callback block.
1091 * @cmpl_list: complete list.
1092 *
1093 * Return: 0, OK; otherwise error.
1094 */
mei_cl_irq_write(struct mei_cl * cl,struct mei_cl_cb * cb,struct mei_cl_cb * cmpl_list)1095 int mei_cl_irq_write(struct mei_cl *cl, struct mei_cl_cb *cb,
1096 struct mei_cl_cb *cmpl_list)
1097 {
1098 struct mei_device *dev;
1099 struct mei_msg_data *buf;
1100 struct mei_msg_hdr mei_hdr;
1101 size_t len;
1102 u32 msg_slots;
1103 int slots;
1104 int rets;
1105
1106 if (WARN_ON(!cl || !cl->dev))
1107 return -ENODEV;
1108
1109 dev = cl->dev;
1110
1111 buf = &cb->buf;
1112
1113 rets = mei_cl_flow_ctrl_creds(cl);
1114 if (rets < 0)
1115 return rets;
1116
1117 if (rets == 0) {
1118 cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
1119 return 0;
1120 }
1121
1122 slots = mei_hbuf_empty_slots(dev);
1123 len = buf->size - cb->buf_idx;
1124 msg_slots = mei_data2slots(len);
1125
1126 mei_hdr.host_addr = cl->host_client_id;
1127 mei_hdr.me_addr = cl->me_client_id;
1128 mei_hdr.reserved = 0;
1129 mei_hdr.internal = cb->internal;
1130
1131 if (slots >= msg_slots) {
1132 mei_hdr.length = len;
1133 mei_hdr.msg_complete = 1;
1134 /* Split the message only if we can write the whole host buffer */
1135 } else if (slots == dev->hbuf_depth) {
1136 msg_slots = slots;
1137 len = (slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
1138 mei_hdr.length = len;
1139 mei_hdr.msg_complete = 0;
1140 } else {
1141 /* wait for next time the host buffer is empty */
1142 return 0;
1143 }
1144
1145 cl_dbg(dev, cl, "buf: size = %d idx = %lu\n",
1146 cb->buf.size, cb->buf_idx);
1147
1148 rets = mei_write_message(dev, &mei_hdr, buf->data + cb->buf_idx);
1149 if (rets) {
1150 cl->status = rets;
1151 list_move_tail(&cb->list, &cmpl_list->list);
1152 return rets;
1153 }
1154
1155 cl->status = 0;
1156 cl->writing_state = MEI_WRITING;
1157 cb->buf_idx += mei_hdr.length;
1158 cb->completed = mei_hdr.msg_complete == 1;
1159
1160 if (mei_hdr.msg_complete) {
1161 if (mei_cl_flow_ctrl_reduce(cl))
1162 return -EIO;
1163 list_move_tail(&cb->list, &dev->write_waiting_list.list);
1164 }
1165
1166 return 0;
1167 }
1168
1169 /**
1170 * mei_cl_write - submit a write cb to mei device
1171 * assumes device_lock is locked
1172 *
1173 * @cl: host client
1174 * @cb: write callback with filled data
1175 * @blocking: block until completed
1176 *
1177 * Return: number of bytes sent on success, <0 on failure.
1178 */
mei_cl_write(struct mei_cl * cl,struct mei_cl_cb * cb,bool blocking)1179 int mei_cl_write(struct mei_cl *cl, struct mei_cl_cb *cb, bool blocking)
1180 {
1181 struct mei_device *dev;
1182 struct mei_msg_data *buf;
1183 struct mei_msg_hdr mei_hdr;
1184 int rets;
1185
1186
1187 if (WARN_ON(!cl || !cl->dev))
1188 return -ENODEV;
1189
1190 if (WARN_ON(!cb))
1191 return -EINVAL;
1192
1193 dev = cl->dev;
1194
1195
1196 buf = &cb->buf;
1197
1198 cl_dbg(dev, cl, "size=%d\n", buf->size);
1199
1200 rets = pm_runtime_get(dev->dev);
1201 if (rets < 0 && rets != -EINPROGRESS) {
1202 pm_runtime_put_noidle(dev->dev);
1203 cl_err(dev, cl, "rpm: get failed %d\n", rets);
1204 return rets;
1205 }
1206
1207 cb->buf_idx = 0;
1208 cl->writing_state = MEI_IDLE;
1209
1210 mei_hdr.host_addr = cl->host_client_id;
1211 mei_hdr.me_addr = cl->me_client_id;
1212 mei_hdr.reserved = 0;
1213 mei_hdr.msg_complete = 0;
1214 mei_hdr.internal = cb->internal;
1215
1216 rets = mei_cl_flow_ctrl_creds(cl);
1217 if (rets < 0)
1218 goto err;
1219
1220 if (rets == 0) {
1221 cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
1222 rets = buf->size;
1223 goto out;
1224 }
1225 if (!mei_hbuf_acquire(dev)) {
1226 cl_dbg(dev, cl, "Cannot acquire the host buffer: not sending.\n");
1227 rets = buf->size;
1228 goto out;
1229 }
1230
1231 /* Check for a maximum length */
1232 if (buf->size > mei_hbuf_max_len(dev)) {
1233 mei_hdr.length = mei_hbuf_max_len(dev);
1234 mei_hdr.msg_complete = 0;
1235 } else {
1236 mei_hdr.length = buf->size;
1237 mei_hdr.msg_complete = 1;
1238 }
1239
1240 rets = mei_write_message(dev, &mei_hdr, buf->data);
1241 if (rets)
1242 goto err;
1243
1244 cl->writing_state = MEI_WRITING;
1245 cb->buf_idx = mei_hdr.length;
1246 cb->completed = mei_hdr.msg_complete == 1;
1247
1248 out:
1249 if (mei_hdr.msg_complete) {
1250 rets = mei_cl_flow_ctrl_reduce(cl);
1251 if (rets < 0)
1252 goto err;
1253
1254 list_add_tail(&cb->list, &dev->write_waiting_list.list);
1255 } else {
1256 list_add_tail(&cb->list, &dev->write_list.list);
1257 }
1258
1259
1260 if (blocking && cl->writing_state != MEI_WRITE_COMPLETE) {
1261
1262 mutex_unlock(&dev->device_lock);
1263 rets = wait_event_interruptible(cl->tx_wait,
1264 cl->writing_state == MEI_WRITE_COMPLETE);
1265 mutex_lock(&dev->device_lock);
1266 /* wait_event_interruptible returns -ERESTARTSYS */
1267 if (rets) {
1268 if (signal_pending(current))
1269 rets = -EINTR;
1270 goto err;
1271 }
1272 }
1273
1274 rets = buf->size;
1275 err:
1276 cl_dbg(dev, cl, "rpm: autosuspend\n");
1277 pm_runtime_mark_last_busy(dev->dev);
1278 pm_runtime_put_autosuspend(dev->dev);
1279
1280 return rets;
1281 }
1282
1283
1284 /**
1285 * mei_cl_complete - processes completed operation for a client
1286 *
1287 * @cl: private data of the file object.
1288 * @cb: callback block.
1289 */
mei_cl_complete(struct mei_cl * cl,struct mei_cl_cb * cb)1290 void mei_cl_complete(struct mei_cl *cl, struct mei_cl_cb *cb)
1291 {
1292 if (cb->fop_type == MEI_FOP_WRITE) {
1293 mei_io_cb_free(cb);
1294 cb = NULL;
1295 cl->writing_state = MEI_WRITE_COMPLETE;
1296 if (waitqueue_active(&cl->tx_wait))
1297 wake_up_interruptible(&cl->tx_wait);
1298
1299 } else if (cb->fop_type == MEI_FOP_READ) {
1300 list_add_tail(&cb->list, &cl->rd_completed);
1301 if (waitqueue_active(&cl->rx_wait))
1302 wake_up_interruptible_all(&cl->rx_wait);
1303 else
1304 mei_cl_bus_rx_event(cl);
1305
1306 }
1307 }
1308
1309
1310 /**
1311 * mei_cl_all_disconnect - disconnect forcefully all connected clients
1312 *
1313 * @dev: mei device
1314 */
1315
mei_cl_all_disconnect(struct mei_device * dev)1316 void mei_cl_all_disconnect(struct mei_device *dev)
1317 {
1318 struct mei_cl *cl;
1319
1320 list_for_each_entry(cl, &dev->file_list, link) {
1321 cl->state = MEI_FILE_DISCONNECTED;
1322 cl->mei_flow_ctrl_creds = 0;
1323 cl->timer_count = 0;
1324 }
1325 }
1326
1327
1328 /**
1329 * mei_cl_all_wakeup - wake up all readers and writers they can be interrupted
1330 *
1331 * @dev: mei device
1332 */
mei_cl_all_wakeup(struct mei_device * dev)1333 void mei_cl_all_wakeup(struct mei_device *dev)
1334 {
1335 struct mei_cl *cl;
1336
1337 list_for_each_entry(cl, &dev->file_list, link) {
1338 if (waitqueue_active(&cl->rx_wait)) {
1339 cl_dbg(dev, cl, "Waking up reading client!\n");
1340 wake_up_interruptible(&cl->rx_wait);
1341 }
1342 if (waitqueue_active(&cl->tx_wait)) {
1343 cl_dbg(dev, cl, "Waking up writing client!\n");
1344 wake_up_interruptible(&cl->tx_wait);
1345 }
1346 }
1347 }
1348
1349 /**
1350 * mei_cl_all_write_clear - clear all pending writes
1351 *
1352 * @dev: mei device
1353 */
mei_cl_all_write_clear(struct mei_device * dev)1354 void mei_cl_all_write_clear(struct mei_device *dev)
1355 {
1356 mei_io_list_free(&dev->write_list, NULL);
1357 mei_io_list_free(&dev->write_waiting_list, NULL);
1358 }
1359
1360
1361