mei_dev.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. /*
  2. *
  3. * Intel Management Engine Interface (Intel MEI) Linux driver
  4. * Copyright (c) 2003-2012, Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. */
  16. #ifndef _MEI_DEV_H_
  17. #define _MEI_DEV_H_
  18. #include <linux/types.h>
  19. #include <linux/watchdog.h>
  20. #include <linux/poll.h>
  21. #include <linux/mei.h>
  22. #include <linux/mei_cl_bus.h>
  23. #include "hw.h"
  24. #include "hw-me-regs.h"
  25. #include "hbm.h"
  26. /*
  27. * watch dog definition
  28. */
  29. #define MEI_WD_HDR_SIZE 4
  30. #define MEI_WD_STOP_MSG_SIZE MEI_WD_HDR_SIZE
  31. #define MEI_WD_START_MSG_SIZE (MEI_WD_HDR_SIZE + 16)
  32. #define MEI_WD_DEFAULT_TIMEOUT 120 /* seconds */
  33. #define MEI_WD_MIN_TIMEOUT 120 /* seconds */
  34. #define MEI_WD_MAX_TIMEOUT 65535 /* seconds */
  35. #define MEI_WD_STOP_TIMEOUT 10 /* msecs */
  36. #define MEI_WD_STATE_INDEPENDENCE_MSG_SENT (1 << 0)
  37. #define MEI_RD_MSG_BUF_SIZE (128 * sizeof(u32))
  38. /*
  39. * AMTHI Client UUID
  40. */
  41. extern const uuid_le mei_amthif_guid;
  42. /*
  43. * Watchdog Client UUID
  44. */
  45. extern const uuid_le mei_wd_guid;
  46. /*
  47. * Number of Maximum MEI Clients
  48. */
  49. #define MEI_CLIENTS_MAX 256
  50. /*
  51. * maximum number of consecutive resets
  52. */
  53. #define MEI_MAX_CONSEC_RESET 3
  54. /*
  55. * Number of File descriptors/handles
  56. * that can be opened to the driver.
  57. *
  58. * Limit to 255: 256 Total Clients
  59. * minus internal client for MEI Bus Messages
  60. */
  61. #define MEI_MAX_OPEN_HANDLE_COUNT (MEI_CLIENTS_MAX - 1)
  62. /*
  63. * Internal Clients Number
  64. */
  65. #define MEI_HOST_CLIENT_ID_ANY (-1)
  66. #define MEI_HBM_HOST_CLIENT_ID 0 /* not used, just for documentation */
  67. #define MEI_WD_HOST_CLIENT_ID 1
  68. #define MEI_IAMTHIF_HOST_CLIENT_ID 2
  69. /* File state */
  70. enum file_state {
  71. MEI_FILE_INITIALIZING = 0,
  72. MEI_FILE_CONNECTING,
  73. MEI_FILE_CONNECTED,
  74. MEI_FILE_DISCONNECTING,
  75. MEI_FILE_DISCONNECTED
  76. };
  77. /* MEI device states */
  78. enum mei_dev_state {
  79. MEI_DEV_INITIALIZING = 0,
  80. MEI_DEV_INIT_CLIENTS,
  81. MEI_DEV_ENABLED,
  82. MEI_DEV_RESETTING,
  83. MEI_DEV_DISABLED,
  84. MEI_DEV_POWER_DOWN,
  85. MEI_DEV_POWER_UP
  86. };
  87. const char *mei_dev_state_str(int state);
  88. enum iamthif_states {
  89. MEI_IAMTHIF_IDLE,
  90. MEI_IAMTHIF_WRITING,
  91. MEI_IAMTHIF_FLOW_CONTROL,
  92. MEI_IAMTHIF_READING,
  93. MEI_IAMTHIF_READ_COMPLETE
  94. };
  95. enum mei_file_transaction_states {
  96. MEI_IDLE,
  97. MEI_WRITING,
  98. MEI_WRITE_COMPLETE,
  99. MEI_FLOW_CONTROL,
  100. MEI_READING,
  101. MEI_READ_COMPLETE
  102. };
  103. enum mei_wd_states {
  104. MEI_WD_IDLE,
  105. MEI_WD_RUNNING,
  106. MEI_WD_STOPPING,
  107. };
  108. /**
  109. * enum mei_cb_file_ops - file operation associated with the callback
  110. * @MEI_FOP_READ - read
  111. * @MEI_FOP_WRITE - write
  112. * @MEI_FOP_IOCTL - ioctl
  113. * @MEI_FOP_OPEN - open
  114. * @MEI_FOP_CLOSE - close
  115. */
  116. enum mei_cb_file_ops {
  117. MEI_FOP_READ = 0,
  118. MEI_FOP_WRITE,
  119. MEI_FOP_IOCTL,
  120. MEI_FOP_OPEN,
  121. MEI_FOP_CLOSE
  122. };
  123. /*
  124. * Intel MEI message data struct
  125. */
  126. struct mei_msg_data {
  127. u32 size;
  128. unsigned char *data;
  129. };
  130. /**
  131. * struct mei_me_client - representation of me (fw) client
  132. *
  133. * @props - client properties
  134. * @client_id - me client id
  135. * @mei_flow_ctrl_creds - flow control credits
  136. */
  137. struct mei_me_client {
  138. struct mei_client_properties props;
  139. u8 client_id;
  140. u8 mei_flow_ctrl_creds;
  141. };
  142. struct mei_cl;
  143. /**
  144. * struct mei_cl_cb - file operation callback structure
  145. *
  146. * @cl - file client who is running this operation
  147. * @fop_type - file operation type
  148. */
  149. struct mei_cl_cb {
  150. struct list_head list;
  151. struct mei_cl *cl;
  152. enum mei_cb_file_ops fop_type;
  153. struct mei_msg_data request_buffer;
  154. struct mei_msg_data response_buffer;
  155. unsigned long buf_idx;
  156. unsigned long read_time;
  157. struct file *file_object;
  158. u32 internal:1;
  159. };
  160. /* MEI client instance carried as file->private_data*/
  161. struct mei_cl {
  162. struct list_head link;
  163. struct mei_device *dev;
  164. enum file_state state;
  165. wait_queue_head_t tx_wait;
  166. wait_queue_head_t rx_wait;
  167. wait_queue_head_t wait;
  168. int status;
  169. /* ID of client connected */
  170. u8 host_client_id;
  171. u8 me_client_id;
  172. u8 mei_flow_ctrl_creds;
  173. u8 timer_count;
  174. enum mei_file_transaction_states reading_state;
  175. enum mei_file_transaction_states writing_state;
  176. struct mei_cl_cb *read_cb;
  177. /* MEI CL bus data */
  178. struct mei_cl_device *device;
  179. struct list_head device_link;
  180. uuid_le device_uuid;
  181. };
  182. /** struct mei_hw_ops
  183. *
  184. * @host_is_ready - query for host readiness
  185. * @hw_is_ready - query if hw is ready
  186. * @hw_reset - reset hw
  187. * @hw_start - start hw after reset
  188. * @hw_config - configure hw
  189. * @intr_clear - clear pending interrupts
  190. * @intr_enable - enable interrupts
  191. * @intr_disable - disable interrupts
  192. * @hbuf_free_slots - query for write buffer empty slots
  193. * @hbuf_is_ready - query if write buffer is empty
  194. * @hbuf_max_len - query for write buffer max len
  195. * @write - write a message to FW
  196. * @rdbuf_full_slots - query how many slots are filled
  197. * @read_hdr - get first 4 bytes (header)
  198. * @read - read a buffer from the FW
  199. */
  200. struct mei_hw_ops {
  201. bool (*host_is_ready) (struct mei_device *dev);
  202. bool (*hw_is_ready) (struct mei_device *dev);
  203. int (*hw_reset) (struct mei_device *dev, bool enable);
  204. int (*hw_start) (struct mei_device *dev);
  205. void (*hw_config) (struct mei_device *dev);
  206. void (*intr_clear) (struct mei_device *dev);
  207. void (*intr_enable) (struct mei_device *dev);
  208. void (*intr_disable) (struct mei_device *dev);
  209. int (*hbuf_free_slots) (struct mei_device *dev);
  210. bool (*hbuf_is_ready) (struct mei_device *dev);
  211. size_t (*hbuf_max_len) (const struct mei_device *dev);
  212. int (*write)(struct mei_device *dev,
  213. struct mei_msg_hdr *hdr,
  214. unsigned char *buf);
  215. int (*rdbuf_full_slots)(struct mei_device *dev);
  216. u32 (*read_hdr)(const struct mei_device *dev);
  217. int (*read) (struct mei_device *dev,
  218. unsigned char *buf, unsigned long len);
  219. };
  220. /* MEI bus API*/
  221. /**
  222. * struct mei_cl_ops - MEI CL device ops
  223. * This structure allows ME host clients to implement technology
  224. * specific operations.
  225. *
  226. * @enable: Enable an MEI CL device. Some devices require specific
  227. * HECI commands to initialize completely.
  228. * @disable: Disable an MEI CL device.
  229. * @send: Tx hook for the device. This allows ME host clients to trap
  230. * the device driver buffers before actually physically
  231. * pushing it to the ME.
  232. * @recv: Rx hook for the device. This allows ME host clients to trap the
  233. * ME buffers before forwarding them to the device driver.
  234. */
  235. struct mei_cl_ops {
  236. int (*enable)(struct mei_cl_device *device);
  237. int (*disable)(struct mei_cl_device *device);
  238. int (*send)(struct mei_cl_device *device, u8 *buf, size_t length);
  239. int (*recv)(struct mei_cl_device *device, u8 *buf, size_t length);
  240. };
  241. struct mei_cl_device *mei_cl_add_device(struct mei_device *dev,
  242. uuid_le uuid, char *name,
  243. struct mei_cl_ops *ops);
  244. void mei_cl_remove_device(struct mei_cl_device *device);
  245. int __mei_cl_async_send(struct mei_cl *cl, u8 *buf, size_t length);
  246. int __mei_cl_send(struct mei_cl *cl, u8 *buf, size_t length);
  247. int __mei_cl_recv(struct mei_cl *cl, u8 *buf, size_t length);
  248. void mei_cl_bus_rx_event(struct mei_cl *cl);
  249. int mei_cl_bus_init(void);
  250. void mei_cl_bus_exit(void);
  251. /**
  252. * struct mei_cl_device - MEI device handle
  253. * An mei_cl_device pointer is returned from mei_add_device()
  254. * and links MEI bus clients to their actual ME host client pointer.
  255. * Drivers for MEI devices will get an mei_cl_device pointer
  256. * when being probed and shall use it for doing ME bus I/O.
  257. *
  258. * @dev: linux driver model device pointer
  259. * @uuid: me client uuid
  260. * @cl: mei client
  261. * @ops: ME transport ops
  262. * @event_cb: Drivers register this callback to get asynchronous ME
  263. * events (e.g. Rx buffer pending) notifications.
  264. * @events: Events bitmask sent to the driver.
  265. * @priv_data: client private data
  266. */
  267. struct mei_cl_device {
  268. struct device dev;
  269. struct mei_cl *cl;
  270. const struct mei_cl_ops *ops;
  271. struct work_struct event_work;
  272. mei_cl_event_cb_t event_cb;
  273. void *event_context;
  274. unsigned long events;
  275. void *priv_data;
  276. };
  277. /**
  278. * struct mei_device - MEI private device struct
  279. * @reset_count - limits the number of consecutive resets
  280. * @hbm_state - state of host bus message protocol
  281. * @mem_addr - mem mapped base register address
  282. * @hbuf_depth - depth of hardware host/write buffer is slots
  283. * @hbuf_is_ready - query if the host host/write buffer is ready
  284. * @wr_msg - the buffer for hbm control messages
  285. * @wr_ext_msg - the buffer for hbm control responses (set in read cycle)
  286. */
  287. struct mei_device {
  288. struct pci_dev *pdev; /* pointer to pci device struct */
  289. /*
  290. * lists of queues
  291. */
  292. /* array of pointers to aio lists */
  293. struct mei_cl_cb read_list; /* driver read queue */
  294. struct mei_cl_cb write_list; /* driver write queue */
  295. struct mei_cl_cb write_waiting_list; /* write waiting queue */
  296. struct mei_cl_cb ctrl_wr_list; /* managed write IOCTL list */
  297. struct mei_cl_cb ctrl_rd_list; /* managed read IOCTL list */
  298. /*
  299. * list of files
  300. */
  301. struct list_head file_list;
  302. long open_handle_count;
  303. /*
  304. * lock for the device
  305. */
  306. struct mutex device_lock; /* device lock */
  307. struct delayed_work timer_work; /* MEI timer delayed work (timeouts) */
  308. bool recvd_hw_ready;
  309. /*
  310. * waiting queue for receive message from FW
  311. */
  312. wait_queue_head_t wait_hw_ready;
  313. wait_queue_head_t wait_recvd_msg;
  314. wait_queue_head_t wait_stop_wd;
  315. /*
  316. * mei device states
  317. */
  318. unsigned long reset_count;
  319. enum mei_dev_state dev_state;
  320. enum mei_hbm_state hbm_state;
  321. u16 init_clients_timer;
  322. unsigned char rd_msg_buf[MEI_RD_MSG_BUF_SIZE]; /* control messages */
  323. u32 rd_msg_hdr;
  324. /* write buffer */
  325. u8 hbuf_depth;
  326. bool hbuf_is_ready;
  327. /* used for control messages */
  328. struct {
  329. struct mei_msg_hdr hdr;
  330. unsigned char data[128];
  331. } wr_msg;
  332. struct {
  333. struct mei_msg_hdr hdr;
  334. unsigned char data[4]; /* All HBM messages are 4 bytes */
  335. } wr_ext_msg; /* for control responses */
  336. struct hbm_version version;
  337. struct mei_me_client *me_clients; /* Note: memory has to be allocated */
  338. DECLARE_BITMAP(me_clients_map, MEI_CLIENTS_MAX);
  339. DECLARE_BITMAP(host_clients_map, MEI_CLIENTS_MAX);
  340. unsigned long me_clients_num;
  341. unsigned long me_client_presentation_num;
  342. unsigned long me_client_index;
  343. struct mei_cl wd_cl;
  344. enum mei_wd_states wd_state;
  345. bool wd_pending;
  346. u16 wd_timeout;
  347. unsigned char wd_data[MEI_WD_START_MSG_SIZE];
  348. /* amthif list for cmd waiting */
  349. struct mei_cl_cb amthif_cmd_list;
  350. /* driver managed amthif list for reading completed amthif cmd data */
  351. struct mei_cl_cb amthif_rd_complete_list;
  352. struct file *iamthif_file_object;
  353. struct mei_cl iamthif_cl;
  354. struct mei_cl_cb *iamthif_current_cb;
  355. long iamthif_open_count;
  356. int iamthif_mtu;
  357. unsigned long iamthif_timer;
  358. u32 iamthif_stall_timer;
  359. unsigned char *iamthif_msg_buf; /* Note: memory has to be allocated */
  360. u32 iamthif_msg_buf_size;
  361. u32 iamthif_msg_buf_index;
  362. enum iamthif_states iamthif_state;
  363. bool iamthif_flow_control_pending;
  364. bool iamthif_ioctl;
  365. bool iamthif_canceled;
  366. struct work_struct init_work;
  367. struct work_struct reset_work;
  368. /* List of bus devices */
  369. struct list_head device_list;
  370. #if IS_ENABLED(CONFIG_DEBUG_FS)
  371. struct dentry *dbgfs_dir;
  372. #endif /* CONFIG_DEBUG_FS */
  373. const struct mei_hw_ops *ops;
  374. char hw[0] __aligned(sizeof(void *));
  375. };
  376. static inline unsigned long mei_secs_to_jiffies(unsigned long sec)
  377. {
  378. return msecs_to_jiffies(sec * MSEC_PER_SEC);
  379. }
  380. /**
  381. * mei_data2slots - get slots - number of (dwords) from a message length
  382. * + size of the mei header
  383. * @length - size of the messages in bytes
  384. * returns - number of slots
  385. */
  386. static inline u32 mei_data2slots(size_t length)
  387. {
  388. return DIV_ROUND_UP(sizeof(struct mei_msg_hdr) + length, 4);
  389. }
  390. /**
  391. * mei_slots2data- get data in slots - bytes from slots
  392. * @slots - number of available slots
  393. * returns - number of bytes in slots
  394. */
  395. static inline u32 mei_slots2data(int slots)
  396. {
  397. return slots * 4;
  398. }
  399. /*
  400. * mei init function prototypes
  401. */
  402. void mei_device_init(struct mei_device *dev);
  403. int mei_reset(struct mei_device *dev);
  404. int mei_start(struct mei_device *dev);
  405. int mei_restart(struct mei_device *dev);
  406. void mei_stop(struct mei_device *dev);
  407. void mei_cancel_work(struct mei_device *dev);
  408. /*
  409. * MEI interrupt functions prototype
  410. */
  411. void mei_timer(struct work_struct *work);
  412. int mei_irq_read_handler(struct mei_device *dev,
  413. struct mei_cl_cb *cmpl_list, s32 *slots);
  414. int mei_irq_write_handler(struct mei_device *dev, struct mei_cl_cb *cmpl_list);
  415. void mei_irq_compl_handler(struct mei_device *dev, struct mei_cl_cb *cmpl_list);
  416. /*
  417. * AMTHIF - AMT Host Interface Functions
  418. */
  419. void mei_amthif_reset_params(struct mei_device *dev);
  420. int mei_amthif_host_init(struct mei_device *dev);
  421. int mei_amthif_write(struct mei_device *dev, struct mei_cl_cb *priv_cb);
  422. int mei_amthif_read(struct mei_device *dev, struct file *file,
  423. char __user *ubuf, size_t length, loff_t *offset);
  424. unsigned int mei_amthif_poll(struct mei_device *dev,
  425. struct file *file, poll_table *wait);
  426. int mei_amthif_release(struct mei_device *dev, struct file *file);
  427. struct mei_cl_cb *mei_amthif_find_read_list_entry(struct mei_device *dev,
  428. struct file *file);
  429. void mei_amthif_run_next_cmd(struct mei_device *dev);
  430. int mei_amthif_irq_write_complete(struct mei_cl *cl, struct mei_cl_cb *cb,
  431. s32 *slots, struct mei_cl_cb *cmpl_list);
  432. void mei_amthif_complete(struct mei_device *dev, struct mei_cl_cb *cb);
  433. int mei_amthif_irq_read_msg(struct mei_device *dev,
  434. struct mei_msg_hdr *mei_hdr,
  435. struct mei_cl_cb *complete_list);
  436. int mei_amthif_irq_read(struct mei_device *dev, s32 *slots);
  437. /*
  438. * NFC functions
  439. */
  440. int mei_nfc_host_init(struct mei_device *dev);
  441. void mei_nfc_host_exit(struct mei_device *dev);
  442. /*
  443. * NFC Client UUID
  444. */
  445. extern const uuid_le mei_nfc_guid;
  446. int mei_wd_send(struct mei_device *dev);
  447. int mei_wd_stop(struct mei_device *dev);
  448. int mei_wd_host_init(struct mei_device *dev);
  449. /*
  450. * mei_watchdog_register - Registering watchdog interface
  451. * once we got connection to the WD Client
  452. * @dev - mei device
  453. */
  454. void mei_watchdog_register(struct mei_device *dev);
  455. /*
  456. * mei_watchdog_unregister - Unregistering watchdog interface
  457. * @dev - mei device
  458. */
  459. void mei_watchdog_unregister(struct mei_device *dev);
  460. /*
  461. * Register Access Function
  462. */
  463. static inline void mei_hw_config(struct mei_device *dev)
  464. {
  465. dev->ops->hw_config(dev);
  466. }
  467. static inline int mei_hw_reset(struct mei_device *dev, bool enable)
  468. {
  469. return dev->ops->hw_reset(dev, enable);
  470. }
  471. static inline int mei_hw_start(struct mei_device *dev)
  472. {
  473. return dev->ops->hw_start(dev);
  474. }
  475. static inline void mei_clear_interrupts(struct mei_device *dev)
  476. {
  477. dev->ops->intr_clear(dev);
  478. }
  479. static inline void mei_enable_interrupts(struct mei_device *dev)
  480. {
  481. dev->ops->intr_enable(dev);
  482. }
  483. static inline void mei_disable_interrupts(struct mei_device *dev)
  484. {
  485. dev->ops->intr_disable(dev);
  486. }
  487. static inline bool mei_host_is_ready(struct mei_device *dev)
  488. {
  489. return dev->ops->host_is_ready(dev);
  490. }
  491. static inline bool mei_hw_is_ready(struct mei_device *dev)
  492. {
  493. return dev->ops->hw_is_ready(dev);
  494. }
  495. static inline bool mei_hbuf_is_ready(struct mei_device *dev)
  496. {
  497. return dev->ops->hbuf_is_ready(dev);
  498. }
  499. static inline int mei_hbuf_empty_slots(struct mei_device *dev)
  500. {
  501. return dev->ops->hbuf_free_slots(dev);
  502. }
  503. static inline size_t mei_hbuf_max_len(const struct mei_device *dev)
  504. {
  505. return dev->ops->hbuf_max_len(dev);
  506. }
  507. static inline int mei_write_message(struct mei_device *dev,
  508. struct mei_msg_hdr *hdr,
  509. unsigned char *buf)
  510. {
  511. return dev->ops->write(dev, hdr, buf);
  512. }
  513. static inline u32 mei_read_hdr(const struct mei_device *dev)
  514. {
  515. return dev->ops->read_hdr(dev);
  516. }
  517. static inline void mei_read_slots(struct mei_device *dev,
  518. unsigned char *buf, unsigned long len)
  519. {
  520. dev->ops->read(dev, buf, len);
  521. }
  522. static inline int mei_count_full_read_slots(struct mei_device *dev)
  523. {
  524. return dev->ops->rdbuf_full_slots(dev);
  525. }
  526. #if IS_ENABLED(CONFIG_DEBUG_FS)
  527. int mei_dbgfs_register(struct mei_device *dev, const char *name);
  528. void mei_dbgfs_deregister(struct mei_device *dev);
  529. #else
  530. static inline int mei_dbgfs_register(struct mei_device *dev, const char *name)
  531. {
  532. return 0;
  533. }
  534. static inline void mei_dbgfs_deregister(struct mei_device *dev) {}
  535. #endif /* CONFIG_DEBUG_FS */
  536. int mei_register(struct mei_device *dev);
  537. void mei_deregister(struct mei_device *dev);
  538. #define MEI_HDR_FMT "hdr:host=%02d me=%02d len=%d internal=%1d comp=%1d"
  539. #define MEI_HDR_PRM(hdr) \
  540. (hdr)->host_addr, (hdr)->me_addr, \
  541. (hdr)->length, (hdr)->internal, (hdr)->msg_complete
  542. #endif