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 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
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)
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--;
356 * mei_io_cb_init - allocate and initialize io callback
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);
374 INIT_LIST_HEAD(&cb->list);
383 * mei_io_list_flush_cl - removes cbs belonging to the cl.
385 * @head: an instance of our list structure
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) {
395 list_del_init(&cb->list);
396 if (cb->fop_type == MEI_FOP_READ)
403 * mei_io_tx_list_free_cl - removes cb belonging to the cl and free them
405 * @head: An instance of our list structure
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) {
415 mei_tx_cb_dequeue(cb);
420 * mei_io_list_free_fp - free cb from a list that matches file pointer
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)
435 * mei_cl_alloc_cb - a convenient wrapper for allocating read cb
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);
457 cb->buf.data = kmalloc(roundup(length, MEI_SLOT_SIZE), GFP_KERNEL);
462 cb->buf.size = length;
468 * mei_cl_enqueue_ctrl_wr_cb - a convenient wrapper for allocating
469 * and enqueuing of the control commands cb
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 */
487 length = max_t(size_t, length, mei_cl_mtu(cl));
489 cb = mei_cl_alloc_cb(cl, length, fop_type, fp);
493 list_add_tail(&cb->list, &cl->dev->ctrl_wr_list);
498 * mei_cl_read_cb - find this cl's callback in the read list
499 * for a specific file
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)
518 * mei_cl_flush_queues - flushes queue lists belonging to cl.
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))
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);
546 * mei_cl_init - initializes cl.
548 * @cl: host client to be initialized
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;
567 * mei_cl_allocate - allocates cl structure and sets it up.
570 * Return: The allocated file or NULL on failure
572 struct mei_cl *mei_cl_allocate(struct mei_device *dev)
576 cl = kmalloc(sizeof(struct mei_cl), GFP_KERNEL);
580 mei_cl_init(cl, dev);
586 * mei_cl_link - allocate host id in the host map
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;
599 if (WARN_ON(!cl || !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);
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);
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");
630 * mei_cl_unlink - remove host client from the list
636 int mei_cl_unlink(struct mei_cl *cl)
638 struct mei_device *dev;
640 /* don't shout on error exit path */
644 if (WARN_ON(!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));
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");
696 if (!dev->hbuf_is_ready) {
697 dev_dbg(dev->dev, "hbuf is not ready\n");
701 dev->hbuf_is_ready = false;
707 * mei_cl_wake_all - wake up readers, writers and event waiters so
708 * they can be interrupted
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");
739 * mei_cl_set_disconnected - set disconnected state and clear
740 * associated states and resources
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)
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);
758 cl->rx_flow_ctrl_creds = 0;
759 cl->tx_flow_ctrl_creds = 0;
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);
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))
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);
789 cl->state = MEI_FILE_CONNECTING;
790 cl->me_cl->connect_count++;
796 * mei_cl_send_disconnect - send disconnect request
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;
810 ret = mei_hbm_cl_disconnect_req(dev, cl);
813 cl->state = MEI_FILE_DISCONNECT_REPLY;
817 list_move_tail(&cb->list, &dev->ctrl_rd_list);
818 cl->timer_count = MEI_CONNECT_TIMEOUT;
819 mei_schedule_stall_timer(dev);
825 * mei_cl_irq_disconnect - processes close related operation from
826 * interrupt thread context - send disconnect request
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;
842 msg_slots = mei_hbm2slots(sizeof(struct hbm_client_connect_request));
843 slots = mei_hbuf_empty_slots(dev);
847 if ((u32)slots < msg_slots)
850 ret = mei_cl_send_disconnect(cl, cb);
852 list_move_tail(&cb->list, cmpl_list);
858 * __mei_cl_disconnect - disconnect host client from the me one
859 * internal function runtime pm has to be already acquired
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;
873 cl->state = MEI_FILE_DISCONNECTING;
875 cb = mei_cl_enqueue_ctrl_wr_cb(cl, 0, MEI_FOP_DISCONNECT, NULL);
881 if (mei_hbuf_acquire(dev)) {
882 rets = mei_cl_send_disconnect(cl, cb);
884 cl_err(dev, cl, "failed to disconnect.\n");
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);
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");
904 /* we disconnect also on error */
905 mei_cl_set_disconnected(cl);
907 cl_dbg(dev, cl, "successfully disconnected from FW client.\n");
914 * mei_cl_disconnect - disconnect host client from the me one
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;
927 if (WARN_ON(!cl || !cl->dev))
932 cl_dbg(dev, cl, "disconnecting");
934 if (!mei_cl_is_connected(cl))
937 if (mei_cl_is_fixed_address(cl)) {
938 mei_cl_set_disconnected(cl);
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);
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);
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);
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;
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))
990 * mei_cl_send_connect - send connect request
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;
1004 ret = mei_hbm_cl_connect_req(dev, cl);
1007 cl->state = MEI_FILE_DISCONNECT_REPLY;
1011 list_move_tail(&cb->list, &dev->ctrl_rd_list);
1012 cl->timer_count = MEI_CONNECT_TIMEOUT;
1013 mei_schedule_stall_timer(dev);
1018 * mei_cl_irq_connect - send connect request in irq_thread context
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;
1034 if (mei_cl_is_other_connecting(cl))
1037 msg_slots = mei_hbm2slots(sizeof(struct hbm_client_connect_request));
1038 slots = mei_hbuf_empty_slots(dev);
1042 if ((u32)slots < msg_slots)
1045 rets = mei_cl_send_connect(cl, cb);
1047 list_move_tail(&cb->list, cmpl_list);
1053 * mei_cl_connect - connect host client to the me one
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;
1070 if (WARN_ON(!cl || !cl->dev || !me_cl))
1075 rets = mei_cl_set_connecting(cl, me_cl);
1079 if (mei_cl_is_fixed_address(cl)) {
1080 cl->state = MEI_FILE_CONNECTED;
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);
1092 cb = mei_cl_enqueue_ctrl_wr_cb(cl, 0, MEI_FOP_CONNECT, fp);
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);
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);
1126 /* timeout or something went really wrong */
1128 cl->status = -EFAULT;
1133 cl_dbg(dev, cl, "rpm: autosuspend\n");
1134 pm_runtime_mark_last_busy(dev->dev);
1135 pm_runtime_put_autosuspend(dev->dev);
1140 if (!mei_cl_is_connected(cl))
1141 mei_cl_set_disconnected(cl);
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)
1158 cl = mei_cl_allocate(dev);
1164 ret = mei_cl_link(cl);
1171 return ERR_PTR(ret);
1175 * mei_cl_tx_flow_ctrl_creds - checks flow_control credits for cl.
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))
1186 if (cl->tx_flow_ctrl_creds > 0)
1189 if (mei_cl_is_fixed_address(cl))
1192 if (mei_cl_is_single_recv_buf(cl)) {
1193 if (cl->me_cl->tx_flow_ctrl_creds > 0)
1200 * mei_cl_tx_flow_ctrl_creds_reduce - reduces transmit flow control credits
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))
1214 if (mei_cl_is_fixed_address(cl))
1217 if (mei_cl_is_single_recv_buf(cl)) {
1218 if (WARN_ON(cl->me_cl->tx_flow_ctrl_creds <= 0))
1220 cl->me_cl->tx_flow_ctrl_creds--;
1222 if (WARN_ON(cl->tx_flow_ctrl_creds <= 0))
1224 cl->tx_flow_ctrl_creds--;
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;
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;
1256 return MEI_FOP_NOTIFY_STOP;
1260 * mei_cl_irq_notify - send notification request in irq_thread context
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;
1277 msg_slots = mei_hbm2slots(sizeof(struct hbm_client_connect_request));
1278 slots = mei_hbuf_empty_slots(dev);
1282 if ((u32)slots < msg_slots)
1285 request = mei_cl_notify_fop2req(cb->fop_type);
1286 ret = mei_hbm_cl_notify_req(dev, cl, request);
1289 list_move_tail(&cb->list, cmpl_list);
1293 list_move_tail(&cb->list, &dev->ctrl_rd_list);
1298 * mei_cl_notify_request - send notification stop/start request
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;
1316 if (WARN_ON(!cl || !cl->dev))
1321 if (!dev->hbm_f_ev_supported) {
1322 cl_dbg(dev, cl, "notifications not supported\n");
1326 if (!mei_cl_is_connected(cl))
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);
1336 fop_type = mei_cl_notify_req2fop(request);
1337 cb = mei_cl_enqueue_ctrl_wr_cb(cl, 0, fop_type, fp);
1343 if (mei_hbuf_acquire(dev)) {
1344 if (mei_hbm_cl_notify_req(dev, cl, request)) {
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 ||
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;
1365 cl_dbg(dev, cl, "rpm: autosuspend\n");
1366 pm_runtime_mark_last_busy(dev->dev);
1367 pm_runtime_put_autosuspend(dev->dev);
1374 * mei_cl_notify - raise notification
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)
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);
1398 kill_fasync(&cl->ev_async, SIGIO, POLL_PRI);
1403 * mei_cl_notify_get - get or wait for notification event
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;
1420 if (WARN_ON(!cl || !cl->dev))
1425 if (!dev->hbm_f_ev_supported) {
1426 cl_dbg(dev, cl, "notifications not supported\n");
1430 if (!mei_cl_is_connected(cl))
1439 mutex_unlock(&dev->device_lock);
1440 rets = wait_event_interruptible(cl->ev_wait, cl->notify_ev);
1441 mutex_lock(&dev->device_lock);
1447 *notify_ev = cl->notify_ev;
1448 cl->notify_ev = false;
1453 * mei_cl_read_start - the start read client message function.
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;
1467 if (WARN_ON(!cl || !cl->dev))
1472 if (!mei_cl_is_connected(cl))
1475 if (!mei_me_cl_is_active(cl->me_cl)) {
1476 cl_err(dev, cl, "no such me client\n");
1480 if (mei_cl_is_fixed_address(cl))
1483 /* HW currently supports only one pending read */
1484 if (cl->rx_flow_ctrl_creds)
1487 cb = mei_cl_enqueue_ctrl_wr_cb(cl, length, MEI_FOP_READ, fp);
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);
1499 if (mei_hbuf_acquire(dev)) {
1500 rets = mei_hbm_cl_flow_control_req(dev, cl);
1504 list_move_tail(&cb->list, &cl->rd_pending);
1506 cl->rx_flow_ctrl_creds++;
1509 cl_dbg(dev, cl, "rpm: autosuspend\n");
1510 pm_runtime_mark_last_busy(dev->dev);
1511 pm_runtime_put_autosuspend(dev->dev);
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
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);
1554 size_t hbuf_len, dr_len;
1562 if (WARN_ON(!cl || !cl->dev))
1569 first_chunk = cb->buf_idx == 0;
1571 rets = first_chunk ? mei_cl_tx_flow_ctrl_creds(cl) : 1;
1576 cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
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) {
1588 hbuf_len = mei_slots2data(hbuf_slots) & MEI_MSG_MAX_LEN_MASK;
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;
1606 mei_hdr.msg_complete = 1;
1608 mei_hdr.length = sizeof(dma_len);
1611 } else if ((u32)hbuf_slots == mei_hbuf_depth(dev)) {
1612 len = hbuf_len - hdr_len;
1613 mei_hdr.length = len;
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);
1626 cl->writing_state = MEI_WRITING;
1630 if (mei_cl_tx_flow_ctrl_creds_reduce(cl)) {
1636 if (mei_hdr.msg_complete)
1637 list_move_tail(&cb->list, &dev->write_waiting_list);
1643 list_move_tail(&cb->list, cmpl_list);
1648 * mei_cl_write - submit a write cb to mei device
1649 * assumes device_lock is locked
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;
1670 if (WARN_ON(!cl || !cl->dev))
1681 cl_dbg(dev, cl, "len=%zd\n", len);
1683 blocking = cb->blocking;
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);
1694 cl->writing_state = MEI_IDLE;
1697 rets = mei_cl_tx_flow_ctrl_creds(cl);
1701 mei_msg_hdr_init(&mei_hdr, cb);
1704 cl_dbg(dev, cl, "No flow control credentials: not sending.\n");
1709 if (!mei_hbuf_acquire(dev)) {
1710 cl_dbg(dev, cl, "Cannot acquire the host buffer: not sending.\n");
1715 hbuf_slots = mei_hbuf_empty_slots(dev);
1716 if (hbuf_slots < 0) {
1721 hbuf_len = mei_slots2data(hbuf_slots) & MEI_MSG_MAX_LEN_MASK;
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;
1733 mei_hdr.msg_complete = 1;
1735 mei_hdr.length = sizeof(dma_len);
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);
1751 rets = mei_cl_tx_flow_ctrl_creds_reduce(cl);
1755 cl->writing_state = MEI_WRITING;
1757 /* restore return value */
1761 if (mei_hdr.msg_complete)
1762 mei_tx_cb_enqueue(cb, &dev->write_waiting_list);
1764 mei_tx_cb_enqueue(cb, &dev->write_list);
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 */
1776 if (signal_pending(current))
1780 if (cl->writing_state != MEI_WRITE_COMPLETE) {
1788 cl_dbg(dev, cl, "rpm: autosuspend\n");
1789 pm_runtime_mark_last_busy(dev->dev);
1790 pm_runtime_put_autosuspend(dev->dev);
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) {
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);
1815 pm_runtime_mark_last_busy(dev->dev);
1816 pm_request_autosuspend(dev->dev);
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);
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))
1837 case MEI_FOP_DISCONNECT_RSP:
1839 mei_cl_set_disconnected(cl);
1848 * mei_cl_all_disconnect - disconnect forcefully all connected clients
1852 void mei_cl_all_disconnect(struct mei_device *dev)
1856 list_for_each_entry(cl, &dev->file_list, link)
1857 mei_cl_set_disconnected(cl);