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 | 90e0b5f1 | 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 | |
| 45 | |
Tomas Winkler | 5bd6471 | 2012-11-18 15:13:14 +0200 | [diff] [blame] | 46 | |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 47 | |
| 48 | /** |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 49 | * init_mei_device - allocates and initializes the mei device structure |
| 50 | * |
| 51 | * @pdev: The pci device structure |
| 52 | * |
| 53 | * returns The mei_device_device pointer on success, NULL on failure. |
| 54 | */ |
Tomas Winkler | c95efb7 | 2011-05-25 17:28:21 +0300 | [diff] [blame] | 55 | struct mei_device *mei_device_init(struct pci_dev *pdev) |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 56 | { |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 57 | struct mei_device *dev; |
| 58 | |
| 59 | dev = kzalloc(sizeof(struct mei_device), GFP_KERNEL); |
| 60 | if (!dev) |
| 61 | return NULL; |
| 62 | |
| 63 | /* setup our list array */ |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 64 | INIT_LIST_HEAD(&dev->file_list); |
| 65 | INIT_LIST_HEAD(&dev->wd_cl.link); |
| 66 | INIT_LIST_HEAD(&dev->iamthif_cl.link); |
| 67 | mutex_init(&dev->device_lock); |
| 68 | init_waitqueue_head(&dev->wait_recvd_msg); |
| 69 | init_waitqueue_head(&dev->wait_stop_wd); |
Tomas Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 70 | dev->dev_state = MEI_DEV_INITIALIZING; |
Tomas Winkler | 0288c7c | 2011-06-06 10:44:34 +0300 | [diff] [blame] | 71 | |
| 72 | mei_io_list_init(&dev->read_list); |
| 73 | mei_io_list_init(&dev->write_list); |
| 74 | mei_io_list_init(&dev->write_waiting_list); |
| 75 | mei_io_list_init(&dev->ctrl_wr_list); |
| 76 | mei_io_list_init(&dev->ctrl_rd_list); |
Tomas Winkler | e773efc | 2012-11-11 17:37:58 +0200 | [diff] [blame] | 77 | mei_io_list_init(&dev->amthif_cmd_list); |
| 78 | mei_io_list_init(&dev->amthif_rd_complete_list); |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 79 | dev->pdev = pdev; |
| 80 | return dev; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * mei_hw_init - initializes host and fw to start work. |
| 85 | * |
| 86 | * @dev: the device structure |
| 87 | * |
| 88 | * returns 0 on success, <0 on failure. |
| 89 | */ |
| 90 | int mei_hw_init(struct mei_device *dev) |
| 91 | { |
| 92 | int err = 0; |
| 93 | int ret; |
| 94 | |
| 95 | mutex_lock(&dev->device_lock); |
| 96 | |
| 97 | dev->host_hw_state = mei_hcsr_read(dev); |
| 98 | dev->me_hw_state = mei_mecsr_read(dev); |
| 99 | dev_dbg(&dev->pdev->dev, "host_hw_state = 0x%08x, mestate = 0x%08x.\n", |
| 100 | dev->host_hw_state, dev->me_hw_state); |
| 101 | |
| 102 | /* acknowledge interrupt and stop interupts */ |
Tomas Winkler | 3a65dd4 | 2012-12-25 19:06:06 +0200 | [diff] [blame] | 103 | mei_clear_interrupts(dev); |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 104 | |
Tomas Winkler | 24aadc8 | 2012-06-25 23:46:27 +0300 | [diff] [blame] | 105 | /* Doesn't change in runtime */ |
| 106 | dev->hbuf_depth = (dev->host_hw_state & H_CBD) >> 24; |
| 107 | |
Tomas Winkler | eb9af0a | 2011-05-25 17:28:22 +0300 | [diff] [blame] | 108 | dev->recvd_msg = false; |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 109 | dev_dbg(&dev->pdev->dev, "reset in start the mei device.\n"); |
| 110 | |
| 111 | mei_reset(dev, 1); |
| 112 | |
| 113 | dev_dbg(&dev->pdev->dev, "host_hw_state = 0x%08x, me_hw_state = 0x%08x.\n", |
| 114 | dev->host_hw_state, dev->me_hw_state); |
| 115 | |
| 116 | /* wait for ME to turn on ME_RDY */ |
| 117 | if (!dev->recvd_msg) { |
| 118 | mutex_unlock(&dev->device_lock); |
| 119 | err = wait_event_interruptible_timeout(dev->wait_recvd_msg, |
Tomas Winkler | 3870c32 | 2012-11-01 21:17:14 +0200 | [diff] [blame] | 120 | dev->recvd_msg, |
| 121 | mei_secs_to_jiffies(MEI_INTEROP_TIMEOUT)); |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 122 | mutex_lock(&dev->device_lock); |
| 123 | } |
| 124 | |
Tomas Winkler | a534bb6 | 2011-06-13 16:39:31 +0300 | [diff] [blame] | 125 | if (err <= 0 && !dev->recvd_msg) { |
Tomas Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 126 | dev->dev_state = MEI_DEV_DISABLED; |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 127 | dev_dbg(&dev->pdev->dev, |
| 128 | "wait_event_interruptible_timeout failed" |
| 129 | "on wait for ME to turn on ME_RDY.\n"); |
| 130 | ret = -ENODEV; |
| 131 | goto out; |
| 132 | } |
| 133 | |
| 134 | if (!(((dev->host_hw_state & H_RDY) == H_RDY) && |
| 135 | ((dev->me_hw_state & ME_RDY_HRA) == ME_RDY_HRA))) { |
Tomas Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 136 | dev->dev_state = MEI_DEV_DISABLED; |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 137 | dev_dbg(&dev->pdev->dev, |
| 138 | "host_hw_state = 0x%08x, me_hw_state = 0x%08x.\n", |
| 139 | dev->host_hw_state, dev->me_hw_state); |
| 140 | |
Dan Carpenter | 8eb73c6 | 2011-06-09 10:18:23 +0300 | [diff] [blame] | 141 | if (!(dev->host_hw_state & H_RDY)) |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 142 | dev_dbg(&dev->pdev->dev, "host turn off H_RDY.\n"); |
| 143 | |
Dan Carpenter | 8eb73c6 | 2011-06-09 10:18:23 +0300 | [diff] [blame] | 144 | if (!(dev->me_hw_state & ME_RDY_HRA)) |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 145 | dev_dbg(&dev->pdev->dev, "ME turn off ME_RDY.\n"); |
| 146 | |
Tomas Winkler | d39a464 | 2012-03-19 17:58:42 +0200 | [diff] [blame] | 147 | dev_err(&dev->pdev->dev, "link layer initialization failed.\n"); |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 148 | ret = -ENODEV; |
| 149 | goto out; |
| 150 | } |
| 151 | |
| 152 | if (dev->version.major_version != HBM_MAJOR_VERSION || |
| 153 | dev->version.minor_version != HBM_MINOR_VERSION) { |
| 154 | dev_dbg(&dev->pdev->dev, "MEI start failed.\n"); |
| 155 | ret = -ENODEV; |
| 156 | goto out; |
| 157 | } |
| 158 | |
Tomas Winkler | eb9af0a | 2011-05-25 17:28:22 +0300 | [diff] [blame] | 159 | dev->recvd_msg = false; |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 160 | dev_dbg(&dev->pdev->dev, "host_hw_state = 0x%08x, me_hw_state = 0x%08x.\n", |
| 161 | dev->host_hw_state, dev->me_hw_state); |
| 162 | dev_dbg(&dev->pdev->dev, "ME turn on ME_RDY and host turn on H_RDY.\n"); |
| 163 | dev_dbg(&dev->pdev->dev, "link layer has been established.\n"); |
| 164 | dev_dbg(&dev->pdev->dev, "MEI start success.\n"); |
| 165 | ret = 0; |
| 166 | |
| 167 | out: |
| 168 | mutex_unlock(&dev->device_lock); |
| 169 | return ret; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * mei_hw_reset - resets fw via mei csr register. |
| 174 | * |
| 175 | * @dev: the device structure |
| 176 | * @interrupts_enabled: if interrupt should be enabled after reset. |
| 177 | */ |
| 178 | static void mei_hw_reset(struct mei_device *dev, int interrupts_enabled) |
| 179 | { |
| 180 | dev->host_hw_state |= (H_RST | H_IG); |
| 181 | |
| 182 | if (interrupts_enabled) |
| 183 | mei_enable_interrupts(dev); |
| 184 | else |
| 185 | mei_disable_interrupts(dev); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * mei_reset - resets host and fw. |
| 190 | * |
| 191 | * @dev: the device structure |
| 192 | * @interrupts_enabled: if interrupt should be enabled after reset. |
| 193 | */ |
| 194 | void mei_reset(struct mei_device *dev, int interrupts_enabled) |
| 195 | { |
| 196 | struct mei_cl *cl_pos = NULL; |
| 197 | struct mei_cl *cl_next = NULL; |
| 198 | struct mei_cl_cb *cb_pos = NULL; |
| 199 | struct mei_cl_cb *cb_next = NULL; |
| 200 | bool unexpected; |
| 201 | |
Tomas Winkler | a9f6b13 | 2013-01-08 23:07:25 +0200 | [diff] [blame^] | 202 | if (dev->dev_state == MEI_DEV_RECOVERING_FROM_RESET) |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 203 | return; |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 204 | |
Tomas Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 205 | unexpected = (dev->dev_state != MEI_DEV_INITIALIZING && |
| 206 | dev->dev_state != MEI_DEV_DISABLED && |
| 207 | dev->dev_state != MEI_DEV_POWER_DOWN && |
| 208 | dev->dev_state != MEI_DEV_POWER_UP); |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 209 | |
| 210 | dev->host_hw_state = mei_hcsr_read(dev); |
| 211 | |
| 212 | dev_dbg(&dev->pdev->dev, "before reset host_hw_state = 0x%08x.\n", |
| 213 | dev->host_hw_state); |
| 214 | |
| 215 | mei_hw_reset(dev, interrupts_enabled); |
| 216 | |
| 217 | dev->host_hw_state &= ~H_RST; |
| 218 | dev->host_hw_state |= H_IG; |
| 219 | |
| 220 | mei_hcsr_set(dev); |
| 221 | |
| 222 | dev_dbg(&dev->pdev->dev, "currently saved host_hw_state = 0x%08x.\n", |
| 223 | dev->host_hw_state); |
| 224 | |
Tomas Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 225 | if (dev->dev_state != MEI_DEV_INITIALIZING) { |
| 226 | if (dev->dev_state != MEI_DEV_DISABLED && |
| 227 | dev->dev_state != MEI_DEV_POWER_DOWN) |
| 228 | dev->dev_state = MEI_DEV_RESETING; |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 229 | |
| 230 | list_for_each_entry_safe(cl_pos, |
| 231 | cl_next, &dev->file_list, link) { |
| 232 | cl_pos->state = MEI_FILE_DISCONNECTED; |
| 233 | cl_pos->mei_flow_ctrl_creds = 0; |
| 234 | cl_pos->read_cb = NULL; |
| 235 | cl_pos->timer_count = 0; |
| 236 | } |
| 237 | /* remove entry if already in list */ |
Tomas Winkler | ff8b2f4 | 2012-11-11 17:38:03 +0200 | [diff] [blame] | 238 | dev_dbg(&dev->pdev->dev, "remove iamthif and wd from the file list.\n"); |
Tomas Winkler | 90e0b5f1 | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 239 | mei_cl_unlink(&dev->wd_cl); |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 240 | if (dev->open_handle_count > 0) |
| 241 | dev->open_handle_count--; |
Tomas Winkler | 90e0b5f1 | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 242 | mei_cl_unlink(&dev->iamthif_cl); |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 243 | if (dev->open_handle_count > 0) |
| 244 | dev->open_handle_count--; |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 245 | |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 246 | mei_amthif_reset_params(dev); |
Tomas Winkler | 5fb54fb | 2012-11-18 15:13:15 +0200 | [diff] [blame] | 247 | memset(&dev->wr_ext_msg, 0, sizeof(dev->wr_ext_msg)); |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 248 | } |
| 249 | |
Tomas Winkler | cf9673d | 2011-06-06 10:44:33 +0300 | [diff] [blame] | 250 | dev->me_clients_num = 0; |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 251 | dev->rd_msg_hdr = 0; |
Tomas Winkler | eb9af0a | 2011-05-25 17:28:22 +0300 | [diff] [blame] | 252 | dev->wd_pending = false; |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 253 | |
| 254 | /* update the state of the registers after reset */ |
| 255 | dev->host_hw_state = mei_hcsr_read(dev); |
| 256 | dev->me_hw_state = mei_mecsr_read(dev); |
| 257 | |
| 258 | dev_dbg(&dev->pdev->dev, "after reset host_hw_state = 0x%08x, me_hw_state = 0x%08x.\n", |
| 259 | dev->host_hw_state, dev->me_hw_state); |
| 260 | |
| 261 | if (unexpected) |
Tomas Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 262 | dev_warn(&dev->pdev->dev, "unexpected reset: dev_state = %s\n", |
| 263 | mei_dev_state_str(dev->dev_state)); |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 264 | |
| 265 | /* Wake up all readings so they can be interrupted */ |
| 266 | list_for_each_entry_safe(cl_pos, cl_next, &dev->file_list, link) { |
| 267 | if (waitqueue_active(&cl_pos->rx_wait)) { |
| 268 | dev_dbg(&dev->pdev->dev, "Waking up client!\n"); |
| 269 | wake_up_interruptible(&cl_pos->rx_wait); |
| 270 | } |
| 271 | } |
| 272 | /* remove all waiting requests */ |
Tomas Winkler | fb601ad | 2012-10-15 12:06:48 +0200 | [diff] [blame] | 273 | list_for_each_entry_safe(cb_pos, cb_next, &dev->write_list.list, list) { |
| 274 | list_del(&cb_pos->list); |
Tomas Winkler | 601a1ef | 2012-10-09 16:50:20 +0200 | [diff] [blame] | 275 | mei_io_cb_free(cb_pos); |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | |
| 279 | |
Samuel Ortiz | c1174c0 | 2012-11-18 15:13:20 +0200 | [diff] [blame] | 280 | |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 281 | |