xref: /openbmc/linux/drivers/misc/mei/mei_dev.h (revision 709f34b4)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (c) 2003-2022, Intel Corporation. All rights reserved.
4  * Intel Management Engine Interface (Intel MEI) Linux driver
5  */
6 
7 #ifndef _MEI_DEV_H_
8 #define _MEI_DEV_H_
9 
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>
15 
16 static inline int uuid_le_cmp(const uuid_le u1, const uuid_le u2)
17 {
18 	return memcmp(&u1, &u2, sizeof(uuid_le));
19 }
20 
21 #include "hw.h"
22 #include "hbm.h"
23 
24 #define MEI_SLOT_SIZE             sizeof(u32)
25 #define MEI_RD_MSG_BUF_SIZE       (128 * MEI_SLOT_SIZE)
26 
27 /*
28  * Number of Maximum MEI Clients
29  */
30 #define MEI_CLIENTS_MAX 256
31 
32 /*
33  * maximum number of consecutive resets
34  */
35 #define MEI_MAX_CONSEC_RESET  3
36 
37 /*
38  * Number of File descriptors/handles
39  * that can be opened to the driver.
40  *
41  * Limit to 255: 256 Total Clients
42  * minus internal client for MEI Bus Messages
43  */
44 #define  MEI_MAX_OPEN_HANDLE_COUNT (MEI_CLIENTS_MAX - 1)
45 
46 /* File state */
47 enum file_state {
48 	MEI_FILE_UNINITIALIZED = 0,
49 	MEI_FILE_INITIALIZING,
50 	MEI_FILE_CONNECTING,
51 	MEI_FILE_CONNECTED,
52 	MEI_FILE_DISCONNECTING,
53 	MEI_FILE_DISCONNECT_REPLY,
54 	MEI_FILE_DISCONNECT_REQUIRED,
55 	MEI_FILE_DISCONNECTED,
56 };
57 
58 /* MEI device states */
59 enum mei_dev_state {
60 	MEI_DEV_INITIALIZING = 0,
61 	MEI_DEV_INIT_CLIENTS,
62 	MEI_DEV_ENABLED,
63 	MEI_DEV_RESETTING,
64 	MEI_DEV_DISABLED,
65 	MEI_DEV_POWERING_DOWN,
66 	MEI_DEV_POWER_DOWN,
67 	MEI_DEV_POWER_UP
68 };
69 
70 /**
71  * enum mei_dev_pxp_mode - MEI PXP mode state
72  *
73  * @MEI_DEV_PXP_DEFAULT: PCH based device, no initailization required
74  * @MEI_DEV_PXP_INIT:    device requires initialization, send setup message to firmware
75  * @MEI_DEV_PXP_SETUP:   device is in setup stage, waiting for firmware repsonse
76  * @MEI_DEV_PXP_READY:   device initialized
77  */
78 enum mei_dev_pxp_mode {
79 	MEI_DEV_PXP_DEFAULT = 0,
80 	MEI_DEV_PXP_INIT    = 1,
81 	MEI_DEV_PXP_SETUP   = 2,
82 	MEI_DEV_PXP_READY   = 3,
83 };
84 
85 const char *mei_dev_state_str(int state);
86 
87 enum mei_file_transaction_states {
88 	MEI_IDLE,
89 	MEI_WRITING,
90 	MEI_WRITE_COMPLETE,
91 };
92 
93 /**
94  * enum mei_cb_file_ops  - file operation associated with the callback
95  * @MEI_FOP_READ:       read
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
102  * @MEI_FOP_DMA_MAP:   request client dma map
103  * @MEI_FOP_DMA_UNMAP: request client dma unmap
104  */
105 enum mei_cb_file_ops {
106 	MEI_FOP_READ = 0,
107 	MEI_FOP_WRITE,
108 	MEI_FOP_CONNECT,
109 	MEI_FOP_DISCONNECT,
110 	MEI_FOP_DISCONNECT_RSP,
111 	MEI_FOP_NOTIFY_START,
112 	MEI_FOP_NOTIFY_STOP,
113 	MEI_FOP_DMA_MAP,
114 	MEI_FOP_DMA_UNMAP,
115 };
116 
117 /**
118  * enum mei_cl_io_mode - io mode between driver and fw
119  *
120  * @MEI_CL_IO_TX_BLOCKING: send is blocking
121  * @MEI_CL_IO_TX_INTERNAL: internal communication between driver and FW
122  *
123  * @MEI_CL_IO_RX_NONBLOCK: recv is non-blocking
124  *
125  * @MEI_CL_IO_SGL: send command with sgl list.
126  */
127 enum mei_cl_io_mode {
128 	MEI_CL_IO_TX_BLOCKING = BIT(0),
129 	MEI_CL_IO_TX_INTERNAL = BIT(1),
130 
131 	MEI_CL_IO_RX_NONBLOCK = BIT(2),
132 
133 	MEI_CL_IO_SGL         = BIT(3),
134 };
135 
136 /*
137  * Intel MEI message data struct
138  */
139 struct mei_msg_data {
140 	size_t size;
141 	unsigned char *data;
142 };
143 
144 struct mei_dma_data {
145 	u8 buffer_id;
146 	void *vaddr;
147 	dma_addr_t daddr;
148 	size_t size;
149 };
150 
151 /**
152  * struct mei_dma_dscr - dma address descriptor
153  *
154  * @vaddr: dma buffer virtual address
155  * @daddr: dma buffer physical address
156  * @size : dma buffer size
157  */
158 struct mei_dma_dscr {
159 	void *vaddr;
160 	dma_addr_t daddr;
161 	size_t size;
162 };
163 
164 /* Maximum number of processed FW status registers */
165 #define MEI_FW_STATUS_MAX 6
166 /* Minimal  buffer for FW status string (8 bytes in dw + space or '\0') */
167 #define MEI_FW_STATUS_STR_SZ (MEI_FW_STATUS_MAX * (8 + 1))
168 
169 
170 /*
171  * struct mei_fw_status - storage of FW status data
172  *
173  * @count: number of actually available elements in array
174  * @status: FW status registers
175  */
176 struct mei_fw_status {
177 	int count;
178 	u32 status[MEI_FW_STATUS_MAX];
179 };
180 
181 /**
182  * struct mei_me_client - representation of me (fw) client
183  *
184  * @list: link in me client list
185  * @refcnt: struct reference count
186  * @props: client properties
187  * @client_id: me client id
188  * @tx_flow_ctrl_creds: flow control credits
189  * @connect_count: number connections to this client
190  * @bus_added: added to bus
191  */
192 struct mei_me_client {
193 	struct list_head list;
194 	struct kref refcnt;
195 	struct mei_client_properties props;
196 	u8 client_id;
197 	u8 tx_flow_ctrl_creds;
198 	u8 connect_count;
199 	u8 bus_added;
200 };
201 
202 
203 struct mei_cl;
204 
205 /**
206  * struct mei_cl_cb - file operation callback structure
207  *
208  * @list: link in callback queue
209  * @cl: file client who is running this operation
210  * @fop_type: file operation type
211  * @buf: buffer for data associated with the callback
212  * @buf_idx: last read index
213  * @vtag: virtual tag
214  * @fp: pointer to file structure
215  * @status: io status of the cb
216  * @internal: communication between driver and FW flag
217  * @blocking: transmission blocking mode
218  * @ext_hdr: extended header
219  */
220 struct mei_cl_cb {
221 	struct list_head list;
222 	struct mei_cl *cl;
223 	enum mei_cb_file_ops fop_type;
224 	struct mei_msg_data buf;
225 	size_t buf_idx;
226 	u8 vtag;
227 	const struct file *fp;
228 	int status;
229 	u32 internal:1;
230 	u32 blocking:1;
231 	struct mei_ext_hdr *ext_hdr;
232 };
233 
234 /**
235  * struct mei_cl_vtag - file pointer to vtag mapping structure
236  *
237  * @list: link in map queue
238  * @fp: file pointer
239  * @vtag: corresponding vtag
240  * @pending_read: the read is pending on this file
241  */
242 struct mei_cl_vtag {
243 	struct list_head list;
244 	const struct file *fp;
245 	u8 vtag;
246 	u8 pending_read:1;
247 };
248 
249 /**
250  * struct mei_cl - me client host representation
251  *    carried in file->private_data
252  *
253  * @link: link in the clients list
254  * @dev: mei parent device
255  * @state: file operation state
256  * @tx_wait: wait queue for tx completion
257  * @rx_wait: wait queue for rx completion
258  * @wait:  wait queue for management operation
259  * @ev_wait: notification wait queue
260  * @ev_async: event async notification
261  * @status: connection status
262  * @me_cl: fw client connected
263  * @fp: file associated with client
264  * @host_client_id: host id
265  * @vtag_map: vtag map
266  * @tx_flow_ctrl_creds: transmit flow credentials
267  * @rx_flow_ctrl_creds: receive flow credentials
268  * @timer_count:  watchdog timer for operation completion
269  * @notify_en: notification - enabled/disabled
270  * @notify_ev: pending notification event
271  * @tx_cb_queued: number of tx callbacks in queue
272  * @writing_state: state of the tx
273  * @rd_pending: pending read credits
274  * @rd_completed_lock: protects rd_completed queue
275  * @rd_completed: completed read
276  * @dma: dma settings
277  * @dma_mapped: dma buffer is currently mapped.
278  *
279  * @cldev: device on the mei client bus
280  */
281 struct mei_cl {
282 	struct list_head link;
283 	struct mei_device *dev;
284 	enum file_state state;
285 	wait_queue_head_t tx_wait;
286 	wait_queue_head_t rx_wait;
287 	wait_queue_head_t wait;
288 	wait_queue_head_t ev_wait;
289 	struct fasync_struct *ev_async;
290 	int status;
291 	struct mei_me_client *me_cl;
292 	const struct file *fp;
293 	u8 host_client_id;
294 	struct list_head vtag_map;
295 	u8 tx_flow_ctrl_creds;
296 	u8 rx_flow_ctrl_creds;
297 	u8 timer_count;
298 	u8 notify_en;
299 	u8 notify_ev;
300 	u8 tx_cb_queued;
301 	enum mei_file_transaction_states writing_state;
302 	struct list_head rd_pending;
303 	spinlock_t rd_completed_lock; /* protects rd_completed queue */
304 	struct list_head rd_completed;
305 	struct mei_dma_data dma;
306 	u8 dma_mapped;
307 
308 	struct mei_cl_device *cldev;
309 };
310 
311 #define MEI_TX_QUEUE_LIMIT_DEFAULT 50
312 #define MEI_TX_QUEUE_LIMIT_MAX 255
313 #define MEI_TX_QUEUE_LIMIT_MIN 30
314 
315 /**
316  * struct mei_hw_ops - hw specific ops
317  *
318  * @host_is_ready    : query for host readiness
319  *
320  * @hw_is_ready      : query if hw is ready
321  * @hw_reset         : reset hw
322  * @hw_start         : start hw after reset
323  * @hw_config        : configure hw
324  *
325  * @fw_status        : get fw status registers
326  * @trc_status       : get trc status register
327  * @pg_state         : power gating state of the device
328  * @pg_in_transition : is device now in pg transition
329  * @pg_is_enabled    : is power gating enabled
330  *
331  * @intr_clear       : clear pending interrupts
332  * @intr_enable      : enable interrupts
333  * @intr_disable     : disable interrupts
334  * @synchronize_irq  : synchronize irqs
335  *
336  * @hbuf_free_slots  : query for write buffer empty slots
337  * @hbuf_is_ready    : query if write buffer is empty
338  * @hbuf_depth       : query for write buffer depth
339  *
340  * @write            : write a message to FW
341  *
342  * @rdbuf_full_slots : query how many slots are filled
343  *
344  * @read_hdr         : get first 4 bytes (header)
345  * @read             : read a buffer from the FW
346  */
347 struct mei_hw_ops {
348 
349 	bool (*host_is_ready)(struct mei_device *dev);
350 
351 	bool (*hw_is_ready)(struct mei_device *dev);
352 	int (*hw_reset)(struct mei_device *dev, bool enable);
353 	int (*hw_start)(struct mei_device *dev);
354 	int (*hw_config)(struct mei_device *dev);
355 
356 	int (*fw_status)(struct mei_device *dev, struct mei_fw_status *fw_sts);
357 	int (*trc_status)(struct mei_device *dev, u32 *trc);
358 
359 	enum mei_pg_state (*pg_state)(struct mei_device *dev);
360 	bool (*pg_in_transition)(struct mei_device *dev);
361 	bool (*pg_is_enabled)(struct mei_device *dev);
362 
363 	void (*intr_clear)(struct mei_device *dev);
364 	void (*intr_enable)(struct mei_device *dev);
365 	void (*intr_disable)(struct mei_device *dev);
366 	void (*synchronize_irq)(struct mei_device *dev);
367 
368 	int (*hbuf_free_slots)(struct mei_device *dev);
369 	bool (*hbuf_is_ready)(struct mei_device *dev);
370 	u32 (*hbuf_depth)(const struct mei_device *dev);
371 	int (*write)(struct mei_device *dev,
372 		     const void *hdr, size_t hdr_len,
373 		     const void *data, size_t data_len);
374 
375 	int (*rdbuf_full_slots)(struct mei_device *dev);
376 
377 	u32 (*read_hdr)(const struct mei_device *dev);
378 	int (*read)(struct mei_device *dev,
379 		     unsigned char *buf, unsigned long len);
380 };
381 
382 /* MEI bus API*/
383 void mei_cl_bus_rescan_work(struct work_struct *work);
384 void mei_cl_bus_dev_fixup(struct mei_cl_device *dev);
385 ssize_t __mei_cl_send(struct mei_cl *cl, const u8 *buf, size_t length, u8 vtag,
386 		      unsigned int mode);
387 ssize_t __mei_cl_send_timeout(struct mei_cl *cl, const u8 *buf, size_t length, u8 vtag,
388 			      unsigned int mode, unsigned long timeout);
389 ssize_t __mei_cl_recv(struct mei_cl *cl, u8 *buf, size_t length, u8 *vtag,
390 		      unsigned int mode, unsigned long timeout);
391 bool mei_cl_bus_rx_event(struct mei_cl *cl);
392 bool mei_cl_bus_notify_event(struct mei_cl *cl);
393 void mei_cl_bus_remove_devices(struct mei_device *bus);
394 int mei_cl_bus_init(void);
395 void mei_cl_bus_exit(void);
396 
397 /**
398  * enum mei_pg_event - power gating transition events
399  *
400  * @MEI_PG_EVENT_IDLE: the driver is not in power gating transition
401  * @MEI_PG_EVENT_WAIT: the driver is waiting for a pg event to complete
402  * @MEI_PG_EVENT_RECEIVED: the driver received pg event
403  * @MEI_PG_EVENT_INTR_WAIT: the driver is waiting for a pg event interrupt
404  * @MEI_PG_EVENT_INTR_RECEIVED: the driver received pg event interrupt
405  */
406 enum mei_pg_event {
407 	MEI_PG_EVENT_IDLE,
408 	MEI_PG_EVENT_WAIT,
409 	MEI_PG_EVENT_RECEIVED,
410 	MEI_PG_EVENT_INTR_WAIT,
411 	MEI_PG_EVENT_INTR_RECEIVED,
412 };
413 
414 /**
415  * enum mei_pg_state - device internal power gating state
416  *
417  * @MEI_PG_OFF: device is not power gated - it is active
418  * @MEI_PG_ON:  device is power gated - it is in lower power state
419  */
420 enum mei_pg_state {
421 	MEI_PG_OFF = 0,
422 	MEI_PG_ON =  1,
423 };
424 
425 const char *mei_pg_state_str(enum mei_pg_state state);
426 
427 /**
428  * struct mei_fw_version - MEI FW version struct
429  *
430  * @platform: platform identifier
431  * @major: major version field
432  * @minor: minor version field
433  * @buildno: build number version field
434  * @hotfix: hotfix number version field
435  */
436 struct mei_fw_version {
437 	u8 platform;
438 	u8 major;
439 	u16 minor;
440 	u16 buildno;
441 	u16 hotfix;
442 };
443 
444 #define MEI_MAX_FW_VER_BLOCKS 3
445 
446 struct mei_dev_timeouts {
447 	unsigned long hw_ready; /* Timeout on ready message, in jiffies */
448 	int connect; /* HPS: at least 2 seconds, in seconds */
449 	unsigned long cl_connect; /* HPS: Client Connect Timeout, in jiffies */
450 	int client_init; /* HPS: Clients Enumeration Timeout, in seconds */
451 	unsigned long pgi; /* PG Isolation time response, in jiffies */
452 	unsigned int d0i3; /* D0i3 set/unset max response time, in jiffies */
453 	unsigned long hbm; /* HBM operation timeout, in jiffies */
454 	unsigned long mkhi_recv; /* receive timeout, in jiffies */
455 };
456 
457 /**
458  * struct mei_device -  MEI private device struct
459  *
460  * @dev         : device on a bus
461  * @cdev        : character device
462  * @minor       : minor number allocated for device
463  *
464  * @write_list  : write pending list
465  * @write_waiting_list : write completion list
466  * @ctrl_wr_list : pending control write list
467  * @ctrl_rd_list : pending control read list
468  * @tx_queue_limit: tx queues per client linit
469  *
470  * @file_list   : list of opened handles
471  * @open_handle_count: number of opened handles
472  *
473  * @device_lock : big device lock
474  * @timer_work  : MEI timer delayed work (timeouts)
475  *
476  * @recvd_hw_ready : hw ready message received flag
477  *
478  * @wait_hw_ready : wait queue for receive HW ready message form FW
479  * @wait_pg     : wait queue for receive PG message from FW
480  * @wait_hbm_start : wait queue for receive HBM start message from FW
481  *
482  * @reset_count : number of consecutive resets
483  * @dev_state   : device state
484  * @hbm_state   : state of host bus message protocol
485  * @pxp_mode    : PXP device mode
486  * @init_clients_timer : HBM init handshake timeout
487  *
488  * @pg_event    : power gating event
489  * @pg_domain   : runtime PM domain
490  *
491  * @rd_msg_buf  : control messages buffer
492  * @rd_msg_hdr  : read message header storage
493  * @rd_msg_hdr_count : how many dwords were already read from header
494  *
495  * @hbuf_is_ready : query if the host host/write buffer is ready
496  * @dr_dscr: DMA ring descriptors: TX, RX, and CTRL
497  *
498  * @version     : HBM protocol version in use
499  * @hbm_f_pg_supported  : hbm feature pgi protocol
500  * @hbm_f_dc_supported  : hbm feature dynamic clients
501  * @hbm_f_dot_supported : hbm feature disconnect on timeout
502  * @hbm_f_ev_supported  : hbm feature event notification
503  * @hbm_f_fa_supported  : hbm feature fixed address client
504  * @hbm_f_ie_supported  : hbm feature immediate reply to enum request
505  * @hbm_f_os_supported  : hbm feature support OS ver message
506  * @hbm_f_dr_supported  : hbm feature dma ring supported
507  * @hbm_f_vt_supported  : hbm feature vtag supported
508  * @hbm_f_cap_supported : hbm feature capabilities message supported
509  * @hbm_f_cd_supported  : hbm feature client dma supported
510  * @hbm_f_gsc_supported : hbm feature gsc supported
511  *
512  * @fw_ver : FW versions
513  *
514  * @fw_f_fw_ver_supported : fw feature: fw version supported
515  *
516  * @me_clients_rwsem: rw lock over me_clients list
517  * @me_clients  : list of FW clients
518  * @me_clients_map : FW clients bit map
519  * @host_clients_map : host clients id pool
520  *
521  * @allow_fixed_address: allow user space to connect a fixed client
522  * @override_fixed_address: force allow fixed address behavior
523  *
524  * @timeouts: actual timeout values
525  *
526  * @reset_work  : work item for the device reset
527  * @bus_rescan_work : work item for the bus rescan
528  *
529  * @device_list : mei client bus list
530  * @cl_bus_lock : client bus list lock
531  *
532  * @kind        : kind of mei device
533  *
534  * @dbgfs_dir   : debugfs mei root directory
535  *
536  * @ops:        : hw specific operations
537  * @hw          : hw specific data
538  */
539 struct mei_device {
540 	struct device *dev;
541 	struct cdev cdev;
542 	int minor;
543 
544 	struct list_head write_list;
545 	struct list_head write_waiting_list;
546 	struct list_head ctrl_wr_list;
547 	struct list_head ctrl_rd_list;
548 	u8 tx_queue_limit;
549 
550 	struct list_head file_list;
551 	long open_handle_count;
552 
553 	struct mutex device_lock;
554 	struct delayed_work timer_work;
555 
556 	bool recvd_hw_ready;
557 	/*
558 	 * waiting queue for receive message from FW
559 	 */
560 	wait_queue_head_t wait_hw_ready;
561 	wait_queue_head_t wait_pg;
562 	wait_queue_head_t wait_hbm_start;
563 
564 	/*
565 	 * mei device  states
566 	 */
567 	unsigned long reset_count;
568 	enum mei_dev_state dev_state;
569 	enum mei_hbm_state hbm_state;
570 	enum mei_dev_pxp_mode pxp_mode;
571 	u16 init_clients_timer;
572 
573 	/*
574 	 * Power Gating support
575 	 */
576 	enum mei_pg_event pg_event;
577 #ifdef CONFIG_PM
578 	struct dev_pm_domain pg_domain;
579 #endif /* CONFIG_PM */
580 
581 	unsigned char rd_msg_buf[MEI_RD_MSG_BUF_SIZE];
582 	u32 rd_msg_hdr[MEI_RD_MSG_BUF_SIZE];
583 	int rd_msg_hdr_count;
584 
585 	/* write buffer */
586 	bool hbuf_is_ready;
587 
588 	struct mei_dma_dscr dr_dscr[DMA_DSCR_NUM];
589 
590 	struct hbm_version version;
591 	unsigned int hbm_f_pg_supported:1;
592 	unsigned int hbm_f_dc_supported:1;
593 	unsigned int hbm_f_dot_supported:1;
594 	unsigned int hbm_f_ev_supported:1;
595 	unsigned int hbm_f_fa_supported:1;
596 	unsigned int hbm_f_ie_supported:1;
597 	unsigned int hbm_f_os_supported:1;
598 	unsigned int hbm_f_dr_supported:1;
599 	unsigned int hbm_f_vt_supported:1;
600 	unsigned int hbm_f_cap_supported:1;
601 	unsigned int hbm_f_cd_supported:1;
602 	unsigned int hbm_f_gsc_supported:1;
603 
604 	struct mei_fw_version fw_ver[MEI_MAX_FW_VER_BLOCKS];
605 
606 	unsigned int fw_f_fw_ver_supported:1;
607 
608 	struct rw_semaphore me_clients_rwsem;
609 	struct list_head me_clients;
610 	DECLARE_BITMAP(me_clients_map, MEI_CLIENTS_MAX);
611 	DECLARE_BITMAP(host_clients_map, MEI_CLIENTS_MAX);
612 
613 	bool allow_fixed_address;
614 	bool override_fixed_address;
615 
616 	struct mei_dev_timeouts timeouts;
617 
618 	struct work_struct reset_work;
619 	struct work_struct bus_rescan_work;
620 
621 	/* List of bus devices */
622 	struct list_head device_list;
623 	struct mutex cl_bus_lock;
624 
625 	const char *kind;
626 
627 #if IS_ENABLED(CONFIG_DEBUG_FS)
628 	struct dentry *dbgfs_dir;
629 #endif /* CONFIG_DEBUG_FS */
630 
631 	const struct mei_hw_ops *ops;
632 	char hw[] __aligned(sizeof(void *));
633 };
634 
635 static inline unsigned long mei_secs_to_jiffies(unsigned long sec)
636 {
637 	return msecs_to_jiffies(sec * MSEC_PER_SEC);
638 }
639 
640 /**
641  * mei_data2slots - get slots number from a message length
642  *
643  * @length: size of the messages in bytes
644  *
645  * Return: number of slots
646  */
647 static inline u32 mei_data2slots(size_t length)
648 {
649 	return DIV_ROUND_UP(length, MEI_SLOT_SIZE);
650 }
651 
652 /**
653  * mei_hbm2slots - get slots number from a hbm message length
654  *                 length + size of the mei message header
655  *
656  * @length: size of the messages in bytes
657  *
658  * Return: number of slots
659  */
660 static inline u32 mei_hbm2slots(size_t length)
661 {
662 	return DIV_ROUND_UP(sizeof(struct mei_msg_hdr) + length, MEI_SLOT_SIZE);
663 }
664 
665 /**
666  * mei_slots2data - get data in slots - bytes from slots
667  *
668  * @slots: number of available slots
669  *
670  * Return: number of bytes in slots
671  */
672 static inline u32 mei_slots2data(int slots)
673 {
674 	return slots * MEI_SLOT_SIZE;
675 }
676 
677 /*
678  * mei init function prototypes
679  */
680 void mei_device_init(struct mei_device *dev,
681 		     struct device *device,
682 		     bool slow_fw,
683 		     const struct mei_hw_ops *hw_ops);
684 int mei_reset(struct mei_device *dev);
685 int mei_start(struct mei_device *dev);
686 int mei_restart(struct mei_device *dev);
687 void mei_stop(struct mei_device *dev);
688 void mei_cancel_work(struct mei_device *dev);
689 
690 void mei_set_devstate(struct mei_device *dev, enum mei_dev_state state);
691 
692 int mei_dmam_ring_alloc(struct mei_device *dev);
693 void mei_dmam_ring_free(struct mei_device *dev);
694 bool mei_dma_ring_is_allocated(struct mei_device *dev);
695 void mei_dma_ring_reset(struct mei_device *dev);
696 void mei_dma_ring_read(struct mei_device *dev, unsigned char *buf, u32 len);
697 void mei_dma_ring_write(struct mei_device *dev, unsigned char *buf, u32 len);
698 u32 mei_dma_ring_empty_slots(struct mei_device *dev);
699 
700 /*
701  *  MEI interrupt functions prototype
702  */
703 
704 void mei_timer(struct work_struct *work);
705 void mei_schedule_stall_timer(struct mei_device *dev);
706 int mei_irq_read_handler(struct mei_device *dev,
707 			 struct list_head *cmpl_list, s32 *slots);
708 
709 int mei_irq_write_handler(struct mei_device *dev, struct list_head *cmpl_list);
710 void mei_irq_compl_handler(struct mei_device *dev, struct list_head *cmpl_list);
711 
712 /*
713  * Register Access Function
714  */
715 
716 
717 static inline int mei_hw_config(struct mei_device *dev)
718 {
719 	return dev->ops->hw_config(dev);
720 }
721 
722 static inline enum mei_pg_state mei_pg_state(struct mei_device *dev)
723 {
724 	return dev->ops->pg_state(dev);
725 }
726 
727 static inline bool mei_pg_in_transition(struct mei_device *dev)
728 {
729 	return dev->ops->pg_in_transition(dev);
730 }
731 
732 static inline bool mei_pg_is_enabled(struct mei_device *dev)
733 {
734 	return dev->ops->pg_is_enabled(dev);
735 }
736 
737 static inline int mei_hw_reset(struct mei_device *dev, bool enable)
738 {
739 	return dev->ops->hw_reset(dev, enable);
740 }
741 
742 static inline int mei_hw_start(struct mei_device *dev)
743 {
744 	return dev->ops->hw_start(dev);
745 }
746 
747 static inline void mei_clear_interrupts(struct mei_device *dev)
748 {
749 	dev->ops->intr_clear(dev);
750 }
751 
752 static inline void mei_enable_interrupts(struct mei_device *dev)
753 {
754 	dev->ops->intr_enable(dev);
755 }
756 
757 static inline void mei_disable_interrupts(struct mei_device *dev)
758 {
759 	dev->ops->intr_disable(dev);
760 }
761 
762 static inline void mei_synchronize_irq(struct mei_device *dev)
763 {
764 	dev->ops->synchronize_irq(dev);
765 }
766 
767 static inline bool mei_host_is_ready(struct mei_device *dev)
768 {
769 	return dev->ops->host_is_ready(dev);
770 }
771 static inline bool mei_hw_is_ready(struct mei_device *dev)
772 {
773 	return dev->ops->hw_is_ready(dev);
774 }
775 
776 static inline bool mei_hbuf_is_ready(struct mei_device *dev)
777 {
778 	return dev->ops->hbuf_is_ready(dev);
779 }
780 
781 static inline int mei_hbuf_empty_slots(struct mei_device *dev)
782 {
783 	return dev->ops->hbuf_free_slots(dev);
784 }
785 
786 static inline u32 mei_hbuf_depth(const struct mei_device *dev)
787 {
788 	return dev->ops->hbuf_depth(dev);
789 }
790 
791 static inline int mei_write_message(struct mei_device *dev,
792 				    const void *hdr, size_t hdr_len,
793 				    const void *data, size_t data_len)
794 {
795 	return dev->ops->write(dev, hdr, hdr_len, data, data_len);
796 }
797 
798 static inline u32 mei_read_hdr(const struct mei_device *dev)
799 {
800 	return dev->ops->read_hdr(dev);
801 }
802 
803 static inline void mei_read_slots(struct mei_device *dev,
804 		     unsigned char *buf, unsigned long len)
805 {
806 	dev->ops->read(dev, buf, len);
807 }
808 
809 static inline int mei_count_full_read_slots(struct mei_device *dev)
810 {
811 	return dev->ops->rdbuf_full_slots(dev);
812 }
813 
814 static inline int mei_trc_status(struct mei_device *dev, u32 *trc)
815 {
816 	if (dev->ops->trc_status)
817 		return dev->ops->trc_status(dev, trc);
818 	return -EOPNOTSUPP;
819 }
820 
821 static inline int mei_fw_status(struct mei_device *dev,
822 				struct mei_fw_status *fw_status)
823 {
824 	return dev->ops->fw_status(dev, fw_status);
825 }
826 
827 bool mei_hbuf_acquire(struct mei_device *dev);
828 
829 bool mei_write_is_idle(struct mei_device *dev);
830 
831 #if IS_ENABLED(CONFIG_DEBUG_FS)
832 void mei_dbgfs_register(struct mei_device *dev, const char *name);
833 void mei_dbgfs_deregister(struct mei_device *dev);
834 #else
835 static inline void mei_dbgfs_register(struct mei_device *dev, const char *name) {}
836 static inline void mei_dbgfs_deregister(struct mei_device *dev) {}
837 #endif /* CONFIG_DEBUG_FS */
838 
839 int mei_register(struct mei_device *dev, struct device *parent);
840 void mei_deregister(struct mei_device *dev);
841 
842 #define MEI_HDR_FMT "hdr:host=%02d me=%02d len=%d dma=%1d ext=%1d internal=%1d comp=%1d"
843 #define MEI_HDR_PRM(hdr)                  \
844 	(hdr)->host_addr, (hdr)->me_addr, \
845 	(hdr)->length, (hdr)->dma_ring, (hdr)->extended, \
846 	(hdr)->internal, (hdr)->msg_complete
847 
848 ssize_t mei_fw_status2str(struct mei_fw_status *fw_sts, char *buf, size_t len);
849 /**
850  * mei_fw_status_str - fetch and convert fw status registers to printable string
851  *
852  * @dev: the device structure
853  * @buf: string buffer at minimal size MEI_FW_STATUS_STR_SZ
854  * @len: buffer len must be >= MEI_FW_STATUS_STR_SZ
855  *
856  * Return: number of bytes written or < 0 on failure
857  */
858 static inline ssize_t mei_fw_status_str(struct mei_device *dev,
859 					char *buf, size_t len)
860 {
861 	struct mei_fw_status fw_status;
862 	int ret;
863 
864 	buf[0] = '\0';
865 
866 	ret = mei_fw_status(dev, &fw_status);
867 	if (ret)
868 		return ret;
869 
870 	ret = mei_fw_status2str(&fw_status, buf, MEI_FW_STATUS_STR_SZ);
871 
872 	return ret;
873 }
874 
875 
876 #endif
877