blob: 5280d82c344e957c5da7aeba9e139c982fd5b916 [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
Don Hiatt72c07e22017-08-04 13:53:58 -0700240static inline struct hfi1_16b_header
241 *hfi1_get_16B_header(struct hfi1_devdata *dd,
242 __le32 *rhf_addr)
243{
244 return (struct hfi1_16b_header *)hfi1_get_header(dd, rhf_addr);
245}
246
Mike Marciniszyn77241052015-07-30 15:17:43 -0400247/*
248 * Validate and encode the a given RcvArray Buffer size.
249 * The function will check whether the given size falls within
250 * allowed size ranges for the respective type and, optionally,
251 * return the proper encoding.
252 */
Mike Marciniszyna82a7fc2017-02-08 05:26:02 -0800253int hfi1_rcvbuf_validate(u32 size, u8 type, u16 *encoded)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400254{
Amitoj Kaur Chawlaf5389662016-03-04 22:40:17 +0530255 if (unlikely(!PAGE_ALIGNED(size)))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400256 return 0;
257 if (unlikely(size < MIN_EAGER_BUFFER))
258 return 0;
259 if (size >
260 (type == PT_EAGER ? MAX_EAGER_BUFFER : MAX_EXPECTED_BUFFER))
261 return 0;
262 if (encoded)
263 *encoded = ilog2(size / PAGE_SIZE) + 1;
264 return 1;
265}
266
267static void rcv_hdrerr(struct hfi1_ctxtdata *rcd, struct hfi1_pportdata *ppd,
268 struct hfi1_packet *packet)
269{
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700270 struct ib_header *rhdr = packet->hdr;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400271 u32 rte = rhf_rcv_type_err(packet->rhf);
Don Hiatt90397462017-05-12 09:20:20 -0700272 u8 lnh = ib_get_lnh(rhdr);
273 bool has_grh = false;
Sebastian Sanchezf3e862c2017-02-08 05:26:25 -0800274 struct hfi1_ibport *ibp = rcd_to_iport(rcd);
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800275 struct hfi1_devdata *dd = ppd->dd;
276 struct rvt_dev_info *rdi = &dd->verbs_dev.rdi;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400277
278 if (packet->rhf & (RHF_VCRC_ERR | RHF_ICRC_ERR))
279 return;
280
Don Hiatt90397462017-05-12 09:20:20 -0700281 if (lnh == HFI1_LRH_BTH) {
282 packet->ohdr = &rhdr->u.oth;
283 } else if (lnh == HFI1_LRH_GRH) {
284 has_grh = true;
285 packet->ohdr = &rhdr->u.l.oth;
286 packet->grh = &rhdr->u.l.grh;
287 } else {
288 goto drop;
289 }
290
Mike Marciniszyn77241052015-07-30 15:17:43 -0400291 if (packet->rhf & RHF_TID_ERR) {
292 /* For TIDERR and RC QPs preemptively schedule a NAK */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400293 u32 tlen = rhf_pkt_len(packet->rhf); /* in bytes */
Don Hiatt90397462017-05-12 09:20:20 -0700294 u32 dlid = ib_get_dlid(rhdr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400295 u32 qp_num;
Don Hiatt90397462017-05-12 09:20:20 -0700296 u32 mlid_base = be16_to_cpu(IB_MULTICAST_LID_BASE);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400297
298 /* Sanity check packet */
299 if (tlen < 24)
300 goto drop;
301
302 /* Check for GRH */
Don Hiatt90397462017-05-12 09:20:20 -0700303 if (has_grh) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400304 u32 vtf;
Don Hiatt90397462017-05-12 09:20:20 -0700305 struct ib_grh *grh = packet->grh;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400306
Don Hiatt90397462017-05-12 09:20:20 -0700307 if (grh->next_hdr != IB_GRH_NEXT_HDR)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400308 goto drop;
Don Hiatt90397462017-05-12 09:20:20 -0700309 vtf = be32_to_cpu(grh->version_tclass_flow);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400310 if ((vtf >> IB_GRH_VERSION_SHIFT) != IB_GRH_VERSION)
311 goto drop;
Jubin Johne4909742016-02-14 20:22:00 -0800312 }
Don Hiatt90397462017-05-12 09:20:20 -0700313
Mike Marciniszyn77241052015-07-30 15:17:43 -0400314 /* Get the destination QP number. */
Don Hiatt90397462017-05-12 09:20:20 -0700315 qp_num = ib_bth_get_qpn(packet->ohdr);
316 if (dlid < mlid_base) {
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800317 struct rvt_qp *qp;
Dean Luickb77d7132015-10-26 10:28:43 -0400318 unsigned long flags;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400319
320 rcu_read_lock();
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800321 qp = rvt_lookup_qpn(rdi, &ibp->rvp, qp_num);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400322 if (!qp) {
323 rcu_read_unlock();
324 goto drop;
325 }
326
327 /*
328 * Handle only RC QPs - for other QP types drop error
329 * packet.
330 */
Dean Luickb77d7132015-10-26 10:28:43 -0400331 spin_lock_irqsave(&qp->r_lock, flags);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400332
333 /* Check for valid receive state. */
Dennis Dalessandro83693bd2016-01-19 14:43:33 -0800334 if (!(ib_rvt_state_ops[qp->state] &
335 RVT_PROCESS_RECV_OK)) {
Dennis Dalessandro4eb06882016-01-19 14:42:39 -0800336 ibp->rvp.n_pkt_drops++;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400337 }
338
339 switch (qp->ibqp.qp_type) {
340 case IB_QPT_RC:
Don Hiatt90397462017-05-12 09:20:20 -0700341 hfi1_rc_hdrerr(rcd, packet, qp);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400342 break;
343 default:
344 /* For now don't handle any other QP types */
345 break;
346 }
347
Dean Luickb77d7132015-10-26 10:28:43 -0400348 spin_unlock_irqrestore(&qp->r_lock, flags);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400349 rcu_read_unlock();
350 } /* Unicast QP */
351 } /* Valid packet with TIDErr */
352
353 /* handle "RcvTypeErr" flags */
354 switch (rte) {
355 case RHF_RTE_ERROR_OP_CODE_ERR:
356 {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400357 void *ebuf = NULL;
Don Hiatt90397462017-05-12 09:20:20 -0700358 u8 opcode;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400359
360 if (rhf_use_egr_bfr(packet->rhf))
361 ebuf = packet->ebuf;
362
Jubin Johnd125a6c2016-02-14 20:19:49 -0800363 if (!ebuf)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400364 goto drop; /* this should never happen */
365
Don Hiatt90397462017-05-12 09:20:20 -0700366 opcode = ib_bth_get_opcode(packet->ohdr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400367 if (opcode == IB_OPCODE_CNP) {
368 /*
369 * Only in pre-B0 h/w is the CNP_OPCODE handled
Edward Mascarenhas80e48982015-12-21 21:57:44 -0500370 * via this code path.
Mike Marciniszyn77241052015-07-30 15:17:43 -0400371 */
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800372 struct rvt_qp *qp = NULL;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400373 u32 lqpn, rqpn;
374 u16 rlid;
375 u8 svc_type, sl, sc5;
376
Dasaratharaman Chandramouliaad559c2017-04-09 10:16:15 -0700377 sc5 = hfi1_9B_get_sc5(rhdr, packet->rhf);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400378 sl = ibp->sc_to_sl[sc5];
379
Don Hiatt90397462017-05-12 09:20:20 -0700380 lqpn = ib_bth_get_qpn(packet->ohdr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400381 rcu_read_lock();
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800382 qp = rvt_lookup_qpn(rdi, &ibp->rvp, lqpn);
Jubin Johnd125a6c2016-02-14 20:19:49 -0800383 if (!qp) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400384 rcu_read_unlock();
385 goto drop;
386 }
387
388 switch (qp->ibqp.qp_type) {
389 case IB_QPT_UD:
390 rlid = 0;
391 rqpn = 0;
392 svc_type = IB_CC_SVCTYPE_UD;
393 break;
394 case IB_QPT_UC:
Don Hiattcb4270572017-04-09 10:16:22 -0700395 rlid = ib_get_slid(rhdr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400396 rqpn = qp->remote_qpn;
397 svc_type = IB_CC_SVCTYPE_UC;
398 break;
399 default:
400 goto drop;
401 }
402
403 process_becn(ppd, sl, rlid, lqpn, rqpn, svc_type);
404 rcu_read_unlock();
405 }
406
407 packet->rhf &= ~RHF_RCV_TYPE_ERR_SMASK;
408 break;
409 }
410 default:
411 break;
412 }
413
414drop:
415 return;
416}
417
418static inline void init_packet(struct hfi1_ctxtdata *rcd,
Jubin John17fb4f22016-02-14 20:21:52 -0800419 struct hfi1_packet *packet)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400420{
Mike Marciniszyn77241052015-07-30 15:17:43 -0400421 packet->rsize = rcd->rcvhdrqentsize; /* words */
422 packet->maxcnt = rcd->rcvhdrq_cnt * packet->rsize; /* words */
423 packet->rcd = rcd;
424 packet->updegr = 0;
425 packet->etail = -1;
Dean Luickf4f30031c2015-10-26 10:28:44 -0400426 packet->rhf_addr = get_rhf_addr(rcd);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400427 packet->rhf = rhf_to_cpu(packet->rhf_addr);
428 packet->rhqoff = rcd->head;
429 packet->numpkt = 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400430}
431
Mitko Haralanov5fd2b562016-07-25 13:38:07 -0700432void hfi1_process_ecn_slowpath(struct rvt_qp *qp, struct hfi1_packet *pkt,
433 bool do_cnp)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400434{
435 struct hfi1_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700436 struct ib_header *hdr = pkt->hdr;
437 struct ib_other_headers *ohdr = pkt->ohdr;
Don Hiatt90397462017-05-12 09:20:20 -0700438 struct ib_grh *grh = pkt->grh;
Mitko Haralanov5fd2b562016-07-25 13:38:07 -0700439 u32 rqpn = 0, bth1;
Don Hiattcb4270572017-04-09 10:16:22 -0700440 u16 rlid, dlid = ib_get_dlid(hdr);
Mitko Haralanov5fd2b562016-07-25 13:38:07 -0700441 u8 sc, svc_type;
442 bool is_mcast = false;
443
Mike Marciniszyn77241052015-07-30 15:17:43 -0400444 switch (qp->ibqp.qp_type) {
Arthur Kepner977940b2015-11-04 21:10:10 -0500445 case IB_QPT_SMI:
446 case IB_QPT_GSI:
Mike Marciniszyn77241052015-07-30 15:17:43 -0400447 case IB_QPT_UD:
Don Hiattcb4270572017-04-09 10:16:22 -0700448 rlid = ib_get_slid(hdr);
Don Hiatt7dafbab2017-05-12 09:19:55 -0700449 rqpn = ib_get_sqpn(ohdr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400450 svc_type = IB_CC_SVCTYPE_UD;
Mitko Haralanov5fd2b562016-07-25 13:38:07 -0700451 is_mcast = (dlid > be16_to_cpu(IB_MULTICAST_LID_BASE)) &&
452 (dlid != be16_to_cpu(IB_LID_PERMISSIVE));
Mike Marciniszyn77241052015-07-30 15:17:43 -0400453 break;
Arthur Kepner977940b2015-11-04 21:10:10 -0500454 case IB_QPT_UC:
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400455 rlid = rdma_ah_get_dlid(&qp->remote_ah_attr);
Arthur Kepner977940b2015-11-04 21:10:10 -0500456 rqpn = qp->remote_qpn;
457 svc_type = IB_CC_SVCTYPE_UC;
458 break;
459 case IB_QPT_RC:
Dasaratharaman Chandramoulid8966fc2017-04-29 14:41:28 -0400460 rlid = rdma_ah_get_dlid(&qp->remote_ah_attr);
Arthur Kepner977940b2015-11-04 21:10:10 -0500461 rqpn = qp->remote_qpn;
462 svc_type = IB_CC_SVCTYPE_RC;
463 break;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400464 default:
465 return;
466 }
467
Dasaratharaman Chandramouliaad559c2017-04-09 10:16:15 -0700468 sc = hfi1_9B_get_sc5(hdr, pkt->rhf);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400469
Mitko Haralanov5fd2b562016-07-25 13:38:07 -0700470 bth1 = be32_to_cpu(ohdr->bth[1]);
Don Hiatt3d591092017-04-09 10:16:28 -0700471 if (do_cnp && (bth1 & IB_FECN_SMASK)) {
Don Hiatt7dafbab2017-05-12 09:19:55 -0700472 u16 pkey = ib_bth_get_pkey(ohdr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400473
Mitko Haralanov5fd2b562016-07-25 13:38:07 -0700474 return_cnp(ibp, qp, rqpn, pkey, dlid, rlid, sc, grh);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400475 }
476
Don Hiatt3d591092017-04-09 10:16:28 -0700477 if (!is_mcast && (bth1 & IB_BECN_SMASK)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400478 struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800479 u32 lqpn = bth1 & RVT_QPN_MASK;
Mitko Haralanov5fd2b562016-07-25 13:38:07 -0700480 u8 sl = ibp->sc_to_sl[sc];
Mike Marciniszyn77241052015-07-30 15:17:43 -0400481
Arthur Kepner977940b2015-11-04 21:10:10 -0500482 process_becn(ppd, sl, rlid, lqpn, rqpn, svc_type);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400483 }
Mitko Haralanov5fd2b562016-07-25 13:38:07 -0700484
Mike Marciniszyn77241052015-07-30 15:17:43 -0400485}
486
487struct ps_mdata {
488 struct hfi1_ctxtdata *rcd;
489 u32 rsize;
490 u32 maxcnt;
491 u32 ps_head;
492 u32 ps_tail;
493 u32 ps_seq;
494};
495
496static inline void init_ps_mdata(struct ps_mdata *mdata,
497 struct hfi1_packet *packet)
498{
499 struct hfi1_ctxtdata *rcd = packet->rcd;
500
501 mdata->rcd = rcd;
502 mdata->rsize = packet->rsize;
503 mdata->maxcnt = packet->maxcnt;
Arthur Kepner3e7ccca2015-11-04 21:10:09 -0500504 mdata->ps_head = packet->rhqoff;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400505
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500506 if (HFI1_CAP_KGET_MASK(rcd->flags, DMA_RTAIL)) {
Arthur Kepner3e7ccca2015-11-04 21:10:09 -0500507 mdata->ps_tail = get_rcvhdrtail(rcd);
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500508 if (rcd->ctxt == HFI1_CTRL_CTXT)
509 mdata->ps_seq = rcd->seq_cnt;
510 else
511 mdata->ps_seq = 0; /* not used with DMA_RTAIL */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400512 } else {
513 mdata->ps_tail = 0; /* used only with DMA_RTAIL*/
514 mdata->ps_seq = rcd->seq_cnt;
515 }
516}
517
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500518static inline int ps_done(struct ps_mdata *mdata, u64 rhf,
519 struct hfi1_ctxtdata *rcd)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400520{
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500521 if (HFI1_CAP_KGET_MASK(rcd->flags, DMA_RTAIL))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400522 return mdata->ps_head == mdata->ps_tail;
523 return mdata->ps_seq != rhf_rcv_seq(rhf);
524}
525
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500526static inline int ps_skip(struct ps_mdata *mdata, u64 rhf,
527 struct hfi1_ctxtdata *rcd)
528{
529 /*
530 * Control context can potentially receive an invalid rhf.
531 * Drop such packets.
532 */
533 if ((rcd->ctxt == HFI1_CTRL_CTXT) && (mdata->ps_head != mdata->ps_tail))
534 return mdata->ps_seq != rhf_rcv_seq(rhf);
535
536 return 0;
537}
538
539static inline void update_ps_mdata(struct ps_mdata *mdata,
540 struct hfi1_ctxtdata *rcd)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400541{
Mike Marciniszyn77241052015-07-30 15:17:43 -0400542 mdata->ps_head += mdata->rsize;
Arthur Kepner3e7ccca2015-11-04 21:10:09 -0500543 if (mdata->ps_head >= mdata->maxcnt)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400544 mdata->ps_head = 0;
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500545
546 /* Control context must do seq counting */
547 if (!HFI1_CAP_KGET_MASK(rcd->flags, DMA_RTAIL) ||
548 (rcd->ctxt == HFI1_CTRL_CTXT)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400549 if (++mdata->ps_seq > 13)
550 mdata->ps_seq = 1;
551 }
552}
553
554/*
555 * prescan_rxq - search through the receive queue looking for packets
556 * containing Excplicit Congestion Notifications (FECNs, or BECNs).
557 * When an ECN is found, process the Congestion Notification, and toggle
558 * it off.
Vennila Megavannan6c9e50f2016-02-03 14:32:57 -0800559 * This is declared as a macro to allow quick checking of the port to avoid
560 * the overhead of a function call if not enabled.
Mike Marciniszyn77241052015-07-30 15:17:43 -0400561 */
Vennila Megavannan6c9e50f2016-02-03 14:32:57 -0800562#define prescan_rxq(rcd, packet) \
563 do { \
564 if (rcd->ppd->cc_prescan) \
565 __prescan_rxq(packet); \
566 } while (0)
567static void __prescan_rxq(struct hfi1_packet *packet)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400568{
569 struct hfi1_ctxtdata *rcd = packet->rcd;
570 struct ps_mdata mdata;
571
Mike Marciniszyn77241052015-07-30 15:17:43 -0400572 init_ps_mdata(&mdata, packet);
573
574 while (1) {
575 struct hfi1_devdata *dd = rcd->dd;
Sebastian Sanchezf3e862c2017-02-08 05:26:25 -0800576 struct hfi1_ibport *ibp = rcd_to_iport(rcd);
Jubin John50e5dcb2016-02-14 20:19:41 -0800577 __le32 *rhf_addr = (__le32 *)rcd->rcvhdrq + mdata.ps_head +
Mike Marciniszyn77241052015-07-30 15:17:43 -0400578 dd->rhf_offset;
Dennis Dalessandro895420d2016-01-19 14:42:28 -0800579 struct rvt_qp *qp;
Mike Marciniszyn261a4352016-09-06 04:35:05 -0700580 struct ib_header *hdr;
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800581 struct rvt_dev_info *rdi = &dd->verbs_dev.rdi;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400582 u64 rhf = rhf_to_cpu(rhf_addr);
Arthur Kepner977940b2015-11-04 21:10:10 -0500583 u32 etype = rhf_rcv_type(rhf), qpn, bth1;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400584 int is_ecn = 0;
585 u8 lnh;
586
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500587 if (ps_done(&mdata, rhf, rcd))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400588 break;
589
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500590 if (ps_skip(&mdata, rhf, rcd))
591 goto next;
592
Mike Marciniszyn77241052015-07-30 15:17:43 -0400593 if (etype != RHF_RCV_TYPE_IB)
594 goto next;
595
Dasaratharaman Chandramoulif2d8a0b2016-10-25 13:12:23 -0700596 packet->hdr = hfi1_get_msgheader(dd, rhf_addr);
597 hdr = packet->hdr;
Don Hiattcb4270572017-04-09 10:16:22 -0700598 lnh = ib_get_lnh(hdr);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400599
Dasaratharaman Chandramoulif2d8a0b2016-10-25 13:12:23 -0700600 if (lnh == HFI1_LRH_BTH) {
601 packet->ohdr = &hdr->u.oth;
Don Hiatt90397462017-05-12 09:20:20 -0700602 packet->grh = NULL;
Dasaratharaman Chandramoulif2d8a0b2016-10-25 13:12:23 -0700603 } else if (lnh == HFI1_LRH_GRH) {
604 packet->ohdr = &hdr->u.l.oth;
Don Hiatt90397462017-05-12 09:20:20 -0700605 packet->grh = &hdr->u.l.grh;
Dasaratharaman Chandramoulif2d8a0b2016-10-25 13:12:23 -0700606 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400607 goto next; /* just in case */
Dasaratharaman Chandramoulif2d8a0b2016-10-25 13:12:23 -0700608 }
Mitko Haralanov5fd2b562016-07-25 13:38:07 -0700609
Dasaratharaman Chandramoulif2d8a0b2016-10-25 13:12:23 -0700610 bth1 = be32_to_cpu(packet->ohdr->bth[1]);
Don Hiatt3d591092017-04-09 10:16:28 -0700611 is_ecn = !!(bth1 & (IB_FECN_SMASK | IB_BECN_SMASK));
Mike Marciniszyn77241052015-07-30 15:17:43 -0400612
613 if (!is_ecn)
614 goto next;
615
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800616 qpn = bth1 & RVT_QPN_MASK;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400617 rcu_read_lock();
Dennis Dalessandroec4274f2016-01-19 14:43:44 -0800618 qp = rvt_lookup_qpn(rdi, &ibp->rvp, qpn);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400619
Jubin Johnd125a6c2016-02-14 20:19:49 -0800620 if (!qp) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400621 rcu_read_unlock();
622 goto next;
623 }
624
Mitko Haralanov5fd2b562016-07-25 13:38:07 -0700625 process_ecn(qp, packet, true);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400626 rcu_read_unlock();
Arthur Kepner977940b2015-11-04 21:10:10 -0500627
628 /* turn off BECN, FECN */
Don Hiatt3d591092017-04-09 10:16:28 -0700629 bth1 &= ~(IB_FECN_SMASK | IB_BECN_SMASK);
Dasaratharaman Chandramoulif2d8a0b2016-10-25 13:12:23 -0700630 packet->ohdr->bth[1] = cpu_to_be32(bth1);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400631next:
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500632 update_ps_mdata(&mdata, rcd);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400633 }
634}
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500635
Mike Marciniszyna82a7fc2017-02-08 05:26:02 -0800636static void process_rcv_qp_work(struct hfi1_ctxtdata *rcd)
637{
638 struct rvt_qp *qp, *nqp;
639
640 /*
641 * Iterate over all QPs waiting to respond.
642 * The list won't change since the IRQ is only run on one CPU.
643 */
644 list_for_each_entry_safe(qp, nqp, &rcd->qp_wait_list, rspwait) {
645 list_del_init(&qp->rspwait);
646 if (qp->r_flags & RVT_R_RSP_NAK) {
647 qp->r_flags &= ~RVT_R_RSP_NAK;
648 hfi1_send_rc_ack(rcd, qp, 0);
649 }
650 if (qp->r_flags & RVT_R_RSP_SEND) {
651 unsigned long flags;
652
653 qp->r_flags &= ~RVT_R_RSP_SEND;
654 spin_lock_irqsave(&qp->s_lock, flags);
655 if (ib_rvt_state_ops[qp->state] &
656 RVT_PROCESS_OR_FLUSH_SEND)
657 hfi1_schedule_send(qp);
658 spin_unlock_irqrestore(&qp->s_lock, flags);
659 }
660 rvt_put_qp(qp);
661 }
662}
663
664static noinline int max_packet_exceeded(struct hfi1_packet *packet, int thread)
665{
666 if (thread) {
667 if ((packet->numpkt & (MAX_PKT_RECV_THREAD - 1)) == 0)
668 /* allow defered processing */
669 process_rcv_qp_work(packet->rcd);
670 cond_resched();
671 return RCV_PKT_OK;
672 } else {
673 this_cpu_inc(*packet->rcd->dd->rcv_limit);
674 return RCV_PKT_LIMIT;
675 }
676}
677
678static inline int check_max_packet(struct hfi1_packet *packet, int thread)
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500679{
680 int ret = RCV_PKT_OK;
681
Mike Marciniszyna82a7fc2017-02-08 05:26:02 -0800682 if (unlikely((packet->numpkt & (MAX_PKT_RECV - 1)) == 0))
683 ret = max_packet_exceeded(packet, thread);
684 return ret;
685}
686
687static noinline int skip_rcv_packet(struct hfi1_packet *packet, int thread)
688{
689 int ret;
690
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500691 /* Set up for the next packet */
692 packet->rhqoff += packet->rsize;
693 if (packet->rhqoff >= packet->maxcnt)
694 packet->rhqoff = 0;
695
696 packet->numpkt++;
Mike Marciniszyna82a7fc2017-02-08 05:26:02 -0800697 ret = check_max_packet(packet, thread);
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500698
699 packet->rhf_addr = (__le32 *)packet->rcd->rcvhdrq + packet->rhqoff +
700 packet->rcd->dd->rhf_offset;
701 packet->rhf = rhf_to_cpu(packet->rhf_addr);
702
703 return ret;
704}
Mike Marciniszyn77241052015-07-30 15:17:43 -0400705
Dean Luickf4f30031c2015-10-26 10:28:44 -0400706static inline int process_rcv_packet(struct hfi1_packet *packet, int thread)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400707{
Mike Marciniszyna82a7fc2017-02-08 05:26:02 -0800708 int ret;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400709
Mike Marciniszyn77241052015-07-30 15:17:43 -0400710 packet->etype = rhf_rcv_type(packet->rhf);
Don Hiatt90397462017-05-12 09:20:20 -0700711
Mike Marciniszyn77241052015-07-30 15:17:43 -0400712 /* total length */
713 packet->tlen = rhf_pkt_len(packet->rhf); /* in bytes */
714 /* retrieve eager buffer details */
715 packet->ebuf = NULL;
716 if (rhf_use_egr_bfr(packet->rhf)) {
717 packet->etail = rhf_egr_index(packet->rhf);
718 packet->ebuf = get_egrbuf(packet->rcd, packet->rhf,
719 &packet->updegr);
720 /*
721 * Prefetch the contents of the eager buffer. It is
722 * OK to send a negative length to prefetch_range().
723 * The +2 is the size of the RHF.
724 */
725 prefetch_range(packet->ebuf,
Jubin John17fb4f22016-02-14 20:21:52 -0800726 packet->tlen - ((packet->rcd->rcvhdrqentsize -
727 (rhf_hdrq_offset(packet->rhf)
728 + 2)) * 4));
Mike Marciniszyn77241052015-07-30 15:17:43 -0400729 }
730
731 /*
732 * Call a type specific handler for the packet. We
733 * should be able to trust that etype won't be beyond
734 * the range of valid indexes. If so something is really
735 * wrong and we can probably just let things come
736 * crashing down. There is no need to eat another
737 * comparison in this performance critical code.
738 */
739 packet->rcd->dd->rhf_rcv_function_map[packet->etype](packet);
740 packet->numpkt++;
741
742 /* Set up for the next packet */
743 packet->rhqoff += packet->rsize;
744 if (packet->rhqoff >= packet->maxcnt)
745 packet->rhqoff = 0;
746
Mike Marciniszyna82a7fc2017-02-08 05:26:02 -0800747 ret = check_max_packet(packet, thread);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400748
Jubin John50e5dcb2016-02-14 20:19:41 -0800749 packet->rhf_addr = (__le32 *)packet->rcd->rcvhdrq + packet->rhqoff +
Mike Marciniszyn77241052015-07-30 15:17:43 -0400750 packet->rcd->dd->rhf_offset;
751 packet->rhf = rhf_to_cpu(packet->rhf_addr);
752
753 return ret;
754}
755
756static inline void process_rcv_update(int last, struct hfi1_packet *packet)
757{
758 /*
759 * Update head regs etc., every 16 packets, if not last pkt,
760 * to help prevent rcvhdrq overflows, when many packets
761 * are processed and queue is nearly full.
762 * Don't request an interrupt for intermediate updates.
763 */
764 if (!last && !(packet->numpkt & 0xf)) {
765 update_usrhead(packet->rcd, packet->rhqoff, packet->updegr,
766 packet->etail, 0, 0);
767 packet->updegr = 0;
768 }
Don Hiatt90397462017-05-12 09:20:20 -0700769 packet->grh = NULL;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400770}
771
772static inline void finish_packet(struct hfi1_packet *packet)
773{
Mike Marciniszyn77241052015-07-30 15:17:43 -0400774 /*
775 * Nothing we need to free for the packet.
776 *
777 * The only thing we need to do is a final update and call for an
778 * interrupt
779 */
780 update_usrhead(packet->rcd, packet->rcd->head, packet->updegr,
781 packet->etail, rcv_intr_dynamic, packet->numpkt);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400782}
783
Mike Marciniszyn77241052015-07-30 15:17:43 -0400784/*
785 * Handle receive interrupts when using the no dma rtail option.
786 */
Dean Luickf4f30031c2015-10-26 10:28:44 -0400787int handle_receive_interrupt_nodma_rtail(struct hfi1_ctxtdata *rcd, int thread)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400788{
789 u32 seq;
Dean Luickf4f30031c2015-10-26 10:28:44 -0400790 int last = RCV_PKT_OK;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400791 struct hfi1_packet packet;
792
793 init_packet(rcd, &packet);
794 seq = rhf_rcv_seq(packet.rhf);
Dean Luickf4f30031c2015-10-26 10:28:44 -0400795 if (seq != rcd->seq_cnt) {
796 last = RCV_PKT_DONE;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400797 goto bail;
Dean Luickf4f30031c2015-10-26 10:28:44 -0400798 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400799
Vennila Megavannan6c9e50f2016-02-03 14:32:57 -0800800 prescan_rxq(rcd, &packet);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400801
Dean Luickf4f30031c2015-10-26 10:28:44 -0400802 while (last == RCV_PKT_OK) {
803 last = process_rcv_packet(&packet, thread);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400804 seq = rhf_rcv_seq(packet.rhf);
805 if (++rcd->seq_cnt > 13)
806 rcd->seq_cnt = 1;
807 if (seq != rcd->seq_cnt)
Dean Luickf4f30031c2015-10-26 10:28:44 -0400808 last = RCV_PKT_DONE;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400809 process_rcv_update(last, &packet);
810 }
Mike Marciniszyna82a7fc2017-02-08 05:26:02 -0800811 process_rcv_qp_work(rcd);
812 rcd->head = packet.rhqoff;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400813bail:
814 finish_packet(&packet);
Dean Luickf4f30031c2015-10-26 10:28:44 -0400815 return last;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400816}
817
Dean Luickf4f30031c2015-10-26 10:28:44 -0400818int handle_receive_interrupt_dma_rtail(struct hfi1_ctxtdata *rcd, int thread)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400819{
820 u32 hdrqtail;
Dean Luickf4f30031c2015-10-26 10:28:44 -0400821 int last = RCV_PKT_OK;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400822 struct hfi1_packet packet;
823
824 init_packet(rcd, &packet);
825 hdrqtail = get_rcvhdrtail(rcd);
Dean Luickf4f30031c2015-10-26 10:28:44 -0400826 if (packet.rhqoff == hdrqtail) {
827 last = RCV_PKT_DONE;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400828 goto bail;
Dean Luickf4f30031c2015-10-26 10:28:44 -0400829 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400830 smp_rmb(); /* prevent speculative reads of dma'ed hdrq */
831
Vennila Megavannan6c9e50f2016-02-03 14:32:57 -0800832 prescan_rxq(rcd, &packet);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400833
Dean Luickf4f30031c2015-10-26 10:28:44 -0400834 while (last == RCV_PKT_OK) {
835 last = process_rcv_packet(&packet, thread);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400836 if (packet.rhqoff == hdrqtail)
Dean Luickf4f30031c2015-10-26 10:28:44 -0400837 last = RCV_PKT_DONE;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400838 process_rcv_update(last, &packet);
839 }
Mike Marciniszyna82a7fc2017-02-08 05:26:02 -0800840 process_rcv_qp_work(rcd);
841 rcd->head = packet.rhqoff;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400842bail:
843 finish_packet(&packet);
Dean Luickf4f30031c2015-10-26 10:28:44 -0400844 return last;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400845}
846
Michael J. Ruhle6f76222017-07-24 07:45:55 -0700847static inline void set_nodma_rtail(struct hfi1_devdata *dd, u16 ctxt)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400848{
Michael J. Ruhld295dbe2017-08-04 13:52:44 -0700849 struct hfi1_ctxtdata *rcd;
Michael J. Ruhle6f76222017-07-24 07:45:55 -0700850 u16 i;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400851
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700852 /*
853 * For dynamically allocated kernel contexts (like vnic) switch
854 * interrupt handler only for that context. Otherwise, switch
855 * interrupt handler for all statically allocated kernel contexts.
856 */
857 if (ctxt >= dd->first_dyn_alloc_ctxt) {
Michael J. Ruhld295dbe2017-08-04 13:52:44 -0700858 rcd = hfi1_rcd_get_by_index(dd, ctxt);
859 if (rcd) {
860 rcd->do_interrupt =
861 &handle_receive_interrupt_nodma_rtail;
862 hfi1_rcd_put(rcd);
863 }
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700864 return;
865 }
866
Michael J. Ruhld295dbe2017-08-04 13:52:44 -0700867 for (i = HFI1_CTRL_CTXT + 1; i < dd->first_dyn_alloc_ctxt; i++) {
868 rcd = hfi1_rcd_get_by_index(dd, i);
869 if (rcd)
870 rcd->do_interrupt =
871 &handle_receive_interrupt_nodma_rtail;
872 hfi1_rcd_put(rcd);
873 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400874}
875
Michael J. Ruhle6f76222017-07-24 07:45:55 -0700876static inline void set_dma_rtail(struct hfi1_devdata *dd, u16 ctxt)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400877{
Michael J. Ruhld295dbe2017-08-04 13:52:44 -0700878 struct hfi1_ctxtdata *rcd;
Michael J. Ruhle6f76222017-07-24 07:45:55 -0700879 u16 i;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400880
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700881 /*
882 * For dynamically allocated kernel contexts (like vnic) switch
883 * interrupt handler only for that context. Otherwise, switch
884 * interrupt handler for all statically allocated kernel contexts.
885 */
886 if (ctxt >= dd->first_dyn_alloc_ctxt) {
Michael J. Ruhld295dbe2017-08-04 13:52:44 -0700887 rcd = hfi1_rcd_get_by_index(dd, ctxt);
888 if (rcd) {
889 rcd->do_interrupt =
890 &handle_receive_interrupt_dma_rtail;
891 hfi1_rcd_put(rcd);
892 }
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700893 return;
894 }
895
Michael J. Ruhld295dbe2017-08-04 13:52:44 -0700896 for (i = HFI1_CTRL_CTXT + 1; i < dd->first_dyn_alloc_ctxt; i++) {
897 rcd = hfi1_rcd_get_by_index(dd, i);
898 if (rcd)
899 rcd->do_interrupt =
900 &handle_receive_interrupt_dma_rtail;
901 hfi1_rcd_put(rcd);
902 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400903}
904
Jim Snowfb9036d2016-01-11 18:32:21 -0500905void set_all_slowpath(struct hfi1_devdata *dd)
906{
Michael J. Ruhld295dbe2017-08-04 13:52:44 -0700907 struct hfi1_ctxtdata *rcd;
Michael J. Ruhle6f76222017-07-24 07:45:55 -0700908 u16 i;
Jim Snowfb9036d2016-01-11 18:32:21 -0500909
910 /* HFI1_CTRL_CTXT must always use the slow path interrupt handler */
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700911 for (i = HFI1_CTRL_CTXT + 1; i < dd->num_rcv_contexts; i++) {
Michael J. Ruhld295dbe2017-08-04 13:52:44 -0700912 rcd = hfi1_rcd_get_by_index(dd, i);
913 if (!rcd)
914 continue;
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700915 if ((i < dd->first_dyn_alloc_ctxt) ||
Michael J. Ruhld295dbe2017-08-04 13:52:44 -0700916 (rcd->sc && (rcd->sc->type == SC_KERNEL))) {
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700917 rcd->do_interrupt = &handle_receive_interrupt;
Michael J. Ruhld295dbe2017-08-04 13:52:44 -0700918 }
919 hfi1_rcd_put(rcd);
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700920 }
Jim Snowfb9036d2016-01-11 18:32:21 -0500921}
922
923static inline int set_armed_to_active(struct hfi1_ctxtdata *rcd,
Mike Marciniszync867caa2016-08-09 11:19:55 -0400924 struct hfi1_packet *packet,
Jim Snowfb9036d2016-01-11 18:32:21 -0500925 struct hfi1_devdata *dd)
926{
927 struct work_struct *lsaw = &rcd->ppd->linkstate_active_work;
Mike Marciniszyn69b9f4a2016-08-09 11:19:56 -0400928 u8 etype = rhf_rcv_type(packet->rhf);
Don Hiatt90397462017-05-12 09:20:20 -0700929 u8 sc = SC15_PACKET;
Jim Snowfb9036d2016-01-11 18:32:21 -0500930
Don Hiatt90397462017-05-12 09:20:20 -0700931 if (etype == RHF_RCV_TYPE_IB) {
932 struct ib_header *hdr = hfi1_get_msgheader(packet->rcd->dd,
933 packet->rhf_addr);
934 sc = hfi1_9B_get_sc5(hdr, packet->rhf);
Don Hiatt72c07e22017-08-04 13:53:58 -0700935 } else if (etype == RHF_RCV_TYPE_BYPASS) {
936 struct hfi1_16b_header *hdr = hfi1_get_16B_header(
937 packet->rcd->dd,
938 packet->rhf_addr);
939 sc = hfi1_16B_get_sc(hdr);
Don Hiatt90397462017-05-12 09:20:20 -0700940 }
941 if (sc != SC15_PACKET) {
Byczkowski, Jakubbec7c792017-05-29 17:21:32 -0700942 int hwstate = driver_lstate(rcd->ppd);
Jim Snowfb9036d2016-01-11 18:32:21 -0500943
Byczkowski, Jakubbec7c792017-05-29 17:21:32 -0700944 if (hwstate != IB_PORT_ACTIVE) {
945 dd_dev_info(dd,
946 "Unexpected link state %s\n",
947 opa_lstate_name(hwstate));
Jim Snowfb9036d2016-01-11 18:32:21 -0500948 return 0;
949 }
950
Sebastian Sanchez71d47002017-07-29 08:43:49 -0700951 queue_work(rcd->ppd->link_wq, lsaw);
Jim Snowfb9036d2016-01-11 18:32:21 -0500952 return 1;
953 }
954 return 0;
955}
956
Mike Marciniszyn77241052015-07-30 15:17:43 -0400957/*
958 * handle_receive_interrupt - receive a packet
959 * @rcd: the context
960 *
961 * Called from interrupt handler for errors or receive interrupt.
962 * This is the slow path interrupt handler.
963 */
Dean Luickf4f30031c2015-10-26 10:28:44 -0400964int handle_receive_interrupt(struct hfi1_ctxtdata *rcd, int thread)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400965{
Mike Marciniszyn77241052015-07-30 15:17:43 -0400966 struct hfi1_devdata *dd = rcd->dd;
967 u32 hdrqtail;
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500968 int needset, last = RCV_PKT_OK;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400969 struct hfi1_packet packet;
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500970 int skip_pkt = 0;
971
972 /* Control context will always use the slow path interrupt handler */
973 needset = (rcd->ctxt == HFI1_CTRL_CTXT) ? 0 : 1;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400974
975 init_packet(rcd, &packet);
976
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500977 if (!HFI1_CAP_KGET_MASK(rcd->flags, DMA_RTAIL)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400978 u32 seq = rhf_rcv_seq(packet.rhf);
979
Dean Luickf4f30031c2015-10-26 10:28:44 -0400980 if (seq != rcd->seq_cnt) {
981 last = RCV_PKT_DONE;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400982 goto bail;
Dean Luickf4f30031c2015-10-26 10:28:44 -0400983 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400984 hdrqtail = 0;
985 } else {
986 hdrqtail = get_rcvhdrtail(rcd);
Dean Luickf4f30031c2015-10-26 10:28:44 -0400987 if (packet.rhqoff == hdrqtail) {
988 last = RCV_PKT_DONE;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400989 goto bail;
Dean Luickf4f30031c2015-10-26 10:28:44 -0400990 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400991 smp_rmb(); /* prevent speculative reads of dma'ed hdrq */
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500992
993 /*
994 * Control context can potentially receive an invalid
995 * rhf. Drop such packets.
996 */
997 if (rcd->ctxt == HFI1_CTRL_CTXT) {
998 u32 seq = rhf_rcv_seq(packet.rhf);
999
1000 if (seq != rcd->seq_cnt)
1001 skip_pkt = 1;
1002 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001003 }
1004
Vennila Megavannan6c9e50f2016-02-03 14:32:57 -08001005 prescan_rxq(rcd, &packet);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001006
Dean Luickf4f30031c2015-10-26 10:28:44 -04001007 while (last == RCV_PKT_OK) {
Jubin John17fb4f22016-02-14 20:21:52 -08001008 if (unlikely(dd->do_drop &&
1009 atomic_xchg(&dd->drop_packet, DROP_PACKET_OFF) ==
1010 DROP_PACKET_ON)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001011 dd->do_drop = 0;
1012
1013 /* On to the next packet */
1014 packet.rhqoff += packet.rsize;
Jubin John50e5dcb2016-02-14 20:19:41 -08001015 packet.rhf_addr = (__le32 *)rcd->rcvhdrq +
Mike Marciniszyn77241052015-07-30 15:17:43 -04001016 packet.rhqoff +
1017 dd->rhf_offset;
1018 packet.rhf = rhf_to_cpu(packet.rhf_addr);
1019
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -05001020 } else if (skip_pkt) {
1021 last = skip_rcv_packet(&packet, thread);
1022 skip_pkt = 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001023 } else {
Jim Snowfb9036d2016-01-11 18:32:21 -05001024 /* Auto activate link on non-SC15 packet receive */
1025 if (unlikely(rcd->ppd->host_link_state ==
1026 HLS_UP_ARMED) &&
Mike Marciniszync867caa2016-08-09 11:19:55 -04001027 set_armed_to_active(rcd, &packet, dd))
Jim Snowfb9036d2016-01-11 18:32:21 -05001028 goto bail;
Dean Luickf4f30031c2015-10-26 10:28:44 -04001029 last = process_rcv_packet(&packet, thread);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001030 }
1031
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -05001032 if (!HFI1_CAP_KGET_MASK(rcd->flags, DMA_RTAIL)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001033 u32 seq = rhf_rcv_seq(packet.rhf);
1034
1035 if (++rcd->seq_cnt > 13)
1036 rcd->seq_cnt = 1;
1037 if (seq != rcd->seq_cnt)
Dean Luickf4f30031c2015-10-26 10:28:44 -04001038 last = RCV_PKT_DONE;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001039 if (needset) {
Jubin John17fb4f22016-02-14 20:21:52 -08001040 dd_dev_info(dd, "Switching to NO_DMA_RTAIL\n");
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -07001041 set_nodma_rtail(dd, rcd->ctxt);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001042 needset = 0;
1043 }
1044 } else {
1045 if (packet.rhqoff == hdrqtail)
Dean Luickf4f30031c2015-10-26 10:28:44 -04001046 last = RCV_PKT_DONE;
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -05001047 /*
1048 * Control context can potentially receive an invalid
1049 * rhf. Drop such packets.
1050 */
1051 if (rcd->ctxt == HFI1_CTRL_CTXT) {
1052 u32 seq = rhf_rcv_seq(packet.rhf);
1053
1054 if (++rcd->seq_cnt > 13)
1055 rcd->seq_cnt = 1;
1056 if (!last && (seq != rcd->seq_cnt))
1057 skip_pkt = 1;
1058 }
1059
Mike Marciniszyn77241052015-07-30 15:17:43 -04001060 if (needset) {
1061 dd_dev_info(dd,
1062 "Switching to DMA_RTAIL\n");
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -07001063 set_dma_rtail(dd, rcd->ctxt);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001064 needset = 0;
1065 }
1066 }
1067
1068 process_rcv_update(last, &packet);
1069 }
1070
Mike Marciniszyna82a7fc2017-02-08 05:26:02 -08001071 process_rcv_qp_work(rcd);
1072 rcd->head = packet.rhqoff;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001073
1074bail:
1075 /*
1076 * Always write head at end, and setup rcv interrupt, even
1077 * if no packets were processed.
1078 */
1079 finish_packet(&packet);
Dean Luickf4f30031c2015-10-26 10:28:44 -04001080 return last;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001081}
1082
1083/*
Jim Snowfb9036d2016-01-11 18:32:21 -05001084 * We may discover in the interrupt that the hardware link state has
1085 * changed from ARMED to ACTIVE (due to the arrival of a non-SC15 packet),
1086 * and we need to update the driver's notion of the link state. We cannot
1087 * run set_link_state from interrupt context, so we queue this function on
1088 * a workqueue.
1089 *
1090 * We delay the regular interrupt processing until after the state changes
1091 * so that the link will be in the correct state by the time any application
1092 * we wake up attempts to send a reply to any message it received.
1093 * (Subsequent receive interrupts may possibly force the wakeup before we
1094 * update the link state.)
1095 *
1096 * The rcd is freed in hfi1_free_ctxtdata after hfi1_postinit_cleanup invokes
1097 * dd->f_cleanup(dd) to disable the interrupt handler and flush workqueues,
1098 * so we're safe from use-after-free of the rcd.
1099 */
1100void receive_interrupt_work(struct work_struct *work)
1101{
1102 struct hfi1_pportdata *ppd = container_of(work, struct hfi1_pportdata,
1103 linkstate_active_work);
1104 struct hfi1_devdata *dd = ppd->dd;
Michael J. Ruhld295dbe2017-08-04 13:52:44 -07001105 struct hfi1_ctxtdata *rcd;
Michael J. Ruhle6f76222017-07-24 07:45:55 -07001106 u16 i;
Jim Snowfb9036d2016-01-11 18:32:21 -05001107
1108 /* Received non-SC15 packet implies neighbor_normal */
1109 ppd->neighbor_normal = 1;
1110 set_link_state(ppd, HLS_UP_ACTIVE);
1111
1112 /*
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -07001113 * Interrupt all statically allocated kernel contexts that could
1114 * have had an interrupt during auto activation.
Jim Snowfb9036d2016-01-11 18:32:21 -05001115 */
Michael J. Ruhld295dbe2017-08-04 13:52:44 -07001116 for (i = HFI1_CTRL_CTXT; i < dd->first_dyn_alloc_ctxt; i++) {
1117 rcd = hfi1_rcd_get_by_index(dd, i);
1118 if (rcd)
1119 force_recv_intr(rcd);
1120 hfi1_rcd_put(rcd);
1121 }
Jim Snowfb9036d2016-01-11 18:32:21 -05001122}
1123
1124/*
Mike Marciniszyn77241052015-07-30 15:17:43 -04001125 * Convert a given MTU size to the on-wire MAD packet enumeration.
1126 * Return -1 if the size is invalid.
1127 */
1128int mtu_to_enum(u32 mtu, int default_if_bad)
1129{
1130 switch (mtu) {
1131 case 0: return OPA_MTU_0;
1132 case 256: return OPA_MTU_256;
1133 case 512: return OPA_MTU_512;
1134 case 1024: return OPA_MTU_1024;
1135 case 2048: return OPA_MTU_2048;
1136 case 4096: return OPA_MTU_4096;
1137 case 8192: return OPA_MTU_8192;
1138 case 10240: return OPA_MTU_10240;
1139 }
1140 return default_if_bad;
1141}
1142
1143u16 enum_to_mtu(int mtu)
1144{
1145 switch (mtu) {
1146 case OPA_MTU_0: return 0;
1147 case OPA_MTU_256: return 256;
1148 case OPA_MTU_512: return 512;
1149 case OPA_MTU_1024: return 1024;
1150 case OPA_MTU_2048: return 2048;
1151 case OPA_MTU_4096: return 4096;
1152 case OPA_MTU_8192: return 8192;
1153 case OPA_MTU_10240: return 10240;
1154 default: return 0xffff;
1155 }
1156}
1157
1158/*
1159 * set_mtu - set the MTU
1160 * @ppd: the per port data
1161 *
1162 * We can handle "any" incoming size, the issue here is whether we
1163 * need to restrict our outgoing size. We do not deal with what happens
1164 * to programs that are already running when the size changes.
1165 */
1166int set_mtu(struct hfi1_pportdata *ppd)
1167{
1168 struct hfi1_devdata *dd = ppd->dd;
1169 int i, drain, ret = 0, is_up = 0;
1170
1171 ppd->ibmtu = 0;
1172 for (i = 0; i < ppd->vls_supported; i++)
1173 if (ppd->ibmtu < dd->vld[i].mtu)
1174 ppd->ibmtu = dd->vld[i].mtu;
1175 ppd->ibmaxlen = ppd->ibmtu + lrh_max_header_bytes(ppd->dd);
1176
1177 mutex_lock(&ppd->hls_lock);
Jubin Johnd0d236e2016-02-14 20:20:15 -08001178 if (ppd->host_link_state == HLS_UP_INIT ||
1179 ppd->host_link_state == HLS_UP_ARMED ||
1180 ppd->host_link_state == HLS_UP_ACTIVE)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001181 is_up = 1;
1182
1183 drain = !is_ax(dd) && is_up;
1184
1185 if (drain)
1186 /*
1187 * MTU is specified per-VL. To ensure that no packet gets
1188 * stuck (due, e.g., to the MTU for the packet's VL being
1189 * reduced), empty the per-VL FIFOs before adjusting MTU.
1190 */
1191 ret = stop_drain_data_vls(dd);
1192
1193 if (ret) {
1194 dd_dev_err(dd, "%s: cannot stop/drain VLs - refusing to change per-VL MTUs\n",
1195 __func__);
1196 goto err;
1197 }
1198
1199 hfi1_set_ib_cfg(ppd, HFI1_IB_CFG_MTU, 0);
1200
1201 if (drain)
1202 open_fill_data_vls(dd); /* reopen all VLs */
1203
1204err:
1205 mutex_unlock(&ppd->hls_lock);
1206
1207 return ret;
1208}
1209
1210int hfi1_set_lid(struct hfi1_pportdata *ppd, u32 lid, u8 lmc)
1211{
1212 struct hfi1_devdata *dd = ppd->dd;
1213
1214 ppd->lid = lid;
1215 ppd->lmc = lmc;
1216 hfi1_set_ib_cfg(ppd, HFI1_IB_CFG_LIDLMC, 0);
1217
Jakub Pawlakcde10af2016-05-12 10:23:35 -07001218 dd_dev_info(dd, "port %u: got a lid: 0x%x\n", ppd->port, lid);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001219
1220 return 0;
1221}
1222
Easwar Hariharan91ab4ed2016-02-03 14:35:57 -08001223void shutdown_led_override(struct hfi1_pportdata *ppd)
1224{
1225 struct hfi1_devdata *dd = ppd->dd;
1226
Easwar Hariharan409b1462016-02-26 13:33:28 -08001227 /*
Easwar Hariharan22434722016-03-07 11:35:03 -08001228 * This pairs with the memory barrier in hfi1_start_led_override to
1229 * ensure that we read the correct state of LED beaconing represented
1230 * by led_override_timer_active
Easwar Hariharan409b1462016-02-26 13:33:28 -08001231 */
Easwar Hariharan22434722016-03-07 11:35:03 -08001232 smp_rmb();
Easwar Hariharan91ab4ed2016-02-03 14:35:57 -08001233 if (atomic_read(&ppd->led_override_timer_active)) {
1234 del_timer_sync(&ppd->led_override_timer);
1235 atomic_set(&ppd->led_override_timer_active, 0);
Easwar Hariharan22434722016-03-07 11:35:03 -08001236 /* Ensure the atomic_set is visible to all CPUs */
1237 smp_wmb();
Easwar Hariharan91ab4ed2016-02-03 14:35:57 -08001238 }
1239
Easwar Hariharan22434722016-03-07 11:35:03 -08001240 /* Hand control of the LED to the DC for normal operation */
1241 write_csr(dd, DCC_CFG_LED_CNTRL, 0);
Easwar Hariharan91ab4ed2016-02-03 14:35:57 -08001242}
Mike Marciniszyn77241052015-07-30 15:17:43 -04001243
1244static void run_led_override(unsigned long opaque)
1245{
1246 struct hfi1_pportdata *ppd = (struct hfi1_pportdata *)opaque;
1247 struct hfi1_devdata *dd = ppd->dd;
Easwar Hariharan91ab4ed2016-02-03 14:35:57 -08001248 unsigned long timeout;
1249 int phase_idx;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001250
1251 if (!(dd->flags & HFI1_INITTED))
1252 return;
1253
Easwar Hariharan91ab4ed2016-02-03 14:35:57 -08001254 phase_idx = ppd->led_override_phase & 1;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001255
Easwar Hariharan91ab4ed2016-02-03 14:35:57 -08001256 setextled(dd, phase_idx);
1257
1258 timeout = ppd->led_override_vals[phase_idx];
Easwar Hariharan22434722016-03-07 11:35:03 -08001259
Easwar Hariharan91ab4ed2016-02-03 14:35:57 -08001260 /* Set up for next phase */
1261 ppd->led_override_phase = !ppd->led_override_phase;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001262
Easwar Hariharan22434722016-03-07 11:35:03 -08001263 mod_timer(&ppd->led_override_timer, jiffies + timeout);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001264}
1265
Easwar Hariharan91ab4ed2016-02-03 14:35:57 -08001266/*
1267 * To have the LED blink in a particular pattern, provide timeon and timeoff
Easwar Hariharan22434722016-03-07 11:35:03 -08001268 * in milliseconds.
1269 * To turn off custom blinking and return to normal operation, use
1270 * shutdown_led_override()
Easwar Hariharan91ab4ed2016-02-03 14:35:57 -08001271 */
Easwar Hariharan22434722016-03-07 11:35:03 -08001272void hfi1_start_led_override(struct hfi1_pportdata *ppd, unsigned int timeon,
1273 unsigned int timeoff)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001274{
Easwar Hariharan22434722016-03-07 11:35:03 -08001275 if (!(ppd->dd->flags & HFI1_INITTED))
Mike Marciniszyn77241052015-07-30 15:17:43 -04001276 return;
1277
Easwar Hariharan91ab4ed2016-02-03 14:35:57 -08001278 /* Convert to jiffies for direct use in timer */
1279 ppd->led_override_vals[0] = msecs_to_jiffies(timeoff);
1280 ppd->led_override_vals[1] = msecs_to_jiffies(timeon);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001281
Easwar Hariharan22434722016-03-07 11:35:03 -08001282 /* Arbitrarily start from LED on phase */
1283 ppd->led_override_phase = 1;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001284
1285 /*
1286 * If the timer has not already been started, do so. Use a "quick"
Easwar Hariharan22434722016-03-07 11:35:03 -08001287 * timeout so the handler will be called soon to look at our request.
Mike Marciniszyn77241052015-07-30 15:17:43 -04001288 */
Easwar Hariharan22434722016-03-07 11:35:03 -08001289 if (!timer_pending(&ppd->led_override_timer)) {
Muhammad Falak R Wania3faf6062015-10-25 16:13:24 +05301290 setup_timer(&ppd->led_override_timer, run_led_override,
Jubin John17fb4f22016-02-14 20:21:52 -08001291 (unsigned long)ppd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001292 ppd->led_override_timer.expires = jiffies + 1;
1293 add_timer(&ppd->led_override_timer);
Easwar Hariharan22434722016-03-07 11:35:03 -08001294 atomic_set(&ppd->led_override_timer_active, 1);
1295 /* Ensure the atomic_set is visible to all CPUs */
1296 smp_wmb();
Mike Marciniszyn77241052015-07-30 15:17:43 -04001297 }
1298}
1299
1300/**
1301 * hfi1_reset_device - reset the chip if possible
1302 * @unit: the device to reset
1303 *
1304 * Whether or not reset is successful, we attempt to re-initialize the chip
1305 * (that is, much like a driver unload/reload). We clear the INITTED flag
1306 * so that the various entry points will fail until we reinitialize. For
1307 * now, we only allow this if no user contexts are open that use chip resources
1308 */
1309int hfi1_reset_device(int unit)
1310{
Michael J. Ruhle6f76222017-07-24 07:45:55 -07001311 int ret;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001312 struct hfi1_devdata *dd = hfi1_lookup(unit);
1313 struct hfi1_pportdata *ppd;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001314 int pidx;
1315
1316 if (!dd) {
1317 ret = -ENODEV;
1318 goto bail;
1319 }
1320
1321 dd_dev_info(dd, "Reset on unit %u requested\n", unit);
1322
Mike Marciniszyncb51c5d2017-07-24 07:45:31 -07001323 if (!dd->kregbase1 || !(dd->flags & HFI1_PRESENT)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001324 dd_dev_info(dd,
Jubin John17fb4f22016-02-14 20:21:52 -08001325 "Invalid unit number %u or not initialized or not present\n",
1326 unit);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001327 ret = -ENXIO;
1328 goto bail;
1329 }
1330
Michael J. Ruhld295dbe2017-08-04 13:52:44 -07001331 /* If there are any user/vnic contexts, we cannot reset */
1332 mutex_lock(&hfi1_mutex);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001333 if (dd->rcd)
Michael J. Ruhld295dbe2017-08-04 13:52:44 -07001334 if (hfi1_stats.sps_ctxts) {
1335 mutex_unlock(&hfi1_mutex);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001336 ret = -EBUSY;
1337 goto bail;
1338 }
Michael J. Ruhld295dbe2017-08-04 13:52:44 -07001339 mutex_unlock(&hfi1_mutex);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001340
1341 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
1342 ppd = dd->pport + pidx;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001343
Easwar Hariharan91ab4ed2016-02-03 14:35:57 -08001344 shutdown_led_override(ppd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001345 }
1346 if (dd->flags & HFI1_HAS_SEND_DMA)
1347 sdma_exit(dd);
1348
1349 hfi1_reset_cpu_counters(dd);
1350
1351 ret = hfi1_init(dd, 1);
1352
1353 if (ret)
1354 dd_dev_err(dd,
Jubin John17fb4f22016-02-14 20:21:52 -08001355 "Reinitialize unit %u after reset failed with %d\n",
1356 unit, ret);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001357 else
1358 dd_dev_info(dd, "Reinitialized unit %u after resetting\n",
Jubin John17fb4f22016-02-14 20:21:52 -08001359 unit);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001360
1361bail:
1362 return ret;
1363}
1364
Don Hiatt90397462017-05-12 09:20:20 -07001365static inline void hfi1_setup_ib_header(struct hfi1_packet *packet)
1366{
1367 packet->hdr = (struct hfi1_ib_message_header *)
1368 hfi1_get_msgheader(packet->rcd->dd,
1369 packet->rhf_addr);
1370 packet->hlen = (u8 *)packet->rhf_addr - (u8 *)packet->hdr;
1371}
1372
1373static int hfi1_setup_9B_packet(struct hfi1_packet *packet)
1374{
1375 struct hfi1_ibport *ibp = rcd_to_iport(packet->rcd);
1376 struct ib_header *hdr;
1377 u8 lnh;
1378
1379 hfi1_setup_ib_header(packet);
1380 hdr = packet->hdr;
1381
1382 lnh = ib_get_lnh(hdr);
1383 if (lnh == HFI1_LRH_BTH) {
1384 packet->ohdr = &hdr->u.oth;
1385 packet->grh = NULL;
1386 } else if (lnh == HFI1_LRH_GRH) {
1387 u32 vtf;
1388
1389 packet->ohdr = &hdr->u.l.oth;
1390 packet->grh = &hdr->u.l.grh;
1391 if (packet->grh->next_hdr != IB_GRH_NEXT_HDR)
1392 goto drop;
1393 vtf = be32_to_cpu(packet->grh->version_tclass_flow);
1394 if ((vtf >> IB_GRH_VERSION_SHIFT) != IB_GRH_VERSION)
1395 goto drop;
1396 } else {
1397 goto drop;
1398 }
1399
1400 /* Query commonly used fields from packet header */
Don Hiatt72c07e22017-08-04 13:53:58 -07001401 packet->payload = packet->ebuf;
Don Hiatt90397462017-05-12 09:20:20 -07001402 packet->opcode = ib_bth_get_opcode(packet->ohdr);
1403 packet->slid = ib_get_slid(hdr);
1404 packet->dlid = ib_get_dlid(hdr);
Don Hiatt72c07e22017-08-04 13:53:58 -07001405 if (unlikely((packet->dlid >= be16_to_cpu(IB_MULTICAST_LID_BASE)) &&
1406 (packet->dlid != be16_to_cpu(IB_LID_PERMISSIVE))))
1407 packet->dlid += opa_get_mcast_base(OPA_MCAST_NR) -
1408 be16_to_cpu(IB_MULTICAST_LID_BASE);
Don Hiatt90397462017-05-12 09:20:20 -07001409 packet->sl = ib_get_sl(hdr);
1410 packet->sc = hfi1_9B_get_sc5(hdr, packet->rhf);
1411 packet->pad = ib_bth_get_pad(packet->ohdr);
1412 packet->extra_byte = 0;
1413 packet->fecn = ib_bth_get_fecn(packet->ohdr);
1414 packet->becn = ib_bth_get_becn(packet->ohdr);
1415
1416 return 0;
1417drop:
1418 ibp->rvp.n_pkt_drops++;
1419 return -EINVAL;
1420}
1421
Don Hiatt72c07e22017-08-04 13:53:58 -07001422static int hfi1_setup_bypass_packet(struct hfi1_packet *packet)
1423{
1424 /*
1425 * Bypass packets have a different header/payload split
1426 * compared to an IB packet.
1427 * Current split is set such that 16 bytes of the actual
1428 * header is in the header buffer and the remining is in
1429 * the eager buffer. We chose 16 since hfi1 driver only
1430 * supports 16B bypass packets and we will be able to
1431 * receive the entire LRH with such a split.
1432 */
1433
1434 struct hfi1_ctxtdata *rcd = packet->rcd;
1435 struct hfi1_pportdata *ppd = rcd->ppd;
1436 struct hfi1_ibport *ibp = &ppd->ibport_data;
1437 u8 l4;
1438 u8 grh_len;
1439
1440 packet->hdr = (struct hfi1_16b_header *)
1441 hfi1_get_16B_header(packet->rcd->dd,
1442 packet->rhf_addr);
1443 packet->hlen = (u8 *)packet->rhf_addr - (u8 *)packet->hdr;
1444
1445 l4 = hfi1_16B_get_l4(packet->hdr);
1446 if (l4 == OPA_16B_L4_IB_LOCAL) {
1447 grh_len = 0;
1448 packet->ohdr = packet->ebuf;
1449 packet->grh = NULL;
1450 } else if (l4 == OPA_16B_L4_IB_GLOBAL) {
1451 u32 vtf;
1452
1453 grh_len = sizeof(struct ib_grh);
1454 packet->ohdr = packet->ebuf + grh_len;
1455 packet->grh = packet->ebuf;
1456 if (packet->grh->next_hdr != IB_GRH_NEXT_HDR)
1457 goto drop;
1458 vtf = be32_to_cpu(packet->grh->version_tclass_flow);
1459 if ((vtf >> IB_GRH_VERSION_SHIFT) != IB_GRH_VERSION)
1460 goto drop;
1461 } else {
1462 goto drop;
1463 }
1464
1465 /* Query commonly used fields from packet header */
1466 packet->opcode = ib_bth_get_opcode(packet->ohdr);
1467 packet->hlen = hdr_len_by_opcode[packet->opcode] + 8 + grh_len;
1468 packet->payload = packet->ebuf + packet->hlen - (4 * sizeof(u32));
1469 packet->slid = hfi1_16B_get_slid(packet->hdr);
1470 packet->dlid = hfi1_16B_get_dlid(packet->hdr);
1471 if (unlikely(hfi1_is_16B_mcast(packet->dlid)))
1472 packet->dlid += opa_get_mcast_base(OPA_MCAST_NR) -
1473 opa_get_lid(opa_get_mcast_base(OPA_MCAST_NR),
1474 16B);
1475 packet->sc = hfi1_16B_get_sc(packet->hdr);
1476 packet->sl = ibp->sc_to_sl[packet->sc];
1477 packet->pad = hfi1_16B_bth_get_pad(packet->ohdr);
1478 packet->extra_byte = SIZE_OF_LT;
1479 packet->fecn = hfi1_16B_get_fecn(packet->hdr);
1480 packet->becn = hfi1_16B_get_becn(packet->hdr);
1481
1482 return 0;
1483drop:
1484 hfi1_cdbg(PKT, "%s: packet dropped\n", __func__);
1485 ibp->rvp.n_pkt_drops++;
1486 return -EINVAL;
1487}
1488
Mike Marciniszyn77241052015-07-30 15:17:43 -04001489void handle_eflags(struct hfi1_packet *packet)
1490{
1491 struct hfi1_ctxtdata *rcd = packet->rcd;
1492 u32 rte = rhf_rcv_type_err(packet->rhf);
1493
Mike Marciniszyn77241052015-07-30 15:17:43 -04001494 rcv_hdrerr(rcd, rcd->ppd, packet);
Ignacio Hernandeza03a03e2015-11-06 20:07:03 -05001495 if (rhf_err_flags(packet->rhf))
1496 dd_dev_err(rcd->dd,
1497 "receive context %d: rhf 0x%016llx, errs [ %s%s%s%s%s%s%s%s] rte 0x%x\n",
1498 rcd->ctxt, packet->rhf,
1499 packet->rhf & RHF_K_HDR_LEN_ERR ? "k_hdr_len " : "",
1500 packet->rhf & RHF_DC_UNC_ERR ? "dc_unc " : "",
1501 packet->rhf & RHF_DC_ERR ? "dc " : "",
1502 packet->rhf & RHF_TID_ERR ? "tid " : "",
1503 packet->rhf & RHF_LEN_ERR ? "len " : "",
1504 packet->rhf & RHF_ECC_ERR ? "ecc " : "",
1505 packet->rhf & RHF_VCRC_ERR ? "vcrc " : "",
1506 packet->rhf & RHF_ICRC_ERR ? "icrc " : "",
1507 rte);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001508}
1509
1510/*
1511 * The following functions are called by the interrupt handler. They are type
1512 * specific handlers for each packet type.
1513 */
1514int process_receive_ib(struct hfi1_packet *packet)
1515{
Don Hiatt0181ce32017-03-20 17:26:14 -07001516 if (unlikely(hfi1_dbg_fault_packet(packet)))
1517 return RHF_RCV_CONTINUE;
1518
Don Hiatt90397462017-05-12 09:20:20 -07001519 if (hfi1_setup_9B_packet(packet))
1520 return RHF_RCV_CONTINUE;
1521
Mike Marciniszyn77241052015-07-30 15:17:43 -04001522 trace_hfi1_rcvhdr(packet->rcd->ppd->dd,
1523 packet->rcd->ctxt,
1524 rhf_err_flags(packet->rhf),
1525 RHF_RCV_TYPE_IB,
1526 packet->hlen,
1527 packet->tlen,
1528 packet->updegr,
1529 rhf_egr_index(packet->rhf));
1530
Don Hiatt243d9f42017-03-20 17:26:20 -07001531 if (unlikely(
1532 (hfi1_dbg_fault_suppress_err(&packet->rcd->dd->verbs_dev) &&
1533 (packet->rhf & RHF_DC_ERR))))
1534 return RHF_RCV_CONTINUE;
1535
Mike Marciniszyn77241052015-07-30 15:17:43 -04001536 if (unlikely(rhf_err_flags(packet->rhf))) {
1537 handle_eflags(packet);
1538 return RHF_RCV_CONTINUE;
1539 }
1540
1541 hfi1_ib_rcv(packet);
1542 return RHF_RCV_CONTINUE;
1543}
1544
Vishwanathapura, Niranjanad4829ea2017-04-12 20:29:28 -07001545static inline bool hfi1_is_vnic_packet(struct hfi1_packet *packet)
1546{
1547 /* Packet received in VNIC context via RSM */
1548 if (packet->rcd->is_vnic)
1549 return true;
1550
Don Hiatt72c07e22017-08-04 13:53:58 -07001551 if ((hfi1_16B_get_l2(packet->ebuf) == OPA_16B_L2_TYPE) &&
1552 (hfi1_16B_get_l4(packet->ebuf) == OPA_16B_L4_ETHR))
Vishwanathapura, Niranjanad4829ea2017-04-12 20:29:28 -07001553 return true;
1554
1555 return false;
1556}
1557
Mike Marciniszyn77241052015-07-30 15:17:43 -04001558int process_receive_bypass(struct hfi1_packet *packet)
1559{
Jakub Pawlak505efe32016-10-25 13:12:17 -07001560 struct hfi1_devdata *dd = packet->rcd->dd;
1561
Don Hiatt72c07e22017-08-04 13:53:58 -07001562 if (hfi1_is_vnic_packet(packet)) {
Vishwanathapura, Niranjanad4829ea2017-04-12 20:29:28 -07001563 hfi1_vnic_bypass_rcv(packet);
1564 return RHF_RCV_CONTINUE;
1565 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001566
Don Hiatt72c07e22017-08-04 13:53:58 -07001567 if (hfi1_setup_bypass_packet(packet))
1568 return RHF_RCV_CONTINUE;
Jakub Pawlak505efe32016-10-25 13:12:17 -07001569
Don Hiatt72c07e22017-08-04 13:53:58 -07001570 if (unlikely(rhf_err_flags(packet->rhf))) {
1571 handle_eflags(packet);
1572 return RHF_RCV_CONTINUE;
1573 }
1574
1575 if (hfi1_16B_get_l2(packet->hdr) == 0x2) {
1576 hfi1_16B_rcv(packet);
1577 } else {
1578 dd_dev_err(dd,
1579 "Bypass packets other than 16B are not supported in normal operation. Dropping\n");
1580 incr_cntr64(&dd->sw_rcv_bypass_packet_errors);
1581 if (!(dd->err_info_rcvport.status_and_code &
1582 OPA_EI_STATUS_SMASK)) {
1583 u64 *flits = packet->ebuf;
1584
1585 if (flits && !(packet->rhf & RHF_LEN_ERR)) {
1586 dd->err_info_rcvport.packet_flit1 = flits[0];
1587 dd->err_info_rcvport.packet_flit2 =
1588 packet->tlen > sizeof(flits[0]) ?
1589 flits[1] : 0;
1590 }
1591 dd->err_info_rcvport.status_and_code |=
1592 (OPA_EI_STATUS_SMASK | BAD_L2_ERR);
Jakub Pawlak505efe32016-10-25 13:12:17 -07001593 }
Jakub Pawlak505efe32016-10-25 13:12:17 -07001594 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001595 return RHF_RCV_CONTINUE;
1596}
1597
1598int process_receive_error(struct hfi1_packet *packet)
1599{
Don Hiatt243d9f42017-03-20 17:26:20 -07001600 /* KHdrHCRCErr -- KDETH packet with a bad HCRC */
1601 if (unlikely(
1602 hfi1_dbg_fault_suppress_err(&packet->rcd->dd->verbs_dev) &&
1603 rhf_rcv_type_err(packet->rhf) == 3))
1604 return RHF_RCV_CONTINUE;
1605
Don Hiatt90397462017-05-12 09:20:20 -07001606 hfi1_setup_ib_header(packet);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001607 handle_eflags(packet);
1608
1609 if (unlikely(rhf_err_flags(packet->rhf)))
1610 dd_dev_err(packet->rcd->dd,
1611 "Unhandled error packet received. Dropping.\n");
1612
1613 return RHF_RCV_CONTINUE;
1614}
1615
1616int kdeth_process_expected(struct hfi1_packet *packet)
1617{
Don Hiatt0181ce32017-03-20 17:26:14 -07001618 if (unlikely(hfi1_dbg_fault_packet(packet)))
1619 return RHF_RCV_CONTINUE;
Don Hiatt90397462017-05-12 09:20:20 -07001620
1621 hfi1_setup_ib_header(packet);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001622 if (unlikely(rhf_err_flags(packet->rhf)))
1623 handle_eflags(packet);
1624
1625 dd_dev_err(packet->rcd->dd,
1626 "Unhandled expected packet received. Dropping.\n");
1627 return RHF_RCV_CONTINUE;
1628}
1629
1630int kdeth_process_eager(struct hfi1_packet *packet)
1631{
Don Hiatt90397462017-05-12 09:20:20 -07001632 hfi1_setup_ib_header(packet);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001633 if (unlikely(rhf_err_flags(packet->rhf)))
1634 handle_eflags(packet);
Don Hiatt0181ce32017-03-20 17:26:14 -07001635 if (unlikely(hfi1_dbg_fault_packet(packet)))
1636 return RHF_RCV_CONTINUE;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001637
1638 dd_dev_err(packet->rcd->dd,
1639 "Unhandled eager packet received. Dropping.\n");
1640 return RHF_RCV_CONTINUE;
1641}
1642
1643int process_receive_invalid(struct hfi1_packet *packet)
1644{
1645 dd_dev_err(packet->rcd->dd, "Invalid packet type %d. Dropping\n",
Jubin John17fb4f22016-02-14 20:21:52 -08001646 rhf_rcv_type(packet->rhf));
Mike Marciniszyn77241052015-07-30 15:17:43 -04001647 return RHF_RCV_CONTINUE;
1648}