mei: remove flash_work_queue
[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 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         bitmap_zero(dev->host_clients_map, MEI_CLIENTS_MAX);
72         dev->open_handle_count = 0;
73
74         /*
75          * Reserving the first client ID
76          * 0: Reserved for MEI Bus Message communications
77          */
78         bitmap_set(dev->host_clients_map, 0, 1);
79 }
80 EXPORT_SYMBOL_GPL(mei_device_init);
81
82 /**
83  * mei_start - initializes host and fw to start work.
84  *
85  * @dev: the device structure
86  *
87  * returns 0 on success, <0 on failure.
88  */
89 int mei_start(struct mei_device *dev)
90 {
91         mutex_lock(&dev->device_lock);
92
93         /* acknowledge interrupt and stop interupts */
94         mei_clear_interrupts(dev);
95
96         mei_hw_config(dev);
97
98         dev_dbg(&dev->pdev->dev, "reset in start the mei device.\n");
99
100         mei_reset(dev, 1);
101
102         if (mei_hbm_start_wait(dev)) {
103                 dev_err(&dev->pdev->dev, "HBM haven't started");
104                 goto err;
105         }
106
107         if (!mei_host_is_ready(dev)) {
108                 dev_err(&dev->pdev->dev, "host is not ready.\n");
109                 goto err;
110         }
111
112         if (!mei_hw_is_ready(dev)) {
113                 dev_err(&dev->pdev->dev, "ME is not ready.\n");
114                 goto err;
115         }
116
117         if (!mei_hbm_version_is_supported(dev)) {
118                 dev_dbg(&dev->pdev->dev, "MEI start failed.\n");
119                 goto err;
120         }
121
122         dev_dbg(&dev->pdev->dev, "link layer has been established.\n");
123
124         mutex_unlock(&dev->device_lock);
125         return 0;
126 err:
127         dev_err(&dev->pdev->dev, "link layer initialization failed.\n");
128         dev->dev_state = MEI_DEV_DISABLED;
129         mutex_unlock(&dev->device_lock);
130         return -ENODEV;
131 }
132 EXPORT_SYMBOL_GPL(mei_start);
133
134
135 void mei_cancel_work(struct mei_device *dev)
136 {
137         cancel_work_sync(&dev->init_work);
138
139         cancel_delayed_work(&dev->timer_work);
140 }
141 EXPORT_SYMBOL_GPL(mei_cancel_work);
142
143 /**
144  * mei_reset - resets host and fw.
145  *
146  * @dev: the device structure
147  * @interrupts_enabled: if interrupt should be enabled after reset.
148  */
149 void mei_reset(struct mei_device *dev, int interrupts_enabled)
150 {
151         bool unexpected;
152         int ret;
153
154         unexpected = (dev->dev_state != MEI_DEV_INITIALIZING &&
155                         dev->dev_state != MEI_DEV_DISABLED &&
156                         dev->dev_state != MEI_DEV_POWER_DOWN &&
157                         dev->dev_state != MEI_DEV_POWER_UP);
158
159         if (unexpected)
160                 dev_warn(&dev->pdev->dev, "unexpected reset: dev_state = %s\n",
161                          mei_dev_state_str(dev->dev_state));
162
163         ret = mei_hw_reset(dev, interrupts_enabled);
164         if (ret) {
165                 dev_err(&dev->pdev->dev, "hw reset failed disabling the device\n");
166                 interrupts_enabled = false;
167                 dev->dev_state = MEI_DEV_DISABLED;
168         }
169
170         dev->hbm_state = MEI_HBM_IDLE;
171
172         if (dev->dev_state != MEI_DEV_INITIALIZING &&
173             dev->dev_state != MEI_DEV_POWER_UP) {
174                 if (dev->dev_state != MEI_DEV_DISABLED &&
175                     dev->dev_state != MEI_DEV_POWER_DOWN)
176                         dev->dev_state = MEI_DEV_RESETTING;
177
178                 /* remove all waiting requests */
179                 mei_cl_all_write_clear(dev);
180
181                 mei_cl_all_disconnect(dev);
182
183                 /* wake up all readings so they can be interrupted */
184                 mei_cl_all_wakeup(dev);
185
186                 /* remove entry if already in list */
187                 dev_dbg(&dev->pdev->dev, "remove iamthif and wd from the file list.\n");
188                 mei_cl_unlink(&dev->wd_cl);
189                 mei_cl_unlink(&dev->iamthif_cl);
190                 mei_amthif_reset_params(dev);
191                 memset(&dev->wr_ext_msg, 0, sizeof(dev->wr_ext_msg));
192         }
193
194         /* we're already in reset, cancel the init timer */
195         dev->init_clients_timer = 0;
196
197         dev->me_clients_num = 0;
198         dev->rd_msg_hdr = 0;
199         dev->wd_pending = false;
200
201         if (!interrupts_enabled) {
202                 dev_dbg(&dev->pdev->dev, "intr not enabled end of reset\n");
203                 return;
204         }
205
206         ret = mei_hw_start(dev);
207         if (ret) {
208                 dev_err(&dev->pdev->dev, "hw_start failed disabling the device\n");
209                 dev->dev_state = MEI_DEV_DISABLED;
210                 return;
211         }
212
213         dev_dbg(&dev->pdev->dev, "link is established start sending messages.\n");
214         /* link is established * start sending messages.  */
215
216         dev->dev_state = MEI_DEV_INIT_CLIENTS;
217
218         mei_hbm_start_req(dev);
219
220 }
221 EXPORT_SYMBOL_GPL(mei_reset);
222
223 void mei_stop(struct mei_device *dev)
224 {
225         dev_dbg(&dev->pdev->dev, "stopping the device.\n");
226
227         mei_cancel_work(dev);
228
229         mei_nfc_host_exit(dev);
230
231         mutex_lock(&dev->device_lock);
232
233         mei_wd_stop(dev);
234
235         dev->dev_state = MEI_DEV_POWER_DOWN;
236         mei_reset(dev, 0);
237
238         mutex_unlock(&dev->device_lock);
239
240         mei_watchdog_unregister(dev);
241 }
242 EXPORT_SYMBOL_GPL(mei_stop);
243
244
245