1158287Smaxim/*
2158287Smaxim * Copyright (c) 2021 Cornelis Networks. All rights reserved.
3158287Smaxim * Copyright (c) 2013 Intel Corporation. All rights reserved.
4158287Smaxim * Copyright (c) 2006, 2007, 2008, 2009 QLogic Corporation. All rights reserved.
5158287Smaxim * Copyright (c) 2003, 2004, 2005, 2006 PathScale, Inc. All rights reserved.
6158287Smaxim *
7158287Smaxim * This software is available to you under a choice of one of two
8158287Smaxim * licenses.  You may choose to be licensed under the terms of the GNU
9158287Smaxim * General Public License (GPL) Version 2, available from the file
10158287Smaxim * COPYING in the main directory of this source tree, or the
11158287Smaxim * OpenIB.org BSD license below:
12158287Smaxim *
13158287Smaxim *     Redistribution and use in source and binary forms, with or
14158287Smaxim *     without modification, are permitted provided that the following
15158287Smaxim *     conditions are met:
16158287Smaxim *
17158287Smaxim *      - Redistributions of source code must retain the above
18158287Smaxim *        copyright notice, this list of conditions and the following
19158287Smaxim *        disclaimer.
20158287Smaxim *
21158287Smaxim *      - Redistributions in binary form must reproduce the above
22158287Smaxim *        copyright notice, this list of conditions and the following
23158287Smaxim *        disclaimer in the documentation and/or other materials
24158287Smaxim *        provided with the distribution.
25158287Smaxim *
26158287Smaxim * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27158287Smaxim * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28158287Smaxim * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29158287Smaxim * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30158287Smaxim * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31158287Smaxim * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32158287Smaxim * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33158287Smaxim * SOFTWARE.
34158287Smaxim */
35158287Smaxim
36158287Smaxim#include <linux/spinlock.h>
37158287Smaxim#include <linux/pci.h>
38158287Smaxim#include <linux/io.h>
39158287Smaxim#include <linux/delay.h>
40158287Smaxim#include <linux/netdevice.h>
41158287Smaxim#include <linux/vmalloc.h>
42158287Smaxim#include <linux/module.h>
43158287Smaxim#include <linux/prefetch.h>
44158287Smaxim
45158287Smaxim#include "qib.h"
46158287Smaxim
47158287Smaxim/*
48158287Smaxim * The size has to be longer than this string, so we can append
49158287Smaxim * board/chip information to it in the init code.
50158287Smaxim */
51202718Sedconst char ib_qib_version[] = QIB_DRIVER_VERSION "\n";
52202718Sed
53158287SmaximDEFINE_MUTEX(qib_mutex);	/* general driver use */
54158287Smaxim
55158287Smaximunsigned qib_ibmtu;
56158287Smaximmodule_param_named(ibmtu, qib_ibmtu, uint, S_IRUGO);
57202198SedMODULE_PARM_DESC(ibmtu, "Set max IB MTU (0=2KB, 1=256, 2=512, ... 5=4096");
58158287Smaxim
59158287Smaximunsigned qib_compat_ddr_negotiate = 1;
60158287Smaximmodule_param_named(compat_ddr_negotiate, qib_compat_ddr_negotiate, uint,
61158287Smaxim		   S_IWUSR | S_IRUGO);
62158287SmaximMODULE_PARM_DESC(compat_ddr_negotiate,
63158287Smaxim		 "Attempt pre-IBTA 1.2 DDR speed negotiation");
64250942Sghelmer
65158287SmaximMODULE_LICENSE("Dual BSD/GPL");
66158287SmaximMODULE_AUTHOR("Cornelis <support@cornelisnetworks.com>");
67158287SmaximMODULE_DESCRIPTION("Cornelis IB driver");
68158287Smaxim
69158287Smaxim/*
70158287Smaxim * QIB_PIO_MAXIBHDR is the max IB header size allowed for in our
71202198Sed * PIO send buffers.  This is well beyond anything currently
72158287Smaxim * defined in the InfiniBand spec.
73158287Smaxim */
74158287Smaxim#define QIB_PIO_MAXIBHDR 128
75158287Smaxim
76158287Smaxim/*
77166504Srse * QIB_MAX_PKT_RCV is the max # if packets processed per receive interrupt.
78158287Smaxim */
79158287Smaxim#define QIB_MAX_PKT_RECV 64
80158287Smaxim
81158287Smaximstruct qlogic_ib_stats qib_stats;
82158287Smaxim
83158287Smaximstruct pci_dev *qib_get_pci_dev(struct rvt_dev_info *rdi)
84158287Smaxim{
85158287Smaxim	struct qib_ibdev *ibdev = container_of(rdi, struct qib_ibdev, rdi);
86158287Smaxim	struct qib_devdata *dd = container_of(ibdev,
87158287Smaxim					      struct qib_devdata, verbs_dev);
88158287Smaxim	return dd->pcidev;
89158287Smaxim}
90158287Smaxim
91158287Smaxim/*
92158287Smaxim * Return count of units with at least one port ACTIVE.
93250942Sghelmer */
94202198Sedint qib_count_active_units(void)
95158287Smaxim{
96158287Smaxim	struct qib_devdata *dd;
97158287Smaxim	struct qib_pportdata *ppd;
98158287Smaxim	unsigned long index, flags;
99158287Smaxim	int pidx, nunits_active = 0;
100158287Smaxim
101158287Smaxim	xa_lock_irqsave(&qib_dev_table, flags);
102158287Smaxim	xa_for_each(&qib_dev_table, index, dd) {
103158287Smaxim		if (!(dd->flags & QIB_PRESENT) || !dd->kregbase)
104158287Smaxim			continue;
105158287Smaxim		for (pidx = 0; pidx < dd->num_pports; ++pidx) {
106158287Smaxim			ppd = dd->pport + pidx;
107158287Smaxim			if (ppd->lid && (ppd->lflags & (QIBL_LINKINIT |
108158287Smaxim					 QIBL_LINKARMED | QIBL_LINKACTIVE))) {
109158287Smaxim				nunits_active++;
110158287Smaxim				break;
111158287Smaxim			}
112158287Smaxim		}
113158287Smaxim	}
114158287Smaxim	xa_unlock_irqrestore(&qib_dev_table, flags);
115158287Smaxim	return nunits_active;
116158287Smaxim}
117158287Smaxim
118158287Smaxim/*
119158287Smaxim * Return count of all units, optionally return in arguments
120158287Smaxim * the number of usable (present) units, and the number of
121158287Smaxim * ports that are up.
122158287Smaxim */
123158287Smaximint qib_count_units(int *npresentp, int *nupp)
124158287Smaxim{
125158287Smaxim	int nunits = 0, npresent = 0, nup = 0;
126158287Smaxim	struct qib_devdata *dd;
127158287Smaxim	unsigned long index, flags;
128158287Smaxim	int pidx;
129158287Smaxim	struct qib_pportdata *ppd;
130158287Smaxim
131158287Smaxim	xa_lock_irqsave(&qib_dev_table, flags);
132158287Smaxim	xa_for_each(&qib_dev_table, index, dd) {
133158287Smaxim		nunits++;
134158287Smaxim		if ((dd->flags & QIB_PRESENT) && dd->kregbase)
135158287Smaxim			npresent++;
136158287Smaxim		for (pidx = 0; pidx < dd->num_pports; ++pidx) {
137158287Smaxim			ppd = dd->pport + pidx;
138158287Smaxim			if (ppd->lid && (ppd->lflags & (QIBL_LINKINIT |
139158287Smaxim					 QIBL_LINKARMED | QIBL_LINKACTIVE)))
140158287Smaxim				nup++;
141158287Smaxim		}
142158287Smaxim	}
143158287Smaxim	xa_unlock_irqrestore(&qib_dev_table, flags);
144158287Smaxim
145158287Smaxim	if (npresentp)
146158287Smaxim		*npresentp = npresent;
147158287Smaxim	if (nupp)
148158287Smaxim		*nupp = nup;
149158287Smaxim
150158287Smaxim	return nunits;
151158287Smaxim}
152158287Smaxim
153158287Smaxim/**
154158287Smaxim * qib_wait_linkstate - wait for an IB link state change to occur
155158287Smaxim * @ppd: the qlogic_ib device
156158287Smaxim * @state: the state to wait for
157158287Smaxim * @msecs: the number of milliseconds to wait
158158287Smaxim *
159158287Smaxim * wait up to msecs milliseconds for IB link state change to occur for
160158287Smaxim * now, take the easy polling route.  Currently used only by
161158287Smaxim * qib_set_linkstate.  Returns 0 if state reached, otherwise
162158287Smaxim * -ETIMEDOUT state can have multiple states set, for any of several
163158287Smaxim * transitions.
164158287Smaxim */
165158287Smaximint qib_wait_linkstate(struct qib_pportdata *ppd, u32 state, int msecs)
166158287Smaxim{
167158287Smaxim	int ret;
168158287Smaxim	unsigned long flags;
169158287Smaxim
170158287Smaxim	spin_lock_irqsave(&ppd->lflags_lock, flags);
171158287Smaxim	if (ppd->state_wanted) {
172158287Smaxim		spin_unlock_irqrestore(&ppd->lflags_lock, flags);
173158287Smaxim		ret = -EBUSY;
174158287Smaxim		goto bail;
175158287Smaxim	}
176158287Smaxim	ppd->state_wanted = state;
177158287Smaxim	spin_unlock_irqrestore(&ppd->lflags_lock, flags);
178158287Smaxim	wait_event_interruptible_timeout(ppd->state_wait,
179166504Srse					 (ppd->lflags & state),
180158287Smaxim					 msecs_to_jiffies(msecs));
181158287Smaxim	spin_lock_irqsave(&ppd->lflags_lock, flags);
182158287Smaxim	ppd->state_wanted = 0;
183158287Smaxim	spin_unlock_irqrestore(&ppd->lflags_lock, flags);
184158287Smaxim
185158287Smaxim	if (!(ppd->lflags & state))
186158287Smaxim		ret = -ETIMEDOUT;
187158287Smaxim	else
188158287Smaxim		ret = 0;
189158287Smaximbail:
190158287Smaxim	return ret;
191158287Smaxim}
192158287Smaxim
193158287Smaximint qib_set_linkstate(struct qib_pportdata *ppd, u8 newstate)
194158287Smaxim{
195158287Smaxim	u32 lstate;
196158287Smaxim	int ret;
197158287Smaxim	struct qib_devdata *dd = ppd->dd;
198158287Smaxim	unsigned long flags;
199158287Smaxim
200158287Smaxim	switch (newstate) {
201158287Smaxim	case QIB_IB_LINKDOWN_ONLY:
202158287Smaxim		dd->f_set_ib_cfg(ppd, QIB_IB_CFG_LSTATE,
203158287Smaxim				 IB_LINKCMD_DOWN | IB_LINKINITCMD_NOP);
204158287Smaxim		/* don't wait */
205158287Smaxim		ret = 0;
206158287Smaxim		goto bail;
207158287Smaxim
208158287Smaxim	case QIB_IB_LINKDOWN:
209158287Smaxim		dd->f_set_ib_cfg(ppd, QIB_IB_CFG_LSTATE,
210158287Smaxim				 IB_LINKCMD_DOWN | IB_LINKINITCMD_POLL);
211158287Smaxim		/* don't wait */
212158287Smaxim		ret = 0;
213158287Smaxim		goto bail;
214158287Smaxim
215158287Smaxim	case QIB_IB_LINKDOWN_SLEEP:
216158287Smaxim		dd->f_set_ib_cfg(ppd, QIB_IB_CFG_LSTATE,
217158287Smaxim				 IB_LINKCMD_DOWN | IB_LINKINITCMD_SLEEP);
218158287Smaxim		/* don't wait */
219158287Smaxim		ret = 0;
220158287Smaxim		goto bail;
221158287Smaxim
222158287Smaxim	case QIB_IB_LINKDOWN_DISABLE:
223158287Smaxim		dd->f_set_ib_cfg(ppd, QIB_IB_CFG_LSTATE,
224158287Smaxim				 IB_LINKCMD_DOWN | IB_LINKINITCMD_DISABLE);
225158287Smaxim		/* don't wait */
226158287Smaxim		ret = 0;
227158287Smaxim		goto bail;
228158287Smaxim
229158287Smaxim	case QIB_IB_LINKARM:
230158287Smaxim		if (ppd->lflags & QIBL_LINKARMED) {
231158287Smaxim			ret = 0;
232158287Smaxim			goto bail;
233158287Smaxim		}
234158287Smaxim		if (!(ppd->lflags & (QIBL_LINKINIT | QIBL_LINKACTIVE))) {
235158287Smaxim			ret = -EINVAL;
236158287Smaxim			goto bail;
237158287Smaxim		}
238158287Smaxim		/*
239158287Smaxim		 * Since the port can be ACTIVE when we ask for ARMED,
240158287Smaxim		 * clear QIBL_LINKV so we can wait for a transition.
241158287Smaxim		 * If the link isn't ARMED, then something else happened
242158287Smaxim		 * and there is no point waiting for ARMED.
243158287Smaxim		 */
244158287Smaxim		spin_lock_irqsave(&ppd->lflags_lock, flags);
245158287Smaxim		ppd->lflags &= ~QIBL_LINKV;
246158287Smaxim		spin_unlock_irqrestore(&ppd->lflags_lock, flags);
247158287Smaxim		dd->f_set_ib_cfg(ppd, QIB_IB_CFG_LSTATE,
248158287Smaxim				 IB_LINKCMD_ARMED | IB_LINKINITCMD_NOP);
249158287Smaxim		lstate = QIBL_LINKV;
250158287Smaxim		break;
251158287Smaxim
252158287Smaxim	case QIB_IB_LINKACTIVE:
253158287Smaxim		if (ppd->lflags & QIBL_LINKACTIVE) {
254158287Smaxim			ret = 0;
255158287Smaxim			goto bail;
256158287Smaxim		}
257158287Smaxim		if (!(ppd->lflags & QIBL_LINKARMED)) {
258158287Smaxim			ret = -EINVAL;
259158287Smaxim			goto bail;
260158287Smaxim		}
261158287Smaxim		dd->f_set_ib_cfg(ppd, QIB_IB_CFG_LSTATE,
262158287Smaxim				 IB_LINKCMD_ACTIVE | IB_LINKINITCMD_NOP);
263158287Smaxim		lstate = QIBL_LINKACTIVE;
264158287Smaxim		break;
265158287Smaxim
266158287Smaxim	default:
267158287Smaxim		ret = -EINVAL;
268158287Smaxim		goto bail;
269158287Smaxim	}
270158287Smaxim	ret = qib_wait_linkstate(ppd, lstate, 10);
271158287Smaxim
272158287Smaximbail:
273158287Smaxim	return ret;
274158287Smaxim}
275158287Smaxim
276158287Smaxim/*
277158287Smaxim * Get address of eager buffer from it's index (allocated in chunks, not
278158287Smaxim * contiguous).
279158287Smaxim */
280158287Smaximstatic inline void *qib_get_egrbuf(const struct qib_ctxtdata *rcd, u32 etail)
281158287Smaxim{
282240954Skevlo	const u32 chunk = etail >> rcd->rcvegrbufs_perchunk_shift;
283158287Smaxim	const u32 idx =  etail & ((u32)rcd->rcvegrbufs_perchunk - 1);
284158287Smaxim
285158287Smaxim	return rcd->rcvegrbuf[chunk] + (idx << rcd->dd->rcvegrbufsize_shift);
286158287Smaxim}
287158287Smaxim
288158287Smaxim/*
289158287Smaxim * Returns 1 if error was a CRC, else 0.
290240954Skevlo * Needed for some chip's synthesized error counters.
291158287Smaxim */
292158287Smaximstatic u32 qib_rcv_hdrerr(struct qib_ctxtdata *rcd, struct qib_pportdata *ppd,
293240954Skevlo			  u32 ctxt, u32 eflags, u32 l, u32 etail,
294240954Skevlo			  __le32 *rhf_addr, struct qib_message_header *rhdr)
295158287Smaxim{
296158287Smaxim	u32 ret = 0;
297240954Skevlo
298240954Skevlo	if (eflags & (QLOGIC_IB_RHF_H_ICRCERR | QLOGIC_IB_RHF_H_VCRCERR))
299240954Skevlo		ret = 1;
300240954Skevlo	else if (eflags == QLOGIC_IB_RHF_H_TIDERR) {
301240954Skevlo		/* For TIDERR and RC QPs premptively schedule a NAK */
302240954Skevlo		struct ib_header *hdr = (struct ib_header *)rhdr;
303240954Skevlo		struct ib_other_headers *ohdr = NULL;
304240954Skevlo		struct qib_ibport *ibp = &ppd->ibport_data;
305240954Skevlo		struct qib_devdata *dd = ppd->dd;
306240954Skevlo		struct rvt_dev_info *rdi = &dd->verbs_dev.rdi;
307240954Skevlo		struct rvt_qp *qp = NULL;
308240954Skevlo		u32 tlen = qib_hdrget_length_in_bytes(rhf_addr);
309240954Skevlo		u16 lid  = be16_to_cpu(hdr->lrh[1]);
310240954Skevlo		int lnh = be16_to_cpu(hdr->lrh[0]) & 3;
311240954Skevlo		u32 qp_num;
312240954Skevlo		u32 opcode;
313240954Skevlo		u32 psn;
314240954Skevlo		int diff;
315158287Smaxim
316158287Smaxim		/* Sanity check packet */
317158287Smaxim		if (tlen < 24)
318158287Smaxim			goto drop;
319158287Smaxim
320158287Smaxim		if (lid < be16_to_cpu(IB_MULTICAST_LID_BASE)) {
321158287Smaxim			lid &= ~((1 << ppd->lmc) - 1);
322158287Smaxim			if (unlikely(lid != ppd->lid))
323158287Smaxim				goto drop;
324158287Smaxim		}
325158287Smaxim
326158287Smaxim		/* Check for GRH */
327158287Smaxim		if (lnh == QIB_LRH_BTH)
328158287Smaxim			ohdr = &hdr->u.oth;
329158287Smaxim		else if (lnh == QIB_LRH_GRH) {
330158287Smaxim			u32 vtf;
331158287Smaxim
332158287Smaxim			ohdr = &hdr->u.l.oth;
333158287Smaxim			if (hdr->u.l.grh.next_hdr != IB_GRH_NEXT_HDR)
334158287Smaxim				goto drop;
335158287Smaxim			vtf = be32_to_cpu(hdr->u.l.grh.version_tclass_flow);
336158287Smaxim			if ((vtf >> IB_GRH_VERSION_SHIFT) != IB_GRH_VERSION)
337158287Smaxim				goto drop;
338158287Smaxim		} else
339158287Smaxim			goto drop;
340158287Smaxim
341158287Smaxim		/* Get opcode and PSN from packet */
342158287Smaxim		opcode = be32_to_cpu(ohdr->bth[0]);
343158287Smaxim		opcode >>= 24;
344158287Smaxim		psn = be32_to_cpu(ohdr->bth[2]);
345158287Smaxim
346158287Smaxim		/* Get the destination QP number. */
347158287Smaxim		qp_num = be32_to_cpu(ohdr->bth[1]) & RVT_QPN_MASK;
348158287Smaxim		if (qp_num != QIB_MULTICAST_QPN) {
349158287Smaxim			int ruc_res;
350158287Smaxim
351158287Smaxim			rcu_read_lock();
352158287Smaxim			qp = rvt_lookup_qpn(rdi, &ibp->rvp, qp_num);
353158287Smaxim			if (!qp) {
354158287Smaxim				rcu_read_unlock();
355158287Smaxim				goto drop;
356158287Smaxim			}
357158287Smaxim
358158287Smaxim			/*
359158287Smaxim			 * Handle only RC QPs - for other QP types drop error
360158287Smaxim			 * packet.
361158287Smaxim			 */
362158287Smaxim			spin_lock(&qp->r_lock);
363158287Smaxim
364158287Smaxim			/* Check for valid receive state. */
365158287Smaxim			if (!(ib_rvt_state_ops[qp->state] &
366158287Smaxim			      RVT_PROCESS_RECV_OK)) {
367158287Smaxim				ibp->rvp.n_pkt_drops++;
368158287Smaxim				goto unlock;
369158287Smaxim			}
370158287Smaxim
371158287Smaxim			switch (qp->ibqp.qp_type) {
372158287Smaxim			case IB_QPT_RC:
373158287Smaxim				ruc_res =
374158287Smaxim					qib_ruc_check_hdr(
375158287Smaxim						ibp, hdr,
376158287Smaxim						lnh == QIB_LRH_GRH,
377158287Smaxim						qp,
378158287Smaxim						be32_to_cpu(ohdr->bth[0]));
379158287Smaxim				if (ruc_res)
380158287Smaxim					goto unlock;
381158287Smaxim
382158287Smaxim				/* Only deal with RDMA Writes for now */
383158287Smaxim				if (opcode <
384158287Smaxim				    IB_OPCODE_RC_RDMA_READ_RESPONSE_FIRST) {
385158287Smaxim					diff = qib_cmp24(psn, qp->r_psn);
386158287Smaxim					if (!qp->r_nak_state && diff >= 0) {
387158287Smaxim						ibp->rvp.n_rc_seqnak++;
388158287Smaxim						qp->r_nak_state =
389158287Smaxim							IB_NAK_PSN_ERROR;
390158287Smaxim						/* Use the expected PSN. */
391158287Smaxim						qp->r_ack_psn = qp->r_psn;
392158287Smaxim						/*
393158287Smaxim						 * Wait to send the sequence
394158287Smaxim						 * NAK until all packets
395158287Smaxim						 * in the receive queue have
396158287Smaxim						 * been processed.
397158287Smaxim						 * Otherwise, we end up
398158287Smaxim						 * propagating congestion.
399158287Smaxim						 */
400158287Smaxim						if (list_empty(&qp->rspwait)) {
401158287Smaxim							qp->r_flags |=
402158287Smaxim								RVT_R_RSP_NAK;
403158287Smaxim							rvt_get_qp(qp);
404158287Smaxim							list_add_tail(
405158287Smaxim							 &qp->rspwait,
406158287Smaxim							 &rcd->qp_wait_list);
407158287Smaxim						}
408158287Smaxim					} /* Out of sequence NAK */
409158287Smaxim				} /* QP Request NAKs */
410158287Smaxim				break;
411158287Smaxim			case IB_QPT_SMI:
412158287Smaxim			case IB_QPT_GSI:
413158287Smaxim			case IB_QPT_UD:
414158287Smaxim			case IB_QPT_UC:
415158287Smaxim			default:
416158287Smaxim				/* For now don't handle any other QP types */
417158287Smaxim				break;
418158287Smaxim			}
419158287Smaxim
420158287Smaximunlock:
421158287Smaxim			spin_unlock(&qp->r_lock);
422158287Smaxim			rcu_read_unlock();
423158287Smaxim		} /* Unicast QP */
424158287Smaxim	} /* Valid packet with TIDErr */
425158287Smaxim
426158287Smaximdrop:
427158287Smaxim	return ret;
428158287Smaxim}
429158287Smaxim
430158287Smaxim/*
431158287Smaxim * qib_kreceive - receive a packet
432158287Smaxim * @rcd: the qlogic_ib context
433158287Smaxim * @llic: gets count of good packets needed to clear lli,
434158287Smaxim *          (used with chips that need need to track crcs for lli)
435158287Smaxim *
436158287Smaxim * called from interrupt handler for errors or receive interrupt
437158287Smaxim * Returns number of CRC error packets, needed by some chips for
438158287Smaxim * local link integrity tracking.   crcs are adjusted down by following
439158287Smaxim * good packets, if any, and count of good packets is also tracked.
440158287Smaxim */
441158287Smaximu32 qib_kreceive(struct qib_ctxtdata *rcd, u32 *llic, u32 *npkts)
442158287Smaxim{
443158287Smaxim	struct qib_devdata *dd = rcd->dd;
444158287Smaxim	struct qib_pportdata *ppd = rcd->ppd;
445158287Smaxim	__le32 *rhf_addr;
446158287Smaxim	void *ebuf;
447158287Smaxim	const u32 rsize = dd->rcvhdrentsize;        /* words */
448158287Smaxim	const u32 maxcnt = dd->rcvhdrcnt * rsize;   /* words */
449158287Smaxim	u32 etail = -1, l, hdrqtail;
450158287Smaxim	struct qib_message_header *hdr;
451158287Smaxim	u32 eflags, etype, tlen, i = 0, updegr = 0, crcs = 0;
452158287Smaxim	int last;
453158287Smaxim	u64 lval;
454158287Smaxim	struct rvt_qp *qp, *nqp;
455158287Smaxim
456158287Smaxim	l = rcd->head;
457158287Smaxim	rhf_addr = (__le32 *) rcd->rcvhdrq + l + dd->rhf_offset;
458158287Smaxim	if (dd->flags & QIB_NODMA_RTAIL) {
459158287Smaxim		u32 seq = qib_hdrget_seq(rhf_addr);
460158287Smaxim
461158287Smaxim		if (seq != rcd->seq_cnt)
462158287Smaxim			goto bail;
463158287Smaxim		hdrqtail = 0;
464158287Smaxim	} else {
465158287Smaxim		hdrqtail = qib_get_rcvhdrtail(rcd);
466158287Smaxim		if (l == hdrqtail)
467158287Smaxim			goto bail;
468158287Smaxim		smp_rmb();  /* prevent speculative reads of dma'ed hdrq */
469158287Smaxim	}
470158287Smaxim
471158287Smaxim	for (last = 0, i = 1; !last; i += !last) {
472158287Smaxim		hdr = dd->f_get_msgheader(dd, rhf_addr);
473158287Smaxim		eflags = qib_hdrget_err_flags(rhf_addr);
474158287Smaxim		etype = qib_hdrget_rcv_type(rhf_addr);
475158287Smaxim		/* total length */
476158287Smaxim		tlen = qib_hdrget_length_in_bytes(rhf_addr);
477158287Smaxim		ebuf = NULL;
478158287Smaxim		if ((dd->flags & QIB_NODMA_RTAIL) ?
479158287Smaxim		    qib_hdrget_use_egr_buf(rhf_addr) :
480158287Smaxim		    (etype != RCVHQ_RCV_TYPE_EXPECTED)) {
481158287Smaxim			etail = qib_hdrget_index(rhf_addr);
482158287Smaxim			updegr = 1;
483158287Smaxim			if (tlen > sizeof(*hdr) ||
484158287Smaxim			    etype >= RCVHQ_RCV_TYPE_NON_KD) {
485158287Smaxim				ebuf = qib_get_egrbuf(rcd, etail);
486158287Smaxim				prefetch_range(ebuf, tlen - sizeof(*hdr));
487158287Smaxim			}
488158287Smaxim		}
489158287Smaxim		if (!eflags) {
490158287Smaxim			u16 lrh_len = be16_to_cpu(hdr->lrh[2]) << 2;
491158287Smaxim
492158287Smaxim			if (lrh_len != tlen) {
493158287Smaxim				qib_stats.sps_lenerrs++;
494158287Smaxim				goto move_along;
495158287Smaxim			}
496158287Smaxim		}
497158287Smaxim		if (etype == RCVHQ_RCV_TYPE_NON_KD && !eflags &&
498158287Smaxim		    ebuf == NULL &&
499158287Smaxim		    tlen > (dd->rcvhdrentsize - 2 + 1 -
500158287Smaxim				qib_hdrget_offset(rhf_addr)) << 2) {
501158287Smaxim			goto move_along;
502158287Smaxim		}
503158287Smaxim
504158287Smaxim		/*
505158287Smaxim		 * Both tiderr and qibhdrerr are set for all plain IB
506158287Smaxim		 * packets; only qibhdrerr should be set.
507158287Smaxim		 */
508158287Smaxim		if (unlikely(eflags))
509158287Smaxim			crcs += qib_rcv_hdrerr(rcd, ppd, rcd->ctxt, eflags, l,
510158287Smaxim					       etail, rhf_addr, hdr);
511158287Smaxim		else if (etype == RCVHQ_RCV_TYPE_NON_KD) {
512158287Smaxim			qib_ib_rcv(rcd, hdr, ebuf, tlen);
513158287Smaxim			if (crcs)
514158287Smaxim				crcs--;
515158287Smaxim			else if (llic && *llic)
516158287Smaxim				--*llic;
517158287Smaxim		}
518158287Smaximmove_along:
519158287Smaxim		l += rsize;
520158287Smaxim		if (l >= maxcnt)
521158289Sume			l = 0;
522158289Sume		if (i == QIB_MAX_PKT_RECV)
523158289Sume			last = 1;
524158287Smaxim
525158287Smaxim		rhf_addr = (__le32 *) rcd->rcvhdrq + l + dd->rhf_offset;
526158287Smaxim		if (dd->flags & QIB_NODMA_RTAIL) {
527158287Smaxim			u32 seq = qib_hdrget_seq(rhf_addr);
528158287Smaxim
529158287Smaxim			if (++rcd->seq_cnt > 13)
530158287Smaxim				rcd->seq_cnt = 1;
531158287Smaxim			if (seq != rcd->seq_cnt)
532158287Smaxim				last = 1;
533158287Smaxim		} else if (l == hdrqtail)
534158287Smaxim			last = 1;
535158287Smaxim		/*
536158287Smaxim		 * Update head regs etc., every 16 packets, if not last pkt,
537158287Smaxim		 * to help prevent rcvhdrq overflows, when many packets
538158287Smaxim		 * are processed and queue is nearly full.
539158287Smaxim		 * Don't request an interrupt for intermediate updates.
540158287Smaxim		 */
541158287Smaxim		lval = l;
542158287Smaxim		if (!last && !(i & 0xf)) {
543158287Smaxim			dd->f_update_usrhead(rcd, lval, updegr, etail, i);
544158287Smaxim			updegr = 0;
545158287Smaxim		}
546158287Smaxim	}
547158287Smaxim
548158287Smaxim	rcd->head = l;
549158287Smaxim
550158287Smaxim	/*
551158287Smaxim	 * Iterate over all QPs waiting to respond.
552158287Smaxim	 * The list won't change since the IRQ is only run on one CPU.
553158287Smaxim	 */
554158287Smaxim	list_for_each_entry_safe(qp, nqp, &rcd->qp_wait_list, rspwait) {
555158287Smaxim		list_del_init(&qp->rspwait);
556158287Smaxim		if (qp->r_flags & RVT_R_RSP_NAK) {
557158287Smaxim			qp->r_flags &= ~RVT_R_RSP_NAK;
558158287Smaxim			qib_send_rc_ack(qp);
559158287Smaxim		}
560158287Smaxim		if (qp->r_flags & RVT_R_RSP_SEND) {
561158287Smaxim			unsigned long flags;
562158287Smaxim
563158287Smaxim			qp->r_flags &= ~RVT_R_RSP_SEND;
564158287Smaxim			spin_lock_irqsave(&qp->s_lock, flags);
565158287Smaxim			if (ib_rvt_state_ops[qp->state] &
566158287Smaxim					RVT_PROCESS_OR_FLUSH_SEND)
567158287Smaxim				qib_schedule_send(qp);
568158287Smaxim			spin_unlock_irqrestore(&qp->s_lock, flags);
569158287Smaxim		}
570158287Smaxim		rvt_put_qp(qp);
571158287Smaxim	}
572158287Smaxim
573158287Smaximbail:
574202198Sed	/* Report number of packets consumed */
575202198Sed	if (npkts)
576250942Sghelmer		*npkts = i;
577250942Sghelmer
578250942Sghelmer	/*
579250942Sghelmer	 * Always write head at end, and setup rcv interrupt, even
580250942Sghelmer	 * if no packets were processed.
581250942Sghelmer	 */
582250942Sghelmer	lval = (u64)rcd->head | dd->rhdrhead_intr_off;
583250942Sghelmer	dd->f_update_usrhead(rcd, lval, updegr, etail, i);
584250942Sghelmer	return crcs;
585250942Sghelmer}
586250942Sghelmer
587250942Sghelmer/**
588250942Sghelmer * qib_set_mtu - set the MTU
589250942Sghelmer * @ppd: the perport data
590250942Sghelmer * @arg: the new MTU
591250942Sghelmer *
592250942Sghelmer * We can handle "any" incoming size, the issue here is whether we
593250942Sghelmer * need to restrict our outgoing size.   For now, we don't do any
594250942Sghelmer * sanity checking on this, and we don't deal with what happens to
595250942Sghelmer * programs that are already running when the size changes.
596250942Sghelmer * NOTE: changing the MTU will usually cause the IBC to go back to
597250942Sghelmer * link INIT state...
598250942Sghelmer */
599250942Sghelmerint qib_set_mtu(struct qib_pportdata *ppd, u16 arg)
600250942Sghelmer{
601250942Sghelmer	u32 piosize;
602250942Sghelmer	int ret, chk;
603250942Sghelmer
604250942Sghelmer	if (arg != 256 && arg != 512 && arg != 1024 && arg != 2048 &&
605250942Sghelmer	    arg != 4096) {
606250942Sghelmer		ret = -EINVAL;
607250942Sghelmer		goto bail;
608250942Sghelmer	}
609250942Sghelmer	chk = ib_mtu_enum_to_int(qib_ibmtu);
610250942Sghelmer	if (chk > 0 && arg > chk) {
611250942Sghelmer		ret = -EINVAL;
612250942Sghelmer		goto bail;
613250942Sghelmer	}
614250942Sghelmer
615250942Sghelmer	piosize = ppd->ibmaxlen;
616250942Sghelmer	ppd->ibmtu = arg;
617202198Sed
618202198Sed	if (arg >= (piosize - QIB_PIO_MAXIBHDR)) {
619202198Sed		/* Only if it's not the initial value (or reset to it) */
620202198Sed		if (piosize != ppd->init_ibmaxlen) {
621202198Sed			if (arg > piosize && arg <= ppd->init_ibmaxlen)
622202198Sed				piosize = ppd->init_ibmaxlen - 2 * sizeof(u32);
623202198Sed			ppd->ibmaxlen = piosize;
624202198Sed		}
625202198Sed	} else if ((arg + QIB_PIO_MAXIBHDR) != ppd->ibmaxlen) {
626202198Sed		piosize = arg + QIB_PIO_MAXIBHDR - 2 * sizeof(u32);
627202198Sed		ppd->ibmaxlen = piosize;
628202198Sed	}
629202198Sed
630202198Sed	ppd->dd->f_set_ib_cfg(ppd, QIB_IB_CFG_MTU, 0);
631202198Sed
632202198Sed	ret = 0;
633202718Sed
634202718Sedbail:
635202718Sed	return ret;
636202198Sed}
637202198Sed
638202198Sedint qib_set_lid(struct qib_pportdata *ppd, u32 lid, u8 lmc)
639202198Sed{
640202198Sed	struct qib_devdata *dd = ppd->dd;
641202198Sed
642202198Sed	ppd->lid = lid;
643202198Sed	ppd->lmc = lmc;
644202198Sed
645202198Sed	dd->f_set_ib_cfg(ppd, QIB_IB_CFG_LIDLMC,
646202198Sed			 lid | (~((1U << lmc) - 1)) << 16);
647202198Sed
648202198Sed	qib_devinfo(dd->pcidev, "IB%u:%u got a lid: 0x%x\n",
649202198Sed		    dd->unit, ppd->port, lid);
650202198Sed
651202198Sed	return 0;
652202198Sed}
653202559Sed
654202559Sed/*
655202198Sed * Following deal with the "obviously simple" task of overriding the state
656226841Sed * of the LEDS, which normally indicate link physical and logical status.
657226841Sed * The complications arise in dealing with different hardware mappings
658226841Sed * and the board-dependent routine being called from interrupts.
659226841Sed * and then there's the requirement to _flash_ them.
660226841Sed */
661226841Sed#define LED_OVER_FREQ_SHIFT 8
662226841Sed#define LED_OVER_FREQ_MASK (0xFF<<LED_OVER_FREQ_SHIFT)
663226841Sed/* Below is "non-zero" to force override, but both actual LEDs are off */
664226841Sed#define LED_OVER_BOTH_OFF (8)
665226841Sed
666226841Sedstatic void qib_run_led_override(struct timer_list *t)
667202198Sed{
668202198Sed	struct qib_pportdata *ppd = from_timer(ppd, t,
669202198Sed						    led_override_timer);
670202559Sed	struct qib_devdata *dd = ppd->dd;
671202198Sed	int timeoff;
672202198Sed	int ph_idx;
673226841Sed
674202198Sed	if (!(dd->flags & QIB_INITTED))
675202198Sed		return;
676202198Sed
677202198Sed	ph_idx = ppd->led_override_phase++ & 1;
678202198Sed	ppd->led_override = ppd->led_override_vals[ph_idx];
679202198Sed	timeoff = ppd->led_override_timeoff;
680202198Sed
681202198Sed	dd->f_setextled(ppd, 1);
682206087Sed	/*
683206087Sed	 * don't re-fire the timer if user asked for it to be off; we let
684202198Sed	 * it fire one more time after they turn it off to simplify
685202198Sed	 */
686202198Sed	if (ppd->led_override_vals[0] || ppd->led_override_vals[1])
687202198Sed		mod_timer(&ppd->led_override_timer, jiffies + timeoff);
688206087Sed}
689202198Sed
690202198Sedvoid qib_set_led_override(struct qib_pportdata *ppd, unsigned int val)
691202198Sed{
692202198Sed	struct qib_devdata *dd = ppd->dd;
693202198Sed	int timeoff, freq;
694202198Sed
695202198Sed	if (!(dd->flags & QIB_INITTED))
696202198Sed		return;
697206087Sed
698206087Sed	/* First check if we are blinking. If not, use 1HZ polling */
699202198Sed	timeoff = HZ;
700202198Sed	freq = (val & LED_OVER_FREQ_MASK) >> LED_OVER_FREQ_SHIFT;
701202198Sed
702202198Sed	if (freq) {
703202198Sed		/* For blink, set each phase from one nybble of val */
704206087Sed		ppd->led_override_vals[0] = val & 0xF;
705206087Sed		ppd->led_override_vals[1] = (val >> 4) & 0xF;
706202198Sed		timeoff = (HZ << 4)/freq;
707202198Sed	} else {
708206087Sed		/* Non-blink set both phases the same. */
709202198Sed		ppd->led_override_vals[0] = val & 0xF;
710202198Sed		ppd->led_override_vals[1] = val & 0xF;
711202198Sed	}
712202198Sed	ppd->led_override_timeoff = timeoff;
713202198Sed
714202198Sed	/*
715202198Sed	 * If the timer has not already been started, do so. Use a "quick"
716	 * timeout so the function will be called soon, to look at our request.
717	 */
718	if (atomic_inc_return(&ppd->led_override_timer_active) == 1) {
719		/* Need to start timer */
720		timer_setup(&ppd->led_override_timer, qib_run_led_override, 0);
721		ppd->led_override_timer.expires = jiffies + 1;
722		add_timer(&ppd->led_override_timer);
723	} else {
724		if (ppd->led_override_vals[0] || ppd->led_override_vals[1])
725			mod_timer(&ppd->led_override_timer, jiffies + 1);
726		atomic_dec(&ppd->led_override_timer_active);
727	}
728}
729
730/**
731 * qib_reset_device - reset the chip if possible
732 * @unit: the device to reset
733 *
734 * Whether or not reset is successful, we attempt to re-initialize the chip
735 * (that is, much like a driver unload/reload).  We clear the INITTED flag
736 * so that the various entry points will fail until we reinitialize.  For
737 * now, we only allow this if no user contexts are open that use chip resources
738 */
739int qib_reset_device(int unit)
740{
741	int ret, i;
742	struct qib_devdata *dd = qib_lookup(unit);
743	struct qib_pportdata *ppd;
744	unsigned long flags;
745	int pidx;
746
747	if (!dd) {
748		ret = -ENODEV;
749		goto bail;
750	}
751
752	qib_devinfo(dd->pcidev, "Reset on unit %u requested\n", unit);
753
754	if (!dd->kregbase || !(dd->flags & QIB_PRESENT)) {
755		qib_devinfo(dd->pcidev,
756			"Invalid unit number %u or not initialized or not present\n",
757			unit);
758		ret = -ENXIO;
759		goto bail;
760	}
761
762	spin_lock_irqsave(&dd->uctxt_lock, flags);
763	if (dd->rcd)
764		for (i = dd->first_user_ctxt; i < dd->cfgctxts; i++) {
765			if (!dd->rcd[i] || !dd->rcd[i]->cnt)
766				continue;
767			spin_unlock_irqrestore(&dd->uctxt_lock, flags);
768			ret = -EBUSY;
769			goto bail;
770		}
771	spin_unlock_irqrestore(&dd->uctxt_lock, flags);
772
773	for (pidx = 0; pidx < dd->num_pports; ++pidx) {
774		ppd = dd->pport + pidx;
775		if (atomic_read(&ppd->led_override_timer_active)) {
776			/* Need to stop LED timer, _then_ shut off LEDs */
777			del_timer_sync(&ppd->led_override_timer);
778			atomic_set(&ppd->led_override_timer_active, 0);
779		}
780
781		/* Shut off LEDs after we are sure timer is not running */
782		ppd->led_override = LED_OVER_BOTH_OFF;
783		dd->f_setextled(ppd, 0);
784		if (dd->flags & QIB_HAS_SEND_DMA)
785			qib_teardown_sdma(ppd);
786	}
787
788	ret = dd->f_reset(dd);
789	if (ret == 1)
790		ret = qib_init(dd, 1);
791	else
792		ret = -EAGAIN;
793	if (ret)
794		qib_dev_err(dd,
795			"Reinitialize unit %u after reset failed with %d\n",
796			unit, ret);
797	else
798		qib_devinfo(dd->pcidev,
799			"Reinitialized unit %u after resetting\n",
800			unit);
801
802bail:
803	return ret;
804}
805