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);
47 * mei_start - initializes host and fw to start work.
49 * @dev: the device structure
51 * returns 0 on success, <0 on failure.
53 int mei_start(struct mei_device *dev)
55 mutex_lock(&dev->device_lock);
57 /* acknowledge interrupt and stop interupts */
58 mei_clear_interrupts(dev);
62 dev_dbg(&dev->pdev->dev, "reset in start the mei device.\n");
66 if (mei_hbm_start_wait(dev)) {
67 dev_err(&dev->pdev->dev, "HBM haven't started");
71 if (!mei_host_is_ready(dev)) {
72 dev_err(&dev->pdev->dev, "host is not ready.\n");
76 if (!mei_hw_is_ready(dev)) {
77 dev_err(&dev->pdev->dev, "ME is not ready.\n");
81 if (!mei_hbm_version_is_supported(dev)) {
82 dev_dbg(&dev->pdev->dev, "MEI start failed.\n");
86 dev_dbg(&dev->pdev->dev, "link layer has been established.\n");
88 mutex_unlock(&dev->device_lock);
91 dev_err(&dev->pdev->dev, "link layer initialization failed.\n");
92 dev->dev_state = MEI_DEV_DISABLED;
93 mutex_unlock(&dev->device_lock);
96 EXPORT_SYMBOL_GPL(mei_start);
99 * mei_cancel_work. Cancel mei background jobs
101 * @dev: the device structure
103 void mei_cancel_work(struct mei_device *dev)
105 cancel_work_sync(&dev->init_work);
106 cancel_work_sync(&dev->reset_work);
108 cancel_delayed_work(&dev->timer_work);
110 EXPORT_SYMBOL_GPL(mei_cancel_work);
113 * mei_reset - resets host and fw.
115 * @dev: the device structure
116 * @interrupts_enabled: if interrupt should be enabled after reset.
118 void mei_reset(struct mei_device *dev, int interrupts_enabled)
123 unexpected = (dev->dev_state != MEI_DEV_INITIALIZING &&
124 dev->dev_state != MEI_DEV_DISABLED &&
125 dev->dev_state != MEI_DEV_POWER_DOWN &&
126 dev->dev_state != MEI_DEV_POWER_UP);
129 dev_warn(&dev->pdev->dev, "unexpected reset: dev_state = %s\n",
130 mei_dev_state_str(dev->dev_state));
132 /* we're already in reset, cancel the init timer
133 * if the reset was called due the hbm protocol error
134 * we need to call it before hw start
135 * so the hbm watchdog won't kick in
139 ret = mei_hw_reset(dev, interrupts_enabled);
141 dev_err(&dev->pdev->dev, "hw reset failed disabling the device\n");
142 interrupts_enabled = false;
146 if (dev->dev_state != MEI_DEV_INITIALIZING &&
147 dev->dev_state != MEI_DEV_POWER_UP) {
148 if (dev->dev_state != MEI_DEV_DISABLED &&
149 dev->dev_state != MEI_DEV_POWER_DOWN)
150 dev->dev_state = MEI_DEV_RESETTING;
152 /* remove all waiting requests */
153 mei_cl_all_write_clear(dev);
155 mei_cl_all_disconnect(dev);
157 /* wake up all readings so they can be interrupted */
158 mei_cl_all_wakeup(dev);
160 /* remove entry if already in list */
161 dev_dbg(&dev->pdev->dev, "remove iamthif and wd from the file list.\n");
162 mei_cl_unlink(&dev->wd_cl);
163 mei_cl_unlink(&dev->iamthif_cl);
164 mei_amthif_reset_params(dev);
165 memset(&dev->wr_ext_msg, 0, sizeof(dev->wr_ext_msg));
169 dev->me_clients_num = 0;
171 dev->wd_pending = false;
173 if (!interrupts_enabled) {
174 dev_dbg(&dev->pdev->dev, "intr not enabled end of reset\n");
175 dev->dev_state = MEI_DEV_DISABLED;
179 ret = mei_hw_start(dev);
181 dev_err(&dev->pdev->dev, "hw_start failed disabling the device\n");
182 dev->dev_state = MEI_DEV_DISABLED;
186 dev_dbg(&dev->pdev->dev, "link is established start sending messages.\n");
187 /* link is established * start sending messages. */
189 dev->dev_state = MEI_DEV_INIT_CLIENTS;
191 ret = mei_hbm_start_req(dev);
193 dev_err(&dev->pdev->dev, "hbm_start failed disabling the device\n");
194 dev->dev_state = MEI_DEV_DISABLED;
198 EXPORT_SYMBOL_GPL(mei_reset);
200 static void mei_reset_work(struct work_struct *work)
202 struct mei_device *dev =
203 container_of(work, struct mei_device, reset_work);
205 mutex_lock(&dev->device_lock);
207 mei_reset(dev, true);
209 mutex_unlock(&dev->device_lock);
212 void mei_stop(struct mei_device *dev)
214 dev_dbg(&dev->pdev->dev, "stopping the device.\n");
216 mei_cancel_work(dev);
218 mei_nfc_host_exit(dev);
220 mutex_lock(&dev->device_lock);
224 dev->dev_state = MEI_DEV_POWER_DOWN;
227 mutex_unlock(&dev->device_lock);
229 mei_watchdog_unregister(dev);
231 EXPORT_SYMBOL_GPL(mei_stop);
235 void mei_device_init(struct mei_device *dev)
237 /* setup our list array */
238 INIT_LIST_HEAD(&dev->file_list);
239 INIT_LIST_HEAD(&dev->device_list);
240 mutex_init(&dev->device_lock);
241 init_waitqueue_head(&dev->wait_hw_ready);
242 init_waitqueue_head(&dev->wait_recvd_msg);
243 init_waitqueue_head(&dev->wait_stop_wd);
244 dev->dev_state = MEI_DEV_INITIALIZING;
246 mei_io_list_init(&dev->read_list);
247 mei_io_list_init(&dev->write_list);
248 mei_io_list_init(&dev->write_waiting_list);
249 mei_io_list_init(&dev->ctrl_wr_list);
250 mei_io_list_init(&dev->ctrl_rd_list);
252 INIT_DELAYED_WORK(&dev->timer_work, mei_timer);
253 INIT_WORK(&dev->init_work, mei_host_client_init);
254 INIT_WORK(&dev->reset_work, mei_reset_work);
256 INIT_LIST_HEAD(&dev->wd_cl.link);
257 INIT_LIST_HEAD(&dev->iamthif_cl.link);
258 mei_io_list_init(&dev->amthif_cmd_list);
259 mei_io_list_init(&dev->amthif_rd_complete_list);
261 bitmap_zero(dev->host_clients_map, MEI_CLIENTS_MAX);
262 dev->open_handle_count = 0;
265 * Reserving the first client ID
266 * 0: Reserved for MEI Bus Message communications
268 bitmap_set(dev->host_clients_map, 0, 1);
270 EXPORT_SYMBOL_GPL(mei_device_init);