mei_dev.h 16 KB

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