1 /* 2 * 3 * Intel Management Engine Interface (Intel MEI) Linux driver 4 * Copyright (c) 2003-2012, Intel Corporation. 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/export.h> 18 #include <linux/sched.h> 19 #include <linux/wait.h> 20 #include <linux/delay.h> 21 22 #include <linux/mei.h> 23 24 #include "mei_dev.h" 25 #include "hbm.h" 26 #include "client.h" 27 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(RESETTING); 36 MEI_DEV_STATE(DISABLED); 37 MEI_DEV_STATE(POWER_DOWN); 38 MEI_DEV_STATE(POWER_UP); 39 default: 40 return "unknown"; 41 } 42 #undef MEI_DEV_STATE 43 } 44 45 const char *mei_pg_state_str(enum mei_pg_state state) 46 { 47 #define MEI_PG_STATE(state) case MEI_PG_##state: return #state 48 switch (state) { 49 MEI_PG_STATE(OFF); 50 MEI_PG_STATE(ON); 51 default: 52 return "unknown"; 53 } 54 #undef MEI_PG_STATE 55 } 56 57 58 /** 59 * mei_cancel_work - Cancel mei background jobs 60 * 61 * @dev: the device structure 62 */ 63 void mei_cancel_work(struct mei_device *dev) 64 { 65 cancel_work_sync(&dev->init_work); 66 cancel_work_sync(&dev->reset_work); 67 68 cancel_delayed_work(&dev->timer_work); 69 } 70 EXPORT_SYMBOL_GPL(mei_cancel_work); 71 72 /** 73 * mei_reset - resets host and fw. 74 * 75 * @dev: the device structure 76 * 77 * Return: 0 on success or < 0 if the reset hasn't succeeded 78 */ 79 int mei_reset(struct mei_device *dev) 80 { 81 enum mei_dev_state state = dev->dev_state; 82 bool interrupts_enabled; 83 int ret; 84 85 if (state != MEI_DEV_INITIALIZING && 86 state != MEI_DEV_DISABLED && 87 state != MEI_DEV_POWER_DOWN && 88 state != MEI_DEV_POWER_UP) { 89 struct mei_fw_status fw_status; 90 91 mei_fw_status(dev, &fw_status); 92 dev_warn(dev->dev, 93 "unexpected reset: dev_state = %s " FW_STS_FMT "\n", 94 mei_dev_state_str(state), FW_STS_PRM(fw_status)); 95 } 96 97 /* we're already in reset, cancel the init timer 98 * if the reset was called due the hbm protocol error 99 * we need to call it before hw start 100 * so the hbm watchdog won't kick in 101 */ 102 mei_hbm_idle(dev); 103 104 /* enter reset flow */ 105 interrupts_enabled = state != MEI_DEV_POWER_DOWN; 106 dev->dev_state = MEI_DEV_RESETTING; 107 108 dev->reset_count++; 109 if (dev->reset_count > MEI_MAX_CONSEC_RESET) { 110 dev_err(dev->dev, "reset: reached maximal consecutive resets: disabling the device\n"); 111 dev->dev_state = MEI_DEV_DISABLED; 112 return -ENODEV; 113 } 114 115 ret = mei_hw_reset(dev, interrupts_enabled); 116 /* fall through and remove the sw state even if hw reset has failed */ 117 118 /* no need to clean up software state in case of power up */ 119 if (state != MEI_DEV_INITIALIZING && 120 state != MEI_DEV_POWER_UP) { 121 122 /* remove all waiting requests */ 123 mei_cl_all_write_clear(dev); 124 125 mei_cl_all_disconnect(dev); 126 127 /* wake up all readers and writers so they can be interrupted */ 128 mei_cl_all_wakeup(dev); 129 130 /* remove entry if already in list */ 131 dev_dbg(dev->dev, "remove iamthif and wd from the file list.\n"); 132 mei_cl_unlink(&dev->wd_cl); 133 mei_cl_unlink(&dev->iamthif_cl); 134 mei_amthif_reset_params(dev); 135 } 136 137 mei_hbm_reset(dev); 138 139 dev->rd_msg_hdr = 0; 140 dev->wd_pending = false; 141 142 if (ret) { 143 dev_err(dev->dev, "hw_reset failed ret = %d\n", ret); 144 return ret; 145 } 146 147 if (state == MEI_DEV_POWER_DOWN) { 148 dev_dbg(dev->dev, "powering down: end of reset\n"); 149 dev->dev_state = MEI_DEV_DISABLED; 150 return 0; 151 } 152 153 ret = mei_hw_start(dev); 154 if (ret) { 155 dev_err(dev->dev, "hw_start failed ret = %d\n", ret); 156 return ret; 157 } 158 159 dev_dbg(dev->dev, "link is established start sending messages.\n"); 160 161 dev->dev_state = MEI_DEV_INIT_CLIENTS; 162 ret = mei_hbm_start_req(dev); 163 if (ret) { 164 dev_err(dev->dev, "hbm_start failed ret = %d\n", ret); 165 dev->dev_state = MEI_DEV_RESETTING; 166 return ret; 167 } 168 169 return 0; 170 } 171 EXPORT_SYMBOL_GPL(mei_reset); 172 173 /** 174 * mei_start - initializes host and fw to start work. 175 * 176 * @dev: the device structure 177 * 178 * Return: 0 on success, <0 on failure. 179 */ 180 int mei_start(struct mei_device *dev) 181 { 182 int ret; 183 184 mutex_lock(&dev->device_lock); 185 186 /* acknowledge interrupt and stop interrupts */ 187 mei_clear_interrupts(dev); 188 189 mei_hw_config(dev); 190 191 dev_dbg(dev->dev, "reset in start the mei device.\n"); 192 193 dev->reset_count = 0; 194 do { 195 dev->dev_state = MEI_DEV_INITIALIZING; 196 ret = mei_reset(dev); 197 198 if (ret == -ENODEV || dev->dev_state == MEI_DEV_DISABLED) { 199 dev_err(dev->dev, "reset failed ret = %d", ret); 200 goto err; 201 } 202 } while (ret); 203 204 /* we cannot start the device w/o hbm start message completed */ 205 if (dev->dev_state == MEI_DEV_DISABLED) { 206 dev_err(dev->dev, "reset failed"); 207 goto err; 208 } 209 210 if (mei_hbm_start_wait(dev)) { 211 dev_err(dev->dev, "HBM haven't started"); 212 goto err; 213 } 214 215 if (!mei_host_is_ready(dev)) { 216 dev_err(dev->dev, "host is not ready.\n"); 217 goto err; 218 } 219 220 if (!mei_hw_is_ready(dev)) { 221 dev_err(dev->dev, "ME is not ready.\n"); 222 goto err; 223 } 224 225 if (!mei_hbm_version_is_supported(dev)) { 226 dev_dbg(dev->dev, "MEI start failed.\n"); 227 goto err; 228 } 229 230 dev_dbg(dev->dev, "link layer has been established.\n"); 231 232 mutex_unlock(&dev->device_lock); 233 return 0; 234 err: 235 dev_err(dev->dev, "link layer initialization failed.\n"); 236 dev->dev_state = MEI_DEV_DISABLED; 237 mutex_unlock(&dev->device_lock); 238 return -ENODEV; 239 } 240 EXPORT_SYMBOL_GPL(mei_start); 241 242 /** 243 * mei_restart - restart device after suspend 244 * 245 * @dev: the device structure 246 * 247 * Return: 0 on success or -ENODEV if the restart hasn't succeeded 248 */ 249 int mei_restart(struct mei_device *dev) 250 { 251 int err; 252 253 mutex_lock(&dev->device_lock); 254 255 mei_clear_interrupts(dev); 256 257 dev->dev_state = MEI_DEV_POWER_UP; 258 dev->reset_count = 0; 259 260 err = mei_reset(dev); 261 262 mutex_unlock(&dev->device_lock); 263 264 if (err == -ENODEV || dev->dev_state == MEI_DEV_DISABLED) { 265 dev_err(dev->dev, "device disabled = %d\n", err); 266 return -ENODEV; 267 } 268 269 /* try to start again */ 270 if (err) 271 schedule_work(&dev->reset_work); 272 273 274 return 0; 275 } 276 EXPORT_SYMBOL_GPL(mei_restart); 277 278 static void mei_reset_work(struct work_struct *work) 279 { 280 struct mei_device *dev = 281 container_of(work, struct mei_device, reset_work); 282 int ret; 283 284 mutex_lock(&dev->device_lock); 285 286 ret = mei_reset(dev); 287 288 mutex_unlock(&dev->device_lock); 289 290 if (dev->dev_state == MEI_DEV_DISABLED) { 291 dev_err(dev->dev, "device disabled = %d\n", ret); 292 return; 293 } 294 295 /* retry reset in case of failure */ 296 if (ret) 297 schedule_work(&dev->reset_work); 298 } 299 300 void mei_stop(struct mei_device *dev) 301 { 302 dev_dbg(dev->dev, "stopping the device.\n"); 303 304 mei_cancel_work(dev); 305 306 mei_nfc_host_exit(dev); 307 308 mei_cl_bus_remove_devices(dev); 309 310 mutex_lock(&dev->device_lock); 311 312 mei_wd_stop(dev); 313 314 dev->dev_state = MEI_DEV_POWER_DOWN; 315 mei_reset(dev); 316 317 mutex_unlock(&dev->device_lock); 318 319 mei_watchdog_unregister(dev); 320 } 321 EXPORT_SYMBOL_GPL(mei_stop); 322 323 /** 324 * mei_write_is_idle - check if the write queues are idle 325 * 326 * @dev: the device structure 327 * 328 * Return: true of there is no pending write 329 */ 330 bool mei_write_is_idle(struct mei_device *dev) 331 { 332 bool idle = (dev->dev_state == MEI_DEV_ENABLED && 333 list_empty(&dev->ctrl_wr_list.list) && 334 list_empty(&dev->write_list.list)); 335 336 dev_dbg(dev->dev, "write pg: is idle[%d] state=%s ctrl=%d write=%d\n", 337 idle, 338 mei_dev_state_str(dev->dev_state), 339 list_empty(&dev->ctrl_wr_list.list), 340 list_empty(&dev->write_list.list)); 341 342 return idle; 343 } 344 EXPORT_SYMBOL_GPL(mei_write_is_idle); 345 346 /** 347 * mei_device_init -- initialize mei_device structure 348 * 349 * @dev: the mei device 350 * @device: the device structure 351 * @hw_ops: hw operations 352 */ 353 void mei_device_init(struct mei_device *dev, 354 struct device *device, 355 const struct mei_hw_ops *hw_ops) 356 { 357 /* setup our list array */ 358 INIT_LIST_HEAD(&dev->file_list); 359 INIT_LIST_HEAD(&dev->device_list); 360 INIT_LIST_HEAD(&dev->me_clients); 361 mutex_init(&dev->device_lock); 362 init_waitqueue_head(&dev->wait_hw_ready); 363 init_waitqueue_head(&dev->wait_pg); 364 init_waitqueue_head(&dev->wait_hbm_start); 365 init_waitqueue_head(&dev->wait_stop_wd); 366 dev->dev_state = MEI_DEV_INITIALIZING; 367 dev->reset_count = 0; 368 369 mei_io_list_init(&dev->read_list); 370 mei_io_list_init(&dev->write_list); 371 mei_io_list_init(&dev->write_waiting_list); 372 mei_io_list_init(&dev->ctrl_wr_list); 373 mei_io_list_init(&dev->ctrl_rd_list); 374 375 INIT_DELAYED_WORK(&dev->timer_work, mei_timer); 376 INIT_WORK(&dev->init_work, mei_host_client_init); 377 INIT_WORK(&dev->reset_work, mei_reset_work); 378 379 INIT_LIST_HEAD(&dev->wd_cl.link); 380 INIT_LIST_HEAD(&dev->iamthif_cl.link); 381 mei_io_list_init(&dev->amthif_cmd_list); 382 mei_io_list_init(&dev->amthif_rd_complete_list); 383 384 bitmap_zero(dev->host_clients_map, MEI_CLIENTS_MAX); 385 dev->open_handle_count = 0; 386 387 /* 388 * Reserving the first client ID 389 * 0: Reserved for MEI Bus Message communications 390 */ 391 bitmap_set(dev->host_clients_map, 0, 1); 392 393 dev->pg_event = MEI_PG_EVENT_IDLE; 394 dev->ops = hw_ops; 395 dev->dev = device; 396 } 397 EXPORT_SYMBOL_GPL(mei_device_init); 398 399