0001
0002
0003
0004
0005
0006
0007 #ifndef _MEI_DEV_H_
0008 #define _MEI_DEV_H_
0009
0010 #include <linux/types.h>
0011 #include <linux/cdev.h>
0012 #include <linux/poll.h>
0013 #include <linux/mei.h>
0014 #include <linux/mei_cl_bus.h>
0015
0016 #include "hw.h"
0017 #include "hbm.h"
0018
0019 #define MEI_SLOT_SIZE sizeof(u32)
0020 #define MEI_RD_MSG_BUF_SIZE (128 * MEI_SLOT_SIZE)
0021
0022
0023
0024
0025 #define MEI_CLIENTS_MAX 256
0026
0027
0028
0029
0030 #define MEI_MAX_CONSEC_RESET 3
0031
0032
0033
0034
0035
0036
0037
0038
0039 #define MEI_MAX_OPEN_HANDLE_COUNT (MEI_CLIENTS_MAX - 1)
0040
0041
0042 enum file_state {
0043 MEI_FILE_UNINITIALIZED = 0,
0044 MEI_FILE_INITIALIZING,
0045 MEI_FILE_CONNECTING,
0046 MEI_FILE_CONNECTED,
0047 MEI_FILE_DISCONNECTING,
0048 MEI_FILE_DISCONNECT_REPLY,
0049 MEI_FILE_DISCONNECT_REQUIRED,
0050 MEI_FILE_DISCONNECTED,
0051 };
0052
0053
0054 enum mei_dev_state {
0055 MEI_DEV_INITIALIZING = 0,
0056 MEI_DEV_INIT_CLIENTS,
0057 MEI_DEV_ENABLED,
0058 MEI_DEV_RESETTING,
0059 MEI_DEV_DISABLED,
0060 MEI_DEV_POWERING_DOWN,
0061 MEI_DEV_POWER_DOWN,
0062 MEI_DEV_POWER_UP
0063 };
0064
0065 const char *mei_dev_state_str(int state);
0066
0067 enum mei_file_transaction_states {
0068 MEI_IDLE,
0069 MEI_WRITING,
0070 MEI_WRITE_COMPLETE,
0071 };
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085 enum mei_cb_file_ops {
0086 MEI_FOP_READ = 0,
0087 MEI_FOP_WRITE,
0088 MEI_FOP_CONNECT,
0089 MEI_FOP_DISCONNECT,
0090 MEI_FOP_DISCONNECT_RSP,
0091 MEI_FOP_NOTIFY_START,
0092 MEI_FOP_NOTIFY_STOP,
0093 MEI_FOP_DMA_MAP,
0094 MEI_FOP_DMA_UNMAP,
0095 };
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105 enum mei_cl_io_mode {
0106 MEI_CL_IO_TX_BLOCKING = BIT(0),
0107 MEI_CL_IO_TX_INTERNAL = BIT(1),
0108
0109 MEI_CL_IO_RX_NONBLOCK = BIT(2),
0110 };
0111
0112
0113
0114
0115 struct mei_msg_data {
0116 size_t size;
0117 unsigned char *data;
0118 };
0119
0120 struct mei_dma_data {
0121 u8 buffer_id;
0122 void *vaddr;
0123 dma_addr_t daddr;
0124 size_t size;
0125 };
0126
0127
0128
0129
0130
0131
0132
0133
0134 struct mei_dma_dscr {
0135 void *vaddr;
0136 dma_addr_t daddr;
0137 size_t size;
0138 };
0139
0140
0141 #define MEI_FW_STATUS_MAX 6
0142
0143 #define MEI_FW_STATUS_STR_SZ (MEI_FW_STATUS_MAX * (8 + 1))
0144
0145
0146
0147
0148
0149
0150
0151
0152 struct mei_fw_status {
0153 int count;
0154 u32 status[MEI_FW_STATUS_MAX];
0155 };
0156
0157
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167
0168 struct mei_me_client {
0169 struct list_head list;
0170 struct kref refcnt;
0171 struct mei_client_properties props;
0172 u8 client_id;
0173 u8 tx_flow_ctrl_creds;
0174 u8 connect_count;
0175 u8 bus_added;
0176 };
0177
0178
0179 struct mei_cl;
0180
0181
0182
0183
0184
0185
0186
0187
0188
0189
0190
0191
0192
0193
0194
0195 struct mei_cl_cb {
0196 struct list_head list;
0197 struct mei_cl *cl;
0198 enum mei_cb_file_ops fop_type;
0199 struct mei_msg_data buf;
0200 size_t buf_idx;
0201 u8 vtag;
0202 const struct file *fp;
0203 int status;
0204 u32 internal:1;
0205 u32 blocking:1;
0206 };
0207
0208
0209
0210
0211
0212
0213
0214
0215
0216 struct mei_cl_vtag {
0217 struct list_head list;
0218 const struct file *fp;
0219 u8 vtag;
0220 u8 pending_read:1;
0221 };
0222
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242
0243
0244
0245
0246
0247
0248
0249
0250
0251
0252
0253
0254
0255 struct mei_cl {
0256 struct list_head link;
0257 struct mei_device *dev;
0258 enum file_state state;
0259 wait_queue_head_t tx_wait;
0260 wait_queue_head_t rx_wait;
0261 wait_queue_head_t wait;
0262 wait_queue_head_t ev_wait;
0263 struct fasync_struct *ev_async;
0264 int status;
0265 struct mei_me_client *me_cl;
0266 const struct file *fp;
0267 u8 host_client_id;
0268 struct list_head vtag_map;
0269 u8 tx_flow_ctrl_creds;
0270 u8 rx_flow_ctrl_creds;
0271 u8 timer_count;
0272 u8 notify_en;
0273 u8 notify_ev;
0274 u8 tx_cb_queued;
0275 enum mei_file_transaction_states writing_state;
0276 struct list_head rd_pending;
0277 spinlock_t rd_completed_lock;
0278 struct list_head rd_completed;
0279 struct mei_dma_data dma;
0280 u8 dma_mapped;
0281
0282 struct mei_cl_device *cldev;
0283 };
0284
0285 #define MEI_TX_QUEUE_LIMIT_DEFAULT 50
0286 #define MEI_TX_QUEUE_LIMIT_MAX 255
0287 #define MEI_TX_QUEUE_LIMIT_MIN 30
0288
0289
0290
0291
0292
0293
0294
0295
0296
0297
0298
0299
0300
0301
0302
0303
0304
0305
0306
0307
0308
0309
0310
0311
0312
0313
0314
0315
0316
0317
0318
0319
0320
0321 struct mei_hw_ops {
0322
0323 bool (*host_is_ready)(struct mei_device *dev);
0324
0325 bool (*hw_is_ready)(struct mei_device *dev);
0326 int (*hw_reset)(struct mei_device *dev, bool enable);
0327 int (*hw_start)(struct mei_device *dev);
0328 int (*hw_config)(struct mei_device *dev);
0329
0330 int (*fw_status)(struct mei_device *dev, struct mei_fw_status *fw_sts);
0331 int (*trc_status)(struct mei_device *dev, u32 *trc);
0332
0333 enum mei_pg_state (*pg_state)(struct mei_device *dev);
0334 bool (*pg_in_transition)(struct mei_device *dev);
0335 bool (*pg_is_enabled)(struct mei_device *dev);
0336
0337 void (*intr_clear)(struct mei_device *dev);
0338 void (*intr_enable)(struct mei_device *dev);
0339 void (*intr_disable)(struct mei_device *dev);
0340 void (*synchronize_irq)(struct mei_device *dev);
0341
0342 int (*hbuf_free_slots)(struct mei_device *dev);
0343 bool (*hbuf_is_ready)(struct mei_device *dev);
0344 u32 (*hbuf_depth)(const struct mei_device *dev);
0345 int (*write)(struct mei_device *dev,
0346 const void *hdr, size_t hdr_len,
0347 const void *data, size_t data_len);
0348
0349 int (*rdbuf_full_slots)(struct mei_device *dev);
0350
0351 u32 (*read_hdr)(const struct mei_device *dev);
0352 int (*read)(struct mei_device *dev,
0353 unsigned char *buf, unsigned long len);
0354 };
0355
0356
0357 void mei_cl_bus_rescan_work(struct work_struct *work);
0358 void mei_cl_bus_dev_fixup(struct mei_cl_device *dev);
0359 ssize_t __mei_cl_send(struct mei_cl *cl, const u8 *buf, size_t length, u8 vtag,
0360 unsigned int mode);
0361 ssize_t __mei_cl_recv(struct mei_cl *cl, u8 *buf, size_t length, u8 *vtag,
0362 unsigned int mode, unsigned long timeout);
0363 bool mei_cl_bus_rx_event(struct mei_cl *cl);
0364 bool mei_cl_bus_notify_event(struct mei_cl *cl);
0365 void mei_cl_bus_remove_devices(struct mei_device *bus);
0366 int mei_cl_bus_init(void);
0367 void mei_cl_bus_exit(void);
0368
0369
0370
0371
0372
0373
0374
0375
0376
0377
0378 enum mei_pg_event {
0379 MEI_PG_EVENT_IDLE,
0380 MEI_PG_EVENT_WAIT,
0381 MEI_PG_EVENT_RECEIVED,
0382 MEI_PG_EVENT_INTR_WAIT,
0383 MEI_PG_EVENT_INTR_RECEIVED,
0384 };
0385
0386
0387
0388
0389
0390
0391
0392 enum mei_pg_state {
0393 MEI_PG_OFF = 0,
0394 MEI_PG_ON = 1,
0395 };
0396
0397 const char *mei_pg_state_str(enum mei_pg_state state);
0398
0399
0400
0401
0402
0403
0404
0405
0406
0407
0408 struct mei_fw_version {
0409 u8 platform;
0410 u8 major;
0411 u16 minor;
0412 u16 buildno;
0413 u16 hotfix;
0414 };
0415
0416 #define MEI_MAX_FW_VER_BLOCKS 3
0417
0418
0419
0420
0421
0422
0423
0424
0425
0426
0427
0428
0429
0430
0431
0432
0433
0434
0435
0436
0437
0438
0439
0440
0441
0442
0443
0444
0445
0446
0447
0448
0449
0450
0451
0452
0453
0454
0455
0456
0457
0458
0459
0460
0461
0462
0463
0464
0465
0466
0467
0468
0469
0470
0471
0472
0473
0474
0475
0476
0477
0478
0479
0480
0481
0482
0483
0484
0485
0486
0487
0488
0489
0490
0491
0492
0493
0494
0495
0496 struct mei_device {
0497 struct device *dev;
0498 struct cdev cdev;
0499 int minor;
0500
0501 struct list_head write_list;
0502 struct list_head write_waiting_list;
0503 struct list_head ctrl_wr_list;
0504 struct list_head ctrl_rd_list;
0505 u8 tx_queue_limit;
0506
0507 struct list_head file_list;
0508 long open_handle_count;
0509
0510 struct mutex device_lock;
0511 struct delayed_work timer_work;
0512
0513 bool recvd_hw_ready;
0514
0515
0516
0517 wait_queue_head_t wait_hw_ready;
0518 wait_queue_head_t wait_pg;
0519 wait_queue_head_t wait_hbm_start;
0520
0521
0522
0523
0524 unsigned long reset_count;
0525 enum mei_dev_state dev_state;
0526 enum mei_hbm_state hbm_state;
0527 u16 init_clients_timer;
0528
0529
0530
0531
0532 enum mei_pg_event pg_event;
0533 #ifdef CONFIG_PM
0534 struct dev_pm_domain pg_domain;
0535 #endif
0536
0537 unsigned char rd_msg_buf[MEI_RD_MSG_BUF_SIZE];
0538 u32 rd_msg_hdr[MEI_RD_MSG_BUF_SIZE];
0539 int rd_msg_hdr_count;
0540
0541
0542 bool hbuf_is_ready;
0543
0544 struct mei_dma_dscr dr_dscr[DMA_DSCR_NUM];
0545
0546 struct hbm_version version;
0547 unsigned int hbm_f_pg_supported:1;
0548 unsigned int hbm_f_dc_supported:1;
0549 unsigned int hbm_f_dot_supported:1;
0550 unsigned int hbm_f_ev_supported:1;
0551 unsigned int hbm_f_fa_supported:1;
0552 unsigned int hbm_f_ie_supported:1;
0553 unsigned int hbm_f_os_supported:1;
0554 unsigned int hbm_f_dr_supported:1;
0555 unsigned int hbm_f_vt_supported:1;
0556 unsigned int hbm_f_cap_supported:1;
0557 unsigned int hbm_f_cd_supported:1;
0558
0559 struct mei_fw_version fw_ver[MEI_MAX_FW_VER_BLOCKS];
0560
0561 unsigned int fw_f_fw_ver_supported:1;
0562
0563 struct rw_semaphore me_clients_rwsem;
0564 struct list_head me_clients;
0565 DECLARE_BITMAP(me_clients_map, MEI_CLIENTS_MAX);
0566 DECLARE_BITMAP(host_clients_map, MEI_CLIENTS_MAX);
0567
0568 bool allow_fixed_address;
0569 bool override_fixed_address;
0570
0571 struct work_struct reset_work;
0572 struct work_struct bus_rescan_work;
0573
0574
0575 struct list_head device_list;
0576 struct mutex cl_bus_lock;
0577
0578 const char *kind;
0579
0580 #if IS_ENABLED(CONFIG_DEBUG_FS)
0581 struct dentry *dbgfs_dir;
0582 #endif
0583
0584 const struct mei_hw_ops *ops;
0585 char hw[] __aligned(sizeof(void *));
0586 };
0587
0588 static inline unsigned long mei_secs_to_jiffies(unsigned long sec)
0589 {
0590 return msecs_to_jiffies(sec * MSEC_PER_SEC);
0591 }
0592
0593
0594
0595
0596
0597
0598
0599
0600 static inline u32 mei_data2slots(size_t length)
0601 {
0602 return DIV_ROUND_UP(length, MEI_SLOT_SIZE);
0603 }
0604
0605
0606
0607
0608
0609
0610
0611
0612
0613 static inline u32 mei_hbm2slots(size_t length)
0614 {
0615 return DIV_ROUND_UP(sizeof(struct mei_msg_hdr) + length, MEI_SLOT_SIZE);
0616 }
0617
0618
0619
0620
0621
0622
0623
0624
0625 static inline u32 mei_slots2data(int slots)
0626 {
0627 return slots * MEI_SLOT_SIZE;
0628 }
0629
0630
0631
0632
0633 void mei_device_init(struct mei_device *dev,
0634 struct device *device,
0635 const struct mei_hw_ops *hw_ops);
0636 int mei_reset(struct mei_device *dev);
0637 int mei_start(struct mei_device *dev);
0638 int mei_restart(struct mei_device *dev);
0639 void mei_stop(struct mei_device *dev);
0640 void mei_cancel_work(struct mei_device *dev);
0641
0642 void mei_set_devstate(struct mei_device *dev, enum mei_dev_state state);
0643
0644 int mei_dmam_ring_alloc(struct mei_device *dev);
0645 void mei_dmam_ring_free(struct mei_device *dev);
0646 bool mei_dma_ring_is_allocated(struct mei_device *dev);
0647 void mei_dma_ring_reset(struct mei_device *dev);
0648 void mei_dma_ring_read(struct mei_device *dev, unsigned char *buf, u32 len);
0649 void mei_dma_ring_write(struct mei_device *dev, unsigned char *buf, u32 len);
0650 u32 mei_dma_ring_empty_slots(struct mei_device *dev);
0651
0652
0653
0654
0655
0656 void mei_timer(struct work_struct *work);
0657 void mei_schedule_stall_timer(struct mei_device *dev);
0658 int mei_irq_read_handler(struct mei_device *dev,
0659 struct list_head *cmpl_list, s32 *slots);
0660
0661 int mei_irq_write_handler(struct mei_device *dev, struct list_head *cmpl_list);
0662 void mei_irq_compl_handler(struct mei_device *dev, struct list_head *cmpl_list);
0663
0664
0665
0666
0667
0668
0669 static inline int mei_hw_config(struct mei_device *dev)
0670 {
0671 return dev->ops->hw_config(dev);
0672 }
0673
0674 static inline enum mei_pg_state mei_pg_state(struct mei_device *dev)
0675 {
0676 return dev->ops->pg_state(dev);
0677 }
0678
0679 static inline bool mei_pg_in_transition(struct mei_device *dev)
0680 {
0681 return dev->ops->pg_in_transition(dev);
0682 }
0683
0684 static inline bool mei_pg_is_enabled(struct mei_device *dev)
0685 {
0686 return dev->ops->pg_is_enabled(dev);
0687 }
0688
0689 static inline int mei_hw_reset(struct mei_device *dev, bool enable)
0690 {
0691 return dev->ops->hw_reset(dev, enable);
0692 }
0693
0694 static inline int mei_hw_start(struct mei_device *dev)
0695 {
0696 return dev->ops->hw_start(dev);
0697 }
0698
0699 static inline void mei_clear_interrupts(struct mei_device *dev)
0700 {
0701 dev->ops->intr_clear(dev);
0702 }
0703
0704 static inline void mei_enable_interrupts(struct mei_device *dev)
0705 {
0706 dev->ops->intr_enable(dev);
0707 }
0708
0709 static inline void mei_disable_interrupts(struct mei_device *dev)
0710 {
0711 dev->ops->intr_disable(dev);
0712 }
0713
0714 static inline void mei_synchronize_irq(struct mei_device *dev)
0715 {
0716 dev->ops->synchronize_irq(dev);
0717 }
0718
0719 static inline bool mei_host_is_ready(struct mei_device *dev)
0720 {
0721 return dev->ops->host_is_ready(dev);
0722 }
0723 static inline bool mei_hw_is_ready(struct mei_device *dev)
0724 {
0725 return dev->ops->hw_is_ready(dev);
0726 }
0727
0728 static inline bool mei_hbuf_is_ready(struct mei_device *dev)
0729 {
0730 return dev->ops->hbuf_is_ready(dev);
0731 }
0732
0733 static inline int mei_hbuf_empty_slots(struct mei_device *dev)
0734 {
0735 return dev->ops->hbuf_free_slots(dev);
0736 }
0737
0738 static inline u32 mei_hbuf_depth(const struct mei_device *dev)
0739 {
0740 return dev->ops->hbuf_depth(dev);
0741 }
0742
0743 static inline int mei_write_message(struct mei_device *dev,
0744 const void *hdr, size_t hdr_len,
0745 const void *data, size_t data_len)
0746 {
0747 return dev->ops->write(dev, hdr, hdr_len, data, data_len);
0748 }
0749
0750 static inline u32 mei_read_hdr(const struct mei_device *dev)
0751 {
0752 return dev->ops->read_hdr(dev);
0753 }
0754
0755 static inline void mei_read_slots(struct mei_device *dev,
0756 unsigned char *buf, unsigned long len)
0757 {
0758 dev->ops->read(dev, buf, len);
0759 }
0760
0761 static inline int mei_count_full_read_slots(struct mei_device *dev)
0762 {
0763 return dev->ops->rdbuf_full_slots(dev);
0764 }
0765
0766 static inline int mei_trc_status(struct mei_device *dev, u32 *trc)
0767 {
0768 if (dev->ops->trc_status)
0769 return dev->ops->trc_status(dev, trc);
0770 return -EOPNOTSUPP;
0771 }
0772
0773 static inline int mei_fw_status(struct mei_device *dev,
0774 struct mei_fw_status *fw_status)
0775 {
0776 return dev->ops->fw_status(dev, fw_status);
0777 }
0778
0779 bool mei_hbuf_acquire(struct mei_device *dev);
0780
0781 bool mei_write_is_idle(struct mei_device *dev);
0782
0783 #if IS_ENABLED(CONFIG_DEBUG_FS)
0784 void mei_dbgfs_register(struct mei_device *dev, const char *name);
0785 void mei_dbgfs_deregister(struct mei_device *dev);
0786 #else
0787 static inline void mei_dbgfs_register(struct mei_device *dev, const char *name) {}
0788 static inline void mei_dbgfs_deregister(struct mei_device *dev) {}
0789 #endif
0790
0791 int mei_register(struct mei_device *dev, struct device *parent);
0792 void mei_deregister(struct mei_device *dev);
0793
0794 #define MEI_HDR_FMT "hdr:host=%02d me=%02d len=%d dma=%1d ext=%1d internal=%1d comp=%1d"
0795 #define MEI_HDR_PRM(hdr) \
0796 (hdr)->host_addr, (hdr)->me_addr, \
0797 (hdr)->length, (hdr)->dma_ring, (hdr)->extended, \
0798 (hdr)->internal, (hdr)->msg_complete
0799
0800 ssize_t mei_fw_status2str(struct mei_fw_status *fw_sts, char *buf, size_t len);
0801
0802
0803
0804
0805
0806
0807
0808
0809
0810 static inline ssize_t mei_fw_status_str(struct mei_device *dev,
0811 char *buf, size_t len)
0812 {
0813 struct mei_fw_status fw_status;
0814 int ret;
0815
0816 buf[0] = '\0';
0817
0818 ret = mei_fw_status(dev, &fw_status);
0819 if (ret)
0820 return ret;
0821
0822 ret = mei_fw_status2str(&fw_status, buf, MEI_FW_STATUS_STR_SZ);
0823
0824 return ret;
0825 }
0826
0827
0828 #endif