blob: 5d08db5b314e21f6d07037a20550c128406f51b5 [file] [log] [blame]
Oren Weil91f01c62011-05-15 13:43:44 +03001/*
2 *
3 * Intel Management Engine Interface (Intel MEI) Linux driver
Tomas Winkler733ba912012-02-09 19:25:53 +02004 * Copyright (c) 2003-2012, Intel Corporation.
Oren Weil91f01c62011-05-15 13:43:44 +03005 *
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
Tomas Winkler4f3afe12012-05-09 16:38:59 +030022#include <linux/mei.h>
Oren Weil91f01c62011-05-15 13:43:44 +030023
Tomas Winkler47a73802012-12-25 19:06:03 +020024#include "mei_dev.h"
Tomas Winkler90e0b5f2013-01-08 23:07:14 +020025#include "client.h"
Tomas Winkler47a73802012-12-25 19:06:03 +020026
Tomas Winklerb210d752012-08-07 00:03:56 +030027const 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
45
Tomas Winkler5bd64712012-11-18 15:13:14 +020046
Oren Weil91f01c62011-05-15 13:43:44 +030047
48/**
Oren Weil91f01c62011-05-15 13:43:44 +030049 * init_mei_device - allocates and initializes the mei device structure
50 *
51 * @pdev: The pci device structure
52 *
53 * returns The mei_device_device pointer on success, NULL on failure.
54 */
Tomas Winklerc95efb72011-05-25 17:28:21 +030055struct mei_device *mei_device_init(struct pci_dev *pdev)
Oren Weil91f01c62011-05-15 13:43:44 +030056{
Oren Weil91f01c62011-05-15 13:43:44 +030057 struct mei_device *dev;
58
59 dev = kzalloc(sizeof(struct mei_device), GFP_KERNEL);
60 if (!dev)
61 return NULL;
62
63 /* setup our list array */
Oren Weil91f01c62011-05-15 13:43:44 +030064 INIT_LIST_HEAD(&dev->file_list);
65 INIT_LIST_HEAD(&dev->wd_cl.link);
66 INIT_LIST_HEAD(&dev->iamthif_cl.link);
67 mutex_init(&dev->device_lock);
68 init_waitqueue_head(&dev->wait_recvd_msg);
69 init_waitqueue_head(&dev->wait_stop_wd);
Tomas Winklerb210d752012-08-07 00:03:56 +030070 dev->dev_state = MEI_DEV_INITIALIZING;
Tomas Winkler0288c7c2011-06-06 10:44:34 +030071
72 mei_io_list_init(&dev->read_list);
73 mei_io_list_init(&dev->write_list);
74 mei_io_list_init(&dev->write_waiting_list);
75 mei_io_list_init(&dev->ctrl_wr_list);
76 mei_io_list_init(&dev->ctrl_rd_list);
Tomas Winklere773efc2012-11-11 17:37:58 +020077 mei_io_list_init(&dev->amthif_cmd_list);
78 mei_io_list_init(&dev->amthif_rd_complete_list);
Tomas Winkler627ca752013-01-08 23:07:32 +020079
80 INIT_DELAYED_WORK(&dev->timer_work, mei_timer);
81 INIT_WORK(&dev->init_work, mei_host_client_init);
82
Oren Weil91f01c62011-05-15 13:43:44 +030083 dev->pdev = pdev;
84 return dev;
85}
86
87/**
88 * mei_hw_init - initializes host and fw to start work.
89 *
90 * @dev: the device structure
91 *
92 * returns 0 on success, <0 on failure.
93 */
94int mei_hw_init(struct mei_device *dev)
95{
Tomas Winklere7e0c232013-01-08 23:07:31 +020096 int ret = 0;
Oren Weil91f01c62011-05-15 13:43:44 +030097
98 mutex_lock(&dev->device_lock);
99
Oren Weil91f01c62011-05-15 13:43:44 +0300100 /* acknowledge interrupt and stop interupts */
Tomas Winkler3a65dd42012-12-25 19:06:06 +0200101 mei_clear_interrupts(dev);
Oren Weil91f01c62011-05-15 13:43:44 +0300102
Tomas Winklere7e0c232013-01-08 23:07:31 +0200103 mei_hw_config(dev);
Tomas Winkler24aadc82012-06-25 23:46:27 +0300104
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300105 dev->recvd_msg = false;
Oren Weil91f01c62011-05-15 13:43:44 +0300106 dev_dbg(&dev->pdev->dev, "reset in start the mei device.\n");
107
108 mei_reset(dev, 1);
109
Oren Weil91f01c62011-05-15 13:43:44 +0300110 /* wait for ME to turn on ME_RDY */
111 if (!dev->recvd_msg) {
112 mutex_unlock(&dev->device_lock);
Tomas Winklere7e0c232013-01-08 23:07:31 +0200113 ret = wait_event_interruptible_timeout(dev->wait_recvd_msg,
Tomas Winkler3870c322012-11-01 21:17:14 +0200114 dev->recvd_msg,
115 mei_secs_to_jiffies(MEI_INTEROP_TIMEOUT));
Oren Weil91f01c62011-05-15 13:43:44 +0300116 mutex_lock(&dev->device_lock);
117 }
118
Tomas Winklere7e0c232013-01-08 23:07:31 +0200119 if (ret <= 0 && !dev->recvd_msg) {
Tomas Winklerb210d752012-08-07 00:03:56 +0300120 dev->dev_state = MEI_DEV_DISABLED;
Oren Weil91f01c62011-05-15 13:43:44 +0300121 dev_dbg(&dev->pdev->dev,
122 "wait_event_interruptible_timeout failed"
123 "on wait for ME to turn on ME_RDY.\n");
Tomas Winklere7e0c232013-01-08 23:07:31 +0200124 goto err;
Oren Weil91f01c62011-05-15 13:43:44 +0300125 }
126
Tomas Winkler115ba282013-01-08 23:07:29 +0200127
Tomas Winklere7e0c232013-01-08 23:07:31 +0200128 if (!mei_host_is_ready(dev)) {
129 dev_err(&dev->pdev->dev, "host is not ready.\n");
130 goto err;
131 }
Oren Weil91f01c62011-05-15 13:43:44 +0300132
Tomas Winklere7e0c232013-01-08 23:07:31 +0200133 if (!mei_me_is_ready(dev)) {
134 dev_err(&dev->pdev->dev, "ME is not ready.\n");
135 goto err;
Oren Weil91f01c62011-05-15 13:43:44 +0300136 }
137
138 if (dev->version.major_version != HBM_MAJOR_VERSION ||
139 dev->version.minor_version != HBM_MINOR_VERSION) {
140 dev_dbg(&dev->pdev->dev, "MEI start failed.\n");
Tomas Winklere7e0c232013-01-08 23:07:31 +0200141 goto err;
Oren Weil91f01c62011-05-15 13:43:44 +0300142 }
143
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300144 dev->recvd_msg = false;
Oren Weil91f01c62011-05-15 13:43:44 +0300145 dev_dbg(&dev->pdev->dev, "link layer has been established.\n");
Oren Weil91f01c62011-05-15 13:43:44 +0300146
Oren Weil91f01c62011-05-15 13:43:44 +0300147 mutex_unlock(&dev->device_lock);
Tomas Winklere7e0c232013-01-08 23:07:31 +0200148 return 0;
149err:
150 dev_err(&dev->pdev->dev, "link layer initialization failed.\n");
151 dev->dev_state = MEI_DEV_DISABLED;
152 mutex_unlock(&dev->device_lock);
153 return -ENODEV;
Oren Weil91f01c62011-05-15 13:43:44 +0300154}
155
156/**
Oren Weil91f01c62011-05-15 13:43:44 +0300157 * mei_reset - resets host and fw.
158 *
159 * @dev: the device structure
160 * @interrupts_enabled: if interrupt should be enabled after reset.
161 */
162void mei_reset(struct mei_device *dev, int interrupts_enabled)
163{
164 struct mei_cl *cl_pos = NULL;
165 struct mei_cl *cl_next = NULL;
166 struct mei_cl_cb *cb_pos = NULL;
167 struct mei_cl_cb *cb_next = NULL;
168 bool unexpected;
169
Tomas Winklera9f6b132013-01-08 23:07:25 +0200170 if (dev->dev_state == MEI_DEV_RECOVERING_FROM_RESET)
Oren Weil91f01c62011-05-15 13:43:44 +0300171 return;
Oren Weil91f01c62011-05-15 13:43:44 +0300172
Tomas Winklerb210d752012-08-07 00:03:56 +0300173 unexpected = (dev->dev_state != MEI_DEV_INITIALIZING &&
174 dev->dev_state != MEI_DEV_DISABLED &&
175 dev->dev_state != MEI_DEV_POWER_DOWN &&
176 dev->dev_state != MEI_DEV_POWER_UP);
Oren Weil91f01c62011-05-15 13:43:44 +0300177
Oren Weil91f01c62011-05-15 13:43:44 +0300178 mei_hw_reset(dev, interrupts_enabled);
179
Oren Weil91f01c62011-05-15 13:43:44 +0300180
Tomas Winklerb210d752012-08-07 00:03:56 +0300181 if (dev->dev_state != MEI_DEV_INITIALIZING) {
182 if (dev->dev_state != MEI_DEV_DISABLED &&
183 dev->dev_state != MEI_DEV_POWER_DOWN)
184 dev->dev_state = MEI_DEV_RESETING;
Oren Weil91f01c62011-05-15 13:43:44 +0300185
186 list_for_each_entry_safe(cl_pos,
187 cl_next, &dev->file_list, link) {
188 cl_pos->state = MEI_FILE_DISCONNECTED;
189 cl_pos->mei_flow_ctrl_creds = 0;
190 cl_pos->read_cb = NULL;
191 cl_pos->timer_count = 0;
192 }
193 /* remove entry if already in list */
Tomas Winklerff8b2f42012-11-11 17:38:03 +0200194 dev_dbg(&dev->pdev->dev, "remove iamthif and wd from the file list.\n");
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200195 mei_cl_unlink(&dev->wd_cl);
Tomas Winkler781d0d82013-01-08 23:07:22 +0200196 if (dev->open_handle_count > 0)
197 dev->open_handle_count--;
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200198 mei_cl_unlink(&dev->iamthif_cl);
Tomas Winkler781d0d82013-01-08 23:07:22 +0200199 if (dev->open_handle_count > 0)
200 dev->open_handle_count--;
Oren Weil91f01c62011-05-15 13:43:44 +0300201
Tomas Winkler19838fb2012-11-01 21:17:15 +0200202 mei_amthif_reset_params(dev);
Tomas Winkler5fb54fb2012-11-18 15:13:15 +0200203 memset(&dev->wr_ext_msg, 0, sizeof(dev->wr_ext_msg));
Oren Weil91f01c62011-05-15 13:43:44 +0300204 }
205
Tomas Winklercf9673d2011-06-06 10:44:33 +0300206 dev->me_clients_num = 0;
Oren Weil91f01c62011-05-15 13:43:44 +0300207 dev->rd_msg_hdr = 0;
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300208 dev->wd_pending = false;
Oren Weil91f01c62011-05-15 13:43:44 +0300209
Oren Weil91f01c62011-05-15 13:43:44 +0300210 if (unexpected)
Tomas Winklerb210d752012-08-07 00:03:56 +0300211 dev_warn(&dev->pdev->dev, "unexpected reset: dev_state = %s\n",
212 mei_dev_state_str(dev->dev_state));
Oren Weil91f01c62011-05-15 13:43:44 +0300213
214 /* Wake up all readings so they can be interrupted */
215 list_for_each_entry_safe(cl_pos, cl_next, &dev->file_list, link) {
216 if (waitqueue_active(&cl_pos->rx_wait)) {
217 dev_dbg(&dev->pdev->dev, "Waking up client!\n");
218 wake_up_interruptible(&cl_pos->rx_wait);
219 }
220 }
221 /* remove all waiting requests */
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200222 list_for_each_entry_safe(cb_pos, cb_next, &dev->write_list.list, list) {
223 list_del(&cb_pos->list);
Tomas Winkler601a1ef2012-10-09 16:50:20 +0200224 mei_io_cb_free(cb_pos);
Oren Weil91f01c62011-05-15 13:43:44 +0300225 }
226}
227
228
Samuel Ortizc1174c02012-11-18 15:13:20 +0200229
Oren Weil91f01c62011-05-15 13:43:44 +0300230