Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1 | /* |
| 2 | * GPL HEADER START |
| 3 | * |
| 4 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 only, |
| 8 | * as published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * General Public License version 2 for more details (a copy is included |
| 14 | * in the LICENSE file that accompanied this code). |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * version 2 along with this program; If not, see |
Oleg Drokin | 6a5b99a | 2016-06-14 23:33:40 -0400 | [diff] [blame] | 18 | * http://www.gnu.org/licenses/gpl-2.0.html |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 19 | * |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 20 | * GPL HEADER END |
| 21 | */ |
| 22 | /* |
| 23 | * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved. |
| 24 | * Use is subject to license terms. |
| 25 | * |
Andreas Dilger | 1dc563a | 2015-11-08 18:09:37 -0500 | [diff] [blame] | 26 | * Copyright (c) 2011, 2015, Intel Corporation. |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 27 | */ |
| 28 | /* |
| 29 | * This file is part of Lustre, http://www.lustre.org/ |
| 30 | * Lustre is a trademark of Sun Microsystems, Inc. |
| 31 | * |
| 32 | * lnet/klnds/o2iblnd/o2iblnd.c |
| 33 | * |
| 34 | * Author: Eric Barton <eric@bartonsoftware.com> |
| 35 | */ |
| 36 | |
Peng Tao | 5f43264 | 2013-06-07 22:07:21 +0800 | [diff] [blame] | 37 | #include <asm/div64.h> |
John L. Hammond | d664d1f | 2015-06-11 15:18:08 -0400 | [diff] [blame] | 38 | #include <asm/page.h> |
| 39 | #include "o2iblnd.h" |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 40 | |
Frank Zago | 439b4d4 | 2016-03-02 17:02:00 -0500 | [diff] [blame] | 41 | static lnd_t the_o2iblnd; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 42 | |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 43 | struct kib_data kiblnd_data; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 44 | |
Guillaume Matheron | febe73b | 2015-04-02 19:35:45 +0200 | [diff] [blame] | 45 | static __u32 kiblnd_cksum(void *ptr, int nob) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 46 | { |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 47 | char *c = ptr; |
| 48 | __u32 sum = 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 49 | |
| 50 | while (nob-- > 0) |
| 51 | sum = ((sum << 1) | (sum >> 31)) + *c++; |
| 52 | |
| 53 | /* ensure I don't return 0 (== no checksum) */ |
James Simmons | 5fd8833 | 2016-02-12 12:06:09 -0500 | [diff] [blame] | 54 | return !sum ? 1 : sum; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 55 | } |
| 56 | |
Guillaume Matheron | febe73b | 2015-04-02 19:35:45 +0200 | [diff] [blame] | 57 | static char *kiblnd_msgtype2str(int type) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 58 | { |
| 59 | switch (type) { |
| 60 | case IBLND_MSG_CONNREQ: |
| 61 | return "CONNREQ"; |
| 62 | |
| 63 | case IBLND_MSG_CONNACK: |
| 64 | return "CONNACK"; |
| 65 | |
| 66 | case IBLND_MSG_NOOP: |
| 67 | return "NOOP"; |
| 68 | |
| 69 | case IBLND_MSG_IMMEDIATE: |
| 70 | return "IMMEDIATE"; |
| 71 | |
| 72 | case IBLND_MSG_PUT_REQ: |
| 73 | return "PUT_REQ"; |
| 74 | |
| 75 | case IBLND_MSG_PUT_NAK: |
| 76 | return "PUT_NAK"; |
| 77 | |
| 78 | case IBLND_MSG_PUT_ACK: |
| 79 | return "PUT_ACK"; |
| 80 | |
| 81 | case IBLND_MSG_PUT_DONE: |
| 82 | return "PUT_DONE"; |
| 83 | |
| 84 | case IBLND_MSG_GET_REQ: |
| 85 | return "GET_REQ"; |
| 86 | |
| 87 | case IBLND_MSG_GET_DONE: |
| 88 | return "GET_DONE"; |
| 89 | |
| 90 | default: |
| 91 | return "???"; |
| 92 | } |
| 93 | } |
| 94 | |
Guillaume Matheron | febe73b | 2015-04-02 19:35:45 +0200 | [diff] [blame] | 95 | static int kiblnd_msgtype2size(int type) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 96 | { |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 97 | const int hdr_size = offsetof(struct kib_msg, ibm_u); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 98 | |
| 99 | switch (type) { |
| 100 | case IBLND_MSG_CONNREQ: |
| 101 | case IBLND_MSG_CONNACK: |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 102 | return hdr_size + sizeof(struct kib_connparams); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 103 | |
| 104 | case IBLND_MSG_NOOP: |
| 105 | return hdr_size; |
| 106 | |
| 107 | case IBLND_MSG_IMMEDIATE: |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 108 | return offsetof(struct kib_msg, ibm_u.immediate.ibim_payload[0]); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 109 | |
| 110 | case IBLND_MSG_PUT_REQ: |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 111 | return hdr_size + sizeof(struct kib_putreq_msg); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 112 | |
| 113 | case IBLND_MSG_PUT_ACK: |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 114 | return hdr_size + sizeof(struct kib_putack_msg); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 115 | |
| 116 | case IBLND_MSG_GET_REQ: |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 117 | return hdr_size + sizeof(struct kib_get_msg); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 118 | |
| 119 | case IBLND_MSG_PUT_NAK: |
| 120 | case IBLND_MSG_PUT_DONE: |
| 121 | case IBLND_MSG_GET_DONE: |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 122 | return hdr_size + sizeof(struct kib_completion_msg); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 123 | default: |
| 124 | return -1; |
| 125 | } |
| 126 | } |
| 127 | |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 128 | static int kiblnd_unpack_rd(struct kib_msg *msg, int flip) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 129 | { |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 130 | struct kib_rdma_desc *rd; |
James Simmons | bbc2d82 | 2016-08-24 11:11:58 -0400 | [diff] [blame] | 131 | int msg_size; |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 132 | int nob; |
| 133 | int n; |
| 134 | int i; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 135 | |
Guillaume Matheron | febe73b | 2015-04-02 19:35:45 +0200 | [diff] [blame] | 136 | LASSERT(msg->ibm_type == IBLND_MSG_GET_REQ || |
James Simmons | c314c31 | 2016-02-12 12:06:01 -0500 | [diff] [blame] | 137 | msg->ibm_type == IBLND_MSG_PUT_ACK); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 138 | |
| 139 | rd = msg->ibm_type == IBLND_MSG_GET_REQ ? |
| 140 | &msg->ibm_u.get.ibgm_rd : |
| 141 | &msg->ibm_u.putack.ibpam_rd; |
| 142 | |
| 143 | if (flip) { |
| 144 | __swab32s(&rd->rd_key); |
| 145 | __swab32s(&rd->rd_nfrags); |
| 146 | } |
| 147 | |
| 148 | n = rd->rd_nfrags; |
| 149 | |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 150 | nob = offsetof(struct kib_msg, ibm_u) + |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 151 | kiblnd_rd_msg_size(rd, msg->ibm_type, n); |
| 152 | |
| 153 | if (msg->ibm_nob < nob) { |
| 154 | CERROR("Short %s: %d(%d)\n", |
| 155 | kiblnd_msgtype2str(msg->ibm_type), msg->ibm_nob, nob); |
| 156 | return 1; |
| 157 | } |
| 158 | |
James Simmons | bbc2d82 | 2016-08-24 11:11:58 -0400 | [diff] [blame] | 159 | msg_size = kiblnd_rd_size(rd); |
| 160 | if (msg_size <= 0 || msg_size > LNET_MAX_PAYLOAD) { |
| 161 | CERROR("Bad msg_size: %d, should be 0 < n <= %d\n", |
| 162 | msg_size, LNET_MAX_PAYLOAD); |
| 163 | return 1; |
| 164 | } |
| 165 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 166 | if (!flip) |
| 167 | return 0; |
| 168 | |
| 169 | for (i = 0; i < n; i++) { |
| 170 | __swab32s(&rd->rd_frags[i].rf_nob); |
| 171 | __swab64s(&rd->rd_frags[i].rf_addr); |
| 172 | } |
| 173 | |
| 174 | return 0; |
| 175 | } |
| 176 | |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 177 | void kiblnd_pack_msg(lnet_ni_t *ni, struct kib_msg *msg, int version, |
Guillaume Matheron | febe73b | 2015-04-02 19:35:45 +0200 | [diff] [blame] | 178 | int credits, lnet_nid_t dstnid, __u64 dststamp) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 179 | { |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 180 | struct kib_net *net = ni->ni_data; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 181 | |
James Simmons | 4420cfd | 2016-02-12 12:06:00 -0500 | [diff] [blame] | 182 | /* |
| 183 | * CAVEAT EMPTOR! all message fields not set here should have been |
| 184 | * initialised previously. |
| 185 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 186 | msg->ibm_magic = IBLND_MSG_MAGIC; |
| 187 | msg->ibm_version = version; |
| 188 | /* ibm_type */ |
| 189 | msg->ibm_credits = credits; |
| 190 | /* ibm_nob */ |
| 191 | msg->ibm_cksum = 0; |
| 192 | msg->ibm_srcnid = ni->ni_nid; |
| 193 | msg->ibm_srcstamp = net->ibn_incarnation; |
| 194 | msg->ibm_dstnid = dstnid; |
| 195 | msg->ibm_dststamp = dststamp; |
| 196 | |
| 197 | if (*kiblnd_tunables.kib_cksum) { |
| 198 | /* NB ibm_cksum zero while computing cksum */ |
| 199 | msg->ibm_cksum = kiblnd_cksum(msg, msg->ibm_nob); |
| 200 | } |
| 201 | } |
| 202 | |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 203 | int kiblnd_unpack_msg(struct kib_msg *msg, int nob) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 204 | { |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 205 | const int hdr_size = offsetof(struct kib_msg, ibm_u); |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 206 | __u32 msg_cksum; |
| 207 | __u16 version; |
| 208 | int msg_nob; |
| 209 | int flip; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 210 | |
| 211 | /* 6 bytes are enough to have received magic + version */ |
| 212 | if (nob < 6) { |
| 213 | CERROR("Short message: %d\n", nob); |
| 214 | return -EPROTO; |
| 215 | } |
| 216 | |
| 217 | if (msg->ibm_magic == IBLND_MSG_MAGIC) { |
| 218 | flip = 0; |
| 219 | } else if (msg->ibm_magic == __swab32(IBLND_MSG_MAGIC)) { |
| 220 | flip = 1; |
| 221 | } else { |
| 222 | CERROR("Bad magic: %08x\n", msg->ibm_magic); |
| 223 | return -EPROTO; |
| 224 | } |
| 225 | |
| 226 | version = flip ? __swab16(msg->ibm_version) : msg->ibm_version; |
| 227 | if (version != IBLND_MSG_VERSION && |
| 228 | version != IBLND_MSG_VERSION_1) { |
| 229 | CERROR("Bad version: %x\n", version); |
| 230 | return -EPROTO; |
| 231 | } |
| 232 | |
| 233 | if (nob < hdr_size) { |
| 234 | CERROR("Short message: %d\n", nob); |
| 235 | return -EPROTO; |
| 236 | } |
| 237 | |
| 238 | msg_nob = flip ? __swab32(msg->ibm_nob) : msg->ibm_nob; |
| 239 | if (msg_nob > nob) { |
| 240 | CERROR("Short message: got %d, wanted %d\n", nob, msg_nob); |
| 241 | return -EPROTO; |
| 242 | } |
| 243 | |
James Simmons | 4420cfd | 2016-02-12 12:06:00 -0500 | [diff] [blame] | 244 | /* |
| 245 | * checksum must be computed with ibm_cksum zero and BEFORE anything |
| 246 | * gets flipped |
| 247 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 248 | msg_cksum = flip ? __swab32(msg->ibm_cksum) : msg->ibm_cksum; |
| 249 | msg->ibm_cksum = 0; |
James Simmons | 5fd8833 | 2016-02-12 12:06:09 -0500 | [diff] [blame] | 250 | if (msg_cksum && |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 251 | msg_cksum != kiblnd_cksum(msg, msg_nob)) { |
| 252 | CERROR("Bad checksum\n"); |
| 253 | return -EPROTO; |
| 254 | } |
| 255 | |
| 256 | msg->ibm_cksum = msg_cksum; |
| 257 | |
| 258 | if (flip) { |
| 259 | /* leave magic unflipped as a clue to peer endianness */ |
| 260 | msg->ibm_version = version; |
Guillaume Matheron | febe73b | 2015-04-02 19:35:45 +0200 | [diff] [blame] | 261 | CLASSERT(sizeof(msg->ibm_type) == 1); |
| 262 | CLASSERT(sizeof(msg->ibm_credits) == 1); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 263 | msg->ibm_nob = msg_nob; |
| 264 | __swab64s(&msg->ibm_srcnid); |
| 265 | __swab64s(&msg->ibm_srcstamp); |
| 266 | __swab64s(&msg->ibm_dstnid); |
| 267 | __swab64s(&msg->ibm_dststamp); |
| 268 | } |
| 269 | |
| 270 | if (msg->ibm_srcnid == LNET_NID_ANY) { |
| 271 | CERROR("Bad src nid: %s\n", libcfs_nid2str(msg->ibm_srcnid)); |
| 272 | return -EPROTO; |
| 273 | } |
| 274 | |
| 275 | if (msg_nob < kiblnd_msgtype2size(msg->ibm_type)) { |
| 276 | CERROR("Short %s: %d(%d)\n", kiblnd_msgtype2str(msg->ibm_type), |
| 277 | msg_nob, kiblnd_msgtype2size(msg->ibm_type)); |
| 278 | return -EPROTO; |
| 279 | } |
| 280 | |
| 281 | switch (msg->ibm_type) { |
| 282 | default: |
| 283 | CERROR("Unknown message type %x\n", msg->ibm_type); |
| 284 | return -EPROTO; |
| 285 | |
| 286 | case IBLND_MSG_NOOP: |
| 287 | case IBLND_MSG_IMMEDIATE: |
| 288 | case IBLND_MSG_PUT_REQ: |
| 289 | break; |
| 290 | |
| 291 | case IBLND_MSG_PUT_ACK: |
| 292 | case IBLND_MSG_GET_REQ: |
| 293 | if (kiblnd_unpack_rd(msg, flip)) |
| 294 | return -EPROTO; |
| 295 | break; |
| 296 | |
| 297 | case IBLND_MSG_PUT_NAK: |
| 298 | case IBLND_MSG_PUT_DONE: |
| 299 | case IBLND_MSG_GET_DONE: |
| 300 | if (flip) |
| 301 | __swab32s(&msg->ibm_u.completion.ibcm_status); |
| 302 | break; |
| 303 | |
| 304 | case IBLND_MSG_CONNREQ: |
| 305 | case IBLND_MSG_CONNACK: |
| 306 | if (flip) { |
| 307 | __swab16s(&msg->ibm_u.connparams.ibcp_queue_depth); |
| 308 | __swab16s(&msg->ibm_u.connparams.ibcp_max_frags); |
| 309 | __swab32s(&msg->ibm_u.connparams.ibcp_max_msg_size); |
| 310 | } |
| 311 | break; |
| 312 | } |
| 313 | return 0; |
| 314 | } |
| 315 | |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 316 | int kiblnd_create_peer(lnet_ni_t *ni, struct kib_peer **peerp, lnet_nid_t nid) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 317 | { |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 318 | struct kib_peer *peer; |
| 319 | struct kib_net *net = ni->ni_data; |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 320 | int cpt = lnet_cpt_of_nid(nid); |
| 321 | unsigned long flags; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 322 | |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 323 | LASSERT(net); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 324 | LASSERT(nid != LNET_NID_ANY); |
| 325 | |
| 326 | LIBCFS_CPT_ALLOC(peer, lnet_cpt_table(), cpt, sizeof(*peer)); |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 327 | if (!peer) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 328 | CERROR("Cannot allocate peer\n"); |
| 329 | return -ENOMEM; |
| 330 | } |
| 331 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 332 | peer->ibp_ni = ni; |
| 333 | peer->ibp_nid = nid; |
| 334 | peer->ibp_error = 0; |
| 335 | peer->ibp_last_alive = 0; |
Amir Shehata | 9e7d5bf | 2016-05-06 21:30:25 -0400 | [diff] [blame] | 336 | peer->ibp_max_frags = kiblnd_cfg_rdma_frags(peer->ibp_ni); |
Amir Shehata | f6e5006 | 2016-05-06 21:30:27 -0400 | [diff] [blame] | 337 | peer->ibp_queue_depth = ni->ni_peertxcredits; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 338 | atomic_set(&peer->ibp_refcount, 1); /* 1 ref for caller */ |
| 339 | |
| 340 | INIT_LIST_HEAD(&peer->ibp_list); /* not in the peer table yet */ |
| 341 | INIT_LIST_HEAD(&peer->ibp_conns); |
| 342 | INIT_LIST_HEAD(&peer->ibp_tx_queue); |
| 343 | |
| 344 | write_lock_irqsave(&kiblnd_data.kib_global_lock, flags); |
| 345 | |
| 346 | /* always called with a ref on ni, which prevents ni being shutdown */ |
James Simmons | 5fd8833 | 2016-02-12 12:06:09 -0500 | [diff] [blame] | 347 | LASSERT(!net->ibn_shutdown); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 348 | |
| 349 | /* npeers only grows with the global lock held */ |
| 350 | atomic_inc(&net->ibn_npeers); |
| 351 | |
| 352 | write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags); |
| 353 | |
| 354 | *peerp = peer; |
| 355 | return 0; |
| 356 | } |
| 357 | |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 358 | void kiblnd_destroy_peer(struct kib_peer *peer) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 359 | { |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 360 | struct kib_net *net = peer->ibp_ni->ni_data; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 361 | |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 362 | LASSERT(net); |
James Simmons | 5fd8833 | 2016-02-12 12:06:09 -0500 | [diff] [blame] | 363 | LASSERT(!atomic_read(&peer->ibp_refcount)); |
Guillaume Matheron | febe73b | 2015-04-02 19:35:45 +0200 | [diff] [blame] | 364 | LASSERT(!kiblnd_peer_active(peer)); |
Liang Zhen | 4d99b25 | 2016-03-02 18:53:29 -0500 | [diff] [blame] | 365 | LASSERT(kiblnd_peer_idle(peer)); |
Guillaume Matheron | febe73b | 2015-04-02 19:35:45 +0200 | [diff] [blame] | 366 | LASSERT(list_empty(&peer->ibp_tx_queue)); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 367 | |
| 368 | LIBCFS_FREE(peer, sizeof(*peer)); |
| 369 | |
James Simmons | 4420cfd | 2016-02-12 12:06:00 -0500 | [diff] [blame] | 370 | /* |
| 371 | * NB a peer's connections keep a reference on their peer until |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 372 | * they are destroyed, so we can be assured that _all_ state to do |
| 373 | * with this peer has been cleaned up when its refcount drops to |
James Simmons | 4420cfd | 2016-02-12 12:06:00 -0500 | [diff] [blame] | 374 | * zero. |
| 375 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 376 | atomic_dec(&net->ibn_npeers); |
| 377 | } |
| 378 | |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 379 | struct kib_peer *kiblnd_find_peer_locked(lnet_nid_t nid) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 380 | { |
James Simmons | 4420cfd | 2016-02-12 12:06:00 -0500 | [diff] [blame] | 381 | /* |
| 382 | * the caller is responsible for accounting the additional reference |
| 383 | * that this creates |
| 384 | */ |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 385 | struct list_head *peer_list = kiblnd_nid2peerlist(nid); |
| 386 | struct list_head *tmp; |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 387 | struct kib_peer *peer; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 388 | |
Guillaume Matheron | febe73b | 2015-04-02 19:35:45 +0200 | [diff] [blame] | 389 | list_for_each(tmp, peer_list) { |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 390 | peer = list_entry(tmp, struct kib_peer, ibp_list); |
Liang Zhen | 4d99b25 | 2016-03-02 18:53:29 -0500 | [diff] [blame] | 391 | LASSERT(!kiblnd_peer_idle(peer)); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 392 | |
| 393 | if (peer->ibp_nid != nid) |
| 394 | continue; |
| 395 | |
| 396 | CDEBUG(D_NET, "got peer [%p] -> %s (%d) version: %x\n", |
| 397 | peer, libcfs_nid2str(nid), |
| 398 | atomic_read(&peer->ibp_refcount), |
| 399 | peer->ibp_version); |
| 400 | return peer; |
| 401 | } |
| 402 | return NULL; |
| 403 | } |
| 404 | |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 405 | void kiblnd_unlink_peer_locked(struct kib_peer *peer) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 406 | { |
Guillaume Matheron | febe73b | 2015-04-02 19:35:45 +0200 | [diff] [blame] | 407 | LASSERT(list_empty(&peer->ibp_conns)); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 408 | |
Guillaume Matheron | febe73b | 2015-04-02 19:35:45 +0200 | [diff] [blame] | 409 | LASSERT(kiblnd_peer_active(peer)); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 410 | list_del_init(&peer->ibp_list); |
| 411 | /* lose peerlist's ref */ |
| 412 | kiblnd_peer_decref(peer); |
| 413 | } |
| 414 | |
Guillaume Matheron | febe73b | 2015-04-02 19:35:45 +0200 | [diff] [blame] | 415 | static int kiblnd_get_peer_info(lnet_ni_t *ni, int index, |
| 416 | lnet_nid_t *nidp, int *count) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 417 | { |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 418 | struct kib_peer *peer; |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 419 | struct list_head *ptmp; |
| 420 | int i; |
| 421 | unsigned long flags; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 422 | |
| 423 | read_lock_irqsave(&kiblnd_data.kib_global_lock, flags); |
| 424 | |
| 425 | for (i = 0; i < kiblnd_data.kib_peer_hash_size; i++) { |
Guillaume Matheron | febe73b | 2015-04-02 19:35:45 +0200 | [diff] [blame] | 426 | list_for_each(ptmp, &kiblnd_data.kib_peers[i]) { |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 427 | peer = list_entry(ptmp, struct kib_peer, ibp_list); |
Liang Zhen | 4d99b25 | 2016-03-02 18:53:29 -0500 | [diff] [blame] | 428 | LASSERT(!kiblnd_peer_idle(peer)); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 429 | |
| 430 | if (peer->ibp_ni != ni) |
| 431 | continue; |
| 432 | |
| 433 | if (index-- > 0) |
| 434 | continue; |
| 435 | |
| 436 | *nidp = peer->ibp_nid; |
| 437 | *count = atomic_read(&peer->ibp_refcount); |
| 438 | |
| 439 | read_unlock_irqrestore(&kiblnd_data.kib_global_lock, |
| 440 | flags); |
| 441 | return 0; |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags); |
| 446 | return -ENOENT; |
| 447 | } |
| 448 | |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 449 | static void kiblnd_del_peer_locked(struct kib_peer *peer) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 450 | { |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 451 | struct list_head *ctmp; |
| 452 | struct list_head *cnxt; |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 453 | struct kib_conn *conn; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 454 | |
| 455 | if (list_empty(&peer->ibp_conns)) { |
| 456 | kiblnd_unlink_peer_locked(peer); |
| 457 | } else { |
Guillaume Matheron | febe73b | 2015-04-02 19:35:45 +0200 | [diff] [blame] | 458 | list_for_each_safe(ctmp, cnxt, &peer->ibp_conns) { |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 459 | conn = list_entry(ctmp, struct kib_conn, ibc_list); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 460 | |
| 461 | kiblnd_close_conn_locked(conn, 0); |
| 462 | } |
| 463 | /* NB closing peer's last conn unlinked it. */ |
| 464 | } |
James Simmons | 4420cfd | 2016-02-12 12:06:00 -0500 | [diff] [blame] | 465 | /* |
| 466 | * NB peer now unlinked; might even be freed if the peer table had the |
| 467 | * last ref on it. |
| 468 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 469 | } |
| 470 | |
Guillaume Matheron | febe73b | 2015-04-02 19:35:45 +0200 | [diff] [blame] | 471 | static int kiblnd_del_peer(lnet_ni_t *ni, lnet_nid_t nid) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 472 | { |
Guillaume Matheron | febe73b | 2015-04-02 19:35:45 +0200 | [diff] [blame] | 473 | LIST_HEAD(zombies); |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 474 | struct list_head *ptmp; |
| 475 | struct list_head *pnxt; |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 476 | struct kib_peer *peer; |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 477 | int lo; |
| 478 | int hi; |
| 479 | int i; |
| 480 | unsigned long flags; |
| 481 | int rc = -ENOENT; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 482 | |
| 483 | write_lock_irqsave(&kiblnd_data.kib_global_lock, flags); |
| 484 | |
| 485 | if (nid != LNET_NID_ANY) { |
James Simmons | d3d3d37 | 2016-02-12 12:06:05 -0500 | [diff] [blame] | 486 | lo = kiblnd_nid2peerlist(nid) - kiblnd_data.kib_peers; |
| 487 | hi = kiblnd_nid2peerlist(nid) - kiblnd_data.kib_peers; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 488 | } else { |
| 489 | lo = 0; |
| 490 | hi = kiblnd_data.kib_peer_hash_size - 1; |
| 491 | } |
| 492 | |
| 493 | for (i = lo; i <= hi; i++) { |
Guillaume Matheron | febe73b | 2015-04-02 19:35:45 +0200 | [diff] [blame] | 494 | list_for_each_safe(ptmp, pnxt, &kiblnd_data.kib_peers[i]) { |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 495 | peer = list_entry(ptmp, struct kib_peer, ibp_list); |
Liang Zhen | 4d99b25 | 2016-03-02 18:53:29 -0500 | [diff] [blame] | 496 | LASSERT(!kiblnd_peer_idle(peer)); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 497 | |
| 498 | if (peer->ibp_ni != ni) |
| 499 | continue; |
| 500 | |
| 501 | if (!(nid == LNET_NID_ANY || peer->ibp_nid == nid)) |
| 502 | continue; |
| 503 | |
| 504 | if (!list_empty(&peer->ibp_tx_queue)) { |
Guillaume Matheron | febe73b | 2015-04-02 19:35:45 +0200 | [diff] [blame] | 505 | LASSERT(list_empty(&peer->ibp_conns)); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 506 | |
| 507 | list_splice_init(&peer->ibp_tx_queue, |
James Simmons | c314c31 | 2016-02-12 12:06:01 -0500 | [diff] [blame] | 508 | &zombies); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | kiblnd_del_peer_locked(peer); |
| 512 | rc = 0; /* matched something */ |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags); |
| 517 | |
| 518 | kiblnd_txlist_done(ni, &zombies, -EIO); |
| 519 | |
| 520 | return rc; |
| 521 | } |
| 522 | |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 523 | static struct kib_conn *kiblnd_get_conn_by_idx(lnet_ni_t *ni, int index) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 524 | { |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 525 | struct kib_peer *peer; |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 526 | struct list_head *ptmp; |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 527 | struct kib_conn *conn; |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 528 | struct list_head *ctmp; |
| 529 | int i; |
| 530 | unsigned long flags; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 531 | |
| 532 | read_lock_irqsave(&kiblnd_data.kib_global_lock, flags); |
| 533 | |
| 534 | for (i = 0; i < kiblnd_data.kib_peer_hash_size; i++) { |
Guillaume Matheron | febe73b | 2015-04-02 19:35:45 +0200 | [diff] [blame] | 535 | list_for_each(ptmp, &kiblnd_data.kib_peers[i]) { |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 536 | peer = list_entry(ptmp, struct kib_peer, ibp_list); |
Liang Zhen | 4d99b25 | 2016-03-02 18:53:29 -0500 | [diff] [blame] | 537 | LASSERT(!kiblnd_peer_idle(peer)); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 538 | |
| 539 | if (peer->ibp_ni != ni) |
| 540 | continue; |
| 541 | |
Guillaume Matheron | febe73b | 2015-04-02 19:35:45 +0200 | [diff] [blame] | 542 | list_for_each(ctmp, &peer->ibp_conns) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 543 | if (index-- > 0) |
| 544 | continue; |
| 545 | |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 546 | conn = list_entry(ctmp, struct kib_conn, |
James Simmons | c314c31 | 2016-02-12 12:06:01 -0500 | [diff] [blame] | 547 | ibc_list); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 548 | kiblnd_conn_addref(conn); |
Guillaume Matheron | 7a3888a | 2015-04-02 19:52:07 +0200 | [diff] [blame] | 549 | read_unlock_irqrestore( |
| 550 | &kiblnd_data.kib_global_lock, |
| 551 | flags); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 552 | return conn; |
| 553 | } |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags); |
| 558 | return NULL; |
| 559 | } |
| 560 | |
Guillaume Matheron | febe73b | 2015-04-02 19:35:45 +0200 | [diff] [blame] | 561 | int kiblnd_translate_mtu(int value) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 562 | { |
| 563 | switch (value) { |
| 564 | default: |
| 565 | return -1; |
| 566 | case 0: |
| 567 | return 0; |
| 568 | case 256: |
| 569 | return IB_MTU_256; |
| 570 | case 512: |
| 571 | return IB_MTU_512; |
| 572 | case 1024: |
| 573 | return IB_MTU_1024; |
| 574 | case 2048: |
| 575 | return IB_MTU_2048; |
| 576 | case 4096: |
| 577 | return IB_MTU_4096; |
| 578 | } |
| 579 | } |
| 580 | |
Guillaume Matheron | febe73b | 2015-04-02 19:35:45 +0200 | [diff] [blame] | 581 | static void kiblnd_setup_mtu_locked(struct rdma_cm_id *cmid) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 582 | { |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 583 | int mtu; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 584 | |
| 585 | /* XXX There is no path record for iWARP, set by netdev->change_mtu? */ |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 586 | if (!cmid->route.path_rec) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 587 | return; |
| 588 | |
| 589 | mtu = kiblnd_translate_mtu(*kiblnd_tunables.kib_ib_mtu); |
Guillaume Matheron | febe73b | 2015-04-02 19:35:45 +0200 | [diff] [blame] | 590 | LASSERT(mtu >= 0); |
James Simmons | 5fd8833 | 2016-02-12 12:06:09 -0500 | [diff] [blame] | 591 | if (mtu) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 592 | cmid->route.path_rec->mtu = mtu; |
| 593 | } |
| 594 | |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 595 | static int kiblnd_get_completion_vector(struct kib_conn *conn, int cpt) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 596 | { |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 597 | cpumask_t *mask; |
| 598 | int vectors; |
| 599 | int off; |
| 600 | int i; |
| 601 | lnet_nid_t nid = conn->ibc_peer->ibp_nid; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 602 | |
| 603 | vectors = conn->ibc_cmid->device->num_comp_vectors; |
| 604 | if (vectors <= 1) |
| 605 | return 0; |
| 606 | |
| 607 | mask = cfs_cpt_cpumask(lnet_cpt_table(), cpt); |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 608 | if (!mask) |
Peng Tao | 3867ea5 | 2013-07-15 22:27:10 +0800 | [diff] [blame] | 609 | return 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 610 | |
| 611 | /* hash NID to CPU id in this partition... */ |
Oleg Drokin | 4a316f7 | 2015-03-07 19:24:27 -0500 | [diff] [blame] | 612 | off = do_div(nid, cpumask_weight(mask)); |
| 613 | for_each_cpu(i, mask) { |
James Simmons | 5fd8833 | 2016-02-12 12:06:09 -0500 | [diff] [blame] | 614 | if (!off--) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 615 | return i % vectors; |
| 616 | } |
| 617 | |
| 618 | LBUG(); |
| 619 | return 1; |
| 620 | } |
| 621 | |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 622 | struct kib_conn *kiblnd_create_conn(struct kib_peer *peer, struct rdma_cm_id *cmid, |
Oleg Drokin | 24c198e | 2016-08-21 18:04:35 -0400 | [diff] [blame] | 623 | int state, int version) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 624 | { |
James Simmons | 4420cfd | 2016-02-12 12:06:00 -0500 | [diff] [blame] | 625 | /* |
| 626 | * CAVEAT EMPTOR: |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 627 | * If the new conn is created successfully it takes over the caller's |
| 628 | * ref on 'peer'. It also "owns" 'cmid' and destroys it when it itself |
| 629 | * is destroyed. On failure, the caller's ref on 'peer' remains and |
| 630 | * she must dispose of 'cmid'. (Actually I'd block forever if I tried |
| 631 | * to destroy 'cmid' here since I'm called from the CM which still has |
James Simmons | 4420cfd | 2016-02-12 12:06:00 -0500 | [diff] [blame] | 632 | * its ref on 'cmid'). |
| 633 | */ |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 634 | rwlock_t *glock = &kiblnd_data.kib_global_lock; |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 635 | struct kib_net *net = peer->ibp_ni->ni_data; |
| 636 | struct kib_dev *dev; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 637 | struct ib_qp_init_attr *init_qp_attr; |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 638 | struct kib_sched_info *sched; |
Linus Torvalds | 23908db | 2015-06-26 15:46:08 -0700 | [diff] [blame] | 639 | struct ib_cq_init_attr cq_attr = {}; |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 640 | struct kib_conn *conn; |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 641 | struct ib_cq *cq; |
| 642 | unsigned long flags; |
| 643 | int cpt; |
| 644 | int rc; |
| 645 | int i; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 646 | |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 647 | LASSERT(net); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 648 | LASSERT(!in_interrupt()); |
| 649 | |
| 650 | dev = net->ibn_dev; |
| 651 | |
| 652 | cpt = lnet_cpt_of_nid(peer->ibp_nid); |
| 653 | sched = kiblnd_data.kib_scheds[cpt]; |
| 654 | |
| 655 | LASSERT(sched->ibs_nthreads > 0); |
| 656 | |
| 657 | LIBCFS_CPT_ALLOC(init_qp_attr, lnet_cpt_table(), cpt, |
| 658 | sizeof(*init_qp_attr)); |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 659 | if (!init_qp_attr) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 660 | CERROR("Can't allocate qp_attr for %s\n", |
| 661 | libcfs_nid2str(peer->ibp_nid)); |
| 662 | goto failed_0; |
| 663 | } |
| 664 | |
| 665 | LIBCFS_CPT_ALLOC(conn, lnet_cpt_table(), cpt, sizeof(*conn)); |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 666 | if (!conn) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 667 | CERROR("Can't allocate connection for %s\n", |
| 668 | libcfs_nid2str(peer->ibp_nid)); |
| 669 | goto failed_1; |
| 670 | } |
| 671 | |
| 672 | conn->ibc_state = IBLND_CONN_INIT; |
| 673 | conn->ibc_version = version; |
| 674 | conn->ibc_peer = peer; /* I take the caller's ref */ |
| 675 | cmid->context = conn; /* for future CM callbacks */ |
| 676 | conn->ibc_cmid = cmid; |
Amir Shehata | a01fa10 | 2016-03-02 18:53:25 -0500 | [diff] [blame] | 677 | conn->ibc_max_frags = peer->ibp_max_frags; |
| 678 | conn->ibc_queue_depth = peer->ibp_queue_depth; |
Jeremy Filizetti | 2fb44f2 | 2016-03-02 18:53:24 -0500 | [diff] [blame] | 679 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 680 | INIT_LIST_HEAD(&conn->ibc_early_rxs); |
| 681 | INIT_LIST_HEAD(&conn->ibc_tx_noops); |
| 682 | INIT_LIST_HEAD(&conn->ibc_tx_queue); |
| 683 | INIT_LIST_HEAD(&conn->ibc_tx_queue_rsrvd); |
| 684 | INIT_LIST_HEAD(&conn->ibc_tx_queue_nocred); |
| 685 | INIT_LIST_HEAD(&conn->ibc_active_txs); |
| 686 | spin_lock_init(&conn->ibc_lock); |
| 687 | |
| 688 | LIBCFS_CPT_ALLOC(conn->ibc_connvars, lnet_cpt_table(), cpt, |
| 689 | sizeof(*conn->ibc_connvars)); |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 690 | if (!conn->ibc_connvars) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 691 | CERROR("Can't allocate in-progress connection state\n"); |
| 692 | goto failed_2; |
| 693 | } |
| 694 | |
| 695 | write_lock_irqsave(glock, flags); |
| 696 | if (dev->ibd_failover) { |
| 697 | write_unlock_irqrestore(glock, flags); |
| 698 | CERROR("%s: failover in progress\n", dev->ibd_ifname); |
| 699 | goto failed_2; |
| 700 | } |
| 701 | |
| 702 | if (dev->ibd_hdev->ibh_ibdev != cmid->device) { |
| 703 | /* wakeup failover thread and teardown connection */ |
| 704 | if (kiblnd_dev_can_failover(dev)) { |
| 705 | list_add_tail(&dev->ibd_fail_list, |
| 706 | &kiblnd_data.kib_failed_devs); |
| 707 | wake_up(&kiblnd_data.kib_failover_waitq); |
| 708 | } |
| 709 | |
| 710 | write_unlock_irqrestore(glock, flags); |
| 711 | CERROR("cmid HCA(%s), kib_dev(%s) need failover\n", |
| 712 | cmid->device->name, dev->ibd_ifname); |
| 713 | goto failed_2; |
| 714 | } |
| 715 | |
| 716 | kiblnd_hdev_addref_locked(dev->ibd_hdev); |
| 717 | conn->ibc_hdev = dev->ibd_hdev; |
| 718 | |
| 719 | kiblnd_setup_mtu_locked(cmid); |
| 720 | |
| 721 | write_unlock_irqrestore(glock, flags); |
| 722 | |
| 723 | LIBCFS_CPT_ALLOC(conn->ibc_rxs, lnet_cpt_table(), cpt, |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 724 | IBLND_RX_MSGS(conn) * sizeof(struct kib_rx)); |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 725 | if (!conn->ibc_rxs) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 726 | CERROR("Cannot allocate RX buffers\n"); |
| 727 | goto failed_2; |
| 728 | } |
| 729 | |
| 730 | rc = kiblnd_alloc_pages(&conn->ibc_rx_pages, cpt, |
Jeremy Filizetti | 2fb44f2 | 2016-03-02 18:53:24 -0500 | [diff] [blame] | 731 | IBLND_RX_MSG_PAGES(conn)); |
James Simmons | 5fd8833 | 2016-02-12 12:06:09 -0500 | [diff] [blame] | 732 | if (rc) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 733 | goto failed_2; |
| 734 | |
| 735 | kiblnd_map_rx_descs(conn); |
| 736 | |
Jeremy Filizetti | 2fb44f2 | 2016-03-02 18:53:24 -0500 | [diff] [blame] | 737 | cq_attr.cqe = IBLND_CQ_ENTRIES(conn); |
Matan Barak | 8e37210 | 2015-06-11 16:35:21 +0300 | [diff] [blame] | 738 | cq_attr.comp_vector = kiblnd_get_completion_vector(conn, cpt); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 739 | cq = ib_create_cq(cmid->device, |
| 740 | kiblnd_cq_completion, kiblnd_cq_event, conn, |
Matan Barak | 8e37210 | 2015-06-11 16:35:21 +0300 | [diff] [blame] | 741 | &cq_attr); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 742 | if (IS_ERR(cq)) { |
Jeremy Filizetti | 2fb44f2 | 2016-03-02 18:53:24 -0500 | [diff] [blame] | 743 | CERROR("Failed to create CQ with %d CQEs: %ld\n", |
| 744 | IBLND_CQ_ENTRIES(conn), PTR_ERR(cq)); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 745 | goto failed_2; |
| 746 | } |
| 747 | |
| 748 | conn->ibc_cq = cq; |
| 749 | |
| 750 | rc = ib_req_notify_cq(cq, IB_CQ_NEXT_COMP); |
James Simmons | 5fd8833 | 2016-02-12 12:06:09 -0500 | [diff] [blame] | 751 | if (rc) { |
Frank Zago | 9c37966 | 2016-03-02 17:02:02 -0500 | [diff] [blame] | 752 | CERROR("Can't request completion notification: %d\n", rc); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 753 | goto failed_2; |
| 754 | } |
| 755 | |
| 756 | init_qp_attr->event_handler = kiblnd_qp_event; |
| 757 | init_qp_attr->qp_context = conn; |
Jeremy Filizetti | 2fb44f2 | 2016-03-02 18:53:24 -0500 | [diff] [blame] | 758 | init_qp_attr->cap.max_send_wr = IBLND_SEND_WRS(conn); |
| 759 | init_qp_attr->cap.max_recv_wr = IBLND_RECV_WRS(conn); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 760 | init_qp_attr->cap.max_send_sge = 1; |
| 761 | init_qp_attr->cap.max_recv_sge = 1; |
| 762 | init_qp_attr->sq_sig_type = IB_SIGNAL_REQ_WR; |
| 763 | init_qp_attr->qp_type = IB_QPT_RC; |
| 764 | init_qp_attr->send_cq = cq; |
| 765 | init_qp_attr->recv_cq = cq; |
| 766 | |
| 767 | conn->ibc_sched = sched; |
| 768 | |
| 769 | rc = rdma_create_qp(cmid, conn->ibc_hdev->ibh_pd, init_qp_attr); |
James Simmons | 5fd8833 | 2016-02-12 12:06:09 -0500 | [diff] [blame] | 770 | if (rc) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 771 | CERROR("Can't create QP: %d, send_wr: %d, recv_wr: %d\n", |
| 772 | rc, init_qp_attr->cap.max_send_wr, |
| 773 | init_qp_attr->cap.max_recv_wr); |
| 774 | goto failed_2; |
| 775 | } |
| 776 | |
| 777 | LIBCFS_FREE(init_qp_attr, sizeof(*init_qp_attr)); |
| 778 | |
| 779 | /* 1 ref for caller and each rxmsg */ |
Jeremy Filizetti | 2fb44f2 | 2016-03-02 18:53:24 -0500 | [diff] [blame] | 780 | atomic_set(&conn->ibc_refcount, 1 + IBLND_RX_MSGS(conn)); |
| 781 | conn->ibc_nrx = IBLND_RX_MSGS(conn); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 782 | |
| 783 | /* post receives */ |
Jeremy Filizetti | 2fb44f2 | 2016-03-02 18:53:24 -0500 | [diff] [blame] | 784 | for (i = 0; i < IBLND_RX_MSGS(conn); i++) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 785 | rc = kiblnd_post_rx(&conn->ibc_rxs[i], |
| 786 | IBLND_POSTRX_NO_CREDIT); |
James Simmons | 5fd8833 | 2016-02-12 12:06:09 -0500 | [diff] [blame] | 787 | if (rc) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 788 | CERROR("Can't post rxmsg: %d\n", rc); |
| 789 | |
| 790 | /* Make posted receives complete */ |
| 791 | kiblnd_abort_receives(conn); |
| 792 | |
James Simmons | 4420cfd | 2016-02-12 12:06:00 -0500 | [diff] [blame] | 793 | /* |
| 794 | * correct # of posted buffers |
| 795 | * NB locking needed now I'm racing with completion |
| 796 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 797 | spin_lock_irqsave(&sched->ibs_lock, flags); |
Jeremy Filizetti | 2fb44f2 | 2016-03-02 18:53:24 -0500 | [diff] [blame] | 798 | conn->ibc_nrx -= IBLND_RX_MSGS(conn) - i; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 799 | spin_unlock_irqrestore(&sched->ibs_lock, flags); |
| 800 | |
James Simmons | 4420cfd | 2016-02-12 12:06:00 -0500 | [diff] [blame] | 801 | /* |
| 802 | * cmid will be destroyed by CM(ofed) after cm_callback |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 803 | * returned, so we can't refer it anymore |
James Simmons | 4420cfd | 2016-02-12 12:06:00 -0500 | [diff] [blame] | 804 | * (by kiblnd_connd()->kiblnd_destroy_conn) |
| 805 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 806 | rdma_destroy_qp(conn->ibc_cmid); |
| 807 | conn->ibc_cmid = NULL; |
| 808 | |
| 809 | /* Drop my own and unused rxbuffer refcounts */ |
Jeremy Filizetti | 2fb44f2 | 2016-03-02 18:53:24 -0500 | [diff] [blame] | 810 | while (i++ <= IBLND_RX_MSGS(conn)) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 811 | kiblnd_conn_decref(conn); |
| 812 | |
| 813 | return NULL; |
| 814 | } |
| 815 | } |
| 816 | |
| 817 | /* Init successful! */ |
Guillaume Matheron | febe73b | 2015-04-02 19:35:45 +0200 | [diff] [blame] | 818 | LASSERT(state == IBLND_CONN_ACTIVE_CONNECT || |
James Simmons | c314c31 | 2016-02-12 12:06:01 -0500 | [diff] [blame] | 819 | state == IBLND_CONN_PASSIVE_WAIT); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 820 | conn->ibc_state = state; |
| 821 | |
| 822 | /* 1 more conn */ |
| 823 | atomic_inc(&net->ibn_nconns); |
| 824 | return conn; |
| 825 | |
| 826 | failed_2: |
Liang Zhen | 4d99b25 | 2016-03-02 18:53:29 -0500 | [diff] [blame] | 827 | kiblnd_destroy_conn(conn, true); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 828 | failed_1: |
| 829 | LIBCFS_FREE(init_qp_attr, sizeof(*init_qp_attr)); |
| 830 | failed_0: |
| 831 | return NULL; |
| 832 | } |
| 833 | |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 834 | void kiblnd_destroy_conn(struct kib_conn *conn, bool free_conn) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 835 | { |
| 836 | struct rdma_cm_id *cmid = conn->ibc_cmid; |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 837 | struct kib_peer *peer = conn->ibc_peer; |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 838 | int rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 839 | |
Guillaume Matheron | febe73b | 2015-04-02 19:35:45 +0200 | [diff] [blame] | 840 | LASSERT(!in_interrupt()); |
James Simmons | 5fd8833 | 2016-02-12 12:06:09 -0500 | [diff] [blame] | 841 | LASSERT(!atomic_read(&conn->ibc_refcount)); |
Guillaume Matheron | febe73b | 2015-04-02 19:35:45 +0200 | [diff] [blame] | 842 | LASSERT(list_empty(&conn->ibc_early_rxs)); |
| 843 | LASSERT(list_empty(&conn->ibc_tx_noops)); |
| 844 | LASSERT(list_empty(&conn->ibc_tx_queue)); |
| 845 | LASSERT(list_empty(&conn->ibc_tx_queue_rsrvd)); |
| 846 | LASSERT(list_empty(&conn->ibc_tx_queue_nocred)); |
| 847 | LASSERT(list_empty(&conn->ibc_active_txs)); |
James Simmons | 5fd8833 | 2016-02-12 12:06:09 -0500 | [diff] [blame] | 848 | LASSERT(!conn->ibc_noops_posted); |
| 849 | LASSERT(!conn->ibc_nsends_posted); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 850 | |
| 851 | switch (conn->ibc_state) { |
| 852 | default: |
| 853 | /* conn must be completely disengaged from the network */ |
| 854 | LBUG(); |
| 855 | |
| 856 | case IBLND_CONN_DISCONNECTED: |
| 857 | /* connvars should have been freed already */ |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 858 | LASSERT(!conn->ibc_connvars); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 859 | break; |
| 860 | |
| 861 | case IBLND_CONN_INIT: |
| 862 | break; |
| 863 | } |
| 864 | |
| 865 | /* conn->ibc_cmid might be destroyed by CM already */ |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 866 | if (cmid && cmid->qp) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 867 | rdma_destroy_qp(cmid); |
| 868 | |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 869 | if (conn->ibc_cq) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 870 | rc = ib_destroy_cq(conn->ibc_cq); |
James Simmons | 5fd8833 | 2016-02-12 12:06:09 -0500 | [diff] [blame] | 871 | if (rc) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 872 | CWARN("Error destroying CQ: %d\n", rc); |
| 873 | } |
| 874 | |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 875 | if (conn->ibc_rx_pages) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 876 | kiblnd_unmap_rx_descs(conn); |
| 877 | |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 878 | if (conn->ibc_rxs) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 879 | LIBCFS_FREE(conn->ibc_rxs, |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 880 | IBLND_RX_MSGS(conn) * sizeof(struct kib_rx)); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 881 | } |
| 882 | |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 883 | if (conn->ibc_connvars) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 884 | LIBCFS_FREE(conn->ibc_connvars, sizeof(*conn->ibc_connvars)); |
| 885 | |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 886 | if (conn->ibc_hdev) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 887 | kiblnd_hdev_decref(conn->ibc_hdev); |
| 888 | |
| 889 | /* See CAVEAT EMPTOR above in kiblnd_create_conn */ |
| 890 | if (conn->ibc_state != IBLND_CONN_INIT) { |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 891 | struct kib_net *net = peer->ibp_ni->ni_data; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 892 | |
| 893 | kiblnd_peer_decref(peer); |
| 894 | rdma_destroy_id(cmid); |
| 895 | atomic_dec(&net->ibn_nconns); |
| 896 | } |
| 897 | |
| 898 | LIBCFS_FREE(conn, sizeof(*conn)); |
| 899 | } |
| 900 | |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 901 | int kiblnd_close_peer_conns_locked(struct kib_peer *peer, int why) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 902 | { |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 903 | struct kib_conn *conn; |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 904 | struct list_head *ctmp; |
| 905 | struct list_head *cnxt; |
| 906 | int count = 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 907 | |
Guillaume Matheron | febe73b | 2015-04-02 19:35:45 +0200 | [diff] [blame] | 908 | list_for_each_safe(ctmp, cnxt, &peer->ibp_conns) { |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 909 | conn = list_entry(ctmp, struct kib_conn, ibc_list); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 910 | |
Joe Perches | 2d00bd1 | 2014-11-23 11:28:50 -0800 | [diff] [blame] | 911 | CDEBUG(D_NET, "Closing conn -> %s, version: %x, reason: %d\n", |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 912 | libcfs_nid2str(peer->ibp_nid), |
| 913 | conn->ibc_version, why); |
| 914 | |
| 915 | kiblnd_close_conn_locked(conn, why); |
| 916 | count++; |
| 917 | } |
| 918 | |
| 919 | return count; |
| 920 | } |
| 921 | |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 922 | int kiblnd_close_stale_conns_locked(struct kib_peer *peer, |
James Simmons | c314c31 | 2016-02-12 12:06:01 -0500 | [diff] [blame] | 923 | int version, __u64 incarnation) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 924 | { |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 925 | struct kib_conn *conn; |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 926 | struct list_head *ctmp; |
| 927 | struct list_head *cnxt; |
| 928 | int count = 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 929 | |
Guillaume Matheron | febe73b | 2015-04-02 19:35:45 +0200 | [diff] [blame] | 930 | list_for_each_safe(ctmp, cnxt, &peer->ibp_conns) { |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 931 | conn = list_entry(ctmp, struct kib_conn, ibc_list); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 932 | |
| 933 | if (conn->ibc_version == version && |
| 934 | conn->ibc_incarnation == incarnation) |
| 935 | continue; |
| 936 | |
Guillaume Matheron | 7a3888a | 2015-04-02 19:52:07 +0200 | [diff] [blame] | 937 | CDEBUG(D_NET, |
| 938 | "Closing stale conn -> %s version: %x, incarnation:%#llx(%x, %#llx)\n", |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 939 | libcfs_nid2str(peer->ibp_nid), |
| 940 | conn->ibc_version, conn->ibc_incarnation, |
| 941 | version, incarnation); |
| 942 | |
| 943 | kiblnd_close_conn_locked(conn, -ESTALE); |
| 944 | count++; |
| 945 | } |
| 946 | |
| 947 | return count; |
| 948 | } |
| 949 | |
Guillaume Matheron | febe73b | 2015-04-02 19:35:45 +0200 | [diff] [blame] | 950 | static int kiblnd_close_matching_conns(lnet_ni_t *ni, lnet_nid_t nid) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 951 | { |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 952 | struct kib_peer *peer; |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 953 | struct list_head *ptmp; |
| 954 | struct list_head *pnxt; |
| 955 | int lo; |
| 956 | int hi; |
| 957 | int i; |
| 958 | unsigned long flags; |
| 959 | int count = 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 960 | |
| 961 | write_lock_irqsave(&kiblnd_data.kib_global_lock, flags); |
| 962 | |
James Simmons | d3d3d37 | 2016-02-12 12:06:05 -0500 | [diff] [blame] | 963 | if (nid != LNET_NID_ANY) { |
| 964 | lo = kiblnd_nid2peerlist(nid) - kiblnd_data.kib_peers; |
| 965 | hi = kiblnd_nid2peerlist(nid) - kiblnd_data.kib_peers; |
| 966 | } else { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 967 | lo = 0; |
| 968 | hi = kiblnd_data.kib_peer_hash_size - 1; |
| 969 | } |
| 970 | |
| 971 | for (i = lo; i <= hi; i++) { |
Guillaume Matheron | febe73b | 2015-04-02 19:35:45 +0200 | [diff] [blame] | 972 | list_for_each_safe(ptmp, pnxt, &kiblnd_data.kib_peers[i]) { |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 973 | peer = list_entry(ptmp, struct kib_peer, ibp_list); |
Liang Zhen | 4d99b25 | 2016-03-02 18:53:29 -0500 | [diff] [blame] | 974 | LASSERT(!kiblnd_peer_idle(peer)); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 975 | |
| 976 | if (peer->ibp_ni != ni) |
| 977 | continue; |
| 978 | |
| 979 | if (!(nid == LNET_NID_ANY || nid == peer->ibp_nid)) |
| 980 | continue; |
| 981 | |
| 982 | count += kiblnd_close_peer_conns_locked(peer, 0); |
| 983 | } |
| 984 | } |
| 985 | |
| 986 | write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags); |
| 987 | |
| 988 | /* wildcards always succeed */ |
| 989 | if (nid == LNET_NID_ANY) |
| 990 | return 0; |
| 991 | |
James Simmons | 5fd8833 | 2016-02-12 12:06:09 -0500 | [diff] [blame] | 992 | return !count ? -ENOENT : 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 993 | } |
| 994 | |
Frank Zago | 439b4d4 | 2016-03-02 17:02:00 -0500 | [diff] [blame] | 995 | static int kiblnd_ctl(lnet_ni_t *ni, unsigned int cmd, void *arg) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 996 | { |
| 997 | struct libcfs_ioctl_data *data = arg; |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 998 | int rc = -EINVAL; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 999 | |
Greg Donald | a58a38a | 2014-08-21 12:40:35 -0500 | [diff] [blame] | 1000 | switch (cmd) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1001 | case IOC_LIBCFS_GET_PEER: { |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 1002 | lnet_nid_t nid = 0; |
| 1003 | int count = 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1004 | |
| 1005 | rc = kiblnd_get_peer_info(ni, data->ioc_count, |
| 1006 | &nid, &count); |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 1007 | data->ioc_nid = nid; |
| 1008 | data->ioc_count = count; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1009 | break; |
| 1010 | } |
| 1011 | |
| 1012 | case IOC_LIBCFS_DEL_PEER: { |
| 1013 | rc = kiblnd_del_peer(ni, data->ioc_nid); |
| 1014 | break; |
| 1015 | } |
| 1016 | case IOC_LIBCFS_GET_CONN: { |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 1017 | struct kib_conn *conn; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1018 | |
| 1019 | rc = 0; |
| 1020 | conn = kiblnd_get_conn_by_idx(ni, data->ioc_count); |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 1021 | if (!conn) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1022 | rc = -ENOENT; |
| 1023 | break; |
| 1024 | } |
| 1025 | |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 1026 | LASSERT(conn->ibc_cmid); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1027 | data->ioc_nid = conn->ibc_peer->ibp_nid; |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 1028 | if (!conn->ibc_cmid->route.path_rec) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1029 | data->ioc_u32[0] = 0; /* iWarp has no path MTU */ |
| 1030 | else |
| 1031 | data->ioc_u32[0] = |
| 1032 | ib_mtu_enum_to_int(conn->ibc_cmid->route.path_rec->mtu); |
| 1033 | kiblnd_conn_decref(conn); |
| 1034 | break; |
| 1035 | } |
| 1036 | case IOC_LIBCFS_CLOSE_CONNECTION: { |
| 1037 | rc = kiblnd_close_matching_conns(ni, data->ioc_nid); |
| 1038 | break; |
| 1039 | } |
| 1040 | |
| 1041 | default: |
| 1042 | break; |
| 1043 | } |
| 1044 | |
| 1045 | return rc; |
| 1046 | } |
| 1047 | |
Frank Zago | 439b4d4 | 2016-03-02 17:02:00 -0500 | [diff] [blame] | 1048 | static void kiblnd_query(lnet_ni_t *ni, lnet_nid_t nid, unsigned long *when) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1049 | { |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 1050 | unsigned long last_alive = 0; |
| 1051 | unsigned long now = cfs_time_current(); |
| 1052 | rwlock_t *glock = &kiblnd_data.kib_global_lock; |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 1053 | struct kib_peer *peer; |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 1054 | unsigned long flags; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1055 | |
| 1056 | read_lock_irqsave(glock, flags); |
| 1057 | |
| 1058 | peer = kiblnd_find_peer_locked(nid); |
Liang Zhen | 4d99b25 | 2016-03-02 18:53:29 -0500 | [diff] [blame] | 1059 | if (peer) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1060 | last_alive = peer->ibp_last_alive; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1061 | |
| 1062 | read_unlock_irqrestore(glock, flags); |
| 1063 | |
James Simmons | 5fd8833 | 2016-02-12 12:06:09 -0500 | [diff] [blame] | 1064 | if (last_alive) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1065 | *when = last_alive; |
| 1066 | |
James Simmons | 4420cfd | 2016-02-12 12:06:00 -0500 | [diff] [blame] | 1067 | /* |
| 1068 | * peer is not persistent in hash, trigger peer creation |
| 1069 | * and connection establishment with a NULL tx |
| 1070 | */ |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 1071 | if (!peer) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1072 | kiblnd_launch_tx(ni, NULL, nid); |
| 1073 | |
| 1074 | CDEBUG(D_NET, "Peer %s %p, alive %ld secs ago\n", |
| 1075 | libcfs_nid2str(nid), peer, |
| 1076 | last_alive ? cfs_duration_sec(now - last_alive) : -1); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1077 | } |
| 1078 | |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 1079 | static void kiblnd_free_pages(struct kib_pages *p) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1080 | { |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 1081 | int npages = p->ibp_npages; |
| 1082 | int i; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1083 | |
| 1084 | for (i = 0; i < npages; i++) { |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 1085 | if (p->ibp_pages[i]) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1086 | __free_page(p->ibp_pages[i]); |
| 1087 | } |
| 1088 | |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 1089 | LIBCFS_FREE(p, offsetof(struct kib_pages, ibp_pages[npages])); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1090 | } |
| 1091 | |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 1092 | int kiblnd_alloc_pages(struct kib_pages **pp, int cpt, int npages) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1093 | { |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 1094 | struct kib_pages *p; |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 1095 | int i; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1096 | |
| 1097 | LIBCFS_CPT_ALLOC(p, lnet_cpt_table(), cpt, |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 1098 | offsetof(struct kib_pages, ibp_pages[npages])); |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 1099 | if (!p) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1100 | CERROR("Can't allocate descriptor for %d pages\n", npages); |
| 1101 | return -ENOMEM; |
| 1102 | } |
| 1103 | |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 1104 | memset(p, 0, offsetof(struct kib_pages, ibp_pages[npages])); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1105 | p->ibp_npages = npages; |
| 1106 | |
| 1107 | for (i = 0; i < npages; i++) { |
Peng Tao | 49c02a7 | 2013-06-03 21:58:22 +0800 | [diff] [blame] | 1108 | p->ibp_pages[i] = alloc_pages_node( |
| 1109 | cfs_cpt_spread_node(lnet_cpt_table(), cpt), |
Ann Koehler | 0be19af | 2014-04-27 13:06:36 -0400 | [diff] [blame] | 1110 | GFP_NOFS, 0); |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 1111 | if (!p->ibp_pages[i]) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1112 | CERROR("Can't allocate page %d of %d\n", i, npages); |
| 1113 | kiblnd_free_pages(p); |
| 1114 | return -ENOMEM; |
| 1115 | } |
| 1116 | } |
| 1117 | |
| 1118 | *pp = p; |
| 1119 | return 0; |
| 1120 | } |
| 1121 | |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 1122 | void kiblnd_unmap_rx_descs(struct kib_conn *conn) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1123 | { |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 1124 | struct kib_rx *rx; |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 1125 | int i; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1126 | |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 1127 | LASSERT(conn->ibc_rxs); |
| 1128 | LASSERT(conn->ibc_hdev); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1129 | |
Jeremy Filizetti | 2fb44f2 | 2016-03-02 18:53:24 -0500 | [diff] [blame] | 1130 | for (i = 0; i < IBLND_RX_MSGS(conn); i++) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1131 | rx = &conn->ibc_rxs[i]; |
| 1132 | |
Guillaume Matheron | febe73b | 2015-04-02 19:35:45 +0200 | [diff] [blame] | 1133 | LASSERT(rx->rx_nob >= 0); /* not posted */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1134 | |
| 1135 | kiblnd_dma_unmap_single(conn->ibc_hdev->ibh_ibdev, |
| 1136 | KIBLND_UNMAP_ADDR(rx, rx_msgunmap, |
| 1137 | rx->rx_msgaddr), |
| 1138 | IBLND_MSG_SIZE, DMA_FROM_DEVICE); |
| 1139 | } |
| 1140 | |
| 1141 | kiblnd_free_pages(conn->ibc_rx_pages); |
| 1142 | |
| 1143 | conn->ibc_rx_pages = NULL; |
| 1144 | } |
| 1145 | |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 1146 | void kiblnd_map_rx_descs(struct kib_conn *conn) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1147 | { |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 1148 | struct kib_rx *rx; |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 1149 | struct page *pg; |
| 1150 | int pg_off; |
| 1151 | int ipg; |
| 1152 | int i; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1153 | |
Jeremy Filizetti | 2fb44f2 | 2016-03-02 18:53:24 -0500 | [diff] [blame] | 1154 | for (pg_off = ipg = i = 0; i < IBLND_RX_MSGS(conn); i++) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1155 | pg = conn->ibc_rx_pages->ibp_pages[ipg]; |
| 1156 | rx = &conn->ibc_rxs[i]; |
| 1157 | |
| 1158 | rx->rx_conn = conn; |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 1159 | rx->rx_msg = (struct kib_msg *)(((char *)page_address(pg)) + pg_off); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1160 | |
| 1161 | rx->rx_msgaddr = kiblnd_dma_map_single(conn->ibc_hdev->ibh_ibdev, |
Guillaume Matheron | 7a3888a | 2015-04-02 19:52:07 +0200 | [diff] [blame] | 1162 | rx->rx_msg, |
| 1163 | IBLND_MSG_SIZE, |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1164 | DMA_FROM_DEVICE); |
Guillaume Matheron | febe73b | 2015-04-02 19:35:45 +0200 | [diff] [blame] | 1165 | LASSERT(!kiblnd_dma_mapping_error(conn->ibc_hdev->ibh_ibdev, |
James Simmons | c314c31 | 2016-02-12 12:06:01 -0500 | [diff] [blame] | 1166 | rx->rx_msgaddr)); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1167 | KIBLND_UNMAP_ADDR_SET(rx, rx_msgunmap, rx->rx_msgaddr); |
| 1168 | |
Greg Donald | 1d8cb70 | 2014-08-25 20:07:19 -0500 | [diff] [blame] | 1169 | CDEBUG(D_NET, "rx %d: %p %#llx(%#llx)\n", |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1170 | i, rx->rx_msg, rx->rx_msgaddr, |
John L. Hammond | d664d1f | 2015-06-11 15:18:08 -0400 | [diff] [blame] | 1171 | (__u64)(page_to_phys(pg) + pg_off)); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1172 | |
| 1173 | pg_off += IBLND_MSG_SIZE; |
Guillaume Matheron | febe73b | 2015-04-02 19:35:45 +0200 | [diff] [blame] | 1174 | LASSERT(pg_off <= PAGE_SIZE); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1175 | |
| 1176 | if (pg_off == PAGE_SIZE) { |
| 1177 | pg_off = 0; |
| 1178 | ipg++; |
Jeremy Filizetti | 2fb44f2 | 2016-03-02 18:53:24 -0500 | [diff] [blame] | 1179 | LASSERT(ipg <= IBLND_RX_MSG_PAGES(conn)); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1180 | } |
| 1181 | } |
| 1182 | } |
| 1183 | |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 1184 | static void kiblnd_unmap_tx_pool(struct kib_tx_pool *tpo) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1185 | { |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 1186 | struct kib_hca_dev *hdev = tpo->tpo_hdev; |
| 1187 | struct kib_tx *tx; |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 1188 | int i; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1189 | |
James Simmons | 5fd8833 | 2016-02-12 12:06:09 -0500 | [diff] [blame] | 1190 | LASSERT(!tpo->tpo_pool.po_allocated); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1191 | |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 1192 | if (!hdev) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1193 | return; |
| 1194 | |
| 1195 | for (i = 0; i < tpo->tpo_pool.po_size; i++) { |
| 1196 | tx = &tpo->tpo_tx_descs[i]; |
| 1197 | kiblnd_dma_unmap_single(hdev->ibh_ibdev, |
| 1198 | KIBLND_UNMAP_ADDR(tx, tx_msgunmap, |
| 1199 | tx->tx_msgaddr), |
| 1200 | IBLND_MSG_SIZE, DMA_TO_DEVICE); |
| 1201 | } |
| 1202 | |
| 1203 | kiblnd_hdev_decref(hdev); |
| 1204 | tpo->tpo_hdev = NULL; |
| 1205 | } |
| 1206 | |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 1207 | static struct kib_hca_dev *kiblnd_current_hdev(struct kib_dev *dev) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1208 | { |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 1209 | struct kib_hca_dev *hdev; |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 1210 | unsigned long flags; |
| 1211 | int i = 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1212 | |
| 1213 | read_lock_irqsave(&kiblnd_data.kib_global_lock, flags); |
| 1214 | while (dev->ibd_failover) { |
| 1215 | read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags); |
James Simmons | 5fd8833 | 2016-02-12 12:06:09 -0500 | [diff] [blame] | 1216 | if (!(i++ % 50)) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1217 | CDEBUG(D_NET, "%s: Wait for failover\n", |
| 1218 | dev->ibd_ifname); |
Liang Zhen | ea363b4 | 2016-03-02 18:53:30 -0500 | [diff] [blame] | 1219 | set_current_state(TASK_INTERRUPTIBLE); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1220 | schedule_timeout(cfs_time_seconds(1) / 100); |
| 1221 | |
| 1222 | read_lock_irqsave(&kiblnd_data.kib_global_lock, flags); |
| 1223 | } |
| 1224 | |
| 1225 | kiblnd_hdev_addref_locked(dev->ibd_hdev); |
| 1226 | hdev = dev->ibd_hdev; |
| 1227 | |
| 1228 | read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags); |
| 1229 | |
| 1230 | return hdev; |
| 1231 | } |
| 1232 | |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 1233 | static void kiblnd_map_tx_pool(struct kib_tx_pool *tpo) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1234 | { |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 1235 | struct kib_pages *txpgs = tpo->tpo_tx_pages; |
| 1236 | struct kib_pool *pool = &tpo->tpo_pool; |
| 1237 | struct kib_net *net = pool->po_owner->ps_net; |
| 1238 | struct kib_dev *dev; |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 1239 | struct page *page; |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 1240 | struct kib_tx *tx; |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 1241 | int page_offset; |
| 1242 | int ipage; |
| 1243 | int i; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1244 | |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 1245 | LASSERT(net); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1246 | |
| 1247 | dev = net->ibn_dev; |
| 1248 | |
| 1249 | /* pre-mapped messages are not bigger than 1 page */ |
Guillaume Matheron | febe73b | 2015-04-02 19:35:45 +0200 | [diff] [blame] | 1250 | CLASSERT(IBLND_MSG_SIZE <= PAGE_SIZE); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1251 | |
| 1252 | /* No fancy arithmetic when we do the buffer calculations */ |
James Simmons | 5fd8833 | 2016-02-12 12:06:09 -0500 | [diff] [blame] | 1253 | CLASSERT(!(PAGE_SIZE % IBLND_MSG_SIZE)); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1254 | |
| 1255 | tpo->tpo_hdev = kiblnd_current_hdev(dev); |
| 1256 | |
| 1257 | for (ipage = page_offset = i = 0; i < pool->po_size; i++) { |
| 1258 | page = txpgs->ibp_pages[ipage]; |
| 1259 | tx = &tpo->tpo_tx_descs[i]; |
| 1260 | |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 1261 | tx->tx_msg = (struct kib_msg *)(((char *)page_address(page)) + |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1262 | page_offset); |
| 1263 | |
| 1264 | tx->tx_msgaddr = kiblnd_dma_map_single( |
| 1265 | tpo->tpo_hdev->ibh_ibdev, tx->tx_msg, |
| 1266 | IBLND_MSG_SIZE, DMA_TO_DEVICE); |
Guillaume Matheron | febe73b | 2015-04-02 19:35:45 +0200 | [diff] [blame] | 1267 | LASSERT(!kiblnd_dma_mapping_error(tpo->tpo_hdev->ibh_ibdev, |
James Simmons | c314c31 | 2016-02-12 12:06:01 -0500 | [diff] [blame] | 1268 | tx->tx_msgaddr)); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1269 | KIBLND_UNMAP_ADDR_SET(tx, tx_msgunmap, tx->tx_msgaddr); |
| 1270 | |
| 1271 | list_add(&tx->tx_list, &pool->po_free_list); |
| 1272 | |
| 1273 | page_offset += IBLND_MSG_SIZE; |
Guillaume Matheron | febe73b | 2015-04-02 19:35:45 +0200 | [diff] [blame] | 1274 | LASSERT(page_offset <= PAGE_SIZE); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1275 | |
| 1276 | if (page_offset == PAGE_SIZE) { |
| 1277 | page_offset = 0; |
| 1278 | ipage++; |
Guillaume Matheron | febe73b | 2015-04-02 19:35:45 +0200 | [diff] [blame] | 1279 | LASSERT(ipage <= txpgs->ibp_npages); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1280 | } |
| 1281 | } |
| 1282 | } |
| 1283 | |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 1284 | struct ib_mr *kiblnd_find_rd_dma_mr(struct lnet_ni *ni, struct kib_rdma_desc *rd, |
Jeremy Filizetti | 2fb44f2 | 2016-03-02 18:53:24 -0500 | [diff] [blame] | 1285 | int negotiated_nfrags) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1286 | { |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 1287 | struct kib_net *net = ni->ni_data; |
| 1288 | struct kib_hca_dev *hdev = net->ibn_dev->ibd_hdev; |
Amir Shehata | 32c8deb8 | 2016-05-06 21:30:28 -0400 | [diff] [blame] | 1289 | struct lnet_ioctl_config_o2iblnd_tunables *tunables; |
| 1290 | __u16 nfrags; |
| 1291 | int mod; |
| 1292 | |
| 1293 | tunables = &ni->ni_lnd_tunables->lt_tun_u.lt_o2ib; |
| 1294 | mod = tunables->lnd_map_on_demand; |
| 1295 | nfrags = (negotiated_nfrags != -1) ? negotiated_nfrags : mod; |
Jeremy Filizetti | 2fb44f2 | 2016-03-02 18:53:24 -0500 | [diff] [blame] | 1296 | |
Amir Shehata | 7cadcc7 | 2016-03-02 17:02:03 -0500 | [diff] [blame] | 1297 | LASSERT(hdev->ibh_mrs); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1298 | |
Amir Shehata | 32c8deb8 | 2016-05-06 21:30:28 -0400 | [diff] [blame] | 1299 | if (mod > 0 && nfrags <= rd->rd_nfrags) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1300 | return NULL; |
| 1301 | |
Amir Shehata | 7cadcc7 | 2016-03-02 17:02:03 -0500 | [diff] [blame] | 1302 | return hdev->ibh_mrs; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1303 | } |
| 1304 | |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 1305 | static void kiblnd_destroy_fmr_pool(struct kib_fmr_pool *fpo) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1306 | { |
Dmitry Eremin | 8daab0a | 2016-05-05 14:53:00 -0400 | [diff] [blame] | 1307 | LASSERT(!fpo->fpo_map_count); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1308 | |
Dmitry Eremin | 80e05b3 | 2016-05-05 14:53:07 -0400 | [diff] [blame] | 1309 | if (fpo->fpo_is_fmr) { |
| 1310 | if (fpo->fmr.fpo_fmr_pool) |
| 1311 | ib_destroy_fmr_pool(fpo->fmr.fpo_fmr_pool); |
| 1312 | } else { |
| 1313 | struct kib_fast_reg_descriptor *frd, *tmp; |
| 1314 | int i = 0; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1315 | |
Dmitry Eremin | 80e05b3 | 2016-05-05 14:53:07 -0400 | [diff] [blame] | 1316 | list_for_each_entry_safe(frd, tmp, &fpo->fast_reg.fpo_pool_list, |
| 1317 | frd_list) { |
| 1318 | list_del(&frd->frd_list); |
| 1319 | ib_dereg_mr(frd->frd_mr); |
| 1320 | LIBCFS_FREE(frd, sizeof(*frd)); |
| 1321 | i++; |
| 1322 | } |
| 1323 | if (i < fpo->fast_reg.fpo_pool_size) |
| 1324 | CERROR("FastReg pool still has %d regions registered\n", |
| 1325 | fpo->fast_reg.fpo_pool_size - i); |
| 1326 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1327 | |
Dmitry Eremin | 8daab0a | 2016-05-05 14:53:00 -0400 | [diff] [blame] | 1328 | if (fpo->fpo_hdev) |
| 1329 | kiblnd_hdev_decref(fpo->fpo_hdev); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1330 | |
Dmitry Eremin | 8daab0a | 2016-05-05 14:53:00 -0400 | [diff] [blame] | 1331 | LIBCFS_FREE(fpo, sizeof(*fpo)); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1332 | } |
| 1333 | |
Guillaume Matheron | febe73b | 2015-04-02 19:35:45 +0200 | [diff] [blame] | 1334 | static void kiblnd_destroy_fmr_pool_list(struct list_head *head) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1335 | { |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 1336 | struct kib_fmr_pool *fpo, *tmp; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1337 | |
Dmitry Eremin | 0d33ec5 | 2016-05-05 14:53:01 -0400 | [diff] [blame] | 1338 | list_for_each_entry_safe(fpo, tmp, head, fpo_list) { |
Dmitry Eremin | 8daab0a | 2016-05-05 14:53:00 -0400 | [diff] [blame] | 1339 | list_del(&fpo->fpo_list); |
| 1340 | kiblnd_destroy_fmr_pool(fpo); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1341 | } |
| 1342 | } |
| 1343 | |
Amir Shehata | 32c8deb8 | 2016-05-06 21:30:28 -0400 | [diff] [blame] | 1344 | static int |
| 1345 | kiblnd_fmr_pool_size(struct lnet_ioctl_config_o2iblnd_tunables *tunables, |
| 1346 | int ncpts) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1347 | { |
Amir Shehata | 32c8deb8 | 2016-05-06 21:30:28 -0400 | [diff] [blame] | 1348 | int size = tunables->lnd_fmr_pool_size / ncpts; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1349 | |
| 1350 | return max(IBLND_FMR_POOL, size); |
| 1351 | } |
| 1352 | |
Amir Shehata | 32c8deb8 | 2016-05-06 21:30:28 -0400 | [diff] [blame] | 1353 | static int |
| 1354 | kiblnd_fmr_flush_trigger(struct lnet_ioctl_config_o2iblnd_tunables *tunables, |
| 1355 | int ncpts) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1356 | { |
Amir Shehata | 32c8deb8 | 2016-05-06 21:30:28 -0400 | [diff] [blame] | 1357 | int size = tunables->lnd_fmr_flush_trigger / ncpts; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1358 | |
| 1359 | return max(IBLND_FMR_POOL_FLUSH, size); |
| 1360 | } |
| 1361 | |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 1362 | static int kiblnd_alloc_fmr_pool(struct kib_fmr_poolset *fps, struct kib_fmr_pool *fpo) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1363 | { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1364 | struct ib_fmr_pool_param param = { |
James Simmons | 51078e2 | 2016-02-12 12:06:04 -0500 | [diff] [blame] | 1365 | .max_pages_per_fmr = LNET_MAX_PAYLOAD / PAGE_SIZE, |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 1366 | .page_shift = PAGE_SHIFT, |
| 1367 | .access = (IB_ACCESS_LOCAL_WRITE | |
Miguel Bernabeu Diaz | e39f6ef | 2015-08-05 23:44:36 +0200 | [diff] [blame] | 1368 | IB_ACCESS_REMOTE_WRITE), |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 1369 | .pool_size = fps->fps_pool_size, |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1370 | .dirty_watermark = fps->fps_flush_trigger, |
| 1371 | .flush_function = NULL, |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 1372 | .flush_arg = NULL, |
Amir Shehata | 32c8deb8 | 2016-05-06 21:30:28 -0400 | [diff] [blame] | 1373 | .cache = !!fps->fps_cache }; |
Dmitry Eremin | f66fb15 | 2016-05-05 14:53:03 -0400 | [diff] [blame] | 1374 | int rc = 0; |
| 1375 | |
| 1376 | fpo->fmr.fpo_fmr_pool = ib_create_fmr_pool(fpo->fpo_hdev->ibh_pd, |
| 1377 | ¶m); |
| 1378 | if (IS_ERR(fpo->fmr.fpo_fmr_pool)) { |
| 1379 | rc = PTR_ERR(fpo->fmr.fpo_fmr_pool); |
| 1380 | if (rc != -ENOSYS) |
| 1381 | CERROR("Failed to create FMR pool: %d\n", rc); |
| 1382 | else |
| 1383 | CERROR("FMRs are not supported\n"); |
| 1384 | } |
| 1385 | |
| 1386 | return rc; |
| 1387 | } |
| 1388 | |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 1389 | static int kiblnd_alloc_freg_pool(struct kib_fmr_poolset *fps, struct kib_fmr_pool *fpo) |
Dmitry Eremin | 80e05b3 | 2016-05-05 14:53:07 -0400 | [diff] [blame] | 1390 | { |
| 1391 | struct kib_fast_reg_descriptor *frd, *tmp; |
| 1392 | int i, rc; |
| 1393 | |
| 1394 | INIT_LIST_HEAD(&fpo->fast_reg.fpo_pool_list); |
| 1395 | fpo->fast_reg.fpo_pool_size = 0; |
| 1396 | for (i = 0; i < fps->fps_pool_size; i++) { |
| 1397 | LIBCFS_CPT_ALLOC(frd, lnet_cpt_table(), fps->fps_cpt, |
| 1398 | sizeof(*frd)); |
| 1399 | if (!frd) { |
| 1400 | CERROR("Failed to allocate a new fast_reg descriptor\n"); |
| 1401 | rc = -ENOMEM; |
| 1402 | goto out; |
| 1403 | } |
| 1404 | |
| 1405 | frd->frd_mr = ib_alloc_mr(fpo->fpo_hdev->ibh_pd, |
| 1406 | IB_MR_TYPE_MEM_REG, |
| 1407 | LNET_MAX_PAYLOAD / PAGE_SIZE); |
| 1408 | if (IS_ERR(frd->frd_mr)) { |
| 1409 | rc = PTR_ERR(frd->frd_mr); |
| 1410 | CERROR("Failed to allocate ib_alloc_mr: %d\n", rc); |
| 1411 | frd->frd_mr = NULL; |
| 1412 | goto out_middle; |
| 1413 | } |
| 1414 | |
| 1415 | frd->frd_valid = true; |
| 1416 | |
| 1417 | list_add_tail(&frd->frd_list, &fpo->fast_reg.fpo_pool_list); |
| 1418 | fpo->fast_reg.fpo_pool_size++; |
| 1419 | } |
| 1420 | |
| 1421 | return 0; |
| 1422 | |
| 1423 | out_middle: |
| 1424 | if (frd->frd_mr) |
| 1425 | ib_dereg_mr(frd->frd_mr); |
| 1426 | LIBCFS_FREE(frd, sizeof(*frd)); |
| 1427 | |
| 1428 | out: |
| 1429 | list_for_each_entry_safe(frd, tmp, &fpo->fast_reg.fpo_pool_list, |
| 1430 | frd_list) { |
| 1431 | list_del(&frd->frd_list); |
| 1432 | ib_dereg_mr(frd->frd_mr); |
| 1433 | LIBCFS_FREE(frd, sizeof(*frd)); |
| 1434 | } |
| 1435 | |
| 1436 | return rc; |
| 1437 | } |
| 1438 | |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 1439 | static int kiblnd_create_fmr_pool(struct kib_fmr_poolset *fps, |
| 1440 | struct kib_fmr_pool **pp_fpo) |
Dmitry Eremin | f66fb15 | 2016-05-05 14:53:03 -0400 | [diff] [blame] | 1441 | { |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 1442 | struct kib_dev *dev = fps->fps_net->ibn_dev; |
Dmitry Eremin | 80e05b3 | 2016-05-05 14:53:07 -0400 | [diff] [blame] | 1443 | struct ib_device_attr *dev_attr; |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 1444 | struct kib_fmr_pool *fpo; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1445 | int rc; |
| 1446 | |
| 1447 | LIBCFS_CPT_ALLOC(fpo, lnet_cpt_table(), fps->fps_cpt, sizeof(*fpo)); |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 1448 | if (!fpo) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1449 | return -ENOMEM; |
| 1450 | |
| 1451 | fpo->fpo_hdev = kiblnd_current_hdev(dev); |
Dmitry Eremin | 80e05b3 | 2016-05-05 14:53:07 -0400 | [diff] [blame] | 1452 | dev_attr = &fpo->fpo_hdev->ibh_ibdev->attrs; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1453 | |
Dmitry Eremin | 80e05b3 | 2016-05-05 14:53:07 -0400 | [diff] [blame] | 1454 | /* Check for FMR or FastReg support */ |
| 1455 | fpo->fpo_is_fmr = 0; |
Dmitry Eremin | f66fb15 | 2016-05-05 14:53:03 -0400 | [diff] [blame] | 1456 | if (fpo->fpo_hdev->ibh_ibdev->alloc_fmr && |
| 1457 | fpo->fpo_hdev->ibh_ibdev->dealloc_fmr && |
| 1458 | fpo->fpo_hdev->ibh_ibdev->map_phys_fmr && |
| 1459 | fpo->fpo_hdev->ibh_ibdev->unmap_fmr) { |
| 1460 | LCONSOLE_INFO("Using FMR for registration\n"); |
Dmitry Eremin | 80e05b3 | 2016-05-05 14:53:07 -0400 | [diff] [blame] | 1461 | fpo->fpo_is_fmr = 1; |
| 1462 | } else if (dev_attr->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS) { |
| 1463 | LCONSOLE_INFO("Using FastReg for registration\n"); |
Dmitry Eremin | f66fb15 | 2016-05-05 14:53:03 -0400 | [diff] [blame] | 1464 | } else { |
| 1465 | rc = -ENOSYS; |
Dmitry Eremin | 80e05b3 | 2016-05-05 14:53:07 -0400 | [diff] [blame] | 1466 | LCONSOLE_ERROR_MSG(rc, "IB device does not support FMRs nor FastRegs, can't register memory\n"); |
Dmitry Eremin | f66fb15 | 2016-05-05 14:53:03 -0400 | [diff] [blame] | 1467 | goto out_fpo; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1468 | } |
| 1469 | |
Dmitry Eremin | 80e05b3 | 2016-05-05 14:53:07 -0400 | [diff] [blame] | 1470 | if (fpo->fpo_is_fmr) |
| 1471 | rc = kiblnd_alloc_fmr_pool(fps, fpo); |
| 1472 | else |
| 1473 | rc = kiblnd_alloc_freg_pool(fps, fpo); |
Dmitry Eremin | f66fb15 | 2016-05-05 14:53:03 -0400 | [diff] [blame] | 1474 | if (rc) |
| 1475 | goto out_fpo; |
| 1476 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1477 | fpo->fpo_deadline = cfs_time_shift(IBLND_POOL_DEADLINE); |
Dmitry Eremin | f66fb15 | 2016-05-05 14:53:03 -0400 | [diff] [blame] | 1478 | fpo->fpo_owner = fps; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1479 | *pp_fpo = fpo; |
| 1480 | |
| 1481 | return 0; |
Dmitry Eremin | f66fb15 | 2016-05-05 14:53:03 -0400 | [diff] [blame] | 1482 | |
| 1483 | out_fpo: |
| 1484 | kiblnd_hdev_decref(fpo->fpo_hdev); |
| 1485 | LIBCFS_FREE(fpo, sizeof(*fpo)); |
| 1486 | return rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1487 | } |
| 1488 | |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 1489 | static void kiblnd_fail_fmr_poolset(struct kib_fmr_poolset *fps, |
Guillaume Matheron | febe73b | 2015-04-02 19:35:45 +0200 | [diff] [blame] | 1490 | struct list_head *zombies) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1491 | { |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 1492 | if (!fps->fps_net) /* intialized? */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1493 | return; |
| 1494 | |
| 1495 | spin_lock(&fps->fps_lock); |
| 1496 | |
| 1497 | while (!list_empty(&fps->fps_pool_list)) { |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 1498 | struct kib_fmr_pool *fpo = list_entry(fps->fps_pool_list.next, |
| 1499 | struct kib_fmr_pool, fpo_list); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1500 | fpo->fpo_failed = 1; |
| 1501 | list_del(&fpo->fpo_list); |
James Simmons | 5fd8833 | 2016-02-12 12:06:09 -0500 | [diff] [blame] | 1502 | if (!fpo->fpo_map_count) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1503 | list_add(&fpo->fpo_list, zombies); |
| 1504 | else |
| 1505 | list_add(&fpo->fpo_list, &fps->fps_failed_pool_list); |
| 1506 | } |
| 1507 | |
| 1508 | spin_unlock(&fps->fps_lock); |
| 1509 | } |
| 1510 | |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 1511 | static void kiblnd_fini_fmr_poolset(struct kib_fmr_poolset *fps) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1512 | { |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 1513 | if (fps->fps_net) { /* initialized? */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1514 | kiblnd_destroy_fmr_pool_list(&fps->fps_failed_pool_list); |
| 1515 | kiblnd_destroy_fmr_pool_list(&fps->fps_pool_list); |
| 1516 | } |
| 1517 | } |
| 1518 | |
Amir Shehata | 32c8deb8 | 2016-05-06 21:30:28 -0400 | [diff] [blame] | 1519 | static int |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 1520 | kiblnd_init_fmr_poolset(struct kib_fmr_poolset *fps, int cpt, int ncpts, |
| 1521 | struct kib_net *net, |
Amir Shehata | 32c8deb8 | 2016-05-06 21:30:28 -0400 | [diff] [blame] | 1522 | struct lnet_ioctl_config_o2iblnd_tunables *tunables) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1523 | { |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 1524 | struct kib_fmr_pool *fpo; |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 1525 | int rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1526 | |
Janani Ravichandran | a4e872f | 2016-02-10 22:47:33 -0500 | [diff] [blame] | 1527 | memset(fps, 0, sizeof(*fps)); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1528 | |
| 1529 | fps->fps_net = net; |
| 1530 | fps->fps_cpt = cpt; |
Amir Shehata | 32c8deb8 | 2016-05-06 21:30:28 -0400 | [diff] [blame] | 1531 | |
| 1532 | fps->fps_pool_size = kiblnd_fmr_pool_size(tunables, ncpts); |
| 1533 | fps->fps_flush_trigger = kiblnd_fmr_flush_trigger(tunables, ncpts); |
| 1534 | fps->fps_cache = tunables->lnd_fmr_cache; |
| 1535 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1536 | spin_lock_init(&fps->fps_lock); |
| 1537 | INIT_LIST_HEAD(&fps->fps_pool_list); |
| 1538 | INIT_LIST_HEAD(&fps->fps_failed_pool_list); |
| 1539 | |
| 1540 | rc = kiblnd_create_fmr_pool(fps, &fpo); |
James Simmons | 5fd8833 | 2016-02-12 12:06:09 -0500 | [diff] [blame] | 1541 | if (!rc) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1542 | list_add_tail(&fpo->fpo_list, &fps->fps_pool_list); |
| 1543 | |
| 1544 | return rc; |
| 1545 | } |
| 1546 | |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 1547 | static int kiblnd_fmr_pool_is_idle(struct kib_fmr_pool *fpo, unsigned long now) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1548 | { |
James Simmons | 5fd8833 | 2016-02-12 12:06:09 -0500 | [diff] [blame] | 1549 | if (fpo->fpo_map_count) /* still in use */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1550 | return 0; |
| 1551 | if (fpo->fpo_failed) |
| 1552 | return 1; |
| 1553 | return cfs_time_aftereq(now, fpo->fpo_deadline); |
| 1554 | } |
| 1555 | |
Dmitry Eremin | 80e05b3 | 2016-05-05 14:53:07 -0400 | [diff] [blame] | 1556 | static int |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 1557 | kiblnd_map_tx_pages(struct kib_tx *tx, struct kib_rdma_desc *rd) |
Dmitry Eremin | 80e05b3 | 2016-05-05 14:53:07 -0400 | [diff] [blame] | 1558 | { |
| 1559 | __u64 *pages = tx->tx_pages; |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 1560 | struct kib_hca_dev *hdev; |
Dmitry Eremin | 80e05b3 | 2016-05-05 14:53:07 -0400 | [diff] [blame] | 1561 | int npages; |
| 1562 | int size; |
| 1563 | int i; |
| 1564 | |
| 1565 | hdev = tx->tx_pool->tpo_hdev; |
| 1566 | |
| 1567 | for (i = 0, npages = 0; i < rd->rd_nfrags; i++) { |
| 1568 | for (size = 0; size < rd->rd_frags[i].rf_nob; |
| 1569 | size += hdev->ibh_page_size) { |
| 1570 | pages[npages++] = (rd->rd_frags[i].rf_addr & |
| 1571 | hdev->ibh_page_mask) + size; |
| 1572 | } |
| 1573 | } |
| 1574 | |
| 1575 | return npages; |
| 1576 | } |
| 1577 | |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 1578 | void kiblnd_fmr_pool_unmap(struct kib_fmr *fmr, int status) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1579 | { |
Guillaume Matheron | febe73b | 2015-04-02 19:35:45 +0200 | [diff] [blame] | 1580 | LIST_HEAD(zombies); |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 1581 | struct kib_fmr_pool *fpo = fmr->fmr_pool; |
| 1582 | struct kib_fmr_poolset *fps; |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 1583 | unsigned long now = cfs_time_current(); |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 1584 | struct kib_fmr_pool *tmp; |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 1585 | int rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1586 | |
Dmitry Eremin | 1f199a0 | 2016-05-05 14:53:05 -0400 | [diff] [blame] | 1587 | if (!fpo) |
| 1588 | return; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1589 | |
Dmitry Eremin | 1f199a0 | 2016-05-05 14:53:05 -0400 | [diff] [blame] | 1590 | fps = fpo->fpo_owner; |
Dmitry Eremin | 80e05b3 | 2016-05-05 14:53:07 -0400 | [diff] [blame] | 1591 | if (fpo->fpo_is_fmr) { |
| 1592 | if (fmr->fmr_pfmr) { |
| 1593 | rc = ib_fmr_pool_unmap(fmr->fmr_pfmr); |
| 1594 | LASSERT(!rc); |
| 1595 | fmr->fmr_pfmr = NULL; |
| 1596 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1597 | |
Dmitry Eremin | 80e05b3 | 2016-05-05 14:53:07 -0400 | [diff] [blame] | 1598 | if (status) { |
| 1599 | rc = ib_flush_fmr_pool(fpo->fmr.fpo_fmr_pool); |
| 1600 | LASSERT(!rc); |
| 1601 | } |
| 1602 | } else { |
| 1603 | struct kib_fast_reg_descriptor *frd = fmr->fmr_frd; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1604 | |
Dmitry Eremin | 80e05b3 | 2016-05-05 14:53:07 -0400 | [diff] [blame] | 1605 | if (frd) { |
| 1606 | frd->frd_valid = false; |
| 1607 | spin_lock(&fps->fps_lock); |
| 1608 | list_add_tail(&frd->frd_list, &fpo->fast_reg.fpo_pool_list); |
| 1609 | spin_unlock(&fps->fps_lock); |
| 1610 | fmr->fmr_frd = NULL; |
| 1611 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1612 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1613 | fmr->fmr_pool = NULL; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1614 | |
| 1615 | spin_lock(&fps->fps_lock); |
Igor Ishchenko | 74732797 | 2015-01-12 18:16:26 +0200 | [diff] [blame] | 1616 | fpo->fpo_map_count--; /* decref the pool */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1617 | |
| 1618 | list_for_each_entry_safe(fpo, tmp, &fps->fps_pool_list, fpo_list) { |
| 1619 | /* the first pool is persistent */ |
| 1620 | if (fps->fps_pool_list.next == &fpo->fpo_list) |
| 1621 | continue; |
| 1622 | |
| 1623 | if (kiblnd_fmr_pool_is_idle(fpo, now)) { |
| 1624 | list_move(&fpo->fpo_list, &zombies); |
Igor Ishchenko | 74732797 | 2015-01-12 18:16:26 +0200 | [diff] [blame] | 1625 | fps->fps_version++; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1626 | } |
| 1627 | } |
| 1628 | spin_unlock(&fps->fps_lock); |
| 1629 | |
| 1630 | if (!list_empty(&zombies)) |
| 1631 | kiblnd_destroy_fmr_pool_list(&zombies); |
| 1632 | } |
| 1633 | |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 1634 | int kiblnd_fmr_pool_map(struct kib_fmr_poolset *fps, struct kib_tx *tx, |
| 1635 | struct kib_rdma_desc *rd, __u32 nob, __u64 iov, |
| 1636 | struct kib_fmr *fmr) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1637 | { |
Dmitry Eremin | 80e05b3 | 2016-05-05 14:53:07 -0400 | [diff] [blame] | 1638 | __u64 *pages = tx->tx_pages; |
| 1639 | bool is_rx = (rd != tx->tx_rd); |
| 1640 | bool tx_pages_mapped = 0; |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 1641 | struct kib_fmr_pool *fpo; |
Dmitry Eremin | 80e05b3 | 2016-05-05 14:53:07 -0400 | [diff] [blame] | 1642 | int npages = 0; |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 1643 | __u64 version; |
| 1644 | int rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1645 | |
| 1646 | again: |
| 1647 | spin_lock(&fps->fps_lock); |
| 1648 | version = fps->fps_version; |
| 1649 | list_for_each_entry(fpo, &fps->fps_pool_list, fpo_list) { |
| 1650 | fpo->fpo_deadline = cfs_time_shift(IBLND_POOL_DEADLINE); |
| 1651 | fpo->fpo_map_count++; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1652 | |
Dmitry Eremin | 80e05b3 | 2016-05-05 14:53:07 -0400 | [diff] [blame] | 1653 | if (fpo->fpo_is_fmr) { |
| 1654 | struct ib_pool_fmr *pfmr; |
| 1655 | |
| 1656 | spin_unlock(&fps->fps_lock); |
| 1657 | |
| 1658 | if (!tx_pages_mapped) { |
| 1659 | npages = kiblnd_map_tx_pages(tx, rd); |
| 1660 | tx_pages_mapped = 1; |
| 1661 | } |
| 1662 | |
| 1663 | pfmr = ib_fmr_pool_map_phys(fpo->fmr.fpo_fmr_pool, |
| 1664 | pages, npages, iov); |
| 1665 | if (likely(!IS_ERR(pfmr))) { |
| 1666 | fmr->fmr_key = is_rx ? pfmr->fmr->rkey : |
| 1667 | pfmr->fmr->lkey; |
| 1668 | fmr->fmr_frd = NULL; |
| 1669 | fmr->fmr_pfmr = pfmr; |
| 1670 | fmr->fmr_pool = fpo; |
| 1671 | return 0; |
| 1672 | } |
| 1673 | rc = PTR_ERR(pfmr); |
| 1674 | } else { |
| 1675 | if (!list_empty(&fpo->fast_reg.fpo_pool_list)) { |
| 1676 | struct kib_fast_reg_descriptor *frd; |
| 1677 | struct ib_reg_wr *wr; |
| 1678 | struct ib_mr *mr; |
| 1679 | int n; |
| 1680 | |
| 1681 | frd = list_first_entry(&fpo->fast_reg.fpo_pool_list, |
| 1682 | struct kib_fast_reg_descriptor, |
| 1683 | frd_list); |
| 1684 | list_del(&frd->frd_list); |
| 1685 | spin_unlock(&fps->fps_lock); |
| 1686 | |
| 1687 | mr = frd->frd_mr; |
| 1688 | |
| 1689 | if (!frd->frd_valid) { |
| 1690 | __u32 key = is_rx ? mr->rkey : mr->lkey; |
| 1691 | struct ib_send_wr *inv_wr; |
| 1692 | |
| 1693 | inv_wr = &frd->frd_inv_wr; |
| 1694 | memset(inv_wr, 0, sizeof(*inv_wr)); |
| 1695 | inv_wr->opcode = IB_WR_LOCAL_INV; |
| 1696 | inv_wr->wr_id = IBLND_WID_MR; |
| 1697 | inv_wr->ex.invalidate_rkey = key; |
| 1698 | |
| 1699 | /* Bump the key */ |
| 1700 | key = ib_inc_rkey(key); |
| 1701 | ib_update_fast_reg_key(mr, key); |
| 1702 | } |
| 1703 | |
| 1704 | n = ib_map_mr_sg(mr, tx->tx_frags, |
Linus Torvalds | 2f37dd1 | 2016-05-20 22:20:48 -0700 | [diff] [blame] | 1705 | tx->tx_nfrags, NULL, PAGE_SIZE); |
Dmitry Eremin | 80e05b3 | 2016-05-05 14:53:07 -0400 | [diff] [blame] | 1706 | if (unlikely(n != tx->tx_nfrags)) { |
| 1707 | CERROR("Failed to map mr %d/%d elements\n", |
| 1708 | n, tx->tx_nfrags); |
| 1709 | return n < 0 ? n : -EINVAL; |
| 1710 | } |
| 1711 | |
| 1712 | mr->iova = iov; |
| 1713 | |
| 1714 | /* Prepare FastReg WR */ |
| 1715 | wr = &frd->frd_fastreg_wr; |
| 1716 | memset(wr, 0, sizeof(*wr)); |
| 1717 | wr->wr.opcode = IB_WR_REG_MR; |
| 1718 | wr->wr.wr_id = IBLND_WID_MR; |
| 1719 | wr->wr.num_sge = 0; |
| 1720 | wr->wr.send_flags = 0; |
| 1721 | wr->mr = mr; |
| 1722 | wr->key = is_rx ? mr->rkey : mr->lkey; |
| 1723 | wr->access = (IB_ACCESS_LOCAL_WRITE | |
| 1724 | IB_ACCESS_REMOTE_WRITE); |
| 1725 | |
| 1726 | fmr->fmr_key = is_rx ? mr->rkey : mr->lkey; |
| 1727 | fmr->fmr_frd = frd; |
| 1728 | fmr->fmr_pfmr = NULL; |
| 1729 | fmr->fmr_pool = fpo; |
| 1730 | return 0; |
| 1731 | } |
| 1732 | spin_unlock(&fps->fps_lock); |
| 1733 | rc = -EBUSY; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1734 | } |
| 1735 | |
| 1736 | spin_lock(&fps->fps_lock); |
| 1737 | fpo->fpo_map_count--; |
Dmitry Eremin | c1b2e0b | 2016-05-05 14:53:04 -0400 | [diff] [blame] | 1738 | if (rc != -EAGAIN) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1739 | spin_unlock(&fps->fps_lock); |
Dmitry Eremin | c1b2e0b | 2016-05-05 14:53:04 -0400 | [diff] [blame] | 1740 | return rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1741 | } |
| 1742 | |
| 1743 | /* EAGAIN and ... */ |
| 1744 | if (version != fps->fps_version) { |
| 1745 | spin_unlock(&fps->fps_lock); |
| 1746 | goto again; |
| 1747 | } |
| 1748 | } |
| 1749 | |
| 1750 | if (fps->fps_increasing) { |
| 1751 | spin_unlock(&fps->fps_lock); |
James Simmons | c314c31 | 2016-02-12 12:06:01 -0500 | [diff] [blame] | 1752 | CDEBUG(D_NET, "Another thread is allocating new FMR pool, waiting for her to complete\n"); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1753 | schedule(); |
| 1754 | goto again; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1755 | } |
| 1756 | |
Greg Kroah-Hartman | 699503b | 2014-07-12 01:03:41 -0700 | [diff] [blame] | 1757 | if (time_before(cfs_time_current(), fps->fps_next_retry)) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1758 | /* someone failed recently */ |
| 1759 | spin_unlock(&fps->fps_lock); |
| 1760 | return -EAGAIN; |
| 1761 | } |
| 1762 | |
| 1763 | fps->fps_increasing = 1; |
| 1764 | spin_unlock(&fps->fps_lock); |
| 1765 | |
| 1766 | CDEBUG(D_NET, "Allocate new FMR pool\n"); |
| 1767 | rc = kiblnd_create_fmr_pool(fps, &fpo); |
| 1768 | spin_lock(&fps->fps_lock); |
| 1769 | fps->fps_increasing = 0; |
James Simmons | 5fd8833 | 2016-02-12 12:06:09 -0500 | [diff] [blame] | 1770 | if (!rc) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1771 | fps->fps_version++; |
| 1772 | list_add_tail(&fpo->fpo_list, &fps->fps_pool_list); |
| 1773 | } else { |
| 1774 | fps->fps_next_retry = cfs_time_shift(IBLND_POOL_RETRY); |
| 1775 | } |
| 1776 | spin_unlock(&fps->fps_lock); |
| 1777 | |
| 1778 | goto again; |
| 1779 | } |
| 1780 | |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 1781 | static void kiblnd_fini_pool(struct kib_pool *pool) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1782 | { |
Guillaume Matheron | febe73b | 2015-04-02 19:35:45 +0200 | [diff] [blame] | 1783 | LASSERT(list_empty(&pool->po_free_list)); |
James Simmons | 5fd8833 | 2016-02-12 12:06:09 -0500 | [diff] [blame] | 1784 | LASSERT(!pool->po_allocated); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1785 | |
| 1786 | CDEBUG(D_NET, "Finalize %s pool\n", pool->po_owner->ps_name); |
| 1787 | } |
| 1788 | |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 1789 | static void kiblnd_init_pool(struct kib_poolset *ps, struct kib_pool *pool, int size) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1790 | { |
| 1791 | CDEBUG(D_NET, "Initialize %s pool\n", ps->ps_name); |
| 1792 | |
Janani Ravichandran | a4e872f | 2016-02-10 22:47:33 -0500 | [diff] [blame] | 1793 | memset(pool, 0, sizeof(*pool)); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1794 | INIT_LIST_HEAD(&pool->po_free_list); |
| 1795 | pool->po_deadline = cfs_time_shift(IBLND_POOL_DEADLINE); |
| 1796 | pool->po_owner = ps; |
| 1797 | pool->po_size = size; |
| 1798 | } |
| 1799 | |
Guillaume Matheron | febe73b | 2015-04-02 19:35:45 +0200 | [diff] [blame] | 1800 | static void kiblnd_destroy_pool_list(struct list_head *head) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1801 | { |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 1802 | struct kib_pool *pool; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1803 | |
| 1804 | while (!list_empty(head)) { |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 1805 | pool = list_entry(head->next, struct kib_pool, po_list); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1806 | list_del(&pool->po_list); |
| 1807 | |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 1808 | LASSERT(pool->po_owner); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1809 | pool->po_owner->ps_pool_destroy(pool); |
| 1810 | } |
| 1811 | } |
| 1812 | |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 1813 | static void kiblnd_fail_poolset(struct kib_poolset *ps, struct list_head *zombies) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1814 | { |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 1815 | if (!ps->ps_net) /* intialized? */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1816 | return; |
| 1817 | |
| 1818 | spin_lock(&ps->ps_lock); |
| 1819 | while (!list_empty(&ps->ps_pool_list)) { |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 1820 | struct kib_pool *po = list_entry(ps->ps_pool_list.next, |
| 1821 | struct kib_pool, po_list); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1822 | po->po_failed = 1; |
| 1823 | list_del(&po->po_list); |
James Simmons | 5fd8833 | 2016-02-12 12:06:09 -0500 | [diff] [blame] | 1824 | if (!po->po_allocated) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1825 | list_add(&po->po_list, zombies); |
| 1826 | else |
| 1827 | list_add(&po->po_list, &ps->ps_failed_pool_list); |
| 1828 | } |
| 1829 | spin_unlock(&ps->ps_lock); |
| 1830 | } |
| 1831 | |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 1832 | static void kiblnd_fini_poolset(struct kib_poolset *ps) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1833 | { |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 1834 | if (ps->ps_net) { /* initialized? */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1835 | kiblnd_destroy_pool_list(&ps->ps_failed_pool_list); |
| 1836 | kiblnd_destroy_pool_list(&ps->ps_pool_list); |
| 1837 | } |
| 1838 | } |
| 1839 | |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 1840 | static int kiblnd_init_poolset(struct kib_poolset *ps, int cpt, |
| 1841 | struct kib_net *net, char *name, int size, |
Guillaume Matheron | febe73b | 2015-04-02 19:35:45 +0200 | [diff] [blame] | 1842 | kib_ps_pool_create_t po_create, |
| 1843 | kib_ps_pool_destroy_t po_destroy, |
| 1844 | kib_ps_node_init_t nd_init, |
| 1845 | kib_ps_node_fini_t nd_fini) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1846 | { |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 1847 | struct kib_pool *pool; |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 1848 | int rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1849 | |
Janani Ravichandran | a4e872f | 2016-02-10 22:47:33 -0500 | [diff] [blame] | 1850 | memset(ps, 0, sizeof(*ps)); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1851 | |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 1852 | ps->ps_cpt = cpt; |
| 1853 | ps->ps_net = net; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1854 | ps->ps_pool_create = po_create; |
| 1855 | ps->ps_pool_destroy = po_destroy; |
| 1856 | ps->ps_node_init = nd_init; |
| 1857 | ps->ps_node_fini = nd_fini; |
| 1858 | ps->ps_pool_size = size; |
| 1859 | if (strlcpy(ps->ps_name, name, sizeof(ps->ps_name)) |
| 1860 | >= sizeof(ps->ps_name)) |
| 1861 | return -E2BIG; |
| 1862 | spin_lock_init(&ps->ps_lock); |
| 1863 | INIT_LIST_HEAD(&ps->ps_pool_list); |
| 1864 | INIT_LIST_HEAD(&ps->ps_failed_pool_list); |
| 1865 | |
| 1866 | rc = ps->ps_pool_create(ps, size, &pool); |
James Simmons | 5fd8833 | 2016-02-12 12:06:09 -0500 | [diff] [blame] | 1867 | if (!rc) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1868 | list_add(&pool->po_list, &ps->ps_pool_list); |
| 1869 | else |
| 1870 | CERROR("Failed to create the first pool for %s\n", ps->ps_name); |
| 1871 | |
| 1872 | return rc; |
| 1873 | } |
| 1874 | |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 1875 | static int kiblnd_pool_is_idle(struct kib_pool *pool, unsigned long now) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1876 | { |
James Simmons | 5fd8833 | 2016-02-12 12:06:09 -0500 | [diff] [blame] | 1877 | if (pool->po_allocated) /* still in use */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1878 | return 0; |
| 1879 | if (pool->po_failed) |
| 1880 | return 1; |
| 1881 | return cfs_time_aftereq(now, pool->po_deadline); |
| 1882 | } |
| 1883 | |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 1884 | void kiblnd_pool_free_node(struct kib_pool *pool, struct list_head *node) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1885 | { |
Guillaume Matheron | febe73b | 2015-04-02 19:35:45 +0200 | [diff] [blame] | 1886 | LIST_HEAD(zombies); |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 1887 | struct kib_poolset *ps = pool->po_owner; |
| 1888 | struct kib_pool *tmp; |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 1889 | unsigned long now = cfs_time_current(); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1890 | |
| 1891 | spin_lock(&ps->ps_lock); |
| 1892 | |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 1893 | if (ps->ps_node_fini) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1894 | ps->ps_node_fini(pool, node); |
| 1895 | |
Guillaume Matheron | febe73b | 2015-04-02 19:35:45 +0200 | [diff] [blame] | 1896 | LASSERT(pool->po_allocated > 0); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1897 | list_add(node, &pool->po_free_list); |
Igor Ishchenko | 74732797 | 2015-01-12 18:16:26 +0200 | [diff] [blame] | 1898 | pool->po_allocated--; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1899 | |
| 1900 | list_for_each_entry_safe(pool, tmp, &ps->ps_pool_list, po_list) { |
| 1901 | /* the first pool is persistent */ |
| 1902 | if (ps->ps_pool_list.next == &pool->po_list) |
| 1903 | continue; |
| 1904 | |
| 1905 | if (kiblnd_pool_is_idle(pool, now)) |
| 1906 | list_move(&pool->po_list, &zombies); |
| 1907 | } |
| 1908 | spin_unlock(&ps->ps_lock); |
| 1909 | |
| 1910 | if (!list_empty(&zombies)) |
| 1911 | kiblnd_destroy_pool_list(&zombies); |
| 1912 | } |
| 1913 | |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 1914 | struct list_head *kiblnd_pool_alloc_node(struct kib_poolset *ps) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1915 | { |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 1916 | struct list_head *node; |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 1917 | struct kib_pool *pool; |
Liang Zhen | ea363b4 | 2016-03-02 18:53:30 -0500 | [diff] [blame] | 1918 | unsigned int interval = 1; |
| 1919 | unsigned long time_before; |
| 1920 | unsigned int trips = 0; |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 1921 | int rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1922 | |
| 1923 | again: |
| 1924 | spin_lock(&ps->ps_lock); |
| 1925 | list_for_each_entry(pool, &ps->ps_pool_list, po_list) { |
| 1926 | if (list_empty(&pool->po_free_list)) |
| 1927 | continue; |
| 1928 | |
Igor Ishchenko | 74732797 | 2015-01-12 18:16:26 +0200 | [diff] [blame] | 1929 | pool->po_allocated++; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1930 | pool->po_deadline = cfs_time_shift(IBLND_POOL_DEADLINE); |
| 1931 | node = pool->po_free_list.next; |
| 1932 | list_del(node); |
| 1933 | |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 1934 | if (ps->ps_node_init) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1935 | /* still hold the lock */ |
| 1936 | ps->ps_node_init(pool, node); |
| 1937 | } |
| 1938 | spin_unlock(&ps->ps_lock); |
| 1939 | return node; |
| 1940 | } |
| 1941 | |
| 1942 | /* no available tx pool and ... */ |
| 1943 | if (ps->ps_increasing) { |
| 1944 | /* another thread is allocating a new pool */ |
| 1945 | spin_unlock(&ps->ps_lock); |
Liang Zhen | ea363b4 | 2016-03-02 18:53:30 -0500 | [diff] [blame] | 1946 | trips++; |
| 1947 | CDEBUG(D_NET, "Another thread is allocating new %s pool, waiting %d HZs for her to complete. trips = %d\n", |
| 1948 | ps->ps_name, interval, trips); |
| 1949 | |
| 1950 | set_current_state(TASK_INTERRUPTIBLE); |
| 1951 | schedule_timeout(interval); |
| 1952 | if (interval < cfs_time_seconds(1)) |
| 1953 | interval *= 2; |
| 1954 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1955 | goto again; |
| 1956 | } |
| 1957 | |
Greg Kroah-Hartman | 699503b | 2014-07-12 01:03:41 -0700 | [diff] [blame] | 1958 | if (time_before(cfs_time_current(), ps->ps_next_retry)) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1959 | /* someone failed recently */ |
| 1960 | spin_unlock(&ps->ps_lock); |
| 1961 | return NULL; |
| 1962 | } |
| 1963 | |
| 1964 | ps->ps_increasing = 1; |
| 1965 | spin_unlock(&ps->ps_lock); |
| 1966 | |
| 1967 | CDEBUG(D_NET, "%s pool exhausted, allocate new pool\n", ps->ps_name); |
Liang Zhen | ea363b4 | 2016-03-02 18:53:30 -0500 | [diff] [blame] | 1968 | time_before = cfs_time_current(); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1969 | rc = ps->ps_pool_create(ps, ps->ps_pool_size, &pool); |
Liang Zhen | ea363b4 | 2016-03-02 18:53:30 -0500 | [diff] [blame] | 1970 | CDEBUG(D_NET, "ps_pool_create took %lu HZ to complete", |
| 1971 | cfs_time_current() - time_before); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1972 | |
| 1973 | spin_lock(&ps->ps_lock); |
| 1974 | ps->ps_increasing = 0; |
James Simmons | 5fd8833 | 2016-02-12 12:06:09 -0500 | [diff] [blame] | 1975 | if (!rc) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1976 | list_add_tail(&pool->po_list, &ps->ps_pool_list); |
| 1977 | } else { |
| 1978 | ps->ps_next_retry = cfs_time_shift(IBLND_POOL_RETRY); |
| 1979 | CERROR("Can't allocate new %s pool because out of memory\n", |
| 1980 | ps->ps_name); |
| 1981 | } |
| 1982 | spin_unlock(&ps->ps_lock); |
| 1983 | |
| 1984 | goto again; |
| 1985 | } |
| 1986 | |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 1987 | static void kiblnd_destroy_tx_pool(struct kib_pool *pool) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1988 | { |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 1989 | struct kib_tx_pool *tpo = container_of(pool, struct kib_tx_pool, tpo_pool); |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 1990 | int i; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1991 | |
James Simmons | 5fd8833 | 2016-02-12 12:06:09 -0500 | [diff] [blame] | 1992 | LASSERT(!pool->po_allocated); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1993 | |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 1994 | if (tpo->tpo_tx_pages) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 1995 | kiblnd_unmap_tx_pool(tpo); |
| 1996 | kiblnd_free_pages(tpo->tpo_tx_pages); |
| 1997 | } |
| 1998 | |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 1999 | if (!tpo->tpo_tx_descs) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2000 | goto out; |
| 2001 | |
| 2002 | for (i = 0; i < pool->po_size; i++) { |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 2003 | struct kib_tx *tx = &tpo->tpo_tx_descs[i]; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2004 | |
| 2005 | list_del(&tx->tx_list); |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 2006 | if (tx->tx_pages) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2007 | LIBCFS_FREE(tx->tx_pages, |
| 2008 | LNET_MAX_IOV * |
| 2009 | sizeof(*tx->tx_pages)); |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 2010 | if (tx->tx_frags) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2011 | LIBCFS_FREE(tx->tx_frags, |
James Simmons | 147280d | 2016-05-09 10:53:48 -0400 | [diff] [blame] | 2012 | (1 + IBLND_MAX_RDMA_FRAGS) * |
| 2013 | sizeof(*tx->tx_frags)); |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 2014 | if (tx->tx_wrq) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2015 | LIBCFS_FREE(tx->tx_wrq, |
| 2016 | (1 + IBLND_MAX_RDMA_FRAGS) * |
| 2017 | sizeof(*tx->tx_wrq)); |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 2018 | if (tx->tx_sge) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2019 | LIBCFS_FREE(tx->tx_sge, |
| 2020 | (1 + IBLND_MAX_RDMA_FRAGS) * |
| 2021 | sizeof(*tx->tx_sge)); |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 2022 | if (tx->tx_rd) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2023 | LIBCFS_FREE(tx->tx_rd, |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 2024 | offsetof(struct kib_rdma_desc, |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2025 | rd_frags[IBLND_MAX_RDMA_FRAGS])); |
| 2026 | } |
| 2027 | |
| 2028 | LIBCFS_FREE(tpo->tpo_tx_descs, |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 2029 | pool->po_size * sizeof(struct kib_tx)); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2030 | out: |
| 2031 | kiblnd_fini_pool(pool); |
Janani Ravichandran | a4e872f | 2016-02-10 22:47:33 -0500 | [diff] [blame] | 2032 | LIBCFS_FREE(tpo, sizeof(*tpo)); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2033 | } |
| 2034 | |
| 2035 | static int kiblnd_tx_pool_size(int ncpts) |
| 2036 | { |
| 2037 | int ntx = *kiblnd_tunables.kib_ntx / ncpts; |
| 2038 | |
| 2039 | return max(IBLND_TX_POOL, ntx); |
| 2040 | } |
| 2041 | |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 2042 | static int kiblnd_create_tx_pool(struct kib_poolset *ps, int size, |
| 2043 | struct kib_pool **pp_po) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2044 | { |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 2045 | int i; |
| 2046 | int npg; |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 2047 | struct kib_pool *pool; |
| 2048 | struct kib_tx_pool *tpo; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2049 | |
| 2050 | LIBCFS_CPT_ALLOC(tpo, lnet_cpt_table(), ps->ps_cpt, sizeof(*tpo)); |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 2051 | if (!tpo) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2052 | CERROR("Failed to allocate TX pool\n"); |
| 2053 | return -ENOMEM; |
| 2054 | } |
| 2055 | |
| 2056 | pool = &tpo->tpo_pool; |
| 2057 | kiblnd_init_pool(ps, pool, size); |
| 2058 | tpo->tpo_tx_descs = NULL; |
| 2059 | tpo->tpo_tx_pages = NULL; |
| 2060 | |
| 2061 | npg = (size * IBLND_MSG_SIZE + PAGE_SIZE - 1) / PAGE_SIZE; |
James Simmons | 5fd8833 | 2016-02-12 12:06:09 -0500 | [diff] [blame] | 2062 | if (kiblnd_alloc_pages(&tpo->tpo_tx_pages, ps->ps_cpt, npg)) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2063 | CERROR("Can't allocate tx pages: %d\n", npg); |
Janani Ravichandran | a4e872f | 2016-02-10 22:47:33 -0500 | [diff] [blame] | 2064 | LIBCFS_FREE(tpo, sizeof(*tpo)); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2065 | return -ENOMEM; |
| 2066 | } |
| 2067 | |
| 2068 | LIBCFS_CPT_ALLOC(tpo->tpo_tx_descs, lnet_cpt_table(), ps->ps_cpt, |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 2069 | size * sizeof(struct kib_tx)); |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 2070 | if (!tpo->tpo_tx_descs) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2071 | CERROR("Can't allocate %d tx descriptors\n", size); |
| 2072 | ps->ps_pool_destroy(pool); |
| 2073 | return -ENOMEM; |
| 2074 | } |
| 2075 | |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 2076 | memset(tpo->tpo_tx_descs, 0, size * sizeof(struct kib_tx)); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2077 | |
| 2078 | for (i = 0; i < size; i++) { |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 2079 | struct kib_tx *tx = &tpo->tpo_tx_descs[i]; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2080 | |
| 2081 | tx->tx_pool = tpo; |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 2082 | if (ps->ps_net->ibn_fmr_ps) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2083 | LIBCFS_CPT_ALLOC(tx->tx_pages, |
| 2084 | lnet_cpt_table(), ps->ps_cpt, |
| 2085 | LNET_MAX_IOV * sizeof(*tx->tx_pages)); |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 2086 | if (!tx->tx_pages) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2087 | break; |
| 2088 | } |
| 2089 | |
| 2090 | LIBCFS_CPT_ALLOC(tx->tx_frags, lnet_cpt_table(), ps->ps_cpt, |
James Simmons | 147280d | 2016-05-09 10:53:48 -0400 | [diff] [blame] | 2091 | (1 + IBLND_MAX_RDMA_FRAGS) * |
| 2092 | sizeof(*tx->tx_frags)); |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 2093 | if (!tx->tx_frags) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2094 | break; |
| 2095 | |
James Simmons | 147280d | 2016-05-09 10:53:48 -0400 | [diff] [blame] | 2096 | sg_init_table(tx->tx_frags, IBLND_MAX_RDMA_FRAGS + 1); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2097 | |
| 2098 | LIBCFS_CPT_ALLOC(tx->tx_wrq, lnet_cpt_table(), ps->ps_cpt, |
| 2099 | (1 + IBLND_MAX_RDMA_FRAGS) * |
| 2100 | sizeof(*tx->tx_wrq)); |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 2101 | if (!tx->tx_wrq) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2102 | break; |
| 2103 | |
| 2104 | LIBCFS_CPT_ALLOC(tx->tx_sge, lnet_cpt_table(), ps->ps_cpt, |
| 2105 | (1 + IBLND_MAX_RDMA_FRAGS) * |
| 2106 | sizeof(*tx->tx_sge)); |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 2107 | if (!tx->tx_sge) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2108 | break; |
| 2109 | |
| 2110 | LIBCFS_CPT_ALLOC(tx->tx_rd, lnet_cpt_table(), ps->ps_cpt, |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 2111 | offsetof(struct kib_rdma_desc, |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2112 | rd_frags[IBLND_MAX_RDMA_FRAGS])); |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 2113 | if (!tx->tx_rd) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2114 | break; |
| 2115 | } |
| 2116 | |
| 2117 | if (i == size) { |
| 2118 | kiblnd_map_tx_pool(tpo); |
| 2119 | *pp_po = pool; |
| 2120 | return 0; |
| 2121 | } |
| 2122 | |
| 2123 | ps->ps_pool_destroy(pool); |
| 2124 | return -ENOMEM; |
| 2125 | } |
| 2126 | |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 2127 | static void kiblnd_tx_init(struct kib_pool *pool, struct list_head *node) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2128 | { |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 2129 | struct kib_tx_poolset *tps = container_of(pool->po_owner, |
| 2130 | struct kib_tx_poolset, |
| 2131 | tps_poolset); |
| 2132 | struct kib_tx *tx = list_entry(node, struct kib_tx, tx_list); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2133 | |
Igor Ishchenko | 74732797 | 2015-01-12 18:16:26 +0200 | [diff] [blame] | 2134 | tx->tx_cookie = tps->tps_next_tx_cookie++; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2135 | } |
| 2136 | |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 2137 | static void kiblnd_net_fini_pools(struct kib_net *net) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2138 | { |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 2139 | int i; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2140 | |
| 2141 | cfs_cpt_for_each(i, lnet_cpt_table()) { |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 2142 | struct kib_tx_poolset *tps; |
| 2143 | struct kib_fmr_poolset *fps; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2144 | |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 2145 | if (net->ibn_tx_ps) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2146 | tps = net->ibn_tx_ps[i]; |
| 2147 | kiblnd_fini_poolset(&tps->tps_poolset); |
| 2148 | } |
| 2149 | |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 2150 | if (net->ibn_fmr_ps) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2151 | fps = net->ibn_fmr_ps[i]; |
| 2152 | kiblnd_fini_fmr_poolset(fps); |
| 2153 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2154 | } |
| 2155 | |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 2156 | if (net->ibn_tx_ps) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2157 | cfs_percpt_free(net->ibn_tx_ps); |
| 2158 | net->ibn_tx_ps = NULL; |
| 2159 | } |
| 2160 | |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 2161 | if (net->ibn_fmr_ps) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2162 | cfs_percpt_free(net->ibn_fmr_ps); |
| 2163 | net->ibn_fmr_ps = NULL; |
| 2164 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2165 | } |
| 2166 | |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 2167 | static int kiblnd_net_init_pools(struct kib_net *net, lnet_ni_t *ni, __u32 *cpts, |
Amir Shehata | 32c8deb8 | 2016-05-06 21:30:28 -0400 | [diff] [blame] | 2168 | int ncpts) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2169 | { |
Amir Shehata | 32c8deb8 | 2016-05-06 21:30:28 -0400 | [diff] [blame] | 2170 | struct lnet_ioctl_config_o2iblnd_tunables *tunables; |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 2171 | unsigned long flags; |
| 2172 | int cpt; |
Amir Shehata | 32c8deb8 | 2016-05-06 21:30:28 -0400 | [diff] [blame] | 2173 | int rc; |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 2174 | int i; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2175 | |
Amir Shehata | 32c8deb8 | 2016-05-06 21:30:28 -0400 | [diff] [blame] | 2176 | tunables = &ni->ni_lnd_tunables->lt_tun_u.lt_o2ib; |
| 2177 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2178 | read_lock_irqsave(&kiblnd_data.kib_global_lock, flags); |
Amir Shehata | 32c8deb8 | 2016-05-06 21:30:28 -0400 | [diff] [blame] | 2179 | if (!tunables->lnd_map_on_demand) { |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 2180 | read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2181 | goto create_tx_pool; |
| 2182 | } |
| 2183 | |
| 2184 | read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags); |
| 2185 | |
Amir Shehata | 32c8deb8 | 2016-05-06 21:30:28 -0400 | [diff] [blame] | 2186 | if (tunables->lnd_fmr_pool_size < *kiblnd_tunables.kib_ntx / 4) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2187 | CERROR("Can't set fmr pool size (%d) < ntx / 4(%d)\n", |
Amir Shehata | 32c8deb8 | 2016-05-06 21:30:28 -0400 | [diff] [blame] | 2188 | tunables->lnd_fmr_pool_size, |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2189 | *kiblnd_tunables.kib_ntx / 4); |
| 2190 | rc = -EINVAL; |
| 2191 | goto failed; |
| 2192 | } |
| 2193 | |
Oleg Drokin | 415bcb5 | 2015-08-18 21:04:35 -0400 | [diff] [blame] | 2194 | /* |
| 2195 | * TX pool must be created later than FMR, see LU-2268 |
| 2196 | * for details |
| 2197 | */ |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 2198 | LASSERT(!net->ibn_tx_ps); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2199 | |
Oleg Drokin | 415bcb5 | 2015-08-18 21:04:35 -0400 | [diff] [blame] | 2200 | /* |
| 2201 | * premapping can fail if ibd_nmr > 1, so we always create |
| 2202 | * FMR pool and map-on-demand if premapping failed |
James Simmons | 7e221b6 | 2016-03-24 11:24:02 -0400 | [diff] [blame] | 2203 | * |
| 2204 | * cfs_precpt_alloc is creating an array of struct kib_fmr_poolset |
| 2205 | * The number of struct kib_fmr_poolsets create is equal to the |
| 2206 | * number of CPTs that exist, i.e net->ibn_fmr_ps[cpt]. |
Oleg Drokin | 415bcb5 | 2015-08-18 21:04:35 -0400 | [diff] [blame] | 2207 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2208 | net->ibn_fmr_ps = cfs_percpt_alloc(lnet_cpt_table(), |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 2209 | sizeof(struct kib_fmr_poolset)); |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 2210 | if (!net->ibn_fmr_ps) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2211 | CERROR("Failed to allocate FMR pool array\n"); |
| 2212 | rc = -ENOMEM; |
| 2213 | goto failed; |
| 2214 | } |
| 2215 | |
| 2216 | for (i = 0; i < ncpts; i++) { |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 2217 | cpt = !cpts ? i : cpts[i]; |
Amir Shehata | 32c8deb8 | 2016-05-06 21:30:28 -0400 | [diff] [blame] | 2218 | rc = kiblnd_init_fmr_poolset(net->ibn_fmr_ps[cpt], cpt, ncpts, |
| 2219 | net, tunables); |
Amir Shehata | 7cadcc7 | 2016-03-02 17:02:03 -0500 | [diff] [blame] | 2220 | if (rc) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2221 | CERROR("Can't initialize FMR pool for CPT %d: %d\n", |
| 2222 | cpt, rc); |
| 2223 | goto failed; |
| 2224 | } |
| 2225 | } |
| 2226 | |
Amir Shehata | 7cadcc7 | 2016-03-02 17:02:03 -0500 | [diff] [blame] | 2227 | if (i > 0) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2228 | LASSERT(i == ncpts); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2229 | |
| 2230 | create_tx_pool: |
James Simmons | 7e221b6 | 2016-03-24 11:24:02 -0400 | [diff] [blame] | 2231 | /* |
| 2232 | * cfs_precpt_alloc is creating an array of struct kib_tx_poolset |
| 2233 | * The number of struct kib_tx_poolsets create is equal to the |
| 2234 | * number of CPTs that exist, i.e net->ibn_tx_ps[cpt]. |
| 2235 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2236 | net->ibn_tx_ps = cfs_percpt_alloc(lnet_cpt_table(), |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 2237 | sizeof(struct kib_tx_poolset)); |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 2238 | if (!net->ibn_tx_ps) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2239 | CERROR("Failed to allocate tx pool array\n"); |
| 2240 | rc = -ENOMEM; |
| 2241 | goto failed; |
| 2242 | } |
| 2243 | |
| 2244 | for (i = 0; i < ncpts; i++) { |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 2245 | cpt = !cpts ? i : cpts[i]; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2246 | rc = kiblnd_init_poolset(&net->ibn_tx_ps[cpt]->tps_poolset, |
| 2247 | cpt, net, "TX", |
| 2248 | kiblnd_tx_pool_size(ncpts), |
| 2249 | kiblnd_create_tx_pool, |
| 2250 | kiblnd_destroy_tx_pool, |
| 2251 | kiblnd_tx_init, NULL); |
James Simmons | 5fd8833 | 2016-02-12 12:06:09 -0500 | [diff] [blame] | 2252 | if (rc) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2253 | CERROR("Can't initialize TX pool for CPT %d: %d\n", |
| 2254 | cpt, rc); |
| 2255 | goto failed; |
| 2256 | } |
| 2257 | } |
| 2258 | |
| 2259 | return 0; |
| 2260 | failed: |
| 2261 | kiblnd_net_fini_pools(net); |
James Simmons | 5fd8833 | 2016-02-12 12:06:09 -0500 | [diff] [blame] | 2262 | LASSERT(rc); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2263 | return rc; |
| 2264 | } |
| 2265 | |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 2266 | static int kiblnd_hdev_get_attr(struct kib_hca_dev *hdev) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2267 | { |
James Simmons | 4420cfd | 2016-02-12 12:06:00 -0500 | [diff] [blame] | 2268 | /* |
| 2269 | * It's safe to assume a HCA can handle a page size |
| 2270 | * matching that of the native system |
| 2271 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2272 | hdev->ibh_page_shift = PAGE_SHIFT; |
| 2273 | hdev->ibh_page_size = 1 << PAGE_SHIFT; |
| 2274 | hdev->ibh_page_mask = ~((__u64)hdev->ibh_page_size - 1); |
| 2275 | |
Or Gerlitz | cebfe5c | 2015-12-18 10:59:49 +0200 | [diff] [blame] | 2276 | hdev->ibh_mr_size = hdev->ibh_ibdev->attrs.max_mr_size; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2277 | if (hdev->ibh_mr_size == ~0ULL) { |
| 2278 | hdev->ibh_mr_shift = 64; |
| 2279 | return 0; |
| 2280 | } |
| 2281 | |
Greg Kroah-Hartman | 55f5a82 | 2014-07-12 20:26:07 -0700 | [diff] [blame] | 2282 | CERROR("Invalid mr size: %#llx\n", hdev->ibh_mr_size); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2283 | return -EINVAL; |
| 2284 | } |
| 2285 | |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 2286 | static void kiblnd_hdev_cleanup_mrs(struct kib_hca_dev *hdev) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2287 | { |
Amir Shehata | 7cadcc7 | 2016-03-02 17:02:03 -0500 | [diff] [blame] | 2288 | if (!hdev->ibh_mrs) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2289 | return; |
| 2290 | |
Amir Shehata | 7cadcc7 | 2016-03-02 17:02:03 -0500 | [diff] [blame] | 2291 | ib_dereg_mr(hdev->ibh_mrs); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2292 | |
Amir Shehata | 7cadcc7 | 2016-03-02 17:02:03 -0500 | [diff] [blame] | 2293 | hdev->ibh_mrs = NULL; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2294 | } |
| 2295 | |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 2296 | void kiblnd_hdev_destroy(struct kib_hca_dev *hdev) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2297 | { |
| 2298 | kiblnd_hdev_cleanup_mrs(hdev); |
| 2299 | |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 2300 | if (hdev->ibh_pd) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2301 | ib_dealloc_pd(hdev->ibh_pd); |
| 2302 | |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 2303 | if (hdev->ibh_cmid) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2304 | rdma_destroy_id(hdev->ibh_cmid); |
| 2305 | |
| 2306 | LIBCFS_FREE(hdev, sizeof(*hdev)); |
| 2307 | } |
| 2308 | |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 2309 | static int kiblnd_hdev_setup_mrs(struct kib_hca_dev *hdev) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2310 | { |
| 2311 | struct ib_mr *mr; |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 2312 | int rc; |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 2313 | int acflags = IB_ACCESS_LOCAL_WRITE | IB_ACCESS_REMOTE_WRITE; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2314 | |
| 2315 | rc = kiblnd_hdev_get_attr(hdev); |
James Simmons | 5fd8833 | 2016-02-12 12:06:09 -0500 | [diff] [blame] | 2316 | if (rc) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2317 | return rc; |
| 2318 | |
Luis de Bethencourt | 0173844 | 2015-10-21 18:40:40 +0100 | [diff] [blame] | 2319 | mr = ib_get_dma_mr(hdev->ibh_pd, acflags); |
| 2320 | if (IS_ERR(mr)) { |
| 2321 | CERROR("Failed ib_get_dma_mr : %ld\n", PTR_ERR(mr)); |
| 2322 | kiblnd_hdev_cleanup_mrs(hdev); |
| 2323 | return PTR_ERR(mr); |
| 2324 | } |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2325 | |
Amir Shehata | 7cadcc7 | 2016-03-02 17:02:03 -0500 | [diff] [blame] | 2326 | hdev->ibh_mrs = mr; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2327 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2328 | return 0; |
| 2329 | } |
| 2330 | |
Guillaume Matheron | febe73b | 2015-04-02 19:35:45 +0200 | [diff] [blame] | 2331 | /* DUMMY */ |
Guillaume Matheron | 7a3888a | 2015-04-02 19:52:07 +0200 | [diff] [blame] | 2332 | static int kiblnd_dummy_callback(struct rdma_cm_id *cmid, |
| 2333 | struct rdma_cm_event *event) |
Guillaume Matheron | febe73b | 2015-04-02 19:35:45 +0200 | [diff] [blame] | 2334 | { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2335 | return 0; |
| 2336 | } |
| 2337 | |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 2338 | static int kiblnd_dev_need_failover(struct kib_dev *dev) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2339 | { |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 2340 | struct rdma_cm_id *cmid; |
| 2341 | struct sockaddr_in srcaddr; |
| 2342 | struct sockaddr_in dstaddr; |
| 2343 | int rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2344 | |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 2345 | if (!dev->ibd_hdev || /* initializing */ |
| 2346 | !dev->ibd_hdev->ibh_cmid || /* listener is dead */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2347 | *kiblnd_tunables.kib_dev_failover > 1) /* debugging */ |
| 2348 | return 1; |
| 2349 | |
James Simmons | 4420cfd | 2016-02-12 12:06:00 -0500 | [diff] [blame] | 2350 | /* |
| 2351 | * XXX: it's UGLY, but I don't have better way to find |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2352 | * ib-bonding HCA failover because: |
| 2353 | * |
| 2354 | * a. no reliable CM event for HCA failover... |
| 2355 | * b. no OFED API to get ib_device for current net_device... |
| 2356 | * |
| 2357 | * We have only two choices at this point: |
| 2358 | * |
| 2359 | * a. rdma_bind_addr(), it will conflict with listener cmid |
James Simmons | 4420cfd | 2016-02-12 12:06:00 -0500 | [diff] [blame] | 2360 | * b. rdma_resolve_addr() to zero addr |
| 2361 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2362 | cmid = kiblnd_rdma_create_id(kiblnd_dummy_callback, dev, RDMA_PS_TCP, |
| 2363 | IB_QPT_RC); |
| 2364 | if (IS_ERR(cmid)) { |
| 2365 | rc = PTR_ERR(cmid); |
| 2366 | CERROR("Failed to create cmid for failover: %d\n", rc); |
| 2367 | return rc; |
| 2368 | } |
| 2369 | |
| 2370 | memset(&srcaddr, 0, sizeof(srcaddr)); |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 2371 | srcaddr.sin_family = AF_INET; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2372 | srcaddr.sin_addr.s_addr = (__force u32)htonl(dev->ibd_ifip); |
| 2373 | |
| 2374 | memset(&dstaddr, 0, sizeof(dstaddr)); |
| 2375 | dstaddr.sin_family = AF_INET; |
| 2376 | rc = rdma_resolve_addr(cmid, (struct sockaddr *)&srcaddr, |
| 2377 | (struct sockaddr *)&dstaddr, 1); |
James Simmons | 5fd8833 | 2016-02-12 12:06:09 -0500 | [diff] [blame] | 2378 | if (rc || !cmid->device) { |
Peng Tao | 5e8f692 | 2013-07-15 22:27:09 +0800 | [diff] [blame] | 2379 | CERROR("Failed to bind %s:%pI4h to device(%p): %d\n", |
| 2380 | dev->ibd_ifname, &dev->ibd_ifip, |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2381 | cmid->device, rc); |
| 2382 | rdma_destroy_id(cmid); |
| 2383 | return rc; |
| 2384 | } |
| 2385 | |
Liang Zhen | 199a0cc | 2015-09-14 18:41:33 -0400 | [diff] [blame] | 2386 | rc = dev->ibd_hdev->ibh_ibdev != cmid->device; /* true for failover */ |
| 2387 | rdma_destroy_id(cmid); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2388 | |
Liang Zhen | 199a0cc | 2015-09-14 18:41:33 -0400 | [diff] [blame] | 2389 | return rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2390 | } |
| 2391 | |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 2392 | int kiblnd_dev_failover(struct kib_dev *dev) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2393 | { |
Guillaume Matheron | febe73b | 2015-04-02 19:35:45 +0200 | [diff] [blame] | 2394 | LIST_HEAD(zombie_tpo); |
| 2395 | LIST_HEAD(zombie_ppo); |
| 2396 | LIST_HEAD(zombie_fpo); |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 2397 | struct rdma_cm_id *cmid = NULL; |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 2398 | struct kib_hca_dev *hdev = NULL; |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 2399 | struct ib_pd *pd; |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 2400 | struct kib_net *net; |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 2401 | struct sockaddr_in addr; |
| 2402 | unsigned long flags; |
| 2403 | int rc = 0; |
| 2404 | int i; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2405 | |
Guillaume Matheron | febe73b | 2015-04-02 19:35:45 +0200 | [diff] [blame] | 2406 | LASSERT(*kiblnd_tunables.kib_dev_failover > 1 || |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 2407 | dev->ibd_can_failover || !dev->ibd_hdev); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2408 | |
| 2409 | rc = kiblnd_dev_need_failover(dev); |
| 2410 | if (rc <= 0) |
| 2411 | goto out; |
| 2412 | |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 2413 | if (dev->ibd_hdev && |
| 2414 | dev->ibd_hdev->ibh_cmid) { |
James Simmons | 4420cfd | 2016-02-12 12:06:00 -0500 | [diff] [blame] | 2415 | /* |
| 2416 | * XXX it's not good to close old listener at here, |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2417 | * because we can fail to create new listener. |
| 2418 | * But we have to close it now, otherwise rdma_bind_addr |
James Simmons | 4420cfd | 2016-02-12 12:06:00 -0500 | [diff] [blame] | 2419 | * will return EADDRINUSE... How crap! |
| 2420 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2421 | write_lock_irqsave(&kiblnd_data.kib_global_lock, flags); |
| 2422 | |
| 2423 | cmid = dev->ibd_hdev->ibh_cmid; |
James Simmons | 4420cfd | 2016-02-12 12:06:00 -0500 | [diff] [blame] | 2424 | /* |
| 2425 | * make next schedule of kiblnd_dev_need_failover() |
| 2426 | * return 1 for me |
| 2427 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2428 | dev->ibd_hdev->ibh_cmid = NULL; |
| 2429 | write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags); |
| 2430 | |
| 2431 | rdma_destroy_id(cmid); |
| 2432 | } |
| 2433 | |
| 2434 | cmid = kiblnd_rdma_create_id(kiblnd_cm_callback, dev, RDMA_PS_TCP, |
| 2435 | IB_QPT_RC); |
| 2436 | if (IS_ERR(cmid)) { |
| 2437 | rc = PTR_ERR(cmid); |
| 2438 | CERROR("Failed to create cmid for failover: %d\n", rc); |
| 2439 | goto out; |
| 2440 | } |
| 2441 | |
| 2442 | memset(&addr, 0, sizeof(addr)); |
| 2443 | addr.sin_family = AF_INET; |
| 2444 | addr.sin_addr.s_addr = (__force u32)htonl(dev->ibd_ifip); |
| 2445 | addr.sin_port = htons(*kiblnd_tunables.kib_service); |
| 2446 | |
| 2447 | /* Bind to failover device or port */ |
| 2448 | rc = rdma_bind_addr(cmid, (struct sockaddr *)&addr); |
James Simmons | 5fd8833 | 2016-02-12 12:06:09 -0500 | [diff] [blame] | 2449 | if (rc || !cmid->device) { |
Peng Tao | 5e8f692 | 2013-07-15 22:27:09 +0800 | [diff] [blame] | 2450 | CERROR("Failed to bind %s:%pI4h to device(%p): %d\n", |
| 2451 | dev->ibd_ifname, &dev->ibd_ifip, |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2452 | cmid->device, rc); |
| 2453 | rdma_destroy_id(cmid); |
| 2454 | goto out; |
| 2455 | } |
| 2456 | |
| 2457 | LIBCFS_ALLOC(hdev, sizeof(*hdev)); |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 2458 | if (!hdev) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2459 | CERROR("Failed to allocate kib_hca_dev\n"); |
| 2460 | rdma_destroy_id(cmid); |
| 2461 | rc = -ENOMEM; |
| 2462 | goto out; |
| 2463 | } |
| 2464 | |
| 2465 | atomic_set(&hdev->ibh_ref, 1); |
| 2466 | hdev->ibh_dev = dev; |
| 2467 | hdev->ibh_cmid = cmid; |
| 2468 | hdev->ibh_ibdev = cmid->device; |
| 2469 | |
| 2470 | pd = ib_alloc_pd(cmid->device); |
| 2471 | if (IS_ERR(pd)) { |
| 2472 | rc = PTR_ERR(pd); |
| 2473 | CERROR("Can't allocate PD: %d\n", rc); |
| 2474 | goto out; |
| 2475 | } |
| 2476 | |
| 2477 | hdev->ibh_pd = pd; |
| 2478 | |
| 2479 | rc = rdma_listen(cmid, 0); |
James Simmons | 5fd8833 | 2016-02-12 12:06:09 -0500 | [diff] [blame] | 2480 | if (rc) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2481 | CERROR("Can't start new listener: %d\n", rc); |
| 2482 | goto out; |
| 2483 | } |
| 2484 | |
| 2485 | rc = kiblnd_hdev_setup_mrs(hdev); |
James Simmons | 5fd8833 | 2016-02-12 12:06:09 -0500 | [diff] [blame] | 2486 | if (rc) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2487 | CERROR("Can't setup device: %d\n", rc); |
| 2488 | goto out; |
| 2489 | } |
| 2490 | |
| 2491 | write_lock_irqsave(&kiblnd_data.kib_global_lock, flags); |
| 2492 | |
Fabian Frederick | 6d37b17 | 2015-06-10 18:32:21 +0200 | [diff] [blame] | 2493 | swap(dev->ibd_hdev, hdev); /* take over the refcount */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2494 | |
| 2495 | list_for_each_entry(net, &dev->ibd_nets, ibn_list) { |
| 2496 | cfs_cpt_for_each(i, lnet_cpt_table()) { |
| 2497 | kiblnd_fail_poolset(&net->ibn_tx_ps[i]->tps_poolset, |
| 2498 | &zombie_tpo); |
| 2499 | |
Oleg Drokin | 415bcb5 | 2015-08-18 21:04:35 -0400 | [diff] [blame] | 2500 | if (net->ibn_fmr_ps) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2501 | kiblnd_fail_fmr_poolset(net->ibn_fmr_ps[i], |
| 2502 | &zombie_fpo); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2503 | } |
| 2504 | } |
| 2505 | |
| 2506 | write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags); |
| 2507 | out: |
| 2508 | if (!list_empty(&zombie_tpo)) |
| 2509 | kiblnd_destroy_pool_list(&zombie_tpo); |
| 2510 | if (!list_empty(&zombie_ppo)) |
| 2511 | kiblnd_destroy_pool_list(&zombie_ppo); |
| 2512 | if (!list_empty(&zombie_fpo)) |
| 2513 | kiblnd_destroy_fmr_pool_list(&zombie_fpo); |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 2514 | if (hdev) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2515 | kiblnd_hdev_decref(hdev); |
| 2516 | |
James Simmons | 5fd8833 | 2016-02-12 12:06:09 -0500 | [diff] [blame] | 2517 | if (rc) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2518 | dev->ibd_failed_failover++; |
| 2519 | else |
| 2520 | dev->ibd_failed_failover = 0; |
| 2521 | |
| 2522 | return rc; |
| 2523 | } |
| 2524 | |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 2525 | void kiblnd_destroy_dev(struct kib_dev *dev) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2526 | { |
James Simmons | 5fd8833 | 2016-02-12 12:06:09 -0500 | [diff] [blame] | 2527 | LASSERT(!dev->ibd_nnets); |
Guillaume Matheron | febe73b | 2015-04-02 19:35:45 +0200 | [diff] [blame] | 2528 | LASSERT(list_empty(&dev->ibd_nets)); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2529 | |
| 2530 | list_del(&dev->ibd_fail_list); |
| 2531 | list_del(&dev->ibd_list); |
| 2532 | |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 2533 | if (dev->ibd_hdev) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2534 | kiblnd_hdev_decref(dev->ibd_hdev); |
| 2535 | |
| 2536 | LIBCFS_FREE(dev, sizeof(*dev)); |
| 2537 | } |
| 2538 | |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 2539 | static struct kib_dev *kiblnd_create_dev(char *ifname) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2540 | { |
| 2541 | struct net_device *netdev; |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 2542 | struct kib_dev *dev; |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 2543 | __u32 netmask; |
| 2544 | __u32 ip; |
| 2545 | int up; |
| 2546 | int rc; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2547 | |
James Simmons | 1ad6a73 | 2015-06-08 22:27:10 -0400 | [diff] [blame] | 2548 | rc = lnet_ipif_query(ifname, &up, &ip, &netmask); |
James Simmons | 5fd8833 | 2016-02-12 12:06:09 -0500 | [diff] [blame] | 2549 | if (rc) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2550 | CERROR("Can't query IPoIB interface %s: %d\n", |
| 2551 | ifname, rc); |
| 2552 | return NULL; |
| 2553 | } |
| 2554 | |
| 2555 | if (!up) { |
| 2556 | CERROR("Can't query IPoIB interface %s: it's down\n", ifname); |
| 2557 | return NULL; |
| 2558 | } |
| 2559 | |
| 2560 | LIBCFS_ALLOC(dev, sizeof(*dev)); |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 2561 | if (!dev) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2562 | return NULL; |
| 2563 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2564 | netdev = dev_get_by_name(&init_net, ifname); |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 2565 | if (!netdev) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2566 | dev->ibd_can_failover = 0; |
| 2567 | } else { |
| 2568 | dev->ibd_can_failover = !!(netdev->flags & IFF_MASTER); |
| 2569 | dev_put(netdev); |
| 2570 | } |
| 2571 | |
| 2572 | INIT_LIST_HEAD(&dev->ibd_nets); |
| 2573 | INIT_LIST_HEAD(&dev->ibd_list); /* not yet in kib_devs */ |
| 2574 | INIT_LIST_HEAD(&dev->ibd_fail_list); |
| 2575 | dev->ibd_ifip = ip; |
| 2576 | strcpy(&dev->ibd_ifname[0], ifname); |
| 2577 | |
| 2578 | /* initialize the device */ |
| 2579 | rc = kiblnd_dev_failover(dev); |
James Simmons | 5fd8833 | 2016-02-12 12:06:09 -0500 | [diff] [blame] | 2580 | if (rc) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2581 | CERROR("Can't initialize device: %d\n", rc); |
| 2582 | LIBCFS_FREE(dev, sizeof(*dev)); |
| 2583 | return NULL; |
| 2584 | } |
| 2585 | |
James Simmons | c314c31 | 2016-02-12 12:06:01 -0500 | [diff] [blame] | 2586 | list_add_tail(&dev->ibd_list, &kiblnd_data.kib_devs); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2587 | return dev; |
| 2588 | } |
| 2589 | |
Guillaume Matheron | febe73b | 2015-04-02 19:35:45 +0200 | [diff] [blame] | 2590 | static void kiblnd_base_shutdown(void) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2591 | { |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 2592 | struct kib_sched_info *sched; |
| 2593 | int i; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2594 | |
Guillaume Matheron | febe73b | 2015-04-02 19:35:45 +0200 | [diff] [blame] | 2595 | LASSERT(list_empty(&kiblnd_data.kib_devs)); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2596 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2597 | switch (kiblnd_data.kib_init) { |
| 2598 | default: |
| 2599 | LBUG(); |
| 2600 | |
| 2601 | case IBLND_INIT_ALL: |
| 2602 | case IBLND_INIT_DATA: |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 2603 | LASSERT(kiblnd_data.kib_peers); |
Guillaume Matheron | 7a3888a | 2015-04-02 19:52:07 +0200 | [diff] [blame] | 2604 | for (i = 0; i < kiblnd_data.kib_peer_hash_size; i++) |
Guillaume Matheron | febe73b | 2015-04-02 19:35:45 +0200 | [diff] [blame] | 2605 | LASSERT(list_empty(&kiblnd_data.kib_peers[i])); |
Guillaume Matheron | febe73b | 2015-04-02 19:35:45 +0200 | [diff] [blame] | 2606 | LASSERT(list_empty(&kiblnd_data.kib_connd_zombies)); |
| 2607 | LASSERT(list_empty(&kiblnd_data.kib_connd_conns)); |
Liang Zhen | 4d99b25 | 2016-03-02 18:53:29 -0500 | [diff] [blame] | 2608 | LASSERT(list_empty(&kiblnd_data.kib_reconn_list)); |
| 2609 | LASSERT(list_empty(&kiblnd_data.kib_reconn_wait)); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2610 | |
| 2611 | /* flag threads to terminate; wake and wait for them to die */ |
| 2612 | kiblnd_data.kib_shutdown = 1; |
| 2613 | |
James Simmons | 4420cfd | 2016-02-12 12:06:00 -0500 | [diff] [blame] | 2614 | /* |
| 2615 | * NB: we really want to stop scheduler threads net by net |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2616 | * instead of the whole module, this should be improved |
James Simmons | 4420cfd | 2016-02-12 12:06:00 -0500 | [diff] [blame] | 2617 | * with dynamic configuration LNet |
| 2618 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2619 | cfs_percpt_for_each(sched, i, kiblnd_data.kib_scheds) |
| 2620 | wake_up_all(&sched->ibs_waitq); |
| 2621 | |
| 2622 | wake_up_all(&kiblnd_data.kib_connd_waitq); |
| 2623 | wake_up_all(&kiblnd_data.kib_failover_waitq); |
| 2624 | |
| 2625 | i = 2; |
James Simmons | 5fd8833 | 2016-02-12 12:06:09 -0500 | [diff] [blame] | 2626 | while (atomic_read(&kiblnd_data.kib_nthreads)) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2627 | i++; |
Guillaume Matheron | 7a3888a | 2015-04-02 19:52:07 +0200 | [diff] [blame] | 2628 | /* power of 2 ? */ |
| 2629 | CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET, |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2630 | "Waiting for %d threads to terminate\n", |
| 2631 | atomic_read(&kiblnd_data.kib_nthreads)); |
Peng Tao | d3caf4d | 2014-03-18 21:05:56 +0800 | [diff] [blame] | 2632 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 2633 | schedule_timeout(cfs_time_seconds(1)); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2634 | } |
| 2635 | |
| 2636 | /* fall through */ |
| 2637 | |
| 2638 | case IBLND_INIT_NOTHING: |
| 2639 | break; |
| 2640 | } |
| 2641 | |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 2642 | if (kiblnd_data.kib_peers) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2643 | LIBCFS_FREE(kiblnd_data.kib_peers, |
| 2644 | sizeof(struct list_head) * |
| 2645 | kiblnd_data.kib_peer_hash_size); |
| 2646 | } |
| 2647 | |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 2648 | if (kiblnd_data.kib_scheds) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2649 | cfs_percpt_free(kiblnd_data.kib_scheds); |
| 2650 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2651 | kiblnd_data.kib_init = IBLND_INIT_NOTHING; |
| 2652 | module_put(THIS_MODULE); |
| 2653 | } |
| 2654 | |
Frank Zago | 439b4d4 | 2016-03-02 17:02:00 -0500 | [diff] [blame] | 2655 | static void kiblnd_shutdown(lnet_ni_t *ni) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2656 | { |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 2657 | struct kib_net *net = ni->ni_data; |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 2658 | rwlock_t *g_lock = &kiblnd_data.kib_global_lock; |
| 2659 | int i; |
| 2660 | unsigned long flags; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2661 | |
| 2662 | LASSERT(kiblnd_data.kib_init == IBLND_INIT_ALL); |
| 2663 | |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 2664 | if (!net) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2665 | goto out; |
| 2666 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2667 | write_lock_irqsave(g_lock, flags); |
| 2668 | net->ibn_shutdown = 1; |
| 2669 | write_unlock_irqrestore(g_lock, flags); |
| 2670 | |
| 2671 | switch (net->ibn_init) { |
| 2672 | default: |
| 2673 | LBUG(); |
| 2674 | |
| 2675 | case IBLND_INIT_ALL: |
| 2676 | /* nuke all existing peers within this net */ |
| 2677 | kiblnd_del_peer(ni, LNET_NID_ANY); |
| 2678 | |
| 2679 | /* Wait for all peer state to clean up */ |
| 2680 | i = 2; |
James Simmons | 5fd8833 | 2016-02-12 12:06:09 -0500 | [diff] [blame] | 2681 | while (atomic_read(&net->ibn_npeers)) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2682 | i++; |
| 2683 | CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET, /* 2**n? */ |
| 2684 | "%s: waiting for %d peers to disconnect\n", |
| 2685 | libcfs_nid2str(ni->ni_nid), |
| 2686 | atomic_read(&net->ibn_npeers)); |
Peng Tao | d3caf4d | 2014-03-18 21:05:56 +0800 | [diff] [blame] | 2687 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 2688 | schedule_timeout(cfs_time_seconds(1)); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2689 | } |
| 2690 | |
| 2691 | kiblnd_net_fini_pools(net); |
| 2692 | |
| 2693 | write_lock_irqsave(g_lock, flags); |
| 2694 | LASSERT(net->ibn_dev->ibd_nnets > 0); |
| 2695 | net->ibn_dev->ibd_nnets--; |
| 2696 | list_del(&net->ibn_list); |
| 2697 | write_unlock_irqrestore(g_lock, flags); |
| 2698 | |
| 2699 | /* fall through */ |
| 2700 | |
| 2701 | case IBLND_INIT_NOTHING: |
James Simmons | 5fd8833 | 2016-02-12 12:06:09 -0500 | [diff] [blame] | 2702 | LASSERT(!atomic_read(&net->ibn_nconns)); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2703 | |
James Simmons | 5fd8833 | 2016-02-12 12:06:09 -0500 | [diff] [blame] | 2704 | if (net->ibn_dev && !net->ibn_dev->ibd_nnets) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2705 | kiblnd_destroy_dev(net->ibn_dev); |
| 2706 | |
| 2707 | break; |
| 2708 | } |
| 2709 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2710 | net->ibn_init = IBLND_INIT_NOTHING; |
| 2711 | ni->ni_data = NULL; |
| 2712 | |
| 2713 | LIBCFS_FREE(net, sizeof(*net)); |
| 2714 | |
| 2715 | out: |
| 2716 | if (list_empty(&kiblnd_data.kib_devs)) |
| 2717 | kiblnd_base_shutdown(); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2718 | } |
| 2719 | |
Guillaume Matheron | febe73b | 2015-04-02 19:35:45 +0200 | [diff] [blame] | 2720 | static int kiblnd_base_startup(void) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2721 | { |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 2722 | struct kib_sched_info *sched; |
| 2723 | int rc; |
| 2724 | int i; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2725 | |
Guillaume Matheron | febe73b | 2015-04-02 19:35:45 +0200 | [diff] [blame] | 2726 | LASSERT(kiblnd_data.kib_init == IBLND_INIT_NOTHING); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2727 | |
| 2728 | try_module_get(THIS_MODULE); |
Guillaume Matheron | 7a3888a | 2015-04-02 19:52:07 +0200 | [diff] [blame] | 2729 | /* zero pointers, flags etc */ |
| 2730 | memset(&kiblnd_data, 0, sizeof(kiblnd_data)); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2731 | |
| 2732 | rwlock_init(&kiblnd_data.kib_global_lock); |
| 2733 | |
| 2734 | INIT_LIST_HEAD(&kiblnd_data.kib_devs); |
| 2735 | INIT_LIST_HEAD(&kiblnd_data.kib_failed_devs); |
| 2736 | |
| 2737 | kiblnd_data.kib_peer_hash_size = IBLND_PEER_HASH_SIZE; |
| 2738 | LIBCFS_ALLOC(kiblnd_data.kib_peers, |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 2739 | sizeof(struct list_head) * kiblnd_data.kib_peer_hash_size); |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 2740 | if (!kiblnd_data.kib_peers) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2741 | goto failed; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2742 | for (i = 0; i < kiblnd_data.kib_peer_hash_size; i++) |
| 2743 | INIT_LIST_HEAD(&kiblnd_data.kib_peers[i]); |
| 2744 | |
| 2745 | spin_lock_init(&kiblnd_data.kib_connd_lock); |
| 2746 | INIT_LIST_HEAD(&kiblnd_data.kib_connd_conns); |
| 2747 | INIT_LIST_HEAD(&kiblnd_data.kib_connd_zombies); |
Liang Zhen | 4d99b25 | 2016-03-02 18:53:29 -0500 | [diff] [blame] | 2748 | INIT_LIST_HEAD(&kiblnd_data.kib_reconn_list); |
| 2749 | INIT_LIST_HEAD(&kiblnd_data.kib_reconn_wait); |
| 2750 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2751 | init_waitqueue_head(&kiblnd_data.kib_connd_waitq); |
| 2752 | init_waitqueue_head(&kiblnd_data.kib_failover_waitq); |
| 2753 | |
| 2754 | kiblnd_data.kib_scheds = cfs_percpt_alloc(lnet_cpt_table(), |
| 2755 | sizeof(*sched)); |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 2756 | if (!kiblnd_data.kib_scheds) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2757 | goto failed; |
| 2758 | |
| 2759 | cfs_percpt_for_each(sched, i, kiblnd_data.kib_scheds) { |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 2760 | int nthrs; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2761 | |
| 2762 | spin_lock_init(&sched->ibs_lock); |
| 2763 | INIT_LIST_HEAD(&sched->ibs_conns); |
| 2764 | init_waitqueue_head(&sched->ibs_waitq); |
| 2765 | |
| 2766 | nthrs = cfs_cpt_weight(lnet_cpt_table(), i); |
| 2767 | if (*kiblnd_tunables.kib_nscheds > 0) { |
| 2768 | nthrs = min(nthrs, *kiblnd_tunables.kib_nscheds); |
| 2769 | } else { |
James Simmons | 4420cfd | 2016-02-12 12:06:00 -0500 | [diff] [blame] | 2770 | /* |
| 2771 | * max to half of CPUs, another half is reserved for |
| 2772 | * upper layer modules |
| 2773 | */ |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2774 | nthrs = min(max(IBLND_N_SCHED, nthrs >> 1), nthrs); |
| 2775 | } |
| 2776 | |
| 2777 | sched->ibs_nthreads_max = nthrs; |
| 2778 | sched->ibs_cpt = i; |
| 2779 | } |
| 2780 | |
| 2781 | kiblnd_data.kib_error_qpa.qp_state = IB_QPS_ERR; |
| 2782 | |
| 2783 | /* lists/ptrs/locks initialised */ |
| 2784 | kiblnd_data.kib_init = IBLND_INIT_DATA; |
| 2785 | /*****************************************************/ |
| 2786 | |
| 2787 | rc = kiblnd_thread_start(kiblnd_connd, NULL, "kiblnd_connd"); |
James Simmons | 5fd8833 | 2016-02-12 12:06:09 -0500 | [diff] [blame] | 2788 | if (rc) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2789 | CERROR("Can't spawn o2iblnd connd: %d\n", rc); |
| 2790 | goto failed; |
| 2791 | } |
| 2792 | |
James Simmons | 5fd8833 | 2016-02-12 12:06:09 -0500 | [diff] [blame] | 2793 | if (*kiblnd_tunables.kib_dev_failover) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2794 | rc = kiblnd_thread_start(kiblnd_failover_thread, NULL, |
| 2795 | "kiblnd_failover"); |
| 2796 | |
James Simmons | 5fd8833 | 2016-02-12 12:06:09 -0500 | [diff] [blame] | 2797 | if (rc) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2798 | CERROR("Can't spawn o2iblnd failover thread: %d\n", rc); |
| 2799 | goto failed; |
| 2800 | } |
| 2801 | |
| 2802 | /* flag everything initialised */ |
| 2803 | kiblnd_data.kib_init = IBLND_INIT_ALL; |
| 2804 | /*****************************************************/ |
| 2805 | |
| 2806 | return 0; |
| 2807 | |
| 2808 | failed: |
| 2809 | kiblnd_base_shutdown(); |
| 2810 | return -ENETDOWN; |
| 2811 | } |
| 2812 | |
Guillaume Matheron | febe73b | 2015-04-02 19:35:45 +0200 | [diff] [blame] | 2813 | static int kiblnd_start_schedulers(struct kib_sched_info *sched) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2814 | { |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 2815 | int rc = 0; |
| 2816 | int nthrs; |
| 2817 | int i; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2818 | |
James Simmons | 5fd8833 | 2016-02-12 12:06:09 -0500 | [diff] [blame] | 2819 | if (!sched->ibs_nthreads) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2820 | if (*kiblnd_tunables.kib_nscheds > 0) { |
| 2821 | nthrs = sched->ibs_nthreads_max; |
| 2822 | } else { |
| 2823 | nthrs = cfs_cpt_weight(lnet_cpt_table(), |
| 2824 | sched->ibs_cpt); |
| 2825 | nthrs = min(max(IBLND_N_SCHED, nthrs >> 1), nthrs); |
| 2826 | nthrs = min(IBLND_N_SCHED_HIGH, nthrs); |
| 2827 | } |
| 2828 | } else { |
| 2829 | LASSERT(sched->ibs_nthreads <= sched->ibs_nthreads_max); |
| 2830 | /* increase one thread if there is new interface */ |
Haneen Mohammed | b6ee382 | 2015-03-13 20:48:53 +0300 | [diff] [blame] | 2831 | nthrs = sched->ibs_nthreads < sched->ibs_nthreads_max; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2832 | } |
| 2833 | |
| 2834 | for (i = 0; i < nthrs; i++) { |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 2835 | long id; |
| 2836 | char name[20]; |
Guillaume Matheron | 7a3888a | 2015-04-02 19:52:07 +0200 | [diff] [blame] | 2837 | |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2838 | id = KIB_THREAD_ID(sched->ibs_cpt, sched->ibs_nthreads + i); |
| 2839 | snprintf(name, sizeof(name), "kiblnd_sd_%02ld_%02ld", |
| 2840 | KIB_THREAD_CPT(id), KIB_THREAD_TID(id)); |
| 2841 | rc = kiblnd_thread_start(kiblnd_scheduler, (void *)id, name); |
James Simmons | 5fd8833 | 2016-02-12 12:06:09 -0500 | [diff] [blame] | 2842 | if (!rc) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2843 | continue; |
| 2844 | |
| 2845 | CERROR("Can't spawn thread %d for scheduler[%d]: %d\n", |
| 2846 | sched->ibs_cpt, sched->ibs_nthreads + i, rc); |
| 2847 | break; |
| 2848 | } |
| 2849 | |
| 2850 | sched->ibs_nthreads += i; |
| 2851 | return rc; |
| 2852 | } |
| 2853 | |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 2854 | static int kiblnd_dev_start_threads(struct kib_dev *dev, int newdev, __u32 *cpts, |
Guillaume Matheron | 7a3888a | 2015-04-02 19:52:07 +0200 | [diff] [blame] | 2855 | int ncpts) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2856 | { |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 2857 | int cpt; |
| 2858 | int rc; |
| 2859 | int i; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2860 | |
| 2861 | for (i = 0; i < ncpts; i++) { |
| 2862 | struct kib_sched_info *sched; |
| 2863 | |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 2864 | cpt = !cpts ? i : cpts[i]; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2865 | sched = kiblnd_data.kib_scheds[cpt]; |
| 2866 | |
| 2867 | if (!newdev && sched->ibs_nthreads > 0) |
| 2868 | continue; |
| 2869 | |
| 2870 | rc = kiblnd_start_schedulers(kiblnd_data.kib_scheds[cpt]); |
James Simmons | 5fd8833 | 2016-02-12 12:06:09 -0500 | [diff] [blame] | 2871 | if (rc) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2872 | CERROR("Failed to start scheduler threads for %s\n", |
| 2873 | dev->ibd_ifname); |
| 2874 | return rc; |
| 2875 | } |
| 2876 | } |
| 2877 | return 0; |
| 2878 | } |
| 2879 | |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 2880 | static struct kib_dev *kiblnd_dev_search(char *ifname) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2881 | { |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 2882 | struct kib_dev *alias = NULL; |
| 2883 | struct kib_dev *dev; |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 2884 | char *colon; |
| 2885 | char *colon2; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2886 | |
| 2887 | colon = strchr(ifname, ':'); |
| 2888 | list_for_each_entry(dev, &kiblnd_data.kib_devs, ibd_list) { |
James Simmons | 5fd8833 | 2016-02-12 12:06:09 -0500 | [diff] [blame] | 2889 | if (!strcmp(&dev->ibd_ifname[0], ifname)) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2890 | return dev; |
| 2891 | |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 2892 | if (alias) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2893 | continue; |
| 2894 | |
| 2895 | colon2 = strchr(dev->ibd_ifname, ':'); |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 2896 | if (colon) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2897 | *colon = 0; |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 2898 | if (colon2) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2899 | *colon2 = 0; |
| 2900 | |
James Simmons | 5fd8833 | 2016-02-12 12:06:09 -0500 | [diff] [blame] | 2901 | if (!strcmp(&dev->ibd_ifname[0], ifname)) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2902 | alias = dev; |
| 2903 | |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 2904 | if (colon) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2905 | *colon = ':'; |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 2906 | if (colon2) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2907 | *colon2 = ':'; |
| 2908 | } |
| 2909 | return alias; |
| 2910 | } |
| 2911 | |
Frank Zago | 439b4d4 | 2016-03-02 17:02:00 -0500 | [diff] [blame] | 2912 | static int kiblnd_startup(lnet_ni_t *ni) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2913 | { |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 2914 | char *ifname; |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 2915 | struct kib_dev *ibdev = NULL; |
| 2916 | struct kib_net *net; |
Arnd Bergmann | 473c4e0 | 2015-09-27 16:45:13 -0400 | [diff] [blame] | 2917 | struct timespec64 tv; |
Mike Shuey | ec3d17c | 2015-05-19 10:14:36 -0400 | [diff] [blame] | 2918 | unsigned long flags; |
| 2919 | int rc; |
| 2920 | int newdev; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2921 | |
Guillaume Matheron | febe73b | 2015-04-02 19:35:45 +0200 | [diff] [blame] | 2922 | LASSERT(ni->ni_lnd == &the_o2iblnd); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2923 | |
| 2924 | if (kiblnd_data.kib_init == IBLND_INIT_NOTHING) { |
| 2925 | rc = kiblnd_base_startup(); |
James Simmons | 5fd8833 | 2016-02-12 12:06:09 -0500 | [diff] [blame] | 2926 | if (rc) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2927 | return rc; |
| 2928 | } |
| 2929 | |
| 2930 | LIBCFS_ALLOC(net, sizeof(*net)); |
| 2931 | ni->ni_data = net; |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 2932 | if (!net) |
Jiayi Ye | 3247c4e | 2014-10-25 11:40:32 +0800 | [diff] [blame] | 2933 | goto net_failed; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2934 | |
Arnd Bergmann | 473c4e0 | 2015-09-27 16:45:13 -0400 | [diff] [blame] | 2935 | ktime_get_real_ts64(&tv); |
| 2936 | net->ibn_incarnation = tv.tv_sec * USEC_PER_SEC + |
| 2937 | tv.tv_nsec / NSEC_PER_USEC; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2938 | |
Amir Shehata | f6e5006 | 2016-05-06 21:30:27 -0400 | [diff] [blame] | 2939 | rc = kiblnd_tunables_setup(ni); |
Amir Shehata | 025ba82 | 2016-05-06 21:30:26 -0400 | [diff] [blame] | 2940 | if (rc) |
| 2941 | goto net_failed; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2942 | |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 2943 | if (ni->ni_interfaces[0]) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2944 | /* Use the IPoIB interface specified in 'networks=' */ |
| 2945 | |
Guillaume Matheron | febe73b | 2015-04-02 19:35:45 +0200 | [diff] [blame] | 2946 | CLASSERT(LNET_MAX_INTERFACES > 1); |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 2947 | if (ni->ni_interfaces[1]) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2948 | CERROR("Multiple interfaces not supported\n"); |
| 2949 | goto failed; |
| 2950 | } |
| 2951 | |
| 2952 | ifname = ni->ni_interfaces[0]; |
| 2953 | } else { |
| 2954 | ifname = *kiblnd_tunables.kib_default_ipif; |
| 2955 | } |
| 2956 | |
| 2957 | if (strlen(ifname) >= sizeof(ibdev->ibd_ifname)) { |
| 2958 | CERROR("IPoIB interface name too long: %s\n", ifname); |
| 2959 | goto failed; |
| 2960 | } |
| 2961 | |
| 2962 | ibdev = kiblnd_dev_search(ifname); |
| 2963 | |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 2964 | newdev = !ibdev; |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2965 | /* hmm...create kib_dev even for alias */ |
James Simmons | 5fd8833 | 2016-02-12 12:06:09 -0500 | [diff] [blame] | 2966 | if (!ibdev || strcmp(&ibdev->ibd_ifname[0], ifname)) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2967 | ibdev = kiblnd_create_dev(ifname); |
| 2968 | |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 2969 | if (!ibdev) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2970 | goto failed; |
| 2971 | |
| 2972 | net->ibn_dev = ibdev; |
| 2973 | ni->ni_nid = LNET_MKNID(LNET_NIDNET(ni->ni_nid), ibdev->ibd_ifip); |
| 2974 | |
| 2975 | rc = kiblnd_dev_start_threads(ibdev, newdev, |
| 2976 | ni->ni_cpts, ni->ni_ncpts); |
James Simmons | 5fd8833 | 2016-02-12 12:06:09 -0500 | [diff] [blame] | 2977 | if (rc) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2978 | goto failed; |
| 2979 | |
Amir Shehata | 32c8deb8 | 2016-05-06 21:30:28 -0400 | [diff] [blame] | 2980 | rc = kiblnd_net_init_pools(net, ni, ni->ni_cpts, ni->ni_ncpts); |
James Simmons | 5fd8833 | 2016-02-12 12:06:09 -0500 | [diff] [blame] | 2981 | if (rc) { |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2982 | CERROR("Failed to initialize NI pools: %d\n", rc); |
| 2983 | goto failed; |
| 2984 | } |
| 2985 | |
| 2986 | write_lock_irqsave(&kiblnd_data.kib_global_lock, flags); |
| 2987 | ibdev->ibd_nnets++; |
| 2988 | list_add_tail(&net->ibn_list, &ibdev->ibd_nets); |
| 2989 | write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags); |
| 2990 | |
| 2991 | net->ibn_init = IBLND_INIT_ALL; |
| 2992 | |
| 2993 | return 0; |
| 2994 | |
| 2995 | failed: |
James Simmons | 06ace26 | 2016-02-12 12:06:08 -0500 | [diff] [blame] | 2996 | if (!net->ibn_dev && ibdev) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 2997 | kiblnd_destroy_dev(ibdev); |
| 2998 | |
Jiayi Ye | 3247c4e | 2014-10-25 11:40:32 +0800 | [diff] [blame] | 2999 | net_failed: |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3000 | kiblnd_shutdown(ni); |
| 3001 | |
| 3002 | CDEBUG(D_NET, "kiblnd_startup failed\n"); |
| 3003 | return -ENETDOWN; |
| 3004 | } |
| 3005 | |
Frank Zago | 439b4d4 | 2016-03-02 17:02:00 -0500 | [diff] [blame] | 3006 | static lnd_t the_o2iblnd = { |
| 3007 | .lnd_type = O2IBLND, |
| 3008 | .lnd_startup = kiblnd_startup, |
| 3009 | .lnd_shutdown = kiblnd_shutdown, |
| 3010 | .lnd_ctl = kiblnd_ctl, |
| 3011 | .lnd_query = kiblnd_query, |
| 3012 | .lnd_send = kiblnd_send, |
| 3013 | .lnd_recv = kiblnd_recv, |
| 3014 | }; |
| 3015 | |
Andreas Dilger | e0f9411 | 2016-02-26 11:36:05 -0500 | [diff] [blame] | 3016 | static void __exit ko2iblnd_exit(void) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3017 | { |
| 3018 | lnet_unregister_lnd(&the_o2iblnd); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3019 | } |
| 3020 | |
Andreas Dilger | e0f9411 | 2016-02-26 11:36:05 -0500 | [diff] [blame] | 3021 | static int __init ko2iblnd_init(void) |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3022 | { |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 3023 | CLASSERT(sizeof(struct kib_msg) <= IBLND_MSG_SIZE); |
| 3024 | CLASSERT(offsetof(struct kib_msg, |
James Simmons | c314c31 | 2016-02-12 12:06:01 -0500 | [diff] [blame] | 3025 | ibm_u.get.ibgm_rd.rd_frags[IBLND_MAX_RDMA_FRAGS]) |
| 3026 | <= IBLND_MSG_SIZE); |
James Simmons | 8d9de3f | 2016-06-10 16:13:39 -0400 | [diff] [blame] | 3027 | CLASSERT(offsetof(struct kib_msg, |
James Simmons | c314c31 | 2016-02-12 12:06:01 -0500 | [diff] [blame] | 3028 | ibm_u.putack.ibpam_rd.rd_frags[IBLND_MAX_RDMA_FRAGS]) |
| 3029 | <= IBLND_MSG_SIZE); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3030 | |
Amir Shehata | 025ba82 | 2016-05-06 21:30:26 -0400 | [diff] [blame] | 3031 | kiblnd_tunables_init(); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3032 | |
| 3033 | lnet_register_lnd(&the_o2iblnd); |
| 3034 | |
| 3035 | return 0; |
| 3036 | } |
| 3037 | |
James Simmons | a045547 | 2015-11-04 13:40:02 -0500 | [diff] [blame] | 3038 | MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>"); |
Andreas Dilger | 57878e1 | 2016-02-26 11:36:04 -0500 | [diff] [blame] | 3039 | MODULE_DESCRIPTION("OpenIB gen2 LNet Network Driver"); |
James Simmons | 5b0e50b | 2016-02-26 11:36:03 -0500 | [diff] [blame] | 3040 | MODULE_VERSION("2.7.0"); |
Peng Tao | d7e09d0 | 2013-05-02 16:46:55 +0800 | [diff] [blame] | 3041 | MODULE_LICENSE("GPL"); |
| 3042 | |
Andreas Dilger | e0f9411 | 2016-02-26 11:36:05 -0500 | [diff] [blame] | 3043 | module_init(ko2iblnd_init); |
| 3044 | module_exit(ko2iblnd_exit); |