Merge remote-tracking branch 'spi/topic/rspi' into spi-pdata
[deliverable/linux.git] / drivers / misc / mei / init.c
CommitLineData
91f01c6d
OW
1/*
2 *
3 * Intel Management Engine Interface (Intel MEI) Linux driver
733ba91c 4 * Copyright (c) 2003-2012, Intel Corporation.
91f01c6d
OW
5 *
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
40e0b67b 17#include <linux/export.h>
91f01c6d
OW
18#include <linux/pci.h>
19#include <linux/sched.h>
20#include <linux/wait.h>
21#include <linux/delay.h>
22
47a73801
TW
23#include <linux/mei.h>
24
91f01c6d 25#include "mei_dev.h"
aafae7ec 26#include "hbm.h"
90e0b5f1 27#include "client.h"
91f01c6d 28
b210d750
TW
29const 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);
0cfee51c 36 MEI_DEV_STATE(RESETTING);
b210d750 37 MEI_DEV_STATE(DISABLED);
b210d750
TW
38 MEI_DEV_STATE(POWER_DOWN);
39 MEI_DEV_STATE(POWER_UP);
40 default:
8b513d0c 41 return "unknown";
b210d750
TW
42 }
43#undef MEI_DEV_STATE
44}
45
52c34561 46void mei_device_init(struct mei_device *dev)
91f01c6d 47{
91f01c6d 48 /* setup our list array */
91f01c6d 49 INIT_LIST_HEAD(&dev->file_list);
a7b71bc0 50 INIT_LIST_HEAD(&dev->device_list);
91f01c6d 51 mutex_init(&dev->device_lock);
aafae7ec 52 init_waitqueue_head(&dev->wait_hw_ready);
91f01c6d
OW
53 init_waitqueue_head(&dev->wait_recvd_msg);
54 init_waitqueue_head(&dev->wait_stop_wd);
b210d750 55 dev->dev_state = MEI_DEV_INITIALIZING;
0288c7c9
TW
56
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);
d0265f12
TW
62
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
91f01c6d 71}
40e0b67b 72EXPORT_SYMBOL_GPL(mei_device_init);
91f01c6d
OW
73
74/**
c4d589be 75 * mei_start - initializes host and fw to start work.
91f01c6d
OW
76 *
77 * @dev: the device structure
78 *
79 * returns 0 on success, <0 on failure.
80 */
c4d589be 81int mei_start(struct mei_device *dev)
91f01c6d 82{
91f01c6d
OW
83 mutex_lock(&dev->device_lock);
84
91f01c6d 85 /* acknowledge interrupt and stop interupts */
3a65dd4e 86 mei_clear_interrupts(dev);
91f01c6d 87
e7e0c231 88 mei_hw_config(dev);
24aadc80 89
91f01c6d
OW
90 dev_dbg(&dev->pdev->dev, "reset in start the mei device.\n");
91
92 mei_reset(dev, 1);
93
9b0d5efc
TW
94 if (mei_hbm_start_wait(dev)) {
95 dev_err(&dev->pdev->dev, "HBM haven't started");
e7e0c231 96 goto err;
91f01c6d
OW
97 }
98
e7e0c231
TW
99 if (!mei_host_is_ready(dev)) {
100 dev_err(&dev->pdev->dev, "host is not ready.\n");
101 goto err;
102 }
91f01c6d 103
827eef51 104 if (!mei_hw_is_ready(dev)) {
e7e0c231
TW
105 dev_err(&dev->pdev->dev, "ME is not ready.\n");
106 goto err;
91f01c6d
OW
107 }
108
2c9b48ac 109 if (!mei_hbm_version_is_supported(dev)) {
91f01c6d 110 dev_dbg(&dev->pdev->dev, "MEI start failed.\n");
e7e0c231 111 goto err;
91f01c6d
OW
112 }
113
91f01c6d 114 dev_dbg(&dev->pdev->dev, "link layer has been established.\n");
91f01c6d 115
91f01c6d 116 mutex_unlock(&dev->device_lock);
e7e0c231
TW
117 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;
91f01c6d 123}
40e0b67b 124EXPORT_SYMBOL_GPL(mei_start);
91f01c6d 125
91f01c6d
OW
126/**
127 * 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{
91f01c6d 134 bool unexpected;
c20c68d5 135 int ret;
91f01c6d 136
b210d750
TW
137 unexpected = (dev->dev_state != MEI_DEV_INITIALIZING &&
138 dev->dev_state != MEI_DEV_DISABLED &&
139 dev->dev_state != MEI_DEV_POWER_DOWN &&
140 dev->dev_state != MEI_DEV_POWER_UP);
91f01c6d 141
c20c68d5
TW
142 ret = mei_hw_reset(dev, interrupts_enabled);
143 if (ret) {
144 dev_err(&dev->pdev->dev, "hw reset failed disabling the device\n");
145 interrupts_enabled = false;
146 dev->dev_state = MEI_DEV_DISABLED;
147 }
91f01c6d 148
9b0d5efc 149 dev->hbm_state = MEI_HBM_IDLE;
91f01c6d 150
99f22c4e
TW
151 if (dev->dev_state != MEI_DEV_INITIALIZING &&
152 dev->dev_state != MEI_DEV_POWER_UP) {
b210d750
TW
153 if (dev->dev_state != MEI_DEV_DISABLED &&
154 dev->dev_state != MEI_DEV_POWER_DOWN)
0cfee51c 155 dev->dev_state = MEI_DEV_RESETTING;
91f01c6d 156
074b4c01
TW
157 mei_cl_all_disconnect(dev);
158
91f01c6d 159 /* remove entry if already in list */
ff8b2f4e 160 dev_dbg(&dev->pdev->dev, "remove iamthif and wd from the file list.\n");
90e0b5f1 161 mei_cl_unlink(&dev->wd_cl);
781d0d89
TW
162 if (dev->open_handle_count > 0)
163 dev->open_handle_count--;
90e0b5f1 164 mei_cl_unlink(&dev->iamthif_cl);
781d0d89
TW
165 if (dev->open_handle_count > 0)
166 dev->open_handle_count--;
91f01c6d 167
19838fb8 168 mei_amthif_reset_params(dev);
5fb54fb4 169 memset(&dev->wr_ext_msg, 0, sizeof(dev->wr_ext_msg));
91f01c6d
OW
170 }
171
cf9673da 172 dev->me_clients_num = 0;
91f01c6d 173 dev->rd_msg_hdr = 0;
eb9af0ac 174 dev->wd_pending = false;
91f01c6d 175
91f01c6d 176 if (unexpected)
b210d750
TW
177 dev_warn(&dev->pdev->dev, "unexpected reset: dev_state = %s\n",
178 mei_dev_state_str(dev->dev_state));
91f01c6d 179
aafae7ec
TW
180 if (!interrupts_enabled) {
181 dev_dbg(&dev->pdev->dev, "intr not enabled end of reset\n");
182 return;
183 }
184
9049f793
TW
185 ret = mei_hw_start(dev);
186 if (ret) {
187 dev_err(&dev->pdev->dev, "hw_start failed disabling the device\n");
188 dev->dev_state = MEI_DEV_DISABLED;
189 return;
190 }
aafae7ec
TW
191
192 dev_dbg(&dev->pdev->dev, "link is established start sending messages.\n");
193 /* link is established * start sending messages. */
194
195 dev->dev_state = MEI_DEV_INIT_CLIENTS;
196
197 mei_hbm_start_req(dev);
198
074b4c01
TW
199 /* wake up all readings so they can be interrupted */
200 mei_cl_all_read_wakeup(dev);
201
91f01c6d 202 /* remove all waiting requests */
074b4c01 203 mei_cl_all_write_clear(dev);
91f01c6d 204}
40e0b67b 205EXPORT_SYMBOL_GPL(mei_reset);
91f01c6d 206
7cb035d9
TW
207void mei_stop(struct mei_device *dev)
208{
209 dev_dbg(&dev->pdev->dev, "stopping the device.\n");
210
5e85b364
SO
211 flush_scheduled_work();
212
7cb035d9
TW
213 mutex_lock(&dev->device_lock);
214
215 cancel_delayed_work(&dev->timer_work);
216
217 mei_wd_stop(dev);
218
59fcd7c6
SO
219 mei_nfc_host_exit();
220
7cb035d9
TW
221 dev->dev_state = MEI_DEV_POWER_DOWN;
222 mei_reset(dev, 0);
223
224 mutex_unlock(&dev->device_lock);
225
2e647124 226 mei_watchdog_unregister(dev);
7cb035d9 227}
40e0b67b 228EXPORT_SYMBOL_GPL(mei_stop);
91f01c6d 229
c1174c0e 230
91f01c6d 231
This page took 0.165401 seconds and 5 git commands to generate.