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)
52 kref_get(&me_cl->refcnt);
58 * mei_me_cl_release - unlink and 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);
68 list_del(&me_cl->list);
72 * mei_me_cl_put - decrease me client refcount and free client if necessary
74 * Locking: called under "dev->device_lock" lock
78 void mei_me_cl_put(struct mei_me_client *me_cl)
81 kref_put(&me_cl->refcnt, mei_me_cl_release);
85 * mei_me_cl_by_uuid - locate me client by uuid
89 * @uuid: me client uuid
91 * Locking: called under "dev->device_lock" lock
93 * Return: me client or NULL if not found
95 struct mei_me_client *mei_me_cl_by_uuid(const struct mei_device *dev,
98 struct mei_me_client *me_cl;
100 list_for_each_entry(me_cl, &dev->me_clients, list)
101 if (uuid_le_cmp(*uuid, me_cl->props.protocol_name) == 0)
102 return mei_me_cl_get(me_cl);
108 * mei_me_cl_by_id - locate me client by client id
109 * increases ref count
111 * @dev: the device structure
112 * @client_id: me client id
114 * Locking: called under "dev->device_lock" lock
116 * Return: me client or NULL if not found
118 struct mei_me_client *mei_me_cl_by_id(struct mei_device *dev, u8 client_id)
121 struct mei_me_client *me_cl;
123 list_for_each_entry(me_cl, &dev->me_clients, list)
124 if (me_cl->client_id == client_id)
125 return mei_me_cl_get(me_cl);
131 * mei_me_cl_by_uuid_id - locate me client by client id and uuid
132 * increases ref count
134 * @dev: the device structure
135 * @uuid: me client uuid
136 * @client_id: me client id
138 * Locking: called under "dev->device_lock" lock
140 * Return: me client or NULL if not found
142 struct mei_me_client *mei_me_cl_by_uuid_id(struct mei_device *dev,
143 const uuid_le *uuid, u8 client_id)
145 struct mei_me_client *me_cl;
147 list_for_each_entry(me_cl, &dev->me_clients, list)
148 if (uuid_le_cmp(*uuid, me_cl->props.protocol_name) == 0 &&
149 me_cl->client_id == client_id)
150 return mei_me_cl_get(me_cl);
156 * mei_me_cl_rm_by_uuid - remove all me clients matching uuid
158 * @dev: the device structure
159 * @uuid: me client uuid
161 * Locking: called under "dev->device_lock" lock
163 void mei_me_cl_rm_by_uuid(struct mei_device *dev, const uuid_le *uuid)
165 struct mei_me_client *me_cl, *next;
167 dev_dbg(dev->dev, "remove %pUl\n", uuid);
168 list_for_each_entry_safe(me_cl, next, &dev->me_clients, list)
169 if (uuid_le_cmp(*uuid, me_cl->props.protocol_name) == 0)
170 mei_me_cl_put(me_cl);
174 * mei_me_cl_rm_by_uuid_id - remove all me clients matching client id
176 * @dev: the device structure
177 * @uuid: me client uuid
180 * Locking: called under "dev->device_lock" lock
182 void mei_me_cl_rm_by_uuid_id(struct mei_device *dev, const uuid_le *uuid, u8 id)
184 struct mei_me_client *me_cl, *next;
187 dev_dbg(dev->dev, "remove %pUl %d\n", uuid, id);
188 list_for_each_entry_safe(me_cl, next, &dev->me_clients, list) {
189 pn = &me_cl->props.protocol_name;
190 if (me_cl->client_id == id && uuid_le_cmp(*uuid, *pn) == 0)
191 mei_me_cl_put(me_cl);
196 * mei_me_cl_rm_all - remove all me clients
198 * @dev: the device structure
200 * Locking: called under "dev->device_lock" lock
202 void mei_me_cl_rm_all(struct mei_device *dev)
204 struct mei_me_client *me_cl, *next;
206 list_for_each_entry_safe(me_cl, next, &dev->me_clients, list)
207 mei_me_cl_put(me_cl);
213 * mei_cl_cmp_id - tells if the clients are the same
215 * @cl1: host client 1
216 * @cl2: host client 2
218 * Return: true - if the clients has same host and me ids
221 static inline bool mei_cl_cmp_id(const struct mei_cl *cl1,
222 const struct mei_cl *cl2)
225 (cl1->host_client_id == cl2->host_client_id) &&
226 (cl1->me_client_id == cl2->me_client_id);
230 * mei_io_list_flush - removes cbs belonging to cl.
232 * @list: an instance of our list structure
233 * @cl: host client, can be NULL for flushing the whole list
234 * @free: whether to free the cbs
236 static void __mei_io_list_flush(struct mei_cl_cb *list,
237 struct mei_cl *cl, bool free)
239 struct mei_cl_cb *cb;
240 struct mei_cl_cb *next;
242 /* enable removing everything if no cl is specified */
243 list_for_each_entry_safe(cb, next, &list->list, list) {
244 if (!cl || mei_cl_cmp_id(cl, cb->cl)) {
253 * mei_io_list_flush - removes list entry belonging to cl.
255 * @list: An instance of our list structure
258 void mei_io_list_flush(struct mei_cl_cb *list, struct mei_cl *cl)
260 __mei_io_list_flush(list, cl, false);
265 * mei_io_list_free - removes cb belonging to cl and free them
267 * @list: An instance of our list structure
270 static inline void mei_io_list_free(struct mei_cl_cb *list, struct mei_cl *cl)
272 __mei_io_list_flush(list, cl, true);
276 * mei_io_cb_free - free mei_cb_private related memory
278 * @cb: mei callback struct
280 void mei_io_cb_free(struct mei_cl_cb *cb)
285 kfree(cb->request_buffer.data);
286 kfree(cb->response_buffer.data);
291 * mei_io_cb_init - allocate and initialize io callback
294 * @fp: pointer to file structure
296 * Return: mei_cl_cb pointer or NULL;
298 struct mei_cl_cb *mei_io_cb_init(struct mei_cl *cl, struct file *fp)
300 struct mei_cl_cb *cb;
302 cb = kzalloc(sizeof(struct mei_cl_cb), GFP_KERNEL);
306 mei_io_list_init(cb);
308 cb->file_object = fp;
315 * mei_io_cb_alloc_req_buf - allocate request buffer
317 * @cb: io callback structure
318 * @length: size of the buffer
320 * Return: 0 on success
321 * -EINVAL if cb is NULL
322 * -ENOMEM if allocation failed
324 int mei_io_cb_alloc_req_buf(struct mei_cl_cb *cb, size_t length)
332 cb->request_buffer.data = kmalloc(length, GFP_KERNEL);
333 if (!cb->request_buffer.data)
335 cb->request_buffer.size = length;
339 * mei_io_cb_alloc_resp_buf - allocate response buffer
341 * @cb: io callback structure
342 * @length: size of the buffer
344 * Return: 0 on success
345 * -EINVAL if cb is NULL
346 * -ENOMEM if allocation failed
348 int mei_io_cb_alloc_resp_buf(struct mei_cl_cb *cb, size_t length)
356 cb->response_buffer.data = kmalloc(length, GFP_KERNEL);
357 if (!cb->response_buffer.data)
359 cb->response_buffer.size = length;
366 * mei_cl_flush_queues - flushes queue lists belonging to cl.
370 * Return: 0 on success, -EINVAL if cl or cl->dev is NULL.
372 int mei_cl_flush_queues(struct mei_cl *cl)
374 struct mei_device *dev;
376 if (WARN_ON(!cl || !cl->dev))
381 cl_dbg(dev, cl, "remove list entry belonging to cl\n");
382 mei_io_list_flush(&cl->dev->read_list, cl);
383 mei_io_list_free(&cl->dev->write_list, cl);
384 mei_io_list_free(&cl->dev->write_waiting_list, cl);
385 mei_io_list_flush(&cl->dev->ctrl_wr_list, cl);
386 mei_io_list_flush(&cl->dev->ctrl_rd_list, cl);
387 mei_io_list_flush(&cl->dev->amthif_cmd_list, cl);
388 mei_io_list_flush(&cl->dev->amthif_rd_complete_list, cl);
394 * mei_cl_init - initializes cl.
396 * @cl: host client to be initialized
399 void mei_cl_init(struct mei_cl *cl, struct mei_device *dev)
401 memset(cl, 0, sizeof(struct mei_cl));
402 init_waitqueue_head(&cl->wait);
403 init_waitqueue_head(&cl->rx_wait);
404 init_waitqueue_head(&cl->tx_wait);
405 INIT_LIST_HEAD(&cl->link);
406 INIT_LIST_HEAD(&cl->device_link);
407 cl->reading_state = MEI_IDLE;
408 cl->writing_state = MEI_IDLE;
413 * mei_cl_allocate - allocates cl structure and sets it up.
416 * Return: The allocated file or NULL on failure
418 struct mei_cl *mei_cl_allocate(struct mei_device *dev)
422 cl = kmalloc(sizeof(struct mei_cl), GFP_KERNEL);
426 mei_cl_init(cl, dev);
432 * mei_cl_find_read_cb - find this cl's callback in the read list
436 * Return: cb on success, NULL on error
438 struct mei_cl_cb *mei_cl_find_read_cb(struct mei_cl *cl)
440 struct mei_device *dev = cl->dev;
441 struct mei_cl_cb *cb;
443 list_for_each_entry(cb, &dev->read_list.list, list)
444 if (mei_cl_cmp_id(cl, cb->cl))
449 /** mei_cl_link: allocate host id in the host map
452 * @id - fixed host id or -1 for generic one
454 * Return: 0 on success
455 * -EINVAL on incorrect values
456 * -ENONET if client not found
458 int mei_cl_link(struct mei_cl *cl, int id)
460 struct mei_device *dev;
461 long open_handle_count;
463 if (WARN_ON(!cl || !cl->dev))
468 /* If Id is not assigned get one*/
469 if (id == MEI_HOST_CLIENT_ID_ANY)
470 id = find_first_zero_bit(dev->host_clients_map,
473 if (id >= MEI_CLIENTS_MAX) {
474 dev_err(dev->dev, "id exceeded %d", MEI_CLIENTS_MAX);
478 open_handle_count = dev->open_handle_count + dev->iamthif_open_count;
479 if (open_handle_count >= MEI_MAX_OPEN_HANDLE_COUNT) {
480 dev_err(dev->dev, "open_handle_count exceeded %d",
481 MEI_MAX_OPEN_HANDLE_COUNT);
485 dev->open_handle_count++;
487 cl->host_client_id = id;
488 list_add_tail(&cl->link, &dev->file_list);
490 set_bit(id, dev->host_clients_map);
492 cl->state = MEI_FILE_INITIALIZING;
494 cl_dbg(dev, cl, "link cl\n");
499 * mei_cl_unlink - remove me_cl from the list
505 int mei_cl_unlink(struct mei_cl *cl)
507 struct mei_device *dev;
509 /* don't shout on error exit path */
513 /* wd and amthif might not be initialized */
519 cl_dbg(dev, cl, "unlink client");
521 if (dev->open_handle_count > 0)
522 dev->open_handle_count--;
524 /* never clear the 0 bit */
525 if (cl->host_client_id)
526 clear_bit(cl->host_client_id, dev->host_clients_map);
528 list_del_init(&cl->link);
530 cl->state = MEI_FILE_INITIALIZING;
536 void mei_host_client_init(struct work_struct *work)
538 struct mei_device *dev = container_of(work,
539 struct mei_device, init_work);
540 struct mei_me_client *me_cl;
541 struct mei_client_properties *props;
543 mutex_lock(&dev->device_lock);
545 list_for_each_entry(me_cl, &dev->me_clients, list) {
546 props = &me_cl->props;
548 if (!uuid_le_cmp(props->protocol_name, mei_amthif_guid))
549 mei_amthif_host_init(dev);
550 else if (!uuid_le_cmp(props->protocol_name, mei_wd_guid))
551 mei_wd_host_init(dev);
552 else if (!uuid_le_cmp(props->protocol_name, mei_nfc_guid))
553 mei_nfc_host_init(dev);
557 dev->dev_state = MEI_DEV_ENABLED;
558 dev->reset_count = 0;
560 mutex_unlock(&dev->device_lock);
562 pm_runtime_mark_last_busy(dev->dev);
563 dev_dbg(dev->dev, "rpm: autosuspend\n");
564 pm_runtime_autosuspend(dev->dev);
568 * mei_hbuf_acquire - try to acquire host buffer
570 * @dev: the device structure
571 * Return: true if host buffer was acquired
573 bool mei_hbuf_acquire(struct mei_device *dev)
575 if (mei_pg_state(dev) == MEI_PG_ON ||
576 dev->pg_event == MEI_PG_EVENT_WAIT) {
577 dev_dbg(dev->dev, "device is in pg\n");
581 if (!dev->hbuf_is_ready) {
582 dev_dbg(dev->dev, "hbuf is not ready\n");
586 dev->hbuf_is_ready = false;
592 * mei_cl_disconnect - disconnect host client from the me one
596 * Locking: called under "dev->device_lock" lock
598 * Return: 0 on success, <0 on failure.
600 int mei_cl_disconnect(struct mei_cl *cl)
602 struct mei_device *dev;
603 struct mei_cl_cb *cb;
606 if (WARN_ON(!cl || !cl->dev))
611 cl_dbg(dev, cl, "disconnecting");
613 if (cl->state != MEI_FILE_DISCONNECTING)
616 rets = pm_runtime_get(dev->dev);
617 if (rets < 0 && rets != -EINPROGRESS) {
618 pm_runtime_put_noidle(dev->dev);
619 cl_err(dev, cl, "rpm: get failed %d\n", rets);
623 cb = mei_io_cb_init(cl, NULL);
629 cb->fop_type = MEI_FOP_DISCONNECT;
631 if (mei_hbuf_acquire(dev)) {
632 if (mei_hbm_cl_disconnect_req(dev, cl)) {
634 cl_err(dev, cl, "failed to disconnect.\n");
637 cl->timer_count = MEI_CONNECT_TIMEOUT;
638 mdelay(10); /* Wait for hardware disconnection ready */
639 list_add_tail(&cb->list, &dev->ctrl_rd_list.list);
641 cl_dbg(dev, cl, "add disconnect cb to control write list\n");
642 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
645 mutex_unlock(&dev->device_lock);
647 wait_event_timeout(cl->wait,
648 MEI_FILE_DISCONNECTED == cl->state,
649 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
651 mutex_lock(&dev->device_lock);
653 if (MEI_FILE_DISCONNECTED == cl->state) {
655 cl_dbg(dev, cl, "successfully disconnected from FW client.\n");
657 cl_dbg(dev, cl, "timeout on disconnect from FW client.\n");
661 mei_io_list_flush(&dev->ctrl_rd_list, cl);
662 mei_io_list_flush(&dev->ctrl_wr_list, cl);
664 cl_dbg(dev, cl, "rpm: autosuspend\n");
665 pm_runtime_mark_last_busy(dev->dev);
666 pm_runtime_put_autosuspend(dev->dev);
674 * mei_cl_is_other_connecting - checks if other
675 * client with the same me client id is connecting
677 * @cl: private data of the file object
679 * Return: true if other client is connected, false - otherwise.
681 bool mei_cl_is_other_connecting(struct mei_cl *cl)
683 struct mei_device *dev;
684 struct mei_cl *ocl; /* the other client */
686 if (WARN_ON(!cl || !cl->dev))
691 list_for_each_entry(ocl, &dev->file_list, link) {
692 if (ocl->state == MEI_FILE_CONNECTING &&
694 cl->me_client_id == ocl->me_client_id)
703 * mei_cl_connect - connect host client to the me one
706 * @file: pointer to file structure
708 * Locking: called under "dev->device_lock" lock
710 * Return: 0 on success, <0 on failure.
712 int mei_cl_connect(struct mei_cl *cl, struct file *file)
714 struct mei_device *dev;
715 struct mei_cl_cb *cb;
718 if (WARN_ON(!cl || !cl->dev))
723 rets = pm_runtime_get(dev->dev);
724 if (rets < 0 && rets != -EINPROGRESS) {
725 pm_runtime_put_noidle(dev->dev);
726 cl_err(dev, cl, "rpm: get failed %d\n", rets);
730 cb = mei_io_cb_init(cl, file);
736 cb->fop_type = MEI_FOP_CONNECT;
738 /* run hbuf acquire last so we don't have to undo */
739 if (!mei_cl_is_other_connecting(cl) && mei_hbuf_acquire(dev)) {
740 cl->state = MEI_FILE_CONNECTING;
741 if (mei_hbm_cl_connect_req(dev, cl)) {
745 cl->timer_count = MEI_CONNECT_TIMEOUT;
746 list_add_tail(&cb->list, &dev->ctrl_rd_list.list);
748 cl->state = MEI_FILE_INITIALIZING;
749 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
752 mutex_unlock(&dev->device_lock);
753 wait_event_timeout(cl->wait,
754 (cl->state == MEI_FILE_CONNECTED ||
755 cl->state == MEI_FILE_DISCONNECTED),
756 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
757 mutex_lock(&dev->device_lock);
759 if (cl->state != MEI_FILE_CONNECTED) {
760 cl->state = MEI_FILE_DISCONNECTED;
761 /* something went really wrong */
763 cl->status = -EFAULT;
765 mei_io_list_flush(&dev->ctrl_rd_list, cl);
766 mei_io_list_flush(&dev->ctrl_wr_list, cl);
772 cl_dbg(dev, cl, "rpm: autosuspend\n");
773 pm_runtime_mark_last_busy(dev->dev);
774 pm_runtime_put_autosuspend(dev->dev);
781 * mei_cl_flow_ctrl_creds - checks flow_control credits for cl.
783 * @cl: private data of the file object
785 * Return: 1 if mei_flow_ctrl_creds >0, 0 - otherwise.
786 * -ENOENT if mei_cl is not present
787 * -EINVAL if single_recv_buf == 0
789 int mei_cl_flow_ctrl_creds(struct mei_cl *cl)
791 struct mei_device *dev;
792 struct mei_me_client *me_cl;
795 if (WARN_ON(!cl || !cl->dev))
800 if (cl->mei_flow_ctrl_creds > 0)
803 me_cl = mei_me_cl_by_uuid_id(dev, &cl->cl_uuid, cl->me_client_id);
805 cl_err(dev, cl, "no such me client %d\n", cl->me_client_id);
809 if (me_cl->mei_flow_ctrl_creds > 0) {
811 if (WARN_ON(me_cl->props.single_recv_buf == 0))
814 mei_me_cl_put(me_cl);
819 * mei_cl_flow_ctrl_reduce - reduces flow_control.
821 * @cl: private data of the file object
825 * -ENOENT when me client is not found
826 * -EINVAL when ctrl credits are <= 0
828 int mei_cl_flow_ctrl_reduce(struct mei_cl *cl)
830 struct mei_device *dev;
831 struct mei_me_client *me_cl;
834 if (WARN_ON(!cl || !cl->dev))
839 me_cl = mei_me_cl_by_uuid_id(dev, &cl->cl_uuid, cl->me_client_id);
841 cl_err(dev, cl, "no such me client %d\n", cl->me_client_id);
845 if (me_cl->props.single_recv_buf) {
846 if (WARN_ON(me_cl->mei_flow_ctrl_creds <= 0)) {
850 me_cl->mei_flow_ctrl_creds--;
852 if (WARN_ON(cl->mei_flow_ctrl_creds <= 0)) {
856 cl->mei_flow_ctrl_creds--;
860 mei_me_cl_put(me_cl);
865 * mei_cl_read_start - the start read client message function.
868 * @length: number of bytes to read
870 * Return: 0 on success, <0 on failure.
872 int mei_cl_read_start(struct mei_cl *cl, size_t length)
874 struct mei_device *dev;
875 struct mei_cl_cb *cb;
876 struct mei_me_client *me_cl;
879 if (WARN_ON(!cl || !cl->dev))
884 if (!mei_cl_is_connected(cl))
888 cl_dbg(dev, cl, "read is pending.\n");
891 me_cl = mei_me_cl_by_uuid_id(dev, &cl->cl_uuid, cl->me_client_id);
893 cl_err(dev, cl, "no such me client %d\n", cl->me_client_id);
896 /* always allocate at least client max message */
897 length = max_t(size_t, length, me_cl->props.max_msg_length);
898 mei_me_cl_put(me_cl);
900 rets = pm_runtime_get(dev->dev);
901 if (rets < 0 && rets != -EINPROGRESS) {
902 pm_runtime_put_noidle(dev->dev);
903 cl_err(dev, cl, "rpm: get failed %d\n", rets);
907 cb = mei_io_cb_init(cl, NULL);
913 rets = mei_io_cb_alloc_resp_buf(cb, length);
917 cb->fop_type = MEI_FOP_READ;
918 if (mei_hbuf_acquire(dev)) {
919 rets = mei_hbm_cl_flow_control_req(dev, cl);
923 list_add_tail(&cb->list, &dev->read_list.list);
925 list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
931 cl_dbg(dev, cl, "rpm: autosuspend\n");
932 pm_runtime_mark_last_busy(dev->dev);
933 pm_runtime_put_autosuspend(dev->dev);
942 * mei_cl_irq_write - write a message to device
943 * from the interrupt thread context
946 * @cb: callback block.
947 * @cmpl_list: complete list.
949 * Return: 0, OK; otherwise error.
951 int mei_cl_irq_write(struct mei_cl *cl, struct mei_cl_cb *cb,
952 struct mei_cl_cb *cmpl_list)
954 struct mei_device *dev;
955 struct mei_msg_data *buf;
956 struct mei_msg_hdr mei_hdr;
962 if (WARN_ON(!cl || !cl->dev))
967 buf = &cb->request_buffer;
969 rets = mei_cl_flow_ctrl_creds(cl);
974 cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
978 slots = mei_hbuf_empty_slots(dev);
979 len = buf->size - cb->buf_idx;
980 msg_slots = mei_data2slots(len);
982 mei_hdr.host_addr = cl->host_client_id;
983 mei_hdr.me_addr = cl->me_client_id;
984 mei_hdr.reserved = 0;
985 mei_hdr.internal = cb->internal;
987 if (slots >= msg_slots) {
988 mei_hdr.length = len;
989 mei_hdr.msg_complete = 1;
990 /* Split the message only if we can write the whole host buffer */
991 } else if (slots == dev->hbuf_depth) {
993 len = (slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
994 mei_hdr.length = len;
995 mei_hdr.msg_complete = 0;
997 /* wait for next time the host buffer is empty */
1001 cl_dbg(dev, cl, "buf: size = %d idx = %lu\n",
1002 cb->request_buffer.size, cb->buf_idx);
1004 rets = mei_write_message(dev, &mei_hdr, buf->data + cb->buf_idx);
1007 list_move_tail(&cb->list, &cmpl_list->list);
1012 cl->writing_state = MEI_WRITING;
1013 cb->buf_idx += mei_hdr.length;
1015 if (mei_hdr.msg_complete) {
1016 if (mei_cl_flow_ctrl_reduce(cl))
1018 list_move_tail(&cb->list, &dev->write_waiting_list.list);
1025 * mei_cl_write - submit a write cb to mei device
1026 * assumes device_lock is locked
1029 * @cb: write callback with filled data
1030 * @blocking: block until completed
1032 * Return: number of bytes sent on success, <0 on failure.
1034 int mei_cl_write(struct mei_cl *cl, struct mei_cl_cb *cb, bool blocking)
1036 struct mei_device *dev;
1037 struct mei_msg_data *buf;
1038 struct mei_msg_hdr mei_hdr;
1042 if (WARN_ON(!cl || !cl->dev))
1051 buf = &cb->request_buffer;
1053 cl_dbg(dev, cl, "size=%d\n", buf->size);
1055 rets = pm_runtime_get(dev->dev);
1056 if (rets < 0 && rets != -EINPROGRESS) {
1057 pm_runtime_put_noidle(dev->dev);
1058 cl_err(dev, cl, "rpm: get failed %d\n", rets);
1062 cb->fop_type = MEI_FOP_WRITE;
1064 cl->writing_state = MEI_IDLE;
1066 mei_hdr.host_addr = cl->host_client_id;
1067 mei_hdr.me_addr = cl->me_client_id;
1068 mei_hdr.reserved = 0;
1069 mei_hdr.msg_complete = 0;
1070 mei_hdr.internal = cb->internal;
1072 rets = mei_cl_flow_ctrl_creds(cl);
1077 cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
1081 if (!mei_hbuf_acquire(dev)) {
1082 cl_dbg(dev, cl, "Cannot acquire the host buffer: not sending.\n");
1087 /* Check for a maximum length */
1088 if (buf->size > mei_hbuf_max_len(dev)) {
1089 mei_hdr.length = mei_hbuf_max_len(dev);
1090 mei_hdr.msg_complete = 0;
1092 mei_hdr.length = buf->size;
1093 mei_hdr.msg_complete = 1;
1096 rets = mei_write_message(dev, &mei_hdr, buf->data);
1100 cl->writing_state = MEI_WRITING;
1101 cb->buf_idx = mei_hdr.length;
1104 if (mei_hdr.msg_complete) {
1105 rets = mei_cl_flow_ctrl_reduce(cl);
1109 list_add_tail(&cb->list, &dev->write_waiting_list.list);
1111 list_add_tail(&cb->list, &dev->write_list.list);
1115 if (blocking && cl->writing_state != MEI_WRITE_COMPLETE) {
1117 mutex_unlock(&dev->device_lock);
1118 rets = wait_event_interruptible(cl->tx_wait,
1119 cl->writing_state == MEI_WRITE_COMPLETE);
1120 mutex_lock(&dev->device_lock);
1121 /* wait_event_interruptible returns -ERESTARTSYS */
1123 if (signal_pending(current))
1131 cl_dbg(dev, cl, "rpm: autosuspend\n");
1132 pm_runtime_mark_last_busy(dev->dev);
1133 pm_runtime_put_autosuspend(dev->dev);
1140 * mei_cl_complete - processes completed operation for a client
1142 * @cl: private data of the file object.
1143 * @cb: callback block.
1145 void mei_cl_complete(struct mei_cl *cl, struct mei_cl_cb *cb)
1147 if (cb->fop_type == MEI_FOP_WRITE) {
1150 cl->writing_state = MEI_WRITE_COMPLETE;
1151 if (waitqueue_active(&cl->tx_wait))
1152 wake_up_interruptible(&cl->tx_wait);
1154 } else if (cb->fop_type == MEI_FOP_READ &&
1155 MEI_READING == cl->reading_state) {
1156 cl->reading_state = MEI_READ_COMPLETE;
1157 if (waitqueue_active(&cl->rx_wait))
1158 wake_up_interruptible(&cl->rx_wait);
1160 mei_cl_bus_rx_event(cl);
1167 * mei_cl_all_disconnect - disconnect forcefully all connected clients
1172 void mei_cl_all_disconnect(struct mei_device *dev)
1176 list_for_each_entry(cl, &dev->file_list, link) {
1177 cl->state = MEI_FILE_DISCONNECTED;
1178 cl->mei_flow_ctrl_creds = 0;
1179 cl->timer_count = 0;
1185 * mei_cl_all_wakeup - wake up all readers and writers they can be interrupted
1189 void mei_cl_all_wakeup(struct mei_device *dev)
1193 list_for_each_entry(cl, &dev->file_list, link) {
1194 if (waitqueue_active(&cl->rx_wait)) {
1195 cl_dbg(dev, cl, "Waking up reading client!\n");
1196 wake_up_interruptible(&cl->rx_wait);
1198 if (waitqueue_active(&cl->tx_wait)) {
1199 cl_dbg(dev, cl, "Waking up writing client!\n");
1200 wake_up_interruptible(&cl->tx_wait);
1206 * mei_cl_all_write_clear - clear all pending writes
1210 void mei_cl_all_write_clear(struct mei_device *dev)
1212 mei_io_list_free(&dev->write_list, NULL);
1213 mei_io_list_free(&dev->write_waiting_list, NULL);