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_queue - 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 (dev->open_handle_count > 0)
704 dev->open_handle_count--;
706 /* never clear the 0 bit */
707 if (cl->host_client_id)
708 clear_bit(cl->host_client_id, dev->host_clients_map);
710 list_del_init(&cl->link);
712 cl->state = MEI_FILE_UNINITIALIZED;
713 cl->writing_state = MEI_IDLE;
715 WARN_ON(!list_empty(&cl->rd_completed) ||
716 !list_empty(&cl->rd_pending) ||
717 !list_empty(&cl->link));
722 void mei_host_client_init(struct mei_device *dev)
724 mei_set_devstate(dev, MEI_DEV_ENABLED);
725 dev->reset_count = 0;
727 schedule_work(&dev->bus_rescan_work);
729 pm_runtime_mark_last_busy(dev->dev);
730 dev_dbg(dev->dev, "rpm: autosuspend\n");
731 pm_request_autosuspend(dev->dev);
735 * mei_hbuf_acquire - try to acquire host buffer
737 * @dev: the device structure
738 * Return: true if host buffer was acquired
740 bool mei_hbuf_acquire(struct mei_device *dev)
742 if (mei_pg_state(dev) == MEI_PG_ON ||
743 mei_pg_in_transition(dev)) {
744 dev_dbg(dev->dev, "device is in pg\n");
748 if (!dev->hbuf_is_ready) {
749 dev_dbg(dev->dev, "hbuf is not ready\n");
753 dev->hbuf_is_ready = false;
759 * mei_cl_wake_all - wake up readers, writers and event waiters so
760 * they can be interrupted
764 static void mei_cl_wake_all(struct mei_cl *cl)
766 struct mei_device *dev = cl->dev;
768 /* synchronized under device mutex */
769 if (waitqueue_active(&cl->rx_wait)) {
770 cl_dbg(dev, cl, "Waking up reading client!\n");
771 wake_up_interruptible(&cl->rx_wait);
773 /* synchronized under device mutex */
774 if (waitqueue_active(&cl->tx_wait)) {
775 cl_dbg(dev, cl, "Waking up writing client!\n");
776 wake_up_interruptible(&cl->tx_wait);
778 /* synchronized under device mutex */
779 if (waitqueue_active(&cl->ev_wait)) {
780 cl_dbg(dev, cl, "Waking up waiting for event clients!\n");
781 wake_up_interruptible(&cl->ev_wait);
783 /* synchronized under device mutex */
784 if (waitqueue_active(&cl->wait)) {
785 cl_dbg(dev, cl, "Waking up ctrl write clients!\n");
791 * mei_cl_set_disconnected - set disconnected state and clear
792 * associated states and resources
796 static void mei_cl_set_disconnected(struct mei_cl *cl)
798 struct mei_device *dev = cl->dev;
800 if (cl->state == MEI_FILE_DISCONNECTED ||
801 cl->state <= MEI_FILE_INITIALIZING)
804 cl->state = MEI_FILE_DISCONNECTED;
805 mei_io_tx_list_free_cl(&dev->write_list, cl, NULL);
806 mei_io_tx_list_free_cl(&dev->write_waiting_list, cl, NULL);
807 mei_io_list_flush_cl(&dev->ctrl_rd_list, cl);
808 mei_io_list_flush_cl(&dev->ctrl_wr_list, cl);
810 cl->rx_flow_ctrl_creds = 0;
811 cl->tx_flow_ctrl_creds = 0;
817 if (!WARN_ON(cl->me_cl->connect_count == 0))
818 cl->me_cl->connect_count--;
820 if (cl->me_cl->connect_count == 0)
821 cl->me_cl->tx_flow_ctrl_creds = 0;
823 mei_me_cl_put(cl->me_cl);
827 static int mei_cl_set_connecting(struct mei_cl *cl, struct mei_me_client *me_cl)
829 if (!mei_me_cl_get(me_cl))
832 /* only one connection is allowed for fixed address clients */
833 if (me_cl->props.fixed_address) {
834 if (me_cl->connect_count) {
835 mei_me_cl_put(me_cl);
841 cl->state = MEI_FILE_CONNECTING;
842 cl->me_cl->connect_count++;
848 * mei_cl_send_disconnect - send disconnect request
851 * @cb: callback block
853 * Return: 0, OK; otherwise, error.
855 static int mei_cl_send_disconnect(struct mei_cl *cl, struct mei_cl_cb *cb)
857 struct mei_device *dev;
862 ret = mei_hbm_cl_disconnect_req(dev, cl);
865 cl->state = MEI_FILE_DISCONNECT_REPLY;
869 list_move_tail(&cb->list, &dev->ctrl_rd_list);
870 cl->timer_count = MEI_CONNECT_TIMEOUT;
871 mei_schedule_stall_timer(dev);
877 * mei_cl_irq_disconnect - processes close related operation from
878 * interrupt thread context - send disconnect request
881 * @cb: callback block.
882 * @cmpl_list: complete list.
884 * Return: 0, OK; otherwise, error.
886 int mei_cl_irq_disconnect(struct mei_cl *cl, struct mei_cl_cb *cb,
887 struct list_head *cmpl_list)
889 struct mei_device *dev = cl->dev;
894 msg_slots = mei_hbm2slots(sizeof(struct hbm_client_connect_request));
895 slots = mei_hbuf_empty_slots(dev);
899 if ((u32)slots < msg_slots)
902 ret = mei_cl_send_disconnect(cl, cb);
904 list_move_tail(&cb->list, cmpl_list);
910 * __mei_cl_disconnect - disconnect host client from the me one
911 * internal function runtime pm has to be already acquired
915 * Return: 0 on success, <0 on failure.
917 static int __mei_cl_disconnect(struct mei_cl *cl)
919 struct mei_device *dev;
920 struct mei_cl_cb *cb;
925 cl->state = MEI_FILE_DISCONNECTING;
927 cb = mei_cl_enqueue_ctrl_wr_cb(cl, 0, MEI_FOP_DISCONNECT, NULL);
933 if (mei_hbuf_acquire(dev)) {
934 rets = mei_cl_send_disconnect(cl, cb);
936 cl_err(dev, cl, "failed to disconnect.\n");
941 mutex_unlock(&dev->device_lock);
942 wait_event_timeout(cl->wait,
943 cl->state == MEI_FILE_DISCONNECT_REPLY ||
944 cl->state == MEI_FILE_DISCONNECTED,
945 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
946 mutex_lock(&dev->device_lock);
949 if (cl->state != MEI_FILE_DISCONNECT_REPLY &&
950 cl->state != MEI_FILE_DISCONNECTED) {
951 cl_dbg(dev, cl, "timeout on disconnect from FW client.\n");
956 /* we disconnect also on error */
957 mei_cl_set_disconnected(cl);
959 cl_dbg(dev, cl, "successfully disconnected from FW client.\n");
966 * mei_cl_disconnect - disconnect host client from the me one
970 * Locking: called under "dev->device_lock" lock
972 * Return: 0 on success, <0 on failure.
974 int mei_cl_disconnect(struct mei_cl *cl)
976 struct mei_device *dev;
979 if (WARN_ON(!cl || !cl->dev))
984 cl_dbg(dev, cl, "disconnecting");
986 if (!mei_cl_is_connected(cl))
989 if (mei_cl_is_fixed_address(cl)) {
990 mei_cl_set_disconnected(cl);
994 if (dev->dev_state == MEI_DEV_POWERING_DOWN ||
995 dev->dev_state == MEI_DEV_POWER_DOWN) {
996 cl_dbg(dev, cl, "Device is powering down, don't bother with disconnection\n");
997 mei_cl_set_disconnected(cl);
1001 rets = pm_runtime_get(dev->dev);
1002 if (rets < 0 && rets != -EINPROGRESS) {
1003 pm_runtime_put_noidle(dev->dev);
1004 cl_err(dev, cl, "rpm: get failed %d\n", rets);
1008 rets = __mei_cl_disconnect(cl);
1010 cl_dbg(dev, cl, "rpm: autosuspend\n");
1011 pm_runtime_mark_last_busy(dev->dev);
1012 pm_runtime_put_autosuspend(dev->dev);
1019 * mei_cl_is_other_connecting - checks if other
1020 * client with the same me client id is connecting
1022 * @cl: private data of the file object
1024 * Return: true if other client is connected, false - otherwise.
1026 static bool mei_cl_is_other_connecting(struct mei_cl *cl)
1028 struct mei_device *dev;
1029 struct mei_cl_cb *cb;
1033 list_for_each_entry(cb, &dev->ctrl_rd_list, list) {
1034 if (cb->fop_type == MEI_FOP_CONNECT &&
1035 mei_cl_me_id(cl) == mei_cl_me_id(cb->cl))
1043 * mei_cl_send_connect - send connect request
1046 * @cb: callback block
1048 * Return: 0, OK; otherwise, error.
1050 static int mei_cl_send_connect(struct mei_cl *cl, struct mei_cl_cb *cb)
1052 struct mei_device *dev;
1057 ret = mei_hbm_cl_connect_req(dev, cl);
1060 cl->state = MEI_FILE_DISCONNECT_REPLY;
1064 list_move_tail(&cb->list, &dev->ctrl_rd_list);
1065 cl->timer_count = MEI_CONNECT_TIMEOUT;
1066 mei_schedule_stall_timer(dev);
1071 * mei_cl_irq_connect - send connect request in irq_thread context
1074 * @cb: callback block
1075 * @cmpl_list: complete list
1077 * Return: 0, OK; otherwise, error.
1079 int mei_cl_irq_connect(struct mei_cl *cl, struct mei_cl_cb *cb,
1080 struct list_head *cmpl_list)
1082 struct mei_device *dev = cl->dev;
1087 if (mei_cl_is_other_connecting(cl))
1090 msg_slots = mei_hbm2slots(sizeof(struct hbm_client_connect_request));
1091 slots = mei_hbuf_empty_slots(dev);
1095 if ((u32)slots < msg_slots)
1098 rets = mei_cl_send_connect(cl, cb);
1100 list_move_tail(&cb->list, cmpl_list);
1106 * mei_cl_connect - connect host client to the me one
1110 * @fp: pointer to file structure
1112 * Locking: called under "dev->device_lock" lock
1114 * Return: 0 on success, <0 on failure.
1116 int mei_cl_connect(struct mei_cl *cl, struct mei_me_client *me_cl,
1117 const struct file *fp)
1119 struct mei_device *dev;
1120 struct mei_cl_cb *cb;
1123 if (WARN_ON(!cl || !cl->dev || !me_cl))
1128 rets = mei_cl_set_connecting(cl, me_cl);
1132 if (mei_cl_is_fixed_address(cl)) {
1133 cl->state = MEI_FILE_CONNECTED;
1138 rets = pm_runtime_get(dev->dev);
1139 if (rets < 0 && rets != -EINPROGRESS) {
1140 pm_runtime_put_noidle(dev->dev);
1141 cl_err(dev, cl, "rpm: get failed %d\n", rets);
1145 cb = mei_cl_enqueue_ctrl_wr_cb(cl, 0, MEI_FOP_CONNECT, fp);
1151 /* run hbuf acquire last so we don't have to undo */
1152 if (!mei_cl_is_other_connecting(cl) && mei_hbuf_acquire(dev)) {
1153 rets = mei_cl_send_connect(cl, cb);
1158 mutex_unlock(&dev->device_lock);
1159 wait_event_timeout(cl->wait,
1160 (cl->state == MEI_FILE_CONNECTED ||
1161 cl->state == MEI_FILE_DISCONNECTED ||
1162 cl->state == MEI_FILE_DISCONNECT_REQUIRED ||
1163 cl->state == MEI_FILE_DISCONNECT_REPLY),
1164 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
1165 mutex_lock(&dev->device_lock);
1167 if (!mei_cl_is_connected(cl)) {
1168 if (cl->state == MEI_FILE_DISCONNECT_REQUIRED) {
1169 mei_io_list_flush_cl(&dev->ctrl_rd_list, cl);
1170 mei_io_list_flush_cl(&dev->ctrl_wr_list, cl);
1171 /* ignore disconnect return valuue;
1172 * in case of failure reset will be invoked
1174 __mei_cl_disconnect(cl);
1179 /* timeout or something went really wrong */
1181 cl->status = -EFAULT;
1186 cl_dbg(dev, cl, "rpm: autosuspend\n");
1187 pm_runtime_mark_last_busy(dev->dev);
1188 pm_runtime_put_autosuspend(dev->dev);
1193 if (!mei_cl_is_connected(cl))
1194 mei_cl_set_disconnected(cl);
1200 * mei_cl_alloc_linked - allocate and link host client
1202 * @dev: the device structure
1204 * Return: cl on success ERR_PTR on failure
1206 struct mei_cl *mei_cl_alloc_linked(struct mei_device *dev)
1211 cl = mei_cl_allocate(dev);
1217 ret = mei_cl_link(cl);
1224 return ERR_PTR(ret);
1228 * mei_cl_tx_flow_ctrl_creds - checks flow_control credits for cl.
1232 * Return: 1 if tx_flow_ctrl_creds >0, 0 - otherwise.
1234 static int mei_cl_tx_flow_ctrl_creds(struct mei_cl *cl)
1236 if (WARN_ON(!cl || !cl->me_cl))
1239 if (cl->tx_flow_ctrl_creds > 0)
1242 if (mei_cl_is_fixed_address(cl))
1245 if (mei_cl_is_single_recv_buf(cl)) {
1246 if (cl->me_cl->tx_flow_ctrl_creds > 0)
1253 * mei_cl_tx_flow_ctrl_creds_reduce - reduces transmit flow control credits
1260 * -EINVAL when ctrl credits are <= 0
1262 static int mei_cl_tx_flow_ctrl_creds_reduce(struct mei_cl *cl)
1264 if (WARN_ON(!cl || !cl->me_cl))
1267 if (mei_cl_is_fixed_address(cl))
1270 if (mei_cl_is_single_recv_buf(cl)) {
1271 if (WARN_ON(cl->me_cl->tx_flow_ctrl_creds <= 0))
1273 cl->me_cl->tx_flow_ctrl_creds--;
1275 if (WARN_ON(cl->tx_flow_ctrl_creds <= 0))
1277 cl->tx_flow_ctrl_creds--;
1283 * mei_cl_vtag_alloc - allocate and fill the vtag structure
1285 * @fp: pointer to file structure
1289 * * Pointer to allocated struct - on success
1290 * * ERR_PTR(-ENOMEM) on memory allocation failure
1292 struct mei_cl_vtag *mei_cl_vtag_alloc(struct file *fp, u8 vtag)
1294 struct mei_cl_vtag *cl_vtag;
1296 cl_vtag = kzalloc(sizeof(*cl_vtag), GFP_KERNEL);
1298 return ERR_PTR(-ENOMEM);
1300 INIT_LIST_HEAD(&cl_vtag->list);
1301 cl_vtag->vtag = vtag;
1308 * mei_cl_fp_by_vtag - obtain the file pointer by vtag
1311 * @vtag: virtual tag
1314 * * A file pointer - on success
1315 * * ERR_PTR(-ENOENT) if vtag is not found in the client vtag list
1317 const struct file *mei_cl_fp_by_vtag(const struct mei_cl *cl, u8 vtag)
1319 struct mei_cl_vtag *vtag_l;
1321 list_for_each_entry(vtag_l, &cl->vtag_map, list)
1322 /* The client on bus has one fixed fp */
1323 if ((cl->cldev && mei_cldev_enabled(cl->cldev)) ||
1324 vtag_l->vtag == vtag)
1327 return ERR_PTR(-ENOENT);
1331 * mei_cl_reset_read_by_vtag - reset pending_read flag by given vtag
1336 static void mei_cl_reset_read_by_vtag(const struct mei_cl *cl, u8 vtag)
1338 struct mei_cl_vtag *vtag_l;
1340 list_for_each_entry(vtag_l, &cl->vtag_map, list) {
1341 if (vtag_l->vtag == vtag) {
1342 vtag_l->pending_read = false;
1349 * mei_cl_read_vtag_add_fc - add flow control for next pending reader
1354 static void mei_cl_read_vtag_add_fc(struct mei_cl *cl)
1356 struct mei_cl_vtag *cl_vtag;
1358 list_for_each_entry(cl_vtag, &cl->vtag_map, list) {
1359 if (cl_vtag->pending_read) {
1360 if (mei_cl_enqueue_ctrl_wr_cb(cl,
1364 cl->rx_flow_ctrl_creds++;
1371 * mei_cl_vt_support_check - check if client support vtags
1376 * * 0 - supported, or not connected at all
1377 * * -EOPNOTSUPP - vtags are not supported by client
1379 int mei_cl_vt_support_check(const struct mei_cl *cl)
1381 struct mei_device *dev = cl->dev;
1383 if (!dev->hbm_f_vt_supported)
1389 return cl->me_cl->props.vt_supported ? 0 : -EOPNOTSUPP;
1393 * mei_cl_add_rd_completed - add read completed callback to list with lock
1397 * @cb: callback block
1400 void mei_cl_add_rd_completed(struct mei_cl *cl, struct mei_cl_cb *cb)
1402 const struct file *fp;
1404 if (!mei_cl_vt_support_check(cl)) {
1405 fp = mei_cl_fp_by_vtag(cl, cb->vtag);
1407 /* client already disconnected, discarding */
1412 mei_cl_reset_read_by_vtag(cl, cb->vtag);
1413 mei_cl_read_vtag_add_fc(cl);
1416 spin_lock(&cl->rd_completed_lock);
1417 list_add_tail(&cb->list, &cl->rd_completed);
1418 spin_unlock(&cl->rd_completed_lock);
1422 * mei_cl_del_rd_completed - free read completed callback with lock
1425 * @cb: callback block
1428 void mei_cl_del_rd_completed(struct mei_cl *cl, struct mei_cl_cb *cb)
1430 spin_lock(&cl->rd_completed_lock);
1432 spin_unlock(&cl->rd_completed_lock);
1436 * mei_cl_notify_fop2req - convert fop to proper request
1438 * @fop: client notification start response command
1440 * Return: MEI_HBM_NOTIFICATION_START/STOP
1442 u8 mei_cl_notify_fop2req(enum mei_cb_file_ops fop)
1444 if (fop == MEI_FOP_NOTIFY_START)
1445 return MEI_HBM_NOTIFICATION_START;
1447 return MEI_HBM_NOTIFICATION_STOP;
1451 * mei_cl_notify_req2fop - convert notification request top file operation type
1453 * @req: hbm notification request type
1455 * Return: MEI_FOP_NOTIFY_START/STOP
1457 enum mei_cb_file_ops mei_cl_notify_req2fop(u8 req)
1459 if (req == MEI_HBM_NOTIFICATION_START)
1460 return MEI_FOP_NOTIFY_START;
1462 return MEI_FOP_NOTIFY_STOP;
1466 * mei_cl_irq_notify - send notification request in irq_thread context
1469 * @cb: callback block.
1470 * @cmpl_list: complete list.
1472 * Return: 0 on such and error otherwise.
1474 int mei_cl_irq_notify(struct mei_cl *cl, struct mei_cl_cb *cb,
1475 struct list_head *cmpl_list)
1477 struct mei_device *dev = cl->dev;
1483 msg_slots = mei_hbm2slots(sizeof(struct hbm_client_connect_request));
1484 slots = mei_hbuf_empty_slots(dev);
1488 if ((u32)slots < msg_slots)
1491 request = mei_cl_notify_fop2req(cb->fop_type);
1492 ret = mei_hbm_cl_notify_req(dev, cl, request);
1495 list_move_tail(&cb->list, cmpl_list);
1499 list_move_tail(&cb->list, &dev->ctrl_rd_list);
1504 * mei_cl_notify_request - send notification stop/start request
1507 * @fp: associate request with file
1508 * @request: 1 for start or 0 for stop
1510 * Locking: called under "dev->device_lock" lock
1512 * Return: 0 on such and error otherwise.
1514 int mei_cl_notify_request(struct mei_cl *cl,
1515 const struct file *fp, u8 request)
1517 struct mei_device *dev;
1518 struct mei_cl_cb *cb;
1519 enum mei_cb_file_ops fop_type;
1522 if (WARN_ON(!cl || !cl->dev))
1527 if (!dev->hbm_f_ev_supported) {
1528 cl_dbg(dev, cl, "notifications not supported\n");
1532 if (!mei_cl_is_connected(cl))
1535 rets = pm_runtime_get(dev->dev);
1536 if (rets < 0 && rets != -EINPROGRESS) {
1537 pm_runtime_put_noidle(dev->dev);
1538 cl_err(dev, cl, "rpm: get failed %d\n", rets);
1542 fop_type = mei_cl_notify_req2fop(request);
1543 cb = mei_cl_enqueue_ctrl_wr_cb(cl, 0, fop_type, fp);
1549 if (mei_hbuf_acquire(dev)) {
1550 if (mei_hbm_cl_notify_req(dev, cl, request)) {
1554 list_move_tail(&cb->list, &dev->ctrl_rd_list);
1557 mutex_unlock(&dev->device_lock);
1558 wait_event_timeout(cl->wait,
1559 cl->notify_en == request ||
1561 !mei_cl_is_connected(cl),
1562 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
1563 mutex_lock(&dev->device_lock);
1565 if (cl->notify_en != request && !cl->status)
1566 cl->status = -EFAULT;
1571 cl_dbg(dev, cl, "rpm: autosuspend\n");
1572 pm_runtime_mark_last_busy(dev->dev);
1573 pm_runtime_put_autosuspend(dev->dev);
1580 * mei_cl_notify - raise notification
1584 * Locking: called under "dev->device_lock" lock
1586 void mei_cl_notify(struct mei_cl *cl)
1588 struct mei_device *dev;
1590 if (!cl || !cl->dev)
1598 cl_dbg(dev, cl, "notify event");
1599 cl->notify_ev = true;
1600 if (!mei_cl_bus_notify_event(cl))
1601 wake_up_interruptible(&cl->ev_wait);
1604 kill_fasync(&cl->ev_async, SIGIO, POLL_PRI);
1609 * mei_cl_notify_get - get or wait for notification event
1612 * @block: this request is blocking
1613 * @notify_ev: true if notification event was received
1615 * Locking: called under "dev->device_lock" lock
1617 * Return: 0 on such and error otherwise.
1619 int mei_cl_notify_get(struct mei_cl *cl, bool block, bool *notify_ev)
1621 struct mei_device *dev;
1626 if (WARN_ON(!cl || !cl->dev))
1631 if (!dev->hbm_f_ev_supported) {
1632 cl_dbg(dev, cl, "notifications not supported\n");
1636 if (!mei_cl_is_connected(cl))
1645 mutex_unlock(&dev->device_lock);
1646 rets = wait_event_interruptible(cl->ev_wait, cl->notify_ev);
1647 mutex_lock(&dev->device_lock);
1653 *notify_ev = cl->notify_ev;
1654 cl->notify_ev = false;
1659 * mei_cl_read_start - the start read client message function.
1662 * @length: number of bytes to read
1663 * @fp: pointer to file structure
1665 * Return: 0 on success, <0 on failure.
1667 int mei_cl_read_start(struct mei_cl *cl, size_t length, const struct file *fp)
1669 struct mei_device *dev;
1670 struct mei_cl_cb *cb;
1673 if (WARN_ON(!cl || !cl->dev))
1678 if (!mei_cl_is_connected(cl))
1681 if (!mei_me_cl_is_active(cl->me_cl)) {
1682 cl_err(dev, cl, "no such me client\n");
1686 if (mei_cl_is_fixed_address(cl))
1689 /* HW currently supports only one pending read */
1690 if (cl->rx_flow_ctrl_creds) {
1691 mei_cl_set_read_by_fp(cl, fp);
1695 cb = mei_cl_enqueue_ctrl_wr_cb(cl, length, MEI_FOP_READ, fp);
1699 mei_cl_set_read_by_fp(cl, fp);
1701 rets = pm_runtime_get(dev->dev);
1702 if (rets < 0 && rets != -EINPROGRESS) {
1703 pm_runtime_put_noidle(dev->dev);
1704 cl_err(dev, cl, "rpm: get failed %d\n", rets);
1709 if (mei_hbuf_acquire(dev)) {
1710 rets = mei_hbm_cl_flow_control_req(dev, cl);
1714 list_move_tail(&cb->list, &cl->rd_pending);
1716 cl->rx_flow_ctrl_creds++;
1719 cl_dbg(dev, cl, "rpm: autosuspend\n");
1720 pm_runtime_mark_last_busy(dev->dev);
1721 pm_runtime_put_autosuspend(dev->dev);
1729 static inline u8 mei_ext_hdr_set_vtag(struct mei_ext_hdr *ext, u8 vtag)
1731 ext->type = MEI_EXT_HDR_VTAG;
1732 ext->ext_payload[0] = vtag;
1733 ext->length = mei_data2slots(sizeof(*ext));
1738 * mei_msg_hdr_init - allocate and initialize mei message header
1740 * @cb: message callback structure
1742 * Return: a pointer to initialized header or ERR_PTR on failure
1744 static struct mei_msg_hdr *mei_msg_hdr_init(const struct mei_cl_cb *cb)
1747 struct mei_ext_meta_hdr *meta;
1748 struct mei_ext_hdr *ext;
1749 struct mei_msg_hdr *mei_hdr;
1750 bool is_ext, is_vtag;
1753 return ERR_PTR(-EINVAL);
1755 /* Extended header for vtag is attached only on the first fragment */
1756 is_vtag = (cb->vtag && cb->buf_idx == 0);
1759 /* Compute extended header size */
1760 hdr_len = sizeof(*mei_hdr);
1765 hdr_len += sizeof(*meta);
1767 hdr_len += sizeof(*ext);
1770 mei_hdr = kzalloc(hdr_len, GFP_KERNEL);
1772 return ERR_PTR(-ENOMEM);
1774 mei_hdr->host_addr = mei_cl_host_addr(cb->cl);
1775 mei_hdr->me_addr = mei_cl_me_id(cb->cl);
1776 mei_hdr->internal = cb->internal;
1777 mei_hdr->extended = is_ext;
1782 meta = (struct mei_ext_meta_hdr *)mei_hdr->extension;
1785 meta->size += mei_ext_hdr_set_vtag(meta->hdrs, cb->vtag);
1788 mei_hdr->length = hdr_len - sizeof(*mei_hdr);
1793 * mei_cl_irq_write - write a message to device
1794 * from the interrupt thread context
1797 * @cb: callback block.
1798 * @cmpl_list: complete list.
1800 * Return: 0, OK; otherwise error.
1802 int mei_cl_irq_write(struct mei_cl *cl, struct mei_cl_cb *cb,
1803 struct list_head *cmpl_list)
1805 struct mei_device *dev;
1806 struct mei_msg_data *buf;
1807 struct mei_msg_hdr *mei_hdr = NULL;
1809 size_t hbuf_len, dr_len;
1819 if (WARN_ON(!cl || !cl->dev))
1826 first_chunk = cb->buf_idx == 0;
1828 rets = first_chunk ? mei_cl_tx_flow_ctrl_creds(cl) : 1;
1833 cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
1837 buf_len = buf->size - cb->buf_idx;
1838 data = buf->data + cb->buf_idx;
1839 hbuf_slots = mei_hbuf_empty_slots(dev);
1840 if (hbuf_slots < 0) {
1845 hbuf_len = mei_slots2data(hbuf_slots) & MEI_MSG_MAX_LEN_MASK;
1846 dr_slots = mei_dma_ring_empty_slots(dev);
1847 dr_len = mei_slots2data(dr_slots);
1849 mei_hdr = mei_msg_hdr_init(cb);
1850 if (IS_ERR(mei_hdr)) {
1851 rets = PTR_ERR(mei_hdr);
1856 cl_dbg(dev, cl, "Extended Header %d vtag = %d\n",
1857 mei_hdr->extended, cb->vtag);
1859 hdr_len = sizeof(*mei_hdr) + mei_hdr->length;
1862 * Split the message only if we can write the whole host buffer
1863 * otherwise wait for next time the host buffer is empty.
1865 if (hdr_len + buf_len <= hbuf_len) {
1867 mei_hdr->msg_complete = 1;
1868 } else if (dr_slots && hbuf_len >= hdr_len + sizeof(dma_len)) {
1869 mei_hdr->dma_ring = 1;
1870 if (buf_len > dr_len)
1873 mei_hdr->msg_complete = 1;
1875 data_len = sizeof(dma_len);
1878 } else if ((u32)hbuf_slots == mei_hbuf_depth(dev)) {
1879 buf_len = hbuf_len - hdr_len;
1885 mei_hdr->length += data_len;
1887 if (mei_hdr->dma_ring)
1888 mei_dma_ring_write(dev, buf->data + cb->buf_idx, buf_len);
1889 rets = mei_write_message(dev, mei_hdr, hdr_len, data, data_len);
1895 cl->writing_state = MEI_WRITING;
1896 cb->buf_idx += buf_len;
1899 if (mei_cl_tx_flow_ctrl_creds_reduce(cl)) {
1905 if (mei_hdr->msg_complete)
1906 list_move_tail(&cb->list, &dev->write_waiting_list);
1914 list_move_tail(&cb->list, cmpl_list);
1919 * mei_cl_write - submit a write cb to mei device
1920 * assumes device_lock is locked
1923 * @cb: write callback with filled data
1925 * Return: number of bytes sent on success, <0 on failure.
1927 ssize_t mei_cl_write(struct mei_cl *cl, struct mei_cl_cb *cb)
1929 struct mei_device *dev;
1930 struct mei_msg_data *buf;
1931 struct mei_msg_hdr *mei_hdr = NULL;
1933 size_t hbuf_len, dr_len;
1943 if (WARN_ON(!cl || !cl->dev))
1952 buf_len = buf->size;
1954 cl_dbg(dev, cl, "buf_len=%zd\n", buf_len);
1956 blocking = cb->blocking;
1959 rets = pm_runtime_get(dev->dev);
1960 if (rets < 0 && rets != -EINPROGRESS) {
1961 pm_runtime_put_noidle(dev->dev);
1962 cl_err(dev, cl, "rpm: get failed %zd\n", rets);
1967 cl->writing_state = MEI_IDLE;
1970 rets = mei_cl_tx_flow_ctrl_creds(cl);
1974 mei_hdr = mei_msg_hdr_init(cb);
1975 if (IS_ERR(mei_hdr)) {
1976 rets = -PTR_ERR(mei_hdr);
1981 cl_dbg(dev, cl, "Extended Header %d vtag = %d\n",
1982 mei_hdr->extended, cb->vtag);
1984 hdr_len = sizeof(*mei_hdr) + mei_hdr->length;
1987 cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
1992 if (!mei_hbuf_acquire(dev)) {
1993 cl_dbg(dev, cl, "Cannot acquire the host buffer: not sending.\n");
1998 hbuf_slots = mei_hbuf_empty_slots(dev);
1999 if (hbuf_slots < 0) {
2004 hbuf_len = mei_slots2data(hbuf_slots) & MEI_MSG_MAX_LEN_MASK;
2005 dr_slots = mei_dma_ring_empty_slots(dev);
2006 dr_len = mei_slots2data(dr_slots);
2008 if (hdr_len + buf_len <= hbuf_len) {
2010 mei_hdr->msg_complete = 1;
2011 } else if (dr_slots && hbuf_len >= hdr_len + sizeof(dma_len)) {
2012 mei_hdr->dma_ring = 1;
2013 if (buf_len > dr_len)
2016 mei_hdr->msg_complete = 1;
2018 data_len = sizeof(dma_len);
2022 buf_len = hbuf_len - hdr_len;
2026 mei_hdr->length += data_len;
2028 if (mei_hdr->dma_ring)
2029 mei_dma_ring_write(dev, buf->data, buf_len);
2030 rets = mei_write_message(dev, mei_hdr, hdr_len, data, data_len);
2035 rets = mei_cl_tx_flow_ctrl_creds_reduce(cl);
2039 cl->writing_state = MEI_WRITING;
2040 cb->buf_idx = buf_len;
2041 /* restore return value */
2042 buf_len = buf->size;
2045 if (mei_hdr->msg_complete)
2046 mei_tx_cb_enqueue(cb, &dev->write_waiting_list);
2048 mei_tx_cb_enqueue(cb, &dev->write_list);
2051 if (blocking && cl->writing_state != MEI_WRITE_COMPLETE) {
2053 mutex_unlock(&dev->device_lock);
2054 rets = wait_event_interruptible(cl->tx_wait,
2055 cl->writing_state == MEI_WRITE_COMPLETE ||
2056 (!mei_cl_is_connected(cl)));
2057 mutex_lock(&dev->device_lock);
2058 /* wait_event_interruptible returns -ERESTARTSYS */
2060 if (signal_pending(current))
2064 if (cl->writing_state != MEI_WRITE_COMPLETE) {
2072 cl_dbg(dev, cl, "rpm: autosuspend\n");
2073 pm_runtime_mark_last_busy(dev->dev);
2074 pm_runtime_put_autosuspend(dev->dev);
2084 * mei_cl_complete - processes completed operation for a client
2086 * @cl: private data of the file object.
2087 * @cb: callback block.
2089 void mei_cl_complete(struct mei_cl *cl, struct mei_cl_cb *cb)
2091 struct mei_device *dev = cl->dev;
2093 switch (cb->fop_type) {
2095 mei_tx_cb_dequeue(cb);
2096 cl->writing_state = MEI_WRITE_COMPLETE;
2097 if (waitqueue_active(&cl->tx_wait)) {
2098 wake_up_interruptible(&cl->tx_wait);
2100 pm_runtime_mark_last_busy(dev->dev);
2101 pm_request_autosuspend(dev->dev);
2106 mei_cl_add_rd_completed(cl, cb);
2107 if (!mei_cl_is_fixed_address(cl) &&
2108 !WARN_ON(!cl->rx_flow_ctrl_creds))
2109 cl->rx_flow_ctrl_creds--;
2110 if (!mei_cl_bus_rx_event(cl))
2111 wake_up_interruptible(&cl->rx_wait);
2114 case MEI_FOP_CONNECT:
2115 case MEI_FOP_DISCONNECT:
2116 case MEI_FOP_NOTIFY_STOP:
2117 case MEI_FOP_NOTIFY_START:
2118 case MEI_FOP_DMA_MAP:
2119 case MEI_FOP_DMA_UNMAP:
2120 if (waitqueue_active(&cl->wait))
2124 case MEI_FOP_DISCONNECT_RSP:
2126 mei_cl_set_disconnected(cl);
2135 * mei_cl_all_disconnect - disconnect forcefully all connected clients
2139 void mei_cl_all_disconnect(struct mei_device *dev)
2143 list_for_each_entry(cl, &dev->file_list, link)
2144 mei_cl_set_disconnected(cl);
2147 static struct mei_cl *mei_cl_dma_map_find(struct mei_device *dev, u8 buffer_id)
2151 list_for_each_entry(cl, &dev->file_list, link)
2152 if (cl->dma.buffer_id == buffer_id)
2158 * mei_cl_irq_dma_map - send client dma map request in irq_thread context
2161 * @cb: callback block.
2162 * @cmpl_list: complete list.
2164 * Return: 0 on such and error otherwise.
2166 int mei_cl_irq_dma_map(struct mei_cl *cl, struct mei_cl_cb *cb,
2167 struct list_head *cmpl_list)
2169 struct mei_device *dev = cl->dev;
2174 msg_slots = mei_hbm2slots(sizeof(struct hbm_client_dma_map_request));
2175 slots = mei_hbuf_empty_slots(dev);
2179 if ((u32)slots < msg_slots)
2182 ret = mei_hbm_cl_dma_map_req(dev, cl);
2185 list_move_tail(&cb->list, cmpl_list);
2189 list_move_tail(&cb->list, &dev->ctrl_rd_list);
2194 * mei_cl_irq_dma_unmap - send client dma unmap request in irq_thread context
2197 * @cb: callback block.
2198 * @cmpl_list: complete list.
2200 * Return: 0 on such and error otherwise.
2202 int mei_cl_irq_dma_unmap(struct mei_cl *cl, struct mei_cl_cb *cb,
2203 struct list_head *cmpl_list)
2205 struct mei_device *dev = cl->dev;
2210 msg_slots = mei_hbm2slots(sizeof(struct hbm_client_dma_unmap_request));
2211 slots = mei_hbuf_empty_slots(dev);
2215 if ((u32)slots < msg_slots)
2218 ret = mei_hbm_cl_dma_unmap_req(dev, cl);
2221 list_move_tail(&cb->list, cmpl_list);
2225 list_move_tail(&cb->list, &dev->ctrl_rd_list);
2229 static int mei_cl_dma_alloc(struct mei_cl *cl, u8 buf_id, size_t size)
2231 cl->dma.vaddr = dmam_alloc_coherent(cl->dev->dev, size,
2232 &cl->dma.daddr, GFP_KERNEL);
2236 cl->dma.buffer_id = buf_id;
2237 cl->dma.size = size;
2242 static void mei_cl_dma_free(struct mei_cl *cl)
2244 cl->dma.buffer_id = 0;
2245 dmam_free_coherent(cl->dev->dev,
2246 cl->dma.size, cl->dma.vaddr, cl->dma.daddr);
2248 cl->dma.vaddr = NULL;
2253 * mei_cl_alloc_and_map - send client dma map request
2256 * @fp: pointer to file structure
2257 * @buffer_id: id of the mapped buffer
2258 * @size: size of the buffer
2260 * Locking: called under "dev->device_lock" lock
2269 int mei_cl_dma_alloc_and_map(struct mei_cl *cl, const struct file *fp,
2270 u8 buffer_id, size_t size)
2272 struct mei_device *dev;
2273 struct mei_cl_cb *cb;
2276 if (WARN_ON(!cl || !cl->dev))
2281 if (!dev->hbm_f_cd_supported) {
2282 cl_dbg(dev, cl, "client dma is not supported\n");
2289 if (!mei_cl_is_connected(cl))
2295 if (mei_cl_dma_map_find(dev, buffer_id)) {
2296 cl_dbg(dev, cl, "client dma with id %d is already allocated\n",
2301 rets = pm_runtime_get(dev->dev);
2302 if (rets < 0 && rets != -EINPROGRESS) {
2303 pm_runtime_put_noidle(dev->dev);
2304 cl_err(dev, cl, "rpm: get failed %d\n", rets);
2308 rets = mei_cl_dma_alloc(cl, buffer_id, size);
2310 pm_runtime_put_noidle(dev->dev);
2314 cb = mei_cl_enqueue_ctrl_wr_cb(cl, 0, MEI_FOP_DMA_MAP, fp);
2320 if (mei_hbuf_acquire(dev)) {
2321 if (mei_hbm_cl_dma_map_req(dev, cl)) {
2325 list_move_tail(&cb->list, &dev->ctrl_rd_list);
2328 mutex_unlock(&dev->device_lock);
2329 wait_event_timeout(cl->wait,
2332 !mei_cl_is_connected(cl),
2333 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
2334 mutex_lock(&dev->device_lock);
2336 if (!cl->dma_mapped && !cl->status)
2337 cl->status = -EFAULT;
2343 mei_cl_dma_free(cl);
2345 cl_dbg(dev, cl, "rpm: autosuspend\n");
2346 pm_runtime_mark_last_busy(dev->dev);
2347 pm_runtime_put_autosuspend(dev->dev);
2354 * mei_cl_unmap_and_free - send client dma unmap request
2357 * @fp: pointer to file structure
2359 * Locking: called under "dev->device_lock" lock
2361 * Return: 0 on such and error otherwise.
2363 int mei_cl_dma_unmap(struct mei_cl *cl, const struct file *fp)
2365 struct mei_device *dev;
2366 struct mei_cl_cb *cb;
2369 if (WARN_ON(!cl || !cl->dev))
2374 if (!dev->hbm_f_cd_supported) {
2375 cl_dbg(dev, cl, "client dma is not supported\n");
2379 if (!mei_cl_is_connected(cl))
2382 if (!cl->dma_mapped)
2385 rets = pm_runtime_get(dev->dev);
2386 if (rets < 0 && rets != -EINPROGRESS) {
2387 pm_runtime_put_noidle(dev->dev);
2388 cl_err(dev, cl, "rpm: get failed %d\n", rets);
2392 cb = mei_cl_enqueue_ctrl_wr_cb(cl, 0, MEI_FOP_DMA_UNMAP, fp);
2398 if (mei_hbuf_acquire(dev)) {
2399 if (mei_hbm_cl_dma_unmap_req(dev, cl)) {
2403 list_move_tail(&cb->list, &dev->ctrl_rd_list);
2406 mutex_unlock(&dev->device_lock);
2407 wait_event_timeout(cl->wait,
2410 !mei_cl_is_connected(cl),
2411 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
2412 mutex_lock(&dev->device_lock);
2414 if (cl->dma_mapped && !cl->status)
2415 cl->status = -EFAULT;
2420 mei_cl_dma_free(cl);
2422 cl_dbg(dev, cl, "rpm: autosuspend\n");
2423 pm_runtime_mark_last_busy(dev->dev);
2424 pm_runtime_put_autosuspend(dev->dev);