Commit | Line | Data |
---|---|---|
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 |
29 | const 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); | |
36 | MEI_DEV_STATE(RESETING); | |
37 | MEI_DEV_STATE(DISABLED); | |
b210d750 TW |
38 | MEI_DEV_STATE(POWER_DOWN); |
39 | MEI_DEV_STATE(POWER_UP); | |
40 | default: | |
41 | return "unkown"; | |
42 | } | |
43 | #undef MEI_DEV_STATE | |
44 | } | |
45 | ||
52c34561 | 46 | void 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 | 72 | EXPORT_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 | 81 | int mei_start(struct mei_device *dev) |
91f01c6d | 82 | { |
e7e0c231 | 83 | int ret = 0; |
91f01c6d OW |
84 | |
85 | mutex_lock(&dev->device_lock); | |
86 | ||
91f01c6d | 87 | /* acknowledge interrupt and stop interupts */ |
3a65dd4e | 88 | mei_clear_interrupts(dev); |
91f01c6d | 89 | |
e7e0c231 | 90 | mei_hw_config(dev); |
24aadc80 | 91 | |
eb9af0ac | 92 | dev->recvd_msg = false; |
91f01c6d OW |
93 | dev_dbg(&dev->pdev->dev, "reset in start the mei device.\n"); |
94 | ||
95 | mei_reset(dev, 1); | |
96 | ||
91f01c6d OW |
97 | /* wait for ME to turn on ME_RDY */ |
98 | if (!dev->recvd_msg) { | |
99 | mutex_unlock(&dev->device_lock); | |
e7e0c231 | 100 | ret = wait_event_interruptible_timeout(dev->wait_recvd_msg, |
3870c320 TW |
101 | dev->recvd_msg, |
102 | mei_secs_to_jiffies(MEI_INTEROP_TIMEOUT)); | |
91f01c6d OW |
103 | mutex_lock(&dev->device_lock); |
104 | } | |
105 | ||
e7e0c231 | 106 | if (ret <= 0 && !dev->recvd_msg) { |
b210d750 | 107 | dev->dev_state = MEI_DEV_DISABLED; |
91f01c6d OW |
108 | dev_dbg(&dev->pdev->dev, |
109 | "wait_event_interruptible_timeout failed" | |
110 | "on wait for ME to turn on ME_RDY.\n"); | |
e7e0c231 | 111 | goto err; |
91f01c6d OW |
112 | } |
113 | ||
91f01c6d | 114 | |
e7e0c231 TW |
115 | if (!mei_host_is_ready(dev)) { |
116 | dev_err(&dev->pdev->dev, "host is not ready.\n"); | |
117 | goto err; | |
118 | } | |
91f01c6d | 119 | |
827eef51 | 120 | if (!mei_hw_is_ready(dev)) { |
e7e0c231 TW |
121 | dev_err(&dev->pdev->dev, "ME is not ready.\n"); |
122 | goto err; | |
91f01c6d OW |
123 | } |
124 | ||
125 | if (dev->version.major_version != HBM_MAJOR_VERSION || | |
126 | dev->version.minor_version != HBM_MINOR_VERSION) { | |
127 | dev_dbg(&dev->pdev->dev, "MEI start failed.\n"); | |
e7e0c231 | 128 | goto err; |
91f01c6d OW |
129 | } |
130 | ||
eb9af0ac | 131 | dev->recvd_msg = false; |
91f01c6d | 132 | dev_dbg(&dev->pdev->dev, "link layer has been established.\n"); |
91f01c6d | 133 | |
91f01c6d | 134 | mutex_unlock(&dev->device_lock); |
e7e0c231 TW |
135 | return 0; |
136 | err: | |
137 | dev_err(&dev->pdev->dev, "link layer initialization failed.\n"); | |
138 | dev->dev_state = MEI_DEV_DISABLED; | |
139 | mutex_unlock(&dev->device_lock); | |
140 | return -ENODEV; | |
91f01c6d | 141 | } |
40e0b67b | 142 | EXPORT_SYMBOL_GPL(mei_start); |
91f01c6d | 143 | |
91f01c6d OW |
144 | /** |
145 | * mei_reset - resets host and fw. | |
146 | * | |
147 | * @dev: the device structure | |
148 | * @interrupts_enabled: if interrupt should be enabled after reset. | |
149 | */ | |
150 | void mei_reset(struct mei_device *dev, int interrupts_enabled) | |
151 | { | |
91f01c6d OW |
152 | bool unexpected; |
153 | ||
b210d750 TW |
154 | unexpected = (dev->dev_state != MEI_DEV_INITIALIZING && |
155 | dev->dev_state != MEI_DEV_DISABLED && | |
156 | dev->dev_state != MEI_DEV_POWER_DOWN && | |
157 | dev->dev_state != MEI_DEV_POWER_UP); | |
91f01c6d | 158 | |
91f01c6d OW |
159 | mei_hw_reset(dev, interrupts_enabled); |
160 | ||
91f01c6d | 161 | |
b210d750 TW |
162 | if (dev->dev_state != MEI_DEV_INITIALIZING) { |
163 | if (dev->dev_state != MEI_DEV_DISABLED && | |
164 | dev->dev_state != MEI_DEV_POWER_DOWN) | |
165 | dev->dev_state = MEI_DEV_RESETING; | |
91f01c6d | 166 | |
074b4c01 TW |
167 | mei_cl_all_disconnect(dev); |
168 | ||
91f01c6d | 169 | /* remove entry if already in list */ |
ff8b2f4e | 170 | dev_dbg(&dev->pdev->dev, "remove iamthif and wd from the file list.\n"); |
90e0b5f1 | 171 | mei_cl_unlink(&dev->wd_cl); |
781d0d89 TW |
172 | if (dev->open_handle_count > 0) |
173 | dev->open_handle_count--; | |
90e0b5f1 | 174 | mei_cl_unlink(&dev->iamthif_cl); |
781d0d89 TW |
175 | if (dev->open_handle_count > 0) |
176 | dev->open_handle_count--; | |
91f01c6d | 177 | |
19838fb8 | 178 | mei_amthif_reset_params(dev); |
5fb54fb4 | 179 | memset(&dev->wr_ext_msg, 0, sizeof(dev->wr_ext_msg)); |
91f01c6d OW |
180 | } |
181 | ||
cf9673da | 182 | dev->me_clients_num = 0; |
91f01c6d | 183 | dev->rd_msg_hdr = 0; |
eb9af0ac | 184 | dev->wd_pending = false; |
91f01c6d | 185 | |
91f01c6d | 186 | if (unexpected) |
b210d750 TW |
187 | dev_warn(&dev->pdev->dev, "unexpected reset: dev_state = %s\n", |
188 | mei_dev_state_str(dev->dev_state)); | |
91f01c6d | 189 | |
aafae7ec TW |
190 | if (!interrupts_enabled) { |
191 | dev_dbg(&dev->pdev->dev, "intr not enabled end of reset\n"); | |
192 | return; | |
193 | } | |
194 | ||
195 | mei_hw_start(dev); | |
196 | ||
197 | dev_dbg(&dev->pdev->dev, "link is established start sending messages.\n"); | |
198 | /* link is established * start sending messages. */ | |
199 | ||
200 | dev->dev_state = MEI_DEV_INIT_CLIENTS; | |
201 | ||
202 | mei_hbm_start_req(dev); | |
203 | ||
074b4c01 TW |
204 | /* wake up all readings so they can be interrupted */ |
205 | mei_cl_all_read_wakeup(dev); | |
206 | ||
91f01c6d | 207 | /* remove all waiting requests */ |
074b4c01 | 208 | mei_cl_all_write_clear(dev); |
91f01c6d | 209 | } |
40e0b67b | 210 | EXPORT_SYMBOL_GPL(mei_reset); |
91f01c6d | 211 | |
7cb035d9 TW |
212 | void mei_stop(struct mei_device *dev) |
213 | { | |
214 | dev_dbg(&dev->pdev->dev, "stopping the device.\n"); | |
215 | ||
216 | mutex_lock(&dev->device_lock); | |
217 | ||
218 | cancel_delayed_work(&dev->timer_work); | |
219 | ||
220 | mei_wd_stop(dev); | |
221 | ||
222 | dev->dev_state = MEI_DEV_POWER_DOWN; | |
223 | mei_reset(dev, 0); | |
224 | ||
225 | mutex_unlock(&dev->device_lock); | |
226 | ||
227 | flush_scheduled_work(); | |
2e647124 TW |
228 | |
229 | mei_watchdog_unregister(dev); | |
7cb035d9 | 230 | } |
40e0b67b | 231 | EXPORT_SYMBOL_GPL(mei_stop); |
91f01c6d | 232 | |
c1174c0e | 233 | |
91f01c6d | 234 |