mei: push all standard settings into mei_device_init
[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 const char *mei_pg_state_str(enum mei_pg_state state)
47 {
48 #define MEI_PG_STATE(state) case MEI_PG_##state: return #state
49         switch (state) {
50         MEI_PG_STATE(OFF);
51         MEI_PG_STATE(ON);
52         default:
53                 return "unknown";
54         }
55 #undef MEI_PG_STATE
56 }
57
58
59 /**
60  * mei_cancel_work. Cancel mei background jobs
61  *
62  * @dev: the device structure
63  *
64  * returns 0 on success or < 0 if the reset hasn't succeeded
65  */
66 void mei_cancel_work(struct mei_device *dev)
67 {
68         cancel_work_sync(&dev->init_work);
69         cancel_work_sync(&dev->reset_work);
70
71         cancel_delayed_work(&dev->timer_work);
72 }
73 EXPORT_SYMBOL_GPL(mei_cancel_work);
74
75 /**
76  * mei_reset - resets host and fw.
77  *
78  * @dev: the device structure
79  */
80 int mei_reset(struct mei_device *dev)
81 {
82         enum mei_dev_state state = dev->dev_state;
83         bool interrupts_enabled;
84         int ret;
85
86         if (state != MEI_DEV_INITIALIZING &&
87             state != MEI_DEV_DISABLED &&
88             state != MEI_DEV_POWER_DOWN &&
89             state != MEI_DEV_POWER_UP) {
90                 struct mei_fw_status fw_status;
91
92                 mei_fw_status(dev, &fw_status);
93                 dev_warn(&dev->pdev->dev,
94                         "unexpected reset: dev_state = %s " FW_STS_FMT "\n",
95                         mei_dev_state_str(state), FW_STS_PRM(fw_status));
96         }
97
98         /* we're already in reset, cancel the init timer
99          * if the reset was called due the hbm protocol error
100          * we need to call it before hw start
101          * so the hbm watchdog won't kick in
102          */
103         mei_hbm_idle(dev);
104
105         /* enter reset flow */
106         interrupts_enabled = state != MEI_DEV_POWER_DOWN;
107         dev->dev_state = MEI_DEV_RESETTING;
108
109         dev->reset_count++;
110         if (dev->reset_count > MEI_MAX_CONSEC_RESET) {
111                 dev_err(&dev->pdev->dev, "reset: reached maximal consecutive resets: disabling the device\n");
112                 dev->dev_state = MEI_DEV_DISABLED;
113                 return -ENODEV;
114         }
115
116         ret = mei_hw_reset(dev, interrupts_enabled);
117         /* fall through and remove the sw state even if hw reset has failed */
118
119         /* no need to clean up software state in case of power up */
120         if (state != MEI_DEV_INITIALIZING &&
121             state != MEI_DEV_POWER_UP) {
122
123                 /* remove all waiting requests */
124                 mei_cl_all_write_clear(dev);
125
126                 mei_cl_all_disconnect(dev);
127
128                 /* wake up all readers and writers so they can be interrupted */
129                 mei_cl_all_wakeup(dev);
130
131                 /* remove entry if already in list */
132                 dev_dbg(&dev->pdev->dev, "remove iamthif and wd from the file list.\n");
133                 mei_cl_unlink(&dev->wd_cl);
134                 mei_cl_unlink(&dev->iamthif_cl);
135                 mei_amthif_reset_params(dev);
136         }
137
138         mei_hbm_reset(dev);
139
140         dev->rd_msg_hdr = 0;
141         dev->wd_pending = false;
142
143         if (ret) {
144                 dev_err(&dev->pdev->dev, "hw_reset failed ret = %d\n", ret);
145                 return ret;
146         }
147
148         if (state == MEI_DEV_POWER_DOWN) {
149                 dev_dbg(&dev->pdev->dev, "powering down: end of reset\n");
150                 dev->dev_state = MEI_DEV_DISABLED;
151                 return 0;
152         }
153
154         ret = mei_hw_start(dev);
155         if (ret) {
156                 dev_err(&dev->pdev->dev, "hw_start failed ret = %d\n", ret);
157                 return ret;
158         }
159
160         dev_dbg(&dev->pdev->dev, "link is established start sending messages.\n");
161
162         dev->dev_state = MEI_DEV_INIT_CLIENTS;
163         ret = mei_hbm_start_req(dev);
164         if (ret) {
165                 dev_err(&dev->pdev->dev, "hbm_start failed ret = %d\n", ret);
166                 dev->dev_state = MEI_DEV_RESETTING;
167                 return ret;
168         }
169
170         return 0;
171 }
172 EXPORT_SYMBOL_GPL(mei_reset);
173
174 /**
175  * mei_start - initializes host and fw to start work.
176  *
177  * @dev: the device structure
178  *
179  * returns 0 on success, <0 on failure.
180  */
181 int mei_start(struct mei_device *dev)
182 {
183         int ret;
184
185         mutex_lock(&dev->device_lock);
186
187         /* acknowledge interrupt and stop interrupts */
188         mei_clear_interrupts(dev);
189
190         mei_hw_config(dev);
191
192         dev_dbg(&dev->pdev->dev, "reset in start the mei device.\n");
193
194         dev->reset_count = 0;
195         do {
196                 dev->dev_state = MEI_DEV_INITIALIZING;
197                 ret = mei_reset(dev);
198
199                 if (ret == -ENODEV || dev->dev_state == MEI_DEV_DISABLED) {
200                         dev_err(&dev->pdev->dev, "reset failed ret = %d", ret);
201                         goto err;
202                 }
203         } while (ret);
204
205         /* we cannot start the device w/o hbm start message completed */
206         if (dev->dev_state == MEI_DEV_DISABLED) {
207                 dev_err(&dev->pdev->dev, "reset failed");
208                 goto err;
209         }
210
211         if (mei_hbm_start_wait(dev)) {
212                 dev_err(&dev->pdev->dev, "HBM haven't started");
213                 goto err;
214         }
215
216         if (!mei_host_is_ready(dev)) {
217                 dev_err(&dev->pdev->dev, "host is not ready.\n");
218                 goto err;
219         }
220
221         if (!mei_hw_is_ready(dev)) {
222                 dev_err(&dev->pdev->dev, "ME is not ready.\n");
223                 goto err;
224         }
225
226         if (!mei_hbm_version_is_supported(dev)) {
227                 dev_dbg(&dev->pdev->dev, "MEI start failed.\n");
228                 goto err;
229         }
230
231         dev_dbg(&dev->pdev->dev, "link layer has been established.\n");
232
233         mutex_unlock(&dev->device_lock);
234         return 0;
235 err:
236         dev_err(&dev->pdev->dev, "link layer initialization failed.\n");
237         dev->dev_state = MEI_DEV_DISABLED;
238         mutex_unlock(&dev->device_lock);
239         return -ENODEV;
240 }
241 EXPORT_SYMBOL_GPL(mei_start);
242
243 /**
244  * mei_restart - restart device after suspend
245  *
246  * @dev: the device structure
247  *
248  * returns 0 on success or -ENODEV if the restart hasn't succeeded
249  */
250 int mei_restart(struct mei_device *dev)
251 {
252         int err;
253
254         mutex_lock(&dev->device_lock);
255
256         mei_clear_interrupts(dev);
257
258         dev->dev_state = MEI_DEV_POWER_UP;
259         dev->reset_count = 0;
260
261         err = mei_reset(dev);
262
263         mutex_unlock(&dev->device_lock);
264
265         if (err == -ENODEV || dev->dev_state == MEI_DEV_DISABLED) {
266                 dev_err(&dev->pdev->dev, "device disabled = %d\n", err);
267                 return -ENODEV;
268         }
269
270         /* try to start again */
271         if (err)
272                 schedule_work(&dev->reset_work);
273
274
275         return 0;
276 }
277 EXPORT_SYMBOL_GPL(mei_restart);
278
279 static void mei_reset_work(struct work_struct *work)
280 {
281         struct mei_device *dev =
282                 container_of(work, struct mei_device,  reset_work);
283         int ret;
284
285         mutex_lock(&dev->device_lock);
286
287         ret = mei_reset(dev);
288
289         mutex_unlock(&dev->device_lock);
290
291         if (dev->dev_state == MEI_DEV_DISABLED) {
292                 dev_err(&dev->pdev->dev, "device disabled = %d\n", ret);
293                 return;
294         }
295
296         /* retry reset in case of failure */
297         if (ret)
298                 schedule_work(&dev->reset_work);
299 }
300
301 void mei_stop(struct mei_device *dev)
302 {
303         dev_dbg(&dev->pdev->dev, "stopping the device.\n");
304
305         mei_cancel_work(dev);
306
307         mei_nfc_host_exit(dev);
308
309         mei_cl_bus_remove_devices(dev);
310
311         mutex_lock(&dev->device_lock);
312
313         mei_wd_stop(dev);
314
315         dev->dev_state = MEI_DEV_POWER_DOWN;
316         mei_reset(dev);
317
318         mutex_unlock(&dev->device_lock);
319
320         mei_watchdog_unregister(dev);
321 }
322 EXPORT_SYMBOL_GPL(mei_stop);
323
324 /**
325  * mei_write_is_idle - check if the write queues are idle
326  *
327  * @dev: the device structure
328  *
329  * returns true of there is no pending write
330  */
331 bool mei_write_is_idle(struct mei_device *dev)
332 {
333         bool idle = (dev->dev_state == MEI_DEV_ENABLED &&
334                 list_empty(&dev->ctrl_wr_list.list) &&
335                 list_empty(&dev->write_list.list));
336
337         dev_dbg(&dev->pdev->dev, "write pg: is idle[%d] state=%s ctrl=%d write=%d\n",
338                 idle,
339                 mei_dev_state_str(dev->dev_state),
340                 list_empty(&dev->ctrl_wr_list.list),
341                 list_empty(&dev->write_list.list));
342
343         return idle;
344 }
345 EXPORT_SYMBOL_GPL(mei_write_is_idle);
346
347 int mei_fw_status(struct mei_device *dev, struct mei_fw_status *fw_status)
348 {
349         const struct mei_fw_status *fw_src = &dev->cfg->fw_status;
350         int ret;
351         int i;
352
353         if (!fw_status)
354                 return -EINVAL;
355
356         fw_status->count = fw_src->count;
357         for (i = 0; i < fw_src->count && i < MEI_FW_STATUS_MAX; i++) {
358                 ret = pci_read_config_dword(dev->pdev,
359                         fw_src->status[i], &fw_status->status[i]);
360                 if (ret)
361                         return ret;
362         }
363
364         return 0;
365 }
366 EXPORT_SYMBOL_GPL(mei_fw_status);
367
368 /**
369  * mei_device_init  -- initialize mei_device structure
370  *
371  * @dev: the mei device
372  * @device: the device structure
373  * @hw_ops: hw operations
374  */
375 void mei_device_init(struct mei_device *dev,
376                      struct device *device,
377                      const struct mei_hw_ops *hw_ops)
378 {
379         /* setup our list array */
380         INIT_LIST_HEAD(&dev->file_list);
381         INIT_LIST_HEAD(&dev->device_list);
382         INIT_LIST_HEAD(&dev->me_clients);
383         mutex_init(&dev->device_lock);
384         init_waitqueue_head(&dev->wait_hw_ready);
385         init_waitqueue_head(&dev->wait_pg);
386         init_waitqueue_head(&dev->wait_hbm_start);
387         init_waitqueue_head(&dev->wait_stop_wd);
388         dev->dev_state = MEI_DEV_INITIALIZING;
389         dev->reset_count = 0;
390
391         mei_io_list_init(&dev->read_list);
392         mei_io_list_init(&dev->write_list);
393         mei_io_list_init(&dev->write_waiting_list);
394         mei_io_list_init(&dev->ctrl_wr_list);
395         mei_io_list_init(&dev->ctrl_rd_list);
396
397         INIT_DELAYED_WORK(&dev->timer_work, mei_timer);
398         INIT_WORK(&dev->init_work, mei_host_client_init);
399         INIT_WORK(&dev->reset_work, mei_reset_work);
400
401         INIT_LIST_HEAD(&dev->wd_cl.link);
402         INIT_LIST_HEAD(&dev->iamthif_cl.link);
403         mei_io_list_init(&dev->amthif_cmd_list);
404         mei_io_list_init(&dev->amthif_rd_complete_list);
405
406         bitmap_zero(dev->host_clients_map, MEI_CLIENTS_MAX);
407         dev->open_handle_count = 0;
408
409         /*
410          * Reserving the first client ID
411          * 0: Reserved for MEI Bus Message communications
412          */
413         bitmap_set(dev->host_clients_map, 0, 1);
414
415         dev->pg_event = MEI_PG_EVENT_IDLE;
416         dev->ops      = hw_ops;
417         dev->dev      = device;
418 }
419 EXPORT_SYMBOL_GPL(mei_device_init);
420