IB/hfi1: Remove unnecessary fecn and becn fields
[linux-block.git] / drivers / infiniband / hw / hfi1 / driver.c
CommitLineData
77241056 1/*
d4829ea6 2 * Copyright(c) 2015-2017 Intel Corporation.
77241056
MM
3 *
4 * This file is provided under a dual BSD/GPLv2 license. When using or
5 * redistributing this file, you may do so under either license.
6 *
7 * GPL LICENSE SUMMARY
8 *
77241056
MM
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of version 2 of the GNU General Public License as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * BSD LICENSE
19 *
77241056
MM
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
22 * are met:
23 *
24 * - Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * - Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in
28 * the documentation and/or other materials provided with the
29 * distribution.
30 * - Neither the name of Intel Corporation nor the names of its
31 * contributors may be used to endorse or promote products derived
32 * from this software without specific prior written permission.
33 *
34 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
35 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
37 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
38 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
41 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
42 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
44 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45 *
46 */
47
48#include <linux/spinlock.h>
49#include <linux/pci.h>
50#include <linux/io.h>
51#include <linux/delay.h>
52#include <linux/netdevice.h>
53#include <linux/vmalloc.h>
54#include <linux/module.h>
55#include <linux/prefetch.h>
8859b4a6 56#include <rdma/ib_verbs.h>
77241056
MM
57
58#include "hfi.h"
59#include "trace.h"
60#include "qp.h"
61#include "sdma.h"
0181ce31 62#include "debugfs.h"
d4829ea6 63#include "vnic.h"
77241056
MM
64
65#undef pr_fmt
66#define pr_fmt(fmt) DRIVER_NAME ": " fmt
67
68/*
69 * The size has to be longer than this string, so we can append
70 * board/chip information to it in the initialization code.
71 */
72const char ib_hfi1_version[] = HFI1_DRIVER_VERSION "\n";
73
74DEFINE_SPINLOCK(hfi1_devs_lock);
75LIST_HEAD(hfi1_dev_list);
76DEFINE_MUTEX(hfi1_mutex); /* general driver use */
77
78unsigned int hfi1_max_mtu = HFI1_DEFAULT_MAX_MTU;
79module_param_named(max_mtu, hfi1_max_mtu, uint, S_IRUGO);
ef699e84
SS
80MODULE_PARM_DESC(max_mtu, "Set max MTU bytes, default is " __stringify(
81 HFI1_DEFAULT_MAX_MTU));
77241056
MM
82
83unsigned int hfi1_cu = 1;
84module_param_named(cu, hfi1_cu, uint, S_IRUGO);
85MODULE_PARM_DESC(cu, "Credit return units");
86
87unsigned long hfi1_cap_mask = HFI1_CAP_MASK_DEFAULT;
f4cd8765
MR
88static int hfi1_caps_set(const char *val, const struct kernel_param *kp);
89static int hfi1_caps_get(char *buffer, const struct kernel_param *kp);
77241056
MM
90static const struct kernel_param_ops cap_ops = {
91 .set = hfi1_caps_set,
92 .get = hfi1_caps_get
93};
94module_param_cb(cap_mask, &cap_ops, &hfi1_cap_mask, S_IWUSR | S_IRUGO);
95MODULE_PARM_DESC(cap_mask, "Bit mask of enabled/disabled HW features");
96
97MODULE_LICENSE("Dual BSD/GPL");
98MODULE_DESCRIPTION("Intel Omni-Path Architecture driver");
77241056
MM
99
100/*
101 * MAX_PKT_RCV is the max # if packets processed per receive interrupt.
102 */
103#define MAX_PKT_RECV 64
a82a7fcd
MM
104/*
105 * MAX_PKT_THREAD_RCV is the max # of packets processed before
106 * the qp_wait_list queue is flushed.
107 */
108#define MAX_PKT_RECV_THREAD (MAX_PKT_RECV * 4)
77241056
MM
109#define EGR_HEAD_UPDATE_THRESHOLD 16
110
111struct hfi1_ib_stats hfi1_stats;
112
113static int hfi1_caps_set(const char *val, const struct kernel_param *kp)
114{
115 int ret = 0;
116 unsigned long *cap_mask_ptr = (unsigned long *)kp->arg,
117 cap_mask = *cap_mask_ptr, value, diff,
118 write_mask = ((HFI1_CAP_WRITABLE_MASK << HFI1_CAP_USER_SHIFT) |
119 HFI1_CAP_WRITABLE_MASK);
120
121 ret = kstrtoul(val, 0, &value);
122 if (ret) {
123 pr_warn("Invalid module parameter value for 'cap_mask'\n");
124 goto done;
125 }
126 /* Get the changed bits (except the locked bit) */
127 diff = value ^ (cap_mask & ~HFI1_CAP_LOCKED_SMASK);
128
129 /* Remove any bits that are not allowed to change after driver load */
130 if (HFI1_CAP_LOCKED() && (diff & ~write_mask)) {
131 pr_warn("Ignoring non-writable capability bits %#lx\n",
132 diff & ~write_mask);
133 diff &= write_mask;
134 }
135
136 /* Mask off any reserved bits */
137 diff &= ~HFI1_CAP_RESERVED_MASK;
138 /* Clear any previously set and changing bits */
139 cap_mask &= ~diff;
140 /* Update the bits with the new capability */
141 cap_mask |= (value & diff);
142 /* Check for any kernel/user restrictions */
143 diff = (cap_mask & (HFI1_CAP_MUST_HAVE_KERN << HFI1_CAP_USER_SHIFT)) ^
144 ((cap_mask & HFI1_CAP_MUST_HAVE_KERN) << HFI1_CAP_USER_SHIFT);
145 cap_mask &= ~diff;
146 /* Set the bitmask to the final set */
147 *cap_mask_ptr = cap_mask;
148done:
149 return ret;
150}
151
152static int hfi1_caps_get(char *buffer, const struct kernel_param *kp)
153{
154 unsigned long cap_mask = *(unsigned long *)kp->arg;
155
156 cap_mask &= ~HFI1_CAP_LOCKED_SMASK;
157 cap_mask |= ((cap_mask & HFI1_CAP_K2U) << HFI1_CAP_USER_SHIFT);
158
159 return scnprintf(buffer, PAGE_SIZE, "0x%lx", cap_mask);
160}
161
49dbb6cf
DD
162struct pci_dev *get_pci_dev(struct rvt_dev_info *rdi)
163{
164 struct hfi1_ibdev *ibdev = container_of(rdi, struct hfi1_ibdev, rdi);
165 struct hfi1_devdata *dd = container_of(ibdev,
166 struct hfi1_devdata, verbs_dev);
167 return dd->pcidev;
168}
169
77241056
MM
170/*
171 * Return count of units with at least one port ACTIVE.
172 */
173int hfi1_count_active_units(void)
174{
175 struct hfi1_devdata *dd;
176 struct hfi1_pportdata *ppd;
177 unsigned long flags;
178 int pidx, nunits_active = 0;
179
180 spin_lock_irqsave(&hfi1_devs_lock, flags);
181 list_for_each_entry(dd, &hfi1_dev_list, list) {
cb51c5d2 182 if (!(dd->flags & HFI1_PRESENT) || !dd->kregbase1)
77241056
MM
183 continue;
184 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
185 ppd = dd->pport + pidx;
186 if (ppd->lid && ppd->linkup) {
187 nunits_active++;
188 break;
189 }
190 }
191 }
192 spin_unlock_irqrestore(&hfi1_devs_lock, flags);
193 return nunits_active;
194}
195
77241056
MM
196/*
197 * Get address of eager buffer from it's index (allocated in chunks, not
198 * contiguous).
199 */
200static inline void *get_egrbuf(const struct hfi1_ctxtdata *rcd, u64 rhf,
201 u8 *update)
202{
203 u32 idx = rhf_egr_index(rhf), offset = rhf_egr_buf_offset(rhf);
204
205 *update |= !(idx & (rcd->egrbufs.threshold - 1)) && !offset;
206 return (void *)(((u64)(rcd->egrbufs.rcvtids[idx].addr)) +
207 (offset * RCV_BUF_BLOCK_SIZE));
208}
209
9039746c
DH
210static inline void *hfi1_get_header(struct hfi1_devdata *dd,
211 __le32 *rhf_addr)
212{
213 u32 offset = rhf_hdrq_offset(rhf_to_cpu(rhf_addr));
214
215 return (void *)(rhf_addr - dd->rhf_offset + offset);
216}
217
218static inline struct ib_header *hfi1_get_msgheader(struct hfi1_devdata *dd,
219 __le32 *rhf_addr)
220{
221 return (struct ib_header *)hfi1_get_header(dd, rhf_addr);
222}
223
72c07e2b
DH
224static inline struct hfi1_16b_header
225 *hfi1_get_16B_header(struct hfi1_devdata *dd,
226 __le32 *rhf_addr)
227{
228 return (struct hfi1_16b_header *)hfi1_get_header(dd, rhf_addr);
229}
230
77241056
MM
231/*
232 * Validate and encode the a given RcvArray Buffer size.
233 * The function will check whether the given size falls within
234 * allowed size ranges for the respective type and, optionally,
235 * return the proper encoding.
236 */
a82a7fcd 237int hfi1_rcvbuf_validate(u32 size, u8 type, u16 *encoded)
77241056 238{
f5389660 239 if (unlikely(!PAGE_ALIGNED(size)))
77241056
MM
240 return 0;
241 if (unlikely(size < MIN_EAGER_BUFFER))
242 return 0;
243 if (size >
244 (type == PT_EAGER ? MAX_EAGER_BUFFER : MAX_EXPECTED_BUFFER))
245 return 0;
246 if (encoded)
247 *encoded = ilog2(size / PAGE_SIZE) + 1;
248 return 1;
249}
250
251static void rcv_hdrerr(struct hfi1_ctxtdata *rcd, struct hfi1_pportdata *ppd,
252 struct hfi1_packet *packet)
253{
261a4351 254 struct ib_header *rhdr = packet->hdr;
77241056 255 u32 rte = rhf_rcv_type_err(packet->rhf);
5786adf3 256 u32 mlid_base;
f3e862cb 257 struct hfi1_ibport *ibp = rcd_to_iport(rcd);
ec4274f1
DD
258 struct hfi1_devdata *dd = ppd->dd;
259 struct rvt_dev_info *rdi = &dd->verbs_dev.rdi;
77241056
MM
260
261 if (packet->rhf & (RHF_VCRC_ERR | RHF_ICRC_ERR))
262 return;
263
5786adf3 264 if (packet->etype == RHF_RCV_TYPE_BYPASS) {
9039746c 265 goto drop;
5786adf3
DH
266 } else {
267 u8 lnh = ib_get_lnh(rhdr);
268
269 mlid_base = be16_to_cpu(IB_MULTICAST_LID_BASE);
270 if (lnh == HFI1_LRH_BTH) {
271 packet->ohdr = &rhdr->u.oth;
272 } else if (lnh == HFI1_LRH_GRH) {
273 packet->ohdr = &rhdr->u.l.oth;
274 packet->grh = &rhdr->u.l.grh;
275 } else {
276 goto drop;
277 }
9039746c
DH
278 }
279
77241056
MM
280 if (packet->rhf & RHF_TID_ERR) {
281 /* For TIDERR and RC QPs preemptively schedule a NAK */
77241056 282 u32 tlen = rhf_pkt_len(packet->rhf); /* in bytes */
9039746c 283 u32 dlid = ib_get_dlid(rhdr);
77241056 284 u32 qp_num;
77241056
MM
285
286 /* Sanity check packet */
287 if (tlen < 24)
288 goto drop;
289
290 /* Check for GRH */
5786adf3 291 if (packet->grh) {
77241056 292 u32 vtf;
9039746c 293 struct ib_grh *grh = packet->grh;
77241056 294
9039746c 295 if (grh->next_hdr != IB_GRH_NEXT_HDR)
77241056 296 goto drop;
9039746c 297 vtf = be32_to_cpu(grh->version_tclass_flow);
77241056
MM
298 if ((vtf >> IB_GRH_VERSION_SHIFT) != IB_GRH_VERSION)
299 goto drop;
e490974e 300 }
9039746c 301
77241056 302 /* Get the destination QP number. */
9039746c
DH
303 qp_num = ib_bth_get_qpn(packet->ohdr);
304 if (dlid < mlid_base) {
895420dd 305 struct rvt_qp *qp;
b77d713a 306 unsigned long flags;
77241056
MM
307
308 rcu_read_lock();
ec4274f1 309 qp = rvt_lookup_qpn(rdi, &ibp->rvp, qp_num);
77241056
MM
310 if (!qp) {
311 rcu_read_unlock();
312 goto drop;
313 }
314
315 /*
316 * Handle only RC QPs - for other QP types drop error
317 * packet.
318 */
b77d713a 319 spin_lock_irqsave(&qp->r_lock, flags);
77241056
MM
320
321 /* Check for valid receive state. */
83693bd1
DD
322 if (!(ib_rvt_state_ops[qp->state] &
323 RVT_PROCESS_RECV_OK)) {
4eb06882 324 ibp->rvp.n_pkt_drops++;
77241056
MM
325 }
326
327 switch (qp->ibqp.qp_type) {
328 case IB_QPT_RC:
9039746c 329 hfi1_rc_hdrerr(rcd, packet, qp);
77241056
MM
330 break;
331 default:
332 /* For now don't handle any other QP types */
333 break;
334 }
335
b77d713a 336 spin_unlock_irqrestore(&qp->r_lock, flags);
77241056
MM
337 rcu_read_unlock();
338 } /* Unicast QP */
339 } /* Valid packet with TIDErr */
340
341 /* handle "RcvTypeErr" flags */
342 switch (rte) {
343 case RHF_RTE_ERROR_OP_CODE_ERR:
344 {
77241056 345 void *ebuf = NULL;
9039746c 346 u8 opcode;
77241056
MM
347
348 if (rhf_use_egr_bfr(packet->rhf))
349 ebuf = packet->ebuf;
350
d125a6c6 351 if (!ebuf)
77241056
MM
352 goto drop; /* this should never happen */
353
9039746c 354 opcode = ib_bth_get_opcode(packet->ohdr);
77241056
MM
355 if (opcode == IB_OPCODE_CNP) {
356 /*
357 * Only in pre-B0 h/w is the CNP_OPCODE handled
80e4898e 358 * via this code path.
77241056 359 */
895420dd 360 struct rvt_qp *qp = NULL;
77241056
MM
361 u32 lqpn, rqpn;
362 u16 rlid;
363 u8 svc_type, sl, sc5;
364
aad559c2 365 sc5 = hfi1_9B_get_sc5(rhdr, packet->rhf);
77241056
MM
366 sl = ibp->sc_to_sl[sc5];
367
9039746c 368 lqpn = ib_bth_get_qpn(packet->ohdr);
77241056 369 rcu_read_lock();
ec4274f1 370 qp = rvt_lookup_qpn(rdi, &ibp->rvp, lqpn);
d125a6c6 371 if (!qp) {
77241056
MM
372 rcu_read_unlock();
373 goto drop;
374 }
375
376 switch (qp->ibqp.qp_type) {
377 case IB_QPT_UD:
378 rlid = 0;
379 rqpn = 0;
380 svc_type = IB_CC_SVCTYPE_UD;
381 break;
382 case IB_QPT_UC:
cb427057 383 rlid = ib_get_slid(rhdr);
77241056
MM
384 rqpn = qp->remote_qpn;
385 svc_type = IB_CC_SVCTYPE_UC;
386 break;
387 default:
388 goto drop;
389 }
390
391 process_becn(ppd, sl, rlid, lqpn, rqpn, svc_type);
392 rcu_read_unlock();
393 }
394
395 packet->rhf &= ~RHF_RCV_TYPE_ERR_SMASK;
396 break;
397 }
398 default:
399 break;
400 }
401
402drop:
403 return;
404}
405
406static inline void init_packet(struct hfi1_ctxtdata *rcd,
17fb4f29 407 struct hfi1_packet *packet)
77241056 408{
77241056
MM
409 packet->rsize = rcd->rcvhdrqentsize; /* words */
410 packet->maxcnt = rcd->rcvhdrq_cnt * packet->rsize; /* words */
411 packet->rcd = rcd;
412 packet->updegr = 0;
413 packet->etail = -1;
f4f30031 414 packet->rhf_addr = get_rhf_addr(rcd);
77241056
MM
415 packet->rhf = rhf_to_cpu(packet->rhf_addr);
416 packet->rhqoff = rcd->head;
417 packet->numpkt = 0;
77241056
MM
418}
419
e2fdbc23
BVA
420/* We support only two types - 9B and 16B for now */
421static const hfi1_handle_cnp hfi1_handle_cnp_tbl[2] = {
422 [HFI1_PKT_TYPE_9B] = &return_cnp,
423 [HFI1_PKT_TYPE_16B] = &return_cnp_16B
424};
425
5fd2b562
MH
426void hfi1_process_ecn_slowpath(struct rvt_qp *qp, struct hfi1_packet *pkt,
427 bool do_cnp)
77241056
MM
428{
429 struct hfi1_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
261a4351 430 struct ib_other_headers *ohdr = pkt->ohdr;
9039746c 431 struct ib_grh *grh = pkt->grh;
5fd2b562 432 u32 rqpn = 0, bth1;
88733e3b
DH
433 u16 pkey, rlid, dlid = ib_get_dlid(pkt->hdr);
434 u8 hdr_type, sc, svc_type;
5fd2b562
MH
435 bool is_mcast = false;
436
88733e3b
DH
437 if (pkt->etype == RHF_RCV_TYPE_BYPASS) {
438 is_mcast = hfi1_is_16B_mcast(dlid);
439 pkey = hfi1_16B_get_pkey(pkt->hdr);
440 sc = hfi1_16B_get_sc(pkt->hdr);
441 hdr_type = HFI1_PKT_TYPE_16B;
442 } else {
443 is_mcast = (dlid > be16_to_cpu(IB_MULTICAST_LID_BASE)) &&
444 (dlid != be16_to_cpu(IB_LID_PERMISSIVE));
445 pkey = ib_bth_get_pkey(ohdr);
446 sc = hfi1_9B_get_sc5(pkt->hdr, pkt->rhf);
447 hdr_type = HFI1_PKT_TYPE_9B;
448 }
449
77241056 450 switch (qp->ibqp.qp_type) {
977940b8
AK
451 case IB_QPT_SMI:
452 case IB_QPT_GSI:
77241056 453 case IB_QPT_UD:
88733e3b
DH
454 rlid = ib_get_slid(pkt->hdr);
455 rqpn = ib_get_sqpn(pkt->ohdr);
77241056
MM
456 svc_type = IB_CC_SVCTYPE_UD;
457 break;
977940b8 458 case IB_QPT_UC:
d8966fcd 459 rlid = rdma_ah_get_dlid(&qp->remote_ah_attr);
977940b8
AK
460 rqpn = qp->remote_qpn;
461 svc_type = IB_CC_SVCTYPE_UC;
462 break;
463 case IB_QPT_RC:
d8966fcd 464 rlid = rdma_ah_get_dlid(&qp->remote_ah_attr);
977940b8
AK
465 rqpn = qp->remote_qpn;
466 svc_type = IB_CC_SVCTYPE_RC;
467 break;
77241056
MM
468 default:
469 return;
470 }
471
5fd2b562 472 bth1 = be32_to_cpu(ohdr->bth[1]);
88733e3b
DH
473 /* Call appropriate CNP handler */
474 if (do_cnp && (bth1 & IB_FECN_SMASK))
475 hfi1_handle_cnp_tbl[hdr_type](ibp, qp, rqpn, pkey,
476 dlid, rlid, sc, grh);
77241056 477
3d591099 478 if (!is_mcast && (bth1 & IB_BECN_SMASK)) {
77241056 479 struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
ec4274f1 480 u32 lqpn = bth1 & RVT_QPN_MASK;
5fd2b562 481 u8 sl = ibp->sc_to_sl[sc];
77241056 482
977940b8 483 process_becn(ppd, sl, rlid, lqpn, rqpn, svc_type);
77241056 484 }
5fd2b562 485
77241056
MM
486}
487
488struct ps_mdata {
489 struct hfi1_ctxtdata *rcd;
490 u32 rsize;
491 u32 maxcnt;
492 u32 ps_head;
493 u32 ps_tail;
494 u32 ps_seq;
495};
496
497static inline void init_ps_mdata(struct ps_mdata *mdata,
498 struct hfi1_packet *packet)
499{
500 struct hfi1_ctxtdata *rcd = packet->rcd;
501
502 mdata->rcd = rcd;
503 mdata->rsize = packet->rsize;
504 mdata->maxcnt = packet->maxcnt;
3e7ccca0 505 mdata->ps_head = packet->rhqoff;
77241056 506
82c2611d 507 if (HFI1_CAP_KGET_MASK(rcd->flags, DMA_RTAIL)) {
3e7ccca0 508 mdata->ps_tail = get_rcvhdrtail(rcd);
82c2611d
NV
509 if (rcd->ctxt == HFI1_CTRL_CTXT)
510 mdata->ps_seq = rcd->seq_cnt;
511 else
512 mdata->ps_seq = 0; /* not used with DMA_RTAIL */
77241056
MM
513 } else {
514 mdata->ps_tail = 0; /* used only with DMA_RTAIL*/
515 mdata->ps_seq = rcd->seq_cnt;
516 }
517}
518
82c2611d
NV
519static inline int ps_done(struct ps_mdata *mdata, u64 rhf,
520 struct hfi1_ctxtdata *rcd)
77241056 521{
82c2611d 522 if (HFI1_CAP_KGET_MASK(rcd->flags, DMA_RTAIL))
77241056
MM
523 return mdata->ps_head == mdata->ps_tail;
524 return mdata->ps_seq != rhf_rcv_seq(rhf);
525}
526
82c2611d
NV
527static inline int ps_skip(struct ps_mdata *mdata, u64 rhf,
528 struct hfi1_ctxtdata *rcd)
529{
530 /*
531 * Control context can potentially receive an invalid rhf.
532 * Drop such packets.
533 */
534 if ((rcd->ctxt == HFI1_CTRL_CTXT) && (mdata->ps_head != mdata->ps_tail))
535 return mdata->ps_seq != rhf_rcv_seq(rhf);
536
537 return 0;
538}
539
540static inline void update_ps_mdata(struct ps_mdata *mdata,
541 struct hfi1_ctxtdata *rcd)
77241056 542{
77241056 543 mdata->ps_head += mdata->rsize;
3e7ccca0 544 if (mdata->ps_head >= mdata->maxcnt)
77241056 545 mdata->ps_head = 0;
82c2611d
NV
546
547 /* Control context must do seq counting */
548 if (!HFI1_CAP_KGET_MASK(rcd->flags, DMA_RTAIL) ||
549 (rcd->ctxt == HFI1_CTRL_CTXT)) {
77241056
MM
550 if (++mdata->ps_seq > 13)
551 mdata->ps_seq = 1;
552 }
553}
554
555/*
556 * prescan_rxq - search through the receive queue looking for packets
557 * containing Excplicit Congestion Notifications (FECNs, or BECNs).
558 * When an ECN is found, process the Congestion Notification, and toggle
559 * it off.
6c9e50f8
VM
560 * This is declared as a macro to allow quick checking of the port to avoid
561 * the overhead of a function call if not enabled.
77241056 562 */
6c9e50f8
VM
563#define prescan_rxq(rcd, packet) \
564 do { \
565 if (rcd->ppd->cc_prescan) \
566 __prescan_rxq(packet); \
567 } while (0)
568static void __prescan_rxq(struct hfi1_packet *packet)
77241056
MM
569{
570 struct hfi1_ctxtdata *rcd = packet->rcd;
571 struct ps_mdata mdata;
572
77241056
MM
573 init_ps_mdata(&mdata, packet);
574
575 while (1) {
576 struct hfi1_devdata *dd = rcd->dd;
f3e862cb 577 struct hfi1_ibport *ibp = rcd_to_iport(rcd);
50e5dcbe 578 __le32 *rhf_addr = (__le32 *)rcd->rcvhdrq + mdata.ps_head +
77241056 579 dd->rhf_offset;
895420dd 580 struct rvt_qp *qp;
261a4351 581 struct ib_header *hdr;
ec4274f1 582 struct rvt_dev_info *rdi = &dd->verbs_dev.rdi;
77241056 583 u64 rhf = rhf_to_cpu(rhf_addr);
977940b8 584 u32 etype = rhf_rcv_type(rhf), qpn, bth1;
77241056
MM
585 int is_ecn = 0;
586 u8 lnh;
587
82c2611d 588 if (ps_done(&mdata, rhf, rcd))
77241056
MM
589 break;
590
82c2611d
NV
591 if (ps_skip(&mdata, rhf, rcd))
592 goto next;
593
77241056
MM
594 if (etype != RHF_RCV_TYPE_IB)
595 goto next;
596
f2d8a0b3
DC
597 packet->hdr = hfi1_get_msgheader(dd, rhf_addr);
598 hdr = packet->hdr;
cb427057 599 lnh = ib_get_lnh(hdr);
77241056 600
f2d8a0b3
DC
601 if (lnh == HFI1_LRH_BTH) {
602 packet->ohdr = &hdr->u.oth;
9039746c 603 packet->grh = NULL;
f2d8a0b3
DC
604 } else if (lnh == HFI1_LRH_GRH) {
605 packet->ohdr = &hdr->u.l.oth;
9039746c 606 packet->grh = &hdr->u.l.grh;
f2d8a0b3 607 } else {
77241056 608 goto next; /* just in case */
f2d8a0b3 609 }
5fd2b562 610
f2d8a0b3 611 bth1 = be32_to_cpu(packet->ohdr->bth[1]);
3d591099 612 is_ecn = !!(bth1 & (IB_FECN_SMASK | IB_BECN_SMASK));
77241056
MM
613
614 if (!is_ecn)
615 goto next;
616
ec4274f1 617 qpn = bth1 & RVT_QPN_MASK;
77241056 618 rcu_read_lock();
ec4274f1 619 qp = rvt_lookup_qpn(rdi, &ibp->rvp, qpn);
77241056 620
d125a6c6 621 if (!qp) {
77241056
MM
622 rcu_read_unlock();
623 goto next;
624 }
625
5fd2b562 626 process_ecn(qp, packet, true);
77241056 627 rcu_read_unlock();
977940b8
AK
628
629 /* turn off BECN, FECN */
3d591099 630 bth1 &= ~(IB_FECN_SMASK | IB_BECN_SMASK);
f2d8a0b3 631 packet->ohdr->bth[1] = cpu_to_be32(bth1);
77241056 632next:
82c2611d 633 update_ps_mdata(&mdata, rcd);
77241056
MM
634 }
635}
82c2611d 636
bdaf96f6 637static void process_rcv_qp_work(struct hfi1_packet *packet)
a82a7fcd
MM
638{
639 struct rvt_qp *qp, *nqp;
bdaf96f6 640 struct hfi1_ctxtdata *rcd = packet->rcd;
a82a7fcd
MM
641
642 /*
643 * Iterate over all QPs waiting to respond.
644 * The list won't change since the IRQ is only run on one CPU.
645 */
646 list_for_each_entry_safe(qp, nqp, &rcd->qp_wait_list, rspwait) {
647 list_del_init(&qp->rspwait);
648 if (qp->r_flags & RVT_R_RSP_NAK) {
649 qp->r_flags &= ~RVT_R_RSP_NAK;
bdaf96f6
SS
650 packet->qp = qp;
651 hfi1_send_rc_ack(packet, 0);
a82a7fcd
MM
652 }
653 if (qp->r_flags & RVT_R_RSP_SEND) {
654 unsigned long flags;
655
656 qp->r_flags &= ~RVT_R_RSP_SEND;
657 spin_lock_irqsave(&qp->s_lock, flags);
658 if (ib_rvt_state_ops[qp->state] &
659 RVT_PROCESS_OR_FLUSH_SEND)
660 hfi1_schedule_send(qp);
661 spin_unlock_irqrestore(&qp->s_lock, flags);
662 }
663 rvt_put_qp(qp);
664 }
665}
666
667static noinline int max_packet_exceeded(struct hfi1_packet *packet, int thread)
668{
669 if (thread) {
670 if ((packet->numpkt & (MAX_PKT_RECV_THREAD - 1)) == 0)
671 /* allow defered processing */
bdaf96f6 672 process_rcv_qp_work(packet);
a82a7fcd
MM
673 cond_resched();
674 return RCV_PKT_OK;
675 } else {
676 this_cpu_inc(*packet->rcd->dd->rcv_limit);
677 return RCV_PKT_LIMIT;
678 }
679}
680
681static inline int check_max_packet(struct hfi1_packet *packet, int thread)
82c2611d
NV
682{
683 int ret = RCV_PKT_OK;
684
a82a7fcd
MM
685 if (unlikely((packet->numpkt & (MAX_PKT_RECV - 1)) == 0))
686 ret = max_packet_exceeded(packet, thread);
687 return ret;
688}
689
690static noinline int skip_rcv_packet(struct hfi1_packet *packet, int thread)
691{
692 int ret;
693
82c2611d
NV
694 /* Set up for the next packet */
695 packet->rhqoff += packet->rsize;
696 if (packet->rhqoff >= packet->maxcnt)
697 packet->rhqoff = 0;
698
699 packet->numpkt++;
a82a7fcd 700 ret = check_max_packet(packet, thread);
82c2611d
NV
701
702 packet->rhf_addr = (__le32 *)packet->rcd->rcvhdrq + packet->rhqoff +
703 packet->rcd->dd->rhf_offset;
704 packet->rhf = rhf_to_cpu(packet->rhf_addr);
705
706 return ret;
707}
77241056 708
f4f30031 709static inline int process_rcv_packet(struct hfi1_packet *packet, int thread)
77241056 710{
a82a7fcd 711 int ret;
77241056 712
77241056 713 packet->etype = rhf_rcv_type(packet->rhf);
9039746c 714
77241056
MM
715 /* total length */
716 packet->tlen = rhf_pkt_len(packet->rhf); /* in bytes */
717 /* retrieve eager buffer details */
718 packet->ebuf = NULL;
719 if (rhf_use_egr_bfr(packet->rhf)) {
720 packet->etail = rhf_egr_index(packet->rhf);
721 packet->ebuf = get_egrbuf(packet->rcd, packet->rhf,
722 &packet->updegr);
723 /*
724 * Prefetch the contents of the eager buffer. It is
725 * OK to send a negative length to prefetch_range().
726 * The +2 is the size of the RHF.
727 */
728 prefetch_range(packet->ebuf,
17fb4f29
JJ
729 packet->tlen - ((packet->rcd->rcvhdrqentsize -
730 (rhf_hdrq_offset(packet->rhf)
731 + 2)) * 4));
77241056
MM
732 }
733
734 /*
735 * Call a type specific handler for the packet. We
736 * should be able to trust that etype won't be beyond
737 * the range of valid indexes. If so something is really
738 * wrong and we can probably just let things come
739 * crashing down. There is no need to eat another
740 * comparison in this performance critical code.
741 */
742 packet->rcd->dd->rhf_rcv_function_map[packet->etype](packet);
743 packet->numpkt++;
744
745 /* Set up for the next packet */
746 packet->rhqoff += packet->rsize;
747 if (packet->rhqoff >= packet->maxcnt)
748 packet->rhqoff = 0;
749
a82a7fcd 750 ret = check_max_packet(packet, thread);
77241056 751
50e5dcbe 752 packet->rhf_addr = (__le32 *)packet->rcd->rcvhdrq + packet->rhqoff +
77241056
MM
753 packet->rcd->dd->rhf_offset;
754 packet->rhf = rhf_to_cpu(packet->rhf_addr);
755
756 return ret;
757}
758
759static inline void process_rcv_update(int last, struct hfi1_packet *packet)
760{
761 /*
762 * Update head regs etc., every 16 packets, if not last pkt,
763 * to help prevent rcvhdrq overflows, when many packets
764 * are processed and queue is nearly full.
765 * Don't request an interrupt for intermediate updates.
766 */
767 if (!last && !(packet->numpkt & 0xf)) {
768 update_usrhead(packet->rcd, packet->rhqoff, packet->updegr,
769 packet->etail, 0, 0);
770 packet->updegr = 0;
771 }
9039746c 772 packet->grh = NULL;
77241056
MM
773}
774
775static inline void finish_packet(struct hfi1_packet *packet)
776{
77241056
MM
777 /*
778 * Nothing we need to free for the packet.
779 *
780 * The only thing we need to do is a final update and call for an
781 * interrupt
782 */
783 update_usrhead(packet->rcd, packet->rcd->head, packet->updegr,
784 packet->etail, rcv_intr_dynamic, packet->numpkt);
77241056
MM
785}
786
77241056
MM
787/*
788 * Handle receive interrupts when using the no dma rtail option.
789 */
f4f30031 790int handle_receive_interrupt_nodma_rtail(struct hfi1_ctxtdata *rcd, int thread)
77241056
MM
791{
792 u32 seq;
f4f30031 793 int last = RCV_PKT_OK;
77241056
MM
794 struct hfi1_packet packet;
795
796 init_packet(rcd, &packet);
797 seq = rhf_rcv_seq(packet.rhf);
f4f30031
DL
798 if (seq != rcd->seq_cnt) {
799 last = RCV_PKT_DONE;
77241056 800 goto bail;
f4f30031 801 }
77241056 802
6c9e50f8 803 prescan_rxq(rcd, &packet);
77241056 804
f4f30031
DL
805 while (last == RCV_PKT_OK) {
806 last = process_rcv_packet(&packet, thread);
77241056
MM
807 seq = rhf_rcv_seq(packet.rhf);
808 if (++rcd->seq_cnt > 13)
809 rcd->seq_cnt = 1;
810 if (seq != rcd->seq_cnt)
f4f30031 811 last = RCV_PKT_DONE;
77241056
MM
812 process_rcv_update(last, &packet);
813 }
bdaf96f6 814 process_rcv_qp_work(&packet);
a82a7fcd 815 rcd->head = packet.rhqoff;
77241056
MM
816bail:
817 finish_packet(&packet);
f4f30031 818 return last;
77241056
MM
819}
820
f4f30031 821int handle_receive_interrupt_dma_rtail(struct hfi1_ctxtdata *rcd, int thread)
77241056
MM
822{
823 u32 hdrqtail;
f4f30031 824 int last = RCV_PKT_OK;
77241056
MM
825 struct hfi1_packet packet;
826
827 init_packet(rcd, &packet);
828 hdrqtail = get_rcvhdrtail(rcd);
f4f30031
DL
829 if (packet.rhqoff == hdrqtail) {
830 last = RCV_PKT_DONE;
77241056 831 goto bail;
f4f30031 832 }
77241056
MM
833 smp_rmb(); /* prevent speculative reads of dma'ed hdrq */
834
6c9e50f8 835 prescan_rxq(rcd, &packet);
77241056 836
f4f30031
DL
837 while (last == RCV_PKT_OK) {
838 last = process_rcv_packet(&packet, thread);
77241056 839 if (packet.rhqoff == hdrqtail)
f4f30031 840 last = RCV_PKT_DONE;
77241056
MM
841 process_rcv_update(last, &packet);
842 }
bdaf96f6 843 process_rcv_qp_work(&packet);
a82a7fcd 844 rcd->head = packet.rhqoff;
77241056
MM
845bail:
846 finish_packet(&packet);
f4f30031 847 return last;
77241056
MM
848}
849
e6f7622d 850static inline void set_nodma_rtail(struct hfi1_devdata *dd, u16 ctxt)
77241056 851{
d295dbeb 852 struct hfi1_ctxtdata *rcd;
e6f7622d 853 u16 i;
77241056 854
2280740f
VN
855 /*
856 * For dynamically allocated kernel contexts (like vnic) switch
857 * interrupt handler only for that context. Otherwise, switch
858 * interrupt handler for all statically allocated kernel contexts.
859 */
860 if (ctxt >= dd->first_dyn_alloc_ctxt) {
d59075ad 861 rcd = hfi1_rcd_get_by_index_safe(dd, ctxt);
d295dbeb
MR
862 if (rcd) {
863 rcd->do_interrupt =
864 &handle_receive_interrupt_nodma_rtail;
865 hfi1_rcd_put(rcd);
866 }
2280740f
VN
867 return;
868 }
869
d295dbeb
MR
870 for (i = HFI1_CTRL_CTXT + 1; i < dd->first_dyn_alloc_ctxt; i++) {
871 rcd = hfi1_rcd_get_by_index(dd, i);
872 if (rcd)
873 rcd->do_interrupt =
874 &handle_receive_interrupt_nodma_rtail;
875 hfi1_rcd_put(rcd);
876 }
77241056
MM
877}
878
e6f7622d 879static inline void set_dma_rtail(struct hfi1_devdata *dd, u16 ctxt)
77241056 880{
d295dbeb 881 struct hfi1_ctxtdata *rcd;
e6f7622d 882 u16 i;
77241056 883
2280740f
VN
884 /*
885 * For dynamically allocated kernel contexts (like vnic) switch
886 * interrupt handler only for that context. Otherwise, switch
887 * interrupt handler for all statically allocated kernel contexts.
888 */
889 if (ctxt >= dd->first_dyn_alloc_ctxt) {
d59075ad 890 rcd = hfi1_rcd_get_by_index_safe(dd, ctxt);
d295dbeb
MR
891 if (rcd) {
892 rcd->do_interrupt =
893 &handle_receive_interrupt_dma_rtail;
894 hfi1_rcd_put(rcd);
895 }
2280740f
VN
896 return;
897 }
898
d295dbeb
MR
899 for (i = HFI1_CTRL_CTXT + 1; i < dd->first_dyn_alloc_ctxt; i++) {
900 rcd = hfi1_rcd_get_by_index(dd, i);
901 if (rcd)
902 rcd->do_interrupt =
903 &handle_receive_interrupt_dma_rtail;
904 hfi1_rcd_put(rcd);
905 }
77241056
MM
906}
907
fb9036dd
JS
908void set_all_slowpath(struct hfi1_devdata *dd)
909{
d295dbeb 910 struct hfi1_ctxtdata *rcd;
e6f7622d 911 u16 i;
fb9036dd
JS
912
913 /* HFI1_CTRL_CTXT must always use the slow path interrupt handler */
2280740f 914 for (i = HFI1_CTRL_CTXT + 1; i < dd->num_rcv_contexts; i++) {
d295dbeb
MR
915 rcd = hfi1_rcd_get_by_index(dd, i);
916 if (!rcd)
917 continue;
cc9a97ea 918 if (i < dd->first_dyn_alloc_ctxt || rcd->is_vnic)
2280740f 919 rcd->do_interrupt = &handle_receive_interrupt;
cc9a97ea 920
d295dbeb 921 hfi1_rcd_put(rcd);
2280740f 922 }
fb9036dd
JS
923}
924
925static inline int set_armed_to_active(struct hfi1_ctxtdata *rcd,
c867caaf 926 struct hfi1_packet *packet,
fb9036dd
JS
927 struct hfi1_devdata *dd)
928{
929 struct work_struct *lsaw = &rcd->ppd->linkstate_active_work;
69b9f4a4 930 u8 etype = rhf_rcv_type(packet->rhf);
9039746c 931 u8 sc = SC15_PACKET;
fb9036dd 932
9039746c
DH
933 if (etype == RHF_RCV_TYPE_IB) {
934 struct ib_header *hdr = hfi1_get_msgheader(packet->rcd->dd,
935 packet->rhf_addr);
936 sc = hfi1_9B_get_sc5(hdr, packet->rhf);
72c07e2b
DH
937 } else if (etype == RHF_RCV_TYPE_BYPASS) {
938 struct hfi1_16b_header *hdr = hfi1_get_16B_header(
939 packet->rcd->dd,
940 packet->rhf_addr);
941 sc = hfi1_16B_get_sc(hdr);
9039746c
DH
942 }
943 if (sc != SC15_PACKET) {
bec7c79c 944 int hwstate = driver_lstate(rcd->ppd);
fb9036dd 945
bec7c79c
BJ
946 if (hwstate != IB_PORT_ACTIVE) {
947 dd_dev_info(dd,
948 "Unexpected link state %s\n",
949 opa_lstate_name(hwstate));
fb9036dd
JS
950 return 0;
951 }
952
71d47008 953 queue_work(rcd->ppd->link_wq, lsaw);
fb9036dd
JS
954 return 1;
955 }
956 return 0;
957}
958
77241056
MM
959/*
960 * handle_receive_interrupt - receive a packet
961 * @rcd: the context
962 *
963 * Called from interrupt handler for errors or receive interrupt.
964 * This is the slow path interrupt handler.
965 */
f4f30031 966int handle_receive_interrupt(struct hfi1_ctxtdata *rcd, int thread)
77241056 967{
77241056
MM
968 struct hfi1_devdata *dd = rcd->dd;
969 u32 hdrqtail;
82c2611d 970 int needset, last = RCV_PKT_OK;
77241056 971 struct hfi1_packet packet;
82c2611d
NV
972 int skip_pkt = 0;
973
974 /* Control context will always use the slow path interrupt handler */
975 needset = (rcd->ctxt == HFI1_CTRL_CTXT) ? 0 : 1;
77241056
MM
976
977 init_packet(rcd, &packet);
978
82c2611d 979 if (!HFI1_CAP_KGET_MASK(rcd->flags, DMA_RTAIL)) {
77241056
MM
980 u32 seq = rhf_rcv_seq(packet.rhf);
981
f4f30031
DL
982 if (seq != rcd->seq_cnt) {
983 last = RCV_PKT_DONE;
77241056 984 goto bail;
f4f30031 985 }
77241056
MM
986 hdrqtail = 0;
987 } else {
988 hdrqtail = get_rcvhdrtail(rcd);
f4f30031
DL
989 if (packet.rhqoff == hdrqtail) {
990 last = RCV_PKT_DONE;
77241056 991 goto bail;
f4f30031 992 }
77241056 993 smp_rmb(); /* prevent speculative reads of dma'ed hdrq */
82c2611d
NV
994
995 /*
996 * Control context can potentially receive an invalid
997 * rhf. Drop such packets.
998 */
999 if (rcd->ctxt == HFI1_CTRL_CTXT) {
1000 u32 seq = rhf_rcv_seq(packet.rhf);
1001
1002 if (seq != rcd->seq_cnt)
1003 skip_pkt = 1;
1004 }
77241056
MM
1005 }
1006
6c9e50f8 1007 prescan_rxq(rcd, &packet);
77241056 1008
f4f30031 1009 while (last == RCV_PKT_OK) {
17fb4f29
JJ
1010 if (unlikely(dd->do_drop &&
1011 atomic_xchg(&dd->drop_packet, DROP_PACKET_OFF) ==
1012 DROP_PACKET_ON)) {
77241056
MM
1013 dd->do_drop = 0;
1014
1015 /* On to the next packet */
1016 packet.rhqoff += packet.rsize;
50e5dcbe 1017 packet.rhf_addr = (__le32 *)rcd->rcvhdrq +
77241056
MM
1018 packet.rhqoff +
1019 dd->rhf_offset;
1020 packet.rhf = rhf_to_cpu(packet.rhf_addr);
1021
82c2611d
NV
1022 } else if (skip_pkt) {
1023 last = skip_rcv_packet(&packet, thread);
1024 skip_pkt = 0;
77241056 1025 } else {
fb9036dd
JS
1026 /* Auto activate link on non-SC15 packet receive */
1027 if (unlikely(rcd->ppd->host_link_state ==
1028 HLS_UP_ARMED) &&
c867caaf 1029 set_armed_to_active(rcd, &packet, dd))
fb9036dd 1030 goto bail;
f4f30031 1031 last = process_rcv_packet(&packet, thread);
77241056
MM
1032 }
1033
82c2611d 1034 if (!HFI1_CAP_KGET_MASK(rcd->flags, DMA_RTAIL)) {
77241056
MM
1035 u32 seq = rhf_rcv_seq(packet.rhf);
1036
1037 if (++rcd->seq_cnt > 13)
1038 rcd->seq_cnt = 1;
1039 if (seq != rcd->seq_cnt)
f4f30031 1040 last = RCV_PKT_DONE;
77241056 1041 if (needset) {
17fb4f29 1042 dd_dev_info(dd, "Switching to NO_DMA_RTAIL\n");
2280740f 1043 set_nodma_rtail(dd, rcd->ctxt);
77241056
MM
1044 needset = 0;
1045 }
1046 } else {
1047 if (packet.rhqoff == hdrqtail)
f4f30031 1048 last = RCV_PKT_DONE;
82c2611d
NV
1049 /*
1050 * Control context can potentially receive an invalid
1051 * rhf. Drop such packets.
1052 */
1053 if (rcd->ctxt == HFI1_CTRL_CTXT) {
1054 u32 seq = rhf_rcv_seq(packet.rhf);
1055
1056 if (++rcd->seq_cnt > 13)
1057 rcd->seq_cnt = 1;
1058 if (!last && (seq != rcd->seq_cnt))
1059 skip_pkt = 1;
1060 }
1061
77241056
MM
1062 if (needset) {
1063 dd_dev_info(dd,
1064 "Switching to DMA_RTAIL\n");
2280740f 1065 set_dma_rtail(dd, rcd->ctxt);
77241056
MM
1066 needset = 0;
1067 }
1068 }
1069
1070 process_rcv_update(last, &packet);
1071 }
1072
bdaf96f6 1073 process_rcv_qp_work(&packet);
a82a7fcd 1074 rcd->head = packet.rhqoff;
77241056
MM
1075
1076bail:
1077 /*
1078 * Always write head at end, and setup rcv interrupt, even
1079 * if no packets were processed.
1080 */
1081 finish_packet(&packet);
f4f30031 1082 return last;
77241056
MM
1083}
1084
fb9036dd
JS
1085/*
1086 * We may discover in the interrupt that the hardware link state has
1087 * changed from ARMED to ACTIVE (due to the arrival of a non-SC15 packet),
1088 * and we need to update the driver's notion of the link state. We cannot
1089 * run set_link_state from interrupt context, so we queue this function on
1090 * a workqueue.
1091 *
1092 * We delay the regular interrupt processing until after the state changes
1093 * so that the link will be in the correct state by the time any application
1094 * we wake up attempts to send a reply to any message it received.
1095 * (Subsequent receive interrupts may possibly force the wakeup before we
1096 * update the link state.)
1097 *
1098 * The rcd is freed in hfi1_free_ctxtdata after hfi1_postinit_cleanup invokes
1099 * dd->f_cleanup(dd) to disable the interrupt handler and flush workqueues,
1100 * so we're safe from use-after-free of the rcd.
1101 */
1102void receive_interrupt_work(struct work_struct *work)
1103{
1104 struct hfi1_pportdata *ppd = container_of(work, struct hfi1_pportdata,
1105 linkstate_active_work);
1106 struct hfi1_devdata *dd = ppd->dd;
d295dbeb 1107 struct hfi1_ctxtdata *rcd;
e6f7622d 1108 u16 i;
fb9036dd
JS
1109
1110 /* Received non-SC15 packet implies neighbor_normal */
1111 ppd->neighbor_normal = 1;
1112 set_link_state(ppd, HLS_UP_ACTIVE);
1113
1114 /*
2280740f
VN
1115 * Interrupt all statically allocated kernel contexts that could
1116 * have had an interrupt during auto activation.
fb9036dd 1117 */
d295dbeb
MR
1118 for (i = HFI1_CTRL_CTXT; i < dd->first_dyn_alloc_ctxt; i++) {
1119 rcd = hfi1_rcd_get_by_index(dd, i);
1120 if (rcd)
1121 force_recv_intr(rcd);
1122 hfi1_rcd_put(rcd);
1123 }
fb9036dd
JS
1124}
1125
77241056
MM
1126/*
1127 * Convert a given MTU size to the on-wire MAD packet enumeration.
1128 * Return -1 if the size is invalid.
1129 */
1130int mtu_to_enum(u32 mtu, int default_if_bad)
1131{
1132 switch (mtu) {
1133 case 0: return OPA_MTU_0;
1134 case 256: return OPA_MTU_256;
1135 case 512: return OPA_MTU_512;
1136 case 1024: return OPA_MTU_1024;
1137 case 2048: return OPA_MTU_2048;
1138 case 4096: return OPA_MTU_4096;
1139 case 8192: return OPA_MTU_8192;
1140 case 10240: return OPA_MTU_10240;
1141 }
1142 return default_if_bad;
1143}
1144
1145u16 enum_to_mtu(int mtu)
1146{
1147 switch (mtu) {
1148 case OPA_MTU_0: return 0;
1149 case OPA_MTU_256: return 256;
1150 case OPA_MTU_512: return 512;
1151 case OPA_MTU_1024: return 1024;
1152 case OPA_MTU_2048: return 2048;
1153 case OPA_MTU_4096: return 4096;
1154 case OPA_MTU_8192: return 8192;
1155 case OPA_MTU_10240: return 10240;
1156 default: return 0xffff;
1157 }
1158}
1159
1160/*
1161 * set_mtu - set the MTU
1162 * @ppd: the per port data
1163 *
1164 * We can handle "any" incoming size, the issue here is whether we
1165 * need to restrict our outgoing size. We do not deal with what happens
1166 * to programs that are already running when the size changes.
1167 */
1168int set_mtu(struct hfi1_pportdata *ppd)
1169{
1170 struct hfi1_devdata *dd = ppd->dd;
1171 int i, drain, ret = 0, is_up = 0;
1172
1173 ppd->ibmtu = 0;
1174 for (i = 0; i < ppd->vls_supported; i++)
1175 if (ppd->ibmtu < dd->vld[i].mtu)
1176 ppd->ibmtu = dd->vld[i].mtu;
1177 ppd->ibmaxlen = ppd->ibmtu + lrh_max_header_bytes(ppd->dd);
1178
1179 mutex_lock(&ppd->hls_lock);
d0d236ea
JJ
1180 if (ppd->host_link_state == HLS_UP_INIT ||
1181 ppd->host_link_state == HLS_UP_ARMED ||
1182 ppd->host_link_state == HLS_UP_ACTIVE)
77241056
MM
1183 is_up = 1;
1184
1185 drain = !is_ax(dd) && is_up;
1186
1187 if (drain)
1188 /*
1189 * MTU is specified per-VL. To ensure that no packet gets
1190 * stuck (due, e.g., to the MTU for the packet's VL being
1191 * reduced), empty the per-VL FIFOs before adjusting MTU.
1192 */
1193 ret = stop_drain_data_vls(dd);
1194
1195 if (ret) {
1196 dd_dev_err(dd, "%s: cannot stop/drain VLs - refusing to change per-VL MTUs\n",
1197 __func__);
1198 goto err;
1199 }
1200
1201 hfi1_set_ib_cfg(ppd, HFI1_IB_CFG_MTU, 0);
1202
1203 if (drain)
1204 open_fill_data_vls(dd); /* reopen all VLs */
1205
1206err:
1207 mutex_unlock(&ppd->hls_lock);
1208
1209 return ret;
1210}
1211
1212int hfi1_set_lid(struct hfi1_pportdata *ppd, u32 lid, u8 lmc)
1213{
1214 struct hfi1_devdata *dd = ppd->dd;
1215
1216 ppd->lid = lid;
1217 ppd->lmc = lmc;
1218 hfi1_set_ib_cfg(ppd, HFI1_IB_CFG_LIDLMC, 0);
1219
cde10afa 1220 dd_dev_info(dd, "port %u: got a lid: 0x%x\n", ppd->port, lid);
77241056
MM
1221
1222 return 0;
1223}
1224
91ab4ed3
EH
1225void shutdown_led_override(struct hfi1_pportdata *ppd)
1226{
1227 struct hfi1_devdata *dd = ppd->dd;
1228
409b1462 1229 /*
2243472e
EH
1230 * This pairs with the memory barrier in hfi1_start_led_override to
1231 * ensure that we read the correct state of LED beaconing represented
1232 * by led_override_timer_active
409b1462 1233 */
2243472e 1234 smp_rmb();
91ab4ed3
EH
1235 if (atomic_read(&ppd->led_override_timer_active)) {
1236 del_timer_sync(&ppd->led_override_timer);
1237 atomic_set(&ppd->led_override_timer_active, 0);
2243472e
EH
1238 /* Ensure the atomic_set is visible to all CPUs */
1239 smp_wmb();
91ab4ed3
EH
1240 }
1241
2243472e
EH
1242 /* Hand control of the LED to the DC for normal operation */
1243 write_csr(dd, DCC_CFG_LED_CNTRL, 0);
91ab4ed3 1244}
77241056 1245
8064135e 1246static void run_led_override(struct timer_list *t)
77241056 1247{
8064135e 1248 struct hfi1_pportdata *ppd = from_timer(ppd, t, led_override_timer);
77241056 1249 struct hfi1_devdata *dd = ppd->dd;
91ab4ed3
EH
1250 unsigned long timeout;
1251 int phase_idx;
77241056
MM
1252
1253 if (!(dd->flags & HFI1_INITTED))
1254 return;
1255
91ab4ed3 1256 phase_idx = ppd->led_override_phase & 1;
77241056 1257
91ab4ed3
EH
1258 setextled(dd, phase_idx);
1259
1260 timeout = ppd->led_override_vals[phase_idx];
2243472e 1261
91ab4ed3
EH
1262 /* Set up for next phase */
1263 ppd->led_override_phase = !ppd->led_override_phase;
77241056 1264
2243472e 1265 mod_timer(&ppd->led_override_timer, jiffies + timeout);
77241056
MM
1266}
1267
91ab4ed3
EH
1268/*
1269 * To have the LED blink in a particular pattern, provide timeon and timeoff
2243472e
EH
1270 * in milliseconds.
1271 * To turn off custom blinking and return to normal operation, use
1272 * shutdown_led_override()
91ab4ed3 1273 */
2243472e
EH
1274void hfi1_start_led_override(struct hfi1_pportdata *ppd, unsigned int timeon,
1275 unsigned int timeoff)
77241056 1276{
2243472e 1277 if (!(ppd->dd->flags & HFI1_INITTED))
77241056
MM
1278 return;
1279
91ab4ed3
EH
1280 /* Convert to jiffies for direct use in timer */
1281 ppd->led_override_vals[0] = msecs_to_jiffies(timeoff);
1282 ppd->led_override_vals[1] = msecs_to_jiffies(timeon);
77241056 1283
2243472e
EH
1284 /* Arbitrarily start from LED on phase */
1285 ppd->led_override_phase = 1;
77241056
MM
1286
1287 /*
1288 * If the timer has not already been started, do so. Use a "quick"
2243472e 1289 * timeout so the handler will be called soon to look at our request.
77241056 1290 */
2243472e 1291 if (!timer_pending(&ppd->led_override_timer)) {
8064135e 1292 timer_setup(&ppd->led_override_timer, run_led_override, 0);
77241056
MM
1293 ppd->led_override_timer.expires = jiffies + 1;
1294 add_timer(&ppd->led_override_timer);
2243472e
EH
1295 atomic_set(&ppd->led_override_timer_active, 1);
1296 /* Ensure the atomic_set is visible to all CPUs */
1297 smp_wmb();
77241056
MM
1298 }
1299}
1300
1301/**
1302 * hfi1_reset_device - reset the chip if possible
1303 * @unit: the device to reset
1304 *
1305 * Whether or not reset is successful, we attempt to re-initialize the chip
1306 * (that is, much like a driver unload/reload). We clear the INITTED flag
1307 * so that the various entry points will fail until we reinitialize. For
1308 * now, we only allow this if no user contexts are open that use chip resources
1309 */
1310int hfi1_reset_device(int unit)
1311{
e6f7622d 1312 int ret;
77241056
MM
1313 struct hfi1_devdata *dd = hfi1_lookup(unit);
1314 struct hfi1_pportdata *ppd;
77241056
MM
1315 int pidx;
1316
1317 if (!dd) {
1318 ret = -ENODEV;
1319 goto bail;
1320 }
1321
1322 dd_dev_info(dd, "Reset on unit %u requested\n", unit);
1323
cb51c5d2 1324 if (!dd->kregbase1 || !(dd->flags & HFI1_PRESENT)) {
77241056 1325 dd_dev_info(dd,
17fb4f29
JJ
1326 "Invalid unit number %u or not initialized or not present\n",
1327 unit);
77241056
MM
1328 ret = -ENXIO;
1329 goto bail;
1330 }
1331
d295dbeb
MR
1332 /* If there are any user/vnic contexts, we cannot reset */
1333 mutex_lock(&hfi1_mutex);
77241056 1334 if (dd->rcd)
d295dbeb
MR
1335 if (hfi1_stats.sps_ctxts) {
1336 mutex_unlock(&hfi1_mutex);
77241056
MM
1337 ret = -EBUSY;
1338 goto bail;
1339 }
d295dbeb 1340 mutex_unlock(&hfi1_mutex);
77241056
MM
1341
1342 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
1343 ppd = dd->pport + pidx;
77241056 1344
91ab4ed3 1345 shutdown_led_override(ppd);
77241056
MM
1346 }
1347 if (dd->flags & HFI1_HAS_SEND_DMA)
1348 sdma_exit(dd);
1349
1350 hfi1_reset_cpu_counters(dd);
1351
1352 ret = hfi1_init(dd, 1);
1353
1354 if (ret)
1355 dd_dev_err(dd,
17fb4f29
JJ
1356 "Reinitialize unit %u after reset failed with %d\n",
1357 unit, ret);
77241056
MM
1358 else
1359 dd_dev_info(dd, "Reinitialized unit %u after resetting\n",
17fb4f29 1360 unit);
77241056
MM
1361
1362bail:
1363 return ret;
1364}
1365
9039746c
DH
1366static inline void hfi1_setup_ib_header(struct hfi1_packet *packet)
1367{
1368 packet->hdr = (struct hfi1_ib_message_header *)
1369 hfi1_get_msgheader(packet->rcd->dd,
1370 packet->rhf_addr);
1371 packet->hlen = (u8 *)packet->rhf_addr - (u8 *)packet->hdr;
1372}
1373
5786adf3
DH
1374static int hfi1_bypass_ingress_pkt_check(struct hfi1_packet *packet)
1375{
1376 struct hfi1_pportdata *ppd = packet->rcd->ppd;
1377
1378 /* slid and dlid cannot be 0 */
1379 if ((!packet->slid) || (!packet->dlid))
1380 return -EINVAL;
1381
1382 /* Compare port lid with incoming packet dlid */
1383 if ((!(hfi1_is_16B_mcast(packet->dlid))) &&
1384 (packet->dlid !=
1385 opa_get_lid(be32_to_cpu(OPA_LID_PERMISSIVE), 16B))) {
1386 if (packet->dlid != ppd->lid)
1387 return -EINVAL;
1388 }
1389
1390 /* No multicast packets with SC15 */
1391 if ((hfi1_is_16B_mcast(packet->dlid)) && (packet->sc == 0xF))
1392 return -EINVAL;
1393
1394 /* Packets with permissive DLID always on SC15 */
1395 if ((packet->dlid == opa_get_lid(be32_to_cpu(OPA_LID_PERMISSIVE),
1396 16B)) &&
1397 (packet->sc != 0xF))
1398 return -EINVAL;
1399
1400 return 0;
1401}
1402
9039746c
DH
1403static int hfi1_setup_9B_packet(struct hfi1_packet *packet)
1404{
1405 struct hfi1_ibport *ibp = rcd_to_iport(packet->rcd);
1406 struct ib_header *hdr;
1407 u8 lnh;
1408
1409 hfi1_setup_ib_header(packet);
1410 hdr = packet->hdr;
1411
1412 lnh = ib_get_lnh(hdr);
1413 if (lnh == HFI1_LRH_BTH) {
1414 packet->ohdr = &hdr->u.oth;
1415 packet->grh = NULL;
1416 } else if (lnh == HFI1_LRH_GRH) {
1417 u32 vtf;
1418
1419 packet->ohdr = &hdr->u.l.oth;
1420 packet->grh = &hdr->u.l.grh;
1421 if (packet->grh->next_hdr != IB_GRH_NEXT_HDR)
1422 goto drop;
1423 vtf = be32_to_cpu(packet->grh->version_tclass_flow);
1424 if ((vtf >> IB_GRH_VERSION_SHIFT) != IB_GRH_VERSION)
1425 goto drop;
1426 } else {
1427 goto drop;
1428 }
1429
1430 /* Query commonly used fields from packet header */
72c07e2b 1431 packet->payload = packet->ebuf;
9039746c
DH
1432 packet->opcode = ib_bth_get_opcode(packet->ohdr);
1433 packet->slid = ib_get_slid(hdr);
1434 packet->dlid = ib_get_dlid(hdr);
72c07e2b
DH
1435 if (unlikely((packet->dlid >= be16_to_cpu(IB_MULTICAST_LID_BASE)) &&
1436 (packet->dlid != be16_to_cpu(IB_LID_PERMISSIVE))))
1437 packet->dlid += opa_get_mcast_base(OPA_MCAST_NR) -
1438 be16_to_cpu(IB_MULTICAST_LID_BASE);
9039746c
DH
1439 packet->sl = ib_get_sl(hdr);
1440 packet->sc = hfi1_9B_get_sc5(hdr, packet->rhf);
1441 packet->pad = ib_bth_get_pad(packet->ohdr);
1442 packet->extra_byte = 0;
6d6b8848
SS
1443 packet->pkey = ib_bth_get_pkey(packet->ohdr);
1444 packet->migrated = ib_bth_is_migration(packet->ohdr);
9039746c
DH
1445
1446 return 0;
1447drop:
1448 ibp->rvp.n_pkt_drops++;
1449 return -EINVAL;
1450}
1451
72c07e2b
DH
1452static int hfi1_setup_bypass_packet(struct hfi1_packet *packet)
1453{
1454 /*
1455 * Bypass packets have a different header/payload split
1456 * compared to an IB packet.
1457 * Current split is set such that 16 bytes of the actual
1458 * header is in the header buffer and the remining is in
1459 * the eager buffer. We chose 16 since hfi1 driver only
1460 * supports 16B bypass packets and we will be able to
1461 * receive the entire LRH with such a split.
1462 */
1463
1464 struct hfi1_ctxtdata *rcd = packet->rcd;
1465 struct hfi1_pportdata *ppd = rcd->ppd;
1466 struct hfi1_ibport *ibp = &ppd->ibport_data;
1467 u8 l4;
1468 u8 grh_len;
1469
1470 packet->hdr = (struct hfi1_16b_header *)
1471 hfi1_get_16B_header(packet->rcd->dd,
1472 packet->rhf_addr);
1473 packet->hlen = (u8 *)packet->rhf_addr - (u8 *)packet->hdr;
1474
1475 l4 = hfi1_16B_get_l4(packet->hdr);
1476 if (l4 == OPA_16B_L4_IB_LOCAL) {
1477 grh_len = 0;
1478 packet->ohdr = packet->ebuf;
1479 packet->grh = NULL;
1480 } else if (l4 == OPA_16B_L4_IB_GLOBAL) {
1481 u32 vtf;
1482
1483 grh_len = sizeof(struct ib_grh);
1484 packet->ohdr = packet->ebuf + grh_len;
1485 packet->grh = packet->ebuf;
1486 if (packet->grh->next_hdr != IB_GRH_NEXT_HDR)
1487 goto drop;
1488 vtf = be32_to_cpu(packet->grh->version_tclass_flow);
1489 if ((vtf >> IB_GRH_VERSION_SHIFT) != IB_GRH_VERSION)
1490 goto drop;
1491 } else {
1492 goto drop;
1493 }
1494
1495 /* Query commonly used fields from packet header */
1496 packet->opcode = ib_bth_get_opcode(packet->ohdr);
1497 packet->hlen = hdr_len_by_opcode[packet->opcode] + 8 + grh_len;
1498 packet->payload = packet->ebuf + packet->hlen - (4 * sizeof(u32));
1499 packet->slid = hfi1_16B_get_slid(packet->hdr);
1500 packet->dlid = hfi1_16B_get_dlid(packet->hdr);
1501 if (unlikely(hfi1_is_16B_mcast(packet->dlid)))
1502 packet->dlid += opa_get_mcast_base(OPA_MCAST_NR) -
1503 opa_get_lid(opa_get_mcast_base(OPA_MCAST_NR),
1504 16B);
1505 packet->sc = hfi1_16B_get_sc(packet->hdr);
1506 packet->sl = ibp->sc_to_sl[packet->sc];
1507 packet->pad = hfi1_16B_bth_get_pad(packet->ohdr);
1508 packet->extra_byte = SIZE_OF_LT;
6d6b8848
SS
1509 packet->pkey = hfi1_16B_get_pkey(packet->hdr);
1510 packet->migrated = opa_bth_is_migration(packet->ohdr);
72c07e2b 1511
5786adf3
DH
1512 if (hfi1_bypass_ingress_pkt_check(packet))
1513 goto drop;
1514
72c07e2b
DH
1515 return 0;
1516drop:
1517 hfi1_cdbg(PKT, "%s: packet dropped\n", __func__);
1518 ibp->rvp.n_pkt_drops++;
1519 return -EINVAL;
1520}
1521
77241056
MM
1522void handle_eflags(struct hfi1_packet *packet)
1523{
1524 struct hfi1_ctxtdata *rcd = packet->rcd;
1525 u32 rte = rhf_rcv_type_err(packet->rhf);
1526
77241056 1527 rcv_hdrerr(rcd, rcd->ppd, packet);
a03a03e9
IH
1528 if (rhf_err_flags(packet->rhf))
1529 dd_dev_err(rcd->dd,
1530 "receive context %d: rhf 0x%016llx, errs [ %s%s%s%s%s%s%s%s] rte 0x%x\n",
1531 rcd->ctxt, packet->rhf,
1532 packet->rhf & RHF_K_HDR_LEN_ERR ? "k_hdr_len " : "",
1533 packet->rhf & RHF_DC_UNC_ERR ? "dc_unc " : "",
1534 packet->rhf & RHF_DC_ERR ? "dc " : "",
1535 packet->rhf & RHF_TID_ERR ? "tid " : "",
1536 packet->rhf & RHF_LEN_ERR ? "len " : "",
1537 packet->rhf & RHF_ECC_ERR ? "ecc " : "",
1538 packet->rhf & RHF_VCRC_ERR ? "vcrc " : "",
1539 packet->rhf & RHF_ICRC_ERR ? "icrc " : "",
1540 rte);
77241056
MM
1541}
1542
1543/*
1544 * The following functions are called by the interrupt handler. They are type
1545 * specific handlers for each packet type.
1546 */
1547int process_receive_ib(struct hfi1_packet *packet)
1548{
0181ce31
DH
1549 if (unlikely(hfi1_dbg_fault_packet(packet)))
1550 return RHF_RCV_CONTINUE;
1551
9039746c
DH
1552 if (hfi1_setup_9B_packet(packet))
1553 return RHF_RCV_CONTINUE;
1554
77241056
MM
1555 trace_hfi1_rcvhdr(packet->rcd->ppd->dd,
1556 packet->rcd->ctxt,
1557 rhf_err_flags(packet->rhf),
1558 RHF_RCV_TYPE_IB,
1559 packet->hlen,
1560 packet->tlen,
1561 packet->updegr,
1562 rhf_egr_index(packet->rhf));
1563
243d9f43
DH
1564 if (unlikely(
1565 (hfi1_dbg_fault_suppress_err(&packet->rcd->dd->verbs_dev) &&
1566 (packet->rhf & RHF_DC_ERR))))
1567 return RHF_RCV_CONTINUE;
1568
77241056
MM
1569 if (unlikely(rhf_err_flags(packet->rhf))) {
1570 handle_eflags(packet);
1571 return RHF_RCV_CONTINUE;
1572 }
1573
1574 hfi1_ib_rcv(packet);
1575 return RHF_RCV_CONTINUE;
1576}
1577
d4829ea6
VN
1578static inline bool hfi1_is_vnic_packet(struct hfi1_packet *packet)
1579{
1580 /* Packet received in VNIC context via RSM */
1581 if (packet->rcd->is_vnic)
1582 return true;
1583
72c07e2b
DH
1584 if ((hfi1_16B_get_l2(packet->ebuf) == OPA_16B_L2_TYPE) &&
1585 (hfi1_16B_get_l4(packet->ebuf) == OPA_16B_L4_ETHR))
d4829ea6
VN
1586 return true;
1587
1588 return false;
1589}
1590
77241056
MM
1591int process_receive_bypass(struct hfi1_packet *packet)
1592{
505efe3e
JP
1593 struct hfi1_devdata *dd = packet->rcd->dd;
1594
72c07e2b 1595 if (hfi1_is_vnic_packet(packet)) {
d4829ea6
VN
1596 hfi1_vnic_bypass_rcv(packet);
1597 return RHF_RCV_CONTINUE;
1598 }
77241056 1599
72c07e2b
DH
1600 if (hfi1_setup_bypass_packet(packet))
1601 return RHF_RCV_CONTINUE;
1602
1603 if (unlikely(rhf_err_flags(packet->rhf))) {
1604 handle_eflags(packet);
1605 return RHF_RCV_CONTINUE;
1606 }
505efe3e 1607
72c07e2b
DH
1608 if (hfi1_16B_get_l2(packet->hdr) == 0x2) {
1609 hfi1_16B_rcv(packet);
1610 } else {
1611 dd_dev_err(dd,
1612 "Bypass packets other than 16B are not supported in normal operation. Dropping\n");
1613 incr_cntr64(&dd->sw_rcv_bypass_packet_errors);
1614 if (!(dd->err_info_rcvport.status_and_code &
1615 OPA_EI_STATUS_SMASK)) {
1616 u64 *flits = packet->ebuf;
1617
1618 if (flits && !(packet->rhf & RHF_LEN_ERR)) {
1619 dd->err_info_rcvport.packet_flit1 = flits[0];
1620 dd->err_info_rcvport.packet_flit2 =
1621 packet->tlen > sizeof(flits[0]) ?
1622 flits[1] : 0;
1623 }
1624 dd->err_info_rcvport.status_and_code |=
1625 (OPA_EI_STATUS_SMASK | BAD_L2_ERR);
505efe3e 1626 }
505efe3e 1627 }
77241056
MM
1628 return RHF_RCV_CONTINUE;
1629}
1630
1631int process_receive_error(struct hfi1_packet *packet)
1632{
243d9f43
DH
1633 /* KHdrHCRCErr -- KDETH packet with a bad HCRC */
1634 if (unlikely(
1635 hfi1_dbg_fault_suppress_err(&packet->rcd->dd->verbs_dev) &&
1636 rhf_rcv_type_err(packet->rhf) == 3))
1637 return RHF_RCV_CONTINUE;
1638
9039746c 1639 hfi1_setup_ib_header(packet);
77241056
MM
1640 handle_eflags(packet);
1641
1642 if (unlikely(rhf_err_flags(packet->rhf)))
1643 dd_dev_err(packet->rcd->dd,
1644 "Unhandled error packet received. Dropping.\n");
1645
1646 return RHF_RCV_CONTINUE;
1647}
1648
1649int kdeth_process_expected(struct hfi1_packet *packet)
1650{
0181ce31
DH
1651 if (unlikely(hfi1_dbg_fault_packet(packet)))
1652 return RHF_RCV_CONTINUE;
9039746c
DH
1653
1654 hfi1_setup_ib_header(packet);
77241056
MM
1655 if (unlikely(rhf_err_flags(packet->rhf)))
1656 handle_eflags(packet);
1657
1658 dd_dev_err(packet->rcd->dd,
1659 "Unhandled expected packet received. Dropping.\n");
1660 return RHF_RCV_CONTINUE;
1661}
1662
1663int kdeth_process_eager(struct hfi1_packet *packet)
1664{
9039746c 1665 hfi1_setup_ib_header(packet);
77241056
MM
1666 if (unlikely(rhf_err_flags(packet->rhf)))
1667 handle_eflags(packet);
0181ce31
DH
1668 if (unlikely(hfi1_dbg_fault_packet(packet)))
1669 return RHF_RCV_CONTINUE;
77241056
MM
1670
1671 dd_dev_err(packet->rcd->dd,
1672 "Unhandled eager packet received. Dropping.\n");
1673 return RHF_RCV_CONTINUE;
1674}
1675
1676int process_receive_invalid(struct hfi1_packet *packet)
1677{
1678 dd_dev_err(packet->rcd->dd, "Invalid packet type %d. Dropping\n",
17fb4f29 1679 rhf_rcv_type(packet->rhf));
77241056
MM
1680 return RHF_RCV_CONTINUE;
1681}
bf808b50
KW
1682
1683void seqfile_dump_rcd(struct seq_file *s, struct hfi1_ctxtdata *rcd)
1684{
1685 struct hfi1_packet packet;
1686 struct ps_mdata mdata;
1687
1688 seq_printf(s, "Rcd %u: RcvHdr cnt %u entsize %u %s head %llu tail %llu\n",
1689 rcd->ctxt, rcd->rcvhdrq_cnt, rcd->rcvhdrqentsize,
1690 HFI1_CAP_KGET_MASK(rcd->flags, DMA_RTAIL) ?
1691 "dma_rtail" : "nodma_rtail",
1692 read_uctxt_csr(rcd->dd, rcd->ctxt, RCV_HDR_HEAD) &
1693 RCV_HDR_HEAD_HEAD_MASK,
1694 read_uctxt_csr(rcd->dd, rcd->ctxt, RCV_HDR_TAIL));
1695
1696 init_packet(rcd, &packet);
1697 init_ps_mdata(&mdata, &packet);
1698
1699 while (1) {
1700 struct hfi1_devdata *dd = rcd->dd;
1701 __le32 *rhf_addr = (__le32 *)rcd->rcvhdrq + mdata.ps_head +
1702 dd->rhf_offset;
1703 struct ib_header *hdr;
1704 u64 rhf = rhf_to_cpu(rhf_addr);
1705 u32 etype = rhf_rcv_type(rhf), qpn;
1706 u8 opcode;
1707 u32 psn;
1708 u8 lnh;
1709
1710 if (ps_done(&mdata, rhf, rcd))
1711 break;
1712
1713 if (ps_skip(&mdata, rhf, rcd))
1714 goto next;
1715
1716 if (etype > RHF_RCV_TYPE_IB)
1717 goto next;
1718
1719 packet.hdr = hfi1_get_msgheader(dd, rhf_addr);
1720 hdr = packet.hdr;
1721
1722 lnh = be16_to_cpu(hdr->lrh[0]) & 3;
1723
1724 if (lnh == HFI1_LRH_BTH)
1725 packet.ohdr = &hdr->u.oth;
1726 else if (lnh == HFI1_LRH_GRH)
1727 packet.ohdr = &hdr->u.l.oth;
1728 else
1729 goto next; /* just in case */
1730
1731 opcode = (be32_to_cpu(packet.ohdr->bth[0]) >> 24);
1732 qpn = be32_to_cpu(packet.ohdr->bth[1]) & RVT_QPN_MASK;
1733 psn = mask_psn(be32_to_cpu(packet.ohdr->bth[2]));
1734
1735 seq_printf(s, "\tEnt %u: opcode 0x%x, qpn 0x%x, psn 0x%x\n",
1736 mdata.ps_head, opcode, qpn, psn);
1737next:
1738 update_ps_mdata(&mdata, rcd);
1739 }
1740}