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); |
Bill Nottingham | 0cfee51 | 2013-04-19 22:01:36 +0300 | [diff] [blame] | 36 | MEI_DEV_STATE(RESETTING); |
Tomas Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 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: |
Masanari Iida | 8b513d0 | 2013-05-21 23:13:12 +0900 | [diff] [blame] | 41 | return "unknown"; |
Tomas Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 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); |
Samuel Ortiz | a7b71bc | 2013-03-27 17:29:56 +0200 | [diff] [blame] | 50 | INIT_LIST_HEAD(&dev->device_list); |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 51 | mutex_init(&dev->device_lock); |
Tomas Winkler | aafae7e | 2013-03-11 18:27:03 +0200 | [diff] [blame] | 52 | init_waitqueue_head(&dev->wait_hw_ready); |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 53 | init_waitqueue_head(&dev->wait_recvd_msg); |
| 54 | init_waitqueue_head(&dev->wait_stop_wd); |
Tomas Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 55 | dev->dev_state = MEI_DEV_INITIALIZING; |
Tomas Winkler | 0288c7c | 2011-06-06 10:44:34 +0300 | [diff] [blame] | 56 | |
| 57 | mei_io_list_init(&dev->read_list); |
| 58 | mei_io_list_init(&dev->write_list); |
| 59 | mei_io_list_init(&dev->write_waiting_list); |
| 60 | mei_io_list_init(&dev->ctrl_wr_list); |
| 61 | mei_io_list_init(&dev->ctrl_rd_list); |
Tomas Winkler | d0265f1 | 2013-03-27 16:58:27 +0200 | [diff] [blame] | 62 | |
| 63 | INIT_DELAYED_WORK(&dev->timer_work, mei_timer); |
| 64 | INIT_WORK(&dev->init_work, mei_host_client_init); |
| 65 | |
| 66 | INIT_LIST_HEAD(&dev->wd_cl.link); |
| 67 | INIT_LIST_HEAD(&dev->iamthif_cl.link); |
| 68 | mei_io_list_init(&dev->amthif_cmd_list); |
| 69 | mei_io_list_init(&dev->amthif_rd_complete_list); |
| 70 | |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 71 | } |
Tomas Winkler | 40e0b67 | 2013-03-27 16:58:30 +0200 | [diff] [blame] | 72 | EXPORT_SYMBOL_GPL(mei_device_init); |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 73 | |
| 74 | /** |
Tomas Winkler | c4d589b | 2013-03-27 16:58:28 +0200 | [diff] [blame] | 75 | * mei_start - initializes host and fw to start work. |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 76 | * |
| 77 | * @dev: the device structure |
| 78 | * |
| 79 | * returns 0 on success, <0 on failure. |
| 80 | */ |
Tomas Winkler | c4d589b | 2013-03-27 16:58:28 +0200 | [diff] [blame] | 81 | int mei_start(struct mei_device *dev) |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 82 | { |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 83 | mutex_lock(&dev->device_lock); |
| 84 | |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 85 | /* acknowledge interrupt and stop interupts */ |
Tomas Winkler | 3a65dd4 | 2012-12-25 19:06:06 +0200 | [diff] [blame] | 86 | mei_clear_interrupts(dev); |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 87 | |
Tomas Winkler | e7e0c23 | 2013-01-08 23:07:31 +0200 | [diff] [blame] | 88 | mei_hw_config(dev); |
Tomas Winkler | 24aadc8 | 2012-06-25 23:46:27 +0300 | [diff] [blame] | 89 | |
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 | |
Tomas Winkler | 9b0d5ef | 2013-04-18 23:03:48 +0300 | [diff] [blame] | 94 | if (mei_hbm_start_wait(dev)) { |
| 95 | dev_err(&dev->pdev->dev, "HBM haven't started"); |
Tomas Winkler | e7e0c23 | 2013-01-08 23:07:31 +0200 | [diff] [blame] | 96 | goto err; |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 97 | } |
| 98 | |
Tomas Winkler | e7e0c23 | 2013-01-08 23:07:31 +0200 | [diff] [blame] | 99 | if (!mei_host_is_ready(dev)) { |
| 100 | dev_err(&dev->pdev->dev, "host is not ready.\n"); |
| 101 | goto err; |
| 102 | } |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 103 | |
Tomas Winkler | 827eef5 | 2013-02-06 14:06:41 +0200 | [diff] [blame] | 104 | if (!mei_hw_is_ready(dev)) { |
Tomas Winkler | e7e0c23 | 2013-01-08 23:07:31 +0200 | [diff] [blame] | 105 | dev_err(&dev->pdev->dev, "ME is not ready.\n"); |
| 106 | goto err; |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 107 | } |
| 108 | |
Tomas Winkler | 2c9b48a | 2013-06-16 09:16:31 +0300 | [diff] [blame] | 109 | if (!mei_hbm_version_is_supported(dev)) { |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 110 | dev_dbg(&dev->pdev->dev, "MEI start failed.\n"); |
Tomas Winkler | e7e0c23 | 2013-01-08 23:07:31 +0200 | [diff] [blame] | 111 | goto err; |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 112 | } |
| 113 | |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 114 | dev_dbg(&dev->pdev->dev, "link layer has been established.\n"); |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 115 | |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 116 | mutex_unlock(&dev->device_lock); |
Tomas Winkler | e7e0c23 | 2013-01-08 23:07:31 +0200 | [diff] [blame] | 117 | return 0; |
| 118 | err: |
| 119 | dev_err(&dev->pdev->dev, "link layer initialization failed.\n"); |
| 120 | dev->dev_state = MEI_DEV_DISABLED; |
| 121 | mutex_unlock(&dev->device_lock); |
| 122 | return -ENODEV; |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 123 | } |
Tomas Winkler | 40e0b67 | 2013-03-27 16:58:30 +0200 | [diff] [blame] | 124 | EXPORT_SYMBOL_GPL(mei_start); |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 125 | |
| 126 | /** |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 127 | * mei_reset - resets host and fw. |
| 128 | * |
| 129 | * @dev: the device structure |
| 130 | * @interrupts_enabled: if interrupt should be enabled after reset. |
| 131 | */ |
| 132 | void mei_reset(struct mei_device *dev, int interrupts_enabled) |
| 133 | { |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 134 | bool unexpected; |
Tomas Winkler | c20c68d | 2013-06-23 10:42:49 +0300 | [diff] [blame] | 135 | int ret; |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 136 | |
Tomas Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 137 | unexpected = (dev->dev_state != MEI_DEV_INITIALIZING && |
| 138 | dev->dev_state != MEI_DEV_DISABLED && |
| 139 | dev->dev_state != MEI_DEV_POWER_DOWN && |
| 140 | dev->dev_state != MEI_DEV_POWER_UP); |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 141 | |
Tomas Winkler | c20c68d | 2013-06-23 10:42:49 +0300 | [diff] [blame] | 142 | ret = mei_hw_reset(dev, interrupts_enabled); |
| 143 | if (ret) { |
| 144 | dev_err(&dev->pdev->dev, "hw reset failed disabling the device\n"); |
| 145 | interrupts_enabled = false; |
| 146 | dev->dev_state = MEI_DEV_DISABLED; |
| 147 | } |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 148 | |
Tomas Winkler | 9b0d5ef | 2013-04-18 23:03:48 +0300 | [diff] [blame] | 149 | dev->hbm_state = MEI_HBM_IDLE; |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 150 | |
Tomas Winkler | 99f22c4 | 2013-07-17 15:13:16 +0300 | [diff] [blame] | 151 | if (dev->dev_state != MEI_DEV_INITIALIZING && |
| 152 | dev->dev_state != MEI_DEV_POWER_UP) { |
Tomas Winkler | b210d75 | 2012-08-07 00:03:56 +0300 | [diff] [blame] | 153 | if (dev->dev_state != MEI_DEV_DISABLED && |
| 154 | dev->dev_state != MEI_DEV_POWER_DOWN) |
Bill Nottingham | 0cfee51 | 2013-04-19 22:01:36 +0300 | [diff] [blame] | 155 | dev->dev_state = MEI_DEV_RESETTING; |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 156 | |
Tomas Winkler | b950ac1 | 2013-07-25 20:15:53 +0300 | [diff] [blame] | 157 | /* remove all waiting requests */ |
| 158 | mei_cl_all_write_clear(dev); |
| 159 | |
Tomas Winkler | 074b4c0 | 2013-02-06 14:06:44 +0200 | [diff] [blame] | 160 | mei_cl_all_disconnect(dev); |
| 161 | |
Tomas Winkler | b950ac1 | 2013-07-25 20:15:53 +0300 | [diff] [blame] | 162 | /* wake up all readings so they can be interrupted */ |
| 163 | mei_cl_all_wakeup(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 | 90e0b5f | 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 | 90e0b5f | 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 | |
Alexander Usyskin | 4a70457 | 2013-09-02 13:29:47 +0300 | [diff] [blame] | 178 | /* we're already in reset, cancel the init timer */ |
| 179 | dev->init_clients_timer = 0; |
| 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 | |
Tomas Winkler | 9049f79 | 2013-06-23 22:49:04 +0300 | [diff] [blame] | 194 | ret = mei_hw_start(dev); |
| 195 | if (ret) { |
| 196 | dev_err(&dev->pdev->dev, "hw_start failed disabling the device\n"); |
| 197 | dev->dev_state = MEI_DEV_DISABLED; |
| 198 | return; |
| 199 | } |
Tomas Winkler | aafae7e | 2013-03-11 18:27:03 +0200 | [diff] [blame] | 200 | |
| 201 | dev_dbg(&dev->pdev->dev, "link is established start sending messages.\n"); |
| 202 | /* link is established * start sending messages. */ |
| 203 | |
| 204 | dev->dev_state = MEI_DEV_INIT_CLIENTS; |
| 205 | |
| 206 | mei_hbm_start_req(dev); |
| 207 | |
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 | |
Samuel Ortiz | 5e85b36 | 2013-06-10 10:10:25 +0300 | [diff] [blame] | 215 | flush_scheduled_work(); |
| 216 | |
Tomas Winkler | 7cb035d | 2013-03-10 13:56:08 +0200 | [diff] [blame] | 217 | mutex_lock(&dev->device_lock); |
| 218 | |
| 219 | cancel_delayed_work(&dev->timer_work); |
| 220 | |
| 221 | mei_wd_stop(dev); |
| 222 | |
Samuel Ortiz | 59fcd7c | 2013-04-11 03:03:29 +0200 | [diff] [blame] | 223 | mei_nfc_host_exit(); |
| 224 | |
Tomas Winkler | 7cb035d | 2013-03-10 13:56:08 +0200 | [diff] [blame] | 225 | dev->dev_state = MEI_DEV_POWER_DOWN; |
| 226 | mei_reset(dev, 0); |
| 227 | |
| 228 | mutex_unlock(&dev->device_lock); |
| 229 | |
Tomas Winkler | 2e64712 | 2013-03-27 16:58:26 +0200 | [diff] [blame] | 230 | mei_watchdog_unregister(dev); |
Tomas Winkler | 7cb035d | 2013-03-10 13:56:08 +0200 | [diff] [blame] | 231 | } |
Tomas Winkler | 40e0b67 | 2013-03-27 16:58:30 +0200 | [diff] [blame] | 232 | EXPORT_SYMBOL_GPL(mei_stop); |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 233 | |
Samuel Ortiz | c1174c0 | 2012-11-18 15:13:20 +0200 | [diff] [blame] | 234 | |
Oren Weil | 91f01c6 | 2011-05-15 13:43:44 +0300 | [diff] [blame] | 235 | |