1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2012-2023, 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 return __mei_cl_send_timeout(cl, buf, length, vtag, mode, MAX_SCHEDULE_TIMEOUT);
42 * __mei_cl_send_timeout - internal client send (write)
45 * @buf: buffer to send
46 * @length: buffer length
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.
53 * Return: written size bytes or < 0 on error
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)
58 struct mei_device *bus;
62 if (WARN_ON(!cl || !cl->dev))
67 mutex_lock(&bus->device_lock);
68 if (bus->dev_state != MEI_DEV_ENABLED &&
69 bus->dev_state != MEI_DEV_POWERING_DOWN) {
74 if (!mei_cl_is_connected(cl)) {
79 /* Check if we have an ME client device */
80 if (!mei_me_cl_is_active(cl->me_cl)) {
86 /* Check if vtag is supported by client */
87 rets = mei_cl_vt_support_check(cl);
92 if (length > mei_cl_mtu(cl)) {
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);
104 if (signal_pending(current))
108 if (!mei_cl_is_connected(cl)) {
114 cb = mei_cl_alloc_cb(cl, length, MEI_FOP_WRITE, NULL);
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;
131 rets = mei_cl_write(cl, cb, timeout);
133 if (mode & MEI_CL_IO_SGL && rets == 0)
137 mutex_unlock(&bus->device_lock);
143 * __mei_cl_recv - internal client receive (read)
146 * @buf: buffer to receive
147 * @length: buffer length
150 * @timeout: recv timeout, 0 for infinite timeout
152 * Return: read size in bytes of < 0 on error
154 ssize_t __mei_cl_recv(struct mei_cl *cl, u8 *buf, size_t length, u8 *vtag,
155 unsigned int mode, unsigned long timeout)
157 struct mei_device *bus;
158 struct mei_cl_cb *cb;
161 bool nonblock = !!(mode & MEI_CL_IO_RX_NONBLOCK);
163 if (WARN_ON(!cl || !cl->dev))
168 mutex_lock(&bus->device_lock);
169 if (bus->dev_state != MEI_DEV_ENABLED &&
170 bus->dev_state != MEI_DEV_POWERING_DOWN) {
175 cb = mei_cl_read_cb(cl, NULL);
179 rets = mei_cl_read_start(cl, length, NULL);
180 if (rets && rets != -EBUSY)
188 /* wait on event only if there is no other waiter */
189 /* synchronized under device mutex */
190 if (!waitqueue_active(&cl->rx_wait)) {
192 mutex_unlock(&bus->device_lock);
195 rets = wait_event_interruptible_timeout
197 mei_cl_read_cb(cl, NULL) ||
198 (!mei_cl_is_connected(cl)),
199 msecs_to_jiffies(timeout));
203 if (signal_pending(current))
208 if (wait_event_interruptible
210 mei_cl_read_cb(cl, NULL) ||
211 (!mei_cl_is_connected(cl)))) {
212 if (signal_pending(current))
218 mutex_lock(&bus->device_lock);
220 if (!mei_cl_is_connected(cl)) {
226 cb = mei_cl_read_cb(cl, NULL);
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);
243 r_length = min_t(size_t, length, cb->buf_idx);
244 memcpy(buf, cb->buf.data, r_length);
252 mei_cl_del_rd_completed(cl, cb);
254 mutex_unlock(&bus->device_lock);
260 * mei_cldev_send_vtag - me device send with vtag (write)
262 * @cldev: me client device
263 * @buf: buffer to send
264 * @length: buffer length
268 * * written size in bytes
272 ssize_t mei_cldev_send_vtag(struct mei_cl_device *cldev, const u8 *buf,
273 size_t length, u8 vtag)
275 struct mei_cl *cl = cldev->cl;
277 return __mei_cl_send(cl, buf, length, vtag, MEI_CL_IO_TX_BLOCKING);
279 EXPORT_SYMBOL_GPL(mei_cldev_send_vtag);
282 * mei_cldev_recv_vtag - client receive with vtag (read)
284 * @cldev: me client device
285 * @buf: buffer to receive
286 * @length: buffer length
290 * * read size in bytes
294 ssize_t mei_cldev_recv_vtag(struct mei_cl_device *cldev, u8 *buf, size_t length,
297 struct mei_cl *cl = cldev->cl;
299 return __mei_cl_recv(cl, buf, length, vtag, 0, 0);
301 EXPORT_SYMBOL_GPL(mei_cldev_recv_vtag);
304 * mei_cldev_recv_nonblock_vtag - non block client receive with vtag (read)
306 * @cldev: me client device
307 * @buf: buffer to receive
308 * @length: buffer length
312 * * read size in bytes
313 * * -EAGAIN if function will block.
314 * * < 0 on other error
316 ssize_t mei_cldev_recv_nonblock_vtag(struct mei_cl_device *cldev, u8 *buf,
317 size_t length, u8 *vtag)
319 struct mei_cl *cl = cldev->cl;
321 return __mei_cl_recv(cl, buf, length, vtag, MEI_CL_IO_RX_NONBLOCK, 0);
323 EXPORT_SYMBOL_GPL(mei_cldev_recv_nonblock_vtag);
326 * mei_cldev_send - me device send (write)
328 * @cldev: me client device
329 * @buf: buffer to send
330 * @length: buffer length
333 * * written size in bytes
336 ssize_t mei_cldev_send(struct mei_cl_device *cldev, const u8 *buf, size_t length)
338 return mei_cldev_send_vtag(cldev, buf, length, 0);
340 EXPORT_SYMBOL_GPL(mei_cldev_send);
343 * mei_cldev_recv - client receive (read)
345 * @cldev: me client device
346 * @buf: buffer to receive
347 * @length: buffer length
349 * Return: read size in bytes of < 0 on error
351 ssize_t mei_cldev_recv(struct mei_cl_device *cldev, u8 *buf, size_t length)
353 return mei_cldev_recv_vtag(cldev, buf, length, NULL);
355 EXPORT_SYMBOL_GPL(mei_cldev_recv);
358 * mei_cldev_recv_nonblock - non block client receive (read)
360 * @cldev: me client device
361 * @buf: buffer to receive
362 * @length: buffer length
364 * Return: read size in bytes of < 0 on error
365 * -EAGAIN if function will block.
367 ssize_t mei_cldev_recv_nonblock(struct mei_cl_device *cldev, u8 *buf,
370 return mei_cldev_recv_nonblock_vtag(cldev, buf, length, NULL);
372 EXPORT_SYMBOL_GPL(mei_cldev_recv_nonblock);
375 * mei_cl_bus_rx_work - dispatch rx event for a bus device
379 static void mei_cl_bus_rx_work(struct work_struct *work)
381 struct mei_cl_device *cldev;
382 struct mei_device *bus;
384 cldev = container_of(work, struct mei_cl_device, rx_work);
391 mutex_lock(&bus->device_lock);
392 if (mei_cl_is_connected(cldev->cl))
393 mei_cl_read_start(cldev->cl, mei_cl_mtu(cldev->cl), NULL);
394 mutex_unlock(&bus->device_lock);
398 * mei_cl_bus_notif_work - dispatch FW notif event for a bus device
402 static void mei_cl_bus_notif_work(struct work_struct *work)
404 struct mei_cl_device *cldev;
406 cldev = container_of(work, struct mei_cl_device, notif_work);
409 cldev->notif_cb(cldev);
413 * mei_cl_bus_notify_event - schedule notify cb on bus client
417 * Return: true if event was scheduled
418 * false if the client is not waiting for event
420 bool mei_cl_bus_notify_event(struct mei_cl *cl)
422 struct mei_cl_device *cldev = cl->cldev;
424 if (!cldev || !cldev->notif_cb)
430 schedule_work(&cldev->notif_work);
432 cl->notify_ev = false;
438 * mei_cl_bus_rx_event - schedule rx event
442 * Return: true if event was scheduled
443 * false if the client is not waiting for event
445 bool mei_cl_bus_rx_event(struct mei_cl *cl)
447 struct mei_cl_device *cldev = cl->cldev;
449 if (!cldev || !cldev->rx_cb)
452 schedule_work(&cldev->rx_work);
458 * mei_cldev_register_rx_cb - register Rx event callback
460 * @cldev: me client devices
461 * @rx_cb: callback function
463 * Return: 0 on success
464 * -EALREADY if an callback is already registered
467 int mei_cldev_register_rx_cb(struct mei_cl_device *cldev, mei_cldev_cb_t rx_cb)
469 struct mei_device *bus = cldev->bus;
477 cldev->rx_cb = rx_cb;
478 INIT_WORK(&cldev->rx_work, mei_cl_bus_rx_work);
480 mutex_lock(&bus->device_lock);
481 if (mei_cl_is_connected(cldev->cl))
482 ret = mei_cl_read_start(cldev->cl, mei_cl_mtu(cldev->cl), NULL);
485 mutex_unlock(&bus->device_lock);
486 if (ret && ret != -EBUSY) {
487 cancel_work_sync(&cldev->rx_work);
494 EXPORT_SYMBOL_GPL(mei_cldev_register_rx_cb);
497 * mei_cldev_register_notif_cb - register FW notification event callback
499 * @cldev: me client devices
500 * @notif_cb: callback function
502 * Return: 0 on success
503 * -EALREADY if an callback is already registered
506 int mei_cldev_register_notif_cb(struct mei_cl_device *cldev,
507 mei_cldev_cb_t notif_cb)
509 struct mei_device *bus = cldev->bus;
518 cldev->notif_cb = notif_cb;
519 INIT_WORK(&cldev->notif_work, mei_cl_bus_notif_work);
521 mutex_lock(&bus->device_lock);
522 ret = mei_cl_notify_request(cldev->cl, NULL, 1);
523 mutex_unlock(&bus->device_lock);
525 cancel_work_sync(&cldev->notif_work);
526 cldev->notif_cb = NULL;
532 EXPORT_SYMBOL_GPL(mei_cldev_register_notif_cb);
535 * mei_cldev_get_drvdata - driver data getter
537 * @cldev: mei client device
539 * Return: driver private data
541 void *mei_cldev_get_drvdata(const struct mei_cl_device *cldev)
543 return dev_get_drvdata(&cldev->dev);
545 EXPORT_SYMBOL_GPL(mei_cldev_get_drvdata);
548 * mei_cldev_set_drvdata - driver data setter
550 * @cldev: mei client device
551 * @data: data to store
553 void mei_cldev_set_drvdata(struct mei_cl_device *cldev, void *data)
555 dev_set_drvdata(&cldev->dev, data);
557 EXPORT_SYMBOL_GPL(mei_cldev_set_drvdata);
560 * mei_cldev_uuid - return uuid of the underlying me client
562 * @cldev: mei client device
564 * Return: me client uuid
566 const uuid_le *mei_cldev_uuid(const struct mei_cl_device *cldev)
568 return mei_me_cl_uuid(cldev->me_cl);
570 EXPORT_SYMBOL_GPL(mei_cldev_uuid);
573 * mei_cldev_ver - return protocol version of the underlying me client
575 * @cldev: mei client device
577 * Return: me client protocol version
579 u8 mei_cldev_ver(const struct mei_cl_device *cldev)
581 return mei_me_cl_ver(cldev->me_cl);
583 EXPORT_SYMBOL_GPL(mei_cldev_ver);
586 * mei_cldev_enabled - check whether the device is enabled
588 * @cldev: mei client device
590 * Return: true if me client is initialized and connected
592 bool mei_cldev_enabled(const struct mei_cl_device *cldev)
594 return mei_cl_is_connected(cldev->cl);
596 EXPORT_SYMBOL_GPL(mei_cldev_enabled);
599 * mei_cl_bus_module_get - acquire module of the underlying
602 * @cldev: mei client device
604 * Return: true on success; false if the module was removed.
606 static bool mei_cl_bus_module_get(struct mei_cl_device *cldev)
608 return try_module_get(cldev->bus->dev->driver->owner);
612 * mei_cl_bus_module_put - release the underlying hw module.
614 * @cldev: mei client device
616 static void mei_cl_bus_module_put(struct mei_cl_device *cldev)
618 module_put(cldev->bus->dev->driver->owner);
622 * mei_cl_bus_vtag - get bus vtag entry wrapper
623 * The tag for bus client is always first.
627 * Return: bus vtag or NULL
629 static inline struct mei_cl_vtag *mei_cl_bus_vtag(struct mei_cl *cl)
631 return list_first_entry_or_null(&cl->vtag_map,
632 struct mei_cl_vtag, list);
636 * mei_cl_bus_vtag_alloc - add bus client entry to vtag map
638 * @cldev: me client device
642 * * -ENOMEM if memory allocation failed
644 static int mei_cl_bus_vtag_alloc(struct mei_cl_device *cldev)
646 struct mei_cl *cl = cldev->cl;
647 struct mei_cl_vtag *cl_vtag;
650 * Bail out if the client does not supports vtags
651 * or has already allocated one
653 if (mei_cl_vt_support_check(cl) || mei_cl_bus_vtag(cl))
656 cl_vtag = mei_cl_vtag_alloc(NULL, 0);
660 list_add_tail(&cl_vtag->list, &cl->vtag_map);
666 * mei_cl_bus_vtag_free - remove the bus entry from vtag map
668 * @cldev: me client device
670 static void mei_cl_bus_vtag_free(struct mei_cl_device *cldev)
672 struct mei_cl *cl = cldev->cl;
673 struct mei_cl_vtag *cl_vtag;
675 cl_vtag = mei_cl_bus_vtag(cl);
679 list_del(&cl_vtag->list);
683 void *mei_cldev_dma_map(struct mei_cl_device *cldev, u8 buffer_id, size_t size)
685 struct mei_device *bus;
689 if (!cldev || !buffer_id || !size)
690 return ERR_PTR(-EINVAL);
692 if (!IS_ALIGNED(size, MEI_FW_PAGE_SIZE)) {
693 dev_err(&cldev->dev, "Map size should be aligned to %lu\n",
695 return ERR_PTR(-EINVAL);
701 mutex_lock(&bus->device_lock);
702 if (cl->state == MEI_FILE_UNINITIALIZED) {
703 ret = mei_cl_link(cl);
706 /* update pointers */
710 ret = mei_cl_dma_alloc_and_map(cl, NULL, buffer_id, size);
714 mutex_unlock(&bus->device_lock);
717 return cl->dma.vaddr;
719 EXPORT_SYMBOL_GPL(mei_cldev_dma_map);
721 int mei_cldev_dma_unmap(struct mei_cl_device *cldev)
723 struct mei_device *bus;
733 mutex_lock(&bus->device_lock);
734 ret = mei_cl_dma_unmap(cl, NULL);
736 mei_cl_flush_queues(cl, NULL);
738 mutex_unlock(&bus->device_lock);
741 EXPORT_SYMBOL_GPL(mei_cldev_dma_unmap);
744 * mei_cldev_enable - enable me client device
745 * create connection with me client
747 * @cldev: me client device
749 * Return: 0 on success and < 0 on error
751 int mei_cldev_enable(struct mei_cl_device *cldev)
753 struct mei_device *bus = cldev->bus;
759 mutex_lock(&bus->device_lock);
760 if (cl->state == MEI_FILE_UNINITIALIZED) {
761 ret = mei_cl_link(cl);
764 /* update pointers */
768 if (mei_cl_is_connected(cl)) {
773 if (!mei_me_cl_is_active(cldev->me_cl)) {
774 dev_err(&cldev->dev, "me client is not active\n");
779 ret = mei_cl_bus_vtag_alloc(cldev);
783 ret = mei_cl_connect(cl, cldev->me_cl, NULL);
785 dev_err(&cldev->dev, "cannot connect\n");
786 mei_cl_bus_vtag_free(cldev);
793 mutex_unlock(&bus->device_lock);
797 EXPORT_SYMBOL_GPL(mei_cldev_enable);
800 * mei_cldev_unregister_callbacks - internal wrapper for unregistering
803 * @cldev: client device
805 static void mei_cldev_unregister_callbacks(struct mei_cl_device *cldev)
808 cancel_work_sync(&cldev->rx_work);
812 if (cldev->notif_cb) {
813 cancel_work_sync(&cldev->notif_work);
814 cldev->notif_cb = NULL;
819 * mei_cldev_disable - disable me client device
820 * disconnect form the me client
822 * @cldev: me client device
824 * Return: 0 on success and < 0 on error
826 int mei_cldev_disable(struct mei_cl_device *cldev)
828 struct mei_device *bus;
839 mei_cldev_unregister_callbacks(cldev);
841 mutex_lock(&bus->device_lock);
843 mei_cl_bus_vtag_free(cldev);
845 if (!mei_cl_is_connected(cl)) {
846 dev_dbg(bus->dev, "Already disconnected\n");
851 err = mei_cl_disconnect(cl);
853 dev_err(bus->dev, "Could not disconnect from the ME client\n");
856 /* Flush queues and remove any pending read unless we have mapped DMA */
857 if (!cl->dma_mapped) {
858 mei_cl_flush_queues(cl, NULL);
862 mutex_unlock(&bus->device_lock);
865 EXPORT_SYMBOL_GPL(mei_cldev_disable);
868 * mei_cldev_send_gsc_command - sends a gsc command, by sending
869 * a gsl mei message to gsc and receiving reply from gsc
871 * @cldev: me client device
872 * @client_id: client id to send the command to
873 * @fence_id: fence id to send the command to
874 * @sg_in: scatter gather list containing addresses for rx message buffer
875 * @total_in_len: total length of data in 'in' sg, can be less than the sum of buffers sizes
876 * @sg_out: scatter gather list containing addresses for tx message buffer
879 * * written size in bytes
882 ssize_t mei_cldev_send_gsc_command(struct mei_cl_device *cldev,
883 u8 client_id, u32 fence_id,
884 struct scatterlist *sg_in,
886 struct scatterlist *sg_out)
889 struct mei_device *bus;
892 struct mei_ext_hdr_gsc_h2f *ext_hdr;
893 size_t buf_sz = sizeof(struct mei_ext_hdr_gsc_h2f);
894 int sg_out_nents, sg_in_nents;
896 struct scatterlist *sg;
897 struct mei_ext_hdr_gsc_f2h rx_msg;
900 if (!cldev || !sg_in || !sg_out)
906 dev_dbg(bus->dev, "client_id %u, fence_id %u\n", client_id, fence_id);
908 if (!bus->hbm_f_gsc_supported)
911 sg_out_nents = sg_nents(sg_out);
912 sg_in_nents = sg_nents(sg_in);
913 /* at least one entry in tx and rx sgls must be present */
914 if (sg_out_nents <= 0 || sg_in_nents <= 0)
917 buf_sz += (sg_out_nents + sg_in_nents) * sizeof(struct mei_gsc_sgl);
918 ext_hdr = kzalloc(buf_sz, GFP_KERNEL);
922 /* construct the GSC message */
923 ext_hdr->hdr.type = MEI_EXT_HDR_GSC;
924 ext_hdr->hdr.length = buf_sz / sizeof(u32); /* length is in dw */
926 ext_hdr->client_id = client_id;
927 ext_hdr->addr_type = GSC_ADDRESS_TYPE_PHYSICAL_SGL;
928 ext_hdr->fence_id = fence_id;
929 ext_hdr->input_address_count = sg_in_nents;
930 ext_hdr->output_address_count = sg_out_nents;
931 ext_hdr->reserved[0] = 0;
932 ext_hdr->reserved[1] = 0;
934 /* copy in-sgl to the message */
935 for (i = 0, sg = sg_in; i < sg_in_nents; i++, sg++) {
936 ext_hdr->sgl[i].low = lower_32_bits(sg_dma_address(sg));
937 ext_hdr->sgl[i].high = upper_32_bits(sg_dma_address(sg));
938 sg_len = min_t(unsigned int, sg_dma_len(sg), PAGE_SIZE);
939 ext_hdr->sgl[i].length = (sg_len <= total_in_len) ? sg_len : total_in_len;
940 total_in_len -= ext_hdr->sgl[i].length;
943 /* copy out-sgl to the message */
944 for (i = sg_in_nents, sg = sg_out; i < sg_in_nents + sg_out_nents; i++, sg++) {
945 ext_hdr->sgl[i].low = lower_32_bits(sg_dma_address(sg));
946 ext_hdr->sgl[i].high = upper_32_bits(sg_dma_address(sg));
947 sg_len = min_t(unsigned int, sg_dma_len(sg), PAGE_SIZE);
948 ext_hdr->sgl[i].length = sg_len;
951 /* send the message to GSC */
952 ret = __mei_cl_send(cl, (u8 *)ext_hdr, buf_sz, 0, MEI_CL_IO_SGL);
954 dev_err(bus->dev, "__mei_cl_send failed, returned %zd\n", ret);
958 dev_err(bus->dev, "__mei_cl_send returned %zd instead of expected %zd\n",
964 /* receive the reply from GSC, note that at this point sg_in should contain the reply */
965 ret = __mei_cl_recv(cl, (u8 *)&rx_msg, sizeof(rx_msg), NULL, MEI_CL_IO_SGL, 0);
967 if (ret != sizeof(rx_msg)) {
968 dev_err(bus->dev, "__mei_cl_recv returned %zd instead of expected %zd\n",
969 ret, sizeof(rx_msg));
975 /* check rx_msg.client_id and rx_msg.fence_id match the ones we send */
976 if (rx_msg.client_id != client_id || rx_msg.fence_id != fence_id) {
977 dev_err(bus->dev, "received client_id/fence_id %u/%u instead of %u/%u sent\n",
978 rx_msg.client_id, rx_msg.fence_id, client_id, fence_id);
983 dev_dbg(bus->dev, "gsc command: successfully written %u bytes\n", rx_msg.written);
984 ret = rx_msg.written;
990 EXPORT_SYMBOL_GPL(mei_cldev_send_gsc_command);
993 * mei_cl_device_find - find matching entry in the driver id table
995 * @cldev: me client device
996 * @cldrv: me client driver
998 * Return: id on success; NULL if no id is matching
1001 struct mei_cl_device_id *mei_cl_device_find(const struct mei_cl_device *cldev,
1002 const struct mei_cl_driver *cldrv)
1004 const struct mei_cl_device_id *id;
1005 const uuid_le *uuid;
1009 uuid = mei_me_cl_uuid(cldev->me_cl);
1010 version = mei_me_cl_ver(cldev->me_cl);
1012 id = cldrv->id_table;
1013 while (uuid_le_cmp(NULL_UUID_LE, id->uuid)) {
1014 if (!uuid_le_cmp(*uuid, id->uuid)) {
1018 if (strncmp(cldev->name, id->name,
1022 if (id->version != MEI_CL_VERSION_ANY)
1023 if (id->version != version)
1036 * mei_cl_device_match - device match function
1041 * Return: 1 if matching device was found 0 otherwise
1043 static int mei_cl_device_match(struct device *dev, struct device_driver *drv)
1045 const struct mei_cl_device *cldev = to_mei_cl_device(dev);
1046 const struct mei_cl_driver *cldrv = to_mei_cl_driver(drv);
1047 const struct mei_cl_device_id *found_id;
1052 if (!cldev->do_match)
1055 if (!cldrv || !cldrv->id_table)
1058 found_id = mei_cl_device_find(cldev, cldrv);
1066 * mei_cl_device_probe - bus probe function
1070 * Return: 0 on success; < 0 otherwise
1072 static int mei_cl_device_probe(struct device *dev)
1074 struct mei_cl_device *cldev;
1075 struct mei_cl_driver *cldrv;
1076 const struct mei_cl_device_id *id;
1079 cldev = to_mei_cl_device(dev);
1080 cldrv = to_mei_cl_driver(dev->driver);
1085 if (!cldrv || !cldrv->probe)
1088 id = mei_cl_device_find(cldev, cldrv);
1092 if (!mei_cl_bus_module_get(cldev)) {
1093 dev_err(&cldev->dev, "get hw module failed");
1097 ret = cldrv->probe(cldev, id);
1099 mei_cl_bus_module_put(cldev);
1103 __module_get(THIS_MODULE);
1108 * mei_cl_device_remove - remove device from the bus
1112 * Return: 0 on success; < 0 otherwise
1114 static void mei_cl_device_remove(struct device *dev)
1116 struct mei_cl_device *cldev = to_mei_cl_device(dev);
1117 struct mei_cl_driver *cldrv = to_mei_cl_driver(dev->driver);
1120 cldrv->remove(cldev);
1122 mei_cldev_unregister_callbacks(cldev);
1124 mei_cl_bus_module_put(cldev);
1125 module_put(THIS_MODULE);
1128 static ssize_t name_show(struct device *dev, struct device_attribute *a,
1131 struct mei_cl_device *cldev = to_mei_cl_device(dev);
1133 return scnprintf(buf, PAGE_SIZE, "%s", cldev->name);
1135 static DEVICE_ATTR_RO(name);
1137 static ssize_t uuid_show(struct device *dev, struct device_attribute *a,
1140 struct mei_cl_device *cldev = to_mei_cl_device(dev);
1141 const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
1143 return sprintf(buf, "%pUl", uuid);
1145 static DEVICE_ATTR_RO(uuid);
1147 static ssize_t version_show(struct device *dev, struct device_attribute *a,
1150 struct mei_cl_device *cldev = to_mei_cl_device(dev);
1151 u8 version = mei_me_cl_ver(cldev->me_cl);
1153 return sprintf(buf, "%02X", version);
1155 static DEVICE_ATTR_RO(version);
1157 static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
1160 struct mei_cl_device *cldev = to_mei_cl_device(dev);
1161 const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
1162 u8 version = mei_me_cl_ver(cldev->me_cl);
1164 return scnprintf(buf, PAGE_SIZE, "mei:%s:%pUl:%02X:",
1165 cldev->name, uuid, version);
1167 static DEVICE_ATTR_RO(modalias);
1169 static ssize_t max_conn_show(struct device *dev, struct device_attribute *a,
1172 struct mei_cl_device *cldev = to_mei_cl_device(dev);
1173 u8 maxconn = mei_me_cl_max_conn(cldev->me_cl);
1175 return sprintf(buf, "%d", maxconn);
1177 static DEVICE_ATTR_RO(max_conn);
1179 static ssize_t fixed_show(struct device *dev, struct device_attribute *a,
1182 struct mei_cl_device *cldev = to_mei_cl_device(dev);
1183 u8 fixed = mei_me_cl_fixed(cldev->me_cl);
1185 return sprintf(buf, "%d", fixed);
1187 static DEVICE_ATTR_RO(fixed);
1189 static ssize_t vtag_show(struct device *dev, struct device_attribute *a,
1192 struct mei_cl_device *cldev = to_mei_cl_device(dev);
1193 bool vt = mei_me_cl_vt(cldev->me_cl);
1195 return sprintf(buf, "%d", vt);
1197 static DEVICE_ATTR_RO(vtag);
1199 static ssize_t max_len_show(struct device *dev, struct device_attribute *a,
1202 struct mei_cl_device *cldev = to_mei_cl_device(dev);
1203 u32 maxlen = mei_me_cl_max_len(cldev->me_cl);
1205 return sprintf(buf, "%u", maxlen);
1207 static DEVICE_ATTR_RO(max_len);
1209 static struct attribute *mei_cldev_attrs[] = {
1210 &dev_attr_name.attr,
1211 &dev_attr_uuid.attr,
1212 &dev_attr_version.attr,
1213 &dev_attr_modalias.attr,
1214 &dev_attr_max_conn.attr,
1215 &dev_attr_fixed.attr,
1216 &dev_attr_vtag.attr,
1217 &dev_attr_max_len.attr,
1220 ATTRIBUTE_GROUPS(mei_cldev);
1223 * mei_cl_device_uevent - me client bus uevent handler
1226 * @env: uevent kobject
1228 * Return: 0 on success -ENOMEM on when add_uevent_var fails
1230 static int mei_cl_device_uevent(const struct device *dev, struct kobj_uevent_env *env)
1232 const struct mei_cl_device *cldev = to_mei_cl_device(dev);
1233 const uuid_le *uuid = mei_me_cl_uuid(cldev->me_cl);
1234 u8 version = mei_me_cl_ver(cldev->me_cl);
1236 if (add_uevent_var(env, "MEI_CL_VERSION=%d", version))
1239 if (add_uevent_var(env, "MEI_CL_UUID=%pUl", uuid))
1242 if (add_uevent_var(env, "MEI_CL_NAME=%s", cldev->name))
1245 if (add_uevent_var(env, "MODALIAS=mei:%s:%pUl:%02X:",
1246 cldev->name, uuid, version))
1252 static struct bus_type mei_cl_bus_type = {
1254 .dev_groups = mei_cldev_groups,
1255 .match = mei_cl_device_match,
1256 .probe = mei_cl_device_probe,
1257 .remove = mei_cl_device_remove,
1258 .uevent = mei_cl_device_uevent,
1261 static struct mei_device *mei_dev_bus_get(struct mei_device *bus)
1264 get_device(bus->dev);
1269 static void mei_dev_bus_put(struct mei_device *bus)
1272 put_device(bus->dev);
1275 static void mei_cl_bus_dev_release(struct device *dev)
1277 struct mei_cl_device *cldev = to_mei_cl_device(dev);
1282 mei_cl_flush_queues(cldev->cl, NULL);
1283 mei_me_cl_put(cldev->me_cl);
1284 mei_dev_bus_put(cldev->bus);
1289 static const struct device_type mei_cl_device_type = {
1290 .release = mei_cl_bus_dev_release,
1294 * mei_cl_bus_set_name - set device name for me client device
1295 * <controller>-<client device>
1296 * Example: 0000:00:16.0-55213584-9a29-4916-badf-0fb7ed682aeb
1298 * @cldev: me client device
1300 static inline void mei_cl_bus_set_name(struct mei_cl_device *cldev)
1302 dev_set_name(&cldev->dev, "%s-%pUl",
1303 dev_name(cldev->bus->dev),
1304 mei_me_cl_uuid(cldev->me_cl));
1308 * mei_cl_bus_dev_alloc - initialize and allocate mei client device
1313 * Return: allocated device structur or NULL on allocation failure
1315 static struct mei_cl_device *mei_cl_bus_dev_alloc(struct mei_device *bus,
1316 struct mei_me_client *me_cl)
1318 struct mei_cl_device *cldev;
1321 cldev = kzalloc(sizeof(*cldev), GFP_KERNEL);
1325 cl = mei_cl_allocate(bus);
1331 device_initialize(&cldev->dev);
1332 cldev->dev.parent = bus->dev;
1333 cldev->dev.bus = &mei_cl_bus_type;
1334 cldev->dev.type = &mei_cl_device_type;
1335 cldev->bus = mei_dev_bus_get(bus);
1336 cldev->me_cl = mei_me_cl_get(me_cl);
1338 mei_cl_bus_set_name(cldev);
1339 cldev->is_added = 0;
1340 INIT_LIST_HEAD(&cldev->bus_list);
1346 * mei_cl_bus_dev_setup - setup me client device
1347 * run fix up routines and set the device name
1350 * @cldev: me client device
1352 * Return: true if the device is eligible for enumeration
1354 static bool mei_cl_bus_dev_setup(struct mei_device *bus,
1355 struct mei_cl_device *cldev)
1357 cldev->do_match = 1;
1358 mei_cl_bus_dev_fixup(cldev);
1360 /* the device name can change during fix up */
1361 if (cldev->do_match)
1362 mei_cl_bus_set_name(cldev);
1364 return cldev->do_match == 1;
1368 * mei_cl_bus_dev_add - add me client devices
1370 * @cldev: me client device
1372 * Return: 0 on success; < 0 on failre
1374 static int mei_cl_bus_dev_add(struct mei_cl_device *cldev)
1378 dev_dbg(cldev->bus->dev, "adding %pUL:%02X\n",
1379 mei_me_cl_uuid(cldev->me_cl),
1380 mei_me_cl_ver(cldev->me_cl));
1381 ret = device_add(&cldev->dev);
1383 cldev->is_added = 1;
1389 * mei_cl_bus_dev_stop - stop the driver
1391 * @cldev: me client device
1393 static void mei_cl_bus_dev_stop(struct mei_cl_device *cldev)
1395 cldev->do_match = 0;
1396 if (cldev->is_added)
1397 device_release_driver(&cldev->dev);
1401 * mei_cl_bus_dev_destroy - destroy me client devices object
1403 * @cldev: me client device
1405 * Locking: called under "dev->cl_bus_lock" lock
1407 static void mei_cl_bus_dev_destroy(struct mei_cl_device *cldev)
1410 WARN_ON(!mutex_is_locked(&cldev->bus->cl_bus_lock));
1412 if (!cldev->is_added)
1415 device_del(&cldev->dev);
1417 list_del_init(&cldev->bus_list);
1419 cldev->is_added = 0;
1420 put_device(&cldev->dev);
1424 * mei_cl_bus_remove_device - remove a devices form the bus
1426 * @cldev: me client device
1428 static void mei_cl_bus_remove_device(struct mei_cl_device *cldev)
1430 mei_cl_bus_dev_stop(cldev);
1431 mei_cl_bus_dev_destroy(cldev);
1435 * mei_cl_bus_remove_devices - remove all devices form the bus
1439 void mei_cl_bus_remove_devices(struct mei_device *bus)
1441 struct mei_cl_device *cldev, *next;
1443 mutex_lock(&bus->cl_bus_lock);
1444 list_for_each_entry_safe(cldev, next, &bus->device_list, bus_list)
1445 mei_cl_bus_remove_device(cldev);
1446 mutex_unlock(&bus->cl_bus_lock);
1451 * mei_cl_bus_dev_init - allocate and initializes an mei client devices
1452 * based on me client
1457 * Locking: called under "dev->cl_bus_lock" lock
1459 static void mei_cl_bus_dev_init(struct mei_device *bus,
1460 struct mei_me_client *me_cl)
1462 struct mei_cl_device *cldev;
1464 WARN_ON(!mutex_is_locked(&bus->cl_bus_lock));
1466 dev_dbg(bus->dev, "initializing %pUl", mei_me_cl_uuid(me_cl));
1468 if (me_cl->bus_added)
1471 cldev = mei_cl_bus_dev_alloc(bus, me_cl);
1475 me_cl->bus_added = true;
1476 list_add_tail(&cldev->bus_list, &bus->device_list);
1481 * mei_cl_bus_rescan - scan me clients list and add create
1482 * devices for eligible clients
1486 static void mei_cl_bus_rescan(struct mei_device *bus)
1488 struct mei_cl_device *cldev, *n;
1489 struct mei_me_client *me_cl;
1491 mutex_lock(&bus->cl_bus_lock);
1493 down_read(&bus->me_clients_rwsem);
1494 list_for_each_entry(me_cl, &bus->me_clients, list)
1495 mei_cl_bus_dev_init(bus, me_cl);
1496 up_read(&bus->me_clients_rwsem);
1498 list_for_each_entry_safe(cldev, n, &bus->device_list, bus_list) {
1500 if (!mei_me_cl_is_active(cldev->me_cl)) {
1501 mei_cl_bus_remove_device(cldev);
1505 if (cldev->is_added)
1508 if (mei_cl_bus_dev_setup(bus, cldev))
1509 mei_cl_bus_dev_add(cldev);
1511 list_del_init(&cldev->bus_list);
1512 put_device(&cldev->dev);
1515 mutex_unlock(&bus->cl_bus_lock);
1517 dev_dbg(bus->dev, "rescan end");
1520 void mei_cl_bus_rescan_work(struct work_struct *work)
1522 struct mei_device *bus =
1523 container_of(work, struct mei_device, bus_rescan_work);
1525 mei_cl_bus_rescan(bus);
1528 int __mei_cldev_driver_register(struct mei_cl_driver *cldrv,
1529 struct module *owner)
1533 cldrv->driver.name = cldrv->name;
1534 cldrv->driver.owner = owner;
1535 cldrv->driver.bus = &mei_cl_bus_type;
1537 err = driver_register(&cldrv->driver);
1541 pr_debug("mei: driver [%s] registered\n", cldrv->driver.name);
1545 EXPORT_SYMBOL_GPL(__mei_cldev_driver_register);
1547 void mei_cldev_driver_unregister(struct mei_cl_driver *cldrv)
1549 driver_unregister(&cldrv->driver);
1551 pr_debug("mei: driver [%s] unregistered\n", cldrv->driver.name);
1553 EXPORT_SYMBOL_GPL(mei_cldev_driver_unregister);
1556 int __init mei_cl_bus_init(void)
1558 return bus_register(&mei_cl_bus_type);
1561 void __exit mei_cl_bus_exit(void)
1563 bus_unregister(&mei_cl_bus_type);