blob: 1256dbd5b2ef05aa7a14072c4e3f8578570973e0 [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001/*
2 * Copyright(c) 2015 - 2018 Intel Corporation.
3 *
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 *
9 * 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 *
20 * 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>
David Brazdil0f672f62019-12-10 10:32:29 +000052#include <linux/xarray.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000053#include <linux/module.h>
54#include <linux/printk.h>
55#include <linux/hrtimer.h>
56#include <linux/bitmap.h>
David Brazdil0f672f62019-12-10 10:32:29 +000057#include <linux/numa.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000058#include <rdma/rdma_vt.h>
59
60#include "hfi.h"
61#include "device.h"
62#include "common.h"
63#include "trace.h"
64#include "mad.h"
65#include "sdma.h"
66#include "debugfs.h"
67#include "verbs.h"
68#include "aspm.h"
69#include "affinity.h"
70#include "vnic.h"
71#include "exp_rcv.h"
72
73#undef pr_fmt
74#define pr_fmt(fmt) DRIVER_NAME ": " fmt
75
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000076/*
77 * min buffers we want to have per context, after driver
78 */
79#define HFI1_MIN_USER_CTXT_BUFCNT 7
80
81#define HFI1_MIN_HDRQ_EGRBUF_CNT 2
82#define HFI1_MAX_HDRQ_EGRBUF_CNT 16352
83#define HFI1_MIN_EAGER_BUFFER_SIZE (4 * 1024) /* 4KB */
84#define HFI1_MAX_EAGER_BUFFER_SIZE (256 * 1024) /* 256KB */
85
David Brazdil0f672f62019-12-10 10:32:29 +000086#define NUM_IB_PORTS 1
87
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000088/*
89 * Number of user receive contexts we are configured to use (to allow for more
90 * pio buffers per ctxt, etc.) Zero means use one user context per CPU.
91 */
92int num_user_contexts = -1;
93module_param_named(num_user_contexts, num_user_contexts, int, 0444);
94MODULE_PARM_DESC(
95 num_user_contexts, "Set max number of user contexts to use (default: -1 will use the real (non-HT) CPU count)");
96
97uint krcvqs[RXE_NUM_DATA_VL];
98int krcvqsset;
99module_param_array(krcvqs, uint, &krcvqsset, S_IRUGO);
100MODULE_PARM_DESC(krcvqs, "Array of the number of non-control kernel receive queues by VL");
101
102/* computed based on above array */
103unsigned long n_krcvqs;
104
105static unsigned hfi1_rcvarr_split = 25;
106module_param_named(rcvarr_split, hfi1_rcvarr_split, uint, S_IRUGO);
107MODULE_PARM_DESC(rcvarr_split, "Percent of context's RcvArray entries used for Eager buffers");
108
109static uint eager_buffer_size = (8 << 20); /* 8MB */
110module_param(eager_buffer_size, uint, S_IRUGO);
111MODULE_PARM_DESC(eager_buffer_size, "Size of the eager buffers, default: 8MB");
112
113static uint rcvhdrcnt = 2048; /* 2x the max eager buffer count */
114module_param_named(rcvhdrcnt, rcvhdrcnt, uint, S_IRUGO);
115MODULE_PARM_DESC(rcvhdrcnt, "Receive header queue count (default 2048)");
116
117static uint hfi1_hdrq_entsize = 32;
118module_param_named(hdrq_entsize, hfi1_hdrq_entsize, uint, 0444);
119MODULE_PARM_DESC(hdrq_entsize, "Size of header queue entries: 2 - 8B, 16 - 64B, 32 - 128B (default)");
120
121unsigned int user_credit_return_threshold = 33; /* default is 33% */
122module_param(user_credit_return_threshold, uint, S_IRUGO);
123MODULE_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)");
124
125static inline u64 encode_rcv_header_entry_size(u16 size);
126
David Brazdil0f672f62019-12-10 10:32:29 +0000127DEFINE_XARRAY_FLAGS(hfi1_dev_table, XA_FLAGS_ALLOC | XA_FLAGS_LOCK_IRQ);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000128
129static int hfi1_create_kctxt(struct hfi1_devdata *dd,
130 struct hfi1_pportdata *ppd)
131{
132 struct hfi1_ctxtdata *rcd;
133 int ret;
134
135 /* Control context has to be always 0 */
136 BUILD_BUG_ON(HFI1_CTRL_CTXT != 0);
137
138 ret = hfi1_create_ctxtdata(ppd, dd->node, &rcd);
139 if (ret < 0) {
140 dd_dev_err(dd, "Kernel receive context allocation failed\n");
141 return ret;
142 }
143
144 /*
145 * Set up the kernel context flags here and now because they use
146 * default values for all receive side memories. User contexts will
147 * be handled as they are created.
148 */
149 rcd->flags = HFI1_CAP_KGET(MULTI_PKT_EGR) |
150 HFI1_CAP_KGET(NODROP_RHQ_FULL) |
151 HFI1_CAP_KGET(NODROP_EGR_FULL) |
152 HFI1_CAP_KGET(DMA_RTAIL);
153
154 /* Control context must use DMA_RTAIL */
155 if (rcd->ctxt == HFI1_CTRL_CTXT)
156 rcd->flags |= HFI1_CAP_DMA_RTAIL;
157 rcd->seq_cnt = 1;
158
159 rcd->sc = sc_alloc(dd, SC_ACK, rcd->rcvhdrqentsize, dd->node);
160 if (!rcd->sc) {
161 dd_dev_err(dd, "Kernel send context allocation failed\n");
162 return -ENOMEM;
163 }
164 hfi1_init_ctxt(rcd->sc);
165
166 return 0;
167}
168
169/*
170 * Create the receive context array and one or more kernel contexts
171 */
172int hfi1_create_kctxts(struct hfi1_devdata *dd)
173{
174 u16 i;
175 int ret;
176
177 dd->rcd = kcalloc_node(dd->num_rcv_contexts, sizeof(*dd->rcd),
178 GFP_KERNEL, dd->node);
179 if (!dd->rcd)
180 return -ENOMEM;
181
182 for (i = 0; i < dd->first_dyn_alloc_ctxt; ++i) {
183 ret = hfi1_create_kctxt(dd, dd->pport);
184 if (ret)
185 goto bail;
186 }
187
188 return 0;
189bail:
190 for (i = 0; dd->rcd && i < dd->first_dyn_alloc_ctxt; ++i)
191 hfi1_free_ctxt(dd->rcd[i]);
192
193 /* All the contexts should be freed, free the array */
194 kfree(dd->rcd);
195 dd->rcd = NULL;
196 return ret;
197}
198
199/*
200 * Helper routines for the receive context reference count (rcd and uctxt).
201 */
202static void hfi1_rcd_init(struct hfi1_ctxtdata *rcd)
203{
204 kref_init(&rcd->kref);
205}
206
207/**
208 * hfi1_rcd_free - When reference is zero clean up.
209 * @kref: pointer to an initialized rcd data structure
210 *
211 */
212static void hfi1_rcd_free(struct kref *kref)
213{
214 unsigned long flags;
215 struct hfi1_ctxtdata *rcd =
216 container_of(kref, struct hfi1_ctxtdata, kref);
217
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000218 spin_lock_irqsave(&rcd->dd->uctxt_lock, flags);
219 rcd->dd->rcd[rcd->ctxt] = NULL;
220 spin_unlock_irqrestore(&rcd->dd->uctxt_lock, flags);
221
David Brazdil0f672f62019-12-10 10:32:29 +0000222 hfi1_free_ctxtdata(rcd->dd, rcd);
223
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000224 kfree(rcd);
225}
226
227/**
228 * hfi1_rcd_put - decrement reference for rcd
229 * @rcd: pointer to an initialized rcd data structure
230 *
231 * Use this to put a reference after the init.
232 */
233int hfi1_rcd_put(struct hfi1_ctxtdata *rcd)
234{
235 if (rcd)
236 return kref_put(&rcd->kref, hfi1_rcd_free);
237
238 return 0;
239}
240
241/**
242 * hfi1_rcd_get - increment reference for rcd
243 * @rcd: pointer to an initialized rcd data structure
244 *
245 * Use this to get a reference after the init.
David Brazdil0f672f62019-12-10 10:32:29 +0000246 *
247 * Return : reflect kref_get_unless_zero(), which returns non-zero on
248 * increment, otherwise 0.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000249 */
David Brazdil0f672f62019-12-10 10:32:29 +0000250int hfi1_rcd_get(struct hfi1_ctxtdata *rcd)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000251{
David Brazdil0f672f62019-12-10 10:32:29 +0000252 return kref_get_unless_zero(&rcd->kref);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000253}
254
255/**
256 * allocate_rcd_index - allocate an rcd index from the rcd array
257 * @dd: pointer to a valid devdata structure
258 * @rcd: rcd data structure to assign
259 * @index: pointer to index that is allocated
260 *
261 * Find an empty index in the rcd array, and assign the given rcd to it.
262 * If the array is full, we are EBUSY.
263 *
264 */
265static int allocate_rcd_index(struct hfi1_devdata *dd,
266 struct hfi1_ctxtdata *rcd, u16 *index)
267{
268 unsigned long flags;
269 u16 ctxt;
270
271 spin_lock_irqsave(&dd->uctxt_lock, flags);
272 for (ctxt = 0; ctxt < dd->num_rcv_contexts; ctxt++)
273 if (!dd->rcd[ctxt])
274 break;
275
276 if (ctxt < dd->num_rcv_contexts) {
277 rcd->ctxt = ctxt;
278 dd->rcd[ctxt] = rcd;
279 hfi1_rcd_init(rcd);
280 }
281 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
282
283 if (ctxt >= dd->num_rcv_contexts)
284 return -EBUSY;
285
286 *index = ctxt;
287
288 return 0;
289}
290
291/**
292 * hfi1_rcd_get_by_index_safe - validate the ctxt index before accessing the
293 * array
294 * @dd: pointer to a valid devdata structure
295 * @ctxt: the index of an possilbe rcd
296 *
297 * This is a wrapper for hfi1_rcd_get_by_index() to validate that the given
298 * ctxt index is valid.
299 *
300 * The caller is responsible for making the _put().
301 *
302 */
303struct hfi1_ctxtdata *hfi1_rcd_get_by_index_safe(struct hfi1_devdata *dd,
304 u16 ctxt)
305{
306 if (ctxt < dd->num_rcv_contexts)
307 return hfi1_rcd_get_by_index(dd, ctxt);
308
309 return NULL;
310}
311
312/**
313 * hfi1_rcd_get_by_index
314 * @dd: pointer to a valid devdata structure
315 * @ctxt: the index of an possilbe rcd
316 *
317 * We need to protect access to the rcd array. If access is needed to
318 * one or more index, get the protecting spinlock and then increment the
319 * kref.
320 *
321 * The caller is responsible for making the _put().
322 *
323 */
324struct hfi1_ctxtdata *hfi1_rcd_get_by_index(struct hfi1_devdata *dd, u16 ctxt)
325{
326 unsigned long flags;
327 struct hfi1_ctxtdata *rcd = NULL;
328
329 spin_lock_irqsave(&dd->uctxt_lock, flags);
330 if (dd->rcd[ctxt]) {
331 rcd = dd->rcd[ctxt];
David Brazdil0f672f62019-12-10 10:32:29 +0000332 if (!hfi1_rcd_get(rcd))
333 rcd = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000334 }
335 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
336
337 return rcd;
338}
339
340/*
341 * Common code for user and kernel context create and setup.
342 * NOTE: the initial kref is done here (hf1_rcd_init()).
343 */
344int hfi1_create_ctxtdata(struct hfi1_pportdata *ppd, int numa,
345 struct hfi1_ctxtdata **context)
346{
347 struct hfi1_devdata *dd = ppd->dd;
348 struct hfi1_ctxtdata *rcd;
349 unsigned kctxt_ngroups = 0;
350 u32 base;
351
352 if (dd->rcv_entries.nctxt_extra >
353 dd->num_rcv_contexts - dd->first_dyn_alloc_ctxt)
354 kctxt_ngroups = (dd->rcv_entries.nctxt_extra -
355 (dd->num_rcv_contexts - dd->first_dyn_alloc_ctxt));
356 rcd = kzalloc_node(sizeof(*rcd), GFP_KERNEL, numa);
357 if (rcd) {
358 u32 rcvtids, max_entries;
359 u16 ctxt;
360 int ret;
361
362 ret = allocate_rcd_index(dd, rcd, &ctxt);
363 if (ret) {
364 *context = NULL;
365 kfree(rcd);
366 return ret;
367 }
368
369 INIT_LIST_HEAD(&rcd->qp_wait_list);
370 hfi1_exp_tid_group_init(rcd);
371 rcd->ppd = ppd;
372 rcd->dd = dd;
373 rcd->numa_id = numa;
374 rcd->rcv_array_groups = dd->rcv_entries.ngroups;
375 rcd->rhf_rcv_function_map = normal_rhf_rcv_functions;
376
377 mutex_init(&rcd->exp_mutex);
David Brazdil0f672f62019-12-10 10:32:29 +0000378 spin_lock_init(&rcd->exp_lock);
379 INIT_LIST_HEAD(&rcd->flow_queue.queue_head);
380 INIT_LIST_HEAD(&rcd->rarr_queue.queue_head);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000381
382 hfi1_cdbg(PROC, "setting up context %u\n", rcd->ctxt);
383
384 /*
385 * Calculate the context's RcvArray entry starting point.
386 * We do this here because we have to take into account all
387 * the RcvArray entries that previous context would have
388 * taken and we have to account for any extra groups assigned
389 * to the static (kernel) or dynamic (vnic/user) contexts.
390 */
391 if (ctxt < dd->first_dyn_alloc_ctxt) {
392 if (ctxt < kctxt_ngroups) {
393 base = ctxt * (dd->rcv_entries.ngroups + 1);
394 rcd->rcv_array_groups++;
395 } else {
396 base = kctxt_ngroups +
397 (ctxt * dd->rcv_entries.ngroups);
398 }
399 } else {
400 u16 ct = ctxt - dd->first_dyn_alloc_ctxt;
401
402 base = ((dd->n_krcv_queues * dd->rcv_entries.ngroups) +
403 kctxt_ngroups);
404 if (ct < dd->rcv_entries.nctxt_extra) {
405 base += ct * (dd->rcv_entries.ngroups + 1);
406 rcd->rcv_array_groups++;
407 } else {
408 base += dd->rcv_entries.nctxt_extra +
409 (ct * dd->rcv_entries.ngroups);
410 }
411 }
412 rcd->eager_base = base * dd->rcv_entries.group_size;
413
414 rcd->rcvhdrq_cnt = rcvhdrcnt;
415 rcd->rcvhdrqentsize = hfi1_hdrq_entsize;
416 rcd->rhf_offset =
417 rcd->rcvhdrqentsize - sizeof(u64) / sizeof(u32);
418 /*
419 * Simple Eager buffer allocation: we have already pre-allocated
420 * the number of RcvArray entry groups. Each ctxtdata structure
421 * holds the number of groups for that context.
422 *
423 * To follow CSR requirements and maintain cacheline alignment,
424 * make sure all sizes and bases are multiples of group_size.
425 *
426 * The expected entry count is what is left after assigning
427 * eager.
428 */
429 max_entries = rcd->rcv_array_groups *
430 dd->rcv_entries.group_size;
431 rcvtids = ((max_entries * hfi1_rcvarr_split) / 100);
432 rcd->egrbufs.count = round_down(rcvtids,
433 dd->rcv_entries.group_size);
434 if (rcd->egrbufs.count > MAX_EAGER_ENTRIES) {
435 dd_dev_err(dd, "ctxt%u: requested too many RcvArray entries.\n",
436 rcd->ctxt);
437 rcd->egrbufs.count = MAX_EAGER_ENTRIES;
438 }
439 hfi1_cdbg(PROC,
440 "ctxt%u: max Eager buffer RcvArray entries: %u\n",
441 rcd->ctxt, rcd->egrbufs.count);
442
443 /*
444 * Allocate array that will hold the eager buffer accounting
445 * data.
446 * This will allocate the maximum possible buffer count based
447 * on the value of the RcvArray split parameter.
448 * The resulting value will be rounded down to the closest
449 * multiple of dd->rcv_entries.group_size.
450 */
451 rcd->egrbufs.buffers =
452 kcalloc_node(rcd->egrbufs.count,
453 sizeof(*rcd->egrbufs.buffers),
454 GFP_KERNEL, numa);
455 if (!rcd->egrbufs.buffers)
456 goto bail;
457 rcd->egrbufs.rcvtids =
458 kcalloc_node(rcd->egrbufs.count,
459 sizeof(*rcd->egrbufs.rcvtids),
460 GFP_KERNEL, numa);
461 if (!rcd->egrbufs.rcvtids)
462 goto bail;
463 rcd->egrbufs.size = eager_buffer_size;
464 /*
465 * The size of the buffers programmed into the RcvArray
466 * entries needs to be big enough to handle the highest
467 * MTU supported.
468 */
469 if (rcd->egrbufs.size < hfi1_max_mtu) {
470 rcd->egrbufs.size = __roundup_pow_of_two(hfi1_max_mtu);
471 hfi1_cdbg(PROC,
David Brazdil0f672f62019-12-10 10:32:29 +0000472 "ctxt%u: eager bufs size too small. Adjusting to %u\n",
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000473 rcd->ctxt, rcd->egrbufs.size);
474 }
475 rcd->egrbufs.rcvtid_size = HFI1_MAX_EAGER_BUFFER_SIZE;
476
477 /* Applicable only for statically created kernel contexts */
478 if (ctxt < dd->first_dyn_alloc_ctxt) {
479 rcd->opstats = kzalloc_node(sizeof(*rcd->opstats),
480 GFP_KERNEL, numa);
481 if (!rcd->opstats)
482 goto bail;
David Brazdil0f672f62019-12-10 10:32:29 +0000483
484 /* Initialize TID flow generations for the context */
485 hfi1_kern_init_ctxt_generations(rcd);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000486 }
487
488 *context = rcd;
489 return 0;
490 }
491
492bail:
493 *context = NULL;
494 hfi1_free_ctxt(rcd);
495 return -ENOMEM;
496}
497
498/**
499 * hfi1_free_ctxt
500 * @rcd: pointer to an initialized rcd data structure
501 *
502 * This wrapper is the free function that matches hfi1_create_ctxtdata().
503 * When a context is done being used (kernel or user), this function is called
504 * for the "final" put to match the kref init from hf1i_create_ctxtdata().
505 * Other users of the context do a get/put sequence to make sure that the
506 * structure isn't removed while in use.
507 */
508void hfi1_free_ctxt(struct hfi1_ctxtdata *rcd)
509{
510 hfi1_rcd_put(rcd);
511}
512
513/*
514 * Convert a receive header entry size that to the encoding used in the CSR.
515 *
516 * Return a zero if the given size is invalid.
517 */
518static inline u64 encode_rcv_header_entry_size(u16 size)
519{
520 /* there are only 3 valid receive header entry sizes */
521 if (size == 2)
522 return 1;
523 if (size == 16)
524 return 2;
525 else if (size == 32)
526 return 4;
527 return 0; /* invalid */
528}
529
530/*
531 * Select the largest ccti value over all SLs to determine the intra-
532 * packet gap for the link.
533 *
534 * called with cca_timer_lock held (to protect access to cca_timer
535 * array), and rcu_read_lock() (to protect access to cc_state).
536 */
537void set_link_ipg(struct hfi1_pportdata *ppd)
538{
539 struct hfi1_devdata *dd = ppd->dd;
540 struct cc_state *cc_state;
541 int i;
542 u16 cce, ccti_limit, max_ccti = 0;
543 u16 shift, mult;
544 u64 src;
545 u32 current_egress_rate; /* Mbits /sec */
546 u32 max_pkt_time;
547 /*
548 * max_pkt_time is the maximum packet egress time in units
549 * of the fabric clock period 1/(805 MHz).
550 */
551
552 cc_state = get_cc_state(ppd);
553
554 if (!cc_state)
555 /*
556 * This should _never_ happen - rcu_read_lock() is held,
557 * and set_link_ipg() should not be called if cc_state
558 * is NULL.
559 */
560 return;
561
562 for (i = 0; i < OPA_MAX_SLS; i++) {
563 u16 ccti = ppd->cca_timer[i].ccti;
564
565 if (ccti > max_ccti)
566 max_ccti = ccti;
567 }
568
569 ccti_limit = cc_state->cct.ccti_limit;
570 if (max_ccti > ccti_limit)
571 max_ccti = ccti_limit;
572
573 cce = cc_state->cct.entries[max_ccti].entry;
574 shift = (cce & 0xc000) >> 14;
575 mult = (cce & 0x3fff);
576
577 current_egress_rate = active_egress_rate(ppd);
578
579 max_pkt_time = egress_cycles(ppd->ibmaxlen, current_egress_rate);
580
581 src = (max_pkt_time >> shift) * mult;
582
583 src &= SEND_STATIC_RATE_CONTROL_CSR_SRC_RELOAD_SMASK;
584 src <<= SEND_STATIC_RATE_CONTROL_CSR_SRC_RELOAD_SHIFT;
585
586 write_csr(dd, SEND_STATIC_RATE_CONTROL, src);
587}
588
589static enum hrtimer_restart cca_timer_fn(struct hrtimer *t)
590{
591 struct cca_timer *cca_timer;
592 struct hfi1_pportdata *ppd;
593 int sl;
594 u16 ccti_timer, ccti_min;
595 struct cc_state *cc_state;
596 unsigned long flags;
597 enum hrtimer_restart ret = HRTIMER_NORESTART;
598
599 cca_timer = container_of(t, struct cca_timer, hrtimer);
600 ppd = cca_timer->ppd;
601 sl = cca_timer->sl;
602
603 rcu_read_lock();
604
605 cc_state = get_cc_state(ppd);
606
607 if (!cc_state) {
608 rcu_read_unlock();
609 return HRTIMER_NORESTART;
610 }
611
612 /*
613 * 1) decrement ccti for SL
614 * 2) calculate IPG for link (set_link_ipg())
615 * 3) restart timer, unless ccti is at min value
616 */
617
618 ccti_min = cc_state->cong_setting.entries[sl].ccti_min;
619 ccti_timer = cc_state->cong_setting.entries[sl].ccti_timer;
620
621 spin_lock_irqsave(&ppd->cca_timer_lock, flags);
622
623 if (cca_timer->ccti > ccti_min) {
624 cca_timer->ccti--;
625 set_link_ipg(ppd);
626 }
627
628 if (cca_timer->ccti > ccti_min) {
629 unsigned long nsec = 1024 * ccti_timer;
630 /* ccti_timer is in units of 1.024 usec */
631 hrtimer_forward_now(t, ns_to_ktime(nsec));
632 ret = HRTIMER_RESTART;
633 }
634
635 spin_unlock_irqrestore(&ppd->cca_timer_lock, flags);
636 rcu_read_unlock();
637 return ret;
638}
639
640/*
641 * Common code for initializing the physical port structure.
642 */
643void hfi1_init_pportdata(struct pci_dev *pdev, struct hfi1_pportdata *ppd,
644 struct hfi1_devdata *dd, u8 hw_pidx, u8 port)
645{
646 int i;
647 uint default_pkey_idx;
648 struct cc_state *cc_state;
649
650 ppd->dd = dd;
651 ppd->hw_pidx = hw_pidx;
652 ppd->port = port; /* IB port number, not index */
653 ppd->prev_link_width = LINK_WIDTH_DEFAULT;
654 /*
655 * There are C_VL_COUNT number of PortVLXmitWait counters.
656 * Adding 1 to C_VL_COUNT to include the PortXmitWait counter.
657 */
658 for (i = 0; i < C_VL_COUNT + 1; i++) {
659 ppd->port_vl_xmit_wait_last[i] = 0;
660 ppd->vl_xmit_flit_cnt[i] = 0;
661 }
662
663 default_pkey_idx = 1;
664
665 ppd->pkeys[default_pkey_idx] = DEFAULT_P_KEY;
666 ppd->part_enforce |= HFI1_PART_ENFORCE_IN;
Olivier Deprez0e641232021-09-23 10:07:05 +0200667 ppd->pkeys[0] = 0x8001;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000668
669 INIT_WORK(&ppd->link_vc_work, handle_verify_cap);
670 INIT_WORK(&ppd->link_up_work, handle_link_up);
671 INIT_WORK(&ppd->link_down_work, handle_link_down);
672 INIT_WORK(&ppd->freeze_work, handle_freeze);
673 INIT_WORK(&ppd->link_downgrade_work, handle_link_downgrade);
674 INIT_WORK(&ppd->sma_message_work, handle_sma_message);
675 INIT_WORK(&ppd->link_bounce_work, handle_link_bounce);
676 INIT_DELAYED_WORK(&ppd->start_link_work, handle_start_link);
677 INIT_WORK(&ppd->linkstate_active_work, receive_interrupt_work);
678 INIT_WORK(&ppd->qsfp_info.qsfp_work, qsfp_event);
679
680 mutex_init(&ppd->hls_lock);
681 spin_lock_init(&ppd->qsfp_info.qsfp_lock);
682
683 ppd->qsfp_info.ppd = ppd;
684 ppd->sm_trap_qp = 0x0;
685 ppd->sa_qp = 0x1;
686
687 ppd->hfi1_wq = NULL;
688
689 spin_lock_init(&ppd->cca_timer_lock);
690
691 for (i = 0; i < OPA_MAX_SLS; i++) {
692 hrtimer_init(&ppd->cca_timer[i].hrtimer, CLOCK_MONOTONIC,
693 HRTIMER_MODE_REL);
694 ppd->cca_timer[i].ppd = ppd;
695 ppd->cca_timer[i].sl = i;
696 ppd->cca_timer[i].ccti = 0;
697 ppd->cca_timer[i].hrtimer.function = cca_timer_fn;
698 }
699
700 ppd->cc_max_table_entries = IB_CC_TABLE_CAP_DEFAULT;
701
702 spin_lock_init(&ppd->cc_state_lock);
703 spin_lock_init(&ppd->cc_log_lock);
704 cc_state = kzalloc(sizeof(*cc_state), GFP_KERNEL);
705 RCU_INIT_POINTER(ppd->cc_state, cc_state);
706 if (!cc_state)
707 goto bail;
708 return;
709
710bail:
David Brazdil0f672f62019-12-10 10:32:29 +0000711 dd_dev_err(dd, "Congestion Control Agent disabled for port %d\n", port);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000712}
713
714/*
715 * Do initialization for device that is only needed on
716 * first detect, not on resets.
717 */
718static int loadtime_init(struct hfi1_devdata *dd)
719{
720 return 0;
721}
722
723/**
724 * init_after_reset - re-initialize after a reset
725 * @dd: the hfi1_ib device
726 *
727 * sanity check at least some of the values after reset, and
728 * ensure no receive or transmit (explicitly, in case reset
729 * failed
730 */
731static int init_after_reset(struct hfi1_devdata *dd)
732{
733 int i;
734 struct hfi1_ctxtdata *rcd;
735 /*
736 * Ensure chip does no sends or receives, tail updates, or
737 * pioavail updates while we re-initialize. This is mostly
738 * for the driver data structures, not chip registers.
739 */
740 for (i = 0; i < dd->num_rcv_contexts; i++) {
741 rcd = hfi1_rcd_get_by_index(dd, i);
742 hfi1_rcvctrl(dd, HFI1_RCVCTRL_CTXT_DIS |
743 HFI1_RCVCTRL_INTRAVAIL_DIS |
744 HFI1_RCVCTRL_TAILUPD_DIS, rcd);
745 hfi1_rcd_put(rcd);
746 }
747 pio_send_control(dd, PSC_GLOBAL_DISABLE);
748 for (i = 0; i < dd->num_send_contexts; i++)
749 sc_disable(dd->send_contexts[i].sc);
750
751 return 0;
752}
753
754static void enable_chip(struct hfi1_devdata *dd)
755{
756 struct hfi1_ctxtdata *rcd;
757 u32 rcvmask;
758 u16 i;
759
760 /* enable PIO send */
761 pio_send_control(dd, PSC_GLOBAL_ENABLE);
762
763 /*
764 * Enable kernel ctxts' receive and receive interrupt.
765 * Other ctxts done as user opens and initializes them.
766 */
767 for (i = 0; i < dd->first_dyn_alloc_ctxt; ++i) {
768 rcd = hfi1_rcd_get_by_index(dd, i);
769 if (!rcd)
770 continue;
771 rcvmask = HFI1_RCVCTRL_CTXT_ENB | HFI1_RCVCTRL_INTRAVAIL_ENB;
772 rcvmask |= HFI1_CAP_KGET_MASK(rcd->flags, DMA_RTAIL) ?
773 HFI1_RCVCTRL_TAILUPD_ENB : HFI1_RCVCTRL_TAILUPD_DIS;
774 if (!HFI1_CAP_KGET_MASK(rcd->flags, MULTI_PKT_EGR))
775 rcvmask |= HFI1_RCVCTRL_ONE_PKT_EGR_ENB;
776 if (HFI1_CAP_KGET_MASK(rcd->flags, NODROP_RHQ_FULL))
777 rcvmask |= HFI1_RCVCTRL_NO_RHQ_DROP_ENB;
778 if (HFI1_CAP_KGET_MASK(rcd->flags, NODROP_EGR_FULL))
779 rcvmask |= HFI1_RCVCTRL_NO_EGR_DROP_ENB;
David Brazdil0f672f62019-12-10 10:32:29 +0000780 if (HFI1_CAP_IS_KSET(TID_RDMA))
781 rcvmask |= HFI1_RCVCTRL_TIDFLOW_ENB;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000782 hfi1_rcvctrl(dd, rcvmask, rcd);
783 sc_enable(rcd->sc);
784 hfi1_rcd_put(rcd);
785 }
786}
787
788/**
789 * create_workqueues - create per port workqueues
790 * @dd: the hfi1_ib device
791 */
792static int create_workqueues(struct hfi1_devdata *dd)
793{
794 int pidx;
795 struct hfi1_pportdata *ppd;
796
797 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
798 ppd = dd->pport + pidx;
799 if (!ppd->hfi1_wq) {
800 ppd->hfi1_wq =
801 alloc_workqueue(
802 "hfi%d_%d",
David Brazdil0f672f62019-12-10 10:32:29 +0000803 WQ_SYSFS | WQ_HIGHPRI | WQ_CPU_INTENSIVE |
804 WQ_MEM_RECLAIM,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000805 HFI1_MAX_ACTIVE_WORKQUEUE_ENTRIES,
806 dd->unit, pidx);
807 if (!ppd->hfi1_wq)
808 goto wq_error;
809 }
810 if (!ppd->link_wq) {
811 /*
812 * Make the link workqueue single-threaded to enforce
813 * serialization.
814 */
815 ppd->link_wq =
816 alloc_workqueue(
817 "hfi_link_%d_%d",
818 WQ_SYSFS | WQ_MEM_RECLAIM | WQ_UNBOUND,
819 1, /* max_active */
820 dd->unit, pidx);
821 if (!ppd->link_wq)
822 goto wq_error;
823 }
824 }
825 return 0;
826wq_error:
827 pr_err("alloc_workqueue failed for port %d\n", pidx + 1);
828 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
829 ppd = dd->pport + pidx;
830 if (ppd->hfi1_wq) {
831 destroy_workqueue(ppd->hfi1_wq);
832 ppd->hfi1_wq = NULL;
833 }
834 if (ppd->link_wq) {
835 destroy_workqueue(ppd->link_wq);
836 ppd->link_wq = NULL;
837 }
838 }
839 return -ENOMEM;
840}
841
842/**
Olivier Deprez0e641232021-09-23 10:07:05 +0200843 * destroy_workqueues - destroy per port workqueues
844 * @dd: the hfi1_ib device
845 */
846static void destroy_workqueues(struct hfi1_devdata *dd)
847{
848 int pidx;
849 struct hfi1_pportdata *ppd;
850
851 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
852 ppd = dd->pport + pidx;
853
854 if (ppd->hfi1_wq) {
855 destroy_workqueue(ppd->hfi1_wq);
856 ppd->hfi1_wq = NULL;
857 }
858 if (ppd->link_wq) {
859 destroy_workqueue(ppd->link_wq);
860 ppd->link_wq = NULL;
861 }
862 }
863}
864
865/**
David Brazdil0f672f62019-12-10 10:32:29 +0000866 * enable_general_intr() - Enable the IRQs that will be handled by the
867 * general interrupt handler.
868 * @dd: valid devdata
869 *
870 */
871static void enable_general_intr(struct hfi1_devdata *dd)
872{
873 set_intr_bits(dd, CCE_ERR_INT, MISC_ERR_INT, true);
874 set_intr_bits(dd, PIO_ERR_INT, TXE_ERR_INT, true);
875 set_intr_bits(dd, IS_SENDCTXT_ERR_START, IS_SENDCTXT_ERR_END, true);
876 set_intr_bits(dd, PBC_INT, GPIO_ASSERT_INT, true);
877 set_intr_bits(dd, TCRIT_INT, TCRIT_INT, true);
878 set_intr_bits(dd, IS_DC_START, IS_DC_END, true);
879 set_intr_bits(dd, IS_SENDCREDIT_START, IS_SENDCREDIT_END, true);
880}
881
882/**
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000883 * hfi1_init - do the actual initialization sequence on the chip
884 * @dd: the hfi1_ib device
885 * @reinit: re-initializing, so don't allocate new memory
886 *
887 * Do the actual initialization sequence on the chip. This is done
888 * both from the init routine called from the PCI infrastructure, and
889 * when we reset the chip, or detect that it was reset internally,
890 * or it's administratively re-enabled.
891 *
892 * Memory allocation here and in called routines is only done in
893 * the first case (reinit == 0). We have to be careful, because even
894 * without memory allocation, we need to re-write all the chip registers
895 * TIDs, etc. after the reset or enable has completed.
896 */
897int hfi1_init(struct hfi1_devdata *dd, int reinit)
898{
899 int ret = 0, pidx, lastfail = 0;
900 unsigned long len;
901 u16 i;
902 struct hfi1_ctxtdata *rcd;
903 struct hfi1_pportdata *ppd;
904
905 /* Set up send low level handlers */
906 dd->process_pio_send = hfi1_verbs_send_pio;
907 dd->process_dma_send = hfi1_verbs_send_dma;
908 dd->pio_inline_send = pio_copy;
909 dd->process_vnic_dma_send = hfi1_vnic_send_dma;
910
911 if (is_ax(dd)) {
912 atomic_set(&dd->drop_packet, DROP_PACKET_ON);
913 dd->do_drop = 1;
914 } else {
915 atomic_set(&dd->drop_packet, DROP_PACKET_OFF);
916 dd->do_drop = 0;
917 }
918
919 /* make sure the link is not "up" */
920 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
921 ppd = dd->pport + pidx;
922 ppd->linkup = 0;
923 }
924
925 if (reinit)
926 ret = init_after_reset(dd);
927 else
928 ret = loadtime_init(dd);
929 if (ret)
930 goto done;
931
932 /* allocate dummy tail memory for all receive contexts */
David Brazdil0f672f62019-12-10 10:32:29 +0000933 dd->rcvhdrtail_dummy_kvaddr = dma_alloc_coherent(&dd->pcidev->dev,
934 sizeof(u64),
935 &dd->rcvhdrtail_dummy_dma,
936 GFP_KERNEL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000937
938 if (!dd->rcvhdrtail_dummy_kvaddr) {
939 dd_dev_err(dd, "cannot allocate dummy tail memory\n");
940 ret = -ENOMEM;
941 goto done;
942 }
943
944 /* dd->rcd can be NULL if early initialization failed */
945 for (i = 0; dd->rcd && i < dd->first_dyn_alloc_ctxt; ++i) {
946 /*
947 * Set up the (kernel) rcvhdr queue and egr TIDs. If doing
948 * re-init, the simplest way to handle this is to free
949 * existing, and re-allocate.
950 * Need to re-create rest of ctxt 0 ctxtdata as well.
951 */
952 rcd = hfi1_rcd_get_by_index(dd, i);
953 if (!rcd)
954 continue;
955
956 rcd->do_interrupt = &handle_receive_interrupt;
957
958 lastfail = hfi1_create_rcvhdrq(dd, rcd);
959 if (!lastfail)
960 lastfail = hfi1_setup_eagerbufs(rcd);
David Brazdil0f672f62019-12-10 10:32:29 +0000961 if (!lastfail)
962 lastfail = hfi1_kern_exp_rcv_init(rcd, reinit);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000963 if (lastfail) {
964 dd_dev_err(dd,
965 "failed to allocate kernel ctxt's rcvhdrq and/or egr bufs\n");
966 ret = lastfail;
967 }
David Brazdil0f672f62019-12-10 10:32:29 +0000968 /* enable IRQ */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000969 hfi1_rcd_put(rcd);
970 }
971
972 /* Allocate enough memory for user event notification. */
973 len = PAGE_ALIGN(chip_rcv_contexts(dd) * HFI1_MAX_SHARED_CTXTS *
974 sizeof(*dd->events));
975 dd->events = vmalloc_user(len);
976 if (!dd->events)
977 dd_dev_err(dd, "Failed to allocate user events page\n");
978 /*
979 * Allocate a page for device and port status.
980 * Page will be shared amongst all user processes.
981 */
982 dd->status = vmalloc_user(PAGE_SIZE);
983 if (!dd->status)
984 dd_dev_err(dd, "Failed to allocate dev status page\n");
985 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
986 ppd = dd->pport + pidx;
987 if (dd->status)
988 /* Currently, we only have one port */
989 ppd->statusp = &dd->status->port;
990
991 set_mtu(ppd);
992 }
993
994 /* enable chip even if we have an error, so we can debug cause */
995 enable_chip(dd);
996
997done:
998 /*
999 * Set status even if port serdes is not initialized
1000 * so that diags will work.
1001 */
1002 if (dd->status)
1003 dd->status->dev |= HFI1_STATUS_CHIP_PRESENT |
1004 HFI1_STATUS_INITTED;
1005 if (!ret) {
1006 /* enable all interrupts from the chip */
David Brazdil0f672f62019-12-10 10:32:29 +00001007 enable_general_intr(dd);
1008 init_qsfp_int(dd);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001009
1010 /* chip is OK for user apps; mark it as initialized */
1011 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
1012 ppd = dd->pport + pidx;
1013
1014 /*
1015 * start the serdes - must be after interrupts are
1016 * enabled so we are notified when the link goes up
1017 */
1018 lastfail = bringup_serdes(ppd);
1019 if (lastfail)
1020 dd_dev_info(dd,
1021 "Failed to bring up port %u\n",
1022 ppd->port);
1023
1024 /*
1025 * Set status even if port serdes is not initialized
1026 * so that diags will work.
1027 */
1028 if (ppd->statusp)
1029 *ppd->statusp |= HFI1_STATUS_CHIP_PRESENT |
1030 HFI1_STATUS_INITTED;
1031 if (!ppd->link_speed_enabled)
1032 continue;
1033 }
1034 }
1035
1036 /* if ret is non-zero, we probably should do some cleanup here... */
1037 return ret;
1038}
1039
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001040struct hfi1_devdata *hfi1_lookup(int unit)
1041{
David Brazdil0f672f62019-12-10 10:32:29 +00001042 return xa_load(&hfi1_dev_table, unit);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001043}
1044
1045/*
1046 * Stop the timers during unit shutdown, or after an error late
1047 * in initialization.
1048 */
1049static void stop_timers(struct hfi1_devdata *dd)
1050{
1051 struct hfi1_pportdata *ppd;
1052 int pidx;
1053
1054 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
1055 ppd = dd->pport + pidx;
1056 if (ppd->led_override_timer.function) {
1057 del_timer_sync(&ppd->led_override_timer);
1058 atomic_set(&ppd->led_override_timer_active, 0);
1059 }
1060 }
1061}
1062
1063/**
1064 * shutdown_device - shut down a device
1065 * @dd: the hfi1_ib device
1066 *
1067 * This is called to make the device quiet when we are about to
1068 * unload the driver, and also when the device is administratively
1069 * disabled. It does not free any data structures.
1070 * Everything it does has to be setup again by hfi1_init(dd, 1)
1071 */
1072static void shutdown_device(struct hfi1_devdata *dd)
1073{
1074 struct hfi1_pportdata *ppd;
1075 struct hfi1_ctxtdata *rcd;
1076 unsigned pidx;
1077 int i;
1078
1079 if (dd->flags & HFI1_SHUTDOWN)
1080 return;
1081 dd->flags |= HFI1_SHUTDOWN;
1082
1083 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
1084 ppd = dd->pport + pidx;
1085
1086 ppd->linkup = 0;
1087 if (ppd->statusp)
1088 *ppd->statusp &= ~(HFI1_STATUS_IB_CONF |
1089 HFI1_STATUS_IB_READY);
1090 }
1091 dd->flags &= ~HFI1_INITTED;
1092
David Brazdil0f672f62019-12-10 10:32:29 +00001093 /* mask and clean up interrupts */
1094 set_intr_bits(dd, IS_FIRST_SOURCE, IS_LAST_SOURCE, false);
1095 msix_clean_up_interrupts(dd);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001096
1097 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
1098 ppd = dd->pport + pidx;
1099 for (i = 0; i < dd->num_rcv_contexts; i++) {
1100 rcd = hfi1_rcd_get_by_index(dd, i);
1101 hfi1_rcvctrl(dd, HFI1_RCVCTRL_TAILUPD_DIS |
1102 HFI1_RCVCTRL_CTXT_DIS |
1103 HFI1_RCVCTRL_INTRAVAIL_DIS |
1104 HFI1_RCVCTRL_PKEY_DIS |
1105 HFI1_RCVCTRL_ONE_PKT_EGR_DIS, rcd);
1106 hfi1_rcd_put(rcd);
1107 }
1108 /*
1109 * Gracefully stop all sends allowing any in progress to
1110 * trickle out first.
1111 */
1112 for (i = 0; i < dd->num_send_contexts; i++)
1113 sc_flush(dd->send_contexts[i].sc);
1114 }
1115
1116 /*
1117 * Enough for anything that's going to trickle out to have actually
1118 * done so.
1119 */
1120 udelay(20);
1121
1122 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
1123 ppd = dd->pport + pidx;
1124
1125 /* disable all contexts */
1126 for (i = 0; i < dd->num_send_contexts; i++)
1127 sc_disable(dd->send_contexts[i].sc);
1128 /* disable the send device */
1129 pio_send_control(dd, PSC_GLOBAL_DISABLE);
1130
1131 shutdown_led_override(ppd);
1132
1133 /*
1134 * Clear SerdesEnable.
1135 * We can't count on interrupts since we are stopping.
1136 */
1137 hfi1_quiet_serdes(ppd);
Olivier Deprez0e641232021-09-23 10:07:05 +02001138 if (ppd->hfi1_wq)
1139 flush_workqueue(ppd->hfi1_wq);
1140 if (ppd->link_wq)
1141 flush_workqueue(ppd->link_wq);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001142 }
1143 sdma_exit(dd);
1144}
1145
1146/**
1147 * hfi1_free_ctxtdata - free a context's allocated data
1148 * @dd: the hfi1_ib device
1149 * @rcd: the ctxtdata structure
1150 *
1151 * free up any allocated data for a context
1152 * It should never change any chip state, or global driver state.
1153 */
1154void hfi1_free_ctxtdata(struct hfi1_devdata *dd, struct hfi1_ctxtdata *rcd)
1155{
1156 u32 e;
1157
1158 if (!rcd)
1159 return;
1160
1161 if (rcd->rcvhdrq) {
1162 dma_free_coherent(&dd->pcidev->dev, rcvhdrq_size(rcd),
1163 rcd->rcvhdrq, rcd->rcvhdrq_dma);
1164 rcd->rcvhdrq = NULL;
1165 if (rcd->rcvhdrtail_kvaddr) {
1166 dma_free_coherent(&dd->pcidev->dev, PAGE_SIZE,
1167 (void *)rcd->rcvhdrtail_kvaddr,
1168 rcd->rcvhdrqtailaddr_dma);
1169 rcd->rcvhdrtail_kvaddr = NULL;
1170 }
1171 }
1172
1173 /* all the RcvArray entries should have been cleared by now */
1174 kfree(rcd->egrbufs.rcvtids);
1175 rcd->egrbufs.rcvtids = NULL;
1176
1177 for (e = 0; e < rcd->egrbufs.alloced; e++) {
1178 if (rcd->egrbufs.buffers[e].dma)
1179 dma_free_coherent(&dd->pcidev->dev,
1180 rcd->egrbufs.buffers[e].len,
1181 rcd->egrbufs.buffers[e].addr,
1182 rcd->egrbufs.buffers[e].dma);
1183 }
1184 kfree(rcd->egrbufs.buffers);
1185 rcd->egrbufs.alloced = 0;
1186 rcd->egrbufs.buffers = NULL;
1187
1188 sc_free(rcd->sc);
1189 rcd->sc = NULL;
1190
1191 vfree(rcd->subctxt_uregbase);
1192 vfree(rcd->subctxt_rcvegrbuf);
1193 vfree(rcd->subctxt_rcvhdr_base);
1194 kfree(rcd->opstats);
1195
1196 rcd->subctxt_uregbase = NULL;
1197 rcd->subctxt_rcvegrbuf = NULL;
1198 rcd->subctxt_rcvhdr_base = NULL;
1199 rcd->opstats = NULL;
1200}
1201
1202/*
1203 * Release our hold on the shared asic data. If we are the last one,
1204 * return the structure to be finalized outside the lock. Must be
David Brazdil0f672f62019-12-10 10:32:29 +00001205 * holding hfi1_dev_table lock.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001206 */
1207static struct hfi1_asic_data *release_asic_data(struct hfi1_devdata *dd)
1208{
1209 struct hfi1_asic_data *ad;
1210 int other;
1211
1212 if (!dd->asic_data)
1213 return NULL;
1214 dd->asic_data->dds[dd->hfi1_id] = NULL;
1215 other = dd->hfi1_id ? 0 : 1;
1216 ad = dd->asic_data;
1217 dd->asic_data = NULL;
1218 /* return NULL if the other dd still has a link */
1219 return ad->dds[other] ? NULL : ad;
1220}
1221
1222static void finalize_asic_data(struct hfi1_devdata *dd,
1223 struct hfi1_asic_data *ad)
1224{
1225 clean_up_i2c(dd, ad);
1226 kfree(ad);
1227}
1228
1229/**
1230 * hfi1_clean_devdata - cleans up per-unit data structure
1231 * @dd: pointer to a valid devdata structure
1232 *
1233 * It cleans up all data structures set up by
1234 * by hfi1_alloc_devdata().
1235 */
1236static void hfi1_clean_devdata(struct hfi1_devdata *dd)
1237{
1238 struct hfi1_asic_data *ad;
1239 unsigned long flags;
1240
David Brazdil0f672f62019-12-10 10:32:29 +00001241 xa_lock_irqsave(&hfi1_dev_table, flags);
1242 __xa_erase(&hfi1_dev_table, dd->unit);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001243 ad = release_asic_data(dd);
David Brazdil0f672f62019-12-10 10:32:29 +00001244 xa_unlock_irqrestore(&hfi1_dev_table, flags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001245
1246 finalize_asic_data(dd, ad);
1247 free_platform_config(dd);
1248 rcu_barrier(); /* wait for rcu callbacks to complete */
1249 free_percpu(dd->int_counter);
1250 free_percpu(dd->rcv_limit);
1251 free_percpu(dd->send_schedule);
1252 free_percpu(dd->tx_opstats);
1253 dd->int_counter = NULL;
1254 dd->rcv_limit = NULL;
1255 dd->send_schedule = NULL;
1256 dd->tx_opstats = NULL;
1257 kfree(dd->comp_vect);
1258 dd->comp_vect = NULL;
1259 sdma_clean(dd, dd->num_sdma);
1260 rvt_dealloc_device(&dd->verbs_dev.rdi);
1261}
1262
1263static void __hfi1_free_devdata(struct kobject *kobj)
1264{
1265 struct hfi1_devdata *dd =
1266 container_of(kobj, struct hfi1_devdata, kobj);
1267
1268 hfi1_clean_devdata(dd);
1269}
1270
1271static struct kobj_type hfi1_devdata_type = {
1272 .release = __hfi1_free_devdata,
1273};
1274
1275void hfi1_free_devdata(struct hfi1_devdata *dd)
1276{
1277 kobject_put(&dd->kobj);
1278}
1279
David Brazdil0f672f62019-12-10 10:32:29 +00001280/**
1281 * hfi1_alloc_devdata - Allocate our primary per-unit data structure.
1282 * @pdev: Valid PCI device
1283 * @extra: How many bytes to alloc past the default
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001284 *
David Brazdil0f672f62019-12-10 10:32:29 +00001285 * Must be done via verbs allocator, because the verbs cleanup process
1286 * both does cleanup and free of the data structure.
1287 * "extra" is for chip-specific data.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001288 */
David Brazdil0f672f62019-12-10 10:32:29 +00001289static struct hfi1_devdata *hfi1_alloc_devdata(struct pci_dev *pdev,
1290 size_t extra)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001291{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001292 struct hfi1_devdata *dd;
1293 int ret, nports;
1294
1295 /* extra is * number of ports */
1296 nports = extra / sizeof(struct hfi1_pportdata);
1297
1298 dd = (struct hfi1_devdata *)rvt_alloc_device(sizeof(*dd) + extra,
1299 nports);
1300 if (!dd)
1301 return ERR_PTR(-ENOMEM);
1302 dd->num_pports = nports;
1303 dd->pport = (struct hfi1_pportdata *)(dd + 1);
1304 dd->pcidev = pdev;
1305 pci_set_drvdata(pdev, dd);
David Brazdil0f672f62019-12-10 10:32:29 +00001306 dd->node = NUMA_NO_NODE;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001307
David Brazdil0f672f62019-12-10 10:32:29 +00001308 ret = xa_alloc_irq(&hfi1_dev_table, &dd->unit, dd, xa_limit_32b,
1309 GFP_KERNEL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001310 if (ret < 0) {
David Brazdil0f672f62019-12-10 10:32:29 +00001311 dev_err(&pdev->dev,
1312 "Could not allocate unit ID: error %d\n", -ret);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001313 goto bail;
1314 }
1315 rvt_set_ibdev_name(&dd->verbs_dev.rdi, "%s_%d", class_name(), dd->unit);
1316
1317 /*
1318 * Initialize all locks for the device. This needs to be as early as
1319 * possible so locks are usable.
1320 */
1321 spin_lock_init(&dd->sc_lock);
1322 spin_lock_init(&dd->sendctrl_lock);
1323 spin_lock_init(&dd->rcvctrl_lock);
1324 spin_lock_init(&dd->uctxt_lock);
1325 spin_lock_init(&dd->hfi1_diag_trans_lock);
1326 spin_lock_init(&dd->sc_init_lock);
1327 spin_lock_init(&dd->dc8051_memlock);
1328 seqlock_init(&dd->sc2vl_lock);
1329 spin_lock_init(&dd->sde_map_lock);
1330 spin_lock_init(&dd->pio_map_lock);
1331 mutex_init(&dd->dc8051_lock);
1332 init_waitqueue_head(&dd->event_queue);
David Brazdil0f672f62019-12-10 10:32:29 +00001333 spin_lock_init(&dd->irq_src_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001334
1335 dd->int_counter = alloc_percpu(u64);
1336 if (!dd->int_counter) {
1337 ret = -ENOMEM;
1338 goto bail;
1339 }
1340
1341 dd->rcv_limit = alloc_percpu(u64);
1342 if (!dd->rcv_limit) {
1343 ret = -ENOMEM;
1344 goto bail;
1345 }
1346
1347 dd->send_schedule = alloc_percpu(u64);
1348 if (!dd->send_schedule) {
1349 ret = -ENOMEM;
1350 goto bail;
1351 }
1352
1353 dd->tx_opstats = alloc_percpu(struct hfi1_opcode_stats_perctx);
1354 if (!dd->tx_opstats) {
1355 ret = -ENOMEM;
1356 goto bail;
1357 }
1358
1359 dd->comp_vect = kzalloc(sizeof(*dd->comp_vect), GFP_KERNEL);
1360 if (!dd->comp_vect) {
1361 ret = -ENOMEM;
1362 goto bail;
1363 }
1364
1365 kobject_init(&dd->kobj, &hfi1_devdata_type);
1366 return dd;
1367
1368bail:
1369 hfi1_clean_devdata(dd);
1370 return ERR_PTR(ret);
1371}
1372
1373/*
1374 * Called from freeze mode handlers, and from PCI error
1375 * reporting code. Should be paranoid about state of
1376 * system and data structures.
1377 */
1378void hfi1_disable_after_error(struct hfi1_devdata *dd)
1379{
1380 if (dd->flags & HFI1_INITTED) {
1381 u32 pidx;
1382
1383 dd->flags &= ~HFI1_INITTED;
1384 if (dd->pport)
1385 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
1386 struct hfi1_pportdata *ppd;
1387
1388 ppd = dd->pport + pidx;
1389 if (dd->flags & HFI1_PRESENT)
1390 set_link_state(ppd, HLS_DN_DISABLE);
1391
1392 if (ppd->statusp)
1393 *ppd->statusp &= ~HFI1_STATUS_IB_READY;
1394 }
1395 }
1396
1397 /*
1398 * Mark as having had an error for driver, and also
1399 * for /sys and status word mapped to user programs.
1400 * This marks unit as not usable, until reset.
1401 */
1402 if (dd->status)
1403 dd->status->dev |= HFI1_STATUS_HWERROR;
1404}
1405
1406static void remove_one(struct pci_dev *);
1407static int init_one(struct pci_dev *, const struct pci_device_id *);
1408static void shutdown_one(struct pci_dev *);
1409
1410#define DRIVER_LOAD_MSG "Intel " DRIVER_NAME " loaded: "
1411#define PFX DRIVER_NAME ": "
1412
1413const struct pci_device_id hfi1_pci_tbl[] = {
1414 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL0) },
1415 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL1) },
1416 { 0, }
1417};
1418
1419MODULE_DEVICE_TABLE(pci, hfi1_pci_tbl);
1420
1421static struct pci_driver hfi1_pci_driver = {
1422 .name = DRIVER_NAME,
1423 .probe = init_one,
1424 .remove = remove_one,
1425 .shutdown = shutdown_one,
1426 .id_table = hfi1_pci_tbl,
1427 .err_handler = &hfi1_pci_err_handler,
1428};
1429
1430static void __init compute_krcvqs(void)
1431{
1432 int i;
1433
1434 for (i = 0; i < krcvqsset; i++)
1435 n_krcvqs += krcvqs[i];
1436}
1437
1438/*
1439 * Do all the generic driver unit- and chip-independent memory
1440 * allocation and initialization.
1441 */
1442static int __init hfi1_mod_init(void)
1443{
1444 int ret;
1445
1446 ret = dev_init();
1447 if (ret)
1448 goto bail;
1449
1450 ret = node_affinity_init();
1451 if (ret)
1452 goto bail;
1453
1454 /* validate max MTU before any devices start */
1455 if (!valid_opa_max_mtu(hfi1_max_mtu)) {
1456 pr_err("Invalid max_mtu 0x%x, using 0x%x instead\n",
1457 hfi1_max_mtu, HFI1_DEFAULT_MAX_MTU);
1458 hfi1_max_mtu = HFI1_DEFAULT_MAX_MTU;
1459 }
1460 /* valid CUs run from 1-128 in powers of 2 */
1461 if (hfi1_cu > 128 || !is_power_of_2(hfi1_cu))
1462 hfi1_cu = 1;
1463 /* valid credit return threshold is 0-100, variable is unsigned */
1464 if (user_credit_return_threshold > 100)
1465 user_credit_return_threshold = 100;
1466
1467 compute_krcvqs();
1468 /*
1469 * sanitize receive interrupt count, time must wait until after
1470 * the hardware type is known
1471 */
1472 if (rcv_intr_count > RCV_HDR_HEAD_COUNTER_MASK)
1473 rcv_intr_count = RCV_HDR_HEAD_COUNTER_MASK;
1474 /* reject invalid combinations */
1475 if (rcv_intr_count == 0 && rcv_intr_timeout == 0) {
1476 pr_err("Invalid mode: both receive interrupt count and available timeout are zero - setting interrupt count to 1\n");
1477 rcv_intr_count = 1;
1478 }
1479 if (rcv_intr_count > 1 && rcv_intr_timeout == 0) {
1480 /*
1481 * Avoid indefinite packet delivery by requiring a timeout
1482 * if count is > 1.
1483 */
1484 pr_err("Invalid mode: receive interrupt count greater than 1 and available timeout is zero - setting available timeout to 1\n");
1485 rcv_intr_timeout = 1;
1486 }
1487 if (rcv_intr_dynamic && !(rcv_intr_count > 1 && rcv_intr_timeout > 0)) {
1488 /*
1489 * The dynamic algorithm expects a non-zero timeout
1490 * and a count > 1.
1491 */
1492 pr_err("Invalid mode: dynamic receive interrupt mitigation with invalid count and timeout - turning dynamic off\n");
1493 rcv_intr_dynamic = 0;
1494 }
1495
1496 /* sanitize link CRC options */
1497 link_crc_mask &= SUPPORTED_CRCS;
1498
David Brazdil0f672f62019-12-10 10:32:29 +00001499 ret = opfn_init();
1500 if (ret < 0) {
1501 pr_err("Failed to allocate opfn_wq");
1502 goto bail_dev;
1503 }
1504
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001505 /*
1506 * These must be called before the driver is registered with
1507 * the PCI subsystem.
1508 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001509 hfi1_dbg_init();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001510 ret = pci_register_driver(&hfi1_pci_driver);
1511 if (ret < 0) {
1512 pr_err("Unable to register driver: error %d\n", -ret);
1513 goto bail_dev;
1514 }
1515 goto bail; /* all OK */
1516
1517bail_dev:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001518 hfi1_dbg_exit();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001519 dev_cleanup();
1520bail:
1521 return ret;
1522}
1523
1524module_init(hfi1_mod_init);
1525
1526/*
1527 * Do the non-unit driver cleanup, memory free, etc. at unload.
1528 */
1529static void __exit hfi1_mod_cleanup(void)
1530{
1531 pci_unregister_driver(&hfi1_pci_driver);
David Brazdil0f672f62019-12-10 10:32:29 +00001532 opfn_exit();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001533 node_affinity_destroy_all();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001534 hfi1_dbg_exit();
1535
David Brazdil0f672f62019-12-10 10:32:29 +00001536 WARN_ON(!xa_empty(&hfi1_dev_table));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001537 dispose_firmware(); /* asymmetric with obtain_firmware() */
1538 dev_cleanup();
1539}
1540
1541module_exit(hfi1_mod_cleanup);
1542
1543/* this can only be called after a successful initialization */
1544static void cleanup_device_data(struct hfi1_devdata *dd)
1545{
1546 int ctxt;
1547 int pidx;
1548
1549 /* users can't do anything more with chip */
1550 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
1551 struct hfi1_pportdata *ppd = &dd->pport[pidx];
1552 struct cc_state *cc_state;
1553 int i;
1554
1555 if (ppd->statusp)
1556 *ppd->statusp &= ~HFI1_STATUS_CHIP_PRESENT;
1557
1558 for (i = 0; i < OPA_MAX_SLS; i++)
1559 hrtimer_cancel(&ppd->cca_timer[i].hrtimer);
1560
1561 spin_lock(&ppd->cc_state_lock);
1562 cc_state = get_cc_state_protected(ppd);
1563 RCU_INIT_POINTER(ppd->cc_state, NULL);
1564 spin_unlock(&ppd->cc_state_lock);
1565
1566 if (cc_state)
1567 kfree_rcu(cc_state, rcu);
1568 }
1569
1570 free_credit_return(dd);
1571
1572 if (dd->rcvhdrtail_dummy_kvaddr) {
1573 dma_free_coherent(&dd->pcidev->dev, sizeof(u64),
1574 (void *)dd->rcvhdrtail_dummy_kvaddr,
1575 dd->rcvhdrtail_dummy_dma);
1576 dd->rcvhdrtail_dummy_kvaddr = NULL;
1577 }
1578
1579 /*
1580 * Free any resources still in use (usually just kernel contexts)
1581 * at unload; we do for ctxtcnt, because that's what we allocate.
1582 */
1583 for (ctxt = 0; dd->rcd && ctxt < dd->num_rcv_contexts; ctxt++) {
1584 struct hfi1_ctxtdata *rcd = dd->rcd[ctxt];
1585
1586 if (rcd) {
David Brazdil0f672f62019-12-10 10:32:29 +00001587 hfi1_free_ctxt_rcv_groups(rcd);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001588 hfi1_free_ctxt(rcd);
1589 }
1590 }
1591
1592 kfree(dd->rcd);
1593 dd->rcd = NULL;
1594
1595 free_pio_map(dd);
1596 /* must follow rcv context free - need to remove rcv's hooks */
1597 for (ctxt = 0; ctxt < dd->num_send_contexts; ctxt++)
1598 sc_free(dd->send_contexts[ctxt].sc);
1599 dd->num_send_contexts = 0;
1600 kfree(dd->send_contexts);
1601 dd->send_contexts = NULL;
1602 kfree(dd->hw_to_sw);
1603 dd->hw_to_sw = NULL;
1604 kfree(dd->boardname);
1605 vfree(dd->events);
1606 vfree(dd->status);
1607}
1608
1609/*
1610 * Clean up on unit shutdown, or error during unit load after
1611 * successful initialization.
1612 */
1613static void postinit_cleanup(struct hfi1_devdata *dd)
1614{
1615 hfi1_start_cleanup(dd);
1616 hfi1_comp_vectors_clean_up(dd);
1617 hfi1_dev_affinity_clean_up(dd);
1618
1619 hfi1_pcie_ddcleanup(dd);
1620 hfi1_pcie_cleanup(dd->pcidev);
1621
1622 cleanup_device_data(dd);
1623
1624 hfi1_free_devdata(dd);
1625}
1626
David Brazdil0f672f62019-12-10 10:32:29 +00001627static int init_validate_rcvhdrcnt(struct hfi1_devdata *dd, uint thecnt)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001628{
1629 if (thecnt <= HFI1_MIN_HDRQ_EGRBUF_CNT) {
David Brazdil0f672f62019-12-10 10:32:29 +00001630 dd_dev_err(dd, "Receive header queue count too small\n");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001631 return -EINVAL;
1632 }
1633
1634 if (thecnt > HFI1_MAX_HDRQ_EGRBUF_CNT) {
David Brazdil0f672f62019-12-10 10:32:29 +00001635 dd_dev_err(dd,
1636 "Receive header queue count cannot be greater than %u\n",
1637 HFI1_MAX_HDRQ_EGRBUF_CNT);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001638 return -EINVAL;
1639 }
1640
1641 if (thecnt % HDRQ_INCREMENT) {
David Brazdil0f672f62019-12-10 10:32:29 +00001642 dd_dev_err(dd, "Receive header queue count %d must be divisible by %lu\n",
1643 thecnt, HDRQ_INCREMENT);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001644 return -EINVAL;
1645 }
1646
1647 return 0;
1648}
1649
1650static int init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
1651{
1652 int ret = 0, j, pidx, initfail;
1653 struct hfi1_devdata *dd;
1654 struct hfi1_pportdata *ppd;
1655
1656 /* First, lock the non-writable module parameters */
1657 HFI1_CAP_LOCK();
1658
1659 /* Validate dev ids */
1660 if (!(ent->device == PCI_DEVICE_ID_INTEL0 ||
1661 ent->device == PCI_DEVICE_ID_INTEL1)) {
David Brazdil0f672f62019-12-10 10:32:29 +00001662 dev_err(&pdev->dev, "Failing on unknown Intel deviceid 0x%x\n",
1663 ent->device);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001664 ret = -ENODEV;
1665 goto bail;
1666 }
1667
David Brazdil0f672f62019-12-10 10:32:29 +00001668 /* Allocate the dd so we can get to work */
1669 dd = hfi1_alloc_devdata(pdev, NUM_IB_PORTS *
1670 sizeof(struct hfi1_pportdata));
1671 if (IS_ERR(dd)) {
1672 ret = PTR_ERR(dd);
1673 goto bail;
1674 }
1675
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001676 /* Validate some global module parameters */
David Brazdil0f672f62019-12-10 10:32:29 +00001677 ret = init_validate_rcvhdrcnt(dd, rcvhdrcnt);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001678 if (ret)
1679 goto bail;
1680
1681 /* use the encoding function as a sanitization check */
1682 if (!encode_rcv_header_entry_size(hfi1_hdrq_entsize)) {
David Brazdil0f672f62019-12-10 10:32:29 +00001683 dd_dev_err(dd, "Invalid HdrQ Entry size %u\n",
1684 hfi1_hdrq_entsize);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001685 ret = -EINVAL;
1686 goto bail;
1687 }
1688
1689 /* The receive eager buffer size must be set before the receive
1690 * contexts are created.
1691 *
1692 * Set the eager buffer size. Validate that it falls in a range
1693 * allowed by the hardware - all powers of 2 between the min and
1694 * max. The maximum valid MTU is within the eager buffer range
1695 * so we do not need to cap the max_mtu by an eager buffer size
1696 * setting.
1697 */
1698 if (eager_buffer_size) {
1699 if (!is_power_of_2(eager_buffer_size))
1700 eager_buffer_size =
1701 roundup_pow_of_two(eager_buffer_size);
1702 eager_buffer_size =
1703 clamp_val(eager_buffer_size,
1704 MIN_EAGER_BUFFER * 8,
1705 MAX_EAGER_BUFFER_TOTAL);
David Brazdil0f672f62019-12-10 10:32:29 +00001706 dd_dev_info(dd, "Eager buffer size %u\n",
1707 eager_buffer_size);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001708 } else {
David Brazdil0f672f62019-12-10 10:32:29 +00001709 dd_dev_err(dd, "Invalid Eager buffer size of 0\n");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001710 ret = -EINVAL;
1711 goto bail;
1712 }
1713
1714 /* restrict value of hfi1_rcvarr_split */
1715 hfi1_rcvarr_split = clamp_val(hfi1_rcvarr_split, 0, 100);
1716
David Brazdil0f672f62019-12-10 10:32:29 +00001717 ret = hfi1_pcie_init(dd);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001718 if (ret)
1719 goto bail;
1720
1721 /*
1722 * Do device-specific initialization, function table setup, dd
1723 * allocation, etc.
1724 */
David Brazdil0f672f62019-12-10 10:32:29 +00001725 ret = hfi1_init_dd(dd);
1726 if (ret)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001727 goto clean_bail; /* error already printed */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001728
1729 ret = create_workqueues(dd);
1730 if (ret)
1731 goto clean_bail;
1732
1733 /* do the generic initialization */
1734 initfail = hfi1_init(dd, 0);
1735
1736 /* setup vnic */
1737 hfi1_vnic_setup(dd);
1738
1739 ret = hfi1_register_ib_device(dd);
1740
1741 /*
1742 * Now ready for use. this should be cleared whenever we
1743 * detect a reset, or initiate one. If earlier failure,
1744 * we still create devices, so diags, etc. can be used
1745 * to determine cause of problem.
1746 */
1747 if (!initfail && !ret) {
1748 dd->flags |= HFI1_INITTED;
1749 /* create debufs files after init and ib register */
1750 hfi1_dbg_ibdev_init(&dd->verbs_dev);
1751 }
1752
1753 j = hfi1_device_create(dd);
1754 if (j)
1755 dd_dev_err(dd, "Failed to create /dev devices: %d\n", -j);
1756
1757 if (initfail || ret) {
David Brazdil0f672f62019-12-10 10:32:29 +00001758 msix_clean_up_interrupts(dd);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001759 stop_timers(dd);
1760 flush_workqueue(ib_wq);
1761 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
1762 hfi1_quiet_serdes(dd->pport + pidx);
1763 ppd = dd->pport + pidx;
1764 if (ppd->hfi1_wq) {
1765 destroy_workqueue(ppd->hfi1_wq);
1766 ppd->hfi1_wq = NULL;
1767 }
1768 if (ppd->link_wq) {
1769 destroy_workqueue(ppd->link_wq);
1770 ppd->link_wq = NULL;
1771 }
1772 }
1773 if (!j)
1774 hfi1_device_remove(dd);
1775 if (!ret)
1776 hfi1_unregister_ib_device(dd);
1777 hfi1_vnic_cleanup(dd);
1778 postinit_cleanup(dd);
1779 if (initfail)
1780 ret = initfail;
1781 goto bail; /* everything already cleaned */
1782 }
1783
1784 sdma_start(dd);
1785
1786 return 0;
1787
1788clean_bail:
1789 hfi1_pcie_cleanup(pdev);
1790bail:
1791 return ret;
1792}
1793
1794static void wait_for_clients(struct hfi1_devdata *dd)
1795{
1796 /*
1797 * Remove the device init value and complete the device if there is
1798 * no clients or wait for active clients to finish.
1799 */
1800 if (atomic_dec_and_test(&dd->user_refcount))
1801 complete(&dd->user_comp);
1802
1803 wait_for_completion(&dd->user_comp);
1804}
1805
1806static void remove_one(struct pci_dev *pdev)
1807{
1808 struct hfi1_devdata *dd = pci_get_drvdata(pdev);
1809
1810 /* close debugfs files before ib unregister */
1811 hfi1_dbg_ibdev_exit(&dd->verbs_dev);
1812
1813 /* remove the /dev hfi1 interface */
1814 hfi1_device_remove(dd);
1815
1816 /* wait for existing user space clients to finish */
1817 wait_for_clients(dd);
1818
1819 /* unregister from IB core */
1820 hfi1_unregister_ib_device(dd);
1821
1822 /* cleanup vnic */
1823 hfi1_vnic_cleanup(dd);
1824
1825 /*
1826 * Disable the IB link, disable interrupts on the device,
1827 * clear dma engines, etc.
1828 */
1829 shutdown_device(dd);
Olivier Deprez0e641232021-09-23 10:07:05 +02001830 destroy_workqueues(dd);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001831
1832 stop_timers(dd);
1833
1834 /* wait until all of our (qsfp) queue_work() calls complete */
1835 flush_workqueue(ib_wq);
1836
1837 postinit_cleanup(dd);
1838}
1839
1840static void shutdown_one(struct pci_dev *pdev)
1841{
1842 struct hfi1_devdata *dd = pci_get_drvdata(pdev);
1843
1844 shutdown_device(dd);
1845}
1846
1847/**
1848 * hfi1_create_rcvhdrq - create a receive header queue
1849 * @dd: the hfi1_ib device
1850 * @rcd: the context data
1851 *
1852 * This must be contiguous memory (from an i/o perspective), and must be
1853 * DMA'able (which means for some systems, it will go through an IOMMU,
1854 * or be forced into a low address range).
1855 */
1856int hfi1_create_rcvhdrq(struct hfi1_devdata *dd, struct hfi1_ctxtdata *rcd)
1857{
1858 unsigned amt;
1859 u64 reg;
1860
1861 if (!rcd->rcvhdrq) {
1862 gfp_t gfp_flags;
1863
1864 amt = rcvhdrq_size(rcd);
1865
1866 if (rcd->ctxt < dd->first_dyn_alloc_ctxt || rcd->is_vnic)
1867 gfp_flags = GFP_KERNEL;
1868 else
1869 gfp_flags = GFP_USER;
David Brazdil0f672f62019-12-10 10:32:29 +00001870 rcd->rcvhdrq = dma_alloc_coherent(&dd->pcidev->dev, amt,
1871 &rcd->rcvhdrq_dma,
1872 gfp_flags | __GFP_COMP);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001873
1874 if (!rcd->rcvhdrq) {
1875 dd_dev_err(dd,
1876 "attempt to allocate %d bytes for ctxt %u rcvhdrq failed\n",
1877 amt, rcd->ctxt);
1878 goto bail;
1879 }
1880
1881 if (HFI1_CAP_KGET_MASK(rcd->flags, DMA_RTAIL) ||
1882 HFI1_CAP_UGET_MASK(rcd->flags, DMA_RTAIL)) {
David Brazdil0f672f62019-12-10 10:32:29 +00001883 rcd->rcvhdrtail_kvaddr = dma_alloc_coherent(&dd->pcidev->dev,
1884 PAGE_SIZE,
1885 &rcd->rcvhdrqtailaddr_dma,
1886 gfp_flags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001887 if (!rcd->rcvhdrtail_kvaddr)
1888 goto bail_free;
1889 }
1890 }
1891 /*
1892 * These values are per-context:
1893 * RcvHdrCnt
1894 * RcvHdrEntSize
1895 * RcvHdrSize
1896 */
1897 reg = ((u64)(rcd->rcvhdrq_cnt >> HDRQ_SIZE_SHIFT)
1898 & RCV_HDR_CNT_CNT_MASK)
1899 << RCV_HDR_CNT_CNT_SHIFT;
1900 write_kctxt_csr(dd, rcd->ctxt, RCV_HDR_CNT, reg);
1901 reg = (encode_rcv_header_entry_size(rcd->rcvhdrqentsize)
1902 & RCV_HDR_ENT_SIZE_ENT_SIZE_MASK)
1903 << RCV_HDR_ENT_SIZE_ENT_SIZE_SHIFT;
1904 write_kctxt_csr(dd, rcd->ctxt, RCV_HDR_ENT_SIZE, reg);
1905 reg = ((u64)DEFAULT_RCVHDRSIZE & RCV_HDR_SIZE_HDR_SIZE_MASK)
1906 << RCV_HDR_SIZE_HDR_SIZE_SHIFT;
1907 write_kctxt_csr(dd, rcd->ctxt, RCV_HDR_SIZE, reg);
1908
1909 /*
1910 * Program dummy tail address for every receive context
1911 * before enabling any receive context
1912 */
1913 write_kctxt_csr(dd, rcd->ctxt, RCV_HDR_TAIL_ADDR,
1914 dd->rcvhdrtail_dummy_dma);
1915
1916 return 0;
1917
1918bail_free:
1919 dd_dev_err(dd,
1920 "attempt to allocate 1 page for ctxt %u rcvhdrqtailaddr failed\n",
1921 rcd->ctxt);
1922 dma_free_coherent(&dd->pcidev->dev, amt, rcd->rcvhdrq,
1923 rcd->rcvhdrq_dma);
1924 rcd->rcvhdrq = NULL;
1925bail:
1926 return -ENOMEM;
1927}
1928
1929/**
1930 * allocate eager buffers, both kernel and user contexts.
1931 * @rcd: the context we are setting up.
1932 *
1933 * Allocate the eager TID buffers and program them into hip.
1934 * They are no longer completely contiguous, we do multiple allocation
1935 * calls. Otherwise we get the OOM code involved, by asking for too
1936 * much per call, with disastrous results on some kernels.
1937 */
1938int hfi1_setup_eagerbufs(struct hfi1_ctxtdata *rcd)
1939{
1940 struct hfi1_devdata *dd = rcd->dd;
1941 u32 max_entries, egrtop, alloced_bytes = 0;
1942 gfp_t gfp_flags;
1943 u16 order, idx = 0;
1944 int ret = 0;
1945 u16 round_mtu = roundup_pow_of_two(hfi1_max_mtu);
1946
1947 /*
1948 * GFP_USER, but without GFP_FS, so buffer cache can be
1949 * coalesced (we hope); otherwise, even at order 4,
1950 * heavy filesystem activity makes these fail, and we can
1951 * use compound pages.
1952 */
1953 gfp_flags = __GFP_RECLAIM | __GFP_IO | __GFP_COMP;
1954
1955 /*
1956 * The minimum size of the eager buffers is a groups of MTU-sized
1957 * buffers.
1958 * The global eager_buffer_size parameter is checked against the
1959 * theoretical lower limit of the value. Here, we check against the
1960 * MTU.
1961 */
1962 if (rcd->egrbufs.size < (round_mtu * dd->rcv_entries.group_size))
1963 rcd->egrbufs.size = round_mtu * dd->rcv_entries.group_size;
1964 /*
1965 * If using one-pkt-per-egr-buffer, lower the eager buffer
1966 * size to the max MTU (page-aligned).
1967 */
1968 if (!HFI1_CAP_KGET_MASK(rcd->flags, MULTI_PKT_EGR))
1969 rcd->egrbufs.rcvtid_size = round_mtu;
1970
1971 /*
1972 * Eager buffers sizes of 1MB or less require smaller TID sizes
1973 * to satisfy the "multiple of 8 RcvArray entries" requirement.
1974 */
1975 if (rcd->egrbufs.size <= (1 << 20))
1976 rcd->egrbufs.rcvtid_size = max((unsigned long)round_mtu,
1977 rounddown_pow_of_two(rcd->egrbufs.size / 8));
1978
1979 while (alloced_bytes < rcd->egrbufs.size &&
1980 rcd->egrbufs.alloced < rcd->egrbufs.count) {
1981 rcd->egrbufs.buffers[idx].addr =
David Brazdil0f672f62019-12-10 10:32:29 +00001982 dma_alloc_coherent(&dd->pcidev->dev,
1983 rcd->egrbufs.rcvtid_size,
1984 &rcd->egrbufs.buffers[idx].dma,
1985 gfp_flags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001986 if (rcd->egrbufs.buffers[idx].addr) {
1987 rcd->egrbufs.buffers[idx].len =
1988 rcd->egrbufs.rcvtid_size;
1989 rcd->egrbufs.rcvtids[rcd->egrbufs.alloced].addr =
1990 rcd->egrbufs.buffers[idx].addr;
1991 rcd->egrbufs.rcvtids[rcd->egrbufs.alloced].dma =
1992 rcd->egrbufs.buffers[idx].dma;
1993 rcd->egrbufs.alloced++;
1994 alloced_bytes += rcd->egrbufs.rcvtid_size;
1995 idx++;
1996 } else {
1997 u32 new_size, i, j;
1998 u64 offset = 0;
1999
2000 /*
2001 * Fail the eager buffer allocation if:
2002 * - we are already using the lowest acceptable size
2003 * - we are using one-pkt-per-egr-buffer (this implies
2004 * that we are accepting only one size)
2005 */
2006 if (rcd->egrbufs.rcvtid_size == round_mtu ||
2007 !HFI1_CAP_KGET_MASK(rcd->flags, MULTI_PKT_EGR)) {
2008 dd_dev_err(dd, "ctxt%u: Failed to allocate eager buffers\n",
2009 rcd->ctxt);
2010 ret = -ENOMEM;
2011 goto bail_rcvegrbuf_phys;
2012 }
2013
2014 new_size = rcd->egrbufs.rcvtid_size / 2;
2015
2016 /*
2017 * If the first attempt to allocate memory failed, don't
2018 * fail everything but continue with the next lower
2019 * size.
2020 */
2021 if (idx == 0) {
2022 rcd->egrbufs.rcvtid_size = new_size;
2023 continue;
2024 }
2025
2026 /*
2027 * Re-partition already allocated buffers to a smaller
2028 * size.
2029 */
2030 rcd->egrbufs.alloced = 0;
2031 for (i = 0, j = 0, offset = 0; j < idx; i++) {
2032 if (i >= rcd->egrbufs.count)
2033 break;
2034 rcd->egrbufs.rcvtids[i].dma =
2035 rcd->egrbufs.buffers[j].dma + offset;
2036 rcd->egrbufs.rcvtids[i].addr =
2037 rcd->egrbufs.buffers[j].addr + offset;
2038 rcd->egrbufs.alloced++;
2039 if ((rcd->egrbufs.buffers[j].dma + offset +
2040 new_size) ==
2041 (rcd->egrbufs.buffers[j].dma +
2042 rcd->egrbufs.buffers[j].len)) {
2043 j++;
2044 offset = 0;
2045 } else {
2046 offset += new_size;
2047 }
2048 }
2049 rcd->egrbufs.rcvtid_size = new_size;
2050 }
2051 }
2052 rcd->egrbufs.numbufs = idx;
2053 rcd->egrbufs.size = alloced_bytes;
2054
2055 hfi1_cdbg(PROC,
David Brazdil0f672f62019-12-10 10:32:29 +00002056 "ctxt%u: Alloced %u rcv tid entries @ %uKB, total %uKB\n",
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002057 rcd->ctxt, rcd->egrbufs.alloced,
2058 rcd->egrbufs.rcvtid_size / 1024, rcd->egrbufs.size / 1024);
2059
2060 /*
2061 * Set the contexts rcv array head update threshold to the closest
2062 * power of 2 (so we can use a mask instead of modulo) below half
2063 * the allocated entries.
2064 */
2065 rcd->egrbufs.threshold =
2066 rounddown_pow_of_two(rcd->egrbufs.alloced / 2);
2067 /*
2068 * Compute the expected RcvArray entry base. This is done after
2069 * allocating the eager buffers in order to maximize the
2070 * expected RcvArray entries for the context.
2071 */
2072 max_entries = rcd->rcv_array_groups * dd->rcv_entries.group_size;
2073 egrtop = roundup(rcd->egrbufs.alloced, dd->rcv_entries.group_size);
2074 rcd->expected_count = max_entries - egrtop;
2075 if (rcd->expected_count > MAX_TID_PAIR_ENTRIES * 2)
2076 rcd->expected_count = MAX_TID_PAIR_ENTRIES * 2;
2077
2078 rcd->expected_base = rcd->eager_base + egrtop;
2079 hfi1_cdbg(PROC, "ctxt%u: eager:%u, exp:%u, egrbase:%u, expbase:%u\n",
2080 rcd->ctxt, rcd->egrbufs.alloced, rcd->expected_count,
2081 rcd->eager_base, rcd->expected_base);
2082
2083 if (!hfi1_rcvbuf_validate(rcd->egrbufs.rcvtid_size, PT_EAGER, &order)) {
2084 hfi1_cdbg(PROC,
2085 "ctxt%u: current Eager buffer size is invalid %u\n",
2086 rcd->ctxt, rcd->egrbufs.rcvtid_size);
2087 ret = -EINVAL;
2088 goto bail_rcvegrbuf_phys;
2089 }
2090
2091 for (idx = 0; idx < rcd->egrbufs.alloced; idx++) {
2092 hfi1_put_tid(dd, rcd->eager_base + idx, PT_EAGER,
2093 rcd->egrbufs.rcvtids[idx].dma, order);
2094 cond_resched();
2095 }
2096
2097 return 0;
2098
2099bail_rcvegrbuf_phys:
2100 for (idx = 0; idx < rcd->egrbufs.alloced &&
2101 rcd->egrbufs.buffers[idx].addr;
2102 idx++) {
2103 dma_free_coherent(&dd->pcidev->dev,
2104 rcd->egrbufs.buffers[idx].len,
2105 rcd->egrbufs.buffers[idx].addr,
2106 rcd->egrbufs.buffers[idx].dma);
2107 rcd->egrbufs.buffers[idx].addr = NULL;
2108 rcd->egrbufs.buffers[idx].dma = 0;
2109 rcd->egrbufs.buffers[idx].len = 0;
2110 }
2111
2112 return ret;
2113}