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
;
553 * mei_cl_allocate - allocates cl structure and sets it up.
556 * Return: The allocated file or NULL on failure
558 struct mei_cl
*mei_cl_allocate(struct mei_device
*dev
)
562 cl
= kmalloc(sizeof(struct mei_cl
), GFP_KERNEL
);
566 mei_cl_init(cl
, dev
);
572 * mei_cl_link - allocate host id in the host map
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
))
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
,
596 if (id
>= MEI_CLIENTS_MAX
) {
597 dev_err(dev
->dev
, "id exceeded %d", MEI_CLIENTS_MAX
);
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
);
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");
622 * mei_cl_unlink - remove me_cl from the list
628 int mei_cl_unlink(struct mei_cl
*cl
)
630 struct mei_device
*dev
;
632 /* don't shout on error exit path */
636 /* wd and amthif might not be initialized */
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
;
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
);
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
);
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
);
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");
707 if (!dev
->hbuf_is_ready
) {
708 dev_dbg(dev
->dev
, "hbuf is not ready\n");
712 dev
->hbuf_is_ready
= false;
718 * mei_cl_disconnect - disconnect host client from the me one
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
;
732 if (WARN_ON(!cl
|| !cl
->dev
))
737 cl_dbg(dev
, cl
, "disconnecting");
739 if (cl
->state
!= MEI_FILE_DISCONNECTING
)
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
);
749 cb
= mei_io_cb_init(cl
, MEI_FOP_DISCONNECT
, NULL
);
750 rets
= cb
? 0 : -ENOMEM
;
754 if (mei_hbuf_acquire(dev
)) {
755 if (mei_hbm_cl_disconnect_req(dev
, cl
)) {
757 cl_err(dev
, cl
, "failed to disconnect.\n");
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
);
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
) {
778 cl_dbg(dev
, cl
, "successfully disconnected from FW client.\n");
780 cl_dbg(dev
, cl
, "timeout on disconnect from FW client.\n");
784 mei_io_list_flush(&dev
->ctrl_rd_list
, cl
);
785 mei_io_list_flush(&dev
->ctrl_wr_list
, cl
);
787 cl_dbg(dev
, cl
, "rpm: autosuspend\n");
788 pm_runtime_mark_last_busy(dev
->dev
);
789 pm_runtime_put_autosuspend(dev
->dev
);
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
))
814 list_for_each_entry(ocl
, &dev
->file_list
, link
) {
815 if (ocl
->state
== MEI_FILE_CONNECTING
&&
817 cl
->me_client_id
== ocl
->me_client_id
)
826 * mei_cl_connect - connect host client to the me one
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
;
841 if (WARN_ON(!cl
|| !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
);
853 cb
= mei_io_cb_init(cl
, MEI_FOP_CONNECT
, file
);
854 rets
= cb
? 0 : -ENOMEM
;
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
)) {
865 cl
->timer_count
= MEI_CONNECT_TIMEOUT
;
866 list_add_tail(&cb
->list
, &dev
->ctrl_rd_list
.list
);
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 */
883 cl
->status
= -EFAULT
;
885 mei_io_list_flush(&dev
->ctrl_rd_list
, cl
);
886 mei_io_list_flush(&dev
->ctrl_wr_list
, cl
);
892 cl_dbg(dev
, cl
, "rpm: autosuspend\n");
893 pm_runtime_mark_last_busy(dev
->dev
);
894 pm_runtime_put_autosuspend(dev
->dev
);
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
)
913 cl
= mei_cl_allocate(dev
);
919 ret
= mei_cl_link(cl
, id
);
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
;
946 if (WARN_ON(!cl
|| !cl
->dev
))
951 if (cl
->mei_flow_ctrl_creds
> 0)
954 me_cl
= mei_me_cl_by_uuid_id(dev
, &cl
->cl_uuid
, cl
->me_client_id
);
956 cl_err(dev
, cl
, "no such me client %d\n", cl
->me_client_id
);
960 if (me_cl
->mei_flow_ctrl_creds
> 0) {
962 if (WARN_ON(me_cl
->props
.single_recv_buf
== 0))
965 mei_me_cl_put(me_cl
);
970 * mei_cl_flow_ctrl_reduce - reduces flow_control.
972 * @cl: private data of the file object
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
;
985 if (WARN_ON(!cl
|| !cl
->dev
))
990 me_cl
= mei_me_cl_by_uuid_id(dev
, &cl
->cl_uuid
, cl
->me_client_id
);
992 cl_err(dev
, cl
, "no such me client %d\n", cl
->me_client_id
);
996 if (me_cl
->props
.single_recv_buf
) {
997 if (WARN_ON(me_cl
->mei_flow_ctrl_creds
<= 0)) {
1001 me_cl
->mei_flow_ctrl_creds
--;
1003 if (WARN_ON(cl
->mei_flow_ctrl_creds
<= 0)) {
1007 cl
->mei_flow_ctrl_creds
--;
1011 mei_me_cl_put(me_cl
);
1016 * mei_cl_read_start - the start read client message function.
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
;
1031 if (WARN_ON(!cl
|| !cl
->dev
))
1036 if (!mei_cl_is_connected(cl
))
1039 /* HW currently supports only one pending read */
1040 if (!list_empty(&cl
->rd_pending
))
1043 me_cl
= mei_me_cl_by_uuid_id(dev
, &cl
->cl_uuid
, cl
->me_client_id
);
1045 cl_err(dev
, cl
, "no such me client %d\n", cl
->me_client_id
);
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
);
1059 cb
= mei_cl_alloc_cb(cl
, length
, MEI_FOP_READ
, fp
);
1060 rets
= cb
? 0 : -ENOMEM
;
1064 if (mei_hbuf_acquire(dev
)) {
1065 rets
= mei_hbm_cl_flow_control_req(dev
, cl
);
1069 list_add_tail(&cb
->list
, &cl
->rd_pending
);
1071 list_add_tail(&cb
->list
, &dev
->ctrl_wr_list
.list
);
1075 cl_dbg(dev
, cl
, "rpm: autosuspend\n");
1076 pm_runtime_mark_last_busy(dev
->dev
);
1077 pm_runtime_put_autosuspend(dev
->dev
);
1086 * mei_cl_irq_write - write a message to device
1087 * from the interrupt thread context
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
;
1106 if (WARN_ON(!cl
|| !cl
->dev
))
1113 rets
= mei_cl_flow_ctrl_creds(cl
);
1118 cl_dbg(dev
, cl
, "No flow control credentials: not sending.\n");
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
) {
1137 len
= (slots
* sizeof(u32
)) - sizeof(struct mei_msg_hdr
);
1138 mei_hdr
.length
= len
;
1139 mei_hdr
.msg_complete
= 0;
1141 /* wait for next time the host buffer is empty */
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
);
1151 list_move_tail(&cb
->list
, &cmpl_list
->list
);
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
))
1163 list_move_tail(&cb
->list
, &dev
->write_waiting_list
.list
);
1170 * mei_cl_write - submit a write cb to mei device
1171 * assumes device_lock is locked
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
;
1187 if (WARN_ON(!cl
|| !cl
->dev
))
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
);
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
);
1221 cl_dbg(dev
, cl
, "No flow control credentials: not sending.\n");
1225 if (!mei_hbuf_acquire(dev
)) {
1226 cl_dbg(dev
, cl
, "Cannot acquire the host buffer: not sending.\n");
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;
1236 mei_hdr
.length
= buf
->size
;
1237 mei_hdr
.msg_complete
= 1;
1240 rets
= mei_write_message(dev
, &mei_hdr
, buf
->data
);
1244 cl
->writing_state
= MEI_WRITING
;
1245 cb
->buf_idx
= mei_hdr
.length
;
1246 cb
->completed
= mei_hdr
.msg_complete
== 1;
1249 if (mei_hdr
.msg_complete
) {
1250 rets
= mei_cl_flow_ctrl_reduce(cl
);
1254 list_add_tail(&cb
->list
, &dev
->write_waiting_list
.list
);
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 */
1268 if (signal_pending(current
))
1276 cl_dbg(dev
, cl
, "rpm: autosuspend\n");
1277 pm_runtime_mark_last_busy(dev
->dev
);
1278 pm_runtime_put_autosuspend(dev
->dev
);
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
) {
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
);
1304 mei_cl_bus_rx_event(cl
);
1311 * mei_cl_all_disconnect - disconnect forcefully all connected clients
1316 void mei_cl_all_disconnect(struct mei_device
*dev
)
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
1333 void mei_cl_all_wakeup(struct mei_device
*dev
)
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
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
);