Merge remote-tracking branch 'lsk/v3.10/topic/gator' into linux-linaro-lsk
[firefly-linux-kernel-4.4.55.git] / drivers / misc / mei / init.c
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 #include <linux/export.h>
18 #include <linux/pci.h>
19 #include <linux/sched.h>
20 #include <linux/wait.h>
21 #include <linux/delay.h>
22
23 #include <linux/mei.h>
24
25 #include "mei_dev.h"
26 #include "hbm.h"
27 #include "client.h"
28
29 const char *mei_dev_state_str(int state)
30 {
31 #define MEI_DEV_STATE(state) case MEI_DEV_##state: return #state
32         switch (state) {
33         MEI_DEV_STATE(INITIALIZING);
34         MEI_DEV_STATE(INIT_CLIENTS);
35         MEI_DEV_STATE(ENABLED);
36         MEI_DEV_STATE(RESETTING);
37         MEI_DEV_STATE(DISABLED);
38         MEI_DEV_STATE(POWER_DOWN);
39         MEI_DEV_STATE(POWER_UP);
40         default:
41                 return "unkown";
42         }
43 #undef MEI_DEV_STATE
44 }
45
46 void mei_device_init(struct mei_device *dev)
47 {
48         /* setup our list array */
49         INIT_LIST_HEAD(&dev->file_list);
50         INIT_LIST_HEAD(&dev->device_list);
51         mutex_init(&dev->device_lock);
52         init_waitqueue_head(&dev->wait_hw_ready);
53         init_waitqueue_head(&dev->wait_recvd_msg);
54         init_waitqueue_head(&dev->wait_stop_wd);
55         dev->dev_state = MEI_DEV_INITIALIZING;
56
57         mei_io_list_init(&dev->read_list);
58         mei_io_list_init(&dev->write_list);
59         mei_io_list_init(&dev->write_waiting_list);
60         mei_io_list_init(&dev->ctrl_wr_list);
61         mei_io_list_init(&dev->ctrl_rd_list);
62
63         INIT_DELAYED_WORK(&dev->timer_work, mei_timer);
64         INIT_WORK(&dev->init_work, mei_host_client_init);
65
66         INIT_LIST_HEAD(&dev->wd_cl.link);
67         INIT_LIST_HEAD(&dev->iamthif_cl.link);
68         mei_io_list_init(&dev->amthif_cmd_list);
69         mei_io_list_init(&dev->amthif_rd_complete_list);
70
71 }
72 EXPORT_SYMBOL_GPL(mei_device_init);
73
74 /**
75  * mei_start - initializes host and fw to start work.
76  *
77  * @dev: the device structure
78  *
79  * returns 0 on success, <0 on failure.
80  */
81 int mei_start(struct mei_device *dev)
82 {
83         mutex_lock(&dev->device_lock);
84
85         /* acknowledge interrupt and stop interupts */
86         mei_clear_interrupts(dev);
87
88         mei_hw_config(dev);
89
90         dev_dbg(&dev->pdev->dev, "reset in start the mei device.\n");
91
92         mei_reset(dev, 1);
93
94         if (mei_hbm_start_wait(dev)) {
95                 dev_err(&dev->pdev->dev, "HBM haven't started");
96                 goto err;
97         }
98
99         if (!mei_host_is_ready(dev)) {
100                 dev_err(&dev->pdev->dev, "host is not ready.\n");
101                 goto err;
102         }
103
104         if (!mei_hw_is_ready(dev)) {
105                 dev_err(&dev->pdev->dev, "ME is not ready.\n");
106                 goto err;
107         }
108
109         if (dev->version.major_version != HBM_MAJOR_VERSION ||
110             dev->version.minor_version != HBM_MINOR_VERSION) {
111                 dev_dbg(&dev->pdev->dev, "MEI start failed.\n");
112                 goto err;
113         }
114
115         dev_dbg(&dev->pdev->dev, "link layer has been established.\n");
116
117         mutex_unlock(&dev->device_lock);
118         return 0;
119 err:
120         dev_err(&dev->pdev->dev, "link layer initialization failed.\n");
121         dev->dev_state = MEI_DEV_DISABLED;
122         mutex_unlock(&dev->device_lock);
123         return -ENODEV;
124 }
125 EXPORT_SYMBOL_GPL(mei_start);
126
127 /**
128  * mei_reset - resets host and fw.
129  *
130  * @dev: the device structure
131  * @interrupts_enabled: if interrupt should be enabled after reset.
132  */
133 void mei_reset(struct mei_device *dev, int interrupts_enabled)
134 {
135         bool unexpected;
136
137         unexpected = (dev->dev_state != MEI_DEV_INITIALIZING &&
138                         dev->dev_state != MEI_DEV_DISABLED &&
139                         dev->dev_state != MEI_DEV_POWER_DOWN &&
140                         dev->dev_state != MEI_DEV_POWER_UP);
141
142         mei_hw_reset(dev, interrupts_enabled);
143
144         dev->hbm_state = MEI_HBM_IDLE;
145
146         if (dev->dev_state != MEI_DEV_INITIALIZING &&
147             dev->dev_state != MEI_DEV_POWER_UP) {
148                 if (dev->dev_state != MEI_DEV_DISABLED &&
149                     dev->dev_state != MEI_DEV_POWER_DOWN)
150                         dev->dev_state = MEI_DEV_RESETTING;
151
152                 mei_cl_all_disconnect(dev);
153
154                 /* remove entry if already in list */
155                 dev_dbg(&dev->pdev->dev, "remove iamthif and wd from the file list.\n");
156                 mei_cl_unlink(&dev->wd_cl);
157                 if (dev->open_handle_count > 0)
158                         dev->open_handle_count--;
159                 mei_cl_unlink(&dev->iamthif_cl);
160                 if (dev->open_handle_count > 0)
161                         dev->open_handle_count--;
162
163                 mei_amthif_reset_params(dev);
164                 memset(&dev->wr_ext_msg, 0, sizeof(dev->wr_ext_msg));
165         }
166
167         /* we're already in reset, cancel the init timer */
168         dev->init_clients_timer = 0;
169
170         dev->me_clients_num = 0;
171         dev->rd_msg_hdr = 0;
172         dev->wd_pending = false;
173
174         if (unexpected)
175                 dev_warn(&dev->pdev->dev, "unexpected reset: dev_state = %s\n",
176                          mei_dev_state_str(dev->dev_state));
177
178         if (!interrupts_enabled) {
179                 dev_dbg(&dev->pdev->dev, "intr not enabled end of reset\n");
180                 return;
181         }
182
183         mei_hw_start(dev);
184
185         dev_dbg(&dev->pdev->dev, "link is established start sending messages.\n");
186         /* link is established * start sending messages.  */
187
188         dev->dev_state = MEI_DEV_INIT_CLIENTS;
189
190         mei_hbm_start_req(dev);
191
192         /* wake up all readings so they can be interrupted */
193         mei_cl_all_read_wakeup(dev);
194
195         /* remove all waiting requests */
196         mei_cl_all_write_clear(dev);
197 }
198 EXPORT_SYMBOL_GPL(mei_reset);
199
200 void mei_stop(struct mei_device *dev)
201 {
202         dev_dbg(&dev->pdev->dev, "stopping the device.\n");
203
204         flush_scheduled_work();
205
206         mutex_lock(&dev->device_lock);
207
208         cancel_delayed_work(&dev->timer_work);
209
210         mei_wd_stop(dev);
211
212         mei_nfc_host_exit();
213
214         dev->dev_state = MEI_DEV_POWER_DOWN;
215         mei_reset(dev, 0);
216
217         mutex_unlock(&dev->device_lock);
218
219         mei_watchdog_unregister(dev);
220 }
221 EXPORT_SYMBOL_GPL(mei_stop);
222
223
224