xref: /openbmc/linux/drivers/misc/mei/bus.c (revision 7e6f7d24)
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/signal.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 /**
34  * __mei_cl_send - internal client send (write)
35  *
36  * @cl: host client
37  * @buf: buffer to send
38  * @length: buffer length
39  * @mode: sending mode
40  *
41  * Return: written size bytes or < 0 on error
42  */
43 ssize_t __mei_cl_send(struct mei_cl *cl, u8 *buf, size_t length,
44 		      unsigned int mode)
45 {
46 	struct mei_device *bus;
47 	struct mei_cl_cb *cb;
48 	ssize_t rets;
49 
50 	if (WARN_ON(!cl || !cl->dev))
51 		return -ENODEV;
52 
53 	bus = cl->dev;
54 
55 	mutex_lock(&bus->device_lock);
56 	if (bus->dev_state != MEI_DEV_ENABLED) {
57 		rets = -ENODEV;
58 		goto out;
59 	}
60 
61 	if (!mei_cl_is_connected(cl)) {
62 		rets = -ENODEV;
63 		goto out;
64 	}
65 
66 	/* Check if we have an ME client device */
67 	if (!mei_me_cl_is_active(cl->me_cl)) {
68 		rets = -ENOTTY;
69 		goto out;
70 	}
71 
72 	if (length > mei_cl_mtu(cl)) {
73 		rets = -EFBIG;
74 		goto out;
75 	}
76 
77 	while (cl->tx_cb_queued >= bus->tx_queue_limit) {
78 		mutex_unlock(&bus->device_lock);
79 		rets = wait_event_interruptible(cl->tx_wait,
80 				cl->writing_state == MEI_WRITE_COMPLETE ||
81 				(!mei_cl_is_connected(cl)));
82 		mutex_lock(&bus->device_lock);
83 		if (rets) {
84 			if (signal_pending(current))
85 				rets = -EINTR;
86 			goto out;
87 		}
88 		if (!mei_cl_is_connected(cl)) {
89 			rets = -ENODEV;
90 			goto out;
91 		}
92 	}
93 
94 	cb = mei_cl_alloc_cb(cl, length, MEI_FOP_WRITE, NULL);
95 	if (!cb) {
96 		rets = -ENOMEM;
97 		goto out;
98 	}
99 
100 	cb->internal = !!(mode & MEI_CL_IO_TX_INTERNAL);
101 	cb->blocking = !!(mode & MEI_CL_IO_TX_BLOCKING);
102 	memcpy(cb->buf.data, buf, length);
103 
104 	rets = mei_cl_write(cl, cb);
105 
106 out:
107 	mutex_unlock(&bus->device_lock);
108 
109 	return rets;
110 }
111 
112 /**
113  * __mei_cl_recv - internal client receive (read)
114  *
115  * @cl: host client
116  * @buf: buffer to receive
117  * @length: buffer length
118  * @mode: io mode
119  * @timeout: recv timeout, 0 for infinite timeout
120  *
121  * Return: read size in bytes of < 0 on error
122  */
123 ssize_t __mei_cl_recv(struct mei_cl *cl, u8 *buf, size_t length,
124 		      unsigned int mode, unsigned long timeout)
125 {
126 	struct mei_device *bus;
127 	struct mei_cl_cb *cb;
128 	size_t r_length;
129 	ssize_t rets;
130 	bool nonblock = !!(mode & MEI_CL_IO_RX_NONBLOCK);
131 
132 	if (WARN_ON(!cl || !cl->dev))
133 		return -ENODEV;
134 
135 	bus = cl->dev;
136 
137 	mutex_lock(&bus->device_lock);
138 	if (bus->dev_state != MEI_DEV_ENABLED) {
139 		rets = -ENODEV;
140 		goto out;
141 	}
142 
143 	cb = mei_cl_read_cb(cl, NULL);
144 	if (cb)
145 		goto copy;
146 
147 	rets = mei_cl_read_start(cl, length, NULL);
148 	if (rets && rets != -EBUSY)
149 		goto out;
150 
151 	if (nonblock) {
152 		rets = -EAGAIN;
153 		goto out;
154 	}
155 
156 	/* wait on event only if there is no other waiter */
157 	/* synchronized under device mutex */
158 	if (!waitqueue_active(&cl->rx_wait)) {
159 
160 		mutex_unlock(&bus->device_lock);
161 
162 		if (timeout) {
163 			rets = wait_event_interruptible_timeout
164 					(cl->rx_wait,
165 					(!list_empty(&cl->rd_completed)) ||
166 					(!mei_cl_is_connected(cl)),
167 					msecs_to_jiffies(timeout));
168 			if (rets == 0)
169 				return -ETIME;
170 			if (rets < 0) {
171 				if (signal_pending(current))
172 					return -EINTR;
173 				return -ERESTARTSYS;
174 			}
175 		} else {
176 			if (wait_event_interruptible
177 					(cl->rx_wait,
178 					(!list_empty(&cl->rd_completed)) ||
179 					(!mei_cl_is_connected(cl)))) {
180 				if (signal_pending(current))
181 					return -EINTR;
182 				return -ERESTARTSYS;
183 			}
184 		}
185 
186 		mutex_lock(&bus->device_lock);
187 
188 		if (!mei_cl_is_connected(cl)) {
189 			rets = -ENODEV;
190 			goto out;
191 		}
192 	}
193 
194 	cb = mei_cl_read_cb(cl, NULL);
195 	if (!cb) {
196 		rets = 0;
197 		goto out;
198 	}
199 
200 copy:
201 	if (cb->status) {
202 		rets = cb->status;
203 		goto free;
204 	}
205 
206 	r_length = min_t(size_t, length, cb->buf_idx);
207 	memcpy(buf, cb->buf.data, r_length);
208 	rets = r_length;
209 
210 free:
211 	mei_io_cb_free(cb);
212 out:
213 	mutex_unlock(&bus->device_lock);
214 
215 	return rets;
216 }
217 
218 /**
219  * mei_cldev_send - me device send  (write)
220  *
221  * @cldev: me client device
222  * @buf: buffer to send
223  * @length: buffer length
224  *
225  * Return: written size in bytes or < 0 on error
226  */
227 ssize_t mei_cldev_send(struct mei_cl_device *cldev, u8 *buf, size_t length)
228 {
229 	struct mei_cl *cl = cldev->cl;
230 
231 	return __mei_cl_send(cl, buf, length, MEI_CL_IO_TX_BLOCKING);
232 }
233 EXPORT_SYMBOL_GPL(mei_cldev_send);
234 
235 /**
236  * mei_cldev_recv_nonblock - non block client receive (read)
237  *
238  * @cldev: me client device
239  * @buf: buffer to receive
240  * @length: buffer length
241  *
242  * Return: read size in bytes of < 0 on error
243  *         -EAGAIN if function will block.
244  */
245 ssize_t mei_cldev_recv_nonblock(struct mei_cl_device *cldev, u8 *buf,
246 				size_t length)
247 {
248 	struct mei_cl *cl = cldev->cl;
249 
250 	return __mei_cl_recv(cl, buf, length, MEI_CL_IO_RX_NONBLOCK, 0);
251 }
252 EXPORT_SYMBOL_GPL(mei_cldev_recv_nonblock);
253 
254 /**
255  * mei_cldev_recv - client receive (read)
256  *
257  * @cldev: me client device
258  * @buf: buffer to receive
259  * @length: buffer length
260  *
261  * Return: read size in bytes of < 0 on error
262  */
263 ssize_t mei_cldev_recv(struct mei_cl_device *cldev, u8 *buf, size_t length)
264 {
265 	struct mei_cl *cl = cldev->cl;
266 
267 	return __mei_cl_recv(cl, buf, length, 0, 0);
268 }
269 EXPORT_SYMBOL_GPL(mei_cldev_recv);
270 
271 /**
272  * mei_cl_bus_rx_work - dispatch rx event for a bus device
273  *
274  * @work: work
275  */
276 static void mei_cl_bus_rx_work(struct work_struct *work)
277 {
278 	struct mei_cl_device *cldev;
279 	struct mei_device *bus;
280 
281 	cldev = container_of(work, struct mei_cl_device, rx_work);
282 
283 	bus = cldev->bus;
284 
285 	if (cldev->rx_cb)
286 		cldev->rx_cb(cldev);
287 
288 	mutex_lock(&bus->device_lock);
289 	mei_cl_read_start(cldev->cl, mei_cl_mtu(cldev->cl), NULL);
290 	mutex_unlock(&bus->device_lock);
291 }
292 
293 /**
294  * mei_cl_bus_notif_work - dispatch FW notif event for a bus device
295  *
296  * @work: work
297  */
298 static void mei_cl_bus_notif_work(struct work_struct *work)
299 {
300 	struct mei_cl_device *cldev;
301 
302 	cldev = container_of(work, struct mei_cl_device, notif_work);
303 
304 	if (cldev->notif_cb)
305 		cldev->notif_cb(cldev);
306 }
307 
308 /**
309  * mei_cl_bus_notify_event - schedule notify cb on bus client
310  *
311  * @cl: host client
312  *
313  * Return: true if event was scheduled
314  *         false if the client is not waiting for event
315  */
316 bool mei_cl_bus_notify_event(struct mei_cl *cl)
317 {
318 	struct mei_cl_device *cldev = cl->cldev;
319 
320 	if (!cldev || !cldev->notif_cb)
321 		return false;
322 
323 	if (!cl->notify_ev)
324 		return false;
325 
326 	schedule_work(&cldev->notif_work);
327 
328 	cl->notify_ev = false;
329 
330 	return true;
331 }
332 
333 /**
334  * mei_cl_bus_rx_event - schedule rx event
335  *
336  * @cl: host client
337  *
338  * Return: true if event was scheduled
339  *         false if the client is not waiting for event
340  */
341 bool mei_cl_bus_rx_event(struct mei_cl *cl)
342 {
343 	struct mei_cl_device *cldev = cl->cldev;
344 
345 	if (!cldev || !cldev->rx_cb)
346 		return false;
347 
348 	schedule_work(&cldev->rx_work);
349 
350 	return true;
351 }
352 
353 /**
354  * mei_cldev_register_rx_cb - register Rx event callback
355  *
356  * @cldev: me client devices
357  * @rx_cb: callback function
358  *
359  * Return: 0 on success
360  *         -EALREADY if an callback is already registered
361  *         <0 on other errors
362  */
363 int mei_cldev_register_rx_cb(struct mei_cl_device *cldev, mei_cldev_cb_t rx_cb)
364 {
365 	struct mei_device *bus = cldev->bus;
366 	int ret;
367 
368 	if (!rx_cb)
369 		return -EINVAL;
370 	if (cldev->rx_cb)
371 		return -EALREADY;
372 
373 	cldev->rx_cb = rx_cb;
374 	INIT_WORK(&cldev->rx_work, mei_cl_bus_rx_work);
375 
376 	mutex_lock(&bus->device_lock);
377 	ret = mei_cl_read_start(cldev->cl, mei_cl_mtu(cldev->cl), NULL);
378 	mutex_unlock(&bus->device_lock);
379 	if (ret && ret != -EBUSY)
380 		return ret;
381 
382 	return 0;
383 }
384 EXPORT_SYMBOL_GPL(mei_cldev_register_rx_cb);
385 
386 /**
387  * mei_cldev_register_notif_cb - register FW notification event callback
388  *
389  * @cldev: me client devices
390  * @notif_cb: callback function
391  *
392  * Return: 0 on success
393  *         -EALREADY if an callback is already registered
394  *         <0 on other errors
395  */
396 int mei_cldev_register_notif_cb(struct mei_cl_device *cldev,
397 				mei_cldev_cb_t notif_cb)
398 {
399 	struct mei_device *bus = cldev->bus;
400 	int ret;
401 
402 	if (!notif_cb)
403 		return -EINVAL;
404 
405 	if (cldev->notif_cb)
406 		return -EALREADY;
407 
408 	cldev->notif_cb = notif_cb;
409 	INIT_WORK(&cldev->notif_work, mei_cl_bus_notif_work);
410 
411 	mutex_lock(&bus->device_lock);
412 	ret = mei_cl_notify_request(cldev->cl, NULL, 1);
413 	mutex_unlock(&bus->device_lock);
414 	if (ret)
415 		return ret;
416 
417 	return 0;
418 }
419 EXPORT_SYMBOL_GPL(mei_cldev_register_notif_cb);
420 
421 /**
422  * mei_cldev_get_drvdata - driver data getter
423  *
424  * @cldev: mei client device
425  *
426  * Return: driver private data
427  */
428 void *mei_cldev_get_drvdata(const struct mei_cl_device *cldev)
429 {
430 	return dev_get_drvdata(&cldev->dev);
431 }
432 EXPORT_SYMBOL_GPL(mei_cldev_get_drvdata);
433 
434 /**
435  * mei_cldev_set_drvdata - driver data setter
436  *
437  * @cldev: mei client device
438  * @data: data to store
439  */
440 void mei_cldev_set_drvdata(struct mei_cl_device *cldev, void *data)
441 {
442 	dev_set_drvdata(&cldev->dev, data);
443 }
444 EXPORT_SYMBOL_GPL(mei_cldev_set_drvdata);
445 
446 /**
447  * mei_cldev_uuid - return uuid of the underlying me client
448  *
449  * @cldev: mei client device
450  *
451  * Return: me client uuid
452  */
453 const uuid_le *mei_cldev_uuid(const struct mei_cl_device *cldev)
454 {
455 	return mei_me_cl_uuid(cldev->me_cl);
456 }
457 EXPORT_SYMBOL_GPL(mei_cldev_uuid);
458 
459 /**
460  * mei_cldev_ver - return protocol version of the underlying me client
461  *
462  * @cldev: mei client device
463  *
464  * Return: me client protocol version
465  */
466 u8 mei_cldev_ver(const struct mei_cl_device *cldev)
467 {
468 	return mei_me_cl_ver(cldev->me_cl);
469 }
470 EXPORT_SYMBOL_GPL(mei_cldev_ver);
471 
472 /**
473  * mei_cldev_enabled - check whether the device is enabled
474  *
475  * @cldev: mei client device
476  *
477  * Return: true if me client is initialized and connected
478  */
479 bool mei_cldev_enabled(struct mei_cl_device *cldev)
480 {
481 	return mei_cl_is_connected(cldev->cl);
482 }
483 EXPORT_SYMBOL_GPL(mei_cldev_enabled);
484 
485 /**
486  * mei_cl_bus_module_get - acquire module of the underlying
487  *    hw driver.
488  *
489  * @cldev: mei client device
490  *
491  * Return: true on success; false if the module was removed.
492  */
493 static bool mei_cl_bus_module_get(struct mei_cl_device *cldev)
494 {
495 	return try_module_get(cldev->bus->dev->driver->owner);
496 }
497 
498 /**
499  * mei_cl_bus_module_put -  release the underlying hw module.
500  *
501  * @cldev: mei client device
502  */
503 static void mei_cl_bus_module_put(struct mei_cl_device *cldev)
504 {
505 	module_put(cldev->bus->dev->driver->owner);
506 }
507 
508 /**
509  * mei_cldev_enable - enable me client device
510  *     create connection with me client
511  *
512  * @cldev: me client device
513  *
514  * Return: 0 on success and < 0 on error
515  */
516 int mei_cldev_enable(struct mei_cl_device *cldev)
517 {
518 	struct mei_device *bus = cldev->bus;
519 	struct mei_cl *cl;
520 	int ret;
521 
522 	cl = cldev->cl;
523 
524 	if (cl->state == MEI_FILE_UNINITIALIZED) {
525 		mutex_lock(&bus->device_lock);
526 		ret = mei_cl_link(cl);
527 		mutex_unlock(&bus->device_lock);
528 		if (ret)
529 			return ret;
530 		/* update pointers */
531 		cl->cldev = cldev;
532 	}
533 
534 	mutex_lock(&bus->device_lock);
535 	if (mei_cl_is_connected(cl)) {
536 		ret = 0;
537 		goto out;
538 	}
539 
540 	if (!mei_me_cl_is_active(cldev->me_cl)) {
541 		dev_err(&cldev->dev, "me client is not active\n");
542 		ret = -ENOTTY;
543 		goto out;
544 	}
545 
546 	if (!mei_cl_bus_module_get(cldev)) {
547 		dev_err(&cldev->dev, "get hw module failed");
548 		ret = -ENODEV;
549 		goto out;
550 	}
551 
552 	ret = mei_cl_connect(cl, cldev->me_cl, NULL);
553 	if (ret < 0) {
554 		dev_err(&cldev->dev, "cannot connect\n");
555 		mei_cl_bus_module_put(cldev);
556 	}
557 
558 out:
559 	mutex_unlock(&bus->device_lock);
560 
561 	return ret;
562 }
563 EXPORT_SYMBOL_GPL(mei_cldev_enable);
564 
565 /**
566  * mei_cldev_unregister_callbacks - internal wrapper for unregistering
567  *  callbacks.
568  *
569  * @cldev: client device
570  */
571 static void mei_cldev_unregister_callbacks(struct mei_cl_device *cldev)
572 {
573 	if (cldev->rx_cb) {
574 		cancel_work_sync(&cldev->rx_work);
575 		cldev->rx_cb = NULL;
576 	}
577 
578 	if (cldev->notif_cb) {
579 		cancel_work_sync(&cldev->notif_work);
580 		cldev->notif_cb = NULL;
581 	}
582 }
583 
584 /**
585  * mei_cldev_disable - disable me client device
586  *     disconnect form the me client
587  *
588  * @cldev: me client device
589  *
590  * Return: 0 on success and < 0 on error
591  */
592 int mei_cldev_disable(struct mei_cl_device *cldev)
593 {
594 	struct mei_device *bus;
595 	struct mei_cl *cl;
596 	int err;
597 
598 	if (!cldev)
599 		return -ENODEV;
600 
601 	cl = cldev->cl;
602 
603 	bus = cldev->bus;
604 
605 	mei_cldev_unregister_callbacks(cldev);
606 
607 	mutex_lock(&bus->device_lock);
608 
609 	if (!mei_cl_is_connected(cl)) {
610 		dev_dbg(bus->dev, "Already disconnected\n");
611 		err = 0;
612 		goto out;
613 	}
614 
615 	err = mei_cl_disconnect(cl);
616 	if (err < 0)
617 		dev_err(bus->dev, "Could not disconnect from the ME client\n");
618 
619 out:
620 	mei_cl_bus_module_put(cldev);
621 
622 	/* Flush queues and remove any pending read */
623 	mei_cl_flush_queues(cl, NULL);
624 	mei_cl_unlink(cl);
625 
626 	mutex_unlock(&bus->device_lock);
627 	return err;
628 }
629 EXPORT_SYMBOL_GPL(mei_cldev_disable);
630 
631 /**
632  * mei_cl_device_find - find matching entry in the driver id table
633  *
634  * @cldev: me client device
635  * @cldrv: me client driver
636  *
637  * Return: id on success; NULL if no id is matching
638  */
639 static const
640 struct mei_cl_device_id *mei_cl_device_find(struct mei_cl_device *cldev,
641 					    struct mei_cl_driver *cldrv)
642 {
643 	const struct mei_cl_device_id *id;
644 	const uuid_le *uuid;
645 	u8 version;
646 	bool match;
647 
648 	uuid = mei_me_cl_uuid(cldev->me_cl);
649 	version = mei_me_cl_ver(cldev->me_cl);
650 
651 	id = cldrv->id_table;
652 	while (uuid_le_cmp(NULL_UUID_LE, id->uuid)) {
653 		if (!uuid_le_cmp(*uuid, id->uuid)) {
654 			match = true;
655 
656 			if (cldev->name[0])
657 				if (strncmp(cldev->name, id->name,
658 					    sizeof(id->name)))
659 					match = false;
660 
661 			if (id->version != MEI_CL_VERSION_ANY)
662 				if (id->version != version)
663 					match = false;
664 			if (match)
665 				return id;
666 		}
667 
668 		id++;
669 	}
670 
671 	return NULL;
672 }
673 
674 /**
675  * mei_cl_device_match  - device match function
676  *
677  * @dev: device
678  * @drv: driver
679  *
680  * Return:  1 if matching device was found 0 otherwise
681  */
682 static int mei_cl_device_match(struct device *dev, struct device_driver *drv)
683 {
684 	struct mei_cl_device *cldev = to_mei_cl_device(dev);
685 	struct mei_cl_driver *cldrv = to_mei_cl_driver(drv);
686 	const struct mei_cl_device_id *found_id;
687 
688 	if (!cldev)
689 		return 0;
690 
691 	if (!cldev->do_match)
692 		return 0;
693 
694 	if (!cldrv || !cldrv->id_table)
695 		return 0;
696 
697 	found_id = mei_cl_device_find(cldev, cldrv);
698 	if (found_id)
699 		return 1;
700 
701 	return 0;
702 }
703 
704 /**
705  * mei_cl_device_probe - bus probe function
706  *
707  * @dev: device
708  *
709  * Return:  0 on success; < 0 otherwise
710  */
711 static int mei_cl_device_probe(struct device *dev)
712 {
713 	struct mei_cl_device *cldev;
714 	struct mei_cl_driver *cldrv;
715 	const struct mei_cl_device_id *id;
716 	int ret;
717 
718 	cldev = to_mei_cl_device(dev);
719 	cldrv = to_mei_cl_driver(dev->driver);
720 
721 	if (!cldev)
722 		return 0;
723 
724 	if (!cldrv || !cldrv->probe)
725 		return -ENODEV;
726 
727 	id = mei_cl_device_find(cldev, cldrv);
728 	if (!id)
729 		return -ENODEV;
730 
731 	ret = cldrv->probe(cldev, id);
732 	if (ret)
733 		return ret;
734 
735 	__module_get(THIS_MODULE);
736 	return 0;
737 }
738 
739 /**
740  * mei_cl_device_remove - remove device from the bus
741  *
742  * @dev: device
743  *
744  * Return:  0 on success; < 0 otherwise
745  */
746 static int mei_cl_device_remove(struct device *dev)
747 {
748 	struct mei_cl_device *cldev = to_mei_cl_device(dev);
749 	struct mei_cl_driver *cldrv;
750 	int ret = 0;
751 
752 	if (!cldev || !dev->driver)
753 		return 0;
754 
755 	cldrv = to_mei_cl_driver(dev->driver);
756 	if (cldrv->remove)
757 		ret = cldrv->remove(cldev);
758 
759 	mei_cldev_unregister_callbacks(cldev);
760 
761 	module_put(THIS_MODULE);
762 	dev->driver = NULL;
763 	return ret;
764 
765 }
766 
767 static ssize_t name_show(struct device *dev, struct device_attribute *a,
768 			     char *buf)
769 {
770 	struct mei_cl_device *cldev = to_mei_cl_device(dev);
771 
772 	return scnprintf(buf, PAGE_SIZE, "%s", cldev->name);
773 }
774 static DEVICE_ATTR_RO(name);
775 
776 static ssize_t uuid_show(struct device *dev, struct device_attribute *a,
777 			     char *buf)
778 {
779 	struct mei_cl_device *cldev = to_mei_cl_device(dev);
780 	const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
781 
782 	return scnprintf(buf, PAGE_SIZE, "%pUl", uuid);
783 }
784 static DEVICE_ATTR_RO(uuid);
785 
786 static ssize_t version_show(struct device *dev, struct device_attribute *a,
787 			     char *buf)
788 {
789 	struct mei_cl_device *cldev = to_mei_cl_device(dev);
790 	u8 version = mei_me_cl_ver(cldev->me_cl);
791 
792 	return scnprintf(buf, PAGE_SIZE, "%02X", version);
793 }
794 static DEVICE_ATTR_RO(version);
795 
796 static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
797 			     char *buf)
798 {
799 	struct mei_cl_device *cldev = to_mei_cl_device(dev);
800 	const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
801 	u8 version = mei_me_cl_ver(cldev->me_cl);
802 
803 	return scnprintf(buf, PAGE_SIZE, "mei:%s:%pUl:%02X:",
804 			 cldev->name, uuid, version);
805 }
806 static DEVICE_ATTR_RO(modalias);
807 
808 static struct attribute *mei_cldev_attrs[] = {
809 	&dev_attr_name.attr,
810 	&dev_attr_uuid.attr,
811 	&dev_attr_version.attr,
812 	&dev_attr_modalias.attr,
813 	NULL,
814 };
815 ATTRIBUTE_GROUPS(mei_cldev);
816 
817 /**
818  * mei_cl_device_uevent - me client bus uevent handler
819  *
820  * @dev: device
821  * @env: uevent kobject
822  *
823  * Return: 0 on success -ENOMEM on when add_uevent_var fails
824  */
825 static int mei_cl_device_uevent(struct device *dev, struct kobj_uevent_env *env)
826 {
827 	struct mei_cl_device *cldev = to_mei_cl_device(dev);
828 	const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
829 	u8 version = mei_me_cl_ver(cldev->me_cl);
830 
831 	if (add_uevent_var(env, "MEI_CL_VERSION=%d", version))
832 		return -ENOMEM;
833 
834 	if (add_uevent_var(env, "MEI_CL_UUID=%pUl", uuid))
835 		return -ENOMEM;
836 
837 	if (add_uevent_var(env, "MEI_CL_NAME=%s", cldev->name))
838 		return -ENOMEM;
839 
840 	if (add_uevent_var(env, "MODALIAS=mei:%s:%pUl:%02X:",
841 			   cldev->name, uuid, version))
842 		return -ENOMEM;
843 
844 	return 0;
845 }
846 
847 static struct bus_type mei_cl_bus_type = {
848 	.name		= "mei",
849 	.dev_groups	= mei_cldev_groups,
850 	.match		= mei_cl_device_match,
851 	.probe		= mei_cl_device_probe,
852 	.remove		= mei_cl_device_remove,
853 	.uevent		= mei_cl_device_uevent,
854 };
855 
856 static struct mei_device *mei_dev_bus_get(struct mei_device *bus)
857 {
858 	if (bus)
859 		get_device(bus->dev);
860 
861 	return bus;
862 }
863 
864 static void mei_dev_bus_put(struct mei_device *bus)
865 {
866 	if (bus)
867 		put_device(bus->dev);
868 }
869 
870 static void mei_cl_bus_dev_release(struct device *dev)
871 {
872 	struct mei_cl_device *cldev = to_mei_cl_device(dev);
873 
874 	if (!cldev)
875 		return;
876 
877 	mei_me_cl_put(cldev->me_cl);
878 	mei_dev_bus_put(cldev->bus);
879 	kfree(cldev->cl);
880 	kfree(cldev);
881 }
882 
883 static const struct device_type mei_cl_device_type = {
884 	.release	= mei_cl_bus_dev_release,
885 };
886 
887 /**
888  * mei_cl_bus_set_name - set device name for me client device
889  *
890  * @cldev: me client device
891  */
892 static inline void mei_cl_bus_set_name(struct mei_cl_device *cldev)
893 {
894 	dev_set_name(&cldev->dev, "mei:%s:%pUl:%02X",
895 		     cldev->name,
896 		     mei_me_cl_uuid(cldev->me_cl),
897 		     mei_me_cl_ver(cldev->me_cl));
898 }
899 
900 /**
901  * mei_cl_bus_dev_alloc - initialize and allocate mei client device
902  *
903  * @bus: mei device
904  * @me_cl: me client
905  *
906  * Return: allocated device structur or NULL on allocation failure
907  */
908 static struct mei_cl_device *mei_cl_bus_dev_alloc(struct mei_device *bus,
909 						  struct mei_me_client *me_cl)
910 {
911 	struct mei_cl_device *cldev;
912 	struct mei_cl *cl;
913 
914 	cldev = kzalloc(sizeof(struct mei_cl_device), GFP_KERNEL);
915 	if (!cldev)
916 		return NULL;
917 
918 	cl = mei_cl_allocate(bus);
919 	if (!cl) {
920 		kfree(cldev);
921 		return NULL;
922 	}
923 
924 	device_initialize(&cldev->dev);
925 	cldev->dev.parent = bus->dev;
926 	cldev->dev.bus    = &mei_cl_bus_type;
927 	cldev->dev.type   = &mei_cl_device_type;
928 	cldev->bus        = mei_dev_bus_get(bus);
929 	cldev->me_cl      = mei_me_cl_get(me_cl);
930 	cldev->cl         = cl;
931 	mei_cl_bus_set_name(cldev);
932 	cldev->is_added   = 0;
933 	INIT_LIST_HEAD(&cldev->bus_list);
934 
935 	return cldev;
936 }
937 
938 /**
939  * mei_cl_dev_setup - setup me client device
940  *    run fix up routines and set the device name
941  *
942  * @bus: mei device
943  * @cldev: me client device
944  *
945  * Return: true if the device is eligible for enumeration
946  */
947 static bool mei_cl_bus_dev_setup(struct mei_device *bus,
948 				 struct mei_cl_device *cldev)
949 {
950 	cldev->do_match = 1;
951 	mei_cl_bus_dev_fixup(cldev);
952 
953 	/* the device name can change during fix up */
954 	if (cldev->do_match)
955 		mei_cl_bus_set_name(cldev);
956 
957 	return cldev->do_match == 1;
958 }
959 
960 /**
961  * mei_cl_bus_dev_add - add me client devices
962  *
963  * @cldev: me client device
964  *
965  * Return: 0 on success; < 0 on failre
966  */
967 static int mei_cl_bus_dev_add(struct mei_cl_device *cldev)
968 {
969 	int ret;
970 
971 	dev_dbg(cldev->bus->dev, "adding %pUL:%02X\n",
972 		mei_me_cl_uuid(cldev->me_cl),
973 		mei_me_cl_ver(cldev->me_cl));
974 	ret = device_add(&cldev->dev);
975 	if (!ret)
976 		cldev->is_added = 1;
977 
978 	return ret;
979 }
980 
981 /**
982  * mei_cl_bus_dev_stop - stop the driver
983  *
984  * @cldev: me client device
985  */
986 static void mei_cl_bus_dev_stop(struct mei_cl_device *cldev)
987 {
988 	if (cldev->is_added)
989 		device_release_driver(&cldev->dev);
990 }
991 
992 /**
993  * mei_cl_bus_dev_destroy - destroy me client devices object
994  *
995  * @cldev: me client device
996  *
997  * Locking: called under "dev->cl_bus_lock" lock
998  */
999 static void mei_cl_bus_dev_destroy(struct mei_cl_device *cldev)
1000 {
1001 
1002 	WARN_ON(!mutex_is_locked(&cldev->bus->cl_bus_lock));
1003 
1004 	if (!cldev->is_added)
1005 		return;
1006 
1007 	device_del(&cldev->dev);
1008 
1009 	list_del_init(&cldev->bus_list);
1010 
1011 	cldev->is_added = 0;
1012 	put_device(&cldev->dev);
1013 }
1014 
1015 /**
1016  * mei_cl_bus_remove_device - remove a devices form the bus
1017  *
1018  * @cldev: me client device
1019  */
1020 static void mei_cl_bus_remove_device(struct mei_cl_device *cldev)
1021 {
1022 	mei_cl_bus_dev_stop(cldev);
1023 	mei_cl_bus_dev_destroy(cldev);
1024 }
1025 
1026 /**
1027  * mei_cl_bus_remove_devices - remove all devices form the bus
1028  *
1029  * @bus: mei device
1030  */
1031 void mei_cl_bus_remove_devices(struct mei_device *bus)
1032 {
1033 	struct mei_cl_device *cldev, *next;
1034 
1035 	mutex_lock(&bus->cl_bus_lock);
1036 	list_for_each_entry_safe(cldev, next, &bus->device_list, bus_list)
1037 		mei_cl_bus_remove_device(cldev);
1038 	mutex_unlock(&bus->cl_bus_lock);
1039 }
1040 
1041 
1042 /**
1043  * mei_cl_bus_dev_init - allocate and initializes an mei client devices
1044  *     based on me client
1045  *
1046  * @bus: mei device
1047  * @me_cl: me client
1048  *
1049  * Locking: called under "dev->cl_bus_lock" lock
1050  */
1051 static void mei_cl_bus_dev_init(struct mei_device *bus,
1052 				struct mei_me_client *me_cl)
1053 {
1054 	struct mei_cl_device *cldev;
1055 
1056 	WARN_ON(!mutex_is_locked(&bus->cl_bus_lock));
1057 
1058 	dev_dbg(bus->dev, "initializing %pUl", mei_me_cl_uuid(me_cl));
1059 
1060 	if (me_cl->bus_added)
1061 		return;
1062 
1063 	cldev = mei_cl_bus_dev_alloc(bus, me_cl);
1064 	if (!cldev)
1065 		return;
1066 
1067 	me_cl->bus_added = true;
1068 	list_add_tail(&cldev->bus_list, &bus->device_list);
1069 
1070 }
1071 
1072 /**
1073  * mei_cl_bus_rescan - scan me clients list and add create
1074  *    devices for eligible clients
1075  *
1076  * @bus: mei device
1077  */
1078 static void mei_cl_bus_rescan(struct mei_device *bus)
1079 {
1080 	struct mei_cl_device *cldev, *n;
1081 	struct mei_me_client *me_cl;
1082 
1083 	mutex_lock(&bus->cl_bus_lock);
1084 
1085 	down_read(&bus->me_clients_rwsem);
1086 	list_for_each_entry(me_cl, &bus->me_clients, list)
1087 		mei_cl_bus_dev_init(bus, me_cl);
1088 	up_read(&bus->me_clients_rwsem);
1089 
1090 	list_for_each_entry_safe(cldev, n, &bus->device_list, bus_list) {
1091 
1092 		if (!mei_me_cl_is_active(cldev->me_cl)) {
1093 			mei_cl_bus_remove_device(cldev);
1094 			continue;
1095 		}
1096 
1097 		if (cldev->is_added)
1098 			continue;
1099 
1100 		if (mei_cl_bus_dev_setup(bus, cldev))
1101 			mei_cl_bus_dev_add(cldev);
1102 		else {
1103 			list_del_init(&cldev->bus_list);
1104 			put_device(&cldev->dev);
1105 		}
1106 	}
1107 	mutex_unlock(&bus->cl_bus_lock);
1108 
1109 	dev_dbg(bus->dev, "rescan end");
1110 }
1111 
1112 void mei_cl_bus_rescan_work(struct work_struct *work)
1113 {
1114 	struct mei_device *bus =
1115 		container_of(work, struct mei_device, bus_rescan_work);
1116 
1117 	mei_cl_bus_rescan(bus);
1118 }
1119 
1120 int __mei_cldev_driver_register(struct mei_cl_driver *cldrv,
1121 				struct module *owner)
1122 {
1123 	int err;
1124 
1125 	cldrv->driver.name = cldrv->name;
1126 	cldrv->driver.owner = owner;
1127 	cldrv->driver.bus = &mei_cl_bus_type;
1128 
1129 	err = driver_register(&cldrv->driver);
1130 	if (err)
1131 		return err;
1132 
1133 	pr_debug("mei: driver [%s] registered\n", cldrv->driver.name);
1134 
1135 	return 0;
1136 }
1137 EXPORT_SYMBOL_GPL(__mei_cldev_driver_register);
1138 
1139 void mei_cldev_driver_unregister(struct mei_cl_driver *cldrv)
1140 {
1141 	driver_unregister(&cldrv->driver);
1142 
1143 	pr_debug("mei: driver [%s] unregistered\n", cldrv->driver.name);
1144 }
1145 EXPORT_SYMBOL_GPL(mei_cldev_driver_unregister);
1146 
1147 
1148 int __init mei_cl_bus_init(void)
1149 {
1150 	return bus_register(&mei_cl_bus_type);
1151 }
1152 
1153 void __exit mei_cl_bus_exit(void)
1154 {
1155 	bus_unregister(&mei_cl_bus_type);
1156 }
1157