xref: /linux/drivers/misc/mei/bus.c (revision b61fe3b5963db935dad5bae8f9ced3bed695bb62)
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 	if (mei_cl_is_connected(cldev->cl))
280 		mei_cl_read_start(cldev->cl, mei_cl_mtu(cldev->cl), NULL);
281 	mutex_unlock(&bus->device_lock);
282 }
283 
284 /**
285  * mei_cl_bus_notif_work - dispatch FW notif event for a bus device
286  *
287  * @work: work
288  */
289 static void mei_cl_bus_notif_work(struct work_struct *work)
290 {
291 	struct mei_cl_device *cldev;
292 
293 	cldev = container_of(work, struct mei_cl_device, notif_work);
294 
295 	if (cldev->notif_cb)
296 		cldev->notif_cb(cldev);
297 }
298 
299 /**
300  * mei_cl_bus_notify_event - schedule notify cb on bus client
301  *
302  * @cl: host client
303  *
304  * Return: true if event was scheduled
305  *         false if the client is not waiting for event
306  */
307 bool mei_cl_bus_notify_event(struct mei_cl *cl)
308 {
309 	struct mei_cl_device *cldev = cl->cldev;
310 
311 	if (!cldev || !cldev->notif_cb)
312 		return false;
313 
314 	if (!cl->notify_ev)
315 		return false;
316 
317 	schedule_work(&cldev->notif_work);
318 
319 	cl->notify_ev = false;
320 
321 	return true;
322 }
323 
324 /**
325  * mei_cl_bus_rx_event - schedule rx event
326  *
327  * @cl: host client
328  *
329  * Return: true if event was scheduled
330  *         false if the client is not waiting for event
331  */
332 bool mei_cl_bus_rx_event(struct mei_cl *cl)
333 {
334 	struct mei_cl_device *cldev = cl->cldev;
335 
336 	if (!cldev || !cldev->rx_cb)
337 		return false;
338 
339 	schedule_work(&cldev->rx_work);
340 
341 	return true;
342 }
343 
344 /**
345  * mei_cldev_register_rx_cb - register Rx event callback
346  *
347  * @cldev: me client devices
348  * @rx_cb: callback function
349  *
350  * Return: 0 on success
351  *         -EALREADY if an callback is already registered
352  *         <0 on other errors
353  */
354 int mei_cldev_register_rx_cb(struct mei_cl_device *cldev, mei_cldev_cb_t rx_cb)
355 {
356 	struct mei_device *bus = cldev->bus;
357 	int ret;
358 
359 	if (!rx_cb)
360 		return -EINVAL;
361 	if (cldev->rx_cb)
362 		return -EALREADY;
363 
364 	cldev->rx_cb = rx_cb;
365 	INIT_WORK(&cldev->rx_work, mei_cl_bus_rx_work);
366 
367 	mutex_lock(&bus->device_lock);
368 	if (mei_cl_is_connected(cldev->cl))
369 		ret = mei_cl_read_start(cldev->cl, mei_cl_mtu(cldev->cl), NULL);
370 	else
371 		ret = -ENODEV;
372 	mutex_unlock(&bus->device_lock);
373 	if (ret && ret != -EBUSY) {
374 		cancel_work_sync(&cldev->rx_work);
375 		cldev->rx_cb = NULL;
376 		return ret;
377 	}
378 
379 	return 0;
380 }
381 EXPORT_SYMBOL_GPL(mei_cldev_register_rx_cb);
382 
383 /**
384  * mei_cldev_register_notif_cb - register FW notification event callback
385  *
386  * @cldev: me client devices
387  * @notif_cb: callback function
388  *
389  * Return: 0 on success
390  *         -EALREADY if an callback is already registered
391  *         <0 on other errors
392  */
393 int mei_cldev_register_notif_cb(struct mei_cl_device *cldev,
394 				mei_cldev_cb_t notif_cb)
395 {
396 	struct mei_device *bus = cldev->bus;
397 	int ret;
398 
399 	if (!notif_cb)
400 		return -EINVAL;
401 
402 	if (cldev->notif_cb)
403 		return -EALREADY;
404 
405 	cldev->notif_cb = notif_cb;
406 	INIT_WORK(&cldev->notif_work, mei_cl_bus_notif_work);
407 
408 	mutex_lock(&bus->device_lock);
409 	ret = mei_cl_notify_request(cldev->cl, NULL, 1);
410 	mutex_unlock(&bus->device_lock);
411 	if (ret) {
412 		cancel_work_sync(&cldev->notif_work);
413 		cldev->notif_cb = NULL;
414 		return ret;
415 	}
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_cl_bus_vtag - get bus vtag entry wrapper
510  *     The tag for bus client is always first.
511  *
512  * @cl: host client
513  *
514  * Return: bus vtag or NULL
515  */
516 static inline struct mei_cl_vtag *mei_cl_bus_vtag(struct mei_cl *cl)
517 {
518 	return list_first_entry_or_null(&cl->vtag_map,
519 					struct mei_cl_vtag, list);
520 }
521 
522 /**
523  * mei_cl_bus_vtag_alloc - add bus client entry to vtag map
524  *
525  * @cldev: me client device
526  *
527  * Return:
528  * * 0 on success
529  * * -ENOMEM if memory allocation failed
530  */
531 static int mei_cl_bus_vtag_alloc(struct mei_cl_device *cldev)
532 {
533 	struct mei_cl *cl = cldev->cl;
534 	struct mei_cl_vtag *cl_vtag;
535 
536 	/*
537 	 * Bail out if the client does not supports vtags
538 	 * or has already allocated one
539 	 */
540 	if (mei_cl_vt_support_check(cl) || mei_cl_bus_vtag(cl))
541 		return 0;
542 
543 	cl_vtag = mei_cl_vtag_alloc(NULL, 0);
544 	if (IS_ERR(cl_vtag))
545 		return -ENOMEM;
546 
547 	list_add_tail(&cl_vtag->list, &cl->vtag_map);
548 
549 	return 0;
550 }
551 
552 /**
553  * mei_cl_bus_vtag_free - remove the bus entry from vtag map
554  *
555  * @cldev: me client device
556  */
557 static void mei_cl_bus_vtag_free(struct mei_cl_device *cldev)
558 {
559 	struct mei_cl *cl = cldev->cl;
560 	struct mei_cl_vtag *cl_vtag;
561 
562 	cl_vtag = mei_cl_bus_vtag(cl);
563 	if (!cl_vtag)
564 		return;
565 
566 	list_del(&cl_vtag->list);
567 	kfree(cl_vtag);
568 }
569 
570 /**
571  * mei_cldev_enable - enable me client device
572  *     create connection with me client
573  *
574  * @cldev: me client device
575  *
576  * Return: 0 on success and < 0 on error
577  */
578 int mei_cldev_enable(struct mei_cl_device *cldev)
579 {
580 	struct mei_device *bus = cldev->bus;
581 	struct mei_cl *cl;
582 	int ret;
583 
584 	cl = cldev->cl;
585 
586 	mutex_lock(&bus->device_lock);
587 	if (cl->state == MEI_FILE_UNINITIALIZED) {
588 		ret = mei_cl_link(cl);
589 		if (ret)
590 			goto out;
591 		/* update pointers */
592 		cl->cldev = cldev;
593 	}
594 
595 	if (mei_cl_is_connected(cl)) {
596 		ret = 0;
597 		goto out;
598 	}
599 
600 	if (!mei_me_cl_is_active(cldev->me_cl)) {
601 		dev_err(&cldev->dev, "me client is not active\n");
602 		ret = -ENOTTY;
603 		goto out;
604 	}
605 
606 	ret = mei_cl_bus_vtag_alloc(cldev);
607 	if (ret)
608 		goto out;
609 
610 	ret = mei_cl_connect(cl, cldev->me_cl, NULL);
611 	if (ret < 0) {
612 		dev_err(&cldev->dev, "cannot connect\n");
613 		mei_cl_bus_vtag_free(cldev);
614 	}
615 
616 out:
617 	mutex_unlock(&bus->device_lock);
618 
619 	return ret;
620 }
621 EXPORT_SYMBOL_GPL(mei_cldev_enable);
622 
623 /**
624  * mei_cldev_unregister_callbacks - internal wrapper for unregistering
625  *  callbacks.
626  *
627  * @cldev: client device
628  */
629 static void mei_cldev_unregister_callbacks(struct mei_cl_device *cldev)
630 {
631 	if (cldev->rx_cb) {
632 		cancel_work_sync(&cldev->rx_work);
633 		cldev->rx_cb = NULL;
634 	}
635 
636 	if (cldev->notif_cb) {
637 		cancel_work_sync(&cldev->notif_work);
638 		cldev->notif_cb = NULL;
639 	}
640 }
641 
642 /**
643  * mei_cldev_disable - disable me client device
644  *     disconnect form the me client
645  *
646  * @cldev: me client device
647  *
648  * Return: 0 on success and < 0 on error
649  */
650 int mei_cldev_disable(struct mei_cl_device *cldev)
651 {
652 	struct mei_device *bus;
653 	struct mei_cl *cl;
654 	int err;
655 
656 	if (!cldev)
657 		return -ENODEV;
658 
659 	cl = cldev->cl;
660 
661 	bus = cldev->bus;
662 
663 	mei_cldev_unregister_callbacks(cldev);
664 
665 	mutex_lock(&bus->device_lock);
666 
667 	mei_cl_bus_vtag_free(cldev);
668 
669 	if (!mei_cl_is_connected(cl)) {
670 		dev_dbg(bus->dev, "Already disconnected\n");
671 		err = 0;
672 		goto out;
673 	}
674 
675 	err = mei_cl_disconnect(cl);
676 	if (err < 0)
677 		dev_err(bus->dev, "Could not disconnect from the ME client\n");
678 
679 out:
680 	/* Flush queues and remove any pending read */
681 	mei_cl_flush_queues(cl, NULL);
682 	mei_cl_unlink(cl);
683 
684 	mutex_unlock(&bus->device_lock);
685 	return err;
686 }
687 EXPORT_SYMBOL_GPL(mei_cldev_disable);
688 
689 /**
690  * mei_cl_device_find - find matching entry in the driver id table
691  *
692  * @cldev: me client device
693  * @cldrv: me client driver
694  *
695  * Return: id on success; NULL if no id is matching
696  */
697 static const
698 struct mei_cl_device_id *mei_cl_device_find(struct mei_cl_device *cldev,
699 					    struct mei_cl_driver *cldrv)
700 {
701 	const struct mei_cl_device_id *id;
702 	const uuid_le *uuid;
703 	u8 version;
704 	bool match;
705 
706 	uuid = mei_me_cl_uuid(cldev->me_cl);
707 	version = mei_me_cl_ver(cldev->me_cl);
708 
709 	id = cldrv->id_table;
710 	while (uuid_le_cmp(NULL_UUID_LE, id->uuid)) {
711 		if (!uuid_le_cmp(*uuid, id->uuid)) {
712 			match = true;
713 
714 			if (cldev->name[0])
715 				if (strncmp(cldev->name, id->name,
716 					    sizeof(id->name)))
717 					match = false;
718 
719 			if (id->version != MEI_CL_VERSION_ANY)
720 				if (id->version != version)
721 					match = false;
722 			if (match)
723 				return id;
724 		}
725 
726 		id++;
727 	}
728 
729 	return NULL;
730 }
731 
732 /**
733  * mei_cl_device_match  - device match function
734  *
735  * @dev: device
736  * @drv: driver
737  *
738  * Return:  1 if matching device was found 0 otherwise
739  */
740 static int mei_cl_device_match(struct device *dev, struct device_driver *drv)
741 {
742 	struct mei_cl_device *cldev = to_mei_cl_device(dev);
743 	struct mei_cl_driver *cldrv = to_mei_cl_driver(drv);
744 	const struct mei_cl_device_id *found_id;
745 
746 	if (!cldev)
747 		return 0;
748 
749 	if (!cldev->do_match)
750 		return 0;
751 
752 	if (!cldrv || !cldrv->id_table)
753 		return 0;
754 
755 	found_id = mei_cl_device_find(cldev, cldrv);
756 	if (found_id)
757 		return 1;
758 
759 	return 0;
760 }
761 
762 /**
763  * mei_cl_device_probe - bus probe function
764  *
765  * @dev: device
766  *
767  * Return:  0 on success; < 0 otherwise
768  */
769 static int mei_cl_device_probe(struct device *dev)
770 {
771 	struct mei_cl_device *cldev;
772 	struct mei_cl_driver *cldrv;
773 	const struct mei_cl_device_id *id;
774 	int ret;
775 
776 	cldev = to_mei_cl_device(dev);
777 	cldrv = to_mei_cl_driver(dev->driver);
778 
779 	if (!cldev)
780 		return 0;
781 
782 	if (!cldrv || !cldrv->probe)
783 		return -ENODEV;
784 
785 	id = mei_cl_device_find(cldev, cldrv);
786 	if (!id)
787 		return -ENODEV;
788 
789 	if (!mei_cl_bus_module_get(cldev)) {
790 		dev_err(&cldev->dev, "get hw module failed");
791 		return -ENODEV;
792 	}
793 
794 	ret = cldrv->probe(cldev, id);
795 	if (ret) {
796 		mei_cl_bus_module_put(cldev);
797 		return ret;
798 	}
799 
800 	__module_get(THIS_MODULE);
801 	return 0;
802 }
803 
804 /**
805  * mei_cl_device_remove - remove device from the bus
806  *
807  * @dev: device
808  *
809  * Return:  0 on success; < 0 otherwise
810  */
811 static int mei_cl_device_remove(struct device *dev)
812 {
813 	struct mei_cl_device *cldev = to_mei_cl_device(dev);
814 	struct mei_cl_driver *cldrv;
815 	int ret = 0;
816 
817 	if (!cldev || !dev->driver)
818 		return 0;
819 
820 	cldrv = to_mei_cl_driver(dev->driver);
821 	if (cldrv->remove)
822 		ret = cldrv->remove(cldev);
823 
824 	mei_cldev_unregister_callbacks(cldev);
825 
826 	mei_cl_bus_module_put(cldev);
827 	module_put(THIS_MODULE);
828 
829 	return ret;
830 }
831 
832 static ssize_t name_show(struct device *dev, struct device_attribute *a,
833 			     char *buf)
834 {
835 	struct mei_cl_device *cldev = to_mei_cl_device(dev);
836 
837 	return scnprintf(buf, PAGE_SIZE, "%s", cldev->name);
838 }
839 static DEVICE_ATTR_RO(name);
840 
841 static ssize_t uuid_show(struct device *dev, struct device_attribute *a,
842 			     char *buf)
843 {
844 	struct mei_cl_device *cldev = to_mei_cl_device(dev);
845 	const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
846 
847 	return sprintf(buf, "%pUl", uuid);
848 }
849 static DEVICE_ATTR_RO(uuid);
850 
851 static ssize_t version_show(struct device *dev, struct device_attribute *a,
852 			     char *buf)
853 {
854 	struct mei_cl_device *cldev = to_mei_cl_device(dev);
855 	u8 version = mei_me_cl_ver(cldev->me_cl);
856 
857 	return sprintf(buf, "%02X", version);
858 }
859 static DEVICE_ATTR_RO(version);
860 
861 static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
862 			     char *buf)
863 {
864 	struct mei_cl_device *cldev = to_mei_cl_device(dev);
865 	const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
866 	u8 version = mei_me_cl_ver(cldev->me_cl);
867 
868 	return scnprintf(buf, PAGE_SIZE, "mei:%s:%pUl:%02X:",
869 			 cldev->name, uuid, version);
870 }
871 static DEVICE_ATTR_RO(modalias);
872 
873 static ssize_t max_conn_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 maxconn = mei_me_cl_max_conn(cldev->me_cl);
878 
879 	return sprintf(buf, "%d", maxconn);
880 }
881 static DEVICE_ATTR_RO(max_conn);
882 
883 static ssize_t fixed_show(struct device *dev, struct device_attribute *a,
884 			  char *buf)
885 {
886 	struct mei_cl_device *cldev = to_mei_cl_device(dev);
887 	u8 fixed = mei_me_cl_fixed(cldev->me_cl);
888 
889 	return sprintf(buf, "%d", fixed);
890 }
891 static DEVICE_ATTR_RO(fixed);
892 
893 static ssize_t vtag_show(struct device *dev, struct device_attribute *a,
894 			 char *buf)
895 {
896 	struct mei_cl_device *cldev = to_mei_cl_device(dev);
897 	bool vt = mei_me_cl_vt(cldev->me_cl);
898 
899 	return sprintf(buf, "%d", vt);
900 }
901 static DEVICE_ATTR_RO(vtag);
902 
903 static ssize_t max_len_show(struct device *dev, struct device_attribute *a,
904 			    char *buf)
905 {
906 	struct mei_cl_device *cldev = to_mei_cl_device(dev);
907 	u32 maxlen = mei_me_cl_max_len(cldev->me_cl);
908 
909 	return sprintf(buf, "%u", maxlen);
910 }
911 static DEVICE_ATTR_RO(max_len);
912 
913 static struct attribute *mei_cldev_attrs[] = {
914 	&dev_attr_name.attr,
915 	&dev_attr_uuid.attr,
916 	&dev_attr_version.attr,
917 	&dev_attr_modalias.attr,
918 	&dev_attr_max_conn.attr,
919 	&dev_attr_fixed.attr,
920 	&dev_attr_vtag.attr,
921 	&dev_attr_max_len.attr,
922 	NULL,
923 };
924 ATTRIBUTE_GROUPS(mei_cldev);
925 
926 /**
927  * mei_cl_device_uevent - me client bus uevent handler
928  *
929  * @dev: device
930  * @env: uevent kobject
931  *
932  * Return: 0 on success -ENOMEM on when add_uevent_var fails
933  */
934 static int mei_cl_device_uevent(struct device *dev, struct kobj_uevent_env *env)
935 {
936 	struct mei_cl_device *cldev = to_mei_cl_device(dev);
937 	const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
938 	u8 version = mei_me_cl_ver(cldev->me_cl);
939 
940 	if (add_uevent_var(env, "MEI_CL_VERSION=%d", version))
941 		return -ENOMEM;
942 
943 	if (add_uevent_var(env, "MEI_CL_UUID=%pUl", uuid))
944 		return -ENOMEM;
945 
946 	if (add_uevent_var(env, "MEI_CL_NAME=%s", cldev->name))
947 		return -ENOMEM;
948 
949 	if (add_uevent_var(env, "MODALIAS=mei:%s:%pUl:%02X:",
950 			   cldev->name, uuid, version))
951 		return -ENOMEM;
952 
953 	return 0;
954 }
955 
956 static struct bus_type mei_cl_bus_type = {
957 	.name		= "mei",
958 	.dev_groups	= mei_cldev_groups,
959 	.match		= mei_cl_device_match,
960 	.probe		= mei_cl_device_probe,
961 	.remove		= mei_cl_device_remove,
962 	.uevent		= mei_cl_device_uevent,
963 };
964 
965 static struct mei_device *mei_dev_bus_get(struct mei_device *bus)
966 {
967 	if (bus)
968 		get_device(bus->dev);
969 
970 	return bus;
971 }
972 
973 static void mei_dev_bus_put(struct mei_device *bus)
974 {
975 	if (bus)
976 		put_device(bus->dev);
977 }
978 
979 static void mei_cl_bus_dev_release(struct device *dev)
980 {
981 	struct mei_cl_device *cldev = to_mei_cl_device(dev);
982 
983 	if (!cldev)
984 		return;
985 
986 	mei_me_cl_put(cldev->me_cl);
987 	mei_dev_bus_put(cldev->bus);
988 	mei_cl_unlink(cldev->cl);
989 	kfree(cldev->cl);
990 	kfree(cldev);
991 }
992 
993 static const struct device_type mei_cl_device_type = {
994 	.release = mei_cl_bus_dev_release,
995 };
996 
997 /**
998  * mei_cl_bus_set_name - set device name for me client device
999  *  <controller>-<client device>
1000  *  Example: 0000:00:16.0-55213584-9a29-4916-badf-0fb7ed682aeb
1001  *
1002  * @cldev: me client device
1003  */
1004 static inline void mei_cl_bus_set_name(struct mei_cl_device *cldev)
1005 {
1006 	dev_set_name(&cldev->dev, "%s-%pUl",
1007 		     dev_name(cldev->bus->dev),
1008 		     mei_me_cl_uuid(cldev->me_cl));
1009 }
1010 
1011 /**
1012  * mei_cl_bus_dev_alloc - initialize and allocate mei client device
1013  *
1014  * @bus: mei device
1015  * @me_cl: me client
1016  *
1017  * Return: allocated device structur or NULL on allocation failure
1018  */
1019 static struct mei_cl_device *mei_cl_bus_dev_alloc(struct mei_device *bus,
1020 						  struct mei_me_client *me_cl)
1021 {
1022 	struct mei_cl_device *cldev;
1023 	struct mei_cl *cl;
1024 
1025 	cldev = kzalloc(sizeof(*cldev), GFP_KERNEL);
1026 	if (!cldev)
1027 		return NULL;
1028 
1029 	cl = mei_cl_allocate(bus);
1030 	if (!cl) {
1031 		kfree(cldev);
1032 		return NULL;
1033 	}
1034 
1035 	device_initialize(&cldev->dev);
1036 	cldev->dev.parent = bus->dev;
1037 	cldev->dev.bus    = &mei_cl_bus_type;
1038 	cldev->dev.type   = &mei_cl_device_type;
1039 	cldev->bus        = mei_dev_bus_get(bus);
1040 	cldev->me_cl      = mei_me_cl_get(me_cl);
1041 	cldev->cl         = cl;
1042 	mei_cl_bus_set_name(cldev);
1043 	cldev->is_added   = 0;
1044 	INIT_LIST_HEAD(&cldev->bus_list);
1045 
1046 	return cldev;
1047 }
1048 
1049 /**
1050  * mei_cl_dev_setup - setup me client device
1051  *    run fix up routines and set the device name
1052  *
1053  * @bus: mei device
1054  * @cldev: me client device
1055  *
1056  * Return: true if the device is eligible for enumeration
1057  */
1058 static bool mei_cl_bus_dev_setup(struct mei_device *bus,
1059 				 struct mei_cl_device *cldev)
1060 {
1061 	cldev->do_match = 1;
1062 	mei_cl_bus_dev_fixup(cldev);
1063 
1064 	/* the device name can change during fix up */
1065 	if (cldev->do_match)
1066 		mei_cl_bus_set_name(cldev);
1067 
1068 	return cldev->do_match == 1;
1069 }
1070 
1071 /**
1072  * mei_cl_bus_dev_add - add me client devices
1073  *
1074  * @cldev: me client device
1075  *
1076  * Return: 0 on success; < 0 on failre
1077  */
1078 static int mei_cl_bus_dev_add(struct mei_cl_device *cldev)
1079 {
1080 	int ret;
1081 
1082 	dev_dbg(cldev->bus->dev, "adding %pUL:%02X\n",
1083 		mei_me_cl_uuid(cldev->me_cl),
1084 		mei_me_cl_ver(cldev->me_cl));
1085 	ret = device_add(&cldev->dev);
1086 	if (!ret)
1087 		cldev->is_added = 1;
1088 
1089 	return ret;
1090 }
1091 
1092 /**
1093  * mei_cl_bus_dev_stop - stop the driver
1094  *
1095  * @cldev: me client device
1096  */
1097 static void mei_cl_bus_dev_stop(struct mei_cl_device *cldev)
1098 {
1099 	if (cldev->is_added)
1100 		device_release_driver(&cldev->dev);
1101 }
1102 
1103 /**
1104  * mei_cl_bus_dev_destroy - destroy me client devices object
1105  *
1106  * @cldev: me client device
1107  *
1108  * Locking: called under "dev->cl_bus_lock" lock
1109  */
1110 static void mei_cl_bus_dev_destroy(struct mei_cl_device *cldev)
1111 {
1112 
1113 	WARN_ON(!mutex_is_locked(&cldev->bus->cl_bus_lock));
1114 
1115 	if (!cldev->is_added)
1116 		return;
1117 
1118 	device_del(&cldev->dev);
1119 
1120 	list_del_init(&cldev->bus_list);
1121 
1122 	cldev->is_added = 0;
1123 	put_device(&cldev->dev);
1124 }
1125 
1126 /**
1127  * mei_cl_bus_remove_device - remove a devices form the bus
1128  *
1129  * @cldev: me client device
1130  */
1131 static void mei_cl_bus_remove_device(struct mei_cl_device *cldev)
1132 {
1133 	mei_cl_bus_dev_stop(cldev);
1134 	mei_cl_bus_dev_destroy(cldev);
1135 }
1136 
1137 /**
1138  * mei_cl_bus_remove_devices - remove all devices form the bus
1139  *
1140  * @bus: mei device
1141  */
1142 void mei_cl_bus_remove_devices(struct mei_device *bus)
1143 {
1144 	struct mei_cl_device *cldev, *next;
1145 
1146 	mutex_lock(&bus->cl_bus_lock);
1147 	list_for_each_entry_safe(cldev, next, &bus->device_list, bus_list)
1148 		mei_cl_bus_remove_device(cldev);
1149 	mutex_unlock(&bus->cl_bus_lock);
1150 }
1151 
1152 
1153 /**
1154  * mei_cl_bus_dev_init - allocate and initializes an mei client devices
1155  *     based on me client
1156  *
1157  * @bus: mei device
1158  * @me_cl: me client
1159  *
1160  * Locking: called under "dev->cl_bus_lock" lock
1161  */
1162 static void mei_cl_bus_dev_init(struct mei_device *bus,
1163 				struct mei_me_client *me_cl)
1164 {
1165 	struct mei_cl_device *cldev;
1166 
1167 	WARN_ON(!mutex_is_locked(&bus->cl_bus_lock));
1168 
1169 	dev_dbg(bus->dev, "initializing %pUl", mei_me_cl_uuid(me_cl));
1170 
1171 	if (me_cl->bus_added)
1172 		return;
1173 
1174 	cldev = mei_cl_bus_dev_alloc(bus, me_cl);
1175 	if (!cldev)
1176 		return;
1177 
1178 	me_cl->bus_added = true;
1179 	list_add_tail(&cldev->bus_list, &bus->device_list);
1180 
1181 }
1182 
1183 /**
1184  * mei_cl_bus_rescan - scan me clients list and add create
1185  *    devices for eligible clients
1186  *
1187  * @bus: mei device
1188  */
1189 static void mei_cl_bus_rescan(struct mei_device *bus)
1190 {
1191 	struct mei_cl_device *cldev, *n;
1192 	struct mei_me_client *me_cl;
1193 
1194 	mutex_lock(&bus->cl_bus_lock);
1195 
1196 	down_read(&bus->me_clients_rwsem);
1197 	list_for_each_entry(me_cl, &bus->me_clients, list)
1198 		mei_cl_bus_dev_init(bus, me_cl);
1199 	up_read(&bus->me_clients_rwsem);
1200 
1201 	list_for_each_entry_safe(cldev, n, &bus->device_list, bus_list) {
1202 
1203 		if (!mei_me_cl_is_active(cldev->me_cl)) {
1204 			mei_cl_bus_remove_device(cldev);
1205 			continue;
1206 		}
1207 
1208 		if (cldev->is_added)
1209 			continue;
1210 
1211 		if (mei_cl_bus_dev_setup(bus, cldev))
1212 			mei_cl_bus_dev_add(cldev);
1213 		else {
1214 			list_del_init(&cldev->bus_list);
1215 			put_device(&cldev->dev);
1216 		}
1217 	}
1218 	mutex_unlock(&bus->cl_bus_lock);
1219 
1220 	dev_dbg(bus->dev, "rescan end");
1221 }
1222 
1223 void mei_cl_bus_rescan_work(struct work_struct *work)
1224 {
1225 	struct mei_device *bus =
1226 		container_of(work, struct mei_device, bus_rescan_work);
1227 
1228 	mei_cl_bus_rescan(bus);
1229 }
1230 
1231 int __mei_cldev_driver_register(struct mei_cl_driver *cldrv,
1232 				struct module *owner)
1233 {
1234 	int err;
1235 
1236 	cldrv->driver.name = cldrv->name;
1237 	cldrv->driver.owner = owner;
1238 	cldrv->driver.bus = &mei_cl_bus_type;
1239 
1240 	err = driver_register(&cldrv->driver);
1241 	if (err)
1242 		return err;
1243 
1244 	pr_debug("mei: driver [%s] registered\n", cldrv->driver.name);
1245 
1246 	return 0;
1247 }
1248 EXPORT_SYMBOL_GPL(__mei_cldev_driver_register);
1249 
1250 void mei_cldev_driver_unregister(struct mei_cl_driver *cldrv)
1251 {
1252 	driver_unregister(&cldrv->driver);
1253 
1254 	pr_debug("mei: driver [%s] unregistered\n", cldrv->driver.name);
1255 }
1256 EXPORT_SYMBOL_GPL(mei_cldev_driver_unregister);
1257 
1258 
1259 int __init mei_cl_bus_init(void)
1260 {
1261 	return bus_register(&mei_cl_bus_type);
1262 }
1263 
1264 void __exit mei_cl_bus_exit(void)
1265 {
1266 	bus_unregister(&mei_cl_bus_type);
1267 }
1268