gro: Allow tunnel stacking in the case of FOU/GUE
[linux/fpc-iii.git] / drivers / misc / mei / client.c
blobb2b9f4382d771f45aea01bc22a602c648ba8f6e0
1 /*
3 * Intel Management Engine Interface (Intel MEI) Linux driver
4 * Copyright (c) 2003-2012, Intel Corporation.
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.
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.
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>
23 #include <linux/mei.h>
25 #include "mei_dev.h"
26 #include "hbm.h"
27 #include "client.h"
29 /**
30 * mei_me_cl_init - initialize me client
32 * @me_cl: me client
34 void mei_me_cl_init(struct mei_me_client *me_cl)
36 INIT_LIST_HEAD(&me_cl->list);
37 kref_init(&me_cl->refcnt);
40 /**
41 * mei_me_cl_get - increases me client refcount
43 * @me_cl: me client
45 * Locking: called under "dev->device_lock" lock
47 * Return: me client or NULL
49 struct mei_me_client *mei_me_cl_get(struct mei_me_client *me_cl)
51 if (me_cl && kref_get_unless_zero(&me_cl->refcnt))
52 return me_cl;
54 return NULL;
57 /**
58 * mei_me_cl_release - free me client
60 * Locking: called under "dev->device_lock" lock
62 * @ref: me_client refcount
64 static void mei_me_cl_release(struct kref *ref)
66 struct mei_me_client *me_cl =
67 container_of(ref, struct mei_me_client, refcnt);
69 kfree(me_cl);
72 /**
73 * mei_me_cl_put - decrease me client refcount and free client if necessary
75 * Locking: called under "dev->device_lock" lock
77 * @me_cl: me client
79 void mei_me_cl_put(struct mei_me_client *me_cl)
81 if (me_cl)
82 kref_put(&me_cl->refcnt, mei_me_cl_release);
85 /**
86 * __mei_me_cl_del - delete me client form the list and decrease
87 * reference counter
89 * @dev: mei device
90 * @me_cl: me client
92 * Locking: dev->me_clients_rwsem
94 static void __mei_me_cl_del(struct mei_device *dev, struct mei_me_client *me_cl)
96 if (!me_cl)
97 return;
99 list_del(&me_cl->list);
100 mei_me_cl_put(me_cl);
104 * mei_me_cl_add - add me client to the list
106 * @dev: mei device
107 * @me_cl: me client
109 void mei_me_cl_add(struct mei_device *dev, struct mei_me_client *me_cl)
111 down_write(&dev->me_clients_rwsem);
112 list_add(&me_cl->list, &dev->me_clients);
113 up_write(&dev->me_clients_rwsem);
117 * __mei_me_cl_by_uuid - locate me client by uuid
118 * increases ref count
120 * @dev: mei device
121 * @uuid: me client uuid
123 * Return: me client or NULL if not found
125 * Locking: dev->me_clients_rwsem
127 static struct mei_me_client *__mei_me_cl_by_uuid(struct mei_device *dev,
128 const uuid_le *uuid)
130 struct mei_me_client *me_cl;
131 const uuid_le *pn;
133 WARN_ON(!rwsem_is_locked(&dev->me_clients_rwsem));
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);
141 return NULL;
145 * mei_me_cl_by_uuid - locate me client by uuid
146 * increases ref count
148 * @dev: mei device
149 * @uuid: me client uuid
151 * Return: me client or NULL if not found
153 * Locking: dev->me_clients_rwsem
155 struct mei_me_client *mei_me_cl_by_uuid(struct mei_device *dev,
156 const uuid_le *uuid)
158 struct mei_me_client *me_cl;
160 down_read(&dev->me_clients_rwsem);
161 me_cl = __mei_me_cl_by_uuid(dev, uuid);
162 up_read(&dev->me_clients_rwsem);
164 return me_cl;
168 * mei_me_cl_by_id - locate me client by client id
169 * increases ref count
171 * @dev: the device structure
172 * @client_id: me client id
174 * Return: me client or NULL if not found
176 * Locking: dev->me_clients_rwsem
178 struct mei_me_client *mei_me_cl_by_id(struct mei_device *dev, u8 client_id)
181 struct mei_me_client *__me_cl, *me_cl = NULL;
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;
190 up_read(&dev->me_clients_rwsem);
192 return me_cl;
196 * __mei_me_cl_by_uuid_id - locate me client by client id and uuid
197 * increases ref count
199 * @dev: the device structure
200 * @uuid: me client uuid
201 * @client_id: me client id
203 * Return: me client or null if not found
205 * Locking: dev->me_clients_rwsem
207 static struct mei_me_client *__mei_me_cl_by_uuid_id(struct mei_device *dev,
208 const uuid_le *uuid, u8 client_id)
210 struct mei_me_client *me_cl;
211 const uuid_le *pn;
213 WARN_ON(!rwsem_is_locked(&dev->me_clients_rwsem));
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);
222 return NULL;
227 * mei_me_cl_by_uuid_id - locate me client by client id and uuid
228 * increases ref count
230 * @dev: the device structure
231 * @uuid: me client uuid
232 * @client_id: me client id
234 * Return: me client or null if not found
236 struct mei_me_client *mei_me_cl_by_uuid_id(struct mei_device *dev,
237 const uuid_le *uuid, u8 client_id)
239 struct mei_me_client *me_cl;
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);
245 return me_cl;
249 * mei_me_cl_rm_by_uuid - remove all me clients matching uuid
251 * @dev: the device structure
252 * @uuid: me client uuid
254 * Locking: called under "dev->device_lock" lock
256 void mei_me_cl_rm_by_uuid(struct mei_device *dev, const uuid_le *uuid)
258 struct mei_me_client *me_cl;
260 dev_dbg(dev->dev, "remove %pUl\n", uuid);
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);
269 * mei_me_cl_rm_by_uuid_id - remove all me clients matching client id
271 * @dev: the device structure
272 * @uuid: me client uuid
273 * @id: me client id
275 * Locking: called under "dev->device_lock" lock
277 void mei_me_cl_rm_by_uuid_id(struct mei_device *dev, const uuid_le *uuid, u8 id)
279 struct mei_me_client *me_cl;
281 dev_dbg(dev->dev, "remove %pUl %d\n", uuid, id);
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);
290 * mei_me_cl_rm_all - remove all me clients
292 * @dev: the device structure
294 * Locking: called under "dev->device_lock" lock
296 void mei_me_cl_rm_all(struct mei_device *dev)
298 struct mei_me_client *me_cl, *next;
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);
307 * mei_cl_cmp_id - tells if the clients are the same
309 * @cl1: host client 1
310 * @cl2: host client 2
312 * Return: true - if the clients has same host and me ids
313 * false - otherwise
315 static inline bool mei_cl_cmp_id(const struct mei_cl *cl1,
316 const struct mei_cl *cl2)
318 return cl1 && cl2 &&
319 (cl1->host_client_id == cl2->host_client_id) &&
320 (cl1->me_client_id == cl2->me_client_id);
324 * mei_io_cb_free - free mei_cb_private related memory
326 * @cb: mei callback struct
328 void mei_io_cb_free(struct mei_cl_cb *cb)
330 if (cb == NULL)
331 return;
333 list_del(&cb->list);
334 kfree(cb->buf.data);
335 kfree(cb);
339 * mei_io_cb_init - allocate and initialize io callback
341 * @cl: mei client
342 * @type: operation type
343 * @fp: pointer to file structure
345 * Return: mei_cl_cb pointer or NULL;
347 struct mei_cl_cb *mei_io_cb_init(struct mei_cl *cl, enum mei_cb_file_ops type,
348 struct file *fp)
350 struct mei_cl_cb *cb;
352 cb = kzalloc(sizeof(struct mei_cl_cb), GFP_KERNEL);
353 if (!cb)
354 return NULL;
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;
365 * __mei_io_list_flush - removes and frees cbs belonging to cl.
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
371 static void __mei_io_list_flush(struct mei_cl_cb *list,
372 struct mei_cl *cl, bool free)
374 struct mei_cl_cb *cb, *next;
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);
387 * mei_io_list_flush - removes list entry belonging to cl.
389 * @list: An instance of our list structure
390 * @cl: host client
392 void mei_io_list_flush(struct mei_cl_cb *list, struct mei_cl *cl)
394 __mei_io_list_flush(list, cl, false);
398 * mei_io_list_free - removes cb belonging to cl and free them
400 * @list: An instance of our list structure
401 * @cl: host client
403 static inline void mei_io_list_free(struct mei_cl_cb *list, struct mei_cl *cl)
405 __mei_io_list_flush(list, cl, true);
409 * mei_io_cb_alloc_buf - allocate callback buffer
411 * @cb: io callback structure
412 * @length: size of the buffer
414 * Return: 0 on success
415 * -EINVAL if cb is NULL
416 * -ENOMEM if allocation failed
418 int mei_io_cb_alloc_buf(struct mei_cl_cb *cb, size_t length)
420 if (!cb)
421 return -EINVAL;
423 if (length == 0)
424 return 0;
426 cb->buf.data = kmalloc(length, GFP_KERNEL);
427 if (!cb->buf.data)
428 return -ENOMEM;
429 cb->buf.size = length;
430 return 0;
434 * mei_cl_alloc_cb - a convenient wrapper for allocating read cb
436 * @cl: host client
437 * @length: size of the buffer
438 * @type: operation type
439 * @fp: associated file pointer (might be NULL)
441 * Return: cb on success and NULL on failure
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)
446 struct mei_cl_cb *cb;
448 cb = mei_io_cb_init(cl, type, fp);
449 if (!cb)
450 return NULL;
452 if (mei_io_cb_alloc_buf(cb, length)) {
453 mei_io_cb_free(cb);
454 return NULL;
457 return cb;
461 * mei_cl_read_cb - find this cl's callback in the read list
462 * for a specific file
464 * @cl: host client
465 * @fp: file pointer (matching cb file object), may be NULL
467 * Return: cb on success, NULL if cb is not found
469 struct mei_cl_cb *mei_cl_read_cb(const struct mei_cl *cl, const struct file *fp)
471 struct mei_cl_cb *cb;
473 list_for_each_entry(cb, &cl->rd_completed, list)
474 if (!fp || fp == cb->file_object)
475 return cb;
477 return NULL;
481 * mei_cl_read_cb_flush - free client's read pending and completed cbs
482 * for a specific file
484 * @cl: host client
485 * @fp: file pointer (matching cb file object), may be NULL
487 void mei_cl_read_cb_flush(const struct mei_cl *cl, const struct file *fp)
489 struct mei_cl_cb *cb, *next;
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);
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);
502 * mei_cl_flush_queues - flushes queue lists belonging to cl.
504 * @cl: host client
505 * @fp: file pointer (matching cb file object), may be NULL
507 * Return: 0 on success, -EINVAL if cl or cl->dev is NULL.
509 int mei_cl_flush_queues(struct mei_cl *cl, const struct file *fp)
511 struct mei_device *dev;
513 if (WARN_ON(!cl || !cl->dev))
514 return -EINVAL;
516 dev = cl->dev;
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);
526 mei_cl_read_cb_flush(cl, fp);
528 return 0;
533 * mei_cl_init - initializes cl.
535 * @cl: host client to be initialized
536 * @dev: mei device
538 void mei_cl_init(struct mei_cl *cl, struct mei_device *dev)
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;
553 * mei_cl_allocate - allocates cl structure and sets it up.
555 * @dev: mei device
556 * Return: The allocated file or NULL on failure
558 struct mei_cl *mei_cl_allocate(struct mei_device *dev)
560 struct mei_cl *cl;
562 cl = kmalloc(sizeof(struct mei_cl), GFP_KERNEL);
563 if (!cl)
564 return NULL;
566 mei_cl_init(cl, dev);
568 return cl;
572 * mei_cl_link - allocate host id in the host map
574 * @cl: host client
575 * @id: fixed host id or MEI_HOST_CLIENT_ID_ANY (-1) for generic one
577 * Return: 0 on success
578 * -EINVAL on incorrect values
579 * -EMFILE if open count exceeded.
581 int mei_cl_link(struct mei_cl *cl, int id)
583 struct mei_device *dev;
584 long open_handle_count;
586 if (WARN_ON(!cl || !cl->dev))
587 return -EINVAL;
589 dev = cl->dev;
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);
596 if (id >= MEI_CLIENTS_MAX) {
597 dev_err(dev->dev, "id exceeded %d", MEI_CLIENTS_MAX);
598 return -EMFILE;
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;
608 dev->open_handle_count++;
610 cl->host_client_id = id;
611 list_add_tail(&cl->link, &dev->file_list);
613 set_bit(id, dev->host_clients_map);
615 cl->state = MEI_FILE_INITIALIZING;
617 cl_dbg(dev, cl, "link cl\n");
618 return 0;
622 * mei_cl_unlink - remove me_cl from the list
624 * @cl: host client
626 * Return: always 0
628 int mei_cl_unlink(struct mei_cl *cl)
630 struct mei_device *dev;
632 /* don't shout on error exit path */
633 if (!cl)
634 return 0;
636 /* wd and amthif might not be initialized */
637 if (!cl->dev)
638 return 0;
640 dev = cl->dev;
642 cl_dbg(dev, cl, "unlink client");
644 if (dev->open_handle_count > 0)
645 dev->open_handle_count--;
647 /* never clear the 0 bit */
648 if (cl->host_client_id)
649 clear_bit(cl->host_client_id, dev->host_clients_map);
651 list_del_init(&cl->link);
653 cl->state = MEI_FILE_INITIALIZING;
655 return 0;
659 void mei_host_client_init(struct work_struct *work)
661 struct mei_device *dev =
662 container_of(work, struct mei_device, init_work);
663 struct mei_me_client *me_cl;
665 mutex_lock(&dev->device_lock);
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);
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);
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);
684 dev->dev_state = MEI_DEV_ENABLED;
685 dev->reset_count = 0;
686 mutex_unlock(&dev->device_lock);
688 pm_runtime_mark_last_busy(dev->dev);
689 dev_dbg(dev->dev, "rpm: autosuspend\n");
690 pm_runtime_autosuspend(dev->dev);
694 * mei_hbuf_acquire - try to acquire host buffer
696 * @dev: the device structure
697 * Return: true if host buffer was acquired
699 bool mei_hbuf_acquire(struct mei_device *dev)
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;
707 if (!dev->hbuf_is_ready) {
708 dev_dbg(dev->dev, "hbuf is not ready\n");
709 return false;
712 dev->hbuf_is_ready = false;
714 return true;
718 * mei_cl_disconnect - disconnect host client from the me one
720 * @cl: host client
722 * Locking: called under "dev->device_lock" lock
724 * Return: 0 on success, <0 on failure.
726 int mei_cl_disconnect(struct mei_cl *cl)
728 struct mei_device *dev;
729 struct mei_cl_cb *cb;
730 int rets;
732 if (WARN_ON(!cl || !cl->dev))
733 return -ENODEV;
735 dev = cl->dev;
737 cl_dbg(dev, cl, "disconnecting");
739 if (cl->state != MEI_FILE_DISCONNECTING)
740 return 0;
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;
749 cb = mei_io_cb_init(cl, MEI_FOP_DISCONNECT, NULL);
750 rets = cb ? 0 : -ENOMEM;
751 if (rets)
752 goto free;
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;
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);
768 mutex_unlock(&dev->device_lock);
770 wait_event_timeout(cl->wait,
771 MEI_FILE_DISCONNECTED == cl->state,
772 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
774 mutex_lock(&dev->device_lock);
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;
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);
791 mei_io_cb_free(cb);
792 return rets;
797 * mei_cl_is_other_connecting - checks if other
798 * client with the same me client id is connecting
800 * @cl: private data of the file object
802 * Return: true if other client is connected, false - otherwise.
804 bool mei_cl_is_other_connecting(struct mei_cl *cl)
806 struct mei_device *dev;
807 struct mei_cl *ocl; /* the other client */
809 if (WARN_ON(!cl || !cl->dev))
810 return false;
812 dev = cl->dev;
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;
822 return false;
826 * mei_cl_connect - connect host client to the me one
828 * @cl: host client
829 * @file: pointer to file structure
831 * Locking: called under "dev->device_lock" lock
833 * Return: 0 on success, <0 on failure.
835 int mei_cl_connect(struct mei_cl *cl, struct file *file)
837 struct mei_device *dev;
838 struct mei_cl_cb *cb;
839 int rets;
841 if (WARN_ON(!cl || !cl->dev))
842 return -ENODEV;
844 dev = cl->dev;
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;
853 cb = mei_io_cb_init(cl, MEI_FOP_CONNECT, file);
854 rets = cb ? 0 : -ENOMEM;
855 if (rets)
856 goto out;
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;
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);
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);
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;
885 mei_io_list_flush(&dev->ctrl_rd_list, cl);
886 mei_io_list_flush(&dev->ctrl_wr_list, cl);
889 rets = cl->status;
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);
896 mei_io_cb_free(cb);
897 return rets;
901 * mei_cl_alloc_linked - allocate and link host client
903 * @dev: the device structure
904 * @id: fixed host id or MEI_HOST_CLIENT_ID_ANY (-1) for generic one
906 * Return: cl on success ERR_PTR on failure
908 struct mei_cl *mei_cl_alloc_linked(struct mei_device *dev, int id)
910 struct mei_cl *cl;
911 int ret;
913 cl = mei_cl_allocate(dev);
914 if (!cl) {
915 ret = -ENOMEM;
916 goto err;
919 ret = mei_cl_link(cl, id);
920 if (ret)
921 goto err;
923 return cl;
924 err:
925 kfree(cl);
926 return ERR_PTR(ret);
932 * mei_cl_flow_ctrl_creds - checks flow_control credits for cl.
934 * @cl: private data of the file object
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
940 int mei_cl_flow_ctrl_creds(struct mei_cl *cl)
942 struct mei_device *dev;
943 struct mei_me_client *me_cl;
944 int rets = 0;
946 if (WARN_ON(!cl || !cl->dev))
947 return -EINVAL;
949 dev = cl->dev;
951 if (cl->mei_flow_ctrl_creds > 0)
952 return 1;
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;
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;
965 mei_me_cl_put(me_cl);
966 return rets;
970 * mei_cl_flow_ctrl_reduce - reduces flow_control.
972 * @cl: private data of the file object
974 * Return:
975 * 0 on success
976 * -ENOENT when me client is not found
977 * -EINVAL when ctrl credits are <= 0
979 int mei_cl_flow_ctrl_reduce(struct mei_cl *cl)
981 struct mei_device *dev;
982 struct mei_me_client *me_cl;
983 int rets;
985 if (WARN_ON(!cl || !cl->dev))
986 return -EINVAL;
988 dev = cl->dev;
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;
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;
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;
1007 cl->mei_flow_ctrl_creds--;
1009 rets = 0;
1010 out:
1011 mei_me_cl_put(me_cl);
1012 return rets;
1016 * mei_cl_read_start - the start read client message function.
1018 * @cl: host client
1019 * @length: number of bytes to read
1020 * @fp: pointer to file structure
1022 * Return: 0 on success, <0 on failure.
1024 int mei_cl_read_start(struct mei_cl *cl, size_t length, struct file *fp)
1026 struct mei_device *dev;
1027 struct mei_cl_cb *cb;
1028 struct mei_me_client *me_cl;
1029 int rets;
1031 if (WARN_ON(!cl || !cl->dev))
1032 return -ENODEV;
1034 dev = cl->dev;
1036 if (!mei_cl_is_connected(cl))
1037 return -ENODEV;
1039 /* HW currently supports only one pending read */
1040 if (!list_empty(&cl->rd_pending))
1041 return -EBUSY;
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;
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);
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;
1059 cb = mei_cl_alloc_cb(cl, length, MEI_FOP_READ, fp);
1060 rets = cb ? 0 : -ENOMEM;
1061 if (rets)
1062 goto out;
1064 if (mei_hbuf_acquire(dev)) {
1065 rets = mei_hbm_cl_flow_control_req(dev, cl);
1066 if (rets < 0)
1067 goto out;
1069 list_add_tail(&cb->list, &cl->rd_pending);
1070 } else {
1071 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
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);
1079 if (rets)
1080 mei_io_cb_free(cb);
1082 return rets;
1086 * mei_cl_irq_write - write a message to device
1087 * from the interrupt thread context
1089 * @cl: client
1090 * @cb: callback block.
1091 * @cmpl_list: complete list.
1093 * Return: 0, OK; otherwise error.
1095 int mei_cl_irq_write(struct mei_cl *cl, struct mei_cl_cb *cb,
1096 struct mei_cl_cb *cmpl_list)
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;
1106 if (WARN_ON(!cl || !cl->dev))
1107 return -ENODEV;
1109 dev = cl->dev;
1111 buf = &cb->buf;
1113 rets = mei_cl_flow_ctrl_creds(cl);
1114 if (rets < 0)
1115 return rets;
1117 if (rets == 0) {
1118 cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
1119 return 0;
1122 slots = mei_hbuf_empty_slots(dev);
1123 len = buf->size - cb->buf_idx;
1124 msg_slots = mei_data2slots(len);
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;
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;
1145 cl_dbg(dev, cl, "buf: size = %d idx = %lu\n",
1146 cb->buf.size, cb->buf_idx);
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;
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;
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);
1166 return 0;
1170 * mei_cl_write - submit a write cb to mei device
1171 * assumes device_lock is locked
1173 * @cl: host client
1174 * @cb: write callback with filled data
1175 * @blocking: block until completed
1177 * Return: number of bytes sent on success, <0 on failure.
1179 int mei_cl_write(struct mei_cl *cl, struct mei_cl_cb *cb, bool blocking)
1181 struct mei_device *dev;
1182 struct mei_msg_data *buf;
1183 struct mei_msg_hdr mei_hdr;
1184 int rets;
1187 if (WARN_ON(!cl || !cl->dev))
1188 return -ENODEV;
1190 if (WARN_ON(!cb))
1191 return -EINVAL;
1193 dev = cl->dev;
1196 buf = &cb->buf;
1198 cl_dbg(dev, cl, "size=%d\n", buf->size);
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;
1207 cb->buf_idx = 0;
1208 cl->writing_state = MEI_IDLE;
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;
1216 rets = mei_cl_flow_ctrl_creds(cl);
1217 if (rets < 0)
1218 goto err;
1220 if (rets == 0) {
1221 cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
1222 rets = buf->size;
1223 goto out;
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;
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;
1240 rets = mei_write_message(dev, &mei_hdr, buf->data);
1241 if (rets)
1242 goto err;
1244 cl->writing_state = MEI_WRITING;
1245 cb->buf_idx = mei_hdr.length;
1246 cb->completed = mei_hdr.msg_complete == 1;
1248 out:
1249 if (mei_hdr.msg_complete) {
1250 rets = mei_cl_flow_ctrl_reduce(cl);
1251 if (rets < 0)
1252 goto err;
1254 list_add_tail(&cb->list, &dev->write_waiting_list.list);
1255 } else {
1256 list_add_tail(&cb->list, &dev->write_list.list);
1260 if (blocking && cl->writing_state != MEI_WRITE_COMPLETE) {
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;
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);
1280 return rets;
1285 * mei_cl_complete - processes completed operation for a client
1287 * @cl: private data of the file object.
1288 * @cb: callback block.
1290 void mei_cl_complete(struct mei_cl *cl, struct mei_cl_cb *cb)
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);
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);
1311 * mei_cl_all_disconnect - disconnect forcefully all connected clients
1313 * @dev: mei device
1316 void mei_cl_all_disconnect(struct mei_device *dev)
1318 struct mei_cl *cl;
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;
1329 * mei_cl_all_wakeup - wake up all readers and writers they can be interrupted
1331 * @dev: mei device
1333 void mei_cl_all_wakeup(struct mei_device *dev)
1335 struct mei_cl *cl;
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);
1342 if (waitqueue_active(&cl->tx_wait)) {
1343 cl_dbg(dev, cl, "Waking up writing client!\n");
1344 wake_up_interruptible(&cl->tx_wait);
1350 * mei_cl_all_write_clear - clear all pending writes
1352 * @dev: mei device
1354 void mei_cl_all_write_clear(struct mei_device *dev)
1356 mei_io_list_free(&dev->write_list, NULL);
1357 mei_io_list_free(&dev->write_waiting_list, NULL);