blob: 23f0bbc9c4366906833e756a094c45b7d54e2dda [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;
126u32 hfi1_cpulist_count;
127unsigned long *hfi1_cpulist;
128
Michael J. Ruhlf2a3bc02017-08-04 13:52:38 -0700129static int hfi1_create_kctxt(struct hfi1_devdata *dd,
130 struct hfi1_pportdata *ppd)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400131{
Michael J. Ruhlf2a3bc02017-08-04 13:52:38 -0700132 struct hfi1_ctxtdata *rcd;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400133 int ret;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400134
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500135 /* Control context has to be always 0 */
136 BUILD_BUG_ON(HFI1_CTRL_CTXT != 0);
137
Michael J. Ruhlf2a3bc02017-08-04 13:52:38 -0700138 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;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400142 }
143
Ashutosh Dixitaffa48d2016-02-03 14:33:06 -0800144 /*
Michael J. Ruhlf2a3bc02017-08-04 13:52:38 -0700145 * 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.
Ashutosh Dixitaffa48d2016-02-03 14:33:06 -0800148 */
Michael J. Ruhlf2a3bc02017-08-04 13:52:38 -0700149 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);
Ashutosh Dixitaffa48d2016-02-03 14:33:06 -0800165
Mike Marciniszyn77241052015-07-30 15:17:43 -0400166 return 0;
Michael J. Ruhlf2a3bc02017-08-04 13:52:38 -0700167}
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -0700168
Michael J. Ruhlf2a3bc02017-08-04 13:52:38 -0700169/*
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 = kzalloc_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:
Michael J. Ruhlf683c802017-06-09 16:00:19 -0700190 for (i = 0; dd->rcd && i < dd->first_dyn_alloc_ctxt; ++i)
191 hfi1_rcd_put(dd->rcd[i]);
192
193 /* All the contexts should be freed, free the array */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400194 kfree(dd->rcd);
195 dd->rcd = NULL;
196 return ret;
197}
198
199/*
Michael J. Ruhlf683c802017-06-09 16:00:19 -0700200 * 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
Michael J. Ruhlf2a3bc02017-08-04 13:52:38 -0700207/**
208 * hfi1_rcd_free - When reference is zero clean up.
209 * @kref: pointer to an initialized rcd data structure
210 *
211 */
Michael J. Ruhlf683c802017-06-09 16:00:19 -0700212static void hfi1_rcd_free(struct kref *kref)
213{
214 struct hfi1_ctxtdata *rcd =
215 container_of(kref, struct hfi1_ctxtdata, kref);
216
217 hfi1_free_ctxtdata(rcd->dd, rcd);
218 kfree(rcd);
219}
220
Michael J. Ruhlf2a3bc02017-08-04 13:52:38 -0700221/**
222 * hfi1_rcd_put - decrement reference for rcd
223 * @rcd: pointer to an initialized rcd data structure
224 *
225 * Use this to put a reference after the init.
226 */
Michael J. Ruhlf683c802017-06-09 16:00:19 -0700227int hfi1_rcd_put(struct hfi1_ctxtdata *rcd)
228{
229 if (rcd)
230 return kref_put(&rcd->kref, hfi1_rcd_free);
231
232 return 0;
233}
234
Michael J. Ruhlf2a3bc02017-08-04 13:52:38 -0700235/**
236 * hfi1_rcd_get - increment reference for rcd
237 * @rcd: pointer to an initialized rcd data structure
238 *
239 * Use this to get a reference after the init.
240 */
Michael J. Ruhlf683c802017-06-09 16:00:19 -0700241void hfi1_rcd_get(struct hfi1_ctxtdata *rcd)
242{
243 kref_get(&rcd->kref);
244}
245
Michael J. Ruhlf2a3bc02017-08-04 13:52:38 -0700246/**
247 * allocate_rcd_index - allocate an rcd index from the rcd array
248 * @dd: pointer to a valid devdata structure
249 * @rcd: rcd data structure to assign
250 * @index: pointer to index that is allocated
251 *
252 * Find an empty index in the rcd array, and assign the given rcd to it.
253 * If the array is full, we are EBUSY.
254 *
255 */
256static u16 allocate_rcd_index(struct hfi1_devdata *dd,
257 struct hfi1_ctxtdata *rcd, u16 *index)
258{
259 unsigned long flags;
260 u16 ctxt;
261
262 spin_lock_irqsave(&dd->uctxt_lock, flags);
263 for (ctxt = 0; ctxt < dd->num_rcv_contexts; ctxt++)
264 if (!dd->rcd[ctxt])
265 break;
266
267 if (ctxt < dd->num_rcv_contexts) {
268 rcd->ctxt = ctxt;
269 dd->rcd[ctxt] = rcd;
270 hfi1_rcd_init(rcd);
271 }
272 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
273
274 if (ctxt >= dd->num_rcv_contexts)
275 return -EBUSY;
276
277 *index = ctxt;
278
279 return 0;
280}
281
Michael J. Ruhlf683c802017-06-09 16:00:19 -0700282/*
Mike Marciniszyn77241052015-07-30 15:17:43 -0400283 * Common code for user and kernel context setup.
284 */
Michael J. Ruhlf2a3bc02017-08-04 13:52:38 -0700285int hfi1_create_ctxtdata(struct hfi1_pportdata *ppd, int numa,
286 struct hfi1_ctxtdata **context)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400287{
288 struct hfi1_devdata *dd = ppd->dd;
289 struct hfi1_ctxtdata *rcd;
290 unsigned kctxt_ngroups = 0;
291 u32 base;
292
293 if (dd->rcv_entries.nctxt_extra >
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700294 dd->num_rcv_contexts - dd->first_dyn_alloc_ctxt)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400295 kctxt_ngroups = (dd->rcv_entries.nctxt_extra -
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700296 (dd->num_rcv_contexts - dd->first_dyn_alloc_ctxt));
Jianxin Xiong4dfe7cc2016-10-17 04:19:41 -0700297 rcd = kzalloc_node(sizeof(*rcd), GFP_KERNEL, numa);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400298 if (rcd) {
299 u32 rcvtids, max_entries;
Michael J. Ruhlf2a3bc02017-08-04 13:52:38 -0700300 u16 ctxt;
301 int ret;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400302
Sebastian Sanchez6c63e422015-11-06 20:06:56 -0500303 hfi1_cdbg(PROC, "setting up context %u\n", ctxt);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400304
Michael J. Ruhlf2a3bc02017-08-04 13:52:38 -0700305 ret = allocate_rcd_index(dd, rcd, &ctxt);
306 if (ret) {
307 *context = NULL;
308 kfree(rcd);
309 return ret;
310 }
311
Mike Marciniszyn77241052015-07-30 15:17:43 -0400312 INIT_LIST_HEAD(&rcd->qp_wait_list);
Michael J. Ruhlfe4e74e2017-06-09 16:00:12 -0700313 hfi1_exp_tid_group_init(&rcd->tid_group_list);
314 hfi1_exp_tid_group_init(&rcd->tid_used_list);
315 hfi1_exp_tid_group_init(&rcd->tid_full_list);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400316 rcd->ppd = ppd;
317 rcd->dd = dd;
Michael J. Ruhl8737ce92017-05-04 05:15:15 -0700318 __set_bit(0, rcd->in_use_ctxts);
Mitko Haralanov957558c2016-02-03 14:33:40 -0800319 rcd->numa_id = numa;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400320 rcd->rcv_array_groups = dd->rcv_entries.ngroups;
321
Mitko Haralanov463e6eb2016-02-05 11:57:53 -0500322 mutex_init(&rcd->exp_lock);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400323
324 /*
325 * Calculate the context's RcvArray entry starting point.
326 * We do this here because we have to take into account all
327 * the RcvArray entries that previous context would have
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700328 * taken and we have to account for any extra groups assigned
329 * to the static (kernel) or dynamic (vnic/user) contexts.
Mike Marciniszyn77241052015-07-30 15:17:43 -0400330 */
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700331 if (ctxt < dd->first_dyn_alloc_ctxt) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400332 if (ctxt < kctxt_ngroups) {
333 base = ctxt * (dd->rcv_entries.ngroups + 1);
334 rcd->rcv_array_groups++;
Dennis Dalessandroee495ad2017-04-09 10:17:18 -0700335 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400336 base = kctxt_ngroups +
337 (ctxt * dd->rcv_entries.ngroups);
Dennis Dalessandroee495ad2017-04-09 10:17:18 -0700338 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400339 } else {
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700340 u16 ct = ctxt - dd->first_dyn_alloc_ctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400341
342 base = ((dd->n_krcv_queues * dd->rcv_entries.ngroups) +
343 kctxt_ngroups);
344 if (ct < dd->rcv_entries.nctxt_extra) {
345 base += ct * (dd->rcv_entries.ngroups + 1);
346 rcd->rcv_array_groups++;
Dennis Dalessandroee495ad2017-04-09 10:17:18 -0700347 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400348 base += dd->rcv_entries.nctxt_extra +
349 (ct * dd->rcv_entries.ngroups);
Dennis Dalessandroee495ad2017-04-09 10:17:18 -0700350 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400351 }
352 rcd->eager_base = base * dd->rcv_entries.group_size;
353
Mike Marciniszyn77241052015-07-30 15:17:43 -0400354 rcd->rcvhdrq_cnt = rcvhdrcnt;
355 rcd->rcvhdrqentsize = hfi1_hdrq_entsize;
356 /*
357 * Simple Eager buffer allocation: we have already pre-allocated
358 * the number of RcvArray entry groups. Each ctxtdata structure
359 * holds the number of groups for that context.
360 *
361 * To follow CSR requirements and maintain cacheline alignment,
362 * make sure all sizes and bases are multiples of group_size.
363 *
364 * The expected entry count is what is left after assigning
365 * eager.
366 */
367 max_entries = rcd->rcv_array_groups *
368 dd->rcv_entries.group_size;
369 rcvtids = ((max_entries * hfi1_rcvarr_split) / 100);
370 rcd->egrbufs.count = round_down(rcvtids,
371 dd->rcv_entries.group_size);
372 if (rcd->egrbufs.count > MAX_EAGER_ENTRIES) {
373 dd_dev_err(dd, "ctxt%u: requested too many RcvArray entries.\n",
374 rcd->ctxt);
375 rcd->egrbufs.count = MAX_EAGER_ENTRIES;
376 }
Sebastian Sanchez6c63e422015-11-06 20:06:56 -0500377 hfi1_cdbg(PROC,
378 "ctxt%u: max Eager buffer RcvArray entries: %u\n",
379 rcd->ctxt, rcd->egrbufs.count);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400380
381 /*
382 * Allocate array that will hold the eager buffer accounting
383 * data.
384 * This will allocate the maximum possible buffer count based
385 * on the value of the RcvArray split parameter.
386 * The resulting value will be rounded down to the closest
387 * multiple of dd->rcv_entries.group_size.
388 */
Sebastian Sanchezb448bf92017-02-08 05:26:37 -0800389 rcd->egrbufs.buffers = kzalloc_node(
390 rcd->egrbufs.count * sizeof(*rcd->egrbufs.buffers),
391 GFP_KERNEL, numa);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400392 if (!rcd->egrbufs.buffers)
393 goto bail;
Sebastian Sanchezb448bf92017-02-08 05:26:37 -0800394 rcd->egrbufs.rcvtids = kzalloc_node(
395 rcd->egrbufs.count *
396 sizeof(*rcd->egrbufs.rcvtids),
397 GFP_KERNEL, numa);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400398 if (!rcd->egrbufs.rcvtids)
399 goto bail;
400 rcd->egrbufs.size = eager_buffer_size;
401 /*
402 * The size of the buffers programmed into the RcvArray
403 * entries needs to be big enough to handle the highest
404 * MTU supported.
405 */
406 if (rcd->egrbufs.size < hfi1_max_mtu) {
407 rcd->egrbufs.size = __roundup_pow_of_two(hfi1_max_mtu);
Sebastian Sanchez6c63e422015-11-06 20:06:56 -0500408 hfi1_cdbg(PROC,
409 "ctxt%u: eager bufs size too small. Adjusting to %zu\n",
Mike Marciniszyn77241052015-07-30 15:17:43 -0400410 rcd->ctxt, rcd->egrbufs.size);
411 }
412 rcd->egrbufs.rcvtid_size = HFI1_MAX_EAGER_BUFFER_SIZE;
413
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700414 /* Applicable only for statically created kernel contexts */
415 if (ctxt < dd->first_dyn_alloc_ctxt) {
Sebastian Sanchezb448bf92017-02-08 05:26:37 -0800416 rcd->opstats = kzalloc_node(sizeof(*rcd->opstats),
417 GFP_KERNEL, numa);
Alison Schofield806e6e12015-10-12 14:28:36 -0700418 if (!rcd->opstats)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400419 goto bail;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400420 }
Michael J. Ruhlf683c802017-06-09 16:00:19 -0700421
Michael J. Ruhlf2a3bc02017-08-04 13:52:38 -0700422 *context = rcd;
423 return 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400424 }
Michael J. Ruhlf2a3bc02017-08-04 13:52:38 -0700425
Mike Marciniszyn77241052015-07-30 15:17:43 -0400426bail:
Michael J. Ruhlf2a3bc02017-08-04 13:52:38 -0700427 *context = NULL;
428 hfi1_free_ctxt(dd, rcd);
429 return -ENOMEM;
430}
431
432/**
433 * hfi1_free_ctxt
434 * @dd: Pointer to a valid device
435 * @rcd: pointer to an initialized rcd data structure
436 *
437 * This is the "free" to match the _create_ctxtdata (alloc) function.
438 * This is the final "put" for the kref.
439 */
440void hfi1_free_ctxt(struct hfi1_devdata *dd, struct hfi1_ctxtdata *rcd)
441{
442 unsigned long flags;
443
444 if (rcd) {
445 spin_lock_irqsave(&dd->uctxt_lock, flags);
446 dd->rcd[rcd->ctxt] = NULL;
447 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
448 hfi1_rcd_put(rcd);
449 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400450}
451
452/*
453 * Convert a receive header entry size that to the encoding used in the CSR.
454 *
455 * Return a zero if the given size is invalid.
456 */
457static inline u64 encode_rcv_header_entry_size(u16 size)
458{
459 /* there are only 3 valid receive header entry sizes */
460 if (size == 2)
461 return 1;
462 if (size == 16)
463 return 2;
464 else if (size == 32)
465 return 4;
466 return 0; /* invalid */
467}
468
469/*
470 * Select the largest ccti value over all SLs to determine the intra-
471 * packet gap for the link.
472 *
473 * called with cca_timer_lock held (to protect access to cca_timer
474 * array), and rcu_read_lock() (to protect access to cc_state).
475 */
476void set_link_ipg(struct hfi1_pportdata *ppd)
477{
478 struct hfi1_devdata *dd = ppd->dd;
479 struct cc_state *cc_state;
480 int i;
481 u16 cce, ccti_limit, max_ccti = 0;
482 u16 shift, mult;
483 u64 src;
484 u32 current_egress_rate; /* Mbits /sec */
485 u32 max_pkt_time;
486 /*
487 * max_pkt_time is the maximum packet egress time in units
488 * of the fabric clock period 1/(805 MHz).
489 */
490
491 cc_state = get_cc_state(ppd);
492
Jubin Johnd125a6c2016-02-14 20:19:49 -0800493 if (!cc_state)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400494 /*
495 * This should _never_ happen - rcu_read_lock() is held,
496 * and set_link_ipg() should not be called if cc_state
497 * is NULL.
498 */
499 return;
500
501 for (i = 0; i < OPA_MAX_SLS; i++) {
502 u16 ccti = ppd->cca_timer[i].ccti;
503
504 if (ccti > max_ccti)
505 max_ccti = ccti;
506 }
507
508 ccti_limit = cc_state->cct.ccti_limit;
509 if (max_ccti > ccti_limit)
510 max_ccti = ccti_limit;
511
512 cce = cc_state->cct.entries[max_ccti].entry;
513 shift = (cce & 0xc000) >> 14;
514 mult = (cce & 0x3fff);
515
516 current_egress_rate = active_egress_rate(ppd);
517
518 max_pkt_time = egress_cycles(ppd->ibmaxlen, current_egress_rate);
519
520 src = (max_pkt_time >> shift) * mult;
521
522 src &= SEND_STATIC_RATE_CONTROL_CSR_SRC_RELOAD_SMASK;
523 src <<= SEND_STATIC_RATE_CONTROL_CSR_SRC_RELOAD_SHIFT;
524
525 write_csr(dd, SEND_STATIC_RATE_CONTROL, src);
526}
527
528static enum hrtimer_restart cca_timer_fn(struct hrtimer *t)
529{
530 struct cca_timer *cca_timer;
531 struct hfi1_pportdata *ppd;
532 int sl;
Jubin Johnd35cf7442016-04-14 08:31:53 -0700533 u16 ccti_timer, ccti_min;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400534 struct cc_state *cc_state;
Dean Luickb77d7132015-10-26 10:28:43 -0400535 unsigned long flags;
Jubin Johnd35cf7442016-04-14 08:31:53 -0700536 enum hrtimer_restart ret = HRTIMER_NORESTART;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400537
538 cca_timer = container_of(t, struct cca_timer, hrtimer);
539 ppd = cca_timer->ppd;
540 sl = cca_timer->sl;
541
542 rcu_read_lock();
543
544 cc_state = get_cc_state(ppd);
545
Jubin Johnd125a6c2016-02-14 20:19:49 -0800546 if (!cc_state) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400547 rcu_read_unlock();
548 return HRTIMER_NORESTART;
549 }
550
551 /*
552 * 1) decrement ccti for SL
553 * 2) calculate IPG for link (set_link_ipg())
554 * 3) restart timer, unless ccti is at min value
555 */
556
557 ccti_min = cc_state->cong_setting.entries[sl].ccti_min;
558 ccti_timer = cc_state->cong_setting.entries[sl].ccti_timer;
559
Dean Luickb77d7132015-10-26 10:28:43 -0400560 spin_lock_irqsave(&ppd->cca_timer_lock, flags);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400561
Jubin Johnd35cf7442016-04-14 08:31:53 -0700562 if (cca_timer->ccti > ccti_min) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400563 cca_timer->ccti--;
564 set_link_ipg(ppd);
565 }
566
Jubin Johnd35cf7442016-04-14 08:31:53 -0700567 if (cca_timer->ccti > ccti_min) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400568 unsigned long nsec = 1024 * ccti_timer;
569 /* ccti_timer is in units of 1.024 usec */
570 hrtimer_forward_now(t, ns_to_ktime(nsec));
Jubin Johnd35cf7442016-04-14 08:31:53 -0700571 ret = HRTIMER_RESTART;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400572 }
Jubin Johnd35cf7442016-04-14 08:31:53 -0700573
574 spin_unlock_irqrestore(&ppd->cca_timer_lock, flags);
575 rcu_read_unlock();
576 return ret;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400577}
578
579/*
580 * Common code for initializing the physical port structure.
581 */
582void hfi1_init_pportdata(struct pci_dev *pdev, struct hfi1_pportdata *ppd,
583 struct hfi1_devdata *dd, u8 hw_pidx, u8 port)
584{
Jianxin Xiong8adf71f2016-07-25 13:39:14 -0700585 int i;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400586 uint default_pkey_idx;
Jianxin Xiong8adf71f2016-07-25 13:39:14 -0700587 struct cc_state *cc_state;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400588
589 ppd->dd = dd;
590 ppd->hw_pidx = hw_pidx;
591 ppd->port = port; /* IB port number, not index */
592
593 default_pkey_idx = 1;
594
595 ppd->pkeys[default_pkey_idx] = DEFAULT_P_KEY;
Neel Desai53526502017-04-09 10:16:59 -0700596 ppd->part_enforce |= HFI1_PART_ENFORCE_IN;
Neel Desai53526502017-04-09 10:16:59 -0700597
Mike Marciniszyn77241052015-07-30 15:17:43 -0400598 if (loopback) {
599 hfi1_early_err(&pdev->dev,
600 "Faking data partition 0x8001 in idx %u\n",
601 !default_pkey_idx);
602 ppd->pkeys[!default_pkey_idx] = 0x8001;
603 }
604
605 INIT_WORK(&ppd->link_vc_work, handle_verify_cap);
606 INIT_WORK(&ppd->link_up_work, handle_link_up);
607 INIT_WORK(&ppd->link_down_work, handle_link_down);
608 INIT_WORK(&ppd->freeze_work, handle_freeze);
609 INIT_WORK(&ppd->link_downgrade_work, handle_link_downgrade);
610 INIT_WORK(&ppd->sma_message_work, handle_sma_message);
611 INIT_WORK(&ppd->link_bounce_work, handle_link_bounce);
Dean Luick673b9752016-08-31 07:24:33 -0700612 INIT_DELAYED_WORK(&ppd->start_link_work, handle_start_link);
Jim Snowfb9036d2016-01-11 18:32:21 -0500613 INIT_WORK(&ppd->linkstate_active_work, receive_interrupt_work);
Easwar Hariharan8ebd4cf2016-02-03 14:31:14 -0800614 INIT_WORK(&ppd->qsfp_info.qsfp_work, qsfp_event);
615
Mike Marciniszyn77241052015-07-30 15:17:43 -0400616 mutex_init(&ppd->hls_lock);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400617 spin_lock_init(&ppd->qsfp_info.qsfp_lock);
618
Easwar Hariharan8ebd4cf2016-02-03 14:31:14 -0800619 ppd->qsfp_info.ppd = ppd;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400620 ppd->sm_trap_qp = 0x0;
621 ppd->sa_qp = 0x1;
622
623 ppd->hfi1_wq = NULL;
624
625 spin_lock_init(&ppd->cca_timer_lock);
626
627 for (i = 0; i < OPA_MAX_SLS; i++) {
628 hrtimer_init(&ppd->cca_timer[i].hrtimer, CLOCK_MONOTONIC,
629 HRTIMER_MODE_REL);
630 ppd->cca_timer[i].ppd = ppd;
631 ppd->cca_timer[i].sl = i;
632 ppd->cca_timer[i].ccti = 0;
633 ppd->cca_timer[i].hrtimer.function = cca_timer_fn;
634 }
635
636 ppd->cc_max_table_entries = IB_CC_TABLE_CAP_DEFAULT;
637
638 spin_lock_init(&ppd->cc_state_lock);
639 spin_lock_init(&ppd->cc_log_lock);
Jianxin Xiong8adf71f2016-07-25 13:39:14 -0700640 cc_state = kzalloc(sizeof(*cc_state), GFP_KERNEL);
641 RCU_INIT_POINTER(ppd->cc_state, cc_state);
642 if (!cc_state)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400643 goto bail;
644 return;
645
646bail:
647
648 hfi1_early_err(&pdev->dev,
649 "Congestion Control Agent disabled for port %d\n", port);
650}
651
652/*
653 * Do initialization for device that is only needed on
654 * first detect, not on resets.
655 */
656static int loadtime_init(struct hfi1_devdata *dd)
657{
658 return 0;
659}
660
661/**
662 * init_after_reset - re-initialize after a reset
663 * @dd: the hfi1_ib device
664 *
665 * sanity check at least some of the values after reset, and
666 * ensure no receive or transmit (explicitly, in case reset
667 * failed
668 */
669static int init_after_reset(struct hfi1_devdata *dd)
670{
671 int i;
672
673 /*
674 * Ensure chip does no sends or receives, tail updates, or
675 * pioavail updates while we re-initialize. This is mostly
676 * for the driver data structures, not chip registers.
677 */
678 for (i = 0; i < dd->num_rcv_contexts; i++)
679 hfi1_rcvctrl(dd, HFI1_RCVCTRL_CTXT_DIS |
Michael J. Ruhl22505632017-07-24 07:46:06 -0700680 HFI1_RCVCTRL_INTRAVAIL_DIS |
681 HFI1_RCVCTRL_TAILUPD_DIS, dd->rcd[i]);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400682 pio_send_control(dd, PSC_GLOBAL_DISABLE);
683 for (i = 0; i < dd->num_send_contexts; i++)
684 sc_disable(dd->send_contexts[i].sc);
685
686 return 0;
687}
688
689static void enable_chip(struct hfi1_devdata *dd)
690{
691 u32 rcvmask;
Michael J. Ruhle6f76222017-07-24 07:45:55 -0700692 u16 i;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400693
694 /* enable PIO send */
695 pio_send_control(dd, PSC_GLOBAL_ENABLE);
696
697 /*
698 * Enable kernel ctxts' receive and receive interrupt.
699 * Other ctxts done as user opens and initializes them.
700 */
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700701 for (i = 0; i < dd->first_dyn_alloc_ctxt; ++i) {
Mitko Haralanov566c1572016-02-03 14:32:49 -0800702 rcvmask = HFI1_RCVCTRL_CTXT_ENB | HFI1_RCVCTRL_INTRAVAIL_ENB;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400703 rcvmask |= HFI1_CAP_KGET_MASK(dd->rcd[i]->flags, DMA_RTAIL) ?
704 HFI1_RCVCTRL_TAILUPD_ENB : HFI1_RCVCTRL_TAILUPD_DIS;
705 if (!HFI1_CAP_KGET_MASK(dd->rcd[i]->flags, MULTI_PKT_EGR))
706 rcvmask |= HFI1_RCVCTRL_ONE_PKT_EGR_ENB;
707 if (HFI1_CAP_KGET_MASK(dd->rcd[i]->flags, NODROP_RHQ_FULL))
708 rcvmask |= HFI1_RCVCTRL_NO_RHQ_DROP_ENB;
709 if (HFI1_CAP_KGET_MASK(dd->rcd[i]->flags, NODROP_EGR_FULL))
710 rcvmask |= HFI1_RCVCTRL_NO_EGR_DROP_ENB;
Michael J. Ruhl22505632017-07-24 07:46:06 -0700711 hfi1_rcvctrl(dd, rcvmask, dd->rcd[i]);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400712 sc_enable(dd->rcd[i]->sc);
713 }
714}
715
716/**
717 * create_workqueues - create per port workqueues
718 * @dd: the hfi1_ib device
719 */
720static int create_workqueues(struct hfi1_devdata *dd)
721{
722 int pidx;
723 struct hfi1_pportdata *ppd;
724
725 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
726 ppd = dd->pport + pidx;
727 if (!ppd->hfi1_wq) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400728 ppd->hfi1_wq =
Mike Marciniszyn0a226ed2015-11-09 19:13:58 -0500729 alloc_workqueue(
730 "hfi%d_%d",
731 WQ_SYSFS | WQ_HIGHPRI | WQ_CPU_INTENSIVE,
Mike Marciniszyndd1ed102017-05-04 05:14:10 -0700732 HFI1_MAX_ACTIVE_WORKQUEUE_ENTRIES,
Mike Marciniszyn0a226ed2015-11-09 19:13:58 -0500733 dd->unit, pidx);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400734 if (!ppd->hfi1_wq)
735 goto wq_error;
736 }
Sebastian Sanchez71d47002017-07-29 08:43:49 -0700737 if (!ppd->link_wq) {
738 /*
739 * Make the link workqueue single-threaded to enforce
740 * serialization.
741 */
742 ppd->link_wq =
743 alloc_workqueue(
744 "hfi_link_%d_%d",
745 WQ_SYSFS | WQ_MEM_RECLAIM | WQ_UNBOUND,
746 1, /* max_active */
747 dd->unit, pidx);
748 if (!ppd->link_wq)
749 goto wq_error;
750 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400751 }
752 return 0;
753wq_error:
Mike Marciniszyn0a226ed2015-11-09 19:13:58 -0500754 pr_err("alloc_workqueue failed for port %d\n", pidx + 1);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400755 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
756 ppd = dd->pport + pidx;
757 if (ppd->hfi1_wq) {
758 destroy_workqueue(ppd->hfi1_wq);
759 ppd->hfi1_wq = NULL;
760 }
Sebastian Sanchez71d47002017-07-29 08:43:49 -0700761 if (ppd->link_wq) {
762 destroy_workqueue(ppd->link_wq);
763 ppd->link_wq = NULL;
764 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400765 }
766 return -ENOMEM;
767}
768
769/**
770 * hfi1_init - do the actual initialization sequence on the chip
771 * @dd: the hfi1_ib device
772 * @reinit: re-initializing, so don't allocate new memory
773 *
774 * Do the actual initialization sequence on the chip. This is done
775 * both from the init routine called from the PCI infrastructure, and
776 * when we reset the chip, or detect that it was reset internally,
777 * or it's administratively re-enabled.
778 *
779 * Memory allocation here and in called routines is only done in
780 * the first case (reinit == 0). We have to be careful, because even
781 * without memory allocation, we need to re-write all the chip registers
782 * TIDs, etc. after the reset or enable has completed.
783 */
784int hfi1_init(struct hfi1_devdata *dd, int reinit)
785{
786 int ret = 0, pidx, lastfail = 0;
Michael J. Ruhle6f76222017-07-24 07:45:55 -0700787 unsigned long len;
788 u16 i;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400789 struct hfi1_ctxtdata *rcd;
790 struct hfi1_pportdata *ppd;
791
792 /* Set up recv low level handlers */
793 dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_EXPECTED] =
794 kdeth_process_expected;
795 dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_EAGER] =
796 kdeth_process_eager;
797 dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_IB] = process_receive_ib;
798 dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_ERROR] =
799 process_receive_error;
800 dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_BYPASS] =
801 process_receive_bypass;
802 dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_INVALID5] =
803 process_receive_invalid;
804 dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_INVALID6] =
805 process_receive_invalid;
806 dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_INVALID7] =
807 process_receive_invalid;
808 dd->rhf_rcv_function_map = dd->normal_rhf_rcv_functions;
809
810 /* Set up send low level handlers */
811 dd->process_pio_send = hfi1_verbs_send_pio;
812 dd->process_dma_send = hfi1_verbs_send_dma;
813 dd->pio_inline_send = pio_copy;
Vishwanathapura, Niranjana64551ed2017-04-12 20:29:30 -0700814 dd->process_vnic_dma_send = hfi1_vnic_send_dma;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400815
Mike Marciniszyn995deaf2015-11-16 21:59:29 -0500816 if (is_ax(dd)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400817 atomic_set(&dd->drop_packet, DROP_PACKET_ON);
818 dd->do_drop = 1;
819 } else {
820 atomic_set(&dd->drop_packet, DROP_PACKET_OFF);
821 dd->do_drop = 0;
822 }
823
824 /* make sure the link is not "up" */
825 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
826 ppd = dd->pport + pidx;
827 ppd->linkup = 0;
828 }
829
830 if (reinit)
831 ret = init_after_reset(dd);
832 else
833 ret = loadtime_init(dd);
834 if (ret)
835 goto done;
836
Mark F. Brown46b010d2015-11-09 19:18:20 -0500837 /* allocate dummy tail memory for all receive contexts */
838 dd->rcvhdrtail_dummy_kvaddr = dma_zalloc_coherent(
839 &dd->pcidev->dev, sizeof(u64),
Tymoteusz Kielan60368182016-09-06 04:35:54 -0700840 &dd->rcvhdrtail_dummy_dma,
Mark F. Brown46b010d2015-11-09 19:18:20 -0500841 GFP_KERNEL);
842
843 if (!dd->rcvhdrtail_dummy_kvaddr) {
844 dd_dev_err(dd, "cannot allocate dummy tail memory\n");
845 ret = -ENOMEM;
846 goto done;
847 }
848
Mike Marciniszyn77241052015-07-30 15:17:43 -0400849 /* dd->rcd can be NULL if early initialization failed */
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700850 for (i = 0; dd->rcd && i < dd->first_dyn_alloc_ctxt; ++i) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400851 /*
852 * Set up the (kernel) rcvhdr queue and egr TIDs. If doing
853 * re-init, the simplest way to handle this is to free
854 * existing, and re-allocate.
855 * Need to re-create rest of ctxt 0 ctxtdata as well.
856 */
857 rcd = dd->rcd[i];
858 if (!rcd)
859 continue;
860
861 rcd->do_interrupt = &handle_receive_interrupt;
862
863 lastfail = hfi1_create_rcvhdrq(dd, rcd);
864 if (!lastfail)
865 lastfail = hfi1_setup_eagerbufs(rcd);
Ashutosh Dixit39239792016-05-12 10:24:00 -0700866 if (lastfail) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400867 dd_dev_err(dd,
Jubin John17fb4f22016-02-14 20:21:52 -0800868 "failed to allocate kernel ctxt's rcvhdrq and/or egr bufs\n");
Ashutosh Dixit39239792016-05-12 10:24:00 -0700869 ret = lastfail;
870 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400871 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400872
873 /* Allocate enough memory for user event notification. */
Amitoj Kaur Chawla84449912016-03-04 22:30:43 +0530874 len = PAGE_ALIGN(dd->chip_rcv_contexts * HFI1_MAX_SHARED_CTXTS *
875 sizeof(*dd->events));
Mike Marciniszyn77241052015-07-30 15:17:43 -0400876 dd->events = vmalloc_user(len);
877 if (!dd->events)
878 dd_dev_err(dd, "Failed to allocate user events page\n");
879 /*
880 * Allocate a page for device and port status.
881 * Page will be shared amongst all user processes.
882 */
883 dd->status = vmalloc_user(PAGE_SIZE);
884 if (!dd->status)
885 dd_dev_err(dd, "Failed to allocate dev status page\n");
886 else
887 dd->freezelen = PAGE_SIZE - (sizeof(*dd->status) -
888 sizeof(dd->status->freezemsg));
889 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
890 ppd = dd->pport + pidx;
891 if (dd->status)
892 /* Currently, we only have one port */
893 ppd->statusp = &dd->status->port;
894
895 set_mtu(ppd);
896 }
897
898 /* enable chip even if we have an error, so we can debug cause */
899 enable_chip(dd);
900
Mike Marciniszyn77241052015-07-30 15:17:43 -0400901done:
902 /*
903 * Set status even if port serdes is not initialized
904 * so that diags will work.
905 */
906 if (dd->status)
907 dd->status->dev |= HFI1_STATUS_CHIP_PRESENT |
908 HFI1_STATUS_INITTED;
909 if (!ret) {
910 /* enable all interrupts from the chip */
911 set_intr_state(dd, 1);
912
913 /* chip is OK for user apps; mark it as initialized */
914 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
915 ppd = dd->pport + pidx;
916
Jubin John4d114fd2016-02-14 20:21:43 -0800917 /*
918 * start the serdes - must be after interrupts are
919 * enabled so we are notified when the link goes up
Mike Marciniszyn77241052015-07-30 15:17:43 -0400920 */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400921 lastfail = bringup_serdes(ppd);
922 if (lastfail)
923 dd_dev_info(dd,
Jubin John17fb4f22016-02-14 20:21:52 -0800924 "Failed to bring up port %u\n",
925 ppd->port);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400926
927 /*
928 * Set status even if port serdes is not initialized
929 * so that diags will work.
930 */
931 if (ppd->statusp)
932 *ppd->statusp |= HFI1_STATUS_CHIP_PRESENT |
933 HFI1_STATUS_INITTED;
934 if (!ppd->link_speed_enabled)
935 continue;
936 }
937 }
938
939 /* if ret is non-zero, we probably should do some cleanup here... */
940 return ret;
941}
942
943static inline struct hfi1_devdata *__hfi1_lookup(int unit)
944{
945 return idr_find(&hfi1_unit_table, unit);
946}
947
948struct hfi1_devdata *hfi1_lookup(int unit)
949{
950 struct hfi1_devdata *dd;
951 unsigned long flags;
952
953 spin_lock_irqsave(&hfi1_devs_lock, flags);
954 dd = __hfi1_lookup(unit);
955 spin_unlock_irqrestore(&hfi1_devs_lock, flags);
956
957 return dd;
958}
959
960/*
961 * Stop the timers during unit shutdown, or after an error late
962 * in initialization.
963 */
964static void stop_timers(struct hfi1_devdata *dd)
965{
966 struct hfi1_pportdata *ppd;
967 int pidx;
968
969 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
970 ppd = dd->pport + pidx;
971 if (ppd->led_override_timer.data) {
972 del_timer_sync(&ppd->led_override_timer);
973 atomic_set(&ppd->led_override_timer_active, 0);
974 }
975 }
976}
977
978/**
979 * shutdown_device - shut down a device
980 * @dd: the hfi1_ib device
981 *
982 * This is called to make the device quiet when we are about to
983 * unload the driver, and also when the device is administratively
984 * disabled. It does not free any data structures.
985 * Everything it does has to be setup again by hfi1_init(dd, 1)
986 */
987static void shutdown_device(struct hfi1_devdata *dd)
988{
989 struct hfi1_pportdata *ppd;
990 unsigned pidx;
991 int i;
992
993 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
994 ppd = dd->pport + pidx;
995
996 ppd->linkup = 0;
997 if (ppd->statusp)
998 *ppd->statusp &= ~(HFI1_STATUS_IB_CONF |
999 HFI1_STATUS_IB_READY);
1000 }
1001 dd->flags &= ~HFI1_INITTED;
1002
1003 /* mask interrupts, but not errors */
1004 set_intr_state(dd, 0);
1005
1006 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
1007 ppd = dd->pport + pidx;
1008 for (i = 0; i < dd->num_rcv_contexts; i++)
1009 hfi1_rcvctrl(dd, HFI1_RCVCTRL_TAILUPD_DIS |
Michael J. Ruhl22505632017-07-24 07:46:06 -07001010 HFI1_RCVCTRL_CTXT_DIS |
1011 HFI1_RCVCTRL_INTRAVAIL_DIS |
1012 HFI1_RCVCTRL_PKEY_DIS |
1013 HFI1_RCVCTRL_ONE_PKT_EGR_DIS, dd->rcd[i]);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001014 /*
1015 * Gracefully stop all sends allowing any in progress to
1016 * trickle out first.
1017 */
1018 for (i = 0; i < dd->num_send_contexts; i++)
1019 sc_flush(dd->send_contexts[i].sc);
1020 }
1021
1022 /*
1023 * Enough for anything that's going to trickle out to have actually
1024 * done so.
1025 */
1026 udelay(20);
1027
1028 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
1029 ppd = dd->pport + pidx;
1030
1031 /* disable all contexts */
1032 for (i = 0; i < dd->num_send_contexts; i++)
1033 sc_disable(dd->send_contexts[i].sc);
1034 /* disable the send device */
1035 pio_send_control(dd, PSC_GLOBAL_DISABLE);
1036
Easwar Hariharan91ab4ed2016-02-03 14:35:57 -08001037 shutdown_led_override(ppd);
1038
Mike Marciniszyn77241052015-07-30 15:17:43 -04001039 /*
1040 * Clear SerdesEnable.
1041 * We can't count on interrupts since we are stopping.
1042 */
1043 hfi1_quiet_serdes(ppd);
1044
1045 if (ppd->hfi1_wq) {
1046 destroy_workqueue(ppd->hfi1_wq);
1047 ppd->hfi1_wq = NULL;
1048 }
Sebastian Sanchez71d47002017-07-29 08:43:49 -07001049 if (ppd->link_wq) {
1050 destroy_workqueue(ppd->link_wq);
1051 ppd->link_wq = NULL;
1052 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001053 }
1054 sdma_exit(dd);
1055}
1056
1057/**
1058 * hfi1_free_ctxtdata - free a context's allocated data
1059 * @dd: the hfi1_ib device
1060 * @rcd: the ctxtdata structure
1061 *
1062 * free up any allocated data for a context
Mike Marciniszyn77241052015-07-30 15:17:43 -04001063 * It should never change any chip state, or global driver state.
1064 */
1065void hfi1_free_ctxtdata(struct hfi1_devdata *dd, struct hfi1_ctxtdata *rcd)
1066{
Michael J. Ruhlf683c802017-06-09 16:00:19 -07001067 u32 e;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001068
1069 if (!rcd)
1070 return;
1071
1072 if (rcd->rcvhdrq) {
1073 dma_free_coherent(&dd->pcidev->dev, rcd->rcvhdrq_size,
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001074 rcd->rcvhdrq, rcd->rcvhdrq_dma);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001075 rcd->rcvhdrq = NULL;
1076 if (rcd->rcvhdrtail_kvaddr) {
1077 dma_free_coherent(&dd->pcidev->dev, PAGE_SIZE,
1078 (void *)rcd->rcvhdrtail_kvaddr,
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001079 rcd->rcvhdrqtailaddr_dma);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001080 rcd->rcvhdrtail_kvaddr = NULL;
1081 }
1082 }
1083
1084 /* all the RcvArray entries should have been cleared by now */
1085 kfree(rcd->egrbufs.rcvtids);
Michael J. Ruhlf683c802017-06-09 16:00:19 -07001086 rcd->egrbufs.rcvtids = NULL;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001087
1088 for (e = 0; e < rcd->egrbufs.alloced; e++) {
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001089 if (rcd->egrbufs.buffers[e].dma)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001090 dma_free_coherent(&dd->pcidev->dev,
1091 rcd->egrbufs.buffers[e].len,
1092 rcd->egrbufs.buffers[e].addr,
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001093 rcd->egrbufs.buffers[e].dma);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001094 }
1095 kfree(rcd->egrbufs.buffers);
Michael J. Ruhlf683c802017-06-09 16:00:19 -07001096 rcd->egrbufs.alloced = 0;
1097 rcd->egrbufs.buffers = NULL;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001098
1099 sc_free(rcd->sc);
Michael J. Ruhlf683c802017-06-09 16:00:19 -07001100 rcd->sc = NULL;
1101
Mike Marciniszyn77241052015-07-30 15:17:43 -04001102 vfree(rcd->subctxt_uregbase);
1103 vfree(rcd->subctxt_rcvegrbuf);
1104 vfree(rcd->subctxt_rcvhdr_base);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001105 kfree(rcd->opstats);
Michael J. Ruhlf683c802017-06-09 16:00:19 -07001106
1107 rcd->subctxt_uregbase = NULL;
1108 rcd->subctxt_rcvegrbuf = NULL;
1109 rcd->subctxt_rcvhdr_base = NULL;
1110 rcd->opstats = NULL;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001111}
1112
Dean Luick78eb1292016-03-05 08:49:45 -08001113/*
1114 * Release our hold on the shared asic data. If we are the last one,
Dean Luickdba715f2016-07-06 17:28:52 -04001115 * return the structure to be finalized outside the lock. Must be
1116 * holding hfi1_devs_lock.
Dean Luick78eb1292016-03-05 08:49:45 -08001117 */
Dean Luickdba715f2016-07-06 17:28:52 -04001118static struct hfi1_asic_data *release_asic_data(struct hfi1_devdata *dd)
Dean Luick78eb1292016-03-05 08:49:45 -08001119{
Dean Luickdba715f2016-07-06 17:28:52 -04001120 struct hfi1_asic_data *ad;
Dean Luick78eb1292016-03-05 08:49:45 -08001121 int other;
1122
1123 if (!dd->asic_data)
Dean Luickdba715f2016-07-06 17:28:52 -04001124 return NULL;
Dean Luick78eb1292016-03-05 08:49:45 -08001125 dd->asic_data->dds[dd->hfi1_id] = NULL;
1126 other = dd->hfi1_id ? 0 : 1;
Dean Luickdba715f2016-07-06 17:28:52 -04001127 ad = dd->asic_data;
Dean Luick78eb1292016-03-05 08:49:45 -08001128 dd->asic_data = NULL;
Dean Luickdba715f2016-07-06 17:28:52 -04001129 /* return NULL if the other dd still has a link */
1130 return ad->dds[other] ? NULL : ad;
1131}
1132
1133static void finalize_asic_data(struct hfi1_devdata *dd,
1134 struct hfi1_asic_data *ad)
1135{
1136 clean_up_i2c(dd, ad);
1137 kfree(ad);
Dean Luick78eb1292016-03-05 08:49:45 -08001138}
1139
Dennis Dalessandroe11ffbd2016-05-19 05:26:44 -07001140static void __hfi1_free_devdata(struct kobject *kobj)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001141{
Dennis Dalessandroe11ffbd2016-05-19 05:26:44 -07001142 struct hfi1_devdata *dd =
1143 container_of(kobj, struct hfi1_devdata, kobj);
Dean Luickdba715f2016-07-06 17:28:52 -04001144 struct hfi1_asic_data *ad;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001145 unsigned long flags;
1146
1147 spin_lock_irqsave(&hfi1_devs_lock, flags);
1148 idr_remove(&hfi1_unit_table, dd->unit);
1149 list_del(&dd->list);
Dean Luickdba715f2016-07-06 17:28:52 -04001150 ad = release_asic_data(dd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001151 spin_unlock_irqrestore(&hfi1_devs_lock, flags);
Dean Luickdba715f2016-07-06 17:28:52 -04001152 if (ad)
1153 finalize_asic_data(dd, ad);
Easwar Hariharanc3838b32016-02-09 14:29:13 -08001154 free_platform_config(dd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001155 rcu_barrier(); /* wait for rcu callbacks to complete */
1156 free_percpu(dd->int_counter);
1157 free_percpu(dd->rcv_limit);
Vennila Megavannan89abfc82016-02-03 14:34:07 -08001158 free_percpu(dd->send_schedule);
Jubin Johnea0e4ce2016-04-20 06:05:24 -07001159 rvt_dealloc_device(&dd->verbs_dev.rdi);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001160}
1161
Dennis Dalessandroe11ffbd2016-05-19 05:26:44 -07001162static struct kobj_type hfi1_devdata_type = {
1163 .release = __hfi1_free_devdata,
1164};
1165
1166void hfi1_free_devdata(struct hfi1_devdata *dd)
1167{
1168 kobject_put(&dd->kobj);
1169}
1170
Mike Marciniszyn77241052015-07-30 15:17:43 -04001171/*
1172 * Allocate our primary per-unit data structure. Must be done via verbs
1173 * allocator, because the verbs cleanup process both does cleanup and
1174 * free of the data structure.
1175 * "extra" is for chip-specific data.
1176 *
1177 * Use the idr mechanism to get a unit number for this unit.
1178 */
1179struct hfi1_devdata *hfi1_alloc_devdata(struct pci_dev *pdev, size_t extra)
1180{
1181 unsigned long flags;
1182 struct hfi1_devdata *dd;
Dennis Dalessandro7af6d002016-01-19 14:44:06 -08001183 int ret, nports;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001184
Dennis Dalessandro7af6d002016-01-19 14:44:06 -08001185 /* extra is * number of ports */
1186 nports = extra / sizeof(struct hfi1_pportdata);
1187
1188 dd = (struct hfi1_devdata *)rvt_alloc_device(sizeof(*dd) + extra,
1189 nports);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001190 if (!dd)
1191 return ERR_PTR(-ENOMEM);
Dennis Dalessandro7af6d002016-01-19 14:44:06 -08001192 dd->num_pports = nports;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001193 dd->pport = (struct hfi1_pportdata *)(dd + 1);
1194
1195 INIT_LIST_HEAD(&dd->list);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001196 idr_preload(GFP_KERNEL);
1197 spin_lock_irqsave(&hfi1_devs_lock, flags);
1198
1199 ret = idr_alloc(&hfi1_unit_table, dd, 0, 0, GFP_NOWAIT);
1200 if (ret >= 0) {
1201 dd->unit = ret;
1202 list_add(&dd->list, &hfi1_dev_list);
1203 }
1204
1205 spin_unlock_irqrestore(&hfi1_devs_lock, flags);
1206 idr_preload_end();
1207
1208 if (ret < 0) {
1209 hfi1_early_err(&pdev->dev,
1210 "Could not allocate unit ID: error %d\n", -ret);
1211 goto bail;
1212 }
1213 /*
1214 * Initialize all locks for the device. This needs to be as early as
1215 * possible so locks are usable.
1216 */
1217 spin_lock_init(&dd->sc_lock);
1218 spin_lock_init(&dd->sendctrl_lock);
1219 spin_lock_init(&dd->rcvctrl_lock);
1220 spin_lock_init(&dd->uctxt_lock);
1221 spin_lock_init(&dd->hfi1_diag_trans_lock);
1222 spin_lock_init(&dd->sc_init_lock);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001223 spin_lock_init(&dd->dc8051_memlock);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001224 seqlock_init(&dd->sc2vl_lock);
1225 spin_lock_init(&dd->sde_map_lock);
Jubin John35f6bef2016-02-14 12:46:10 -08001226 spin_lock_init(&dd->pio_map_lock);
Tadeusz Struk22546b72017-04-28 10:40:02 -07001227 mutex_init(&dd->dc8051_lock);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001228 init_waitqueue_head(&dd->event_queue);
1229
1230 dd->int_counter = alloc_percpu(u64);
1231 if (!dd->int_counter) {
1232 ret = -ENOMEM;
1233 hfi1_early_err(&pdev->dev,
1234 "Could not allocate per-cpu int_counter\n");
1235 goto bail;
1236 }
1237
1238 dd->rcv_limit = alloc_percpu(u64);
1239 if (!dd->rcv_limit) {
1240 ret = -ENOMEM;
1241 hfi1_early_err(&pdev->dev,
1242 "Could not allocate per-cpu rcv_limit\n");
1243 goto bail;
1244 }
1245
Vennila Megavannan89abfc82016-02-03 14:34:07 -08001246 dd->send_schedule = alloc_percpu(u64);
1247 if (!dd->send_schedule) {
1248 ret = -ENOMEM;
1249 hfi1_early_err(&pdev->dev,
1250 "Could not allocate per-cpu int_counter\n");
1251 goto bail;
1252 }
1253
Mike Marciniszyn77241052015-07-30 15:17:43 -04001254 if (!hfi1_cpulist_count) {
1255 u32 count = num_online_cpus();
1256
Shraddha Barke314fcc02015-10-09 21:03:26 +05301257 hfi1_cpulist = kcalloc(BITS_TO_LONGS(count), sizeof(long),
1258 GFP_KERNEL);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001259 if (hfi1_cpulist)
1260 hfi1_cpulist_count = count;
1261 else
1262 hfi1_early_err(
1263 &pdev->dev,
1264 "Could not alloc cpulist info, cpu affinity might be wrong\n");
1265 }
Dennis Dalessandroe11ffbd2016-05-19 05:26:44 -07001266 kobject_init(&dd->kobj, &hfi1_devdata_type);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001267 return dd;
1268
1269bail:
1270 if (!list_empty(&dd->list))
1271 list_del_init(&dd->list);
Jubin Johnea0e4ce2016-04-20 06:05:24 -07001272 rvt_dealloc_device(&dd->verbs_dev.rdi);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001273 return ERR_PTR(ret);
1274}
1275
1276/*
1277 * Called from freeze mode handlers, and from PCI error
1278 * reporting code. Should be paranoid about state of
1279 * system and data structures.
1280 */
1281void hfi1_disable_after_error(struct hfi1_devdata *dd)
1282{
1283 if (dd->flags & HFI1_INITTED) {
1284 u32 pidx;
1285
1286 dd->flags &= ~HFI1_INITTED;
1287 if (dd->pport)
1288 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
1289 struct hfi1_pportdata *ppd;
1290
1291 ppd = dd->pport + pidx;
1292 if (dd->flags & HFI1_PRESENT)
1293 set_link_state(ppd, HLS_DN_DISABLE);
1294
1295 if (ppd->statusp)
1296 *ppd->statusp &= ~HFI1_STATUS_IB_READY;
1297 }
1298 }
1299
1300 /*
1301 * Mark as having had an error for driver, and also
1302 * for /sys and status word mapped to user programs.
1303 * This marks unit as not usable, until reset.
1304 */
1305 if (dd->status)
1306 dd->status->dev |= HFI1_STATUS_HWERROR;
1307}
1308
1309static void remove_one(struct pci_dev *);
1310static int init_one(struct pci_dev *, const struct pci_device_id *);
1311
1312#define DRIVER_LOAD_MSG "Intel " DRIVER_NAME " loaded: "
1313#define PFX DRIVER_NAME ": "
1314
Sebastian Sanchezd6373012016-07-25 07:54:48 -07001315const struct pci_device_id hfi1_pci_tbl[] = {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001316 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL0) },
1317 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL1) },
1318 { 0, }
1319};
1320
1321MODULE_DEVICE_TABLE(pci, hfi1_pci_tbl);
1322
1323static struct pci_driver hfi1_pci_driver = {
1324 .name = DRIVER_NAME,
1325 .probe = init_one,
1326 .remove = remove_one,
1327 .id_table = hfi1_pci_tbl,
1328 .err_handler = &hfi1_pci_err_handler,
1329};
1330
1331static void __init compute_krcvqs(void)
1332{
1333 int i;
1334
1335 for (i = 0; i < krcvqsset; i++)
1336 n_krcvqs += krcvqs[i];
1337}
1338
1339/*
1340 * Do all the generic driver unit- and chip-independent memory
1341 * allocation and initialization.
1342 */
1343static int __init hfi1_mod_init(void)
1344{
1345 int ret;
1346
1347 ret = dev_init();
1348 if (ret)
1349 goto bail;
1350
Sebastian Sanchezd6373012016-07-25 07:54:48 -07001351 ret = node_affinity_init();
1352 if (ret)
1353 goto bail;
Dennis Dalessandro41973442016-07-25 07:52:36 -07001354
Mike Marciniszyn77241052015-07-30 15:17:43 -04001355 /* validate max MTU before any devices start */
1356 if (!valid_opa_max_mtu(hfi1_max_mtu)) {
1357 pr_err("Invalid max_mtu 0x%x, using 0x%x instead\n",
1358 hfi1_max_mtu, HFI1_DEFAULT_MAX_MTU);
1359 hfi1_max_mtu = HFI1_DEFAULT_MAX_MTU;
1360 }
1361 /* valid CUs run from 1-128 in powers of 2 */
1362 if (hfi1_cu > 128 || !is_power_of_2(hfi1_cu))
1363 hfi1_cu = 1;
1364 /* valid credit return threshold is 0-100, variable is unsigned */
1365 if (user_credit_return_threshold > 100)
1366 user_credit_return_threshold = 100;
1367
1368 compute_krcvqs();
Jubin John4d114fd2016-02-14 20:21:43 -08001369 /*
1370 * sanitize receive interrupt count, time must wait until after
1371 * the hardware type is known
1372 */
Mike Marciniszyn77241052015-07-30 15:17:43 -04001373 if (rcv_intr_count > RCV_HDR_HEAD_COUNTER_MASK)
1374 rcv_intr_count = RCV_HDR_HEAD_COUNTER_MASK;
1375 /* reject invalid combinations */
1376 if (rcv_intr_count == 0 && rcv_intr_timeout == 0) {
1377 pr_err("Invalid mode: both receive interrupt count and available timeout are zero - setting interrupt count to 1\n");
1378 rcv_intr_count = 1;
1379 }
1380 if (rcv_intr_count > 1 && rcv_intr_timeout == 0) {
1381 /*
1382 * Avoid indefinite packet delivery by requiring a timeout
1383 * if count is > 1.
1384 */
1385 pr_err("Invalid mode: receive interrupt count greater than 1 and available timeout is zero - setting available timeout to 1\n");
1386 rcv_intr_timeout = 1;
1387 }
1388 if (rcv_intr_dynamic && !(rcv_intr_count > 1 && rcv_intr_timeout > 0)) {
1389 /*
1390 * The dynamic algorithm expects a non-zero timeout
1391 * and a count > 1.
1392 */
1393 pr_err("Invalid mode: dynamic receive interrupt mitigation with invalid count and timeout - turning dynamic off\n");
1394 rcv_intr_dynamic = 0;
1395 }
1396
1397 /* sanitize link CRC options */
1398 link_crc_mask &= SUPPORTED_CRCS;
1399
1400 /*
1401 * These must be called before the driver is registered with
1402 * the PCI subsystem.
1403 */
1404 idr_init(&hfi1_unit_table);
1405
1406 hfi1_dbg_init();
Dean Luick528ee9f2016-03-05 08:50:43 -08001407 ret = hfi1_wss_init();
1408 if (ret < 0)
1409 goto bail_wss;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001410 ret = pci_register_driver(&hfi1_pci_driver);
1411 if (ret < 0) {
1412 pr_err("Unable to register driver: error %d\n", -ret);
1413 goto bail_dev;
1414 }
1415 goto bail; /* all OK */
1416
1417bail_dev:
Dean Luick528ee9f2016-03-05 08:50:43 -08001418 hfi1_wss_exit();
1419bail_wss:
Mike Marciniszyn77241052015-07-30 15:17:43 -04001420 hfi1_dbg_exit();
1421 idr_destroy(&hfi1_unit_table);
1422 dev_cleanup();
1423bail:
1424 return ret;
1425}
1426
1427module_init(hfi1_mod_init);
1428
1429/*
1430 * Do the non-unit driver cleanup, memory free, etc. at unload.
1431 */
1432static void __exit hfi1_mod_cleanup(void)
1433{
1434 pci_unregister_driver(&hfi1_pci_driver);
Dennis Dalessandro41973442016-07-25 07:52:36 -07001435 node_affinity_destroy();
Dean Luick528ee9f2016-03-05 08:50:43 -08001436 hfi1_wss_exit();
Mike Marciniszyn77241052015-07-30 15:17:43 -04001437 hfi1_dbg_exit();
1438 hfi1_cpulist_count = 0;
1439 kfree(hfi1_cpulist);
1440
1441 idr_destroy(&hfi1_unit_table);
1442 dispose_firmware(); /* asymmetric with obtain_firmware() */
1443 dev_cleanup();
1444}
1445
1446module_exit(hfi1_mod_cleanup);
1447
1448/* this can only be called after a successful initialization */
1449static void cleanup_device_data(struct hfi1_devdata *dd)
1450{
1451 int ctxt;
1452 int pidx;
1453 struct hfi1_ctxtdata **tmp;
1454 unsigned long flags;
1455
1456 /* users can't do anything more with chip */
1457 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
1458 struct hfi1_pportdata *ppd = &dd->pport[pidx];
1459 struct cc_state *cc_state;
1460 int i;
1461
1462 if (ppd->statusp)
1463 *ppd->statusp &= ~HFI1_STATUS_CHIP_PRESENT;
1464
1465 for (i = 0; i < OPA_MAX_SLS; i++)
1466 hrtimer_cancel(&ppd->cca_timer[i].hrtimer);
1467
1468 spin_lock(&ppd->cc_state_lock);
Jianxin Xiong8adf71f2016-07-25 13:39:14 -07001469 cc_state = get_cc_state_protected(ppd);
Muhammad Falak R Wanieea57072016-05-01 18:05:31 +05301470 RCU_INIT_POINTER(ppd->cc_state, NULL);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001471 spin_unlock(&ppd->cc_state_lock);
1472
1473 if (cc_state)
Wei Yongjun476d95b2016-08-10 03:14:04 +00001474 kfree_rcu(cc_state, rcu);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001475 }
1476
1477 free_credit_return(dd);
1478
1479 /*
1480 * Free any resources still in use (usually just kernel contexts)
1481 * at unload; we do for ctxtcnt, because that's what we allocate.
1482 * We acquire lock to be really paranoid that rcd isn't being
1483 * accessed from some interrupt-related code (that should not happen,
1484 * but best to be sure).
1485 */
1486 spin_lock_irqsave(&dd->uctxt_lock, flags);
1487 tmp = dd->rcd;
1488 dd->rcd = NULL;
1489 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
Mark F. Brown46b010d2015-11-09 19:18:20 -05001490
1491 if (dd->rcvhdrtail_dummy_kvaddr) {
1492 dma_free_coherent(&dd->pcidev->dev, sizeof(u64),
1493 (void *)dd->rcvhdrtail_dummy_kvaddr,
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001494 dd->rcvhdrtail_dummy_dma);
Dan Carpentera8b7da52016-05-28 08:01:20 +03001495 dd->rcvhdrtail_dummy_kvaddr = NULL;
Mark F. Brown46b010d2015-11-09 19:18:20 -05001496 }
1497
Mike Marciniszyn77241052015-07-30 15:17:43 -04001498 for (ctxt = 0; tmp && ctxt < dd->num_rcv_contexts; ctxt++) {
1499 struct hfi1_ctxtdata *rcd = tmp[ctxt];
1500
1501 tmp[ctxt] = NULL; /* debugging paranoia */
1502 if (rcd) {
1503 hfi1_clear_tids(rcd);
Michael J. Ruhlf683c802017-06-09 16:00:19 -07001504 hfi1_rcd_put(rcd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001505 }
1506 }
1507 kfree(tmp);
Jubin John35f6bef2016-02-14 12:46:10 -08001508 free_pio_map(dd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001509 /* must follow rcv context free - need to remove rcv's hooks */
1510 for (ctxt = 0; ctxt < dd->num_send_contexts; ctxt++)
1511 sc_free(dd->send_contexts[ctxt].sc);
1512 dd->num_send_contexts = 0;
1513 kfree(dd->send_contexts);
1514 dd->send_contexts = NULL;
Jubin John79d0c082016-02-26 13:33:33 -08001515 kfree(dd->hw_to_sw);
1516 dd->hw_to_sw = NULL;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001517 kfree(dd->boardname);
1518 vfree(dd->events);
1519 vfree(dd->status);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001520}
1521
1522/*
1523 * Clean up on unit shutdown, or error during unit load after
1524 * successful initialization.
1525 */
1526static void postinit_cleanup(struct hfi1_devdata *dd)
1527{
1528 hfi1_start_cleanup(dd);
1529
1530 hfi1_pcie_ddcleanup(dd);
1531 hfi1_pcie_cleanup(dd->pcidev);
1532
1533 cleanup_device_data(dd);
1534
1535 hfi1_free_devdata(dd);
1536}
1537
Krzysztof Blaszkowski11501ab2016-10-25 13:12:11 -07001538static int init_validate_rcvhdrcnt(struct device *dev, uint thecnt)
1539{
1540 if (thecnt <= HFI1_MIN_HDRQ_EGRBUF_CNT) {
1541 hfi1_early_err(dev, "Receive header queue count too small\n");
1542 return -EINVAL;
1543 }
1544
1545 if (thecnt > HFI1_MAX_HDRQ_EGRBUF_CNT) {
1546 hfi1_early_err(dev,
1547 "Receive header queue count cannot be greater than %u\n",
1548 HFI1_MAX_HDRQ_EGRBUF_CNT);
1549 return -EINVAL;
1550 }
1551
1552 if (thecnt % HDRQ_INCREMENT) {
1553 hfi1_early_err(dev, "Receive header queue count %d must be divisible by %lu\n",
1554 thecnt, HDRQ_INCREMENT);
1555 return -EINVAL;
1556 }
1557
1558 return 0;
1559}
1560
Mike Marciniszyn77241052015-07-30 15:17:43 -04001561static int init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
1562{
1563 int ret = 0, j, pidx, initfail;
Krzysztof Blaszkowski83fb4af2016-10-17 04:19:24 -07001564 struct hfi1_devdata *dd;
Harish Chegondie8597eb2015-12-01 15:38:20 -05001565 struct hfi1_pportdata *ppd;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001566
1567 /* First, lock the non-writable module parameters */
1568 HFI1_CAP_LOCK();
1569
Tadeusz Struk5d6f08a2017-03-20 17:25:29 -07001570 /* Validate dev ids */
1571 if (!(ent->device == PCI_DEVICE_ID_INTEL0 ||
1572 ent->device == PCI_DEVICE_ID_INTEL1)) {
1573 hfi1_early_err(&pdev->dev,
1574 "Failing on unknown Intel deviceid 0x%x\n",
1575 ent->device);
1576 ret = -ENODEV;
1577 goto bail;
1578 }
1579
Mike Marciniszyn77241052015-07-30 15:17:43 -04001580 /* Validate some global module parameters */
Krzysztof Blaszkowski11501ab2016-10-25 13:12:11 -07001581 ret = init_validate_rcvhdrcnt(&pdev->dev, rcvhdrcnt);
1582 if (ret)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001583 goto bail;
Krzysztof Blaszkowski11501ab2016-10-25 13:12:11 -07001584
Mike Marciniszyn77241052015-07-30 15:17:43 -04001585 /* use the encoding function as a sanitization check */
1586 if (!encode_rcv_header_entry_size(hfi1_hdrq_entsize)) {
1587 hfi1_early_err(&pdev->dev, "Invalid HdrQ Entry size %u\n",
1588 hfi1_hdrq_entsize);
Sebastian Sanchez07859de2015-12-10 16:02:49 -05001589 ret = -EINVAL;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001590 goto bail;
1591 }
1592
1593 /* The receive eager buffer size must be set before the receive
1594 * contexts are created.
1595 *
1596 * Set the eager buffer size. Validate that it falls in a range
1597 * allowed by the hardware - all powers of 2 between the min and
1598 * max. The maximum valid MTU is within the eager buffer range
1599 * so we do not need to cap the max_mtu by an eager buffer size
1600 * setting.
1601 */
1602 if (eager_buffer_size) {
1603 if (!is_power_of_2(eager_buffer_size))
1604 eager_buffer_size =
1605 roundup_pow_of_two(eager_buffer_size);
1606 eager_buffer_size =
1607 clamp_val(eager_buffer_size,
1608 MIN_EAGER_BUFFER * 8,
1609 MAX_EAGER_BUFFER_TOTAL);
1610 hfi1_early_info(&pdev->dev, "Eager buffer size %u\n",
1611 eager_buffer_size);
1612 } else {
1613 hfi1_early_err(&pdev->dev, "Invalid Eager buffer size of 0\n");
1614 ret = -EINVAL;
1615 goto bail;
1616 }
1617
1618 /* restrict value of hfi1_rcvarr_split */
1619 hfi1_rcvarr_split = clamp_val(hfi1_rcvarr_split, 0, 100);
1620
1621 ret = hfi1_pcie_init(pdev, ent);
1622 if (ret)
1623 goto bail;
1624
Krzysztof Blaszkowski83fb4af2016-10-17 04:19:24 -07001625 /*
1626 * Do device-specific initialization, function table setup, dd
1627 * allocation, etc.
1628 */
1629 dd = hfi1_init_dd(pdev, ent);
1630
1631 if (IS_ERR(dd)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001632 ret = PTR_ERR(dd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001633 goto clean_bail; /* error already printed */
Krzysztof Blaszkowski83fb4af2016-10-17 04:19:24 -07001634 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001635
1636 ret = create_workqueues(dd);
1637 if (ret)
1638 goto clean_bail;
1639
1640 /* do the generic initialization */
1641 initfail = hfi1_init(dd, 0);
1642
Vishwanathapura, Niranjanad4829ea2017-04-12 20:29:28 -07001643 /* setup vnic */
1644 hfi1_vnic_setup(dd);
1645
Mike Marciniszyn77241052015-07-30 15:17:43 -04001646 ret = hfi1_register_ib_device(dd);
1647
1648 /*
1649 * Now ready for use. this should be cleared whenever we
1650 * detect a reset, or initiate one. If earlier failure,
1651 * we still create devices, so diags, etc. can be used
1652 * to determine cause of problem.
1653 */
Dean Luicked6f6532016-02-18 11:12:25 -08001654 if (!initfail && !ret) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001655 dd->flags |= HFI1_INITTED;
Dean Luicked6f6532016-02-18 11:12:25 -08001656 /* create debufs files after init and ib register */
1657 hfi1_dbg_ibdev_init(&dd->verbs_dev);
1658 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001659
1660 j = hfi1_device_create(dd);
1661 if (j)
1662 dd_dev_err(dd, "Failed to create /dev devices: %d\n", -j);
1663
1664 if (initfail || ret) {
1665 stop_timers(dd);
1666 flush_workqueue(ib_wq);
Harish Chegondie8597eb2015-12-01 15:38:20 -05001667 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001668 hfi1_quiet_serdes(dd->pport + pidx);
Harish Chegondie8597eb2015-12-01 15:38:20 -05001669 ppd = dd->pport + pidx;
1670 if (ppd->hfi1_wq) {
1671 destroy_workqueue(ppd->hfi1_wq);
1672 ppd->hfi1_wq = NULL;
1673 }
Sebastian Sanchez71d47002017-07-29 08:43:49 -07001674 if (ppd->link_wq) {
1675 destroy_workqueue(ppd->link_wq);
1676 ppd->link_wq = NULL;
1677 }
Harish Chegondie8597eb2015-12-01 15:38:20 -05001678 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001679 if (!j)
1680 hfi1_device_remove(dd);
1681 if (!ret)
1682 hfi1_unregister_ib_device(dd);
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -07001683 hfi1_vnic_cleanup(dd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001684 postinit_cleanup(dd);
1685 if (initfail)
1686 ret = initfail;
1687 goto bail; /* everything already cleaned */
1688 }
1689
1690 sdma_start(dd);
1691
1692 return 0;
1693
1694clean_bail:
1695 hfi1_pcie_cleanup(pdev);
1696bail:
1697 return ret;
1698}
1699
Tadeusz Strukacd7c8f2016-10-25 08:57:55 -07001700static void wait_for_clients(struct hfi1_devdata *dd)
1701{
1702 /*
1703 * Remove the device init value and complete the device if there is
1704 * no clients or wait for active clients to finish.
1705 */
1706 if (atomic_dec_and_test(&dd->user_refcount))
1707 complete(&dd->user_comp);
1708
1709 wait_for_completion(&dd->user_comp);
1710}
1711
Mike Marciniszyn77241052015-07-30 15:17:43 -04001712static void remove_one(struct pci_dev *pdev)
1713{
1714 struct hfi1_devdata *dd = pci_get_drvdata(pdev);
1715
Dean Luicked6f6532016-02-18 11:12:25 -08001716 /* close debugfs files before ib unregister */
1717 hfi1_dbg_ibdev_exit(&dd->verbs_dev);
Tadeusz Strukacd7c8f2016-10-25 08:57:55 -07001718
1719 /* remove the /dev hfi1 interface */
1720 hfi1_device_remove(dd);
1721
1722 /* wait for existing user space clients to finish */
1723 wait_for_clients(dd);
1724
Mike Marciniszyn77241052015-07-30 15:17:43 -04001725 /* unregister from IB core */
1726 hfi1_unregister_ib_device(dd);
1727
Vishwanathapura, Niranjanad4829ea2017-04-12 20:29:28 -07001728 /* cleanup vnic */
1729 hfi1_vnic_cleanup(dd);
1730
Mike Marciniszyn77241052015-07-30 15:17:43 -04001731 /*
1732 * Disable the IB link, disable interrupts on the device,
1733 * clear dma engines, etc.
1734 */
1735 shutdown_device(dd);
1736
1737 stop_timers(dd);
1738
1739 /* wait until all of our (qsfp) queue_work() calls complete */
1740 flush_workqueue(ib_wq);
1741
Mike Marciniszyn77241052015-07-30 15:17:43 -04001742 postinit_cleanup(dd);
1743}
1744
1745/**
1746 * hfi1_create_rcvhdrq - create a receive header queue
1747 * @dd: the hfi1_ib device
1748 * @rcd: the context data
1749 *
1750 * This must be contiguous memory (from an i/o perspective), and must be
1751 * DMA'able (which means for some systems, it will go through an IOMMU,
1752 * or be forced into a low address range).
1753 */
1754int hfi1_create_rcvhdrq(struct hfi1_devdata *dd, struct hfi1_ctxtdata *rcd)
1755{
1756 unsigned amt;
1757 u64 reg;
1758
1759 if (!rcd->rcvhdrq) {
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001760 dma_addr_t dma_hdrqtail;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001761 gfp_t gfp_flags;
1762
1763 /*
1764 * rcvhdrqentsize is in DWs, so we have to convert to bytes
1765 * (* sizeof(u32)).
1766 */
Amitoj Kaur Chawla84449912016-03-04 22:30:43 +05301767 amt = PAGE_ALIGN(rcd->rcvhdrq_cnt * rcd->rcvhdrqentsize *
1768 sizeof(u32));
Mike Marciniszyn77241052015-07-30 15:17:43 -04001769
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -07001770 if ((rcd->ctxt < dd->first_dyn_alloc_ctxt) ||
1771 (rcd->sc && (rcd->sc->type == SC_KERNEL)))
1772 gfp_flags = GFP_KERNEL;
1773 else
1774 gfp_flags = GFP_USER;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001775 rcd->rcvhdrq = dma_zalloc_coherent(
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001776 &dd->pcidev->dev, amt, &rcd->rcvhdrq_dma,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001777 gfp_flags | __GFP_COMP);
1778
1779 if (!rcd->rcvhdrq) {
1780 dd_dev_err(dd,
Jubin John17fb4f22016-02-14 20:21:52 -08001781 "attempt to allocate %d bytes for ctxt %u rcvhdrq failed\n",
1782 amt, rcd->ctxt);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001783 goto bail;
1784 }
1785
Mike Marciniszyn77241052015-07-30 15:17:43 -04001786 if (HFI1_CAP_KGET_MASK(rcd->flags, DMA_RTAIL)) {
1787 rcd->rcvhdrtail_kvaddr = dma_zalloc_coherent(
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001788 &dd->pcidev->dev, PAGE_SIZE, &dma_hdrqtail,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001789 gfp_flags);
1790 if (!rcd->rcvhdrtail_kvaddr)
1791 goto bail_free;
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001792 rcd->rcvhdrqtailaddr_dma = dma_hdrqtail;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001793 }
1794
1795 rcd->rcvhdrq_size = amt;
1796 }
1797 /*
1798 * These values are per-context:
1799 * RcvHdrCnt
1800 * RcvHdrEntSize
1801 * RcvHdrSize
1802 */
1803 reg = ((u64)(rcd->rcvhdrq_cnt >> HDRQ_SIZE_SHIFT)
1804 & RCV_HDR_CNT_CNT_MASK)
1805 << RCV_HDR_CNT_CNT_SHIFT;
1806 write_kctxt_csr(dd, rcd->ctxt, RCV_HDR_CNT, reg);
1807 reg = (encode_rcv_header_entry_size(rcd->rcvhdrqentsize)
1808 & RCV_HDR_ENT_SIZE_ENT_SIZE_MASK)
1809 << RCV_HDR_ENT_SIZE_ENT_SIZE_SHIFT;
1810 write_kctxt_csr(dd, rcd->ctxt, RCV_HDR_ENT_SIZE, reg);
1811 reg = (dd->rcvhdrsize & RCV_HDR_SIZE_HDR_SIZE_MASK)
1812 << RCV_HDR_SIZE_HDR_SIZE_SHIFT;
1813 write_kctxt_csr(dd, rcd->ctxt, RCV_HDR_SIZE, reg);
Mark F. Brown46b010d2015-11-09 19:18:20 -05001814
1815 /*
1816 * Program dummy tail address for every receive context
1817 * before enabling any receive context
1818 */
1819 write_kctxt_csr(dd, rcd->ctxt, RCV_HDR_TAIL_ADDR,
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001820 dd->rcvhdrtail_dummy_dma);
Mark F. Brown46b010d2015-11-09 19:18:20 -05001821
Mike Marciniszyn77241052015-07-30 15:17:43 -04001822 return 0;
1823
1824bail_free:
1825 dd_dev_err(dd,
Jubin John17fb4f22016-02-14 20:21:52 -08001826 "attempt to allocate 1 page for ctxt %u rcvhdrqtailaddr failed\n",
1827 rcd->ctxt);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001828 dma_free_coherent(&dd->pcidev->dev, amt, rcd->rcvhdrq,
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001829 rcd->rcvhdrq_dma);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001830 rcd->rcvhdrq = NULL;
1831bail:
1832 return -ENOMEM;
1833}
1834
1835/**
1836 * allocate eager buffers, both kernel and user contexts.
1837 * @rcd: the context we are setting up.
1838 *
1839 * Allocate the eager TID buffers and program them into hip.
1840 * They are no longer completely contiguous, we do multiple allocation
1841 * calls. Otherwise we get the OOM code involved, by asking for too
1842 * much per call, with disastrous results on some kernels.
1843 */
1844int hfi1_setup_eagerbufs(struct hfi1_ctxtdata *rcd)
1845{
1846 struct hfi1_devdata *dd = rcd->dd;
1847 u32 max_entries, egrtop, alloced_bytes = 0, idx = 0;
1848 gfp_t gfp_flags;
1849 u16 order;
1850 int ret = 0;
1851 u16 round_mtu = roundup_pow_of_two(hfi1_max_mtu);
1852
1853 /*
1854 * GFP_USER, but without GFP_FS, so buffer cache can be
1855 * coalesced (we hope); otherwise, even at order 4,
1856 * heavy filesystem activity makes these fail, and we can
1857 * use compound pages.
1858 */
Mel Gorman71baba42015-11-06 16:28:28 -08001859 gfp_flags = __GFP_RECLAIM | __GFP_IO | __GFP_COMP;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001860
1861 /*
1862 * The minimum size of the eager buffers is a groups of MTU-sized
1863 * buffers.
1864 * The global eager_buffer_size parameter is checked against the
1865 * theoretical lower limit of the value. Here, we check against the
1866 * MTU.
1867 */
1868 if (rcd->egrbufs.size < (round_mtu * dd->rcv_entries.group_size))
1869 rcd->egrbufs.size = round_mtu * dd->rcv_entries.group_size;
1870 /*
1871 * If using one-pkt-per-egr-buffer, lower the eager buffer
1872 * size to the max MTU (page-aligned).
1873 */
1874 if (!HFI1_CAP_KGET_MASK(rcd->flags, MULTI_PKT_EGR))
1875 rcd->egrbufs.rcvtid_size = round_mtu;
1876
1877 /*
1878 * Eager buffers sizes of 1MB or less require smaller TID sizes
1879 * to satisfy the "multiple of 8 RcvArray entries" requirement.
1880 */
1881 if (rcd->egrbufs.size <= (1 << 20))
1882 rcd->egrbufs.rcvtid_size = max((unsigned long)round_mtu,
1883 rounddown_pow_of_two(rcd->egrbufs.size / 8));
1884
1885 while (alloced_bytes < rcd->egrbufs.size &&
1886 rcd->egrbufs.alloced < rcd->egrbufs.count) {
1887 rcd->egrbufs.buffers[idx].addr =
1888 dma_zalloc_coherent(&dd->pcidev->dev,
1889 rcd->egrbufs.rcvtid_size,
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001890 &rcd->egrbufs.buffers[idx].dma,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001891 gfp_flags);
1892 if (rcd->egrbufs.buffers[idx].addr) {
1893 rcd->egrbufs.buffers[idx].len =
1894 rcd->egrbufs.rcvtid_size;
1895 rcd->egrbufs.rcvtids[rcd->egrbufs.alloced].addr =
1896 rcd->egrbufs.buffers[idx].addr;
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001897 rcd->egrbufs.rcvtids[rcd->egrbufs.alloced].dma =
1898 rcd->egrbufs.buffers[idx].dma;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001899 rcd->egrbufs.alloced++;
1900 alloced_bytes += rcd->egrbufs.rcvtid_size;
1901 idx++;
1902 } else {
1903 u32 new_size, i, j;
1904 u64 offset = 0;
1905
1906 /*
1907 * Fail the eager buffer allocation if:
1908 * - we are already using the lowest acceptable size
1909 * - we are using one-pkt-per-egr-buffer (this implies
1910 * that we are accepting only one size)
1911 */
1912 if (rcd->egrbufs.rcvtid_size == round_mtu ||
1913 !HFI1_CAP_KGET_MASK(rcd->flags, MULTI_PKT_EGR)) {
1914 dd_dev_err(dd, "ctxt%u: Failed to allocate eager buffers\n",
Jubin John17fb4f22016-02-14 20:21:52 -08001915 rcd->ctxt);
Michael J. Ruhl94679062017-05-04 05:14:28 -07001916 ret = -ENOMEM;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001917 goto bail_rcvegrbuf_phys;
1918 }
1919
1920 new_size = rcd->egrbufs.rcvtid_size / 2;
1921
1922 /*
1923 * If the first attempt to allocate memory failed, don't
1924 * fail everything but continue with the next lower
1925 * size.
1926 */
1927 if (idx == 0) {
1928 rcd->egrbufs.rcvtid_size = new_size;
1929 continue;
1930 }
1931
1932 /*
1933 * Re-partition already allocated buffers to a smaller
1934 * size.
1935 */
1936 rcd->egrbufs.alloced = 0;
1937 for (i = 0, j = 0, offset = 0; j < idx; i++) {
1938 if (i >= rcd->egrbufs.count)
1939 break;
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001940 rcd->egrbufs.rcvtids[i].dma =
1941 rcd->egrbufs.buffers[j].dma + offset;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001942 rcd->egrbufs.rcvtids[i].addr =
1943 rcd->egrbufs.buffers[j].addr + offset;
1944 rcd->egrbufs.alloced++;
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001945 if ((rcd->egrbufs.buffers[j].dma + offset +
Mike Marciniszyn77241052015-07-30 15:17:43 -04001946 new_size) ==
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001947 (rcd->egrbufs.buffers[j].dma +
Mike Marciniszyn77241052015-07-30 15:17:43 -04001948 rcd->egrbufs.buffers[j].len)) {
1949 j++;
1950 offset = 0;
Jubin Johne4909742016-02-14 20:22:00 -08001951 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001952 offset += new_size;
Jubin Johne4909742016-02-14 20:22:00 -08001953 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001954 }
1955 rcd->egrbufs.rcvtid_size = new_size;
1956 }
1957 }
1958 rcd->egrbufs.numbufs = idx;
1959 rcd->egrbufs.size = alloced_bytes;
1960
Sebastian Sanchez6c63e422015-11-06 20:06:56 -05001961 hfi1_cdbg(PROC,
1962 "ctxt%u: Alloced %u rcv tid entries @ %uKB, total %zuKB\n",
Grzegorz Heldt23002d52016-07-25 13:39:33 -07001963 rcd->ctxt, rcd->egrbufs.alloced,
1964 rcd->egrbufs.rcvtid_size / 1024, rcd->egrbufs.size / 1024);
Sebastian Sanchez6c63e422015-11-06 20:06:56 -05001965
Mike Marciniszyn77241052015-07-30 15:17:43 -04001966 /*
1967 * Set the contexts rcv array head update threshold to the closest
1968 * power of 2 (so we can use a mask instead of modulo) below half
1969 * the allocated entries.
1970 */
1971 rcd->egrbufs.threshold =
1972 rounddown_pow_of_two(rcd->egrbufs.alloced / 2);
1973 /*
1974 * Compute the expected RcvArray entry base. This is done after
1975 * allocating the eager buffers in order to maximize the
1976 * expected RcvArray entries for the context.
1977 */
1978 max_entries = rcd->rcv_array_groups * dd->rcv_entries.group_size;
1979 egrtop = roundup(rcd->egrbufs.alloced, dd->rcv_entries.group_size);
1980 rcd->expected_count = max_entries - egrtop;
1981 if (rcd->expected_count > MAX_TID_PAIR_ENTRIES * 2)
1982 rcd->expected_count = MAX_TID_PAIR_ENTRIES * 2;
1983
1984 rcd->expected_base = rcd->eager_base + egrtop;
Sebastian Sanchez6c63e422015-11-06 20:06:56 -05001985 hfi1_cdbg(PROC, "ctxt%u: eager:%u, exp:%u, egrbase:%u, expbase:%u\n",
1986 rcd->ctxt, rcd->egrbufs.alloced, rcd->expected_count,
1987 rcd->eager_base, rcd->expected_base);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001988
1989 if (!hfi1_rcvbuf_validate(rcd->egrbufs.rcvtid_size, PT_EAGER, &order)) {
Sebastian Sanchez6c63e422015-11-06 20:06:56 -05001990 hfi1_cdbg(PROC,
1991 "ctxt%u: current Eager buffer size is invalid %u\n",
1992 rcd->ctxt, rcd->egrbufs.rcvtid_size);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001993 ret = -EINVAL;
Michael J. Ruhl62239fc2017-05-04 05:15:21 -07001994 goto bail_rcvegrbuf_phys;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001995 }
1996
1997 for (idx = 0; idx < rcd->egrbufs.alloced; idx++) {
1998 hfi1_put_tid(dd, rcd->eager_base + idx, PT_EAGER,
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001999 rcd->egrbufs.rcvtids[idx].dma, order);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002000 cond_resched();
2001 }
Michael J. Ruhl62239fc2017-05-04 05:15:21 -07002002
2003 return 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002004
2005bail_rcvegrbuf_phys:
2006 for (idx = 0; idx < rcd->egrbufs.alloced &&
Jubin John17fb4f22016-02-14 20:21:52 -08002007 rcd->egrbufs.buffers[idx].addr;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002008 idx++) {
2009 dma_free_coherent(&dd->pcidev->dev,
2010 rcd->egrbufs.buffers[idx].len,
2011 rcd->egrbufs.buffers[idx].addr,
Tymoteusz Kielan60368182016-09-06 04:35:54 -07002012 rcd->egrbufs.buffers[idx].dma);
Mike Marciniszyn77241052015-07-30 15:17:43 -04002013 rcd->egrbufs.buffers[idx].addr = NULL;
Tymoteusz Kielan60368182016-09-06 04:35:54 -07002014 rcd->egrbufs.buffers[idx].dma = 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -04002015 rcd->egrbufs.buffers[idx].len = 0;
2016 }
Michael J. Ruhl62239fc2017-05-04 05:15:21 -07002017
Mike Marciniszyn77241052015-07-30 15:17:43 -04002018 return ret;
2019}