87c077bae5d8fab852f216df366394b68e627350
[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 "unknown";
42         }
43 #undef MEI_DEV_STATE
44 }
45
46 /**
47  * mei_start - initializes host and fw to start work.
48  *
49  * @dev: the device structure
50  *
51  * returns 0 on success, <0 on failure.
52  */
53 int mei_start(struct mei_device *dev)
54 {
55         mutex_lock(&dev->device_lock);
56
57         /* acknowledge interrupt and stop interupts */
58         mei_clear_interrupts(dev);
59
60         mei_hw_config(dev);
61
62         dev_dbg(&dev->pdev->dev, "reset in start the mei device.\n");
63
64         mei_reset(dev, 1);
65
66         if (mei_hbm_start_wait(dev)) {
67                 dev_err(&dev->pdev->dev, "HBM haven't started");
68                 goto err;
69         }
70
71         if (!mei_host_is_ready(dev)) {
72                 dev_err(&dev->pdev->dev, "host is not ready.\n");
73                 goto err;
74         }
75
76         if (!mei_hw_is_ready(dev)) {
77                 dev_err(&dev->pdev->dev, "ME is not ready.\n");
78                 goto err;
79         }
80
81         if (!mei_hbm_version_is_supported(dev)) {
82                 dev_dbg(&dev->pdev->dev, "MEI start failed.\n");
83                 goto err;
84         }
85
86         dev_dbg(&dev->pdev->dev, "link layer has been established.\n");
87
88         mutex_unlock(&dev->device_lock);
89         return 0;
90 err:
91         dev_err(&dev->pdev->dev, "link layer initialization failed.\n");
92         dev->dev_state = MEI_DEV_DISABLED;
93         mutex_unlock(&dev->device_lock);
94         return -ENODEV;
95 }
96 EXPORT_SYMBOL_GPL(mei_start);
97
98 /**
99  * mei_cancel_work. Cancel mei background jobs
100  *
101  * @dev: the device structure
102  */
103 void mei_cancel_work(struct mei_device *dev)
104 {
105         cancel_work_sync(&dev->init_work);
106         cancel_work_sync(&dev->reset_work);
107
108         cancel_delayed_work(&dev->timer_work);
109 }
110 EXPORT_SYMBOL_GPL(mei_cancel_work);
111
112 /**
113  * mei_reset - resets host and fw.
114  *
115  * @dev: the device structure
116  * @interrupts_enabled: if interrupt should be enabled after reset.
117  */
118 void mei_reset(struct mei_device *dev, int interrupts_enabled)
119 {
120         bool unexpected;
121         int ret;
122
123         unexpected = (dev->dev_state != MEI_DEV_INITIALIZING &&
124                         dev->dev_state != MEI_DEV_DISABLED &&
125                         dev->dev_state != MEI_DEV_POWER_DOWN &&
126                         dev->dev_state != MEI_DEV_POWER_UP);
127
128         if (unexpected)
129                 dev_warn(&dev->pdev->dev, "unexpected reset: dev_state = %s\n",
130                          mei_dev_state_str(dev->dev_state));
131
132         ret = mei_hw_reset(dev, interrupts_enabled);
133         if (ret) {
134                 dev_err(&dev->pdev->dev, "hw reset failed disabling the device\n");
135                 interrupts_enabled = false;
136                 dev->dev_state = MEI_DEV_DISABLED;
137         }
138
139         dev->hbm_state = MEI_HBM_IDLE;
140
141         if (dev->dev_state != MEI_DEV_INITIALIZING &&
142             dev->dev_state != MEI_DEV_POWER_UP) {
143                 if (dev->dev_state != MEI_DEV_DISABLED &&
144                     dev->dev_state != MEI_DEV_POWER_DOWN)
145                         dev->dev_state = MEI_DEV_RESETTING;
146
147                 /* remove all waiting requests */
148                 mei_cl_all_write_clear(dev);
149
150                 mei_cl_all_disconnect(dev);
151
152                 /* wake up all readings so they can be interrupted */
153                 mei_cl_all_wakeup(dev);
154
155                 /* remove entry if already in list */
156                 dev_dbg(&dev->pdev->dev, "remove iamthif and wd from the file list.\n");
157                 mei_cl_unlink(&dev->wd_cl);
158                 mei_cl_unlink(&dev->iamthif_cl);
159                 mei_amthif_reset_params(dev);
160                 memset(&dev->wr_ext_msg, 0, sizeof(dev->wr_ext_msg));
161         }
162
163         /* we're already in reset, cancel the init timer */
164         dev->init_clients_timer = 0;
165
166         dev->me_clients_num = 0;
167         dev->rd_msg_hdr = 0;
168         dev->wd_pending = false;
169
170         if (!interrupts_enabled) {
171                 dev_dbg(&dev->pdev->dev, "intr not enabled end of reset\n");
172                 return;
173         }
174
175         ret = mei_hw_start(dev);
176         if (ret) {
177                 dev_err(&dev->pdev->dev, "hw_start failed disabling the device\n");
178                 dev->dev_state = MEI_DEV_DISABLED;
179                 return;
180         }
181
182         dev_dbg(&dev->pdev->dev, "link is established start sending messages.\n");
183         /* link is established * start sending messages.  */
184
185         dev->dev_state = MEI_DEV_INIT_CLIENTS;
186
187         ret = mei_hbm_start_req(dev);
188         if (ret) {
189                 dev_err(&dev->pdev->dev, "hbm_start failed disabling the device\n");
190                 dev->dev_state = MEI_DEV_DISABLED;
191                 return;
192         }
193 }
194 EXPORT_SYMBOL_GPL(mei_reset);
195
196 static void mei_reset_work(struct work_struct *work)
197 {
198         struct mei_device *dev =
199                 container_of(work, struct mei_device,  reset_work);
200
201         mutex_lock(&dev->device_lock);
202
203         mei_reset(dev, true);
204
205         mutex_unlock(&dev->device_lock);
206 }
207
208 void mei_stop(struct mei_device *dev)
209 {
210         dev_dbg(&dev->pdev->dev, "stopping the device.\n");
211
212         mei_cancel_work(dev);
213
214         mei_nfc_host_exit(dev);
215
216         mutex_lock(&dev->device_lock);
217
218         mei_wd_stop(dev);
219
220         dev->dev_state = MEI_DEV_POWER_DOWN;
221         mei_reset(dev, 0);
222
223         mutex_unlock(&dev->device_lock);
224
225         mei_watchdog_unregister(dev);
226 }
227 EXPORT_SYMBOL_GPL(mei_stop);
228
229
230
231 void mei_device_init(struct mei_device *dev)
232 {
233         /* setup our list array */
234         INIT_LIST_HEAD(&dev->file_list);
235         INIT_LIST_HEAD(&dev->device_list);
236         mutex_init(&dev->device_lock);
237         init_waitqueue_head(&dev->wait_hw_ready);
238         init_waitqueue_head(&dev->wait_recvd_msg);
239         init_waitqueue_head(&dev->wait_stop_wd);
240         dev->dev_state = MEI_DEV_INITIALIZING;
241
242         mei_io_list_init(&dev->read_list);
243         mei_io_list_init(&dev->write_list);
244         mei_io_list_init(&dev->write_waiting_list);
245         mei_io_list_init(&dev->ctrl_wr_list);
246         mei_io_list_init(&dev->ctrl_rd_list);
247
248         INIT_DELAYED_WORK(&dev->timer_work, mei_timer);
249         INIT_WORK(&dev->init_work, mei_host_client_init);
250         INIT_WORK(&dev->reset_work, mei_reset_work);
251
252         INIT_LIST_HEAD(&dev->wd_cl.link);
253         INIT_LIST_HEAD(&dev->iamthif_cl.link);
254         mei_io_list_init(&dev->amthif_cmd_list);
255         mei_io_list_init(&dev->amthif_rd_complete_list);
256
257         bitmap_zero(dev->host_clients_map, MEI_CLIENTS_MAX);
258         dev->open_handle_count = 0;
259
260         /*
261          * Reserving the first client ID
262          * 0: Reserved for MEI Bus Message communications
263          */
264         bitmap_set(dev->host_clients_map, 0, 1);
265 }
266 EXPORT_SYMBOL_GPL(mei_device_init);
267