Linux 5.1.15
[linux/fpc-iii.git] / drivers / misc / mei / interrupt.c
blob055c2d89b310819521bd83a68b2de30f7524da7d
1 /*
3 * Intel Management Engine Interface (Intel MEI) Linux driver
4 * Copyright (c) 2003-2012, Intel Corporation.
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.
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.
18 #include <linux/export.h>
19 #include <linux/kthread.h>
20 #include <linux/interrupt.h>
21 #include <linux/fs.h>
22 #include <linux/jiffies.h>
23 #include <linux/slab.h>
24 #include <linux/pm_runtime.h>
26 #include <linux/mei.h>
28 #include "mei_dev.h"
29 #include "hbm.h"
30 #include "client.h"
33 /**
34 * mei_irq_compl_handler - dispatch complete handlers
35 * for the completed callbacks
37 * @dev: mei device
38 * @cmpl_list: list of completed cbs
40 void mei_irq_compl_handler(struct mei_device *dev, struct list_head *cmpl_list)
42 struct mei_cl_cb *cb, *next;
43 struct mei_cl *cl;
45 list_for_each_entry_safe(cb, next, cmpl_list, list) {
46 cl = cb->cl;
47 list_del_init(&cb->list);
49 dev_dbg(dev->dev, "completing call back.\n");
50 mei_cl_complete(cl, cb);
53 EXPORT_SYMBOL_GPL(mei_irq_compl_handler);
55 /**
56 * mei_cl_hbm_equal - check if hbm is addressed to the client
58 * @cl: host client
59 * @mei_hdr: header of mei client message
61 * Return: true if matches, false otherwise
63 static inline int mei_cl_hbm_equal(struct mei_cl *cl,
64 struct mei_msg_hdr *mei_hdr)
66 return mei_cl_host_addr(cl) == mei_hdr->host_addr &&
67 mei_cl_me_id(cl) == mei_hdr->me_addr;
70 /**
71 * mei_irq_discard_msg - discard received message
73 * @dev: mei device
74 * @hdr: message header
76 static void mei_irq_discard_msg(struct mei_device *dev, struct mei_msg_hdr *hdr)
78 if (hdr->dma_ring)
79 mei_dma_ring_read(dev, NULL, hdr->extension[0]);
81 * no need to check for size as it is guarantied
82 * that length fits into rd_msg_buf
84 mei_read_slots(dev, dev->rd_msg_buf, hdr->length);
85 dev_dbg(dev->dev, "discarding message " MEI_HDR_FMT "\n",
86 MEI_HDR_PRM(hdr));
89 /**
90 * mei_cl_irq_read_msg - process client message
92 * @cl: reading client
93 * @mei_hdr: header of mei client message
94 * @cmpl_list: completion list
96 * Return: always 0
98 static int mei_cl_irq_read_msg(struct mei_cl *cl,
99 struct mei_msg_hdr *mei_hdr,
100 struct list_head *cmpl_list)
102 struct mei_device *dev = cl->dev;
103 struct mei_cl_cb *cb;
104 size_t buf_sz;
105 u32 length;
107 cb = list_first_entry_or_null(&cl->rd_pending, struct mei_cl_cb, list);
108 if (!cb) {
109 if (!mei_cl_is_fixed_address(cl)) {
110 cl_err(dev, cl, "pending read cb not found\n");
111 goto discard;
113 cb = mei_cl_alloc_cb(cl, mei_cl_mtu(cl), MEI_FOP_READ, cl->fp);
114 if (!cb)
115 goto discard;
116 list_add_tail(&cb->list, &cl->rd_pending);
119 if (!mei_cl_is_connected(cl)) {
120 cl_dbg(dev, cl, "not connected\n");
121 cb->status = -ENODEV;
122 goto discard;
125 length = mei_hdr->dma_ring ? mei_hdr->extension[0] : mei_hdr->length;
127 buf_sz = length + cb->buf_idx;
128 /* catch for integer overflow */
129 if (buf_sz < cb->buf_idx) {
130 cl_err(dev, cl, "message is too big len %d idx %zu\n",
131 length, cb->buf_idx);
132 cb->status = -EMSGSIZE;
133 goto discard;
136 if (cb->buf.size < buf_sz) {
137 cl_dbg(dev, cl, "message overflow. size %zu len %d idx %zu\n",
138 cb->buf.size, length, cb->buf_idx);
139 cb->status = -EMSGSIZE;
140 goto discard;
143 if (mei_hdr->dma_ring)
144 mei_dma_ring_read(dev, cb->buf.data + cb->buf_idx, length);
146 /* for DMA read 0 length to generate an interrupt to the device */
147 mei_read_slots(dev, cb->buf.data + cb->buf_idx, mei_hdr->length);
149 cb->buf_idx += length;
151 if (mei_hdr->msg_complete) {
152 cl_dbg(dev, cl, "completed read length = %zu\n", cb->buf_idx);
153 list_move_tail(&cb->list, cmpl_list);
154 } else {
155 pm_runtime_mark_last_busy(dev->dev);
156 pm_request_autosuspend(dev->dev);
159 return 0;
161 discard:
162 if (cb)
163 list_move_tail(&cb->list, cmpl_list);
164 mei_irq_discard_msg(dev, mei_hdr);
165 return 0;
169 * mei_cl_irq_disconnect_rsp - send disconnection response message
171 * @cl: client
172 * @cb: callback block.
173 * @cmpl_list: complete list.
175 * Return: 0, OK; otherwise, error.
177 static int mei_cl_irq_disconnect_rsp(struct mei_cl *cl, struct mei_cl_cb *cb,
178 struct list_head *cmpl_list)
180 struct mei_device *dev = cl->dev;
181 u32 msg_slots;
182 int slots;
183 int ret;
185 msg_slots = mei_hbm2slots(sizeof(struct hbm_client_connect_response));
186 slots = mei_hbuf_empty_slots(dev);
187 if (slots < 0)
188 return -EOVERFLOW;
190 if ((u32)slots < msg_slots)
191 return -EMSGSIZE;
193 ret = mei_hbm_cl_disconnect_rsp(dev, cl);
194 list_move_tail(&cb->list, cmpl_list);
196 return ret;
200 * mei_cl_irq_read - processes client read related operation from the
201 * interrupt thread context - request for flow control credits
203 * @cl: client
204 * @cb: callback block.
205 * @cmpl_list: complete list.
207 * Return: 0, OK; otherwise, error.
209 static int mei_cl_irq_read(struct mei_cl *cl, struct mei_cl_cb *cb,
210 struct list_head *cmpl_list)
212 struct mei_device *dev = cl->dev;
213 u32 msg_slots;
214 int slots;
215 int ret;
217 if (!list_empty(&cl->rd_pending))
218 return 0;
220 msg_slots = mei_hbm2slots(sizeof(struct hbm_flow_control));
221 slots = mei_hbuf_empty_slots(dev);
222 if (slots < 0)
223 return -EOVERFLOW;
225 if ((u32)slots < msg_slots)
226 return -EMSGSIZE;
228 ret = mei_hbm_cl_flow_control_req(dev, cl);
229 if (ret) {
230 cl->status = ret;
231 cb->buf_idx = 0;
232 list_move_tail(&cb->list, cmpl_list);
233 return ret;
236 list_move_tail(&cb->list, &cl->rd_pending);
238 return 0;
241 static inline bool hdr_is_hbm(struct mei_msg_hdr *mei_hdr)
243 return mei_hdr->host_addr == 0 && mei_hdr->me_addr == 0;
246 static inline bool hdr_is_fixed(struct mei_msg_hdr *mei_hdr)
248 return mei_hdr->host_addr == 0 && mei_hdr->me_addr != 0;
251 static inline int hdr_is_valid(u32 msg_hdr)
253 struct mei_msg_hdr *mei_hdr;
255 mei_hdr = (struct mei_msg_hdr *)&msg_hdr;
256 if (!msg_hdr || mei_hdr->reserved)
257 return -EBADMSG;
259 if (mei_hdr->dma_ring && mei_hdr->length != MEI_SLOT_SIZE)
260 return -EBADMSG;
262 return 0;
266 * mei_irq_read_handler - bottom half read routine after ISR to
267 * handle the read processing.
269 * @dev: the device structure
270 * @cmpl_list: An instance of our list structure
271 * @slots: slots to read.
273 * Return: 0 on success, <0 on failure.
275 int mei_irq_read_handler(struct mei_device *dev,
276 struct list_head *cmpl_list, s32 *slots)
278 struct mei_msg_hdr *mei_hdr;
279 struct mei_cl *cl;
280 int ret;
282 if (!dev->rd_msg_hdr[0]) {
283 dev->rd_msg_hdr[0] = mei_read_hdr(dev);
284 (*slots)--;
285 dev_dbg(dev->dev, "slots =%08x.\n", *slots);
287 ret = hdr_is_valid(dev->rd_msg_hdr[0]);
288 if (ret) {
289 dev_err(dev->dev, "corrupted message header 0x%08X\n",
290 dev->rd_msg_hdr[0]);
291 goto end;
295 mei_hdr = (struct mei_msg_hdr *)dev->rd_msg_hdr;
296 dev_dbg(dev->dev, MEI_HDR_FMT, MEI_HDR_PRM(mei_hdr));
298 if (mei_slots2data(*slots) < mei_hdr->length) {
299 dev_err(dev->dev, "less data available than length=%08x.\n",
300 *slots);
301 /* we can't read the message */
302 ret = -ENODATA;
303 goto end;
306 if (mei_hdr->dma_ring) {
307 dev->rd_msg_hdr[1] = mei_read_hdr(dev);
308 (*slots)--;
309 mei_hdr->length = 0;
312 /* HBM message */
313 if (hdr_is_hbm(mei_hdr)) {
314 ret = mei_hbm_dispatch(dev, mei_hdr);
315 if (ret) {
316 dev_dbg(dev->dev, "mei_hbm_dispatch failed ret = %d\n",
317 ret);
318 goto end;
320 goto reset_slots;
323 /* find recipient cl */
324 list_for_each_entry(cl, &dev->file_list, link) {
325 if (mei_cl_hbm_equal(cl, mei_hdr)) {
326 cl_dbg(dev, cl, "got a message\n");
327 break;
331 /* if no recipient cl was found we assume corrupted header */
332 if (&cl->link == &dev->file_list) {
333 /* A message for not connected fixed address clients
334 * should be silently discarded
335 * On power down client may be force cleaned,
336 * silently discard such messages
338 if (hdr_is_fixed(mei_hdr) ||
339 dev->dev_state == MEI_DEV_POWER_DOWN) {
340 mei_irq_discard_msg(dev, mei_hdr);
341 ret = 0;
342 goto reset_slots;
344 dev_err(dev->dev, "no destination client found 0x%08X\n",
345 dev->rd_msg_hdr[0]);
346 ret = -EBADMSG;
347 goto end;
350 ret = mei_cl_irq_read_msg(cl, mei_hdr, cmpl_list);
353 reset_slots:
354 /* reset the number of slots and header */
355 memset(dev->rd_msg_hdr, 0, sizeof(dev->rd_msg_hdr));
356 *slots = mei_count_full_read_slots(dev);
357 if (*slots == -EOVERFLOW) {
358 /* overflow - reset */
359 dev_err(dev->dev, "resetting due to slots overflow.\n");
360 /* set the event since message has been read */
361 ret = -ERANGE;
362 goto end;
364 end:
365 return ret;
367 EXPORT_SYMBOL_GPL(mei_irq_read_handler);
371 * mei_irq_write_handler - dispatch write requests
372 * after irq received
374 * @dev: the device structure
375 * @cmpl_list: An instance of our list structure
377 * Return: 0 on success, <0 on failure.
379 int mei_irq_write_handler(struct mei_device *dev, struct list_head *cmpl_list)
382 struct mei_cl *cl;
383 struct mei_cl_cb *cb, *next;
384 s32 slots;
385 int ret;
388 if (!mei_hbuf_acquire(dev))
389 return 0;
391 slots = mei_hbuf_empty_slots(dev);
392 if (slots < 0)
393 return -EOVERFLOW;
395 if (slots == 0)
396 return -EMSGSIZE;
398 /* complete all waiting for write CB */
399 dev_dbg(dev->dev, "complete all waiting for write cb.\n");
401 list_for_each_entry_safe(cb, next, &dev->write_waiting_list, list) {
402 cl = cb->cl;
404 cl->status = 0;
405 cl_dbg(dev, cl, "MEI WRITE COMPLETE\n");
406 cl->writing_state = MEI_WRITE_COMPLETE;
407 list_move_tail(&cb->list, cmpl_list);
410 /* complete control write list CB */
411 dev_dbg(dev->dev, "complete control write list cb.\n");
412 list_for_each_entry_safe(cb, next, &dev->ctrl_wr_list, list) {
413 cl = cb->cl;
414 switch (cb->fop_type) {
415 case MEI_FOP_DISCONNECT:
416 /* send disconnect message */
417 ret = mei_cl_irq_disconnect(cl, cb, cmpl_list);
418 if (ret)
419 return ret;
421 break;
422 case MEI_FOP_READ:
423 /* send flow control message */
424 ret = mei_cl_irq_read(cl, cb, cmpl_list);
425 if (ret)
426 return ret;
428 break;
429 case MEI_FOP_CONNECT:
430 /* connect message */
431 ret = mei_cl_irq_connect(cl, cb, cmpl_list);
432 if (ret)
433 return ret;
435 break;
436 case MEI_FOP_DISCONNECT_RSP:
437 /* send disconnect resp */
438 ret = mei_cl_irq_disconnect_rsp(cl, cb, cmpl_list);
439 if (ret)
440 return ret;
441 break;
443 case MEI_FOP_NOTIFY_START:
444 case MEI_FOP_NOTIFY_STOP:
445 ret = mei_cl_irq_notify(cl, cb, cmpl_list);
446 if (ret)
447 return ret;
448 break;
449 default:
450 BUG();
454 /* complete write list CB */
455 dev_dbg(dev->dev, "complete write list cb.\n");
456 list_for_each_entry_safe(cb, next, &dev->write_list, list) {
457 cl = cb->cl;
458 ret = mei_cl_irq_write(cl, cb, cmpl_list);
459 if (ret)
460 return ret;
462 return 0;
464 EXPORT_SYMBOL_GPL(mei_irq_write_handler);
468 * mei_connect_timeout - connect/disconnect timeouts
470 * @cl: host client
472 static void mei_connect_timeout(struct mei_cl *cl)
474 struct mei_device *dev = cl->dev;
476 if (cl->state == MEI_FILE_CONNECTING) {
477 if (dev->hbm_f_dot_supported) {
478 cl->state = MEI_FILE_DISCONNECT_REQUIRED;
479 wake_up(&cl->wait);
480 return;
483 mei_reset(dev);
486 #define MEI_STALL_TIMER_FREQ (2 * HZ)
488 * mei_schedule_stall_timer - re-arm stall_timer work
490 * Schedule stall timer
492 * @dev: the device structure
494 void mei_schedule_stall_timer(struct mei_device *dev)
496 schedule_delayed_work(&dev->timer_work, MEI_STALL_TIMER_FREQ);
500 * mei_timer - timer function.
502 * @work: pointer to the work_struct structure
505 void mei_timer(struct work_struct *work)
507 struct mei_cl *cl;
508 struct mei_device *dev = container_of(work,
509 struct mei_device, timer_work.work);
510 bool reschedule_timer = false;
512 mutex_lock(&dev->device_lock);
514 /* Catch interrupt stalls during HBM init handshake */
515 if (dev->dev_state == MEI_DEV_INIT_CLIENTS &&
516 dev->hbm_state != MEI_HBM_IDLE) {
518 if (dev->init_clients_timer) {
519 if (--dev->init_clients_timer == 0) {
520 dev_err(dev->dev, "timer: init clients timeout hbm_state = %d.\n",
521 dev->hbm_state);
522 mei_reset(dev);
523 goto out;
525 reschedule_timer = true;
529 if (dev->dev_state != MEI_DEV_ENABLED)
530 goto out;
532 /*** connect/disconnect timeouts ***/
533 list_for_each_entry(cl, &dev->file_list, link) {
534 if (cl->timer_count) {
535 if (--cl->timer_count == 0) {
536 dev_err(dev->dev, "timer: connect/disconnect timeout.\n");
537 mei_connect_timeout(cl);
538 goto out;
540 reschedule_timer = true;
544 out:
545 if (dev->dev_state != MEI_DEV_DISABLED && reschedule_timer)
546 mei_schedule_stall_timer(dev);
548 mutex_unlock(&dev->device_lock);