3 * Intel Management Engine Interface (Intel MEI) Linux driver
4 * Copyright (c) 2003-2012, Intel Corporation.
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.
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
17 #include <linux/export.h>
18 #include <linux/sched.h>
19 #include <linux/wait.h>
20 #include <linux/delay.h>
22 #include <linux/mei.h>
28 const char *mei_dev_state_str(int state)
30 #define MEI_DEV_STATE(state) case MEI_DEV_##state: return #state
32 MEI_DEV_STATE(INITIALIZING);
33 MEI_DEV_STATE(INIT_CLIENTS);
34 MEI_DEV_STATE(ENABLED);
35 MEI_DEV_STATE(RESETTING);
36 MEI_DEV_STATE(DISABLED);
37 MEI_DEV_STATE(POWER_DOWN);
38 MEI_DEV_STATE(POWER_UP);
45 const char *mei_pg_state_str(enum mei_pg_state state)
47 #define MEI_PG_STATE(state) case MEI_PG_##state: return #state
59 * mei_cancel_work - Cancel mei background jobs
61 * @dev: the device structure
63 void mei_cancel_work(struct mei_device *dev)
65 cancel_work_sync(&dev->init_work);
66 cancel_work_sync(&dev->reset_work);
68 cancel_delayed_work(&dev->timer_work);
70 EXPORT_SYMBOL_GPL(mei_cancel_work);
73 * mei_reset - resets host and fw.
75 * @dev: the device structure
77 * Return: 0 on success or < 0 if the reset hasn't succeeded
79 int mei_reset(struct mei_device *dev)
81 enum mei_dev_state state = dev->dev_state;
82 bool interrupts_enabled;
85 if (state != MEI_DEV_INITIALIZING &&
86 state != MEI_DEV_DISABLED &&
87 state != MEI_DEV_POWER_DOWN &&
88 state != MEI_DEV_POWER_UP) {
89 struct mei_fw_status fw_status;
91 mei_fw_status(dev, &fw_status);
93 "unexpected reset: dev_state = %s " FW_STS_FMT "\n",
94 mei_dev_state_str(state), FW_STS_PRM(fw_status));
97 /* we're already in reset, cancel the init timer
98 * if the reset was called due the hbm protocol error
99 * we need to call it before hw start
100 * so the hbm watchdog won't kick in
104 /* enter reset flow */
105 interrupts_enabled = state != MEI_DEV_POWER_DOWN;
106 dev->dev_state = MEI_DEV_RESETTING;
109 if (dev->reset_count > MEI_MAX_CONSEC_RESET) {
110 dev_err(dev->dev, "reset: reached maximal consecutive resets: disabling the device\n");
111 dev->dev_state = MEI_DEV_DISABLED;
115 ret = mei_hw_reset(dev, interrupts_enabled);
116 /* fall through and remove the sw state even if hw reset has failed */
118 /* no need to clean up software state in case of power up */
119 if (state != MEI_DEV_INITIALIZING &&
120 state != MEI_DEV_POWER_UP) {
122 /* remove all waiting requests */
123 mei_cl_all_write_clear(dev);
125 mei_cl_all_disconnect(dev);
127 /* wake up all readers and writers so they can be interrupted */
128 mei_cl_all_wakeup(dev);
130 /* remove entry if already in list */
131 dev_dbg(dev->dev, "remove iamthif and wd from the file list.\n");
132 mei_cl_unlink(&dev->wd_cl);
133 mei_cl_unlink(&dev->iamthif_cl);
134 mei_amthif_reset_params(dev);
140 dev->wd_pending = false;
143 dev_err(dev->dev, "hw_reset failed ret = %d\n", ret);
147 if (state == MEI_DEV_POWER_DOWN) {
148 dev_dbg(dev->dev, "powering down: end of reset\n");
149 dev->dev_state = MEI_DEV_DISABLED;
153 ret = mei_hw_start(dev);
155 dev_err(dev->dev, "hw_start failed ret = %d\n", ret);
159 dev_dbg(dev->dev, "link is established start sending messages.\n");
161 dev->dev_state = MEI_DEV_INIT_CLIENTS;
162 ret = mei_hbm_start_req(dev);
164 dev_err(dev->dev, "hbm_start failed ret = %d\n", ret);
165 dev->dev_state = MEI_DEV_RESETTING;
171 EXPORT_SYMBOL_GPL(mei_reset);
174 * mei_start - initializes host and fw to start work.
176 * @dev: the device structure
178 * Return: 0 on success, <0 on failure.
180 int mei_start(struct mei_device *dev)
184 mutex_lock(&dev->device_lock);
186 /* acknowledge interrupt and stop interrupts */
187 mei_clear_interrupts(dev);
191 dev_dbg(dev->dev, "reset in start the mei device.\n");
193 dev->reset_count = 0;
195 dev->dev_state = MEI_DEV_INITIALIZING;
196 ret = mei_reset(dev);
198 if (ret == -ENODEV || dev->dev_state == MEI_DEV_DISABLED) {
199 dev_err(dev->dev, "reset failed ret = %d", ret);
204 /* we cannot start the device w/o hbm start message completed */
205 if (dev->dev_state == MEI_DEV_DISABLED) {
206 dev_err(dev->dev, "reset failed");
210 if (mei_hbm_start_wait(dev)) {
211 dev_err(dev->dev, "HBM haven't started");
215 if (!mei_host_is_ready(dev)) {
216 dev_err(dev->dev, "host is not ready.\n");
220 if (!mei_hw_is_ready(dev)) {
221 dev_err(dev->dev, "ME is not ready.\n");
225 if (!mei_hbm_version_is_supported(dev)) {
226 dev_dbg(dev->dev, "MEI start failed.\n");
230 dev_dbg(dev->dev, "link layer has been established.\n");
232 mutex_unlock(&dev->device_lock);
235 dev_err(dev->dev, "link layer initialization failed.\n");
236 dev->dev_state = MEI_DEV_DISABLED;
237 mutex_unlock(&dev->device_lock);
240 EXPORT_SYMBOL_GPL(mei_start);
243 * mei_restart - restart device after suspend
245 * @dev: the device structure
247 * Return: 0 on success or -ENODEV if the restart hasn't succeeded
249 int mei_restart(struct mei_device *dev)
253 mutex_lock(&dev->device_lock);
255 mei_clear_interrupts(dev);
257 dev->dev_state = MEI_DEV_POWER_UP;
258 dev->reset_count = 0;
260 err = mei_reset(dev);
262 mutex_unlock(&dev->device_lock);
264 if (err == -ENODEV || dev->dev_state == MEI_DEV_DISABLED) {
265 dev_err(dev->dev, "device disabled = %d\n", err);
269 /* try to start again */
271 schedule_work(&dev->reset_work);
276 EXPORT_SYMBOL_GPL(mei_restart);
278 static void mei_reset_work(struct work_struct *work)
280 struct mei_device *dev =
281 container_of(work, struct mei_device, reset_work);
284 mutex_lock(&dev->device_lock);
286 ret = mei_reset(dev);
288 mutex_unlock(&dev->device_lock);
290 if (dev->dev_state == MEI_DEV_DISABLED) {
291 dev_err(dev->dev, "device disabled = %d\n", ret);
295 /* retry reset in case of failure */
297 schedule_work(&dev->reset_work);
300 void mei_stop(struct mei_device *dev)
302 dev_dbg(dev->dev, "stopping the device.\n");
304 mei_cancel_work(dev);
306 mei_nfc_host_exit(dev);
308 mei_cl_bus_remove_devices(dev);
310 mutex_lock(&dev->device_lock);
314 dev->dev_state = MEI_DEV_POWER_DOWN;
317 mutex_unlock(&dev->device_lock);
319 mei_watchdog_unregister(dev);
321 EXPORT_SYMBOL_GPL(mei_stop);
324 * mei_write_is_idle - check if the write queues are idle
326 * @dev: the device structure
328 * Return: true of there is no pending write
330 bool mei_write_is_idle(struct mei_device *dev)
332 bool idle = (dev->dev_state == MEI_DEV_ENABLED &&
333 list_empty(&dev->ctrl_wr_list.list) &&
334 list_empty(&dev->write_list.list));
336 dev_dbg(dev->dev, "write pg: is idle[%d] state=%s ctrl=%d write=%d\n",
338 mei_dev_state_str(dev->dev_state),
339 list_empty(&dev->ctrl_wr_list.list),
340 list_empty(&dev->write_list.list));
344 EXPORT_SYMBOL_GPL(mei_write_is_idle);
347 * mei_device_init -- initialize mei_device structure
349 * @dev: the mei device
350 * @device: the device structure
351 * @hw_ops: hw operations
353 void mei_device_init(struct mei_device *dev,
354 struct device *device,
355 const struct mei_hw_ops *hw_ops)
357 /* setup our list array */
358 INIT_LIST_HEAD(&dev->file_list);
359 INIT_LIST_HEAD(&dev->device_list);
360 INIT_LIST_HEAD(&dev->me_clients);
361 mutex_init(&dev->device_lock);
362 init_waitqueue_head(&dev->wait_hw_ready);
363 init_waitqueue_head(&dev->wait_pg);
364 init_waitqueue_head(&dev->wait_hbm_start);
365 init_waitqueue_head(&dev->wait_stop_wd);
366 dev->dev_state = MEI_DEV_INITIALIZING;
367 dev->reset_count = 0;
369 mei_io_list_init(&dev->read_list);
370 mei_io_list_init(&dev->write_list);
371 mei_io_list_init(&dev->write_waiting_list);
372 mei_io_list_init(&dev->ctrl_wr_list);
373 mei_io_list_init(&dev->ctrl_rd_list);
375 INIT_DELAYED_WORK(&dev->timer_work, mei_timer);
376 INIT_WORK(&dev->init_work, mei_host_client_init);
377 INIT_WORK(&dev->reset_work, mei_reset_work);
379 INIT_LIST_HEAD(&dev->wd_cl.link);
380 INIT_LIST_HEAD(&dev->iamthif_cl.link);
381 mei_io_list_init(&dev->amthif_cmd_list);
382 mei_io_list_init(&dev->amthif_rd_complete_list);
384 bitmap_zero(dev->host_clients_map, MEI_CLIENTS_MAX);
385 dev->open_handle_count = 0;
388 * Reserving the first client ID
389 * 0: Reserved for MEI Bus Message communications
391 bitmap_set(dev->host_clients_map, 0, 1);
393 dev->pg_event = MEI_PG_EVENT_IDLE;
397 EXPORT_SYMBOL_GPL(mei_device_init);