3 * Intel Management Engine Interface (Intel MEI) Linux driver
4 * Copyright (c) 2003-2012, Intel Corporation.
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.
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
20 #include <linux/types.h>
21 #include <linux/watchdog.h>
22 #include <linux/poll.h>
23 #include <linux/mei.h>
27 * watch dog definition
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)
33 #define MEI_WD_DEFAULT_TIMEOUT 120 /* seconds */
34 #define MEI_WD_MIN_TIMEOUT 120 /* seconds */
35 #define MEI_WD_MAX_TIMEOUT 65535 /* seconds */
37 #define MEI_WD_STOP_TIMEOUT 10 /* msecs */
39 #define MEI_WD_STATE_INDEPENDENCE_MSG_SENT (1 << 0)
41 #define MEI_RD_MSG_BUF_SIZE (128 * sizeof(u32))
47 extern const uuid_le mei_amthi_guid;
50 * Watchdog Client UUID
52 extern const uuid_le mei_wd_guid;
55 * Watchdog independence state message
57 extern const u8 mei_wd_state_independence_msg[3][4];
60 * Number of Maximum MEI Clients
62 #define MEI_CLIENTS_MAX 256
65 * Number of File descriptors/handles
66 * that can be opened to the driver.
68 * Limit to 253: 256 Total Clients
69 * minus internal client for MEI Bus Messags
70 * minus internal client for AMTHI
71 * minus internal client for Watchdog
73 #define MEI_MAX_OPEN_HANDLE_COUNT (MEI_CLIENTS_MAX - 3)
78 MEI_FILE_INITIALIZING = 0,
81 MEI_FILE_DISCONNECTING,
85 /* MEI device states */
87 MEI_DEV_INITIALIZING = 0,
92 MEI_DEV_RECOVERING_FROM_RESET,
97 const char *mei_dev_state_str(int state);
99 /* init clients states*/
100 enum mei_init_clients_states {
101 MEI_START_MESSAGE = 0,
102 MEI_ENUM_CLIENTS_MESSAGE,
103 MEI_CLIENT_PROPERTIES_MESSAGE
106 enum iamthif_states {
109 MEI_IAMTHIF_FLOW_CONTROL,
111 MEI_IAMTHIF_READ_COMPLETE
114 enum mei_file_transaction_states {
130 * enum mei_cb_file_ops - file operation associated with the callback
131 * @MEI_FOP_READ - read
132 * @MEI_FOP_WRITE - write
133 * @MEI_FOP_IOCTL - ioctl
134 * @MEI_FOP_OPEN - open
135 * @MEI_FOP_CLOSE - close
137 enum mei_cb_file_ops {
146 * Intel MEI message data struct
148 struct mei_message_data {
157 * struct mei_cl_cb - file operation callback structure
159 * @cl - file client who is running this operation
160 * @fop_type - file operation type
163 struct list_head list;
165 enum mei_cb_file_ops fop_type;
166 struct mei_message_data request_buffer;
167 struct mei_message_data response_buffer;
168 unsigned long buf_idx;
169 unsigned long read_time;
170 struct file *file_object;
173 /* MEI client instance carried as file->pirvate_data*/
175 struct list_head link;
176 struct mei_device *dev;
177 enum file_state state;
178 wait_queue_head_t tx_wait;
179 wait_queue_head_t rx_wait;
180 wait_queue_head_t wait;
183 /* ID of client connected */
186 u8 mei_flow_ctrl_creds;
188 enum mei_file_transaction_states reading_state;
189 enum mei_file_transaction_states writing_state;
191 struct mei_cl_cb *read_cb;
195 * struct mei_device - MEI private device struct
196 * @hbuf_depth - depth of host(write) buffer
197 * @wr_ext_msg - buffer for hbm control responses (set in read cycle)
200 struct pci_dev *pdev; /* pointer to pci device struct */
204 /* array of pointers to aio lists */
205 struct mei_cl_cb read_list; /* driver read queue */
206 struct mei_cl_cb write_list; /* driver write queue */
207 struct mei_cl_cb write_waiting_list; /* write waiting queue */
208 struct mei_cl_cb ctrl_wr_list; /* managed write IOCTL list */
209 struct mei_cl_cb ctrl_rd_list; /* managed read IOCTL list */
214 struct list_head file_list;
215 long open_handle_count;
219 unsigned int mem_base;
220 unsigned int mem_length;
221 void __iomem *mem_addr;
223 * lock for the device
225 struct mutex device_lock; /* device lock */
226 struct delayed_work timer_work; /* MEI timer delayed work (timeouts) */
229 * hw states of host and fw(ME)
235 * waiting queue for receive message from FW
237 wait_queue_head_t wait_recvd_msg;
238 wait_queue_head_t wait_stop_wd;
243 enum mei_dev_state dev_state;
244 enum mei_init_clients_states init_clients_state;
245 u16 init_clients_timer;
248 unsigned char rd_msg_buf[MEI_RD_MSG_BUF_SIZE]; /* control messages */
250 u32 wr_msg_buf[128]; /* used for control messages */
252 struct mei_msg_hdr hdr;
253 unsigned char data[4]; /* All HBM messages are 4 bytes */
254 } wr_ext_msg; /* for control responses */
256 struct hbm_version version;
258 struct mei_me_client *me_clients; /* Note: memory has to be allocated */
259 DECLARE_BITMAP(me_clients_map, MEI_CLIENTS_MAX);
260 DECLARE_BITMAP(host_clients_map, MEI_CLIENTS_MAX);
262 u8 me_client_presentation_num;
264 bool mei_host_buffer_is_empty;
267 enum mei_wd_states wd_state;
270 unsigned char wd_data[MEI_WD_START_MSG_SIZE];
273 /* amthif list for cmd waiting */
274 struct mei_cl_cb amthif_cmd_list;
275 /* driver managed amthif list for reading completed amthif cmd data */
276 struct mei_cl_cb amthif_rd_complete_list;
277 struct file *iamthif_file_object;
278 struct mei_cl iamthif_cl;
279 struct mei_cl_cb *iamthif_current_cb;
281 unsigned long iamthif_timer;
282 u32 iamthif_stall_timer;
283 unsigned char *iamthif_msg_buf; /* Note: memory has to be allocated */
284 u32 iamthif_msg_buf_size;
285 u32 iamthif_msg_buf_index;
286 enum iamthif_states iamthif_state;
287 bool iamthif_flow_control_pending;
289 bool iamthif_canceled;
291 struct work_struct init_work;
294 static inline unsigned long mei_secs_to_jiffies(unsigned long sec)
296 return msecs_to_jiffies(sec * MSEC_PER_SEC);
301 * mei init function prototypes
303 struct mei_device *mei_device_init(struct pci_dev *pdev);
304 void mei_reset(struct mei_device *dev, int interrupts);
305 int mei_hw_init(struct mei_device *dev);
306 int mei_task_initialize_clients(void *data);
307 int mei_initialize_clients(struct mei_device *dev);
308 int mei_disconnect_host_client(struct mei_device *dev, struct mei_cl *cl);
309 void mei_allocate_me_clients_storage(struct mei_device *dev);
312 int mei_me_cl_link(struct mei_device *dev, struct mei_cl *cl,
313 const uuid_le *cguid, u8 host_client_id);
314 void mei_me_cl_unlink(struct mei_device *dev, struct mei_cl *cl);
315 int mei_me_cl_by_uuid(const struct mei_device *dev, const uuid_le *cuuid);
316 int mei_me_cl_by_id(struct mei_device *dev, u8 client_id);
321 struct mei_cl_cb *mei_io_cb_init(struct mei_cl *cl, struct file *fp);
322 void mei_io_cb_free(struct mei_cl_cb *priv_cb);
323 int mei_io_cb_alloc_req_buf(struct mei_cl_cb *cb, size_t length);
324 int mei_io_cb_alloc_resp_buf(struct mei_cl_cb *cb, size_t length);
328 * mei_io_list_init - Sets up a queue list.
330 * @list: An instance cl callback structure
332 static inline void mei_io_list_init(struct mei_cl_cb *list)
334 INIT_LIST_HEAD(&list->list);
336 void mei_io_list_flush(struct mei_cl_cb *list, struct mei_cl *cl);
339 * MEI ME Client Functions
342 struct mei_cl *mei_cl_allocate(struct mei_device *dev);
343 void mei_cl_init(struct mei_cl *cl, struct mei_device *dev);
344 int mei_cl_flush_queues(struct mei_cl *cl);
346 * mei_cl_cmp_id - tells if file private data have same id
348 * @fe1: private data of 1. file object
349 * @fe2: private data of 2. file object
351 * returns true - if ids are the same and not NULL
353 static inline bool mei_cl_cmp_id(const struct mei_cl *cl1,
354 const struct mei_cl *cl2)
357 (cl1->host_client_id == cl2->host_client_id) &&
358 (cl1->me_client_id == cl2->me_client_id);
364 * MEI Host Client Functions
366 void mei_host_start_message(struct mei_device *dev);
367 void mei_host_enum_clients_message(struct mei_device *dev);
368 int mei_host_client_enumerate(struct mei_device *dev);
369 void mei_host_client_init(struct work_struct *work);
372 * MEI interrupt functions prototype
374 irqreturn_t mei_interrupt_quick_handler(int irq, void *dev_id);
375 irqreturn_t mei_interrupt_thread_handler(int irq, void *dev_id);
376 void mei_timer(struct work_struct *work);
379 * MEI input output function prototype
381 int mei_ioctl_connect_client(struct file *file,
382 struct mei_connect_client_data *data);
384 int mei_start_read(struct mei_device *dev, struct mei_cl *cl);
388 * AMTHIF - AMT Host Interface Functions
390 void mei_amthif_reset_params(struct mei_device *dev);
392 void mei_amthif_host_init(struct mei_device *dev);
394 int mei_amthif_write(struct mei_device *dev, struct mei_cl_cb *priv_cb);
396 int mei_amthif_read(struct mei_device *dev, struct file *file,
397 char __user *ubuf, size_t length, loff_t *offset);
399 unsigned int mei_amthif_poll(struct mei_device *dev,
400 struct file *file, poll_table *wait);
402 int mei_amthif_release(struct mei_device *dev, struct file *file);
404 struct mei_cl_cb *mei_amthif_find_read_list_entry(struct mei_device *dev,
407 void mei_amthif_run_next_cmd(struct mei_device *dev);
410 int mei_amthif_read_message(struct mei_cl_cb *complete_list,
411 struct mei_device *dev, struct mei_msg_hdr *mei_hdr);
413 int mei_amthif_irq_write_complete(struct mei_device *dev, s32 *slots,
414 struct mei_cl_cb *cb, struct mei_cl_cb *cmpl_list);
416 void mei_amthif_complete(struct mei_device *dev, struct mei_cl_cb *cb);
417 int mei_amthif_irq_read_message(struct mei_cl_cb *complete_list,
418 struct mei_device *dev, struct mei_msg_hdr *mei_hdr);
419 int mei_amthif_irq_read(struct mei_device *dev, s32 *slots);
422 * Register Access Function
426 * mei_reg_read - Reads 32bit data from the mei device
428 * @dev: the device structure
429 * @offset: offset from which to read the data
431 * returns register value (u32)
433 static inline u32 mei_reg_read(const struct mei_device *dev,
434 unsigned long offset)
436 return ioread32(dev->mem_addr + offset);
440 * mei_reg_write - Writes 32bit data to the mei device
442 * @dev: the device structure
443 * @offset: offset from which to write the data
444 * @value: register value to write (u32)
446 static inline void mei_reg_write(const struct mei_device *dev,
447 unsigned long offset, u32 value)
449 iowrite32(value, dev->mem_addr + offset);
453 * mei_hcsr_read - Reads 32bit data from the host CSR
455 * @dev: the device structure
457 * returns the byte read.
459 static inline u32 mei_hcsr_read(const struct mei_device *dev)
461 return mei_reg_read(dev, H_CSR);
465 * mei_mecsr_read - Reads 32bit data from the ME CSR
467 * @dev: the device structure
469 * returns ME_CSR_HA register value (u32)
471 static inline u32 mei_mecsr_read(const struct mei_device *dev)
473 return mei_reg_read(dev, ME_CSR_HA);
477 * get_me_cb_rw - Reads 32bit data from the mei ME_CB_RW register
479 * @dev: the device structure
481 * returns ME_CB_RW register value (u32)
483 static inline u32 mei_mecbrw_read(const struct mei_device *dev)
485 return mei_reg_read(dev, ME_CB_RW);
490 * mei interface function prototypes
492 void mei_hcsr_set(struct mei_device *dev);
493 void mei_csr_clear_his(struct mei_device *dev);
495 void mei_enable_interrupts(struct mei_device *dev);
496 void mei_disable_interrupts(struct mei_device *dev);
498 static inline struct mei_msg_hdr *mei_hbm_hdr(u32 *buf, size_t length)
500 struct mei_msg_hdr *hdr = (struct mei_msg_hdr *)buf;
503 hdr->length = length;
504 hdr->msg_complete = 1;