mei: extract AMTHI functions into the amthif.c file
[firefly-linux-kernel-4.4.55.git] / drivers / misc / mei / mei_dev.h
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
17 #ifndef _MEI_DEV_H_
18 #define _MEI_DEV_H_
19
20 #include <linux/types.h>
21 #include <linux/watchdog.h>
22 #include <linux/mei.h>
23 #include "hw.h"
24
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
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
36 #define MEI_WD_STOP_TIMEOUT      10 /* msecs */
37
38 #define MEI_WD_STATE_INDEPENDENCE_MSG_SENT       (1 << 0)
39
40 #define MEI_RD_MSG_BUF_SIZE           (128 * sizeof(u32))
41
42
43 /*
44  * AMTHI Client UUID
45  */
46 extern const uuid_le mei_amthi_guid;
47
48 /*
49  * Watchdog Client UUID
50  */
51 extern const uuid_le mei_wd_guid;
52
53 /*
54  * Watchdog independence state message
55  */
56 extern const u8 mei_wd_state_independence_msg[3][4];
57
58 /*
59  * Number of Maximum MEI Clients
60  */
61 #define MEI_CLIENTS_MAX 256
62
63 /*
64  * Number of File descriptors/handles
65  * that can be opened to the driver.
66  *
67  * Limit to 253: 256 Total Clients
68  * minus internal client for MEI Bus Messags
69  * minus internal client for AMTHI
70  * minus internal client for Watchdog
71  */
72 #define  MEI_MAX_OPEN_HANDLE_COUNT (MEI_CLIENTS_MAX - 3)
73
74
75 /* File state */
76 enum file_state {
77         MEI_FILE_INITIALIZING = 0,
78         MEI_FILE_CONNECTING,
79         MEI_FILE_CONNECTED,
80         MEI_FILE_DISCONNECTING,
81         MEI_FILE_DISCONNECTED
82 };
83
84 /* MEI device states */
85 enum mei_dev_state {
86         MEI_DEV_INITIALIZING = 0,
87         MEI_DEV_INIT_CLIENTS,
88         MEI_DEV_ENABLED,
89         MEI_DEV_RESETING,
90         MEI_DEV_DISABLED,
91         MEI_DEV_RECOVERING_FROM_RESET,
92         MEI_DEV_POWER_DOWN,
93         MEI_DEV_POWER_UP
94 };
95
96 const char *mei_dev_state_str(int state);
97
98 /* init clients states*/
99 enum mei_init_clients_states {
100         MEI_START_MESSAGE = 0,
101         MEI_ENUM_CLIENTS_MESSAGE,
102         MEI_CLIENT_PROPERTIES_MESSAGE
103 };
104
105 enum iamthif_states {
106         MEI_IAMTHIF_IDLE,
107         MEI_IAMTHIF_WRITING,
108         MEI_IAMTHIF_FLOW_CONTROL,
109         MEI_IAMTHIF_READING,
110         MEI_IAMTHIF_READ_COMPLETE
111 };
112
113 enum mei_file_transaction_states {
114         MEI_IDLE,
115         MEI_WRITING,
116         MEI_WRITE_COMPLETE,
117         MEI_FLOW_CONTROL,
118         MEI_READING,
119         MEI_READ_COMPLETE
120 };
121
122 enum mei_wd_states {
123         MEI_WD_IDLE,
124         MEI_WD_RUNNING,
125         MEI_WD_STOPPING,
126 };
127
128 /* MEI CB */
129 enum mei_cb_major_types {
130         MEI_READ = 0,
131         MEI_WRITE,
132         MEI_IOCTL,
133         MEI_OPEN,
134         MEI_CLOSE
135 };
136
137 /*
138  * Intel MEI message data struct
139  */
140 struct mei_message_data {
141         u32 size;
142         unsigned char *data;
143 };
144
145
146 struct mei_cl_cb {
147         struct list_head list;
148         enum mei_cb_major_types major_file_operations;
149         void *file_private;
150         struct mei_message_data request_buffer;
151         struct mei_message_data response_buffer;
152         unsigned long buf_idx;
153         unsigned long read_time;
154         struct file *file_object;
155 };
156
157 /* MEI client instance carried as file->pirvate_data*/
158 struct mei_cl {
159         struct list_head link;
160         struct mei_device *dev;
161         enum file_state state;
162         wait_queue_head_t tx_wait;
163         wait_queue_head_t rx_wait;
164         wait_queue_head_t wait;
165         int read_pending;
166         int status;
167         /* ID of client connected */
168         u8 host_client_id;
169         u8 me_client_id;
170         u8 mei_flow_ctrl_creds;
171         u8 timer_count;
172         enum mei_file_transaction_states reading_state;
173         enum mei_file_transaction_states writing_state;
174         int sm_state;
175         struct mei_cl_cb *read_cb;
176 };
177
178 /**
179  * struct mei_deive -  MEI private device struct
180  * @hbuf_depth - depth of host(write) buffer
181  */
182 struct mei_device {
183         struct pci_dev *pdev;   /* pointer to pci device struct */
184         /*
185          * lists of queues
186          */
187          /* array of pointers to aio lists */
188         struct mei_cl_cb read_list;             /* driver read queue */
189         struct mei_cl_cb write_list;            /* driver write queue */
190         struct mei_cl_cb write_waiting_list;    /* write waiting queue */
191         struct mei_cl_cb ctrl_wr_list;          /* managed write IOCTL list */
192         struct mei_cl_cb ctrl_rd_list;          /* managed read IOCTL list */
193         struct mei_cl_cb amthi_cmd_list;        /* amthi list for cmd waiting */
194
195         /* driver managed amthi list for reading completed amthi cmd data */
196         struct mei_cl_cb amthi_read_complete_list;
197         /*
198          * list of files
199          */
200         struct list_head file_list;
201         long open_handle_count;
202         /*
203          * memory of device
204          */
205         unsigned int mem_base;
206         unsigned int mem_length;
207         void __iomem *mem_addr;
208         /*
209          * lock for the device
210          */
211         struct mutex device_lock; /* device lock */
212         struct delayed_work timer_work; /* MEI timer delayed work (timeouts) */
213         bool recvd_msg;
214         /*
215          * hw states of host and fw(ME)
216          */
217         u32 host_hw_state;
218         u32 me_hw_state;
219         u8  hbuf_depth;
220         /*
221          * waiting queue for receive message from FW
222          */
223         wait_queue_head_t wait_recvd_msg;
224         wait_queue_head_t wait_stop_wd;
225
226         /*
227          * mei device  states
228          */
229         enum mei_dev_state dev_state;
230         enum mei_init_clients_states init_clients_state;
231         u16 init_clients_timer;
232         bool need_reset;
233
234         u32 extra_write_index;
235         unsigned char rd_msg_buf[MEI_RD_MSG_BUF_SIZE];  /* control messages */
236         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
242         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);
245         u8 me_clients_num;
246         u8 me_client_presentation_num;
247         u8 me_client_index;
248         bool mei_host_buffer_is_empty;
249
250         struct mei_cl wd_cl;
251         enum mei_wd_states wd_state;
252         bool wd_interface_reg;
253         bool wd_pending;
254         u16 wd_timeout;
255         unsigned char wd_data[MEI_WD_START_MSG_SIZE];
256
257
258         struct file *iamthif_file_object;
259         struct mei_cl iamthif_cl;
260         struct mei_cl_cb *iamthif_current_cb;
261         int iamthif_mtu;
262         unsigned long iamthif_timer;
263         u32 iamthif_stall_timer;
264         unsigned char *iamthif_msg_buf; /* Note: memory has to be allocated */
265         u32 iamthif_msg_buf_size;
266         u32 iamthif_msg_buf_index;
267         enum iamthif_states iamthif_state;
268         bool iamthif_flow_control_pending;
269         bool iamthif_ioctl;
270         bool iamthif_canceled;
271 };
272
273 static inline unsigned long mei_secs_to_jiffies(unsigned long sec)
274 {
275         return msecs_to_jiffies(sec * MSEC_PER_SEC);
276 }
277
278
279 /*
280  * mei init function prototypes
281  */
282 struct mei_device *mei_device_init(struct pci_dev *pdev);
283 void mei_reset(struct mei_device *dev, int interrupts);
284 int mei_hw_init(struct mei_device *dev);
285 int mei_task_initialize_clients(void *data);
286 int mei_initialize_clients(struct mei_device *dev);
287 int mei_disconnect_host_client(struct mei_device *dev, struct mei_cl *cl);
288 void mei_remove_client_from_file_list(struct mei_device *dev, u8 host_client_id);
289 void mei_allocate_me_clients_storage(struct mei_device *dev);
290
291
292 int mei_me_cl_update_filext(struct mei_device *dev, struct mei_cl *cl,
293                         const uuid_le *cguid, u8 host_client_id);
294 int mei_me_cl_by_uuid(const struct mei_device *dev, const uuid_le *cuuid);
295 int mei_me_cl_by_id(struct mei_device *dev, u8 client_id);
296
297 /*
298  * MEI IO Functions
299  */
300 struct mei_cl_cb *mei_io_cb_init(struct mei_cl *cl, struct file *fp);
301 void mei_io_cb_free(struct mei_cl_cb *priv_cb);
302 int mei_io_cb_alloc_req_buf(struct mei_cl_cb *cb, size_t length);
303 int mei_io_cb_alloc_resp_buf(struct mei_cl_cb *cb, size_t length);
304
305
306 /**
307  * mei_io_list_init - Sets up a queue list.
308  *
309  * @list: An instance cl callback structure
310  */
311 static inline void mei_io_list_init(struct mei_cl_cb *list)
312 {
313         INIT_LIST_HEAD(&list->list);
314 }
315 void mei_io_list_flush(struct mei_cl_cb *list, struct mei_cl *cl);
316
317 /*
318  * MEI ME Client Functions
319  */
320
321 struct mei_cl *mei_cl_allocate(struct mei_device *dev);
322 void mei_cl_init(struct mei_cl *cl, struct mei_device *dev);
323 int mei_cl_flush_queues(struct mei_cl *cl);
324 /**
325  * mei_cl_cmp_id - tells if file private data have same id
326  *
327  * @fe1: private data of 1. file object
328  * @fe2: private data of 2. file object
329  *
330  * returns true  - if ids are the same and not NULL
331  */
332 static inline bool mei_cl_cmp_id(const struct mei_cl *cl1,
333                                 const struct mei_cl *cl2)
334 {
335         return cl1 && cl2 &&
336                 (cl1->host_client_id == cl2->host_client_id) &&
337                 (cl1->me_client_id == cl2->me_client_id);
338 }
339
340
341
342 /*
343  * MEI Host Client Functions
344  */
345 void mei_host_start_message(struct mei_device *dev);
346 void mei_host_enum_clients_message(struct mei_device *dev);
347 int mei_host_client_properties(struct mei_device *dev);
348
349 /*
350  *  MEI interrupt functions prototype
351  */
352 irqreturn_t mei_interrupt_quick_handler(int irq, void *dev_id);
353 irqreturn_t mei_interrupt_thread_handler(int irq, void *dev_id);
354 void mei_timer(struct work_struct *work);
355
356 /*
357  *  MEI input output function prototype
358  */
359 int mei_ioctl_connect_client(struct file *file,
360                         struct mei_connect_client_data *data);
361
362 int mei_start_read(struct mei_device *dev, struct mei_cl *cl);
363
364
365 /*
366  * AMTHIF - AMT Host Interface Functions
367  */
368 void mei_amthif_reset_params(struct mei_device *dev);
369
370 void mei_amthif_host_init(struct mei_device *dev);
371
372 int mei_amthif_write(struct mei_device *dev, struct mei_cl_cb *priv_cb);
373
374 int mei_amthif_read(struct mei_device *dev, struct file *file,
375               char __user *ubuf, size_t length, loff_t *offset);
376
377 struct mei_cl_cb *mei_amthif_find_read_list_entry(struct mei_device *dev,
378                                                 struct file *file);
379
380 void mei_amthif_run_next_cmd(struct mei_device *dev);
381
382
383 int mei_amthif_read_message(struct mei_cl_cb *complete_list,
384                 struct mei_device *dev, struct mei_msg_hdr *mei_hdr);
385
386 int mei_amthif_irq_process_completed(struct mei_device *dev, s32 *slots,
387                         struct mei_cl_cb *cb_pos,
388                         struct mei_cl *cl,
389                         struct mei_cl_cb *cmpl_list);
390
391 void mei_amthif_complete(struct mei_device *dev, struct mei_cl_cb *cb);
392 int mei_amthif_irq_read_message(struct mei_cl_cb *complete_list,
393                 struct mei_device *dev, struct mei_msg_hdr *mei_hdr);
394 int mei_amthif_irq_read(struct mei_device *dev, s32 *slots);
395
396 /*
397  * Register Access Function
398  */
399
400 /**
401  * mei_reg_read - Reads 32bit data from the mei device
402  *
403  * @dev: the device structure
404  * @offset: offset from which to read the data
405  *
406  * returns register value (u32)
407  */
408 static inline u32 mei_reg_read(const struct mei_device *dev,
409                                unsigned long offset)
410 {
411         return ioread32(dev->mem_addr + offset);
412 }
413
414 /**
415  * mei_reg_write - Writes 32bit data to the mei device
416  *
417  * @dev: the device structure
418  * @offset: offset from which to write the data
419  * @value: register value to write (u32)
420  */
421 static inline void mei_reg_write(const struct mei_device *dev,
422                                  unsigned long offset, u32 value)
423 {
424         iowrite32(value, dev->mem_addr + offset);
425 }
426
427 /**
428  * mei_hcsr_read - Reads 32bit data from the host CSR
429  *
430  * @dev: the device structure
431  *
432  * returns the byte read.
433  */
434 static inline u32 mei_hcsr_read(const struct mei_device *dev)
435 {
436         return mei_reg_read(dev, H_CSR);
437 }
438
439 /**
440  * mei_mecsr_read - Reads 32bit data from the ME CSR
441  *
442  * @dev: the device structure
443  *
444  * returns ME_CSR_HA register value (u32)
445  */
446 static inline u32 mei_mecsr_read(const struct mei_device *dev)
447 {
448         return mei_reg_read(dev, ME_CSR_HA);
449 }
450
451 /**
452  * get_me_cb_rw - Reads 32bit data from the mei ME_CB_RW register
453  *
454  * @dev: the device structure
455  *
456  * returns ME_CB_RW register value (u32)
457  */
458 static inline u32 mei_mecbrw_read(const struct mei_device *dev)
459 {
460         return mei_reg_read(dev, ME_CB_RW);
461 }
462
463
464 /*
465  * mei interface function prototypes
466  */
467 void mei_hcsr_set(struct mei_device *dev);
468 void mei_csr_clear_his(struct mei_device *dev);
469
470 void mei_enable_interrupts(struct mei_device *dev);
471 void mei_disable_interrupts(struct mei_device *dev);
472
473 #endif