xref: /openbmc/linux/drivers/misc/mei/init.c (revision 94c7b6fc)
1 /*
2  *
3  * Intel Management Engine Interface (Intel MEI) Linux driver
4  * Copyright (c) 2003-2012, Intel Corporation.
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/export.h>
18 #include <linux/pci.h>
19 #include <linux/sched.h>
20 #include <linux/wait.h>
21 #include <linux/delay.h>
22 
23 #include <linux/mei.h>
24 
25 #include "mei_dev.h"
26 #include "hbm.h"
27 #include "client.h"
28 
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(RESETTING);
37 	MEI_DEV_STATE(DISABLED);
38 	MEI_DEV_STATE(POWER_DOWN);
39 	MEI_DEV_STATE(POWER_UP);
40 	default:
41 		return "unknown";
42 	}
43 #undef MEI_DEV_STATE
44 }
45 
46 
47 /**
48  * mei_cancel_work. Cancel mei background jobs
49  *
50  * @dev: the device structure
51  *
52  * returns 0 on success or < 0 if the reset hasn't succeeded
53  */
54 void mei_cancel_work(struct mei_device *dev)
55 {
56 	cancel_work_sync(&dev->init_work);
57 	cancel_work_sync(&dev->reset_work);
58 
59 	cancel_delayed_work(&dev->timer_work);
60 }
61 EXPORT_SYMBOL_GPL(mei_cancel_work);
62 
63 /**
64  * mei_reset - resets host and fw.
65  *
66  * @dev: the device structure
67  */
68 int mei_reset(struct mei_device *dev)
69 {
70 	enum mei_dev_state state = dev->dev_state;
71 	bool interrupts_enabled;
72 	int ret;
73 
74 	if (state != MEI_DEV_INITIALIZING &&
75 	    state != MEI_DEV_DISABLED &&
76 	    state != MEI_DEV_POWER_DOWN &&
77 	    state != MEI_DEV_POWER_UP) {
78 		struct mei_fw_status fw_status;
79 		mei_fw_status(dev, &fw_status);
80 		dev_warn(&dev->pdev->dev,
81 			"unexpected reset: dev_state = %s " FW_STS_FMT "\n",
82 			mei_dev_state_str(state), FW_STS_PRM(fw_status));
83 	}
84 
85 	/* we're already in reset, cancel the init timer
86 	 * if the reset was called due the hbm protocol error
87 	 * we need to call it before hw start
88 	 * so the hbm watchdog won't kick in
89 	 */
90 	mei_hbm_idle(dev);
91 
92 	/* enter reset flow */
93 	interrupts_enabled = state != MEI_DEV_POWER_DOWN;
94 	dev->dev_state = MEI_DEV_RESETTING;
95 
96 	dev->reset_count++;
97 	if (dev->reset_count > MEI_MAX_CONSEC_RESET) {
98 		dev_err(&dev->pdev->dev, "reset: reached maximal consecutive resets: disabling the device\n");
99 		dev->dev_state = MEI_DEV_DISABLED;
100 		return -ENODEV;
101 	}
102 
103 	ret = mei_hw_reset(dev, interrupts_enabled);
104 	/* fall through and remove the sw state even if hw reset has failed */
105 
106 	/* no need to clean up software state in case of power up */
107 	if (state != MEI_DEV_INITIALIZING &&
108 	    state != MEI_DEV_POWER_UP) {
109 
110 		/* remove all waiting requests */
111 		mei_cl_all_write_clear(dev);
112 
113 		mei_cl_all_disconnect(dev);
114 
115 		/* wake up all readers and writers so they can be interrupted */
116 		mei_cl_all_wakeup(dev);
117 
118 		/* remove entry if already in list */
119 		dev_dbg(&dev->pdev->dev, "remove iamthif and wd from the file list.\n");
120 		mei_cl_unlink(&dev->wd_cl);
121 		mei_cl_unlink(&dev->iamthif_cl);
122 		mei_amthif_reset_params(dev);
123 	}
124 
125 	mei_hbm_reset(dev);
126 
127 	dev->rd_msg_hdr = 0;
128 	dev->wd_pending = false;
129 
130 	if (ret) {
131 		dev_err(&dev->pdev->dev, "hw_reset failed ret = %d\n", ret);
132 		return ret;
133 	}
134 
135 	if (state == MEI_DEV_POWER_DOWN) {
136 		dev_dbg(&dev->pdev->dev, "powering down: end of reset\n");
137 		dev->dev_state = MEI_DEV_DISABLED;
138 		return 0;
139 	}
140 
141 	ret = mei_hw_start(dev);
142 	if (ret) {
143 		dev_err(&dev->pdev->dev, "hw_start failed ret = %d\n", ret);
144 		return ret;
145 	}
146 
147 	dev_dbg(&dev->pdev->dev, "link is established start sending messages.\n");
148 
149 	dev->dev_state = MEI_DEV_INIT_CLIENTS;
150 	ret = mei_hbm_start_req(dev);
151 	if (ret) {
152 		dev_err(&dev->pdev->dev, "hbm_start failed ret = %d\n", ret);
153 		dev->dev_state = MEI_DEV_RESETTING;
154 		return ret;
155 	}
156 
157 	return 0;
158 }
159 EXPORT_SYMBOL_GPL(mei_reset);
160 
161 /**
162  * mei_start - initializes host and fw to start work.
163  *
164  * @dev: the device structure
165  *
166  * returns 0 on success, <0 on failure.
167  */
168 int mei_start(struct mei_device *dev)
169 {
170 	int ret;
171 	mutex_lock(&dev->device_lock);
172 
173 	/* acknowledge interrupt and stop interrupts */
174 	mei_clear_interrupts(dev);
175 
176 	mei_hw_config(dev);
177 
178 	dev_dbg(&dev->pdev->dev, "reset in start the mei device.\n");
179 
180 	dev->reset_count = 0;
181 	do {
182 		dev->dev_state = MEI_DEV_INITIALIZING;
183 		ret = mei_reset(dev);
184 
185 		if (ret == -ENODEV || dev->dev_state == MEI_DEV_DISABLED) {
186 			dev_err(&dev->pdev->dev, "reset failed ret = %d", ret);
187 			goto err;
188 		}
189 	} while (ret);
190 
191 	/* we cannot start the device w/o hbm start message completed */
192 	if (dev->dev_state == MEI_DEV_DISABLED) {
193 		dev_err(&dev->pdev->dev, "reset failed");
194 		goto err;
195 	}
196 
197 	if (mei_hbm_start_wait(dev)) {
198 		dev_err(&dev->pdev->dev, "HBM haven't started");
199 		goto err;
200 	}
201 
202 	if (!mei_host_is_ready(dev)) {
203 		dev_err(&dev->pdev->dev, "host is not ready.\n");
204 		goto err;
205 	}
206 
207 	if (!mei_hw_is_ready(dev)) {
208 		dev_err(&dev->pdev->dev, "ME is not ready.\n");
209 		goto err;
210 	}
211 
212 	if (!mei_hbm_version_is_supported(dev)) {
213 		dev_dbg(&dev->pdev->dev, "MEI start failed.\n");
214 		goto err;
215 	}
216 
217 	dev_dbg(&dev->pdev->dev, "link layer has been established.\n");
218 
219 	mutex_unlock(&dev->device_lock);
220 	return 0;
221 err:
222 	dev_err(&dev->pdev->dev, "link layer initialization failed.\n");
223 	dev->dev_state = MEI_DEV_DISABLED;
224 	mutex_unlock(&dev->device_lock);
225 	return -ENODEV;
226 }
227 EXPORT_SYMBOL_GPL(mei_start);
228 
229 /**
230  * mei_restart - restart device after suspend
231  *
232  * @dev: the device structure
233  *
234  * returns 0 on success or -ENODEV if the restart hasn't succeeded
235  */
236 int mei_restart(struct mei_device *dev)
237 {
238 	int err;
239 
240 	mutex_lock(&dev->device_lock);
241 
242 	mei_clear_interrupts(dev);
243 
244 	dev->dev_state = MEI_DEV_POWER_UP;
245 	dev->reset_count = 0;
246 
247 	err = mei_reset(dev);
248 
249 	mutex_unlock(&dev->device_lock);
250 
251 	if (err == -ENODEV || dev->dev_state == MEI_DEV_DISABLED) {
252 		dev_err(&dev->pdev->dev, "device disabled = %d\n", err);
253 		return -ENODEV;
254 	}
255 
256 	/* try to start again */
257 	if (err)
258 		schedule_work(&dev->reset_work);
259 
260 
261 	return 0;
262 }
263 EXPORT_SYMBOL_GPL(mei_restart);
264 
265 static void mei_reset_work(struct work_struct *work)
266 {
267 	struct mei_device *dev =
268 		container_of(work, struct mei_device,  reset_work);
269 	int ret;
270 
271 	mutex_lock(&dev->device_lock);
272 
273 	ret = mei_reset(dev);
274 
275 	mutex_unlock(&dev->device_lock);
276 
277 	if (dev->dev_state == MEI_DEV_DISABLED) {
278 		dev_err(&dev->pdev->dev, "device disabled = %d\n", ret);
279 		return;
280 	}
281 
282 	/* retry reset in case of failure */
283 	if (ret)
284 		schedule_work(&dev->reset_work);
285 }
286 
287 void mei_stop(struct mei_device *dev)
288 {
289 	dev_dbg(&dev->pdev->dev, "stopping the device.\n");
290 
291 	mei_cancel_work(dev);
292 
293 	mei_nfc_host_exit(dev);
294 
295 	mei_cl_bus_remove_devices(dev);
296 
297 	mutex_lock(&dev->device_lock);
298 
299 	mei_wd_stop(dev);
300 
301 	dev->dev_state = MEI_DEV_POWER_DOWN;
302 	mei_reset(dev);
303 
304 	mutex_unlock(&dev->device_lock);
305 
306 	mei_watchdog_unregister(dev);
307 }
308 EXPORT_SYMBOL_GPL(mei_stop);
309 
310 /**
311  * mei_write_is_idle - check if the write queues are idle
312  *
313  * @dev: the device structure
314  *
315  * returns true of there is no pending write
316  */
317 bool mei_write_is_idle(struct mei_device *dev)
318 {
319 	bool idle = (dev->dev_state == MEI_DEV_ENABLED &&
320 		list_empty(&dev->ctrl_wr_list.list) &&
321 		list_empty(&dev->write_list.list));
322 
323 	dev_dbg(&dev->pdev->dev, "write pg: is idle[%d] state=%s ctrl=%d write=%d\n",
324 		idle,
325 		mei_dev_state_str(dev->dev_state),
326 		list_empty(&dev->ctrl_wr_list.list),
327 		list_empty(&dev->write_list.list));
328 
329 	return idle;
330 }
331 EXPORT_SYMBOL_GPL(mei_write_is_idle);
332 
333 int mei_fw_status(struct mei_device *dev, struct mei_fw_status *fw_status)
334 {
335 	int i;
336 	const struct mei_fw_status *fw_src = &dev->cfg->fw_status;
337 
338 	if (!fw_status)
339 		return -EINVAL;
340 
341 	fw_status->count = fw_src->count;
342 	for (i = 0; i < fw_src->count && i < MEI_FW_STATUS_MAX; i++) {
343 		int ret;
344 		ret = pci_read_config_dword(dev->pdev,
345 			fw_src->status[i], &fw_status->status[i]);
346 		if (ret)
347 			return ret;
348 	}
349 
350 	return 0;
351 }
352 EXPORT_SYMBOL_GPL(mei_fw_status);
353 
354 void mei_device_init(struct mei_device *dev, const struct mei_cfg *cfg)
355 {
356 	/* setup our list array */
357 	INIT_LIST_HEAD(&dev->file_list);
358 	INIT_LIST_HEAD(&dev->device_list);
359 	mutex_init(&dev->device_lock);
360 	init_waitqueue_head(&dev->wait_hw_ready);
361 	init_waitqueue_head(&dev->wait_pg);
362 	init_waitqueue_head(&dev->wait_recvd_msg);
363 	init_waitqueue_head(&dev->wait_stop_wd);
364 	dev->dev_state = MEI_DEV_INITIALIZING;
365 	dev->reset_count = 0;
366 
367 	mei_io_list_init(&dev->read_list);
368 	mei_io_list_init(&dev->write_list);
369 	mei_io_list_init(&dev->write_waiting_list);
370 	mei_io_list_init(&dev->ctrl_wr_list);
371 	mei_io_list_init(&dev->ctrl_rd_list);
372 
373 	INIT_DELAYED_WORK(&dev->timer_work, mei_timer);
374 	INIT_WORK(&dev->init_work, mei_host_client_init);
375 	INIT_WORK(&dev->reset_work, mei_reset_work);
376 
377 	INIT_LIST_HEAD(&dev->wd_cl.link);
378 	INIT_LIST_HEAD(&dev->iamthif_cl.link);
379 	mei_io_list_init(&dev->amthif_cmd_list);
380 	mei_io_list_init(&dev->amthif_rd_complete_list);
381 
382 	bitmap_zero(dev->host_clients_map, MEI_CLIENTS_MAX);
383 	dev->open_handle_count = 0;
384 
385 	/*
386 	 * Reserving the first client ID
387 	 * 0: Reserved for MEI Bus Message communications
388 	 */
389 	bitmap_set(dev->host_clients_map, 0, 1);
390 
391 	dev->pg_event = MEI_PG_EVENT_IDLE;
392 	dev->cfg      = cfg;
393 }
394 EXPORT_SYMBOL_GPL(mei_device_init);
395 
396