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