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