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