xref: /openbmc/linux/drivers/misc/mei/bus.c (revision 99b7e93c)
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/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>
26 
27 #include "mei_dev.h"
28 #include "client.h"
29 
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)
32 
33 static inline uuid_le uuid_le_cast(const __u8 uuid[16])
34 {
35 	return *(uuid_le *)uuid;
36 }
37 
38 static int mei_cl_device_match(struct device *dev, struct device_driver *drv)
39 {
40 	struct mei_cl_device *device = to_mei_cl_device(dev);
41 	struct mei_cl_driver *driver = to_mei_cl_driver(drv);
42 	const struct mei_cl_device_id *id;
43 	const uuid_le *uuid;
44 	const char *name;
45 
46 	if (!device)
47 		return 0;
48 
49 	uuid = mei_me_cl_uuid(device->me_cl);
50 	name = device->name;
51 
52 	if (!driver || !driver->id_table)
53 		return 0;
54 
55 	id = driver->id_table;
56 
57 	while (uuid_le_cmp(NULL_UUID_LE, uuid_le_cast(id->uuid))) {
58 
59 		if (!uuid_le_cmp(*uuid, uuid_le_cast(id->uuid))) {
60 			if (id->name[0]) {
61 				if (!strncmp(name, id->name, sizeof(id->name)))
62 					return 1;
63 			} else {
64 				return 1;
65 			}
66 		}
67 
68 		id++;
69 	}
70 
71 	return 0;
72 }
73 
74 static int mei_cl_device_probe(struct device *dev)
75 {
76 	struct mei_cl_device *device = to_mei_cl_device(dev);
77 	struct mei_cl_driver *driver;
78 	struct mei_cl_device_id id;
79 
80 	if (!device)
81 		return 0;
82 
83 	driver = to_mei_cl_driver(dev->driver);
84 	if (!driver || !driver->probe)
85 		return -ENODEV;
86 
87 	dev_dbg(dev, "Device probe\n");
88 
89 	strlcpy(id.name, device->name, sizeof(id.name));
90 
91 	return driver->probe(device, &id);
92 }
93 
94 static int mei_cl_device_remove(struct device *dev)
95 {
96 	struct mei_cl_device *device = to_mei_cl_device(dev);
97 	struct mei_cl_driver *driver;
98 
99 	if (!device || !dev->driver)
100 		return 0;
101 
102 	if (device->event_cb) {
103 		device->event_cb = NULL;
104 		cancel_work_sync(&device->event_work);
105 	}
106 
107 	driver = to_mei_cl_driver(dev->driver);
108 	if (!driver->remove) {
109 		dev->driver = NULL;
110 
111 		return 0;
112 	}
113 
114 	return driver->remove(device);
115 }
116 
117 static ssize_t name_show(struct device *dev, struct device_attribute *a,
118 			     char *buf)
119 {
120 	struct mei_cl_device *device = to_mei_cl_device(dev);
121 	size_t len;
122 
123 	len = snprintf(buf, PAGE_SIZE, "%s", device->name);
124 
125 	return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
126 }
127 static DEVICE_ATTR_RO(name);
128 
129 static ssize_t uuid_show(struct device *dev, struct device_attribute *a,
130 			     char *buf)
131 {
132 	struct mei_cl_device *device = to_mei_cl_device(dev);
133 	const uuid_le *uuid = mei_me_cl_uuid(device->me_cl);
134 	size_t len;
135 
136 	len = snprintf(buf, PAGE_SIZE, "%pUl", uuid);
137 
138 	return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
139 }
140 static DEVICE_ATTR_RO(uuid);
141 
142 static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
143 			     char *buf)
144 {
145 	struct mei_cl_device *device = to_mei_cl_device(dev);
146 	const uuid_le *uuid = mei_me_cl_uuid(device->me_cl);
147 	size_t len;
148 
149 	len = snprintf(buf, PAGE_SIZE, "mei:%s:" MEI_CL_UUID_FMT ":",
150 		device->name, MEI_CL_UUID_ARGS(uuid->b));
151 
152 	return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
153 }
154 static DEVICE_ATTR_RO(modalias);
155 
156 static struct attribute *mei_cl_dev_attrs[] = {
157 	&dev_attr_name.attr,
158 	&dev_attr_uuid.attr,
159 	&dev_attr_modalias.attr,
160 	NULL,
161 };
162 ATTRIBUTE_GROUPS(mei_cl_dev);
163 
164 static int mei_cl_uevent(struct device *dev, struct kobj_uevent_env *env)
165 {
166 	struct mei_cl_device *device = to_mei_cl_device(dev);
167 	const uuid_le *uuid = mei_me_cl_uuid(device->me_cl);
168 
169 	if (add_uevent_var(env, "MEI_CL_UUID=%pUl", uuid))
170 		return -ENOMEM;
171 
172 	if (add_uevent_var(env, "MEI_CL_NAME=%s", device->name))
173 		return -ENOMEM;
174 
175 	if (add_uevent_var(env, "MODALIAS=mei:%s:" MEI_CL_UUID_FMT ":",
176 		device->name, MEI_CL_UUID_ARGS(uuid->b)))
177 		return -ENOMEM;
178 
179 	return 0;
180 }
181 
182 static struct bus_type mei_cl_bus_type = {
183 	.name		= "mei",
184 	.dev_groups	= mei_cl_dev_groups,
185 	.match		= mei_cl_device_match,
186 	.probe		= mei_cl_device_probe,
187 	.remove		= mei_cl_device_remove,
188 	.uevent		= mei_cl_uevent,
189 };
190 
191 static void mei_cl_dev_release(struct device *dev)
192 {
193 	struct mei_cl_device *device = to_mei_cl_device(dev);
194 
195 	if (!device)
196 		return;
197 
198 	mei_me_cl_put(device->me_cl);
199 	kfree(device);
200 }
201 
202 static struct device_type mei_cl_device_type = {
203 	.release	= mei_cl_dev_release,
204 };
205 
206 struct mei_cl *mei_cl_bus_find_cl_by_uuid(struct mei_device *dev,
207 					 uuid_le uuid)
208 {
209 	struct mei_cl *cl;
210 
211 	list_for_each_entry(cl, &dev->device_list, device_link) {
212 		if (cl->device && cl->device->me_cl &&
213 		    !uuid_le_cmp(uuid, *mei_me_cl_uuid(cl->device->me_cl)))
214 			return cl;
215 	}
216 
217 	return NULL;
218 }
219 
220 struct mei_cl_device *mei_cl_add_device(struct mei_device *dev,
221 					struct mei_me_client *me_cl,
222 					struct mei_cl *cl,
223 					char *name)
224 {
225 	struct mei_cl_device *device;
226 	int status;
227 
228 	device = kzalloc(sizeof(struct mei_cl_device), GFP_KERNEL);
229 	if (!device)
230 		return NULL;
231 
232 	device->me_cl = mei_me_cl_get(me_cl);
233 	if (!device->me_cl) {
234 		kfree(device);
235 		return NULL;
236 	}
237 
238 	device->cl = cl;
239 	device->dev.parent = dev->dev;
240 	device->dev.bus = &mei_cl_bus_type;
241 	device->dev.type = &mei_cl_device_type;
242 
243 	strlcpy(device->name, name, sizeof(device->name));
244 
245 	dev_set_name(&device->dev, "mei:%s:%pUl", name, mei_me_cl_uuid(me_cl));
246 
247 	status = device_register(&device->dev);
248 	if (status) {
249 		dev_err(dev->dev, "Failed to register MEI device\n");
250 		mei_me_cl_put(device->me_cl);
251 		kfree(device);
252 		return NULL;
253 	}
254 
255 	cl->device = device;
256 
257 	dev_dbg(&device->dev, "client %s registered\n", name);
258 
259 	return device;
260 }
261 EXPORT_SYMBOL_GPL(mei_cl_add_device);
262 
263 void mei_cl_remove_device(struct mei_cl_device *device)
264 {
265 	device_unregister(&device->dev);
266 }
267 EXPORT_SYMBOL_GPL(mei_cl_remove_device);
268 
269 int __mei_cl_driver_register(struct mei_cl_driver *driver, struct module *owner)
270 {
271 	int err;
272 
273 	driver->driver.name = driver->name;
274 	driver->driver.owner = owner;
275 	driver->driver.bus = &mei_cl_bus_type;
276 
277 	err = driver_register(&driver->driver);
278 	if (err)
279 		return err;
280 
281 	pr_debug("mei: driver [%s] registered\n", driver->driver.name);
282 
283 	return 0;
284 }
285 EXPORT_SYMBOL_GPL(__mei_cl_driver_register);
286 
287 void mei_cl_driver_unregister(struct mei_cl_driver *driver)
288 {
289 	driver_unregister(&driver->driver);
290 
291 	pr_debug("mei: driver [%s] unregistered\n", driver->driver.name);
292 }
293 EXPORT_SYMBOL_GPL(mei_cl_driver_unregister);
294 
295 ssize_t __mei_cl_send(struct mei_cl *cl, u8 *buf, size_t length,
296 			bool blocking)
297 {
298 	struct mei_device *dev;
299 	struct mei_cl_cb *cb = NULL;
300 	ssize_t rets;
301 
302 	if (WARN_ON(!cl || !cl->dev))
303 		return -ENODEV;
304 
305 	dev = cl->dev;
306 
307 	mutex_lock(&dev->device_lock);
308 	if (!mei_cl_is_connected(cl)) {
309 		rets = -ENODEV;
310 		goto out;
311 	}
312 
313 	/* Check if we have an ME client device */
314 	if (!mei_me_cl_is_active(cl->me_cl)) {
315 		rets = -ENOTTY;
316 		goto out;
317 	}
318 
319 	if (length > mei_cl_mtu(cl)) {
320 		rets = -EFBIG;
321 		goto out;
322 	}
323 
324 	cb = mei_cl_alloc_cb(cl, length, MEI_FOP_WRITE, NULL);
325 	if (!cb) {
326 		rets = -ENOMEM;
327 		goto out;
328 	}
329 
330 	memcpy(cb->buf.data, buf, length);
331 
332 	rets = mei_cl_write(cl, cb, blocking);
333 
334 out:
335 	mutex_unlock(&dev->device_lock);
336 	if (rets < 0)
337 		mei_io_cb_free(cb);
338 
339 	return rets;
340 }
341 
342 ssize_t __mei_cl_recv(struct mei_cl *cl, u8 *buf, size_t length)
343 {
344 	struct mei_device *dev;
345 	struct mei_cl_cb *cb;
346 	size_t r_length;
347 	ssize_t rets;
348 
349 	if (WARN_ON(!cl || !cl->dev))
350 		return -ENODEV;
351 
352 	dev = cl->dev;
353 
354 	mutex_lock(&dev->device_lock);
355 
356 	cb = mei_cl_read_cb(cl, NULL);
357 	if (cb)
358 		goto copy;
359 
360 	rets = mei_cl_read_start(cl, length, NULL);
361 	if (rets && rets != -EBUSY)
362 		goto out;
363 
364 	if (list_empty(&cl->rd_completed) && !waitqueue_active(&cl->rx_wait)) {
365 
366 		mutex_unlock(&dev->device_lock);
367 
368 		if (wait_event_interruptible(cl->rx_wait,
369 				(!list_empty(&cl->rd_completed)) ||
370 				(!mei_cl_is_connected(cl)))) {
371 
372 			if (signal_pending(current))
373 				return -EINTR;
374 			return -ERESTARTSYS;
375 		}
376 
377 		mutex_lock(&dev->device_lock);
378 
379 		if (!mei_cl_is_connected(cl)) {
380 			rets = -EBUSY;
381 			goto out;
382 		}
383 	}
384 
385 	cb = mei_cl_read_cb(cl, NULL);
386 	if (!cb) {
387 		rets = 0;
388 		goto out;
389 	}
390 
391 copy:
392 	if (cb->status) {
393 		rets = cb->status;
394 		goto free;
395 	}
396 
397 	r_length = min_t(size_t, length, cb->buf_idx);
398 	memcpy(buf, cb->buf.data, r_length);
399 	rets = r_length;
400 
401 free:
402 	mei_io_cb_free(cb);
403 out:
404 	mutex_unlock(&dev->device_lock);
405 
406 	return rets;
407 }
408 
409 ssize_t mei_cl_send(struct mei_cl_device *device, u8 *buf, size_t length)
410 {
411 	struct mei_cl *cl = device->cl;
412 
413 	if (cl == NULL)
414 		return -ENODEV;
415 
416 	return __mei_cl_send(cl, buf, length, 1);
417 }
418 EXPORT_SYMBOL_GPL(mei_cl_send);
419 
420 ssize_t mei_cl_recv(struct mei_cl_device *device, u8 *buf, size_t length)
421 {
422 	struct mei_cl *cl = device->cl;
423 
424 	if (cl == NULL)
425 		return -ENODEV;
426 
427 	return __mei_cl_recv(cl, buf, length);
428 }
429 EXPORT_SYMBOL_GPL(mei_cl_recv);
430 
431 static void mei_bus_event_work(struct work_struct *work)
432 {
433 	struct mei_cl_device *device;
434 
435 	device = container_of(work, struct mei_cl_device, event_work);
436 
437 	if (device->event_cb)
438 		device->event_cb(device, device->events, device->event_context);
439 
440 	device->events = 0;
441 
442 	/* Prepare for the next read */
443 	mei_cl_read_start(device->cl, 0, NULL);
444 }
445 
446 int mei_cl_register_event_cb(struct mei_cl_device *device,
447 			  mei_cl_event_cb_t event_cb, void *context)
448 {
449 	if (device->event_cb)
450 		return -EALREADY;
451 
452 	device->events = 0;
453 	device->event_cb = event_cb;
454 	device->event_context = context;
455 	INIT_WORK(&device->event_work, mei_bus_event_work);
456 
457 	mei_cl_read_start(device->cl, 0, NULL);
458 
459 	return 0;
460 }
461 EXPORT_SYMBOL_GPL(mei_cl_register_event_cb);
462 
463 void *mei_cl_get_drvdata(const struct mei_cl_device *device)
464 {
465 	return dev_get_drvdata(&device->dev);
466 }
467 EXPORT_SYMBOL_GPL(mei_cl_get_drvdata);
468 
469 void mei_cl_set_drvdata(struct mei_cl_device *device, void *data)
470 {
471 	dev_set_drvdata(&device->dev, data);
472 }
473 EXPORT_SYMBOL_GPL(mei_cl_set_drvdata);
474 
475 int mei_cl_enable_device(struct mei_cl_device *device)
476 {
477 	int err;
478 	struct mei_device *dev;
479 	struct mei_cl *cl = device->cl;
480 
481 	if (cl == NULL)
482 		return -ENODEV;
483 
484 	dev = cl->dev;
485 
486 	mutex_lock(&dev->device_lock);
487 
488 	if (mei_cl_is_connected(cl)) {
489 		mutex_unlock(&dev->device_lock);
490 		dev_warn(dev->dev, "Already connected");
491 		return -EBUSY;
492 	}
493 
494 	err = mei_cl_connect(cl, device->me_cl, NULL);
495 	if (err < 0) {
496 		mutex_unlock(&dev->device_lock);
497 		dev_err(dev->dev, "Could not connect to the ME client");
498 
499 		return err;
500 	}
501 
502 	mutex_unlock(&dev->device_lock);
503 
504 	if (device->event_cb)
505 		mei_cl_read_start(device->cl, 0, NULL);
506 
507 	return 0;
508 }
509 EXPORT_SYMBOL_GPL(mei_cl_enable_device);
510 
511 int mei_cl_disable_device(struct mei_cl_device *device)
512 {
513 	int err;
514 	struct mei_device *dev;
515 	struct mei_cl *cl = device->cl;
516 
517 	if (cl == NULL)
518 		return -ENODEV;
519 
520 	dev = cl->dev;
521 
522 	device->event_cb = NULL;
523 
524 	mutex_lock(&dev->device_lock);
525 
526 	if (!mei_cl_is_connected(cl)) {
527 		dev_err(dev->dev, "Already disconnected");
528 		err = 0;
529 		goto out;
530 	}
531 
532 	err = mei_cl_disconnect(cl);
533 	if (err < 0) {
534 		dev_err(dev->dev, "Could not disconnect from the ME client");
535 		goto out;
536 	}
537 
538 	/* Flush queues and remove any pending read */
539 	mei_cl_flush_queues(cl, NULL);
540 
541 out:
542 	mutex_unlock(&dev->device_lock);
543 	return err;
544 
545 }
546 EXPORT_SYMBOL_GPL(mei_cl_disable_device);
547 
548 void mei_cl_bus_rx_event(struct mei_cl *cl)
549 {
550 	struct mei_cl_device *device = cl->device;
551 
552 	if (!device || !device->event_cb)
553 		return;
554 
555 	set_bit(MEI_CL_EVENT_RX, &device->events);
556 
557 	schedule_work(&device->event_work);
558 }
559 
560 void mei_cl_bus_remove_devices(struct mei_device *dev)
561 {
562 	struct mei_cl *cl, *next;
563 
564 	mutex_lock(&dev->device_lock);
565 	list_for_each_entry_safe(cl, next, &dev->device_list, device_link) {
566 		if (cl->device)
567 			mei_cl_remove_device(cl->device);
568 
569 		list_del(&cl->device_link);
570 		mei_cl_unlink(cl);
571 		kfree(cl);
572 	}
573 	mutex_unlock(&dev->device_lock);
574 }
575 
576 int __init mei_cl_bus_init(void)
577 {
578 	return bus_register(&mei_cl_bus_type);
579 }
580 
581 void __exit mei_cl_bus_exit(void)
582 {
583 	bus_unregister(&mei_cl_bus_type);
584 }
585