mei: move clients cleanup code from init.c to client.c
[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
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"
90e0b5f1 25#include "client.h"
91f01c6d 26
b210d750
TW
27const char *mei_dev_state_str(int state)
28{
29#define MEI_DEV_STATE(state) case MEI_DEV_##state: return #state
30 switch (state) {
31 MEI_DEV_STATE(INITIALIZING);
32 MEI_DEV_STATE(INIT_CLIENTS);
33 MEI_DEV_STATE(ENABLED);
34 MEI_DEV_STATE(RESETING);
35 MEI_DEV_STATE(DISABLED);
36 MEI_DEV_STATE(RECOVERING_FROM_RESET);
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 45void mei_device_init(struct mei_device *dev)
91f01c6d 46{
91f01c6d 47 /* setup our list array */
91f01c6d 48 INIT_LIST_HEAD(&dev->file_list);
91f01c6d
OW
49 mutex_init(&dev->device_lock);
50 init_waitqueue_head(&dev->wait_recvd_msg);
51 init_waitqueue_head(&dev->wait_stop_wd);
b210d750 52 dev->dev_state = MEI_DEV_INITIALIZING;
0288c7c9
TW
53
54 mei_io_list_init(&dev->read_list);
55 mei_io_list_init(&dev->write_list);
56 mei_io_list_init(&dev->write_waiting_list);
57 mei_io_list_init(&dev->ctrl_wr_list);
58 mei_io_list_init(&dev->ctrl_rd_list);
91f01c6d
OW
59}
60
61/**
62 * mei_hw_init - initializes host and fw to start work.
63 *
64 * @dev: the device structure
65 *
66 * returns 0 on success, <0 on failure.
67 */
68int mei_hw_init(struct mei_device *dev)
69{
e7e0c231 70 int ret = 0;
91f01c6d
OW
71
72 mutex_lock(&dev->device_lock);
73
91f01c6d 74 /* acknowledge interrupt and stop interupts */
3a65dd4e 75 mei_clear_interrupts(dev);
91f01c6d 76
e7e0c231 77 mei_hw_config(dev);
24aadc80 78
eb9af0ac 79 dev->recvd_msg = false;
91f01c6d
OW
80 dev_dbg(&dev->pdev->dev, "reset in start the mei device.\n");
81
82 mei_reset(dev, 1);
83
91f01c6d
OW
84 /* wait for ME to turn on ME_RDY */
85 if (!dev->recvd_msg) {
86 mutex_unlock(&dev->device_lock);
e7e0c231 87 ret = wait_event_interruptible_timeout(dev->wait_recvd_msg,
3870c320
TW
88 dev->recvd_msg,
89 mei_secs_to_jiffies(MEI_INTEROP_TIMEOUT));
91f01c6d
OW
90 mutex_lock(&dev->device_lock);
91 }
92
e7e0c231 93 if (ret <= 0 && !dev->recvd_msg) {
b210d750 94 dev->dev_state = MEI_DEV_DISABLED;
91f01c6d
OW
95 dev_dbg(&dev->pdev->dev,
96 "wait_event_interruptible_timeout failed"
97 "on wait for ME to turn on ME_RDY.\n");
e7e0c231 98 goto err;
91f01c6d
OW
99 }
100
91f01c6d 101
e7e0c231
TW
102 if (!mei_host_is_ready(dev)) {
103 dev_err(&dev->pdev->dev, "host is not ready.\n");
104 goto err;
105 }
91f01c6d 106
827eef51 107 if (!mei_hw_is_ready(dev)) {
e7e0c231
TW
108 dev_err(&dev->pdev->dev, "ME is not ready.\n");
109 goto err;
91f01c6d
OW
110 }
111
112 if (dev->version.major_version != HBM_MAJOR_VERSION ||
113 dev->version.minor_version != HBM_MINOR_VERSION) {
114 dev_dbg(&dev->pdev->dev, "MEI start failed.\n");
e7e0c231 115 goto err;
91f01c6d
OW
116 }
117
eb9af0ac 118 dev->recvd_msg = false;
91f01c6d 119 dev_dbg(&dev->pdev->dev, "link layer has been established.\n");
91f01c6d 120
91f01c6d 121 mutex_unlock(&dev->device_lock);
e7e0c231
TW
122 return 0;
123err:
124 dev_err(&dev->pdev->dev, "link layer initialization failed.\n");
125 dev->dev_state = MEI_DEV_DISABLED;
126 mutex_unlock(&dev->device_lock);
127 return -ENODEV;
91f01c6d
OW
128}
129
91f01c6d
OW
130/**
131 * mei_reset - resets host and fw.
132 *
133 * @dev: the device structure
134 * @interrupts_enabled: if interrupt should be enabled after reset.
135 */
136void mei_reset(struct mei_device *dev, int interrupts_enabled)
137{
91f01c6d
OW
138 bool unexpected;
139
a9f6b133 140 if (dev->dev_state == MEI_DEV_RECOVERING_FROM_RESET)
91f01c6d 141 return;
91f01c6d 142
b210d750
TW
143 unexpected = (dev->dev_state != MEI_DEV_INITIALIZING &&
144 dev->dev_state != MEI_DEV_DISABLED &&
145 dev->dev_state != MEI_DEV_POWER_DOWN &&
146 dev->dev_state != MEI_DEV_POWER_UP);
91f01c6d 147
91f01c6d
OW
148 mei_hw_reset(dev, interrupts_enabled);
149
91f01c6d 150
b210d750
TW
151 if (dev->dev_state != MEI_DEV_INITIALIZING) {
152 if (dev->dev_state != MEI_DEV_DISABLED &&
153 dev->dev_state != MEI_DEV_POWER_DOWN)
154 dev->dev_state = MEI_DEV_RESETING;
91f01c6d 155
074b4c01
TW
156 mei_cl_all_disconnect(dev);
157
91f01c6d 158 /* remove entry if already in list */
ff8b2f4e 159 dev_dbg(&dev->pdev->dev, "remove iamthif and wd from the file list.\n");
90e0b5f1 160 mei_cl_unlink(&dev->wd_cl);
781d0d89
TW
161 if (dev->open_handle_count > 0)
162 dev->open_handle_count--;
90e0b5f1 163 mei_cl_unlink(&dev->iamthif_cl);
781d0d89
TW
164 if (dev->open_handle_count > 0)
165 dev->open_handle_count--;
91f01c6d 166
19838fb8 167 mei_amthif_reset_params(dev);
5fb54fb4 168 memset(&dev->wr_ext_msg, 0, sizeof(dev->wr_ext_msg));
91f01c6d
OW
169 }
170
cf9673da 171 dev->me_clients_num = 0;
91f01c6d 172 dev->rd_msg_hdr = 0;
eb9af0ac 173 dev->wd_pending = false;
91f01c6d 174
91f01c6d 175 if (unexpected)
b210d750
TW
176 dev_warn(&dev->pdev->dev, "unexpected reset: dev_state = %s\n",
177 mei_dev_state_str(dev->dev_state));
91f01c6d 178
074b4c01
TW
179 /* wake up all readings so they can be interrupted */
180 mei_cl_all_read_wakeup(dev);
181
91f01c6d 182 /* remove all waiting requests */
074b4c01 183 mei_cl_all_write_clear(dev);
91f01c6d
OW
184}
185
186
c1174c0e 187
91f01c6d 188
This page took 0.266215 seconds and 5 git commands to generate.