blob: be027c9c880ee9f0f84b101f6c115294f91dc8d0 [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
129/*
130 * Common code for creating the receive context array.
131 */
132int hfi1_create_ctxts(struct hfi1_devdata *dd)
133{
Michael J. Ruhle6f76222017-07-24 07:45:55 -0700134 u16 i;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400135 int ret;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400136
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500137 /* Control context has to be always 0 */
138 BUILD_BUG_ON(HFI1_CTRL_CTXT != 0);
139
Mitko Haralanov377f1112016-02-03 14:33:58 -0800140 dd->rcd = kzalloc_node(dd->num_rcv_contexts * sizeof(*dd->rcd),
141 GFP_KERNEL, dd->node);
Alison Schofield806e6e12015-10-12 14:28:36 -0700142 if (!dd->rcd)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400143 goto nomem;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400144
145 /* create one or more kernel contexts */
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700146 for (i = 0; i < dd->first_dyn_alloc_ctxt; ++i) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400147 struct hfi1_pportdata *ppd;
148 struct hfi1_ctxtdata *rcd;
149
150 ppd = dd->pport + (i % dd->num_pports);
Jianxin Xiong4dfe7cc2016-10-17 04:19:41 -0700151
152 /* dd->rcd[i] gets assigned inside the callee */
Mitko Haralanov957558c2016-02-03 14:33:40 -0800153 rcd = hfi1_create_ctxtdata(ppd, i, dd->node);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400154 if (!rcd) {
155 dd_dev_err(dd,
Jubin John17fb4f22016-02-14 20:21:52 -0800156 "Unable to allocate kernel receive context, failing\n");
Mike Marciniszyn77241052015-07-30 15:17:43 -0400157 goto nomem;
158 }
159 /*
160 * Set up the kernel context flags here and now because they
161 * use default values for all receive side memories. User
162 * contexts will be handled as they are created.
163 */
164 rcd->flags = HFI1_CAP_KGET(MULTI_PKT_EGR) |
165 HFI1_CAP_KGET(NODROP_RHQ_FULL) |
166 HFI1_CAP_KGET(NODROP_EGR_FULL) |
167 HFI1_CAP_KGET(DMA_RTAIL);
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500168
169 /* Control context must use DMA_RTAIL */
170 if (rcd->ctxt == HFI1_CTRL_CTXT)
171 rcd->flags |= HFI1_CAP_DMA_RTAIL;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400172 rcd->seq_cnt = 1;
173
174 rcd->sc = sc_alloc(dd, SC_ACK, rcd->rcvhdrqentsize, dd->node);
175 if (!rcd->sc) {
176 dd_dev_err(dd,
Jubin John17fb4f22016-02-14 20:21:52 -0800177 "Unable to allocate kernel send context, failing\n");
Mike Marciniszyn77241052015-07-30 15:17:43 -0400178 goto nomem;
179 }
180
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -0700181 hfi1_init_ctxt(rcd->sc);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400182 }
183
Ashutosh Dixitaffa48d2016-02-03 14:33:06 -0800184 /*
185 * Initialize aspm, to be done after gen3 transition and setting up
186 * contexts and before enabling interrupts
187 */
188 aspm_init(dd);
189
Mike Marciniszyn77241052015-07-30 15:17:43 -0400190 return 0;
191nomem:
192 ret = -ENOMEM;
Michael J. Ruhl9b60d2c2017-05-04 05:15:09 -0700193
Michael J. Ruhlf683c802017-06-09 16:00:19 -0700194 for (i = 0; dd->rcd && i < dd->first_dyn_alloc_ctxt; ++i)
195 hfi1_rcd_put(dd->rcd[i]);
196
197 /* All the contexts should be freed, free the array */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400198 kfree(dd->rcd);
199 dd->rcd = NULL;
200 return ret;
201}
202
203/*
Michael J. Ruhlf683c802017-06-09 16:00:19 -0700204 * Helper routines for the receive context reference count (rcd and uctxt)
205 */
206static void hfi1_rcd_init(struct hfi1_ctxtdata *rcd)
207{
208 kref_init(&rcd->kref);
209}
210
211static void hfi1_rcd_free(struct kref *kref)
212{
213 struct hfi1_ctxtdata *rcd =
214 container_of(kref, struct hfi1_ctxtdata, kref);
215
216 hfi1_free_ctxtdata(rcd->dd, rcd);
217 kfree(rcd);
218}
219
220int hfi1_rcd_put(struct hfi1_ctxtdata *rcd)
221{
222 if (rcd)
223 return kref_put(&rcd->kref, hfi1_rcd_free);
224
225 return 0;
226}
227
228void hfi1_rcd_get(struct hfi1_ctxtdata *rcd)
229{
230 kref_get(&rcd->kref);
231}
232
233/*
Mike Marciniszyn77241052015-07-30 15:17:43 -0400234 * Common code for user and kernel context setup.
235 */
Michael J. Ruhle6f76222017-07-24 07:45:55 -0700236struct hfi1_ctxtdata *hfi1_create_ctxtdata(struct hfi1_pportdata *ppd, u16 ctxt,
Mitko Haralanov957558c2016-02-03 14:33:40 -0800237 int numa)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400238{
239 struct hfi1_devdata *dd = ppd->dd;
240 struct hfi1_ctxtdata *rcd;
241 unsigned kctxt_ngroups = 0;
242 u32 base;
243
244 if (dd->rcv_entries.nctxt_extra >
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700245 dd->num_rcv_contexts - dd->first_dyn_alloc_ctxt)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400246 kctxt_ngroups = (dd->rcv_entries.nctxt_extra -
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700247 (dd->num_rcv_contexts - dd->first_dyn_alloc_ctxt));
Jianxin Xiong4dfe7cc2016-10-17 04:19:41 -0700248 rcd = kzalloc_node(sizeof(*rcd), GFP_KERNEL, numa);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400249 if (rcd) {
250 u32 rcvtids, max_entries;
251
Sebastian Sanchez6c63e422015-11-06 20:06:56 -0500252 hfi1_cdbg(PROC, "setting up context %u\n", ctxt);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400253
254 INIT_LIST_HEAD(&rcd->qp_wait_list);
Michael J. Ruhlfe4e74e2017-06-09 16:00:12 -0700255 hfi1_exp_tid_group_init(&rcd->tid_group_list);
256 hfi1_exp_tid_group_init(&rcd->tid_used_list);
257 hfi1_exp_tid_group_init(&rcd->tid_full_list);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400258 rcd->ppd = ppd;
259 rcd->dd = dd;
Michael J. Ruhl8737ce92017-05-04 05:15:15 -0700260 __set_bit(0, rcd->in_use_ctxts);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400261 rcd->ctxt = ctxt;
262 dd->rcd[ctxt] = rcd;
Mitko Haralanov957558c2016-02-03 14:33:40 -0800263 rcd->numa_id = numa;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400264 rcd->rcv_array_groups = dd->rcv_entries.ngroups;
265
Mitko Haralanov463e6eb2016-02-05 11:57:53 -0500266 mutex_init(&rcd->exp_lock);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400267
268 /*
269 * Calculate the context's RcvArray entry starting point.
270 * We do this here because we have to take into account all
271 * the RcvArray entries that previous context would have
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700272 * taken and we have to account for any extra groups assigned
273 * to the static (kernel) or dynamic (vnic/user) contexts.
Mike Marciniszyn77241052015-07-30 15:17:43 -0400274 */
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700275 if (ctxt < dd->first_dyn_alloc_ctxt) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400276 if (ctxt < kctxt_ngroups) {
277 base = ctxt * (dd->rcv_entries.ngroups + 1);
278 rcd->rcv_array_groups++;
Dennis Dalessandroee495ad2017-04-09 10:17:18 -0700279 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400280 base = kctxt_ngroups +
281 (ctxt * dd->rcv_entries.ngroups);
Dennis Dalessandroee495ad2017-04-09 10:17:18 -0700282 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400283 } else {
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700284 u16 ct = ctxt - dd->first_dyn_alloc_ctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400285
286 base = ((dd->n_krcv_queues * dd->rcv_entries.ngroups) +
287 kctxt_ngroups);
288 if (ct < dd->rcv_entries.nctxt_extra) {
289 base += ct * (dd->rcv_entries.ngroups + 1);
290 rcd->rcv_array_groups++;
Dennis Dalessandroee495ad2017-04-09 10:17:18 -0700291 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400292 base += dd->rcv_entries.nctxt_extra +
293 (ct * dd->rcv_entries.ngroups);
Dennis Dalessandroee495ad2017-04-09 10:17:18 -0700294 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400295 }
296 rcd->eager_base = base * dd->rcv_entries.group_size;
297
Mike Marciniszyn77241052015-07-30 15:17:43 -0400298 rcd->rcvhdrq_cnt = rcvhdrcnt;
299 rcd->rcvhdrqentsize = hfi1_hdrq_entsize;
300 /*
301 * Simple Eager buffer allocation: we have already pre-allocated
302 * the number of RcvArray entry groups. Each ctxtdata structure
303 * holds the number of groups for that context.
304 *
305 * To follow CSR requirements and maintain cacheline alignment,
306 * make sure all sizes and bases are multiples of group_size.
307 *
308 * The expected entry count is what is left after assigning
309 * eager.
310 */
311 max_entries = rcd->rcv_array_groups *
312 dd->rcv_entries.group_size;
313 rcvtids = ((max_entries * hfi1_rcvarr_split) / 100);
314 rcd->egrbufs.count = round_down(rcvtids,
315 dd->rcv_entries.group_size);
316 if (rcd->egrbufs.count > MAX_EAGER_ENTRIES) {
317 dd_dev_err(dd, "ctxt%u: requested too many RcvArray entries.\n",
318 rcd->ctxt);
319 rcd->egrbufs.count = MAX_EAGER_ENTRIES;
320 }
Sebastian Sanchez6c63e422015-11-06 20:06:56 -0500321 hfi1_cdbg(PROC,
322 "ctxt%u: max Eager buffer RcvArray entries: %u\n",
323 rcd->ctxt, rcd->egrbufs.count);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400324
325 /*
326 * Allocate array that will hold the eager buffer accounting
327 * data.
328 * This will allocate the maximum possible buffer count based
329 * on the value of the RcvArray split parameter.
330 * The resulting value will be rounded down to the closest
331 * multiple of dd->rcv_entries.group_size.
332 */
Sebastian Sanchezb448bf92017-02-08 05:26:37 -0800333 rcd->egrbufs.buffers = kzalloc_node(
334 rcd->egrbufs.count * sizeof(*rcd->egrbufs.buffers),
335 GFP_KERNEL, numa);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400336 if (!rcd->egrbufs.buffers)
337 goto bail;
Sebastian Sanchezb448bf92017-02-08 05:26:37 -0800338 rcd->egrbufs.rcvtids = kzalloc_node(
339 rcd->egrbufs.count *
340 sizeof(*rcd->egrbufs.rcvtids),
341 GFP_KERNEL, numa);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400342 if (!rcd->egrbufs.rcvtids)
343 goto bail;
344 rcd->egrbufs.size = eager_buffer_size;
345 /*
346 * The size of the buffers programmed into the RcvArray
347 * entries needs to be big enough to handle the highest
348 * MTU supported.
349 */
350 if (rcd->egrbufs.size < hfi1_max_mtu) {
351 rcd->egrbufs.size = __roundup_pow_of_two(hfi1_max_mtu);
Sebastian Sanchez6c63e422015-11-06 20:06:56 -0500352 hfi1_cdbg(PROC,
353 "ctxt%u: eager bufs size too small. Adjusting to %zu\n",
Mike Marciniszyn77241052015-07-30 15:17:43 -0400354 rcd->ctxt, rcd->egrbufs.size);
355 }
356 rcd->egrbufs.rcvtid_size = HFI1_MAX_EAGER_BUFFER_SIZE;
357
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700358 /* Applicable only for statically created kernel contexts */
359 if (ctxt < dd->first_dyn_alloc_ctxt) {
Sebastian Sanchezb448bf92017-02-08 05:26:37 -0800360 rcd->opstats = kzalloc_node(sizeof(*rcd->opstats),
361 GFP_KERNEL, numa);
Alison Schofield806e6e12015-10-12 14:28:36 -0700362 if (!rcd->opstats)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400363 goto bail;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400364 }
Michael J. Ruhlf683c802017-06-09 16:00:19 -0700365
366 hfi1_rcd_init(rcd);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400367 }
368 return rcd;
369bail:
Jakub Pawlak3a6982d2016-09-25 07:42:23 -0700370 dd->rcd[ctxt] = NULL;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400371 kfree(rcd->egrbufs.rcvtids);
372 kfree(rcd->egrbufs.buffers);
373 kfree(rcd);
374 return NULL;
375}
376
377/*
378 * Convert a receive header entry size that to the encoding used in the CSR.
379 *
380 * Return a zero if the given size is invalid.
381 */
382static inline u64 encode_rcv_header_entry_size(u16 size)
383{
384 /* there are only 3 valid receive header entry sizes */
385 if (size == 2)
386 return 1;
387 if (size == 16)
388 return 2;
389 else if (size == 32)
390 return 4;
391 return 0; /* invalid */
392}
393
394/*
395 * Select the largest ccti value over all SLs to determine the intra-
396 * packet gap for the link.
397 *
398 * called with cca_timer_lock held (to protect access to cca_timer
399 * array), and rcu_read_lock() (to protect access to cc_state).
400 */
401void set_link_ipg(struct hfi1_pportdata *ppd)
402{
403 struct hfi1_devdata *dd = ppd->dd;
404 struct cc_state *cc_state;
405 int i;
406 u16 cce, ccti_limit, max_ccti = 0;
407 u16 shift, mult;
408 u64 src;
409 u32 current_egress_rate; /* Mbits /sec */
410 u32 max_pkt_time;
411 /*
412 * max_pkt_time is the maximum packet egress time in units
413 * of the fabric clock period 1/(805 MHz).
414 */
415
416 cc_state = get_cc_state(ppd);
417
Jubin Johnd125a6c2016-02-14 20:19:49 -0800418 if (!cc_state)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400419 /*
420 * This should _never_ happen - rcu_read_lock() is held,
421 * and set_link_ipg() should not be called if cc_state
422 * is NULL.
423 */
424 return;
425
426 for (i = 0; i < OPA_MAX_SLS; i++) {
427 u16 ccti = ppd->cca_timer[i].ccti;
428
429 if (ccti > max_ccti)
430 max_ccti = ccti;
431 }
432
433 ccti_limit = cc_state->cct.ccti_limit;
434 if (max_ccti > ccti_limit)
435 max_ccti = ccti_limit;
436
437 cce = cc_state->cct.entries[max_ccti].entry;
438 shift = (cce & 0xc000) >> 14;
439 mult = (cce & 0x3fff);
440
441 current_egress_rate = active_egress_rate(ppd);
442
443 max_pkt_time = egress_cycles(ppd->ibmaxlen, current_egress_rate);
444
445 src = (max_pkt_time >> shift) * mult;
446
447 src &= SEND_STATIC_RATE_CONTROL_CSR_SRC_RELOAD_SMASK;
448 src <<= SEND_STATIC_RATE_CONTROL_CSR_SRC_RELOAD_SHIFT;
449
450 write_csr(dd, SEND_STATIC_RATE_CONTROL, src);
451}
452
453static enum hrtimer_restart cca_timer_fn(struct hrtimer *t)
454{
455 struct cca_timer *cca_timer;
456 struct hfi1_pportdata *ppd;
457 int sl;
Jubin Johnd35cf7442016-04-14 08:31:53 -0700458 u16 ccti_timer, ccti_min;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400459 struct cc_state *cc_state;
Dean Luickb77d7132015-10-26 10:28:43 -0400460 unsigned long flags;
Jubin Johnd35cf7442016-04-14 08:31:53 -0700461 enum hrtimer_restart ret = HRTIMER_NORESTART;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400462
463 cca_timer = container_of(t, struct cca_timer, hrtimer);
464 ppd = cca_timer->ppd;
465 sl = cca_timer->sl;
466
467 rcu_read_lock();
468
469 cc_state = get_cc_state(ppd);
470
Jubin Johnd125a6c2016-02-14 20:19:49 -0800471 if (!cc_state) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400472 rcu_read_unlock();
473 return HRTIMER_NORESTART;
474 }
475
476 /*
477 * 1) decrement ccti for SL
478 * 2) calculate IPG for link (set_link_ipg())
479 * 3) restart timer, unless ccti is at min value
480 */
481
482 ccti_min = cc_state->cong_setting.entries[sl].ccti_min;
483 ccti_timer = cc_state->cong_setting.entries[sl].ccti_timer;
484
Dean Luickb77d7132015-10-26 10:28:43 -0400485 spin_lock_irqsave(&ppd->cca_timer_lock, flags);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400486
Jubin Johnd35cf7442016-04-14 08:31:53 -0700487 if (cca_timer->ccti > ccti_min) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400488 cca_timer->ccti--;
489 set_link_ipg(ppd);
490 }
491
Jubin Johnd35cf7442016-04-14 08:31:53 -0700492 if (cca_timer->ccti > ccti_min) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400493 unsigned long nsec = 1024 * ccti_timer;
494 /* ccti_timer is in units of 1.024 usec */
495 hrtimer_forward_now(t, ns_to_ktime(nsec));
Jubin Johnd35cf7442016-04-14 08:31:53 -0700496 ret = HRTIMER_RESTART;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400497 }
Jubin Johnd35cf7442016-04-14 08:31:53 -0700498
499 spin_unlock_irqrestore(&ppd->cca_timer_lock, flags);
500 rcu_read_unlock();
501 return ret;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400502}
503
504/*
505 * Common code for initializing the physical port structure.
506 */
507void hfi1_init_pportdata(struct pci_dev *pdev, struct hfi1_pportdata *ppd,
508 struct hfi1_devdata *dd, u8 hw_pidx, u8 port)
509{
Jianxin Xiong8adf71f2016-07-25 13:39:14 -0700510 int i;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400511 uint default_pkey_idx;
Jianxin Xiong8adf71f2016-07-25 13:39:14 -0700512 struct cc_state *cc_state;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400513
514 ppd->dd = dd;
515 ppd->hw_pidx = hw_pidx;
516 ppd->port = port; /* IB port number, not index */
517
518 default_pkey_idx = 1;
519
520 ppd->pkeys[default_pkey_idx] = DEFAULT_P_KEY;
Neel Desai53526502017-04-09 10:16:59 -0700521 ppd->part_enforce |= HFI1_PART_ENFORCE_IN;
522 ppd->part_enforce |= HFI1_PART_ENFORCE_OUT;
523
Mike Marciniszyn77241052015-07-30 15:17:43 -0400524 if (loopback) {
525 hfi1_early_err(&pdev->dev,
526 "Faking data partition 0x8001 in idx %u\n",
527 !default_pkey_idx);
528 ppd->pkeys[!default_pkey_idx] = 0x8001;
529 }
530
531 INIT_WORK(&ppd->link_vc_work, handle_verify_cap);
532 INIT_WORK(&ppd->link_up_work, handle_link_up);
533 INIT_WORK(&ppd->link_down_work, handle_link_down);
534 INIT_WORK(&ppd->freeze_work, handle_freeze);
535 INIT_WORK(&ppd->link_downgrade_work, handle_link_downgrade);
536 INIT_WORK(&ppd->sma_message_work, handle_sma_message);
537 INIT_WORK(&ppd->link_bounce_work, handle_link_bounce);
Dean Luick673b9752016-08-31 07:24:33 -0700538 INIT_DELAYED_WORK(&ppd->start_link_work, handle_start_link);
Jim Snowfb9036d2016-01-11 18:32:21 -0500539 INIT_WORK(&ppd->linkstate_active_work, receive_interrupt_work);
Easwar Hariharan8ebd4cf2016-02-03 14:31:14 -0800540 INIT_WORK(&ppd->qsfp_info.qsfp_work, qsfp_event);
541
Mike Marciniszyn77241052015-07-30 15:17:43 -0400542 mutex_init(&ppd->hls_lock);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400543 spin_lock_init(&ppd->qsfp_info.qsfp_lock);
544
Easwar Hariharan8ebd4cf2016-02-03 14:31:14 -0800545 ppd->qsfp_info.ppd = ppd;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400546 ppd->sm_trap_qp = 0x0;
547 ppd->sa_qp = 0x1;
548
549 ppd->hfi1_wq = NULL;
550
551 spin_lock_init(&ppd->cca_timer_lock);
552
553 for (i = 0; i < OPA_MAX_SLS; i++) {
554 hrtimer_init(&ppd->cca_timer[i].hrtimer, CLOCK_MONOTONIC,
555 HRTIMER_MODE_REL);
556 ppd->cca_timer[i].ppd = ppd;
557 ppd->cca_timer[i].sl = i;
558 ppd->cca_timer[i].ccti = 0;
559 ppd->cca_timer[i].hrtimer.function = cca_timer_fn;
560 }
561
562 ppd->cc_max_table_entries = IB_CC_TABLE_CAP_DEFAULT;
563
564 spin_lock_init(&ppd->cc_state_lock);
565 spin_lock_init(&ppd->cc_log_lock);
Jianxin Xiong8adf71f2016-07-25 13:39:14 -0700566 cc_state = kzalloc(sizeof(*cc_state), GFP_KERNEL);
567 RCU_INIT_POINTER(ppd->cc_state, cc_state);
568 if (!cc_state)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400569 goto bail;
570 return;
571
572bail:
573
574 hfi1_early_err(&pdev->dev,
575 "Congestion Control Agent disabled for port %d\n", port);
576}
577
578/*
579 * Do initialization for device that is only needed on
580 * first detect, not on resets.
581 */
582static int loadtime_init(struct hfi1_devdata *dd)
583{
584 return 0;
585}
586
587/**
588 * init_after_reset - re-initialize after a reset
589 * @dd: the hfi1_ib device
590 *
591 * sanity check at least some of the values after reset, and
592 * ensure no receive or transmit (explicitly, in case reset
593 * failed
594 */
595static int init_after_reset(struct hfi1_devdata *dd)
596{
597 int i;
598
599 /*
600 * Ensure chip does no sends or receives, tail updates, or
601 * pioavail updates while we re-initialize. This is mostly
602 * for the driver data structures, not chip registers.
603 */
604 for (i = 0; i < dd->num_rcv_contexts; i++)
605 hfi1_rcvctrl(dd, HFI1_RCVCTRL_CTXT_DIS |
Michael J. Ruhl22505632017-07-24 07:46:06 -0700606 HFI1_RCVCTRL_INTRAVAIL_DIS |
607 HFI1_RCVCTRL_TAILUPD_DIS, dd->rcd[i]);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400608 pio_send_control(dd, PSC_GLOBAL_DISABLE);
609 for (i = 0; i < dd->num_send_contexts; i++)
610 sc_disable(dd->send_contexts[i].sc);
611
612 return 0;
613}
614
615static void enable_chip(struct hfi1_devdata *dd)
616{
617 u32 rcvmask;
Michael J. Ruhle6f76222017-07-24 07:45:55 -0700618 u16 i;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400619
620 /* enable PIO send */
621 pio_send_control(dd, PSC_GLOBAL_ENABLE);
622
623 /*
624 * Enable kernel ctxts' receive and receive interrupt.
625 * Other ctxts done as user opens and initializes them.
626 */
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700627 for (i = 0; i < dd->first_dyn_alloc_ctxt; ++i) {
Mitko Haralanov566c1572016-02-03 14:32:49 -0800628 rcvmask = HFI1_RCVCTRL_CTXT_ENB | HFI1_RCVCTRL_INTRAVAIL_ENB;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400629 rcvmask |= HFI1_CAP_KGET_MASK(dd->rcd[i]->flags, DMA_RTAIL) ?
630 HFI1_RCVCTRL_TAILUPD_ENB : HFI1_RCVCTRL_TAILUPD_DIS;
631 if (!HFI1_CAP_KGET_MASK(dd->rcd[i]->flags, MULTI_PKT_EGR))
632 rcvmask |= HFI1_RCVCTRL_ONE_PKT_EGR_ENB;
633 if (HFI1_CAP_KGET_MASK(dd->rcd[i]->flags, NODROP_RHQ_FULL))
634 rcvmask |= HFI1_RCVCTRL_NO_RHQ_DROP_ENB;
635 if (HFI1_CAP_KGET_MASK(dd->rcd[i]->flags, NODROP_EGR_FULL))
636 rcvmask |= HFI1_RCVCTRL_NO_EGR_DROP_ENB;
Michael J. Ruhl22505632017-07-24 07:46:06 -0700637 hfi1_rcvctrl(dd, rcvmask, dd->rcd[i]);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400638 sc_enable(dd->rcd[i]->sc);
639 }
640}
641
642/**
643 * create_workqueues - create per port workqueues
644 * @dd: the hfi1_ib device
645 */
646static int create_workqueues(struct hfi1_devdata *dd)
647{
648 int pidx;
649 struct hfi1_pportdata *ppd;
650
651 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
652 ppd = dd->pport + pidx;
653 if (!ppd->hfi1_wq) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400654 ppd->hfi1_wq =
Mike Marciniszyn0a226ed2015-11-09 19:13:58 -0500655 alloc_workqueue(
656 "hfi%d_%d",
657 WQ_SYSFS | WQ_HIGHPRI | WQ_CPU_INTENSIVE,
Mike Marciniszyndd1ed102017-05-04 05:14:10 -0700658 HFI1_MAX_ACTIVE_WORKQUEUE_ENTRIES,
Mike Marciniszyn0a226ed2015-11-09 19:13:58 -0500659 dd->unit, pidx);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400660 if (!ppd->hfi1_wq)
661 goto wq_error;
662 }
Sebastian Sanchez71d47002017-07-29 08:43:49 -0700663 if (!ppd->link_wq) {
664 /*
665 * Make the link workqueue single-threaded to enforce
666 * serialization.
667 */
668 ppd->link_wq =
669 alloc_workqueue(
670 "hfi_link_%d_%d",
671 WQ_SYSFS | WQ_MEM_RECLAIM | WQ_UNBOUND,
672 1, /* max_active */
673 dd->unit, pidx);
674 if (!ppd->link_wq)
675 goto wq_error;
676 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400677 }
678 return 0;
679wq_error:
Mike Marciniszyn0a226ed2015-11-09 19:13:58 -0500680 pr_err("alloc_workqueue failed for port %d\n", pidx + 1);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400681 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
682 ppd = dd->pport + pidx;
683 if (ppd->hfi1_wq) {
684 destroy_workqueue(ppd->hfi1_wq);
685 ppd->hfi1_wq = NULL;
686 }
Sebastian Sanchez71d47002017-07-29 08:43:49 -0700687 if (ppd->link_wq) {
688 destroy_workqueue(ppd->link_wq);
689 ppd->link_wq = NULL;
690 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400691 }
692 return -ENOMEM;
693}
694
695/**
696 * hfi1_init - do the actual initialization sequence on the chip
697 * @dd: the hfi1_ib device
698 * @reinit: re-initializing, so don't allocate new memory
699 *
700 * Do the actual initialization sequence on the chip. This is done
701 * both from the init routine called from the PCI infrastructure, and
702 * when we reset the chip, or detect that it was reset internally,
703 * or it's administratively re-enabled.
704 *
705 * Memory allocation here and in called routines is only done in
706 * the first case (reinit == 0). We have to be careful, because even
707 * without memory allocation, we need to re-write all the chip registers
708 * TIDs, etc. after the reset or enable has completed.
709 */
710int hfi1_init(struct hfi1_devdata *dd, int reinit)
711{
712 int ret = 0, pidx, lastfail = 0;
Michael J. Ruhle6f76222017-07-24 07:45:55 -0700713 unsigned long len;
714 u16 i;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400715 struct hfi1_ctxtdata *rcd;
716 struct hfi1_pportdata *ppd;
717
718 /* Set up recv low level handlers */
719 dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_EXPECTED] =
720 kdeth_process_expected;
721 dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_EAGER] =
722 kdeth_process_eager;
723 dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_IB] = process_receive_ib;
724 dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_ERROR] =
725 process_receive_error;
726 dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_BYPASS] =
727 process_receive_bypass;
728 dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_INVALID5] =
729 process_receive_invalid;
730 dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_INVALID6] =
731 process_receive_invalid;
732 dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_INVALID7] =
733 process_receive_invalid;
734 dd->rhf_rcv_function_map = dd->normal_rhf_rcv_functions;
735
736 /* Set up send low level handlers */
737 dd->process_pio_send = hfi1_verbs_send_pio;
738 dd->process_dma_send = hfi1_verbs_send_dma;
739 dd->pio_inline_send = pio_copy;
Vishwanathapura, Niranjana64551ed2017-04-12 20:29:30 -0700740 dd->process_vnic_dma_send = hfi1_vnic_send_dma;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400741
Mike Marciniszyn995deaf2015-11-16 21:59:29 -0500742 if (is_ax(dd)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400743 atomic_set(&dd->drop_packet, DROP_PACKET_ON);
744 dd->do_drop = 1;
745 } else {
746 atomic_set(&dd->drop_packet, DROP_PACKET_OFF);
747 dd->do_drop = 0;
748 }
749
750 /* make sure the link is not "up" */
751 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
752 ppd = dd->pport + pidx;
753 ppd->linkup = 0;
754 }
755
756 if (reinit)
757 ret = init_after_reset(dd);
758 else
759 ret = loadtime_init(dd);
760 if (ret)
761 goto done;
762
Mark F. Brown46b010d2015-11-09 19:18:20 -0500763 /* allocate dummy tail memory for all receive contexts */
764 dd->rcvhdrtail_dummy_kvaddr = dma_zalloc_coherent(
765 &dd->pcidev->dev, sizeof(u64),
Tymoteusz Kielan60368182016-09-06 04:35:54 -0700766 &dd->rcvhdrtail_dummy_dma,
Mark F. Brown46b010d2015-11-09 19:18:20 -0500767 GFP_KERNEL);
768
769 if (!dd->rcvhdrtail_dummy_kvaddr) {
770 dd_dev_err(dd, "cannot allocate dummy tail memory\n");
771 ret = -ENOMEM;
772 goto done;
773 }
774
Mike Marciniszyn77241052015-07-30 15:17:43 -0400775 /* dd->rcd can be NULL if early initialization failed */
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700776 for (i = 0; dd->rcd && i < dd->first_dyn_alloc_ctxt; ++i) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400777 /*
778 * Set up the (kernel) rcvhdr queue and egr TIDs. If doing
779 * re-init, the simplest way to handle this is to free
780 * existing, and re-allocate.
781 * Need to re-create rest of ctxt 0 ctxtdata as well.
782 */
783 rcd = dd->rcd[i];
784 if (!rcd)
785 continue;
786
787 rcd->do_interrupt = &handle_receive_interrupt;
788
789 lastfail = hfi1_create_rcvhdrq(dd, rcd);
790 if (!lastfail)
791 lastfail = hfi1_setup_eagerbufs(rcd);
Ashutosh Dixit39239792016-05-12 10:24:00 -0700792 if (lastfail) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400793 dd_dev_err(dd,
Jubin John17fb4f22016-02-14 20:21:52 -0800794 "failed to allocate kernel ctxt's rcvhdrq and/or egr bufs\n");
Ashutosh Dixit39239792016-05-12 10:24:00 -0700795 ret = lastfail;
796 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400797 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400798
799 /* Allocate enough memory for user event notification. */
Amitoj Kaur Chawla84449912016-03-04 22:30:43 +0530800 len = PAGE_ALIGN(dd->chip_rcv_contexts * HFI1_MAX_SHARED_CTXTS *
801 sizeof(*dd->events));
Mike Marciniszyn77241052015-07-30 15:17:43 -0400802 dd->events = vmalloc_user(len);
803 if (!dd->events)
804 dd_dev_err(dd, "Failed to allocate user events page\n");
805 /*
806 * Allocate a page for device and port status.
807 * Page will be shared amongst all user processes.
808 */
809 dd->status = vmalloc_user(PAGE_SIZE);
810 if (!dd->status)
811 dd_dev_err(dd, "Failed to allocate dev status page\n");
812 else
813 dd->freezelen = PAGE_SIZE - (sizeof(*dd->status) -
814 sizeof(dd->status->freezemsg));
815 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
816 ppd = dd->pport + pidx;
817 if (dd->status)
818 /* Currently, we only have one port */
819 ppd->statusp = &dd->status->port;
820
821 set_mtu(ppd);
822 }
823
824 /* enable chip even if we have an error, so we can debug cause */
825 enable_chip(dd);
826
Mike Marciniszyn77241052015-07-30 15:17:43 -0400827done:
828 /*
829 * Set status even if port serdes is not initialized
830 * so that diags will work.
831 */
832 if (dd->status)
833 dd->status->dev |= HFI1_STATUS_CHIP_PRESENT |
834 HFI1_STATUS_INITTED;
835 if (!ret) {
836 /* enable all interrupts from the chip */
837 set_intr_state(dd, 1);
838
839 /* chip is OK for user apps; mark it as initialized */
840 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
841 ppd = dd->pport + pidx;
842
Jubin John4d114fd2016-02-14 20:21:43 -0800843 /*
844 * start the serdes - must be after interrupts are
845 * enabled so we are notified when the link goes up
Mike Marciniszyn77241052015-07-30 15:17:43 -0400846 */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400847 lastfail = bringup_serdes(ppd);
848 if (lastfail)
849 dd_dev_info(dd,
Jubin John17fb4f22016-02-14 20:21:52 -0800850 "Failed to bring up port %u\n",
851 ppd->port);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400852
853 /*
854 * Set status even if port serdes is not initialized
855 * so that diags will work.
856 */
857 if (ppd->statusp)
858 *ppd->statusp |= HFI1_STATUS_CHIP_PRESENT |
859 HFI1_STATUS_INITTED;
860 if (!ppd->link_speed_enabled)
861 continue;
862 }
863 }
864
865 /* if ret is non-zero, we probably should do some cleanup here... */
866 return ret;
867}
868
869static inline struct hfi1_devdata *__hfi1_lookup(int unit)
870{
871 return idr_find(&hfi1_unit_table, unit);
872}
873
874struct hfi1_devdata *hfi1_lookup(int unit)
875{
876 struct hfi1_devdata *dd;
877 unsigned long flags;
878
879 spin_lock_irqsave(&hfi1_devs_lock, flags);
880 dd = __hfi1_lookup(unit);
881 spin_unlock_irqrestore(&hfi1_devs_lock, flags);
882
883 return dd;
884}
885
886/*
887 * Stop the timers during unit shutdown, or after an error late
888 * in initialization.
889 */
890static void stop_timers(struct hfi1_devdata *dd)
891{
892 struct hfi1_pportdata *ppd;
893 int pidx;
894
895 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
896 ppd = dd->pport + pidx;
897 if (ppd->led_override_timer.data) {
898 del_timer_sync(&ppd->led_override_timer);
899 atomic_set(&ppd->led_override_timer_active, 0);
900 }
901 }
902}
903
904/**
905 * shutdown_device - shut down a device
906 * @dd: the hfi1_ib device
907 *
908 * This is called to make the device quiet when we are about to
909 * unload the driver, and also when the device is administratively
910 * disabled. It does not free any data structures.
911 * Everything it does has to be setup again by hfi1_init(dd, 1)
912 */
913static void shutdown_device(struct hfi1_devdata *dd)
914{
915 struct hfi1_pportdata *ppd;
916 unsigned pidx;
917 int i;
918
919 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
920 ppd = dd->pport + pidx;
921
922 ppd->linkup = 0;
923 if (ppd->statusp)
924 *ppd->statusp &= ~(HFI1_STATUS_IB_CONF |
925 HFI1_STATUS_IB_READY);
926 }
927 dd->flags &= ~HFI1_INITTED;
928
929 /* mask interrupts, but not errors */
930 set_intr_state(dd, 0);
931
932 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
933 ppd = dd->pport + pidx;
934 for (i = 0; i < dd->num_rcv_contexts; i++)
935 hfi1_rcvctrl(dd, HFI1_RCVCTRL_TAILUPD_DIS |
Michael J. Ruhl22505632017-07-24 07:46:06 -0700936 HFI1_RCVCTRL_CTXT_DIS |
937 HFI1_RCVCTRL_INTRAVAIL_DIS |
938 HFI1_RCVCTRL_PKEY_DIS |
939 HFI1_RCVCTRL_ONE_PKT_EGR_DIS, dd->rcd[i]);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400940 /*
941 * Gracefully stop all sends allowing any in progress to
942 * trickle out first.
943 */
944 for (i = 0; i < dd->num_send_contexts; i++)
945 sc_flush(dd->send_contexts[i].sc);
946 }
947
948 /*
949 * Enough for anything that's going to trickle out to have actually
950 * done so.
951 */
952 udelay(20);
953
954 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
955 ppd = dd->pport + pidx;
956
957 /* disable all contexts */
958 for (i = 0; i < dd->num_send_contexts; i++)
959 sc_disable(dd->send_contexts[i].sc);
960 /* disable the send device */
961 pio_send_control(dd, PSC_GLOBAL_DISABLE);
962
Easwar Hariharan91ab4ed2016-02-03 14:35:57 -0800963 shutdown_led_override(ppd);
964
Mike Marciniszyn77241052015-07-30 15:17:43 -0400965 /*
966 * Clear SerdesEnable.
967 * We can't count on interrupts since we are stopping.
968 */
969 hfi1_quiet_serdes(ppd);
970
971 if (ppd->hfi1_wq) {
972 destroy_workqueue(ppd->hfi1_wq);
973 ppd->hfi1_wq = NULL;
974 }
Sebastian Sanchez71d47002017-07-29 08:43:49 -0700975 if (ppd->link_wq) {
976 destroy_workqueue(ppd->link_wq);
977 ppd->link_wq = NULL;
978 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400979 }
980 sdma_exit(dd);
981}
982
983/**
984 * hfi1_free_ctxtdata - free a context's allocated data
985 * @dd: the hfi1_ib device
986 * @rcd: the ctxtdata structure
987 *
988 * free up any allocated data for a context
Mike Marciniszyn77241052015-07-30 15:17:43 -0400989 * It should never change any chip state, or global driver state.
990 */
991void hfi1_free_ctxtdata(struct hfi1_devdata *dd, struct hfi1_ctxtdata *rcd)
992{
Michael J. Ruhlf683c802017-06-09 16:00:19 -0700993 u32 e;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400994
995 if (!rcd)
996 return;
997
998 if (rcd->rcvhdrq) {
999 dma_free_coherent(&dd->pcidev->dev, rcd->rcvhdrq_size,
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001000 rcd->rcvhdrq, rcd->rcvhdrq_dma);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001001 rcd->rcvhdrq = NULL;
1002 if (rcd->rcvhdrtail_kvaddr) {
1003 dma_free_coherent(&dd->pcidev->dev, PAGE_SIZE,
1004 (void *)rcd->rcvhdrtail_kvaddr,
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001005 rcd->rcvhdrqtailaddr_dma);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001006 rcd->rcvhdrtail_kvaddr = NULL;
1007 }
1008 }
1009
1010 /* all the RcvArray entries should have been cleared by now */
1011 kfree(rcd->egrbufs.rcvtids);
Michael J. Ruhlf683c802017-06-09 16:00:19 -07001012 rcd->egrbufs.rcvtids = NULL;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001013
1014 for (e = 0; e < rcd->egrbufs.alloced; e++) {
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001015 if (rcd->egrbufs.buffers[e].dma)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001016 dma_free_coherent(&dd->pcidev->dev,
1017 rcd->egrbufs.buffers[e].len,
1018 rcd->egrbufs.buffers[e].addr,
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001019 rcd->egrbufs.buffers[e].dma);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001020 }
1021 kfree(rcd->egrbufs.buffers);
Michael J. Ruhlf683c802017-06-09 16:00:19 -07001022 rcd->egrbufs.alloced = 0;
1023 rcd->egrbufs.buffers = NULL;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001024
1025 sc_free(rcd->sc);
Michael J. Ruhlf683c802017-06-09 16:00:19 -07001026 rcd->sc = NULL;
1027
Mike Marciniszyn77241052015-07-30 15:17:43 -04001028 vfree(rcd->subctxt_uregbase);
1029 vfree(rcd->subctxt_rcvegrbuf);
1030 vfree(rcd->subctxt_rcvhdr_base);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001031 kfree(rcd->opstats);
Michael J. Ruhlf683c802017-06-09 16:00:19 -07001032
1033 rcd->subctxt_uregbase = NULL;
1034 rcd->subctxt_rcvegrbuf = NULL;
1035 rcd->subctxt_rcvhdr_base = NULL;
1036 rcd->opstats = NULL;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001037}
1038
Dean Luick78eb1292016-03-05 08:49:45 -08001039/*
1040 * Release our hold on the shared asic data. If we are the last one,
Dean Luickdba715f2016-07-06 17:28:52 -04001041 * return the structure to be finalized outside the lock. Must be
1042 * holding hfi1_devs_lock.
Dean Luick78eb1292016-03-05 08:49:45 -08001043 */
Dean Luickdba715f2016-07-06 17:28:52 -04001044static struct hfi1_asic_data *release_asic_data(struct hfi1_devdata *dd)
Dean Luick78eb1292016-03-05 08:49:45 -08001045{
Dean Luickdba715f2016-07-06 17:28:52 -04001046 struct hfi1_asic_data *ad;
Dean Luick78eb1292016-03-05 08:49:45 -08001047 int other;
1048
1049 if (!dd->asic_data)
Dean Luickdba715f2016-07-06 17:28:52 -04001050 return NULL;
Dean Luick78eb1292016-03-05 08:49:45 -08001051 dd->asic_data->dds[dd->hfi1_id] = NULL;
1052 other = dd->hfi1_id ? 0 : 1;
Dean Luickdba715f2016-07-06 17:28:52 -04001053 ad = dd->asic_data;
Dean Luick78eb1292016-03-05 08:49:45 -08001054 dd->asic_data = NULL;
Dean Luickdba715f2016-07-06 17:28:52 -04001055 /* return NULL if the other dd still has a link */
1056 return ad->dds[other] ? NULL : ad;
1057}
1058
1059static void finalize_asic_data(struct hfi1_devdata *dd,
1060 struct hfi1_asic_data *ad)
1061{
1062 clean_up_i2c(dd, ad);
1063 kfree(ad);
Dean Luick78eb1292016-03-05 08:49:45 -08001064}
1065
Dennis Dalessandroe11ffbd2016-05-19 05:26:44 -07001066static void __hfi1_free_devdata(struct kobject *kobj)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001067{
Dennis Dalessandroe11ffbd2016-05-19 05:26:44 -07001068 struct hfi1_devdata *dd =
1069 container_of(kobj, struct hfi1_devdata, kobj);
Dean Luickdba715f2016-07-06 17:28:52 -04001070 struct hfi1_asic_data *ad;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001071 unsigned long flags;
1072
1073 spin_lock_irqsave(&hfi1_devs_lock, flags);
1074 idr_remove(&hfi1_unit_table, dd->unit);
1075 list_del(&dd->list);
Dean Luickdba715f2016-07-06 17:28:52 -04001076 ad = release_asic_data(dd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001077 spin_unlock_irqrestore(&hfi1_devs_lock, flags);
Dean Luickdba715f2016-07-06 17:28:52 -04001078 if (ad)
1079 finalize_asic_data(dd, ad);
Easwar Hariharanc3838b32016-02-09 14:29:13 -08001080 free_platform_config(dd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001081 rcu_barrier(); /* wait for rcu callbacks to complete */
1082 free_percpu(dd->int_counter);
1083 free_percpu(dd->rcv_limit);
Vennila Megavannan89abfc82016-02-03 14:34:07 -08001084 free_percpu(dd->send_schedule);
Jubin Johnea0e4ce2016-04-20 06:05:24 -07001085 rvt_dealloc_device(&dd->verbs_dev.rdi);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001086}
1087
Dennis Dalessandroe11ffbd2016-05-19 05:26:44 -07001088static struct kobj_type hfi1_devdata_type = {
1089 .release = __hfi1_free_devdata,
1090};
1091
1092void hfi1_free_devdata(struct hfi1_devdata *dd)
1093{
1094 kobject_put(&dd->kobj);
1095}
1096
Mike Marciniszyn77241052015-07-30 15:17:43 -04001097/*
1098 * Allocate our primary per-unit data structure. Must be done via verbs
1099 * allocator, because the verbs cleanup process both does cleanup and
1100 * free of the data structure.
1101 * "extra" is for chip-specific data.
1102 *
1103 * Use the idr mechanism to get a unit number for this unit.
1104 */
1105struct hfi1_devdata *hfi1_alloc_devdata(struct pci_dev *pdev, size_t extra)
1106{
1107 unsigned long flags;
1108 struct hfi1_devdata *dd;
Dennis Dalessandro7af6d002016-01-19 14:44:06 -08001109 int ret, nports;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001110
Dennis Dalessandro7af6d002016-01-19 14:44:06 -08001111 /* extra is * number of ports */
1112 nports = extra / sizeof(struct hfi1_pportdata);
1113
1114 dd = (struct hfi1_devdata *)rvt_alloc_device(sizeof(*dd) + extra,
1115 nports);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001116 if (!dd)
1117 return ERR_PTR(-ENOMEM);
Dennis Dalessandro7af6d002016-01-19 14:44:06 -08001118 dd->num_pports = nports;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001119 dd->pport = (struct hfi1_pportdata *)(dd + 1);
1120
1121 INIT_LIST_HEAD(&dd->list);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001122 idr_preload(GFP_KERNEL);
1123 spin_lock_irqsave(&hfi1_devs_lock, flags);
1124
1125 ret = idr_alloc(&hfi1_unit_table, dd, 0, 0, GFP_NOWAIT);
1126 if (ret >= 0) {
1127 dd->unit = ret;
1128 list_add(&dd->list, &hfi1_dev_list);
1129 }
1130
1131 spin_unlock_irqrestore(&hfi1_devs_lock, flags);
1132 idr_preload_end();
1133
1134 if (ret < 0) {
1135 hfi1_early_err(&pdev->dev,
1136 "Could not allocate unit ID: error %d\n", -ret);
1137 goto bail;
1138 }
1139 /*
1140 * Initialize all locks for the device. This needs to be as early as
1141 * possible so locks are usable.
1142 */
1143 spin_lock_init(&dd->sc_lock);
1144 spin_lock_init(&dd->sendctrl_lock);
1145 spin_lock_init(&dd->rcvctrl_lock);
1146 spin_lock_init(&dd->uctxt_lock);
1147 spin_lock_init(&dd->hfi1_diag_trans_lock);
1148 spin_lock_init(&dd->sc_init_lock);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001149 spin_lock_init(&dd->dc8051_memlock);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001150 seqlock_init(&dd->sc2vl_lock);
1151 spin_lock_init(&dd->sde_map_lock);
Jubin John35f6bef2016-02-14 12:46:10 -08001152 spin_lock_init(&dd->pio_map_lock);
Tadeusz Struk22546b72017-04-28 10:40:02 -07001153 mutex_init(&dd->dc8051_lock);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001154 init_waitqueue_head(&dd->event_queue);
1155
1156 dd->int_counter = alloc_percpu(u64);
1157 if (!dd->int_counter) {
1158 ret = -ENOMEM;
1159 hfi1_early_err(&pdev->dev,
1160 "Could not allocate per-cpu int_counter\n");
1161 goto bail;
1162 }
1163
1164 dd->rcv_limit = alloc_percpu(u64);
1165 if (!dd->rcv_limit) {
1166 ret = -ENOMEM;
1167 hfi1_early_err(&pdev->dev,
1168 "Could not allocate per-cpu rcv_limit\n");
1169 goto bail;
1170 }
1171
Vennila Megavannan89abfc82016-02-03 14:34:07 -08001172 dd->send_schedule = alloc_percpu(u64);
1173 if (!dd->send_schedule) {
1174 ret = -ENOMEM;
1175 hfi1_early_err(&pdev->dev,
1176 "Could not allocate per-cpu int_counter\n");
1177 goto bail;
1178 }
1179
Mike Marciniszyn77241052015-07-30 15:17:43 -04001180 if (!hfi1_cpulist_count) {
1181 u32 count = num_online_cpus();
1182
Shraddha Barke314fcc02015-10-09 21:03:26 +05301183 hfi1_cpulist = kcalloc(BITS_TO_LONGS(count), sizeof(long),
1184 GFP_KERNEL);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001185 if (hfi1_cpulist)
1186 hfi1_cpulist_count = count;
1187 else
1188 hfi1_early_err(
1189 &pdev->dev,
1190 "Could not alloc cpulist info, cpu affinity might be wrong\n");
1191 }
Dennis Dalessandroe11ffbd2016-05-19 05:26:44 -07001192 kobject_init(&dd->kobj, &hfi1_devdata_type);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001193 return dd;
1194
1195bail:
1196 if (!list_empty(&dd->list))
1197 list_del_init(&dd->list);
Jubin Johnea0e4ce2016-04-20 06:05:24 -07001198 rvt_dealloc_device(&dd->verbs_dev.rdi);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001199 return ERR_PTR(ret);
1200}
1201
1202/*
1203 * Called from freeze mode handlers, and from PCI error
1204 * reporting code. Should be paranoid about state of
1205 * system and data structures.
1206 */
1207void hfi1_disable_after_error(struct hfi1_devdata *dd)
1208{
1209 if (dd->flags & HFI1_INITTED) {
1210 u32 pidx;
1211
1212 dd->flags &= ~HFI1_INITTED;
1213 if (dd->pport)
1214 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
1215 struct hfi1_pportdata *ppd;
1216
1217 ppd = dd->pport + pidx;
1218 if (dd->flags & HFI1_PRESENT)
1219 set_link_state(ppd, HLS_DN_DISABLE);
1220
1221 if (ppd->statusp)
1222 *ppd->statusp &= ~HFI1_STATUS_IB_READY;
1223 }
1224 }
1225
1226 /*
1227 * Mark as having had an error for driver, and also
1228 * for /sys and status word mapped to user programs.
1229 * This marks unit as not usable, until reset.
1230 */
1231 if (dd->status)
1232 dd->status->dev |= HFI1_STATUS_HWERROR;
1233}
1234
1235static void remove_one(struct pci_dev *);
1236static int init_one(struct pci_dev *, const struct pci_device_id *);
1237
1238#define DRIVER_LOAD_MSG "Intel " DRIVER_NAME " loaded: "
1239#define PFX DRIVER_NAME ": "
1240
Sebastian Sanchezd6373012016-07-25 07:54:48 -07001241const struct pci_device_id hfi1_pci_tbl[] = {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001242 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL0) },
1243 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL1) },
1244 { 0, }
1245};
1246
1247MODULE_DEVICE_TABLE(pci, hfi1_pci_tbl);
1248
1249static struct pci_driver hfi1_pci_driver = {
1250 .name = DRIVER_NAME,
1251 .probe = init_one,
1252 .remove = remove_one,
1253 .id_table = hfi1_pci_tbl,
1254 .err_handler = &hfi1_pci_err_handler,
1255};
1256
1257static void __init compute_krcvqs(void)
1258{
1259 int i;
1260
1261 for (i = 0; i < krcvqsset; i++)
1262 n_krcvqs += krcvqs[i];
1263}
1264
1265/*
1266 * Do all the generic driver unit- and chip-independent memory
1267 * allocation and initialization.
1268 */
1269static int __init hfi1_mod_init(void)
1270{
1271 int ret;
1272
1273 ret = dev_init();
1274 if (ret)
1275 goto bail;
1276
Sebastian Sanchezd6373012016-07-25 07:54:48 -07001277 ret = node_affinity_init();
1278 if (ret)
1279 goto bail;
Dennis Dalessandro41973442016-07-25 07:52:36 -07001280
Mike Marciniszyn77241052015-07-30 15:17:43 -04001281 /* validate max MTU before any devices start */
1282 if (!valid_opa_max_mtu(hfi1_max_mtu)) {
1283 pr_err("Invalid max_mtu 0x%x, using 0x%x instead\n",
1284 hfi1_max_mtu, HFI1_DEFAULT_MAX_MTU);
1285 hfi1_max_mtu = HFI1_DEFAULT_MAX_MTU;
1286 }
1287 /* valid CUs run from 1-128 in powers of 2 */
1288 if (hfi1_cu > 128 || !is_power_of_2(hfi1_cu))
1289 hfi1_cu = 1;
1290 /* valid credit return threshold is 0-100, variable is unsigned */
1291 if (user_credit_return_threshold > 100)
1292 user_credit_return_threshold = 100;
1293
1294 compute_krcvqs();
Jubin John4d114fd2016-02-14 20:21:43 -08001295 /*
1296 * sanitize receive interrupt count, time must wait until after
1297 * the hardware type is known
1298 */
Mike Marciniszyn77241052015-07-30 15:17:43 -04001299 if (rcv_intr_count > RCV_HDR_HEAD_COUNTER_MASK)
1300 rcv_intr_count = RCV_HDR_HEAD_COUNTER_MASK;
1301 /* reject invalid combinations */
1302 if (rcv_intr_count == 0 && rcv_intr_timeout == 0) {
1303 pr_err("Invalid mode: both receive interrupt count and available timeout are zero - setting interrupt count to 1\n");
1304 rcv_intr_count = 1;
1305 }
1306 if (rcv_intr_count > 1 && rcv_intr_timeout == 0) {
1307 /*
1308 * Avoid indefinite packet delivery by requiring a timeout
1309 * if count is > 1.
1310 */
1311 pr_err("Invalid mode: receive interrupt count greater than 1 and available timeout is zero - setting available timeout to 1\n");
1312 rcv_intr_timeout = 1;
1313 }
1314 if (rcv_intr_dynamic && !(rcv_intr_count > 1 && rcv_intr_timeout > 0)) {
1315 /*
1316 * The dynamic algorithm expects a non-zero timeout
1317 * and a count > 1.
1318 */
1319 pr_err("Invalid mode: dynamic receive interrupt mitigation with invalid count and timeout - turning dynamic off\n");
1320 rcv_intr_dynamic = 0;
1321 }
1322
1323 /* sanitize link CRC options */
1324 link_crc_mask &= SUPPORTED_CRCS;
1325
1326 /*
1327 * These must be called before the driver is registered with
1328 * the PCI subsystem.
1329 */
1330 idr_init(&hfi1_unit_table);
1331
1332 hfi1_dbg_init();
Dean Luick528ee9f2016-03-05 08:50:43 -08001333 ret = hfi1_wss_init();
1334 if (ret < 0)
1335 goto bail_wss;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001336 ret = pci_register_driver(&hfi1_pci_driver);
1337 if (ret < 0) {
1338 pr_err("Unable to register driver: error %d\n", -ret);
1339 goto bail_dev;
1340 }
1341 goto bail; /* all OK */
1342
1343bail_dev:
Dean Luick528ee9f2016-03-05 08:50:43 -08001344 hfi1_wss_exit();
1345bail_wss:
Mike Marciniszyn77241052015-07-30 15:17:43 -04001346 hfi1_dbg_exit();
1347 idr_destroy(&hfi1_unit_table);
1348 dev_cleanup();
1349bail:
1350 return ret;
1351}
1352
1353module_init(hfi1_mod_init);
1354
1355/*
1356 * Do the non-unit driver cleanup, memory free, etc. at unload.
1357 */
1358static void __exit hfi1_mod_cleanup(void)
1359{
1360 pci_unregister_driver(&hfi1_pci_driver);
Dennis Dalessandro41973442016-07-25 07:52:36 -07001361 node_affinity_destroy();
Dean Luick528ee9f2016-03-05 08:50:43 -08001362 hfi1_wss_exit();
Mike Marciniszyn77241052015-07-30 15:17:43 -04001363 hfi1_dbg_exit();
1364 hfi1_cpulist_count = 0;
1365 kfree(hfi1_cpulist);
1366
1367 idr_destroy(&hfi1_unit_table);
1368 dispose_firmware(); /* asymmetric with obtain_firmware() */
1369 dev_cleanup();
1370}
1371
1372module_exit(hfi1_mod_cleanup);
1373
1374/* this can only be called after a successful initialization */
1375static void cleanup_device_data(struct hfi1_devdata *dd)
1376{
1377 int ctxt;
1378 int pidx;
1379 struct hfi1_ctxtdata **tmp;
1380 unsigned long flags;
1381
1382 /* users can't do anything more with chip */
1383 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
1384 struct hfi1_pportdata *ppd = &dd->pport[pidx];
1385 struct cc_state *cc_state;
1386 int i;
1387
1388 if (ppd->statusp)
1389 *ppd->statusp &= ~HFI1_STATUS_CHIP_PRESENT;
1390
1391 for (i = 0; i < OPA_MAX_SLS; i++)
1392 hrtimer_cancel(&ppd->cca_timer[i].hrtimer);
1393
1394 spin_lock(&ppd->cc_state_lock);
Jianxin Xiong8adf71f2016-07-25 13:39:14 -07001395 cc_state = get_cc_state_protected(ppd);
Muhammad Falak R Wanieea57072016-05-01 18:05:31 +05301396 RCU_INIT_POINTER(ppd->cc_state, NULL);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001397 spin_unlock(&ppd->cc_state_lock);
1398
1399 if (cc_state)
Wei Yongjun476d95b2016-08-10 03:14:04 +00001400 kfree_rcu(cc_state, rcu);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001401 }
1402
1403 free_credit_return(dd);
1404
1405 /*
1406 * Free any resources still in use (usually just kernel contexts)
1407 * at unload; we do for ctxtcnt, because that's what we allocate.
1408 * We acquire lock to be really paranoid that rcd isn't being
1409 * accessed from some interrupt-related code (that should not happen,
1410 * but best to be sure).
1411 */
1412 spin_lock_irqsave(&dd->uctxt_lock, flags);
1413 tmp = dd->rcd;
1414 dd->rcd = NULL;
1415 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
Mark F. Brown46b010d2015-11-09 19:18:20 -05001416
1417 if (dd->rcvhdrtail_dummy_kvaddr) {
1418 dma_free_coherent(&dd->pcidev->dev, sizeof(u64),
1419 (void *)dd->rcvhdrtail_dummy_kvaddr,
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001420 dd->rcvhdrtail_dummy_dma);
Dan Carpentera8b7da52016-05-28 08:01:20 +03001421 dd->rcvhdrtail_dummy_kvaddr = NULL;
Mark F. Brown46b010d2015-11-09 19:18:20 -05001422 }
1423
Mike Marciniszyn77241052015-07-30 15:17:43 -04001424 for (ctxt = 0; tmp && ctxt < dd->num_rcv_contexts; ctxt++) {
1425 struct hfi1_ctxtdata *rcd = tmp[ctxt];
1426
1427 tmp[ctxt] = NULL; /* debugging paranoia */
1428 if (rcd) {
1429 hfi1_clear_tids(rcd);
Michael J. Ruhlf683c802017-06-09 16:00:19 -07001430 hfi1_rcd_put(rcd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001431 }
1432 }
1433 kfree(tmp);
Jubin John35f6bef2016-02-14 12:46:10 -08001434 free_pio_map(dd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001435 /* must follow rcv context free - need to remove rcv's hooks */
1436 for (ctxt = 0; ctxt < dd->num_send_contexts; ctxt++)
1437 sc_free(dd->send_contexts[ctxt].sc);
1438 dd->num_send_contexts = 0;
1439 kfree(dd->send_contexts);
1440 dd->send_contexts = NULL;
Jubin John79d0c082016-02-26 13:33:33 -08001441 kfree(dd->hw_to_sw);
1442 dd->hw_to_sw = NULL;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001443 kfree(dd->boardname);
1444 vfree(dd->events);
1445 vfree(dd->status);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001446}
1447
1448/*
1449 * Clean up on unit shutdown, or error during unit load after
1450 * successful initialization.
1451 */
1452static void postinit_cleanup(struct hfi1_devdata *dd)
1453{
1454 hfi1_start_cleanup(dd);
1455
1456 hfi1_pcie_ddcleanup(dd);
1457 hfi1_pcie_cleanup(dd->pcidev);
1458
1459 cleanup_device_data(dd);
1460
1461 hfi1_free_devdata(dd);
1462}
1463
Krzysztof Blaszkowski11501ab2016-10-25 13:12:11 -07001464static int init_validate_rcvhdrcnt(struct device *dev, uint thecnt)
1465{
1466 if (thecnt <= HFI1_MIN_HDRQ_EGRBUF_CNT) {
1467 hfi1_early_err(dev, "Receive header queue count too small\n");
1468 return -EINVAL;
1469 }
1470
1471 if (thecnt > HFI1_MAX_HDRQ_EGRBUF_CNT) {
1472 hfi1_early_err(dev,
1473 "Receive header queue count cannot be greater than %u\n",
1474 HFI1_MAX_HDRQ_EGRBUF_CNT);
1475 return -EINVAL;
1476 }
1477
1478 if (thecnt % HDRQ_INCREMENT) {
1479 hfi1_early_err(dev, "Receive header queue count %d must be divisible by %lu\n",
1480 thecnt, HDRQ_INCREMENT);
1481 return -EINVAL;
1482 }
1483
1484 return 0;
1485}
1486
Mike Marciniszyn77241052015-07-30 15:17:43 -04001487static int init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
1488{
1489 int ret = 0, j, pidx, initfail;
Krzysztof Blaszkowski83fb4af2016-10-17 04:19:24 -07001490 struct hfi1_devdata *dd;
Harish Chegondie8597eb2015-12-01 15:38:20 -05001491 struct hfi1_pportdata *ppd;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001492
1493 /* First, lock the non-writable module parameters */
1494 HFI1_CAP_LOCK();
1495
Tadeusz Struk5d6f08a2017-03-20 17:25:29 -07001496 /* Validate dev ids */
1497 if (!(ent->device == PCI_DEVICE_ID_INTEL0 ||
1498 ent->device == PCI_DEVICE_ID_INTEL1)) {
1499 hfi1_early_err(&pdev->dev,
1500 "Failing on unknown Intel deviceid 0x%x\n",
1501 ent->device);
1502 ret = -ENODEV;
1503 goto bail;
1504 }
1505
Mike Marciniszyn77241052015-07-30 15:17:43 -04001506 /* Validate some global module parameters */
Krzysztof Blaszkowski11501ab2016-10-25 13:12:11 -07001507 ret = init_validate_rcvhdrcnt(&pdev->dev, rcvhdrcnt);
1508 if (ret)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001509 goto bail;
Krzysztof Blaszkowski11501ab2016-10-25 13:12:11 -07001510
Mike Marciniszyn77241052015-07-30 15:17:43 -04001511 /* use the encoding function as a sanitization check */
1512 if (!encode_rcv_header_entry_size(hfi1_hdrq_entsize)) {
1513 hfi1_early_err(&pdev->dev, "Invalid HdrQ Entry size %u\n",
1514 hfi1_hdrq_entsize);
Sebastian Sanchez07859de2015-12-10 16:02:49 -05001515 ret = -EINVAL;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001516 goto bail;
1517 }
1518
1519 /* The receive eager buffer size must be set before the receive
1520 * contexts are created.
1521 *
1522 * Set the eager buffer size. Validate that it falls in a range
1523 * allowed by the hardware - all powers of 2 between the min and
1524 * max. The maximum valid MTU is within the eager buffer range
1525 * so we do not need to cap the max_mtu by an eager buffer size
1526 * setting.
1527 */
1528 if (eager_buffer_size) {
1529 if (!is_power_of_2(eager_buffer_size))
1530 eager_buffer_size =
1531 roundup_pow_of_two(eager_buffer_size);
1532 eager_buffer_size =
1533 clamp_val(eager_buffer_size,
1534 MIN_EAGER_BUFFER * 8,
1535 MAX_EAGER_BUFFER_TOTAL);
1536 hfi1_early_info(&pdev->dev, "Eager buffer size %u\n",
1537 eager_buffer_size);
1538 } else {
1539 hfi1_early_err(&pdev->dev, "Invalid Eager buffer size of 0\n");
1540 ret = -EINVAL;
1541 goto bail;
1542 }
1543
1544 /* restrict value of hfi1_rcvarr_split */
1545 hfi1_rcvarr_split = clamp_val(hfi1_rcvarr_split, 0, 100);
1546
1547 ret = hfi1_pcie_init(pdev, ent);
1548 if (ret)
1549 goto bail;
1550
Krzysztof Blaszkowski83fb4af2016-10-17 04:19:24 -07001551 /*
1552 * Do device-specific initialization, function table setup, dd
1553 * allocation, etc.
1554 */
1555 dd = hfi1_init_dd(pdev, ent);
1556
1557 if (IS_ERR(dd)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001558 ret = PTR_ERR(dd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001559 goto clean_bail; /* error already printed */
Krzysztof Blaszkowski83fb4af2016-10-17 04:19:24 -07001560 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001561
1562 ret = create_workqueues(dd);
1563 if (ret)
1564 goto clean_bail;
1565
1566 /* do the generic initialization */
1567 initfail = hfi1_init(dd, 0);
1568
Vishwanathapura, Niranjanad4829ea2017-04-12 20:29:28 -07001569 /* setup vnic */
1570 hfi1_vnic_setup(dd);
1571
Mike Marciniszyn77241052015-07-30 15:17:43 -04001572 ret = hfi1_register_ib_device(dd);
1573
1574 /*
1575 * Now ready for use. this should be cleared whenever we
1576 * detect a reset, or initiate one. If earlier failure,
1577 * we still create devices, so diags, etc. can be used
1578 * to determine cause of problem.
1579 */
Dean Luicked6f6532016-02-18 11:12:25 -08001580 if (!initfail && !ret) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001581 dd->flags |= HFI1_INITTED;
Dean Luicked6f6532016-02-18 11:12:25 -08001582 /* create debufs files after init and ib register */
1583 hfi1_dbg_ibdev_init(&dd->verbs_dev);
1584 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001585
1586 j = hfi1_device_create(dd);
1587 if (j)
1588 dd_dev_err(dd, "Failed to create /dev devices: %d\n", -j);
1589
1590 if (initfail || ret) {
1591 stop_timers(dd);
1592 flush_workqueue(ib_wq);
Harish Chegondie8597eb2015-12-01 15:38:20 -05001593 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001594 hfi1_quiet_serdes(dd->pport + pidx);
Harish Chegondie8597eb2015-12-01 15:38:20 -05001595 ppd = dd->pport + pidx;
1596 if (ppd->hfi1_wq) {
1597 destroy_workqueue(ppd->hfi1_wq);
1598 ppd->hfi1_wq = NULL;
1599 }
Sebastian Sanchez71d47002017-07-29 08:43:49 -07001600 if (ppd->link_wq) {
1601 destroy_workqueue(ppd->link_wq);
1602 ppd->link_wq = NULL;
1603 }
Harish Chegondie8597eb2015-12-01 15:38:20 -05001604 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001605 if (!j)
1606 hfi1_device_remove(dd);
1607 if (!ret)
1608 hfi1_unregister_ib_device(dd);
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -07001609 hfi1_vnic_cleanup(dd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001610 postinit_cleanup(dd);
1611 if (initfail)
1612 ret = initfail;
1613 goto bail; /* everything already cleaned */
1614 }
1615
1616 sdma_start(dd);
1617
1618 return 0;
1619
1620clean_bail:
1621 hfi1_pcie_cleanup(pdev);
1622bail:
1623 return ret;
1624}
1625
Tadeusz Strukacd7c8f2016-10-25 08:57:55 -07001626static void wait_for_clients(struct hfi1_devdata *dd)
1627{
1628 /*
1629 * Remove the device init value and complete the device if there is
1630 * no clients or wait for active clients to finish.
1631 */
1632 if (atomic_dec_and_test(&dd->user_refcount))
1633 complete(&dd->user_comp);
1634
1635 wait_for_completion(&dd->user_comp);
1636}
1637
Mike Marciniszyn77241052015-07-30 15:17:43 -04001638static void remove_one(struct pci_dev *pdev)
1639{
1640 struct hfi1_devdata *dd = pci_get_drvdata(pdev);
1641
Dean Luicked6f6532016-02-18 11:12:25 -08001642 /* close debugfs files before ib unregister */
1643 hfi1_dbg_ibdev_exit(&dd->verbs_dev);
Tadeusz Strukacd7c8f2016-10-25 08:57:55 -07001644
1645 /* remove the /dev hfi1 interface */
1646 hfi1_device_remove(dd);
1647
1648 /* wait for existing user space clients to finish */
1649 wait_for_clients(dd);
1650
Mike Marciniszyn77241052015-07-30 15:17:43 -04001651 /* unregister from IB core */
1652 hfi1_unregister_ib_device(dd);
1653
Vishwanathapura, Niranjanad4829ea2017-04-12 20:29:28 -07001654 /* cleanup vnic */
1655 hfi1_vnic_cleanup(dd);
1656
Mike Marciniszyn77241052015-07-30 15:17:43 -04001657 /*
1658 * Disable the IB link, disable interrupts on the device,
1659 * clear dma engines, etc.
1660 */
1661 shutdown_device(dd);
1662
1663 stop_timers(dd);
1664
1665 /* wait until all of our (qsfp) queue_work() calls complete */
1666 flush_workqueue(ib_wq);
1667
Mike Marciniszyn77241052015-07-30 15:17:43 -04001668 postinit_cleanup(dd);
1669}
1670
1671/**
1672 * hfi1_create_rcvhdrq - create a receive header queue
1673 * @dd: the hfi1_ib device
1674 * @rcd: the context data
1675 *
1676 * This must be contiguous memory (from an i/o perspective), and must be
1677 * DMA'able (which means for some systems, it will go through an IOMMU,
1678 * or be forced into a low address range).
1679 */
1680int hfi1_create_rcvhdrq(struct hfi1_devdata *dd, struct hfi1_ctxtdata *rcd)
1681{
1682 unsigned amt;
1683 u64 reg;
1684
1685 if (!rcd->rcvhdrq) {
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001686 dma_addr_t dma_hdrqtail;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001687 gfp_t gfp_flags;
1688
1689 /*
1690 * rcvhdrqentsize is in DWs, so we have to convert to bytes
1691 * (* sizeof(u32)).
1692 */
Amitoj Kaur Chawla84449912016-03-04 22:30:43 +05301693 amt = PAGE_ALIGN(rcd->rcvhdrq_cnt * rcd->rcvhdrqentsize *
1694 sizeof(u32));
Mike Marciniszyn77241052015-07-30 15:17:43 -04001695
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -07001696 if ((rcd->ctxt < dd->first_dyn_alloc_ctxt) ||
1697 (rcd->sc && (rcd->sc->type == SC_KERNEL)))
1698 gfp_flags = GFP_KERNEL;
1699 else
1700 gfp_flags = GFP_USER;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001701 rcd->rcvhdrq = dma_zalloc_coherent(
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001702 &dd->pcidev->dev, amt, &rcd->rcvhdrq_dma,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001703 gfp_flags | __GFP_COMP);
1704
1705 if (!rcd->rcvhdrq) {
1706 dd_dev_err(dd,
Jubin John17fb4f22016-02-14 20:21:52 -08001707 "attempt to allocate %d bytes for ctxt %u rcvhdrq failed\n",
1708 amt, rcd->ctxt);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001709 goto bail;
1710 }
1711
Mike Marciniszyn77241052015-07-30 15:17:43 -04001712 if (HFI1_CAP_KGET_MASK(rcd->flags, DMA_RTAIL)) {
1713 rcd->rcvhdrtail_kvaddr = dma_zalloc_coherent(
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001714 &dd->pcidev->dev, PAGE_SIZE, &dma_hdrqtail,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001715 gfp_flags);
1716 if (!rcd->rcvhdrtail_kvaddr)
1717 goto bail_free;
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001718 rcd->rcvhdrqtailaddr_dma = dma_hdrqtail;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001719 }
1720
1721 rcd->rcvhdrq_size = amt;
1722 }
1723 /*
1724 * These values are per-context:
1725 * RcvHdrCnt
1726 * RcvHdrEntSize
1727 * RcvHdrSize
1728 */
1729 reg = ((u64)(rcd->rcvhdrq_cnt >> HDRQ_SIZE_SHIFT)
1730 & RCV_HDR_CNT_CNT_MASK)
1731 << RCV_HDR_CNT_CNT_SHIFT;
1732 write_kctxt_csr(dd, rcd->ctxt, RCV_HDR_CNT, reg);
1733 reg = (encode_rcv_header_entry_size(rcd->rcvhdrqentsize)
1734 & RCV_HDR_ENT_SIZE_ENT_SIZE_MASK)
1735 << RCV_HDR_ENT_SIZE_ENT_SIZE_SHIFT;
1736 write_kctxt_csr(dd, rcd->ctxt, RCV_HDR_ENT_SIZE, reg);
1737 reg = (dd->rcvhdrsize & RCV_HDR_SIZE_HDR_SIZE_MASK)
1738 << RCV_HDR_SIZE_HDR_SIZE_SHIFT;
1739 write_kctxt_csr(dd, rcd->ctxt, RCV_HDR_SIZE, reg);
Mark F. Brown46b010d2015-11-09 19:18:20 -05001740
1741 /*
1742 * Program dummy tail address for every receive context
1743 * before enabling any receive context
1744 */
1745 write_kctxt_csr(dd, rcd->ctxt, RCV_HDR_TAIL_ADDR,
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001746 dd->rcvhdrtail_dummy_dma);
Mark F. Brown46b010d2015-11-09 19:18:20 -05001747
Mike Marciniszyn77241052015-07-30 15:17:43 -04001748 return 0;
1749
1750bail_free:
1751 dd_dev_err(dd,
Jubin John17fb4f22016-02-14 20:21:52 -08001752 "attempt to allocate 1 page for ctxt %u rcvhdrqtailaddr failed\n",
1753 rcd->ctxt);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001754 dma_free_coherent(&dd->pcidev->dev, amt, rcd->rcvhdrq,
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001755 rcd->rcvhdrq_dma);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001756 rcd->rcvhdrq = NULL;
1757bail:
1758 return -ENOMEM;
1759}
1760
1761/**
1762 * allocate eager buffers, both kernel and user contexts.
1763 * @rcd: the context we are setting up.
1764 *
1765 * Allocate the eager TID buffers and program them into hip.
1766 * They are no longer completely contiguous, we do multiple allocation
1767 * calls. Otherwise we get the OOM code involved, by asking for too
1768 * much per call, with disastrous results on some kernels.
1769 */
1770int hfi1_setup_eagerbufs(struct hfi1_ctxtdata *rcd)
1771{
1772 struct hfi1_devdata *dd = rcd->dd;
1773 u32 max_entries, egrtop, alloced_bytes = 0, idx = 0;
1774 gfp_t gfp_flags;
1775 u16 order;
1776 int ret = 0;
1777 u16 round_mtu = roundup_pow_of_two(hfi1_max_mtu);
1778
1779 /*
1780 * GFP_USER, but without GFP_FS, so buffer cache can be
1781 * coalesced (we hope); otherwise, even at order 4,
1782 * heavy filesystem activity makes these fail, and we can
1783 * use compound pages.
1784 */
Mel Gorman71baba42015-11-06 16:28:28 -08001785 gfp_flags = __GFP_RECLAIM | __GFP_IO | __GFP_COMP;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001786
1787 /*
1788 * The minimum size of the eager buffers is a groups of MTU-sized
1789 * buffers.
1790 * The global eager_buffer_size parameter is checked against the
1791 * theoretical lower limit of the value. Here, we check against the
1792 * MTU.
1793 */
1794 if (rcd->egrbufs.size < (round_mtu * dd->rcv_entries.group_size))
1795 rcd->egrbufs.size = round_mtu * dd->rcv_entries.group_size;
1796 /*
1797 * If using one-pkt-per-egr-buffer, lower the eager buffer
1798 * size to the max MTU (page-aligned).
1799 */
1800 if (!HFI1_CAP_KGET_MASK(rcd->flags, MULTI_PKT_EGR))
1801 rcd->egrbufs.rcvtid_size = round_mtu;
1802
1803 /*
1804 * Eager buffers sizes of 1MB or less require smaller TID sizes
1805 * to satisfy the "multiple of 8 RcvArray entries" requirement.
1806 */
1807 if (rcd->egrbufs.size <= (1 << 20))
1808 rcd->egrbufs.rcvtid_size = max((unsigned long)round_mtu,
1809 rounddown_pow_of_two(rcd->egrbufs.size / 8));
1810
1811 while (alloced_bytes < rcd->egrbufs.size &&
1812 rcd->egrbufs.alloced < rcd->egrbufs.count) {
1813 rcd->egrbufs.buffers[idx].addr =
1814 dma_zalloc_coherent(&dd->pcidev->dev,
1815 rcd->egrbufs.rcvtid_size,
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001816 &rcd->egrbufs.buffers[idx].dma,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001817 gfp_flags);
1818 if (rcd->egrbufs.buffers[idx].addr) {
1819 rcd->egrbufs.buffers[idx].len =
1820 rcd->egrbufs.rcvtid_size;
1821 rcd->egrbufs.rcvtids[rcd->egrbufs.alloced].addr =
1822 rcd->egrbufs.buffers[idx].addr;
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001823 rcd->egrbufs.rcvtids[rcd->egrbufs.alloced].dma =
1824 rcd->egrbufs.buffers[idx].dma;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001825 rcd->egrbufs.alloced++;
1826 alloced_bytes += rcd->egrbufs.rcvtid_size;
1827 idx++;
1828 } else {
1829 u32 new_size, i, j;
1830 u64 offset = 0;
1831
1832 /*
1833 * Fail the eager buffer allocation if:
1834 * - we are already using the lowest acceptable size
1835 * - we are using one-pkt-per-egr-buffer (this implies
1836 * that we are accepting only one size)
1837 */
1838 if (rcd->egrbufs.rcvtid_size == round_mtu ||
1839 !HFI1_CAP_KGET_MASK(rcd->flags, MULTI_PKT_EGR)) {
1840 dd_dev_err(dd, "ctxt%u: Failed to allocate eager buffers\n",
Jubin John17fb4f22016-02-14 20:21:52 -08001841 rcd->ctxt);
Michael J. Ruhl94679062017-05-04 05:14:28 -07001842 ret = -ENOMEM;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001843 goto bail_rcvegrbuf_phys;
1844 }
1845
1846 new_size = rcd->egrbufs.rcvtid_size / 2;
1847
1848 /*
1849 * If the first attempt to allocate memory failed, don't
1850 * fail everything but continue with the next lower
1851 * size.
1852 */
1853 if (idx == 0) {
1854 rcd->egrbufs.rcvtid_size = new_size;
1855 continue;
1856 }
1857
1858 /*
1859 * Re-partition already allocated buffers to a smaller
1860 * size.
1861 */
1862 rcd->egrbufs.alloced = 0;
1863 for (i = 0, j = 0, offset = 0; j < idx; i++) {
1864 if (i >= rcd->egrbufs.count)
1865 break;
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001866 rcd->egrbufs.rcvtids[i].dma =
1867 rcd->egrbufs.buffers[j].dma + offset;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001868 rcd->egrbufs.rcvtids[i].addr =
1869 rcd->egrbufs.buffers[j].addr + offset;
1870 rcd->egrbufs.alloced++;
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001871 if ((rcd->egrbufs.buffers[j].dma + offset +
Mike Marciniszyn77241052015-07-30 15:17:43 -04001872 new_size) ==
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001873 (rcd->egrbufs.buffers[j].dma +
Mike Marciniszyn77241052015-07-30 15:17:43 -04001874 rcd->egrbufs.buffers[j].len)) {
1875 j++;
1876 offset = 0;
Jubin Johne4909742016-02-14 20:22:00 -08001877 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001878 offset += new_size;
Jubin Johne4909742016-02-14 20:22:00 -08001879 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001880 }
1881 rcd->egrbufs.rcvtid_size = new_size;
1882 }
1883 }
1884 rcd->egrbufs.numbufs = idx;
1885 rcd->egrbufs.size = alloced_bytes;
1886
Sebastian Sanchez6c63e422015-11-06 20:06:56 -05001887 hfi1_cdbg(PROC,
1888 "ctxt%u: Alloced %u rcv tid entries @ %uKB, total %zuKB\n",
Grzegorz Heldt23002d52016-07-25 13:39:33 -07001889 rcd->ctxt, rcd->egrbufs.alloced,
1890 rcd->egrbufs.rcvtid_size / 1024, rcd->egrbufs.size / 1024);
Sebastian Sanchez6c63e422015-11-06 20:06:56 -05001891
Mike Marciniszyn77241052015-07-30 15:17:43 -04001892 /*
1893 * Set the contexts rcv array head update threshold to the closest
1894 * power of 2 (so we can use a mask instead of modulo) below half
1895 * the allocated entries.
1896 */
1897 rcd->egrbufs.threshold =
1898 rounddown_pow_of_two(rcd->egrbufs.alloced / 2);
1899 /*
1900 * Compute the expected RcvArray entry base. This is done after
1901 * allocating the eager buffers in order to maximize the
1902 * expected RcvArray entries for the context.
1903 */
1904 max_entries = rcd->rcv_array_groups * dd->rcv_entries.group_size;
1905 egrtop = roundup(rcd->egrbufs.alloced, dd->rcv_entries.group_size);
1906 rcd->expected_count = max_entries - egrtop;
1907 if (rcd->expected_count > MAX_TID_PAIR_ENTRIES * 2)
1908 rcd->expected_count = MAX_TID_PAIR_ENTRIES * 2;
1909
1910 rcd->expected_base = rcd->eager_base + egrtop;
Sebastian Sanchez6c63e422015-11-06 20:06:56 -05001911 hfi1_cdbg(PROC, "ctxt%u: eager:%u, exp:%u, egrbase:%u, expbase:%u\n",
1912 rcd->ctxt, rcd->egrbufs.alloced, rcd->expected_count,
1913 rcd->eager_base, rcd->expected_base);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001914
1915 if (!hfi1_rcvbuf_validate(rcd->egrbufs.rcvtid_size, PT_EAGER, &order)) {
Sebastian Sanchez6c63e422015-11-06 20:06:56 -05001916 hfi1_cdbg(PROC,
1917 "ctxt%u: current Eager buffer size is invalid %u\n",
1918 rcd->ctxt, rcd->egrbufs.rcvtid_size);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001919 ret = -EINVAL;
Michael J. Ruhl62239fc2017-05-04 05:15:21 -07001920 goto bail_rcvegrbuf_phys;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001921 }
1922
1923 for (idx = 0; idx < rcd->egrbufs.alloced; idx++) {
1924 hfi1_put_tid(dd, rcd->eager_base + idx, PT_EAGER,
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001925 rcd->egrbufs.rcvtids[idx].dma, order);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001926 cond_resched();
1927 }
Michael J. Ruhl62239fc2017-05-04 05:15:21 -07001928
1929 return 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001930
1931bail_rcvegrbuf_phys:
1932 for (idx = 0; idx < rcd->egrbufs.alloced &&
Jubin John17fb4f22016-02-14 20:21:52 -08001933 rcd->egrbufs.buffers[idx].addr;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001934 idx++) {
1935 dma_free_coherent(&dd->pcidev->dev,
1936 rcd->egrbufs.buffers[idx].len,
1937 rcd->egrbufs.buffers[idx].addr,
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001938 rcd->egrbufs.buffers[idx].dma);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001939 rcd->egrbufs.buffers[idx].addr = NULL;
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001940 rcd->egrbufs.buffers[idx].dma = 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001941 rcd->egrbufs.buffers[idx].len = 0;
1942 }
Michael J. Ruhl62239fc2017-05-04 05:15:21 -07001943
Mike Marciniszyn77241052015-07-30 15:17:43 -04001944 return ret;
1945}