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
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>
30 * mei_me_cl_init - initialize 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);
41 * mei_me_cl_get - increases me client refcount
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))
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);
73 * mei_me_cl_put - decrease me client refcount and free client if necessary
75 * Locking: called under "dev->device_lock" lock
79 void mei_me_cl_put(struct mei_me_client *me_cl)
82 kref_put(&me_cl->refcnt, mei_me_cl_release);
86 * __mei_me_cl_del - delete me client form the list and decrease
92 * Locking: dev->me_clients_rwsem
94 static void __mei_me_cl_del(struct mei_device *dev, struct mei_me_client *me_cl)
99 list_del(&me_cl->list);
100 mei_me_cl_put(me_cl);
104 * mei_me_cl_add - add me client to the list
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
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,
130 struct mei_me_client *me_cl;
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);
145 * mei_me_cl_by_uuid - locate me client by uuid
146 * increases ref count
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,
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);
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);
190 up_read(&dev->me_clients_rwsem);
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;
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);
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);
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
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
315 static inline bool mei_cl_cmp_id(const struct mei_cl *cl1,
316 const struct mei_cl *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)
339 * mei_io_cb_init - allocate and initialize io callback
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,
350 struct mei_cl_cb *cb;
352 cb = kzalloc(sizeof(struct mei_cl_cb), GFP_KERNEL);
356 INIT_LIST_HEAD(&cb->list);
357 cb->file_object = fp;
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);
387 * mei_io_list_flush - removes list entry belonging to cl.
389 * @list: An instance of our list structure
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
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)
426 cb->buf.data = kmalloc(length, GFP_KERNEL);
429 cb->buf.size = length;
434 * mei_cl_alloc_cb - a convenient wrapper for allocating read cb
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);
452 if (mei_io_cb_alloc_buf(cb, length)) {
461 * mei_cl_read_cb - find this cl's callback in the read list
462 * for a specific file
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)
481 * mei_cl_read_cb_flush - free client's read pending and completed cbs
482 * for a specific file
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)
496 list_for_each_entry_safe(cb, next, &cl->rd_pending, list)
497 if (!fp || fp == cb->file_object)
502 * mei_cl_flush_queues - flushes queue lists belonging to cl.
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))
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);
533 * mei_cl_init - initializes cl.
535 * @cl: host client to be initialized
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->state = MEI_FILE_INITIALIZING;
554 * mei_cl_allocate - allocates cl structure and sets it up.
557 * Return: The allocated file or NULL on failure
559 struct mei_cl *mei_cl_allocate(struct mei_device *dev)
563 cl = kmalloc(sizeof(struct mei_cl), GFP_KERNEL);
567 mei_cl_init(cl, dev);
573 * mei_cl_link - allocate host id in the host map
576 * @id: fixed host id or MEI_HOST_CLIENT_ID_ANY (-1) for generic one
578 * Return: 0 on success
579 * -EINVAL on incorrect values
580 * -EMFILE if open count exceeded.
582 int mei_cl_link(struct mei_cl *cl, int id)
584 struct mei_device *dev;
585 long open_handle_count;
587 if (WARN_ON(!cl || !cl->dev))
592 /* If Id is not assigned get one*/
593 if (id == MEI_HOST_CLIENT_ID_ANY)
594 id = find_first_zero_bit(dev->host_clients_map,
597 if (id >= MEI_CLIENTS_MAX) {
598 dev_err(dev->dev, "id exceeded %d", MEI_CLIENTS_MAX);
602 open_handle_count = dev->open_handle_count + dev->iamthif_open_count;
603 if (open_handle_count >= MEI_MAX_OPEN_HANDLE_COUNT) {
604 dev_err(dev->dev, "open_handle_count exceeded %d",
605 MEI_MAX_OPEN_HANDLE_COUNT);
609 dev->open_handle_count++;
611 cl->host_client_id = id;
612 list_add_tail(&cl->link, &dev->file_list);
614 set_bit(id, dev->host_clients_map);
616 cl->state = MEI_FILE_INITIALIZING;
618 cl_dbg(dev, cl, "link cl\n");
623 * mei_cl_unlink - remove me_cl from the list
629 int mei_cl_unlink(struct mei_cl *cl)
631 struct mei_device *dev;
633 /* don't shout on error exit path */
637 /* wd and amthif might not be initialized */
643 cl_dbg(dev, cl, "unlink client");
645 if (dev->open_handle_count > 0)
646 dev->open_handle_count--;
648 /* never clear the 0 bit */
649 if (cl->host_client_id)
650 clear_bit(cl->host_client_id, dev->host_clients_map);
652 list_del_init(&cl->link);
654 cl->state = MEI_FILE_INITIALIZING;
660 void mei_host_client_init(struct work_struct *work)
662 struct mei_device *dev =
663 container_of(work, struct mei_device, init_work);
664 struct mei_me_client *me_cl;
666 mutex_lock(&dev->device_lock);
669 me_cl = mei_me_cl_by_uuid(dev, &mei_amthif_guid);
671 mei_amthif_host_init(dev);
672 mei_me_cl_put(me_cl);
674 me_cl = mei_me_cl_by_uuid(dev, &mei_wd_guid);
676 mei_wd_host_init(dev);
677 mei_me_cl_put(me_cl);
679 me_cl = mei_me_cl_by_uuid(dev, &mei_nfc_guid);
681 mei_nfc_host_init(dev);
682 mei_me_cl_put(me_cl);
685 dev->dev_state = MEI_DEV_ENABLED;
686 dev->reset_count = 0;
687 mutex_unlock(&dev->device_lock);
689 pm_runtime_mark_last_busy(dev->dev);
690 dev_dbg(dev->dev, "rpm: autosuspend\n");
691 pm_runtime_autosuspend(dev->dev);
695 * mei_hbuf_acquire - try to acquire host buffer
697 * @dev: the device structure
698 * Return: true if host buffer was acquired
700 bool mei_hbuf_acquire(struct mei_device *dev)
702 if (mei_pg_state(dev) == MEI_PG_ON ||
703 dev->pg_event == MEI_PG_EVENT_WAIT) {
704 dev_dbg(dev->dev, "device is in pg\n");
708 if (!dev->hbuf_is_ready) {
709 dev_dbg(dev->dev, "hbuf is not ready\n");
713 dev->hbuf_is_ready = false;
719 * mei_cl_set_disconnected - set disconnected state and clear
720 * associated states and resources
724 void mei_cl_set_disconnected(struct mei_cl *cl)
726 struct mei_device *dev = cl->dev;
728 if (cl->state == MEI_FILE_DISCONNECTED ||
729 cl->state == MEI_FILE_INITIALIZING)
732 cl->state = MEI_FILE_DISCONNECTED;
733 mei_io_list_flush(&dev->ctrl_rd_list, cl);
734 mei_io_list_flush(&dev->ctrl_wr_list, cl);
735 cl->mei_flow_ctrl_creds = 0;
740 * mei_cl_send_disconnect - send disconnect request
743 * @cb: callback block
745 * Return: 0, OK; otherwise, error.
747 static int mei_cl_send_disconnect(struct mei_cl *cl, struct mei_cl_cb *cb)
749 struct mei_device *dev;
754 ret = mei_hbm_cl_disconnect_req(dev, cl);
757 cl->state = MEI_FILE_DISCONNECT_REPLY;
761 list_move_tail(&cb->list, &dev->ctrl_rd_list.list);
762 cl->timer_count = MEI_CONNECT_TIMEOUT;
768 * mei_cl_irq_disconnect - processes close related operation from
769 * interrupt thread context - send disconnect request
772 * @cb: callback block.
773 * @cmpl_list: complete list.
775 * Return: 0, OK; otherwise, error.
777 int mei_cl_irq_disconnect(struct mei_cl *cl, struct mei_cl_cb *cb,
778 struct mei_cl_cb *cmpl_list)
780 struct mei_device *dev = cl->dev;
785 msg_slots = mei_data2slots(sizeof(struct hbm_client_connect_request));
786 slots = mei_hbuf_empty_slots(dev);
788 if (slots < msg_slots)
791 ret = mei_cl_send_disconnect(cl, cb);
793 list_move_tail(&cb->list, &cmpl_list->list);
801 * mei_cl_disconnect - disconnect host client from the me one
805 * Locking: called under "dev->device_lock" lock
807 * Return: 0 on success, <0 on failure.
809 int mei_cl_disconnect(struct mei_cl *cl)
811 struct mei_device *dev;
812 struct mei_cl_cb *cb;
815 if (WARN_ON(!cl || !cl->dev))
820 cl_dbg(dev, cl, "disconnecting");
822 if (!mei_cl_is_connected(cl))
825 rets = pm_runtime_get(dev->dev);
826 if (rets < 0 && rets != -EINPROGRESS) {
827 pm_runtime_put_noidle(dev->dev);
828 cl_err(dev, cl, "rpm: get failed %d\n", rets);
832 cl->state = MEI_FILE_DISCONNECTING;
834 cb = mei_io_cb_init(cl, MEI_FOP_DISCONNECT, NULL);
835 rets = cb ? 0 : -ENOMEM;
839 cl_dbg(dev, cl, "add disconnect cb to control write list\n");
840 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
842 if (mei_hbuf_acquire(dev)) {
843 rets = mei_cl_send_disconnect(cl, cb);
845 cl_err(dev, cl, "failed to disconnect.\n");
850 mutex_unlock(&dev->device_lock);
851 wait_event_timeout(cl->wait, cl->state == MEI_FILE_DISCONNECT_REPLY,
852 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
853 mutex_lock(&dev->device_lock);
856 if (cl->state != MEI_FILE_DISCONNECT_REPLY) {
857 cl_dbg(dev, cl, "timeout on disconnect from FW client.\n");
862 /* we disconnect also on error */
863 mei_cl_set_disconnected(cl);
865 cl_dbg(dev, cl, "successfully disconnected from FW client.\n");
867 cl_dbg(dev, cl, "rpm: autosuspend\n");
868 pm_runtime_mark_last_busy(dev->dev);
869 pm_runtime_put_autosuspend(dev->dev);
877 * mei_cl_is_other_connecting - checks if other
878 * client with the same me client id is connecting
880 * @cl: private data of the file object
882 * Return: true if other client is connected, false - otherwise.
884 static bool mei_cl_is_other_connecting(struct mei_cl *cl)
886 struct mei_device *dev;
887 struct mei_cl_cb *cb;
891 list_for_each_entry(cb, &dev->ctrl_rd_list.list, list) {
892 if (cb->fop_type == MEI_FOP_CONNECT &&
893 cl->me_client_id == cb->cl->me_client_id)
901 * mei_cl_send_connect - send connect request
904 * @cb: callback block
906 * Return: 0, OK; otherwise, error.
908 static int mei_cl_send_connect(struct mei_cl *cl, struct mei_cl_cb *cb)
910 struct mei_device *dev;
915 ret = mei_hbm_cl_connect_req(dev, cl);
918 cl->state = MEI_FILE_DISCONNECT_REPLY;
922 list_move_tail(&cb->list, &dev->ctrl_rd_list.list);
923 cl->timer_count = MEI_CONNECT_TIMEOUT;
928 * mei_cl_irq_connect - send connect request in irq_thread context
931 * @cb: callback block
932 * @cmpl_list: complete list
934 * Return: 0, OK; otherwise, error.
936 int mei_cl_irq_connect(struct mei_cl *cl, struct mei_cl_cb *cb,
937 struct mei_cl_cb *cmpl_list)
939 struct mei_device *dev = cl->dev;
944 msg_slots = mei_data2slots(sizeof(struct hbm_client_connect_request));
945 slots = mei_hbuf_empty_slots(dev);
947 if (mei_cl_is_other_connecting(cl))
950 if (slots < msg_slots)
953 rets = mei_cl_send_connect(cl, cb);
955 list_move_tail(&cb->list, &cmpl_list->list);
961 * mei_cl_connect - connect host client to the me one
964 * @file: pointer to file structure
966 * Locking: called under "dev->device_lock" lock
968 * Return: 0 on success, <0 on failure.
970 int mei_cl_connect(struct mei_cl *cl, struct file *file)
972 struct mei_device *dev;
973 struct mei_cl_cb *cb;
976 if (WARN_ON(!cl || !cl->dev))
981 rets = pm_runtime_get(dev->dev);
982 if (rets < 0 && rets != -EINPROGRESS) {
983 pm_runtime_put_noidle(dev->dev);
984 cl_err(dev, cl, "rpm: get failed %d\n", rets);
988 cb = mei_io_cb_init(cl, MEI_FOP_CONNECT, file);
989 rets = cb ? 0 : -ENOMEM;
993 cl->state = MEI_FILE_CONNECTING;
994 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
996 /* run hbuf acquire last so we don't have to undo */
997 if (!mei_cl_is_other_connecting(cl) && mei_hbuf_acquire(dev)) {
998 rets = mei_cl_send_connect(cl, cb);
1003 mutex_unlock(&dev->device_lock);
1004 wait_event_timeout(cl->wait,
1005 (cl->state == MEI_FILE_CONNECTED ||
1006 cl->state == MEI_FILE_DISCONNECT_REPLY),
1007 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
1008 mutex_lock(&dev->device_lock);
1010 if (!mei_cl_is_connected(cl)) {
1011 /* timeout or something went really wrong */
1013 cl->status = -EFAULT;
1018 cl_dbg(dev, cl, "rpm: autosuspend\n");
1019 pm_runtime_mark_last_busy(dev->dev);
1020 pm_runtime_put_autosuspend(dev->dev);
1024 if (!mei_cl_is_connected(cl))
1025 mei_cl_set_disconnected(cl);
1031 * mei_cl_alloc_linked - allocate and link host client
1033 * @dev: the device structure
1034 * @id: fixed host id or MEI_HOST_CLIENT_ID_ANY (-1) for generic one
1036 * Return: cl on success ERR_PTR on failure
1038 struct mei_cl *mei_cl_alloc_linked(struct mei_device *dev, int id)
1043 cl = mei_cl_allocate(dev);
1049 ret = mei_cl_link(cl, id);
1056 return ERR_PTR(ret);
1062 * mei_cl_flow_ctrl_creds - checks flow_control credits for cl.
1064 * @cl: private data of the file object
1066 * Return: 1 if mei_flow_ctrl_creds >0, 0 - otherwise.
1067 * -ENOENT if mei_cl is not present
1068 * -EINVAL if single_recv_buf == 0
1070 int mei_cl_flow_ctrl_creds(struct mei_cl *cl)
1072 struct mei_device *dev;
1073 struct mei_me_client *me_cl;
1076 if (WARN_ON(!cl || !cl->dev))
1081 if (cl->mei_flow_ctrl_creds > 0)
1084 me_cl = mei_me_cl_by_uuid_id(dev, &cl->cl_uuid, cl->me_client_id);
1086 cl_err(dev, cl, "no such me client %d\n", cl->me_client_id);
1090 if (me_cl->mei_flow_ctrl_creds > 0) {
1092 if (WARN_ON(me_cl->props.single_recv_buf == 0))
1095 mei_me_cl_put(me_cl);
1100 * mei_cl_flow_ctrl_reduce - reduces flow_control.
1102 * @cl: private data of the file object
1106 * -ENOENT when me client is not found
1107 * -EINVAL when ctrl credits are <= 0
1109 int mei_cl_flow_ctrl_reduce(struct mei_cl *cl)
1111 struct mei_device *dev;
1112 struct mei_me_client *me_cl;
1115 if (WARN_ON(!cl || !cl->dev))
1120 me_cl = mei_me_cl_by_uuid_id(dev, &cl->cl_uuid, cl->me_client_id);
1122 cl_err(dev, cl, "no such me client %d\n", cl->me_client_id);
1126 if (me_cl->props.single_recv_buf) {
1127 if (WARN_ON(me_cl->mei_flow_ctrl_creds <= 0)) {
1131 me_cl->mei_flow_ctrl_creds--;
1133 if (WARN_ON(cl->mei_flow_ctrl_creds <= 0)) {
1137 cl->mei_flow_ctrl_creds--;
1141 mei_me_cl_put(me_cl);
1146 * mei_cl_read_start - the start read client message function.
1149 * @length: number of bytes to read
1150 * @fp: pointer to file structure
1152 * Return: 0 on success, <0 on failure.
1154 int mei_cl_read_start(struct mei_cl *cl, size_t length, struct file *fp)
1156 struct mei_device *dev;
1157 struct mei_cl_cb *cb;
1158 struct mei_me_client *me_cl;
1161 if (WARN_ON(!cl || !cl->dev))
1166 if (!mei_cl_is_connected(cl))
1169 /* HW currently supports only one pending read */
1170 if (!list_empty(&cl->rd_pending))
1173 me_cl = mei_me_cl_by_uuid_id(dev, &cl->cl_uuid, cl->me_client_id);
1175 cl_err(dev, cl, "no such me client %d\n", cl->me_client_id);
1178 /* always allocate at least client max message */
1179 length = max_t(size_t, length, me_cl->props.max_msg_length);
1180 mei_me_cl_put(me_cl);
1182 rets = pm_runtime_get(dev->dev);
1183 if (rets < 0 && rets != -EINPROGRESS) {
1184 pm_runtime_put_noidle(dev->dev);
1185 cl_err(dev, cl, "rpm: get failed %d\n", rets);
1189 cb = mei_cl_alloc_cb(cl, length, MEI_FOP_READ, fp);
1190 rets = cb ? 0 : -ENOMEM;
1194 if (mei_hbuf_acquire(dev)) {
1195 rets = mei_hbm_cl_flow_control_req(dev, cl);
1199 list_add_tail(&cb->list, &cl->rd_pending);
1201 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
1205 cl_dbg(dev, cl, "rpm: autosuspend\n");
1206 pm_runtime_mark_last_busy(dev->dev);
1207 pm_runtime_put_autosuspend(dev->dev);
1216 * mei_cl_irq_write - write a message to device
1217 * from the interrupt thread context
1220 * @cb: callback block.
1221 * @cmpl_list: complete list.
1223 * Return: 0, OK; otherwise error.
1225 int mei_cl_irq_write(struct mei_cl *cl, struct mei_cl_cb *cb,
1226 struct mei_cl_cb *cmpl_list)
1228 struct mei_device *dev;
1229 struct mei_msg_data *buf;
1230 struct mei_msg_hdr mei_hdr;
1236 if (WARN_ON(!cl || !cl->dev))
1243 rets = mei_cl_flow_ctrl_creds(cl);
1248 cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
1252 slots = mei_hbuf_empty_slots(dev);
1253 len = buf->size - cb->buf_idx;
1254 msg_slots = mei_data2slots(len);
1256 mei_hdr.host_addr = cl->host_client_id;
1257 mei_hdr.me_addr = cl->me_client_id;
1258 mei_hdr.reserved = 0;
1259 mei_hdr.internal = cb->internal;
1261 if (slots >= msg_slots) {
1262 mei_hdr.length = len;
1263 mei_hdr.msg_complete = 1;
1264 /* Split the message only if we can write the whole host buffer */
1265 } else if (slots == dev->hbuf_depth) {
1267 len = (slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
1268 mei_hdr.length = len;
1269 mei_hdr.msg_complete = 0;
1271 /* wait for next time the host buffer is empty */
1275 cl_dbg(dev, cl, "buf: size = %d idx = %lu\n",
1276 cb->buf.size, cb->buf_idx);
1278 rets = mei_write_message(dev, &mei_hdr, buf->data + cb->buf_idx);
1281 list_move_tail(&cb->list, &cmpl_list->list);
1286 cl->writing_state = MEI_WRITING;
1287 cb->buf_idx += mei_hdr.length;
1288 cb->completed = mei_hdr.msg_complete == 1;
1290 if (mei_hdr.msg_complete) {
1291 if (mei_cl_flow_ctrl_reduce(cl))
1293 list_move_tail(&cb->list, &dev->write_waiting_list.list);
1300 * mei_cl_write - submit a write cb to mei device
1301 * assumes device_lock is locked
1304 * @cb: write callback with filled data
1305 * @blocking: block until completed
1307 * Return: number of bytes sent on success, <0 on failure.
1309 int mei_cl_write(struct mei_cl *cl, struct mei_cl_cb *cb, bool blocking)
1311 struct mei_device *dev;
1312 struct mei_msg_data *buf;
1313 struct mei_msg_hdr mei_hdr;
1317 if (WARN_ON(!cl || !cl->dev))
1328 cl_dbg(dev, cl, "size=%d\n", buf->size);
1330 rets = pm_runtime_get(dev->dev);
1331 if (rets < 0 && rets != -EINPROGRESS) {
1332 pm_runtime_put_noidle(dev->dev);
1333 cl_err(dev, cl, "rpm: get failed %d\n", rets);
1338 cl->writing_state = MEI_IDLE;
1340 mei_hdr.host_addr = cl->host_client_id;
1341 mei_hdr.me_addr = cl->me_client_id;
1342 mei_hdr.reserved = 0;
1343 mei_hdr.msg_complete = 0;
1344 mei_hdr.internal = cb->internal;
1346 rets = mei_cl_flow_ctrl_creds(cl);
1351 cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
1355 if (!mei_hbuf_acquire(dev)) {
1356 cl_dbg(dev, cl, "Cannot acquire the host buffer: not sending.\n");
1361 /* Check for a maximum length */
1362 if (buf->size > mei_hbuf_max_len(dev)) {
1363 mei_hdr.length = mei_hbuf_max_len(dev);
1364 mei_hdr.msg_complete = 0;
1366 mei_hdr.length = buf->size;
1367 mei_hdr.msg_complete = 1;
1370 rets = mei_write_message(dev, &mei_hdr, buf->data);
1374 cl->writing_state = MEI_WRITING;
1375 cb->buf_idx = mei_hdr.length;
1376 cb->completed = mei_hdr.msg_complete == 1;
1379 if (mei_hdr.msg_complete) {
1380 rets = mei_cl_flow_ctrl_reduce(cl);
1384 list_add_tail(&cb->list, &dev->write_waiting_list.list);
1386 list_add_tail(&cb->list, &dev->write_list.list);
1390 if (blocking && cl->writing_state != MEI_WRITE_COMPLETE) {
1392 mutex_unlock(&dev->device_lock);
1393 rets = wait_event_interruptible(cl->tx_wait,
1394 cl->writing_state == MEI_WRITE_COMPLETE);
1395 mutex_lock(&dev->device_lock);
1396 /* wait_event_interruptible returns -ERESTARTSYS */
1398 if (signal_pending(current))
1406 cl_dbg(dev, cl, "rpm: autosuspend\n");
1407 pm_runtime_mark_last_busy(dev->dev);
1408 pm_runtime_put_autosuspend(dev->dev);
1415 * mei_cl_complete - processes completed operation for a client
1417 * @cl: private data of the file object.
1418 * @cb: callback block.
1420 void mei_cl_complete(struct mei_cl *cl, struct mei_cl_cb *cb)
1422 switch (cb->fop_type) {
1425 cl->writing_state = MEI_WRITE_COMPLETE;
1426 if (waitqueue_active(&cl->tx_wait))
1427 wake_up_interruptible(&cl->tx_wait);
1431 list_add_tail(&cb->list, &cl->rd_completed);
1432 if (waitqueue_active(&cl->rx_wait))
1433 wake_up_interruptible_all(&cl->rx_wait);
1435 mei_cl_bus_rx_event(cl);
1438 case MEI_FOP_CONNECT:
1439 case MEI_FOP_DISCONNECT:
1440 if (waitqueue_active(&cl->wait))
1451 * mei_cl_all_disconnect - disconnect forcefully all connected clients
1455 void mei_cl_all_disconnect(struct mei_device *dev)
1459 list_for_each_entry(cl, &dev->file_list, link)
1460 mei_cl_set_disconnected(cl);
1465 * mei_cl_all_wakeup - wake up all readers and writers they can be interrupted
1469 void mei_cl_all_wakeup(struct mei_device *dev)
1473 list_for_each_entry(cl, &dev->file_list, link) {
1474 if (waitqueue_active(&cl->rx_wait)) {
1475 cl_dbg(dev, cl, "Waking up reading client!\n");
1476 wake_up_interruptible(&cl->rx_wait);
1478 if (waitqueue_active(&cl->tx_wait)) {
1479 cl_dbg(dev, cl, "Waking up writing client!\n");
1480 wake_up_interruptible(&cl->tx_wait);
1486 * mei_cl_all_write_clear - clear all pending writes
1490 void mei_cl_all_write_clear(struct mei_device *dev)
1492 mei_io_list_free(&dev->write_list, NULL);
1493 mei_io_list_free(&dev->write_waiting_list, NULL);