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. end = msecs_to_jiffies(MLX4_COMM_TIME) + jiffies;
  111. while (time_before(jiffies, end)) {
  112. comm_flags = swab32(readl((__iomem char *)priv->mfunc.comm +
  113. MLX4_COMM_CHAN_FLAGS));
  114. rst_ack = (comm_flags & (u32)(1 << COM_CHAN_RST_ACK_OFFSET)) >>
  115. COM_CHAN_RST_ACK_OFFSET;
  116. /* Reading rst_req again since the communication channel can
  117. * be reset at any time by the PF and all its bits will be
  118. * set to zero.
  119. */
  120. rst_req = (comm_flags & (u32)(1 << COM_CHAN_RST_REQ_OFFSET)) >>
  121. COM_CHAN_RST_REQ_OFFSET;
  122. if (rst_ack == rst_req) {
  123. mlx4_warn(dev, "VF Reset succeed\n");
  124. return 0;
  125. }
  126. cond_resched();
  127. }
  128. mlx4_err(dev, "Fail to send reset over the communication channel\n");
  129. return -ETIMEDOUT;
  130. }
  131. int mlx4_comm_internal_err(u32 slave_read)
  132. {
  133. return (u32)COMM_CHAN_EVENT_INTERNAL_ERR ==
  134. (slave_read & (u32)COMM_CHAN_EVENT_INTERNAL_ERR) ? 1 : 0;
  135. }
  136. void mlx4_enter_error_state(struct mlx4_dev_persistent *persist)
  137. {
  138. int err;
  139. struct mlx4_dev *dev;
  140. if (!mlx4_internal_err_reset)
  141. return;
  142. mutex_lock(&persist->device_state_mutex);
  143. if (persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR)
  144. goto out;
  145. dev = persist->dev;
  146. mlx4_err(dev, "device is going to be reset\n");
  147. if (mlx4_is_slave(dev)) {
  148. err = mlx4_reset_slave(dev);
  149. } else {
  150. mlx4_crdump_collect(dev);
  151. err = mlx4_reset_master(dev);
  152. }
  153. if (!err) {
  154. mlx4_err(dev, "device was reset successfully\n");
  155. } else {
  156. /* EEH could have disabled the PCI channel during reset. That's
  157. * recoverable and the PCI error flow will handle it.
  158. */
  159. if (!pci_channel_offline(dev->persist->pdev))
  160. BUG_ON(1);
  161. }
  162. dev->persist->state |= MLX4_DEVICE_STATE_INTERNAL_ERROR;
  163. mutex_unlock(&persist->device_state_mutex);
  164. /* At that step HW was already reset, now notify clients */
  165. mlx4_dispatch_event(dev, MLX4_DEV_EVENT_CATASTROPHIC_ERROR, 0);
  166. mlx4_cmd_wake_completions(dev);
  167. return;
  168. out:
  169. mutex_unlock(&persist->device_state_mutex);
  170. }
  171. static void mlx4_handle_error_state(struct mlx4_dev_persistent *persist)
  172. {
  173. struct mlx4_dev *dev = persist->dev;
  174. struct devlink *devlink;
  175. int err = 0;
  176. mlx4_enter_error_state(persist);
  177. devlink = priv_to_devlink(mlx4_priv(dev));
  178. devl_lock(devlink);
  179. mutex_lock(&persist->interface_state_mutex);
  180. if (persist->interface_state & MLX4_INTERFACE_STATE_UP &&
  181. !(persist->interface_state & MLX4_INTERFACE_STATE_DELETION)) {
  182. err = mlx4_restart_one(persist->pdev);
  183. mlx4_info(persist->dev, "mlx4_restart_one was ended, ret=%d\n",
  184. err);
  185. }
  186. mutex_unlock(&persist->interface_state_mutex);
  187. devl_unlock(devlink);
  188. }
  189. static void dump_err_buf(struct mlx4_dev *dev)
  190. {
  191. struct mlx4_priv *priv = mlx4_priv(dev);
  192. int i;
  193. mlx4_err(dev, "Internal error detected:\n");
  194. for (i = 0; i < priv->fw.catas_size; ++i)
  195. mlx4_err(dev, " buf[%02x]: %08x\n",
  196. i, swab32(readl(priv->catas_err.map + i)));
  197. }
  198. static void poll_catas(struct timer_list *t)
  199. {
  200. struct mlx4_priv *priv = from_timer(priv, t, catas_err.timer);
  201. struct mlx4_dev *dev = &priv->dev;
  202. u32 slave_read;
  203. if (mlx4_is_slave(dev)) {
  204. slave_read = swab32(readl(&priv->mfunc.comm->slave_read));
  205. if (mlx4_comm_internal_err(slave_read)) {
  206. mlx4_warn(dev, "Internal error detected on the communication channel\n");
  207. goto internal_err;
  208. }
  209. } else if (readl(priv->catas_err.map)) {
  210. dump_err_buf(dev);
  211. goto internal_err;
  212. }
  213. if (dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR) {
  214. mlx4_warn(dev, "Internal error mark was detected on device\n");
  215. goto internal_err;
  216. }
  217. mod_timer(&priv->catas_err.timer,
  218. round_jiffies(jiffies + MLX4_CATAS_POLL_INTERVAL));
  219. return;
  220. internal_err:
  221. if (mlx4_internal_err_reset)
  222. queue_work(dev->persist->catas_wq, &dev->persist->catas_work);
  223. }
  224. static void catas_reset(struct work_struct *work)
  225. {
  226. struct mlx4_dev_persistent *persist =
  227. container_of(work, struct mlx4_dev_persistent,
  228. catas_work);
  229. mlx4_handle_error_state(persist);
  230. }
  231. void mlx4_start_catas_poll(struct mlx4_dev *dev)
  232. {
  233. struct mlx4_priv *priv = mlx4_priv(dev);
  234. phys_addr_t addr;
  235. INIT_LIST_HEAD(&priv->catas_err.list);
  236. timer_setup(&priv->catas_err.timer, poll_catas, 0);
  237. priv->catas_err.map = NULL;
  238. if (!mlx4_is_slave(dev)) {
  239. addr = pci_resource_start(dev->persist->pdev,
  240. priv->fw.catas_bar) +
  241. priv->fw.catas_offset;
  242. priv->catas_err.map = ioremap(addr, priv->fw.catas_size * 4);
  243. if (!priv->catas_err.map) {
  244. mlx4_warn(dev, "Failed to map internal error buffer at 0x%llx\n",
  245. (unsigned long long)addr);
  246. return;
  247. }
  248. }
  249. priv->catas_err.timer.expires =
  250. round_jiffies(jiffies + MLX4_CATAS_POLL_INTERVAL);
  251. add_timer(&priv->catas_err.timer);
  252. }
  253. void mlx4_stop_catas_poll(struct mlx4_dev *dev)
  254. {
  255. struct mlx4_priv *priv = mlx4_priv(dev);
  256. del_timer_sync(&priv->catas_err.timer);
  257. if (priv->catas_err.map) {
  258. iounmap(priv->catas_err.map);
  259. priv->catas_err.map = NULL;
  260. }
  261. if (dev->persist->interface_state & MLX4_INTERFACE_STATE_DELETION)
  262. flush_workqueue(dev->persist->catas_wq);
  263. }
  264. int mlx4_catas_init(struct mlx4_dev *dev)
  265. {
  266. INIT_WORK(&dev->persist->catas_work, catas_reset);
  267. dev->persist->catas_wq = create_singlethread_workqueue("mlx4_health");
  268. if (!dev->persist->catas_wq)
  269. return -ENOMEM;
  270. return 0;
  271. }
  272. void mlx4_catas_end(struct mlx4_dev *dev)
  273. {
  274. if (dev->persist->catas_wq) {
  275. destroy_workqueue(dev->persist->catas_wq);
  276. dev->persist->catas_wq = NULL;
  277. }
  278. }