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 | |
Tomas Winkler | 40e0b67 | 2013-03-27 16:58:30 +0200 | [diff] [blame^] | 17 | #include <linux/export.h> |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 18 | #include <linux/pci.h> |
| 19 | #include <linux/sched.h> |
| 20 | #include <linux/wait.h> |
| 21 | #include <linux/delay.h> |
| 22 | |
Tomas Winkler | 4f3afe1 | 2012-05-09 16:38:59 +0300 | [diff] [blame] | 23 | #include <linux/mei.h> |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 24 | |
Tomas Winkler | 47a7380 | 2012-12-25 19:06:03 +0200 | [diff] [blame] | 25 | #include "mei_dev.h" |
Tomas Winkler | aafae7e | 2013-03-11 18:27:03 +0200 | [diff] [blame] | 26 | #include "hbm.h" |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 27 | #include "client.h" |
Tomas Winkler | 47a7380 | 2012-12-25 19:06:03 +0200 | [diff] [blame] | 28 | |
Tomas Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 29 | const char *mei_dev_state_str(int state) |
| 30 | { |
| 31 | #define MEI_DEV_STATE(state) case MEI_DEV_##state: return #state |
| 32 | switch (state) { |
| 33 | MEI_DEV_STATE(INITIALIZING); |
| 34 | MEI_DEV_STATE(INIT_CLIENTS); |
| 35 | MEI_DEV_STATE(ENABLED); |
| 36 | MEI_DEV_STATE(RESETING); |
| 37 | MEI_DEV_STATE(DISABLED); |
Tomas Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 38 | MEI_DEV_STATE(POWER_DOWN); |
| 39 | MEI_DEV_STATE(POWER_UP); |
| 40 | default: |
| 41 | return "unkown"; |
| 42 | } |
| 43 | #undef MEI_DEV_STATE |
| 44 | } |
| 45 | |
Tomas Winkler | 52c3456 | 2013-02-06 14:06:40 +0200 | [diff] [blame] | 46 | void mei_device_init(struct mei_device *dev) |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 47 | { |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 48 | /* setup our list array */ |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 49 | INIT_LIST_HEAD(&dev->file_list); |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 50 | mutex_init(&dev->device_lock); |
Tomas Winkler | aafae7e | 2013-03-11 18:27:03 +0200 | [diff] [blame] | 51 | init_waitqueue_head(&dev->wait_hw_ready); |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 52 | init_waitqueue_head(&dev->wait_recvd_msg); |
| 53 | init_waitqueue_head(&dev->wait_stop_wd); |
Tomas Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 54 | dev->dev_state = MEI_DEV_INITIALIZING; |
Tomas Winkler | 0288c7c | 2011-06-06 10:44:34 +0300 | [diff] [blame] | 55 | |
| 56 | mei_io_list_init(&dev->read_list); |
| 57 | mei_io_list_init(&dev->write_list); |
| 58 | mei_io_list_init(&dev->write_waiting_list); |
| 59 | mei_io_list_init(&dev->ctrl_wr_list); |
| 60 | mei_io_list_init(&dev->ctrl_rd_list); |
Tomas Winkler | d0265f1 | 2013-03-27 16:58:27 +0200 | [diff] [blame] | 61 | |
| 62 | INIT_DELAYED_WORK(&dev->timer_work, mei_timer); |
| 63 | INIT_WORK(&dev->init_work, mei_host_client_init); |
| 64 | |
| 65 | INIT_LIST_HEAD(&dev->wd_cl.link); |
| 66 | INIT_LIST_HEAD(&dev->iamthif_cl.link); |
| 67 | mei_io_list_init(&dev->amthif_cmd_list); |
| 68 | mei_io_list_init(&dev->amthif_rd_complete_list); |
| 69 | |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 70 | } |
Tomas Winkler | 40e0b67 | 2013-03-27 16:58:30 +0200 | [diff] [blame^] | 71 | EXPORT_SYMBOL_GPL(mei_device_init); |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 72 | |
| 73 | /** |
Tomas Winkler | c4d589b | 2013-03-27 16:58:28 +0200 | [diff] [blame] | 74 | * mei_start - initializes host and fw to start work. |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 75 | * |
| 76 | * @dev: the device structure |
| 77 | * |
| 78 | * returns 0 on success, <0 on failure. |
| 79 | */ |
Tomas Winkler | c4d589b | 2013-03-27 16:58:28 +0200 | [diff] [blame] | 80 | int mei_start(struct mei_device *dev) |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 81 | { |
Tomas Winkler | e7e0c23 | 2013-01-08 23:07:31 +0200 | [diff] [blame] | 82 | int ret = 0; |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 83 | |
| 84 | mutex_lock(&dev->device_lock); |
| 85 | |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 86 | /* acknowledge interrupt and stop interupts */ |
Tomas Winkler | 3a65dd4 | 2012-12-25 19:06:06 +0200 | [diff] [blame] | 87 | mei_clear_interrupts(dev); |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 88 | |
Tomas Winkler | e7e0c23 | 2013-01-08 23:07:31 +0200 | [diff] [blame] | 89 | mei_hw_config(dev); |
Tomas Winkler | 24aadc8 | 2012-06-25 23:46:27 +0300 | [diff] [blame] | 90 | |
Tomas Winkler | eb9af0a | 2011-05-25 17:28:22 +0300 | [diff] [blame] | 91 | dev->recvd_msg = false; |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 92 | dev_dbg(&dev->pdev->dev, "reset in start the mei device.\n"); |
| 93 | |
| 94 | mei_reset(dev, 1); |
| 95 | |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 96 | /* wait for ME to turn on ME_RDY */ |
| 97 | if (!dev->recvd_msg) { |
| 98 | mutex_unlock(&dev->device_lock); |
Tomas Winkler | e7e0c23 | 2013-01-08 23:07:31 +0200 | [diff] [blame] | 99 | ret = wait_event_interruptible_timeout(dev->wait_recvd_msg, |
Tomas Winkler | 3870c32 | 2012-11-01 21:17:14 +0200 | [diff] [blame] | 100 | dev->recvd_msg, |
| 101 | mei_secs_to_jiffies(MEI_INTEROP_TIMEOUT)); |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 102 | mutex_lock(&dev->device_lock); |
| 103 | } |
| 104 | |
Tomas Winkler | e7e0c23 | 2013-01-08 23:07:31 +0200 | [diff] [blame] | 105 | if (ret <= 0 && !dev->recvd_msg) { |
Tomas Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 106 | dev->dev_state = MEI_DEV_DISABLED; |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 107 | dev_dbg(&dev->pdev->dev, |
| 108 | "wait_event_interruptible_timeout failed" |
| 109 | "on wait for ME to turn on ME_RDY.\n"); |
Tomas Winkler | e7e0c23 | 2013-01-08 23:07:31 +0200 | [diff] [blame] | 110 | goto err; |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 111 | } |
| 112 | |
Tomas Winkler | 115ba28 | 2013-01-08 23:07:29 +0200 | [diff] [blame] | 113 | |
Tomas Winkler | e7e0c23 | 2013-01-08 23:07:31 +0200 | [diff] [blame] | 114 | if (!mei_host_is_ready(dev)) { |
| 115 | dev_err(&dev->pdev->dev, "host is not ready.\n"); |
| 116 | goto err; |
| 117 | } |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 118 | |
Tomas Winkler | 827eef5 | 2013-02-06 14:06:41 +0200 | [diff] [blame] | 119 | if (!mei_hw_is_ready(dev)) { |
Tomas Winkler | e7e0c23 | 2013-01-08 23:07:31 +0200 | [diff] [blame] | 120 | dev_err(&dev->pdev->dev, "ME is not ready.\n"); |
| 121 | goto err; |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | if (dev->version.major_version != HBM_MAJOR_VERSION || |
| 125 | dev->version.minor_version != HBM_MINOR_VERSION) { |
| 126 | dev_dbg(&dev->pdev->dev, "MEI start failed.\n"); |
Tomas Winkler | e7e0c23 | 2013-01-08 23:07:31 +0200 | [diff] [blame] | 127 | goto err; |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 128 | } |
| 129 | |
Tomas Winkler | eb9af0a | 2011-05-25 17:28:22 +0300 | [diff] [blame] | 130 | dev->recvd_msg = false; |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 131 | dev_dbg(&dev->pdev->dev, "link layer has been established.\n"); |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 132 | |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 133 | mutex_unlock(&dev->device_lock); |
Tomas Winkler | e7e0c23 | 2013-01-08 23:07:31 +0200 | [diff] [blame] | 134 | return 0; |
| 135 | err: |
| 136 | dev_err(&dev->pdev->dev, "link layer initialization failed.\n"); |
| 137 | dev->dev_state = MEI_DEV_DISABLED; |
| 138 | mutex_unlock(&dev->device_lock); |
| 139 | return -ENODEV; |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 140 | } |
Tomas Winkler | 40e0b67 | 2013-03-27 16:58:30 +0200 | [diff] [blame^] | 141 | EXPORT_SYMBOL_GPL(mei_start); |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 142 | |
| 143 | /** |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 144 | * mei_reset - resets host and fw. |
| 145 | * |
| 146 | * @dev: the device structure |
| 147 | * @interrupts_enabled: if interrupt should be enabled after reset. |
| 148 | */ |
| 149 | void mei_reset(struct mei_device *dev, int interrupts_enabled) |
| 150 | { |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 151 | bool unexpected; |
| 152 | |
Tomas Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 153 | unexpected = (dev->dev_state != MEI_DEV_INITIALIZING && |
| 154 | dev->dev_state != MEI_DEV_DISABLED && |
| 155 | dev->dev_state != MEI_DEV_POWER_DOWN && |
| 156 | dev->dev_state != MEI_DEV_POWER_UP); |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 157 | |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 158 | mei_hw_reset(dev, interrupts_enabled); |
| 159 | |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 160 | |
Tomas Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 161 | if (dev->dev_state != MEI_DEV_INITIALIZING) { |
| 162 | if (dev->dev_state != MEI_DEV_DISABLED && |
| 163 | dev->dev_state != MEI_DEV_POWER_DOWN) |
| 164 | dev->dev_state = MEI_DEV_RESETING; |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 165 | |
Tomas Winkler | 074b4c0 | 2013-02-06 14:06:44 +0200 | [diff] [blame] | 166 | mei_cl_all_disconnect(dev); |
| 167 | |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 168 | /* remove entry if already in list */ |
Tomas Winkler | ff8b2f4 | 2012-11-11 17:38:03 +0200 | [diff] [blame] | 169 | 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] | 170 | mei_cl_unlink(&dev->wd_cl); |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 171 | if (dev->open_handle_count > 0) |
| 172 | dev->open_handle_count--; |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 173 | mei_cl_unlink(&dev->iamthif_cl); |
Tomas Winkler | 781d0d8 | 2013-01-08 23:07:22 +0200 | [diff] [blame] | 174 | if (dev->open_handle_count > 0) |
| 175 | dev->open_handle_count--; |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 176 | |
Tomas Winkler | 19838fb | 2012-11-01 21:17:15 +0200 | [diff] [blame] | 177 | mei_amthif_reset_params(dev); |
Tomas Winkler | 5fb54fb | 2012-11-18 15:13:15 +0200 | [diff] [blame] | 178 | memset(&dev->wr_ext_msg, 0, sizeof(dev->wr_ext_msg)); |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 179 | } |
| 180 | |
Tomas Winkler | cf9673d | 2011-06-06 10:44:33 +0300 | [diff] [blame] | 181 | dev->me_clients_num = 0; |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 182 | dev->rd_msg_hdr = 0; |
Tomas Winkler | eb9af0a | 2011-05-25 17:28:22 +0300 | [diff] [blame] | 183 | dev->wd_pending = false; |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 184 | |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 185 | if (unexpected) |
Tomas Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 186 | dev_warn(&dev->pdev->dev, "unexpected reset: dev_state = %s\n", |
| 187 | mei_dev_state_str(dev->dev_state)); |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 188 | |
Tomas Winkler | aafae7e | 2013-03-11 18:27:03 +0200 | [diff] [blame] | 189 | if (!interrupts_enabled) { |
| 190 | dev_dbg(&dev->pdev->dev, "intr not enabled end of reset\n"); |
| 191 | return; |
| 192 | } |
| 193 | |
| 194 | mei_hw_start(dev); |
| 195 | |
| 196 | dev_dbg(&dev->pdev->dev, "link is established start sending messages.\n"); |
| 197 | /* link is established * start sending messages. */ |
| 198 | |
| 199 | dev->dev_state = MEI_DEV_INIT_CLIENTS; |
| 200 | |
| 201 | mei_hbm_start_req(dev); |
| 202 | |
Tomas Winkler | 074b4c0 | 2013-02-06 14:06:44 +0200 | [diff] [blame] | 203 | /* wake up all readings so they can be interrupted */ |
| 204 | mei_cl_all_read_wakeup(dev); |
| 205 | |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 206 | /* remove all waiting requests */ |
Tomas Winkler | 074b4c0 | 2013-02-06 14:06:44 +0200 | [diff] [blame] | 207 | mei_cl_all_write_clear(dev); |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 208 | } |
Tomas Winkler | 40e0b67 | 2013-03-27 16:58:30 +0200 | [diff] [blame^] | 209 | EXPORT_SYMBOL_GPL(mei_reset); |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 210 | |
Tomas Winkler | 7cb035d | 2013-03-10 13:56:08 +0200 | [diff] [blame] | 211 | void mei_stop(struct mei_device *dev) |
| 212 | { |
| 213 | dev_dbg(&dev->pdev->dev, "stopping the device.\n"); |
| 214 | |
| 215 | mutex_lock(&dev->device_lock); |
| 216 | |
| 217 | cancel_delayed_work(&dev->timer_work); |
| 218 | |
| 219 | mei_wd_stop(dev); |
| 220 | |
| 221 | dev->dev_state = MEI_DEV_POWER_DOWN; |
| 222 | mei_reset(dev, 0); |
| 223 | |
| 224 | mutex_unlock(&dev->device_lock); |
| 225 | |
| 226 | flush_scheduled_work(); |
Tomas Winkler | 2e64712 | 2013-03-27 16:58:26 +0200 | [diff] [blame] | 227 | |
| 228 | mei_watchdog_unregister(dev); |
Tomas Winkler | 7cb035d | 2013-03-10 13:56:08 +0200 | [diff] [blame] | 229 | } |
Tomas Winkler | 40e0b67 | 2013-03-27 16:58:30 +0200 | [diff] [blame^] | 230 | EXPORT_SYMBOL_GPL(mei_stop); |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 231 | |
Samuel Ortiz | c1174c0 | 2012-11-18 15:13:20 +0200 | [diff] [blame] | 232 | |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 233 | |