1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2003-2020, Intel Corporation. All rights reserved.
4 * Intel Management Engine Interface (Intel MEI) Linux driver
7 #include <linux/sched/signal.h>
8 #include <linux/wait.h>
9 #include <linux/delay.h>
10 #include <linux/slab.h>
11 #include <linux/pm_runtime.h>
12 #include <linux/dma-mapping.h>
14 #include <linux/mei.h>
21 * mei_me_cl_init - initialize me client
25 void mei_me_cl_init(struct mei_me_client *me_cl)
27 INIT_LIST_HEAD(&me_cl->list);
28 kref_init(&me_cl->refcnt);
32 * mei_me_cl_get - increases me client refcount
36 * Locking: called under "dev->device_lock" lock
38 * Return: me client or NULL
40 struct mei_me_client *mei_me_cl_get(struct mei_me_client *me_cl)
42 if (me_cl && kref_get_unless_zero(&me_cl->refcnt))
49 * mei_me_cl_release - free me client
51 * Locking: called under "dev->device_lock" lock
53 * @ref: me_client refcount
55 static void mei_me_cl_release(struct kref *ref)
57 struct mei_me_client *me_cl =
58 container_of(ref, struct mei_me_client, refcnt);
64 * mei_me_cl_put - decrease me client refcount and free client if necessary
66 * Locking: called under "dev->device_lock" lock
70 void mei_me_cl_put(struct mei_me_client *me_cl)
73 kref_put(&me_cl->refcnt, mei_me_cl_release);
77 * __mei_me_cl_del - delete me client from the list and decrease
83 * Locking: dev->me_clients_rwsem
85 static void __mei_me_cl_del(struct mei_device *dev, struct mei_me_client *me_cl)
90 list_del_init(&me_cl->list);
95 * mei_me_cl_del - delete me client from the list and decrease
101 void mei_me_cl_del(struct mei_device *dev, struct mei_me_client *me_cl)
103 down_write(&dev->me_clients_rwsem);
104 __mei_me_cl_del(dev, me_cl);
105 up_write(&dev->me_clients_rwsem);
109 * mei_me_cl_add - add me client to the list
114 void mei_me_cl_add(struct mei_device *dev, struct mei_me_client *me_cl)
116 down_write(&dev->me_clients_rwsem);
117 list_add(&me_cl->list, &dev->me_clients);
118 up_write(&dev->me_clients_rwsem);
122 * __mei_me_cl_by_uuid - locate me client by uuid
123 * increases ref count
126 * @uuid: me client uuid
128 * Return: me client or NULL if not found
130 * Locking: dev->me_clients_rwsem
132 static struct mei_me_client *__mei_me_cl_by_uuid(struct mei_device *dev,
135 struct mei_me_client *me_cl;
138 WARN_ON(!rwsem_is_locked(&dev->me_clients_rwsem));
140 list_for_each_entry(me_cl, &dev->me_clients, list) {
141 pn = &me_cl->props.protocol_name;
142 if (uuid_le_cmp(*uuid, *pn) == 0)
143 return mei_me_cl_get(me_cl);
150 * mei_me_cl_by_uuid - locate me client by uuid
151 * increases ref count
154 * @uuid: me client uuid
156 * Return: me client or NULL if not found
158 * Locking: dev->me_clients_rwsem
160 struct mei_me_client *mei_me_cl_by_uuid(struct mei_device *dev,
163 struct mei_me_client *me_cl;
165 down_read(&dev->me_clients_rwsem);
166 me_cl = __mei_me_cl_by_uuid(dev, uuid);
167 up_read(&dev->me_clients_rwsem);
173 * mei_me_cl_by_id - locate me client by client id
174 * increases ref count
176 * @dev: the device structure
177 * @client_id: me client id
179 * Return: me client or NULL if not found
181 * Locking: dev->me_clients_rwsem
183 struct mei_me_client *mei_me_cl_by_id(struct mei_device *dev, u8 client_id)
186 struct mei_me_client *__me_cl, *me_cl = NULL;
188 down_read(&dev->me_clients_rwsem);
189 list_for_each_entry(__me_cl, &dev->me_clients, list) {
190 if (__me_cl->client_id == client_id) {
191 me_cl = mei_me_cl_get(__me_cl);
195 up_read(&dev->me_clients_rwsem);
201 * __mei_me_cl_by_uuid_id - locate me client by client id and uuid
202 * increases ref count
204 * @dev: the device structure
205 * @uuid: me client uuid
206 * @client_id: me client id
208 * Return: me client or null if not found
210 * Locking: dev->me_clients_rwsem
212 static struct mei_me_client *__mei_me_cl_by_uuid_id(struct mei_device *dev,
213 const uuid_le *uuid, u8 client_id)
215 struct mei_me_client *me_cl;
218 WARN_ON(!rwsem_is_locked(&dev->me_clients_rwsem));
220 list_for_each_entry(me_cl, &dev->me_clients, list) {
221 pn = &me_cl->props.protocol_name;
222 if (uuid_le_cmp(*uuid, *pn) == 0 &&
223 me_cl->client_id == client_id)
224 return mei_me_cl_get(me_cl);
232 * mei_me_cl_by_uuid_id - locate me client by client id and uuid
233 * increases ref count
235 * @dev: the device structure
236 * @uuid: me client uuid
237 * @client_id: me client id
239 * Return: me client or null if not found
241 struct mei_me_client *mei_me_cl_by_uuid_id(struct mei_device *dev,
242 const uuid_le *uuid, u8 client_id)
244 struct mei_me_client *me_cl;
246 down_read(&dev->me_clients_rwsem);
247 me_cl = __mei_me_cl_by_uuid_id(dev, uuid, client_id);
248 up_read(&dev->me_clients_rwsem);
254 * mei_me_cl_rm_by_uuid - remove all me clients matching uuid
256 * @dev: the device structure
257 * @uuid: me client uuid
259 * Locking: called under "dev->device_lock" lock
261 void mei_me_cl_rm_by_uuid(struct mei_device *dev, const uuid_le *uuid)
263 struct mei_me_client *me_cl;
265 dev_dbg(dev->dev, "remove %pUl\n", uuid);
267 down_write(&dev->me_clients_rwsem);
268 me_cl = __mei_me_cl_by_uuid(dev, uuid);
269 __mei_me_cl_del(dev, me_cl);
270 mei_me_cl_put(me_cl);
271 up_write(&dev->me_clients_rwsem);
275 * mei_me_cl_rm_by_uuid_id - remove all me clients matching client id
277 * @dev: the device structure
278 * @uuid: me client uuid
281 * Locking: called under "dev->device_lock" lock
283 void mei_me_cl_rm_by_uuid_id(struct mei_device *dev, const uuid_le *uuid, u8 id)
285 struct mei_me_client *me_cl;
287 dev_dbg(dev->dev, "remove %pUl %d\n", uuid, id);
289 down_write(&dev->me_clients_rwsem);
290 me_cl = __mei_me_cl_by_uuid_id(dev, uuid, id);
291 __mei_me_cl_del(dev, me_cl);
292 mei_me_cl_put(me_cl);
293 up_write(&dev->me_clients_rwsem);
297 * mei_me_cl_rm_all - remove all me clients
299 * @dev: the device structure
301 * Locking: called under "dev->device_lock" lock
303 void mei_me_cl_rm_all(struct mei_device *dev)
305 struct mei_me_client *me_cl, *next;
307 down_write(&dev->me_clients_rwsem);
308 list_for_each_entry_safe(me_cl, next, &dev->me_clients, list)
309 __mei_me_cl_del(dev, me_cl);
310 up_write(&dev->me_clients_rwsem);
314 * mei_io_cb_free - free mei_cb_private related memory
316 * @cb: mei callback struct
318 void mei_io_cb_free(struct mei_cl_cb *cb)
329 * mei_tx_cb_enqueue - queue tx callback
331 * Locking: called under "dev->device_lock" lock
333 * @cb: mei callback struct
334 * @head: an instance of list to queue on
336 static inline void mei_tx_cb_enqueue(struct mei_cl_cb *cb,
337 struct list_head *head)
339 list_add_tail(&cb->list, head);
340 cb->cl->tx_cb_queued++;
344 * mei_tx_cb_dequeue - dequeue tx callback
346 * Locking: called under "dev->device_lock" lock
348 * @cb: mei callback struct to dequeue and free
350 static inline void mei_tx_cb_dequeue(struct mei_cl_cb *cb)
352 if (!WARN_ON(cb->cl->tx_cb_queued == 0))
353 cb->cl->tx_cb_queued--;
359 * mei_cl_set_read_by_fp - set pending_read flag to vtag struct for given fp
361 * Locking: called under "dev->device_lock" lock
364 * @fp: pointer to file structure
366 static void mei_cl_set_read_by_fp(const struct mei_cl *cl,
367 const struct file *fp)
369 struct mei_cl_vtag *cl_vtag;
371 list_for_each_entry(cl_vtag, &cl->vtag_map, list) {
372 if (cl_vtag->fp == fp) {
373 cl_vtag->pending_read = true;
380 * mei_io_cb_init - allocate and initialize io callback
383 * @type: operation type
384 * @fp: pointer to file structure
386 * Return: mei_cl_cb pointer or NULL;
388 static struct mei_cl_cb *mei_io_cb_init(struct mei_cl *cl,
389 enum mei_cb_file_ops type,
390 const struct file *fp)
392 struct mei_cl_cb *cb;
394 cb = kzalloc(sizeof(*cb), GFP_KERNEL);
398 INIT_LIST_HEAD(&cb->list);
409 * mei_io_list_flush_cl - removes cbs belonging to the cl.
411 * @head: an instance of our list structure
414 static void mei_io_list_flush_cl(struct list_head *head,
415 const struct mei_cl *cl)
417 struct mei_cl_cb *cb, *next;
419 list_for_each_entry_safe(cb, next, head, list) {
421 list_del_init(&cb->list);
422 if (cb->fop_type == MEI_FOP_READ)
429 * mei_io_tx_list_free_cl - removes cb belonging to the cl and free them
431 * @head: An instance of our list structure
433 * @fp: file pointer (matching cb file object), may be NULL
435 static void mei_io_tx_list_free_cl(struct list_head *head,
436 const struct mei_cl *cl,
437 const struct file *fp)
439 struct mei_cl_cb *cb, *next;
441 list_for_each_entry_safe(cb, next, head, list) {
442 if (cl == cb->cl && (!fp || fp == cb->fp))
443 mei_tx_cb_dequeue(cb);
448 * mei_io_list_free_fp - free cb from a list that matches file pointer
451 * @fp: file pointer (matching cb file object), may be NULL
453 static void mei_io_list_free_fp(struct list_head *head, const struct file *fp)
455 struct mei_cl_cb *cb, *next;
457 list_for_each_entry_safe(cb, next, head, list)
458 if (!fp || fp == cb->fp)
463 * mei_cl_free_pending - free pending cb
467 static void mei_cl_free_pending(struct mei_cl *cl)
469 struct mei_cl_cb *cb;
471 cb = list_first_entry_or_null(&cl->rd_pending, struct mei_cl_cb, list);
476 * mei_cl_alloc_cb - a convenient wrapper for allocating read cb
479 * @length: size of the buffer
480 * @fop_type: operation type
481 * @fp: associated file pointer (might be NULL)
483 * Return: cb on success and NULL on failure
485 struct mei_cl_cb *mei_cl_alloc_cb(struct mei_cl *cl, size_t length,
486 enum mei_cb_file_ops fop_type,
487 const struct file *fp)
489 struct mei_cl_cb *cb;
491 cb = mei_io_cb_init(cl, fop_type, fp);
498 cb->buf.data = kmalloc(roundup(length, MEI_SLOT_SIZE), GFP_KERNEL);
503 cb->buf.size = length;
509 * mei_cl_enqueue_ctrl_wr_cb - a convenient wrapper for allocating
510 * and enqueuing of the control commands cb
513 * @length: size of the buffer
514 * @fop_type: operation type
515 * @fp: associated file pointer (might be NULL)
517 * Return: cb on success and NULL on failure
518 * Locking: called under "dev->device_lock" lock
520 struct mei_cl_cb *mei_cl_enqueue_ctrl_wr_cb(struct mei_cl *cl, size_t length,
521 enum mei_cb_file_ops fop_type,
522 const struct file *fp)
524 struct mei_cl_cb *cb;
526 /* for RX always allocate at least client's mtu */
528 length = max_t(size_t, length, mei_cl_mtu(cl));
530 cb = mei_cl_alloc_cb(cl, length, fop_type, fp);
534 list_add_tail(&cb->list, &cl->dev->ctrl_wr_list);
539 * mei_cl_read_cb - find this cl's callback in the read list
540 * for a specific file
543 * @fp: file pointer (matching cb file object), may be NULL
545 * Return: cb on success, NULL if cb is not found
547 struct mei_cl_cb *mei_cl_read_cb(struct mei_cl *cl, const struct file *fp)
549 struct mei_cl_cb *cb;
550 struct mei_cl_cb *ret_cb = NULL;
552 spin_lock(&cl->rd_completed_lock);
553 list_for_each_entry(cb, &cl->rd_completed, list)
554 if (!fp || fp == cb->fp) {
558 spin_unlock(&cl->rd_completed_lock);
563 * mei_cl_flush_queues - flushes queue lists belonging to cl.
566 * @fp: file pointer (matching cb file object), may be NULL
568 * Return: 0 on success, -EINVAL if cl or cl->dev is NULL.
570 int mei_cl_flush_queues(struct mei_cl *cl, const struct file *fp)
572 struct mei_device *dev;
574 if (WARN_ON(!cl || !cl->dev))
579 cl_dbg(dev, cl, "remove list entry belonging to cl\n");
580 mei_io_tx_list_free_cl(&cl->dev->write_list, cl, fp);
581 mei_io_tx_list_free_cl(&cl->dev->write_waiting_list, cl, fp);
582 /* free pending and control cb only in final flush */
584 mei_io_list_flush_cl(&cl->dev->ctrl_wr_list, cl);
585 mei_io_list_flush_cl(&cl->dev->ctrl_rd_list, cl);
586 mei_cl_free_pending(cl);
588 spin_lock(&cl->rd_completed_lock);
589 mei_io_list_free_fp(&cl->rd_completed, fp);
590 spin_unlock(&cl->rd_completed_lock);
596 * mei_cl_init - initializes cl.
598 * @cl: host client to be initialized
601 static void mei_cl_init(struct mei_cl *cl, struct mei_device *dev)
603 memset(cl, 0, sizeof(*cl));
604 init_waitqueue_head(&cl->wait);
605 init_waitqueue_head(&cl->rx_wait);
606 init_waitqueue_head(&cl->tx_wait);
607 init_waitqueue_head(&cl->ev_wait);
608 INIT_LIST_HEAD(&cl->vtag_map);
609 spin_lock_init(&cl->rd_completed_lock);
610 INIT_LIST_HEAD(&cl->rd_completed);
611 INIT_LIST_HEAD(&cl->rd_pending);
612 INIT_LIST_HEAD(&cl->link);
613 cl->writing_state = MEI_IDLE;
614 cl->state = MEI_FILE_UNINITIALIZED;
619 * mei_cl_allocate - allocates cl structure and sets it up.
622 * Return: The allocated file or NULL on failure
624 struct mei_cl *mei_cl_allocate(struct mei_device *dev)
628 cl = kmalloc(sizeof(*cl), GFP_KERNEL);
632 mei_cl_init(cl, dev);
638 * mei_cl_link - allocate host id in the host map
642 * Return: 0 on success
643 * -EINVAL on incorrect values
644 * -EMFILE if open count exceeded.
646 int mei_cl_link(struct mei_cl *cl)
648 struct mei_device *dev;
651 if (WARN_ON(!cl || !cl->dev))
656 id = find_first_zero_bit(dev->host_clients_map, MEI_CLIENTS_MAX);
657 if (id >= MEI_CLIENTS_MAX) {
658 dev_err(dev->dev, "id exceeded %d", MEI_CLIENTS_MAX);
662 if (dev->open_handle_count >= MEI_MAX_OPEN_HANDLE_COUNT) {
663 dev_err(dev->dev, "open_handle_count exceeded %d",
664 MEI_MAX_OPEN_HANDLE_COUNT);
668 dev->open_handle_count++;
670 cl->host_client_id = id;
671 list_add_tail(&cl->link, &dev->file_list);
673 set_bit(id, dev->host_clients_map);
675 cl->state = MEI_FILE_INITIALIZING;
677 cl_dbg(dev, cl, "link cl\n");
682 * mei_cl_unlink - remove host client from the list
688 int mei_cl_unlink(struct mei_cl *cl)
690 struct mei_device *dev;
692 /* don't shout on error exit path */
696 if (WARN_ON(!cl->dev))
701 cl_dbg(dev, cl, "unlink client");
703 if (cl->state == MEI_FILE_UNINITIALIZED)
706 if (dev->open_handle_count > 0)
707 dev->open_handle_count--;
709 /* never clear the 0 bit */
710 if (cl->host_client_id)
711 clear_bit(cl->host_client_id, dev->host_clients_map);
713 list_del_init(&cl->link);
715 cl->state = MEI_FILE_UNINITIALIZED;
716 cl->writing_state = MEI_IDLE;
718 WARN_ON(!list_empty(&cl->rd_completed) ||
719 !list_empty(&cl->rd_pending) ||
720 !list_empty(&cl->link));
725 void mei_host_client_init(struct mei_device *dev)
727 mei_set_devstate(dev, MEI_DEV_ENABLED);
728 dev->reset_count = 0;
730 schedule_work(&dev->bus_rescan_work);
732 pm_runtime_mark_last_busy(dev->dev);
733 dev_dbg(dev->dev, "rpm: autosuspend\n");
734 pm_request_autosuspend(dev->dev);
738 * mei_hbuf_acquire - try to acquire host buffer
740 * @dev: the device structure
741 * Return: true if host buffer was acquired
743 bool mei_hbuf_acquire(struct mei_device *dev)
745 if (mei_pg_state(dev) == MEI_PG_ON ||
746 mei_pg_in_transition(dev)) {
747 dev_dbg(dev->dev, "device is in pg\n");
751 if (!dev->hbuf_is_ready) {
752 dev_dbg(dev->dev, "hbuf is not ready\n");
756 dev->hbuf_is_ready = false;
762 * mei_cl_wake_all - wake up readers, writers and event waiters so
763 * they can be interrupted
767 static void mei_cl_wake_all(struct mei_cl *cl)
769 struct mei_device *dev = cl->dev;
771 /* synchronized under device mutex */
772 if (waitqueue_active(&cl->rx_wait)) {
773 cl_dbg(dev, cl, "Waking up reading client!\n");
774 wake_up_interruptible(&cl->rx_wait);
776 /* synchronized under device mutex */
777 if (waitqueue_active(&cl->tx_wait)) {
778 cl_dbg(dev, cl, "Waking up writing client!\n");
779 wake_up_interruptible(&cl->tx_wait);
781 /* synchronized under device mutex */
782 if (waitqueue_active(&cl->ev_wait)) {
783 cl_dbg(dev, cl, "Waking up waiting for event clients!\n");
784 wake_up_interruptible(&cl->ev_wait);
786 /* synchronized under device mutex */
787 if (waitqueue_active(&cl->wait)) {
788 cl_dbg(dev, cl, "Waking up ctrl write clients!\n");
794 * mei_cl_set_disconnected - set disconnected state and clear
795 * associated states and resources
799 static void mei_cl_set_disconnected(struct mei_cl *cl)
801 struct mei_device *dev = cl->dev;
803 if (cl->state == MEI_FILE_DISCONNECTED ||
804 cl->state <= MEI_FILE_INITIALIZING)
807 cl->state = MEI_FILE_DISCONNECTED;
808 mei_io_tx_list_free_cl(&dev->write_list, cl, NULL);
809 mei_io_tx_list_free_cl(&dev->write_waiting_list, cl, NULL);
810 mei_io_list_flush_cl(&dev->ctrl_rd_list, cl);
811 mei_io_list_flush_cl(&dev->ctrl_wr_list, cl);
813 cl->rx_flow_ctrl_creds = 0;
814 cl->tx_flow_ctrl_creds = 0;
820 if (!WARN_ON(cl->me_cl->connect_count == 0))
821 cl->me_cl->connect_count--;
823 if (cl->me_cl->connect_count == 0)
824 cl->me_cl->tx_flow_ctrl_creds = 0;
826 mei_me_cl_put(cl->me_cl);
830 static int mei_cl_set_connecting(struct mei_cl *cl, struct mei_me_client *me_cl)
832 if (!mei_me_cl_get(me_cl))
835 /* only one connection is allowed for fixed address clients */
836 if (me_cl->props.fixed_address) {
837 if (me_cl->connect_count) {
838 mei_me_cl_put(me_cl);
844 cl->state = MEI_FILE_CONNECTING;
845 cl->me_cl->connect_count++;
851 * mei_cl_send_disconnect - send disconnect request
854 * @cb: callback block
856 * Return: 0, OK; otherwise, error.
858 static int mei_cl_send_disconnect(struct mei_cl *cl, struct mei_cl_cb *cb)
860 struct mei_device *dev;
865 ret = mei_hbm_cl_disconnect_req(dev, cl);
868 cl->state = MEI_FILE_DISCONNECT_REPLY;
872 list_move_tail(&cb->list, &dev->ctrl_rd_list);
873 cl->timer_count = MEI_CONNECT_TIMEOUT;
874 mei_schedule_stall_timer(dev);
880 * mei_cl_irq_disconnect - processes close related operation from
881 * interrupt thread context - send disconnect request
884 * @cb: callback block.
885 * @cmpl_list: complete list.
887 * Return: 0, OK; otherwise, error.
889 int mei_cl_irq_disconnect(struct mei_cl *cl, struct mei_cl_cb *cb,
890 struct list_head *cmpl_list)
892 struct mei_device *dev = cl->dev;
897 msg_slots = mei_hbm2slots(sizeof(struct hbm_client_connect_request));
898 slots = mei_hbuf_empty_slots(dev);
902 if ((u32)slots < msg_slots)
905 ret = mei_cl_send_disconnect(cl, cb);
907 list_move_tail(&cb->list, cmpl_list);
913 * __mei_cl_disconnect - disconnect host client from the me one
914 * internal function runtime pm has to be already acquired
918 * Return: 0 on success, <0 on failure.
920 static int __mei_cl_disconnect(struct mei_cl *cl)
922 struct mei_device *dev;
923 struct mei_cl_cb *cb;
928 cl->state = MEI_FILE_DISCONNECTING;
930 cb = mei_cl_enqueue_ctrl_wr_cb(cl, 0, MEI_FOP_DISCONNECT, NULL);
936 if (mei_hbuf_acquire(dev)) {
937 rets = mei_cl_send_disconnect(cl, cb);
939 cl_err(dev, cl, "failed to disconnect.\n");
944 mutex_unlock(&dev->device_lock);
945 wait_event_timeout(cl->wait,
946 cl->state == MEI_FILE_DISCONNECT_REPLY ||
947 cl->state == MEI_FILE_DISCONNECTED,
948 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
949 mutex_lock(&dev->device_lock);
952 if (cl->state != MEI_FILE_DISCONNECT_REPLY &&
953 cl->state != MEI_FILE_DISCONNECTED) {
954 cl_dbg(dev, cl, "timeout on disconnect from FW client.\n");
959 /* we disconnect also on error */
960 mei_cl_set_disconnected(cl);
962 cl_dbg(dev, cl, "successfully disconnected from FW client.\n");
969 * mei_cl_disconnect - disconnect host client from the me one
973 * Locking: called under "dev->device_lock" lock
975 * Return: 0 on success, <0 on failure.
977 int mei_cl_disconnect(struct mei_cl *cl)
979 struct mei_device *dev;
982 if (WARN_ON(!cl || !cl->dev))
987 cl_dbg(dev, cl, "disconnecting");
989 if (!mei_cl_is_connected(cl))
992 if (mei_cl_is_fixed_address(cl)) {
993 mei_cl_set_disconnected(cl);
997 if (dev->dev_state == MEI_DEV_POWERING_DOWN ||
998 dev->dev_state == MEI_DEV_POWER_DOWN) {
999 cl_dbg(dev, cl, "Device is powering down, don't bother with disconnection\n");
1000 mei_cl_set_disconnected(cl);
1004 rets = pm_runtime_get(dev->dev);
1005 if (rets < 0 && rets != -EINPROGRESS) {
1006 pm_runtime_put_noidle(dev->dev);
1007 cl_err(dev, cl, "rpm: get failed %d\n", rets);
1011 rets = __mei_cl_disconnect(cl);
1013 cl_dbg(dev, cl, "rpm: autosuspend\n");
1014 pm_runtime_mark_last_busy(dev->dev);
1015 pm_runtime_put_autosuspend(dev->dev);
1022 * mei_cl_is_other_connecting - checks if other
1023 * client with the same me client id is connecting
1025 * @cl: private data of the file object
1027 * Return: true if other client is connected, false - otherwise.
1029 static bool mei_cl_is_other_connecting(struct mei_cl *cl)
1031 struct mei_device *dev;
1032 struct mei_cl_cb *cb;
1036 list_for_each_entry(cb, &dev->ctrl_rd_list, list) {
1037 if (cb->fop_type == MEI_FOP_CONNECT &&
1038 mei_cl_me_id(cl) == mei_cl_me_id(cb->cl))
1046 * mei_cl_send_connect - send connect request
1049 * @cb: callback block
1051 * Return: 0, OK; otherwise, error.
1053 static int mei_cl_send_connect(struct mei_cl *cl, struct mei_cl_cb *cb)
1055 struct mei_device *dev;
1060 ret = mei_hbm_cl_connect_req(dev, cl);
1063 cl->state = MEI_FILE_DISCONNECT_REPLY;
1067 list_move_tail(&cb->list, &dev->ctrl_rd_list);
1068 cl->timer_count = MEI_CONNECT_TIMEOUT;
1069 mei_schedule_stall_timer(dev);
1074 * mei_cl_irq_connect - send connect request in irq_thread context
1077 * @cb: callback block
1078 * @cmpl_list: complete list
1080 * Return: 0, OK; otherwise, error.
1082 int mei_cl_irq_connect(struct mei_cl *cl, struct mei_cl_cb *cb,
1083 struct list_head *cmpl_list)
1085 struct mei_device *dev = cl->dev;
1090 if (mei_cl_is_other_connecting(cl))
1093 msg_slots = mei_hbm2slots(sizeof(struct hbm_client_connect_request));
1094 slots = mei_hbuf_empty_slots(dev);
1098 if ((u32)slots < msg_slots)
1101 rets = mei_cl_send_connect(cl, cb);
1103 list_move_tail(&cb->list, cmpl_list);
1109 * mei_cl_connect - connect host client to the me one
1113 * @fp: pointer to file structure
1115 * Locking: called under "dev->device_lock" lock
1117 * Return: 0 on success, <0 on failure.
1119 int mei_cl_connect(struct mei_cl *cl, struct mei_me_client *me_cl,
1120 const struct file *fp)
1122 struct mei_device *dev;
1123 struct mei_cl_cb *cb;
1126 if (WARN_ON(!cl || !cl->dev || !me_cl))
1131 rets = mei_cl_set_connecting(cl, me_cl);
1135 if (mei_cl_is_fixed_address(cl)) {
1136 cl->state = MEI_FILE_CONNECTED;
1141 rets = pm_runtime_get(dev->dev);
1142 if (rets < 0 && rets != -EINPROGRESS) {
1143 pm_runtime_put_noidle(dev->dev);
1144 cl_err(dev, cl, "rpm: get failed %d\n", rets);
1148 cb = mei_cl_enqueue_ctrl_wr_cb(cl, 0, MEI_FOP_CONNECT, fp);
1154 /* run hbuf acquire last so we don't have to undo */
1155 if (!mei_cl_is_other_connecting(cl) && mei_hbuf_acquire(dev)) {
1156 rets = mei_cl_send_connect(cl, cb);
1161 mutex_unlock(&dev->device_lock);
1162 wait_event_timeout(cl->wait,
1163 (cl->state == MEI_FILE_CONNECTED ||
1164 cl->state == MEI_FILE_DISCONNECTED ||
1165 cl->state == MEI_FILE_DISCONNECT_REQUIRED ||
1166 cl->state == MEI_FILE_DISCONNECT_REPLY),
1167 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
1168 mutex_lock(&dev->device_lock);
1170 if (!mei_cl_is_connected(cl)) {
1171 if (cl->state == MEI_FILE_DISCONNECT_REQUIRED) {
1172 mei_io_list_flush_cl(&dev->ctrl_rd_list, cl);
1173 mei_io_list_flush_cl(&dev->ctrl_wr_list, cl);
1174 /* ignore disconnect return valuue;
1175 * in case of failure reset will be invoked
1177 __mei_cl_disconnect(cl);
1182 /* timeout or something went really wrong */
1184 cl->status = -EFAULT;
1189 cl_dbg(dev, cl, "rpm: autosuspend\n");
1190 pm_runtime_mark_last_busy(dev->dev);
1191 pm_runtime_put_autosuspend(dev->dev);
1196 if (!mei_cl_is_connected(cl))
1197 mei_cl_set_disconnected(cl);
1203 * mei_cl_alloc_linked - allocate and link host client
1205 * @dev: the device structure
1207 * Return: cl on success ERR_PTR on failure
1209 struct mei_cl *mei_cl_alloc_linked(struct mei_device *dev)
1214 cl = mei_cl_allocate(dev);
1220 ret = mei_cl_link(cl);
1227 return ERR_PTR(ret);
1231 * mei_cl_tx_flow_ctrl_creds - checks flow_control credits for cl.
1235 * Return: 1 if tx_flow_ctrl_creds >0, 0 - otherwise.
1237 static int mei_cl_tx_flow_ctrl_creds(struct mei_cl *cl)
1239 if (WARN_ON(!cl || !cl->me_cl))
1242 if (cl->tx_flow_ctrl_creds > 0)
1245 if (mei_cl_is_fixed_address(cl))
1248 if (mei_cl_is_single_recv_buf(cl)) {
1249 if (cl->me_cl->tx_flow_ctrl_creds > 0)
1256 * mei_cl_tx_flow_ctrl_creds_reduce - reduces transmit flow control credits
1263 * -EINVAL when ctrl credits are <= 0
1265 static int mei_cl_tx_flow_ctrl_creds_reduce(struct mei_cl *cl)
1267 if (WARN_ON(!cl || !cl->me_cl))
1270 if (mei_cl_is_fixed_address(cl))
1273 if (mei_cl_is_single_recv_buf(cl)) {
1274 if (WARN_ON(cl->me_cl->tx_flow_ctrl_creds <= 0))
1276 cl->me_cl->tx_flow_ctrl_creds--;
1278 if (WARN_ON(cl->tx_flow_ctrl_creds <= 0))
1280 cl->tx_flow_ctrl_creds--;
1286 * mei_cl_vtag_alloc - allocate and fill the vtag structure
1288 * @fp: pointer to file structure
1292 * * Pointer to allocated struct - on success
1293 * * ERR_PTR(-ENOMEM) on memory allocation failure
1295 struct mei_cl_vtag *mei_cl_vtag_alloc(struct file *fp, u8 vtag)
1297 struct mei_cl_vtag *cl_vtag;
1299 cl_vtag = kzalloc(sizeof(*cl_vtag), GFP_KERNEL);
1301 return ERR_PTR(-ENOMEM);
1303 INIT_LIST_HEAD(&cl_vtag->list);
1304 cl_vtag->vtag = vtag;
1311 * mei_cl_fp_by_vtag - obtain the file pointer by vtag
1314 * @vtag: virtual tag
1317 * * A file pointer - on success
1318 * * ERR_PTR(-ENOENT) if vtag is not found in the client vtag list
1320 const struct file *mei_cl_fp_by_vtag(const struct mei_cl *cl, u8 vtag)
1322 struct mei_cl_vtag *vtag_l;
1324 list_for_each_entry(vtag_l, &cl->vtag_map, list)
1325 /* The client on bus has one fixed fp */
1326 if ((cl->cldev && mei_cldev_enabled(cl->cldev)) ||
1327 vtag_l->vtag == vtag)
1330 return ERR_PTR(-ENOENT);
1334 * mei_cl_reset_read_by_vtag - reset pending_read flag by given vtag
1339 static void mei_cl_reset_read_by_vtag(const struct mei_cl *cl, u8 vtag)
1341 struct mei_cl_vtag *vtag_l;
1343 list_for_each_entry(vtag_l, &cl->vtag_map, list) {
1344 if (vtag_l->vtag == vtag) {
1345 vtag_l->pending_read = false;
1352 * mei_cl_read_vtag_add_fc - add flow control for next pending reader
1357 static void mei_cl_read_vtag_add_fc(struct mei_cl *cl)
1359 struct mei_cl_vtag *cl_vtag;
1361 list_for_each_entry(cl_vtag, &cl->vtag_map, list) {
1362 if (cl_vtag->pending_read) {
1363 if (mei_cl_enqueue_ctrl_wr_cb(cl,
1367 cl->rx_flow_ctrl_creds++;
1374 * mei_cl_vt_support_check - check if client support vtags
1379 * * 0 - supported, or not connected at all
1380 * * -EOPNOTSUPP - vtags are not supported by client
1382 int mei_cl_vt_support_check(const struct mei_cl *cl)
1384 struct mei_device *dev = cl->dev;
1386 if (!dev->hbm_f_vt_supported)
1392 return cl->me_cl->props.vt_supported ? 0 : -EOPNOTSUPP;
1396 * mei_cl_add_rd_completed - add read completed callback to list with lock
1400 * @cb: callback block
1403 void mei_cl_add_rd_completed(struct mei_cl *cl, struct mei_cl_cb *cb)
1405 const struct file *fp;
1407 if (!mei_cl_vt_support_check(cl)) {
1408 fp = mei_cl_fp_by_vtag(cl, cb->vtag);
1410 /* client already disconnected, discarding */
1415 mei_cl_reset_read_by_vtag(cl, cb->vtag);
1416 mei_cl_read_vtag_add_fc(cl);
1419 spin_lock(&cl->rd_completed_lock);
1420 list_add_tail(&cb->list, &cl->rd_completed);
1421 spin_unlock(&cl->rd_completed_lock);
1425 * mei_cl_del_rd_completed - free read completed callback with lock
1428 * @cb: callback block
1431 void mei_cl_del_rd_completed(struct mei_cl *cl, struct mei_cl_cb *cb)
1433 spin_lock(&cl->rd_completed_lock);
1435 spin_unlock(&cl->rd_completed_lock);
1439 * mei_cl_notify_fop2req - convert fop to proper request
1441 * @fop: client notification start response command
1443 * Return: MEI_HBM_NOTIFICATION_START/STOP
1445 u8 mei_cl_notify_fop2req(enum mei_cb_file_ops fop)
1447 if (fop == MEI_FOP_NOTIFY_START)
1448 return MEI_HBM_NOTIFICATION_START;
1450 return MEI_HBM_NOTIFICATION_STOP;
1454 * mei_cl_notify_req2fop - convert notification request top file operation type
1456 * @req: hbm notification request type
1458 * Return: MEI_FOP_NOTIFY_START/STOP
1460 enum mei_cb_file_ops mei_cl_notify_req2fop(u8 req)
1462 if (req == MEI_HBM_NOTIFICATION_START)
1463 return MEI_FOP_NOTIFY_START;
1465 return MEI_FOP_NOTIFY_STOP;
1469 * mei_cl_irq_notify - send notification request in irq_thread context
1472 * @cb: callback block.
1473 * @cmpl_list: complete list.
1475 * Return: 0 on such and error otherwise.
1477 int mei_cl_irq_notify(struct mei_cl *cl, struct mei_cl_cb *cb,
1478 struct list_head *cmpl_list)
1480 struct mei_device *dev = cl->dev;
1486 msg_slots = mei_hbm2slots(sizeof(struct hbm_client_connect_request));
1487 slots = mei_hbuf_empty_slots(dev);
1491 if ((u32)slots < msg_slots)
1494 request = mei_cl_notify_fop2req(cb->fop_type);
1495 ret = mei_hbm_cl_notify_req(dev, cl, request);
1498 list_move_tail(&cb->list, cmpl_list);
1502 list_move_tail(&cb->list, &dev->ctrl_rd_list);
1507 * mei_cl_notify_request - send notification stop/start request
1510 * @fp: associate request with file
1511 * @request: 1 for start or 0 for stop
1513 * Locking: called under "dev->device_lock" lock
1515 * Return: 0 on such and error otherwise.
1517 int mei_cl_notify_request(struct mei_cl *cl,
1518 const struct file *fp, u8 request)
1520 struct mei_device *dev;
1521 struct mei_cl_cb *cb;
1522 enum mei_cb_file_ops fop_type;
1525 if (WARN_ON(!cl || !cl->dev))
1530 if (!dev->hbm_f_ev_supported) {
1531 cl_dbg(dev, cl, "notifications not supported\n");
1535 if (!mei_cl_is_connected(cl))
1538 rets = pm_runtime_get(dev->dev);
1539 if (rets < 0 && rets != -EINPROGRESS) {
1540 pm_runtime_put_noidle(dev->dev);
1541 cl_err(dev, cl, "rpm: get failed %d\n", rets);
1545 fop_type = mei_cl_notify_req2fop(request);
1546 cb = mei_cl_enqueue_ctrl_wr_cb(cl, 0, fop_type, fp);
1552 if (mei_hbuf_acquire(dev)) {
1553 if (mei_hbm_cl_notify_req(dev, cl, request)) {
1557 list_move_tail(&cb->list, &dev->ctrl_rd_list);
1560 mutex_unlock(&dev->device_lock);
1561 wait_event_timeout(cl->wait,
1562 cl->notify_en == request ||
1564 !mei_cl_is_connected(cl),
1565 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
1566 mutex_lock(&dev->device_lock);
1568 if (cl->notify_en != request && !cl->status)
1569 cl->status = -EFAULT;
1574 cl_dbg(dev, cl, "rpm: autosuspend\n");
1575 pm_runtime_mark_last_busy(dev->dev);
1576 pm_runtime_put_autosuspend(dev->dev);
1583 * mei_cl_notify - raise notification
1587 * Locking: called under "dev->device_lock" lock
1589 void mei_cl_notify(struct mei_cl *cl)
1591 struct mei_device *dev;
1593 if (!cl || !cl->dev)
1601 cl_dbg(dev, cl, "notify event");
1602 cl->notify_ev = true;
1603 if (!mei_cl_bus_notify_event(cl))
1604 wake_up_interruptible(&cl->ev_wait);
1607 kill_fasync(&cl->ev_async, SIGIO, POLL_PRI);
1612 * mei_cl_notify_get - get or wait for notification event
1615 * @block: this request is blocking
1616 * @notify_ev: true if notification event was received
1618 * Locking: called under "dev->device_lock" lock
1620 * Return: 0 on such and error otherwise.
1622 int mei_cl_notify_get(struct mei_cl *cl, bool block, bool *notify_ev)
1624 struct mei_device *dev;
1629 if (WARN_ON(!cl || !cl->dev))
1634 if (!dev->hbm_f_ev_supported) {
1635 cl_dbg(dev, cl, "notifications not supported\n");
1639 if (!mei_cl_is_connected(cl))
1648 mutex_unlock(&dev->device_lock);
1649 rets = wait_event_interruptible(cl->ev_wait, cl->notify_ev);
1650 mutex_lock(&dev->device_lock);
1656 *notify_ev = cl->notify_ev;
1657 cl->notify_ev = false;
1662 * mei_cl_read_start - the start read client message function.
1665 * @length: number of bytes to read
1666 * @fp: pointer to file structure
1668 * Return: 0 on success, <0 on failure.
1670 int mei_cl_read_start(struct mei_cl *cl, size_t length, const struct file *fp)
1672 struct mei_device *dev;
1673 struct mei_cl_cb *cb;
1676 if (WARN_ON(!cl || !cl->dev))
1681 if (!mei_cl_is_connected(cl))
1684 if (!mei_me_cl_is_active(cl->me_cl)) {
1685 cl_err(dev, cl, "no such me client\n");
1689 if (mei_cl_is_fixed_address(cl))
1692 /* HW currently supports only one pending read */
1693 if (cl->rx_flow_ctrl_creds) {
1694 mei_cl_set_read_by_fp(cl, fp);
1698 cb = mei_cl_enqueue_ctrl_wr_cb(cl, length, MEI_FOP_READ, fp);
1702 mei_cl_set_read_by_fp(cl, fp);
1704 rets = pm_runtime_get(dev->dev);
1705 if (rets < 0 && rets != -EINPROGRESS) {
1706 pm_runtime_put_noidle(dev->dev);
1707 cl_err(dev, cl, "rpm: get failed %d\n", rets);
1712 if (mei_hbuf_acquire(dev)) {
1713 rets = mei_hbm_cl_flow_control_req(dev, cl);
1717 list_move_tail(&cb->list, &cl->rd_pending);
1719 cl->rx_flow_ctrl_creds++;
1722 cl_dbg(dev, cl, "rpm: autosuspend\n");
1723 pm_runtime_mark_last_busy(dev->dev);
1724 pm_runtime_put_autosuspend(dev->dev);
1732 static inline u8 mei_ext_hdr_set_vtag(void *ext, u8 vtag)
1734 struct mei_ext_hdr_vtag *vtag_hdr = ext;
1736 vtag_hdr->hdr.type = MEI_EXT_HDR_VTAG;
1737 vtag_hdr->hdr.length = mei_data2slots(sizeof(*vtag_hdr));
1738 vtag_hdr->vtag = vtag;
1739 vtag_hdr->reserved = 0;
1740 return vtag_hdr->hdr.length;
1744 * mei_msg_hdr_init - allocate and initialize mei message header
1746 * @cb: message callback structure
1748 * Return: a pointer to initialized header or ERR_PTR on failure
1750 static struct mei_msg_hdr *mei_msg_hdr_init(const struct mei_cl_cb *cb)
1753 struct mei_ext_meta_hdr *meta;
1754 struct mei_msg_hdr *mei_hdr;
1755 bool is_ext, is_vtag;
1758 return ERR_PTR(-EINVAL);
1760 /* Extended header for vtag is attached only on the first fragment */
1761 is_vtag = (cb->vtag && cb->buf_idx == 0);
1764 /* Compute extended header size */
1765 hdr_len = sizeof(*mei_hdr);
1770 hdr_len += sizeof(*meta);
1772 hdr_len += sizeof(struct mei_ext_hdr_vtag);
1775 mei_hdr = kzalloc(hdr_len, GFP_KERNEL);
1777 return ERR_PTR(-ENOMEM);
1779 mei_hdr->host_addr = mei_cl_host_addr(cb->cl);
1780 mei_hdr->me_addr = mei_cl_me_id(cb->cl);
1781 mei_hdr->internal = cb->internal;
1782 mei_hdr->extended = is_ext;
1787 meta = (struct mei_ext_meta_hdr *)mei_hdr->extension;
1790 meta->size += mei_ext_hdr_set_vtag(meta->hdrs, cb->vtag);
1793 mei_hdr->length = hdr_len - sizeof(*mei_hdr);
1798 * mei_cl_irq_write - write a message to device
1799 * from the interrupt thread context
1802 * @cb: callback block.
1803 * @cmpl_list: complete list.
1805 * Return: 0, OK; otherwise error.
1807 int mei_cl_irq_write(struct mei_cl *cl, struct mei_cl_cb *cb,
1808 struct list_head *cmpl_list)
1810 struct mei_device *dev;
1811 struct mei_msg_data *buf;
1812 struct mei_msg_hdr *mei_hdr = NULL;
1814 size_t hbuf_len, dr_len;
1824 if (WARN_ON(!cl || !cl->dev))
1831 first_chunk = cb->buf_idx == 0;
1833 rets = first_chunk ? mei_cl_tx_flow_ctrl_creds(cl) : 1;
1838 cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
1842 buf_len = buf->size - cb->buf_idx;
1843 data = buf->data + cb->buf_idx;
1844 hbuf_slots = mei_hbuf_empty_slots(dev);
1845 if (hbuf_slots < 0) {
1850 hbuf_len = mei_slots2data(hbuf_slots) & MEI_MSG_MAX_LEN_MASK;
1851 dr_slots = mei_dma_ring_empty_slots(dev);
1852 dr_len = mei_slots2data(dr_slots);
1854 mei_hdr = mei_msg_hdr_init(cb);
1855 if (IS_ERR(mei_hdr)) {
1856 rets = PTR_ERR(mei_hdr);
1861 cl_dbg(dev, cl, "Extended Header %d vtag = %d\n",
1862 mei_hdr->extended, cb->vtag);
1864 hdr_len = sizeof(*mei_hdr) + mei_hdr->length;
1867 * Split the message only if we can write the whole host buffer
1868 * otherwise wait for next time the host buffer is empty.
1870 if (hdr_len + buf_len <= hbuf_len) {
1872 mei_hdr->msg_complete = 1;
1873 } else if (dr_slots && hbuf_len >= hdr_len + sizeof(dma_len)) {
1874 mei_hdr->dma_ring = 1;
1875 if (buf_len > dr_len)
1878 mei_hdr->msg_complete = 1;
1880 data_len = sizeof(dma_len);
1883 } else if ((u32)hbuf_slots == mei_hbuf_depth(dev)) {
1884 buf_len = hbuf_len - hdr_len;
1890 mei_hdr->length += data_len;
1892 if (mei_hdr->dma_ring)
1893 mei_dma_ring_write(dev, buf->data + cb->buf_idx, buf_len);
1894 rets = mei_write_message(dev, mei_hdr, hdr_len, data, data_len);
1900 cl->writing_state = MEI_WRITING;
1901 cb->buf_idx += buf_len;
1904 if (mei_cl_tx_flow_ctrl_creds_reduce(cl)) {
1910 if (mei_hdr->msg_complete)
1911 list_move_tail(&cb->list, &dev->write_waiting_list);
1919 list_move_tail(&cb->list, cmpl_list);
1924 * mei_cl_write - submit a write cb to mei device
1925 * assumes device_lock is locked
1928 * @cb: write callback with filled data
1930 * Return: number of bytes sent on success, <0 on failure.
1932 ssize_t mei_cl_write(struct mei_cl *cl, struct mei_cl_cb *cb)
1934 struct mei_device *dev;
1935 struct mei_msg_data *buf;
1936 struct mei_msg_hdr *mei_hdr = NULL;
1938 size_t hbuf_len, dr_len;
1948 if (WARN_ON(!cl || !cl->dev))
1957 buf_len = buf->size;
1959 cl_dbg(dev, cl, "buf_len=%zd\n", buf_len);
1961 blocking = cb->blocking;
1964 rets = pm_runtime_get(dev->dev);
1965 if (rets < 0 && rets != -EINPROGRESS) {
1966 pm_runtime_put_noidle(dev->dev);
1967 cl_err(dev, cl, "rpm: get failed %zd\n", rets);
1972 cl->writing_state = MEI_IDLE;
1975 rets = mei_cl_tx_flow_ctrl_creds(cl);
1979 mei_hdr = mei_msg_hdr_init(cb);
1980 if (IS_ERR(mei_hdr)) {
1981 rets = -PTR_ERR(mei_hdr);
1986 cl_dbg(dev, cl, "Extended Header %d vtag = %d\n",
1987 mei_hdr->extended, cb->vtag);
1989 hdr_len = sizeof(*mei_hdr) + mei_hdr->length;
1992 cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
1997 if (!mei_hbuf_acquire(dev)) {
1998 cl_dbg(dev, cl, "Cannot acquire the host buffer: not sending.\n");
2003 hbuf_slots = mei_hbuf_empty_slots(dev);
2004 if (hbuf_slots < 0) {
2009 hbuf_len = mei_slots2data(hbuf_slots) & MEI_MSG_MAX_LEN_MASK;
2010 dr_slots = mei_dma_ring_empty_slots(dev);
2011 dr_len = mei_slots2data(dr_slots);
2013 if (hdr_len + buf_len <= hbuf_len) {
2015 mei_hdr->msg_complete = 1;
2016 } else if (dr_slots && hbuf_len >= hdr_len + sizeof(dma_len)) {
2017 mei_hdr->dma_ring = 1;
2018 if (buf_len > dr_len)
2021 mei_hdr->msg_complete = 1;
2023 data_len = sizeof(dma_len);
2027 buf_len = hbuf_len - hdr_len;
2031 mei_hdr->length += data_len;
2033 if (mei_hdr->dma_ring)
2034 mei_dma_ring_write(dev, buf->data, buf_len);
2035 rets = mei_write_message(dev, mei_hdr, hdr_len, data, data_len);
2040 rets = mei_cl_tx_flow_ctrl_creds_reduce(cl);
2044 cl->writing_state = MEI_WRITING;
2045 cb->buf_idx = buf_len;
2046 /* restore return value */
2047 buf_len = buf->size;
2050 if (mei_hdr->msg_complete)
2051 mei_tx_cb_enqueue(cb, &dev->write_waiting_list);
2053 mei_tx_cb_enqueue(cb, &dev->write_list);
2056 if (blocking && cl->writing_state != MEI_WRITE_COMPLETE) {
2058 mutex_unlock(&dev->device_lock);
2059 rets = wait_event_interruptible(cl->tx_wait,
2060 cl->writing_state == MEI_WRITE_COMPLETE ||
2061 (!mei_cl_is_connected(cl)));
2062 mutex_lock(&dev->device_lock);
2063 /* wait_event_interruptible returns -ERESTARTSYS */
2065 if (signal_pending(current))
2069 if (cl->writing_state != MEI_WRITE_COMPLETE) {
2077 cl_dbg(dev, cl, "rpm: autosuspend\n");
2078 pm_runtime_mark_last_busy(dev->dev);
2079 pm_runtime_put_autosuspend(dev->dev);
2089 * mei_cl_complete - processes completed operation for a client
2091 * @cl: private data of the file object.
2092 * @cb: callback block.
2094 void mei_cl_complete(struct mei_cl *cl, struct mei_cl_cb *cb)
2096 struct mei_device *dev = cl->dev;
2098 switch (cb->fop_type) {
2100 mei_tx_cb_dequeue(cb);
2101 cl->writing_state = MEI_WRITE_COMPLETE;
2102 if (waitqueue_active(&cl->tx_wait)) {
2103 wake_up_interruptible(&cl->tx_wait);
2105 pm_runtime_mark_last_busy(dev->dev);
2106 pm_request_autosuspend(dev->dev);
2111 mei_cl_add_rd_completed(cl, cb);
2112 if (!mei_cl_is_fixed_address(cl) &&
2113 !WARN_ON(!cl->rx_flow_ctrl_creds))
2114 cl->rx_flow_ctrl_creds--;
2115 if (!mei_cl_bus_rx_event(cl))
2116 wake_up_interruptible(&cl->rx_wait);
2119 case MEI_FOP_CONNECT:
2120 case MEI_FOP_DISCONNECT:
2121 case MEI_FOP_NOTIFY_STOP:
2122 case MEI_FOP_NOTIFY_START:
2123 case MEI_FOP_DMA_MAP:
2124 case MEI_FOP_DMA_UNMAP:
2125 if (waitqueue_active(&cl->wait))
2129 case MEI_FOP_DISCONNECT_RSP:
2131 mei_cl_set_disconnected(cl);
2140 * mei_cl_all_disconnect - disconnect forcefully all connected clients
2144 void mei_cl_all_disconnect(struct mei_device *dev)
2148 list_for_each_entry(cl, &dev->file_list, link)
2149 mei_cl_set_disconnected(cl);
2152 static struct mei_cl *mei_cl_dma_map_find(struct mei_device *dev, u8 buffer_id)
2156 list_for_each_entry(cl, &dev->file_list, link)
2157 if (cl->dma.buffer_id == buffer_id)
2163 * mei_cl_irq_dma_map - send client dma map request in irq_thread context
2166 * @cb: callback block.
2167 * @cmpl_list: complete list.
2169 * Return: 0 on such and error otherwise.
2171 int mei_cl_irq_dma_map(struct mei_cl *cl, struct mei_cl_cb *cb,
2172 struct list_head *cmpl_list)
2174 struct mei_device *dev = cl->dev;
2179 msg_slots = mei_hbm2slots(sizeof(struct hbm_client_dma_map_request));
2180 slots = mei_hbuf_empty_slots(dev);
2184 if ((u32)slots < msg_slots)
2187 ret = mei_hbm_cl_dma_map_req(dev, cl);
2190 list_move_tail(&cb->list, cmpl_list);
2194 list_move_tail(&cb->list, &dev->ctrl_rd_list);
2199 * mei_cl_irq_dma_unmap - send client dma unmap request in irq_thread context
2202 * @cb: callback block.
2203 * @cmpl_list: complete list.
2205 * Return: 0 on such and error otherwise.
2207 int mei_cl_irq_dma_unmap(struct mei_cl *cl, struct mei_cl_cb *cb,
2208 struct list_head *cmpl_list)
2210 struct mei_device *dev = cl->dev;
2215 msg_slots = mei_hbm2slots(sizeof(struct hbm_client_dma_unmap_request));
2216 slots = mei_hbuf_empty_slots(dev);
2220 if ((u32)slots < msg_slots)
2223 ret = mei_hbm_cl_dma_unmap_req(dev, cl);
2226 list_move_tail(&cb->list, cmpl_list);
2230 list_move_tail(&cb->list, &dev->ctrl_rd_list);
2234 static int mei_cl_dma_alloc(struct mei_cl *cl, u8 buf_id, size_t size)
2236 cl->dma.vaddr = dmam_alloc_coherent(cl->dev->dev, size,
2237 &cl->dma.daddr, GFP_KERNEL);
2241 cl->dma.buffer_id = buf_id;
2242 cl->dma.size = size;
2247 static void mei_cl_dma_free(struct mei_cl *cl)
2249 cl->dma.buffer_id = 0;
2250 dmam_free_coherent(cl->dev->dev,
2251 cl->dma.size, cl->dma.vaddr, cl->dma.daddr);
2253 cl->dma.vaddr = NULL;
2258 * mei_cl_dma_alloc_and_map - send client dma map request
2261 * @fp: pointer to file structure
2262 * @buffer_id: id of the mapped buffer
2263 * @size: size of the buffer
2265 * Locking: called under "dev->device_lock" lock
2274 int mei_cl_dma_alloc_and_map(struct mei_cl *cl, const struct file *fp,
2275 u8 buffer_id, size_t size)
2277 struct mei_device *dev;
2278 struct mei_cl_cb *cb;
2281 if (WARN_ON(!cl || !cl->dev))
2286 if (!dev->hbm_f_cd_supported) {
2287 cl_dbg(dev, cl, "client dma is not supported\n");
2294 if (mei_cl_is_connected(cl))
2300 if (mei_cl_dma_map_find(dev, buffer_id)) {
2301 cl_dbg(dev, cl, "client dma with id %d is already allocated\n",
2306 rets = pm_runtime_get(dev->dev);
2307 if (rets < 0 && rets != -EINPROGRESS) {
2308 pm_runtime_put_noidle(dev->dev);
2309 cl_err(dev, cl, "rpm: get failed %d\n", rets);
2313 rets = mei_cl_dma_alloc(cl, buffer_id, size);
2315 pm_runtime_put_noidle(dev->dev);
2319 cb = mei_cl_enqueue_ctrl_wr_cb(cl, 0, MEI_FOP_DMA_MAP, fp);
2325 if (mei_hbuf_acquire(dev)) {
2326 if (mei_hbm_cl_dma_map_req(dev, cl)) {
2330 list_move_tail(&cb->list, &dev->ctrl_rd_list);
2333 mutex_unlock(&dev->device_lock);
2334 wait_event_timeout(cl->wait,
2335 cl->dma_mapped || cl->status,
2336 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
2337 mutex_lock(&dev->device_lock);
2339 if (!cl->dma_mapped && !cl->status)
2340 cl->status = -EFAULT;
2346 mei_cl_dma_free(cl);
2348 cl_dbg(dev, cl, "rpm: autosuspend\n");
2349 pm_runtime_mark_last_busy(dev->dev);
2350 pm_runtime_put_autosuspend(dev->dev);
2357 * mei_cl_dma_unmap - send client dma unmap request
2360 * @fp: pointer to file structure
2362 * Locking: called under "dev->device_lock" lock
2364 * Return: 0 on such and error otherwise.
2366 int mei_cl_dma_unmap(struct mei_cl *cl, const struct file *fp)
2368 struct mei_device *dev;
2369 struct mei_cl_cb *cb;
2372 if (WARN_ON(!cl || !cl->dev))
2377 if (!dev->hbm_f_cd_supported) {
2378 cl_dbg(dev, cl, "client dma is not supported\n");
2382 /* do not allow unmap for connected client */
2383 if (mei_cl_is_connected(cl))
2386 if (!cl->dma_mapped)
2389 rets = pm_runtime_get(dev->dev);
2390 if (rets < 0 && rets != -EINPROGRESS) {
2391 pm_runtime_put_noidle(dev->dev);
2392 cl_err(dev, cl, "rpm: get failed %d\n", rets);
2396 cb = mei_cl_enqueue_ctrl_wr_cb(cl, 0, MEI_FOP_DMA_UNMAP, fp);
2402 if (mei_hbuf_acquire(dev)) {
2403 if (mei_hbm_cl_dma_unmap_req(dev, cl)) {
2407 list_move_tail(&cb->list, &dev->ctrl_rd_list);
2410 mutex_unlock(&dev->device_lock);
2411 wait_event_timeout(cl->wait,
2412 !cl->dma_mapped || cl->status,
2413 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
2414 mutex_lock(&dev->device_lock);
2416 if (cl->dma_mapped && !cl->status)
2417 cl->status = -EFAULT;
2422 mei_cl_dma_free(cl);
2424 cl_dbg(dev, cl, "rpm: autosuspend\n");
2425 pm_runtime_mark_last_busy(dev->dev);
2426 pm_runtime_put_autosuspend(dev->dev);