Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Intel Management Engine Interface (Intel MEI) Linux driver |
Tomas Winkler | 733ba91 | 2012-02-09 19:25:53 +0200 | [diff] [blame] | 4 | * Copyright (c) 2003-2012, Intel Corporation. |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 5 | * |
| 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 | #include <linux/pci.h> |
| 18 | #include <linux/sched.h> |
| 19 | #include <linux/wait.h> |
| 20 | #include <linux/delay.h> |
| 21 | |
Tomas Winkler | 4f3afe1 | 2012-05-09 16:38:59 +0300 | [diff] [blame] | 22 | #include <linux/mei.h> |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 23 | |
Tomas Winkler | 47a7380 | 2012-12-25 19:06:03 +0200 | [diff] [blame] | 24 | #include "mei_dev.h" |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 25 | #include "client.h" |
Tomas Winkler | 47a7380 | 2012-12-25 19:06:03 +0200 | [diff] [blame] | 26 | |
Tomas Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 27 | const char *mei_dev_state_str(int state) |
| 28 | { |
| 29 | #define MEI_DEV_STATE(state) case MEI_DEV_##state: return #state |
| 30 | switch (state) { |
| 31 | MEI_DEV_STATE(INITIALIZING); |
| 32 | MEI_DEV_STATE(INIT_CLIENTS); |
| 33 | MEI_DEV_STATE(ENABLED); |
| 34 | MEI_DEV_STATE(RESETING); |
| 35 | MEI_DEV_STATE(DISABLED); |
| 36 | MEI_DEV_STATE(RECOVERING_FROM_RESET); |
| 37 | MEI_DEV_STATE(POWER_DOWN); |
| 38 | MEI_DEV_STATE(POWER_UP); |
| 39 | default: |
| 40 | return "unkown"; |
| 41 | } |
| 42 | #undef MEI_DEV_STATE |
| 43 | } |
| 44 | |
Tomas Winkler | 52c3456 | 2013-02-06 14:06:40 +0200 | [diff] [blame^] | 45 | void mei_device_init(struct mei_device *dev) |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 46 | { |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 47 | /* setup our list array */ |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 48 | INIT_LIST_HEAD(&dev->file_list); |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 49 | mutex_init(&dev->device_lock); |
| 50 | init_waitqueue_head(&dev->wait_recvd_msg); |
| 51 | init_waitqueue_head(&dev->wait_stop_wd); |
Tomas Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 52 | dev->dev_state = MEI_DEV_INITIALIZING; |
Tomas Winkler | 0288c7c | 2011-06-06 10:44:34 +0300 | [diff] [blame] | 53 | |
| 54 | mei_io_list_init(&dev->read_list); |
| 55 | mei_io_list_init(&dev->write_list); |
| 56 | mei_io_list_init(&dev->write_waiting_list); |
| 57 | mei_io_list_init(&dev->ctrl_wr_list); |
| 58 | mei_io_list_init(&dev->ctrl_rd_list); |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | /** |
| 62 | * mei_hw_init - initializes host and fw to start work. |
| 63 | * |
| 64 | * @dev: the device structure |
| 65 | * |
| 66 | * returns 0 on success, <0 on failure. |
| 67 | */ |
| 68 | int mei_hw_init(struct mei_device *dev) |
| 69 | { |
Tomas Winkler | e7e0c23 | 2013-01-08 23:07:31 +0200 | [diff] [blame] | 70 | int ret = 0; |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 71 | |
| 72 | mutex_lock(&dev->device_lock); |
| 73 | |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 74 | /* acknowledge interrupt and stop interupts */ |
Tomas Winkler | 3a65dd4 | 2012-12-25 19:06:06 +0200 | [diff] [blame] | 75 | mei_clear_interrupts(dev); |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 76 | |
Tomas Winkler | e7e0c23 | 2013-01-08 23:07:31 +0200 | [diff] [blame] | 77 | mei_hw_config(dev); |
Tomas Winkler | 24aadc8 | 2012-06-25 23:46:27 +0300 | [diff] [blame] | 78 | |
Tomas Winkler | eb9af0a | 2011-05-25 17:28:22 +0300 | [diff] [blame] | 79 | dev->recvd_msg = false; |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 80 | dev_dbg(&dev->pdev->dev, "reset in start the mei device.\n"); |
| 81 | |
| 82 | mei_reset(dev, 1); |
| 83 | |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 84 | /* wait for ME to turn on ME_RDY */ |
| 85 | if (!dev->recvd_msg) { |
| 86 | mutex_unlock(&dev->device_lock); |
Tomas Winkler | e7e0c23 | 2013-01-08 23:07:31 +0200 | [diff] [blame] | 87 | ret = wait_event_interruptible_timeout(dev->wait_recvd_msg, |
Tomas Winkler | 3870c32 | 2012-11-01 21:17:14 +0200 | [diff] [blame] | 88 | dev->recvd_msg, |
| 89 | mei_secs_to_jiffies(MEI_INTEROP_TIMEOUT)); |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 90 | mutex_lock(&dev->device_lock); |
| 91 | } |
| 92 | |
Tomas Winkler | e7e0c23 | 2013-01-08 23:07:31 +0200 | [diff] [blame] | 93 | if (ret <= 0 && !dev->recvd_msg) { |
Tomas Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 94 | dev->dev_state = MEI_DEV_DISABLED; |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 95 | dev_dbg(&dev->pdev->dev, |
| 96 | "wait_event_interruptible_timeout failed" |
| 97 | "on wait for ME to turn on ME_RDY.\n"); |
Tomas Winkler | e7e0c23 | 2013-01-08 23:07:31 +0200 | [diff] [blame] | 98 | goto err; |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 99 | } |
| 100 | |
Tomas Winkler | 115ba28 | 2013-01-08 23:07:29 +0200 | [diff] [blame] | 101 | |
Tomas Winkler | e7e0c23 | 2013-01-08 23:07:31 +0200 | [diff] [blame] | 102 | if (!mei_host_is_ready(dev)) { |
| 103 | dev_err(&dev->pdev->dev, "host is not ready.\n"); |
| 104 | goto err; |
| 105 | } |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 106 | |
Tomas Winkler | e7e0c23 | 2013-01-08 23:07:31 +0200 | [diff] [blame] | 107 | if (!mei_me_is_ready(dev)) { |
| 108 | dev_err(&dev->pdev->dev, "ME is not ready.\n"); |
| 109 | goto err; |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | if (dev->version.major_version != HBM_MAJOR_VERSION || |
| 113 | dev->version.minor_version != HBM_MINOR_VERSION) { |
| 114 | dev_dbg(&dev->pdev->dev, "MEI start failed.\n"); |
Tomas Winkler | e7e0c23 | 2013-01-08 23:07:31 +0200 | [diff] [blame] | 115 | goto err; |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 116 | } |
| 117 | |
Tomas Winkler | eb9af0a | 2011-05-25 17:28:22 +0300 | [diff] [blame] | 118 | dev->recvd_msg = false; |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 119 | dev_dbg(&dev->pdev->dev, "link layer has been established.\n"); |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 120 | |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 121 | mutex_unlock(&dev->device_lock); |
Tomas Winkler | e7e0c23 | 2013-01-08 23:07:31 +0200 | [diff] [blame] | 122 | return 0; |
| 123 | err: |
| 124 | dev_err(&dev->pdev->dev, "link layer initialization failed.\n"); |
| 125 | dev->dev_state = MEI_DEV_DISABLED; |
| 126 | mutex_unlock(&dev->device_lock); |
| 127 | return -ENODEV; |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | /** |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 131 | * mei_reset - resets host and fw. |
| 132 | * |
| 133 | * @dev: the device structure |
| 134 | * @interrupts_enabled: if interrupt should be enabled after reset. |
| 135 | */ |
| 136 | void mei_reset(struct mei_device *dev, int interrupts_enabled) |
| 137 | { |
| 138 | struct mei_cl *cl_pos = NULL; |
| 139 | struct mei_cl *cl_next = NULL; |
| 140 | struct mei_cl_cb *cb_pos = NULL; |
| 141 | struct mei_cl_cb *cb_next = NULL; |
| 142 | bool unexpected; |
| 143 | |
Tomas Winkler | a9f6b13 | 2013-01-08 23:07:25 +0200 | [diff] [blame] | 144 | if (dev->dev_state == MEI_DEV_RECOVERING_FROM_RESET) |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 145 | return; |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 146 | |
Tomas Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 147 | unexpected = (dev->dev_state != MEI_DEV_INITIALIZING && |
| 148 | dev->dev_state != MEI_DEV_DISABLED && |
| 149 | dev->dev_state != MEI_DEV_POWER_DOWN && |
| 150 | dev->dev_state != MEI_DEV_POWER_UP); |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 151 | |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 152 | mei_hw_reset(dev, interrupts_enabled); |
| 153 | |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 154 | |
Tomas Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 155 | if (dev->dev_state != MEI_DEV_INITIALIZING) { |
| 156 | if (dev->dev_state != MEI_DEV_DISABLED && |
| 157 | dev->dev_state != MEI_DEV_POWER_DOWN) |
| 158 | dev->dev_state = MEI_DEV_RESETING; |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 159 | |
| 160 | list_for_each_entry_safe(cl_pos, |
| 161 | cl_next, &dev->file_list, link) { |
| 162 | cl_pos->state = MEI_FILE_DISCONNECTED; |
| 163 | cl_pos->mei_flow_ctrl_creds = 0; |
| 164 | cl_pos->read_cb = NULL; |
| 165 | cl_pos->timer_count = 0; |
| 166 | } |
| 167 | /* remove entry if already in list */ |
Tomas Winkler | ff8b2f4 | 2012-11-11 17:38:03 +0200 | [diff] [blame] | 168 | dev_dbg(&dev->pdev->dev, "remove iamthif and wd from the file list.\n"); |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 169 | mei_cl_unlink(&dev->wd_cl); |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 170 | if (dev->open_handle_count > 0) |
| 171 | dev->open_handle_count--; |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 172 | mei_cl_unlink(&dev->iamthif_cl); |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 173 | if (dev->open_handle_count > 0) |
| 174 | dev->open_handle_count--; |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 175 | |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 176 | mei_amthif_reset_params(dev); |
Tomas Winkler | 5fb54fb | 2012-11-18 15:13:15 +0200 | [diff] [blame] | 177 | memset(&dev->wr_ext_msg, 0, sizeof(dev->wr_ext_msg)); |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 178 | } |
| 179 | |
Tomas Winkler | cf9673d | 2011-06-06 10:44:33 +0300 | [diff] [blame] | 180 | dev->me_clients_num = 0; |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 181 | dev->rd_msg_hdr = 0; |
Tomas Winkler | eb9af0a | 2011-05-25 17:28:22 +0300 | [diff] [blame] | 182 | dev->wd_pending = false; |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 183 | |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 184 | if (unexpected) |
Tomas Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 185 | dev_warn(&dev->pdev->dev, "unexpected reset: dev_state = %s\n", |
| 186 | mei_dev_state_str(dev->dev_state)); |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 187 | |
| 188 | /* Wake up all readings so they can be interrupted */ |
| 189 | list_for_each_entry_safe(cl_pos, cl_next, &dev->file_list, link) { |
| 190 | if (waitqueue_active(&cl_pos->rx_wait)) { |
| 191 | dev_dbg(&dev->pdev->dev, "Waking up client!\n"); |
| 192 | wake_up_interruptible(&cl_pos->rx_wait); |
| 193 | } |
| 194 | } |
| 195 | /* remove all waiting requests */ |
Tomas Winkler | fb601ad | 2012-10-15 12:06:48 +0200 | [diff] [blame] | 196 | list_for_each_entry_safe(cb_pos, cb_next, &dev->write_list.list, list) { |
| 197 | list_del(&cb_pos->list); |
Tomas Winkler | 601a1ef | 2012-10-09 16:50:20 +0200 | [diff] [blame] | 198 | mei_io_cb_free(cb_pos); |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | |
| 202 | |
Samuel Ortiz | c1174c0 | 2012-11-18 15:13:20 +0200 | [diff] [blame] | 203 | |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 204 | |