Commit | Line | Data |
---|---|---|
91f01c6d OW |
1 | /* |
2 | * | |
3 | * Intel Management Engine Interface (Intel MEI) Linux driver | |
733ba91c | 4 | * Copyright (c) 2003-2012, Intel Corporation. |
91f01c6d OW |
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 | ||
40e0b67b | 17 | #include <linux/export.h> |
91f01c6d OW |
18 | #include <linux/pci.h> |
19 | #include <linux/sched.h> | |
20 | #include <linux/wait.h> | |
21 | #include <linux/delay.h> | |
22 | ||
47a73801 TW |
23 | #include <linux/mei.h> |
24 | ||
91f01c6d | 25 | #include "mei_dev.h" |
aafae7ec | 26 | #include "hbm.h" |
90e0b5f1 | 27 | #include "client.h" |
91f01c6d | 28 | |
b210d750 TW |
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); | |
0cfee51c | 36 | MEI_DEV_STATE(RESETTING); |
b210d750 | 37 | MEI_DEV_STATE(DISABLED); |
b210d750 TW |
38 | MEI_DEV_STATE(POWER_DOWN); |
39 | MEI_DEV_STATE(POWER_UP); | |
40 | default: | |
8b513d0c | 41 | return "unknown"; |
b210d750 TW |
42 | } |
43 | #undef MEI_DEV_STATE | |
44 | } | |
45 | ||
1beeb4b9 AU |
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 | ||
91f01c6d | 58 | |
544f9460 TW |
59 | /** |
60 | * mei_cancel_work. Cancel mei background jobs | |
61 | * | |
62 | * @dev: the device structure | |
33ec0826 TW |
63 | * |
64 | * returns 0 on success or < 0 if the reset hasn't succeeded | |
544f9460 | 65 | */ |
dc844b0d TW |
66 | void mei_cancel_work(struct mei_device *dev) |
67 | { | |
68 | cancel_work_sync(&dev->init_work); | |
544f9460 | 69 | cancel_work_sync(&dev->reset_work); |
dc844b0d TW |
70 | |
71 | cancel_delayed_work(&dev->timer_work); | |
72 | } | |
73 | EXPORT_SYMBOL_GPL(mei_cancel_work); | |
74 | ||
91f01c6d OW |
75 | /** |
76 | * mei_reset - resets host and fw. | |
77 | * | |
78 | * @dev: the device structure | |
91f01c6d | 79 | */ |
33ec0826 | 80 | int mei_reset(struct mei_device *dev) |
91f01c6d | 81 | { |
33ec0826 TW |
82 | enum mei_dev_state state = dev->dev_state; |
83 | bool interrupts_enabled; | |
c20c68d5 | 84 | int ret; |
91f01c6d | 85 | |
33ec0826 TW |
86 | if (state != MEI_DEV_INITIALIZING && |
87 | state != MEI_DEV_DISABLED && | |
88 | state != MEI_DEV_POWER_DOWN && | |
04dd3661 AU |
89 | state != MEI_DEV_POWER_UP) { |
90 | struct mei_fw_status fw_status; | |
92db1555 | 91 | |
04dd3661 AU |
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 | } | |
f931f4f3 | 97 | |
66ae460b TW |
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 | ||
33ec0826 TW |
105 | /* enter reset flow */ |
106 | interrupts_enabled = state != MEI_DEV_POWER_DOWN; | |
107 | dev->dev_state = MEI_DEV_RESETTING; | |
91f01c6d | 108 | |
6adb8efb TW |
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 | ||
33ec0826 TW |
116 | ret = mei_hw_reset(dev, interrupts_enabled); |
117 | /* fall through and remove the sw state even if hw reset has failed */ | |
91f01c6d | 118 | |
33ec0826 TW |
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) { | |
91f01c6d | 122 | |
b950ac1d TW |
123 | /* remove all waiting requests */ |
124 | mei_cl_all_write_clear(dev); | |
125 | ||
074b4c01 TW |
126 | mei_cl_all_disconnect(dev); |
127 | ||
33ec0826 | 128 | /* wake up all readers and writers so they can be interrupted */ |
b950ac1d TW |
129 | mei_cl_all_wakeup(dev); |
130 | ||
91f01c6d | 131 | /* remove entry if already in list */ |
ff8b2f4e | 132 | dev_dbg(&dev->pdev->dev, "remove iamthif and wd from the file list.\n"); |
90e0b5f1 TW |
133 | mei_cl_unlink(&dev->wd_cl); |
134 | mei_cl_unlink(&dev->iamthif_cl); | |
19838fb8 | 135 | mei_amthif_reset_params(dev); |
91f01c6d OW |
136 | } |
137 | ||
84b3294a | 138 | mei_hbm_reset(dev); |
4a704575 | 139 | |
91f01c6d | 140 | dev->rd_msg_hdr = 0; |
eb9af0ac | 141 | dev->wd_pending = false; |
91f01c6d | 142 | |
33ec0826 TW |
143 | if (ret) { |
144 | dev_err(&dev->pdev->dev, "hw_reset failed ret = %d\n", ret); | |
33ec0826 TW |
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; | |
aafae7ec TW |
152 | } |
153 | ||
9049f793 TW |
154 | ret = mei_hw_start(dev); |
155 | if (ret) { | |
33ec0826 | 156 | dev_err(&dev->pdev->dev, "hw_start failed ret = %d\n", ret); |
33ec0826 | 157 | return ret; |
9049f793 | 158 | } |
aafae7ec TW |
159 | |
160 | dev_dbg(&dev->pdev->dev, "link is established start sending messages.\n"); | |
aafae7ec TW |
161 | |
162 | dev->dev_state = MEI_DEV_INIT_CLIENTS; | |
544f9460 TW |
163 | ret = mei_hbm_start_req(dev); |
164 | if (ret) { | |
33ec0826 | 165 | dev_err(&dev->pdev->dev, "hbm_start failed ret = %d\n", ret); |
7d93e58d | 166 | dev->dev_state = MEI_DEV_RESETTING; |
33ec0826 | 167 | return ret; |
544f9460 | 168 | } |
33ec0826 TW |
169 | |
170 | return 0; | |
91f01c6d | 171 | } |
40e0b67b | 172 | EXPORT_SYMBOL_GPL(mei_reset); |
91f01c6d | 173 | |
33ec0826 TW |
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 | { | |
7d93e58d | 183 | int ret; |
92db1555 | 184 | |
33ec0826 TW |
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 | ||
6adb8efb | 194 | dev->reset_count = 0; |
7d93e58d TW |
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); | |
33ec0826 | 204 | |
7d93e58d | 205 | /* we cannot start the device w/o hbm start message completed */ |
33ec0826 TW |
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; | |
6adb8efb | 259 | dev->reset_count = 0; |
33ec0826 TW |
260 | |
261 | err = mei_reset(dev); | |
262 | ||
263 | mutex_unlock(&dev->device_lock); | |
264 | ||
7d93e58d TW |
265 | if (err == -ENODEV || dev->dev_state == MEI_DEV_DISABLED) { |
266 | dev_err(&dev->pdev->dev, "device disabled = %d\n", err); | |
33ec0826 | 267 | return -ENODEV; |
7d93e58d TW |
268 | } |
269 | ||
270 | /* try to start again */ | |
271 | if (err) | |
272 | schedule_work(&dev->reset_work); | |
273 | ||
33ec0826 TW |
274 | |
275 | return 0; | |
276 | } | |
277 | EXPORT_SYMBOL_GPL(mei_restart); | |
278 | ||
544f9460 TW |
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); | |
7d93e58d | 283 | int ret; |
544f9460 TW |
284 | |
285 | mutex_lock(&dev->device_lock); | |
286 | ||
7d93e58d | 287 | ret = mei_reset(dev); |
544f9460 TW |
288 | |
289 | mutex_unlock(&dev->device_lock); | |
33ec0826 | 290 | |
7d93e58d TW |
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); | |
544f9460 TW |
299 | } |
300 | ||
7cb035d9 TW |
301 | void mei_stop(struct mei_device *dev) |
302 | { | |
303 | dev_dbg(&dev->pdev->dev, "stopping the device.\n"); | |
304 | ||
dc844b0d | 305 | mei_cancel_work(dev); |
5e85b364 | 306 | |
dc844b0d | 307 | mei_nfc_host_exit(dev); |
7cb035d9 | 308 | |
48705693 TW |
309 | mei_cl_bus_remove_devices(dev); |
310 | ||
dc844b0d | 311 | mutex_lock(&dev->device_lock); |
7cb035d9 TW |
312 | |
313 | mei_wd_stop(dev); | |
314 | ||
315 | dev->dev_state = MEI_DEV_POWER_DOWN; | |
33ec0826 | 316 | mei_reset(dev); |
7cb035d9 TW |
317 | |
318 | mutex_unlock(&dev->device_lock); | |
319 | ||
2e647124 | 320 | mei_watchdog_unregister(dev); |
7cb035d9 | 321 | } |
40e0b67b | 322 | EXPORT_SYMBOL_GPL(mei_stop); |
91f01c6d | 323 | |
a532bbed TW |
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)); | |
c1174c0e | 336 | |
a532bbed TW |
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); | |
91f01c6d | 346 | |
8d929d48 AU |
347 | int mei_fw_status(struct mei_device *dev, struct mei_fw_status *fw_status) |
348 | { | |
8d929d48 | 349 | const struct mei_fw_status *fw_src = &dev->cfg->fw_status; |
92db1555 TW |
350 | int ret; |
351 | int i; | |
8d929d48 AU |
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++) { | |
8d929d48 AU |
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 | ||
3a7e9b6c TW |
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) | |
544f9460 TW |
378 | { |
379 | /* setup our list array */ | |
380 | INIT_LIST_HEAD(&dev->file_list); | |
381 | INIT_LIST_HEAD(&dev->device_list); | |
5ca2d388 | 382 | INIT_LIST_HEAD(&dev->me_clients); |
544f9460 TW |
383 | mutex_init(&dev->device_lock); |
384 | init_waitqueue_head(&dev->wait_hw_ready); | |
4fcbc99b | 385 | init_waitqueue_head(&dev->wait_pg); |
cb02efc3 | 386 | init_waitqueue_head(&dev->wait_hbm_start); |
544f9460 TW |
387 | init_waitqueue_head(&dev->wait_stop_wd); |
388 | dev->dev_state = MEI_DEV_INITIALIZING; | |
6adb8efb | 389 | dev->reset_count = 0; |
544f9460 TW |
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); | |
964a2331 TW |
414 | |
415 | dev->pg_event = MEI_PG_EVENT_IDLE; | |
3a7e9b6c TW |
416 | dev->ops = hw_ops; |
417 | dev->dev = device; | |
544f9460 TW |
418 | } |
419 | EXPORT_SYMBOL_GPL(mei_device_init); | |
420 |