treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / misc / mei / client.c
blob1e3edbbacb1e29d35babd37dcdfb8867ef4c1c26
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2003-2019, Intel Corporation. All rights reserved.
4 * Intel Management Engine Interface (Intel MEI) Linux driver
5 */
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>
13 #include <linux/mei.h>
15 #include "mei_dev.h"
16 #include "hbm.h"
17 #include "client.h"
19 /**
20 * mei_me_cl_init - initialize me client
22 * @me_cl: me client
24 void mei_me_cl_init(struct mei_me_client *me_cl)
26 INIT_LIST_HEAD(&me_cl->list);
27 kref_init(&me_cl->refcnt);
30 /**
31 * mei_me_cl_get - increases me client refcount
33 * @me_cl: me client
35 * Locking: called under "dev->device_lock" lock
37 * Return: me client or NULL
39 struct mei_me_client *mei_me_cl_get(struct mei_me_client *me_cl)
41 if (me_cl && kref_get_unless_zero(&me_cl->refcnt))
42 return me_cl;
44 return NULL;
47 /**
48 * mei_me_cl_release - free me client
50 * Locking: called under "dev->device_lock" lock
52 * @ref: me_client refcount
54 static void mei_me_cl_release(struct kref *ref)
56 struct mei_me_client *me_cl =
57 container_of(ref, struct mei_me_client, refcnt);
59 kfree(me_cl);
62 /**
63 * mei_me_cl_put - decrease me client refcount and free client if necessary
65 * Locking: called under "dev->device_lock" lock
67 * @me_cl: me client
69 void mei_me_cl_put(struct mei_me_client *me_cl)
71 if (me_cl)
72 kref_put(&me_cl->refcnt, mei_me_cl_release);
75 /**
76 * __mei_me_cl_del - delete me client from the list and decrease
77 * reference counter
79 * @dev: mei device
80 * @me_cl: me client
82 * Locking: dev->me_clients_rwsem
84 static void __mei_me_cl_del(struct mei_device *dev, struct mei_me_client *me_cl)
86 if (!me_cl)
87 return;
89 list_del_init(&me_cl->list);
90 mei_me_cl_put(me_cl);
93 /**
94 * mei_me_cl_del - delete me client from the list and decrease
95 * reference counter
97 * @dev: mei device
98 * @me_cl: me client
100 void mei_me_cl_del(struct mei_device *dev, struct mei_me_client *me_cl)
102 down_write(&dev->me_clients_rwsem);
103 __mei_me_cl_del(dev, me_cl);
104 up_write(&dev->me_clients_rwsem);
108 * mei_me_cl_add - add me client to the list
110 * @dev: mei device
111 * @me_cl: me client
113 void mei_me_cl_add(struct mei_device *dev, struct mei_me_client *me_cl)
115 down_write(&dev->me_clients_rwsem);
116 list_add(&me_cl->list, &dev->me_clients);
117 up_write(&dev->me_clients_rwsem);
121 * __mei_me_cl_by_uuid - locate me client by uuid
122 * increases ref count
124 * @dev: mei device
125 * @uuid: me client uuid
127 * Return: me client or NULL if not found
129 * Locking: dev->me_clients_rwsem
131 static struct mei_me_client *__mei_me_cl_by_uuid(struct mei_device *dev,
132 const uuid_le *uuid)
134 struct mei_me_client *me_cl;
135 const uuid_le *pn;
137 WARN_ON(!rwsem_is_locked(&dev->me_clients_rwsem));
139 list_for_each_entry(me_cl, &dev->me_clients, list) {
140 pn = &me_cl->props.protocol_name;
141 if (uuid_le_cmp(*uuid, *pn) == 0)
142 return mei_me_cl_get(me_cl);
145 return NULL;
149 * mei_me_cl_by_uuid - locate me client by uuid
150 * increases ref count
152 * @dev: mei device
153 * @uuid: me client uuid
155 * Return: me client or NULL if not found
157 * Locking: dev->me_clients_rwsem
159 struct mei_me_client *mei_me_cl_by_uuid(struct mei_device *dev,
160 const uuid_le *uuid)
162 struct mei_me_client *me_cl;
164 down_read(&dev->me_clients_rwsem);
165 me_cl = __mei_me_cl_by_uuid(dev, uuid);
166 up_read(&dev->me_clients_rwsem);
168 return me_cl;
172 * mei_me_cl_by_id - locate me client by client id
173 * increases ref count
175 * @dev: the device structure
176 * @client_id: me client id
178 * Return: me client or NULL if not found
180 * Locking: dev->me_clients_rwsem
182 struct mei_me_client *mei_me_cl_by_id(struct mei_device *dev, u8 client_id)
185 struct mei_me_client *__me_cl, *me_cl = NULL;
187 down_read(&dev->me_clients_rwsem);
188 list_for_each_entry(__me_cl, &dev->me_clients, list) {
189 if (__me_cl->client_id == client_id) {
190 me_cl = mei_me_cl_get(__me_cl);
191 break;
194 up_read(&dev->me_clients_rwsem);
196 return me_cl;
200 * __mei_me_cl_by_uuid_id - locate me client by client id and uuid
201 * increases ref count
203 * @dev: the device structure
204 * @uuid: me client uuid
205 * @client_id: me client id
207 * Return: me client or null if not found
209 * Locking: dev->me_clients_rwsem
211 static struct mei_me_client *__mei_me_cl_by_uuid_id(struct mei_device *dev,
212 const uuid_le *uuid, u8 client_id)
214 struct mei_me_client *me_cl;
215 const uuid_le *pn;
217 WARN_ON(!rwsem_is_locked(&dev->me_clients_rwsem));
219 list_for_each_entry(me_cl, &dev->me_clients, list) {
220 pn = &me_cl->props.protocol_name;
221 if (uuid_le_cmp(*uuid, *pn) == 0 &&
222 me_cl->client_id == client_id)
223 return mei_me_cl_get(me_cl);
226 return NULL;
231 * mei_me_cl_by_uuid_id - locate me client by client id and uuid
232 * increases ref count
234 * @dev: the device structure
235 * @uuid: me client uuid
236 * @client_id: me client id
238 * Return: me client or null if not found
240 struct mei_me_client *mei_me_cl_by_uuid_id(struct mei_device *dev,
241 const uuid_le *uuid, u8 client_id)
243 struct mei_me_client *me_cl;
245 down_read(&dev->me_clients_rwsem);
246 me_cl = __mei_me_cl_by_uuid_id(dev, uuid, client_id);
247 up_read(&dev->me_clients_rwsem);
249 return me_cl;
253 * mei_me_cl_rm_by_uuid - remove all me clients matching uuid
255 * @dev: the device structure
256 * @uuid: me client uuid
258 * Locking: called under "dev->device_lock" lock
260 void mei_me_cl_rm_by_uuid(struct mei_device *dev, const uuid_le *uuid)
262 struct mei_me_client *me_cl;
264 dev_dbg(dev->dev, "remove %pUl\n", uuid);
266 down_write(&dev->me_clients_rwsem);
267 me_cl = __mei_me_cl_by_uuid(dev, uuid);
268 __mei_me_cl_del(dev, me_cl);
269 up_write(&dev->me_clients_rwsem);
273 * mei_me_cl_rm_by_uuid_id - remove all me clients matching client id
275 * @dev: the device structure
276 * @uuid: me client uuid
277 * @id: me client id
279 * Locking: called under "dev->device_lock" lock
281 void mei_me_cl_rm_by_uuid_id(struct mei_device *dev, const uuid_le *uuid, u8 id)
283 struct mei_me_client *me_cl;
285 dev_dbg(dev->dev, "remove %pUl %d\n", uuid, id);
287 down_write(&dev->me_clients_rwsem);
288 me_cl = __mei_me_cl_by_uuid_id(dev, uuid, id);
289 __mei_me_cl_del(dev, me_cl);
290 up_write(&dev->me_clients_rwsem);
294 * mei_me_cl_rm_all - remove all me clients
296 * @dev: the device structure
298 * Locking: called under "dev->device_lock" lock
300 void mei_me_cl_rm_all(struct mei_device *dev)
302 struct mei_me_client *me_cl, *next;
304 down_write(&dev->me_clients_rwsem);
305 list_for_each_entry_safe(me_cl, next, &dev->me_clients, list)
306 __mei_me_cl_del(dev, me_cl);
307 up_write(&dev->me_clients_rwsem);
311 * mei_io_cb_free - free mei_cb_private related memory
313 * @cb: mei callback struct
315 void mei_io_cb_free(struct mei_cl_cb *cb)
317 if (cb == NULL)
318 return;
320 list_del(&cb->list);
321 kfree(cb->buf.data);
322 kfree(cb);
326 * mei_tx_cb_queue - queue tx callback
328 * Locking: called under "dev->device_lock" lock
330 * @cb: mei callback struct
331 * @head: an instance of list to queue on
333 static inline void mei_tx_cb_enqueue(struct mei_cl_cb *cb,
334 struct list_head *head)
336 list_add_tail(&cb->list, head);
337 cb->cl->tx_cb_queued++;
341 * mei_tx_cb_dequeue - dequeue tx callback
343 * Locking: called under "dev->device_lock" lock
345 * @cb: mei callback struct to dequeue and free
347 static inline void mei_tx_cb_dequeue(struct mei_cl_cb *cb)
349 if (!WARN_ON(cb->cl->tx_cb_queued == 0))
350 cb->cl->tx_cb_queued--;
352 mei_io_cb_free(cb);
356 * mei_io_cb_init - allocate and initialize io callback
358 * @cl: mei client
359 * @type: operation type
360 * @fp: pointer to file structure
362 * Return: mei_cl_cb pointer or NULL;
364 static struct mei_cl_cb *mei_io_cb_init(struct mei_cl *cl,
365 enum mei_cb_file_ops type,
366 const struct file *fp)
368 struct mei_cl_cb *cb;
370 cb = kzalloc(sizeof(struct mei_cl_cb), GFP_KERNEL);
371 if (!cb)
372 return NULL;
374 INIT_LIST_HEAD(&cb->list);
375 cb->fp = fp;
376 cb->cl = cl;
377 cb->buf_idx = 0;
378 cb->fop_type = type;
379 return cb;
383 * mei_io_list_flush_cl - removes cbs belonging to the cl.
385 * @head: an instance of our list structure
386 * @cl: host client
388 static void mei_io_list_flush_cl(struct list_head *head,
389 const struct mei_cl *cl)
391 struct mei_cl_cb *cb, *next;
393 list_for_each_entry_safe(cb, next, head, list) {
394 if (cl == cb->cl) {
395 list_del_init(&cb->list);
396 if (cb->fop_type == MEI_FOP_READ)
397 mei_io_cb_free(cb);
403 * mei_io_tx_list_free_cl - removes cb belonging to the cl and free them
405 * @head: An instance of our list structure
406 * @cl: host client
408 static void mei_io_tx_list_free_cl(struct list_head *head,
409 const struct mei_cl *cl)
411 struct mei_cl_cb *cb, *next;
413 list_for_each_entry_safe(cb, next, head, list) {
414 if (cl == cb->cl)
415 mei_tx_cb_dequeue(cb);
420 * mei_io_list_free_fp - free cb from a list that matches file pointer
422 * @head: io list
423 * @fp: file pointer (matching cb file object), may be NULL
425 static void mei_io_list_free_fp(struct list_head *head, const struct file *fp)
427 struct mei_cl_cb *cb, *next;
429 list_for_each_entry_safe(cb, next, head, list)
430 if (!fp || fp == cb->fp)
431 mei_io_cb_free(cb);
435 * mei_cl_alloc_cb - a convenient wrapper for allocating read cb
437 * @cl: host client
438 * @length: size of the buffer
439 * @fop_type: operation type
440 * @fp: associated file pointer (might be NULL)
442 * Return: cb on success and NULL on failure
444 struct mei_cl_cb *mei_cl_alloc_cb(struct mei_cl *cl, size_t length,
445 enum mei_cb_file_ops fop_type,
446 const struct file *fp)
448 struct mei_cl_cb *cb;
450 cb = mei_io_cb_init(cl, fop_type, fp);
451 if (!cb)
452 return NULL;
454 if (length == 0)
455 return cb;
457 cb->buf.data = kmalloc(roundup(length, MEI_SLOT_SIZE), GFP_KERNEL);
458 if (!cb->buf.data) {
459 mei_io_cb_free(cb);
460 return NULL;
462 cb->buf.size = length;
464 return cb;
468 * mei_cl_enqueue_ctrl_wr_cb - a convenient wrapper for allocating
469 * and enqueuing of the control commands cb
471 * @cl: host client
472 * @length: size of the buffer
473 * @fop_type: operation type
474 * @fp: associated file pointer (might be NULL)
476 * Return: cb on success and NULL on failure
477 * Locking: called under "dev->device_lock" lock
479 struct mei_cl_cb *mei_cl_enqueue_ctrl_wr_cb(struct mei_cl *cl, size_t length,
480 enum mei_cb_file_ops fop_type,
481 const struct file *fp)
483 struct mei_cl_cb *cb;
485 /* for RX always allocate at least client's mtu */
486 if (length)
487 length = max_t(size_t, length, mei_cl_mtu(cl));
489 cb = mei_cl_alloc_cb(cl, length, fop_type, fp);
490 if (!cb)
491 return NULL;
493 list_add_tail(&cb->list, &cl->dev->ctrl_wr_list);
494 return cb;
498 * mei_cl_read_cb - find this cl's callback in the read list
499 * for a specific file
501 * @cl: host client
502 * @fp: file pointer (matching cb file object), may be NULL
504 * Return: cb on success, NULL if cb is not found
506 struct mei_cl_cb *mei_cl_read_cb(const struct mei_cl *cl, const struct file *fp)
508 struct mei_cl_cb *cb;
510 list_for_each_entry(cb, &cl->rd_completed, list)
511 if (!fp || fp == cb->fp)
512 return cb;
514 return NULL;
518 * mei_cl_flush_queues - flushes queue lists belonging to cl.
520 * @cl: host client
521 * @fp: file pointer (matching cb file object), may be NULL
523 * Return: 0 on success, -EINVAL if cl or cl->dev is NULL.
525 int mei_cl_flush_queues(struct mei_cl *cl, const struct file *fp)
527 struct mei_device *dev;
529 if (WARN_ON(!cl || !cl->dev))
530 return -EINVAL;
532 dev = cl->dev;
534 cl_dbg(dev, cl, "remove list entry belonging to cl\n");
535 mei_io_tx_list_free_cl(&cl->dev->write_list, cl);
536 mei_io_tx_list_free_cl(&cl->dev->write_waiting_list, cl);
537 mei_io_list_flush_cl(&cl->dev->ctrl_wr_list, cl);
538 mei_io_list_flush_cl(&cl->dev->ctrl_rd_list, cl);
539 mei_io_list_free_fp(&cl->rd_pending, fp);
540 mei_io_list_free_fp(&cl->rd_completed, fp);
542 return 0;
546 * mei_cl_init - initializes cl.
548 * @cl: host client to be initialized
549 * @dev: mei device
551 static void mei_cl_init(struct mei_cl *cl, struct mei_device *dev)
553 memset(cl, 0, sizeof(struct mei_cl));
554 init_waitqueue_head(&cl->wait);
555 init_waitqueue_head(&cl->rx_wait);
556 init_waitqueue_head(&cl->tx_wait);
557 init_waitqueue_head(&cl->ev_wait);
558 INIT_LIST_HEAD(&cl->rd_completed);
559 INIT_LIST_HEAD(&cl->rd_pending);
560 INIT_LIST_HEAD(&cl->link);
561 cl->writing_state = MEI_IDLE;
562 cl->state = MEI_FILE_UNINITIALIZED;
563 cl->dev = dev;
567 * mei_cl_allocate - allocates cl structure and sets it up.
569 * @dev: mei device
570 * Return: The allocated file or NULL on failure
572 struct mei_cl *mei_cl_allocate(struct mei_device *dev)
574 struct mei_cl *cl;
576 cl = kmalloc(sizeof(struct mei_cl), GFP_KERNEL);
577 if (!cl)
578 return NULL;
580 mei_cl_init(cl, dev);
582 return cl;
586 * mei_cl_link - allocate host id in the host map
588 * @cl: host client
590 * Return: 0 on success
591 * -EINVAL on incorrect values
592 * -EMFILE if open count exceeded.
594 int mei_cl_link(struct mei_cl *cl)
596 struct mei_device *dev;
597 int id;
599 if (WARN_ON(!cl || !cl->dev))
600 return -EINVAL;
602 dev = cl->dev;
604 id = find_first_zero_bit(dev->host_clients_map, MEI_CLIENTS_MAX);
605 if (id >= MEI_CLIENTS_MAX) {
606 dev_err(dev->dev, "id exceeded %d", MEI_CLIENTS_MAX);
607 return -EMFILE;
610 if (dev->open_handle_count >= MEI_MAX_OPEN_HANDLE_COUNT) {
611 dev_err(dev->dev, "open_handle_count exceeded %d",
612 MEI_MAX_OPEN_HANDLE_COUNT);
613 return -EMFILE;
616 dev->open_handle_count++;
618 cl->host_client_id = id;
619 list_add_tail(&cl->link, &dev->file_list);
621 set_bit(id, dev->host_clients_map);
623 cl->state = MEI_FILE_INITIALIZING;
625 cl_dbg(dev, cl, "link cl\n");
626 return 0;
630 * mei_cl_unlink - remove host client from the list
632 * @cl: host client
634 * Return: always 0
636 int mei_cl_unlink(struct mei_cl *cl)
638 struct mei_device *dev;
640 /* don't shout on error exit path */
641 if (!cl)
642 return 0;
644 if (WARN_ON(!cl->dev))
645 return 0;
647 dev = cl->dev;
649 cl_dbg(dev, cl, "unlink client");
651 if (dev->open_handle_count > 0)
652 dev->open_handle_count--;
654 /* never clear the 0 bit */
655 if (cl->host_client_id)
656 clear_bit(cl->host_client_id, dev->host_clients_map);
658 list_del_init(&cl->link);
660 cl->state = MEI_FILE_UNINITIALIZED;
661 cl->writing_state = MEI_IDLE;
663 WARN_ON(!list_empty(&cl->rd_completed) ||
664 !list_empty(&cl->rd_pending) ||
665 !list_empty(&cl->link));
667 return 0;
670 void mei_host_client_init(struct mei_device *dev)
672 mei_set_devstate(dev, MEI_DEV_ENABLED);
673 dev->reset_count = 0;
675 schedule_work(&dev->bus_rescan_work);
677 pm_runtime_mark_last_busy(dev->dev);
678 dev_dbg(dev->dev, "rpm: autosuspend\n");
679 pm_request_autosuspend(dev->dev);
683 * mei_hbuf_acquire - try to acquire host buffer
685 * @dev: the device structure
686 * Return: true if host buffer was acquired
688 bool mei_hbuf_acquire(struct mei_device *dev)
690 if (mei_pg_state(dev) == MEI_PG_ON ||
691 mei_pg_in_transition(dev)) {
692 dev_dbg(dev->dev, "device is in pg\n");
693 return false;
696 if (!dev->hbuf_is_ready) {
697 dev_dbg(dev->dev, "hbuf is not ready\n");
698 return false;
701 dev->hbuf_is_ready = false;
703 return true;
707 * mei_cl_wake_all - wake up readers, writers and event waiters so
708 * they can be interrupted
710 * @cl: host client
712 static void mei_cl_wake_all(struct mei_cl *cl)
714 struct mei_device *dev = cl->dev;
716 /* synchronized under device mutex */
717 if (waitqueue_active(&cl->rx_wait)) {
718 cl_dbg(dev, cl, "Waking up reading client!\n");
719 wake_up_interruptible(&cl->rx_wait);
721 /* synchronized under device mutex */
722 if (waitqueue_active(&cl->tx_wait)) {
723 cl_dbg(dev, cl, "Waking up writing client!\n");
724 wake_up_interruptible(&cl->tx_wait);
726 /* synchronized under device mutex */
727 if (waitqueue_active(&cl->ev_wait)) {
728 cl_dbg(dev, cl, "Waking up waiting for event clients!\n");
729 wake_up_interruptible(&cl->ev_wait);
731 /* synchronized under device mutex */
732 if (waitqueue_active(&cl->wait)) {
733 cl_dbg(dev, cl, "Waking up ctrl write clients!\n");
734 wake_up(&cl->wait);
739 * mei_cl_set_disconnected - set disconnected state and clear
740 * associated states and resources
742 * @cl: host client
744 static void mei_cl_set_disconnected(struct mei_cl *cl)
746 struct mei_device *dev = cl->dev;
748 if (cl->state == MEI_FILE_DISCONNECTED ||
749 cl->state <= MEI_FILE_INITIALIZING)
750 return;
752 cl->state = MEI_FILE_DISCONNECTED;
753 mei_io_tx_list_free_cl(&dev->write_list, cl);
754 mei_io_tx_list_free_cl(&dev->write_waiting_list, cl);
755 mei_io_list_flush_cl(&dev->ctrl_rd_list, cl);
756 mei_io_list_flush_cl(&dev->ctrl_wr_list, cl);
757 mei_cl_wake_all(cl);
758 cl->rx_flow_ctrl_creds = 0;
759 cl->tx_flow_ctrl_creds = 0;
760 cl->timer_count = 0;
762 if (!cl->me_cl)
763 return;
765 if (!WARN_ON(cl->me_cl->connect_count == 0))
766 cl->me_cl->connect_count--;
768 if (cl->me_cl->connect_count == 0)
769 cl->me_cl->tx_flow_ctrl_creds = 0;
771 mei_me_cl_put(cl->me_cl);
772 cl->me_cl = NULL;
775 static int mei_cl_set_connecting(struct mei_cl *cl, struct mei_me_client *me_cl)
777 if (!mei_me_cl_get(me_cl))
778 return -ENOENT;
780 /* only one connection is allowed for fixed address clients */
781 if (me_cl->props.fixed_address) {
782 if (me_cl->connect_count) {
783 mei_me_cl_put(me_cl);
784 return -EBUSY;
788 cl->me_cl = me_cl;
789 cl->state = MEI_FILE_CONNECTING;
790 cl->me_cl->connect_count++;
792 return 0;
796 * mei_cl_send_disconnect - send disconnect request
798 * @cl: host client
799 * @cb: callback block
801 * Return: 0, OK; otherwise, error.
803 static int mei_cl_send_disconnect(struct mei_cl *cl, struct mei_cl_cb *cb)
805 struct mei_device *dev;
806 int ret;
808 dev = cl->dev;
810 ret = mei_hbm_cl_disconnect_req(dev, cl);
811 cl->status = ret;
812 if (ret) {
813 cl->state = MEI_FILE_DISCONNECT_REPLY;
814 return ret;
817 list_move_tail(&cb->list, &dev->ctrl_rd_list);
818 cl->timer_count = MEI_CONNECT_TIMEOUT;
819 mei_schedule_stall_timer(dev);
821 return 0;
825 * mei_cl_irq_disconnect - processes close related operation from
826 * interrupt thread context - send disconnect request
828 * @cl: client
829 * @cb: callback block.
830 * @cmpl_list: complete list.
832 * Return: 0, OK; otherwise, error.
834 int mei_cl_irq_disconnect(struct mei_cl *cl, struct mei_cl_cb *cb,
835 struct list_head *cmpl_list)
837 struct mei_device *dev = cl->dev;
838 u32 msg_slots;
839 int slots;
840 int ret;
842 msg_slots = mei_hbm2slots(sizeof(struct hbm_client_connect_request));
843 slots = mei_hbuf_empty_slots(dev);
844 if (slots < 0)
845 return -EOVERFLOW;
847 if ((u32)slots < msg_slots)
848 return -EMSGSIZE;
850 ret = mei_cl_send_disconnect(cl, cb);
851 if (ret)
852 list_move_tail(&cb->list, cmpl_list);
854 return ret;
858 * __mei_cl_disconnect - disconnect host client from the me one
859 * internal function runtime pm has to be already acquired
861 * @cl: host client
863 * Return: 0 on success, <0 on failure.
865 static int __mei_cl_disconnect(struct mei_cl *cl)
867 struct mei_device *dev;
868 struct mei_cl_cb *cb;
869 int rets;
871 dev = cl->dev;
873 cl->state = MEI_FILE_DISCONNECTING;
875 cb = mei_cl_enqueue_ctrl_wr_cb(cl, 0, MEI_FOP_DISCONNECT, NULL);
876 if (!cb) {
877 rets = -ENOMEM;
878 goto out;
881 if (mei_hbuf_acquire(dev)) {
882 rets = mei_cl_send_disconnect(cl, cb);
883 if (rets) {
884 cl_err(dev, cl, "failed to disconnect.\n");
885 goto out;
889 mutex_unlock(&dev->device_lock);
890 wait_event_timeout(cl->wait,
891 cl->state == MEI_FILE_DISCONNECT_REPLY ||
892 cl->state == MEI_FILE_DISCONNECTED,
893 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
894 mutex_lock(&dev->device_lock);
896 rets = cl->status;
897 if (cl->state != MEI_FILE_DISCONNECT_REPLY &&
898 cl->state != MEI_FILE_DISCONNECTED) {
899 cl_dbg(dev, cl, "timeout on disconnect from FW client.\n");
900 rets = -ETIME;
903 out:
904 /* we disconnect also on error */
905 mei_cl_set_disconnected(cl);
906 if (!rets)
907 cl_dbg(dev, cl, "successfully disconnected from FW client.\n");
909 mei_io_cb_free(cb);
910 return rets;
914 * mei_cl_disconnect - disconnect host client from the me one
916 * @cl: host client
918 * Locking: called under "dev->device_lock" lock
920 * Return: 0 on success, <0 on failure.
922 int mei_cl_disconnect(struct mei_cl *cl)
924 struct mei_device *dev;
925 int rets;
927 if (WARN_ON(!cl || !cl->dev))
928 return -ENODEV;
930 dev = cl->dev;
932 cl_dbg(dev, cl, "disconnecting");
934 if (!mei_cl_is_connected(cl))
935 return 0;
937 if (mei_cl_is_fixed_address(cl)) {
938 mei_cl_set_disconnected(cl);
939 return 0;
942 if (dev->dev_state == MEI_DEV_POWER_DOWN) {
943 cl_dbg(dev, cl, "Device is powering down, don't bother with disconnection\n");
944 mei_cl_set_disconnected(cl);
945 return 0;
948 rets = pm_runtime_get(dev->dev);
949 if (rets < 0 && rets != -EINPROGRESS) {
950 pm_runtime_put_noidle(dev->dev);
951 cl_err(dev, cl, "rpm: get failed %d\n", rets);
952 return rets;
955 rets = __mei_cl_disconnect(cl);
957 cl_dbg(dev, cl, "rpm: autosuspend\n");
958 pm_runtime_mark_last_busy(dev->dev);
959 pm_runtime_put_autosuspend(dev->dev);
961 return rets;
966 * mei_cl_is_other_connecting - checks if other
967 * client with the same me client id is connecting
969 * @cl: private data of the file object
971 * Return: true if other client is connected, false - otherwise.
973 static bool mei_cl_is_other_connecting(struct mei_cl *cl)
975 struct mei_device *dev;
976 struct mei_cl_cb *cb;
978 dev = cl->dev;
980 list_for_each_entry(cb, &dev->ctrl_rd_list, list) {
981 if (cb->fop_type == MEI_FOP_CONNECT &&
982 mei_cl_me_id(cl) == mei_cl_me_id(cb->cl))
983 return true;
986 return false;
990 * mei_cl_send_connect - send connect request
992 * @cl: host client
993 * @cb: callback block
995 * Return: 0, OK; otherwise, error.
997 static int mei_cl_send_connect(struct mei_cl *cl, struct mei_cl_cb *cb)
999 struct mei_device *dev;
1000 int ret;
1002 dev = cl->dev;
1004 ret = mei_hbm_cl_connect_req(dev, cl);
1005 cl->status = ret;
1006 if (ret) {
1007 cl->state = MEI_FILE_DISCONNECT_REPLY;
1008 return ret;
1011 list_move_tail(&cb->list, &dev->ctrl_rd_list);
1012 cl->timer_count = MEI_CONNECT_TIMEOUT;
1013 mei_schedule_stall_timer(dev);
1014 return 0;
1018 * mei_cl_irq_connect - send connect request in irq_thread context
1020 * @cl: host client
1021 * @cb: callback block
1022 * @cmpl_list: complete list
1024 * Return: 0, OK; otherwise, error.
1026 int mei_cl_irq_connect(struct mei_cl *cl, struct mei_cl_cb *cb,
1027 struct list_head *cmpl_list)
1029 struct mei_device *dev = cl->dev;
1030 u32 msg_slots;
1031 int slots;
1032 int rets;
1034 if (mei_cl_is_other_connecting(cl))
1035 return 0;
1037 msg_slots = mei_hbm2slots(sizeof(struct hbm_client_connect_request));
1038 slots = mei_hbuf_empty_slots(dev);
1039 if (slots < 0)
1040 return -EOVERFLOW;
1042 if ((u32)slots < msg_slots)
1043 return -EMSGSIZE;
1045 rets = mei_cl_send_connect(cl, cb);
1046 if (rets)
1047 list_move_tail(&cb->list, cmpl_list);
1049 return rets;
1053 * mei_cl_connect - connect host client to the me one
1055 * @cl: host client
1056 * @me_cl: me client
1057 * @fp: pointer to file structure
1059 * Locking: called under "dev->device_lock" lock
1061 * Return: 0 on success, <0 on failure.
1063 int mei_cl_connect(struct mei_cl *cl, struct mei_me_client *me_cl,
1064 const struct file *fp)
1066 struct mei_device *dev;
1067 struct mei_cl_cb *cb;
1068 int rets;
1070 if (WARN_ON(!cl || !cl->dev || !me_cl))
1071 return -ENODEV;
1073 dev = cl->dev;
1075 rets = mei_cl_set_connecting(cl, me_cl);
1076 if (rets)
1077 goto nortpm;
1079 if (mei_cl_is_fixed_address(cl)) {
1080 cl->state = MEI_FILE_CONNECTED;
1081 rets = 0;
1082 goto nortpm;
1085 rets = pm_runtime_get(dev->dev);
1086 if (rets < 0 && rets != -EINPROGRESS) {
1087 pm_runtime_put_noidle(dev->dev);
1088 cl_err(dev, cl, "rpm: get failed %d\n", rets);
1089 goto nortpm;
1092 cb = mei_cl_enqueue_ctrl_wr_cb(cl, 0, MEI_FOP_CONNECT, fp);
1093 if (!cb) {
1094 rets = -ENOMEM;
1095 goto out;
1098 /* run hbuf acquire last so we don't have to undo */
1099 if (!mei_cl_is_other_connecting(cl) && mei_hbuf_acquire(dev)) {
1100 rets = mei_cl_send_connect(cl, cb);
1101 if (rets)
1102 goto out;
1105 mutex_unlock(&dev->device_lock);
1106 wait_event_timeout(cl->wait,
1107 (cl->state == MEI_FILE_CONNECTED ||
1108 cl->state == MEI_FILE_DISCONNECTED ||
1109 cl->state == MEI_FILE_DISCONNECT_REQUIRED ||
1110 cl->state == MEI_FILE_DISCONNECT_REPLY),
1111 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
1112 mutex_lock(&dev->device_lock);
1114 if (!mei_cl_is_connected(cl)) {
1115 if (cl->state == MEI_FILE_DISCONNECT_REQUIRED) {
1116 mei_io_list_flush_cl(&dev->ctrl_rd_list, cl);
1117 mei_io_list_flush_cl(&dev->ctrl_wr_list, cl);
1118 /* ignore disconnect return valuue;
1119 * in case of failure reset will be invoked
1121 __mei_cl_disconnect(cl);
1122 rets = -EFAULT;
1123 goto out;
1126 /* timeout or something went really wrong */
1127 if (!cl->status)
1128 cl->status = -EFAULT;
1131 rets = cl->status;
1132 out:
1133 cl_dbg(dev, cl, "rpm: autosuspend\n");
1134 pm_runtime_mark_last_busy(dev->dev);
1135 pm_runtime_put_autosuspend(dev->dev);
1137 mei_io_cb_free(cb);
1139 nortpm:
1140 if (!mei_cl_is_connected(cl))
1141 mei_cl_set_disconnected(cl);
1143 return rets;
1147 * mei_cl_alloc_linked - allocate and link host client
1149 * @dev: the device structure
1151 * Return: cl on success ERR_PTR on failure
1153 struct mei_cl *mei_cl_alloc_linked(struct mei_device *dev)
1155 struct mei_cl *cl;
1156 int ret;
1158 cl = mei_cl_allocate(dev);
1159 if (!cl) {
1160 ret = -ENOMEM;
1161 goto err;
1164 ret = mei_cl_link(cl);
1165 if (ret)
1166 goto err;
1168 return cl;
1169 err:
1170 kfree(cl);
1171 return ERR_PTR(ret);
1175 * mei_cl_tx_flow_ctrl_creds - checks flow_control credits for cl.
1177 * @cl: host client
1179 * Return: 1 if tx_flow_ctrl_creds >0, 0 - otherwise.
1181 static int mei_cl_tx_flow_ctrl_creds(struct mei_cl *cl)
1183 if (WARN_ON(!cl || !cl->me_cl))
1184 return -EINVAL;
1186 if (cl->tx_flow_ctrl_creds > 0)
1187 return 1;
1189 if (mei_cl_is_fixed_address(cl))
1190 return 1;
1192 if (mei_cl_is_single_recv_buf(cl)) {
1193 if (cl->me_cl->tx_flow_ctrl_creds > 0)
1194 return 1;
1196 return 0;
1200 * mei_cl_tx_flow_ctrl_creds_reduce - reduces transmit flow control credits
1201 * for a client
1203 * @cl: host client
1205 * Return:
1206 * 0 on success
1207 * -EINVAL when ctrl credits are <= 0
1209 static int mei_cl_tx_flow_ctrl_creds_reduce(struct mei_cl *cl)
1211 if (WARN_ON(!cl || !cl->me_cl))
1212 return -EINVAL;
1214 if (mei_cl_is_fixed_address(cl))
1215 return 0;
1217 if (mei_cl_is_single_recv_buf(cl)) {
1218 if (WARN_ON(cl->me_cl->tx_flow_ctrl_creds <= 0))
1219 return -EINVAL;
1220 cl->me_cl->tx_flow_ctrl_creds--;
1221 } else {
1222 if (WARN_ON(cl->tx_flow_ctrl_creds <= 0))
1223 return -EINVAL;
1224 cl->tx_flow_ctrl_creds--;
1226 return 0;
1230 * mei_cl_notify_fop2req - convert fop to proper request
1232 * @fop: client notification start response command
1234 * Return: MEI_HBM_NOTIFICATION_START/STOP
1236 u8 mei_cl_notify_fop2req(enum mei_cb_file_ops fop)
1238 if (fop == MEI_FOP_NOTIFY_START)
1239 return MEI_HBM_NOTIFICATION_START;
1240 else
1241 return MEI_HBM_NOTIFICATION_STOP;
1245 * mei_cl_notify_req2fop - convert notification request top file operation type
1247 * @req: hbm notification request type
1249 * Return: MEI_FOP_NOTIFY_START/STOP
1251 enum mei_cb_file_ops mei_cl_notify_req2fop(u8 req)
1253 if (req == MEI_HBM_NOTIFICATION_START)
1254 return MEI_FOP_NOTIFY_START;
1255 else
1256 return MEI_FOP_NOTIFY_STOP;
1260 * mei_cl_irq_notify - send notification request in irq_thread context
1262 * @cl: client
1263 * @cb: callback block.
1264 * @cmpl_list: complete list.
1266 * Return: 0 on such and error otherwise.
1268 int mei_cl_irq_notify(struct mei_cl *cl, struct mei_cl_cb *cb,
1269 struct list_head *cmpl_list)
1271 struct mei_device *dev = cl->dev;
1272 u32 msg_slots;
1273 int slots;
1274 int ret;
1275 bool request;
1277 msg_slots = mei_hbm2slots(sizeof(struct hbm_client_connect_request));
1278 slots = mei_hbuf_empty_slots(dev);
1279 if (slots < 0)
1280 return -EOVERFLOW;
1282 if ((u32)slots < msg_slots)
1283 return -EMSGSIZE;
1285 request = mei_cl_notify_fop2req(cb->fop_type);
1286 ret = mei_hbm_cl_notify_req(dev, cl, request);
1287 if (ret) {
1288 cl->status = ret;
1289 list_move_tail(&cb->list, cmpl_list);
1290 return ret;
1293 list_move_tail(&cb->list, &dev->ctrl_rd_list);
1294 return 0;
1298 * mei_cl_notify_request - send notification stop/start request
1300 * @cl: host client
1301 * @fp: associate request with file
1302 * @request: 1 for start or 0 for stop
1304 * Locking: called under "dev->device_lock" lock
1306 * Return: 0 on such and error otherwise.
1308 int mei_cl_notify_request(struct mei_cl *cl,
1309 const struct file *fp, u8 request)
1311 struct mei_device *dev;
1312 struct mei_cl_cb *cb;
1313 enum mei_cb_file_ops fop_type;
1314 int rets;
1316 if (WARN_ON(!cl || !cl->dev))
1317 return -ENODEV;
1319 dev = cl->dev;
1321 if (!dev->hbm_f_ev_supported) {
1322 cl_dbg(dev, cl, "notifications not supported\n");
1323 return -EOPNOTSUPP;
1326 if (!mei_cl_is_connected(cl))
1327 return -ENODEV;
1329 rets = pm_runtime_get(dev->dev);
1330 if (rets < 0 && rets != -EINPROGRESS) {
1331 pm_runtime_put_noidle(dev->dev);
1332 cl_err(dev, cl, "rpm: get failed %d\n", rets);
1333 return rets;
1336 fop_type = mei_cl_notify_req2fop(request);
1337 cb = mei_cl_enqueue_ctrl_wr_cb(cl, 0, fop_type, fp);
1338 if (!cb) {
1339 rets = -ENOMEM;
1340 goto out;
1343 if (mei_hbuf_acquire(dev)) {
1344 if (mei_hbm_cl_notify_req(dev, cl, request)) {
1345 rets = -ENODEV;
1346 goto out;
1348 list_move_tail(&cb->list, &dev->ctrl_rd_list);
1351 mutex_unlock(&dev->device_lock);
1352 wait_event_timeout(cl->wait,
1353 cl->notify_en == request ||
1354 cl->status ||
1355 !mei_cl_is_connected(cl),
1356 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
1357 mutex_lock(&dev->device_lock);
1359 if (cl->notify_en != request && !cl->status)
1360 cl->status = -EFAULT;
1362 rets = cl->status;
1364 out:
1365 cl_dbg(dev, cl, "rpm: autosuspend\n");
1366 pm_runtime_mark_last_busy(dev->dev);
1367 pm_runtime_put_autosuspend(dev->dev);
1369 mei_io_cb_free(cb);
1370 return rets;
1374 * mei_cl_notify - raise notification
1376 * @cl: host client
1378 * Locking: called under "dev->device_lock" lock
1380 void mei_cl_notify(struct mei_cl *cl)
1382 struct mei_device *dev;
1384 if (!cl || !cl->dev)
1385 return;
1387 dev = cl->dev;
1389 if (!cl->notify_en)
1390 return;
1392 cl_dbg(dev, cl, "notify event");
1393 cl->notify_ev = true;
1394 if (!mei_cl_bus_notify_event(cl))
1395 wake_up_interruptible(&cl->ev_wait);
1397 if (cl->ev_async)
1398 kill_fasync(&cl->ev_async, SIGIO, POLL_PRI);
1403 * mei_cl_notify_get - get or wait for notification event
1405 * @cl: host client
1406 * @block: this request is blocking
1407 * @notify_ev: true if notification event was received
1409 * Locking: called under "dev->device_lock" lock
1411 * Return: 0 on such and error otherwise.
1413 int mei_cl_notify_get(struct mei_cl *cl, bool block, bool *notify_ev)
1415 struct mei_device *dev;
1416 int rets;
1418 *notify_ev = false;
1420 if (WARN_ON(!cl || !cl->dev))
1421 return -ENODEV;
1423 dev = cl->dev;
1425 if (!dev->hbm_f_ev_supported) {
1426 cl_dbg(dev, cl, "notifications not supported\n");
1427 return -EOPNOTSUPP;
1430 if (!mei_cl_is_connected(cl))
1431 return -ENODEV;
1433 if (cl->notify_ev)
1434 goto out;
1436 if (!block)
1437 return -EAGAIN;
1439 mutex_unlock(&dev->device_lock);
1440 rets = wait_event_interruptible(cl->ev_wait, cl->notify_ev);
1441 mutex_lock(&dev->device_lock);
1443 if (rets < 0)
1444 return rets;
1446 out:
1447 *notify_ev = cl->notify_ev;
1448 cl->notify_ev = false;
1449 return 0;
1453 * mei_cl_read_start - the start read client message function.
1455 * @cl: host client
1456 * @length: number of bytes to read
1457 * @fp: pointer to file structure
1459 * Return: 0 on success, <0 on failure.
1461 int mei_cl_read_start(struct mei_cl *cl, size_t length, const struct file *fp)
1463 struct mei_device *dev;
1464 struct mei_cl_cb *cb;
1465 int rets;
1467 if (WARN_ON(!cl || !cl->dev))
1468 return -ENODEV;
1470 dev = cl->dev;
1472 if (!mei_cl_is_connected(cl))
1473 return -ENODEV;
1475 if (!mei_me_cl_is_active(cl->me_cl)) {
1476 cl_err(dev, cl, "no such me client\n");
1477 return -ENOTTY;
1480 if (mei_cl_is_fixed_address(cl))
1481 return 0;
1483 /* HW currently supports only one pending read */
1484 if (cl->rx_flow_ctrl_creds)
1485 return -EBUSY;
1487 cb = mei_cl_enqueue_ctrl_wr_cb(cl, length, MEI_FOP_READ, fp);
1488 if (!cb)
1489 return -ENOMEM;
1491 rets = pm_runtime_get(dev->dev);
1492 if (rets < 0 && rets != -EINPROGRESS) {
1493 pm_runtime_put_noidle(dev->dev);
1494 cl_err(dev, cl, "rpm: get failed %d\n", rets);
1495 goto nortpm;
1498 rets = 0;
1499 if (mei_hbuf_acquire(dev)) {
1500 rets = mei_hbm_cl_flow_control_req(dev, cl);
1501 if (rets < 0)
1502 goto out;
1504 list_move_tail(&cb->list, &cl->rd_pending);
1506 cl->rx_flow_ctrl_creds++;
1508 out:
1509 cl_dbg(dev, cl, "rpm: autosuspend\n");
1510 pm_runtime_mark_last_busy(dev->dev);
1511 pm_runtime_put_autosuspend(dev->dev);
1512 nortpm:
1513 if (rets)
1514 mei_io_cb_free(cb);
1516 return rets;
1520 * mei_msg_hdr_init - initialize mei message header
1522 * @mei_hdr: mei message header
1523 * @cb: message callback structure
1525 static void mei_msg_hdr_init(struct mei_msg_hdr *mei_hdr, struct mei_cl_cb *cb)
1527 mei_hdr->host_addr = mei_cl_host_addr(cb->cl);
1528 mei_hdr->me_addr = mei_cl_me_id(cb->cl);
1529 mei_hdr->length = 0;
1530 mei_hdr->reserved = 0;
1531 mei_hdr->msg_complete = 0;
1532 mei_hdr->dma_ring = 0;
1533 mei_hdr->internal = cb->internal;
1537 * mei_cl_irq_write - write a message to device
1538 * from the interrupt thread context
1540 * @cl: client
1541 * @cb: callback block.
1542 * @cmpl_list: complete list.
1544 * Return: 0, OK; otherwise error.
1546 int mei_cl_irq_write(struct mei_cl *cl, struct mei_cl_cb *cb,
1547 struct list_head *cmpl_list)
1549 struct mei_device *dev;
1550 struct mei_msg_data *buf;
1551 struct mei_msg_hdr mei_hdr;
1552 size_t hdr_len = sizeof(mei_hdr);
1553 size_t len;
1554 size_t hbuf_len, dr_len;
1555 int hbuf_slots;
1556 u32 dr_slots;
1557 u32 dma_len;
1558 int rets;
1559 bool first_chunk;
1560 const void *data;
1562 if (WARN_ON(!cl || !cl->dev))
1563 return -ENODEV;
1565 dev = cl->dev;
1567 buf = &cb->buf;
1569 first_chunk = cb->buf_idx == 0;
1571 rets = first_chunk ? mei_cl_tx_flow_ctrl_creds(cl) : 1;
1572 if (rets < 0)
1573 goto err;
1575 if (rets == 0) {
1576 cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
1577 return 0;
1580 len = buf->size - cb->buf_idx;
1581 data = buf->data + cb->buf_idx;
1582 hbuf_slots = mei_hbuf_empty_slots(dev);
1583 if (hbuf_slots < 0) {
1584 rets = -EOVERFLOW;
1585 goto err;
1588 hbuf_len = mei_slots2data(hbuf_slots);
1589 dr_slots = mei_dma_ring_empty_slots(dev);
1590 dr_len = mei_slots2data(dr_slots);
1592 mei_msg_hdr_init(&mei_hdr, cb);
1595 * Split the message only if we can write the whole host buffer
1596 * otherwise wait for next time the host buffer is empty.
1598 if (len + hdr_len <= hbuf_len) {
1599 mei_hdr.length = len;
1600 mei_hdr.msg_complete = 1;
1601 } else if (dr_slots && hbuf_len >= hdr_len + sizeof(dma_len)) {
1602 mei_hdr.dma_ring = 1;
1603 if (len > dr_len)
1604 len = dr_len;
1605 else
1606 mei_hdr.msg_complete = 1;
1608 mei_hdr.length = sizeof(dma_len);
1609 dma_len = len;
1610 data = &dma_len;
1611 } else if ((u32)hbuf_slots == mei_hbuf_depth(dev)) {
1612 len = hbuf_len - hdr_len;
1613 mei_hdr.length = len;
1614 } else {
1615 return 0;
1618 if (mei_hdr.dma_ring)
1619 mei_dma_ring_write(dev, buf->data + cb->buf_idx, len);
1621 rets = mei_write_message(dev, &mei_hdr, hdr_len, data, mei_hdr.length);
1622 if (rets)
1623 goto err;
1625 cl->status = 0;
1626 cl->writing_state = MEI_WRITING;
1627 cb->buf_idx += len;
1629 if (first_chunk) {
1630 if (mei_cl_tx_flow_ctrl_creds_reduce(cl)) {
1631 rets = -EIO;
1632 goto err;
1636 if (mei_hdr.msg_complete)
1637 list_move_tail(&cb->list, &dev->write_waiting_list);
1639 return 0;
1641 err:
1642 cl->status = rets;
1643 list_move_tail(&cb->list, cmpl_list);
1644 return rets;
1648 * mei_cl_write - submit a write cb to mei device
1649 * assumes device_lock is locked
1651 * @cl: host client
1652 * @cb: write callback with filled data
1654 * Return: number of bytes sent on success, <0 on failure.
1656 ssize_t mei_cl_write(struct mei_cl *cl, struct mei_cl_cb *cb)
1658 struct mei_device *dev;
1659 struct mei_msg_data *buf;
1660 struct mei_msg_hdr mei_hdr;
1661 size_t hdr_len = sizeof(mei_hdr);
1662 size_t len, hbuf_len, dr_len;
1663 int hbuf_slots;
1664 u32 dr_slots;
1665 u32 dma_len;
1666 ssize_t rets;
1667 bool blocking;
1668 const void *data;
1670 if (WARN_ON(!cl || !cl->dev))
1671 return -ENODEV;
1673 if (WARN_ON(!cb))
1674 return -EINVAL;
1676 dev = cl->dev;
1678 buf = &cb->buf;
1679 len = buf->size;
1681 cl_dbg(dev, cl, "len=%zd\n", len);
1683 blocking = cb->blocking;
1684 data = buf->data;
1686 rets = pm_runtime_get(dev->dev);
1687 if (rets < 0 && rets != -EINPROGRESS) {
1688 pm_runtime_put_noidle(dev->dev);
1689 cl_err(dev, cl, "rpm: get failed %zd\n", rets);
1690 goto free;
1693 cb->buf_idx = 0;
1694 cl->writing_state = MEI_IDLE;
1697 rets = mei_cl_tx_flow_ctrl_creds(cl);
1698 if (rets < 0)
1699 goto err;
1701 mei_msg_hdr_init(&mei_hdr, cb);
1703 if (rets == 0) {
1704 cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
1705 rets = len;
1706 goto out;
1709 if (!mei_hbuf_acquire(dev)) {
1710 cl_dbg(dev, cl, "Cannot acquire the host buffer: not sending.\n");
1711 rets = len;
1712 goto out;
1715 hbuf_slots = mei_hbuf_empty_slots(dev);
1716 if (hbuf_slots < 0) {
1717 rets = -EOVERFLOW;
1718 goto out;
1721 hbuf_len = mei_slots2data(hbuf_slots);
1722 dr_slots = mei_dma_ring_empty_slots(dev);
1723 dr_len = mei_slots2data(dr_slots);
1725 if (len + hdr_len <= hbuf_len) {
1726 mei_hdr.length = len;
1727 mei_hdr.msg_complete = 1;
1728 } else if (dr_slots && hbuf_len >= hdr_len + sizeof(dma_len)) {
1729 mei_hdr.dma_ring = 1;
1730 if (len > dr_len)
1731 len = dr_len;
1732 else
1733 mei_hdr.msg_complete = 1;
1735 mei_hdr.length = sizeof(dma_len);
1736 dma_len = len;
1737 data = &dma_len;
1738 } else {
1739 len = hbuf_len - hdr_len;
1740 mei_hdr.length = len;
1743 if (mei_hdr.dma_ring)
1744 mei_dma_ring_write(dev, buf->data, len);
1746 rets = mei_write_message(dev, &mei_hdr, hdr_len,
1747 data, mei_hdr.length);
1748 if (rets)
1749 goto err;
1751 rets = mei_cl_tx_flow_ctrl_creds_reduce(cl);
1752 if (rets)
1753 goto err;
1755 cl->writing_state = MEI_WRITING;
1756 cb->buf_idx = len;
1757 /* restore return value */
1758 len = buf->size;
1760 out:
1761 if (mei_hdr.msg_complete)
1762 mei_tx_cb_enqueue(cb, &dev->write_waiting_list);
1763 else
1764 mei_tx_cb_enqueue(cb, &dev->write_list);
1766 cb = NULL;
1767 if (blocking && cl->writing_state != MEI_WRITE_COMPLETE) {
1769 mutex_unlock(&dev->device_lock);
1770 rets = wait_event_interruptible(cl->tx_wait,
1771 cl->writing_state == MEI_WRITE_COMPLETE ||
1772 (!mei_cl_is_connected(cl)));
1773 mutex_lock(&dev->device_lock);
1774 /* wait_event_interruptible returns -ERESTARTSYS */
1775 if (rets) {
1776 if (signal_pending(current))
1777 rets = -EINTR;
1778 goto err;
1780 if (cl->writing_state != MEI_WRITE_COMPLETE) {
1781 rets = -EFAULT;
1782 goto err;
1786 rets = len;
1787 err:
1788 cl_dbg(dev, cl, "rpm: autosuspend\n");
1789 pm_runtime_mark_last_busy(dev->dev);
1790 pm_runtime_put_autosuspend(dev->dev);
1791 free:
1792 mei_io_cb_free(cb);
1794 return rets;
1799 * mei_cl_complete - processes completed operation for a client
1801 * @cl: private data of the file object.
1802 * @cb: callback block.
1804 void mei_cl_complete(struct mei_cl *cl, struct mei_cl_cb *cb)
1806 struct mei_device *dev = cl->dev;
1808 switch (cb->fop_type) {
1809 case MEI_FOP_WRITE:
1810 mei_tx_cb_dequeue(cb);
1811 cl->writing_state = MEI_WRITE_COMPLETE;
1812 if (waitqueue_active(&cl->tx_wait)) {
1813 wake_up_interruptible(&cl->tx_wait);
1814 } else {
1815 pm_runtime_mark_last_busy(dev->dev);
1816 pm_request_autosuspend(dev->dev);
1818 break;
1820 case MEI_FOP_READ:
1821 list_add_tail(&cb->list, &cl->rd_completed);
1822 if (!mei_cl_is_fixed_address(cl) &&
1823 !WARN_ON(!cl->rx_flow_ctrl_creds))
1824 cl->rx_flow_ctrl_creds--;
1825 if (!mei_cl_bus_rx_event(cl))
1826 wake_up_interruptible(&cl->rx_wait);
1827 break;
1829 case MEI_FOP_CONNECT:
1830 case MEI_FOP_DISCONNECT:
1831 case MEI_FOP_NOTIFY_STOP:
1832 case MEI_FOP_NOTIFY_START:
1833 if (waitqueue_active(&cl->wait))
1834 wake_up(&cl->wait);
1836 break;
1837 case MEI_FOP_DISCONNECT_RSP:
1838 mei_io_cb_free(cb);
1839 mei_cl_set_disconnected(cl);
1840 break;
1841 default:
1842 BUG_ON(0);
1848 * mei_cl_all_disconnect - disconnect forcefully all connected clients
1850 * @dev: mei device
1852 void mei_cl_all_disconnect(struct mei_device *dev)
1854 struct mei_cl *cl;
1856 list_for_each_entry(cl, &dev->file_list, link)
1857 mei_cl_set_disconnected(cl);