2 * Copyright(c) 2015 - 2020 Intel Corporation.
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.
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.
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.
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
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
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.
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.
48 #include <linux/pci.h>
49 #include <linux/netdevice.h>
50 #include <linux/vmalloc.h>
51 #include <linux/delay.h>
52 #include <linux/xarray.h>
53 #include <linux/module.h>
54 #include <linux/printk.h>
55 #include <linux/hrtimer.h>
56 #include <linux/bitmap.h>
57 #include <linux/numa.h>
58 #include <rdma/rdma_vt.h>
75 #define pr_fmt(fmt) DRIVER_NAME ": " fmt
78 * min buffers we want to have per context, after driver
80 #define HFI1_MIN_USER_CTXT_BUFCNT 7
82 #define HFI1_MIN_EAGER_BUFFER_SIZE (4 * 1024) /* 4KB */
83 #define HFI1_MAX_EAGER_BUFFER_SIZE (256 * 1024) /* 256KB */
85 #define NUM_IB_PORTS 1
88 * Number of user receive contexts we are configured to use (to allow for more
89 * pio buffers per ctxt, etc.) Zero means use one user context per CPU.
91 int num_user_contexts = -1;
92 module_param_named(num_user_contexts, num_user_contexts, int, 0444);
94 num_user_contexts, "Set max number of user contexts to use (default: -1 will use the real (non-HT) CPU count)");
96 uint krcvqs[RXE_NUM_DATA_VL];
98 module_param_array(krcvqs, uint, &krcvqsset, S_IRUGO);
99 MODULE_PARM_DESC(krcvqs, "Array of the number of non-control kernel receive queues by VL");
101 /* computed based on above array */
102 unsigned long n_krcvqs;
104 static unsigned hfi1_rcvarr_split = 25;
105 module_param_named(rcvarr_split, hfi1_rcvarr_split, uint, S_IRUGO);
106 MODULE_PARM_DESC(rcvarr_split, "Percent of context's RcvArray entries used for Eager buffers");
108 static uint eager_buffer_size = (8 << 20); /* 8MB */
109 module_param(eager_buffer_size, uint, S_IRUGO);
110 MODULE_PARM_DESC(eager_buffer_size, "Size of the eager buffers, default: 8MB");
112 static uint rcvhdrcnt = 2048; /* 2x the max eager buffer count */
113 module_param_named(rcvhdrcnt, rcvhdrcnt, uint, S_IRUGO);
114 MODULE_PARM_DESC(rcvhdrcnt, "Receive header queue count (default 2048)");
116 static uint hfi1_hdrq_entsize = 32;
117 module_param_named(hdrq_entsize, hfi1_hdrq_entsize, uint, 0444);
118 MODULE_PARM_DESC(hdrq_entsize, "Size of header queue entries: 2 - 8B, 16 - 64B, 32 - 128B (default)");
120 unsigned int user_credit_return_threshold = 33; /* default is 33% */
121 module_param(user_credit_return_threshold, uint, S_IRUGO);
122 MODULE_PARM_DESC(user_credit_return_threshold, "Credit return threshold for user send contexts, return when unreturned credits passes this many blocks (in percent of allocated blocks, 0 is off)");
124 DEFINE_XARRAY_FLAGS(hfi1_dev_table, XA_FLAGS_ALLOC | XA_FLAGS_LOCK_IRQ);
126 static int hfi1_create_kctxt(struct hfi1_devdata *dd,
127 struct hfi1_pportdata *ppd)
129 struct hfi1_ctxtdata *rcd;
132 /* Control context has to be always 0 */
133 BUILD_BUG_ON(HFI1_CTRL_CTXT != 0);
135 ret = hfi1_create_ctxtdata(ppd, dd->node, &rcd);
137 dd_dev_err(dd, "Kernel receive context allocation failed\n");
142 * Set up the kernel context flags here and now because they use
143 * default values for all receive side memories. User contexts will
144 * be handled as they are created.
146 rcd->flags = HFI1_CAP_KGET(MULTI_PKT_EGR) |
147 HFI1_CAP_KGET(NODROP_RHQ_FULL) |
148 HFI1_CAP_KGET(NODROP_EGR_FULL) |
149 HFI1_CAP_KGET(DMA_RTAIL);
151 /* Control context must use DMA_RTAIL */
152 if (rcd->ctxt == HFI1_CTRL_CTXT)
153 rcd->flags |= HFI1_CAP_DMA_RTAIL;
154 rcd->fast_handler = get_dma_rtail_setting(rcd) ?
155 handle_receive_interrupt_dma_rtail :
156 handle_receive_interrupt_nodma_rtail;
157 rcd->slow_handler = handle_receive_interrupt;
159 hfi1_set_seq_cnt(rcd, 1);
161 rcd->sc = sc_alloc(dd, SC_ACK, rcd->rcvhdrqentsize, dd->node);
163 dd_dev_err(dd, "Kernel send context allocation failed\n");
166 hfi1_init_ctxt(rcd->sc);
172 * Create the receive context array and one or more kernel contexts
174 int hfi1_create_kctxts(struct hfi1_devdata *dd)
179 dd->rcd = kcalloc_node(dd->num_rcv_contexts, sizeof(*dd->rcd),
180 GFP_KERNEL, dd->node);
184 for (i = 0; i < dd->first_dyn_alloc_ctxt; ++i) {
185 ret = hfi1_create_kctxt(dd, dd->pport);
192 for (i = 0; dd->rcd && i < dd->first_dyn_alloc_ctxt; ++i)
193 hfi1_free_ctxt(dd->rcd[i]);
195 /* All the contexts should be freed, free the array */
202 * Helper routines for the receive context reference count (rcd and uctxt).
204 static void hfi1_rcd_init(struct hfi1_ctxtdata *rcd)
206 kref_init(&rcd->kref);
210 * hfi1_rcd_free - When reference is zero clean up.
211 * @kref: pointer to an initialized rcd data structure
214 static void hfi1_rcd_free(struct kref *kref)
217 struct hfi1_ctxtdata *rcd =
218 container_of(kref, struct hfi1_ctxtdata, kref);
220 spin_lock_irqsave(&rcd->dd->uctxt_lock, flags);
221 rcd->dd->rcd[rcd->ctxt] = NULL;
222 spin_unlock_irqrestore(&rcd->dd->uctxt_lock, flags);
224 hfi1_free_ctxtdata(rcd->dd, rcd);
230 * hfi1_rcd_put - decrement reference for rcd
231 * @rcd: pointer to an initialized rcd data structure
233 * Use this to put a reference after the init.
235 int hfi1_rcd_put(struct hfi1_ctxtdata *rcd)
238 return kref_put(&rcd->kref, hfi1_rcd_free);
244 * hfi1_rcd_get - increment reference for rcd
245 * @rcd: pointer to an initialized rcd data structure
247 * Use this to get a reference after the init.
249 * Return : reflect kref_get_unless_zero(), which returns non-zero on
250 * increment, otherwise 0.
252 int hfi1_rcd_get(struct hfi1_ctxtdata *rcd)
254 return kref_get_unless_zero(&rcd->kref);
258 * allocate_rcd_index - allocate an rcd index from the rcd array
259 * @dd: pointer to a valid devdata structure
260 * @rcd: rcd data structure to assign
261 * @index: pointer to index that is allocated
263 * Find an empty index in the rcd array, and assign the given rcd to it.
264 * If the array is full, we are EBUSY.
267 static int allocate_rcd_index(struct hfi1_devdata *dd,
268 struct hfi1_ctxtdata *rcd, u16 *index)
273 spin_lock_irqsave(&dd->uctxt_lock, flags);
274 for (ctxt = 0; ctxt < dd->num_rcv_contexts; ctxt++)
278 if (ctxt < dd->num_rcv_contexts) {
283 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
285 if (ctxt >= dd->num_rcv_contexts)
294 * hfi1_rcd_get_by_index_safe - validate the ctxt index before accessing the
296 * @dd: pointer to a valid devdata structure
297 * @ctxt: the index of an possilbe rcd
299 * This is a wrapper for hfi1_rcd_get_by_index() to validate that the given
300 * ctxt index is valid.
302 * The caller is responsible for making the _put().
305 struct hfi1_ctxtdata *hfi1_rcd_get_by_index_safe(struct hfi1_devdata *dd,
308 if (ctxt < dd->num_rcv_contexts)
309 return hfi1_rcd_get_by_index(dd, ctxt);
315 * hfi1_rcd_get_by_index
316 * @dd: pointer to a valid devdata structure
317 * @ctxt: the index of an possilbe rcd
319 * We need to protect access to the rcd array. If access is needed to
320 * one or more index, get the protecting spinlock and then increment the
323 * The caller is responsible for making the _put().
326 struct hfi1_ctxtdata *hfi1_rcd_get_by_index(struct hfi1_devdata *dd, u16 ctxt)
329 struct hfi1_ctxtdata *rcd = NULL;
331 spin_lock_irqsave(&dd->uctxt_lock, flags);
334 if (!hfi1_rcd_get(rcd))
337 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
343 * Common code for user and kernel context create and setup.
344 * NOTE: the initial kref is done here (hf1_rcd_init()).
346 int hfi1_create_ctxtdata(struct hfi1_pportdata *ppd, int numa,
347 struct hfi1_ctxtdata **context)
349 struct hfi1_devdata *dd = ppd->dd;
350 struct hfi1_ctxtdata *rcd;
351 unsigned kctxt_ngroups = 0;
354 if (dd->rcv_entries.nctxt_extra >
355 dd->num_rcv_contexts - dd->first_dyn_alloc_ctxt)
356 kctxt_ngroups = (dd->rcv_entries.nctxt_extra -
357 (dd->num_rcv_contexts - dd->first_dyn_alloc_ctxt));
358 rcd = kzalloc_node(sizeof(*rcd), GFP_KERNEL, numa);
360 u32 rcvtids, max_entries;
364 ret = allocate_rcd_index(dd, rcd, &ctxt);
371 INIT_LIST_HEAD(&rcd->qp_wait_list);
372 hfi1_exp_tid_group_init(rcd);
376 rcd->rcv_array_groups = dd->rcv_entries.ngroups;
377 rcd->rhf_rcv_function_map = normal_rhf_rcv_functions;
378 rcd->msix_intr = CCE_NUM_MSIX_VECTORS;
380 mutex_init(&rcd->exp_mutex);
381 spin_lock_init(&rcd->exp_lock);
382 INIT_LIST_HEAD(&rcd->flow_queue.queue_head);
383 INIT_LIST_HEAD(&rcd->rarr_queue.queue_head);
385 hfi1_cdbg(PROC, "setting up context %u\n", rcd->ctxt);
388 * Calculate the context's RcvArray entry starting point.
389 * We do this here because we have to take into account all
390 * the RcvArray entries that previous context would have
391 * taken and we have to account for any extra groups assigned
392 * to the static (kernel) or dynamic (vnic/user) contexts.
394 if (ctxt < dd->first_dyn_alloc_ctxt) {
395 if (ctxt < kctxt_ngroups) {
396 base = ctxt * (dd->rcv_entries.ngroups + 1);
397 rcd->rcv_array_groups++;
399 base = kctxt_ngroups +
400 (ctxt * dd->rcv_entries.ngroups);
403 u16 ct = ctxt - dd->first_dyn_alloc_ctxt;
405 base = ((dd->n_krcv_queues * dd->rcv_entries.ngroups) +
407 if (ct < dd->rcv_entries.nctxt_extra) {
408 base += ct * (dd->rcv_entries.ngroups + 1);
409 rcd->rcv_array_groups++;
411 base += dd->rcv_entries.nctxt_extra +
412 (ct * dd->rcv_entries.ngroups);
415 rcd->eager_base = base * dd->rcv_entries.group_size;
417 rcd->rcvhdrq_cnt = rcvhdrcnt;
418 rcd->rcvhdrqentsize = hfi1_hdrq_entsize;
420 rcd->rcvhdrqentsize - sizeof(u64) / sizeof(u32);
422 * Simple Eager buffer allocation: we have already pre-allocated
423 * the number of RcvArray entry groups. Each ctxtdata structure
424 * holds the number of groups for that context.
426 * To follow CSR requirements and maintain cacheline alignment,
427 * make sure all sizes and bases are multiples of group_size.
429 * The expected entry count is what is left after assigning
432 max_entries = rcd->rcv_array_groups *
433 dd->rcv_entries.group_size;
434 rcvtids = ((max_entries * hfi1_rcvarr_split) / 100);
435 rcd->egrbufs.count = round_down(rcvtids,
436 dd->rcv_entries.group_size);
437 if (rcd->egrbufs.count > MAX_EAGER_ENTRIES) {
438 dd_dev_err(dd, "ctxt%u: requested too many RcvArray entries.\n",
440 rcd->egrbufs.count = MAX_EAGER_ENTRIES;
443 "ctxt%u: max Eager buffer RcvArray entries: %u\n",
444 rcd->ctxt, rcd->egrbufs.count);
447 * Allocate array that will hold the eager buffer accounting
449 * This will allocate the maximum possible buffer count based
450 * on the value of the RcvArray split parameter.
451 * The resulting value will be rounded down to the closest
452 * multiple of dd->rcv_entries.group_size.
454 rcd->egrbufs.buffers =
455 kcalloc_node(rcd->egrbufs.count,
456 sizeof(*rcd->egrbufs.buffers),
458 if (!rcd->egrbufs.buffers)
460 rcd->egrbufs.rcvtids =
461 kcalloc_node(rcd->egrbufs.count,
462 sizeof(*rcd->egrbufs.rcvtids),
464 if (!rcd->egrbufs.rcvtids)
466 rcd->egrbufs.size = eager_buffer_size;
468 * The size of the buffers programmed into the RcvArray
469 * entries needs to be big enough to handle the highest
472 if (rcd->egrbufs.size < hfi1_max_mtu) {
473 rcd->egrbufs.size = __roundup_pow_of_two(hfi1_max_mtu);
475 "ctxt%u: eager bufs size too small. Adjusting to %u\n",
476 rcd->ctxt, rcd->egrbufs.size);
478 rcd->egrbufs.rcvtid_size = HFI1_MAX_EAGER_BUFFER_SIZE;
480 /* Applicable only for statically created kernel contexts */
481 if (ctxt < dd->first_dyn_alloc_ctxt) {
482 rcd->opstats = kzalloc_node(sizeof(*rcd->opstats),
487 /* Initialize TID flow generations for the context */
488 hfi1_kern_init_ctxt_generations(rcd);
503 * @rcd: pointer to an initialized rcd data structure
505 * This wrapper is the free function that matches hfi1_create_ctxtdata().
506 * When a context is done being used (kernel or user), this function is called
507 * for the "final" put to match the kref init from hf1i_create_ctxtdata().
508 * Other users of the context do a get/put sequence to make sure that the
509 * structure isn't removed while in use.
511 void hfi1_free_ctxt(struct hfi1_ctxtdata *rcd)
517 * Select the largest ccti value over all SLs to determine the intra-
518 * packet gap for the link.
520 * called with cca_timer_lock held (to protect access to cca_timer
521 * array), and rcu_read_lock() (to protect access to cc_state).
523 void set_link_ipg(struct hfi1_pportdata *ppd)
525 struct hfi1_devdata *dd = ppd->dd;
526 struct cc_state *cc_state;
528 u16 cce, ccti_limit, max_ccti = 0;
531 u32 current_egress_rate; /* Mbits /sec */
534 * max_pkt_time is the maximum packet egress time in units
535 * of the fabric clock period 1/(805 MHz).
538 cc_state = get_cc_state(ppd);
542 * This should _never_ happen - rcu_read_lock() is held,
543 * and set_link_ipg() should not be called if cc_state
548 for (i = 0; i < OPA_MAX_SLS; i++) {
549 u16 ccti = ppd->cca_timer[i].ccti;
555 ccti_limit = cc_state->cct.ccti_limit;
556 if (max_ccti > ccti_limit)
557 max_ccti = ccti_limit;
559 cce = cc_state->cct.entries[max_ccti].entry;
560 shift = (cce & 0xc000) >> 14;
561 mult = (cce & 0x3fff);
563 current_egress_rate = active_egress_rate(ppd);
565 max_pkt_time = egress_cycles(ppd->ibmaxlen, current_egress_rate);
567 src = (max_pkt_time >> shift) * mult;
569 src &= SEND_STATIC_RATE_CONTROL_CSR_SRC_RELOAD_SMASK;
570 src <<= SEND_STATIC_RATE_CONTROL_CSR_SRC_RELOAD_SHIFT;
572 write_csr(dd, SEND_STATIC_RATE_CONTROL, src);
575 static enum hrtimer_restart cca_timer_fn(struct hrtimer *t)
577 struct cca_timer *cca_timer;
578 struct hfi1_pportdata *ppd;
580 u16 ccti_timer, ccti_min;
581 struct cc_state *cc_state;
583 enum hrtimer_restart ret = HRTIMER_NORESTART;
585 cca_timer = container_of(t, struct cca_timer, hrtimer);
586 ppd = cca_timer->ppd;
591 cc_state = get_cc_state(ppd);
595 return HRTIMER_NORESTART;
599 * 1) decrement ccti for SL
600 * 2) calculate IPG for link (set_link_ipg())
601 * 3) restart timer, unless ccti is at min value
604 ccti_min = cc_state->cong_setting.entries[sl].ccti_min;
605 ccti_timer = cc_state->cong_setting.entries[sl].ccti_timer;
607 spin_lock_irqsave(&ppd->cca_timer_lock, flags);
609 if (cca_timer->ccti > ccti_min) {
614 if (cca_timer->ccti > ccti_min) {
615 unsigned long nsec = 1024 * ccti_timer;
616 /* ccti_timer is in units of 1.024 usec */
617 hrtimer_forward_now(t, ns_to_ktime(nsec));
618 ret = HRTIMER_RESTART;
621 spin_unlock_irqrestore(&ppd->cca_timer_lock, flags);
627 * Common code for initializing the physical port structure.
629 void hfi1_init_pportdata(struct pci_dev *pdev, struct hfi1_pportdata *ppd,
630 struct hfi1_devdata *dd, u8 hw_pidx, u8 port)
633 uint default_pkey_idx;
634 struct cc_state *cc_state;
637 ppd->hw_pidx = hw_pidx;
638 ppd->port = port; /* IB port number, not index */
639 ppd->prev_link_width = LINK_WIDTH_DEFAULT;
641 * There are C_VL_COUNT number of PortVLXmitWait counters.
642 * Adding 1 to C_VL_COUNT to include the PortXmitWait counter.
644 for (i = 0; i < C_VL_COUNT + 1; i++) {
645 ppd->port_vl_xmit_wait_last[i] = 0;
646 ppd->vl_xmit_flit_cnt[i] = 0;
649 default_pkey_idx = 1;
651 ppd->pkeys[default_pkey_idx] = DEFAULT_P_KEY;
652 ppd->part_enforce |= HFI1_PART_ENFORCE_IN;
655 dd_dev_err(dd, "Faking data partition 0x8001 in idx %u\n",
657 ppd->pkeys[!default_pkey_idx] = 0x8001;
660 INIT_WORK(&ppd->link_vc_work, handle_verify_cap);
661 INIT_WORK(&ppd->link_up_work, handle_link_up);
662 INIT_WORK(&ppd->link_down_work, handle_link_down);
663 INIT_WORK(&ppd->freeze_work, handle_freeze);
664 INIT_WORK(&ppd->link_downgrade_work, handle_link_downgrade);
665 INIT_WORK(&ppd->sma_message_work, handle_sma_message);
666 INIT_WORK(&ppd->link_bounce_work, handle_link_bounce);
667 INIT_DELAYED_WORK(&ppd->start_link_work, handle_start_link);
668 INIT_WORK(&ppd->linkstate_active_work, receive_interrupt_work);
669 INIT_WORK(&ppd->qsfp_info.qsfp_work, qsfp_event);
671 mutex_init(&ppd->hls_lock);
672 spin_lock_init(&ppd->qsfp_info.qsfp_lock);
674 ppd->qsfp_info.ppd = ppd;
675 ppd->sm_trap_qp = 0x0;
680 spin_lock_init(&ppd->cca_timer_lock);
682 for (i = 0; i < OPA_MAX_SLS; i++) {
683 hrtimer_init(&ppd->cca_timer[i].hrtimer, CLOCK_MONOTONIC,
685 ppd->cca_timer[i].ppd = ppd;
686 ppd->cca_timer[i].sl = i;
687 ppd->cca_timer[i].ccti = 0;
688 ppd->cca_timer[i].hrtimer.function = cca_timer_fn;
691 ppd->cc_max_table_entries = IB_CC_TABLE_CAP_DEFAULT;
693 spin_lock_init(&ppd->cc_state_lock);
694 spin_lock_init(&ppd->cc_log_lock);
695 cc_state = kzalloc(sizeof(*cc_state), GFP_KERNEL);
696 RCU_INIT_POINTER(ppd->cc_state, cc_state);
702 dd_dev_err(dd, "Congestion Control Agent disabled for port %d\n", port);
706 * Do initialization for device that is only needed on
707 * first detect, not on resets.
709 static int loadtime_init(struct hfi1_devdata *dd)
715 * init_after_reset - re-initialize after a reset
716 * @dd: the hfi1_ib device
718 * sanity check at least some of the values after reset, and
719 * ensure no receive or transmit (explicitly, in case reset
722 static int init_after_reset(struct hfi1_devdata *dd)
725 struct hfi1_ctxtdata *rcd;
727 * Ensure chip does no sends or receives, tail updates, or
728 * pioavail updates while we re-initialize. This is mostly
729 * for the driver data structures, not chip registers.
731 for (i = 0; i < dd->num_rcv_contexts; i++) {
732 rcd = hfi1_rcd_get_by_index(dd, i);
733 hfi1_rcvctrl(dd, HFI1_RCVCTRL_CTXT_DIS |
734 HFI1_RCVCTRL_INTRAVAIL_DIS |
735 HFI1_RCVCTRL_TAILUPD_DIS, rcd);
738 pio_send_control(dd, PSC_GLOBAL_DISABLE);
739 for (i = 0; i < dd->num_send_contexts; i++)
740 sc_disable(dd->send_contexts[i].sc);
745 static void enable_chip(struct hfi1_devdata *dd)
747 struct hfi1_ctxtdata *rcd;
751 /* enable PIO send */
752 pio_send_control(dd, PSC_GLOBAL_ENABLE);
755 * Enable kernel ctxts' receive and receive interrupt.
756 * Other ctxts done as user opens and initializes them.
758 for (i = 0; i < dd->first_dyn_alloc_ctxt; ++i) {
759 rcd = hfi1_rcd_get_by_index(dd, i);
762 rcvmask = HFI1_RCVCTRL_CTXT_ENB | HFI1_RCVCTRL_INTRAVAIL_ENB;
763 rcvmask |= HFI1_CAP_KGET_MASK(rcd->flags, DMA_RTAIL) ?
764 HFI1_RCVCTRL_TAILUPD_ENB : HFI1_RCVCTRL_TAILUPD_DIS;
765 if (!HFI1_CAP_KGET_MASK(rcd->flags, MULTI_PKT_EGR))
766 rcvmask |= HFI1_RCVCTRL_ONE_PKT_EGR_ENB;
767 if (HFI1_CAP_KGET_MASK(rcd->flags, NODROP_RHQ_FULL))
768 rcvmask |= HFI1_RCVCTRL_NO_RHQ_DROP_ENB;
769 if (HFI1_CAP_KGET_MASK(rcd->flags, NODROP_EGR_FULL))
770 rcvmask |= HFI1_RCVCTRL_NO_EGR_DROP_ENB;
771 if (HFI1_CAP_IS_KSET(TID_RDMA))
772 rcvmask |= HFI1_RCVCTRL_TIDFLOW_ENB;
773 hfi1_rcvctrl(dd, rcvmask, rcd);
780 * create_workqueues - create per port workqueues
781 * @dd: the hfi1_ib device
783 static int create_workqueues(struct hfi1_devdata *dd)
786 struct hfi1_pportdata *ppd;
788 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
789 ppd = dd->pport + pidx;
794 WQ_SYSFS | WQ_HIGHPRI | WQ_CPU_INTENSIVE |
796 HFI1_MAX_ACTIVE_WORKQUEUE_ENTRIES,
803 * Make the link workqueue single-threaded to enforce
809 WQ_SYSFS | WQ_MEM_RECLAIM | WQ_UNBOUND,
818 pr_err("alloc_workqueue failed for port %d\n", pidx + 1);
819 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
820 ppd = dd->pport + pidx;
822 destroy_workqueue(ppd->hfi1_wq);
826 destroy_workqueue(ppd->link_wq);
834 * destroy_workqueues - destroy per port workqueues
835 * @dd: the hfi1_ib device
837 static void destroy_workqueues(struct hfi1_devdata *dd)
840 struct hfi1_pportdata *ppd;
842 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
843 ppd = dd->pport + pidx;
846 destroy_workqueue(ppd->hfi1_wq);
850 destroy_workqueue(ppd->link_wq);
857 * enable_general_intr() - Enable the IRQs that will be handled by the
858 * general interrupt handler.
862 static void enable_general_intr(struct hfi1_devdata *dd)
864 set_intr_bits(dd, CCE_ERR_INT, MISC_ERR_INT, true);
865 set_intr_bits(dd, PIO_ERR_INT, TXE_ERR_INT, true);
866 set_intr_bits(dd, IS_SENDCTXT_ERR_START, IS_SENDCTXT_ERR_END, true);
867 set_intr_bits(dd, PBC_INT, GPIO_ASSERT_INT, true);
868 set_intr_bits(dd, TCRIT_INT, TCRIT_INT, true);
869 set_intr_bits(dd, IS_DC_START, IS_DC_END, true);
870 set_intr_bits(dd, IS_SENDCREDIT_START, IS_SENDCREDIT_END, true);
874 * hfi1_init - do the actual initialization sequence on the chip
875 * @dd: the hfi1_ib device
876 * @reinit: re-initializing, so don't allocate new memory
878 * Do the actual initialization sequence on the chip. This is done
879 * both from the init routine called from the PCI infrastructure, and
880 * when we reset the chip, or detect that it was reset internally,
881 * or it's administratively re-enabled.
883 * Memory allocation here and in called routines is only done in
884 * the first case (reinit == 0). We have to be careful, because even
885 * without memory allocation, we need to re-write all the chip registers
886 * TIDs, etc. after the reset or enable has completed.
888 int hfi1_init(struct hfi1_devdata *dd, int reinit)
890 int ret = 0, pidx, lastfail = 0;
893 struct hfi1_ctxtdata *rcd;
894 struct hfi1_pportdata *ppd;
896 /* Set up send low level handlers */
897 dd->process_pio_send = hfi1_verbs_send_pio;
898 dd->process_dma_send = hfi1_verbs_send_dma;
899 dd->pio_inline_send = pio_copy;
900 dd->process_vnic_dma_send = hfi1_vnic_send_dma;
903 atomic_set(&dd->drop_packet, DROP_PACKET_ON);
906 atomic_set(&dd->drop_packet, DROP_PACKET_OFF);
910 /* make sure the link is not "up" */
911 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
912 ppd = dd->pport + pidx;
917 ret = init_after_reset(dd);
919 ret = loadtime_init(dd);
923 /* allocate dummy tail memory for all receive contexts */
924 dd->rcvhdrtail_dummy_kvaddr = dma_alloc_coherent(&dd->pcidev->dev,
926 &dd->rcvhdrtail_dummy_dma,
929 if (!dd->rcvhdrtail_dummy_kvaddr) {
930 dd_dev_err(dd, "cannot allocate dummy tail memory\n");
935 /* dd->rcd can be NULL if early initialization failed */
936 for (i = 0; dd->rcd && i < dd->first_dyn_alloc_ctxt; ++i) {
938 * Set up the (kernel) rcvhdr queue and egr TIDs. If doing
939 * re-init, the simplest way to handle this is to free
940 * existing, and re-allocate.
941 * Need to re-create rest of ctxt 0 ctxtdata as well.
943 rcd = hfi1_rcd_get_by_index(dd, i);
947 rcd->do_interrupt = &handle_receive_interrupt;
949 lastfail = hfi1_create_rcvhdrq(dd, rcd);
951 lastfail = hfi1_setup_eagerbufs(rcd);
953 lastfail = hfi1_kern_exp_rcv_init(rcd, reinit);
956 "failed to allocate kernel ctxt's rcvhdrq and/or egr bufs\n");
963 /* Allocate enough memory for user event notification. */
964 len = PAGE_ALIGN(chip_rcv_contexts(dd) * HFI1_MAX_SHARED_CTXTS *
965 sizeof(*dd->events));
966 dd->events = vmalloc_user(len);
968 dd_dev_err(dd, "Failed to allocate user events page\n");
970 * Allocate a page for device and port status.
971 * Page will be shared amongst all user processes.
973 dd->status = vmalloc_user(PAGE_SIZE);
975 dd_dev_err(dd, "Failed to allocate dev status page\n");
976 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
977 ppd = dd->pport + pidx;
979 /* Currently, we only have one port */
980 ppd->statusp = &dd->status->port;
985 /* enable chip even if we have an error, so we can debug cause */
990 * Set status even if port serdes is not initialized
991 * so that diags will work.
994 dd->status->dev |= HFI1_STATUS_CHIP_PRESENT |
997 /* enable all interrupts from the chip */
998 enable_general_intr(dd);
1001 /* chip is OK for user apps; mark it as initialized */
1002 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
1003 ppd = dd->pport + pidx;
1006 * start the serdes - must be after interrupts are
1007 * enabled so we are notified when the link goes up
1009 lastfail = bringup_serdes(ppd);
1012 "Failed to bring up port %u\n",
1016 * Set status even if port serdes is not initialized
1017 * so that diags will work.
1020 *ppd->statusp |= HFI1_STATUS_CHIP_PRESENT |
1021 HFI1_STATUS_INITTED;
1022 if (!ppd->link_speed_enabled)
1027 /* if ret is non-zero, we probably should do some cleanup here... */
1031 struct hfi1_devdata *hfi1_lookup(int unit)
1033 return xa_load(&hfi1_dev_table, unit);
1037 * Stop the timers during unit shutdown, or after an error late
1038 * in initialization.
1040 static void stop_timers(struct hfi1_devdata *dd)
1042 struct hfi1_pportdata *ppd;
1045 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
1046 ppd = dd->pport + pidx;
1047 if (ppd->led_override_timer.function) {
1048 del_timer_sync(&ppd->led_override_timer);
1049 atomic_set(&ppd->led_override_timer_active, 0);
1055 * shutdown_device - shut down a device
1056 * @dd: the hfi1_ib device
1058 * This is called to make the device quiet when we are about to
1059 * unload the driver, and also when the device is administratively
1060 * disabled. It does not free any data structures.
1061 * Everything it does has to be setup again by hfi1_init(dd, 1)
1063 static void shutdown_device(struct hfi1_devdata *dd)
1065 struct hfi1_pportdata *ppd;
1066 struct hfi1_ctxtdata *rcd;
1070 if (dd->flags & HFI1_SHUTDOWN)
1072 dd->flags |= HFI1_SHUTDOWN;
1074 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
1075 ppd = dd->pport + pidx;
1079 *ppd->statusp &= ~(HFI1_STATUS_IB_CONF |
1080 HFI1_STATUS_IB_READY);
1082 dd->flags &= ~HFI1_INITTED;
1084 /* mask and clean up interrupts */
1085 set_intr_bits(dd, IS_FIRST_SOURCE, IS_LAST_SOURCE, false);
1086 msix_clean_up_interrupts(dd);
1088 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
1089 ppd = dd->pport + pidx;
1090 for (i = 0; i < dd->num_rcv_contexts; i++) {
1091 rcd = hfi1_rcd_get_by_index(dd, i);
1092 hfi1_rcvctrl(dd, HFI1_RCVCTRL_TAILUPD_DIS |
1093 HFI1_RCVCTRL_CTXT_DIS |
1094 HFI1_RCVCTRL_INTRAVAIL_DIS |
1095 HFI1_RCVCTRL_PKEY_DIS |
1096 HFI1_RCVCTRL_ONE_PKT_EGR_DIS, rcd);
1100 * Gracefully stop all sends allowing any in progress to
1101 * trickle out first.
1103 for (i = 0; i < dd->num_send_contexts; i++)
1104 sc_flush(dd->send_contexts[i].sc);
1108 * Enough for anything that's going to trickle out to have actually
1113 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
1114 ppd = dd->pport + pidx;
1116 /* disable all contexts */
1117 for (i = 0; i < dd->num_send_contexts; i++)
1118 sc_disable(dd->send_contexts[i].sc);
1119 /* disable the send device */
1120 pio_send_control(dd, PSC_GLOBAL_DISABLE);
1122 shutdown_led_override(ppd);
1125 * Clear SerdesEnable.
1126 * We can't count on interrupts since we are stopping.
1128 hfi1_quiet_serdes(ppd);
1130 flush_workqueue(ppd->hfi1_wq);
1132 flush_workqueue(ppd->link_wq);
1138 * hfi1_free_ctxtdata - free a context's allocated data
1139 * @dd: the hfi1_ib device
1140 * @rcd: the ctxtdata structure
1142 * free up any allocated data for a context
1143 * It should never change any chip state, or global driver state.
1145 void hfi1_free_ctxtdata(struct hfi1_devdata *dd, struct hfi1_ctxtdata *rcd)
1153 dma_free_coherent(&dd->pcidev->dev, rcvhdrq_size(rcd),
1154 rcd->rcvhdrq, rcd->rcvhdrq_dma);
1155 rcd->rcvhdrq = NULL;
1156 if (hfi1_rcvhdrtail_kvaddr(rcd)) {
1157 dma_free_coherent(&dd->pcidev->dev, PAGE_SIZE,
1158 (void *)hfi1_rcvhdrtail_kvaddr(rcd),
1159 rcd->rcvhdrqtailaddr_dma);
1160 rcd->rcvhdrtail_kvaddr = NULL;
1164 /* all the RcvArray entries should have been cleared by now */
1165 kfree(rcd->egrbufs.rcvtids);
1166 rcd->egrbufs.rcvtids = NULL;
1168 for (e = 0; e < rcd->egrbufs.alloced; e++) {
1169 if (rcd->egrbufs.buffers[e].dma)
1170 dma_free_coherent(&dd->pcidev->dev,
1171 rcd->egrbufs.buffers[e].len,
1172 rcd->egrbufs.buffers[e].addr,
1173 rcd->egrbufs.buffers[e].dma);
1175 kfree(rcd->egrbufs.buffers);
1176 rcd->egrbufs.alloced = 0;
1177 rcd->egrbufs.buffers = NULL;
1182 vfree(rcd->subctxt_uregbase);
1183 vfree(rcd->subctxt_rcvegrbuf);
1184 vfree(rcd->subctxt_rcvhdr_base);
1185 kfree(rcd->opstats);
1187 rcd->subctxt_uregbase = NULL;
1188 rcd->subctxt_rcvegrbuf = NULL;
1189 rcd->subctxt_rcvhdr_base = NULL;
1190 rcd->opstats = NULL;
1194 * Release our hold on the shared asic data. If we are the last one,
1195 * return the structure to be finalized outside the lock. Must be
1196 * holding hfi1_dev_table lock.
1198 static struct hfi1_asic_data *release_asic_data(struct hfi1_devdata *dd)
1200 struct hfi1_asic_data *ad;
1205 dd->asic_data->dds[dd->hfi1_id] = NULL;
1206 other = dd->hfi1_id ? 0 : 1;
1208 dd->asic_data = NULL;
1209 /* return NULL if the other dd still has a link */
1210 return ad->dds[other] ? NULL : ad;
1213 static void finalize_asic_data(struct hfi1_devdata *dd,
1214 struct hfi1_asic_data *ad)
1216 clean_up_i2c(dd, ad);
1221 * hfi1_free_devdata - cleans up and frees per-unit data structure
1222 * @dd: pointer to a valid devdata structure
1224 * It cleans up and frees all data structures set up by
1225 * by hfi1_alloc_devdata().
1227 void hfi1_free_devdata(struct hfi1_devdata *dd)
1229 struct hfi1_asic_data *ad;
1230 unsigned long flags;
1232 xa_lock_irqsave(&hfi1_dev_table, flags);
1233 __xa_erase(&hfi1_dev_table, dd->unit);
1234 ad = release_asic_data(dd);
1235 xa_unlock_irqrestore(&hfi1_dev_table, flags);
1237 finalize_asic_data(dd, ad);
1238 free_platform_config(dd);
1239 rcu_barrier(); /* wait for rcu callbacks to complete */
1240 free_percpu(dd->int_counter);
1241 free_percpu(dd->rcv_limit);
1242 free_percpu(dd->send_schedule);
1243 free_percpu(dd->tx_opstats);
1244 dd->int_counter = NULL;
1245 dd->rcv_limit = NULL;
1246 dd->send_schedule = NULL;
1247 dd->tx_opstats = NULL;
1248 kfree(dd->comp_vect);
1249 dd->comp_vect = NULL;
1250 sdma_clean(dd, dd->num_sdma);
1251 rvt_dealloc_device(&dd->verbs_dev.rdi);
1255 * hfi1_alloc_devdata - Allocate our primary per-unit data structure.
1256 * @pdev: Valid PCI device
1257 * @extra: How many bytes to alloc past the default
1259 * Must be done via verbs allocator, because the verbs cleanup process
1260 * both does cleanup and free of the data structure.
1261 * "extra" is for chip-specific data.
1263 static struct hfi1_devdata *hfi1_alloc_devdata(struct pci_dev *pdev,
1266 struct hfi1_devdata *dd;
1269 /* extra is * number of ports */
1270 nports = extra / sizeof(struct hfi1_pportdata);
1272 dd = (struct hfi1_devdata *)rvt_alloc_device(sizeof(*dd) + extra,
1275 return ERR_PTR(-ENOMEM);
1276 dd->num_pports = nports;
1277 dd->pport = (struct hfi1_pportdata *)(dd + 1);
1279 pci_set_drvdata(pdev, dd);
1281 ret = xa_alloc_irq(&hfi1_dev_table, &dd->unit, dd, xa_limit_32b,
1285 "Could not allocate unit ID: error %d\n", -ret);
1288 rvt_set_ibdev_name(&dd->verbs_dev.rdi, "%s_%d", class_name(), dd->unit);
1290 * If the BIOS does not have the NUMA node information set, select
1291 * NUMA 0 so we get consistent performance.
1293 dd->node = pcibus_to_node(pdev->bus);
1294 if (dd->node == NUMA_NO_NODE) {
1295 dd_dev_err(dd, "Invalid PCI NUMA node. Performance may be affected\n");
1300 * Initialize all locks for the device. This needs to be as early as
1301 * possible so locks are usable.
1303 spin_lock_init(&dd->sc_lock);
1304 spin_lock_init(&dd->sendctrl_lock);
1305 spin_lock_init(&dd->rcvctrl_lock);
1306 spin_lock_init(&dd->uctxt_lock);
1307 spin_lock_init(&dd->hfi1_diag_trans_lock);
1308 spin_lock_init(&dd->sc_init_lock);
1309 spin_lock_init(&dd->dc8051_memlock);
1310 seqlock_init(&dd->sc2vl_lock);
1311 spin_lock_init(&dd->sde_map_lock);
1312 spin_lock_init(&dd->pio_map_lock);
1313 mutex_init(&dd->dc8051_lock);
1314 init_waitqueue_head(&dd->event_queue);
1315 spin_lock_init(&dd->irq_src_lock);
1317 dd->int_counter = alloc_percpu(u64);
1318 if (!dd->int_counter) {
1323 dd->rcv_limit = alloc_percpu(u64);
1324 if (!dd->rcv_limit) {
1329 dd->send_schedule = alloc_percpu(u64);
1330 if (!dd->send_schedule) {
1335 dd->tx_opstats = alloc_percpu(struct hfi1_opcode_stats_perctx);
1336 if (!dd->tx_opstats) {
1341 dd->comp_vect = kzalloc(sizeof(*dd->comp_vect), GFP_KERNEL);
1342 if (!dd->comp_vect) {
1347 atomic_set(&dd->ipoib_rsm_usr_num, 0);
1351 hfi1_free_devdata(dd);
1352 return ERR_PTR(ret);
1356 * Called from freeze mode handlers, and from PCI error
1357 * reporting code. Should be paranoid about state of
1358 * system and data structures.
1360 void hfi1_disable_after_error(struct hfi1_devdata *dd)
1362 if (dd->flags & HFI1_INITTED) {
1365 dd->flags &= ~HFI1_INITTED;
1367 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
1368 struct hfi1_pportdata *ppd;
1370 ppd = dd->pport + pidx;
1371 if (dd->flags & HFI1_PRESENT)
1372 set_link_state(ppd, HLS_DN_DISABLE);
1375 *ppd->statusp &= ~HFI1_STATUS_IB_READY;
1380 * Mark as having had an error for driver, and also
1381 * for /sys and status word mapped to user programs.
1382 * This marks unit as not usable, until reset.
1385 dd->status->dev |= HFI1_STATUS_HWERROR;
1388 static void remove_one(struct pci_dev *);
1389 static int init_one(struct pci_dev *, const struct pci_device_id *);
1390 static void shutdown_one(struct pci_dev *);
1392 #define DRIVER_LOAD_MSG "Intel " DRIVER_NAME " loaded: "
1393 #define PFX DRIVER_NAME ": "
1395 const struct pci_device_id hfi1_pci_tbl[] = {
1396 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL0) },
1397 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL1) },
1401 MODULE_DEVICE_TABLE(pci, hfi1_pci_tbl);
1403 static struct pci_driver hfi1_pci_driver = {
1404 .name = DRIVER_NAME,
1406 .remove = remove_one,
1407 .shutdown = shutdown_one,
1408 .id_table = hfi1_pci_tbl,
1409 .err_handler = &hfi1_pci_err_handler,
1412 static void __init compute_krcvqs(void)
1416 for (i = 0; i < krcvqsset; i++)
1417 n_krcvqs += krcvqs[i];
1421 * Do all the generic driver unit- and chip-independent memory
1422 * allocation and initialization.
1424 static int __init hfi1_mod_init(void)
1432 ret = node_affinity_init();
1436 /* validate max MTU before any devices start */
1437 if (!valid_opa_max_mtu(hfi1_max_mtu)) {
1438 pr_err("Invalid max_mtu 0x%x, using 0x%x instead\n",
1439 hfi1_max_mtu, HFI1_DEFAULT_MAX_MTU);
1440 hfi1_max_mtu = HFI1_DEFAULT_MAX_MTU;
1442 /* valid CUs run from 1-128 in powers of 2 */
1443 if (hfi1_cu > 128 || !is_power_of_2(hfi1_cu))
1445 /* valid credit return threshold is 0-100, variable is unsigned */
1446 if (user_credit_return_threshold > 100)
1447 user_credit_return_threshold = 100;
1451 * sanitize receive interrupt count, time must wait until after
1452 * the hardware type is known
1454 if (rcv_intr_count > RCV_HDR_HEAD_COUNTER_MASK)
1455 rcv_intr_count = RCV_HDR_HEAD_COUNTER_MASK;
1456 /* reject invalid combinations */
1457 if (rcv_intr_count == 0 && rcv_intr_timeout == 0) {
1458 pr_err("Invalid mode: both receive interrupt count and available timeout are zero - setting interrupt count to 1\n");
1461 if (rcv_intr_count > 1 && rcv_intr_timeout == 0) {
1463 * Avoid indefinite packet delivery by requiring a timeout
1466 pr_err("Invalid mode: receive interrupt count greater than 1 and available timeout is zero - setting available timeout to 1\n");
1467 rcv_intr_timeout = 1;
1469 if (rcv_intr_dynamic && !(rcv_intr_count > 1 && rcv_intr_timeout > 0)) {
1471 * The dynamic algorithm expects a non-zero timeout
1474 pr_err("Invalid mode: dynamic receive interrupt mitigation with invalid count and timeout - turning dynamic off\n");
1475 rcv_intr_dynamic = 0;
1478 /* sanitize link CRC options */
1479 link_crc_mask &= SUPPORTED_CRCS;
1483 pr_err("Failed to allocate opfn_wq");
1488 * These must be called before the driver is registered with
1489 * the PCI subsystem.
1492 ret = pci_register_driver(&hfi1_pci_driver);
1494 pr_err("Unable to register driver: error %d\n", -ret);
1497 goto bail; /* all OK */
1506 module_init(hfi1_mod_init);
1509 * Do the non-unit driver cleanup, memory free, etc. at unload.
1511 static void __exit hfi1_mod_cleanup(void)
1513 pci_unregister_driver(&hfi1_pci_driver);
1515 node_affinity_destroy_all();
1518 WARN_ON(!xa_empty(&hfi1_dev_table));
1519 dispose_firmware(); /* asymmetric with obtain_firmware() */
1523 module_exit(hfi1_mod_cleanup);
1525 /* this can only be called after a successful initialization */
1526 static void cleanup_device_data(struct hfi1_devdata *dd)
1531 /* users can't do anything more with chip */
1532 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
1533 struct hfi1_pportdata *ppd = &dd->pport[pidx];
1534 struct cc_state *cc_state;
1538 *ppd->statusp &= ~HFI1_STATUS_CHIP_PRESENT;
1540 for (i = 0; i < OPA_MAX_SLS; i++)
1541 hrtimer_cancel(&ppd->cca_timer[i].hrtimer);
1543 spin_lock(&ppd->cc_state_lock);
1544 cc_state = get_cc_state_protected(ppd);
1545 RCU_INIT_POINTER(ppd->cc_state, NULL);
1546 spin_unlock(&ppd->cc_state_lock);
1549 kfree_rcu(cc_state, rcu);
1552 free_credit_return(dd);
1554 if (dd->rcvhdrtail_dummy_kvaddr) {
1555 dma_free_coherent(&dd->pcidev->dev, sizeof(u64),
1556 (void *)dd->rcvhdrtail_dummy_kvaddr,
1557 dd->rcvhdrtail_dummy_dma);
1558 dd->rcvhdrtail_dummy_kvaddr = NULL;
1562 * Free any resources still in use (usually just kernel contexts)
1563 * at unload; we do for ctxtcnt, because that's what we allocate.
1565 for (ctxt = 0; dd->rcd && ctxt < dd->num_rcv_contexts; ctxt++) {
1566 struct hfi1_ctxtdata *rcd = dd->rcd[ctxt];
1569 hfi1_free_ctxt_rcv_groups(rcd);
1570 hfi1_free_ctxt(rcd);
1578 /* must follow rcv context free - need to remove rcv's hooks */
1579 for (ctxt = 0; ctxt < dd->num_send_contexts; ctxt++)
1580 sc_free(dd->send_contexts[ctxt].sc);
1581 dd->num_send_contexts = 0;
1582 kfree(dd->send_contexts);
1583 dd->send_contexts = NULL;
1584 kfree(dd->hw_to_sw);
1585 dd->hw_to_sw = NULL;
1586 kfree(dd->boardname);
1592 * Clean up on unit shutdown, or error during unit load after
1593 * successful initialization.
1595 static void postinit_cleanup(struct hfi1_devdata *dd)
1597 hfi1_start_cleanup(dd);
1598 hfi1_comp_vectors_clean_up(dd);
1599 hfi1_dev_affinity_clean_up(dd);
1601 hfi1_pcie_ddcleanup(dd);
1602 hfi1_pcie_cleanup(dd->pcidev);
1604 cleanup_device_data(dd);
1606 hfi1_free_devdata(dd);
1609 static int init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
1611 int ret = 0, j, pidx, initfail;
1612 struct hfi1_devdata *dd;
1613 struct hfi1_pportdata *ppd;
1615 /* First, lock the non-writable module parameters */
1618 /* Validate dev ids */
1619 if (!(ent->device == PCI_DEVICE_ID_INTEL0 ||
1620 ent->device == PCI_DEVICE_ID_INTEL1)) {
1621 dev_err(&pdev->dev, "Failing on unknown Intel deviceid 0x%x\n",
1627 /* Allocate the dd so we can get to work */
1628 dd = hfi1_alloc_devdata(pdev, NUM_IB_PORTS *
1629 sizeof(struct hfi1_pportdata));
1635 /* Validate some global module parameters */
1636 ret = hfi1_validate_rcvhdrcnt(dd, rcvhdrcnt);
1640 /* use the encoding function as a sanitization check */
1641 if (!encode_rcv_header_entry_size(hfi1_hdrq_entsize)) {
1642 dd_dev_err(dd, "Invalid HdrQ Entry size %u\n",
1648 /* The receive eager buffer size must be set before the receive
1649 * contexts are created.
1651 * Set the eager buffer size. Validate that it falls in a range
1652 * allowed by the hardware - all powers of 2 between the min and
1653 * max. The maximum valid MTU is within the eager buffer range
1654 * so we do not need to cap the max_mtu by an eager buffer size
1657 if (eager_buffer_size) {
1658 if (!is_power_of_2(eager_buffer_size))
1660 roundup_pow_of_two(eager_buffer_size);
1662 clamp_val(eager_buffer_size,
1663 MIN_EAGER_BUFFER * 8,
1664 MAX_EAGER_BUFFER_TOTAL);
1665 dd_dev_info(dd, "Eager buffer size %u\n",
1668 dd_dev_err(dd, "Invalid Eager buffer size of 0\n");
1673 /* restrict value of hfi1_rcvarr_split */
1674 hfi1_rcvarr_split = clamp_val(hfi1_rcvarr_split, 0, 100);
1676 ret = hfi1_pcie_init(dd);
1681 * Do device-specific initialization, function table setup, dd
1684 ret = hfi1_init_dd(dd);
1686 goto clean_bail; /* error already printed */
1688 ret = create_workqueues(dd);
1692 /* do the generic initialization */
1693 initfail = hfi1_init(dd, 0);
1695 ret = hfi1_register_ib_device(dd);
1698 * Now ready for use. this should be cleared whenever we
1699 * detect a reset, or initiate one. If earlier failure,
1700 * we still create devices, so diags, etc. can be used
1701 * to determine cause of problem.
1703 if (!initfail && !ret) {
1704 dd->flags |= HFI1_INITTED;
1705 /* create debufs files after init and ib register */
1706 hfi1_dbg_ibdev_init(&dd->verbs_dev);
1709 j = hfi1_device_create(dd);
1711 dd_dev_err(dd, "Failed to create /dev devices: %d\n", -j);
1713 if (initfail || ret) {
1714 msix_clean_up_interrupts(dd);
1716 flush_workqueue(ib_wq);
1717 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
1718 hfi1_quiet_serdes(dd->pport + pidx);
1719 ppd = dd->pport + pidx;
1721 destroy_workqueue(ppd->hfi1_wq);
1722 ppd->hfi1_wq = NULL;
1725 destroy_workqueue(ppd->link_wq);
1726 ppd->link_wq = NULL;
1730 hfi1_device_remove(dd);
1732 hfi1_unregister_ib_device(dd);
1733 postinit_cleanup(dd);
1736 goto bail; /* everything already cleaned */
1744 hfi1_pcie_cleanup(pdev);
1749 static void wait_for_clients(struct hfi1_devdata *dd)
1752 * Remove the device init value and complete the device if there is
1753 * no clients or wait for active clients to finish.
1755 if (atomic_dec_and_test(&dd->user_refcount))
1756 complete(&dd->user_comp);
1758 wait_for_completion(&dd->user_comp);
1761 static void remove_one(struct pci_dev *pdev)
1763 struct hfi1_devdata *dd = pci_get_drvdata(pdev);
1765 /* close debugfs files before ib unregister */
1766 hfi1_dbg_ibdev_exit(&dd->verbs_dev);
1768 /* remove the /dev hfi1 interface */
1769 hfi1_device_remove(dd);
1771 /* wait for existing user space clients to finish */
1772 wait_for_clients(dd);
1774 /* unregister from IB core */
1775 hfi1_unregister_ib_device(dd);
1777 /* free netdev data */
1778 hfi1_netdev_free(dd);
1781 * Disable the IB link, disable interrupts on the device,
1782 * clear dma engines, etc.
1784 shutdown_device(dd);
1785 destroy_workqueues(dd);
1789 /* wait until all of our (qsfp) queue_work() calls complete */
1790 flush_workqueue(ib_wq);
1792 postinit_cleanup(dd);
1795 static void shutdown_one(struct pci_dev *pdev)
1797 struct hfi1_devdata *dd = pci_get_drvdata(pdev);
1799 shutdown_device(dd);
1803 * hfi1_create_rcvhdrq - create a receive header queue
1804 * @dd: the hfi1_ib device
1805 * @rcd: the context data
1807 * This must be contiguous memory (from an i/o perspective), and must be
1808 * DMA'able (which means for some systems, it will go through an IOMMU,
1809 * or be forced into a low address range).
1811 int hfi1_create_rcvhdrq(struct hfi1_devdata *dd, struct hfi1_ctxtdata *rcd)
1815 if (!rcd->rcvhdrq) {
1818 amt = rcvhdrq_size(rcd);
1820 if (rcd->ctxt < dd->first_dyn_alloc_ctxt || rcd->is_vnic)
1821 gfp_flags = GFP_KERNEL;
1823 gfp_flags = GFP_USER;
1824 rcd->rcvhdrq = dma_alloc_coherent(&dd->pcidev->dev, amt,
1826 gfp_flags | __GFP_COMP);
1828 if (!rcd->rcvhdrq) {
1830 "attempt to allocate %d bytes for ctxt %u rcvhdrq failed\n",
1835 if (HFI1_CAP_KGET_MASK(rcd->flags, DMA_RTAIL) ||
1836 HFI1_CAP_UGET_MASK(rcd->flags, DMA_RTAIL)) {
1837 rcd->rcvhdrtail_kvaddr = dma_alloc_coherent(&dd->pcidev->dev,
1839 &rcd->rcvhdrqtailaddr_dma,
1841 if (!rcd->rcvhdrtail_kvaddr)
1846 set_hdrq_regs(rcd->dd, rcd->ctxt, rcd->rcvhdrqentsize,
1853 "attempt to allocate 1 page for ctxt %u rcvhdrqtailaddr failed\n",
1855 dma_free_coherent(&dd->pcidev->dev, amt, rcd->rcvhdrq,
1857 rcd->rcvhdrq = NULL;
1863 * allocate eager buffers, both kernel and user contexts.
1864 * @rcd: the context we are setting up.
1866 * Allocate the eager TID buffers and program them into hip.
1867 * They are no longer completely contiguous, we do multiple allocation
1868 * calls. Otherwise we get the OOM code involved, by asking for too
1869 * much per call, with disastrous results on some kernels.
1871 int hfi1_setup_eagerbufs(struct hfi1_ctxtdata *rcd)
1873 struct hfi1_devdata *dd = rcd->dd;
1874 u32 max_entries, egrtop, alloced_bytes = 0;
1878 u16 round_mtu = roundup_pow_of_two(hfi1_max_mtu);
1881 * GFP_USER, but without GFP_FS, so buffer cache can be
1882 * coalesced (we hope); otherwise, even at order 4,
1883 * heavy filesystem activity makes these fail, and we can
1884 * use compound pages.
1886 gfp_flags = __GFP_RECLAIM | __GFP_IO | __GFP_COMP;
1889 * The minimum size of the eager buffers is a groups of MTU-sized
1891 * The global eager_buffer_size parameter is checked against the
1892 * theoretical lower limit of the value. Here, we check against the
1895 if (rcd->egrbufs.size < (round_mtu * dd->rcv_entries.group_size))
1896 rcd->egrbufs.size = round_mtu * dd->rcv_entries.group_size;
1898 * If using one-pkt-per-egr-buffer, lower the eager buffer
1899 * size to the max MTU (page-aligned).
1901 if (!HFI1_CAP_KGET_MASK(rcd->flags, MULTI_PKT_EGR))
1902 rcd->egrbufs.rcvtid_size = round_mtu;
1905 * Eager buffers sizes of 1MB or less require smaller TID sizes
1906 * to satisfy the "multiple of 8 RcvArray entries" requirement.
1908 if (rcd->egrbufs.size <= (1 << 20))
1909 rcd->egrbufs.rcvtid_size = max((unsigned long)round_mtu,
1910 rounddown_pow_of_two(rcd->egrbufs.size / 8));
1912 while (alloced_bytes < rcd->egrbufs.size &&
1913 rcd->egrbufs.alloced < rcd->egrbufs.count) {
1914 rcd->egrbufs.buffers[idx].addr =
1915 dma_alloc_coherent(&dd->pcidev->dev,
1916 rcd->egrbufs.rcvtid_size,
1917 &rcd->egrbufs.buffers[idx].dma,
1919 if (rcd->egrbufs.buffers[idx].addr) {
1920 rcd->egrbufs.buffers[idx].len =
1921 rcd->egrbufs.rcvtid_size;
1922 rcd->egrbufs.rcvtids[rcd->egrbufs.alloced].addr =
1923 rcd->egrbufs.buffers[idx].addr;
1924 rcd->egrbufs.rcvtids[rcd->egrbufs.alloced].dma =
1925 rcd->egrbufs.buffers[idx].dma;
1926 rcd->egrbufs.alloced++;
1927 alloced_bytes += rcd->egrbufs.rcvtid_size;
1934 * Fail the eager buffer allocation if:
1935 * - we are already using the lowest acceptable size
1936 * - we are using one-pkt-per-egr-buffer (this implies
1937 * that we are accepting only one size)
1939 if (rcd->egrbufs.rcvtid_size == round_mtu ||
1940 !HFI1_CAP_KGET_MASK(rcd->flags, MULTI_PKT_EGR)) {
1941 dd_dev_err(dd, "ctxt%u: Failed to allocate eager buffers\n",
1944 goto bail_rcvegrbuf_phys;
1947 new_size = rcd->egrbufs.rcvtid_size / 2;
1950 * If the first attempt to allocate memory failed, don't
1951 * fail everything but continue with the next lower
1955 rcd->egrbufs.rcvtid_size = new_size;
1960 * Re-partition already allocated buffers to a smaller
1963 rcd->egrbufs.alloced = 0;
1964 for (i = 0, j = 0, offset = 0; j < idx; i++) {
1965 if (i >= rcd->egrbufs.count)
1967 rcd->egrbufs.rcvtids[i].dma =
1968 rcd->egrbufs.buffers[j].dma + offset;
1969 rcd->egrbufs.rcvtids[i].addr =
1970 rcd->egrbufs.buffers[j].addr + offset;
1971 rcd->egrbufs.alloced++;
1972 if ((rcd->egrbufs.buffers[j].dma + offset +
1974 (rcd->egrbufs.buffers[j].dma +
1975 rcd->egrbufs.buffers[j].len)) {
1982 rcd->egrbufs.rcvtid_size = new_size;
1985 rcd->egrbufs.numbufs = idx;
1986 rcd->egrbufs.size = alloced_bytes;
1989 "ctxt%u: Alloced %u rcv tid entries @ %uKB, total %uKB\n",
1990 rcd->ctxt, rcd->egrbufs.alloced,
1991 rcd->egrbufs.rcvtid_size / 1024, rcd->egrbufs.size / 1024);
1994 * Set the contexts rcv array head update threshold to the closest
1995 * power of 2 (so we can use a mask instead of modulo) below half
1996 * the allocated entries.
1998 rcd->egrbufs.threshold =
1999 rounddown_pow_of_two(rcd->egrbufs.alloced / 2);
2001 * Compute the expected RcvArray entry base. This is done after
2002 * allocating the eager buffers in order to maximize the
2003 * expected RcvArray entries for the context.
2005 max_entries = rcd->rcv_array_groups * dd->rcv_entries.group_size;
2006 egrtop = roundup(rcd->egrbufs.alloced, dd->rcv_entries.group_size);
2007 rcd->expected_count = max_entries - egrtop;
2008 if (rcd->expected_count > MAX_TID_PAIR_ENTRIES * 2)
2009 rcd->expected_count = MAX_TID_PAIR_ENTRIES * 2;
2011 rcd->expected_base = rcd->eager_base + egrtop;
2012 hfi1_cdbg(PROC, "ctxt%u: eager:%u, exp:%u, egrbase:%u, expbase:%u\n",
2013 rcd->ctxt, rcd->egrbufs.alloced, rcd->expected_count,
2014 rcd->eager_base, rcd->expected_base);
2016 if (!hfi1_rcvbuf_validate(rcd->egrbufs.rcvtid_size, PT_EAGER, &order)) {
2018 "ctxt%u: current Eager buffer size is invalid %u\n",
2019 rcd->ctxt, rcd->egrbufs.rcvtid_size);
2021 goto bail_rcvegrbuf_phys;
2024 for (idx = 0; idx < rcd->egrbufs.alloced; idx++) {
2025 hfi1_put_tid(dd, rcd->eager_base + idx, PT_EAGER,
2026 rcd->egrbufs.rcvtids[idx].dma, order);
2032 bail_rcvegrbuf_phys:
2033 for (idx = 0; idx < rcd->egrbufs.alloced &&
2034 rcd->egrbufs.buffers[idx].addr;
2036 dma_free_coherent(&dd->pcidev->dev,
2037 rcd->egrbufs.buffers[idx].len,
2038 rcd->egrbufs.buffers[idx].addr,
2039 rcd->egrbufs.buffers[idx].dma);
2040 rcd->egrbufs.buffers[idx].addr = NULL;
2041 rcd->egrbufs.buffers[idx].dma = 0;
2042 rcd->egrbufs.buffers[idx].len = 0;