blob: dfdb4126ca05e937db795a8ce5c50fa64c6658ee [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{
134 unsigned i;
135 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 */
Mitko Haralanov957558c2016-02-03 14:33:40 -0800236struct hfi1_ctxtdata *hfi1_create_ctxtdata(struct hfi1_pportdata *ppd, u32 ctxt,
237 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 |
606 HFI1_RCVCTRL_INTRAVAIL_DIS |
607 HFI1_RCVCTRL_TAILUPD_DIS, i);
608 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;
618 u32 i;
619
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;
637 hfi1_rcvctrl(dd, rcvmask, i);
638 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 }
663 }
664 return 0;
665wq_error:
Mike Marciniszyn0a226ed2015-11-09 19:13:58 -0500666 pr_err("alloc_workqueue failed for port %d\n", pidx + 1);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400667 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
668 ppd = dd->pport + pidx;
669 if (ppd->hfi1_wq) {
670 destroy_workqueue(ppd->hfi1_wq);
671 ppd->hfi1_wq = NULL;
672 }
673 }
674 return -ENOMEM;
675}
676
677/**
678 * hfi1_init - do the actual initialization sequence on the chip
679 * @dd: the hfi1_ib device
680 * @reinit: re-initializing, so don't allocate new memory
681 *
682 * Do the actual initialization sequence on the chip. This is done
683 * both from the init routine called from the PCI infrastructure, and
684 * when we reset the chip, or detect that it was reset internally,
685 * or it's administratively re-enabled.
686 *
687 * Memory allocation here and in called routines is only done in
688 * the first case (reinit == 0). We have to be careful, because even
689 * without memory allocation, we need to re-write all the chip registers
690 * TIDs, etc. after the reset or enable has completed.
691 */
692int hfi1_init(struct hfi1_devdata *dd, int reinit)
693{
694 int ret = 0, pidx, lastfail = 0;
695 unsigned i, len;
696 struct hfi1_ctxtdata *rcd;
697 struct hfi1_pportdata *ppd;
698
699 /* Set up recv low level handlers */
700 dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_EXPECTED] =
701 kdeth_process_expected;
702 dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_EAGER] =
703 kdeth_process_eager;
704 dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_IB] = process_receive_ib;
705 dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_ERROR] =
706 process_receive_error;
707 dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_BYPASS] =
708 process_receive_bypass;
709 dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_INVALID5] =
710 process_receive_invalid;
711 dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_INVALID6] =
712 process_receive_invalid;
713 dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_INVALID7] =
714 process_receive_invalid;
715 dd->rhf_rcv_function_map = dd->normal_rhf_rcv_functions;
716
717 /* Set up send low level handlers */
718 dd->process_pio_send = hfi1_verbs_send_pio;
719 dd->process_dma_send = hfi1_verbs_send_dma;
720 dd->pio_inline_send = pio_copy;
Vishwanathapura, Niranjana64551ed2017-04-12 20:29:30 -0700721 dd->process_vnic_dma_send = hfi1_vnic_send_dma;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400722
Mike Marciniszyn995deaf2015-11-16 21:59:29 -0500723 if (is_ax(dd)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400724 atomic_set(&dd->drop_packet, DROP_PACKET_ON);
725 dd->do_drop = 1;
726 } else {
727 atomic_set(&dd->drop_packet, DROP_PACKET_OFF);
728 dd->do_drop = 0;
729 }
730
731 /* make sure the link is not "up" */
732 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
733 ppd = dd->pport + pidx;
734 ppd->linkup = 0;
735 }
736
737 if (reinit)
738 ret = init_after_reset(dd);
739 else
740 ret = loadtime_init(dd);
741 if (ret)
742 goto done;
743
Mark F. Brown46b010d2015-11-09 19:18:20 -0500744 /* allocate dummy tail memory for all receive contexts */
745 dd->rcvhdrtail_dummy_kvaddr = dma_zalloc_coherent(
746 &dd->pcidev->dev, sizeof(u64),
Tymoteusz Kielan60368182016-09-06 04:35:54 -0700747 &dd->rcvhdrtail_dummy_dma,
Mark F. Brown46b010d2015-11-09 19:18:20 -0500748 GFP_KERNEL);
749
750 if (!dd->rcvhdrtail_dummy_kvaddr) {
751 dd_dev_err(dd, "cannot allocate dummy tail memory\n");
752 ret = -ENOMEM;
753 goto done;
754 }
755
Mike Marciniszyn77241052015-07-30 15:17:43 -0400756 /* dd->rcd can be NULL if early initialization failed */
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700757 for (i = 0; dd->rcd && i < dd->first_dyn_alloc_ctxt; ++i) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400758 /*
759 * Set up the (kernel) rcvhdr queue and egr TIDs. If doing
760 * re-init, the simplest way to handle this is to free
761 * existing, and re-allocate.
762 * Need to re-create rest of ctxt 0 ctxtdata as well.
763 */
764 rcd = dd->rcd[i];
765 if (!rcd)
766 continue;
767
768 rcd->do_interrupt = &handle_receive_interrupt;
769
770 lastfail = hfi1_create_rcvhdrq(dd, rcd);
771 if (!lastfail)
772 lastfail = hfi1_setup_eagerbufs(rcd);
Ashutosh Dixit39239792016-05-12 10:24:00 -0700773 if (lastfail) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400774 dd_dev_err(dd,
Jubin John17fb4f22016-02-14 20:21:52 -0800775 "failed to allocate kernel ctxt's rcvhdrq and/or egr bufs\n");
Ashutosh Dixit39239792016-05-12 10:24:00 -0700776 ret = lastfail;
777 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400778 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400779
780 /* Allocate enough memory for user event notification. */
Amitoj Kaur Chawla84449912016-03-04 22:30:43 +0530781 len = PAGE_ALIGN(dd->chip_rcv_contexts * HFI1_MAX_SHARED_CTXTS *
782 sizeof(*dd->events));
Mike Marciniszyn77241052015-07-30 15:17:43 -0400783 dd->events = vmalloc_user(len);
784 if (!dd->events)
785 dd_dev_err(dd, "Failed to allocate user events page\n");
786 /*
787 * Allocate a page for device and port status.
788 * Page will be shared amongst all user processes.
789 */
790 dd->status = vmalloc_user(PAGE_SIZE);
791 if (!dd->status)
792 dd_dev_err(dd, "Failed to allocate dev status page\n");
793 else
794 dd->freezelen = PAGE_SIZE - (sizeof(*dd->status) -
795 sizeof(dd->status->freezemsg));
796 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
797 ppd = dd->pport + pidx;
798 if (dd->status)
799 /* Currently, we only have one port */
800 ppd->statusp = &dd->status->port;
801
802 set_mtu(ppd);
803 }
804
805 /* enable chip even if we have an error, so we can debug cause */
806 enable_chip(dd);
807
Mike Marciniszyn77241052015-07-30 15:17:43 -0400808done:
809 /*
810 * Set status even if port serdes is not initialized
811 * so that diags will work.
812 */
813 if (dd->status)
814 dd->status->dev |= HFI1_STATUS_CHIP_PRESENT |
815 HFI1_STATUS_INITTED;
816 if (!ret) {
817 /* enable all interrupts from the chip */
818 set_intr_state(dd, 1);
819
820 /* chip is OK for user apps; mark it as initialized */
821 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
822 ppd = dd->pport + pidx;
823
Jubin John4d114fd2016-02-14 20:21:43 -0800824 /*
825 * start the serdes - must be after interrupts are
826 * enabled so we are notified when the link goes up
Mike Marciniszyn77241052015-07-30 15:17:43 -0400827 */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400828 lastfail = bringup_serdes(ppd);
829 if (lastfail)
830 dd_dev_info(dd,
Jubin John17fb4f22016-02-14 20:21:52 -0800831 "Failed to bring up port %u\n",
832 ppd->port);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400833
834 /*
835 * Set status even if port serdes is not initialized
836 * so that diags will work.
837 */
838 if (ppd->statusp)
839 *ppd->statusp |= HFI1_STATUS_CHIP_PRESENT |
840 HFI1_STATUS_INITTED;
841 if (!ppd->link_speed_enabled)
842 continue;
843 }
844 }
845
846 /* if ret is non-zero, we probably should do some cleanup here... */
847 return ret;
848}
849
850static inline struct hfi1_devdata *__hfi1_lookup(int unit)
851{
852 return idr_find(&hfi1_unit_table, unit);
853}
854
855struct hfi1_devdata *hfi1_lookup(int unit)
856{
857 struct hfi1_devdata *dd;
858 unsigned long flags;
859
860 spin_lock_irqsave(&hfi1_devs_lock, flags);
861 dd = __hfi1_lookup(unit);
862 spin_unlock_irqrestore(&hfi1_devs_lock, flags);
863
864 return dd;
865}
866
867/*
868 * Stop the timers during unit shutdown, or after an error late
869 * in initialization.
870 */
871static void stop_timers(struct hfi1_devdata *dd)
872{
873 struct hfi1_pportdata *ppd;
874 int pidx;
875
876 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
877 ppd = dd->pport + pidx;
878 if (ppd->led_override_timer.data) {
879 del_timer_sync(&ppd->led_override_timer);
880 atomic_set(&ppd->led_override_timer_active, 0);
881 }
882 }
883}
884
885/**
886 * shutdown_device - shut down a device
887 * @dd: the hfi1_ib device
888 *
889 * This is called to make the device quiet when we are about to
890 * unload the driver, and also when the device is administratively
891 * disabled. It does not free any data structures.
892 * Everything it does has to be setup again by hfi1_init(dd, 1)
893 */
894static void shutdown_device(struct hfi1_devdata *dd)
895{
896 struct hfi1_pportdata *ppd;
897 unsigned pidx;
898 int i;
899
900 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
901 ppd = dd->pport + pidx;
902
903 ppd->linkup = 0;
904 if (ppd->statusp)
905 *ppd->statusp &= ~(HFI1_STATUS_IB_CONF |
906 HFI1_STATUS_IB_READY);
907 }
908 dd->flags &= ~HFI1_INITTED;
909
910 /* mask interrupts, but not errors */
911 set_intr_state(dd, 0);
912
913 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
914 ppd = dd->pport + pidx;
915 for (i = 0; i < dd->num_rcv_contexts; i++)
916 hfi1_rcvctrl(dd, HFI1_RCVCTRL_TAILUPD_DIS |
917 HFI1_RCVCTRL_CTXT_DIS |
918 HFI1_RCVCTRL_INTRAVAIL_DIS |
919 HFI1_RCVCTRL_PKEY_DIS |
920 HFI1_RCVCTRL_ONE_PKT_EGR_DIS, i);
921 /*
922 * Gracefully stop all sends allowing any in progress to
923 * trickle out first.
924 */
925 for (i = 0; i < dd->num_send_contexts; i++)
926 sc_flush(dd->send_contexts[i].sc);
927 }
928
929 /*
930 * Enough for anything that's going to trickle out to have actually
931 * done so.
932 */
933 udelay(20);
934
935 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
936 ppd = dd->pport + pidx;
937
938 /* disable all contexts */
939 for (i = 0; i < dd->num_send_contexts; i++)
940 sc_disable(dd->send_contexts[i].sc);
941 /* disable the send device */
942 pio_send_control(dd, PSC_GLOBAL_DISABLE);
943
Easwar Hariharan91ab4ed2016-02-03 14:35:57 -0800944 shutdown_led_override(ppd);
945
Mike Marciniszyn77241052015-07-30 15:17:43 -0400946 /*
947 * Clear SerdesEnable.
948 * We can't count on interrupts since we are stopping.
949 */
950 hfi1_quiet_serdes(ppd);
951
952 if (ppd->hfi1_wq) {
953 destroy_workqueue(ppd->hfi1_wq);
954 ppd->hfi1_wq = NULL;
955 }
956 }
957 sdma_exit(dd);
958}
959
960/**
961 * hfi1_free_ctxtdata - free a context's allocated data
962 * @dd: the hfi1_ib device
963 * @rcd: the ctxtdata structure
964 *
965 * free up any allocated data for a context
Mike Marciniszyn77241052015-07-30 15:17:43 -0400966 * It should never change any chip state, or global driver state.
967 */
968void hfi1_free_ctxtdata(struct hfi1_devdata *dd, struct hfi1_ctxtdata *rcd)
969{
Michael J. Ruhlf683c802017-06-09 16:00:19 -0700970 u32 e;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400971
972 if (!rcd)
973 return;
974
975 if (rcd->rcvhdrq) {
976 dma_free_coherent(&dd->pcidev->dev, rcd->rcvhdrq_size,
Tymoteusz Kielan60368182016-09-06 04:35:54 -0700977 rcd->rcvhdrq, rcd->rcvhdrq_dma);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400978 rcd->rcvhdrq = NULL;
979 if (rcd->rcvhdrtail_kvaddr) {
980 dma_free_coherent(&dd->pcidev->dev, PAGE_SIZE,
981 (void *)rcd->rcvhdrtail_kvaddr,
Tymoteusz Kielan60368182016-09-06 04:35:54 -0700982 rcd->rcvhdrqtailaddr_dma);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400983 rcd->rcvhdrtail_kvaddr = NULL;
984 }
985 }
986
987 /* all the RcvArray entries should have been cleared by now */
988 kfree(rcd->egrbufs.rcvtids);
Michael J. Ruhlf683c802017-06-09 16:00:19 -0700989 rcd->egrbufs.rcvtids = NULL;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400990
991 for (e = 0; e < rcd->egrbufs.alloced; e++) {
Tymoteusz Kielan60368182016-09-06 04:35:54 -0700992 if (rcd->egrbufs.buffers[e].dma)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400993 dma_free_coherent(&dd->pcidev->dev,
994 rcd->egrbufs.buffers[e].len,
995 rcd->egrbufs.buffers[e].addr,
Tymoteusz Kielan60368182016-09-06 04:35:54 -0700996 rcd->egrbufs.buffers[e].dma);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400997 }
998 kfree(rcd->egrbufs.buffers);
Michael J. Ruhlf683c802017-06-09 16:00:19 -0700999 rcd->egrbufs.alloced = 0;
1000 rcd->egrbufs.buffers = NULL;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001001
1002 sc_free(rcd->sc);
Michael J. Ruhlf683c802017-06-09 16:00:19 -07001003 rcd->sc = NULL;
1004
Mike Marciniszyn77241052015-07-30 15:17:43 -04001005 vfree(rcd->subctxt_uregbase);
1006 vfree(rcd->subctxt_rcvegrbuf);
1007 vfree(rcd->subctxt_rcvhdr_base);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001008 kfree(rcd->opstats);
Michael J. Ruhlf683c802017-06-09 16:00:19 -07001009
1010 rcd->subctxt_uregbase = NULL;
1011 rcd->subctxt_rcvegrbuf = NULL;
1012 rcd->subctxt_rcvhdr_base = NULL;
1013 rcd->opstats = NULL;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001014}
1015
Dean Luick78eb1292016-03-05 08:49:45 -08001016/*
1017 * Release our hold on the shared asic data. If we are the last one,
Dean Luickdba715f2016-07-06 17:28:52 -04001018 * return the structure to be finalized outside the lock. Must be
1019 * holding hfi1_devs_lock.
Dean Luick78eb1292016-03-05 08:49:45 -08001020 */
Dean Luickdba715f2016-07-06 17:28:52 -04001021static struct hfi1_asic_data *release_asic_data(struct hfi1_devdata *dd)
Dean Luick78eb1292016-03-05 08:49:45 -08001022{
Dean Luickdba715f2016-07-06 17:28:52 -04001023 struct hfi1_asic_data *ad;
Dean Luick78eb1292016-03-05 08:49:45 -08001024 int other;
1025
1026 if (!dd->asic_data)
Dean Luickdba715f2016-07-06 17:28:52 -04001027 return NULL;
Dean Luick78eb1292016-03-05 08:49:45 -08001028 dd->asic_data->dds[dd->hfi1_id] = NULL;
1029 other = dd->hfi1_id ? 0 : 1;
Dean Luickdba715f2016-07-06 17:28:52 -04001030 ad = dd->asic_data;
Dean Luick78eb1292016-03-05 08:49:45 -08001031 dd->asic_data = NULL;
Dean Luickdba715f2016-07-06 17:28:52 -04001032 /* return NULL if the other dd still has a link */
1033 return ad->dds[other] ? NULL : ad;
1034}
1035
1036static void finalize_asic_data(struct hfi1_devdata *dd,
1037 struct hfi1_asic_data *ad)
1038{
1039 clean_up_i2c(dd, ad);
1040 kfree(ad);
Dean Luick78eb1292016-03-05 08:49:45 -08001041}
1042
Dennis Dalessandroe11ffbd2016-05-19 05:26:44 -07001043static void __hfi1_free_devdata(struct kobject *kobj)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001044{
Dennis Dalessandroe11ffbd2016-05-19 05:26:44 -07001045 struct hfi1_devdata *dd =
1046 container_of(kobj, struct hfi1_devdata, kobj);
Dean Luickdba715f2016-07-06 17:28:52 -04001047 struct hfi1_asic_data *ad;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001048 unsigned long flags;
1049
1050 spin_lock_irqsave(&hfi1_devs_lock, flags);
1051 idr_remove(&hfi1_unit_table, dd->unit);
1052 list_del(&dd->list);
Dean Luickdba715f2016-07-06 17:28:52 -04001053 ad = release_asic_data(dd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001054 spin_unlock_irqrestore(&hfi1_devs_lock, flags);
Dean Luickdba715f2016-07-06 17:28:52 -04001055 if (ad)
1056 finalize_asic_data(dd, ad);
Easwar Hariharanc3838b32016-02-09 14:29:13 -08001057 free_platform_config(dd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001058 rcu_barrier(); /* wait for rcu callbacks to complete */
1059 free_percpu(dd->int_counter);
1060 free_percpu(dd->rcv_limit);
Vennila Megavannan89abfc82016-02-03 14:34:07 -08001061 free_percpu(dd->send_schedule);
Jubin Johnea0e4ce2016-04-20 06:05:24 -07001062 rvt_dealloc_device(&dd->verbs_dev.rdi);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001063}
1064
Dennis Dalessandroe11ffbd2016-05-19 05:26:44 -07001065static struct kobj_type hfi1_devdata_type = {
1066 .release = __hfi1_free_devdata,
1067};
1068
1069void hfi1_free_devdata(struct hfi1_devdata *dd)
1070{
1071 kobject_put(&dd->kobj);
1072}
1073
Mike Marciniszyn77241052015-07-30 15:17:43 -04001074/*
1075 * Allocate our primary per-unit data structure. Must be done via verbs
1076 * allocator, because the verbs cleanup process both does cleanup and
1077 * free of the data structure.
1078 * "extra" is for chip-specific data.
1079 *
1080 * Use the idr mechanism to get a unit number for this unit.
1081 */
1082struct hfi1_devdata *hfi1_alloc_devdata(struct pci_dev *pdev, size_t extra)
1083{
1084 unsigned long flags;
1085 struct hfi1_devdata *dd;
Dennis Dalessandro7af6d002016-01-19 14:44:06 -08001086 int ret, nports;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001087
Dennis Dalessandro7af6d002016-01-19 14:44:06 -08001088 /* extra is * number of ports */
1089 nports = extra / sizeof(struct hfi1_pportdata);
1090
1091 dd = (struct hfi1_devdata *)rvt_alloc_device(sizeof(*dd) + extra,
1092 nports);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001093 if (!dd)
1094 return ERR_PTR(-ENOMEM);
Dennis Dalessandro7af6d002016-01-19 14:44:06 -08001095 dd->num_pports = nports;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001096 dd->pport = (struct hfi1_pportdata *)(dd + 1);
1097
1098 INIT_LIST_HEAD(&dd->list);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001099 idr_preload(GFP_KERNEL);
1100 spin_lock_irqsave(&hfi1_devs_lock, flags);
1101
1102 ret = idr_alloc(&hfi1_unit_table, dd, 0, 0, GFP_NOWAIT);
1103 if (ret >= 0) {
1104 dd->unit = ret;
1105 list_add(&dd->list, &hfi1_dev_list);
1106 }
1107
1108 spin_unlock_irqrestore(&hfi1_devs_lock, flags);
1109 idr_preload_end();
1110
1111 if (ret < 0) {
1112 hfi1_early_err(&pdev->dev,
1113 "Could not allocate unit ID: error %d\n", -ret);
1114 goto bail;
1115 }
1116 /*
1117 * Initialize all locks for the device. This needs to be as early as
1118 * possible so locks are usable.
1119 */
1120 spin_lock_init(&dd->sc_lock);
1121 spin_lock_init(&dd->sendctrl_lock);
1122 spin_lock_init(&dd->rcvctrl_lock);
1123 spin_lock_init(&dd->uctxt_lock);
1124 spin_lock_init(&dd->hfi1_diag_trans_lock);
1125 spin_lock_init(&dd->sc_init_lock);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001126 spin_lock_init(&dd->dc8051_memlock);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001127 seqlock_init(&dd->sc2vl_lock);
1128 spin_lock_init(&dd->sde_map_lock);
Jubin John35f6bef2016-02-14 12:46:10 -08001129 spin_lock_init(&dd->pio_map_lock);
Tadeusz Struk22546b72017-04-28 10:40:02 -07001130 mutex_init(&dd->dc8051_lock);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001131 init_waitqueue_head(&dd->event_queue);
1132
1133 dd->int_counter = alloc_percpu(u64);
1134 if (!dd->int_counter) {
1135 ret = -ENOMEM;
1136 hfi1_early_err(&pdev->dev,
1137 "Could not allocate per-cpu int_counter\n");
1138 goto bail;
1139 }
1140
1141 dd->rcv_limit = alloc_percpu(u64);
1142 if (!dd->rcv_limit) {
1143 ret = -ENOMEM;
1144 hfi1_early_err(&pdev->dev,
1145 "Could not allocate per-cpu rcv_limit\n");
1146 goto bail;
1147 }
1148
Vennila Megavannan89abfc82016-02-03 14:34:07 -08001149 dd->send_schedule = alloc_percpu(u64);
1150 if (!dd->send_schedule) {
1151 ret = -ENOMEM;
1152 hfi1_early_err(&pdev->dev,
1153 "Could not allocate per-cpu int_counter\n");
1154 goto bail;
1155 }
1156
Mike Marciniszyn77241052015-07-30 15:17:43 -04001157 if (!hfi1_cpulist_count) {
1158 u32 count = num_online_cpus();
1159
Shraddha Barke314fcc02015-10-09 21:03:26 +05301160 hfi1_cpulist = kcalloc(BITS_TO_LONGS(count), sizeof(long),
1161 GFP_KERNEL);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001162 if (hfi1_cpulist)
1163 hfi1_cpulist_count = count;
1164 else
1165 hfi1_early_err(
1166 &pdev->dev,
1167 "Could not alloc cpulist info, cpu affinity might be wrong\n");
1168 }
Dennis Dalessandroe11ffbd2016-05-19 05:26:44 -07001169 kobject_init(&dd->kobj, &hfi1_devdata_type);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001170 return dd;
1171
1172bail:
1173 if (!list_empty(&dd->list))
1174 list_del_init(&dd->list);
Jubin Johnea0e4ce2016-04-20 06:05:24 -07001175 rvt_dealloc_device(&dd->verbs_dev.rdi);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001176 return ERR_PTR(ret);
1177}
1178
1179/*
1180 * Called from freeze mode handlers, and from PCI error
1181 * reporting code. Should be paranoid about state of
1182 * system and data structures.
1183 */
1184void hfi1_disable_after_error(struct hfi1_devdata *dd)
1185{
1186 if (dd->flags & HFI1_INITTED) {
1187 u32 pidx;
1188
1189 dd->flags &= ~HFI1_INITTED;
1190 if (dd->pport)
1191 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
1192 struct hfi1_pportdata *ppd;
1193
1194 ppd = dd->pport + pidx;
1195 if (dd->flags & HFI1_PRESENT)
1196 set_link_state(ppd, HLS_DN_DISABLE);
1197
1198 if (ppd->statusp)
1199 *ppd->statusp &= ~HFI1_STATUS_IB_READY;
1200 }
1201 }
1202
1203 /*
1204 * Mark as having had an error for driver, and also
1205 * for /sys and status word mapped to user programs.
1206 * This marks unit as not usable, until reset.
1207 */
1208 if (dd->status)
1209 dd->status->dev |= HFI1_STATUS_HWERROR;
1210}
1211
1212static void remove_one(struct pci_dev *);
1213static int init_one(struct pci_dev *, const struct pci_device_id *);
1214
1215#define DRIVER_LOAD_MSG "Intel " DRIVER_NAME " loaded: "
1216#define PFX DRIVER_NAME ": "
1217
Sebastian Sanchezd6373012016-07-25 07:54:48 -07001218const struct pci_device_id hfi1_pci_tbl[] = {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001219 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL0) },
1220 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL1) },
1221 { 0, }
1222};
1223
1224MODULE_DEVICE_TABLE(pci, hfi1_pci_tbl);
1225
1226static struct pci_driver hfi1_pci_driver = {
1227 .name = DRIVER_NAME,
1228 .probe = init_one,
1229 .remove = remove_one,
1230 .id_table = hfi1_pci_tbl,
1231 .err_handler = &hfi1_pci_err_handler,
1232};
1233
1234static void __init compute_krcvqs(void)
1235{
1236 int i;
1237
1238 for (i = 0; i < krcvqsset; i++)
1239 n_krcvqs += krcvqs[i];
1240}
1241
1242/*
1243 * Do all the generic driver unit- and chip-independent memory
1244 * allocation and initialization.
1245 */
1246static int __init hfi1_mod_init(void)
1247{
1248 int ret;
1249
1250 ret = dev_init();
1251 if (ret)
1252 goto bail;
1253
Sebastian Sanchezd6373012016-07-25 07:54:48 -07001254 ret = node_affinity_init();
1255 if (ret)
1256 goto bail;
Dennis Dalessandro41973442016-07-25 07:52:36 -07001257
Mike Marciniszyn77241052015-07-30 15:17:43 -04001258 /* validate max MTU before any devices start */
1259 if (!valid_opa_max_mtu(hfi1_max_mtu)) {
1260 pr_err("Invalid max_mtu 0x%x, using 0x%x instead\n",
1261 hfi1_max_mtu, HFI1_DEFAULT_MAX_MTU);
1262 hfi1_max_mtu = HFI1_DEFAULT_MAX_MTU;
1263 }
1264 /* valid CUs run from 1-128 in powers of 2 */
1265 if (hfi1_cu > 128 || !is_power_of_2(hfi1_cu))
1266 hfi1_cu = 1;
1267 /* valid credit return threshold is 0-100, variable is unsigned */
1268 if (user_credit_return_threshold > 100)
1269 user_credit_return_threshold = 100;
1270
1271 compute_krcvqs();
Jubin John4d114fd2016-02-14 20:21:43 -08001272 /*
1273 * sanitize receive interrupt count, time must wait until after
1274 * the hardware type is known
1275 */
Mike Marciniszyn77241052015-07-30 15:17:43 -04001276 if (rcv_intr_count > RCV_HDR_HEAD_COUNTER_MASK)
1277 rcv_intr_count = RCV_HDR_HEAD_COUNTER_MASK;
1278 /* reject invalid combinations */
1279 if (rcv_intr_count == 0 && rcv_intr_timeout == 0) {
1280 pr_err("Invalid mode: both receive interrupt count and available timeout are zero - setting interrupt count to 1\n");
1281 rcv_intr_count = 1;
1282 }
1283 if (rcv_intr_count > 1 && rcv_intr_timeout == 0) {
1284 /*
1285 * Avoid indefinite packet delivery by requiring a timeout
1286 * if count is > 1.
1287 */
1288 pr_err("Invalid mode: receive interrupt count greater than 1 and available timeout is zero - setting available timeout to 1\n");
1289 rcv_intr_timeout = 1;
1290 }
1291 if (rcv_intr_dynamic && !(rcv_intr_count > 1 && rcv_intr_timeout > 0)) {
1292 /*
1293 * The dynamic algorithm expects a non-zero timeout
1294 * and a count > 1.
1295 */
1296 pr_err("Invalid mode: dynamic receive interrupt mitigation with invalid count and timeout - turning dynamic off\n");
1297 rcv_intr_dynamic = 0;
1298 }
1299
1300 /* sanitize link CRC options */
1301 link_crc_mask &= SUPPORTED_CRCS;
1302
1303 /*
1304 * These must be called before the driver is registered with
1305 * the PCI subsystem.
1306 */
1307 idr_init(&hfi1_unit_table);
1308
1309 hfi1_dbg_init();
Dean Luick528ee9f2016-03-05 08:50:43 -08001310 ret = hfi1_wss_init();
1311 if (ret < 0)
1312 goto bail_wss;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001313 ret = pci_register_driver(&hfi1_pci_driver);
1314 if (ret < 0) {
1315 pr_err("Unable to register driver: error %d\n", -ret);
1316 goto bail_dev;
1317 }
1318 goto bail; /* all OK */
1319
1320bail_dev:
Dean Luick528ee9f2016-03-05 08:50:43 -08001321 hfi1_wss_exit();
1322bail_wss:
Mike Marciniszyn77241052015-07-30 15:17:43 -04001323 hfi1_dbg_exit();
1324 idr_destroy(&hfi1_unit_table);
1325 dev_cleanup();
1326bail:
1327 return ret;
1328}
1329
1330module_init(hfi1_mod_init);
1331
1332/*
1333 * Do the non-unit driver cleanup, memory free, etc. at unload.
1334 */
1335static void __exit hfi1_mod_cleanup(void)
1336{
1337 pci_unregister_driver(&hfi1_pci_driver);
Dennis Dalessandro41973442016-07-25 07:52:36 -07001338 node_affinity_destroy();
Dean Luick528ee9f2016-03-05 08:50:43 -08001339 hfi1_wss_exit();
Mike Marciniszyn77241052015-07-30 15:17:43 -04001340 hfi1_dbg_exit();
1341 hfi1_cpulist_count = 0;
1342 kfree(hfi1_cpulist);
1343
1344 idr_destroy(&hfi1_unit_table);
1345 dispose_firmware(); /* asymmetric with obtain_firmware() */
1346 dev_cleanup();
1347}
1348
1349module_exit(hfi1_mod_cleanup);
1350
1351/* this can only be called after a successful initialization */
1352static void cleanup_device_data(struct hfi1_devdata *dd)
1353{
1354 int ctxt;
1355 int pidx;
1356 struct hfi1_ctxtdata **tmp;
1357 unsigned long flags;
1358
1359 /* users can't do anything more with chip */
1360 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
1361 struct hfi1_pportdata *ppd = &dd->pport[pidx];
1362 struct cc_state *cc_state;
1363 int i;
1364
1365 if (ppd->statusp)
1366 *ppd->statusp &= ~HFI1_STATUS_CHIP_PRESENT;
1367
1368 for (i = 0; i < OPA_MAX_SLS; i++)
1369 hrtimer_cancel(&ppd->cca_timer[i].hrtimer);
1370
1371 spin_lock(&ppd->cc_state_lock);
Jianxin Xiong8adf71f2016-07-25 13:39:14 -07001372 cc_state = get_cc_state_protected(ppd);
Muhammad Falak R Wanieea57072016-05-01 18:05:31 +05301373 RCU_INIT_POINTER(ppd->cc_state, NULL);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001374 spin_unlock(&ppd->cc_state_lock);
1375
1376 if (cc_state)
Wei Yongjun476d95b2016-08-10 03:14:04 +00001377 kfree_rcu(cc_state, rcu);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001378 }
1379
1380 free_credit_return(dd);
1381
1382 /*
1383 * Free any resources still in use (usually just kernel contexts)
1384 * at unload; we do for ctxtcnt, because that's what we allocate.
1385 * We acquire lock to be really paranoid that rcd isn't being
1386 * accessed from some interrupt-related code (that should not happen,
1387 * but best to be sure).
1388 */
1389 spin_lock_irqsave(&dd->uctxt_lock, flags);
1390 tmp = dd->rcd;
1391 dd->rcd = NULL;
1392 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
Mark F. Brown46b010d2015-11-09 19:18:20 -05001393
1394 if (dd->rcvhdrtail_dummy_kvaddr) {
1395 dma_free_coherent(&dd->pcidev->dev, sizeof(u64),
1396 (void *)dd->rcvhdrtail_dummy_kvaddr,
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001397 dd->rcvhdrtail_dummy_dma);
Dan Carpentera8b7da52016-05-28 08:01:20 +03001398 dd->rcvhdrtail_dummy_kvaddr = NULL;
Mark F. Brown46b010d2015-11-09 19:18:20 -05001399 }
1400
Mike Marciniszyn77241052015-07-30 15:17:43 -04001401 for (ctxt = 0; tmp && ctxt < dd->num_rcv_contexts; ctxt++) {
1402 struct hfi1_ctxtdata *rcd = tmp[ctxt];
1403
1404 tmp[ctxt] = NULL; /* debugging paranoia */
1405 if (rcd) {
1406 hfi1_clear_tids(rcd);
Michael J. Ruhlf683c802017-06-09 16:00:19 -07001407 hfi1_rcd_put(rcd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001408 }
1409 }
1410 kfree(tmp);
Jubin John35f6bef2016-02-14 12:46:10 -08001411 free_pio_map(dd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001412 /* must follow rcv context free - need to remove rcv's hooks */
1413 for (ctxt = 0; ctxt < dd->num_send_contexts; ctxt++)
1414 sc_free(dd->send_contexts[ctxt].sc);
1415 dd->num_send_contexts = 0;
1416 kfree(dd->send_contexts);
1417 dd->send_contexts = NULL;
Jubin John79d0c082016-02-26 13:33:33 -08001418 kfree(dd->hw_to_sw);
1419 dd->hw_to_sw = NULL;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001420 kfree(dd->boardname);
1421 vfree(dd->events);
1422 vfree(dd->status);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001423}
1424
1425/*
1426 * Clean up on unit shutdown, or error during unit load after
1427 * successful initialization.
1428 */
1429static void postinit_cleanup(struct hfi1_devdata *dd)
1430{
1431 hfi1_start_cleanup(dd);
1432
1433 hfi1_pcie_ddcleanup(dd);
1434 hfi1_pcie_cleanup(dd->pcidev);
1435
1436 cleanup_device_data(dd);
1437
1438 hfi1_free_devdata(dd);
1439}
1440
Krzysztof Blaszkowski11501ab2016-10-25 13:12:11 -07001441static int init_validate_rcvhdrcnt(struct device *dev, uint thecnt)
1442{
1443 if (thecnt <= HFI1_MIN_HDRQ_EGRBUF_CNT) {
1444 hfi1_early_err(dev, "Receive header queue count too small\n");
1445 return -EINVAL;
1446 }
1447
1448 if (thecnt > HFI1_MAX_HDRQ_EGRBUF_CNT) {
1449 hfi1_early_err(dev,
1450 "Receive header queue count cannot be greater than %u\n",
1451 HFI1_MAX_HDRQ_EGRBUF_CNT);
1452 return -EINVAL;
1453 }
1454
1455 if (thecnt % HDRQ_INCREMENT) {
1456 hfi1_early_err(dev, "Receive header queue count %d must be divisible by %lu\n",
1457 thecnt, HDRQ_INCREMENT);
1458 return -EINVAL;
1459 }
1460
1461 return 0;
1462}
1463
Mike Marciniszyn77241052015-07-30 15:17:43 -04001464static int init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
1465{
1466 int ret = 0, j, pidx, initfail;
Krzysztof Blaszkowski83fb4af2016-10-17 04:19:24 -07001467 struct hfi1_devdata *dd;
Harish Chegondie8597eb2015-12-01 15:38:20 -05001468 struct hfi1_pportdata *ppd;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001469
1470 /* First, lock the non-writable module parameters */
1471 HFI1_CAP_LOCK();
1472
Tadeusz Struk5d6f08a2017-03-20 17:25:29 -07001473 /* Validate dev ids */
1474 if (!(ent->device == PCI_DEVICE_ID_INTEL0 ||
1475 ent->device == PCI_DEVICE_ID_INTEL1)) {
1476 hfi1_early_err(&pdev->dev,
1477 "Failing on unknown Intel deviceid 0x%x\n",
1478 ent->device);
1479 ret = -ENODEV;
1480 goto bail;
1481 }
1482
Mike Marciniszyn77241052015-07-30 15:17:43 -04001483 /* Validate some global module parameters */
Krzysztof Blaszkowski11501ab2016-10-25 13:12:11 -07001484 ret = init_validate_rcvhdrcnt(&pdev->dev, rcvhdrcnt);
1485 if (ret)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001486 goto bail;
Krzysztof Blaszkowski11501ab2016-10-25 13:12:11 -07001487
Mike Marciniszyn77241052015-07-30 15:17:43 -04001488 /* use the encoding function as a sanitization check */
1489 if (!encode_rcv_header_entry_size(hfi1_hdrq_entsize)) {
1490 hfi1_early_err(&pdev->dev, "Invalid HdrQ Entry size %u\n",
1491 hfi1_hdrq_entsize);
Sebastian Sanchez07859de2015-12-10 16:02:49 -05001492 ret = -EINVAL;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001493 goto bail;
1494 }
1495
1496 /* The receive eager buffer size must be set before the receive
1497 * contexts are created.
1498 *
1499 * Set the eager buffer size. Validate that it falls in a range
1500 * allowed by the hardware - all powers of 2 between the min and
1501 * max. The maximum valid MTU is within the eager buffer range
1502 * so we do not need to cap the max_mtu by an eager buffer size
1503 * setting.
1504 */
1505 if (eager_buffer_size) {
1506 if (!is_power_of_2(eager_buffer_size))
1507 eager_buffer_size =
1508 roundup_pow_of_two(eager_buffer_size);
1509 eager_buffer_size =
1510 clamp_val(eager_buffer_size,
1511 MIN_EAGER_BUFFER * 8,
1512 MAX_EAGER_BUFFER_TOTAL);
1513 hfi1_early_info(&pdev->dev, "Eager buffer size %u\n",
1514 eager_buffer_size);
1515 } else {
1516 hfi1_early_err(&pdev->dev, "Invalid Eager buffer size of 0\n");
1517 ret = -EINVAL;
1518 goto bail;
1519 }
1520
1521 /* restrict value of hfi1_rcvarr_split */
1522 hfi1_rcvarr_split = clamp_val(hfi1_rcvarr_split, 0, 100);
1523
1524 ret = hfi1_pcie_init(pdev, ent);
1525 if (ret)
1526 goto bail;
1527
Krzysztof Blaszkowski83fb4af2016-10-17 04:19:24 -07001528 /*
1529 * Do device-specific initialization, function table setup, dd
1530 * allocation, etc.
1531 */
1532 dd = hfi1_init_dd(pdev, ent);
1533
1534 if (IS_ERR(dd)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001535 ret = PTR_ERR(dd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001536 goto clean_bail; /* error already printed */
Krzysztof Blaszkowski83fb4af2016-10-17 04:19:24 -07001537 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001538
1539 ret = create_workqueues(dd);
1540 if (ret)
1541 goto clean_bail;
1542
1543 /* do the generic initialization */
1544 initfail = hfi1_init(dd, 0);
1545
Vishwanathapura, Niranjanad4829ea2017-04-12 20:29:28 -07001546 /* setup vnic */
1547 hfi1_vnic_setup(dd);
1548
Mike Marciniszyn77241052015-07-30 15:17:43 -04001549 ret = hfi1_register_ib_device(dd);
1550
1551 /*
1552 * Now ready for use. this should be cleared whenever we
1553 * detect a reset, or initiate one. If earlier failure,
1554 * we still create devices, so diags, etc. can be used
1555 * to determine cause of problem.
1556 */
Dean Luicked6f6532016-02-18 11:12:25 -08001557 if (!initfail && !ret) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001558 dd->flags |= HFI1_INITTED;
Dean Luicked6f6532016-02-18 11:12:25 -08001559 /* create debufs files after init and ib register */
1560 hfi1_dbg_ibdev_init(&dd->verbs_dev);
1561 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001562
1563 j = hfi1_device_create(dd);
1564 if (j)
1565 dd_dev_err(dd, "Failed to create /dev devices: %d\n", -j);
1566
1567 if (initfail || ret) {
1568 stop_timers(dd);
1569 flush_workqueue(ib_wq);
Harish Chegondie8597eb2015-12-01 15:38:20 -05001570 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001571 hfi1_quiet_serdes(dd->pport + pidx);
Harish Chegondie8597eb2015-12-01 15:38:20 -05001572 ppd = dd->pport + pidx;
1573 if (ppd->hfi1_wq) {
1574 destroy_workqueue(ppd->hfi1_wq);
1575 ppd->hfi1_wq = NULL;
1576 }
1577 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001578 if (!j)
1579 hfi1_device_remove(dd);
1580 if (!ret)
1581 hfi1_unregister_ib_device(dd);
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -07001582 hfi1_vnic_cleanup(dd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001583 postinit_cleanup(dd);
1584 if (initfail)
1585 ret = initfail;
1586 goto bail; /* everything already cleaned */
1587 }
1588
1589 sdma_start(dd);
1590
1591 return 0;
1592
1593clean_bail:
1594 hfi1_pcie_cleanup(pdev);
1595bail:
1596 return ret;
1597}
1598
Tadeusz Strukacd7c8f2016-10-25 08:57:55 -07001599static void wait_for_clients(struct hfi1_devdata *dd)
1600{
1601 /*
1602 * Remove the device init value and complete the device if there is
1603 * no clients or wait for active clients to finish.
1604 */
1605 if (atomic_dec_and_test(&dd->user_refcount))
1606 complete(&dd->user_comp);
1607
1608 wait_for_completion(&dd->user_comp);
1609}
1610
Mike Marciniszyn77241052015-07-30 15:17:43 -04001611static void remove_one(struct pci_dev *pdev)
1612{
1613 struct hfi1_devdata *dd = pci_get_drvdata(pdev);
1614
Dean Luicked6f6532016-02-18 11:12:25 -08001615 /* close debugfs files before ib unregister */
1616 hfi1_dbg_ibdev_exit(&dd->verbs_dev);
Tadeusz Strukacd7c8f2016-10-25 08:57:55 -07001617
1618 /* remove the /dev hfi1 interface */
1619 hfi1_device_remove(dd);
1620
1621 /* wait for existing user space clients to finish */
1622 wait_for_clients(dd);
1623
Mike Marciniszyn77241052015-07-30 15:17:43 -04001624 /* unregister from IB core */
1625 hfi1_unregister_ib_device(dd);
1626
Vishwanathapura, Niranjanad4829ea2017-04-12 20:29:28 -07001627 /* cleanup vnic */
1628 hfi1_vnic_cleanup(dd);
1629
Mike Marciniszyn77241052015-07-30 15:17:43 -04001630 /*
1631 * Disable the IB link, disable interrupts on the device,
1632 * clear dma engines, etc.
1633 */
1634 shutdown_device(dd);
1635
1636 stop_timers(dd);
1637
1638 /* wait until all of our (qsfp) queue_work() calls complete */
1639 flush_workqueue(ib_wq);
1640
Mike Marciniszyn77241052015-07-30 15:17:43 -04001641 postinit_cleanup(dd);
1642}
1643
1644/**
1645 * hfi1_create_rcvhdrq - create a receive header queue
1646 * @dd: the hfi1_ib device
1647 * @rcd: the context data
1648 *
1649 * This must be contiguous memory (from an i/o perspective), and must be
1650 * DMA'able (which means for some systems, it will go through an IOMMU,
1651 * or be forced into a low address range).
1652 */
1653int hfi1_create_rcvhdrq(struct hfi1_devdata *dd, struct hfi1_ctxtdata *rcd)
1654{
1655 unsigned amt;
1656 u64 reg;
1657
1658 if (!rcd->rcvhdrq) {
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001659 dma_addr_t dma_hdrqtail;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001660 gfp_t gfp_flags;
1661
1662 /*
1663 * rcvhdrqentsize is in DWs, so we have to convert to bytes
1664 * (* sizeof(u32)).
1665 */
Amitoj Kaur Chawla84449912016-03-04 22:30:43 +05301666 amt = PAGE_ALIGN(rcd->rcvhdrq_cnt * rcd->rcvhdrqentsize *
1667 sizeof(u32));
Mike Marciniszyn77241052015-07-30 15:17:43 -04001668
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -07001669 if ((rcd->ctxt < dd->first_dyn_alloc_ctxt) ||
1670 (rcd->sc && (rcd->sc->type == SC_KERNEL)))
1671 gfp_flags = GFP_KERNEL;
1672 else
1673 gfp_flags = GFP_USER;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001674 rcd->rcvhdrq = dma_zalloc_coherent(
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001675 &dd->pcidev->dev, amt, &rcd->rcvhdrq_dma,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001676 gfp_flags | __GFP_COMP);
1677
1678 if (!rcd->rcvhdrq) {
1679 dd_dev_err(dd,
Jubin John17fb4f22016-02-14 20:21:52 -08001680 "attempt to allocate %d bytes for ctxt %u rcvhdrq failed\n",
1681 amt, rcd->ctxt);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001682 goto bail;
1683 }
1684
Mike Marciniszyn77241052015-07-30 15:17:43 -04001685 if (HFI1_CAP_KGET_MASK(rcd->flags, DMA_RTAIL)) {
1686 rcd->rcvhdrtail_kvaddr = dma_zalloc_coherent(
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001687 &dd->pcidev->dev, PAGE_SIZE, &dma_hdrqtail,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001688 gfp_flags);
1689 if (!rcd->rcvhdrtail_kvaddr)
1690 goto bail_free;
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001691 rcd->rcvhdrqtailaddr_dma = dma_hdrqtail;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001692 }
1693
1694 rcd->rcvhdrq_size = amt;
1695 }
1696 /*
1697 * These values are per-context:
1698 * RcvHdrCnt
1699 * RcvHdrEntSize
1700 * RcvHdrSize
1701 */
1702 reg = ((u64)(rcd->rcvhdrq_cnt >> HDRQ_SIZE_SHIFT)
1703 & RCV_HDR_CNT_CNT_MASK)
1704 << RCV_HDR_CNT_CNT_SHIFT;
1705 write_kctxt_csr(dd, rcd->ctxt, RCV_HDR_CNT, reg);
1706 reg = (encode_rcv_header_entry_size(rcd->rcvhdrqentsize)
1707 & RCV_HDR_ENT_SIZE_ENT_SIZE_MASK)
1708 << RCV_HDR_ENT_SIZE_ENT_SIZE_SHIFT;
1709 write_kctxt_csr(dd, rcd->ctxt, RCV_HDR_ENT_SIZE, reg);
1710 reg = (dd->rcvhdrsize & RCV_HDR_SIZE_HDR_SIZE_MASK)
1711 << RCV_HDR_SIZE_HDR_SIZE_SHIFT;
1712 write_kctxt_csr(dd, rcd->ctxt, RCV_HDR_SIZE, reg);
Mark F. Brown46b010d2015-11-09 19:18:20 -05001713
1714 /*
1715 * Program dummy tail address for every receive context
1716 * before enabling any receive context
1717 */
1718 write_kctxt_csr(dd, rcd->ctxt, RCV_HDR_TAIL_ADDR,
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001719 dd->rcvhdrtail_dummy_dma);
Mark F. Brown46b010d2015-11-09 19:18:20 -05001720
Mike Marciniszyn77241052015-07-30 15:17:43 -04001721 return 0;
1722
1723bail_free:
1724 dd_dev_err(dd,
Jubin John17fb4f22016-02-14 20:21:52 -08001725 "attempt to allocate 1 page for ctxt %u rcvhdrqtailaddr failed\n",
1726 rcd->ctxt);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001727 dma_free_coherent(&dd->pcidev->dev, amt, rcd->rcvhdrq,
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001728 rcd->rcvhdrq_dma);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001729 rcd->rcvhdrq = NULL;
1730bail:
1731 return -ENOMEM;
1732}
1733
1734/**
1735 * allocate eager buffers, both kernel and user contexts.
1736 * @rcd: the context we are setting up.
1737 *
1738 * Allocate the eager TID buffers and program them into hip.
1739 * They are no longer completely contiguous, we do multiple allocation
1740 * calls. Otherwise we get the OOM code involved, by asking for too
1741 * much per call, with disastrous results on some kernels.
1742 */
1743int hfi1_setup_eagerbufs(struct hfi1_ctxtdata *rcd)
1744{
1745 struct hfi1_devdata *dd = rcd->dd;
1746 u32 max_entries, egrtop, alloced_bytes = 0, idx = 0;
1747 gfp_t gfp_flags;
1748 u16 order;
1749 int ret = 0;
1750 u16 round_mtu = roundup_pow_of_two(hfi1_max_mtu);
1751
1752 /*
1753 * GFP_USER, but without GFP_FS, so buffer cache can be
1754 * coalesced (we hope); otherwise, even at order 4,
1755 * heavy filesystem activity makes these fail, and we can
1756 * use compound pages.
1757 */
Mel Gorman71baba42015-11-06 16:28:28 -08001758 gfp_flags = __GFP_RECLAIM | __GFP_IO | __GFP_COMP;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001759
1760 /*
1761 * The minimum size of the eager buffers is a groups of MTU-sized
1762 * buffers.
1763 * The global eager_buffer_size parameter is checked against the
1764 * theoretical lower limit of the value. Here, we check against the
1765 * MTU.
1766 */
1767 if (rcd->egrbufs.size < (round_mtu * dd->rcv_entries.group_size))
1768 rcd->egrbufs.size = round_mtu * dd->rcv_entries.group_size;
1769 /*
1770 * If using one-pkt-per-egr-buffer, lower the eager buffer
1771 * size to the max MTU (page-aligned).
1772 */
1773 if (!HFI1_CAP_KGET_MASK(rcd->flags, MULTI_PKT_EGR))
1774 rcd->egrbufs.rcvtid_size = round_mtu;
1775
1776 /*
1777 * Eager buffers sizes of 1MB or less require smaller TID sizes
1778 * to satisfy the "multiple of 8 RcvArray entries" requirement.
1779 */
1780 if (rcd->egrbufs.size <= (1 << 20))
1781 rcd->egrbufs.rcvtid_size = max((unsigned long)round_mtu,
1782 rounddown_pow_of_two(rcd->egrbufs.size / 8));
1783
1784 while (alloced_bytes < rcd->egrbufs.size &&
1785 rcd->egrbufs.alloced < rcd->egrbufs.count) {
1786 rcd->egrbufs.buffers[idx].addr =
1787 dma_zalloc_coherent(&dd->pcidev->dev,
1788 rcd->egrbufs.rcvtid_size,
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001789 &rcd->egrbufs.buffers[idx].dma,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001790 gfp_flags);
1791 if (rcd->egrbufs.buffers[idx].addr) {
1792 rcd->egrbufs.buffers[idx].len =
1793 rcd->egrbufs.rcvtid_size;
1794 rcd->egrbufs.rcvtids[rcd->egrbufs.alloced].addr =
1795 rcd->egrbufs.buffers[idx].addr;
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001796 rcd->egrbufs.rcvtids[rcd->egrbufs.alloced].dma =
1797 rcd->egrbufs.buffers[idx].dma;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001798 rcd->egrbufs.alloced++;
1799 alloced_bytes += rcd->egrbufs.rcvtid_size;
1800 idx++;
1801 } else {
1802 u32 new_size, i, j;
1803 u64 offset = 0;
1804
1805 /*
1806 * Fail the eager buffer allocation if:
1807 * - we are already using the lowest acceptable size
1808 * - we are using one-pkt-per-egr-buffer (this implies
1809 * that we are accepting only one size)
1810 */
1811 if (rcd->egrbufs.rcvtid_size == round_mtu ||
1812 !HFI1_CAP_KGET_MASK(rcd->flags, MULTI_PKT_EGR)) {
1813 dd_dev_err(dd, "ctxt%u: Failed to allocate eager buffers\n",
Jubin John17fb4f22016-02-14 20:21:52 -08001814 rcd->ctxt);
Michael J. Ruhl94679062017-05-04 05:14:28 -07001815 ret = -ENOMEM;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001816 goto bail_rcvegrbuf_phys;
1817 }
1818
1819 new_size = rcd->egrbufs.rcvtid_size / 2;
1820
1821 /*
1822 * If the first attempt to allocate memory failed, don't
1823 * fail everything but continue with the next lower
1824 * size.
1825 */
1826 if (idx == 0) {
1827 rcd->egrbufs.rcvtid_size = new_size;
1828 continue;
1829 }
1830
1831 /*
1832 * Re-partition already allocated buffers to a smaller
1833 * size.
1834 */
1835 rcd->egrbufs.alloced = 0;
1836 for (i = 0, j = 0, offset = 0; j < idx; i++) {
1837 if (i >= rcd->egrbufs.count)
1838 break;
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001839 rcd->egrbufs.rcvtids[i].dma =
1840 rcd->egrbufs.buffers[j].dma + offset;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001841 rcd->egrbufs.rcvtids[i].addr =
1842 rcd->egrbufs.buffers[j].addr + offset;
1843 rcd->egrbufs.alloced++;
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001844 if ((rcd->egrbufs.buffers[j].dma + offset +
Mike Marciniszyn77241052015-07-30 15:17:43 -04001845 new_size) ==
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001846 (rcd->egrbufs.buffers[j].dma +
Mike Marciniszyn77241052015-07-30 15:17:43 -04001847 rcd->egrbufs.buffers[j].len)) {
1848 j++;
1849 offset = 0;
Jubin Johne4909742016-02-14 20:22:00 -08001850 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001851 offset += new_size;
Jubin Johne4909742016-02-14 20:22:00 -08001852 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001853 }
1854 rcd->egrbufs.rcvtid_size = new_size;
1855 }
1856 }
1857 rcd->egrbufs.numbufs = idx;
1858 rcd->egrbufs.size = alloced_bytes;
1859
Sebastian Sanchez6c63e422015-11-06 20:06:56 -05001860 hfi1_cdbg(PROC,
1861 "ctxt%u: Alloced %u rcv tid entries @ %uKB, total %zuKB\n",
Grzegorz Heldt23002d52016-07-25 13:39:33 -07001862 rcd->ctxt, rcd->egrbufs.alloced,
1863 rcd->egrbufs.rcvtid_size / 1024, rcd->egrbufs.size / 1024);
Sebastian Sanchez6c63e422015-11-06 20:06:56 -05001864
Mike Marciniszyn77241052015-07-30 15:17:43 -04001865 /*
1866 * Set the contexts rcv array head update threshold to the closest
1867 * power of 2 (so we can use a mask instead of modulo) below half
1868 * the allocated entries.
1869 */
1870 rcd->egrbufs.threshold =
1871 rounddown_pow_of_two(rcd->egrbufs.alloced / 2);
1872 /*
1873 * Compute the expected RcvArray entry base. This is done after
1874 * allocating the eager buffers in order to maximize the
1875 * expected RcvArray entries for the context.
1876 */
1877 max_entries = rcd->rcv_array_groups * dd->rcv_entries.group_size;
1878 egrtop = roundup(rcd->egrbufs.alloced, dd->rcv_entries.group_size);
1879 rcd->expected_count = max_entries - egrtop;
1880 if (rcd->expected_count > MAX_TID_PAIR_ENTRIES * 2)
1881 rcd->expected_count = MAX_TID_PAIR_ENTRIES * 2;
1882
1883 rcd->expected_base = rcd->eager_base + egrtop;
Sebastian Sanchez6c63e422015-11-06 20:06:56 -05001884 hfi1_cdbg(PROC, "ctxt%u: eager:%u, exp:%u, egrbase:%u, expbase:%u\n",
1885 rcd->ctxt, rcd->egrbufs.alloced, rcd->expected_count,
1886 rcd->eager_base, rcd->expected_base);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001887
1888 if (!hfi1_rcvbuf_validate(rcd->egrbufs.rcvtid_size, PT_EAGER, &order)) {
Sebastian Sanchez6c63e422015-11-06 20:06:56 -05001889 hfi1_cdbg(PROC,
1890 "ctxt%u: current Eager buffer size is invalid %u\n",
1891 rcd->ctxt, rcd->egrbufs.rcvtid_size);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001892 ret = -EINVAL;
Michael J. Ruhl62239fc2017-05-04 05:15:21 -07001893 goto bail_rcvegrbuf_phys;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001894 }
1895
1896 for (idx = 0; idx < rcd->egrbufs.alloced; idx++) {
1897 hfi1_put_tid(dd, rcd->eager_base + idx, PT_EAGER,
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001898 rcd->egrbufs.rcvtids[idx].dma, order);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001899 cond_resched();
1900 }
Michael J. Ruhl62239fc2017-05-04 05:15:21 -07001901
1902 return 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001903
1904bail_rcvegrbuf_phys:
1905 for (idx = 0; idx < rcd->egrbufs.alloced &&
Jubin John17fb4f22016-02-14 20:21:52 -08001906 rcd->egrbufs.buffers[idx].addr;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001907 idx++) {
1908 dma_free_coherent(&dd->pcidev->dev,
1909 rcd->egrbufs.buffers[idx].len,
1910 rcd->egrbufs.buffers[idx].addr,
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001911 rcd->egrbufs.buffers[idx].dma);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001912 rcd->egrbufs.buffers[idx].addr = NULL;
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001913 rcd->egrbufs.buffers[idx].dma = 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001914 rcd->egrbufs.buffers[idx].len = 0;
1915 }
Michael J. Ruhl62239fc2017-05-04 05:15:21 -07001916
Mike Marciniszyn77241052015-07-30 15:17:43 -04001917 return ret;
1918}