3 * Intel Management Engine Interface (Intel MEI) Linux driver
4 * Copyright (c) 2003-2018, 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>
29 #define MEI_SLOT_SIZE sizeof(u32)
30 #define MEI_RD_MSG_BUF_SIZE (128 * MEI_SLOT_SIZE)
33 * Number of Maximum MEI Clients
35 #define MEI_CLIENTS_MAX 256
38 * maximum number of consecutive resets
40 #define MEI_MAX_CONSEC_RESET 3
43 * Number of File descriptors/handles
44 * that can be opened to the driver.
46 * Limit to 255: 256 Total Clients
47 * minus internal client for MEI Bus Messages
49 #define MEI_MAX_OPEN_HANDLE_COUNT (MEI_CLIENTS_MAX - 1)
53 MEI_FILE_UNINITIALIZED = 0,
54 MEI_FILE_INITIALIZING,
57 MEI_FILE_DISCONNECTING,
58 MEI_FILE_DISCONNECT_REPLY,
59 MEI_FILE_DISCONNECT_REQUIRED,
60 MEI_FILE_DISCONNECTED,
63 /* MEI device states */
65 MEI_DEV_INITIALIZING = 0,
74 const char *mei_dev_state_str(int state);
76 enum mei_file_transaction_states {
83 * enum mei_cb_file_ops - file operation associated with the callback
85 * @MEI_FOP_WRITE: write
86 * @MEI_FOP_CONNECT: connect
87 * @MEI_FOP_DISCONNECT: disconnect
88 * @MEI_FOP_DISCONNECT_RSP: disconnect response
89 * @MEI_FOP_NOTIFY_START: start notification
90 * @MEI_FOP_NOTIFY_STOP: stop notification
92 enum mei_cb_file_ops {
97 MEI_FOP_DISCONNECT_RSP,
103 * enum mei_cl_io_mode - io mode between driver and fw
105 * @MEI_CL_IO_TX_BLOCKING: send is blocking
106 * @MEI_CL_IO_TX_INTERNAL: internal communication between driver and FW
108 * @MEI_CL_IO_RX_NONBLOCK: recv is non-blocking
110 enum mei_cl_io_mode {
111 MEI_CL_IO_TX_BLOCKING = BIT(0),
112 MEI_CL_IO_TX_INTERNAL = BIT(1),
114 MEI_CL_IO_RX_NONBLOCK = BIT(2),
118 * Intel MEI message data struct
120 struct mei_msg_data {
126 * struct mei_dma_dscr - dma address descriptor
128 * @vaddr: dma buffer virtual address
129 * @daddr: dma buffer physical address
130 * @size : dma buffer size
132 struct mei_dma_dscr {
138 /* Maximum number of processed FW status registers */
139 #define MEI_FW_STATUS_MAX 6
140 /* Minimal buffer for FW status string (8 bytes in dw + space or '\0') */
141 #define MEI_FW_STATUS_STR_SZ (MEI_FW_STATUS_MAX * (8 + 1))
145 * struct mei_fw_status - storage of FW status data
147 * @count: number of actually available elements in array
148 * @status: FW status registers
150 struct mei_fw_status {
152 u32 status[MEI_FW_STATUS_MAX];
156 * struct mei_me_client - representation of me (fw) client
158 * @list: link in me client list
159 * @refcnt: struct reference count
160 * @props: client properties
161 * @client_id: me client id
162 * @tx_flow_ctrl_creds: flow control credits
163 * @connect_count: number connections to this client
164 * @bus_added: added to bus
166 struct mei_me_client {
167 struct list_head list;
169 struct mei_client_properties props;
171 u8 tx_flow_ctrl_creds;
180 * struct mei_cl_cb - file operation callback structure
182 * @list: link in callback queue
183 * @cl: file client who is running this operation
184 * @fop_type: file operation type
185 * @buf: buffer for data associated with the callback
186 * @buf_idx: last read index
187 * @fp: pointer to file structure
188 * @status: io status of the cb
189 * @internal: communication between driver and FW flag
190 * @blocking: transmission blocking mode
193 struct list_head list;
195 enum mei_cb_file_ops fop_type;
196 struct mei_msg_data buf;
198 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 * @tx_cb_queued: number of tx callbacks in queue
226 * @writing_state: state of the tx
227 * @rd_pending: pending read credits
228 * @rd_completed: completed read
230 * @cldev: device on the mei client bus
233 struct list_head link;
234 struct mei_device *dev;
235 enum file_state state;
236 wait_queue_head_t tx_wait;
237 wait_queue_head_t rx_wait;
238 wait_queue_head_t wait;
239 wait_queue_head_t ev_wait;
240 struct fasync_struct *ev_async;
242 struct mei_me_client *me_cl;
243 const struct file *fp;
245 u8 tx_flow_ctrl_creds;
246 u8 rx_flow_ctrl_creds;
251 enum mei_file_transaction_states writing_state;
252 struct list_head rd_pending;
253 struct list_head rd_completed;
255 struct mei_cl_device *cldev;
258 #define MEI_TX_QUEUE_LIMIT_DEFAULT 50
259 #define MEI_TX_QUEUE_LIMIT_MAX 255
260 #define MEI_TX_QUEUE_LIMIT_MIN 30
263 * struct mei_hw_ops - hw specific ops
265 * @host_is_ready : query for host readiness
267 * @hw_is_ready : query if hw is ready
268 * @hw_reset : reset hw
269 * @hw_start : start hw after reset
270 * @hw_config : configure hw
272 * @fw_status : get fw status registers
273 * @pg_state : power gating state of the device
274 * @pg_in_transition : is device now in pg transition
275 * @pg_is_enabled : is power gating enabled
277 * @intr_clear : clear pending interrupts
278 * @intr_enable : enable interrupts
279 * @intr_disable : disable interrupts
280 * @synchronize_irq : synchronize irqs
282 * @hbuf_free_slots : query for write buffer empty slots
283 * @hbuf_is_ready : query if write buffer is empty
284 * @hbuf_depth : query for write buffer depth
286 * @write : write a message to FW
288 * @rdbuf_full_slots : query how many slots are filled
290 * @read_hdr : get first 4 bytes (header)
291 * @read : read a buffer from the FW
295 bool (*host_is_ready)(struct mei_device *dev);
297 bool (*hw_is_ready)(struct mei_device *dev);
298 int (*hw_reset)(struct mei_device *dev, bool enable);
299 int (*hw_start)(struct mei_device *dev);
300 void (*hw_config)(struct mei_device *dev);
302 int (*fw_status)(struct mei_device *dev, struct mei_fw_status *fw_sts);
303 enum mei_pg_state (*pg_state)(struct mei_device *dev);
304 bool (*pg_in_transition)(struct mei_device *dev);
305 bool (*pg_is_enabled)(struct mei_device *dev);
307 void (*intr_clear)(struct mei_device *dev);
308 void (*intr_enable)(struct mei_device *dev);
309 void (*intr_disable)(struct mei_device *dev);
310 void (*synchronize_irq)(struct mei_device *dev);
312 int (*hbuf_free_slots)(struct mei_device *dev);
313 bool (*hbuf_is_ready)(struct mei_device *dev);
314 u32 (*hbuf_depth)(const struct mei_device *dev);
315 int (*write)(struct mei_device *dev,
316 const void *hdr, size_t hdr_len,
317 const void *data, size_t data_len);
319 int (*rdbuf_full_slots)(struct mei_device *dev);
321 u32 (*read_hdr)(const struct mei_device *dev);
322 int (*read)(struct mei_device *dev,
323 unsigned char *buf, unsigned long len);
327 void mei_cl_bus_rescan_work(struct work_struct *work);
328 void mei_cl_bus_dev_fixup(struct mei_cl_device *dev);
329 ssize_t __mei_cl_send(struct mei_cl *cl, u8 *buf, size_t length,
331 ssize_t __mei_cl_recv(struct mei_cl *cl, u8 *buf, size_t length,
332 unsigned int mode, unsigned long timeout);
333 bool mei_cl_bus_rx_event(struct mei_cl *cl);
334 bool mei_cl_bus_notify_event(struct mei_cl *cl);
335 void mei_cl_bus_remove_devices(struct mei_device *bus);
336 int mei_cl_bus_init(void);
337 void mei_cl_bus_exit(void);
340 * enum mei_pg_event - power gating transition events
342 * @MEI_PG_EVENT_IDLE: the driver is not in power gating transition
343 * @MEI_PG_EVENT_WAIT: the driver is waiting for a pg event to complete
344 * @MEI_PG_EVENT_RECEIVED: the driver received pg event
345 * @MEI_PG_EVENT_INTR_WAIT: the driver is waiting for a pg event interrupt
346 * @MEI_PG_EVENT_INTR_RECEIVED: the driver received pg event interrupt
351 MEI_PG_EVENT_RECEIVED,
352 MEI_PG_EVENT_INTR_WAIT,
353 MEI_PG_EVENT_INTR_RECEIVED,
357 * enum mei_pg_state - device internal power gating state
359 * @MEI_PG_OFF: device is not power gated - it is active
360 * @MEI_PG_ON: device is power gated - it is in lower power state
367 const char *mei_pg_state_str(enum mei_pg_state state);
370 * struct mei_fw_version - MEI FW version struct
372 * @platform: platform identifier
373 * @major: major version field
374 * @minor: minor version field
375 * @buildno: build number version field
376 * @hotfix: hotfix number version field
378 struct mei_fw_version {
386 #define MEI_MAX_FW_VER_BLOCKS 3
389 * struct mei_device - MEI private device struct
391 * @dev : device on a bus
392 * @cdev : character device
393 * @minor : minor number allocated for device
395 * @write_list : write pending list
396 * @write_waiting_list : write completion list
397 * @ctrl_wr_list : pending control write list
398 * @ctrl_rd_list : pending control read list
399 * @tx_queue_limit: tx queues per client linit
401 * @file_list : list of opened handles
402 * @open_handle_count: number of opened handles
404 * @device_lock : big device lock
405 * @timer_work : MEI timer delayed work (timeouts)
407 * @recvd_hw_ready : hw ready message received flag
409 * @wait_hw_ready : wait queue for receive HW ready message form FW
410 * @wait_pg : wait queue for receive PG message from FW
411 * @wait_hbm_start : wait queue for receive HBM start message from FW
413 * @reset_count : number of consecutive resets
414 * @dev_state : device state
415 * @hbm_state : state of host bus message protocol
416 * @init_clients_timer : HBM init handshake timeout
418 * @pg_event : power gating event
419 * @pg_domain : runtime PM domain
421 * @rd_msg_buf : control messages buffer
422 * @rd_msg_hdr : read message header storage
424 * @hbuf_is_ready : query if the host host/write buffer is ready
425 * @dr_dscr: DMA ring descriptors: TX, RX, and CTRL
427 * @version : HBM protocol version in use
428 * @hbm_f_pg_supported : hbm feature pgi protocol
429 * @hbm_f_dc_supported : hbm feature dynamic clients
430 * @hbm_f_dot_supported : hbm feature disconnect on timeout
431 * @hbm_f_ev_supported : hbm feature event notification
432 * @hbm_f_fa_supported : hbm feature fixed address client
433 * @hbm_f_ie_supported : hbm feature immediate reply to enum request
434 * @hbm_f_os_supported : hbm feature support OS ver message
435 * @hbm_f_dr_supported : hbm feature dma ring supported
437 * @fw_ver : FW versions
439 * @me_clients_rwsem: rw lock over me_clients list
440 * @me_clients : list of FW clients
441 * @me_clients_map : FW clients bit map
442 * @host_clients_map : host clients id pool
444 * @allow_fixed_address: allow user space to connect a fixed client
445 * @override_fixed_address: force allow fixed address behavior
447 * @reset_work : work item for the device reset
448 * @bus_rescan_work : work item for the bus rescan
450 * @device_list : mei client bus list
451 * @cl_bus_lock : client bus list lock
453 * @dbgfs_dir : debugfs mei root directory
455 * @ops: : hw specific operations
456 * @hw : hw specific data
463 struct list_head write_list;
464 struct list_head write_waiting_list;
465 struct list_head ctrl_wr_list;
466 struct list_head ctrl_rd_list;
469 struct list_head file_list;
470 long open_handle_count;
472 struct mutex device_lock;
473 struct delayed_work timer_work;
477 * waiting queue for receive message from FW
479 wait_queue_head_t wait_hw_ready;
480 wait_queue_head_t wait_pg;
481 wait_queue_head_t wait_hbm_start;
486 unsigned long reset_count;
487 enum mei_dev_state dev_state;
488 enum mei_hbm_state hbm_state;
489 u16 init_clients_timer;
492 * Power Gating support
494 enum mei_pg_event pg_event;
496 struct dev_pm_domain pg_domain;
497 #endif /* CONFIG_PM */
499 unsigned char rd_msg_buf[MEI_RD_MSG_BUF_SIZE];
500 u32 rd_msg_hdr[MEI_MSG_HDR_MAX];
505 struct mei_dma_dscr dr_dscr[DMA_DSCR_NUM];
507 struct hbm_version version;
508 unsigned int hbm_f_pg_supported:1;
509 unsigned int hbm_f_dc_supported:1;
510 unsigned int hbm_f_dot_supported:1;
511 unsigned int hbm_f_ev_supported:1;
512 unsigned int hbm_f_fa_supported:1;
513 unsigned int hbm_f_ie_supported:1;
514 unsigned int hbm_f_os_supported:1;
515 unsigned int hbm_f_dr_supported:1;
517 struct mei_fw_version fw_ver[MEI_MAX_FW_VER_BLOCKS];
519 struct rw_semaphore me_clients_rwsem;
520 struct list_head me_clients;
521 DECLARE_BITMAP(me_clients_map, MEI_CLIENTS_MAX);
522 DECLARE_BITMAP(host_clients_map, MEI_CLIENTS_MAX);
524 bool allow_fixed_address;
525 bool override_fixed_address;
527 struct work_struct reset_work;
528 struct work_struct bus_rescan_work;
530 /* List of bus devices */
531 struct list_head device_list;
532 struct mutex cl_bus_lock;
534 #if IS_ENABLED(CONFIG_DEBUG_FS)
535 struct dentry *dbgfs_dir;
536 #endif /* CONFIG_DEBUG_FS */
539 const struct mei_hw_ops *ops;
540 char hw[0] __aligned(sizeof(void *));
543 static inline unsigned long mei_secs_to_jiffies(unsigned long sec)
545 return msecs_to_jiffies(sec * MSEC_PER_SEC);
549 * mei_data2slots - get slots number from a message length
551 * @length: size of the messages in bytes
553 * Return: number of slots
555 static inline u32 mei_data2slots(size_t length)
557 return DIV_ROUND_UP(length, MEI_SLOT_SIZE);
561 * mei_hbm2slots - get slots number from a hbm message length
562 * length + size of the mei message header
564 * @length: size of the messages in bytes
566 * Return: number of slots
568 static inline u32 mei_hbm2slots(size_t length)
570 return DIV_ROUND_UP(sizeof(struct mei_msg_hdr) + length, MEI_SLOT_SIZE);
574 * mei_slots2data - get data in slots - bytes from slots
576 * @slots: number of available slots
578 * Return: number of bytes in slots
580 static inline u32 mei_slots2data(int slots)
582 return slots * MEI_SLOT_SIZE;
586 * mei init function prototypes
588 void mei_device_init(struct mei_device *dev,
589 struct device *device,
590 const struct mei_hw_ops *hw_ops);
591 int mei_reset(struct mei_device *dev);
592 int mei_start(struct mei_device *dev);
593 int mei_restart(struct mei_device *dev);
594 void mei_stop(struct mei_device *dev);
595 void mei_cancel_work(struct mei_device *dev);
597 int mei_dmam_ring_alloc(struct mei_device *dev);
598 void mei_dmam_ring_free(struct mei_device *dev);
599 bool mei_dma_ring_is_allocated(struct mei_device *dev);
600 void mei_dma_ring_reset(struct mei_device *dev);
601 void mei_dma_ring_read(struct mei_device *dev, unsigned char *buf, u32 len);
602 void mei_dma_ring_write(struct mei_device *dev, unsigned char *buf, u32 len);
603 u32 mei_dma_ring_empty_slots(struct mei_device *dev);
606 * MEI interrupt functions prototype
609 void mei_timer(struct work_struct *work);
610 void mei_schedule_stall_timer(struct mei_device *dev);
611 int mei_irq_read_handler(struct mei_device *dev,
612 struct list_head *cmpl_list, s32 *slots);
614 int mei_irq_write_handler(struct mei_device *dev, struct list_head *cmpl_list);
615 void mei_irq_compl_handler(struct mei_device *dev, struct list_head *cmpl_list);
618 * Register Access Function
622 static inline void mei_hw_config(struct mei_device *dev)
624 dev->ops->hw_config(dev);
627 static inline enum mei_pg_state mei_pg_state(struct mei_device *dev)
629 return dev->ops->pg_state(dev);
632 static inline bool mei_pg_in_transition(struct mei_device *dev)
634 return dev->ops->pg_in_transition(dev);
637 static inline bool mei_pg_is_enabled(struct mei_device *dev)
639 return dev->ops->pg_is_enabled(dev);
642 static inline int mei_hw_reset(struct mei_device *dev, bool enable)
644 return dev->ops->hw_reset(dev, enable);
647 static inline int mei_hw_start(struct mei_device *dev)
649 return dev->ops->hw_start(dev);
652 static inline void mei_clear_interrupts(struct mei_device *dev)
654 dev->ops->intr_clear(dev);
657 static inline void mei_enable_interrupts(struct mei_device *dev)
659 dev->ops->intr_enable(dev);
662 static inline void mei_disable_interrupts(struct mei_device *dev)
664 dev->ops->intr_disable(dev);
667 static inline void mei_synchronize_irq(struct mei_device *dev)
669 dev->ops->synchronize_irq(dev);
672 static inline bool mei_host_is_ready(struct mei_device *dev)
674 return dev->ops->host_is_ready(dev);
676 static inline bool mei_hw_is_ready(struct mei_device *dev)
678 return dev->ops->hw_is_ready(dev);
681 static inline bool mei_hbuf_is_ready(struct mei_device *dev)
683 return dev->ops->hbuf_is_ready(dev);
686 static inline int mei_hbuf_empty_slots(struct mei_device *dev)
688 return dev->ops->hbuf_free_slots(dev);
691 static inline u32 mei_hbuf_depth(const struct mei_device *dev)
693 return dev->ops->hbuf_depth(dev);
696 static inline int mei_write_message(struct mei_device *dev,
697 const void *hdr, size_t hdr_len,
698 const void *data, size_t data_len)
700 return dev->ops->write(dev, hdr, hdr_len, data, data_len);
703 static inline u32 mei_read_hdr(const struct mei_device *dev)
705 return dev->ops->read_hdr(dev);
708 static inline void mei_read_slots(struct mei_device *dev,
709 unsigned char *buf, unsigned long len)
711 dev->ops->read(dev, buf, len);
714 static inline int mei_count_full_read_slots(struct mei_device *dev)
716 return dev->ops->rdbuf_full_slots(dev);
719 static inline int mei_fw_status(struct mei_device *dev,
720 struct mei_fw_status *fw_status)
722 return dev->ops->fw_status(dev, fw_status);
725 bool mei_hbuf_acquire(struct mei_device *dev);
727 bool mei_write_is_idle(struct mei_device *dev);
729 #if IS_ENABLED(CONFIG_DEBUG_FS)
730 int mei_dbgfs_register(struct mei_device *dev, const char *name);
731 void mei_dbgfs_deregister(struct mei_device *dev);
733 static inline int mei_dbgfs_register(struct mei_device *dev, const char *name)
737 static inline void mei_dbgfs_deregister(struct mei_device *dev) {}
738 #endif /* CONFIG_DEBUG_FS */
740 int mei_register(struct mei_device *dev, struct device *parent);
741 void mei_deregister(struct mei_device *dev);
743 #define MEI_HDR_FMT "hdr:host=%02d me=%02d len=%d dma=%1d internal=%1d comp=%1d"
744 #define MEI_HDR_PRM(hdr) \
745 (hdr)->host_addr, (hdr)->me_addr, \
746 (hdr)->length, (hdr)->dma_ring, (hdr)->internal, (hdr)->msg_complete
748 ssize_t mei_fw_status2str(struct mei_fw_status *fw_sts, char *buf, size_t len);
750 * mei_fw_status_str - fetch and convert fw status registers to printable string
752 * @dev: the device structure
753 * @buf: string buffer at minimal size MEI_FW_STATUS_STR_SZ
754 * @len: buffer len must be >= MEI_FW_STATUS_STR_SZ
756 * Return: number of bytes written or < 0 on failure
758 static inline ssize_t mei_fw_status_str(struct mei_device *dev,
759 char *buf, size_t len)
761 struct mei_fw_status fw_status;
766 ret = mei_fw_status(dev, &fw_status);
770 ret = mei_fw_status2str(&fw_status, buf, MEI_FW_STATUS_STR_SZ);