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