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/pci.h>
19 #include <linux/sched.h>
20 #include <linux/wait.h>
21 #include <linux/delay.h>
23 #include <linux/mei.h>
29 const char *mei_dev_state_str(int state)
31 #define MEI_DEV_STATE(state) case MEI_DEV_##state: return #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);
48 * mei_cancel_work. Cancel mei background jobs
50 * @dev: the device structure
52 * returns 0 on success or < 0 if the reset hasn't succeeded
54 void mei_cancel_work(struct mei_device *dev)
56 cancel_work_sync(&dev->init_work);
57 cancel_work_sync(&dev->reset_work);
59 cancel_delayed_work(&dev->timer_work);
61 EXPORT_SYMBOL_GPL(mei_cancel_work);
64 * mei_reset - resets host and fw.
66 * @dev: the device structure
68 int mei_reset(struct mei_device *dev)
70 enum mei_dev_state state = dev->dev_state;
71 bool interrupts_enabled;
74 if (state != MEI_DEV_INITIALIZING &&
75 state != MEI_DEV_DISABLED &&
76 state != MEI_DEV_POWER_DOWN &&
77 state != MEI_DEV_POWER_UP) {
78 struct mei_fw_status fw_status;
79 mei_fw_status(dev, &fw_status);
80 dev_warn(&dev->pdev->dev,
81 "unexpected reset: dev_state = %s " FW_STS_FMT "\n",
82 mei_dev_state_str(state), FW_STS_PRM(fw_status));
85 /* we're already in reset, cancel the init timer
86 * if the reset was called due the hbm protocol error
87 * we need to call it before hw start
88 * so the hbm watchdog won't kick in
92 /* enter reset flow */
93 interrupts_enabled = state != MEI_DEV_POWER_DOWN;
94 dev->dev_state = MEI_DEV_RESETTING;
97 if (dev->reset_count > MEI_MAX_CONSEC_RESET) {
98 dev_err(&dev->pdev->dev, "reset: reached maximal consecutive resets: disabling the device\n");
99 dev->dev_state = MEI_DEV_DISABLED;
103 ret = mei_hw_reset(dev, interrupts_enabled);
104 /* fall through and remove the sw state even if hw reset has failed */
106 /* no need to clean up software state in case of power up */
107 if (state != MEI_DEV_INITIALIZING &&
108 state != MEI_DEV_POWER_UP) {
110 /* remove all waiting requests */
111 mei_cl_all_write_clear(dev);
113 mei_cl_all_disconnect(dev);
115 /* wake up all readers and writers so they can be interrupted */
116 mei_cl_all_wakeup(dev);
118 /* remove entry if already in list */
119 dev_dbg(&dev->pdev->dev, "remove iamthif and wd from the file list.\n");
120 mei_cl_unlink(&dev->wd_cl);
121 mei_cl_unlink(&dev->iamthif_cl);
122 mei_amthif_reset_params(dev);
128 dev->wd_pending = false;
131 dev_err(&dev->pdev->dev, "hw_reset failed ret = %d\n", ret);
135 if (state == MEI_DEV_POWER_DOWN) {
136 dev_dbg(&dev->pdev->dev, "powering down: end of reset\n");
137 dev->dev_state = MEI_DEV_DISABLED;
141 ret = mei_hw_start(dev);
143 dev_err(&dev->pdev->dev, "hw_start failed ret = %d\n", ret);
147 dev_dbg(&dev->pdev->dev, "link is established start sending messages.\n");
149 dev->dev_state = MEI_DEV_INIT_CLIENTS;
150 ret = mei_hbm_start_req(dev);
152 dev_err(&dev->pdev->dev, "hbm_start failed ret = %d\n", ret);
153 dev->dev_state = MEI_DEV_RESETTING;
159 EXPORT_SYMBOL_GPL(mei_reset);
162 * mei_start - initializes host and fw to start work.
164 * @dev: the device structure
166 * returns 0 on success, <0 on failure.
168 int mei_start(struct mei_device *dev)
171 mutex_lock(&dev->device_lock);
173 /* acknowledge interrupt and stop interrupts */
174 mei_clear_interrupts(dev);
178 dev_dbg(&dev->pdev->dev, "reset in start the mei device.\n");
180 dev->reset_count = 0;
182 dev->dev_state = MEI_DEV_INITIALIZING;
183 ret = mei_reset(dev);
185 if (ret == -ENODEV || dev->dev_state == MEI_DEV_DISABLED) {
186 dev_err(&dev->pdev->dev, "reset failed ret = %d", ret);
191 /* we cannot start the device w/o hbm start message completed */
192 if (dev->dev_state == MEI_DEV_DISABLED) {
193 dev_err(&dev->pdev->dev, "reset failed");
197 if (mei_hbm_start_wait(dev)) {
198 dev_err(&dev->pdev->dev, "HBM haven't started");
202 if (!mei_host_is_ready(dev)) {
203 dev_err(&dev->pdev->dev, "host is not ready.\n");
207 if (!mei_hw_is_ready(dev)) {
208 dev_err(&dev->pdev->dev, "ME is not ready.\n");
212 if (!mei_hbm_version_is_supported(dev)) {
213 dev_dbg(&dev->pdev->dev, "MEI start failed.\n");
217 dev_dbg(&dev->pdev->dev, "link layer has been established.\n");
219 mutex_unlock(&dev->device_lock);
222 dev_err(&dev->pdev->dev, "link layer initialization failed.\n");
223 dev->dev_state = MEI_DEV_DISABLED;
224 mutex_unlock(&dev->device_lock);
227 EXPORT_SYMBOL_GPL(mei_start);
230 * mei_restart - restart device after suspend
232 * @dev: the device structure
234 * returns 0 on success or -ENODEV if the restart hasn't succeeded
236 int mei_restart(struct mei_device *dev)
240 mutex_lock(&dev->device_lock);
242 mei_clear_interrupts(dev);
244 dev->dev_state = MEI_DEV_POWER_UP;
245 dev->reset_count = 0;
247 err = mei_reset(dev);
249 mutex_unlock(&dev->device_lock);
251 if (err == -ENODEV || dev->dev_state == MEI_DEV_DISABLED) {
252 dev_err(&dev->pdev->dev, "device disabled = %d\n", err);
256 /* try to start again */
258 schedule_work(&dev->reset_work);
263 EXPORT_SYMBOL_GPL(mei_restart);
265 static void mei_reset_work(struct work_struct *work)
267 struct mei_device *dev =
268 container_of(work, struct mei_device, reset_work);
271 mutex_lock(&dev->device_lock);
273 ret = mei_reset(dev);
275 mutex_unlock(&dev->device_lock);
277 if (dev->dev_state == MEI_DEV_DISABLED) {
278 dev_err(&dev->pdev->dev, "device disabled = %d\n", ret);
282 /* retry reset in case of failure */
284 schedule_work(&dev->reset_work);
287 void mei_stop(struct mei_device *dev)
289 dev_dbg(&dev->pdev->dev, "stopping the device.\n");
291 mei_cancel_work(dev);
293 mei_nfc_host_exit(dev);
295 mei_cl_bus_remove_devices(dev);
297 mutex_lock(&dev->device_lock);
301 dev->dev_state = MEI_DEV_POWER_DOWN;
304 mutex_unlock(&dev->device_lock);
306 mei_watchdog_unregister(dev);
308 EXPORT_SYMBOL_GPL(mei_stop);
311 * mei_write_is_idle - check if the write queues are idle
313 * @dev: the device structure
315 * returns true of there is no pending write
317 bool mei_write_is_idle(struct mei_device *dev)
319 bool idle = (dev->dev_state == MEI_DEV_ENABLED &&
320 list_empty(&dev->ctrl_wr_list.list) &&
321 list_empty(&dev->write_list.list));
323 dev_dbg(&dev->pdev->dev, "write pg: is idle[%d] state=%s ctrl=%d write=%d\n",
325 mei_dev_state_str(dev->dev_state),
326 list_empty(&dev->ctrl_wr_list.list),
327 list_empty(&dev->write_list.list));
331 EXPORT_SYMBOL_GPL(mei_write_is_idle);
333 int mei_fw_status(struct mei_device *dev, struct mei_fw_status *fw_status)
336 const struct mei_fw_status *fw_src = &dev->cfg->fw_status;
341 fw_status->count = fw_src->count;
342 for (i = 0; i < fw_src->count && i < MEI_FW_STATUS_MAX; i++) {
344 ret = pci_read_config_dword(dev->pdev,
345 fw_src->status[i], &fw_status->status[i]);
352 EXPORT_SYMBOL_GPL(mei_fw_status);
354 void mei_device_init(struct mei_device *dev, const struct mei_cfg *cfg)
356 /* setup our list array */
357 INIT_LIST_HEAD(&dev->file_list);
358 INIT_LIST_HEAD(&dev->device_list);
359 mutex_init(&dev->device_lock);
360 init_waitqueue_head(&dev->wait_hw_ready);
361 init_waitqueue_head(&dev->wait_pg);
362 init_waitqueue_head(&dev->wait_recvd_msg);
363 init_waitqueue_head(&dev->wait_stop_wd);
364 dev->dev_state = MEI_DEV_INITIALIZING;
365 dev->reset_count = 0;
367 mei_io_list_init(&dev->read_list);
368 mei_io_list_init(&dev->write_list);
369 mei_io_list_init(&dev->write_waiting_list);
370 mei_io_list_init(&dev->ctrl_wr_list);
371 mei_io_list_init(&dev->ctrl_rd_list);
373 INIT_DELAYED_WORK(&dev->timer_work, mei_timer);
374 INIT_WORK(&dev->init_work, mei_host_client_init);
375 INIT_WORK(&dev->reset_work, mei_reset_work);
377 INIT_LIST_HEAD(&dev->wd_cl.link);
378 INIT_LIST_HEAD(&dev->iamthif_cl.link);
379 mei_io_list_init(&dev->amthif_cmd_list);
380 mei_io_list_init(&dev->amthif_rd_complete_list);
382 bitmap_zero(dev->host_clients_map, MEI_CLIENTS_MAX);
383 dev->open_handle_count = 0;
386 * Reserving the first client ID
387 * 0: Reserved for MEI Bus Message communications
389 bitmap_set(dev->host_clients_map, 0, 1);
391 dev->pg_event = MEI_PG_EVENT_IDLE;
394 EXPORT_SYMBOL_GPL(mei_device_init);