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