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