blob: a70d989032c19c0545c8c4351347a168a58ecdc8 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
David Brazdil0f672f62019-12-10 10:32:29 +00003 * Copyright (c) 2003-2018, Intel Corporation. All rights reserved.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004 * Intel Management Engine Interface (Intel MEI) Linux driver
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00005 */
6
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007#include <linux/export.h>
8#include <linux/kthread.h>
9#include <linux/interrupt.h>
10#include <linux/fs.h>
11#include <linux/jiffies.h>
12#include <linux/slab.h>
13#include <linux/pm_runtime.h>
14
15#include <linux/mei.h>
16
17#include "mei_dev.h"
18#include "hbm.h"
19#include "client.h"
20
21
22/**
23 * mei_irq_compl_handler - dispatch complete handlers
24 * for the completed callbacks
25 *
26 * @dev: mei device
27 * @cmpl_list: list of completed cbs
28 */
29void mei_irq_compl_handler(struct mei_device *dev, struct list_head *cmpl_list)
30{
31 struct mei_cl_cb *cb, *next;
32 struct mei_cl *cl;
33
34 list_for_each_entry_safe(cb, next, cmpl_list, list) {
35 cl = cb->cl;
36 list_del_init(&cb->list);
37
38 dev_dbg(dev->dev, "completing call back.\n");
39 mei_cl_complete(cl, cb);
40 }
41}
42EXPORT_SYMBOL_GPL(mei_irq_compl_handler);
43
44/**
45 * mei_cl_hbm_equal - check if hbm is addressed to the client
46 *
47 * @cl: host client
48 * @mei_hdr: header of mei client message
49 *
50 * Return: true if matches, false otherwise
51 */
52static inline int mei_cl_hbm_equal(struct mei_cl *cl,
53 struct mei_msg_hdr *mei_hdr)
54{
55 return mei_cl_host_addr(cl) == mei_hdr->host_addr &&
56 mei_cl_me_id(cl) == mei_hdr->me_addr;
57}
58
59/**
60 * mei_irq_discard_msg - discard received message
61 *
62 * @dev: mei device
63 * @hdr: message header
64 */
65static void mei_irq_discard_msg(struct mei_device *dev, struct mei_msg_hdr *hdr)
66{
David Brazdil0f672f62019-12-10 10:32:29 +000067 if (hdr->dma_ring)
68 mei_dma_ring_read(dev, NULL, hdr->extension[0]);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000069 /*
70 * no need to check for size as it is guarantied
71 * that length fits into rd_msg_buf
72 */
73 mei_read_slots(dev, dev->rd_msg_buf, hdr->length);
74 dev_dbg(dev->dev, "discarding message " MEI_HDR_FMT "\n",
75 MEI_HDR_PRM(hdr));
76}
77
78/**
79 * mei_cl_irq_read_msg - process client message
80 *
81 * @cl: reading client
82 * @mei_hdr: header of mei client message
83 * @cmpl_list: completion list
84 *
85 * Return: always 0
86 */
87static int mei_cl_irq_read_msg(struct mei_cl *cl,
88 struct mei_msg_hdr *mei_hdr,
89 struct list_head *cmpl_list)
90{
91 struct mei_device *dev = cl->dev;
92 struct mei_cl_cb *cb;
93 size_t buf_sz;
David Brazdil0f672f62019-12-10 10:32:29 +000094 u32 length;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000095
96 cb = list_first_entry_or_null(&cl->rd_pending, struct mei_cl_cb, list);
97 if (!cb) {
98 if (!mei_cl_is_fixed_address(cl)) {
99 cl_err(dev, cl, "pending read cb not found\n");
100 goto discard;
101 }
102 cb = mei_cl_alloc_cb(cl, mei_cl_mtu(cl), MEI_FOP_READ, cl->fp);
103 if (!cb)
104 goto discard;
105 list_add_tail(&cb->list, &cl->rd_pending);
106 }
107
108 if (!mei_cl_is_connected(cl)) {
109 cl_dbg(dev, cl, "not connected\n");
110 cb->status = -ENODEV;
111 goto discard;
112 }
113
David Brazdil0f672f62019-12-10 10:32:29 +0000114 length = mei_hdr->dma_ring ? mei_hdr->extension[0] : mei_hdr->length;
115
116 buf_sz = length + cb->buf_idx;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000117 /* catch for integer overflow */
118 if (buf_sz < cb->buf_idx) {
119 cl_err(dev, cl, "message is too big len %d idx %zu\n",
David Brazdil0f672f62019-12-10 10:32:29 +0000120 length, cb->buf_idx);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000121 cb->status = -EMSGSIZE;
122 goto discard;
123 }
124
125 if (cb->buf.size < buf_sz) {
126 cl_dbg(dev, cl, "message overflow. size %zu len %d idx %zu\n",
David Brazdil0f672f62019-12-10 10:32:29 +0000127 cb->buf.size, length, cb->buf_idx);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000128 cb->status = -EMSGSIZE;
129 goto discard;
130 }
131
David Brazdil0f672f62019-12-10 10:32:29 +0000132 if (mei_hdr->dma_ring)
133 mei_dma_ring_read(dev, cb->buf.data + cb->buf_idx, length);
134
135 /* for DMA read 0 length to generate an interrupt to the device */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000136 mei_read_slots(dev, cb->buf.data + cb->buf_idx, mei_hdr->length);
137
David Brazdil0f672f62019-12-10 10:32:29 +0000138 cb->buf_idx += length;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000139
140 if (mei_hdr->msg_complete) {
141 cl_dbg(dev, cl, "completed read length = %zu\n", cb->buf_idx);
142 list_move_tail(&cb->list, cmpl_list);
143 } else {
144 pm_runtime_mark_last_busy(dev->dev);
145 pm_request_autosuspend(dev->dev);
146 }
147
148 return 0;
149
150discard:
151 if (cb)
152 list_move_tail(&cb->list, cmpl_list);
153 mei_irq_discard_msg(dev, mei_hdr);
154 return 0;
155}
156
157/**
158 * mei_cl_irq_disconnect_rsp - send disconnection response message
159 *
160 * @cl: client
161 * @cb: callback block.
162 * @cmpl_list: complete list.
163 *
164 * Return: 0, OK; otherwise, error.
165 */
166static int mei_cl_irq_disconnect_rsp(struct mei_cl *cl, struct mei_cl_cb *cb,
167 struct list_head *cmpl_list)
168{
169 struct mei_device *dev = cl->dev;
170 u32 msg_slots;
171 int slots;
172 int ret;
173
174 msg_slots = mei_hbm2slots(sizeof(struct hbm_client_connect_response));
175 slots = mei_hbuf_empty_slots(dev);
176 if (slots < 0)
177 return -EOVERFLOW;
178
179 if ((u32)slots < msg_slots)
180 return -EMSGSIZE;
181
182 ret = mei_hbm_cl_disconnect_rsp(dev, cl);
183 list_move_tail(&cb->list, cmpl_list);
184
185 return ret;
186}
187
188/**
189 * mei_cl_irq_read - processes client read related operation from the
190 * interrupt thread context - request for flow control credits
191 *
192 * @cl: client
193 * @cb: callback block.
194 * @cmpl_list: complete list.
195 *
196 * Return: 0, OK; otherwise, error.
197 */
198static int mei_cl_irq_read(struct mei_cl *cl, struct mei_cl_cb *cb,
199 struct list_head *cmpl_list)
200{
201 struct mei_device *dev = cl->dev;
202 u32 msg_slots;
203 int slots;
204 int ret;
205
206 if (!list_empty(&cl->rd_pending))
207 return 0;
208
209 msg_slots = mei_hbm2slots(sizeof(struct hbm_flow_control));
210 slots = mei_hbuf_empty_slots(dev);
211 if (slots < 0)
212 return -EOVERFLOW;
213
214 if ((u32)slots < msg_slots)
215 return -EMSGSIZE;
216
217 ret = mei_hbm_cl_flow_control_req(dev, cl);
218 if (ret) {
219 cl->status = ret;
220 cb->buf_idx = 0;
221 list_move_tail(&cb->list, cmpl_list);
222 return ret;
223 }
224
Olivier Deprez0e641232021-09-23 10:07:05 +0200225 pm_runtime_mark_last_busy(dev->dev);
226 pm_request_autosuspend(dev->dev);
227
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000228 list_move_tail(&cb->list, &cl->rd_pending);
229
230 return 0;
231}
232
233static inline bool hdr_is_hbm(struct mei_msg_hdr *mei_hdr)
234{
235 return mei_hdr->host_addr == 0 && mei_hdr->me_addr == 0;
236}
237
238static inline bool hdr_is_fixed(struct mei_msg_hdr *mei_hdr)
239{
240 return mei_hdr->host_addr == 0 && mei_hdr->me_addr != 0;
241}
242
243static inline int hdr_is_valid(u32 msg_hdr)
244{
245 struct mei_msg_hdr *mei_hdr;
246
247 mei_hdr = (struct mei_msg_hdr *)&msg_hdr;
248 if (!msg_hdr || mei_hdr->reserved)
249 return -EBADMSG;
250
David Brazdil0f672f62019-12-10 10:32:29 +0000251 if (mei_hdr->dma_ring && mei_hdr->length != MEI_SLOT_SIZE)
252 return -EBADMSG;
253
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000254 return 0;
255}
256
257/**
258 * mei_irq_read_handler - bottom half read routine after ISR to
259 * handle the read processing.
260 *
261 * @dev: the device structure
262 * @cmpl_list: An instance of our list structure
263 * @slots: slots to read.
264 *
265 * Return: 0 on success, <0 on failure.
266 */
267int mei_irq_read_handler(struct mei_device *dev,
268 struct list_head *cmpl_list, s32 *slots)
269{
270 struct mei_msg_hdr *mei_hdr;
271 struct mei_cl *cl;
272 int ret;
273
David Brazdil0f672f62019-12-10 10:32:29 +0000274 if (!dev->rd_msg_hdr[0]) {
275 dev->rd_msg_hdr[0] = mei_read_hdr(dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000276 (*slots)--;
277 dev_dbg(dev->dev, "slots =%08x.\n", *slots);
278
David Brazdil0f672f62019-12-10 10:32:29 +0000279 ret = hdr_is_valid(dev->rd_msg_hdr[0]);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000280 if (ret) {
281 dev_err(dev->dev, "corrupted message header 0x%08X\n",
David Brazdil0f672f62019-12-10 10:32:29 +0000282 dev->rd_msg_hdr[0]);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000283 goto end;
284 }
285 }
286
David Brazdil0f672f62019-12-10 10:32:29 +0000287 mei_hdr = (struct mei_msg_hdr *)dev->rd_msg_hdr;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000288 dev_dbg(dev->dev, MEI_HDR_FMT, MEI_HDR_PRM(mei_hdr));
289
290 if (mei_slots2data(*slots) < mei_hdr->length) {
291 dev_err(dev->dev, "less data available than length=%08x.\n",
292 *slots);
293 /* we can't read the message */
294 ret = -ENODATA;
295 goto end;
296 }
297
David Brazdil0f672f62019-12-10 10:32:29 +0000298 if (mei_hdr->dma_ring) {
299 dev->rd_msg_hdr[1] = mei_read_hdr(dev);
300 (*slots)--;
301 mei_hdr->length = 0;
302 }
303
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000304 /* HBM message */
305 if (hdr_is_hbm(mei_hdr)) {
306 ret = mei_hbm_dispatch(dev, mei_hdr);
307 if (ret) {
308 dev_dbg(dev->dev, "mei_hbm_dispatch failed ret = %d\n",
309 ret);
310 goto end;
311 }
312 goto reset_slots;
313 }
314
315 /* find recipient cl */
316 list_for_each_entry(cl, &dev->file_list, link) {
317 if (mei_cl_hbm_equal(cl, mei_hdr)) {
318 cl_dbg(dev, cl, "got a message\n");
319 break;
320 }
321 }
322
323 /* if no recipient cl was found we assume corrupted header */
324 if (&cl->link == &dev->file_list) {
325 /* A message for not connected fixed address clients
326 * should be silently discarded
327 * On power down client may be force cleaned,
328 * silently discard such messages
329 */
330 if (hdr_is_fixed(mei_hdr) ||
331 dev->dev_state == MEI_DEV_POWER_DOWN) {
332 mei_irq_discard_msg(dev, mei_hdr);
333 ret = 0;
334 goto reset_slots;
335 }
336 dev_err(dev->dev, "no destination client found 0x%08X\n",
David Brazdil0f672f62019-12-10 10:32:29 +0000337 dev->rd_msg_hdr[0]);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000338 ret = -EBADMSG;
339 goto end;
340 }
341
342 ret = mei_cl_irq_read_msg(cl, mei_hdr, cmpl_list);
343
344
345reset_slots:
346 /* reset the number of slots and header */
David Brazdil0f672f62019-12-10 10:32:29 +0000347 memset(dev->rd_msg_hdr, 0, sizeof(dev->rd_msg_hdr));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000348 *slots = mei_count_full_read_slots(dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000349 if (*slots == -EOVERFLOW) {
350 /* overflow - reset */
351 dev_err(dev->dev, "resetting due to slots overflow.\n");
352 /* set the event since message has been read */
353 ret = -ERANGE;
354 goto end;
355 }
356end:
357 return ret;
358}
359EXPORT_SYMBOL_GPL(mei_irq_read_handler);
360
361
362/**
363 * mei_irq_write_handler - dispatch write requests
364 * after irq received
365 *
366 * @dev: the device structure
367 * @cmpl_list: An instance of our list structure
368 *
369 * Return: 0 on success, <0 on failure.
370 */
371int mei_irq_write_handler(struct mei_device *dev, struct list_head *cmpl_list)
372{
373
374 struct mei_cl *cl;
375 struct mei_cl_cb *cb, *next;
376 s32 slots;
377 int ret;
378
379
380 if (!mei_hbuf_acquire(dev))
381 return 0;
382
383 slots = mei_hbuf_empty_slots(dev);
384 if (slots < 0)
385 return -EOVERFLOW;
386
387 if (slots == 0)
388 return -EMSGSIZE;
389
390 /* complete all waiting for write CB */
391 dev_dbg(dev->dev, "complete all waiting for write cb.\n");
392
393 list_for_each_entry_safe(cb, next, &dev->write_waiting_list, list) {
394 cl = cb->cl;
395
396 cl->status = 0;
397 cl_dbg(dev, cl, "MEI WRITE COMPLETE\n");
398 cl->writing_state = MEI_WRITE_COMPLETE;
399 list_move_tail(&cb->list, cmpl_list);
400 }
401
402 /* complete control write list CB */
403 dev_dbg(dev->dev, "complete control write list cb.\n");
404 list_for_each_entry_safe(cb, next, &dev->ctrl_wr_list, list) {
405 cl = cb->cl;
406 switch (cb->fop_type) {
407 case MEI_FOP_DISCONNECT:
408 /* send disconnect message */
409 ret = mei_cl_irq_disconnect(cl, cb, cmpl_list);
410 if (ret)
411 return ret;
412
413 break;
414 case MEI_FOP_READ:
415 /* send flow control message */
416 ret = mei_cl_irq_read(cl, cb, cmpl_list);
417 if (ret)
418 return ret;
419
420 break;
421 case MEI_FOP_CONNECT:
422 /* connect message */
423 ret = mei_cl_irq_connect(cl, cb, cmpl_list);
424 if (ret)
425 return ret;
426
427 break;
428 case MEI_FOP_DISCONNECT_RSP:
429 /* send disconnect resp */
430 ret = mei_cl_irq_disconnect_rsp(cl, cb, cmpl_list);
431 if (ret)
432 return ret;
433 break;
434
435 case MEI_FOP_NOTIFY_START:
436 case MEI_FOP_NOTIFY_STOP:
437 ret = mei_cl_irq_notify(cl, cb, cmpl_list);
438 if (ret)
439 return ret;
440 break;
441 default:
442 BUG();
443 }
444
445 }
446 /* complete write list CB */
447 dev_dbg(dev->dev, "complete write list cb.\n");
448 list_for_each_entry_safe(cb, next, &dev->write_list, list) {
449 cl = cb->cl;
450 ret = mei_cl_irq_write(cl, cb, cmpl_list);
451 if (ret)
452 return ret;
453 }
454 return 0;
455}
456EXPORT_SYMBOL_GPL(mei_irq_write_handler);
457
458
459/**
460 * mei_connect_timeout - connect/disconnect timeouts
461 *
462 * @cl: host client
463 */
464static void mei_connect_timeout(struct mei_cl *cl)
465{
466 struct mei_device *dev = cl->dev;
467
468 if (cl->state == MEI_FILE_CONNECTING) {
469 if (dev->hbm_f_dot_supported) {
470 cl->state = MEI_FILE_DISCONNECT_REQUIRED;
471 wake_up(&cl->wait);
472 return;
473 }
474 }
475 mei_reset(dev);
476}
477
478#define MEI_STALL_TIMER_FREQ (2 * HZ)
479/**
480 * mei_schedule_stall_timer - re-arm stall_timer work
481 *
482 * Schedule stall timer
483 *
484 * @dev: the device structure
485 */
486void mei_schedule_stall_timer(struct mei_device *dev)
487{
488 schedule_delayed_work(&dev->timer_work, MEI_STALL_TIMER_FREQ);
489}
490
491/**
492 * mei_timer - timer function.
493 *
494 * @work: pointer to the work_struct structure
495 *
496 */
497void mei_timer(struct work_struct *work)
498{
499 struct mei_cl *cl;
500 struct mei_device *dev = container_of(work,
501 struct mei_device, timer_work.work);
502 bool reschedule_timer = false;
503
504 mutex_lock(&dev->device_lock);
505
506 /* Catch interrupt stalls during HBM init handshake */
507 if (dev->dev_state == MEI_DEV_INIT_CLIENTS &&
508 dev->hbm_state != MEI_HBM_IDLE) {
509
510 if (dev->init_clients_timer) {
511 if (--dev->init_clients_timer == 0) {
512 dev_err(dev->dev, "timer: init clients timeout hbm_state = %d.\n",
513 dev->hbm_state);
514 mei_reset(dev);
515 goto out;
516 }
517 reschedule_timer = true;
518 }
519 }
520
521 if (dev->dev_state != MEI_DEV_ENABLED)
522 goto out;
523
524 /*** connect/disconnect timeouts ***/
525 list_for_each_entry(cl, &dev->file_list, link) {
526 if (cl->timer_count) {
527 if (--cl->timer_count == 0) {
528 dev_err(dev->dev, "timer: connect/disconnect timeout.\n");
529 mei_connect_timeout(cl);
530 goto out;
531 }
532 reschedule_timer = true;
533 }
534 }
535
536out:
537 if (dev->dev_state != MEI_DEV_DISABLED && reschedule_timer)
538 mei_schedule_stall_timer(dev);
539
540 mutex_unlock(&dev->device_lock);
541}