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