blob: 7def3f33ac87b6b0d1d40458735a88032979dfeb [file] [log] [blame]
Mike Marciniszyn77241052015-07-30 15:17:43 -04001/*
2 *
3 * This file is provided under a dual BSD/GPLv2 license. When using or
4 * redistributing this file, you may do so under either license.
5 *
6 * GPL LICENSE SUMMARY
7 *
8 * Copyright(c) 2015 Intel Corporation.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * BSD LICENSE
20 *
21 * Copyright(c) 2015 Intel Corporation.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 *
27 * - Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
29 * - Redistributions in binary form must reproduce the above copyright
30 * notice, this list of conditions and the following disclaimer in
31 * the documentation and/or other materials provided with the
32 * distribution.
33 * - Neither the name of Intel Corporation nor the names of its
34 * contributors may be used to endorse or promote products derived
35 * from this software without specific prior written permission.
36 *
37 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
38 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
39 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
40 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
41 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
44 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
45 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
46 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
47 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48 *
49 */
50
51#include <linux/pci.h>
52#include <linux/netdevice.h>
53#include <linux/vmalloc.h>
54#include <linux/delay.h>
55#include <linux/idr.h>
56#include <linux/module.h>
57#include <linux/printk.h>
58#include <linux/hrtimer.h>
Dennis Dalessandroec3f2c12016-01-19 14:41:33 -080059#include <rdma/rdma_vt.h>
Mike Marciniszyn77241052015-07-30 15:17:43 -040060
61#include "hfi.h"
62#include "device.h"
63#include "common.h"
Sebastian Sanchez6c63e422015-11-06 20:06:56 -050064#include "trace.h"
Mike Marciniszyn77241052015-07-30 15:17:43 -040065#include "mad.h"
66#include "sdma.h"
67#include "debugfs.h"
68#include "verbs.h"
69
70#undef pr_fmt
71#define pr_fmt(fmt) DRIVER_NAME ": " fmt
72
73/*
74 * min buffers we want to have per context, after driver
75 */
76#define HFI1_MIN_USER_CTXT_BUFCNT 7
77
78#define HFI1_MIN_HDRQ_EGRBUF_CNT 2
79#define HFI1_MIN_EAGER_BUFFER_SIZE (4 * 1024) /* 4KB */
80#define HFI1_MAX_EAGER_BUFFER_SIZE (256 * 1024) /* 256KB */
81
82/*
83 * Number of user receive contexts we are configured to use (to allow for more
84 * pio buffers per ctxt, etc.) Zero means use one user context per CPU.
85 */
Sebastian Sanchez2ce6bf22015-12-11 08:44:48 -050086int num_user_contexts = -1;
87module_param_named(num_user_contexts, num_user_contexts, uint, S_IRUGO);
Mike Marciniszyn77241052015-07-30 15:17:43 -040088MODULE_PARM_DESC(
Sebastian Sanchez2ce6bf22015-12-11 08:44:48 -050089 num_user_contexts, "Set max number of user contexts to use");
Mike Marciniszyn77241052015-07-30 15:17:43 -040090
Mark F. Brown5b55ea32016-01-11 18:30:54 -050091uint krcvqs[RXE_NUM_DATA_VL];
Mike Marciniszyn77241052015-07-30 15:17:43 -040092int krcvqsset;
Mark F. Brown5b55ea32016-01-11 18:30:54 -050093module_param_array(krcvqs, uint, &krcvqsset, S_IRUGO);
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -050094MODULE_PARM_DESC(krcvqs, "Array of the number of non-control kernel receive queues by VL");
Mike Marciniszyn77241052015-07-30 15:17:43 -040095
96/* computed based on above array */
97unsigned n_krcvqs;
98
99static unsigned hfi1_rcvarr_split = 25;
100module_param_named(rcvarr_split, hfi1_rcvarr_split, uint, S_IRUGO);
101MODULE_PARM_DESC(rcvarr_split, "Percent of context's RcvArray entries used for Eager buffers");
102
103static uint eager_buffer_size = (2 << 20); /* 2MB */
104module_param(eager_buffer_size, uint, S_IRUGO);
105MODULE_PARM_DESC(eager_buffer_size, "Size of the eager buffers, default: 2MB");
106
107static uint rcvhdrcnt = 2048; /* 2x the max eager buffer count */
108module_param_named(rcvhdrcnt, rcvhdrcnt, uint, S_IRUGO);
109MODULE_PARM_DESC(rcvhdrcnt, "Receive header queue count (default 2048)");
110
111static uint hfi1_hdrq_entsize = 32;
112module_param_named(hdrq_entsize, hfi1_hdrq_entsize, uint, S_IRUGO);
113MODULE_PARM_DESC(hdrq_entsize, "Size of header queue entries: 2 - 8B, 16 - 64B (default), 32 - 128B");
114
115unsigned int user_credit_return_threshold = 33; /* default is 33% */
116module_param(user_credit_return_threshold, uint, S_IRUGO);
Jubin Johnecb95a02015-12-17 19:24:14 -0500117MODULE_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)");
Mike Marciniszyn77241052015-07-30 15:17:43 -0400118
119static inline u64 encode_rcv_header_entry_size(u16);
120
121static struct idr hfi1_unit_table;
122u32 hfi1_cpulist_count;
123unsigned long *hfi1_cpulist;
124
125/*
126 * Common code for creating the receive context array.
127 */
128int hfi1_create_ctxts(struct hfi1_devdata *dd)
129{
130 unsigned i;
131 int ret;
132 int local_node_id = pcibus_to_node(dd->pcidev->bus);
133
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500134 /* Control context has to be always 0 */
135 BUILD_BUG_ON(HFI1_CTRL_CTXT != 0);
136
Mike Marciniszyn77241052015-07-30 15:17:43 -0400137 if (local_node_id < 0)
138 local_node_id = numa_node_id();
139 dd->assigned_node_id = local_node_id;
140
141 dd->rcd = kcalloc(dd->num_rcv_contexts, sizeof(*dd->rcd), GFP_KERNEL);
Alison Schofield806e6e12015-10-12 14:28:36 -0700142 if (!dd->rcd)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400143 goto nomem;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400144
145 /* create one or more kernel contexts */
146 for (i = 0; i < dd->first_user_ctxt; ++i) {
147 struct hfi1_pportdata *ppd;
148 struct hfi1_ctxtdata *rcd;
149
150 ppd = dd->pport + (i % dd->num_pports);
151 rcd = hfi1_create_ctxtdata(ppd, i);
152 if (!rcd) {
153 dd_dev_err(dd,
154 "Unable to allocate kernel receive context, failing\n");
155 goto nomem;
156 }
157 /*
158 * Set up the kernel context flags here and now because they
159 * use default values for all receive side memories. User
160 * contexts will be handled as they are created.
161 */
162 rcd->flags = HFI1_CAP_KGET(MULTI_PKT_EGR) |
163 HFI1_CAP_KGET(NODROP_RHQ_FULL) |
164 HFI1_CAP_KGET(NODROP_EGR_FULL) |
165 HFI1_CAP_KGET(DMA_RTAIL);
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500166
167 /* Control context must use DMA_RTAIL */
168 if (rcd->ctxt == HFI1_CTRL_CTXT)
169 rcd->flags |= HFI1_CAP_DMA_RTAIL;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400170 rcd->seq_cnt = 1;
171
172 rcd->sc = sc_alloc(dd, SC_ACK, rcd->rcvhdrqentsize, dd->node);
173 if (!rcd->sc) {
174 dd_dev_err(dd,
175 "Unable to allocate kernel send context, failing\n");
176 dd->rcd[rcd->ctxt] = NULL;
177 hfi1_free_ctxtdata(dd, rcd);
178 goto nomem;
179 }
180
181 ret = hfi1_init_ctxt(rcd->sc);
182 if (ret < 0) {
183 dd_dev_err(dd,
184 "Failed to setup kernel receive context, failing\n");
185 sc_free(rcd->sc);
186 dd->rcd[rcd->ctxt] = NULL;
187 hfi1_free_ctxtdata(dd, rcd);
188 ret = -EFAULT;
189 goto bail;
190 }
191 }
192
193 return 0;
194nomem:
195 ret = -ENOMEM;
196bail:
197 kfree(dd->rcd);
198 dd->rcd = NULL;
199 return ret;
200}
201
202/*
203 * Common code for user and kernel context setup.
204 */
205struct hfi1_ctxtdata *hfi1_create_ctxtdata(struct hfi1_pportdata *ppd, u32 ctxt)
206{
207 struct hfi1_devdata *dd = ppd->dd;
208 struct hfi1_ctxtdata *rcd;
209 unsigned kctxt_ngroups = 0;
210 u32 base;
211
212 if (dd->rcv_entries.nctxt_extra >
213 dd->num_rcv_contexts - dd->first_user_ctxt)
214 kctxt_ngroups = (dd->rcv_entries.nctxt_extra -
215 (dd->num_rcv_contexts - dd->first_user_ctxt));
216 rcd = kzalloc(sizeof(*rcd), GFP_KERNEL);
217 if (rcd) {
218 u32 rcvtids, max_entries;
219
Sebastian Sanchez6c63e422015-11-06 20:06:56 -0500220 hfi1_cdbg(PROC, "setting up context %u\n", ctxt);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400221
222 INIT_LIST_HEAD(&rcd->qp_wait_list);
223 rcd->ppd = ppd;
224 rcd->dd = dd;
225 rcd->cnt = 1;
226 rcd->ctxt = ctxt;
227 dd->rcd[ctxt] = rcd;
228 rcd->numa_id = numa_node_id();
229 rcd->rcv_array_groups = dd->rcv_entries.ngroups;
230
Mitko Haralanov463e6eb2016-02-05 11:57:53 -0500231 mutex_init(&rcd->exp_lock);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400232
233 /*
234 * Calculate the context's RcvArray entry starting point.
235 * We do this here because we have to take into account all
236 * the RcvArray entries that previous context would have
237 * taken and we have to account for any extra groups
238 * assigned to the kernel or user contexts.
239 */
240 if (ctxt < dd->first_user_ctxt) {
241 if (ctxt < kctxt_ngroups) {
242 base = ctxt * (dd->rcv_entries.ngroups + 1);
243 rcd->rcv_array_groups++;
244 } else
245 base = kctxt_ngroups +
246 (ctxt * dd->rcv_entries.ngroups);
247 } else {
248 u16 ct = ctxt - dd->first_user_ctxt;
249
250 base = ((dd->n_krcv_queues * dd->rcv_entries.ngroups) +
251 kctxt_ngroups);
252 if (ct < dd->rcv_entries.nctxt_extra) {
253 base += ct * (dd->rcv_entries.ngroups + 1);
254 rcd->rcv_array_groups++;
255 } else
256 base += dd->rcv_entries.nctxt_extra +
257 (ct * dd->rcv_entries.ngroups);
258 }
259 rcd->eager_base = base * dd->rcv_entries.group_size;
260
261 /* Validate and initialize Rcv Hdr Q variables */
262 if (rcvhdrcnt % HDRQ_INCREMENT) {
263 dd_dev_err(dd,
jubin.john@intel.com349ac712016-01-11 18:30:52 -0500264 "ctxt%u: header queue count %d must be divisible by %lu\n",
Mike Marciniszyn77241052015-07-30 15:17:43 -0400265 rcd->ctxt, rcvhdrcnt, HDRQ_INCREMENT);
266 goto bail;
267 }
268 rcd->rcvhdrq_cnt = rcvhdrcnt;
269 rcd->rcvhdrqentsize = hfi1_hdrq_entsize;
270 /*
271 * Simple Eager buffer allocation: we have already pre-allocated
272 * the number of RcvArray entry groups. Each ctxtdata structure
273 * holds the number of groups for that context.
274 *
275 * To follow CSR requirements and maintain cacheline alignment,
276 * make sure all sizes and bases are multiples of group_size.
277 *
278 * The expected entry count is what is left after assigning
279 * eager.
280 */
281 max_entries = rcd->rcv_array_groups *
282 dd->rcv_entries.group_size;
283 rcvtids = ((max_entries * hfi1_rcvarr_split) / 100);
284 rcd->egrbufs.count = round_down(rcvtids,
285 dd->rcv_entries.group_size);
286 if (rcd->egrbufs.count > MAX_EAGER_ENTRIES) {
287 dd_dev_err(dd, "ctxt%u: requested too many RcvArray entries.\n",
288 rcd->ctxt);
289 rcd->egrbufs.count = MAX_EAGER_ENTRIES;
290 }
Sebastian Sanchez6c63e422015-11-06 20:06:56 -0500291 hfi1_cdbg(PROC,
292 "ctxt%u: max Eager buffer RcvArray entries: %u\n",
293 rcd->ctxt, rcd->egrbufs.count);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400294
295 /*
296 * Allocate array that will hold the eager buffer accounting
297 * data.
298 * This will allocate the maximum possible buffer count based
299 * on the value of the RcvArray split parameter.
300 * The resulting value will be rounded down to the closest
301 * multiple of dd->rcv_entries.group_size.
302 */
Shraddha Barke314fcc02015-10-09 21:03:26 +0530303 rcd->egrbufs.buffers = kcalloc(rcd->egrbufs.count,
304 sizeof(*rcd->egrbufs.buffers),
305 GFP_KERNEL);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400306 if (!rcd->egrbufs.buffers)
307 goto bail;
Shraddha Barke314fcc02015-10-09 21:03:26 +0530308 rcd->egrbufs.rcvtids = kcalloc(rcd->egrbufs.count,
309 sizeof(*rcd->egrbufs.rcvtids),
310 GFP_KERNEL);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400311 if (!rcd->egrbufs.rcvtids)
312 goto bail;
313 rcd->egrbufs.size = eager_buffer_size;
314 /*
315 * The size of the buffers programmed into the RcvArray
316 * entries needs to be big enough to handle the highest
317 * MTU supported.
318 */
319 if (rcd->egrbufs.size < hfi1_max_mtu) {
320 rcd->egrbufs.size = __roundup_pow_of_two(hfi1_max_mtu);
Sebastian Sanchez6c63e422015-11-06 20:06:56 -0500321 hfi1_cdbg(PROC,
322 "ctxt%u: eager bufs size too small. Adjusting to %zu\n",
Mike Marciniszyn77241052015-07-30 15:17:43 -0400323 rcd->ctxt, rcd->egrbufs.size);
324 }
325 rcd->egrbufs.rcvtid_size = HFI1_MAX_EAGER_BUFFER_SIZE;
326
327 if (ctxt < dd->first_user_ctxt) { /* N/A for PSM contexts */
328 rcd->opstats = kzalloc(sizeof(*rcd->opstats),
329 GFP_KERNEL);
Alison Schofield806e6e12015-10-12 14:28:36 -0700330 if (!rcd->opstats)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400331 goto bail;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400332 }
333 }
334 return rcd;
335bail:
336 kfree(rcd->opstats);
337 kfree(rcd->egrbufs.rcvtids);
338 kfree(rcd->egrbufs.buffers);
339 kfree(rcd);
340 return NULL;
341}
342
343/*
344 * Convert a receive header entry size that to the encoding used in the CSR.
345 *
346 * Return a zero if the given size is invalid.
347 */
348static inline u64 encode_rcv_header_entry_size(u16 size)
349{
350 /* there are only 3 valid receive header entry sizes */
351 if (size == 2)
352 return 1;
353 if (size == 16)
354 return 2;
355 else if (size == 32)
356 return 4;
357 return 0; /* invalid */
358}
359
360/*
361 * Select the largest ccti value over all SLs to determine the intra-
362 * packet gap for the link.
363 *
364 * called with cca_timer_lock held (to protect access to cca_timer
365 * array), and rcu_read_lock() (to protect access to cc_state).
366 */
367void set_link_ipg(struct hfi1_pportdata *ppd)
368{
369 struct hfi1_devdata *dd = ppd->dd;
370 struct cc_state *cc_state;
371 int i;
372 u16 cce, ccti_limit, max_ccti = 0;
373 u16 shift, mult;
374 u64 src;
375 u32 current_egress_rate; /* Mbits /sec */
376 u32 max_pkt_time;
377 /*
378 * max_pkt_time is the maximum packet egress time in units
379 * of the fabric clock period 1/(805 MHz).
380 */
381
382 cc_state = get_cc_state(ppd);
383
384 if (cc_state == NULL)
385 /*
386 * This should _never_ happen - rcu_read_lock() is held,
387 * and set_link_ipg() should not be called if cc_state
388 * is NULL.
389 */
390 return;
391
392 for (i = 0; i < OPA_MAX_SLS; i++) {
393 u16 ccti = ppd->cca_timer[i].ccti;
394
395 if (ccti > max_ccti)
396 max_ccti = ccti;
397 }
398
399 ccti_limit = cc_state->cct.ccti_limit;
400 if (max_ccti > ccti_limit)
401 max_ccti = ccti_limit;
402
403 cce = cc_state->cct.entries[max_ccti].entry;
404 shift = (cce & 0xc000) >> 14;
405 mult = (cce & 0x3fff);
406
407 current_egress_rate = active_egress_rate(ppd);
408
409 max_pkt_time = egress_cycles(ppd->ibmaxlen, current_egress_rate);
410
411 src = (max_pkt_time >> shift) * mult;
412
413 src &= SEND_STATIC_RATE_CONTROL_CSR_SRC_RELOAD_SMASK;
414 src <<= SEND_STATIC_RATE_CONTROL_CSR_SRC_RELOAD_SHIFT;
415
416 write_csr(dd, SEND_STATIC_RATE_CONTROL, src);
417}
418
419static enum hrtimer_restart cca_timer_fn(struct hrtimer *t)
420{
421 struct cca_timer *cca_timer;
422 struct hfi1_pportdata *ppd;
423 int sl;
424 u16 ccti, ccti_timer, ccti_min;
425 struct cc_state *cc_state;
Dean Luickb77d7132015-10-26 10:28:43 -0400426 unsigned long flags;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400427
428 cca_timer = container_of(t, struct cca_timer, hrtimer);
429 ppd = cca_timer->ppd;
430 sl = cca_timer->sl;
431
432 rcu_read_lock();
433
434 cc_state = get_cc_state(ppd);
435
436 if (cc_state == NULL) {
437 rcu_read_unlock();
438 return HRTIMER_NORESTART;
439 }
440
441 /*
442 * 1) decrement ccti for SL
443 * 2) calculate IPG for link (set_link_ipg())
444 * 3) restart timer, unless ccti is at min value
445 */
446
447 ccti_min = cc_state->cong_setting.entries[sl].ccti_min;
448 ccti_timer = cc_state->cong_setting.entries[sl].ccti_timer;
449
Dean Luickb77d7132015-10-26 10:28:43 -0400450 spin_lock_irqsave(&ppd->cca_timer_lock, flags);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400451
452 ccti = cca_timer->ccti;
453
454 if (ccti > ccti_min) {
455 cca_timer->ccti--;
456 set_link_ipg(ppd);
457 }
458
Dean Luickb77d7132015-10-26 10:28:43 -0400459 spin_unlock_irqrestore(&ppd->cca_timer_lock, flags);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400460
461 rcu_read_unlock();
462
463 if (ccti > ccti_min) {
464 unsigned long nsec = 1024 * ccti_timer;
465 /* ccti_timer is in units of 1.024 usec */
466 hrtimer_forward_now(t, ns_to_ktime(nsec));
467 return HRTIMER_RESTART;
468 }
469 return HRTIMER_NORESTART;
470}
471
472/*
473 * Common code for initializing the physical port structure.
474 */
475void hfi1_init_pportdata(struct pci_dev *pdev, struct hfi1_pportdata *ppd,
476 struct hfi1_devdata *dd, u8 hw_pidx, u8 port)
477{
478 int i, size;
479 uint default_pkey_idx;
480
481 ppd->dd = dd;
482 ppd->hw_pidx = hw_pidx;
483 ppd->port = port; /* IB port number, not index */
484
485 default_pkey_idx = 1;
486
487 ppd->pkeys[default_pkey_idx] = DEFAULT_P_KEY;
488 if (loopback) {
489 hfi1_early_err(&pdev->dev,
490 "Faking data partition 0x8001 in idx %u\n",
491 !default_pkey_idx);
492 ppd->pkeys[!default_pkey_idx] = 0x8001;
493 }
494
495 INIT_WORK(&ppd->link_vc_work, handle_verify_cap);
496 INIT_WORK(&ppd->link_up_work, handle_link_up);
497 INIT_WORK(&ppd->link_down_work, handle_link_down);
498 INIT_WORK(&ppd->freeze_work, handle_freeze);
499 INIT_WORK(&ppd->link_downgrade_work, handle_link_downgrade);
500 INIT_WORK(&ppd->sma_message_work, handle_sma_message);
501 INIT_WORK(&ppd->link_bounce_work, handle_link_bounce);
Jim Snowfb9036d2016-01-11 18:32:21 -0500502 INIT_WORK(&ppd->linkstate_active_work, receive_interrupt_work);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400503 mutex_init(&ppd->hls_lock);
504 spin_lock_init(&ppd->sdma_alllock);
505 spin_lock_init(&ppd->qsfp_info.qsfp_lock);
506
507 ppd->sm_trap_qp = 0x0;
508 ppd->sa_qp = 0x1;
509
510 ppd->hfi1_wq = NULL;
511
512 spin_lock_init(&ppd->cca_timer_lock);
513
514 for (i = 0; i < OPA_MAX_SLS; i++) {
515 hrtimer_init(&ppd->cca_timer[i].hrtimer, CLOCK_MONOTONIC,
516 HRTIMER_MODE_REL);
517 ppd->cca_timer[i].ppd = ppd;
518 ppd->cca_timer[i].sl = i;
519 ppd->cca_timer[i].ccti = 0;
520 ppd->cca_timer[i].hrtimer.function = cca_timer_fn;
521 }
522
523 ppd->cc_max_table_entries = IB_CC_TABLE_CAP_DEFAULT;
524
525 spin_lock_init(&ppd->cc_state_lock);
526 spin_lock_init(&ppd->cc_log_lock);
527 size = sizeof(struct cc_state);
528 RCU_INIT_POINTER(ppd->cc_state, kzalloc(size, GFP_KERNEL));
529 if (!rcu_dereference(ppd->cc_state))
530 goto bail;
531 return;
532
533bail:
534
535 hfi1_early_err(&pdev->dev,
536 "Congestion Control Agent disabled for port %d\n", port);
537}
538
539/*
540 * Do initialization for device that is only needed on
541 * first detect, not on resets.
542 */
543static int loadtime_init(struct hfi1_devdata *dd)
544{
545 return 0;
546}
547
548/**
549 * init_after_reset - re-initialize after a reset
550 * @dd: the hfi1_ib device
551 *
552 * sanity check at least some of the values after reset, and
553 * ensure no receive or transmit (explicitly, in case reset
554 * failed
555 */
556static int init_after_reset(struct hfi1_devdata *dd)
557{
558 int i;
559
560 /*
561 * Ensure chip does no sends or receives, tail updates, or
562 * pioavail updates while we re-initialize. This is mostly
563 * for the driver data structures, not chip registers.
564 */
565 for (i = 0; i < dd->num_rcv_contexts; i++)
566 hfi1_rcvctrl(dd, HFI1_RCVCTRL_CTXT_DIS |
567 HFI1_RCVCTRL_INTRAVAIL_DIS |
568 HFI1_RCVCTRL_TAILUPD_DIS, i);
569 pio_send_control(dd, PSC_GLOBAL_DISABLE);
570 for (i = 0; i < dd->num_send_contexts; i++)
571 sc_disable(dd->send_contexts[i].sc);
572
573 return 0;
574}
575
576static void enable_chip(struct hfi1_devdata *dd)
577{
578 u32 rcvmask;
579 u32 i;
580
581 /* enable PIO send */
582 pio_send_control(dd, PSC_GLOBAL_ENABLE);
583
584 /*
585 * Enable kernel ctxts' receive and receive interrupt.
586 * Other ctxts done as user opens and initializes them.
587 */
588 rcvmask = HFI1_RCVCTRL_CTXT_ENB | HFI1_RCVCTRL_INTRAVAIL_ENB;
589 for (i = 0; i < dd->first_user_ctxt; ++i) {
590 rcvmask |= HFI1_CAP_KGET_MASK(dd->rcd[i]->flags, DMA_RTAIL) ?
591 HFI1_RCVCTRL_TAILUPD_ENB : HFI1_RCVCTRL_TAILUPD_DIS;
592 if (!HFI1_CAP_KGET_MASK(dd->rcd[i]->flags, MULTI_PKT_EGR))
593 rcvmask |= HFI1_RCVCTRL_ONE_PKT_EGR_ENB;
594 if (HFI1_CAP_KGET_MASK(dd->rcd[i]->flags, NODROP_RHQ_FULL))
595 rcvmask |= HFI1_RCVCTRL_NO_RHQ_DROP_ENB;
596 if (HFI1_CAP_KGET_MASK(dd->rcd[i]->flags, NODROP_EGR_FULL))
597 rcvmask |= HFI1_RCVCTRL_NO_EGR_DROP_ENB;
598 hfi1_rcvctrl(dd, rcvmask, i);
599 sc_enable(dd->rcd[i]->sc);
600 }
601}
602
603/**
604 * create_workqueues - create per port workqueues
605 * @dd: the hfi1_ib device
606 */
607static int create_workqueues(struct hfi1_devdata *dd)
608{
609 int pidx;
610 struct hfi1_pportdata *ppd;
611
612 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
613 ppd = dd->pport + pidx;
614 if (!ppd->hfi1_wq) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400615 ppd->hfi1_wq =
Mike Marciniszyn0a226ed2015-11-09 19:13:58 -0500616 alloc_workqueue(
617 "hfi%d_%d",
618 WQ_SYSFS | WQ_HIGHPRI | WQ_CPU_INTENSIVE,
619 dd->num_sdma,
620 dd->unit, pidx);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400621 if (!ppd->hfi1_wq)
622 goto wq_error;
623 }
624 }
625 return 0;
626wq_error:
Mike Marciniszyn0a226ed2015-11-09 19:13:58 -0500627 pr_err("alloc_workqueue failed for port %d\n", pidx + 1);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400628 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
629 ppd = dd->pport + pidx;
630 if (ppd->hfi1_wq) {
631 destroy_workqueue(ppd->hfi1_wq);
632 ppd->hfi1_wq = NULL;
633 }
634 }
635 return -ENOMEM;
636}
637
638/**
639 * hfi1_init - do the actual initialization sequence on the chip
640 * @dd: the hfi1_ib device
641 * @reinit: re-initializing, so don't allocate new memory
642 *
643 * Do the actual initialization sequence on the chip. This is done
644 * both from the init routine called from the PCI infrastructure, and
645 * when we reset the chip, or detect that it was reset internally,
646 * or it's administratively re-enabled.
647 *
648 * Memory allocation here and in called routines is only done in
649 * the first case (reinit == 0). We have to be careful, because even
650 * without memory allocation, we need to re-write all the chip registers
651 * TIDs, etc. after the reset or enable has completed.
652 */
653int hfi1_init(struct hfi1_devdata *dd, int reinit)
654{
655 int ret = 0, pidx, lastfail = 0;
656 unsigned i, len;
657 struct hfi1_ctxtdata *rcd;
658 struct hfi1_pportdata *ppd;
659
660 /* Set up recv low level handlers */
661 dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_EXPECTED] =
662 kdeth_process_expected;
663 dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_EAGER] =
664 kdeth_process_eager;
665 dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_IB] = process_receive_ib;
666 dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_ERROR] =
667 process_receive_error;
668 dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_BYPASS] =
669 process_receive_bypass;
670 dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_INVALID5] =
671 process_receive_invalid;
672 dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_INVALID6] =
673 process_receive_invalid;
674 dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_INVALID7] =
675 process_receive_invalid;
676 dd->rhf_rcv_function_map = dd->normal_rhf_rcv_functions;
677
678 /* Set up send low level handlers */
679 dd->process_pio_send = hfi1_verbs_send_pio;
680 dd->process_dma_send = hfi1_verbs_send_dma;
681 dd->pio_inline_send = pio_copy;
682
Mike Marciniszyn995deaf2015-11-16 21:59:29 -0500683 if (is_ax(dd)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400684 atomic_set(&dd->drop_packet, DROP_PACKET_ON);
685 dd->do_drop = 1;
686 } else {
687 atomic_set(&dd->drop_packet, DROP_PACKET_OFF);
688 dd->do_drop = 0;
689 }
690
691 /* make sure the link is not "up" */
692 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
693 ppd = dd->pport + pidx;
694 ppd->linkup = 0;
695 }
696
697 if (reinit)
698 ret = init_after_reset(dd);
699 else
700 ret = loadtime_init(dd);
701 if (ret)
702 goto done;
703
Mark F. Brown46b010d2015-11-09 19:18:20 -0500704 /* allocate dummy tail memory for all receive contexts */
705 dd->rcvhdrtail_dummy_kvaddr = dma_zalloc_coherent(
706 &dd->pcidev->dev, sizeof(u64),
707 &dd->rcvhdrtail_dummy_physaddr,
708 GFP_KERNEL);
709
710 if (!dd->rcvhdrtail_dummy_kvaddr) {
711 dd_dev_err(dd, "cannot allocate dummy tail memory\n");
712 ret = -ENOMEM;
713 goto done;
714 }
715
Mike Marciniszyn77241052015-07-30 15:17:43 -0400716 /* dd->rcd can be NULL if early initialization failed */
717 for (i = 0; dd->rcd && i < dd->first_user_ctxt; ++i) {
718 /*
719 * Set up the (kernel) rcvhdr queue and egr TIDs. If doing
720 * re-init, the simplest way to handle this is to free
721 * existing, and re-allocate.
722 * Need to re-create rest of ctxt 0 ctxtdata as well.
723 */
724 rcd = dd->rcd[i];
725 if (!rcd)
726 continue;
727
728 rcd->do_interrupt = &handle_receive_interrupt;
729
730 lastfail = hfi1_create_rcvhdrq(dd, rcd);
731 if (!lastfail)
732 lastfail = hfi1_setup_eagerbufs(rcd);
733 if (lastfail)
734 dd_dev_err(dd,
735 "failed to allocate kernel ctxt's rcvhdrq and/or egr bufs\n");
736 }
737 if (lastfail)
738 ret = lastfail;
739
740 /* Allocate enough memory for user event notification. */
741 len = ALIGN(dd->chip_rcv_contexts * HFI1_MAX_SHARED_CTXTS *
742 sizeof(*dd->events), PAGE_SIZE);
743 dd->events = vmalloc_user(len);
744 if (!dd->events)
745 dd_dev_err(dd, "Failed to allocate user events page\n");
746 /*
747 * Allocate a page for device and port status.
748 * Page will be shared amongst all user processes.
749 */
750 dd->status = vmalloc_user(PAGE_SIZE);
751 if (!dd->status)
752 dd_dev_err(dd, "Failed to allocate dev status page\n");
753 else
754 dd->freezelen = PAGE_SIZE - (sizeof(*dd->status) -
755 sizeof(dd->status->freezemsg));
756 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
757 ppd = dd->pport + pidx;
758 if (dd->status)
759 /* Currently, we only have one port */
760 ppd->statusp = &dd->status->port;
761
762 set_mtu(ppd);
763 }
764
765 /* enable chip even if we have an error, so we can debug cause */
766 enable_chip(dd);
767
Mike Marciniszyn77241052015-07-30 15:17:43 -0400768done:
769 /*
770 * Set status even if port serdes is not initialized
771 * so that diags will work.
772 */
773 if (dd->status)
774 dd->status->dev |= HFI1_STATUS_CHIP_PRESENT |
775 HFI1_STATUS_INITTED;
776 if (!ret) {
777 /* enable all interrupts from the chip */
778 set_intr_state(dd, 1);
779
780 /* chip is OK for user apps; mark it as initialized */
781 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
782 ppd = dd->pport + pidx;
783
784 /* initialize the qsfp if it exists
785 * Requires interrupts to be enabled so we are notified
786 * when the QSFP completes reset, and has
787 * to be done before bringing up the SERDES
788 */
789 init_qsfp(ppd);
790
791 /* start the serdes - must be after interrupts are
792 enabled so we are notified when the link goes up */
793 lastfail = bringup_serdes(ppd);
794 if (lastfail)
795 dd_dev_info(dd,
796 "Failed to bring up port %u\n",
797 ppd->port);
798
799 /*
800 * Set status even if port serdes is not initialized
801 * so that diags will work.
802 */
803 if (ppd->statusp)
804 *ppd->statusp |= HFI1_STATUS_CHIP_PRESENT |
805 HFI1_STATUS_INITTED;
806 if (!ppd->link_speed_enabled)
807 continue;
808 }
809 }
810
811 /* if ret is non-zero, we probably should do some cleanup here... */
812 return ret;
813}
814
815static inline struct hfi1_devdata *__hfi1_lookup(int unit)
816{
817 return idr_find(&hfi1_unit_table, unit);
818}
819
820struct hfi1_devdata *hfi1_lookup(int unit)
821{
822 struct hfi1_devdata *dd;
823 unsigned long flags;
824
825 spin_lock_irqsave(&hfi1_devs_lock, flags);
826 dd = __hfi1_lookup(unit);
827 spin_unlock_irqrestore(&hfi1_devs_lock, flags);
828
829 return dd;
830}
831
832/*
833 * Stop the timers during unit shutdown, or after an error late
834 * in initialization.
835 */
836static void stop_timers(struct hfi1_devdata *dd)
837{
838 struct hfi1_pportdata *ppd;
839 int pidx;
840
841 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
842 ppd = dd->pport + pidx;
843 if (ppd->led_override_timer.data) {
844 del_timer_sync(&ppd->led_override_timer);
845 atomic_set(&ppd->led_override_timer_active, 0);
846 }
847 }
848}
849
850/**
851 * shutdown_device - shut down a device
852 * @dd: the hfi1_ib device
853 *
854 * This is called to make the device quiet when we are about to
855 * unload the driver, and also when the device is administratively
856 * disabled. It does not free any data structures.
857 * Everything it does has to be setup again by hfi1_init(dd, 1)
858 */
859static void shutdown_device(struct hfi1_devdata *dd)
860{
861 struct hfi1_pportdata *ppd;
862 unsigned pidx;
863 int i;
864
865 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
866 ppd = dd->pport + pidx;
867
868 ppd->linkup = 0;
869 if (ppd->statusp)
870 *ppd->statusp &= ~(HFI1_STATUS_IB_CONF |
871 HFI1_STATUS_IB_READY);
872 }
873 dd->flags &= ~HFI1_INITTED;
874
875 /* mask interrupts, but not errors */
876 set_intr_state(dd, 0);
877
878 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
879 ppd = dd->pport + pidx;
880 for (i = 0; i < dd->num_rcv_contexts; i++)
881 hfi1_rcvctrl(dd, HFI1_RCVCTRL_TAILUPD_DIS |
882 HFI1_RCVCTRL_CTXT_DIS |
883 HFI1_RCVCTRL_INTRAVAIL_DIS |
884 HFI1_RCVCTRL_PKEY_DIS |
885 HFI1_RCVCTRL_ONE_PKT_EGR_DIS, i);
886 /*
887 * Gracefully stop all sends allowing any in progress to
888 * trickle out first.
889 */
890 for (i = 0; i < dd->num_send_contexts; i++)
891 sc_flush(dd->send_contexts[i].sc);
892 }
893
894 /*
895 * Enough for anything that's going to trickle out to have actually
896 * done so.
897 */
898 udelay(20);
899
900 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
901 ppd = dd->pport + pidx;
902
903 /* disable all contexts */
904 for (i = 0; i < dd->num_send_contexts; i++)
905 sc_disable(dd->send_contexts[i].sc);
906 /* disable the send device */
907 pio_send_control(dd, PSC_GLOBAL_DISABLE);
908
909 /*
910 * Clear SerdesEnable.
911 * We can't count on interrupts since we are stopping.
912 */
913 hfi1_quiet_serdes(ppd);
914
915 if (ppd->hfi1_wq) {
916 destroy_workqueue(ppd->hfi1_wq);
917 ppd->hfi1_wq = NULL;
918 }
919 }
920 sdma_exit(dd);
921}
922
923/**
924 * hfi1_free_ctxtdata - free a context's allocated data
925 * @dd: the hfi1_ib device
926 * @rcd: the ctxtdata structure
927 *
928 * free up any allocated data for a context
929 * This should not touch anything that would affect a simultaneous
930 * re-allocation of context data, because it is called after hfi1_mutex
931 * is released (and can be called from reinit as well).
932 * It should never change any chip state, or global driver state.
933 */
934void hfi1_free_ctxtdata(struct hfi1_devdata *dd, struct hfi1_ctxtdata *rcd)
935{
936 unsigned e;
937
938 if (!rcd)
939 return;
940
941 if (rcd->rcvhdrq) {
942 dma_free_coherent(&dd->pcidev->dev, rcd->rcvhdrq_size,
943 rcd->rcvhdrq, rcd->rcvhdrq_phys);
944 rcd->rcvhdrq = NULL;
945 if (rcd->rcvhdrtail_kvaddr) {
946 dma_free_coherent(&dd->pcidev->dev, PAGE_SIZE,
947 (void *)rcd->rcvhdrtail_kvaddr,
948 rcd->rcvhdrqtailaddr_phys);
949 rcd->rcvhdrtail_kvaddr = NULL;
950 }
951 }
952
953 /* all the RcvArray entries should have been cleared by now */
954 kfree(rcd->egrbufs.rcvtids);
955
956 for (e = 0; e < rcd->egrbufs.alloced; e++) {
957 if (rcd->egrbufs.buffers[e].phys)
958 dma_free_coherent(&dd->pcidev->dev,
959 rcd->egrbufs.buffers[e].len,
960 rcd->egrbufs.buffers[e].addr,
961 rcd->egrbufs.buffers[e].phys);
962 }
963 kfree(rcd->egrbufs.buffers);
964
965 sc_free(rcd->sc);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400966 vfree(rcd->user_event_mask);
967 vfree(rcd->subctxt_uregbase);
968 vfree(rcd->subctxt_rcvegrbuf);
969 vfree(rcd->subctxt_rcvhdr_base);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400970 kfree(rcd->opstats);
971 kfree(rcd);
972}
973
974void hfi1_free_devdata(struct hfi1_devdata *dd)
975{
976 unsigned long flags;
977
978 spin_lock_irqsave(&hfi1_devs_lock, flags);
979 idr_remove(&hfi1_unit_table, dd->unit);
980 list_del(&dd->list);
981 spin_unlock_irqrestore(&hfi1_devs_lock, flags);
982 hfi1_dbg_ibdev_exit(&dd->verbs_dev);
983 rcu_barrier(); /* wait for rcu callbacks to complete */
984 free_percpu(dd->int_counter);
985 free_percpu(dd->rcv_limit);
Dennis Dalessandroec3f2c12016-01-19 14:41:33 -0800986 ib_dealloc_device(&dd->verbs_dev.rdi.ibdev);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400987}
988
989/*
990 * Allocate our primary per-unit data structure. Must be done via verbs
991 * allocator, because the verbs cleanup process both does cleanup and
992 * free of the data structure.
993 * "extra" is for chip-specific data.
994 *
995 * Use the idr mechanism to get a unit number for this unit.
996 */
997struct hfi1_devdata *hfi1_alloc_devdata(struct pci_dev *pdev, size_t extra)
998{
999 unsigned long flags;
1000 struct hfi1_devdata *dd;
Dennis Dalessandro7af6d002016-01-19 14:44:06 -08001001 int ret, nports;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001002
Dennis Dalessandro7af6d002016-01-19 14:44:06 -08001003 /* extra is * number of ports */
1004 nports = extra / sizeof(struct hfi1_pportdata);
1005
1006 dd = (struct hfi1_devdata *)rvt_alloc_device(sizeof(*dd) + extra,
1007 nports);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001008 if (!dd)
1009 return ERR_PTR(-ENOMEM);
Dennis Dalessandro7af6d002016-01-19 14:44:06 -08001010 dd->num_pports = nports;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001011 dd->pport = (struct hfi1_pportdata *)(dd + 1);
1012
1013 INIT_LIST_HEAD(&dd->list);
1014 dd->node = dev_to_node(&pdev->dev);
1015 if (dd->node < 0)
1016 dd->node = 0;
1017 idr_preload(GFP_KERNEL);
1018 spin_lock_irqsave(&hfi1_devs_lock, flags);
1019
1020 ret = idr_alloc(&hfi1_unit_table, dd, 0, 0, GFP_NOWAIT);
1021 if (ret >= 0) {
1022 dd->unit = ret;
1023 list_add(&dd->list, &hfi1_dev_list);
1024 }
1025
1026 spin_unlock_irqrestore(&hfi1_devs_lock, flags);
1027 idr_preload_end();
1028
1029 if (ret < 0) {
1030 hfi1_early_err(&pdev->dev,
1031 "Could not allocate unit ID: error %d\n", -ret);
1032 goto bail;
1033 }
1034 /*
1035 * Initialize all locks for the device. This needs to be as early as
1036 * possible so locks are usable.
1037 */
1038 spin_lock_init(&dd->sc_lock);
1039 spin_lock_init(&dd->sendctrl_lock);
1040 spin_lock_init(&dd->rcvctrl_lock);
1041 spin_lock_init(&dd->uctxt_lock);
1042 spin_lock_init(&dd->hfi1_diag_trans_lock);
1043 spin_lock_init(&dd->sc_init_lock);
1044 spin_lock_init(&dd->dc8051_lock);
1045 spin_lock_init(&dd->dc8051_memlock);
1046 mutex_init(&dd->qsfp_i2c_mutex);
1047 seqlock_init(&dd->sc2vl_lock);
1048 spin_lock_init(&dd->sde_map_lock);
1049 init_waitqueue_head(&dd->event_queue);
1050
1051 dd->int_counter = alloc_percpu(u64);
1052 if (!dd->int_counter) {
1053 ret = -ENOMEM;
1054 hfi1_early_err(&pdev->dev,
1055 "Could not allocate per-cpu int_counter\n");
1056 goto bail;
1057 }
1058
1059 dd->rcv_limit = alloc_percpu(u64);
1060 if (!dd->rcv_limit) {
1061 ret = -ENOMEM;
1062 hfi1_early_err(&pdev->dev,
1063 "Could not allocate per-cpu rcv_limit\n");
1064 goto bail;
1065 }
1066
1067 if (!hfi1_cpulist_count) {
1068 u32 count = num_online_cpus();
1069
Shraddha Barke314fcc02015-10-09 21:03:26 +05301070 hfi1_cpulist = kcalloc(BITS_TO_LONGS(count), sizeof(long),
1071 GFP_KERNEL);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001072 if (hfi1_cpulist)
1073 hfi1_cpulist_count = count;
1074 else
1075 hfi1_early_err(
1076 &pdev->dev,
1077 "Could not alloc cpulist info, cpu affinity might be wrong\n");
1078 }
1079 hfi1_dbg_ibdev_init(&dd->verbs_dev);
1080 return dd;
1081
1082bail:
1083 if (!list_empty(&dd->list))
1084 list_del_init(&dd->list);
Dennis Dalessandroec3f2c12016-01-19 14:41:33 -08001085 ib_dealloc_device(&dd->verbs_dev.rdi.ibdev);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001086 return ERR_PTR(ret);
1087}
1088
1089/*
1090 * Called from freeze mode handlers, and from PCI error
1091 * reporting code. Should be paranoid about state of
1092 * system and data structures.
1093 */
1094void hfi1_disable_after_error(struct hfi1_devdata *dd)
1095{
1096 if (dd->flags & HFI1_INITTED) {
1097 u32 pidx;
1098
1099 dd->flags &= ~HFI1_INITTED;
1100 if (dd->pport)
1101 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
1102 struct hfi1_pportdata *ppd;
1103
1104 ppd = dd->pport + pidx;
1105 if (dd->flags & HFI1_PRESENT)
1106 set_link_state(ppd, HLS_DN_DISABLE);
1107
1108 if (ppd->statusp)
1109 *ppd->statusp &= ~HFI1_STATUS_IB_READY;
1110 }
1111 }
1112
1113 /*
1114 * Mark as having had an error for driver, and also
1115 * for /sys and status word mapped to user programs.
1116 * This marks unit as not usable, until reset.
1117 */
1118 if (dd->status)
1119 dd->status->dev |= HFI1_STATUS_HWERROR;
1120}
1121
1122static void remove_one(struct pci_dev *);
1123static int init_one(struct pci_dev *, const struct pci_device_id *);
1124
1125#define DRIVER_LOAD_MSG "Intel " DRIVER_NAME " loaded: "
1126#define PFX DRIVER_NAME ": "
1127
1128static const struct pci_device_id hfi1_pci_tbl[] = {
1129 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL0) },
1130 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL1) },
1131 { 0, }
1132};
1133
1134MODULE_DEVICE_TABLE(pci, hfi1_pci_tbl);
1135
1136static struct pci_driver hfi1_pci_driver = {
1137 .name = DRIVER_NAME,
1138 .probe = init_one,
1139 .remove = remove_one,
1140 .id_table = hfi1_pci_tbl,
1141 .err_handler = &hfi1_pci_err_handler,
1142};
1143
1144static void __init compute_krcvqs(void)
1145{
1146 int i;
1147
1148 for (i = 0; i < krcvqsset; i++)
1149 n_krcvqs += krcvqs[i];
1150}
1151
1152/*
1153 * Do all the generic driver unit- and chip-independent memory
1154 * allocation and initialization.
1155 */
1156static int __init hfi1_mod_init(void)
1157{
1158 int ret;
1159
1160 ret = dev_init();
1161 if (ret)
1162 goto bail;
1163
1164 /* validate max MTU before any devices start */
1165 if (!valid_opa_max_mtu(hfi1_max_mtu)) {
1166 pr_err("Invalid max_mtu 0x%x, using 0x%x instead\n",
1167 hfi1_max_mtu, HFI1_DEFAULT_MAX_MTU);
1168 hfi1_max_mtu = HFI1_DEFAULT_MAX_MTU;
1169 }
1170 /* valid CUs run from 1-128 in powers of 2 */
1171 if (hfi1_cu > 128 || !is_power_of_2(hfi1_cu))
1172 hfi1_cu = 1;
1173 /* valid credit return threshold is 0-100, variable is unsigned */
1174 if (user_credit_return_threshold > 100)
1175 user_credit_return_threshold = 100;
1176
1177 compute_krcvqs();
1178 /* sanitize receive interrupt count, time must wait until after
1179 the hardware type is known */
1180 if (rcv_intr_count > RCV_HDR_HEAD_COUNTER_MASK)
1181 rcv_intr_count = RCV_HDR_HEAD_COUNTER_MASK;
1182 /* reject invalid combinations */
1183 if (rcv_intr_count == 0 && rcv_intr_timeout == 0) {
1184 pr_err("Invalid mode: both receive interrupt count and available timeout are zero - setting interrupt count to 1\n");
1185 rcv_intr_count = 1;
1186 }
1187 if (rcv_intr_count > 1 && rcv_intr_timeout == 0) {
1188 /*
1189 * Avoid indefinite packet delivery by requiring a timeout
1190 * if count is > 1.
1191 */
1192 pr_err("Invalid mode: receive interrupt count greater than 1 and available timeout is zero - setting available timeout to 1\n");
1193 rcv_intr_timeout = 1;
1194 }
1195 if (rcv_intr_dynamic && !(rcv_intr_count > 1 && rcv_intr_timeout > 0)) {
1196 /*
1197 * The dynamic algorithm expects a non-zero timeout
1198 * and a count > 1.
1199 */
1200 pr_err("Invalid mode: dynamic receive interrupt mitigation with invalid count and timeout - turning dynamic off\n");
1201 rcv_intr_dynamic = 0;
1202 }
1203
1204 /* sanitize link CRC options */
1205 link_crc_mask &= SUPPORTED_CRCS;
1206
1207 /*
1208 * These must be called before the driver is registered with
1209 * the PCI subsystem.
1210 */
1211 idr_init(&hfi1_unit_table);
1212
1213 hfi1_dbg_init();
1214 ret = pci_register_driver(&hfi1_pci_driver);
1215 if (ret < 0) {
1216 pr_err("Unable to register driver: error %d\n", -ret);
1217 goto bail_dev;
1218 }
1219 goto bail; /* all OK */
1220
1221bail_dev:
1222 hfi1_dbg_exit();
1223 idr_destroy(&hfi1_unit_table);
1224 dev_cleanup();
1225bail:
1226 return ret;
1227}
1228
1229module_init(hfi1_mod_init);
1230
1231/*
1232 * Do the non-unit driver cleanup, memory free, etc. at unload.
1233 */
1234static void __exit hfi1_mod_cleanup(void)
1235{
1236 pci_unregister_driver(&hfi1_pci_driver);
1237 hfi1_dbg_exit();
1238 hfi1_cpulist_count = 0;
1239 kfree(hfi1_cpulist);
1240
1241 idr_destroy(&hfi1_unit_table);
1242 dispose_firmware(); /* asymmetric with obtain_firmware() */
1243 dev_cleanup();
1244}
1245
1246module_exit(hfi1_mod_cleanup);
1247
1248/* this can only be called after a successful initialization */
1249static void cleanup_device_data(struct hfi1_devdata *dd)
1250{
1251 int ctxt;
1252 int pidx;
1253 struct hfi1_ctxtdata **tmp;
1254 unsigned long flags;
1255
1256 /* users can't do anything more with chip */
1257 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
1258 struct hfi1_pportdata *ppd = &dd->pport[pidx];
1259 struct cc_state *cc_state;
1260 int i;
1261
1262 if (ppd->statusp)
1263 *ppd->statusp &= ~HFI1_STATUS_CHIP_PRESENT;
1264
1265 for (i = 0; i < OPA_MAX_SLS; i++)
1266 hrtimer_cancel(&ppd->cca_timer[i].hrtimer);
1267
1268 spin_lock(&ppd->cc_state_lock);
1269 cc_state = get_cc_state(ppd);
1270 rcu_assign_pointer(ppd->cc_state, NULL);
1271 spin_unlock(&ppd->cc_state_lock);
1272
1273 if (cc_state)
1274 call_rcu(&cc_state->rcu, cc_state_reclaim);
1275 }
1276
1277 free_credit_return(dd);
1278
1279 /*
1280 * Free any resources still in use (usually just kernel contexts)
1281 * at unload; we do for ctxtcnt, because that's what we allocate.
1282 * We acquire lock to be really paranoid that rcd isn't being
1283 * accessed from some interrupt-related code (that should not happen,
1284 * but best to be sure).
1285 */
1286 spin_lock_irqsave(&dd->uctxt_lock, flags);
1287 tmp = dd->rcd;
1288 dd->rcd = NULL;
1289 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
Mark F. Brown46b010d2015-11-09 19:18:20 -05001290
1291 if (dd->rcvhdrtail_dummy_kvaddr) {
1292 dma_free_coherent(&dd->pcidev->dev, sizeof(u64),
1293 (void *)dd->rcvhdrtail_dummy_kvaddr,
1294 dd->rcvhdrtail_dummy_physaddr);
1295 dd->rcvhdrtail_dummy_kvaddr = NULL;
1296 }
1297
Mike Marciniszyn77241052015-07-30 15:17:43 -04001298 for (ctxt = 0; tmp && ctxt < dd->num_rcv_contexts; ctxt++) {
1299 struct hfi1_ctxtdata *rcd = tmp[ctxt];
1300
1301 tmp[ctxt] = NULL; /* debugging paranoia */
1302 if (rcd) {
1303 hfi1_clear_tids(rcd);
1304 hfi1_free_ctxtdata(dd, rcd);
1305 }
1306 }
1307 kfree(tmp);
1308 /* must follow rcv context free - need to remove rcv's hooks */
1309 for (ctxt = 0; ctxt < dd->num_send_contexts; ctxt++)
1310 sc_free(dd->send_contexts[ctxt].sc);
1311 dd->num_send_contexts = 0;
1312 kfree(dd->send_contexts);
1313 dd->send_contexts = NULL;
1314 kfree(dd->boardname);
1315 vfree(dd->events);
1316 vfree(dd->status);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001317}
1318
1319/*
1320 * Clean up on unit shutdown, or error during unit load after
1321 * successful initialization.
1322 */
1323static void postinit_cleanup(struct hfi1_devdata *dd)
1324{
1325 hfi1_start_cleanup(dd);
1326
1327 hfi1_pcie_ddcleanup(dd);
1328 hfi1_pcie_cleanup(dd->pcidev);
1329
1330 cleanup_device_data(dd);
1331
1332 hfi1_free_devdata(dd);
1333}
1334
1335static int init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
1336{
1337 int ret = 0, j, pidx, initfail;
1338 struct hfi1_devdata *dd = NULL;
Harish Chegondie8597eb2015-12-01 15:38:20 -05001339 struct hfi1_pportdata *ppd;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001340
1341 /* First, lock the non-writable module parameters */
1342 HFI1_CAP_LOCK();
1343
1344 /* Validate some global module parameters */
1345 if (rcvhdrcnt <= HFI1_MIN_HDRQ_EGRBUF_CNT) {
1346 hfi1_early_err(&pdev->dev, "Header queue count too small\n");
1347 ret = -EINVAL;
1348 goto bail;
1349 }
1350 /* use the encoding function as a sanitization check */
1351 if (!encode_rcv_header_entry_size(hfi1_hdrq_entsize)) {
1352 hfi1_early_err(&pdev->dev, "Invalid HdrQ Entry size %u\n",
1353 hfi1_hdrq_entsize);
Sebastian Sanchez07859de2015-12-10 16:02:49 -05001354 ret = -EINVAL;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001355 goto bail;
1356 }
1357
1358 /* The receive eager buffer size must be set before the receive
1359 * contexts are created.
1360 *
1361 * Set the eager buffer size. Validate that it falls in a range
1362 * allowed by the hardware - all powers of 2 between the min and
1363 * max. The maximum valid MTU is within the eager buffer range
1364 * so we do not need to cap the max_mtu by an eager buffer size
1365 * setting.
1366 */
1367 if (eager_buffer_size) {
1368 if (!is_power_of_2(eager_buffer_size))
1369 eager_buffer_size =
1370 roundup_pow_of_two(eager_buffer_size);
1371 eager_buffer_size =
1372 clamp_val(eager_buffer_size,
1373 MIN_EAGER_BUFFER * 8,
1374 MAX_EAGER_BUFFER_TOTAL);
1375 hfi1_early_info(&pdev->dev, "Eager buffer size %u\n",
1376 eager_buffer_size);
1377 } else {
1378 hfi1_early_err(&pdev->dev, "Invalid Eager buffer size of 0\n");
1379 ret = -EINVAL;
1380 goto bail;
1381 }
1382
1383 /* restrict value of hfi1_rcvarr_split */
1384 hfi1_rcvarr_split = clamp_val(hfi1_rcvarr_split, 0, 100);
1385
1386 ret = hfi1_pcie_init(pdev, ent);
1387 if (ret)
1388 goto bail;
1389
1390 /*
1391 * Do device-specific initialization, function table setup, dd
1392 * allocation, etc.
1393 */
1394 switch (ent->device) {
1395 case PCI_DEVICE_ID_INTEL0:
1396 case PCI_DEVICE_ID_INTEL1:
1397 dd = hfi1_init_dd(pdev, ent);
1398 break;
1399 default:
1400 hfi1_early_err(&pdev->dev,
1401 "Failing on unknown Intel deviceid 0x%x\n",
1402 ent->device);
1403 ret = -ENODEV;
1404 }
1405
1406 if (IS_ERR(dd))
1407 ret = PTR_ERR(dd);
1408 if (ret)
1409 goto clean_bail; /* error already printed */
1410
1411 ret = create_workqueues(dd);
1412 if (ret)
1413 goto clean_bail;
1414
1415 /* do the generic initialization */
1416 initfail = hfi1_init(dd, 0);
1417
1418 ret = hfi1_register_ib_device(dd);
1419
1420 /*
1421 * Now ready for use. this should be cleared whenever we
1422 * detect a reset, or initiate one. If earlier failure,
1423 * we still create devices, so diags, etc. can be used
1424 * to determine cause of problem.
1425 */
1426 if (!initfail && !ret)
1427 dd->flags |= HFI1_INITTED;
1428
1429 j = hfi1_device_create(dd);
1430 if (j)
1431 dd_dev_err(dd, "Failed to create /dev devices: %d\n", -j);
1432
1433 if (initfail || ret) {
1434 stop_timers(dd);
1435 flush_workqueue(ib_wq);
Harish Chegondie8597eb2015-12-01 15:38:20 -05001436 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001437 hfi1_quiet_serdes(dd->pport + pidx);
Harish Chegondie8597eb2015-12-01 15:38:20 -05001438 ppd = dd->pport + pidx;
1439 if (ppd->hfi1_wq) {
1440 destroy_workqueue(ppd->hfi1_wq);
1441 ppd->hfi1_wq = NULL;
1442 }
1443 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001444 if (!j)
1445 hfi1_device_remove(dd);
1446 if (!ret)
1447 hfi1_unregister_ib_device(dd);
1448 postinit_cleanup(dd);
1449 if (initfail)
1450 ret = initfail;
1451 goto bail; /* everything already cleaned */
1452 }
1453
1454 sdma_start(dd);
1455
1456 return 0;
1457
1458clean_bail:
1459 hfi1_pcie_cleanup(pdev);
1460bail:
1461 return ret;
1462}
1463
1464static void remove_one(struct pci_dev *pdev)
1465{
1466 struct hfi1_devdata *dd = pci_get_drvdata(pdev);
1467
1468 /* unregister from IB core */
1469 hfi1_unregister_ib_device(dd);
1470
1471 /*
1472 * Disable the IB link, disable interrupts on the device,
1473 * clear dma engines, etc.
1474 */
1475 shutdown_device(dd);
1476
1477 stop_timers(dd);
1478
1479 /* wait until all of our (qsfp) queue_work() calls complete */
1480 flush_workqueue(ib_wq);
1481
1482 hfi1_device_remove(dd);
1483
1484 postinit_cleanup(dd);
1485}
1486
1487/**
1488 * hfi1_create_rcvhdrq - create a receive header queue
1489 * @dd: the hfi1_ib device
1490 * @rcd: the context data
1491 *
1492 * This must be contiguous memory (from an i/o perspective), and must be
1493 * DMA'able (which means for some systems, it will go through an IOMMU,
1494 * or be forced into a low address range).
1495 */
1496int hfi1_create_rcvhdrq(struct hfi1_devdata *dd, struct hfi1_ctxtdata *rcd)
1497{
1498 unsigned amt;
1499 u64 reg;
1500
1501 if (!rcd->rcvhdrq) {
1502 dma_addr_t phys_hdrqtail;
1503 gfp_t gfp_flags;
1504
1505 /*
1506 * rcvhdrqentsize is in DWs, so we have to convert to bytes
1507 * (* sizeof(u32)).
1508 */
1509 amt = ALIGN(rcd->rcvhdrq_cnt * rcd->rcvhdrqentsize *
1510 sizeof(u32), PAGE_SIZE);
1511
1512 gfp_flags = (rcd->ctxt >= dd->first_user_ctxt) ?
1513 GFP_USER : GFP_KERNEL;
1514 rcd->rcvhdrq = dma_zalloc_coherent(
1515 &dd->pcidev->dev, amt, &rcd->rcvhdrq_phys,
1516 gfp_flags | __GFP_COMP);
1517
1518 if (!rcd->rcvhdrq) {
1519 dd_dev_err(dd,
1520 "attempt to allocate %d bytes for ctxt %u rcvhdrq failed\n",
1521 amt, rcd->ctxt);
1522 goto bail;
1523 }
1524
1525 /* Event mask is per device now and is in hfi1_devdata */
1526 /*if (rcd->ctxt >= dd->first_user_ctxt) {
1527 rcd->user_event_mask = vmalloc_user(PAGE_SIZE);
1528 if (!rcd->user_event_mask)
1529 goto bail_free_hdrq;
1530 }*/
1531
1532 if (HFI1_CAP_KGET_MASK(rcd->flags, DMA_RTAIL)) {
1533 rcd->rcvhdrtail_kvaddr = dma_zalloc_coherent(
1534 &dd->pcidev->dev, PAGE_SIZE, &phys_hdrqtail,
1535 gfp_flags);
1536 if (!rcd->rcvhdrtail_kvaddr)
1537 goto bail_free;
1538 rcd->rcvhdrqtailaddr_phys = phys_hdrqtail;
1539 }
1540
1541 rcd->rcvhdrq_size = amt;
1542 }
1543 /*
1544 * These values are per-context:
1545 * RcvHdrCnt
1546 * RcvHdrEntSize
1547 * RcvHdrSize
1548 */
1549 reg = ((u64)(rcd->rcvhdrq_cnt >> HDRQ_SIZE_SHIFT)
1550 & RCV_HDR_CNT_CNT_MASK)
1551 << RCV_HDR_CNT_CNT_SHIFT;
1552 write_kctxt_csr(dd, rcd->ctxt, RCV_HDR_CNT, reg);
1553 reg = (encode_rcv_header_entry_size(rcd->rcvhdrqentsize)
1554 & RCV_HDR_ENT_SIZE_ENT_SIZE_MASK)
1555 << RCV_HDR_ENT_SIZE_ENT_SIZE_SHIFT;
1556 write_kctxt_csr(dd, rcd->ctxt, RCV_HDR_ENT_SIZE, reg);
1557 reg = (dd->rcvhdrsize & RCV_HDR_SIZE_HDR_SIZE_MASK)
1558 << RCV_HDR_SIZE_HDR_SIZE_SHIFT;
1559 write_kctxt_csr(dd, rcd->ctxt, RCV_HDR_SIZE, reg);
Mark F. Brown46b010d2015-11-09 19:18:20 -05001560
1561 /*
1562 * Program dummy tail address for every receive context
1563 * before enabling any receive context
1564 */
1565 write_kctxt_csr(dd, rcd->ctxt, RCV_HDR_TAIL_ADDR,
1566 dd->rcvhdrtail_dummy_physaddr);
1567
Mike Marciniszyn77241052015-07-30 15:17:43 -04001568 return 0;
1569
1570bail_free:
1571 dd_dev_err(dd,
1572 "attempt to allocate 1 page for ctxt %u rcvhdrqtailaddr failed\n",
1573 rcd->ctxt);
1574 vfree(rcd->user_event_mask);
1575 rcd->user_event_mask = NULL;
1576 dma_free_coherent(&dd->pcidev->dev, amt, rcd->rcvhdrq,
1577 rcd->rcvhdrq_phys);
1578 rcd->rcvhdrq = NULL;
1579bail:
1580 return -ENOMEM;
1581}
1582
1583/**
1584 * allocate eager buffers, both kernel and user contexts.
1585 * @rcd: the context we are setting up.
1586 *
1587 * Allocate the eager TID buffers and program them into hip.
1588 * They are no longer completely contiguous, we do multiple allocation
1589 * calls. Otherwise we get the OOM code involved, by asking for too
1590 * much per call, with disastrous results on some kernels.
1591 */
1592int hfi1_setup_eagerbufs(struct hfi1_ctxtdata *rcd)
1593{
1594 struct hfi1_devdata *dd = rcd->dd;
1595 u32 max_entries, egrtop, alloced_bytes = 0, idx = 0;
1596 gfp_t gfp_flags;
1597 u16 order;
1598 int ret = 0;
1599 u16 round_mtu = roundup_pow_of_two(hfi1_max_mtu);
1600
1601 /*
1602 * GFP_USER, but without GFP_FS, so buffer cache can be
1603 * coalesced (we hope); otherwise, even at order 4,
1604 * heavy filesystem activity makes these fail, and we can
1605 * use compound pages.
1606 */
Mel Gorman71baba42015-11-06 16:28:28 -08001607 gfp_flags = __GFP_RECLAIM | __GFP_IO | __GFP_COMP;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001608
1609 /*
1610 * The minimum size of the eager buffers is a groups of MTU-sized
1611 * buffers.
1612 * The global eager_buffer_size parameter is checked against the
1613 * theoretical lower limit of the value. Here, we check against the
1614 * MTU.
1615 */
1616 if (rcd->egrbufs.size < (round_mtu * dd->rcv_entries.group_size))
1617 rcd->egrbufs.size = round_mtu * dd->rcv_entries.group_size;
1618 /*
1619 * If using one-pkt-per-egr-buffer, lower the eager buffer
1620 * size to the max MTU (page-aligned).
1621 */
1622 if (!HFI1_CAP_KGET_MASK(rcd->flags, MULTI_PKT_EGR))
1623 rcd->egrbufs.rcvtid_size = round_mtu;
1624
1625 /*
1626 * Eager buffers sizes of 1MB or less require smaller TID sizes
1627 * to satisfy the "multiple of 8 RcvArray entries" requirement.
1628 */
1629 if (rcd->egrbufs.size <= (1 << 20))
1630 rcd->egrbufs.rcvtid_size = max((unsigned long)round_mtu,
1631 rounddown_pow_of_two(rcd->egrbufs.size / 8));
1632
1633 while (alloced_bytes < rcd->egrbufs.size &&
1634 rcd->egrbufs.alloced < rcd->egrbufs.count) {
1635 rcd->egrbufs.buffers[idx].addr =
1636 dma_zalloc_coherent(&dd->pcidev->dev,
1637 rcd->egrbufs.rcvtid_size,
1638 &rcd->egrbufs.buffers[idx].phys,
1639 gfp_flags);
1640 if (rcd->egrbufs.buffers[idx].addr) {
1641 rcd->egrbufs.buffers[idx].len =
1642 rcd->egrbufs.rcvtid_size;
1643 rcd->egrbufs.rcvtids[rcd->egrbufs.alloced].addr =
1644 rcd->egrbufs.buffers[idx].addr;
1645 rcd->egrbufs.rcvtids[rcd->egrbufs.alloced].phys =
1646 rcd->egrbufs.buffers[idx].phys;
1647 rcd->egrbufs.alloced++;
1648 alloced_bytes += rcd->egrbufs.rcvtid_size;
1649 idx++;
1650 } else {
1651 u32 new_size, i, j;
1652 u64 offset = 0;
1653
1654 /*
1655 * Fail the eager buffer allocation if:
1656 * - we are already using the lowest acceptable size
1657 * - we are using one-pkt-per-egr-buffer (this implies
1658 * that we are accepting only one size)
1659 */
1660 if (rcd->egrbufs.rcvtid_size == round_mtu ||
1661 !HFI1_CAP_KGET_MASK(rcd->flags, MULTI_PKT_EGR)) {
1662 dd_dev_err(dd, "ctxt%u: Failed to allocate eager buffers\n",
1663 rcd->ctxt);
1664 goto bail_rcvegrbuf_phys;
1665 }
1666
1667 new_size = rcd->egrbufs.rcvtid_size / 2;
1668
1669 /*
1670 * If the first attempt to allocate memory failed, don't
1671 * fail everything but continue with the next lower
1672 * size.
1673 */
1674 if (idx == 0) {
1675 rcd->egrbufs.rcvtid_size = new_size;
1676 continue;
1677 }
1678
1679 /*
1680 * Re-partition already allocated buffers to a smaller
1681 * size.
1682 */
1683 rcd->egrbufs.alloced = 0;
1684 for (i = 0, j = 0, offset = 0; j < idx; i++) {
1685 if (i >= rcd->egrbufs.count)
1686 break;
1687 rcd->egrbufs.rcvtids[i].phys =
1688 rcd->egrbufs.buffers[j].phys + offset;
1689 rcd->egrbufs.rcvtids[i].addr =
1690 rcd->egrbufs.buffers[j].addr + offset;
1691 rcd->egrbufs.alloced++;
1692 if ((rcd->egrbufs.buffers[j].phys + offset +
1693 new_size) ==
1694 (rcd->egrbufs.buffers[j].phys +
1695 rcd->egrbufs.buffers[j].len)) {
1696 j++;
1697 offset = 0;
1698 } else
1699 offset += new_size;
1700 }
1701 rcd->egrbufs.rcvtid_size = new_size;
1702 }
1703 }
1704 rcd->egrbufs.numbufs = idx;
1705 rcd->egrbufs.size = alloced_bytes;
1706
Sebastian Sanchez6c63e422015-11-06 20:06:56 -05001707 hfi1_cdbg(PROC,
1708 "ctxt%u: Alloced %u rcv tid entries @ %uKB, total %zuKB\n",
1709 rcd->ctxt, rcd->egrbufs.alloced, rcd->egrbufs.rcvtid_size,
1710 rcd->egrbufs.size);
1711
Mike Marciniszyn77241052015-07-30 15:17:43 -04001712
1713 /*
1714 * Set the contexts rcv array head update threshold to the closest
1715 * power of 2 (so we can use a mask instead of modulo) below half
1716 * the allocated entries.
1717 */
1718 rcd->egrbufs.threshold =
1719 rounddown_pow_of_two(rcd->egrbufs.alloced / 2);
1720 /*
1721 * Compute the expected RcvArray entry base. This is done after
1722 * allocating the eager buffers in order to maximize the
1723 * expected RcvArray entries for the context.
1724 */
1725 max_entries = rcd->rcv_array_groups * dd->rcv_entries.group_size;
1726 egrtop = roundup(rcd->egrbufs.alloced, dd->rcv_entries.group_size);
1727 rcd->expected_count = max_entries - egrtop;
1728 if (rcd->expected_count > MAX_TID_PAIR_ENTRIES * 2)
1729 rcd->expected_count = MAX_TID_PAIR_ENTRIES * 2;
1730
1731 rcd->expected_base = rcd->eager_base + egrtop;
Sebastian Sanchez6c63e422015-11-06 20:06:56 -05001732 hfi1_cdbg(PROC, "ctxt%u: eager:%u, exp:%u, egrbase:%u, expbase:%u\n",
1733 rcd->ctxt, rcd->egrbufs.alloced, rcd->expected_count,
1734 rcd->eager_base, rcd->expected_base);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001735
1736 if (!hfi1_rcvbuf_validate(rcd->egrbufs.rcvtid_size, PT_EAGER, &order)) {
Sebastian Sanchez6c63e422015-11-06 20:06:56 -05001737 hfi1_cdbg(PROC,
1738 "ctxt%u: current Eager buffer size is invalid %u\n",
1739 rcd->ctxt, rcd->egrbufs.rcvtid_size);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001740 ret = -EINVAL;
1741 goto bail;
1742 }
1743
1744 for (idx = 0; idx < rcd->egrbufs.alloced; idx++) {
1745 hfi1_put_tid(dd, rcd->eager_base + idx, PT_EAGER,
1746 rcd->egrbufs.rcvtids[idx].phys, order);
1747 cond_resched();
1748 }
1749 goto bail;
1750
1751bail_rcvegrbuf_phys:
1752 for (idx = 0; idx < rcd->egrbufs.alloced &&
1753 rcd->egrbufs.buffers[idx].addr;
1754 idx++) {
1755 dma_free_coherent(&dd->pcidev->dev,
1756 rcd->egrbufs.buffers[idx].len,
1757 rcd->egrbufs.buffers[idx].addr,
1758 rcd->egrbufs.buffers[idx].phys);
1759 rcd->egrbufs.buffers[idx].addr = NULL;
1760 rcd->egrbufs.buffers[idx].phys = 0;
1761 rcd->egrbufs.buffers[idx].len = 0;
1762 }
1763bail:
1764 return ret;
1765}