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