blob: 79e9e1c3056295f6ab68b98ff3294bcfc1fe39d8 [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:
41 return "unkown";
42 }
43#undef MEI_DEV_STATE
44}
45
Tomas Winkler52c34562013-02-06 14:06:40 +020046void mei_device_init(struct mei_device *dev)
Oren Weil91f01c62011-05-15 13:43:44 +030047{
Oren Weil91f01c62011-05-15 13:43:44 +030048 /* setup our list array */
Oren Weil91f01c62011-05-15 13:43:44 +030049 INIT_LIST_HEAD(&dev->file_list);
Samuel Ortiza7b71bc2013-03-27 17:29:56 +020050 INIT_LIST_HEAD(&dev->device_list);
Oren Weil91f01c62011-05-15 13:43:44 +030051 mutex_init(&dev->device_lock);
Tomas Winkleraafae7e2013-03-11 18:27:03 +020052 init_waitqueue_head(&dev->wait_hw_ready);
Oren Weil91f01c62011-05-15 13:43:44 +030053 init_waitqueue_head(&dev->wait_recvd_msg);
54 init_waitqueue_head(&dev->wait_stop_wd);
Tomas Winklerb210d752012-08-07 00:03:56 +030055 dev->dev_state = MEI_DEV_INITIALIZING;
Tomas Winkler0288c7c2011-06-06 10:44:34 +030056
57 mei_io_list_init(&dev->read_list);
58 mei_io_list_init(&dev->write_list);
59 mei_io_list_init(&dev->write_waiting_list);
60 mei_io_list_init(&dev->ctrl_wr_list);
61 mei_io_list_init(&dev->ctrl_rd_list);
Tomas Winklerd0265f12013-03-27 16:58:27 +020062
63 INIT_DELAYED_WORK(&dev->timer_work, mei_timer);
64 INIT_WORK(&dev->init_work, mei_host_client_init);
65
66 INIT_LIST_HEAD(&dev->wd_cl.link);
67 INIT_LIST_HEAD(&dev->iamthif_cl.link);
68 mei_io_list_init(&dev->amthif_cmd_list);
69 mei_io_list_init(&dev->amthif_rd_complete_list);
70
Oren Weil91f01c62011-05-15 13:43:44 +030071}
Tomas Winkler40e0b672013-03-27 16:58:30 +020072EXPORT_SYMBOL_GPL(mei_device_init);
Oren Weil91f01c62011-05-15 13:43:44 +030073
74/**
Tomas Winklerc4d589b2013-03-27 16:58:28 +020075 * mei_start - initializes host and fw to start work.
Oren Weil91f01c62011-05-15 13:43:44 +030076 *
77 * @dev: the device structure
78 *
79 * returns 0 on success, <0 on failure.
80 */
Tomas Winklerc4d589b2013-03-27 16:58:28 +020081int mei_start(struct mei_device *dev)
Oren Weil91f01c62011-05-15 13:43:44 +030082{
Oren Weil91f01c62011-05-15 13:43:44 +030083 mutex_lock(&dev->device_lock);
84
Oren Weil91f01c62011-05-15 13:43:44 +030085 /* acknowledge interrupt and stop interupts */
Tomas Winkler3a65dd42012-12-25 19:06:06 +020086 mei_clear_interrupts(dev);
Oren Weil91f01c62011-05-15 13:43:44 +030087
Tomas Winklere7e0c232013-01-08 23:07:31 +020088 mei_hw_config(dev);
Tomas Winkler24aadc82012-06-25 23:46:27 +030089
Oren Weil91f01c62011-05-15 13:43:44 +030090 dev_dbg(&dev->pdev->dev, "reset in start the mei device.\n");
91
92 mei_reset(dev, 1);
93
Tomas Winkler9b0d5ef2013-04-18 23:03:48 +030094 if (mei_hbm_start_wait(dev)) {
95 dev_err(&dev->pdev->dev, "HBM haven't started");
Tomas Winklere7e0c232013-01-08 23:07:31 +020096 goto err;
Oren Weil91f01c62011-05-15 13:43:44 +030097 }
98
Tomas Winklere7e0c232013-01-08 23:07:31 +020099 if (!mei_host_is_ready(dev)) {
100 dev_err(&dev->pdev->dev, "host is not ready.\n");
101 goto err;
102 }
Oren Weil91f01c62011-05-15 13:43:44 +0300103
Tomas Winkler827eef52013-02-06 14:06:41 +0200104 if (!mei_hw_is_ready(dev)) {
Tomas Winklere7e0c232013-01-08 23:07:31 +0200105 dev_err(&dev->pdev->dev, "ME is not ready.\n");
106 goto err;
Oren Weil91f01c62011-05-15 13:43:44 +0300107 }
108
Tomas Winkler2c9b48a2013-06-16 09:16:31 +0300109 if (!mei_hbm_version_is_supported(dev)) {
Oren Weil91f01c62011-05-15 13:43:44 +0300110 dev_dbg(&dev->pdev->dev, "MEI start failed.\n");
Tomas Winklere7e0c232013-01-08 23:07:31 +0200111 goto err;
Oren Weil91f01c62011-05-15 13:43:44 +0300112 }
113
Oren Weil91f01c62011-05-15 13:43:44 +0300114 dev_dbg(&dev->pdev->dev, "link layer has been established.\n");
Oren Weil91f01c62011-05-15 13:43:44 +0300115
Oren Weil91f01c62011-05-15 13:43:44 +0300116 mutex_unlock(&dev->device_lock);
Tomas Winklere7e0c232013-01-08 23:07:31 +0200117 return 0;
118err:
119 dev_err(&dev->pdev->dev, "link layer initialization failed.\n");
120 dev->dev_state = MEI_DEV_DISABLED;
121 mutex_unlock(&dev->device_lock);
122 return -ENODEV;
Oren Weil91f01c62011-05-15 13:43:44 +0300123}
Tomas Winkler40e0b672013-03-27 16:58:30 +0200124EXPORT_SYMBOL_GPL(mei_start);
Oren Weil91f01c62011-05-15 13:43:44 +0300125
126/**
Oren Weil91f01c62011-05-15 13:43:44 +0300127 * mei_reset - resets host and fw.
128 *
129 * @dev: the device structure
130 * @interrupts_enabled: if interrupt should be enabled after reset.
131 */
132void mei_reset(struct mei_device *dev, int interrupts_enabled)
133{
Oren Weil91f01c62011-05-15 13:43:44 +0300134 bool unexpected;
135
Tomas Winklerb210d752012-08-07 00:03:56 +0300136 unexpected = (dev->dev_state != MEI_DEV_INITIALIZING &&
137 dev->dev_state != MEI_DEV_DISABLED &&
138 dev->dev_state != MEI_DEV_POWER_DOWN &&
139 dev->dev_state != MEI_DEV_POWER_UP);
Oren Weil91f01c62011-05-15 13:43:44 +0300140
Oren Weil91f01c62011-05-15 13:43:44 +0300141 mei_hw_reset(dev, interrupts_enabled);
142
Tomas Winkler9b0d5ef2013-04-18 23:03:48 +0300143 dev->hbm_state = MEI_HBM_IDLE;
Oren Weil91f01c62011-05-15 13:43:44 +0300144
Tomas Winklerb210d752012-08-07 00:03:56 +0300145 if (dev->dev_state != MEI_DEV_INITIALIZING) {
146 if (dev->dev_state != MEI_DEV_DISABLED &&
147 dev->dev_state != MEI_DEV_POWER_DOWN)
Bill Nottingham0cfee512013-04-19 22:01:36 +0300148 dev->dev_state = MEI_DEV_RESETTING;
Oren Weil91f01c62011-05-15 13:43:44 +0300149
Tomas Winkler074b4c02013-02-06 14:06:44 +0200150 mei_cl_all_disconnect(dev);
151
Oren Weil91f01c62011-05-15 13:43:44 +0300152 /* remove entry if already in list */
Tomas Winklerff8b2f42012-11-11 17:38:03 +0200153 dev_dbg(&dev->pdev->dev, "remove iamthif and wd from the file list.\n");
Tomas Winkler90e0b5f12013-01-08 23:07:14 +0200154 mei_cl_unlink(&dev->wd_cl);
Tomas Winkler781d0d82013-01-08 23:07:22 +0200155 if (dev->open_handle_count > 0)
156 dev->open_handle_count--;
Tomas Winkler90e0b5f12013-01-08 23:07:14 +0200157 mei_cl_unlink(&dev->iamthif_cl);
Tomas Winkler781d0d82013-01-08 23:07:22 +0200158 if (dev->open_handle_count > 0)
159 dev->open_handle_count--;
Oren Weil91f01c62011-05-15 13:43:44 +0300160
Tomas Winkler19838fb2012-11-01 21:17:15 +0200161 mei_amthif_reset_params(dev);
Tomas Winkler5fb54fb2012-11-18 15:13:15 +0200162 memset(&dev->wr_ext_msg, 0, sizeof(dev->wr_ext_msg));
Oren Weil91f01c62011-05-15 13:43:44 +0300163 }
164
Tomas Winklercf9673d2011-06-06 10:44:33 +0300165 dev->me_clients_num = 0;
Oren Weil91f01c62011-05-15 13:43:44 +0300166 dev->rd_msg_hdr = 0;
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300167 dev->wd_pending = false;
Oren Weil91f01c62011-05-15 13:43:44 +0300168
Oren Weil91f01c62011-05-15 13:43:44 +0300169 if (unexpected)
Tomas Winklerb210d752012-08-07 00:03:56 +0300170 dev_warn(&dev->pdev->dev, "unexpected reset: dev_state = %s\n",
171 mei_dev_state_str(dev->dev_state));
Oren Weil91f01c62011-05-15 13:43:44 +0300172
Tomas Winkleraafae7e2013-03-11 18:27:03 +0200173 if (!interrupts_enabled) {
174 dev_dbg(&dev->pdev->dev, "intr not enabled end of reset\n");
175 return;
176 }
177
178 mei_hw_start(dev);
179
180 dev_dbg(&dev->pdev->dev, "link is established start sending messages.\n");
181 /* link is established * start sending messages. */
182
183 dev->dev_state = MEI_DEV_INIT_CLIENTS;
184
185 mei_hbm_start_req(dev);
186
Tomas Winkler074b4c02013-02-06 14:06:44 +0200187 /* wake up all readings so they can be interrupted */
188 mei_cl_all_read_wakeup(dev);
189
Oren Weil91f01c62011-05-15 13:43:44 +0300190 /* remove all waiting requests */
Tomas Winkler074b4c02013-02-06 14:06:44 +0200191 mei_cl_all_write_clear(dev);
Oren Weil91f01c62011-05-15 13:43:44 +0300192}
Tomas Winkler40e0b672013-03-27 16:58:30 +0200193EXPORT_SYMBOL_GPL(mei_reset);
Oren Weil91f01c62011-05-15 13:43:44 +0300194
Tomas Winkler7cb035d2013-03-10 13:56:08 +0200195void mei_stop(struct mei_device *dev)
196{
197 dev_dbg(&dev->pdev->dev, "stopping the device.\n");
198
Samuel Ortiz5e85b362013-06-10 10:10:25 +0300199 flush_scheduled_work();
200
Tomas Winkler7cb035d2013-03-10 13:56:08 +0200201 mutex_lock(&dev->device_lock);
202
203 cancel_delayed_work(&dev->timer_work);
204
205 mei_wd_stop(dev);
206
Samuel Ortiz59fcd7c2013-04-11 03:03:29 +0200207 mei_nfc_host_exit();
208
Tomas Winkler7cb035d2013-03-10 13:56:08 +0200209 dev->dev_state = MEI_DEV_POWER_DOWN;
210 mei_reset(dev, 0);
211
212 mutex_unlock(&dev->device_lock);
213
Tomas Winkler2e647122013-03-27 16:58:26 +0200214 mei_watchdog_unregister(dev);
Tomas Winkler7cb035d2013-03-10 13:56:08 +0200215}
Tomas Winkler40e0b672013-03-27 16:58:30 +0200216EXPORT_SYMBOL_GPL(mei_stop);
Oren Weil91f01c62011-05-15 13:43:44 +0300217
Samuel Ortizc1174c02012-11-18 15:13:20 +0200218
Oren Weil91f01c62011-05-15 13:43:44 +0300219