blob: 2a1022e374a581c1eb8b0be874da024780524e35 [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
Don Hiatt90397462017-05-12 09:20:20 -0700227static inline void *hfi1_get_header(struct hfi1_devdata *dd,
228 __le32 *rhf_addr)
229{
230 u32 offset = rhf_hdrq_offset(rhf_to_cpu(rhf_addr));
231
232 return (void *)(rhf_addr - dd->rhf_offset + offset);
233}
234
235static inline struct ib_header *hfi1_get_msgheader(struct hfi1_devdata *dd,
236 __le32 *rhf_addr)
237{
238 return (struct ib_header *)hfi1_get_header(dd, rhf_addr);
239}
240
Mike Marciniszyn77241052015-07-30 15:17:43 -0400241/*
242 * Validate and encode the a given RcvArray Buffer size.
243 * The function will check whether the given size falls within
244 * allowed size ranges for the respective type and, optionally,
245 * return the proper encoding.
246 */
Mike Marciniszyna82a7fc2017-02-08 05:26:02 -0800247int hfi1_rcvbuf_validate(u32 size, u8 type, u16 *encoded)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400248{
Amitoj Kaur Chawlaf5389662016-03-04 22:40:17 +0530249 if (unlikely(!PAGE_ALIGNED(size)))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400250 return 0;
251 if (unlikely(size < MIN_EAGER_BUFFER))
252 return 0;
253 if (size >
254 (type == PT_EAGER ? MAX_EAGER_BUFFER : MAX_EXPECTED_BUFFER))
255 return 0;
256 if (encoded)
257 *encoded = ilog2(size / PAGE_SIZE) + 1;
258 return 1;
259}
260
261static void rcv_hdrerr(struct hfi1_ctxtdata *rcd, struct hfi1_pportdata *ppd,
262 struct hfi1_packet *packet)
263{
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700264 struct ib_header *rhdr = packet->hdr;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400265 u32 rte = rhf_rcv_type_err(packet->rhf);
Don Hiatt90397462017-05-12 09:20:20 -0700266 u8 lnh = ib_get_lnh(rhdr);
267 bool has_grh = false;
Sebastian Sanchezf3e862c2017-02-08 05:26:25 -0800268 struct hfi1_ibport *ibp = rcd_to_iport(rcd);
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800269 struct hfi1_devdata *dd = ppd->dd;
270 struct rvt_dev_info *rdi = &dd->verbs_dev.rdi;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400271
272 if (packet->rhf & (RHF_VCRC_ERR | RHF_ICRC_ERR))
273 return;
274
Don Hiatt90397462017-05-12 09:20:20 -0700275 if (lnh == HFI1_LRH_BTH) {
276 packet->ohdr = &rhdr->u.oth;
277 } else if (lnh == HFI1_LRH_GRH) {
278 has_grh = true;
279 packet->ohdr = &rhdr->u.l.oth;
280 packet->grh = &rhdr->u.l.grh;
281 } else {
282 goto drop;
283 }
284
Mike Marciniszyn77241052015-07-30 15:17:43 -0400285 if (packet->rhf & RHF_TID_ERR) {
286 /* For TIDERR and RC QPs preemptively schedule a NAK */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400287 u32 tlen = rhf_pkt_len(packet->rhf); /* in bytes */
Don Hiatt90397462017-05-12 09:20:20 -0700288 u32 dlid = ib_get_dlid(rhdr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400289 u32 qp_num;
Don Hiatt90397462017-05-12 09:20:20 -0700290 u32 mlid_base = be16_to_cpu(IB_MULTICAST_LID_BASE);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400291
292 /* Sanity check packet */
293 if (tlen < 24)
294 goto drop;
295
296 /* Check for GRH */
Don Hiatt90397462017-05-12 09:20:20 -0700297 if (has_grh) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400298 u32 vtf;
Don Hiatt90397462017-05-12 09:20:20 -0700299 struct ib_grh *grh = packet->grh;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400300
Don Hiatt90397462017-05-12 09:20:20 -0700301 if (grh->next_hdr != IB_GRH_NEXT_HDR)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400302 goto drop;
Don Hiatt90397462017-05-12 09:20:20 -0700303 vtf = be32_to_cpu(grh->version_tclass_flow);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400304 if ((vtf >> IB_GRH_VERSION_SHIFT) != IB_GRH_VERSION)
305 goto drop;
Jubin Johne4909742016-02-14 20:22:00 -0800306 }
Don Hiatt90397462017-05-12 09:20:20 -0700307
Mike Marciniszyn77241052015-07-30 15:17:43 -0400308 /* Get the destination QP number. */
Don Hiatt90397462017-05-12 09:20:20 -0700309 qp_num = ib_bth_get_qpn(packet->ohdr);
310 if (dlid < mlid_base) {
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800311 struct rvt_qp *qp;
Dean Luickb77d7132015-10-26 10:28:43 -0400312 unsigned long flags;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400313
314 rcu_read_lock();
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800315 qp = rvt_lookup_qpn(rdi, &ibp->rvp, qp_num);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400316 if (!qp) {
317 rcu_read_unlock();
318 goto drop;
319 }
320
321 /*
322 * Handle only RC QPs - for other QP types drop error
323 * packet.
324 */
Dean Luickb77d7132015-10-26 10:28:43 -0400325 spin_lock_irqsave(&qp->r_lock, flags);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400326
327 /* Check for valid receive state. */
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800328 if (!(ib_rvt_state_ops[qp->state] &
329 RVT_PROCESS_RECV_OK)) {
Dennis Dalessandro4eb06882016-01-19 14:42:39 -0800330 ibp->rvp.n_pkt_drops++;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400331 }
332
333 switch (qp->ibqp.qp_type) {
334 case IB_QPT_RC:
Don Hiatt90397462017-05-12 09:20:20 -0700335 hfi1_rc_hdrerr(rcd, packet, qp);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400336 break;
337 default:
338 /* For now don't handle any other QP types */
339 break;
340 }
341
Dean Luickb77d7132015-10-26 10:28:43 -0400342 spin_unlock_irqrestore(&qp->r_lock, flags);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400343 rcu_read_unlock();
344 } /* Unicast QP */
345 } /* Valid packet with TIDErr */
346
347 /* handle "RcvTypeErr" flags */
348 switch (rte) {
349 case RHF_RTE_ERROR_OP_CODE_ERR:
350 {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400351 void *ebuf = NULL;
Don Hiatt90397462017-05-12 09:20:20 -0700352 u8 opcode;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400353
354 if (rhf_use_egr_bfr(packet->rhf))
355 ebuf = packet->ebuf;
356
Jubin Johnd125a6c2016-02-14 20:19:49 -0800357 if (!ebuf)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400358 goto drop; /* this should never happen */
359
Don Hiatt90397462017-05-12 09:20:20 -0700360 opcode = ib_bth_get_opcode(packet->ohdr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400361 if (opcode == IB_OPCODE_CNP) {
362 /*
363 * Only in pre-B0 h/w is the CNP_OPCODE handled
Edward Mascarenhas80e48982015-12-21 21:57:44 -0500364 * via this code path.
Mike Marciniszyn77241052015-07-30 15:17:43 -0400365 */
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800366 struct rvt_qp *qp = NULL;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400367 u32 lqpn, rqpn;
368 u16 rlid;
369 u8 svc_type, sl, sc5;
370
Dasaratharaman Chandramouliaad559c2017-04-09 10:16:15 -0700371 sc5 = hfi1_9B_get_sc5(rhdr, packet->rhf);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400372 sl = ibp->sc_to_sl[sc5];
373
Don Hiatt90397462017-05-12 09:20:20 -0700374 lqpn = ib_bth_get_qpn(packet->ohdr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400375 rcu_read_lock();
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800376 qp = rvt_lookup_qpn(rdi, &ibp->rvp, lqpn);
Jubin Johnd125a6c2016-02-14 20:19:49 -0800377 if (!qp) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400378 rcu_read_unlock();
379 goto drop;
380 }
381
382 switch (qp->ibqp.qp_type) {
383 case IB_QPT_UD:
384 rlid = 0;
385 rqpn = 0;
386 svc_type = IB_CC_SVCTYPE_UD;
387 break;
388 case IB_QPT_UC:
Don Hiattcb4270572017-04-09 10:16:22 -0700389 rlid = ib_get_slid(rhdr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400390 rqpn = qp->remote_qpn;
391 svc_type = IB_CC_SVCTYPE_UC;
392 break;
393 default:
394 goto drop;
395 }
396
397 process_becn(ppd, sl, rlid, lqpn, rqpn, svc_type);
398 rcu_read_unlock();
399 }
400
401 packet->rhf &= ~RHF_RCV_TYPE_ERR_SMASK;
402 break;
403 }
404 default:
405 break;
406 }
407
408drop:
409 return;
410}
411
412static inline void init_packet(struct hfi1_ctxtdata *rcd,
Jubin John17fb4f22016-02-14 20:21:52 -0800413 struct hfi1_packet *packet)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400414{
Mike Marciniszyn77241052015-07-30 15:17:43 -0400415 packet->rsize = rcd->rcvhdrqentsize; /* words */
416 packet->maxcnt = rcd->rcvhdrq_cnt * packet->rsize; /* words */
417 packet->rcd = rcd;
418 packet->updegr = 0;
419 packet->etail = -1;
Dean Luickf4f30031c2015-10-26 10:28:44 -0400420 packet->rhf_addr = get_rhf_addr(rcd);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400421 packet->rhf = rhf_to_cpu(packet->rhf_addr);
422 packet->rhqoff = rcd->head;
423 packet->numpkt = 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400424}
425
Mitko Haralanov5fd2b562016-07-25 13:38:07 -0700426void hfi1_process_ecn_slowpath(struct rvt_qp *qp, struct hfi1_packet *pkt,
427 bool do_cnp)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400428{
429 struct hfi1_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700430 struct ib_header *hdr = pkt->hdr;
431 struct ib_other_headers *ohdr = pkt->ohdr;
Don Hiatt90397462017-05-12 09:20:20 -0700432 struct ib_grh *grh = pkt->grh;
Mitko Haralanov5fd2b562016-07-25 13:38:07 -0700433 u32 rqpn = 0, bth1;
Don Hiattcb4270572017-04-09 10:16:22 -0700434 u16 rlid, dlid = ib_get_dlid(hdr);
Mitko Haralanov5fd2b562016-07-25 13:38:07 -0700435 u8 sc, svc_type;
436 bool is_mcast = false;
437
Mike Marciniszyn77241052015-07-30 15:17:43 -0400438 switch (qp->ibqp.qp_type) {
Arthur Kepner977940b2015-11-04 21:10:10 -0500439 case IB_QPT_SMI:
440 case IB_QPT_GSI:
Mike Marciniszyn77241052015-07-30 15:17:43 -0400441 case IB_QPT_UD:
Don Hiattcb4270572017-04-09 10:16:22 -0700442 rlid = ib_get_slid(hdr);
Don Hiatt7dafbab2017-05-12 09:19:55 -0700443 rqpn = ib_get_sqpn(ohdr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400444 svc_type = IB_CC_SVCTYPE_UD;
Mitko Haralanov5fd2b562016-07-25 13:38:07 -0700445 is_mcast = (dlid > be16_to_cpu(IB_MULTICAST_LID_BASE)) &&
446 (dlid != be16_to_cpu(IB_LID_PERMISSIVE));
Mike Marciniszyn77241052015-07-30 15:17:43 -0400447 break;
Arthur Kepner977940b2015-11-04 21:10:10 -0500448 case IB_QPT_UC:
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400449 rlid = rdma_ah_get_dlid(&qp->remote_ah_attr);
Arthur Kepner977940b2015-11-04 21:10:10 -0500450 rqpn = qp->remote_qpn;
451 svc_type = IB_CC_SVCTYPE_UC;
452 break;
453 case IB_QPT_RC:
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400454 rlid = rdma_ah_get_dlid(&qp->remote_ah_attr);
Arthur Kepner977940b2015-11-04 21:10:10 -0500455 rqpn = qp->remote_qpn;
456 svc_type = IB_CC_SVCTYPE_RC;
457 break;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400458 default:
459 return;
460 }
461
Dasaratharaman Chandramouliaad559c2017-04-09 10:16:15 -0700462 sc = hfi1_9B_get_sc5(hdr, pkt->rhf);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400463
Mitko Haralanov5fd2b562016-07-25 13:38:07 -0700464 bth1 = be32_to_cpu(ohdr->bth[1]);
Don Hiatt3d591092017-04-09 10:16:28 -0700465 if (do_cnp && (bth1 & IB_FECN_SMASK)) {
Don Hiatt7dafbab2017-05-12 09:19:55 -0700466 u16 pkey = ib_bth_get_pkey(ohdr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400467
Mitko Haralanov5fd2b562016-07-25 13:38:07 -0700468 return_cnp(ibp, qp, rqpn, pkey, dlid, rlid, sc, grh);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400469 }
470
Don Hiatt3d591092017-04-09 10:16:28 -0700471 if (!is_mcast && (bth1 & IB_BECN_SMASK)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400472 struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800473 u32 lqpn = bth1 & RVT_QPN_MASK;
Mitko Haralanov5fd2b562016-07-25 13:38:07 -0700474 u8 sl = ibp->sc_to_sl[sc];
Mike Marciniszyn77241052015-07-30 15:17:43 -0400475
Arthur Kepner977940b2015-11-04 21:10:10 -0500476 process_becn(ppd, sl, rlid, lqpn, rqpn, svc_type);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400477 }
Mitko Haralanov5fd2b562016-07-25 13:38:07 -0700478
Mike Marciniszyn77241052015-07-30 15:17:43 -0400479}
480
481struct ps_mdata {
482 struct hfi1_ctxtdata *rcd;
483 u32 rsize;
484 u32 maxcnt;
485 u32 ps_head;
486 u32 ps_tail;
487 u32 ps_seq;
488};
489
490static inline void init_ps_mdata(struct ps_mdata *mdata,
491 struct hfi1_packet *packet)
492{
493 struct hfi1_ctxtdata *rcd = packet->rcd;
494
495 mdata->rcd = rcd;
496 mdata->rsize = packet->rsize;
497 mdata->maxcnt = packet->maxcnt;
Arthur Kepner3e7ccca2015-11-04 21:10:09 -0500498 mdata->ps_head = packet->rhqoff;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400499
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500500 if (HFI1_CAP_KGET_MASK(rcd->flags, DMA_RTAIL)) {
Arthur Kepner3e7ccca2015-11-04 21:10:09 -0500501 mdata->ps_tail = get_rcvhdrtail(rcd);
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500502 if (rcd->ctxt == HFI1_CTRL_CTXT)
503 mdata->ps_seq = rcd->seq_cnt;
504 else
505 mdata->ps_seq = 0; /* not used with DMA_RTAIL */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400506 } else {
507 mdata->ps_tail = 0; /* used only with DMA_RTAIL*/
508 mdata->ps_seq = rcd->seq_cnt;
509 }
510}
511
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500512static inline int ps_done(struct ps_mdata *mdata, u64 rhf,
513 struct hfi1_ctxtdata *rcd)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400514{
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500515 if (HFI1_CAP_KGET_MASK(rcd->flags, DMA_RTAIL))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400516 return mdata->ps_head == mdata->ps_tail;
517 return mdata->ps_seq != rhf_rcv_seq(rhf);
518}
519
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500520static inline int ps_skip(struct ps_mdata *mdata, u64 rhf,
521 struct hfi1_ctxtdata *rcd)
522{
523 /*
524 * Control context can potentially receive an invalid rhf.
525 * Drop such packets.
526 */
527 if ((rcd->ctxt == HFI1_CTRL_CTXT) && (mdata->ps_head != mdata->ps_tail))
528 return mdata->ps_seq != rhf_rcv_seq(rhf);
529
530 return 0;
531}
532
533static inline void update_ps_mdata(struct ps_mdata *mdata,
534 struct hfi1_ctxtdata *rcd)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400535{
Mike Marciniszyn77241052015-07-30 15:17:43 -0400536 mdata->ps_head += mdata->rsize;
Arthur Kepner3e7ccca2015-11-04 21:10:09 -0500537 if (mdata->ps_head >= mdata->maxcnt)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400538 mdata->ps_head = 0;
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500539
540 /* Control context must do seq counting */
541 if (!HFI1_CAP_KGET_MASK(rcd->flags, DMA_RTAIL) ||
542 (rcd->ctxt == HFI1_CTRL_CTXT)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400543 if (++mdata->ps_seq > 13)
544 mdata->ps_seq = 1;
545 }
546}
547
548/*
549 * prescan_rxq - search through the receive queue looking for packets
550 * containing Excplicit Congestion Notifications (FECNs, or BECNs).
551 * When an ECN is found, process the Congestion Notification, and toggle
552 * it off.
Vennila Megavannan6c9e50f2016-02-03 14:32:57 -0800553 * This is declared as a macro to allow quick checking of the port to avoid
554 * the overhead of a function call if not enabled.
Mike Marciniszyn77241052015-07-30 15:17:43 -0400555 */
Vennila Megavannan6c9e50f2016-02-03 14:32:57 -0800556#define prescan_rxq(rcd, packet) \
557 do { \
558 if (rcd->ppd->cc_prescan) \
559 __prescan_rxq(packet); \
560 } while (0)
561static void __prescan_rxq(struct hfi1_packet *packet)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400562{
563 struct hfi1_ctxtdata *rcd = packet->rcd;
564 struct ps_mdata mdata;
565
Mike Marciniszyn77241052015-07-30 15:17:43 -0400566 init_ps_mdata(&mdata, packet);
567
568 while (1) {
569 struct hfi1_devdata *dd = rcd->dd;
Sebastian Sanchezf3e862c2017-02-08 05:26:25 -0800570 struct hfi1_ibport *ibp = rcd_to_iport(rcd);
Jubin John50e5dcb2016-02-14 20:19:41 -0800571 __le32 *rhf_addr = (__le32 *)rcd->rcvhdrq + mdata.ps_head +
Mike Marciniszyn77241052015-07-30 15:17:43 -0400572 dd->rhf_offset;
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800573 struct rvt_qp *qp;
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700574 struct ib_header *hdr;
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800575 struct rvt_dev_info *rdi = &dd->verbs_dev.rdi;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400576 u64 rhf = rhf_to_cpu(rhf_addr);
Arthur Kepner977940b2015-11-04 21:10:10 -0500577 u32 etype = rhf_rcv_type(rhf), qpn, bth1;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400578 int is_ecn = 0;
579 u8 lnh;
580
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500581 if (ps_done(&mdata, rhf, rcd))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400582 break;
583
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500584 if (ps_skip(&mdata, rhf, rcd))
585 goto next;
586
Mike Marciniszyn77241052015-07-30 15:17:43 -0400587 if (etype != RHF_RCV_TYPE_IB)
588 goto next;
589
Dasaratharaman Chandramoulif2d8a0b2016-10-25 13:12:23 -0700590 packet->hdr = hfi1_get_msgheader(dd, rhf_addr);
591 hdr = packet->hdr;
Don Hiattcb4270572017-04-09 10:16:22 -0700592 lnh = ib_get_lnh(hdr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400593
Dasaratharaman Chandramoulif2d8a0b2016-10-25 13:12:23 -0700594 if (lnh == HFI1_LRH_BTH) {
595 packet->ohdr = &hdr->u.oth;
Don Hiatt90397462017-05-12 09:20:20 -0700596 packet->grh = NULL;
Dasaratharaman Chandramoulif2d8a0b2016-10-25 13:12:23 -0700597 } else if (lnh == HFI1_LRH_GRH) {
598 packet->ohdr = &hdr->u.l.oth;
Don Hiatt90397462017-05-12 09:20:20 -0700599 packet->grh = &hdr->u.l.grh;
Dasaratharaman Chandramoulif2d8a0b2016-10-25 13:12:23 -0700600 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400601 goto next; /* just in case */
Dasaratharaman Chandramoulif2d8a0b2016-10-25 13:12:23 -0700602 }
Mitko Haralanov5fd2b562016-07-25 13:38:07 -0700603
Dasaratharaman Chandramoulif2d8a0b2016-10-25 13:12:23 -0700604 bth1 = be32_to_cpu(packet->ohdr->bth[1]);
Don Hiatt3d591092017-04-09 10:16:28 -0700605 is_ecn = !!(bth1 & (IB_FECN_SMASK | IB_BECN_SMASK));
Mike Marciniszyn77241052015-07-30 15:17:43 -0400606
607 if (!is_ecn)
608 goto next;
609
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800610 qpn = bth1 & RVT_QPN_MASK;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400611 rcu_read_lock();
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800612 qp = rvt_lookup_qpn(rdi, &ibp->rvp, qpn);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400613
Jubin Johnd125a6c2016-02-14 20:19:49 -0800614 if (!qp) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400615 rcu_read_unlock();
616 goto next;
617 }
618
Mitko Haralanov5fd2b562016-07-25 13:38:07 -0700619 process_ecn(qp, packet, true);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400620 rcu_read_unlock();
Arthur Kepner977940b2015-11-04 21:10:10 -0500621
622 /* turn off BECN, FECN */
Don Hiatt3d591092017-04-09 10:16:28 -0700623 bth1 &= ~(IB_FECN_SMASK | IB_BECN_SMASK);
Dasaratharaman Chandramoulif2d8a0b2016-10-25 13:12:23 -0700624 packet->ohdr->bth[1] = cpu_to_be32(bth1);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400625next:
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500626 update_ps_mdata(&mdata, rcd);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400627 }
628}
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500629
Mike Marciniszyna82a7fc2017-02-08 05:26:02 -0800630static void process_rcv_qp_work(struct hfi1_ctxtdata *rcd)
631{
632 struct rvt_qp *qp, *nqp;
633
634 /*
635 * Iterate over all QPs waiting to respond.
636 * The list won't change since the IRQ is only run on one CPU.
637 */
638 list_for_each_entry_safe(qp, nqp, &rcd->qp_wait_list, rspwait) {
639 list_del_init(&qp->rspwait);
640 if (qp->r_flags & RVT_R_RSP_NAK) {
641 qp->r_flags &= ~RVT_R_RSP_NAK;
642 hfi1_send_rc_ack(rcd, qp, 0);
643 }
644 if (qp->r_flags & RVT_R_RSP_SEND) {
645 unsigned long flags;
646
647 qp->r_flags &= ~RVT_R_RSP_SEND;
648 spin_lock_irqsave(&qp->s_lock, flags);
649 if (ib_rvt_state_ops[qp->state] &
650 RVT_PROCESS_OR_FLUSH_SEND)
651 hfi1_schedule_send(qp);
652 spin_unlock_irqrestore(&qp->s_lock, flags);
653 }
654 rvt_put_qp(qp);
655 }
656}
657
658static noinline int max_packet_exceeded(struct hfi1_packet *packet, int thread)
659{
660 if (thread) {
661 if ((packet->numpkt & (MAX_PKT_RECV_THREAD - 1)) == 0)
662 /* allow defered processing */
663 process_rcv_qp_work(packet->rcd);
664 cond_resched();
665 return RCV_PKT_OK;
666 } else {
667 this_cpu_inc(*packet->rcd->dd->rcv_limit);
668 return RCV_PKT_LIMIT;
669 }
670}
671
672static inline int check_max_packet(struct hfi1_packet *packet, int thread)
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500673{
674 int ret = RCV_PKT_OK;
675
Mike Marciniszyna82a7fc2017-02-08 05:26:02 -0800676 if (unlikely((packet->numpkt & (MAX_PKT_RECV - 1)) == 0))
677 ret = max_packet_exceeded(packet, thread);
678 return ret;
679}
680
681static noinline int skip_rcv_packet(struct hfi1_packet *packet, int thread)
682{
683 int ret;
684
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500685 /* Set up for the next packet */
686 packet->rhqoff += packet->rsize;
687 if (packet->rhqoff >= packet->maxcnt)
688 packet->rhqoff = 0;
689
690 packet->numpkt++;
Mike Marciniszyna82a7fc2017-02-08 05:26:02 -0800691 ret = check_max_packet(packet, thread);
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500692
693 packet->rhf_addr = (__le32 *)packet->rcd->rcvhdrq + packet->rhqoff +
694 packet->rcd->dd->rhf_offset;
695 packet->rhf = rhf_to_cpu(packet->rhf_addr);
696
697 return ret;
698}
Mike Marciniszyn77241052015-07-30 15:17:43 -0400699
Dean Luickf4f30031c2015-10-26 10:28:44 -0400700static inline int process_rcv_packet(struct hfi1_packet *packet, int thread)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400701{
Mike Marciniszyna82a7fc2017-02-08 05:26:02 -0800702 int ret;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400703
Mike Marciniszyn77241052015-07-30 15:17:43 -0400704 packet->etype = rhf_rcv_type(packet->rhf);
Don Hiatt90397462017-05-12 09:20:20 -0700705
706 packet->hlen = (u8 *)packet->rhf_addr - (u8 *)packet->hdr;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400707 /* total length */
708 packet->tlen = rhf_pkt_len(packet->rhf); /* in bytes */
709 /* retrieve eager buffer details */
710 packet->ebuf = NULL;
711 if (rhf_use_egr_bfr(packet->rhf)) {
712 packet->etail = rhf_egr_index(packet->rhf);
713 packet->ebuf = get_egrbuf(packet->rcd, packet->rhf,
714 &packet->updegr);
715 /*
716 * Prefetch the contents of the eager buffer. It is
717 * OK to send a negative length to prefetch_range().
718 * The +2 is the size of the RHF.
719 */
720 prefetch_range(packet->ebuf,
Jubin John17fb4f22016-02-14 20:21:52 -0800721 packet->tlen - ((packet->rcd->rcvhdrqentsize -
722 (rhf_hdrq_offset(packet->rhf)
723 + 2)) * 4));
Mike Marciniszyn77241052015-07-30 15:17:43 -0400724 }
725
726 /*
727 * Call a type specific handler for the packet. We
728 * should be able to trust that etype won't be beyond
729 * the range of valid indexes. If so something is really
730 * wrong and we can probably just let things come
731 * crashing down. There is no need to eat another
732 * comparison in this performance critical code.
733 */
734 packet->rcd->dd->rhf_rcv_function_map[packet->etype](packet);
735 packet->numpkt++;
736
737 /* Set up for the next packet */
738 packet->rhqoff += packet->rsize;
739 if (packet->rhqoff >= packet->maxcnt)
740 packet->rhqoff = 0;
741
Mike Marciniszyna82a7fc2017-02-08 05:26:02 -0800742 ret = check_max_packet(packet, thread);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400743
Jubin John50e5dcb2016-02-14 20:19:41 -0800744 packet->rhf_addr = (__le32 *)packet->rcd->rcvhdrq + packet->rhqoff +
Mike Marciniszyn77241052015-07-30 15:17:43 -0400745 packet->rcd->dd->rhf_offset;
746 packet->rhf = rhf_to_cpu(packet->rhf_addr);
747
748 return ret;
749}
750
751static inline void process_rcv_update(int last, struct hfi1_packet *packet)
752{
753 /*
754 * Update head regs etc., every 16 packets, if not last pkt,
755 * to help prevent rcvhdrq overflows, when many packets
756 * are processed and queue is nearly full.
757 * Don't request an interrupt for intermediate updates.
758 */
759 if (!last && !(packet->numpkt & 0xf)) {
760 update_usrhead(packet->rcd, packet->rhqoff, packet->updegr,
761 packet->etail, 0, 0);
762 packet->updegr = 0;
763 }
Don Hiatt90397462017-05-12 09:20:20 -0700764 packet->grh = NULL;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400765}
766
767static inline void finish_packet(struct hfi1_packet *packet)
768{
Mike Marciniszyn77241052015-07-30 15:17:43 -0400769 /*
770 * Nothing we need to free for the packet.
771 *
772 * The only thing we need to do is a final update and call for an
773 * interrupt
774 */
775 update_usrhead(packet->rcd, packet->rcd->head, packet->updegr,
776 packet->etail, rcv_intr_dynamic, packet->numpkt);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400777}
778
Mike Marciniszyn77241052015-07-30 15:17:43 -0400779/*
780 * Handle receive interrupts when using the no dma rtail option.
781 */
Dean Luickf4f30031c2015-10-26 10:28:44 -0400782int handle_receive_interrupt_nodma_rtail(struct hfi1_ctxtdata *rcd, int thread)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400783{
784 u32 seq;
Dean Luickf4f30031c2015-10-26 10:28:44 -0400785 int last = RCV_PKT_OK;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400786 struct hfi1_packet packet;
787
788 init_packet(rcd, &packet);
789 seq = rhf_rcv_seq(packet.rhf);
Dean Luickf4f30031c2015-10-26 10:28:44 -0400790 if (seq != rcd->seq_cnt) {
791 last = RCV_PKT_DONE;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400792 goto bail;
Dean Luickf4f30031c2015-10-26 10:28:44 -0400793 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400794
Vennila Megavannan6c9e50f2016-02-03 14:32:57 -0800795 prescan_rxq(rcd, &packet);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400796
Dean Luickf4f30031c2015-10-26 10:28:44 -0400797 while (last == RCV_PKT_OK) {
798 last = process_rcv_packet(&packet, thread);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400799 seq = rhf_rcv_seq(packet.rhf);
800 if (++rcd->seq_cnt > 13)
801 rcd->seq_cnt = 1;
802 if (seq != rcd->seq_cnt)
Dean Luickf4f30031c2015-10-26 10:28:44 -0400803 last = RCV_PKT_DONE;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400804 process_rcv_update(last, &packet);
805 }
Mike Marciniszyna82a7fc2017-02-08 05:26:02 -0800806 process_rcv_qp_work(rcd);
807 rcd->head = packet.rhqoff;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400808bail:
809 finish_packet(&packet);
Dean Luickf4f30031c2015-10-26 10:28:44 -0400810 return last;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400811}
812
Dean Luickf4f30031c2015-10-26 10:28:44 -0400813int handle_receive_interrupt_dma_rtail(struct hfi1_ctxtdata *rcd, int thread)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400814{
815 u32 hdrqtail;
Dean Luickf4f30031c2015-10-26 10:28:44 -0400816 int last = RCV_PKT_OK;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400817 struct hfi1_packet packet;
818
819 init_packet(rcd, &packet);
820 hdrqtail = get_rcvhdrtail(rcd);
Dean Luickf4f30031c2015-10-26 10:28:44 -0400821 if (packet.rhqoff == hdrqtail) {
822 last = RCV_PKT_DONE;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400823 goto bail;
Dean Luickf4f30031c2015-10-26 10:28:44 -0400824 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400825 smp_rmb(); /* prevent speculative reads of dma'ed hdrq */
826
Vennila Megavannan6c9e50f2016-02-03 14:32:57 -0800827 prescan_rxq(rcd, &packet);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400828
Dean Luickf4f30031c2015-10-26 10:28:44 -0400829 while (last == RCV_PKT_OK) {
830 last = process_rcv_packet(&packet, thread);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400831 if (packet.rhqoff == hdrqtail)
Dean Luickf4f30031c2015-10-26 10:28:44 -0400832 last = RCV_PKT_DONE;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400833 process_rcv_update(last, &packet);
834 }
Mike Marciniszyna82a7fc2017-02-08 05:26:02 -0800835 process_rcv_qp_work(rcd);
836 rcd->head = packet.rhqoff;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400837bail:
838 finish_packet(&packet);
Dean Luickf4f30031c2015-10-26 10:28:44 -0400839 return last;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400840}
841
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700842static inline void set_nodma_rtail(struct hfi1_devdata *dd, u8 ctxt)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400843{
844 int i;
845
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700846 /*
847 * For dynamically allocated kernel contexts (like vnic) switch
848 * interrupt handler only for that context. Otherwise, switch
849 * interrupt handler for all statically allocated kernel contexts.
850 */
851 if (ctxt >= dd->first_dyn_alloc_ctxt) {
852 dd->rcd[ctxt]->do_interrupt =
853 &handle_receive_interrupt_nodma_rtail;
854 return;
855 }
856
857 for (i = HFI1_CTRL_CTXT + 1; i < dd->first_dyn_alloc_ctxt; i++)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400858 dd->rcd[i]->do_interrupt =
859 &handle_receive_interrupt_nodma_rtail;
860}
861
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700862static inline void set_dma_rtail(struct hfi1_devdata *dd, u8 ctxt)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400863{
864 int i;
865
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700866 /*
867 * For dynamically allocated kernel contexts (like vnic) switch
868 * interrupt handler only for that context. Otherwise, switch
869 * interrupt handler for all statically allocated kernel contexts.
870 */
871 if (ctxt >= dd->first_dyn_alloc_ctxt) {
872 dd->rcd[ctxt]->do_interrupt =
873 &handle_receive_interrupt_dma_rtail;
874 return;
875 }
876
877 for (i = HFI1_CTRL_CTXT + 1; i < dd->first_dyn_alloc_ctxt; i++)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400878 dd->rcd[i]->do_interrupt =
879 &handle_receive_interrupt_dma_rtail;
880}
881
Jim Snowfb9036d2016-01-11 18:32:21 -0500882void set_all_slowpath(struct hfi1_devdata *dd)
883{
884 int i;
885
886 /* HFI1_CTRL_CTXT must always use the slow path interrupt handler */
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700887 for (i = HFI1_CTRL_CTXT + 1; i < dd->num_rcv_contexts; i++) {
888 struct hfi1_ctxtdata *rcd = dd->rcd[i];
889
890 if ((i < dd->first_dyn_alloc_ctxt) ||
891 (rcd && rcd->sc && (rcd->sc->type == SC_KERNEL)))
892 rcd->do_interrupt = &handle_receive_interrupt;
893 }
Jim Snowfb9036d2016-01-11 18:32:21 -0500894}
895
896static inline int set_armed_to_active(struct hfi1_ctxtdata *rcd,
Mike Marciniszync867caa2016-08-09 11:19:55 -0400897 struct hfi1_packet *packet,
Jim Snowfb9036d2016-01-11 18:32:21 -0500898 struct hfi1_devdata *dd)
899{
900 struct work_struct *lsaw = &rcd->ppd->linkstate_active_work;
Mike Marciniszyn69b9f4a2016-08-09 11:19:56 -0400901 u8 etype = rhf_rcv_type(packet->rhf);
Don Hiatt90397462017-05-12 09:20:20 -0700902 u8 sc = SC15_PACKET;
Jim Snowfb9036d2016-01-11 18:32:21 -0500903
Don Hiatt90397462017-05-12 09:20:20 -0700904 if (etype == RHF_RCV_TYPE_IB) {
905 struct ib_header *hdr = hfi1_get_msgheader(packet->rcd->dd,
906 packet->rhf_addr);
907 sc = hfi1_9B_get_sc5(hdr, packet->rhf);
908 }
909 if (sc != SC15_PACKET) {
Jim Snowfb9036d2016-01-11 18:32:21 -0500910 int hwstate = read_logical_state(dd);
911
912 if (hwstate != LSTATE_ACTIVE) {
913 dd_dev_info(dd, "Unexpected link state %d\n", hwstate);
914 return 0;
915 }
916
917 queue_work(rcd->ppd->hfi1_wq, lsaw);
918 return 1;
919 }
920 return 0;
921}
922
Mike Marciniszyn77241052015-07-30 15:17:43 -0400923/*
924 * handle_receive_interrupt - receive a packet
925 * @rcd: the context
926 *
927 * Called from interrupt handler for errors or receive interrupt.
928 * This is the slow path interrupt handler.
929 */
Dean Luickf4f30031c2015-10-26 10:28:44 -0400930int handle_receive_interrupt(struct hfi1_ctxtdata *rcd, int thread)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400931{
Mike Marciniszyn77241052015-07-30 15:17:43 -0400932 struct hfi1_devdata *dd = rcd->dd;
933 u32 hdrqtail;
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500934 int needset, last = RCV_PKT_OK;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400935 struct hfi1_packet packet;
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500936 int skip_pkt = 0;
937
938 /* Control context will always use the slow path interrupt handler */
939 needset = (rcd->ctxt == HFI1_CTRL_CTXT) ? 0 : 1;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400940
941 init_packet(rcd, &packet);
942
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500943 if (!HFI1_CAP_KGET_MASK(rcd->flags, DMA_RTAIL)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400944 u32 seq = rhf_rcv_seq(packet.rhf);
945
Dean Luickf4f30031c2015-10-26 10:28:44 -0400946 if (seq != rcd->seq_cnt) {
947 last = RCV_PKT_DONE;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400948 goto bail;
Dean Luickf4f30031c2015-10-26 10:28:44 -0400949 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400950 hdrqtail = 0;
951 } else {
952 hdrqtail = get_rcvhdrtail(rcd);
Dean Luickf4f30031c2015-10-26 10:28:44 -0400953 if (packet.rhqoff == hdrqtail) {
954 last = RCV_PKT_DONE;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400955 goto bail;
Dean Luickf4f30031c2015-10-26 10:28:44 -0400956 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400957 smp_rmb(); /* prevent speculative reads of dma'ed hdrq */
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500958
959 /*
960 * Control context can potentially receive an invalid
961 * rhf. Drop such packets.
962 */
963 if (rcd->ctxt == HFI1_CTRL_CTXT) {
964 u32 seq = rhf_rcv_seq(packet.rhf);
965
966 if (seq != rcd->seq_cnt)
967 skip_pkt = 1;
968 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400969 }
970
Vennila Megavannan6c9e50f2016-02-03 14:32:57 -0800971 prescan_rxq(rcd, &packet);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400972
Dean Luickf4f30031c2015-10-26 10:28:44 -0400973 while (last == RCV_PKT_OK) {
Jubin John17fb4f22016-02-14 20:21:52 -0800974 if (unlikely(dd->do_drop &&
975 atomic_xchg(&dd->drop_packet, DROP_PACKET_OFF) ==
976 DROP_PACKET_ON)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400977 dd->do_drop = 0;
978
979 /* On to the next packet */
980 packet.rhqoff += packet.rsize;
Jubin John50e5dcb2016-02-14 20:19:41 -0800981 packet.rhf_addr = (__le32 *)rcd->rcvhdrq +
Mike Marciniszyn77241052015-07-30 15:17:43 -0400982 packet.rhqoff +
983 dd->rhf_offset;
984 packet.rhf = rhf_to_cpu(packet.rhf_addr);
985
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500986 } else if (skip_pkt) {
987 last = skip_rcv_packet(&packet, thread);
988 skip_pkt = 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400989 } else {
Jim Snowfb9036d2016-01-11 18:32:21 -0500990 /* Auto activate link on non-SC15 packet receive */
991 if (unlikely(rcd->ppd->host_link_state ==
992 HLS_UP_ARMED) &&
Mike Marciniszync867caa2016-08-09 11:19:55 -0400993 set_armed_to_active(rcd, &packet, dd))
Jim Snowfb9036d2016-01-11 18:32:21 -0500994 goto bail;
Dean Luickf4f30031c2015-10-26 10:28:44 -0400995 last = process_rcv_packet(&packet, thread);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400996 }
997
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500998 if (!HFI1_CAP_KGET_MASK(rcd->flags, DMA_RTAIL)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400999 u32 seq = rhf_rcv_seq(packet.rhf);
1000
1001 if (++rcd->seq_cnt > 13)
1002 rcd->seq_cnt = 1;
1003 if (seq != rcd->seq_cnt)
Dean Luickf4f30031c2015-10-26 10:28:44 -04001004 last = RCV_PKT_DONE;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001005 if (needset) {
Jubin John17fb4f22016-02-14 20:21:52 -08001006 dd_dev_info(dd, "Switching to NO_DMA_RTAIL\n");
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -07001007 set_nodma_rtail(dd, rcd->ctxt);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001008 needset = 0;
1009 }
1010 } else {
1011 if (packet.rhqoff == hdrqtail)
Dean Luickf4f30031c2015-10-26 10:28:44 -04001012 last = RCV_PKT_DONE;
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -05001013 /*
1014 * Control context can potentially receive an invalid
1015 * rhf. Drop such packets.
1016 */
1017 if (rcd->ctxt == HFI1_CTRL_CTXT) {
1018 u32 seq = rhf_rcv_seq(packet.rhf);
1019
1020 if (++rcd->seq_cnt > 13)
1021 rcd->seq_cnt = 1;
1022 if (!last && (seq != rcd->seq_cnt))
1023 skip_pkt = 1;
1024 }
1025
Mike Marciniszyn77241052015-07-30 15:17:43 -04001026 if (needset) {
1027 dd_dev_info(dd,
1028 "Switching to DMA_RTAIL\n");
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -07001029 set_dma_rtail(dd, rcd->ctxt);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001030 needset = 0;
1031 }
1032 }
1033
1034 process_rcv_update(last, &packet);
1035 }
1036
Mike Marciniszyna82a7fc2017-02-08 05:26:02 -08001037 process_rcv_qp_work(rcd);
1038 rcd->head = packet.rhqoff;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001039
1040bail:
1041 /*
1042 * Always write head at end, and setup rcv interrupt, even
1043 * if no packets were processed.
1044 */
1045 finish_packet(&packet);
Dean Luickf4f30031c2015-10-26 10:28:44 -04001046 return last;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001047}
1048
1049/*
Jim Snowfb9036d2016-01-11 18:32:21 -05001050 * We may discover in the interrupt that the hardware link state has
1051 * changed from ARMED to ACTIVE (due to the arrival of a non-SC15 packet),
1052 * and we need to update the driver's notion of the link state. We cannot
1053 * run set_link_state from interrupt context, so we queue this function on
1054 * a workqueue.
1055 *
1056 * We delay the regular interrupt processing until after the state changes
1057 * so that the link will be in the correct state by the time any application
1058 * we wake up attempts to send a reply to any message it received.
1059 * (Subsequent receive interrupts may possibly force the wakeup before we
1060 * update the link state.)
1061 *
1062 * The rcd is freed in hfi1_free_ctxtdata after hfi1_postinit_cleanup invokes
1063 * dd->f_cleanup(dd) to disable the interrupt handler and flush workqueues,
1064 * so we're safe from use-after-free of the rcd.
1065 */
1066void receive_interrupt_work(struct work_struct *work)
1067{
1068 struct hfi1_pportdata *ppd = container_of(work, struct hfi1_pportdata,
1069 linkstate_active_work);
1070 struct hfi1_devdata *dd = ppd->dd;
1071 int i;
1072
1073 /* Received non-SC15 packet implies neighbor_normal */
1074 ppd->neighbor_normal = 1;
1075 set_link_state(ppd, HLS_UP_ACTIVE);
1076
1077 /*
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -07001078 * Interrupt all statically allocated kernel contexts that could
1079 * have had an interrupt during auto activation.
Jim Snowfb9036d2016-01-11 18:32:21 -05001080 */
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -07001081 for (i = HFI1_CTRL_CTXT; i < dd->first_dyn_alloc_ctxt; i++)
Jim Snowfb9036d2016-01-11 18:32:21 -05001082 force_recv_intr(dd->rcd[i]);
1083}
1084
1085/*
Mike Marciniszyn77241052015-07-30 15:17:43 -04001086 * Convert a given MTU size to the on-wire MAD packet enumeration.
1087 * Return -1 if the size is invalid.
1088 */
1089int mtu_to_enum(u32 mtu, int default_if_bad)
1090{
1091 switch (mtu) {
1092 case 0: return OPA_MTU_0;
1093 case 256: return OPA_MTU_256;
1094 case 512: return OPA_MTU_512;
1095 case 1024: return OPA_MTU_1024;
1096 case 2048: return OPA_MTU_2048;
1097 case 4096: return OPA_MTU_4096;
1098 case 8192: return OPA_MTU_8192;
1099 case 10240: return OPA_MTU_10240;
1100 }
1101 return default_if_bad;
1102}
1103
1104u16 enum_to_mtu(int mtu)
1105{
1106 switch (mtu) {
1107 case OPA_MTU_0: return 0;
1108 case OPA_MTU_256: return 256;
1109 case OPA_MTU_512: return 512;
1110 case OPA_MTU_1024: return 1024;
1111 case OPA_MTU_2048: return 2048;
1112 case OPA_MTU_4096: return 4096;
1113 case OPA_MTU_8192: return 8192;
1114 case OPA_MTU_10240: return 10240;
1115 default: return 0xffff;
1116 }
1117}
1118
1119/*
1120 * set_mtu - set the MTU
1121 * @ppd: the per port data
1122 *
1123 * We can handle "any" incoming size, the issue here is whether we
1124 * need to restrict our outgoing size. We do not deal with what happens
1125 * to programs that are already running when the size changes.
1126 */
1127int set_mtu(struct hfi1_pportdata *ppd)
1128{
1129 struct hfi1_devdata *dd = ppd->dd;
1130 int i, drain, ret = 0, is_up = 0;
1131
1132 ppd->ibmtu = 0;
1133 for (i = 0; i < ppd->vls_supported; i++)
1134 if (ppd->ibmtu < dd->vld[i].mtu)
1135 ppd->ibmtu = dd->vld[i].mtu;
1136 ppd->ibmaxlen = ppd->ibmtu + lrh_max_header_bytes(ppd->dd);
1137
1138 mutex_lock(&ppd->hls_lock);
Jubin Johnd0d236e2016-02-14 20:20:15 -08001139 if (ppd->host_link_state == HLS_UP_INIT ||
1140 ppd->host_link_state == HLS_UP_ARMED ||
1141 ppd->host_link_state == HLS_UP_ACTIVE)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001142 is_up = 1;
1143
1144 drain = !is_ax(dd) && is_up;
1145
1146 if (drain)
1147 /*
1148 * MTU is specified per-VL. To ensure that no packet gets
1149 * stuck (due, e.g., to the MTU for the packet's VL being
1150 * reduced), empty the per-VL FIFOs before adjusting MTU.
1151 */
1152 ret = stop_drain_data_vls(dd);
1153
1154 if (ret) {
1155 dd_dev_err(dd, "%s: cannot stop/drain VLs - refusing to change per-VL MTUs\n",
1156 __func__);
1157 goto err;
1158 }
1159
1160 hfi1_set_ib_cfg(ppd, HFI1_IB_CFG_MTU, 0);
1161
1162 if (drain)
1163 open_fill_data_vls(dd); /* reopen all VLs */
1164
1165err:
1166 mutex_unlock(&ppd->hls_lock);
1167
1168 return ret;
1169}
1170
1171int hfi1_set_lid(struct hfi1_pportdata *ppd, u32 lid, u8 lmc)
1172{
1173 struct hfi1_devdata *dd = ppd->dd;
1174
1175 ppd->lid = lid;
1176 ppd->lmc = lmc;
1177 hfi1_set_ib_cfg(ppd, HFI1_IB_CFG_LIDLMC, 0);
1178
Jakub Pawlakcde10af2016-05-12 10:23:35 -07001179 dd_dev_info(dd, "port %u: got a lid: 0x%x\n", ppd->port, lid);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001180
1181 return 0;
1182}
1183
Easwar Hariharan91ab4ed2016-02-03 14:35:57 -08001184void shutdown_led_override(struct hfi1_pportdata *ppd)
1185{
1186 struct hfi1_devdata *dd = ppd->dd;
1187
Easwar Hariharan409b1462016-02-26 13:33:28 -08001188 /*
Easwar Hariharan22434722016-03-07 11:35:03 -08001189 * This pairs with the memory barrier in hfi1_start_led_override to
1190 * ensure that we read the correct state of LED beaconing represented
1191 * by led_override_timer_active
Easwar Hariharan409b1462016-02-26 13:33:28 -08001192 */
Easwar Hariharan22434722016-03-07 11:35:03 -08001193 smp_rmb();
Easwar Hariharan91ab4ed2016-02-03 14:35:57 -08001194 if (atomic_read(&ppd->led_override_timer_active)) {
1195 del_timer_sync(&ppd->led_override_timer);
1196 atomic_set(&ppd->led_override_timer_active, 0);
Easwar Hariharan22434722016-03-07 11:35:03 -08001197 /* Ensure the atomic_set is visible to all CPUs */
1198 smp_wmb();
Easwar Hariharan91ab4ed2016-02-03 14:35:57 -08001199 }
1200
Easwar Hariharan22434722016-03-07 11:35:03 -08001201 /* Hand control of the LED to the DC for normal operation */
1202 write_csr(dd, DCC_CFG_LED_CNTRL, 0);
Easwar Hariharan91ab4ed2016-02-03 14:35:57 -08001203}
Mike Marciniszyn77241052015-07-30 15:17:43 -04001204
1205static void run_led_override(unsigned long opaque)
1206{
1207 struct hfi1_pportdata *ppd = (struct hfi1_pportdata *)opaque;
1208 struct hfi1_devdata *dd = ppd->dd;
Easwar Hariharan91ab4ed2016-02-03 14:35:57 -08001209 unsigned long timeout;
1210 int phase_idx;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001211
1212 if (!(dd->flags & HFI1_INITTED))
1213 return;
1214
Easwar Hariharan91ab4ed2016-02-03 14:35:57 -08001215 phase_idx = ppd->led_override_phase & 1;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001216
Easwar Hariharan91ab4ed2016-02-03 14:35:57 -08001217 setextled(dd, phase_idx);
1218
1219 timeout = ppd->led_override_vals[phase_idx];
Easwar Hariharan22434722016-03-07 11:35:03 -08001220
Easwar Hariharan91ab4ed2016-02-03 14:35:57 -08001221 /* Set up for next phase */
1222 ppd->led_override_phase = !ppd->led_override_phase;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001223
Easwar Hariharan22434722016-03-07 11:35:03 -08001224 mod_timer(&ppd->led_override_timer, jiffies + timeout);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001225}
1226
Easwar Hariharan91ab4ed2016-02-03 14:35:57 -08001227/*
1228 * To have the LED blink in a particular pattern, provide timeon and timeoff
Easwar Hariharan22434722016-03-07 11:35:03 -08001229 * in milliseconds.
1230 * To turn off custom blinking and return to normal operation, use
1231 * shutdown_led_override()
Easwar Hariharan91ab4ed2016-02-03 14:35:57 -08001232 */
Easwar Hariharan22434722016-03-07 11:35:03 -08001233void hfi1_start_led_override(struct hfi1_pportdata *ppd, unsigned int timeon,
1234 unsigned int timeoff)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001235{
Easwar Hariharan22434722016-03-07 11:35:03 -08001236 if (!(ppd->dd->flags & HFI1_INITTED))
Mike Marciniszyn77241052015-07-30 15:17:43 -04001237 return;
1238
Easwar Hariharan91ab4ed2016-02-03 14:35:57 -08001239 /* Convert to jiffies for direct use in timer */
1240 ppd->led_override_vals[0] = msecs_to_jiffies(timeoff);
1241 ppd->led_override_vals[1] = msecs_to_jiffies(timeon);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001242
Easwar Hariharan22434722016-03-07 11:35:03 -08001243 /* Arbitrarily start from LED on phase */
1244 ppd->led_override_phase = 1;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001245
1246 /*
1247 * If the timer has not already been started, do so. Use a "quick"
Easwar Hariharan22434722016-03-07 11:35:03 -08001248 * timeout so the handler will be called soon to look at our request.
Mike Marciniszyn77241052015-07-30 15:17:43 -04001249 */
Easwar Hariharan22434722016-03-07 11:35:03 -08001250 if (!timer_pending(&ppd->led_override_timer)) {
Muhammad Falak R Wania3faf6062015-10-25 16:13:24 +05301251 setup_timer(&ppd->led_override_timer, run_led_override,
Jubin John17fb4f22016-02-14 20:21:52 -08001252 (unsigned long)ppd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001253 ppd->led_override_timer.expires = jiffies + 1;
1254 add_timer(&ppd->led_override_timer);
Easwar Hariharan22434722016-03-07 11:35:03 -08001255 atomic_set(&ppd->led_override_timer_active, 1);
1256 /* Ensure the atomic_set is visible to all CPUs */
1257 smp_wmb();
Mike Marciniszyn77241052015-07-30 15:17:43 -04001258 }
1259}
1260
1261/**
1262 * hfi1_reset_device - reset the chip if possible
1263 * @unit: the device to reset
1264 *
1265 * Whether or not reset is successful, we attempt to re-initialize the chip
1266 * (that is, much like a driver unload/reload). We clear the INITTED flag
1267 * so that the various entry points will fail until we reinitialize. For
1268 * now, we only allow this if no user contexts are open that use chip resources
1269 */
1270int hfi1_reset_device(int unit)
1271{
1272 int ret, i;
1273 struct hfi1_devdata *dd = hfi1_lookup(unit);
1274 struct hfi1_pportdata *ppd;
1275 unsigned long flags;
1276 int pidx;
1277
1278 if (!dd) {
1279 ret = -ENODEV;
1280 goto bail;
1281 }
1282
1283 dd_dev_info(dd, "Reset on unit %u requested\n", unit);
1284
1285 if (!dd->kregbase || !(dd->flags & HFI1_PRESENT)) {
1286 dd_dev_info(dd,
Jubin John17fb4f22016-02-14 20:21:52 -08001287 "Invalid unit number %u or not initialized or not present\n",
1288 unit);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001289 ret = -ENXIO;
1290 goto bail;
1291 }
1292
1293 spin_lock_irqsave(&dd->uctxt_lock, flags);
1294 if (dd->rcd)
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -07001295 for (i = dd->first_dyn_alloc_ctxt;
1296 i < dd->num_rcv_contexts; i++) {
Michael J. Ruhl8737ce92017-05-04 05:15:15 -07001297 if (!dd->rcd[i])
Mike Marciniszyn77241052015-07-30 15:17:43 -04001298 continue;
1299 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
1300 ret = -EBUSY;
1301 goto bail;
1302 }
1303 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
1304
1305 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
1306 ppd = dd->pport + pidx;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001307
Easwar Hariharan91ab4ed2016-02-03 14:35:57 -08001308 shutdown_led_override(ppd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001309 }
1310 if (dd->flags & HFI1_HAS_SEND_DMA)
1311 sdma_exit(dd);
1312
1313 hfi1_reset_cpu_counters(dd);
1314
1315 ret = hfi1_init(dd, 1);
1316
1317 if (ret)
1318 dd_dev_err(dd,
Jubin John17fb4f22016-02-14 20:21:52 -08001319 "Reinitialize unit %u after reset failed with %d\n",
1320 unit, ret);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001321 else
1322 dd_dev_info(dd, "Reinitialized unit %u after resetting\n",
Jubin John17fb4f22016-02-14 20:21:52 -08001323 unit);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001324
1325bail:
1326 return ret;
1327}
1328
Don Hiatt90397462017-05-12 09:20:20 -07001329static inline void hfi1_setup_ib_header(struct hfi1_packet *packet)
1330{
1331 packet->hdr = (struct hfi1_ib_message_header *)
1332 hfi1_get_msgheader(packet->rcd->dd,
1333 packet->rhf_addr);
1334 packet->hlen = (u8 *)packet->rhf_addr - (u8 *)packet->hdr;
1335}
1336
1337static int hfi1_setup_9B_packet(struct hfi1_packet *packet)
1338{
1339 struct hfi1_ibport *ibp = rcd_to_iport(packet->rcd);
1340 struct ib_header *hdr;
1341 u8 lnh;
1342
1343 hfi1_setup_ib_header(packet);
1344 hdr = packet->hdr;
1345
1346 lnh = ib_get_lnh(hdr);
1347 if (lnh == HFI1_LRH_BTH) {
1348 packet->ohdr = &hdr->u.oth;
1349 packet->grh = NULL;
1350 } else if (lnh == HFI1_LRH_GRH) {
1351 u32 vtf;
1352
1353 packet->ohdr = &hdr->u.l.oth;
1354 packet->grh = &hdr->u.l.grh;
1355 if (packet->grh->next_hdr != IB_GRH_NEXT_HDR)
1356 goto drop;
1357 vtf = be32_to_cpu(packet->grh->version_tclass_flow);
1358 if ((vtf >> IB_GRH_VERSION_SHIFT) != IB_GRH_VERSION)
1359 goto drop;
1360 } else {
1361 goto drop;
1362 }
1363
1364 /* Query commonly used fields from packet header */
1365 packet->opcode = ib_bth_get_opcode(packet->ohdr);
1366 packet->slid = ib_get_slid(hdr);
1367 packet->dlid = ib_get_dlid(hdr);
1368 packet->sl = ib_get_sl(hdr);
1369 packet->sc = hfi1_9B_get_sc5(hdr, packet->rhf);
1370 packet->pad = ib_bth_get_pad(packet->ohdr);
1371 packet->extra_byte = 0;
1372 packet->fecn = ib_bth_get_fecn(packet->ohdr);
1373 packet->becn = ib_bth_get_becn(packet->ohdr);
1374
1375 return 0;
1376drop:
1377 ibp->rvp.n_pkt_drops++;
1378 return -EINVAL;
1379}
1380
Mike Marciniszyn77241052015-07-30 15:17:43 -04001381void handle_eflags(struct hfi1_packet *packet)
1382{
1383 struct hfi1_ctxtdata *rcd = packet->rcd;
1384 u32 rte = rhf_rcv_type_err(packet->rhf);
1385
Mike Marciniszyn77241052015-07-30 15:17:43 -04001386 rcv_hdrerr(rcd, rcd->ppd, packet);
Ignacio Hernandeza03a03e2015-11-06 20:07:03 -05001387 if (rhf_err_flags(packet->rhf))
1388 dd_dev_err(rcd->dd,
1389 "receive context %d: rhf 0x%016llx, errs [ %s%s%s%s%s%s%s%s] rte 0x%x\n",
1390 rcd->ctxt, packet->rhf,
1391 packet->rhf & RHF_K_HDR_LEN_ERR ? "k_hdr_len " : "",
1392 packet->rhf & RHF_DC_UNC_ERR ? "dc_unc " : "",
1393 packet->rhf & RHF_DC_ERR ? "dc " : "",
1394 packet->rhf & RHF_TID_ERR ? "tid " : "",
1395 packet->rhf & RHF_LEN_ERR ? "len " : "",
1396 packet->rhf & RHF_ECC_ERR ? "ecc " : "",
1397 packet->rhf & RHF_VCRC_ERR ? "vcrc " : "",
1398 packet->rhf & RHF_ICRC_ERR ? "icrc " : "",
1399 rte);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001400}
1401
1402/*
1403 * The following functions are called by the interrupt handler. They are type
1404 * specific handlers for each packet type.
1405 */
1406int process_receive_ib(struct hfi1_packet *packet)
1407{
Don Hiatt0181ce32017-03-20 17:26:14 -07001408 if (unlikely(hfi1_dbg_fault_packet(packet)))
1409 return RHF_RCV_CONTINUE;
1410
Don Hiatt90397462017-05-12 09:20:20 -07001411 if (hfi1_setup_9B_packet(packet))
1412 return RHF_RCV_CONTINUE;
1413
Mike Marciniszyn77241052015-07-30 15:17:43 -04001414 trace_hfi1_rcvhdr(packet->rcd->ppd->dd,
1415 packet->rcd->ctxt,
1416 rhf_err_flags(packet->rhf),
1417 RHF_RCV_TYPE_IB,
1418 packet->hlen,
1419 packet->tlen,
1420 packet->updegr,
1421 rhf_egr_index(packet->rhf));
1422
Don Hiatt243d9f42017-03-20 17:26:20 -07001423 if (unlikely(
1424 (hfi1_dbg_fault_suppress_err(&packet->rcd->dd->verbs_dev) &&
1425 (packet->rhf & RHF_DC_ERR))))
1426 return RHF_RCV_CONTINUE;
1427
Mike Marciniszyn77241052015-07-30 15:17:43 -04001428 if (unlikely(rhf_err_flags(packet->rhf))) {
1429 handle_eflags(packet);
1430 return RHF_RCV_CONTINUE;
1431 }
1432
1433 hfi1_ib_rcv(packet);
1434 return RHF_RCV_CONTINUE;
1435}
1436
Vishwanathapura, Niranjanad4829ea2017-04-12 20:29:28 -07001437static inline bool hfi1_is_vnic_packet(struct hfi1_packet *packet)
1438{
1439 /* Packet received in VNIC context via RSM */
1440 if (packet->rcd->is_vnic)
1441 return true;
1442
1443 if ((HFI1_GET_L2_TYPE(packet->ebuf) == OPA_VNIC_L2_TYPE) &&
1444 (HFI1_GET_L4_TYPE(packet->ebuf) == OPA_VNIC_L4_ETHR))
1445 return true;
1446
1447 return false;
1448}
1449
Mike Marciniszyn77241052015-07-30 15:17:43 -04001450int process_receive_bypass(struct hfi1_packet *packet)
1451{
Jakub Pawlak505efe32016-10-25 13:12:17 -07001452 struct hfi1_devdata *dd = packet->rcd->dd;
1453
Vishwanathapura, Niranjanad4829ea2017-04-12 20:29:28 -07001454 if (unlikely(rhf_err_flags(packet->rhf))) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001455 handle_eflags(packet);
Vishwanathapura, Niranjanad4829ea2017-04-12 20:29:28 -07001456 } else if (hfi1_is_vnic_packet(packet)) {
1457 hfi1_vnic_bypass_rcv(packet);
1458 return RHF_RCV_CONTINUE;
1459 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001460
Vishwanathapura, Niranjanad4829ea2017-04-12 20:29:28 -07001461 dd_dev_err(dd, "Unsupported bypass packet. Dropping\n");
Jakub Pawlak505efe32016-10-25 13:12:17 -07001462 incr_cntr64(&dd->sw_rcv_bypass_packet_errors);
1463 if (!(dd->err_info_rcvport.status_and_code & OPA_EI_STATUS_SMASK)) {
1464 u64 *flits = packet->ebuf;
1465
1466 if (flits && !(packet->rhf & RHF_LEN_ERR)) {
1467 dd->err_info_rcvport.packet_flit1 = flits[0];
1468 dd->err_info_rcvport.packet_flit2 =
1469 packet->tlen > sizeof(flits[0]) ? flits[1] : 0;
1470 }
1471 dd->err_info_rcvport.status_and_code |=
1472 (OPA_EI_STATUS_SMASK | BAD_L2_ERR);
1473 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001474 return RHF_RCV_CONTINUE;
1475}
1476
1477int process_receive_error(struct hfi1_packet *packet)
1478{
Don Hiatt243d9f42017-03-20 17:26:20 -07001479 /* KHdrHCRCErr -- KDETH packet with a bad HCRC */
1480 if (unlikely(
1481 hfi1_dbg_fault_suppress_err(&packet->rcd->dd->verbs_dev) &&
1482 rhf_rcv_type_err(packet->rhf) == 3))
1483 return RHF_RCV_CONTINUE;
1484
Don Hiatt90397462017-05-12 09:20:20 -07001485 hfi1_setup_ib_header(packet);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001486 handle_eflags(packet);
1487
1488 if (unlikely(rhf_err_flags(packet->rhf)))
1489 dd_dev_err(packet->rcd->dd,
1490 "Unhandled error packet received. Dropping.\n");
1491
1492 return RHF_RCV_CONTINUE;
1493}
1494
1495int kdeth_process_expected(struct hfi1_packet *packet)
1496{
Don Hiatt0181ce32017-03-20 17:26:14 -07001497 if (unlikely(hfi1_dbg_fault_packet(packet)))
1498 return RHF_RCV_CONTINUE;
Don Hiatt90397462017-05-12 09:20:20 -07001499
1500 hfi1_setup_ib_header(packet);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001501 if (unlikely(rhf_err_flags(packet->rhf)))
1502 handle_eflags(packet);
1503
1504 dd_dev_err(packet->rcd->dd,
1505 "Unhandled expected packet received. Dropping.\n");
1506 return RHF_RCV_CONTINUE;
1507}
1508
1509int kdeth_process_eager(struct hfi1_packet *packet)
1510{
Don Hiatt90397462017-05-12 09:20:20 -07001511 hfi1_setup_ib_header(packet);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001512 if (unlikely(rhf_err_flags(packet->rhf)))
1513 handle_eflags(packet);
Don Hiatt0181ce32017-03-20 17:26:14 -07001514 if (unlikely(hfi1_dbg_fault_packet(packet)))
1515 return RHF_RCV_CONTINUE;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001516
1517 dd_dev_err(packet->rcd->dd,
1518 "Unhandled eager packet received. Dropping.\n");
1519 return RHF_RCV_CONTINUE;
1520}
1521
1522int process_receive_invalid(struct hfi1_packet *packet)
1523{
1524 dd_dev_err(packet->rcd->dd, "Invalid packet type %d. Dropping\n",
Jubin John17fb4f22016-02-14 20:21:52 -08001525 rhf_rcv_type(packet->rhf));
Mike Marciniszyn77241052015-07-30 15:17:43 -04001526 return RHF_RCV_CONTINUE;
1527}