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