1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2012-2019, Intel Corporation. All rights reserved.
4 * Intel Management Engine Interface (Intel MEI) Linux driver
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>
22 #define to_mei_cl_driver(d) container_of(d, struct mei_cl_driver, driver)
25 * __mei_cl_send - internal client send (write)
28 * @buf: buffer to send
29 * @length: buffer length
33 * Return: written size bytes or < 0 on error
35 ssize_t __mei_cl_send(struct mei_cl *cl, const u8 *buf, size_t length, u8 vtag,
38 struct mei_device *bus;
42 if (WARN_ON(!cl || !cl->dev))
47 mutex_lock(&bus->device_lock);
48 if (bus->dev_state != MEI_DEV_ENABLED &&
49 bus->dev_state != MEI_DEV_POWERING_DOWN) {
54 if (!mei_cl_is_connected(cl)) {
59 /* Check if we have an ME client device */
60 if (!mei_me_cl_is_active(cl->me_cl)) {
66 /* Check if vtag is supported by client */
67 rets = mei_cl_vt_support_check(cl);
72 if (length > mei_cl_mtu(cl)) {
77 while (cl->tx_cb_queued >= bus->tx_queue_limit) {
78 mutex_unlock(&bus->device_lock);
79 rets = wait_event_interruptible(cl->tx_wait,
80 cl->writing_state == MEI_WRITE_COMPLETE ||
81 (!mei_cl_is_connected(cl)));
82 mutex_lock(&bus->device_lock);
84 if (signal_pending(current))
88 if (!mei_cl_is_connected(cl)) {
94 cb = mei_cl_alloc_cb(cl, length, MEI_FOP_WRITE, NULL);
101 cb->internal = !!(mode & MEI_CL_IO_TX_INTERNAL);
102 cb->blocking = !!(mode & MEI_CL_IO_TX_BLOCKING);
103 memcpy(cb->buf.data, buf, length);
104 /* hack we point data to header */
105 if (mode & MEI_CL_IO_SGL) {
106 cb->ext_hdr = (struct mei_ext_hdr *)cb->buf.data;
111 rets = mei_cl_write(cl, cb);
113 if (mode & MEI_CL_IO_SGL && rets == 0)
117 mutex_unlock(&bus->device_lock);
123 * __mei_cl_recv - internal client receive (read)
126 * @buf: buffer to receive
127 * @length: buffer length
130 * @timeout: recv timeout, 0 for infinite timeout
132 * Return: read size in bytes of < 0 on error
134 ssize_t __mei_cl_recv(struct mei_cl *cl, u8 *buf, size_t length, u8 *vtag,
135 unsigned int mode, unsigned long timeout)
137 struct mei_device *bus;
138 struct mei_cl_cb *cb;
141 bool nonblock = !!(mode & MEI_CL_IO_RX_NONBLOCK);
143 if (WARN_ON(!cl || !cl->dev))
148 mutex_lock(&bus->device_lock);
149 if (bus->dev_state != MEI_DEV_ENABLED &&
150 bus->dev_state != MEI_DEV_POWERING_DOWN) {
155 cb = mei_cl_read_cb(cl, NULL);
159 rets = mei_cl_read_start(cl, length, NULL);
160 if (rets && rets != -EBUSY)
168 /* wait on event only if there is no other waiter */
169 /* synchronized under device mutex */
170 if (!waitqueue_active(&cl->rx_wait)) {
172 mutex_unlock(&bus->device_lock);
175 rets = wait_event_interruptible_timeout
177 mei_cl_read_cb(cl, NULL) ||
178 (!mei_cl_is_connected(cl)),
179 msecs_to_jiffies(timeout));
183 if (signal_pending(current))
188 if (wait_event_interruptible
190 mei_cl_read_cb(cl, NULL) ||
191 (!mei_cl_is_connected(cl)))) {
192 if (signal_pending(current))
198 mutex_lock(&bus->device_lock);
200 if (!mei_cl_is_connected(cl)) {
206 cb = mei_cl_read_cb(cl, NULL);
218 /* for the GSC type - copy the extended header to the buffer */
219 if (cb->ext_hdr && cb->ext_hdr->type == MEI_EXT_HDR_GSC) {
220 r_length = min_t(size_t, length, cb->ext_hdr->length * sizeof(u32));
221 memcpy(buf, cb->ext_hdr, r_length);
223 r_length = min_t(size_t, length, cb->buf_idx);
224 memcpy(buf, cb->buf.data, r_length);
232 mei_cl_del_rd_completed(cl, cb);
234 mutex_unlock(&bus->device_lock);
240 * mei_cldev_send_vtag - me device send with vtag (write)
242 * @cldev: me client device
243 * @buf: buffer to send
244 * @length: buffer length
248 * * written size in bytes
252 ssize_t mei_cldev_send_vtag(struct mei_cl_device *cldev, const u8 *buf,
253 size_t length, u8 vtag)
255 struct mei_cl *cl = cldev->cl;
257 return __mei_cl_send(cl, buf, length, vtag, MEI_CL_IO_TX_BLOCKING);
259 EXPORT_SYMBOL_GPL(mei_cldev_send_vtag);
262 * mei_cldev_recv_vtag - client receive with vtag (read)
264 * @cldev: me client device
265 * @buf: buffer to receive
266 * @length: buffer length
270 * * read size in bytes
274 ssize_t mei_cldev_recv_vtag(struct mei_cl_device *cldev, u8 *buf, size_t length,
277 struct mei_cl *cl = cldev->cl;
279 return __mei_cl_recv(cl, buf, length, vtag, 0, 0);
281 EXPORT_SYMBOL_GPL(mei_cldev_recv_vtag);
284 * mei_cldev_recv_nonblock_vtag - non block client receive with vtag (read)
286 * @cldev: me client device
287 * @buf: buffer to receive
288 * @length: buffer length
292 * * read size in bytes
293 * * -EAGAIN if function will block.
294 * * < 0 on other error
296 ssize_t mei_cldev_recv_nonblock_vtag(struct mei_cl_device *cldev, u8 *buf,
297 size_t length, u8 *vtag)
299 struct mei_cl *cl = cldev->cl;
301 return __mei_cl_recv(cl, buf, length, vtag, MEI_CL_IO_RX_NONBLOCK, 0);
303 EXPORT_SYMBOL_GPL(mei_cldev_recv_nonblock_vtag);
306 * mei_cldev_send - me device send (write)
308 * @cldev: me client device
309 * @buf: buffer to send
310 * @length: buffer length
313 * * written size in bytes
316 ssize_t mei_cldev_send(struct mei_cl_device *cldev, const u8 *buf, size_t length)
318 return mei_cldev_send_vtag(cldev, buf, length, 0);
320 EXPORT_SYMBOL_GPL(mei_cldev_send);
323 * mei_cldev_recv - client receive (read)
325 * @cldev: me client device
326 * @buf: buffer to receive
327 * @length: buffer length
329 * Return: read size in bytes of < 0 on error
331 ssize_t mei_cldev_recv(struct mei_cl_device *cldev, u8 *buf, size_t length)
333 return mei_cldev_recv_vtag(cldev, buf, length, NULL);
335 EXPORT_SYMBOL_GPL(mei_cldev_recv);
338 * mei_cldev_recv_nonblock - non block client receive (read)
340 * @cldev: me client device
341 * @buf: buffer to receive
342 * @length: buffer length
344 * Return: read size in bytes of < 0 on error
345 * -EAGAIN if function will block.
347 ssize_t mei_cldev_recv_nonblock(struct mei_cl_device *cldev, u8 *buf,
350 return mei_cldev_recv_nonblock_vtag(cldev, buf, length, NULL);
352 EXPORT_SYMBOL_GPL(mei_cldev_recv_nonblock);
355 * mei_cl_bus_rx_work - dispatch rx event for a bus device
359 static void mei_cl_bus_rx_work(struct work_struct *work)
361 struct mei_cl_device *cldev;
362 struct mei_device *bus;
364 cldev = container_of(work, struct mei_cl_device, rx_work);
371 mutex_lock(&bus->device_lock);
372 if (mei_cl_is_connected(cldev->cl))
373 mei_cl_read_start(cldev->cl, mei_cl_mtu(cldev->cl), NULL);
374 mutex_unlock(&bus->device_lock);
378 * mei_cl_bus_notif_work - dispatch FW notif event for a bus device
382 static void mei_cl_bus_notif_work(struct work_struct *work)
384 struct mei_cl_device *cldev;
386 cldev = container_of(work, struct mei_cl_device, notif_work);
389 cldev->notif_cb(cldev);
393 * mei_cl_bus_notify_event - schedule notify cb on bus client
397 * Return: true if event was scheduled
398 * false if the client is not waiting for event
400 bool mei_cl_bus_notify_event(struct mei_cl *cl)
402 struct mei_cl_device *cldev = cl->cldev;
404 if (!cldev || !cldev->notif_cb)
410 schedule_work(&cldev->notif_work);
412 cl->notify_ev = false;
418 * mei_cl_bus_rx_event - schedule rx event
422 * Return: true if event was scheduled
423 * false if the client is not waiting for event
425 bool mei_cl_bus_rx_event(struct mei_cl *cl)
427 struct mei_cl_device *cldev = cl->cldev;
429 if (!cldev || !cldev->rx_cb)
432 schedule_work(&cldev->rx_work);
438 * mei_cldev_register_rx_cb - register Rx event callback
440 * @cldev: me client devices
441 * @rx_cb: callback function
443 * Return: 0 on success
444 * -EALREADY if an callback is already registered
447 int mei_cldev_register_rx_cb(struct mei_cl_device *cldev, mei_cldev_cb_t rx_cb)
449 struct mei_device *bus = cldev->bus;
457 cldev->rx_cb = rx_cb;
458 INIT_WORK(&cldev->rx_work, mei_cl_bus_rx_work);
460 mutex_lock(&bus->device_lock);
461 if (mei_cl_is_connected(cldev->cl))
462 ret = mei_cl_read_start(cldev->cl, mei_cl_mtu(cldev->cl), NULL);
465 mutex_unlock(&bus->device_lock);
466 if (ret && ret != -EBUSY) {
467 cancel_work_sync(&cldev->rx_work);
474 EXPORT_SYMBOL_GPL(mei_cldev_register_rx_cb);
477 * mei_cldev_register_notif_cb - register FW notification event callback
479 * @cldev: me client devices
480 * @notif_cb: callback function
482 * Return: 0 on success
483 * -EALREADY if an callback is already registered
486 int mei_cldev_register_notif_cb(struct mei_cl_device *cldev,
487 mei_cldev_cb_t notif_cb)
489 struct mei_device *bus = cldev->bus;
498 cldev->notif_cb = notif_cb;
499 INIT_WORK(&cldev->notif_work, mei_cl_bus_notif_work);
501 mutex_lock(&bus->device_lock);
502 ret = mei_cl_notify_request(cldev->cl, NULL, 1);
503 mutex_unlock(&bus->device_lock);
505 cancel_work_sync(&cldev->notif_work);
506 cldev->notif_cb = NULL;
512 EXPORT_SYMBOL_GPL(mei_cldev_register_notif_cb);
515 * mei_cldev_get_drvdata - driver data getter
517 * @cldev: mei client device
519 * Return: driver private data
521 void *mei_cldev_get_drvdata(const struct mei_cl_device *cldev)
523 return dev_get_drvdata(&cldev->dev);
525 EXPORT_SYMBOL_GPL(mei_cldev_get_drvdata);
528 * mei_cldev_set_drvdata - driver data setter
530 * @cldev: mei client device
531 * @data: data to store
533 void mei_cldev_set_drvdata(struct mei_cl_device *cldev, void *data)
535 dev_set_drvdata(&cldev->dev, data);
537 EXPORT_SYMBOL_GPL(mei_cldev_set_drvdata);
540 * mei_cldev_uuid - return uuid of the underlying me client
542 * @cldev: mei client device
544 * Return: me client uuid
546 const uuid_le *mei_cldev_uuid(const struct mei_cl_device *cldev)
548 return mei_me_cl_uuid(cldev->me_cl);
550 EXPORT_SYMBOL_GPL(mei_cldev_uuid);
553 * mei_cldev_ver - return protocol version of the underlying me client
555 * @cldev: mei client device
557 * Return: me client protocol version
559 u8 mei_cldev_ver(const struct mei_cl_device *cldev)
561 return mei_me_cl_ver(cldev->me_cl);
563 EXPORT_SYMBOL_GPL(mei_cldev_ver);
566 * mei_cldev_enabled - check whether the device is enabled
568 * @cldev: mei client device
570 * Return: true if me client is initialized and connected
572 bool mei_cldev_enabled(const struct mei_cl_device *cldev)
574 return mei_cl_is_connected(cldev->cl);
576 EXPORT_SYMBOL_GPL(mei_cldev_enabled);
579 * mei_cl_bus_module_get - acquire module of the underlying
582 * @cldev: mei client device
584 * Return: true on success; false if the module was removed.
586 static bool mei_cl_bus_module_get(struct mei_cl_device *cldev)
588 return try_module_get(cldev->bus->dev->driver->owner);
592 * mei_cl_bus_module_put - release the underlying hw module.
594 * @cldev: mei client device
596 static void mei_cl_bus_module_put(struct mei_cl_device *cldev)
598 module_put(cldev->bus->dev->driver->owner);
602 * mei_cl_bus_vtag - get bus vtag entry wrapper
603 * The tag for bus client is always first.
607 * Return: bus vtag or NULL
609 static inline struct mei_cl_vtag *mei_cl_bus_vtag(struct mei_cl *cl)
611 return list_first_entry_or_null(&cl->vtag_map,
612 struct mei_cl_vtag, list);
616 * mei_cl_bus_vtag_alloc - add bus client entry to vtag map
618 * @cldev: me client device
622 * * -ENOMEM if memory allocation failed
624 static int mei_cl_bus_vtag_alloc(struct mei_cl_device *cldev)
626 struct mei_cl *cl = cldev->cl;
627 struct mei_cl_vtag *cl_vtag;
630 * Bail out if the client does not supports vtags
631 * or has already allocated one
633 if (mei_cl_vt_support_check(cl) || mei_cl_bus_vtag(cl))
636 cl_vtag = mei_cl_vtag_alloc(NULL, 0);
640 list_add_tail(&cl_vtag->list, &cl->vtag_map);
646 * mei_cl_bus_vtag_free - remove the bus entry from vtag map
648 * @cldev: me client device
650 static void mei_cl_bus_vtag_free(struct mei_cl_device *cldev)
652 struct mei_cl *cl = cldev->cl;
653 struct mei_cl_vtag *cl_vtag;
655 cl_vtag = mei_cl_bus_vtag(cl);
659 list_del(&cl_vtag->list);
663 void *mei_cldev_dma_map(struct mei_cl_device *cldev, u8 buffer_id, size_t size)
665 struct mei_device *bus;
669 if (!cldev || !buffer_id || !size)
670 return ERR_PTR(-EINVAL);
672 if (!IS_ALIGNED(size, MEI_FW_PAGE_SIZE)) {
673 dev_err(&cldev->dev, "Map size should be aligned to %lu\n",
675 return ERR_PTR(-EINVAL);
681 mutex_lock(&bus->device_lock);
682 if (cl->state == MEI_FILE_UNINITIALIZED) {
683 ret = mei_cl_link(cl);
686 /* update pointers */
690 ret = mei_cl_dma_alloc_and_map(cl, NULL, buffer_id, size);
692 mutex_unlock(&bus->device_lock);
695 return cl->dma.vaddr;
697 EXPORT_SYMBOL_GPL(mei_cldev_dma_map);
699 int mei_cldev_dma_unmap(struct mei_cl_device *cldev)
701 struct mei_device *bus;
711 mutex_lock(&bus->device_lock);
712 ret = mei_cl_dma_unmap(cl, NULL);
714 mei_cl_flush_queues(cl, NULL);
716 mutex_unlock(&bus->device_lock);
719 EXPORT_SYMBOL_GPL(mei_cldev_dma_unmap);
722 * mei_cldev_enable - enable me client device
723 * create connection with me client
725 * @cldev: me client device
727 * Return: 0 on success and < 0 on error
729 int mei_cldev_enable(struct mei_cl_device *cldev)
731 struct mei_device *bus = cldev->bus;
737 mutex_lock(&bus->device_lock);
738 if (cl->state == MEI_FILE_UNINITIALIZED) {
739 ret = mei_cl_link(cl);
742 /* update pointers */
746 if (mei_cl_is_connected(cl)) {
751 if (!mei_me_cl_is_active(cldev->me_cl)) {
752 dev_err(&cldev->dev, "me client is not active\n");
757 ret = mei_cl_bus_vtag_alloc(cldev);
761 ret = mei_cl_connect(cl, cldev->me_cl, NULL);
763 dev_err(&cldev->dev, "cannot connect\n");
764 mei_cl_bus_vtag_free(cldev);
768 mutex_unlock(&bus->device_lock);
772 EXPORT_SYMBOL_GPL(mei_cldev_enable);
775 * mei_cldev_unregister_callbacks - internal wrapper for unregistering
778 * @cldev: client device
780 static void mei_cldev_unregister_callbacks(struct mei_cl_device *cldev)
783 cancel_work_sync(&cldev->rx_work);
787 if (cldev->notif_cb) {
788 cancel_work_sync(&cldev->notif_work);
789 cldev->notif_cb = NULL;
794 * mei_cldev_disable - disable me client device
795 * disconnect form the me client
797 * @cldev: me client device
799 * Return: 0 on success and < 0 on error
801 int mei_cldev_disable(struct mei_cl_device *cldev)
803 struct mei_device *bus;
814 mei_cldev_unregister_callbacks(cldev);
816 mutex_lock(&bus->device_lock);
818 mei_cl_bus_vtag_free(cldev);
820 if (!mei_cl_is_connected(cl)) {
821 dev_dbg(bus->dev, "Already disconnected\n");
826 err = mei_cl_disconnect(cl);
828 dev_err(bus->dev, "Could not disconnect from the ME client\n");
831 /* Flush queues and remove any pending read unless we have mapped DMA */
832 if (!cl->dma_mapped) {
833 mei_cl_flush_queues(cl, NULL);
837 mutex_unlock(&bus->device_lock);
840 EXPORT_SYMBOL_GPL(mei_cldev_disable);
843 * mei_cldev_send_gsc_command - sends a gsc command, by sending
844 * a gsl mei message to gsc and receiving reply from gsc
846 * @cldev: me client device
847 * @client_id: client id to send the command to
848 * @fence_id: fence id to send the command to
849 * @sg_in: scatter gather list containing addresses for rx message buffer
850 * @total_in_len: total length of data in 'in' sg, can be less than the sum of buffers sizes
851 * @sg_out: scatter gather list containing addresses for tx message buffer
854 * * written size in bytes
857 ssize_t mei_cldev_send_gsc_command(struct mei_cl_device *cldev,
858 u8 client_id, u32 fence_id,
859 struct scatterlist *sg_in,
861 struct scatterlist *sg_out)
864 struct mei_device *bus;
867 struct mei_ext_hdr_gsc_h2f *ext_hdr;
868 size_t buf_sz = sizeof(struct mei_ext_hdr_gsc_h2f);
869 int sg_out_nents, sg_in_nents;
871 struct scatterlist *sg;
872 struct mei_ext_hdr_gsc_f2h rx_msg;
875 if (!cldev || !sg_in || !sg_out)
881 dev_dbg(bus->dev, "client_id %u, fence_id %u\n", client_id, fence_id);
883 if (!bus->hbm_f_gsc_supported)
886 sg_out_nents = sg_nents(sg_out);
887 sg_in_nents = sg_nents(sg_in);
888 /* at least one entry in tx and rx sgls must be present */
889 if (sg_out_nents <= 0 || sg_in_nents <= 0)
892 buf_sz += (sg_out_nents + sg_in_nents) * sizeof(struct mei_gsc_sgl);
893 ext_hdr = kzalloc(buf_sz, GFP_KERNEL);
897 /* construct the GSC message */
898 ext_hdr->hdr.type = MEI_EXT_HDR_GSC;
899 ext_hdr->hdr.length = buf_sz / sizeof(u32); /* length is in dw */
901 ext_hdr->client_id = client_id;
902 ext_hdr->addr_type = GSC_ADDRESS_TYPE_PHYSICAL_SGL;
903 ext_hdr->fence_id = fence_id;
904 ext_hdr->input_address_count = sg_in_nents;
905 ext_hdr->output_address_count = sg_out_nents;
906 ext_hdr->reserved[0] = 0;
907 ext_hdr->reserved[1] = 0;
909 /* copy in-sgl to the message */
910 for (i = 0, sg = sg_in; i < sg_in_nents; i++, sg++) {
911 ext_hdr->sgl[i].low = lower_32_bits(sg_dma_address(sg));
912 ext_hdr->sgl[i].high = upper_32_bits(sg_dma_address(sg));
913 sg_len = min_t(unsigned int, sg_dma_len(sg), PAGE_SIZE);
914 ext_hdr->sgl[i].length = (sg_len <= total_in_len) ? sg_len : total_in_len;
915 total_in_len -= ext_hdr->sgl[i].length;
918 /* copy out-sgl to the message */
919 for (i = sg_in_nents, sg = sg_out; i < sg_in_nents + sg_out_nents; i++, sg++) {
920 ext_hdr->sgl[i].low = lower_32_bits(sg_dma_address(sg));
921 ext_hdr->sgl[i].high = upper_32_bits(sg_dma_address(sg));
922 sg_len = min_t(unsigned int, sg_dma_len(sg), PAGE_SIZE);
923 ext_hdr->sgl[i].length = sg_len;
926 /* send the message to GSC */
927 ret = __mei_cl_send(cl, (u8 *)ext_hdr, buf_sz, 0, MEI_CL_IO_SGL);
929 dev_err(bus->dev, "__mei_cl_send failed, returned %zd\n", ret);
933 dev_err(bus->dev, "__mei_cl_send returned %zd instead of expected %zd\n",
939 /* receive the reply from GSC, note that at this point sg_in should contain the reply */
940 ret = __mei_cl_recv(cl, (u8 *)&rx_msg, sizeof(rx_msg), NULL, MEI_CL_IO_SGL, 0);
942 if (ret != sizeof(rx_msg)) {
943 dev_err(bus->dev, "__mei_cl_recv returned %zd instead of expected %zd\n",
944 ret, sizeof(rx_msg));
950 /* check rx_msg.client_id and rx_msg.fence_id match the ones we send */
951 if (rx_msg.client_id != client_id || rx_msg.fence_id != fence_id) {
952 dev_err(bus->dev, "received client_id/fence_id %u/%u instead of %u/%u sent\n",
953 rx_msg.client_id, rx_msg.fence_id, client_id, fence_id);
958 dev_dbg(bus->dev, "gsc command: successfully written %u bytes\n", rx_msg.written);
959 ret = rx_msg.written;
965 EXPORT_SYMBOL_GPL(mei_cldev_send_gsc_command);
968 * mei_cl_device_find - find matching entry in the driver id table
970 * @cldev: me client device
971 * @cldrv: me client driver
973 * Return: id on success; NULL if no id is matching
976 struct mei_cl_device_id *mei_cl_device_find(const struct mei_cl_device *cldev,
977 const struct mei_cl_driver *cldrv)
979 const struct mei_cl_device_id *id;
984 uuid = mei_me_cl_uuid(cldev->me_cl);
985 version = mei_me_cl_ver(cldev->me_cl);
987 id = cldrv->id_table;
988 while (uuid_le_cmp(NULL_UUID_LE, id->uuid)) {
989 if (!uuid_le_cmp(*uuid, id->uuid)) {
993 if (strncmp(cldev->name, id->name,
997 if (id->version != MEI_CL_VERSION_ANY)
998 if (id->version != version)
1011 * mei_cl_device_match - device match function
1016 * Return: 1 if matching device was found 0 otherwise
1018 static int mei_cl_device_match(struct device *dev, struct device_driver *drv)
1020 const struct mei_cl_device *cldev = to_mei_cl_device(dev);
1021 const struct mei_cl_driver *cldrv = to_mei_cl_driver(drv);
1022 const struct mei_cl_device_id *found_id;
1027 if (!cldev->do_match)
1030 if (!cldrv || !cldrv->id_table)
1033 found_id = mei_cl_device_find(cldev, cldrv);
1041 * mei_cl_device_probe - bus probe function
1045 * Return: 0 on success; < 0 otherwise
1047 static int mei_cl_device_probe(struct device *dev)
1049 struct mei_cl_device *cldev;
1050 struct mei_cl_driver *cldrv;
1051 const struct mei_cl_device_id *id;
1054 cldev = to_mei_cl_device(dev);
1055 cldrv = to_mei_cl_driver(dev->driver);
1060 if (!cldrv || !cldrv->probe)
1063 id = mei_cl_device_find(cldev, cldrv);
1067 if (!mei_cl_bus_module_get(cldev)) {
1068 dev_err(&cldev->dev, "get hw module failed");
1072 ret = cldrv->probe(cldev, id);
1074 mei_cl_bus_module_put(cldev);
1078 __module_get(THIS_MODULE);
1083 * mei_cl_device_remove - remove device from the bus
1087 * Return: 0 on success; < 0 otherwise
1089 static void mei_cl_device_remove(struct device *dev)
1091 struct mei_cl_device *cldev = to_mei_cl_device(dev);
1092 struct mei_cl_driver *cldrv = to_mei_cl_driver(dev->driver);
1095 cldrv->remove(cldev);
1097 mei_cldev_unregister_callbacks(cldev);
1099 mei_cl_bus_module_put(cldev);
1100 module_put(THIS_MODULE);
1103 static ssize_t name_show(struct device *dev, struct device_attribute *a,
1106 struct mei_cl_device *cldev = to_mei_cl_device(dev);
1108 return scnprintf(buf, PAGE_SIZE, "%s", cldev->name);
1110 static DEVICE_ATTR_RO(name);
1112 static ssize_t uuid_show(struct device *dev, struct device_attribute *a,
1115 struct mei_cl_device *cldev = to_mei_cl_device(dev);
1116 const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
1118 return sprintf(buf, "%pUl", uuid);
1120 static DEVICE_ATTR_RO(uuid);
1122 static ssize_t version_show(struct device *dev, struct device_attribute *a,
1125 struct mei_cl_device *cldev = to_mei_cl_device(dev);
1126 u8 version = mei_me_cl_ver(cldev->me_cl);
1128 return sprintf(buf, "%02X", version);
1130 static DEVICE_ATTR_RO(version);
1132 static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
1135 struct mei_cl_device *cldev = to_mei_cl_device(dev);
1136 const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
1137 u8 version = mei_me_cl_ver(cldev->me_cl);
1139 return scnprintf(buf, PAGE_SIZE, "mei:%s:%pUl:%02X:",
1140 cldev->name, uuid, version);
1142 static DEVICE_ATTR_RO(modalias);
1144 static ssize_t max_conn_show(struct device *dev, struct device_attribute *a,
1147 struct mei_cl_device *cldev = to_mei_cl_device(dev);
1148 u8 maxconn = mei_me_cl_max_conn(cldev->me_cl);
1150 return sprintf(buf, "%d", maxconn);
1152 static DEVICE_ATTR_RO(max_conn);
1154 static ssize_t fixed_show(struct device *dev, struct device_attribute *a,
1157 struct mei_cl_device *cldev = to_mei_cl_device(dev);
1158 u8 fixed = mei_me_cl_fixed(cldev->me_cl);
1160 return sprintf(buf, "%d", fixed);
1162 static DEVICE_ATTR_RO(fixed);
1164 static ssize_t vtag_show(struct device *dev, struct device_attribute *a,
1167 struct mei_cl_device *cldev = to_mei_cl_device(dev);
1168 bool vt = mei_me_cl_vt(cldev->me_cl);
1170 return sprintf(buf, "%d", vt);
1172 static DEVICE_ATTR_RO(vtag);
1174 static ssize_t max_len_show(struct device *dev, struct device_attribute *a,
1177 struct mei_cl_device *cldev = to_mei_cl_device(dev);
1178 u32 maxlen = mei_me_cl_max_len(cldev->me_cl);
1180 return sprintf(buf, "%u", maxlen);
1182 static DEVICE_ATTR_RO(max_len);
1184 static struct attribute *mei_cldev_attrs[] = {
1185 &dev_attr_name.attr,
1186 &dev_attr_uuid.attr,
1187 &dev_attr_version.attr,
1188 &dev_attr_modalias.attr,
1189 &dev_attr_max_conn.attr,
1190 &dev_attr_fixed.attr,
1191 &dev_attr_vtag.attr,
1192 &dev_attr_max_len.attr,
1195 ATTRIBUTE_GROUPS(mei_cldev);
1198 * mei_cl_device_uevent - me client bus uevent handler
1201 * @env: uevent kobject
1203 * Return: 0 on success -ENOMEM on when add_uevent_var fails
1205 static int mei_cl_device_uevent(struct device *dev, struct kobj_uevent_env *env)
1207 struct mei_cl_device *cldev = to_mei_cl_device(dev);
1208 const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
1209 u8 version = mei_me_cl_ver(cldev->me_cl);
1211 if (add_uevent_var(env, "MEI_CL_VERSION=%d", version))
1214 if (add_uevent_var(env, "MEI_CL_UUID=%pUl", uuid))
1217 if (add_uevent_var(env, "MEI_CL_NAME=%s", cldev->name))
1220 if (add_uevent_var(env, "MODALIAS=mei:%s:%pUl:%02X:",
1221 cldev->name, uuid, version))
1227 static struct bus_type mei_cl_bus_type = {
1229 .dev_groups = mei_cldev_groups,
1230 .match = mei_cl_device_match,
1231 .probe = mei_cl_device_probe,
1232 .remove = mei_cl_device_remove,
1233 .uevent = mei_cl_device_uevent,
1236 static struct mei_device *mei_dev_bus_get(struct mei_device *bus)
1239 get_device(bus->dev);
1244 static void mei_dev_bus_put(struct mei_device *bus)
1247 put_device(bus->dev);
1250 static void mei_cl_bus_dev_release(struct device *dev)
1252 struct mei_cl_device *cldev = to_mei_cl_device(dev);
1257 mei_cl_flush_queues(cldev->cl, NULL);
1258 mei_me_cl_put(cldev->me_cl);
1259 mei_dev_bus_put(cldev->bus);
1260 mei_cl_unlink(cldev->cl);
1265 static const struct device_type mei_cl_device_type = {
1266 .release = mei_cl_bus_dev_release,
1270 * mei_cl_bus_set_name - set device name for me client device
1271 * <controller>-<client device>
1272 * Example: 0000:00:16.0-55213584-9a29-4916-badf-0fb7ed682aeb
1274 * @cldev: me client device
1276 static inline void mei_cl_bus_set_name(struct mei_cl_device *cldev)
1278 dev_set_name(&cldev->dev, "%s-%pUl",
1279 dev_name(cldev->bus->dev),
1280 mei_me_cl_uuid(cldev->me_cl));
1284 * mei_cl_bus_dev_alloc - initialize and allocate mei client device
1289 * Return: allocated device structur or NULL on allocation failure
1291 static struct mei_cl_device *mei_cl_bus_dev_alloc(struct mei_device *bus,
1292 struct mei_me_client *me_cl)
1294 struct mei_cl_device *cldev;
1297 cldev = kzalloc(sizeof(*cldev), GFP_KERNEL);
1301 cl = mei_cl_allocate(bus);
1307 device_initialize(&cldev->dev);
1308 cldev->dev.parent = bus->dev;
1309 cldev->dev.bus = &mei_cl_bus_type;
1310 cldev->dev.type = &mei_cl_device_type;
1311 cldev->bus = mei_dev_bus_get(bus);
1312 cldev->me_cl = mei_me_cl_get(me_cl);
1314 mei_cl_bus_set_name(cldev);
1315 cldev->is_added = 0;
1316 INIT_LIST_HEAD(&cldev->bus_list);
1322 * mei_cl_bus_dev_setup - setup me client device
1323 * run fix up routines and set the device name
1326 * @cldev: me client device
1328 * Return: true if the device is eligible for enumeration
1330 static bool mei_cl_bus_dev_setup(struct mei_device *bus,
1331 struct mei_cl_device *cldev)
1333 cldev->do_match = 1;
1334 mei_cl_bus_dev_fixup(cldev);
1336 /* the device name can change during fix up */
1337 if (cldev->do_match)
1338 mei_cl_bus_set_name(cldev);
1340 return cldev->do_match == 1;
1344 * mei_cl_bus_dev_add - add me client devices
1346 * @cldev: me client device
1348 * Return: 0 on success; < 0 on failre
1350 static int mei_cl_bus_dev_add(struct mei_cl_device *cldev)
1354 dev_dbg(cldev->bus->dev, "adding %pUL:%02X\n",
1355 mei_me_cl_uuid(cldev->me_cl),
1356 mei_me_cl_ver(cldev->me_cl));
1357 ret = device_add(&cldev->dev);
1359 cldev->is_added = 1;
1365 * mei_cl_bus_dev_stop - stop the driver
1367 * @cldev: me client device
1369 static void mei_cl_bus_dev_stop(struct mei_cl_device *cldev)
1371 if (cldev->is_added)
1372 device_release_driver(&cldev->dev);
1376 * mei_cl_bus_dev_destroy - destroy me client devices object
1378 * @cldev: me client device
1380 * Locking: called under "dev->cl_bus_lock" lock
1382 static void mei_cl_bus_dev_destroy(struct mei_cl_device *cldev)
1385 WARN_ON(!mutex_is_locked(&cldev->bus->cl_bus_lock));
1387 if (!cldev->is_added)
1390 device_del(&cldev->dev);
1392 list_del_init(&cldev->bus_list);
1394 cldev->is_added = 0;
1395 put_device(&cldev->dev);
1399 * mei_cl_bus_remove_device - remove a devices form the bus
1401 * @cldev: me client device
1403 static void mei_cl_bus_remove_device(struct mei_cl_device *cldev)
1405 mei_cl_bus_dev_stop(cldev);
1406 mei_cl_bus_dev_destroy(cldev);
1410 * mei_cl_bus_remove_devices - remove all devices form the bus
1414 void mei_cl_bus_remove_devices(struct mei_device *bus)
1416 struct mei_cl_device *cldev, *next;
1418 mutex_lock(&bus->cl_bus_lock);
1419 list_for_each_entry_safe(cldev, next, &bus->device_list, bus_list)
1420 mei_cl_bus_remove_device(cldev);
1421 mutex_unlock(&bus->cl_bus_lock);
1426 * mei_cl_bus_dev_init - allocate and initializes an mei client devices
1427 * based on me client
1432 * Locking: called under "dev->cl_bus_lock" lock
1434 static void mei_cl_bus_dev_init(struct mei_device *bus,
1435 struct mei_me_client *me_cl)
1437 struct mei_cl_device *cldev;
1439 WARN_ON(!mutex_is_locked(&bus->cl_bus_lock));
1441 dev_dbg(bus->dev, "initializing %pUl", mei_me_cl_uuid(me_cl));
1443 if (me_cl->bus_added)
1446 cldev = mei_cl_bus_dev_alloc(bus, me_cl);
1450 me_cl->bus_added = true;
1451 list_add_tail(&cldev->bus_list, &bus->device_list);
1456 * mei_cl_bus_rescan - scan me clients list and add create
1457 * devices for eligible clients
1461 static void mei_cl_bus_rescan(struct mei_device *bus)
1463 struct mei_cl_device *cldev, *n;
1464 struct mei_me_client *me_cl;
1466 mutex_lock(&bus->cl_bus_lock);
1468 down_read(&bus->me_clients_rwsem);
1469 list_for_each_entry(me_cl, &bus->me_clients, list)
1470 mei_cl_bus_dev_init(bus, me_cl);
1471 up_read(&bus->me_clients_rwsem);
1473 list_for_each_entry_safe(cldev, n, &bus->device_list, bus_list) {
1475 if (!mei_me_cl_is_active(cldev->me_cl)) {
1476 mei_cl_bus_remove_device(cldev);
1480 if (cldev->is_added)
1483 if (mei_cl_bus_dev_setup(bus, cldev))
1484 mei_cl_bus_dev_add(cldev);
1486 list_del_init(&cldev->bus_list);
1487 put_device(&cldev->dev);
1490 mutex_unlock(&bus->cl_bus_lock);
1492 dev_dbg(bus->dev, "rescan end");
1495 void mei_cl_bus_rescan_work(struct work_struct *work)
1497 struct mei_device *bus =
1498 container_of(work, struct mei_device, bus_rescan_work);
1500 mei_cl_bus_rescan(bus);
1503 int __mei_cldev_driver_register(struct mei_cl_driver *cldrv,
1504 struct module *owner)
1508 cldrv->driver.name = cldrv->name;
1509 cldrv->driver.owner = owner;
1510 cldrv->driver.bus = &mei_cl_bus_type;
1512 err = driver_register(&cldrv->driver);
1516 pr_debug("mei: driver [%s] registered\n", cldrv->driver.name);
1520 EXPORT_SYMBOL_GPL(__mei_cldev_driver_register);
1522 void mei_cldev_driver_unregister(struct mei_cl_driver *cldrv)
1524 driver_unregister(&cldrv->driver);
1526 pr_debug("mei: driver [%s] unregistered\n", cldrv->driver.name);
1528 EXPORT_SYMBOL_GPL(mei_cldev_driver_unregister);
1531 int __init mei_cl_bus_init(void)
1533 return bus_register(&mei_cl_bus_type);
1536 void __exit mei_cl_bus_exit(void)
1538 bus_unregister(&mei_cl_bus_type);