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