2 * Intel Management Engine Interface (Intel MEI) Linux driver
3 * Copyright (c) 2012-2013, Intel Corporation.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 #include <linux/module.h>
17 #include <linux/device.h>
18 #include <linux/kernel.h>
19 #include <linux/sched.h>
20 #include <linux/init.h>
21 #include <linux/errno.h>
22 #include <linux/slab.h>
23 #include <linux/mutex.h>
24 #include <linux/interrupt.h>
25 #include <linux/mei_cl_bus.h>
30 #define to_mei_cl_driver(d) container_of(d, struct mei_cl_driver, driver)
31 #define to_mei_cl_device(d) container_of(d, struct mei_cl_device, dev)
34 * __mei_cl_send - internal client send (write)
37 * @buf: buffer to send
38 * @length: buffer length
39 * @blocking: wait for write completion
41 * Return: written size bytes or < 0 on error
43 ssize_t __mei_cl_send(struct mei_cl *cl, u8 *buf, size_t length,
46 struct mei_device *bus;
50 if (WARN_ON(!cl || !cl->dev))
55 mutex_lock(&bus->device_lock);
56 if (bus->dev_state != MEI_DEV_ENABLED) {
61 if (!mei_cl_is_connected(cl)) {
66 /* Check if we have an ME client device */
67 if (!mei_me_cl_is_active(cl->me_cl)) {
72 if (length > mei_cl_mtu(cl)) {
77 cb = mei_cl_alloc_cb(cl, length, MEI_FOP_WRITE, NULL);
83 memcpy(cb->buf.data, buf, length);
85 rets = mei_cl_write(cl, cb, blocking);
88 mutex_unlock(&bus->device_lock);
94 * __mei_cl_recv - internal client receive (read)
97 * @buf: buffer to receive
98 * @length: buffer length
100 * Return: read size in bytes of < 0 on error
102 ssize_t __mei_cl_recv(struct mei_cl *cl, u8 *buf, size_t length)
104 struct mei_device *bus;
105 struct mei_cl_cb *cb;
109 if (WARN_ON(!cl || !cl->dev))
114 mutex_lock(&bus->device_lock);
115 if (bus->dev_state != MEI_DEV_ENABLED) {
120 cb = mei_cl_read_cb(cl, NULL);
124 rets = mei_cl_read_start(cl, length, NULL);
125 if (rets && rets != -EBUSY)
128 /* wait on event only if there is no other waiter */
129 if (list_empty(&cl->rd_completed) && !waitqueue_active(&cl->rx_wait)) {
131 mutex_unlock(&bus->device_lock);
133 if (wait_event_interruptible(cl->rx_wait,
134 (!list_empty(&cl->rd_completed)) ||
135 (!mei_cl_is_connected(cl)))) {
137 if (signal_pending(current))
142 mutex_lock(&bus->device_lock);
144 if (!mei_cl_is_connected(cl)) {
150 cb = mei_cl_read_cb(cl, NULL);
162 r_length = min_t(size_t, length, cb->buf_idx);
163 memcpy(buf, cb->buf.data, r_length);
169 mutex_unlock(&bus->device_lock);
175 * mei_cldev_send - me device send (write)
177 * @cldev: me client device
178 * @buf: buffer to send
179 * @length: buffer length
181 * Return: written size in bytes or < 0 on error
183 ssize_t mei_cldev_send(struct mei_cl_device *cldev, u8 *buf, size_t length)
185 struct mei_cl *cl = cldev->cl;
190 return __mei_cl_send(cl, buf, length, 1);
192 EXPORT_SYMBOL_GPL(mei_cldev_send);
195 * mei_cldev_recv - client receive (read)
197 * @cldev: me client device
198 * @buf: buffer to receive
199 * @length: buffer length
201 * Return: read size in bytes of < 0 on error
203 ssize_t mei_cldev_recv(struct mei_cl_device *cldev, u8 *buf, size_t length)
205 struct mei_cl *cl = cldev->cl;
210 return __mei_cl_recv(cl, buf, length);
212 EXPORT_SYMBOL_GPL(mei_cldev_recv);
215 * mei_cl_bus_event_work - dispatch rx event for a bus device
216 * and schedule new work
220 static void mei_cl_bus_event_work(struct work_struct *work)
222 struct mei_cl_device *cldev;
224 cldev = container_of(work, struct mei_cl_device, event_work);
227 cldev->event_cb(cldev, cldev->events, cldev->event_context);
231 /* Prepare for the next read */
232 if (cldev->events_mask & BIT(MEI_CL_EVENT_RX))
233 mei_cl_read_start(cldev->cl, 0, NULL);
237 * mei_cl_bus_notify_event - schedule notify cb on bus client
241 * Return: true if event was scheduled
242 * false if the client is not waiting for event
244 bool mei_cl_bus_notify_event(struct mei_cl *cl)
246 struct mei_cl_device *cldev = cl->cldev;
248 if (!cldev || !cldev->event_cb)
251 if (!(cldev->events_mask & BIT(MEI_CL_EVENT_NOTIF)))
257 set_bit(MEI_CL_EVENT_NOTIF, &cldev->events);
259 schedule_work(&cldev->event_work);
261 cl->notify_ev = false;
267 * mei_cl_bus_rx_event - schedule rx event
271 * Return: true if event was scheduled
272 * false if the client is not waiting for event
274 bool mei_cl_bus_rx_event(struct mei_cl *cl)
276 struct mei_cl_device *cldev = cl->cldev;
278 if (!cldev || !cldev->event_cb)
281 if (!(cldev->events_mask & BIT(MEI_CL_EVENT_RX)))
284 set_bit(MEI_CL_EVENT_RX, &cldev->events);
286 schedule_work(&cldev->event_work);
292 * mei_cldev_register_event_cb - register event callback
294 * @cldev: me client devices
295 * @event_cb: callback function
296 * @events_mask: requested events bitmask
297 * @context: driver context data
299 * Return: 0 on success
300 * -EALREADY if an callback is already registered
303 int mei_cldev_register_event_cb(struct mei_cl_device *cldev,
304 unsigned long events_mask,
305 mei_cldev_event_cb_t event_cb, void *context)
313 cldev->events_mask = events_mask;
314 cldev->event_cb = event_cb;
315 cldev->event_context = context;
316 INIT_WORK(&cldev->event_work, mei_cl_bus_event_work);
318 if (cldev->events_mask & BIT(MEI_CL_EVENT_RX)) {
319 ret = mei_cl_read_start(cldev->cl, 0, NULL);
320 if (ret && ret != -EBUSY)
324 if (cldev->events_mask & BIT(MEI_CL_EVENT_NOTIF)) {
325 mutex_lock(&cldev->cl->dev->device_lock);
326 ret = mei_cl_notify_request(cldev->cl, NULL, event_cb ? 1 : 0);
327 mutex_unlock(&cldev->cl->dev->device_lock);
334 EXPORT_SYMBOL_GPL(mei_cldev_register_event_cb);
337 * mei_cldev_get_drvdata - driver data getter
339 * @cldev: mei client device
341 * Return: driver private data
343 void *mei_cldev_get_drvdata(const struct mei_cl_device *cldev)
345 return dev_get_drvdata(&cldev->dev);
347 EXPORT_SYMBOL_GPL(mei_cldev_get_drvdata);
350 * mei_cldev_set_drvdata - driver data setter
352 * @cldev: mei client device
353 * @data: data to store
355 void mei_cldev_set_drvdata(struct mei_cl_device *cldev, void *data)
357 dev_set_drvdata(&cldev->dev, data);
359 EXPORT_SYMBOL_GPL(mei_cldev_set_drvdata);
362 * mei_cldev_uuid - return uuid of the underlying me client
364 * @cldev: mei client device
366 * Return: me client uuid
368 const uuid_le *mei_cldev_uuid(const struct mei_cl_device *cldev)
370 return mei_me_cl_uuid(cldev->me_cl);
372 EXPORT_SYMBOL_GPL(mei_cldev_uuid);
375 * mei_cldev_ver - return protocol version of the underlying me client
377 * @cldev: mei client device
379 * Return: me client protocol version
381 u8 mei_cldev_ver(const struct mei_cl_device *cldev)
383 return mei_me_cl_ver(cldev->me_cl);
385 EXPORT_SYMBOL_GPL(mei_cldev_ver);
388 * mei_cldev_enabled - check whether the device is enabled
390 * @cldev: mei client device
392 * Return: true if me client is initialized and connected
394 bool mei_cldev_enabled(struct mei_cl_device *cldev)
396 return cldev->cl && mei_cl_is_connected(cldev->cl);
398 EXPORT_SYMBOL_GPL(mei_cldev_enabled);
401 * mei_cldev_enable_device - enable me client device
402 * create connection with me client
404 * @cldev: me client device
406 * Return: 0 on success and < 0 on error
408 int mei_cldev_enable(struct mei_cl_device *cldev)
410 struct mei_device *bus = cldev->bus;
417 mutex_lock(&bus->device_lock);
418 cl = mei_cl_alloc_linked(bus);
419 mutex_unlock(&bus->device_lock);
422 /* update pointers */
427 mutex_lock(&bus->device_lock);
428 if (mei_cl_is_connected(cl)) {
433 if (!mei_me_cl_is_active(cldev->me_cl)) {
434 dev_err(&cldev->dev, "me client is not active\n");
439 ret = mei_cl_connect(cl, cldev->me_cl, NULL);
441 dev_err(&cldev->dev, "cannot connect\n");
444 mutex_unlock(&bus->device_lock);
448 EXPORT_SYMBOL_GPL(mei_cldev_enable);
451 * mei_cldev_disable - disable me client device
452 * disconnect form the me client
454 * @cldev: me client device
456 * Return: 0 on success and < 0 on error
458 int mei_cldev_disable(struct mei_cl_device *cldev)
460 struct mei_device *bus;
464 if (!cldev || !cldev->cl)
471 cldev->event_cb = NULL;
473 mutex_lock(&bus->device_lock);
475 if (!mei_cl_is_connected(cl)) {
476 dev_err(bus->dev, "Already disconnected");
481 err = mei_cl_disconnect(cl);
483 dev_err(bus->dev, "Could not disconnect from the ME client");
486 /* Flush queues and remove any pending read */
487 mei_cl_flush_queues(cl, NULL);
493 mutex_unlock(&bus->device_lock);
496 EXPORT_SYMBOL_GPL(mei_cldev_disable);
499 * mei_cl_device_find - find matching entry in the driver id table
501 * @cldev: me client device
502 * @cldrv: me client driver
504 * Return: id on success; NULL if no id is matching
507 struct mei_cl_device_id *mei_cl_device_find(struct mei_cl_device *cldev,
508 struct mei_cl_driver *cldrv)
510 const struct mei_cl_device_id *id;
515 uuid = mei_me_cl_uuid(cldev->me_cl);
516 version = mei_me_cl_ver(cldev->me_cl);
518 id = cldrv->id_table;
519 while (uuid_le_cmp(NULL_UUID_LE, id->uuid)) {
520 if (!uuid_le_cmp(*uuid, id->uuid)) {
524 if (strncmp(cldev->name, id->name,
528 if (id->version != MEI_CL_VERSION_ANY)
529 if (id->version != version)
542 * mei_cl_device_match - device match function
547 * Return: 1 if matching device was found 0 otherwise
549 static int mei_cl_device_match(struct device *dev, struct device_driver *drv)
551 struct mei_cl_device *cldev = to_mei_cl_device(dev);
552 struct mei_cl_driver *cldrv = to_mei_cl_driver(drv);
553 const struct mei_cl_device_id *found_id;
558 if (!cldev->do_match)
561 if (!cldrv || !cldrv->id_table)
564 found_id = mei_cl_device_find(cldev, cldrv);
572 * mei_cl_device_probe - bus probe function
576 * Return: 0 on success; < 0 otherwise
578 static int mei_cl_device_probe(struct device *dev)
580 struct mei_cl_device *cldev;
581 struct mei_cl_driver *cldrv;
582 const struct mei_cl_device_id *id;
584 cldev = to_mei_cl_device(dev);
585 cldrv = to_mei_cl_driver(dev->driver);
590 if (!cldrv || !cldrv->probe)
593 id = mei_cl_device_find(cldev, cldrv);
597 __module_get(THIS_MODULE);
599 return cldrv->probe(cldev, id);
603 * mei_cl_device_remove - remove device from the bus
607 * Return: 0 on success; < 0 otherwise
609 static int mei_cl_device_remove(struct device *dev)
611 struct mei_cl_device *cldev = to_mei_cl_device(dev);
612 struct mei_cl_driver *cldrv;
615 if (!cldev || !dev->driver)
618 if (cldev->event_cb) {
619 cldev->event_cb = NULL;
620 cancel_work_sync(&cldev->event_work);
623 cldrv = to_mei_cl_driver(dev->driver);
625 ret = cldrv->remove(cldev);
627 module_put(THIS_MODULE);
633 static ssize_t name_show(struct device *dev, struct device_attribute *a,
636 struct mei_cl_device *cldev = to_mei_cl_device(dev);
639 len = snprintf(buf, PAGE_SIZE, "%s", cldev->name);
641 return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
643 static DEVICE_ATTR_RO(name);
645 static ssize_t uuid_show(struct device *dev, struct device_attribute *a,
648 struct mei_cl_device *cldev = to_mei_cl_device(dev);
649 const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
652 len = snprintf(buf, PAGE_SIZE, "%pUl", uuid);
654 return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
656 static DEVICE_ATTR_RO(uuid);
658 static ssize_t version_show(struct device *dev, struct device_attribute *a,
661 struct mei_cl_device *cldev = to_mei_cl_device(dev);
662 u8 version = mei_me_cl_ver(cldev->me_cl);
665 len = snprintf(buf, PAGE_SIZE, "%02X", version);
667 return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
669 static DEVICE_ATTR_RO(version);
671 static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
674 struct mei_cl_device *cldev = to_mei_cl_device(dev);
675 const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
678 len = snprintf(buf, PAGE_SIZE, "mei:%s:%pUl:", cldev->name, uuid);
679 return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
681 static DEVICE_ATTR_RO(modalias);
683 static struct attribute *mei_cldev_attrs[] = {
686 &dev_attr_version.attr,
687 &dev_attr_modalias.attr,
690 ATTRIBUTE_GROUPS(mei_cldev);
693 * mei_cl_device_uevent - me client bus uevent handler
696 * @env: uevent kobject
698 * Return: 0 on success -ENOMEM on when add_uevent_var fails
700 static int mei_cl_device_uevent(struct device *dev, struct kobj_uevent_env *env)
702 struct mei_cl_device *cldev = to_mei_cl_device(dev);
703 const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
704 u8 version = mei_me_cl_ver(cldev->me_cl);
706 if (add_uevent_var(env, "MEI_CL_VERSION=%d", version))
709 if (add_uevent_var(env, "MEI_CL_UUID=%pUl", uuid))
712 if (add_uevent_var(env, "MEI_CL_NAME=%s", cldev->name))
715 if (add_uevent_var(env, "MODALIAS=mei:%s:%pUl:%02X:",
716 cldev->name, uuid, version))
722 static struct bus_type mei_cl_bus_type = {
724 .dev_groups = mei_cldev_groups,
725 .match = mei_cl_device_match,
726 .probe = mei_cl_device_probe,
727 .remove = mei_cl_device_remove,
728 .uevent = mei_cl_device_uevent,
731 static struct mei_device *mei_dev_bus_get(struct mei_device *bus)
734 get_device(bus->dev);
739 static void mei_dev_bus_put(struct mei_device *bus)
742 put_device(bus->dev);
745 static void mei_cl_bus_dev_release(struct device *dev)
747 struct mei_cl_device *cldev = to_mei_cl_device(dev);
752 mei_me_cl_put(cldev->me_cl);
753 mei_dev_bus_put(cldev->bus);
757 static struct device_type mei_cl_device_type = {
758 .release = mei_cl_bus_dev_release,
762 * mei_cl_bus_set_name - set device name for me client device
764 * @cldev: me client device
766 static inline void mei_cl_bus_set_name(struct mei_cl_device *cldev)
768 dev_set_name(&cldev->dev, "mei:%s:%pUl:%02X",
770 mei_me_cl_uuid(cldev->me_cl),
771 mei_me_cl_ver(cldev->me_cl));
775 * mei_cl_bus_dev_alloc - initialize and allocate mei client device
780 * Return: allocated device structur or NULL on allocation failure
782 static struct mei_cl_device *mei_cl_bus_dev_alloc(struct mei_device *bus,
783 struct mei_me_client *me_cl)
785 struct mei_cl_device *cldev;
787 cldev = kzalloc(sizeof(struct mei_cl_device), GFP_KERNEL);
791 device_initialize(&cldev->dev);
792 cldev->dev.parent = bus->dev;
793 cldev->dev.bus = &mei_cl_bus_type;
794 cldev->dev.type = &mei_cl_device_type;
795 cldev->bus = mei_dev_bus_get(bus);
796 cldev->me_cl = mei_me_cl_get(me_cl);
797 mei_cl_bus_set_name(cldev);
799 INIT_LIST_HEAD(&cldev->bus_list);
805 * mei_cl_dev_setup - setup me client device
806 * run fix up routines and set the device name
809 * @cldev: me client device
811 * Return: true if the device is eligible for enumeration
813 static bool mei_cl_bus_dev_setup(struct mei_device *bus,
814 struct mei_cl_device *cldev)
817 mei_cl_bus_dev_fixup(cldev);
819 /* the device name can change during fix up */
821 mei_cl_bus_set_name(cldev);
823 return cldev->do_match == 1;
827 * mei_cl_bus_dev_add - add me client devices
829 * @cldev: me client device
831 * Return: 0 on success; < 0 on failre
833 static int mei_cl_bus_dev_add(struct mei_cl_device *cldev)
837 dev_dbg(cldev->bus->dev, "adding %pUL:%02X\n",
838 mei_me_cl_uuid(cldev->me_cl),
839 mei_me_cl_ver(cldev->me_cl));
840 ret = device_add(&cldev->dev);
848 * mei_cl_bus_dev_stop - stop the driver
850 * @cldev: me client device
852 static void mei_cl_bus_dev_stop(struct mei_cl_device *cldev)
855 device_release_driver(&cldev->dev);
859 * mei_cl_bus_dev_destroy - destroy me client devices object
861 * @cldev: me client device
863 * Locking: called under "dev->cl_bus_lock" lock
865 static void mei_cl_bus_dev_destroy(struct mei_cl_device *cldev)
868 WARN_ON(!mutex_is_locked(&cldev->bus->cl_bus_lock));
870 if (!cldev->is_added)
873 device_del(&cldev->dev);
875 list_del_init(&cldev->bus_list);
878 put_device(&cldev->dev);
882 * mei_cl_bus_remove_device - remove a devices form the bus
884 * @cldev: me client device
886 static void mei_cl_bus_remove_device(struct mei_cl_device *cldev)
888 mei_cl_bus_dev_stop(cldev);
889 mei_cl_bus_dev_destroy(cldev);
893 * mei_cl_bus_remove_devices - remove all devices form the bus
897 void mei_cl_bus_remove_devices(struct mei_device *bus)
899 struct mei_cl_device *cldev, *next;
901 mutex_lock(&bus->cl_bus_lock);
902 list_for_each_entry_safe(cldev, next, &bus->device_list, bus_list)
903 mei_cl_bus_remove_device(cldev);
904 mutex_unlock(&bus->cl_bus_lock);
909 * mei_cl_bus_dev_init - allocate and initializes an mei client devices
915 * Locking: called under "dev->cl_bus_lock" lock
917 static void mei_cl_bus_dev_init(struct mei_device *bus,
918 struct mei_me_client *me_cl)
920 struct mei_cl_device *cldev;
922 WARN_ON(!mutex_is_locked(&bus->cl_bus_lock));
924 dev_dbg(bus->dev, "initializing %pUl", mei_me_cl_uuid(me_cl));
926 if (me_cl->bus_added)
929 cldev = mei_cl_bus_dev_alloc(bus, me_cl);
933 me_cl->bus_added = true;
934 list_add_tail(&cldev->bus_list, &bus->device_list);
939 * mei_cl_bus_rescan - scan me clients list and add create
940 * devices for eligible clients
944 void mei_cl_bus_rescan(struct mei_device *bus)
946 struct mei_cl_device *cldev, *n;
947 struct mei_me_client *me_cl;
949 mutex_lock(&bus->cl_bus_lock);
951 down_read(&bus->me_clients_rwsem);
952 list_for_each_entry(me_cl, &bus->me_clients, list)
953 mei_cl_bus_dev_init(bus, me_cl);
954 up_read(&bus->me_clients_rwsem);
956 list_for_each_entry_safe(cldev, n, &bus->device_list, bus_list) {
958 if (!mei_me_cl_is_active(cldev->me_cl)) {
959 mei_cl_bus_remove_device(cldev);
966 if (mei_cl_bus_dev_setup(bus, cldev))
967 mei_cl_bus_dev_add(cldev);
969 list_del_init(&cldev->bus_list);
970 put_device(&cldev->dev);
973 mutex_unlock(&bus->cl_bus_lock);
975 dev_dbg(bus->dev, "rescan end");
978 void mei_cl_bus_rescan_work(struct work_struct *work)
980 struct mei_device *bus =
981 container_of(work, struct mei_device, bus_rescan_work);
982 struct mei_me_client *me_cl;
984 mutex_lock(&bus->device_lock);
985 me_cl = mei_me_cl_by_uuid(bus, &mei_amthif_guid);
987 mei_amthif_host_init(bus, me_cl);
988 mei_me_cl_put(me_cl);
989 mutex_unlock(&bus->device_lock);
991 mei_cl_bus_rescan(bus);
994 int __mei_cldev_driver_register(struct mei_cl_driver *cldrv,
995 struct module *owner)
999 cldrv->driver.name = cldrv->name;
1000 cldrv->driver.owner = owner;
1001 cldrv->driver.bus = &mei_cl_bus_type;
1003 err = driver_register(&cldrv->driver);
1007 pr_debug("mei: driver [%s] registered\n", cldrv->driver.name);
1011 EXPORT_SYMBOL_GPL(__mei_cldev_driver_register);
1013 void mei_cldev_driver_unregister(struct mei_cl_driver *cldrv)
1015 driver_unregister(&cldrv->driver);
1017 pr_debug("mei: driver [%s] unregistered\n", cldrv->driver.name);
1019 EXPORT_SYMBOL_GPL(mei_cldev_driver_unregister);
1022 int __init mei_cl_bus_init(void)
1024 return bus_register(&mei_cl_bus_type);
1027 void __exit mei_cl_bus_exit(void)
1029 bus_unregister(&mei_cl_bus_type);