blob: b2db77626fc5437c4761a95b42ad898d1597d6e2 [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>
Dennis Dalessandroec3f2c12016-01-19 14:41:33 -080056#include <rdma/rdma_vt.h>
Mike Marciniszyn77241052015-07-30 15:17:43 -040057
58#include "hfi.h"
59#include "device.h"
60#include "common.h"
Sebastian Sanchez6c63e422015-11-06 20:06:56 -050061#include "trace.h"
Mike Marciniszyn77241052015-07-30 15:17:43 -040062#include "mad.h"
63#include "sdma.h"
64#include "debugfs.h"
65#include "verbs.h"
Ashutosh Dixitaffa48d2016-02-03 14:33:06 -080066#include "aspm.h"
Dennis Dalessandro41973442016-07-25 07:52:36 -070067#include "affinity.h"
Vishwanathapura, Niranjanad4829ea2017-04-12 20:29:28 -070068#include "vnic.h"
Mike Marciniszyn77241052015-07-30 15:17:43 -040069
70#undef pr_fmt
71#define pr_fmt(fmt) DRIVER_NAME ": " fmt
72
Mike Marciniszyndd1ed102017-05-04 05:14:10 -070073#define HFI1_MAX_ACTIVE_WORKQUEUE_ENTRIES 5
Mike Marciniszyn77241052015-07-30 15:17:43 -040074/*
75 * min buffers we want to have per context, after driver
76 */
77#define HFI1_MIN_USER_CTXT_BUFCNT 7
78
79#define HFI1_MIN_HDRQ_EGRBUF_CNT 2
Sebastian Sancheze002dcc2016-02-03 14:34:32 -080080#define HFI1_MAX_HDRQ_EGRBUF_CNT 16352
Mike Marciniszyn77241052015-07-30 15:17:43 -040081#define HFI1_MIN_EAGER_BUFFER_SIZE (4 * 1024) /* 4KB */
82#define HFI1_MAX_EAGER_BUFFER_SIZE (256 * 1024) /* 256KB */
83
84/*
85 * Number of user receive contexts we are configured to use (to allow for more
86 * pio buffers per ctxt, etc.) Zero means use one user context per CPU.
87 */
Sebastian Sanchez2ce6bf22015-12-11 08:44:48 -050088int num_user_contexts = -1;
89module_param_named(num_user_contexts, num_user_contexts, uint, S_IRUGO);
Mike Marciniszyn77241052015-07-30 15:17:43 -040090MODULE_PARM_DESC(
Sebastian Sanchez2ce6bf22015-12-11 08:44:48 -050091 num_user_contexts, "Set max number of user contexts to use");
Mike Marciniszyn77241052015-07-30 15:17:43 -040092
Mark F. Brown5b55ea32016-01-11 18:30:54 -050093uint krcvqs[RXE_NUM_DATA_VL];
Mike Marciniszyn77241052015-07-30 15:17:43 -040094int krcvqsset;
Mark F. Brown5b55ea32016-01-11 18:30:54 -050095module_param_array(krcvqs, uint, &krcvqsset, S_IRUGO);
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -050096MODULE_PARM_DESC(krcvqs, "Array of the number of non-control kernel receive queues by VL");
Mike Marciniszyn77241052015-07-30 15:17:43 -040097
98/* computed based on above array */
Harish Chegondi429b6a72016-08-31 07:24:40 -070099unsigned long n_krcvqs;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400100
101static unsigned hfi1_rcvarr_split = 25;
102module_param_named(rcvarr_split, hfi1_rcvarr_split, uint, S_IRUGO);
103MODULE_PARM_DESC(rcvarr_split, "Percent of context's RcvArray entries used for Eager buffers");
104
Tymoteusz Kielan9746fa42017-05-04 05:14:22 -0700105static uint eager_buffer_size = (8 << 20); /* 8MB */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400106module_param(eager_buffer_size, uint, S_IRUGO);
Tymoteusz Kielan9746fa42017-05-04 05:14:22 -0700107MODULE_PARM_DESC(eager_buffer_size, "Size of the eager buffers, default: 8MB");
Mike Marciniszyn77241052015-07-30 15:17:43 -0400108
109static uint rcvhdrcnt = 2048; /* 2x the max eager buffer count */
110module_param_named(rcvhdrcnt, rcvhdrcnt, uint, S_IRUGO);
111MODULE_PARM_DESC(rcvhdrcnt, "Receive header queue count (default 2048)");
112
113static uint hfi1_hdrq_entsize = 32;
114module_param_named(hdrq_entsize, hfi1_hdrq_entsize, uint, S_IRUGO);
115MODULE_PARM_DESC(hdrq_entsize, "Size of header queue entries: 2 - 8B, 16 - 64B (default), 32 - 128B");
116
117unsigned int user_credit_return_threshold = 33; /* default is 33% */
118module_param(user_credit_return_threshold, uint, S_IRUGO);
Jubin Johnecb95a02015-12-17 19:24:14 -0500119MODULE_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 -0400120
121static inline u64 encode_rcv_header_entry_size(u16);
122
123static struct idr hfi1_unit_table;
124u32 hfi1_cpulist_count;
125unsigned long *hfi1_cpulist;
126
127/*
128 * Common code for creating the receive context array.
129 */
130int hfi1_create_ctxts(struct hfi1_devdata *dd)
131{
132 unsigned i;
133 int ret;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400134
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500135 /* Control context has to be always 0 */
136 BUILD_BUG_ON(HFI1_CTRL_CTXT != 0);
137
Mitko Haralanov377f1112016-02-03 14:33:58 -0800138 dd->rcd = kzalloc_node(dd->num_rcv_contexts * sizeof(*dd->rcd),
139 GFP_KERNEL, dd->node);
Alison Schofield806e6e12015-10-12 14:28:36 -0700140 if (!dd->rcd)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400141 goto nomem;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400142
143 /* create one or more kernel contexts */
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700144 for (i = 0; i < dd->first_dyn_alloc_ctxt; ++i) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400145 struct hfi1_pportdata *ppd;
146 struct hfi1_ctxtdata *rcd;
147
148 ppd = dd->pport + (i % dd->num_pports);
Jianxin Xiong4dfe7cc2016-10-17 04:19:41 -0700149
150 /* dd->rcd[i] gets assigned inside the callee */
Mitko Haralanov957558c2016-02-03 14:33:40 -0800151 rcd = hfi1_create_ctxtdata(ppd, i, dd->node);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400152 if (!rcd) {
153 dd_dev_err(dd,
Jubin John17fb4f22016-02-14 20:21:52 -0800154 "Unable to allocate kernel receive context, failing\n");
Mike Marciniszyn77241052015-07-30 15:17:43 -0400155 goto nomem;
156 }
157 /*
158 * Set up the kernel context flags here and now because they
159 * use default values for all receive side memories. User
160 * contexts will be handled as they are created.
161 */
162 rcd->flags = HFI1_CAP_KGET(MULTI_PKT_EGR) |
163 HFI1_CAP_KGET(NODROP_RHQ_FULL) |
164 HFI1_CAP_KGET(NODROP_EGR_FULL) |
165 HFI1_CAP_KGET(DMA_RTAIL);
Niranjana Vishwanathapura82c26112015-11-11 00:35:19 -0500166
167 /* Control context must use DMA_RTAIL */
168 if (rcd->ctxt == HFI1_CTRL_CTXT)
169 rcd->flags |= HFI1_CAP_DMA_RTAIL;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400170 rcd->seq_cnt = 1;
171
172 rcd->sc = sc_alloc(dd, SC_ACK, rcd->rcvhdrqentsize, dd->node);
173 if (!rcd->sc) {
174 dd_dev_err(dd,
Jubin John17fb4f22016-02-14 20:21:52 -0800175 "Unable to allocate kernel send context, failing\n");
Mike Marciniszyn77241052015-07-30 15:17:43 -0400176 goto nomem;
177 }
178
179 ret = hfi1_init_ctxt(rcd->sc);
180 if (ret < 0) {
181 dd_dev_err(dd,
182 "Failed to setup kernel receive context, failing\n");
Mike Marciniszyn77241052015-07-30 15:17:43 -0400183 ret = -EFAULT;
184 goto bail;
185 }
186 }
187
Ashutosh Dixitaffa48d2016-02-03 14:33:06 -0800188 /*
189 * Initialize aspm, to be done after gen3 transition and setting up
190 * contexts and before enabling interrupts
191 */
192 aspm_init(dd);
193
Mike Marciniszyn77241052015-07-30 15:17:43 -0400194 return 0;
195nomem:
196 ret = -ENOMEM;
197bail:
Jianxin Xiong4dfe7cc2016-10-17 04:19:41 -0700198 if (dd->rcd) {
199 for (i = 0; i < dd->num_rcv_contexts; ++i)
200 hfi1_free_ctxtdata(dd, dd->rcd[i]);
201 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400202 kfree(dd->rcd);
203 dd->rcd = NULL;
204 return ret;
205}
206
207/*
208 * Common code for user and kernel context setup.
209 */
Mitko Haralanov957558c2016-02-03 14:33:40 -0800210struct hfi1_ctxtdata *hfi1_create_ctxtdata(struct hfi1_pportdata *ppd, u32 ctxt,
211 int numa)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400212{
213 struct hfi1_devdata *dd = ppd->dd;
214 struct hfi1_ctxtdata *rcd;
215 unsigned kctxt_ngroups = 0;
216 u32 base;
217
218 if (dd->rcv_entries.nctxt_extra >
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700219 dd->num_rcv_contexts - dd->first_dyn_alloc_ctxt)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400220 kctxt_ngroups = (dd->rcv_entries.nctxt_extra -
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700221 (dd->num_rcv_contexts - dd->first_dyn_alloc_ctxt));
Jianxin Xiong4dfe7cc2016-10-17 04:19:41 -0700222 rcd = kzalloc_node(sizeof(*rcd), GFP_KERNEL, numa);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400223 if (rcd) {
224 u32 rcvtids, max_entries;
225
Sebastian Sanchez6c63e422015-11-06 20:06:56 -0500226 hfi1_cdbg(PROC, "setting up context %u\n", ctxt);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400227
228 INIT_LIST_HEAD(&rcd->qp_wait_list);
229 rcd->ppd = ppd;
230 rcd->dd = dd;
231 rcd->cnt = 1;
232 rcd->ctxt = ctxt;
233 dd->rcd[ctxt] = rcd;
Mitko Haralanov957558c2016-02-03 14:33:40 -0800234 rcd->numa_id = numa;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400235 rcd->rcv_array_groups = dd->rcv_entries.ngroups;
236
Mitko Haralanov463e6eb2016-02-05 11:57:53 -0500237 mutex_init(&rcd->exp_lock);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400238
239 /*
240 * Calculate the context's RcvArray entry starting point.
241 * We do this here because we have to take into account all
242 * the RcvArray entries that previous context would have
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700243 * taken and we have to account for any extra groups assigned
244 * to the static (kernel) or dynamic (vnic/user) contexts.
Mike Marciniszyn77241052015-07-30 15:17:43 -0400245 */
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700246 if (ctxt < dd->first_dyn_alloc_ctxt) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400247 if (ctxt < kctxt_ngroups) {
248 base = ctxt * (dd->rcv_entries.ngroups + 1);
249 rcd->rcv_array_groups++;
Dennis Dalessandroee495ad2017-04-09 10:17:18 -0700250 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400251 base = kctxt_ngroups +
252 (ctxt * dd->rcv_entries.ngroups);
Dennis Dalessandroee495ad2017-04-09 10:17:18 -0700253 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400254 } else {
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700255 u16 ct = ctxt - dd->first_dyn_alloc_ctxt;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400256
257 base = ((dd->n_krcv_queues * dd->rcv_entries.ngroups) +
258 kctxt_ngroups);
259 if (ct < dd->rcv_entries.nctxt_extra) {
260 base += ct * (dd->rcv_entries.ngroups + 1);
261 rcd->rcv_array_groups++;
Dennis Dalessandroee495ad2017-04-09 10:17:18 -0700262 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400263 base += dd->rcv_entries.nctxt_extra +
264 (ct * dd->rcv_entries.ngroups);
Dennis Dalessandroee495ad2017-04-09 10:17:18 -0700265 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400266 }
267 rcd->eager_base = base * dd->rcv_entries.group_size;
268
Mike Marciniszyn77241052015-07-30 15:17:43 -0400269 rcd->rcvhdrq_cnt = rcvhdrcnt;
270 rcd->rcvhdrqentsize = hfi1_hdrq_entsize;
271 /*
272 * Simple Eager buffer allocation: we have already pre-allocated
273 * the number of RcvArray entry groups. Each ctxtdata structure
274 * holds the number of groups for that context.
275 *
276 * To follow CSR requirements and maintain cacheline alignment,
277 * make sure all sizes and bases are multiples of group_size.
278 *
279 * The expected entry count is what is left after assigning
280 * eager.
281 */
282 max_entries = rcd->rcv_array_groups *
283 dd->rcv_entries.group_size;
284 rcvtids = ((max_entries * hfi1_rcvarr_split) / 100);
285 rcd->egrbufs.count = round_down(rcvtids,
286 dd->rcv_entries.group_size);
287 if (rcd->egrbufs.count > MAX_EAGER_ENTRIES) {
288 dd_dev_err(dd, "ctxt%u: requested too many RcvArray entries.\n",
289 rcd->ctxt);
290 rcd->egrbufs.count = MAX_EAGER_ENTRIES;
291 }
Sebastian Sanchez6c63e422015-11-06 20:06:56 -0500292 hfi1_cdbg(PROC,
293 "ctxt%u: max Eager buffer RcvArray entries: %u\n",
294 rcd->ctxt, rcd->egrbufs.count);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400295
296 /*
297 * Allocate array that will hold the eager buffer accounting
298 * data.
299 * This will allocate the maximum possible buffer count based
300 * on the value of the RcvArray split parameter.
301 * The resulting value will be rounded down to the closest
302 * multiple of dd->rcv_entries.group_size.
303 */
Sebastian Sanchezb448bf92017-02-08 05:26:37 -0800304 rcd->egrbufs.buffers = kzalloc_node(
305 rcd->egrbufs.count * sizeof(*rcd->egrbufs.buffers),
306 GFP_KERNEL, numa);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400307 if (!rcd->egrbufs.buffers)
308 goto bail;
Sebastian Sanchezb448bf92017-02-08 05:26:37 -0800309 rcd->egrbufs.rcvtids = kzalloc_node(
310 rcd->egrbufs.count *
311 sizeof(*rcd->egrbufs.rcvtids),
312 GFP_KERNEL, numa);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400313 if (!rcd->egrbufs.rcvtids)
314 goto bail;
315 rcd->egrbufs.size = eager_buffer_size;
316 /*
317 * The size of the buffers programmed into the RcvArray
318 * entries needs to be big enough to handle the highest
319 * MTU supported.
320 */
321 if (rcd->egrbufs.size < hfi1_max_mtu) {
322 rcd->egrbufs.size = __roundup_pow_of_two(hfi1_max_mtu);
Sebastian Sanchez6c63e422015-11-06 20:06:56 -0500323 hfi1_cdbg(PROC,
324 "ctxt%u: eager bufs size too small. Adjusting to %zu\n",
Mike Marciniszyn77241052015-07-30 15:17:43 -0400325 rcd->ctxt, rcd->egrbufs.size);
326 }
327 rcd->egrbufs.rcvtid_size = HFI1_MAX_EAGER_BUFFER_SIZE;
328
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700329 /* Applicable only for statically created kernel contexts */
330 if (ctxt < dd->first_dyn_alloc_ctxt) {
Sebastian Sanchezb448bf92017-02-08 05:26:37 -0800331 rcd->opstats = kzalloc_node(sizeof(*rcd->opstats),
332 GFP_KERNEL, numa);
Alison Schofield806e6e12015-10-12 14:28:36 -0700333 if (!rcd->opstats)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400334 goto bail;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400335 }
336 }
337 return rcd;
338bail:
Jakub Pawlak3a6982d2016-09-25 07:42:23 -0700339 dd->rcd[ctxt] = NULL;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400340 kfree(rcd->egrbufs.rcvtids);
341 kfree(rcd->egrbufs.buffers);
342 kfree(rcd);
343 return NULL;
344}
345
346/*
347 * Convert a receive header entry size that to the encoding used in the CSR.
348 *
349 * Return a zero if the given size is invalid.
350 */
351static inline u64 encode_rcv_header_entry_size(u16 size)
352{
353 /* there are only 3 valid receive header entry sizes */
354 if (size == 2)
355 return 1;
356 if (size == 16)
357 return 2;
358 else if (size == 32)
359 return 4;
360 return 0; /* invalid */
361}
362
363/*
364 * Select the largest ccti value over all SLs to determine the intra-
365 * packet gap for the link.
366 *
367 * called with cca_timer_lock held (to protect access to cca_timer
368 * array), and rcu_read_lock() (to protect access to cc_state).
369 */
370void set_link_ipg(struct hfi1_pportdata *ppd)
371{
372 struct hfi1_devdata *dd = ppd->dd;
373 struct cc_state *cc_state;
374 int i;
375 u16 cce, ccti_limit, max_ccti = 0;
376 u16 shift, mult;
377 u64 src;
378 u32 current_egress_rate; /* Mbits /sec */
379 u32 max_pkt_time;
380 /*
381 * max_pkt_time is the maximum packet egress time in units
382 * of the fabric clock period 1/(805 MHz).
383 */
384
385 cc_state = get_cc_state(ppd);
386
Jubin Johnd125a6c2016-02-14 20:19:49 -0800387 if (!cc_state)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400388 /*
389 * This should _never_ happen - rcu_read_lock() is held,
390 * and set_link_ipg() should not be called if cc_state
391 * is NULL.
392 */
393 return;
394
395 for (i = 0; i < OPA_MAX_SLS; i++) {
396 u16 ccti = ppd->cca_timer[i].ccti;
397
398 if (ccti > max_ccti)
399 max_ccti = ccti;
400 }
401
402 ccti_limit = cc_state->cct.ccti_limit;
403 if (max_ccti > ccti_limit)
404 max_ccti = ccti_limit;
405
406 cce = cc_state->cct.entries[max_ccti].entry;
407 shift = (cce & 0xc000) >> 14;
408 mult = (cce & 0x3fff);
409
410 current_egress_rate = active_egress_rate(ppd);
411
412 max_pkt_time = egress_cycles(ppd->ibmaxlen, current_egress_rate);
413
414 src = (max_pkt_time >> shift) * mult;
415
416 src &= SEND_STATIC_RATE_CONTROL_CSR_SRC_RELOAD_SMASK;
417 src <<= SEND_STATIC_RATE_CONTROL_CSR_SRC_RELOAD_SHIFT;
418
419 write_csr(dd, SEND_STATIC_RATE_CONTROL, src);
420}
421
422static enum hrtimer_restart cca_timer_fn(struct hrtimer *t)
423{
424 struct cca_timer *cca_timer;
425 struct hfi1_pportdata *ppd;
426 int sl;
Jubin Johnd35cf7442016-04-14 08:31:53 -0700427 u16 ccti_timer, ccti_min;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400428 struct cc_state *cc_state;
Dean Luickb77d7132015-10-26 10:28:43 -0400429 unsigned long flags;
Jubin Johnd35cf7442016-04-14 08:31:53 -0700430 enum hrtimer_restart ret = HRTIMER_NORESTART;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400431
432 cca_timer = container_of(t, struct cca_timer, hrtimer);
433 ppd = cca_timer->ppd;
434 sl = cca_timer->sl;
435
436 rcu_read_lock();
437
438 cc_state = get_cc_state(ppd);
439
Jubin Johnd125a6c2016-02-14 20:19:49 -0800440 if (!cc_state) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400441 rcu_read_unlock();
442 return HRTIMER_NORESTART;
443 }
444
445 /*
446 * 1) decrement ccti for SL
447 * 2) calculate IPG for link (set_link_ipg())
448 * 3) restart timer, unless ccti is at min value
449 */
450
451 ccti_min = cc_state->cong_setting.entries[sl].ccti_min;
452 ccti_timer = cc_state->cong_setting.entries[sl].ccti_timer;
453
Dean Luickb77d7132015-10-26 10:28:43 -0400454 spin_lock_irqsave(&ppd->cca_timer_lock, flags);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400455
Jubin Johnd35cf7442016-04-14 08:31:53 -0700456 if (cca_timer->ccti > ccti_min) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400457 cca_timer->ccti--;
458 set_link_ipg(ppd);
459 }
460
Jubin Johnd35cf7442016-04-14 08:31:53 -0700461 if (cca_timer->ccti > ccti_min) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400462 unsigned long nsec = 1024 * ccti_timer;
463 /* ccti_timer is in units of 1.024 usec */
464 hrtimer_forward_now(t, ns_to_ktime(nsec));
Jubin Johnd35cf7442016-04-14 08:31:53 -0700465 ret = HRTIMER_RESTART;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400466 }
Jubin Johnd35cf7442016-04-14 08:31:53 -0700467
468 spin_unlock_irqrestore(&ppd->cca_timer_lock, flags);
469 rcu_read_unlock();
470 return ret;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400471}
472
473/*
474 * Common code for initializing the physical port structure.
475 */
476void hfi1_init_pportdata(struct pci_dev *pdev, struct hfi1_pportdata *ppd,
477 struct hfi1_devdata *dd, u8 hw_pidx, u8 port)
478{
Jianxin Xiong8adf71f2016-07-25 13:39:14 -0700479 int i;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400480 uint default_pkey_idx;
Jianxin Xiong8adf71f2016-07-25 13:39:14 -0700481 struct cc_state *cc_state;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400482
483 ppd->dd = dd;
484 ppd->hw_pidx = hw_pidx;
485 ppd->port = port; /* IB port number, not index */
486
487 default_pkey_idx = 1;
488
489 ppd->pkeys[default_pkey_idx] = DEFAULT_P_KEY;
Neel Desai53526502017-04-09 10:16:59 -0700490 ppd->part_enforce |= HFI1_PART_ENFORCE_IN;
491 ppd->part_enforce |= HFI1_PART_ENFORCE_OUT;
492
Mike Marciniszyn77241052015-07-30 15:17:43 -0400493 if (loopback) {
494 hfi1_early_err(&pdev->dev,
495 "Faking data partition 0x8001 in idx %u\n",
496 !default_pkey_idx);
497 ppd->pkeys[!default_pkey_idx] = 0x8001;
498 }
499
500 INIT_WORK(&ppd->link_vc_work, handle_verify_cap);
501 INIT_WORK(&ppd->link_up_work, handle_link_up);
502 INIT_WORK(&ppd->link_down_work, handle_link_down);
503 INIT_WORK(&ppd->freeze_work, handle_freeze);
504 INIT_WORK(&ppd->link_downgrade_work, handle_link_downgrade);
505 INIT_WORK(&ppd->sma_message_work, handle_sma_message);
506 INIT_WORK(&ppd->link_bounce_work, handle_link_bounce);
Dean Luick673b9752016-08-31 07:24:33 -0700507 INIT_DELAYED_WORK(&ppd->start_link_work, handle_start_link);
Jim Snowfb9036d2016-01-11 18:32:21 -0500508 INIT_WORK(&ppd->linkstate_active_work, receive_interrupt_work);
Easwar Hariharan8ebd4cf2016-02-03 14:31:14 -0800509 INIT_WORK(&ppd->qsfp_info.qsfp_work, qsfp_event);
510
Mike Marciniszyn77241052015-07-30 15:17:43 -0400511 mutex_init(&ppd->hls_lock);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400512 spin_lock_init(&ppd->qsfp_info.qsfp_lock);
513
Easwar Hariharan8ebd4cf2016-02-03 14:31:14 -0800514 ppd->qsfp_info.ppd = ppd;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400515 ppd->sm_trap_qp = 0x0;
516 ppd->sa_qp = 0x1;
517
518 ppd->hfi1_wq = NULL;
519
520 spin_lock_init(&ppd->cca_timer_lock);
521
522 for (i = 0; i < OPA_MAX_SLS; i++) {
523 hrtimer_init(&ppd->cca_timer[i].hrtimer, CLOCK_MONOTONIC,
524 HRTIMER_MODE_REL);
525 ppd->cca_timer[i].ppd = ppd;
526 ppd->cca_timer[i].sl = i;
527 ppd->cca_timer[i].ccti = 0;
528 ppd->cca_timer[i].hrtimer.function = cca_timer_fn;
529 }
530
531 ppd->cc_max_table_entries = IB_CC_TABLE_CAP_DEFAULT;
532
533 spin_lock_init(&ppd->cc_state_lock);
534 spin_lock_init(&ppd->cc_log_lock);
Jianxin Xiong8adf71f2016-07-25 13:39:14 -0700535 cc_state = kzalloc(sizeof(*cc_state), GFP_KERNEL);
536 RCU_INIT_POINTER(ppd->cc_state, cc_state);
537 if (!cc_state)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400538 goto bail;
539 return;
540
541bail:
542
543 hfi1_early_err(&pdev->dev,
544 "Congestion Control Agent disabled for port %d\n", port);
545}
546
547/*
548 * Do initialization for device that is only needed on
549 * first detect, not on resets.
550 */
551static int loadtime_init(struct hfi1_devdata *dd)
552{
553 return 0;
554}
555
556/**
557 * init_after_reset - re-initialize after a reset
558 * @dd: the hfi1_ib device
559 *
560 * sanity check at least some of the values after reset, and
561 * ensure no receive or transmit (explicitly, in case reset
562 * failed
563 */
564static int init_after_reset(struct hfi1_devdata *dd)
565{
566 int i;
567
568 /*
569 * Ensure chip does no sends or receives, tail updates, or
570 * pioavail updates while we re-initialize. This is mostly
571 * for the driver data structures, not chip registers.
572 */
573 for (i = 0; i < dd->num_rcv_contexts; i++)
574 hfi1_rcvctrl(dd, HFI1_RCVCTRL_CTXT_DIS |
575 HFI1_RCVCTRL_INTRAVAIL_DIS |
576 HFI1_RCVCTRL_TAILUPD_DIS, i);
577 pio_send_control(dd, PSC_GLOBAL_DISABLE);
578 for (i = 0; i < dd->num_send_contexts; i++)
579 sc_disable(dd->send_contexts[i].sc);
580
581 return 0;
582}
583
584static void enable_chip(struct hfi1_devdata *dd)
585{
586 u32 rcvmask;
587 u32 i;
588
589 /* enable PIO send */
590 pio_send_control(dd, PSC_GLOBAL_ENABLE);
591
592 /*
593 * Enable kernel ctxts' receive and receive interrupt.
594 * Other ctxts done as user opens and initializes them.
595 */
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700596 for (i = 0; i < dd->first_dyn_alloc_ctxt; ++i) {
Mitko Haralanov566c1572016-02-03 14:32:49 -0800597 rcvmask = HFI1_RCVCTRL_CTXT_ENB | HFI1_RCVCTRL_INTRAVAIL_ENB;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400598 rcvmask |= HFI1_CAP_KGET_MASK(dd->rcd[i]->flags, DMA_RTAIL) ?
599 HFI1_RCVCTRL_TAILUPD_ENB : HFI1_RCVCTRL_TAILUPD_DIS;
600 if (!HFI1_CAP_KGET_MASK(dd->rcd[i]->flags, MULTI_PKT_EGR))
601 rcvmask |= HFI1_RCVCTRL_ONE_PKT_EGR_ENB;
602 if (HFI1_CAP_KGET_MASK(dd->rcd[i]->flags, NODROP_RHQ_FULL))
603 rcvmask |= HFI1_RCVCTRL_NO_RHQ_DROP_ENB;
604 if (HFI1_CAP_KGET_MASK(dd->rcd[i]->flags, NODROP_EGR_FULL))
605 rcvmask |= HFI1_RCVCTRL_NO_EGR_DROP_ENB;
606 hfi1_rcvctrl(dd, rcvmask, i);
607 sc_enable(dd->rcd[i]->sc);
608 }
609}
610
611/**
612 * create_workqueues - create per port workqueues
613 * @dd: the hfi1_ib device
614 */
615static int create_workqueues(struct hfi1_devdata *dd)
616{
617 int pidx;
618 struct hfi1_pportdata *ppd;
619
620 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
621 ppd = dd->pport + pidx;
622 if (!ppd->hfi1_wq) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400623 ppd->hfi1_wq =
Mike Marciniszyn0a226ed2015-11-09 19:13:58 -0500624 alloc_workqueue(
625 "hfi%d_%d",
626 WQ_SYSFS | WQ_HIGHPRI | WQ_CPU_INTENSIVE,
Mike Marciniszyndd1ed102017-05-04 05:14:10 -0700627 HFI1_MAX_ACTIVE_WORKQUEUE_ENTRIES,
Mike Marciniszyn0a226ed2015-11-09 19:13:58 -0500628 dd->unit, pidx);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400629 if (!ppd->hfi1_wq)
630 goto wq_error;
631 }
632 }
633 return 0;
634wq_error:
Mike Marciniszyn0a226ed2015-11-09 19:13:58 -0500635 pr_err("alloc_workqueue failed for port %d\n", pidx + 1);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400636 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
637 ppd = dd->pport + pidx;
638 if (ppd->hfi1_wq) {
639 destroy_workqueue(ppd->hfi1_wq);
640 ppd->hfi1_wq = NULL;
641 }
642 }
643 return -ENOMEM;
644}
645
646/**
647 * hfi1_init - do the actual initialization sequence on the chip
648 * @dd: the hfi1_ib device
649 * @reinit: re-initializing, so don't allocate new memory
650 *
651 * Do the actual initialization sequence on the chip. This is done
652 * both from the init routine called from the PCI infrastructure, and
653 * when we reset the chip, or detect that it was reset internally,
654 * or it's administratively re-enabled.
655 *
656 * Memory allocation here and in called routines is only done in
657 * the first case (reinit == 0). We have to be careful, because even
658 * without memory allocation, we need to re-write all the chip registers
659 * TIDs, etc. after the reset or enable has completed.
660 */
661int hfi1_init(struct hfi1_devdata *dd, int reinit)
662{
663 int ret = 0, pidx, lastfail = 0;
664 unsigned i, len;
665 struct hfi1_ctxtdata *rcd;
666 struct hfi1_pportdata *ppd;
667
668 /* Set up recv low level handlers */
669 dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_EXPECTED] =
670 kdeth_process_expected;
671 dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_EAGER] =
672 kdeth_process_eager;
673 dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_IB] = process_receive_ib;
674 dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_ERROR] =
675 process_receive_error;
676 dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_BYPASS] =
677 process_receive_bypass;
678 dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_INVALID5] =
679 process_receive_invalid;
680 dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_INVALID6] =
681 process_receive_invalid;
682 dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_INVALID7] =
683 process_receive_invalid;
684 dd->rhf_rcv_function_map = dd->normal_rhf_rcv_functions;
685
686 /* Set up send low level handlers */
687 dd->process_pio_send = hfi1_verbs_send_pio;
688 dd->process_dma_send = hfi1_verbs_send_dma;
689 dd->pio_inline_send = pio_copy;
Vishwanathapura, Niranjana64551ed2017-04-12 20:29:30 -0700690 dd->process_vnic_dma_send = hfi1_vnic_send_dma;
Mike Marciniszyn77241052015-07-30 15:17:43 -0400691
Mike Marciniszyn995deaf2015-11-16 21:59:29 -0500692 if (is_ax(dd)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400693 atomic_set(&dd->drop_packet, DROP_PACKET_ON);
694 dd->do_drop = 1;
695 } else {
696 atomic_set(&dd->drop_packet, DROP_PACKET_OFF);
697 dd->do_drop = 0;
698 }
699
700 /* make sure the link is not "up" */
701 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
702 ppd = dd->pport + pidx;
703 ppd->linkup = 0;
704 }
705
706 if (reinit)
707 ret = init_after_reset(dd);
708 else
709 ret = loadtime_init(dd);
710 if (ret)
711 goto done;
712
Mark F. Brown46b010d2015-11-09 19:18:20 -0500713 /* allocate dummy tail memory for all receive contexts */
714 dd->rcvhdrtail_dummy_kvaddr = dma_zalloc_coherent(
715 &dd->pcidev->dev, sizeof(u64),
Tymoteusz Kielan60368182016-09-06 04:35:54 -0700716 &dd->rcvhdrtail_dummy_dma,
Mark F. Brown46b010d2015-11-09 19:18:20 -0500717 GFP_KERNEL);
718
719 if (!dd->rcvhdrtail_dummy_kvaddr) {
720 dd_dev_err(dd, "cannot allocate dummy tail memory\n");
721 ret = -ENOMEM;
722 goto done;
723 }
724
Mike Marciniszyn77241052015-07-30 15:17:43 -0400725 /* dd->rcd can be NULL if early initialization failed */
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -0700726 for (i = 0; dd->rcd && i < dd->first_dyn_alloc_ctxt; ++i) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400727 /*
728 * Set up the (kernel) rcvhdr queue and egr TIDs. If doing
729 * re-init, the simplest way to handle this is to free
730 * existing, and re-allocate.
731 * Need to re-create rest of ctxt 0 ctxtdata as well.
732 */
733 rcd = dd->rcd[i];
734 if (!rcd)
735 continue;
736
737 rcd->do_interrupt = &handle_receive_interrupt;
738
739 lastfail = hfi1_create_rcvhdrq(dd, rcd);
740 if (!lastfail)
741 lastfail = hfi1_setup_eagerbufs(rcd);
Ashutosh Dixit39239792016-05-12 10:24:00 -0700742 if (lastfail) {
Mike Marciniszyn77241052015-07-30 15:17:43 -0400743 dd_dev_err(dd,
Jubin John17fb4f22016-02-14 20:21:52 -0800744 "failed to allocate kernel ctxt's rcvhdrq and/or egr bufs\n");
Ashutosh Dixit39239792016-05-12 10:24:00 -0700745 ret = lastfail;
746 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400747 }
Mike Marciniszyn77241052015-07-30 15:17:43 -0400748
749 /* Allocate enough memory for user event notification. */
Amitoj Kaur Chawla84449912016-03-04 22:30:43 +0530750 len = PAGE_ALIGN(dd->chip_rcv_contexts * HFI1_MAX_SHARED_CTXTS *
751 sizeof(*dd->events));
Mike Marciniszyn77241052015-07-30 15:17:43 -0400752 dd->events = vmalloc_user(len);
753 if (!dd->events)
754 dd_dev_err(dd, "Failed to allocate user events page\n");
755 /*
756 * Allocate a page for device and port status.
757 * Page will be shared amongst all user processes.
758 */
759 dd->status = vmalloc_user(PAGE_SIZE);
760 if (!dd->status)
761 dd_dev_err(dd, "Failed to allocate dev status page\n");
762 else
763 dd->freezelen = PAGE_SIZE - (sizeof(*dd->status) -
764 sizeof(dd->status->freezemsg));
765 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
766 ppd = dd->pport + pidx;
767 if (dd->status)
768 /* Currently, we only have one port */
769 ppd->statusp = &dd->status->port;
770
771 set_mtu(ppd);
772 }
773
774 /* enable chip even if we have an error, so we can debug cause */
775 enable_chip(dd);
776
Mike Marciniszyn77241052015-07-30 15:17:43 -0400777done:
778 /*
779 * Set status even if port serdes is not initialized
780 * so that diags will work.
781 */
782 if (dd->status)
783 dd->status->dev |= HFI1_STATUS_CHIP_PRESENT |
784 HFI1_STATUS_INITTED;
785 if (!ret) {
786 /* enable all interrupts from the chip */
787 set_intr_state(dd, 1);
788
789 /* chip is OK for user apps; mark it as initialized */
790 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
791 ppd = dd->pport + pidx;
792
Jubin John4d114fd2016-02-14 20:21:43 -0800793 /*
794 * start the serdes - must be after interrupts are
795 * enabled so we are notified when the link goes up
Mike Marciniszyn77241052015-07-30 15:17:43 -0400796 */
Mike Marciniszyn77241052015-07-30 15:17:43 -0400797 lastfail = bringup_serdes(ppd);
798 if (lastfail)
799 dd_dev_info(dd,
Jubin John17fb4f22016-02-14 20:21:52 -0800800 "Failed to bring up port %u\n",
801 ppd->port);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400802
803 /*
804 * Set status even if port serdes is not initialized
805 * so that diags will work.
806 */
807 if (ppd->statusp)
808 *ppd->statusp |= HFI1_STATUS_CHIP_PRESENT |
809 HFI1_STATUS_INITTED;
810 if (!ppd->link_speed_enabled)
811 continue;
812 }
813 }
814
815 /* if ret is non-zero, we probably should do some cleanup here... */
816 return ret;
817}
818
819static inline struct hfi1_devdata *__hfi1_lookup(int unit)
820{
821 return idr_find(&hfi1_unit_table, unit);
822}
823
824struct hfi1_devdata *hfi1_lookup(int unit)
825{
826 struct hfi1_devdata *dd;
827 unsigned long flags;
828
829 spin_lock_irqsave(&hfi1_devs_lock, flags);
830 dd = __hfi1_lookup(unit);
831 spin_unlock_irqrestore(&hfi1_devs_lock, flags);
832
833 return dd;
834}
835
836/*
837 * Stop the timers during unit shutdown, or after an error late
838 * in initialization.
839 */
840static void stop_timers(struct hfi1_devdata *dd)
841{
842 struct hfi1_pportdata *ppd;
843 int pidx;
844
845 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
846 ppd = dd->pport + pidx;
847 if (ppd->led_override_timer.data) {
848 del_timer_sync(&ppd->led_override_timer);
849 atomic_set(&ppd->led_override_timer_active, 0);
850 }
851 }
852}
853
854/**
855 * shutdown_device - shut down a device
856 * @dd: the hfi1_ib device
857 *
858 * This is called to make the device quiet when we are about to
859 * unload the driver, and also when the device is administratively
860 * disabled. It does not free any data structures.
861 * Everything it does has to be setup again by hfi1_init(dd, 1)
862 */
863static void shutdown_device(struct hfi1_devdata *dd)
864{
865 struct hfi1_pportdata *ppd;
866 unsigned pidx;
867 int i;
868
869 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
870 ppd = dd->pport + pidx;
871
872 ppd->linkup = 0;
873 if (ppd->statusp)
874 *ppd->statusp &= ~(HFI1_STATUS_IB_CONF |
875 HFI1_STATUS_IB_READY);
876 }
877 dd->flags &= ~HFI1_INITTED;
878
879 /* mask interrupts, but not errors */
880 set_intr_state(dd, 0);
881
882 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
883 ppd = dd->pport + pidx;
884 for (i = 0; i < dd->num_rcv_contexts; i++)
885 hfi1_rcvctrl(dd, HFI1_RCVCTRL_TAILUPD_DIS |
886 HFI1_RCVCTRL_CTXT_DIS |
887 HFI1_RCVCTRL_INTRAVAIL_DIS |
888 HFI1_RCVCTRL_PKEY_DIS |
889 HFI1_RCVCTRL_ONE_PKT_EGR_DIS, i);
890 /*
891 * Gracefully stop all sends allowing any in progress to
892 * trickle out first.
893 */
894 for (i = 0; i < dd->num_send_contexts; i++)
895 sc_flush(dd->send_contexts[i].sc);
896 }
897
898 /*
899 * Enough for anything that's going to trickle out to have actually
900 * done so.
901 */
902 udelay(20);
903
904 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
905 ppd = dd->pport + pidx;
906
907 /* disable all contexts */
908 for (i = 0; i < dd->num_send_contexts; i++)
909 sc_disable(dd->send_contexts[i].sc);
910 /* disable the send device */
911 pio_send_control(dd, PSC_GLOBAL_DISABLE);
912
Easwar Hariharan91ab4ed2016-02-03 14:35:57 -0800913 shutdown_led_override(ppd);
914
Mike Marciniszyn77241052015-07-30 15:17:43 -0400915 /*
916 * Clear SerdesEnable.
917 * We can't count on interrupts since we are stopping.
918 */
919 hfi1_quiet_serdes(ppd);
920
921 if (ppd->hfi1_wq) {
922 destroy_workqueue(ppd->hfi1_wq);
923 ppd->hfi1_wq = NULL;
924 }
925 }
926 sdma_exit(dd);
927}
928
929/**
930 * hfi1_free_ctxtdata - free a context's allocated data
931 * @dd: the hfi1_ib device
932 * @rcd: the ctxtdata structure
933 *
934 * free up any allocated data for a context
935 * This should not touch anything that would affect a simultaneous
936 * re-allocation of context data, because it is called after hfi1_mutex
937 * is released (and can be called from reinit as well).
938 * It should never change any chip state, or global driver state.
939 */
940void hfi1_free_ctxtdata(struct hfi1_devdata *dd, struct hfi1_ctxtdata *rcd)
941{
942 unsigned e;
943
944 if (!rcd)
945 return;
946
947 if (rcd->rcvhdrq) {
948 dma_free_coherent(&dd->pcidev->dev, rcd->rcvhdrq_size,
Tymoteusz Kielan60368182016-09-06 04:35:54 -0700949 rcd->rcvhdrq, rcd->rcvhdrq_dma);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400950 rcd->rcvhdrq = NULL;
951 if (rcd->rcvhdrtail_kvaddr) {
952 dma_free_coherent(&dd->pcidev->dev, PAGE_SIZE,
953 (void *)rcd->rcvhdrtail_kvaddr,
Tymoteusz Kielan60368182016-09-06 04:35:54 -0700954 rcd->rcvhdrqtailaddr_dma);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400955 rcd->rcvhdrtail_kvaddr = NULL;
956 }
957 }
958
959 /* all the RcvArray entries should have been cleared by now */
960 kfree(rcd->egrbufs.rcvtids);
961
962 for (e = 0; e < rcd->egrbufs.alloced; e++) {
Tymoteusz Kielan60368182016-09-06 04:35:54 -0700963 if (rcd->egrbufs.buffers[e].dma)
Mike Marciniszyn77241052015-07-30 15:17:43 -0400964 dma_free_coherent(&dd->pcidev->dev,
965 rcd->egrbufs.buffers[e].len,
966 rcd->egrbufs.buffers[e].addr,
Tymoteusz Kielan60368182016-09-06 04:35:54 -0700967 rcd->egrbufs.buffers[e].dma);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400968 }
969 kfree(rcd->egrbufs.buffers);
970
971 sc_free(rcd->sc);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400972 vfree(rcd->user_event_mask);
973 vfree(rcd->subctxt_uregbase);
974 vfree(rcd->subctxt_rcvegrbuf);
975 vfree(rcd->subctxt_rcvhdr_base);
Mike Marciniszyn77241052015-07-30 15:17:43 -0400976 kfree(rcd->opstats);
977 kfree(rcd);
978}
979
Dean Luick78eb1292016-03-05 08:49:45 -0800980/*
981 * Release our hold on the shared asic data. If we are the last one,
Dean Luickdba715f2016-07-06 17:28:52 -0400982 * return the structure to be finalized outside the lock. Must be
983 * holding hfi1_devs_lock.
Dean Luick78eb1292016-03-05 08:49:45 -0800984 */
Dean Luickdba715f2016-07-06 17:28:52 -0400985static struct hfi1_asic_data *release_asic_data(struct hfi1_devdata *dd)
Dean Luick78eb1292016-03-05 08:49:45 -0800986{
Dean Luickdba715f2016-07-06 17:28:52 -0400987 struct hfi1_asic_data *ad;
Dean Luick78eb1292016-03-05 08:49:45 -0800988 int other;
989
990 if (!dd->asic_data)
Dean Luickdba715f2016-07-06 17:28:52 -0400991 return NULL;
Dean Luick78eb1292016-03-05 08:49:45 -0800992 dd->asic_data->dds[dd->hfi1_id] = NULL;
993 other = dd->hfi1_id ? 0 : 1;
Dean Luickdba715f2016-07-06 17:28:52 -0400994 ad = dd->asic_data;
Dean Luick78eb1292016-03-05 08:49:45 -0800995 dd->asic_data = NULL;
Dean Luickdba715f2016-07-06 17:28:52 -0400996 /* return NULL if the other dd still has a link */
997 return ad->dds[other] ? NULL : ad;
998}
999
1000static void finalize_asic_data(struct hfi1_devdata *dd,
1001 struct hfi1_asic_data *ad)
1002{
1003 clean_up_i2c(dd, ad);
1004 kfree(ad);
Dean Luick78eb1292016-03-05 08:49:45 -08001005}
1006
Dennis Dalessandroe11ffbd2016-05-19 05:26:44 -07001007static void __hfi1_free_devdata(struct kobject *kobj)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001008{
Dennis Dalessandroe11ffbd2016-05-19 05:26:44 -07001009 struct hfi1_devdata *dd =
1010 container_of(kobj, struct hfi1_devdata, kobj);
Dean Luickdba715f2016-07-06 17:28:52 -04001011 struct hfi1_asic_data *ad;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001012 unsigned long flags;
1013
1014 spin_lock_irqsave(&hfi1_devs_lock, flags);
1015 idr_remove(&hfi1_unit_table, dd->unit);
1016 list_del(&dd->list);
Dean Luickdba715f2016-07-06 17:28:52 -04001017 ad = release_asic_data(dd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001018 spin_unlock_irqrestore(&hfi1_devs_lock, flags);
Dean Luickdba715f2016-07-06 17:28:52 -04001019 if (ad)
1020 finalize_asic_data(dd, ad);
Easwar Hariharanc3838b32016-02-09 14:29:13 -08001021 free_platform_config(dd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001022 rcu_barrier(); /* wait for rcu callbacks to complete */
1023 free_percpu(dd->int_counter);
1024 free_percpu(dd->rcv_limit);
Vennila Megavannan89abfc82016-02-03 14:34:07 -08001025 free_percpu(dd->send_schedule);
Jubin Johnea0e4ce2016-04-20 06:05:24 -07001026 rvt_dealloc_device(&dd->verbs_dev.rdi);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001027}
1028
Dennis Dalessandroe11ffbd2016-05-19 05:26:44 -07001029static struct kobj_type hfi1_devdata_type = {
1030 .release = __hfi1_free_devdata,
1031};
1032
1033void hfi1_free_devdata(struct hfi1_devdata *dd)
1034{
1035 kobject_put(&dd->kobj);
1036}
1037
Mike Marciniszyn77241052015-07-30 15:17:43 -04001038/*
1039 * Allocate our primary per-unit data structure. Must be done via verbs
1040 * allocator, because the verbs cleanup process both does cleanup and
1041 * free of the data structure.
1042 * "extra" is for chip-specific data.
1043 *
1044 * Use the idr mechanism to get a unit number for this unit.
1045 */
1046struct hfi1_devdata *hfi1_alloc_devdata(struct pci_dev *pdev, size_t extra)
1047{
1048 unsigned long flags;
1049 struct hfi1_devdata *dd;
Dennis Dalessandro7af6d002016-01-19 14:44:06 -08001050 int ret, nports;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001051
Dennis Dalessandro7af6d002016-01-19 14:44:06 -08001052 /* extra is * number of ports */
1053 nports = extra / sizeof(struct hfi1_pportdata);
1054
1055 dd = (struct hfi1_devdata *)rvt_alloc_device(sizeof(*dd) + extra,
1056 nports);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001057 if (!dd)
1058 return ERR_PTR(-ENOMEM);
Dennis Dalessandro7af6d002016-01-19 14:44:06 -08001059 dd->num_pports = nports;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001060 dd->pport = (struct hfi1_pportdata *)(dd + 1);
1061
1062 INIT_LIST_HEAD(&dd->list);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001063 idr_preload(GFP_KERNEL);
1064 spin_lock_irqsave(&hfi1_devs_lock, flags);
1065
1066 ret = idr_alloc(&hfi1_unit_table, dd, 0, 0, GFP_NOWAIT);
1067 if (ret >= 0) {
1068 dd->unit = ret;
1069 list_add(&dd->list, &hfi1_dev_list);
1070 }
1071
1072 spin_unlock_irqrestore(&hfi1_devs_lock, flags);
1073 idr_preload_end();
1074
1075 if (ret < 0) {
1076 hfi1_early_err(&pdev->dev,
1077 "Could not allocate unit ID: error %d\n", -ret);
1078 goto bail;
1079 }
1080 /*
1081 * Initialize all locks for the device. This needs to be as early as
1082 * possible so locks are usable.
1083 */
1084 spin_lock_init(&dd->sc_lock);
1085 spin_lock_init(&dd->sendctrl_lock);
1086 spin_lock_init(&dd->rcvctrl_lock);
1087 spin_lock_init(&dd->uctxt_lock);
1088 spin_lock_init(&dd->hfi1_diag_trans_lock);
1089 spin_lock_init(&dd->sc_init_lock);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001090 spin_lock_init(&dd->dc8051_memlock);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001091 seqlock_init(&dd->sc2vl_lock);
1092 spin_lock_init(&dd->sde_map_lock);
Jubin John35f6bef2016-02-14 12:46:10 -08001093 spin_lock_init(&dd->pio_map_lock);
Tadeusz Struk22546b72017-04-28 10:40:02 -07001094 mutex_init(&dd->dc8051_lock);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001095 init_waitqueue_head(&dd->event_queue);
1096
1097 dd->int_counter = alloc_percpu(u64);
1098 if (!dd->int_counter) {
1099 ret = -ENOMEM;
1100 hfi1_early_err(&pdev->dev,
1101 "Could not allocate per-cpu int_counter\n");
1102 goto bail;
1103 }
1104
1105 dd->rcv_limit = alloc_percpu(u64);
1106 if (!dd->rcv_limit) {
1107 ret = -ENOMEM;
1108 hfi1_early_err(&pdev->dev,
1109 "Could not allocate per-cpu rcv_limit\n");
1110 goto bail;
1111 }
1112
Vennila Megavannan89abfc82016-02-03 14:34:07 -08001113 dd->send_schedule = alloc_percpu(u64);
1114 if (!dd->send_schedule) {
1115 ret = -ENOMEM;
1116 hfi1_early_err(&pdev->dev,
1117 "Could not allocate per-cpu int_counter\n");
1118 goto bail;
1119 }
1120
Mike Marciniszyn77241052015-07-30 15:17:43 -04001121 if (!hfi1_cpulist_count) {
1122 u32 count = num_online_cpus();
1123
Shraddha Barke314fcc02015-10-09 21:03:26 +05301124 hfi1_cpulist = kcalloc(BITS_TO_LONGS(count), sizeof(long),
1125 GFP_KERNEL);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001126 if (hfi1_cpulist)
1127 hfi1_cpulist_count = count;
1128 else
1129 hfi1_early_err(
1130 &pdev->dev,
1131 "Could not alloc cpulist info, cpu affinity might be wrong\n");
1132 }
Dennis Dalessandroe11ffbd2016-05-19 05:26:44 -07001133 kobject_init(&dd->kobj, &hfi1_devdata_type);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001134 return dd;
1135
1136bail:
1137 if (!list_empty(&dd->list))
1138 list_del_init(&dd->list);
Jubin Johnea0e4ce2016-04-20 06:05:24 -07001139 rvt_dealloc_device(&dd->verbs_dev.rdi);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001140 return ERR_PTR(ret);
1141}
1142
1143/*
1144 * Called from freeze mode handlers, and from PCI error
1145 * reporting code. Should be paranoid about state of
1146 * system and data structures.
1147 */
1148void hfi1_disable_after_error(struct hfi1_devdata *dd)
1149{
1150 if (dd->flags & HFI1_INITTED) {
1151 u32 pidx;
1152
1153 dd->flags &= ~HFI1_INITTED;
1154 if (dd->pport)
1155 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
1156 struct hfi1_pportdata *ppd;
1157
1158 ppd = dd->pport + pidx;
1159 if (dd->flags & HFI1_PRESENT)
1160 set_link_state(ppd, HLS_DN_DISABLE);
1161
1162 if (ppd->statusp)
1163 *ppd->statusp &= ~HFI1_STATUS_IB_READY;
1164 }
1165 }
1166
1167 /*
1168 * Mark as having had an error for driver, and also
1169 * for /sys and status word mapped to user programs.
1170 * This marks unit as not usable, until reset.
1171 */
1172 if (dd->status)
1173 dd->status->dev |= HFI1_STATUS_HWERROR;
1174}
1175
1176static void remove_one(struct pci_dev *);
1177static int init_one(struct pci_dev *, const struct pci_device_id *);
1178
1179#define DRIVER_LOAD_MSG "Intel " DRIVER_NAME " loaded: "
1180#define PFX DRIVER_NAME ": "
1181
Sebastian Sanchezd6373012016-07-25 07:54:48 -07001182const struct pci_device_id hfi1_pci_tbl[] = {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001183 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL0) },
1184 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL1) },
1185 { 0, }
1186};
1187
1188MODULE_DEVICE_TABLE(pci, hfi1_pci_tbl);
1189
1190static struct pci_driver hfi1_pci_driver = {
1191 .name = DRIVER_NAME,
1192 .probe = init_one,
1193 .remove = remove_one,
1194 .id_table = hfi1_pci_tbl,
1195 .err_handler = &hfi1_pci_err_handler,
1196};
1197
1198static void __init compute_krcvqs(void)
1199{
1200 int i;
1201
1202 for (i = 0; i < krcvqsset; i++)
1203 n_krcvqs += krcvqs[i];
1204}
1205
1206/*
1207 * Do all the generic driver unit- and chip-independent memory
1208 * allocation and initialization.
1209 */
1210static int __init hfi1_mod_init(void)
1211{
1212 int ret;
1213
1214 ret = dev_init();
1215 if (ret)
1216 goto bail;
1217
Sebastian Sanchezd6373012016-07-25 07:54:48 -07001218 ret = node_affinity_init();
1219 if (ret)
1220 goto bail;
Dennis Dalessandro41973442016-07-25 07:52:36 -07001221
Mike Marciniszyn77241052015-07-30 15:17:43 -04001222 /* validate max MTU before any devices start */
1223 if (!valid_opa_max_mtu(hfi1_max_mtu)) {
1224 pr_err("Invalid max_mtu 0x%x, using 0x%x instead\n",
1225 hfi1_max_mtu, HFI1_DEFAULT_MAX_MTU);
1226 hfi1_max_mtu = HFI1_DEFAULT_MAX_MTU;
1227 }
1228 /* valid CUs run from 1-128 in powers of 2 */
1229 if (hfi1_cu > 128 || !is_power_of_2(hfi1_cu))
1230 hfi1_cu = 1;
1231 /* valid credit return threshold is 0-100, variable is unsigned */
1232 if (user_credit_return_threshold > 100)
1233 user_credit_return_threshold = 100;
1234
1235 compute_krcvqs();
Jubin John4d114fd2016-02-14 20:21:43 -08001236 /*
1237 * sanitize receive interrupt count, time must wait until after
1238 * the hardware type is known
1239 */
Mike Marciniszyn77241052015-07-30 15:17:43 -04001240 if (rcv_intr_count > RCV_HDR_HEAD_COUNTER_MASK)
1241 rcv_intr_count = RCV_HDR_HEAD_COUNTER_MASK;
1242 /* reject invalid combinations */
1243 if (rcv_intr_count == 0 && rcv_intr_timeout == 0) {
1244 pr_err("Invalid mode: both receive interrupt count and available timeout are zero - setting interrupt count to 1\n");
1245 rcv_intr_count = 1;
1246 }
1247 if (rcv_intr_count > 1 && rcv_intr_timeout == 0) {
1248 /*
1249 * Avoid indefinite packet delivery by requiring a timeout
1250 * if count is > 1.
1251 */
1252 pr_err("Invalid mode: receive interrupt count greater than 1 and available timeout is zero - setting available timeout to 1\n");
1253 rcv_intr_timeout = 1;
1254 }
1255 if (rcv_intr_dynamic && !(rcv_intr_count > 1 && rcv_intr_timeout > 0)) {
1256 /*
1257 * The dynamic algorithm expects a non-zero timeout
1258 * and a count > 1.
1259 */
1260 pr_err("Invalid mode: dynamic receive interrupt mitigation with invalid count and timeout - turning dynamic off\n");
1261 rcv_intr_dynamic = 0;
1262 }
1263
1264 /* sanitize link CRC options */
1265 link_crc_mask &= SUPPORTED_CRCS;
1266
1267 /*
1268 * These must be called before the driver is registered with
1269 * the PCI subsystem.
1270 */
1271 idr_init(&hfi1_unit_table);
1272
1273 hfi1_dbg_init();
Dean Luick528ee9f2016-03-05 08:50:43 -08001274 ret = hfi1_wss_init();
1275 if (ret < 0)
1276 goto bail_wss;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001277 ret = pci_register_driver(&hfi1_pci_driver);
1278 if (ret < 0) {
1279 pr_err("Unable to register driver: error %d\n", -ret);
1280 goto bail_dev;
1281 }
1282 goto bail; /* all OK */
1283
1284bail_dev:
Dean Luick528ee9f2016-03-05 08:50:43 -08001285 hfi1_wss_exit();
1286bail_wss:
Mike Marciniszyn77241052015-07-30 15:17:43 -04001287 hfi1_dbg_exit();
1288 idr_destroy(&hfi1_unit_table);
1289 dev_cleanup();
1290bail:
1291 return ret;
1292}
1293
1294module_init(hfi1_mod_init);
1295
1296/*
1297 * Do the non-unit driver cleanup, memory free, etc. at unload.
1298 */
1299static void __exit hfi1_mod_cleanup(void)
1300{
1301 pci_unregister_driver(&hfi1_pci_driver);
Dennis Dalessandro41973442016-07-25 07:52:36 -07001302 node_affinity_destroy();
Dean Luick528ee9f2016-03-05 08:50:43 -08001303 hfi1_wss_exit();
Mike Marciniszyn77241052015-07-30 15:17:43 -04001304 hfi1_dbg_exit();
1305 hfi1_cpulist_count = 0;
1306 kfree(hfi1_cpulist);
1307
1308 idr_destroy(&hfi1_unit_table);
1309 dispose_firmware(); /* asymmetric with obtain_firmware() */
1310 dev_cleanup();
1311}
1312
1313module_exit(hfi1_mod_cleanup);
1314
1315/* this can only be called after a successful initialization */
1316static void cleanup_device_data(struct hfi1_devdata *dd)
1317{
1318 int ctxt;
1319 int pidx;
1320 struct hfi1_ctxtdata **tmp;
1321 unsigned long flags;
1322
1323 /* users can't do anything more with chip */
1324 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
1325 struct hfi1_pportdata *ppd = &dd->pport[pidx];
1326 struct cc_state *cc_state;
1327 int i;
1328
1329 if (ppd->statusp)
1330 *ppd->statusp &= ~HFI1_STATUS_CHIP_PRESENT;
1331
1332 for (i = 0; i < OPA_MAX_SLS; i++)
1333 hrtimer_cancel(&ppd->cca_timer[i].hrtimer);
1334
1335 spin_lock(&ppd->cc_state_lock);
Jianxin Xiong8adf71f2016-07-25 13:39:14 -07001336 cc_state = get_cc_state_protected(ppd);
Muhammad Falak R Wanieea57072016-05-01 18:05:31 +05301337 RCU_INIT_POINTER(ppd->cc_state, NULL);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001338 spin_unlock(&ppd->cc_state_lock);
1339
1340 if (cc_state)
Wei Yongjun476d95b2016-08-10 03:14:04 +00001341 kfree_rcu(cc_state, rcu);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001342 }
1343
1344 free_credit_return(dd);
1345
1346 /*
1347 * Free any resources still in use (usually just kernel contexts)
1348 * at unload; we do for ctxtcnt, because that's what we allocate.
1349 * We acquire lock to be really paranoid that rcd isn't being
1350 * accessed from some interrupt-related code (that should not happen,
1351 * but best to be sure).
1352 */
1353 spin_lock_irqsave(&dd->uctxt_lock, flags);
1354 tmp = dd->rcd;
1355 dd->rcd = NULL;
1356 spin_unlock_irqrestore(&dd->uctxt_lock, flags);
Mark F. Brown46b010d2015-11-09 19:18:20 -05001357
1358 if (dd->rcvhdrtail_dummy_kvaddr) {
1359 dma_free_coherent(&dd->pcidev->dev, sizeof(u64),
1360 (void *)dd->rcvhdrtail_dummy_kvaddr,
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001361 dd->rcvhdrtail_dummy_dma);
Dan Carpentera8b7da52016-05-28 08:01:20 +03001362 dd->rcvhdrtail_dummy_kvaddr = NULL;
Mark F. Brown46b010d2015-11-09 19:18:20 -05001363 }
1364
Mike Marciniszyn77241052015-07-30 15:17:43 -04001365 for (ctxt = 0; tmp && ctxt < dd->num_rcv_contexts; ctxt++) {
1366 struct hfi1_ctxtdata *rcd = tmp[ctxt];
1367
1368 tmp[ctxt] = NULL; /* debugging paranoia */
1369 if (rcd) {
1370 hfi1_clear_tids(rcd);
1371 hfi1_free_ctxtdata(dd, rcd);
1372 }
1373 }
1374 kfree(tmp);
Jubin John35f6bef2016-02-14 12:46:10 -08001375 free_pio_map(dd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001376 /* must follow rcv context free - need to remove rcv's hooks */
1377 for (ctxt = 0; ctxt < dd->num_send_contexts; ctxt++)
1378 sc_free(dd->send_contexts[ctxt].sc);
1379 dd->num_send_contexts = 0;
1380 kfree(dd->send_contexts);
1381 dd->send_contexts = NULL;
Jubin John79d0c082016-02-26 13:33:33 -08001382 kfree(dd->hw_to_sw);
1383 dd->hw_to_sw = NULL;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001384 kfree(dd->boardname);
1385 vfree(dd->events);
1386 vfree(dd->status);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001387}
1388
1389/*
1390 * Clean up on unit shutdown, or error during unit load after
1391 * successful initialization.
1392 */
1393static void postinit_cleanup(struct hfi1_devdata *dd)
1394{
1395 hfi1_start_cleanup(dd);
1396
1397 hfi1_pcie_ddcleanup(dd);
1398 hfi1_pcie_cleanup(dd->pcidev);
1399
1400 cleanup_device_data(dd);
1401
1402 hfi1_free_devdata(dd);
1403}
1404
Krzysztof Blaszkowski11501ab2016-10-25 13:12:11 -07001405static int init_validate_rcvhdrcnt(struct device *dev, uint thecnt)
1406{
1407 if (thecnt <= HFI1_MIN_HDRQ_EGRBUF_CNT) {
1408 hfi1_early_err(dev, "Receive header queue count too small\n");
1409 return -EINVAL;
1410 }
1411
1412 if (thecnt > HFI1_MAX_HDRQ_EGRBUF_CNT) {
1413 hfi1_early_err(dev,
1414 "Receive header queue count cannot be greater than %u\n",
1415 HFI1_MAX_HDRQ_EGRBUF_CNT);
1416 return -EINVAL;
1417 }
1418
1419 if (thecnt % HDRQ_INCREMENT) {
1420 hfi1_early_err(dev, "Receive header queue count %d must be divisible by %lu\n",
1421 thecnt, HDRQ_INCREMENT);
1422 return -EINVAL;
1423 }
1424
1425 return 0;
1426}
1427
Mike Marciniszyn77241052015-07-30 15:17:43 -04001428static int init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
1429{
1430 int ret = 0, j, pidx, initfail;
Krzysztof Blaszkowski83fb4af2016-10-17 04:19:24 -07001431 struct hfi1_devdata *dd;
Harish Chegondie8597eb2015-12-01 15:38:20 -05001432 struct hfi1_pportdata *ppd;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001433
1434 /* First, lock the non-writable module parameters */
1435 HFI1_CAP_LOCK();
1436
Tadeusz Struk5d6f08a2017-03-20 17:25:29 -07001437 /* Validate dev ids */
1438 if (!(ent->device == PCI_DEVICE_ID_INTEL0 ||
1439 ent->device == PCI_DEVICE_ID_INTEL1)) {
1440 hfi1_early_err(&pdev->dev,
1441 "Failing on unknown Intel deviceid 0x%x\n",
1442 ent->device);
1443 ret = -ENODEV;
1444 goto bail;
1445 }
1446
Mike Marciniszyn77241052015-07-30 15:17:43 -04001447 /* Validate some global module parameters */
Krzysztof Blaszkowski11501ab2016-10-25 13:12:11 -07001448 ret = init_validate_rcvhdrcnt(&pdev->dev, rcvhdrcnt);
1449 if (ret)
Mike Marciniszyn77241052015-07-30 15:17:43 -04001450 goto bail;
Krzysztof Blaszkowski11501ab2016-10-25 13:12:11 -07001451
Mike Marciniszyn77241052015-07-30 15:17:43 -04001452 /* use the encoding function as a sanitization check */
1453 if (!encode_rcv_header_entry_size(hfi1_hdrq_entsize)) {
1454 hfi1_early_err(&pdev->dev, "Invalid HdrQ Entry size %u\n",
1455 hfi1_hdrq_entsize);
Sebastian Sanchez07859de2015-12-10 16:02:49 -05001456 ret = -EINVAL;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001457 goto bail;
1458 }
1459
1460 /* The receive eager buffer size must be set before the receive
1461 * contexts are created.
1462 *
1463 * Set the eager buffer size. Validate that it falls in a range
1464 * allowed by the hardware - all powers of 2 between the min and
1465 * max. The maximum valid MTU is within the eager buffer range
1466 * so we do not need to cap the max_mtu by an eager buffer size
1467 * setting.
1468 */
1469 if (eager_buffer_size) {
1470 if (!is_power_of_2(eager_buffer_size))
1471 eager_buffer_size =
1472 roundup_pow_of_two(eager_buffer_size);
1473 eager_buffer_size =
1474 clamp_val(eager_buffer_size,
1475 MIN_EAGER_BUFFER * 8,
1476 MAX_EAGER_BUFFER_TOTAL);
1477 hfi1_early_info(&pdev->dev, "Eager buffer size %u\n",
1478 eager_buffer_size);
1479 } else {
1480 hfi1_early_err(&pdev->dev, "Invalid Eager buffer size of 0\n");
1481 ret = -EINVAL;
1482 goto bail;
1483 }
1484
1485 /* restrict value of hfi1_rcvarr_split */
1486 hfi1_rcvarr_split = clamp_val(hfi1_rcvarr_split, 0, 100);
1487
1488 ret = hfi1_pcie_init(pdev, ent);
1489 if (ret)
1490 goto bail;
1491
Krzysztof Blaszkowski83fb4af2016-10-17 04:19:24 -07001492 /*
1493 * Do device-specific initialization, function table setup, dd
1494 * allocation, etc.
1495 */
1496 dd = hfi1_init_dd(pdev, ent);
1497
1498 if (IS_ERR(dd)) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001499 ret = PTR_ERR(dd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001500 goto clean_bail; /* error already printed */
Krzysztof Blaszkowski83fb4af2016-10-17 04:19:24 -07001501 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001502
1503 ret = create_workqueues(dd);
1504 if (ret)
1505 goto clean_bail;
1506
1507 /* do the generic initialization */
1508 initfail = hfi1_init(dd, 0);
1509
Vishwanathapura, Niranjanad4829ea2017-04-12 20:29:28 -07001510 /* setup vnic */
1511 hfi1_vnic_setup(dd);
1512
Mike Marciniszyn77241052015-07-30 15:17:43 -04001513 ret = hfi1_register_ib_device(dd);
1514
1515 /*
1516 * Now ready for use. this should be cleared whenever we
1517 * detect a reset, or initiate one. If earlier failure,
1518 * we still create devices, so diags, etc. can be used
1519 * to determine cause of problem.
1520 */
Dean Luicked6f6532016-02-18 11:12:25 -08001521 if (!initfail && !ret) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001522 dd->flags |= HFI1_INITTED;
Dean Luicked6f6532016-02-18 11:12:25 -08001523 /* create debufs files after init and ib register */
1524 hfi1_dbg_ibdev_init(&dd->verbs_dev);
1525 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001526
1527 j = hfi1_device_create(dd);
1528 if (j)
1529 dd_dev_err(dd, "Failed to create /dev devices: %d\n", -j);
1530
1531 if (initfail || ret) {
1532 stop_timers(dd);
1533 flush_workqueue(ib_wq);
Harish Chegondie8597eb2015-12-01 15:38:20 -05001534 for (pidx = 0; pidx < dd->num_pports; ++pidx) {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001535 hfi1_quiet_serdes(dd->pport + pidx);
Harish Chegondie8597eb2015-12-01 15:38:20 -05001536 ppd = dd->pport + pidx;
1537 if (ppd->hfi1_wq) {
1538 destroy_workqueue(ppd->hfi1_wq);
1539 ppd->hfi1_wq = NULL;
1540 }
1541 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001542 if (!j)
1543 hfi1_device_remove(dd);
1544 if (!ret)
1545 hfi1_unregister_ib_device(dd);
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -07001546 hfi1_vnic_cleanup(dd);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001547 postinit_cleanup(dd);
1548 if (initfail)
1549 ret = initfail;
1550 goto bail; /* everything already cleaned */
1551 }
1552
1553 sdma_start(dd);
1554
1555 return 0;
1556
1557clean_bail:
1558 hfi1_pcie_cleanup(pdev);
1559bail:
1560 return ret;
1561}
1562
Tadeusz Strukacd7c8f2016-10-25 08:57:55 -07001563static void wait_for_clients(struct hfi1_devdata *dd)
1564{
1565 /*
1566 * Remove the device init value and complete the device if there is
1567 * no clients or wait for active clients to finish.
1568 */
1569 if (atomic_dec_and_test(&dd->user_refcount))
1570 complete(&dd->user_comp);
1571
1572 wait_for_completion(&dd->user_comp);
1573}
1574
Mike Marciniszyn77241052015-07-30 15:17:43 -04001575static void remove_one(struct pci_dev *pdev)
1576{
1577 struct hfi1_devdata *dd = pci_get_drvdata(pdev);
1578
Dean Luicked6f6532016-02-18 11:12:25 -08001579 /* close debugfs files before ib unregister */
1580 hfi1_dbg_ibdev_exit(&dd->verbs_dev);
Tadeusz Strukacd7c8f2016-10-25 08:57:55 -07001581
1582 /* remove the /dev hfi1 interface */
1583 hfi1_device_remove(dd);
1584
1585 /* wait for existing user space clients to finish */
1586 wait_for_clients(dd);
1587
Mike Marciniszyn77241052015-07-30 15:17:43 -04001588 /* unregister from IB core */
1589 hfi1_unregister_ib_device(dd);
1590
Vishwanathapura, Niranjanad4829ea2017-04-12 20:29:28 -07001591 /* cleanup vnic */
1592 hfi1_vnic_cleanup(dd);
1593
Mike Marciniszyn77241052015-07-30 15:17:43 -04001594 /*
1595 * Disable the IB link, disable interrupts on the device,
1596 * clear dma engines, etc.
1597 */
1598 shutdown_device(dd);
1599
1600 stop_timers(dd);
1601
1602 /* wait until all of our (qsfp) queue_work() calls complete */
1603 flush_workqueue(ib_wq);
1604
Mike Marciniszyn77241052015-07-30 15:17:43 -04001605 postinit_cleanup(dd);
1606}
1607
1608/**
1609 * hfi1_create_rcvhdrq - create a receive header queue
1610 * @dd: the hfi1_ib device
1611 * @rcd: the context data
1612 *
1613 * This must be contiguous memory (from an i/o perspective), and must be
1614 * DMA'able (which means for some systems, it will go through an IOMMU,
1615 * or be forced into a low address range).
1616 */
1617int hfi1_create_rcvhdrq(struct hfi1_devdata *dd, struct hfi1_ctxtdata *rcd)
1618{
1619 unsigned amt;
1620 u64 reg;
1621
1622 if (!rcd->rcvhdrq) {
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001623 dma_addr_t dma_hdrqtail;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001624 gfp_t gfp_flags;
1625
1626 /*
1627 * rcvhdrqentsize is in DWs, so we have to convert to bytes
1628 * (* sizeof(u32)).
1629 */
Amitoj Kaur Chawla84449912016-03-04 22:30:43 +05301630 amt = PAGE_ALIGN(rcd->rcvhdrq_cnt * rcd->rcvhdrqentsize *
1631 sizeof(u32));
Mike Marciniszyn77241052015-07-30 15:17:43 -04001632
Vishwanathapura, Niranjana22807402017-04-12 20:29:29 -07001633 if ((rcd->ctxt < dd->first_dyn_alloc_ctxt) ||
1634 (rcd->sc && (rcd->sc->type == SC_KERNEL)))
1635 gfp_flags = GFP_KERNEL;
1636 else
1637 gfp_flags = GFP_USER;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001638 rcd->rcvhdrq = dma_zalloc_coherent(
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001639 &dd->pcidev->dev, amt, &rcd->rcvhdrq_dma,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001640 gfp_flags | __GFP_COMP);
1641
1642 if (!rcd->rcvhdrq) {
1643 dd_dev_err(dd,
Jubin John17fb4f22016-02-14 20:21:52 -08001644 "attempt to allocate %d bytes for ctxt %u rcvhdrq failed\n",
1645 amt, rcd->ctxt);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001646 goto bail;
1647 }
1648
Mike Marciniszyn77241052015-07-30 15:17:43 -04001649 if (HFI1_CAP_KGET_MASK(rcd->flags, DMA_RTAIL)) {
1650 rcd->rcvhdrtail_kvaddr = dma_zalloc_coherent(
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001651 &dd->pcidev->dev, PAGE_SIZE, &dma_hdrqtail,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001652 gfp_flags);
1653 if (!rcd->rcvhdrtail_kvaddr)
1654 goto bail_free;
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001655 rcd->rcvhdrqtailaddr_dma = dma_hdrqtail;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001656 }
1657
1658 rcd->rcvhdrq_size = amt;
1659 }
1660 /*
1661 * These values are per-context:
1662 * RcvHdrCnt
1663 * RcvHdrEntSize
1664 * RcvHdrSize
1665 */
1666 reg = ((u64)(rcd->rcvhdrq_cnt >> HDRQ_SIZE_SHIFT)
1667 & RCV_HDR_CNT_CNT_MASK)
1668 << RCV_HDR_CNT_CNT_SHIFT;
1669 write_kctxt_csr(dd, rcd->ctxt, RCV_HDR_CNT, reg);
1670 reg = (encode_rcv_header_entry_size(rcd->rcvhdrqentsize)
1671 & RCV_HDR_ENT_SIZE_ENT_SIZE_MASK)
1672 << RCV_HDR_ENT_SIZE_ENT_SIZE_SHIFT;
1673 write_kctxt_csr(dd, rcd->ctxt, RCV_HDR_ENT_SIZE, reg);
1674 reg = (dd->rcvhdrsize & RCV_HDR_SIZE_HDR_SIZE_MASK)
1675 << RCV_HDR_SIZE_HDR_SIZE_SHIFT;
1676 write_kctxt_csr(dd, rcd->ctxt, RCV_HDR_SIZE, reg);
Mark F. Brown46b010d2015-11-09 19:18:20 -05001677
1678 /*
1679 * Program dummy tail address for every receive context
1680 * before enabling any receive context
1681 */
1682 write_kctxt_csr(dd, rcd->ctxt, RCV_HDR_TAIL_ADDR,
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001683 dd->rcvhdrtail_dummy_dma);
Mark F. Brown46b010d2015-11-09 19:18:20 -05001684
Mike Marciniszyn77241052015-07-30 15:17:43 -04001685 return 0;
1686
1687bail_free:
1688 dd_dev_err(dd,
Jubin John17fb4f22016-02-14 20:21:52 -08001689 "attempt to allocate 1 page for ctxt %u rcvhdrqtailaddr failed\n",
1690 rcd->ctxt);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001691 vfree(rcd->user_event_mask);
1692 rcd->user_event_mask = NULL;
1693 dma_free_coherent(&dd->pcidev->dev, amt, rcd->rcvhdrq,
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001694 rcd->rcvhdrq_dma);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001695 rcd->rcvhdrq = NULL;
1696bail:
1697 return -ENOMEM;
1698}
1699
1700/**
1701 * allocate eager buffers, both kernel and user contexts.
1702 * @rcd: the context we are setting up.
1703 *
1704 * Allocate the eager TID buffers and program them into hip.
1705 * They are no longer completely contiguous, we do multiple allocation
1706 * calls. Otherwise we get the OOM code involved, by asking for too
1707 * much per call, with disastrous results on some kernels.
1708 */
1709int hfi1_setup_eagerbufs(struct hfi1_ctxtdata *rcd)
1710{
1711 struct hfi1_devdata *dd = rcd->dd;
1712 u32 max_entries, egrtop, alloced_bytes = 0, idx = 0;
1713 gfp_t gfp_flags;
1714 u16 order;
1715 int ret = 0;
1716 u16 round_mtu = roundup_pow_of_two(hfi1_max_mtu);
1717
1718 /*
1719 * GFP_USER, but without GFP_FS, so buffer cache can be
1720 * coalesced (we hope); otherwise, even at order 4,
1721 * heavy filesystem activity makes these fail, and we can
1722 * use compound pages.
1723 */
Mel Gorman71baba42015-11-06 16:28:28 -08001724 gfp_flags = __GFP_RECLAIM | __GFP_IO | __GFP_COMP;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001725
1726 /*
1727 * The minimum size of the eager buffers is a groups of MTU-sized
1728 * buffers.
1729 * The global eager_buffer_size parameter is checked against the
1730 * theoretical lower limit of the value. Here, we check against the
1731 * MTU.
1732 */
1733 if (rcd->egrbufs.size < (round_mtu * dd->rcv_entries.group_size))
1734 rcd->egrbufs.size = round_mtu * dd->rcv_entries.group_size;
1735 /*
1736 * If using one-pkt-per-egr-buffer, lower the eager buffer
1737 * size to the max MTU (page-aligned).
1738 */
1739 if (!HFI1_CAP_KGET_MASK(rcd->flags, MULTI_PKT_EGR))
1740 rcd->egrbufs.rcvtid_size = round_mtu;
1741
1742 /*
1743 * Eager buffers sizes of 1MB or less require smaller TID sizes
1744 * to satisfy the "multiple of 8 RcvArray entries" requirement.
1745 */
1746 if (rcd->egrbufs.size <= (1 << 20))
1747 rcd->egrbufs.rcvtid_size = max((unsigned long)round_mtu,
1748 rounddown_pow_of_two(rcd->egrbufs.size / 8));
1749
1750 while (alloced_bytes < rcd->egrbufs.size &&
1751 rcd->egrbufs.alloced < rcd->egrbufs.count) {
1752 rcd->egrbufs.buffers[idx].addr =
1753 dma_zalloc_coherent(&dd->pcidev->dev,
1754 rcd->egrbufs.rcvtid_size,
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001755 &rcd->egrbufs.buffers[idx].dma,
Mike Marciniszyn77241052015-07-30 15:17:43 -04001756 gfp_flags);
1757 if (rcd->egrbufs.buffers[idx].addr) {
1758 rcd->egrbufs.buffers[idx].len =
1759 rcd->egrbufs.rcvtid_size;
1760 rcd->egrbufs.rcvtids[rcd->egrbufs.alloced].addr =
1761 rcd->egrbufs.buffers[idx].addr;
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001762 rcd->egrbufs.rcvtids[rcd->egrbufs.alloced].dma =
1763 rcd->egrbufs.buffers[idx].dma;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001764 rcd->egrbufs.alloced++;
1765 alloced_bytes += rcd->egrbufs.rcvtid_size;
1766 idx++;
1767 } else {
1768 u32 new_size, i, j;
1769 u64 offset = 0;
1770
1771 /*
1772 * Fail the eager buffer allocation if:
1773 * - we are already using the lowest acceptable size
1774 * - we are using one-pkt-per-egr-buffer (this implies
1775 * that we are accepting only one size)
1776 */
1777 if (rcd->egrbufs.rcvtid_size == round_mtu ||
1778 !HFI1_CAP_KGET_MASK(rcd->flags, MULTI_PKT_EGR)) {
1779 dd_dev_err(dd, "ctxt%u: Failed to allocate eager buffers\n",
Jubin John17fb4f22016-02-14 20:21:52 -08001780 rcd->ctxt);
Michael J. Ruhl94679062017-05-04 05:14:28 -07001781 ret = -ENOMEM;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001782 goto bail_rcvegrbuf_phys;
1783 }
1784
1785 new_size = rcd->egrbufs.rcvtid_size / 2;
1786
1787 /*
1788 * If the first attempt to allocate memory failed, don't
1789 * fail everything but continue with the next lower
1790 * size.
1791 */
1792 if (idx == 0) {
1793 rcd->egrbufs.rcvtid_size = new_size;
1794 continue;
1795 }
1796
1797 /*
1798 * Re-partition already allocated buffers to a smaller
1799 * size.
1800 */
1801 rcd->egrbufs.alloced = 0;
1802 for (i = 0, j = 0, offset = 0; j < idx; i++) {
1803 if (i >= rcd->egrbufs.count)
1804 break;
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001805 rcd->egrbufs.rcvtids[i].dma =
1806 rcd->egrbufs.buffers[j].dma + offset;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001807 rcd->egrbufs.rcvtids[i].addr =
1808 rcd->egrbufs.buffers[j].addr + offset;
1809 rcd->egrbufs.alloced++;
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001810 if ((rcd->egrbufs.buffers[j].dma + offset +
Mike Marciniszyn77241052015-07-30 15:17:43 -04001811 new_size) ==
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001812 (rcd->egrbufs.buffers[j].dma +
Mike Marciniszyn77241052015-07-30 15:17:43 -04001813 rcd->egrbufs.buffers[j].len)) {
1814 j++;
1815 offset = 0;
Jubin Johne4909742016-02-14 20:22:00 -08001816 } else {
Mike Marciniszyn77241052015-07-30 15:17:43 -04001817 offset += new_size;
Jubin Johne4909742016-02-14 20:22:00 -08001818 }
Mike Marciniszyn77241052015-07-30 15:17:43 -04001819 }
1820 rcd->egrbufs.rcvtid_size = new_size;
1821 }
1822 }
1823 rcd->egrbufs.numbufs = idx;
1824 rcd->egrbufs.size = alloced_bytes;
1825
Sebastian Sanchez6c63e422015-11-06 20:06:56 -05001826 hfi1_cdbg(PROC,
1827 "ctxt%u: Alloced %u rcv tid entries @ %uKB, total %zuKB\n",
Grzegorz Heldt23002d52016-07-25 13:39:33 -07001828 rcd->ctxt, rcd->egrbufs.alloced,
1829 rcd->egrbufs.rcvtid_size / 1024, rcd->egrbufs.size / 1024);
Sebastian Sanchez6c63e422015-11-06 20:06:56 -05001830
Mike Marciniszyn77241052015-07-30 15:17:43 -04001831 /*
1832 * Set the contexts rcv array head update threshold to the closest
1833 * power of 2 (so we can use a mask instead of modulo) below half
1834 * the allocated entries.
1835 */
1836 rcd->egrbufs.threshold =
1837 rounddown_pow_of_two(rcd->egrbufs.alloced / 2);
1838 /*
1839 * Compute the expected RcvArray entry base. This is done after
1840 * allocating the eager buffers in order to maximize the
1841 * expected RcvArray entries for the context.
1842 */
1843 max_entries = rcd->rcv_array_groups * dd->rcv_entries.group_size;
1844 egrtop = roundup(rcd->egrbufs.alloced, dd->rcv_entries.group_size);
1845 rcd->expected_count = max_entries - egrtop;
1846 if (rcd->expected_count > MAX_TID_PAIR_ENTRIES * 2)
1847 rcd->expected_count = MAX_TID_PAIR_ENTRIES * 2;
1848
1849 rcd->expected_base = rcd->eager_base + egrtop;
Sebastian Sanchez6c63e422015-11-06 20:06:56 -05001850 hfi1_cdbg(PROC, "ctxt%u: eager:%u, exp:%u, egrbase:%u, expbase:%u\n",
1851 rcd->ctxt, rcd->egrbufs.alloced, rcd->expected_count,
1852 rcd->eager_base, rcd->expected_base);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001853
1854 if (!hfi1_rcvbuf_validate(rcd->egrbufs.rcvtid_size, PT_EAGER, &order)) {
Sebastian Sanchez6c63e422015-11-06 20:06:56 -05001855 hfi1_cdbg(PROC,
1856 "ctxt%u: current Eager buffer size is invalid %u\n",
1857 rcd->ctxt, rcd->egrbufs.rcvtid_size);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001858 ret = -EINVAL;
1859 goto bail;
1860 }
1861
1862 for (idx = 0; idx < rcd->egrbufs.alloced; idx++) {
1863 hfi1_put_tid(dd, rcd->eager_base + idx, PT_EAGER,
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001864 rcd->egrbufs.rcvtids[idx].dma, order);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001865 cond_resched();
1866 }
1867 goto bail;
1868
1869bail_rcvegrbuf_phys:
1870 for (idx = 0; idx < rcd->egrbufs.alloced &&
Jubin John17fb4f22016-02-14 20:21:52 -08001871 rcd->egrbufs.buffers[idx].addr;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001872 idx++) {
1873 dma_free_coherent(&dd->pcidev->dev,
1874 rcd->egrbufs.buffers[idx].len,
1875 rcd->egrbufs.buffers[idx].addr,
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001876 rcd->egrbufs.buffers[idx].dma);
Mike Marciniszyn77241052015-07-30 15:17:43 -04001877 rcd->egrbufs.buffers[idx].addr = NULL;
Tymoteusz Kielan60368182016-09-06 04:35:54 -07001878 rcd->egrbufs.buffers[idx].dma = 0;
Mike Marciniszyn77241052015-07-30 15:17:43 -04001879 rcd->egrbufs.buffers[idx].len = 0;
1880 }
1881bail:
1882 return ret;
1883}