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
18 #include <linux/export.h>
19 #include <linux/pci.h>
20 #include <linux/kthread.h>
21 #include <linux/interrupt.h>
23 #include <linux/jiffies.h>
25 #include <linux/mei.h>
34 * mei_cl_complete_handler - processes completed operation for a client
36 * @cl: private data of the file object.
37 * @cb: callback block.
39 static void mei_cl_complete_handler(struct mei_cl *cl, struct mei_cl_cb *cb)
41 if (cb->fop_type == MEI_FOP_WRITE) {
44 cl->writing_state = MEI_WRITE_COMPLETE;
45 if (waitqueue_active(&cl->tx_wait))
46 wake_up_interruptible(&cl->tx_wait);
48 } else if (cb->fop_type == MEI_FOP_READ &&
49 MEI_READING == cl->reading_state) {
50 cl->reading_state = MEI_READ_COMPLETE;
51 if (waitqueue_active(&cl->rx_wait))
52 wake_up_interruptible(&cl->rx_wait);
54 mei_cl_bus_rx_event(cl);
60 * mei_irq_compl_handler - dispatch complete handelers
61 * for the completed callbacks
64 * @compl_list - list of completed cbs
66 void mei_irq_compl_handler(struct mei_device *dev, struct mei_cl_cb *compl_list)
68 struct mei_cl_cb *cb, *next;
71 list_for_each_entry_safe(cb, next, &compl_list->list, list) {
77 dev_dbg(&dev->pdev->dev, "completing call back.\n");
78 if (cl == &dev->iamthif_cl)
79 mei_amthif_complete(dev, cb);
81 mei_cl_complete_handler(cl, cb);
84 EXPORT_SYMBOL_GPL(mei_irq_compl_handler);
87 * mei_cl_hbm_equal - check if hbm is addressed to the client
90 * @mei_hdr: header of mei client message
92 * returns true if matches, false otherwise
94 static inline int mei_cl_hbm_equal(struct mei_cl *cl,
95 struct mei_msg_hdr *mei_hdr)
97 return cl->host_client_id == mei_hdr->host_addr &&
98 cl->me_client_id == mei_hdr->me_addr;
101 * mei_cl_is_reading - checks if the client
102 is the one to read this message
105 * @mei_hdr: header of mei message
107 * returns true on match and false otherwise
109 static bool mei_cl_is_reading(struct mei_cl *cl, struct mei_msg_hdr *mei_hdr)
111 return mei_cl_hbm_equal(cl, mei_hdr) &&
112 cl->state == MEI_FILE_CONNECTED &&
113 cl->reading_state != MEI_READ_COMPLETE;
117 * mei_irq_read_client_message - process client message
119 * @dev: the device structure
120 * @mei_hdr: header of mei client message
121 * @complete_list: An instance of our list structure
123 * returns 0 on success, <0 on failure.
125 static int mei_cl_irq_read_msg(struct mei_device *dev,
126 struct mei_msg_hdr *mei_hdr,
127 struct mei_cl_cb *complete_list)
130 struct mei_cl_cb *cb, *next;
131 unsigned char *buffer = NULL;
133 list_for_each_entry_safe(cb, next, &dev->read_list.list, list) {
135 if (!cl || !mei_cl_is_reading(cl, mei_hdr))
138 cl->reading_state = MEI_READING;
140 if (cb->response_buffer.size == 0 ||
141 cb->response_buffer.data == NULL) {
142 dev_err(&dev->pdev->dev, "response buffer is not allocated.\n");
147 if (cb->response_buffer.size < mei_hdr->length + cb->buf_idx) {
148 dev_dbg(&dev->pdev->dev, "message overflow. size %d len %d idx %ld\n",
149 cb->response_buffer.size,
150 mei_hdr->length, cb->buf_idx);
151 buffer = krealloc(cb->response_buffer.data,
152 mei_hdr->length + cb->buf_idx,
156 dev_err(&dev->pdev->dev, "allocation failed.\n");
160 cb->response_buffer.data = buffer;
161 cb->response_buffer.size =
162 mei_hdr->length + cb->buf_idx;
165 buffer = cb->response_buffer.data + cb->buf_idx;
166 mei_read_slots(dev, buffer, mei_hdr->length);
168 cb->buf_idx += mei_hdr->length;
169 if (mei_hdr->msg_complete) {
172 dev_dbg(&dev->pdev->dev, "completed read H cl = %d, ME cl = %d, length = %lu\n",
176 list_add_tail(&cb->list, &complete_list->list);
181 dev_dbg(&dev->pdev->dev, "message read\n");
183 mei_read_slots(dev, dev->rd_msg_buf, mei_hdr->length);
184 dev_dbg(&dev->pdev->dev, "discarding message " MEI_HDR_FMT "\n",
185 MEI_HDR_PRM(mei_hdr));
192 * _mei_irq_thread_close - processes close related operation.
194 * @dev: the device structure.
195 * @slots: free slots.
196 * @cb_pos: callback block.
197 * @cl: private data of the file object.
198 * @cmpl_list: complete list.
200 * returns 0, OK; otherwise, error.
202 static int _mei_irq_thread_close(struct mei_device *dev, s32 *slots,
203 struct mei_cl_cb *cb_pos,
205 struct mei_cl_cb *cmpl_list)
208 mei_data2slots(sizeof(struct hbm_client_connect_request));
210 if (*slots < msg_slots)
215 if (mei_hbm_cl_disconnect_req(dev, cl)) {
218 list_move_tail(&cb_pos->list, &cmpl_list->list);
222 cl->state = MEI_FILE_DISCONNECTING;
225 list_move_tail(&cb_pos->list, &dev->ctrl_rd_list.list);
226 cl->timer_count = MEI_CONNECT_TIMEOUT;
233 * _mei_hb_read - processes read related operation.
235 * @dev: the device structure.
236 * @slots: free slots.
237 * @cb_pos: callback block.
238 * @cl: private data of the file object.
239 * @cmpl_list: complete list.
241 * returns 0, OK; otherwise, error.
243 static int _mei_irq_thread_read(struct mei_device *dev, s32 *slots,
244 struct mei_cl_cb *cb_pos,
246 struct mei_cl_cb *cmpl_list)
248 u32 msg_slots = mei_data2slots(sizeof(struct hbm_flow_control));
250 if (*slots < msg_slots) {
251 /* return the cancel routine */
252 list_del(&cb_pos->list);
258 if (mei_hbm_cl_flow_control_req(dev, cl)) {
259 cl->status = -ENODEV;
261 list_move_tail(&cb_pos->list, &cmpl_list->list);
264 list_move_tail(&cb_pos->list, &dev->read_list.list);
271 * _mei_irq_thread_ioctl - processes ioctl related operation.
273 * @dev: the device structure.
274 * @slots: free slots.
275 * @cb_pos: callback block.
276 * @cl: private data of the file object.
277 * @cmpl_list: complete list.
279 * returns 0, OK; otherwise, error.
281 static int _mei_irq_thread_ioctl(struct mei_device *dev, s32 *slots,
282 struct mei_cl_cb *cb_pos,
284 struct mei_cl_cb *cmpl_list)
287 mei_data2slots(sizeof(struct hbm_client_connect_request));
289 if (*slots < msg_slots) {
290 /* return the cancel routine */
291 list_del(&cb_pos->list);
297 cl->state = MEI_FILE_CONNECTING;
299 if (mei_hbm_cl_connect_req(dev, cl)) {
300 cl->status = -ENODEV;
302 list_del(&cb_pos->list);
305 list_move_tail(&cb_pos->list, &dev->ctrl_rd_list.list);
306 cl->timer_count = MEI_CONNECT_TIMEOUT;
312 * mei_irq_thread_write_complete - write messages to device.
314 * @dev: the device structure.
315 * @slots: free slots.
316 * @cb: callback block.
317 * @cmpl_list: complete list.
319 * returns 0, OK; otherwise, error.
321 static int mei_irq_thread_write_complete(struct mei_device *dev, s32 *slots,
322 struct mei_cl_cb *cb, struct mei_cl_cb *cmpl_list)
324 struct mei_msg_hdr mei_hdr;
325 struct mei_cl *cl = cb->cl;
326 size_t len = cb->request_buffer.size - cb->buf_idx;
327 u32 msg_slots = mei_data2slots(len);
329 mei_hdr.host_addr = cl->host_client_id;
330 mei_hdr.me_addr = cl->me_client_id;
331 mei_hdr.reserved = 0;
333 if (*slots >= msg_slots) {
334 mei_hdr.length = len;
335 mei_hdr.msg_complete = 1;
336 /* Split the message only if we can write the whole host buffer */
337 } else if (*slots == dev->hbuf_depth) {
339 len = (*slots * sizeof(u32)) - sizeof(struct mei_msg_hdr);
340 mei_hdr.length = len;
341 mei_hdr.msg_complete = 0;
343 /* wait for next time the host buffer is empty */
347 dev_dbg(&dev->pdev->dev, "buf: size = %d idx = %lu\n",
348 cb->request_buffer.size, cb->buf_idx);
349 dev_dbg(&dev->pdev->dev, MEI_HDR_FMT, MEI_HDR_PRM(&mei_hdr));
352 if (mei_write_message(dev, &mei_hdr,
353 cb->request_buffer.data + cb->buf_idx)) {
354 cl->status = -ENODEV;
355 list_move_tail(&cb->list, &cmpl_list->list);
361 cb->buf_idx += mei_hdr.length;
362 if (mei_hdr.msg_complete) {
363 if (mei_cl_flow_ctrl_reduce(cl))
365 list_move_tail(&cb->list, &dev->write_waiting_list.list);
372 * mei_irq_thread_read_handler - bottom half read routine after ISR to
373 * handle the read processing.
375 * @dev: the device structure
376 * @cmpl_list: An instance of our list structure
377 * @slots: slots to read.
379 * returns 0 on success, <0 on failure.
381 int mei_irq_read_handler(struct mei_device *dev,
382 struct mei_cl_cb *cmpl_list, s32 *slots)
384 struct mei_msg_hdr *mei_hdr;
385 struct mei_cl *cl_pos = NULL;
386 struct mei_cl *cl_next = NULL;
389 if (!dev->rd_msg_hdr) {
390 dev->rd_msg_hdr = mei_read_hdr(dev);
391 dev_dbg(&dev->pdev->dev, "slots =%08x.\n", *slots);
393 dev_dbg(&dev->pdev->dev, "slots =%08x.\n", *slots);
395 mei_hdr = (struct mei_msg_hdr *) &dev->rd_msg_hdr;
396 dev_dbg(&dev->pdev->dev, MEI_HDR_FMT, MEI_HDR_PRM(mei_hdr));
398 if (mei_hdr->reserved || !dev->rd_msg_hdr) {
399 dev_dbg(&dev->pdev->dev, "corrupted message header.\n");
404 if (mei_hdr->host_addr || mei_hdr->me_addr) {
405 list_for_each_entry_safe(cl_pos, cl_next,
406 &dev->file_list, link) {
407 dev_dbg(&dev->pdev->dev,
408 "list_for_each_entry_safe read host"
409 " client = %d, ME client = %d\n",
410 cl_pos->host_client_id,
411 cl_pos->me_client_id);
412 if (mei_cl_hbm_equal(cl_pos, mei_hdr))
416 if (&cl_pos->link == &dev->file_list) {
417 dev_dbg(&dev->pdev->dev, "corrupted message header\n");
422 if (((*slots) * sizeof(u32)) < mei_hdr->length) {
423 dev_err(&dev->pdev->dev,
424 "we can't read the message slots =%08x.\n",
426 /* we can't read the message */
431 /* decide where to read the message too */
432 if (!mei_hdr->host_addr) {
433 dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_bus_message.\n");
434 mei_hbm_dispatch(dev, mei_hdr);
435 dev_dbg(&dev->pdev->dev, "end mei_irq_thread_read_bus_message.\n");
436 } else if (mei_hdr->host_addr == dev->iamthif_cl.host_client_id &&
437 (MEI_FILE_CONNECTED == dev->iamthif_cl.state) &&
438 (dev->iamthif_state == MEI_IAMTHIF_READING)) {
440 dev_dbg(&dev->pdev->dev, "call mei_irq_thread_read_iamthif_message.\n");
441 dev_dbg(&dev->pdev->dev, MEI_HDR_FMT, MEI_HDR_PRM(mei_hdr));
443 ret = mei_amthif_irq_read_msg(dev, mei_hdr, cmpl_list);
447 dev_dbg(&dev->pdev->dev, "call mei_cl_irq_read_msg.\n");
448 dev_dbg(&dev->pdev->dev, MEI_HDR_FMT, MEI_HDR_PRM(mei_hdr));
449 ret = mei_cl_irq_read_msg(dev, mei_hdr, cmpl_list);
454 /* reset the number of slots and header */
455 *slots = mei_count_full_read_slots(dev);
458 if (*slots == -EOVERFLOW) {
459 /* overflow - reset */
460 dev_err(&dev->pdev->dev, "resetting due to slots overflow.\n");
461 /* set the event since message has been read */
468 EXPORT_SYMBOL_GPL(mei_irq_read_handler);
472 * mei_irq_write_handler - dispatch write requests
475 * @dev: the device structure
476 * @cmpl_list: An instance of our list structure
478 * returns 0 on success, <0 on failure.
480 int mei_irq_write_handler(struct mei_device *dev, struct mei_cl_cb *cmpl_list)
484 struct mei_cl_cb *pos = NULL, *next = NULL;
485 struct mei_cl_cb *list;
489 if (!mei_hbuf_is_ready(dev)) {
490 dev_dbg(&dev->pdev->dev, "host buffer is not empty.\n");
493 slots = mei_hbuf_empty_slots(dev);
497 /* complete all waiting for write CB */
498 dev_dbg(&dev->pdev->dev, "complete all waiting for write cb.\n");
500 list = &dev->write_waiting_list;
501 list_for_each_entry_safe(pos, next, &list->list, list) {
507 list_del(&pos->list);
508 if (MEI_WRITING == cl->writing_state &&
509 pos->fop_type == MEI_FOP_WRITE &&
510 cl != &dev->iamthif_cl) {
511 dev_dbg(&dev->pdev->dev, "MEI WRITE COMPLETE\n");
512 cl->writing_state = MEI_WRITE_COMPLETE;
513 list_add_tail(&pos->list, &cmpl_list->list);
515 if (cl == &dev->iamthif_cl) {
516 dev_dbg(&dev->pdev->dev, "check iamthif flow control.\n");
517 if (dev->iamthif_flow_control_pending) {
518 ret = mei_amthif_irq_read(dev, &slots);
525 if (dev->wd_state == MEI_WD_STOPPING) {
526 dev->wd_state = MEI_WD_IDLE;
527 wake_up_interruptible(&dev->wait_stop_wd);
530 if (dev->wr_ext_msg.hdr.length) {
531 mei_write_message(dev, &dev->wr_ext_msg.hdr,
532 dev->wr_ext_msg.data);
533 slots -= mei_data2slots(dev->wr_ext_msg.hdr.length);
534 dev->wr_ext_msg.hdr.length = 0;
536 if (dev->dev_state == MEI_DEV_ENABLED) {
537 if (dev->wd_pending &&
538 mei_cl_flow_ctrl_creds(&dev->wd_cl) > 0) {
539 if (mei_wd_send(dev))
540 dev_dbg(&dev->pdev->dev, "wd send failed.\n");
541 else if (mei_cl_flow_ctrl_reduce(&dev->wd_cl))
544 dev->wd_pending = false;
546 if (dev->wd_state == MEI_WD_RUNNING)
547 slots -= mei_data2slots(MEI_WD_START_MSG_SIZE);
549 slots -= mei_data2slots(MEI_WD_STOP_MSG_SIZE);
553 /* complete control write list CB */
554 dev_dbg(&dev->pdev->dev, "complete control write list cb.\n");
555 list_for_each_entry_safe(pos, next, &dev->ctrl_wr_list.list, list) {
558 list_del(&pos->list);
561 switch (pos->fop_type) {
563 /* send disconnect message */
564 ret = _mei_irq_thread_close(dev, &slots, pos,
571 /* send flow control message */
572 ret = _mei_irq_thread_read(dev, &slots, pos,
579 /* connect message */
580 if (mei_cl_is_other_connecting(cl))
582 ret = _mei_irq_thread_ioctl(dev, &slots, pos,
594 /* complete write list CB */
595 dev_dbg(&dev->pdev->dev, "complete write list cb.\n");
596 list_for_each_entry_safe(pos, next, &dev->write_list.list, list) {
600 if (mei_cl_flow_ctrl_creds(cl) <= 0) {
601 dev_dbg(&dev->pdev->dev,
602 "No flow control credentials for client %d, not sending.\n",
607 if (cl == &dev->iamthif_cl)
608 ret = mei_amthif_irq_write_complete(dev, &slots,
611 ret = mei_irq_thread_write_complete(dev, &slots, pos,
619 EXPORT_SYMBOL_GPL(mei_irq_write_handler);
624 * mei_timer - timer function.
626 * @work: pointer to the work_struct structure
628 * NOTE: This function is called by timer interrupt work
630 void mei_timer(struct work_struct *work)
632 unsigned long timeout;
633 struct mei_cl *cl_pos = NULL;
634 struct mei_cl *cl_next = NULL;
635 struct mei_cl_cb *cb_pos = NULL;
636 struct mei_cl_cb *cb_next = NULL;
638 struct mei_device *dev = container_of(work,
639 struct mei_device, timer_work.work);
642 mutex_lock(&dev->device_lock);
643 if (dev->dev_state != MEI_DEV_ENABLED) {
644 if (dev->dev_state == MEI_DEV_INIT_CLIENTS) {
645 if (dev->init_clients_timer) {
646 if (--dev->init_clients_timer == 0) {
647 dev_err(&dev->pdev->dev, "reset: init clients timeout hbm_state = %d.\n",
655 /*** connect/disconnect timeouts ***/
656 list_for_each_entry_safe(cl_pos, cl_next, &dev->file_list, link) {
657 if (cl_pos->timer_count) {
658 if (--cl_pos->timer_count == 0) {
659 dev_err(&dev->pdev->dev, "reset: connect/disconnect timeout.\n");
666 if (dev->iamthif_stall_timer) {
667 if (--dev->iamthif_stall_timer == 0) {
668 dev_err(&dev->pdev->dev, "reset: amthif hanged.\n");
670 dev->iamthif_msg_buf_size = 0;
671 dev->iamthif_msg_buf_index = 0;
672 dev->iamthif_canceled = false;
673 dev->iamthif_ioctl = true;
674 dev->iamthif_state = MEI_IAMTHIF_IDLE;
675 dev->iamthif_timer = 0;
677 mei_io_cb_free(dev->iamthif_current_cb);
678 dev->iamthif_current_cb = NULL;
680 dev->iamthif_file_object = NULL;
681 mei_amthif_run_next_cmd(dev);
685 if (dev->iamthif_timer) {
687 timeout = dev->iamthif_timer +
688 mei_secs_to_jiffies(MEI_IAMTHIF_READ_TIMER);
690 dev_dbg(&dev->pdev->dev, "dev->iamthif_timer = %ld\n",
692 dev_dbg(&dev->pdev->dev, "timeout = %ld\n", timeout);
693 dev_dbg(&dev->pdev->dev, "jiffies = %ld\n", jiffies);
694 if (time_after(jiffies, timeout)) {
696 * User didn't read the AMTHI data on time (15sec)
697 * freeing AMTHI for other requests
700 dev_dbg(&dev->pdev->dev, "freeing AMTHI for other requests\n");
702 list_for_each_entry_safe(cb_pos, cb_next,
703 &dev->amthif_rd_complete_list.list, list) {
705 cl_pos = cb_pos->file_object->private_data;
707 /* Finding the AMTHI entry. */
708 if (cl_pos == &dev->iamthif_cl)
709 list_del(&cb_pos->list);
711 mei_io_cb_free(dev->iamthif_current_cb);
712 dev->iamthif_current_cb = NULL;
714 dev->iamthif_file_object->private_data = NULL;
715 dev->iamthif_file_object = NULL;
716 dev->iamthif_timer = 0;
717 mei_amthif_run_next_cmd(dev);
722 schedule_delayed_work(&dev->timer_work, 2 * HZ);
723 mutex_unlock(&dev->device_lock);