162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * Copyright (c) 2006, 2007, 2008, 2009, 2010 QLogic Corporation.
362306a36Sopenharmony_ci * All rights reserved.
462306a36Sopenharmony_ci * Copyright (c) 2003, 2004, 2005, 2006 PathScale, Inc. All rights reserved.
562306a36Sopenharmony_ci *
662306a36Sopenharmony_ci * This software is available to you under a choice of one of two
762306a36Sopenharmony_ci * licenses.  You may choose to be licensed under the terms of the GNU
862306a36Sopenharmony_ci * General Public License (GPL) Version 2, available from the file
962306a36Sopenharmony_ci * COPYING in the main directory of this source tree, or the
1062306a36Sopenharmony_ci * OpenIB.org BSD license below:
1162306a36Sopenharmony_ci *
1262306a36Sopenharmony_ci *     Redistribution and use in source and binary forms, with or
1362306a36Sopenharmony_ci *     without modification, are permitted provided that the following
1462306a36Sopenharmony_ci *     conditions are met:
1562306a36Sopenharmony_ci *
1662306a36Sopenharmony_ci *      - Redistributions of source code must retain the above
1762306a36Sopenharmony_ci *        copyright notice, this list of conditions and the following
1862306a36Sopenharmony_ci *        disclaimer.
1962306a36Sopenharmony_ci *
2062306a36Sopenharmony_ci *      - Redistributions in binary form must reproduce the above
2162306a36Sopenharmony_ci *        copyright notice, this list of conditions and the following
2262306a36Sopenharmony_ci *        disclaimer in the documentation and/or other materials
2362306a36Sopenharmony_ci *        provided with the distribution.
2462306a36Sopenharmony_ci *
2562306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
2662306a36Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
2762306a36Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
2862306a36Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
2962306a36Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
3062306a36Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
3162306a36Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3262306a36Sopenharmony_ci * SOFTWARE.
3362306a36Sopenharmony_ci */
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci#include <linux/pci.h>
3662306a36Sopenharmony_ci#include <linux/delay.h>
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci#include "qib.h"
3962306a36Sopenharmony_ci#include "qib_common.h"
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci/**
4262306a36Sopenharmony_ci * qib_format_hwmsg - format a single hwerror message
4362306a36Sopenharmony_ci * @msg: message buffer
4462306a36Sopenharmony_ci * @msgl: length of message buffer
4562306a36Sopenharmony_ci * @hwmsg: message to add to message buffer
4662306a36Sopenharmony_ci */
4762306a36Sopenharmony_cistatic void qib_format_hwmsg(char *msg, size_t msgl, const char *hwmsg)
4862306a36Sopenharmony_ci{
4962306a36Sopenharmony_ci	strlcat(msg, "[", msgl);
5062306a36Sopenharmony_ci	strlcat(msg, hwmsg, msgl);
5162306a36Sopenharmony_ci	strlcat(msg, "]", msgl);
5262306a36Sopenharmony_ci}
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ci/**
5562306a36Sopenharmony_ci * qib_format_hwerrors - format hardware error messages for display
5662306a36Sopenharmony_ci * @hwerrs: hardware errors bit vector
5762306a36Sopenharmony_ci * @hwerrmsgs: hardware error descriptions
5862306a36Sopenharmony_ci * @nhwerrmsgs: number of hwerrmsgs
5962306a36Sopenharmony_ci * @msg: message buffer
6062306a36Sopenharmony_ci * @msgl: message buffer length
6162306a36Sopenharmony_ci */
6262306a36Sopenharmony_civoid qib_format_hwerrors(u64 hwerrs, const struct qib_hwerror_msgs *hwerrmsgs,
6362306a36Sopenharmony_ci			 size_t nhwerrmsgs, char *msg, size_t msgl)
6462306a36Sopenharmony_ci{
6562306a36Sopenharmony_ci	int i;
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci	for (i = 0; i < nhwerrmsgs; i++)
6862306a36Sopenharmony_ci		if (hwerrs & hwerrmsgs[i].mask)
6962306a36Sopenharmony_ci			qib_format_hwmsg(msg, msgl, hwerrmsgs[i].msg);
7062306a36Sopenharmony_ci}
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_cistatic void signal_ib_event(struct qib_pportdata *ppd, enum ib_event_type ev)
7362306a36Sopenharmony_ci{
7462306a36Sopenharmony_ci	struct ib_event event;
7562306a36Sopenharmony_ci	struct qib_devdata *dd = ppd->dd;
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci	event.device = &dd->verbs_dev.rdi.ibdev;
7862306a36Sopenharmony_ci	event.element.port_num = ppd->port;
7962306a36Sopenharmony_ci	event.event = ev;
8062306a36Sopenharmony_ci	ib_dispatch_event(&event);
8162306a36Sopenharmony_ci}
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_civoid qib_handle_e_ibstatuschanged(struct qib_pportdata *ppd, u64 ibcs)
8462306a36Sopenharmony_ci{
8562306a36Sopenharmony_ci	struct qib_devdata *dd = ppd->dd;
8662306a36Sopenharmony_ci	unsigned long flags;
8762306a36Sopenharmony_ci	u32 lstate;
8862306a36Sopenharmony_ci	u8 ltstate;
8962306a36Sopenharmony_ci	enum ib_event_type ev = 0;
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_ci	lstate = dd->f_iblink_state(ibcs); /* linkstate */
9262306a36Sopenharmony_ci	ltstate = dd->f_ibphys_portstate(ibcs);
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ci	/*
9562306a36Sopenharmony_ci	 * If linkstate transitions into INIT from any of the various down
9662306a36Sopenharmony_ci	 * states, or if it transitions from any of the up (INIT or better)
9762306a36Sopenharmony_ci	 * states into any of the down states (except link recovery), then
9862306a36Sopenharmony_ci	 * call the chip-specific code to take appropriate actions.
9962306a36Sopenharmony_ci	 *
10062306a36Sopenharmony_ci	 * ppd->lflags could be 0 if this is the first time the interrupt
10162306a36Sopenharmony_ci	 * handlers has been called but the link is already up.
10262306a36Sopenharmony_ci	 */
10362306a36Sopenharmony_ci	if (lstate >= IB_PORT_INIT &&
10462306a36Sopenharmony_ci	    (!ppd->lflags || (ppd->lflags & QIBL_LINKDOWN)) &&
10562306a36Sopenharmony_ci	    ltstate == IB_PHYSPORTSTATE_LINKUP) {
10662306a36Sopenharmony_ci		/* transitioned to UP */
10762306a36Sopenharmony_ci		if (dd->f_ib_updown(ppd, 1, ibcs))
10862306a36Sopenharmony_ci			goto skip_ibchange; /* chip-code handled */
10962306a36Sopenharmony_ci	} else if (ppd->lflags & (QIBL_LINKINIT | QIBL_LINKARMED |
11062306a36Sopenharmony_ci		   QIBL_LINKACTIVE | QIBL_IB_FORCE_NOTIFY)) {
11162306a36Sopenharmony_ci		if (ltstate != IB_PHYSPORTSTATE_LINKUP &&
11262306a36Sopenharmony_ci		    ltstate <= IB_PHYSPORTSTATE_CFG_TRAIN &&
11362306a36Sopenharmony_ci		    dd->f_ib_updown(ppd, 0, ibcs))
11462306a36Sopenharmony_ci			goto skip_ibchange; /* chip-code handled */
11562306a36Sopenharmony_ci		qib_set_uevent_bits(ppd, _QIB_EVENT_LINKDOWN_BIT);
11662306a36Sopenharmony_ci	}
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_ci	if (lstate != IB_PORT_DOWN) {
11962306a36Sopenharmony_ci		/* lstate is INIT, ARMED, or ACTIVE */
12062306a36Sopenharmony_ci		if (lstate != IB_PORT_ACTIVE) {
12162306a36Sopenharmony_ci			*ppd->statusp &= ~QIB_STATUS_IB_READY;
12262306a36Sopenharmony_ci			if (ppd->lflags & QIBL_LINKACTIVE)
12362306a36Sopenharmony_ci				ev = IB_EVENT_PORT_ERR;
12462306a36Sopenharmony_ci			spin_lock_irqsave(&ppd->lflags_lock, flags);
12562306a36Sopenharmony_ci			if (lstate == IB_PORT_ARMED) {
12662306a36Sopenharmony_ci				ppd->lflags |= QIBL_LINKARMED | QIBL_LINKV;
12762306a36Sopenharmony_ci				ppd->lflags &= ~(QIBL_LINKINIT |
12862306a36Sopenharmony_ci					QIBL_LINKDOWN | QIBL_LINKACTIVE);
12962306a36Sopenharmony_ci			} else {
13062306a36Sopenharmony_ci				ppd->lflags |= QIBL_LINKINIT | QIBL_LINKV;
13162306a36Sopenharmony_ci				ppd->lflags &= ~(QIBL_LINKARMED |
13262306a36Sopenharmony_ci					QIBL_LINKDOWN | QIBL_LINKACTIVE);
13362306a36Sopenharmony_ci			}
13462306a36Sopenharmony_ci			spin_unlock_irqrestore(&ppd->lflags_lock, flags);
13562306a36Sopenharmony_ci			/* start a 75msec timer to clear symbol errors */
13662306a36Sopenharmony_ci			mod_timer(&ppd->symerr_clear_timer,
13762306a36Sopenharmony_ci				  msecs_to_jiffies(75));
13862306a36Sopenharmony_ci		} else if (ltstate == IB_PHYSPORTSTATE_LINKUP &&
13962306a36Sopenharmony_ci			   !(ppd->lflags & QIBL_LINKACTIVE)) {
14062306a36Sopenharmony_ci			/* active, but not active defered */
14162306a36Sopenharmony_ci			qib_hol_up(ppd); /* useful only for 6120 now */
14262306a36Sopenharmony_ci			*ppd->statusp |=
14362306a36Sopenharmony_ci				QIB_STATUS_IB_READY | QIB_STATUS_IB_CONF;
14462306a36Sopenharmony_ci			qib_clear_symerror_on_linkup(&ppd->symerr_clear_timer);
14562306a36Sopenharmony_ci			spin_lock_irqsave(&ppd->lflags_lock, flags);
14662306a36Sopenharmony_ci			ppd->lflags |= QIBL_LINKACTIVE | QIBL_LINKV;
14762306a36Sopenharmony_ci			ppd->lflags &= ~(QIBL_LINKINIT |
14862306a36Sopenharmony_ci				QIBL_LINKDOWN | QIBL_LINKARMED);
14962306a36Sopenharmony_ci			spin_unlock_irqrestore(&ppd->lflags_lock, flags);
15062306a36Sopenharmony_ci			if (dd->flags & QIB_HAS_SEND_DMA)
15162306a36Sopenharmony_ci				qib_sdma_process_event(ppd,
15262306a36Sopenharmony_ci					qib_sdma_event_e30_go_running);
15362306a36Sopenharmony_ci			ev = IB_EVENT_PORT_ACTIVE;
15462306a36Sopenharmony_ci			dd->f_setextled(ppd, 1);
15562306a36Sopenharmony_ci		}
15662306a36Sopenharmony_ci	} else { /* down */
15762306a36Sopenharmony_ci		if (ppd->lflags & QIBL_LINKACTIVE)
15862306a36Sopenharmony_ci			ev = IB_EVENT_PORT_ERR;
15962306a36Sopenharmony_ci		spin_lock_irqsave(&ppd->lflags_lock, flags);
16062306a36Sopenharmony_ci		ppd->lflags |= QIBL_LINKDOWN | QIBL_LINKV;
16162306a36Sopenharmony_ci		ppd->lflags &= ~(QIBL_LINKINIT |
16262306a36Sopenharmony_ci				 QIBL_LINKACTIVE | QIBL_LINKARMED);
16362306a36Sopenharmony_ci		spin_unlock_irqrestore(&ppd->lflags_lock, flags);
16462306a36Sopenharmony_ci		*ppd->statusp &= ~QIB_STATUS_IB_READY;
16562306a36Sopenharmony_ci	}
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_ciskip_ibchange:
16862306a36Sopenharmony_ci	ppd->lastibcstat = ibcs;
16962306a36Sopenharmony_ci	if (ev)
17062306a36Sopenharmony_ci		signal_ib_event(ppd, ev);
17162306a36Sopenharmony_ci}
17262306a36Sopenharmony_ci
17362306a36Sopenharmony_civoid qib_clear_symerror_on_linkup(struct timer_list *t)
17462306a36Sopenharmony_ci{
17562306a36Sopenharmony_ci	struct qib_pportdata *ppd = from_timer(ppd, t, symerr_clear_timer);
17662306a36Sopenharmony_ci
17762306a36Sopenharmony_ci	if (ppd->lflags & QIBL_LINKACTIVE)
17862306a36Sopenharmony_ci		return;
17962306a36Sopenharmony_ci
18062306a36Sopenharmony_ci	ppd->ibport_data.z_symbol_error_counter =
18162306a36Sopenharmony_ci		ppd->dd->f_portcntr(ppd, QIBPORTCNTR_IBSYMBOLERR);
18262306a36Sopenharmony_ci}
18362306a36Sopenharmony_ci
18462306a36Sopenharmony_ci/*
18562306a36Sopenharmony_ci * Handle receive interrupts for user ctxts; this means a user
18662306a36Sopenharmony_ci * process was waiting for a packet to arrive, and didn't want
18762306a36Sopenharmony_ci * to poll.
18862306a36Sopenharmony_ci */
18962306a36Sopenharmony_civoid qib_handle_urcv(struct qib_devdata *dd, u64 ctxtr)
19062306a36Sopenharmony_ci{
19162306a36Sopenharmony_ci	struct qib_ctxtdata *rcd;
19262306a36Sopenharmony_ci	unsigned long flags;
19362306a36Sopenharmony_ci	int i;
19462306a36Sopenharmony_ci
19562306a36Sopenharmony_ci	spin_lock_irqsave(&dd->uctxt_lock, flags);
19662306a36Sopenharmony_ci	for (i = dd->first_user_ctxt; dd->rcd && i < dd->cfgctxts; i++) {
19762306a36Sopenharmony_ci		if (!(ctxtr & (1ULL << i)))
19862306a36Sopenharmony_ci			continue;
19962306a36Sopenharmony_ci		rcd = dd->rcd[i];
20062306a36Sopenharmony_ci		if (!rcd || !rcd->cnt)
20162306a36Sopenharmony_ci			continue;
20262306a36Sopenharmony_ci
20362306a36Sopenharmony_ci		if (test_and_clear_bit(QIB_CTXT_WAITING_RCV, &rcd->flag)) {
20462306a36Sopenharmony_ci			wake_up_interruptible(&rcd->wait);
20562306a36Sopenharmony_ci			dd->f_rcvctrl(rcd->ppd, QIB_RCVCTRL_INTRAVAIL_DIS,
20662306a36Sopenharmony_ci				      rcd->ctxt);
20762306a36Sopenharmony_ci		} else if (test_and_clear_bit(QIB_CTXT_WAITING_URG,
20862306a36Sopenharmony_ci					      &rcd->flag)) {
20962306a36Sopenharmony_ci			rcd->urgent++;
21062306a36Sopenharmony_ci			wake_up_interruptible(&rcd->wait);
21162306a36Sopenharmony_ci		}
21262306a36Sopenharmony_ci	}
21362306a36Sopenharmony_ci	spin_unlock_irqrestore(&dd->uctxt_lock, flags);
21462306a36Sopenharmony_ci}
21562306a36Sopenharmony_ci
21662306a36Sopenharmony_civoid qib_bad_intrstatus(struct qib_devdata *dd)
21762306a36Sopenharmony_ci{
21862306a36Sopenharmony_ci	static int allbits;
21962306a36Sopenharmony_ci
22062306a36Sopenharmony_ci	/* separate routine, for better optimization of qib_intr() */
22162306a36Sopenharmony_ci
22262306a36Sopenharmony_ci	/*
22362306a36Sopenharmony_ci	 * We print the message and disable interrupts, in hope of
22462306a36Sopenharmony_ci	 * having a better chance of debugging the problem.
22562306a36Sopenharmony_ci	 */
22662306a36Sopenharmony_ci	qib_dev_err(dd,
22762306a36Sopenharmony_ci		"Read of chip interrupt status failed disabling interrupts\n");
22862306a36Sopenharmony_ci	if (allbits++) {
22962306a36Sopenharmony_ci		/* disable interrupt delivery, something is very wrong */
23062306a36Sopenharmony_ci		if (allbits == 2)
23162306a36Sopenharmony_ci			dd->f_set_intr_state(dd, 0);
23262306a36Sopenharmony_ci		if (allbits == 3) {
23362306a36Sopenharmony_ci			qib_dev_err(dd,
23462306a36Sopenharmony_ci				"2nd bad interrupt status, unregistering interrupts\n");
23562306a36Sopenharmony_ci			dd->flags |= QIB_BADINTR;
23662306a36Sopenharmony_ci			dd->flags &= ~QIB_INITTED;
23762306a36Sopenharmony_ci			dd->f_free_irq(dd);
23862306a36Sopenharmony_ci		}
23962306a36Sopenharmony_ci	}
24062306a36Sopenharmony_ci}
241