blob: b0c3e8a977252c540b7e3c35cc6bfab2bef3560b [file] [log] [blame]
Mike Marciniszyn77241052015-07-30 15:17:43 -04001/*
Jubin John05d6ac12016-02-14 20:22:17 -08002 * Copyright(c) 2015, 2016 Intel Corporation.
Mike Marciniszyn77241052015-07-30 15:17:43 -04003 *
4 * This file is provided under a dual BSD/GPLv2 license. When using or
5 * redistributing this file, you may do so under either license.
6 *
7 * GPL LICENSE SUMMARY
8 *
Mike Marciniszyn77241052015-07-30 15:17:43 -04009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of version 2 of the GNU General Public License as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * BSD LICENSE
19 *
Mike Marciniszyn77241052015-07-30 15:17:43 -040020 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
22 * are met:
23 *
24 * - Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * - Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in
28 * the documentation and/or other materials provided with the
29 * distribution.
30 * - Neither the name of Intel Corporation nor the names of its
31 * contributors may be used to endorse or promote products derived
32 * from this software without specific prior written permission.
33 *
34 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
35 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
37 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
38 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
41 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
42 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
44 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45 *
46 */
47
48#include <linux/pci.h>
49#include <linux/netdevice.h>
50#include <linux/vmalloc.h>
51#include <linux/delay.h>
52#include <linux/idr.h>
53#include <linux/module.h>
54#include <linux/printk.h>
55#include <linux/hrtimer.h>
Dennis Dalessandroec3f2c12016-01-19 14:41:33 -080056#include <rdma/rdma_vt.h>
Mike Marciniszyn77241052015-07-30 15:17:43 -040057
58#include "hfi.h"
59#include "device.h"
60#include "common.h"
Sebastian Sanchez6c63e422015-11-06 20:06:56 -050061#include "trace.h"
Mike Marciniszyn77241052015-07-30 15:17:43 -040062#include "mad.h"
63#include "sdma.h"
64#include "debugfs.h"
65#include "verbs.h"
Ashutosh Dixitaffa48d2016-02-03 14:33:06 -080066#include "aspm.h"
Dennis Dalessandro41973442016-07-25 07:52:36 -070067#include "affinity.h"
Mike Marciniszyn77241052015-07-30 15:17:43 -040068
69#undef pr_fmt
70#define pr_fmt(fmt) DRIVER_NAME ": " fmt
71
72/*
73 * min buffers we want to have per context, after driver
74 */
75#define HFI1_MIN_USER_CTXT_BUFCNT 7
76
77#define HFI1_MIN_HDRQ_EGRBUF_CNT 2
Sebastian Sancheze002dcc2016-02-03 14:34:32 -080078#define HFI1_MAX_HDRQ_EGRBUF_CNT 16352
Mike Marciniszyn77241052015-07-30 15:17:43 -040079#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;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400132
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500133 /* Control context has to be always 0 */
134 BUILD_BUG_ON(HFI1_CTRL_CTXT != 0);
135
Mitko Haralanov377f1112016-02-03 14:33:58 -0800136 dd->rcd = kzalloc_node(dd->num_rcv_contexts * sizeof(*dd->rcd),
137 GFP_KERNEL, dd->node);
Alison Schofield806e6e12015-10-12 14:28:36 -0700138 if (!dd->rcd)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400139 goto nomem;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400140
141 /* create one or more kernel contexts */
142 for (i = 0; i < dd->first_user_ctxt; ++i) {
143 struct hfi1_pportdata *ppd;
144 struct hfi1_ctxtdata *rcd;
145
146 ppd = dd->pport + (i % dd->num_pports);
Mitko Haralanov957558c2016-02-03 14:33:40 -0800147 rcd = hfi1_create_ctxtdata(ppd, i, dd->node);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400148 if (!rcd) {
149 dd_dev_err(dd,
Jubin John17fb4f22016-02-14 20:21:52 -0800150 "Unable to allocate kernel receive context, failing\n");
Mike Marciniszyn77241052015-07-30 15:17:43 -0400151 goto nomem;
152 }
153 /*
154 * Set up the kernel context flags here and now because they
155 * use default values for all receive side memories. User
156 * contexts will be handled as they are created.
157 */
158 rcd->flags = HFI1_CAP_KGET(MULTI_PKT_EGR) |
159 HFI1_CAP_KGET(NODROP_RHQ_FULL) |
160 HFI1_CAP_KGET(NODROP_EGR_FULL) |
161 HFI1_CAP_KGET(DMA_RTAIL);
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500162
163 /* Control context must use DMA_RTAIL */
164 if (rcd->ctxt == HFI1_CTRL_CTXT)
165 rcd->flags |= HFI1_CAP_DMA_RTAIL;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400166 rcd->seq_cnt = 1;
167
168 rcd->sc = sc_alloc(dd, SC_ACK, rcd->rcvhdrqentsize, dd->node);
169 if (!rcd->sc) {
170 dd_dev_err(dd,
Jubin John17fb4f22016-02-14 20:21:52 -0800171 "Unable to allocate kernel send context, failing\n");
Mike Marciniszyn77241052015-07-30 15:17:43 -0400172 dd->rcd[rcd->ctxt] = NULL;
173 hfi1_free_ctxtdata(dd, rcd);
174 goto nomem;
175 }
176
177 ret = hfi1_init_ctxt(rcd->sc);
178 if (ret < 0) {
179 dd_dev_err(dd,
180 "Failed to setup kernel receive context, failing\n");
181 sc_free(rcd->sc);
182 dd->rcd[rcd->ctxt] = NULL;
183 hfi1_free_ctxtdata(dd, rcd);
184 ret = -EFAULT;
185 goto bail;
186 }
187 }
188
Ashutosh Dixitaffa48d2016-02-03 14:33:06 -0800189 /*
190 * Initialize aspm, to be done after gen3 transition and setting up
191 * contexts and before enabling interrupts
192 */
193 aspm_init(dd);
194
Mike Marciniszyn77241052015-07-30 15:17:43 -0400195 return 0;
196nomem:
197 ret = -ENOMEM;
198bail:
199 kfree(dd->rcd);
200 dd->rcd = NULL;
201 return ret;
202}
203
204/*
205 * Common code for user and kernel context setup.
206 */
Mitko Haralanov957558c2016-02-03 14:33:40 -0800207struct hfi1_ctxtdata *hfi1_create_ctxtdata(struct hfi1_pportdata *ppd, u32 ctxt,
208 int numa)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400209{
210 struct hfi1_devdata *dd = ppd->dd;
211 struct hfi1_ctxtdata *rcd;
212 unsigned kctxt_ngroups = 0;
213 u32 base;
214
215 if (dd->rcv_entries.nctxt_extra >
216 dd->num_rcv_contexts - dd->first_user_ctxt)
217 kctxt_ngroups = (dd->rcv_entries.nctxt_extra -
218 (dd->num_rcv_contexts - dd->first_user_ctxt));
219 rcd = kzalloc(sizeof(*rcd), GFP_KERNEL);
220 if (rcd) {
221 u32 rcvtids, max_entries;
222
Sebastian Sanchez6c63e422015-11-06 20:06:56 -0500223 hfi1_cdbg(PROC, "setting up context %u\n", ctxt);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400224
225 INIT_LIST_HEAD(&rcd->qp_wait_list);
226 rcd->ppd = ppd;
227 rcd->dd = dd;
228 rcd->cnt = 1;
229 rcd->ctxt = ctxt;
230 dd->rcd[ctxt] = rcd;
Mitko Haralanov957558c2016-02-03 14:33:40 -0800231 rcd->numa_id = numa;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400232 rcd->rcv_array_groups = dd->rcv_entries.ngroups;
233
Mitko Haralanov463e6eb2016-02-05 11:57:53 -0500234 mutex_init(&rcd->exp_lock);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400235
236 /*
237 * Calculate the context's RcvArray entry starting point.
238 * We do this here because we have to take into account all
239 * the RcvArray entries that previous context would have
240 * taken and we have to account for any extra groups
241 * assigned to the kernel or user contexts.
242 */
243 if (ctxt < dd->first_user_ctxt) {
244 if (ctxt < kctxt_ngroups) {
245 base = ctxt * (dd->rcv_entries.ngroups + 1);
246 rcd->rcv_array_groups++;
247 } else
248 base = kctxt_ngroups +
249 (ctxt * dd->rcv_entries.ngroups);
250 } else {
251 u16 ct = ctxt - dd->first_user_ctxt;
252
253 base = ((dd->n_krcv_queues * dd->rcv_entries.ngroups) +
254 kctxt_ngroups);
255 if (ct < dd->rcv_entries.nctxt_extra) {
256 base += ct * (dd->rcv_entries.ngroups + 1);
257 rcd->rcv_array_groups++;
258 } else
259 base += dd->rcv_entries.nctxt_extra +
260 (ct * dd->rcv_entries.ngroups);
261 }
262 rcd->eager_base = base * dd->rcv_entries.group_size;
263
264 /* Validate and initialize Rcv Hdr Q variables */
265 if (rcvhdrcnt % HDRQ_INCREMENT) {
266 dd_dev_err(dd,
jubin.john@intel.com349ac712016-01-11 18:30:52 -0500267 "ctxt%u: header queue count %d must be divisible by %lu\n",
Mike Marciniszyn77241052015-07-30 15:17:43 -0400268 rcd->ctxt, rcvhdrcnt, HDRQ_INCREMENT);
269 goto bail;
270 }
271 rcd->rcvhdrq_cnt = rcvhdrcnt;
272 rcd->rcvhdrqentsize = hfi1_hdrq_entsize;
273 /*
274 * Simple Eager buffer allocation: we have already pre-allocated
275 * the number of RcvArray entry groups. Each ctxtdata structure
276 * holds the number of groups for that context.
277 *
278 * To follow CSR requirements and maintain cacheline alignment,
279 * make sure all sizes and bases are multiples of group_size.
280 *
281 * The expected entry count is what is left after assigning
282 * eager.
283 */
284 max_entries = rcd->rcv_array_groups *
285 dd->rcv_entries.group_size;
286 rcvtids = ((max_entries * hfi1_rcvarr_split) / 100);
287 rcd->egrbufs.count = round_down(rcvtids,
288 dd->rcv_entries.group_size);
289 if (rcd->egrbufs.count > MAX_EAGER_ENTRIES) {
290 dd_dev_err(dd, "ctxt%u: requested too many RcvArray entries.\n",
291 rcd->ctxt);
292 rcd->egrbufs.count = MAX_EAGER_ENTRIES;
293 }
Sebastian Sanchez6c63e422015-11-06 20:06:56 -0500294 hfi1_cdbg(PROC,
295 "ctxt%u: max Eager buffer RcvArray entries: %u\n",
296 rcd->ctxt, rcd->egrbufs.count);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400297
298 /*
299 * Allocate array that will hold the eager buffer accounting
300 * data.
301 * This will allocate the maximum possible buffer count based
302 * on the value of the RcvArray split parameter.
303 * The resulting value will be rounded down to the closest
304 * multiple of dd->rcv_entries.group_size.
305 */
Shraddha Barke314fcc02015-10-09 21:03:26 +0530306 rcd->egrbufs.buffers = kcalloc(rcd->egrbufs.count,
307 sizeof(*rcd->egrbufs.buffers),
308 GFP_KERNEL);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400309 if (!rcd->egrbufs.buffers)
310 goto bail;
Shraddha Barke314fcc02015-10-09 21:03:26 +0530311 rcd->egrbufs.rcvtids = kcalloc(rcd->egrbufs.count,
312 sizeof(*rcd->egrbufs.rcvtids),
313 GFP_KERNEL);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400314 if (!rcd->egrbufs.rcvtids)
315 goto bail;
316 rcd->egrbufs.size = eager_buffer_size;
317 /*
318 * The size of the buffers programmed into the RcvArray
319 * entries needs to be big enough to handle the highest
320 * MTU supported.
321 */
322 if (rcd->egrbufs.size < hfi1_max_mtu) {
323 rcd->egrbufs.size = __roundup_pow_of_two(hfi1_max_mtu);
Sebastian Sanchez6c63e422015-11-06 20:06:56 -0500324 hfi1_cdbg(PROC,
325 "ctxt%u: eager bufs size too small. Adjusting to %zu\n",
Mike Marciniszyn77241052015-07-30 15:17:43 -0400326 rcd->ctxt, rcd->egrbufs.size);
327 }
328 rcd->egrbufs.rcvtid_size = HFI1_MAX_EAGER_BUFFER_SIZE;
329
330 if (ctxt < dd->first_user_ctxt) { /* N/A for PSM contexts */
331 rcd->opstats = kzalloc(sizeof(*rcd->opstats),
332 GFP_KERNEL);
Alison Schofield806e6e12015-10-12 14:28:36 -0700333 if (!rcd->opstats)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400334 goto bail;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400335 }
336 }
337 return rcd;
338bail:
Mike Marciniszyn77241052015-07-30 15:17:43 -0400339 kfree(rcd->egrbufs.rcvtids);
340 kfree(rcd->egrbufs.buffers);
341 kfree(rcd);
342 return NULL;
343}
344
345/*
346 * Convert a receive header entry size that to the encoding used in the CSR.
347 *
348 * Return a zero if the given size is invalid.
349 */
350static inline u64 encode_rcv_header_entry_size(u16 size)
351{
352 /* there are only 3 valid receive header entry sizes */
353 if (size == 2)
354 return 1;
355 if (size == 16)
356 return 2;
357 else if (size == 32)
358 return 4;
359 return 0; /* invalid */
360}
361
362/*
363 * Select the largest ccti value over all SLs to determine the intra-
364 * packet gap for the link.
365 *
366 * called with cca_timer_lock held (to protect access to cca_timer
367 * array), and rcu_read_lock() (to protect access to cc_state).
368 */
369void set_link_ipg(struct hfi1_pportdata *ppd)
370{
371 struct hfi1_devdata *dd = ppd->dd;
372 struct cc_state *cc_state;
373 int i;
374 u16 cce, ccti_limit, max_ccti = 0;
375 u16 shift, mult;
376 u64 src;
377 u32 current_egress_rate; /* Mbits /sec */
378 u32 max_pkt_time;
379 /*
380 * max_pkt_time is the maximum packet egress time in units
381 * of the fabric clock period 1/(805 MHz).
382 */
383
384 cc_state = get_cc_state(ppd);
385
Jubin Johnd125a6c2016-02-14 20:19:49 -0800386 if (!cc_state)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400387 /*
388 * This should _never_ happen - rcu_read_lock() is held,
389 * and set_link_ipg() should not be called if cc_state
390 * is NULL.
391 */
392 return;
393
394 for (i = 0; i < OPA_MAX_SLS; i++) {
395 u16 ccti = ppd->cca_timer[i].ccti;
396
397 if (ccti > max_ccti)
398 max_ccti = ccti;
399 }
400
401 ccti_limit = cc_state->cct.ccti_limit;
402 if (max_ccti > ccti_limit)
403 max_ccti = ccti_limit;
404
405 cce = cc_state->cct.entries[max_ccti].entry;
406 shift = (cce & 0xc000) >> 14;
407 mult = (cce & 0x3fff);
408
409 current_egress_rate = active_egress_rate(ppd);
410
411 max_pkt_time = egress_cycles(ppd->ibmaxlen, current_egress_rate);
412
413 src = (max_pkt_time >> shift) * mult;
414
415 src &= SEND_STATIC_RATE_CONTROL_CSR_SRC_RELOAD_SMASK;
416 src <<= SEND_STATIC_RATE_CONTROL_CSR_SRC_RELOAD_SHIFT;
417
418 write_csr(dd, SEND_STATIC_RATE_CONTROL, src);
419}
420
421static enum hrtimer_restart cca_timer_fn(struct hrtimer *t)
422{
423 struct cca_timer *cca_timer;
424 struct hfi1_pportdata *ppd;
425 int sl;
Jubin Johnd35cf742016-04-14 08:31:53 -0700426 u16 ccti_timer, ccti_min;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400427 struct cc_state *cc_state;
Dean Luickb77d7132015-10-26 10:28:43 -0400428 unsigned long flags;
Jubin Johnd35cf742016-04-14 08:31:53 -0700429 enum hrtimer_restart ret = HRTIMER_NORESTART;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400430
431 cca_timer = container_of(t, struct cca_timer, hrtimer);
432 ppd = cca_timer->ppd;
433 sl = cca_timer->sl;
434
435 rcu_read_lock();
436
437 cc_state = get_cc_state(ppd);
438
Jubin Johnd125a6c2016-02-14 20:19:49 -0800439 if (!cc_state) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400440 rcu_read_unlock();
441 return HRTIMER_NORESTART;
442 }
443
444 /*
445 * 1) decrement ccti for SL
446 * 2) calculate IPG for link (set_link_ipg())
447 * 3) restart timer, unless ccti is at min value
448 */
449
450 ccti_min = cc_state->cong_setting.entries[sl].ccti_min;
451 ccti_timer = cc_state->cong_setting.entries[sl].ccti_timer;
452
Dean Luickb77d7132015-10-26 10:28:43 -0400453 spin_lock_irqsave(&ppd->cca_timer_lock, flags);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400454
Jubin Johnd35cf742016-04-14 08:31:53 -0700455 if (cca_timer->ccti > ccti_min) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400456 cca_timer->ccti--;
457 set_link_ipg(ppd);
458 }
459
Jubin Johnd35cf742016-04-14 08:31:53 -0700460 if (cca_timer->ccti > ccti_min) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400461 unsigned long nsec = 1024 * ccti_timer;
462 /* ccti_timer is in units of 1.024 usec */
463 hrtimer_forward_now(t, ns_to_ktime(nsec));
Jubin Johnd35cf742016-04-14 08:31:53 -0700464 ret = HRTIMER_RESTART;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400465 }
Jubin Johnd35cf742016-04-14 08:31:53 -0700466
467 spin_unlock_irqrestore(&ppd->cca_timer_lock, flags);
468 rcu_read_unlock();
469 return ret;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400470}
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 */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400591 for (i = 0; i < dd->first_user_ctxt; ++i) {
Mitko Haralanov566c1572016-02-03 14:32:49 -0800592 rcvmask = HFI1_RCVCTRL_CTXT_ENB | HFI1_RCVCTRL_INTRAVAIL_ENB;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400593 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);
Ashutosh Dixit39239792016-05-12 10:24:00 -0700736 if (lastfail) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400737 dd_dev_err(dd,
Jubin John17fb4f22016-02-14 20:21:52 -0800738 "failed to allocate kernel ctxt's rcvhdrq and/or egr bufs\n");
Ashutosh Dixit39239792016-05-12 10:24:00 -0700739 ret = lastfail;
740 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400741 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400742
743 /* Allocate enough memory for user event notification. */
Amitoj Kaur Chawla84449912016-03-04 22:30:43 +0530744 len = PAGE_ALIGN(dd->chip_rcv_contexts * HFI1_MAX_SHARED_CTXTS *
745 sizeof(*dd->events));
Mike Marciniszyn77241052015-07-30 15:17:43 -0400746 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
Jubin John4d114fd2016-02-14 20:21:43 -0800787 /*
788 * start the serdes - must be after interrupts are
789 * enabled so we are notified when the link goes up
Mike Marciniszyn77241052015-07-30 15:17:43 -0400790 */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400791 lastfail = bringup_serdes(ppd);
792 if (lastfail)
793 dd_dev_info(dd,
Jubin John17fb4f22016-02-14 20:21:52 -0800794 "Failed to bring up port %u\n",
795 ppd->port);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400796
797 /*
798 * Set status even if port serdes is not initialized
799 * so that diags will work.
800 */
801 if (ppd->statusp)
802 *ppd->statusp |= HFI1_STATUS_CHIP_PRESENT |
803 HFI1_STATUS_INITTED;
804 if (!ppd->link_speed_enabled)
805 continue;
806 }
807 }
808
809 /* if ret is non-zero, we probably should do some cleanup here... */
810 return ret;
811}
812
813static inline struct hfi1_devdata *__hfi1_lookup(int unit)
814{
815 return idr_find(&hfi1_unit_table, unit);
816}
817
818struct hfi1_devdata *hfi1_lookup(int unit)
819{
820 struct hfi1_devdata *dd;
821 unsigned long flags;
822
823 spin_lock_irqsave(&hfi1_devs_lock, flags);
824 dd = __hfi1_lookup(unit);
825 spin_unlock_irqrestore(&hfi1_devs_lock, flags);
826
827 return dd;
828}
829
830/*
831 * Stop the timers during unit shutdown, or after an error late
832 * in initialization.
833 */
834static void stop_timers(struct hfi1_devdata *dd)
835{
836 struct hfi1_pportdata *ppd;
837 int pidx;
838
839 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
840 ppd = dd->pport + pidx;
841 if (ppd->led_override_timer.data) {
842 del_timer_sync(&ppd->led_override_timer);
843 atomic_set(&ppd->led_override_timer_active, 0);
844 }
845 }
846}
847
848/**
849 * shutdown_device - shut down a device
850 * @dd: the hfi1_ib device
851 *
852 * This is called to make the device quiet when we are about to
853 * unload the driver, and also when the device is administratively
854 * disabled. It does not free any data structures.
855 * Everything it does has to be setup again by hfi1_init(dd, 1)
856 */
857static void shutdown_device(struct hfi1_devdata *dd)
858{
859 struct hfi1_pportdata *ppd;
860 unsigned pidx;
861 int i;
862
863 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
864 ppd = dd->pport + pidx;
865
866 ppd->linkup = 0;
867 if (ppd->statusp)
868 *ppd->statusp &= ~(HFI1_STATUS_IB_CONF |
869 HFI1_STATUS_IB_READY);
870 }
871 dd->flags &= ~HFI1_INITTED;
872
873 /* mask interrupts, but not errors */
874 set_intr_state(dd, 0);
875
876 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
877 ppd = dd->pport + pidx;
878 for (i = 0; i < dd->num_rcv_contexts; i++)
879 hfi1_rcvctrl(dd, HFI1_RCVCTRL_TAILUPD_DIS |
880 HFI1_RCVCTRL_CTXT_DIS |
881 HFI1_RCVCTRL_INTRAVAIL_DIS |
882 HFI1_RCVCTRL_PKEY_DIS |
883 HFI1_RCVCTRL_ONE_PKT_EGR_DIS, i);
884 /*
885 * Gracefully stop all sends allowing any in progress to
886 * trickle out first.
887 */
888 for (i = 0; i < dd->num_send_contexts; i++)
889 sc_flush(dd->send_contexts[i].sc);
890 }
891
892 /*
893 * Enough for anything that's going to trickle out to have actually
894 * done so.
895 */
896 udelay(20);
897
898 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
899 ppd = dd->pport + pidx;
900
901 /* disable all contexts */
902 for (i = 0; i < dd->num_send_contexts; i++)
903 sc_disable(dd->send_contexts[i].sc);
904 /* disable the send device */
905 pio_send_control(dd, PSC_GLOBAL_DISABLE);
906
Easwar Hariharan91ab4ed2016-02-03 14:35:57 -0800907 shutdown_led_override(ppd);
908
Mike Marciniszyn77241052015-07-30 15:17:43 -0400909 /*
910 * Clear SerdesEnable.
911 * We can't count on interrupts since we are stopping.
912 */
913 hfi1_quiet_serdes(ppd);
914
915 if (ppd->hfi1_wq) {
916 destroy_workqueue(ppd->hfi1_wq);
917 ppd->hfi1_wq = NULL;
918 }
919 }
920 sdma_exit(dd);
921}
922
923/**
924 * hfi1_free_ctxtdata - free a context's allocated data
925 * @dd: the hfi1_ib device
926 * @rcd: the ctxtdata structure
927 *
928 * free up any allocated data for a context
929 * This should not touch anything that would affect a simultaneous
930 * re-allocation of context data, because it is called after hfi1_mutex
931 * is released (and can be called from reinit as well).
932 * It should never change any chip state, or global driver state.
933 */
934void hfi1_free_ctxtdata(struct hfi1_devdata *dd, struct hfi1_ctxtdata *rcd)
935{
936 unsigned e;
937
938 if (!rcd)
939 return;
940
941 if (rcd->rcvhdrq) {
942 dma_free_coherent(&dd->pcidev->dev, rcd->rcvhdrq_size,
943 rcd->rcvhdrq, rcd->rcvhdrq_phys);
944 rcd->rcvhdrq = NULL;
945 if (rcd->rcvhdrtail_kvaddr) {
946 dma_free_coherent(&dd->pcidev->dev, PAGE_SIZE,
947 (void *)rcd->rcvhdrtail_kvaddr,
948 rcd->rcvhdrqtailaddr_phys);
949 rcd->rcvhdrtail_kvaddr = NULL;
950 }
951 }
952
953 /* all the RcvArray entries should have been cleared by now */
954 kfree(rcd->egrbufs.rcvtids);
955
956 for (e = 0; e < rcd->egrbufs.alloced; e++) {
957 if (rcd->egrbufs.buffers[e].phys)
958 dma_free_coherent(&dd->pcidev->dev,
959 rcd->egrbufs.buffers[e].len,
960 rcd->egrbufs.buffers[e].addr,
961 rcd->egrbufs.buffers[e].phys);
962 }
963 kfree(rcd->egrbufs.buffers);
964
965 sc_free(rcd->sc);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400966 vfree(rcd->user_event_mask);
967 vfree(rcd->subctxt_uregbase);
968 vfree(rcd->subctxt_rcvegrbuf);
969 vfree(rcd->subctxt_rcvhdr_base);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400970 kfree(rcd->opstats);
971 kfree(rcd);
972}
973
Dean Luick78eb1292016-03-05 08:49:45 -0800974/*
975 * Release our hold on the shared asic data. If we are the last one,
976 * free the structure. Must be holding hfi1_devs_lock.
977 */
978static void release_asic_data(struct hfi1_devdata *dd)
979{
980 int other;
981
982 if (!dd->asic_data)
983 return;
984 dd->asic_data->dds[dd->hfi1_id] = NULL;
985 other = dd->hfi1_id ? 0 : 1;
986 if (!dd->asic_data->dds[other]) {
987 /* we are the last holder, free it */
988 kfree(dd->asic_data);
989 }
990 dd->asic_data = NULL;
991}
992
Dennis Dalessandroe11ffbd2016-05-19 05:26:44 -0700993static void __hfi1_free_devdata(struct kobject *kobj)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400994{
Dennis Dalessandroe11ffbd2016-05-19 05:26:44 -0700995 struct hfi1_devdata *dd =
996 container_of(kobj, struct hfi1_devdata, kobj);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400997 unsigned long flags;
998
999 spin_lock_irqsave(&hfi1_devs_lock, flags);
1000 idr_remove(&hfi1_unit_table, dd->unit);
1001 list_del(&dd->list);
Dean Luick78eb1292016-03-05 08:49:45 -08001002 release_asic_data(dd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001003 spin_unlock_irqrestore(&hfi1_devs_lock, flags);
Easwar Hariharanc3838b32016-02-09 14:29:13 -08001004 free_platform_config(dd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001005 rcu_barrier(); /* wait for rcu callbacks to complete */
1006 free_percpu(dd->int_counter);
1007 free_percpu(dd->rcv_limit);
Vennila Megavannan89abfc82016-02-03 14:34:07 -08001008 free_percpu(dd->send_schedule);
Jubin Johnea0e4ce2016-04-20 06:05:24 -07001009 rvt_dealloc_device(&dd->verbs_dev.rdi);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001010}
1011
Dennis Dalessandroe11ffbd2016-05-19 05:26:44 -07001012static struct kobj_type hfi1_devdata_type = {
1013 .release = __hfi1_free_devdata,
1014};
1015
1016void hfi1_free_devdata(struct hfi1_devdata *dd)
1017{
1018 kobject_put(&dd->kobj);
1019}
1020
Mike Marciniszyn77241052015-07-30 15:17:43 -04001021/*
1022 * Allocate our primary per-unit data structure. Must be done via verbs
1023 * allocator, because the verbs cleanup process both does cleanup and
1024 * free of the data structure.
1025 * "extra" is for chip-specific data.
1026 *
1027 * Use the idr mechanism to get a unit number for this unit.
1028 */
1029struct hfi1_devdata *hfi1_alloc_devdata(struct pci_dev *pdev, size_t extra)
1030{
1031 unsigned long flags;
1032 struct hfi1_devdata *dd;
Dennis Dalessandro7af6d002016-01-19 14:44:06 -08001033 int ret, nports;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001034
Dennis Dalessandro7af6d002016-01-19 14:44:06 -08001035 /* extra is * number of ports */
1036 nports = extra / sizeof(struct hfi1_pportdata);
1037
1038 dd = (struct hfi1_devdata *)rvt_alloc_device(sizeof(*dd) + extra,
1039 nports);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001040 if (!dd)
1041 return ERR_PTR(-ENOMEM);
Dennis Dalessandro7af6d002016-01-19 14:44:06 -08001042 dd->num_pports = nports;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001043 dd->pport = (struct hfi1_pportdata *)(dd + 1);
1044
1045 INIT_LIST_HEAD(&dd->list);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001046 idr_preload(GFP_KERNEL);
1047 spin_lock_irqsave(&hfi1_devs_lock, flags);
1048
1049 ret = idr_alloc(&hfi1_unit_table, dd, 0, 0, GFP_NOWAIT);
1050 if (ret >= 0) {
1051 dd->unit = ret;
1052 list_add(&dd->list, &hfi1_dev_list);
1053 }
1054
1055 spin_unlock_irqrestore(&hfi1_devs_lock, flags);
1056 idr_preload_end();
1057
1058 if (ret < 0) {
1059 hfi1_early_err(&pdev->dev,
1060 "Could not allocate unit ID: error %d\n", -ret);
1061 goto bail;
1062 }
1063 /*
1064 * Initialize all locks for the device. This needs to be as early as
1065 * possible so locks are usable.
1066 */
1067 spin_lock_init(&dd->sc_lock);
1068 spin_lock_init(&dd->sendctrl_lock);
1069 spin_lock_init(&dd->rcvctrl_lock);
1070 spin_lock_init(&dd->uctxt_lock);
1071 spin_lock_init(&dd->hfi1_diag_trans_lock);
1072 spin_lock_init(&dd->sc_init_lock);
1073 spin_lock_init(&dd->dc8051_lock);
1074 spin_lock_init(&dd->dc8051_memlock);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001075 seqlock_init(&dd->sc2vl_lock);
1076 spin_lock_init(&dd->sde_map_lock);
Jubin John35f6bef2016-02-14 12:46:10 -08001077 spin_lock_init(&dd->pio_map_lock);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001078 init_waitqueue_head(&dd->event_queue);
1079
1080 dd->int_counter = alloc_percpu(u64);
1081 if (!dd->int_counter) {
1082 ret = -ENOMEM;
1083 hfi1_early_err(&pdev->dev,
1084 "Could not allocate per-cpu int_counter\n");
1085 goto bail;
1086 }
1087
1088 dd->rcv_limit = alloc_percpu(u64);
1089 if (!dd->rcv_limit) {
1090 ret = -ENOMEM;
1091 hfi1_early_err(&pdev->dev,
1092 "Could not allocate per-cpu rcv_limit\n");
1093 goto bail;
1094 }
1095
Vennila Megavannan89abfc82016-02-03 14:34:07 -08001096 dd->send_schedule = alloc_percpu(u64);
1097 if (!dd->send_schedule) {
1098 ret = -ENOMEM;
1099 hfi1_early_err(&pdev->dev,
1100 "Could not allocate per-cpu int_counter\n");
1101 goto bail;
1102 }
1103
Mike Marciniszyn77241052015-07-30 15:17:43 -04001104 if (!hfi1_cpulist_count) {
1105 u32 count = num_online_cpus();
1106
Shraddha Barke314fcc02015-10-09 21:03:26 +05301107 hfi1_cpulist = kcalloc(BITS_TO_LONGS(count), sizeof(long),
1108 GFP_KERNEL);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001109 if (hfi1_cpulist)
1110 hfi1_cpulist_count = count;
1111 else
1112 hfi1_early_err(
1113 &pdev->dev,
1114 "Could not alloc cpulist info, cpu affinity might be wrong\n");
1115 }
Dennis Dalessandroe11ffbd2016-05-19 05:26:44 -07001116 kobject_init(&dd->kobj, &hfi1_devdata_type);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001117 return dd;
1118
1119bail:
1120 if (!list_empty(&dd->list))
1121 list_del_init(&dd->list);
Jubin Johnea0e4ce2016-04-20 06:05:24 -07001122 rvt_dealloc_device(&dd->verbs_dev.rdi);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001123 return ERR_PTR(ret);
1124}
1125
1126/*
1127 * Called from freeze mode handlers, and from PCI error
1128 * reporting code. Should be paranoid about state of
1129 * system and data structures.
1130 */
1131void hfi1_disable_after_error(struct hfi1_devdata *dd)
1132{
1133 if (dd->flags & HFI1_INITTED) {
1134 u32 pidx;
1135
1136 dd->flags &= ~HFI1_INITTED;
1137 if (dd->pport)
1138 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
1139 struct hfi1_pportdata *ppd;
1140
1141 ppd = dd->pport + pidx;
1142 if (dd->flags & HFI1_PRESENT)
1143 set_link_state(ppd, HLS_DN_DISABLE);
1144
1145 if (ppd->statusp)
1146 *ppd->statusp &= ~HFI1_STATUS_IB_READY;
1147 }
1148 }
1149
1150 /*
1151 * Mark as having had an error for driver, and also
1152 * for /sys and status word mapped to user programs.
1153 * This marks unit as not usable, until reset.
1154 */
1155 if (dd->status)
1156 dd->status->dev |= HFI1_STATUS_HWERROR;
1157}
1158
1159static void remove_one(struct pci_dev *);
1160static int init_one(struct pci_dev *, const struct pci_device_id *);
1161
1162#define DRIVER_LOAD_MSG "Intel " DRIVER_NAME " loaded: "
1163#define PFX DRIVER_NAME ": "
1164
1165static const struct pci_device_id hfi1_pci_tbl[] = {
1166 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL0) },
1167 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL1) },
1168 { 0, }
1169};
1170
1171MODULE_DEVICE_TABLE(pci, hfi1_pci_tbl);
1172
1173static struct pci_driver hfi1_pci_driver = {
1174 .name = DRIVER_NAME,
1175 .probe = init_one,
1176 .remove = remove_one,
1177 .id_table = hfi1_pci_tbl,
1178 .err_handler = &hfi1_pci_err_handler,
1179};
1180
1181static void __init compute_krcvqs(void)
1182{
1183 int i;
1184
1185 for (i = 0; i < krcvqsset; i++)
1186 n_krcvqs += krcvqs[i];
1187}
1188
1189/*
1190 * Do all the generic driver unit- and chip-independent memory
1191 * allocation and initialization.
1192 */
1193static int __init hfi1_mod_init(void)
1194{
1195 int ret;
1196
1197 ret = dev_init();
1198 if (ret)
1199 goto bail;
1200
Dennis Dalessandro41973442016-07-25 07:52:36 -07001201 node_affinity_init();
1202
Mike Marciniszyn77241052015-07-30 15:17:43 -04001203 /* validate max MTU before any devices start */
1204 if (!valid_opa_max_mtu(hfi1_max_mtu)) {
1205 pr_err("Invalid max_mtu 0x%x, using 0x%x instead\n",
1206 hfi1_max_mtu, HFI1_DEFAULT_MAX_MTU);
1207 hfi1_max_mtu = HFI1_DEFAULT_MAX_MTU;
1208 }
1209 /* valid CUs run from 1-128 in powers of 2 */
1210 if (hfi1_cu > 128 || !is_power_of_2(hfi1_cu))
1211 hfi1_cu = 1;
1212 /* valid credit return threshold is 0-100, variable is unsigned */
1213 if (user_credit_return_threshold > 100)
1214 user_credit_return_threshold = 100;
1215
1216 compute_krcvqs();
Jubin John4d114fd2016-02-14 20:21:43 -08001217 /*
1218 * sanitize receive interrupt count, time must wait until after
1219 * the hardware type is known
1220 */
Mike Marciniszyn77241052015-07-30 15:17:43 -04001221 if (rcv_intr_count > RCV_HDR_HEAD_COUNTER_MASK)
1222 rcv_intr_count = RCV_HDR_HEAD_COUNTER_MASK;
1223 /* reject invalid combinations */
1224 if (rcv_intr_count == 0 && rcv_intr_timeout == 0) {
1225 pr_err("Invalid mode: both receive interrupt count and available timeout are zero - setting interrupt count to 1\n");
1226 rcv_intr_count = 1;
1227 }
1228 if (rcv_intr_count > 1 && rcv_intr_timeout == 0) {
1229 /*
1230 * Avoid indefinite packet delivery by requiring a timeout
1231 * if count is > 1.
1232 */
1233 pr_err("Invalid mode: receive interrupt count greater than 1 and available timeout is zero - setting available timeout to 1\n");
1234 rcv_intr_timeout = 1;
1235 }
1236 if (rcv_intr_dynamic && !(rcv_intr_count > 1 && rcv_intr_timeout > 0)) {
1237 /*
1238 * The dynamic algorithm expects a non-zero timeout
1239 * and a count > 1.
1240 */
1241 pr_err("Invalid mode: dynamic receive interrupt mitigation with invalid count and timeout - turning dynamic off\n");
1242 rcv_intr_dynamic = 0;
1243 }
1244
1245 /* sanitize link CRC options */
1246 link_crc_mask &= SUPPORTED_CRCS;
1247
1248 /*
1249 * These must be called before the driver is registered with
1250 * the PCI subsystem.
1251 */
1252 idr_init(&hfi1_unit_table);
1253
1254 hfi1_dbg_init();
Dean Luick528ee9f2016-03-05 08:50:43 -08001255 ret = hfi1_wss_init();
1256 if (ret < 0)
1257 goto bail_wss;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001258 ret = pci_register_driver(&hfi1_pci_driver);
1259 if (ret < 0) {
1260 pr_err("Unable to register driver: error %d\n", -ret);
1261 goto bail_dev;
1262 }
1263 goto bail; /* all OK */
1264
1265bail_dev:
Dean Luick528ee9f2016-03-05 08:50:43 -08001266 hfi1_wss_exit();
1267bail_wss:
Mike Marciniszyn77241052015-07-30 15:17:43 -04001268 hfi1_dbg_exit();
1269 idr_destroy(&hfi1_unit_table);
1270 dev_cleanup();
1271bail:
1272 return ret;
1273}
1274
1275module_init(hfi1_mod_init);
1276
1277/*
1278 * Do the non-unit driver cleanup, memory free, etc. at unload.
1279 */
1280static void __exit hfi1_mod_cleanup(void)
1281{
1282 pci_unregister_driver(&hfi1_pci_driver);
Dennis Dalessandro41973442016-07-25 07:52:36 -07001283 node_affinity_destroy();
Dean Luick528ee9f2016-03-05 08:50:43 -08001284 hfi1_wss_exit();
Mike Marciniszyn77241052015-07-30 15:17:43 -04001285 hfi1_dbg_exit();
1286 hfi1_cpulist_count = 0;
1287 kfree(hfi1_cpulist);
1288
1289 idr_destroy(&hfi1_unit_table);
1290 dispose_firmware(); /* asymmetric with obtain_firmware() */
1291 dev_cleanup();
1292}
1293
1294module_exit(hfi1_mod_cleanup);
1295
1296/* this can only be called after a successful initialization */
1297static void cleanup_device_data(struct hfi1_devdata *dd)
1298{
1299 int ctxt;
1300 int pidx;
1301 struct hfi1_ctxtdata **tmp;
1302 unsigned long flags;
1303
1304 /* users can't do anything more with chip */
1305 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
1306 struct hfi1_pportdata *ppd = &dd->pport[pidx];
1307 struct cc_state *cc_state;
1308 int i;
1309
1310 if (ppd->statusp)
1311 *ppd->statusp &= ~HFI1_STATUS_CHIP_PRESENT;
1312
1313 for (i = 0; i < OPA_MAX_SLS; i++)
1314 hrtimer_cancel(&ppd->cca_timer[i].hrtimer);
1315
1316 spin_lock(&ppd->cc_state_lock);
1317 cc_state = get_cc_state(ppd);
Muhammad Falak R Wanieea57072016-05-01 18:05:31 +05301318 RCU_INIT_POINTER(ppd->cc_state, NULL);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001319 spin_unlock(&ppd->cc_state_lock);
1320
1321 if (cc_state)
1322 call_rcu(&cc_state->rcu, cc_state_reclaim);
1323 }
1324
1325 free_credit_return(dd);
1326
1327 /*
1328 * Free any resources still in use (usually just kernel contexts)
1329 * at unload; we do for ctxtcnt, because that's what we allocate.
1330 * We acquire lock to be really paranoid that rcd isn't being
1331 * accessed from some interrupt-related code (that should not happen,
1332 * but best to be sure).
1333 */
1334 spin_lock_irqsave(&dd->uctxt_lock, flags);
1335 tmp = dd->rcd;
1336 dd->rcd = NULL;
1337 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
Mark F. Brown46b010d2015-11-09 19:18:20 -05001338
1339 if (dd->rcvhdrtail_dummy_kvaddr) {
1340 dma_free_coherent(&dd->pcidev->dev, sizeof(u64),
1341 (void *)dd->rcvhdrtail_dummy_kvaddr,
1342 dd->rcvhdrtail_dummy_physaddr);
Dan Carpentera8b7da52016-05-28 08:01:20 +03001343 dd->rcvhdrtail_dummy_kvaddr = NULL;
Mark F. Brown46b010d2015-11-09 19:18:20 -05001344 }
1345
Mike Marciniszyn77241052015-07-30 15:17:43 -04001346 for (ctxt = 0; tmp && ctxt < dd->num_rcv_contexts; ctxt++) {
1347 struct hfi1_ctxtdata *rcd = tmp[ctxt];
1348
1349 tmp[ctxt] = NULL; /* debugging paranoia */
1350 if (rcd) {
1351 hfi1_clear_tids(rcd);
1352 hfi1_free_ctxtdata(dd, rcd);
1353 }
1354 }
1355 kfree(tmp);
Jubin John35f6bef2016-02-14 12:46:10 -08001356 free_pio_map(dd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001357 /* must follow rcv context free - need to remove rcv's hooks */
1358 for (ctxt = 0; ctxt < dd->num_send_contexts; ctxt++)
1359 sc_free(dd->send_contexts[ctxt].sc);
1360 dd->num_send_contexts = 0;
1361 kfree(dd->send_contexts);
1362 dd->send_contexts = NULL;
Jubin John79d0c082016-02-26 13:33:33 -08001363 kfree(dd->hw_to_sw);
1364 dd->hw_to_sw = NULL;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001365 kfree(dd->boardname);
1366 vfree(dd->events);
1367 vfree(dd->status);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001368}
1369
1370/*
1371 * Clean up on unit shutdown, or error during unit load after
1372 * successful initialization.
1373 */
1374static void postinit_cleanup(struct hfi1_devdata *dd)
1375{
1376 hfi1_start_cleanup(dd);
1377
1378 hfi1_pcie_ddcleanup(dd);
1379 hfi1_pcie_cleanup(dd->pcidev);
1380
1381 cleanup_device_data(dd);
1382
1383 hfi1_free_devdata(dd);
1384}
1385
1386static int init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
1387{
1388 int ret = 0, j, pidx, initfail;
Tadeusz Struk93dd0a02016-06-09 07:51:45 -07001389 struct hfi1_devdata *dd = ERR_PTR(-EINVAL);
Harish Chegondie8597eb2015-12-01 15:38:20 -05001390 struct hfi1_pportdata *ppd;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001391
1392 /* First, lock the non-writable module parameters */
1393 HFI1_CAP_LOCK();
1394
1395 /* Validate some global module parameters */
1396 if (rcvhdrcnt <= HFI1_MIN_HDRQ_EGRBUF_CNT) {
1397 hfi1_early_err(&pdev->dev, "Header queue count too small\n");
1398 ret = -EINVAL;
1399 goto bail;
1400 }
Sebastian Sancheze002dcc2016-02-03 14:34:32 -08001401 if (rcvhdrcnt > HFI1_MAX_HDRQ_EGRBUF_CNT) {
1402 hfi1_early_err(&pdev->dev,
1403 "Receive header queue count cannot be greater than %u\n",
1404 HFI1_MAX_HDRQ_EGRBUF_CNT);
1405 ret = -EINVAL;
1406 goto bail;
1407 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001408 /* use the encoding function as a sanitization check */
1409 if (!encode_rcv_header_entry_size(hfi1_hdrq_entsize)) {
1410 hfi1_early_err(&pdev->dev, "Invalid HdrQ Entry size %u\n",
1411 hfi1_hdrq_entsize);
Sebastian Sanchez07859de2015-12-10 16:02:49 -05001412 ret = -EINVAL;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001413 goto bail;
1414 }
1415
1416 /* The receive eager buffer size must be set before the receive
1417 * contexts are created.
1418 *
1419 * Set the eager buffer size. Validate that it falls in a range
1420 * allowed by the hardware - all powers of 2 between the min and
1421 * max. The maximum valid MTU is within the eager buffer range
1422 * so we do not need to cap the max_mtu by an eager buffer size
1423 * setting.
1424 */
1425 if (eager_buffer_size) {
1426 if (!is_power_of_2(eager_buffer_size))
1427 eager_buffer_size =
1428 roundup_pow_of_two(eager_buffer_size);
1429 eager_buffer_size =
1430 clamp_val(eager_buffer_size,
1431 MIN_EAGER_BUFFER * 8,
1432 MAX_EAGER_BUFFER_TOTAL);
1433 hfi1_early_info(&pdev->dev, "Eager buffer size %u\n",
1434 eager_buffer_size);
1435 } else {
1436 hfi1_early_err(&pdev->dev, "Invalid Eager buffer size of 0\n");
1437 ret = -EINVAL;
1438 goto bail;
1439 }
1440
1441 /* restrict value of hfi1_rcvarr_split */
1442 hfi1_rcvarr_split = clamp_val(hfi1_rcvarr_split, 0, 100);
1443
1444 ret = hfi1_pcie_init(pdev, ent);
1445 if (ret)
1446 goto bail;
1447
1448 /*
1449 * Do device-specific initialization, function table setup, dd
1450 * allocation, etc.
1451 */
1452 switch (ent->device) {
1453 case PCI_DEVICE_ID_INTEL0:
1454 case PCI_DEVICE_ID_INTEL1:
1455 dd = hfi1_init_dd(pdev, ent);
1456 break;
1457 default:
1458 hfi1_early_err(&pdev->dev,
1459 "Failing on unknown Intel deviceid 0x%x\n",
1460 ent->device);
1461 ret = -ENODEV;
1462 }
1463
1464 if (IS_ERR(dd))
1465 ret = PTR_ERR(dd);
1466 if (ret)
1467 goto clean_bail; /* error already printed */
1468
1469 ret = create_workqueues(dd);
1470 if (ret)
1471 goto clean_bail;
1472
1473 /* do the generic initialization */
1474 initfail = hfi1_init(dd, 0);
1475
1476 ret = hfi1_register_ib_device(dd);
1477
1478 /*
1479 * Now ready for use. this should be cleared whenever we
1480 * detect a reset, or initiate one. If earlier failure,
1481 * we still create devices, so diags, etc. can be used
1482 * to determine cause of problem.
1483 */
Dean Luicked6f6532016-02-18 11:12:25 -08001484 if (!initfail && !ret) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001485 dd->flags |= HFI1_INITTED;
Dean Luicked6f6532016-02-18 11:12:25 -08001486 /* create debufs files after init and ib register */
1487 hfi1_dbg_ibdev_init(&dd->verbs_dev);
1488 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001489
1490 j = hfi1_device_create(dd);
1491 if (j)
1492 dd_dev_err(dd, "Failed to create /dev devices: %d\n", -j);
1493
1494 if (initfail || ret) {
1495 stop_timers(dd);
1496 flush_workqueue(ib_wq);
Harish Chegondie8597eb2015-12-01 15:38:20 -05001497 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001498 hfi1_quiet_serdes(dd->pport + pidx);
Harish Chegondie8597eb2015-12-01 15:38:20 -05001499 ppd = dd->pport + pidx;
1500 if (ppd->hfi1_wq) {
1501 destroy_workqueue(ppd->hfi1_wq);
1502 ppd->hfi1_wq = NULL;
1503 }
1504 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001505 if (!j)
1506 hfi1_device_remove(dd);
1507 if (!ret)
1508 hfi1_unregister_ib_device(dd);
1509 postinit_cleanup(dd);
1510 if (initfail)
1511 ret = initfail;
1512 goto bail; /* everything already cleaned */
1513 }
1514
1515 sdma_start(dd);
1516
1517 return 0;
1518
1519clean_bail:
1520 hfi1_pcie_cleanup(pdev);
1521bail:
1522 return ret;
1523}
1524
1525static void remove_one(struct pci_dev *pdev)
1526{
1527 struct hfi1_devdata *dd = pci_get_drvdata(pdev);
1528
Dean Luicked6f6532016-02-18 11:12:25 -08001529 /* close debugfs files before ib unregister */
1530 hfi1_dbg_ibdev_exit(&dd->verbs_dev);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001531 /* unregister from IB core */
1532 hfi1_unregister_ib_device(dd);
1533
1534 /*
1535 * Disable the IB link, disable interrupts on the device,
1536 * clear dma engines, etc.
1537 */
1538 shutdown_device(dd);
1539
1540 stop_timers(dd);
1541
1542 /* wait until all of our (qsfp) queue_work() calls complete */
1543 flush_workqueue(ib_wq);
1544
1545 hfi1_device_remove(dd);
1546
1547 postinit_cleanup(dd);
1548}
1549
1550/**
1551 * hfi1_create_rcvhdrq - create a receive header queue
1552 * @dd: the hfi1_ib device
1553 * @rcd: the context data
1554 *
1555 * This must be contiguous memory (from an i/o perspective), and must be
1556 * DMA'able (which means for some systems, it will go through an IOMMU,
1557 * or be forced into a low address range).
1558 */
1559int hfi1_create_rcvhdrq(struct hfi1_devdata *dd, struct hfi1_ctxtdata *rcd)
1560{
1561 unsigned amt;
1562 u64 reg;
1563
1564 if (!rcd->rcvhdrq) {
1565 dma_addr_t phys_hdrqtail;
1566 gfp_t gfp_flags;
1567
1568 /*
1569 * rcvhdrqentsize is in DWs, so we have to convert to bytes
1570 * (* sizeof(u32)).
1571 */
Amitoj Kaur Chawla84449912016-03-04 22:30:43 +05301572 amt = PAGE_ALIGN(rcd->rcvhdrq_cnt * rcd->rcvhdrqentsize *
1573 sizeof(u32));
Mike Marciniszyn77241052015-07-30 15:17:43 -04001574
1575 gfp_flags = (rcd->ctxt >= dd->first_user_ctxt) ?
1576 GFP_USER : GFP_KERNEL;
1577 rcd->rcvhdrq = dma_zalloc_coherent(
1578 &dd->pcidev->dev, amt, &rcd->rcvhdrq_phys,
1579 gfp_flags | __GFP_COMP);
1580
1581 if (!rcd->rcvhdrq) {
1582 dd_dev_err(dd,
Jubin John17fb4f22016-02-14 20:21:52 -08001583 "attempt to allocate %d bytes for ctxt %u rcvhdrq failed\n",
1584 amt, rcd->ctxt);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001585 goto bail;
1586 }
1587
Mike Marciniszyn77241052015-07-30 15:17:43 -04001588 if (HFI1_CAP_KGET_MASK(rcd->flags, DMA_RTAIL)) {
1589 rcd->rcvhdrtail_kvaddr = dma_zalloc_coherent(
1590 &dd->pcidev->dev, PAGE_SIZE, &phys_hdrqtail,
1591 gfp_flags);
1592 if (!rcd->rcvhdrtail_kvaddr)
1593 goto bail_free;
1594 rcd->rcvhdrqtailaddr_phys = phys_hdrqtail;
1595 }
1596
1597 rcd->rcvhdrq_size = amt;
1598 }
1599 /*
1600 * These values are per-context:
1601 * RcvHdrCnt
1602 * RcvHdrEntSize
1603 * RcvHdrSize
1604 */
1605 reg = ((u64)(rcd->rcvhdrq_cnt >> HDRQ_SIZE_SHIFT)
1606 & RCV_HDR_CNT_CNT_MASK)
1607 << RCV_HDR_CNT_CNT_SHIFT;
1608 write_kctxt_csr(dd, rcd->ctxt, RCV_HDR_CNT, reg);
1609 reg = (encode_rcv_header_entry_size(rcd->rcvhdrqentsize)
1610 & RCV_HDR_ENT_SIZE_ENT_SIZE_MASK)
1611 << RCV_HDR_ENT_SIZE_ENT_SIZE_SHIFT;
1612 write_kctxt_csr(dd, rcd->ctxt, RCV_HDR_ENT_SIZE, reg);
1613 reg = (dd->rcvhdrsize & RCV_HDR_SIZE_HDR_SIZE_MASK)
1614 << RCV_HDR_SIZE_HDR_SIZE_SHIFT;
1615 write_kctxt_csr(dd, rcd->ctxt, RCV_HDR_SIZE, reg);
Mark F. Brown46b010d2015-11-09 19:18:20 -05001616
1617 /*
1618 * Program dummy tail address for every receive context
1619 * before enabling any receive context
1620 */
1621 write_kctxt_csr(dd, rcd->ctxt, RCV_HDR_TAIL_ADDR,
1622 dd->rcvhdrtail_dummy_physaddr);
1623
Mike Marciniszyn77241052015-07-30 15:17:43 -04001624 return 0;
1625
1626bail_free:
1627 dd_dev_err(dd,
Jubin John17fb4f22016-02-14 20:21:52 -08001628 "attempt to allocate 1 page for ctxt %u rcvhdrqtailaddr failed\n",
1629 rcd->ctxt);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001630 vfree(rcd->user_event_mask);
1631 rcd->user_event_mask = NULL;
1632 dma_free_coherent(&dd->pcidev->dev, amt, rcd->rcvhdrq,
1633 rcd->rcvhdrq_phys);
1634 rcd->rcvhdrq = NULL;
1635bail:
1636 return -ENOMEM;
1637}
1638
1639/**
1640 * allocate eager buffers, both kernel and user contexts.
1641 * @rcd: the context we are setting up.
1642 *
1643 * Allocate the eager TID buffers and program them into hip.
1644 * They are no longer completely contiguous, we do multiple allocation
1645 * calls. Otherwise we get the OOM code involved, by asking for too
1646 * much per call, with disastrous results on some kernels.
1647 */
1648int hfi1_setup_eagerbufs(struct hfi1_ctxtdata *rcd)
1649{
1650 struct hfi1_devdata *dd = rcd->dd;
1651 u32 max_entries, egrtop, alloced_bytes = 0, idx = 0;
1652 gfp_t gfp_flags;
1653 u16 order;
1654 int ret = 0;
1655 u16 round_mtu = roundup_pow_of_two(hfi1_max_mtu);
1656
1657 /*
1658 * GFP_USER, but without GFP_FS, so buffer cache can be
1659 * coalesced (we hope); otherwise, even at order 4,
1660 * heavy filesystem activity makes these fail, and we can
1661 * use compound pages.
1662 */
Mel Gorman71baba42015-11-06 16:28:28 -08001663 gfp_flags = __GFP_RECLAIM | __GFP_IO | __GFP_COMP;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001664
1665 /*
1666 * The minimum size of the eager buffers is a groups of MTU-sized
1667 * buffers.
1668 * The global eager_buffer_size parameter is checked against the
1669 * theoretical lower limit of the value. Here, we check against the
1670 * MTU.
1671 */
1672 if (rcd->egrbufs.size < (round_mtu * dd->rcv_entries.group_size))
1673 rcd->egrbufs.size = round_mtu * dd->rcv_entries.group_size;
1674 /*
1675 * If using one-pkt-per-egr-buffer, lower the eager buffer
1676 * size to the max MTU (page-aligned).
1677 */
1678 if (!HFI1_CAP_KGET_MASK(rcd->flags, MULTI_PKT_EGR))
1679 rcd->egrbufs.rcvtid_size = round_mtu;
1680
1681 /*
1682 * Eager buffers sizes of 1MB or less require smaller TID sizes
1683 * to satisfy the "multiple of 8 RcvArray entries" requirement.
1684 */
1685 if (rcd->egrbufs.size <= (1 << 20))
1686 rcd->egrbufs.rcvtid_size = max((unsigned long)round_mtu,
1687 rounddown_pow_of_two(rcd->egrbufs.size / 8));
1688
1689 while (alloced_bytes < rcd->egrbufs.size &&
1690 rcd->egrbufs.alloced < rcd->egrbufs.count) {
1691 rcd->egrbufs.buffers[idx].addr =
1692 dma_zalloc_coherent(&dd->pcidev->dev,
1693 rcd->egrbufs.rcvtid_size,
1694 &rcd->egrbufs.buffers[idx].phys,
1695 gfp_flags);
1696 if (rcd->egrbufs.buffers[idx].addr) {
1697 rcd->egrbufs.buffers[idx].len =
1698 rcd->egrbufs.rcvtid_size;
1699 rcd->egrbufs.rcvtids[rcd->egrbufs.alloced].addr =
1700 rcd->egrbufs.buffers[idx].addr;
1701 rcd->egrbufs.rcvtids[rcd->egrbufs.alloced].phys =
1702 rcd->egrbufs.buffers[idx].phys;
1703 rcd->egrbufs.alloced++;
1704 alloced_bytes += rcd->egrbufs.rcvtid_size;
1705 idx++;
1706 } else {
1707 u32 new_size, i, j;
1708 u64 offset = 0;
1709
1710 /*
1711 * Fail the eager buffer allocation if:
1712 * - we are already using the lowest acceptable size
1713 * - we are using one-pkt-per-egr-buffer (this implies
1714 * that we are accepting only one size)
1715 */
1716 if (rcd->egrbufs.rcvtid_size == round_mtu ||
1717 !HFI1_CAP_KGET_MASK(rcd->flags, MULTI_PKT_EGR)) {
1718 dd_dev_err(dd, "ctxt%u: Failed to allocate eager buffers\n",
Jubin John17fb4f22016-02-14 20:21:52 -08001719 rcd->ctxt);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001720 goto bail_rcvegrbuf_phys;
1721 }
1722
1723 new_size = rcd->egrbufs.rcvtid_size / 2;
1724
1725 /*
1726 * If the first attempt to allocate memory failed, don't
1727 * fail everything but continue with the next lower
1728 * size.
1729 */
1730 if (idx == 0) {
1731 rcd->egrbufs.rcvtid_size = new_size;
1732 continue;
1733 }
1734
1735 /*
1736 * Re-partition already allocated buffers to a smaller
1737 * size.
1738 */
1739 rcd->egrbufs.alloced = 0;
1740 for (i = 0, j = 0, offset = 0; j < idx; i++) {
1741 if (i >= rcd->egrbufs.count)
1742 break;
1743 rcd->egrbufs.rcvtids[i].phys =
1744 rcd->egrbufs.buffers[j].phys + offset;
1745 rcd->egrbufs.rcvtids[i].addr =
1746 rcd->egrbufs.buffers[j].addr + offset;
1747 rcd->egrbufs.alloced++;
1748 if ((rcd->egrbufs.buffers[j].phys + offset +
1749 new_size) ==
1750 (rcd->egrbufs.buffers[j].phys +
1751 rcd->egrbufs.buffers[j].len)) {
1752 j++;
1753 offset = 0;
Jubin Johne4909742016-02-14 20:22:00 -08001754 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001755 offset += new_size;
Jubin Johne4909742016-02-14 20:22:00 -08001756 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001757 }
1758 rcd->egrbufs.rcvtid_size = new_size;
1759 }
1760 }
1761 rcd->egrbufs.numbufs = idx;
1762 rcd->egrbufs.size = alloced_bytes;
1763
Sebastian Sanchez6c63e422015-11-06 20:06:56 -05001764 hfi1_cdbg(PROC,
1765 "ctxt%u: Alloced %u rcv tid entries @ %uKB, total %zuKB\n",
1766 rcd->ctxt, rcd->egrbufs.alloced, rcd->egrbufs.rcvtid_size,
1767 rcd->egrbufs.size);
1768
Mike Marciniszyn77241052015-07-30 15:17:43 -04001769 /*
1770 * Set the contexts rcv array head update threshold to the closest
1771 * power of 2 (so we can use a mask instead of modulo) below half
1772 * the allocated entries.
1773 */
1774 rcd->egrbufs.threshold =
1775 rounddown_pow_of_two(rcd->egrbufs.alloced / 2);
1776 /*
1777 * Compute the expected RcvArray entry base. This is done after
1778 * allocating the eager buffers in order to maximize the
1779 * expected RcvArray entries for the context.
1780 */
1781 max_entries = rcd->rcv_array_groups * dd->rcv_entries.group_size;
1782 egrtop = roundup(rcd->egrbufs.alloced, dd->rcv_entries.group_size);
1783 rcd->expected_count = max_entries - egrtop;
1784 if (rcd->expected_count > MAX_TID_PAIR_ENTRIES * 2)
1785 rcd->expected_count = MAX_TID_PAIR_ENTRIES * 2;
1786
1787 rcd->expected_base = rcd->eager_base + egrtop;
Sebastian Sanchez6c63e422015-11-06 20:06:56 -05001788 hfi1_cdbg(PROC, "ctxt%u: eager:%u, exp:%u, egrbase:%u, expbase:%u\n",
1789 rcd->ctxt, rcd->egrbufs.alloced, rcd->expected_count,
1790 rcd->eager_base, rcd->expected_base);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001791
1792 if (!hfi1_rcvbuf_validate(rcd->egrbufs.rcvtid_size, PT_EAGER, &order)) {
Sebastian Sanchez6c63e422015-11-06 20:06:56 -05001793 hfi1_cdbg(PROC,
1794 "ctxt%u: current Eager buffer size is invalid %u\n",
1795 rcd->ctxt, rcd->egrbufs.rcvtid_size);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001796 ret = -EINVAL;
1797 goto bail;
1798 }
1799
1800 for (idx = 0; idx < rcd->egrbufs.alloced; idx++) {
1801 hfi1_put_tid(dd, rcd->eager_base + idx, PT_EAGER,
Jubin John17fb4f22016-02-14 20:21:52 -08001802 rcd->egrbufs.rcvtids[idx].phys, order);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001803 cond_resched();
1804 }
1805 goto bail;
1806
1807bail_rcvegrbuf_phys:
1808 for (idx = 0; idx < rcd->egrbufs.alloced &&
Jubin John17fb4f22016-02-14 20:21:52 -08001809 rcd->egrbufs.buffers[idx].addr;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001810 idx++) {
1811 dma_free_coherent(&dd->pcidev->dev,
1812 rcd->egrbufs.buffers[idx].len,
1813 rcd->egrbufs.buffers[idx].addr,
1814 rcd->egrbufs.buffers[idx].phys);
1815 rcd->egrbufs.buffers[idx].addr = NULL;
1816 rcd->egrbufs.buffers[idx].phys = 0;
1817 rcd->egrbufs.buffers[idx].len = 0;
1818 }
1819bail:
1820 return ret;
1821}