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