2 * Copyright (c) 2013-2015, Mellanox Technologies, Ltd. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS `AS IS' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <linux/kernel.h>
29 #include <linux/module.h>
30 #include <linux/random.h>
31 #include <linux/vmalloc.h>
32 #include <linux/hardirq.h>
33 #include <dev/mlx5/driver.h>
34 #include <dev/mlx5/mlx5_ifc.h>
35 #include "mlx5_core.h"
37 #define MLX5_HEALTH_POLL_INTERVAL (2 * HZ)
41 MLX5_NIC_IFC_FULL = 0,
42 MLX5_NIC_IFC_DISABLED = 1,
43 MLX5_NIC_IFC_NO_DRAM_NIC = 2
46 static u8 get_nic_interface(struct mlx5_core_dev *dev)
48 return (ioread32be(&dev->iseg->cmdq_addr_l_sz) >> 8) & 3;
51 static void mlx5_trigger_cmd_completions(struct mlx5_core_dev *dev)
56 /* wait for pending handlers to complete */
57 synchronize_irq(dev->priv.msix_arr[MLX5_EQ_VEC_CMD].vector);
58 spin_lock_irqsave(&dev->cmd.alloc_lock, flags);
59 vector = ~dev->cmd.bitmask & ((1ul << (1 << dev->cmd.log_sz)) - 1);
63 vector |= MLX5_TRIGGERED_CMD_COMP;
64 spin_unlock_irqrestore(&dev->cmd.alloc_lock, flags);
66 mlx5_core_dbg(dev, "vector 0x%lx\n", vector);
67 mlx5_cmd_comp_handler(dev, vector);
71 spin_unlock_irqrestore(&dev->cmd.alloc_lock, flags);
74 static int in_fatal(struct mlx5_core_dev *dev)
76 struct mlx5_core_health *health = &dev->priv.health;
77 struct mlx5_health_buffer __iomem *h = health->health;
79 if (get_nic_interface(dev) == MLX5_NIC_IFC_DISABLED)
82 if (ioread32be(&h->fw_ver) == 0xffffffff)
88 void mlx5_enter_error_state(struct mlx5_core_dev *dev)
90 mutex_lock(&dev->intf_state_mutex);
91 if (dev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
96 mlx5_core_err(dev, "start\n");
97 if (pci_channel_offline(dev->pdev) || in_fatal(dev)) {
98 dev->state = MLX5_DEVICE_STATE_INTERNAL_ERROR;
99 mlx5_trigger_cmd_completions(dev);
102 mlx5_core_event(dev, MLX5_DEV_EVENT_SYS_ERROR, 0);
103 mlx5_core_err(dev, "end\n");
106 mutex_unlock(&dev->intf_state_mutex);
109 static void mlx5_handle_bad_state(struct mlx5_core_dev *dev)
111 u8 nic_interface = get_nic_interface(dev);
113 switch (nic_interface) {
114 case MLX5_NIC_IFC_FULL:
115 mlx5_core_warn(dev, "Expected to see disabled NIC but it is full driver\n");
118 case MLX5_NIC_IFC_DISABLED:
119 mlx5_core_warn(dev, "starting teardown\n");
122 case MLX5_NIC_IFC_NO_DRAM_NIC:
123 mlx5_core_warn(dev, "Expected to see disabled NIC but it is no dram nic\n");
126 mlx5_core_warn(dev, "Expected to see disabled NIC but it is has invalid value %d\n",
130 mlx5_disable_device(dev);
133 static void health_care(struct work_struct *work)
135 struct mlx5_core_health *health;
136 struct mlx5_core_dev *dev;
137 struct mlx5_priv *priv;
139 health = container_of(work, struct mlx5_core_health, work);
140 priv = container_of(health, struct mlx5_priv, health);
141 dev = container_of(priv, struct mlx5_core_dev, priv);
142 mlx5_core_warn(dev, "handling bad device here\n");
143 mlx5_handle_bad_state(dev);
146 static int get_next_poll_jiffies(void)
150 get_random_bytes(&next, sizeof(next));
152 next += jiffies + MLX5_HEALTH_POLL_INTERVAL;
157 static const char *hsynd_str(u8 synd)
160 case MLX5_HEALTH_SYNDR_FW_ERR:
161 return "firmware internal error";
162 case MLX5_HEALTH_SYNDR_IRISC_ERR:
163 return "irisc not responding";
164 case MLX5_HEALTH_SYNDR_HW_UNRECOVERABLE_ERR:
165 return "unrecoverable hardware error";
166 case MLX5_HEALTH_SYNDR_CRC_ERR:
167 return "firmware CRC error";
168 case MLX5_HEALTH_SYNDR_FETCH_PCI_ERR:
169 return "ICM fetch PCI error";
170 case MLX5_HEALTH_SYNDR_HW_FTL_ERR:
171 return "HW fatal error\n";
172 case MLX5_HEALTH_SYNDR_ASYNC_EQ_OVERRUN_ERR:
173 return "async EQ buffer overrun";
174 case MLX5_HEALTH_SYNDR_EQ_ERR:
176 case MLX5_HEALTH_SYNDR_EQ_INV:
177 return "Invalid EQ referenced";
178 case MLX5_HEALTH_SYNDR_FFSER_ERR:
179 return "FFSER error";
180 case MLX5_HEALTH_SYNDR_HIGH_TEMP:
181 return "High temprature";
183 return "unrecognized error";
187 static void print_health_info(struct mlx5_core_dev *dev)
189 struct mlx5_core_health *health = &dev->priv.health;
190 struct mlx5_health_buffer __iomem *h = health->health;
195 /* If the syndrom is 0, the device is OK and no need to print buffer */
196 if (!ioread8(&h->synd))
199 for (i = 0; i < ARRAY_SIZE(h->assert_var); i++)
200 printf("mlx5_core: INFO: ""assert_var[%d] 0x%08x\n", i, ioread32be(h->assert_var + i));
202 printf("mlx5_core: INFO: ""assert_exit_ptr 0x%08x\n", ioread32be(&h->assert_exit_ptr));
203 printf("mlx5_core: INFO: ""assert_callra 0x%08x\n", ioread32be(&h->assert_callra));
204 snprintf(fw_str, sizeof(fw_str), "%d.%d.%d", fw_rev_maj(dev), fw_rev_min(dev), fw_rev_sub(dev));
205 printf("mlx5_core: INFO: ""fw_ver %s\n", fw_str);
206 printf("mlx5_core: INFO: ""hw_id 0x%08x\n", ioread32be(&h->hw_id));
207 printf("mlx5_core: INFO: ""irisc_index %d\n", ioread8(&h->irisc_index));
208 printf("mlx5_core: INFO: ""synd 0x%x: %s\n", ioread8(&h->synd), hsynd_str(ioread8(&h->synd)));
209 printf("mlx5_core: INFO: ""ext_synd 0x%04x\n", ioread16be(&h->ext_synd));
210 fw = ioread32be(&h->fw_ver);
211 printf("mlx5_core: INFO: ""raw fw_ver 0x%08x\n", fw);
214 static void poll_health(unsigned long data)
216 struct mlx5_core_dev *dev = (struct mlx5_core_dev *)data;
217 struct mlx5_core_health *health = &dev->priv.health;
220 if (dev->state != MLX5_DEVICE_STATE_UP)
223 if (dev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
224 mod_timer(&health->timer, get_next_poll_jiffies());
228 count = ioread32be(health->health_counter);
229 if (count == health->prev)
230 ++health->miss_counter;
232 health->miss_counter = 0;
234 health->prev = count;
235 if (health->miss_counter == MAX_MISSES) {
236 mlx5_core_err(dev, "device's health compromised - reached miss count\n");
237 print_health_info(dev);
239 mod_timer(&health->timer, get_next_poll_jiffies());
242 if (in_fatal(dev) && !health->sick) {
244 print_health_info(dev);
245 queue_work(health->wq, &health->work);
249 void mlx5_start_health_poll(struct mlx5_core_dev *dev)
251 struct mlx5_core_health *health = &dev->priv.health;
253 init_timer(&health->timer);
255 health->health = &dev->iseg->health;
256 health->health_counter = &dev->iseg->health_counter;
258 setup_timer(&health->timer, poll_health, (unsigned long)dev);
259 mod_timer(&health->timer,
260 round_jiffies(jiffies + MLX5_HEALTH_POLL_INTERVAL));
263 void mlx5_stop_health_poll(struct mlx5_core_dev *dev)
265 struct mlx5_core_health *health = &dev->priv.health;
267 del_timer_sync(&health->timer);
270 void mlx5_health_cleanup(struct mlx5_core_dev *dev)
272 struct mlx5_core_health *health = &dev->priv.health;
274 destroy_workqueue(health->wq);
277 #define HEALTH_NAME "mlx5_health"
278 int mlx5_health_init(struct mlx5_core_dev *dev)
280 struct mlx5_core_health *health;
284 health = &dev->priv.health;
285 len = strlen(HEALTH_NAME) + strlen(dev_name(&dev->pdev->dev));
286 name = kmalloc(len + 1, GFP_KERNEL);
290 snprintf(name, len, "%s:%s", HEALTH_NAME, dev_name(&dev->pdev->dev));
291 health->wq = create_singlethread_workqueue(name);
296 INIT_WORK(&health->work, health_care);