catas.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. * Copyright (c) 2007 Cisco Systems, Inc. All rights reserved.
  3. * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. #include <linux/workqueue.h>
  34. #include <linux/module.h>
  35. #include "mlx4.h"
  36. enum {
  37. MLX4_CATAS_POLL_INTERVAL = 5 * HZ,
  38. };
  39. int mlx4_internal_err_reset = 1;
  40. module_param_named(internal_err_reset, mlx4_internal_err_reset, int, 0644);
  41. MODULE_PARM_DESC(internal_err_reset,
  42. "Reset device on internal errors if non-zero (default 1)");
  43. static int read_vendor_id(struct mlx4_dev *dev)
  44. {
  45. u16 vendor_id = 0;
  46. int ret;
  47. ret = pci_read_config_word(dev->persist->pdev, 0, &vendor_id);
  48. if (ret) {
  49. mlx4_err(dev, "Failed to read vendor ID, ret=%d\n", ret);
  50. return ret;
  51. }
  52. if (vendor_id == 0xffff) {
  53. mlx4_err(dev, "PCI can't be accessed to read vendor id\n");
  54. return -EINVAL;
  55. }
  56. return 0;
  57. }
  58. static int mlx4_reset_master(struct mlx4_dev *dev)
  59. {
  60. int err = 0;
  61. if (mlx4_is_master(dev))
  62. mlx4_report_internal_err_comm_event(dev);
  63. if (!pci_channel_offline(dev->persist->pdev)) {
  64. err = read_vendor_id(dev);
  65. /* If PCI can't be accessed to read vendor ID we assume that its
  66. * link was disabled and chip was already reset.
  67. */
  68. if (err)
  69. return 0;
  70. err = mlx4_reset(dev);
  71. if (err)
  72. mlx4_err(dev, "Fail to reset HCA\n");
  73. }
  74. return err;
  75. }
  76. static int mlx4_reset_slave(struct mlx4_dev *dev)
  77. {
  78. #define COM_CHAN_RST_REQ_OFFSET 0x10
  79. #define COM_CHAN_RST_ACK_OFFSET 0x08
  80. u32 comm_flags;
  81. u32 rst_req;
  82. u32 rst_ack;
  83. unsigned long end;
  84. struct mlx4_priv *priv = mlx4_priv(dev);
  85. if (pci_channel_offline(dev->persist->pdev))
  86. return 0;
  87. comm_flags = swab32(readl((__iomem char *)priv->mfunc.comm +
  88. MLX4_COMM_CHAN_FLAGS));
  89. if (comm_flags == 0xffffffff) {
  90. mlx4_err(dev, "VF reset is not needed\n");
  91. return 0;
  92. }
  93. if (!(dev->caps.vf_caps & MLX4_VF_CAP_FLAG_RESET)) {
  94. mlx4_err(dev, "VF reset is not supported\n");
  95. return -EOPNOTSUPP;
  96. }
  97. rst_req = (comm_flags & (u32)(1 << COM_CHAN_RST_REQ_OFFSET)) >>
  98. COM_CHAN_RST_REQ_OFFSET;
  99. rst_ack = (comm_flags & (u32)(1 << COM_CHAN_RST_ACK_OFFSET)) >>
  100. COM_CHAN_RST_ACK_OFFSET;
  101. if (rst_req != rst_ack) {
  102. mlx4_err(dev, "Communication channel isn't sync, fail to send reset\n");
  103. return -EIO;
  104. }
  105. rst_req ^= 1;
  106. mlx4_warn(dev, "VF is sending reset request to Firmware\n");
  107. comm_flags = rst_req << COM_CHAN_RST_REQ_OFFSET;
  108. __raw_writel((__force u32)cpu_to_be32(comm_flags),
  109. (__iomem char *)priv->mfunc.comm + MLX4_COMM_CHAN_FLAGS);
  110. /* Make sure that our comm channel write doesn't
  111. * get mixed in with writes from another CPU.
  112. */
  113. mmiowb();
  114. end = msecs_to_jiffies(MLX4_COMM_TIME) + jiffies;
  115. while (time_before(jiffies, end)) {
  116. comm_flags = swab32(readl((__iomem char *)priv->mfunc.comm +
  117. MLX4_COMM_CHAN_FLAGS));
  118. rst_ack = (comm_flags & (u32)(1 << COM_CHAN_RST_ACK_OFFSET)) >>
  119. COM_CHAN_RST_ACK_OFFSET;
  120. /* Reading rst_req again since the communication channel can
  121. * be reset at any time by the PF and all its bits will be
  122. * set to zero.
  123. */
  124. rst_req = (comm_flags & (u32)(1 << COM_CHAN_RST_REQ_OFFSET)) >>
  125. COM_CHAN_RST_REQ_OFFSET;
  126. if (rst_ack == rst_req) {
  127. mlx4_warn(dev, "VF Reset succeed\n");
  128. return 0;
  129. }
  130. cond_resched();
  131. }
  132. mlx4_err(dev, "Fail to send reset over the communication channel\n");
  133. return -ETIMEDOUT;
  134. }
  135. int mlx4_comm_internal_err(u32 slave_read)
  136. {
  137. return (u32)COMM_CHAN_EVENT_INTERNAL_ERR ==
  138. (slave_read & (u32)COMM_CHAN_EVENT_INTERNAL_ERR) ? 1 : 0;
  139. }
  140. void mlx4_enter_error_state(struct mlx4_dev_persistent *persist)
  141. {
  142. int err;
  143. struct mlx4_dev *dev;
  144. if (!mlx4_internal_err_reset)
  145. return;
  146. mutex_lock(&persist->device_state_mutex);
  147. if (persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR)
  148. goto out;
  149. dev = persist->dev;
  150. mlx4_err(dev, "device is going to be reset\n");
  151. if (mlx4_is_slave(dev))
  152. err = mlx4_reset_slave(dev);
  153. else
  154. err = mlx4_reset_master(dev);
  155. if (!err) {
  156. mlx4_err(dev, "device was reset successfully\n");
  157. } else {
  158. /* EEH could have disabled the PCI channel during reset. That's
  159. * recoverable and the PCI error flow will handle it.
  160. */
  161. if (!pci_channel_offline(dev->persist->pdev))
  162. BUG_ON(1);
  163. }
  164. dev->persist->state |= MLX4_DEVICE_STATE_INTERNAL_ERROR;
  165. mutex_unlock(&persist->device_state_mutex);
  166. /* At that step HW was already reset, now notify clients */
  167. mlx4_dispatch_event(dev, MLX4_DEV_EVENT_CATASTROPHIC_ERROR, 0);
  168. mlx4_cmd_wake_completions(dev);
  169. return;
  170. out:
  171. mutex_unlock(&persist->device_state_mutex);
  172. }
  173. static void mlx4_handle_error_state(struct mlx4_dev_persistent *persist)
  174. {
  175. int err = 0;
  176. mlx4_enter_error_state(persist);
  177. mutex_lock(&persist->interface_state_mutex);
  178. if (persist->interface_state & MLX4_INTERFACE_STATE_UP &&
  179. !(persist->interface_state & MLX4_INTERFACE_STATE_DELETION)) {
  180. err = mlx4_restart_one(persist->pdev);
  181. mlx4_info(persist->dev, "mlx4_restart_one was ended, ret=%d\n",
  182. err);
  183. }
  184. mutex_unlock(&persist->interface_state_mutex);
  185. }
  186. static void dump_err_buf(struct mlx4_dev *dev)
  187. {
  188. struct mlx4_priv *priv = mlx4_priv(dev);
  189. int i;
  190. mlx4_err(dev, "Internal error detected:\n");
  191. for (i = 0; i < priv->fw.catas_size; ++i)
  192. mlx4_err(dev, " buf[%02x]: %08x\n",
  193. i, swab32(readl(priv->catas_err.map + i)));
  194. }
  195. static void poll_catas(unsigned long dev_ptr)
  196. {
  197. struct mlx4_dev *dev = (struct mlx4_dev *) dev_ptr;
  198. struct mlx4_priv *priv = mlx4_priv(dev);
  199. u32 slave_read;
  200. if (mlx4_is_slave(dev)) {
  201. slave_read = swab32(readl(&priv->mfunc.comm->slave_read));
  202. if (mlx4_comm_internal_err(slave_read)) {
  203. mlx4_warn(dev, "Internal error detected on the communication channel\n");
  204. goto internal_err;
  205. }
  206. } else if (readl(priv->catas_err.map)) {
  207. dump_err_buf(dev);
  208. goto internal_err;
  209. }
  210. if (dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR) {
  211. mlx4_warn(dev, "Internal error mark was detected on device\n");
  212. goto internal_err;
  213. }
  214. mod_timer(&priv->catas_err.timer,
  215. round_jiffies(jiffies + MLX4_CATAS_POLL_INTERVAL));
  216. return;
  217. internal_err:
  218. if (mlx4_internal_err_reset)
  219. queue_work(dev->persist->catas_wq, &dev->persist->catas_work);
  220. }
  221. static void catas_reset(struct work_struct *work)
  222. {
  223. struct mlx4_dev_persistent *persist =
  224. container_of(work, struct mlx4_dev_persistent,
  225. catas_work);
  226. mlx4_handle_error_state(persist);
  227. }
  228. void mlx4_start_catas_poll(struct mlx4_dev *dev)
  229. {
  230. struct mlx4_priv *priv = mlx4_priv(dev);
  231. phys_addr_t addr;
  232. INIT_LIST_HEAD(&priv->catas_err.list);
  233. init_timer(&priv->catas_err.timer);
  234. priv->catas_err.map = NULL;
  235. if (!mlx4_is_slave(dev)) {
  236. addr = pci_resource_start(dev->persist->pdev,
  237. priv->fw.catas_bar) +
  238. priv->fw.catas_offset;
  239. priv->catas_err.map = ioremap(addr, priv->fw.catas_size * 4);
  240. if (!priv->catas_err.map) {
  241. mlx4_warn(dev, "Failed to map internal error buffer at 0x%llx\n",
  242. (unsigned long long)addr);
  243. return;
  244. }
  245. }
  246. priv->catas_err.timer.data = (unsigned long) dev;
  247. priv->catas_err.timer.function = poll_catas;
  248. priv->catas_err.timer.expires =
  249. round_jiffies(jiffies + MLX4_CATAS_POLL_INTERVAL);
  250. add_timer(&priv->catas_err.timer);
  251. }
  252. void mlx4_stop_catas_poll(struct mlx4_dev *dev)
  253. {
  254. struct mlx4_priv *priv = mlx4_priv(dev);
  255. del_timer_sync(&priv->catas_err.timer);
  256. if (priv->catas_err.map) {
  257. iounmap(priv->catas_err.map);
  258. priv->catas_err.map = NULL;
  259. }
  260. if (dev->persist->interface_state & MLX4_INTERFACE_STATE_DELETION)
  261. flush_workqueue(dev->persist->catas_wq);
  262. }
  263. int mlx4_catas_init(struct mlx4_dev *dev)
  264. {
  265. INIT_WORK(&dev->persist->catas_work, catas_reset);
  266. dev->persist->catas_wq = create_singlethread_workqueue("mlx4_health");
  267. if (!dev->persist->catas_wq)
  268. return -ENOMEM;
  269. return 0;
  270. }
  271. void mlx4_catas_end(struct mlx4_dev *dev)
  272. {
  273. if (dev->persist->catas_wq) {
  274. destroy_workqueue(dev->persist->catas_wq);
  275. dev->persist->catas_wq = NULL;
  276. }
  277. }