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;
223 struct mei_device *bus;
225 cldev = container_of(work, struct mei_cl_device, event_work);
230 cldev->event_cb(cldev, cldev->events, cldev->event_context);
234 /* Prepare for the next read */
235 if (cldev->events_mask & BIT(MEI_CL_EVENT_RX)) {
236 mutex_lock(&bus->device_lock);
237 mei_cl_read_start(cldev->cl, 0, NULL);
238 mutex_unlock(&bus->device_lock);
243 * mei_cl_bus_notify_event - schedule notify cb on bus client
247 * Return: true if event was scheduled
248 * false if the client is not waiting for event
250 bool mei_cl_bus_notify_event(struct mei_cl *cl)
252 struct mei_cl_device *cldev = cl->cldev;
254 if (!cldev || !cldev->event_cb)
257 if (!(cldev->events_mask & BIT(MEI_CL_EVENT_NOTIF)))
263 set_bit(MEI_CL_EVENT_NOTIF, &cldev->events);
265 schedule_work(&cldev->event_work);
267 cl->notify_ev = false;
273 * mei_cl_bus_rx_event - schedule rx event
277 * Return: true if event was scheduled
278 * false if the client is not waiting for event
280 bool mei_cl_bus_rx_event(struct mei_cl *cl)
282 struct mei_cl_device *cldev = cl->cldev;
284 if (!cldev || !cldev->event_cb)
287 if (!(cldev->events_mask & BIT(MEI_CL_EVENT_RX)))
290 set_bit(MEI_CL_EVENT_RX, &cldev->events);
292 schedule_work(&cldev->event_work);
298 * mei_cldev_register_event_cb - register event callback
300 * @cldev: me client devices
301 * @event_cb: callback function
302 * @events_mask: requested events bitmask
303 * @context: driver context data
305 * Return: 0 on success
306 * -EALREADY if an callback is already registered
309 int mei_cldev_register_event_cb(struct mei_cl_device *cldev,
310 unsigned long events_mask,
311 mei_cldev_event_cb_t event_cb, void *context)
313 struct mei_device *bus = cldev->bus;
320 cldev->events_mask = events_mask;
321 cldev->event_cb = event_cb;
322 cldev->event_context = context;
323 INIT_WORK(&cldev->event_work, mei_cl_bus_event_work);
325 if (cldev->events_mask & BIT(MEI_CL_EVENT_RX)) {
326 mutex_lock(&bus->device_lock);
327 ret = mei_cl_read_start(cldev->cl, 0, NULL);
328 mutex_unlock(&bus->device_lock);
329 if (ret && ret != -EBUSY)
333 if (cldev->events_mask & BIT(MEI_CL_EVENT_NOTIF)) {
334 mutex_lock(&bus->device_lock);
335 ret = mei_cl_notify_request(cldev->cl, NULL, event_cb ? 1 : 0);
336 mutex_unlock(&bus->device_lock);
343 EXPORT_SYMBOL_GPL(mei_cldev_register_event_cb);
346 * mei_cldev_get_drvdata - driver data getter
348 * @cldev: mei client device
350 * Return: driver private data
352 void *mei_cldev_get_drvdata(const struct mei_cl_device *cldev)
354 return dev_get_drvdata(&cldev->dev);
356 EXPORT_SYMBOL_GPL(mei_cldev_get_drvdata);
359 * mei_cldev_set_drvdata - driver data setter
361 * @cldev: mei client device
362 * @data: data to store
364 void mei_cldev_set_drvdata(struct mei_cl_device *cldev, void *data)
366 dev_set_drvdata(&cldev->dev, data);
368 EXPORT_SYMBOL_GPL(mei_cldev_set_drvdata);
371 * mei_cldev_uuid - return uuid of the underlying me client
373 * @cldev: mei client device
375 * Return: me client uuid
377 const uuid_le *mei_cldev_uuid(const struct mei_cl_device *cldev)
379 return mei_me_cl_uuid(cldev->me_cl);
381 EXPORT_SYMBOL_GPL(mei_cldev_uuid);
384 * mei_cldev_ver - return protocol version of the underlying me client
386 * @cldev: mei client device
388 * Return: me client protocol version
390 u8 mei_cldev_ver(const struct mei_cl_device *cldev)
392 return mei_me_cl_ver(cldev->me_cl);
394 EXPORT_SYMBOL_GPL(mei_cldev_ver);
397 * mei_cldev_enabled - check whether the device is enabled
399 * @cldev: mei client device
401 * Return: true if me client is initialized and connected
403 bool mei_cldev_enabled(struct mei_cl_device *cldev)
405 return cldev->cl && mei_cl_is_connected(cldev->cl);
407 EXPORT_SYMBOL_GPL(mei_cldev_enabled);
410 * mei_cldev_enable_device - enable me client device
411 * create connection with me client
413 * @cldev: me client device
415 * Return: 0 on success and < 0 on error
417 int mei_cldev_enable(struct mei_cl_device *cldev)
419 struct mei_device *bus = cldev->bus;
426 mutex_lock(&bus->device_lock);
427 cl = mei_cl_alloc_linked(bus);
428 mutex_unlock(&bus->device_lock);
431 /* update pointers */
436 mutex_lock(&bus->device_lock);
437 if (mei_cl_is_connected(cl)) {
442 if (!mei_me_cl_is_active(cldev->me_cl)) {
443 dev_err(&cldev->dev, "me client is not active\n");
448 ret = mei_cl_connect(cl, cldev->me_cl, NULL);
450 dev_err(&cldev->dev, "cannot connect\n");
453 mutex_unlock(&bus->device_lock);
457 EXPORT_SYMBOL_GPL(mei_cldev_enable);
460 * mei_cldev_disable - disable me client device
461 * disconnect form the me client
463 * @cldev: me client device
465 * Return: 0 on success and < 0 on error
467 int mei_cldev_disable(struct mei_cl_device *cldev)
469 struct mei_device *bus;
473 if (!cldev || !cldev->cl)
480 cldev->event_cb = NULL;
482 mutex_lock(&bus->device_lock);
484 if (!mei_cl_is_connected(cl)) {
485 dev_err(bus->dev, "Already disconnected");
490 err = mei_cl_disconnect(cl);
492 dev_err(bus->dev, "Could not disconnect from the ME client");
495 /* Flush queues and remove any pending read */
496 mei_cl_flush_queues(cl, NULL);
502 mutex_unlock(&bus->device_lock);
505 EXPORT_SYMBOL_GPL(mei_cldev_disable);
508 * mei_cl_device_find - find matching entry in the driver id table
510 * @cldev: me client device
511 * @cldrv: me client driver
513 * Return: id on success; NULL if no id is matching
516 struct mei_cl_device_id *mei_cl_device_find(struct mei_cl_device *cldev,
517 struct mei_cl_driver *cldrv)
519 const struct mei_cl_device_id *id;
524 uuid = mei_me_cl_uuid(cldev->me_cl);
525 version = mei_me_cl_ver(cldev->me_cl);
527 id = cldrv->id_table;
528 while (uuid_le_cmp(NULL_UUID_LE, id->uuid)) {
529 if (!uuid_le_cmp(*uuid, id->uuid)) {
533 if (strncmp(cldev->name, id->name,
537 if (id->version != MEI_CL_VERSION_ANY)
538 if (id->version != version)
551 * mei_cl_device_match - device match function
556 * Return: 1 if matching device was found 0 otherwise
558 static int mei_cl_device_match(struct device *dev, struct device_driver *drv)
560 struct mei_cl_device *cldev = to_mei_cl_device(dev);
561 struct mei_cl_driver *cldrv = to_mei_cl_driver(drv);
562 const struct mei_cl_device_id *found_id;
567 if (!cldev->do_match)
570 if (!cldrv || !cldrv->id_table)
573 found_id = mei_cl_device_find(cldev, cldrv);
581 * mei_cl_device_probe - bus probe function
585 * Return: 0 on success; < 0 otherwise
587 static int mei_cl_device_probe(struct device *dev)
589 struct mei_cl_device *cldev;
590 struct mei_cl_driver *cldrv;
591 const struct mei_cl_device_id *id;
594 cldev = to_mei_cl_device(dev);
595 cldrv = to_mei_cl_driver(dev->driver);
600 if (!cldrv || !cldrv->probe)
603 id = mei_cl_device_find(cldev, cldrv);
607 ret = cldrv->probe(cldev, id);
611 __module_get(THIS_MODULE);
616 * mei_cl_device_remove - remove device from the bus
620 * Return: 0 on success; < 0 otherwise
622 static int mei_cl_device_remove(struct device *dev)
624 struct mei_cl_device *cldev = to_mei_cl_device(dev);
625 struct mei_cl_driver *cldrv;
628 if (!cldev || !dev->driver)
631 if (cldev->event_cb) {
632 cldev->event_cb = NULL;
633 cancel_work_sync(&cldev->event_work);
636 cldrv = to_mei_cl_driver(dev->driver);
638 ret = cldrv->remove(cldev);
640 module_put(THIS_MODULE);
646 static ssize_t name_show(struct device *dev, struct device_attribute *a,
649 struct mei_cl_device *cldev = to_mei_cl_device(dev);
651 return scnprintf(buf, PAGE_SIZE, "%s", cldev->name);
653 static DEVICE_ATTR_RO(name);
655 static ssize_t uuid_show(struct device *dev, struct device_attribute *a,
658 struct mei_cl_device *cldev = to_mei_cl_device(dev);
659 const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
661 return scnprintf(buf, PAGE_SIZE, "%pUl", uuid);
663 static DEVICE_ATTR_RO(uuid);
665 static ssize_t version_show(struct device *dev, struct device_attribute *a,
668 struct mei_cl_device *cldev = to_mei_cl_device(dev);
669 u8 version = mei_me_cl_ver(cldev->me_cl);
671 return scnprintf(buf, PAGE_SIZE, "%02X", version);
673 static DEVICE_ATTR_RO(version);
675 static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
678 struct mei_cl_device *cldev = to_mei_cl_device(dev);
679 const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
681 return scnprintf(buf, PAGE_SIZE, "mei:%s:%pUl:", cldev->name, uuid);
683 static DEVICE_ATTR_RO(modalias);
685 static struct attribute *mei_cldev_attrs[] = {
688 &dev_attr_version.attr,
689 &dev_attr_modalias.attr,
692 ATTRIBUTE_GROUPS(mei_cldev);
695 * mei_cl_device_uevent - me client bus uevent handler
698 * @env: uevent kobject
700 * Return: 0 on success -ENOMEM on when add_uevent_var fails
702 static int mei_cl_device_uevent(struct device *dev, struct kobj_uevent_env *env)
704 struct mei_cl_device *cldev = to_mei_cl_device(dev);
705 const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
706 u8 version = mei_me_cl_ver(cldev->me_cl);
708 if (add_uevent_var(env, "MEI_CL_VERSION=%d", version))
711 if (add_uevent_var(env, "MEI_CL_UUID=%pUl", uuid))
714 if (add_uevent_var(env, "MEI_CL_NAME=%s", cldev->name))
717 if (add_uevent_var(env, "MODALIAS=mei:%s:%pUl:%02X:",
718 cldev->name, uuid, version))
724 static struct bus_type mei_cl_bus_type = {
726 .dev_groups = mei_cldev_groups,
727 .match = mei_cl_device_match,
728 .probe = mei_cl_device_probe,
729 .remove = mei_cl_device_remove,
730 .uevent = mei_cl_device_uevent,
733 static struct mei_device *mei_dev_bus_get(struct mei_device *bus)
736 get_device(bus->dev);
741 static void mei_dev_bus_put(struct mei_device *bus)
744 put_device(bus->dev);
747 static void mei_cl_bus_dev_release(struct device *dev)
749 struct mei_cl_device *cldev = to_mei_cl_device(dev);
754 mei_me_cl_put(cldev->me_cl);
755 mei_dev_bus_put(cldev->bus);
759 static struct device_type mei_cl_device_type = {
760 .release = mei_cl_bus_dev_release,
764 * mei_cl_bus_set_name - set device name for me client device
766 * @cldev: me client device
768 static inline void mei_cl_bus_set_name(struct mei_cl_device *cldev)
770 dev_set_name(&cldev->dev, "mei:%s:%pUl:%02X",
772 mei_me_cl_uuid(cldev->me_cl),
773 mei_me_cl_ver(cldev->me_cl));
777 * mei_cl_bus_dev_alloc - initialize and allocate mei client device
782 * Return: allocated device structur or NULL on allocation failure
784 static struct mei_cl_device *mei_cl_bus_dev_alloc(struct mei_device *bus,
785 struct mei_me_client *me_cl)
787 struct mei_cl_device *cldev;
789 cldev = kzalloc(sizeof(struct mei_cl_device), GFP_KERNEL);
793 device_initialize(&cldev->dev);
794 cldev->dev.parent = bus->dev;
795 cldev->dev.bus = &mei_cl_bus_type;
796 cldev->dev.type = &mei_cl_device_type;
797 cldev->bus = mei_dev_bus_get(bus);
798 cldev->me_cl = mei_me_cl_get(me_cl);
799 mei_cl_bus_set_name(cldev);
801 INIT_LIST_HEAD(&cldev->bus_list);
807 * mei_cl_dev_setup - setup me client device
808 * run fix up routines and set the device name
811 * @cldev: me client device
813 * Return: true if the device is eligible for enumeration
815 static bool mei_cl_bus_dev_setup(struct mei_device *bus,
816 struct mei_cl_device *cldev)
819 mei_cl_bus_dev_fixup(cldev);
821 /* the device name can change during fix up */
823 mei_cl_bus_set_name(cldev);
825 return cldev->do_match == 1;
829 * mei_cl_bus_dev_add - add me client devices
831 * @cldev: me client device
833 * Return: 0 on success; < 0 on failre
835 static int mei_cl_bus_dev_add(struct mei_cl_device *cldev)
839 dev_dbg(cldev->bus->dev, "adding %pUL:%02X\n",
840 mei_me_cl_uuid(cldev->me_cl),
841 mei_me_cl_ver(cldev->me_cl));
842 ret = device_add(&cldev->dev);
850 * mei_cl_bus_dev_stop - stop the driver
852 * @cldev: me client device
854 static void mei_cl_bus_dev_stop(struct mei_cl_device *cldev)
857 device_release_driver(&cldev->dev);
861 * mei_cl_bus_dev_destroy - destroy me client devices object
863 * @cldev: me client device
865 * Locking: called under "dev->cl_bus_lock" lock
867 static void mei_cl_bus_dev_destroy(struct mei_cl_device *cldev)
870 WARN_ON(!mutex_is_locked(&cldev->bus->cl_bus_lock));
872 if (!cldev->is_added)
875 device_del(&cldev->dev);
877 list_del_init(&cldev->bus_list);
880 put_device(&cldev->dev);
884 * mei_cl_bus_remove_device - remove a devices form the bus
886 * @cldev: me client device
888 static void mei_cl_bus_remove_device(struct mei_cl_device *cldev)
890 mei_cl_bus_dev_stop(cldev);
891 mei_cl_bus_dev_destroy(cldev);
895 * mei_cl_bus_remove_devices - remove all devices form the bus
899 void mei_cl_bus_remove_devices(struct mei_device *bus)
901 struct mei_cl_device *cldev, *next;
903 mutex_lock(&bus->cl_bus_lock);
904 list_for_each_entry_safe(cldev, next, &bus->device_list, bus_list)
905 mei_cl_bus_remove_device(cldev);
906 mutex_unlock(&bus->cl_bus_lock);
911 * mei_cl_bus_dev_init - allocate and initializes an mei client devices
917 * Locking: called under "dev->cl_bus_lock" lock
919 static void mei_cl_bus_dev_init(struct mei_device *bus,
920 struct mei_me_client *me_cl)
922 struct mei_cl_device *cldev;
924 WARN_ON(!mutex_is_locked(&bus->cl_bus_lock));
926 dev_dbg(bus->dev, "initializing %pUl", mei_me_cl_uuid(me_cl));
928 if (me_cl->bus_added)
931 cldev = mei_cl_bus_dev_alloc(bus, me_cl);
935 me_cl->bus_added = true;
936 list_add_tail(&cldev->bus_list, &bus->device_list);
941 * mei_cl_bus_rescan - scan me clients list and add create
942 * devices for eligible clients
946 void mei_cl_bus_rescan(struct mei_device *bus)
948 struct mei_cl_device *cldev, *n;
949 struct mei_me_client *me_cl;
951 mutex_lock(&bus->cl_bus_lock);
953 down_read(&bus->me_clients_rwsem);
954 list_for_each_entry(me_cl, &bus->me_clients, list)
955 mei_cl_bus_dev_init(bus, me_cl);
956 up_read(&bus->me_clients_rwsem);
958 list_for_each_entry_safe(cldev, n, &bus->device_list, bus_list) {
960 if (!mei_me_cl_is_active(cldev->me_cl)) {
961 mei_cl_bus_remove_device(cldev);
968 if (mei_cl_bus_dev_setup(bus, cldev))
969 mei_cl_bus_dev_add(cldev);
971 list_del_init(&cldev->bus_list);
972 put_device(&cldev->dev);
975 mutex_unlock(&bus->cl_bus_lock);
977 dev_dbg(bus->dev, "rescan end");
980 void mei_cl_bus_rescan_work(struct work_struct *work)
982 struct mei_device *bus =
983 container_of(work, struct mei_device, bus_rescan_work);
984 struct mei_me_client *me_cl;
986 mutex_lock(&bus->device_lock);
987 me_cl = mei_me_cl_by_uuid(bus, &mei_amthif_guid);
989 mei_amthif_host_init(bus, me_cl);
990 mei_me_cl_put(me_cl);
991 mutex_unlock(&bus->device_lock);
993 mei_cl_bus_rescan(bus);
996 int __mei_cldev_driver_register(struct mei_cl_driver *cldrv,
997 struct module *owner)
1001 cldrv->driver.name = cldrv->name;
1002 cldrv->driver.owner = owner;
1003 cldrv->driver.bus = &mei_cl_bus_type;
1005 err = driver_register(&cldrv->driver);
1009 pr_debug("mei: driver [%s] registered\n", cldrv->driver.name);
1013 EXPORT_SYMBOL_GPL(__mei_cldev_driver_register);
1015 void mei_cldev_driver_unregister(struct mei_cl_driver *cldrv)
1017 driver_unregister(&cldrv->driver);
1019 pr_debug("mei: driver [%s] unregistered\n", cldrv->driver.name);
1021 EXPORT_SYMBOL_GPL(mei_cldev_driver_unregister);
1024 int __init mei_cl_bus_init(void)
1026 return bus_register(&mei_cl_bus_type);
1029 void __exit mei_cl_bus_exit(void)
1031 bus_unregister(&mei_cl_bus_type);