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
47 MLX5_DROP_NEW_HEALTH_WORK,
50 static u8 get_nic_interface(struct mlx5_core_dev *dev)
52 return (ioread32be(&dev->iseg->cmdq_addr_l_sz) >> 8) & 3;
55 static void mlx5_trigger_cmd_completions(struct mlx5_core_dev *dev)
60 /* wait for pending handlers to complete */
61 synchronize_irq(dev->priv.msix_arr[MLX5_EQ_VEC_CMD].vector);
62 spin_lock_irqsave(&dev->cmd.alloc_lock, flags);
63 vector = ~dev->cmd.bitmask & ((1ul << (1 << dev->cmd.log_sz)) - 1);
67 vector |= MLX5_TRIGGERED_CMD_COMP;
68 spin_unlock_irqrestore(&dev->cmd.alloc_lock, flags);
70 mlx5_core_dbg(dev, "vector 0x%jx\n", (uintmax_t)vector);
71 mlx5_cmd_comp_handler(dev, vector);
75 spin_unlock_irqrestore(&dev->cmd.alloc_lock, flags);
78 static int in_fatal(struct mlx5_core_dev *dev)
80 struct mlx5_core_health *health = &dev->priv.health;
81 struct mlx5_health_buffer __iomem *h = health->health;
83 if (get_nic_interface(dev) == MLX5_NIC_IFC_DISABLED)
86 if (ioread32be(&h->fw_ver) == 0xffffffff)
92 void mlx5_enter_error_state(struct mlx5_core_dev *dev)
94 mutex_lock(&dev->intf_state_mutex);
95 if (dev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
100 mlx5_core_err(dev, "start\n");
101 if (pci_channel_offline(dev->pdev) || in_fatal(dev)) {
102 dev->state = MLX5_DEVICE_STATE_INTERNAL_ERROR;
103 mlx5_trigger_cmd_completions(dev);
106 mlx5_core_event(dev, MLX5_DEV_EVENT_SYS_ERROR, 0);
107 mlx5_core_err(dev, "end\n");
110 mutex_unlock(&dev->intf_state_mutex);
113 static void mlx5_handle_bad_state(struct mlx5_core_dev *dev)
115 u8 nic_interface = get_nic_interface(dev);
117 switch (nic_interface) {
118 case MLX5_NIC_IFC_FULL:
119 mlx5_core_warn(dev, "Expected to see disabled NIC but it is full driver\n");
122 case MLX5_NIC_IFC_DISABLED:
123 mlx5_core_warn(dev, "starting teardown\n");
126 case MLX5_NIC_IFC_NO_DRAM_NIC:
127 mlx5_core_warn(dev, "Expected to see disabled NIC but it is no dram nic\n");
130 mlx5_core_warn(dev, "Expected to see disabled NIC but it is has invalid value %d\n",
134 mlx5_disable_device(dev);
137 static void health_care(struct work_struct *work)
139 struct mlx5_core_health *health;
140 struct mlx5_core_dev *dev;
141 struct mlx5_priv *priv;
143 health = container_of(work, struct mlx5_core_health, work);
144 priv = container_of(health, struct mlx5_priv, health);
145 dev = container_of(priv, struct mlx5_core_dev, priv);
146 mlx5_core_warn(dev, "handling bad device here\n");
147 mlx5_handle_bad_state(dev);
150 static int get_next_poll_jiffies(void)
154 get_random_bytes(&next, sizeof(next));
156 next += jiffies + MLX5_HEALTH_POLL_INTERVAL;
161 static const char *hsynd_str(u8 synd)
164 case MLX5_HEALTH_SYNDR_FW_ERR:
165 return "firmware internal error";
166 case MLX5_HEALTH_SYNDR_IRISC_ERR:
167 return "irisc not responding";
168 case MLX5_HEALTH_SYNDR_HW_UNRECOVERABLE_ERR:
169 return "unrecoverable hardware error";
170 case MLX5_HEALTH_SYNDR_CRC_ERR:
171 return "firmware CRC error";
172 case MLX5_HEALTH_SYNDR_FETCH_PCI_ERR:
173 return "ICM fetch PCI error";
174 case MLX5_HEALTH_SYNDR_HW_FTL_ERR:
175 return "HW fatal error\n";
176 case MLX5_HEALTH_SYNDR_ASYNC_EQ_OVERRUN_ERR:
177 return "async EQ buffer overrun";
178 case MLX5_HEALTH_SYNDR_EQ_ERR:
180 case MLX5_HEALTH_SYNDR_EQ_INV:
181 return "Invalid EQ referenced";
182 case MLX5_HEALTH_SYNDR_FFSER_ERR:
183 return "FFSER error";
184 case MLX5_HEALTH_SYNDR_HIGH_TEMP:
185 return "High temprature";
187 return "unrecognized error";
191 static void print_health_info(struct mlx5_core_dev *dev)
193 struct mlx5_core_health *health = &dev->priv.health;
194 struct mlx5_health_buffer __iomem *h = health->health;
199 /* If the syndrom is 0, the device is OK and no need to print buffer */
200 if (!ioread8(&h->synd))
203 for (i = 0; i < ARRAY_SIZE(h->assert_var); i++)
204 printf("mlx5_core: INFO: ""assert_var[%d] 0x%08x\n", i, ioread32be(h->assert_var + i));
206 printf("mlx5_core: INFO: ""assert_exit_ptr 0x%08x\n", ioread32be(&h->assert_exit_ptr));
207 printf("mlx5_core: INFO: ""assert_callra 0x%08x\n", ioread32be(&h->assert_callra));
208 snprintf(fw_str, sizeof(fw_str), "%d.%d.%d", fw_rev_maj(dev), fw_rev_min(dev), fw_rev_sub(dev));
209 printf("mlx5_core: INFO: ""fw_ver %s\n", fw_str);
210 printf("mlx5_core: INFO: ""hw_id 0x%08x\n", ioread32be(&h->hw_id));
211 printf("mlx5_core: INFO: ""irisc_index %d\n", ioread8(&h->irisc_index));
212 printf("mlx5_core: INFO: ""synd 0x%x: %s\n", ioread8(&h->synd), hsynd_str(ioread8(&h->synd)));
213 printf("mlx5_core: INFO: ""ext_synd 0x%04x\n", ioread16be(&h->ext_synd));
214 fw = ioread32be(&h->fw_ver);
215 printf("mlx5_core: INFO: ""raw fw_ver 0x%08x\n", fw);
218 static void poll_health(unsigned long data)
220 struct mlx5_core_dev *dev = (struct mlx5_core_dev *)data;
221 struct mlx5_core_health *health = &dev->priv.health;
224 if (dev->state != MLX5_DEVICE_STATE_UP)
227 if (dev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
228 mod_timer(&health->timer, get_next_poll_jiffies());
232 count = ioread32be(health->health_counter);
233 if (count == health->prev)
234 ++health->miss_counter;
236 health->miss_counter = 0;
238 health->prev = count;
239 if (health->miss_counter == MAX_MISSES) {
240 mlx5_core_err(dev, "device's health compromised - reached miss count\n");
241 print_health_info(dev);
243 mod_timer(&health->timer, get_next_poll_jiffies());
246 if (in_fatal(dev) && !health->sick) {
248 print_health_info(dev);
249 spin_lock(&health->wq_lock);
250 if (!test_bit(MLX5_DROP_NEW_HEALTH_WORK, &health->flags))
251 queue_work(health->wq, &health->work);
253 dev_err(&dev->pdev->dev,
254 "new health works are not permitted at this stage\n");
255 spin_unlock(&health->wq_lock);
259 void mlx5_start_health_poll(struct mlx5_core_dev *dev)
261 struct mlx5_core_health *health = &dev->priv.health;
263 init_timer(&health->timer);
265 clear_bit(MLX5_DROP_NEW_HEALTH_WORK, &health->flags);
266 health->health = &dev->iseg->health;
267 health->health_counter = &dev->iseg->health_counter;
269 setup_timer(&health->timer, poll_health, (unsigned long)dev);
270 mod_timer(&health->timer,
271 round_jiffies(jiffies + MLX5_HEALTH_POLL_INTERVAL));
274 void mlx5_stop_health_poll(struct mlx5_core_dev *dev)
276 struct mlx5_core_health *health = &dev->priv.health;
278 del_timer_sync(&health->timer);
281 void mlx5_drain_health_wq(struct mlx5_core_dev *dev)
283 struct mlx5_core_health *health = &dev->priv.health;
285 spin_lock(&health->wq_lock);
286 set_bit(MLX5_DROP_NEW_HEALTH_WORK, &health->flags);
287 spin_unlock(&health->wq_lock);
288 cancel_work_sync(&health->work);
291 void mlx5_health_cleanup(struct mlx5_core_dev *dev)
293 struct mlx5_core_health *health = &dev->priv.health;
295 destroy_workqueue(health->wq);
298 #define HEALTH_NAME "mlx5_health"
299 int mlx5_health_init(struct mlx5_core_dev *dev)
301 struct mlx5_core_health *health;
305 health = &dev->priv.health;
306 len = strlen(HEALTH_NAME) + strlen(dev_name(&dev->pdev->dev));
307 name = kmalloc(len + 1, GFP_KERNEL);
311 snprintf(name, len, "%s:%s", HEALTH_NAME, dev_name(&dev->pdev->dev));
312 health->wq = create_singlethread_workqueue(name);
317 spin_lock_init(&health->wq_lock);
318 INIT_WORK(&health->work, health_care);