blob: c14e35201721201f9841bd38606e1ee193b6788e [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
Alexander Usyskin962ff7b2017-01-27 16:32:45 +020038 * @cmpl_list: list of completed cbs
Tomas Winkler4c6e22b2013-03-17 11:41:20 +020039 */
Alexander Usyskin962ff7b2017-01-27 16:32:45 +020040void mei_irq_compl_handler(struct mei_device *dev, struct list_head *cmpl_list)
Tomas Winkler4c6e22b2013-03-17 11:41:20 +020041{
42 struct mei_cl_cb *cb, *next;
43 struct mei_cl *cl;
44
Alexander Usyskin962ff7b2017-01-27 16:32:45 +020045 list_for_each_entry_safe(cb, next, cmpl_list, list) {
Tomas Winkler4c6e22b2013-03-17 11:41:20 +020046 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");
Alexander Usyskin394a77d2017-03-20 15:04:03 +020050 mei_cl_complete(cl, cb);
Tomas Winkler4c6e22b2013-03-17 11:41:20 +020051 }
52}
Tomas Winkler40e0b672013-03-27 16:58:30 +020053EXPORT_SYMBOL_GPL(mei_irq_compl_handler);
Tomas Winkler6e0f1802013-04-19 22:01:34 +030054
Tomas Winkler4c6e22b2013-03-17 11:41:20 +020055/**
Tomas Winkler6e0f1802013-04-19 22:01:34 +030056 * mei_cl_hbm_equal - check if hbm is addressed to the client
Oren Weilfb7d8792011-05-15 13:43:42 +030057 *
Tomas Winkler6e0f1802013-04-19 22:01:34 +030058 * @cl: host client
Oren Weilfb7d8792011-05-15 13:43:42 +030059 * @mei_hdr: header of mei client message
60 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +030061 * Return: true if matches, false otherwise
Oren Weilfb7d8792011-05-15 13:43:42 +030062 */
Tomas Winkler6e0f1802013-04-19 22:01:34 +030063static inline int mei_cl_hbm_equal(struct mei_cl *cl,
64 struct mei_msg_hdr *mei_hdr)
Oren Weilfb7d8792011-05-15 13:43:42 +030065{
Alexander Usyskin1df629e2015-05-04 09:43:56 +030066 return mei_cl_host_addr(cl) == mei_hdr->host_addr &&
Alexander Usyskind49ed642015-05-04 09:43:54 +030067 mei_cl_me_id(cl) == mei_hdr->me_addr;
Tomas Winkler6e0f1802013-04-19 22:01:34 +030068}
Oren Weilfb7d8792011-05-15 13:43:42 +030069
70/**
Tomas Winkler331e4182015-02-10 10:39:41 +020071 * mei_irq_discard_msg - discard received message
72 *
73 * @dev: mei device
74 * @hdr: message header
75 */
Alexander Usyskin394a77d2017-03-20 15:04:03 +020076static void mei_irq_discard_msg(struct mei_device *dev, struct mei_msg_hdr *hdr)
Tomas Winkler331e4182015-02-10 10:39:41 +020077{
78 /*
79 * no need to check for size as it is guarantied
80 * that length fits into rd_msg_buf
81 */
82 mei_read_slots(dev, dev->rd_msg_buf, hdr->length);
83 dev_dbg(dev->dev, "discarding message " MEI_HDR_FMT "\n",
84 MEI_HDR_PRM(hdr));
85}
86
87/**
Alexander Usyskince231392014-09-29 16:31:50 +030088 * mei_cl_irq_read_msg - process client message
Oren Weilfb7d8792011-05-15 13:43:42 +030089 *
Tomas Winkler3d33ff22015-02-10 10:39:36 +020090 * @cl: reading client
Oren Weilfb7d8792011-05-15 13:43:42 +030091 * @mei_hdr: header of mei client message
Alexander Usyskin962ff7b2017-01-27 16:32:45 +020092 * @cmpl_list: completion list
Oren Weilfb7d8792011-05-15 13:43:42 +030093 *
Tomas Winkler3d33ff22015-02-10 10:39:36 +020094 * Return: always 0
Oren Weilfb7d8792011-05-15 13:43:42 +030095 */
Alexander Usyskin394a77d2017-03-20 15:04:03 +020096static int mei_cl_irq_read_msg(struct mei_cl *cl,
97 struct mei_msg_hdr *mei_hdr,
98 struct list_head *cmpl_list)
Oren Weilfb7d8792011-05-15 13:43:42 +030099{
Tomas Winkler3d33ff22015-02-10 10:39:36 +0200100 struct mei_device *dev = cl->dev;
101 struct mei_cl_cb *cb;
Tomas Winklerf862b6b2016-02-07 23:35:19 +0200102 size_t buf_sz;
Oren Weilfb7d8792011-05-15 13:43:42 +0300103
Tomas Winklera9bed612015-02-10 10:39:46 +0200104 cb = list_first_entry_or_null(&cl->rd_pending, struct mei_cl_cb, list);
105 if (!cb) {
Alexander Usyskina808c802016-06-16 17:58:58 +0300106 if (!mei_cl_is_fixed_address(cl)) {
107 cl_err(dev, cl, "pending read cb not found\n");
Alexander Usyskindfe5f752016-07-26 01:06:01 +0300108 goto discard;
Alexander Usyskina808c802016-06-16 17:58:58 +0300109 }
110 cb = mei_cl_alloc_cb(cl, mei_cl_mtu(cl), MEI_FOP_READ, cl->fp);
111 if (!cb)
Alexander Usyskindfe5f752016-07-26 01:06:01 +0300112 goto discard;
Alexander Usyskina808c802016-06-16 17:58:58 +0300113 list_add_tail(&cb->list, &cl->rd_pending);
Tomas Winkler3d33ff22015-02-10 10:39:36 +0200114 }
115
Tomas Winklerf3de9b62015-03-27 00:27:58 +0200116 if (!mei_cl_is_connected(cl)) {
Tomas Winklera9bed612015-02-10 10:39:46 +0200117 cl_dbg(dev, cl, "not connected\n");
118 cb->status = -ENODEV;
Alexander Usyskindfe5f752016-07-26 01:06:01 +0300119 goto discard;
Tomas Winkler3d33ff22015-02-10 10:39:36 +0200120 }
121
Tomas Winklerf862b6b2016-02-07 23:35:19 +0200122 buf_sz = mei_hdr->length + cb->buf_idx;
123 /* catch for integer overflow */
124 if (buf_sz < cb->buf_idx) {
Alexander Usyskin35bf7692016-02-17 18:27:34 +0200125 cl_err(dev, cl, "message is too big len %d idx %zu\n",
Tomas Winklerf862b6b2016-02-07 23:35:19 +0200126 mei_hdr->length, cb->buf_idx);
Tomas Winklerf862b6b2016-02-07 23:35:19 +0200127 cb->status = -EMSGSIZE;
Alexander Usyskindfe5f752016-07-26 01:06:01 +0300128 goto discard;
Tomas Winklerf862b6b2016-02-07 23:35:19 +0200129 }
130
131 if (cb->buf.size < buf_sz) {
Alexander Usyskin35bf7692016-02-17 18:27:34 +0200132 cl_dbg(dev, cl, "message overflow. size %zu len %d idx %zu\n",
Tomas Winkler5db75142015-02-10 10:39:42 +0200133 cb->buf.size, mei_hdr->length, cb->buf_idx);
Alexander Usyskindfe5f752016-07-26 01:06:01 +0300134 cb->status = -EMSGSIZE;
135 goto discard;
Tomas Winkler3d33ff22015-02-10 10:39:36 +0200136 }
137
Alexander Usyskindfe5f752016-07-26 01:06:01 +0300138 mei_read_slots(dev, cb->buf.data + cb->buf_idx, mei_hdr->length);
Tomas Winkler3d33ff22015-02-10 10:39:36 +0200139
140 cb->buf_idx += mei_hdr->length;
Tomas Winkler331e4182015-02-10 10:39:41 +0200141
Tomas Winkler3d33ff22015-02-10 10:39:36 +0200142 if (mei_hdr->msg_complete) {
Alexander Usyskin35bf7692016-02-17 18:27:34 +0200143 cl_dbg(dev, cl, "completed read length = %zu\n", cb->buf_idx);
Alexander Usyskin962ff7b2017-01-27 16:32:45 +0200144 list_move_tail(&cb->list, cmpl_list);
Alexander Usyskin34af1912015-10-13 15:02:38 +0300145 } else {
146 pm_runtime_mark_last_busy(dev->dev);
147 pm_request_autosuspend(dev->dev);
Tomas Winkler3d33ff22015-02-10 10:39:36 +0200148 }
149
Alexander Usyskindfe5f752016-07-26 01:06:01 +0300150 return 0;
Oren Weilfb7d8792011-05-15 13:43:42 +0300151
Alexander Usyskindfe5f752016-07-26 01:06:01 +0300152discard:
Tomas Winkler17ba8a02016-11-11 03:00:07 +0200153 if (cb)
Alexander Usyskin962ff7b2017-01-27 16:32:45 +0200154 list_move_tail(&cb->list, cmpl_list);
Alexander Usyskindfe5f752016-07-26 01:06:01 +0300155 mei_irq_discard_msg(dev, mei_hdr);
Oren Weilfb7d8792011-05-15 13:43:42 +0300156 return 0;
157}
158
159/**
Tomas Winkler6bb948c2014-02-12 21:41:52 +0200160 * mei_cl_irq_disconnect_rsp - send disconnection response message
161 *
162 * @cl: client
163 * @cb: callback block.
Tomas Winkler6bb948c2014-02-12 21:41:52 +0200164 * @cmpl_list: complete list.
165 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300166 * Return: 0, OK; otherwise, error.
Tomas Winkler6bb948c2014-02-12 21:41:52 +0200167 */
168static int mei_cl_irq_disconnect_rsp(struct mei_cl *cl, struct mei_cl_cb *cb,
Alexander Usyskin962ff7b2017-01-27 16:32:45 +0200169 struct list_head *cmpl_list)
Tomas Winkler6bb948c2014-02-12 21:41:52 +0200170{
171 struct mei_device *dev = cl->dev;
Tomas Winkler9d098192014-02-19 17:35:48 +0200172 u32 msg_slots;
173 int slots;
Tomas Winkler6bb948c2014-02-12 21:41:52 +0200174 int ret;
175
Tomas Winkler9d098192014-02-19 17:35:48 +0200176 slots = mei_hbuf_empty_slots(dev);
177 msg_slots = mei_data2slots(sizeof(struct hbm_client_connect_response));
Tomas Winkler6bb948c2014-02-12 21:41:52 +0200178
Tomas Winkler9d098192014-02-19 17:35:48 +0200179 if (slots < msg_slots)
Tomas Winkler6bb948c2014-02-12 21:41:52 +0200180 return -EMSGSIZE;
181
Tomas Winkler6bb948c2014-02-12 21:41:52 +0200182 ret = mei_hbm_cl_disconnect_rsp(dev, cl);
Alexander Usyskin962ff7b2017-01-27 16:32:45 +0200183 list_move_tail(&cb->list, cmpl_list);
Tomas Winkler6bb948c2014-02-12 21:41:52 +0200184
185 return ret;
186}
187
Oren Weilfb7d8792011-05-15 13:43:42 +0300188/**
Alexander Usyskince231392014-09-29 16:31:50 +0300189 * mei_cl_irq_read - processes client read related operation from the
Tomas Winkler6220d6a2013-05-12 15:34:46 +0300190 * interrupt thread context - request for flow control credits
Oren Weilfb7d8792011-05-15 13:43:42 +0300191 *
Tomas Winkler6220d6a2013-05-12 15:34:46 +0300192 * @cl: client
193 * @cb: callback block.
Oren Weilfb7d8792011-05-15 13:43:42 +0300194 * @cmpl_list: complete list.
195 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300196 * Return: 0, OK; otherwise, error.
Oren Weilfb7d8792011-05-15 13:43:42 +0300197 */
Tomas Winkler6220d6a2013-05-12 15:34:46 +0300198static int mei_cl_irq_read(struct mei_cl *cl, struct mei_cl_cb *cb,
Alexander Usyskin962ff7b2017-01-27 16:32:45 +0200199 struct list_head *cmpl_list)
Oren Weilfb7d8792011-05-15 13:43:42 +0300200{
Tomas Winkler6220d6a2013-05-12 15:34:46 +0300201 struct mei_device *dev = cl->dev;
Tomas Winkler9d098192014-02-19 17:35:48 +0200202 u32 msg_slots;
203 int slots;
Tomas Winkler2ebf8c92013-09-16 23:44:43 +0300204 int ret;
205
Alexander Usyskinff1586a2016-07-26 01:06:06 +0300206 if (!list_empty(&cl->rd_pending))
207 return 0;
208
Tomas Winkler9d098192014-02-19 17:35:48 +0200209 msg_slots = mei_data2slots(sizeof(struct hbm_flow_control));
210 slots = mei_hbuf_empty_slots(dev);
Tomas Winkler2ebf8c92013-09-16 23:44:43 +0300211
Tomas Winkler9d098192014-02-19 17:35:48 +0200212 if (slots < msg_slots)
Tomas Winklerc8c8d082013-03-11 18:27:02 +0200213 return -EMSGSIZE;
Tomas Winkler7bdf72d2012-07-04 19:24:52 +0300214
Tomas Winkler2ebf8c92013-09-16 23:44:43 +0300215 ret = mei_hbm_cl_flow_control_req(dev, cl);
216 if (ret) {
217 cl->status = ret;
Tomas Winkler6220d6a2013-05-12 15:34:46 +0300218 cb->buf_idx = 0;
Alexander Usyskin962ff7b2017-01-27 16:32:45 +0200219 list_move_tail(&cb->list, cmpl_list);
Tomas Winkler2ebf8c92013-09-16 23:44:43 +0300220 return ret;
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200221 }
Tomas Winkler2ebf8c92013-09-16 23:44:43 +0300222
Tomas Winklera9bed612015-02-10 10:39:46 +0200223 list_move_tail(&cb->list, &cl->rd_pending);
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200224
Oren Weilfb7d8792011-05-15 13:43:42 +0300225 return 0;
226}
227
Alexander Usyskin603c53e2016-02-07 23:35:36 +0200228static inline bool hdr_is_hbm(struct mei_msg_hdr *mei_hdr)
229{
230 return mei_hdr->host_addr == 0 && mei_hdr->me_addr == 0;
231}
232
233static inline bool hdr_is_fixed(struct mei_msg_hdr *mei_hdr)
234{
235 return mei_hdr->host_addr == 0 && mei_hdr->me_addr != 0;
236}
237
Oren Weilfb7d8792011-05-15 13:43:42 +0300238/**
Masanari Iida393b1482013-04-05 01:05:05 +0900239 * mei_irq_read_handler - bottom half read routine after ISR to
Oren Weilfb7d8792011-05-15 13:43:42 +0300240 * handle the read processing.
241 *
Oren Weilfb7d8792011-05-15 13:43:42 +0300242 * @dev: the device structure
Tomas Winkler06ecd642013-02-06 14:06:42 +0200243 * @cmpl_list: An instance of our list structure
Oren Weilfb7d8792011-05-15 13:43:42 +0300244 * @slots: slots to read.
245 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300246 * Return: 0 on success, <0 on failure.
Oren Weilfb7d8792011-05-15 13:43:42 +0300247 */
Tomas Winkler06ecd642013-02-06 14:06:42 +0200248int mei_irq_read_handler(struct mei_device *dev,
Alexander Usyskin962ff7b2017-01-27 16:32:45 +0200249 struct list_head *cmpl_list, s32 *slots)
Oren Weilfb7d8792011-05-15 13:43:42 +0300250{
251 struct mei_msg_hdr *mei_hdr;
Tomas Winkler10ee9072013-11-11 13:26:08 +0200252 struct mei_cl *cl;
253 int ret;
Oren Weilfb7d8792011-05-15 13:43:42 +0300254
255 if (!dev->rd_msg_hdr) {
Tomas Winkler827eef52013-02-06 14:06:41 +0200256 dev->rd_msg_hdr = mei_read_hdr(dev);
Oren Weilfb7d8792011-05-15 13:43:42 +0300257 (*slots)--;
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300258 dev_dbg(dev->dev, "slots =%08x.\n", *slots);
Oren Weilfb7d8792011-05-15 13:43:42 +0300259 }
260 mei_hdr = (struct mei_msg_hdr *) &dev->rd_msg_hdr;
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300261 dev_dbg(dev->dev, MEI_HDR_FMT, MEI_HDR_PRM(mei_hdr));
Oren Weilfb7d8792011-05-15 13:43:42 +0300262
263 if (mei_hdr->reserved || !dev->rd_msg_hdr) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300264 dev_err(dev->dev, "corrupted message header 0x%08X\n",
Tomas Winkler10ee9072013-11-11 13:26:08 +0200265 dev->rd_msg_hdr);
Oren Weilfb7d8792011-05-15 13:43:42 +0300266 ret = -EBADMSG;
267 goto end;
268 }
269
Tomas Winkler10ee9072013-11-11 13:26:08 +0200270 if (mei_slots2data(*slots) < mei_hdr->length) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300271 dev_err(dev->dev, "less data available than length=%08x.\n",
Oren Weilfb7d8792011-05-15 13:43:42 +0300272 *slots);
273 /* we can't read the message */
Tomas Winklerb1b94b52014-03-03 00:21:28 +0200274 ret = -ENODATA;
Oren Weilfb7d8792011-05-15 13:43:42 +0300275 goto end;
276 }
277
Tomas Winkler10ee9072013-11-11 13:26:08 +0200278 /* HBM message */
Alexander Usyskin603c53e2016-02-07 23:35:36 +0200279 if (hdr_is_hbm(mei_hdr)) {
Tomas Winkler544f9462014-01-08 20:19:21 +0200280 ret = mei_hbm_dispatch(dev, mei_hdr);
281 if (ret) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300282 dev_dbg(dev->dev, "mei_hbm_dispatch failed ret = %d\n",
Tomas Winkler544f9462014-01-08 20:19:21 +0200283 ret);
284 goto end;
285 }
Tomas Winkler10ee9072013-11-11 13:26:08 +0200286 goto reset_slots;
Oren Weilfb7d8792011-05-15 13:43:42 +0300287 }
288
Alexander Usyskin83ce0742014-01-08 22:31:46 +0200289 /* find recipient cl */
Tomas Winkler10ee9072013-11-11 13:26:08 +0200290 list_for_each_entry(cl, &dev->file_list, link) {
291 if (mei_cl_hbm_equal(cl, mei_hdr)) {
292 cl_dbg(dev, cl, "got a message\n");
293 break;
294 }
295 }
296
Alexander Usyskin83ce0742014-01-08 22:31:46 +0200297 /* if no recipient cl was found we assume corrupted header */
Tomas Winkler10ee9072013-11-11 13:26:08 +0200298 if (&cl->link == &dev->file_list) {
Alexander Usyskin603c53e2016-02-07 23:35:36 +0200299 /* A message for not connected fixed address clients
300 * should be silently discarded
301 */
302 if (hdr_is_fixed(mei_hdr)) {
303 mei_irq_discard_msg(dev, mei_hdr);
304 ret = 0;
305 goto reset_slots;
306 }
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300307 dev_err(dev->dev, "no destination client found 0x%08X\n",
Tomas Winkler10ee9072013-11-11 13:26:08 +0200308 dev->rd_msg_hdr);
309 ret = -EBADMSG;
310 goto end;
311 }
312
Alexander Usyskin394a77d2017-03-20 15:04:03 +0200313 ret = mei_cl_irq_read_msg(cl, mei_hdr, cmpl_list);
Tomas Winkler10ee9072013-11-11 13:26:08 +0200314
Tomas Winkler3d33ff22015-02-10 10:39:36 +0200315
Tomas Winkler10ee9072013-11-11 13:26:08 +0200316reset_slots:
Oren Weilfb7d8792011-05-15 13:43:42 +0300317 /* reset the number of slots and header */
318 *slots = mei_count_full_read_slots(dev);
319 dev->rd_msg_hdr = 0;
320
321 if (*slots == -EOVERFLOW) {
322 /* overflow - reset */
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300323 dev_err(dev->dev, "resetting due to slots overflow.\n");
Oren Weilfb7d8792011-05-15 13:43:42 +0300324 /* set the event since message has been read */
325 ret = -ERANGE;
326 goto end;
327 }
328end:
329 return ret;
330}
Tomas Winkler40e0b672013-03-27 16:58:30 +0200331EXPORT_SYMBOL_GPL(mei_irq_read_handler);
Oren Weilfb7d8792011-05-15 13:43:42 +0300332
333
334/**
Tomas Winkler06ecd642013-02-06 14:06:42 +0200335 * mei_irq_write_handler - dispatch write requests
336 * after irq received
Oren Weilfb7d8792011-05-15 13:43:42 +0300337 *
Oren Weilfb7d8792011-05-15 13:43:42 +0300338 * @dev: the device structure
Tomas Winkler9a84d612012-11-18 15:13:18 +0200339 * @cmpl_list: An instance of our list structure
Oren Weilfb7d8792011-05-15 13:43:42 +0300340 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300341 * Return: 0 on success, <0 on failure.
Oren Weilfb7d8792011-05-15 13:43:42 +0300342 */
Alexander Usyskin962ff7b2017-01-27 16:32:45 +0200343int mei_irq_write_handler(struct mei_device *dev, struct list_head *cmpl_list)
Oren Weilfb7d8792011-05-15 13:43:42 +0300344{
345
346 struct mei_cl *cl;
Tomas Winkler6220d6a2013-05-12 15:34:46 +0300347 struct mei_cl_cb *cb, *next;
Tomas Winkler9a84d612012-11-18 15:13:18 +0200348 s32 slots;
Oren Weilfb7d8792011-05-15 13:43:42 +0300349 int ret;
350
Tomas Winkler6aae48f2014-02-19 17:35:47 +0200351
352 if (!mei_hbuf_acquire(dev))
Oren Weilfb7d8792011-05-15 13:43:42 +0300353 return 0;
Tomas Winkler6aae48f2014-02-19 17:35:47 +0200354
Tomas Winkler9a84d612012-11-18 15:13:18 +0200355 slots = mei_hbuf_empty_slots(dev);
356 if (slots <= 0)
Tomas Winkler7d5e0e52012-06-19 09:13:36 +0300357 return -EMSGSIZE;
358
Oren Weilfb7d8792011-05-15 13:43:42 +0300359 /* complete all waiting for write CB */
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300360 dev_dbg(dev->dev, "complete all waiting for write cb.\n");
Oren Weilfb7d8792011-05-15 13:43:42 +0300361
Alexander Usyskin962ff7b2017-01-27 16:32:45 +0200362 list_for_each_entry_safe(cb, next, &dev->write_waiting_list, list) {
Tomas Winkler6220d6a2013-05-12 15:34:46 +0300363 cl = cb->cl;
Oren Weilfb7d8792011-05-15 13:43:42 +0300364
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200365 cl->status = 0;
Tomas Winklerc54bf3a2015-02-10 10:39:39 +0200366 cl_dbg(dev, cl, "MEI WRITE COMPLETE\n");
367 cl->writing_state = MEI_WRITE_COMPLETE;
Alexander Usyskin962ff7b2017-01-27 16:32:45 +0200368 list_move_tail(&cb->list, cmpl_list);
Oren Weilfb7d8792011-05-15 13:43:42 +0300369 }
370
Oren Weilfb7d8792011-05-15 13:43:42 +0300371 /* complete control write list CB */
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300372 dev_dbg(dev->dev, "complete control write list cb.\n");
Alexander Usyskin962ff7b2017-01-27 16:32:45 +0200373 list_for_each_entry_safe(cb, next, &dev->ctrl_wr_list, list) {
Tomas Winkler6220d6a2013-05-12 15:34:46 +0300374 cl = cb->cl;
Tomas Winkler6220d6a2013-05-12 15:34:46 +0300375 switch (cb->fop_type) {
Tomas Winkler5a8373f2014-08-21 14:29:17 +0300376 case MEI_FOP_DISCONNECT:
Tomas Winklerc8372092011-11-27 21:43:33 +0200377 /* send disconnect message */
Tomas Winkler5a8373f2014-08-21 14:29:17 +0300378 ret = mei_cl_irq_disconnect(cl, cb, cmpl_list);
Tomas Winklerc8372092011-11-27 21:43:33 +0200379 if (ret)
380 return ret;
381
382 break;
Tomas Winkler4b8960b2012-11-11 17:38:00 +0200383 case MEI_FOP_READ:
Tomas Winklerc8372092011-11-27 21:43:33 +0200384 /* send flow control message */
Tomas Winkler9d098192014-02-19 17:35:48 +0200385 ret = mei_cl_irq_read(cl, cb, cmpl_list);
Tomas Winklerc8372092011-11-27 21:43:33 +0200386 if (ret)
387 return ret;
388
389 break;
Tomas Winkler02a7eec2014-02-12 21:41:51 +0200390 case MEI_FOP_CONNECT:
Tomas Winklerc8372092011-11-27 21:43:33 +0200391 /* connect message */
Tomas Winkler9d098192014-02-19 17:35:48 +0200392 ret = mei_cl_irq_connect(cl, cb, cmpl_list);
Tomas Winklerc8372092011-11-27 21:43:33 +0200393 if (ret)
394 return ret;
395
396 break;
Tomas Winkler6bb948c2014-02-12 21:41:52 +0200397 case MEI_FOP_DISCONNECT_RSP:
398 /* send disconnect resp */
Tomas Winkler9d098192014-02-19 17:35:48 +0200399 ret = mei_cl_irq_disconnect_rsp(cl, cb, cmpl_list);
Tomas Winkler6bb948c2014-02-12 21:41:52 +0200400 if (ret)
401 return ret;
Alexander Usyskin31a5ef22014-03-06 23:53:53 +0200402 break;
Tomas Winkler51678cc2015-07-26 09:54:18 +0300403
404 case MEI_FOP_NOTIFY_START:
405 case MEI_FOP_NOTIFY_STOP:
406 ret = mei_cl_irq_notify(cl, cb, cmpl_list);
407 if (ret)
408 return ret;
409 break;
Tomas Winklerc8372092011-11-27 21:43:33 +0200410 default:
411 BUG();
412 }
413
Oren Weilfb7d8792011-05-15 13:43:42 +0300414 }
415 /* complete write list CB */
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300416 dev_dbg(dev->dev, "complete write list cb.\n");
Alexander Usyskin962ff7b2017-01-27 16:32:45 +0200417 list_for_each_entry_safe(cb, next, &dev->write_list, list) {
Tomas Winkler6220d6a2013-05-12 15:34:46 +0300418 cl = cb->cl;
Alexander Usyskin394a77d2017-03-20 15:04:03 +0200419 ret = mei_cl_irq_write(cl, cb, cmpl_list);
Tomas Winklerbe9d87a2012-11-18 15:13:19 +0200420 if (ret)
421 return ret;
Oren Weilfb7d8792011-05-15 13:43:42 +0300422 }
423 return 0;
424}
Tomas Winkler40e0b672013-03-27 16:58:30 +0200425EXPORT_SYMBOL_GPL(mei_irq_write_handler);
Oren Weilfb7d8792011-05-15 13:43:42 +0300426
427
Alexander Usyskin18901352015-07-23 21:37:13 +0300428/**
429 * mei_connect_timeout - connect/disconnect timeouts
430 *
431 * @cl: host client
432 */
433static void mei_connect_timeout(struct mei_cl *cl)
434{
435 struct mei_device *dev = cl->dev;
436
437 if (cl->state == MEI_FILE_CONNECTING) {
438 if (dev->hbm_f_dot_supported) {
439 cl->state = MEI_FILE_DISCONNECT_REQUIRED;
440 wake_up(&cl->wait);
441 return;
442 }
443 }
444 mei_reset(dev);
445}
Oren Weilfb7d8792011-05-15 13:43:42 +0300446
Alexander Usyskin1892fc22016-09-25 13:25:31 +0300447#define MEI_STALL_TIMER_FREQ (2 * HZ)
448/**
449 * mei_schedule_stall_timer - re-arm stall_timer work
450 *
451 * Schedule stall timer
452 *
453 * @dev: the device structure
454 */
455void mei_schedule_stall_timer(struct mei_device *dev)
456{
457 schedule_delayed_work(&dev->timer_work, MEI_STALL_TIMER_FREQ);
458}
459
Oren Weilfb7d8792011-05-15 13:43:42 +0300460/**
461 * mei_timer - timer function.
462 *
463 * @work: pointer to the work_struct structure
464 *
Oren Weilfb7d8792011-05-15 13:43:42 +0300465 */
Oren Weila61c6532011-09-07 09:03:13 +0300466void mei_timer(struct work_struct *work)
Oren Weilfb7d8792011-05-15 13:43:42 +0300467{
Tomas Winkler31f88f52014-02-17 15:13:25 +0200468 struct mei_cl *cl;
Oren Weilfb7d8792011-05-15 13:43:42 +0300469 struct mei_device *dev = container_of(work,
Oren Weila61c6532011-09-07 09:03:13 +0300470 struct mei_device, timer_work.work);
Alexander Usyskin1892fc22016-09-25 13:25:31 +0300471 bool reschedule_timer = false;
Oren Weilfb7d8792011-05-15 13:43:42 +0300472
473 mutex_lock(&dev->device_lock);
Tomas Winkler66ae4602014-01-08 20:19:22 +0200474
475 /* Catch interrupt stalls during HBM init handshake */
476 if (dev->dev_state == MEI_DEV_INIT_CLIENTS &&
477 dev->hbm_state != MEI_HBM_IDLE) {
478
479 if (dev->init_clients_timer) {
480 if (--dev->init_clients_timer == 0) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300481 dev_err(dev->dev, "timer: init clients timeout hbm_state = %d.\n",
Tomas Winkler66ae4602014-01-08 20:19:22 +0200482 dev->hbm_state);
Tomas Winkler33ec0822014-01-12 00:36:09 +0200483 mei_reset(dev);
Tomas Winkler66ae4602014-01-08 20:19:22 +0200484 goto out;
Oren Weilfb7d8792011-05-15 13:43:42 +0300485 }
Alexander Usyskin1892fc22016-09-25 13:25:31 +0300486 reschedule_timer = true;
Oren Weilfb7d8792011-05-15 13:43:42 +0300487 }
Oren Weilfb7d8792011-05-15 13:43:42 +0300488 }
Tomas Winkler66ae4602014-01-08 20:19:22 +0200489
490 if (dev->dev_state != MEI_DEV_ENABLED)
491 goto out;
492
Oren Weilfb7d8792011-05-15 13:43:42 +0300493 /*** connect/disconnect timeouts ***/
Tomas Winkler31f88f52014-02-17 15:13:25 +0200494 list_for_each_entry(cl, &dev->file_list, link) {
495 if (cl->timer_count) {
496 if (--cl->timer_count == 0) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300497 dev_err(dev->dev, "timer: connect/disconnect timeout.\n");
Alexander Usyskin18901352015-07-23 21:37:13 +0300498 mei_connect_timeout(cl);
Oren Weilfb7d8792011-05-15 13:43:42 +0300499 goto out;
500 }
Alexander Usyskin1892fc22016-09-25 13:25:31 +0300501 reschedule_timer = true;
Oren Weilfb7d8792011-05-15 13:43:42 +0300502 }
503 }
504
Oren Weilfb7d8792011-05-15 13:43:42 +0300505out:
Alexander Usyskin1892fc22016-09-25 13:25:31 +0300506 if (dev->dev_state != MEI_DEV_DISABLED && reschedule_timer)
507 mei_schedule_stall_timer(dev);
508
Tomas Winkler441ab502011-12-13 23:39:34 +0200509 mutex_unlock(&dev->device_lock);
Oren Weilfb7d8792011-05-15 13:43:42 +0300510}