2 * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 #include <linux/kernel.h>
34 #include <linux/module.h>
35 #include <linux/random.h>
36 #include <linux/vmalloc.h>
37 #include <linux/hardirq.h>
38 #include <linux/mlx5/driver.h>
39 #include <linux/mlx5/cmd.h>
40 #include "mlx5_core.h"
43 MLX5_HEALTH_POLL_INTERVAL
= 2 * HZ
,
48 MLX5_HEALTH_SYNDR_FW_ERR
= 0x1,
49 MLX5_HEALTH_SYNDR_IRISC_ERR
= 0x7,
50 MLX5_HEALTH_SYNDR_HW_UNRECOVERABLE_ERR
= 0x8,
51 MLX5_HEALTH_SYNDR_CRC_ERR
= 0x9,
52 MLX5_HEALTH_SYNDR_FETCH_PCI_ERR
= 0xa,
53 MLX5_HEALTH_SYNDR_HW_FTL_ERR
= 0xb,
54 MLX5_HEALTH_SYNDR_ASYNC_EQ_OVERRUN_ERR
= 0xc,
55 MLX5_HEALTH_SYNDR_EQ_ERR
= 0xd,
56 MLX5_HEALTH_SYNDR_EQ_INV
= 0xe,
57 MLX5_HEALTH_SYNDR_FFSER_ERR
= 0xf,
58 MLX5_HEALTH_SYNDR_HIGH_TEMP
= 0x10
62 MLX5_NIC_IFC_FULL
= 0,
63 MLX5_NIC_IFC_DISABLED
= 1,
64 MLX5_NIC_IFC_NO_DRAM_NIC
= 2,
65 MLX5_NIC_IFC_INVALID
= 3
69 MLX5_DROP_NEW_HEALTH_WORK
,
72 static u8
get_nic_state(struct mlx5_core_dev
*dev
)
74 return (ioread32be(&dev
->iseg
->cmdq_addr_l_sz
) >> 8) & 3;
77 static void trigger_cmd_completions(struct mlx5_core_dev
*dev
)
82 /* wait for pending handlers to complete */
83 synchronize_irq(dev
->priv
.msix_arr
[MLX5_EQ_VEC_CMD
].vector
);
84 spin_lock_irqsave(&dev
->cmd
.alloc_lock
, flags
);
85 vector
= ~dev
->cmd
.bitmask
& ((1ul << (1 << dev
->cmd
.log_sz
)) - 1);
89 vector
|= MLX5_TRIGGERED_CMD_COMP
;
90 spin_unlock_irqrestore(&dev
->cmd
.alloc_lock
, flags
);
92 mlx5_core_dbg(dev
, "vector 0x%llx\n", vector
);
93 mlx5_cmd_comp_handler(dev
, vector
, true);
97 spin_unlock_irqrestore(&dev
->cmd
.alloc_lock
, flags
);
100 static int in_fatal(struct mlx5_core_dev
*dev
)
102 struct mlx5_core_health
*health
= &dev
->priv
.health
;
103 struct health_buffer __iomem
*h
= health
->health
;
105 if (get_nic_state(dev
) == MLX5_NIC_IFC_DISABLED
)
108 if (ioread32be(&h
->fw_ver
) == 0xffffffff)
114 void mlx5_enter_error_state(struct mlx5_core_dev
*dev
)
116 mutex_lock(&dev
->intf_state_mutex
);
117 if (dev
->state
== MLX5_DEVICE_STATE_INTERNAL_ERROR
)
120 mlx5_core_err(dev
, "start\n");
121 if (pci_channel_offline(dev
->pdev
) || in_fatal(dev
)) {
122 dev
->state
= MLX5_DEVICE_STATE_INTERNAL_ERROR
;
123 trigger_cmd_completions(dev
);
126 mlx5_core_event(dev
, MLX5_DEV_EVENT_SYS_ERROR
, 0);
127 mlx5_core_err(dev
, "end\n");
130 mutex_unlock(&dev
->intf_state_mutex
);
133 static void mlx5_handle_bad_state(struct mlx5_core_dev
*dev
)
135 u8 nic_interface
= get_nic_state(dev
);
137 switch (nic_interface
) {
138 case MLX5_NIC_IFC_FULL
:
139 mlx5_core_warn(dev
, "Expected to see disabled NIC but it is full driver\n");
142 case MLX5_NIC_IFC_DISABLED
:
143 mlx5_core_warn(dev
, "starting teardown\n");
146 case MLX5_NIC_IFC_NO_DRAM_NIC
:
147 mlx5_core_warn(dev
, "Expected to see disabled NIC but it is no dram nic\n");
150 mlx5_core_warn(dev
, "Expected to see disabled NIC but it is has invalid value %d\n",
154 mlx5_disable_device(dev
);
157 static void health_recover(struct work_struct
*work
)
159 struct mlx5_core_health
*health
;
160 struct delayed_work
*dwork
;
161 struct mlx5_core_dev
*dev
;
162 struct mlx5_priv
*priv
;
165 dwork
= container_of(work
, struct delayed_work
, work
);
166 health
= container_of(dwork
, struct mlx5_core_health
, recover_work
);
167 priv
= container_of(health
, struct mlx5_priv
, health
);
168 dev
= container_of(priv
, struct mlx5_core_dev
, priv
);
170 nic_state
= get_nic_state(dev
);
171 if (nic_state
== MLX5_NIC_IFC_INVALID
) {
172 dev_err(&dev
->pdev
->dev
, "health recovery flow aborted since the nic state is invalid\n");
176 dev_err(&dev
->pdev
->dev
, "starting health recovery flow\n");
177 mlx5_recover_device(dev
);
180 /* How much time to wait until health resetting the driver (in msecs) */
181 #define MLX5_RECOVERY_DELAY_MSECS 60000
182 static void health_care(struct work_struct
*work
)
184 unsigned long recover_delay
= msecs_to_jiffies(MLX5_RECOVERY_DELAY_MSECS
);
185 struct mlx5_core_health
*health
;
186 struct mlx5_core_dev
*dev
;
187 struct mlx5_priv
*priv
;
189 health
= container_of(work
, struct mlx5_core_health
, work
);
190 priv
= container_of(health
, struct mlx5_priv
, health
);
191 dev
= container_of(priv
, struct mlx5_core_dev
, priv
);
192 mlx5_core_warn(dev
, "handling bad device here\n");
193 mlx5_handle_bad_state(dev
);
195 spin_lock(&health
->wq_lock
);
196 if (!test_bit(MLX5_DROP_NEW_HEALTH_WORK
, &health
->flags
))
197 schedule_delayed_work(&health
->recover_work
, recover_delay
);
199 dev_err(&dev
->pdev
->dev
,
200 "new health works are not permitted at this stage\n");
201 spin_unlock(&health
->wq_lock
);
204 static const char *hsynd_str(u8 synd
)
207 case MLX5_HEALTH_SYNDR_FW_ERR
:
208 return "firmware internal error";
209 case MLX5_HEALTH_SYNDR_IRISC_ERR
:
210 return "irisc not responding";
211 case MLX5_HEALTH_SYNDR_HW_UNRECOVERABLE_ERR
:
212 return "unrecoverable hardware error";
213 case MLX5_HEALTH_SYNDR_CRC_ERR
:
214 return "firmware CRC error";
215 case MLX5_HEALTH_SYNDR_FETCH_PCI_ERR
:
216 return "ICM fetch PCI error";
217 case MLX5_HEALTH_SYNDR_HW_FTL_ERR
:
218 return "HW fatal error\n";
219 case MLX5_HEALTH_SYNDR_ASYNC_EQ_OVERRUN_ERR
:
220 return "async EQ buffer overrun";
221 case MLX5_HEALTH_SYNDR_EQ_ERR
:
223 case MLX5_HEALTH_SYNDR_EQ_INV
:
224 return "Invalid EQ referenced";
225 case MLX5_HEALTH_SYNDR_FFSER_ERR
:
226 return "FFSER error";
227 case MLX5_HEALTH_SYNDR_HIGH_TEMP
:
228 return "High temperature";
230 return "unrecognized error";
234 static void print_health_info(struct mlx5_core_dev
*dev
)
236 struct mlx5_core_health
*health
= &dev
->priv
.health
;
237 struct health_buffer __iomem
*h
= health
->health
;
242 /* If the syndrom is 0, the device is OK and no need to print buffer */
243 if (!ioread8(&h
->synd
))
246 for (i
= 0; i
< ARRAY_SIZE(h
->assert_var
); i
++)
247 dev_err(&dev
->pdev
->dev
, "assert_var[%d] 0x%08x\n", i
, ioread32be(h
->assert_var
+ i
));
249 dev_err(&dev
->pdev
->dev
, "assert_exit_ptr 0x%08x\n", ioread32be(&h
->assert_exit_ptr
));
250 dev_err(&dev
->pdev
->dev
, "assert_callra 0x%08x\n", ioread32be(&h
->assert_callra
));
251 sprintf(fw_str
, "%d.%d.%d", fw_rev_maj(dev
), fw_rev_min(dev
), fw_rev_sub(dev
));
252 dev_err(&dev
->pdev
->dev
, "fw_ver %s\n", fw_str
);
253 dev_err(&dev
->pdev
->dev
, "hw_id 0x%08x\n", ioread32be(&h
->hw_id
));
254 dev_err(&dev
->pdev
->dev
, "irisc_index %d\n", ioread8(&h
->irisc_index
));
255 dev_err(&dev
->pdev
->dev
, "synd 0x%x: %s\n", ioread8(&h
->synd
), hsynd_str(ioread8(&h
->synd
)));
256 dev_err(&dev
->pdev
->dev
, "ext_synd 0x%04x\n", ioread16be(&h
->ext_synd
));
257 fw
= ioread32be(&h
->fw_ver
);
258 dev_err(&dev
->pdev
->dev
, "raw fw_ver 0x%08x\n", fw
);
261 static unsigned long get_next_poll_jiffies(void)
265 get_random_bytes(&next
, sizeof(next
));
267 next
+= jiffies
+ MLX5_HEALTH_POLL_INTERVAL
;
272 static void poll_health(unsigned long data
)
274 struct mlx5_core_dev
*dev
= (struct mlx5_core_dev
*)data
;
275 struct mlx5_core_health
*health
= &dev
->priv
.health
;
278 if (dev
->state
== MLX5_DEVICE_STATE_INTERNAL_ERROR
) {
279 mod_timer(&health
->timer
, get_next_poll_jiffies());
283 count
= ioread32be(health
->health_counter
);
284 if (count
== health
->prev
)
285 ++health
->miss_counter
;
287 health
->miss_counter
= 0;
289 health
->prev
= count
;
290 if (health
->miss_counter
== MAX_MISSES
) {
291 dev_err(&dev
->pdev
->dev
, "device's health compromised - reached miss count\n");
292 print_health_info(dev
);
294 mod_timer(&health
->timer
, get_next_poll_jiffies());
297 if (in_fatal(dev
) && !health
->sick
) {
299 print_health_info(dev
);
300 spin_lock(&health
->wq_lock
);
301 if (!test_bit(MLX5_DROP_NEW_HEALTH_WORK
, &health
->flags
))
302 queue_work(health
->wq
, &health
->work
);
304 dev_err(&dev
->pdev
->dev
,
305 "new health works are not permitted at this stage\n");
306 spin_unlock(&health
->wq_lock
);
310 void mlx5_start_health_poll(struct mlx5_core_dev
*dev
)
312 struct mlx5_core_health
*health
= &dev
->priv
.health
;
314 init_timer(&health
->timer
);
316 clear_bit(MLX5_DROP_NEW_HEALTH_WORK
, &health
->flags
);
317 health
->health
= &dev
->iseg
->health
;
318 health
->health_counter
= &dev
->iseg
->health_counter
;
320 health
->timer
.data
= (unsigned long)dev
;
321 health
->timer
.function
= poll_health
;
322 health
->timer
.expires
= round_jiffies(jiffies
+ MLX5_HEALTH_POLL_INTERVAL
);
323 add_timer(&health
->timer
);
326 void mlx5_stop_health_poll(struct mlx5_core_dev
*dev
)
328 struct mlx5_core_health
*health
= &dev
->priv
.health
;
330 del_timer_sync(&health
->timer
);
333 void mlx5_drain_health_wq(struct mlx5_core_dev
*dev
)
335 struct mlx5_core_health
*health
= &dev
->priv
.health
;
337 spin_lock(&health
->wq_lock
);
338 set_bit(MLX5_DROP_NEW_HEALTH_WORK
, &health
->flags
);
339 spin_unlock(&health
->wq_lock
);
340 cancel_delayed_work_sync(&health
->recover_work
);
341 cancel_work_sync(&health
->work
);
344 void mlx5_health_cleanup(struct mlx5_core_dev
*dev
)
346 struct mlx5_core_health
*health
= &dev
->priv
.health
;
348 destroy_workqueue(health
->wq
);
351 int mlx5_health_init(struct mlx5_core_dev
*dev
)
353 struct mlx5_core_health
*health
;
356 health
= &dev
->priv
.health
;
357 name
= kmalloc(64, GFP_KERNEL
);
361 strcpy(name
, "mlx5_health");
362 strcat(name
, dev_name(&dev
->pdev
->dev
));
363 health
->wq
= create_singlethread_workqueue(name
);
367 spin_lock_init(&health
->wq_lock
);
368 INIT_WORK(&health
->work
, health_care
);
369 INIT_DELAYED_WORK(&health
->recover_work
, health_recover
);