wd.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. *
  3. * Intel Management Engine Interface (Intel MEI) Linux driver
  4. * Copyright (c) 2003-2011, 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/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/moduleparam.h>
  19. #include <linux/device.h>
  20. #include <linux/pci.h>
  21. #include <linux/sched.h>
  22. #include "mei_dev.h"
  23. #include "hw.h"
  24. #include "interface.h"
  25. #include "mei.h"
  26. /*
  27. * MEI Watchdog Module Parameters
  28. */
  29. static u16 watchdog_timeout = AMT_WD_VALUE;
  30. module_param(watchdog_timeout, ushort, 0);
  31. MODULE_PARM_DESC(watchdog_timeout,
  32. "Intel(R) AMT Watchdog timeout value in seconds. (default="
  33. __MODULE_STRING(AMT_WD_VALUE)
  34. ", disable=0)");
  35. static const u8 mei_start_wd_params[] = { 0x02, 0x12, 0x13, 0x10 };
  36. static const u8 mei_stop_wd_params[] = { 0x02, 0x02, 0x14, 0x10 };
  37. const u8 mei_wd_state_independence_msg[3][4] = {
  38. {0x05, 0x02, 0x51, 0x10},
  39. {0x05, 0x02, 0x52, 0x10},
  40. {0x07, 0x02, 0x01, 0x10}
  41. };
  42. /* UUIDs for AMT F/W clients */
  43. const uuid_le mei_wd_guid = UUID_LE(0x05B79A6F, 0x4628, 0x4D7F, 0x89,
  44. 0x9D, 0xA9, 0x15, 0x14, 0xCB,
  45. 0x32, 0xAB);
  46. void mei_wd_start_setup(struct mei_device *dev)
  47. {
  48. dev_dbg(&dev->pdev->dev, "dev->wd_timeout=%d.\n", dev->wd_timeout);
  49. memcpy(dev->wd_data, mei_start_wd_params, MEI_WD_PARAMS_SIZE);
  50. memcpy(dev->wd_data + MEI_WD_PARAMS_SIZE,
  51. &dev->wd_timeout, sizeof(u16));
  52. }
  53. /**
  54. * host_init_wd - mei initialization wd.
  55. *
  56. * @dev: the device structure
  57. */
  58. void mei_wd_host_init(struct mei_device *dev)
  59. {
  60. mei_init_file_private(&dev->wd_cl, dev);
  61. /* look for WD client and connect to it */
  62. dev->wd_cl.state = MEI_FILE_DISCONNECTED;
  63. dev->wd_timeout = watchdog_timeout;
  64. if (dev->wd_timeout > 0) {
  65. mei_wd_start_setup(dev);
  66. /* find ME WD client */
  67. mei_find_me_client_update_filext(dev, &dev->wd_cl,
  68. &mei_wd_guid, MEI_WD_HOST_CLIENT_ID);
  69. dev_dbg(&dev->pdev->dev, "check wd_cl\n");
  70. if (MEI_FILE_CONNECTING == dev->wd_cl.state) {
  71. if (!mei_connect(dev, &dev->wd_cl)) {
  72. dev_dbg(&dev->pdev->dev, "Failed to connect to WD client\n");
  73. dev->wd_cl.state = MEI_FILE_DISCONNECTED;
  74. dev->wd_cl.host_client_id = 0;
  75. host_init_iamthif(dev) ;
  76. } else {
  77. dev->wd_cl.timer_count = CONNECT_TIMEOUT;
  78. }
  79. } else {
  80. dev_dbg(&dev->pdev->dev, "Failed to find WD client\n");
  81. host_init_iamthif(dev) ;
  82. }
  83. } else {
  84. dev->wd_bypass = true;
  85. dev_dbg(&dev->pdev->dev, "WD requested to be disabled\n");
  86. host_init_iamthif(dev) ;
  87. }
  88. }
  89. /**
  90. * mei_wd_send - sends watch dog message to fw.
  91. *
  92. * @dev: the device structure
  93. *
  94. * returns 0 if success,
  95. * -EIO when message send fails
  96. * -EINVAL when invalid message is to be sent
  97. */
  98. int mei_wd_send(struct mei_device *dev)
  99. {
  100. struct mei_msg_hdr *mei_hdr;
  101. mei_hdr = (struct mei_msg_hdr *) &dev->wr_msg_buf[0];
  102. mei_hdr->host_addr = dev->wd_cl.host_client_id;
  103. mei_hdr->me_addr = dev->wd_cl.me_client_id;
  104. mei_hdr->msg_complete = 1;
  105. mei_hdr->reserved = 0;
  106. if (!memcmp(dev->wd_data, mei_start_wd_params, MEI_WD_PARAMS_SIZE))
  107. mei_hdr->length = MEI_START_WD_DATA_SIZE;
  108. else if (!memcmp(dev->wd_data, mei_stop_wd_params, MEI_WD_PARAMS_SIZE))
  109. mei_hdr->length = MEI_WD_PARAMS_SIZE;
  110. else
  111. return -EINVAL;
  112. if (mei_write_message(dev, mei_hdr, dev->wd_data, mei_hdr->length))
  113. return 0;
  114. return -EIO;
  115. }
  116. int mei_wd_stop(struct mei_device *dev, bool preserve)
  117. {
  118. int ret;
  119. u16 wd_timeout = dev->wd_timeout;
  120. cancel_delayed_work(&dev->wd_work);
  121. if (dev->wd_cl.state != MEI_FILE_CONNECTED || !dev->wd_timeout)
  122. return 0;
  123. dev->wd_timeout = 0;
  124. dev->wd_due_counter = 0;
  125. memcpy(dev->wd_data, mei_stop_wd_params, MEI_WD_PARAMS_SIZE);
  126. dev->stop = 1;
  127. ret = mei_flow_ctrl_creds(dev, &dev->wd_cl);
  128. if (ret < 0)
  129. goto out;
  130. if (ret && dev->mei_host_buffer_is_empty) {
  131. ret = 0;
  132. dev->mei_host_buffer_is_empty = 0;
  133. if (!mei_wd_send(dev)) {
  134. ret = mei_flow_ctrl_reduce(dev, &dev->wd_cl);
  135. if (ret)
  136. goto out;
  137. } else {
  138. dev_dbg(&dev->pdev->dev, "send stop WD failed\n");
  139. }
  140. dev->wd_pending = 0;
  141. } else {
  142. dev->wd_pending = 1;
  143. }
  144. dev->wd_stopped = 0;
  145. mutex_unlock(&dev->device_lock);
  146. ret = wait_event_interruptible_timeout(dev->wait_stop_wd,
  147. dev->wd_stopped, 10 * HZ);
  148. mutex_lock(&dev->device_lock);
  149. if (dev->wd_stopped) {
  150. dev_dbg(&dev->pdev->dev, "stop wd complete ret=%d.\n", ret);
  151. ret = 0;
  152. } else {
  153. if (!ret)
  154. ret = -ETIMEDOUT;
  155. dev_warn(&dev->pdev->dev,
  156. "stop wd failed to complete ret=%d.\n", ret);
  157. }
  158. if (preserve)
  159. dev->wd_timeout = wd_timeout;
  160. out:
  161. return ret;
  162. }