init.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  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. #include <linux/pci.h>
  17. #include <linux/sched.h>
  18. #include <linux/wait.h>
  19. #include <linux/delay.h>
  20. #include <linux/mei.h>
  21. #include "mei_dev.h"
  22. #include "hbm.h"
  23. #include "interface.h"
  24. const char *mei_dev_state_str(int state)
  25. {
  26. #define MEI_DEV_STATE(state) case MEI_DEV_##state: return #state
  27. switch (state) {
  28. MEI_DEV_STATE(INITIALIZING);
  29. MEI_DEV_STATE(INIT_CLIENTS);
  30. MEI_DEV_STATE(ENABLED);
  31. MEI_DEV_STATE(RESETING);
  32. MEI_DEV_STATE(DISABLED);
  33. MEI_DEV_STATE(RECOVERING_FROM_RESET);
  34. MEI_DEV_STATE(POWER_DOWN);
  35. MEI_DEV_STATE(POWER_UP);
  36. default:
  37. return "unkown";
  38. }
  39. #undef MEI_DEV_STATE
  40. }
  41. /**
  42. * mei_io_list_flush - removes list entry belonging to cl.
  43. *
  44. * @list: An instance of our list structure
  45. * @cl: private data of the file object
  46. */
  47. void mei_io_list_flush(struct mei_cl_cb *list, struct mei_cl *cl)
  48. {
  49. struct mei_cl_cb *pos;
  50. struct mei_cl_cb *next;
  51. list_for_each_entry_safe(pos, next, &list->list, list) {
  52. if (pos->cl) {
  53. if (mei_cl_cmp_id(cl, pos->cl))
  54. list_del(&pos->list);
  55. }
  56. }
  57. }
  58. /**
  59. * mei_cl_flush_queues - flushes queue lists belonging to cl.
  60. *
  61. * @dev: the device structure
  62. * @cl: private data of the file object
  63. */
  64. int mei_cl_flush_queues(struct mei_cl *cl)
  65. {
  66. if (!cl || !cl->dev)
  67. return -EINVAL;
  68. dev_dbg(&cl->dev->pdev->dev, "remove list entry belonging to cl\n");
  69. mei_io_list_flush(&cl->dev->read_list, cl);
  70. mei_io_list_flush(&cl->dev->write_list, cl);
  71. mei_io_list_flush(&cl->dev->write_waiting_list, cl);
  72. mei_io_list_flush(&cl->dev->ctrl_wr_list, cl);
  73. mei_io_list_flush(&cl->dev->ctrl_rd_list, cl);
  74. mei_io_list_flush(&cl->dev->amthif_cmd_list, cl);
  75. mei_io_list_flush(&cl->dev->amthif_rd_complete_list, cl);
  76. return 0;
  77. }
  78. /**
  79. * init_mei_device - allocates and initializes the mei device structure
  80. *
  81. * @pdev: The pci device structure
  82. *
  83. * returns The mei_device_device pointer on success, NULL on failure.
  84. */
  85. struct mei_device *mei_device_init(struct pci_dev *pdev)
  86. {
  87. struct mei_device *dev;
  88. dev = kzalloc(sizeof(struct mei_device), GFP_KERNEL);
  89. if (!dev)
  90. return NULL;
  91. /* setup our list array */
  92. INIT_LIST_HEAD(&dev->file_list);
  93. INIT_LIST_HEAD(&dev->wd_cl.link);
  94. INIT_LIST_HEAD(&dev->iamthif_cl.link);
  95. mutex_init(&dev->device_lock);
  96. init_waitqueue_head(&dev->wait_recvd_msg);
  97. init_waitqueue_head(&dev->wait_stop_wd);
  98. dev->dev_state = MEI_DEV_INITIALIZING;
  99. dev->iamthif_state = MEI_IAMTHIF_IDLE;
  100. mei_io_list_init(&dev->read_list);
  101. mei_io_list_init(&dev->write_list);
  102. mei_io_list_init(&dev->write_waiting_list);
  103. mei_io_list_init(&dev->ctrl_wr_list);
  104. mei_io_list_init(&dev->ctrl_rd_list);
  105. mei_io_list_init(&dev->amthif_cmd_list);
  106. mei_io_list_init(&dev->amthif_rd_complete_list);
  107. dev->pdev = pdev;
  108. return dev;
  109. }
  110. /**
  111. * mei_hw_init - initializes host and fw to start work.
  112. *
  113. * @dev: the device structure
  114. *
  115. * returns 0 on success, <0 on failure.
  116. */
  117. int mei_hw_init(struct mei_device *dev)
  118. {
  119. int err = 0;
  120. int ret;
  121. mutex_lock(&dev->device_lock);
  122. dev->host_hw_state = mei_hcsr_read(dev);
  123. dev->me_hw_state = mei_mecsr_read(dev);
  124. dev_dbg(&dev->pdev->dev, "host_hw_state = 0x%08x, mestate = 0x%08x.\n",
  125. dev->host_hw_state, dev->me_hw_state);
  126. /* acknowledge interrupt and stop interupts */
  127. mei_clear_interrupts(dev);
  128. /* Doesn't change in runtime */
  129. dev->hbuf_depth = (dev->host_hw_state & H_CBD) >> 24;
  130. dev->recvd_msg = false;
  131. dev_dbg(&dev->pdev->dev, "reset in start the mei device.\n");
  132. mei_reset(dev, 1);
  133. dev_dbg(&dev->pdev->dev, "host_hw_state = 0x%08x, me_hw_state = 0x%08x.\n",
  134. dev->host_hw_state, dev->me_hw_state);
  135. /* wait for ME to turn on ME_RDY */
  136. if (!dev->recvd_msg) {
  137. mutex_unlock(&dev->device_lock);
  138. err = wait_event_interruptible_timeout(dev->wait_recvd_msg,
  139. dev->recvd_msg,
  140. mei_secs_to_jiffies(MEI_INTEROP_TIMEOUT));
  141. mutex_lock(&dev->device_lock);
  142. }
  143. if (err <= 0 && !dev->recvd_msg) {
  144. dev->dev_state = MEI_DEV_DISABLED;
  145. dev_dbg(&dev->pdev->dev,
  146. "wait_event_interruptible_timeout failed"
  147. "on wait for ME to turn on ME_RDY.\n");
  148. ret = -ENODEV;
  149. goto out;
  150. }
  151. if (!(((dev->host_hw_state & H_RDY) == H_RDY) &&
  152. ((dev->me_hw_state & ME_RDY_HRA) == ME_RDY_HRA))) {
  153. dev->dev_state = MEI_DEV_DISABLED;
  154. dev_dbg(&dev->pdev->dev,
  155. "host_hw_state = 0x%08x, me_hw_state = 0x%08x.\n",
  156. dev->host_hw_state, dev->me_hw_state);
  157. if (!(dev->host_hw_state & H_RDY))
  158. dev_dbg(&dev->pdev->dev, "host turn off H_RDY.\n");
  159. if (!(dev->me_hw_state & ME_RDY_HRA))
  160. dev_dbg(&dev->pdev->dev, "ME turn off ME_RDY.\n");
  161. dev_err(&dev->pdev->dev, "link layer initialization failed.\n");
  162. ret = -ENODEV;
  163. goto out;
  164. }
  165. if (dev->version.major_version != HBM_MAJOR_VERSION ||
  166. dev->version.minor_version != HBM_MINOR_VERSION) {
  167. dev_dbg(&dev->pdev->dev, "MEI start failed.\n");
  168. ret = -ENODEV;
  169. goto out;
  170. }
  171. dev->recvd_msg = false;
  172. dev_dbg(&dev->pdev->dev, "host_hw_state = 0x%08x, me_hw_state = 0x%08x.\n",
  173. dev->host_hw_state, dev->me_hw_state);
  174. dev_dbg(&dev->pdev->dev, "ME turn on ME_RDY and host turn on H_RDY.\n");
  175. dev_dbg(&dev->pdev->dev, "link layer has been established.\n");
  176. dev_dbg(&dev->pdev->dev, "MEI start success.\n");
  177. ret = 0;
  178. out:
  179. mutex_unlock(&dev->device_lock);
  180. return ret;
  181. }
  182. /**
  183. * mei_hw_reset - resets fw via mei csr register.
  184. *
  185. * @dev: the device structure
  186. * @interrupts_enabled: if interrupt should be enabled after reset.
  187. */
  188. static void mei_hw_reset(struct mei_device *dev, int interrupts_enabled)
  189. {
  190. dev->host_hw_state |= (H_RST | H_IG);
  191. if (interrupts_enabled)
  192. mei_enable_interrupts(dev);
  193. else
  194. mei_disable_interrupts(dev);
  195. }
  196. /**
  197. * mei_reset - resets host and fw.
  198. *
  199. * @dev: the device structure
  200. * @interrupts_enabled: if interrupt should be enabled after reset.
  201. */
  202. void mei_reset(struct mei_device *dev, int interrupts_enabled)
  203. {
  204. struct mei_cl *cl_pos = NULL;
  205. struct mei_cl *cl_next = NULL;
  206. struct mei_cl_cb *cb_pos = NULL;
  207. struct mei_cl_cb *cb_next = NULL;
  208. bool unexpected;
  209. if (dev->dev_state == MEI_DEV_RECOVERING_FROM_RESET) {
  210. dev->need_reset = true;
  211. return;
  212. }
  213. unexpected = (dev->dev_state != MEI_DEV_INITIALIZING &&
  214. dev->dev_state != MEI_DEV_DISABLED &&
  215. dev->dev_state != MEI_DEV_POWER_DOWN &&
  216. dev->dev_state != MEI_DEV_POWER_UP);
  217. dev->host_hw_state = mei_hcsr_read(dev);
  218. dev_dbg(&dev->pdev->dev, "before reset host_hw_state = 0x%08x.\n",
  219. dev->host_hw_state);
  220. mei_hw_reset(dev, interrupts_enabled);
  221. dev->host_hw_state &= ~H_RST;
  222. dev->host_hw_state |= H_IG;
  223. mei_hcsr_set(dev);
  224. dev_dbg(&dev->pdev->dev, "currently saved host_hw_state = 0x%08x.\n",
  225. dev->host_hw_state);
  226. dev->need_reset = false;
  227. if (dev->dev_state != MEI_DEV_INITIALIZING) {
  228. if (dev->dev_state != MEI_DEV_DISABLED &&
  229. dev->dev_state != MEI_DEV_POWER_DOWN)
  230. dev->dev_state = MEI_DEV_RESETING;
  231. list_for_each_entry_safe(cl_pos,
  232. cl_next, &dev->file_list, link) {
  233. cl_pos->state = MEI_FILE_DISCONNECTED;
  234. cl_pos->mei_flow_ctrl_creds = 0;
  235. cl_pos->read_cb = NULL;
  236. cl_pos->timer_count = 0;
  237. }
  238. /* remove entry if already in list */
  239. dev_dbg(&dev->pdev->dev, "remove iamthif and wd from the file list.\n");
  240. mei_me_cl_unlink(dev, &dev->wd_cl);
  241. mei_me_cl_unlink(dev, &dev->iamthif_cl);
  242. mei_amthif_reset_params(dev);
  243. memset(&dev->wr_ext_msg, 0, sizeof(dev->wr_ext_msg));
  244. }
  245. dev->me_clients_num = 0;
  246. dev->rd_msg_hdr = 0;
  247. dev->wd_pending = false;
  248. /* update the state of the registers after reset */
  249. dev->host_hw_state = mei_hcsr_read(dev);
  250. dev->me_hw_state = mei_mecsr_read(dev);
  251. dev_dbg(&dev->pdev->dev, "after reset host_hw_state = 0x%08x, me_hw_state = 0x%08x.\n",
  252. dev->host_hw_state, dev->me_hw_state);
  253. if (unexpected)
  254. dev_warn(&dev->pdev->dev, "unexpected reset: dev_state = %s\n",
  255. mei_dev_state_str(dev->dev_state));
  256. /* Wake up all readings so they can be interrupted */
  257. list_for_each_entry_safe(cl_pos, cl_next, &dev->file_list, link) {
  258. if (waitqueue_active(&cl_pos->rx_wait)) {
  259. dev_dbg(&dev->pdev->dev, "Waking up client!\n");
  260. wake_up_interruptible(&cl_pos->rx_wait);
  261. }
  262. }
  263. /* remove all waiting requests */
  264. list_for_each_entry_safe(cb_pos, cb_next, &dev->write_list.list, list) {
  265. list_del(&cb_pos->list);
  266. mei_io_cb_free(cb_pos);
  267. }
  268. }
  269. /**
  270. * allocate_me_clients_storage - allocates storage for me clients
  271. *
  272. * @dev: the device structure
  273. *
  274. * returns none.
  275. */
  276. void mei_allocate_me_clients_storage(struct mei_device *dev)
  277. {
  278. struct mei_me_client *clients;
  279. int b;
  280. /* count how many ME clients we have */
  281. for_each_set_bit(b, dev->me_clients_map, MEI_CLIENTS_MAX)
  282. dev->me_clients_num++;
  283. if (dev->me_clients_num <= 0)
  284. return ;
  285. if (dev->me_clients != NULL) {
  286. kfree(dev->me_clients);
  287. dev->me_clients = NULL;
  288. }
  289. dev_dbg(&dev->pdev->dev, "memory allocation for ME clients size=%zd.\n",
  290. dev->me_clients_num * sizeof(struct mei_me_client));
  291. /* allocate storage for ME clients representation */
  292. clients = kcalloc(dev->me_clients_num,
  293. sizeof(struct mei_me_client), GFP_KERNEL);
  294. if (!clients) {
  295. dev_dbg(&dev->pdev->dev, "memory allocation for ME clients failed.\n");
  296. dev->dev_state = MEI_DEV_RESETING;
  297. mei_reset(dev, 1);
  298. return ;
  299. }
  300. dev->me_clients = clients;
  301. return ;
  302. }
  303. void mei_host_client_init(struct work_struct *work)
  304. {
  305. struct mei_device *dev = container_of(work,
  306. struct mei_device, init_work);
  307. struct mei_client_properties *client_props;
  308. int i;
  309. mutex_lock(&dev->device_lock);
  310. bitmap_zero(dev->host_clients_map, MEI_CLIENTS_MAX);
  311. dev->open_handle_count = 0;
  312. /*
  313. * Reserving the first three client IDs
  314. * 0: Reserved for MEI Bus Message communications
  315. * 1: Reserved for Watchdog
  316. * 2: Reserved for AMTHI
  317. */
  318. bitmap_set(dev->host_clients_map, 0, 3);
  319. for (i = 0; i < dev->me_clients_num; i++) {
  320. client_props = &dev->me_clients[i].props;
  321. if (!uuid_le_cmp(client_props->protocol_name, mei_amthi_guid))
  322. mei_amthif_host_init(dev);
  323. else if (!uuid_le_cmp(client_props->protocol_name, mei_wd_guid))
  324. mei_wd_host_init(dev);
  325. }
  326. dev->dev_state = MEI_DEV_ENABLED;
  327. mutex_unlock(&dev->device_lock);
  328. }
  329. /**
  330. * mei_init_file_private - initializes private file structure.
  331. *
  332. * @priv: private file structure to be initialized
  333. * @file: the file structure
  334. */
  335. void mei_cl_init(struct mei_cl *priv, struct mei_device *dev)
  336. {
  337. memset(priv, 0, sizeof(struct mei_cl));
  338. init_waitqueue_head(&priv->wait);
  339. init_waitqueue_head(&priv->rx_wait);
  340. init_waitqueue_head(&priv->tx_wait);
  341. INIT_LIST_HEAD(&priv->link);
  342. priv->reading_state = MEI_IDLE;
  343. priv->writing_state = MEI_IDLE;
  344. priv->dev = dev;
  345. }
  346. int mei_me_cl_by_uuid(const struct mei_device *dev, const uuid_le *cuuid)
  347. {
  348. int i, res = -ENOENT;
  349. for (i = 0; i < dev->me_clients_num; ++i)
  350. if (uuid_le_cmp(*cuuid,
  351. dev->me_clients[i].props.protocol_name) == 0) {
  352. res = i;
  353. break;
  354. }
  355. return res;
  356. }
  357. /**
  358. * mei_me_cl_link - create link between host and me clinet and add
  359. * me_cl to the list
  360. *
  361. * @dev: the device structure
  362. * @cl: link between me and host client assocated with opened file descriptor
  363. * @cuuid: uuid of ME client
  364. * @client_id: id of the host client
  365. *
  366. * returns ME client index if ME client
  367. * -EINVAL on incorrect values
  368. * -ENONET if client not found
  369. */
  370. int mei_me_cl_link(struct mei_device *dev, struct mei_cl *cl,
  371. const uuid_le *cuuid, u8 host_cl_id)
  372. {
  373. int i;
  374. if (!dev || !cl || !cuuid)
  375. return -EINVAL;
  376. /* check for valid client id */
  377. i = mei_me_cl_by_uuid(dev, cuuid);
  378. if (i >= 0) {
  379. cl->me_client_id = dev->me_clients[i].client_id;
  380. cl->state = MEI_FILE_CONNECTING;
  381. cl->host_client_id = host_cl_id;
  382. list_add_tail(&cl->link, &dev->file_list);
  383. return (u8)i;
  384. }
  385. return -ENOENT;
  386. }
  387. /**
  388. * mei_me_cl_unlink - remove me_cl from the list
  389. *
  390. * @dev: the device structure
  391. * @host_client_id: host client id to be removed
  392. */
  393. void mei_me_cl_unlink(struct mei_device *dev, struct mei_cl *cl)
  394. {
  395. struct mei_cl *pos, *next;
  396. list_for_each_entry_safe(pos, next, &dev->file_list, link) {
  397. if (cl->host_client_id == pos->host_client_id) {
  398. dev_dbg(&dev->pdev->dev, "remove host client = %d, ME client = %d\n",
  399. pos->host_client_id, pos->me_client_id);
  400. list_del_init(&pos->link);
  401. break;
  402. }
  403. }
  404. }
  405. /**
  406. * mei_alloc_file_private - allocates a private file structure and sets it up.
  407. * @file: the file structure
  408. *
  409. * returns The allocated file or NULL on failure
  410. */
  411. struct mei_cl *mei_cl_allocate(struct mei_device *dev)
  412. {
  413. struct mei_cl *cl;
  414. cl = kmalloc(sizeof(struct mei_cl), GFP_KERNEL);
  415. if (!cl)
  416. return NULL;
  417. mei_cl_init(cl, dev);
  418. return cl;
  419. }
  420. /**
  421. * mei_disconnect_host_client - sends disconnect message to fw from host client.
  422. *
  423. * @dev: the device structure
  424. * @cl: private data of the file object
  425. *
  426. * Locking: called under "dev->device_lock" lock
  427. *
  428. * returns 0 on success, <0 on failure.
  429. */
  430. int mei_disconnect_host_client(struct mei_device *dev, struct mei_cl *cl)
  431. {
  432. struct mei_cl_cb *cb;
  433. int rets, err;
  434. if (!dev || !cl)
  435. return -ENODEV;
  436. if (cl->state != MEI_FILE_DISCONNECTING)
  437. return 0;
  438. cb = mei_io_cb_init(cl, NULL);
  439. if (!cb)
  440. return -ENOMEM;
  441. cb->fop_type = MEI_FOP_CLOSE;
  442. if (dev->mei_host_buffer_is_empty) {
  443. dev->mei_host_buffer_is_empty = false;
  444. if (mei_hbm_cl_disconnect_req(dev, cl)) {
  445. rets = -ENODEV;
  446. dev_err(&dev->pdev->dev, "failed to disconnect.\n");
  447. goto free;
  448. }
  449. mdelay(10); /* Wait for hardware disconnection ready */
  450. list_add_tail(&cb->list, &dev->ctrl_rd_list.list);
  451. } else {
  452. dev_dbg(&dev->pdev->dev, "add disconnect cb to control write list\n");
  453. list_add_tail(&cb->list, &dev->ctrl_wr_list.list);
  454. }
  455. mutex_unlock(&dev->device_lock);
  456. err = wait_event_timeout(dev->wait_recvd_msg,
  457. MEI_FILE_DISCONNECTED == cl->state,
  458. mei_secs_to_jiffies(MEI_CL_CONNECT_TIMEOUT));
  459. mutex_lock(&dev->device_lock);
  460. if (MEI_FILE_DISCONNECTED == cl->state) {
  461. rets = 0;
  462. dev_dbg(&dev->pdev->dev, "successfully disconnected from FW client.\n");
  463. } else {
  464. rets = -ENODEV;
  465. if (MEI_FILE_DISCONNECTED != cl->state)
  466. dev_dbg(&dev->pdev->dev, "wrong status client disconnect.\n");
  467. if (err)
  468. dev_dbg(&dev->pdev->dev,
  469. "wait failed disconnect err=%08x\n",
  470. err);
  471. dev_dbg(&dev->pdev->dev, "failed to disconnect from FW client.\n");
  472. }
  473. mei_io_list_flush(&dev->ctrl_rd_list, cl);
  474. mei_io_list_flush(&dev->ctrl_wr_list, cl);
  475. free:
  476. mei_io_cb_free(cb);
  477. return rets;
  478. }