blob: 14f2a00c13c261a87df8cc64a0ecae3c40e2f7de [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");
Mike Marciniszyn77241052015-07-30 15:17:43 -040099
100/*
101 * MAX_PKT_RCV is the max # if packets processed per receive interrupt.
102 */
103#define MAX_PKT_RECV 64
Mike Marciniszyna82a7fc2017-02-08 05:26:02 -0800104/*
105 * MAX_PKT_THREAD_RCV is the max # of packets processed before
106 * the qp_wait_list queue is flushed.
107 */
108#define MAX_PKT_RECV_THREAD (MAX_PKT_RECV * 4)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400109#define EGR_HEAD_UPDATE_THRESHOLD 16
110
111struct hfi1_ib_stats hfi1_stats;
112
113static int hfi1_caps_set(const char *val, const struct kernel_param *kp)
114{
115 int ret = 0;
116 unsigned long *cap_mask_ptr = (unsigned long *)kp->arg,
117 cap_mask = *cap_mask_ptr, value, diff,
118 write_mask = ((HFI1_CAP_WRITABLE_MASK << HFI1_CAP_USER_SHIFT) |
119 HFI1_CAP_WRITABLE_MASK);
120
121 ret = kstrtoul(val, 0, &value);
122 if (ret) {
123 pr_warn("Invalid module parameter value for 'cap_mask'\n");
124 goto done;
125 }
126 /* Get the changed bits (except the locked bit) */
127 diff = value ^ (cap_mask & ~HFI1_CAP_LOCKED_SMASK);
128
129 /* Remove any bits that are not allowed to change after driver load */
130 if (HFI1_CAP_LOCKED() && (diff & ~write_mask)) {
131 pr_warn("Ignoring non-writable capability bits %#lx\n",
132 diff & ~write_mask);
133 diff &= write_mask;
134 }
135
136 /* Mask off any reserved bits */
137 diff &= ~HFI1_CAP_RESERVED_MASK;
138 /* Clear any previously set and changing bits */
139 cap_mask &= ~diff;
140 /* Update the bits with the new capability */
141 cap_mask |= (value & diff);
142 /* Check for any kernel/user restrictions */
143 diff = (cap_mask & (HFI1_CAP_MUST_HAVE_KERN << HFI1_CAP_USER_SHIFT)) ^
144 ((cap_mask & HFI1_CAP_MUST_HAVE_KERN) << HFI1_CAP_USER_SHIFT);
145 cap_mask &= ~diff;
146 /* Set the bitmask to the final set */
147 *cap_mask_ptr = cap_mask;
148done:
149 return ret;
150}
151
152static int hfi1_caps_get(char *buffer, const struct kernel_param *kp)
153{
154 unsigned long cap_mask = *(unsigned long *)kp->arg;
155
156 cap_mask &= ~HFI1_CAP_LOCKED_SMASK;
157 cap_mask |= ((cap_mask & HFI1_CAP_K2U) << HFI1_CAP_USER_SHIFT);
158
159 return scnprintf(buffer, PAGE_SIZE, "0x%lx", cap_mask);
160}
161
162const char *get_unit_name(int unit)
163{
164 static char iname[16];
165
Jubin John98050712015-11-16 21:59:27 -0500166 snprintf(iname, sizeof(iname), DRIVER_NAME "_%u", unit);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400167 return iname;
168}
169
Dennis Dalessandro49dbb6c2016-01-19 14:42:06 -0800170const char *get_card_name(struct rvt_dev_info *rdi)
171{
172 struct hfi1_ibdev *ibdev = container_of(rdi, struct hfi1_ibdev, rdi);
173 struct hfi1_devdata *dd = container_of(ibdev,
174 struct hfi1_devdata, verbs_dev);
175 return get_unit_name(dd->unit);
176}
177
178struct pci_dev *get_pci_dev(struct rvt_dev_info *rdi)
179{
180 struct hfi1_ibdev *ibdev = container_of(rdi, struct hfi1_ibdev, rdi);
181 struct hfi1_devdata *dd = container_of(ibdev,
182 struct hfi1_devdata, verbs_dev);
183 return dd->pcidev;
184}
185
Mike Marciniszyn77241052015-07-30 15:17:43 -0400186/*
187 * Return count of units with at least one port ACTIVE.
188 */
189int hfi1_count_active_units(void)
190{
191 struct hfi1_devdata *dd;
192 struct hfi1_pportdata *ppd;
193 unsigned long flags;
194 int pidx, nunits_active = 0;
195
196 spin_lock_irqsave(&hfi1_devs_lock, flags);
197 list_for_each_entry(dd, &hfi1_dev_list, list) {
Mike Marciniszyncb51c5d2017-07-24 07:45:31 -0700198 if (!(dd->flags & HFI1_PRESENT) || !dd->kregbase1)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400199 continue;
200 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
201 ppd = dd->pport + pidx;
202 if (ppd->lid && ppd->linkup) {
203 nunits_active++;
204 break;
205 }
206 }
207 }
208 spin_unlock_irqrestore(&hfi1_devs_lock, flags);
209 return nunits_active;
210}
211
212/*
Mike Marciniszyn77241052015-07-30 15:17:43 -0400213 * Get address of eager buffer from it's index (allocated in chunks, not
214 * contiguous).
215 */
216static inline void *get_egrbuf(const struct hfi1_ctxtdata *rcd, u64 rhf,
217 u8 *update)
218{
219 u32 idx = rhf_egr_index(rhf), offset = rhf_egr_buf_offset(rhf);
220
221 *update |= !(idx & (rcd->egrbufs.threshold - 1)) && !offset;
222 return (void *)(((u64)(rcd->egrbufs.rcvtids[idx].addr)) +
223 (offset * RCV_BUF_BLOCK_SIZE));
224}
225
Don Hiatt90397462017-05-12 09:20:20 -0700226static inline void *hfi1_get_header(struct hfi1_devdata *dd,
227 __le32 *rhf_addr)
228{
229 u32 offset = rhf_hdrq_offset(rhf_to_cpu(rhf_addr));
230
231 return (void *)(rhf_addr - dd->rhf_offset + offset);
232}
233
234static inline struct ib_header *hfi1_get_msgheader(struct hfi1_devdata *dd,
235 __le32 *rhf_addr)
236{
237 return (struct ib_header *)hfi1_get_header(dd, rhf_addr);
238}
239
Mike Marciniszyn77241052015-07-30 15:17:43 -0400240/*
241 * Validate and encode the a given RcvArray Buffer size.
242 * The function will check whether the given size falls within
243 * allowed size ranges for the respective type and, optionally,
244 * return the proper encoding.
245 */
Mike Marciniszyna82a7fc2017-02-08 05:26:02 -0800246int hfi1_rcvbuf_validate(u32 size, u8 type, u16 *encoded)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400247{
Amitoj Kaur Chawlaf5389662016-03-04 22:40:17 +0530248 if (unlikely(!PAGE_ALIGNED(size)))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400249 return 0;
250 if (unlikely(size < MIN_EAGER_BUFFER))
251 return 0;
252 if (size >
253 (type == PT_EAGER ? MAX_EAGER_BUFFER : MAX_EXPECTED_BUFFER))
254 return 0;
255 if (encoded)
256 *encoded = ilog2(size / PAGE_SIZE) + 1;
257 return 1;
258}
259
260static void rcv_hdrerr(struct hfi1_ctxtdata *rcd, struct hfi1_pportdata *ppd,
261 struct hfi1_packet *packet)
262{
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700263 struct ib_header *rhdr = packet->hdr;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400264 u32 rte = rhf_rcv_type_err(packet->rhf);
Don Hiatt90397462017-05-12 09:20:20 -0700265 u8 lnh = ib_get_lnh(rhdr);
266 bool has_grh = false;
Sebastian Sanchezf3e862c2017-02-08 05:26:25 -0800267 struct hfi1_ibport *ibp = rcd_to_iport(rcd);
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800268 struct hfi1_devdata *dd = ppd->dd;
269 struct rvt_dev_info *rdi = &dd->verbs_dev.rdi;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400270
271 if (packet->rhf & (RHF_VCRC_ERR | RHF_ICRC_ERR))
272 return;
273
Don Hiatt90397462017-05-12 09:20:20 -0700274 if (lnh == HFI1_LRH_BTH) {
275 packet->ohdr = &rhdr->u.oth;
276 } else if (lnh == HFI1_LRH_GRH) {
277 has_grh = true;
278 packet->ohdr = &rhdr->u.l.oth;
279 packet->grh = &rhdr->u.l.grh;
280 } else {
281 goto drop;
282 }
283
Mike Marciniszyn77241052015-07-30 15:17:43 -0400284 if (packet->rhf & RHF_TID_ERR) {
285 /* For TIDERR and RC QPs preemptively schedule a NAK */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400286 u32 tlen = rhf_pkt_len(packet->rhf); /* in bytes */
Don Hiatt90397462017-05-12 09:20:20 -0700287 u32 dlid = ib_get_dlid(rhdr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400288 u32 qp_num;
Don Hiatt90397462017-05-12 09:20:20 -0700289 u32 mlid_base = be16_to_cpu(IB_MULTICAST_LID_BASE);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400290
291 /* Sanity check packet */
292 if (tlen < 24)
293 goto drop;
294
295 /* Check for GRH */
Don Hiatt90397462017-05-12 09:20:20 -0700296 if (has_grh) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400297 u32 vtf;
Don Hiatt90397462017-05-12 09:20:20 -0700298 struct ib_grh *grh = packet->grh;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400299
Don Hiatt90397462017-05-12 09:20:20 -0700300 if (grh->next_hdr != IB_GRH_NEXT_HDR)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400301 goto drop;
Don Hiatt90397462017-05-12 09:20:20 -0700302 vtf = be32_to_cpu(grh->version_tclass_flow);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400303 if ((vtf >> IB_GRH_VERSION_SHIFT) != IB_GRH_VERSION)
304 goto drop;
Jubin Johne4909742016-02-14 20:22:00 -0800305 }
Don Hiatt90397462017-05-12 09:20:20 -0700306
Mike Marciniszyn77241052015-07-30 15:17:43 -0400307 /* Get the destination QP number. */
Don Hiatt90397462017-05-12 09:20:20 -0700308 qp_num = ib_bth_get_qpn(packet->ohdr);
309 if (dlid < mlid_base) {
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800310 struct rvt_qp *qp;
Dean Luickb77d7132015-10-26 10:28:43 -0400311 unsigned long flags;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400312
313 rcu_read_lock();
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800314 qp = rvt_lookup_qpn(rdi, &ibp->rvp, qp_num);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400315 if (!qp) {
316 rcu_read_unlock();
317 goto drop;
318 }
319
320 /*
321 * Handle only RC QPs - for other QP types drop error
322 * packet.
323 */
Dean Luickb77d7132015-10-26 10:28:43 -0400324 spin_lock_irqsave(&qp->r_lock, flags);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400325
326 /* Check for valid receive state. */
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800327 if (!(ib_rvt_state_ops[qp->state] &
328 RVT_PROCESS_RECV_OK)) {
Dennis Dalessandro4eb06882016-01-19 14:42:39 -0800329 ibp->rvp.n_pkt_drops++;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400330 }
331
332 switch (qp->ibqp.qp_type) {
333 case IB_QPT_RC:
Don Hiatt90397462017-05-12 09:20:20 -0700334 hfi1_rc_hdrerr(rcd, packet, qp);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400335 break;
336 default:
337 /* For now don't handle any other QP types */
338 break;
339 }
340
Dean Luickb77d7132015-10-26 10:28:43 -0400341 spin_unlock_irqrestore(&qp->r_lock, flags);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400342 rcu_read_unlock();
343 } /* Unicast QP */
344 } /* Valid packet with TIDErr */
345
346 /* handle "RcvTypeErr" flags */
347 switch (rte) {
348 case RHF_RTE_ERROR_OP_CODE_ERR:
349 {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400350 void *ebuf = NULL;
Don Hiatt90397462017-05-12 09:20:20 -0700351 u8 opcode;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400352
353 if (rhf_use_egr_bfr(packet->rhf))
354 ebuf = packet->ebuf;
355
Jubin Johnd125a6c2016-02-14 20:19:49 -0800356 if (!ebuf)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400357 goto drop; /* this should never happen */
358
Don Hiatt90397462017-05-12 09:20:20 -0700359 opcode = ib_bth_get_opcode(packet->ohdr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400360 if (opcode == IB_OPCODE_CNP) {
361 /*
362 * Only in pre-B0 h/w is the CNP_OPCODE handled
Edward Mascarenhas80e48982015-12-21 21:57:44 -0500363 * via this code path.
Mike Marciniszyn77241052015-07-30 15:17:43 -0400364 */
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800365 struct rvt_qp *qp = NULL;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400366 u32 lqpn, rqpn;
367 u16 rlid;
368 u8 svc_type, sl, sc5;
369
Dasaratharaman Chandramouliaad559c2017-04-09 10:16:15 -0700370 sc5 = hfi1_9B_get_sc5(rhdr, packet->rhf);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400371 sl = ibp->sc_to_sl[sc5];
372
Don Hiatt90397462017-05-12 09:20:20 -0700373 lqpn = ib_bth_get_qpn(packet->ohdr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400374 rcu_read_lock();
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800375 qp = rvt_lookup_qpn(rdi, &ibp->rvp, lqpn);
Jubin Johnd125a6c2016-02-14 20:19:49 -0800376 if (!qp) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400377 rcu_read_unlock();
378 goto drop;
379 }
380
381 switch (qp->ibqp.qp_type) {
382 case IB_QPT_UD:
383 rlid = 0;
384 rqpn = 0;
385 svc_type = IB_CC_SVCTYPE_UD;
386 break;
387 case IB_QPT_UC:
Don Hiattcb4270572017-04-09 10:16:22 -0700388 rlid = ib_get_slid(rhdr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400389 rqpn = qp->remote_qpn;
390 svc_type = IB_CC_SVCTYPE_UC;
391 break;
392 default:
393 goto drop;
394 }
395
396 process_becn(ppd, sl, rlid, lqpn, rqpn, svc_type);
397 rcu_read_unlock();
398 }
399
400 packet->rhf &= ~RHF_RCV_TYPE_ERR_SMASK;
401 break;
402 }
403 default:
404 break;
405 }
406
407drop:
408 return;
409}
410
411static inline void init_packet(struct hfi1_ctxtdata *rcd,
Jubin John17fb4f22016-02-14 20:21:52 -0800412 struct hfi1_packet *packet)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400413{
Mike Marciniszyn77241052015-07-30 15:17:43 -0400414 packet->rsize = rcd->rcvhdrqentsize; /* words */
415 packet->maxcnt = rcd->rcvhdrq_cnt * packet->rsize; /* words */
416 packet->rcd = rcd;
417 packet->updegr = 0;
418 packet->etail = -1;
Dean Luickf4f30031c2015-10-26 10:28:44 -0400419 packet->rhf_addr = get_rhf_addr(rcd);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400420 packet->rhf = rhf_to_cpu(packet->rhf_addr);
421 packet->rhqoff = rcd->head;
422 packet->numpkt = 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400423}
424
Mitko Haralanov5fd2b562016-07-25 13:38:07 -0700425void hfi1_process_ecn_slowpath(struct rvt_qp *qp, struct hfi1_packet *pkt,
426 bool do_cnp)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400427{
428 struct hfi1_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700429 struct ib_header *hdr = pkt->hdr;
430 struct ib_other_headers *ohdr = pkt->ohdr;
Don Hiatt90397462017-05-12 09:20:20 -0700431 struct ib_grh *grh = pkt->grh;
Mitko Haralanov5fd2b562016-07-25 13:38:07 -0700432 u32 rqpn = 0, bth1;
Don Hiattcb4270572017-04-09 10:16:22 -0700433 u16 rlid, dlid = ib_get_dlid(hdr);
Mitko Haralanov5fd2b562016-07-25 13:38:07 -0700434 u8 sc, svc_type;
435 bool is_mcast = false;
436
Mike Marciniszyn77241052015-07-30 15:17:43 -0400437 switch (qp->ibqp.qp_type) {
Arthur Kepner977940b2015-11-04 21:10:10 -0500438 case IB_QPT_SMI:
439 case IB_QPT_GSI:
Mike Marciniszyn77241052015-07-30 15:17:43 -0400440 case IB_QPT_UD:
Don Hiattcb4270572017-04-09 10:16:22 -0700441 rlid = ib_get_slid(hdr);
Don Hiatt7dafbab2017-05-12 09:19:55 -0700442 rqpn = ib_get_sqpn(ohdr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400443 svc_type = IB_CC_SVCTYPE_UD;
Mitko Haralanov5fd2b562016-07-25 13:38:07 -0700444 is_mcast = (dlid > be16_to_cpu(IB_MULTICAST_LID_BASE)) &&
445 (dlid != be16_to_cpu(IB_LID_PERMISSIVE));
Mike Marciniszyn77241052015-07-30 15:17:43 -0400446 break;
Arthur Kepner977940b2015-11-04 21:10:10 -0500447 case IB_QPT_UC:
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400448 rlid = rdma_ah_get_dlid(&qp->remote_ah_attr);
Arthur Kepner977940b2015-11-04 21:10:10 -0500449 rqpn = qp->remote_qpn;
450 svc_type = IB_CC_SVCTYPE_UC;
451 break;
452 case IB_QPT_RC:
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400453 rlid = rdma_ah_get_dlid(&qp->remote_ah_attr);
Arthur Kepner977940b2015-11-04 21:10:10 -0500454 rqpn = qp->remote_qpn;
455 svc_type = IB_CC_SVCTYPE_RC;
456 break;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400457 default:
458 return;
459 }
460
Dasaratharaman Chandramouliaad559c2017-04-09 10:16:15 -0700461 sc = hfi1_9B_get_sc5(hdr, pkt->rhf);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400462
Mitko Haralanov5fd2b562016-07-25 13:38:07 -0700463 bth1 = be32_to_cpu(ohdr->bth[1]);
Don Hiatt3d591092017-04-09 10:16:28 -0700464 if (do_cnp && (bth1 & IB_FECN_SMASK)) {
Don Hiatt7dafbab2017-05-12 09:19:55 -0700465 u16 pkey = ib_bth_get_pkey(ohdr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400466
Mitko Haralanov5fd2b562016-07-25 13:38:07 -0700467 return_cnp(ibp, qp, rqpn, pkey, dlid, rlid, sc, grh);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400468 }
469
Don Hiatt3d591092017-04-09 10:16:28 -0700470 if (!is_mcast && (bth1 & IB_BECN_SMASK)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400471 struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800472 u32 lqpn = bth1 & RVT_QPN_MASK;
Mitko Haralanov5fd2b562016-07-25 13:38:07 -0700473 u8 sl = ibp->sc_to_sl[sc];
Mike Marciniszyn77241052015-07-30 15:17:43 -0400474
Arthur Kepner977940b2015-11-04 21:10:10 -0500475 process_becn(ppd, sl, rlid, lqpn, rqpn, svc_type);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400476 }
Mitko Haralanov5fd2b562016-07-25 13:38:07 -0700477
Mike Marciniszyn77241052015-07-30 15:17:43 -0400478}
479
480struct ps_mdata {
481 struct hfi1_ctxtdata *rcd;
482 u32 rsize;
483 u32 maxcnt;
484 u32 ps_head;
485 u32 ps_tail;
486 u32 ps_seq;
487};
488
489static inline void init_ps_mdata(struct ps_mdata *mdata,
490 struct hfi1_packet *packet)
491{
492 struct hfi1_ctxtdata *rcd = packet->rcd;
493
494 mdata->rcd = rcd;
495 mdata->rsize = packet->rsize;
496 mdata->maxcnt = packet->maxcnt;
Arthur Kepner3e7ccca2015-11-04 21:10:09 -0500497 mdata->ps_head = packet->rhqoff;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400498
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500499 if (HFI1_CAP_KGET_MASK(rcd->flags, DMA_RTAIL)) {
Arthur Kepner3e7ccca2015-11-04 21:10:09 -0500500 mdata->ps_tail = get_rcvhdrtail(rcd);
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500501 if (rcd->ctxt == HFI1_CTRL_CTXT)
502 mdata->ps_seq = rcd->seq_cnt;
503 else
504 mdata->ps_seq = 0; /* not used with DMA_RTAIL */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400505 } else {
506 mdata->ps_tail = 0; /* used only with DMA_RTAIL*/
507 mdata->ps_seq = rcd->seq_cnt;
508 }
509}
510
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500511static inline int ps_done(struct ps_mdata *mdata, u64 rhf,
512 struct hfi1_ctxtdata *rcd)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400513{
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500514 if (HFI1_CAP_KGET_MASK(rcd->flags, DMA_RTAIL))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400515 return mdata->ps_head == mdata->ps_tail;
516 return mdata->ps_seq != rhf_rcv_seq(rhf);
517}
518
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500519static inline int ps_skip(struct ps_mdata *mdata, u64 rhf,
520 struct hfi1_ctxtdata *rcd)
521{
522 /*
523 * Control context can potentially receive an invalid rhf.
524 * Drop such packets.
525 */
526 if ((rcd->ctxt == HFI1_CTRL_CTXT) && (mdata->ps_head != mdata->ps_tail))
527 return mdata->ps_seq != rhf_rcv_seq(rhf);
528
529 return 0;
530}
531
532static inline void update_ps_mdata(struct ps_mdata *mdata,
533 struct hfi1_ctxtdata *rcd)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400534{
Mike Marciniszyn77241052015-07-30 15:17:43 -0400535 mdata->ps_head += mdata->rsize;
Arthur Kepner3e7ccca2015-11-04 21:10:09 -0500536 if (mdata->ps_head >= mdata->maxcnt)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400537 mdata->ps_head = 0;
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500538
539 /* Control context must do seq counting */
540 if (!HFI1_CAP_KGET_MASK(rcd->flags, DMA_RTAIL) ||
541 (rcd->ctxt == HFI1_CTRL_CTXT)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400542 if (++mdata->ps_seq > 13)
543 mdata->ps_seq = 1;
544 }
545}
546
547/*
548 * prescan_rxq - search through the receive queue looking for packets
549 * containing Excplicit Congestion Notifications (FECNs, or BECNs).
550 * When an ECN is found, process the Congestion Notification, and toggle
551 * it off.
Vennila Megavannan6c9e50f2016-02-03 14:32:57 -0800552 * This is declared as a macro to allow quick checking of the port to avoid
553 * the overhead of a function call if not enabled.
Mike Marciniszyn77241052015-07-30 15:17:43 -0400554 */
Vennila Megavannan6c9e50f2016-02-03 14:32:57 -0800555#define prescan_rxq(rcd, packet) \
556 do { \
557 if (rcd->ppd->cc_prescan) \
558 __prescan_rxq(packet); \
559 } while (0)
560static void __prescan_rxq(struct hfi1_packet *packet)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400561{
562 struct hfi1_ctxtdata *rcd = packet->rcd;
563 struct ps_mdata mdata;
564
Mike Marciniszyn77241052015-07-30 15:17:43 -0400565 init_ps_mdata(&mdata, packet);
566
567 while (1) {
568 struct hfi1_devdata *dd = rcd->dd;
Sebastian Sanchezf3e862c2017-02-08 05:26:25 -0800569 struct hfi1_ibport *ibp = rcd_to_iport(rcd);
Jubin John50e5dcb2016-02-14 20:19:41 -0800570 __le32 *rhf_addr = (__le32 *)rcd->rcvhdrq + mdata.ps_head +
Mike Marciniszyn77241052015-07-30 15:17:43 -0400571 dd->rhf_offset;
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800572 struct rvt_qp *qp;
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700573 struct ib_header *hdr;
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800574 struct rvt_dev_info *rdi = &dd->verbs_dev.rdi;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400575 u64 rhf = rhf_to_cpu(rhf_addr);
Arthur Kepner977940b2015-11-04 21:10:10 -0500576 u32 etype = rhf_rcv_type(rhf), qpn, bth1;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400577 int is_ecn = 0;
578 u8 lnh;
579
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500580 if (ps_done(&mdata, rhf, rcd))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400581 break;
582
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500583 if (ps_skip(&mdata, rhf, rcd))
584 goto next;
585
Mike Marciniszyn77241052015-07-30 15:17:43 -0400586 if (etype != RHF_RCV_TYPE_IB)
587 goto next;
588
Dasaratharaman Chandramoulif2d8a0b2016-10-25 13:12:23 -0700589 packet->hdr = hfi1_get_msgheader(dd, rhf_addr);
590 hdr = packet->hdr;
Don Hiattcb4270572017-04-09 10:16:22 -0700591 lnh = ib_get_lnh(hdr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400592
Dasaratharaman Chandramoulif2d8a0b2016-10-25 13:12:23 -0700593 if (lnh == HFI1_LRH_BTH) {
594 packet->ohdr = &hdr->u.oth;
Don Hiatt90397462017-05-12 09:20:20 -0700595 packet->grh = NULL;
Dasaratharaman Chandramoulif2d8a0b2016-10-25 13:12:23 -0700596 } else if (lnh == HFI1_LRH_GRH) {
597 packet->ohdr = &hdr->u.l.oth;
Don Hiatt90397462017-05-12 09:20:20 -0700598 packet->grh = &hdr->u.l.grh;
Dasaratharaman Chandramoulif2d8a0b2016-10-25 13:12:23 -0700599 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400600 goto next; /* just in case */
Dasaratharaman Chandramoulif2d8a0b2016-10-25 13:12:23 -0700601 }
Mitko Haralanov5fd2b562016-07-25 13:38:07 -0700602
Dasaratharaman Chandramoulif2d8a0b2016-10-25 13:12:23 -0700603 bth1 = be32_to_cpu(packet->ohdr->bth[1]);
Don Hiatt3d591092017-04-09 10:16:28 -0700604 is_ecn = !!(bth1 & (IB_FECN_SMASK | IB_BECN_SMASK));
Mike Marciniszyn77241052015-07-30 15:17:43 -0400605
606 if (!is_ecn)
607 goto next;
608
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800609 qpn = bth1 & RVT_QPN_MASK;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400610 rcu_read_lock();
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800611 qp = rvt_lookup_qpn(rdi, &ibp->rvp, qpn);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400612
Jubin Johnd125a6c2016-02-14 20:19:49 -0800613 if (!qp) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400614 rcu_read_unlock();
615 goto next;
616 }
617
Mitko Haralanov5fd2b562016-07-25 13:38:07 -0700618 process_ecn(qp, packet, true);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400619 rcu_read_unlock();
Arthur Kepner977940b2015-11-04 21:10:10 -0500620
621 /* turn off BECN, FECN */
Don Hiatt3d591092017-04-09 10:16:28 -0700622 bth1 &= ~(IB_FECN_SMASK | IB_BECN_SMASK);
Dasaratharaman Chandramoulif2d8a0b2016-10-25 13:12:23 -0700623 packet->ohdr->bth[1] = cpu_to_be32(bth1);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400624next:
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500625 update_ps_mdata(&mdata, rcd);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400626 }
627}
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500628
Mike Marciniszyna82a7fc2017-02-08 05:26:02 -0800629static void process_rcv_qp_work(struct hfi1_ctxtdata *rcd)
630{
631 struct rvt_qp *qp, *nqp;
632
633 /*
634 * Iterate over all QPs waiting to respond.
635 * The list won't change since the IRQ is only run on one CPU.
636 */
637 list_for_each_entry_safe(qp, nqp, &rcd->qp_wait_list, rspwait) {
638 list_del_init(&qp->rspwait);
639 if (qp->r_flags & RVT_R_RSP_NAK) {
640 qp->r_flags &= ~RVT_R_RSP_NAK;
641 hfi1_send_rc_ack(rcd, qp, 0);
642 }
643 if (qp->r_flags & RVT_R_RSP_SEND) {
644 unsigned long flags;
645
646 qp->r_flags &= ~RVT_R_RSP_SEND;
647 spin_lock_irqsave(&qp->s_lock, flags);
648 if (ib_rvt_state_ops[qp->state] &
649 RVT_PROCESS_OR_FLUSH_SEND)
650 hfi1_schedule_send(qp);
651 spin_unlock_irqrestore(&qp->s_lock, flags);
652 }
653 rvt_put_qp(qp);
654 }
655}
656
657static noinline int max_packet_exceeded(struct hfi1_packet *packet, int thread)
658{
659 if (thread) {
660 if ((packet->numpkt & (MAX_PKT_RECV_THREAD - 1)) == 0)
661 /* allow defered processing */
662 process_rcv_qp_work(packet->rcd);
663 cond_resched();
664 return RCV_PKT_OK;
665 } else {
666 this_cpu_inc(*packet->rcd->dd->rcv_limit);
667 return RCV_PKT_LIMIT;
668 }
669}
670
671static inline int check_max_packet(struct hfi1_packet *packet, int thread)
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500672{
673 int ret = RCV_PKT_OK;
674
Mike Marciniszyna82a7fc2017-02-08 05:26:02 -0800675 if (unlikely((packet->numpkt & (MAX_PKT_RECV - 1)) == 0))
676 ret = max_packet_exceeded(packet, thread);
677 return ret;
678}
679
680static noinline int skip_rcv_packet(struct hfi1_packet *packet, int thread)
681{
682 int ret;
683
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500684 /* Set up for the next packet */
685 packet->rhqoff += packet->rsize;
686 if (packet->rhqoff >= packet->maxcnt)
687 packet->rhqoff = 0;
688
689 packet->numpkt++;
Mike Marciniszyna82a7fc2017-02-08 05:26:02 -0800690 ret = check_max_packet(packet, thread);
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500691
692 packet->rhf_addr = (__le32 *)packet->rcd->rcvhdrq + packet->rhqoff +
693 packet->rcd->dd->rhf_offset;
694 packet->rhf = rhf_to_cpu(packet->rhf_addr);
695
696 return ret;
697}
Mike Marciniszyn77241052015-07-30 15:17:43 -0400698
Dean Luickf4f30031c2015-10-26 10:28:44 -0400699static inline int process_rcv_packet(struct hfi1_packet *packet, int thread)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400700{
Mike Marciniszyna82a7fc2017-02-08 05:26:02 -0800701 int ret;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400702
Mike Marciniszyn77241052015-07-30 15:17:43 -0400703 packet->etype = rhf_rcv_type(packet->rhf);
Don Hiatt90397462017-05-12 09:20:20 -0700704
Mike Marciniszyn77241052015-07-30 15:17:43 -0400705 /* 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 }
Don Hiatt90397462017-05-12 09:20:20 -0700762 packet->grh = NULL;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400763}
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
Michael J. Ruhle6f76222017-07-24 07:45:55 -0700840static inline void set_nodma_rtail(struct hfi1_devdata *dd, u16 ctxt)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400841{
Michael J. Ruhld295dbe2017-08-04 13:52:44 -0700842 struct hfi1_ctxtdata *rcd;
Michael J. Ruhle6f76222017-07-24 07:45:55 -0700843 u16 i;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400844
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700845 /*
846 * For dynamically allocated kernel contexts (like vnic) switch
847 * interrupt handler only for that context. Otherwise, switch
848 * interrupt handler for all statically allocated kernel contexts.
849 */
850 if (ctxt >= dd->first_dyn_alloc_ctxt) {
Michael J. Ruhld295dbe2017-08-04 13:52:44 -0700851 rcd = hfi1_rcd_get_by_index(dd, ctxt);
852 if (rcd) {
853 rcd->do_interrupt =
854 &handle_receive_interrupt_nodma_rtail;
855 hfi1_rcd_put(rcd);
856 }
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700857 return;
858 }
859
Michael J. Ruhld295dbe2017-08-04 13:52:44 -0700860 for (i = HFI1_CTRL_CTXT + 1; i < dd->first_dyn_alloc_ctxt; i++) {
861 rcd = hfi1_rcd_get_by_index(dd, i);
862 if (rcd)
863 rcd->do_interrupt =
864 &handle_receive_interrupt_nodma_rtail;
865 hfi1_rcd_put(rcd);
866 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400867}
868
Michael J. Ruhle6f76222017-07-24 07:45:55 -0700869static inline void set_dma_rtail(struct hfi1_devdata *dd, u16 ctxt)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400870{
Michael J. Ruhld295dbe2017-08-04 13:52:44 -0700871 struct hfi1_ctxtdata *rcd;
Michael J. Ruhle6f76222017-07-24 07:45:55 -0700872 u16 i;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400873
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700874 /*
875 * For dynamically allocated kernel contexts (like vnic) switch
876 * interrupt handler only for that context. Otherwise, switch
877 * interrupt handler for all statically allocated kernel contexts.
878 */
879 if (ctxt >= dd->first_dyn_alloc_ctxt) {
Michael J. Ruhld295dbe2017-08-04 13:52:44 -0700880 rcd = hfi1_rcd_get_by_index(dd, ctxt);
881 if (rcd) {
882 rcd->do_interrupt =
883 &handle_receive_interrupt_dma_rtail;
884 hfi1_rcd_put(rcd);
885 }
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700886 return;
887 }
888
Michael J. Ruhld295dbe2017-08-04 13:52:44 -0700889 for (i = HFI1_CTRL_CTXT + 1; i < dd->first_dyn_alloc_ctxt; i++) {
890 rcd = hfi1_rcd_get_by_index(dd, i);
891 if (rcd)
892 rcd->do_interrupt =
893 &handle_receive_interrupt_dma_rtail;
894 hfi1_rcd_put(rcd);
895 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400896}
897
Jim Snowfb9036d2016-01-11 18:32:21 -0500898void set_all_slowpath(struct hfi1_devdata *dd)
899{
Michael J. Ruhld295dbe2017-08-04 13:52:44 -0700900 struct hfi1_ctxtdata *rcd;
Michael J. Ruhle6f76222017-07-24 07:45:55 -0700901 u16 i;
Jim Snowfb9036d2016-01-11 18:32:21 -0500902
903 /* HFI1_CTRL_CTXT must always use the slow path interrupt handler */
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700904 for (i = HFI1_CTRL_CTXT + 1; i < dd->num_rcv_contexts; i++) {
Michael J. Ruhld295dbe2017-08-04 13:52:44 -0700905 rcd = hfi1_rcd_get_by_index(dd, i);
906 if (!rcd)
907 continue;
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700908 if ((i < dd->first_dyn_alloc_ctxt) ||
Michael J. Ruhld295dbe2017-08-04 13:52:44 -0700909 (rcd->sc && (rcd->sc->type == SC_KERNEL))) {
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700910 rcd->do_interrupt = &handle_receive_interrupt;
Michael J. Ruhld295dbe2017-08-04 13:52:44 -0700911 }
912 hfi1_rcd_put(rcd);
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700913 }
Jim Snowfb9036d2016-01-11 18:32:21 -0500914}
915
916static inline int set_armed_to_active(struct hfi1_ctxtdata *rcd,
Mike Marciniszync867caa2016-08-09 11:19:55 -0400917 struct hfi1_packet *packet,
Jim Snowfb9036d2016-01-11 18:32:21 -0500918 struct hfi1_devdata *dd)
919{
920 struct work_struct *lsaw = &rcd->ppd->linkstate_active_work;
Mike Marciniszyn69b9f4a2016-08-09 11:19:56 -0400921 u8 etype = rhf_rcv_type(packet->rhf);
Don Hiatt90397462017-05-12 09:20:20 -0700922 u8 sc = SC15_PACKET;
Jim Snowfb9036d2016-01-11 18:32:21 -0500923
Don Hiatt90397462017-05-12 09:20:20 -0700924 if (etype == RHF_RCV_TYPE_IB) {
925 struct ib_header *hdr = hfi1_get_msgheader(packet->rcd->dd,
926 packet->rhf_addr);
927 sc = hfi1_9B_get_sc5(hdr, packet->rhf);
928 }
929 if (sc != SC15_PACKET) {
Byczkowski, Jakubbec7c792017-05-29 17:21:32 -0700930 int hwstate = driver_lstate(rcd->ppd);
Jim Snowfb9036d2016-01-11 18:32:21 -0500931
Byczkowski, Jakubbec7c792017-05-29 17:21:32 -0700932 if (hwstate != IB_PORT_ACTIVE) {
933 dd_dev_info(dd,
934 "Unexpected link state %s\n",
935 opa_lstate_name(hwstate));
Jim Snowfb9036d2016-01-11 18:32:21 -0500936 return 0;
937 }
938
Sebastian Sanchez71d47002017-07-29 08:43:49 -0700939 queue_work(rcd->ppd->link_wq, lsaw);
Jim Snowfb9036d2016-01-11 18:32:21 -0500940 return 1;
941 }
942 return 0;
943}
944
Mike Marciniszyn77241052015-07-30 15:17:43 -0400945/*
946 * handle_receive_interrupt - receive a packet
947 * @rcd: the context
948 *
949 * Called from interrupt handler for errors or receive interrupt.
950 * This is the slow path interrupt handler.
951 */
Dean Luickf4f30031c2015-10-26 10:28:44 -0400952int handle_receive_interrupt(struct hfi1_ctxtdata *rcd, int thread)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400953{
Mike Marciniszyn77241052015-07-30 15:17:43 -0400954 struct hfi1_devdata *dd = rcd->dd;
955 u32 hdrqtail;
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500956 int needset, last = RCV_PKT_OK;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400957 struct hfi1_packet packet;
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500958 int skip_pkt = 0;
959
960 /* Control context will always use the slow path interrupt handler */
961 needset = (rcd->ctxt == HFI1_CTRL_CTXT) ? 0 : 1;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400962
963 init_packet(rcd, &packet);
964
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500965 if (!HFI1_CAP_KGET_MASK(rcd->flags, DMA_RTAIL)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400966 u32 seq = rhf_rcv_seq(packet.rhf);
967
Dean Luickf4f30031c2015-10-26 10:28:44 -0400968 if (seq != rcd->seq_cnt) {
969 last = RCV_PKT_DONE;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400970 goto bail;
Dean Luickf4f30031c2015-10-26 10:28:44 -0400971 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400972 hdrqtail = 0;
973 } else {
974 hdrqtail = get_rcvhdrtail(rcd);
Dean Luickf4f30031c2015-10-26 10:28:44 -0400975 if (packet.rhqoff == hdrqtail) {
976 last = RCV_PKT_DONE;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400977 goto bail;
Dean Luickf4f30031c2015-10-26 10:28:44 -0400978 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400979 smp_rmb(); /* prevent speculative reads of dma'ed hdrq */
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500980
981 /*
982 * Control context can potentially receive an invalid
983 * rhf. Drop such packets.
984 */
985 if (rcd->ctxt == HFI1_CTRL_CTXT) {
986 u32 seq = rhf_rcv_seq(packet.rhf);
987
988 if (seq != rcd->seq_cnt)
989 skip_pkt = 1;
990 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400991 }
992
Vennila Megavannan6c9e50f2016-02-03 14:32:57 -0800993 prescan_rxq(rcd, &packet);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400994
Dean Luickf4f30031c2015-10-26 10:28:44 -0400995 while (last == RCV_PKT_OK) {
Jubin John17fb4f22016-02-14 20:21:52 -0800996 if (unlikely(dd->do_drop &&
997 atomic_xchg(&dd->drop_packet, DROP_PACKET_OFF) ==
998 DROP_PACKET_ON)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400999 dd->do_drop = 0;
1000
1001 /* On to the next packet */
1002 packet.rhqoff += packet.rsize;
Jubin John50e5dcb2016-02-14 20:19:41 -08001003 packet.rhf_addr = (__le32 *)rcd->rcvhdrq +
Mike Marciniszyn77241052015-07-30 15:17:43 -04001004 packet.rhqoff +
1005 dd->rhf_offset;
1006 packet.rhf = rhf_to_cpu(packet.rhf_addr);
1007
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -05001008 } else if (skip_pkt) {
1009 last = skip_rcv_packet(&packet, thread);
1010 skip_pkt = 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001011 } else {
Jim Snowfb9036d2016-01-11 18:32:21 -05001012 /* Auto activate link on non-SC15 packet receive */
1013 if (unlikely(rcd->ppd->host_link_state ==
1014 HLS_UP_ARMED) &&
Mike Marciniszync867caa2016-08-09 11:19:55 -04001015 set_armed_to_active(rcd, &packet, dd))
Jim Snowfb9036d2016-01-11 18:32:21 -05001016 goto bail;
Dean Luickf4f30031c2015-10-26 10:28:44 -04001017 last = process_rcv_packet(&packet, thread);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001018 }
1019
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -05001020 if (!HFI1_CAP_KGET_MASK(rcd->flags, DMA_RTAIL)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001021 u32 seq = rhf_rcv_seq(packet.rhf);
1022
1023 if (++rcd->seq_cnt > 13)
1024 rcd->seq_cnt = 1;
1025 if (seq != rcd->seq_cnt)
Dean Luickf4f30031c2015-10-26 10:28:44 -04001026 last = RCV_PKT_DONE;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001027 if (needset) {
Jubin John17fb4f22016-02-14 20:21:52 -08001028 dd_dev_info(dd, "Switching to NO_DMA_RTAIL\n");
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -07001029 set_nodma_rtail(dd, rcd->ctxt);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001030 needset = 0;
1031 }
1032 } else {
1033 if (packet.rhqoff == hdrqtail)
Dean Luickf4f30031c2015-10-26 10:28:44 -04001034 last = RCV_PKT_DONE;
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -05001035 /*
1036 * Control context can potentially receive an invalid
1037 * rhf. Drop such packets.
1038 */
1039 if (rcd->ctxt == HFI1_CTRL_CTXT) {
1040 u32 seq = rhf_rcv_seq(packet.rhf);
1041
1042 if (++rcd->seq_cnt > 13)
1043 rcd->seq_cnt = 1;
1044 if (!last && (seq != rcd->seq_cnt))
1045 skip_pkt = 1;
1046 }
1047
Mike Marciniszyn77241052015-07-30 15:17:43 -04001048 if (needset) {
1049 dd_dev_info(dd,
1050 "Switching to DMA_RTAIL\n");
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -07001051 set_dma_rtail(dd, rcd->ctxt);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001052 needset = 0;
1053 }
1054 }
1055
1056 process_rcv_update(last, &packet);
1057 }
1058
Mike Marciniszyna82a7fc2017-02-08 05:26:02 -08001059 process_rcv_qp_work(rcd);
1060 rcd->head = packet.rhqoff;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001061
1062bail:
1063 /*
1064 * Always write head at end, and setup rcv interrupt, even
1065 * if no packets were processed.
1066 */
1067 finish_packet(&packet);
Dean Luickf4f30031c2015-10-26 10:28:44 -04001068 return last;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001069}
1070
1071/*
Jim Snowfb9036d2016-01-11 18:32:21 -05001072 * We may discover in the interrupt that the hardware link state has
1073 * changed from ARMED to ACTIVE (due to the arrival of a non-SC15 packet),
1074 * and we need to update the driver's notion of the link state. We cannot
1075 * run set_link_state from interrupt context, so we queue this function on
1076 * a workqueue.
1077 *
1078 * We delay the regular interrupt processing until after the state changes
1079 * so that the link will be in the correct state by the time any application
1080 * we wake up attempts to send a reply to any message it received.
1081 * (Subsequent receive interrupts may possibly force the wakeup before we
1082 * update the link state.)
1083 *
1084 * The rcd is freed in hfi1_free_ctxtdata after hfi1_postinit_cleanup invokes
1085 * dd->f_cleanup(dd) to disable the interrupt handler and flush workqueues,
1086 * so we're safe from use-after-free of the rcd.
1087 */
1088void receive_interrupt_work(struct work_struct *work)
1089{
1090 struct hfi1_pportdata *ppd = container_of(work, struct hfi1_pportdata,
1091 linkstate_active_work);
1092 struct hfi1_devdata *dd = ppd->dd;
Michael J. Ruhld295dbe2017-08-04 13:52:44 -07001093 struct hfi1_ctxtdata *rcd;
Michael J. Ruhle6f76222017-07-24 07:45:55 -07001094 u16 i;
Jim Snowfb9036d2016-01-11 18:32:21 -05001095
1096 /* Received non-SC15 packet implies neighbor_normal */
1097 ppd->neighbor_normal = 1;
1098 set_link_state(ppd, HLS_UP_ACTIVE);
1099
1100 /*
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -07001101 * Interrupt all statically allocated kernel contexts that could
1102 * have had an interrupt during auto activation.
Jim Snowfb9036d2016-01-11 18:32:21 -05001103 */
Michael J. Ruhld295dbe2017-08-04 13:52:44 -07001104 for (i = HFI1_CTRL_CTXT; i < dd->first_dyn_alloc_ctxt; i++) {
1105 rcd = hfi1_rcd_get_by_index(dd, i);
1106 if (rcd)
1107 force_recv_intr(rcd);
1108 hfi1_rcd_put(rcd);
1109 }
Jim Snowfb9036d2016-01-11 18:32:21 -05001110}
1111
1112/*
Mike Marciniszyn77241052015-07-30 15:17:43 -04001113 * Convert a given MTU size to the on-wire MAD packet enumeration.
1114 * Return -1 if the size is invalid.
1115 */
1116int mtu_to_enum(u32 mtu, int default_if_bad)
1117{
1118 switch (mtu) {
1119 case 0: return OPA_MTU_0;
1120 case 256: return OPA_MTU_256;
1121 case 512: return OPA_MTU_512;
1122 case 1024: return OPA_MTU_1024;
1123 case 2048: return OPA_MTU_2048;
1124 case 4096: return OPA_MTU_4096;
1125 case 8192: return OPA_MTU_8192;
1126 case 10240: return OPA_MTU_10240;
1127 }
1128 return default_if_bad;
1129}
1130
1131u16 enum_to_mtu(int mtu)
1132{
1133 switch (mtu) {
1134 case OPA_MTU_0: return 0;
1135 case OPA_MTU_256: return 256;
1136 case OPA_MTU_512: return 512;
1137 case OPA_MTU_1024: return 1024;
1138 case OPA_MTU_2048: return 2048;
1139 case OPA_MTU_4096: return 4096;
1140 case OPA_MTU_8192: return 8192;
1141 case OPA_MTU_10240: return 10240;
1142 default: return 0xffff;
1143 }
1144}
1145
1146/*
1147 * set_mtu - set the MTU
1148 * @ppd: the per port data
1149 *
1150 * We can handle "any" incoming size, the issue here is whether we
1151 * need to restrict our outgoing size. We do not deal with what happens
1152 * to programs that are already running when the size changes.
1153 */
1154int set_mtu(struct hfi1_pportdata *ppd)
1155{
1156 struct hfi1_devdata *dd = ppd->dd;
1157 int i, drain, ret = 0, is_up = 0;
1158
1159 ppd->ibmtu = 0;
1160 for (i = 0; i < ppd->vls_supported; i++)
1161 if (ppd->ibmtu < dd->vld[i].mtu)
1162 ppd->ibmtu = dd->vld[i].mtu;
1163 ppd->ibmaxlen = ppd->ibmtu + lrh_max_header_bytes(ppd->dd);
1164
1165 mutex_lock(&ppd->hls_lock);
Jubin Johnd0d236e2016-02-14 20:20:15 -08001166 if (ppd->host_link_state == HLS_UP_INIT ||
1167 ppd->host_link_state == HLS_UP_ARMED ||
1168 ppd->host_link_state == HLS_UP_ACTIVE)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001169 is_up = 1;
1170
1171 drain = !is_ax(dd) && is_up;
1172
1173 if (drain)
1174 /*
1175 * MTU is specified per-VL. To ensure that no packet gets
1176 * stuck (due, e.g., to the MTU for the packet's VL being
1177 * reduced), empty the per-VL FIFOs before adjusting MTU.
1178 */
1179 ret = stop_drain_data_vls(dd);
1180
1181 if (ret) {
1182 dd_dev_err(dd, "%s: cannot stop/drain VLs - refusing to change per-VL MTUs\n",
1183 __func__);
1184 goto err;
1185 }
1186
1187 hfi1_set_ib_cfg(ppd, HFI1_IB_CFG_MTU, 0);
1188
1189 if (drain)
1190 open_fill_data_vls(dd); /* reopen all VLs */
1191
1192err:
1193 mutex_unlock(&ppd->hls_lock);
1194
1195 return ret;
1196}
1197
1198int hfi1_set_lid(struct hfi1_pportdata *ppd, u32 lid, u8 lmc)
1199{
1200 struct hfi1_devdata *dd = ppd->dd;
1201
1202 ppd->lid = lid;
1203 ppd->lmc = lmc;
1204 hfi1_set_ib_cfg(ppd, HFI1_IB_CFG_LIDLMC, 0);
1205
Jakub Pawlakcde10af2016-05-12 10:23:35 -07001206 dd_dev_info(dd, "port %u: got a lid: 0x%x\n", ppd->port, lid);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001207
1208 return 0;
1209}
1210
Easwar Hariharan91ab4ed2016-02-03 14:35:57 -08001211void shutdown_led_override(struct hfi1_pportdata *ppd)
1212{
1213 struct hfi1_devdata *dd = ppd->dd;
1214
Easwar Hariharan409b1462016-02-26 13:33:28 -08001215 /*
Easwar Hariharan22434722016-03-07 11:35:03 -08001216 * This pairs with the memory barrier in hfi1_start_led_override to
1217 * ensure that we read the correct state of LED beaconing represented
1218 * by led_override_timer_active
Easwar Hariharan409b1462016-02-26 13:33:28 -08001219 */
Easwar Hariharan22434722016-03-07 11:35:03 -08001220 smp_rmb();
Easwar Hariharan91ab4ed2016-02-03 14:35:57 -08001221 if (atomic_read(&ppd->led_override_timer_active)) {
1222 del_timer_sync(&ppd->led_override_timer);
1223 atomic_set(&ppd->led_override_timer_active, 0);
Easwar Hariharan22434722016-03-07 11:35:03 -08001224 /* Ensure the atomic_set is visible to all CPUs */
1225 smp_wmb();
Easwar Hariharan91ab4ed2016-02-03 14:35:57 -08001226 }
1227
Easwar Hariharan22434722016-03-07 11:35:03 -08001228 /* Hand control of the LED to the DC for normal operation */
1229 write_csr(dd, DCC_CFG_LED_CNTRL, 0);
Easwar Hariharan91ab4ed2016-02-03 14:35:57 -08001230}
Mike Marciniszyn77241052015-07-30 15:17:43 -04001231
1232static void run_led_override(unsigned long opaque)
1233{
1234 struct hfi1_pportdata *ppd = (struct hfi1_pportdata *)opaque;
1235 struct hfi1_devdata *dd = ppd->dd;
Easwar Hariharan91ab4ed2016-02-03 14:35:57 -08001236 unsigned long timeout;
1237 int phase_idx;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001238
1239 if (!(dd->flags & HFI1_INITTED))
1240 return;
1241
Easwar Hariharan91ab4ed2016-02-03 14:35:57 -08001242 phase_idx = ppd->led_override_phase & 1;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001243
Easwar Hariharan91ab4ed2016-02-03 14:35:57 -08001244 setextled(dd, phase_idx);
1245
1246 timeout = ppd->led_override_vals[phase_idx];
Easwar Hariharan22434722016-03-07 11:35:03 -08001247
Easwar Hariharan91ab4ed2016-02-03 14:35:57 -08001248 /* Set up for next phase */
1249 ppd->led_override_phase = !ppd->led_override_phase;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001250
Easwar Hariharan22434722016-03-07 11:35:03 -08001251 mod_timer(&ppd->led_override_timer, jiffies + timeout);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001252}
1253
Easwar Hariharan91ab4ed2016-02-03 14:35:57 -08001254/*
1255 * To have the LED blink in a particular pattern, provide timeon and timeoff
Easwar Hariharan22434722016-03-07 11:35:03 -08001256 * in milliseconds.
1257 * To turn off custom blinking and return to normal operation, use
1258 * shutdown_led_override()
Easwar Hariharan91ab4ed2016-02-03 14:35:57 -08001259 */
Easwar Hariharan22434722016-03-07 11:35:03 -08001260void hfi1_start_led_override(struct hfi1_pportdata *ppd, unsigned int timeon,
1261 unsigned int timeoff)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001262{
Easwar Hariharan22434722016-03-07 11:35:03 -08001263 if (!(ppd->dd->flags & HFI1_INITTED))
Mike Marciniszyn77241052015-07-30 15:17:43 -04001264 return;
1265
Easwar Hariharan91ab4ed2016-02-03 14:35:57 -08001266 /* Convert to jiffies for direct use in timer */
1267 ppd->led_override_vals[0] = msecs_to_jiffies(timeoff);
1268 ppd->led_override_vals[1] = msecs_to_jiffies(timeon);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001269
Easwar Hariharan22434722016-03-07 11:35:03 -08001270 /* Arbitrarily start from LED on phase */
1271 ppd->led_override_phase = 1;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001272
1273 /*
1274 * If the timer has not already been started, do so. Use a "quick"
Easwar Hariharan22434722016-03-07 11:35:03 -08001275 * timeout so the handler will be called soon to look at our request.
Mike Marciniszyn77241052015-07-30 15:17:43 -04001276 */
Easwar Hariharan22434722016-03-07 11:35:03 -08001277 if (!timer_pending(&ppd->led_override_timer)) {
Muhammad Falak R Wania3faf6062015-10-25 16:13:24 +05301278 setup_timer(&ppd->led_override_timer, run_led_override,
Jubin John17fb4f22016-02-14 20:21:52 -08001279 (unsigned long)ppd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001280 ppd->led_override_timer.expires = jiffies + 1;
1281 add_timer(&ppd->led_override_timer);
Easwar Hariharan22434722016-03-07 11:35:03 -08001282 atomic_set(&ppd->led_override_timer_active, 1);
1283 /* Ensure the atomic_set is visible to all CPUs */
1284 smp_wmb();
Mike Marciniszyn77241052015-07-30 15:17:43 -04001285 }
1286}
1287
1288/**
1289 * hfi1_reset_device - reset the chip if possible
1290 * @unit: the device to reset
1291 *
1292 * Whether or not reset is successful, we attempt to re-initialize the chip
1293 * (that is, much like a driver unload/reload). We clear the INITTED flag
1294 * so that the various entry points will fail until we reinitialize. For
1295 * now, we only allow this if no user contexts are open that use chip resources
1296 */
1297int hfi1_reset_device(int unit)
1298{
Michael J. Ruhle6f76222017-07-24 07:45:55 -07001299 int ret;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001300 struct hfi1_devdata *dd = hfi1_lookup(unit);
1301 struct hfi1_pportdata *ppd;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001302 int pidx;
1303
1304 if (!dd) {
1305 ret = -ENODEV;
1306 goto bail;
1307 }
1308
1309 dd_dev_info(dd, "Reset on unit %u requested\n", unit);
1310
Mike Marciniszyncb51c5d2017-07-24 07:45:31 -07001311 if (!dd->kregbase1 || !(dd->flags & HFI1_PRESENT)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001312 dd_dev_info(dd,
Jubin John17fb4f22016-02-14 20:21:52 -08001313 "Invalid unit number %u or not initialized or not present\n",
1314 unit);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001315 ret = -ENXIO;
1316 goto bail;
1317 }
1318
Michael J. Ruhld295dbe2017-08-04 13:52:44 -07001319 /* If there are any user/vnic contexts, we cannot reset */
1320 mutex_lock(&hfi1_mutex);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001321 if (dd->rcd)
Michael J. Ruhld295dbe2017-08-04 13:52:44 -07001322 if (hfi1_stats.sps_ctxts) {
1323 mutex_unlock(&hfi1_mutex);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001324 ret = -EBUSY;
1325 goto bail;
1326 }
Michael J. Ruhld295dbe2017-08-04 13:52:44 -07001327 mutex_unlock(&hfi1_mutex);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001328
1329 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
1330 ppd = dd->pport + pidx;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001331
Easwar Hariharan91ab4ed2016-02-03 14:35:57 -08001332 shutdown_led_override(ppd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001333 }
1334 if (dd->flags & HFI1_HAS_SEND_DMA)
1335 sdma_exit(dd);
1336
1337 hfi1_reset_cpu_counters(dd);
1338
1339 ret = hfi1_init(dd, 1);
1340
1341 if (ret)
1342 dd_dev_err(dd,
Jubin John17fb4f22016-02-14 20:21:52 -08001343 "Reinitialize unit %u after reset failed with %d\n",
1344 unit, ret);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001345 else
1346 dd_dev_info(dd, "Reinitialized unit %u after resetting\n",
Jubin John17fb4f22016-02-14 20:21:52 -08001347 unit);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001348
1349bail:
1350 return ret;
1351}
1352
Don Hiatt90397462017-05-12 09:20:20 -07001353static inline void hfi1_setup_ib_header(struct hfi1_packet *packet)
1354{
1355 packet->hdr = (struct hfi1_ib_message_header *)
1356 hfi1_get_msgheader(packet->rcd->dd,
1357 packet->rhf_addr);
1358 packet->hlen = (u8 *)packet->rhf_addr - (u8 *)packet->hdr;
1359}
1360
1361static int hfi1_setup_9B_packet(struct hfi1_packet *packet)
1362{
1363 struct hfi1_ibport *ibp = rcd_to_iport(packet->rcd);
1364 struct ib_header *hdr;
1365 u8 lnh;
1366
1367 hfi1_setup_ib_header(packet);
1368 hdr = packet->hdr;
1369
1370 lnh = ib_get_lnh(hdr);
1371 if (lnh == HFI1_LRH_BTH) {
1372 packet->ohdr = &hdr->u.oth;
1373 packet->grh = NULL;
1374 } else if (lnh == HFI1_LRH_GRH) {
1375 u32 vtf;
1376
1377 packet->ohdr = &hdr->u.l.oth;
1378 packet->grh = &hdr->u.l.grh;
1379 if (packet->grh->next_hdr != IB_GRH_NEXT_HDR)
1380 goto drop;
1381 vtf = be32_to_cpu(packet->grh->version_tclass_flow);
1382 if ((vtf >> IB_GRH_VERSION_SHIFT) != IB_GRH_VERSION)
1383 goto drop;
1384 } else {
1385 goto drop;
1386 }
1387
1388 /* Query commonly used fields from packet header */
1389 packet->opcode = ib_bth_get_opcode(packet->ohdr);
1390 packet->slid = ib_get_slid(hdr);
1391 packet->dlid = ib_get_dlid(hdr);
1392 packet->sl = ib_get_sl(hdr);
1393 packet->sc = hfi1_9B_get_sc5(hdr, packet->rhf);
1394 packet->pad = ib_bth_get_pad(packet->ohdr);
1395 packet->extra_byte = 0;
1396 packet->fecn = ib_bth_get_fecn(packet->ohdr);
1397 packet->becn = ib_bth_get_becn(packet->ohdr);
1398
1399 return 0;
1400drop:
1401 ibp->rvp.n_pkt_drops++;
1402 return -EINVAL;
1403}
1404
Mike Marciniszyn77241052015-07-30 15:17:43 -04001405void handle_eflags(struct hfi1_packet *packet)
1406{
1407 struct hfi1_ctxtdata *rcd = packet->rcd;
1408 u32 rte = rhf_rcv_type_err(packet->rhf);
1409
Mike Marciniszyn77241052015-07-30 15:17:43 -04001410 rcv_hdrerr(rcd, rcd->ppd, packet);
Ignacio Hernandeza03a03e2015-11-06 20:07:03 -05001411 if (rhf_err_flags(packet->rhf))
1412 dd_dev_err(rcd->dd,
1413 "receive context %d: rhf 0x%016llx, errs [ %s%s%s%s%s%s%s%s] rte 0x%x\n",
1414 rcd->ctxt, packet->rhf,
1415 packet->rhf & RHF_K_HDR_LEN_ERR ? "k_hdr_len " : "",
1416 packet->rhf & RHF_DC_UNC_ERR ? "dc_unc " : "",
1417 packet->rhf & RHF_DC_ERR ? "dc " : "",
1418 packet->rhf & RHF_TID_ERR ? "tid " : "",
1419 packet->rhf & RHF_LEN_ERR ? "len " : "",
1420 packet->rhf & RHF_ECC_ERR ? "ecc " : "",
1421 packet->rhf & RHF_VCRC_ERR ? "vcrc " : "",
1422 packet->rhf & RHF_ICRC_ERR ? "icrc " : "",
1423 rte);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001424}
1425
1426/*
1427 * The following functions are called by the interrupt handler. They are type
1428 * specific handlers for each packet type.
1429 */
1430int process_receive_ib(struct hfi1_packet *packet)
1431{
Don Hiatt0181ce32017-03-20 17:26:14 -07001432 if (unlikely(hfi1_dbg_fault_packet(packet)))
1433 return RHF_RCV_CONTINUE;
1434
Don Hiatt90397462017-05-12 09:20:20 -07001435 if (hfi1_setup_9B_packet(packet))
1436 return RHF_RCV_CONTINUE;
1437
Mike Marciniszyn77241052015-07-30 15:17:43 -04001438 trace_hfi1_rcvhdr(packet->rcd->ppd->dd,
1439 packet->rcd->ctxt,
1440 rhf_err_flags(packet->rhf),
1441 RHF_RCV_TYPE_IB,
1442 packet->hlen,
1443 packet->tlen,
1444 packet->updegr,
1445 rhf_egr_index(packet->rhf));
1446
Don Hiatt243d9f42017-03-20 17:26:20 -07001447 if (unlikely(
1448 (hfi1_dbg_fault_suppress_err(&packet->rcd->dd->verbs_dev) &&
1449 (packet->rhf & RHF_DC_ERR))))
1450 return RHF_RCV_CONTINUE;
1451
Mike Marciniszyn77241052015-07-30 15:17:43 -04001452 if (unlikely(rhf_err_flags(packet->rhf))) {
1453 handle_eflags(packet);
1454 return RHF_RCV_CONTINUE;
1455 }
1456
1457 hfi1_ib_rcv(packet);
1458 return RHF_RCV_CONTINUE;
1459}
1460
Vishwanathapura, Niranjanad4829ea2017-04-12 20:29:28 -07001461static inline bool hfi1_is_vnic_packet(struct hfi1_packet *packet)
1462{
1463 /* Packet received in VNIC context via RSM */
1464 if (packet->rcd->is_vnic)
1465 return true;
1466
1467 if ((HFI1_GET_L2_TYPE(packet->ebuf) == OPA_VNIC_L2_TYPE) &&
1468 (HFI1_GET_L4_TYPE(packet->ebuf) == OPA_VNIC_L4_ETHR))
1469 return true;
1470
1471 return false;
1472}
1473
Mike Marciniszyn77241052015-07-30 15:17:43 -04001474int process_receive_bypass(struct hfi1_packet *packet)
1475{
Jakub Pawlak505efe32016-10-25 13:12:17 -07001476 struct hfi1_devdata *dd = packet->rcd->dd;
1477
Vishwanathapura, Niranjanad4829ea2017-04-12 20:29:28 -07001478 if (unlikely(rhf_err_flags(packet->rhf))) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001479 handle_eflags(packet);
Vishwanathapura, Niranjanad4829ea2017-04-12 20:29:28 -07001480 } else if (hfi1_is_vnic_packet(packet)) {
1481 hfi1_vnic_bypass_rcv(packet);
1482 return RHF_RCV_CONTINUE;
1483 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001484
Vishwanathapura, Niranjanad4829ea2017-04-12 20:29:28 -07001485 dd_dev_err(dd, "Unsupported bypass packet. Dropping\n");
Jakub Pawlak505efe32016-10-25 13:12:17 -07001486 incr_cntr64(&dd->sw_rcv_bypass_packet_errors);
1487 if (!(dd->err_info_rcvport.status_and_code & OPA_EI_STATUS_SMASK)) {
1488 u64 *flits = packet->ebuf;
1489
1490 if (flits && !(packet->rhf & RHF_LEN_ERR)) {
1491 dd->err_info_rcvport.packet_flit1 = flits[0];
1492 dd->err_info_rcvport.packet_flit2 =
1493 packet->tlen > sizeof(flits[0]) ? flits[1] : 0;
1494 }
1495 dd->err_info_rcvport.status_and_code |=
1496 (OPA_EI_STATUS_SMASK | BAD_L2_ERR);
1497 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001498 return RHF_RCV_CONTINUE;
1499}
1500
1501int process_receive_error(struct hfi1_packet *packet)
1502{
Don Hiatt243d9f42017-03-20 17:26:20 -07001503 /* KHdrHCRCErr -- KDETH packet with a bad HCRC */
1504 if (unlikely(
1505 hfi1_dbg_fault_suppress_err(&packet->rcd->dd->verbs_dev) &&
1506 rhf_rcv_type_err(packet->rhf) == 3))
1507 return RHF_RCV_CONTINUE;
1508
Don Hiatt90397462017-05-12 09:20:20 -07001509 hfi1_setup_ib_header(packet);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001510 handle_eflags(packet);
1511
1512 if (unlikely(rhf_err_flags(packet->rhf)))
1513 dd_dev_err(packet->rcd->dd,
1514 "Unhandled error packet received. Dropping.\n");
1515
1516 return RHF_RCV_CONTINUE;
1517}
1518
1519int kdeth_process_expected(struct hfi1_packet *packet)
1520{
Don Hiatt0181ce32017-03-20 17:26:14 -07001521 if (unlikely(hfi1_dbg_fault_packet(packet)))
1522 return RHF_RCV_CONTINUE;
Don Hiatt90397462017-05-12 09:20:20 -07001523
1524 hfi1_setup_ib_header(packet);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001525 if (unlikely(rhf_err_flags(packet->rhf)))
1526 handle_eflags(packet);
1527
1528 dd_dev_err(packet->rcd->dd,
1529 "Unhandled expected packet received. Dropping.\n");
1530 return RHF_RCV_CONTINUE;
1531}
1532
1533int kdeth_process_eager(struct hfi1_packet *packet)
1534{
Don Hiatt90397462017-05-12 09:20:20 -07001535 hfi1_setup_ib_header(packet);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001536 if (unlikely(rhf_err_flags(packet->rhf)))
1537 handle_eflags(packet);
Don Hiatt0181ce32017-03-20 17:26:14 -07001538 if (unlikely(hfi1_dbg_fault_packet(packet)))
1539 return RHF_RCV_CONTINUE;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001540
1541 dd_dev_err(packet->rcd->dd,
1542 "Unhandled eager packet received. Dropping.\n");
1543 return RHF_RCV_CONTINUE;
1544}
1545
1546int process_receive_invalid(struct hfi1_packet *packet)
1547{
1548 dd_dev_err(packet->rcd->dd, "Invalid packet type %d. Dropping\n",
Jubin John17fb4f22016-02-14 20:21:52 -08001549 rhf_rcv_type(packet->rhf));
Mike Marciniszyn77241052015-07-30 15:17:43 -04001550 return RHF_RCV_CONTINUE;
1551}