18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright (c) 2006, 2007, 2008, 2009, 2010 QLogic Corporation.
38c2ecf20Sopenharmony_ci * All rights reserved.
48c2ecf20Sopenharmony_ci * Copyright (c) 2003, 2004, 2005, 2006 PathScale, Inc. All rights reserved.
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * This software is available to you under a choice of one of two
78c2ecf20Sopenharmony_ci * licenses.  You may choose to be licensed under the terms of the GNU
88c2ecf20Sopenharmony_ci * General Public License (GPL) Version 2, available from the file
98c2ecf20Sopenharmony_ci * COPYING in the main directory of this source tree, or the
108c2ecf20Sopenharmony_ci * OpenIB.org BSD license below:
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci *     Redistribution and use in source and binary forms, with or
138c2ecf20Sopenharmony_ci *     without modification, are permitted provided that the following
148c2ecf20Sopenharmony_ci *     conditions are met:
158c2ecf20Sopenharmony_ci *
168c2ecf20Sopenharmony_ci *      - Redistributions of source code must retain the above
178c2ecf20Sopenharmony_ci *        copyright notice, this list of conditions and the following
188c2ecf20Sopenharmony_ci *        disclaimer.
198c2ecf20Sopenharmony_ci *
208c2ecf20Sopenharmony_ci *      - Redistributions in binary form must reproduce the above
218c2ecf20Sopenharmony_ci *        copyright notice, this list of conditions and the following
228c2ecf20Sopenharmony_ci *        disclaimer in the documentation and/or other materials
238c2ecf20Sopenharmony_ci *        provided with the distribution.
248c2ecf20Sopenharmony_ci *
258c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
268c2ecf20Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
278c2ecf20Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
288c2ecf20Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
298c2ecf20Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
308c2ecf20Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
318c2ecf20Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
328c2ecf20Sopenharmony_ci * SOFTWARE.
338c2ecf20Sopenharmony_ci */
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci#include <linux/pci.h>
368c2ecf20Sopenharmony_ci#include <linux/delay.h>
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci#include "qib.h"
398c2ecf20Sopenharmony_ci#include "qib_common.h"
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci/**
428c2ecf20Sopenharmony_ci * qib_format_hwmsg - format a single hwerror message
438c2ecf20Sopenharmony_ci * @msg message buffer
448c2ecf20Sopenharmony_ci * @msgl length of message buffer
458c2ecf20Sopenharmony_ci * @hwmsg message to add to message buffer
468c2ecf20Sopenharmony_ci */
478c2ecf20Sopenharmony_cistatic void qib_format_hwmsg(char *msg, size_t msgl, const char *hwmsg)
488c2ecf20Sopenharmony_ci{
498c2ecf20Sopenharmony_ci	strlcat(msg, "[", msgl);
508c2ecf20Sopenharmony_ci	strlcat(msg, hwmsg, msgl);
518c2ecf20Sopenharmony_ci	strlcat(msg, "]", msgl);
528c2ecf20Sopenharmony_ci}
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci/**
558c2ecf20Sopenharmony_ci * qib_format_hwerrors - format hardware error messages for display
568c2ecf20Sopenharmony_ci * @hwerrs hardware errors bit vector
578c2ecf20Sopenharmony_ci * @hwerrmsgs hardware error descriptions
588c2ecf20Sopenharmony_ci * @nhwerrmsgs number of hwerrmsgs
598c2ecf20Sopenharmony_ci * @msg message buffer
608c2ecf20Sopenharmony_ci * @msgl message buffer length
618c2ecf20Sopenharmony_ci */
628c2ecf20Sopenharmony_civoid qib_format_hwerrors(u64 hwerrs, const struct qib_hwerror_msgs *hwerrmsgs,
638c2ecf20Sopenharmony_ci			 size_t nhwerrmsgs, char *msg, size_t msgl)
648c2ecf20Sopenharmony_ci{
658c2ecf20Sopenharmony_ci	int i;
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	for (i = 0; i < nhwerrmsgs; i++)
688c2ecf20Sopenharmony_ci		if (hwerrs & hwerrmsgs[i].mask)
698c2ecf20Sopenharmony_ci			qib_format_hwmsg(msg, msgl, hwerrmsgs[i].msg);
708c2ecf20Sopenharmony_ci}
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_cistatic void signal_ib_event(struct qib_pportdata *ppd, enum ib_event_type ev)
738c2ecf20Sopenharmony_ci{
748c2ecf20Sopenharmony_ci	struct ib_event event;
758c2ecf20Sopenharmony_ci	struct qib_devdata *dd = ppd->dd;
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	event.device = &dd->verbs_dev.rdi.ibdev;
788c2ecf20Sopenharmony_ci	event.element.port_num = ppd->port;
798c2ecf20Sopenharmony_ci	event.event = ev;
808c2ecf20Sopenharmony_ci	ib_dispatch_event(&event);
818c2ecf20Sopenharmony_ci}
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_civoid qib_handle_e_ibstatuschanged(struct qib_pportdata *ppd, u64 ibcs)
848c2ecf20Sopenharmony_ci{
858c2ecf20Sopenharmony_ci	struct qib_devdata *dd = ppd->dd;
868c2ecf20Sopenharmony_ci	unsigned long flags;
878c2ecf20Sopenharmony_ci	u32 lstate;
888c2ecf20Sopenharmony_ci	u8 ltstate;
898c2ecf20Sopenharmony_ci	enum ib_event_type ev = 0;
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	lstate = dd->f_iblink_state(ibcs); /* linkstate */
928c2ecf20Sopenharmony_ci	ltstate = dd->f_ibphys_portstate(ibcs);
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	/*
958c2ecf20Sopenharmony_ci	 * If linkstate transitions into INIT from any of the various down
968c2ecf20Sopenharmony_ci	 * states, or if it transitions from any of the up (INIT or better)
978c2ecf20Sopenharmony_ci	 * states into any of the down states (except link recovery), then
988c2ecf20Sopenharmony_ci	 * call the chip-specific code to take appropriate actions.
998c2ecf20Sopenharmony_ci	 *
1008c2ecf20Sopenharmony_ci	 * ppd->lflags could be 0 if this is the first time the interrupt
1018c2ecf20Sopenharmony_ci	 * handlers has been called but the link is already up.
1028c2ecf20Sopenharmony_ci	 */
1038c2ecf20Sopenharmony_ci	if (lstate >= IB_PORT_INIT &&
1048c2ecf20Sopenharmony_ci	    (!ppd->lflags || (ppd->lflags & QIBL_LINKDOWN)) &&
1058c2ecf20Sopenharmony_ci	    ltstate == IB_PHYSPORTSTATE_LINKUP) {
1068c2ecf20Sopenharmony_ci		/* transitioned to UP */
1078c2ecf20Sopenharmony_ci		if (dd->f_ib_updown(ppd, 1, ibcs))
1088c2ecf20Sopenharmony_ci			goto skip_ibchange; /* chip-code handled */
1098c2ecf20Sopenharmony_ci	} else if (ppd->lflags & (QIBL_LINKINIT | QIBL_LINKARMED |
1108c2ecf20Sopenharmony_ci		   QIBL_LINKACTIVE | QIBL_IB_FORCE_NOTIFY)) {
1118c2ecf20Sopenharmony_ci		if (ltstate != IB_PHYSPORTSTATE_LINKUP &&
1128c2ecf20Sopenharmony_ci		    ltstate <= IB_PHYSPORTSTATE_CFG_TRAIN &&
1138c2ecf20Sopenharmony_ci		    dd->f_ib_updown(ppd, 0, ibcs))
1148c2ecf20Sopenharmony_ci			goto skip_ibchange; /* chip-code handled */
1158c2ecf20Sopenharmony_ci		qib_set_uevent_bits(ppd, _QIB_EVENT_LINKDOWN_BIT);
1168c2ecf20Sopenharmony_ci	}
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	if (lstate != IB_PORT_DOWN) {
1198c2ecf20Sopenharmony_ci		/* lstate is INIT, ARMED, or ACTIVE */
1208c2ecf20Sopenharmony_ci		if (lstate != IB_PORT_ACTIVE) {
1218c2ecf20Sopenharmony_ci			*ppd->statusp &= ~QIB_STATUS_IB_READY;
1228c2ecf20Sopenharmony_ci			if (ppd->lflags & QIBL_LINKACTIVE)
1238c2ecf20Sopenharmony_ci				ev = IB_EVENT_PORT_ERR;
1248c2ecf20Sopenharmony_ci			spin_lock_irqsave(&ppd->lflags_lock, flags);
1258c2ecf20Sopenharmony_ci			if (lstate == IB_PORT_ARMED) {
1268c2ecf20Sopenharmony_ci				ppd->lflags |= QIBL_LINKARMED | QIBL_LINKV;
1278c2ecf20Sopenharmony_ci				ppd->lflags &= ~(QIBL_LINKINIT |
1288c2ecf20Sopenharmony_ci					QIBL_LINKDOWN | QIBL_LINKACTIVE);
1298c2ecf20Sopenharmony_ci			} else {
1308c2ecf20Sopenharmony_ci				ppd->lflags |= QIBL_LINKINIT | QIBL_LINKV;
1318c2ecf20Sopenharmony_ci				ppd->lflags &= ~(QIBL_LINKARMED |
1328c2ecf20Sopenharmony_ci					QIBL_LINKDOWN | QIBL_LINKACTIVE);
1338c2ecf20Sopenharmony_ci			}
1348c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&ppd->lflags_lock, flags);
1358c2ecf20Sopenharmony_ci			/* start a 75msec timer to clear symbol errors */
1368c2ecf20Sopenharmony_ci			mod_timer(&ppd->symerr_clear_timer,
1378c2ecf20Sopenharmony_ci				  msecs_to_jiffies(75));
1388c2ecf20Sopenharmony_ci		} else if (ltstate == IB_PHYSPORTSTATE_LINKUP &&
1398c2ecf20Sopenharmony_ci			   !(ppd->lflags & QIBL_LINKACTIVE)) {
1408c2ecf20Sopenharmony_ci			/* active, but not active defered */
1418c2ecf20Sopenharmony_ci			qib_hol_up(ppd); /* useful only for 6120 now */
1428c2ecf20Sopenharmony_ci			*ppd->statusp |=
1438c2ecf20Sopenharmony_ci				QIB_STATUS_IB_READY | QIB_STATUS_IB_CONF;
1448c2ecf20Sopenharmony_ci			qib_clear_symerror_on_linkup(&ppd->symerr_clear_timer);
1458c2ecf20Sopenharmony_ci			spin_lock_irqsave(&ppd->lflags_lock, flags);
1468c2ecf20Sopenharmony_ci			ppd->lflags |= QIBL_LINKACTIVE | QIBL_LINKV;
1478c2ecf20Sopenharmony_ci			ppd->lflags &= ~(QIBL_LINKINIT |
1488c2ecf20Sopenharmony_ci				QIBL_LINKDOWN | QIBL_LINKARMED);
1498c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&ppd->lflags_lock, flags);
1508c2ecf20Sopenharmony_ci			if (dd->flags & QIB_HAS_SEND_DMA)
1518c2ecf20Sopenharmony_ci				qib_sdma_process_event(ppd,
1528c2ecf20Sopenharmony_ci					qib_sdma_event_e30_go_running);
1538c2ecf20Sopenharmony_ci			ev = IB_EVENT_PORT_ACTIVE;
1548c2ecf20Sopenharmony_ci			dd->f_setextled(ppd, 1);
1558c2ecf20Sopenharmony_ci		}
1568c2ecf20Sopenharmony_ci	} else { /* down */
1578c2ecf20Sopenharmony_ci		if (ppd->lflags & QIBL_LINKACTIVE)
1588c2ecf20Sopenharmony_ci			ev = IB_EVENT_PORT_ERR;
1598c2ecf20Sopenharmony_ci		spin_lock_irqsave(&ppd->lflags_lock, flags);
1608c2ecf20Sopenharmony_ci		ppd->lflags |= QIBL_LINKDOWN | QIBL_LINKV;
1618c2ecf20Sopenharmony_ci		ppd->lflags &= ~(QIBL_LINKINIT |
1628c2ecf20Sopenharmony_ci				 QIBL_LINKACTIVE | QIBL_LINKARMED);
1638c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&ppd->lflags_lock, flags);
1648c2ecf20Sopenharmony_ci		*ppd->statusp &= ~QIB_STATUS_IB_READY;
1658c2ecf20Sopenharmony_ci	}
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ciskip_ibchange:
1688c2ecf20Sopenharmony_ci	ppd->lastibcstat = ibcs;
1698c2ecf20Sopenharmony_ci	if (ev)
1708c2ecf20Sopenharmony_ci		signal_ib_event(ppd, ev);
1718c2ecf20Sopenharmony_ci}
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_civoid qib_clear_symerror_on_linkup(struct timer_list *t)
1748c2ecf20Sopenharmony_ci{
1758c2ecf20Sopenharmony_ci	struct qib_pportdata *ppd = from_timer(ppd, t, symerr_clear_timer);
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci	if (ppd->lflags & QIBL_LINKACTIVE)
1788c2ecf20Sopenharmony_ci		return;
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci	ppd->ibport_data.z_symbol_error_counter =
1818c2ecf20Sopenharmony_ci		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_IBSYMBOLERR);
1828c2ecf20Sopenharmony_ci}
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci/*
1858c2ecf20Sopenharmony_ci * Handle receive interrupts for user ctxts; this means a user
1868c2ecf20Sopenharmony_ci * process was waiting for a packet to arrive, and didn't want
1878c2ecf20Sopenharmony_ci * to poll.
1888c2ecf20Sopenharmony_ci */
1898c2ecf20Sopenharmony_civoid qib_handle_urcv(struct qib_devdata *dd, u64 ctxtr)
1908c2ecf20Sopenharmony_ci{
1918c2ecf20Sopenharmony_ci	struct qib_ctxtdata *rcd;
1928c2ecf20Sopenharmony_ci	unsigned long flags;
1938c2ecf20Sopenharmony_ci	int i;
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci	spin_lock_irqsave(&dd->uctxt_lock, flags);
1968c2ecf20Sopenharmony_ci	for (i = dd->first_user_ctxt; dd->rcd && i < dd->cfgctxts; i++) {
1978c2ecf20Sopenharmony_ci		if (!(ctxtr & (1ULL << i)))
1988c2ecf20Sopenharmony_ci			continue;
1998c2ecf20Sopenharmony_ci		rcd = dd->rcd[i];
2008c2ecf20Sopenharmony_ci		if (!rcd || !rcd->cnt)
2018c2ecf20Sopenharmony_ci			continue;
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci		if (test_and_clear_bit(QIB_CTXT_WAITING_RCV, &rcd->flag)) {
2048c2ecf20Sopenharmony_ci			wake_up_interruptible(&rcd->wait);
2058c2ecf20Sopenharmony_ci			dd->f_rcvctrl(rcd->ppd, QIB_RCVCTRL_INTRAVAIL_DIS,
2068c2ecf20Sopenharmony_ci				      rcd->ctxt);
2078c2ecf20Sopenharmony_ci		} else if (test_and_clear_bit(QIB_CTXT_WAITING_URG,
2088c2ecf20Sopenharmony_ci					      &rcd->flag)) {
2098c2ecf20Sopenharmony_ci			rcd->urgent++;
2108c2ecf20Sopenharmony_ci			wake_up_interruptible(&rcd->wait);
2118c2ecf20Sopenharmony_ci		}
2128c2ecf20Sopenharmony_ci	}
2138c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&dd->uctxt_lock, flags);
2148c2ecf20Sopenharmony_ci}
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_civoid qib_bad_intrstatus(struct qib_devdata *dd)
2178c2ecf20Sopenharmony_ci{
2188c2ecf20Sopenharmony_ci	static int allbits;
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci	/* separate routine, for better optimization of qib_intr() */
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci	/*
2238c2ecf20Sopenharmony_ci	 * We print the message and disable interrupts, in hope of
2248c2ecf20Sopenharmony_ci	 * having a better chance of debugging the problem.
2258c2ecf20Sopenharmony_ci	 */
2268c2ecf20Sopenharmony_ci	qib_dev_err(dd,
2278c2ecf20Sopenharmony_ci		"Read of chip interrupt status failed disabling interrupts\n");
2288c2ecf20Sopenharmony_ci	if (allbits++) {
2298c2ecf20Sopenharmony_ci		/* disable interrupt delivery, something is very wrong */
2308c2ecf20Sopenharmony_ci		if (allbits == 2)
2318c2ecf20Sopenharmony_ci			dd->f_set_intr_state(dd, 0);
2328c2ecf20Sopenharmony_ci		if (allbits == 3) {
2338c2ecf20Sopenharmony_ci			qib_dev_err(dd,
2348c2ecf20Sopenharmony_ci				"2nd bad interrupt status, unregistering interrupts\n");
2358c2ecf20Sopenharmony_ci			dd->flags |= QIB_BADINTR;
2368c2ecf20Sopenharmony_ci			dd->flags &= ~QIB_INITTED;
2378c2ecf20Sopenharmony_ci			dd->f_free_irq(dd);
2388c2ecf20Sopenharmony_ci		}
2398c2ecf20Sopenharmony_ci	}
2408c2ecf20Sopenharmony_ci}
241