dm writecache: add cond_resched to loop in persistent_memory_claim()
[linux/fpc-iii.git] / drivers / misc / mei / client.c
blobb32c825a09459e56e64a5e6d07f0454cff91d5b4
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 mei_me_cl_put(me_cl);
270 up_write(&dev->me_clients_rwsem);
274 * mei_me_cl_rm_by_uuid_id - remove all me clients matching client id
276 * @dev: the device structure
277 * @uuid: me client uuid
278 * @id: me client id
280 * Locking: called under "dev->device_lock" lock
282 void mei_me_cl_rm_by_uuid_id(struct mei_device *dev, const uuid_le *uuid, u8 id)
284 struct mei_me_client *me_cl;
286 dev_dbg(dev->dev, "remove %pUl %d\n", uuid, id);
288 down_write(&dev->me_clients_rwsem);
289 me_cl = __mei_me_cl_by_uuid_id(dev, uuid, id);
290 __mei_me_cl_del(dev, me_cl);
291 mei_me_cl_put(me_cl);
292 up_write(&dev->me_clients_rwsem);
296 * mei_me_cl_rm_all - remove all me clients
298 * @dev: the device structure
300 * Locking: called under "dev->device_lock" lock
302 void mei_me_cl_rm_all(struct mei_device *dev)
304 struct mei_me_client *me_cl, *next;
306 down_write(&dev->me_clients_rwsem);
307 list_for_each_entry_safe(me_cl, next, &dev->me_clients, list)
308 __mei_me_cl_del(dev, me_cl);
309 up_write(&dev->me_clients_rwsem);
313 * mei_io_cb_free - free mei_cb_private related memory
315 * @cb: mei callback struct
317 void mei_io_cb_free(struct mei_cl_cb *cb)
319 if (cb == NULL)
320 return;
322 list_del(&cb->list);
323 kfree(cb->buf.data);
324 kfree(cb);
328 * mei_tx_cb_queue - queue tx callback
330 * Locking: called under "dev->device_lock" lock
332 * @cb: mei callback struct
333 * @head: an instance of list to queue on
335 static inline void mei_tx_cb_enqueue(struct mei_cl_cb *cb,
336 struct list_head *head)
338 list_add_tail(&cb->list, head);
339 cb->cl->tx_cb_queued++;
343 * mei_tx_cb_dequeue - dequeue tx callback
345 * Locking: called under "dev->device_lock" lock
347 * @cb: mei callback struct to dequeue and free
349 static inline void mei_tx_cb_dequeue(struct mei_cl_cb *cb)
351 if (!WARN_ON(cb->cl->tx_cb_queued == 0))
352 cb->cl->tx_cb_queued--;
354 mei_io_cb_free(cb);
358 * mei_io_cb_init - allocate and initialize io callback
360 * @cl: mei client
361 * @type: operation type
362 * @fp: pointer to file structure
364 * Return: mei_cl_cb pointer or NULL;
366 static struct mei_cl_cb *mei_io_cb_init(struct mei_cl *cl,
367 enum mei_cb_file_ops type,
368 const struct file *fp)
370 struct mei_cl_cb *cb;
372 cb = kzalloc(sizeof(struct mei_cl_cb), GFP_KERNEL);
373 if (!cb)
374 return NULL;
376 INIT_LIST_HEAD(&cb->list);
377 cb->fp = fp;
378 cb->cl = cl;
379 cb->buf_idx = 0;
380 cb->fop_type = type;
381 return cb;
385 * mei_io_list_flush_cl - removes cbs belonging to the cl.
387 * @head: an instance of our list structure
388 * @cl: host client
390 static void mei_io_list_flush_cl(struct list_head *head,
391 const struct mei_cl *cl)
393 struct mei_cl_cb *cb, *next;
395 list_for_each_entry_safe(cb, next, head, list) {
396 if (cl == cb->cl) {
397 list_del_init(&cb->list);
398 if (cb->fop_type == MEI_FOP_READ)
399 mei_io_cb_free(cb);
405 * mei_io_tx_list_free_cl - removes cb belonging to the cl and free them
407 * @head: An instance of our list structure
408 * @cl: host client
410 static void mei_io_tx_list_free_cl(struct list_head *head,
411 const struct mei_cl *cl)
413 struct mei_cl_cb *cb, *next;
415 list_for_each_entry_safe(cb, next, head, list) {
416 if (cl == cb->cl)
417 mei_tx_cb_dequeue(cb);
422 * mei_io_list_free_fp - free cb from a list that matches file pointer
424 * @head: io list
425 * @fp: file pointer (matching cb file object), may be NULL
427 static void mei_io_list_free_fp(struct list_head *head, const struct file *fp)
429 struct mei_cl_cb *cb, *next;
431 list_for_each_entry_safe(cb, next, head, list)
432 if (!fp || fp == cb->fp)
433 mei_io_cb_free(cb);
437 * mei_cl_alloc_cb - a convenient wrapper for allocating read cb
439 * @cl: host client
440 * @length: size of the buffer
441 * @fop_type: operation type
442 * @fp: associated file pointer (might be NULL)
444 * Return: cb on success and NULL on failure
446 struct mei_cl_cb *mei_cl_alloc_cb(struct mei_cl *cl, size_t length,
447 enum mei_cb_file_ops fop_type,
448 const struct file *fp)
450 struct mei_cl_cb *cb;
452 cb = mei_io_cb_init(cl, fop_type, fp);
453 if (!cb)
454 return NULL;
456 if (length == 0)
457 return cb;
459 cb->buf.data = kmalloc(roundup(length, MEI_SLOT_SIZE), GFP_KERNEL);
460 if (!cb->buf.data) {
461 mei_io_cb_free(cb);
462 return NULL;
464 cb->buf.size = length;
466 return cb;
470 * mei_cl_enqueue_ctrl_wr_cb - a convenient wrapper for allocating
471 * and enqueuing of the control commands cb
473 * @cl: host client
474 * @length: size of the buffer
475 * @fop_type: operation type
476 * @fp: associated file pointer (might be NULL)
478 * Return: cb on success and NULL on failure
479 * Locking: called under "dev->device_lock" lock
481 struct mei_cl_cb *mei_cl_enqueue_ctrl_wr_cb(struct mei_cl *cl, size_t length,
482 enum mei_cb_file_ops fop_type,
483 const struct file *fp)
485 struct mei_cl_cb *cb;
487 /* for RX always allocate at least client's mtu */
488 if (length)
489 length = max_t(size_t, length, mei_cl_mtu(cl));
491 cb = mei_cl_alloc_cb(cl, length, fop_type, fp);
492 if (!cb)
493 return NULL;
495 list_add_tail(&cb->list, &cl->dev->ctrl_wr_list);
496 return cb;
500 * mei_cl_read_cb - find this cl's callback in the read list
501 * for a specific file
503 * @cl: host client
504 * @fp: file pointer (matching cb file object), may be NULL
506 * Return: cb on success, NULL if cb is not found
508 struct mei_cl_cb *mei_cl_read_cb(const struct mei_cl *cl, const struct file *fp)
510 struct mei_cl_cb *cb;
512 list_for_each_entry(cb, &cl->rd_completed, list)
513 if (!fp || fp == cb->fp)
514 return cb;
516 return NULL;
520 * mei_cl_flush_queues - flushes queue lists belonging to cl.
522 * @cl: host client
523 * @fp: file pointer (matching cb file object), may be NULL
525 * Return: 0 on success, -EINVAL if cl or cl->dev is NULL.
527 int mei_cl_flush_queues(struct mei_cl *cl, const struct file *fp)
529 struct mei_device *dev;
531 if (WARN_ON(!cl || !cl->dev))
532 return -EINVAL;
534 dev = cl->dev;
536 cl_dbg(dev, cl, "remove list entry belonging to cl\n");
537 mei_io_tx_list_free_cl(&cl->dev->write_list, cl);
538 mei_io_tx_list_free_cl(&cl->dev->write_waiting_list, cl);
539 mei_io_list_flush_cl(&cl->dev->ctrl_wr_list, cl);
540 mei_io_list_flush_cl(&cl->dev->ctrl_rd_list, cl);
541 mei_io_list_free_fp(&cl->rd_pending, fp);
542 mei_io_list_free_fp(&cl->rd_completed, fp);
544 return 0;
548 * mei_cl_init - initializes cl.
550 * @cl: host client to be initialized
551 * @dev: mei device
553 static void mei_cl_init(struct mei_cl *cl, struct mei_device *dev)
555 memset(cl, 0, sizeof(struct mei_cl));
556 init_waitqueue_head(&cl->wait);
557 init_waitqueue_head(&cl->rx_wait);
558 init_waitqueue_head(&cl->tx_wait);
559 init_waitqueue_head(&cl->ev_wait);
560 INIT_LIST_HEAD(&cl->rd_completed);
561 INIT_LIST_HEAD(&cl->rd_pending);
562 INIT_LIST_HEAD(&cl->link);
563 cl->writing_state = MEI_IDLE;
564 cl->state = MEI_FILE_UNINITIALIZED;
565 cl->dev = dev;
569 * mei_cl_allocate - allocates cl structure and sets it up.
571 * @dev: mei device
572 * Return: The allocated file or NULL on failure
574 struct mei_cl *mei_cl_allocate(struct mei_device *dev)
576 struct mei_cl *cl;
578 cl = kmalloc(sizeof(struct mei_cl), GFP_KERNEL);
579 if (!cl)
580 return NULL;
582 mei_cl_init(cl, dev);
584 return cl;
588 * mei_cl_link - allocate host id in the host map
590 * @cl: host client
592 * Return: 0 on success
593 * -EINVAL on incorrect values
594 * -EMFILE if open count exceeded.
596 int mei_cl_link(struct mei_cl *cl)
598 struct mei_device *dev;
599 int id;
601 if (WARN_ON(!cl || !cl->dev))
602 return -EINVAL;
604 dev = cl->dev;
606 id = find_first_zero_bit(dev->host_clients_map, MEI_CLIENTS_MAX);
607 if (id >= MEI_CLIENTS_MAX) {
608 dev_err(dev->dev, "id exceeded %d", MEI_CLIENTS_MAX);
609 return -EMFILE;
612 if (dev->open_handle_count >= MEI_MAX_OPEN_HANDLE_COUNT) {
613 dev_err(dev->dev, "open_handle_count exceeded %d",
614 MEI_MAX_OPEN_HANDLE_COUNT);
615 return -EMFILE;
618 dev->open_handle_count++;
620 cl->host_client_id = id;
621 list_add_tail(&cl->link, &dev->file_list);
623 set_bit(id, dev->host_clients_map);
625 cl->state = MEI_FILE_INITIALIZING;
627 cl_dbg(dev, cl, "link cl\n");
628 return 0;
632 * mei_cl_unlink - remove host client from the list
634 * @cl: host client
636 * Return: always 0
638 int mei_cl_unlink(struct mei_cl *cl)
640 struct mei_device *dev;
642 /* don't shout on error exit path */
643 if (!cl)
644 return 0;
646 if (WARN_ON(!cl->dev))
647 return 0;
649 dev = cl->dev;
651 cl_dbg(dev, cl, "unlink client");
653 if (dev->open_handle_count > 0)
654 dev->open_handle_count--;
656 /* never clear the 0 bit */
657 if (cl->host_client_id)
658 clear_bit(cl->host_client_id, dev->host_clients_map);
660 list_del_init(&cl->link);
662 cl->state = MEI_FILE_UNINITIALIZED;
663 cl->writing_state = MEI_IDLE;
665 WARN_ON(!list_empty(&cl->rd_completed) ||
666 !list_empty(&cl->rd_pending) ||
667 !list_empty(&cl->link));
669 return 0;
672 void mei_host_client_init(struct mei_device *dev)
674 mei_set_devstate(dev, MEI_DEV_ENABLED);
675 dev->reset_count = 0;
677 schedule_work(&dev->bus_rescan_work);
679 pm_runtime_mark_last_busy(dev->dev);
680 dev_dbg(dev->dev, "rpm: autosuspend\n");
681 pm_request_autosuspend(dev->dev);
685 * mei_hbuf_acquire - try to acquire host buffer
687 * @dev: the device structure
688 * Return: true if host buffer was acquired
690 bool mei_hbuf_acquire(struct mei_device *dev)
692 if (mei_pg_state(dev) == MEI_PG_ON ||
693 mei_pg_in_transition(dev)) {
694 dev_dbg(dev->dev, "device is in pg\n");
695 return false;
698 if (!dev->hbuf_is_ready) {
699 dev_dbg(dev->dev, "hbuf is not ready\n");
700 return false;
703 dev->hbuf_is_ready = false;
705 return true;
709 * mei_cl_wake_all - wake up readers, writers and event waiters so
710 * they can be interrupted
712 * @cl: host client
714 static void mei_cl_wake_all(struct mei_cl *cl)
716 struct mei_device *dev = cl->dev;
718 /* synchronized under device mutex */
719 if (waitqueue_active(&cl->rx_wait)) {
720 cl_dbg(dev, cl, "Waking up reading client!\n");
721 wake_up_interruptible(&cl->rx_wait);
723 /* synchronized under device mutex */
724 if (waitqueue_active(&cl->tx_wait)) {
725 cl_dbg(dev, cl, "Waking up writing client!\n");
726 wake_up_interruptible(&cl->tx_wait);
728 /* synchronized under device mutex */
729 if (waitqueue_active(&cl->ev_wait)) {
730 cl_dbg(dev, cl, "Waking up waiting for event clients!\n");
731 wake_up_interruptible(&cl->ev_wait);
733 /* synchronized under device mutex */
734 if (waitqueue_active(&cl->wait)) {
735 cl_dbg(dev, cl, "Waking up ctrl write clients!\n");
736 wake_up(&cl->wait);
741 * mei_cl_set_disconnected - set disconnected state and clear
742 * associated states and resources
744 * @cl: host client
746 static void mei_cl_set_disconnected(struct mei_cl *cl)
748 struct mei_device *dev = cl->dev;
750 if (cl->state == MEI_FILE_DISCONNECTED ||
751 cl->state <= MEI_FILE_INITIALIZING)
752 return;
754 cl->state = MEI_FILE_DISCONNECTED;
755 mei_io_tx_list_free_cl(&dev->write_list, cl);
756 mei_io_tx_list_free_cl(&dev->write_waiting_list, cl);
757 mei_io_list_flush_cl(&dev->ctrl_rd_list, cl);
758 mei_io_list_flush_cl(&dev->ctrl_wr_list, cl);
759 mei_cl_wake_all(cl);
760 cl->rx_flow_ctrl_creds = 0;
761 cl->tx_flow_ctrl_creds = 0;
762 cl->timer_count = 0;
764 if (!cl->me_cl)
765 return;
767 if (!WARN_ON(cl->me_cl->connect_count == 0))
768 cl->me_cl->connect_count--;
770 if (cl->me_cl->connect_count == 0)
771 cl->me_cl->tx_flow_ctrl_creds = 0;
773 mei_me_cl_put(cl->me_cl);
774 cl->me_cl = NULL;
777 static int mei_cl_set_connecting(struct mei_cl *cl, struct mei_me_client *me_cl)
779 if (!mei_me_cl_get(me_cl))
780 return -ENOENT;
782 /* only one connection is allowed for fixed address clients */
783 if (me_cl->props.fixed_address) {
784 if (me_cl->connect_count) {
785 mei_me_cl_put(me_cl);
786 return -EBUSY;
790 cl->me_cl = me_cl;
791 cl->state = MEI_FILE_CONNECTING;
792 cl->me_cl->connect_count++;
794 return 0;
798 * mei_cl_send_disconnect - send disconnect request
800 * @cl: host client
801 * @cb: callback block
803 * Return: 0, OK; otherwise, error.
805 static int mei_cl_send_disconnect(struct mei_cl *cl, struct mei_cl_cb *cb)
807 struct mei_device *dev;
808 int ret;
810 dev = cl->dev;
812 ret = mei_hbm_cl_disconnect_req(dev, cl);
813 cl->status = ret;
814 if (ret) {
815 cl->state = MEI_FILE_DISCONNECT_REPLY;
816 return ret;
819 list_move_tail(&cb->list, &dev->ctrl_rd_list);
820 cl->timer_count = MEI_CONNECT_TIMEOUT;
821 mei_schedule_stall_timer(dev);
823 return 0;
827 * mei_cl_irq_disconnect - processes close related operation from
828 * interrupt thread context - send disconnect request
830 * @cl: client
831 * @cb: callback block.
832 * @cmpl_list: complete list.
834 * Return: 0, OK; otherwise, error.
836 int mei_cl_irq_disconnect(struct mei_cl *cl, struct mei_cl_cb *cb,
837 struct list_head *cmpl_list)
839 struct mei_device *dev = cl->dev;
840 u32 msg_slots;
841 int slots;
842 int ret;
844 msg_slots = mei_hbm2slots(sizeof(struct hbm_client_connect_request));
845 slots = mei_hbuf_empty_slots(dev);
846 if (slots < 0)
847 return -EOVERFLOW;
849 if ((u32)slots < msg_slots)
850 return -EMSGSIZE;
852 ret = mei_cl_send_disconnect(cl, cb);
853 if (ret)
854 list_move_tail(&cb->list, cmpl_list);
856 return ret;
860 * __mei_cl_disconnect - disconnect host client from the me one
861 * internal function runtime pm has to be already acquired
863 * @cl: host client
865 * Return: 0 on success, <0 on failure.
867 static int __mei_cl_disconnect(struct mei_cl *cl)
869 struct mei_device *dev;
870 struct mei_cl_cb *cb;
871 int rets;
873 dev = cl->dev;
875 cl->state = MEI_FILE_DISCONNECTING;
877 cb = mei_cl_enqueue_ctrl_wr_cb(cl, 0, MEI_FOP_DISCONNECT, NULL);
878 if (!cb) {
879 rets = -ENOMEM;
880 goto out;
883 if (mei_hbuf_acquire(dev)) {
884 rets = mei_cl_send_disconnect(cl, cb);
885 if (rets) {
886 cl_err(dev, cl, "failed to disconnect.\n");
887 goto out;
891 mutex_unlock(&dev->device_lock);
892 wait_event_timeout(cl->wait,
893 cl->state == MEI_FILE_DISCONNECT_REPLY ||
894 cl->state == MEI_FILE_DISCONNECTED,
895 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
896 mutex_lock(&dev->device_lock);
898 rets = cl->status;
899 if (cl->state != MEI_FILE_DISCONNECT_REPLY &&
900 cl->state != MEI_FILE_DISCONNECTED) {
901 cl_dbg(dev, cl, "timeout on disconnect from FW client.\n");
902 rets = -ETIME;
905 out:
906 /* we disconnect also on error */
907 mei_cl_set_disconnected(cl);
908 if (!rets)
909 cl_dbg(dev, cl, "successfully disconnected from FW client.\n");
911 mei_io_cb_free(cb);
912 return rets;
916 * mei_cl_disconnect - disconnect host client from the me one
918 * @cl: host client
920 * Locking: called under "dev->device_lock" lock
922 * Return: 0 on success, <0 on failure.
924 int mei_cl_disconnect(struct mei_cl *cl)
926 struct mei_device *dev;
927 int rets;
929 if (WARN_ON(!cl || !cl->dev))
930 return -ENODEV;
932 dev = cl->dev;
934 cl_dbg(dev, cl, "disconnecting");
936 if (!mei_cl_is_connected(cl))
937 return 0;
939 if (mei_cl_is_fixed_address(cl)) {
940 mei_cl_set_disconnected(cl);
941 return 0;
944 if (dev->dev_state == MEI_DEV_POWER_DOWN) {
945 cl_dbg(dev, cl, "Device is powering down, don't bother with disconnection\n");
946 mei_cl_set_disconnected(cl);
947 return 0;
950 rets = pm_runtime_get(dev->dev);
951 if (rets < 0 && rets != -EINPROGRESS) {
952 pm_runtime_put_noidle(dev->dev);
953 cl_err(dev, cl, "rpm: get failed %d\n", rets);
954 return rets;
957 rets = __mei_cl_disconnect(cl);
959 cl_dbg(dev, cl, "rpm: autosuspend\n");
960 pm_runtime_mark_last_busy(dev->dev);
961 pm_runtime_put_autosuspend(dev->dev);
963 return rets;
968 * mei_cl_is_other_connecting - checks if other
969 * client with the same me client id is connecting
971 * @cl: private data of the file object
973 * Return: true if other client is connected, false - otherwise.
975 static bool mei_cl_is_other_connecting(struct mei_cl *cl)
977 struct mei_device *dev;
978 struct mei_cl_cb *cb;
980 dev = cl->dev;
982 list_for_each_entry(cb, &dev->ctrl_rd_list, list) {
983 if (cb->fop_type == MEI_FOP_CONNECT &&
984 mei_cl_me_id(cl) == mei_cl_me_id(cb->cl))
985 return true;
988 return false;
992 * mei_cl_send_connect - send connect request
994 * @cl: host client
995 * @cb: callback block
997 * Return: 0, OK; otherwise, error.
999 static int mei_cl_send_connect(struct mei_cl *cl, struct mei_cl_cb *cb)
1001 struct mei_device *dev;
1002 int ret;
1004 dev = cl->dev;
1006 ret = mei_hbm_cl_connect_req(dev, cl);
1007 cl->status = ret;
1008 if (ret) {
1009 cl->state = MEI_FILE_DISCONNECT_REPLY;
1010 return ret;
1013 list_move_tail(&cb->list, &dev->ctrl_rd_list);
1014 cl->timer_count = MEI_CONNECT_TIMEOUT;
1015 mei_schedule_stall_timer(dev);
1016 return 0;
1020 * mei_cl_irq_connect - send connect request in irq_thread context
1022 * @cl: host client
1023 * @cb: callback block
1024 * @cmpl_list: complete list
1026 * Return: 0, OK; otherwise, error.
1028 int mei_cl_irq_connect(struct mei_cl *cl, struct mei_cl_cb *cb,
1029 struct list_head *cmpl_list)
1031 struct mei_device *dev = cl->dev;
1032 u32 msg_slots;
1033 int slots;
1034 int rets;
1036 if (mei_cl_is_other_connecting(cl))
1037 return 0;
1039 msg_slots = mei_hbm2slots(sizeof(struct hbm_client_connect_request));
1040 slots = mei_hbuf_empty_slots(dev);
1041 if (slots < 0)
1042 return -EOVERFLOW;
1044 if ((u32)slots < msg_slots)
1045 return -EMSGSIZE;
1047 rets = mei_cl_send_connect(cl, cb);
1048 if (rets)
1049 list_move_tail(&cb->list, cmpl_list);
1051 return rets;
1055 * mei_cl_connect - connect host client to the me one
1057 * @cl: host client
1058 * @me_cl: me client
1059 * @fp: pointer to file structure
1061 * Locking: called under "dev->device_lock" lock
1063 * Return: 0 on success, <0 on failure.
1065 int mei_cl_connect(struct mei_cl *cl, struct mei_me_client *me_cl,
1066 const struct file *fp)
1068 struct mei_device *dev;
1069 struct mei_cl_cb *cb;
1070 int rets;
1072 if (WARN_ON(!cl || !cl->dev || !me_cl))
1073 return -ENODEV;
1075 dev = cl->dev;
1077 rets = mei_cl_set_connecting(cl, me_cl);
1078 if (rets)
1079 goto nortpm;
1081 if (mei_cl_is_fixed_address(cl)) {
1082 cl->state = MEI_FILE_CONNECTED;
1083 rets = 0;
1084 goto nortpm;
1087 rets = pm_runtime_get(dev->dev);
1088 if (rets < 0 && rets != -EINPROGRESS) {
1089 pm_runtime_put_noidle(dev->dev);
1090 cl_err(dev, cl, "rpm: get failed %d\n", rets);
1091 goto nortpm;
1094 cb = mei_cl_enqueue_ctrl_wr_cb(cl, 0, MEI_FOP_CONNECT, fp);
1095 if (!cb) {
1096 rets = -ENOMEM;
1097 goto out;
1100 /* run hbuf acquire last so we don't have to undo */
1101 if (!mei_cl_is_other_connecting(cl) && mei_hbuf_acquire(dev)) {
1102 rets = mei_cl_send_connect(cl, cb);
1103 if (rets)
1104 goto out;
1107 mutex_unlock(&dev->device_lock);
1108 wait_event_timeout(cl->wait,
1109 (cl->state == MEI_FILE_CONNECTED ||
1110 cl->state == MEI_FILE_DISCONNECTED ||
1111 cl->state == MEI_FILE_DISCONNECT_REQUIRED ||
1112 cl->state == MEI_FILE_DISCONNECT_REPLY),
1113 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
1114 mutex_lock(&dev->device_lock);
1116 if (!mei_cl_is_connected(cl)) {
1117 if (cl->state == MEI_FILE_DISCONNECT_REQUIRED) {
1118 mei_io_list_flush_cl(&dev->ctrl_rd_list, cl);
1119 mei_io_list_flush_cl(&dev->ctrl_wr_list, cl);
1120 /* ignore disconnect return valuue;
1121 * in case of failure reset will be invoked
1123 __mei_cl_disconnect(cl);
1124 rets = -EFAULT;
1125 goto out;
1128 /* timeout or something went really wrong */
1129 if (!cl->status)
1130 cl->status = -EFAULT;
1133 rets = cl->status;
1134 out:
1135 cl_dbg(dev, cl, "rpm: autosuspend\n");
1136 pm_runtime_mark_last_busy(dev->dev);
1137 pm_runtime_put_autosuspend(dev->dev);
1139 mei_io_cb_free(cb);
1141 nortpm:
1142 if (!mei_cl_is_connected(cl))
1143 mei_cl_set_disconnected(cl);
1145 return rets;
1149 * mei_cl_alloc_linked - allocate and link host client
1151 * @dev: the device structure
1153 * Return: cl on success ERR_PTR on failure
1155 struct mei_cl *mei_cl_alloc_linked(struct mei_device *dev)
1157 struct mei_cl *cl;
1158 int ret;
1160 cl = mei_cl_allocate(dev);
1161 if (!cl) {
1162 ret = -ENOMEM;
1163 goto err;
1166 ret = mei_cl_link(cl);
1167 if (ret)
1168 goto err;
1170 return cl;
1171 err:
1172 kfree(cl);
1173 return ERR_PTR(ret);
1177 * mei_cl_tx_flow_ctrl_creds - checks flow_control credits for cl.
1179 * @cl: host client
1181 * Return: 1 if tx_flow_ctrl_creds >0, 0 - otherwise.
1183 static int mei_cl_tx_flow_ctrl_creds(struct mei_cl *cl)
1185 if (WARN_ON(!cl || !cl->me_cl))
1186 return -EINVAL;
1188 if (cl->tx_flow_ctrl_creds > 0)
1189 return 1;
1191 if (mei_cl_is_fixed_address(cl))
1192 return 1;
1194 if (mei_cl_is_single_recv_buf(cl)) {
1195 if (cl->me_cl->tx_flow_ctrl_creds > 0)
1196 return 1;
1198 return 0;
1202 * mei_cl_tx_flow_ctrl_creds_reduce - reduces transmit flow control credits
1203 * for a client
1205 * @cl: host client
1207 * Return:
1208 * 0 on success
1209 * -EINVAL when ctrl credits are <= 0
1211 static int mei_cl_tx_flow_ctrl_creds_reduce(struct mei_cl *cl)
1213 if (WARN_ON(!cl || !cl->me_cl))
1214 return -EINVAL;
1216 if (mei_cl_is_fixed_address(cl))
1217 return 0;
1219 if (mei_cl_is_single_recv_buf(cl)) {
1220 if (WARN_ON(cl->me_cl->tx_flow_ctrl_creds <= 0))
1221 return -EINVAL;
1222 cl->me_cl->tx_flow_ctrl_creds--;
1223 } else {
1224 if (WARN_ON(cl->tx_flow_ctrl_creds <= 0))
1225 return -EINVAL;
1226 cl->tx_flow_ctrl_creds--;
1228 return 0;
1232 * mei_cl_notify_fop2req - convert fop to proper request
1234 * @fop: client notification start response command
1236 * Return: MEI_HBM_NOTIFICATION_START/STOP
1238 u8 mei_cl_notify_fop2req(enum mei_cb_file_ops fop)
1240 if (fop == MEI_FOP_NOTIFY_START)
1241 return MEI_HBM_NOTIFICATION_START;
1242 else
1243 return MEI_HBM_NOTIFICATION_STOP;
1247 * mei_cl_notify_req2fop - convert notification request top file operation type
1249 * @req: hbm notification request type
1251 * Return: MEI_FOP_NOTIFY_START/STOP
1253 enum mei_cb_file_ops mei_cl_notify_req2fop(u8 req)
1255 if (req == MEI_HBM_NOTIFICATION_START)
1256 return MEI_FOP_NOTIFY_START;
1257 else
1258 return MEI_FOP_NOTIFY_STOP;
1262 * mei_cl_irq_notify - send notification request in irq_thread context
1264 * @cl: client
1265 * @cb: callback block.
1266 * @cmpl_list: complete list.
1268 * Return: 0 on such and error otherwise.
1270 int mei_cl_irq_notify(struct mei_cl *cl, struct mei_cl_cb *cb,
1271 struct list_head *cmpl_list)
1273 struct mei_device *dev = cl->dev;
1274 u32 msg_slots;
1275 int slots;
1276 int ret;
1277 bool request;
1279 msg_slots = mei_hbm2slots(sizeof(struct hbm_client_connect_request));
1280 slots = mei_hbuf_empty_slots(dev);
1281 if (slots < 0)
1282 return -EOVERFLOW;
1284 if ((u32)slots < msg_slots)
1285 return -EMSGSIZE;
1287 request = mei_cl_notify_fop2req(cb->fop_type);
1288 ret = mei_hbm_cl_notify_req(dev, cl, request);
1289 if (ret) {
1290 cl->status = ret;
1291 list_move_tail(&cb->list, cmpl_list);
1292 return ret;
1295 list_move_tail(&cb->list, &dev->ctrl_rd_list);
1296 return 0;
1300 * mei_cl_notify_request - send notification stop/start request
1302 * @cl: host client
1303 * @fp: associate request with file
1304 * @request: 1 for start or 0 for stop
1306 * Locking: called under "dev->device_lock" lock
1308 * Return: 0 on such and error otherwise.
1310 int mei_cl_notify_request(struct mei_cl *cl,
1311 const struct file *fp, u8 request)
1313 struct mei_device *dev;
1314 struct mei_cl_cb *cb;
1315 enum mei_cb_file_ops fop_type;
1316 int rets;
1318 if (WARN_ON(!cl || !cl->dev))
1319 return -ENODEV;
1321 dev = cl->dev;
1323 if (!dev->hbm_f_ev_supported) {
1324 cl_dbg(dev, cl, "notifications not supported\n");
1325 return -EOPNOTSUPP;
1328 if (!mei_cl_is_connected(cl))
1329 return -ENODEV;
1331 rets = pm_runtime_get(dev->dev);
1332 if (rets < 0 && rets != -EINPROGRESS) {
1333 pm_runtime_put_noidle(dev->dev);
1334 cl_err(dev, cl, "rpm: get failed %d\n", rets);
1335 return rets;
1338 fop_type = mei_cl_notify_req2fop(request);
1339 cb = mei_cl_enqueue_ctrl_wr_cb(cl, 0, fop_type, fp);
1340 if (!cb) {
1341 rets = -ENOMEM;
1342 goto out;
1345 if (mei_hbuf_acquire(dev)) {
1346 if (mei_hbm_cl_notify_req(dev, cl, request)) {
1347 rets = -ENODEV;
1348 goto out;
1350 list_move_tail(&cb->list, &dev->ctrl_rd_list);
1353 mutex_unlock(&dev->device_lock);
1354 wait_event_timeout(cl->wait,
1355 cl->notify_en == request ||
1356 cl->status ||
1357 !mei_cl_is_connected(cl),
1358 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
1359 mutex_lock(&dev->device_lock);
1361 if (cl->notify_en != request && !cl->status)
1362 cl->status = -EFAULT;
1364 rets = cl->status;
1366 out:
1367 cl_dbg(dev, cl, "rpm: autosuspend\n");
1368 pm_runtime_mark_last_busy(dev->dev);
1369 pm_runtime_put_autosuspend(dev->dev);
1371 mei_io_cb_free(cb);
1372 return rets;
1376 * mei_cl_notify - raise notification
1378 * @cl: host client
1380 * Locking: called under "dev->device_lock" lock
1382 void mei_cl_notify(struct mei_cl *cl)
1384 struct mei_device *dev;
1386 if (!cl || !cl->dev)
1387 return;
1389 dev = cl->dev;
1391 if (!cl->notify_en)
1392 return;
1394 cl_dbg(dev, cl, "notify event");
1395 cl->notify_ev = true;
1396 if (!mei_cl_bus_notify_event(cl))
1397 wake_up_interruptible(&cl->ev_wait);
1399 if (cl->ev_async)
1400 kill_fasync(&cl->ev_async, SIGIO, POLL_PRI);
1405 * mei_cl_notify_get - get or wait for notification event
1407 * @cl: host client
1408 * @block: this request is blocking
1409 * @notify_ev: true if notification event was received
1411 * Locking: called under "dev->device_lock" lock
1413 * Return: 0 on such and error otherwise.
1415 int mei_cl_notify_get(struct mei_cl *cl, bool block, bool *notify_ev)
1417 struct mei_device *dev;
1418 int rets;
1420 *notify_ev = false;
1422 if (WARN_ON(!cl || !cl->dev))
1423 return -ENODEV;
1425 dev = cl->dev;
1427 if (!dev->hbm_f_ev_supported) {
1428 cl_dbg(dev, cl, "notifications not supported\n");
1429 return -EOPNOTSUPP;
1432 if (!mei_cl_is_connected(cl))
1433 return -ENODEV;
1435 if (cl->notify_ev)
1436 goto out;
1438 if (!block)
1439 return -EAGAIN;
1441 mutex_unlock(&dev->device_lock);
1442 rets = wait_event_interruptible(cl->ev_wait, cl->notify_ev);
1443 mutex_lock(&dev->device_lock);
1445 if (rets < 0)
1446 return rets;
1448 out:
1449 *notify_ev = cl->notify_ev;
1450 cl->notify_ev = false;
1451 return 0;
1455 * mei_cl_read_start - the start read client message function.
1457 * @cl: host client
1458 * @length: number of bytes to read
1459 * @fp: pointer to file structure
1461 * Return: 0 on success, <0 on failure.
1463 int mei_cl_read_start(struct mei_cl *cl, size_t length, const struct file *fp)
1465 struct mei_device *dev;
1466 struct mei_cl_cb *cb;
1467 int rets;
1469 if (WARN_ON(!cl || !cl->dev))
1470 return -ENODEV;
1472 dev = cl->dev;
1474 if (!mei_cl_is_connected(cl))
1475 return -ENODEV;
1477 if (!mei_me_cl_is_active(cl->me_cl)) {
1478 cl_err(dev, cl, "no such me client\n");
1479 return -ENOTTY;
1482 if (mei_cl_is_fixed_address(cl))
1483 return 0;
1485 /* HW currently supports only one pending read */
1486 if (cl->rx_flow_ctrl_creds)
1487 return -EBUSY;
1489 cb = mei_cl_enqueue_ctrl_wr_cb(cl, length, MEI_FOP_READ, fp);
1490 if (!cb)
1491 return -ENOMEM;
1493 rets = pm_runtime_get(dev->dev);
1494 if (rets < 0 && rets != -EINPROGRESS) {
1495 pm_runtime_put_noidle(dev->dev);
1496 cl_err(dev, cl, "rpm: get failed %d\n", rets);
1497 goto nortpm;
1500 rets = 0;
1501 if (mei_hbuf_acquire(dev)) {
1502 rets = mei_hbm_cl_flow_control_req(dev, cl);
1503 if (rets < 0)
1504 goto out;
1506 list_move_tail(&cb->list, &cl->rd_pending);
1508 cl->rx_flow_ctrl_creds++;
1510 out:
1511 cl_dbg(dev, cl, "rpm: autosuspend\n");
1512 pm_runtime_mark_last_busy(dev->dev);
1513 pm_runtime_put_autosuspend(dev->dev);
1514 nortpm:
1515 if (rets)
1516 mei_io_cb_free(cb);
1518 return rets;
1522 * mei_msg_hdr_init - initialize mei message header
1524 * @mei_hdr: mei message header
1525 * @cb: message callback structure
1527 static void mei_msg_hdr_init(struct mei_msg_hdr *mei_hdr, struct mei_cl_cb *cb)
1529 mei_hdr->host_addr = mei_cl_host_addr(cb->cl);
1530 mei_hdr->me_addr = mei_cl_me_id(cb->cl);
1531 mei_hdr->length = 0;
1532 mei_hdr->reserved = 0;
1533 mei_hdr->msg_complete = 0;
1534 mei_hdr->dma_ring = 0;
1535 mei_hdr->internal = cb->internal;
1539 * mei_cl_irq_write - write a message to device
1540 * from the interrupt thread context
1542 * @cl: client
1543 * @cb: callback block.
1544 * @cmpl_list: complete list.
1546 * Return: 0, OK; otherwise error.
1548 int mei_cl_irq_write(struct mei_cl *cl, struct mei_cl_cb *cb,
1549 struct list_head *cmpl_list)
1551 struct mei_device *dev;
1552 struct mei_msg_data *buf;
1553 struct mei_msg_hdr mei_hdr;
1554 size_t hdr_len = sizeof(mei_hdr);
1555 size_t len;
1556 size_t hbuf_len, dr_len;
1557 int hbuf_slots;
1558 u32 dr_slots;
1559 u32 dma_len;
1560 int rets;
1561 bool first_chunk;
1562 const void *data;
1564 if (WARN_ON(!cl || !cl->dev))
1565 return -ENODEV;
1567 dev = cl->dev;
1569 buf = &cb->buf;
1571 first_chunk = cb->buf_idx == 0;
1573 rets = first_chunk ? mei_cl_tx_flow_ctrl_creds(cl) : 1;
1574 if (rets < 0)
1575 goto err;
1577 if (rets == 0) {
1578 cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
1579 return 0;
1582 len = buf->size - cb->buf_idx;
1583 data = buf->data + cb->buf_idx;
1584 hbuf_slots = mei_hbuf_empty_slots(dev);
1585 if (hbuf_slots < 0) {
1586 rets = -EOVERFLOW;
1587 goto err;
1590 hbuf_len = mei_slots2data(hbuf_slots) & MEI_MSG_MAX_LEN_MASK;
1591 dr_slots = mei_dma_ring_empty_slots(dev);
1592 dr_len = mei_slots2data(dr_slots);
1594 mei_msg_hdr_init(&mei_hdr, cb);
1597 * Split the message only if we can write the whole host buffer
1598 * otherwise wait for next time the host buffer is empty.
1600 if (len + hdr_len <= hbuf_len) {
1601 mei_hdr.length = len;
1602 mei_hdr.msg_complete = 1;
1603 } else if (dr_slots && hbuf_len >= hdr_len + sizeof(dma_len)) {
1604 mei_hdr.dma_ring = 1;
1605 if (len > dr_len)
1606 len = dr_len;
1607 else
1608 mei_hdr.msg_complete = 1;
1610 mei_hdr.length = sizeof(dma_len);
1611 dma_len = len;
1612 data = &dma_len;
1613 } else if ((u32)hbuf_slots == mei_hbuf_depth(dev)) {
1614 len = hbuf_len - hdr_len;
1615 mei_hdr.length = len;
1616 } else {
1617 return 0;
1620 if (mei_hdr.dma_ring)
1621 mei_dma_ring_write(dev, buf->data + cb->buf_idx, len);
1623 rets = mei_write_message(dev, &mei_hdr, hdr_len, data, mei_hdr.length);
1624 if (rets)
1625 goto err;
1627 cl->status = 0;
1628 cl->writing_state = MEI_WRITING;
1629 cb->buf_idx += len;
1631 if (first_chunk) {
1632 if (mei_cl_tx_flow_ctrl_creds_reduce(cl)) {
1633 rets = -EIO;
1634 goto err;
1638 if (mei_hdr.msg_complete)
1639 list_move_tail(&cb->list, &dev->write_waiting_list);
1641 return 0;
1643 err:
1644 cl->status = rets;
1645 list_move_tail(&cb->list, cmpl_list);
1646 return rets;
1650 * mei_cl_write - submit a write cb to mei device
1651 * assumes device_lock is locked
1653 * @cl: host client
1654 * @cb: write callback with filled data
1656 * Return: number of bytes sent on success, <0 on failure.
1658 ssize_t mei_cl_write(struct mei_cl *cl, struct mei_cl_cb *cb)
1660 struct mei_device *dev;
1661 struct mei_msg_data *buf;
1662 struct mei_msg_hdr mei_hdr;
1663 size_t hdr_len = sizeof(mei_hdr);
1664 size_t len, hbuf_len, dr_len;
1665 int hbuf_slots;
1666 u32 dr_slots;
1667 u32 dma_len;
1668 ssize_t rets;
1669 bool blocking;
1670 const void *data;
1672 if (WARN_ON(!cl || !cl->dev))
1673 return -ENODEV;
1675 if (WARN_ON(!cb))
1676 return -EINVAL;
1678 dev = cl->dev;
1680 buf = &cb->buf;
1681 len = buf->size;
1683 cl_dbg(dev, cl, "len=%zd\n", len);
1685 blocking = cb->blocking;
1686 data = buf->data;
1688 rets = pm_runtime_get(dev->dev);
1689 if (rets < 0 && rets != -EINPROGRESS) {
1690 pm_runtime_put_noidle(dev->dev);
1691 cl_err(dev, cl, "rpm: get failed %zd\n", rets);
1692 goto free;
1695 cb->buf_idx = 0;
1696 cl->writing_state = MEI_IDLE;
1699 rets = mei_cl_tx_flow_ctrl_creds(cl);
1700 if (rets < 0)
1701 goto err;
1703 mei_msg_hdr_init(&mei_hdr, cb);
1705 if (rets == 0) {
1706 cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
1707 rets = len;
1708 goto out;
1711 if (!mei_hbuf_acquire(dev)) {
1712 cl_dbg(dev, cl, "Cannot acquire the host buffer: not sending.\n");
1713 rets = len;
1714 goto out;
1717 hbuf_slots = mei_hbuf_empty_slots(dev);
1718 if (hbuf_slots < 0) {
1719 rets = -EOVERFLOW;
1720 goto out;
1723 hbuf_len = mei_slots2data(hbuf_slots) & MEI_MSG_MAX_LEN_MASK;
1724 dr_slots = mei_dma_ring_empty_slots(dev);
1725 dr_len = mei_slots2data(dr_slots);
1727 if (len + hdr_len <= hbuf_len) {
1728 mei_hdr.length = len;
1729 mei_hdr.msg_complete = 1;
1730 } else if (dr_slots && hbuf_len >= hdr_len + sizeof(dma_len)) {
1731 mei_hdr.dma_ring = 1;
1732 if (len > dr_len)
1733 len = dr_len;
1734 else
1735 mei_hdr.msg_complete = 1;
1737 mei_hdr.length = sizeof(dma_len);
1738 dma_len = len;
1739 data = &dma_len;
1740 } else {
1741 len = hbuf_len - hdr_len;
1742 mei_hdr.length = len;
1745 if (mei_hdr.dma_ring)
1746 mei_dma_ring_write(dev, buf->data, len);
1748 rets = mei_write_message(dev, &mei_hdr, hdr_len,
1749 data, mei_hdr.length);
1750 if (rets)
1751 goto err;
1753 rets = mei_cl_tx_flow_ctrl_creds_reduce(cl);
1754 if (rets)
1755 goto err;
1757 cl->writing_state = MEI_WRITING;
1758 cb->buf_idx = len;
1759 /* restore return value */
1760 len = buf->size;
1762 out:
1763 if (mei_hdr.msg_complete)
1764 mei_tx_cb_enqueue(cb, &dev->write_waiting_list);
1765 else
1766 mei_tx_cb_enqueue(cb, &dev->write_list);
1768 cb = NULL;
1769 if (blocking && cl->writing_state != MEI_WRITE_COMPLETE) {
1771 mutex_unlock(&dev->device_lock);
1772 rets = wait_event_interruptible(cl->tx_wait,
1773 cl->writing_state == MEI_WRITE_COMPLETE ||
1774 (!mei_cl_is_connected(cl)));
1775 mutex_lock(&dev->device_lock);
1776 /* wait_event_interruptible returns -ERESTARTSYS */
1777 if (rets) {
1778 if (signal_pending(current))
1779 rets = -EINTR;
1780 goto err;
1782 if (cl->writing_state != MEI_WRITE_COMPLETE) {
1783 rets = -EFAULT;
1784 goto err;
1788 rets = len;
1789 err:
1790 cl_dbg(dev, cl, "rpm: autosuspend\n");
1791 pm_runtime_mark_last_busy(dev->dev);
1792 pm_runtime_put_autosuspend(dev->dev);
1793 free:
1794 mei_io_cb_free(cb);
1796 return rets;
1801 * mei_cl_complete - processes completed operation for a client
1803 * @cl: private data of the file object.
1804 * @cb: callback block.
1806 void mei_cl_complete(struct mei_cl *cl, struct mei_cl_cb *cb)
1808 struct mei_device *dev = cl->dev;
1810 switch (cb->fop_type) {
1811 case MEI_FOP_WRITE:
1812 mei_tx_cb_dequeue(cb);
1813 cl->writing_state = MEI_WRITE_COMPLETE;
1814 if (waitqueue_active(&cl->tx_wait)) {
1815 wake_up_interruptible(&cl->tx_wait);
1816 } else {
1817 pm_runtime_mark_last_busy(dev->dev);
1818 pm_request_autosuspend(dev->dev);
1820 break;
1822 case MEI_FOP_READ:
1823 list_add_tail(&cb->list, &cl->rd_completed);
1824 if (!mei_cl_is_fixed_address(cl) &&
1825 !WARN_ON(!cl->rx_flow_ctrl_creds))
1826 cl->rx_flow_ctrl_creds--;
1827 if (!mei_cl_bus_rx_event(cl))
1828 wake_up_interruptible(&cl->rx_wait);
1829 break;
1831 case MEI_FOP_CONNECT:
1832 case MEI_FOP_DISCONNECT:
1833 case MEI_FOP_NOTIFY_STOP:
1834 case MEI_FOP_NOTIFY_START:
1835 if (waitqueue_active(&cl->wait))
1836 wake_up(&cl->wait);
1838 break;
1839 case MEI_FOP_DISCONNECT_RSP:
1840 mei_io_cb_free(cb);
1841 mei_cl_set_disconnected(cl);
1842 break;
1843 default:
1844 BUG_ON(0);
1850 * mei_cl_all_disconnect - disconnect forcefully all connected clients
1852 * @dev: mei device
1854 void mei_cl_all_disconnect(struct mei_device *dev)
1856 struct mei_cl *cl;
1858 list_for_each_entry(cl, &dev->file_list, link)
1859 mei_cl_set_disconnected(cl);