blob: 64a4f17893e503f9bd917ffd7ca146c1dbfb9b09 [file] [log] [blame]
Oren Weilab841162011-05-15 13:43:41 +03001/*
2 *
3 * Intel Management Engine Interface (Intel MEI) Linux driver
Tomas Winkler733ba91c2012-02-09 19:25:53 +02004 * Copyright (c) 2003-2012, Intel Corporation.
Oren Weilab841162011-05-15 13:43:41 +03005 *
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
17#ifndef _MEI_DEV_H_
18#define _MEI_DEV_H_
19
20#include <linux/types.h>
Oren Weil9ce178e2011-09-07 09:03:09 +030021#include <linux/watchdog.h>
Tomas Winkler4f3afe12012-05-09 16:38:59 +030022#include <linux/mei.h>
Oren Weilab841162011-05-15 13:43:41 +030023#include "hw.h"
24
25/*
Oren Weilab841162011-05-15 13:43:41 +030026 * watch dog definition
27 */
Tomas Winkler248ffdf2012-08-16 19:39:42 +030028#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
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
Oren Weilab841162011-05-15 13:43:41 +030036#define MEI_WD_STATE_INDEPENDENCE_MSG_SENT (1 << 0)
37
Tomas Winkleredf1eed2012-02-09 19:25:54 +020038#define MEI_RD_MSG_BUF_SIZE (128 * sizeof(u32))
39
Oren Weilab841162011-05-15 13:43:41 +030040/*
Oren Weil4a3cafd2011-09-07 09:03:10 +030041 * MEI PCI Device object
42 */
43extern struct pci_dev *mei_device;
44
Oren Weil9ce178e2011-09-07 09:03:09 +030045
46/*
Oren Weilab841162011-05-15 13:43:41 +030047 * AMTHI Client UUID
48 */
49extern const uuid_le mei_amthi_guid;
50
51/*
52 * Watchdog Client UUID
53 */
54extern const uuid_le mei_wd_guid;
55
56/*
57 * Watchdog independence state message
58 */
59extern const u8 mei_wd_state_independence_msg[3][4];
60
61/*
62 * Number of File descriptors/handles
63 * that can be opened to the driver.
64 *
65 * Limit to 253: 255 Total Clients
66 * minus internal client for AMTHI
67 * minus internal client for Watchdog
68 */
69#define MEI_MAX_OPEN_HANDLE_COUNT 253
70
71/*
Oren Weilab841162011-05-15 13:43:41 +030072 * Number of Maximum MEI Clients
73 */
74#define MEI_CLIENTS_MAX 255
75
76/* File state */
77enum file_state {
78 MEI_FILE_INITIALIZING = 0,
79 MEI_FILE_CONNECTING,
80 MEI_FILE_CONNECTED,
81 MEI_FILE_DISCONNECTING,
82 MEI_FILE_DISCONNECTED
83};
84
85/* MEI device states */
Tomas Winklerb210d752012-08-07 00:03:56 +030086enum mei_dev_state {
87 MEI_DEV_INITIALIZING = 0,
88 MEI_DEV_INIT_CLIENTS,
89 MEI_DEV_ENABLED,
90 MEI_DEV_RESETING,
91 MEI_DEV_DISABLED,
92 MEI_DEV_RECOVERING_FROM_RESET,
93 MEI_DEV_POWER_DOWN,
94 MEI_DEV_POWER_UP
Oren Weilab841162011-05-15 13:43:41 +030095};
96
Tomas Winklerb210d752012-08-07 00:03:56 +030097const char *mei_dev_state_str(int state);
98
Justin P. Mattock5f9092f32012-03-12 07:18:09 -070099/* init clients states*/
Oren Weilab841162011-05-15 13:43:41 +0300100enum mei_init_clients_states {
101 MEI_START_MESSAGE = 0,
102 MEI_ENUM_CLIENTS_MESSAGE,
103 MEI_CLIENT_PROPERTIES_MESSAGE
104};
105
106enum iamthif_states {
107 MEI_IAMTHIF_IDLE,
108 MEI_IAMTHIF_WRITING,
109 MEI_IAMTHIF_FLOW_CONTROL,
110 MEI_IAMTHIF_READING,
111 MEI_IAMTHIF_READ_COMPLETE
112};
113
114enum mei_file_transaction_states {
115 MEI_IDLE,
116 MEI_WRITING,
117 MEI_WRITE_COMPLETE,
118 MEI_FLOW_CONTROL,
119 MEI_READING,
120 MEI_READ_COMPLETE
121};
122
123/* MEI CB */
124enum mei_cb_major_types {
125 MEI_READ = 0,
126 MEI_WRITE,
127 MEI_IOCTL,
128 MEI_OPEN,
129 MEI_CLOSE
130};
131
132/*
133 * Intel MEI message data struct
134 */
135struct mei_message_data {
136 u32 size;
Tomas Winkleredf1eed2012-02-09 19:25:54 +0200137 unsigned char *data;
Oren Weilab841162011-05-15 13:43:41 +0300138} __packed;
139
140
141struct mei_cl_cb {
142 struct list_head cb_list;
143 enum mei_cb_major_types major_file_operations;
144 void *file_private;
145 struct mei_message_data request_buffer;
146 struct mei_message_data response_buffer;
147 unsigned long information;
148 unsigned long read_time;
149 struct file *file_object;
150};
151
152/* MEI client instance carried as file->pirvate_data*/
153struct 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
173struct mei_io_list {
174 struct mei_cl_cb mei_cb;
Oren Weilab841162011-05-15 13:43:41 +0300175};
176
Tomas Winkler24aadc82012-06-25 23:46:27 +0300177/**
178 * struct mei_deive - MEI private device struct
179 * @hbuf_depth - depth of host(write) buffer
180 */
Oren Weilab841162011-05-15 13:43:41 +0300181struct mei_device {
182 struct pci_dev *pdev; /* pointer to pci device struct */
183 /*
184 * lists of queues
185 */
186 /* array of pointers to aio lists */
Oren Weilab841162011-05-15 13:43:41 +0300187 struct mei_io_list read_list; /* driver read queue */
188 struct mei_io_list write_list; /* driver write queue */
189 struct mei_io_list write_waiting_list; /* write waiting queue */
190 struct mei_io_list ctrl_wr_list; /* managed write IOCTL list */
191 struct mei_io_list ctrl_rd_list; /* managed read IOCTL list */
192 struct mei_io_list amthi_cmd_list; /* amthi list for cmd waiting */
193
194 /* driver managed amthi list for reading completed amthi cmd data */
195 struct mei_io_list amthi_read_complete_list;
196 /*
197 * list of files
198 */
199 struct list_head file_list;
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300200 long open_handle_count;
Oren Weilab841162011-05-15 13:43:41 +0300201 /*
202 * memory of device
203 */
204 unsigned int mem_base;
205 unsigned int mem_length;
206 void __iomem *mem_addr;
207 /*
208 * lock for the device
209 */
210 struct mutex device_lock; /* device lock */
Oren Weila61c6532011-09-07 09:03:13 +0300211 struct delayed_work timer_work; /* MEI timer delayed work (timeouts) */
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300212 bool recvd_msg;
Oren Weilab841162011-05-15 13:43:41 +0300213 /*
214 * hw states of host and fw(ME)
215 */
216 u32 host_hw_state;
217 u32 me_hw_state;
Tomas Winkler24aadc82012-06-25 23:46:27 +0300218 u8 hbuf_depth;
Oren Weilab841162011-05-15 13:43:41 +0300219 /*
220 * waiting queue for receive message from FW
221 */
222 wait_queue_head_t wait_recvd_msg;
223 wait_queue_head_t wait_stop_wd;
224
225 /*
226 * mei device states
227 */
Tomas Winklerb210d752012-08-07 00:03:56 +0300228 enum mei_dev_state dev_state;
Oren Weilab841162011-05-15 13:43:41 +0300229 enum mei_init_clients_states init_clients_state;
230 u16 init_clients_timer;
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300231 bool stop;
232 bool need_reset;
Oren Weilab841162011-05-15 13:43:41 +0300233
234 u32 extra_write_index;
Tomas Winkleredf1eed2012-02-09 19:25:54 +0200235 unsigned char rd_msg_buf[MEI_RD_MSG_BUF_SIZE]; /* control messages */
Oren Weilab841162011-05-15 13:43:41 +0300236 u32 wr_msg_buf[128]; /* used for control messages */
237 u32 ext_msg_buf[8]; /* for control responses */
238 u32 rd_msg_hdr;
239
240 struct hbm_version version;
241
Oren Weilab841162011-05-15 13:43:41 +0300242 struct mei_me_client *me_clients; /* Note: memory has to be allocated */
243 DECLARE_BITMAP(me_clients_map, MEI_CLIENTS_MAX);
244 DECLARE_BITMAP(host_clients_map, MEI_CLIENTS_MAX);
Tomas Winklercf9673d2011-06-06 10:44:33 +0300245 u8 me_clients_num;
Oren Weilab841162011-05-15 13:43:41 +0300246 u8 me_client_presentation_num;
247 u8 me_client_index;
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300248 bool mei_host_buffer_is_empty;
Oren Weilab841162011-05-15 13:43:41 +0300249
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300250 struct mei_cl wd_cl;
Tomas Winkler21c66d12012-06-19 09:13:37 +0300251 bool wd_interface_reg;
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300252 bool wd_pending;
253 bool wd_stopped;
254 bool wd_bypass; /* if false, don't refresh watchdog ME client */
Oren Weilab841162011-05-15 13:43:41 +0300255 u16 wd_timeout; /* seconds ((wd_data[1] << 8) + wd_data[0]) */
Tomas Winkler248ffdf2012-08-16 19:39:42 +0300256 unsigned char wd_data[MEI_WD_START_MSG_SIZE];
Oren Weilab841162011-05-15 13:43:41 +0300257
258
Oren Weilab841162011-05-15 13:43:41 +0300259 struct file *iamthif_file_object;
260 struct mei_cl iamthif_cl;
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300261 struct mei_cl_cb *iamthif_current_cb;
Oren Weilab841162011-05-15 13:43:41 +0300262 int iamthif_mtu;
263 unsigned long iamthif_timer;
264 u32 iamthif_stall_timer;
265 unsigned char *iamthif_msg_buf; /* Note: memory has to be allocated */
266 u32 iamthif_msg_buf_size;
267 u32 iamthif_msg_buf_index;
Oren Weilab841162011-05-15 13:43:41 +0300268 enum iamthif_states iamthif_state;
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300269 bool iamthif_flow_control_pending;
270 bool iamthif_ioctl;
271 bool iamthif_canceled;
Oren Weilab841162011-05-15 13:43:41 +0300272};
273
274
275/*
276 * mei init function prototypes
277 */
Tomas Winklerc95efb72011-05-25 17:28:21 +0300278struct mei_device *mei_device_init(struct pci_dev *pdev);
Oren Weilab841162011-05-15 13:43:41 +0300279void mei_reset(struct mei_device *dev, int interrupts);
280int mei_hw_init(struct mei_device *dev);
281int mei_task_initialize_clients(void *data);
282int mei_initialize_clients(struct mei_device *dev);
Oren Weilab841162011-05-15 13:43:41 +0300283int mei_disconnect_host_client(struct mei_device *dev, struct mei_cl *cl);
Tomas Winkler106fb492011-05-25 17:28:20 +0300284void mei_remove_client_from_file_list(struct mei_device *dev, u8 host_client_id);
Tomas Winklerc95efb72011-05-25 17:28:21 +0300285void mei_host_init_iamthif(struct mei_device *dev);
286void mei_allocate_me_clients_storage(struct mei_device *dev);
Oren Weilab841162011-05-15 13:43:41 +0300287
Oren Weilab841162011-05-15 13:43:41 +0300288
Tomas Winkler07b509b2012-07-23 14:05:39 +0300289int mei_me_cl_update_filext(struct mei_device *dev, struct mei_cl *cl,
290 const uuid_le *cguid, u8 host_client_id);
291int mei_me_cl_by_uuid(const struct mei_device *dev, const uuid_le *cuuid);
292int mei_me_cl_by_id(struct mei_device *dev, u8 client_id);
Oren Weilab841162011-05-15 13:43:41 +0300293
294/*
Tomas Winkler0288c7c2011-06-06 10:44:34 +0300295 * MEI IO List Functions
296 */
297void mei_io_list_init(struct mei_io_list *list);
298void mei_io_list_flush(struct mei_io_list *list, struct mei_cl *cl);
299
300/*
Tomas Winklerc95efb72011-05-25 17:28:21 +0300301 * MEI ME Client Functions
302 */
303
304struct mei_cl *mei_cl_allocate(struct mei_device *dev);
Tomas Winkler0288c7c2011-06-06 10:44:34 +0300305void mei_cl_init(struct mei_cl *cl, struct mei_device *dev);
306int mei_cl_flush_queues(struct mei_cl *cl);
307/**
308 * mei_cl_cmp_id - tells if file private data have same id
309 *
310 * @fe1: private data of 1. file object
311 * @fe2: private data of 2. file object
312 *
313 * returns true - if ids are the same and not NULL
314 */
315static inline bool mei_cl_cmp_id(const struct mei_cl *cl1,
316 const struct mei_cl *cl2)
317{
318 return cl1 && cl2 &&
319 (cl1->host_client_id == cl2->host_client_id) &&
320 (cl1->me_client_id == cl2->me_client_id);
321}
322
Tomas Winklerc95efb72011-05-25 17:28:21 +0300323
324
325/*
326 * MEI Host Client Functions
327 */
328void mei_host_start_message(struct mei_device *dev);
329void mei_host_enum_clients_message(struct mei_device *dev);
Oren Weilabc51b62011-09-21 16:45:30 +0300330int mei_host_client_properties(struct mei_device *dev);
Tomas Winklerc95efb72011-05-25 17:28:21 +0300331
332/*
333 * MEI interrupt functions prototype
Oren Weilab841162011-05-15 13:43:41 +0300334 */
335irqreturn_t mei_interrupt_quick_handler(int irq, void *dev_id);
Tomas Winklerc95efb72011-05-25 17:28:21 +0300336irqreturn_t mei_interrupt_thread_handler(int irq, void *dev_id);
Oren Weila61c6532011-09-07 09:03:13 +0300337void mei_timer(struct work_struct *work);
Oren Weilab841162011-05-15 13:43:41 +0300338
339/*
Tomas Winklerc95efb72011-05-25 17:28:21 +0300340 * MEI input output function prototype
Oren Weilab841162011-05-15 13:43:41 +0300341 */
342int mei_ioctl_connect_client(struct file *file,
343 struct mei_connect_client_data *data);
344
345int mei_start_read(struct mei_device *dev, struct mei_cl *cl);
346
347int amthi_write(struct mei_device *dev, struct mei_cl_cb *priv_cb);
348
349int amthi_read(struct mei_device *dev, struct file *file,
350 char __user *ubuf, size_t length, loff_t *offset);
351
352struct mei_cl_cb *find_amthi_read_list_entry(struct mei_device *dev,
353 struct file *file);
354
Tomas Winklerc95efb72011-05-25 17:28:21 +0300355void mei_run_next_iamthif_cmd(struct mei_device *dev);
Oren Weilab841162011-05-15 13:43:41 +0300356
357void mei_free_cb_private(struct mei_cl_cb *priv_cb);
358
Oren Weilab841162011-05-15 13:43:41 +0300359
360/*
361 * Register Access Function
362 */
363
364/**
365 * mei_reg_read - Reads 32bit data from the mei device
366 *
367 * @dev: the device structure
368 * @offset: offset from which to read the data
369 *
Oren Weil92eb4012011-09-07 09:03:08 +0300370 * returns register value (u32)
Oren Weilab841162011-05-15 13:43:41 +0300371 */
Tomas Winkler4a0d6a72012-07-04 19:24:49 +0300372static inline u32 mei_reg_read(const struct mei_device *dev,
373 unsigned long offset)
Oren Weilab841162011-05-15 13:43:41 +0300374{
375 return ioread32(dev->mem_addr + offset);
376}
377
378/**
379 * mei_reg_write - Writes 32bit data to the mei device
380 *
381 * @dev: the device structure
382 * @offset: offset from which to write the data
Oren Weil92eb4012011-09-07 09:03:08 +0300383 * @value: register value to write (u32)
Oren Weilab841162011-05-15 13:43:41 +0300384 */
Tomas Winkler4a0d6a72012-07-04 19:24:49 +0300385static inline void mei_reg_write(const struct mei_device *dev,
386 unsigned long offset, u32 value)
Oren Weilab841162011-05-15 13:43:41 +0300387{
388 iowrite32(value, dev->mem_addr + offset);
389}
390
391/**
392 * mei_hcsr_read - Reads 32bit data from the host CSR
393 *
394 * @dev: the device structure
395 *
396 * returns the byte read.
397 */
Tomas Winkler4a0d6a72012-07-04 19:24:49 +0300398static inline u32 mei_hcsr_read(const struct mei_device *dev)
Oren Weilab841162011-05-15 13:43:41 +0300399{
400 return mei_reg_read(dev, H_CSR);
401}
402
403/**
404 * mei_mecsr_read - Reads 32bit data from the ME CSR
405 *
406 * @dev: the device structure
407 *
408 * returns ME_CSR_HA register value (u32)
409 */
Tomas Winkler4a0d6a72012-07-04 19:24:49 +0300410static inline u32 mei_mecsr_read(const struct mei_device *dev)
Oren Weilab841162011-05-15 13:43:41 +0300411{
412 return mei_reg_read(dev, ME_CSR_HA);
413}
414
415/**
416 * get_me_cb_rw - Reads 32bit data from the mei ME_CB_RW register
417 *
418 * @dev: the device structure
419 *
420 * returns ME_CB_RW register value (u32)
421 */
Tomas Winkler4a0d6a72012-07-04 19:24:49 +0300422static inline u32 mei_mecbrw_read(const struct mei_device *dev)
Oren Weilab841162011-05-15 13:43:41 +0300423{
424 return mei_reg_read(dev, ME_CB_RW);
425}
426
427
428/*
429 * mei interface function prototypes
430 */
431void mei_hcsr_set(struct mei_device *dev);
432void mei_csr_clear_his(struct mei_device *dev);
433
434void mei_enable_interrupts(struct mei_device *dev);
435void mei_disable_interrupts(struct mei_device *dev);
436
Oren Weilab841162011-05-15 13:43:41 +0300437#endif