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. mlx4_crdump_collect(dev);
  155. err = mlx4_reset_master(dev);
  156. }
  157. if (!err) {
  158. mlx4_err(dev, "device was reset successfully\n");
  159. } else {
  160. /* EEH could have disabled the PCI channel during reset. That's
  161. * recoverable and the PCI error flow will handle it.
  162. */
  163. if (!pci_channel_offline(dev->persist->pdev))
  164. BUG_ON(1);
  165. }
  166. dev->persist->state |= MLX4_DEVICE_STATE_INTERNAL_ERROR;
  167. mutex_unlock(&persist->device_state_mutex);
  168. /* At that step HW was already reset, now notify clients */
  169. mlx4_dispatch_event(dev, MLX4_DEV_EVENT_CATASTROPHIC_ERROR, 0);
  170. mlx4_cmd_wake_completions(dev);
  171. return;
  172. out:
  173. mutex_unlock(&persist->device_state_mutex);
  174. }
  175. static void mlx4_handle_error_state(struct mlx4_dev_persistent *persist)
  176. {
  177. int err = 0;
  178. mlx4_enter_error_state(persist);
  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, false, NULL);
  183. mlx4_info(persist->dev, "mlx4_restart_one was ended, ret=%d\n",
  184. err);
  185. }
  186. mutex_unlock(&persist->interface_state_mutex);
  187. }
  188. static void dump_err_buf(struct mlx4_dev *dev)
  189. {
  190. struct mlx4_priv *priv = mlx4_priv(dev);
  191. int i;
  192. mlx4_err(dev, "Internal error detected:\n");
  193. for (i = 0; i < priv->fw.catas_size; ++i)
  194. mlx4_err(dev, " buf[%02x]: %08x\n",
  195. i, swab32(readl(priv->catas_err.map + i)));
  196. }
  197. static void poll_catas(struct timer_list *t)
  198. {
  199. struct mlx4_priv *priv = from_timer(priv, t, catas_err.timer);
  200. struct mlx4_dev *dev = &priv->dev;
  201. u32 slave_read;
  202. if (mlx4_is_slave(dev)) {
  203. slave_read = swab32(readl(&priv->mfunc.comm->slave_read));
  204. if (mlx4_comm_internal_err(slave_read)) {
  205. mlx4_warn(dev, "Internal error detected on the communication channel\n");
  206. goto internal_err;
  207. }
  208. } else if (readl(priv->catas_err.map)) {
  209. dump_err_buf(dev);
  210. goto internal_err;
  211. }
  212. if (dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR) {
  213. mlx4_warn(dev, "Internal error mark was detected on device\n");
  214. goto internal_err;
  215. }
  216. mod_timer(&priv->catas_err.timer,
  217. round_jiffies(jiffies + MLX4_CATAS_POLL_INTERVAL));
  218. return;
  219. internal_err:
  220. if (mlx4_internal_err_reset)
  221. queue_work(dev->persist->catas_wq, &dev->persist->catas_work);
  222. }
  223. static void catas_reset(struct work_struct *work)
  224. {
  225. struct mlx4_dev_persistent *persist =
  226. container_of(work, struct mlx4_dev_persistent,
  227. catas_work);
  228. mlx4_handle_error_state(persist);
  229. }
  230. void mlx4_start_catas_poll(struct mlx4_dev *dev)
  231. {
  232. struct mlx4_priv *priv = mlx4_priv(dev);
  233. phys_addr_t addr;
  234. INIT_LIST_HEAD(&priv->catas_err.list);
  235. timer_setup(&priv->catas_err.timer, poll_catas, 0);
  236. priv->catas_err.map = NULL;
  237. if (!mlx4_is_slave(dev)) {
  238. addr = pci_resource_start(dev->persist->pdev,
  239. priv->fw.catas_bar) +
  240. priv->fw.catas_offset;
  241. priv->catas_err.map = ioremap(addr, priv->fw.catas_size * 4);
  242. if (!priv->catas_err.map) {
  243. mlx4_warn(dev, "Failed to map internal error buffer at 0x%llx\n",
  244. (unsigned long long)addr);
  245. return;
  246. }
  247. }
  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. }