blob: 60469a0053bbe4e8208e390a25504a7d339b8f22 [file] [log] [blame]
Oren Weilfb7d8792011-05-15 13:43:42 +03001/*
2 *
3 * Intel Management Engine Interface (Intel MEI) Linux driver
Tomas Winkler733ba91c2012-02-09 19:25:53 +02004 * Copyright (c) 2003-2012, Intel Corporation.
Oren Weilfb7d8792011-05-15 13:43:42 +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
Tomas Winkler40e0b672013-03-27 16:58:30 +020018#include <linux/export.h>
Oren Weilfb7d8792011-05-15 13:43:42 +030019#include <linux/kthread.h>
20#include <linux/interrupt.h>
21#include <linux/fs.h>
22#include <linux/jiffies.h>
Tomas Winkler1f180352014-09-29 16:31:46 +030023#include <linux/slab.h>
Oren Weilfb7d8792011-05-15 13:43:42 +030024
Tomas Winkler4f3afe12012-05-09 16:38:59 +030025#include <linux/mei.h>
Tomas Winkler47a73802012-12-25 19:06:03 +020026
27#include "mei_dev.h"
Tomas Winkler0edb23f2013-01-08 23:07:12 +020028#include "hbm.h"
Tomas Winkler90e0b5f2013-01-08 23:07:14 +020029#include "client.h"
Oren Weilfb7d8792011-05-15 13:43:42 +030030
31
32/**
Alexander Usyskin83ce0742014-01-08 22:31:46 +020033 * mei_irq_compl_handler - dispatch complete handlers
Tomas Winkler4c6e22b2013-03-17 11:41:20 +020034 * for the completed callbacks
35 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +030036 * @dev: mei device
37 * @compl_list: list of completed cbs
Tomas Winkler4c6e22b2013-03-17 11:41:20 +020038 */
39void mei_irq_compl_handler(struct mei_device *dev, struct mei_cl_cb *compl_list)
40{
41 struct mei_cl_cb *cb, *next;
42 struct mei_cl *cl;
43
44 list_for_each_entry_safe(cb, next, &compl_list->list, list) {
45 cl = cb->cl;
Tomas Winklerc54bf3a2015-02-10 10:39:39 +020046 list_del_init(&cb->list);
Tomas Winkler4c6e22b2013-03-17 11:41:20 +020047
Tomas Winkler2bf94cab2014-09-29 16:31:42 +030048 dev_dbg(dev->dev, "completing call back.\n");
Tomas Winkler4c6e22b2013-03-17 11:41:20 +020049 if (cl == &dev->iamthif_cl)
50 mei_amthif_complete(dev, cb);
51 else
Tomas Winklerdb086fa2013-05-12 15:34:45 +030052 mei_cl_complete(cl, cb);
Tomas Winkler4c6e22b2013-03-17 11:41:20 +020053 }
54}
Tomas Winkler40e0b672013-03-27 16:58:30 +020055EXPORT_SYMBOL_GPL(mei_irq_compl_handler);
Tomas Winkler6e0f1802013-04-19 22:01:34 +030056
Tomas Winkler4c6e22b2013-03-17 11:41:20 +020057/**
Tomas Winkler6e0f1802013-04-19 22:01:34 +030058 * mei_cl_hbm_equal - check if hbm is addressed to the client
Oren Weilfb7d8792011-05-15 13:43:42 +030059 *
Tomas Winkler6e0f1802013-04-19 22:01:34 +030060 * @cl: host client
Oren Weilfb7d8792011-05-15 13:43:42 +030061 * @mei_hdr: header of mei client message
62 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +030063 * Return: true if matches, false otherwise
Oren Weilfb7d8792011-05-15 13:43:42 +030064 */
Tomas Winkler6e0f1802013-04-19 22:01:34 +030065static inline int mei_cl_hbm_equal(struct mei_cl *cl,
66 struct mei_msg_hdr *mei_hdr)
Oren Weilfb7d8792011-05-15 13:43:42 +030067{
Tomas Winkler6e0f1802013-04-19 22:01:34 +030068 return cl->host_client_id == mei_hdr->host_addr &&
69 cl->me_client_id == mei_hdr->me_addr;
70}
71/**
Tomas Winkler3d33ff22015-02-10 10:39:36 +020072 * mei_cl_is_reading - checks if the client is in reading state
Tomas Winkler6e0f1802013-04-19 22:01:34 +030073 *
74 * @cl: mei client
Tomas Winkler6e0f1802013-04-19 22:01:34 +030075 *
Tomas Winkler3d33ff22015-02-10 10:39:36 +020076 * Return: true if the client is reading
Tomas Winkler6e0f1802013-04-19 22:01:34 +030077 */
Tomas Winkler3d33ff22015-02-10 10:39:36 +020078static bool mei_cl_is_reading(struct mei_cl *cl)
Tomas Winkler6e0f1802013-04-19 22:01:34 +030079{
Tomas Winkler3d33ff22015-02-10 10:39:36 +020080 return cl->state == MEI_FILE_CONNECTED &&
Tomas Winkler6e0f1802013-04-19 22:01:34 +030081 cl->reading_state != MEI_READ_COMPLETE;
Oren Weilfb7d8792011-05-15 13:43:42 +030082}
83
84/**
Tomas Winkler331e4182015-02-10 10:39:41 +020085 * mei_irq_discard_msg - discard received message
86 *
87 * @dev: mei device
88 * @hdr: message header
89 */
90static inline
91void mei_irq_discard_msg(struct mei_device *dev, struct mei_msg_hdr *hdr)
92{
93 /*
94 * no need to check for size as it is guarantied
95 * that length fits into rd_msg_buf
96 */
97 mei_read_slots(dev, dev->rd_msg_buf, hdr->length);
98 dev_dbg(dev->dev, "discarding message " MEI_HDR_FMT "\n",
99 MEI_HDR_PRM(hdr));
100}
101
102/**
Alexander Usyskince231392014-09-29 16:31:50 +0300103 * mei_cl_irq_read_msg - process client message
Oren Weilfb7d8792011-05-15 13:43:42 +0300104 *
Tomas Winkler3d33ff22015-02-10 10:39:36 +0200105 * @cl: reading client
Oren Weilfb7d8792011-05-15 13:43:42 +0300106 * @mei_hdr: header of mei client message
Tomas Winkler3d33ff22015-02-10 10:39:36 +0200107 * @complete_list: completion list
Oren Weilfb7d8792011-05-15 13:43:42 +0300108 *
Tomas Winkler3d33ff22015-02-10 10:39:36 +0200109 * Return: always 0
Oren Weilfb7d8792011-05-15 13:43:42 +0300110 */
Tomas Winkler331e4182015-02-10 10:39:41 +0200111int mei_cl_irq_read_msg(struct mei_cl *cl,
112 struct mei_msg_hdr *mei_hdr,
113 struct mei_cl_cb *complete_list)
Oren Weilfb7d8792011-05-15 13:43:42 +0300114{
Tomas Winkler3d33ff22015-02-10 10:39:36 +0200115 struct mei_device *dev = cl->dev;
116 struct mei_cl_cb *cb;
Tomas Winkler479bc592011-06-16 00:46:03 +0300117 unsigned char *buffer = NULL;
Oren Weilfb7d8792011-05-15 13:43:42 +0300118
Tomas Winkler3d33ff22015-02-10 10:39:36 +0200119 list_for_each_entry(cb, &dev->read_list.list, list) {
120 if (cl == cb->cl)
121 break;
Oren Weilfb7d8792011-05-15 13:43:42 +0300122 }
123
Tomas Winkler3d33ff22015-02-10 10:39:36 +0200124 if (&cb->list == &dev->read_list.list) {
125 dev_err(dev->dev, "no reader found\n");
126 goto out;
127 }
128
129 if (!mei_cl_is_reading(cl)) {
130 cl_err(dev, cl, "cl is not reading state=%d reading state=%d\n",
131 cl->state, cl->reading_state);
132 goto out;
133 }
134
135 cl->reading_state = MEI_READING;
136
Tomas Winkler5db75142015-02-10 10:39:42 +0200137 if (cb->buf.size == 0 || cb->buf.data == NULL) {
Tomas Winkler3d33ff22015-02-10 10:39:36 +0200138 cl_err(dev, cl, "response buffer is not allocated.\n");
139 list_move_tail(&cb->list, &complete_list->list);
140 cb->status = -ENOMEM;
141 goto out;
142 }
143
Tomas Winkler5db75142015-02-10 10:39:42 +0200144 if (cb->buf.size < mei_hdr->length + cb->buf_idx) {
Tomas Winkler3d33ff22015-02-10 10:39:36 +0200145 cl_dbg(dev, cl, "message overflow. size %d len %d idx %ld\n",
Tomas Winkler5db75142015-02-10 10:39:42 +0200146 cb->buf.size, mei_hdr->length, cb->buf_idx);
147 buffer = krealloc(cb->buf.data, mei_hdr->length + cb->buf_idx,
Tomas Winkler3d33ff22015-02-10 10:39:36 +0200148 GFP_KERNEL);
149
150 if (!buffer) {
151 cb->status = -ENOMEM;
152 list_move_tail(&cb->list, &complete_list->list);
153 goto out;
154 }
Tomas Winkler5db75142015-02-10 10:39:42 +0200155 cb->buf.data = buffer;
156 cb->buf.size = mei_hdr->length + cb->buf_idx;
Tomas Winkler3d33ff22015-02-10 10:39:36 +0200157 }
158
Tomas Winkler5db75142015-02-10 10:39:42 +0200159 buffer = cb->buf.data + cb->buf_idx;
Tomas Winkler3d33ff22015-02-10 10:39:36 +0200160 mei_read_slots(dev, buffer, mei_hdr->length);
161
162 cb->buf_idx += mei_hdr->length;
Tomas Winkler331e4182015-02-10 10:39:41 +0200163
Tomas Winkler3d33ff22015-02-10 10:39:36 +0200164 if (mei_hdr->msg_complete) {
Tomas Winkler331e4182015-02-10 10:39:41 +0200165 cb->read_time = jiffies;
Tomas Winkler3d33ff22015-02-10 10:39:36 +0200166 cl_dbg(dev, cl, "completed read length = %lu\n",
167 cb->buf_idx);
168 list_move_tail(&cb->list, &complete_list->list);
169 }
170
171out:
Tomas Winkler331e4182015-02-10 10:39:41 +0200172 if (!buffer)
173 mei_irq_discard_msg(dev, mei_hdr);
Oren Weilfb7d8792011-05-15 13:43:42 +0300174
175 return 0;
176}
177
178/**
Tomas Winkler6bb948c2014-02-12 21:41:52 +0200179 * mei_cl_irq_disconnect_rsp - send disconnection response message
180 *
181 * @cl: client
182 * @cb: callback block.
Tomas Winkler6bb948c2014-02-12 21:41:52 +0200183 * @cmpl_list: complete list.
184 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300185 * Return: 0, OK; otherwise, error.
Tomas Winkler6bb948c2014-02-12 21:41:52 +0200186 */
187static int mei_cl_irq_disconnect_rsp(struct mei_cl *cl, struct mei_cl_cb *cb,
Tomas Winkler9d098192014-02-19 17:35:48 +0200188 struct mei_cl_cb *cmpl_list)
Tomas Winkler6bb948c2014-02-12 21:41:52 +0200189{
190 struct mei_device *dev = cl->dev;
Tomas Winkler9d098192014-02-19 17:35:48 +0200191 u32 msg_slots;
192 int slots;
Tomas Winkler6bb948c2014-02-12 21:41:52 +0200193 int ret;
194
Tomas Winkler9d098192014-02-19 17:35:48 +0200195 slots = mei_hbuf_empty_slots(dev);
196 msg_slots = mei_data2slots(sizeof(struct hbm_client_connect_response));
Tomas Winkler6bb948c2014-02-12 21:41:52 +0200197
Tomas Winkler9d098192014-02-19 17:35:48 +0200198 if (slots < msg_slots)
Tomas Winkler6bb948c2014-02-12 21:41:52 +0200199 return -EMSGSIZE;
200
Tomas Winkler6bb948c2014-02-12 21:41:52 +0200201 ret = mei_hbm_cl_disconnect_rsp(dev, cl);
202
203 cl->state = MEI_FILE_DISCONNECTED;
204 cl->status = 0;
Alexander Usyskin31a5ef22014-03-06 23:53:53 +0200205 list_del(&cb->list);
Tomas Winkler6bb948c2014-02-12 21:41:52 +0200206 mei_io_cb_free(cb);
207
208 return ret;
209}
210
211
212
213/**
Tomas Winkler5a8373f2014-08-21 14:29:17 +0300214 * mei_cl_irq_disconnect - processes close related operation from
Tomas Winkler6220d6a2013-05-12 15:34:46 +0300215 * interrupt thread context - send disconnect request
Oren Weilfb7d8792011-05-15 13:43:42 +0300216 *
Tomas Winkler6220d6a2013-05-12 15:34:46 +0300217 * @cl: client
218 * @cb: callback block.
Oren Weilfb7d8792011-05-15 13:43:42 +0300219 * @cmpl_list: complete list.
220 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300221 * Return: 0, OK; otherwise, error.
Oren Weilfb7d8792011-05-15 13:43:42 +0300222 */
Tomas Winkler5a8373f2014-08-21 14:29:17 +0300223static int mei_cl_irq_disconnect(struct mei_cl *cl, struct mei_cl_cb *cb,
Tomas Winkler9d098192014-02-19 17:35:48 +0200224 struct mei_cl_cb *cmpl_list)
Oren Weilfb7d8792011-05-15 13:43:42 +0300225{
Tomas Winkler6220d6a2013-05-12 15:34:46 +0300226 struct mei_device *dev = cl->dev;
Tomas Winkler9d098192014-02-19 17:35:48 +0200227 u32 msg_slots;
228 int slots;
Tomas Winkler6220d6a2013-05-12 15:34:46 +0300229
Tomas Winkler9d098192014-02-19 17:35:48 +0200230 msg_slots = mei_data2slots(sizeof(struct hbm_client_connect_request));
231 slots = mei_hbuf_empty_slots(dev);
Tomas Winklerb45f3cc2012-07-04 19:24:53 +0300232
Tomas Winkler9d098192014-02-19 17:35:48 +0200233 if (slots < msg_slots)
Tomas Winklerc8c8d082013-03-11 18:27:02 +0200234 return -EMSGSIZE;
235
Tomas Winkler8120e722012-12-25 19:06:11 +0200236 if (mei_hbm_cl_disconnect_req(dev, cl)) {
Tomas Winklerb45f3cc2012-07-04 19:24:53 +0300237 cl->status = 0;
Tomas Winkler6220d6a2013-05-12 15:34:46 +0300238 cb->buf_idx = 0;
239 list_move_tail(&cb->list, &cmpl_list->list);
Tomas Winklerc8c8d082013-03-11 18:27:02 +0200240 return -EIO;
Oren Weilfb7d8792011-05-15 13:43:42 +0300241 }
242
Tomas Winklerc8c8d082013-03-11 18:27:02 +0200243 cl->state = MEI_FILE_DISCONNECTING;
244 cl->status = 0;
Tomas Winkler6220d6a2013-05-12 15:34:46 +0300245 cb->buf_idx = 0;
246 list_move_tail(&cb->list, &dev->ctrl_rd_list.list);
Tomas Winklerc8c8d082013-03-11 18:27:02 +0200247 cl->timer_count = MEI_CONNECT_TIMEOUT;
248
Oren Weilfb7d8792011-05-15 13:43:42 +0300249 return 0;
250}
251
Oren Weilfb7d8792011-05-15 13:43:42 +0300252
253/**
Alexander Usyskince231392014-09-29 16:31:50 +0300254 * mei_cl_irq_read - processes client read related operation from the
Tomas Winkler6220d6a2013-05-12 15:34:46 +0300255 * interrupt thread context - request for flow control credits
Oren Weilfb7d8792011-05-15 13:43:42 +0300256 *
Tomas Winkler6220d6a2013-05-12 15:34:46 +0300257 * @cl: client
258 * @cb: callback block.
Oren Weilfb7d8792011-05-15 13:43:42 +0300259 * @cmpl_list: complete list.
260 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300261 * Return: 0, OK; otherwise, error.
Oren Weilfb7d8792011-05-15 13:43:42 +0300262 */
Tomas Winkler6220d6a2013-05-12 15:34:46 +0300263static int mei_cl_irq_read(struct mei_cl *cl, struct mei_cl_cb *cb,
Tomas Winkler9d098192014-02-19 17:35:48 +0200264 struct mei_cl_cb *cmpl_list)
Oren Weilfb7d8792011-05-15 13:43:42 +0300265{
Tomas Winkler6220d6a2013-05-12 15:34:46 +0300266 struct mei_device *dev = cl->dev;
Tomas Winkler9d098192014-02-19 17:35:48 +0200267 u32 msg_slots;
268 int slots;
Tomas Winkler2ebf8c92013-09-16 23:44:43 +0300269 int ret;
270
Tomas Winkler9d098192014-02-19 17:35:48 +0200271 msg_slots = mei_data2slots(sizeof(struct hbm_flow_control));
272 slots = mei_hbuf_empty_slots(dev);
Tomas Winkler2ebf8c92013-09-16 23:44:43 +0300273
Tomas Winkler9d098192014-02-19 17:35:48 +0200274 if (slots < msg_slots)
Tomas Winklerc8c8d082013-03-11 18:27:02 +0200275 return -EMSGSIZE;
Tomas Winkler7bdf72d2012-07-04 19:24:52 +0300276
Tomas Winkler2ebf8c92013-09-16 23:44:43 +0300277 ret = mei_hbm_cl_flow_control_req(dev, cl);
278 if (ret) {
279 cl->status = ret;
Tomas Winkler6220d6a2013-05-12 15:34:46 +0300280 cb->buf_idx = 0;
281 list_move_tail(&cb->list, &cmpl_list->list);
Tomas Winkler2ebf8c92013-09-16 23:44:43 +0300282 return ret;
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200283 }
Tomas Winkler2ebf8c92013-09-16 23:44:43 +0300284
Tomas Winkler6220d6a2013-05-12 15:34:46 +0300285 list_move_tail(&cb->list, &dev->read_list.list);
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200286
Oren Weilfb7d8792011-05-15 13:43:42 +0300287 return 0;
288}
289
290
291/**
Tomas Winkler02a7eec2014-02-12 21:41:51 +0200292 * mei_cl_irq_connect - send connect request in irq_thread context
Oren Weilfb7d8792011-05-15 13:43:42 +0300293 *
Tomas Winkler6220d6a2013-05-12 15:34:46 +0300294 * @cl: client
295 * @cb: callback block.
Oren Weilfb7d8792011-05-15 13:43:42 +0300296 * @cmpl_list: complete list.
297 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300298 * Return: 0, OK; otherwise, error.
Oren Weilfb7d8792011-05-15 13:43:42 +0300299 */
Tomas Winkler02a7eec2014-02-12 21:41:51 +0200300static int mei_cl_irq_connect(struct mei_cl *cl, struct mei_cl_cb *cb,
Tomas Winkler9d098192014-02-19 17:35:48 +0200301 struct mei_cl_cb *cmpl_list)
Oren Weilfb7d8792011-05-15 13:43:42 +0300302{
Tomas Winkler6220d6a2013-05-12 15:34:46 +0300303 struct mei_device *dev = cl->dev;
Tomas Winkler9d098192014-02-19 17:35:48 +0200304 u32 msg_slots;
305 int slots;
Tomas Winkler2ebf8c92013-09-16 23:44:43 +0300306 int ret;
Tomas Winkler6220d6a2013-05-12 15:34:46 +0300307
Tomas Winkler9d098192014-02-19 17:35:48 +0200308 msg_slots = mei_data2slots(sizeof(struct hbm_client_connect_request));
309 slots = mei_hbuf_empty_slots(dev);
Tomas Winklerc8c8d082013-03-11 18:27:02 +0200310
Tomas Winkler02a7eec2014-02-12 21:41:51 +0200311 if (mei_cl_is_other_connecting(cl))
312 return 0;
313
Tomas Winkler9d098192014-02-19 17:35:48 +0200314 if (slots < msg_slots)
Tomas Winklerc8c8d082013-03-11 18:27:02 +0200315 return -EMSGSIZE;
Tomas Winklerc8c8d082013-03-11 18:27:02 +0200316
Tomas Winklerb45f3cc2012-07-04 19:24:53 +0300317 cl->state = MEI_FILE_CONNECTING;
Tomas Winklerc8c8d082013-03-11 18:27:02 +0200318
Tomas Winkler2ebf8c92013-09-16 23:44:43 +0300319 ret = mei_hbm_cl_connect_req(dev, cl);
320 if (ret) {
321 cl->status = ret;
Tomas Winkler6220d6a2013-05-12 15:34:46 +0300322 cb->buf_idx = 0;
323 list_del(&cb->list);
Tomas Winkler2ebf8c92013-09-16 23:44:43 +0300324 return ret;
Tomas Winklerb45f3cc2012-07-04 19:24:53 +0300325 }
Tomas Winkler6220d6a2013-05-12 15:34:46 +0300326
327 list_move_tail(&cb->list, &dev->ctrl_rd_list.list);
328 cl->timer_count = MEI_CONNECT_TIMEOUT;
Oren Weilfb7d8792011-05-15 13:43:42 +0300329 return 0;
330}
331
Oren Weilfb7d8792011-05-15 13:43:42 +0300332
333/**
Masanari Iida393b1482013-04-05 01:05:05 +0900334 * mei_irq_read_handler - bottom half read routine after ISR to
Oren Weilfb7d8792011-05-15 13:43:42 +0300335 * handle the read processing.
336 *
Oren Weilfb7d8792011-05-15 13:43:42 +0300337 * @dev: the device structure
Tomas Winkler06ecd642013-02-06 14:06:42 +0200338 * @cmpl_list: An instance of our list structure
Oren Weilfb7d8792011-05-15 13:43:42 +0300339 * @slots: slots to read.
340 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300341 * Return: 0 on success, <0 on failure.
Oren Weilfb7d8792011-05-15 13:43:42 +0300342 */
Tomas Winkler06ecd642013-02-06 14:06:42 +0200343int mei_irq_read_handler(struct mei_device *dev,
344 struct mei_cl_cb *cmpl_list, s32 *slots)
Oren Weilfb7d8792011-05-15 13:43:42 +0300345{
346 struct mei_msg_hdr *mei_hdr;
Tomas Winkler10ee9072013-11-11 13:26:08 +0200347 struct mei_cl *cl;
348 int ret;
Oren Weilfb7d8792011-05-15 13:43:42 +0300349
350 if (!dev->rd_msg_hdr) {
Tomas Winkler827eef52013-02-06 14:06:41 +0200351 dev->rd_msg_hdr = mei_read_hdr(dev);
Oren Weilfb7d8792011-05-15 13:43:42 +0300352 (*slots)--;
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300353 dev_dbg(dev->dev, "slots =%08x.\n", *slots);
Oren Weilfb7d8792011-05-15 13:43:42 +0300354 }
355 mei_hdr = (struct mei_msg_hdr *) &dev->rd_msg_hdr;
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300356 dev_dbg(dev->dev, MEI_HDR_FMT, MEI_HDR_PRM(mei_hdr));
Oren Weilfb7d8792011-05-15 13:43:42 +0300357
358 if (mei_hdr->reserved || !dev->rd_msg_hdr) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300359 dev_err(dev->dev, "corrupted message header 0x%08X\n",
Tomas Winkler10ee9072013-11-11 13:26:08 +0200360 dev->rd_msg_hdr);
Oren Weilfb7d8792011-05-15 13:43:42 +0300361 ret = -EBADMSG;
362 goto end;
363 }
364
Tomas Winkler10ee9072013-11-11 13:26:08 +0200365 if (mei_slots2data(*slots) < mei_hdr->length) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300366 dev_err(dev->dev, "less data available than length=%08x.\n",
Oren Weilfb7d8792011-05-15 13:43:42 +0300367 *slots);
368 /* we can't read the message */
Tomas Winklerb1b94b52014-03-03 00:21:28 +0200369 ret = -ENODATA;
Oren Weilfb7d8792011-05-15 13:43:42 +0300370 goto end;
371 }
372
Tomas Winkler10ee9072013-11-11 13:26:08 +0200373 /* HBM message */
374 if (mei_hdr->host_addr == 0 && mei_hdr->me_addr == 0) {
Tomas Winkler544f9462014-01-08 20:19:21 +0200375 ret = mei_hbm_dispatch(dev, mei_hdr);
376 if (ret) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300377 dev_dbg(dev->dev, "mei_hbm_dispatch failed ret = %d\n",
Tomas Winkler544f9462014-01-08 20:19:21 +0200378 ret);
379 goto end;
380 }
Tomas Winkler10ee9072013-11-11 13:26:08 +0200381 goto reset_slots;
Oren Weilfb7d8792011-05-15 13:43:42 +0300382 }
383
Alexander Usyskin83ce0742014-01-08 22:31:46 +0200384 /* find recipient cl */
Tomas Winkler10ee9072013-11-11 13:26:08 +0200385 list_for_each_entry(cl, &dev->file_list, link) {
386 if (mei_cl_hbm_equal(cl, mei_hdr)) {
387 cl_dbg(dev, cl, "got a message\n");
388 break;
389 }
390 }
391
Alexander Usyskin83ce0742014-01-08 22:31:46 +0200392 /* if no recipient cl was found we assume corrupted header */
Tomas Winkler10ee9072013-11-11 13:26:08 +0200393 if (&cl->link == &dev->file_list) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300394 dev_err(dev->dev, "no destination client found 0x%08X\n",
Tomas Winkler10ee9072013-11-11 13:26:08 +0200395 dev->rd_msg_hdr);
396 ret = -EBADMSG;
397 goto end;
398 }
399
Tomas Winklerdb4756f2015-02-10 10:39:37 +0200400 if (cl == &dev->iamthif_cl) {
401 ret = mei_amthif_irq_read_msg(cl, mei_hdr, cmpl_list);
Tomas Winkler10ee9072013-11-11 13:26:08 +0200402 } else {
Tomas Winkler3d33ff22015-02-10 10:39:36 +0200403 ret = mei_cl_irq_read_msg(cl, mei_hdr, cmpl_list);
Tomas Winkler10ee9072013-11-11 13:26:08 +0200404 }
405
Tomas Winkler3d33ff22015-02-10 10:39:36 +0200406
Tomas Winkler10ee9072013-11-11 13:26:08 +0200407reset_slots:
Oren Weilfb7d8792011-05-15 13:43:42 +0300408 /* reset the number of slots and header */
409 *slots = mei_count_full_read_slots(dev);
410 dev->rd_msg_hdr = 0;
411
412 if (*slots == -EOVERFLOW) {
413 /* overflow - reset */
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300414 dev_err(dev->dev, "resetting due to slots overflow.\n");
Oren Weilfb7d8792011-05-15 13:43:42 +0300415 /* set the event since message has been read */
416 ret = -ERANGE;
417 goto end;
418 }
419end:
420 return ret;
421}
Tomas Winkler40e0b672013-03-27 16:58:30 +0200422EXPORT_SYMBOL_GPL(mei_irq_read_handler);
Oren Weilfb7d8792011-05-15 13:43:42 +0300423
424
425/**
Tomas Winkler06ecd642013-02-06 14:06:42 +0200426 * mei_irq_write_handler - dispatch write requests
427 * after irq received
Oren Weilfb7d8792011-05-15 13:43:42 +0300428 *
Oren Weilfb7d8792011-05-15 13:43:42 +0300429 * @dev: the device structure
Tomas Winkler9a84d612012-11-18 15:13:18 +0200430 * @cmpl_list: An instance of our list structure
Oren Weilfb7d8792011-05-15 13:43:42 +0300431 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300432 * Return: 0 on success, <0 on failure.
Oren Weilfb7d8792011-05-15 13:43:42 +0300433 */
Tomas Winklerc8c8d082013-03-11 18:27:02 +0200434int mei_irq_write_handler(struct mei_device *dev, struct mei_cl_cb *cmpl_list)
Oren Weilfb7d8792011-05-15 13:43:42 +0300435{
436
437 struct mei_cl *cl;
Tomas Winkler6220d6a2013-05-12 15:34:46 +0300438 struct mei_cl_cb *cb, *next;
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200439 struct mei_cl_cb *list;
Tomas Winkler9a84d612012-11-18 15:13:18 +0200440 s32 slots;
Oren Weilfb7d8792011-05-15 13:43:42 +0300441 int ret;
442
Tomas Winkler6aae48f2014-02-19 17:35:47 +0200443
444 if (!mei_hbuf_acquire(dev))
Oren Weilfb7d8792011-05-15 13:43:42 +0300445 return 0;
Tomas Winkler6aae48f2014-02-19 17:35:47 +0200446
Tomas Winkler9a84d612012-11-18 15:13:18 +0200447 slots = mei_hbuf_empty_slots(dev);
448 if (slots <= 0)
Tomas Winkler7d5e0e52012-06-19 09:13:36 +0300449 return -EMSGSIZE;
450
Oren Weilfb7d8792011-05-15 13:43:42 +0300451 /* complete all waiting for write CB */
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300452 dev_dbg(dev->dev, "complete all waiting for write cb.\n");
Oren Weilfb7d8792011-05-15 13:43:42 +0300453
454 list = &dev->write_waiting_list;
Tomas Winkler6220d6a2013-05-12 15:34:46 +0300455 list_for_each_entry_safe(cb, next, &list->list, list) {
456 cl = cb->cl;
Oren Weilfb7d8792011-05-15 13:43:42 +0300457
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200458 cl->status = 0;
Tomas Winklerc54bf3a2015-02-10 10:39:39 +0200459 cl_dbg(dev, cl, "MEI WRITE COMPLETE\n");
460 cl->writing_state = MEI_WRITE_COMPLETE;
461 list_move_tail(&cb->list, &cmpl_list->list);
Oren Weilfb7d8792011-05-15 13:43:42 +0300462 }
463
Tomas Winklerc216fde2012-08-16 19:39:43 +0300464 if (dev->wd_state == MEI_WD_STOPPING) {
465 dev->wd_state = MEI_WD_IDLE;
Tomas Winkler58772552014-02-19 17:35:51 +0200466 wake_up(&dev->wait_stop_wd);
Oren Weilfb7d8792011-05-15 13:43:42 +0300467 }
468
Tomas Winkler64092852014-02-17 15:13:21 +0200469 if (mei_cl_is_connected(&dev->wd_cl)) {
Oren Weilfb7d8792011-05-15 13:43:42 +0300470 if (dev->wd_pending &&
Tomas Winkler90e0b5f2013-01-08 23:07:14 +0200471 mei_cl_flow_ctrl_creds(&dev->wd_cl) > 0) {
Tomas Winklerb6d81fd2014-02-19 17:35:50 +0200472 ret = mei_wd_send(dev);
473 if (ret)
474 return ret;
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300475 dev->wd_pending = false;
Oren Weilfb7d8792011-05-15 13:43:42 +0300476 }
477 }
Oren Weilfb7d8792011-05-15 13:43:42 +0300478
479 /* complete control write list CB */
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300480 dev_dbg(dev->dev, "complete control write list cb.\n");
Tomas Winkler6220d6a2013-05-12 15:34:46 +0300481 list_for_each_entry_safe(cb, next, &dev->ctrl_wr_list.list, list) {
482 cl = cb->cl;
Tomas Winkler6220d6a2013-05-12 15:34:46 +0300483 switch (cb->fop_type) {
Tomas Winkler5a8373f2014-08-21 14:29:17 +0300484 case MEI_FOP_DISCONNECT:
Tomas Winklerc8372092011-11-27 21:43:33 +0200485 /* send disconnect message */
Tomas Winkler5a8373f2014-08-21 14:29:17 +0300486 ret = mei_cl_irq_disconnect(cl, cb, cmpl_list);
Tomas Winklerc8372092011-11-27 21:43:33 +0200487 if (ret)
488 return ret;
489
490 break;
Tomas Winkler4b8960b2012-11-11 17:38:00 +0200491 case MEI_FOP_READ:
Tomas Winklerc8372092011-11-27 21:43:33 +0200492 /* send flow control message */
Tomas Winkler9d098192014-02-19 17:35:48 +0200493 ret = mei_cl_irq_read(cl, cb, cmpl_list);
Tomas Winklerc8372092011-11-27 21:43:33 +0200494 if (ret)
495 return ret;
496
497 break;
Tomas Winkler02a7eec2014-02-12 21:41:51 +0200498 case MEI_FOP_CONNECT:
Tomas Winklerc8372092011-11-27 21:43:33 +0200499 /* connect message */
Tomas Winkler9d098192014-02-19 17:35:48 +0200500 ret = mei_cl_irq_connect(cl, cb, cmpl_list);
Tomas Winklerc8372092011-11-27 21:43:33 +0200501 if (ret)
502 return ret;
503
504 break;
Tomas Winkler6bb948c2014-02-12 21:41:52 +0200505 case MEI_FOP_DISCONNECT_RSP:
506 /* send disconnect resp */
Tomas Winkler9d098192014-02-19 17:35:48 +0200507 ret = mei_cl_irq_disconnect_rsp(cl, cb, cmpl_list);
Tomas Winkler6bb948c2014-02-12 21:41:52 +0200508 if (ret)
509 return ret;
Alexander Usyskin31a5ef22014-03-06 23:53:53 +0200510 break;
Tomas Winklerc8372092011-11-27 21:43:33 +0200511 default:
512 BUG();
513 }
514
Oren Weilfb7d8792011-05-15 13:43:42 +0300515 }
516 /* complete write list CB */
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300517 dev_dbg(dev->dev, "complete write list cb.\n");
Tomas Winkler6220d6a2013-05-12 15:34:46 +0300518 list_for_each_entry_safe(cb, next, &dev->write_list.list, list) {
519 cl = cb->cl;
Tomas Winklerbe9d87a2012-11-18 15:13:19 +0200520 if (cl == &dev->iamthif_cl)
Tomas Winkler9d098192014-02-19 17:35:48 +0200521 ret = mei_amthif_irq_write(cl, cb, cmpl_list);
Tomas Winklerbe9d87a2012-11-18 15:13:19 +0200522 else
Tomas Winkler9d098192014-02-19 17:35:48 +0200523 ret = mei_cl_irq_write(cl, cb, cmpl_list);
Tomas Winklerbe9d87a2012-11-18 15:13:19 +0200524 if (ret)
525 return ret;
Oren Weilfb7d8792011-05-15 13:43:42 +0300526 }
527 return 0;
528}
Tomas Winkler40e0b672013-03-27 16:58:30 +0200529EXPORT_SYMBOL_GPL(mei_irq_write_handler);
Oren Weilfb7d8792011-05-15 13:43:42 +0300530
531
532
533/**
534 * mei_timer - timer function.
535 *
536 * @work: pointer to the work_struct structure
537 *
Oren Weilfb7d8792011-05-15 13:43:42 +0300538 */
Oren Weila61c6532011-09-07 09:03:13 +0300539void mei_timer(struct work_struct *work)
Oren Weilfb7d8792011-05-15 13:43:42 +0300540{
541 unsigned long timeout;
Tomas Winkler31f88f52014-02-17 15:13:25 +0200542 struct mei_cl *cl;
Oren Weilfb7d8792011-05-15 13:43:42 +0300543
544 struct mei_device *dev = container_of(work,
Oren Weila61c6532011-09-07 09:03:13 +0300545 struct mei_device, timer_work.work);
Oren Weilfb7d8792011-05-15 13:43:42 +0300546
547
548 mutex_lock(&dev->device_lock);
Tomas Winkler66ae4602014-01-08 20:19:22 +0200549
550 /* Catch interrupt stalls during HBM init handshake */
551 if (dev->dev_state == MEI_DEV_INIT_CLIENTS &&
552 dev->hbm_state != MEI_HBM_IDLE) {
553
554 if (dev->init_clients_timer) {
555 if (--dev->init_clients_timer == 0) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300556 dev_err(dev->dev, "timer: init clients timeout hbm_state = %d.\n",
Tomas Winkler66ae4602014-01-08 20:19:22 +0200557 dev->hbm_state);
Tomas Winkler33ec0822014-01-12 00:36:09 +0200558 mei_reset(dev);
Tomas Winkler66ae4602014-01-08 20:19:22 +0200559 goto out;
Oren Weilfb7d8792011-05-15 13:43:42 +0300560 }
561 }
Oren Weilfb7d8792011-05-15 13:43:42 +0300562 }
Tomas Winkler66ae4602014-01-08 20:19:22 +0200563
564 if (dev->dev_state != MEI_DEV_ENABLED)
565 goto out;
566
Oren Weilfb7d8792011-05-15 13:43:42 +0300567 /*** connect/disconnect timeouts ***/
Tomas Winkler31f88f52014-02-17 15:13:25 +0200568 list_for_each_entry(cl, &dev->file_list, link) {
569 if (cl->timer_count) {
570 if (--cl->timer_count == 0) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300571 dev_err(dev->dev, "timer: connect/disconnect timeout.\n");
Tomas Winkler33ec0822014-01-12 00:36:09 +0200572 mei_reset(dev);
Oren Weilfb7d8792011-05-15 13:43:42 +0300573 goto out;
574 }
575 }
576 }
577
Tomas Winkler64092852014-02-17 15:13:21 +0200578 if (!mei_cl_is_connected(&dev->iamthif_cl))
579 goto out;
580
Oren Weilfb7d8792011-05-15 13:43:42 +0300581 if (dev->iamthif_stall_timer) {
582 if (--dev->iamthif_stall_timer == 0) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300583 dev_err(dev->dev, "timer: amthif hanged.\n");
Tomas Winkler33ec0822014-01-12 00:36:09 +0200584 mei_reset(dev);
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300585 dev->iamthif_canceled = false;
Oren Weilfb7d8792011-05-15 13:43:42 +0300586 dev->iamthif_state = MEI_IAMTHIF_IDLE;
587 dev->iamthif_timer = 0;
588
Tomas Winkler601a1ef2012-10-09 16:50:20 +0200589 mei_io_cb_free(dev->iamthif_current_cb);
590 dev->iamthif_current_cb = NULL;
Oren Weilfb7d8792011-05-15 13:43:42 +0300591
592 dev->iamthif_file_object = NULL;
Tomas Winkler19838fb2012-11-01 21:17:15 +0200593 mei_amthif_run_next_cmd(dev);
Oren Weilfb7d8792011-05-15 13:43:42 +0300594 }
595 }
596
597 if (dev->iamthif_timer) {
598
599 timeout = dev->iamthif_timer +
Tomas Winkler3870c322012-11-01 21:17:14 +0200600 mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER);
Oren Weilfb7d8792011-05-15 13:43:42 +0300601
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300602 dev_dbg(dev->dev, "dev->iamthif_timer = %ld\n",
Oren Weilfb7d8792011-05-15 13:43:42 +0300603 dev->iamthif_timer);
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300604 dev_dbg(dev->dev, "timeout = %ld\n", timeout);
605 dev_dbg(dev->dev, "jiffies = %ld\n", jiffies);
Oren Weilfb7d8792011-05-15 13:43:42 +0300606 if (time_after(jiffies, timeout)) {
607 /*
608 * User didn't read the AMTHI data on time (15sec)
609 * freeing AMTHI for other requests
610 */
611
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300612 dev_dbg(dev->dev, "freeing AMTHI for other requests\n");
Oren Weilfb7d8792011-05-15 13:43:42 +0300613
Alexander Usyskin54567962014-08-14 17:22:20 +0300614 mei_io_list_flush(&dev->amthif_rd_complete_list,
615 &dev->iamthif_cl);
Tomas Winkler601a1ef2012-10-09 16:50:20 +0200616 mei_io_cb_free(dev->iamthif_current_cb);
617 dev->iamthif_current_cb = NULL;
Oren Weilfb7d8792011-05-15 13:43:42 +0300618
619 dev->iamthif_file_object->private_data = NULL;
620 dev->iamthif_file_object = NULL;
Oren Weilfb7d8792011-05-15 13:43:42 +0300621 dev->iamthif_timer = 0;
Tomas Winkler19838fb2012-11-01 21:17:15 +0200622 mei_amthif_run_next_cmd(dev);
Oren Weilfb7d8792011-05-15 13:43:42 +0300623
624 }
625 }
626out:
Tomas Winkler33ec0822014-01-12 00:36:09 +0200627 if (dev->dev_state != MEI_DEV_DISABLED)
628 schedule_delayed_work(&dev->timer_work, 2 * HZ);
Tomas Winkler441ab502011-12-13 23:39:34 +0200629 mutex_unlock(&dev->device_lock);
Oren Weilfb7d8792011-05-15 13:43:42 +0300630}