1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright (c) 2003-2018, Intel Corporation. All rights reserved.
4 * Intel Management Engine Interface (Intel MEI) Linux driver
10 #include <linux/types.h>
11 #include <linux/cdev.h>
12 #include <linux/poll.h>
13 #include <linux/mei.h>
14 #include <linux/mei_cl_bus.h>
19 #define MEI_SLOT_SIZE sizeof(u32)
20 #define MEI_RD_MSG_BUF_SIZE (128 * MEI_SLOT_SIZE)
23 * Number of Maximum MEI Clients
25 #define MEI_CLIENTS_MAX 256
28 * maximum number of consecutive resets
30 #define MEI_MAX_CONSEC_RESET 3
33 * Number of File descriptors/handles
34 * that can be opened to the driver.
36 * Limit to 255: 256 Total Clients
37 * minus internal client for MEI Bus Messages
39 #define MEI_MAX_OPEN_HANDLE_COUNT (MEI_CLIENTS_MAX - 1)
43 MEI_FILE_UNINITIALIZED = 0,
44 MEI_FILE_INITIALIZING,
47 MEI_FILE_DISCONNECTING,
48 MEI_FILE_DISCONNECT_REPLY,
49 MEI_FILE_DISCONNECT_REQUIRED,
50 MEI_FILE_DISCONNECTED,
53 /* MEI device states */
55 MEI_DEV_INITIALIZING = 0,
64 const char *mei_dev_state_str(int state);
66 enum mei_file_transaction_states {
73 * enum mei_cb_file_ops - file operation associated with the callback
75 * @MEI_FOP_WRITE: write
76 * @MEI_FOP_CONNECT: connect
77 * @MEI_FOP_DISCONNECT: disconnect
78 * @MEI_FOP_DISCONNECT_RSP: disconnect response
79 * @MEI_FOP_NOTIFY_START: start notification
80 * @MEI_FOP_NOTIFY_STOP: stop notification
82 enum mei_cb_file_ops {
87 MEI_FOP_DISCONNECT_RSP,
93 * enum mei_cl_io_mode - io mode between driver and fw
95 * @MEI_CL_IO_TX_BLOCKING: send is blocking
96 * @MEI_CL_IO_TX_INTERNAL: internal communication between driver and FW
98 * @MEI_CL_IO_RX_NONBLOCK: recv is non-blocking
100 enum mei_cl_io_mode {
101 MEI_CL_IO_TX_BLOCKING = BIT(0),
102 MEI_CL_IO_TX_INTERNAL = BIT(1),
104 MEI_CL_IO_RX_NONBLOCK = BIT(2),
108 * Intel MEI message data struct
110 struct mei_msg_data {
116 * struct mei_dma_dscr - dma address descriptor
118 * @vaddr: dma buffer virtual address
119 * @daddr: dma buffer physical address
120 * @size : dma buffer size
122 struct mei_dma_dscr {
128 /* Maximum number of processed FW status registers */
129 #define MEI_FW_STATUS_MAX 6
130 /* Minimal buffer for FW status string (8 bytes in dw + space or '\0') */
131 #define MEI_FW_STATUS_STR_SZ (MEI_FW_STATUS_MAX * (8 + 1))
135 * struct mei_fw_status - storage of FW status data
137 * @count: number of actually available elements in array
138 * @status: FW status registers
140 struct mei_fw_status {
142 u32 status[MEI_FW_STATUS_MAX];
146 * struct mei_me_client - representation of me (fw) client
148 * @list: link in me client list
149 * @refcnt: struct reference count
150 * @props: client properties
151 * @client_id: me client id
152 * @tx_flow_ctrl_creds: flow control credits
153 * @connect_count: number connections to this client
154 * @bus_added: added to bus
156 struct mei_me_client {
157 struct list_head list;
159 struct mei_client_properties props;
161 u8 tx_flow_ctrl_creds;
170 * struct mei_cl_cb - file operation callback structure
172 * @list: link in callback queue
173 * @cl: file client who is running this operation
174 * @fop_type: file operation type
175 * @buf: buffer for data associated with the callback
176 * @buf_idx: last read index
177 * @fp: pointer to file structure
178 * @status: io status of the cb
179 * @internal: communication between driver and FW flag
180 * @blocking: transmission blocking mode
183 struct list_head list;
185 enum mei_cb_file_ops fop_type;
186 struct mei_msg_data buf;
188 const struct file *fp;
195 * struct mei_cl - me client host representation
196 * carried in file->private_data
198 * @link: link in the clients list
199 * @dev: mei parent device
200 * @state: file operation state
201 * @tx_wait: wait queue for tx completion
202 * @rx_wait: wait queue for rx completion
203 * @wait: wait queue for management operation
204 * @ev_wait: notification wait queue
205 * @ev_async: event async notification
206 * @status: connection status
207 * @me_cl: fw client connected
208 * @fp: file associated with client
209 * @host_client_id: host id
210 * @tx_flow_ctrl_creds: transmit flow credentials
211 * @rx_flow_ctrl_creds: receive flow credentials
212 * @timer_count: watchdog timer for operation completion
213 * @notify_en: notification - enabled/disabled
214 * @notify_ev: pending notification event
215 * @tx_cb_queued: number of tx callbacks in queue
216 * @writing_state: state of the tx
217 * @rd_pending: pending read credits
218 * @rd_completed: completed read
220 * @cldev: device on the mei client bus
223 struct list_head link;
224 struct mei_device *dev;
225 enum file_state state;
226 wait_queue_head_t tx_wait;
227 wait_queue_head_t rx_wait;
228 wait_queue_head_t wait;
229 wait_queue_head_t ev_wait;
230 struct fasync_struct *ev_async;
232 struct mei_me_client *me_cl;
233 const struct file *fp;
235 u8 tx_flow_ctrl_creds;
236 u8 rx_flow_ctrl_creds;
241 enum mei_file_transaction_states writing_state;
242 struct list_head rd_pending;
243 struct list_head rd_completed;
245 struct mei_cl_device *cldev;
248 #define MEI_TX_QUEUE_LIMIT_DEFAULT 50
249 #define MEI_TX_QUEUE_LIMIT_MAX 255
250 #define MEI_TX_QUEUE_LIMIT_MIN 30
253 * struct mei_hw_ops - hw specific ops
255 * @host_is_ready : query for host readiness
257 * @hw_is_ready : query if hw is ready
258 * @hw_reset : reset hw
259 * @hw_start : start hw after reset
260 * @hw_config : configure hw
262 * @fw_status : get fw status registers
263 * @pg_state : power gating state of the device
264 * @pg_in_transition : is device now in pg transition
265 * @pg_is_enabled : is power gating enabled
267 * @intr_clear : clear pending interrupts
268 * @intr_enable : enable interrupts
269 * @intr_disable : disable interrupts
270 * @synchronize_irq : synchronize irqs
272 * @hbuf_free_slots : query for write buffer empty slots
273 * @hbuf_is_ready : query if write buffer is empty
274 * @hbuf_depth : query for write buffer depth
276 * @write : write a message to FW
278 * @rdbuf_full_slots : query how many slots are filled
280 * @read_hdr : get first 4 bytes (header)
281 * @read : read a buffer from the FW
285 bool (*host_is_ready)(struct mei_device *dev);
287 bool (*hw_is_ready)(struct mei_device *dev);
288 int (*hw_reset)(struct mei_device *dev, bool enable);
289 int (*hw_start)(struct mei_device *dev);
290 void (*hw_config)(struct mei_device *dev);
292 int (*fw_status)(struct mei_device *dev, struct mei_fw_status *fw_sts);
293 enum mei_pg_state (*pg_state)(struct mei_device *dev);
294 bool (*pg_in_transition)(struct mei_device *dev);
295 bool (*pg_is_enabled)(struct mei_device *dev);
297 void (*intr_clear)(struct mei_device *dev);
298 void (*intr_enable)(struct mei_device *dev);
299 void (*intr_disable)(struct mei_device *dev);
300 void (*synchronize_irq)(struct mei_device *dev);
302 int (*hbuf_free_slots)(struct mei_device *dev);
303 bool (*hbuf_is_ready)(struct mei_device *dev);
304 u32 (*hbuf_depth)(const struct mei_device *dev);
305 int (*write)(struct mei_device *dev,
306 const void *hdr, size_t hdr_len,
307 const void *data, size_t data_len);
309 int (*rdbuf_full_slots)(struct mei_device *dev);
311 u32 (*read_hdr)(const struct mei_device *dev);
312 int (*read)(struct mei_device *dev,
313 unsigned char *buf, unsigned long len);
317 void mei_cl_bus_rescan_work(struct work_struct *work);
318 void mei_cl_bus_dev_fixup(struct mei_cl_device *dev);
319 ssize_t __mei_cl_send(struct mei_cl *cl, u8 *buf, size_t length,
321 ssize_t __mei_cl_recv(struct mei_cl *cl, u8 *buf, size_t length,
322 unsigned int mode, unsigned long timeout);
323 bool mei_cl_bus_rx_event(struct mei_cl *cl);
324 bool mei_cl_bus_notify_event(struct mei_cl *cl);
325 void mei_cl_bus_remove_devices(struct mei_device *bus);
326 int mei_cl_bus_init(void);
327 void mei_cl_bus_exit(void);
330 * enum mei_pg_event - power gating transition events
332 * @MEI_PG_EVENT_IDLE: the driver is not in power gating transition
333 * @MEI_PG_EVENT_WAIT: the driver is waiting for a pg event to complete
334 * @MEI_PG_EVENT_RECEIVED: the driver received pg event
335 * @MEI_PG_EVENT_INTR_WAIT: the driver is waiting for a pg event interrupt
336 * @MEI_PG_EVENT_INTR_RECEIVED: the driver received pg event interrupt
341 MEI_PG_EVENT_RECEIVED,
342 MEI_PG_EVENT_INTR_WAIT,
343 MEI_PG_EVENT_INTR_RECEIVED,
347 * enum mei_pg_state - device internal power gating state
349 * @MEI_PG_OFF: device is not power gated - it is active
350 * @MEI_PG_ON: device is power gated - it is in lower power state
357 const char *mei_pg_state_str(enum mei_pg_state state);
360 * struct mei_fw_version - MEI FW version struct
362 * @platform: platform identifier
363 * @major: major version field
364 * @minor: minor version field
365 * @buildno: build number version field
366 * @hotfix: hotfix number version field
368 struct mei_fw_version {
376 #define MEI_MAX_FW_VER_BLOCKS 3
379 * struct mei_device - MEI private device struct
381 * @dev : device on a bus
382 * @cdev : character device
383 * @minor : minor number allocated for device
385 * @write_list : write pending list
386 * @write_waiting_list : write completion list
387 * @ctrl_wr_list : pending control write list
388 * @ctrl_rd_list : pending control read list
389 * @tx_queue_limit: tx queues per client linit
391 * @file_list : list of opened handles
392 * @open_handle_count: number of opened handles
394 * @device_lock : big device lock
395 * @timer_work : MEI timer delayed work (timeouts)
397 * @recvd_hw_ready : hw ready message received flag
399 * @wait_hw_ready : wait queue for receive HW ready message form FW
400 * @wait_pg : wait queue for receive PG message from FW
401 * @wait_hbm_start : wait queue for receive HBM start message from FW
403 * @reset_count : number of consecutive resets
404 * @dev_state : device state
405 * @hbm_state : state of host bus message protocol
406 * @init_clients_timer : HBM init handshake timeout
408 * @pg_event : power gating event
409 * @pg_domain : runtime PM domain
411 * @rd_msg_buf : control messages buffer
412 * @rd_msg_hdr : read message header storage
414 * @hbuf_is_ready : query if the host host/write buffer is ready
415 * @dr_dscr: DMA ring descriptors: TX, RX, and CTRL
417 * @version : HBM protocol version in use
418 * @hbm_f_pg_supported : hbm feature pgi protocol
419 * @hbm_f_dc_supported : hbm feature dynamic clients
420 * @hbm_f_dot_supported : hbm feature disconnect on timeout
421 * @hbm_f_ev_supported : hbm feature event notification
422 * @hbm_f_fa_supported : hbm feature fixed address client
423 * @hbm_f_ie_supported : hbm feature immediate reply to enum request
424 * @hbm_f_os_supported : hbm feature support OS ver message
425 * @hbm_f_dr_supported : hbm feature dma ring supported
427 * @fw_ver : FW versions
429 * @me_clients_rwsem: rw lock over me_clients list
430 * @me_clients : list of FW clients
431 * @me_clients_map : FW clients bit map
432 * @host_clients_map : host clients id pool
434 * @allow_fixed_address: allow user space to connect a fixed client
435 * @override_fixed_address: force allow fixed address behavior
437 * @reset_work : work item for the device reset
438 * @bus_rescan_work : work item for the bus rescan
440 * @device_list : mei client bus list
441 * @cl_bus_lock : client bus list lock
443 * @dbgfs_dir : debugfs mei root directory
445 * @ops: : hw specific operations
446 * @hw : hw specific data
453 struct list_head write_list;
454 struct list_head write_waiting_list;
455 struct list_head ctrl_wr_list;
456 struct list_head ctrl_rd_list;
459 struct list_head file_list;
460 long open_handle_count;
462 struct mutex device_lock;
463 struct delayed_work timer_work;
467 * waiting queue for receive message from FW
469 wait_queue_head_t wait_hw_ready;
470 wait_queue_head_t wait_pg;
471 wait_queue_head_t wait_hbm_start;
476 unsigned long reset_count;
477 enum mei_dev_state dev_state;
478 enum mei_hbm_state hbm_state;
479 u16 init_clients_timer;
482 * Power Gating support
484 enum mei_pg_event pg_event;
486 struct dev_pm_domain pg_domain;
487 #endif /* CONFIG_PM */
489 unsigned char rd_msg_buf[MEI_RD_MSG_BUF_SIZE];
490 u32 rd_msg_hdr[MEI_MSG_HDR_MAX];
495 struct mei_dma_dscr dr_dscr[DMA_DSCR_NUM];
497 struct hbm_version version;
498 unsigned int hbm_f_pg_supported:1;
499 unsigned int hbm_f_dc_supported:1;
500 unsigned int hbm_f_dot_supported:1;
501 unsigned int hbm_f_ev_supported:1;
502 unsigned int hbm_f_fa_supported:1;
503 unsigned int hbm_f_ie_supported:1;
504 unsigned int hbm_f_os_supported:1;
505 unsigned int hbm_f_dr_supported:1;
507 struct mei_fw_version fw_ver[MEI_MAX_FW_VER_BLOCKS];
509 struct rw_semaphore me_clients_rwsem;
510 struct list_head me_clients;
511 DECLARE_BITMAP(me_clients_map, MEI_CLIENTS_MAX);
512 DECLARE_BITMAP(host_clients_map, MEI_CLIENTS_MAX);
514 bool allow_fixed_address;
515 bool override_fixed_address;
517 struct work_struct reset_work;
518 struct work_struct bus_rescan_work;
520 /* List of bus devices */
521 struct list_head device_list;
522 struct mutex cl_bus_lock;
524 #if IS_ENABLED(CONFIG_DEBUG_FS)
525 struct dentry *dbgfs_dir;
526 #endif /* CONFIG_DEBUG_FS */
528 const struct mei_hw_ops *ops;
529 char hw[0] __aligned(sizeof(void *));
532 static inline unsigned long mei_secs_to_jiffies(unsigned long sec)
534 return msecs_to_jiffies(sec * MSEC_PER_SEC);
538 * mei_data2slots - get slots number from a message length
540 * @length: size of the messages in bytes
542 * Return: number of slots
544 static inline u32 mei_data2slots(size_t length)
546 return DIV_ROUND_UP(length, MEI_SLOT_SIZE);
550 * mei_hbm2slots - get slots number from a hbm message length
551 * length + size of the mei message header
553 * @length: size of the messages in bytes
555 * Return: number of slots
557 static inline u32 mei_hbm2slots(size_t length)
559 return DIV_ROUND_UP(sizeof(struct mei_msg_hdr) + length, MEI_SLOT_SIZE);
563 * mei_slots2data - get data in slots - bytes from slots
565 * @slots: number of available slots
567 * Return: number of bytes in slots
569 static inline u32 mei_slots2data(int slots)
571 return slots * MEI_SLOT_SIZE;
575 * mei init function prototypes
577 void mei_device_init(struct mei_device *dev,
578 struct device *device,
579 const struct mei_hw_ops *hw_ops);
580 int mei_reset(struct mei_device *dev);
581 int mei_start(struct mei_device *dev);
582 int mei_restart(struct mei_device *dev);
583 void mei_stop(struct mei_device *dev);
584 void mei_cancel_work(struct mei_device *dev);
586 void mei_set_devstate(struct mei_device *dev, enum mei_dev_state state);
588 int mei_dmam_ring_alloc(struct mei_device *dev);
589 void mei_dmam_ring_free(struct mei_device *dev);
590 bool mei_dma_ring_is_allocated(struct mei_device *dev);
591 void mei_dma_ring_reset(struct mei_device *dev);
592 void mei_dma_ring_read(struct mei_device *dev, unsigned char *buf, u32 len);
593 void mei_dma_ring_write(struct mei_device *dev, unsigned char *buf, u32 len);
594 u32 mei_dma_ring_empty_slots(struct mei_device *dev);
597 * MEI interrupt functions prototype
600 void mei_timer(struct work_struct *work);
601 void mei_schedule_stall_timer(struct mei_device *dev);
602 int mei_irq_read_handler(struct mei_device *dev,
603 struct list_head *cmpl_list, s32 *slots);
605 int mei_irq_write_handler(struct mei_device *dev, struct list_head *cmpl_list);
606 void mei_irq_compl_handler(struct mei_device *dev, struct list_head *cmpl_list);
609 * Register Access Function
613 static inline void mei_hw_config(struct mei_device *dev)
615 dev->ops->hw_config(dev);
618 static inline enum mei_pg_state mei_pg_state(struct mei_device *dev)
620 return dev->ops->pg_state(dev);
623 static inline bool mei_pg_in_transition(struct mei_device *dev)
625 return dev->ops->pg_in_transition(dev);
628 static inline bool mei_pg_is_enabled(struct mei_device *dev)
630 return dev->ops->pg_is_enabled(dev);
633 static inline int mei_hw_reset(struct mei_device *dev, bool enable)
635 return dev->ops->hw_reset(dev, enable);
638 static inline int mei_hw_start(struct mei_device *dev)
640 return dev->ops->hw_start(dev);
643 static inline void mei_clear_interrupts(struct mei_device *dev)
645 dev->ops->intr_clear(dev);
648 static inline void mei_enable_interrupts(struct mei_device *dev)
650 dev->ops->intr_enable(dev);
653 static inline void mei_disable_interrupts(struct mei_device *dev)
655 dev->ops->intr_disable(dev);
658 static inline void mei_synchronize_irq(struct mei_device *dev)
660 dev->ops->synchronize_irq(dev);
663 static inline bool mei_host_is_ready(struct mei_device *dev)
665 return dev->ops->host_is_ready(dev);
667 static inline bool mei_hw_is_ready(struct mei_device *dev)
669 return dev->ops->hw_is_ready(dev);
672 static inline bool mei_hbuf_is_ready(struct mei_device *dev)
674 return dev->ops->hbuf_is_ready(dev);
677 static inline int mei_hbuf_empty_slots(struct mei_device *dev)
679 return dev->ops->hbuf_free_slots(dev);
682 static inline u32 mei_hbuf_depth(const struct mei_device *dev)
684 return dev->ops->hbuf_depth(dev);
687 static inline int mei_write_message(struct mei_device *dev,
688 const void *hdr, size_t hdr_len,
689 const void *data, size_t data_len)
691 return dev->ops->write(dev, hdr, hdr_len, data, data_len);
694 static inline u32 mei_read_hdr(const struct mei_device *dev)
696 return dev->ops->read_hdr(dev);
699 static inline void mei_read_slots(struct mei_device *dev,
700 unsigned char *buf, unsigned long len)
702 dev->ops->read(dev, buf, len);
705 static inline int mei_count_full_read_slots(struct mei_device *dev)
707 return dev->ops->rdbuf_full_slots(dev);
710 static inline int mei_fw_status(struct mei_device *dev,
711 struct mei_fw_status *fw_status)
713 return dev->ops->fw_status(dev, fw_status);
716 bool mei_hbuf_acquire(struct mei_device *dev);
718 bool mei_write_is_idle(struct mei_device *dev);
720 #if IS_ENABLED(CONFIG_DEBUG_FS)
721 int mei_dbgfs_register(struct mei_device *dev, const char *name);
722 void mei_dbgfs_deregister(struct mei_device *dev);
724 static inline int mei_dbgfs_register(struct mei_device *dev, const char *name)
728 static inline void mei_dbgfs_deregister(struct mei_device *dev) {}
729 #endif /* CONFIG_DEBUG_FS */
731 int mei_register(struct mei_device *dev, struct device *parent);
732 void mei_deregister(struct mei_device *dev);
734 #define MEI_HDR_FMT "hdr:host=%02d me=%02d len=%d dma=%1d internal=%1d comp=%1d"
735 #define MEI_HDR_PRM(hdr) \
736 (hdr)->host_addr, (hdr)->me_addr, \
737 (hdr)->length, (hdr)->dma_ring, (hdr)->internal, (hdr)->msg_complete
739 ssize_t mei_fw_status2str(struct mei_fw_status *fw_sts, char *buf, size_t len);
741 * mei_fw_status_str - fetch and convert fw status registers to printable string
743 * @dev: the device structure
744 * @buf: string buffer at minimal size MEI_FW_STATUS_STR_SZ
745 * @len: buffer len must be >= MEI_FW_STATUS_STR_SZ
747 * Return: number of bytes written or < 0 on failure
749 static inline ssize_t mei_fw_status_str(struct mei_device *dev,
750 char *buf, size_t len)
752 struct mei_fw_status fw_status;
757 ret = mei_fw_status(dev, &fw_status);
761 ret = mei_fw_status2str(&fw_status, buf, MEI_FW_STATUS_STR_SZ);