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 ret = mei_hw_reset(dev, interrupts_enabled);
134 dev_err(&dev->pdev->dev, "hw reset failed disabling the device\n");
135 interrupts_enabled = false;
136 dev->dev_state = MEI_DEV_DISABLED;
139 dev->hbm_state = MEI_HBM_IDLE;
141 if (dev->dev_state != MEI_DEV_INITIALIZING &&
142 dev->dev_state != MEI_DEV_POWER_UP) {
143 if (dev->dev_state != MEI_DEV_DISABLED &&
144 dev->dev_state != MEI_DEV_POWER_DOWN)
145 dev->dev_state = MEI_DEV_RESETTING;
147 /* remove all waiting requests */
148 mei_cl_all_write_clear(dev);
150 mei_cl_all_disconnect(dev);
152 /* wake up all readings so they can be interrupted */
153 mei_cl_all_wakeup(dev);
155 /* remove entry if already in list */
156 dev_dbg(&dev->pdev->dev, "remove iamthif and wd from the file list.\n");
157 mei_cl_unlink(&dev->wd_cl);
158 mei_cl_unlink(&dev->iamthif_cl);
159 mei_amthif_reset_params(dev);
160 memset(&dev->wr_ext_msg, 0, sizeof(dev->wr_ext_msg));
163 /* we're already in reset, cancel the init timer */
164 dev->init_clients_timer = 0;
166 dev->me_clients_num = 0;
168 dev->wd_pending = false;
170 if (!interrupts_enabled) {
171 dev_dbg(&dev->pdev->dev, "intr not enabled end of reset\n");
175 ret = mei_hw_start(dev);
177 dev_err(&dev->pdev->dev, "hw_start failed disabling the device\n");
178 dev->dev_state = MEI_DEV_DISABLED;
182 dev_dbg(&dev->pdev->dev, "link is established start sending messages.\n");
183 /* link is established * start sending messages. */
185 dev->dev_state = MEI_DEV_INIT_CLIENTS;
187 ret = mei_hbm_start_req(dev);
189 dev_err(&dev->pdev->dev, "hbm_start failed disabling the device\n");
190 dev->dev_state = MEI_DEV_DISABLED;
194 EXPORT_SYMBOL_GPL(mei_reset);
196 static void mei_reset_work(struct work_struct *work)
198 struct mei_device *dev =
199 container_of(work, struct mei_device, reset_work);
201 mutex_lock(&dev->device_lock);
203 mei_reset(dev, true);
205 mutex_unlock(&dev->device_lock);
208 void mei_stop(struct mei_device *dev)
210 dev_dbg(&dev->pdev->dev, "stopping the device.\n");
212 mei_cancel_work(dev);
214 mei_nfc_host_exit(dev);
216 mutex_lock(&dev->device_lock);
220 dev->dev_state = MEI_DEV_POWER_DOWN;
223 mutex_unlock(&dev->device_lock);
225 mei_watchdog_unregister(dev);
227 EXPORT_SYMBOL_GPL(mei_stop);
231 void mei_device_init(struct mei_device *dev)
233 /* setup our list array */
234 INIT_LIST_HEAD(&dev->file_list);
235 INIT_LIST_HEAD(&dev->device_list);
236 mutex_init(&dev->device_lock);
237 init_waitqueue_head(&dev->wait_hw_ready);
238 init_waitqueue_head(&dev->wait_recvd_msg);
239 init_waitqueue_head(&dev->wait_stop_wd);
240 dev->dev_state = MEI_DEV_INITIALIZING;
242 mei_io_list_init(&dev->read_list);
243 mei_io_list_init(&dev->write_list);
244 mei_io_list_init(&dev->write_waiting_list);
245 mei_io_list_init(&dev->ctrl_wr_list);
246 mei_io_list_init(&dev->ctrl_rd_list);
248 INIT_DELAYED_WORK(&dev->timer_work, mei_timer);
249 INIT_WORK(&dev->init_work, mei_host_client_init);
250 INIT_WORK(&dev->reset_work, mei_reset_work);
252 INIT_LIST_HEAD(&dev->wd_cl.link);
253 INIT_LIST_HEAD(&dev->iamthif_cl.link);
254 mei_io_list_init(&dev->amthif_cmd_list);
255 mei_io_list_init(&dev->amthif_rd_complete_list);
257 bitmap_zero(dev->host_clients_map, MEI_CLIENTS_MAX);
258 dev->open_handle_count = 0;
261 * Reserving the first client ID
262 * 0: Reserved for MEI Bus Message communications
264 bitmap_set(dev->host_clients_map, 0, 1);
266 EXPORT_SYMBOL_GPL(mei_device_init);