blob: 1e5cb1f704f809d56c350969953567e091203227 [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>
Alexander Usyskin34af1912015-10-13 15:02:38 +030024#include <linux/pm_runtime.h>
Oren Weilfb7d8792011-05-15 13:43:42 +030025
Tomas Winkler4f3afe12012-05-09 16:38:59 +030026#include <linux/mei.h>
Tomas Winkler47a73802012-12-25 19:06:03 +020027
28#include "mei_dev.h"
Tomas Winkler0edb23f2013-01-08 23:07:12 +020029#include "hbm.h"
Tomas Winkler90e0b5f2013-01-08 23:07:14 +020030#include "client.h"
Oren Weilfb7d8792011-05-15 13:43:42 +030031
32
33/**
Alexander Usyskin83ce0742014-01-08 22:31:46 +020034 * mei_irq_compl_handler - dispatch complete handlers
Tomas Winkler4c6e22b2013-03-17 11:41:20 +020035 * for the completed callbacks
36 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +030037 * @dev: mei device
38 * @compl_list: list of completed cbs
Tomas Winkler4c6e22b2013-03-17 11:41:20 +020039 */
40void mei_irq_compl_handler(struct mei_device *dev, struct mei_cl_cb *compl_list)
41{
42 struct mei_cl_cb *cb, *next;
43 struct mei_cl *cl;
44
45 list_for_each_entry_safe(cb, next, &compl_list->list, list) {
46 cl = cb->cl;
Tomas Winklerc54bf3a2015-02-10 10:39:39 +020047 list_del_init(&cb->list);
Tomas Winkler4c6e22b2013-03-17 11:41:20 +020048
Tomas Winkler2bf94cab2014-09-29 16:31:42 +030049 dev_dbg(dev->dev, "completing call back.\n");
Tomas Winkler4c6e22b2013-03-17 11:41:20 +020050 if (cl == &dev->iamthif_cl)
Tomas Winkler9abd8b32016-02-07 23:35:26 +020051 mei_amthif_complete(cl, cb);
Tomas Winkler4c6e22b2013-03-17 11:41:20 +020052 else
Tomas Winklerdb086fa2013-05-12 15:34:45 +030053 mei_cl_complete(cl, cb);
Tomas Winkler4c6e22b2013-03-17 11:41:20 +020054 }
55}
Tomas Winkler40e0b672013-03-27 16:58:30 +020056EXPORT_SYMBOL_GPL(mei_irq_compl_handler);
Tomas Winkler6e0f1802013-04-19 22:01:34 +030057
Tomas Winkler4c6e22b2013-03-17 11:41:20 +020058/**
Tomas Winkler6e0f1802013-04-19 22:01:34 +030059 * mei_cl_hbm_equal - check if hbm is addressed to the client
Oren Weilfb7d8792011-05-15 13:43:42 +030060 *
Tomas Winkler6e0f1802013-04-19 22:01:34 +030061 * @cl: host client
Oren Weilfb7d8792011-05-15 13:43:42 +030062 * @mei_hdr: header of mei client message
63 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +030064 * Return: true if matches, false otherwise
Oren Weilfb7d8792011-05-15 13:43:42 +030065 */
Tomas Winkler6e0f1802013-04-19 22:01:34 +030066static inline int mei_cl_hbm_equal(struct mei_cl *cl,
67 struct mei_msg_hdr *mei_hdr)
Oren Weilfb7d8792011-05-15 13:43:42 +030068{
Alexander Usyskin1df629e2015-05-04 09:43:56 +030069 return mei_cl_host_addr(cl) == mei_hdr->host_addr &&
Alexander Usyskind49ed642015-05-04 09:43:54 +030070 mei_cl_me_id(cl) == mei_hdr->me_addr;
Tomas Winkler6e0f1802013-04-19 22:01:34 +030071}
Oren Weilfb7d8792011-05-15 13:43:42 +030072
73/**
Tomas Winkler331e4182015-02-10 10:39:41 +020074 * mei_irq_discard_msg - discard received message
75 *
76 * @dev: mei device
77 * @hdr: message header
78 */
79static inline
80void mei_irq_discard_msg(struct mei_device *dev, struct mei_msg_hdr *hdr)
81{
82 /*
83 * no need to check for size as it is guarantied
84 * that length fits into rd_msg_buf
85 */
86 mei_read_slots(dev, dev->rd_msg_buf, hdr->length);
87 dev_dbg(dev->dev, "discarding message " MEI_HDR_FMT "\n",
88 MEI_HDR_PRM(hdr));
89}
90
91/**
Alexander Usyskince231392014-09-29 16:31:50 +030092 * mei_cl_irq_read_msg - process client message
Oren Weilfb7d8792011-05-15 13:43:42 +030093 *
Tomas Winkler3d33ff22015-02-10 10:39:36 +020094 * @cl: reading client
Oren Weilfb7d8792011-05-15 13:43:42 +030095 * @mei_hdr: header of mei client message
Tomas Winkler3d33ff22015-02-10 10:39:36 +020096 * @complete_list: completion list
Oren Weilfb7d8792011-05-15 13:43:42 +030097 *
Tomas Winkler3d33ff22015-02-10 10:39:36 +020098 * Return: always 0
Oren Weilfb7d8792011-05-15 13:43:42 +030099 */
Tomas Winkler331e4182015-02-10 10:39:41 +0200100int mei_cl_irq_read_msg(struct mei_cl *cl,
101 struct mei_msg_hdr *mei_hdr,
102 struct mei_cl_cb *complete_list)
Oren Weilfb7d8792011-05-15 13:43:42 +0300103{
Tomas Winkler3d33ff22015-02-10 10:39:36 +0200104 struct mei_device *dev = cl->dev;
105 struct mei_cl_cb *cb;
Tomas Winkler479bc592011-06-16 00:46:03 +0300106 unsigned char *buffer = NULL;
Tomas Winklerf862b6b2016-02-07 23:35:19 +0200107 size_t buf_sz;
Oren Weilfb7d8792011-05-15 13:43:42 +0300108
Tomas Winklera9bed612015-02-10 10:39:46 +0200109 cb = list_first_entry_or_null(&cl->rd_pending, struct mei_cl_cb, list);
110 if (!cb) {
111 cl_err(dev, cl, "pending read cb not found\n");
Tomas Winkler3d33ff22015-02-10 10:39:36 +0200112 goto out;
113 }
114
Tomas Winklerf3de9b62015-03-27 00:27:58 +0200115 if (!mei_cl_is_connected(cl)) {
Tomas Winklera9bed612015-02-10 10:39:46 +0200116 cl_dbg(dev, cl, "not connected\n");
117 cb->status = -ENODEV;
Tomas Winkler3d33ff22015-02-10 10:39:36 +0200118 goto out;
119 }
120
Tomas Winkler5db75142015-02-10 10:39:42 +0200121 if (cb->buf.size == 0 || cb->buf.data == NULL) {
Tomas Winkler3d33ff22015-02-10 10:39:36 +0200122 cl_err(dev, cl, "response buffer is not allocated.\n");
123 list_move_tail(&cb->list, &complete_list->list);
124 cb->status = -ENOMEM;
125 goto out;
126 }
127
Tomas Winklerf862b6b2016-02-07 23:35:19 +0200128 buf_sz = mei_hdr->length + cb->buf_idx;
129 /* catch for integer overflow */
130 if (buf_sz < cb->buf_idx) {
Alexander Usyskin35bf7692016-02-17 18:27:34 +0200131 cl_err(dev, cl, "message is too big len %d idx %zu\n",
Tomas Winklerf862b6b2016-02-07 23:35:19 +0200132 mei_hdr->length, cb->buf_idx);
133
134 list_move_tail(&cb->list, &complete_list->list);
135 cb->status = -EMSGSIZE;
136 goto out;
137 }
138
139 if (cb->buf.size < buf_sz) {
Alexander Usyskin35bf7692016-02-17 18:27:34 +0200140 cl_dbg(dev, cl, "message overflow. size %zu len %d idx %zu\n",
Tomas Winkler5db75142015-02-10 10:39:42 +0200141 cb->buf.size, mei_hdr->length, cb->buf_idx);
Tomas Winklerf862b6b2016-02-07 23:35:19 +0200142 buffer = krealloc(cb->buf.data, buf_sz, GFP_KERNEL);
Tomas Winkler3d33ff22015-02-10 10:39:36 +0200143
144 if (!buffer) {
145 cb->status = -ENOMEM;
146 list_move_tail(&cb->list, &complete_list->list);
147 goto out;
148 }
Tomas Winkler5db75142015-02-10 10:39:42 +0200149 cb->buf.data = buffer;
Tomas Winklerf862b6b2016-02-07 23:35:19 +0200150 cb->buf.size = buf_sz;
Tomas Winkler3d33ff22015-02-10 10:39:36 +0200151 }
152
Tomas Winkler5db75142015-02-10 10:39:42 +0200153 buffer = cb->buf.data + cb->buf_idx;
Tomas Winkler3d33ff22015-02-10 10:39:36 +0200154 mei_read_slots(dev, buffer, mei_hdr->length);
155
156 cb->buf_idx += mei_hdr->length;
Tomas Winkler331e4182015-02-10 10:39:41 +0200157
Tomas Winkler3d33ff22015-02-10 10:39:36 +0200158 if (mei_hdr->msg_complete) {
Alexander Usyskin35bf7692016-02-17 18:27:34 +0200159 cl_dbg(dev, cl, "completed read length = %zu\n", cb->buf_idx);
Tomas Winkler3d33ff22015-02-10 10:39:36 +0200160 list_move_tail(&cb->list, &complete_list->list);
Alexander Usyskin34af1912015-10-13 15:02:38 +0300161 } else {
162 pm_runtime_mark_last_busy(dev->dev);
163 pm_request_autosuspend(dev->dev);
Tomas Winkler3d33ff22015-02-10 10:39:36 +0200164 }
165
166out:
Tomas Winkler331e4182015-02-10 10:39:41 +0200167 if (!buffer)
168 mei_irq_discard_msg(dev, mei_hdr);
Oren Weilfb7d8792011-05-15 13:43:42 +0300169
170 return 0;
171}
172
173/**
Tomas Winkler6bb948c2014-02-12 21:41:52 +0200174 * mei_cl_irq_disconnect_rsp - send disconnection response message
175 *
176 * @cl: client
177 * @cb: callback block.
Tomas Winkler6bb948c2014-02-12 21:41:52 +0200178 * @cmpl_list: complete list.
179 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300180 * Return: 0, OK; otherwise, error.
Tomas Winkler6bb948c2014-02-12 21:41:52 +0200181 */
182static int mei_cl_irq_disconnect_rsp(struct mei_cl *cl, struct mei_cl_cb *cb,
Tomas Winkler9d098192014-02-19 17:35:48 +0200183 struct mei_cl_cb *cmpl_list)
Tomas Winkler6bb948c2014-02-12 21:41:52 +0200184{
185 struct mei_device *dev = cl->dev;
Tomas Winkler9d098192014-02-19 17:35:48 +0200186 u32 msg_slots;
187 int slots;
Tomas Winkler6bb948c2014-02-12 21:41:52 +0200188 int ret;
189
Tomas Winkler9d098192014-02-19 17:35:48 +0200190 slots = mei_hbuf_empty_slots(dev);
191 msg_slots = mei_data2slots(sizeof(struct hbm_client_connect_response));
Tomas Winkler6bb948c2014-02-12 21:41:52 +0200192
Tomas Winkler9d098192014-02-19 17:35:48 +0200193 if (slots < msg_slots)
Tomas Winkler6bb948c2014-02-12 21:41:52 +0200194 return -EMSGSIZE;
195
Tomas Winkler6bb948c2014-02-12 21:41:52 +0200196 ret = mei_hbm_cl_disconnect_rsp(dev, cl);
Tomas Winkler3c666182015-05-04 09:43:52 +0300197 mei_cl_set_disconnected(cl);
Tomas Winkler6bb948c2014-02-12 21:41:52 +0200198 mei_io_cb_free(cb);
Alexander Usyskind49ed642015-05-04 09:43:54 +0300199 mei_me_cl_put(cl->me_cl);
200 cl->me_cl = NULL;
Tomas Winkler6bb948c2014-02-12 21:41:52 +0200201
202 return ret;
203}
204
Oren Weilfb7d8792011-05-15 13:43:42 +0300205/**
Alexander Usyskince231392014-09-29 16:31:50 +0300206 * mei_cl_irq_read - processes client read related operation from the
Tomas Winkler6220d6a2013-05-12 15:34:46 +0300207 * interrupt thread context - request for flow control credits
Oren Weilfb7d8792011-05-15 13:43:42 +0300208 *
Tomas Winkler6220d6a2013-05-12 15:34:46 +0300209 * @cl: client
210 * @cb: callback block.
Oren Weilfb7d8792011-05-15 13:43:42 +0300211 * @cmpl_list: complete list.
212 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300213 * Return: 0, OK; otherwise, error.
Oren Weilfb7d8792011-05-15 13:43:42 +0300214 */
Tomas Winkler6220d6a2013-05-12 15:34:46 +0300215static int mei_cl_irq_read(struct mei_cl *cl, struct mei_cl_cb *cb,
Tomas Winkler9d098192014-02-19 17:35:48 +0200216 struct mei_cl_cb *cmpl_list)
Oren Weilfb7d8792011-05-15 13:43:42 +0300217{
Tomas Winkler6220d6a2013-05-12 15:34:46 +0300218 struct mei_device *dev = cl->dev;
Tomas Winkler9d098192014-02-19 17:35:48 +0200219 u32 msg_slots;
220 int slots;
Tomas Winkler2ebf8c92013-09-16 23:44:43 +0300221 int ret;
222
Tomas Winkler9d098192014-02-19 17:35:48 +0200223 msg_slots = mei_data2slots(sizeof(struct hbm_flow_control));
224 slots = mei_hbuf_empty_slots(dev);
Tomas Winkler2ebf8c92013-09-16 23:44:43 +0300225
Tomas Winkler9d098192014-02-19 17:35:48 +0200226 if (slots < msg_slots)
Tomas Winklerc8c8d082013-03-11 18:27:02 +0200227 return -EMSGSIZE;
Tomas Winkler7bdf72d2012-07-04 19:24:52 +0300228
Tomas Winkler2ebf8c92013-09-16 23:44:43 +0300229 ret = mei_hbm_cl_flow_control_req(dev, cl);
230 if (ret) {
231 cl->status = ret;
Tomas Winkler6220d6a2013-05-12 15:34:46 +0300232 cb->buf_idx = 0;
233 list_move_tail(&cb->list, &cmpl_list->list);
Tomas Winkler2ebf8c92013-09-16 23:44:43 +0300234 return ret;
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200235 }
Tomas Winkler2ebf8c92013-09-16 23:44:43 +0300236
Tomas Winklera9bed612015-02-10 10:39:46 +0200237 list_move_tail(&cb->list, &cl->rd_pending);
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200238
Oren Weilfb7d8792011-05-15 13:43:42 +0300239 return 0;
240}
241
Alexander Usyskin603c53e2016-02-07 23:35:36 +0200242static inline bool hdr_is_hbm(struct mei_msg_hdr *mei_hdr)
243{
244 return mei_hdr->host_addr == 0 && mei_hdr->me_addr == 0;
245}
246
247static inline bool hdr_is_fixed(struct mei_msg_hdr *mei_hdr)
248{
249 return mei_hdr->host_addr == 0 && mei_hdr->me_addr != 0;
250}
251
Oren Weilfb7d8792011-05-15 13:43:42 +0300252/**
Masanari Iida393b1482013-04-05 01:05:05 +0900253 * mei_irq_read_handler - bottom half read routine after ISR to
Oren Weilfb7d8792011-05-15 13:43:42 +0300254 * handle the read processing.
255 *
Oren Weilfb7d8792011-05-15 13:43:42 +0300256 * @dev: the device structure
Tomas Winkler06ecd642013-02-06 14:06:42 +0200257 * @cmpl_list: An instance of our list structure
Oren Weilfb7d8792011-05-15 13:43:42 +0300258 * @slots: slots to read.
259 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300260 * Return: 0 on success, <0 on failure.
Oren Weilfb7d8792011-05-15 13:43:42 +0300261 */
Tomas Winkler06ecd642013-02-06 14:06:42 +0200262int mei_irq_read_handler(struct mei_device *dev,
263 struct mei_cl_cb *cmpl_list, s32 *slots)
Oren Weilfb7d8792011-05-15 13:43:42 +0300264{
265 struct mei_msg_hdr *mei_hdr;
Tomas Winkler10ee9072013-11-11 13:26:08 +0200266 struct mei_cl *cl;
267 int ret;
Oren Weilfb7d8792011-05-15 13:43:42 +0300268
269 if (!dev->rd_msg_hdr) {
Tomas Winkler827eef52013-02-06 14:06:41 +0200270 dev->rd_msg_hdr = mei_read_hdr(dev);
Oren Weilfb7d8792011-05-15 13:43:42 +0300271 (*slots)--;
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300272 dev_dbg(dev->dev, "slots =%08x.\n", *slots);
Oren Weilfb7d8792011-05-15 13:43:42 +0300273 }
274 mei_hdr = (struct mei_msg_hdr *) &dev->rd_msg_hdr;
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300275 dev_dbg(dev->dev, MEI_HDR_FMT, MEI_HDR_PRM(mei_hdr));
Oren Weilfb7d8792011-05-15 13:43:42 +0300276
277 if (mei_hdr->reserved || !dev->rd_msg_hdr) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300278 dev_err(dev->dev, "corrupted message header 0x%08X\n",
Tomas Winkler10ee9072013-11-11 13:26:08 +0200279 dev->rd_msg_hdr);
Oren Weilfb7d8792011-05-15 13:43:42 +0300280 ret = -EBADMSG;
281 goto end;
282 }
283
Tomas Winkler10ee9072013-11-11 13:26:08 +0200284 if (mei_slots2data(*slots) < mei_hdr->length) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300285 dev_err(dev->dev, "less data available than length=%08x.\n",
Oren Weilfb7d8792011-05-15 13:43:42 +0300286 *slots);
287 /* we can't read the message */
Tomas Winklerb1b94b52014-03-03 00:21:28 +0200288 ret = -ENODATA;
Oren Weilfb7d8792011-05-15 13:43:42 +0300289 goto end;
290 }
291
Tomas Winkler10ee9072013-11-11 13:26:08 +0200292 /* HBM message */
Alexander Usyskin603c53e2016-02-07 23:35:36 +0200293 if (hdr_is_hbm(mei_hdr)) {
Tomas Winkler544f9462014-01-08 20:19:21 +0200294 ret = mei_hbm_dispatch(dev, mei_hdr);
295 if (ret) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300296 dev_dbg(dev->dev, "mei_hbm_dispatch failed ret = %d\n",
Tomas Winkler544f9462014-01-08 20:19:21 +0200297 ret);
298 goto end;
299 }
Tomas Winkler10ee9072013-11-11 13:26:08 +0200300 goto reset_slots;
Oren Weilfb7d8792011-05-15 13:43:42 +0300301 }
302
Alexander Usyskin83ce0742014-01-08 22:31:46 +0200303 /* find recipient cl */
Tomas Winkler10ee9072013-11-11 13:26:08 +0200304 list_for_each_entry(cl, &dev->file_list, link) {
305 if (mei_cl_hbm_equal(cl, mei_hdr)) {
306 cl_dbg(dev, cl, "got a message\n");
307 break;
308 }
309 }
310
Alexander Usyskin83ce0742014-01-08 22:31:46 +0200311 /* if no recipient cl was found we assume corrupted header */
Tomas Winkler10ee9072013-11-11 13:26:08 +0200312 if (&cl->link == &dev->file_list) {
Alexander Usyskin603c53e2016-02-07 23:35:36 +0200313 /* A message for not connected fixed address clients
314 * should be silently discarded
315 */
316 if (hdr_is_fixed(mei_hdr)) {
317 mei_irq_discard_msg(dev, mei_hdr);
318 ret = 0;
319 goto reset_slots;
320 }
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300321 dev_err(dev->dev, "no destination client found 0x%08X\n",
Tomas Winkler10ee9072013-11-11 13:26:08 +0200322 dev->rd_msg_hdr);
323 ret = -EBADMSG;
324 goto end;
325 }
326
Tomas Winklerdb4756f2015-02-10 10:39:37 +0200327 if (cl == &dev->iamthif_cl) {
328 ret = mei_amthif_irq_read_msg(cl, mei_hdr, cmpl_list);
Tomas Winkler10ee9072013-11-11 13:26:08 +0200329 } else {
Tomas Winkler3d33ff22015-02-10 10:39:36 +0200330 ret = mei_cl_irq_read_msg(cl, mei_hdr, cmpl_list);
Tomas Winkler10ee9072013-11-11 13:26:08 +0200331 }
332
Tomas Winkler3d33ff22015-02-10 10:39:36 +0200333
Tomas Winkler10ee9072013-11-11 13:26:08 +0200334reset_slots:
Oren Weilfb7d8792011-05-15 13:43:42 +0300335 /* reset the number of slots and header */
336 *slots = mei_count_full_read_slots(dev);
337 dev->rd_msg_hdr = 0;
338
339 if (*slots == -EOVERFLOW) {
340 /* overflow - reset */
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300341 dev_err(dev->dev, "resetting due to slots overflow.\n");
Oren Weilfb7d8792011-05-15 13:43:42 +0300342 /* set the event since message has been read */
343 ret = -ERANGE;
344 goto end;
345 }
346end:
347 return ret;
348}
Tomas Winkler40e0b672013-03-27 16:58:30 +0200349EXPORT_SYMBOL_GPL(mei_irq_read_handler);
Oren Weilfb7d8792011-05-15 13:43:42 +0300350
351
352/**
Tomas Winkler06ecd642013-02-06 14:06:42 +0200353 * mei_irq_write_handler - dispatch write requests
354 * after irq received
Oren Weilfb7d8792011-05-15 13:43:42 +0300355 *
Oren Weilfb7d8792011-05-15 13:43:42 +0300356 * @dev: the device structure
Tomas Winkler9a84d612012-11-18 15:13:18 +0200357 * @cmpl_list: An instance of our list structure
Oren Weilfb7d8792011-05-15 13:43:42 +0300358 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300359 * Return: 0 on success, <0 on failure.
Oren Weilfb7d8792011-05-15 13:43:42 +0300360 */
Tomas Winklerc8c8d082013-03-11 18:27:02 +0200361int mei_irq_write_handler(struct mei_device *dev, struct mei_cl_cb *cmpl_list)
Oren Weilfb7d8792011-05-15 13:43:42 +0300362{
363
364 struct mei_cl *cl;
Tomas Winkler6220d6a2013-05-12 15:34:46 +0300365 struct mei_cl_cb *cb, *next;
Tomas Winklerfb601ad2012-10-15 12:06:48 +0200366 struct mei_cl_cb *list;
Tomas Winkler9a84d612012-11-18 15:13:18 +0200367 s32 slots;
Oren Weilfb7d8792011-05-15 13:43:42 +0300368 int ret;
369
Tomas Winkler6aae48f2014-02-19 17:35:47 +0200370
371 if (!mei_hbuf_acquire(dev))
Oren Weilfb7d8792011-05-15 13:43:42 +0300372 return 0;
Tomas Winkler6aae48f2014-02-19 17:35:47 +0200373
Tomas Winkler9a84d612012-11-18 15:13:18 +0200374 slots = mei_hbuf_empty_slots(dev);
375 if (slots <= 0)
Tomas Winkler7d5e0e52012-06-19 09:13:36 +0300376 return -EMSGSIZE;
377
Oren Weilfb7d8792011-05-15 13:43:42 +0300378 /* complete all waiting for write CB */
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300379 dev_dbg(dev->dev, "complete all waiting for write cb.\n");
Oren Weilfb7d8792011-05-15 13:43:42 +0300380
381 list = &dev->write_waiting_list;
Tomas Winkler6220d6a2013-05-12 15:34:46 +0300382 list_for_each_entry_safe(cb, next, &list->list, list) {
383 cl = cb->cl;
Oren Weilfb7d8792011-05-15 13:43:42 +0300384
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200385 cl->status = 0;
Tomas Winklerc54bf3a2015-02-10 10:39:39 +0200386 cl_dbg(dev, cl, "MEI WRITE COMPLETE\n");
387 cl->writing_state = MEI_WRITE_COMPLETE;
388 list_move_tail(&cb->list, &cmpl_list->list);
Oren Weilfb7d8792011-05-15 13:43:42 +0300389 }
390
Oren Weilfb7d8792011-05-15 13:43:42 +0300391 /* complete control write list CB */
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300392 dev_dbg(dev->dev, "complete control write list cb.\n");
Tomas Winkler6220d6a2013-05-12 15:34:46 +0300393 list_for_each_entry_safe(cb, next, &dev->ctrl_wr_list.list, list) {
394 cl = cb->cl;
Tomas Winkler6220d6a2013-05-12 15:34:46 +0300395 switch (cb->fop_type) {
Tomas Winkler5a8373f2014-08-21 14:29:17 +0300396 case MEI_FOP_DISCONNECT:
Tomas Winklerc8372092011-11-27 21:43:33 +0200397 /* send disconnect message */
Tomas Winkler5a8373f2014-08-21 14:29:17 +0300398 ret = mei_cl_irq_disconnect(cl, cb, cmpl_list);
Tomas Winklerc8372092011-11-27 21:43:33 +0200399 if (ret)
400 return ret;
401
402 break;
Tomas Winkler4b8960b2012-11-11 17:38:00 +0200403 case MEI_FOP_READ:
Tomas Winklerc8372092011-11-27 21:43:33 +0200404 /* send flow control message */
Tomas Winkler9d098192014-02-19 17:35:48 +0200405 ret = mei_cl_irq_read(cl, cb, cmpl_list);
Tomas Winklerc8372092011-11-27 21:43:33 +0200406 if (ret)
407 return ret;
408
409 break;
Tomas Winkler02a7eec2014-02-12 21:41:51 +0200410 case MEI_FOP_CONNECT:
Tomas Winklerc8372092011-11-27 21:43:33 +0200411 /* connect message */
Tomas Winkler9d098192014-02-19 17:35:48 +0200412 ret = mei_cl_irq_connect(cl, cb, cmpl_list);
Tomas Winklerc8372092011-11-27 21:43:33 +0200413 if (ret)
414 return ret;
415
416 break;
Tomas Winkler6bb948c2014-02-12 21:41:52 +0200417 case MEI_FOP_DISCONNECT_RSP:
418 /* send disconnect resp */
Tomas Winkler9d098192014-02-19 17:35:48 +0200419 ret = mei_cl_irq_disconnect_rsp(cl, cb, cmpl_list);
Tomas Winkler6bb948c2014-02-12 21:41:52 +0200420 if (ret)
421 return ret;
Alexander Usyskin31a5ef22014-03-06 23:53:53 +0200422 break;
Tomas Winkler51678cc2015-07-26 09:54:18 +0300423
424 case MEI_FOP_NOTIFY_START:
425 case MEI_FOP_NOTIFY_STOP:
426 ret = mei_cl_irq_notify(cl, cb, cmpl_list);
427 if (ret)
428 return ret;
429 break;
Tomas Winklerc8372092011-11-27 21:43:33 +0200430 default:
431 BUG();
432 }
433
Oren Weilfb7d8792011-05-15 13:43:42 +0300434 }
435 /* complete write list CB */
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300436 dev_dbg(dev->dev, "complete write list cb.\n");
Tomas Winkler6220d6a2013-05-12 15:34:46 +0300437 list_for_each_entry_safe(cb, next, &dev->write_list.list, list) {
438 cl = cb->cl;
Tomas Winklerbe9d87a2012-11-18 15:13:19 +0200439 if (cl == &dev->iamthif_cl)
Tomas Winkler9d098192014-02-19 17:35:48 +0200440 ret = mei_amthif_irq_write(cl, cb, cmpl_list);
Tomas Winklerbe9d87a2012-11-18 15:13:19 +0200441 else
Tomas Winkler9d098192014-02-19 17:35:48 +0200442 ret = mei_cl_irq_write(cl, cb, cmpl_list);
Tomas Winklerbe9d87a2012-11-18 15:13:19 +0200443 if (ret)
444 return ret;
Oren Weilfb7d8792011-05-15 13:43:42 +0300445 }
446 return 0;
447}
Tomas Winkler40e0b672013-03-27 16:58:30 +0200448EXPORT_SYMBOL_GPL(mei_irq_write_handler);
Oren Weilfb7d8792011-05-15 13:43:42 +0300449
450
Alexander Usyskin18901352015-07-23 21:37:13 +0300451/**
452 * mei_connect_timeout - connect/disconnect timeouts
453 *
454 * @cl: host client
455 */
456static void mei_connect_timeout(struct mei_cl *cl)
457{
458 struct mei_device *dev = cl->dev;
459
460 if (cl->state == MEI_FILE_CONNECTING) {
461 if (dev->hbm_f_dot_supported) {
462 cl->state = MEI_FILE_DISCONNECT_REQUIRED;
463 wake_up(&cl->wait);
464 return;
465 }
466 }
467 mei_reset(dev);
468}
Oren Weilfb7d8792011-05-15 13:43:42 +0300469
470/**
471 * mei_timer - timer function.
472 *
473 * @work: pointer to the work_struct structure
474 *
Oren Weilfb7d8792011-05-15 13:43:42 +0300475 */
Oren Weila61c6532011-09-07 09:03:13 +0300476void mei_timer(struct work_struct *work)
Oren Weilfb7d8792011-05-15 13:43:42 +0300477{
Tomas Winkler31f88f52014-02-17 15:13:25 +0200478 struct mei_cl *cl;
Oren Weilfb7d8792011-05-15 13:43:42 +0300479
480 struct mei_device *dev = container_of(work,
Oren Weila61c6532011-09-07 09:03:13 +0300481 struct mei_device, timer_work.work);
Oren Weilfb7d8792011-05-15 13:43:42 +0300482
483
484 mutex_lock(&dev->device_lock);
Tomas Winkler66ae4602014-01-08 20:19:22 +0200485
486 /* Catch interrupt stalls during HBM init handshake */
487 if (dev->dev_state == MEI_DEV_INIT_CLIENTS &&
488 dev->hbm_state != MEI_HBM_IDLE) {
489
490 if (dev->init_clients_timer) {
491 if (--dev->init_clients_timer == 0) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300492 dev_err(dev->dev, "timer: init clients timeout hbm_state = %d.\n",
Tomas Winkler66ae4602014-01-08 20:19:22 +0200493 dev->hbm_state);
Tomas Winkler33ec0822014-01-12 00:36:09 +0200494 mei_reset(dev);
Tomas Winkler66ae4602014-01-08 20:19:22 +0200495 goto out;
Oren Weilfb7d8792011-05-15 13:43:42 +0300496 }
497 }
Oren Weilfb7d8792011-05-15 13:43:42 +0300498 }
Tomas Winkler66ae4602014-01-08 20:19:22 +0200499
500 if (dev->dev_state != MEI_DEV_ENABLED)
501 goto out;
502
Oren Weilfb7d8792011-05-15 13:43:42 +0300503 /*** connect/disconnect timeouts ***/
Tomas Winkler31f88f52014-02-17 15:13:25 +0200504 list_for_each_entry(cl, &dev->file_list, link) {
505 if (cl->timer_count) {
506 if (--cl->timer_count == 0) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300507 dev_err(dev->dev, "timer: connect/disconnect timeout.\n");
Alexander Usyskin18901352015-07-23 21:37:13 +0300508 mei_connect_timeout(cl);
Oren Weilfb7d8792011-05-15 13:43:42 +0300509 goto out;
510 }
511 }
512 }
513
Tomas Winkler64092852014-02-17 15:13:21 +0200514 if (!mei_cl_is_connected(&dev->iamthif_cl))
515 goto out;
516
Oren Weilfb7d8792011-05-15 13:43:42 +0300517 if (dev->iamthif_stall_timer) {
518 if (--dev->iamthif_stall_timer == 0) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300519 dev_err(dev->dev, "timer: amthif hanged.\n");
Tomas Winkler33ec0822014-01-12 00:36:09 +0200520 mei_reset(dev);
Tomas Winklereb9af0a2011-05-25 17:28:22 +0300521 dev->iamthif_canceled = false;
Oren Weilfb7d8792011-05-15 13:43:42 +0300522 dev->iamthif_state = MEI_IAMTHIF_IDLE;
Oren Weilfb7d8792011-05-15 13:43:42 +0300523
Tomas Winkler601a1ef2012-10-09 16:50:20 +0200524 mei_io_cb_free(dev->iamthif_current_cb);
525 dev->iamthif_current_cb = NULL;
Oren Weilfb7d8792011-05-15 13:43:42 +0300526
Tomas Winkler62e8e6a2016-02-07 23:35:24 +0200527 dev->iamthif_fp = NULL;
Tomas Winkler19838fb2012-11-01 21:17:15 +0200528 mei_amthif_run_next_cmd(dev);
Oren Weilfb7d8792011-05-15 13:43:42 +0300529 }
530 }
531
Oren Weilfb7d8792011-05-15 13:43:42 +0300532out:
Tomas Winkler33ec0822014-01-12 00:36:09 +0200533 if (dev->dev_state != MEI_DEV_DISABLED)
534 schedule_delayed_work(&dev->timer_work, 2 * HZ);
Tomas Winkler441ab502011-12-13 23:39:34 +0200535 mutex_unlock(&dev->device_lock);
Oren Weilfb7d8792011-05-15 13:43:42 +0300536}