1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause 2 /* 3 * Copyright(c) 2015 - 2020 Intel Corporation. 4 * Copyright(c) 2021 Cornelis Networks. 5 */ 6 7 #include <linux/pci.h> 8 #include <linux/netdevice.h> 9 #include <linux/vmalloc.h> 10 #include <linux/delay.h> 11 #include <linux/xarray.h> 12 #include <linux/module.h> 13 #include <linux/printk.h> 14 #include <linux/hrtimer.h> 15 #include <linux/bitmap.h> 16 #include <linux/numa.h> 17 #include <rdma/rdma_vt.h> 18 19 #include "hfi.h" 20 #include "device.h" 21 #include "common.h" 22 #include "trace.h" 23 #include "mad.h" 24 #include "sdma.h" 25 #include "debugfs.h" 26 #include "verbs.h" 27 #include "aspm.h" 28 #include "affinity.h" 29 #include "exp_rcv.h" 30 #include "netdev.h" 31 32 #undef pr_fmt 33 #define pr_fmt(fmt) DRIVER_NAME ": " fmt 34 35 /* 36 * min buffers we want to have per context, after driver 37 */ 38 #define HFI1_MIN_USER_CTXT_BUFCNT 7 39 40 #define HFI1_MIN_EAGER_BUFFER_SIZE (4 * 1024) /* 4KB */ 41 #define HFI1_MAX_EAGER_BUFFER_SIZE (256 * 1024) /* 256KB */ 42 43 #define NUM_IB_PORTS 1 44 45 /* 46 * Number of user receive contexts we are configured to use (to allow for more 47 * pio buffers per ctxt, etc.) Zero means use one user context per CPU. 48 */ 49 int num_user_contexts = -1; 50 module_param_named(num_user_contexts, num_user_contexts, int, 0444); 51 MODULE_PARM_DESC( 52 num_user_contexts, "Set max number of user contexts to use (default: -1 will use the real (non-HT) CPU count)"); 53 54 uint krcvqs[RXE_NUM_DATA_VL]; 55 int krcvqsset; 56 module_param_array(krcvqs, uint, &krcvqsset, S_IRUGO); 57 MODULE_PARM_DESC(krcvqs, "Array of the number of non-control kernel receive queues by VL"); 58 59 /* computed based on above array */ 60 unsigned long n_krcvqs; 61 62 static unsigned hfi1_rcvarr_split = 25; 63 module_param_named(rcvarr_split, hfi1_rcvarr_split, uint, S_IRUGO); 64 MODULE_PARM_DESC(rcvarr_split, "Percent of context's RcvArray entries used for Eager buffers"); 65 66 static uint eager_buffer_size = (8 << 20); /* 8MB */ 67 module_param(eager_buffer_size, uint, S_IRUGO); 68 MODULE_PARM_DESC(eager_buffer_size, "Size of the eager buffers, default: 8MB"); 69 70 static uint rcvhdrcnt = 2048; /* 2x the max eager buffer count */ 71 module_param_named(rcvhdrcnt, rcvhdrcnt, uint, S_IRUGO); 72 MODULE_PARM_DESC(rcvhdrcnt, "Receive header queue count (default 2048)"); 73 74 static uint hfi1_hdrq_entsize = 32; 75 module_param_named(hdrq_entsize, hfi1_hdrq_entsize, uint, 0444); 76 MODULE_PARM_DESC(hdrq_entsize, "Size of header queue entries: 2 - 8B, 16 - 64B, 32 - 128B (default)"); 77 78 unsigned int user_credit_return_threshold = 33; /* default is 33% */ 79 module_param(user_credit_return_threshold, uint, S_IRUGO); 80 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)"); 81 82 DEFINE_XARRAY_FLAGS(hfi1_dev_table, XA_FLAGS_ALLOC | XA_FLAGS_LOCK_IRQ); 83 84 static int hfi1_create_kctxt(struct hfi1_devdata *dd, 85 struct hfi1_pportdata *ppd) 86 { 87 struct hfi1_ctxtdata *rcd; 88 int ret; 89 90 /* Control context has to be always 0 */ 91 BUILD_BUG_ON(HFI1_CTRL_CTXT != 0); 92 93 ret = hfi1_create_ctxtdata(ppd, dd->node, &rcd); 94 if (ret < 0) { 95 dd_dev_err(dd, "Kernel receive context allocation failed\n"); 96 return ret; 97 } 98 99 /* 100 * Set up the kernel context flags here and now because they use 101 * default values for all receive side memories. User contexts will 102 * be handled as they are created. 103 */ 104 rcd->flags = HFI1_CAP_KGET(MULTI_PKT_EGR) | 105 HFI1_CAP_KGET(NODROP_RHQ_FULL) | 106 HFI1_CAP_KGET(NODROP_EGR_FULL) | 107 HFI1_CAP_KGET(DMA_RTAIL); 108 109 /* Control context must use DMA_RTAIL */ 110 if (rcd->ctxt == HFI1_CTRL_CTXT) 111 rcd->flags |= HFI1_CAP_DMA_RTAIL; 112 rcd->fast_handler = get_dma_rtail_setting(rcd) ? 113 handle_receive_interrupt_dma_rtail : 114 handle_receive_interrupt_nodma_rtail; 115 116 hfi1_set_seq_cnt(rcd, 1); 117 118 rcd->sc = sc_alloc(dd, SC_ACK, rcd->rcvhdrqentsize, dd->node); 119 if (!rcd->sc) { 120 dd_dev_err(dd, "Kernel send context allocation failed\n"); 121 return -ENOMEM; 122 } 123 hfi1_init_ctxt(rcd->sc); 124 125 return 0; 126 } 127 128 /* 129 * Create the receive context array and one or more kernel contexts 130 */ 131 int hfi1_create_kctxts(struct hfi1_devdata *dd) 132 { 133 u16 i; 134 int ret; 135 136 dd->rcd = kcalloc_node(dd->num_rcv_contexts, sizeof(*dd->rcd), 137 GFP_KERNEL, dd->node); 138 if (!dd->rcd) 139 return -ENOMEM; 140 141 for (i = 0; i < dd->first_dyn_alloc_ctxt; ++i) { 142 ret = hfi1_create_kctxt(dd, dd->pport); 143 if (ret) 144 goto bail; 145 } 146 147 return 0; 148 bail: 149 for (i = 0; dd->rcd && i < dd->first_dyn_alloc_ctxt; ++i) 150 hfi1_free_ctxt(dd->rcd[i]); 151 152 /* All the contexts should be freed, free the array */ 153 kfree(dd->rcd); 154 dd->rcd = NULL; 155 return ret; 156 } 157 158 /* 159 * Helper routines for the receive context reference count (rcd and uctxt). 160 */ 161 static void hfi1_rcd_init(struct hfi1_ctxtdata *rcd) 162 { 163 kref_init(&rcd->kref); 164 } 165 166 /** 167 * hfi1_rcd_free - When reference is zero clean up. 168 * @kref: pointer to an initialized rcd data structure 169 * 170 */ 171 static void hfi1_rcd_free(struct kref *kref) 172 { 173 unsigned long flags; 174 struct hfi1_ctxtdata *rcd = 175 container_of(kref, struct hfi1_ctxtdata, kref); 176 177 spin_lock_irqsave(&rcd->dd->uctxt_lock, flags); 178 rcd->dd->rcd[rcd->ctxt] = NULL; 179 spin_unlock_irqrestore(&rcd->dd->uctxt_lock, flags); 180 181 hfi1_free_ctxtdata(rcd->dd, rcd); 182 183 kfree(rcd); 184 } 185 186 /** 187 * hfi1_rcd_put - decrement reference for rcd 188 * @rcd: pointer to an initialized rcd data structure 189 * 190 * Use this to put a reference after the init. 191 */ 192 int hfi1_rcd_put(struct hfi1_ctxtdata *rcd) 193 { 194 if (rcd) 195 return kref_put(&rcd->kref, hfi1_rcd_free); 196 197 return 0; 198 } 199 200 /** 201 * hfi1_rcd_get - increment reference for rcd 202 * @rcd: pointer to an initialized rcd data structure 203 * 204 * Use this to get a reference after the init. 205 * 206 * Return : reflect kref_get_unless_zero(), which returns non-zero on 207 * increment, otherwise 0. 208 */ 209 int hfi1_rcd_get(struct hfi1_ctxtdata *rcd) 210 { 211 return kref_get_unless_zero(&rcd->kref); 212 } 213 214 /** 215 * allocate_rcd_index - allocate an rcd index from the rcd array 216 * @dd: pointer to a valid devdata structure 217 * @rcd: rcd data structure to assign 218 * @index: pointer to index that is allocated 219 * 220 * Find an empty index in the rcd array, and assign the given rcd to it. 221 * If the array is full, we are EBUSY. 222 * 223 */ 224 static int allocate_rcd_index(struct hfi1_devdata *dd, 225 struct hfi1_ctxtdata *rcd, u16 *index) 226 { 227 unsigned long flags; 228 u16 ctxt; 229 230 spin_lock_irqsave(&dd->uctxt_lock, flags); 231 for (ctxt = 0; ctxt < dd->num_rcv_contexts; ctxt++) 232 if (!dd->rcd[ctxt]) 233 break; 234 235 if (ctxt < dd->num_rcv_contexts) { 236 rcd->ctxt = ctxt; 237 dd->rcd[ctxt] = rcd; 238 hfi1_rcd_init(rcd); 239 } 240 spin_unlock_irqrestore(&dd->uctxt_lock, flags); 241 242 if (ctxt >= dd->num_rcv_contexts) 243 return -EBUSY; 244 245 *index = ctxt; 246 247 return 0; 248 } 249 250 /** 251 * hfi1_rcd_get_by_index_safe - validate the ctxt index before accessing the 252 * array 253 * @dd: pointer to a valid devdata structure 254 * @ctxt: the index of an possilbe rcd 255 * 256 * This is a wrapper for hfi1_rcd_get_by_index() to validate that the given 257 * ctxt index is valid. 258 * 259 * The caller is responsible for making the _put(). 260 * 261 */ 262 struct hfi1_ctxtdata *hfi1_rcd_get_by_index_safe(struct hfi1_devdata *dd, 263 u16 ctxt) 264 { 265 if (ctxt < dd->num_rcv_contexts) 266 return hfi1_rcd_get_by_index(dd, ctxt); 267 268 return NULL; 269 } 270 271 /** 272 * hfi1_rcd_get_by_index - get by index 273 * @dd: pointer to a valid devdata structure 274 * @ctxt: the index of an possilbe rcd 275 * 276 * We need to protect access to the rcd array. If access is needed to 277 * one or more index, get the protecting spinlock and then increment the 278 * kref. 279 * 280 * The caller is responsible for making the _put(). 281 * 282 */ 283 struct hfi1_ctxtdata *hfi1_rcd_get_by_index(struct hfi1_devdata *dd, u16 ctxt) 284 { 285 unsigned long flags; 286 struct hfi1_ctxtdata *rcd = NULL; 287 288 spin_lock_irqsave(&dd->uctxt_lock, flags); 289 if (dd->rcd[ctxt]) { 290 rcd = dd->rcd[ctxt]; 291 if (!hfi1_rcd_get(rcd)) 292 rcd = NULL; 293 } 294 spin_unlock_irqrestore(&dd->uctxt_lock, flags); 295 296 return rcd; 297 } 298 299 /* 300 * Common code for user and kernel context create and setup. 301 * NOTE: the initial kref is done here (hf1_rcd_init()). 302 */ 303 int hfi1_create_ctxtdata(struct hfi1_pportdata *ppd, int numa, 304 struct hfi1_ctxtdata **context) 305 { 306 struct hfi1_devdata *dd = ppd->dd; 307 struct hfi1_ctxtdata *rcd; 308 unsigned kctxt_ngroups = 0; 309 u32 base; 310 311 if (dd->rcv_entries.nctxt_extra > 312 dd->num_rcv_contexts - dd->first_dyn_alloc_ctxt) 313 kctxt_ngroups = (dd->rcv_entries.nctxt_extra - 314 (dd->num_rcv_contexts - dd->first_dyn_alloc_ctxt)); 315 rcd = kzalloc_node(sizeof(*rcd), GFP_KERNEL, numa); 316 if (rcd) { 317 u32 rcvtids, max_entries; 318 u16 ctxt; 319 int ret; 320 321 ret = allocate_rcd_index(dd, rcd, &ctxt); 322 if (ret) { 323 *context = NULL; 324 kfree(rcd); 325 return ret; 326 } 327 328 INIT_LIST_HEAD(&rcd->qp_wait_list); 329 hfi1_exp_tid_group_init(rcd); 330 rcd->ppd = ppd; 331 rcd->dd = dd; 332 rcd->numa_id = numa; 333 rcd->rcv_array_groups = dd->rcv_entries.ngroups; 334 rcd->rhf_rcv_function_map = normal_rhf_rcv_functions; 335 rcd->slow_handler = handle_receive_interrupt; 336 rcd->do_interrupt = rcd->slow_handler; 337 rcd->msix_intr = CCE_NUM_MSIX_VECTORS; 338 339 mutex_init(&rcd->exp_mutex); 340 spin_lock_init(&rcd->exp_lock); 341 INIT_LIST_HEAD(&rcd->flow_queue.queue_head); 342 INIT_LIST_HEAD(&rcd->rarr_queue.queue_head); 343 344 hfi1_cdbg(PROC, "setting up context %u", rcd->ctxt); 345 346 /* 347 * Calculate the context's RcvArray entry starting point. 348 * We do this here because we have to take into account all 349 * the RcvArray entries that previous context would have 350 * taken and we have to account for any extra groups assigned 351 * to the static (kernel) or dynamic (user) contexts. 352 */ 353 if (ctxt < dd->first_dyn_alloc_ctxt) { 354 if (ctxt < kctxt_ngroups) { 355 base = ctxt * (dd->rcv_entries.ngroups + 1); 356 rcd->rcv_array_groups++; 357 } else { 358 base = kctxt_ngroups + 359 (ctxt * dd->rcv_entries.ngroups); 360 } 361 } else { 362 u16 ct = ctxt - dd->first_dyn_alloc_ctxt; 363 364 base = ((dd->n_krcv_queues * dd->rcv_entries.ngroups) + 365 kctxt_ngroups); 366 if (ct < dd->rcv_entries.nctxt_extra) { 367 base += ct * (dd->rcv_entries.ngroups + 1); 368 rcd->rcv_array_groups++; 369 } else { 370 base += dd->rcv_entries.nctxt_extra + 371 (ct * dd->rcv_entries.ngroups); 372 } 373 } 374 rcd->eager_base = base * dd->rcv_entries.group_size; 375 376 rcd->rcvhdrq_cnt = rcvhdrcnt; 377 rcd->rcvhdrqentsize = hfi1_hdrq_entsize; 378 rcd->rhf_offset = 379 rcd->rcvhdrqentsize - sizeof(u64) / sizeof(u32); 380 /* 381 * Simple Eager buffer allocation: we have already pre-allocated 382 * the number of RcvArray entry groups. Each ctxtdata structure 383 * holds the number of groups for that context. 384 * 385 * To follow CSR requirements and maintain cacheline alignment, 386 * make sure all sizes and bases are multiples of group_size. 387 * 388 * The expected entry count is what is left after assigning 389 * eager. 390 */ 391 max_entries = rcd->rcv_array_groups * 392 dd->rcv_entries.group_size; 393 rcvtids = ((max_entries * hfi1_rcvarr_split) / 100); 394 rcd->egrbufs.count = round_down(rcvtids, 395 dd->rcv_entries.group_size); 396 if (rcd->egrbufs.count > MAX_EAGER_ENTRIES) { 397 dd_dev_err(dd, "ctxt%u: requested too many RcvArray entries.\n", 398 rcd->ctxt); 399 rcd->egrbufs.count = MAX_EAGER_ENTRIES; 400 } 401 hfi1_cdbg(PROC, 402 "ctxt%u: max Eager buffer RcvArray entries: %u", 403 rcd->ctxt, rcd->egrbufs.count); 404 405 /* 406 * Allocate array that will hold the eager buffer accounting 407 * data. 408 * This will allocate the maximum possible buffer count based 409 * on the value of the RcvArray split parameter. 410 * The resulting value will be rounded down to the closest 411 * multiple of dd->rcv_entries.group_size. 412 */ 413 rcd->egrbufs.buffers = 414 kcalloc_node(rcd->egrbufs.count, 415 sizeof(*rcd->egrbufs.buffers), 416 GFP_KERNEL, numa); 417 if (!rcd->egrbufs.buffers) 418 goto bail; 419 rcd->egrbufs.rcvtids = 420 kcalloc_node(rcd->egrbufs.count, 421 sizeof(*rcd->egrbufs.rcvtids), 422 GFP_KERNEL, numa); 423 if (!rcd->egrbufs.rcvtids) 424 goto bail; 425 rcd->egrbufs.size = eager_buffer_size; 426 /* 427 * The size of the buffers programmed into the RcvArray 428 * entries needs to be big enough to handle the highest 429 * MTU supported. 430 */ 431 if (rcd->egrbufs.size < hfi1_max_mtu) { 432 rcd->egrbufs.size = __roundup_pow_of_two(hfi1_max_mtu); 433 hfi1_cdbg(PROC, 434 "ctxt%u: eager bufs size too small. Adjusting to %u", 435 rcd->ctxt, rcd->egrbufs.size); 436 } 437 rcd->egrbufs.rcvtid_size = HFI1_MAX_EAGER_BUFFER_SIZE; 438 439 /* Applicable only for statically created kernel contexts */ 440 if (ctxt < dd->first_dyn_alloc_ctxt) { 441 rcd->opstats = kzalloc_node(sizeof(*rcd->opstats), 442 GFP_KERNEL, numa); 443 if (!rcd->opstats) 444 goto bail; 445 446 /* Initialize TID flow generations for the context */ 447 hfi1_kern_init_ctxt_generations(rcd); 448 } 449 450 *context = rcd; 451 return 0; 452 } 453 454 bail: 455 *context = NULL; 456 hfi1_free_ctxt(rcd); 457 return -ENOMEM; 458 } 459 460 /** 461 * hfi1_free_ctxt - free context 462 * @rcd: pointer to an initialized rcd data structure 463 * 464 * This wrapper is the free function that matches hfi1_create_ctxtdata(). 465 * When a context is done being used (kernel or user), this function is called 466 * for the "final" put to match the kref init from hfi1_create_ctxtdata(). 467 * Other users of the context do a get/put sequence to make sure that the 468 * structure isn't removed while in use. 469 */ 470 void hfi1_free_ctxt(struct hfi1_ctxtdata *rcd) 471 { 472 hfi1_rcd_put(rcd); 473 } 474 475 /* 476 * Select the largest ccti value over all SLs to determine the intra- 477 * packet gap for the link. 478 * 479 * called with cca_timer_lock held (to protect access to cca_timer 480 * array), and rcu_read_lock() (to protect access to cc_state). 481 */ 482 void set_link_ipg(struct hfi1_pportdata *ppd) 483 { 484 struct hfi1_devdata *dd = ppd->dd; 485 struct cc_state *cc_state; 486 int i; 487 u16 cce, ccti_limit, max_ccti = 0; 488 u16 shift, mult; 489 u64 src; 490 u32 current_egress_rate; /* Mbits /sec */ 491 u64 max_pkt_time; 492 /* 493 * max_pkt_time is the maximum packet egress time in units 494 * of the fabric clock period 1/(805 MHz). 495 */ 496 497 cc_state = get_cc_state(ppd); 498 499 if (!cc_state) 500 /* 501 * This should _never_ happen - rcu_read_lock() is held, 502 * and set_link_ipg() should not be called if cc_state 503 * is NULL. 504 */ 505 return; 506 507 for (i = 0; i < OPA_MAX_SLS; i++) { 508 u16 ccti = ppd->cca_timer[i].ccti; 509 510 if (ccti > max_ccti) 511 max_ccti = ccti; 512 } 513 514 ccti_limit = cc_state->cct.ccti_limit; 515 if (max_ccti > ccti_limit) 516 max_ccti = ccti_limit; 517 518 cce = cc_state->cct.entries[max_ccti].entry; 519 shift = (cce & 0xc000) >> 14; 520 mult = (cce & 0x3fff); 521 522 current_egress_rate = active_egress_rate(ppd); 523 524 max_pkt_time = egress_cycles(ppd->ibmaxlen, current_egress_rate); 525 526 src = (max_pkt_time >> shift) * mult; 527 528 src &= SEND_STATIC_RATE_CONTROL_CSR_SRC_RELOAD_SMASK; 529 src <<= SEND_STATIC_RATE_CONTROL_CSR_SRC_RELOAD_SHIFT; 530 531 write_csr(dd, SEND_STATIC_RATE_CONTROL, src); 532 } 533 534 static enum hrtimer_restart cca_timer_fn(struct hrtimer *t) 535 { 536 struct cca_timer *cca_timer; 537 struct hfi1_pportdata *ppd; 538 int sl; 539 u16 ccti_timer, ccti_min; 540 struct cc_state *cc_state; 541 unsigned long flags; 542 enum hrtimer_restart ret = HRTIMER_NORESTART; 543 544 cca_timer = container_of(t, struct cca_timer, hrtimer); 545 ppd = cca_timer->ppd; 546 sl = cca_timer->sl; 547 548 rcu_read_lock(); 549 550 cc_state = get_cc_state(ppd); 551 552 if (!cc_state) { 553 rcu_read_unlock(); 554 return HRTIMER_NORESTART; 555 } 556 557 /* 558 * 1) decrement ccti for SL 559 * 2) calculate IPG for link (set_link_ipg()) 560 * 3) restart timer, unless ccti is at min value 561 */ 562 563 ccti_min = cc_state->cong_setting.entries[sl].ccti_min; 564 ccti_timer = cc_state->cong_setting.entries[sl].ccti_timer; 565 566 spin_lock_irqsave(&ppd->cca_timer_lock, flags); 567 568 if (cca_timer->ccti > ccti_min) { 569 cca_timer->ccti--; 570 set_link_ipg(ppd); 571 } 572 573 if (cca_timer->ccti > ccti_min) { 574 unsigned long nsec = 1024 * ccti_timer; 575 /* ccti_timer is in units of 1.024 usec */ 576 hrtimer_forward_now(t, ns_to_ktime(nsec)); 577 ret = HRTIMER_RESTART; 578 } 579 580 spin_unlock_irqrestore(&ppd->cca_timer_lock, flags); 581 rcu_read_unlock(); 582 return ret; 583 } 584 585 /* 586 * Common code for initializing the physical port structure. 587 */ 588 void hfi1_init_pportdata(struct pci_dev *pdev, struct hfi1_pportdata *ppd, 589 struct hfi1_devdata *dd, u8 hw_pidx, u32 port) 590 { 591 int i; 592 uint default_pkey_idx; 593 struct cc_state *cc_state; 594 595 ppd->dd = dd; 596 ppd->hw_pidx = hw_pidx; 597 ppd->port = port; /* IB port number, not index */ 598 ppd->prev_link_width = LINK_WIDTH_DEFAULT; 599 /* 600 * There are C_VL_COUNT number of PortVLXmitWait counters. 601 * Adding 1 to C_VL_COUNT to include the PortXmitWait counter. 602 */ 603 for (i = 0; i < C_VL_COUNT + 1; i++) { 604 ppd->port_vl_xmit_wait_last[i] = 0; 605 ppd->vl_xmit_flit_cnt[i] = 0; 606 } 607 608 default_pkey_idx = 1; 609 610 ppd->pkeys[default_pkey_idx] = DEFAULT_P_KEY; 611 ppd->part_enforce |= HFI1_PART_ENFORCE_IN; 612 ppd->pkeys[0] = 0x8001; 613 614 INIT_WORK(&ppd->link_vc_work, handle_verify_cap); 615 INIT_WORK(&ppd->link_up_work, handle_link_up); 616 INIT_WORK(&ppd->link_down_work, handle_link_down); 617 INIT_WORK(&ppd->freeze_work, handle_freeze); 618 INIT_WORK(&ppd->link_downgrade_work, handle_link_downgrade); 619 INIT_WORK(&ppd->sma_message_work, handle_sma_message); 620 INIT_WORK(&ppd->link_bounce_work, handle_link_bounce); 621 INIT_DELAYED_WORK(&ppd->start_link_work, handle_start_link); 622 INIT_WORK(&ppd->linkstate_active_work, receive_interrupt_work); 623 INIT_WORK(&ppd->qsfp_info.qsfp_work, qsfp_event); 624 625 mutex_init(&ppd->hls_lock); 626 spin_lock_init(&ppd->qsfp_info.qsfp_lock); 627 628 ppd->qsfp_info.ppd = ppd; 629 ppd->sm_trap_qp = 0x0; 630 ppd->sa_qp = 0x1; 631 632 spin_lock_init(&ppd->cca_timer_lock); 633 634 for (i = 0; i < OPA_MAX_SLS; i++) { 635 ppd->cca_timer[i].ppd = ppd; 636 ppd->cca_timer[i].sl = i; 637 ppd->cca_timer[i].ccti = 0; 638 hrtimer_setup(&ppd->cca_timer[i].hrtimer, cca_timer_fn, CLOCK_MONOTONIC, 639 HRTIMER_MODE_REL); 640 } 641 642 ppd->cc_max_table_entries = IB_CC_TABLE_CAP_DEFAULT; 643 644 spin_lock_init(&ppd->cc_state_lock); 645 spin_lock_init(&ppd->cc_log_lock); 646 cc_state = kzalloc_obj(*cc_state); 647 RCU_INIT_POINTER(ppd->cc_state, cc_state); 648 if (!cc_state) 649 goto bail; 650 return; 651 652 bail: 653 dd_dev_err(dd, "Congestion Control Agent disabled for port %d\n", port); 654 } 655 656 /* 657 * Do initialization for device that is only needed on 658 * first detect, not on resets. 659 */ 660 static int loadtime_init(struct hfi1_devdata *dd) 661 { 662 return 0; 663 } 664 665 /** 666 * init_after_reset - re-initialize after a reset 667 * @dd: the hfi1_ib device 668 * 669 * sanity check at least some of the values after reset, and 670 * ensure no receive or transmit (explicitly, in case reset 671 * failed 672 */ 673 static int init_after_reset(struct hfi1_devdata *dd) 674 { 675 int i; 676 struct hfi1_ctxtdata *rcd; 677 /* 678 * Ensure chip does no sends or receives, tail updates, or 679 * pioavail updates while we re-initialize. This is mostly 680 * for the driver data structures, not chip registers. 681 */ 682 for (i = 0; i < dd->num_rcv_contexts; i++) { 683 rcd = hfi1_rcd_get_by_index(dd, i); 684 hfi1_rcvctrl(dd, HFI1_RCVCTRL_CTXT_DIS | 685 HFI1_RCVCTRL_INTRAVAIL_DIS | 686 HFI1_RCVCTRL_TAILUPD_DIS, rcd); 687 hfi1_rcd_put(rcd); 688 } 689 pio_send_control(dd, PSC_GLOBAL_DISABLE); 690 for (i = 0; i < dd->num_send_contexts; i++) 691 sc_disable(dd->send_contexts[i].sc); 692 693 return 0; 694 } 695 696 static void enable_chip(struct hfi1_devdata *dd) 697 { 698 struct hfi1_ctxtdata *rcd; 699 u32 rcvmask; 700 u16 i; 701 702 /* enable PIO send */ 703 pio_send_control(dd, PSC_GLOBAL_ENABLE); 704 705 /* 706 * Enable kernel ctxts' receive and receive interrupt. 707 * Other ctxts done as user opens and initializes them. 708 */ 709 for (i = 0; i < dd->first_dyn_alloc_ctxt; ++i) { 710 rcd = hfi1_rcd_get_by_index(dd, i); 711 if (!rcd) 712 continue; 713 rcvmask = HFI1_RCVCTRL_CTXT_ENB | HFI1_RCVCTRL_INTRAVAIL_ENB; 714 rcvmask |= HFI1_CAP_KGET_MASK(rcd->flags, DMA_RTAIL) ? 715 HFI1_RCVCTRL_TAILUPD_ENB : HFI1_RCVCTRL_TAILUPD_DIS; 716 if (!HFI1_CAP_KGET_MASK(rcd->flags, MULTI_PKT_EGR)) 717 rcvmask |= HFI1_RCVCTRL_ONE_PKT_EGR_ENB; 718 if (HFI1_CAP_KGET_MASK(rcd->flags, NODROP_RHQ_FULL)) 719 rcvmask |= HFI1_RCVCTRL_NO_RHQ_DROP_ENB; 720 if (HFI1_CAP_KGET_MASK(rcd->flags, NODROP_EGR_FULL)) 721 rcvmask |= HFI1_RCVCTRL_NO_EGR_DROP_ENB; 722 if (HFI1_CAP_IS_KSET(TID_RDMA)) 723 rcvmask |= HFI1_RCVCTRL_TIDFLOW_ENB; 724 hfi1_rcvctrl(dd, rcvmask, rcd); 725 sc_enable(rcd->sc); 726 hfi1_rcd_put(rcd); 727 } 728 } 729 730 /** 731 * create_workqueues - create per port workqueues 732 * @dd: the hfi1_ib device 733 */ 734 static int create_workqueues(struct hfi1_devdata *dd) 735 { 736 int pidx; 737 struct hfi1_pportdata *ppd; 738 739 for (pidx = 0; pidx < dd->num_pports; ++pidx) { 740 ppd = dd->pport + pidx; 741 ppd->hfi1_wq = 742 alloc_workqueue( 743 "hfi%d_%d", 744 WQ_SYSFS | WQ_HIGHPRI | WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM | 745 WQ_PERCPU, 746 HFI1_MAX_ACTIVE_WORKQUEUE_ENTRIES, 747 dd->unit, pidx); 748 if (!ppd->hfi1_wq) 749 goto wq_error; 750 /* 751 * Make the link workqueue single-threaded to enforce 752 * serialization. 753 */ 754 ppd->link_wq = 755 alloc_workqueue( 756 "hfi_link_%d_%d", 757 WQ_SYSFS | WQ_MEM_RECLAIM | WQ_UNBOUND, 758 1, /* max_active */ 759 dd->unit, pidx); 760 if (!ppd->link_wq) 761 goto wq_error; 762 } 763 return 0; 764 wq_error: 765 pr_err("alloc_workqueue failed for port %d\n", pidx + 1); 766 for (pidx = 0; pidx < dd->num_pports; ++pidx) { 767 ppd = dd->pport + pidx; 768 if (ppd->hfi1_wq) { 769 destroy_workqueue(ppd->hfi1_wq); 770 ppd->hfi1_wq = NULL; 771 } 772 if (ppd->link_wq) { 773 destroy_workqueue(ppd->link_wq); 774 ppd->link_wq = NULL; 775 } 776 } 777 return -ENOMEM; 778 } 779 780 /** 781 * destroy_workqueues - destroy per port workqueues 782 * @dd: the hfi1_ib device 783 */ 784 static void destroy_workqueues(struct hfi1_devdata *dd) 785 { 786 int pidx; 787 struct hfi1_pportdata *ppd; 788 789 for (pidx = 0; pidx < dd->num_pports; ++pidx) { 790 ppd = dd->pport + pidx; 791 792 if (ppd->hfi1_wq) { 793 destroy_workqueue(ppd->hfi1_wq); 794 ppd->hfi1_wq = NULL; 795 } 796 if (ppd->link_wq) { 797 destroy_workqueue(ppd->link_wq); 798 ppd->link_wq = NULL; 799 } 800 } 801 } 802 803 /** 804 * enable_general_intr() - Enable the IRQs that will be handled by the 805 * general interrupt handler. 806 * @dd: valid devdata 807 * 808 */ 809 static void enable_general_intr(struct hfi1_devdata *dd) 810 { 811 set_intr_bits(dd, CCE_ERR_INT, MISC_ERR_INT, true); 812 set_intr_bits(dd, PIO_ERR_INT, TXE_ERR_INT, true); 813 set_intr_bits(dd, IS_SENDCTXT_ERR_START, IS_SENDCTXT_ERR_END, true); 814 set_intr_bits(dd, PBC_INT, GPIO_ASSERT_INT, true); 815 set_intr_bits(dd, TCRIT_INT, TCRIT_INT, true); 816 set_intr_bits(dd, IS_DC_START, IS_DC_END, true); 817 set_intr_bits(dd, IS_SENDCREDIT_START, IS_SENDCREDIT_END, true); 818 } 819 820 /** 821 * hfi1_init - do the actual initialization sequence on the chip 822 * @dd: the hfi1_ib device 823 * @reinit: re-initializing, so don't allocate new memory 824 * 825 * Do the actual initialization sequence on the chip. This is done 826 * both from the init routine called from the PCI infrastructure, and 827 * when we reset the chip, or detect that it was reset internally, 828 * or it's administratively re-enabled. 829 * 830 * Memory allocation here and in called routines is only done in 831 * the first case (reinit == 0). We have to be careful, because even 832 * without memory allocation, we need to re-write all the chip registers 833 * TIDs, etc. after the reset or enable has completed. 834 */ 835 int hfi1_init(struct hfi1_devdata *dd, int reinit) 836 { 837 int ret = 0, pidx, lastfail = 0; 838 unsigned long len; 839 u16 i; 840 struct hfi1_ctxtdata *rcd; 841 struct hfi1_pportdata *ppd; 842 843 /* Set up send low level handlers */ 844 dd->process_pio_send = hfi1_verbs_send_pio; 845 dd->process_dma_send = hfi1_verbs_send_dma; 846 dd->pio_inline_send = pio_copy; 847 848 if (is_ax(dd)) { 849 atomic_set(&dd->drop_packet, DROP_PACKET_ON); 850 dd->do_drop = true; 851 } else { 852 atomic_set(&dd->drop_packet, DROP_PACKET_OFF); 853 dd->do_drop = false; 854 } 855 856 /* make sure the link is not "up" */ 857 for (pidx = 0; pidx < dd->num_pports; ++pidx) { 858 ppd = dd->pport + pidx; 859 ppd->linkup = 0; 860 } 861 862 if (reinit) 863 ret = init_after_reset(dd); 864 else 865 ret = loadtime_init(dd); 866 if (ret) 867 goto done; 868 869 /* dd->rcd can be NULL if early initialization failed */ 870 for (i = 0; dd->rcd && i < dd->first_dyn_alloc_ctxt; ++i) { 871 /* 872 * Set up the (kernel) rcvhdr queue and egr TIDs. If doing 873 * re-init, the simplest way to handle this is to free 874 * existing, and re-allocate. 875 * Need to re-create rest of ctxt 0 ctxtdata as well. 876 */ 877 rcd = hfi1_rcd_get_by_index(dd, i); 878 if (!rcd) 879 continue; 880 881 lastfail = hfi1_create_rcvhdrq(dd, rcd); 882 if (!lastfail) 883 lastfail = hfi1_setup_eagerbufs(rcd); 884 if (!lastfail) 885 lastfail = hfi1_kern_exp_rcv_init(rcd, reinit); 886 if (lastfail) { 887 dd_dev_err(dd, 888 "failed to allocate kernel ctxt's rcvhdrq and/or egr bufs\n"); 889 ret = lastfail; 890 } 891 /* enable IRQ */ 892 hfi1_rcd_put(rcd); 893 } 894 895 /* Allocate enough memory for user event notification. */ 896 len = PAGE_ALIGN(chip_rcv_contexts(dd) * HFI1_MAX_SHARED_CTXTS * 897 sizeof(*dd->events)); 898 dd->events = vmalloc_user(len); 899 if (!dd->events) 900 dd_dev_err(dd, "Failed to allocate user events page\n"); 901 /* 902 * Allocate a page for device and port status. 903 * Page will be shared amongst all user processes. 904 */ 905 dd->status = vmalloc_user(PAGE_SIZE); 906 if (!dd->status) 907 dd_dev_err(dd, "Failed to allocate dev status page\n"); 908 for (pidx = 0; pidx < dd->num_pports; ++pidx) { 909 ppd = dd->pport + pidx; 910 if (dd->status) 911 /* Currently, we only have one port */ 912 ppd->statusp = &dd->status->port; 913 914 set_mtu(ppd); 915 } 916 917 /* enable chip even if we have an error, so we can debug cause */ 918 enable_chip(dd); 919 920 done: 921 /* 922 * Set status even if port serdes is not initialized 923 * so that diags will work. 924 */ 925 if (dd->status) 926 dd->status->dev |= HFI1_STATUS_CHIP_PRESENT | 927 HFI1_STATUS_INITTED; 928 if (!ret) { 929 /* enable all interrupts from the chip */ 930 enable_general_intr(dd); 931 init_qsfp_int(dd); 932 933 /* chip is OK for user apps; mark it as initialized */ 934 for (pidx = 0; pidx < dd->num_pports; ++pidx) { 935 ppd = dd->pport + pidx; 936 937 /* 938 * start the serdes - must be after interrupts are 939 * enabled so we are notified when the link goes up 940 */ 941 lastfail = bringup_serdes(ppd); 942 if (lastfail) 943 dd_dev_info(dd, 944 "Failed to bring up port %u\n", 945 ppd->port); 946 947 /* 948 * Set status even if port serdes is not initialized 949 * so that diags will work. 950 */ 951 if (ppd->statusp) 952 *ppd->statusp |= HFI1_STATUS_CHIP_PRESENT | 953 HFI1_STATUS_INITTED; 954 if (!ppd->link_speed_enabled) 955 continue; 956 } 957 } 958 959 /* if ret is non-zero, we probably should do some cleanup here... */ 960 return ret; 961 } 962 963 struct hfi1_devdata *hfi1_lookup(int unit) 964 { 965 return xa_load(&hfi1_dev_table, unit); 966 } 967 968 /* 969 * Stop the timers during unit shutdown, or after an error late 970 * in initialization. 971 */ 972 static void stop_timers(struct hfi1_devdata *dd) 973 { 974 struct hfi1_pportdata *ppd; 975 int pidx; 976 977 for (pidx = 0; pidx < dd->num_pports; ++pidx) { 978 ppd = dd->pport + pidx; 979 if (ppd->led_override_timer.function) { 980 timer_delete_sync(&ppd->led_override_timer); 981 atomic_set(&ppd->led_override_timer_active, 0); 982 } 983 } 984 } 985 986 /** 987 * shutdown_device - shut down a device 988 * @dd: the hfi1_ib device 989 * 990 * This is called to make the device quiet when we are about to 991 * unload the driver, and also when the device is administratively 992 * disabled. It does not free any data structures. 993 * Everything it does has to be setup again by hfi1_init(dd, 1) 994 */ 995 static void shutdown_device(struct hfi1_devdata *dd) 996 { 997 struct hfi1_pportdata *ppd; 998 struct hfi1_ctxtdata *rcd; 999 unsigned pidx; 1000 int i; 1001 1002 if (dd->flags & HFI1_SHUTDOWN) 1003 return; 1004 dd->flags |= HFI1_SHUTDOWN; 1005 1006 for (pidx = 0; pidx < dd->num_pports; ++pidx) { 1007 ppd = dd->pport + pidx; 1008 1009 ppd->linkup = 0; 1010 if (ppd->statusp) 1011 *ppd->statusp &= ~(HFI1_STATUS_IB_CONF | 1012 HFI1_STATUS_IB_READY); 1013 } 1014 dd->flags &= ~HFI1_INITTED; 1015 1016 /* mask and clean up interrupts */ 1017 set_intr_bits(dd, IS_FIRST_SOURCE, IS_LAST_SOURCE, false); 1018 msix_clean_up_interrupts(dd); 1019 1020 for (pidx = 0; pidx < dd->num_pports; ++pidx) { 1021 for (i = 0; i < dd->num_rcv_contexts; i++) { 1022 rcd = hfi1_rcd_get_by_index(dd, i); 1023 hfi1_rcvctrl(dd, HFI1_RCVCTRL_TAILUPD_DIS | 1024 HFI1_RCVCTRL_CTXT_DIS | 1025 HFI1_RCVCTRL_INTRAVAIL_DIS | 1026 HFI1_RCVCTRL_PKEY_DIS | 1027 HFI1_RCVCTRL_ONE_PKT_EGR_DIS, rcd); 1028 hfi1_rcd_put(rcd); 1029 } 1030 /* 1031 * Gracefully stop all sends allowing any in progress to 1032 * trickle out first. 1033 */ 1034 for (i = 0; i < dd->num_send_contexts; i++) 1035 sc_flush(dd->send_contexts[i].sc); 1036 } 1037 1038 /* 1039 * Enough for anything that's going to trickle out to have actually 1040 * done so. 1041 */ 1042 udelay(20); 1043 1044 for (pidx = 0; pidx < dd->num_pports; ++pidx) { 1045 ppd = dd->pport + pidx; 1046 1047 /* disable all contexts */ 1048 for (i = 0; i < dd->num_send_contexts; i++) 1049 sc_disable(dd->send_contexts[i].sc); 1050 /* disable the send device */ 1051 pio_send_control(dd, PSC_GLOBAL_DISABLE); 1052 1053 shutdown_led_override(ppd); 1054 1055 /* 1056 * Clear SerdesEnable. 1057 * We can't count on interrupts since we are stopping. 1058 */ 1059 hfi1_quiet_serdes(ppd); 1060 if (ppd->hfi1_wq) 1061 flush_workqueue(ppd->hfi1_wq); 1062 if (ppd->link_wq) 1063 flush_workqueue(ppd->link_wq); 1064 } 1065 sdma_exit(dd); 1066 } 1067 1068 /** 1069 * hfi1_free_ctxtdata - free a context's allocated data 1070 * @dd: the hfi1_ib device 1071 * @rcd: the ctxtdata structure 1072 * 1073 * free up any allocated data for a context 1074 * It should never change any chip state, or global driver state. 1075 */ 1076 void hfi1_free_ctxtdata(struct hfi1_devdata *dd, struct hfi1_ctxtdata *rcd) 1077 { 1078 u32 e; 1079 1080 if (!rcd) 1081 return; 1082 1083 if (rcd->rcvhdrq) { 1084 dma_free_coherent(&dd->pcidev->dev, rcvhdrq_size(rcd), 1085 rcd->rcvhdrq, rcd->rcvhdrq_dma); 1086 rcd->rcvhdrq = NULL; 1087 if (hfi1_rcvhdrtail_kvaddr(rcd)) { 1088 dma_free_coherent(&dd->pcidev->dev, PAGE_SIZE, 1089 (void *)hfi1_rcvhdrtail_kvaddr(rcd), 1090 rcd->rcvhdrqtailaddr_dma); 1091 rcd->rcvhdrtail_kvaddr = NULL; 1092 } 1093 } 1094 1095 /* all the RcvArray entries should have been cleared by now */ 1096 kfree(rcd->egrbufs.rcvtids); 1097 rcd->egrbufs.rcvtids = NULL; 1098 1099 for (e = 0; e < rcd->egrbufs.alloced; e++) { 1100 if (rcd->egrbufs.buffers[e].addr) 1101 dma_free_coherent(&dd->pcidev->dev, 1102 rcd->egrbufs.buffers[e].len, 1103 rcd->egrbufs.buffers[e].addr, 1104 rcd->egrbufs.buffers[e].dma); 1105 } 1106 kfree(rcd->egrbufs.buffers); 1107 rcd->egrbufs.alloced = 0; 1108 rcd->egrbufs.buffers = NULL; 1109 1110 sc_free(rcd->sc); 1111 rcd->sc = NULL; 1112 1113 vfree(rcd->subctxt_uregbase); 1114 vfree(rcd->subctxt_rcvegrbuf); 1115 vfree(rcd->subctxt_rcvhdr_base); 1116 kfree(rcd->opstats); 1117 1118 rcd->subctxt_uregbase = NULL; 1119 rcd->subctxt_rcvegrbuf = NULL; 1120 rcd->subctxt_rcvhdr_base = NULL; 1121 rcd->opstats = NULL; 1122 } 1123 1124 /* 1125 * Release our hold on the shared asic data. If we are the last one, 1126 * return the structure to be finalized outside the lock. Must be 1127 * holding hfi1_dev_table lock. 1128 */ 1129 static struct hfi1_asic_data *release_asic_data(struct hfi1_devdata *dd) 1130 { 1131 struct hfi1_asic_data *ad; 1132 int other; 1133 1134 if (!dd->asic_data) 1135 return NULL; 1136 dd->asic_data->dds[dd->hfi1_id] = NULL; 1137 other = dd->hfi1_id ? 0 : 1; 1138 ad = dd->asic_data; 1139 dd->asic_data = NULL; 1140 /* return NULL if the other dd still has a link */ 1141 return ad->dds[other] ? NULL : ad; 1142 } 1143 1144 static void finalize_asic_data(struct hfi1_devdata *dd, 1145 struct hfi1_asic_data *ad) 1146 { 1147 clean_up_i2c(dd, ad); 1148 kfree(ad); 1149 } 1150 1151 /** 1152 * hfi1_free_devdata - cleans up and frees per-unit data structure 1153 * @dd: pointer to a valid devdata structure 1154 * 1155 * It cleans up and frees all data structures set up by 1156 * by hfi1_alloc_devdata(). 1157 */ 1158 static void hfi1_free_devdata(struct hfi1_devdata *dd) 1159 { 1160 struct hfi1_asic_data *ad; 1161 unsigned long flags; 1162 1163 xa_lock_irqsave(&hfi1_dev_table, flags); 1164 __xa_erase(&hfi1_dev_table, dd->unit); 1165 ad = release_asic_data(dd); 1166 xa_unlock_irqrestore(&hfi1_dev_table, flags); 1167 1168 finalize_asic_data(dd, ad); 1169 free_platform_config(dd); 1170 rcu_barrier(); /* wait for rcu callbacks to complete */ 1171 free_percpu(dd->int_counter); 1172 free_percpu(dd->rcv_limit); 1173 free_percpu(dd->send_schedule); 1174 free_percpu(dd->tx_opstats); 1175 dd->int_counter = NULL; 1176 dd->rcv_limit = NULL; 1177 dd->send_schedule = NULL; 1178 dd->tx_opstats = NULL; 1179 kfree(dd->comp_vect); 1180 dd->comp_vect = NULL; 1181 if (dd->rcvhdrtail_dummy_kvaddr) 1182 dma_free_coherent(&dd->pcidev->dev, sizeof(u64), 1183 (void *)dd->rcvhdrtail_dummy_kvaddr, 1184 dd->rcvhdrtail_dummy_dma); 1185 dd->rcvhdrtail_dummy_kvaddr = NULL; 1186 sdma_clean(dd, dd->num_sdma); 1187 rvt_dealloc_device(&dd->verbs_dev.rdi); 1188 } 1189 1190 /** 1191 * hfi1_alloc_devdata - Allocate our primary per-unit data structure. 1192 * @pdev: Valid PCI device 1193 * @extra: How many bytes to alloc past the default 1194 * 1195 * Must be done via verbs allocator, because the verbs cleanup process 1196 * both does cleanup and free of the data structure. 1197 * "extra" is for chip-specific data. 1198 */ 1199 static struct hfi1_devdata *hfi1_alloc_devdata(struct pci_dev *pdev, 1200 size_t extra) 1201 { 1202 struct hfi1_devdata *dd; 1203 struct ib_device *ibdev; 1204 int ret, nports; 1205 1206 /* extra is * number of ports */ 1207 nports = extra / sizeof(struct hfi1_pportdata); 1208 1209 dd = (struct hfi1_devdata *)rvt_alloc_device(sizeof(*dd) + extra, 1210 nports); 1211 if (!dd) 1212 return ERR_PTR(-ENOMEM); 1213 dd->num_pports = nports; 1214 dd->pport = (struct hfi1_pportdata *)(dd + 1); 1215 dd->pcidev = pdev; 1216 pci_set_drvdata(pdev, dd); 1217 1218 ret = xa_alloc_irq(&hfi1_dev_table, &dd->unit, dd, xa_limit_32b, 1219 GFP_KERNEL); 1220 if (ret < 0) { 1221 dev_err(&pdev->dev, 1222 "Could not allocate unit ID: error %pe\n", ERR_PTR(ret)); 1223 rvt_dealloc_device(&dd->verbs_dev.rdi); 1224 return ERR_PTR(ret); 1225 } 1226 1227 /* 1228 * FIXME: rvt and its users want to touch the ibdev before 1229 * registration and have things like the name work. We don't have the 1230 * infrastructure in the core to support this directly today, hack it 1231 * to work by setting the name manually here. 1232 */ 1233 ibdev = &dd->verbs_dev.rdi.ibdev; 1234 dev_set_name(&ibdev->dev, "%s_%d", class_name(), dd->unit); 1235 strscpy(ibdev->name, dev_name(&ibdev->dev), IB_DEVICE_NAME_MAX); 1236 1237 /* 1238 * If the BIOS does not have the NUMA node information set, select 1239 * NUMA 0 so we get consistent performance. 1240 */ 1241 dd->node = pcibus_to_node(pdev->bus); 1242 if (dd->node == NUMA_NO_NODE) { 1243 dd_dev_err(dd, "Invalid PCI NUMA node. Performance may be affected\n"); 1244 dd->node = 0; 1245 } 1246 1247 /* 1248 * Initialize all locks for the device. This needs to be as early as 1249 * possible so locks are usable. 1250 */ 1251 spin_lock_init(&dd->sc_lock); 1252 spin_lock_init(&dd->sendctrl_lock); 1253 spin_lock_init(&dd->rcvctrl_lock); 1254 spin_lock_init(&dd->uctxt_lock); 1255 spin_lock_init(&dd->hfi1_diag_trans_lock); 1256 spin_lock_init(&dd->sc_init_lock); 1257 spin_lock_init(&dd->dc8051_memlock); 1258 seqlock_init(&dd->sc2vl_lock); 1259 spin_lock_init(&dd->sde_map_lock); 1260 spin_lock_init(&dd->pio_map_lock); 1261 mutex_init(&dd->dc8051_lock); 1262 init_waitqueue_head(&dd->event_queue); 1263 spin_lock_init(&dd->irq_src_lock); 1264 1265 dd->int_counter = alloc_percpu(u64); 1266 if (!dd->int_counter) { 1267 ret = -ENOMEM; 1268 goto bail; 1269 } 1270 1271 dd->rcv_limit = alloc_percpu(u64); 1272 if (!dd->rcv_limit) { 1273 ret = -ENOMEM; 1274 goto bail; 1275 } 1276 1277 dd->send_schedule = alloc_percpu(u64); 1278 if (!dd->send_schedule) { 1279 ret = -ENOMEM; 1280 goto bail; 1281 } 1282 1283 dd->tx_opstats = alloc_percpu(struct hfi1_opcode_stats_perctx); 1284 if (!dd->tx_opstats) { 1285 ret = -ENOMEM; 1286 goto bail; 1287 } 1288 1289 dd->comp_vect = kzalloc_obj(*dd->comp_vect); 1290 if (!dd->comp_vect) { 1291 ret = -ENOMEM; 1292 goto bail; 1293 } 1294 1295 /* allocate dummy tail memory for all receive contexts */ 1296 dd->rcvhdrtail_dummy_kvaddr = 1297 dma_alloc_coherent(&dd->pcidev->dev, sizeof(u64), 1298 &dd->rcvhdrtail_dummy_dma, GFP_KERNEL); 1299 if (!dd->rcvhdrtail_dummy_kvaddr) { 1300 ret = -ENOMEM; 1301 goto bail; 1302 } 1303 1304 atomic_set(&dd->ipoib_rsm_usr_num, 0); 1305 return dd; 1306 1307 bail: 1308 hfi1_free_devdata(dd); 1309 return ERR_PTR(ret); 1310 } 1311 1312 /* 1313 * Called from freeze mode handlers, and from PCI error 1314 * reporting code. Should be paranoid about state of 1315 * system and data structures. 1316 */ 1317 void hfi1_disable_after_error(struct hfi1_devdata *dd) 1318 { 1319 if (dd->flags & HFI1_INITTED) { 1320 u32 pidx; 1321 1322 dd->flags &= ~HFI1_INITTED; 1323 if (dd->pport) 1324 for (pidx = 0; pidx < dd->num_pports; ++pidx) { 1325 struct hfi1_pportdata *ppd; 1326 1327 ppd = dd->pport + pidx; 1328 if (dd->flags & HFI1_PRESENT) 1329 set_link_state(ppd, HLS_DN_DISABLE); 1330 1331 if (ppd->statusp) 1332 *ppd->statusp &= ~HFI1_STATUS_IB_READY; 1333 } 1334 } 1335 1336 /* 1337 * Mark as having had an error for driver, and also 1338 * for /sys and status word mapped to user programs. 1339 * This marks unit as not usable, until reset. 1340 */ 1341 if (dd->status) 1342 dd->status->dev |= HFI1_STATUS_HWERROR; 1343 } 1344 1345 static void remove_one(struct pci_dev *); 1346 static int init_one(struct pci_dev *, const struct pci_device_id *); 1347 static void shutdown_one(struct pci_dev *); 1348 1349 #define DRIVER_LOAD_MSG "Cornelis " DRIVER_NAME " loaded: " 1350 #define PFX DRIVER_NAME ": " 1351 1352 const struct pci_device_id hfi1_pci_tbl[] = { 1353 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL0) }, 1354 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL1) }, 1355 { 0, } 1356 }; 1357 1358 MODULE_DEVICE_TABLE(pci, hfi1_pci_tbl); 1359 1360 static struct pci_driver hfi1_pci_driver = { 1361 .name = DRIVER_NAME, 1362 .probe = init_one, 1363 .remove = remove_one, 1364 .shutdown = shutdown_one, 1365 .id_table = hfi1_pci_tbl, 1366 .err_handler = &hfi1_pci_err_handler, 1367 }; 1368 1369 static void __init compute_krcvqs(void) 1370 { 1371 int i; 1372 1373 for (i = 0; i < krcvqsset; i++) 1374 n_krcvqs += krcvqs[i]; 1375 } 1376 1377 /* 1378 * Do all the generic driver unit- and chip-independent memory 1379 * allocation and initialization. 1380 */ 1381 static int __init hfi1_mod_init(void) 1382 { 1383 int ret; 1384 1385 ret = dev_init(); 1386 if (ret) 1387 goto bail; 1388 1389 ret = node_affinity_init(); 1390 if (ret) 1391 goto bail; 1392 1393 /* validate max MTU before any devices start */ 1394 if (!valid_opa_max_mtu(hfi1_max_mtu)) { 1395 pr_err("Invalid max_mtu 0x%x, using 0x%x instead\n", 1396 hfi1_max_mtu, HFI1_DEFAULT_MAX_MTU); 1397 hfi1_max_mtu = HFI1_DEFAULT_MAX_MTU; 1398 } 1399 /* valid CUs run from 1-128 in powers of 2 */ 1400 if (hfi1_cu > 128 || !is_power_of_2(hfi1_cu)) 1401 hfi1_cu = 1; 1402 /* valid credit return threshold is 0-100, variable is unsigned */ 1403 if (user_credit_return_threshold > 100) 1404 user_credit_return_threshold = 100; 1405 1406 compute_krcvqs(); 1407 /* 1408 * sanitize receive interrupt count, time must wait until after 1409 * the hardware type is known 1410 */ 1411 if (rcv_intr_count > RCV_HDR_HEAD_COUNTER_MASK) 1412 rcv_intr_count = RCV_HDR_HEAD_COUNTER_MASK; 1413 /* reject invalid combinations */ 1414 if (rcv_intr_count == 0 && rcv_intr_timeout == 0) { 1415 pr_err("Invalid mode: both receive interrupt count and available timeout are zero - setting interrupt count to 1\n"); 1416 rcv_intr_count = 1; 1417 } 1418 if (rcv_intr_count > 1 && rcv_intr_timeout == 0) { 1419 /* 1420 * Avoid indefinite packet delivery by requiring a timeout 1421 * if count is > 1. 1422 */ 1423 pr_err("Invalid mode: receive interrupt count greater than 1 and available timeout is zero - setting available timeout to 1\n"); 1424 rcv_intr_timeout = 1; 1425 } 1426 if (rcv_intr_dynamic && !(rcv_intr_count > 1 && rcv_intr_timeout > 0)) { 1427 /* 1428 * The dynamic algorithm expects a non-zero timeout 1429 * and a count > 1. 1430 */ 1431 pr_err("Invalid mode: dynamic receive interrupt mitigation with invalid count and timeout - turning dynamic off\n"); 1432 rcv_intr_dynamic = 0; 1433 } 1434 1435 /* sanitize link CRC options */ 1436 link_crc_mask &= SUPPORTED_CRCS; 1437 1438 ret = opfn_init(); 1439 if (ret < 0) { 1440 pr_err("Failed to allocate opfn_wq"); 1441 goto bail_dev; 1442 } 1443 1444 /* 1445 * These must be called before the driver is registered with 1446 * the PCI subsystem. 1447 */ 1448 hfi1_dbg_init(); 1449 ret = pci_register_driver(&hfi1_pci_driver); 1450 if (ret < 0) { 1451 pr_err("Unable to register driver: error %d\n", -ret); 1452 goto bail_dev; 1453 } 1454 goto bail; /* all OK */ 1455 1456 bail_dev: 1457 hfi1_dbg_exit(); 1458 dev_cleanup(); 1459 bail: 1460 return ret; 1461 } 1462 1463 module_init(hfi1_mod_init); 1464 1465 /* 1466 * Do the non-unit driver cleanup, memory free, etc. at unload. 1467 */ 1468 static void __exit hfi1_mod_cleanup(void) 1469 { 1470 pci_unregister_driver(&hfi1_pci_driver); 1471 opfn_exit(); 1472 node_affinity_destroy_all(); 1473 hfi1_dbg_exit(); 1474 1475 WARN_ON(!xa_empty(&hfi1_dev_table)); 1476 dispose_firmware(); /* asymmetric with obtain_firmware() */ 1477 dev_cleanup(); 1478 } 1479 1480 module_exit(hfi1_mod_cleanup); 1481 1482 /* this can only be called after a successful initialization */ 1483 static void cleanup_device_data(struct hfi1_devdata *dd) 1484 { 1485 int ctxt; 1486 int pidx; 1487 1488 /* users can't do anything more with chip */ 1489 for (pidx = 0; pidx < dd->num_pports; ++pidx) { 1490 struct hfi1_pportdata *ppd = &dd->pport[pidx]; 1491 struct cc_state *cc_state; 1492 int i; 1493 1494 if (ppd->statusp) 1495 *ppd->statusp &= ~HFI1_STATUS_CHIP_PRESENT; 1496 1497 for (i = 0; i < OPA_MAX_SLS; i++) 1498 hrtimer_cancel(&ppd->cca_timer[i].hrtimer); 1499 1500 spin_lock(&ppd->cc_state_lock); 1501 cc_state = get_cc_state_protected(ppd); 1502 RCU_INIT_POINTER(ppd->cc_state, NULL); 1503 spin_unlock(&ppd->cc_state_lock); 1504 1505 if (cc_state) 1506 kfree_rcu(cc_state, rcu); 1507 } 1508 1509 free_credit_return(dd); 1510 1511 /* 1512 * Free any resources still in use (usually just kernel contexts) 1513 * at unload; we do for ctxtcnt, because that's what we allocate. 1514 */ 1515 for (ctxt = 0; dd->rcd && ctxt < dd->num_rcv_contexts; ctxt++) { 1516 struct hfi1_ctxtdata *rcd = dd->rcd[ctxt]; 1517 1518 if (rcd) { 1519 hfi1_free_ctxt_rcv_groups(rcd); 1520 hfi1_free_ctxt(rcd); 1521 } 1522 } 1523 1524 kfree(dd->rcd); 1525 dd->rcd = NULL; 1526 1527 free_pio_map(dd); 1528 /* must follow rcv context free - need to remove rcv's hooks */ 1529 for (ctxt = 0; ctxt < dd->num_send_contexts; ctxt++) 1530 sc_free(dd->send_contexts[ctxt].sc); 1531 dd->num_send_contexts = 0; 1532 kfree(dd->send_contexts); 1533 dd->send_contexts = NULL; 1534 kfree(dd->hw_to_sw); 1535 dd->hw_to_sw = NULL; 1536 kfree(dd->boardname); 1537 vfree(dd->events); 1538 vfree(dd->status); 1539 } 1540 1541 /* 1542 * Clean up on unit shutdown, or error during unit load after 1543 * successful initialization. 1544 */ 1545 static void postinit_cleanup(struct hfi1_devdata *dd) 1546 { 1547 hfi1_start_cleanup(dd); 1548 hfi1_comp_vectors_clean_up(dd); 1549 hfi1_dev_affinity_clean_up(dd); 1550 1551 hfi1_pcie_ddcleanup(dd); 1552 1553 cleanup_device_data(dd); 1554 } 1555 1556 static int init_one(struct pci_dev *pdev, const struct pci_device_id *ent) 1557 { 1558 int ret; 1559 struct hfi1_devdata *dd; 1560 1561 /* First, lock the non-writable module parameters */ 1562 HFI1_CAP_LOCK(); 1563 1564 /* Validate some global module parameters */ 1565 ret = hfi1_validate_rcvhdrcnt(pdev, rcvhdrcnt); 1566 if (ret) 1567 return ret; 1568 1569 /* use the encoding function as a sanitization check */ 1570 if (!encode_rcv_header_entry_size(hfi1_hdrq_entsize)) { 1571 dev_err(&pdev->dev, "Invalid HdrQ Entry size %u\n", 1572 hfi1_hdrq_entsize); 1573 return -EINVAL; 1574 } 1575 1576 /* The receive eager buffer size must be set before the receive 1577 * contexts are created. 1578 * 1579 * Set the eager buffer size. Validate that it falls in a range 1580 * allowed by the hardware - all powers of 2 between the min and 1581 * max. The maximum valid MTU is within the eager buffer range 1582 * so we do not need to cap the max_mtu by an eager buffer size 1583 * setting. 1584 */ 1585 if (eager_buffer_size) { 1586 if (!is_power_of_2(eager_buffer_size)) 1587 eager_buffer_size = 1588 roundup_pow_of_two(eager_buffer_size); 1589 eager_buffer_size = 1590 clamp_val(eager_buffer_size, 1591 MIN_EAGER_BUFFER * 8, 1592 MAX_EAGER_BUFFER_TOTAL); 1593 pci_info(pdev, "Eager buffer size %u\n", eager_buffer_size); 1594 } else { 1595 dev_err(&pdev->dev, "Invalid Eager buffer size of 0\n"); 1596 return -EINVAL; 1597 } 1598 1599 /* restrict value of hfi1_rcvarr_split */ 1600 hfi1_rcvarr_split = clamp_val(hfi1_rcvarr_split, 0, 100); 1601 1602 ret = hfi1_pcie_init(pdev); 1603 if (ret) 1604 return ret; 1605 1606 /* Allocate the dd so we can get to work */ 1607 dd = hfi1_alloc_devdata(pdev, NUM_IB_PORTS * 1608 sizeof(struct hfi1_pportdata)); 1609 if (IS_ERR(dd)) { 1610 ret = PTR_ERR(dd); 1611 goto clean_pcie; 1612 } 1613 1614 ret = create_workqueues(dd); 1615 if (ret) 1616 goto free_devdata; 1617 1618 /* 1619 * Do device-specific initialization, function table setup, etc. 1620 */ 1621 ret = hfi1_init_dd(dd); 1622 if (ret) 1623 goto destroy_workqueues; /* error already printed */ 1624 1625 /* do the generic initialization */ 1626 ret = hfi1_init(dd, 0); 1627 if (ret) 1628 goto free_rx; 1629 1630 ret = hfi1_register_ib_device(dd); 1631 if (ret) 1632 goto free_rx; 1633 1634 /* 1635 * Now ready for use. this should be cleared whenever we 1636 * detect a reset, or initiate one. 1637 */ 1638 dd->flags |= HFI1_INITTED; 1639 1640 ret = hfi1_device_create(dd); 1641 if (ret) 1642 dd_dev_err(dd, "Failed to create /dev devices: %pe\n", 1643 ERR_PTR(ret)); 1644 1645 sdma_start(dd); 1646 hfi1_dbg_ibdev_init(&dd->verbs_dev); 1647 1648 return 0; 1649 1650 free_rx: 1651 hfi1_free_rx(dd); 1652 shutdown_device(dd); 1653 stop_timers(dd); 1654 postinit_cleanup(dd); 1655 1656 destroy_workqueues: 1657 destroy_workqueues(dd); 1658 free_devdata: 1659 hfi1_free_devdata(dd); 1660 clean_pcie: 1661 hfi1_pcie_cleanup(pdev); 1662 return ret; 1663 } 1664 1665 static void wait_for_clients(struct hfi1_devdata *dd) 1666 { 1667 /* 1668 * Remove the device init value and complete the device if there is 1669 * no clients or wait for active clients to finish. 1670 */ 1671 if (refcount_dec_and_test(&dd->user_refcount)) 1672 complete(&dd->user_comp); 1673 1674 wait_for_completion(&dd->user_comp); 1675 } 1676 1677 static void remove_one(struct pci_dev *pdev) 1678 { 1679 struct hfi1_devdata *dd = pci_get_drvdata(pdev); 1680 1681 /* close debugfs files before ib unregister */ 1682 hfi1_dbg_ibdev_exit(&dd->verbs_dev); 1683 1684 /* remove the /dev hfi1 interface */ 1685 hfi1_device_remove(dd); 1686 1687 /* wait for existing user space clients to finish */ 1688 wait_for_clients(dd); 1689 1690 /* unregister from IB core */ 1691 hfi1_unregister_ib_device(dd); 1692 1693 /* free netdev data */ 1694 hfi1_free_rx(dd); 1695 1696 /* 1697 * Disable the IB link, disable interrupts on the device, 1698 * clear dma engines, etc. 1699 */ 1700 shutdown_device(dd); 1701 stop_timers(dd); 1702 postinit_cleanup(dd); 1703 destroy_workqueues(dd); 1704 hfi1_free_devdata(dd); 1705 hfi1_pcie_cleanup(pdev); 1706 } 1707 1708 static void shutdown_one(struct pci_dev *pdev) 1709 { 1710 struct hfi1_devdata *dd = pci_get_drvdata(pdev); 1711 1712 shutdown_device(dd); 1713 } 1714 1715 /** 1716 * hfi1_create_rcvhdrq - create a receive header queue 1717 * @dd: the hfi1_ib device 1718 * @rcd: the context data 1719 * 1720 * This must be contiguous memory (from an i/o perspective), and must be 1721 * DMA'able (which means for some systems, it will go through an IOMMU, 1722 * or be forced into a low address range). 1723 */ 1724 int hfi1_create_rcvhdrq(struct hfi1_devdata *dd, struct hfi1_ctxtdata *rcd) 1725 { 1726 unsigned amt; 1727 1728 if (!rcd->rcvhdrq) { 1729 amt = rcvhdrq_size(rcd); 1730 1731 rcd->rcvhdrq = dma_alloc_coherent(&dd->pcidev->dev, amt, 1732 &rcd->rcvhdrq_dma, 1733 GFP_KERNEL); 1734 1735 if (!rcd->rcvhdrq) { 1736 dd_dev_err(dd, 1737 "attempt to allocate %d bytes for ctxt %u rcvhdrq failed\n", 1738 amt, rcd->ctxt); 1739 goto bail; 1740 } 1741 1742 if (HFI1_CAP_KGET_MASK(rcd->flags, DMA_RTAIL) || 1743 HFI1_CAP_UGET_MASK(rcd->flags, DMA_RTAIL)) { 1744 rcd->rcvhdrtail_kvaddr = dma_alloc_coherent(&dd->pcidev->dev, 1745 PAGE_SIZE, 1746 &rcd->rcvhdrqtailaddr_dma, 1747 GFP_KERNEL); 1748 if (!rcd->rcvhdrtail_kvaddr) 1749 goto bail_free; 1750 } 1751 } 1752 1753 set_hdrq_regs(rcd->dd, rcd->ctxt, rcd->rcvhdrqentsize, 1754 rcd->rcvhdrq_cnt); 1755 1756 return 0; 1757 1758 bail_free: 1759 dd_dev_err(dd, 1760 "attempt to allocate 1 page for ctxt %u rcvhdrqtailaddr failed\n", 1761 rcd->ctxt); 1762 dma_free_coherent(&dd->pcidev->dev, amt, rcd->rcvhdrq, 1763 rcd->rcvhdrq_dma); 1764 rcd->rcvhdrq = NULL; 1765 bail: 1766 return -ENOMEM; 1767 } 1768 1769 /** 1770 * hfi1_setup_eagerbufs - llocate eager buffers, both kernel and user 1771 * contexts. 1772 * @rcd: the context we are setting up. 1773 * 1774 * Allocate the eager TID buffers and program them into hip. 1775 * They are no longer completely contiguous, we do multiple allocation 1776 * calls. Otherwise we get the OOM code involved, by asking for too 1777 * much per call, with disastrous results on some kernels. 1778 */ 1779 int hfi1_setup_eagerbufs(struct hfi1_ctxtdata *rcd) 1780 { 1781 struct hfi1_devdata *dd = rcd->dd; 1782 u32 max_entries, egrtop, alloced_bytes = 0; 1783 u16 order, idx = 0; 1784 int ret = 0; 1785 u16 round_mtu = roundup_pow_of_two(hfi1_max_mtu); 1786 1787 /* 1788 * The minimum size of the eager buffers is a groups of MTU-sized 1789 * buffers. 1790 * The global eager_buffer_size parameter is checked against the 1791 * theoretical lower limit of the value. Here, we check against the 1792 * MTU. 1793 */ 1794 if (rcd->egrbufs.size < (round_mtu * dd->rcv_entries.group_size)) 1795 rcd->egrbufs.size = round_mtu * dd->rcv_entries.group_size; 1796 /* 1797 * If using one-pkt-per-egr-buffer, lower the eager buffer 1798 * size to the max MTU (page-aligned). 1799 */ 1800 if (!HFI1_CAP_KGET_MASK(rcd->flags, MULTI_PKT_EGR)) 1801 rcd->egrbufs.rcvtid_size = round_mtu; 1802 1803 /* 1804 * Eager buffers sizes of 1MB or less require smaller TID sizes 1805 * to satisfy the "multiple of 8 RcvArray entries" requirement. 1806 */ 1807 if (rcd->egrbufs.size <= (1 << 20)) 1808 rcd->egrbufs.rcvtid_size = max((unsigned long)round_mtu, 1809 rounddown_pow_of_two(rcd->egrbufs.size / 8)); 1810 1811 while (alloced_bytes < rcd->egrbufs.size && 1812 rcd->egrbufs.alloced < rcd->egrbufs.count) { 1813 rcd->egrbufs.buffers[idx].addr = 1814 dma_alloc_coherent(&dd->pcidev->dev, 1815 rcd->egrbufs.rcvtid_size, 1816 &rcd->egrbufs.buffers[idx].dma, 1817 GFP_KERNEL); 1818 if (rcd->egrbufs.buffers[idx].addr) { 1819 rcd->egrbufs.buffers[idx].len = 1820 rcd->egrbufs.rcvtid_size; 1821 rcd->egrbufs.rcvtids[rcd->egrbufs.alloced].addr = 1822 rcd->egrbufs.buffers[idx].addr; 1823 rcd->egrbufs.rcvtids[rcd->egrbufs.alloced].dma = 1824 rcd->egrbufs.buffers[idx].dma; 1825 rcd->egrbufs.alloced++; 1826 alloced_bytes += rcd->egrbufs.rcvtid_size; 1827 idx++; 1828 } else { 1829 u32 new_size, i, j; 1830 u64 offset = 0; 1831 1832 /* 1833 * Fail the eager buffer allocation if: 1834 * - we are already using the lowest acceptable size 1835 * - we are using one-pkt-per-egr-buffer (this implies 1836 * that we are accepting only one size) 1837 */ 1838 if (rcd->egrbufs.rcvtid_size == round_mtu || 1839 !HFI1_CAP_KGET_MASK(rcd->flags, MULTI_PKT_EGR)) { 1840 dd_dev_err(dd, "ctxt%u: Failed to allocate eager buffers\n", 1841 rcd->ctxt); 1842 ret = -ENOMEM; 1843 goto bail_rcvegrbuf_phys; 1844 } 1845 1846 new_size = rcd->egrbufs.rcvtid_size / 2; 1847 1848 /* 1849 * If the first attempt to allocate memory failed, don't 1850 * fail everything but continue with the next lower 1851 * size. 1852 */ 1853 if (idx == 0) { 1854 rcd->egrbufs.rcvtid_size = new_size; 1855 continue; 1856 } 1857 1858 /* 1859 * Re-partition already allocated buffers to a smaller 1860 * size. 1861 */ 1862 rcd->egrbufs.alloced = 0; 1863 for (i = 0, j = 0, offset = 0; j < idx; i++) { 1864 if (i >= rcd->egrbufs.count) 1865 break; 1866 rcd->egrbufs.rcvtids[i].dma = 1867 rcd->egrbufs.buffers[j].dma + offset; 1868 rcd->egrbufs.rcvtids[i].addr = 1869 rcd->egrbufs.buffers[j].addr + offset; 1870 rcd->egrbufs.alloced++; 1871 if ((rcd->egrbufs.buffers[j].dma + offset + 1872 new_size) == 1873 (rcd->egrbufs.buffers[j].dma + 1874 rcd->egrbufs.buffers[j].len)) { 1875 j++; 1876 offset = 0; 1877 } else { 1878 offset += new_size; 1879 } 1880 } 1881 rcd->egrbufs.rcvtid_size = new_size; 1882 } 1883 } 1884 rcd->egrbufs.numbufs = idx; 1885 rcd->egrbufs.size = alloced_bytes; 1886 1887 hfi1_cdbg(PROC, 1888 "ctxt%u: Alloced %u rcv tid entries @ %uKB, total %uKB", 1889 rcd->ctxt, rcd->egrbufs.alloced, 1890 rcd->egrbufs.rcvtid_size / 1024, rcd->egrbufs.size / 1024); 1891 1892 /* 1893 * Set the contexts rcv array head update threshold to the closest 1894 * power of 2 (so we can use a mask instead of modulo) below half 1895 * the allocated entries. 1896 */ 1897 rcd->egrbufs.threshold = 1898 rounddown_pow_of_two(rcd->egrbufs.alloced / 2); 1899 /* 1900 * Compute the expected RcvArray entry base. This is done after 1901 * allocating the eager buffers in order to maximize the 1902 * expected RcvArray entries for the context. 1903 */ 1904 max_entries = rcd->rcv_array_groups * dd->rcv_entries.group_size; 1905 egrtop = roundup(rcd->egrbufs.alloced, dd->rcv_entries.group_size); 1906 rcd->expected_count = max_entries - egrtop; 1907 if (rcd->expected_count > MAX_TID_PAIR_ENTRIES * 2) 1908 rcd->expected_count = MAX_TID_PAIR_ENTRIES * 2; 1909 1910 rcd->expected_base = rcd->eager_base + egrtop; 1911 hfi1_cdbg(PROC, "ctxt%u: eager:%u, exp:%u, egrbase:%u, expbase:%u", 1912 rcd->ctxt, rcd->egrbufs.alloced, rcd->expected_count, 1913 rcd->eager_base, rcd->expected_base); 1914 1915 if (!hfi1_rcvbuf_validate(rcd->egrbufs.rcvtid_size, PT_EAGER, &order)) { 1916 hfi1_cdbg(PROC, 1917 "ctxt%u: current Eager buffer size is invalid %u", 1918 rcd->ctxt, rcd->egrbufs.rcvtid_size); 1919 ret = -EINVAL; 1920 goto bail_rcvegrbuf_phys; 1921 } 1922 1923 for (idx = 0; idx < rcd->egrbufs.alloced; idx++) { 1924 hfi1_put_tid(dd, rcd->eager_base + idx, PT_EAGER, 1925 rcd->egrbufs.rcvtids[idx].dma, order); 1926 cond_resched(); 1927 } 1928 1929 return 0; 1930 1931 bail_rcvegrbuf_phys: 1932 for (idx = 0; idx < rcd->egrbufs.alloced && 1933 rcd->egrbufs.buffers[idx].addr; 1934 idx++) { 1935 dma_free_coherent(&dd->pcidev->dev, 1936 rcd->egrbufs.buffers[idx].len, 1937 rcd->egrbufs.buffers[idx].addr, 1938 rcd->egrbufs.buffers[idx].dma); 1939 rcd->egrbufs.buffers[idx].addr = NULL; 1940 rcd->egrbufs.buffers[idx].dma = 0; 1941 rcd->egrbufs.buffers[idx].len = 0; 1942 } 1943 1944 return ret; 1945 } 1946