mei_dev.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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 "hw.h"
  23. #include "hw-me-regs.h"
  24. /*
  25. * watch dog definition
  26. */
  27. #define MEI_WD_HDR_SIZE 4
  28. #define MEI_WD_STOP_MSG_SIZE MEI_WD_HDR_SIZE
  29. #define MEI_WD_START_MSG_SIZE (MEI_WD_HDR_SIZE + 16)
  30. #define MEI_WD_DEFAULT_TIMEOUT 120 /* seconds */
  31. #define MEI_WD_MIN_TIMEOUT 120 /* seconds */
  32. #define MEI_WD_MAX_TIMEOUT 65535 /* seconds */
  33. #define MEI_WD_STOP_TIMEOUT 10 /* msecs */
  34. #define MEI_WD_STATE_INDEPENDENCE_MSG_SENT (1 << 0)
  35. #define MEI_RD_MSG_BUF_SIZE (128 * sizeof(u32))
  36. /*
  37. * AMTHI Client UUID
  38. */
  39. extern const uuid_le mei_amthif_guid;
  40. /*
  41. * Watchdog Client UUID
  42. */
  43. extern const uuid_le mei_wd_guid;
  44. /*
  45. * Watchdog independence state message
  46. */
  47. extern const u8 mei_wd_state_independence_msg[3][4];
  48. /*
  49. * Number of Maximum MEI Clients
  50. */
  51. #define MEI_CLIENTS_MAX 256
  52. /*
  53. * Number of File descriptors/handles
  54. * that can be opened to the driver.
  55. *
  56. * Limit to 255: 256 Total Clients
  57. * minus internal client for MEI Bus Messags
  58. */
  59. #define MEI_MAX_OPEN_HANDLE_COUNT (MEI_CLIENTS_MAX - 1)
  60. /*
  61. * Internal Clients Number
  62. */
  63. #define MEI_HOST_CLIENT_ID_ANY (-1)
  64. #define MEI_HBM_HOST_CLIENT_ID 0 /* not used, just for documentation */
  65. #define MEI_WD_HOST_CLIENT_ID 1
  66. #define MEI_IAMTHIF_HOST_CLIENT_ID 2
  67. /* File state */
  68. enum file_state {
  69. MEI_FILE_INITIALIZING = 0,
  70. MEI_FILE_CONNECTING,
  71. MEI_FILE_CONNECTED,
  72. MEI_FILE_DISCONNECTING,
  73. MEI_FILE_DISCONNECTED
  74. };
  75. /* MEI device states */
  76. enum mei_dev_state {
  77. MEI_DEV_INITIALIZING = 0,
  78. MEI_DEV_INIT_CLIENTS,
  79. MEI_DEV_ENABLED,
  80. MEI_DEV_RESETING,
  81. MEI_DEV_DISABLED,
  82. MEI_DEV_POWER_DOWN,
  83. MEI_DEV_POWER_UP
  84. };
  85. const char *mei_dev_state_str(int state);
  86. /* init clients states*/
  87. enum mei_init_clients_states {
  88. MEI_START_MESSAGE = 0,
  89. MEI_ENUM_CLIENTS_MESSAGE,
  90. MEI_CLIENT_PROPERTIES_MESSAGE
  91. };
  92. enum iamthif_states {
  93. MEI_IAMTHIF_IDLE,
  94. MEI_IAMTHIF_WRITING,
  95. MEI_IAMTHIF_FLOW_CONTROL,
  96. MEI_IAMTHIF_READING,
  97. MEI_IAMTHIF_READ_COMPLETE
  98. };
  99. enum mei_file_transaction_states {
  100. MEI_IDLE,
  101. MEI_WRITING,
  102. MEI_WRITE_COMPLETE,
  103. MEI_FLOW_CONTROL,
  104. MEI_READING,
  105. MEI_READ_COMPLETE
  106. };
  107. enum mei_wd_states {
  108. MEI_WD_IDLE,
  109. MEI_WD_RUNNING,
  110. MEI_WD_STOPPING,
  111. };
  112. /**
  113. * enum mei_cb_file_ops - file operation associated with the callback
  114. * @MEI_FOP_READ - read
  115. * @MEI_FOP_WRITE - write
  116. * @MEI_FOP_IOCTL - ioctl
  117. * @MEI_FOP_OPEN - open
  118. * @MEI_FOP_CLOSE - close
  119. */
  120. enum mei_cb_file_ops {
  121. MEI_FOP_READ = 0,
  122. MEI_FOP_WRITE,
  123. MEI_FOP_IOCTL,
  124. MEI_FOP_OPEN,
  125. MEI_FOP_CLOSE
  126. };
  127. /*
  128. * Intel MEI message data struct
  129. */
  130. struct mei_message_data {
  131. u32 size;
  132. unsigned char *data;
  133. };
  134. /**
  135. * struct mei_me_client - representation of me (fw) client
  136. *
  137. * @props - client properties
  138. * @client_id - me client id
  139. * @mei_flow_ctrl_creds - flow control credits
  140. */
  141. struct mei_me_client {
  142. struct mei_client_properties props;
  143. u8 client_id;
  144. u8 mei_flow_ctrl_creds;
  145. };
  146. struct mei_cl;
  147. /**
  148. * struct mei_cl_cb - file operation callback structure
  149. *
  150. * @cl - file client who is running this operation
  151. * @fop_type - file operation type
  152. */
  153. struct mei_cl_cb {
  154. struct list_head list;
  155. struct mei_cl *cl;
  156. enum mei_cb_file_ops fop_type;
  157. struct mei_message_data request_buffer;
  158. struct mei_message_data response_buffer;
  159. unsigned long buf_idx;
  160. unsigned long read_time;
  161. struct file *file_object;
  162. };
  163. /* MEI client instance carried as file->pirvate_data*/
  164. struct mei_cl {
  165. struct list_head link;
  166. struct mei_device *dev;
  167. enum file_state state;
  168. wait_queue_head_t tx_wait;
  169. wait_queue_head_t rx_wait;
  170. wait_queue_head_t wait;
  171. int status;
  172. /* ID of client connected */
  173. u8 host_client_id;
  174. u8 me_client_id;
  175. u8 mei_flow_ctrl_creds;
  176. u8 timer_count;
  177. enum mei_file_transaction_states reading_state;
  178. enum mei_file_transaction_states writing_state;
  179. int sm_state;
  180. struct mei_cl_cb *read_cb;
  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. void (*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. /**
  221. * struct mei_device - MEI private device struct
  222. * @mem_addr - mem mapped base register address
  223. * @hbuf_depth - depth of hardware host/write buffer is slots
  224. * @hbuf_is_ready - query if the host host/write buffer is ready
  225. * @wr_msg - the buffer for hbm control messages
  226. * @wr_ext_msg - the buffer for hbm control responses (set in read cycle)
  227. */
  228. struct mei_device {
  229. struct pci_dev *pdev; /* pointer to pci device struct */
  230. /*
  231. * lists of queues
  232. */
  233. /* array of pointers to aio lists */
  234. struct mei_cl_cb read_list; /* driver read queue */
  235. struct mei_cl_cb write_list; /* driver write queue */
  236. struct mei_cl_cb write_waiting_list; /* write waiting queue */
  237. struct mei_cl_cb ctrl_wr_list; /* managed write IOCTL list */
  238. struct mei_cl_cb ctrl_rd_list; /* managed read IOCTL list */
  239. /*
  240. * list of files
  241. */
  242. struct list_head file_list;
  243. long open_handle_count;
  244. /*
  245. * lock for the device
  246. */
  247. struct mutex device_lock; /* device lock */
  248. struct delayed_work timer_work; /* MEI timer delayed work (timeouts) */
  249. bool recvd_hw_ready;
  250. bool recvd_msg;
  251. /*
  252. * waiting queue for receive message from FW
  253. */
  254. wait_queue_head_t wait_hw_ready;
  255. wait_queue_head_t wait_recvd_msg;
  256. wait_queue_head_t wait_stop_wd;
  257. /*
  258. * mei device states
  259. */
  260. enum mei_dev_state dev_state;
  261. enum mei_init_clients_states init_clients_state;
  262. u16 init_clients_timer;
  263. unsigned char rd_msg_buf[MEI_RD_MSG_BUF_SIZE]; /* control messages */
  264. u32 rd_msg_hdr;
  265. /* write buffer */
  266. u8 hbuf_depth;
  267. bool hbuf_is_ready;
  268. /* used for control messages */
  269. struct {
  270. struct mei_msg_hdr hdr;
  271. unsigned char data[128];
  272. } wr_msg;
  273. struct {
  274. struct mei_msg_hdr hdr;
  275. unsigned char data[4]; /* All HBM messages are 4 bytes */
  276. } wr_ext_msg; /* for control responses */
  277. struct hbm_version version;
  278. struct mei_me_client *me_clients; /* Note: memory has to be allocated */
  279. DECLARE_BITMAP(me_clients_map, MEI_CLIENTS_MAX);
  280. DECLARE_BITMAP(host_clients_map, MEI_CLIENTS_MAX);
  281. u8 me_clients_num;
  282. u8 me_client_presentation_num;
  283. u8 me_client_index;
  284. struct mei_cl wd_cl;
  285. enum mei_wd_states wd_state;
  286. bool wd_pending;
  287. u16 wd_timeout;
  288. unsigned char wd_data[MEI_WD_START_MSG_SIZE];
  289. /* amthif list for cmd waiting */
  290. struct mei_cl_cb amthif_cmd_list;
  291. /* driver managed amthif list for reading completed amthif cmd data */
  292. struct mei_cl_cb amthif_rd_complete_list;
  293. struct file *iamthif_file_object;
  294. struct mei_cl iamthif_cl;
  295. struct mei_cl_cb *iamthif_current_cb;
  296. int iamthif_mtu;
  297. unsigned long iamthif_timer;
  298. u32 iamthif_stall_timer;
  299. unsigned char *iamthif_msg_buf; /* Note: memory has to be allocated */
  300. u32 iamthif_msg_buf_size;
  301. u32 iamthif_msg_buf_index;
  302. enum iamthif_states iamthif_state;
  303. bool iamthif_flow_control_pending;
  304. bool iamthif_ioctl;
  305. bool iamthif_canceled;
  306. struct work_struct init_work;
  307. const struct mei_hw_ops *ops;
  308. char hw[0] __aligned(sizeof(void *));
  309. };
  310. static inline unsigned long mei_secs_to_jiffies(unsigned long sec)
  311. {
  312. return msecs_to_jiffies(sec * MSEC_PER_SEC);
  313. }
  314. /**
  315. * mei_data2slots - get slots - number of (dwords) from a message length
  316. * + size of the mei header
  317. * @length - size of the messages in bytes
  318. * returns - number of slots
  319. */
  320. static inline u32 mei_data2slots(size_t length)
  321. {
  322. return DIV_ROUND_UP(sizeof(struct mei_msg_hdr) + length, 4);
  323. }
  324. /*
  325. * mei init function prototypes
  326. */
  327. void mei_device_init(struct mei_device *dev);
  328. void mei_reset(struct mei_device *dev, int interrupts);
  329. int mei_start(struct mei_device *dev);
  330. void mei_stop(struct mei_device *dev);
  331. /*
  332. * MEI interrupt functions prototype
  333. */
  334. void mei_timer(struct work_struct *work);
  335. int mei_irq_read_handler(struct mei_device *dev,
  336. struct mei_cl_cb *cmpl_list, s32 *slots);
  337. int mei_irq_write_handler(struct mei_device *dev, struct mei_cl_cb *cmpl_list);
  338. void mei_irq_compl_handler(struct mei_device *dev, struct mei_cl_cb *cmpl_list);
  339. /*
  340. * AMTHIF - AMT Host Interface Functions
  341. */
  342. void mei_amthif_reset_params(struct mei_device *dev);
  343. int mei_amthif_host_init(struct mei_device *dev);
  344. int mei_amthif_write(struct mei_device *dev, struct mei_cl_cb *priv_cb);
  345. int mei_amthif_read(struct mei_device *dev, struct file *file,
  346. char __user *ubuf, size_t length, loff_t *offset);
  347. unsigned int mei_amthif_poll(struct mei_device *dev,
  348. struct file *file, poll_table *wait);
  349. int mei_amthif_release(struct mei_device *dev, struct file *file);
  350. struct mei_cl_cb *mei_amthif_find_read_list_entry(struct mei_device *dev,
  351. struct file *file);
  352. void mei_amthif_run_next_cmd(struct mei_device *dev);
  353. int mei_amthif_irq_write_complete(struct mei_device *dev, s32 *slots,
  354. struct mei_cl_cb *cb, struct mei_cl_cb *cmpl_list);
  355. void mei_amthif_complete(struct mei_device *dev, struct mei_cl_cb *cb);
  356. int mei_amthif_irq_read_message(struct mei_cl_cb *complete_list,
  357. struct mei_device *dev, struct mei_msg_hdr *mei_hdr);
  358. int mei_amthif_irq_read(struct mei_device *dev, s32 *slots);
  359. int mei_wd_send(struct mei_device *dev);
  360. int mei_wd_stop(struct mei_device *dev);
  361. int mei_wd_host_init(struct mei_device *dev);
  362. /*
  363. * mei_watchdog_register - Registering watchdog interface
  364. * once we got connection to the WD Client
  365. * @dev - mei device
  366. */
  367. void mei_watchdog_register(struct mei_device *dev);
  368. /*
  369. * mei_watchdog_unregister - Unregistering watchdog interface
  370. * @dev - mei device
  371. */
  372. void mei_watchdog_unregister(struct mei_device *dev);
  373. /*
  374. * Register Access Function
  375. */
  376. static inline void mei_hw_config(struct mei_device *dev)
  377. {
  378. dev->ops->hw_config(dev);
  379. }
  380. static inline void mei_hw_reset(struct mei_device *dev, bool enable)
  381. {
  382. dev->ops->hw_reset(dev, enable);
  383. }
  384. static inline void mei_hw_start(struct mei_device *dev)
  385. {
  386. dev->ops->hw_start(dev);
  387. }
  388. static inline void mei_clear_interrupts(struct mei_device *dev)
  389. {
  390. dev->ops->intr_clear(dev);
  391. }
  392. static inline void mei_enable_interrupts(struct mei_device *dev)
  393. {
  394. dev->ops->intr_enable(dev);
  395. }
  396. static inline void mei_disable_interrupts(struct mei_device *dev)
  397. {
  398. dev->ops->intr_disable(dev);
  399. }
  400. static inline bool mei_host_is_ready(struct mei_device *dev)
  401. {
  402. return dev->ops->host_is_ready(dev);
  403. }
  404. static inline bool mei_hw_is_ready(struct mei_device *dev)
  405. {
  406. return dev->ops->hw_is_ready(dev);
  407. }
  408. static inline bool mei_hbuf_is_ready(struct mei_device *dev)
  409. {
  410. return dev->ops->hbuf_is_ready(dev);
  411. }
  412. static inline int mei_hbuf_empty_slots(struct mei_device *dev)
  413. {
  414. return dev->ops->hbuf_free_slots(dev);
  415. }
  416. static inline size_t mei_hbuf_max_len(const struct mei_device *dev)
  417. {
  418. return dev->ops->hbuf_max_len(dev);
  419. }
  420. static inline int mei_write_message(struct mei_device *dev,
  421. struct mei_msg_hdr *hdr,
  422. unsigned char *buf)
  423. {
  424. return dev->ops->write(dev, hdr, buf);
  425. }
  426. static inline u32 mei_read_hdr(const struct mei_device *dev)
  427. {
  428. return dev->ops->read_hdr(dev);
  429. }
  430. static inline void mei_read_slots(struct mei_device *dev,
  431. unsigned char *buf, unsigned long len)
  432. {
  433. dev->ops->read(dev, buf, len);
  434. }
  435. static inline int mei_count_full_read_slots(struct mei_device *dev)
  436. {
  437. return dev->ops->rdbuf_full_slots(dev);
  438. }
  439. int mei_register(struct device *dev);
  440. void mei_deregister(void);
  441. #define MEI_HDR_FMT "hdr:host=%02d me=%02d len=%d comp=%1d"
  442. #define MEI_HDR_PRM(hdr) \
  443. (hdr)->host_addr, (hdr)->me_addr, \
  444. (hdr)->length, (hdr)->msg_complete
  445. #endif