]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/dev/mlx5/mlx5_core/mlx5_health.c
MFC r356201:
[FreeBSD/stable/10.git] / sys / dev / mlx5 / mlx5_core / mlx5_health.c
1 /*-
2  * Copyright (c) 2013-2015, Mellanox Technologies, Ltd.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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
23  * SUCH DAMAGE.
24  *
25  * $FreeBSD$
26  */
27
28 #include <linux/kernel.h>
29 #include <linux/module.h>
30 #include <linux/random.h>
31 #include <linux/vmalloc.h>
32 #include <dev/mlx5/driver.h>
33 #include <dev/mlx5/mlx5_ifc.h>
34 #include "mlx5_core.h"
35
36 #define MLX5_HEALTH_POLL_INTERVAL       (2 * HZ)
37 #define MAX_MISSES                      3
38
39 static DEFINE_SPINLOCK(health_lock);
40 static LIST_HEAD(health_list);
41 static struct work_struct health_work;
42
43 static void health_care(struct work_struct *work)
44 {
45         struct mlx5_core_health *health, *n;
46         struct mlx5_core_dev *dev;
47         struct mlx5_priv *priv;
48         LIST_HEAD(tlist);
49
50         spin_lock_irq(&health_lock);
51         list_splice_init(&health_list, &tlist);
52
53         spin_unlock_irq(&health_lock);
54
55         list_for_each_entry_safe(health, n, &tlist, list) {
56                 priv = container_of(health, struct mlx5_priv, health);
57                 dev = container_of(priv, struct mlx5_core_dev, priv);
58                 mlx5_core_warn(dev, "handling bad device here\n");
59
60                 spin_lock_irq(&health_lock);
61                 list_del_init(&health->list);
62                 spin_unlock_irq(&health_lock);
63
64                 /* enter error state */
65                 mlx5_enter_error_state(dev);
66         }
67 }
68
69 static const char *hsynd_str(u8 synd)
70 {
71         switch (synd) {
72         case MLX5_HEALTH_SYNDR_FW_ERR:
73                 return "firmware internal error";
74         case MLX5_HEALTH_SYNDR_IRISC_ERR:
75                 return "irisc not responding";
76         case MLX5_HEALTH_SYNDR_CRC_ERR:
77                 return "firmware CRC error";
78         case MLX5_HEALTH_SYNDR_FETCH_PCI_ERR:
79                 return "ICM fetch PCI error";
80         case MLX5_HEALTH_SYNDR_HW_FTL_ERR:
81                 return "HW fatal error\n";
82         case MLX5_HEALTH_SYNDR_ASYNC_EQ_OVERRUN_ERR:
83                 return "async EQ buffer overrun";
84         case MLX5_HEALTH_SYNDR_EQ_ERR:
85                 return "EQ error";
86         case MLX5_HEALTH_SYNDR_FFSER_ERR:
87                 return "FFSER error";
88         default:
89                 return "unrecognized error";
90         }
91 }
92
93 static u16 read_be16(__be16 __iomem *p)
94 {
95         return swab16(readl((__force u16 __iomem *) p));
96 }
97
98 static u32 read_be32(__be32 __iomem *p)
99 {
100         return swab32(readl((__force u32 __iomem *) p));
101 }
102
103 static void print_health_info(struct mlx5_core_dev *dev)
104 {
105         struct mlx5_core_health *health = &dev->priv.health;
106         struct mlx5_health_buffer __iomem *h = health->health;
107         int i;
108
109         for (i = 0; i < ARRAY_SIZE(h->assert_var); i++)
110                 printf("mlx5_core: INFO: ""assert_var[%d] 0x%08x\n", i, read_be32(h->assert_var + i));
111
112         printf("mlx5_core: INFO: ""assert_exit_ptr 0x%08x\n", read_be32(&h->assert_exit_ptr));
113         printf("mlx5_core: INFO: ""assert_callra 0x%08x\n", read_be32(&h->assert_callra));
114         printf("mlx5_core: INFO: ""fw_ver 0x%08x\n", read_be32(&h->fw_ver));
115         printf("mlx5_core: INFO: ""hw_id 0x%08x\n", read_be32(&h->hw_id));
116         printf("mlx5_core: INFO: ""irisc_index %d\n", readb(&h->irisc_index));
117         printf("mlx5_core: INFO: ""synd 0x%x: %s\n", readb(&h->synd), hsynd_str(readb(&h->synd)));
118         printf("mlx5_core: INFO: ""ext_sync 0x%04x\n", read_be16(&h->ext_sync));
119 }
120
121 static void poll_health(unsigned long data)
122 {
123         struct mlx5_core_dev *dev = (struct mlx5_core_dev *)data;
124         struct mlx5_core_health *health = &dev->priv.health;
125         int next;
126         u32 count;
127
128         if (dev->state != MLX5_DEVICE_STATE_UP)
129                 return;
130
131         count = ioread32be(health->health_counter);
132         if (count == health->prev)
133                 ++health->miss_counter;
134         else
135                 health->miss_counter = 0;
136
137         health->prev = count;
138         if (health->miss_counter == MAX_MISSES) {
139                 mlx5_core_err(dev, "device's health compromised\n");
140                 print_health_info(dev);
141                 spin_lock_irq(&health_lock);
142                 list_add_tail(&health->list, &health_list);
143                 spin_unlock_irq(&health_lock);
144
145                 if (!queue_work(mlx5_core_wq, &health_work))
146                         mlx5_core_warn(dev, "failed to queue health work\n");
147         } else {
148                 get_random_bytes(&next, sizeof(next));
149                 next %= HZ;
150                 next += jiffies + MLX5_HEALTH_POLL_INTERVAL;
151                 mod_timer(&health->timer, next);
152         }
153 }
154
155 void mlx5_start_health_poll(struct mlx5_core_dev *dev)
156 {
157         struct mlx5_core_health *health = &dev->priv.health;
158
159         INIT_LIST_HEAD(&health->list);
160         init_timer(&health->timer);
161         health->health = &dev->iseg->health;
162         health->health_counter = &dev->iseg->health_counter;
163
164         setup_timer(&health->timer, poll_health, (unsigned long)dev);
165         mod_timer(&health->timer,
166                   round_jiffies(jiffies + MLX5_HEALTH_POLL_INTERVAL));
167 }
168
169 void mlx5_stop_health_poll(struct mlx5_core_dev *dev)
170 {
171         struct mlx5_core_health *health = &dev->priv.health;
172
173         del_timer_sync(&health->timer);
174
175         spin_lock_irq(&health_lock);
176         if (!list_empty(&health->list))
177                 list_del_init(&health->list);
178         spin_unlock_irq(&health_lock);
179 }
180
181 void mlx5_health_cleanup(void)
182 {
183 }
184
185 void  __init mlx5_health_init(void)
186 {
187
188         INIT_WORK(&health_work, health_care);
189 }