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