mei_dev.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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.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_amthi_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 253: 256 Total Clients
  57. * minus internal client for MEI Bus Messags
  58. * minus internal client for AMTHI
  59. * minus internal client for Watchdog
  60. */
  61. #define MEI_MAX_OPEN_HANDLE_COUNT (MEI_CLIENTS_MAX - 3)
  62. /*
  63. * Internal Clients Number
  64. */
  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_RECOVERING_FROM_RESET,
  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_message_data {
  132. u32 size;
  133. unsigned char *data;
  134. };
  135. struct mei_cl;
  136. /**
  137. * struct mei_cl_cb - file operation callback structure
  138. *
  139. * @cl - file client who is running this operation
  140. * @fop_type - file operation type
  141. */
  142. struct mei_cl_cb {
  143. struct list_head list;
  144. struct mei_cl *cl;
  145. enum mei_cb_file_ops fop_type;
  146. struct mei_message_data request_buffer;
  147. struct mei_message_data response_buffer;
  148. unsigned long buf_idx;
  149. unsigned long read_time;
  150. struct file *file_object;
  151. };
  152. /* MEI client instance carried as file->pirvate_data*/
  153. struct mei_cl {
  154. struct list_head link;
  155. struct mei_device *dev;
  156. enum file_state state;
  157. wait_queue_head_t tx_wait;
  158. wait_queue_head_t rx_wait;
  159. wait_queue_head_t wait;
  160. int read_pending;
  161. int status;
  162. /* ID of client connected */
  163. u8 host_client_id;
  164. u8 me_client_id;
  165. u8 mei_flow_ctrl_creds;
  166. u8 timer_count;
  167. enum mei_file_transaction_states reading_state;
  168. enum mei_file_transaction_states writing_state;
  169. int sm_state;
  170. struct mei_cl_cb *read_cb;
  171. };
  172. /**
  173. * struct mei_device - MEI private device struct
  174. * @hbuf_depth - depth of host(write) buffer
  175. * @wr_ext_msg - buffer for hbm control responses (set in read cycle)
  176. */
  177. struct mei_device {
  178. struct pci_dev *pdev; /* pointer to pci device struct */
  179. /*
  180. * lists of queues
  181. */
  182. /* array of pointers to aio lists */
  183. struct mei_cl_cb read_list; /* driver read queue */
  184. struct mei_cl_cb write_list; /* driver write queue */
  185. struct mei_cl_cb write_waiting_list; /* write waiting queue */
  186. struct mei_cl_cb ctrl_wr_list; /* managed write IOCTL list */
  187. struct mei_cl_cb ctrl_rd_list; /* managed read IOCTL list */
  188. /*
  189. * list of files
  190. */
  191. struct list_head file_list;
  192. long open_handle_count;
  193. /*
  194. * memory of device
  195. */
  196. unsigned int mem_base;
  197. unsigned int mem_length;
  198. void __iomem *mem_addr;
  199. /*
  200. * lock for the device
  201. */
  202. struct mutex device_lock; /* device lock */
  203. struct delayed_work timer_work; /* MEI timer delayed work (timeouts) */
  204. bool recvd_msg;
  205. /*
  206. * hw states of host and fw(ME)
  207. */
  208. u32 host_hw_state;
  209. u32 me_hw_state;
  210. u8 hbuf_depth;
  211. /*
  212. * waiting queue for receive message from FW
  213. */
  214. wait_queue_head_t wait_recvd_msg;
  215. wait_queue_head_t wait_stop_wd;
  216. /*
  217. * mei device states
  218. */
  219. enum mei_dev_state dev_state;
  220. enum mei_init_clients_states init_clients_state;
  221. u16 init_clients_timer;
  222. bool need_reset;
  223. unsigned char rd_msg_buf[MEI_RD_MSG_BUF_SIZE]; /* control messages */
  224. u32 rd_msg_hdr;
  225. u32 wr_msg_buf[128]; /* used for control messages */
  226. struct {
  227. struct mei_msg_hdr hdr;
  228. unsigned char data[4]; /* All HBM messages are 4 bytes */
  229. } wr_ext_msg; /* for control responses */
  230. struct hbm_version version;
  231. struct mei_me_client *me_clients; /* Note: memory has to be allocated */
  232. DECLARE_BITMAP(me_clients_map, MEI_CLIENTS_MAX);
  233. DECLARE_BITMAP(host_clients_map, MEI_CLIENTS_MAX);
  234. u8 me_clients_num;
  235. u8 me_client_presentation_num;
  236. u8 me_client_index;
  237. bool mei_host_buffer_is_empty;
  238. struct mei_cl wd_cl;
  239. enum mei_wd_states wd_state;
  240. bool wd_pending;
  241. u16 wd_timeout;
  242. unsigned char wd_data[MEI_WD_START_MSG_SIZE];
  243. /* amthif list for cmd waiting */
  244. struct mei_cl_cb amthif_cmd_list;
  245. /* driver managed amthif list for reading completed amthif cmd data */
  246. struct mei_cl_cb amthif_rd_complete_list;
  247. struct file *iamthif_file_object;
  248. struct mei_cl iamthif_cl;
  249. struct mei_cl_cb *iamthif_current_cb;
  250. int iamthif_mtu;
  251. unsigned long iamthif_timer;
  252. u32 iamthif_stall_timer;
  253. unsigned char *iamthif_msg_buf; /* Note: memory has to be allocated */
  254. u32 iamthif_msg_buf_size;
  255. u32 iamthif_msg_buf_index;
  256. enum iamthif_states iamthif_state;
  257. bool iamthif_flow_control_pending;
  258. bool iamthif_ioctl;
  259. bool iamthif_canceled;
  260. struct work_struct init_work;
  261. };
  262. static inline unsigned long mei_secs_to_jiffies(unsigned long sec)
  263. {
  264. return msecs_to_jiffies(sec * MSEC_PER_SEC);
  265. }
  266. /*
  267. * mei init function prototypes
  268. */
  269. struct mei_device *mei_device_init(struct pci_dev *pdev);
  270. void mei_reset(struct mei_device *dev, int interrupts);
  271. int mei_hw_init(struct mei_device *dev);
  272. int mei_task_initialize_clients(void *data);
  273. int mei_initialize_clients(struct mei_device *dev);
  274. int mei_disconnect_host_client(struct mei_device *dev, struct mei_cl *cl);
  275. void mei_allocate_me_clients_storage(struct mei_device *dev);
  276. int mei_me_cl_link(struct mei_device *dev, struct mei_cl *cl,
  277. const uuid_le *cguid, u8 host_client_id);
  278. void mei_me_cl_unlink(struct mei_device *dev, struct mei_cl *cl);
  279. int mei_me_cl_by_uuid(const struct mei_device *dev, const uuid_le *cuuid);
  280. int mei_me_cl_by_id(struct mei_device *dev, u8 client_id);
  281. /*
  282. * MEI IO Functions
  283. */
  284. struct mei_cl_cb *mei_io_cb_init(struct mei_cl *cl, struct file *fp);
  285. void mei_io_cb_free(struct mei_cl_cb *priv_cb);
  286. int mei_io_cb_alloc_req_buf(struct mei_cl_cb *cb, size_t length);
  287. int mei_io_cb_alloc_resp_buf(struct mei_cl_cb *cb, size_t length);
  288. /**
  289. * mei_io_list_init - Sets up a queue list.
  290. *
  291. * @list: An instance cl callback structure
  292. */
  293. static inline void mei_io_list_init(struct mei_cl_cb *list)
  294. {
  295. INIT_LIST_HEAD(&list->list);
  296. }
  297. void mei_io_list_flush(struct mei_cl_cb *list, struct mei_cl *cl);
  298. /*
  299. * MEI ME Client Functions
  300. */
  301. struct mei_cl *mei_cl_allocate(struct mei_device *dev);
  302. void mei_cl_init(struct mei_cl *cl, struct mei_device *dev);
  303. int mei_cl_flush_queues(struct mei_cl *cl);
  304. /**
  305. * mei_cl_cmp_id - tells if file private data have same id
  306. *
  307. * @fe1: private data of 1. file object
  308. * @fe2: private data of 2. file object
  309. *
  310. * returns true - if ids are the same and not NULL
  311. */
  312. static inline bool mei_cl_cmp_id(const struct mei_cl *cl1,
  313. const struct mei_cl *cl2)
  314. {
  315. return cl1 && cl2 &&
  316. (cl1->host_client_id == cl2->host_client_id) &&
  317. (cl1->me_client_id == cl2->me_client_id);
  318. }
  319. /*
  320. * MEI Host Client Functions
  321. */
  322. void mei_host_start_message(struct mei_device *dev);
  323. void mei_host_enum_clients_message(struct mei_device *dev);
  324. int mei_host_client_enumerate(struct mei_device *dev);
  325. void mei_host_client_init(struct work_struct *work);
  326. /*
  327. * MEI interrupt functions prototype
  328. */
  329. irqreturn_t mei_interrupt_quick_handler(int irq, void *dev_id);
  330. irqreturn_t mei_interrupt_thread_handler(int irq, void *dev_id);
  331. void mei_timer(struct work_struct *work);
  332. /*
  333. * MEI input output function prototype
  334. */
  335. int mei_ioctl_connect_client(struct file *file,
  336. struct mei_connect_client_data *data);
  337. int mei_start_read(struct mei_device *dev, struct mei_cl *cl);
  338. /*
  339. * AMTHIF - AMT Host Interface Functions
  340. */
  341. void mei_amthif_reset_params(struct mei_device *dev);
  342. void mei_amthif_host_init(struct mei_device *dev);
  343. int mei_amthif_write(struct mei_device *dev, struct mei_cl_cb *priv_cb);
  344. int mei_amthif_read(struct mei_device *dev, struct file *file,
  345. char __user *ubuf, size_t length, loff_t *offset);
  346. unsigned int mei_amthif_poll(struct mei_device *dev,
  347. struct file *file, poll_table *wait);
  348. int mei_amthif_release(struct mei_device *dev, struct file *file);
  349. struct mei_cl_cb *mei_amthif_find_read_list_entry(struct mei_device *dev,
  350. struct file *file);
  351. void mei_amthif_run_next_cmd(struct mei_device *dev);
  352. int mei_amthif_read_message(struct mei_cl_cb *complete_list,
  353. struct mei_device *dev, struct mei_msg_hdr *mei_hdr);
  354. int mei_amthif_irq_write_complete(struct mei_device *dev, s32 *slots,
  355. struct mei_cl_cb *cb, struct mei_cl_cb *cmpl_list);
  356. void mei_amthif_complete(struct mei_device *dev, struct mei_cl_cb *cb);
  357. int mei_amthif_irq_read_message(struct mei_cl_cb *complete_list,
  358. struct mei_device *dev, struct mei_msg_hdr *mei_hdr);
  359. int mei_amthif_irq_read(struct mei_device *dev, s32 *slots);
  360. /*
  361. * Register Access Function
  362. */
  363. /**
  364. * mei_reg_read - Reads 32bit data from the mei device
  365. *
  366. * @dev: the device structure
  367. * @offset: offset from which to read the data
  368. *
  369. * returns register value (u32)
  370. */
  371. static inline u32 mei_reg_read(const struct mei_device *dev,
  372. unsigned long offset)
  373. {
  374. return ioread32(dev->mem_addr + offset);
  375. }
  376. /**
  377. * mei_reg_write - Writes 32bit data to the mei device
  378. *
  379. * @dev: the device structure
  380. * @offset: offset from which to write the data
  381. * @value: register value to write (u32)
  382. */
  383. static inline void mei_reg_write(const struct mei_device *dev,
  384. unsigned long offset, u32 value)
  385. {
  386. iowrite32(value, dev->mem_addr + offset);
  387. }
  388. /**
  389. * mei_hcsr_read - Reads 32bit data from the host CSR
  390. *
  391. * @dev: the device structure
  392. *
  393. * returns the byte read.
  394. */
  395. static inline u32 mei_hcsr_read(const struct mei_device *dev)
  396. {
  397. return mei_reg_read(dev, H_CSR);
  398. }
  399. /**
  400. * mei_mecsr_read - Reads 32bit data from the ME CSR
  401. *
  402. * @dev: the device structure
  403. *
  404. * returns ME_CSR_HA register value (u32)
  405. */
  406. static inline u32 mei_mecsr_read(const struct mei_device *dev)
  407. {
  408. return mei_reg_read(dev, ME_CSR_HA);
  409. }
  410. /**
  411. * get_me_cb_rw - Reads 32bit data from the mei ME_CB_RW register
  412. *
  413. * @dev: the device structure
  414. *
  415. * returns ME_CB_RW register value (u32)
  416. */
  417. static inline u32 mei_mecbrw_read(const struct mei_device *dev)
  418. {
  419. return mei_reg_read(dev, ME_CB_RW);
  420. }
  421. /*
  422. * mei interface function prototypes
  423. */
  424. void mei_hcsr_set(struct mei_device *dev);
  425. void mei_csr_clear_his(struct mei_device *dev);
  426. void mei_enable_interrupts(struct mei_device *dev);
  427. void mei_disable_interrupts(struct mei_device *dev);
  428. static inline struct mei_msg_hdr *mei_hbm_hdr(u32 *buf, size_t length)
  429. {
  430. struct mei_msg_hdr *hdr = (struct mei_msg_hdr *)buf;
  431. hdr->host_addr = 0;
  432. hdr->me_addr = 0;
  433. hdr->length = length;
  434. hdr->msg_complete = 1;
  435. hdr->reserved = 0;
  436. return hdr;
  437. }
  438. #define MEI_HDR_FMT "hdr:host=%02d me=%02d len=%d comp=%1d"
  439. #define MEI_HDR_PRM(hdr) \
  440. (hdr)->host_addr, (hdr)->me_addr, \
  441. (hdr)->length, (hdr)->msg_complete
  442. #endif