blob: 354935f20437a32c5fdc61ca0c9b22ec71709f24 [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);
Easwar Hariharan8ebd4cf2016-02-03 14:31:14 -0800503 INIT_WORK(&ppd->qsfp_info.qsfp_work, qsfp_event);
504
Mike Marciniszyn77241052015-07-30 15:17:43 -0400505 mutex_init(&ppd->hls_lock);
506 spin_lock_init(&ppd->sdma_alllock);
507 spin_lock_init(&ppd->qsfp_info.qsfp_lock);
508
Easwar Hariharan8ebd4cf2016-02-03 14:31:14 -0800509 ppd->qsfp_info.ppd = ppd;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400510 ppd->sm_trap_qp = 0x0;
511 ppd->sa_qp = 0x1;
512
513 ppd->hfi1_wq = NULL;
514
515 spin_lock_init(&ppd->cca_timer_lock);
516
517 for (i = 0; i < OPA_MAX_SLS; i++) {
518 hrtimer_init(&ppd->cca_timer[i].hrtimer, CLOCK_MONOTONIC,
519 HRTIMER_MODE_REL);
520 ppd->cca_timer[i].ppd = ppd;
521 ppd->cca_timer[i].sl = i;
522 ppd->cca_timer[i].ccti = 0;
523 ppd->cca_timer[i].hrtimer.function = cca_timer_fn;
524 }
525
526 ppd->cc_max_table_entries = IB_CC_TABLE_CAP_DEFAULT;
527
528 spin_lock_init(&ppd->cc_state_lock);
529 spin_lock_init(&ppd->cc_log_lock);
530 size = sizeof(struct cc_state);
531 RCU_INIT_POINTER(ppd->cc_state, kzalloc(size, GFP_KERNEL));
532 if (!rcu_dereference(ppd->cc_state))
533 goto bail;
534 return;
535
536bail:
537
538 hfi1_early_err(&pdev->dev,
539 "Congestion Control Agent disabled for port %d\n", port);
540}
541
542/*
543 * Do initialization for device that is only needed on
544 * first detect, not on resets.
545 */
546static int loadtime_init(struct hfi1_devdata *dd)
547{
548 return 0;
549}
550
551/**
552 * init_after_reset - re-initialize after a reset
553 * @dd: the hfi1_ib device
554 *
555 * sanity check at least some of the values after reset, and
556 * ensure no receive or transmit (explicitly, in case reset
557 * failed
558 */
559static int init_after_reset(struct hfi1_devdata *dd)
560{
561 int i;
562
563 /*
564 * Ensure chip does no sends or receives, tail updates, or
565 * pioavail updates while we re-initialize. This is mostly
566 * for the driver data structures, not chip registers.
567 */
568 for (i = 0; i < dd->num_rcv_contexts; i++)
569 hfi1_rcvctrl(dd, HFI1_RCVCTRL_CTXT_DIS |
570 HFI1_RCVCTRL_INTRAVAIL_DIS |
571 HFI1_RCVCTRL_TAILUPD_DIS, i);
572 pio_send_control(dd, PSC_GLOBAL_DISABLE);
573 for (i = 0; i < dd->num_send_contexts; i++)
574 sc_disable(dd->send_contexts[i].sc);
575
576 return 0;
577}
578
579static void enable_chip(struct hfi1_devdata *dd)
580{
581 u32 rcvmask;
582 u32 i;
583
584 /* enable PIO send */
585 pio_send_control(dd, PSC_GLOBAL_ENABLE);
586
587 /*
588 * Enable kernel ctxts' receive and receive interrupt.
589 * Other ctxts done as user opens and initializes them.
590 */
591 rcvmask = HFI1_RCVCTRL_CTXT_ENB | HFI1_RCVCTRL_INTRAVAIL_ENB;
592 for (i = 0; i < dd->first_user_ctxt; ++i) {
593 rcvmask |= HFI1_CAP_KGET_MASK(dd->rcd[i]->flags, DMA_RTAIL) ?
594 HFI1_RCVCTRL_TAILUPD_ENB : HFI1_RCVCTRL_TAILUPD_DIS;
595 if (!HFI1_CAP_KGET_MASK(dd->rcd[i]->flags, MULTI_PKT_EGR))
596 rcvmask |= HFI1_RCVCTRL_ONE_PKT_EGR_ENB;
597 if (HFI1_CAP_KGET_MASK(dd->rcd[i]->flags, NODROP_RHQ_FULL))
598 rcvmask |= HFI1_RCVCTRL_NO_RHQ_DROP_ENB;
599 if (HFI1_CAP_KGET_MASK(dd->rcd[i]->flags, NODROP_EGR_FULL))
600 rcvmask |= HFI1_RCVCTRL_NO_EGR_DROP_ENB;
601 hfi1_rcvctrl(dd, rcvmask, i);
602 sc_enable(dd->rcd[i]->sc);
603 }
604}
605
606/**
607 * create_workqueues - create per port workqueues
608 * @dd: the hfi1_ib device
609 */
610static int create_workqueues(struct hfi1_devdata *dd)
611{
612 int pidx;
613 struct hfi1_pportdata *ppd;
614
615 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
616 ppd = dd->pport + pidx;
617 if (!ppd->hfi1_wq) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400618 ppd->hfi1_wq =
Mike Marciniszyn0a226ed2015-11-09 19:13:58 -0500619 alloc_workqueue(
620 "hfi%d_%d",
621 WQ_SYSFS | WQ_HIGHPRI | WQ_CPU_INTENSIVE,
622 dd->num_sdma,
623 dd->unit, pidx);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400624 if (!ppd->hfi1_wq)
625 goto wq_error;
626 }
627 }
628 return 0;
629wq_error:
Mike Marciniszyn0a226ed2015-11-09 19:13:58 -0500630 pr_err("alloc_workqueue failed for port %d\n", pidx + 1);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400631 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
632 ppd = dd->pport + pidx;
633 if (ppd->hfi1_wq) {
634 destroy_workqueue(ppd->hfi1_wq);
635 ppd->hfi1_wq = NULL;
636 }
637 }
638 return -ENOMEM;
639}
640
641/**
642 * hfi1_init - do the actual initialization sequence on the chip
643 * @dd: the hfi1_ib device
644 * @reinit: re-initializing, so don't allocate new memory
645 *
646 * Do the actual initialization sequence on the chip. This is done
647 * both from the init routine called from the PCI infrastructure, and
648 * when we reset the chip, or detect that it was reset internally,
649 * or it's administratively re-enabled.
650 *
651 * Memory allocation here and in called routines is only done in
652 * the first case (reinit == 0). We have to be careful, because even
653 * without memory allocation, we need to re-write all the chip registers
654 * TIDs, etc. after the reset or enable has completed.
655 */
656int hfi1_init(struct hfi1_devdata *dd, int reinit)
657{
658 int ret = 0, pidx, lastfail = 0;
659 unsigned i, len;
660 struct hfi1_ctxtdata *rcd;
661 struct hfi1_pportdata *ppd;
662
663 /* Set up recv low level handlers */
664 dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_EXPECTED] =
665 kdeth_process_expected;
666 dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_EAGER] =
667 kdeth_process_eager;
668 dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_IB] = process_receive_ib;
669 dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_ERROR] =
670 process_receive_error;
671 dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_BYPASS] =
672 process_receive_bypass;
673 dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_INVALID5] =
674 process_receive_invalid;
675 dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_INVALID6] =
676 process_receive_invalid;
677 dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_INVALID7] =
678 process_receive_invalid;
679 dd->rhf_rcv_function_map = dd->normal_rhf_rcv_functions;
680
681 /* Set up send low level handlers */
682 dd->process_pio_send = hfi1_verbs_send_pio;
683 dd->process_dma_send = hfi1_verbs_send_dma;
684 dd->pio_inline_send = pio_copy;
685
Mike Marciniszyn995deaf2015-11-16 21:59:29 -0500686 if (is_ax(dd)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400687 atomic_set(&dd->drop_packet, DROP_PACKET_ON);
688 dd->do_drop = 1;
689 } else {
690 atomic_set(&dd->drop_packet, DROP_PACKET_OFF);
691 dd->do_drop = 0;
692 }
693
694 /* make sure the link is not "up" */
695 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
696 ppd = dd->pport + pidx;
697 ppd->linkup = 0;
698 }
699
700 if (reinit)
701 ret = init_after_reset(dd);
702 else
703 ret = loadtime_init(dd);
704 if (ret)
705 goto done;
706
Mark F. Brown46b010d2015-11-09 19:18:20 -0500707 /* allocate dummy tail memory for all receive contexts */
708 dd->rcvhdrtail_dummy_kvaddr = dma_zalloc_coherent(
709 &dd->pcidev->dev, sizeof(u64),
710 &dd->rcvhdrtail_dummy_physaddr,
711 GFP_KERNEL);
712
713 if (!dd->rcvhdrtail_dummy_kvaddr) {
714 dd_dev_err(dd, "cannot allocate dummy tail memory\n");
715 ret = -ENOMEM;
716 goto done;
717 }
718
Mike Marciniszyn77241052015-07-30 15:17:43 -0400719 /* dd->rcd can be NULL if early initialization failed */
720 for (i = 0; dd->rcd && i < dd->first_user_ctxt; ++i) {
721 /*
722 * Set up the (kernel) rcvhdr queue and egr TIDs. If doing
723 * re-init, the simplest way to handle this is to free
724 * existing, and re-allocate.
725 * Need to re-create rest of ctxt 0 ctxtdata as well.
726 */
727 rcd = dd->rcd[i];
728 if (!rcd)
729 continue;
730
731 rcd->do_interrupt = &handle_receive_interrupt;
732
733 lastfail = hfi1_create_rcvhdrq(dd, rcd);
734 if (!lastfail)
735 lastfail = hfi1_setup_eagerbufs(rcd);
736 if (lastfail)
737 dd_dev_err(dd,
738 "failed to allocate kernel ctxt's rcvhdrq and/or egr bufs\n");
739 }
740 if (lastfail)
741 ret = lastfail;
742
743 /* Allocate enough memory for user event notification. */
744 len = ALIGN(dd->chip_rcv_contexts * HFI1_MAX_SHARED_CTXTS *
745 sizeof(*dd->events), PAGE_SIZE);
746 dd->events = vmalloc_user(len);
747 if (!dd->events)
748 dd_dev_err(dd, "Failed to allocate user events page\n");
749 /*
750 * Allocate a page for device and port status.
751 * Page will be shared amongst all user processes.
752 */
753 dd->status = vmalloc_user(PAGE_SIZE);
754 if (!dd->status)
755 dd_dev_err(dd, "Failed to allocate dev status page\n");
756 else
757 dd->freezelen = PAGE_SIZE - (sizeof(*dd->status) -
758 sizeof(dd->status->freezemsg));
759 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
760 ppd = dd->pport + pidx;
761 if (dd->status)
762 /* Currently, we only have one port */
763 ppd->statusp = &dd->status->port;
764
765 set_mtu(ppd);
766 }
767
768 /* enable chip even if we have an error, so we can debug cause */
769 enable_chip(dd);
770
Mike Marciniszyn77241052015-07-30 15:17:43 -0400771done:
772 /*
773 * Set status even if port serdes is not initialized
774 * so that diags will work.
775 */
776 if (dd->status)
777 dd->status->dev |= HFI1_STATUS_CHIP_PRESENT |
778 HFI1_STATUS_INITTED;
779 if (!ret) {
780 /* enable all interrupts from the chip */
781 set_intr_state(dd, 1);
782
783 /* chip is OK for user apps; mark it as initialized */
784 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
785 ppd = dd->pport + pidx;
786
Mike Marciniszyn77241052015-07-30 15:17:43 -0400787 /* start the serdes - must be after interrupts are
788 enabled so we are notified when the link goes up */
789 lastfail = bringup_serdes(ppd);
790 if (lastfail)
791 dd_dev_info(dd,
792 "Failed to bring up port %u\n",
793 ppd->port);
794
795 /*
796 * Set status even if port serdes is not initialized
797 * so that diags will work.
798 */
799 if (ppd->statusp)
800 *ppd->statusp |= HFI1_STATUS_CHIP_PRESENT |
801 HFI1_STATUS_INITTED;
802 if (!ppd->link_speed_enabled)
803 continue;
804 }
805 }
806
807 /* if ret is non-zero, we probably should do some cleanup here... */
808 return ret;
809}
810
811static inline struct hfi1_devdata *__hfi1_lookup(int unit)
812{
813 return idr_find(&hfi1_unit_table, unit);
814}
815
816struct hfi1_devdata *hfi1_lookup(int unit)
817{
818 struct hfi1_devdata *dd;
819 unsigned long flags;
820
821 spin_lock_irqsave(&hfi1_devs_lock, flags);
822 dd = __hfi1_lookup(unit);
823 spin_unlock_irqrestore(&hfi1_devs_lock, flags);
824
825 return dd;
826}
827
828/*
829 * Stop the timers during unit shutdown, or after an error late
830 * in initialization.
831 */
832static void stop_timers(struct hfi1_devdata *dd)
833{
834 struct hfi1_pportdata *ppd;
835 int pidx;
836
837 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
838 ppd = dd->pport + pidx;
839 if (ppd->led_override_timer.data) {
840 del_timer_sync(&ppd->led_override_timer);
841 atomic_set(&ppd->led_override_timer_active, 0);
842 }
843 }
844}
845
846/**
847 * shutdown_device - shut down a device
848 * @dd: the hfi1_ib device
849 *
850 * This is called to make the device quiet when we are about to
851 * unload the driver, and also when the device is administratively
852 * disabled. It does not free any data structures.
853 * Everything it does has to be setup again by hfi1_init(dd, 1)
854 */
855static void shutdown_device(struct hfi1_devdata *dd)
856{
857 struct hfi1_pportdata *ppd;
858 unsigned pidx;
859 int i;
860
861 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
862 ppd = dd->pport + pidx;
863
864 ppd->linkup = 0;
865 if (ppd->statusp)
866 *ppd->statusp &= ~(HFI1_STATUS_IB_CONF |
867 HFI1_STATUS_IB_READY);
868 }
869 dd->flags &= ~HFI1_INITTED;
870
871 /* mask interrupts, but not errors */
872 set_intr_state(dd, 0);
873
874 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
875 ppd = dd->pport + pidx;
876 for (i = 0; i < dd->num_rcv_contexts; i++)
877 hfi1_rcvctrl(dd, HFI1_RCVCTRL_TAILUPD_DIS |
878 HFI1_RCVCTRL_CTXT_DIS |
879 HFI1_RCVCTRL_INTRAVAIL_DIS |
880 HFI1_RCVCTRL_PKEY_DIS |
881 HFI1_RCVCTRL_ONE_PKT_EGR_DIS, i);
882 /*
883 * Gracefully stop all sends allowing any in progress to
884 * trickle out first.
885 */
886 for (i = 0; i < dd->num_send_contexts; i++)
887 sc_flush(dd->send_contexts[i].sc);
888 }
889
890 /*
891 * Enough for anything that's going to trickle out to have actually
892 * done so.
893 */
894 udelay(20);
895
896 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
897 ppd = dd->pport + pidx;
898
899 /* disable all contexts */
900 for (i = 0; i < dd->num_send_contexts; i++)
901 sc_disable(dd->send_contexts[i].sc);
902 /* disable the send device */
903 pio_send_control(dd, PSC_GLOBAL_DISABLE);
904
905 /*
906 * Clear SerdesEnable.
907 * We can't count on interrupts since we are stopping.
908 */
909 hfi1_quiet_serdes(ppd);
910
911 if (ppd->hfi1_wq) {
912 destroy_workqueue(ppd->hfi1_wq);
913 ppd->hfi1_wq = NULL;
914 }
915 }
916 sdma_exit(dd);
917}
918
919/**
920 * hfi1_free_ctxtdata - free a context's allocated data
921 * @dd: the hfi1_ib device
922 * @rcd: the ctxtdata structure
923 *
924 * free up any allocated data for a context
925 * This should not touch anything that would affect a simultaneous
926 * re-allocation of context data, because it is called after hfi1_mutex
927 * is released (and can be called from reinit as well).
928 * It should never change any chip state, or global driver state.
929 */
930void hfi1_free_ctxtdata(struct hfi1_devdata *dd, struct hfi1_ctxtdata *rcd)
931{
932 unsigned e;
933
934 if (!rcd)
935 return;
936
937 if (rcd->rcvhdrq) {
938 dma_free_coherent(&dd->pcidev->dev, rcd->rcvhdrq_size,
939 rcd->rcvhdrq, rcd->rcvhdrq_phys);
940 rcd->rcvhdrq = NULL;
941 if (rcd->rcvhdrtail_kvaddr) {
942 dma_free_coherent(&dd->pcidev->dev, PAGE_SIZE,
943 (void *)rcd->rcvhdrtail_kvaddr,
944 rcd->rcvhdrqtailaddr_phys);
945 rcd->rcvhdrtail_kvaddr = NULL;
946 }
947 }
948
949 /* all the RcvArray entries should have been cleared by now */
950 kfree(rcd->egrbufs.rcvtids);
951
952 for (e = 0; e < rcd->egrbufs.alloced; e++) {
953 if (rcd->egrbufs.buffers[e].phys)
954 dma_free_coherent(&dd->pcidev->dev,
955 rcd->egrbufs.buffers[e].len,
956 rcd->egrbufs.buffers[e].addr,
957 rcd->egrbufs.buffers[e].phys);
958 }
959 kfree(rcd->egrbufs.buffers);
960
961 sc_free(rcd->sc);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400962 vfree(rcd->user_event_mask);
963 vfree(rcd->subctxt_uregbase);
964 vfree(rcd->subctxt_rcvegrbuf);
965 vfree(rcd->subctxt_rcvhdr_base);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400966 kfree(rcd->opstats);
967 kfree(rcd);
968}
969
970void hfi1_free_devdata(struct hfi1_devdata *dd)
971{
972 unsigned long flags;
973
974 spin_lock_irqsave(&hfi1_devs_lock, flags);
975 idr_remove(&hfi1_unit_table, dd->unit);
976 list_del(&dd->list);
977 spin_unlock_irqrestore(&hfi1_devs_lock, flags);
978 hfi1_dbg_ibdev_exit(&dd->verbs_dev);
979 rcu_barrier(); /* wait for rcu callbacks to complete */
980 free_percpu(dd->int_counter);
981 free_percpu(dd->rcv_limit);
Dennis Dalessandroec3f2c12016-01-19 14:41:33 -0800982 ib_dealloc_device(&dd->verbs_dev.rdi.ibdev);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400983}
984
985/*
986 * Allocate our primary per-unit data structure. Must be done via verbs
987 * allocator, because the verbs cleanup process both does cleanup and
988 * free of the data structure.
989 * "extra" is for chip-specific data.
990 *
991 * Use the idr mechanism to get a unit number for this unit.
992 */
993struct hfi1_devdata *hfi1_alloc_devdata(struct pci_dev *pdev, size_t extra)
994{
995 unsigned long flags;
996 struct hfi1_devdata *dd;
Dennis Dalessandro7af6d002016-01-19 14:44:06 -0800997 int ret, nports;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400998
Dennis Dalessandro7af6d002016-01-19 14:44:06 -0800999 /* extra is * number of ports */
1000 nports = extra / sizeof(struct hfi1_pportdata);
1001
1002 dd = (struct hfi1_devdata *)rvt_alloc_device(sizeof(*dd) + extra,
1003 nports);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001004 if (!dd)
1005 return ERR_PTR(-ENOMEM);
Dennis Dalessandro7af6d002016-01-19 14:44:06 -08001006 dd->num_pports = nports;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001007 dd->pport = (struct hfi1_pportdata *)(dd + 1);
1008
1009 INIT_LIST_HEAD(&dd->list);
1010 dd->node = dev_to_node(&pdev->dev);
1011 if (dd->node < 0)
1012 dd->node = 0;
1013 idr_preload(GFP_KERNEL);
1014 spin_lock_irqsave(&hfi1_devs_lock, flags);
1015
1016 ret = idr_alloc(&hfi1_unit_table, dd, 0, 0, GFP_NOWAIT);
1017 if (ret >= 0) {
1018 dd->unit = ret;
1019 list_add(&dd->list, &hfi1_dev_list);
1020 }
1021
1022 spin_unlock_irqrestore(&hfi1_devs_lock, flags);
1023 idr_preload_end();
1024
1025 if (ret < 0) {
1026 hfi1_early_err(&pdev->dev,
1027 "Could not allocate unit ID: error %d\n", -ret);
1028 goto bail;
1029 }
1030 /*
1031 * Initialize all locks for the device. This needs to be as early as
1032 * possible so locks are usable.
1033 */
1034 spin_lock_init(&dd->sc_lock);
1035 spin_lock_init(&dd->sendctrl_lock);
1036 spin_lock_init(&dd->rcvctrl_lock);
1037 spin_lock_init(&dd->uctxt_lock);
1038 spin_lock_init(&dd->hfi1_diag_trans_lock);
1039 spin_lock_init(&dd->sc_init_lock);
1040 spin_lock_init(&dd->dc8051_lock);
1041 spin_lock_init(&dd->dc8051_memlock);
1042 mutex_init(&dd->qsfp_i2c_mutex);
1043 seqlock_init(&dd->sc2vl_lock);
1044 spin_lock_init(&dd->sde_map_lock);
1045 init_waitqueue_head(&dd->event_queue);
1046
1047 dd->int_counter = alloc_percpu(u64);
1048 if (!dd->int_counter) {
1049 ret = -ENOMEM;
1050 hfi1_early_err(&pdev->dev,
1051 "Could not allocate per-cpu int_counter\n");
1052 goto bail;
1053 }
1054
1055 dd->rcv_limit = alloc_percpu(u64);
1056 if (!dd->rcv_limit) {
1057 ret = -ENOMEM;
1058 hfi1_early_err(&pdev->dev,
1059 "Could not allocate per-cpu rcv_limit\n");
1060 goto bail;
1061 }
1062
1063 if (!hfi1_cpulist_count) {
1064 u32 count = num_online_cpus();
1065
Shraddha Barke314fcc02015-10-09 21:03:26 +05301066 hfi1_cpulist = kcalloc(BITS_TO_LONGS(count), sizeof(long),
1067 GFP_KERNEL);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001068 if (hfi1_cpulist)
1069 hfi1_cpulist_count = count;
1070 else
1071 hfi1_early_err(
1072 &pdev->dev,
1073 "Could not alloc cpulist info, cpu affinity might be wrong\n");
1074 }
1075 hfi1_dbg_ibdev_init(&dd->verbs_dev);
1076 return dd;
1077
1078bail:
1079 if (!list_empty(&dd->list))
1080 list_del_init(&dd->list);
Dennis Dalessandroec3f2c12016-01-19 14:41:33 -08001081 ib_dealloc_device(&dd->verbs_dev.rdi.ibdev);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001082 return ERR_PTR(ret);
1083}
1084
1085/*
1086 * Called from freeze mode handlers, and from PCI error
1087 * reporting code. Should be paranoid about state of
1088 * system and data structures.
1089 */
1090void hfi1_disable_after_error(struct hfi1_devdata *dd)
1091{
1092 if (dd->flags & HFI1_INITTED) {
1093 u32 pidx;
1094
1095 dd->flags &= ~HFI1_INITTED;
1096 if (dd->pport)
1097 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
1098 struct hfi1_pportdata *ppd;
1099
1100 ppd = dd->pport + pidx;
1101 if (dd->flags & HFI1_PRESENT)
1102 set_link_state(ppd, HLS_DN_DISABLE);
1103
1104 if (ppd->statusp)
1105 *ppd->statusp &= ~HFI1_STATUS_IB_READY;
1106 }
1107 }
1108
1109 /*
1110 * Mark as having had an error for driver, and also
1111 * for /sys and status word mapped to user programs.
1112 * This marks unit as not usable, until reset.
1113 */
1114 if (dd->status)
1115 dd->status->dev |= HFI1_STATUS_HWERROR;
1116}
1117
1118static void remove_one(struct pci_dev *);
1119static int init_one(struct pci_dev *, const struct pci_device_id *);
1120
1121#define DRIVER_LOAD_MSG "Intel " DRIVER_NAME " loaded: "
1122#define PFX DRIVER_NAME ": "
1123
1124static const struct pci_device_id hfi1_pci_tbl[] = {
1125 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL0) },
1126 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL1) },
1127 { 0, }
1128};
1129
1130MODULE_DEVICE_TABLE(pci, hfi1_pci_tbl);
1131
1132static struct pci_driver hfi1_pci_driver = {
1133 .name = DRIVER_NAME,
1134 .probe = init_one,
1135 .remove = remove_one,
1136 .id_table = hfi1_pci_tbl,
1137 .err_handler = &hfi1_pci_err_handler,
1138};
1139
1140static void __init compute_krcvqs(void)
1141{
1142 int i;
1143
1144 for (i = 0; i < krcvqsset; i++)
1145 n_krcvqs += krcvqs[i];
1146}
1147
1148/*
1149 * Do all the generic driver unit- and chip-independent memory
1150 * allocation and initialization.
1151 */
1152static int __init hfi1_mod_init(void)
1153{
1154 int ret;
1155
1156 ret = dev_init();
1157 if (ret)
1158 goto bail;
1159
1160 /* validate max MTU before any devices start */
1161 if (!valid_opa_max_mtu(hfi1_max_mtu)) {
1162 pr_err("Invalid max_mtu 0x%x, using 0x%x instead\n",
1163 hfi1_max_mtu, HFI1_DEFAULT_MAX_MTU);
1164 hfi1_max_mtu = HFI1_DEFAULT_MAX_MTU;
1165 }
1166 /* valid CUs run from 1-128 in powers of 2 */
1167 if (hfi1_cu > 128 || !is_power_of_2(hfi1_cu))
1168 hfi1_cu = 1;
1169 /* valid credit return threshold is 0-100, variable is unsigned */
1170 if (user_credit_return_threshold > 100)
1171 user_credit_return_threshold = 100;
1172
1173 compute_krcvqs();
1174 /* sanitize receive interrupt count, time must wait until after
1175 the hardware type is known */
1176 if (rcv_intr_count > RCV_HDR_HEAD_COUNTER_MASK)
1177 rcv_intr_count = RCV_HDR_HEAD_COUNTER_MASK;
1178 /* reject invalid combinations */
1179 if (rcv_intr_count == 0 && rcv_intr_timeout == 0) {
1180 pr_err("Invalid mode: both receive interrupt count and available timeout are zero - setting interrupt count to 1\n");
1181 rcv_intr_count = 1;
1182 }
1183 if (rcv_intr_count > 1 && rcv_intr_timeout == 0) {
1184 /*
1185 * Avoid indefinite packet delivery by requiring a timeout
1186 * if count is > 1.
1187 */
1188 pr_err("Invalid mode: receive interrupt count greater than 1 and available timeout is zero - setting available timeout to 1\n");
1189 rcv_intr_timeout = 1;
1190 }
1191 if (rcv_intr_dynamic && !(rcv_intr_count > 1 && rcv_intr_timeout > 0)) {
1192 /*
1193 * The dynamic algorithm expects a non-zero timeout
1194 * and a count > 1.
1195 */
1196 pr_err("Invalid mode: dynamic receive interrupt mitigation with invalid count and timeout - turning dynamic off\n");
1197 rcv_intr_dynamic = 0;
1198 }
1199
1200 /* sanitize link CRC options */
1201 link_crc_mask &= SUPPORTED_CRCS;
1202
1203 /*
1204 * These must be called before the driver is registered with
1205 * the PCI subsystem.
1206 */
1207 idr_init(&hfi1_unit_table);
1208
1209 hfi1_dbg_init();
1210 ret = pci_register_driver(&hfi1_pci_driver);
1211 if (ret < 0) {
1212 pr_err("Unable to register driver: error %d\n", -ret);
1213 goto bail_dev;
1214 }
1215 goto bail; /* all OK */
1216
1217bail_dev:
1218 hfi1_dbg_exit();
1219 idr_destroy(&hfi1_unit_table);
1220 dev_cleanup();
1221bail:
1222 return ret;
1223}
1224
1225module_init(hfi1_mod_init);
1226
1227/*
1228 * Do the non-unit driver cleanup, memory free, etc. at unload.
1229 */
1230static void __exit hfi1_mod_cleanup(void)
1231{
1232 pci_unregister_driver(&hfi1_pci_driver);
1233 hfi1_dbg_exit();
1234 hfi1_cpulist_count = 0;
1235 kfree(hfi1_cpulist);
1236
1237 idr_destroy(&hfi1_unit_table);
1238 dispose_firmware(); /* asymmetric with obtain_firmware() */
1239 dev_cleanup();
1240}
1241
1242module_exit(hfi1_mod_cleanup);
1243
1244/* this can only be called after a successful initialization */
1245static void cleanup_device_data(struct hfi1_devdata *dd)
1246{
1247 int ctxt;
1248 int pidx;
1249 struct hfi1_ctxtdata **tmp;
1250 unsigned long flags;
1251
1252 /* users can't do anything more with chip */
1253 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
1254 struct hfi1_pportdata *ppd = &dd->pport[pidx];
1255 struct cc_state *cc_state;
1256 int i;
1257
1258 if (ppd->statusp)
1259 *ppd->statusp &= ~HFI1_STATUS_CHIP_PRESENT;
1260
1261 for (i = 0; i < OPA_MAX_SLS; i++)
1262 hrtimer_cancel(&ppd->cca_timer[i].hrtimer);
1263
1264 spin_lock(&ppd->cc_state_lock);
1265 cc_state = get_cc_state(ppd);
1266 rcu_assign_pointer(ppd->cc_state, NULL);
1267 spin_unlock(&ppd->cc_state_lock);
1268
1269 if (cc_state)
1270 call_rcu(&cc_state->rcu, cc_state_reclaim);
1271 }
1272
1273 free_credit_return(dd);
1274
1275 /*
1276 * Free any resources still in use (usually just kernel contexts)
1277 * at unload; we do for ctxtcnt, because that's what we allocate.
1278 * We acquire lock to be really paranoid that rcd isn't being
1279 * accessed from some interrupt-related code (that should not happen,
1280 * but best to be sure).
1281 */
1282 spin_lock_irqsave(&dd->uctxt_lock, flags);
1283 tmp = dd->rcd;
1284 dd->rcd = NULL;
1285 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
Mark F. Brown46b010d2015-11-09 19:18:20 -05001286
1287 if (dd->rcvhdrtail_dummy_kvaddr) {
1288 dma_free_coherent(&dd->pcidev->dev, sizeof(u64),
1289 (void *)dd->rcvhdrtail_dummy_kvaddr,
1290 dd->rcvhdrtail_dummy_physaddr);
1291 dd->rcvhdrtail_dummy_kvaddr = NULL;
1292 }
1293
Mike Marciniszyn77241052015-07-30 15:17:43 -04001294 for (ctxt = 0; tmp && ctxt < dd->num_rcv_contexts; ctxt++) {
1295 struct hfi1_ctxtdata *rcd = tmp[ctxt];
1296
1297 tmp[ctxt] = NULL; /* debugging paranoia */
1298 if (rcd) {
1299 hfi1_clear_tids(rcd);
1300 hfi1_free_ctxtdata(dd, rcd);
1301 }
1302 }
1303 kfree(tmp);
1304 /* must follow rcv context free - need to remove rcv's hooks */
1305 for (ctxt = 0; ctxt < dd->num_send_contexts; ctxt++)
1306 sc_free(dd->send_contexts[ctxt].sc);
1307 dd->num_send_contexts = 0;
1308 kfree(dd->send_contexts);
1309 dd->send_contexts = NULL;
1310 kfree(dd->boardname);
1311 vfree(dd->events);
1312 vfree(dd->status);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001313}
1314
1315/*
1316 * Clean up on unit shutdown, or error during unit load after
1317 * successful initialization.
1318 */
1319static void postinit_cleanup(struct hfi1_devdata *dd)
1320{
1321 hfi1_start_cleanup(dd);
1322
1323 hfi1_pcie_ddcleanup(dd);
1324 hfi1_pcie_cleanup(dd->pcidev);
1325
1326 cleanup_device_data(dd);
1327
1328 hfi1_free_devdata(dd);
1329}
1330
1331static int init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
1332{
1333 int ret = 0, j, pidx, initfail;
1334 struct hfi1_devdata *dd = NULL;
Harish Chegondie8597eb2015-12-01 15:38:20 -05001335 struct hfi1_pportdata *ppd;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001336
1337 /* First, lock the non-writable module parameters */
1338 HFI1_CAP_LOCK();
1339
1340 /* Validate some global module parameters */
1341 if (rcvhdrcnt <= HFI1_MIN_HDRQ_EGRBUF_CNT) {
1342 hfi1_early_err(&pdev->dev, "Header queue count too small\n");
1343 ret = -EINVAL;
1344 goto bail;
1345 }
1346 /* use the encoding function as a sanitization check */
1347 if (!encode_rcv_header_entry_size(hfi1_hdrq_entsize)) {
1348 hfi1_early_err(&pdev->dev, "Invalid HdrQ Entry size %u\n",
1349 hfi1_hdrq_entsize);
Sebastian Sanchez07859de2015-12-10 16:02:49 -05001350 ret = -EINVAL;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001351 goto bail;
1352 }
1353
1354 /* The receive eager buffer size must be set before the receive
1355 * contexts are created.
1356 *
1357 * Set the eager buffer size. Validate that it falls in a range
1358 * allowed by the hardware - all powers of 2 between the min and
1359 * max. The maximum valid MTU is within the eager buffer range
1360 * so we do not need to cap the max_mtu by an eager buffer size
1361 * setting.
1362 */
1363 if (eager_buffer_size) {
1364 if (!is_power_of_2(eager_buffer_size))
1365 eager_buffer_size =
1366 roundup_pow_of_two(eager_buffer_size);
1367 eager_buffer_size =
1368 clamp_val(eager_buffer_size,
1369 MIN_EAGER_BUFFER * 8,
1370 MAX_EAGER_BUFFER_TOTAL);
1371 hfi1_early_info(&pdev->dev, "Eager buffer size %u\n",
1372 eager_buffer_size);
1373 } else {
1374 hfi1_early_err(&pdev->dev, "Invalid Eager buffer size of 0\n");
1375 ret = -EINVAL;
1376 goto bail;
1377 }
1378
1379 /* restrict value of hfi1_rcvarr_split */
1380 hfi1_rcvarr_split = clamp_val(hfi1_rcvarr_split, 0, 100);
1381
1382 ret = hfi1_pcie_init(pdev, ent);
1383 if (ret)
1384 goto bail;
1385
1386 /*
1387 * Do device-specific initialization, function table setup, dd
1388 * allocation, etc.
1389 */
1390 switch (ent->device) {
1391 case PCI_DEVICE_ID_INTEL0:
1392 case PCI_DEVICE_ID_INTEL1:
1393 dd = hfi1_init_dd(pdev, ent);
1394 break;
1395 default:
1396 hfi1_early_err(&pdev->dev,
1397 "Failing on unknown Intel deviceid 0x%x\n",
1398 ent->device);
1399 ret = -ENODEV;
1400 }
1401
1402 if (IS_ERR(dd))
1403 ret = PTR_ERR(dd);
1404 if (ret)
1405 goto clean_bail; /* error already printed */
1406
1407 ret = create_workqueues(dd);
1408 if (ret)
1409 goto clean_bail;
1410
1411 /* do the generic initialization */
1412 initfail = hfi1_init(dd, 0);
1413
1414 ret = hfi1_register_ib_device(dd);
1415
1416 /*
1417 * Now ready for use. this should be cleared whenever we
1418 * detect a reset, or initiate one. If earlier failure,
1419 * we still create devices, so diags, etc. can be used
1420 * to determine cause of problem.
1421 */
1422 if (!initfail && !ret)
1423 dd->flags |= HFI1_INITTED;
1424
1425 j = hfi1_device_create(dd);
1426 if (j)
1427 dd_dev_err(dd, "Failed to create /dev devices: %d\n", -j);
1428
1429 if (initfail || ret) {
1430 stop_timers(dd);
1431 flush_workqueue(ib_wq);
Harish Chegondie8597eb2015-12-01 15:38:20 -05001432 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001433 hfi1_quiet_serdes(dd->pport + pidx);
Harish Chegondie8597eb2015-12-01 15:38:20 -05001434 ppd = dd->pport + pidx;
1435 if (ppd->hfi1_wq) {
1436 destroy_workqueue(ppd->hfi1_wq);
1437 ppd->hfi1_wq = NULL;
1438 }
1439 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001440 if (!j)
1441 hfi1_device_remove(dd);
1442 if (!ret)
1443 hfi1_unregister_ib_device(dd);
1444 postinit_cleanup(dd);
1445 if (initfail)
1446 ret = initfail;
1447 goto bail; /* everything already cleaned */
1448 }
1449
1450 sdma_start(dd);
1451
1452 return 0;
1453
1454clean_bail:
1455 hfi1_pcie_cleanup(pdev);
1456bail:
1457 return ret;
1458}
1459
1460static void remove_one(struct pci_dev *pdev)
1461{
1462 struct hfi1_devdata *dd = pci_get_drvdata(pdev);
1463
1464 /* unregister from IB core */
1465 hfi1_unregister_ib_device(dd);
1466
1467 /*
1468 * Disable the IB link, disable interrupts on the device,
1469 * clear dma engines, etc.
1470 */
1471 shutdown_device(dd);
1472
1473 stop_timers(dd);
1474
1475 /* wait until all of our (qsfp) queue_work() calls complete */
1476 flush_workqueue(ib_wq);
1477
1478 hfi1_device_remove(dd);
1479
1480 postinit_cleanup(dd);
1481}
1482
1483/**
1484 * hfi1_create_rcvhdrq - create a receive header queue
1485 * @dd: the hfi1_ib device
1486 * @rcd: the context data
1487 *
1488 * This must be contiguous memory (from an i/o perspective), and must be
1489 * DMA'able (which means for some systems, it will go through an IOMMU,
1490 * or be forced into a low address range).
1491 */
1492int hfi1_create_rcvhdrq(struct hfi1_devdata *dd, struct hfi1_ctxtdata *rcd)
1493{
1494 unsigned amt;
1495 u64 reg;
1496
1497 if (!rcd->rcvhdrq) {
1498 dma_addr_t phys_hdrqtail;
1499 gfp_t gfp_flags;
1500
1501 /*
1502 * rcvhdrqentsize is in DWs, so we have to convert to bytes
1503 * (* sizeof(u32)).
1504 */
1505 amt = ALIGN(rcd->rcvhdrq_cnt * rcd->rcvhdrqentsize *
1506 sizeof(u32), PAGE_SIZE);
1507
1508 gfp_flags = (rcd->ctxt >= dd->first_user_ctxt) ?
1509 GFP_USER : GFP_KERNEL;
1510 rcd->rcvhdrq = dma_zalloc_coherent(
1511 &dd->pcidev->dev, amt, &rcd->rcvhdrq_phys,
1512 gfp_flags | __GFP_COMP);
1513
1514 if (!rcd->rcvhdrq) {
1515 dd_dev_err(dd,
1516 "attempt to allocate %d bytes for ctxt %u rcvhdrq failed\n",
1517 amt, rcd->ctxt);
1518 goto bail;
1519 }
1520
1521 /* Event mask is per device now and is in hfi1_devdata */
1522 /*if (rcd->ctxt >= dd->first_user_ctxt) {
1523 rcd->user_event_mask = vmalloc_user(PAGE_SIZE);
1524 if (!rcd->user_event_mask)
1525 goto bail_free_hdrq;
1526 }*/
1527
1528 if (HFI1_CAP_KGET_MASK(rcd->flags, DMA_RTAIL)) {
1529 rcd->rcvhdrtail_kvaddr = dma_zalloc_coherent(
1530 &dd->pcidev->dev, PAGE_SIZE, &phys_hdrqtail,
1531 gfp_flags);
1532 if (!rcd->rcvhdrtail_kvaddr)
1533 goto bail_free;
1534 rcd->rcvhdrqtailaddr_phys = phys_hdrqtail;
1535 }
1536
1537 rcd->rcvhdrq_size = amt;
1538 }
1539 /*
1540 * These values are per-context:
1541 * RcvHdrCnt
1542 * RcvHdrEntSize
1543 * RcvHdrSize
1544 */
1545 reg = ((u64)(rcd->rcvhdrq_cnt >> HDRQ_SIZE_SHIFT)
1546 & RCV_HDR_CNT_CNT_MASK)
1547 << RCV_HDR_CNT_CNT_SHIFT;
1548 write_kctxt_csr(dd, rcd->ctxt, RCV_HDR_CNT, reg);
1549 reg = (encode_rcv_header_entry_size(rcd->rcvhdrqentsize)
1550 & RCV_HDR_ENT_SIZE_ENT_SIZE_MASK)
1551 << RCV_HDR_ENT_SIZE_ENT_SIZE_SHIFT;
1552 write_kctxt_csr(dd, rcd->ctxt, RCV_HDR_ENT_SIZE, reg);
1553 reg = (dd->rcvhdrsize & RCV_HDR_SIZE_HDR_SIZE_MASK)
1554 << RCV_HDR_SIZE_HDR_SIZE_SHIFT;
1555 write_kctxt_csr(dd, rcd->ctxt, RCV_HDR_SIZE, reg);
Mark F. Brown46b010d2015-11-09 19:18:20 -05001556
1557 /*
1558 * Program dummy tail address for every receive context
1559 * before enabling any receive context
1560 */
1561 write_kctxt_csr(dd, rcd->ctxt, RCV_HDR_TAIL_ADDR,
1562 dd->rcvhdrtail_dummy_physaddr);
1563
Mike Marciniszyn77241052015-07-30 15:17:43 -04001564 return 0;
1565
1566bail_free:
1567 dd_dev_err(dd,
1568 "attempt to allocate 1 page for ctxt %u rcvhdrqtailaddr failed\n",
1569 rcd->ctxt);
1570 vfree(rcd->user_event_mask);
1571 rcd->user_event_mask = NULL;
1572 dma_free_coherent(&dd->pcidev->dev, amt, rcd->rcvhdrq,
1573 rcd->rcvhdrq_phys);
1574 rcd->rcvhdrq = NULL;
1575bail:
1576 return -ENOMEM;
1577}
1578
1579/**
1580 * allocate eager buffers, both kernel and user contexts.
1581 * @rcd: the context we are setting up.
1582 *
1583 * Allocate the eager TID buffers and program them into hip.
1584 * They are no longer completely contiguous, we do multiple allocation
1585 * calls. Otherwise we get the OOM code involved, by asking for too
1586 * much per call, with disastrous results on some kernels.
1587 */
1588int hfi1_setup_eagerbufs(struct hfi1_ctxtdata *rcd)
1589{
1590 struct hfi1_devdata *dd = rcd->dd;
1591 u32 max_entries, egrtop, alloced_bytes = 0, idx = 0;
1592 gfp_t gfp_flags;
1593 u16 order;
1594 int ret = 0;
1595 u16 round_mtu = roundup_pow_of_two(hfi1_max_mtu);
1596
1597 /*
1598 * GFP_USER, but without GFP_FS, so buffer cache can be
1599 * coalesced (we hope); otherwise, even at order 4,
1600 * heavy filesystem activity makes these fail, and we can
1601 * use compound pages.
1602 */
Mel Gorman71baba42015-11-06 16:28:28 -08001603 gfp_flags = __GFP_RECLAIM | __GFP_IO | __GFP_COMP;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001604
1605 /*
1606 * The minimum size of the eager buffers is a groups of MTU-sized
1607 * buffers.
1608 * The global eager_buffer_size parameter is checked against the
1609 * theoretical lower limit of the value. Here, we check against the
1610 * MTU.
1611 */
1612 if (rcd->egrbufs.size < (round_mtu * dd->rcv_entries.group_size))
1613 rcd->egrbufs.size = round_mtu * dd->rcv_entries.group_size;
1614 /*
1615 * If using one-pkt-per-egr-buffer, lower the eager buffer
1616 * size to the max MTU (page-aligned).
1617 */
1618 if (!HFI1_CAP_KGET_MASK(rcd->flags, MULTI_PKT_EGR))
1619 rcd->egrbufs.rcvtid_size = round_mtu;
1620
1621 /*
1622 * Eager buffers sizes of 1MB or less require smaller TID sizes
1623 * to satisfy the "multiple of 8 RcvArray entries" requirement.
1624 */
1625 if (rcd->egrbufs.size <= (1 << 20))
1626 rcd->egrbufs.rcvtid_size = max((unsigned long)round_mtu,
1627 rounddown_pow_of_two(rcd->egrbufs.size / 8));
1628
1629 while (alloced_bytes < rcd->egrbufs.size &&
1630 rcd->egrbufs.alloced < rcd->egrbufs.count) {
1631 rcd->egrbufs.buffers[idx].addr =
1632 dma_zalloc_coherent(&dd->pcidev->dev,
1633 rcd->egrbufs.rcvtid_size,
1634 &rcd->egrbufs.buffers[idx].phys,
1635 gfp_flags);
1636 if (rcd->egrbufs.buffers[idx].addr) {
1637 rcd->egrbufs.buffers[idx].len =
1638 rcd->egrbufs.rcvtid_size;
1639 rcd->egrbufs.rcvtids[rcd->egrbufs.alloced].addr =
1640 rcd->egrbufs.buffers[idx].addr;
1641 rcd->egrbufs.rcvtids[rcd->egrbufs.alloced].phys =
1642 rcd->egrbufs.buffers[idx].phys;
1643 rcd->egrbufs.alloced++;
1644 alloced_bytes += rcd->egrbufs.rcvtid_size;
1645 idx++;
1646 } else {
1647 u32 new_size, i, j;
1648 u64 offset = 0;
1649
1650 /*
1651 * Fail the eager buffer allocation if:
1652 * - we are already using the lowest acceptable size
1653 * - we are using one-pkt-per-egr-buffer (this implies
1654 * that we are accepting only one size)
1655 */
1656 if (rcd->egrbufs.rcvtid_size == round_mtu ||
1657 !HFI1_CAP_KGET_MASK(rcd->flags, MULTI_PKT_EGR)) {
1658 dd_dev_err(dd, "ctxt%u: Failed to allocate eager buffers\n",
1659 rcd->ctxt);
1660 goto bail_rcvegrbuf_phys;
1661 }
1662
1663 new_size = rcd->egrbufs.rcvtid_size / 2;
1664
1665 /*
1666 * If the first attempt to allocate memory failed, don't
1667 * fail everything but continue with the next lower
1668 * size.
1669 */
1670 if (idx == 0) {
1671 rcd->egrbufs.rcvtid_size = new_size;
1672 continue;
1673 }
1674
1675 /*
1676 * Re-partition already allocated buffers to a smaller
1677 * size.
1678 */
1679 rcd->egrbufs.alloced = 0;
1680 for (i = 0, j = 0, offset = 0; j < idx; i++) {
1681 if (i >= rcd->egrbufs.count)
1682 break;
1683 rcd->egrbufs.rcvtids[i].phys =
1684 rcd->egrbufs.buffers[j].phys + offset;
1685 rcd->egrbufs.rcvtids[i].addr =
1686 rcd->egrbufs.buffers[j].addr + offset;
1687 rcd->egrbufs.alloced++;
1688 if ((rcd->egrbufs.buffers[j].phys + offset +
1689 new_size) ==
1690 (rcd->egrbufs.buffers[j].phys +
1691 rcd->egrbufs.buffers[j].len)) {
1692 j++;
1693 offset = 0;
1694 } else
1695 offset += new_size;
1696 }
1697 rcd->egrbufs.rcvtid_size = new_size;
1698 }
1699 }
1700 rcd->egrbufs.numbufs = idx;
1701 rcd->egrbufs.size = alloced_bytes;
1702
Sebastian Sanchez6c63e422015-11-06 20:06:56 -05001703 hfi1_cdbg(PROC,
1704 "ctxt%u: Alloced %u rcv tid entries @ %uKB, total %zuKB\n",
1705 rcd->ctxt, rcd->egrbufs.alloced, rcd->egrbufs.rcvtid_size,
1706 rcd->egrbufs.size);
1707
Mike Marciniszyn77241052015-07-30 15:17:43 -04001708
1709 /*
1710 * Set the contexts rcv array head update threshold to the closest
1711 * power of 2 (so we can use a mask instead of modulo) below half
1712 * the allocated entries.
1713 */
1714 rcd->egrbufs.threshold =
1715 rounddown_pow_of_two(rcd->egrbufs.alloced / 2);
1716 /*
1717 * Compute the expected RcvArray entry base. This is done after
1718 * allocating the eager buffers in order to maximize the
1719 * expected RcvArray entries for the context.
1720 */
1721 max_entries = rcd->rcv_array_groups * dd->rcv_entries.group_size;
1722 egrtop = roundup(rcd->egrbufs.alloced, dd->rcv_entries.group_size);
1723 rcd->expected_count = max_entries - egrtop;
1724 if (rcd->expected_count > MAX_TID_PAIR_ENTRIES * 2)
1725 rcd->expected_count = MAX_TID_PAIR_ENTRIES * 2;
1726
1727 rcd->expected_base = rcd->eager_base + egrtop;
Sebastian Sanchez6c63e422015-11-06 20:06:56 -05001728 hfi1_cdbg(PROC, "ctxt%u: eager:%u, exp:%u, egrbase:%u, expbase:%u\n",
1729 rcd->ctxt, rcd->egrbufs.alloced, rcd->expected_count,
1730 rcd->eager_base, rcd->expected_base);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001731
1732 if (!hfi1_rcvbuf_validate(rcd->egrbufs.rcvtid_size, PT_EAGER, &order)) {
Sebastian Sanchez6c63e422015-11-06 20:06:56 -05001733 hfi1_cdbg(PROC,
1734 "ctxt%u: current Eager buffer size is invalid %u\n",
1735 rcd->ctxt, rcd->egrbufs.rcvtid_size);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001736 ret = -EINVAL;
1737 goto bail;
1738 }
1739
1740 for (idx = 0; idx < rcd->egrbufs.alloced; idx++) {
1741 hfi1_put_tid(dd, rcd->eager_base + idx, PT_EAGER,
1742 rcd->egrbufs.rcvtids[idx].phys, order);
1743 cond_resched();
1744 }
1745 goto bail;
1746
1747bail_rcvegrbuf_phys:
1748 for (idx = 0; idx < rcd->egrbufs.alloced &&
1749 rcd->egrbufs.buffers[idx].addr;
1750 idx++) {
1751 dma_free_coherent(&dd->pcidev->dev,
1752 rcd->egrbufs.buffers[idx].len,
1753 rcd->egrbufs.buffers[idx].addr,
1754 rcd->egrbufs.buffers[idx].phys);
1755 rcd->egrbufs.buffers[idx].addr = NULL;
1756 rcd->egrbufs.buffers[idx].phys = 0;
1757 rcd->egrbufs.buffers[idx].len = 0;
1758 }
1759bail:
1760 return ret;
1761}