xref: /openbmc/linux/drivers/misc/mei/mei_dev.h (revision 50371be6)
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_recv(struct mei_cl *cl, u8 *buf, size_t length, u8 *vtag,
383 		      unsigned int mode, unsigned long timeout);
384 bool mei_cl_bus_rx_event(struct mei_cl *cl);
385 bool mei_cl_bus_notify_event(struct mei_cl *cl);
386 void mei_cl_bus_remove_devices(struct mei_device *bus);
387 int mei_cl_bus_init(void);
388 void mei_cl_bus_exit(void);
389 
390 /**
391  * enum mei_pg_event - power gating transition events
392  *
393  * @MEI_PG_EVENT_IDLE: the driver is not in power gating transition
394  * @MEI_PG_EVENT_WAIT: the driver is waiting for a pg event to complete
395  * @MEI_PG_EVENT_RECEIVED: the driver received pg event
396  * @MEI_PG_EVENT_INTR_WAIT: the driver is waiting for a pg event interrupt
397  * @MEI_PG_EVENT_INTR_RECEIVED: the driver received pg event interrupt
398  */
399 enum mei_pg_event {
400 	MEI_PG_EVENT_IDLE,
401 	MEI_PG_EVENT_WAIT,
402 	MEI_PG_EVENT_RECEIVED,
403 	MEI_PG_EVENT_INTR_WAIT,
404 	MEI_PG_EVENT_INTR_RECEIVED,
405 };
406 
407 /**
408  * enum mei_pg_state - device internal power gating state
409  *
410  * @MEI_PG_OFF: device is not power gated - it is active
411  * @MEI_PG_ON:  device is power gated - it is in lower power state
412  */
413 enum mei_pg_state {
414 	MEI_PG_OFF = 0,
415 	MEI_PG_ON =  1,
416 };
417 
418 const char *mei_pg_state_str(enum mei_pg_state state);
419 
420 /**
421  * struct mei_fw_version - MEI FW version struct
422  *
423  * @platform: platform identifier
424  * @major: major version field
425  * @minor: minor version field
426  * @buildno: build number version field
427  * @hotfix: hotfix number version field
428  */
429 struct mei_fw_version {
430 	u8 platform;
431 	u8 major;
432 	u16 minor;
433 	u16 buildno;
434 	u16 hotfix;
435 };
436 
437 #define MEI_MAX_FW_VER_BLOCKS 3
438 
439 struct mei_dev_timeouts {
440 	unsigned long hw_ready; /* Timeout on ready message, in jiffies */
441 	int connect; /* HPS: at least 2 seconds, in seconds */
442 	unsigned long cl_connect; /* HPS: Client Connect Timeout, in jiffies */
443 	int client_init; /* HPS: Clients Enumeration Timeout, in seconds */
444 	unsigned long pgi; /* PG Isolation time response, in jiffies */
445 	unsigned int d0i3; /* D0i3 set/unset max response time, in jiffies */
446 	unsigned long hbm; /* HBM operation timeout, in jiffies */
447 	unsigned long mkhi_recv; /* receive timeout, in jiffies */
448 };
449 
450 /**
451  * struct mei_device -  MEI private device struct
452  *
453  * @dev         : device on a bus
454  * @cdev        : character device
455  * @minor       : minor number allocated for device
456  *
457  * @write_list  : write pending list
458  * @write_waiting_list : write completion list
459  * @ctrl_wr_list : pending control write list
460  * @ctrl_rd_list : pending control read list
461  * @tx_queue_limit: tx queues per client linit
462  *
463  * @file_list   : list of opened handles
464  * @open_handle_count: number of opened handles
465  *
466  * @device_lock : big device lock
467  * @timer_work  : MEI timer delayed work (timeouts)
468  *
469  * @recvd_hw_ready : hw ready message received flag
470  *
471  * @wait_hw_ready : wait queue for receive HW ready message form FW
472  * @wait_pg     : wait queue for receive PG message from FW
473  * @wait_hbm_start : wait queue for receive HBM start message from FW
474  *
475  * @reset_count : number of consecutive resets
476  * @dev_state   : device state
477  * @hbm_state   : state of host bus message protocol
478  * @pxp_mode    : PXP device mode
479  * @init_clients_timer : HBM init handshake timeout
480  *
481  * @pg_event    : power gating event
482  * @pg_domain   : runtime PM domain
483  *
484  * @rd_msg_buf  : control messages buffer
485  * @rd_msg_hdr  : read message header storage
486  * @rd_msg_hdr_count : how many dwords were already read from header
487  *
488  * @hbuf_is_ready : query if the host host/write buffer is ready
489  * @dr_dscr: DMA ring descriptors: TX, RX, and CTRL
490  *
491  * @version     : HBM protocol version in use
492  * @hbm_f_pg_supported  : hbm feature pgi protocol
493  * @hbm_f_dc_supported  : hbm feature dynamic clients
494  * @hbm_f_dot_supported : hbm feature disconnect on timeout
495  * @hbm_f_ev_supported  : hbm feature event notification
496  * @hbm_f_fa_supported  : hbm feature fixed address client
497  * @hbm_f_ie_supported  : hbm feature immediate reply to enum request
498  * @hbm_f_os_supported  : hbm feature support OS ver message
499  * @hbm_f_dr_supported  : hbm feature dma ring supported
500  * @hbm_f_vt_supported  : hbm feature vtag supported
501  * @hbm_f_cap_supported : hbm feature capabilities message supported
502  * @hbm_f_cd_supported  : hbm feature client dma supported
503  * @hbm_f_gsc_supported : hbm feature gsc supported
504  *
505  * @fw_ver : FW versions
506  *
507  * @fw_f_fw_ver_supported : fw feature: fw version supported
508  *
509  * @me_clients_rwsem: rw lock over me_clients list
510  * @me_clients  : list of FW clients
511  * @me_clients_map : FW clients bit map
512  * @host_clients_map : host clients id pool
513  *
514  * @allow_fixed_address: allow user space to connect a fixed client
515  * @override_fixed_address: force allow fixed address behavior
516  *
517  * @timeouts: actual timeout values
518  *
519  * @reset_work  : work item for the device reset
520  * @bus_rescan_work : work item for the bus rescan
521  *
522  * @device_list : mei client bus list
523  * @cl_bus_lock : client bus list lock
524  *
525  * @kind        : kind of mei device
526  *
527  * @dbgfs_dir   : debugfs mei root directory
528  *
529  * @ops:        : hw specific operations
530  * @hw          : hw specific data
531  */
532 struct mei_device {
533 	struct device *dev;
534 	struct cdev cdev;
535 	int minor;
536 
537 	struct list_head write_list;
538 	struct list_head write_waiting_list;
539 	struct list_head ctrl_wr_list;
540 	struct list_head ctrl_rd_list;
541 	u8 tx_queue_limit;
542 
543 	struct list_head file_list;
544 	long open_handle_count;
545 
546 	struct mutex device_lock;
547 	struct delayed_work timer_work;
548 
549 	bool recvd_hw_ready;
550 	/*
551 	 * waiting queue for receive message from FW
552 	 */
553 	wait_queue_head_t wait_hw_ready;
554 	wait_queue_head_t wait_pg;
555 	wait_queue_head_t wait_hbm_start;
556 
557 	/*
558 	 * mei device  states
559 	 */
560 	unsigned long reset_count;
561 	enum mei_dev_state dev_state;
562 	enum mei_hbm_state hbm_state;
563 	enum mei_dev_pxp_mode pxp_mode;
564 	u16 init_clients_timer;
565 
566 	/*
567 	 * Power Gating support
568 	 */
569 	enum mei_pg_event pg_event;
570 #ifdef CONFIG_PM
571 	struct dev_pm_domain pg_domain;
572 #endif /* CONFIG_PM */
573 
574 	unsigned char rd_msg_buf[MEI_RD_MSG_BUF_SIZE];
575 	u32 rd_msg_hdr[MEI_RD_MSG_BUF_SIZE];
576 	int rd_msg_hdr_count;
577 
578 	/* write buffer */
579 	bool hbuf_is_ready;
580 
581 	struct mei_dma_dscr dr_dscr[DMA_DSCR_NUM];
582 
583 	struct hbm_version version;
584 	unsigned int hbm_f_pg_supported:1;
585 	unsigned int hbm_f_dc_supported:1;
586 	unsigned int hbm_f_dot_supported:1;
587 	unsigned int hbm_f_ev_supported:1;
588 	unsigned int hbm_f_fa_supported:1;
589 	unsigned int hbm_f_ie_supported:1;
590 	unsigned int hbm_f_os_supported:1;
591 	unsigned int hbm_f_dr_supported:1;
592 	unsigned int hbm_f_vt_supported:1;
593 	unsigned int hbm_f_cap_supported:1;
594 	unsigned int hbm_f_cd_supported:1;
595 	unsigned int hbm_f_gsc_supported:1;
596 
597 	struct mei_fw_version fw_ver[MEI_MAX_FW_VER_BLOCKS];
598 
599 	unsigned int fw_f_fw_ver_supported:1;
600 
601 	struct rw_semaphore me_clients_rwsem;
602 	struct list_head me_clients;
603 	DECLARE_BITMAP(me_clients_map, MEI_CLIENTS_MAX);
604 	DECLARE_BITMAP(host_clients_map, MEI_CLIENTS_MAX);
605 
606 	bool allow_fixed_address;
607 	bool override_fixed_address;
608 
609 	struct mei_dev_timeouts timeouts;
610 
611 	struct work_struct reset_work;
612 	struct work_struct bus_rescan_work;
613 
614 	/* List of bus devices */
615 	struct list_head device_list;
616 	struct mutex cl_bus_lock;
617 
618 	const char *kind;
619 
620 #if IS_ENABLED(CONFIG_DEBUG_FS)
621 	struct dentry *dbgfs_dir;
622 #endif /* CONFIG_DEBUG_FS */
623 
624 	const struct mei_hw_ops *ops;
625 	char hw[] __aligned(sizeof(void *));
626 };
627 
628 static inline unsigned long mei_secs_to_jiffies(unsigned long sec)
629 {
630 	return msecs_to_jiffies(sec * MSEC_PER_SEC);
631 }
632 
633 /**
634  * mei_data2slots - get slots number from a message length
635  *
636  * @length: size of the messages in bytes
637  *
638  * Return: number of slots
639  */
640 static inline u32 mei_data2slots(size_t length)
641 {
642 	return DIV_ROUND_UP(length, MEI_SLOT_SIZE);
643 }
644 
645 /**
646  * mei_hbm2slots - get slots number from a hbm message length
647  *                 length + size of the mei message header
648  *
649  * @length: size of the messages in bytes
650  *
651  * Return: number of slots
652  */
653 static inline u32 mei_hbm2slots(size_t length)
654 {
655 	return DIV_ROUND_UP(sizeof(struct mei_msg_hdr) + length, MEI_SLOT_SIZE);
656 }
657 
658 /**
659  * mei_slots2data - get data in slots - bytes from slots
660  *
661  * @slots: number of available slots
662  *
663  * Return: number of bytes in slots
664  */
665 static inline u32 mei_slots2data(int slots)
666 {
667 	return slots * MEI_SLOT_SIZE;
668 }
669 
670 /*
671  * mei init function prototypes
672  */
673 void mei_device_init(struct mei_device *dev,
674 		     struct device *device,
675 		     bool slow_fw,
676 		     const struct mei_hw_ops *hw_ops);
677 int mei_reset(struct mei_device *dev);
678 int mei_start(struct mei_device *dev);
679 int mei_restart(struct mei_device *dev);
680 void mei_stop(struct mei_device *dev);
681 void mei_cancel_work(struct mei_device *dev);
682 
683 void mei_set_devstate(struct mei_device *dev, enum mei_dev_state state);
684 
685 int mei_dmam_ring_alloc(struct mei_device *dev);
686 void mei_dmam_ring_free(struct mei_device *dev);
687 bool mei_dma_ring_is_allocated(struct mei_device *dev);
688 void mei_dma_ring_reset(struct mei_device *dev);
689 void mei_dma_ring_read(struct mei_device *dev, unsigned char *buf, u32 len);
690 void mei_dma_ring_write(struct mei_device *dev, unsigned char *buf, u32 len);
691 u32 mei_dma_ring_empty_slots(struct mei_device *dev);
692 
693 /*
694  *  MEI interrupt functions prototype
695  */
696 
697 void mei_timer(struct work_struct *work);
698 void mei_schedule_stall_timer(struct mei_device *dev);
699 int mei_irq_read_handler(struct mei_device *dev,
700 			 struct list_head *cmpl_list, s32 *slots);
701 
702 int mei_irq_write_handler(struct mei_device *dev, struct list_head *cmpl_list);
703 void mei_irq_compl_handler(struct mei_device *dev, struct list_head *cmpl_list);
704 
705 /*
706  * Register Access Function
707  */
708 
709 
710 static inline int mei_hw_config(struct mei_device *dev)
711 {
712 	return dev->ops->hw_config(dev);
713 }
714 
715 static inline enum mei_pg_state mei_pg_state(struct mei_device *dev)
716 {
717 	return dev->ops->pg_state(dev);
718 }
719 
720 static inline bool mei_pg_in_transition(struct mei_device *dev)
721 {
722 	return dev->ops->pg_in_transition(dev);
723 }
724 
725 static inline bool mei_pg_is_enabled(struct mei_device *dev)
726 {
727 	return dev->ops->pg_is_enabled(dev);
728 }
729 
730 static inline int mei_hw_reset(struct mei_device *dev, bool enable)
731 {
732 	return dev->ops->hw_reset(dev, enable);
733 }
734 
735 static inline int mei_hw_start(struct mei_device *dev)
736 {
737 	return dev->ops->hw_start(dev);
738 }
739 
740 static inline void mei_clear_interrupts(struct mei_device *dev)
741 {
742 	dev->ops->intr_clear(dev);
743 }
744 
745 static inline void mei_enable_interrupts(struct mei_device *dev)
746 {
747 	dev->ops->intr_enable(dev);
748 }
749 
750 static inline void mei_disable_interrupts(struct mei_device *dev)
751 {
752 	dev->ops->intr_disable(dev);
753 }
754 
755 static inline void mei_synchronize_irq(struct mei_device *dev)
756 {
757 	dev->ops->synchronize_irq(dev);
758 }
759 
760 static inline bool mei_host_is_ready(struct mei_device *dev)
761 {
762 	return dev->ops->host_is_ready(dev);
763 }
764 static inline bool mei_hw_is_ready(struct mei_device *dev)
765 {
766 	return dev->ops->hw_is_ready(dev);
767 }
768 
769 static inline bool mei_hbuf_is_ready(struct mei_device *dev)
770 {
771 	return dev->ops->hbuf_is_ready(dev);
772 }
773 
774 static inline int mei_hbuf_empty_slots(struct mei_device *dev)
775 {
776 	return dev->ops->hbuf_free_slots(dev);
777 }
778 
779 static inline u32 mei_hbuf_depth(const struct mei_device *dev)
780 {
781 	return dev->ops->hbuf_depth(dev);
782 }
783 
784 static inline int mei_write_message(struct mei_device *dev,
785 				    const void *hdr, size_t hdr_len,
786 				    const void *data, size_t data_len)
787 {
788 	return dev->ops->write(dev, hdr, hdr_len, data, data_len);
789 }
790 
791 static inline u32 mei_read_hdr(const struct mei_device *dev)
792 {
793 	return dev->ops->read_hdr(dev);
794 }
795 
796 static inline void mei_read_slots(struct mei_device *dev,
797 		     unsigned char *buf, unsigned long len)
798 {
799 	dev->ops->read(dev, buf, len);
800 }
801 
802 static inline int mei_count_full_read_slots(struct mei_device *dev)
803 {
804 	return dev->ops->rdbuf_full_slots(dev);
805 }
806 
807 static inline int mei_trc_status(struct mei_device *dev, u32 *trc)
808 {
809 	if (dev->ops->trc_status)
810 		return dev->ops->trc_status(dev, trc);
811 	return -EOPNOTSUPP;
812 }
813 
814 static inline int mei_fw_status(struct mei_device *dev,
815 				struct mei_fw_status *fw_status)
816 {
817 	return dev->ops->fw_status(dev, fw_status);
818 }
819 
820 bool mei_hbuf_acquire(struct mei_device *dev);
821 
822 bool mei_write_is_idle(struct mei_device *dev);
823 
824 #if IS_ENABLED(CONFIG_DEBUG_FS)
825 void mei_dbgfs_register(struct mei_device *dev, const char *name);
826 void mei_dbgfs_deregister(struct mei_device *dev);
827 #else
828 static inline void mei_dbgfs_register(struct mei_device *dev, const char *name) {}
829 static inline void mei_dbgfs_deregister(struct mei_device *dev) {}
830 #endif /* CONFIG_DEBUG_FS */
831 
832 int mei_register(struct mei_device *dev, struct device *parent);
833 void mei_deregister(struct mei_device *dev);
834 
835 #define MEI_HDR_FMT "hdr:host=%02d me=%02d len=%d dma=%1d ext=%1d internal=%1d comp=%1d"
836 #define MEI_HDR_PRM(hdr)                  \
837 	(hdr)->host_addr, (hdr)->me_addr, \
838 	(hdr)->length, (hdr)->dma_ring, (hdr)->extended, \
839 	(hdr)->internal, (hdr)->msg_complete
840 
841 ssize_t mei_fw_status2str(struct mei_fw_status *fw_sts, char *buf, size_t len);
842 /**
843  * mei_fw_status_str - fetch and convert fw status registers to printable string
844  *
845  * @dev: the device structure
846  * @buf: string buffer at minimal size MEI_FW_STATUS_STR_SZ
847  * @len: buffer len must be >= MEI_FW_STATUS_STR_SZ
848  *
849  * Return: number of bytes written or < 0 on failure
850  */
851 static inline ssize_t mei_fw_status_str(struct mei_device *dev,
852 					char *buf, size_t len)
853 {
854 	struct mei_fw_status fw_status;
855 	int ret;
856 
857 	buf[0] = '\0';
858 
859 	ret = mei_fw_status(dev, &fw_status);
860 	if (ret)
861 		return ret;
862 
863 	ret = mei_fw_status2str(&fw_status, buf, MEI_FW_STATUS_STR_SZ);
864 
865 	return ret;
866 }
867 
868 
869 #endif
870