blob: 0583479f2576ae67c57c651088a1a0fc720d27fb [file] [log] [blame]
Mike Marciniszyn77241052015-07-30 15:17:43 -04001/*
Vishwanathapura, Niranjanad4829ea2017-04-12 20:29:28 -07002 * Copyright(c) 2015-2017 Intel Corporation.
Mike Marciniszyn77241052015-07-30 15:17:43 -04003 *
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 *
Mike Marciniszyn77241052015-07-30 15:17:43 -04009 * 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 *
Mike Marciniszyn77241052015-07-30 15:17:43 -040020 * 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>
Dennis Dalessandro8859b4a2016-01-19 14:42:11 -080056#include <rdma/ib_verbs.h>
Mike Marciniszyn77241052015-07-30 15:17:43 -040057
58#include "hfi.h"
59#include "trace.h"
60#include "qp.h"
61#include "sdma.h"
Don Hiatt0181ce32017-03-20 17:26:14 -070062#include "debugfs.h"
Vishwanathapura, Niranjanad4829ea2017-04-12 20:29:28 -070063#include "vnic.h"
Mike Marciniszyn77241052015-07-30 15:17:43 -040064
65#undef pr_fmt
66#define pr_fmt(fmt) DRIVER_NAME ": " fmt
67
68/*
69 * The size has to be longer than this string, so we can append
70 * board/chip information to it in the initialization code.
71 */
72const char ib_hfi1_version[] = HFI1_DRIVER_VERSION "\n";
73
74DEFINE_SPINLOCK(hfi1_devs_lock);
75LIST_HEAD(hfi1_dev_list);
76DEFINE_MUTEX(hfi1_mutex); /* general driver use */
77
78unsigned int hfi1_max_mtu = HFI1_DEFAULT_MAX_MTU;
79module_param_named(max_mtu, hfi1_max_mtu, uint, S_IRUGO);
Sebastian Sanchezef699e82016-04-12 11:17:09 -070080MODULE_PARM_DESC(max_mtu, "Set max MTU bytes, default is " __stringify(
81 HFI1_DEFAULT_MAX_MTU));
Mike Marciniszyn77241052015-07-30 15:17:43 -040082
83unsigned int hfi1_cu = 1;
84module_param_named(cu, hfi1_cu, uint, S_IRUGO);
85MODULE_PARM_DESC(cu, "Credit return units");
86
87unsigned long hfi1_cap_mask = HFI1_CAP_MASK_DEFAULT;
Michael J. Ruhlf4cd8762017-05-04 05:14:39 -070088static int hfi1_caps_set(const char *val, const struct kernel_param *kp);
89static int hfi1_caps_get(char *buffer, const struct kernel_param *kp);
Mike Marciniszyn77241052015-07-30 15:17:43 -040090static const struct kernel_param_ops cap_ops = {
91 .set = hfi1_caps_set,
92 .get = hfi1_caps_get
93};
94module_param_cb(cap_mask, &cap_ops, &hfi1_cap_mask, S_IWUSR | S_IRUGO);
95MODULE_PARM_DESC(cap_mask, "Bit mask of enabled/disabled HW features");
96
97MODULE_LICENSE("Dual BSD/GPL");
98MODULE_DESCRIPTION("Intel Omni-Path Architecture driver");
99MODULE_VERSION(HFI1_DRIVER_VERSION);
100
101/*
102 * MAX_PKT_RCV is the max # if packets processed per receive interrupt.
103 */
104#define MAX_PKT_RECV 64
Mike Marciniszyna82a7fc2017-02-08 05:26:02 -0800105/*
106 * MAX_PKT_THREAD_RCV is the max # of packets processed before
107 * the qp_wait_list queue is flushed.
108 */
109#define MAX_PKT_RECV_THREAD (MAX_PKT_RECV * 4)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400110#define EGR_HEAD_UPDATE_THRESHOLD 16
111
112struct hfi1_ib_stats hfi1_stats;
113
114static int hfi1_caps_set(const char *val, const struct kernel_param *kp)
115{
116 int ret = 0;
117 unsigned long *cap_mask_ptr = (unsigned long *)kp->arg,
118 cap_mask = *cap_mask_ptr, value, diff,
119 write_mask = ((HFI1_CAP_WRITABLE_MASK << HFI1_CAP_USER_SHIFT) |
120 HFI1_CAP_WRITABLE_MASK);
121
122 ret = kstrtoul(val, 0, &value);
123 if (ret) {
124 pr_warn("Invalid module parameter value for 'cap_mask'\n");
125 goto done;
126 }
127 /* Get the changed bits (except the locked bit) */
128 diff = value ^ (cap_mask & ~HFI1_CAP_LOCKED_SMASK);
129
130 /* Remove any bits that are not allowed to change after driver load */
131 if (HFI1_CAP_LOCKED() && (diff & ~write_mask)) {
132 pr_warn("Ignoring non-writable capability bits %#lx\n",
133 diff & ~write_mask);
134 diff &= write_mask;
135 }
136
137 /* Mask off any reserved bits */
138 diff &= ~HFI1_CAP_RESERVED_MASK;
139 /* Clear any previously set and changing bits */
140 cap_mask &= ~diff;
141 /* Update the bits with the new capability */
142 cap_mask |= (value & diff);
143 /* Check for any kernel/user restrictions */
144 diff = (cap_mask & (HFI1_CAP_MUST_HAVE_KERN << HFI1_CAP_USER_SHIFT)) ^
145 ((cap_mask & HFI1_CAP_MUST_HAVE_KERN) << HFI1_CAP_USER_SHIFT);
146 cap_mask &= ~diff;
147 /* Set the bitmask to the final set */
148 *cap_mask_ptr = cap_mask;
149done:
150 return ret;
151}
152
153static int hfi1_caps_get(char *buffer, const struct kernel_param *kp)
154{
155 unsigned long cap_mask = *(unsigned long *)kp->arg;
156
157 cap_mask &= ~HFI1_CAP_LOCKED_SMASK;
158 cap_mask |= ((cap_mask & HFI1_CAP_K2U) << HFI1_CAP_USER_SHIFT);
159
160 return scnprintf(buffer, PAGE_SIZE, "0x%lx", cap_mask);
161}
162
163const char *get_unit_name(int unit)
164{
165 static char iname[16];
166
Jubin John98050712015-11-16 21:59:27 -0500167 snprintf(iname, sizeof(iname), DRIVER_NAME "_%u", unit);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400168 return iname;
169}
170
Dennis Dalessandro49dbb6c2016-01-19 14:42:06 -0800171const char *get_card_name(struct rvt_dev_info *rdi)
172{
173 struct hfi1_ibdev *ibdev = container_of(rdi, struct hfi1_ibdev, rdi);
174 struct hfi1_devdata *dd = container_of(ibdev,
175 struct hfi1_devdata, verbs_dev);
176 return get_unit_name(dd->unit);
177}
178
179struct pci_dev *get_pci_dev(struct rvt_dev_info *rdi)
180{
181 struct hfi1_ibdev *ibdev = container_of(rdi, struct hfi1_ibdev, rdi);
182 struct hfi1_devdata *dd = container_of(ibdev,
183 struct hfi1_devdata, verbs_dev);
184 return dd->pcidev;
185}
186
Mike Marciniszyn77241052015-07-30 15:17:43 -0400187/*
188 * Return count of units with at least one port ACTIVE.
189 */
190int hfi1_count_active_units(void)
191{
192 struct hfi1_devdata *dd;
193 struct hfi1_pportdata *ppd;
194 unsigned long flags;
195 int pidx, nunits_active = 0;
196
197 spin_lock_irqsave(&hfi1_devs_lock, flags);
198 list_for_each_entry(dd, &hfi1_dev_list, list) {
199 if (!(dd->flags & HFI1_PRESENT) || !dd->kregbase)
200 continue;
201 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
202 ppd = dd->pport + pidx;
203 if (ppd->lid && ppd->linkup) {
204 nunits_active++;
205 break;
206 }
207 }
208 }
209 spin_unlock_irqrestore(&hfi1_devs_lock, flags);
210 return nunits_active;
211}
212
213/*
Mike Marciniszyn77241052015-07-30 15:17:43 -0400214 * Get address of eager buffer from it's index (allocated in chunks, not
215 * contiguous).
216 */
217static inline void *get_egrbuf(const struct hfi1_ctxtdata *rcd, u64 rhf,
218 u8 *update)
219{
220 u32 idx = rhf_egr_index(rhf), offset = rhf_egr_buf_offset(rhf);
221
222 *update |= !(idx & (rcd->egrbufs.threshold - 1)) && !offset;
223 return (void *)(((u64)(rcd->egrbufs.rcvtids[idx].addr)) +
224 (offset * RCV_BUF_BLOCK_SIZE));
225}
226
227/*
228 * Validate and encode the a given RcvArray Buffer size.
229 * The function will check whether the given size falls within
230 * allowed size ranges for the respective type and, optionally,
231 * return the proper encoding.
232 */
Mike Marciniszyna82a7fc2017-02-08 05:26:02 -0800233int hfi1_rcvbuf_validate(u32 size, u8 type, u16 *encoded)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400234{
Amitoj Kaur Chawlaf5389662016-03-04 22:40:17 +0530235 if (unlikely(!PAGE_ALIGNED(size)))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400236 return 0;
237 if (unlikely(size < MIN_EAGER_BUFFER))
238 return 0;
239 if (size >
240 (type == PT_EAGER ? MAX_EAGER_BUFFER : MAX_EXPECTED_BUFFER))
241 return 0;
242 if (encoded)
243 *encoded = ilog2(size / PAGE_SIZE) + 1;
244 return 1;
245}
246
247static void rcv_hdrerr(struct hfi1_ctxtdata *rcd, struct hfi1_pportdata *ppd,
248 struct hfi1_packet *packet)
249{
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700250 struct ib_header *rhdr = packet->hdr;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400251 u32 rte = rhf_rcv_type_err(packet->rhf);
Don Hiattcb4270572017-04-09 10:16:22 -0700252 int lnh = ib_get_lnh(rhdr);
Sebastian Sanchezf3e862c2017-02-08 05:26:25 -0800253 struct hfi1_ibport *ibp = rcd_to_iport(rcd);
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800254 struct hfi1_devdata *dd = ppd->dd;
255 struct rvt_dev_info *rdi = &dd->verbs_dev.rdi;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400256
257 if (packet->rhf & (RHF_VCRC_ERR | RHF_ICRC_ERR))
258 return;
259
260 if (packet->rhf & RHF_TID_ERR) {
261 /* For TIDERR and RC QPs preemptively schedule a NAK */
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700262 struct ib_other_headers *ohdr = NULL;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400263 u32 tlen = rhf_pkt_len(packet->rhf); /* in bytes */
Don Hiattcb4270572017-04-09 10:16:22 -0700264 u16 lid = ib_get_dlid(rhdr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400265 u32 qp_num;
266 u32 rcv_flags = 0;
267
268 /* Sanity check packet */
269 if (tlen < 24)
270 goto drop;
271
272 /* Check for GRH */
Jubin Johne4909742016-02-14 20:22:00 -0800273 if (lnh == HFI1_LRH_BTH) {
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700274 ohdr = &rhdr->u.oth;
Jubin Johne4909742016-02-14 20:22:00 -0800275 } else if (lnh == HFI1_LRH_GRH) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400276 u32 vtf;
277
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700278 ohdr = &rhdr->u.l.oth;
279 if (rhdr->u.l.grh.next_hdr != IB_GRH_NEXT_HDR)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400280 goto drop;
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700281 vtf = be32_to_cpu(rhdr->u.l.grh.version_tclass_flow);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400282 if ((vtf >> IB_GRH_VERSION_SHIFT) != IB_GRH_VERSION)
283 goto drop;
284 rcv_flags |= HFI1_HAS_GRH;
Jubin Johne4909742016-02-14 20:22:00 -0800285 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400286 goto drop;
Jubin Johne4909742016-02-14 20:22:00 -0800287 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400288 /* Get the destination QP number. */
Don Hiatt7dafbab2017-05-12 09:19:55 -0700289 qp_num = ib_bth_get_qpn(ohdr);
Dennis Dalessandro8859b4a2016-01-19 14:42:11 -0800290 if (lid < be16_to_cpu(IB_MULTICAST_LID_BASE)) {
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800291 struct rvt_qp *qp;
Dean Luickb77d7132015-10-26 10:28:43 -0400292 unsigned long flags;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400293
294 rcu_read_lock();
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800295 qp = rvt_lookup_qpn(rdi, &ibp->rvp, qp_num);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400296 if (!qp) {
297 rcu_read_unlock();
298 goto drop;
299 }
300
301 /*
302 * Handle only RC QPs - for other QP types drop error
303 * packet.
304 */
Dean Luickb77d7132015-10-26 10:28:43 -0400305 spin_lock_irqsave(&qp->r_lock, flags);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400306
307 /* Check for valid receive state. */
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800308 if (!(ib_rvt_state_ops[qp->state] &
309 RVT_PROCESS_RECV_OK)) {
Dennis Dalessandro4eb06882016-01-19 14:42:39 -0800310 ibp->rvp.n_pkt_drops++;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400311 }
312
313 switch (qp->ibqp.qp_type) {
314 case IB_QPT_RC:
315 hfi1_rc_hdrerr(
316 rcd,
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700317 rhdr,
Mike Marciniszyn77241052015-07-30 15:17:43 -0400318 rcv_flags,
319 qp);
320 break;
321 default:
322 /* For now don't handle any other QP types */
323 break;
324 }
325
Dean Luickb77d7132015-10-26 10:28:43 -0400326 spin_unlock_irqrestore(&qp->r_lock, flags);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400327 rcu_read_unlock();
328 } /* Unicast QP */
329 } /* Valid packet with TIDErr */
330
331 /* handle "RcvTypeErr" flags */
332 switch (rte) {
333 case RHF_RTE_ERROR_OP_CODE_ERR:
334 {
335 u32 opcode;
336 void *ebuf = NULL;
337 __be32 *bth = NULL;
338
339 if (rhf_use_egr_bfr(packet->rhf))
340 ebuf = packet->ebuf;
341
Jubin Johnd125a6c2016-02-14 20:19:49 -0800342 if (!ebuf)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400343 goto drop; /* this should never happen */
344
345 if (lnh == HFI1_LRH_BTH)
346 bth = (__be32 *)ebuf;
347 else if (lnh == HFI1_LRH_GRH)
348 bth = (__be32 *)((char *)ebuf + sizeof(struct ib_grh));
349 else
350 goto drop;
351
352 opcode = be32_to_cpu(bth[0]) >> 24;
353 opcode &= 0xff;
354
355 if (opcode == IB_OPCODE_CNP) {
356 /*
357 * Only in pre-B0 h/w is the CNP_OPCODE handled
Edward Mascarenhas80e48982015-12-21 21:57:44 -0500358 * via this code path.
Mike Marciniszyn77241052015-07-30 15:17:43 -0400359 */
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800360 struct rvt_qp *qp = NULL;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400361 u32 lqpn, rqpn;
362 u16 rlid;
363 u8 svc_type, sl, sc5;
364
Dasaratharaman Chandramouliaad559c2017-04-09 10:16:15 -0700365 sc5 = hfi1_9B_get_sc5(rhdr, packet->rhf);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400366 sl = ibp->sc_to_sl[sc5];
367
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800368 lqpn = be32_to_cpu(bth[1]) & RVT_QPN_MASK;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400369 rcu_read_lock();
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800370 qp = rvt_lookup_qpn(rdi, &ibp->rvp, lqpn);
Jubin Johnd125a6c2016-02-14 20:19:49 -0800371 if (!qp) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400372 rcu_read_unlock();
373 goto drop;
374 }
375
376 switch (qp->ibqp.qp_type) {
377 case IB_QPT_UD:
378 rlid = 0;
379 rqpn = 0;
380 svc_type = IB_CC_SVCTYPE_UD;
381 break;
382 case IB_QPT_UC:
Don Hiattcb4270572017-04-09 10:16:22 -0700383 rlid = ib_get_slid(rhdr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400384 rqpn = qp->remote_qpn;
385 svc_type = IB_CC_SVCTYPE_UC;
386 break;
387 default:
388 goto drop;
389 }
390
391 process_becn(ppd, sl, rlid, lqpn, rqpn, svc_type);
392 rcu_read_unlock();
393 }
394
395 packet->rhf &= ~RHF_RCV_TYPE_ERR_SMASK;
396 break;
397 }
398 default:
399 break;
400 }
401
402drop:
403 return;
404}
405
406static inline void init_packet(struct hfi1_ctxtdata *rcd,
Jubin John17fb4f22016-02-14 20:21:52 -0800407 struct hfi1_packet *packet)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400408{
Mike Marciniszyn77241052015-07-30 15:17:43 -0400409 packet->rsize = rcd->rcvhdrqentsize; /* words */
410 packet->maxcnt = rcd->rcvhdrq_cnt * packet->rsize; /* words */
411 packet->rcd = rcd;
412 packet->updegr = 0;
413 packet->etail = -1;
Dean Luickf4f30031c2015-10-26 10:28:44 -0400414 packet->rhf_addr = get_rhf_addr(rcd);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400415 packet->rhf = rhf_to_cpu(packet->rhf_addr);
416 packet->rhqoff = rcd->head;
417 packet->numpkt = 0;
418 packet->rcv_flags = 0;
419}
420
Mitko Haralanov5fd2b562016-07-25 13:38:07 -0700421void hfi1_process_ecn_slowpath(struct rvt_qp *qp, struct hfi1_packet *pkt,
422 bool do_cnp)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400423{
424 struct hfi1_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700425 struct ib_header *hdr = pkt->hdr;
426 struct ib_other_headers *ohdr = pkt->ohdr;
Mitko Haralanov5fd2b562016-07-25 13:38:07 -0700427 struct ib_grh *grh = NULL;
428 u32 rqpn = 0, bth1;
Don Hiattcb4270572017-04-09 10:16:22 -0700429 u16 rlid, dlid = ib_get_dlid(hdr);
Mitko Haralanov5fd2b562016-07-25 13:38:07 -0700430 u8 sc, svc_type;
431 bool is_mcast = false;
432
433 if (pkt->rcv_flags & HFI1_HAS_GRH)
434 grh = &hdr->u.l.grh;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400435
436 switch (qp->ibqp.qp_type) {
Arthur Kepner977940b2015-11-04 21:10:10 -0500437 case IB_QPT_SMI:
438 case IB_QPT_GSI:
Mike Marciniszyn77241052015-07-30 15:17:43 -0400439 case IB_QPT_UD:
Don Hiattcb4270572017-04-09 10:16:22 -0700440 rlid = ib_get_slid(hdr);
Don Hiatt7dafbab2017-05-12 09:19:55 -0700441 rqpn = ib_get_sqpn(ohdr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400442 svc_type = IB_CC_SVCTYPE_UD;
Mitko Haralanov5fd2b562016-07-25 13:38:07 -0700443 is_mcast = (dlid > be16_to_cpu(IB_MULTICAST_LID_BASE)) &&
444 (dlid != be16_to_cpu(IB_LID_PERMISSIVE));
Mike Marciniszyn77241052015-07-30 15:17:43 -0400445 break;
Arthur Kepner977940b2015-11-04 21:10:10 -0500446 case IB_QPT_UC:
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400447 rlid = rdma_ah_get_dlid(&qp->remote_ah_attr);
Arthur Kepner977940b2015-11-04 21:10:10 -0500448 rqpn = qp->remote_qpn;
449 svc_type = IB_CC_SVCTYPE_UC;
450 break;
451 case IB_QPT_RC:
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400452 rlid = rdma_ah_get_dlid(&qp->remote_ah_attr);
Arthur Kepner977940b2015-11-04 21:10:10 -0500453 rqpn = qp->remote_qpn;
454 svc_type = IB_CC_SVCTYPE_RC;
455 break;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400456 default:
457 return;
458 }
459
Dasaratharaman Chandramouliaad559c2017-04-09 10:16:15 -0700460 sc = hfi1_9B_get_sc5(hdr, pkt->rhf);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400461
Mitko Haralanov5fd2b562016-07-25 13:38:07 -0700462 bth1 = be32_to_cpu(ohdr->bth[1]);
Don Hiatt3d591092017-04-09 10:16:28 -0700463 if (do_cnp && (bth1 & IB_FECN_SMASK)) {
Don Hiatt7dafbab2017-05-12 09:19:55 -0700464 u16 pkey = ib_bth_get_pkey(ohdr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400465
Mitko Haralanov5fd2b562016-07-25 13:38:07 -0700466 return_cnp(ibp, qp, rqpn, pkey, dlid, rlid, sc, grh);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400467 }
468
Don Hiatt3d591092017-04-09 10:16:28 -0700469 if (!is_mcast && (bth1 & IB_BECN_SMASK)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400470 struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800471 u32 lqpn = bth1 & RVT_QPN_MASK;
Mitko Haralanov5fd2b562016-07-25 13:38:07 -0700472 u8 sl = ibp->sc_to_sl[sc];
Mike Marciniszyn77241052015-07-30 15:17:43 -0400473
Arthur Kepner977940b2015-11-04 21:10:10 -0500474 process_becn(ppd, sl, rlid, lqpn, rqpn, svc_type);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400475 }
Mitko Haralanov5fd2b562016-07-25 13:38:07 -0700476
Mike Marciniszyn77241052015-07-30 15:17:43 -0400477}
478
479struct ps_mdata {
480 struct hfi1_ctxtdata *rcd;
481 u32 rsize;
482 u32 maxcnt;
483 u32 ps_head;
484 u32 ps_tail;
485 u32 ps_seq;
486};
487
488static inline void init_ps_mdata(struct ps_mdata *mdata,
489 struct hfi1_packet *packet)
490{
491 struct hfi1_ctxtdata *rcd = packet->rcd;
492
493 mdata->rcd = rcd;
494 mdata->rsize = packet->rsize;
495 mdata->maxcnt = packet->maxcnt;
Arthur Kepner3e7ccca2015-11-04 21:10:09 -0500496 mdata->ps_head = packet->rhqoff;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400497
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500498 if (HFI1_CAP_KGET_MASK(rcd->flags, DMA_RTAIL)) {
Arthur Kepner3e7ccca2015-11-04 21:10:09 -0500499 mdata->ps_tail = get_rcvhdrtail(rcd);
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500500 if (rcd->ctxt == HFI1_CTRL_CTXT)
501 mdata->ps_seq = rcd->seq_cnt;
502 else
503 mdata->ps_seq = 0; /* not used with DMA_RTAIL */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400504 } else {
505 mdata->ps_tail = 0; /* used only with DMA_RTAIL*/
506 mdata->ps_seq = rcd->seq_cnt;
507 }
508}
509
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500510static inline int ps_done(struct ps_mdata *mdata, u64 rhf,
511 struct hfi1_ctxtdata *rcd)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400512{
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500513 if (HFI1_CAP_KGET_MASK(rcd->flags, DMA_RTAIL))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400514 return mdata->ps_head == mdata->ps_tail;
515 return mdata->ps_seq != rhf_rcv_seq(rhf);
516}
517
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500518static inline int ps_skip(struct ps_mdata *mdata, u64 rhf,
519 struct hfi1_ctxtdata *rcd)
520{
521 /*
522 * Control context can potentially receive an invalid rhf.
523 * Drop such packets.
524 */
525 if ((rcd->ctxt == HFI1_CTRL_CTXT) && (mdata->ps_head != mdata->ps_tail))
526 return mdata->ps_seq != rhf_rcv_seq(rhf);
527
528 return 0;
529}
530
531static inline void update_ps_mdata(struct ps_mdata *mdata,
532 struct hfi1_ctxtdata *rcd)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400533{
Mike Marciniszyn77241052015-07-30 15:17:43 -0400534 mdata->ps_head += mdata->rsize;
Arthur Kepner3e7ccca2015-11-04 21:10:09 -0500535 if (mdata->ps_head >= mdata->maxcnt)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400536 mdata->ps_head = 0;
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500537
538 /* Control context must do seq counting */
539 if (!HFI1_CAP_KGET_MASK(rcd->flags, DMA_RTAIL) ||
540 (rcd->ctxt == HFI1_CTRL_CTXT)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400541 if (++mdata->ps_seq > 13)
542 mdata->ps_seq = 1;
543 }
544}
545
546/*
547 * prescan_rxq - search through the receive queue looking for packets
548 * containing Excplicit Congestion Notifications (FECNs, or BECNs).
549 * When an ECN is found, process the Congestion Notification, and toggle
550 * it off.
Vennila Megavannan6c9e50f2016-02-03 14:32:57 -0800551 * This is declared as a macro to allow quick checking of the port to avoid
552 * the overhead of a function call if not enabled.
Mike Marciniszyn77241052015-07-30 15:17:43 -0400553 */
Vennila Megavannan6c9e50f2016-02-03 14:32:57 -0800554#define prescan_rxq(rcd, packet) \
555 do { \
556 if (rcd->ppd->cc_prescan) \
557 __prescan_rxq(packet); \
558 } while (0)
559static void __prescan_rxq(struct hfi1_packet *packet)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400560{
561 struct hfi1_ctxtdata *rcd = packet->rcd;
562 struct ps_mdata mdata;
563
Mike Marciniszyn77241052015-07-30 15:17:43 -0400564 init_ps_mdata(&mdata, packet);
565
566 while (1) {
567 struct hfi1_devdata *dd = rcd->dd;
Sebastian Sanchezf3e862c2017-02-08 05:26:25 -0800568 struct hfi1_ibport *ibp = rcd_to_iport(rcd);
Jubin John50e5dcb2016-02-14 20:19:41 -0800569 __le32 *rhf_addr = (__le32 *)rcd->rcvhdrq + mdata.ps_head +
Mike Marciniszyn77241052015-07-30 15:17:43 -0400570 dd->rhf_offset;
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800571 struct rvt_qp *qp;
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700572 struct ib_header *hdr;
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800573 struct rvt_dev_info *rdi = &dd->verbs_dev.rdi;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400574 u64 rhf = rhf_to_cpu(rhf_addr);
Arthur Kepner977940b2015-11-04 21:10:10 -0500575 u32 etype = rhf_rcv_type(rhf), qpn, bth1;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400576 int is_ecn = 0;
577 u8 lnh;
578
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500579 if (ps_done(&mdata, rhf, rcd))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400580 break;
581
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500582 if (ps_skip(&mdata, rhf, rcd))
583 goto next;
584
Mike Marciniszyn77241052015-07-30 15:17:43 -0400585 if (etype != RHF_RCV_TYPE_IB)
586 goto next;
587
Dasaratharaman Chandramoulif2d8a0b2016-10-25 13:12:23 -0700588 packet->hdr = hfi1_get_msgheader(dd, rhf_addr);
589 hdr = packet->hdr;
Don Hiattcb4270572017-04-09 10:16:22 -0700590 lnh = ib_get_lnh(hdr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400591
Dasaratharaman Chandramoulif2d8a0b2016-10-25 13:12:23 -0700592 if (lnh == HFI1_LRH_BTH) {
593 packet->ohdr = &hdr->u.oth;
594 } else if (lnh == HFI1_LRH_GRH) {
595 packet->ohdr = &hdr->u.l.oth;
596 packet->rcv_flags |= HFI1_HAS_GRH;
597 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400598 goto next; /* just in case */
Dasaratharaman Chandramoulif2d8a0b2016-10-25 13:12:23 -0700599 }
Mitko Haralanov5fd2b562016-07-25 13:38:07 -0700600
Dasaratharaman Chandramoulif2d8a0b2016-10-25 13:12:23 -0700601 bth1 = be32_to_cpu(packet->ohdr->bth[1]);
Don Hiatt3d591092017-04-09 10:16:28 -0700602 is_ecn = !!(bth1 & (IB_FECN_SMASK | IB_BECN_SMASK));
Mike Marciniszyn77241052015-07-30 15:17:43 -0400603
604 if (!is_ecn)
605 goto next;
606
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800607 qpn = bth1 & RVT_QPN_MASK;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400608 rcu_read_lock();
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800609 qp = rvt_lookup_qpn(rdi, &ibp->rvp, qpn);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400610
Jubin Johnd125a6c2016-02-14 20:19:49 -0800611 if (!qp) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400612 rcu_read_unlock();
613 goto next;
614 }
615
Mitko Haralanov5fd2b562016-07-25 13:38:07 -0700616 process_ecn(qp, packet, true);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400617 rcu_read_unlock();
Arthur Kepner977940b2015-11-04 21:10:10 -0500618
619 /* turn off BECN, FECN */
Don Hiatt3d591092017-04-09 10:16:28 -0700620 bth1 &= ~(IB_FECN_SMASK | IB_BECN_SMASK);
Dasaratharaman Chandramoulif2d8a0b2016-10-25 13:12:23 -0700621 packet->ohdr->bth[1] = cpu_to_be32(bth1);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400622next:
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500623 update_ps_mdata(&mdata, rcd);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400624 }
625}
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500626
Mike Marciniszyna82a7fc2017-02-08 05:26:02 -0800627static void process_rcv_qp_work(struct hfi1_ctxtdata *rcd)
628{
629 struct rvt_qp *qp, *nqp;
630
631 /*
632 * Iterate over all QPs waiting to respond.
633 * The list won't change since the IRQ is only run on one CPU.
634 */
635 list_for_each_entry_safe(qp, nqp, &rcd->qp_wait_list, rspwait) {
636 list_del_init(&qp->rspwait);
637 if (qp->r_flags & RVT_R_RSP_NAK) {
638 qp->r_flags &= ~RVT_R_RSP_NAK;
639 hfi1_send_rc_ack(rcd, qp, 0);
640 }
641 if (qp->r_flags & RVT_R_RSP_SEND) {
642 unsigned long flags;
643
644 qp->r_flags &= ~RVT_R_RSP_SEND;
645 spin_lock_irqsave(&qp->s_lock, flags);
646 if (ib_rvt_state_ops[qp->state] &
647 RVT_PROCESS_OR_FLUSH_SEND)
648 hfi1_schedule_send(qp);
649 spin_unlock_irqrestore(&qp->s_lock, flags);
650 }
651 rvt_put_qp(qp);
652 }
653}
654
655static noinline int max_packet_exceeded(struct hfi1_packet *packet, int thread)
656{
657 if (thread) {
658 if ((packet->numpkt & (MAX_PKT_RECV_THREAD - 1)) == 0)
659 /* allow defered processing */
660 process_rcv_qp_work(packet->rcd);
661 cond_resched();
662 return RCV_PKT_OK;
663 } else {
664 this_cpu_inc(*packet->rcd->dd->rcv_limit);
665 return RCV_PKT_LIMIT;
666 }
667}
668
669static inline int check_max_packet(struct hfi1_packet *packet, int thread)
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500670{
671 int ret = RCV_PKT_OK;
672
Mike Marciniszyna82a7fc2017-02-08 05:26:02 -0800673 if (unlikely((packet->numpkt & (MAX_PKT_RECV - 1)) == 0))
674 ret = max_packet_exceeded(packet, thread);
675 return ret;
676}
677
678static noinline int skip_rcv_packet(struct hfi1_packet *packet, int thread)
679{
680 int ret;
681
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500682 /* Set up for the next packet */
683 packet->rhqoff += packet->rsize;
684 if (packet->rhqoff >= packet->maxcnt)
685 packet->rhqoff = 0;
686
687 packet->numpkt++;
Mike Marciniszyna82a7fc2017-02-08 05:26:02 -0800688 ret = check_max_packet(packet, thread);
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500689
690 packet->rhf_addr = (__le32 *)packet->rcd->rcvhdrq + packet->rhqoff +
691 packet->rcd->dd->rhf_offset;
692 packet->rhf = rhf_to_cpu(packet->rhf_addr);
693
694 return ret;
695}
Mike Marciniszyn77241052015-07-30 15:17:43 -0400696
Dean Luickf4f30031c2015-10-26 10:28:44 -0400697static inline int process_rcv_packet(struct hfi1_packet *packet, int thread)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400698{
Mike Marciniszyna82a7fc2017-02-08 05:26:02 -0800699 int ret;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400700
701 packet->hdr = hfi1_get_msgheader(packet->rcd->dd,
702 packet->rhf_addr);
703 packet->hlen = (u8 *)packet->rhf_addr - (u8 *)packet->hdr;
704 packet->etype = rhf_rcv_type(packet->rhf);
705 /* total length */
706 packet->tlen = rhf_pkt_len(packet->rhf); /* in bytes */
707 /* retrieve eager buffer details */
708 packet->ebuf = NULL;
709 if (rhf_use_egr_bfr(packet->rhf)) {
710 packet->etail = rhf_egr_index(packet->rhf);
711 packet->ebuf = get_egrbuf(packet->rcd, packet->rhf,
712 &packet->updegr);
713 /*
714 * Prefetch the contents of the eager buffer. It is
715 * OK to send a negative length to prefetch_range().
716 * The +2 is the size of the RHF.
717 */
718 prefetch_range(packet->ebuf,
Jubin John17fb4f22016-02-14 20:21:52 -0800719 packet->tlen - ((packet->rcd->rcvhdrqentsize -
720 (rhf_hdrq_offset(packet->rhf)
721 + 2)) * 4));
Mike Marciniszyn77241052015-07-30 15:17:43 -0400722 }
723
724 /*
725 * Call a type specific handler for the packet. We
726 * should be able to trust that etype won't be beyond
727 * the range of valid indexes. If so something is really
728 * wrong and we can probably just let things come
729 * crashing down. There is no need to eat another
730 * comparison in this performance critical code.
731 */
732 packet->rcd->dd->rhf_rcv_function_map[packet->etype](packet);
733 packet->numpkt++;
734
735 /* Set up for the next packet */
736 packet->rhqoff += packet->rsize;
737 if (packet->rhqoff >= packet->maxcnt)
738 packet->rhqoff = 0;
739
Mike Marciniszyna82a7fc2017-02-08 05:26:02 -0800740 ret = check_max_packet(packet, thread);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400741
Jubin John50e5dcb2016-02-14 20:19:41 -0800742 packet->rhf_addr = (__le32 *)packet->rcd->rcvhdrq + packet->rhqoff +
Mike Marciniszyn77241052015-07-30 15:17:43 -0400743 packet->rcd->dd->rhf_offset;
744 packet->rhf = rhf_to_cpu(packet->rhf_addr);
745
746 return ret;
747}
748
749static inline void process_rcv_update(int last, struct hfi1_packet *packet)
750{
751 /*
752 * Update head regs etc., every 16 packets, if not last pkt,
753 * to help prevent rcvhdrq overflows, when many packets
754 * are processed and queue is nearly full.
755 * Don't request an interrupt for intermediate updates.
756 */
757 if (!last && !(packet->numpkt & 0xf)) {
758 update_usrhead(packet->rcd, packet->rhqoff, packet->updegr,
759 packet->etail, 0, 0);
760 packet->updegr = 0;
761 }
762 packet->rcv_flags = 0;
763}
764
765static inline void finish_packet(struct hfi1_packet *packet)
766{
Mike Marciniszyn77241052015-07-30 15:17:43 -0400767 /*
768 * Nothing we need to free for the packet.
769 *
770 * The only thing we need to do is a final update and call for an
771 * interrupt
772 */
773 update_usrhead(packet->rcd, packet->rcd->head, packet->updegr,
774 packet->etail, rcv_intr_dynamic, packet->numpkt);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400775}
776
Mike Marciniszyn77241052015-07-30 15:17:43 -0400777/*
778 * Handle receive interrupts when using the no dma rtail option.
779 */
Dean Luickf4f30031c2015-10-26 10:28:44 -0400780int handle_receive_interrupt_nodma_rtail(struct hfi1_ctxtdata *rcd, int thread)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400781{
782 u32 seq;
Dean Luickf4f30031c2015-10-26 10:28:44 -0400783 int last = RCV_PKT_OK;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400784 struct hfi1_packet packet;
785
786 init_packet(rcd, &packet);
787 seq = rhf_rcv_seq(packet.rhf);
Dean Luickf4f30031c2015-10-26 10:28:44 -0400788 if (seq != rcd->seq_cnt) {
789 last = RCV_PKT_DONE;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400790 goto bail;
Dean Luickf4f30031c2015-10-26 10:28:44 -0400791 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400792
Vennila Megavannan6c9e50f2016-02-03 14:32:57 -0800793 prescan_rxq(rcd, &packet);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400794
Dean Luickf4f30031c2015-10-26 10:28:44 -0400795 while (last == RCV_PKT_OK) {
796 last = process_rcv_packet(&packet, thread);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400797 seq = rhf_rcv_seq(packet.rhf);
798 if (++rcd->seq_cnt > 13)
799 rcd->seq_cnt = 1;
800 if (seq != rcd->seq_cnt)
Dean Luickf4f30031c2015-10-26 10:28:44 -0400801 last = RCV_PKT_DONE;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400802 process_rcv_update(last, &packet);
803 }
Mike Marciniszyna82a7fc2017-02-08 05:26:02 -0800804 process_rcv_qp_work(rcd);
805 rcd->head = packet.rhqoff;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400806bail:
807 finish_packet(&packet);
Dean Luickf4f30031c2015-10-26 10:28:44 -0400808 return last;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400809}
810
Dean Luickf4f30031c2015-10-26 10:28:44 -0400811int handle_receive_interrupt_dma_rtail(struct hfi1_ctxtdata *rcd, int thread)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400812{
813 u32 hdrqtail;
Dean Luickf4f30031c2015-10-26 10:28:44 -0400814 int last = RCV_PKT_OK;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400815 struct hfi1_packet packet;
816
817 init_packet(rcd, &packet);
818 hdrqtail = get_rcvhdrtail(rcd);
Dean Luickf4f30031c2015-10-26 10:28:44 -0400819 if (packet.rhqoff == hdrqtail) {
820 last = RCV_PKT_DONE;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400821 goto bail;
Dean Luickf4f30031c2015-10-26 10:28:44 -0400822 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400823 smp_rmb(); /* prevent speculative reads of dma'ed hdrq */
824
Vennila Megavannan6c9e50f2016-02-03 14:32:57 -0800825 prescan_rxq(rcd, &packet);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400826
Dean Luickf4f30031c2015-10-26 10:28:44 -0400827 while (last == RCV_PKT_OK) {
828 last = process_rcv_packet(&packet, thread);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400829 if (packet.rhqoff == hdrqtail)
Dean Luickf4f30031c2015-10-26 10:28:44 -0400830 last = RCV_PKT_DONE;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400831 process_rcv_update(last, &packet);
832 }
Mike Marciniszyna82a7fc2017-02-08 05:26:02 -0800833 process_rcv_qp_work(rcd);
834 rcd->head = packet.rhqoff;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400835bail:
836 finish_packet(&packet);
Dean Luickf4f30031c2015-10-26 10:28:44 -0400837 return last;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400838}
839
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700840static inline void set_nodma_rtail(struct hfi1_devdata *dd, u8 ctxt)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400841{
842 int i;
843
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700844 /*
845 * For dynamically allocated kernel contexts (like vnic) switch
846 * interrupt handler only for that context. Otherwise, switch
847 * interrupt handler for all statically allocated kernel contexts.
848 */
849 if (ctxt >= dd->first_dyn_alloc_ctxt) {
850 dd->rcd[ctxt]->do_interrupt =
851 &handle_receive_interrupt_nodma_rtail;
852 return;
853 }
854
855 for (i = HFI1_CTRL_CTXT + 1; i < dd->first_dyn_alloc_ctxt; i++)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400856 dd->rcd[i]->do_interrupt =
857 &handle_receive_interrupt_nodma_rtail;
858}
859
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700860static inline void set_dma_rtail(struct hfi1_devdata *dd, u8 ctxt)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400861{
862 int i;
863
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700864 /*
865 * For dynamically allocated kernel contexts (like vnic) switch
866 * interrupt handler only for that context. Otherwise, switch
867 * interrupt handler for all statically allocated kernel contexts.
868 */
869 if (ctxt >= dd->first_dyn_alloc_ctxt) {
870 dd->rcd[ctxt]->do_interrupt =
871 &handle_receive_interrupt_dma_rtail;
872 return;
873 }
874
875 for (i = HFI1_CTRL_CTXT + 1; i < dd->first_dyn_alloc_ctxt; i++)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400876 dd->rcd[i]->do_interrupt =
877 &handle_receive_interrupt_dma_rtail;
878}
879
Jim Snowfb9036d2016-01-11 18:32:21 -0500880void set_all_slowpath(struct hfi1_devdata *dd)
881{
882 int i;
883
884 /* HFI1_CTRL_CTXT must always use the slow path interrupt handler */
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700885 for (i = HFI1_CTRL_CTXT + 1; i < dd->num_rcv_contexts; i++) {
886 struct hfi1_ctxtdata *rcd = dd->rcd[i];
887
888 if ((i < dd->first_dyn_alloc_ctxt) ||
889 (rcd && rcd->sc && (rcd->sc->type == SC_KERNEL)))
890 rcd->do_interrupt = &handle_receive_interrupt;
891 }
Jim Snowfb9036d2016-01-11 18:32:21 -0500892}
893
894static inline int set_armed_to_active(struct hfi1_ctxtdata *rcd,
Mike Marciniszync867caa2016-08-09 11:19:55 -0400895 struct hfi1_packet *packet,
Jim Snowfb9036d2016-01-11 18:32:21 -0500896 struct hfi1_devdata *dd)
897{
898 struct work_struct *lsaw = &rcd->ppd->linkstate_active_work;
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700899 struct ib_header *hdr = hfi1_get_msgheader(packet->rcd->dd,
900 packet->rhf_addr);
Mike Marciniszyn69b9f4a2016-08-09 11:19:56 -0400901 u8 etype = rhf_rcv_type(packet->rhf);
Jim Snowfb9036d2016-01-11 18:32:21 -0500902
Dasaratharaman Chandramouliaad559c2017-04-09 10:16:15 -0700903 if (etype == RHF_RCV_TYPE_IB &&
904 hfi1_9B_get_sc5(hdr, packet->rhf) != 0xf) {
Jim Snowfb9036d2016-01-11 18:32:21 -0500905 int hwstate = read_logical_state(dd);
906
907 if (hwstate != LSTATE_ACTIVE) {
908 dd_dev_info(dd, "Unexpected link state %d\n", hwstate);
909 return 0;
910 }
911
912 queue_work(rcd->ppd->hfi1_wq, lsaw);
913 return 1;
914 }
915 return 0;
916}
917
Mike Marciniszyn77241052015-07-30 15:17:43 -0400918/*
919 * handle_receive_interrupt - receive a packet
920 * @rcd: the context
921 *
922 * Called from interrupt handler for errors or receive interrupt.
923 * This is the slow path interrupt handler.
924 */
Dean Luickf4f30031c2015-10-26 10:28:44 -0400925int handle_receive_interrupt(struct hfi1_ctxtdata *rcd, int thread)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400926{
Mike Marciniszyn77241052015-07-30 15:17:43 -0400927 struct hfi1_devdata *dd = rcd->dd;
928 u32 hdrqtail;
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500929 int needset, last = RCV_PKT_OK;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400930 struct hfi1_packet packet;
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500931 int skip_pkt = 0;
932
933 /* Control context will always use the slow path interrupt handler */
934 needset = (rcd->ctxt == HFI1_CTRL_CTXT) ? 0 : 1;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400935
936 init_packet(rcd, &packet);
937
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500938 if (!HFI1_CAP_KGET_MASK(rcd->flags, DMA_RTAIL)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400939 u32 seq = rhf_rcv_seq(packet.rhf);
940
Dean Luickf4f30031c2015-10-26 10:28:44 -0400941 if (seq != rcd->seq_cnt) {
942 last = RCV_PKT_DONE;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400943 goto bail;
Dean Luickf4f30031c2015-10-26 10:28:44 -0400944 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400945 hdrqtail = 0;
946 } else {
947 hdrqtail = get_rcvhdrtail(rcd);
Dean Luickf4f30031c2015-10-26 10:28:44 -0400948 if (packet.rhqoff == hdrqtail) {
949 last = RCV_PKT_DONE;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400950 goto bail;
Dean Luickf4f30031c2015-10-26 10:28:44 -0400951 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400952 smp_rmb(); /* prevent speculative reads of dma'ed hdrq */
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500953
954 /*
955 * Control context can potentially receive an invalid
956 * rhf. Drop such packets.
957 */
958 if (rcd->ctxt == HFI1_CTRL_CTXT) {
959 u32 seq = rhf_rcv_seq(packet.rhf);
960
961 if (seq != rcd->seq_cnt)
962 skip_pkt = 1;
963 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400964 }
965
Vennila Megavannan6c9e50f2016-02-03 14:32:57 -0800966 prescan_rxq(rcd, &packet);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400967
Dean Luickf4f30031c2015-10-26 10:28:44 -0400968 while (last == RCV_PKT_OK) {
Jubin John17fb4f22016-02-14 20:21:52 -0800969 if (unlikely(dd->do_drop &&
970 atomic_xchg(&dd->drop_packet, DROP_PACKET_OFF) ==
971 DROP_PACKET_ON)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400972 dd->do_drop = 0;
973
974 /* On to the next packet */
975 packet.rhqoff += packet.rsize;
Jubin John50e5dcb2016-02-14 20:19:41 -0800976 packet.rhf_addr = (__le32 *)rcd->rcvhdrq +
Mike Marciniszyn77241052015-07-30 15:17:43 -0400977 packet.rhqoff +
978 dd->rhf_offset;
979 packet.rhf = rhf_to_cpu(packet.rhf_addr);
980
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500981 } else if (skip_pkt) {
982 last = skip_rcv_packet(&packet, thread);
983 skip_pkt = 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400984 } else {
Jim Snowfb9036d2016-01-11 18:32:21 -0500985 /* Auto activate link on non-SC15 packet receive */
986 if (unlikely(rcd->ppd->host_link_state ==
987 HLS_UP_ARMED) &&
Mike Marciniszync867caa2016-08-09 11:19:55 -0400988 set_armed_to_active(rcd, &packet, dd))
Jim Snowfb9036d2016-01-11 18:32:21 -0500989 goto bail;
Dean Luickf4f30031c2015-10-26 10:28:44 -0400990 last = process_rcv_packet(&packet, thread);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400991 }
992
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500993 if (!HFI1_CAP_KGET_MASK(rcd->flags, DMA_RTAIL)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400994 u32 seq = rhf_rcv_seq(packet.rhf);
995
996 if (++rcd->seq_cnt > 13)
997 rcd->seq_cnt = 1;
998 if (seq != rcd->seq_cnt)
Dean Luickf4f30031c2015-10-26 10:28:44 -0400999 last = RCV_PKT_DONE;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001000 if (needset) {
Jubin John17fb4f22016-02-14 20:21:52 -08001001 dd_dev_info(dd, "Switching to NO_DMA_RTAIL\n");
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -07001002 set_nodma_rtail(dd, rcd->ctxt);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001003 needset = 0;
1004 }
1005 } else {
1006 if (packet.rhqoff == hdrqtail)
Dean Luickf4f30031c2015-10-26 10:28:44 -04001007 last = RCV_PKT_DONE;
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -05001008 /*
1009 * Control context can potentially receive an invalid
1010 * rhf. Drop such packets.
1011 */
1012 if (rcd->ctxt == HFI1_CTRL_CTXT) {
1013 u32 seq = rhf_rcv_seq(packet.rhf);
1014
1015 if (++rcd->seq_cnt > 13)
1016 rcd->seq_cnt = 1;
1017 if (!last && (seq != rcd->seq_cnt))
1018 skip_pkt = 1;
1019 }
1020
Mike Marciniszyn77241052015-07-30 15:17:43 -04001021 if (needset) {
1022 dd_dev_info(dd,
1023 "Switching to DMA_RTAIL\n");
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -07001024 set_dma_rtail(dd, rcd->ctxt);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001025 needset = 0;
1026 }
1027 }
1028
1029 process_rcv_update(last, &packet);
1030 }
1031
Mike Marciniszyna82a7fc2017-02-08 05:26:02 -08001032 process_rcv_qp_work(rcd);
1033 rcd->head = packet.rhqoff;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001034
1035bail:
1036 /*
1037 * Always write head at end, and setup rcv interrupt, even
1038 * if no packets were processed.
1039 */
1040 finish_packet(&packet);
Dean Luickf4f30031c2015-10-26 10:28:44 -04001041 return last;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001042}
1043
1044/*
Jim Snowfb9036d2016-01-11 18:32:21 -05001045 * We may discover in the interrupt that the hardware link state has
1046 * changed from ARMED to ACTIVE (due to the arrival of a non-SC15 packet),
1047 * and we need to update the driver's notion of the link state. We cannot
1048 * run set_link_state from interrupt context, so we queue this function on
1049 * a workqueue.
1050 *
1051 * We delay the regular interrupt processing until after the state changes
1052 * so that the link will be in the correct state by the time any application
1053 * we wake up attempts to send a reply to any message it received.
1054 * (Subsequent receive interrupts may possibly force the wakeup before we
1055 * update the link state.)
1056 *
1057 * The rcd is freed in hfi1_free_ctxtdata after hfi1_postinit_cleanup invokes
1058 * dd->f_cleanup(dd) to disable the interrupt handler and flush workqueues,
1059 * so we're safe from use-after-free of the rcd.
1060 */
1061void receive_interrupt_work(struct work_struct *work)
1062{
1063 struct hfi1_pportdata *ppd = container_of(work, struct hfi1_pportdata,
1064 linkstate_active_work);
1065 struct hfi1_devdata *dd = ppd->dd;
1066 int i;
1067
1068 /* Received non-SC15 packet implies neighbor_normal */
1069 ppd->neighbor_normal = 1;
1070 set_link_state(ppd, HLS_UP_ACTIVE);
1071
1072 /*
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -07001073 * Interrupt all statically allocated kernel contexts that could
1074 * have had an interrupt during auto activation.
Jim Snowfb9036d2016-01-11 18:32:21 -05001075 */
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -07001076 for (i = HFI1_CTRL_CTXT; i < dd->first_dyn_alloc_ctxt; i++)
Jim Snowfb9036d2016-01-11 18:32:21 -05001077 force_recv_intr(dd->rcd[i]);
1078}
1079
1080/*
Mike Marciniszyn77241052015-07-30 15:17:43 -04001081 * Convert a given MTU size to the on-wire MAD packet enumeration.
1082 * Return -1 if the size is invalid.
1083 */
1084int mtu_to_enum(u32 mtu, int default_if_bad)
1085{
1086 switch (mtu) {
1087 case 0: return OPA_MTU_0;
1088 case 256: return OPA_MTU_256;
1089 case 512: return OPA_MTU_512;
1090 case 1024: return OPA_MTU_1024;
1091 case 2048: return OPA_MTU_2048;
1092 case 4096: return OPA_MTU_4096;
1093 case 8192: return OPA_MTU_8192;
1094 case 10240: return OPA_MTU_10240;
1095 }
1096 return default_if_bad;
1097}
1098
1099u16 enum_to_mtu(int mtu)
1100{
1101 switch (mtu) {
1102 case OPA_MTU_0: return 0;
1103 case OPA_MTU_256: return 256;
1104 case OPA_MTU_512: return 512;
1105 case OPA_MTU_1024: return 1024;
1106 case OPA_MTU_2048: return 2048;
1107 case OPA_MTU_4096: return 4096;
1108 case OPA_MTU_8192: return 8192;
1109 case OPA_MTU_10240: return 10240;
1110 default: return 0xffff;
1111 }
1112}
1113
1114/*
1115 * set_mtu - set the MTU
1116 * @ppd: the per port data
1117 *
1118 * We can handle "any" incoming size, the issue here is whether we
1119 * need to restrict our outgoing size. We do not deal with what happens
1120 * to programs that are already running when the size changes.
1121 */
1122int set_mtu(struct hfi1_pportdata *ppd)
1123{
1124 struct hfi1_devdata *dd = ppd->dd;
1125 int i, drain, ret = 0, is_up = 0;
1126
1127 ppd->ibmtu = 0;
1128 for (i = 0; i < ppd->vls_supported; i++)
1129 if (ppd->ibmtu < dd->vld[i].mtu)
1130 ppd->ibmtu = dd->vld[i].mtu;
1131 ppd->ibmaxlen = ppd->ibmtu + lrh_max_header_bytes(ppd->dd);
1132
1133 mutex_lock(&ppd->hls_lock);
Jubin Johnd0d236e2016-02-14 20:20:15 -08001134 if (ppd->host_link_state == HLS_UP_INIT ||
1135 ppd->host_link_state == HLS_UP_ARMED ||
1136 ppd->host_link_state == HLS_UP_ACTIVE)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001137 is_up = 1;
1138
1139 drain = !is_ax(dd) && is_up;
1140
1141 if (drain)
1142 /*
1143 * MTU is specified per-VL. To ensure that no packet gets
1144 * stuck (due, e.g., to the MTU for the packet's VL being
1145 * reduced), empty the per-VL FIFOs before adjusting MTU.
1146 */
1147 ret = stop_drain_data_vls(dd);
1148
1149 if (ret) {
1150 dd_dev_err(dd, "%s: cannot stop/drain VLs - refusing to change per-VL MTUs\n",
1151 __func__);
1152 goto err;
1153 }
1154
1155 hfi1_set_ib_cfg(ppd, HFI1_IB_CFG_MTU, 0);
1156
1157 if (drain)
1158 open_fill_data_vls(dd); /* reopen all VLs */
1159
1160err:
1161 mutex_unlock(&ppd->hls_lock);
1162
1163 return ret;
1164}
1165
1166int hfi1_set_lid(struct hfi1_pportdata *ppd, u32 lid, u8 lmc)
1167{
1168 struct hfi1_devdata *dd = ppd->dd;
1169
1170 ppd->lid = lid;
1171 ppd->lmc = lmc;
1172 hfi1_set_ib_cfg(ppd, HFI1_IB_CFG_LIDLMC, 0);
1173
Jakub Pawlakcde10af2016-05-12 10:23:35 -07001174 dd_dev_info(dd, "port %u: got a lid: 0x%x\n", ppd->port, lid);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001175
1176 return 0;
1177}
1178
Easwar Hariharan91ab4ed2016-02-03 14:35:57 -08001179void shutdown_led_override(struct hfi1_pportdata *ppd)
1180{
1181 struct hfi1_devdata *dd = ppd->dd;
1182
Easwar Hariharan409b1462016-02-26 13:33:28 -08001183 /*
Easwar Hariharan22434722016-03-07 11:35:03 -08001184 * This pairs with the memory barrier in hfi1_start_led_override to
1185 * ensure that we read the correct state of LED beaconing represented
1186 * by led_override_timer_active
Easwar Hariharan409b1462016-02-26 13:33:28 -08001187 */
Easwar Hariharan22434722016-03-07 11:35:03 -08001188 smp_rmb();
Easwar Hariharan91ab4ed2016-02-03 14:35:57 -08001189 if (atomic_read(&ppd->led_override_timer_active)) {
1190 del_timer_sync(&ppd->led_override_timer);
1191 atomic_set(&ppd->led_override_timer_active, 0);
Easwar Hariharan22434722016-03-07 11:35:03 -08001192 /* Ensure the atomic_set is visible to all CPUs */
1193 smp_wmb();
Easwar Hariharan91ab4ed2016-02-03 14:35:57 -08001194 }
1195
Easwar Hariharan22434722016-03-07 11:35:03 -08001196 /* Hand control of the LED to the DC for normal operation */
1197 write_csr(dd, DCC_CFG_LED_CNTRL, 0);
Easwar Hariharan91ab4ed2016-02-03 14:35:57 -08001198}
Mike Marciniszyn77241052015-07-30 15:17:43 -04001199
1200static void run_led_override(unsigned long opaque)
1201{
1202 struct hfi1_pportdata *ppd = (struct hfi1_pportdata *)opaque;
1203 struct hfi1_devdata *dd = ppd->dd;
Easwar Hariharan91ab4ed2016-02-03 14:35:57 -08001204 unsigned long timeout;
1205 int phase_idx;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001206
1207 if (!(dd->flags & HFI1_INITTED))
1208 return;
1209
Easwar Hariharan91ab4ed2016-02-03 14:35:57 -08001210 phase_idx = ppd->led_override_phase & 1;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001211
Easwar Hariharan91ab4ed2016-02-03 14:35:57 -08001212 setextled(dd, phase_idx);
1213
1214 timeout = ppd->led_override_vals[phase_idx];
Easwar Hariharan22434722016-03-07 11:35:03 -08001215
Easwar Hariharan91ab4ed2016-02-03 14:35:57 -08001216 /* Set up for next phase */
1217 ppd->led_override_phase = !ppd->led_override_phase;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001218
Easwar Hariharan22434722016-03-07 11:35:03 -08001219 mod_timer(&ppd->led_override_timer, jiffies + timeout);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001220}
1221
Easwar Hariharan91ab4ed2016-02-03 14:35:57 -08001222/*
1223 * To have the LED blink in a particular pattern, provide timeon and timeoff
Easwar Hariharan22434722016-03-07 11:35:03 -08001224 * in milliseconds.
1225 * To turn off custom blinking and return to normal operation, use
1226 * shutdown_led_override()
Easwar Hariharan91ab4ed2016-02-03 14:35:57 -08001227 */
Easwar Hariharan22434722016-03-07 11:35:03 -08001228void hfi1_start_led_override(struct hfi1_pportdata *ppd, unsigned int timeon,
1229 unsigned int timeoff)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001230{
Easwar Hariharan22434722016-03-07 11:35:03 -08001231 if (!(ppd->dd->flags & HFI1_INITTED))
Mike Marciniszyn77241052015-07-30 15:17:43 -04001232 return;
1233
Easwar Hariharan91ab4ed2016-02-03 14:35:57 -08001234 /* Convert to jiffies for direct use in timer */
1235 ppd->led_override_vals[0] = msecs_to_jiffies(timeoff);
1236 ppd->led_override_vals[1] = msecs_to_jiffies(timeon);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001237
Easwar Hariharan22434722016-03-07 11:35:03 -08001238 /* Arbitrarily start from LED on phase */
1239 ppd->led_override_phase = 1;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001240
1241 /*
1242 * If the timer has not already been started, do so. Use a "quick"
Easwar Hariharan22434722016-03-07 11:35:03 -08001243 * timeout so the handler will be called soon to look at our request.
Mike Marciniszyn77241052015-07-30 15:17:43 -04001244 */
Easwar Hariharan22434722016-03-07 11:35:03 -08001245 if (!timer_pending(&ppd->led_override_timer)) {
Muhammad Falak R Wania3faf6062015-10-25 16:13:24 +05301246 setup_timer(&ppd->led_override_timer, run_led_override,
Jubin John17fb4f22016-02-14 20:21:52 -08001247 (unsigned long)ppd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001248 ppd->led_override_timer.expires = jiffies + 1;
1249 add_timer(&ppd->led_override_timer);
Easwar Hariharan22434722016-03-07 11:35:03 -08001250 atomic_set(&ppd->led_override_timer_active, 1);
1251 /* Ensure the atomic_set is visible to all CPUs */
1252 smp_wmb();
Mike Marciniszyn77241052015-07-30 15:17:43 -04001253 }
1254}
1255
1256/**
1257 * hfi1_reset_device - reset the chip if possible
1258 * @unit: the device to reset
1259 *
1260 * Whether or not reset is successful, we attempt to re-initialize the chip
1261 * (that is, much like a driver unload/reload). We clear the INITTED flag
1262 * so that the various entry points will fail until we reinitialize. For
1263 * now, we only allow this if no user contexts are open that use chip resources
1264 */
1265int hfi1_reset_device(int unit)
1266{
1267 int ret, i;
1268 struct hfi1_devdata *dd = hfi1_lookup(unit);
1269 struct hfi1_pportdata *ppd;
1270 unsigned long flags;
1271 int pidx;
1272
1273 if (!dd) {
1274 ret = -ENODEV;
1275 goto bail;
1276 }
1277
1278 dd_dev_info(dd, "Reset on unit %u requested\n", unit);
1279
1280 if (!dd->kregbase || !(dd->flags & HFI1_PRESENT)) {
1281 dd_dev_info(dd,
Jubin John17fb4f22016-02-14 20:21:52 -08001282 "Invalid unit number %u or not initialized or not present\n",
1283 unit);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001284 ret = -ENXIO;
1285 goto bail;
1286 }
1287
1288 spin_lock_irqsave(&dd->uctxt_lock, flags);
1289 if (dd->rcd)
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -07001290 for (i = dd->first_dyn_alloc_ctxt;
1291 i < dd->num_rcv_contexts; i++) {
Michael J. Ruhl8737ce92017-05-04 05:15:15 -07001292 if (!dd->rcd[i])
Mike Marciniszyn77241052015-07-30 15:17:43 -04001293 continue;
1294 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
1295 ret = -EBUSY;
1296 goto bail;
1297 }
1298 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
1299
1300 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
1301 ppd = dd->pport + pidx;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001302
Easwar Hariharan91ab4ed2016-02-03 14:35:57 -08001303 shutdown_led_override(ppd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001304 }
1305 if (dd->flags & HFI1_HAS_SEND_DMA)
1306 sdma_exit(dd);
1307
1308 hfi1_reset_cpu_counters(dd);
1309
1310 ret = hfi1_init(dd, 1);
1311
1312 if (ret)
1313 dd_dev_err(dd,
Jubin John17fb4f22016-02-14 20:21:52 -08001314 "Reinitialize unit %u after reset failed with %d\n",
1315 unit, ret);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001316 else
1317 dd_dev_info(dd, "Reinitialized unit %u after resetting\n",
Jubin John17fb4f22016-02-14 20:21:52 -08001318 unit);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001319
1320bail:
1321 return ret;
1322}
1323
1324void handle_eflags(struct hfi1_packet *packet)
1325{
1326 struct hfi1_ctxtdata *rcd = packet->rcd;
1327 u32 rte = rhf_rcv_type_err(packet->rhf);
1328
Mike Marciniszyn77241052015-07-30 15:17:43 -04001329 rcv_hdrerr(rcd, rcd->ppd, packet);
Ignacio Hernandeza03a03e2015-11-06 20:07:03 -05001330 if (rhf_err_flags(packet->rhf))
1331 dd_dev_err(rcd->dd,
1332 "receive context %d: rhf 0x%016llx, errs [ %s%s%s%s%s%s%s%s] rte 0x%x\n",
1333 rcd->ctxt, packet->rhf,
1334 packet->rhf & RHF_K_HDR_LEN_ERR ? "k_hdr_len " : "",
1335 packet->rhf & RHF_DC_UNC_ERR ? "dc_unc " : "",
1336 packet->rhf & RHF_DC_ERR ? "dc " : "",
1337 packet->rhf & RHF_TID_ERR ? "tid " : "",
1338 packet->rhf & RHF_LEN_ERR ? "len " : "",
1339 packet->rhf & RHF_ECC_ERR ? "ecc " : "",
1340 packet->rhf & RHF_VCRC_ERR ? "vcrc " : "",
1341 packet->rhf & RHF_ICRC_ERR ? "icrc " : "",
1342 rte);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001343}
1344
1345/*
1346 * The following functions are called by the interrupt handler. They are type
1347 * specific handlers for each packet type.
1348 */
1349int process_receive_ib(struct hfi1_packet *packet)
1350{
Don Hiatt0181ce32017-03-20 17:26:14 -07001351 if (unlikely(hfi1_dbg_fault_packet(packet)))
1352 return RHF_RCV_CONTINUE;
1353
Mike Marciniszyn77241052015-07-30 15:17:43 -04001354 trace_hfi1_rcvhdr(packet->rcd->ppd->dd,
1355 packet->rcd->ctxt,
1356 rhf_err_flags(packet->rhf),
1357 RHF_RCV_TYPE_IB,
1358 packet->hlen,
1359 packet->tlen,
1360 packet->updegr,
1361 rhf_egr_index(packet->rhf));
1362
Don Hiatt243d9f42017-03-20 17:26:20 -07001363 if (unlikely(
1364 (hfi1_dbg_fault_suppress_err(&packet->rcd->dd->verbs_dev) &&
1365 (packet->rhf & RHF_DC_ERR))))
1366 return RHF_RCV_CONTINUE;
1367
Mike Marciniszyn77241052015-07-30 15:17:43 -04001368 if (unlikely(rhf_err_flags(packet->rhf))) {
1369 handle_eflags(packet);
1370 return RHF_RCV_CONTINUE;
1371 }
1372
1373 hfi1_ib_rcv(packet);
1374 return RHF_RCV_CONTINUE;
1375}
1376
Vishwanathapura, Niranjanad4829ea2017-04-12 20:29:28 -07001377static inline bool hfi1_is_vnic_packet(struct hfi1_packet *packet)
1378{
1379 /* Packet received in VNIC context via RSM */
1380 if (packet->rcd->is_vnic)
1381 return true;
1382
1383 if ((HFI1_GET_L2_TYPE(packet->ebuf) == OPA_VNIC_L2_TYPE) &&
1384 (HFI1_GET_L4_TYPE(packet->ebuf) == OPA_VNIC_L4_ETHR))
1385 return true;
1386
1387 return false;
1388}
1389
Mike Marciniszyn77241052015-07-30 15:17:43 -04001390int process_receive_bypass(struct hfi1_packet *packet)
1391{
Jakub Pawlak505efe32016-10-25 13:12:17 -07001392 struct hfi1_devdata *dd = packet->rcd->dd;
1393
Vishwanathapura, Niranjanad4829ea2017-04-12 20:29:28 -07001394 if (unlikely(rhf_err_flags(packet->rhf))) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001395 handle_eflags(packet);
Vishwanathapura, Niranjanad4829ea2017-04-12 20:29:28 -07001396 } else if (hfi1_is_vnic_packet(packet)) {
1397 hfi1_vnic_bypass_rcv(packet);
1398 return RHF_RCV_CONTINUE;
1399 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001400
Vishwanathapura, Niranjanad4829ea2017-04-12 20:29:28 -07001401 dd_dev_err(dd, "Unsupported bypass packet. Dropping\n");
Jakub Pawlak505efe32016-10-25 13:12:17 -07001402 incr_cntr64(&dd->sw_rcv_bypass_packet_errors);
1403 if (!(dd->err_info_rcvport.status_and_code & OPA_EI_STATUS_SMASK)) {
1404 u64 *flits = packet->ebuf;
1405
1406 if (flits && !(packet->rhf & RHF_LEN_ERR)) {
1407 dd->err_info_rcvport.packet_flit1 = flits[0];
1408 dd->err_info_rcvport.packet_flit2 =
1409 packet->tlen > sizeof(flits[0]) ? flits[1] : 0;
1410 }
1411 dd->err_info_rcvport.status_and_code |=
1412 (OPA_EI_STATUS_SMASK | BAD_L2_ERR);
1413 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001414 return RHF_RCV_CONTINUE;
1415}
1416
1417int process_receive_error(struct hfi1_packet *packet)
1418{
Don Hiatt243d9f42017-03-20 17:26:20 -07001419 /* KHdrHCRCErr -- KDETH packet with a bad HCRC */
1420 if (unlikely(
1421 hfi1_dbg_fault_suppress_err(&packet->rcd->dd->verbs_dev) &&
1422 rhf_rcv_type_err(packet->rhf) == 3))
1423 return RHF_RCV_CONTINUE;
1424
Mike Marciniszyn77241052015-07-30 15:17:43 -04001425 handle_eflags(packet);
1426
1427 if (unlikely(rhf_err_flags(packet->rhf)))
1428 dd_dev_err(packet->rcd->dd,
1429 "Unhandled error packet received. Dropping.\n");
1430
1431 return RHF_RCV_CONTINUE;
1432}
1433
1434int kdeth_process_expected(struct hfi1_packet *packet)
1435{
Don Hiatt0181ce32017-03-20 17:26:14 -07001436 if (unlikely(hfi1_dbg_fault_packet(packet)))
1437 return RHF_RCV_CONTINUE;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001438 if (unlikely(rhf_err_flags(packet->rhf)))
1439 handle_eflags(packet);
1440
1441 dd_dev_err(packet->rcd->dd,
1442 "Unhandled expected packet received. Dropping.\n");
1443 return RHF_RCV_CONTINUE;
1444}
1445
1446int kdeth_process_eager(struct hfi1_packet *packet)
1447{
1448 if (unlikely(rhf_err_flags(packet->rhf)))
1449 handle_eflags(packet);
Don Hiatt0181ce32017-03-20 17:26:14 -07001450 if (unlikely(hfi1_dbg_fault_packet(packet)))
1451 return RHF_RCV_CONTINUE;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001452
1453 dd_dev_err(packet->rcd->dd,
1454 "Unhandled eager packet received. Dropping.\n");
1455 return RHF_RCV_CONTINUE;
1456}
1457
1458int process_receive_invalid(struct hfi1_packet *packet)
1459{
1460 dd_dev_err(packet->rcd->dd, "Invalid packet type %d. Dropping\n",
Jubin John17fb4f22016-02-14 20:21:52 -08001461 rhf_rcv_type(packet->rhf));
Mike Marciniszyn77241052015-07-30 15:17:43 -04001462 return RHF_RCV_CONTINUE;
1463}