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
34#include <linux/workqueue.h>
35
36#include "mlx4.h"
37
38#define	MLX4_CATAS_POLL_INTERVAL	(5 * HZ)
39
40static DEFINE_SPINLOCK(catas_lock);
41
42static LIST_HEAD(catas_list);
43static struct work_struct catas_work;
44
45static int internal_err_reset = 1;
46module_param(internal_err_reset, int, 0644);
47MODULE_PARM_DESC(internal_err_reset,
48		 "Reset device on internal errors if non-zero (default 1)");
49
50static void dump_err_buf(struct mlx4_dev *dev)
51{
52	struct mlx4_priv *priv = mlx4_priv(dev);
53
54	int i;
55
56	mlx4_err(dev, "Internal error detected:\n");
57	for (i = 0; i < priv->fw.catas_size; ++i)
58		mlx4_err(dev, "  buf[%02x]: %08x\n",
59			 i, swab32(readl(priv->catas_err.map + i)));
60}
61
62static void poll_catas(unsigned long dev_ptr)
63{
64	struct mlx4_dev *dev = (struct mlx4_dev *) dev_ptr;
65	struct mlx4_priv *priv = mlx4_priv(dev);
66
67	if (readl(priv->catas_err.map)) {
68		dump_err_buf(dev);
69
70		mlx4_dispatch_event(dev, MLX4_DEV_EVENT_CATASTROPHIC_ERROR, 0);
71
72		if (internal_err_reset) {
73			spin_lock(&catas_lock);
74			list_add(&priv->catas_err.list, &catas_list);
75			spin_unlock(&catas_lock);
76
77			queue_work(mlx4_wq, &catas_work);
78		}
79	} else
80		mod_timer(&priv->catas_err.timer,
81			  round_jiffies(jiffies + MLX4_CATAS_POLL_INTERVAL));
82}
83
84static void catas_reset(struct work_struct *work)
85{
86	struct mlx4_priv *priv, *tmppriv;
87	struct mlx4_dev *dev;
88
89	LIST_HEAD(tlist);
90	int ret;
91
92	if (!mutex_trylock(&drv_mutex))
93		return;
94
95	spin_lock_irq(&catas_lock);
96	list_splice_init(&catas_list, &tlist);
97	spin_unlock_irq(&catas_lock);
98
99	list_for_each_entry_safe(priv, tmppriv, &tlist, catas_err.list) {
100		struct pci_dev *pdev = priv->dev.pdev;
101
102		ret = mlx4_restart_one(priv->dev.pdev);
103		/* 'priv' now is not valid */
104		if (ret)
105			printk(KERN_ERR "mlx4 %s: Reset failed (%d)\n",
106				pci_name(pdev), ret);
107		else {
108			dev  = pci_get_drvdata(pdev);
109			mlx4_dbg(dev, "Reset succeeded\n");
110		}
111	}
112	mutex_unlock(&drv_mutex);
113}
114
115void mlx4_start_catas_poll(struct mlx4_dev *dev)
116{
117	struct mlx4_priv *priv = mlx4_priv(dev);
118	unsigned long addr;
119
120	INIT_LIST_HEAD(&priv->catas_err.list);
121	init_timer(&priv->catas_err.timer);
122	priv->catas_err.map = NULL;
123
124	addr = pci_resource_start(dev->pdev, priv->fw.catas_bar) +
125		priv->fw.catas_offset;
126
127	priv->catas_err.map = ioremap(addr, priv->fw.catas_size * 4);
128	if (!priv->catas_err.map) {
129		mlx4_warn(dev, "Failed to map internal error buffer at 0x%lx\n",
130			  addr);
131		return;
132	}
133
134	priv->catas_err.timer.data     = (unsigned long) dev;
135	priv->catas_err.timer.function = poll_catas;
136	priv->catas_err.timer.expires  =
137		round_jiffies(jiffies + MLX4_CATAS_POLL_INTERVAL);
138	add_timer(&priv->catas_err.timer);
139}
140
141void mlx4_stop_catas_poll(struct mlx4_dev *dev)
142{
143	struct mlx4_priv *priv = mlx4_priv(dev);
144
145	del_timer_sync(&priv->catas_err.timer);
146
147	if (priv->catas_err.map)
148		iounmap(priv->catas_err.map);
149
150	spin_lock_irq(&catas_lock);
151	list_del(&priv->catas_err.list);
152	spin_unlock_irq(&catas_lock);
153}
154
155void  __init mlx4_catas_init(void)
156{
157	INIT_WORK(&catas_work, catas_reset);
158}
159