Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

mei: bus: Initial MEI Client bus type implementation

mei client bus will present some of the mei clients
as devices for other standard subsystems

Implement the probe, remove, match, device addtion routines, along with
the sysfs and uevent ones. mei_cl_device_id is also added to
mod_devicetable.h
A mei-cleint-bus.txt document describing the rationale and the API usage
is also added while ABI/testing/sysfs-bus-mei describeis the modalias ABI.

Signed-off-by: Samuel Ortiz <sameo@linux.intel.com>
Signed-off-by: Tomas Winkler <tomas.winkler@intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Samuel Ortiz and committed by
Greg Kroah-Hartman
e5354107 40e0b67b

+385
+7
Documentation/ABI/testing/sysfs-bus-mei
··· 1 + What: /sys/bus/mei/devices/.../modalias 2 + Date: March 2013 3 + KernelVersion: 3.10 4 + Contact: Samuel Ortiz <sameo@linux.intel.com> 5 + linux-mei@linux.intel.com 6 + Description: Stores the same MODALIAS value emitted by uevent 7 + Format: mei:<mei device name>
+135
Documentation/misc-devices/mei/mei-client-bus.txt
··· 1 + Intel(R) Management Engine (ME) Client bus API 2 + =============================================== 3 + 4 + 5 + Rationale 6 + ========= 7 + MEI misc character device is useful for dedicated applications to send and receive 8 + data to the many FW appliance found in Intel's ME from the user space. 9 + However for some of the ME functionalities it make sense to leverage existing software 10 + stack and expose them through existing kernel subsystems. 11 + 12 + In order to plug seamlessly into the kernel device driver model we add kernel virtual 13 + bus abstraction on top of the MEI driver. This allows implementing linux kernel drivers 14 + for the various MEI features as a stand alone entities found in their respective subsystem. 15 + Existing device drivers can even potentially be re-used by adding an MEI CL bus layer to 16 + the existing code. 17 + 18 + 19 + MEI CL bus API 20 + =========== 21 + A driver implementation for an MEI Client is very similar to existing bus 22 + based device drivers. The driver registers itself as an MEI CL bus driver through 23 + the mei_cl_driver structure: 24 + 25 + struct mei_cl_driver { 26 + struct device_driver driver; 27 + const char *name; 28 + 29 + const struct mei_cl_device_id *id_table; 30 + 31 + int (*probe)(struct mei_cl_device *dev, const struct mei_cl_id *id); 32 + int (*remove)(struct mei_cl_device *dev); 33 + }; 34 + 35 + struct mei_cl_id { 36 + char name[MEI_NAME_SIZE]; 37 + kernel_ulong_t driver_info; 38 + }; 39 + 40 + The mei_cl_id structure allows the driver to bind itself against a device name. 41 + 42 + To actually register a driver on the ME Client bus one must call the mei_cl_add_driver() 43 + API. This is typically called at module init time. 44 + 45 + Once registered on the ME Client bus, a driver will typically try to do some I/O on 46 + this bus and this should be done through the mei_cl_send() and mei_cl_recv() 47 + routines. The latter is synchronous (blocks and sleeps until data shows up). 48 + In order for drivers to be notified of pending events waiting for them (e.g. 49 + an Rx event) they can register an event handler through the 50 + mei_cl_register_event_cb() routine. Currently only the MEI_EVENT_RX event 51 + will trigger an event handler call and the driver implementation is supposed 52 + to call mei_recv() from the event handler in order to fetch the pending 53 + received buffers. 54 + 55 + 56 + Example 57 + ======= 58 + As a theoretical example let's pretend the ME comes with a "contact" NFC IP. 59 + The driver init and exit routines for this device would look like: 60 + 61 + #define CONTACT_DRIVER_NAME "contact" 62 + 63 + static struct mei_cl_device_id contact_mei_cl_tbl[] = { 64 + { CONTACT_DRIVER_NAME, }, 65 + 66 + /* required last entry */ 67 + { } 68 + }; 69 + MODULE_DEVICE_TABLE(mei_cl, contact_mei_cl_tbl); 70 + 71 + static struct mei_cl_driver contact_driver = { 72 + .id_table = contact_mei_tbl, 73 + .name = CONTACT_DRIVER_NAME, 74 + 75 + .probe = contact_probe, 76 + .remove = contact_remove, 77 + }; 78 + 79 + static int contact_init(void) 80 + { 81 + int r; 82 + 83 + r = mei_cl_driver_register(&contact_driver); 84 + if (r) { 85 + pr_err(CONTACT_DRIVER_NAME ": driver registration failed\n"); 86 + return r; 87 + } 88 + 89 + return 0; 90 + } 91 + 92 + static void __exit contact_exit(void) 93 + { 94 + mei_cl_driver_unregister(&contact_driver); 95 + } 96 + 97 + module_init(contact_init); 98 + module_exit(contact_exit); 99 + 100 + And the driver's simplified probe routine would look like that: 101 + 102 + int contact_probe(struct mei_cl_device *dev, struct mei_cl_device_id *id) 103 + { 104 + struct contact_driver *contact; 105 + 106 + [...] 107 + mei_cl_register_event_cb(dev, contact_event_cb, contact); 108 + 109 + return 0; 110 + } 111 + 112 + In the probe routine the driver basically registers an ME bus event handler 113 + which is as close as it can get to registering a threaded IRQ handler. 114 + The handler implementation will typically call some I/O routine depending on 115 + the pending events: 116 + 117 + #define MAX_NFC_PAYLOAD 128 118 + 119 + static void contact_event_cb(struct mei_cl_device *dev, u32 events, 120 + void *context) 121 + { 122 + struct contact_driver *contact = context; 123 + 124 + if (events & BIT(MEI_EVENT_RX)) { 125 + u8 payload[MAX_NFC_PAYLOAD]; 126 + int payload_size; 127 + 128 + payload_size = mei_recv(dev, payload, MAX_NFC_PAYLOAD); 129 + if (payload_size <= 0) 130 + return; 131 + 132 + /* Hook to the NFC subsystem */ 133 + nfc_hci_recv_frame(contact->hdev, payload, payload_size); 134 + } 135 + }
+1
drivers/misc/mei/Makefile
··· 10 10 mei-objs += main.o 11 11 mei-objs += amthif.o 12 12 mei-objs += wd.o 13 + mei-objs += bus.o 13 14 14 15 obj-$(CONFIG_INTEL_MEI_ME) += mei-me.o 15 16 mei-me-objs := pci-me.o
+172
drivers/misc/mei/bus.c
··· 1 + /* 2 + * Intel Management Engine Interface (Intel MEI) Linux driver 3 + * Copyright (c) 2012-2013, Intel Corporation. 4 + * 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. 8 + * 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 12 + * more details. 13 + * 14 + */ 15 + 16 + #include <linux/module.h> 17 + #include <linux/device.h> 18 + #include <linux/kernel.h> 19 + #include <linux/init.h> 20 + #include <linux/errno.h> 21 + #include <linux/slab.h> 22 + #include <linux/mutex.h> 23 + #include <linux/interrupt.h> 24 + #include <linux/pci.h> 25 + #include <linux/mei_cl_bus.h> 26 + 27 + #include "mei_dev.h" 28 + 29 + #define to_mei_cl_driver(d) container_of(d, struct mei_cl_driver, driver) 30 + #define to_mei_cl_device(d) container_of(d, struct mei_cl_device, dev) 31 + 32 + static int mei_cl_device_match(struct device *dev, struct device_driver *drv) 33 + { 34 + struct mei_cl_device *device = to_mei_cl_device(dev); 35 + struct mei_cl_driver *driver = to_mei_cl_driver(drv); 36 + const struct mei_cl_device_id *id; 37 + 38 + if (!device) 39 + return 0; 40 + 41 + if (!driver || !driver->id_table) 42 + return 0; 43 + 44 + id = driver->id_table; 45 + 46 + while (id->name[0]) { 47 + if (!strcmp(dev_name(dev), id->name)) 48 + return 1; 49 + 50 + id++; 51 + } 52 + 53 + return 0; 54 + } 55 + 56 + static int mei_cl_device_probe(struct device *dev) 57 + { 58 + struct mei_cl_device *device = to_mei_cl_device(dev); 59 + struct mei_cl_driver *driver; 60 + struct mei_cl_device_id id; 61 + 62 + if (!device) 63 + return 0; 64 + 65 + driver = to_mei_cl_driver(dev->driver); 66 + if (!driver || !driver->probe) 67 + return -ENODEV; 68 + 69 + dev_dbg(dev, "Device probe\n"); 70 + 71 + strncpy(id.name, dev_name(dev), MEI_CL_NAME_SIZE); 72 + 73 + return driver->probe(device, &id); 74 + } 75 + 76 + static int mei_cl_device_remove(struct device *dev) 77 + { 78 + struct mei_cl_device *device = to_mei_cl_device(dev); 79 + struct mei_cl_driver *driver; 80 + 81 + if (!device || !dev->driver) 82 + return 0; 83 + 84 + driver = to_mei_cl_driver(dev->driver); 85 + if (!driver->remove) { 86 + dev->driver = NULL; 87 + 88 + return 0; 89 + } 90 + 91 + return driver->remove(device); 92 + } 93 + 94 + static ssize_t modalias_show(struct device *dev, struct device_attribute *a, 95 + char *buf) 96 + { 97 + int len; 98 + 99 + len = snprintf(buf, PAGE_SIZE, "mei:%s\n", dev_name(dev)); 100 + 101 + return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len; 102 + } 103 + 104 + static struct device_attribute mei_cl_dev_attrs[] = { 105 + __ATTR_RO(modalias), 106 + __ATTR_NULL, 107 + }; 108 + 109 + static int mei_cl_uevent(struct device *dev, struct kobj_uevent_env *env) 110 + { 111 + if (add_uevent_var(env, "MODALIAS=mei:%s", dev_name(dev))) 112 + return -ENOMEM; 113 + 114 + return 0; 115 + } 116 + 117 + static struct bus_type mei_cl_bus_type = { 118 + .name = "mei", 119 + .dev_attrs = mei_cl_dev_attrs, 120 + .match = mei_cl_device_match, 121 + .probe = mei_cl_device_probe, 122 + .remove = mei_cl_device_remove, 123 + .uevent = mei_cl_uevent, 124 + }; 125 + 126 + static void mei_cl_dev_release(struct device *dev) 127 + { 128 + kfree(to_mei_cl_device(dev)); 129 + } 130 + 131 + static struct device_type mei_cl_device_type = { 132 + .release = mei_cl_dev_release, 133 + }; 134 + 135 + struct mei_cl_device *mei_cl_add_device(struct mei_device *mei_device, 136 + uuid_le uuid, char *name) 137 + { 138 + struct mei_cl_device *device; 139 + int status; 140 + 141 + device = kzalloc(sizeof(struct mei_cl_device), GFP_KERNEL); 142 + if (!device) 143 + return NULL; 144 + 145 + device->dev.parent = &mei_device->pdev->dev; 146 + device->dev.bus = &mei_cl_bus_type; 147 + device->dev.type = &mei_cl_device_type; 148 + 149 + dev_set_name(&device->dev, "%s", name); 150 + 151 + status = device_register(&device->dev); 152 + if (status) 153 + goto out_err; 154 + 155 + dev_dbg(&device->dev, "client %s registered\n", name); 156 + 157 + return device; 158 + 159 + out_err: 160 + dev_err(device->dev.parent, "Failed to register MEI client\n"); 161 + 162 + kfree(device); 163 + 164 + return NULL; 165 + } 166 + EXPORT_SYMBOL_GPL(mei_cl_add_device); 167 + 168 + void mei_cl_remove_device(struct mei_cl_device *device) 169 + { 170 + device_unregister(&device->dev); 171 + } 172 + EXPORT_SYMBOL_GPL(mei_cl_remove_device);
+26
drivers/misc/mei/mei_dev.h
··· 21 21 #include <linux/watchdog.h> 22 22 #include <linux/poll.h> 23 23 #include <linux/mei.h> 24 + #include <linux/mei_cl_bus.h> 24 25 25 26 #include "hw.h" 26 27 #include "hw-me-regs.h" ··· 261 260 u32 (*read_hdr)(const struct mei_device *dev); 262 261 int (*read) (struct mei_device *dev, 263 262 unsigned char *buf, unsigned long len); 263 + }; 264 + 265 + /* MEI bus API*/ 266 + struct mei_cl_device *mei_cl_add_device(struct mei_device *dev, 267 + uuid_le uuid, char *name); 268 + void mei_cl_remove_device(struct mei_cl_device *device); 269 + 270 + /** 271 + * struct mei_cl_device - MEI device handle 272 + * An mei_cl_device pointer is returned from mei_add_device() 273 + * and links MEI bus clients to their actual ME host client pointer. 274 + * Drivers for MEI devices will get an mei_cl_device pointer 275 + * when being probed and shall use it for doing ME bus I/O. 276 + * 277 + * @dev: linux driver model device pointer 278 + * @uuid: me client uuid 279 + * @cl: mei client 280 + * @priv_data: client private data 281 + */ 282 + struct mei_cl_device { 283 + struct device dev; 284 + 285 + struct mei_cl *cl; 286 + 287 + void *priv_data; 264 288 }; 265 289 266 290 /**
+20
include/linux/mei_cl_bus.h
··· 1 + #ifndef _LINUX_MEI_CL_BUS_H 2 + #define _LINUX_MEI_CL_BUS_H 3 + 4 + #include <linux/device.h> 5 + #include <linux/uuid.h> 6 + 7 + struct mei_cl_device; 8 + 9 + struct mei_cl_driver { 10 + struct device_driver driver; 11 + const char *name; 12 + 13 + const struct mei_cl_device_id *id_table; 14 + 15 + int (*probe)(struct mei_cl_device *dev, 16 + const struct mei_cl_device_id *id); 17 + int (*remove)(struct mei_cl_device *dev); 18 + }; 19 + 20 + #endif /* _LINUX_MEI_CL_BUS_H */
+9
include/linux/mod_devicetable.h
··· 9 9 10 10 #ifdef __KERNEL__ 11 11 #include <linux/types.h> 12 + #include <linux/uuid.h> 12 13 typedef unsigned long kernel_ulong_t; 13 14 #endif 14 15 ··· 567 566 __u8 format; /* Format version or IPACK_ANY_ID */ 568 567 __u32 vendor; /* Vendor ID or IPACK_ANY_ID */ 569 568 __u32 device; /* Device ID or IPACK_ANY_ID */ 569 + }; 570 + 571 + #define MEI_CL_MODULE_PREFIX "mei:" 572 + #define MEI_CL_NAME_SIZE 32 573 + 574 + struct mei_cl_device_id { 575 + char name[MEI_CL_NAME_SIZE]; 576 + kernel_ulong_t driver_info; 570 577 }; 571 578 572 579 #endif /* LINUX_MOD_DEVICETABLE_H */
+3
scripts/mod/devicetable-offsets.c
··· 174 174 DEVID_FIELD(x86_cpu_id, model); 175 175 DEVID_FIELD(x86_cpu_id, vendor); 176 176 177 + DEVID(mei_cl_device_id); 178 + DEVID_FIELD(mei_cl_device_id, name); 179 + 177 180 return 0; 178 181 }
+12
scripts/mod/file2alias.c
··· 1133 1133 } 1134 1134 ADD_TO_DEVTABLE("x86cpu", x86_cpu_id, do_x86cpu_entry); 1135 1135 1136 + /* Looks like: mei:S */ 1137 + static int do_mei_entry(const char *filename, void *symval, 1138 + char *alias) 1139 + { 1140 + DEF_FIELD_ADDR(symval, mei_cl_device_id, name); 1141 + 1142 + sprintf(alias, MEI_CL_MODULE_PREFIX "%s", *name); 1143 + 1144 + return 1; 1145 + } 1146 + ADD_TO_DEVTABLE("mei", mei_cl_device_id, do_mei_entry); 1147 + 1136 1148 /* Does namelen bytes of name exactly match the symbol? */ 1137 1149 static bool sym_is(const char *name, unsigned namelen, const char *symbol) 1138 1150 {