1219820Sjeff/*
2219820Sjeff * Copyright (c) 2005 Cisco Systems.  All rights reserved.
3219820Sjeff *
4219820Sjeff * This software is available to you under a choice of one of two
5219820Sjeff * licenses.  You may choose to be licensed under the terms of the GNU
6219820Sjeff * General Public License (GPL) Version 2, available from the file
7219820Sjeff * COPYING in the main directory of this source tree, or the
8219820Sjeff * OpenIB.org BSD license below:
9219820Sjeff *
10219820Sjeff *     Redistribution and use in source and binary forms, with or
11219820Sjeff *     without modification, are permitted provided that the following
12219820Sjeff *     conditions are met:
13219820Sjeff *
14219820Sjeff *      - Redistributions of source code must retain the above
15219820Sjeff *        copyright notice, this list of conditions and the following
16219820Sjeff *        disclaimer.
17219820Sjeff *
18219820Sjeff *      - Redistributions in binary form must reproduce the above
19219820Sjeff *        copyright notice, this list of conditions and the following
20219820Sjeff *        disclaimer in the documentation and/or other materials
21219820Sjeff *        provided with the distribution.
22219820Sjeff *
23219820Sjeff * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24219820Sjeff * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25219820Sjeff * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26219820Sjeff * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27219820Sjeff * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28219820Sjeff * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29219820Sjeff * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30219820Sjeff * SOFTWARE.
31219820Sjeff */
32219820Sjeff
33219820Sjeff#include <linux/jiffies.h>
34219820Sjeff#include <linux/timer.h>
35219820Sjeff#include <linux/workqueue.h>
36219820Sjeff
37219820Sjeff#include "mthca_dev.h"
38219820Sjeff
39219820Sjeffenum {
40219820Sjeff	MTHCA_CATAS_TYPE_INTERNAL	= 0,
41219820Sjeff	MTHCA_CATAS_TYPE_UPLINK		= 3,
42219820Sjeff	MTHCA_CATAS_TYPE_DDR		= 4,
43219820Sjeff	MTHCA_CATAS_TYPE_PARITY		= 5,
44219820Sjeff};
45219820Sjeff
46219820Sjeff#define	MTHCA_CATAS_POLL_INTERVAL	(5 * HZ)
47219820Sjeff
48219820Sjeffstatic DEFINE_SPINLOCK(catas_lock);
49219820Sjeff
50219820Sjeffstatic LIST_HEAD(catas_list);
51219820Sjeffstatic struct workqueue_struct *catas_wq;
52219820Sjeffstatic struct work_struct catas_work;
53219820Sjeff
54219820Sjeffstatic int catas_reset_disable;
55219820Sjeffmodule_param_named(catas_reset_disable, catas_reset_disable, int, 0644);
56219820SjeffMODULE_PARM_DESC(catas_reset_disable, "disable reset on catastrophic event if nonzero");
57219820Sjeff
58219820Sjeffstatic void catas_reset(struct work_struct *work)
59219820Sjeff{
60219820Sjeff	struct mthca_dev *dev, *tmpdev;
61219820Sjeff	LIST_HEAD(tlist);
62219820Sjeff	int ret;
63219820Sjeff
64219820Sjeff	mutex_lock(&mthca_device_mutex);
65219820Sjeff
66219820Sjeff	spin_lock_irq(&catas_lock);
67219820Sjeff	list_splice_init(&catas_list, &tlist);
68219820Sjeff	spin_unlock_irq(&catas_lock);
69219820Sjeff
70219820Sjeff	list_for_each_entry_safe(dev, tmpdev, &tlist, catas_err.list) {
71219820Sjeff		struct pci_dev *pdev = dev->pdev;
72219820Sjeff		ret = __mthca_restart_one(dev->pdev);
73219820Sjeff		/* 'dev' now is not valid */
74219820Sjeff		if (ret)
75219820Sjeff			printk(KERN_ERR "mthca %s: Reset failed (%d)\n",
76219820Sjeff			       pci_name(pdev), ret);
77219820Sjeff		else {
78219820Sjeff			struct mthca_dev *d = pci_get_drvdata(pdev);
79219820Sjeff			mthca_dbg(d, "Reset succeeded\n");
80219820Sjeff		}
81219820Sjeff	}
82219820Sjeff
83219820Sjeff	mutex_unlock(&mthca_device_mutex);
84219820Sjeff}
85219820Sjeff
86219820Sjeffstatic void handle_catas(struct mthca_dev *dev)
87219820Sjeff{
88219820Sjeff	struct ib_event event;
89219820Sjeff	unsigned long flags;
90219820Sjeff	const char *type;
91219820Sjeff	int i;
92219820Sjeff
93219820Sjeff	event.device = &dev->ib_dev;
94219820Sjeff	event.event  = IB_EVENT_DEVICE_FATAL;
95219820Sjeff	event.element.port_num = 0;
96219820Sjeff	dev->active = 0;
97219820Sjeff
98219820Sjeff	ib_dispatch_event(&event);
99219820Sjeff
100219820Sjeff	switch (swab32(readl(dev->catas_err.map)) >> 24) {
101219820Sjeff	case MTHCA_CATAS_TYPE_INTERNAL:
102219820Sjeff		type = "internal error";
103219820Sjeff		break;
104219820Sjeff	case MTHCA_CATAS_TYPE_UPLINK:
105219820Sjeff		type = "uplink bus error";
106219820Sjeff		break;
107219820Sjeff	case MTHCA_CATAS_TYPE_DDR:
108219820Sjeff		type = "DDR data error";
109219820Sjeff		break;
110219820Sjeff	case MTHCA_CATAS_TYPE_PARITY:
111219820Sjeff		type = "internal parity error";
112219820Sjeff		break;
113219820Sjeff	default:
114219820Sjeff		type = "unknown error";
115219820Sjeff		break;
116219820Sjeff	}
117219820Sjeff
118219820Sjeff	mthca_err(dev, "Catastrophic error detected: %s\n", type);
119219820Sjeff	for (i = 0; i < dev->catas_err.size; ++i)
120219820Sjeff		mthca_err(dev, "  buf[%02x]: %08x\n",
121219820Sjeff			  i, swab32(readl(dev->catas_err.map + i)));
122219820Sjeff
123219820Sjeff	if (catas_reset_disable)
124219820Sjeff		return;
125219820Sjeff
126219820Sjeff	spin_lock_irqsave(&catas_lock, flags);
127219820Sjeff	list_add(&dev->catas_err.list, &catas_list);
128219820Sjeff	queue_work(catas_wq, &catas_work);
129219820Sjeff	spin_unlock_irqrestore(&catas_lock, flags);
130219820Sjeff}
131219820Sjeff
132219820Sjeffstatic void poll_catas(unsigned long dev_ptr)
133219820Sjeff{
134219820Sjeff	struct mthca_dev *dev = (struct mthca_dev *) dev_ptr;
135219820Sjeff	int i;
136219820Sjeff
137219820Sjeff	for (i = 0; i < dev->catas_err.size; ++i)
138219820Sjeff		if (readl(dev->catas_err.map + i)) {
139219820Sjeff			handle_catas(dev);
140219820Sjeff			return;
141219820Sjeff		}
142219820Sjeff
143219820Sjeff	mod_timer(&dev->catas_err.timer,
144219820Sjeff		  round_jiffies(jiffies + MTHCA_CATAS_POLL_INTERVAL));
145219820Sjeff}
146219820Sjeff
147219820Sjeffvoid mthca_start_catas_poll(struct mthca_dev *dev)
148219820Sjeff{
149219820Sjeff	unsigned long addr;
150219820Sjeff
151219820Sjeff	init_timer(&dev->catas_err.timer);
152219820Sjeff	dev->catas_err.map  = NULL;
153219820Sjeff
154219820Sjeff	addr = pci_resource_start(dev->pdev, 0) +
155219820Sjeff		((pci_resource_len(dev->pdev, 0) - 1) &
156219820Sjeff		 dev->catas_err.addr);
157219820Sjeff
158219820Sjeff	dev->catas_err.map = ioremap(addr, dev->catas_err.size * 4);
159219820Sjeff	if (!dev->catas_err.map) {
160219820Sjeff		mthca_warn(dev, "couldn't map catastrophic error region "
161219820Sjeff			   "at 0x%lx/0x%x\n", addr, dev->catas_err.size * 4);
162219820Sjeff		return;
163219820Sjeff	}
164219820Sjeff
165219820Sjeff	dev->catas_err.timer.data     = (unsigned long) dev;
166219820Sjeff	dev->catas_err.timer.function = poll_catas;
167219820Sjeff	dev->catas_err.timer.expires  = jiffies + MTHCA_CATAS_POLL_INTERVAL;
168219820Sjeff	INIT_LIST_HEAD(&dev->catas_err.list);
169219820Sjeff	add_timer(&dev->catas_err.timer);
170219820Sjeff}
171219820Sjeff
172219820Sjeffvoid mthca_stop_catas_poll(struct mthca_dev *dev)
173219820Sjeff{
174219820Sjeff	del_timer_sync(&dev->catas_err.timer);
175219820Sjeff
176219820Sjeff	if (dev->catas_err.map)
177219820Sjeff		iounmap(dev->catas_err.map);
178219820Sjeff
179219820Sjeff	spin_lock_irq(&catas_lock);
180219820Sjeff	list_del(&dev->catas_err.list);
181219820Sjeff	spin_unlock_irq(&catas_lock);
182219820Sjeff}
183219820Sjeff
184219820Sjeffint __init mthca_catas_init(void)
185219820Sjeff{
186219820Sjeff	INIT_WORK(&catas_work, catas_reset);
187219820Sjeff
188219820Sjeff	catas_wq = create_singlethread_workqueue("mthcacatas");
189219820Sjeff	if (!catas_wq)
190219820Sjeff		return -ENOMEM;
191219820Sjeff
192219820Sjeff	return 0;
193219820Sjeff}
194219820Sjeff
195219820Sjeffvoid mthca_catas_cleanup(void)
196219820Sjeff{
197219820Sjeff	destroy_workqueue(catas_wq);
198219820Sjeff}
199