blob: 87c077bae5d8fab852f216df366394b68e627350 [file] [log] [blame]
Oren Weil91f01c62011-05-15 13:43:44 +03001/*
2 *
3 * Intel Management Engine Interface (Intel MEI) Linux driver
Tomas Winkler733ba912012-02-09 19:25:53 +02004 * Copyright (c) 2003-2012, Intel Corporation.
Oren Weil91f01c62011-05-15 13:43:44 +03005 *
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.
9 *
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
13 * more details.
14 *
15 */
16
Tomas Winkler40e0b672013-03-27 16:58:30 +020017#include <linux/export.h>
Oren Weil91f01c62011-05-15 13:43:44 +030018#include <linux/pci.h>
19#include <linux/sched.h>
20#include <linux/wait.h>
21#include <linux/delay.h>
22
Tomas Winkler4f3afe12012-05-09 16:38:59 +030023#include <linux/mei.h>
Oren Weil91f01c62011-05-15 13:43:44 +030024
Tomas Winkler47a73802012-12-25 19:06:03 +020025#include "mei_dev.h"
Tomas Winkleraafae7e2013-03-11 18:27:03 +020026#include "hbm.h"
Tomas Winkler90e0b5f12013-01-08 23:07:14 +020027#include "client.h"
Tomas Winkler47a73802012-12-25 19:06:03 +020028
Tomas Winklerb210d752012-08-07 00:03:56 +030029const char *mei_dev_state_str(int state)
30{
31#define MEI_DEV_STATE(state) case MEI_DEV_##state: return #state
32 switch (state) {
33 MEI_DEV_STATE(INITIALIZING);
34 MEI_DEV_STATE(INIT_CLIENTS);
35 MEI_DEV_STATE(ENABLED);
Bill Nottingham0cfee512013-04-19 22:01:36 +030036 MEI_DEV_STATE(RESETTING);
Tomas Winklerb210d752012-08-07 00:03:56 +030037 MEI_DEV_STATE(DISABLED);
Tomas Winklerb210d752012-08-07 00:03:56 +030038 MEI_DEV_STATE(POWER_DOWN);
39 MEI_DEV_STATE(POWER_UP);
40 default:
Masanari Iida8b513d02013-05-21 23:13:12 +090041 return "unknown";
Tomas Winklerb210d752012-08-07 00:03:56 +030042 }
43#undef MEI_DEV_STATE
44}
45
Oren Weil91f01c62011-05-15 13:43:44 +030046/**
Tomas Winklerc4d589b2013-03-27 16:58:28 +020047 * mei_start - initializes host and fw to start work.
Oren Weil91f01c62011-05-15 13:43:44 +030048 *
49 * @dev: the device structure
50 *
51 * returns 0 on success, <0 on failure.
52 */
Tomas Winklerc4d589b2013-03-27 16:58:28 +020053int mei_start(struct mei_device *dev)
Oren Weil91f01c62011-05-15 13:43:44 +030054{
Oren Weil91f01c62011-05-15 13:43:44 +030055 mutex_lock(&dev->device_lock);
56
Oren Weil91f01c62011-05-15 13:43:44 +030057 /* acknowledge interrupt and stop interupts */
Tomas Winkler3a65dd42012-12-25 19:06:06 +020058 mei_clear_interrupts(dev);
Oren Weil91f01c62011-05-15 13:43:44 +030059
Tomas Winklere7e0c232013-01-08 23:07:31 +020060 mei_hw_config(dev);
Tomas Winkler24aadc82012-06-25 23:46:27 +030061
Oren Weil91f01c62011-05-15 13:43:44 +030062 dev_dbg(&dev->pdev->dev, "reset in start the mei device.\n");
63
64 mei_reset(dev, 1);
65
Tomas Winkler9b0d5ef2013-04-18 23:03:48 +030066 if (mei_hbm_start_wait(dev)) {
67 dev_err(&dev->pdev->dev, "HBM haven't started");
Tomas Winklere7e0c232013-01-08 23:07:31 +020068 goto err;
Oren Weil91f01c62011-05-15 13:43:44 +030069 }
70
Tomas Winklere7e0c232013-01-08 23:07:31 +020071 if (!mei_host_is_ready(dev)) {
72 dev_err(&dev->pdev->dev, "host is not ready.\n");
73 goto err;
74 }
Oren Weil91f01c62011-05-15 13:43:44 +030075
Tomas Winkler827eef52013-02-06 14:06:41 +020076 if (!mei_hw_is_ready(dev)) {
Tomas Winklere7e0c232013-01-08 23:07:31 +020077 dev_err(&dev->pdev->dev, "ME is not ready.\n");
78 goto err;
Oren Weil91f01c62011-05-15 13:43:44 +030079 }
80
Tomas Winkler2c9b48a2013-06-16 09:16:31 +030081 if (!mei_hbm_version_is_supported(dev)) {
Oren Weil91f01c62011-05-15 13:43:44 +030082 dev_dbg(&dev->pdev->dev, "MEI start failed.\n");
Tomas Winklere7e0c232013-01-08 23:07:31 +020083 goto err;
Oren Weil91f01c62011-05-15 13:43:44 +030084 }
85
Oren Weil91f01c62011-05-15 13:43:44 +030086 dev_dbg(&dev->pdev->dev, "link layer has been established.\n");
Oren Weil91f01c62011-05-15 13:43:44 +030087
Oren Weil91f01c62011-05-15 13:43:44 +030088 mutex_unlock(&dev->device_lock);
Tomas Winklere7e0c232013-01-08 23:07:31 +020089 return 0;
90err:
91 dev_err(&dev->pdev->dev, "link layer initialization failed.\n");
92 dev->dev_state = MEI_DEV_DISABLED;
93 mutex_unlock(&dev->device_lock);
94 return -ENODEV;
Oren Weil91f01c62011-05-15 13:43:44 +030095}
Tomas Winkler40e0b672013-03-27 16:58:30 +020096EXPORT_SYMBOL_GPL(mei_start);
Oren Weil91f01c62011-05-15 13:43:44 +030097
Tomas Winkler544f9462014-01-08 20:19:21 +020098/**
99 * mei_cancel_work. Cancel mei background jobs
100 *
101 * @dev: the device structure
102 */
Tomas Winklerdc844b02013-11-11 13:26:06 +0200103void mei_cancel_work(struct mei_device *dev)
104{
105 cancel_work_sync(&dev->init_work);
Tomas Winkler544f9462014-01-08 20:19:21 +0200106 cancel_work_sync(&dev->reset_work);
Tomas Winklerdc844b02013-11-11 13:26:06 +0200107
108 cancel_delayed_work(&dev->timer_work);
109}
110EXPORT_SYMBOL_GPL(mei_cancel_work);
111
Oren Weil91f01c62011-05-15 13:43:44 +0300112/**
Oren Weil91f01c62011-05-15 13:43:44 +0300113 * mei_reset - resets host and fw.
114 *
115 * @dev: the device structure
116 * @interrupts_enabled: if interrupt should be enabled after reset.
117 */
118void mei_reset(struct mei_device *dev, int interrupts_enabled)
119{
Oren Weil91f01c62011-05-15 13:43:44 +0300120 bool unexpected;
Tomas Winklerc20c68d2013-06-23 10:42:49 +0300121 int ret;
Oren Weil91f01c62011-05-15 13:43:44 +0300122
Tomas Winklerb210d752012-08-07 00:03:56 +0300123 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);
Oren Weil91f01c62011-05-15 13:43:44 +0300127
Alexander Usyskinf931f4f2013-10-21 22:05:43 +0300128 if (unexpected)
129 dev_warn(&dev->pdev->dev, "unexpected reset: dev_state = %s\n",
130 mei_dev_state_str(dev->dev_state));
131
Tomas Winklerc20c68d2013-06-23 10:42:49 +0300132 ret = mei_hw_reset(dev, interrupts_enabled);
133 if (ret) {
134 dev_err(&dev->pdev->dev, "hw reset failed disabling the device\n");
135 interrupts_enabled = false;
136 dev->dev_state = MEI_DEV_DISABLED;
137 }
Oren Weil91f01c62011-05-15 13:43:44 +0300138
Tomas Winkler9b0d5ef2013-04-18 23:03:48 +0300139 dev->hbm_state = MEI_HBM_IDLE;
Oren Weil91f01c62011-05-15 13:43:44 +0300140
Tomas Winkler99f22c42013-07-17 15:13:16 +0300141 if (dev->dev_state != MEI_DEV_INITIALIZING &&
142 dev->dev_state != MEI_DEV_POWER_UP) {
Tomas Winklerb210d752012-08-07 00:03:56 +0300143 if (dev->dev_state != MEI_DEV_DISABLED &&
144 dev->dev_state != MEI_DEV_POWER_DOWN)
Bill Nottingham0cfee512013-04-19 22:01:36 +0300145 dev->dev_state = MEI_DEV_RESETTING;
Oren Weil91f01c62011-05-15 13:43:44 +0300146
Tomas Winklerb950ac12013-07-25 20:15:53 +0300147 /* remove all waiting requests */
148 mei_cl_all_write_clear(dev);
149
Tomas Winkler074b4c02013-02-06 14:06:44 +0200150 mei_cl_all_disconnect(dev);
151
Tomas Winklerb950ac12013-07-25 20:15:53 +0300152 /* wake up all readings so they can be interrupted */
153 mei_cl_all_wakeup(dev);
154
Oren Weil91f01c62011-05-15 13:43:44 +0300155 /* remove entry if already in list */
Tomas Winklerff8b2f42012-11-11 17:38:03 +0200156 dev_dbg(&dev->pdev->dev, "remove iamthif and wd from the file list.\n");
Tomas Winkler90e0b5f12013-01-08 23:07:14 +0200157 mei_cl_unlink(&dev->wd_cl);
158 mei_cl_unlink(&dev->iamthif_cl);
Tomas Winkler19838fb2012-11-01 21:17:15 +0200159 mei_amthif_reset_params(dev);
Tomas Winkler5fb54fb2012-11-18 15:13:15 +0200160 memset(&dev->wr_ext_msg, 0, sizeof(dev->wr_ext_msg));
Oren Weil91f01c62011-05-15 13:43:44 +0300161 }
162
Alexander Usyskin4a704572013-09-02 13:29:47 +0300163 /* we're already in reset, cancel the init timer */
164 dev->init_clients_timer = 0;
165
Tomas Winklercf9673d2011-06-06 10:44:33 +0300166 dev->me_clients_num = 0;
Oren Weil91f01c62011-05-15 13:43:44 +0300167 dev->rd_msg_hdr = 0;
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300168 dev->wd_pending = false;
Oren Weil91f01c62011-05-15 13:43:44 +0300169
Tomas Winkleraafae7e2013-03-11 18:27:03 +0200170 if (!interrupts_enabled) {
171 dev_dbg(&dev->pdev->dev, "intr not enabled end of reset\n");
172 return;
173 }
174
Tomas Winkler9049f792013-06-23 22:49:04 +0300175 ret = mei_hw_start(dev);
176 if (ret) {
177 dev_err(&dev->pdev->dev, "hw_start failed disabling the device\n");
178 dev->dev_state = MEI_DEV_DISABLED;
179 return;
180 }
Tomas Winkleraafae7e2013-03-11 18:27:03 +0200181
182 dev_dbg(&dev->pdev->dev, "link is established start sending messages.\n");
183 /* link is established * start sending messages. */
184
185 dev->dev_state = MEI_DEV_INIT_CLIENTS;
186
Tomas Winkler544f9462014-01-08 20:19:21 +0200187 ret = mei_hbm_start_req(dev);
188 if (ret) {
189 dev_err(&dev->pdev->dev, "hbm_start failed disabling the device\n");
190 dev->dev_state = MEI_DEV_DISABLED;
191 return;
192 }
Oren Weil91f01c62011-05-15 13:43:44 +0300193}
Tomas Winkler40e0b672013-03-27 16:58:30 +0200194EXPORT_SYMBOL_GPL(mei_reset);
Oren Weil91f01c62011-05-15 13:43:44 +0300195
Tomas Winkler544f9462014-01-08 20:19:21 +0200196static void mei_reset_work(struct work_struct *work)
197{
198 struct mei_device *dev =
199 container_of(work, struct mei_device, reset_work);
200
201 mutex_lock(&dev->device_lock);
202
203 mei_reset(dev, true);
204
205 mutex_unlock(&dev->device_lock);
206}
207
Tomas Winkler7cb035d2013-03-10 13:56:08 +0200208void mei_stop(struct mei_device *dev)
209{
210 dev_dbg(&dev->pdev->dev, "stopping the device.\n");
211
Tomas Winklerdc844b02013-11-11 13:26:06 +0200212 mei_cancel_work(dev);
213
214 mei_nfc_host_exit(dev);
Samuel Ortiz5e85b362013-06-10 10:10:25 +0300215
Tomas Winkler7cb035d2013-03-10 13:56:08 +0200216 mutex_lock(&dev->device_lock);
217
Tomas Winkler7cb035d2013-03-10 13:56:08 +0200218 mei_wd_stop(dev);
219
220 dev->dev_state = MEI_DEV_POWER_DOWN;
221 mei_reset(dev, 0);
222
223 mutex_unlock(&dev->device_lock);
224
Tomas Winkler2e647122013-03-27 16:58:26 +0200225 mei_watchdog_unregister(dev);
Tomas Winkler7cb035d2013-03-10 13:56:08 +0200226}
Tomas Winkler40e0b672013-03-27 16:58:30 +0200227EXPORT_SYMBOL_GPL(mei_stop);
Oren Weil91f01c62011-05-15 13:43:44 +0300228
Samuel Ortizc1174c02012-11-18 15:13:20 +0200229
Oren Weil91f01c62011-05-15 13:43:44 +0300230
Tomas Winkler544f9462014-01-08 20:19:21 +0200231void mei_device_init(struct mei_device *dev)
232{
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;
241
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);
247
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);
251
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);
256
257 bitmap_zero(dev->host_clients_map, MEI_CLIENTS_MAX);
258 dev->open_handle_count = 0;
259
260 /*
261 * Reserving the first client ID
262 * 0: Reserved for MEI Bus Message communications
263 */
264 bitmap_set(dev->host_clients_map, 0, 1);
265}
266EXPORT_SYMBOL_GPL(mei_device_init);
267