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 	/* Control context will always use the slow path interrupt handler */
1015 	needset = (rcd->ctxt == HFI1_CTRL_CTXT) ? 0 : 1;
1016 
1017 	init_packet(rcd, &packet);
1018 
1019 	if (!get_dma_rtail_setting(rcd)) {
1020 		if (last_rcv_seq(rcd, rhf_rcv_seq(packet.rhf))) {
1021 			last = RCV_PKT_DONE;
1022 			goto bail;
1023 		}
1024 		hdrqtail = 0;
1025 	} else {
1026 		hdrqtail = get_rcvhdrtail(rcd);
1027 		if (packet.rhqoff == hdrqtail) {
1028 			last = RCV_PKT_DONE;
1029 			goto bail;
1030 		}
1031 		smp_rmb();  /* prevent speculative reads of dma'ed hdrq */
1032 
1033 		/*
1034 		 * Control context can potentially receive an invalid
1035 		 * rhf. Drop such packets.
1036 		 */
1037 		if (rcd->ctxt == HFI1_CTRL_CTXT)
1038 			if (last_rcv_seq(rcd, rhf_rcv_seq(packet.rhf)))
1039 				skip_pkt = 1;
1040 	}
1041 
1042 	prescan_rxq(rcd, &packet);
1043 
1044 	while (last == RCV_PKT_OK) {
1045 		if (hfi1_need_drop(dd)) {
1046 			/* On to the next packet */
1047 			packet.rhqoff += packet.rsize;
1048 			packet.rhf_addr = (__le32 *)rcd->rcvhdrq +
1049 					  packet.rhqoff +
1050 					  rcd->rhf_offset;
1051 			packet.rhf = rhf_to_cpu(packet.rhf_addr);
1052 
1053 		} else if (skip_pkt) {
1054 			last = skip_rcv_packet(&packet, thread);
1055 			skip_pkt = 0;
1056 		} else {
1057 			if (set_armed_to_active(&packet))
1058 				goto bail;
1059 			last = process_rcv_packet(&packet, thread);
1060 		}
1061 
1062 		if (!get_dma_rtail_setting(rcd)) {
1063 			if (hfi1_seq_incr(rcd, rhf_rcv_seq(packet.rhf)))
1064 				last = RCV_PKT_DONE;
1065 		} else {
1066 			if (packet.rhqoff == hdrqtail)
1067 				last = RCV_PKT_DONE;
1068 			/*
1069 			 * Control context can potentially receive an invalid
1070 			 * rhf. Drop such packets.
1071 			 */
1072 			if (rcd->ctxt == HFI1_CTRL_CTXT) {
1073 				bool lseq;
1074 
1075 				lseq = hfi1_seq_incr(rcd,
1076 						     rhf_rcv_seq(packet.rhf));
1077 				if (!last && lseq)
1078 					skip_pkt = 1;
1079 			}
1080 		}
1081 
1082 		if (needset) {
1083 			needset = false;
1084 			set_all_fastpath(dd, rcd);
1085 		}
1086 		process_rcv_update(last, &packet);
1087 	}
1088 
1089 	process_rcv_qp_work(&packet);
1090 	hfi1_set_rcd_head(rcd, packet.rhqoff);
1091 
1092 bail:
1093 	/*
1094 	 * Always write head at end, and setup rcv interrupt, even
1095 	 * if no packets were processed.
1096 	 */
1097 	finish_packet(&packet);
1098 	return last;
1099 }
1100 
1101 /*
1102  * handle_receive_interrupt_napi_sp - receive a packet
1103  * @rcd: the context
1104  * @budget: polling budget
1105  *
1106  * Called from interrupt handler for errors or receive interrupt.
1107  * This is the slow path interrupt handler
1108  * when executing napi soft irq environment.
1109  */
handle_receive_interrupt_napi_sp(struct hfi1_ctxtdata * rcd,int budget)1110 int handle_receive_interrupt_napi_sp(struct hfi1_ctxtdata *rcd, int budget)
1111 {
1112 	struct hfi1_devdata *dd = rcd->dd;
1113 	int last = RCV_PKT_OK;
1114 	bool needset = true;
1115 	struct hfi1_packet packet;
1116 
1117 	init_packet(rcd, &packet);
1118 	if (last_rcv_seq(rcd, rhf_rcv_seq(packet.rhf)))
1119 		goto bail;
1120 
1121 	while (last != RCV_PKT_DONE && packet.numpkt < budget) {
1122 		if (hfi1_need_drop(dd)) {
1123 			/* On to the next packet */
1124 			packet.rhqoff += packet.rsize;
1125 			packet.rhf_addr = (__le32 *)rcd->rcvhdrq +
1126 					  packet.rhqoff +
1127 					  rcd->rhf_offset;
1128 			packet.rhf = rhf_to_cpu(packet.rhf_addr);
1129 
1130 		} else {
1131 			if (set_armed_to_active(&packet))
1132 				goto bail;
1133 			process_rcv_packet_napi(&packet);
1134 		}
1135 
1136 		if (hfi1_seq_incr(rcd, rhf_rcv_seq(packet.rhf)))
1137 			last = RCV_PKT_DONE;
1138 
1139 		if (needset) {
1140 			needset = false;
1141 			set_all_fastpath(dd, rcd);
1142 		}
1143 
1144 		process_rcv_update(last, &packet);
1145 	}
1146 
1147 	hfi1_set_rcd_head(rcd, packet.rhqoff);
1148 
1149 bail:
1150 	/*
1151 	 * Always write head at end, and setup rcv interrupt, even
1152 	 * if no packets were processed.
1153 	 */
1154 	finish_packet(&packet);
1155 	return packet.numpkt;
1156 }
1157 
1158 /*
1159  * We may discover in the interrupt that the hardware link state has
1160  * changed from ARMED to ACTIVE (due to the arrival of a non-SC15 packet),
1161  * and we need to update the driver's notion of the link state.  We cannot
1162  * run set_link_state from interrupt context, so we queue this function on
1163  * a workqueue.
1164  *
1165  * We delay the regular interrupt processing until after the state changes
1166  * so that the link will be in the correct state by the time any application
1167  * we wake up attempts to send a reply to any message it received.
1168  * (Subsequent receive interrupts may possibly force the wakeup before we
1169  * update the link state.)
1170  *
1171  * The rcd is freed in hfi1_free_ctxtdata after hfi1_postinit_cleanup invokes
1172  * dd->f_cleanup(dd) to disable the interrupt handler and flush workqueues,
1173  * so we're safe from use-after-free of the rcd.
1174  */
receive_interrupt_work(struct work_struct * work)1175 void receive_interrupt_work(struct work_struct *work)
1176 {
1177 	struct hfi1_pportdata *ppd = container_of(work, struct hfi1_pportdata,
1178 						  linkstate_active_work);
1179 	struct hfi1_devdata *dd = ppd->dd;
1180 	struct hfi1_ctxtdata *rcd;
1181 	u16 i;
1182 
1183 	/* Received non-SC15 packet implies neighbor_normal */
1184 	ppd->neighbor_normal = 1;
1185 	set_link_state(ppd, HLS_UP_ACTIVE);
1186 
1187 	/*
1188 	 * Interrupt all statically allocated kernel contexts that could
1189 	 * have had an interrupt during auto activation.
1190 	 */
1191 	for (i = HFI1_CTRL_CTXT; i < dd->first_dyn_alloc_ctxt; i++) {
1192 		rcd = hfi1_rcd_get_by_index(dd, i);
1193 		if (rcd)
1194 			force_recv_intr(rcd);
1195 		hfi1_rcd_put(rcd);
1196 	}
1197 }
1198 
1199 /*
1200  * Convert a given MTU size to the on-wire MAD packet enumeration.
1201  * Return -1 if the size is invalid.
1202  */
mtu_to_enum(u32 mtu,int default_if_bad)1203 int mtu_to_enum(u32 mtu, int default_if_bad)
1204 {
1205 	switch (mtu) {
1206 	case     0: return OPA_MTU_0;
1207 	case   256: return OPA_MTU_256;
1208 	case   512: return OPA_MTU_512;
1209 	case  1024: return OPA_MTU_1024;
1210 	case  2048: return OPA_MTU_2048;
1211 	case  4096: return OPA_MTU_4096;
1212 	case  8192: return OPA_MTU_8192;
1213 	case 10240: return OPA_MTU_10240;
1214 	}
1215 	return default_if_bad;
1216 }
1217 
enum_to_mtu(int mtu)1218 u16 enum_to_mtu(int mtu)
1219 {
1220 	switch (mtu) {
1221 	case OPA_MTU_0:     return 0;
1222 	case OPA_MTU_256:   return 256;
1223 	case OPA_MTU_512:   return 512;
1224 	case OPA_MTU_1024:  return 1024;
1225 	case OPA_MTU_2048:  return 2048;
1226 	case OPA_MTU_4096:  return 4096;
1227 	case OPA_MTU_8192:  return 8192;
1228 	case OPA_MTU_10240: return 10240;
1229 	default: return 0xffff;
1230 	}
1231 }
1232 
1233 /*
1234  * set_mtu - set the MTU
1235  * @ppd: the per port data
1236  *
1237  * We can handle "any" incoming size, the issue here is whether we
1238  * need to restrict our outgoing size.  We do not deal with what happens
1239  * to programs that are already running when the size changes.
1240  */
set_mtu(struct hfi1_pportdata * ppd)1241 int set_mtu(struct hfi1_pportdata *ppd)
1242 {
1243 	struct hfi1_devdata *dd = ppd->dd;
1244 	int i, drain, ret = 0, is_up = 0;
1245 
1246 	ppd->ibmtu = 0;
1247 	for (i = 0; i < ppd->vls_supported; i++)
1248 		if (ppd->ibmtu < dd->vld[i].mtu)
1249 			ppd->ibmtu = dd->vld[i].mtu;
1250 	ppd->ibmaxlen = ppd->ibmtu + lrh_max_header_bytes(ppd->dd);
1251 
1252 	mutex_lock(&ppd->hls_lock);
1253 	if (ppd->host_link_state == HLS_UP_INIT ||
1254 	    ppd->host_link_state == HLS_UP_ARMED ||
1255 	    ppd->host_link_state == HLS_UP_ACTIVE)
1256 		is_up = 1;
1257 
1258 	drain = !is_ax(dd) && is_up;
1259 
1260 	if (drain)
1261 		/*
1262 		 * MTU is specified per-VL. To ensure that no packet gets
1263 		 * stuck (due, e.g., to the MTU for the packet's VL being
1264 		 * reduced), empty the per-VL FIFOs before adjusting MTU.
1265 		 */
1266 		ret = stop_drain_data_vls(dd);
1267 
1268 	if (ret) {
1269 		dd_dev_err(dd, "%s: cannot stop/drain VLs - refusing to change per-VL MTUs\n",
1270 			   __func__);
1271 		goto err;
1272 	}
1273 
1274 	hfi1_set_ib_cfg(ppd, HFI1_IB_CFG_MTU, 0);
1275 
1276 	if (drain)
1277 		open_fill_data_vls(dd); /* reopen all VLs */
1278 
1279 err:
1280 	mutex_unlock(&ppd->hls_lock);
1281 
1282 	return ret;
1283 }
1284 
hfi1_set_lid(struct hfi1_pportdata * ppd,u32 lid,u8 lmc)1285 int hfi1_set_lid(struct hfi1_pportdata *ppd, u32 lid, u8 lmc)
1286 {
1287 	struct hfi1_devdata *dd = ppd->dd;
1288 
1289 	ppd->lid = lid;
1290 	ppd->lmc = lmc;
1291 	hfi1_set_ib_cfg(ppd, HFI1_IB_CFG_LIDLMC, 0);
1292 
1293 	dd_dev_info(dd, "port %u: got a lid: 0x%x\n", ppd->port, lid);
1294 
1295 	return 0;
1296 }
1297 
shutdown_led_override(struct hfi1_pportdata * ppd)1298 void shutdown_led_override(struct hfi1_pportdata *ppd)
1299 {
1300 	struct hfi1_devdata *dd = ppd->dd;
1301 
1302 	/*
1303 	 * This pairs with the memory barrier in hfi1_start_led_override to
1304 	 * ensure that we read the correct state of LED beaconing represented
1305 	 * by led_override_timer_active
1306 	 */
1307 	smp_rmb();
1308 	if (atomic_read(&ppd->led_override_timer_active)) {
1309 		del_timer_sync(&ppd->led_override_timer);
1310 		atomic_set(&ppd->led_override_timer_active, 0);
1311 		/* Ensure the atomic_set is visible to all CPUs */
1312 		smp_wmb();
1313 	}
1314 
1315 	/* Hand control of the LED to the DC for normal operation */
1316 	write_csr(dd, DCC_CFG_LED_CNTRL, 0);
1317 }
1318 
run_led_override(struct timer_list * t)1319 static void run_led_override(struct timer_list *t)
1320 {
1321 	struct hfi1_pportdata *ppd = from_timer(ppd, t, led_override_timer);
1322 	struct hfi1_devdata *dd = ppd->dd;
1323 	unsigned long timeout;
1324 	int phase_idx;
1325 
1326 	if (!(dd->flags & HFI1_INITTED))
1327 		return;
1328 
1329 	phase_idx = ppd->led_override_phase & 1;
1330 
1331 	setextled(dd, phase_idx);
1332 
1333 	timeout = ppd->led_override_vals[phase_idx];
1334 
1335 	/* Set up for next phase */
1336 	ppd->led_override_phase = !ppd->led_override_phase;
1337 
1338 	mod_timer(&ppd->led_override_timer, jiffies + timeout);
1339 }
1340 
1341 /*
1342  * To have the LED blink in a particular pattern, provide timeon and timeoff
1343  * in milliseconds.
1344  * To turn off custom blinking and return to normal operation, use
1345  * shutdown_led_override()
1346  */
hfi1_start_led_override(struct hfi1_pportdata * ppd,unsigned int timeon,unsigned int timeoff)1347 void hfi1_start_led_override(struct hfi1_pportdata *ppd, unsigned int timeon,
1348 			     unsigned int timeoff)
1349 {
1350 	if (!(ppd->dd->flags & HFI1_INITTED))
1351 		return;
1352 
1353 	/* Convert to jiffies for direct use in timer */
1354 	ppd->led_override_vals[0] = msecs_to_jiffies(timeoff);
1355 	ppd->led_override_vals[1] = msecs_to_jiffies(timeon);
1356 
1357 	/* Arbitrarily start from LED on phase */
1358 	ppd->led_override_phase = 1;
1359 
1360 	/*
1361 	 * If the timer has not already been started, do so. Use a "quick"
1362 	 * timeout so the handler will be called soon to look at our request.
1363 	 */
1364 	if (!timer_pending(&ppd->led_override_timer)) {
1365 		timer_setup(&ppd->led_override_timer, run_led_override, 0);
1366 		ppd->led_override_timer.expires = jiffies + 1;
1367 		add_timer(&ppd->led_override_timer);
1368 		atomic_set(&ppd->led_override_timer_active, 1);
1369 		/* Ensure the atomic_set is visible to all CPUs */
1370 		smp_wmb();
1371 	}
1372 }
1373 
1374 /**
1375  * hfi1_reset_device - reset the chip if possible
1376  * @unit: the device to reset
1377  *
1378  * Whether or not reset is successful, we attempt to re-initialize the chip
1379  * (that is, much like a driver unload/reload).  We clear the INITTED flag
1380  * so that the various entry points will fail until we reinitialize.  For
1381  * now, we only allow this if no user contexts are open that use chip resources
1382  */
hfi1_reset_device(int unit)1383 int hfi1_reset_device(int unit)
1384 {
1385 	int ret;
1386 	struct hfi1_devdata *dd = hfi1_lookup(unit);
1387 	struct hfi1_pportdata *ppd;
1388 	int pidx;
1389 
1390 	if (!dd) {
1391 		ret = -ENODEV;
1392 		goto bail;
1393 	}
1394 
1395 	dd_dev_info(dd, "Reset on unit %u requested\n", unit);
1396 
1397 	if (!dd->kregbase1 || !(dd->flags & HFI1_PRESENT)) {
1398 		dd_dev_info(dd,
1399 			    "Invalid unit number %u or not initialized or not present\n",
1400 			    unit);
1401 		ret = -ENXIO;
1402 		goto bail;
1403 	}
1404 
1405 	/* If there are any user/vnic contexts, we cannot reset */
1406 	mutex_lock(&hfi1_mutex);
1407 	if (dd->rcd)
1408 		if (hfi1_stats.sps_ctxts) {
1409 			mutex_unlock(&hfi1_mutex);
1410 			ret = -EBUSY;
1411 			goto bail;
1412 		}
1413 	mutex_unlock(&hfi1_mutex);
1414 
1415 	for (pidx = 0; pidx < dd->num_pports; ++pidx) {
1416 		ppd = dd->pport + pidx;
1417 
1418 		shutdown_led_override(ppd);
1419 	}
1420 	if (dd->flags & HFI1_HAS_SEND_DMA)
1421 		sdma_exit(dd);
1422 
1423 	hfi1_reset_cpu_counters(dd);
1424 
1425 	ret = hfi1_init(dd, 1);
1426 
1427 	if (ret)
1428 		dd_dev_err(dd,
1429 			   "Reinitialize unit %u after reset failed with %d\n",
1430 			   unit, ret);
1431 	else
1432 		dd_dev_info(dd, "Reinitialized unit %u after resetting\n",
1433 			    unit);
1434 
1435 bail:
1436 	return ret;
1437 }
1438 
hfi1_setup_ib_header(struct hfi1_packet * packet)1439 static inline void hfi1_setup_ib_header(struct hfi1_packet *packet)
1440 {
1441 	packet->hdr = (struct hfi1_ib_message_header *)
1442 			hfi1_get_msgheader(packet->rcd,
1443 					   packet->rhf_addr);
1444 	packet->hlen = (u8 *)packet->rhf_addr - (u8 *)packet->hdr;
1445 }
1446 
hfi1_bypass_ingress_pkt_check(struct hfi1_packet * packet)1447 static int hfi1_bypass_ingress_pkt_check(struct hfi1_packet *packet)
1448 {
1449 	struct hfi1_pportdata *ppd = packet->rcd->ppd;
1450 
1451 	/* slid and dlid cannot be 0 */
1452 	if ((!packet->slid) || (!packet->dlid))
1453 		return -EINVAL;
1454 
1455 	/* Compare port lid with incoming packet dlid */
1456 	if ((!(hfi1_is_16B_mcast(packet->dlid))) &&
1457 	    (packet->dlid !=
1458 		opa_get_lid(be32_to_cpu(OPA_LID_PERMISSIVE), 16B))) {
1459 		if ((packet->dlid & ~((1 << ppd->lmc) - 1)) != ppd->lid)
1460 			return -EINVAL;
1461 	}
1462 
1463 	/* No multicast packets with SC15 */
1464 	if ((hfi1_is_16B_mcast(packet->dlid)) && (packet->sc == 0xF))
1465 		return -EINVAL;
1466 
1467 	/* Packets with permissive DLID always on SC15 */
1468 	if ((packet->dlid == opa_get_lid(be32_to_cpu(OPA_LID_PERMISSIVE),
1469 					 16B)) &&
1470 	    (packet->sc != 0xF))
1471 		return -EINVAL;
1472 
1473 	return 0;
1474 }
1475 
hfi1_setup_9B_packet(struct hfi1_packet * packet)1476 static int hfi1_setup_9B_packet(struct hfi1_packet *packet)
1477 {
1478 	struct hfi1_ibport *ibp = rcd_to_iport(packet->rcd);
1479 	struct ib_header *hdr;
1480 	u8 lnh;
1481 
1482 	hfi1_setup_ib_header(packet);
1483 	hdr = packet->hdr;
1484 
1485 	lnh = ib_get_lnh(hdr);
1486 	if (lnh == HFI1_LRH_BTH) {
1487 		packet->ohdr = &hdr->u.oth;
1488 		packet->grh = NULL;
1489 	} else if (lnh == HFI1_LRH_GRH) {
1490 		u32 vtf;
1491 
1492 		packet->ohdr = &hdr->u.l.oth;
1493 		packet->grh = &hdr->u.l.grh;
1494 		if (packet->grh->next_hdr != IB_GRH_NEXT_HDR)
1495 			goto drop;
1496 		vtf = be32_to_cpu(packet->grh->version_tclass_flow);
1497 		if ((vtf >> IB_GRH_VERSION_SHIFT) != IB_GRH_VERSION)
1498 			goto drop;
1499 	} else {
1500 		goto drop;
1501 	}
1502 
1503 	/* Query commonly used fields from packet header */
1504 	packet->payload = packet->ebuf;
1505 	packet->opcode = ib_bth_get_opcode(packet->ohdr);
1506 	packet->slid = ib_get_slid(hdr);
1507 	packet->dlid = ib_get_dlid(hdr);
1508 	if (unlikely((packet->dlid >= be16_to_cpu(IB_MULTICAST_LID_BASE)) &&
1509 		     (packet->dlid != be16_to_cpu(IB_LID_PERMISSIVE))))
1510 		packet->dlid += opa_get_mcast_base(OPA_MCAST_NR) -
1511 				be16_to_cpu(IB_MULTICAST_LID_BASE);
1512 	packet->sl = ib_get_sl(hdr);
1513 	packet->sc = hfi1_9B_get_sc5(hdr, packet->rhf);
1514 	packet->pad = ib_bth_get_pad(packet->ohdr);
1515 	packet->extra_byte = 0;
1516 	packet->pkey = ib_bth_get_pkey(packet->ohdr);
1517 	packet->migrated = ib_bth_is_migration(packet->ohdr);
1518 
1519 	return 0;
1520 drop:
1521 	ibp->rvp.n_pkt_drops++;
1522 	return -EINVAL;
1523 }
1524 
hfi1_setup_bypass_packet(struct hfi1_packet * packet)1525 static int hfi1_setup_bypass_packet(struct hfi1_packet *packet)
1526 {
1527 	/*
1528 	 * Bypass packets have a different header/payload split
1529 	 * compared to an IB packet.
1530 	 * Current split is set such that 16 bytes of the actual
1531 	 * header is in the header buffer and the remining is in
1532 	 * the eager buffer. We chose 16 since hfi1 driver only
1533 	 * supports 16B bypass packets and we will be able to
1534 	 * receive the entire LRH with such a split.
1535 	 */
1536 
1537 	struct hfi1_ctxtdata *rcd = packet->rcd;
1538 	struct hfi1_pportdata *ppd = rcd->ppd;
1539 	struct hfi1_ibport *ibp = &ppd->ibport_data;
1540 	u8 l4;
1541 
1542 	packet->hdr = (struct hfi1_16b_header *)
1543 			hfi1_get_16B_header(packet->rcd,
1544 					    packet->rhf_addr);
1545 	l4 = hfi1_16B_get_l4(packet->hdr);
1546 	if (l4 == OPA_16B_L4_IB_LOCAL) {
1547 		packet->ohdr = packet->ebuf;
1548 		packet->grh = NULL;
1549 		packet->opcode = ib_bth_get_opcode(packet->ohdr);
1550 		packet->pad = hfi1_16B_bth_get_pad(packet->ohdr);
1551 		/* hdr_len_by_opcode already has an IB LRH factored in */
1552 		packet->hlen = hdr_len_by_opcode[packet->opcode] +
1553 			(LRH_16B_BYTES - LRH_9B_BYTES);
1554 		packet->migrated = opa_bth_is_migration(packet->ohdr);
1555 	} else if (l4 == OPA_16B_L4_IB_GLOBAL) {
1556 		u32 vtf;
1557 		u8 grh_len = sizeof(struct ib_grh);
1558 
1559 		packet->ohdr = packet->ebuf + grh_len;
1560 		packet->grh = packet->ebuf;
1561 		packet->opcode = ib_bth_get_opcode(packet->ohdr);
1562 		packet->pad = hfi1_16B_bth_get_pad(packet->ohdr);
1563 		/* hdr_len_by_opcode already has an IB LRH factored in */
1564 		packet->hlen = hdr_len_by_opcode[packet->opcode] +
1565 			(LRH_16B_BYTES - LRH_9B_BYTES) + grh_len;
1566 		packet->migrated = opa_bth_is_migration(packet->ohdr);
1567 
1568 		if (packet->grh->next_hdr != IB_GRH_NEXT_HDR)
1569 			goto drop;
1570 		vtf = be32_to_cpu(packet->grh->version_tclass_flow);
1571 		if ((vtf >> IB_GRH_VERSION_SHIFT) != IB_GRH_VERSION)
1572 			goto drop;
1573 	} else if (l4 == OPA_16B_L4_FM) {
1574 		packet->mgmt = packet->ebuf;
1575 		packet->ohdr = NULL;
1576 		packet->grh = NULL;
1577 		packet->opcode = IB_OPCODE_UD_SEND_ONLY;
1578 		packet->pad = OPA_16B_L4_FM_PAD;
1579 		packet->hlen = OPA_16B_L4_FM_HLEN;
1580 		packet->migrated = false;
1581 	} else {
1582 		goto drop;
1583 	}
1584 
1585 	/* Query commonly used fields from packet header */
1586 	packet->payload = packet->ebuf + packet->hlen - LRH_16B_BYTES;
1587 	packet->slid = hfi1_16B_get_slid(packet->hdr);
1588 	packet->dlid = hfi1_16B_get_dlid(packet->hdr);
1589 	if (unlikely(hfi1_is_16B_mcast(packet->dlid)))
1590 		packet->dlid += opa_get_mcast_base(OPA_MCAST_NR) -
1591 				opa_get_lid(opa_get_mcast_base(OPA_MCAST_NR),
1592 					    16B);
1593 	packet->sc = hfi1_16B_get_sc(packet->hdr);
1594 	packet->sl = ibp->sc_to_sl[packet->sc];
1595 	packet->extra_byte = SIZE_OF_LT;
1596 	packet->pkey = hfi1_16B_get_pkey(packet->hdr);
1597 
1598 	if (hfi1_bypass_ingress_pkt_check(packet))
1599 		goto drop;
1600 
1601 	return 0;
1602 drop:
1603 	hfi1_cdbg(PKT, "%s: packet dropped\n", __func__);
1604 	ibp->rvp.n_pkt_drops++;
1605 	return -EINVAL;
1606 }
1607 
show_eflags_errs(struct hfi1_packet * packet)1608 static void show_eflags_errs(struct hfi1_packet *packet)
1609 {
1610 	struct hfi1_ctxtdata *rcd = packet->rcd;
1611 	u32 rte = rhf_rcv_type_err(packet->rhf);
1612 
1613 	dd_dev_err(rcd->dd,
1614 		   "receive context %d: rhf 0x%016llx, errs [ %s%s%s%s%s%s%s] rte 0x%x\n",
1615 		   rcd->ctxt, packet->rhf,
1616 		   packet->rhf & RHF_K_HDR_LEN_ERR ? "k_hdr_len " : "",
1617 		   packet->rhf & RHF_DC_UNC_ERR ? "dc_unc " : "",
1618 		   packet->rhf & RHF_DC_ERR ? "dc " : "",
1619 		   packet->rhf & RHF_TID_ERR ? "tid " : "",
1620 		   packet->rhf & RHF_LEN_ERR ? "len " : "",
1621 		   packet->rhf & RHF_ECC_ERR ? "ecc " : "",
1622 		   packet->rhf & RHF_ICRC_ERR ? "icrc " : "",
1623 		   rte);
1624 }
1625 
handle_eflags(struct hfi1_packet * packet)1626 void handle_eflags(struct hfi1_packet *packet)
1627 {
1628 	struct hfi1_ctxtdata *rcd = packet->rcd;
1629 
1630 	rcv_hdrerr(rcd, rcd->ppd, packet);
1631 	if (rhf_err_flags(packet->rhf))
1632 		show_eflags_errs(packet);
1633 }
1634 
hfi1_ipoib_ib_rcv(struct hfi1_packet * packet)1635 static void hfi1_ipoib_ib_rcv(struct hfi1_packet *packet)
1636 {
1637 	struct hfi1_ibport *ibp;
1638 	struct net_device *netdev;
1639 	struct hfi1_ctxtdata *rcd = packet->rcd;
1640 	struct napi_struct *napi = rcd->napi;
1641 	struct sk_buff *skb;
1642 	struct hfi1_netdev_rxq *rxq = container_of(napi,
1643 			struct hfi1_netdev_rxq, napi);
1644 	u32 extra_bytes;
1645 	u32 tlen, qpnum;
1646 	bool do_work, do_cnp;
1647 
1648 	trace_hfi1_rcvhdr(packet);
1649 
1650 	hfi1_setup_ib_header(packet);
1651 
1652 	packet->ohdr = &((struct ib_header *)packet->hdr)->u.oth;
1653 	packet->grh = NULL;
1654 
1655 	if (unlikely(rhf_err_flags(packet->rhf))) {
1656 		handle_eflags(packet);
1657 		return;
1658 	}
1659 
1660 	qpnum = ib_bth_get_qpn(packet->ohdr);
1661 	netdev = hfi1_netdev_get_data(rcd->dd, qpnum);
1662 	if (!netdev)
1663 		goto drop_no_nd;
1664 
1665 	trace_input_ibhdr(rcd->dd, packet, !!(rhf_dc_info(packet->rhf)));
1666 	trace_ctxt_rsm_hist(rcd->ctxt);
1667 
1668 	/* handle congestion notifications */
1669 	do_work = hfi1_may_ecn(packet);
1670 	if (unlikely(do_work)) {
1671 		do_cnp = (packet->opcode != IB_OPCODE_CNP);
1672 		(void)hfi1_process_ecn_slowpath(hfi1_ipoib_priv(netdev)->qp,
1673 						 packet, do_cnp);
1674 	}
1675 
1676 	/*
1677 	 * We have split point after last byte of DETH
1678 	 * lets strip padding and CRC and ICRC.
1679 	 * tlen is whole packet len so we need to
1680 	 * subtract header size as well.
1681 	 */
1682 	tlen = packet->tlen;
1683 	extra_bytes = ib_bth_get_pad(packet->ohdr) + (SIZE_OF_CRC << 2) +
1684 			packet->hlen;
1685 	if (unlikely(tlen < extra_bytes))
1686 		goto drop;
1687 
1688 	tlen -= extra_bytes;
1689 
1690 	skb = hfi1_ipoib_prepare_skb(rxq, tlen, packet->ebuf);
1691 	if (unlikely(!skb))
1692 		goto drop;
1693 
1694 	dev_sw_netstats_rx_add(netdev, skb->len);
1695 
1696 	skb->dev = netdev;
1697 	skb->pkt_type = PACKET_HOST;
1698 	netif_receive_skb(skb);
1699 
1700 	return;
1701 
1702 drop:
1703 	++netdev->stats.rx_dropped;
1704 drop_no_nd:
1705 	ibp = rcd_to_iport(packet->rcd);
1706 	++ibp->rvp.n_pkt_drops;
1707 }
1708 
1709 /*
1710  * The following functions are called by the interrupt handler. They are type
1711  * specific handlers for each packet type.
1712  */
process_receive_ib(struct hfi1_packet * packet)1713 static void process_receive_ib(struct hfi1_packet *packet)
1714 {
1715 	if (hfi1_setup_9B_packet(packet))
1716 		return;
1717 
1718 	if (unlikely(hfi1_dbg_should_fault_rx(packet)))
1719 		return;
1720 
1721 	trace_hfi1_rcvhdr(packet);
1722 
1723 	if (unlikely(rhf_err_flags(packet->rhf))) {
1724 		handle_eflags(packet);
1725 		return;
1726 	}
1727 
1728 	hfi1_ib_rcv(packet);
1729 }
1730 
process_receive_bypass(struct hfi1_packet * packet)1731 static void process_receive_bypass(struct hfi1_packet *packet)
1732 {
1733 	struct hfi1_devdata *dd = packet->rcd->dd;
1734 
1735 	if (hfi1_setup_bypass_packet(packet))
1736 		return;
1737 
1738 	trace_hfi1_rcvhdr(packet);
1739 
1740 	if (unlikely(rhf_err_flags(packet->rhf))) {
1741 		handle_eflags(packet);
1742 		return;
1743 	}
1744 
1745 	if (hfi1_16B_get_l2(packet->hdr) == 0x2) {
1746 		hfi1_16B_rcv(packet);
1747 	} else {
1748 		dd_dev_err(dd,
1749 			   "Bypass packets other than 16B are not supported in normal operation. Dropping\n");
1750 		incr_cntr64(&dd->sw_rcv_bypass_packet_errors);
1751 		if (!(dd->err_info_rcvport.status_and_code &
1752 		      OPA_EI_STATUS_SMASK)) {
1753 			u64 *flits = packet->ebuf;
1754 
1755 			if (flits && !(packet->rhf & RHF_LEN_ERR)) {
1756 				dd->err_info_rcvport.packet_flit1 = flits[0];
1757 				dd->err_info_rcvport.packet_flit2 =
1758 					packet->tlen > sizeof(flits[0]) ?
1759 					flits[1] : 0;
1760 			}
1761 			dd->err_info_rcvport.status_and_code |=
1762 				(OPA_EI_STATUS_SMASK | BAD_L2_ERR);
1763 		}
1764 	}
1765 }
1766 
process_receive_error(struct hfi1_packet * packet)1767 static void process_receive_error(struct hfi1_packet *packet)
1768 {
1769 	/* KHdrHCRCErr -- KDETH packet with a bad HCRC */
1770 	if (unlikely(
1771 		 hfi1_dbg_fault_suppress_err(&packet->rcd->dd->verbs_dev) &&
1772 		 (rhf_rcv_type_err(packet->rhf) == RHF_RCV_TYPE_ERROR ||
1773 		  packet->rhf & RHF_DC_ERR)))
1774 		return;
1775 
1776 	hfi1_setup_ib_header(packet);
1777 	handle_eflags(packet);
1778 
1779 	if (unlikely(rhf_err_flags(packet->rhf)))
1780 		dd_dev_err(packet->rcd->dd,
1781 			   "Unhandled error packet received. Dropping.\n");
1782 }
1783 
kdeth_process_expected(struct hfi1_packet * packet)1784 static void kdeth_process_expected(struct hfi1_packet *packet)
1785 {
1786 	hfi1_setup_9B_packet(packet);
1787 	if (unlikely(hfi1_dbg_should_fault_rx(packet)))
1788 		return;
1789 
1790 	if (unlikely(rhf_err_flags(packet->rhf))) {
1791 		struct hfi1_ctxtdata *rcd = packet->rcd;
1792 
1793 		if (hfi1_handle_kdeth_eflags(rcd, rcd->ppd, packet))
1794 			return;
1795 	}
1796 
1797 	hfi1_kdeth_expected_rcv(packet);
1798 }
1799 
kdeth_process_eager(struct hfi1_packet * packet)1800 static void kdeth_process_eager(struct hfi1_packet *packet)
1801 {
1802 	hfi1_setup_9B_packet(packet);
1803 	if (unlikely(hfi1_dbg_should_fault_rx(packet)))
1804 		return;
1805 
1806 	trace_hfi1_rcvhdr(packet);
1807 	if (unlikely(rhf_err_flags(packet->rhf))) {
1808 		struct hfi1_ctxtdata *rcd = packet->rcd;
1809 
1810 		show_eflags_errs(packet);
1811 		if (hfi1_handle_kdeth_eflags(rcd, rcd->ppd, packet))
1812 			return;
1813 	}
1814 
1815 	hfi1_kdeth_eager_rcv(packet);
1816 }
1817 
process_receive_invalid(struct hfi1_packet * packet)1818 static void process_receive_invalid(struct hfi1_packet *packet)
1819 {
1820 	dd_dev_err(packet->rcd->dd, "Invalid packet type %d. Dropping\n",
1821 		   rhf_rcv_type(packet->rhf));
1822 }
1823 
1824 #define HFI1_RCVHDR_DUMP_MAX	5
1825 
seqfile_dump_rcd(struct seq_file * s,struct hfi1_ctxtdata * rcd)1826 void seqfile_dump_rcd(struct seq_file *s, struct hfi1_ctxtdata *rcd)
1827 {
1828 	struct hfi1_packet packet;
1829 	struct ps_mdata mdata;
1830 	int i;
1831 
1832 	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",
1833 		   rcd->ctxt, get_hdrq_cnt(rcd), get_hdrqentsize(rcd),
1834 		   get_dma_rtail_setting(rcd) ?
1835 		   "dma_rtail" : "nodma_rtail",
1836 		   read_kctxt_csr(rcd->dd, rcd->ctxt, RCV_CTXT_CTRL),
1837 		   read_kctxt_csr(rcd->dd, rcd->ctxt, RCV_CTXT_STATUS),
1838 		   read_uctxt_csr(rcd->dd, rcd->ctxt, RCV_HDR_HEAD) &
1839 		   RCV_HDR_HEAD_HEAD_MASK,
1840 		   read_uctxt_csr(rcd->dd, rcd->ctxt, RCV_HDR_TAIL),
1841 		   rcd->head);
1842 
1843 	init_packet(rcd, &packet);
1844 	init_ps_mdata(&mdata, &packet);
1845 
1846 	for (i = 0; i < HFI1_RCVHDR_DUMP_MAX; i++) {
1847 		__le32 *rhf_addr = (__le32 *)rcd->rcvhdrq + mdata.ps_head +
1848 					 rcd->rhf_offset;
1849 		struct ib_header *hdr;
1850 		u64 rhf = rhf_to_cpu(rhf_addr);
1851 		u32 etype = rhf_rcv_type(rhf), qpn;
1852 		u8 opcode;
1853 		u32 psn;
1854 		u8 lnh;
1855 
1856 		if (ps_done(&mdata, rhf, rcd))
1857 			break;
1858 
1859 		if (ps_skip(&mdata, rhf, rcd))
1860 			goto next;
1861 
1862 		if (etype > RHF_RCV_TYPE_IB)
1863 			goto next;
1864 
1865 		packet.hdr = hfi1_get_msgheader(rcd, rhf_addr);
1866 		hdr = packet.hdr;
1867 
1868 		lnh = be16_to_cpu(hdr->lrh[0]) & 3;
1869 
1870 		if (lnh == HFI1_LRH_BTH)
1871 			packet.ohdr = &hdr->u.oth;
1872 		else if (lnh == HFI1_LRH_GRH)
1873 			packet.ohdr = &hdr->u.l.oth;
1874 		else
1875 			goto next; /* just in case */
1876 
1877 		opcode = (be32_to_cpu(packet.ohdr->bth[0]) >> 24);
1878 		qpn = be32_to_cpu(packet.ohdr->bth[1]) & RVT_QPN_MASK;
1879 		psn = mask_psn(be32_to_cpu(packet.ohdr->bth[2]));
1880 
1881 		seq_printf(s, "\tEnt %u: opcode 0x%x, qpn 0x%x, psn 0x%x\n",
1882 			   mdata.ps_head, opcode, qpn, psn);
1883 next:
1884 		update_ps_mdata(&mdata, rcd);
1885 	}
1886 }
1887 
1888 const rhf_rcv_function_ptr normal_rhf_rcv_functions[] = {
1889 	[RHF_RCV_TYPE_EXPECTED] = kdeth_process_expected,
1890 	[RHF_RCV_TYPE_EAGER] = kdeth_process_eager,
1891 	[RHF_RCV_TYPE_IB] = process_receive_ib,
1892 	[RHF_RCV_TYPE_ERROR] = process_receive_error,
1893 	[RHF_RCV_TYPE_BYPASS] = process_receive_bypass,
1894 	[RHF_RCV_TYPE_INVALID5] = process_receive_invalid,
1895 	[RHF_RCV_TYPE_INVALID6] = process_receive_invalid,
1896 	[RHF_RCV_TYPE_INVALID7] = process_receive_invalid,
1897 };
1898 
1899 const rhf_rcv_function_ptr netdev_rhf_rcv_functions[] = {
1900 	[RHF_RCV_TYPE_EXPECTED] = process_receive_invalid,
1901 	[RHF_RCV_TYPE_EAGER] = process_receive_invalid,
1902 	[RHF_RCV_TYPE_IB] = hfi1_ipoib_ib_rcv,
1903 	[RHF_RCV_TYPE_ERROR] = process_receive_error,
1904 	[RHF_RCV_TYPE_BYPASS] = hfi1_vnic_bypass_rcv,
1905 	[RHF_RCV_TYPE_INVALID5] = process_receive_invalid,
1906 	[RHF_RCV_TYPE_INVALID6] = process_receive_invalid,
1907 	[RHF_RCV_TYPE_INVALID7] = process_receive_invalid,
1908 };
1909