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 | ||
17 | #include <linux/pci.h> | |
18 | #include <linux/sched.h> | |
19 | #include <linux/wait.h> | |
20 | #include <linux/delay.h> | |
21 | ||
47a73801 TW |
22 | #include <linux/mei.h> |
23 | ||
91f01c6d | 24 | #include "mei_dev.h" |
aafae7ec | 25 | #include "hbm.h" |
90e0b5f1 | 26 | #include "client.h" |
91f01c6d | 27 | |
b210d750 TW |
28 | const char *mei_dev_state_str(int state) |
29 | { | |
30 | #define MEI_DEV_STATE(state) case MEI_DEV_##state: return #state | |
31 | switch (state) { | |
32 | MEI_DEV_STATE(INITIALIZING); | |
33 | MEI_DEV_STATE(INIT_CLIENTS); | |
34 | MEI_DEV_STATE(ENABLED); | |
35 | MEI_DEV_STATE(RESETING); | |
36 | MEI_DEV_STATE(DISABLED); | |
b210d750 TW |
37 | MEI_DEV_STATE(POWER_DOWN); |
38 | MEI_DEV_STATE(POWER_UP); | |
39 | default: | |
40 | return "unkown"; | |
41 | } | |
42 | #undef MEI_DEV_STATE | |
43 | } | |
44 | ||
52c34561 | 45 | void mei_device_init(struct mei_device *dev) |
91f01c6d | 46 | { |
91f01c6d | 47 | /* setup our list array */ |
91f01c6d | 48 | INIT_LIST_HEAD(&dev->file_list); |
91f01c6d | 49 | mutex_init(&dev->device_lock); |
aafae7ec | 50 | init_waitqueue_head(&dev->wait_hw_ready); |
91f01c6d OW |
51 | init_waitqueue_head(&dev->wait_recvd_msg); |
52 | init_waitqueue_head(&dev->wait_stop_wd); | |
b210d750 | 53 | dev->dev_state = MEI_DEV_INITIALIZING; |
0288c7c9 TW |
54 | |
55 | mei_io_list_init(&dev->read_list); | |
56 | mei_io_list_init(&dev->write_list); | |
57 | mei_io_list_init(&dev->write_waiting_list); | |
58 | mei_io_list_init(&dev->ctrl_wr_list); | |
59 | mei_io_list_init(&dev->ctrl_rd_list); | |
91f01c6d OW |
60 | } |
61 | ||
62 | /** | |
63 | * mei_hw_init - initializes host and fw to start work. | |
64 | * | |
65 | * @dev: the device structure | |
66 | * | |
67 | * returns 0 on success, <0 on failure. | |
68 | */ | |
69 | int mei_hw_init(struct mei_device *dev) | |
70 | { | |
e7e0c231 | 71 | int ret = 0; |
91f01c6d OW |
72 | |
73 | mutex_lock(&dev->device_lock); | |
74 | ||
91f01c6d | 75 | /* acknowledge interrupt and stop interupts */ |
3a65dd4e | 76 | mei_clear_interrupts(dev); |
91f01c6d | 77 | |
e7e0c231 | 78 | mei_hw_config(dev); |
24aadc80 | 79 | |
eb9af0ac | 80 | dev->recvd_msg = false; |
91f01c6d OW |
81 | dev_dbg(&dev->pdev->dev, "reset in start the mei device.\n"); |
82 | ||
83 | mei_reset(dev, 1); | |
84 | ||
91f01c6d OW |
85 | /* wait for ME to turn on ME_RDY */ |
86 | if (!dev->recvd_msg) { | |
87 | mutex_unlock(&dev->device_lock); | |
e7e0c231 | 88 | ret = wait_event_interruptible_timeout(dev->wait_recvd_msg, |
3870c320 TW |
89 | dev->recvd_msg, |
90 | mei_secs_to_jiffies(MEI_INTEROP_TIMEOUT)); | |
91f01c6d OW |
91 | mutex_lock(&dev->device_lock); |
92 | } | |
93 | ||
e7e0c231 | 94 | if (ret <= 0 && !dev->recvd_msg) { |
b210d750 | 95 | dev->dev_state = MEI_DEV_DISABLED; |
91f01c6d OW |
96 | dev_dbg(&dev->pdev->dev, |
97 | "wait_event_interruptible_timeout failed" | |
98 | "on wait for ME to turn on ME_RDY.\n"); | |
e7e0c231 | 99 | goto err; |
91f01c6d OW |
100 | } |
101 | ||
91f01c6d | 102 | |
e7e0c231 TW |
103 | if (!mei_host_is_ready(dev)) { |
104 | dev_err(&dev->pdev->dev, "host is not ready.\n"); | |
105 | goto err; | |
106 | } | |
91f01c6d | 107 | |
827eef51 | 108 | if (!mei_hw_is_ready(dev)) { |
e7e0c231 TW |
109 | dev_err(&dev->pdev->dev, "ME is not ready.\n"); |
110 | goto err; | |
91f01c6d OW |
111 | } |
112 | ||
113 | if (dev->version.major_version != HBM_MAJOR_VERSION || | |
114 | dev->version.minor_version != HBM_MINOR_VERSION) { | |
115 | dev_dbg(&dev->pdev->dev, "MEI start failed.\n"); | |
e7e0c231 | 116 | goto err; |
91f01c6d OW |
117 | } |
118 | ||
eb9af0ac | 119 | dev->recvd_msg = false; |
91f01c6d | 120 | dev_dbg(&dev->pdev->dev, "link layer has been established.\n"); |
91f01c6d | 121 | |
91f01c6d | 122 | mutex_unlock(&dev->device_lock); |
e7e0c231 TW |
123 | return 0; |
124 | err: | |
125 | dev_err(&dev->pdev->dev, "link layer initialization failed.\n"); | |
126 | dev->dev_state = MEI_DEV_DISABLED; | |
127 | mutex_unlock(&dev->device_lock); | |
128 | return -ENODEV; | |
91f01c6d OW |
129 | } |
130 | ||
91f01c6d OW |
131 | /** |
132 | * mei_reset - resets host and fw. | |
133 | * | |
134 | * @dev: the device structure | |
135 | * @interrupts_enabled: if interrupt should be enabled after reset. | |
136 | */ | |
137 | void mei_reset(struct mei_device *dev, int interrupts_enabled) | |
138 | { | |
91f01c6d OW |
139 | bool unexpected; |
140 | ||
b210d750 TW |
141 | unexpected = (dev->dev_state != MEI_DEV_INITIALIZING && |
142 | dev->dev_state != MEI_DEV_DISABLED && | |
143 | dev->dev_state != MEI_DEV_POWER_DOWN && | |
144 | dev->dev_state != MEI_DEV_POWER_UP); | |
91f01c6d | 145 | |
91f01c6d OW |
146 | mei_hw_reset(dev, interrupts_enabled); |
147 | ||
91f01c6d | 148 | |
b210d750 TW |
149 | if (dev->dev_state != MEI_DEV_INITIALIZING) { |
150 | if (dev->dev_state != MEI_DEV_DISABLED && | |
151 | dev->dev_state != MEI_DEV_POWER_DOWN) | |
152 | dev->dev_state = MEI_DEV_RESETING; | |
91f01c6d | 153 | |
074b4c01 TW |
154 | mei_cl_all_disconnect(dev); |
155 | ||
91f01c6d | 156 | /* remove entry if already in list */ |
ff8b2f4e | 157 | dev_dbg(&dev->pdev->dev, "remove iamthif and wd from the file list.\n"); |
90e0b5f1 | 158 | mei_cl_unlink(&dev->wd_cl); |
781d0d89 TW |
159 | if (dev->open_handle_count > 0) |
160 | dev->open_handle_count--; | |
90e0b5f1 | 161 | mei_cl_unlink(&dev->iamthif_cl); |
781d0d89 TW |
162 | if (dev->open_handle_count > 0) |
163 | dev->open_handle_count--; | |
91f01c6d | 164 | |
19838fb8 | 165 | mei_amthif_reset_params(dev); |
5fb54fb4 | 166 | memset(&dev->wr_ext_msg, 0, sizeof(dev->wr_ext_msg)); |
91f01c6d OW |
167 | } |
168 | ||
cf9673da | 169 | dev->me_clients_num = 0; |
91f01c6d | 170 | dev->rd_msg_hdr = 0; |
eb9af0ac | 171 | dev->wd_pending = false; |
91f01c6d | 172 | |
91f01c6d | 173 | if (unexpected) |
b210d750 TW |
174 | dev_warn(&dev->pdev->dev, "unexpected reset: dev_state = %s\n", |
175 | mei_dev_state_str(dev->dev_state)); | |
91f01c6d | 176 | |
aafae7ec TW |
177 | if (!interrupts_enabled) { |
178 | dev_dbg(&dev->pdev->dev, "intr not enabled end of reset\n"); | |
179 | return; | |
180 | } | |
181 | ||
182 | mei_hw_start(dev); | |
183 | ||
184 | dev_dbg(&dev->pdev->dev, "link is established start sending messages.\n"); | |
185 | /* link is established * start sending messages. */ | |
186 | ||
187 | dev->dev_state = MEI_DEV_INIT_CLIENTS; | |
188 | ||
189 | mei_hbm_start_req(dev); | |
190 | ||
074b4c01 TW |
191 | /* wake up all readings so they can be interrupted */ |
192 | mei_cl_all_read_wakeup(dev); | |
193 | ||
91f01c6d | 194 | /* remove all waiting requests */ |
074b4c01 | 195 | mei_cl_all_write_clear(dev); |
91f01c6d OW |
196 | } |
197 | ||
7cb035d9 TW |
198 | void mei_stop(struct mei_device *dev) |
199 | { | |
200 | dev_dbg(&dev->pdev->dev, "stopping the device.\n"); | |
201 | ||
202 | mutex_lock(&dev->device_lock); | |
203 | ||
204 | cancel_delayed_work(&dev->timer_work); | |
205 | ||
206 | mei_wd_stop(dev); | |
207 | ||
208 | dev->dev_state = MEI_DEV_POWER_DOWN; | |
209 | mei_reset(dev, 0); | |
210 | ||
211 | mutex_unlock(&dev->device_lock); | |
212 | ||
213 | flush_scheduled_work(); | |
2e647124 TW |
214 | |
215 | mei_watchdog_unregister(dev); | |
216 | ||
7cb035d9 TW |
217 | } |
218 | ||
91f01c6d | 219 | |
c1174c0e | 220 | |
91f01c6d | 221 |