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