Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1 | /* |
Vishwanathapura, Niranjana | d4829ea | 2017-04-12 20:29:28 -0700 | [diff] [blame] | 2 | * Copyright(c) 2015-2017 Intel Corporation. |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 3 | * |
| 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 Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 9 | * 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 Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 20 | * 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. Ruhl | 8737ce9 | 2017-05-04 05:15:15 -0700 | [diff] [blame^] | 56 | #include <linux/bitmap.h> |
Dennis Dalessandro | ec3f2c1 | 2016-01-19 14:41:33 -0800 | [diff] [blame] | 57 | #include <rdma/rdma_vt.h> |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 58 | |
| 59 | #include "hfi.h" |
| 60 | #include "device.h" |
| 61 | #include "common.h" |
Sebastian Sanchez | 6c63e42 | 2015-11-06 20:06:56 -0500 | [diff] [blame] | 62 | #include "trace.h" |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 63 | #include "mad.h" |
| 64 | #include "sdma.h" |
| 65 | #include "debugfs.h" |
| 66 | #include "verbs.h" |
Ashutosh Dixit | affa48d | 2016-02-03 14:33:06 -0800 | [diff] [blame] | 67 | #include "aspm.h" |
Dennis Dalessandro | 4197344 | 2016-07-25 07:52:36 -0700 | [diff] [blame] | 68 | #include "affinity.h" |
Vishwanathapura, Niranjana | d4829ea | 2017-04-12 20:29:28 -0700 | [diff] [blame] | 69 | #include "vnic.h" |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 70 | |
| 71 | #undef pr_fmt |
| 72 | #define pr_fmt(fmt) DRIVER_NAME ": " fmt |
| 73 | |
Mike Marciniszyn | dd1ed10 | 2017-05-04 05:14:10 -0700 | [diff] [blame] | 74 | #define HFI1_MAX_ACTIVE_WORKQUEUE_ENTRIES 5 |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 75 | /* |
| 76 | * min buffers we want to have per context, after driver |
| 77 | */ |
| 78 | #define HFI1_MIN_USER_CTXT_BUFCNT 7 |
| 79 | |
| 80 | #define HFI1_MIN_HDRQ_EGRBUF_CNT 2 |
Sebastian Sanchez | e002dcc | 2016-02-03 14:34:32 -0800 | [diff] [blame] | 81 | #define HFI1_MAX_HDRQ_EGRBUF_CNT 16352 |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 82 | #define HFI1_MIN_EAGER_BUFFER_SIZE (4 * 1024) /* 4KB */ |
| 83 | #define HFI1_MAX_EAGER_BUFFER_SIZE (256 * 1024) /* 256KB */ |
| 84 | |
| 85 | /* |
| 86 | * Number of user receive contexts we are configured to use (to allow for more |
| 87 | * pio buffers per ctxt, etc.) Zero means use one user context per CPU. |
| 88 | */ |
Sebastian Sanchez | 2ce6bf2 | 2015-12-11 08:44:48 -0500 | [diff] [blame] | 89 | int num_user_contexts = -1; |
| 90 | module_param_named(num_user_contexts, num_user_contexts, uint, S_IRUGO); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 91 | MODULE_PARM_DESC( |
Sebastian Sanchez | 2ce6bf2 | 2015-12-11 08:44:48 -0500 | [diff] [blame] | 92 | num_user_contexts, "Set max number of user contexts to use"); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 93 | |
Mark F. Brown | 5b55ea3 | 2016-01-11 18:30:54 -0500 | [diff] [blame] | 94 | uint krcvqs[RXE_NUM_DATA_VL]; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 95 | int krcvqsset; |
Mark F. Brown | 5b55ea3 | 2016-01-11 18:30:54 -0500 | [diff] [blame] | 96 | module_param_array(krcvqs, uint, &krcvqsset, S_IRUGO); |
Niranjana Vishwanathapura | 82c2611 | 2015-11-11 00:35:19 -0500 | [diff] [blame] | 97 | MODULE_PARM_DESC(krcvqs, "Array of the number of non-control kernel receive queues by VL"); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 98 | |
| 99 | /* computed based on above array */ |
Harish Chegondi | 429b6a7 | 2016-08-31 07:24:40 -0700 | [diff] [blame] | 100 | unsigned long n_krcvqs; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 101 | |
| 102 | static unsigned hfi1_rcvarr_split = 25; |
| 103 | module_param_named(rcvarr_split, hfi1_rcvarr_split, uint, S_IRUGO); |
| 104 | MODULE_PARM_DESC(rcvarr_split, "Percent of context's RcvArray entries used for Eager buffers"); |
| 105 | |
Tymoteusz Kielan | 9746fa4 | 2017-05-04 05:14:22 -0700 | [diff] [blame] | 106 | static uint eager_buffer_size = (8 << 20); /* 8MB */ |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 107 | module_param(eager_buffer_size, uint, S_IRUGO); |
Tymoteusz Kielan | 9746fa4 | 2017-05-04 05:14:22 -0700 | [diff] [blame] | 108 | MODULE_PARM_DESC(eager_buffer_size, "Size of the eager buffers, default: 8MB"); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 109 | |
| 110 | static uint rcvhdrcnt = 2048; /* 2x the max eager buffer count */ |
| 111 | module_param_named(rcvhdrcnt, rcvhdrcnt, uint, S_IRUGO); |
| 112 | MODULE_PARM_DESC(rcvhdrcnt, "Receive header queue count (default 2048)"); |
| 113 | |
| 114 | static uint hfi1_hdrq_entsize = 32; |
| 115 | module_param_named(hdrq_entsize, hfi1_hdrq_entsize, uint, S_IRUGO); |
| 116 | MODULE_PARM_DESC(hdrq_entsize, "Size of header queue entries: 2 - 8B, 16 - 64B (default), 32 - 128B"); |
| 117 | |
| 118 | unsigned int user_credit_return_threshold = 33; /* default is 33% */ |
| 119 | module_param(user_credit_return_threshold, uint, S_IRUGO); |
Jubin John | ecb95a0 | 2015-12-17 19:24:14 -0500 | [diff] [blame] | 120 | MODULE_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 Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 121 | |
Michael J. Ruhl | f4cd876 | 2017-05-04 05:14:39 -0700 | [diff] [blame] | 122 | static inline u64 encode_rcv_header_entry_size(u16 size); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 123 | |
| 124 | static struct idr hfi1_unit_table; |
| 125 | u32 hfi1_cpulist_count; |
| 126 | unsigned long *hfi1_cpulist; |
| 127 | |
| 128 | /* |
| 129 | * Common code for creating the receive context array. |
| 130 | */ |
| 131 | int hfi1_create_ctxts(struct hfi1_devdata *dd) |
| 132 | { |
| 133 | unsigned i; |
| 134 | int ret; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 135 | |
Niranjana Vishwanathapura | 82c2611 | 2015-11-11 00:35:19 -0500 | [diff] [blame] | 136 | /* Control context has to be always 0 */ |
| 137 | BUILD_BUG_ON(HFI1_CTRL_CTXT != 0); |
| 138 | |
Mitko Haralanov | 377f111 | 2016-02-03 14:33:58 -0800 | [diff] [blame] | 139 | dd->rcd = kzalloc_node(dd->num_rcv_contexts * sizeof(*dd->rcd), |
| 140 | GFP_KERNEL, dd->node); |
Alison Schofield | 806e6e1 | 2015-10-12 14:28:36 -0700 | [diff] [blame] | 141 | if (!dd->rcd) |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 142 | goto nomem; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 143 | |
| 144 | /* create one or more kernel contexts */ |
Vishwanathapura, Niranjana | 2280740 | 2017-04-12 20:29:29 -0700 | [diff] [blame] | 145 | for (i = 0; i < dd->first_dyn_alloc_ctxt; ++i) { |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 146 | struct hfi1_pportdata *ppd; |
| 147 | struct hfi1_ctxtdata *rcd; |
| 148 | |
| 149 | ppd = dd->pport + (i % dd->num_pports); |
Jianxin Xiong | 4dfe7cc | 2016-10-17 04:19:41 -0700 | [diff] [blame] | 150 | |
| 151 | /* dd->rcd[i] gets assigned inside the callee */ |
Mitko Haralanov | 957558c | 2016-02-03 14:33:40 -0800 | [diff] [blame] | 152 | rcd = hfi1_create_ctxtdata(ppd, i, dd->node); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 153 | if (!rcd) { |
| 154 | dd_dev_err(dd, |
Jubin John | 17fb4f2 | 2016-02-14 20:21:52 -0800 | [diff] [blame] | 155 | "Unable to allocate kernel receive context, failing\n"); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 156 | goto nomem; |
| 157 | } |
| 158 | /* |
| 159 | * Set up the kernel context flags here and now because they |
| 160 | * use default values for all receive side memories. User |
| 161 | * contexts will be handled as they are created. |
| 162 | */ |
| 163 | rcd->flags = HFI1_CAP_KGET(MULTI_PKT_EGR) | |
| 164 | HFI1_CAP_KGET(NODROP_RHQ_FULL) | |
| 165 | HFI1_CAP_KGET(NODROP_EGR_FULL) | |
| 166 | HFI1_CAP_KGET(DMA_RTAIL); |
Niranjana Vishwanathapura | 82c2611 | 2015-11-11 00:35:19 -0500 | [diff] [blame] | 167 | |
| 168 | /* Control context must use DMA_RTAIL */ |
| 169 | if (rcd->ctxt == HFI1_CTRL_CTXT) |
| 170 | rcd->flags |= HFI1_CAP_DMA_RTAIL; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 171 | rcd->seq_cnt = 1; |
| 172 | |
| 173 | rcd->sc = sc_alloc(dd, SC_ACK, rcd->rcvhdrqentsize, dd->node); |
| 174 | if (!rcd->sc) { |
| 175 | dd_dev_err(dd, |
Jubin John | 17fb4f2 | 2016-02-14 20:21:52 -0800 | [diff] [blame] | 176 | "Unable to allocate kernel send context, failing\n"); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 177 | goto nomem; |
| 178 | } |
| 179 | |
Michael J. Ruhl | 9b60d2c | 2017-05-04 05:15:09 -0700 | [diff] [blame] | 180 | hfi1_init_ctxt(rcd->sc); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 181 | } |
| 182 | |
Ashutosh Dixit | affa48d | 2016-02-03 14:33:06 -0800 | [diff] [blame] | 183 | /* |
| 184 | * Initialize aspm, to be done after gen3 transition and setting up |
| 185 | * contexts and before enabling interrupts |
| 186 | */ |
| 187 | aspm_init(dd); |
| 188 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 189 | return 0; |
| 190 | nomem: |
| 191 | ret = -ENOMEM; |
Michael J. Ruhl | 9b60d2c | 2017-05-04 05:15:09 -0700 | [diff] [blame] | 192 | |
Jianxin Xiong | 4dfe7cc | 2016-10-17 04:19:41 -0700 | [diff] [blame] | 193 | if (dd->rcd) { |
| 194 | for (i = 0; i < dd->num_rcv_contexts; ++i) |
| 195 | hfi1_free_ctxtdata(dd, dd->rcd[i]); |
| 196 | } |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 197 | kfree(dd->rcd); |
| 198 | dd->rcd = NULL; |
| 199 | return ret; |
| 200 | } |
| 201 | |
| 202 | /* |
| 203 | * Common code for user and kernel context setup. |
| 204 | */ |
Mitko Haralanov | 957558c | 2016-02-03 14:33:40 -0800 | [diff] [blame] | 205 | struct hfi1_ctxtdata *hfi1_create_ctxtdata(struct hfi1_pportdata *ppd, u32 ctxt, |
| 206 | int numa) |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 207 | { |
| 208 | struct hfi1_devdata *dd = ppd->dd; |
| 209 | struct hfi1_ctxtdata *rcd; |
| 210 | unsigned kctxt_ngroups = 0; |
| 211 | u32 base; |
| 212 | |
| 213 | if (dd->rcv_entries.nctxt_extra > |
Vishwanathapura, Niranjana | 2280740 | 2017-04-12 20:29:29 -0700 | [diff] [blame] | 214 | dd->num_rcv_contexts - dd->first_dyn_alloc_ctxt) |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 215 | kctxt_ngroups = (dd->rcv_entries.nctxt_extra - |
Vishwanathapura, Niranjana | 2280740 | 2017-04-12 20:29:29 -0700 | [diff] [blame] | 216 | (dd->num_rcv_contexts - dd->first_dyn_alloc_ctxt)); |
Jianxin Xiong | 4dfe7cc | 2016-10-17 04:19:41 -0700 | [diff] [blame] | 217 | rcd = kzalloc_node(sizeof(*rcd), GFP_KERNEL, numa); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 218 | if (rcd) { |
| 219 | u32 rcvtids, max_entries; |
| 220 | |
Sebastian Sanchez | 6c63e42 | 2015-11-06 20:06:56 -0500 | [diff] [blame] | 221 | hfi1_cdbg(PROC, "setting up context %u\n", ctxt); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 222 | |
| 223 | INIT_LIST_HEAD(&rcd->qp_wait_list); |
| 224 | rcd->ppd = ppd; |
| 225 | rcd->dd = dd; |
Michael J. Ruhl | 8737ce9 | 2017-05-04 05:15:15 -0700 | [diff] [blame^] | 226 | __set_bit(0, rcd->in_use_ctxts); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 227 | rcd->ctxt = ctxt; |
| 228 | dd->rcd[ctxt] = rcd; |
Mitko Haralanov | 957558c | 2016-02-03 14:33:40 -0800 | [diff] [blame] | 229 | rcd->numa_id = numa; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 230 | rcd->rcv_array_groups = dd->rcv_entries.ngroups; |
| 231 | |
Mitko Haralanov | 463e6eb | 2016-02-05 11:57:53 -0500 | [diff] [blame] | 232 | mutex_init(&rcd->exp_lock); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 233 | |
| 234 | /* |
| 235 | * Calculate the context's RcvArray entry starting point. |
| 236 | * We do this here because we have to take into account all |
| 237 | * the RcvArray entries that previous context would have |
Vishwanathapura, Niranjana | 2280740 | 2017-04-12 20:29:29 -0700 | [diff] [blame] | 238 | * taken and we have to account for any extra groups assigned |
| 239 | * to the static (kernel) or dynamic (vnic/user) contexts. |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 240 | */ |
Vishwanathapura, Niranjana | 2280740 | 2017-04-12 20:29:29 -0700 | [diff] [blame] | 241 | if (ctxt < dd->first_dyn_alloc_ctxt) { |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 242 | if (ctxt < kctxt_ngroups) { |
| 243 | base = ctxt * (dd->rcv_entries.ngroups + 1); |
| 244 | rcd->rcv_array_groups++; |
Dennis Dalessandro | ee495ad | 2017-04-09 10:17:18 -0700 | [diff] [blame] | 245 | } else { |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 246 | base = kctxt_ngroups + |
| 247 | (ctxt * dd->rcv_entries.ngroups); |
Dennis Dalessandro | ee495ad | 2017-04-09 10:17:18 -0700 | [diff] [blame] | 248 | } |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 249 | } else { |
Vishwanathapura, Niranjana | 2280740 | 2017-04-12 20:29:29 -0700 | [diff] [blame] | 250 | u16 ct = ctxt - dd->first_dyn_alloc_ctxt; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 251 | |
| 252 | base = ((dd->n_krcv_queues * dd->rcv_entries.ngroups) + |
| 253 | kctxt_ngroups); |
| 254 | if (ct < dd->rcv_entries.nctxt_extra) { |
| 255 | base += ct * (dd->rcv_entries.ngroups + 1); |
| 256 | rcd->rcv_array_groups++; |
Dennis Dalessandro | ee495ad | 2017-04-09 10:17:18 -0700 | [diff] [blame] | 257 | } else { |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 258 | base += dd->rcv_entries.nctxt_extra + |
| 259 | (ct * dd->rcv_entries.ngroups); |
Dennis Dalessandro | ee495ad | 2017-04-09 10:17:18 -0700 | [diff] [blame] | 260 | } |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 261 | } |
| 262 | rcd->eager_base = base * dd->rcv_entries.group_size; |
| 263 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 264 | rcd->rcvhdrq_cnt = rcvhdrcnt; |
| 265 | rcd->rcvhdrqentsize = hfi1_hdrq_entsize; |
| 266 | /* |
| 267 | * Simple Eager buffer allocation: we have already pre-allocated |
| 268 | * the number of RcvArray entry groups. Each ctxtdata structure |
| 269 | * holds the number of groups for that context. |
| 270 | * |
| 271 | * To follow CSR requirements and maintain cacheline alignment, |
| 272 | * make sure all sizes and bases are multiples of group_size. |
| 273 | * |
| 274 | * The expected entry count is what is left after assigning |
| 275 | * eager. |
| 276 | */ |
| 277 | max_entries = rcd->rcv_array_groups * |
| 278 | dd->rcv_entries.group_size; |
| 279 | rcvtids = ((max_entries * hfi1_rcvarr_split) / 100); |
| 280 | rcd->egrbufs.count = round_down(rcvtids, |
| 281 | dd->rcv_entries.group_size); |
| 282 | if (rcd->egrbufs.count > MAX_EAGER_ENTRIES) { |
| 283 | dd_dev_err(dd, "ctxt%u: requested too many RcvArray entries.\n", |
| 284 | rcd->ctxt); |
| 285 | rcd->egrbufs.count = MAX_EAGER_ENTRIES; |
| 286 | } |
Sebastian Sanchez | 6c63e42 | 2015-11-06 20:06:56 -0500 | [diff] [blame] | 287 | hfi1_cdbg(PROC, |
| 288 | "ctxt%u: max Eager buffer RcvArray entries: %u\n", |
| 289 | rcd->ctxt, rcd->egrbufs.count); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 290 | |
| 291 | /* |
| 292 | * Allocate array that will hold the eager buffer accounting |
| 293 | * data. |
| 294 | * This will allocate the maximum possible buffer count based |
| 295 | * on the value of the RcvArray split parameter. |
| 296 | * The resulting value will be rounded down to the closest |
| 297 | * multiple of dd->rcv_entries.group_size. |
| 298 | */ |
Sebastian Sanchez | b448bf9 | 2017-02-08 05:26:37 -0800 | [diff] [blame] | 299 | rcd->egrbufs.buffers = kzalloc_node( |
| 300 | rcd->egrbufs.count * sizeof(*rcd->egrbufs.buffers), |
| 301 | GFP_KERNEL, numa); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 302 | if (!rcd->egrbufs.buffers) |
| 303 | goto bail; |
Sebastian Sanchez | b448bf9 | 2017-02-08 05:26:37 -0800 | [diff] [blame] | 304 | rcd->egrbufs.rcvtids = kzalloc_node( |
| 305 | rcd->egrbufs.count * |
| 306 | sizeof(*rcd->egrbufs.rcvtids), |
| 307 | GFP_KERNEL, numa); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 308 | if (!rcd->egrbufs.rcvtids) |
| 309 | goto bail; |
| 310 | rcd->egrbufs.size = eager_buffer_size; |
| 311 | /* |
| 312 | * The size of the buffers programmed into the RcvArray |
| 313 | * entries needs to be big enough to handle the highest |
| 314 | * MTU supported. |
| 315 | */ |
| 316 | if (rcd->egrbufs.size < hfi1_max_mtu) { |
| 317 | rcd->egrbufs.size = __roundup_pow_of_two(hfi1_max_mtu); |
Sebastian Sanchez | 6c63e42 | 2015-11-06 20:06:56 -0500 | [diff] [blame] | 318 | hfi1_cdbg(PROC, |
| 319 | "ctxt%u: eager bufs size too small. Adjusting to %zu\n", |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 320 | rcd->ctxt, rcd->egrbufs.size); |
| 321 | } |
| 322 | rcd->egrbufs.rcvtid_size = HFI1_MAX_EAGER_BUFFER_SIZE; |
| 323 | |
Vishwanathapura, Niranjana | 2280740 | 2017-04-12 20:29:29 -0700 | [diff] [blame] | 324 | /* Applicable only for statically created kernel contexts */ |
| 325 | if (ctxt < dd->first_dyn_alloc_ctxt) { |
Sebastian Sanchez | b448bf9 | 2017-02-08 05:26:37 -0800 | [diff] [blame] | 326 | rcd->opstats = kzalloc_node(sizeof(*rcd->opstats), |
| 327 | GFP_KERNEL, numa); |
Alison Schofield | 806e6e1 | 2015-10-12 14:28:36 -0700 | [diff] [blame] | 328 | if (!rcd->opstats) |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 329 | goto bail; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | return rcd; |
| 333 | bail: |
Jakub Pawlak | 3a6982d | 2016-09-25 07:42:23 -0700 | [diff] [blame] | 334 | dd->rcd[ctxt] = NULL; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 335 | kfree(rcd->egrbufs.rcvtids); |
| 336 | kfree(rcd->egrbufs.buffers); |
| 337 | kfree(rcd); |
| 338 | return NULL; |
| 339 | } |
| 340 | |
| 341 | /* |
| 342 | * Convert a receive header entry size that to the encoding used in the CSR. |
| 343 | * |
| 344 | * Return a zero if the given size is invalid. |
| 345 | */ |
| 346 | static inline u64 encode_rcv_header_entry_size(u16 size) |
| 347 | { |
| 348 | /* there are only 3 valid receive header entry sizes */ |
| 349 | if (size == 2) |
| 350 | return 1; |
| 351 | if (size == 16) |
| 352 | return 2; |
| 353 | else if (size == 32) |
| 354 | return 4; |
| 355 | return 0; /* invalid */ |
| 356 | } |
| 357 | |
| 358 | /* |
| 359 | * Select the largest ccti value over all SLs to determine the intra- |
| 360 | * packet gap for the link. |
| 361 | * |
| 362 | * called with cca_timer_lock held (to protect access to cca_timer |
| 363 | * array), and rcu_read_lock() (to protect access to cc_state). |
| 364 | */ |
| 365 | void set_link_ipg(struct hfi1_pportdata *ppd) |
| 366 | { |
| 367 | struct hfi1_devdata *dd = ppd->dd; |
| 368 | struct cc_state *cc_state; |
| 369 | int i; |
| 370 | u16 cce, ccti_limit, max_ccti = 0; |
| 371 | u16 shift, mult; |
| 372 | u64 src; |
| 373 | u32 current_egress_rate; /* Mbits /sec */ |
| 374 | u32 max_pkt_time; |
| 375 | /* |
| 376 | * max_pkt_time is the maximum packet egress time in units |
| 377 | * of the fabric clock period 1/(805 MHz). |
| 378 | */ |
| 379 | |
| 380 | cc_state = get_cc_state(ppd); |
| 381 | |
Jubin John | d125a6c | 2016-02-14 20:19:49 -0800 | [diff] [blame] | 382 | if (!cc_state) |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 383 | /* |
| 384 | * This should _never_ happen - rcu_read_lock() is held, |
| 385 | * and set_link_ipg() should not be called if cc_state |
| 386 | * is NULL. |
| 387 | */ |
| 388 | return; |
| 389 | |
| 390 | for (i = 0; i < OPA_MAX_SLS; i++) { |
| 391 | u16 ccti = ppd->cca_timer[i].ccti; |
| 392 | |
| 393 | if (ccti > max_ccti) |
| 394 | max_ccti = ccti; |
| 395 | } |
| 396 | |
| 397 | ccti_limit = cc_state->cct.ccti_limit; |
| 398 | if (max_ccti > ccti_limit) |
| 399 | max_ccti = ccti_limit; |
| 400 | |
| 401 | cce = cc_state->cct.entries[max_ccti].entry; |
| 402 | shift = (cce & 0xc000) >> 14; |
| 403 | mult = (cce & 0x3fff); |
| 404 | |
| 405 | current_egress_rate = active_egress_rate(ppd); |
| 406 | |
| 407 | max_pkt_time = egress_cycles(ppd->ibmaxlen, current_egress_rate); |
| 408 | |
| 409 | src = (max_pkt_time >> shift) * mult; |
| 410 | |
| 411 | src &= SEND_STATIC_RATE_CONTROL_CSR_SRC_RELOAD_SMASK; |
| 412 | src <<= SEND_STATIC_RATE_CONTROL_CSR_SRC_RELOAD_SHIFT; |
| 413 | |
| 414 | write_csr(dd, SEND_STATIC_RATE_CONTROL, src); |
| 415 | } |
| 416 | |
| 417 | static enum hrtimer_restart cca_timer_fn(struct hrtimer *t) |
| 418 | { |
| 419 | struct cca_timer *cca_timer; |
| 420 | struct hfi1_pportdata *ppd; |
| 421 | int sl; |
Jubin John | d35cf744 | 2016-04-14 08:31:53 -0700 | [diff] [blame] | 422 | u16 ccti_timer, ccti_min; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 423 | struct cc_state *cc_state; |
Dean Luick | b77d713 | 2015-10-26 10:28:43 -0400 | [diff] [blame] | 424 | unsigned long flags; |
Jubin John | d35cf744 | 2016-04-14 08:31:53 -0700 | [diff] [blame] | 425 | enum hrtimer_restart ret = HRTIMER_NORESTART; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 426 | |
| 427 | cca_timer = container_of(t, struct cca_timer, hrtimer); |
| 428 | ppd = cca_timer->ppd; |
| 429 | sl = cca_timer->sl; |
| 430 | |
| 431 | rcu_read_lock(); |
| 432 | |
| 433 | cc_state = get_cc_state(ppd); |
| 434 | |
Jubin John | d125a6c | 2016-02-14 20:19:49 -0800 | [diff] [blame] | 435 | if (!cc_state) { |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 436 | rcu_read_unlock(); |
| 437 | return HRTIMER_NORESTART; |
| 438 | } |
| 439 | |
| 440 | /* |
| 441 | * 1) decrement ccti for SL |
| 442 | * 2) calculate IPG for link (set_link_ipg()) |
| 443 | * 3) restart timer, unless ccti is at min value |
| 444 | */ |
| 445 | |
| 446 | ccti_min = cc_state->cong_setting.entries[sl].ccti_min; |
| 447 | ccti_timer = cc_state->cong_setting.entries[sl].ccti_timer; |
| 448 | |
Dean Luick | b77d713 | 2015-10-26 10:28:43 -0400 | [diff] [blame] | 449 | spin_lock_irqsave(&ppd->cca_timer_lock, flags); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 450 | |
Jubin John | d35cf744 | 2016-04-14 08:31:53 -0700 | [diff] [blame] | 451 | if (cca_timer->ccti > ccti_min) { |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 452 | cca_timer->ccti--; |
| 453 | set_link_ipg(ppd); |
| 454 | } |
| 455 | |
Jubin John | d35cf744 | 2016-04-14 08:31:53 -0700 | [diff] [blame] | 456 | if (cca_timer->ccti > ccti_min) { |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 457 | unsigned long nsec = 1024 * ccti_timer; |
| 458 | /* ccti_timer is in units of 1.024 usec */ |
| 459 | hrtimer_forward_now(t, ns_to_ktime(nsec)); |
Jubin John | d35cf744 | 2016-04-14 08:31:53 -0700 | [diff] [blame] | 460 | ret = HRTIMER_RESTART; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 461 | } |
Jubin John | d35cf744 | 2016-04-14 08:31:53 -0700 | [diff] [blame] | 462 | |
| 463 | spin_unlock_irqrestore(&ppd->cca_timer_lock, flags); |
| 464 | rcu_read_unlock(); |
| 465 | return ret; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | /* |
| 469 | * Common code for initializing the physical port structure. |
| 470 | */ |
| 471 | void hfi1_init_pportdata(struct pci_dev *pdev, struct hfi1_pportdata *ppd, |
| 472 | struct hfi1_devdata *dd, u8 hw_pidx, u8 port) |
| 473 | { |
Jianxin Xiong | 8adf71f | 2016-07-25 13:39:14 -0700 | [diff] [blame] | 474 | int i; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 475 | uint default_pkey_idx; |
Jianxin Xiong | 8adf71f | 2016-07-25 13:39:14 -0700 | [diff] [blame] | 476 | struct cc_state *cc_state; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 477 | |
| 478 | ppd->dd = dd; |
| 479 | ppd->hw_pidx = hw_pidx; |
| 480 | ppd->port = port; /* IB port number, not index */ |
| 481 | |
| 482 | default_pkey_idx = 1; |
| 483 | |
| 484 | ppd->pkeys[default_pkey_idx] = DEFAULT_P_KEY; |
Neel Desai | 5352650 | 2017-04-09 10:16:59 -0700 | [diff] [blame] | 485 | ppd->part_enforce |= HFI1_PART_ENFORCE_IN; |
| 486 | ppd->part_enforce |= HFI1_PART_ENFORCE_OUT; |
| 487 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 488 | if (loopback) { |
| 489 | hfi1_early_err(&pdev->dev, |
| 490 | "Faking data partition 0x8001 in idx %u\n", |
| 491 | !default_pkey_idx); |
| 492 | ppd->pkeys[!default_pkey_idx] = 0x8001; |
| 493 | } |
| 494 | |
| 495 | INIT_WORK(&ppd->link_vc_work, handle_verify_cap); |
| 496 | INIT_WORK(&ppd->link_up_work, handle_link_up); |
| 497 | INIT_WORK(&ppd->link_down_work, handle_link_down); |
| 498 | INIT_WORK(&ppd->freeze_work, handle_freeze); |
| 499 | INIT_WORK(&ppd->link_downgrade_work, handle_link_downgrade); |
| 500 | INIT_WORK(&ppd->sma_message_work, handle_sma_message); |
| 501 | INIT_WORK(&ppd->link_bounce_work, handle_link_bounce); |
Dean Luick | 673b975 | 2016-08-31 07:24:33 -0700 | [diff] [blame] | 502 | INIT_DELAYED_WORK(&ppd->start_link_work, handle_start_link); |
Jim Snow | fb9036d | 2016-01-11 18:32:21 -0500 | [diff] [blame] | 503 | INIT_WORK(&ppd->linkstate_active_work, receive_interrupt_work); |
Easwar Hariharan | 8ebd4cf | 2016-02-03 14:31:14 -0800 | [diff] [blame] | 504 | INIT_WORK(&ppd->qsfp_info.qsfp_work, qsfp_event); |
| 505 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 506 | mutex_init(&ppd->hls_lock); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 507 | spin_lock_init(&ppd->qsfp_info.qsfp_lock); |
| 508 | |
Easwar Hariharan | 8ebd4cf | 2016-02-03 14:31:14 -0800 | [diff] [blame] | 509 | ppd->qsfp_info.ppd = ppd; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 510 | ppd->sm_trap_qp = 0x0; |
| 511 | ppd->sa_qp = 0x1; |
| 512 | |
| 513 | ppd->hfi1_wq = NULL; |
| 514 | |
| 515 | spin_lock_init(&ppd->cca_timer_lock); |
| 516 | |
| 517 | for (i = 0; i < OPA_MAX_SLS; i++) { |
| 518 | hrtimer_init(&ppd->cca_timer[i].hrtimer, CLOCK_MONOTONIC, |
| 519 | HRTIMER_MODE_REL); |
| 520 | ppd->cca_timer[i].ppd = ppd; |
| 521 | ppd->cca_timer[i].sl = i; |
| 522 | ppd->cca_timer[i].ccti = 0; |
| 523 | ppd->cca_timer[i].hrtimer.function = cca_timer_fn; |
| 524 | } |
| 525 | |
| 526 | ppd->cc_max_table_entries = IB_CC_TABLE_CAP_DEFAULT; |
| 527 | |
| 528 | spin_lock_init(&ppd->cc_state_lock); |
| 529 | spin_lock_init(&ppd->cc_log_lock); |
Jianxin Xiong | 8adf71f | 2016-07-25 13:39:14 -0700 | [diff] [blame] | 530 | cc_state = kzalloc(sizeof(*cc_state), GFP_KERNEL); |
| 531 | RCU_INIT_POINTER(ppd->cc_state, cc_state); |
| 532 | if (!cc_state) |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 533 | goto bail; |
| 534 | return; |
| 535 | |
| 536 | bail: |
| 537 | |
| 538 | hfi1_early_err(&pdev->dev, |
| 539 | "Congestion Control Agent disabled for port %d\n", port); |
| 540 | } |
| 541 | |
| 542 | /* |
| 543 | * Do initialization for device that is only needed on |
| 544 | * first detect, not on resets. |
| 545 | */ |
| 546 | static int loadtime_init(struct hfi1_devdata *dd) |
| 547 | { |
| 548 | return 0; |
| 549 | } |
| 550 | |
| 551 | /** |
| 552 | * init_after_reset - re-initialize after a reset |
| 553 | * @dd: the hfi1_ib device |
| 554 | * |
| 555 | * sanity check at least some of the values after reset, and |
| 556 | * ensure no receive or transmit (explicitly, in case reset |
| 557 | * failed |
| 558 | */ |
| 559 | static int init_after_reset(struct hfi1_devdata *dd) |
| 560 | { |
| 561 | int i; |
| 562 | |
| 563 | /* |
| 564 | * Ensure chip does no sends or receives, tail updates, or |
| 565 | * pioavail updates while we re-initialize. This is mostly |
| 566 | * for the driver data structures, not chip registers. |
| 567 | */ |
| 568 | for (i = 0; i < dd->num_rcv_contexts; i++) |
| 569 | hfi1_rcvctrl(dd, HFI1_RCVCTRL_CTXT_DIS | |
| 570 | HFI1_RCVCTRL_INTRAVAIL_DIS | |
| 571 | HFI1_RCVCTRL_TAILUPD_DIS, i); |
| 572 | pio_send_control(dd, PSC_GLOBAL_DISABLE); |
| 573 | for (i = 0; i < dd->num_send_contexts; i++) |
| 574 | sc_disable(dd->send_contexts[i].sc); |
| 575 | |
| 576 | return 0; |
| 577 | } |
| 578 | |
| 579 | static void enable_chip(struct hfi1_devdata *dd) |
| 580 | { |
| 581 | u32 rcvmask; |
| 582 | u32 i; |
| 583 | |
| 584 | /* enable PIO send */ |
| 585 | pio_send_control(dd, PSC_GLOBAL_ENABLE); |
| 586 | |
| 587 | /* |
| 588 | * Enable kernel ctxts' receive and receive interrupt. |
| 589 | * Other ctxts done as user opens and initializes them. |
| 590 | */ |
Vishwanathapura, Niranjana | 2280740 | 2017-04-12 20:29:29 -0700 | [diff] [blame] | 591 | for (i = 0; i < dd->first_dyn_alloc_ctxt; ++i) { |
Mitko Haralanov | 566c157 | 2016-02-03 14:32:49 -0800 | [diff] [blame] | 592 | rcvmask = HFI1_RCVCTRL_CTXT_ENB | HFI1_RCVCTRL_INTRAVAIL_ENB; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 593 | rcvmask |= HFI1_CAP_KGET_MASK(dd->rcd[i]->flags, DMA_RTAIL) ? |
| 594 | HFI1_RCVCTRL_TAILUPD_ENB : HFI1_RCVCTRL_TAILUPD_DIS; |
| 595 | if (!HFI1_CAP_KGET_MASK(dd->rcd[i]->flags, MULTI_PKT_EGR)) |
| 596 | rcvmask |= HFI1_RCVCTRL_ONE_PKT_EGR_ENB; |
| 597 | if (HFI1_CAP_KGET_MASK(dd->rcd[i]->flags, NODROP_RHQ_FULL)) |
| 598 | rcvmask |= HFI1_RCVCTRL_NO_RHQ_DROP_ENB; |
| 599 | if (HFI1_CAP_KGET_MASK(dd->rcd[i]->flags, NODROP_EGR_FULL)) |
| 600 | rcvmask |= HFI1_RCVCTRL_NO_EGR_DROP_ENB; |
| 601 | hfi1_rcvctrl(dd, rcvmask, i); |
| 602 | sc_enable(dd->rcd[i]->sc); |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | /** |
| 607 | * create_workqueues - create per port workqueues |
| 608 | * @dd: the hfi1_ib device |
| 609 | */ |
| 610 | static int create_workqueues(struct hfi1_devdata *dd) |
| 611 | { |
| 612 | int pidx; |
| 613 | struct hfi1_pportdata *ppd; |
| 614 | |
| 615 | for (pidx = 0; pidx < dd->num_pports; ++pidx) { |
| 616 | ppd = dd->pport + pidx; |
| 617 | if (!ppd->hfi1_wq) { |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 618 | ppd->hfi1_wq = |
Mike Marciniszyn | 0a226ed | 2015-11-09 19:13:58 -0500 | [diff] [blame] | 619 | alloc_workqueue( |
| 620 | "hfi%d_%d", |
| 621 | WQ_SYSFS | WQ_HIGHPRI | WQ_CPU_INTENSIVE, |
Mike Marciniszyn | dd1ed10 | 2017-05-04 05:14:10 -0700 | [diff] [blame] | 622 | HFI1_MAX_ACTIVE_WORKQUEUE_ENTRIES, |
Mike Marciniszyn | 0a226ed | 2015-11-09 19:13:58 -0500 | [diff] [blame] | 623 | dd->unit, pidx); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 624 | if (!ppd->hfi1_wq) |
| 625 | goto wq_error; |
| 626 | } |
| 627 | } |
| 628 | return 0; |
| 629 | wq_error: |
Mike Marciniszyn | 0a226ed | 2015-11-09 19:13:58 -0500 | [diff] [blame] | 630 | pr_err("alloc_workqueue failed for port %d\n", pidx + 1); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 631 | for (pidx = 0; pidx < dd->num_pports; ++pidx) { |
| 632 | ppd = dd->pport + pidx; |
| 633 | if (ppd->hfi1_wq) { |
| 634 | destroy_workqueue(ppd->hfi1_wq); |
| 635 | ppd->hfi1_wq = NULL; |
| 636 | } |
| 637 | } |
| 638 | return -ENOMEM; |
| 639 | } |
| 640 | |
| 641 | /** |
| 642 | * hfi1_init - do the actual initialization sequence on the chip |
| 643 | * @dd: the hfi1_ib device |
| 644 | * @reinit: re-initializing, so don't allocate new memory |
| 645 | * |
| 646 | * Do the actual initialization sequence on the chip. This is done |
| 647 | * both from the init routine called from the PCI infrastructure, and |
| 648 | * when we reset the chip, or detect that it was reset internally, |
| 649 | * or it's administratively re-enabled. |
| 650 | * |
| 651 | * Memory allocation here and in called routines is only done in |
| 652 | * the first case (reinit == 0). We have to be careful, because even |
| 653 | * without memory allocation, we need to re-write all the chip registers |
| 654 | * TIDs, etc. after the reset or enable has completed. |
| 655 | */ |
| 656 | int hfi1_init(struct hfi1_devdata *dd, int reinit) |
| 657 | { |
| 658 | int ret = 0, pidx, lastfail = 0; |
| 659 | unsigned i, len; |
| 660 | struct hfi1_ctxtdata *rcd; |
| 661 | struct hfi1_pportdata *ppd; |
| 662 | |
| 663 | /* Set up recv low level handlers */ |
| 664 | dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_EXPECTED] = |
| 665 | kdeth_process_expected; |
| 666 | dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_EAGER] = |
| 667 | kdeth_process_eager; |
| 668 | dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_IB] = process_receive_ib; |
| 669 | dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_ERROR] = |
| 670 | process_receive_error; |
| 671 | dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_BYPASS] = |
| 672 | process_receive_bypass; |
| 673 | dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_INVALID5] = |
| 674 | process_receive_invalid; |
| 675 | dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_INVALID6] = |
| 676 | process_receive_invalid; |
| 677 | dd->normal_rhf_rcv_functions[RHF_RCV_TYPE_INVALID7] = |
| 678 | process_receive_invalid; |
| 679 | dd->rhf_rcv_function_map = dd->normal_rhf_rcv_functions; |
| 680 | |
| 681 | /* Set up send low level handlers */ |
| 682 | dd->process_pio_send = hfi1_verbs_send_pio; |
| 683 | dd->process_dma_send = hfi1_verbs_send_dma; |
| 684 | dd->pio_inline_send = pio_copy; |
Vishwanathapura, Niranjana | 64551ed | 2017-04-12 20:29:30 -0700 | [diff] [blame] | 685 | dd->process_vnic_dma_send = hfi1_vnic_send_dma; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 686 | |
Mike Marciniszyn | 995deaf | 2015-11-16 21:59:29 -0500 | [diff] [blame] | 687 | if (is_ax(dd)) { |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 688 | atomic_set(&dd->drop_packet, DROP_PACKET_ON); |
| 689 | dd->do_drop = 1; |
| 690 | } else { |
| 691 | atomic_set(&dd->drop_packet, DROP_PACKET_OFF); |
| 692 | dd->do_drop = 0; |
| 693 | } |
| 694 | |
| 695 | /* make sure the link is not "up" */ |
| 696 | for (pidx = 0; pidx < dd->num_pports; ++pidx) { |
| 697 | ppd = dd->pport + pidx; |
| 698 | ppd->linkup = 0; |
| 699 | } |
| 700 | |
| 701 | if (reinit) |
| 702 | ret = init_after_reset(dd); |
| 703 | else |
| 704 | ret = loadtime_init(dd); |
| 705 | if (ret) |
| 706 | goto done; |
| 707 | |
Mark F. Brown | 46b010d | 2015-11-09 19:18:20 -0500 | [diff] [blame] | 708 | /* allocate dummy tail memory for all receive contexts */ |
| 709 | dd->rcvhdrtail_dummy_kvaddr = dma_zalloc_coherent( |
| 710 | &dd->pcidev->dev, sizeof(u64), |
Tymoteusz Kielan | 6036818 | 2016-09-06 04:35:54 -0700 | [diff] [blame] | 711 | &dd->rcvhdrtail_dummy_dma, |
Mark F. Brown | 46b010d | 2015-11-09 19:18:20 -0500 | [diff] [blame] | 712 | GFP_KERNEL); |
| 713 | |
| 714 | if (!dd->rcvhdrtail_dummy_kvaddr) { |
| 715 | dd_dev_err(dd, "cannot allocate dummy tail memory\n"); |
| 716 | ret = -ENOMEM; |
| 717 | goto done; |
| 718 | } |
| 719 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 720 | /* dd->rcd can be NULL if early initialization failed */ |
Vishwanathapura, Niranjana | 2280740 | 2017-04-12 20:29:29 -0700 | [diff] [blame] | 721 | for (i = 0; dd->rcd && i < dd->first_dyn_alloc_ctxt; ++i) { |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 722 | /* |
| 723 | * Set up the (kernel) rcvhdr queue and egr TIDs. If doing |
| 724 | * re-init, the simplest way to handle this is to free |
| 725 | * existing, and re-allocate. |
| 726 | * Need to re-create rest of ctxt 0 ctxtdata as well. |
| 727 | */ |
| 728 | rcd = dd->rcd[i]; |
| 729 | if (!rcd) |
| 730 | continue; |
| 731 | |
| 732 | rcd->do_interrupt = &handle_receive_interrupt; |
| 733 | |
| 734 | lastfail = hfi1_create_rcvhdrq(dd, rcd); |
| 735 | if (!lastfail) |
| 736 | lastfail = hfi1_setup_eagerbufs(rcd); |
Ashutosh Dixit | 3923979 | 2016-05-12 10:24:00 -0700 | [diff] [blame] | 737 | if (lastfail) { |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 738 | dd_dev_err(dd, |
Jubin John | 17fb4f2 | 2016-02-14 20:21:52 -0800 | [diff] [blame] | 739 | "failed to allocate kernel ctxt's rcvhdrq and/or egr bufs\n"); |
Ashutosh Dixit | 3923979 | 2016-05-12 10:24:00 -0700 | [diff] [blame] | 740 | ret = lastfail; |
| 741 | } |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 742 | } |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 743 | |
| 744 | /* Allocate enough memory for user event notification. */ |
Amitoj Kaur Chawla | 8444991 | 2016-03-04 22:30:43 +0530 | [diff] [blame] | 745 | len = PAGE_ALIGN(dd->chip_rcv_contexts * HFI1_MAX_SHARED_CTXTS * |
| 746 | sizeof(*dd->events)); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 747 | dd->events = vmalloc_user(len); |
| 748 | if (!dd->events) |
| 749 | dd_dev_err(dd, "Failed to allocate user events page\n"); |
| 750 | /* |
| 751 | * Allocate a page for device and port status. |
| 752 | * Page will be shared amongst all user processes. |
| 753 | */ |
| 754 | dd->status = vmalloc_user(PAGE_SIZE); |
| 755 | if (!dd->status) |
| 756 | dd_dev_err(dd, "Failed to allocate dev status page\n"); |
| 757 | else |
| 758 | dd->freezelen = PAGE_SIZE - (sizeof(*dd->status) - |
| 759 | sizeof(dd->status->freezemsg)); |
| 760 | for (pidx = 0; pidx < dd->num_pports; ++pidx) { |
| 761 | ppd = dd->pport + pidx; |
| 762 | if (dd->status) |
| 763 | /* Currently, we only have one port */ |
| 764 | ppd->statusp = &dd->status->port; |
| 765 | |
| 766 | set_mtu(ppd); |
| 767 | } |
| 768 | |
| 769 | /* enable chip even if we have an error, so we can debug cause */ |
| 770 | enable_chip(dd); |
| 771 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 772 | done: |
| 773 | /* |
| 774 | * Set status even if port serdes is not initialized |
| 775 | * so that diags will work. |
| 776 | */ |
| 777 | if (dd->status) |
| 778 | dd->status->dev |= HFI1_STATUS_CHIP_PRESENT | |
| 779 | HFI1_STATUS_INITTED; |
| 780 | if (!ret) { |
| 781 | /* enable all interrupts from the chip */ |
| 782 | set_intr_state(dd, 1); |
| 783 | |
| 784 | /* chip is OK for user apps; mark it as initialized */ |
| 785 | for (pidx = 0; pidx < dd->num_pports; ++pidx) { |
| 786 | ppd = dd->pport + pidx; |
| 787 | |
Jubin John | 4d114fd | 2016-02-14 20:21:43 -0800 | [diff] [blame] | 788 | /* |
| 789 | * start the serdes - must be after interrupts are |
| 790 | * enabled so we are notified when the link goes up |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 791 | */ |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 792 | lastfail = bringup_serdes(ppd); |
| 793 | if (lastfail) |
| 794 | dd_dev_info(dd, |
Jubin John | 17fb4f2 | 2016-02-14 20:21:52 -0800 | [diff] [blame] | 795 | "Failed to bring up port %u\n", |
| 796 | ppd->port); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 797 | |
| 798 | /* |
| 799 | * Set status even if port serdes is not initialized |
| 800 | * so that diags will work. |
| 801 | */ |
| 802 | if (ppd->statusp) |
| 803 | *ppd->statusp |= HFI1_STATUS_CHIP_PRESENT | |
| 804 | HFI1_STATUS_INITTED; |
| 805 | if (!ppd->link_speed_enabled) |
| 806 | continue; |
| 807 | } |
| 808 | } |
| 809 | |
| 810 | /* if ret is non-zero, we probably should do some cleanup here... */ |
| 811 | return ret; |
| 812 | } |
| 813 | |
| 814 | static inline struct hfi1_devdata *__hfi1_lookup(int unit) |
| 815 | { |
| 816 | return idr_find(&hfi1_unit_table, unit); |
| 817 | } |
| 818 | |
| 819 | struct hfi1_devdata *hfi1_lookup(int unit) |
| 820 | { |
| 821 | struct hfi1_devdata *dd; |
| 822 | unsigned long flags; |
| 823 | |
| 824 | spin_lock_irqsave(&hfi1_devs_lock, flags); |
| 825 | dd = __hfi1_lookup(unit); |
| 826 | spin_unlock_irqrestore(&hfi1_devs_lock, flags); |
| 827 | |
| 828 | return dd; |
| 829 | } |
| 830 | |
| 831 | /* |
| 832 | * Stop the timers during unit shutdown, or after an error late |
| 833 | * in initialization. |
| 834 | */ |
| 835 | static void stop_timers(struct hfi1_devdata *dd) |
| 836 | { |
| 837 | struct hfi1_pportdata *ppd; |
| 838 | int pidx; |
| 839 | |
| 840 | for (pidx = 0; pidx < dd->num_pports; ++pidx) { |
| 841 | ppd = dd->pport + pidx; |
| 842 | if (ppd->led_override_timer.data) { |
| 843 | del_timer_sync(&ppd->led_override_timer); |
| 844 | atomic_set(&ppd->led_override_timer_active, 0); |
| 845 | } |
| 846 | } |
| 847 | } |
| 848 | |
| 849 | /** |
| 850 | * shutdown_device - shut down a device |
| 851 | * @dd: the hfi1_ib device |
| 852 | * |
| 853 | * This is called to make the device quiet when we are about to |
| 854 | * unload the driver, and also when the device is administratively |
| 855 | * disabled. It does not free any data structures. |
| 856 | * Everything it does has to be setup again by hfi1_init(dd, 1) |
| 857 | */ |
| 858 | static void shutdown_device(struct hfi1_devdata *dd) |
| 859 | { |
| 860 | struct hfi1_pportdata *ppd; |
| 861 | unsigned pidx; |
| 862 | int i; |
| 863 | |
| 864 | for (pidx = 0; pidx < dd->num_pports; ++pidx) { |
| 865 | ppd = dd->pport + pidx; |
| 866 | |
| 867 | ppd->linkup = 0; |
| 868 | if (ppd->statusp) |
| 869 | *ppd->statusp &= ~(HFI1_STATUS_IB_CONF | |
| 870 | HFI1_STATUS_IB_READY); |
| 871 | } |
| 872 | dd->flags &= ~HFI1_INITTED; |
| 873 | |
| 874 | /* mask interrupts, but not errors */ |
| 875 | set_intr_state(dd, 0); |
| 876 | |
| 877 | for (pidx = 0; pidx < dd->num_pports; ++pidx) { |
| 878 | ppd = dd->pport + pidx; |
| 879 | for (i = 0; i < dd->num_rcv_contexts; i++) |
| 880 | hfi1_rcvctrl(dd, HFI1_RCVCTRL_TAILUPD_DIS | |
| 881 | HFI1_RCVCTRL_CTXT_DIS | |
| 882 | HFI1_RCVCTRL_INTRAVAIL_DIS | |
| 883 | HFI1_RCVCTRL_PKEY_DIS | |
| 884 | HFI1_RCVCTRL_ONE_PKT_EGR_DIS, i); |
| 885 | /* |
| 886 | * Gracefully stop all sends allowing any in progress to |
| 887 | * trickle out first. |
| 888 | */ |
| 889 | for (i = 0; i < dd->num_send_contexts; i++) |
| 890 | sc_flush(dd->send_contexts[i].sc); |
| 891 | } |
| 892 | |
| 893 | /* |
| 894 | * Enough for anything that's going to trickle out to have actually |
| 895 | * done so. |
| 896 | */ |
| 897 | udelay(20); |
| 898 | |
| 899 | for (pidx = 0; pidx < dd->num_pports; ++pidx) { |
| 900 | ppd = dd->pport + pidx; |
| 901 | |
| 902 | /* disable all contexts */ |
| 903 | for (i = 0; i < dd->num_send_contexts; i++) |
| 904 | sc_disable(dd->send_contexts[i].sc); |
| 905 | /* disable the send device */ |
| 906 | pio_send_control(dd, PSC_GLOBAL_DISABLE); |
| 907 | |
Easwar Hariharan | 91ab4ed | 2016-02-03 14:35:57 -0800 | [diff] [blame] | 908 | shutdown_led_override(ppd); |
| 909 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 910 | /* |
| 911 | * Clear SerdesEnable. |
| 912 | * We can't count on interrupts since we are stopping. |
| 913 | */ |
| 914 | hfi1_quiet_serdes(ppd); |
| 915 | |
| 916 | if (ppd->hfi1_wq) { |
| 917 | destroy_workqueue(ppd->hfi1_wq); |
| 918 | ppd->hfi1_wq = NULL; |
| 919 | } |
| 920 | } |
| 921 | sdma_exit(dd); |
| 922 | } |
| 923 | |
| 924 | /** |
| 925 | * hfi1_free_ctxtdata - free a context's allocated data |
| 926 | * @dd: the hfi1_ib device |
| 927 | * @rcd: the ctxtdata structure |
| 928 | * |
| 929 | * free up any allocated data for a context |
| 930 | * This should not touch anything that would affect a simultaneous |
| 931 | * re-allocation of context data, because it is called after hfi1_mutex |
| 932 | * is released (and can be called from reinit as well). |
| 933 | * It should never change any chip state, or global driver state. |
| 934 | */ |
| 935 | void hfi1_free_ctxtdata(struct hfi1_devdata *dd, struct hfi1_ctxtdata *rcd) |
| 936 | { |
| 937 | unsigned e; |
| 938 | |
| 939 | if (!rcd) |
| 940 | return; |
| 941 | |
| 942 | if (rcd->rcvhdrq) { |
| 943 | dma_free_coherent(&dd->pcidev->dev, rcd->rcvhdrq_size, |
Tymoteusz Kielan | 6036818 | 2016-09-06 04:35:54 -0700 | [diff] [blame] | 944 | rcd->rcvhdrq, rcd->rcvhdrq_dma); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 945 | rcd->rcvhdrq = NULL; |
| 946 | if (rcd->rcvhdrtail_kvaddr) { |
| 947 | dma_free_coherent(&dd->pcidev->dev, PAGE_SIZE, |
| 948 | (void *)rcd->rcvhdrtail_kvaddr, |
Tymoteusz Kielan | 6036818 | 2016-09-06 04:35:54 -0700 | [diff] [blame] | 949 | rcd->rcvhdrqtailaddr_dma); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 950 | rcd->rcvhdrtail_kvaddr = NULL; |
| 951 | } |
| 952 | } |
| 953 | |
| 954 | /* all the RcvArray entries should have been cleared by now */ |
| 955 | kfree(rcd->egrbufs.rcvtids); |
| 956 | |
| 957 | for (e = 0; e < rcd->egrbufs.alloced; e++) { |
Tymoteusz Kielan | 6036818 | 2016-09-06 04:35:54 -0700 | [diff] [blame] | 958 | if (rcd->egrbufs.buffers[e].dma) |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 959 | dma_free_coherent(&dd->pcidev->dev, |
| 960 | rcd->egrbufs.buffers[e].len, |
| 961 | rcd->egrbufs.buffers[e].addr, |
Tymoteusz Kielan | 6036818 | 2016-09-06 04:35:54 -0700 | [diff] [blame] | 962 | rcd->egrbufs.buffers[e].dma); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 963 | } |
| 964 | kfree(rcd->egrbufs.buffers); |
| 965 | |
| 966 | sc_free(rcd->sc); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 967 | vfree(rcd->user_event_mask); |
| 968 | vfree(rcd->subctxt_uregbase); |
| 969 | vfree(rcd->subctxt_rcvegrbuf); |
| 970 | vfree(rcd->subctxt_rcvhdr_base); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 971 | kfree(rcd->opstats); |
| 972 | kfree(rcd); |
| 973 | } |
| 974 | |
Dean Luick | 78eb129 | 2016-03-05 08:49:45 -0800 | [diff] [blame] | 975 | /* |
| 976 | * Release our hold on the shared asic data. If we are the last one, |
Dean Luick | dba715f | 2016-07-06 17:28:52 -0400 | [diff] [blame] | 977 | * return the structure to be finalized outside the lock. Must be |
| 978 | * holding hfi1_devs_lock. |
Dean Luick | 78eb129 | 2016-03-05 08:49:45 -0800 | [diff] [blame] | 979 | */ |
Dean Luick | dba715f | 2016-07-06 17:28:52 -0400 | [diff] [blame] | 980 | static struct hfi1_asic_data *release_asic_data(struct hfi1_devdata *dd) |
Dean Luick | 78eb129 | 2016-03-05 08:49:45 -0800 | [diff] [blame] | 981 | { |
Dean Luick | dba715f | 2016-07-06 17:28:52 -0400 | [diff] [blame] | 982 | struct hfi1_asic_data *ad; |
Dean Luick | 78eb129 | 2016-03-05 08:49:45 -0800 | [diff] [blame] | 983 | int other; |
| 984 | |
| 985 | if (!dd->asic_data) |
Dean Luick | dba715f | 2016-07-06 17:28:52 -0400 | [diff] [blame] | 986 | return NULL; |
Dean Luick | 78eb129 | 2016-03-05 08:49:45 -0800 | [diff] [blame] | 987 | dd->asic_data->dds[dd->hfi1_id] = NULL; |
| 988 | other = dd->hfi1_id ? 0 : 1; |
Dean Luick | dba715f | 2016-07-06 17:28:52 -0400 | [diff] [blame] | 989 | ad = dd->asic_data; |
Dean Luick | 78eb129 | 2016-03-05 08:49:45 -0800 | [diff] [blame] | 990 | dd->asic_data = NULL; |
Dean Luick | dba715f | 2016-07-06 17:28:52 -0400 | [diff] [blame] | 991 | /* return NULL if the other dd still has a link */ |
| 992 | return ad->dds[other] ? NULL : ad; |
| 993 | } |
| 994 | |
| 995 | static void finalize_asic_data(struct hfi1_devdata *dd, |
| 996 | struct hfi1_asic_data *ad) |
| 997 | { |
| 998 | clean_up_i2c(dd, ad); |
| 999 | kfree(ad); |
Dean Luick | 78eb129 | 2016-03-05 08:49:45 -0800 | [diff] [blame] | 1000 | } |
| 1001 | |
Dennis Dalessandro | e11ffbd | 2016-05-19 05:26:44 -0700 | [diff] [blame] | 1002 | static void __hfi1_free_devdata(struct kobject *kobj) |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1003 | { |
Dennis Dalessandro | e11ffbd | 2016-05-19 05:26:44 -0700 | [diff] [blame] | 1004 | struct hfi1_devdata *dd = |
| 1005 | container_of(kobj, struct hfi1_devdata, kobj); |
Dean Luick | dba715f | 2016-07-06 17:28:52 -0400 | [diff] [blame] | 1006 | struct hfi1_asic_data *ad; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1007 | unsigned long flags; |
| 1008 | |
| 1009 | spin_lock_irqsave(&hfi1_devs_lock, flags); |
| 1010 | idr_remove(&hfi1_unit_table, dd->unit); |
| 1011 | list_del(&dd->list); |
Dean Luick | dba715f | 2016-07-06 17:28:52 -0400 | [diff] [blame] | 1012 | ad = release_asic_data(dd); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1013 | spin_unlock_irqrestore(&hfi1_devs_lock, flags); |
Dean Luick | dba715f | 2016-07-06 17:28:52 -0400 | [diff] [blame] | 1014 | if (ad) |
| 1015 | finalize_asic_data(dd, ad); |
Easwar Hariharan | c3838b3 | 2016-02-09 14:29:13 -0800 | [diff] [blame] | 1016 | free_platform_config(dd); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1017 | rcu_barrier(); /* wait for rcu callbacks to complete */ |
| 1018 | free_percpu(dd->int_counter); |
| 1019 | free_percpu(dd->rcv_limit); |
Vennila Megavannan | 89abfc8 | 2016-02-03 14:34:07 -0800 | [diff] [blame] | 1020 | free_percpu(dd->send_schedule); |
Jubin John | ea0e4ce | 2016-04-20 06:05:24 -0700 | [diff] [blame] | 1021 | rvt_dealloc_device(&dd->verbs_dev.rdi); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1022 | } |
| 1023 | |
Dennis Dalessandro | e11ffbd | 2016-05-19 05:26:44 -0700 | [diff] [blame] | 1024 | static struct kobj_type hfi1_devdata_type = { |
| 1025 | .release = __hfi1_free_devdata, |
| 1026 | }; |
| 1027 | |
| 1028 | void hfi1_free_devdata(struct hfi1_devdata *dd) |
| 1029 | { |
| 1030 | kobject_put(&dd->kobj); |
| 1031 | } |
| 1032 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1033 | /* |
| 1034 | * Allocate our primary per-unit data structure. Must be done via verbs |
| 1035 | * allocator, because the verbs cleanup process both does cleanup and |
| 1036 | * free of the data structure. |
| 1037 | * "extra" is for chip-specific data. |
| 1038 | * |
| 1039 | * Use the idr mechanism to get a unit number for this unit. |
| 1040 | */ |
| 1041 | struct hfi1_devdata *hfi1_alloc_devdata(struct pci_dev *pdev, size_t extra) |
| 1042 | { |
| 1043 | unsigned long flags; |
| 1044 | struct hfi1_devdata *dd; |
Dennis Dalessandro | 7af6d00 | 2016-01-19 14:44:06 -0800 | [diff] [blame] | 1045 | int ret, nports; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1046 | |
Dennis Dalessandro | 7af6d00 | 2016-01-19 14:44:06 -0800 | [diff] [blame] | 1047 | /* extra is * number of ports */ |
| 1048 | nports = extra / sizeof(struct hfi1_pportdata); |
| 1049 | |
| 1050 | dd = (struct hfi1_devdata *)rvt_alloc_device(sizeof(*dd) + extra, |
| 1051 | nports); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1052 | if (!dd) |
| 1053 | return ERR_PTR(-ENOMEM); |
Dennis Dalessandro | 7af6d00 | 2016-01-19 14:44:06 -0800 | [diff] [blame] | 1054 | dd->num_pports = nports; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1055 | dd->pport = (struct hfi1_pportdata *)(dd + 1); |
| 1056 | |
| 1057 | INIT_LIST_HEAD(&dd->list); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1058 | idr_preload(GFP_KERNEL); |
| 1059 | spin_lock_irqsave(&hfi1_devs_lock, flags); |
| 1060 | |
| 1061 | ret = idr_alloc(&hfi1_unit_table, dd, 0, 0, GFP_NOWAIT); |
| 1062 | if (ret >= 0) { |
| 1063 | dd->unit = ret; |
| 1064 | list_add(&dd->list, &hfi1_dev_list); |
| 1065 | } |
| 1066 | |
| 1067 | spin_unlock_irqrestore(&hfi1_devs_lock, flags); |
| 1068 | idr_preload_end(); |
| 1069 | |
| 1070 | if (ret < 0) { |
| 1071 | hfi1_early_err(&pdev->dev, |
| 1072 | "Could not allocate unit ID: error %d\n", -ret); |
| 1073 | goto bail; |
| 1074 | } |
| 1075 | /* |
| 1076 | * Initialize all locks for the device. This needs to be as early as |
| 1077 | * possible so locks are usable. |
| 1078 | */ |
| 1079 | spin_lock_init(&dd->sc_lock); |
| 1080 | spin_lock_init(&dd->sendctrl_lock); |
| 1081 | spin_lock_init(&dd->rcvctrl_lock); |
| 1082 | spin_lock_init(&dd->uctxt_lock); |
| 1083 | spin_lock_init(&dd->hfi1_diag_trans_lock); |
| 1084 | spin_lock_init(&dd->sc_init_lock); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1085 | spin_lock_init(&dd->dc8051_memlock); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1086 | seqlock_init(&dd->sc2vl_lock); |
| 1087 | spin_lock_init(&dd->sde_map_lock); |
Jubin John | 35f6bef | 2016-02-14 12:46:10 -0800 | [diff] [blame] | 1088 | spin_lock_init(&dd->pio_map_lock); |
Tadeusz Struk | 22546b7 | 2017-04-28 10:40:02 -0700 | [diff] [blame] | 1089 | mutex_init(&dd->dc8051_lock); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1090 | init_waitqueue_head(&dd->event_queue); |
| 1091 | |
| 1092 | dd->int_counter = alloc_percpu(u64); |
| 1093 | if (!dd->int_counter) { |
| 1094 | ret = -ENOMEM; |
| 1095 | hfi1_early_err(&pdev->dev, |
| 1096 | "Could not allocate per-cpu int_counter\n"); |
| 1097 | goto bail; |
| 1098 | } |
| 1099 | |
| 1100 | dd->rcv_limit = alloc_percpu(u64); |
| 1101 | if (!dd->rcv_limit) { |
| 1102 | ret = -ENOMEM; |
| 1103 | hfi1_early_err(&pdev->dev, |
| 1104 | "Could not allocate per-cpu rcv_limit\n"); |
| 1105 | goto bail; |
| 1106 | } |
| 1107 | |
Vennila Megavannan | 89abfc8 | 2016-02-03 14:34:07 -0800 | [diff] [blame] | 1108 | dd->send_schedule = alloc_percpu(u64); |
| 1109 | if (!dd->send_schedule) { |
| 1110 | ret = -ENOMEM; |
| 1111 | hfi1_early_err(&pdev->dev, |
| 1112 | "Could not allocate per-cpu int_counter\n"); |
| 1113 | goto bail; |
| 1114 | } |
| 1115 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1116 | if (!hfi1_cpulist_count) { |
| 1117 | u32 count = num_online_cpus(); |
| 1118 | |
Shraddha Barke | 314fcc0 | 2015-10-09 21:03:26 +0530 | [diff] [blame] | 1119 | hfi1_cpulist = kcalloc(BITS_TO_LONGS(count), sizeof(long), |
| 1120 | GFP_KERNEL); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1121 | if (hfi1_cpulist) |
| 1122 | hfi1_cpulist_count = count; |
| 1123 | else |
| 1124 | hfi1_early_err( |
| 1125 | &pdev->dev, |
| 1126 | "Could not alloc cpulist info, cpu affinity might be wrong\n"); |
| 1127 | } |
Dennis Dalessandro | e11ffbd | 2016-05-19 05:26:44 -0700 | [diff] [blame] | 1128 | kobject_init(&dd->kobj, &hfi1_devdata_type); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1129 | return dd; |
| 1130 | |
| 1131 | bail: |
| 1132 | if (!list_empty(&dd->list)) |
| 1133 | list_del_init(&dd->list); |
Jubin John | ea0e4ce | 2016-04-20 06:05:24 -0700 | [diff] [blame] | 1134 | rvt_dealloc_device(&dd->verbs_dev.rdi); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1135 | return ERR_PTR(ret); |
| 1136 | } |
| 1137 | |
| 1138 | /* |
| 1139 | * Called from freeze mode handlers, and from PCI error |
| 1140 | * reporting code. Should be paranoid about state of |
| 1141 | * system and data structures. |
| 1142 | */ |
| 1143 | void hfi1_disable_after_error(struct hfi1_devdata *dd) |
| 1144 | { |
| 1145 | if (dd->flags & HFI1_INITTED) { |
| 1146 | u32 pidx; |
| 1147 | |
| 1148 | dd->flags &= ~HFI1_INITTED; |
| 1149 | if (dd->pport) |
| 1150 | for (pidx = 0; pidx < dd->num_pports; ++pidx) { |
| 1151 | struct hfi1_pportdata *ppd; |
| 1152 | |
| 1153 | ppd = dd->pport + pidx; |
| 1154 | if (dd->flags & HFI1_PRESENT) |
| 1155 | set_link_state(ppd, HLS_DN_DISABLE); |
| 1156 | |
| 1157 | if (ppd->statusp) |
| 1158 | *ppd->statusp &= ~HFI1_STATUS_IB_READY; |
| 1159 | } |
| 1160 | } |
| 1161 | |
| 1162 | /* |
| 1163 | * Mark as having had an error for driver, and also |
| 1164 | * for /sys and status word mapped to user programs. |
| 1165 | * This marks unit as not usable, until reset. |
| 1166 | */ |
| 1167 | if (dd->status) |
| 1168 | dd->status->dev |= HFI1_STATUS_HWERROR; |
| 1169 | } |
| 1170 | |
| 1171 | static void remove_one(struct pci_dev *); |
| 1172 | static int init_one(struct pci_dev *, const struct pci_device_id *); |
| 1173 | |
| 1174 | #define DRIVER_LOAD_MSG "Intel " DRIVER_NAME " loaded: " |
| 1175 | #define PFX DRIVER_NAME ": " |
| 1176 | |
Sebastian Sanchez | d637301 | 2016-07-25 07:54:48 -0700 | [diff] [blame] | 1177 | const struct pci_device_id hfi1_pci_tbl[] = { |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1178 | { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL0) }, |
| 1179 | { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL1) }, |
| 1180 | { 0, } |
| 1181 | }; |
| 1182 | |
| 1183 | MODULE_DEVICE_TABLE(pci, hfi1_pci_tbl); |
| 1184 | |
| 1185 | static struct pci_driver hfi1_pci_driver = { |
| 1186 | .name = DRIVER_NAME, |
| 1187 | .probe = init_one, |
| 1188 | .remove = remove_one, |
| 1189 | .id_table = hfi1_pci_tbl, |
| 1190 | .err_handler = &hfi1_pci_err_handler, |
| 1191 | }; |
| 1192 | |
| 1193 | static void __init compute_krcvqs(void) |
| 1194 | { |
| 1195 | int i; |
| 1196 | |
| 1197 | for (i = 0; i < krcvqsset; i++) |
| 1198 | n_krcvqs += krcvqs[i]; |
| 1199 | } |
| 1200 | |
| 1201 | /* |
| 1202 | * Do all the generic driver unit- and chip-independent memory |
| 1203 | * allocation and initialization. |
| 1204 | */ |
| 1205 | static int __init hfi1_mod_init(void) |
| 1206 | { |
| 1207 | int ret; |
| 1208 | |
| 1209 | ret = dev_init(); |
| 1210 | if (ret) |
| 1211 | goto bail; |
| 1212 | |
Sebastian Sanchez | d637301 | 2016-07-25 07:54:48 -0700 | [diff] [blame] | 1213 | ret = node_affinity_init(); |
| 1214 | if (ret) |
| 1215 | goto bail; |
Dennis Dalessandro | 4197344 | 2016-07-25 07:52:36 -0700 | [diff] [blame] | 1216 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1217 | /* validate max MTU before any devices start */ |
| 1218 | if (!valid_opa_max_mtu(hfi1_max_mtu)) { |
| 1219 | pr_err("Invalid max_mtu 0x%x, using 0x%x instead\n", |
| 1220 | hfi1_max_mtu, HFI1_DEFAULT_MAX_MTU); |
| 1221 | hfi1_max_mtu = HFI1_DEFAULT_MAX_MTU; |
| 1222 | } |
| 1223 | /* valid CUs run from 1-128 in powers of 2 */ |
| 1224 | if (hfi1_cu > 128 || !is_power_of_2(hfi1_cu)) |
| 1225 | hfi1_cu = 1; |
| 1226 | /* valid credit return threshold is 0-100, variable is unsigned */ |
| 1227 | if (user_credit_return_threshold > 100) |
| 1228 | user_credit_return_threshold = 100; |
| 1229 | |
| 1230 | compute_krcvqs(); |
Jubin John | 4d114fd | 2016-02-14 20:21:43 -0800 | [diff] [blame] | 1231 | /* |
| 1232 | * sanitize receive interrupt count, time must wait until after |
| 1233 | * the hardware type is known |
| 1234 | */ |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1235 | if (rcv_intr_count > RCV_HDR_HEAD_COUNTER_MASK) |
| 1236 | rcv_intr_count = RCV_HDR_HEAD_COUNTER_MASK; |
| 1237 | /* reject invalid combinations */ |
| 1238 | if (rcv_intr_count == 0 && rcv_intr_timeout == 0) { |
| 1239 | pr_err("Invalid mode: both receive interrupt count and available timeout are zero - setting interrupt count to 1\n"); |
| 1240 | rcv_intr_count = 1; |
| 1241 | } |
| 1242 | if (rcv_intr_count > 1 && rcv_intr_timeout == 0) { |
| 1243 | /* |
| 1244 | * Avoid indefinite packet delivery by requiring a timeout |
| 1245 | * if count is > 1. |
| 1246 | */ |
| 1247 | pr_err("Invalid mode: receive interrupt count greater than 1 and available timeout is zero - setting available timeout to 1\n"); |
| 1248 | rcv_intr_timeout = 1; |
| 1249 | } |
| 1250 | if (rcv_intr_dynamic && !(rcv_intr_count > 1 && rcv_intr_timeout > 0)) { |
| 1251 | /* |
| 1252 | * The dynamic algorithm expects a non-zero timeout |
| 1253 | * and a count > 1. |
| 1254 | */ |
| 1255 | pr_err("Invalid mode: dynamic receive interrupt mitigation with invalid count and timeout - turning dynamic off\n"); |
| 1256 | rcv_intr_dynamic = 0; |
| 1257 | } |
| 1258 | |
| 1259 | /* sanitize link CRC options */ |
| 1260 | link_crc_mask &= SUPPORTED_CRCS; |
| 1261 | |
| 1262 | /* |
| 1263 | * These must be called before the driver is registered with |
| 1264 | * the PCI subsystem. |
| 1265 | */ |
| 1266 | idr_init(&hfi1_unit_table); |
| 1267 | |
| 1268 | hfi1_dbg_init(); |
Dean Luick | 528ee9f | 2016-03-05 08:50:43 -0800 | [diff] [blame] | 1269 | ret = hfi1_wss_init(); |
| 1270 | if (ret < 0) |
| 1271 | goto bail_wss; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1272 | ret = pci_register_driver(&hfi1_pci_driver); |
| 1273 | if (ret < 0) { |
| 1274 | pr_err("Unable to register driver: error %d\n", -ret); |
| 1275 | goto bail_dev; |
| 1276 | } |
| 1277 | goto bail; /* all OK */ |
| 1278 | |
| 1279 | bail_dev: |
Dean Luick | 528ee9f | 2016-03-05 08:50:43 -0800 | [diff] [blame] | 1280 | hfi1_wss_exit(); |
| 1281 | bail_wss: |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1282 | hfi1_dbg_exit(); |
| 1283 | idr_destroy(&hfi1_unit_table); |
| 1284 | dev_cleanup(); |
| 1285 | bail: |
| 1286 | return ret; |
| 1287 | } |
| 1288 | |
| 1289 | module_init(hfi1_mod_init); |
| 1290 | |
| 1291 | /* |
| 1292 | * Do the non-unit driver cleanup, memory free, etc. at unload. |
| 1293 | */ |
| 1294 | static void __exit hfi1_mod_cleanup(void) |
| 1295 | { |
| 1296 | pci_unregister_driver(&hfi1_pci_driver); |
Dennis Dalessandro | 4197344 | 2016-07-25 07:52:36 -0700 | [diff] [blame] | 1297 | node_affinity_destroy(); |
Dean Luick | 528ee9f | 2016-03-05 08:50:43 -0800 | [diff] [blame] | 1298 | hfi1_wss_exit(); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1299 | hfi1_dbg_exit(); |
| 1300 | hfi1_cpulist_count = 0; |
| 1301 | kfree(hfi1_cpulist); |
| 1302 | |
| 1303 | idr_destroy(&hfi1_unit_table); |
| 1304 | dispose_firmware(); /* asymmetric with obtain_firmware() */ |
| 1305 | dev_cleanup(); |
| 1306 | } |
| 1307 | |
| 1308 | module_exit(hfi1_mod_cleanup); |
| 1309 | |
| 1310 | /* this can only be called after a successful initialization */ |
| 1311 | static void cleanup_device_data(struct hfi1_devdata *dd) |
| 1312 | { |
| 1313 | int ctxt; |
| 1314 | int pidx; |
| 1315 | struct hfi1_ctxtdata **tmp; |
| 1316 | unsigned long flags; |
| 1317 | |
| 1318 | /* users can't do anything more with chip */ |
| 1319 | for (pidx = 0; pidx < dd->num_pports; ++pidx) { |
| 1320 | struct hfi1_pportdata *ppd = &dd->pport[pidx]; |
| 1321 | struct cc_state *cc_state; |
| 1322 | int i; |
| 1323 | |
| 1324 | if (ppd->statusp) |
| 1325 | *ppd->statusp &= ~HFI1_STATUS_CHIP_PRESENT; |
| 1326 | |
| 1327 | for (i = 0; i < OPA_MAX_SLS; i++) |
| 1328 | hrtimer_cancel(&ppd->cca_timer[i].hrtimer); |
| 1329 | |
| 1330 | spin_lock(&ppd->cc_state_lock); |
Jianxin Xiong | 8adf71f | 2016-07-25 13:39:14 -0700 | [diff] [blame] | 1331 | cc_state = get_cc_state_protected(ppd); |
Muhammad Falak R Wani | eea5707 | 2016-05-01 18:05:31 +0530 | [diff] [blame] | 1332 | RCU_INIT_POINTER(ppd->cc_state, NULL); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1333 | spin_unlock(&ppd->cc_state_lock); |
| 1334 | |
| 1335 | if (cc_state) |
Wei Yongjun | 476d95b | 2016-08-10 03:14:04 +0000 | [diff] [blame] | 1336 | kfree_rcu(cc_state, rcu); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1337 | } |
| 1338 | |
| 1339 | free_credit_return(dd); |
| 1340 | |
| 1341 | /* |
| 1342 | * Free any resources still in use (usually just kernel contexts) |
| 1343 | * at unload; we do for ctxtcnt, because that's what we allocate. |
| 1344 | * We acquire lock to be really paranoid that rcd isn't being |
| 1345 | * accessed from some interrupt-related code (that should not happen, |
| 1346 | * but best to be sure). |
| 1347 | */ |
| 1348 | spin_lock_irqsave(&dd->uctxt_lock, flags); |
| 1349 | tmp = dd->rcd; |
| 1350 | dd->rcd = NULL; |
| 1351 | spin_unlock_irqrestore(&dd->uctxt_lock, flags); |
Mark F. Brown | 46b010d | 2015-11-09 19:18:20 -0500 | [diff] [blame] | 1352 | |
| 1353 | if (dd->rcvhdrtail_dummy_kvaddr) { |
| 1354 | dma_free_coherent(&dd->pcidev->dev, sizeof(u64), |
| 1355 | (void *)dd->rcvhdrtail_dummy_kvaddr, |
Tymoteusz Kielan | 6036818 | 2016-09-06 04:35:54 -0700 | [diff] [blame] | 1356 | dd->rcvhdrtail_dummy_dma); |
Dan Carpenter | a8b7da5 | 2016-05-28 08:01:20 +0300 | [diff] [blame] | 1357 | dd->rcvhdrtail_dummy_kvaddr = NULL; |
Mark F. Brown | 46b010d | 2015-11-09 19:18:20 -0500 | [diff] [blame] | 1358 | } |
| 1359 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1360 | for (ctxt = 0; tmp && ctxt < dd->num_rcv_contexts; ctxt++) { |
| 1361 | struct hfi1_ctxtdata *rcd = tmp[ctxt]; |
| 1362 | |
| 1363 | tmp[ctxt] = NULL; /* debugging paranoia */ |
| 1364 | if (rcd) { |
| 1365 | hfi1_clear_tids(rcd); |
| 1366 | hfi1_free_ctxtdata(dd, rcd); |
| 1367 | } |
| 1368 | } |
| 1369 | kfree(tmp); |
Jubin John | 35f6bef | 2016-02-14 12:46:10 -0800 | [diff] [blame] | 1370 | free_pio_map(dd); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1371 | /* must follow rcv context free - need to remove rcv's hooks */ |
| 1372 | for (ctxt = 0; ctxt < dd->num_send_contexts; ctxt++) |
| 1373 | sc_free(dd->send_contexts[ctxt].sc); |
| 1374 | dd->num_send_contexts = 0; |
| 1375 | kfree(dd->send_contexts); |
| 1376 | dd->send_contexts = NULL; |
Jubin John | 79d0c08 | 2016-02-26 13:33:33 -0800 | [diff] [blame] | 1377 | kfree(dd->hw_to_sw); |
| 1378 | dd->hw_to_sw = NULL; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1379 | kfree(dd->boardname); |
| 1380 | vfree(dd->events); |
| 1381 | vfree(dd->status); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1382 | } |
| 1383 | |
| 1384 | /* |
| 1385 | * Clean up on unit shutdown, or error during unit load after |
| 1386 | * successful initialization. |
| 1387 | */ |
| 1388 | static void postinit_cleanup(struct hfi1_devdata *dd) |
| 1389 | { |
| 1390 | hfi1_start_cleanup(dd); |
| 1391 | |
| 1392 | hfi1_pcie_ddcleanup(dd); |
| 1393 | hfi1_pcie_cleanup(dd->pcidev); |
| 1394 | |
| 1395 | cleanup_device_data(dd); |
| 1396 | |
| 1397 | hfi1_free_devdata(dd); |
| 1398 | } |
| 1399 | |
Krzysztof Blaszkowski | 11501ab | 2016-10-25 13:12:11 -0700 | [diff] [blame] | 1400 | static int init_validate_rcvhdrcnt(struct device *dev, uint thecnt) |
| 1401 | { |
| 1402 | if (thecnt <= HFI1_MIN_HDRQ_EGRBUF_CNT) { |
| 1403 | hfi1_early_err(dev, "Receive header queue count too small\n"); |
| 1404 | return -EINVAL; |
| 1405 | } |
| 1406 | |
| 1407 | if (thecnt > HFI1_MAX_HDRQ_EGRBUF_CNT) { |
| 1408 | hfi1_early_err(dev, |
| 1409 | "Receive header queue count cannot be greater than %u\n", |
| 1410 | HFI1_MAX_HDRQ_EGRBUF_CNT); |
| 1411 | return -EINVAL; |
| 1412 | } |
| 1413 | |
| 1414 | if (thecnt % HDRQ_INCREMENT) { |
| 1415 | hfi1_early_err(dev, "Receive header queue count %d must be divisible by %lu\n", |
| 1416 | thecnt, HDRQ_INCREMENT); |
| 1417 | return -EINVAL; |
| 1418 | } |
| 1419 | |
| 1420 | return 0; |
| 1421 | } |
| 1422 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1423 | static int init_one(struct pci_dev *pdev, const struct pci_device_id *ent) |
| 1424 | { |
| 1425 | int ret = 0, j, pidx, initfail; |
Krzysztof Blaszkowski | 83fb4af | 2016-10-17 04:19:24 -0700 | [diff] [blame] | 1426 | struct hfi1_devdata *dd; |
Harish Chegondi | e8597eb | 2015-12-01 15:38:20 -0500 | [diff] [blame] | 1427 | struct hfi1_pportdata *ppd; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1428 | |
| 1429 | /* First, lock the non-writable module parameters */ |
| 1430 | HFI1_CAP_LOCK(); |
| 1431 | |
Tadeusz Struk | 5d6f08a | 2017-03-20 17:25:29 -0700 | [diff] [blame] | 1432 | /* Validate dev ids */ |
| 1433 | if (!(ent->device == PCI_DEVICE_ID_INTEL0 || |
| 1434 | ent->device == PCI_DEVICE_ID_INTEL1)) { |
| 1435 | hfi1_early_err(&pdev->dev, |
| 1436 | "Failing on unknown Intel deviceid 0x%x\n", |
| 1437 | ent->device); |
| 1438 | ret = -ENODEV; |
| 1439 | goto bail; |
| 1440 | } |
| 1441 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1442 | /* Validate some global module parameters */ |
Krzysztof Blaszkowski | 11501ab | 2016-10-25 13:12:11 -0700 | [diff] [blame] | 1443 | ret = init_validate_rcvhdrcnt(&pdev->dev, rcvhdrcnt); |
| 1444 | if (ret) |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1445 | goto bail; |
Krzysztof Blaszkowski | 11501ab | 2016-10-25 13:12:11 -0700 | [diff] [blame] | 1446 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1447 | /* use the encoding function as a sanitization check */ |
| 1448 | if (!encode_rcv_header_entry_size(hfi1_hdrq_entsize)) { |
| 1449 | hfi1_early_err(&pdev->dev, "Invalid HdrQ Entry size %u\n", |
| 1450 | hfi1_hdrq_entsize); |
Sebastian Sanchez | 07859de | 2015-12-10 16:02:49 -0500 | [diff] [blame] | 1451 | ret = -EINVAL; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1452 | goto bail; |
| 1453 | } |
| 1454 | |
| 1455 | /* The receive eager buffer size must be set before the receive |
| 1456 | * contexts are created. |
| 1457 | * |
| 1458 | * Set the eager buffer size. Validate that it falls in a range |
| 1459 | * allowed by the hardware - all powers of 2 between the min and |
| 1460 | * max. The maximum valid MTU is within the eager buffer range |
| 1461 | * so we do not need to cap the max_mtu by an eager buffer size |
| 1462 | * setting. |
| 1463 | */ |
| 1464 | if (eager_buffer_size) { |
| 1465 | if (!is_power_of_2(eager_buffer_size)) |
| 1466 | eager_buffer_size = |
| 1467 | roundup_pow_of_two(eager_buffer_size); |
| 1468 | eager_buffer_size = |
| 1469 | clamp_val(eager_buffer_size, |
| 1470 | MIN_EAGER_BUFFER * 8, |
| 1471 | MAX_EAGER_BUFFER_TOTAL); |
| 1472 | hfi1_early_info(&pdev->dev, "Eager buffer size %u\n", |
| 1473 | eager_buffer_size); |
| 1474 | } else { |
| 1475 | hfi1_early_err(&pdev->dev, "Invalid Eager buffer size of 0\n"); |
| 1476 | ret = -EINVAL; |
| 1477 | goto bail; |
| 1478 | } |
| 1479 | |
| 1480 | /* restrict value of hfi1_rcvarr_split */ |
| 1481 | hfi1_rcvarr_split = clamp_val(hfi1_rcvarr_split, 0, 100); |
| 1482 | |
| 1483 | ret = hfi1_pcie_init(pdev, ent); |
| 1484 | if (ret) |
| 1485 | goto bail; |
| 1486 | |
Krzysztof Blaszkowski | 83fb4af | 2016-10-17 04:19:24 -0700 | [diff] [blame] | 1487 | /* |
| 1488 | * Do device-specific initialization, function table setup, dd |
| 1489 | * allocation, etc. |
| 1490 | */ |
| 1491 | dd = hfi1_init_dd(pdev, ent); |
| 1492 | |
| 1493 | if (IS_ERR(dd)) { |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1494 | ret = PTR_ERR(dd); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1495 | goto clean_bail; /* error already printed */ |
Krzysztof Blaszkowski | 83fb4af | 2016-10-17 04:19:24 -0700 | [diff] [blame] | 1496 | } |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1497 | |
| 1498 | ret = create_workqueues(dd); |
| 1499 | if (ret) |
| 1500 | goto clean_bail; |
| 1501 | |
| 1502 | /* do the generic initialization */ |
| 1503 | initfail = hfi1_init(dd, 0); |
| 1504 | |
Vishwanathapura, Niranjana | d4829ea | 2017-04-12 20:29:28 -0700 | [diff] [blame] | 1505 | /* setup vnic */ |
| 1506 | hfi1_vnic_setup(dd); |
| 1507 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1508 | ret = hfi1_register_ib_device(dd); |
| 1509 | |
| 1510 | /* |
| 1511 | * Now ready for use. this should be cleared whenever we |
| 1512 | * detect a reset, or initiate one. If earlier failure, |
| 1513 | * we still create devices, so diags, etc. can be used |
| 1514 | * to determine cause of problem. |
| 1515 | */ |
Dean Luick | ed6f653 | 2016-02-18 11:12:25 -0800 | [diff] [blame] | 1516 | if (!initfail && !ret) { |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1517 | dd->flags |= HFI1_INITTED; |
Dean Luick | ed6f653 | 2016-02-18 11:12:25 -0800 | [diff] [blame] | 1518 | /* create debufs files after init and ib register */ |
| 1519 | hfi1_dbg_ibdev_init(&dd->verbs_dev); |
| 1520 | } |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1521 | |
| 1522 | j = hfi1_device_create(dd); |
| 1523 | if (j) |
| 1524 | dd_dev_err(dd, "Failed to create /dev devices: %d\n", -j); |
| 1525 | |
| 1526 | if (initfail || ret) { |
| 1527 | stop_timers(dd); |
| 1528 | flush_workqueue(ib_wq); |
Harish Chegondi | e8597eb | 2015-12-01 15:38:20 -0500 | [diff] [blame] | 1529 | for (pidx = 0; pidx < dd->num_pports; ++pidx) { |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1530 | hfi1_quiet_serdes(dd->pport + pidx); |
Harish Chegondi | e8597eb | 2015-12-01 15:38:20 -0500 | [diff] [blame] | 1531 | ppd = dd->pport + pidx; |
| 1532 | if (ppd->hfi1_wq) { |
| 1533 | destroy_workqueue(ppd->hfi1_wq); |
| 1534 | ppd->hfi1_wq = NULL; |
| 1535 | } |
| 1536 | } |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1537 | if (!j) |
| 1538 | hfi1_device_remove(dd); |
| 1539 | if (!ret) |
| 1540 | hfi1_unregister_ib_device(dd); |
Vishwanathapura, Niranjana | 2280740 | 2017-04-12 20:29:29 -0700 | [diff] [blame] | 1541 | hfi1_vnic_cleanup(dd); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1542 | postinit_cleanup(dd); |
| 1543 | if (initfail) |
| 1544 | ret = initfail; |
| 1545 | goto bail; /* everything already cleaned */ |
| 1546 | } |
| 1547 | |
| 1548 | sdma_start(dd); |
| 1549 | |
| 1550 | return 0; |
| 1551 | |
| 1552 | clean_bail: |
| 1553 | hfi1_pcie_cleanup(pdev); |
| 1554 | bail: |
| 1555 | return ret; |
| 1556 | } |
| 1557 | |
Tadeusz Struk | acd7c8f | 2016-10-25 08:57:55 -0700 | [diff] [blame] | 1558 | static void wait_for_clients(struct hfi1_devdata *dd) |
| 1559 | { |
| 1560 | /* |
| 1561 | * Remove the device init value and complete the device if there is |
| 1562 | * no clients or wait for active clients to finish. |
| 1563 | */ |
| 1564 | if (atomic_dec_and_test(&dd->user_refcount)) |
| 1565 | complete(&dd->user_comp); |
| 1566 | |
| 1567 | wait_for_completion(&dd->user_comp); |
| 1568 | } |
| 1569 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1570 | static void remove_one(struct pci_dev *pdev) |
| 1571 | { |
| 1572 | struct hfi1_devdata *dd = pci_get_drvdata(pdev); |
| 1573 | |
Dean Luick | ed6f653 | 2016-02-18 11:12:25 -0800 | [diff] [blame] | 1574 | /* close debugfs files before ib unregister */ |
| 1575 | hfi1_dbg_ibdev_exit(&dd->verbs_dev); |
Tadeusz Struk | acd7c8f | 2016-10-25 08:57:55 -0700 | [diff] [blame] | 1576 | |
| 1577 | /* remove the /dev hfi1 interface */ |
| 1578 | hfi1_device_remove(dd); |
| 1579 | |
| 1580 | /* wait for existing user space clients to finish */ |
| 1581 | wait_for_clients(dd); |
| 1582 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1583 | /* unregister from IB core */ |
| 1584 | hfi1_unregister_ib_device(dd); |
| 1585 | |
Vishwanathapura, Niranjana | d4829ea | 2017-04-12 20:29:28 -0700 | [diff] [blame] | 1586 | /* cleanup vnic */ |
| 1587 | hfi1_vnic_cleanup(dd); |
| 1588 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1589 | /* |
| 1590 | * Disable the IB link, disable interrupts on the device, |
| 1591 | * clear dma engines, etc. |
| 1592 | */ |
| 1593 | shutdown_device(dd); |
| 1594 | |
| 1595 | stop_timers(dd); |
| 1596 | |
| 1597 | /* wait until all of our (qsfp) queue_work() calls complete */ |
| 1598 | flush_workqueue(ib_wq); |
| 1599 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1600 | postinit_cleanup(dd); |
| 1601 | } |
| 1602 | |
| 1603 | /** |
| 1604 | * hfi1_create_rcvhdrq - create a receive header queue |
| 1605 | * @dd: the hfi1_ib device |
| 1606 | * @rcd: the context data |
| 1607 | * |
| 1608 | * This must be contiguous memory (from an i/o perspective), and must be |
| 1609 | * DMA'able (which means for some systems, it will go through an IOMMU, |
| 1610 | * or be forced into a low address range). |
| 1611 | */ |
| 1612 | int hfi1_create_rcvhdrq(struct hfi1_devdata *dd, struct hfi1_ctxtdata *rcd) |
| 1613 | { |
| 1614 | unsigned amt; |
| 1615 | u64 reg; |
| 1616 | |
| 1617 | if (!rcd->rcvhdrq) { |
Tymoteusz Kielan | 6036818 | 2016-09-06 04:35:54 -0700 | [diff] [blame] | 1618 | dma_addr_t dma_hdrqtail; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1619 | gfp_t gfp_flags; |
| 1620 | |
| 1621 | /* |
| 1622 | * rcvhdrqentsize is in DWs, so we have to convert to bytes |
| 1623 | * (* sizeof(u32)). |
| 1624 | */ |
Amitoj Kaur Chawla | 8444991 | 2016-03-04 22:30:43 +0530 | [diff] [blame] | 1625 | amt = PAGE_ALIGN(rcd->rcvhdrq_cnt * rcd->rcvhdrqentsize * |
| 1626 | sizeof(u32)); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1627 | |
Vishwanathapura, Niranjana | 2280740 | 2017-04-12 20:29:29 -0700 | [diff] [blame] | 1628 | if ((rcd->ctxt < dd->first_dyn_alloc_ctxt) || |
| 1629 | (rcd->sc && (rcd->sc->type == SC_KERNEL))) |
| 1630 | gfp_flags = GFP_KERNEL; |
| 1631 | else |
| 1632 | gfp_flags = GFP_USER; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1633 | rcd->rcvhdrq = dma_zalloc_coherent( |
Tymoteusz Kielan | 6036818 | 2016-09-06 04:35:54 -0700 | [diff] [blame] | 1634 | &dd->pcidev->dev, amt, &rcd->rcvhdrq_dma, |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1635 | gfp_flags | __GFP_COMP); |
| 1636 | |
| 1637 | if (!rcd->rcvhdrq) { |
| 1638 | dd_dev_err(dd, |
Jubin John | 17fb4f2 | 2016-02-14 20:21:52 -0800 | [diff] [blame] | 1639 | "attempt to allocate %d bytes for ctxt %u rcvhdrq failed\n", |
| 1640 | amt, rcd->ctxt); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1641 | goto bail; |
| 1642 | } |
| 1643 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1644 | if (HFI1_CAP_KGET_MASK(rcd->flags, DMA_RTAIL)) { |
| 1645 | rcd->rcvhdrtail_kvaddr = dma_zalloc_coherent( |
Tymoteusz Kielan | 6036818 | 2016-09-06 04:35:54 -0700 | [diff] [blame] | 1646 | &dd->pcidev->dev, PAGE_SIZE, &dma_hdrqtail, |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1647 | gfp_flags); |
| 1648 | if (!rcd->rcvhdrtail_kvaddr) |
| 1649 | goto bail_free; |
Tymoteusz Kielan | 6036818 | 2016-09-06 04:35:54 -0700 | [diff] [blame] | 1650 | rcd->rcvhdrqtailaddr_dma = dma_hdrqtail; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1651 | } |
| 1652 | |
| 1653 | rcd->rcvhdrq_size = amt; |
| 1654 | } |
| 1655 | /* |
| 1656 | * These values are per-context: |
| 1657 | * RcvHdrCnt |
| 1658 | * RcvHdrEntSize |
| 1659 | * RcvHdrSize |
| 1660 | */ |
| 1661 | reg = ((u64)(rcd->rcvhdrq_cnt >> HDRQ_SIZE_SHIFT) |
| 1662 | & RCV_HDR_CNT_CNT_MASK) |
| 1663 | << RCV_HDR_CNT_CNT_SHIFT; |
| 1664 | write_kctxt_csr(dd, rcd->ctxt, RCV_HDR_CNT, reg); |
| 1665 | reg = (encode_rcv_header_entry_size(rcd->rcvhdrqentsize) |
| 1666 | & RCV_HDR_ENT_SIZE_ENT_SIZE_MASK) |
| 1667 | << RCV_HDR_ENT_SIZE_ENT_SIZE_SHIFT; |
| 1668 | write_kctxt_csr(dd, rcd->ctxt, RCV_HDR_ENT_SIZE, reg); |
| 1669 | reg = (dd->rcvhdrsize & RCV_HDR_SIZE_HDR_SIZE_MASK) |
| 1670 | << RCV_HDR_SIZE_HDR_SIZE_SHIFT; |
| 1671 | write_kctxt_csr(dd, rcd->ctxt, RCV_HDR_SIZE, reg); |
Mark F. Brown | 46b010d | 2015-11-09 19:18:20 -0500 | [diff] [blame] | 1672 | |
| 1673 | /* |
| 1674 | * Program dummy tail address for every receive context |
| 1675 | * before enabling any receive context |
| 1676 | */ |
| 1677 | write_kctxt_csr(dd, rcd->ctxt, RCV_HDR_TAIL_ADDR, |
Tymoteusz Kielan | 6036818 | 2016-09-06 04:35:54 -0700 | [diff] [blame] | 1678 | dd->rcvhdrtail_dummy_dma); |
Mark F. Brown | 46b010d | 2015-11-09 19:18:20 -0500 | [diff] [blame] | 1679 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1680 | return 0; |
| 1681 | |
| 1682 | bail_free: |
| 1683 | dd_dev_err(dd, |
Jubin John | 17fb4f2 | 2016-02-14 20:21:52 -0800 | [diff] [blame] | 1684 | "attempt to allocate 1 page for ctxt %u rcvhdrqtailaddr failed\n", |
| 1685 | rcd->ctxt); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1686 | vfree(rcd->user_event_mask); |
| 1687 | rcd->user_event_mask = NULL; |
| 1688 | dma_free_coherent(&dd->pcidev->dev, amt, rcd->rcvhdrq, |
Tymoteusz Kielan | 6036818 | 2016-09-06 04:35:54 -0700 | [diff] [blame] | 1689 | rcd->rcvhdrq_dma); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1690 | rcd->rcvhdrq = NULL; |
| 1691 | bail: |
| 1692 | return -ENOMEM; |
| 1693 | } |
| 1694 | |
| 1695 | /** |
| 1696 | * allocate eager buffers, both kernel and user contexts. |
| 1697 | * @rcd: the context we are setting up. |
| 1698 | * |
| 1699 | * Allocate the eager TID buffers and program them into hip. |
| 1700 | * They are no longer completely contiguous, we do multiple allocation |
| 1701 | * calls. Otherwise we get the OOM code involved, by asking for too |
| 1702 | * much per call, with disastrous results on some kernels. |
| 1703 | */ |
| 1704 | int hfi1_setup_eagerbufs(struct hfi1_ctxtdata *rcd) |
| 1705 | { |
| 1706 | struct hfi1_devdata *dd = rcd->dd; |
| 1707 | u32 max_entries, egrtop, alloced_bytes = 0, idx = 0; |
| 1708 | gfp_t gfp_flags; |
| 1709 | u16 order; |
| 1710 | int ret = 0; |
| 1711 | u16 round_mtu = roundup_pow_of_two(hfi1_max_mtu); |
| 1712 | |
| 1713 | /* |
| 1714 | * GFP_USER, but without GFP_FS, so buffer cache can be |
| 1715 | * coalesced (we hope); otherwise, even at order 4, |
| 1716 | * heavy filesystem activity makes these fail, and we can |
| 1717 | * use compound pages. |
| 1718 | */ |
Mel Gorman | 71baba4 | 2015-11-06 16:28:28 -0800 | [diff] [blame] | 1719 | gfp_flags = __GFP_RECLAIM | __GFP_IO | __GFP_COMP; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1720 | |
| 1721 | /* |
| 1722 | * The minimum size of the eager buffers is a groups of MTU-sized |
| 1723 | * buffers. |
| 1724 | * The global eager_buffer_size parameter is checked against the |
| 1725 | * theoretical lower limit of the value. Here, we check against the |
| 1726 | * MTU. |
| 1727 | */ |
| 1728 | if (rcd->egrbufs.size < (round_mtu * dd->rcv_entries.group_size)) |
| 1729 | rcd->egrbufs.size = round_mtu * dd->rcv_entries.group_size; |
| 1730 | /* |
| 1731 | * If using one-pkt-per-egr-buffer, lower the eager buffer |
| 1732 | * size to the max MTU (page-aligned). |
| 1733 | */ |
| 1734 | if (!HFI1_CAP_KGET_MASK(rcd->flags, MULTI_PKT_EGR)) |
| 1735 | rcd->egrbufs.rcvtid_size = round_mtu; |
| 1736 | |
| 1737 | /* |
| 1738 | * Eager buffers sizes of 1MB or less require smaller TID sizes |
| 1739 | * to satisfy the "multiple of 8 RcvArray entries" requirement. |
| 1740 | */ |
| 1741 | if (rcd->egrbufs.size <= (1 << 20)) |
| 1742 | rcd->egrbufs.rcvtid_size = max((unsigned long)round_mtu, |
| 1743 | rounddown_pow_of_two(rcd->egrbufs.size / 8)); |
| 1744 | |
| 1745 | while (alloced_bytes < rcd->egrbufs.size && |
| 1746 | rcd->egrbufs.alloced < rcd->egrbufs.count) { |
| 1747 | rcd->egrbufs.buffers[idx].addr = |
| 1748 | dma_zalloc_coherent(&dd->pcidev->dev, |
| 1749 | rcd->egrbufs.rcvtid_size, |
Tymoteusz Kielan | 6036818 | 2016-09-06 04:35:54 -0700 | [diff] [blame] | 1750 | &rcd->egrbufs.buffers[idx].dma, |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1751 | gfp_flags); |
| 1752 | if (rcd->egrbufs.buffers[idx].addr) { |
| 1753 | rcd->egrbufs.buffers[idx].len = |
| 1754 | rcd->egrbufs.rcvtid_size; |
| 1755 | rcd->egrbufs.rcvtids[rcd->egrbufs.alloced].addr = |
| 1756 | rcd->egrbufs.buffers[idx].addr; |
Tymoteusz Kielan | 6036818 | 2016-09-06 04:35:54 -0700 | [diff] [blame] | 1757 | rcd->egrbufs.rcvtids[rcd->egrbufs.alloced].dma = |
| 1758 | rcd->egrbufs.buffers[idx].dma; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1759 | rcd->egrbufs.alloced++; |
| 1760 | alloced_bytes += rcd->egrbufs.rcvtid_size; |
| 1761 | idx++; |
| 1762 | } else { |
| 1763 | u32 new_size, i, j; |
| 1764 | u64 offset = 0; |
| 1765 | |
| 1766 | /* |
| 1767 | * Fail the eager buffer allocation if: |
| 1768 | * - we are already using the lowest acceptable size |
| 1769 | * - we are using one-pkt-per-egr-buffer (this implies |
| 1770 | * that we are accepting only one size) |
| 1771 | */ |
| 1772 | if (rcd->egrbufs.rcvtid_size == round_mtu || |
| 1773 | !HFI1_CAP_KGET_MASK(rcd->flags, MULTI_PKT_EGR)) { |
| 1774 | dd_dev_err(dd, "ctxt%u: Failed to allocate eager buffers\n", |
Jubin John | 17fb4f2 | 2016-02-14 20:21:52 -0800 | [diff] [blame] | 1775 | rcd->ctxt); |
Michael J. Ruhl | 9467906 | 2017-05-04 05:14:28 -0700 | [diff] [blame] | 1776 | ret = -ENOMEM; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1777 | goto bail_rcvegrbuf_phys; |
| 1778 | } |
| 1779 | |
| 1780 | new_size = rcd->egrbufs.rcvtid_size / 2; |
| 1781 | |
| 1782 | /* |
| 1783 | * If the first attempt to allocate memory failed, don't |
| 1784 | * fail everything but continue with the next lower |
| 1785 | * size. |
| 1786 | */ |
| 1787 | if (idx == 0) { |
| 1788 | rcd->egrbufs.rcvtid_size = new_size; |
| 1789 | continue; |
| 1790 | } |
| 1791 | |
| 1792 | /* |
| 1793 | * Re-partition already allocated buffers to a smaller |
| 1794 | * size. |
| 1795 | */ |
| 1796 | rcd->egrbufs.alloced = 0; |
| 1797 | for (i = 0, j = 0, offset = 0; j < idx; i++) { |
| 1798 | if (i >= rcd->egrbufs.count) |
| 1799 | break; |
Tymoteusz Kielan | 6036818 | 2016-09-06 04:35:54 -0700 | [diff] [blame] | 1800 | rcd->egrbufs.rcvtids[i].dma = |
| 1801 | rcd->egrbufs.buffers[j].dma + offset; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1802 | rcd->egrbufs.rcvtids[i].addr = |
| 1803 | rcd->egrbufs.buffers[j].addr + offset; |
| 1804 | rcd->egrbufs.alloced++; |
Tymoteusz Kielan | 6036818 | 2016-09-06 04:35:54 -0700 | [diff] [blame] | 1805 | if ((rcd->egrbufs.buffers[j].dma + offset + |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1806 | new_size) == |
Tymoteusz Kielan | 6036818 | 2016-09-06 04:35:54 -0700 | [diff] [blame] | 1807 | (rcd->egrbufs.buffers[j].dma + |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1808 | rcd->egrbufs.buffers[j].len)) { |
| 1809 | j++; |
| 1810 | offset = 0; |
Jubin John | e490974 | 2016-02-14 20:22:00 -0800 | [diff] [blame] | 1811 | } else { |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1812 | offset += new_size; |
Jubin John | e490974 | 2016-02-14 20:22:00 -0800 | [diff] [blame] | 1813 | } |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1814 | } |
| 1815 | rcd->egrbufs.rcvtid_size = new_size; |
| 1816 | } |
| 1817 | } |
| 1818 | rcd->egrbufs.numbufs = idx; |
| 1819 | rcd->egrbufs.size = alloced_bytes; |
| 1820 | |
Sebastian Sanchez | 6c63e42 | 2015-11-06 20:06:56 -0500 | [diff] [blame] | 1821 | hfi1_cdbg(PROC, |
| 1822 | "ctxt%u: Alloced %u rcv tid entries @ %uKB, total %zuKB\n", |
Grzegorz Heldt | 23002d5 | 2016-07-25 13:39:33 -0700 | [diff] [blame] | 1823 | rcd->ctxt, rcd->egrbufs.alloced, |
| 1824 | rcd->egrbufs.rcvtid_size / 1024, rcd->egrbufs.size / 1024); |
Sebastian Sanchez | 6c63e42 | 2015-11-06 20:06:56 -0500 | [diff] [blame] | 1825 | |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1826 | /* |
| 1827 | * Set the contexts rcv array head update threshold to the closest |
| 1828 | * power of 2 (so we can use a mask instead of modulo) below half |
| 1829 | * the allocated entries. |
| 1830 | */ |
| 1831 | rcd->egrbufs.threshold = |
| 1832 | rounddown_pow_of_two(rcd->egrbufs.alloced / 2); |
| 1833 | /* |
| 1834 | * Compute the expected RcvArray entry base. This is done after |
| 1835 | * allocating the eager buffers in order to maximize the |
| 1836 | * expected RcvArray entries for the context. |
| 1837 | */ |
| 1838 | max_entries = rcd->rcv_array_groups * dd->rcv_entries.group_size; |
| 1839 | egrtop = roundup(rcd->egrbufs.alloced, dd->rcv_entries.group_size); |
| 1840 | rcd->expected_count = max_entries - egrtop; |
| 1841 | if (rcd->expected_count > MAX_TID_PAIR_ENTRIES * 2) |
| 1842 | rcd->expected_count = MAX_TID_PAIR_ENTRIES * 2; |
| 1843 | |
| 1844 | rcd->expected_base = rcd->eager_base + egrtop; |
Sebastian Sanchez | 6c63e42 | 2015-11-06 20:06:56 -0500 | [diff] [blame] | 1845 | hfi1_cdbg(PROC, "ctxt%u: eager:%u, exp:%u, egrbase:%u, expbase:%u\n", |
| 1846 | rcd->ctxt, rcd->egrbufs.alloced, rcd->expected_count, |
| 1847 | rcd->eager_base, rcd->expected_base); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1848 | |
| 1849 | if (!hfi1_rcvbuf_validate(rcd->egrbufs.rcvtid_size, PT_EAGER, &order)) { |
Sebastian Sanchez | 6c63e42 | 2015-11-06 20:06:56 -0500 | [diff] [blame] | 1850 | hfi1_cdbg(PROC, |
| 1851 | "ctxt%u: current Eager buffer size is invalid %u\n", |
| 1852 | rcd->ctxt, rcd->egrbufs.rcvtid_size); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1853 | ret = -EINVAL; |
| 1854 | goto bail; |
| 1855 | } |
| 1856 | |
| 1857 | for (idx = 0; idx < rcd->egrbufs.alloced; idx++) { |
| 1858 | hfi1_put_tid(dd, rcd->eager_base + idx, PT_EAGER, |
Tymoteusz Kielan | 6036818 | 2016-09-06 04:35:54 -0700 | [diff] [blame] | 1859 | rcd->egrbufs.rcvtids[idx].dma, order); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1860 | cond_resched(); |
| 1861 | } |
| 1862 | goto bail; |
| 1863 | |
| 1864 | bail_rcvegrbuf_phys: |
| 1865 | for (idx = 0; idx < rcd->egrbufs.alloced && |
Jubin John | 17fb4f2 | 2016-02-14 20:21:52 -0800 | [diff] [blame] | 1866 | rcd->egrbufs.buffers[idx].addr; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1867 | idx++) { |
| 1868 | dma_free_coherent(&dd->pcidev->dev, |
| 1869 | rcd->egrbufs.buffers[idx].len, |
| 1870 | rcd->egrbufs.buffers[idx].addr, |
Tymoteusz Kielan | 6036818 | 2016-09-06 04:35:54 -0700 | [diff] [blame] | 1871 | rcd->egrbufs.buffers[idx].dma); |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1872 | rcd->egrbufs.buffers[idx].addr = NULL; |
Tymoteusz Kielan | 6036818 | 2016-09-06 04:35:54 -0700 | [diff] [blame] | 1873 | rcd->egrbufs.buffers[idx].dma = 0; |
Mike Marciniszyn | 7724105 | 2015-07-30 15:17:43 -0400 | [diff] [blame] | 1874 | rcd->egrbufs.buffers[idx].len = 0; |
| 1875 | } |
| 1876 | bail: |
| 1877 | return ret; |
| 1878 | } |