1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2003-2019, 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>
13 #include <linux/mei.h>
20 * mei_me_cl_init - initialize 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);
31 * mei_me_cl_get - increases me client refcount
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))
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);
63 * mei_me_cl_put - decrease me client refcount and free client if necessary
65 * Locking: called under "dev->device_lock" lock
69 void mei_me_cl_put(struct mei_me_client *me_cl)
72 kref_put(&me_cl->refcnt, mei_me_cl_release);
76 * __mei_me_cl_del - delete me client from the list and decrease
82 * Locking: dev->me_clients_rwsem
84 static void __mei_me_cl_del(struct mei_device *dev, struct mei_me_client *me_cl)
89 list_del_init(&me_cl->list);
94 * mei_me_cl_del - delete me client from the list and decrease
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
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
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,
134 struct mei_me_client *me_cl;
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);
149 * mei_me_cl_by_uuid - locate me client by uuid
150 * increases ref count
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,
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);
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);
194 up_read(&dev->me_clients_rwsem);
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;
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);
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);
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
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)
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--;
358 * mei_io_cb_init - allocate and initialize io callback
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);
376 INIT_LIST_HEAD(&cb->list);
385 * mei_io_list_flush_cl - removes cbs belonging to the cl.
387 * @head: an instance of our list structure
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) {
397 list_del_init(&cb->list);
398 if (cb->fop_type == MEI_FOP_READ)
405 * mei_io_tx_list_free_cl - removes cb belonging to the cl and free them
407 * @head: An instance of our list structure
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) {
417 mei_tx_cb_dequeue(cb);
422 * mei_io_list_free_fp - free cb from a list that matches file pointer
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)
437 * mei_cl_alloc_cb - a convenient wrapper for allocating read cb
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);
459 cb->buf.data = kmalloc(roundup(length, MEI_SLOT_SIZE), GFP_KERNEL);
464 cb->buf.size = length;
470 * mei_cl_enqueue_ctrl_wr_cb - a convenient wrapper for allocating
471 * and enqueuing of the control commands cb
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 */
489 length = max_t(size_t, length, mei_cl_mtu(cl));
491 cb = mei_cl_alloc_cb(cl, length, fop_type, fp);
495 list_add_tail(&cb->list, &cl->dev->ctrl_wr_list);
500 * mei_cl_read_cb - find this cl's callback in the read list
501 * for a specific file
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)
520 * mei_cl_flush_queues - flushes queue lists belonging to cl.
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))
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);
548 * mei_cl_init - initializes cl.
550 * @cl: host client to be initialized
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;
569 * mei_cl_allocate - allocates cl structure and sets it up.
572 * Return: The allocated file or NULL on failure
574 struct mei_cl *mei_cl_allocate(struct mei_device *dev)
578 cl = kmalloc(sizeof(struct mei_cl), GFP_KERNEL);
582 mei_cl_init(cl, dev);
588 * mei_cl_link - allocate host id in the host map
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;
601 if (WARN_ON(!cl || !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);
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);
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");
632 * mei_cl_unlink - remove host client from the list
638 int mei_cl_unlink(struct mei_cl *cl)
640 struct mei_device *dev;
642 /* don't shout on error exit path */
646 if (WARN_ON(!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));
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");
698 if (!dev->hbuf_is_ready) {
699 dev_dbg(dev->dev, "hbuf is not ready\n");
703 dev->hbuf_is_ready = false;
709 * mei_cl_wake_all - wake up readers, writers and event waiters so
710 * they can be interrupted
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");
741 * mei_cl_set_disconnected - set disconnected state and clear
742 * associated states and resources
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)
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);
760 cl->rx_flow_ctrl_creds = 0;
761 cl->tx_flow_ctrl_creds = 0;
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);
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))
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);
791 cl->state = MEI_FILE_CONNECTING;
792 cl->me_cl->connect_count++;
798 * mei_cl_send_disconnect - send disconnect request
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;
812 ret = mei_hbm_cl_disconnect_req(dev, cl);
815 cl->state = MEI_FILE_DISCONNECT_REPLY;
819 list_move_tail(&cb->list, &dev->ctrl_rd_list);
820 cl->timer_count = MEI_CONNECT_TIMEOUT;
821 mei_schedule_stall_timer(dev);
827 * mei_cl_irq_disconnect - processes close related operation from
828 * interrupt thread context - send disconnect request
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;
844 msg_slots = mei_hbm2slots(sizeof(struct hbm_client_connect_request));
845 slots = mei_hbuf_empty_slots(dev);
849 if ((u32)slots < msg_slots)
852 ret = mei_cl_send_disconnect(cl, cb);
854 list_move_tail(&cb->list, cmpl_list);
860 * __mei_cl_disconnect - disconnect host client from the me one
861 * internal function runtime pm has to be already acquired
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;
875 cl->state = MEI_FILE_DISCONNECTING;
877 cb = mei_cl_enqueue_ctrl_wr_cb(cl, 0, MEI_FOP_DISCONNECT, NULL);
883 if (mei_hbuf_acquire(dev)) {
884 rets = mei_cl_send_disconnect(cl, cb);
886 cl_err(dev, cl, "failed to disconnect.\n");
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);
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");
906 /* we disconnect also on error */
907 mei_cl_set_disconnected(cl);
909 cl_dbg(dev, cl, "successfully disconnected from FW client.\n");
916 * mei_cl_disconnect - disconnect host client from the me one
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;
929 if (WARN_ON(!cl || !cl->dev))
934 cl_dbg(dev, cl, "disconnecting");
936 if (!mei_cl_is_connected(cl))
939 if (mei_cl_is_fixed_address(cl)) {
940 mei_cl_set_disconnected(cl);
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);
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);
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);
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;
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))
992 * mei_cl_send_connect - send connect request
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;
1006 ret = mei_hbm_cl_connect_req(dev, cl);
1009 cl->state = MEI_FILE_DISCONNECT_REPLY;
1013 list_move_tail(&cb->list, &dev->ctrl_rd_list);
1014 cl->timer_count = MEI_CONNECT_TIMEOUT;
1015 mei_schedule_stall_timer(dev);
1020 * mei_cl_irq_connect - send connect request in irq_thread context
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;
1036 if (mei_cl_is_other_connecting(cl))
1039 msg_slots = mei_hbm2slots(sizeof(struct hbm_client_connect_request));
1040 slots = mei_hbuf_empty_slots(dev);
1044 if ((u32)slots < msg_slots)
1047 rets = mei_cl_send_connect(cl, cb);
1049 list_move_tail(&cb->list, cmpl_list);
1055 * mei_cl_connect - connect host client to the me one
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;
1072 if (WARN_ON(!cl || !cl->dev || !me_cl))
1077 rets = mei_cl_set_connecting(cl, me_cl);
1081 if (mei_cl_is_fixed_address(cl)) {
1082 cl->state = MEI_FILE_CONNECTED;
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);
1094 cb = mei_cl_enqueue_ctrl_wr_cb(cl, 0, MEI_FOP_CONNECT, fp);
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);
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);
1128 /* timeout or something went really wrong */
1130 cl->status = -EFAULT;
1135 cl_dbg(dev, cl, "rpm: autosuspend\n");
1136 pm_runtime_mark_last_busy(dev->dev);
1137 pm_runtime_put_autosuspend(dev->dev);
1142 if (!mei_cl_is_connected(cl))
1143 mei_cl_set_disconnected(cl);
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)
1160 cl = mei_cl_allocate(dev);
1166 ret = mei_cl_link(cl);
1173 return ERR_PTR(ret);
1177 * mei_cl_tx_flow_ctrl_creds - checks flow_control credits for cl.
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))
1188 if (cl->tx_flow_ctrl_creds > 0)
1191 if (mei_cl_is_fixed_address(cl))
1194 if (mei_cl_is_single_recv_buf(cl)) {
1195 if (cl->me_cl->tx_flow_ctrl_creds > 0)
1202 * mei_cl_tx_flow_ctrl_creds_reduce - reduces transmit flow control credits
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))
1216 if (mei_cl_is_fixed_address(cl))
1219 if (mei_cl_is_single_recv_buf(cl)) {
1220 if (WARN_ON(cl->me_cl->tx_flow_ctrl_creds <= 0))
1222 cl->me_cl->tx_flow_ctrl_creds--;
1224 if (WARN_ON(cl->tx_flow_ctrl_creds <= 0))
1226 cl->tx_flow_ctrl_creds--;
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;
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;
1258 return MEI_FOP_NOTIFY_STOP;
1262 * mei_cl_irq_notify - send notification request in irq_thread context
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;
1279 msg_slots = mei_hbm2slots(sizeof(struct hbm_client_connect_request));
1280 slots = mei_hbuf_empty_slots(dev);
1284 if ((u32)slots < msg_slots)
1287 request = mei_cl_notify_fop2req(cb->fop_type);
1288 ret = mei_hbm_cl_notify_req(dev, cl, request);
1291 list_move_tail(&cb->list, cmpl_list);
1295 list_move_tail(&cb->list, &dev->ctrl_rd_list);
1300 * mei_cl_notify_request - send notification stop/start request
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;
1318 if (WARN_ON(!cl || !cl->dev))
1323 if (!dev->hbm_f_ev_supported) {
1324 cl_dbg(dev, cl, "notifications not supported\n");
1328 if (!mei_cl_is_connected(cl))
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);
1338 fop_type = mei_cl_notify_req2fop(request);
1339 cb = mei_cl_enqueue_ctrl_wr_cb(cl, 0, fop_type, fp);
1345 if (mei_hbuf_acquire(dev)) {
1346 if (mei_hbm_cl_notify_req(dev, cl, request)) {
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 ||
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;
1367 cl_dbg(dev, cl, "rpm: autosuspend\n");
1368 pm_runtime_mark_last_busy(dev->dev);
1369 pm_runtime_put_autosuspend(dev->dev);
1376 * mei_cl_notify - raise notification
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)
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);
1400 kill_fasync(&cl->ev_async, SIGIO, POLL_PRI);
1405 * mei_cl_notify_get - get or wait for notification event
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;
1422 if (WARN_ON(!cl || !cl->dev))
1427 if (!dev->hbm_f_ev_supported) {
1428 cl_dbg(dev, cl, "notifications not supported\n");
1432 if (!mei_cl_is_connected(cl))
1441 mutex_unlock(&dev->device_lock);
1442 rets = wait_event_interruptible(cl->ev_wait, cl->notify_ev);
1443 mutex_lock(&dev->device_lock);
1449 *notify_ev = cl->notify_ev;
1450 cl->notify_ev = false;
1455 * mei_cl_read_start - the start read client message function.
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;
1469 if (WARN_ON(!cl || !cl->dev))
1474 if (!mei_cl_is_connected(cl))
1477 if (!mei_me_cl_is_active(cl->me_cl)) {
1478 cl_err(dev, cl, "no such me client\n");
1482 if (mei_cl_is_fixed_address(cl))
1485 /* HW currently supports only one pending read */
1486 if (cl->rx_flow_ctrl_creds)
1489 cb = mei_cl_enqueue_ctrl_wr_cb(cl, length, MEI_FOP_READ, fp);
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);
1501 if (mei_hbuf_acquire(dev)) {
1502 rets = mei_hbm_cl_flow_control_req(dev, cl);
1506 list_move_tail(&cb->list, &cl->rd_pending);
1508 cl->rx_flow_ctrl_creds++;
1511 cl_dbg(dev, cl, "rpm: autosuspend\n");
1512 pm_runtime_mark_last_busy(dev->dev);
1513 pm_runtime_put_autosuspend(dev->dev);
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
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);
1556 size_t hbuf_len, dr_len;
1564 if (WARN_ON(!cl || !cl->dev))
1571 first_chunk = cb->buf_idx == 0;
1573 rets = first_chunk ? mei_cl_tx_flow_ctrl_creds(cl) : 1;
1578 cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
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) {
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;
1608 mei_hdr.msg_complete = 1;
1610 mei_hdr.length = sizeof(dma_len);
1613 } else if ((u32)hbuf_slots == mei_hbuf_depth(dev)) {
1614 len = hbuf_len - hdr_len;
1615 mei_hdr.length = len;
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);
1628 cl->writing_state = MEI_WRITING;
1632 if (mei_cl_tx_flow_ctrl_creds_reduce(cl)) {
1638 if (mei_hdr.msg_complete)
1639 list_move_tail(&cb->list, &dev->write_waiting_list);
1645 list_move_tail(&cb->list, cmpl_list);
1650 * mei_cl_write - submit a write cb to mei device
1651 * assumes device_lock is locked
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;
1672 if (WARN_ON(!cl || !cl->dev))
1683 cl_dbg(dev, cl, "len=%zd\n", len);
1685 blocking = cb->blocking;
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);
1696 cl->writing_state = MEI_IDLE;
1699 rets = mei_cl_tx_flow_ctrl_creds(cl);
1703 mei_msg_hdr_init(&mei_hdr, cb);
1706 cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
1711 if (!mei_hbuf_acquire(dev)) {
1712 cl_dbg(dev, cl, "Cannot acquire the host buffer: not sending.\n");
1717 hbuf_slots = mei_hbuf_empty_slots(dev);
1718 if (hbuf_slots < 0) {
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;
1735 mei_hdr.msg_complete = 1;
1737 mei_hdr.length = sizeof(dma_len);
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);
1753 rets = mei_cl_tx_flow_ctrl_creds_reduce(cl);
1757 cl->writing_state = MEI_WRITING;
1759 /* restore return value */
1763 if (mei_hdr.msg_complete)
1764 mei_tx_cb_enqueue(cb, &dev->write_waiting_list);
1766 mei_tx_cb_enqueue(cb, &dev->write_list);
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 */
1778 if (signal_pending(current))
1782 if (cl->writing_state != MEI_WRITE_COMPLETE) {
1790 cl_dbg(dev, cl, "rpm: autosuspend\n");
1791 pm_runtime_mark_last_busy(dev->dev);
1792 pm_runtime_put_autosuspend(dev->dev);
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) {
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);
1817 pm_runtime_mark_last_busy(dev->dev);
1818 pm_request_autosuspend(dev->dev);
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);
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))
1839 case MEI_FOP_DISCONNECT_RSP:
1841 mei_cl_set_disconnected(cl);
1850 * mei_cl_all_disconnect - disconnect forcefully all connected clients
1854 void mei_cl_all_disconnect(struct mei_device *dev)
1858 list_for_each_entry(cl, &dev->file_list, link)
1859 mei_cl_set_disconnected(cl);