blob: b09b79fedaba06677281446cba18dc4b333e8a81 [file] [log] [blame]
Tomas Winkler9fff0422019-03-12 00:10:41 +02001// SPDX-License-Identifier: GPL-2.0
Oren Weilfb7d8792011-05-15 13:43:42 +03002/*
Tomas Winkler1e55b602019-03-12 00:10:44 +02003 * Copyright (c) 2003-2018, Intel Corporation. All rights reserved.
Oren Weilfb7d8792011-05-15 13:43:42 +03004 * Intel Management Engine Interface (Intel MEI) Linux driver
Oren Weilfb7d8792011-05-15 13:43:42 +03005 */
6
Tomas Winkler40e0b672013-03-27 16:58:30 +02007#include <linux/export.h>
Oren Weilfb7d8792011-05-15 13:43:42 +03008#include <linux/kthread.h>
9#include <linux/interrupt.h>
10#include <linux/fs.h>
11#include <linux/jiffies.h>
Tomas Winkler1f180352014-09-29 16:31:46 +030012#include <linux/slab.h>
Alexander Usyskin34af1912015-10-13 15:02:38 +030013#include <linux/pm_runtime.h>
Oren Weilfb7d8792011-05-15 13:43:42 +030014
Tomas Winkler4f3afe12012-05-09 16:38:59 +030015#include <linux/mei.h>
Tomas Winkler47a73802012-12-25 19:06:03 +020016
17#include "mei_dev.h"
Tomas Winkler0edb23f2013-01-08 23:07:12 +020018#include "hbm.h"
Tomas Winkler90e0b5f2013-01-08 23:07:14 +020019#include "client.h"
Oren Weilfb7d8792011-05-15 13:43:42 +030020
21
22/**
Alexander Usyskin83ce0742014-01-08 22:31:46 +020023 * mei_irq_compl_handler - dispatch complete handlers
Tomas Winkler4c6e22b2013-03-17 11:41:20 +020024 * for the completed callbacks
25 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +030026 * @dev: mei device
Alexander Usyskin962ff7b2017-01-27 16:32:45 +020027 * @cmpl_list: list of completed cbs
Tomas Winkler4c6e22b2013-03-17 11:41:20 +020028 */
Alexander Usyskin962ff7b2017-01-27 16:32:45 +020029void mei_irq_compl_handler(struct mei_device *dev, struct list_head *cmpl_list)
Tomas Winkler4c6e22b2013-03-17 11:41:20 +020030{
31 struct mei_cl_cb *cb, *next;
32 struct mei_cl *cl;
33
Alexander Usyskin962ff7b2017-01-27 16:32:45 +020034 list_for_each_entry_safe(cb, next, cmpl_list, list) {
Tomas Winkler4c6e22b2013-03-17 11:41:20 +020035 cl = cb->cl;
Tomas Winklerc54bf3a2015-02-10 10:39:39 +020036 list_del_init(&cb->list);
Tomas Winkler4c6e22b2013-03-17 11:41:20 +020037
Tomas Winkler2bf94cab2014-09-29 16:31:42 +030038 dev_dbg(dev->dev, "completing call back.\n");
Alexander Usyskin394a77d2017-03-20 15:04:03 +020039 mei_cl_complete(cl, cb);
Tomas Winkler4c6e22b2013-03-17 11:41:20 +020040 }
41}
Tomas Winkler40e0b672013-03-27 16:58:30 +020042EXPORT_SYMBOL_GPL(mei_irq_compl_handler);
Tomas Winkler6e0f1802013-04-19 22:01:34 +030043
Tomas Winkler4c6e22b2013-03-17 11:41:20 +020044/**
Tomas Winkler6e0f1802013-04-19 22:01:34 +030045 * mei_cl_hbm_equal - check if hbm is addressed to the client
Oren Weilfb7d8792011-05-15 13:43:42 +030046 *
Tomas Winkler6e0f1802013-04-19 22:01:34 +030047 * @cl: host client
Oren Weilfb7d8792011-05-15 13:43:42 +030048 * @mei_hdr: header of mei client message
49 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +030050 * Return: true if matches, false otherwise
Oren Weilfb7d8792011-05-15 13:43:42 +030051 */
Tomas Winkler6e0f1802013-04-19 22:01:34 +030052static inline int mei_cl_hbm_equal(struct mei_cl *cl,
53 struct mei_msg_hdr *mei_hdr)
Oren Weilfb7d8792011-05-15 13:43:42 +030054{
Alexander Usyskin1df629e2015-05-04 09:43:56 +030055 return mei_cl_host_addr(cl) == mei_hdr->host_addr &&
Alexander Usyskind49ed642015-05-04 09:43:54 +030056 mei_cl_me_id(cl) == mei_hdr->me_addr;
Tomas Winkler6e0f1802013-04-19 22:01:34 +030057}
Oren Weilfb7d8792011-05-15 13:43:42 +030058
59/**
Tomas Winkler331e4182015-02-10 10:39:41 +020060 * mei_irq_discard_msg - discard received message
61 *
62 * @dev: mei device
63 * @hdr: message header
Tomas Winkler0cd7c012020-08-18 14:51:38 +030064 * @discard_len: the length of the message to discard (excluding header)
Tomas Winkler331e4182015-02-10 10:39:41 +020065 */
Tomas Winkler0cd7c012020-08-18 14:51:38 +030066static void mei_irq_discard_msg(struct mei_device *dev, struct mei_msg_hdr *hdr,
67 size_t discard_len)
Tomas Winkler331e4182015-02-10 10:39:41 +020068{
Tomas Winkler0cd7c012020-08-18 14:51:38 +030069 if (hdr->dma_ring) {
70 mei_dma_ring_read(dev, NULL,
71 hdr->extension[dev->rd_msg_hdr_count - 2]);
72 discard_len = 0;
73 }
Tomas Winkler331e4182015-02-10 10:39:41 +020074 /*
Tomas Winklerae4cb6b2023-10-11 10:43:01 +030075 * no need to check for size as it is guaranteed
Tomas Winkler331e4182015-02-10 10:39:41 +020076 * that length fits into rd_msg_buf
77 */
Tomas Winkler0cd7c012020-08-18 14:51:38 +030078 mei_read_slots(dev, dev->rd_msg_buf, discard_len);
Tomas Winkler331e4182015-02-10 10:39:41 +020079 dev_dbg(dev->dev, "discarding message " MEI_HDR_FMT "\n",
80 MEI_HDR_PRM(hdr));
81}
82
83/**
Alexander Usyskince231392014-09-29 16:31:50 +030084 * mei_cl_irq_read_msg - process client message
Oren Weilfb7d8792011-05-15 13:43:42 +030085 *
Tomas Winkler3d33ff22015-02-10 10:39:36 +020086 * @cl: reading client
Oren Weilfb7d8792011-05-15 13:43:42 +030087 * @mei_hdr: header of mei client message
Tomas Winkler0cd7c012020-08-18 14:51:38 +030088 * @meta: extend meta header
Alexander Usyskin962ff7b2017-01-27 16:32:45 +020089 * @cmpl_list: completion list
Oren Weilfb7d8792011-05-15 13:43:42 +030090 *
Tomas Winkler3d33ff22015-02-10 10:39:36 +020091 * Return: always 0
Oren Weilfb7d8792011-05-15 13:43:42 +030092 */
Alexander Usyskin394a77d2017-03-20 15:04:03 +020093static int mei_cl_irq_read_msg(struct mei_cl *cl,
94 struct mei_msg_hdr *mei_hdr,
Tomas Winkler0cd7c012020-08-18 14:51:38 +030095 struct mei_ext_meta_hdr *meta,
Alexander Usyskin394a77d2017-03-20 15:04:03 +020096 struct list_head *cmpl_list)
Oren Weilfb7d8792011-05-15 13:43:42 +030097{
Tomas Winkler3d33ff22015-02-10 10:39:36 +020098 struct mei_device *dev = cl->dev;
99 struct mei_cl_cb *cb;
Tomas Winkler0cd7c012020-08-18 14:51:38 +0300100
Tomas Winkler4ed1cc92022-09-27 17:41:31 -0700101 struct mei_ext_hdr_vtag *vtag_hdr = NULL;
102 struct mei_ext_hdr_gsc_f2h *gsc_f2h = NULL;
103
Tomas Winklerf862b6b2016-02-07 23:35:19 +0200104 size_t buf_sz;
Tomas Winkler63163212018-11-22 13:11:39 +0200105 u32 length;
Tomas Winkler4ed1cc92022-09-27 17:41:31 -0700106 u32 ext_len;
Tomas Winkler0cd7c012020-08-18 14:51:38 +0300107
108 length = mei_hdr->length;
109 ext_len = 0;
110 if (mei_hdr->extended) {
111 ext_len = sizeof(*meta) + mei_slots2data(meta->size);
112 length -= ext_len;
113 }
Oren Weilfb7d8792011-05-15 13:43:42 +0300114
Tomas Winklera9bed612015-02-10 10:39:46 +0200115 cb = list_first_entry_or_null(&cl->rd_pending, struct mei_cl_cb, list);
116 if (!cb) {
Alexander Usyskina808c802016-06-16 17:58:58 +0300117 if (!mei_cl_is_fixed_address(cl)) {
118 cl_err(dev, cl, "pending read cb not found\n");
Alexander Usyskindfe5f752016-07-26 01:06:01 +0300119 goto discard;
Alexander Usyskina808c802016-06-16 17:58:58 +0300120 }
121 cb = mei_cl_alloc_cb(cl, mei_cl_mtu(cl), MEI_FOP_READ, cl->fp);
122 if (!cb)
Alexander Usyskindfe5f752016-07-26 01:06:01 +0300123 goto discard;
Alexander Usyskina808c802016-06-16 17:58:58 +0300124 list_add_tail(&cb->list, &cl->rd_pending);
Tomas Winkler3d33ff22015-02-10 10:39:36 +0200125 }
126
Tomas Winkler0cd7c012020-08-18 14:51:38 +0300127 if (mei_hdr->extended) {
Tomas Winkler4ed1cc92022-09-27 17:41:31 -0700128 struct mei_ext_hdr *ext = mei_ext_begin(meta);
Tomas Winkler0cd7c012020-08-18 14:51:38 +0300129 do {
130 switch (ext->type) {
131 case MEI_EXT_HDR_VTAG:
Tomas Winkler40292382021-06-21 22:37:56 +0300132 vtag_hdr = (struct mei_ext_hdr_vtag *)ext;
Tomas Winkler0cd7c012020-08-18 14:51:38 +0300133 break;
Tomas Winkler4ed1cc92022-09-27 17:41:31 -0700134 case MEI_EXT_HDR_GSC:
135 gsc_f2h = (struct mei_ext_hdr_gsc_f2h *)ext;
136 cb->ext_hdr = kzalloc(sizeof(*gsc_f2h), GFP_KERNEL);
137 if (!cb->ext_hdr) {
138 cb->status = -ENOMEM;
139 goto discard;
140 }
141 break;
Tomas Winkler0cd7c012020-08-18 14:51:38 +0300142 case MEI_EXT_HDR_NONE:
143 fallthrough;
144 default:
Tomas Winkler4ed1cc92022-09-27 17:41:31 -0700145 cl_err(dev, cl, "unknown extended header\n");
Tomas Winkler0cd7c012020-08-18 14:51:38 +0300146 cb->status = -EPROTO;
147 break;
148 }
149
150 ext = mei_ext_next(ext);
151 } while (!mei_ext_last(meta, ext));
152
Tomas Winkler4ed1cc92022-09-27 17:41:31 -0700153 if (!vtag_hdr && !gsc_f2h) {
154 cl_dbg(dev, cl, "no vtag or gsc found in extended header.\n");
Tomas Winkler0cd7c012020-08-18 14:51:38 +0300155 cb->status = -EPROTO;
156 goto discard;
157 }
Tomas Winkler4ed1cc92022-09-27 17:41:31 -0700158 }
Tomas Winkler0cd7c012020-08-18 14:51:38 +0300159
Tomas Winkler4ed1cc92022-09-27 17:41:31 -0700160 if (vtag_hdr) {
Tomas Winkler40292382021-06-21 22:37:56 +0300161 cl_dbg(dev, cl, "vtag: %d\n", vtag_hdr->vtag);
162 if (cb->vtag && cb->vtag != vtag_hdr->vtag) {
Tomas Winkler0cd7c012020-08-18 14:51:38 +0300163 cl_err(dev, cl, "mismatched tag: %d != %d\n",
Tomas Winkler40292382021-06-21 22:37:56 +0300164 cb->vtag, vtag_hdr->vtag);
Tomas Winkler0cd7c012020-08-18 14:51:38 +0300165 cb->status = -EPROTO;
166 goto discard;
167 }
Tomas Winkler40292382021-06-21 22:37:56 +0300168 cb->vtag = vtag_hdr->vtag;
Tomas Winkler0cd7c012020-08-18 14:51:38 +0300169 }
170
Tomas Winkler4ed1cc92022-09-27 17:41:31 -0700171 if (gsc_f2h) {
172 u32 ext_hdr_len = mei_ext_hdr_len(&gsc_f2h->hdr);
173
174 if (!dev->hbm_f_gsc_supported) {
175 cl_err(dev, cl, "gsc extended header is not supported\n");
176 cb->status = -EPROTO;
177 goto discard;
178 }
179
180 if (length) {
181 cl_err(dev, cl, "no data allowed in cb with gsc\n");
182 cb->status = -EPROTO;
183 goto discard;
184 }
185 if (ext_hdr_len > sizeof(*gsc_f2h)) {
186 cl_err(dev, cl, "gsc extended header is too big %u\n", ext_hdr_len);
187 cb->status = -EPROTO;
188 goto discard;
189 }
190 memcpy(cb->ext_hdr, gsc_f2h, ext_hdr_len);
191 }
192
Tomas Winklerf3de9b62015-03-27 00:27:58 +0200193 if (!mei_cl_is_connected(cl)) {
Tomas Winklera9bed612015-02-10 10:39:46 +0200194 cl_dbg(dev, cl, "not connected\n");
195 cb->status = -ENODEV;
Alexander Usyskindfe5f752016-07-26 01:06:01 +0300196 goto discard;
Tomas Winkler3d33ff22015-02-10 10:39:36 +0200197 }
198
Tomas Winkler0cd7c012020-08-18 14:51:38 +0300199 if (mei_hdr->dma_ring)
200 length = mei_hdr->extension[mei_data2slots(ext_len)];
Tomas Winkler63163212018-11-22 13:11:39 +0200201
202 buf_sz = length + cb->buf_idx;
Tomas Winklerf862b6b2016-02-07 23:35:19 +0200203 /* catch for integer overflow */
204 if (buf_sz < cb->buf_idx) {
Alexander Usyskin35bf7692016-02-17 18:27:34 +0200205 cl_err(dev, cl, "message is too big len %d idx %zu\n",
Tomas Winkler63163212018-11-22 13:11:39 +0200206 length, cb->buf_idx);
Tomas Winklerf862b6b2016-02-07 23:35:19 +0200207 cb->status = -EMSGSIZE;
Alexander Usyskindfe5f752016-07-26 01:06:01 +0300208 goto discard;
Tomas Winklerf862b6b2016-02-07 23:35:19 +0200209 }
210
211 if (cb->buf.size < buf_sz) {
Alexander Usyskin35bf7692016-02-17 18:27:34 +0200212 cl_dbg(dev, cl, "message overflow. size %zu len %d idx %zu\n",
Tomas Winkler63163212018-11-22 13:11:39 +0200213 cb->buf.size, length, cb->buf_idx);
Alexander Usyskindfe5f752016-07-26 01:06:01 +0300214 cb->status = -EMSGSIZE;
215 goto discard;
Tomas Winkler3d33ff22015-02-10 10:39:36 +0200216 }
217
Tomas Winkler0cd7c012020-08-18 14:51:38 +0300218 if (mei_hdr->dma_ring) {
Tomas Winkler63163212018-11-22 13:11:39 +0200219 mei_dma_ring_read(dev, cb->buf.data + cb->buf_idx, length);
Tomas Winkler0cd7c012020-08-18 14:51:38 +0300220 /* for DMA read 0 length to generate interrupt to the device */
221 mei_read_slots(dev, cb->buf.data + cb->buf_idx, 0);
222 } else {
223 mei_read_slots(dev, cb->buf.data + cb->buf_idx, length);
224 }
Tomas Winkler3d33ff22015-02-10 10:39:36 +0200225
Tomas Winkler63163212018-11-22 13:11:39 +0200226 cb->buf_idx += length;
Tomas Winkler331e4182015-02-10 10:39:41 +0200227
Tomas Winkler3d33ff22015-02-10 10:39:36 +0200228 if (mei_hdr->msg_complete) {
Alexander Usyskin35bf7692016-02-17 18:27:34 +0200229 cl_dbg(dev, cl, "completed read length = %zu\n", cb->buf_idx);
Alexander Usyskin962ff7b2017-01-27 16:32:45 +0200230 list_move_tail(&cb->list, cmpl_list);
Alexander Usyskin34af1912015-10-13 15:02:38 +0300231 } else {
232 pm_runtime_mark_last_busy(dev->dev);
233 pm_request_autosuspend(dev->dev);
Tomas Winkler3d33ff22015-02-10 10:39:36 +0200234 }
235
Alexander Usyskindfe5f752016-07-26 01:06:01 +0300236 return 0;
Oren Weilfb7d8792011-05-15 13:43:42 +0300237
Alexander Usyskindfe5f752016-07-26 01:06:01 +0300238discard:
Tomas Winkler17ba8a02016-11-11 03:00:07 +0200239 if (cb)
Alexander Usyskin962ff7b2017-01-27 16:32:45 +0200240 list_move_tail(&cb->list, cmpl_list);
Tomas Winkler0cd7c012020-08-18 14:51:38 +0300241 mei_irq_discard_msg(dev, mei_hdr, length);
Oren Weilfb7d8792011-05-15 13:43:42 +0300242 return 0;
243}
244
245/**
Tomas Winkler6bb948c2014-02-12 21:41:52 +0200246 * mei_cl_irq_disconnect_rsp - send disconnection response message
247 *
248 * @cl: client
249 * @cb: callback block.
Tomas Winkler6bb948c2014-02-12 21:41:52 +0200250 * @cmpl_list: complete list.
251 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300252 * Return: 0, OK; otherwise, error.
Tomas Winkler6bb948c2014-02-12 21:41:52 +0200253 */
254static int mei_cl_irq_disconnect_rsp(struct mei_cl *cl, struct mei_cl_cb *cb,
Alexander Usyskin962ff7b2017-01-27 16:32:45 +0200255 struct list_head *cmpl_list)
Tomas Winkler6bb948c2014-02-12 21:41:52 +0200256{
257 struct mei_device *dev = cl->dev;
Tomas Winkler9d098192014-02-19 17:35:48 +0200258 u32 msg_slots;
259 int slots;
Tomas Winkler6bb948c2014-02-12 21:41:52 +0200260 int ret;
261
Tomas Winkler98e70862018-07-31 09:35:33 +0300262 msg_slots = mei_hbm2slots(sizeof(struct hbm_client_connect_response));
Tomas Winklerde877432018-07-12 17:10:08 +0300263 slots = mei_hbuf_empty_slots(dev);
264 if (slots < 0)
265 return -EOVERFLOW;
Tomas Winkler6bb948c2014-02-12 21:41:52 +0200266
Tomas Winklerde877432018-07-12 17:10:08 +0300267 if ((u32)slots < msg_slots)
Tomas Winkler6bb948c2014-02-12 21:41:52 +0200268 return -EMSGSIZE;
269
Tomas Winkler6bb948c2014-02-12 21:41:52 +0200270 ret = mei_hbm_cl_disconnect_rsp(dev, cl);
Alexander Usyskin962ff7b2017-01-27 16:32:45 +0200271 list_move_tail(&cb->list, cmpl_list);
Tomas Winkler6bb948c2014-02-12 21:41:52 +0200272
273 return ret;
274}
275
Oren Weilfb7d8792011-05-15 13:43:42 +0300276/**
Alexander Usyskince231392014-09-29 16:31:50 +0300277 * mei_cl_irq_read - processes client read related operation from the
Tomas Winkler6220d6a2013-05-12 15:34:46 +0300278 * interrupt thread context - request for flow control credits
Oren Weilfb7d8792011-05-15 13:43:42 +0300279 *
Tomas Winkler6220d6a2013-05-12 15:34:46 +0300280 * @cl: client
281 * @cb: callback block.
Oren Weilfb7d8792011-05-15 13:43:42 +0300282 * @cmpl_list: complete list.
283 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300284 * Return: 0, OK; otherwise, error.
Oren Weilfb7d8792011-05-15 13:43:42 +0300285 */
Tomas Winkler6220d6a2013-05-12 15:34:46 +0300286static int mei_cl_irq_read(struct mei_cl *cl, struct mei_cl_cb *cb,
Alexander Usyskin962ff7b2017-01-27 16:32:45 +0200287 struct list_head *cmpl_list)
Oren Weilfb7d8792011-05-15 13:43:42 +0300288{
Tomas Winkler6220d6a2013-05-12 15:34:46 +0300289 struct mei_device *dev = cl->dev;
Tomas Winkler9d098192014-02-19 17:35:48 +0200290 u32 msg_slots;
291 int slots;
Tomas Winkler2ebf8c92013-09-16 23:44:43 +0300292 int ret;
293
Alexander Usyskinff1586a2016-07-26 01:06:06 +0300294 if (!list_empty(&cl->rd_pending))
295 return 0;
296
Tomas Winkler98e70862018-07-31 09:35:33 +0300297 msg_slots = mei_hbm2slots(sizeof(struct hbm_flow_control));
Tomas Winkler9d098192014-02-19 17:35:48 +0200298 slots = mei_hbuf_empty_slots(dev);
Tomas Winklerde877432018-07-12 17:10:08 +0300299 if (slots < 0)
300 return -EOVERFLOW;
Tomas Winkler2ebf8c92013-09-16 23:44:43 +0300301
Tomas Winklerde877432018-07-12 17:10:08 +0300302 if ((u32)slots < msg_slots)
Tomas Winklerc8c8d082013-03-11 18:27:02 +0200303 return -EMSGSIZE;
Tomas Winkler7bdf72d2012-07-04 19:24:52 +0300304
Tomas Winkler2ebf8c92013-09-16 23:44:43 +0300305 ret = mei_hbm_cl_flow_control_req(dev, cl);
306 if (ret) {
307 cl->status = ret;
Tomas Winkler6220d6a2013-05-12 15:34:46 +0300308 cb->buf_idx = 0;
Alexander Usyskin962ff7b2017-01-27 16:32:45 +0200309 list_move_tail(&cb->list, cmpl_list);
Tomas Winkler2ebf8c92013-09-16 23:44:43 +0300310 return ret;
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200311 }
Tomas Winkler2ebf8c92013-09-16 23:44:43 +0300312
Alexander Usyskinbbf0a942021-05-26 22:33:34 +0300313 pm_runtime_mark_last_busy(dev->dev);
314 pm_request_autosuspend(dev->dev);
315
Tomas Winklera9bed612015-02-10 10:39:46 +0200316 list_move_tail(&cb->list, &cl->rd_pending);
Tomas Winkler1ccb7b62012-03-14 14:39:42 +0200317
Oren Weilfb7d8792011-05-15 13:43:42 +0300318 return 0;
319}
320
Alexander Usyskin603c53e2016-02-07 23:35:36 +0200321static inline bool hdr_is_hbm(struct mei_msg_hdr *mei_hdr)
322{
323 return mei_hdr->host_addr == 0 && mei_hdr->me_addr == 0;
324}
325
326static inline bool hdr_is_fixed(struct mei_msg_hdr *mei_hdr)
327{
328 return mei_hdr->host_addr == 0 && mei_hdr->me_addr != 0;
329}
330
Tomas Winkler99c06862017-06-12 12:15:55 +0300331static inline int hdr_is_valid(u32 msg_hdr)
332{
333 struct mei_msg_hdr *mei_hdr;
Alexander Usyskin1309ecc2021-01-29 14:07:46 +0200334 u32 expected_len = 0;
Tomas Winkler99c06862017-06-12 12:15:55 +0300335
336 mei_hdr = (struct mei_msg_hdr *)&msg_hdr;
337 if (!msg_hdr || mei_hdr->reserved)
338 return -EBADMSG;
339
Alexander Usyskin1309ecc2021-01-29 14:07:46 +0200340 if (mei_hdr->dma_ring)
341 expected_len += MEI_SLOT_SIZE;
342 if (mei_hdr->extended)
343 expected_len += MEI_SLOT_SIZE;
344 if (mei_hdr->length < expected_len)
Tomas Winkler63163212018-11-22 13:11:39 +0200345 return -EBADMSG;
346
Tomas Winkler99c06862017-06-12 12:15:55 +0300347 return 0;
348}
349
Oren Weilfb7d8792011-05-15 13:43:42 +0300350/**
Masanari Iida393b1482013-04-05 01:05:05 +0900351 * mei_irq_read_handler - bottom half read routine after ISR to
Oren Weilfb7d8792011-05-15 13:43:42 +0300352 * handle the read processing.
353 *
Oren Weilfb7d8792011-05-15 13:43:42 +0300354 * @dev: the device structure
Tomas Winkler06ecd642013-02-06 14:06:42 +0200355 * @cmpl_list: An instance of our list structure
Oren Weilfb7d8792011-05-15 13:43:42 +0300356 * @slots: slots to read.
357 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300358 * Return: 0 on success, <0 on failure.
Oren Weilfb7d8792011-05-15 13:43:42 +0300359 */
Tomas Winkler06ecd642013-02-06 14:06:42 +0200360int mei_irq_read_handler(struct mei_device *dev,
Alexander Usyskin962ff7b2017-01-27 16:32:45 +0200361 struct list_head *cmpl_list, s32 *slots)
Oren Weilfb7d8792011-05-15 13:43:42 +0300362{
363 struct mei_msg_hdr *mei_hdr;
Tomas Winkler0cd7c012020-08-18 14:51:38 +0300364 struct mei_ext_meta_hdr *meta_hdr = NULL;
Tomas Winkler10ee9072013-11-11 13:26:08 +0200365 struct mei_cl *cl;
366 int ret;
Alexander Usyskin1309ecc2021-01-29 14:07:46 +0200367 u32 hdr_size_left;
368 u32 hdr_size_ext;
Tomas Winkler0cd7c012020-08-18 14:51:38 +0300369 int i;
370 int ext_hdr_end;
Oren Weilfb7d8792011-05-15 13:43:42 +0300371
Tomas Winkler63163212018-11-22 13:11:39 +0200372 if (!dev->rd_msg_hdr[0]) {
373 dev->rd_msg_hdr[0] = mei_read_hdr(dev);
Tomas Winkler0cd7c012020-08-18 14:51:38 +0300374 dev->rd_msg_hdr_count = 1;
Oren Weilfb7d8792011-05-15 13:43:42 +0300375 (*slots)--;
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300376 dev_dbg(dev->dev, "slots =%08x.\n", *slots);
Oren Weilfb7d8792011-05-15 13:43:42 +0300377
Tomas Winkler63163212018-11-22 13:11:39 +0200378 ret = hdr_is_valid(dev->rd_msg_hdr[0]);
Tomas Winkler99c06862017-06-12 12:15:55 +0300379 if (ret) {
380 dev_err(dev->dev, "corrupted message header 0x%08X\n",
Tomas Winkler63163212018-11-22 13:11:39 +0200381 dev->rd_msg_hdr[0]);
Tomas Winkler99c06862017-06-12 12:15:55 +0300382 goto end;
383 }
Oren Weilfb7d8792011-05-15 13:43:42 +0300384 }
385
Tomas Winkler63163212018-11-22 13:11:39 +0200386 mei_hdr = (struct mei_msg_hdr *)dev->rd_msg_hdr;
Tomas Winkler99c06862017-06-12 12:15:55 +0300387 dev_dbg(dev->dev, MEI_HDR_FMT, MEI_HDR_PRM(mei_hdr));
388
Tomas Winkler10ee9072013-11-11 13:26:08 +0200389 if (mei_slots2data(*slots) < mei_hdr->length) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300390 dev_err(dev->dev, "less data available than length=%08x.\n",
Oren Weilfb7d8792011-05-15 13:43:42 +0300391 *slots);
392 /* we can't read the message */
Tomas Winklerb1b94b52014-03-03 00:21:28 +0200393 ret = -ENODATA;
Oren Weilfb7d8792011-05-15 13:43:42 +0300394 goto end;
395 }
396
Tomas Winkler0cd7c012020-08-18 14:51:38 +0300397 ext_hdr_end = 1;
Alexander Usyskin1309ecc2021-01-29 14:07:46 +0200398 hdr_size_left = mei_hdr->length;
Tomas Winkler0cd7c012020-08-18 14:51:38 +0300399
400 if (mei_hdr->extended) {
401 if (!dev->rd_msg_hdr[1]) {
Tomas Winkler40292382021-06-21 22:37:56 +0300402 dev->rd_msg_hdr[1] = mei_read_hdr(dev);
Tomas Winkler0cd7c012020-08-18 14:51:38 +0300403 dev->rd_msg_hdr_count++;
404 (*slots)--;
Tomas Winkler40292382021-06-21 22:37:56 +0300405 dev_dbg(dev->dev, "extended header is %08x\n", dev->rd_msg_hdr[1]);
Tomas Winkler0cd7c012020-08-18 14:51:38 +0300406 }
Tomas Winkler40292382021-06-21 22:37:56 +0300407 meta_hdr = ((struct mei_ext_meta_hdr *)&dev->rd_msg_hdr[1]);
Alexander Usyskin1309ecc2021-01-29 14:07:46 +0200408 if (check_add_overflow((u32)sizeof(*meta_hdr),
409 mei_slots2data(meta_hdr->size),
410 &hdr_size_ext)) {
411 dev_err(dev->dev, "extended message size too big %d\n",
412 meta_hdr->size);
413 return -EBADMSG;
414 }
415 if (hdr_size_left < hdr_size_ext) {
416 dev_err(dev->dev, "corrupted message header len %d\n",
417 mei_hdr->length);
418 return -EBADMSG;
419 }
420 hdr_size_left -= hdr_size_ext;
421
Tomas Winkler0cd7c012020-08-18 14:51:38 +0300422 ext_hdr_end = meta_hdr->size + 2;
423 for (i = dev->rd_msg_hdr_count; i < ext_hdr_end; i++) {
424 dev->rd_msg_hdr[i] = mei_read_hdr(dev);
425 dev_dbg(dev->dev, "extended header %d is %08x\n", i,
426 dev->rd_msg_hdr[i]);
427 dev->rd_msg_hdr_count++;
428 (*slots)--;
429 }
430 }
431
Tomas Winkler63163212018-11-22 13:11:39 +0200432 if (mei_hdr->dma_ring) {
Alexander Usyskin1309ecc2021-01-29 14:07:46 +0200433 if (hdr_size_left != sizeof(dev->rd_msg_hdr[ext_hdr_end])) {
434 dev_err(dev->dev, "corrupted message header len %d\n",
435 mei_hdr->length);
436 return -EBADMSG;
437 }
438
Tomas Winkler0cd7c012020-08-18 14:51:38 +0300439 dev->rd_msg_hdr[ext_hdr_end] = mei_read_hdr(dev);
440 dev->rd_msg_hdr_count++;
Tomas Winkler63163212018-11-22 13:11:39 +0200441 (*slots)--;
Tomas Winkler0cd7c012020-08-18 14:51:38 +0300442 mei_hdr->length -= sizeof(dev->rd_msg_hdr[ext_hdr_end]);
Tomas Winkler63163212018-11-22 13:11:39 +0200443 }
444
Tomas Winkler10ee9072013-11-11 13:26:08 +0200445 /* HBM message */
Alexander Usyskin603c53e2016-02-07 23:35:36 +0200446 if (hdr_is_hbm(mei_hdr)) {
Tomas Winkler544f9462014-01-08 20:19:21 +0200447 ret = mei_hbm_dispatch(dev, mei_hdr);
448 if (ret) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300449 dev_dbg(dev->dev, "mei_hbm_dispatch failed ret = %d\n",
Tomas Winkler544f9462014-01-08 20:19:21 +0200450 ret);
451 goto end;
452 }
Tomas Winkler10ee9072013-11-11 13:26:08 +0200453 goto reset_slots;
Oren Weilfb7d8792011-05-15 13:43:42 +0300454 }
455
Alexander Usyskin83ce0742014-01-08 22:31:46 +0200456 /* find recipient cl */
Tomas Winkler10ee9072013-11-11 13:26:08 +0200457 list_for_each_entry(cl, &dev->file_list, link) {
458 if (mei_cl_hbm_equal(cl, mei_hdr)) {
459 cl_dbg(dev, cl, "got a message\n");
Alexander Usyskinc10187b2022-03-08 11:59:26 +0200460 ret = mei_cl_irq_read_msg(cl, mei_hdr, meta_hdr, cmpl_list);
461 goto reset_slots;
Tomas Winkler10ee9072013-11-11 13:26:08 +0200462 }
463 }
464
Alexander Usyskin83ce0742014-01-08 22:31:46 +0200465 /* if no recipient cl was found we assume corrupted header */
Alexander Usyskinc10187b2022-03-08 11:59:26 +0200466 /* A message for not connected fixed address clients
467 * should be silently discarded
468 * On power down client may be force cleaned,
469 * silently discard such messages
470 */
471 if (hdr_is_fixed(mei_hdr) ||
472 dev->dev_state == MEI_DEV_POWER_DOWN) {
473 mei_irq_discard_msg(dev, mei_hdr, mei_hdr->length);
474 ret = 0;
475 goto reset_slots;
Tomas Winkler10ee9072013-11-11 13:26:08 +0200476 }
Alexander Usyskinc10187b2022-03-08 11:59:26 +0200477 dev_err(dev->dev, "no destination client found 0x%08X\n", dev->rd_msg_hdr[0]);
478 ret = -EBADMSG;
479 goto end;
Tomas Winkler3d33ff22015-02-10 10:39:36 +0200480
Tomas Winkler10ee9072013-11-11 13:26:08 +0200481reset_slots:
Oren Weilfb7d8792011-05-15 13:43:42 +0300482 /* reset the number of slots and header */
Tomas Winkler63163212018-11-22 13:11:39 +0200483 memset(dev->rd_msg_hdr, 0, sizeof(dev->rd_msg_hdr));
Tomas Winkler0cd7c012020-08-18 14:51:38 +0300484 dev->rd_msg_hdr_count = 0;
Oren Weilfb7d8792011-05-15 13:43:42 +0300485 *slots = mei_count_full_read_slots(dev);
Oren Weilfb7d8792011-05-15 13:43:42 +0300486 if (*slots == -EOVERFLOW) {
487 /* overflow - reset */
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300488 dev_err(dev->dev, "resetting due to slots overflow.\n");
Oren Weilfb7d8792011-05-15 13:43:42 +0300489 /* set the event since message has been read */
490 ret = -ERANGE;
491 goto end;
492 }
493end:
494 return ret;
495}
Tomas Winkler40e0b672013-03-27 16:58:30 +0200496EXPORT_SYMBOL_GPL(mei_irq_read_handler);
Oren Weilfb7d8792011-05-15 13:43:42 +0300497
498
499/**
Tomas Winkler06ecd642013-02-06 14:06:42 +0200500 * mei_irq_write_handler - dispatch write requests
501 * after irq received
Oren Weilfb7d8792011-05-15 13:43:42 +0300502 *
Oren Weilfb7d8792011-05-15 13:43:42 +0300503 * @dev: the device structure
Tomas Winkler9a84d612012-11-18 15:13:18 +0200504 * @cmpl_list: An instance of our list structure
Oren Weilfb7d8792011-05-15 13:43:42 +0300505 *
Alexander Usyskina8605ea2014-09-29 16:31:49 +0300506 * Return: 0 on success, <0 on failure.
Oren Weilfb7d8792011-05-15 13:43:42 +0300507 */
Alexander Usyskin962ff7b2017-01-27 16:32:45 +0200508int mei_irq_write_handler(struct mei_device *dev, struct list_head *cmpl_list)
Oren Weilfb7d8792011-05-15 13:43:42 +0300509{
510
511 struct mei_cl *cl;
Tomas Winkler6220d6a2013-05-12 15:34:46 +0300512 struct mei_cl_cb *cb, *next;
Tomas Winkler9a84d612012-11-18 15:13:18 +0200513 s32 slots;
Oren Weilfb7d8792011-05-15 13:43:42 +0300514 int ret;
515
Tomas Winkler6aae48f2014-02-19 17:35:47 +0200516
517 if (!mei_hbuf_acquire(dev))
Oren Weilfb7d8792011-05-15 13:43:42 +0300518 return 0;
Tomas Winkler6aae48f2014-02-19 17:35:47 +0200519
Tomas Winkler9a84d612012-11-18 15:13:18 +0200520 slots = mei_hbuf_empty_slots(dev);
Tomas Winklerde877432018-07-12 17:10:08 +0300521 if (slots < 0)
522 return -EOVERFLOW;
523
524 if (slots == 0)
Tomas Winkler7d5e0e52012-06-19 09:13:36 +0300525 return -EMSGSIZE;
526
Oren Weilfb7d8792011-05-15 13:43:42 +0300527 /* complete all waiting for write CB */
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300528 dev_dbg(dev->dev, "complete all waiting for write cb.\n");
Oren Weilfb7d8792011-05-15 13:43:42 +0300529
Alexander Usyskin962ff7b2017-01-27 16:32:45 +0200530 list_for_each_entry_safe(cb, next, &dev->write_waiting_list, list) {
Tomas Winkler6220d6a2013-05-12 15:34:46 +0300531 cl = cb->cl;
Oren Weilfb7d8792011-05-15 13:43:42 +0300532
Tomas Winklerb7cd2d92011-11-27 21:43:34 +0200533 cl->status = 0;
Tomas Winklerc54bf3a2015-02-10 10:39:39 +0200534 cl_dbg(dev, cl, "MEI WRITE COMPLETE\n");
535 cl->writing_state = MEI_WRITE_COMPLETE;
Alexander Usyskin962ff7b2017-01-27 16:32:45 +0200536 list_move_tail(&cb->list, cmpl_list);
Oren Weilfb7d8792011-05-15 13:43:42 +0300537 }
538
Oren Weilfb7d8792011-05-15 13:43:42 +0300539 /* complete control write list CB */
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300540 dev_dbg(dev->dev, "complete control write list cb.\n");
Alexander Usyskin962ff7b2017-01-27 16:32:45 +0200541 list_for_each_entry_safe(cb, next, &dev->ctrl_wr_list, list) {
Tomas Winkler6220d6a2013-05-12 15:34:46 +0300542 cl = cb->cl;
Tomas Winkler6220d6a2013-05-12 15:34:46 +0300543 switch (cb->fop_type) {
Tomas Winkler5a8373f2014-08-21 14:29:17 +0300544 case MEI_FOP_DISCONNECT:
Tomas Winklerc8372092011-11-27 21:43:33 +0200545 /* send disconnect message */
Tomas Winkler5a8373f2014-08-21 14:29:17 +0300546 ret = mei_cl_irq_disconnect(cl, cb, cmpl_list);
Tomas Winklerc8372092011-11-27 21:43:33 +0200547 if (ret)
548 return ret;
549
550 break;
Tomas Winkler4b8960b2012-11-11 17:38:00 +0200551 case MEI_FOP_READ:
Tomas Winklerc8372092011-11-27 21:43:33 +0200552 /* send flow control message */
Tomas Winkler9d098192014-02-19 17:35:48 +0200553 ret = mei_cl_irq_read(cl, cb, cmpl_list);
Tomas Winklerc8372092011-11-27 21:43:33 +0200554 if (ret)
555 return ret;
556
557 break;
Tomas Winkler02a7eec2014-02-12 21:41:51 +0200558 case MEI_FOP_CONNECT:
Tomas Winklerc8372092011-11-27 21:43:33 +0200559 /* connect message */
Tomas Winkler9d098192014-02-19 17:35:48 +0200560 ret = mei_cl_irq_connect(cl, cb, cmpl_list);
Tomas Winklerc8372092011-11-27 21:43:33 +0200561 if (ret)
562 return ret;
563
564 break;
Tomas Winkler6bb948c2014-02-12 21:41:52 +0200565 case MEI_FOP_DISCONNECT_RSP:
566 /* send disconnect resp */
Tomas Winkler9d098192014-02-19 17:35:48 +0200567 ret = mei_cl_irq_disconnect_rsp(cl, cb, cmpl_list);
Tomas Winkler6bb948c2014-02-12 21:41:52 +0200568 if (ret)
569 return ret;
Alexander Usyskin31a5ef22014-03-06 23:53:53 +0200570 break;
Tomas Winkler51678cc2015-07-26 09:54:18 +0300571
572 case MEI_FOP_NOTIFY_START:
573 case MEI_FOP_NOTIFY_STOP:
574 ret = mei_cl_irq_notify(cl, cb, cmpl_list);
575 if (ret)
576 return ret;
577 break;
Alexander Usyskin369aea82021-02-06 16:43:24 +0200578 case MEI_FOP_DMA_MAP:
579 ret = mei_cl_irq_dma_map(cl, cb, cmpl_list);
580 if (ret)
581 return ret;
582 break;
583 case MEI_FOP_DMA_UNMAP:
584 ret = mei_cl_irq_dma_unmap(cl, cb, cmpl_list);
585 if (ret)
586 return ret;
587 break;
Tomas Winklerc8372092011-11-27 21:43:33 +0200588 default:
589 BUG();
590 }
591
Oren Weilfb7d8792011-05-15 13:43:42 +0300592 }
593 /* complete write list CB */
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300594 dev_dbg(dev->dev, "complete write list cb.\n");
Alexander Usyskin962ff7b2017-01-27 16:32:45 +0200595 list_for_each_entry_safe(cb, next, &dev->write_list, list) {
Tomas Winkler6220d6a2013-05-12 15:34:46 +0300596 cl = cb->cl;
Alexander Usyskin394a77d2017-03-20 15:04:03 +0200597 ret = mei_cl_irq_write(cl, cb, cmpl_list);
Tomas Winklerbe9d87a2012-11-18 15:13:19 +0200598 if (ret)
599 return ret;
Oren Weilfb7d8792011-05-15 13:43:42 +0300600 }
601 return 0;
602}
Tomas Winkler40e0b672013-03-27 16:58:30 +0200603EXPORT_SYMBOL_GPL(mei_irq_write_handler);
Oren Weilfb7d8792011-05-15 13:43:42 +0300604
605
Alexander Usyskin18901352015-07-23 21:37:13 +0300606/**
607 * mei_connect_timeout - connect/disconnect timeouts
608 *
609 * @cl: host client
610 */
611static void mei_connect_timeout(struct mei_cl *cl)
612{
613 struct mei_device *dev = cl->dev;
614
615 if (cl->state == MEI_FILE_CONNECTING) {
616 if (dev->hbm_f_dot_supported) {
617 cl->state = MEI_FILE_DISCONNECT_REQUIRED;
618 wake_up(&cl->wait);
619 return;
620 }
621 }
622 mei_reset(dev);
623}
Oren Weilfb7d8792011-05-15 13:43:42 +0300624
Alexander Usyskin1892fc22016-09-25 13:25:31 +0300625#define MEI_STALL_TIMER_FREQ (2 * HZ)
626/**
627 * mei_schedule_stall_timer - re-arm stall_timer work
628 *
Alexander Usyskin1892fc22016-09-25 13:25:31 +0300629 * @dev: the device structure
Randy Dunlap980dcc72023-10-11 19:48:44 -0700630 *
631 * Schedule stall timer
Alexander Usyskin1892fc22016-09-25 13:25:31 +0300632 */
633void mei_schedule_stall_timer(struct mei_device *dev)
634{
635 schedule_delayed_work(&dev->timer_work, MEI_STALL_TIMER_FREQ);
636}
637
Oren Weilfb7d8792011-05-15 13:43:42 +0300638/**
639 * mei_timer - timer function.
640 *
641 * @work: pointer to the work_struct structure
642 *
Oren Weilfb7d8792011-05-15 13:43:42 +0300643 */
Oren Weila61c6532011-09-07 09:03:13 +0300644void mei_timer(struct work_struct *work)
Oren Weilfb7d8792011-05-15 13:43:42 +0300645{
Tomas Winkler31f88f52014-02-17 15:13:25 +0200646 struct mei_cl *cl;
Oren Weilfb7d8792011-05-15 13:43:42 +0300647 struct mei_device *dev = container_of(work,
Oren Weila61c6532011-09-07 09:03:13 +0300648 struct mei_device, timer_work.work);
Alexander Usyskin1892fc22016-09-25 13:25:31 +0300649 bool reschedule_timer = false;
Oren Weilfb7d8792011-05-15 13:43:42 +0300650
651 mutex_lock(&dev->device_lock);
Tomas Winkler66ae4602014-01-08 20:19:22 +0200652
653 /* Catch interrupt stalls during HBM init handshake */
654 if (dev->dev_state == MEI_DEV_INIT_CLIENTS &&
655 dev->hbm_state != MEI_HBM_IDLE) {
656
657 if (dev->init_clients_timer) {
658 if (--dev->init_clients_timer == 0) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300659 dev_err(dev->dev, "timer: init clients timeout hbm_state = %d.\n",
Tomas Winkler66ae4602014-01-08 20:19:22 +0200660 dev->hbm_state);
Tomas Winkler33ec0822014-01-12 00:36:09 +0200661 mei_reset(dev);
Tomas Winkler66ae4602014-01-08 20:19:22 +0200662 goto out;
Oren Weilfb7d8792011-05-15 13:43:42 +0300663 }
Alexander Usyskin1892fc22016-09-25 13:25:31 +0300664 reschedule_timer = true;
Oren Weilfb7d8792011-05-15 13:43:42 +0300665 }
Oren Weilfb7d8792011-05-15 13:43:42 +0300666 }
Tomas Winkler66ae4602014-01-08 20:19:22 +0200667
668 if (dev->dev_state != MEI_DEV_ENABLED)
669 goto out;
670
Oren Weilfb7d8792011-05-15 13:43:42 +0300671 /*** connect/disconnect timeouts ***/
Tomas Winkler31f88f52014-02-17 15:13:25 +0200672 list_for_each_entry(cl, &dev->file_list, link) {
673 if (cl->timer_count) {
674 if (--cl->timer_count == 0) {
Tomas Winkler2bf94cab2014-09-29 16:31:42 +0300675 dev_err(dev->dev, "timer: connect/disconnect timeout.\n");
Alexander Usyskin18901352015-07-23 21:37:13 +0300676 mei_connect_timeout(cl);
Oren Weilfb7d8792011-05-15 13:43:42 +0300677 goto out;
678 }
Alexander Usyskin1892fc22016-09-25 13:25:31 +0300679 reschedule_timer = true;
Oren Weilfb7d8792011-05-15 13:43:42 +0300680 }
681 }
682
Oren Weilfb7d8792011-05-15 13:43:42 +0300683out:
Alexander Usyskin1892fc22016-09-25 13:25:31 +0300684 if (dev->dev_state != MEI_DEV_DISABLED && reschedule_timer)
685 mei_schedule_stall_timer(dev);
686
Tomas Winkler441ab502011-12-13 23:39:34 +0200687 mutex_unlock(&dev->device_lock);
Oren Weilfb7d8792011-05-15 13:43:42 +0300688}