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