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