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