1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2003-2020, Intel Corporation. All rights reserved.
4 * Intel Management Engine Interface (Intel MEI) Linux driver
7 #include <linux/module.h>
8 #include <linux/moduleparam.h>
9 #include <linux/kernel.h>
10 #include <linux/device.h>
11 #include <linux/slab.h>
13 #include <linux/errno.h>
14 #include <linux/types.h>
15 #include <linux/fcntl.h>
16 #include <linux/poll.h>
17 #include <linux/init.h>
18 #include <linux/ioctl.h>
19 #include <linux/cdev.h>
20 #include <linux/sched/signal.h>
21 #include <linux/uuid.h>
22 #include <linux/compat.h>
23 #include <linux/jiffies.h>
24 #include <linux/interrupt.h>
26 #include <linux/mei.h>
31 static struct class *mei_class
;
32 static dev_t mei_devt
;
33 #define MEI_MAX_DEVS MINORMASK
34 static DEFINE_MUTEX(mei_minor_lock
);
35 static DEFINE_IDR(mei_idr
);
38 * mei_open - the open function
40 * @inode: pointer to inode structure
41 * @file: pointer to file structure
43 * Return: 0 on success, <0 on error
45 static int mei_open(struct inode
*inode
, struct file
*file
)
47 struct mei_device
*dev
;
52 dev
= container_of(inode
->i_cdev
, struct mei_device
, cdev
);
56 mutex_lock(&dev
->device_lock
);
58 if (dev
->dev_state
!= MEI_DEV_ENABLED
) {
59 dev_dbg(dev
->dev
, "dev_state != MEI_ENABLED dev_state = %s\n",
60 mei_dev_state_str(dev
->dev_state
));
65 cl
= mei_cl_alloc_linked(dev
);
72 file
->private_data
= cl
;
74 mutex_unlock(&dev
->device_lock
);
76 return nonseekable_open(inode
, file
);
79 mutex_unlock(&dev
->device_lock
);
84 * mei_cl_vtag_remove_by_fp - remove vtag that corresponds to fp from list
87 * @fp: pointer to file structure
90 static void mei_cl_vtag_remove_by_fp(const struct mei_cl
*cl
,
91 const struct file
*fp
)
93 struct mei_cl_vtag
*vtag_l
, *next
;
95 list_for_each_entry_safe(vtag_l
, next
, &cl
->vtag_map
, list
) {
96 if (vtag_l
->fp
== fp
) {
97 list_del(&vtag_l
->list
);
105 * mei_release - the release function
107 * @inode: pointer to inode structure
108 * @file: pointer to file structure
110 * Return: 0 on success, <0 on error
112 static int mei_release(struct inode
*inode
, struct file
*file
)
114 struct mei_cl
*cl
= file
->private_data
;
115 struct mei_device
*dev
;
118 if (WARN_ON(!cl
|| !cl
->dev
))
123 mutex_lock(&dev
->device_lock
);
125 mei_cl_vtag_remove_by_fp(cl
, file
);
127 if (!list_empty(&cl
->vtag_map
)) {
128 cl_dbg(dev
, cl
, "not the last vtag\n");
129 mei_cl_flush_queues(cl
, file
);
134 rets
= mei_cl_disconnect(cl
);
136 * Check again: This is necessary since disconnect releases the lock
137 * and another client can connect in the meantime.
139 if (!list_empty(&cl
->vtag_map
)) {
140 cl_dbg(dev
, cl
, "not the last vtag after disconnect\n");
141 mei_cl_flush_queues(cl
, file
);
145 mei_cl_flush_queues(cl
, NULL
);
146 cl_dbg(dev
, cl
, "removing\n");
152 file
->private_data
= NULL
;
154 mutex_unlock(&dev
->device_lock
);
160 * mei_read - the read function.
162 * @file: pointer to file structure
163 * @ubuf: pointer to user buffer
164 * @length: buffer length
165 * @offset: data offset in buffer
167 * Return: >=0 data length on success , <0 on error
169 static ssize_t
mei_read(struct file
*file
, char __user
*ubuf
,
170 size_t length
, loff_t
*offset
)
172 struct mei_cl
*cl
= file
->private_data
;
173 struct mei_device
*dev
;
174 struct mei_cl_cb
*cb
= NULL
;
175 bool nonblock
= !!(file
->f_flags
& O_NONBLOCK
);
178 if (WARN_ON(!cl
|| !cl
->dev
))
184 mutex_lock(&dev
->device_lock
);
185 if (dev
->dev_state
!= MEI_DEV_ENABLED
) {
200 cb
= mei_cl_read_cb(cl
, file
);
207 rets
= mei_cl_read_start(cl
, length
, file
);
208 if (rets
&& rets
!= -EBUSY
) {
209 cl_dbg(dev
, cl
, "mei start read failure status = %zd\n", rets
);
218 mutex_unlock(&dev
->device_lock
);
219 if (wait_event_interruptible(cl
->rx_wait
,
220 mei_cl_read_cb(cl
, file
) ||
221 !mei_cl_is_connected(cl
))) {
222 if (signal_pending(current
))
226 mutex_lock(&dev
->device_lock
);
228 if (!mei_cl_is_connected(cl
)) {
233 cb
= mei_cl_read_cb(cl
, file
);
240 /* now copy the data to user space */
243 cl_dbg(dev
, cl
, "read operation failed %zd\n", rets
);
247 cl_dbg(dev
, cl
, "buf.size = %zu buf.idx = %zu offset = %lld\n",
248 cb
->buf
.size
, cb
->buf_idx
, *offset
);
249 if (*offset
>= cb
->buf_idx
) {
254 /* length is being truncated to PAGE_SIZE,
255 * however buf_idx may point beyond that */
256 length
= min_t(size_t, length
, cb
->buf_idx
- *offset
);
258 if (copy_to_user(ubuf
, cb
->buf
.data
+ *offset
, length
)) {
259 dev_dbg(dev
->dev
, "failed to copy data to userland\n");
266 /* not all data was read, keep the cb */
267 if (*offset
< cb
->buf_idx
)
271 mei_cl_del_rd_completed(cl
, cb
);
275 cl_dbg(dev
, cl
, "end mei read rets = %zd\n", rets
);
276 mutex_unlock(&dev
->device_lock
);
281 * mei_cl_vtag_by_fp - obtain the vtag by file pointer
284 * @fp: pointer to file structure
286 * Return: vtag value on success, otherwise 0
288 static u8
mei_cl_vtag_by_fp(const struct mei_cl
*cl
, const struct file
*fp
)
290 struct mei_cl_vtag
*cl_vtag
;
295 list_for_each_entry(cl_vtag
, &cl
->vtag_map
, list
)
296 if (cl_vtag
->fp
== fp
)
297 return cl_vtag
->vtag
;
302 * mei_write - the write function.
304 * @file: pointer to file structure
305 * @ubuf: pointer to user buffer
306 * @length: buffer length
307 * @offset: data offset in buffer
309 * Return: >=0 data length on success , <0 on error
311 static ssize_t
mei_write(struct file
*file
, const char __user
*ubuf
,
312 size_t length
, loff_t
*offset
)
314 struct mei_cl
*cl
= file
->private_data
;
315 struct mei_cl_cb
*cb
;
316 struct mei_device
*dev
;
319 if (WARN_ON(!cl
|| !cl
->dev
))
324 mutex_lock(&dev
->device_lock
);
326 if (dev
->dev_state
!= MEI_DEV_ENABLED
) {
331 if (!mei_cl_is_connected(cl
)) {
332 cl_err(dev
, cl
, "is not connected");
337 if (!mei_me_cl_is_active(cl
->me_cl
)) {
342 if (length
> mei_cl_mtu(cl
)) {
352 while (cl
->tx_cb_queued
>= dev
->tx_queue_limit
) {
353 if (file
->f_flags
& O_NONBLOCK
) {
357 mutex_unlock(&dev
->device_lock
);
358 rets
= wait_event_interruptible(cl
->tx_wait
,
359 cl
->writing_state
== MEI_WRITE_COMPLETE
||
360 (!mei_cl_is_connected(cl
)));
361 mutex_lock(&dev
->device_lock
);
363 if (signal_pending(current
))
367 if (!mei_cl_is_connected(cl
)) {
373 cb
= mei_cl_alloc_cb(cl
, length
, MEI_FOP_WRITE
, file
);
378 cb
->vtag
= mei_cl_vtag_by_fp(cl
, file
);
380 rets
= copy_from_user(cb
->buf
.data
, ubuf
, length
);
382 dev_dbg(dev
->dev
, "failed to copy data from userland\n");
388 rets
= mei_cl_write(cl
, cb
);
390 mutex_unlock(&dev
->device_lock
);
395 * mei_ioctl_connect_client - the connect to fw client IOCTL function
397 * @file: private data of the file object
398 * @in_client_uuid: requested UUID for connection
399 * @client: IOCTL connect data, output parameters
401 * Locking: called under "dev->device_lock" lock
403 * Return: 0 on success, <0 on failure.
405 static int mei_ioctl_connect_client(struct file
*file
,
406 const uuid_le
*in_client_uuid
,
407 struct mei_client
*client
)
409 struct mei_device
*dev
;
410 struct mei_me_client
*me_cl
;
414 cl
= file
->private_data
;
417 if (cl
->state
!= MEI_FILE_INITIALIZING
&&
418 cl
->state
!= MEI_FILE_DISCONNECTED
)
421 /* find ME client we're trying to connect to */
422 me_cl
= mei_me_cl_by_uuid(dev
, in_client_uuid
);
424 dev_dbg(dev
->dev
, "Cannot connect to FW Client UUID = %pUl\n",
430 if (me_cl
->props
.fixed_address
) {
431 bool forbidden
= dev
->override_fixed_address
?
432 !dev
->allow_fixed_address
: !dev
->hbm_f_fa_supported
;
434 dev_dbg(dev
->dev
, "Connection forbidden to FW Client UUID = %pUl\n",
441 dev_dbg(dev
->dev
, "Connect to FW Client ID = %d\n",
443 dev_dbg(dev
->dev
, "FW Client - Protocol Version = %d\n",
444 me_cl
->props
.protocol_version
);
445 dev_dbg(dev
->dev
, "FW Client - Max Msg Len = %d\n",
446 me_cl
->props
.max_msg_length
);
448 /* prepare the output buffer */
449 client
->max_msg_length
= me_cl
->props
.max_msg_length
;
450 client
->protocol_version
= me_cl
->props
.protocol_version
;
451 dev_dbg(dev
->dev
, "Can connect?\n");
453 rets
= mei_cl_connect(cl
, me_cl
, file
);
456 mei_me_cl_put(me_cl
);
461 * mei_vt_support_check - check if client support vtags
463 * Locking: called under "dev->device_lock" lock
470 * -ENOTTY - no such client
471 * -EOPNOTSUPP - vtags are not supported by client
473 static int mei_vt_support_check(struct mei_device
*dev
, const uuid_le
*uuid
)
475 struct mei_me_client
*me_cl
;
478 if (!dev
->hbm_f_vt_supported
)
481 me_cl
= mei_me_cl_by_uuid(dev
, uuid
);
483 dev_dbg(dev
->dev
, "Cannot connect to FW Client UUID = %pUl\n",
487 ret
= me_cl
->props
.vt_supported
? 0 : -EOPNOTSUPP
;
488 mei_me_cl_put(me_cl
);
494 * mei_ioctl_connect_vtag - connect to fw client with vtag IOCTL function
496 * @file: private data of the file object
497 * @in_client_uuid: requested UUID for connection
498 * @client: IOCTL connect data, output parameters
501 * Locking: called under "dev->device_lock" lock
503 * Return: 0 on success, <0 on failure.
505 static int mei_ioctl_connect_vtag(struct file
*file
,
506 const uuid_le
*in_client_uuid
,
507 struct mei_client
*client
,
510 struct mei_device
*dev
;
513 struct mei_cl_vtag
*cl_vtag
;
515 cl
= file
->private_data
;
518 dev_dbg(dev
->dev
, "FW Client %pUl vtag %d\n", in_client_uuid
, vtag
);
521 case MEI_FILE_DISCONNECTED
:
522 if (mei_cl_vtag_by_fp(cl
, file
) != vtag
) {
523 dev_err(dev
->dev
, "reconnect with different vtag\n");
527 case MEI_FILE_INITIALIZING
:
528 /* malicious connect from another thread may push vtag */
529 if (!IS_ERR(mei_cl_fp_by_vtag(cl
, vtag
))) {
530 dev_err(dev
->dev
, "vtag already filled\n");
534 list_for_each_entry(pos
, &dev
->file_list
, link
) {
540 /* only search for same UUID */
541 if (uuid_le_cmp(*mei_cl_uuid(pos
), *in_client_uuid
))
544 /* if tag already exist try another fp */
545 if (!IS_ERR(mei_cl_fp_by_vtag(pos
, vtag
)))
548 /* replace cl with acquired one */
549 dev_dbg(dev
->dev
, "replacing with existing cl\n");
552 file
->private_data
= pos
;
557 cl_vtag
= mei_cl_vtag_alloc(file
, vtag
);
561 list_add_tail(&cl_vtag
->list
, &cl
->vtag_map
);
567 while (cl
->state
!= MEI_FILE_INITIALIZING
&&
568 cl
->state
!= MEI_FILE_DISCONNECTED
&&
569 cl
->state
!= MEI_FILE_CONNECTED
) {
570 mutex_unlock(&dev
->device_lock
);
571 wait_event_timeout(cl
->wait
,
572 (cl
->state
== MEI_FILE_CONNECTED
||
573 cl
->state
== MEI_FILE_DISCONNECTED
||
574 cl
->state
== MEI_FILE_DISCONNECT_REQUIRED
||
575 cl
->state
== MEI_FILE_DISCONNECT_REPLY
),
576 mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT
));
577 mutex_lock(&dev
->device_lock
);
580 if (!mei_cl_is_connected(cl
))
581 return mei_ioctl_connect_client(file
, in_client_uuid
, client
);
583 client
->max_msg_length
= cl
->me_cl
->props
.max_msg_length
;
584 client
->protocol_version
= cl
->me_cl
->props
.protocol_version
;
590 * mei_ioctl_client_notify_request -
591 * propagate event notification request to client
593 * @file: pointer to file structure
594 * @request: 0 - disable, 1 - enable
596 * Return: 0 on success , <0 on error
598 static int mei_ioctl_client_notify_request(const struct file
*file
, u32 request
)
600 struct mei_cl
*cl
= file
->private_data
;
602 if (request
!= MEI_HBM_NOTIFICATION_START
&&
603 request
!= MEI_HBM_NOTIFICATION_STOP
)
606 return mei_cl_notify_request(cl
, file
, (u8
)request
);
610 * mei_ioctl_client_notify_get - wait for notification request
612 * @file: pointer to file structure
613 * @notify_get: 0 - disable, 1 - enable
615 * Return: 0 on success , <0 on error
617 static int mei_ioctl_client_notify_get(const struct file
*file
, u32
*notify_get
)
619 struct mei_cl
*cl
= file
->private_data
;
621 bool block
= (file
->f_flags
& O_NONBLOCK
) == 0;
624 rets
= mei_cl_notify_get(cl
, block
, ¬ify_ev
);
628 *notify_get
= notify_ev
? 1 : 0;
633 * mei_ioctl - the IOCTL function
635 * @file: pointer to file structure
636 * @cmd: ioctl command
637 * @data: pointer to mei message structure
639 * Return: 0 on success , <0 on error
641 static long mei_ioctl(struct file
*file
, unsigned int cmd
, unsigned long data
)
643 struct mei_device
*dev
;
644 struct mei_cl
*cl
= file
->private_data
;
645 struct mei_connect_client_data conn
;
646 struct mei_connect_client_data_vtag conn_vtag
;
647 const uuid_le
*cl_uuid
;
648 struct mei_client
*props
;
650 u32 notify_get
, notify_req
;
654 if (WARN_ON(!cl
|| !cl
->dev
))
659 dev_dbg(dev
->dev
, "IOCTL cmd = 0x%x", cmd
);
661 mutex_lock(&dev
->device_lock
);
662 if (dev
->dev_state
!= MEI_DEV_ENABLED
) {
668 case IOCTL_MEI_CONNECT_CLIENT
:
669 dev_dbg(dev
->dev
, ": IOCTL_MEI_CONNECT_CLIENT.\n");
670 if (copy_from_user(&conn
, (char __user
*)data
, sizeof(conn
))) {
671 dev_dbg(dev
->dev
, "failed to copy data from userland\n");
675 cl_uuid
= &conn
.in_client_uuid
;
676 props
= &conn
.out_client_properties
;
679 rets
= mei_vt_support_check(dev
, cl_uuid
);
683 rets
= mei_ioctl_connect_vtag(file
, cl_uuid
, props
,
686 rets
= mei_ioctl_connect_client(file
, cl_uuid
, props
);
690 /* if all is ok, copying the data back to user. */
691 if (copy_to_user((char __user
*)data
, &conn
, sizeof(conn
))) {
692 dev_dbg(dev
->dev
, "failed to copy data to userland\n");
699 case IOCTL_MEI_CONNECT_CLIENT_VTAG
:
700 dev_dbg(dev
->dev
, "IOCTL_MEI_CONNECT_CLIENT_VTAG\n");
701 if (copy_from_user(&conn_vtag
, (char __user
*)data
,
702 sizeof(conn_vtag
))) {
703 dev_dbg(dev
->dev
, "failed to copy data from userland\n");
708 cl_uuid
= &conn_vtag
.connect
.in_client_uuid
;
709 props
= &conn_vtag
.out_client_properties
;
710 vtag
= conn_vtag
.connect
.vtag
;
712 rets
= mei_vt_support_check(dev
, cl_uuid
);
713 if (rets
== -EOPNOTSUPP
)
714 dev_dbg(dev
->dev
, "FW Client %pUl does not support vtags\n",
720 dev_dbg(dev
->dev
, "vtag can't be zero\n");
725 rets
= mei_ioctl_connect_vtag(file
, cl_uuid
, props
, vtag
);
729 /* if all is ok, copying the data back to user. */
730 if (copy_to_user((char __user
*)data
, &conn_vtag
,
731 sizeof(conn_vtag
))) {
732 dev_dbg(dev
->dev
, "failed to copy data to userland\n");
739 case IOCTL_MEI_NOTIFY_SET
:
740 dev_dbg(dev
->dev
, ": IOCTL_MEI_NOTIFY_SET.\n");
741 if (copy_from_user(¬ify_req
,
742 (char __user
*)data
, sizeof(notify_req
))) {
743 dev_dbg(dev
->dev
, "failed to copy data from userland\n");
747 rets
= mei_ioctl_client_notify_request(file
, notify_req
);
750 case IOCTL_MEI_NOTIFY_GET
:
751 dev_dbg(dev
->dev
, ": IOCTL_MEI_NOTIFY_GET.\n");
752 rets
= mei_ioctl_client_notify_get(file
, ¬ify_get
);
756 dev_dbg(dev
->dev
, "copy connect data to user\n");
757 if (copy_to_user((char __user
*)data
,
758 ¬ify_get
, sizeof(notify_get
))) {
759 dev_dbg(dev
->dev
, "failed to copy data to userland\n");
771 mutex_unlock(&dev
->device_lock
);
776 * mei_poll - the poll function
778 * @file: pointer to file structure
779 * @wait: pointer to poll_table structure
783 static __poll_t
mei_poll(struct file
*file
, poll_table
*wait
)
785 __poll_t req_events
= poll_requested_events(wait
);
786 struct mei_cl
*cl
= file
->private_data
;
787 struct mei_device
*dev
;
791 if (WARN_ON(!cl
|| !cl
->dev
))
796 mutex_lock(&dev
->device_lock
);
798 notify_en
= cl
->notify_en
&& (req_events
& EPOLLPRI
);
800 if (dev
->dev_state
!= MEI_DEV_ENABLED
||
801 !mei_cl_is_connected(cl
)) {
807 poll_wait(file
, &cl
->ev_wait
, wait
);
812 if (req_events
& (EPOLLIN
| EPOLLRDNORM
)) {
813 poll_wait(file
, &cl
->rx_wait
, wait
);
815 if (mei_cl_read_cb(cl
, file
))
816 mask
|= EPOLLIN
| EPOLLRDNORM
;
818 mei_cl_read_start(cl
, mei_cl_mtu(cl
), file
);
821 if (req_events
& (EPOLLOUT
| EPOLLWRNORM
)) {
822 poll_wait(file
, &cl
->tx_wait
, wait
);
823 if (cl
->tx_cb_queued
< dev
->tx_queue_limit
)
824 mask
|= EPOLLOUT
| EPOLLWRNORM
;
828 mutex_unlock(&dev
->device_lock
);
833 * mei_cl_is_write_queued - check if the client has pending writes.
835 * @cl: writing host client
837 * Return: true if client is writing, false otherwise.
839 static bool mei_cl_is_write_queued(struct mei_cl
*cl
)
841 struct mei_device
*dev
= cl
->dev
;
842 struct mei_cl_cb
*cb
;
844 list_for_each_entry(cb
, &dev
->write_list
, list
)
847 list_for_each_entry(cb
, &dev
->write_waiting_list
, list
)
854 * mei_fsync - the fsync handler
856 * @fp: pointer to file structure
861 * Return: 0 on success, -ENODEV if client is not connected
863 static int mei_fsync(struct file
*fp
, loff_t start
, loff_t end
, int datasync
)
865 struct mei_cl
*cl
= fp
->private_data
;
866 struct mei_device
*dev
;
869 if (WARN_ON(!cl
|| !cl
->dev
))
874 mutex_lock(&dev
->device_lock
);
876 if (dev
->dev_state
!= MEI_DEV_ENABLED
|| !mei_cl_is_connected(cl
)) {
881 while (mei_cl_is_write_queued(cl
)) {
882 mutex_unlock(&dev
->device_lock
);
883 rets
= wait_event_interruptible(cl
->tx_wait
,
884 cl
->writing_state
== MEI_WRITE_COMPLETE
||
885 !mei_cl_is_connected(cl
));
886 mutex_lock(&dev
->device_lock
);
888 if (signal_pending(current
))
892 if (!mei_cl_is_connected(cl
)) {
899 mutex_unlock(&dev
->device_lock
);
904 * mei_fasync - asynchronous io support
906 * @fd: file descriptor
907 * @file: pointer to file structure
910 * Return: negative on error,
911 * 0 if it did no changes,
912 * and positive a process was added or deleted
914 static int mei_fasync(int fd
, struct file
*file
, int band
)
917 struct mei_cl
*cl
= file
->private_data
;
919 if (!mei_cl_is_connected(cl
))
922 return fasync_helper(fd
, file
, band
, &cl
->ev_async
);
926 * trc_show - mei device trc attribute show method
928 * @device: device pointer
929 * @attr: attribute pointer
930 * @buf: char out buffer
932 * Return: number of the bytes printed into buf or error
934 static ssize_t
trc_show(struct device
*device
,
935 struct device_attribute
*attr
, char *buf
)
937 struct mei_device
*dev
= dev_get_drvdata(device
);
941 ret
= mei_trc_status(dev
, &trc
);
944 return sprintf(buf
, "%08X\n", trc
);
946 static DEVICE_ATTR_RO(trc
);
949 * fw_status_show - mei device fw_status attribute show method
951 * @device: device pointer
952 * @attr: attribute pointer
953 * @buf: char out buffer
955 * Return: number of the bytes printed into buf or error
957 static ssize_t
fw_status_show(struct device
*device
,
958 struct device_attribute
*attr
, char *buf
)
960 struct mei_device
*dev
= dev_get_drvdata(device
);
961 struct mei_fw_status fw_status
;
965 mutex_lock(&dev
->device_lock
);
966 err
= mei_fw_status(dev
, &fw_status
);
967 mutex_unlock(&dev
->device_lock
);
969 dev_err(device
, "read fw_status error = %d\n", err
);
973 for (i
= 0; i
< fw_status
.count
; i
++)
974 cnt
+= scnprintf(buf
+ cnt
, PAGE_SIZE
- cnt
, "%08X\n",
975 fw_status
.status
[i
]);
978 static DEVICE_ATTR_RO(fw_status
);
981 * hbm_ver_show - display HBM protocol version negotiated with FW
983 * @device: device pointer
984 * @attr: attribute pointer
985 * @buf: char out buffer
987 * Return: number of the bytes printed into buf or error
989 static ssize_t
hbm_ver_show(struct device
*device
,
990 struct device_attribute
*attr
, char *buf
)
992 struct mei_device
*dev
= dev_get_drvdata(device
);
993 struct hbm_version ver
;
995 mutex_lock(&dev
->device_lock
);
997 mutex_unlock(&dev
->device_lock
);
999 return sprintf(buf
, "%u.%u\n", ver
.major_version
, ver
.minor_version
);
1001 static DEVICE_ATTR_RO(hbm_ver
);
1004 * hbm_ver_drv_show - display HBM protocol version advertised by driver
1006 * @device: device pointer
1007 * @attr: attribute pointer
1008 * @buf: char out buffer
1010 * Return: number of the bytes printed into buf or error
1012 static ssize_t
hbm_ver_drv_show(struct device
*device
,
1013 struct device_attribute
*attr
, char *buf
)
1015 return sprintf(buf
, "%u.%u\n", HBM_MAJOR_VERSION
, HBM_MINOR_VERSION
);
1017 static DEVICE_ATTR_RO(hbm_ver_drv
);
1019 static ssize_t
tx_queue_limit_show(struct device
*device
,
1020 struct device_attribute
*attr
, char *buf
)
1022 struct mei_device
*dev
= dev_get_drvdata(device
);
1025 mutex_lock(&dev
->device_lock
);
1026 size
= dev
->tx_queue_limit
;
1027 mutex_unlock(&dev
->device_lock
);
1029 return snprintf(buf
, PAGE_SIZE
, "%u\n", size
);
1032 static ssize_t
tx_queue_limit_store(struct device
*device
,
1033 struct device_attribute
*attr
,
1034 const char *buf
, size_t count
)
1036 struct mei_device
*dev
= dev_get_drvdata(device
);
1041 err
= kstrtouint(buf
, 10, &inp
);
1044 if (inp
> MEI_TX_QUEUE_LIMIT_MAX
|| inp
< MEI_TX_QUEUE_LIMIT_MIN
)
1048 mutex_lock(&dev
->device_lock
);
1049 dev
->tx_queue_limit
= limit
;
1050 mutex_unlock(&dev
->device_lock
);
1054 static DEVICE_ATTR_RW(tx_queue_limit
);
1057 * fw_ver_show - display ME FW version
1059 * @device: device pointer
1060 * @attr: attribute pointer
1061 * @buf: char out buffer
1063 * Return: number of the bytes printed into buf or error
1065 static ssize_t
fw_ver_show(struct device
*device
,
1066 struct device_attribute
*attr
, char *buf
)
1068 struct mei_device
*dev
= dev_get_drvdata(device
);
1069 struct mei_fw_version
*ver
;
1075 for (i
= 0; i
< MEI_MAX_FW_VER_BLOCKS
; i
++)
1076 cnt
+= scnprintf(buf
+ cnt
, PAGE_SIZE
- cnt
, "%u:%u.%u.%u.%u\n",
1077 ver
[i
].platform
, ver
[i
].major
, ver
[i
].minor
,
1078 ver
[i
].hotfix
, ver
[i
].buildno
);
1081 static DEVICE_ATTR_RO(fw_ver
);
1084 * dev_state_show - display device state
1086 * @device: device pointer
1087 * @attr: attribute pointer
1088 * @buf: char out buffer
1090 * Return: number of the bytes printed into buf or error
1092 static ssize_t
dev_state_show(struct device
*device
,
1093 struct device_attribute
*attr
, char *buf
)
1095 struct mei_device
*dev
= dev_get_drvdata(device
);
1096 enum mei_dev_state dev_state
;
1098 mutex_lock(&dev
->device_lock
);
1099 dev_state
= dev
->dev_state
;
1100 mutex_unlock(&dev
->device_lock
);
1102 return sprintf(buf
, "%s", mei_dev_state_str(dev_state
));
1104 static DEVICE_ATTR_RO(dev_state
);
1107 * dev_set_devstate: set to new device state and notify sysfs file.
1110 * @state: new device state
1112 void mei_set_devstate(struct mei_device
*dev
, enum mei_dev_state state
)
1114 struct device
*clsdev
;
1116 if (dev
->dev_state
== state
)
1119 dev
->dev_state
= state
;
1121 clsdev
= class_find_device_by_devt(mei_class
, dev
->cdev
.dev
);
1123 sysfs_notify(&clsdev
->kobj
, NULL
, "dev_state");
1129 * kind_show - display device kind
1131 * @device: device pointer
1132 * @attr: attribute pointer
1133 * @buf: char out buffer
1135 * Return: number of the bytes printed into buf or error
1137 static ssize_t
kind_show(struct device
*device
,
1138 struct device_attribute
*attr
, char *buf
)
1140 struct mei_device
*dev
= dev_get_drvdata(device
);
1144 ret
= sprintf(buf
, "%s\n", dev
->kind
);
1146 ret
= sprintf(buf
, "%s\n", "mei");
1150 static DEVICE_ATTR_RO(kind
);
1152 static struct attribute
*mei_attrs
[] = {
1153 &dev_attr_fw_status
.attr
,
1154 &dev_attr_hbm_ver
.attr
,
1155 &dev_attr_hbm_ver_drv
.attr
,
1156 &dev_attr_tx_queue_limit
.attr
,
1157 &dev_attr_fw_ver
.attr
,
1158 &dev_attr_dev_state
.attr
,
1160 &dev_attr_kind
.attr
,
1163 ATTRIBUTE_GROUPS(mei
);
1166 * file operations structure will be used for mei char device.
1168 static const struct file_operations mei_fops
= {
1169 .owner
= THIS_MODULE
,
1171 .unlocked_ioctl
= mei_ioctl
,
1172 .compat_ioctl
= compat_ptr_ioctl
,
1174 .release
= mei_release
,
1178 .fasync
= mei_fasync
,
1183 * mei_minor_get - obtain next free device minor number
1185 * @dev: device pointer
1187 * Return: allocated minor, or -ENOSPC if no free minor left
1189 static int mei_minor_get(struct mei_device
*dev
)
1193 mutex_lock(&mei_minor_lock
);
1194 ret
= idr_alloc(&mei_idr
, dev
, 0, MEI_MAX_DEVS
, GFP_KERNEL
);
1197 else if (ret
== -ENOSPC
)
1198 dev_err(dev
->dev
, "too many mei devices\n");
1200 mutex_unlock(&mei_minor_lock
);
1205 * mei_minor_free - mark device minor number as free
1207 * @dev: device pointer
1209 static void mei_minor_free(struct mei_device
*dev
)
1211 mutex_lock(&mei_minor_lock
);
1212 idr_remove(&mei_idr
, dev
->minor
);
1213 mutex_unlock(&mei_minor_lock
);
1216 int mei_register(struct mei_device
*dev
, struct device
*parent
)
1218 struct device
*clsdev
; /* class device */
1221 ret
= mei_minor_get(dev
);
1225 /* Fill in the data structures */
1226 devno
= MKDEV(MAJOR(mei_devt
), dev
->minor
);
1227 cdev_init(&dev
->cdev
, &mei_fops
);
1228 dev
->cdev
.owner
= parent
->driver
->owner
;
1230 /* Add the device */
1231 ret
= cdev_add(&dev
->cdev
, devno
, 1);
1233 dev_err(parent
, "unable to add device %d:%d\n",
1234 MAJOR(mei_devt
), dev
->minor
);
1238 clsdev
= device_create_with_groups(mei_class
, parent
, devno
,
1240 "mei%d", dev
->minor
);
1242 if (IS_ERR(clsdev
)) {
1243 dev_err(parent
, "unable to create device %d:%d\n",
1244 MAJOR(mei_devt
), dev
->minor
);
1245 ret
= PTR_ERR(clsdev
);
1246 goto err_dev_create
;
1249 mei_dbgfs_register(dev
, dev_name(clsdev
));
1254 cdev_del(&dev
->cdev
);
1256 mei_minor_free(dev
);
1259 EXPORT_SYMBOL_GPL(mei_register
);
1261 void mei_deregister(struct mei_device
*dev
)
1265 devno
= dev
->cdev
.dev
;
1266 cdev_del(&dev
->cdev
);
1268 mei_dbgfs_deregister(dev
);
1270 device_destroy(mei_class
, devno
);
1272 mei_minor_free(dev
);
1274 EXPORT_SYMBOL_GPL(mei_deregister
);
1276 static int __init
mei_init(void)
1280 mei_class
= class_create(THIS_MODULE
, "mei");
1281 if (IS_ERR(mei_class
)) {
1282 pr_err("couldn't create class\n");
1283 ret
= PTR_ERR(mei_class
);
1287 ret
= alloc_chrdev_region(&mei_devt
, 0, MEI_MAX_DEVS
, "mei");
1289 pr_err("unable to allocate char dev region\n");
1293 ret
= mei_cl_bus_init();
1295 pr_err("unable to initialize bus\n");
1302 unregister_chrdev_region(mei_devt
, MEI_MAX_DEVS
);
1304 class_destroy(mei_class
);
1309 static void __exit
mei_exit(void)
1311 unregister_chrdev_region(mei_devt
, MEI_MAX_DEVS
);
1312 class_destroy(mei_class
);
1316 module_init(mei_init
);
1317 module_exit(mei_exit
);
1319 MODULE_AUTHOR("Intel Corporation");
1320 MODULE_DESCRIPTION("Intel(R) Management Engine Interface");
1321 MODULE_LICENSE("GPL v2");