xref: /linux/drivers/misc/mei/mei_dev.h (revision 666ed8bfd1de3b091cf32ca03b651757dd86cfff)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (c) 2003-2019, 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_POWER_DOWN,
61 	MEI_DEV_POWER_UP
62 };
63 
64 const char *mei_dev_state_str(int state);
65 
66 enum mei_file_transaction_states {
67 	MEI_IDLE,
68 	MEI_WRITING,
69 	MEI_WRITE_COMPLETE,
70 };
71 
72 /**
73  * enum mei_cb_file_ops  - file operation associated with the callback
74  * @MEI_FOP_READ:       read
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
81  */
82 enum mei_cb_file_ops {
83 	MEI_FOP_READ = 0,
84 	MEI_FOP_WRITE,
85 	MEI_FOP_CONNECT,
86 	MEI_FOP_DISCONNECT,
87 	MEI_FOP_DISCONNECT_RSP,
88 	MEI_FOP_NOTIFY_START,
89 	MEI_FOP_NOTIFY_STOP,
90 };
91 
92 /**
93  * enum mei_cl_io_mode - io mode between driver and fw
94  *
95  * @MEI_CL_IO_TX_BLOCKING: send is blocking
96  * @MEI_CL_IO_TX_INTERNAL: internal communication between driver and FW
97  *
98  * @MEI_CL_IO_RX_NONBLOCK: recv is non-blocking
99  */
100 enum mei_cl_io_mode {
101 	MEI_CL_IO_TX_BLOCKING = BIT(0),
102 	MEI_CL_IO_TX_INTERNAL = BIT(1),
103 
104 	MEI_CL_IO_RX_NONBLOCK = BIT(2),
105 };
106 
107 /*
108  * Intel MEI message data struct
109  */
110 struct mei_msg_data {
111 	size_t size;
112 	unsigned char *data;
113 };
114 
115 /**
116  * struct mei_dma_dscr - dma address descriptor
117  *
118  * @vaddr: dma buffer virtual address
119  * @daddr: dma buffer physical address
120  * @size : dma buffer size
121  */
122 struct mei_dma_dscr {
123 	void *vaddr;
124 	dma_addr_t daddr;
125 	size_t size;
126 };
127 
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))
132 
133 
134 /*
135  * struct mei_fw_status - storage of FW status data
136  *
137  * @count: number of actually available elements in array
138  * @status: FW status registers
139  */
140 struct mei_fw_status {
141 	int count;
142 	u32 status[MEI_FW_STATUS_MAX];
143 };
144 
145 /**
146  * struct mei_me_client - representation of me (fw) client
147  *
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
155  */
156 struct mei_me_client {
157 	struct list_head list;
158 	struct kref refcnt;
159 	struct mei_client_properties props;
160 	u8 client_id;
161 	u8 tx_flow_ctrl_creds;
162 	u8 connect_count;
163 	u8 bus_added;
164 };
165 
166 
167 struct mei_cl;
168 
169 /**
170  * struct mei_cl_cb - file operation callback structure
171  *
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
181  */
182 struct mei_cl_cb {
183 	struct list_head list;
184 	struct mei_cl *cl;
185 	enum mei_cb_file_ops fop_type;
186 	struct mei_msg_data buf;
187 	size_t buf_idx;
188 	const struct file *fp;
189 	int status;
190 	u32 internal:1;
191 	u32 blocking:1;
192 };
193 
194 /**
195  * struct mei_cl - me client host representation
196  *    carried in file->private_data
197  *
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
219  *
220  * @cldev: device on the mei client bus
221  */
222 struct mei_cl {
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;
231 	int status;
232 	struct mei_me_client *me_cl;
233 	const struct file *fp;
234 	u8 host_client_id;
235 	u8 tx_flow_ctrl_creds;
236 	u8 rx_flow_ctrl_creds;
237 	u8 timer_count;
238 	u8 notify_en;
239 	u8 notify_ev;
240 	u8 tx_cb_queued;
241 	enum mei_file_transaction_states writing_state;
242 	struct list_head rd_pending;
243 	struct list_head rd_completed;
244 
245 	struct mei_cl_device *cldev;
246 };
247 
248 #define MEI_TX_QUEUE_LIMIT_DEFAULT 50
249 #define MEI_TX_QUEUE_LIMIT_MAX 255
250 #define MEI_TX_QUEUE_LIMIT_MIN 30
251 
252 /**
253  * struct mei_hw_ops - hw specific ops
254  *
255  * @host_is_ready    : query for host readiness
256  *
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
261  *
262  * @fw_status        : get fw status registers
263  * @trc_status       : get trc status register
264  * @pg_state         : power gating state of the device
265  * @pg_in_transition : is device now in pg transition
266  * @pg_is_enabled    : is power gating enabled
267  *
268  * @intr_clear       : clear pending interrupts
269  * @intr_enable      : enable interrupts
270  * @intr_disable     : disable interrupts
271  * @synchronize_irq  : synchronize irqs
272  *
273  * @hbuf_free_slots  : query for write buffer empty slots
274  * @hbuf_is_ready    : query if write buffer is empty
275  * @hbuf_depth       : query for write buffer depth
276  *
277  * @write            : write a message to FW
278  *
279  * @rdbuf_full_slots : query how many slots are filled
280  *
281  * @read_hdr         : get first 4 bytes (header)
282  * @read             : read a buffer from the FW
283  */
284 struct mei_hw_ops {
285 
286 	bool (*host_is_ready)(struct mei_device *dev);
287 
288 	bool (*hw_is_ready)(struct mei_device *dev);
289 	int (*hw_reset)(struct mei_device *dev, bool enable);
290 	int (*hw_start)(struct mei_device *dev);
291 	int (*hw_config)(struct mei_device *dev);
292 
293 	int (*fw_status)(struct mei_device *dev, struct mei_fw_status *fw_sts);
294 	int (*trc_status)(struct mei_device *dev, u32 *trc);
295 
296 	enum mei_pg_state (*pg_state)(struct mei_device *dev);
297 	bool (*pg_in_transition)(struct mei_device *dev);
298 	bool (*pg_is_enabled)(struct mei_device *dev);
299 
300 	void (*intr_clear)(struct mei_device *dev);
301 	void (*intr_enable)(struct mei_device *dev);
302 	void (*intr_disable)(struct mei_device *dev);
303 	void (*synchronize_irq)(struct mei_device *dev);
304 
305 	int (*hbuf_free_slots)(struct mei_device *dev);
306 	bool (*hbuf_is_ready)(struct mei_device *dev);
307 	u32 (*hbuf_depth)(const struct mei_device *dev);
308 	int (*write)(struct mei_device *dev,
309 		     const void *hdr, size_t hdr_len,
310 		     const void *data, size_t data_len);
311 
312 	int (*rdbuf_full_slots)(struct mei_device *dev);
313 
314 	u32 (*read_hdr)(const struct mei_device *dev);
315 	int (*read)(struct mei_device *dev,
316 		     unsigned char *buf, unsigned long len);
317 };
318 
319 /* MEI bus API*/
320 void mei_cl_bus_rescan_work(struct work_struct *work);
321 void mei_cl_bus_dev_fixup(struct mei_cl_device *dev);
322 ssize_t __mei_cl_send(struct mei_cl *cl, u8 *buf, size_t length,
323 		      unsigned int mode);
324 ssize_t __mei_cl_recv(struct mei_cl *cl, u8 *buf, size_t length,
325 		      unsigned int mode, unsigned long timeout);
326 bool mei_cl_bus_rx_event(struct mei_cl *cl);
327 bool mei_cl_bus_notify_event(struct mei_cl *cl);
328 void mei_cl_bus_remove_devices(struct mei_device *bus);
329 int mei_cl_bus_init(void);
330 void mei_cl_bus_exit(void);
331 
332 /**
333  * enum mei_pg_event - power gating transition events
334  *
335  * @MEI_PG_EVENT_IDLE: the driver is not in power gating transition
336  * @MEI_PG_EVENT_WAIT: the driver is waiting for a pg event to complete
337  * @MEI_PG_EVENT_RECEIVED: the driver received pg event
338  * @MEI_PG_EVENT_INTR_WAIT: the driver is waiting for a pg event interrupt
339  * @MEI_PG_EVENT_INTR_RECEIVED: the driver received pg event interrupt
340  */
341 enum mei_pg_event {
342 	MEI_PG_EVENT_IDLE,
343 	MEI_PG_EVENT_WAIT,
344 	MEI_PG_EVENT_RECEIVED,
345 	MEI_PG_EVENT_INTR_WAIT,
346 	MEI_PG_EVENT_INTR_RECEIVED,
347 };
348 
349 /**
350  * enum mei_pg_state - device internal power gating state
351  *
352  * @MEI_PG_OFF: device is not power gated - it is active
353  * @MEI_PG_ON:  device is power gated - it is in lower power state
354  */
355 enum mei_pg_state {
356 	MEI_PG_OFF = 0,
357 	MEI_PG_ON =  1,
358 };
359 
360 const char *mei_pg_state_str(enum mei_pg_state state);
361 
362 /**
363  * struct mei_fw_version - MEI FW version struct
364  *
365  * @platform: platform identifier
366  * @major: major version field
367  * @minor: minor version field
368  * @buildno: build number version field
369  * @hotfix: hotfix number version field
370  */
371 struct mei_fw_version {
372 	u8 platform;
373 	u8 major;
374 	u16 minor;
375 	u16 buildno;
376 	u16 hotfix;
377 };
378 
379 #define MEI_MAX_FW_VER_BLOCKS 3
380 
381 /**
382  * struct mei_device -  MEI private device struct
383  *
384  * @dev         : device on a bus
385  * @cdev        : character device
386  * @minor       : minor number allocated for device
387  *
388  * @write_list  : write pending list
389  * @write_waiting_list : write completion list
390  * @ctrl_wr_list : pending control write list
391  * @ctrl_rd_list : pending control read list
392  * @tx_queue_limit: tx queues per client linit
393  *
394  * @file_list   : list of opened handles
395  * @open_handle_count: number of opened handles
396  *
397  * @device_lock : big device lock
398  * @timer_work  : MEI timer delayed work (timeouts)
399  *
400  * @recvd_hw_ready : hw ready message received flag
401  *
402  * @wait_hw_ready : wait queue for receive HW ready message form FW
403  * @wait_pg     : wait queue for receive PG message from FW
404  * @wait_hbm_start : wait queue for receive HBM start message from FW
405  *
406  * @reset_count : number of consecutive resets
407  * @dev_state   : device state
408  * @hbm_state   : state of host bus message protocol
409  * @init_clients_timer : HBM init handshake timeout
410  *
411  * @pg_event    : power gating event
412  * @pg_domain   : runtime PM domain
413  *
414  * @rd_msg_buf  : control messages buffer
415  * @rd_msg_hdr  : read message header storage
416  *
417  * @hbuf_is_ready : query if the host host/write buffer is ready
418  * @dr_dscr: DMA ring descriptors: TX, RX, and CTRL
419  *
420  * @version     : HBM protocol version in use
421  * @hbm_f_pg_supported  : hbm feature pgi protocol
422  * @hbm_f_dc_supported  : hbm feature dynamic clients
423  * @hbm_f_dot_supported : hbm feature disconnect on timeout
424  * @hbm_f_ev_supported  : hbm feature event notification
425  * @hbm_f_fa_supported  : hbm feature fixed address client
426  * @hbm_f_ie_supported  : hbm feature immediate reply to enum request
427  * @hbm_f_os_supported  : hbm feature support OS ver message
428  * @hbm_f_dr_supported  : hbm feature dma ring supported
429  *
430  * @fw_ver : FW versions
431  *
432  * @fw_f_fw_ver_supported : fw feature: fw version supported
433  *
434  * @me_clients_rwsem: rw lock over me_clients list
435  * @me_clients  : list of FW clients
436  * @me_clients_map : FW clients bit map
437  * @host_clients_map : host clients id pool
438  *
439  * @allow_fixed_address: allow user space to connect a fixed client
440  * @override_fixed_address: force allow fixed address behavior
441  *
442  * @reset_work  : work item for the device reset
443  * @bus_rescan_work : work item for the bus rescan
444  *
445  * @device_list : mei client bus list
446  * @cl_bus_lock : client bus list lock
447  *
448  * @dbgfs_dir   : debugfs mei root directory
449  *
450  * @ops:        : hw specific operations
451  * @hw          : hw specific data
452  */
453 struct mei_device {
454 	struct device *dev;
455 	struct cdev cdev;
456 	int minor;
457 
458 	struct list_head write_list;
459 	struct list_head write_waiting_list;
460 	struct list_head ctrl_wr_list;
461 	struct list_head ctrl_rd_list;
462 	u8 tx_queue_limit;
463 
464 	struct list_head file_list;
465 	long open_handle_count;
466 
467 	struct mutex device_lock;
468 	struct delayed_work timer_work;
469 
470 	bool recvd_hw_ready;
471 	/*
472 	 * waiting queue for receive message from FW
473 	 */
474 	wait_queue_head_t wait_hw_ready;
475 	wait_queue_head_t wait_pg;
476 	wait_queue_head_t wait_hbm_start;
477 
478 	/*
479 	 * mei device  states
480 	 */
481 	unsigned long reset_count;
482 	enum mei_dev_state dev_state;
483 	enum mei_hbm_state hbm_state;
484 	u16 init_clients_timer;
485 
486 	/*
487 	 * Power Gating support
488 	 */
489 	enum mei_pg_event pg_event;
490 #ifdef CONFIG_PM
491 	struct dev_pm_domain pg_domain;
492 #endif /* CONFIG_PM */
493 
494 	unsigned char rd_msg_buf[MEI_RD_MSG_BUF_SIZE];
495 	u32 rd_msg_hdr[MEI_MSG_HDR_MAX];
496 
497 	/* write buffer */
498 	bool hbuf_is_ready;
499 
500 	struct mei_dma_dscr dr_dscr[DMA_DSCR_NUM];
501 
502 	struct hbm_version version;
503 	unsigned int hbm_f_pg_supported:1;
504 	unsigned int hbm_f_dc_supported:1;
505 	unsigned int hbm_f_dot_supported:1;
506 	unsigned int hbm_f_ev_supported:1;
507 	unsigned int hbm_f_fa_supported:1;
508 	unsigned int hbm_f_ie_supported:1;
509 	unsigned int hbm_f_os_supported:1;
510 	unsigned int hbm_f_dr_supported:1;
511 
512 	struct mei_fw_version fw_ver[MEI_MAX_FW_VER_BLOCKS];
513 
514 	unsigned int fw_f_fw_ver_supported:1;
515 
516 	struct rw_semaphore me_clients_rwsem;
517 	struct list_head me_clients;
518 	DECLARE_BITMAP(me_clients_map, MEI_CLIENTS_MAX);
519 	DECLARE_BITMAP(host_clients_map, MEI_CLIENTS_MAX);
520 
521 	bool allow_fixed_address;
522 	bool override_fixed_address;
523 
524 	struct work_struct reset_work;
525 	struct work_struct bus_rescan_work;
526 
527 	/* List of bus devices */
528 	struct list_head device_list;
529 	struct mutex cl_bus_lock;
530 
531 #if IS_ENABLED(CONFIG_DEBUG_FS)
532 	struct dentry *dbgfs_dir;
533 #endif /* CONFIG_DEBUG_FS */
534 
535 	const struct mei_hw_ops *ops;
536 	char hw[] __aligned(sizeof(void *));
537 };
538 
539 static inline unsigned long mei_secs_to_jiffies(unsigned long sec)
540 {
541 	return msecs_to_jiffies(sec * MSEC_PER_SEC);
542 }
543 
544 /**
545  * mei_data2slots - get slots number from a message length
546  *
547  * @length: size of the messages in bytes
548  *
549  * Return: number of slots
550  */
551 static inline u32 mei_data2slots(size_t length)
552 {
553 	return DIV_ROUND_UP(length, MEI_SLOT_SIZE);
554 }
555 
556 /**
557  * mei_hbm2slots - get slots number from a hbm message length
558  *                 length + size of the mei message header
559  *
560  * @length: size of the messages in bytes
561  *
562  * Return: number of slots
563  */
564 static inline u32 mei_hbm2slots(size_t length)
565 {
566 	return DIV_ROUND_UP(sizeof(struct mei_msg_hdr) + length, MEI_SLOT_SIZE);
567 }
568 
569 /**
570  * mei_slots2data - get data in slots - bytes from slots
571  *
572  * @slots: number of available slots
573  *
574  * Return: number of bytes in slots
575  */
576 static inline u32 mei_slots2data(int slots)
577 {
578 	return slots * MEI_SLOT_SIZE;
579 }
580 
581 /*
582  * mei init function prototypes
583  */
584 void mei_device_init(struct mei_device *dev,
585 		     struct device *device,
586 		     const struct mei_hw_ops *hw_ops);
587 int mei_reset(struct mei_device *dev);
588 int mei_start(struct mei_device *dev);
589 int mei_restart(struct mei_device *dev);
590 void mei_stop(struct mei_device *dev);
591 void mei_cancel_work(struct mei_device *dev);
592 
593 void mei_set_devstate(struct mei_device *dev, enum mei_dev_state state);
594 
595 int mei_dmam_ring_alloc(struct mei_device *dev);
596 void mei_dmam_ring_free(struct mei_device *dev);
597 bool mei_dma_ring_is_allocated(struct mei_device *dev);
598 void mei_dma_ring_reset(struct mei_device *dev);
599 void mei_dma_ring_read(struct mei_device *dev, unsigned char *buf, u32 len);
600 void mei_dma_ring_write(struct mei_device *dev, unsigned char *buf, u32 len);
601 u32 mei_dma_ring_empty_slots(struct mei_device *dev);
602 
603 /*
604  *  MEI interrupt functions prototype
605  */
606 
607 void mei_timer(struct work_struct *work);
608 void mei_schedule_stall_timer(struct mei_device *dev);
609 int mei_irq_read_handler(struct mei_device *dev,
610 			 struct list_head *cmpl_list, s32 *slots);
611 
612 int mei_irq_write_handler(struct mei_device *dev, struct list_head *cmpl_list);
613 void mei_irq_compl_handler(struct mei_device *dev, struct list_head *cmpl_list);
614 
615 /*
616  * Register Access Function
617  */
618 
619 
620 static inline int mei_hw_config(struct mei_device *dev)
621 {
622 	return dev->ops->hw_config(dev);
623 }
624 
625 static inline enum mei_pg_state mei_pg_state(struct mei_device *dev)
626 {
627 	return dev->ops->pg_state(dev);
628 }
629 
630 static inline bool mei_pg_in_transition(struct mei_device *dev)
631 {
632 	return dev->ops->pg_in_transition(dev);
633 }
634 
635 static inline bool mei_pg_is_enabled(struct mei_device *dev)
636 {
637 	return dev->ops->pg_is_enabled(dev);
638 }
639 
640 static inline int mei_hw_reset(struct mei_device *dev, bool enable)
641 {
642 	return dev->ops->hw_reset(dev, enable);
643 }
644 
645 static inline int mei_hw_start(struct mei_device *dev)
646 {
647 	return dev->ops->hw_start(dev);
648 }
649 
650 static inline void mei_clear_interrupts(struct mei_device *dev)
651 {
652 	dev->ops->intr_clear(dev);
653 }
654 
655 static inline void mei_enable_interrupts(struct mei_device *dev)
656 {
657 	dev->ops->intr_enable(dev);
658 }
659 
660 static inline void mei_disable_interrupts(struct mei_device *dev)
661 {
662 	dev->ops->intr_disable(dev);
663 }
664 
665 static inline void mei_synchronize_irq(struct mei_device *dev)
666 {
667 	dev->ops->synchronize_irq(dev);
668 }
669 
670 static inline bool mei_host_is_ready(struct mei_device *dev)
671 {
672 	return dev->ops->host_is_ready(dev);
673 }
674 static inline bool mei_hw_is_ready(struct mei_device *dev)
675 {
676 	return dev->ops->hw_is_ready(dev);
677 }
678 
679 static inline bool mei_hbuf_is_ready(struct mei_device *dev)
680 {
681 	return dev->ops->hbuf_is_ready(dev);
682 }
683 
684 static inline int mei_hbuf_empty_slots(struct mei_device *dev)
685 {
686 	return dev->ops->hbuf_free_slots(dev);
687 }
688 
689 static inline u32 mei_hbuf_depth(const struct mei_device *dev)
690 {
691 	return dev->ops->hbuf_depth(dev);
692 }
693 
694 static inline int mei_write_message(struct mei_device *dev,
695 				    const void *hdr, size_t hdr_len,
696 				    const void *data, size_t data_len)
697 {
698 	return dev->ops->write(dev, hdr, hdr_len, data, data_len);
699 }
700 
701 static inline u32 mei_read_hdr(const struct mei_device *dev)
702 {
703 	return dev->ops->read_hdr(dev);
704 }
705 
706 static inline void mei_read_slots(struct mei_device *dev,
707 		     unsigned char *buf, unsigned long len)
708 {
709 	dev->ops->read(dev, buf, len);
710 }
711 
712 static inline int mei_count_full_read_slots(struct mei_device *dev)
713 {
714 	return dev->ops->rdbuf_full_slots(dev);
715 }
716 
717 static inline int mei_trc_status(struct mei_device *dev, u32 *trc)
718 {
719 	if (dev->ops->trc_status)
720 		return dev->ops->trc_status(dev, trc);
721 	return -EOPNOTSUPP;
722 }
723 
724 static inline int mei_fw_status(struct mei_device *dev,
725 				struct mei_fw_status *fw_status)
726 {
727 	return dev->ops->fw_status(dev, fw_status);
728 }
729 
730 bool mei_hbuf_acquire(struct mei_device *dev);
731 
732 bool mei_write_is_idle(struct mei_device *dev);
733 
734 #if IS_ENABLED(CONFIG_DEBUG_FS)
735 void mei_dbgfs_register(struct mei_device *dev, const char *name);
736 void mei_dbgfs_deregister(struct mei_device *dev);
737 #else
738 static inline void mei_dbgfs_register(struct mei_device *dev, const char *name) {}
739 static inline void mei_dbgfs_deregister(struct mei_device *dev) {}
740 #endif /* CONFIG_DEBUG_FS */
741 
742 int mei_register(struct mei_device *dev, struct device *parent);
743 void mei_deregister(struct mei_device *dev);
744 
745 #define MEI_HDR_FMT "hdr:host=%02d me=%02d len=%d dma=%1d internal=%1d comp=%1d"
746 #define MEI_HDR_PRM(hdr)                  \
747 	(hdr)->host_addr, (hdr)->me_addr, \
748 	(hdr)->length, (hdr)->dma_ring, (hdr)->internal, (hdr)->msg_complete
749 
750 ssize_t mei_fw_status2str(struct mei_fw_status *fw_sts, char *buf, size_t len);
751 /**
752  * mei_fw_status_str - fetch and convert fw status registers to printable string
753  *
754  * @dev: the device structure
755  * @buf: string buffer at minimal size MEI_FW_STATUS_STR_SZ
756  * @len: buffer len must be >= MEI_FW_STATUS_STR_SZ
757  *
758  * Return: number of bytes written or < 0 on failure
759  */
760 static inline ssize_t mei_fw_status_str(struct mei_device *dev,
761 					char *buf, size_t len)
762 {
763 	struct mei_fw_status fw_status;
764 	int ret;
765 
766 	buf[0] = '\0';
767 
768 	ret = mei_fw_status(dev, &fw_status);
769 	if (ret)
770 		return ret;
771 
772 	ret = mei_fw_status2str(&fw_status, buf, MEI_FW_STATUS_STR_SZ);
773 
774 	return ret;
775 }
776 
777 
778 #endif
779