1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#ifndef _MEI_DEV_H_
18#define _MEI_DEV_H_
19
20#include <linux/types.h>
21#include <linux/cdev.h>
22#include <linux/poll.h>
23#include <linux/mei.h>
24#include <linux/mei_cl_bus.h>
25
26#include "hw.h"
27#include "hbm.h"
28
29#define MEI_SLOT_SIZE sizeof(u32)
30#define MEI_RD_MSG_BUF_SIZE (128 * MEI_SLOT_SIZE)
31
32
33
34
35#define MEI_CLIENTS_MAX 256
36
37
38
39
40#define MEI_MAX_CONSEC_RESET 3
41
42
43
44
45
46
47
48
49#define MEI_MAX_OPEN_HANDLE_COUNT (MEI_CLIENTS_MAX - 1)
50
51
52enum file_state {
53 MEI_FILE_UNINITIALIZED = 0,
54 MEI_FILE_INITIALIZING,
55 MEI_FILE_CONNECTING,
56 MEI_FILE_CONNECTED,
57 MEI_FILE_DISCONNECTING,
58 MEI_FILE_DISCONNECT_REPLY,
59 MEI_FILE_DISCONNECT_REQUIRED,
60 MEI_FILE_DISCONNECTED,
61};
62
63
64enum mei_dev_state {
65 MEI_DEV_INITIALIZING = 0,
66 MEI_DEV_INIT_CLIENTS,
67 MEI_DEV_ENABLED,
68 MEI_DEV_RESETTING,
69 MEI_DEV_DISABLED,
70 MEI_DEV_POWER_DOWN,
71 MEI_DEV_POWER_UP
72};
73
74const char *mei_dev_state_str(int state);
75
76enum mei_file_transaction_states {
77 MEI_IDLE,
78 MEI_WRITING,
79 MEI_WRITE_COMPLETE,
80};
81
82
83
84
85
86
87
88
89
90
91
92enum mei_cb_file_ops {
93 MEI_FOP_READ = 0,
94 MEI_FOP_WRITE,
95 MEI_FOP_CONNECT,
96 MEI_FOP_DISCONNECT,
97 MEI_FOP_DISCONNECT_RSP,
98 MEI_FOP_NOTIFY_START,
99 MEI_FOP_NOTIFY_STOP,
100};
101
102
103
104
105
106
107
108
109
110enum mei_cl_io_mode {
111 MEI_CL_IO_TX_BLOCKING = BIT(0),
112 MEI_CL_IO_TX_INTERNAL = BIT(1),
113
114 MEI_CL_IO_RX_NONBLOCK = BIT(2),
115};
116
117
118
119
120struct mei_msg_data {
121 size_t size;
122 unsigned char *data;
123};
124
125
126
127
128
129
130
131
132struct mei_dma_dscr {
133 void *vaddr;
134 dma_addr_t daddr;
135 size_t size;
136};
137
138
139#define MEI_FW_STATUS_MAX 6
140
141#define MEI_FW_STATUS_STR_SZ (MEI_FW_STATUS_MAX * (8 + 1))
142
143
144
145
146
147
148
149
150struct mei_fw_status {
151 int count;
152 u32 status[MEI_FW_STATUS_MAX];
153};
154
155
156
157
158
159
160
161
162
163
164
165
166struct mei_me_client {
167 struct list_head list;
168 struct kref refcnt;
169 struct mei_client_properties props;
170 u8 client_id;
171 u8 tx_flow_ctrl_creds;
172 u8 connect_count;
173 u8 bus_added;
174};
175
176
177struct mei_cl;
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192struct mei_cl_cb {
193 struct list_head list;
194 struct mei_cl *cl;
195 enum mei_cb_file_ops fop_type;
196 struct mei_msg_data buf;
197 size_t buf_idx;
198 const struct file *fp;
199 int status;
200 u32 internal:1;
201 u32 blocking:1;
202};
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232struct mei_cl {
233 struct list_head link;
234 struct mei_device *dev;
235 enum file_state state;
236 wait_queue_head_t tx_wait;
237 wait_queue_head_t rx_wait;
238 wait_queue_head_t wait;
239 wait_queue_head_t ev_wait;
240 struct fasync_struct *ev_async;
241 int status;
242 struct mei_me_client *me_cl;
243 const struct file *fp;
244 u8 host_client_id;
245 u8 tx_flow_ctrl_creds;
246 u8 rx_flow_ctrl_creds;
247 u8 timer_count;
248 u8 notify_en;
249 u8 notify_ev;
250 u8 tx_cb_queued;
251 enum mei_file_transaction_states writing_state;
252 struct list_head rd_pending;
253 struct list_head rd_completed;
254
255 struct mei_cl_device *cldev;
256};
257
258#define MEI_TX_QUEUE_LIMIT_DEFAULT 50
259#define MEI_TX_QUEUE_LIMIT_MAX 255
260#define MEI_TX_QUEUE_LIMIT_MIN 30
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293struct mei_hw_ops {
294
295 bool (*host_is_ready)(struct mei_device *dev);
296
297 bool (*hw_is_ready)(struct mei_device *dev);
298 int (*hw_reset)(struct mei_device *dev, bool enable);
299 int (*hw_start)(struct mei_device *dev);
300 void (*hw_config)(struct mei_device *dev);
301
302 int (*fw_status)(struct mei_device *dev, struct mei_fw_status *fw_sts);
303 enum mei_pg_state (*pg_state)(struct mei_device *dev);
304 bool (*pg_in_transition)(struct mei_device *dev);
305 bool (*pg_is_enabled)(struct mei_device *dev);
306
307 void (*intr_clear)(struct mei_device *dev);
308 void (*intr_enable)(struct mei_device *dev);
309 void (*intr_disable)(struct mei_device *dev);
310 void (*synchronize_irq)(struct mei_device *dev);
311
312 int (*hbuf_free_slots)(struct mei_device *dev);
313 bool (*hbuf_is_ready)(struct mei_device *dev);
314 u32 (*hbuf_depth)(const struct mei_device *dev);
315 int (*write)(struct mei_device *dev,
316 const void *hdr, size_t hdr_len,
317 const void *data, size_t data_len);
318
319 int (*rdbuf_full_slots)(struct mei_device *dev);
320
321 u32 (*read_hdr)(const struct mei_device *dev);
322 int (*read)(struct mei_device *dev,
323 unsigned char *buf, unsigned long len);
324};
325
326
327void mei_cl_bus_rescan_work(struct work_struct *work);
328void mei_cl_bus_dev_fixup(struct mei_cl_device *dev);
329ssize_t __mei_cl_send(struct mei_cl *cl, u8 *buf, size_t length,
330 unsigned int mode);
331ssize_t __mei_cl_recv(struct mei_cl *cl, u8 *buf, size_t length,
332 unsigned int mode, unsigned long timeout);
333bool mei_cl_bus_rx_event(struct mei_cl *cl);
334bool mei_cl_bus_notify_event(struct mei_cl *cl);
335void mei_cl_bus_remove_devices(struct mei_device *bus);
336int mei_cl_bus_init(void);
337void mei_cl_bus_exit(void);
338
339
340
341
342
343
344
345
346
347
348enum mei_pg_event {
349 MEI_PG_EVENT_IDLE,
350 MEI_PG_EVENT_WAIT,
351 MEI_PG_EVENT_RECEIVED,
352 MEI_PG_EVENT_INTR_WAIT,
353 MEI_PG_EVENT_INTR_RECEIVED,
354};
355
356
357
358
359
360
361
362enum mei_pg_state {
363 MEI_PG_OFF = 0,
364 MEI_PG_ON = 1,
365};
366
367const char *mei_pg_state_str(enum mei_pg_state state);
368
369
370
371
372
373
374
375
376
377
378struct mei_fw_version {
379 u8 platform;
380 u8 major;
381 u16 minor;
382 u16 buildno;
383 u16 hotfix;
384};
385
386#define MEI_MAX_FW_VER_BLOCKS 3
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458struct mei_device {
459 struct device *dev;
460 struct cdev cdev;
461 int minor;
462
463 struct list_head write_list;
464 struct list_head write_waiting_list;
465 struct list_head ctrl_wr_list;
466 struct list_head ctrl_rd_list;
467 u8 tx_queue_limit;
468
469 struct list_head file_list;
470 long open_handle_count;
471
472 struct mutex device_lock;
473 struct delayed_work timer_work;
474
475 bool recvd_hw_ready;
476
477
478
479 wait_queue_head_t wait_hw_ready;
480 wait_queue_head_t wait_pg;
481 wait_queue_head_t wait_hbm_start;
482
483
484
485
486 unsigned long reset_count;
487 enum mei_dev_state dev_state;
488 enum mei_hbm_state hbm_state;
489 u16 init_clients_timer;
490
491
492
493
494 enum mei_pg_event pg_event;
495#ifdef CONFIG_PM
496 struct dev_pm_domain pg_domain;
497#endif
498
499 unsigned char rd_msg_buf[MEI_RD_MSG_BUF_SIZE];
500 u32 rd_msg_hdr[MEI_MSG_HDR_MAX];
501
502
503 bool hbuf_is_ready;
504
505 struct mei_dma_dscr dr_dscr[DMA_DSCR_NUM];
506
507 struct hbm_version version;
508 unsigned int hbm_f_pg_supported:1;
509 unsigned int hbm_f_dc_supported:1;
510 unsigned int hbm_f_dot_supported:1;
511 unsigned int hbm_f_ev_supported:1;
512 unsigned int hbm_f_fa_supported:1;
513 unsigned int hbm_f_ie_supported:1;
514 unsigned int hbm_f_os_supported:1;
515 unsigned int hbm_f_dr_supported:1;
516
517 struct mei_fw_version fw_ver[MEI_MAX_FW_VER_BLOCKS];
518
519 struct rw_semaphore me_clients_rwsem;
520 struct list_head me_clients;
521 DECLARE_BITMAP(me_clients_map, MEI_CLIENTS_MAX);
522 DECLARE_BITMAP(host_clients_map, MEI_CLIENTS_MAX);
523
524 bool allow_fixed_address;
525 bool override_fixed_address;
526
527 struct work_struct reset_work;
528 struct work_struct bus_rescan_work;
529
530
531 struct list_head device_list;
532 struct mutex cl_bus_lock;
533
534#if IS_ENABLED(CONFIG_DEBUG_FS)
535 struct dentry *dbgfs_dir;
536#endif
537
538
539 const struct mei_hw_ops *ops;
540 char hw[0] __aligned(sizeof(void *));
541};
542
543static inline unsigned long mei_secs_to_jiffies(unsigned long sec)
544{
545 return msecs_to_jiffies(sec * MSEC_PER_SEC);
546}
547
548
549
550
551
552
553
554
555static inline u32 mei_data2slots(size_t length)
556{
557 return DIV_ROUND_UP(length, MEI_SLOT_SIZE);
558}
559
560
561
562
563
564
565
566
567
568static inline u32 mei_hbm2slots(size_t length)
569{
570 return DIV_ROUND_UP(sizeof(struct mei_msg_hdr) + length, MEI_SLOT_SIZE);
571}
572
573
574
575
576
577
578
579
580static inline u32 mei_slots2data(int slots)
581{
582 return slots * MEI_SLOT_SIZE;
583}
584
585
586
587
588void mei_device_init(struct mei_device *dev,
589 struct device *device,
590 const struct mei_hw_ops *hw_ops);
591int mei_reset(struct mei_device *dev);
592int mei_start(struct mei_device *dev);
593int mei_restart(struct mei_device *dev);
594void mei_stop(struct mei_device *dev);
595void mei_cancel_work(struct mei_device *dev);
596
597int mei_dmam_ring_alloc(struct mei_device *dev);
598void mei_dmam_ring_free(struct mei_device *dev);
599bool mei_dma_ring_is_allocated(struct mei_device *dev);
600void mei_dma_ring_reset(struct mei_device *dev);
601void mei_dma_ring_read(struct mei_device *dev, unsigned char *buf, u32 len);
602void mei_dma_ring_write(struct mei_device *dev, unsigned char *buf, u32 len);
603u32 mei_dma_ring_empty_slots(struct mei_device *dev);
604
605
606
607
608
609void mei_timer(struct work_struct *work);
610void mei_schedule_stall_timer(struct mei_device *dev);
611int mei_irq_read_handler(struct mei_device *dev,
612 struct list_head *cmpl_list, s32 *slots);
613
614int mei_irq_write_handler(struct mei_device *dev, struct list_head *cmpl_list);
615void mei_irq_compl_handler(struct mei_device *dev, struct list_head *cmpl_list);
616
617
618
619
620
621
622static inline void mei_hw_config(struct mei_device *dev)
623{
624 dev->ops->hw_config(dev);
625}
626
627static inline enum mei_pg_state mei_pg_state(struct mei_device *dev)
628{
629 return dev->ops->pg_state(dev);
630}
631
632static inline bool mei_pg_in_transition(struct mei_device *dev)
633{
634 return dev->ops->pg_in_transition(dev);
635}
636
637static inline bool mei_pg_is_enabled(struct mei_device *dev)
638{
639 return dev->ops->pg_is_enabled(dev);
640}
641
642static inline int mei_hw_reset(struct mei_device *dev, bool enable)
643{
644 return dev->ops->hw_reset(dev, enable);
645}
646
647static inline int mei_hw_start(struct mei_device *dev)
648{
649 return dev->ops->hw_start(dev);
650}
651
652static inline void mei_clear_interrupts(struct mei_device *dev)
653{
654 dev->ops->intr_clear(dev);
655}
656
657static inline void mei_enable_interrupts(struct mei_device *dev)
658{
659 dev->ops->intr_enable(dev);
660}
661
662static inline void mei_disable_interrupts(struct mei_device *dev)
663{
664 dev->ops->intr_disable(dev);
665}
666
667static inline void mei_synchronize_irq(struct mei_device *dev)
668{
669 dev->ops->synchronize_irq(dev);
670}
671
672static inline bool mei_host_is_ready(struct mei_device *dev)
673{
674 return dev->ops->host_is_ready(dev);
675}
676static inline bool mei_hw_is_ready(struct mei_device *dev)
677{
678 return dev->ops->hw_is_ready(dev);
679}
680
681static inline bool mei_hbuf_is_ready(struct mei_device *dev)
682{
683 return dev->ops->hbuf_is_ready(dev);
684}
685
686static inline int mei_hbuf_empty_slots(struct mei_device *dev)
687{
688 return dev->ops->hbuf_free_slots(dev);
689}
690
691static inline u32 mei_hbuf_depth(const struct mei_device *dev)
692{
693 return dev->ops->hbuf_depth(dev);
694}
695
696static inline int mei_write_message(struct mei_device *dev,
697 const void *hdr, size_t hdr_len,
698 const void *data, size_t data_len)
699{
700 return dev->ops->write(dev, hdr, hdr_len, data, data_len);
701}
702
703static inline u32 mei_read_hdr(const struct mei_device *dev)
704{
705 return dev->ops->read_hdr(dev);
706}
707
708static inline void mei_read_slots(struct mei_device *dev,
709 unsigned char *buf, unsigned long len)
710{
711 dev->ops->read(dev, buf, len);
712}
713
714static inline int mei_count_full_read_slots(struct mei_device *dev)
715{
716 return dev->ops->rdbuf_full_slots(dev);
717}
718
719static inline int mei_fw_status(struct mei_device *dev,
720 struct mei_fw_status *fw_status)
721{
722 return dev->ops->fw_status(dev, fw_status);
723}
724
725bool mei_hbuf_acquire(struct mei_device *dev);
726
727bool mei_write_is_idle(struct mei_device *dev);
728
729#if IS_ENABLED(CONFIG_DEBUG_FS)
730int mei_dbgfs_register(struct mei_device *dev, const char *name);
731void mei_dbgfs_deregister(struct mei_device *dev);
732#else
733static inline int mei_dbgfs_register(struct mei_device *dev, const char *name)
734{
735 return 0;
736}
737static inline void mei_dbgfs_deregister(struct mei_device *dev) {}
738#endif
739
740int mei_register(struct mei_device *dev, struct device *parent);
741void mei_deregister(struct mei_device *dev);
742
743#define MEI_HDR_FMT "hdr:host=%02d me=%02d len=%d dma=%1d internal=%1d comp=%1d"
744#define MEI_HDR_PRM(hdr) \
745 (hdr)->host_addr, (hdr)->me_addr, \
746 (hdr)->length, (hdr)->dma_ring, (hdr)->internal, (hdr)->msg_complete
747
748ssize_t mei_fw_status2str(struct mei_fw_status *fw_sts, char *buf, size_t len);
749
750
751
752
753
754
755
756
757
758static inline ssize_t mei_fw_status_str(struct mei_device *dev,
759 char *buf, size_t len)
760{
761 struct mei_fw_status fw_status;
762 int ret;
763
764 buf[0] = '\0';
765
766 ret = mei_fw_status(dev, &fw_status);
767 if (ret)
768 return ret;
769
770 ret = mei_fw_status2str(&fw_status, buf, MEI_FW_STATUS_STR_SZ);
771
772 return ret;
773}
774
775
776#endif
777