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);
46 const char *mei_pg_state_str(enum mei_pg_state state)
48 #define MEI_PG_STATE(state) case MEI_PG_##state: return #state
60 * mei_cancel_work. Cancel mei background jobs
62 * @dev: the device structure
64 * returns 0 on success or < 0 if the reset hasn't succeeded
66 void mei_cancel_work(struct mei_device *dev)
68 cancel_work_sync(&dev->init_work);
69 cancel_work_sync(&dev->reset_work);
71 cancel_delayed_work(&dev->timer_work);
73 EXPORT_SYMBOL_GPL(mei_cancel_work);
76 * mei_reset - resets host and fw.
78 * @dev: the device structure
80 int mei_reset(struct mei_device *dev)
82 enum mei_dev_state state = dev->dev_state;
83 bool interrupts_enabled;
86 if (state != MEI_DEV_INITIALIZING &&
87 state != MEI_DEV_DISABLED &&
88 state != MEI_DEV_POWER_DOWN &&
89 state != MEI_DEV_POWER_UP) {
90 struct mei_fw_status fw_status;
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));
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
105 /* enter reset flow */
106 interrupts_enabled = state != MEI_DEV_POWER_DOWN;
107 dev->dev_state = MEI_DEV_RESETTING;
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;
116 ret = mei_hw_reset(dev, interrupts_enabled);
117 /* fall through and remove the sw state even if hw reset has failed */
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) {
123 /* remove all waiting requests */
124 mei_cl_all_write_clear(dev);
126 mei_cl_all_disconnect(dev);
128 /* wake up all readers and writers so they can be interrupted */
129 mei_cl_all_wakeup(dev);
131 /* remove entry if already in list */
132 dev_dbg(&dev->pdev->dev, "remove iamthif and wd from the file list.\n");
133 mei_cl_unlink(&dev->wd_cl);
134 mei_cl_unlink(&dev->iamthif_cl);
135 mei_amthif_reset_params(dev);
141 dev->wd_pending = false;
144 dev_err(&dev->pdev->dev, "hw_reset failed ret = %d\n", ret);
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;
154 ret = mei_hw_start(dev);
156 dev_err(&dev->pdev->dev, "hw_start failed ret = %d\n", ret);
160 dev_dbg(&dev->pdev->dev, "link is established start sending messages.\n");
162 dev->dev_state = MEI_DEV_INIT_CLIENTS;
163 ret = mei_hbm_start_req(dev);
165 dev_err(&dev->pdev->dev, "hbm_start failed ret = %d\n", ret);
166 dev->dev_state = MEI_DEV_RESETTING;
172 EXPORT_SYMBOL_GPL(mei_reset);
175 * mei_start - initializes host and fw to start work.
177 * @dev: the device structure
179 * returns 0 on success, <0 on failure.
181 int mei_start(struct mei_device *dev)
185 mutex_lock(&dev->device_lock);
187 /* acknowledge interrupt and stop interrupts */
188 mei_clear_interrupts(dev);
192 dev_dbg(&dev->pdev->dev, "reset in start the mei device.\n");
194 dev->reset_count = 0;
196 dev->dev_state = MEI_DEV_INITIALIZING;
197 ret = mei_reset(dev);
199 if (ret == -ENODEV || dev->dev_state == MEI_DEV_DISABLED) {
200 dev_err(&dev->pdev->dev, "reset failed ret = %d", ret);
205 /* we cannot start the device w/o hbm start message completed */
206 if (dev->dev_state == MEI_DEV_DISABLED) {
207 dev_err(&dev->pdev->dev, "reset failed");
211 if (mei_hbm_start_wait(dev)) {
212 dev_err(&dev->pdev->dev, "HBM haven't started");
216 if (!mei_host_is_ready(dev)) {
217 dev_err(&dev->pdev->dev, "host is not ready.\n");
221 if (!mei_hw_is_ready(dev)) {
222 dev_err(&dev->pdev->dev, "ME is not ready.\n");
226 if (!mei_hbm_version_is_supported(dev)) {
227 dev_dbg(&dev->pdev->dev, "MEI start failed.\n");
231 dev_dbg(&dev->pdev->dev, "link layer has been established.\n");
233 mutex_unlock(&dev->device_lock);
236 dev_err(&dev->pdev->dev, "link layer initialization failed.\n");
237 dev->dev_state = MEI_DEV_DISABLED;
238 mutex_unlock(&dev->device_lock);
241 EXPORT_SYMBOL_GPL(mei_start);
244 * mei_restart - restart device after suspend
246 * @dev: the device structure
248 * returns 0 on success or -ENODEV if the restart hasn't succeeded
250 int mei_restart(struct mei_device *dev)
254 mutex_lock(&dev->device_lock);
256 mei_clear_interrupts(dev);
258 dev->dev_state = MEI_DEV_POWER_UP;
259 dev->reset_count = 0;
261 err = mei_reset(dev);
263 mutex_unlock(&dev->device_lock);
265 if (err == -ENODEV || dev->dev_state == MEI_DEV_DISABLED) {
266 dev_err(&dev->pdev->dev, "device disabled = %d\n", err);
270 /* try to start again */
272 schedule_work(&dev->reset_work);
277 EXPORT_SYMBOL_GPL(mei_restart);
279 static void mei_reset_work(struct work_struct *work)
281 struct mei_device *dev =
282 container_of(work, struct mei_device, reset_work);
285 mutex_lock(&dev->device_lock);
287 ret = mei_reset(dev);
289 mutex_unlock(&dev->device_lock);
291 if (dev->dev_state == MEI_DEV_DISABLED) {
292 dev_err(&dev->pdev->dev, "device disabled = %d\n", ret);
296 /* retry reset in case of failure */
298 schedule_work(&dev->reset_work);
301 void mei_stop(struct mei_device *dev)
303 dev_dbg(&dev->pdev->dev, "stopping the device.\n");
305 mei_cancel_work(dev);
307 mei_nfc_host_exit(dev);
309 mei_cl_bus_remove_devices(dev);
311 mutex_lock(&dev->device_lock);
315 dev->dev_state = MEI_DEV_POWER_DOWN;
318 mutex_unlock(&dev->device_lock);
320 mei_watchdog_unregister(dev);
322 EXPORT_SYMBOL_GPL(mei_stop);
325 * mei_write_is_idle - check if the write queues are idle
327 * @dev: the device structure
329 * returns true of there is no pending write
331 bool mei_write_is_idle(struct mei_device *dev)
333 bool idle = (dev->dev_state == MEI_DEV_ENABLED &&
334 list_empty(&dev->ctrl_wr_list.list) &&
335 list_empty(&dev->write_list.list));
337 dev_dbg(&dev->pdev->dev, "write pg: is idle[%d] state=%s ctrl=%d write=%d\n",
339 mei_dev_state_str(dev->dev_state),
340 list_empty(&dev->ctrl_wr_list.list),
341 list_empty(&dev->write_list.list));
345 EXPORT_SYMBOL_GPL(mei_write_is_idle);
347 int mei_fw_status(struct mei_device *dev, struct mei_fw_status *fw_status)
349 const struct mei_fw_status *fw_src = &dev->cfg->fw_status;
356 fw_status->count = fw_src->count;
357 for (i = 0; i < fw_src->count && i < MEI_FW_STATUS_MAX; i++) {
358 ret = pci_read_config_dword(dev->pdev,
359 fw_src->status[i], &fw_status->status[i]);
366 EXPORT_SYMBOL_GPL(mei_fw_status);
368 void mei_device_init(struct mei_device *dev, const struct mei_cfg *cfg)
370 /* setup our list array */
371 INIT_LIST_HEAD(&dev->file_list);
372 INIT_LIST_HEAD(&dev->device_list);
373 INIT_LIST_HEAD(&dev->me_clients);
374 mutex_init(&dev->device_lock);
375 init_waitqueue_head(&dev->wait_hw_ready);
376 init_waitqueue_head(&dev->wait_pg);
377 init_waitqueue_head(&dev->wait_hbm_start);
378 init_waitqueue_head(&dev->wait_stop_wd);
379 dev->dev_state = MEI_DEV_INITIALIZING;
380 dev->reset_count = 0;
382 mei_io_list_init(&dev->read_list);
383 mei_io_list_init(&dev->write_list);
384 mei_io_list_init(&dev->write_waiting_list);
385 mei_io_list_init(&dev->ctrl_wr_list);
386 mei_io_list_init(&dev->ctrl_rd_list);
388 INIT_DELAYED_WORK(&dev->timer_work, mei_timer);
389 INIT_WORK(&dev->init_work, mei_host_client_init);
390 INIT_WORK(&dev->reset_work, mei_reset_work);
392 INIT_LIST_HEAD(&dev->wd_cl.link);
393 INIT_LIST_HEAD(&dev->iamthif_cl.link);
394 mei_io_list_init(&dev->amthif_cmd_list);
395 mei_io_list_init(&dev->amthif_rd_complete_list);
397 bitmap_zero(dev->host_clients_map, MEI_CLIENTS_MAX);
398 dev->open_handle_count = 0;
401 * Reserving the first client ID
402 * 0: Reserved for MEI Bus Message communications
404 bitmap_set(dev->host_clients_map, 0, 1);
406 dev->pg_event = MEI_PG_EVENT_IDLE;
409 EXPORT_SYMBOL_GPL(mei_device_init);