staging/rdma/hfi1: Check interrupt registers mapping
[linux-2.6-block.git] / drivers / staging / rdma / hfi1 / driver.c
CommitLineData
77241056 1/*
05d6ac1d 2 * Copyright(c) 2015, 2016 Intel Corporation.
77241056
MM
3 *
4 * This file is provided under a dual BSD/GPLv2 license. When using or
5 * redistributing this file, you may do so under either license.
6 *
7 * GPL LICENSE SUMMARY
8 *
77241056
MM
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of version 2 of the GNU General Public License as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * BSD LICENSE
19 *
77241056
MM
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
22 * are met:
23 *
24 * - Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * - Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in
28 * the documentation and/or other materials provided with the
29 * distribution.
30 * - Neither the name of Intel Corporation nor the names of its
31 * contributors may be used to endorse or promote products derived
32 * from this software without specific prior written permission.
33 *
34 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
35 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
37 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
38 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
41 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
42 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
44 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45 *
46 */
47
48#include <linux/spinlock.h>
49#include <linux/pci.h>
50#include <linux/io.h>
51#include <linux/delay.h>
52#include <linux/netdevice.h>
53#include <linux/vmalloc.h>
54#include <linux/module.h>
55#include <linux/prefetch.h>
8859b4a6 56#include <rdma/ib_verbs.h>
77241056
MM
57
58#include "hfi.h"
59#include "trace.h"
60#include "qp.h"
61#include "sdma.h"
62
63#undef pr_fmt
64#define pr_fmt(fmt) DRIVER_NAME ": " fmt
65
66/*
67 * The size has to be longer than this string, so we can append
68 * board/chip information to it in the initialization code.
69 */
70const char ib_hfi1_version[] = HFI1_DRIVER_VERSION "\n";
71
72DEFINE_SPINLOCK(hfi1_devs_lock);
73LIST_HEAD(hfi1_dev_list);
74DEFINE_MUTEX(hfi1_mutex); /* general driver use */
75
76unsigned int hfi1_max_mtu = HFI1_DEFAULT_MAX_MTU;
77module_param_named(max_mtu, hfi1_max_mtu, uint, S_IRUGO);
78MODULE_PARM_DESC(max_mtu, "Set max MTU bytes, default is 8192");
79
80unsigned int hfi1_cu = 1;
81module_param_named(cu, hfi1_cu, uint, S_IRUGO);
82MODULE_PARM_DESC(cu, "Credit return units");
83
84unsigned long hfi1_cap_mask = HFI1_CAP_MASK_DEFAULT;
85static int hfi1_caps_set(const char *, const struct kernel_param *);
86static int hfi1_caps_get(char *, const struct kernel_param *);
87static const struct kernel_param_ops cap_ops = {
88 .set = hfi1_caps_set,
89 .get = hfi1_caps_get
90};
91module_param_cb(cap_mask, &cap_ops, &hfi1_cap_mask, S_IWUSR | S_IRUGO);
92MODULE_PARM_DESC(cap_mask, "Bit mask of enabled/disabled HW features");
93
94MODULE_LICENSE("Dual BSD/GPL");
95MODULE_DESCRIPTION("Intel Omni-Path Architecture driver");
96MODULE_VERSION(HFI1_DRIVER_VERSION);
97
98/*
99 * MAX_PKT_RCV is the max # if packets processed per receive interrupt.
100 */
101#define MAX_PKT_RECV 64
102#define EGR_HEAD_UPDATE_THRESHOLD 16
103
104struct hfi1_ib_stats hfi1_stats;
105
106static int hfi1_caps_set(const char *val, const struct kernel_param *kp)
107{
108 int ret = 0;
109 unsigned long *cap_mask_ptr = (unsigned long *)kp->arg,
110 cap_mask = *cap_mask_ptr, value, diff,
111 write_mask = ((HFI1_CAP_WRITABLE_MASK << HFI1_CAP_USER_SHIFT) |
112 HFI1_CAP_WRITABLE_MASK);
113
114 ret = kstrtoul(val, 0, &value);
115 if (ret) {
116 pr_warn("Invalid module parameter value for 'cap_mask'\n");
117 goto done;
118 }
119 /* Get the changed bits (except the locked bit) */
120 diff = value ^ (cap_mask & ~HFI1_CAP_LOCKED_SMASK);
121
122 /* Remove any bits that are not allowed to change after driver load */
123 if (HFI1_CAP_LOCKED() && (diff & ~write_mask)) {
124 pr_warn("Ignoring non-writable capability bits %#lx\n",
125 diff & ~write_mask);
126 diff &= write_mask;
127 }
128
129 /* Mask off any reserved bits */
130 diff &= ~HFI1_CAP_RESERVED_MASK;
131 /* Clear any previously set and changing bits */
132 cap_mask &= ~diff;
133 /* Update the bits with the new capability */
134 cap_mask |= (value & diff);
135 /* Check for any kernel/user restrictions */
136 diff = (cap_mask & (HFI1_CAP_MUST_HAVE_KERN << HFI1_CAP_USER_SHIFT)) ^
137 ((cap_mask & HFI1_CAP_MUST_HAVE_KERN) << HFI1_CAP_USER_SHIFT);
138 cap_mask &= ~diff;
139 /* Set the bitmask to the final set */
140 *cap_mask_ptr = cap_mask;
141done:
142 return ret;
143}
144
145static int hfi1_caps_get(char *buffer, const struct kernel_param *kp)
146{
147 unsigned long cap_mask = *(unsigned long *)kp->arg;
148
149 cap_mask &= ~HFI1_CAP_LOCKED_SMASK;
150 cap_mask |= ((cap_mask & HFI1_CAP_K2U) << HFI1_CAP_USER_SHIFT);
151
152 return scnprintf(buffer, PAGE_SIZE, "0x%lx", cap_mask);
153}
154
155const char *get_unit_name(int unit)
156{
157 static char iname[16];
158
9805071e 159 snprintf(iname, sizeof(iname), DRIVER_NAME "_%u", unit);
77241056
MM
160 return iname;
161}
162
49dbb6cf
DD
163const char *get_card_name(struct rvt_dev_info *rdi)
164{
165 struct hfi1_ibdev *ibdev = container_of(rdi, struct hfi1_ibdev, rdi);
166 struct hfi1_devdata *dd = container_of(ibdev,
167 struct hfi1_devdata, verbs_dev);
168 return get_unit_name(dd->unit);
169}
170
171struct pci_dev *get_pci_dev(struct rvt_dev_info *rdi)
172{
173 struct hfi1_ibdev *ibdev = container_of(rdi, struct hfi1_ibdev, rdi);
174 struct hfi1_devdata *dd = container_of(ibdev,
175 struct hfi1_devdata, verbs_dev);
176 return dd->pcidev;
177}
178
77241056
MM
179/*
180 * Return count of units with at least one port ACTIVE.
181 */
182int hfi1_count_active_units(void)
183{
184 struct hfi1_devdata *dd;
185 struct hfi1_pportdata *ppd;
186 unsigned long flags;
187 int pidx, nunits_active = 0;
188
189 spin_lock_irqsave(&hfi1_devs_lock, flags);
190 list_for_each_entry(dd, &hfi1_dev_list, list) {
191 if (!(dd->flags & HFI1_PRESENT) || !dd->kregbase)
192 continue;
193 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
194 ppd = dd->pport + pidx;
195 if (ppd->lid && ppd->linkup) {
196 nunits_active++;
197 break;
198 }
199 }
200 }
201 spin_unlock_irqrestore(&hfi1_devs_lock, flags);
202 return nunits_active;
203}
204
205/*
206 * Return count of all units, optionally return in arguments
207 * the number of usable (present) units, and the number of
208 * ports that are up.
209 */
210int hfi1_count_units(int *npresentp, int *nupp)
211{
212 int nunits = 0, npresent = 0, nup = 0;
213 struct hfi1_devdata *dd;
214 unsigned long flags;
215 int pidx;
216 struct hfi1_pportdata *ppd;
217
218 spin_lock_irqsave(&hfi1_devs_lock, flags);
219
220 list_for_each_entry(dd, &hfi1_dev_list, list) {
221 nunits++;
222 if ((dd->flags & HFI1_PRESENT) && dd->kregbase)
223 npresent++;
224 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
225 ppd = dd->pport + pidx;
226 if (ppd->lid && ppd->linkup)
227 nup++;
228 }
229 }
230
231 spin_unlock_irqrestore(&hfi1_devs_lock, flags);
232
233 if (npresentp)
234 *npresentp = npresent;
235 if (nupp)
236 *nupp = nup;
237
238 return nunits;
239}
240
241/*
242 * Get address of eager buffer from it's index (allocated in chunks, not
243 * contiguous).
244 */
245static inline void *get_egrbuf(const struct hfi1_ctxtdata *rcd, u64 rhf,
246 u8 *update)
247{
248 u32 idx = rhf_egr_index(rhf), offset = rhf_egr_buf_offset(rhf);
249
250 *update |= !(idx & (rcd->egrbufs.threshold - 1)) && !offset;
251 return (void *)(((u64)(rcd->egrbufs.rcvtids[idx].addr)) +
252 (offset * RCV_BUF_BLOCK_SIZE));
253}
254
255/*
256 * Validate and encode the a given RcvArray Buffer size.
257 * The function will check whether the given size falls within
258 * allowed size ranges for the respective type and, optionally,
259 * return the proper encoding.
260 */
261inline int hfi1_rcvbuf_validate(u32 size, u8 type, u16 *encoded)
262{
263 if (unlikely(!IS_ALIGNED(size, PAGE_SIZE)))
264 return 0;
265 if (unlikely(size < MIN_EAGER_BUFFER))
266 return 0;
267 if (size >
268 (type == PT_EAGER ? MAX_EAGER_BUFFER : MAX_EXPECTED_BUFFER))
269 return 0;
270 if (encoded)
271 *encoded = ilog2(size / PAGE_SIZE) + 1;
272 return 1;
273}
274
275static void rcv_hdrerr(struct hfi1_ctxtdata *rcd, struct hfi1_pportdata *ppd,
276 struct hfi1_packet *packet)
277{
278 struct hfi1_message_header *rhdr = packet->hdr;
279 u32 rte = rhf_rcv_type_err(packet->rhf);
280 int lnh = be16_to_cpu(rhdr->lrh[0]) & 3;
281 struct hfi1_ibport *ibp = &ppd->ibport_data;
ec4274f1
DD
282 struct hfi1_devdata *dd = ppd->dd;
283 struct rvt_dev_info *rdi = &dd->verbs_dev.rdi;
77241056
MM
284
285 if (packet->rhf & (RHF_VCRC_ERR | RHF_ICRC_ERR))
286 return;
287
288 if (packet->rhf & RHF_TID_ERR) {
289 /* For TIDERR and RC QPs preemptively schedule a NAK */
290 struct hfi1_ib_header *hdr = (struct hfi1_ib_header *)rhdr;
291 struct hfi1_other_headers *ohdr = NULL;
292 u32 tlen = rhf_pkt_len(packet->rhf); /* in bytes */
293 u16 lid = be16_to_cpu(hdr->lrh[1]);
294 u32 qp_num;
295 u32 rcv_flags = 0;
296
297 /* Sanity check packet */
298 if (tlen < 24)
299 goto drop;
300
301 /* Check for GRH */
e490974e 302 if (lnh == HFI1_LRH_BTH) {
77241056 303 ohdr = &hdr->u.oth;
e490974e 304 } else if (lnh == HFI1_LRH_GRH) {
77241056
MM
305 u32 vtf;
306
307 ohdr = &hdr->u.l.oth;
308 if (hdr->u.l.grh.next_hdr != IB_GRH_NEXT_HDR)
309 goto drop;
310 vtf = be32_to_cpu(hdr->u.l.grh.version_tclass_flow);
311 if ((vtf >> IB_GRH_VERSION_SHIFT) != IB_GRH_VERSION)
312 goto drop;
313 rcv_flags |= HFI1_HAS_GRH;
e490974e 314 } else {
77241056 315 goto drop;
e490974e 316 }
77241056 317 /* Get the destination QP number. */
ec4274f1 318 qp_num = be32_to_cpu(ohdr->bth[1]) & RVT_QPN_MASK;
8859b4a6 319 if (lid < be16_to_cpu(IB_MULTICAST_LID_BASE)) {
895420dd 320 struct rvt_qp *qp;
b77d713a 321 unsigned long flags;
77241056
MM
322
323 rcu_read_lock();
ec4274f1 324 qp = rvt_lookup_qpn(rdi, &ibp->rvp, qp_num);
77241056
MM
325 if (!qp) {
326 rcu_read_unlock();
327 goto drop;
328 }
329
330 /*
331 * Handle only RC QPs - for other QP types drop error
332 * packet.
333 */
b77d713a 334 spin_lock_irqsave(&qp->r_lock, flags);
77241056
MM
335
336 /* Check for valid receive state. */
83693bd1
DD
337 if (!(ib_rvt_state_ops[qp->state] &
338 RVT_PROCESS_RECV_OK)) {
4eb06882 339 ibp->rvp.n_pkt_drops++;
77241056
MM
340 }
341
342 switch (qp->ibqp.qp_type) {
343 case IB_QPT_RC:
344 hfi1_rc_hdrerr(
345 rcd,
346 hdr,
347 rcv_flags,
348 qp);
349 break;
350 default:
351 /* For now don't handle any other QP types */
352 break;
353 }
354
b77d713a 355 spin_unlock_irqrestore(&qp->r_lock, flags);
77241056
MM
356 rcu_read_unlock();
357 } /* Unicast QP */
358 } /* Valid packet with TIDErr */
359
360 /* handle "RcvTypeErr" flags */
361 switch (rte) {
362 case RHF_RTE_ERROR_OP_CODE_ERR:
363 {
364 u32 opcode;
365 void *ebuf = NULL;
366 __be32 *bth = NULL;
367
368 if (rhf_use_egr_bfr(packet->rhf))
369 ebuf = packet->ebuf;
370
d125a6c6 371 if (!ebuf)
77241056
MM
372 goto drop; /* this should never happen */
373
374 if (lnh == HFI1_LRH_BTH)
375 bth = (__be32 *)ebuf;
376 else if (lnh == HFI1_LRH_GRH)
377 bth = (__be32 *)((char *)ebuf + sizeof(struct ib_grh));
378 else
379 goto drop;
380
381 opcode = be32_to_cpu(bth[0]) >> 24;
382 opcode &= 0xff;
383
384 if (opcode == IB_OPCODE_CNP) {
385 /*
386 * Only in pre-B0 h/w is the CNP_OPCODE handled
624be1db 387 * via this code path.
77241056 388 */
895420dd 389 struct rvt_qp *qp = NULL;
77241056
MM
390 u32 lqpn, rqpn;
391 u16 rlid;
392 u8 svc_type, sl, sc5;
393
394 sc5 = (be16_to_cpu(rhdr->lrh[0]) >> 12) & 0xf;
395 if (rhf_dc_info(packet->rhf))
396 sc5 |= 0x10;
397 sl = ibp->sc_to_sl[sc5];
398
ec4274f1 399 lqpn = be32_to_cpu(bth[1]) & RVT_QPN_MASK;
77241056 400 rcu_read_lock();
ec4274f1 401 qp = rvt_lookup_qpn(rdi, &ibp->rvp, lqpn);
d125a6c6 402 if (!qp) {
77241056
MM
403 rcu_read_unlock();
404 goto drop;
405 }
406
407 switch (qp->ibqp.qp_type) {
408 case IB_QPT_UD:
409 rlid = 0;
410 rqpn = 0;
411 svc_type = IB_CC_SVCTYPE_UD;
412 break;
413 case IB_QPT_UC:
414 rlid = be16_to_cpu(rhdr->lrh[3]);
415 rqpn = qp->remote_qpn;
416 svc_type = IB_CC_SVCTYPE_UC;
417 break;
418 default:
419 goto drop;
420 }
421
422 process_becn(ppd, sl, rlid, lqpn, rqpn, svc_type);
423 rcu_read_unlock();
424 }
425
426 packet->rhf &= ~RHF_RCV_TYPE_ERR_SMASK;
427 break;
428 }
429 default:
430 break;
431 }
432
433drop:
434 return;
435}
436
437static inline void init_packet(struct hfi1_ctxtdata *rcd,
17fb4f29 438 struct hfi1_packet *packet)
77241056 439{
77241056
MM
440 packet->rsize = rcd->rcvhdrqentsize; /* words */
441 packet->maxcnt = rcd->rcvhdrq_cnt * packet->rsize; /* words */
442 packet->rcd = rcd;
443 packet->updegr = 0;
444 packet->etail = -1;
f4f30031 445 packet->rhf_addr = get_rhf_addr(rcd);
77241056
MM
446 packet->rhf = rhf_to_cpu(packet->rhf_addr);
447 packet->rhqoff = rcd->head;
448 packet->numpkt = 0;
449 packet->rcv_flags = 0;
450}
451
895420dd 452static void process_ecn(struct rvt_qp *qp, struct hfi1_ib_header *hdr,
77241056 453 struct hfi1_other_headers *ohdr,
977940b8 454 u64 rhf, u32 bth1, struct ib_grh *grh)
77241056
MM
455{
456 struct hfi1_ibport *ibp = to_iport(qp->ibqp.device, qp->port_num);
977940b8
AK
457 u32 rqpn = 0;
458 u16 rlid;
77241056 459 u8 sc5, svc_type;
77241056
MM
460
461 switch (qp->ibqp.qp_type) {
977940b8
AK
462 case IB_QPT_SMI:
463 case IB_QPT_GSI:
77241056 464 case IB_QPT_UD:
977940b8 465 rlid = be16_to_cpu(hdr->lrh[3]);
ec4274f1 466 rqpn = be32_to_cpu(ohdr->u.ud.deth[1]) & RVT_QPN_MASK;
77241056
MM
467 svc_type = IB_CC_SVCTYPE_UD;
468 break;
977940b8
AK
469 case IB_QPT_UC:
470 rlid = qp->remote_ah_attr.dlid;
471 rqpn = qp->remote_qpn;
472 svc_type = IB_CC_SVCTYPE_UC;
473 break;
474 case IB_QPT_RC:
475 rlid = qp->remote_ah_attr.dlid;
476 rqpn = qp->remote_qpn;
477 svc_type = IB_CC_SVCTYPE_RC;
478 break;
77241056
MM
479 default:
480 return;
481 }
482
77241056
MM
483 sc5 = (be16_to_cpu(hdr->lrh[0]) >> 12) & 0xf;
484 if (rhf_dc_info(rhf))
485 sc5 |= 0x10;
486
977940b8 487 if (bth1 & HFI1_FECN_SMASK) {
77241056
MM
488 u16 pkey = (u16)be32_to_cpu(ohdr->bth[0]);
489 u16 dlid = be16_to_cpu(hdr->lrh[1]);
77241056 490
977940b8 491 return_cnp(ibp, qp, rqpn, pkey, dlid, rlid, sc5, grh);
77241056
MM
492 }
493
977940b8 494 if (bth1 & HFI1_BECN_SMASK) {
77241056 495 struct hfi1_pportdata *ppd = ppd_from_ibp(ibp);
ec4274f1 496 u32 lqpn = bth1 & RVT_QPN_MASK;
77241056
MM
497 u8 sl = ibp->sc_to_sl[sc5];
498
977940b8 499 process_becn(ppd, sl, rlid, lqpn, rqpn, svc_type);
77241056 500 }
77241056
MM
501}
502
503struct ps_mdata {
504 struct hfi1_ctxtdata *rcd;
505 u32 rsize;
506 u32 maxcnt;
507 u32 ps_head;
508 u32 ps_tail;
509 u32 ps_seq;
510};
511
512static inline void init_ps_mdata(struct ps_mdata *mdata,
513 struct hfi1_packet *packet)
514{
515 struct hfi1_ctxtdata *rcd = packet->rcd;
516
517 mdata->rcd = rcd;
518 mdata->rsize = packet->rsize;
519 mdata->maxcnt = packet->maxcnt;
3e7ccca0 520 mdata->ps_head = packet->rhqoff;
77241056 521
82c2611d 522 if (HFI1_CAP_KGET_MASK(rcd->flags, DMA_RTAIL)) {
3e7ccca0 523 mdata->ps_tail = get_rcvhdrtail(rcd);
82c2611d
NV
524 if (rcd->ctxt == HFI1_CTRL_CTXT)
525 mdata->ps_seq = rcd->seq_cnt;
526 else
527 mdata->ps_seq = 0; /* not used with DMA_RTAIL */
77241056
MM
528 } else {
529 mdata->ps_tail = 0; /* used only with DMA_RTAIL*/
530 mdata->ps_seq = rcd->seq_cnt;
531 }
532}
533
82c2611d
NV
534static inline int ps_done(struct ps_mdata *mdata, u64 rhf,
535 struct hfi1_ctxtdata *rcd)
77241056 536{
82c2611d 537 if (HFI1_CAP_KGET_MASK(rcd->flags, DMA_RTAIL))
77241056
MM
538 return mdata->ps_head == mdata->ps_tail;
539 return mdata->ps_seq != rhf_rcv_seq(rhf);
540}
541
82c2611d
NV
542static inline int ps_skip(struct ps_mdata *mdata, u64 rhf,
543 struct hfi1_ctxtdata *rcd)
544{
545 /*
546 * Control context can potentially receive an invalid rhf.
547 * Drop such packets.
548 */
549 if ((rcd->ctxt == HFI1_CTRL_CTXT) && (mdata->ps_head != mdata->ps_tail))
550 return mdata->ps_seq != rhf_rcv_seq(rhf);
551
552 return 0;
553}
554
555static inline void update_ps_mdata(struct ps_mdata *mdata,
556 struct hfi1_ctxtdata *rcd)
77241056 557{
77241056 558 mdata->ps_head += mdata->rsize;
3e7ccca0 559 if (mdata->ps_head >= mdata->maxcnt)
77241056 560 mdata->ps_head = 0;
82c2611d
NV
561
562 /* Control context must do seq counting */
563 if (!HFI1_CAP_KGET_MASK(rcd->flags, DMA_RTAIL) ||
564 (rcd->ctxt == HFI1_CTRL_CTXT)) {
77241056
MM
565 if (++mdata->ps_seq > 13)
566 mdata->ps_seq = 1;
567 }
568}
569
570/*
571 * prescan_rxq - search through the receive queue looking for packets
572 * containing Excplicit Congestion Notifications (FECNs, or BECNs).
573 * When an ECN is found, process the Congestion Notification, and toggle
574 * it off.
6c9e50f8
VM
575 * This is declared as a macro to allow quick checking of the port to avoid
576 * the overhead of a function call if not enabled.
77241056 577 */
6c9e50f8
VM
578#define prescan_rxq(rcd, packet) \
579 do { \
580 if (rcd->ppd->cc_prescan) \
581 __prescan_rxq(packet); \
582 } while (0)
583static void __prescan_rxq(struct hfi1_packet *packet)
77241056
MM
584{
585 struct hfi1_ctxtdata *rcd = packet->rcd;
586 struct ps_mdata mdata;
587
77241056
MM
588 init_ps_mdata(&mdata, packet);
589
590 while (1) {
591 struct hfi1_devdata *dd = rcd->dd;
592 struct hfi1_ibport *ibp = &rcd->ppd->ibport_data;
50e5dcbe 593 __le32 *rhf_addr = (__le32 *)rcd->rcvhdrq + mdata.ps_head +
77241056 594 dd->rhf_offset;
895420dd 595 struct rvt_qp *qp;
77241056
MM
596 struct hfi1_ib_header *hdr;
597 struct hfi1_other_headers *ohdr;
598 struct ib_grh *grh = NULL;
ec4274f1 599 struct rvt_dev_info *rdi = &dd->verbs_dev.rdi;
77241056 600 u64 rhf = rhf_to_cpu(rhf_addr);
977940b8 601 u32 etype = rhf_rcv_type(rhf), qpn, bth1;
77241056
MM
602 int is_ecn = 0;
603 u8 lnh;
604
82c2611d 605 if (ps_done(&mdata, rhf, rcd))
77241056
MM
606 break;
607
82c2611d
NV
608 if (ps_skip(&mdata, rhf, rcd))
609 goto next;
610
77241056
MM
611 if (etype != RHF_RCV_TYPE_IB)
612 goto next;
613
614 hdr = (struct hfi1_ib_header *)
615 hfi1_get_msgheader(dd, rhf_addr);
616 lnh = be16_to_cpu(hdr->lrh[0]) & 3;
617
e490974e 618 if (lnh == HFI1_LRH_BTH) {
77241056 619 ohdr = &hdr->u.oth;
e490974e 620 } else if (lnh == HFI1_LRH_GRH) {
77241056
MM
621 ohdr = &hdr->u.l.oth;
622 grh = &hdr->u.l.grh;
e490974e 623 } else {
77241056 624 goto next; /* just in case */
e490974e 625 }
977940b8
AK
626 bth1 = be32_to_cpu(ohdr->bth[1]);
627 is_ecn = !!(bth1 & (HFI1_FECN_SMASK | HFI1_BECN_SMASK));
77241056
MM
628
629 if (!is_ecn)
630 goto next;
631
ec4274f1 632 qpn = bth1 & RVT_QPN_MASK;
77241056 633 rcu_read_lock();
ec4274f1 634 qp = rvt_lookup_qpn(rdi, &ibp->rvp, qpn);
77241056 635
d125a6c6 636 if (!qp) {
77241056
MM
637 rcu_read_unlock();
638 goto next;
639 }
640
977940b8 641 process_ecn(qp, hdr, ohdr, rhf, bth1, grh);
77241056 642 rcu_read_unlock();
977940b8
AK
643
644 /* turn off BECN, FECN */
645 bth1 &= ~(HFI1_FECN_SMASK | HFI1_BECN_SMASK);
646 ohdr->bth[1] = cpu_to_be32(bth1);
77241056 647next:
82c2611d 648 update_ps_mdata(&mdata, rcd);
77241056
MM
649 }
650}
82c2611d
NV
651
652static inline int skip_rcv_packet(struct hfi1_packet *packet, int thread)
653{
654 int ret = RCV_PKT_OK;
655
656 /* Set up for the next packet */
657 packet->rhqoff += packet->rsize;
658 if (packet->rhqoff >= packet->maxcnt)
659 packet->rhqoff = 0;
660
661 packet->numpkt++;
662 if (unlikely((packet->numpkt & (MAX_PKT_RECV - 1)) == 0)) {
663 if (thread) {
664 cond_resched();
665 } else {
666 ret = RCV_PKT_LIMIT;
667 this_cpu_inc(*packet->rcd->dd->rcv_limit);
668 }
669 }
670
671 packet->rhf_addr = (__le32 *)packet->rcd->rcvhdrq + packet->rhqoff +
672 packet->rcd->dd->rhf_offset;
673 packet->rhf = rhf_to_cpu(packet->rhf_addr);
674
675 return ret;
676}
77241056 677
f4f30031 678static inline int process_rcv_packet(struct hfi1_packet *packet, int thread)
77241056
MM
679{
680 int ret = RCV_PKT_OK;
681
682 packet->hdr = hfi1_get_msgheader(packet->rcd->dd,
683 packet->rhf_addr);
684 packet->hlen = (u8 *)packet->rhf_addr - (u8 *)packet->hdr;
685 packet->etype = rhf_rcv_type(packet->rhf);
686 /* total length */
687 packet->tlen = rhf_pkt_len(packet->rhf); /* in bytes */
688 /* retrieve eager buffer details */
689 packet->ebuf = NULL;
690 if (rhf_use_egr_bfr(packet->rhf)) {
691 packet->etail = rhf_egr_index(packet->rhf);
692 packet->ebuf = get_egrbuf(packet->rcd, packet->rhf,
693 &packet->updegr);
694 /*
695 * Prefetch the contents of the eager buffer. It is
696 * OK to send a negative length to prefetch_range().
697 * The +2 is the size of the RHF.
698 */
699 prefetch_range(packet->ebuf,
17fb4f29
JJ
700 packet->tlen - ((packet->rcd->rcvhdrqentsize -
701 (rhf_hdrq_offset(packet->rhf)
702 + 2)) * 4));
77241056
MM
703 }
704
705 /*
706 * Call a type specific handler for the packet. We
707 * should be able to trust that etype won't be beyond
708 * the range of valid indexes. If so something is really
709 * wrong and we can probably just let things come
710 * crashing down. There is no need to eat another
711 * comparison in this performance critical code.
712 */
713 packet->rcd->dd->rhf_rcv_function_map[packet->etype](packet);
714 packet->numpkt++;
715
716 /* Set up for the next packet */
717 packet->rhqoff += packet->rsize;
718 if (packet->rhqoff >= packet->maxcnt)
719 packet->rhqoff = 0;
720
f4f30031
DL
721 if (unlikely((packet->numpkt & (MAX_PKT_RECV - 1)) == 0)) {
722 if (thread) {
723 cond_resched();
724 } else {
725 ret = RCV_PKT_LIMIT;
726 this_cpu_inc(*packet->rcd->dd->rcv_limit);
727 }
77241056
MM
728 }
729
50e5dcbe 730 packet->rhf_addr = (__le32 *)packet->rcd->rcvhdrq + packet->rhqoff +
77241056
MM
731 packet->rcd->dd->rhf_offset;
732 packet->rhf = rhf_to_cpu(packet->rhf_addr);
733
734 return ret;
735}
736
737static inline void process_rcv_update(int last, struct hfi1_packet *packet)
738{
739 /*
740 * Update head regs etc., every 16 packets, if not last pkt,
741 * to help prevent rcvhdrq overflows, when many packets
742 * are processed and queue is nearly full.
743 * Don't request an interrupt for intermediate updates.
744 */
745 if (!last && !(packet->numpkt & 0xf)) {
746 update_usrhead(packet->rcd, packet->rhqoff, packet->updegr,
747 packet->etail, 0, 0);
748 packet->updegr = 0;
749 }
750 packet->rcv_flags = 0;
751}
752
753static inline void finish_packet(struct hfi1_packet *packet)
754{
77241056
MM
755 /*
756 * Nothing we need to free for the packet.
757 *
758 * The only thing we need to do is a final update and call for an
759 * interrupt
760 */
761 update_usrhead(packet->rcd, packet->rcd->head, packet->updegr,
762 packet->etail, rcv_intr_dynamic, packet->numpkt);
77241056
MM
763}
764
765static inline void process_rcv_qp_work(struct hfi1_packet *packet)
766{
77241056 767 struct hfi1_ctxtdata *rcd;
895420dd 768 struct rvt_qp *qp, *nqp;
77241056
MM
769
770 rcd = packet->rcd;
771 rcd->head = packet->rhqoff;
772
773 /*
774 * Iterate over all QPs waiting to respond.
775 * The list won't change since the IRQ is only run on one CPU.
776 */
777 list_for_each_entry_safe(qp, nqp, &rcd->qp_wait_list, rspwait) {
778 list_del_init(&qp->rspwait);
54d10c1e
DD
779 if (qp->r_flags & RVT_R_RSP_NAK) {
780 qp->r_flags &= ~RVT_R_RSP_NAK;
77241056
MM
781 hfi1_send_rc_ack(rcd, qp, 0);
782 }
54d10c1e 783 if (qp->r_flags & RVT_R_RSP_SEND) {
77241056
MM
784 unsigned long flags;
785
54d10c1e 786 qp->r_flags &= ~RVT_R_RSP_SEND;
77241056 787 spin_lock_irqsave(&qp->s_lock, flags);
83693bd1
DD
788 if (ib_rvt_state_ops[qp->state] &
789 RVT_PROCESS_OR_FLUSH_SEND)
77241056
MM
790 hfi1_schedule_send(qp);
791 spin_unlock_irqrestore(&qp->s_lock, flags);
792 }
793 if (atomic_dec_and_test(&qp->refcount))
794 wake_up(&qp->wait);
795 }
796}
797
798/*
799 * Handle receive interrupts when using the no dma rtail option.
800 */
f4f30031 801int handle_receive_interrupt_nodma_rtail(struct hfi1_ctxtdata *rcd, int thread)
77241056
MM
802{
803 u32 seq;
f4f30031 804 int last = RCV_PKT_OK;
77241056
MM
805 struct hfi1_packet packet;
806
807 init_packet(rcd, &packet);
808 seq = rhf_rcv_seq(packet.rhf);
f4f30031
DL
809 if (seq != rcd->seq_cnt) {
810 last = RCV_PKT_DONE;
77241056 811 goto bail;
f4f30031 812 }
77241056 813
6c9e50f8 814 prescan_rxq(rcd, &packet);
77241056 815
f4f30031
DL
816 while (last == RCV_PKT_OK) {
817 last = process_rcv_packet(&packet, thread);
77241056
MM
818 seq = rhf_rcv_seq(packet.rhf);
819 if (++rcd->seq_cnt > 13)
820 rcd->seq_cnt = 1;
821 if (seq != rcd->seq_cnt)
f4f30031 822 last = RCV_PKT_DONE;
77241056
MM
823 process_rcv_update(last, &packet);
824 }
825 process_rcv_qp_work(&packet);
826bail:
827 finish_packet(&packet);
f4f30031 828 return last;
77241056
MM
829}
830
f4f30031 831int handle_receive_interrupt_dma_rtail(struct hfi1_ctxtdata *rcd, int thread)
77241056
MM
832{
833 u32 hdrqtail;
f4f30031 834 int last = RCV_PKT_OK;
77241056
MM
835 struct hfi1_packet packet;
836
837 init_packet(rcd, &packet);
838 hdrqtail = get_rcvhdrtail(rcd);
f4f30031
DL
839 if (packet.rhqoff == hdrqtail) {
840 last = RCV_PKT_DONE;
77241056 841 goto bail;
f4f30031 842 }
77241056
MM
843 smp_rmb(); /* prevent speculative reads of dma'ed hdrq */
844
6c9e50f8 845 prescan_rxq(rcd, &packet);
77241056 846
f4f30031
DL
847 while (last == RCV_PKT_OK) {
848 last = process_rcv_packet(&packet, thread);
77241056 849 if (packet.rhqoff == hdrqtail)
f4f30031 850 last = RCV_PKT_DONE;
77241056
MM
851 process_rcv_update(last, &packet);
852 }
853 process_rcv_qp_work(&packet);
854bail:
855 finish_packet(&packet);
f4f30031 856 return last;
77241056
MM
857}
858
859static inline void set_all_nodma_rtail(struct hfi1_devdata *dd)
860{
861 int i;
862
82c2611d 863 for (i = HFI1_CTRL_CTXT + 1; i < dd->first_user_ctxt; i++)
77241056
MM
864 dd->rcd[i]->do_interrupt =
865 &handle_receive_interrupt_nodma_rtail;
866}
867
868static inline void set_all_dma_rtail(struct hfi1_devdata *dd)
869{
870 int i;
871
82c2611d 872 for (i = HFI1_CTRL_CTXT + 1; i < dd->first_user_ctxt; i++)
77241056
MM
873 dd->rcd[i]->do_interrupt =
874 &handle_receive_interrupt_dma_rtail;
875}
876
fb9036dd
JS
877void set_all_slowpath(struct hfi1_devdata *dd)
878{
879 int i;
880
881 /* HFI1_CTRL_CTXT must always use the slow path interrupt handler */
882 for (i = HFI1_CTRL_CTXT + 1; i < dd->first_user_ctxt; i++)
883 dd->rcd[i]->do_interrupt = &handle_receive_interrupt;
884}
885
886static inline int set_armed_to_active(struct hfi1_ctxtdata *rcd,
887 struct hfi1_packet packet,
888 struct hfi1_devdata *dd)
889{
890 struct work_struct *lsaw = &rcd->ppd->linkstate_active_work;
891 struct hfi1_message_header *hdr = hfi1_get_msgheader(packet.rcd->dd,
892 packet.rhf_addr);
893
894 if (hdr2sc(hdr, packet.rhf) != 0xf) {
895 int hwstate = read_logical_state(dd);
896
897 if (hwstate != LSTATE_ACTIVE) {
898 dd_dev_info(dd, "Unexpected link state %d\n", hwstate);
899 return 0;
900 }
901
902 queue_work(rcd->ppd->hfi1_wq, lsaw);
903 return 1;
904 }
905 return 0;
906}
907
77241056
MM
908/*
909 * handle_receive_interrupt - receive a packet
910 * @rcd: the context
911 *
912 * Called from interrupt handler for errors or receive interrupt.
913 * This is the slow path interrupt handler.
914 */
f4f30031 915int handle_receive_interrupt(struct hfi1_ctxtdata *rcd, int thread)
77241056 916{
77241056
MM
917 struct hfi1_devdata *dd = rcd->dd;
918 u32 hdrqtail;
82c2611d 919 int needset, last = RCV_PKT_OK;
77241056 920 struct hfi1_packet packet;
82c2611d
NV
921 int skip_pkt = 0;
922
923 /* Control context will always use the slow path interrupt handler */
924 needset = (rcd->ctxt == HFI1_CTRL_CTXT) ? 0 : 1;
77241056
MM
925
926 init_packet(rcd, &packet);
927
82c2611d 928 if (!HFI1_CAP_KGET_MASK(rcd->flags, DMA_RTAIL)) {
77241056
MM
929 u32 seq = rhf_rcv_seq(packet.rhf);
930
f4f30031
DL
931 if (seq != rcd->seq_cnt) {
932 last = RCV_PKT_DONE;
77241056 933 goto bail;
f4f30031 934 }
77241056
MM
935 hdrqtail = 0;
936 } else {
937 hdrqtail = get_rcvhdrtail(rcd);
f4f30031
DL
938 if (packet.rhqoff == hdrqtail) {
939 last = RCV_PKT_DONE;
77241056 940 goto bail;
f4f30031 941 }
77241056 942 smp_rmb(); /* prevent speculative reads of dma'ed hdrq */
82c2611d
NV
943
944 /*
945 * Control context can potentially receive an invalid
946 * rhf. Drop such packets.
947 */
948 if (rcd->ctxt == HFI1_CTRL_CTXT) {
949 u32 seq = rhf_rcv_seq(packet.rhf);
950
951 if (seq != rcd->seq_cnt)
952 skip_pkt = 1;
953 }
77241056
MM
954 }
955
6c9e50f8 956 prescan_rxq(rcd, &packet);
77241056 957
f4f30031 958 while (last == RCV_PKT_OK) {
17fb4f29
JJ
959 if (unlikely(dd->do_drop &&
960 atomic_xchg(&dd->drop_packet, DROP_PACKET_OFF) ==
961 DROP_PACKET_ON)) {
77241056
MM
962 dd->do_drop = 0;
963
964 /* On to the next packet */
965 packet.rhqoff += packet.rsize;
50e5dcbe 966 packet.rhf_addr = (__le32 *)rcd->rcvhdrq +
77241056
MM
967 packet.rhqoff +
968 dd->rhf_offset;
969 packet.rhf = rhf_to_cpu(packet.rhf_addr);
970
82c2611d
NV
971 } else if (skip_pkt) {
972 last = skip_rcv_packet(&packet, thread);
973 skip_pkt = 0;
77241056 974 } else {
fb9036dd
JS
975 /* Auto activate link on non-SC15 packet receive */
976 if (unlikely(rcd->ppd->host_link_state ==
977 HLS_UP_ARMED) &&
978 set_armed_to_active(rcd, packet, dd))
979 goto bail;
f4f30031 980 last = process_rcv_packet(&packet, thread);
77241056
MM
981 }
982
82c2611d 983 if (!HFI1_CAP_KGET_MASK(rcd->flags, DMA_RTAIL)) {
77241056
MM
984 u32 seq = rhf_rcv_seq(packet.rhf);
985
986 if (++rcd->seq_cnt > 13)
987 rcd->seq_cnt = 1;
988 if (seq != rcd->seq_cnt)
f4f30031 989 last = RCV_PKT_DONE;
77241056 990 if (needset) {
17fb4f29 991 dd_dev_info(dd, "Switching to NO_DMA_RTAIL\n");
77241056
MM
992 set_all_nodma_rtail(dd);
993 needset = 0;
994 }
995 } else {
996 if (packet.rhqoff == hdrqtail)
f4f30031 997 last = RCV_PKT_DONE;
82c2611d
NV
998 /*
999 * Control context can potentially receive an invalid
1000 * rhf. Drop such packets.
1001 */
1002 if (rcd->ctxt == HFI1_CTRL_CTXT) {
1003 u32 seq = rhf_rcv_seq(packet.rhf);
1004
1005 if (++rcd->seq_cnt > 13)
1006 rcd->seq_cnt = 1;
1007 if (!last && (seq != rcd->seq_cnt))
1008 skip_pkt = 1;
1009 }
1010
77241056
MM
1011 if (needset) {
1012 dd_dev_info(dd,
1013 "Switching to DMA_RTAIL\n");
1014 set_all_dma_rtail(dd);
1015 needset = 0;
1016 }
1017 }
1018
1019 process_rcv_update(last, &packet);
1020 }
1021
1022 process_rcv_qp_work(&packet);
1023
1024bail:
1025 /*
1026 * Always write head at end, and setup rcv interrupt, even
1027 * if no packets were processed.
1028 */
1029 finish_packet(&packet);
f4f30031 1030 return last;
77241056
MM
1031}
1032
fb9036dd
JS
1033/*
1034 * We may discover in the interrupt that the hardware link state has
1035 * changed from ARMED to ACTIVE (due to the arrival of a non-SC15 packet),
1036 * and we need to update the driver's notion of the link state. We cannot
1037 * run set_link_state from interrupt context, so we queue this function on
1038 * a workqueue.
1039 *
1040 * We delay the regular interrupt processing until after the state changes
1041 * so that the link will be in the correct state by the time any application
1042 * we wake up attempts to send a reply to any message it received.
1043 * (Subsequent receive interrupts may possibly force the wakeup before we
1044 * update the link state.)
1045 *
1046 * The rcd is freed in hfi1_free_ctxtdata after hfi1_postinit_cleanup invokes
1047 * dd->f_cleanup(dd) to disable the interrupt handler and flush workqueues,
1048 * so we're safe from use-after-free of the rcd.
1049 */
1050void receive_interrupt_work(struct work_struct *work)
1051{
1052 struct hfi1_pportdata *ppd = container_of(work, struct hfi1_pportdata,
1053 linkstate_active_work);
1054 struct hfi1_devdata *dd = ppd->dd;
1055 int i;
1056
1057 /* Received non-SC15 packet implies neighbor_normal */
1058 ppd->neighbor_normal = 1;
1059 set_link_state(ppd, HLS_UP_ACTIVE);
1060
1061 /*
1062 * Interrupt all kernel contexts that could have had an
1063 * interrupt during auto activation.
1064 */
1065 for (i = HFI1_CTRL_CTXT; i < dd->first_user_ctxt; i++)
1066 force_recv_intr(dd->rcd[i]);
1067}
1068
77241056
MM
1069/*
1070 * Convert a given MTU size to the on-wire MAD packet enumeration.
1071 * Return -1 if the size is invalid.
1072 */
1073int mtu_to_enum(u32 mtu, int default_if_bad)
1074{
1075 switch (mtu) {
1076 case 0: return OPA_MTU_0;
1077 case 256: return OPA_MTU_256;
1078 case 512: return OPA_MTU_512;
1079 case 1024: return OPA_MTU_1024;
1080 case 2048: return OPA_MTU_2048;
1081 case 4096: return OPA_MTU_4096;
1082 case 8192: return OPA_MTU_8192;
1083 case 10240: return OPA_MTU_10240;
1084 }
1085 return default_if_bad;
1086}
1087
1088u16 enum_to_mtu(int mtu)
1089{
1090 switch (mtu) {
1091 case OPA_MTU_0: return 0;
1092 case OPA_MTU_256: return 256;
1093 case OPA_MTU_512: return 512;
1094 case OPA_MTU_1024: return 1024;
1095 case OPA_MTU_2048: return 2048;
1096 case OPA_MTU_4096: return 4096;
1097 case OPA_MTU_8192: return 8192;
1098 case OPA_MTU_10240: return 10240;
1099 default: return 0xffff;
1100 }
1101}
1102
1103/*
1104 * set_mtu - set the MTU
1105 * @ppd: the per port data
1106 *
1107 * We can handle "any" incoming size, the issue here is whether we
1108 * need to restrict our outgoing size. We do not deal with what happens
1109 * to programs that are already running when the size changes.
1110 */
1111int set_mtu(struct hfi1_pportdata *ppd)
1112{
1113 struct hfi1_devdata *dd = ppd->dd;
1114 int i, drain, ret = 0, is_up = 0;
1115
1116 ppd->ibmtu = 0;
1117 for (i = 0; i < ppd->vls_supported; i++)
1118 if (ppd->ibmtu < dd->vld[i].mtu)
1119 ppd->ibmtu = dd->vld[i].mtu;
1120 ppd->ibmaxlen = ppd->ibmtu + lrh_max_header_bytes(ppd->dd);
1121
1122 mutex_lock(&ppd->hls_lock);
d0d236ea
JJ
1123 if (ppd->host_link_state == HLS_UP_INIT ||
1124 ppd->host_link_state == HLS_UP_ARMED ||
1125 ppd->host_link_state == HLS_UP_ACTIVE)
77241056
MM
1126 is_up = 1;
1127
1128 drain = !is_ax(dd) && is_up;
1129
1130 if (drain)
1131 /*
1132 * MTU is specified per-VL. To ensure that no packet gets
1133 * stuck (due, e.g., to the MTU for the packet's VL being
1134 * reduced), empty the per-VL FIFOs before adjusting MTU.
1135 */
1136 ret = stop_drain_data_vls(dd);
1137
1138 if (ret) {
1139 dd_dev_err(dd, "%s: cannot stop/drain VLs - refusing to change per-VL MTUs\n",
1140 __func__);
1141 goto err;
1142 }
1143
1144 hfi1_set_ib_cfg(ppd, HFI1_IB_CFG_MTU, 0);
1145
1146 if (drain)
1147 open_fill_data_vls(dd); /* reopen all VLs */
1148
1149err:
1150 mutex_unlock(&ppd->hls_lock);
1151
1152 return ret;
1153}
1154
1155int hfi1_set_lid(struct hfi1_pportdata *ppd, u32 lid, u8 lmc)
1156{
1157 struct hfi1_devdata *dd = ppd->dd;
1158
1159 ppd->lid = lid;
1160 ppd->lmc = lmc;
1161 hfi1_set_ib_cfg(ppd, HFI1_IB_CFG_LIDLMC, 0);
1162
1163 dd_dev_info(dd, "IB%u:%u got a lid: 0x%x\n", dd->unit, ppd->port, lid);
1164
1165 return 0;
1166}
1167
91ab4ed3
EH
1168void shutdown_led_override(struct hfi1_pportdata *ppd)
1169{
1170 struct hfi1_devdata *dd = ppd->dd;
1171
1172 if (atomic_read(&ppd->led_override_timer_active)) {
1173 del_timer_sync(&ppd->led_override_timer);
1174 atomic_set(&ppd->led_override_timer_active, 0);
1175 }
1176
1177 /* Shut off LEDs after we are sure timer is not running */
1178 setextled(dd, 0);
1179}
77241056
MM
1180
1181static void run_led_override(unsigned long opaque)
1182{
1183 struct hfi1_pportdata *ppd = (struct hfi1_pportdata *)opaque;
1184 struct hfi1_devdata *dd = ppd->dd;
91ab4ed3
EH
1185 unsigned long timeout;
1186 int phase_idx;
77241056
MM
1187
1188 if (!(dd->flags & HFI1_INITTED))
1189 return;
1190
91ab4ed3
EH
1191 phase_idx = ppd->led_override_phase & 1;
1192 setextled(dd, phase_idx);
1193
1194 timeout = ppd->led_override_vals[phase_idx];
1195 /* Set up for next phase */
1196 ppd->led_override_phase = !ppd->led_override_phase;
77241056
MM
1197
1198 /*
1199 * don't re-fire the timer if user asked for it to be off; we let
1200 * it fire one more time after they turn it off to simplify
1201 */
1202 if (ppd->led_override_vals[0] || ppd->led_override_vals[1])
91ab4ed3
EH
1203 mod_timer(&ppd->led_override_timer, jiffies + timeout);
1204 else
1205 /* Hand control of the LED to the DC for normal operation */
1206 write_csr(dd, DCC_CFG_LED_CNTRL, 0);
77241056
MM
1207}
1208
91ab4ed3
EH
1209/*
1210 * To have the LED blink in a particular pattern, provide timeon and timeoff
1211 * in milliseconds. To turn off custom blinking and return to normal operation,
1212 * provide timeon = timeoff = 0.
1213 */
1214void hfi1_set_led_override(struct hfi1_pportdata *ppd, unsigned int timeon,
1215 unsigned int timeoff)
77241056
MM
1216{
1217 struct hfi1_devdata *dd = ppd->dd;
77241056
MM
1218
1219 if (!(dd->flags & HFI1_INITTED))
1220 return;
1221
91ab4ed3
EH
1222 /* Convert to jiffies for direct use in timer */
1223 ppd->led_override_vals[0] = msecs_to_jiffies(timeoff);
1224 ppd->led_override_vals[1] = msecs_to_jiffies(timeon);
1225 ppd->led_override_phase = 1; /* Arbitrarily start from LED on phase */
77241056
MM
1226
1227 /*
1228 * If the timer has not already been started, do so. Use a "quick"
1229 * timeout so the function will be called soon, to look at our request.
1230 */
1231 if (atomic_inc_return(&ppd->led_override_timer_active) == 1) {
1232 /* Need to start timer */
a3faf606 1233 setup_timer(&ppd->led_override_timer, run_led_override,
17fb4f29 1234 (unsigned long)ppd);
a3faf606 1235
77241056
MM
1236 ppd->led_override_timer.expires = jiffies + 1;
1237 add_timer(&ppd->led_override_timer);
1238 } else {
1239 if (ppd->led_override_vals[0] || ppd->led_override_vals[1])
1240 mod_timer(&ppd->led_override_timer, jiffies + 1);
1241 atomic_dec(&ppd->led_override_timer_active);
1242 }
1243}
1244
1245/**
1246 * hfi1_reset_device - reset the chip if possible
1247 * @unit: the device to reset
1248 *
1249 * Whether or not reset is successful, we attempt to re-initialize the chip
1250 * (that is, much like a driver unload/reload). We clear the INITTED flag
1251 * so that the various entry points will fail until we reinitialize. For
1252 * now, we only allow this if no user contexts are open that use chip resources
1253 */
1254int hfi1_reset_device(int unit)
1255{
1256 int ret, i;
1257 struct hfi1_devdata *dd = hfi1_lookup(unit);
1258 struct hfi1_pportdata *ppd;
1259 unsigned long flags;
1260 int pidx;
1261
1262 if (!dd) {
1263 ret = -ENODEV;
1264 goto bail;
1265 }
1266
1267 dd_dev_info(dd, "Reset on unit %u requested\n", unit);
1268
1269 if (!dd->kregbase || !(dd->flags & HFI1_PRESENT)) {
1270 dd_dev_info(dd,
17fb4f29
JJ
1271 "Invalid unit number %u or not initialized or not present\n",
1272 unit);
77241056
MM
1273 ret = -ENXIO;
1274 goto bail;
1275 }
1276
1277 spin_lock_irqsave(&dd->uctxt_lock, flags);
1278 if (dd->rcd)
1279 for (i = dd->first_user_ctxt; i < dd->num_rcv_contexts; i++) {
1280 if (!dd->rcd[i] || !dd->rcd[i]->cnt)
1281 continue;
1282 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
1283 ret = -EBUSY;
1284 goto bail;
1285 }
1286 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
1287
1288 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
1289 ppd = dd->pport + pidx;
77241056 1290
91ab4ed3 1291 shutdown_led_override(ppd);
77241056
MM
1292 }
1293 if (dd->flags & HFI1_HAS_SEND_DMA)
1294 sdma_exit(dd);
1295
1296 hfi1_reset_cpu_counters(dd);
1297
1298 ret = hfi1_init(dd, 1);
1299
1300 if (ret)
1301 dd_dev_err(dd,
17fb4f29
JJ
1302 "Reinitialize unit %u after reset failed with %d\n",
1303 unit, ret);
77241056
MM
1304 else
1305 dd_dev_info(dd, "Reinitialized unit %u after resetting\n",
17fb4f29 1306 unit);
77241056
MM
1307
1308bail:
1309 return ret;
1310}
1311
1312void handle_eflags(struct hfi1_packet *packet)
1313{
1314 struct hfi1_ctxtdata *rcd = packet->rcd;
1315 u32 rte = rhf_rcv_type_err(packet->rhf);
1316
77241056 1317 rcv_hdrerr(rcd, rcd->ppd, packet);
a03a03e9
IH
1318 if (rhf_err_flags(packet->rhf))
1319 dd_dev_err(rcd->dd,
1320 "receive context %d: rhf 0x%016llx, errs [ %s%s%s%s%s%s%s%s] rte 0x%x\n",
1321 rcd->ctxt, packet->rhf,
1322 packet->rhf & RHF_K_HDR_LEN_ERR ? "k_hdr_len " : "",
1323 packet->rhf & RHF_DC_UNC_ERR ? "dc_unc " : "",
1324 packet->rhf & RHF_DC_ERR ? "dc " : "",
1325 packet->rhf & RHF_TID_ERR ? "tid " : "",
1326 packet->rhf & RHF_LEN_ERR ? "len " : "",
1327 packet->rhf & RHF_ECC_ERR ? "ecc " : "",
1328 packet->rhf & RHF_VCRC_ERR ? "vcrc " : "",
1329 packet->rhf & RHF_ICRC_ERR ? "icrc " : "",
1330 rte);
77241056
MM
1331}
1332
1333/*
1334 * The following functions are called by the interrupt handler. They are type
1335 * specific handlers for each packet type.
1336 */
1337int process_receive_ib(struct hfi1_packet *packet)
1338{
1339 trace_hfi1_rcvhdr(packet->rcd->ppd->dd,
1340 packet->rcd->ctxt,
1341 rhf_err_flags(packet->rhf),
1342 RHF_RCV_TYPE_IB,
1343 packet->hlen,
1344 packet->tlen,
1345 packet->updegr,
1346 rhf_egr_index(packet->rhf));
1347
1348 if (unlikely(rhf_err_flags(packet->rhf))) {
1349 handle_eflags(packet);
1350 return RHF_RCV_CONTINUE;
1351 }
1352
1353 hfi1_ib_rcv(packet);
1354 return RHF_RCV_CONTINUE;
1355}
1356
1357int process_receive_bypass(struct hfi1_packet *packet)
1358{
1359 if (unlikely(rhf_err_flags(packet->rhf)))
1360 handle_eflags(packet);
1361
1362 dd_dev_err(packet->rcd->dd,
17fb4f29 1363 "Bypass packets are not supported in normal operation. Dropping\n");
77241056
MM
1364 return RHF_RCV_CONTINUE;
1365}
1366
1367int process_receive_error(struct hfi1_packet *packet)
1368{
1369 handle_eflags(packet);
1370
1371 if (unlikely(rhf_err_flags(packet->rhf)))
1372 dd_dev_err(packet->rcd->dd,
1373 "Unhandled error packet received. Dropping.\n");
1374
1375 return RHF_RCV_CONTINUE;
1376}
1377
1378int kdeth_process_expected(struct hfi1_packet *packet)
1379{
1380 if (unlikely(rhf_err_flags(packet->rhf)))
1381 handle_eflags(packet);
1382
1383 dd_dev_err(packet->rcd->dd,
1384 "Unhandled expected packet received. Dropping.\n");
1385 return RHF_RCV_CONTINUE;
1386}
1387
1388int kdeth_process_eager(struct hfi1_packet *packet)
1389{
1390 if (unlikely(rhf_err_flags(packet->rhf)))
1391 handle_eflags(packet);
1392
1393 dd_dev_err(packet->rcd->dd,
1394 "Unhandled eager packet received. Dropping.\n");
1395 return RHF_RCV_CONTINUE;
1396}
1397
1398int process_receive_invalid(struct hfi1_packet *packet)
1399{
1400 dd_dev_err(packet->rcd->dd, "Invalid packet type %d. Dropping\n",
17fb4f29 1401 rhf_rcv_type(packet->rhf));
77241056
MM
1402 return RHF_RCV_CONTINUE;
1403}