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