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