3 * Intel Management Engine Interface (Intel MEI) Linux driver
4 * Copyright (c) 2003-2012, Intel Corporation.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
20 #include <linux/types.h>
21 #include <linux/cdev.h>
22 #include <linux/poll.h>
23 #include <linux/mei.h>
24 #include <linux/mei_cl_bus.h>
33 extern const uuid_le mei_amthif_guid
;
35 #define MEI_RD_MSG_BUF_SIZE (128 * sizeof(u32))
38 * Number of Maximum MEI Clients
40 #define MEI_CLIENTS_MAX 256
43 * maximum number of consecutive resets
45 #define MEI_MAX_CONSEC_RESET 3
48 * Number of File descriptors/handles
49 * that can be opened to the driver.
51 * Limit to 255: 256 Total Clients
52 * minus internal client for MEI Bus Messages
54 #define MEI_MAX_OPEN_HANDLE_COUNT (MEI_CLIENTS_MAX - 1)
58 MEI_FILE_UNINITIALIZED
= 0,
59 MEI_FILE_INITIALIZING
,
62 MEI_FILE_DISCONNECTING
,
63 MEI_FILE_DISCONNECT_REPLY
,
64 MEI_FILE_DISCONNECT_REQUIRED
,
65 MEI_FILE_DISCONNECTED
,
68 /* MEI device states */
70 MEI_DEV_INITIALIZING
= 0,
79 const char *mei_dev_state_str(int state
);
87 enum mei_file_transaction_states
{
94 * enum mei_cb_file_ops - file operation associated with the callback
96 * @MEI_FOP_WRITE: write
97 * @MEI_FOP_CONNECT: connect
98 * @MEI_FOP_DISCONNECT: disconnect
99 * @MEI_FOP_DISCONNECT_RSP: disconnect response
100 * @MEI_FOP_NOTIFY_START: start notification
101 * @MEI_FOP_NOTIFY_STOP: stop notification
103 enum mei_cb_file_ops
{
108 MEI_FOP_DISCONNECT_RSP
,
109 MEI_FOP_NOTIFY_START
,
114 * enum mei_cl_io_mode - io mode between driver and fw
116 * @MEI_CL_IO_TX_BLOCKING: send is blocking
117 * @MEI_CL_IO_TX_INTERNAL: internal communication between driver and FW
119 * @MEI_CL_IO_RX_NONBLOCK: recv is non-blocking
121 enum mei_cl_io_mode
{
122 MEI_CL_IO_TX_BLOCKING
= BIT(0),
123 MEI_CL_IO_TX_INTERNAL
= BIT(1),
125 MEI_CL_IO_RX_NONBLOCK
= BIT(2),
129 * Intel MEI message data struct
131 struct mei_msg_data
{
136 /* Maximum number of processed FW status registers */
137 #define MEI_FW_STATUS_MAX 6
138 /* Minimal buffer for FW status string (8 bytes in dw + space or '\0') */
139 #define MEI_FW_STATUS_STR_SZ (MEI_FW_STATUS_MAX * (8 + 1))
143 * struct mei_fw_status - storage of FW status data
145 * @count: number of actually available elements in array
146 * @status: FW status registers
148 struct mei_fw_status
{
150 u32 status
[MEI_FW_STATUS_MAX
];
154 * struct mei_me_client - representation of me (fw) client
156 * @list: link in me client list
157 * @refcnt: struct reference count
158 * @props: client properties
159 * @client_id: me client id
160 * @tx_flow_ctrl_creds: flow control credits
161 * @connect_count: number connections to this client
162 * @bus_added: added to bus
164 struct mei_me_client
{
165 struct list_head list
;
167 struct mei_client_properties props
;
169 u8 tx_flow_ctrl_creds
;
178 * struct mei_cl_cb - file operation callback structure
180 * @list: link in callback queue
181 * @cl: file client who is running this operation
182 * @fop_type: file operation type
183 * @buf: buffer for data associated with the callback
184 * @buf_idx: last read index
185 * @fp: pointer to file structure
186 * @status: io status of the cb
187 * @internal: communication between driver and FW flag
188 * @blocking: transmission blocking mode
189 * @completed: the transfer or reception has completed
192 struct list_head list
;
194 enum mei_cb_file_ops fop_type
;
195 struct mei_msg_data buf
;
197 const struct file
*fp
;
205 * struct mei_cl - me client host representation
206 * carried in file->private_data
208 * @link: link in the clients list
209 * @dev: mei parent device
210 * @state: file operation state
211 * @tx_wait: wait queue for tx completion
212 * @rx_wait: wait queue for rx completion
213 * @wait: wait queue for management operation
214 * @ev_wait: notification wait queue
215 * @ev_async: event async notification
216 * @status: connection status
217 * @me_cl: fw client connected
218 * @fp: file associated with client
219 * @host_client_id: host id
220 * @tx_flow_ctrl_creds: transmit flow credentials
221 * @rx_flow_ctrl_creds: receive flow credentials
222 * @timer_count: watchdog timer for operation completion
223 * @notify_en: notification - enabled/disabled
224 * @notify_ev: pending notification event
225 * @writing_state: state of the tx
226 * @rd_pending: pending read credits
227 * @rd_completed: completed read
229 * @cldev: device on the mei client bus
232 struct list_head link
;
233 struct mei_device
*dev
;
234 enum file_state state
;
235 wait_queue_head_t tx_wait
;
236 wait_queue_head_t rx_wait
;
237 wait_queue_head_t wait
;
238 wait_queue_head_t ev_wait
;
239 struct fasync_struct
*ev_async
;
241 struct mei_me_client
*me_cl
;
242 const struct file
*fp
;
244 u8 tx_flow_ctrl_creds
;
245 u8 rx_flow_ctrl_creds
;
249 enum mei_file_transaction_states writing_state
;
250 struct list_head rd_pending
;
251 struct list_head rd_completed
;
253 struct mei_cl_device
*cldev
;
257 * struct mei_hw_ops - hw specific ops
259 * @host_is_ready : query for host readiness
261 * @hw_is_ready : query if hw is ready
262 * @hw_reset : reset hw
263 * @hw_start : start hw after reset
264 * @hw_config : configure hw
266 * @fw_status : get fw status registers
267 * @pg_state : power gating state of the device
268 * @pg_in_transition : is device now in pg transition
269 * @pg_is_enabled : is power gating enabled
271 * @intr_clear : clear pending interrupts
272 * @intr_enable : enable interrupts
273 * @intr_disable : disable interrupts
274 * @synchronize_irq : synchronize irqs
276 * @hbuf_free_slots : query for write buffer empty slots
277 * @hbuf_is_ready : query if write buffer is empty
278 * @hbuf_max_len : query for write buffer max len
280 * @write : write a message to FW
282 * @rdbuf_full_slots : query how many slots are filled
284 * @read_hdr : get first 4 bytes (header)
285 * @read : read a buffer from the FW
289 bool (*host_is_ready
)(struct mei_device
*dev
);
291 bool (*hw_is_ready
)(struct mei_device
*dev
);
292 int (*hw_reset
)(struct mei_device
*dev
, bool enable
);
293 int (*hw_start
)(struct mei_device
*dev
);
294 void (*hw_config
)(struct mei_device
*dev
);
296 int (*fw_status
)(struct mei_device
*dev
, struct mei_fw_status
*fw_sts
);
297 enum mei_pg_state (*pg_state
)(struct mei_device
*dev
);
298 bool (*pg_in_transition
)(struct mei_device
*dev
);
299 bool (*pg_is_enabled
)(struct mei_device
*dev
);
301 void (*intr_clear
)(struct mei_device
*dev
);
302 void (*intr_enable
)(struct mei_device
*dev
);
303 void (*intr_disable
)(struct mei_device
*dev
);
304 void (*synchronize_irq
)(struct mei_device
*dev
);
306 int (*hbuf_free_slots
)(struct mei_device
*dev
);
307 bool (*hbuf_is_ready
)(struct mei_device
*dev
);
308 size_t (*hbuf_max_len
)(const struct mei_device
*dev
);
309 int (*write
)(struct mei_device
*dev
,
310 struct mei_msg_hdr
*hdr
,
311 const unsigned char *buf
);
313 int (*rdbuf_full_slots
)(struct mei_device
*dev
);
315 u32 (*read_hdr
)(const struct mei_device
*dev
);
316 int (*read
)(struct mei_device
*dev
,
317 unsigned char *buf
, unsigned long len
);
321 void mei_cl_bus_rescan(struct mei_device
*bus
);
322 void mei_cl_bus_rescan_work(struct work_struct
*work
);
323 void mei_cl_bus_dev_fixup(struct mei_cl_device
*dev
);
324 ssize_t
__mei_cl_send(struct mei_cl
*cl
, u8
*buf
, size_t length
,
326 ssize_t
__mei_cl_recv(struct mei_cl
*cl
, u8
*buf
, size_t length
,
328 bool mei_cl_bus_rx_event(struct mei_cl
*cl
);
329 bool mei_cl_bus_notify_event(struct mei_cl
*cl
);
330 void mei_cl_bus_remove_devices(struct mei_device
*bus
);
331 int mei_cl_bus_init(void);
332 void mei_cl_bus_exit(void);
335 * enum mei_pg_event - power gating transition events
337 * @MEI_PG_EVENT_IDLE: the driver is not in power gating transition
338 * @MEI_PG_EVENT_WAIT: the driver is waiting for a pg event to complete
339 * @MEI_PG_EVENT_RECEIVED: the driver received pg event
340 * @MEI_PG_EVENT_INTR_WAIT: the driver is waiting for a pg event interrupt
341 * @MEI_PG_EVENT_INTR_RECEIVED: the driver received pg event interrupt
346 MEI_PG_EVENT_RECEIVED
,
347 MEI_PG_EVENT_INTR_WAIT
,
348 MEI_PG_EVENT_INTR_RECEIVED
,
352 * enum mei_pg_state - device internal power gating state
354 * @MEI_PG_OFF: device is not power gated - it is active
355 * @MEI_PG_ON: device is power gated - it is in lower power state
362 const char *mei_pg_state_str(enum mei_pg_state state
);
365 * struct mei_device - MEI private device struct
367 * @dev : device on a bus
368 * @cdev : character device
369 * @minor : minor number allocated for device
371 * @write_list : write pending list
372 * @write_waiting_list : write completion list
373 * @ctrl_wr_list : pending control write list
374 * @ctrl_rd_list : pending control read list
376 * @file_list : list of opened handles
377 * @open_handle_count: number of opened handles
379 * @device_lock : big device lock
380 * @timer_work : MEI timer delayed work (timeouts)
382 * @recvd_hw_ready : hw ready message received flag
384 * @wait_hw_ready : wait queue for receive HW ready message form FW
385 * @wait_pg : wait queue for receive PG message from FW
386 * @wait_hbm_start : wait queue for receive HBM start message from FW
388 * @reset_count : number of consecutive resets
389 * @dev_state : device state
390 * @hbm_state : state of host bus message protocol
391 * @init_clients_timer : HBM init handshake timeout
393 * @pg_event : power gating event
394 * @pg_domain : runtime PM domain
396 * @rd_msg_buf : control messages buffer
397 * @rd_msg_hdr : read message header storage
399 * @hbuf_depth : depth of hardware host/write buffer is slots
400 * @hbuf_is_ready : query if the host host/write buffer is ready
402 * @version : HBM protocol version in use
403 * @hbm_f_pg_supported : hbm feature pgi protocol
404 * @hbm_f_dc_supported : hbm feature dynamic clients
405 * @hbm_f_dot_supported : hbm feature disconnect on timeout
406 * @hbm_f_ev_supported : hbm feature event notification
407 * @hbm_f_fa_supported : hbm feature fixed address client
408 * @hbm_f_ie_supported : hbm feature immediate reply to enum request
410 * @me_clients_rwsem: rw lock over me_clients list
411 * @me_clients : list of FW clients
412 * @me_clients_map : FW clients bit map
413 * @host_clients_map : host clients id pool
415 * @allow_fixed_address: allow user space to connect a fixed client
416 * @override_fixed_address: force allow fixed address behavior
418 * @amthif_cmd_list : amthif list for cmd waiting
419 * @iamthif_cl : amthif host client
420 * @iamthif_open_count : number of opened amthif connections
421 * @iamthif_stall_timer : timer to detect amthif hang
422 * @iamthif_state : amthif processor state
423 * @iamthif_canceled : current amthif command is canceled
425 * @reset_work : work item for the device reset
426 * @bus_rescan_work : work item for the bus rescan
428 * @device_list : mei client bus list
429 * @cl_bus_lock : client bus list lock
431 * @dbgfs_dir : debugfs mei root directory
433 * @ops: : hw specific operations
434 * @hw : hw specific data
441 struct mei_cl_cb write_list
;
442 struct mei_cl_cb write_waiting_list
;
443 struct mei_cl_cb ctrl_wr_list
;
444 struct mei_cl_cb ctrl_rd_list
;
446 struct list_head file_list
;
447 long open_handle_count
;
449 struct mutex device_lock
;
450 struct delayed_work timer_work
;
454 * waiting queue for receive message from FW
456 wait_queue_head_t wait_hw_ready
;
457 wait_queue_head_t wait_pg
;
458 wait_queue_head_t wait_hbm_start
;
463 unsigned long reset_count
;
464 enum mei_dev_state dev_state
;
465 enum mei_hbm_state hbm_state
;
466 u16 init_clients_timer
;
469 * Power Gating support
471 enum mei_pg_event pg_event
;
473 struct dev_pm_domain pg_domain
;
474 #endif /* CONFIG_PM */
476 unsigned char rd_msg_buf
[MEI_RD_MSG_BUF_SIZE
];
483 struct hbm_version version
;
484 unsigned int hbm_f_pg_supported
:1;
485 unsigned int hbm_f_dc_supported
:1;
486 unsigned int hbm_f_dot_supported
:1;
487 unsigned int hbm_f_ev_supported
:1;
488 unsigned int hbm_f_fa_supported
:1;
489 unsigned int hbm_f_ie_supported
:1;
491 struct rw_semaphore me_clients_rwsem
;
492 struct list_head me_clients
;
493 DECLARE_BITMAP(me_clients_map
, MEI_CLIENTS_MAX
);
494 DECLARE_BITMAP(host_clients_map
, MEI_CLIENTS_MAX
);
496 bool allow_fixed_address
;
497 bool override_fixed_address
;
499 /* amthif list for cmd waiting */
500 struct mei_cl_cb amthif_cmd_list
;
501 struct mei_cl iamthif_cl
;
502 long iamthif_open_count
;
503 u32 iamthif_stall_timer
;
504 enum iamthif_states iamthif_state
;
505 bool iamthif_canceled
;
507 struct work_struct reset_work
;
508 struct work_struct bus_rescan_work
;
510 /* List of bus devices */
511 struct list_head device_list
;
512 struct mutex cl_bus_lock
;
514 #if IS_ENABLED(CONFIG_DEBUG_FS)
515 struct dentry
*dbgfs_dir
;
516 #endif /* CONFIG_DEBUG_FS */
519 const struct mei_hw_ops
*ops
;
520 char hw
[0] __aligned(sizeof(void *));
523 static inline unsigned long mei_secs_to_jiffies(unsigned long sec
)
525 return msecs_to_jiffies(sec
* MSEC_PER_SEC
);
529 * mei_data2slots - get slots - number of (dwords) from a message length
530 * + size of the mei header
532 * @length: size of the messages in bytes
534 * Return: number of slots
536 static inline u32
mei_data2slots(size_t length
)
538 return DIV_ROUND_UP(sizeof(struct mei_msg_hdr
) + length
, 4);
542 * mei_slots2data - get data in slots - bytes from slots
544 * @slots: number of available slots
546 * Return: number of bytes in slots
548 static inline u32
mei_slots2data(int slots
)
554 * mei init function prototypes
556 void mei_device_init(struct mei_device
*dev
,
557 struct device
*device
,
558 const struct mei_hw_ops
*hw_ops
);
559 int mei_reset(struct mei_device
*dev
);
560 int mei_start(struct mei_device
*dev
);
561 int mei_restart(struct mei_device
*dev
);
562 void mei_stop(struct mei_device
*dev
);
563 void mei_cancel_work(struct mei_device
*dev
);
566 * MEI interrupt functions prototype
569 void mei_timer(struct work_struct
*work
);
570 void mei_schedule_stall_timer(struct mei_device
*dev
);
571 int mei_irq_read_handler(struct mei_device
*dev
,
572 struct mei_cl_cb
*cmpl_list
, s32
*slots
);
574 int mei_irq_write_handler(struct mei_device
*dev
, struct mei_cl_cb
*cmpl_list
);
575 void mei_irq_compl_handler(struct mei_device
*dev
, struct mei_cl_cb
*cmpl_list
);
578 * AMTHIF - AMT Host Interface Functions
580 void mei_amthif_reset_params(struct mei_device
*dev
);
582 int mei_amthif_host_init(struct mei_device
*dev
, struct mei_me_client
*me_cl
);
584 unsigned int mei_amthif_poll(struct file
*file
, poll_table
*wait
);
586 int mei_amthif_release(struct mei_device
*dev
, struct file
*file
);
588 int mei_amthif_write(struct mei_cl
*cl
, struct mei_cl_cb
*cb
);
589 int mei_amthif_run_next_cmd(struct mei_device
*dev
);
590 int mei_amthif_irq_write(struct mei_cl
*cl
, struct mei_cl_cb
*cb
,
591 struct mei_cl_cb
*cmpl_list
);
593 void mei_amthif_complete(struct mei_cl
*cl
, struct mei_cl_cb
*cb
);
594 int mei_amthif_irq_read_msg(struct mei_cl
*cl
,
595 struct mei_msg_hdr
*mei_hdr
,
596 struct mei_cl_cb
*complete_list
);
597 int mei_amthif_irq_read(struct mei_device
*dev
, s32
*slots
);
600 * Register Access Function
604 static inline void mei_hw_config(struct mei_device
*dev
)
606 dev
->ops
->hw_config(dev
);
609 static inline enum mei_pg_state
mei_pg_state(struct mei_device
*dev
)
611 return dev
->ops
->pg_state(dev
);
614 static inline bool mei_pg_in_transition(struct mei_device
*dev
)
616 return dev
->ops
->pg_in_transition(dev
);
619 static inline bool mei_pg_is_enabled(struct mei_device
*dev
)
621 return dev
->ops
->pg_is_enabled(dev
);
624 static inline int mei_hw_reset(struct mei_device
*dev
, bool enable
)
626 return dev
->ops
->hw_reset(dev
, enable
);
629 static inline int mei_hw_start(struct mei_device
*dev
)
631 return dev
->ops
->hw_start(dev
);
634 static inline void mei_clear_interrupts(struct mei_device
*dev
)
636 dev
->ops
->intr_clear(dev
);
639 static inline void mei_enable_interrupts(struct mei_device
*dev
)
641 dev
->ops
->intr_enable(dev
);
644 static inline void mei_disable_interrupts(struct mei_device
*dev
)
646 dev
->ops
->intr_disable(dev
);
649 static inline void mei_synchronize_irq(struct mei_device
*dev
)
651 dev
->ops
->synchronize_irq(dev
);
654 static inline bool mei_host_is_ready(struct mei_device
*dev
)
656 return dev
->ops
->host_is_ready(dev
);
658 static inline bool mei_hw_is_ready(struct mei_device
*dev
)
660 return dev
->ops
->hw_is_ready(dev
);
663 static inline bool mei_hbuf_is_ready(struct mei_device
*dev
)
665 return dev
->ops
->hbuf_is_ready(dev
);
668 static inline int mei_hbuf_empty_slots(struct mei_device
*dev
)
670 return dev
->ops
->hbuf_free_slots(dev
);
673 static inline size_t mei_hbuf_max_len(const struct mei_device
*dev
)
675 return dev
->ops
->hbuf_max_len(dev
);
678 static inline int mei_write_message(struct mei_device
*dev
,
679 struct mei_msg_hdr
*hdr
, const void *buf
)
681 return dev
->ops
->write(dev
, hdr
, buf
);
684 static inline u32
mei_read_hdr(const struct mei_device
*dev
)
686 return dev
->ops
->read_hdr(dev
);
689 static inline void mei_read_slots(struct mei_device
*dev
,
690 unsigned char *buf
, unsigned long len
)
692 dev
->ops
->read(dev
, buf
, len
);
695 static inline int mei_count_full_read_slots(struct mei_device
*dev
)
697 return dev
->ops
->rdbuf_full_slots(dev
);
700 static inline int mei_fw_status(struct mei_device
*dev
,
701 struct mei_fw_status
*fw_status
)
703 return dev
->ops
->fw_status(dev
, fw_status
);
706 bool mei_hbuf_acquire(struct mei_device
*dev
);
708 bool mei_write_is_idle(struct mei_device
*dev
);
710 void mei_irq_discard_msg(struct mei_device
*dev
, struct mei_msg_hdr
*hdr
);
712 #if IS_ENABLED(CONFIG_DEBUG_FS)
713 int mei_dbgfs_register(struct mei_device
*dev
, const char *name
);
714 void mei_dbgfs_deregister(struct mei_device
*dev
);
716 static inline int mei_dbgfs_register(struct mei_device
*dev
, const char *name
)
720 static inline void mei_dbgfs_deregister(struct mei_device
*dev
) {}
721 #endif /* CONFIG_DEBUG_FS */
723 int mei_register(struct mei_device
*dev
, struct device
*parent
);
724 void mei_deregister(struct mei_device
*dev
);
726 #define MEI_HDR_FMT "hdr:host=%02d me=%02d len=%d internal=%1d comp=%1d"
727 #define MEI_HDR_PRM(hdr) \
728 (hdr)->host_addr, (hdr)->me_addr, \
729 (hdr)->length, (hdr)->internal, (hdr)->msg_complete
731 ssize_t
mei_fw_status2str(struct mei_fw_status
*fw_sts
, char *buf
, size_t len
);
733 * mei_fw_status_str - fetch and convert fw status registers to printable string
735 * @dev: the device structure
736 * @buf: string buffer at minimal size MEI_FW_STATUS_STR_SZ
737 * @len: buffer len must be >= MEI_FW_STATUS_STR_SZ
739 * Return: number of bytes written or < 0 on failure
741 static inline ssize_t
mei_fw_status_str(struct mei_device
*dev
,
742 char *buf
, size_t len
)
744 struct mei_fw_status fw_status
;
749 ret
= mei_fw_status(dev
, &fw_status
);
753 ret
= mei_fw_status2str(&fw_status
, buf
, MEI_FW_STATUS_STR_SZ
);