blob: 8c4f04032b282c88f543c34dd1e5aaac0f0f9485 [file] [log] [blame]
Mike Marciniszyn77241052015-07-30 15:17:43 -04001/*
Vishwanathapura, Niranjanad4829ea2017-04-12 20:29:28 -07002 * Copyright(c) 2015-2017 Intel Corporation.
Mike Marciniszyn77241052015-07-30 15:17:43 -04003 *
4 * This file is provided under a dual BSD/GPLv2 license. When using or
5 * redistributing this file, you may do so under either license.
6 *
7 * GPL LICENSE SUMMARY
8 *
Mike Marciniszyn77241052015-07-30 15:17:43 -04009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of version 2 of the GNU General Public License as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * BSD LICENSE
19 *
Mike Marciniszyn77241052015-07-30 15:17:43 -040020 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
22 * are met:
23 *
24 * - Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * - Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in
28 * the documentation and/or other materials provided with the
29 * distribution.
30 * - Neither the name of Intel Corporation nor the names of its
31 * contributors may be used to endorse or promote products derived
32 * from this software without specific prior written permission.
33 *
34 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
35 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
37 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
38 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
41 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
42 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
44 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45 *
46 */
47
48#include <linux/pci.h>
49#include <linux/netdevice.h>
50#include <linux/vmalloc.h>
51#include <linux/delay.h>
52#include <linux/idr.h>
53#include <linux/module.h>
54#include <linux/printk.h>
55#include <linux/hrtimer.h>
Michael J. Ruhl8737ce92017-05-04 05:15:15 -070056#include <linux/bitmap.h>
Dennis Dalessandroec3f2c12016-01-19 14:41:33 -080057#include <rdma/rdma_vt.h>
Mike Marciniszyn77241052015-07-30 15:17:43 -040058
59#include "hfi.h"
60#include "device.h"
61#include "common.h"
Sebastian Sanchez6c63e422015-11-06 20:06:56 -050062#include "trace.h"
Mike Marciniszyn77241052015-07-30 15:17:43 -040063#include "mad.h"
64#include "sdma.h"
65#include "debugfs.h"
66#include "verbs.h"
Ashutosh Dixitaffa48d2016-02-03 14:33:06 -080067#include "aspm.h"
Dennis Dalessandro41973442016-07-25 07:52:36 -070068#include "affinity.h"
Vishwanathapura, Niranjanad4829ea2017-04-12 20:29:28 -070069#include "vnic.h"
Michael J. Ruhlfe4e74e2017-06-09 16:00:12 -070070#include "exp_rcv.h"
Mike Marciniszyn77241052015-07-30 15:17:43 -040071
72#undef pr_fmt
73#define pr_fmt(fmt) DRIVER_NAME ": " fmt
74
Mike Marciniszyndd1ed102017-05-04 05:14:10 -070075#define HFI1_MAX_ACTIVE_WORKQUEUE_ENTRIES 5
Mike Marciniszyn77241052015-07-30 15:17:43 -040076/*
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
Sebastian Sancheze002dcc2016-02-03 14:34:32 -080082#define HFI1_MAX_HDRQ_EGRBUF_CNT 16352
Mike Marciniszyn77241052015-07-30 15:17:43 -040083#define HFI1_MIN_EAGER_BUFFER_SIZE (4 * 1024) /* 4KB */
84#define HFI1_MAX_EAGER_BUFFER_SIZE (256 * 1024) /* 256KB */
85
86/*
87 * Number of user receive contexts we are configured to use (to allow for more
88 * pio buffers per ctxt, etc.) Zero means use one user context per CPU.
89 */
Sebastian Sanchez2ce6bf22015-12-11 08:44:48 -050090int num_user_contexts = -1;
91module_param_named(num_user_contexts, num_user_contexts, uint, S_IRUGO);
Mike Marciniszyn77241052015-07-30 15:17:43 -040092MODULE_PARM_DESC(
Sebastian Sanchez2ce6bf22015-12-11 08:44:48 -050093 num_user_contexts, "Set max number of user contexts to use");
Mike Marciniszyn77241052015-07-30 15:17:43 -040094
Mark F. Brown5b55ea32016-01-11 18:30:54 -050095uint krcvqs[RXE_NUM_DATA_VL];
Mike Marciniszyn77241052015-07-30 15:17:43 -040096int krcvqsset;
Mark F. Brown5b55ea32016-01-11 18:30:54 -050097module_param_array(krcvqs, uint, &krcvqsset, S_IRUGO);
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -050098MODULE_PARM_DESC(krcvqs, "Array of the number of non-control kernel receive queues by VL");
Mike Marciniszyn77241052015-07-30 15:17:43 -040099
100/* computed based on above array */
Harish Chegondi429b6a72016-08-31 07:24:40 -0700101unsigned long n_krcvqs;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400102
103static unsigned hfi1_rcvarr_split = 25;
104module_param_named(rcvarr_split, hfi1_rcvarr_split, uint, S_IRUGO);
105MODULE_PARM_DESC(rcvarr_split, "Percent of context's RcvArray entries used for Eager buffers");
106
Tymoteusz Kielan9746fa42017-05-04 05:14:22 -0700107static uint eager_buffer_size = (8 << 20); /* 8MB */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400108module_param(eager_buffer_size, uint, S_IRUGO);
Tymoteusz Kielan9746fa42017-05-04 05:14:22 -0700109MODULE_PARM_DESC(eager_buffer_size, "Size of the eager buffers, default: 8MB");
Mike Marciniszyn77241052015-07-30 15:17:43 -0400110
111static uint rcvhdrcnt = 2048; /* 2x the max eager buffer count */
112module_param_named(rcvhdrcnt, rcvhdrcnt, uint, S_IRUGO);
113MODULE_PARM_DESC(rcvhdrcnt, "Receive header queue count (default 2048)");
114
115static uint hfi1_hdrq_entsize = 32;
116module_param_named(hdrq_entsize, hfi1_hdrq_entsize, uint, S_IRUGO);
117MODULE_PARM_DESC(hdrq_entsize, "Size of header queue entries: 2 - 8B, 16 - 64B (default), 32 - 128B");
118
119unsigned int user_credit_return_threshold = 33; /* default is 33% */
120module_param(user_credit_return_threshold, uint, S_IRUGO);
Jubin Johnecb95a02015-12-17 19:24:14 -0500121MODULE_PARM_DESC(user_credit_return_threshold, "Credit return threshold for user send contexts, return when unreturned credits passes this many blocks (in percent of allocated blocks, 0 is off)");
Mike Marciniszyn77241052015-07-30 15:17:43 -0400122
Michael J. Ruhlf4cd8762017-05-04 05:14:39 -0700123static inline u64 encode_rcv_header_entry_size(u16 size);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400124
125static struct idr hfi1_unit_table;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400126
Michael J. Ruhlf2a3bc02017-08-04 13:52:38 -0700127static int hfi1_create_kctxt(struct hfi1_devdata *dd,
128 struct hfi1_pportdata *ppd)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400129{
Michael J. Ruhlf2a3bc02017-08-04 13:52:38 -0700130 struct hfi1_ctxtdata *rcd;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400131 int ret;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400132
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500133 /* Control context has to be always 0 */
134 BUILD_BUG_ON(HFI1_CTRL_CTXT != 0);
135
Michael J. Ruhlf2a3bc02017-08-04 13:52:38 -0700136 ret = hfi1_create_ctxtdata(ppd, dd->node, &rcd);
137 if (ret < 0) {
138 dd_dev_err(dd, "Kernel receive context allocation failed\n");
139 return ret;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400140 }
141
Ashutosh Dixitaffa48d2016-02-03 14:33:06 -0800142 /*
Michael J. Ruhlf2a3bc02017-08-04 13:52:38 -0700143 * Set up the kernel context flags here and now because they use
144 * default values for all receive side memories. User contexts will
145 * be handled as they are created.
Ashutosh Dixitaffa48d2016-02-03 14:33:06 -0800146 */
Michael J. Ruhlf2a3bc02017-08-04 13:52:38 -0700147 rcd->flags = HFI1_CAP_KGET(MULTI_PKT_EGR) |
148 HFI1_CAP_KGET(NODROP_RHQ_FULL) |
149 HFI1_CAP_KGET(NODROP_EGR_FULL) |
150 HFI1_CAP_KGET(DMA_RTAIL);
151
152 /* Control context must use DMA_RTAIL */
153 if (rcd->ctxt == HFI1_CTRL_CTXT)
154 rcd->flags |= HFI1_CAP_DMA_RTAIL;
155 rcd->seq_cnt = 1;
156
157 rcd->sc = sc_alloc(dd, SC_ACK, rcd->rcvhdrqentsize, dd->node);
158 if (!rcd->sc) {
159 dd_dev_err(dd, "Kernel send context allocation failed\n");
160 return -ENOMEM;
161 }
162 hfi1_init_ctxt(rcd->sc);
Ashutosh Dixitaffa48d2016-02-03 14:33:06 -0800163
Mike Marciniszyn77241052015-07-30 15:17:43 -0400164 return 0;
Michael J. Ruhlf2a3bc02017-08-04 13:52:38 -0700165}
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -0700166
Michael J. Ruhlf2a3bc02017-08-04 13:52:38 -0700167/*
168 * Create the receive context array and one or more kernel contexts
169 */
170int hfi1_create_kctxts(struct hfi1_devdata *dd)
171{
172 u16 i;
173 int ret;
174
175 dd->rcd = kzalloc_node(dd->num_rcv_contexts * sizeof(*dd->rcd),
176 GFP_KERNEL, dd->node);
177 if (!dd->rcd)
178 return -ENOMEM;
179
180 for (i = 0; i < dd->first_dyn_alloc_ctxt; ++i) {
181 ret = hfi1_create_kctxt(dd, dd->pport);
182 if (ret)
183 goto bail;
184 }
185
186 return 0;
187bail:
Michael J. Ruhlf683c802017-06-09 16:00:19 -0700188 for (i = 0; dd->rcd && i < dd->first_dyn_alloc_ctxt; ++i)
Michael J. Ruhld295dbe2017-08-04 13:52:44 -0700189 hfi1_free_ctxt(dd->rcd[i]);
Michael J. Ruhlf683c802017-06-09 16:00:19 -0700190
191 /* All the contexts should be freed, free the array */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400192 kfree(dd->rcd);
193 dd->rcd = NULL;
194 return ret;
195}
196
197/*
Michael J. Ruhld295dbe2017-08-04 13:52:44 -0700198 * Helper routines for the receive context reference count (rcd and uctxt).
Michael J. Ruhlf683c802017-06-09 16:00:19 -0700199 */
200static void hfi1_rcd_init(struct hfi1_ctxtdata *rcd)
201{
202 kref_init(&rcd->kref);
203}
204
Michael J. Ruhlf2a3bc02017-08-04 13:52:38 -0700205/**
206 * hfi1_rcd_free - When reference is zero clean up.
207 * @kref: pointer to an initialized rcd data structure
208 *
209 */
Michael J. Ruhlf683c802017-06-09 16:00:19 -0700210static void hfi1_rcd_free(struct kref *kref)
211{
Michael J. Ruhld295dbe2017-08-04 13:52:44 -0700212 unsigned long flags;
Michael J. Ruhlf683c802017-06-09 16:00:19 -0700213 struct hfi1_ctxtdata *rcd =
214 container_of(kref, struct hfi1_ctxtdata, kref);
215
216 hfi1_free_ctxtdata(rcd->dd, rcd);
Michael J. Ruhld295dbe2017-08-04 13:52:44 -0700217
218 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
Michael J. Ruhlf683c802017-06-09 16:00:19 -0700222 kfree(rcd);
223}
224
Michael J. Ruhlf2a3bc02017-08-04 13:52:38 -0700225/**
226 * hfi1_rcd_put - decrement reference for rcd
227 * @rcd: pointer to an initialized rcd data structure
228 *
229 * Use this to put a reference after the init.
230 */
Michael J. Ruhlf683c802017-06-09 16:00:19 -0700231int hfi1_rcd_put(struct hfi1_ctxtdata *rcd)
232{
233 if (rcd)
234 return kref_put(&rcd->kref, hfi1_rcd_free);
235
236 return 0;
237}
238
Michael J. Ruhlf2a3bc02017-08-04 13:52:38 -0700239/**
240 * hfi1_rcd_get - increment reference for rcd
241 * @rcd: pointer to an initialized rcd data structure
242 *
243 * Use this to get a reference after the init.
244 */
Michael J. Ruhlf683c802017-06-09 16:00:19 -0700245void hfi1_rcd_get(struct hfi1_ctxtdata *rcd)
246{
247 kref_get(&rcd->kref);
248}
249
Michael J. Ruhlf2a3bc02017-08-04 13:52:38 -0700250/**
251 * allocate_rcd_index - allocate an rcd index from the rcd array
252 * @dd: pointer to a valid devdata structure
253 * @rcd: rcd data structure to assign
254 * @index: pointer to index that is allocated
255 *
256 * Find an empty index in the rcd array, and assign the given rcd to it.
257 * If the array is full, we are EBUSY.
258 *
259 */
Michael J. Ruhld295dbe2017-08-04 13:52:44 -0700260static int allocate_rcd_index(struct hfi1_devdata *dd,
Michael J. Ruhlf2a3bc02017-08-04 13:52:38 -0700261 struct hfi1_ctxtdata *rcd, u16 *index)
262{
263 unsigned long flags;
264 u16 ctxt;
265
266 spin_lock_irqsave(&dd->uctxt_lock, flags);
267 for (ctxt = 0; ctxt < dd->num_rcv_contexts; ctxt++)
268 if (!dd->rcd[ctxt])
269 break;
270
271 if (ctxt < dd->num_rcv_contexts) {
272 rcd->ctxt = ctxt;
273 dd->rcd[ctxt] = rcd;
274 hfi1_rcd_init(rcd);
275 }
276 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
277
278 if (ctxt >= dd->num_rcv_contexts)
279 return -EBUSY;
280
281 *index = ctxt;
282
283 return 0;
284}
285
Michael J. Ruhld295dbe2017-08-04 13:52:44 -0700286/**
Michael J. Ruhld59075a2017-09-26 07:01:16 -0700287 * hfi1_rcd_get_by_index_safe - validate the ctxt index before accessing the
288 * array
289 * @dd: pointer to a valid devdata structure
290 * @ctxt: the index of an possilbe rcd
291 *
292 * This is a wrapper for hfi1_rcd_get_by_index() to validate that the given
293 * ctxt index is valid.
294 *
295 * The caller is responsible for making the _put().
296 *
297 */
298struct hfi1_ctxtdata *hfi1_rcd_get_by_index_safe(struct hfi1_devdata *dd,
299 u16 ctxt)
300{
301 if (ctxt < dd->num_rcv_contexts)
302 return hfi1_rcd_get_by_index(dd, ctxt);
303
304 return NULL;
305}
306
307/**
Michael J. Ruhld295dbe2017-08-04 13:52:44 -0700308 * hfi1_rcd_get_by_index
309 * @dd: pointer to a valid devdata structure
310 * @ctxt: the index of an possilbe rcd
311 *
312 * We need to protect access to the rcd array. If access is needed to
313 * one or more index, get the protecting spinlock and then increment the
314 * kref.
315 *
316 * The caller is responsible for making the _put().
317 *
318 */
319struct hfi1_ctxtdata *hfi1_rcd_get_by_index(struct hfi1_devdata *dd, u16 ctxt)
320{
321 unsigned long flags;
322 struct hfi1_ctxtdata *rcd = NULL;
323
324 spin_lock_irqsave(&dd->uctxt_lock, flags);
325 if (dd->rcd[ctxt]) {
326 rcd = dd->rcd[ctxt];
327 hfi1_rcd_get(rcd);
328 }
329 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
330
331 return rcd;
332}
333
Michael J. Ruhlf683c802017-06-09 16:00:19 -0700334/*
Michael J. Ruhld295dbe2017-08-04 13:52:44 -0700335 * Common code for user and kernel context create and setup.
336 * NOTE: the initial kref is done here (hf1_rcd_init()).
Mike Marciniszyn77241052015-07-30 15:17:43 -0400337 */
Michael J. Ruhlf2a3bc02017-08-04 13:52:38 -0700338int hfi1_create_ctxtdata(struct hfi1_pportdata *ppd, int numa,
339 struct hfi1_ctxtdata **context)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400340{
341 struct hfi1_devdata *dd = ppd->dd;
342 struct hfi1_ctxtdata *rcd;
343 unsigned kctxt_ngroups = 0;
344 u32 base;
345
346 if (dd->rcv_entries.nctxt_extra >
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700347 dd->num_rcv_contexts - dd->first_dyn_alloc_ctxt)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400348 kctxt_ngroups = (dd->rcv_entries.nctxt_extra -
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700349 (dd->num_rcv_contexts - dd->first_dyn_alloc_ctxt));
Jianxin Xiong4dfe7cc2016-10-17 04:19:41 -0700350 rcd = kzalloc_node(sizeof(*rcd), GFP_KERNEL, numa);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400351 if (rcd) {
352 u32 rcvtids, max_entries;
Michael J. Ruhlf2a3bc02017-08-04 13:52:38 -0700353 u16 ctxt;
354 int ret;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400355
Michael J. Ruhlf2a3bc02017-08-04 13:52:38 -0700356 ret = allocate_rcd_index(dd, rcd, &ctxt);
357 if (ret) {
358 *context = NULL;
359 kfree(rcd);
360 return ret;
361 }
362
Mike Marciniszyn77241052015-07-30 15:17:43 -0400363 INIT_LIST_HEAD(&rcd->qp_wait_list);
Michael J. Ruhlfe4e74e2017-06-09 16:00:12 -0700364 hfi1_exp_tid_group_init(&rcd->tid_group_list);
365 hfi1_exp_tid_group_init(&rcd->tid_used_list);
366 hfi1_exp_tid_group_init(&rcd->tid_full_list);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400367 rcd->ppd = ppd;
368 rcd->dd = dd;
Michael J. Ruhl8737ce92017-05-04 05:15:15 -0700369 __set_bit(0, rcd->in_use_ctxts);
Mitko Haralanov957558c2016-02-03 14:33:40 -0800370 rcd->numa_id = numa;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400371 rcd->rcv_array_groups = dd->rcv_entries.ngroups;
372
Mitko Haralanov463e6eb2016-02-05 11:57:53 -0500373 mutex_init(&rcd->exp_lock);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400374
Michael J. Ruhld295dbe2017-08-04 13:52:44 -0700375 hfi1_cdbg(PROC, "setting up context %u\n", rcd->ctxt);
376
Mike Marciniszyn77241052015-07-30 15:17:43 -0400377 /*
378 * Calculate the context's RcvArray entry starting point.
379 * We do this here because we have to take into account all
380 * the RcvArray entries that previous context would have
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700381 * taken and we have to account for any extra groups assigned
382 * to the static (kernel) or dynamic (vnic/user) contexts.
Mike Marciniszyn77241052015-07-30 15:17:43 -0400383 */
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700384 if (ctxt < dd->first_dyn_alloc_ctxt) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400385 if (ctxt < kctxt_ngroups) {
386 base = ctxt * (dd->rcv_entries.ngroups + 1);
387 rcd->rcv_array_groups++;
Dennis Dalessandroee495ad2017-04-09 10:17:18 -0700388 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400389 base = kctxt_ngroups +
390 (ctxt * dd->rcv_entries.ngroups);
Dennis Dalessandroee495ad2017-04-09 10:17:18 -0700391 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400392 } else {
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700393 u16 ct = ctxt - dd->first_dyn_alloc_ctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400394
395 base = ((dd->n_krcv_queues * dd->rcv_entries.ngroups) +
396 kctxt_ngroups);
397 if (ct < dd->rcv_entries.nctxt_extra) {
398 base += ct * (dd->rcv_entries.ngroups + 1);
399 rcd->rcv_array_groups++;
Dennis Dalessandroee495ad2017-04-09 10:17:18 -0700400 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400401 base += dd->rcv_entries.nctxt_extra +
402 (ct * dd->rcv_entries.ngroups);
Dennis Dalessandroee495ad2017-04-09 10:17:18 -0700403 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400404 }
405 rcd->eager_base = base * dd->rcv_entries.group_size;
406
Mike Marciniszyn77241052015-07-30 15:17:43 -0400407 rcd->rcvhdrq_cnt = rcvhdrcnt;
408 rcd->rcvhdrqentsize = hfi1_hdrq_entsize;
409 /*
410 * Simple Eager buffer allocation: we have already pre-allocated
411 * the number of RcvArray entry groups. Each ctxtdata structure
412 * holds the number of groups for that context.
413 *
414 * To follow CSR requirements and maintain cacheline alignment,
415 * make sure all sizes and bases are multiples of group_size.
416 *
417 * The expected entry count is what is left after assigning
418 * eager.
419 */
420 max_entries = rcd->rcv_array_groups *
421 dd->rcv_entries.group_size;
422 rcvtids = ((max_entries * hfi1_rcvarr_split) / 100);
423 rcd->egrbufs.count = round_down(rcvtids,
424 dd->rcv_entries.group_size);
425 if (rcd->egrbufs.count > MAX_EAGER_ENTRIES) {
426 dd_dev_err(dd, "ctxt%u: requested too many RcvArray entries.\n",
427 rcd->ctxt);
428 rcd->egrbufs.count = MAX_EAGER_ENTRIES;
429 }
Sebastian Sanchez6c63e422015-11-06 20:06:56 -0500430 hfi1_cdbg(PROC,
431 "ctxt%u: max Eager buffer RcvArray entries: %u\n",
432 rcd->ctxt, rcd->egrbufs.count);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400433
434 /*
435 * Allocate array that will hold the eager buffer accounting
436 * data.
437 * This will allocate the maximum possible buffer count based
438 * on the value of the RcvArray split parameter.
439 * The resulting value will be rounded down to the closest
440 * multiple of dd->rcv_entries.group_size.
441 */
Sebastian Sanchezb448bf92017-02-08 05:26:37 -0800442 rcd->egrbufs.buffers = kzalloc_node(
443 rcd->egrbufs.count * sizeof(*rcd->egrbufs.buffers),
444 GFP_KERNEL, numa);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400445 if (!rcd->egrbufs.buffers)
446 goto bail;
Sebastian Sanchezb448bf92017-02-08 05:26:37 -0800447 rcd->egrbufs.rcvtids = kzalloc_node(
448 rcd->egrbufs.count *
449 sizeof(*rcd->egrbufs.rcvtids),
450 GFP_KERNEL, numa);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400451 if (!rcd->egrbufs.rcvtids)
452 goto bail;
453 rcd->egrbufs.size = eager_buffer_size;
454 /*
455 * The size of the buffers programmed into the RcvArray
456 * entries needs to be big enough to handle the highest
457 * MTU supported.
458 */
459 if (rcd->egrbufs.size < hfi1_max_mtu) {
460 rcd->egrbufs.size = __roundup_pow_of_two(hfi1_max_mtu);
Sebastian Sanchez6c63e422015-11-06 20:06:56 -0500461 hfi1_cdbg(PROC,
462 "ctxt%u: eager bufs size too small. Adjusting to %zu\n",
Mike Marciniszyn77241052015-07-30 15:17:43 -0400463 rcd->ctxt, rcd->egrbufs.size);
464 }
465 rcd->egrbufs.rcvtid_size = HFI1_MAX_EAGER_BUFFER_SIZE;
466
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700467 /* Applicable only for statically created kernel contexts */
468 if (ctxt < dd->first_dyn_alloc_ctxt) {
Sebastian Sanchezb448bf92017-02-08 05:26:37 -0800469 rcd->opstats = kzalloc_node(sizeof(*rcd->opstats),
470 GFP_KERNEL, numa);
Alison Schofield806e6e12015-10-12 14:28:36 -0700471 if (!rcd->opstats)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400472 goto bail;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400473 }
Michael J. Ruhlf683c802017-06-09 16:00:19 -0700474
Michael J. Ruhlf2a3bc02017-08-04 13:52:38 -0700475 *context = rcd;
476 return 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400477 }
Michael J. Ruhlf2a3bc02017-08-04 13:52:38 -0700478
Mike Marciniszyn77241052015-07-30 15:17:43 -0400479bail:
Michael J. Ruhlf2a3bc02017-08-04 13:52:38 -0700480 *context = NULL;
Michael J. Ruhld295dbe2017-08-04 13:52:44 -0700481 hfi1_free_ctxt(rcd);
Michael J. Ruhlf2a3bc02017-08-04 13:52:38 -0700482 return -ENOMEM;
483}
484
485/**
486 * hfi1_free_ctxt
Michael J. Ruhlf2a3bc02017-08-04 13:52:38 -0700487 * @rcd: pointer to an initialized rcd data structure
488 *
Michael J. Ruhld295dbe2017-08-04 13:52:44 -0700489 * This wrapper is the free function that matches hfi1_create_ctxtdata().
490 * When a context is done being used (kernel or user), this function is called
491 * for the "final" put to match the kref init from hf1i_create_ctxtdata().
492 * Other users of the context do a get/put sequence to make sure that the
493 * structure isn't removed while in use.
Michael J. Ruhlf2a3bc02017-08-04 13:52:38 -0700494 */
Michael J. Ruhld295dbe2017-08-04 13:52:44 -0700495void hfi1_free_ctxt(struct hfi1_ctxtdata *rcd)
Michael J. Ruhlf2a3bc02017-08-04 13:52:38 -0700496{
Michael J. Ruhld295dbe2017-08-04 13:52:44 -0700497 hfi1_rcd_put(rcd);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400498}
499
500/*
501 * Convert a receive header entry size that to the encoding used in the CSR.
502 *
503 * Return a zero if the given size is invalid.
504 */
505static inline u64 encode_rcv_header_entry_size(u16 size)
506{
507 /* there are only 3 valid receive header entry sizes */
508 if (size == 2)
509 return 1;
510 if (size == 16)
511 return 2;
512 else if (size == 32)
513 return 4;
514 return 0; /* invalid */
515}
516
517/*
518 * Select the largest ccti value over all SLs to determine the intra-
519 * packet gap for the link.
520 *
521 * called with cca_timer_lock held (to protect access to cca_timer
522 * array), and rcu_read_lock() (to protect access to cc_state).
523 */
524void set_link_ipg(struct hfi1_pportdata *ppd)
525{
526 struct hfi1_devdata *dd = ppd->dd;
527 struct cc_state *cc_state;
528 int i;
529 u16 cce, ccti_limit, max_ccti = 0;
530 u16 shift, mult;
531 u64 src;
532 u32 current_egress_rate; /* Mbits /sec */
533 u32 max_pkt_time;
534 /*
535 * max_pkt_time is the maximum packet egress time in units
536 * of the fabric clock period 1/(805 MHz).
537 */
538
539 cc_state = get_cc_state(ppd);
540
Jubin Johnd125a6c2016-02-14 20:19:49 -0800541 if (!cc_state)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400542 /*
543 * This should _never_ happen - rcu_read_lock() is held,
544 * and set_link_ipg() should not be called if cc_state
545 * is NULL.
546 */
547 return;
548
549 for (i = 0; i < OPA_MAX_SLS; i++) {
550 u16 ccti = ppd->cca_timer[i].ccti;
551
552 if (ccti > max_ccti)
553 max_ccti = ccti;
554 }
555
556 ccti_limit = cc_state->cct.ccti_limit;
557 if (max_ccti > ccti_limit)
558 max_ccti = ccti_limit;
559
560 cce = cc_state->cct.entries[max_ccti].entry;
561 shift = (cce & 0xc000) >> 14;
562 mult = (cce & 0x3fff);
563
564 current_egress_rate = active_egress_rate(ppd);
565
566 max_pkt_time = egress_cycles(ppd->ibmaxlen, current_egress_rate);
567
568 src = (max_pkt_time >> shift) * mult;
569
570 src &= SEND_STATIC_RATE_CONTROL_CSR_SRC_RELOAD_SMASK;
571 src <<= SEND_STATIC_RATE_CONTROL_CSR_SRC_RELOAD_SHIFT;
572
573 write_csr(dd, SEND_STATIC_RATE_CONTROL, src);
574}
575
576static enum hrtimer_restart cca_timer_fn(struct hrtimer *t)
577{
578 struct cca_timer *cca_timer;
579 struct hfi1_pportdata *ppd;
580 int sl;
Jubin Johnd35cf7442016-04-14 08:31:53 -0700581 u16 ccti_timer, ccti_min;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400582 struct cc_state *cc_state;
Dean Luickb77d7132015-10-26 10:28:43 -0400583 unsigned long flags;
Jubin Johnd35cf7442016-04-14 08:31:53 -0700584 enum hrtimer_restart ret = HRTIMER_NORESTART;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400585
586 cca_timer = container_of(t, struct cca_timer, hrtimer);
587 ppd = cca_timer->ppd;
588 sl = cca_timer->sl;
589
590 rcu_read_lock();
591
592 cc_state = get_cc_state(ppd);
593
Jubin Johnd125a6c2016-02-14 20:19:49 -0800594 if (!cc_state) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400595 rcu_read_unlock();
596 return HRTIMER_NORESTART;
597 }
598
599 /*
600 * 1) decrement ccti for SL
601 * 2) calculate IPG for link (set_link_ipg())
602 * 3) restart timer, unless ccti is at min value
603 */
604
605 ccti_min = cc_state->cong_setting.entries[sl].ccti_min;
606 ccti_timer = cc_state->cong_setting.entries[sl].ccti_timer;
607
Dean Luickb77d7132015-10-26 10:28:43 -0400608 spin_lock_irqsave(&ppd->cca_timer_lock, flags);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400609
Jubin Johnd35cf7442016-04-14 08:31:53 -0700610 if (cca_timer->ccti > ccti_min) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400611 cca_timer->ccti--;
612 set_link_ipg(ppd);
613 }
614
Jubin Johnd35cf7442016-04-14 08:31:53 -0700615 if (cca_timer->ccti > ccti_min) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400616 unsigned long nsec = 1024 * ccti_timer;
617 /* ccti_timer is in units of 1.024 usec */
618 hrtimer_forward_now(t, ns_to_ktime(nsec));
Jubin Johnd35cf7442016-04-14 08:31:53 -0700619 ret = HRTIMER_RESTART;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400620 }
Jubin Johnd35cf7442016-04-14 08:31:53 -0700621
622 spin_unlock_irqrestore(&ppd->cca_timer_lock, flags);
623 rcu_read_unlock();
624 return ret;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400625}
626
627/*
628 * Common code for initializing the physical port structure.
629 */
630void hfi1_init_pportdata(struct pci_dev *pdev, struct hfi1_pportdata *ppd,
631 struct hfi1_devdata *dd, u8 hw_pidx, u8 port)
632{
Jianxin Xiong8adf71f2016-07-25 13:39:14 -0700633 int i;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400634 uint default_pkey_idx;
Jianxin Xiong8adf71f2016-07-25 13:39:14 -0700635 struct cc_state *cc_state;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400636
637 ppd->dd = dd;
638 ppd->hw_pidx = hw_pidx;
639 ppd->port = port; /* IB port number, not index */
Kamenee Arumugam07190072018-02-01 10:52:28 -0800640 ppd->prev_link_width = LINK_WIDTH_DEFAULT;
641 /*
642 * There are C_VL_COUNT number of PortVLXmitWait counters.
643 * Adding 1 to C_VL_COUNT to include the PortXmitWait counter.
644 */
645 for (i = 0; i < C_VL_COUNT + 1; i++) {
646 ppd->port_vl_xmit_wait_last[i] = 0;
647 ppd->vl_xmit_flit_cnt[i] = 0;
648 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400649
650 default_pkey_idx = 1;
651
652 ppd->pkeys[default_pkey_idx] = DEFAULT_P_KEY;
Neel Desai53526502017-04-09 10:16:59 -0700653 ppd->part_enforce |= HFI1_PART_ENFORCE_IN;
Neel Desai53526502017-04-09 10:16:59 -0700654
Mike Marciniszyn77241052015-07-30 15:17:43 -0400655 if (loopback) {
656 hfi1_early_err(&pdev->dev,
657 "Faking data partition 0x8001 in idx %u\n",
658 !default_pkey_idx);
659 ppd->pkeys[!default_pkey_idx] = 0x8001;
660 }
661
662 INIT_WORK(&ppd->link_vc_work, handle_verify_cap);
663 INIT_WORK(&ppd->link_up_work, handle_link_up);
664 INIT_WORK(&ppd->link_down_work, handle_link_down);
665 INIT_WORK(&ppd->freeze_work, handle_freeze);
666 INIT_WORK(&ppd->link_downgrade_work, handle_link_downgrade);
667 INIT_WORK(&ppd->sma_message_work, handle_sma_message);
668 INIT_WORK(&ppd->link_bounce_work, handle_link_bounce);
Dean Luick673b9752016-08-31 07:24:33 -0700669 INIT_DELAYED_WORK(&ppd->start_link_work, handle_start_link);
Jim Snowfb9036d2016-01-11 18:32:21 -0500670 INIT_WORK(&ppd->linkstate_active_work, receive_interrupt_work);
Easwar Hariharan8ebd4cf2016-02-03 14:31:14 -0800671 INIT_WORK(&ppd->qsfp_info.qsfp_work, qsfp_event);
672
Mike Marciniszyn77241052015-07-30 15:17:43 -0400673 mutex_init(&ppd->hls_lock);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400674 spin_lock_init(&ppd->qsfp_info.qsfp_lock);
675
Easwar Hariharan8ebd4cf2016-02-03 14:31:14 -0800676 ppd->qsfp_info.ppd = ppd;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400677 ppd->sm_trap_qp = 0x0;
678 ppd->sa_qp = 0x1;
679
680 ppd->hfi1_wq = NULL;
681
682 spin_lock_init(&ppd->cca_timer_lock);
683
684 for (i = 0; i < OPA_MAX_SLS; i++) {
685 hrtimer_init(&ppd->cca_timer[i].hrtimer, CLOCK_MONOTONIC,
686 HRTIMER_MODE_REL);
687 ppd->cca_timer[i].ppd = ppd;
688 ppd->cca_timer[i].sl = i;
689 ppd->cca_timer[i].ccti = 0;
690 ppd->cca_timer[i].hrtimer.function = cca_timer_fn;
691 }
692
693 ppd->cc_max_table_entries = IB_CC_TABLE_CAP_DEFAULT;
694
695 spin_lock_init(&ppd->cc_state_lock);
696 spin_lock_init(&ppd->cc_log_lock);
Jianxin Xiong8adf71f2016-07-25 13:39:14 -0700697 cc_state = kzalloc(sizeof(*cc_state), GFP_KERNEL);
698 RCU_INIT_POINTER(ppd->cc_state, cc_state);
699 if (!cc_state)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400700 goto bail;
701 return;
702
703bail:
704
705 hfi1_early_err(&pdev->dev,
706 "Congestion Control Agent disabled for port %d\n", port);
707}
708
709/*
710 * Do initialization for device that is only needed on
711 * first detect, not on resets.
712 */
713static int loadtime_init(struct hfi1_devdata *dd)
714{
715 return 0;
716}
717
718/**
719 * init_after_reset - re-initialize after a reset
720 * @dd: the hfi1_ib device
721 *
722 * sanity check at least some of the values after reset, and
723 * ensure no receive or transmit (explicitly, in case reset
724 * failed
725 */
726static int init_after_reset(struct hfi1_devdata *dd)
727{
728 int i;
Michael J. Ruhld295dbe2017-08-04 13:52:44 -0700729 struct hfi1_ctxtdata *rcd;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400730 /*
731 * Ensure chip does no sends or receives, tail updates, or
732 * pioavail updates while we re-initialize. This is mostly
733 * for the driver data structures, not chip registers.
734 */
Michael J. Ruhld295dbe2017-08-04 13:52:44 -0700735 for (i = 0; i < dd->num_rcv_contexts; i++) {
736 rcd = hfi1_rcd_get_by_index(dd, i);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400737 hfi1_rcvctrl(dd, HFI1_RCVCTRL_CTXT_DIS |
Michael J. Ruhl22505632017-07-24 07:46:06 -0700738 HFI1_RCVCTRL_INTRAVAIL_DIS |
Michael J. Ruhld295dbe2017-08-04 13:52:44 -0700739 HFI1_RCVCTRL_TAILUPD_DIS, rcd);
740 hfi1_rcd_put(rcd);
741 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400742 pio_send_control(dd, PSC_GLOBAL_DISABLE);
743 for (i = 0; i < dd->num_send_contexts; i++)
744 sc_disable(dd->send_contexts[i].sc);
745
746 return 0;
747}
748
749static void enable_chip(struct hfi1_devdata *dd)
750{
Michael J. Ruhld295dbe2017-08-04 13:52:44 -0700751 struct hfi1_ctxtdata *rcd;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400752 u32 rcvmask;
Michael J. Ruhle6f76222017-07-24 07:45:55 -0700753 u16 i;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400754
755 /* enable PIO send */
756 pio_send_control(dd, PSC_GLOBAL_ENABLE);
757
758 /*
759 * Enable kernel ctxts' receive and receive interrupt.
760 * Other ctxts done as user opens and initializes them.
761 */
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700762 for (i = 0; i < dd->first_dyn_alloc_ctxt; ++i) {
Michael J. Ruhld295dbe2017-08-04 13:52:44 -0700763 rcd = hfi1_rcd_get_by_index(dd, i);
764 if (!rcd)
765 continue;
Mitko Haralanov566c1572016-02-03 14:32:49 -0800766 rcvmask = HFI1_RCVCTRL_CTXT_ENB | HFI1_RCVCTRL_INTRAVAIL_ENB;
Michael J. Ruhld295dbe2017-08-04 13:52:44 -0700767 rcvmask |= HFI1_CAP_KGET_MASK(rcd->flags, DMA_RTAIL) ?
Mike Marciniszyn77241052015-07-30 15:17:43 -0400768 HFI1_RCVCTRL_TAILUPD_ENB : HFI1_RCVCTRL_TAILUPD_DIS;
Michael J. Ruhld295dbe2017-08-04 13:52:44 -0700769 if (!HFI1_CAP_KGET_MASK(rcd->flags, MULTI_PKT_EGR))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400770 rcvmask |= HFI1_RCVCTRL_ONE_PKT_EGR_ENB;
Michael J. Ruhld295dbe2017-08-04 13:52:44 -0700771 if (HFI1_CAP_KGET_MASK(rcd->flags, NODROP_RHQ_FULL))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400772 rcvmask |= HFI1_RCVCTRL_NO_RHQ_DROP_ENB;
Michael J. Ruhld295dbe2017-08-04 13:52:44 -0700773 if (HFI1_CAP_KGET_MASK(rcd->flags, NODROP_EGR_FULL))
Mike Marciniszyn77241052015-07-30 15:17:43 -0400774 rcvmask |= HFI1_RCVCTRL_NO_EGR_DROP_ENB;
Michael J. Ruhld295dbe2017-08-04 13:52:44 -0700775 hfi1_rcvctrl(dd, rcvmask, rcd);
776 sc_enable(rcd->sc);
777 hfi1_rcd_put(rcd);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400778 }
779}
780
781/**
782 * create_workqueues - create per port workqueues
783 * @dd: the hfi1_ib device
784 */
785static int create_workqueues(struct hfi1_devdata *dd)
786{
787 int pidx;
788 struct hfi1_pportdata *ppd;
789
790 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
791 ppd = dd->pport + pidx;
792 if (!ppd->hfi1_wq) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400793 ppd->hfi1_wq =
Mike Marciniszyn0a226ed2015-11-09 19:13:58 -0500794 alloc_workqueue(
795 "hfi%d_%d",
796 WQ_SYSFS | WQ_HIGHPRI | WQ_CPU_INTENSIVE,
Mike Marciniszyndd1ed102017-05-04 05:14:10 -0700797 HFI1_MAX_ACTIVE_WORKQUEUE_ENTRIES,
Mike Marciniszyn0a226ed2015-11-09 19:13:58 -0500798 dd->unit, pidx);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400799 if (!ppd->hfi1_wq)
800 goto wq_error;
801 }
Sebastian Sanchez71d47002017-07-29 08:43:49 -0700802 if (!ppd->link_wq) {
803 /*
804 * Make the link workqueue single-threaded to enforce
805 * serialization.
806 */
807 ppd->link_wq =
808 alloc_workqueue(
809 "hfi_link_%d_%d",
810 WQ_SYSFS | WQ_MEM_RECLAIM | WQ_UNBOUND,
811 1, /* max_active */
812 dd->unit, pidx);
813 if (!ppd->link_wq)
814 goto wq_error;
815 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400816 }
817 return 0;
818wq_error:
Mike Marciniszyn0a226ed2015-11-09 19:13:58 -0500819 pr_err("alloc_workqueue failed for port %d\n", pidx + 1);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400820 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
821 ppd = dd->pport + pidx;
822 if (ppd->hfi1_wq) {
823 destroy_workqueue(ppd->hfi1_wq);
824 ppd->hfi1_wq = NULL;
825 }
Sebastian Sanchez71d47002017-07-29 08:43:49 -0700826 if (ppd->link_wq) {
827 destroy_workqueue(ppd->link_wq);
828 ppd->link_wq = NULL;
829 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400830 }
831 return -ENOMEM;
832}
833
834/**
835 * hfi1_init - do the actual initialization sequence on the chip
836 * @dd: the hfi1_ib device
837 * @reinit: re-initializing, so don't allocate new memory
838 *
839 * Do the actual initialization sequence on the chip. This is done
840 * both from the init routine called from the PCI infrastructure, and
841 * when we reset the chip, or detect that it was reset internally,
842 * or it's administratively re-enabled.
843 *
844 * Memory allocation here and in called routines is only done in
845 * the first case (reinit == 0). We have to be careful, because even
846 * without memory allocation, we need to re-write all the chip registers
847 * TIDs, etc. after the reset or enable has completed.
848 */
849int hfi1_init(struct hfi1_devdata *dd, int reinit)
850{
851 int ret = 0, pidx, lastfail = 0;
Michael J. Ruhle6f76222017-07-24 07:45:55 -0700852 unsigned long len;
853 u16 i;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400854 struct hfi1_ctxtdata *rcd;
855 struct hfi1_pportdata *ppd;
856
857 /* Set up recv low level handlers */
858 dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_EXPECTED] =
859 kdeth_process_expected;
860 dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_EAGER] =
861 kdeth_process_eager;
862 dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_IB] = process_receive_ib;
863 dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_ERROR] =
864 process_receive_error;
865 dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_BYPASS] =
866 process_receive_bypass;
867 dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_INVALID5] =
868 process_receive_invalid;
869 dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_INVALID6] =
870 process_receive_invalid;
871 dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_INVALID7] =
872 process_receive_invalid;
873 dd->rhf_rcv_function_map = dd->normal_rhf_rcv_functions;
874
875 /* Set up send low level handlers */
876 dd->process_pio_send = hfi1_verbs_send_pio;
877 dd->process_dma_send = hfi1_verbs_send_dma;
878 dd->pio_inline_send = pio_copy;
Vishwanathapura, Niranjana64551ed2017-04-12 20:29:30 -0700879 dd->process_vnic_dma_send = hfi1_vnic_send_dma;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400880
Mike Marciniszyn995deaf2015-11-16 21:59:29 -0500881 if (is_ax(dd)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400882 atomic_set(&dd->drop_packet, DROP_PACKET_ON);
883 dd->do_drop = 1;
884 } else {
885 atomic_set(&dd->drop_packet, DROP_PACKET_OFF);
886 dd->do_drop = 0;
887 }
888
889 /* make sure the link is not "up" */
890 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
891 ppd = dd->pport + pidx;
892 ppd->linkup = 0;
893 }
894
895 if (reinit)
896 ret = init_after_reset(dd);
897 else
898 ret = loadtime_init(dd);
899 if (ret)
900 goto done;
901
Mark F. Brown46b010d2015-11-09 19:18:20 -0500902 /* allocate dummy tail memory for all receive contexts */
903 dd->rcvhdrtail_dummy_kvaddr = dma_zalloc_coherent(
904 &dd->pcidev->dev, sizeof(u64),
Tymoteusz Kielan60368182016-09-06 04:35:54 -0700905 &dd->rcvhdrtail_dummy_dma,
Mark F. Brown46b010d2015-11-09 19:18:20 -0500906 GFP_KERNEL);
907
908 if (!dd->rcvhdrtail_dummy_kvaddr) {
909 dd_dev_err(dd, "cannot allocate dummy tail memory\n");
910 ret = -ENOMEM;
911 goto done;
912 }
913
Mike Marciniszyn77241052015-07-30 15:17:43 -0400914 /* dd->rcd can be NULL if early initialization failed */
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700915 for (i = 0; dd->rcd && i < dd->first_dyn_alloc_ctxt; ++i) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400916 /*
917 * Set up the (kernel) rcvhdr queue and egr TIDs. If doing
918 * re-init, the simplest way to handle this is to free
919 * existing, and re-allocate.
920 * Need to re-create rest of ctxt 0 ctxtdata as well.
921 */
Michael J. Ruhld295dbe2017-08-04 13:52:44 -0700922 rcd = hfi1_rcd_get_by_index(dd, i);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400923 if (!rcd)
924 continue;
925
926 rcd->do_interrupt = &handle_receive_interrupt;
927
928 lastfail = hfi1_create_rcvhdrq(dd, rcd);
929 if (!lastfail)
930 lastfail = hfi1_setup_eagerbufs(rcd);
Ashutosh Dixit39239792016-05-12 10:24:00 -0700931 if (lastfail) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400932 dd_dev_err(dd,
Jubin John17fb4f22016-02-14 20:21:52 -0800933 "failed to allocate kernel ctxt's rcvhdrq and/or egr bufs\n");
Ashutosh Dixit39239792016-05-12 10:24:00 -0700934 ret = lastfail;
935 }
Michael J. Ruhld295dbe2017-08-04 13:52:44 -0700936 hfi1_rcd_put(rcd);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400937 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400938
939 /* Allocate enough memory for user event notification. */
Amitoj Kaur Chawla84449912016-03-04 22:30:43 +0530940 len = PAGE_ALIGN(dd->chip_rcv_contexts * HFI1_MAX_SHARED_CTXTS *
941 sizeof(*dd->events));
Mike Marciniszyn77241052015-07-30 15:17:43 -0400942 dd->events = vmalloc_user(len);
943 if (!dd->events)
944 dd_dev_err(dd, "Failed to allocate user events page\n");
945 /*
946 * Allocate a page for device and port status.
947 * Page will be shared amongst all user processes.
948 */
949 dd->status = vmalloc_user(PAGE_SIZE);
950 if (!dd->status)
951 dd_dev_err(dd, "Failed to allocate dev status page\n");
952 else
953 dd->freezelen = PAGE_SIZE - (sizeof(*dd->status) -
954 sizeof(dd->status->freezemsg));
955 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
956 ppd = dd->pport + pidx;
957 if (dd->status)
958 /* Currently, we only have one port */
959 ppd->statusp = &dd->status->port;
960
961 set_mtu(ppd);
962 }
963
964 /* enable chip even if we have an error, so we can debug cause */
965 enable_chip(dd);
966
Mike Marciniszyn77241052015-07-30 15:17:43 -0400967done:
968 /*
969 * Set status even if port serdes is not initialized
970 * so that diags will work.
971 */
972 if (dd->status)
973 dd->status->dev |= HFI1_STATUS_CHIP_PRESENT |
974 HFI1_STATUS_INITTED;
975 if (!ret) {
976 /* enable all interrupts from the chip */
977 set_intr_state(dd, 1);
978
979 /* chip is OK for user apps; mark it as initialized */
980 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
981 ppd = dd->pport + pidx;
982
Jubin John4d114fd2016-02-14 20:21:43 -0800983 /*
984 * start the serdes - must be after interrupts are
985 * enabled so we are notified when the link goes up
Mike Marciniszyn77241052015-07-30 15:17:43 -0400986 */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400987 lastfail = bringup_serdes(ppd);
988 if (lastfail)
989 dd_dev_info(dd,
Jubin John17fb4f22016-02-14 20:21:52 -0800990 "Failed to bring up port %u\n",
991 ppd->port);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400992
993 /*
994 * Set status even if port serdes is not initialized
995 * so that diags will work.
996 */
997 if (ppd->statusp)
998 *ppd->statusp |= HFI1_STATUS_CHIP_PRESENT |
999 HFI1_STATUS_INITTED;
1000 if (!ppd->link_speed_enabled)
1001 continue;
1002 }
1003 }
1004
1005 /* if ret is non-zero, we probably should do some cleanup here... */
1006 return ret;
1007}
1008
1009static inline struct hfi1_devdata *__hfi1_lookup(int unit)
1010{
1011 return idr_find(&hfi1_unit_table, unit);
1012}
1013
1014struct hfi1_devdata *hfi1_lookup(int unit)
1015{
1016 struct hfi1_devdata *dd;
1017 unsigned long flags;
1018
1019 spin_lock_irqsave(&hfi1_devs_lock, flags);
1020 dd = __hfi1_lookup(unit);
1021 spin_unlock_irqrestore(&hfi1_devs_lock, flags);
1022
1023 return dd;
1024}
1025
1026/*
1027 * Stop the timers during unit shutdown, or after an error late
1028 * in initialization.
1029 */
1030static void stop_timers(struct hfi1_devdata *dd)
1031{
1032 struct hfi1_pportdata *ppd;
1033 int pidx;
1034
1035 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
1036 ppd = dd->pport + pidx;
Kees Cook80641352017-10-16 15:51:54 -07001037 if (ppd->led_override_timer.function) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001038 del_timer_sync(&ppd->led_override_timer);
1039 atomic_set(&ppd->led_override_timer_active, 0);
1040 }
1041 }
1042}
1043
1044/**
1045 * shutdown_device - shut down a device
1046 * @dd: the hfi1_ib device
1047 *
1048 * This is called to make the device quiet when we are about to
1049 * unload the driver, and also when the device is administratively
1050 * disabled. It does not free any data structures.
1051 * Everything it does has to be setup again by hfi1_init(dd, 1)
1052 */
1053static void shutdown_device(struct hfi1_devdata *dd)
1054{
1055 struct hfi1_pportdata *ppd;
Michael J. Ruhld295dbe2017-08-04 13:52:44 -07001056 struct hfi1_ctxtdata *rcd;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001057 unsigned pidx;
1058 int i;
1059
1060 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
1061 ppd = dd->pport + pidx;
1062
1063 ppd->linkup = 0;
1064 if (ppd->statusp)
1065 *ppd->statusp &= ~(HFI1_STATUS_IB_CONF |
1066 HFI1_STATUS_IB_READY);
1067 }
1068 dd->flags &= ~HFI1_INITTED;
1069
Michael J. Ruhl82a97922018-02-01 10:43:42 -08001070 /* mask and clean up interrupts, but not errors */
Mike Marciniszyn77241052015-07-30 15:17:43 -04001071 set_intr_state(dd, 0);
Michael J. Ruhl82a97922018-02-01 10:43:42 -08001072 hfi1_clean_up_interrupts(dd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001073
1074 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
1075 ppd = dd->pport + pidx;
Michael J. Ruhld295dbe2017-08-04 13:52:44 -07001076 for (i = 0; i < dd->num_rcv_contexts; i++) {
1077 rcd = hfi1_rcd_get_by_index(dd, i);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001078 hfi1_rcvctrl(dd, HFI1_RCVCTRL_TAILUPD_DIS |
Michael J. Ruhl22505632017-07-24 07:46:06 -07001079 HFI1_RCVCTRL_CTXT_DIS |
1080 HFI1_RCVCTRL_INTRAVAIL_DIS |
1081 HFI1_RCVCTRL_PKEY_DIS |
Michael J. Ruhld295dbe2017-08-04 13:52:44 -07001082 HFI1_RCVCTRL_ONE_PKT_EGR_DIS, rcd);
1083 hfi1_rcd_put(rcd);
1084 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001085 /*
1086 * Gracefully stop all sends allowing any in progress to
1087 * trickle out first.
1088 */
1089 for (i = 0; i < dd->num_send_contexts; i++)
1090 sc_flush(dd->send_contexts[i].sc);
1091 }
1092
1093 /*
1094 * Enough for anything that's going to trickle out to have actually
1095 * done so.
1096 */
1097 udelay(20);
1098
1099 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
1100 ppd = dd->pport + pidx;
1101
1102 /* disable all contexts */
1103 for (i = 0; i < dd->num_send_contexts; i++)
1104 sc_disable(dd->send_contexts[i].sc);
1105 /* disable the send device */
1106 pio_send_control(dd, PSC_GLOBAL_DISABLE);
1107
Easwar Hariharan91ab4ed2016-02-03 14:35:57 -08001108 shutdown_led_override(ppd);
1109
Mike Marciniszyn77241052015-07-30 15:17:43 -04001110 /*
1111 * Clear SerdesEnable.
1112 * We can't count on interrupts since we are stopping.
1113 */
1114 hfi1_quiet_serdes(ppd);
1115
1116 if (ppd->hfi1_wq) {
1117 destroy_workqueue(ppd->hfi1_wq);
1118 ppd->hfi1_wq = NULL;
1119 }
Sebastian Sanchez71d47002017-07-29 08:43:49 -07001120 if (ppd->link_wq) {
1121 destroy_workqueue(ppd->link_wq);
1122 ppd->link_wq = NULL;
1123 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001124 }
1125 sdma_exit(dd);
1126}
1127
1128/**
1129 * hfi1_free_ctxtdata - free a context's allocated data
1130 * @dd: the hfi1_ib device
1131 * @rcd: the ctxtdata structure
1132 *
1133 * free up any allocated data for a context
Mike Marciniszyn77241052015-07-30 15:17:43 -04001134 * It should never change any chip state, or global driver state.
1135 */
1136void hfi1_free_ctxtdata(struct hfi1_devdata *dd, struct hfi1_ctxtdata *rcd)
1137{
Michael J. Ruhlf683c802017-06-09 16:00:19 -07001138 u32 e;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001139
1140 if (!rcd)
1141 return;
1142
1143 if (rcd->rcvhdrq) {
1144 dma_free_coherent(&dd->pcidev->dev, rcd->rcvhdrq_size,
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001145 rcd->rcvhdrq, rcd->rcvhdrq_dma);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001146 rcd->rcvhdrq = NULL;
1147 if (rcd->rcvhdrtail_kvaddr) {
1148 dma_free_coherent(&dd->pcidev->dev, PAGE_SIZE,
1149 (void *)rcd->rcvhdrtail_kvaddr,
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001150 rcd->rcvhdrqtailaddr_dma);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001151 rcd->rcvhdrtail_kvaddr = NULL;
1152 }
1153 }
1154
1155 /* all the RcvArray entries should have been cleared by now */
1156 kfree(rcd->egrbufs.rcvtids);
Michael J. Ruhlf683c802017-06-09 16:00:19 -07001157 rcd->egrbufs.rcvtids = NULL;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001158
1159 for (e = 0; e < rcd->egrbufs.alloced; e++) {
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001160 if (rcd->egrbufs.buffers[e].dma)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001161 dma_free_coherent(&dd->pcidev->dev,
1162 rcd->egrbufs.buffers[e].len,
1163 rcd->egrbufs.buffers[e].addr,
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001164 rcd->egrbufs.buffers[e].dma);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001165 }
1166 kfree(rcd->egrbufs.buffers);
Michael J. Ruhlf683c802017-06-09 16:00:19 -07001167 rcd->egrbufs.alloced = 0;
1168 rcd->egrbufs.buffers = NULL;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001169
1170 sc_free(rcd->sc);
Michael J. Ruhlf683c802017-06-09 16:00:19 -07001171 rcd->sc = NULL;
1172
Mike Marciniszyn77241052015-07-30 15:17:43 -04001173 vfree(rcd->subctxt_uregbase);
1174 vfree(rcd->subctxt_rcvegrbuf);
1175 vfree(rcd->subctxt_rcvhdr_base);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001176 kfree(rcd->opstats);
Michael J. Ruhlf683c802017-06-09 16:00:19 -07001177
1178 rcd->subctxt_uregbase = NULL;
1179 rcd->subctxt_rcvegrbuf = NULL;
1180 rcd->subctxt_rcvhdr_base = NULL;
1181 rcd->opstats = NULL;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001182}
1183
Dean Luick78eb1292016-03-05 08:49:45 -08001184/*
1185 * Release our hold on the shared asic data. If we are the last one,
Dean Luickdba715f2016-07-06 17:28:52 -04001186 * return the structure to be finalized outside the lock. Must be
1187 * holding hfi1_devs_lock.
Dean Luick78eb1292016-03-05 08:49:45 -08001188 */
Dean Luickdba715f2016-07-06 17:28:52 -04001189static struct hfi1_asic_data *release_asic_data(struct hfi1_devdata *dd)
Dean Luick78eb1292016-03-05 08:49:45 -08001190{
Dean Luickdba715f2016-07-06 17:28:52 -04001191 struct hfi1_asic_data *ad;
Dean Luick78eb1292016-03-05 08:49:45 -08001192 int other;
1193
1194 if (!dd->asic_data)
Dean Luickdba715f2016-07-06 17:28:52 -04001195 return NULL;
Dean Luick78eb1292016-03-05 08:49:45 -08001196 dd->asic_data->dds[dd->hfi1_id] = NULL;
1197 other = dd->hfi1_id ? 0 : 1;
Dean Luickdba715f2016-07-06 17:28:52 -04001198 ad = dd->asic_data;
Dean Luick78eb1292016-03-05 08:49:45 -08001199 dd->asic_data = NULL;
Dean Luickdba715f2016-07-06 17:28:52 -04001200 /* return NULL if the other dd still has a link */
1201 return ad->dds[other] ? NULL : ad;
1202}
1203
1204static void finalize_asic_data(struct hfi1_devdata *dd,
1205 struct hfi1_asic_data *ad)
1206{
1207 clean_up_i2c(dd, ad);
1208 kfree(ad);
Dean Luick78eb1292016-03-05 08:49:45 -08001209}
1210
Dennis Dalessandroe11ffbd2016-05-19 05:26:44 -07001211static void __hfi1_free_devdata(struct kobject *kobj)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001212{
Dennis Dalessandroe11ffbd2016-05-19 05:26:44 -07001213 struct hfi1_devdata *dd =
1214 container_of(kobj, struct hfi1_devdata, kobj);
Dean Luickdba715f2016-07-06 17:28:52 -04001215 struct hfi1_asic_data *ad;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001216 unsigned long flags;
1217
1218 spin_lock_irqsave(&hfi1_devs_lock, flags);
1219 idr_remove(&hfi1_unit_table, dd->unit);
1220 list_del(&dd->list);
Dean Luickdba715f2016-07-06 17:28:52 -04001221 ad = release_asic_data(dd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001222 spin_unlock_irqrestore(&hfi1_devs_lock, flags);
Dean Luickdba715f2016-07-06 17:28:52 -04001223 if (ad)
1224 finalize_asic_data(dd, ad);
Easwar Hariharanc3838b32016-02-09 14:29:13 -08001225 free_platform_config(dd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001226 rcu_barrier(); /* wait for rcu callbacks to complete */
1227 free_percpu(dd->int_counter);
1228 free_percpu(dd->rcv_limit);
Vennila Megavannan89abfc82016-02-03 14:34:07 -08001229 free_percpu(dd->send_schedule);
Mike Marciniszyn1b311f82017-10-23 06:06:08 -07001230 free_percpu(dd->tx_opstats);
Alex Estrin473291b2018-02-01 10:43:50 -08001231 sdma_clean(dd, dd->num_sdma);
Jubin Johnea0e4ce2016-04-20 06:05:24 -07001232 rvt_dealloc_device(&dd->verbs_dev.rdi);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001233}
1234
Dennis Dalessandroe11ffbd2016-05-19 05:26:44 -07001235static struct kobj_type hfi1_devdata_type = {
1236 .release = __hfi1_free_devdata,
1237};
1238
1239void hfi1_free_devdata(struct hfi1_devdata *dd)
1240{
1241 kobject_put(&dd->kobj);
1242}
1243
Mike Marciniszyn77241052015-07-30 15:17:43 -04001244/*
1245 * Allocate our primary per-unit data structure. Must be done via verbs
1246 * allocator, because the verbs cleanup process both does cleanup and
1247 * free of the data structure.
1248 * "extra" is for chip-specific data.
1249 *
1250 * Use the idr mechanism to get a unit number for this unit.
1251 */
1252struct hfi1_devdata *hfi1_alloc_devdata(struct pci_dev *pdev, size_t extra)
1253{
1254 unsigned long flags;
1255 struct hfi1_devdata *dd;
Dennis Dalessandro7af6d002016-01-19 14:44:06 -08001256 int ret, nports;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001257
Dennis Dalessandro7af6d002016-01-19 14:44:06 -08001258 /* extra is * number of ports */
1259 nports = extra / sizeof(struct hfi1_pportdata);
1260
1261 dd = (struct hfi1_devdata *)rvt_alloc_device(sizeof(*dd) + extra,
1262 nports);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001263 if (!dd)
1264 return ERR_PTR(-ENOMEM);
Dennis Dalessandro7af6d002016-01-19 14:44:06 -08001265 dd->num_pports = nports;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001266 dd->pport = (struct hfi1_pportdata *)(dd + 1);
1267
1268 INIT_LIST_HEAD(&dd->list);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001269 idr_preload(GFP_KERNEL);
1270 spin_lock_irqsave(&hfi1_devs_lock, flags);
1271
1272 ret = idr_alloc(&hfi1_unit_table, dd, 0, 0, GFP_NOWAIT);
1273 if (ret >= 0) {
1274 dd->unit = ret;
1275 list_add(&dd->list, &hfi1_dev_list);
1276 }
1277
1278 spin_unlock_irqrestore(&hfi1_devs_lock, flags);
1279 idr_preload_end();
1280
1281 if (ret < 0) {
1282 hfi1_early_err(&pdev->dev,
1283 "Could not allocate unit ID: error %d\n", -ret);
1284 goto bail;
1285 }
Michael J. Ruhl5084c8f2017-12-18 19:56:37 -08001286 rvt_set_ibdev_name(&dd->verbs_dev.rdi, "%s_%d", class_name(), dd->unit);
1287
Mike Marciniszyn77241052015-07-30 15:17:43 -04001288 /*
1289 * Initialize all locks for the device. This needs to be as early as
1290 * possible so locks are usable.
1291 */
1292 spin_lock_init(&dd->sc_lock);
1293 spin_lock_init(&dd->sendctrl_lock);
1294 spin_lock_init(&dd->rcvctrl_lock);
1295 spin_lock_init(&dd->uctxt_lock);
1296 spin_lock_init(&dd->hfi1_diag_trans_lock);
1297 spin_lock_init(&dd->sc_init_lock);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001298 spin_lock_init(&dd->dc8051_memlock);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001299 seqlock_init(&dd->sc2vl_lock);
1300 spin_lock_init(&dd->sde_map_lock);
Jubin John35f6bef2016-02-14 12:46:10 -08001301 spin_lock_init(&dd->pio_map_lock);
Tadeusz Struk22546b72017-04-28 10:40:02 -07001302 mutex_init(&dd->dc8051_lock);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001303 init_waitqueue_head(&dd->event_queue);
1304
1305 dd->int_counter = alloc_percpu(u64);
1306 if (!dd->int_counter) {
1307 ret = -ENOMEM;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001308 goto bail;
1309 }
1310
1311 dd->rcv_limit = alloc_percpu(u64);
1312 if (!dd->rcv_limit) {
1313 ret = -ENOMEM;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001314 goto bail;
1315 }
1316
Vennila Megavannan89abfc82016-02-03 14:34:07 -08001317 dd->send_schedule = alloc_percpu(u64);
1318 if (!dd->send_schedule) {
1319 ret = -ENOMEM;
Vennila Megavannan89abfc82016-02-03 14:34:07 -08001320 goto bail;
1321 }
1322
Mike Marciniszyn1b311f82017-10-23 06:06:08 -07001323 dd->tx_opstats = alloc_percpu(struct hfi1_opcode_stats_perctx);
1324 if (!dd->tx_opstats) {
1325 ret = -ENOMEM;
1326 goto bail;
1327 }
1328
Dennis Dalessandroe11ffbd2016-05-19 05:26:44 -07001329 kobject_init(&dd->kobj, &hfi1_devdata_type);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001330 return dd;
1331
1332bail:
1333 if (!list_empty(&dd->list))
1334 list_del_init(&dd->list);
Jubin Johnea0e4ce2016-04-20 06:05:24 -07001335 rvt_dealloc_device(&dd->verbs_dev.rdi);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001336 return ERR_PTR(ret);
1337}
1338
1339/*
1340 * Called from freeze mode handlers, and from PCI error
1341 * reporting code. Should be paranoid about state of
1342 * system and data structures.
1343 */
1344void hfi1_disable_after_error(struct hfi1_devdata *dd)
1345{
1346 if (dd->flags & HFI1_INITTED) {
1347 u32 pidx;
1348
1349 dd->flags &= ~HFI1_INITTED;
1350 if (dd->pport)
1351 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
1352 struct hfi1_pportdata *ppd;
1353
1354 ppd = dd->pport + pidx;
1355 if (dd->flags & HFI1_PRESENT)
1356 set_link_state(ppd, HLS_DN_DISABLE);
1357
1358 if (ppd->statusp)
1359 *ppd->statusp &= ~HFI1_STATUS_IB_READY;
1360 }
1361 }
1362
1363 /*
1364 * Mark as having had an error for driver, and also
1365 * for /sys and status word mapped to user programs.
1366 * This marks unit as not usable, until reset.
1367 */
1368 if (dd->status)
1369 dd->status->dev |= HFI1_STATUS_HWERROR;
1370}
1371
1372static void remove_one(struct pci_dev *);
1373static int init_one(struct pci_dev *, const struct pci_device_id *);
1374
1375#define DRIVER_LOAD_MSG "Intel " DRIVER_NAME " loaded: "
1376#define PFX DRIVER_NAME ": "
1377
Sebastian Sanchezd6373012016-07-25 07:54:48 -07001378const struct pci_device_id hfi1_pci_tbl[] = {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001379 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL0) },
1380 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL1) },
1381 { 0, }
1382};
1383
1384MODULE_DEVICE_TABLE(pci, hfi1_pci_tbl);
1385
1386static struct pci_driver hfi1_pci_driver = {
1387 .name = DRIVER_NAME,
1388 .probe = init_one,
1389 .remove = remove_one,
1390 .id_table = hfi1_pci_tbl,
1391 .err_handler = &hfi1_pci_err_handler,
1392};
1393
1394static void __init compute_krcvqs(void)
1395{
1396 int i;
1397
1398 for (i = 0; i < krcvqsset; i++)
1399 n_krcvqs += krcvqs[i];
1400}
1401
1402/*
1403 * Do all the generic driver unit- and chip-independent memory
1404 * allocation and initialization.
1405 */
1406static int __init hfi1_mod_init(void)
1407{
1408 int ret;
1409
1410 ret = dev_init();
1411 if (ret)
1412 goto bail;
1413
Sebastian Sanchezd6373012016-07-25 07:54:48 -07001414 ret = node_affinity_init();
1415 if (ret)
1416 goto bail;
Dennis Dalessandro41973442016-07-25 07:52:36 -07001417
Mike Marciniszyn77241052015-07-30 15:17:43 -04001418 /* validate max MTU before any devices start */
1419 if (!valid_opa_max_mtu(hfi1_max_mtu)) {
1420 pr_err("Invalid max_mtu 0x%x, using 0x%x instead\n",
1421 hfi1_max_mtu, HFI1_DEFAULT_MAX_MTU);
1422 hfi1_max_mtu = HFI1_DEFAULT_MAX_MTU;
1423 }
1424 /* valid CUs run from 1-128 in powers of 2 */
1425 if (hfi1_cu > 128 || !is_power_of_2(hfi1_cu))
1426 hfi1_cu = 1;
1427 /* valid credit return threshold is 0-100, variable is unsigned */
1428 if (user_credit_return_threshold > 100)
1429 user_credit_return_threshold = 100;
1430
1431 compute_krcvqs();
Jubin John4d114fd2016-02-14 20:21:43 -08001432 /*
1433 * sanitize receive interrupt count, time must wait until after
1434 * the hardware type is known
1435 */
Mike Marciniszyn77241052015-07-30 15:17:43 -04001436 if (rcv_intr_count > RCV_HDR_HEAD_COUNTER_MASK)
1437 rcv_intr_count = RCV_HDR_HEAD_COUNTER_MASK;
1438 /* reject invalid combinations */
1439 if (rcv_intr_count == 0 && rcv_intr_timeout == 0) {
1440 pr_err("Invalid mode: both receive interrupt count and available timeout are zero - setting interrupt count to 1\n");
1441 rcv_intr_count = 1;
1442 }
1443 if (rcv_intr_count > 1 && rcv_intr_timeout == 0) {
1444 /*
1445 * Avoid indefinite packet delivery by requiring a timeout
1446 * if count is > 1.
1447 */
1448 pr_err("Invalid mode: receive interrupt count greater than 1 and available timeout is zero - setting available timeout to 1\n");
1449 rcv_intr_timeout = 1;
1450 }
1451 if (rcv_intr_dynamic && !(rcv_intr_count > 1 && rcv_intr_timeout > 0)) {
1452 /*
1453 * The dynamic algorithm expects a non-zero timeout
1454 * and a count > 1.
1455 */
1456 pr_err("Invalid mode: dynamic receive interrupt mitigation with invalid count and timeout - turning dynamic off\n");
1457 rcv_intr_dynamic = 0;
1458 }
1459
1460 /* sanitize link CRC options */
1461 link_crc_mask &= SUPPORTED_CRCS;
1462
1463 /*
1464 * These must be called before the driver is registered with
1465 * the PCI subsystem.
1466 */
1467 idr_init(&hfi1_unit_table);
1468
1469 hfi1_dbg_init();
Dean Luick528ee9f2016-03-05 08:50:43 -08001470 ret = hfi1_wss_init();
1471 if (ret < 0)
1472 goto bail_wss;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001473 ret = pci_register_driver(&hfi1_pci_driver);
1474 if (ret < 0) {
1475 pr_err("Unable to register driver: error %d\n", -ret);
1476 goto bail_dev;
1477 }
1478 goto bail; /* all OK */
1479
1480bail_dev:
Dean Luick528ee9f2016-03-05 08:50:43 -08001481 hfi1_wss_exit();
1482bail_wss:
Mike Marciniszyn77241052015-07-30 15:17:43 -04001483 hfi1_dbg_exit();
1484 idr_destroy(&hfi1_unit_table);
1485 dev_cleanup();
1486bail:
1487 return ret;
1488}
1489
1490module_init(hfi1_mod_init);
1491
1492/*
1493 * Do the non-unit driver cleanup, memory free, etc. at unload.
1494 */
1495static void __exit hfi1_mod_cleanup(void)
1496{
1497 pci_unregister_driver(&hfi1_pci_driver);
Dennis Dalessandro41973442016-07-25 07:52:36 -07001498 node_affinity_destroy();
Dean Luick528ee9f2016-03-05 08:50:43 -08001499 hfi1_wss_exit();
Mike Marciniszyn77241052015-07-30 15:17:43 -04001500 hfi1_dbg_exit();
Mike Marciniszyn77241052015-07-30 15:17:43 -04001501
1502 idr_destroy(&hfi1_unit_table);
1503 dispose_firmware(); /* asymmetric with obtain_firmware() */
1504 dev_cleanup();
1505}
1506
1507module_exit(hfi1_mod_cleanup);
1508
1509/* this can only be called after a successful initialization */
1510static void cleanup_device_data(struct hfi1_devdata *dd)
1511{
1512 int ctxt;
1513 int pidx;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001514
1515 /* users can't do anything more with chip */
1516 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
1517 struct hfi1_pportdata *ppd = &dd->pport[pidx];
1518 struct cc_state *cc_state;
1519 int i;
1520
1521 if (ppd->statusp)
1522 *ppd->statusp &= ~HFI1_STATUS_CHIP_PRESENT;
1523
1524 for (i = 0; i < OPA_MAX_SLS; i++)
1525 hrtimer_cancel(&ppd->cca_timer[i].hrtimer);
1526
1527 spin_lock(&ppd->cc_state_lock);
Jianxin Xiong8adf71f2016-07-25 13:39:14 -07001528 cc_state = get_cc_state_protected(ppd);
Muhammad Falak R Wanieea57072016-05-01 18:05:31 +05301529 RCU_INIT_POINTER(ppd->cc_state, NULL);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001530 spin_unlock(&ppd->cc_state_lock);
1531
1532 if (cc_state)
Wei Yongjun476d95b2016-08-10 03:14:04 +00001533 kfree_rcu(cc_state, rcu);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001534 }
1535
1536 free_credit_return(dd);
1537
Mark F. Brown46b010d2015-11-09 19:18:20 -05001538 if (dd->rcvhdrtail_dummy_kvaddr) {
1539 dma_free_coherent(&dd->pcidev->dev, sizeof(u64),
1540 (void *)dd->rcvhdrtail_dummy_kvaddr,
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001541 dd->rcvhdrtail_dummy_dma);
Dan Carpentera8b7da52016-05-28 08:01:20 +03001542 dd->rcvhdrtail_dummy_kvaddr = NULL;
Mark F. Brown46b010d2015-11-09 19:18:20 -05001543 }
1544
Michael J. Ruhld295dbe2017-08-04 13:52:44 -07001545 /*
1546 * Free any resources still in use (usually just kernel contexts)
1547 * at unload; we do for ctxtcnt, because that's what we allocate.
1548 */
1549 for (ctxt = 0; dd->rcd && ctxt < dd->num_rcv_contexts; ctxt++) {
1550 struct hfi1_ctxtdata *rcd = dd->rcd[ctxt];
Mike Marciniszyn77241052015-07-30 15:17:43 -04001551
Mike Marciniszyn77241052015-07-30 15:17:43 -04001552 if (rcd) {
1553 hfi1_clear_tids(rcd);
Michael J. Ruhld295dbe2017-08-04 13:52:44 -07001554 hfi1_free_ctxt(rcd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001555 }
1556 }
Michael J. Ruhld295dbe2017-08-04 13:52:44 -07001557
1558 kfree(dd->rcd);
1559 dd->rcd = NULL;
1560
Jubin John35f6bef2016-02-14 12:46:10 -08001561 free_pio_map(dd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001562 /* must follow rcv context free - need to remove rcv's hooks */
1563 for (ctxt = 0; ctxt < dd->num_send_contexts; ctxt++)
1564 sc_free(dd->send_contexts[ctxt].sc);
1565 dd->num_send_contexts = 0;
1566 kfree(dd->send_contexts);
1567 dd->send_contexts = NULL;
Jubin John79d0c082016-02-26 13:33:33 -08001568 kfree(dd->hw_to_sw);
1569 dd->hw_to_sw = NULL;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001570 kfree(dd->boardname);
1571 vfree(dd->events);
1572 vfree(dd->status);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001573}
1574
1575/*
1576 * Clean up on unit shutdown, or error during unit load after
1577 * successful initialization.
1578 */
1579static void postinit_cleanup(struct hfi1_devdata *dd)
1580{
1581 hfi1_start_cleanup(dd);
1582
1583 hfi1_pcie_ddcleanup(dd);
1584 hfi1_pcie_cleanup(dd->pcidev);
1585
1586 cleanup_device_data(dd);
1587
1588 hfi1_free_devdata(dd);
1589}
1590
Krzysztof Blaszkowski11501ab2016-10-25 13:12:11 -07001591static int init_validate_rcvhdrcnt(struct device *dev, uint thecnt)
1592{
1593 if (thecnt <= HFI1_MIN_HDRQ_EGRBUF_CNT) {
1594 hfi1_early_err(dev, "Receive header queue count too small\n");
1595 return -EINVAL;
1596 }
1597
1598 if (thecnt > HFI1_MAX_HDRQ_EGRBUF_CNT) {
1599 hfi1_early_err(dev,
1600 "Receive header queue count cannot be greater than %u\n",
1601 HFI1_MAX_HDRQ_EGRBUF_CNT);
1602 return -EINVAL;
1603 }
1604
1605 if (thecnt % HDRQ_INCREMENT) {
1606 hfi1_early_err(dev, "Receive header queue count %d must be divisible by %lu\n",
1607 thecnt, HDRQ_INCREMENT);
1608 return -EINVAL;
1609 }
1610
1611 return 0;
1612}
1613
Mike Marciniszyn77241052015-07-30 15:17:43 -04001614static int init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
1615{
1616 int ret = 0, j, pidx, initfail;
Krzysztof Blaszkowski83fb4af2016-10-17 04:19:24 -07001617 struct hfi1_devdata *dd;
Harish Chegondie8597eb2015-12-01 15:38:20 -05001618 struct hfi1_pportdata *ppd;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001619
1620 /* First, lock the non-writable module parameters */
1621 HFI1_CAP_LOCK();
1622
Tadeusz Struk5d6f08a2017-03-20 17:25:29 -07001623 /* Validate dev ids */
1624 if (!(ent->device == PCI_DEVICE_ID_INTEL0 ||
1625 ent->device == PCI_DEVICE_ID_INTEL1)) {
1626 hfi1_early_err(&pdev->dev,
1627 "Failing on unknown Intel deviceid 0x%x\n",
1628 ent->device);
1629 ret = -ENODEV;
1630 goto bail;
1631 }
1632
Mike Marciniszyn77241052015-07-30 15:17:43 -04001633 /* Validate some global module parameters */
Krzysztof Blaszkowski11501ab2016-10-25 13:12:11 -07001634 ret = init_validate_rcvhdrcnt(&pdev->dev, rcvhdrcnt);
1635 if (ret)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001636 goto bail;
Krzysztof Blaszkowski11501ab2016-10-25 13:12:11 -07001637
Mike Marciniszyn77241052015-07-30 15:17:43 -04001638 /* use the encoding function as a sanitization check */
1639 if (!encode_rcv_header_entry_size(hfi1_hdrq_entsize)) {
1640 hfi1_early_err(&pdev->dev, "Invalid HdrQ Entry size %u\n",
1641 hfi1_hdrq_entsize);
Sebastian Sanchez07859de2015-12-10 16:02:49 -05001642 ret = -EINVAL;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001643 goto bail;
1644 }
1645
1646 /* The receive eager buffer size must be set before the receive
1647 * contexts are created.
1648 *
1649 * Set the eager buffer size. Validate that it falls in a range
1650 * allowed by the hardware - all powers of 2 between the min and
1651 * max. The maximum valid MTU is within the eager buffer range
1652 * so we do not need to cap the max_mtu by an eager buffer size
1653 * setting.
1654 */
1655 if (eager_buffer_size) {
1656 if (!is_power_of_2(eager_buffer_size))
1657 eager_buffer_size =
1658 roundup_pow_of_two(eager_buffer_size);
1659 eager_buffer_size =
1660 clamp_val(eager_buffer_size,
1661 MIN_EAGER_BUFFER * 8,
1662 MAX_EAGER_BUFFER_TOTAL);
1663 hfi1_early_info(&pdev->dev, "Eager buffer size %u\n",
1664 eager_buffer_size);
1665 } else {
1666 hfi1_early_err(&pdev->dev, "Invalid Eager buffer size of 0\n");
1667 ret = -EINVAL;
1668 goto bail;
1669 }
1670
1671 /* restrict value of hfi1_rcvarr_split */
1672 hfi1_rcvarr_split = clamp_val(hfi1_rcvarr_split, 0, 100);
1673
1674 ret = hfi1_pcie_init(pdev, ent);
1675 if (ret)
1676 goto bail;
1677
Krzysztof Blaszkowski83fb4af2016-10-17 04:19:24 -07001678 /*
1679 * Do device-specific initialization, function table setup, dd
1680 * allocation, etc.
1681 */
1682 dd = hfi1_init_dd(pdev, ent);
1683
1684 if (IS_ERR(dd)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001685 ret = PTR_ERR(dd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001686 goto clean_bail; /* error already printed */
Krzysztof Blaszkowski83fb4af2016-10-17 04:19:24 -07001687 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001688
1689 ret = create_workqueues(dd);
1690 if (ret)
1691 goto clean_bail;
1692
1693 /* do the generic initialization */
1694 initfail = hfi1_init(dd, 0);
1695
Vishwanathapura, Niranjanad4829ea2017-04-12 20:29:28 -07001696 /* setup vnic */
1697 hfi1_vnic_setup(dd);
1698
Mike Marciniszyn77241052015-07-30 15:17:43 -04001699 ret = hfi1_register_ib_device(dd);
1700
1701 /*
1702 * Now ready for use. this should be cleared whenever we
1703 * detect a reset, or initiate one. If earlier failure,
1704 * we still create devices, so diags, etc. can be used
1705 * to determine cause of problem.
1706 */
Dean Luicked6f6532016-02-18 11:12:25 -08001707 if (!initfail && !ret) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001708 dd->flags |= HFI1_INITTED;
Dean Luicked6f6532016-02-18 11:12:25 -08001709 /* create debufs files after init and ib register */
1710 hfi1_dbg_ibdev_init(&dd->verbs_dev);
1711 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001712
1713 j = hfi1_device_create(dd);
1714 if (j)
1715 dd_dev_err(dd, "Failed to create /dev devices: %d\n", -j);
1716
1717 if (initfail || ret) {
Michael J. Ruhl82a97922018-02-01 10:43:42 -08001718 hfi1_clean_up_interrupts(dd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001719 stop_timers(dd);
1720 flush_workqueue(ib_wq);
Harish Chegondie8597eb2015-12-01 15:38:20 -05001721 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001722 hfi1_quiet_serdes(dd->pport + pidx);
Harish Chegondie8597eb2015-12-01 15:38:20 -05001723 ppd = dd->pport + pidx;
1724 if (ppd->hfi1_wq) {
1725 destroy_workqueue(ppd->hfi1_wq);
1726 ppd->hfi1_wq = NULL;
1727 }
Sebastian Sanchez71d47002017-07-29 08:43:49 -07001728 if (ppd->link_wq) {
1729 destroy_workqueue(ppd->link_wq);
1730 ppd->link_wq = NULL;
1731 }
Harish Chegondie8597eb2015-12-01 15:38:20 -05001732 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001733 if (!j)
1734 hfi1_device_remove(dd);
1735 if (!ret)
1736 hfi1_unregister_ib_device(dd);
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -07001737 hfi1_vnic_cleanup(dd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001738 postinit_cleanup(dd);
1739 if (initfail)
1740 ret = initfail;
1741 goto bail; /* everything already cleaned */
1742 }
1743
1744 sdma_start(dd);
1745
1746 return 0;
1747
1748clean_bail:
1749 hfi1_pcie_cleanup(pdev);
1750bail:
1751 return ret;
1752}
1753
Tadeusz Strukacd7c8f2016-10-25 08:57:55 -07001754static void wait_for_clients(struct hfi1_devdata *dd)
1755{
1756 /*
1757 * Remove the device init value and complete the device if there is
1758 * no clients or wait for active clients to finish.
1759 */
1760 if (atomic_dec_and_test(&dd->user_refcount))
1761 complete(&dd->user_comp);
1762
1763 wait_for_completion(&dd->user_comp);
1764}
1765
Mike Marciniszyn77241052015-07-30 15:17:43 -04001766static void remove_one(struct pci_dev *pdev)
1767{
1768 struct hfi1_devdata *dd = pci_get_drvdata(pdev);
1769
Dean Luicked6f6532016-02-18 11:12:25 -08001770 /* close debugfs files before ib unregister */
1771 hfi1_dbg_ibdev_exit(&dd->verbs_dev);
Tadeusz Strukacd7c8f2016-10-25 08:57:55 -07001772
1773 /* remove the /dev hfi1 interface */
1774 hfi1_device_remove(dd);
1775
1776 /* wait for existing user space clients to finish */
1777 wait_for_clients(dd);
1778
Mike Marciniszyn77241052015-07-30 15:17:43 -04001779 /* unregister from IB core */
1780 hfi1_unregister_ib_device(dd);
1781
Vishwanathapura, Niranjanad4829ea2017-04-12 20:29:28 -07001782 /* cleanup vnic */
1783 hfi1_vnic_cleanup(dd);
1784
Mike Marciniszyn77241052015-07-30 15:17:43 -04001785 /*
1786 * Disable the IB link, disable interrupts on the device,
1787 * clear dma engines, etc.
1788 */
1789 shutdown_device(dd);
1790
1791 stop_timers(dd);
1792
1793 /* wait until all of our (qsfp) queue_work() calls complete */
1794 flush_workqueue(ib_wq);
1795
Mike Marciniszyn77241052015-07-30 15:17:43 -04001796 postinit_cleanup(dd);
1797}
1798
1799/**
1800 * hfi1_create_rcvhdrq - create a receive header queue
1801 * @dd: the hfi1_ib device
1802 * @rcd: the context data
1803 *
1804 * This must be contiguous memory (from an i/o perspective), and must be
1805 * DMA'able (which means for some systems, it will go through an IOMMU,
1806 * or be forced into a low address range).
1807 */
1808int hfi1_create_rcvhdrq(struct hfi1_devdata *dd, struct hfi1_ctxtdata *rcd)
1809{
1810 unsigned amt;
1811 u64 reg;
1812
1813 if (!rcd->rcvhdrq) {
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001814 dma_addr_t dma_hdrqtail;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001815 gfp_t gfp_flags;
1816
1817 /*
1818 * rcvhdrqentsize is in DWs, so we have to convert to bytes
1819 * (* sizeof(u32)).
1820 */
Amitoj Kaur Chawla84449912016-03-04 22:30:43 +05301821 amt = PAGE_ALIGN(rcd->rcvhdrq_cnt * rcd->rcvhdrqentsize *
1822 sizeof(u32));
Mike Marciniszyn77241052015-07-30 15:17:43 -04001823
Niranjana Vishwanathapuracc9a97e2017-11-06 06:38:52 -08001824 if (rcd->ctxt < dd->first_dyn_alloc_ctxt || rcd->is_vnic)
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -07001825 gfp_flags = GFP_KERNEL;
1826 else
1827 gfp_flags = GFP_USER;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001828 rcd->rcvhdrq = dma_zalloc_coherent(
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001829 &dd->pcidev->dev, amt, &rcd->rcvhdrq_dma,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001830 gfp_flags | __GFP_COMP);
1831
1832 if (!rcd->rcvhdrq) {
1833 dd_dev_err(dd,
Jubin John17fb4f22016-02-14 20:21:52 -08001834 "attempt to allocate %d bytes for ctxt %u rcvhdrq failed\n",
1835 amt, rcd->ctxt);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001836 goto bail;
1837 }
1838
Mike Marciniszyn77241052015-07-30 15:17:43 -04001839 if (HFI1_CAP_KGET_MASK(rcd->flags, DMA_RTAIL)) {
1840 rcd->rcvhdrtail_kvaddr = dma_zalloc_coherent(
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001841 &dd->pcidev->dev, PAGE_SIZE, &dma_hdrqtail,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001842 gfp_flags);
1843 if (!rcd->rcvhdrtail_kvaddr)
1844 goto bail_free;
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001845 rcd->rcvhdrqtailaddr_dma = dma_hdrqtail;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001846 }
1847
1848 rcd->rcvhdrq_size = amt;
1849 }
1850 /*
1851 * These values are per-context:
1852 * RcvHdrCnt
1853 * RcvHdrEntSize
1854 * RcvHdrSize
1855 */
1856 reg = ((u64)(rcd->rcvhdrq_cnt >> HDRQ_SIZE_SHIFT)
1857 & RCV_HDR_CNT_CNT_MASK)
1858 << RCV_HDR_CNT_CNT_SHIFT;
1859 write_kctxt_csr(dd, rcd->ctxt, RCV_HDR_CNT, reg);
1860 reg = (encode_rcv_header_entry_size(rcd->rcvhdrqentsize)
1861 & RCV_HDR_ENT_SIZE_ENT_SIZE_MASK)
1862 << RCV_HDR_ENT_SIZE_ENT_SIZE_SHIFT;
1863 write_kctxt_csr(dd, rcd->ctxt, RCV_HDR_ENT_SIZE, reg);
1864 reg = (dd->rcvhdrsize & RCV_HDR_SIZE_HDR_SIZE_MASK)
1865 << RCV_HDR_SIZE_HDR_SIZE_SHIFT;
1866 write_kctxt_csr(dd, rcd->ctxt, RCV_HDR_SIZE, reg);
Mark F. Brown46b010d2015-11-09 19:18:20 -05001867
1868 /*
1869 * Program dummy tail address for every receive context
1870 * before enabling any receive context
1871 */
1872 write_kctxt_csr(dd, rcd->ctxt, RCV_HDR_TAIL_ADDR,
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001873 dd->rcvhdrtail_dummy_dma);
Mark F. Brown46b010d2015-11-09 19:18:20 -05001874
Mike Marciniszyn77241052015-07-30 15:17:43 -04001875 return 0;
1876
1877bail_free:
1878 dd_dev_err(dd,
Jubin John17fb4f22016-02-14 20:21:52 -08001879 "attempt to allocate 1 page for ctxt %u rcvhdrqtailaddr failed\n",
1880 rcd->ctxt);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001881 dma_free_coherent(&dd->pcidev->dev, amt, rcd->rcvhdrq,
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001882 rcd->rcvhdrq_dma);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001883 rcd->rcvhdrq = NULL;
1884bail:
1885 return -ENOMEM;
1886}
1887
1888/**
1889 * allocate eager buffers, both kernel and user contexts.
1890 * @rcd: the context we are setting up.
1891 *
1892 * Allocate the eager TID buffers and program them into hip.
1893 * They are no longer completely contiguous, we do multiple allocation
1894 * calls. Otherwise we get the OOM code involved, by asking for too
1895 * much per call, with disastrous results on some kernels.
1896 */
1897int hfi1_setup_eagerbufs(struct hfi1_ctxtdata *rcd)
1898{
1899 struct hfi1_devdata *dd = rcd->dd;
1900 u32 max_entries, egrtop, alloced_bytes = 0, idx = 0;
1901 gfp_t gfp_flags;
1902 u16 order;
1903 int ret = 0;
1904 u16 round_mtu = roundup_pow_of_two(hfi1_max_mtu);
1905
1906 /*
1907 * GFP_USER, but without GFP_FS, so buffer cache can be
1908 * coalesced (we hope); otherwise, even at order 4,
1909 * heavy filesystem activity makes these fail, and we can
1910 * use compound pages.
1911 */
Mel Gorman71baba42015-11-06 16:28:28 -08001912 gfp_flags = __GFP_RECLAIM | __GFP_IO | __GFP_COMP;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001913
1914 /*
1915 * The minimum size of the eager buffers is a groups of MTU-sized
1916 * buffers.
1917 * The global eager_buffer_size parameter is checked against the
1918 * theoretical lower limit of the value. Here, we check against the
1919 * MTU.
1920 */
1921 if (rcd->egrbufs.size < (round_mtu * dd->rcv_entries.group_size))
1922 rcd->egrbufs.size = round_mtu * dd->rcv_entries.group_size;
1923 /*
1924 * If using one-pkt-per-egr-buffer, lower the eager buffer
1925 * size to the max MTU (page-aligned).
1926 */
1927 if (!HFI1_CAP_KGET_MASK(rcd->flags, MULTI_PKT_EGR))
1928 rcd->egrbufs.rcvtid_size = round_mtu;
1929
1930 /*
1931 * Eager buffers sizes of 1MB or less require smaller TID sizes
1932 * to satisfy the "multiple of 8 RcvArray entries" requirement.
1933 */
1934 if (rcd->egrbufs.size <= (1 << 20))
1935 rcd->egrbufs.rcvtid_size = max((unsigned long)round_mtu,
1936 rounddown_pow_of_two(rcd->egrbufs.size / 8));
1937
1938 while (alloced_bytes < rcd->egrbufs.size &&
1939 rcd->egrbufs.alloced < rcd->egrbufs.count) {
1940 rcd->egrbufs.buffers[idx].addr =
1941 dma_zalloc_coherent(&dd->pcidev->dev,
1942 rcd->egrbufs.rcvtid_size,
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001943 &rcd->egrbufs.buffers[idx].dma,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001944 gfp_flags);
1945 if (rcd->egrbufs.buffers[idx].addr) {
1946 rcd->egrbufs.buffers[idx].len =
1947 rcd->egrbufs.rcvtid_size;
1948 rcd->egrbufs.rcvtids[rcd->egrbufs.alloced].addr =
1949 rcd->egrbufs.buffers[idx].addr;
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001950 rcd->egrbufs.rcvtids[rcd->egrbufs.alloced].dma =
1951 rcd->egrbufs.buffers[idx].dma;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001952 rcd->egrbufs.alloced++;
1953 alloced_bytes += rcd->egrbufs.rcvtid_size;
1954 idx++;
1955 } else {
1956 u32 new_size, i, j;
1957 u64 offset = 0;
1958
1959 /*
1960 * Fail the eager buffer allocation if:
1961 * - we are already using the lowest acceptable size
1962 * - we are using one-pkt-per-egr-buffer (this implies
1963 * that we are accepting only one size)
1964 */
1965 if (rcd->egrbufs.rcvtid_size == round_mtu ||
1966 !HFI1_CAP_KGET_MASK(rcd->flags, MULTI_PKT_EGR)) {
1967 dd_dev_err(dd, "ctxt%u: Failed to allocate eager buffers\n",
Jubin John17fb4f22016-02-14 20:21:52 -08001968 rcd->ctxt);
Michael J. Ruhl94679062017-05-04 05:14:28 -07001969 ret = -ENOMEM;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001970 goto bail_rcvegrbuf_phys;
1971 }
1972
1973 new_size = rcd->egrbufs.rcvtid_size / 2;
1974
1975 /*
1976 * If the first attempt to allocate memory failed, don't
1977 * fail everything but continue with the next lower
1978 * size.
1979 */
1980 if (idx == 0) {
1981 rcd->egrbufs.rcvtid_size = new_size;
1982 continue;
1983 }
1984
1985 /*
1986 * Re-partition already allocated buffers to a smaller
1987 * size.
1988 */
1989 rcd->egrbufs.alloced = 0;
1990 for (i = 0, j = 0, offset = 0; j < idx; i++) {
1991 if (i >= rcd->egrbufs.count)
1992 break;
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001993 rcd->egrbufs.rcvtids[i].dma =
1994 rcd->egrbufs.buffers[j].dma + offset;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001995 rcd->egrbufs.rcvtids[i].addr =
1996 rcd->egrbufs.buffers[j].addr + offset;
1997 rcd->egrbufs.alloced++;
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001998 if ((rcd->egrbufs.buffers[j].dma + offset +
Mike Marciniszyn77241052015-07-30 15:17:43 -04001999 new_size) ==
Tymoteusz Kielan60368182016-09-06 04:35:54 -07002000 (rcd->egrbufs.buffers[j].dma +
Mike Marciniszyn77241052015-07-30 15:17:43 -04002001 rcd->egrbufs.buffers[j].len)) {
2002 j++;
2003 offset = 0;
Jubin Johne4909742016-02-14 20:22:00 -08002004 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -04002005 offset += new_size;
Jubin Johne4909742016-02-14 20:22:00 -08002006 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04002007 }
2008 rcd->egrbufs.rcvtid_size = new_size;
2009 }
2010 }
2011 rcd->egrbufs.numbufs = idx;
2012 rcd->egrbufs.size = alloced_bytes;
2013
Sebastian Sanchez6c63e422015-11-06 20:06:56 -05002014 hfi1_cdbg(PROC,
2015 "ctxt%u: Alloced %u rcv tid entries @ %uKB, total %zuKB\n",
Grzegorz Heldt23002d52016-07-25 13:39:33 -07002016 rcd->ctxt, rcd->egrbufs.alloced,
2017 rcd->egrbufs.rcvtid_size / 1024, rcd->egrbufs.size / 1024);
Sebastian Sanchez6c63e422015-11-06 20:06:56 -05002018
Mike Marciniszyn77241052015-07-30 15:17:43 -04002019 /*
2020 * Set the contexts rcv array head update threshold to the closest
2021 * power of 2 (so we can use a mask instead of modulo) below half
2022 * the allocated entries.
2023 */
2024 rcd->egrbufs.threshold =
2025 rounddown_pow_of_two(rcd->egrbufs.alloced / 2);
2026 /*
2027 * Compute the expected RcvArray entry base. This is done after
2028 * allocating the eager buffers in order to maximize the
2029 * expected RcvArray entries for the context.
2030 */
2031 max_entries = rcd->rcv_array_groups * dd->rcv_entries.group_size;
2032 egrtop = roundup(rcd->egrbufs.alloced, dd->rcv_entries.group_size);
2033 rcd->expected_count = max_entries - egrtop;
2034 if (rcd->expected_count > MAX_TID_PAIR_ENTRIES * 2)
2035 rcd->expected_count = MAX_TID_PAIR_ENTRIES * 2;
2036
2037 rcd->expected_base = rcd->eager_base + egrtop;
Sebastian Sanchez6c63e422015-11-06 20:06:56 -05002038 hfi1_cdbg(PROC, "ctxt%u: eager:%u, exp:%u, egrbase:%u, expbase:%u\n",
2039 rcd->ctxt, rcd->egrbufs.alloced, rcd->expected_count,
2040 rcd->eager_base, rcd->expected_base);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002041
2042 if (!hfi1_rcvbuf_validate(rcd->egrbufs.rcvtid_size, PT_EAGER, &order)) {
Sebastian Sanchez6c63e422015-11-06 20:06:56 -05002043 hfi1_cdbg(PROC,
2044 "ctxt%u: current Eager buffer size is invalid %u\n",
2045 rcd->ctxt, rcd->egrbufs.rcvtid_size);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002046 ret = -EINVAL;
Michael J. Ruhl62239fc2017-05-04 05:15:21 -07002047 goto bail_rcvegrbuf_phys;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002048 }
2049
2050 for (idx = 0; idx < rcd->egrbufs.alloced; idx++) {
2051 hfi1_put_tid(dd, rcd->eager_base + idx, PT_EAGER,
Tymoteusz Kielan60368182016-09-06 04:35:54 -07002052 rcd->egrbufs.rcvtids[idx].dma, order);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002053 cond_resched();
2054 }
Michael J. Ruhl62239fc2017-05-04 05:15:21 -07002055
2056 return 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002057
2058bail_rcvegrbuf_phys:
2059 for (idx = 0; idx < rcd->egrbufs.alloced &&
Jubin John17fb4f22016-02-14 20:21:52 -08002060 rcd->egrbufs.buffers[idx].addr;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002061 idx++) {
2062 dma_free_coherent(&dd->pcidev->dev,
2063 rcd->egrbufs.buffers[idx].len,
2064 rcd->egrbufs.buffers[idx].addr,
Tymoteusz Kielan60368182016-09-06 04:35:54 -07002065 rcd->egrbufs.buffers[idx].dma);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002066 rcd->egrbufs.buffers[idx].addr = NULL;
Tymoteusz Kielan60368182016-09-06 04:35:54 -07002067 rcd->egrbufs.buffers[idx].dma = 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002068 rcd->egrbufs.buffers[idx].len = 0;
2069 }
Michael J. Ruhl62239fc2017-05-04 05:15:21 -07002070
Mike Marciniszyn77241052015-07-30 15:17:43 -04002071 return ret;
2072}