Whamcloud - gitweb
LU-17744 ldiskfs: mballoc stats fixes
[fs/lustre-release.git] / lnet / klnds / o2iblnd / o2iblnd.c
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
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
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  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * lnet/klnds/o2iblnd/o2iblnd.c
32  *
33  * Author: Eric Barton <eric@bartonsoftware.com>
34  */
35
36 #include <asm/page.h>
37 #include <linux/ethtool.h>
38 #include <linux/inetdevice.h>
39
40 #include "o2iblnd.h"
41
42 static const struct lnet_lnd the_o2iblnd;
43
44 struct kib_data kiblnd_data;
45
46 static __u32
47 kiblnd_cksum (void *ptr, int nob)
48 {
49         char  *c  = ptr;
50         __u32  sum = 0;
51
52         while (nob-- > 0)
53                 sum = ((sum << 1) | (sum >> 31)) + *c++;
54
55         /* ensure I don't return 0 (== no checksum) */
56         return (sum == 0) ? 1 : sum;
57 }
58
59 static char *
60 kiblnd_msgtype2str(int type)
61 {
62         switch (type) {
63         case IBLND_MSG_CONNREQ:
64                 return "CONNREQ";
65
66         case IBLND_MSG_CONNACK:
67                 return "CONNACK";
68
69         case IBLND_MSG_NOOP:
70                 return "NOOP";
71
72         case IBLND_MSG_IMMEDIATE:
73                 return "IMMEDIATE";
74
75         case IBLND_MSG_PUT_REQ:
76                 return "PUT_REQ";
77
78         case IBLND_MSG_PUT_NAK:
79                 return "PUT_NAK";
80
81         case IBLND_MSG_PUT_ACK:
82                 return "PUT_ACK";
83
84         case IBLND_MSG_PUT_DONE:
85                 return "PUT_DONE";
86
87         case IBLND_MSG_GET_REQ:
88                 return "GET_REQ";
89
90         case IBLND_MSG_GET_DONE:
91                 return "GET_DONE";
92
93         default:
94                 return "???";
95         }
96 }
97
98 static int
99 kiblnd_msgtype2size(int type)
100 {
101         const int hdr_size = offsetof(struct kib_msg, ibm_u);
102
103         switch (type) {
104         case IBLND_MSG_CONNREQ:
105         case IBLND_MSG_CONNACK:
106                 return hdr_size + sizeof(struct kib_connparams);
107
108         case IBLND_MSG_NOOP:
109                 return hdr_size;
110
111         case IBLND_MSG_IMMEDIATE:
112                 return offsetof(struct kib_msg, ibm_u.immediate.ibim_payload[0]);
113
114         case IBLND_MSG_PUT_REQ:
115                 return hdr_size + sizeof(struct kib_putreq_msg);
116
117         case IBLND_MSG_PUT_ACK:
118                 return hdr_size + sizeof(struct kib_putack_msg);
119
120         case IBLND_MSG_GET_REQ:
121                 return hdr_size + sizeof(struct kib_get_msg);
122
123         case IBLND_MSG_PUT_NAK:
124         case IBLND_MSG_PUT_DONE:
125         case IBLND_MSG_GET_DONE:
126                 return hdr_size + sizeof(struct kib_completion_msg);
127         default:
128                 return -1;
129         }
130 }
131
132 static int kiblnd_unpack_rd(struct kib_msg *msg, bool flip)
133 {
134         struct kib_rdma_desc *rd;
135         int nob;
136         int n;
137         int i;
138
139         LASSERT(msg->ibm_type == IBLND_MSG_GET_REQ ||
140                 msg->ibm_type == IBLND_MSG_PUT_ACK);
141
142         rd = msg->ibm_type == IBLND_MSG_GET_REQ ?
143                 &msg->ibm_u.get.ibgm_rd :
144                 &msg->ibm_u.putack.ibpam_rd;
145
146         if (flip) {
147                 __swab32s(&rd->rd_key);
148                 __swab32s(&rd->rd_nfrags);
149         }
150
151         n = rd->rd_nfrags;
152
153         if (n <= 0 || n > IBLND_MAX_RDMA_FRAGS) {
154                 CERROR("Bad nfrags: %d, should be 0 < n <= %d\n",
155                        n, IBLND_MAX_RDMA_FRAGS);
156                 return 1;
157         }
158
159         nob = offsetof(struct kib_msg, ibm_u) +
160                 kiblnd_rd_msg_size(rd, msg->ibm_type, n);
161
162         if (msg->ibm_nob < nob) {
163                 CERROR("Short %s: %d(%d)\n",
164                        kiblnd_msgtype2str(msg->ibm_type), msg->ibm_nob, nob);
165                 return 1;
166         }
167
168         if (!flip)
169                 return 0;
170
171         for (i = 0; i < n; i++) {
172                 __swab32s(&rd->rd_frags[i].rf_nob);
173                 __swab64s(&rd->rd_frags[i].rf_addr);
174         }
175
176         return 0;
177 }
178
179 void kiblnd_pack_msg(struct lnet_ni *ni, struct kib_msg *msg, int version,
180                      int credits, lnet_nid_t dstnid, __u64 dststamp)
181 {
182         struct kib_net *net = ni->ni_data;
183
184         /* CAVEAT EMPTOR! all message fields not set here should have been
185          * initialised previously.
186          */
187         msg->ibm_magic    = IBLND_MSG_MAGIC;
188         msg->ibm_version  = version;
189         /*   ibm_type */
190         msg->ibm_credits  = credits;
191         /*   ibm_nob */
192         msg->ibm_cksum    = 0;
193         msg->ibm_srcnid   = lnet_nid_to_nid4(&ni->ni_nid);
194         msg->ibm_srcstamp = net->ibn_incarnation;
195         msg->ibm_dstnid   = dstnid;
196         msg->ibm_dststamp = dststamp;
197
198         if (*kiblnd_tunables.kib_cksum) {
199                 /* NB ibm_cksum zero while computing cksum */
200                 msg->ibm_cksum = kiblnd_cksum(msg, msg->ibm_nob);
201         }
202 }
203
204 int kiblnd_unpack_msg(struct kib_msg *msg, int nob)
205 {
206         const int hdr_size = offsetof(struct kib_msg, ibm_u);
207         __u32 msg_cksum;
208         __u16 version;
209         int msg_nob;
210         bool flip;
211
212         /* 6 bytes are enough to have received magic + version */
213         if (nob < 6) {
214                 CERROR("Short message: %d\n", nob);
215                 return -EPROTO;
216         }
217
218         if (msg->ibm_magic == IBLND_MSG_MAGIC) {
219                 flip = false;
220         } else if (msg->ibm_magic == __swab32(IBLND_MSG_MAGIC)) {
221                 flip = true;
222         } else {
223                 CERROR("Bad magic: %08x\n", msg->ibm_magic);
224                 return -EPROTO;
225         }
226
227         version = flip ? __swab16(msg->ibm_version) : msg->ibm_version;
228         if (version != IBLND_MSG_VERSION &&
229             version != IBLND_MSG_VERSION_1) {
230                 CERROR("Bad version: %x\n", version);
231                 return -EPROTO;
232         }
233
234         if (nob < hdr_size) {
235                 CERROR("Short message: %d\n", nob);
236                 return -EPROTO;
237         }
238
239         msg_nob = flip ? __swab32(msg->ibm_nob) : msg->ibm_nob;
240         if (msg_nob > nob) {
241                 CERROR("Short message: got %d, wanted %d\n", nob, msg_nob);
242                 return -EPROTO;
243         }
244
245         /* checksum must be computed with ibm_cksum zero and BEFORE anything
246          * gets flipped
247          */
248         msg_cksum = flip ? __swab32(msg->ibm_cksum) : msg->ibm_cksum;
249         msg->ibm_cksum = 0;
250         if (msg_cksum != 0 &&
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_ni endianness */
260                 msg->ibm_version = version;
261                 BUILD_BUG_ON(sizeof(msg->ibm_type) != 1);
262                 BUILD_BUG_ON(sizeof(msg->ibm_credits) != 1);
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
316 int
317 kiblnd_create_peer(struct lnet_ni *ni, struct kib_peer_ni **peerp,
318                    lnet_nid_t nid)
319 {
320         struct kib_peer_ni *peer_ni;
321         struct kib_net *net = ni->ni_data;
322         int cpt = lnet_cpt_of_nid(nid, ni);
323         unsigned long flags;
324
325         LASSERT(net != NULL);
326         LASSERT(nid != LNET_NID_ANY);
327
328         LIBCFS_CPT_ALLOC(peer_ni, lnet_cpt_table(), cpt, sizeof(*peer_ni));
329         if (!peer_ni) {
330                 CERROR("Cannot allocate peer_ni\n");
331                 return -ENOMEM;
332         }
333
334         peer_ni->ibp_ni = ni;
335         peer_ni->ibp_nid = nid;
336         peer_ni->ibp_error = 0;
337         peer_ni->ibp_last_alive = 0;
338         peer_ni->ibp_max_frags = IBLND_MAX_RDMA_FRAGS;
339         peer_ni->ibp_queue_depth = ni->ni_net->net_tunables.lct_peer_tx_credits;
340         peer_ni->ibp_queue_depth_mod = 0;       /* try to use the default */
341         kref_init(&peer_ni->ibp_kref);
342         atomic_set(&peer_ni->ibp_nconns, 0);
343
344         INIT_HLIST_NODE(&peer_ni->ibp_list);
345         INIT_LIST_HEAD(&peer_ni->ibp_conns);
346         INIT_LIST_HEAD(&peer_ni->ibp_tx_queue);
347
348         write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
349
350         /* always called with a ref on ni, which prevents ni being shutdown */
351         LASSERT(net->ibn_shutdown == 0);
352
353         /* npeers only grows with the global lock held */
354         atomic_inc(&net->ibn_npeers);
355
356         write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
357
358         *peerp = peer_ni;
359         return 0;
360 }
361
362 void
363 kiblnd_destroy_peer(struct kref *kref)
364 {
365         struct kib_peer_ni *peer_ni = container_of(kref, struct kib_peer_ni,
366                                                    ibp_kref);
367         struct kib_net *net = peer_ni->ibp_ni->ni_data;
368
369         LASSERT(net != NULL);
370         LASSERT(!kiblnd_peer_active(peer_ni));
371         LASSERT(kiblnd_peer_idle(peer_ni));
372         LASSERT(list_empty(&peer_ni->ibp_tx_queue));
373
374         LIBCFS_FREE(peer_ni, sizeof(*peer_ni));
375
376         /* NB a peer_ni's connections keep a reference on their peer_ni until
377          * they are destroyed, so we can be assured that _all_ state to do
378          * with this peer_ni has been cleaned up when its refcount drops to
379          * zero.
380          */
381         if (atomic_dec_and_test(&net->ibn_npeers))
382                 wake_up_var(&net->ibn_npeers);
383 }
384
385 struct kib_peer_ni *
386 kiblnd_find_peer_locked(struct lnet_ni *ni, lnet_nid_t nid)
387 {
388         /* the caller is responsible for accounting the additional reference
389          * that this creates
390          */
391         struct kib_peer_ni *peer_ni;
392
393         hash_for_each_possible(kiblnd_data.kib_peers, peer_ni,
394                                ibp_list, nid) {
395                 LASSERT(!kiblnd_peer_idle(peer_ni));
396
397                 /*
398                  * Match a peer if its NID and the NID of the local NI it
399                  * communicates over are the same. Otherwise don't match
400                  * the peer, which will result in a new lnd peer being
401                  * created.
402                  */
403                 if (peer_ni->ibp_nid != nid ||
404                     !nid_same(&peer_ni->ibp_ni->ni_nid, &ni->ni_nid))
405                         continue;
406
407                 CDEBUG(D_NET, "got peer_ni [%p] -> %s (%d) version: %x\n",
408                        peer_ni, libcfs_nid2str(nid),
409                        kref_read(&peer_ni->ibp_kref),
410                        peer_ni->ibp_version);
411                 return peer_ni;
412         }
413         return NULL;
414 }
415
416 void
417 kiblnd_unlink_peer_locked(struct kib_peer_ni *peer_ni)
418 {
419         LASSERT(list_empty(&peer_ni->ibp_conns));
420
421         LASSERT(kiblnd_peer_active(peer_ni));
422         hlist_del_init(&peer_ni->ibp_list);
423         /* lose peerlist's ref */
424         kiblnd_peer_decref(peer_ni);
425 }
426
427
428 static void
429 kiblnd_debug_rx(struct kib_rx *rx)
430 {
431         CDEBUG(D_CONSOLE, "      %p msg_type %x cred %d\n",
432                rx, rx->rx_msg->ibm_type,
433                rx->rx_msg->ibm_credits);
434 }
435
436 static void
437 kiblnd_debug_tx(struct kib_tx *tx)
438 {
439         CDEBUG(D_CONSOLE, "      %p snd %d q %d w %d rc %d dl %lld "
440                "cookie %#llx msg %s%s type %x cred %d\n",
441                tx, tx->tx_sending, tx->tx_queued, tx->tx_waiting,
442                tx->tx_status, ktime_to_ns(tx->tx_deadline), tx->tx_cookie,
443                tx->tx_lntmsg[0] == NULL ? "-" : "!",
444                tx->tx_lntmsg[1] == NULL ? "-" : "!",
445                tx->tx_msg->ibm_type, tx->tx_msg->ibm_credits);
446 }
447
448 static void
449 kiblnd_debug_conn(struct kib_conn *conn)
450 {
451         struct list_head        *tmp;
452         int                     i;
453
454         spin_lock(&conn->ibc_lock);
455
456         CDEBUG(D_CONSOLE, "conn[%d] %p [version %x] -> %s:\n",
457                atomic_read(&conn->ibc_refcount), conn,
458                conn->ibc_version, libcfs_nid2str(conn->ibc_peer->ibp_nid));
459         CDEBUG(D_CONSOLE, "   state %d nposted %d/%d cred %d o_cred %d "
460                " r_cred %d\n", conn->ibc_state, conn->ibc_noops_posted,
461                conn->ibc_nsends_posted, conn->ibc_credits,
462                conn->ibc_outstanding_credits, conn->ibc_reserved_credits);
463         CDEBUG(D_CONSOLE, "   comms_err %d\n", conn->ibc_comms_error);
464
465         CDEBUG(D_CONSOLE, "   early_rxs:\n");
466         list_for_each(tmp, &conn->ibc_early_rxs)
467                 kiblnd_debug_rx(list_entry(tmp, struct kib_rx, rx_list));
468
469         CDEBUG(D_CONSOLE, "   tx_noops:\n");
470         list_for_each(tmp, &conn->ibc_tx_noops)
471                 kiblnd_debug_tx(list_entry(tmp, struct kib_tx, tx_list));
472
473         CDEBUG(D_CONSOLE, "   tx_queue_nocred:\n");
474         list_for_each(tmp, &conn->ibc_tx_queue_nocred)
475                 kiblnd_debug_tx(list_entry(tmp, struct kib_tx, tx_list));
476
477         CDEBUG(D_CONSOLE, "   tx_queue_rsrvd:\n");
478         list_for_each(tmp, &conn->ibc_tx_queue_rsrvd)
479                 kiblnd_debug_tx(list_entry(tmp, struct kib_tx, tx_list));
480
481         CDEBUG(D_CONSOLE, "   tx_queue:\n");
482         list_for_each(tmp, &conn->ibc_tx_queue)
483                 kiblnd_debug_tx(list_entry(tmp, struct kib_tx, tx_list));
484
485         CDEBUG(D_CONSOLE, "   active_txs:\n");
486         list_for_each(tmp, &conn->ibc_active_txs)
487                 kiblnd_debug_tx(list_entry(tmp, struct kib_tx, tx_list));
488
489         CDEBUG(D_CONSOLE, "   rxs:\n");
490         for (i = 0; i < IBLND_RX_MSGS(conn); i++)
491                 kiblnd_debug_rx(&conn->ibc_rxs[i]);
492
493         spin_unlock(&conn->ibc_lock);
494 }
495
496 static void
497 kiblnd_dump_peer_debug_info(struct kib_peer_ni *peer_ni)
498 {
499         struct kib_conn *conn;
500         struct kib_conn *cnxt;
501         int count = 0;
502
503         CDEBUG(D_CONSOLE, "[last_alive, races, reconnected, error]: %lld, %d, %d, %d\n",
504                peer_ni->ibp_last_alive,
505                peer_ni->ibp_races,
506                peer_ni->ibp_reconnected,
507                peer_ni->ibp_error);
508         list_for_each_entry_safe(conn, cnxt, &peer_ni->ibp_conns,
509                                  ibc_list) {
510                 CDEBUG(D_CONSOLE, "Conn %d:\n", count);
511                 kiblnd_debug_conn(conn);
512                 count++;
513         }
514 }
515
516
517 static int
518 kiblnd_get_peer_info(struct lnet_ni *ni, lnet_nid_t nid, int index,
519                      lnet_nid_t *nidp, int *count)
520 {
521         struct kib_peer_ni              *peer_ni;
522         int                      i;
523         unsigned long            flags;
524
525         read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
526
527         hash_for_each(kiblnd_data.kib_peers, i, peer_ni, ibp_list) {
528                 LASSERT(!kiblnd_peer_idle(peer_ni));
529
530                 if (peer_ni->ibp_ni != ni)
531                         continue;
532
533                 if (peer_ni->ibp_nid == nid)
534                         kiblnd_dump_peer_debug_info(peer_ni);
535
536                 if (index-- > 0)
537                         continue;
538
539                 *nidp = peer_ni->ibp_nid;
540                 *count = kref_read(&peer_ni->ibp_kref);
541
542                 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
543                 return 0;
544         }
545
546         read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
547         return -ENOENT;
548 }
549
550 static void
551 kiblnd_del_peer_locked(struct kib_peer_ni *peer_ni)
552 {
553         struct kib_conn *cnxt;
554         struct kib_conn *conn;
555
556         if (list_empty(&peer_ni->ibp_conns)) {
557                 kiblnd_unlink_peer_locked(peer_ni);
558         } else {
559                 list_for_each_entry_safe(conn, cnxt, &peer_ni->ibp_conns,
560                                          ibc_list)
561                         kiblnd_close_conn_locked(conn, 0);
562                 /* NB closing peer_ni's last conn unlinked it. */
563         }
564         /* NB peer_ni now unlinked; might even be freed if the peer_ni table had the
565          * last ref on it. */
566 }
567
568 static int
569 kiblnd_del_peer(struct lnet_ni *ni, lnet_nid_t nid)
570 {
571         LIST_HEAD(zombies);
572         struct hlist_node *pnxt;
573         struct kib_peer_ni *peer_ni;
574         int lo;
575         int hi;
576         int i;
577         unsigned long flags;
578         int rc = -ENOENT;
579
580         write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
581
582         if (nid != LNET_NID_ANY) {
583                 lo = hash_min(nid, HASH_BITS(kiblnd_data.kib_peers));
584                 hi = lo;
585         } else {
586                 lo = 0;
587                 hi = HASH_SIZE(kiblnd_data.kib_peers) - 1;
588         }
589
590         for (i = lo; i <= hi; i++) {
591                 hlist_for_each_entry_safe(peer_ni, pnxt,
592                                           &kiblnd_data.kib_peers[i], ibp_list) {
593                         LASSERT(!kiblnd_peer_idle(peer_ni));
594
595                         if (peer_ni->ibp_ni != ni)
596                                 continue;
597
598                         if (!(nid == LNET_NID_ANY || peer_ni->ibp_nid == nid))
599                                 continue;
600
601                         if (!list_empty(&peer_ni->ibp_tx_queue)) {
602                                 LASSERT(list_empty(&peer_ni->ibp_conns));
603
604                                 list_splice_init(&peer_ni->ibp_tx_queue,
605                                                  &zombies);
606                         }
607
608                         kiblnd_del_peer_locked(peer_ni);
609                         rc = 0;         /* matched something */
610                 }
611         }
612
613         write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
614
615         kiblnd_txlist_done(&zombies, -EIO, LNET_MSG_STATUS_LOCAL_ERROR);
616
617         return rc;
618 }
619
620 static struct kib_conn *
621 kiblnd_get_conn_by_idx(struct lnet_ni *ni, int index)
622 {
623         struct kib_peer_ni *peer_ni;
624         struct kib_conn *conn;
625         int i;
626         unsigned long flags;
627
628         read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
629
630         hash_for_each(kiblnd_data.kib_peers, i, peer_ni, ibp_list) {
631                 LASSERT(!kiblnd_peer_idle(peer_ni));
632
633                 if (peer_ni->ibp_ni != ni)
634                         continue;
635
636                 list_for_each_entry(conn, &peer_ni->ibp_conns,
637                                     ibc_list) {
638                         if (index-- > 0)
639                                 continue;
640
641                         kiblnd_conn_addref(conn);
642                         read_unlock_irqrestore(&kiblnd_data.kib_global_lock,
643                                                flags);
644                         return conn;
645                 }
646         }
647
648         read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
649         return NULL;
650 }
651
652 static void
653 kiblnd_setup_mtu_locked(struct rdma_cm_id *cmid)
654 {
655         /* XXX There is no path record for iWARP, set by netdev->change_mtu? */
656         if (cmid->route.path_rec == NULL)
657                 return;
658
659         if (*kiblnd_tunables.kib_ib_mtu)
660                 cmid->route.path_rec->mtu =
661                         ib_mtu_int_to_enum(*kiblnd_tunables.kib_ib_mtu);
662 }
663
664 static int
665 kiblnd_get_completion_vector(struct kib_conn *conn, int cpt)
666 {
667         cpumask_var_t   *mask;
668         int             vectors;
669         int             off;
670         int             i;
671         lnet_nid_t      ibp_nid;
672
673         vectors = conn->ibc_cmid->device->num_comp_vectors;
674         if (vectors <= 1)
675                 return 0;
676
677         mask = cfs_cpt_cpumask(lnet_cpt_table(), cpt);
678
679         /* hash NID to CPU id in this partition... when targeting a single peer
680          * with multiple QPs, to engage more cores in CQ processing to a single
681          * peer, use ibp_nconns to salt the value the comp_vector value
682          */
683         ibp_nid = conn->ibc_peer->ibp_nid +
684                 atomic_read(&conn->ibc_peer->ibp_nconns);
685         off = do_div(ibp_nid, cpumask_weight(*mask));
686         for_each_cpu(i, *mask) {
687                 if (off-- == 0)
688                         return i % vectors;
689         }
690
691         LBUG();
692         return 1;
693 }
694
695 /*
696  * Get the scheduler bound to this CPT. If the scheduler has no
697  * threads, which means that the CPT has no CPUs, then grab the
698  * next scheduler that we can use.
699  *
700  * This case would be triggered if a NUMA node is configured with
701  * no associated CPUs.
702  */
703 static struct kib_sched_info *
704 kiblnd_get_scheduler(int cpt)
705 {
706         struct kib_sched_info *sched;
707         int i;
708
709         sched = kiblnd_data.kib_scheds[cpt];
710
711         if (sched->ibs_nthreads > 0)
712                 return sched;
713
714         cfs_percpt_for_each(sched, i, kiblnd_data.kib_scheds) {
715                 if (sched->ibs_nthreads > 0) {
716                         CDEBUG(D_NET, "scheduler[%d] has no threads. selected scheduler[%d]\n",
717                                         cpt, sched->ibs_cpt);
718                         return sched;
719                 }
720         }
721
722         return NULL;
723 }
724
725 static unsigned int kiblnd_send_wrs(struct kib_conn *conn)
726 {
727         /*
728          * One WR for the LNet message
729          * And ibc_max_frags for the transfer WRs
730          */
731         int ret;
732         int multiplier = 1 + conn->ibc_max_frags;
733
734         /* FastReg needs two extra WRs for map and invalidate */
735         if (IS_FAST_REG_DEV(conn->ibc_hdev->ibh_dev))
736                 multiplier += 2;
737
738         /* account for a maximum of ibc_queue_depth in-flight transfers */
739         ret = multiplier * conn->ibc_queue_depth;
740
741         if (ret > conn->ibc_hdev->ibh_max_qp_wr) {
742                 CDEBUG(D_NET, "peer_credits %u will result in send work "
743                        "request size %d larger than maximum %d device "
744                        "can handle\n", conn->ibc_queue_depth, ret,
745                        conn->ibc_hdev->ibh_max_qp_wr);
746                 conn->ibc_queue_depth =
747                         conn->ibc_hdev->ibh_max_qp_wr / multiplier;
748         }
749
750         /* don't go beyond the maximum the device can handle */
751         return min(ret, conn->ibc_hdev->ibh_max_qp_wr);
752 }
753
754 struct kib_conn *
755 kiblnd_create_conn(struct kib_peer_ni *peer_ni, struct rdma_cm_id *cmid,
756                    int state, int version)
757 {
758         /* CAVEAT EMPTOR:
759          * If the new conn is created successfully it takes over the caller's
760          * ref on 'peer_ni'.  It also "owns" 'cmid' and destroys it when it itself
761          * is destroyed.  On failure, the caller's ref on 'peer_ni' remains and
762          * she must dispose of 'cmid'.  (Actually I'd block forever if I tried
763          * to destroy 'cmid' here since I'm called from the CM which still has
764          * its ref on 'cmid'). */
765         rwlock_t               *glock = &kiblnd_data.kib_global_lock;
766         struct kib_net              *net = peer_ni->ibp_ni->ni_data;
767         struct kib_dev *dev;
768         struct ib_qp_init_attr init_qp_attr = {};
769         struct kib_sched_info   *sched;
770 #ifdef HAVE_OFED_IB_CQ_INIT_ATTR
771         struct ib_cq_init_attr  cq_attr = {};
772 #endif
773         struct kib_conn *conn;
774         struct ib_cq            *cq;
775         unsigned long           flags;
776         int                     cpt;
777         int                     rc;
778         int                     i;
779
780         LASSERT(net != NULL);
781         LASSERT(!in_interrupt());
782
783         dev = net->ibn_dev;
784
785         cpt = lnet_cpt_of_nid(peer_ni->ibp_nid, peer_ni->ibp_ni);
786         sched = kiblnd_get_scheduler(cpt);
787
788         if (sched == NULL) {
789                 CERROR("no schedulers available. node is unhealthy\n");
790                 goto failed_0;
791         }
792
793         /*
794          * The cpt might have changed if we ended up selecting a non cpt
795          * native scheduler. So use the scheduler's cpt instead.
796          */
797         cpt = sched->ibs_cpt;
798
799         LIBCFS_CPT_ALLOC(conn, lnet_cpt_table(), cpt, sizeof(*conn));
800         if (conn == NULL) {
801                 CERROR("Can't allocate connection for %s\n",
802                        libcfs_nid2str(peer_ni->ibp_nid));
803                 goto failed_0;
804         }
805
806         conn->ibc_state = IBLND_CONN_INIT;
807         conn->ibc_version = version;
808         conn->ibc_peer = peer_ni;                       /* I take the caller's ref */
809         cmid->context = conn;                   /* for future CM callbacks */
810         conn->ibc_cmid = cmid;
811         conn->ibc_max_frags = peer_ni->ibp_max_frags;
812         conn->ibc_queue_depth = peer_ni->ibp_queue_depth;
813         conn->ibc_rxs = NULL;
814         conn->ibc_rx_pages = NULL;
815
816         INIT_LIST_HEAD(&conn->ibc_early_rxs);
817         INIT_LIST_HEAD(&conn->ibc_tx_noops);
818         INIT_LIST_HEAD(&conn->ibc_tx_queue);
819         INIT_LIST_HEAD(&conn->ibc_tx_queue_rsrvd);
820         INIT_LIST_HEAD(&conn->ibc_tx_queue_nocred);
821         INIT_LIST_HEAD(&conn->ibc_active_txs);
822         INIT_LIST_HEAD(&conn->ibc_zombie_txs);
823         spin_lock_init(&conn->ibc_lock);
824
825         LIBCFS_CPT_ALLOC(conn->ibc_connvars, lnet_cpt_table(), cpt,
826                          sizeof(*conn->ibc_connvars));
827         if (conn->ibc_connvars == NULL) {
828                 CERROR("Can't allocate in-progress connection state\n");
829                 goto failed_2;
830         }
831
832         write_lock_irqsave(glock, flags);
833         if (dev->ibd_failover) {
834                 write_unlock_irqrestore(glock, flags);
835                 CERROR("%s: failover in progress\n", dev->ibd_ifname);
836                 goto failed_2;
837         }
838
839         if (dev->ibd_hdev->ibh_ibdev != cmid->device) {
840                 /* wakeup failover thread and teardown connection */
841                 if (kiblnd_dev_can_failover(dev)) {
842                         list_add_tail(&dev->ibd_fail_list,
843                                       &kiblnd_data.kib_failed_devs);
844                         wake_up(&kiblnd_data.kib_failover_waitq);
845                 }
846
847                 write_unlock_irqrestore(glock, flags);
848                 CERROR("cmid HCA(%s), kib_dev(%s) need failover\n",
849                        cmid->device->name, dev->ibd_ifname);
850                 goto failed_2;
851         }
852
853         kiblnd_hdev_addref_locked(dev->ibd_hdev);
854         conn->ibc_hdev = dev->ibd_hdev;
855
856         kiblnd_setup_mtu_locked(cmid);
857
858         write_unlock_irqrestore(glock, flags);
859
860 #ifdef HAVE_OFED_IB_CQ_INIT_ATTR
861         cq_attr.cqe = IBLND_CQ_ENTRIES(conn);
862         cq_attr.comp_vector = kiblnd_get_completion_vector(conn, cpt);
863         cq = ib_create_cq(cmid->device,
864                           kiblnd_cq_completion, kiblnd_cq_event, conn,
865                           &cq_attr);
866 #else
867         cq = ib_create_cq(cmid->device,
868                           kiblnd_cq_completion, kiblnd_cq_event, conn,
869                           IBLND_CQ_ENTRIES(conn),
870                           kiblnd_get_completion_vector(conn, cpt));
871 #endif
872         if (IS_ERR(cq)) {
873                 /*
874                  * on MLX-5 (possibly MLX-4 as well) this error could be
875                  * hit if the concurrent_sends and/or peer_tx_credits is set
876                  * too high. Or due to an MLX-5 bug which tries to
877                  * allocate 256kb via kmalloc for WR cookie array
878                  */
879                 CERROR("Failed to create CQ with %d CQEs: %ld\n",
880                         IBLND_CQ_ENTRIES(conn), PTR_ERR(cq));
881                 goto failed_2;
882         }
883
884         conn->ibc_cq = cq;
885
886         rc = ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
887         if (rc != 0) {
888                 CERROR("Can't request completion notification: %d\n", rc);
889                 goto failed_2;
890         }
891
892         init_qp_attr.event_handler = kiblnd_qp_event;
893         init_qp_attr.qp_context = conn;
894         init_qp_attr.cap.max_send_sge = *kiblnd_tunables.kib_wrq_sge;
895         init_qp_attr.cap.max_recv_sge = 1;
896         init_qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
897         init_qp_attr.qp_type = IB_QPT_RC;
898         init_qp_attr.send_cq = cq;
899         init_qp_attr.recv_cq = cq;
900
901         if (peer_ni->ibp_queue_depth_mod &&
902             peer_ni->ibp_queue_depth_mod < peer_ni->ibp_queue_depth) {
903                 conn->ibc_queue_depth = peer_ni->ibp_queue_depth_mod;
904                 CDEBUG(D_NET, "Use reduced queue depth %u (from %u)\n",
905                        peer_ni->ibp_queue_depth_mod,
906                        peer_ni->ibp_queue_depth);
907         }
908
909         do {
910                 /* kiblnd_send_wrs() can change the connection's queue depth if
911                  * the maximum work requests for the device is maxed out
912                  */
913                 init_qp_attr.cap.max_send_wr = kiblnd_send_wrs(conn);
914                 init_qp_attr.cap.max_recv_wr = IBLND_RECV_WRS(conn);
915                 rc = rdma_create_qp(cmid, conn->ibc_hdev->ibh_pd,
916                                     &init_qp_attr);
917                 if (rc != -ENOMEM || conn->ibc_queue_depth < 2)
918                         break;
919                 conn->ibc_queue_depth--;
920         } while (rc);
921
922         if (rc) {
923                 CERROR("Can't create QP: %d, send_wr: %d, recv_wr: %d, "
924                        "send_sge: %d, recv_sge: %d\n",
925                        rc, init_qp_attr.cap.max_send_wr,
926                        init_qp_attr.cap.max_recv_wr,
927                        init_qp_attr.cap.max_send_sge,
928                        init_qp_attr.cap.max_recv_sge);
929                 goto failed_2;
930         }
931
932         conn->ibc_sched = sched;
933
934         if (!peer_ni->ibp_queue_depth_mod &&
935             conn->ibc_queue_depth != peer_ni->ibp_queue_depth) {
936                 CWARN("peer %s - queue depth reduced from %u to %u"
937                       "  to allow for qp creation\n",
938                       libcfs_nid2str(peer_ni->ibp_nid),
939                       peer_ni->ibp_queue_depth,
940                       conn->ibc_queue_depth);
941                 peer_ni->ibp_queue_depth_mod = conn->ibc_queue_depth;
942         }
943
944         LIBCFS_CPT_ALLOC(conn->ibc_rxs, lnet_cpt_table(), cpt,
945                          IBLND_RX_MSGS(conn) * sizeof(struct kib_rx));
946         if (conn->ibc_rxs == NULL) {
947                 CERROR("Cannot allocate RX buffers\n");
948                 goto failed_2;
949         }
950
951         rc = kiblnd_alloc_pages(&conn->ibc_rx_pages, cpt,
952                                 IBLND_RX_MSG_PAGES(conn));
953         if (rc != 0)
954                 goto failed_2;
955
956         kiblnd_map_rx_descs(conn);
957
958         /* 1 ref for caller and each rxmsg */
959         atomic_set(&conn->ibc_refcount, 1 + IBLND_RX_MSGS(conn));
960         conn->ibc_nrx = IBLND_RX_MSGS(conn);
961
962         /* post receives */
963         for (i = 0; i < IBLND_RX_MSGS(conn); i++) {
964                 rc = kiblnd_post_rx(&conn->ibc_rxs[i], IBLND_POSTRX_NO_CREDIT);
965                 if (rc != 0) {
966                         CERROR("Can't post rxmsg: %d\n", rc);
967
968                         /* Make posted receives complete */
969                         kiblnd_abort_receives(conn);
970
971                         /* correct # of posted buffers
972                          * NB locking needed now I'm racing with completion */
973                         spin_lock_irqsave(&sched->ibs_lock, flags);
974                         conn->ibc_nrx -= IBLND_RX_MSGS(conn) - i;
975                         spin_unlock_irqrestore(&sched->ibs_lock, flags);
976
977                         /* cmid will be destroyed by CM(ofed) after cm_callback
978                          * returned, so we can't refer it anymore
979                          * (by kiblnd_connd()->kiblnd_destroy_conn) */
980                         rdma_destroy_qp(conn->ibc_cmid);
981                         conn->ibc_cmid = NULL;
982
983                         /* Drop my own and unused rxbuffer refcounts */
984                         while (i++ <= IBLND_RX_MSGS(conn))
985                                 kiblnd_conn_decref(conn);
986
987                         return NULL;
988                 }
989         }
990
991         /* Init successful! */
992         LASSERT (state == IBLND_CONN_ACTIVE_CONNECT ||
993                  state == IBLND_CONN_PASSIVE_WAIT);
994         conn->ibc_state = state;
995
996         /* 1 more conn */
997         atomic_inc(&peer_ni->ibp_nconns);
998         atomic_inc(&net->ibn_nconns);
999         return conn;
1000
1001  failed_2:
1002         kiblnd_destroy_conn(conn);
1003         LIBCFS_FREE(conn, sizeof(*conn));
1004  failed_0:
1005         return NULL;
1006 }
1007
1008 void
1009 kiblnd_destroy_conn(struct kib_conn *conn)
1010 {
1011         struct rdma_cm_id *cmid = conn->ibc_cmid;
1012         struct kib_peer_ni *peer_ni = conn->ibc_peer;
1013
1014         LASSERT (!in_interrupt());
1015         LASSERT (atomic_read(&conn->ibc_refcount) == 0);
1016         LASSERT(list_empty(&conn->ibc_early_rxs));
1017         LASSERT(list_empty(&conn->ibc_tx_noops));
1018         LASSERT(list_empty(&conn->ibc_tx_queue));
1019         LASSERT(list_empty(&conn->ibc_tx_queue_rsrvd));
1020         LASSERT(list_empty(&conn->ibc_tx_queue_nocred));
1021         LASSERT(list_empty(&conn->ibc_active_txs));
1022         LASSERT (conn->ibc_noops_posted == 0);
1023         LASSERT (conn->ibc_nsends_posted == 0);
1024
1025         switch (conn->ibc_state) {
1026         default:
1027                 /* conn must be completely disengaged from the network */
1028                 LBUG();
1029
1030         case IBLND_CONN_DISCONNECTED:
1031                 /* connvars should have been freed already */
1032                 LASSERT (conn->ibc_connvars == NULL);
1033                 break;
1034
1035         case IBLND_CONN_INIT:
1036                 break;
1037         }
1038
1039         /* conn->ibc_cmid might be destroyed by CM already */
1040         if (cmid != NULL && cmid->qp != NULL)
1041                 rdma_destroy_qp(cmid);
1042
1043         if (conn->ibc_cq)
1044                 ib_destroy_cq(conn->ibc_cq);
1045
1046         kiblnd_txlist_done(&conn->ibc_zombie_txs, -ECONNABORTED,
1047                            LNET_MSG_STATUS_OK);
1048
1049         if (conn->ibc_rx_pages != NULL)
1050                 kiblnd_unmap_rx_descs(conn);
1051
1052         if (conn->ibc_rxs != NULL)
1053                 CFS_FREE_PTR_ARRAY(conn->ibc_rxs, IBLND_RX_MSGS(conn));
1054
1055         if (conn->ibc_connvars != NULL)
1056                 LIBCFS_FREE(conn->ibc_connvars, sizeof(*conn->ibc_connvars));
1057
1058         if (conn->ibc_hdev != NULL)
1059                 kiblnd_hdev_decref(conn->ibc_hdev);
1060
1061         /* See CAVEAT EMPTOR above in kiblnd_create_conn */
1062         if (conn->ibc_state != IBLND_CONN_INIT) {
1063                 struct kib_net *net = peer_ni->ibp_ni->ni_data;
1064
1065                 kiblnd_peer_decref(peer_ni);
1066                 rdma_destroy_id(cmid);
1067                 atomic_dec(&peer_ni->ibp_nconns);
1068                 atomic_dec(&net->ibn_nconns);
1069         }
1070 }
1071
1072 int
1073 kiblnd_close_peer_conns_locked(struct kib_peer_ni *peer_ni, int why)
1074 {
1075         struct kib_conn *conn;
1076         struct kib_conn *cnxt;
1077         int count = 0;
1078
1079         list_for_each_entry_safe(conn, cnxt, &peer_ni->ibp_conns,
1080                                  ibc_list) {
1081                 CDEBUG(D_NET, "Closing conn -> %s, "
1082                               "version: %x, reason: %d\n",
1083                        libcfs_nid2str(peer_ni->ibp_nid),
1084                        conn->ibc_version, why);
1085
1086                 kiblnd_close_conn_locked(conn, why);
1087                 count++;
1088         }
1089
1090         return count;
1091 }
1092
1093 int
1094 kiblnd_close_stale_conns_locked(struct kib_peer_ni *peer_ni,
1095                                 int version, __u64 incarnation)
1096 {
1097         struct kib_conn *conn;
1098         struct kib_conn *cnxt;
1099         int count = 0;
1100
1101         list_for_each_entry_safe(conn, cnxt, &peer_ni->ibp_conns,
1102                                  ibc_list) {
1103                 if (conn->ibc_version     == version &&
1104                     conn->ibc_incarnation == incarnation)
1105                         continue;
1106
1107                 CDEBUG(D_NET, "Closing stale conn -> %s version: %x, "
1108                               "incarnation:%#llx(%x, %#llx)\n",
1109                        libcfs_nid2str(peer_ni->ibp_nid),
1110                        conn->ibc_version, conn->ibc_incarnation,
1111                        version, incarnation);
1112
1113                 kiblnd_close_conn_locked(conn, -ESTALE);
1114                 count++;
1115         }
1116
1117         return count;
1118 }
1119
1120 static int
1121 kiblnd_close_matching_conns(struct lnet_ni *ni, lnet_nid_t nid)
1122 {
1123         struct kib_peer_ni *peer_ni;
1124         struct hlist_node *pnxt;
1125         int lo;
1126         int hi;
1127         int i;
1128         unsigned long flags;
1129         int count = 0;
1130
1131         write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1132
1133         if (nid != LNET_NID_ANY) {
1134                 lo = hash_min(nid, HASH_BITS(kiblnd_data.kib_peers));
1135                 hi = lo;
1136         } else {
1137                 lo = 0;
1138                 hi = HASH_SIZE(kiblnd_data.kib_peers) - 1;
1139         }
1140
1141         for (i = lo; i <= hi; i++) {
1142                 hlist_for_each_entry_safe(peer_ni, pnxt,
1143                                           &kiblnd_data.kib_peers[i], ibp_list) {
1144                         LASSERT(!kiblnd_peer_idle(peer_ni));
1145
1146                         if (peer_ni->ibp_ni != ni)
1147                                 continue;
1148
1149                         if (!(nid == LNET_NID_ANY || nid == peer_ni->ibp_nid))
1150                                 continue;
1151
1152                         count += kiblnd_close_peer_conns_locked(peer_ni, 0);
1153                 }
1154         }
1155
1156         write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1157
1158         /* wildcards always succeed */
1159         if (nid == LNET_NID_ANY)
1160                 return 0;
1161
1162         return (count == 0) ? -ENOENT : 0;
1163 }
1164
1165 static int
1166 kiblnd_ctl(struct lnet_ni *ni, unsigned int cmd, void *arg)
1167 {
1168         struct libcfs_ioctl_data *data = arg;
1169         int                       rc = -EINVAL;
1170
1171         switch(cmd) {
1172         case IOC_LIBCFS_GET_PEER: {
1173                 lnet_nid_t   nid = 0;
1174                 int          count = 0;
1175
1176                 rc = kiblnd_get_peer_info(ni, data->ioc_nid, data->ioc_count,
1177                                           &nid, &count);
1178                 data->ioc_nid    = nid;
1179                 data->ioc_count  = count;
1180                 break;
1181         }
1182
1183         case IOC_LIBCFS_DEL_PEER: {
1184                 rc = kiblnd_del_peer(ni, data->ioc_nid);
1185                 break;
1186         }
1187         case IOC_LIBCFS_GET_CONN: {
1188                 struct kib_conn *conn;
1189
1190                 rc = 0;
1191                 conn = kiblnd_get_conn_by_idx(ni, data->ioc_count);
1192                 if (conn == NULL) {
1193                         rc = -ENOENT;
1194                         break;
1195                 }
1196
1197                 LASSERT(conn->ibc_cmid != NULL);
1198                 data->ioc_nid = conn->ibc_peer->ibp_nid;
1199                 if (conn->ibc_cmid->route.path_rec == NULL)
1200                         data->ioc_u32[0] = 0; /* iWarp has no path MTU */
1201                 else
1202                         data->ioc_u32[0] =
1203                         ib_mtu_enum_to_int(conn->ibc_cmid->route.path_rec->mtu);
1204                 kiblnd_conn_decref(conn);
1205                 break;
1206         }
1207         case IOC_LIBCFS_CLOSE_CONNECTION: {
1208                 rc = kiblnd_close_matching_conns(ni, data->ioc_nid);
1209                 break;
1210         }
1211
1212         default:
1213                 break;
1214         }
1215
1216         return rc;
1217 }
1218
1219 static const struct ln_key_list kiblnd_tunables_keys = {
1220         .lkl_maxattr                    = LNET_NET_O2IBLND_TUNABLES_ATTR_MAX,
1221         .lkl_list                       = {
1222                 [LNET_NET_O2IBLND_TUNABLES_ATTR_HIW_PEER_CREDITS]  = {
1223                         .lkp_value      = "peercredits_hiw",
1224                         .lkp_data_type  = NLA_U32
1225                 },
1226                 [LNET_NET_O2IBLND_TUNABLES_ATTR_MAP_ON_DEMAND]  = {
1227                         .lkp_value      = "map_on_demand",
1228                         .lkp_data_type  = NLA_FLAG
1229                 },
1230                 [LNET_NET_O2IBLND_TUNABLES_ATTR_CONCURRENT_SENDS]  = {
1231                         .lkp_value      = "concurrent_sends",
1232                         .lkp_data_type  = NLA_U32
1233                 },
1234                 [LNET_NET_O2IBLND_TUNABLES_ATTR_FMR_POOL_SIZE]  = {
1235                         .lkp_value      = "fmr_pool_size",
1236                         .lkp_data_type  = NLA_U32
1237                 },
1238                 [LNET_NET_O2IBLND_TUNABLES_ATTR_FMR_FLUSH_TRIGGER]  = {
1239                         .lkp_value      = "fmr_flush_trigger",
1240                         .lkp_data_type  = NLA_U32
1241                 },
1242                 [LNET_NET_O2IBLND_TUNABLES_ATTR_FMR_CACHE]  = {
1243                         .lkp_value      = "fmr_cache",
1244                         .lkp_data_type  = NLA_U32
1245                 },
1246                 [LNET_NET_O2IBLND_TUNABLES_ATTR_NTX]  = {
1247                         .lkp_value      = "ntx",
1248                         .lkp_data_type  = NLA_U16
1249                 },
1250                 [LNET_NET_O2IBLND_TUNABLES_ATTR_CONNS_PER_PEER]  = {
1251                         .lkp_value      = "conns_per_peer",
1252                         .lkp_data_type  = NLA_U16
1253                 },
1254                 [LNET_NET_O2IBLND_TUNABLES_ATTR_LND_TIMEOUT] = {
1255                         .lkp_value      = "timeout",
1256                         .lkp_data_type  = NLA_U32,
1257                 },
1258                 [LNET_NET_O2IBLND_TUNABLES_ATTR_LND_TOS] = {
1259                         .lkp_value      = "tos",
1260                         .lkp_data_type  = NLA_S16,
1261                 },
1262         },
1263 };
1264
1265 static int
1266 kiblnd_nl_get(int cmd, struct sk_buff *msg, int type, void *data)
1267 {
1268         struct lnet_ioctl_config_o2iblnd_tunables *tuns;
1269         struct lnet_ni *ni = data;
1270
1271         if (!ni || !msg)
1272                 return -EINVAL;
1273
1274         if (cmd != LNET_CMD_NETS || type != LNET_NET_LOCAL_NI_ATTR_LND_TUNABLES)
1275                 return -EOPNOTSUPP;
1276
1277         tuns = &ni->ni_lnd_tunables.lnd_tun_u.lnd_o2ib;
1278         nla_put_u32(msg, LNET_NET_O2IBLND_TUNABLES_ATTR_HIW_PEER_CREDITS,
1279                     tuns->lnd_peercredits_hiw);
1280         if (tuns->lnd_map_on_demand) {
1281                 nla_put_flag(msg,
1282                              LNET_NET_O2IBLND_TUNABLES_ATTR_MAP_ON_DEMAND);
1283         }
1284         nla_put_u32(msg, LNET_NET_O2IBLND_TUNABLES_ATTR_CONCURRENT_SENDS,
1285                     tuns->lnd_concurrent_sends);
1286         nla_put_u32(msg, LNET_NET_O2IBLND_TUNABLES_ATTR_FMR_POOL_SIZE,
1287                     tuns->lnd_fmr_pool_size);
1288         nla_put_u32(msg, LNET_NET_O2IBLND_TUNABLES_ATTR_FMR_FLUSH_TRIGGER,
1289                     tuns->lnd_fmr_flush_trigger);
1290         nla_put_u32(msg, LNET_NET_O2IBLND_TUNABLES_ATTR_FMR_CACHE,
1291                     tuns->lnd_fmr_cache);
1292         nla_put_u16(msg, LNET_NET_O2IBLND_TUNABLES_ATTR_NTX, tuns->lnd_ntx);
1293         nla_put_u16(msg, LNET_NET_O2IBLND_TUNABLES_ATTR_CONNS_PER_PEER,
1294                     tuns->lnd_conns_per_peer);
1295         nla_put_u32(msg, LNET_NET_O2IBLND_TUNABLES_ATTR_LND_TIMEOUT,
1296                     kiblnd_timeout());
1297         nla_put_s16(msg, LNET_NET_O2IBLND_TUNABLES_ATTR_LND_TOS,
1298                     tuns->lnd_tos);
1299
1300         return 0;
1301 }
1302
1303 static inline void
1304 kiblnd_nl_set_default(int cmd, int type, void *data)
1305 {
1306         struct lnet_lnd_tunables *tunables = data;
1307         struct lnet_ioctl_config_o2iblnd_tunables *lt;
1308         struct lnet_ioctl_config_o2iblnd_tunables *df;
1309
1310         lt = &tunables->lnd_tun_u.lnd_o2ib;
1311         df = &kib_default_tunables;
1312         switch (type) {
1313         case LNET_NET_O2IBLND_TUNABLES_ATTR_HIW_PEER_CREDITS:
1314                 lt->lnd_peercredits_hiw = df->lnd_peercredits_hiw;
1315                 break;
1316         case LNET_NET_O2IBLND_TUNABLES_ATTR_MAP_ON_DEMAND:
1317                 lt->lnd_map_on_demand = df->lnd_map_on_demand;
1318                 break;
1319         case LNET_NET_O2IBLND_TUNABLES_ATTR_CONCURRENT_SENDS:
1320                 lt->lnd_concurrent_sends = df->lnd_concurrent_sends;
1321                 break;
1322         case LNET_NET_O2IBLND_TUNABLES_ATTR_FMR_POOL_SIZE:
1323                 lt->lnd_fmr_pool_size = df->lnd_fmr_pool_size;
1324                 break;
1325         case LNET_NET_O2IBLND_TUNABLES_ATTR_FMR_FLUSH_TRIGGER:
1326                 lt->lnd_fmr_flush_trigger = df->lnd_fmr_flush_trigger;
1327                 break;
1328         case LNET_NET_O2IBLND_TUNABLES_ATTR_FMR_CACHE:
1329                 lt->lnd_fmr_cache = df->lnd_fmr_cache;
1330                 break;
1331         case LNET_NET_O2IBLND_TUNABLES_ATTR_NTX:
1332                 lt->lnd_ntx = df->lnd_ntx;
1333                 break;
1334         case LNET_NET_O2IBLND_TUNABLES_ATTR_LND_TIMEOUT:
1335                 lt->lnd_timeout = df->lnd_timeout;
1336                 break;
1337         case LNET_NET_O2IBLND_TUNABLES_ATTR_CONNS_PER_PEER:
1338                 lt->lnd_conns_per_peer = df->lnd_conns_per_peer;
1339                 fallthrough;
1340         default:
1341                 break;
1342         }
1343
1344 }
1345
1346 static int
1347 kiblnd_nl_set(int cmd, struct nlattr *attr, int type, void *data)
1348 {
1349         struct lnet_lnd_tunables *tunables = data;
1350         int rc = 0;
1351         s64 num;
1352
1353         if (cmd != LNET_CMD_NETS)
1354                 return -EOPNOTSUPP;
1355
1356         if (!attr) {
1357                 kiblnd_nl_set_default(cmd, type, data);
1358                 return 0;
1359         }
1360
1361         if (nla_type(attr) != LN_SCALAR_ATTR_INT_VALUE)
1362                 return -EINVAL;
1363
1364         switch (type) {
1365         case LNET_NET_O2IBLND_TUNABLES_ATTR_HIW_PEER_CREDITS:
1366                 tunables->lnd_tun_u.lnd_o2ib.lnd_peercredits_hiw = nla_get_s64(attr);
1367                 break;
1368         case LNET_NET_O2IBLND_TUNABLES_ATTR_MAP_ON_DEMAND:
1369                 tunables->lnd_tun_u.lnd_o2ib.lnd_map_on_demand = nla_get_s64(attr);
1370                 break;
1371         case LNET_NET_O2IBLND_TUNABLES_ATTR_CONCURRENT_SENDS:
1372                 tunables->lnd_tun_u.lnd_o2ib.lnd_concurrent_sends = nla_get_s64(attr);
1373                 break;
1374         case LNET_NET_O2IBLND_TUNABLES_ATTR_FMR_POOL_SIZE:
1375                 tunables->lnd_tun_u.lnd_o2ib.lnd_fmr_pool_size = nla_get_s64(attr);
1376                 break;
1377         case LNET_NET_O2IBLND_TUNABLES_ATTR_FMR_FLUSH_TRIGGER:
1378                 tunables->lnd_tun_u.lnd_o2ib.lnd_fmr_flush_trigger = nla_get_s64(attr);
1379                 break;
1380         case LNET_NET_O2IBLND_TUNABLES_ATTR_FMR_CACHE:
1381                 tunables->lnd_tun_u.lnd_o2ib.lnd_fmr_cache = nla_get_s64(attr);
1382                 break;
1383         case LNET_NET_O2IBLND_TUNABLES_ATTR_NTX:
1384                 tunables->lnd_tun_u.lnd_o2ib.lnd_ntx = nla_get_s64(attr);
1385                 break;
1386         case LNET_NET_O2IBLND_TUNABLES_ATTR_LND_TIMEOUT:
1387                 tunables->lnd_tun_u.lnd_o2ib.lnd_timeout = nla_get_s64(attr);
1388                 break;
1389         case LNET_NET_O2IBLND_TUNABLES_ATTR_CONNS_PER_PEER:
1390                 num = nla_get_s64(attr);
1391                 if (num >= 0 && num < 128)
1392                         tunables->lnd_tun_u.lnd_o2ib.lnd_conns_per_peer = num;
1393                 else
1394                         rc = -ERANGE;
1395                 break;
1396         case LNET_NET_O2IBLND_TUNABLES_ATTR_LND_TOS:
1397                 num = nla_get_s64(attr);
1398                 tunables->lnd_tun_u.lnd_o2ib.lnd_tos = num;
1399                 fallthrough;
1400         default:
1401                 break;
1402         }
1403
1404         return rc;
1405 }
1406
1407 static void
1408 kiblnd_free_pages(struct kib_pages *p)
1409 {
1410         int     npages = p->ibp_npages;
1411         int     i;
1412
1413         for (i = 0; i < npages; i++) {
1414                 if (p->ibp_pages[i] != NULL)
1415                         __free_page(p->ibp_pages[i]);
1416         }
1417
1418         LIBCFS_FREE(p, offsetof(struct kib_pages, ibp_pages[npages]));
1419 }
1420
1421 int
1422 kiblnd_alloc_pages(struct kib_pages **pp, int cpt, int npages)
1423 {
1424         struct kib_pages *p;
1425         int i;
1426
1427         LIBCFS_CPT_ALLOC(p, lnet_cpt_table(), cpt,
1428                          offsetof(struct kib_pages, ibp_pages[npages]));
1429         if (p == NULL) {
1430                 CERROR("Can't allocate descriptor for %d pages\n", npages);
1431                 return -ENOMEM;
1432         }
1433
1434         memset(p, 0, offsetof(struct kib_pages, ibp_pages[npages]));
1435         p->ibp_npages = npages;
1436
1437         for (i = 0; i < npages; i++) {
1438                 p->ibp_pages[i] = cfs_page_cpt_alloc(lnet_cpt_table(), cpt,
1439                                                      GFP_NOFS);
1440                 if (p->ibp_pages[i] == NULL) {
1441                         CERROR("Can't allocate page %d of %d\n", i, npages);
1442                         kiblnd_free_pages(p);
1443                         return -ENOMEM;
1444                 }
1445         }
1446
1447         *pp = p;
1448         return 0;
1449 }
1450
1451 void
1452 kiblnd_unmap_rx_descs(struct kib_conn *conn)
1453 {
1454         struct kib_rx *rx;
1455         int       i;
1456
1457         LASSERT (conn->ibc_rxs != NULL);
1458         LASSERT (conn->ibc_hdev != NULL);
1459
1460         for (i = 0; i < IBLND_RX_MSGS(conn); i++) {
1461                 rx = &conn->ibc_rxs[i];
1462
1463                 LASSERT(rx->rx_nob >= 0); /* not posted */
1464
1465                 kiblnd_dma_unmap_single(conn->ibc_hdev->ibh_ibdev,
1466                                         KIBLND_UNMAP_ADDR(rx, rx_msgunmap,
1467                                                           rx->rx_msgaddr),
1468                                         IBLND_MSG_SIZE, DMA_FROM_DEVICE);
1469         }
1470
1471         kiblnd_free_pages(conn->ibc_rx_pages);
1472
1473         conn->ibc_rx_pages = NULL;
1474 }
1475
1476 void
1477 kiblnd_map_rx_descs(struct kib_conn *conn)
1478 {
1479         struct kib_rx *rx;
1480         struct page    *pg;
1481         int             pg_off;
1482         int             ipg;
1483         int             i;
1484
1485         for (pg_off = ipg = i = 0; i < IBLND_RX_MSGS(conn); i++) {
1486                 pg = conn->ibc_rx_pages->ibp_pages[ipg];
1487                 rx = &conn->ibc_rxs[i];
1488
1489                 rx->rx_conn = conn;
1490                 rx->rx_msg = (struct kib_msg *)(((char *)page_address(pg)) + pg_off);
1491
1492                 rx->rx_msgaddr =
1493                         kiblnd_dma_map_single(conn->ibc_hdev->ibh_ibdev,
1494                                               rx->rx_msg, IBLND_MSG_SIZE,
1495                                               DMA_FROM_DEVICE);
1496                 LASSERT(!kiblnd_dma_mapping_error(conn->ibc_hdev->ibh_ibdev,
1497                                                   rx->rx_msgaddr));
1498                 KIBLND_UNMAP_ADDR_SET(rx, rx_msgunmap, rx->rx_msgaddr);
1499
1500                 CDEBUG(D_NET, "rx %d: %p %#llx(%#llx)\n",
1501                        i, rx->rx_msg, rx->rx_msgaddr,
1502                        (__u64)(page_to_phys(pg) + pg_off));
1503
1504                 pg_off += IBLND_MSG_SIZE;
1505                 LASSERT(pg_off <= PAGE_SIZE);
1506
1507                 if (pg_off == PAGE_SIZE) {
1508                         pg_off = 0;
1509                         ipg++;
1510                         LASSERT(ipg <= IBLND_RX_MSG_PAGES(conn));
1511                 }
1512         }
1513 }
1514
1515 static void
1516 kiblnd_unmap_tx_pool(struct kib_tx_pool *tpo)
1517 {
1518         struct kib_hca_dev *hdev = tpo->tpo_hdev;
1519         struct kib_tx *tx;
1520         int i;
1521
1522         LASSERT (tpo->tpo_pool.po_allocated == 0);
1523
1524         if (hdev == NULL)
1525                 return;
1526
1527         for (i = 0; i < tpo->tpo_pool.po_size; i++) {
1528                 tx = &tpo->tpo_tx_descs[i];
1529                 kiblnd_dma_unmap_single(hdev->ibh_ibdev,
1530                                         KIBLND_UNMAP_ADDR(tx, tx_msgunmap,
1531                                                           tx->tx_msgaddr),
1532                                         IBLND_MSG_SIZE, DMA_TO_DEVICE);
1533         }
1534
1535         kiblnd_hdev_decref(hdev);
1536         tpo->tpo_hdev = NULL;
1537 }
1538
1539 static struct kib_hca_dev *
1540 kiblnd_current_hdev(struct kib_dev *dev)
1541 {
1542         struct kib_hca_dev *hdev;
1543         unsigned long  flags;
1544         int            i = 0;
1545
1546         read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1547         while (dev->ibd_failover) {
1548                 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1549                 if (i++ % 50 == 0)
1550                         CDEBUG(D_NET, "%s: Wait for failover\n",
1551                                dev->ibd_ifname);
1552                 schedule_timeout_interruptible(cfs_time_seconds(1) / 100);
1553
1554                 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1555         }
1556
1557         kiblnd_hdev_addref_locked(dev->ibd_hdev);
1558         hdev = dev->ibd_hdev;
1559
1560         read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1561
1562         return hdev;
1563 }
1564
1565 static void
1566 kiblnd_map_tx_pool(struct kib_tx_pool *tpo)
1567 {
1568         struct kib_pages *txpgs = tpo->tpo_tx_pages;
1569         struct kib_pool *pool = &tpo->tpo_pool;
1570         struct kib_net      *net   = pool->po_owner->ps_net;
1571         struct kib_dev *dev;
1572         struct page *page;
1573         struct kib_tx *tx;
1574         int             page_offset;
1575         int             ipage;
1576         int             i;
1577
1578         LASSERT (net != NULL);
1579
1580         dev = net->ibn_dev;
1581
1582         /* pre-mapped messages are not bigger than 1 page */
1583         BUILD_BUG_ON(IBLND_MSG_SIZE > PAGE_SIZE);
1584
1585         /* No fancy arithmetic when we do the buffer calculations */
1586         BUILD_BUG_ON(PAGE_SIZE % IBLND_MSG_SIZE != 0);
1587
1588         tpo->tpo_hdev = kiblnd_current_hdev(dev);
1589
1590         for (ipage = page_offset = i = 0; i < pool->po_size; i++) {
1591                 page = txpgs->ibp_pages[ipage];
1592                 tx = &tpo->tpo_tx_descs[i];
1593
1594                 tx->tx_msg = (struct kib_msg *)(((char *)page_address(page)) +
1595                                                 page_offset);
1596
1597                 tx->tx_msgaddr = kiblnd_dma_map_single(tpo->tpo_hdev->ibh_ibdev,
1598                                                        tx->tx_msg,
1599                                                        IBLND_MSG_SIZE,
1600                                                        DMA_TO_DEVICE);
1601                 LASSERT(!kiblnd_dma_mapping_error(tpo->tpo_hdev->ibh_ibdev,
1602                                                   tx->tx_msgaddr));
1603                 KIBLND_UNMAP_ADDR_SET(tx, tx_msgunmap, tx->tx_msgaddr);
1604
1605                 list_add(&tx->tx_list, &pool->po_free_list);
1606
1607                 page_offset += IBLND_MSG_SIZE;
1608                 LASSERT(page_offset <= PAGE_SIZE);
1609
1610                 if (page_offset == PAGE_SIZE) {
1611                         page_offset = 0;
1612                         ipage++;
1613                         LASSERT(ipage <= txpgs->ibp_npages);
1614                 }
1615         }
1616 }
1617
1618 static void
1619 kiblnd_destroy_fmr_pool(struct kib_fmr_pool *fpo)
1620 {
1621         LASSERT(fpo->fpo_map_count == 0);
1622
1623 #ifdef HAVE_OFED_FMR_POOL_API
1624         if (fpo->fpo_is_fmr && fpo->fmr.fpo_fmr_pool) {
1625                 ib_destroy_fmr_pool(fpo->fmr.fpo_fmr_pool);
1626         } else
1627 #endif /* HAVE_OFED_FMR_POOL_API */
1628         {
1629                 struct kib_fast_reg_descriptor *frd, *tmp;
1630                 int i = 0;
1631
1632                 list_for_each_entry_safe(frd, tmp, &fpo->fast_reg.fpo_pool_list,
1633                                          frd_list) {
1634                         list_del(&frd->frd_list);
1635 #ifndef HAVE_OFED_IB_MAP_MR_SG
1636                         ib_free_fast_reg_page_list(frd->frd_frpl);
1637 #endif
1638                         ib_dereg_mr(frd->frd_mr);
1639                         LIBCFS_FREE(frd, sizeof(*frd));
1640                         i++;
1641                 }
1642                 if (i < fpo->fast_reg.fpo_pool_size)
1643                         CERROR("FastReg pool still has %d regions registered\n",
1644                                 fpo->fast_reg.fpo_pool_size - i);
1645         }
1646
1647         if (fpo->fpo_hdev)
1648                 kiblnd_hdev_decref(fpo->fpo_hdev);
1649
1650         LIBCFS_FREE(fpo, sizeof(*fpo));
1651 }
1652
1653 static void
1654 kiblnd_destroy_fmr_pool_list(struct list_head *head)
1655 {
1656         struct kib_fmr_pool *fpo, *tmp;
1657
1658         list_for_each_entry_safe(fpo, tmp, head, fpo_list) {
1659                 list_del(&fpo->fpo_list);
1660                 kiblnd_destroy_fmr_pool(fpo);
1661         }
1662 }
1663
1664 static int
1665 kiblnd_fmr_pool_size(struct lnet_ioctl_config_o2iblnd_tunables *tunables,
1666                      int ncpts)
1667 {
1668         int size = tunables->lnd_fmr_pool_size / ncpts;
1669
1670         return max(IBLND_FMR_POOL, size);
1671 }
1672
1673 static int
1674 kiblnd_fmr_flush_trigger(struct lnet_ioctl_config_o2iblnd_tunables *tunables,
1675                          int ncpts)
1676 {
1677         int size = tunables->lnd_fmr_flush_trigger / ncpts;
1678
1679         return max(IBLND_FMR_POOL_FLUSH, size);
1680 }
1681
1682 #ifdef HAVE_OFED_FMR_POOL_API
1683 static int kiblnd_alloc_fmr_pool(struct kib_fmr_poolset *fps,
1684                                  struct kib_fmr_pool *fpo)
1685 {
1686         struct ib_fmr_pool_param param = {
1687                 .max_pages_per_fmr = IBLND_MAX_RDMA_FRAGS,
1688                 .page_shift        = PAGE_SHIFT,
1689                 .access            = (IB_ACCESS_LOCAL_WRITE |
1690                                       IB_ACCESS_REMOTE_WRITE),
1691                 .pool_size         = fps->fps_pool_size,
1692                 .dirty_watermark   = fps->fps_flush_trigger,
1693                 .flush_function    = NULL,
1694                 .flush_arg         = NULL,
1695                 .cache             = !!fps->fps_cache };
1696         int rc = 0;
1697
1698         fpo->fmr.fpo_fmr_pool = ib_create_fmr_pool(fpo->fpo_hdev->ibh_pd,
1699                                                    &param);
1700         if (IS_ERR(fpo->fmr.fpo_fmr_pool)) {
1701                 rc = PTR_ERR(fpo->fmr.fpo_fmr_pool);
1702                 if (rc != -ENOSYS)
1703                         CERROR("Failed to create FMR pool: %d\n", rc);
1704                 else
1705                         CERROR("FMRs are not supported\n");
1706         }
1707         fpo->fpo_is_fmr = true;
1708
1709         return rc;
1710 }
1711 #endif /* HAVE_OFED_FMR_POOL_API */
1712
1713 static int kiblnd_alloc_freg_pool(struct kib_fmr_poolset *fps,
1714                                   struct kib_fmr_pool *fpo,
1715                                   enum kib_dev_caps dev_caps)
1716 {
1717         struct kib_fast_reg_descriptor *frd, *tmp;
1718         int i, rc;
1719
1720 #ifdef HAVE_OFED_FMR_POOL_API
1721         fpo->fpo_is_fmr = false;
1722 #endif
1723
1724         INIT_LIST_HEAD(&fpo->fast_reg.fpo_pool_list);
1725         fpo->fast_reg.fpo_pool_size = 0;
1726         for (i = 0; i < fps->fps_pool_size; i++) {
1727                 LIBCFS_CPT_ALLOC(frd, lnet_cpt_table(), fps->fps_cpt,
1728                                  sizeof(*frd));
1729                 if (!frd) {
1730                         CERROR("Failed to allocate a new fast_reg descriptor\n");
1731                         rc = -ENOMEM;
1732                         goto out;
1733                 }
1734                 frd->frd_mr = NULL;
1735
1736 #ifndef HAVE_OFED_IB_MAP_MR_SG
1737                 frd->frd_frpl = ib_alloc_fast_reg_page_list(fpo->fpo_hdev->ibh_ibdev,
1738                                                             IBLND_MAX_RDMA_FRAGS);
1739                 if (IS_ERR(frd->frd_frpl)) {
1740                         rc = PTR_ERR(frd->frd_frpl);
1741                         CERROR("Failed to allocate ib_fast_reg_page_list: %d\n",
1742                                 rc);
1743                         frd->frd_frpl = NULL;
1744                         goto out_middle;
1745                 }
1746 #endif
1747
1748 #ifdef HAVE_OFED_IB_ALLOC_FAST_REG_MR
1749                 frd->frd_mr = ib_alloc_fast_reg_mr(fpo->fpo_hdev->ibh_pd,
1750                                                    IBLND_MAX_RDMA_FRAGS);
1751 #else
1752                 /*
1753                  * it is expected to get here if this is an MLX-5 card.
1754                  * MLX-4 cards will always use FMR and MLX-5 cards will
1755                  * always use fast_reg. It turns out that some MLX-5 cards
1756                  * (possibly due to older FW versions) do not natively support
1757                  * gaps. So we will need to track them here.
1758                  */
1759                 frd->frd_mr = ib_alloc_mr(fpo->fpo_hdev->ibh_pd,
1760 #ifdef IB_MR_TYPE_SG_GAPS
1761                                           ((*kiblnd_tunables.kib_use_fastreg_gaps == 1) &&
1762                                            (dev_caps & IBLND_DEV_CAPS_FASTREG_GAPS_SUPPORT)) ?
1763                                                 IB_MR_TYPE_SG_GAPS :
1764                                                 IB_MR_TYPE_MEM_REG,
1765 #else
1766                                                 IB_MR_TYPE_MEM_REG,
1767 #endif
1768                                           IBLND_MAX_RDMA_FRAGS);
1769                 if ((*kiblnd_tunables.kib_use_fastreg_gaps == 1) &&
1770                     (dev_caps & IBLND_DEV_CAPS_FASTREG_GAPS_SUPPORT))
1771                         CWARN("using IB_MR_TYPE_SG_GAPS, expect a performance drop\n");
1772 #endif
1773                 if (IS_ERR(frd->frd_mr)) {
1774                         rc = PTR_ERR(frd->frd_mr);
1775                         CERROR("Failed to allocate ib_fast_reg_mr: %d\n", rc);
1776                         frd->frd_mr = NULL;
1777                         goto out_middle;
1778                 }
1779
1780                 /* indicate that the local invalidate needs to be generated */
1781                 frd->frd_valid = false;
1782
1783                 list_add_tail(&frd->frd_list, &fpo->fast_reg.fpo_pool_list);
1784                 fpo->fast_reg.fpo_pool_size++;
1785         }
1786
1787         return 0;
1788
1789 out_middle:
1790         if (frd->frd_mr)
1791                 ib_dereg_mr(frd->frd_mr);
1792 #ifndef HAVE_OFED_IB_MAP_MR_SG
1793         if (frd->frd_frpl)
1794                 ib_free_fast_reg_page_list(frd->frd_frpl);
1795 #endif
1796         LIBCFS_FREE(frd, sizeof(*frd));
1797
1798 out:
1799         list_for_each_entry_safe(frd, tmp, &fpo->fast_reg.fpo_pool_list,
1800                                  frd_list) {
1801                 list_del(&frd->frd_list);
1802 #ifndef HAVE_OFED_IB_MAP_MR_SG
1803                 ib_free_fast_reg_page_list(frd->frd_frpl);
1804 #endif
1805                 ib_dereg_mr(frd->frd_mr);
1806                 LIBCFS_FREE(frd, sizeof(*frd));
1807         }
1808
1809         return rc;
1810 }
1811
1812 static int kiblnd_create_fmr_pool(struct kib_fmr_poolset *fps,
1813                                   struct kib_fmr_pool **pp_fpo)
1814 {
1815         struct kib_dev *dev = fps->fps_net->ibn_dev;
1816         struct kib_fmr_pool *fpo;
1817         int rc;
1818
1819         LIBCFS_CPT_ALLOC(fpo, lnet_cpt_table(), fps->fps_cpt, sizeof(*fpo));
1820         if (!fpo) {
1821                 return -ENOMEM;
1822         }
1823         memset(fpo, 0, sizeof(*fpo));
1824
1825         fpo->fpo_hdev = kiblnd_current_hdev(dev);
1826
1827 #ifdef HAVE_OFED_FMR_POOL_API
1828         if (dev->ibd_dev_caps & IBLND_DEV_CAPS_FMR_ENABLED)
1829                 rc = kiblnd_alloc_fmr_pool(fps, fpo);
1830         else
1831 #endif /* HAVE_OFED_FMR_POOL_API */
1832                 rc = kiblnd_alloc_freg_pool(fps, fpo, dev->ibd_dev_caps);
1833         if (rc)
1834                 goto out_fpo;
1835
1836         fpo->fpo_deadline = ktime_get_seconds() + IBLND_POOL_DEADLINE;
1837         fpo->fpo_owner = fps;
1838         *pp_fpo = fpo;
1839
1840         return 0;
1841
1842 out_fpo:
1843         kiblnd_hdev_decref(fpo->fpo_hdev);
1844         LIBCFS_FREE(fpo, sizeof(*fpo));
1845         return rc;
1846 }
1847
1848 static void
1849 kiblnd_fail_fmr_poolset(struct kib_fmr_poolset *fps, struct list_head *zombies)
1850 {
1851         struct kib_fmr_pool *fpo;
1852
1853         if (fps->fps_net == NULL) /* intialized? */
1854                 return;
1855
1856         spin_lock(&fps->fps_lock);
1857
1858         while ((fpo = list_first_entry_or_null(&fps->fps_pool_list,
1859                                                struct kib_fmr_pool,
1860                                                fpo_list)) != NULL) {
1861                 fpo->fpo_failed = 1;
1862                 if (fpo->fpo_map_count == 0)
1863                         list_move(&fpo->fpo_list, zombies);
1864                 else
1865                         list_move(&fpo->fpo_list, &fps->fps_failed_pool_list);
1866         }
1867
1868         spin_unlock(&fps->fps_lock);
1869 }
1870
1871 static void
1872 kiblnd_fini_fmr_poolset(struct kib_fmr_poolset *fps)
1873 {
1874         if (fps->fps_net != NULL) { /* initialized? */
1875                 kiblnd_destroy_fmr_pool_list(&fps->fps_failed_pool_list);
1876                 kiblnd_destroy_fmr_pool_list(&fps->fps_pool_list);
1877         }
1878 }
1879
1880 static int
1881 kiblnd_init_fmr_poolset(struct kib_fmr_poolset *fps, int cpt, int ncpts,
1882                         struct kib_net *net,
1883                         struct lnet_ioctl_config_o2iblnd_tunables *tunables)
1884 {
1885         struct kib_fmr_pool *fpo;
1886         int rc;
1887
1888         memset(fps, 0, sizeof(struct kib_fmr_poolset));
1889
1890         fps->fps_net = net;
1891         fps->fps_cpt = cpt;
1892
1893         fps->fps_pool_size = kiblnd_fmr_pool_size(tunables, ncpts);
1894         fps->fps_flush_trigger = kiblnd_fmr_flush_trigger(tunables, ncpts);
1895         fps->fps_cache = tunables->lnd_fmr_cache;
1896
1897         spin_lock_init(&fps->fps_lock);
1898         INIT_LIST_HEAD(&fps->fps_pool_list);
1899         INIT_LIST_HEAD(&fps->fps_failed_pool_list);
1900
1901         rc = kiblnd_create_fmr_pool(fps, &fpo);
1902         if (rc == 0)
1903                 list_add_tail(&fpo->fpo_list, &fps->fps_pool_list);
1904
1905         return rc;
1906 }
1907
1908 static int
1909 kiblnd_fmr_pool_is_idle(struct kib_fmr_pool *fpo, time64_t now)
1910 {
1911         if (fpo->fpo_map_count != 0) /* still in use */
1912                 return 0;
1913         if (fpo->fpo_failed)
1914                 return 1;
1915         return now >= fpo->fpo_deadline;
1916 }
1917
1918 #if defined(HAVE_OFED_FMR_POOL_API) || !defined(HAVE_OFED_IB_MAP_MR_SG)
1919 static int
1920 kiblnd_map_tx_pages(struct kib_tx *tx, struct kib_rdma_desc *rd)
1921 {
1922         struct kib_hca_dev *hdev;
1923         __u64           *pages = tx->tx_pages;
1924         int             npages;
1925         int             size;
1926         int             i;
1927
1928         hdev = tx->tx_pool->tpo_hdev;
1929
1930         for (i = 0, npages = 0; i < rd->rd_nfrags; i++) {
1931                 for (size = 0; size <  rd->rd_frags[i].rf_nob;
1932                         size += hdev->ibh_page_size) {
1933                         pages[npages++] = (rd->rd_frags[i].rf_addr &
1934                                            hdev->ibh_page_mask) + size;
1935                 }
1936         }
1937
1938         return npages;
1939 }
1940 #endif
1941
1942 void
1943 kiblnd_fmr_pool_unmap(struct kib_fmr *fmr, int status)
1944 {
1945         LIST_HEAD(zombies);
1946         struct kib_fmr_pool *fpo = fmr->fmr_pool;
1947         struct kib_fmr_poolset *fps;
1948         time64_t now = ktime_get_seconds();
1949         struct kib_fmr_pool *tmp;
1950
1951         if (!fpo)
1952                 return;
1953
1954         fps = fpo->fpo_owner;
1955
1956 #ifdef HAVE_OFED_FMR_POOL_API
1957         if (fpo->fpo_is_fmr) {
1958                 if (fmr->fmr_pfmr) {
1959                         ib_fmr_pool_unmap(fmr->fmr_pfmr);
1960                         fmr->fmr_pfmr = NULL;
1961                 }
1962
1963                 if (status) {
1964                         int rc = ib_flush_fmr_pool(fpo->fmr.fpo_fmr_pool);
1965                         LASSERT(!rc);
1966                 }
1967         } else
1968 #endif /* HAVE_OFED_FMR_POOL_API */
1969         {
1970                 struct kib_fast_reg_descriptor *frd = fmr->fmr_frd;
1971                 if (frd) {
1972                         frd->frd_posted = false;
1973                         fmr->fmr_frd = NULL;
1974                         spin_lock(&fps->fps_lock);
1975                         list_add_tail(&frd->frd_list, &fpo->fast_reg.fpo_pool_list);
1976                         spin_unlock(&fps->fps_lock);
1977                 }
1978         }
1979         fmr->fmr_pool = NULL;
1980
1981         spin_lock(&fps->fps_lock);
1982         fpo->fpo_map_count--;   /* decref the pool */
1983
1984         list_for_each_entry_safe(fpo, tmp, &fps->fps_pool_list, fpo_list) {
1985                 /* the first pool is persistent */
1986                 if (fps->fps_pool_list.next == &fpo->fpo_list)
1987                         continue;
1988
1989                 if (kiblnd_fmr_pool_is_idle(fpo, now)) {
1990                         list_move(&fpo->fpo_list, &zombies);
1991                         fps->fps_version++;
1992                 }
1993         }
1994         spin_unlock(&fps->fps_lock);
1995
1996         if (!list_empty(&zombies))
1997                 kiblnd_destroy_fmr_pool_list(&zombies);
1998 }
1999
2000 int kiblnd_fmr_pool_map(struct kib_fmr_poolset *fps, struct kib_tx *tx,
2001                         struct kib_rdma_desc *rd, u32 nob, u64 iov,
2002                         struct kib_fmr *fmr)
2003 {
2004         struct kib_fmr_pool *fpo;
2005         __u64 version;
2006         bool is_rx = (rd != tx->tx_rd);
2007 #ifdef HAVE_OFED_FMR_POOL_API
2008         __u64 *pages = tx->tx_pages;
2009         bool tx_pages_mapped = false;
2010         int npages = 0;
2011 #endif
2012         int rc;
2013
2014 again:
2015         spin_lock(&fps->fps_lock);
2016         version = fps->fps_version;
2017         list_for_each_entry(fpo, &fps->fps_pool_list, fpo_list) {
2018                 fpo->fpo_deadline = ktime_get_seconds() + IBLND_POOL_DEADLINE;
2019                 fpo->fpo_map_count++;
2020
2021 #ifdef HAVE_OFED_FMR_POOL_API
2022                 fmr->fmr_pfmr = NULL;
2023                 if (fpo->fpo_is_fmr) {
2024                         struct ib_pool_fmr *pfmr;
2025
2026                         spin_unlock(&fps->fps_lock);
2027
2028                         if (!tx_pages_mapped) {
2029                                 npages = kiblnd_map_tx_pages(tx, rd);
2030                                 tx_pages_mapped = true;
2031                         }
2032
2033                         pfmr = kib_fmr_pool_map(fpo->fmr.fpo_fmr_pool,
2034                                                 pages, npages, iov);
2035                         if (likely(!IS_ERR(pfmr))) {
2036                                 fmr->fmr_key  = is_rx ? pfmr->fmr->rkey
2037                                         : pfmr->fmr->lkey;
2038                                 fmr->fmr_frd  = NULL;
2039                                 fmr->fmr_pfmr = pfmr;
2040                                 fmr->fmr_pool = fpo;
2041                                 return 0;
2042                         }
2043                         rc = PTR_ERR(pfmr);
2044                 } else
2045 #endif /* HAVE_OFED_FMR_POOL_API */
2046                 {
2047                         if (!list_empty(&fpo->fast_reg.fpo_pool_list)) {
2048                                 struct kib_fast_reg_descriptor *frd;
2049 #ifdef HAVE_OFED_IB_MAP_MR_SG
2050                                 struct ib_reg_wr *wr;
2051                                 int n;
2052 #else
2053                                 struct ib_rdma_wr *wr;
2054                                 struct ib_fast_reg_page_list *frpl;
2055 #endif
2056                                 struct ib_mr *mr;
2057
2058                                 frd = list_first_entry(
2059                                         &fpo->fast_reg.fpo_pool_list,
2060                                         struct kib_fast_reg_descriptor,
2061                                         frd_list);
2062                                 list_del(&frd->frd_list);
2063                                 spin_unlock(&fps->fps_lock);
2064
2065 #ifndef HAVE_OFED_IB_MAP_MR_SG
2066                                 frpl = frd->frd_frpl;
2067 #endif
2068                                 mr   = frd->frd_mr;
2069
2070                                 if (!frd->frd_valid) {
2071                                         struct ib_rdma_wr *inv_wr;
2072                                         __u32 key = is_rx ? mr->rkey : mr->lkey;
2073
2074                                         frd->frd_valid = true;
2075                                         inv_wr = &frd->frd_inv_wr;
2076                                         memset(inv_wr, 0, sizeof(*inv_wr));
2077
2078                                         inv_wr->wr.opcode = IB_WR_LOCAL_INV;
2079                                         inv_wr->wr.wr_id  = IBLND_WID_MR;
2080                                         inv_wr->wr.ex.invalidate_rkey = key;
2081
2082                                         /* Bump the key */
2083                                         key = ib_inc_rkey(key);
2084                                         ib_update_fast_reg_key(mr, key);
2085                                 }
2086
2087 #ifdef HAVE_OFED_IB_MAP_MR_SG
2088 #ifdef HAVE_OFED_IB_MAP_MR_SG_5ARGS
2089                                 n = ib_map_mr_sg(mr, tx->tx_frags,
2090                                                  rd->rd_nfrags, NULL, PAGE_SIZE);
2091 #else
2092                                 n = ib_map_mr_sg(mr, tx->tx_frags,
2093                                                  rd->rd_nfrags, PAGE_SIZE);
2094 #endif /* HAVE_OFED_IB_MAP_MR_SG_5ARGS */
2095                                 if (unlikely(n != rd->rd_nfrags)) {
2096                                         CERROR("Failed to map mr %d/%d elements\n",
2097                                                n, rd->rd_nfrags);
2098                                         return n < 0 ? n : -EINVAL;
2099                                 }
2100
2101                                 wr = &frd->frd_fastreg_wr;
2102                                 memset(wr, 0, sizeof(*wr));
2103
2104                                 wr->wr.opcode = IB_WR_REG_MR;
2105                                 wr->wr.wr_id  = IBLND_WID_MR;
2106                                 wr->wr.num_sge = 0;
2107                                 wr->wr.send_flags = 0;
2108                                 wr->mr = mr;
2109                                 wr->key = is_rx ? mr->rkey : mr->lkey;
2110                                 wr->access = (IB_ACCESS_LOCAL_WRITE |
2111                                               IB_ACCESS_REMOTE_WRITE);
2112 #else /* HAVE_OFED_IB_MAP_MR_SG */
2113                                 if (!tx_pages_mapped) {
2114                                         npages = kiblnd_map_tx_pages(tx, rd);
2115                                         tx_pages_mapped = true;
2116                                 }
2117
2118                                 LASSERT(npages <= frpl->max_page_list_len);
2119                                 memcpy(frpl->page_list, pages,
2120                                        sizeof(*pages) * npages);
2121
2122                                 /* Prepare FastReg WR */
2123                                 wr = &frd->frd_fastreg_wr;
2124                                 memset(wr, 0, sizeof(*wr));
2125
2126                                 wr->wr.opcode = IB_WR_FAST_REG_MR;
2127                                 wr->wr.wr_id  = IBLND_WID_MR;
2128
2129                                 wr->wr.wr.fast_reg.iova_start = iov;
2130                                 wr->wr.wr.fast_reg.page_list  = frpl;
2131                                 wr->wr.wr.fast_reg.page_list_len = npages;
2132                                 wr->wr.wr.fast_reg.page_shift = PAGE_SHIFT;
2133                                 wr->wr.wr.fast_reg.length = nob;
2134                                 wr->wr.wr.fast_reg.rkey =
2135                                         is_rx ? mr->rkey : mr->lkey;
2136                                 wr->wr.wr.fast_reg.access_flags =
2137                                         (IB_ACCESS_LOCAL_WRITE |
2138                                          IB_ACCESS_REMOTE_WRITE);
2139 #endif /* HAVE_OFED_IB_MAP_MR_SG */
2140
2141                                 fmr->fmr_key  = is_rx ? mr->rkey : mr->lkey;
2142                                 fmr->fmr_frd  = frd;
2143                                 fmr->fmr_pool = fpo;
2144                                 frd->frd_posted = false;
2145                                 return 0;
2146                         }
2147                         spin_unlock(&fps->fps_lock);
2148                         rc = -EAGAIN;
2149                 }
2150
2151                 spin_lock(&fps->fps_lock);
2152                 fpo->fpo_map_count--;
2153                 if (rc != -EAGAIN) {
2154                         spin_unlock(&fps->fps_lock);
2155                         return rc;
2156                 }
2157
2158                 /* EAGAIN and ... */
2159                 if (version != fps->fps_version) {
2160                         spin_unlock(&fps->fps_lock);
2161                         goto again;
2162                 }
2163         }
2164
2165         if (fps->fps_increasing) {
2166                 spin_unlock(&fps->fps_lock);
2167                 CDEBUG(D_NET, "Another thread is allocating new "
2168                        "FMR pool, waiting for her to complete\n");
2169                 wait_var_event(fps, !fps->fps_increasing);
2170                 goto again;
2171
2172         }
2173
2174         if (ktime_get_seconds() < fps->fps_next_retry) {
2175                 /* someone failed recently */
2176                 spin_unlock(&fps->fps_lock);
2177                 return -EAGAIN;
2178         }
2179
2180         fps->fps_increasing = 1;
2181         spin_unlock(&fps->fps_lock);
2182
2183         CDEBUG(D_NET, "Allocate new FMR pool\n");
2184         rc = kiblnd_create_fmr_pool(fps, &fpo);
2185         spin_lock(&fps->fps_lock);
2186         fps->fps_increasing = 0;
2187         wake_up_var(fps);
2188         if (rc == 0) {
2189                 fps->fps_version++;
2190                 list_add_tail(&fpo->fpo_list, &fps->fps_pool_list);
2191         } else {
2192                 fps->fps_next_retry = ktime_get_seconds() + IBLND_POOL_RETRY;
2193         }
2194         spin_unlock(&fps->fps_lock);
2195
2196         goto again;
2197 }
2198
2199 static void
2200 kiblnd_fini_pool(struct kib_pool *pool)
2201 {
2202         LASSERT(list_empty(&pool->po_free_list));
2203         LASSERT(pool->po_allocated == 0);
2204
2205         CDEBUG(D_NET, "Finalize %s pool\n", pool->po_owner->ps_name);
2206 }
2207
2208 static void
2209 kiblnd_init_pool(struct kib_poolset *ps, struct kib_pool *pool, int size)
2210 {
2211         CDEBUG(D_NET, "Initialize %s pool\n", ps->ps_name);
2212
2213         memset(pool, 0, sizeof(struct kib_pool));
2214         INIT_LIST_HEAD(&pool->po_free_list);
2215         pool->po_deadline = ktime_get_seconds() + IBLND_POOL_DEADLINE;
2216         pool->po_owner = ps;
2217         pool->po_size = size;
2218 }
2219
2220 static void
2221 kiblnd_destroy_pool_list(struct list_head *head)
2222 {
2223         struct kib_pool *pool;
2224
2225         while ((pool = list_first_entry_or_null(head,
2226                                                 struct kib_pool,
2227                                                 po_list)) != NULL) {
2228                 list_del(&pool->po_list);
2229
2230                 LASSERT(pool->po_owner != NULL);
2231                 pool->po_owner->ps_pool_destroy(pool);
2232         }
2233 }
2234
2235 static void
2236 kiblnd_fail_poolset(struct kib_poolset *ps, struct list_head *zombies)
2237 {
2238         struct kib_pool *po;
2239
2240         if (ps->ps_net == NULL) /* intialized? */
2241                 return;
2242
2243         spin_lock(&ps->ps_lock);
2244         while ((po = list_first_entry_or_null(&ps->ps_pool_list,
2245                                               struct kib_pool,
2246                                               po_list)) != NULL) {
2247                 po->po_failed = 1;
2248                 if (po->po_allocated == 0)
2249                         list_move(&po->po_list, zombies);
2250                 else
2251                         list_move(&po->po_list, &ps->ps_failed_pool_list);
2252         }
2253         spin_unlock(&ps->ps_lock);
2254 }
2255
2256 static void
2257 kiblnd_fini_poolset(struct kib_poolset *ps)
2258 {
2259         if (ps->ps_net != NULL) { /* initialized? */
2260                 kiblnd_destroy_pool_list(&ps->ps_failed_pool_list);
2261                 kiblnd_destroy_pool_list(&ps->ps_pool_list);
2262         }
2263 }
2264
2265 static int
2266 kiblnd_init_poolset(struct kib_poolset *ps, int cpt,
2267                     struct kib_net *net, char *name, int size,
2268                     kib_ps_pool_create_t po_create,
2269                     kib_ps_pool_destroy_t po_destroy,
2270                     kib_ps_node_init_t nd_init,
2271                     kib_ps_node_fini_t nd_fini)
2272 {
2273         struct kib_pool *pool;
2274         int rc;
2275
2276         memset(ps, 0, sizeof(struct kib_poolset));
2277
2278         ps->ps_cpt          = cpt;
2279         ps->ps_net          = net;
2280         ps->ps_pool_create  = po_create;
2281         ps->ps_pool_destroy = po_destroy;
2282         ps->ps_node_init    = nd_init;
2283         ps->ps_node_fini    = nd_fini;
2284         ps->ps_pool_size    = size;
2285         rc = strscpy(ps->ps_name, name, sizeof(ps->ps_name));
2286         if (rc < 0)
2287                 return rc;
2288         spin_lock_init(&ps->ps_lock);
2289         INIT_LIST_HEAD(&ps->ps_pool_list);
2290         INIT_LIST_HEAD(&ps->ps_failed_pool_list);
2291
2292         rc = ps->ps_pool_create(ps, size, &pool);
2293         if (rc == 0)
2294                 list_add(&pool->po_list, &ps->ps_pool_list);
2295         else
2296                 CERROR("Failed to create the first pool for %s\n", ps->ps_name);
2297
2298         return rc;
2299 }
2300
2301 static int
2302 kiblnd_pool_is_idle(struct kib_pool *pool, time64_t now)
2303 {
2304         if (pool->po_allocated != 0) /* still in use */
2305                 return 0;
2306         if (pool->po_failed)
2307                 return 1;
2308         return now >= pool->po_deadline;
2309 }
2310
2311 void
2312 kiblnd_pool_free_node(struct kib_pool *pool, struct list_head *node)
2313 {
2314         LIST_HEAD(zombies);
2315         struct kib_poolset *ps = pool->po_owner;
2316         struct kib_pool *tmp;
2317         time64_t now = ktime_get_seconds();
2318
2319         spin_lock(&ps->ps_lock);
2320
2321         if (ps->ps_node_fini != NULL)
2322                 ps->ps_node_fini(pool, node);
2323
2324         LASSERT(pool->po_allocated > 0);
2325         list_add(node, &pool->po_free_list);
2326         pool->po_allocated--;
2327
2328         list_for_each_entry_safe(pool, tmp, &ps->ps_pool_list, po_list) {
2329                 /* the first pool is persistent */
2330                 if (ps->ps_pool_list.next == &pool->po_list)
2331                         continue;
2332
2333                 if (kiblnd_pool_is_idle(pool, now))
2334                         list_move(&pool->po_list, &zombies);
2335         }
2336         spin_unlock(&ps->ps_lock);
2337
2338         if (!list_empty(&zombies))
2339                 kiblnd_destroy_pool_list(&zombies);
2340 }
2341
2342 struct list_head *
2343 kiblnd_pool_alloc_node(struct kib_poolset *ps)
2344 {
2345         struct list_head        *node;
2346         struct kib_pool *pool;
2347         int                     rc;
2348         unsigned int            interval = 1;
2349         ktime_t time_before;
2350         unsigned int trips = 0;
2351
2352 again:
2353         spin_lock(&ps->ps_lock);
2354         list_for_each_entry(pool, &ps->ps_pool_list, po_list) {
2355                 if (list_empty(&pool->po_free_list))
2356                         continue;
2357
2358                 pool->po_allocated++;
2359                 pool->po_deadline = ktime_get_seconds() +
2360                                     IBLND_POOL_DEADLINE;
2361                 node = pool->po_free_list.next;
2362                 list_del(node);
2363
2364                 if (ps->ps_node_init != NULL) {
2365                         /* still hold the lock */
2366                         ps->ps_node_init(pool, node);
2367                 }
2368                 spin_unlock(&ps->ps_lock);
2369                 return node;
2370         }
2371
2372         /* no available tx pool and ... */
2373         if (ps->ps_increasing) {
2374                 /* another thread is allocating a new pool */
2375                 spin_unlock(&ps->ps_lock);
2376                 trips++;
2377                 CDEBUG(D_NET,
2378                        "Another thread is allocating new %s pool, waiting %d jiffies for her to complete. trips = %d\n",
2379                        ps->ps_name, interval, trips);
2380
2381                 schedule_timeout_interruptible(interval);
2382                 if (interval < cfs_time_seconds(1))
2383                         interval *= 2;
2384
2385                 goto again;
2386         }
2387
2388         if (ktime_get_seconds() < ps->ps_next_retry) {
2389                 /* someone failed recently */
2390                 spin_unlock(&ps->ps_lock);
2391                 return NULL;
2392         }
2393
2394         ps->ps_increasing = 1;
2395         spin_unlock(&ps->ps_lock);
2396
2397         CDEBUG(D_NET, "%s pool exhausted, allocate new pool\n", ps->ps_name);
2398         time_before = ktime_get();
2399         rc = ps->ps_pool_create(ps, ps->ps_pool_size, &pool);
2400         CDEBUG(D_NET, "ps_pool_create took %lld ms to complete\n",
2401                ktime_ms_delta(ktime_get(), time_before));
2402
2403         spin_lock(&ps->ps_lock);
2404         ps->ps_increasing = 0;
2405         if (rc == 0) {
2406                 list_add_tail(&pool->po_list, &ps->ps_pool_list);
2407         } else {
2408                 ps->ps_next_retry = ktime_get_seconds() + IBLND_POOL_RETRY;
2409                 CERROR("Can't allocate new %s pool because out of memory\n",
2410                        ps->ps_name);
2411         }
2412         spin_unlock(&ps->ps_lock);
2413
2414         goto again;
2415 }
2416
2417 static void
2418 kiblnd_destroy_tx_pool(struct kib_pool *pool)
2419 {
2420         struct kib_tx_pool *tpo = container_of(pool, struct kib_tx_pool,
2421                                                tpo_pool);
2422         int i;
2423
2424         LASSERT (pool->po_allocated == 0);
2425
2426         if (tpo->tpo_tx_pages != NULL) {
2427                 kiblnd_unmap_tx_pool(tpo);
2428                 kiblnd_free_pages(tpo->tpo_tx_pages);
2429         }
2430
2431         if (tpo->tpo_tx_descs == NULL)
2432                 goto out;
2433
2434         for (i = 0; i < pool->po_size; i++) {
2435                 struct kib_tx *tx = &tpo->tpo_tx_descs[i];
2436                 int       wrq_sge = *kiblnd_tunables.kib_wrq_sge;
2437
2438                 list_del(&tx->tx_list);
2439                 if (tx->tx_pages != NULL)
2440                         CFS_FREE_PTR_ARRAY(tx->tx_pages, LNET_MAX_IOV);
2441                 if (tx->tx_frags != NULL)
2442                         CFS_FREE_PTR_ARRAY(tx->tx_frags,
2443                                            IBLND_MAX_RDMA_FRAGS);
2444                 if (tx->tx_wrq != NULL)
2445                         CFS_FREE_PTR_ARRAY(tx->tx_wrq,
2446                                            IBLND_MAX_RDMA_FRAGS);
2447                 if (tx->tx_sge != NULL) {
2448                         /* +1 is for the lnet header/message itself */
2449                         CFS_FREE_PTR_ARRAY(tx->tx_sge,
2450                                            (IBLND_MAX_RDMA_FRAGS *
2451                                            wrq_sge + 1));
2452                 }
2453                 if (tx->tx_rd != NULL)
2454                         LIBCFS_FREE(tx->tx_rd,
2455                                     offsetof(struct kib_rdma_desc,
2456                                              rd_frags[IBLND_MAX_RDMA_FRAGS]));
2457         }
2458
2459         CFS_FREE_PTR_ARRAY(tpo->tpo_tx_descs, pool->po_size);
2460 out:
2461         kiblnd_fini_pool(pool);
2462         CFS_FREE_PTR(tpo);
2463 }
2464
2465 static int kiblnd_tx_pool_size(struct lnet_ni *ni, int ncpts)
2466 {
2467         struct lnet_ioctl_config_o2iblnd_tunables *tunables;
2468         int ntx;
2469
2470         tunables = &ni->ni_lnd_tunables.lnd_tun_u.lnd_o2ib;
2471         ntx = tunables->lnd_ntx / ncpts;
2472
2473         return max(IBLND_TX_POOL, ntx);
2474 }
2475
2476 static int
2477 kiblnd_create_tx_pool(struct kib_poolset *ps, int size, struct kib_pool **pp_po)
2478 {
2479         int            i;
2480         int            npg;
2481         struct kib_pool *pool;
2482         struct kib_tx_pool *tpo;
2483
2484         LIBCFS_CPT_ALLOC(tpo, lnet_cpt_table(), ps->ps_cpt, sizeof(*tpo));
2485         if (tpo == NULL) {
2486                 CERROR("Failed to allocate TX pool\n");
2487                 return -ENOMEM;
2488         }
2489
2490         pool = &tpo->tpo_pool;
2491         kiblnd_init_pool(ps, pool, size);
2492         tpo->tpo_tx_descs = NULL;
2493         tpo->tpo_tx_pages = NULL;
2494
2495         npg = (size * IBLND_MSG_SIZE + PAGE_SIZE - 1) / PAGE_SIZE;
2496         if (kiblnd_alloc_pages(&tpo->tpo_tx_pages, ps->ps_cpt, npg) != 0) {
2497                 CERROR("Can't allocate tx pages: %d\n", npg);
2498                 CFS_FREE_PTR(tpo);
2499                 return -ENOMEM;
2500         }
2501
2502         LIBCFS_CPT_ALLOC(tpo->tpo_tx_descs, lnet_cpt_table(), ps->ps_cpt,
2503                          size * sizeof(struct kib_tx));
2504         if (tpo->tpo_tx_descs == NULL) {
2505                 CERROR("Can't allocate %d tx descriptors\n", size);
2506                 ps->ps_pool_destroy(pool);
2507                 return -ENOMEM;
2508         }
2509
2510         memset(tpo->tpo_tx_descs, 0, size * sizeof(struct kib_tx));
2511
2512         for (i = 0; i < size; i++) {
2513                 struct kib_tx *tx = &tpo->tpo_tx_descs[i];
2514                 int       wrq_sge = *kiblnd_tunables.kib_wrq_sge;
2515
2516                 tx->tx_pool = tpo;
2517                 if (ps->ps_net->ibn_fmr_ps != NULL) {
2518                         LIBCFS_CPT_ALLOC(tx->tx_pages,
2519                                          lnet_cpt_table(), ps->ps_cpt,
2520                                          LNET_MAX_IOV * sizeof(*tx->tx_pages));
2521                         if (tx->tx_pages == NULL)
2522                                 break;
2523                 }
2524
2525                 LIBCFS_CPT_ALLOC(tx->tx_frags, lnet_cpt_table(), ps->ps_cpt,
2526                                  IBLND_MAX_RDMA_FRAGS *
2527                                  sizeof(*tx->tx_frags));
2528                 if (tx->tx_frags == NULL)
2529                         break;
2530
2531                 sg_init_table(tx->tx_frags, IBLND_MAX_RDMA_FRAGS);
2532
2533                 LIBCFS_CPT_ALLOC(tx->tx_wrq, lnet_cpt_table(), ps->ps_cpt,
2534                                  IBLND_MAX_RDMA_FRAGS *
2535                                  sizeof(*tx->tx_wrq));
2536                 if (tx->tx_wrq == NULL)
2537                         break;
2538
2539                 /* +1 is for the lnet header/message itself */
2540                 LIBCFS_CPT_ALLOC(tx->tx_sge, lnet_cpt_table(), ps->ps_cpt,
2541                                  (IBLND_MAX_RDMA_FRAGS * wrq_sge + 1) *
2542                                  sizeof(*tx->tx_sge));
2543                 if (tx->tx_sge == NULL)
2544                         break;
2545
2546                 LIBCFS_CPT_ALLOC(tx->tx_rd, lnet_cpt_table(), ps->ps_cpt,
2547                                  offsetof(struct kib_rdma_desc,
2548                                           rd_frags[IBLND_MAX_RDMA_FRAGS]));
2549                 if (tx->tx_rd == NULL)
2550                         break;
2551         }
2552
2553         if (i == size) {
2554                 kiblnd_map_tx_pool(tpo);
2555                 *pp_po = pool;
2556                 return 0;
2557         }
2558
2559         ps->ps_pool_destroy(pool);
2560         return -ENOMEM;
2561 }
2562
2563 static void
2564 kiblnd_tx_init(struct kib_pool *pool, struct list_head *node)
2565 {
2566         struct kib_tx_poolset *tps = container_of(pool->po_owner,
2567                                                   struct kib_tx_poolset,
2568                                                   tps_poolset);
2569         struct kib_tx *tx  = list_entry(node, struct kib_tx, tx_list);
2570
2571         tx->tx_cookie = tps->tps_next_tx_cookie++;
2572 }
2573
2574 static void
2575 kiblnd_net_fini_pools(struct kib_net *net)
2576 {
2577         int     i;
2578
2579         cfs_cpt_for_each(i, lnet_cpt_table()) {
2580                 struct kib_tx_poolset *tps;
2581                 struct kib_fmr_poolset *fps;
2582
2583                 if (net->ibn_tx_ps != NULL) {
2584                         tps = net->ibn_tx_ps[i];
2585                         kiblnd_fini_poolset(&tps->tps_poolset);
2586                 }
2587
2588                 if (net->ibn_fmr_ps != NULL) {
2589                         fps = net->ibn_fmr_ps[i];
2590                         kiblnd_fini_fmr_poolset(fps);
2591                 }
2592         }
2593
2594         if (net->ibn_tx_ps != NULL) {
2595                 cfs_percpt_free(net->ibn_tx_ps);
2596                 net->ibn_tx_ps = NULL;
2597         }
2598
2599         if (net->ibn_fmr_ps != NULL) {
2600                 cfs_percpt_free(net->ibn_fmr_ps);
2601                 net->ibn_fmr_ps = NULL;
2602         }
2603 }
2604
2605 static int
2606 kiblnd_net_init_pools(struct kib_net *net, struct lnet_ni *ni, __u32 *cpts,
2607                       int ncpts)
2608 {
2609         struct lnet_ioctl_config_o2iblnd_tunables *tunables;
2610 #ifdef HAVE_OFED_IB_GET_DMA_MR
2611         unsigned long   flags;
2612 #endif
2613         int             cpt;
2614         int             rc;
2615         int             i;
2616
2617         tunables = &ni->ni_lnd_tunables.lnd_tun_u.lnd_o2ib;
2618
2619 #ifdef HAVE_OFED_IB_GET_DMA_MR
2620         read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2621         /*
2622          * if lnd_map_on_demand is zero then we have effectively disabled
2623          * FMR or FastReg and we're using global memory regions
2624          * exclusively.
2625          */
2626         if (!tunables->lnd_map_on_demand) {
2627                 read_unlock_irqrestore(&kiblnd_data.kib_global_lock,
2628                                            flags);
2629                 goto create_tx_pool;
2630         }
2631
2632         read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2633 #endif
2634
2635         if (tunables->lnd_fmr_pool_size < tunables->lnd_ntx / 4) {
2636                 CERROR("Can't set fmr pool size (%d) < ntx / 4(%d)\n",
2637                        tunables->lnd_fmr_pool_size,
2638                        tunables->lnd_ntx / 4);
2639                 rc = -EINVAL;
2640                 goto failed;
2641         }
2642
2643         /* TX pool must be created later than FMR, see LU-2268
2644          * for details */
2645         LASSERT(net->ibn_tx_ps == NULL);
2646
2647         /* premapping can fail if ibd_nmr > 1, so we always create
2648          * FMR pool and map-on-demand if premapping failed */
2649
2650         net->ibn_fmr_ps = cfs_percpt_alloc(lnet_cpt_table(),
2651                                            sizeof(struct kib_fmr_poolset));
2652         if (net->ibn_fmr_ps == NULL) {
2653                 CERROR("Failed to allocate FMR pool array\n");
2654                 rc = -ENOMEM;
2655                 goto failed;
2656         }
2657
2658         for (i = 0; i < ncpts; i++) {
2659                 cpt = (cpts == NULL) ? i : cpts[i];
2660                 rc = kiblnd_init_fmr_poolset(net->ibn_fmr_ps[cpt], cpt, ncpts,
2661                                              net, tunables);
2662                 if (rc != 0) {
2663                         CERROR("Can't initialize FMR pool for CPT %d: %d\n",
2664                                cpt, rc);
2665                         goto failed;
2666                 }
2667         }
2668
2669         if (i > 0)
2670                 LASSERT(i == ncpts);
2671
2672 #ifdef HAVE_OFED_IB_GET_DMA_MR
2673  create_tx_pool:
2674 #endif
2675         net->ibn_tx_ps = cfs_percpt_alloc(lnet_cpt_table(),
2676                                           sizeof(struct kib_tx_poolset));
2677         if (net->ibn_tx_ps == NULL) {
2678                 CERROR("Failed to allocate tx pool array\n");
2679                 rc = -ENOMEM;
2680                 goto failed;
2681         }
2682
2683         for (i = 0; i < ncpts; i++) {
2684                 cpt = (cpts == NULL) ? i : cpts[i];
2685                 rc = kiblnd_init_poolset(&net->ibn_tx_ps[cpt]->tps_poolset,
2686                                          cpt, net, "TX",
2687                                          kiblnd_tx_pool_size(ni, ncpts),
2688                                          kiblnd_create_tx_pool,
2689                                          kiblnd_destroy_tx_pool,
2690                                          kiblnd_tx_init, NULL);
2691                 if (rc != 0) {
2692                         CERROR("Can't initialize TX pool for CPT %d: %d\n",
2693                                cpt, rc);
2694                         goto failed;
2695                 }
2696         }
2697
2698         return 0;
2699  failed:
2700         kiblnd_net_fini_pools(net);
2701         LASSERT(rc != 0);
2702         return rc;
2703 }
2704
2705 static int
2706 kiblnd_port_get_attr(struct kib_hca_dev *hdev)
2707 {
2708         struct ib_port_attr *port_attr;
2709         int rc;
2710         unsigned long flags;
2711         rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
2712
2713         LIBCFS_ALLOC(port_attr, sizeof(*port_attr));
2714         if (port_attr == NULL) {
2715                 CDEBUG(D_NETERROR, "Out of memory\n");
2716                 return -ENOMEM;
2717         }
2718
2719         rc = ib_query_port(hdev->ibh_ibdev, hdev->ibh_port, port_attr);
2720
2721         write_lock_irqsave(g_lock, flags);
2722
2723         if (rc == 0)
2724                 hdev->ibh_state = port_attr->state == IB_PORT_ACTIVE
2725                                  ? IBLND_DEV_PORT_ACTIVE
2726                                  : IBLND_DEV_PORT_DOWN;
2727
2728         write_unlock_irqrestore(g_lock, flags);
2729         LIBCFS_FREE(port_attr, sizeof(*port_attr));
2730
2731         if (rc != 0) {
2732                 CDEBUG(D_NETERROR, "Failed to query IB port: %d\n", rc);
2733                 return rc;
2734         }
2735         return 0;
2736 }
2737
2738 static inline void
2739 kiblnd_set_ni_fatal_on(struct kib_hca_dev *hdev, int val)
2740 {
2741         struct kib_net  *net;
2742         __u32 ni_state_before;
2743         bool update_ping_buf = false;
2744         struct lnet_ni *ni = NULL;
2745
2746         /* for health check */
2747         list_for_each_entry(net, &hdev->ibh_dev->ibd_nets, ibn_list) {
2748                 ni = net->ibn_ni;
2749                 if (val)
2750                         CDEBUG(D_NETERROR, "Fatal device error for NI %s\n",
2751                                         libcfs_nidstr(&ni->ni_nid));
2752                 ni_state_before = lnet_set_link_fatal_state(ni, val);
2753
2754                 if (!update_ping_buf &&
2755                     (ni->ni_state == LNET_NI_STATE_ACTIVE) &&
2756                     (val != ni_state_before) &&
2757                     (net->ibn_init == IBLND_INIT_ALL))
2758                         update_ping_buf = true;
2759         }
2760
2761         if (update_ping_buf)
2762                 lnet_mark_ping_buffer_for_update();
2763 }
2764
2765 static void
2766 kiblnd_event_handler(struct ib_event_handler *handler, struct ib_event *event)
2767 {
2768         rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
2769         struct kib_hca_dev  *hdev;
2770         unsigned long flags;
2771
2772         hdev = container_of(handler, struct kib_hca_dev, ibh_event_handler);
2773
2774         write_lock_irqsave(g_lock, flags);
2775
2776         switch (event->event) {
2777         case IB_EVENT_DEVICE_FATAL:
2778                 CDEBUG(D_NET, "IB device fatal\n");
2779                 hdev->ibh_state = IBLND_DEV_FATAL;
2780                 kiblnd_set_ni_fatal_on(hdev, 1);
2781                 break;
2782         case IB_EVENT_PORT_ACTIVE:
2783                 CDEBUG(D_NET, "IB port active\n");
2784                 if (event->element.port_num == hdev->ibh_port) {
2785                         hdev->ibh_state = IBLND_DEV_PORT_ACTIVE;
2786                         kiblnd_set_ni_fatal_on(hdev, 0);
2787                 }
2788                 break;
2789         case IB_EVENT_PORT_ERR:
2790                 CDEBUG(D_NET, "IB port err\n");
2791                 if (event->element.port_num == hdev->ibh_port) {
2792                         hdev->ibh_state = IBLND_DEV_PORT_DOWN;
2793                         kiblnd_set_ni_fatal_on(hdev, 1);
2794                 }
2795                 break;
2796         default:
2797                 break;
2798         }
2799         write_unlock_irqrestore(g_lock, flags);
2800 }
2801
2802 static int
2803 kiblnd_hdev_get_attr(struct kib_hca_dev *hdev)
2804 {
2805         struct ib_device_attr *dev_attr;
2806         int rc = 0;
2807         int rc2 = 0;
2808
2809         /* It's safe to assume a HCA can handle a page size
2810          * matching that of the native system */
2811         hdev->ibh_page_shift = PAGE_SHIFT;
2812         hdev->ibh_page_size  = 1 << PAGE_SHIFT;
2813         hdev->ibh_page_mask  = ~((__u64)hdev->ibh_page_size - 1);
2814
2815 #ifndef HAVE_OFED_IB_DEVICE_ATTRS
2816         LIBCFS_ALLOC(dev_attr, sizeof(*dev_attr));
2817         if (dev_attr == NULL) {
2818                 CERROR("Out of memory\n");
2819                 return -ENOMEM;
2820         }
2821
2822         rc = ib_query_device(hdev->ibh_ibdev, dev_attr);
2823         if (rc != 0) {
2824                 CERROR("Failed to query IB device: %d\n", rc);
2825                 goto out_clean_attr;
2826         }
2827 #else
2828         dev_attr = &hdev->ibh_ibdev->attrs;
2829 #endif
2830
2831         hdev->ibh_mr_size = dev_attr->max_mr_size;
2832         hdev->ibh_max_qp_wr = dev_attr->max_qp_wr;
2833
2834         /* Setup device Memory Registration capabilities */
2835 #ifdef HAVE_OFED_FMR_POOL_API
2836 #ifdef HAVE_OFED_IB_DEVICE_OPS
2837         if (hdev->ibh_ibdev->ops.alloc_fmr &&
2838             hdev->ibh_ibdev->ops.dealloc_fmr &&
2839             hdev->ibh_ibdev->ops.map_phys_fmr &&
2840             hdev->ibh_ibdev->ops.unmap_fmr) {
2841 #else
2842         if (hdev->ibh_ibdev->alloc_fmr &&
2843             hdev->ibh_ibdev->dealloc_fmr &&
2844             hdev->ibh_ibdev->map_phys_fmr &&
2845             hdev->ibh_ibdev->unmap_fmr) {
2846 #endif
2847                 LCONSOLE_INFO("Using FMR for registration\n");
2848                 hdev->ibh_dev->ibd_dev_caps |= IBLND_DEV_CAPS_FMR_ENABLED;
2849         } else
2850 #endif /* HAVE_OFED_FMR_POOL_API */
2851         if (dev_attr->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS) {
2852                 LCONSOLE_INFO("Using FastReg for registration\n");
2853                 hdev->ibh_dev->ibd_dev_caps |= IBLND_DEV_CAPS_FASTREG_ENABLED;
2854 #ifndef HAVE_OFED_IB_ALLOC_FAST_REG_MR
2855 #ifdef IB_DEVICE_SG_GAPS_REG
2856                 if (dev_attr->device_cap_flags & IB_DEVICE_SG_GAPS_REG)
2857                         hdev->ibh_dev->ibd_dev_caps |= IBLND_DEV_CAPS_FASTREG_GAPS_SUPPORT;
2858 #endif
2859 #endif
2860         } else {
2861                 rc = -ENOSYS;
2862         }
2863
2864         rc2 = kiblnd_port_get_attr(hdev);
2865         if (rc2 != 0)
2866                 return rc2;
2867
2868         if (rc != 0)
2869                 rc = -EINVAL;
2870
2871 #ifndef HAVE_OFED_IB_DEVICE_ATTRS
2872 out_clean_attr:
2873         LIBCFS_FREE(dev_attr, sizeof(*dev_attr));
2874 #endif
2875
2876         if (rc == -ENOSYS)
2877                 CERROR("IB device does not support FMRs nor FastRegs, can't "
2878                        "register memory: %d\n", rc);
2879         else if (rc == -EINVAL)
2880                 CERROR("Invalid mr size: %#llx\n", hdev->ibh_mr_size);
2881         return rc;
2882 }
2883
2884 #ifdef HAVE_OFED_IB_GET_DMA_MR
2885 static void
2886 kiblnd_hdev_cleanup_mrs(struct kib_hca_dev *hdev)
2887 {
2888         if (hdev->ibh_mrs == NULL)
2889                 return;
2890
2891         ib_dereg_mr(hdev->ibh_mrs);
2892
2893         hdev->ibh_mrs = NULL;
2894 }
2895 #endif
2896
2897 void
2898 kiblnd_hdev_destroy(struct kib_hca_dev *hdev)
2899 {
2900         if (hdev->ibh_event_handler.device != NULL)
2901                 ib_unregister_event_handler(&hdev->ibh_event_handler);
2902
2903 #ifdef HAVE_OFED_IB_GET_DMA_MR
2904         kiblnd_hdev_cleanup_mrs(hdev);
2905 #endif
2906
2907         if (hdev->ibh_pd != NULL)
2908                 ib_dealloc_pd(hdev->ibh_pd);
2909
2910         if (hdev->ibh_cmid != NULL)
2911                 rdma_destroy_id(hdev->ibh_cmid);
2912
2913         LIBCFS_FREE(hdev, sizeof(*hdev));
2914 }
2915
2916 #ifdef HAVE_OFED_IB_GET_DMA_MR
2917 static int
2918 kiblnd_hdev_setup_mrs(struct kib_hca_dev *hdev)
2919 {
2920         struct ib_mr *mr;
2921         int           acflags = IB_ACCESS_LOCAL_WRITE |
2922                                 IB_ACCESS_REMOTE_WRITE;
2923
2924         mr = ib_get_dma_mr(hdev->ibh_pd, acflags);
2925         if (IS_ERR(mr)) {
2926                 CERROR("Failed ib_get_dma_mr: %ld\n", PTR_ERR(mr));
2927                 kiblnd_hdev_cleanup_mrs(hdev);
2928                 return PTR_ERR(mr);
2929         }
2930
2931         hdev->ibh_mrs = mr;
2932
2933         return 0;
2934 }
2935 #endif
2936
2937 static int
2938 kiblnd_dummy_callback(struct rdma_cm_id *cmid, struct rdma_cm_event *event)
2939 {       /* DUMMY */
2940         return 0;
2941 }
2942
2943 static int
2944 kiblnd_dev_need_failover(struct kib_dev *dev, struct net *ns)
2945 {
2946         struct rdma_cm_id  *cmid;
2947         struct sockaddr_in  srcaddr;
2948         struct sockaddr_in  dstaddr;
2949         int                 rc;
2950
2951         if (dev->ibd_hdev == NULL || /* initializing */
2952             dev->ibd_hdev->ibh_cmid == NULL || /* listener is dead */
2953             *kiblnd_tunables.kib_dev_failover > 1) /* debugging */
2954                 return 1;
2955
2956         /* XXX: it's UGLY, but I don't have better way to find
2957          * ib-bonding HCA failover because:
2958          *
2959          * a. no reliable CM event for HCA failover...
2960          * b. no OFED API to get ib_device for current net_device...
2961          *
2962          * We have only two choices at this point:
2963          *
2964          * a. rdma_bind_addr(), it will conflict with listener cmid
2965          * b. rdma_resolve_addr() to zero addr */
2966         cmid = kiblnd_rdma_create_id(ns, kiblnd_dummy_callback, dev,
2967                                      RDMA_PS_TCP, IB_QPT_RC);
2968         if (IS_ERR(cmid)) {
2969                 rc = PTR_ERR(cmid);
2970                 CERROR("Failed to create cmid for failover: %d\n", rc);
2971                 return rc;
2972         }
2973
2974         memset(&srcaddr, 0, sizeof(srcaddr));
2975         srcaddr.sin_family      = AF_INET;
2976         srcaddr.sin_addr.s_addr = (__force u32)htonl(dev->ibd_ifip);
2977
2978         memset(&dstaddr, 0, sizeof(dstaddr));
2979         dstaddr.sin_family = AF_INET;
2980         rc = rdma_resolve_addr(cmid, (struct sockaddr *)&srcaddr,
2981                                (struct sockaddr *)&dstaddr, 1);
2982         if (rc != 0 || cmid->device == NULL) {
2983                 CERROR("Failed to bind %s:%pI4h to device(%p): %d\n",
2984                        dev->ibd_ifname, &dev->ibd_ifip,
2985                        cmid->device, rc);
2986                 rdma_destroy_id(cmid);
2987                 return rc;
2988         }
2989
2990         rc = dev->ibd_hdev->ibh_ibdev != cmid->device; /* true for failover */
2991         rdma_destroy_id(cmid);
2992         return rc;
2993 }
2994
2995 int
2996 kiblnd_dev_failover(struct kib_dev *dev, struct net *ns)
2997 {
2998         LIST_HEAD(zombie_tpo);
2999         LIST_HEAD(zombie_ppo);
3000         LIST_HEAD(zombie_fpo);
3001         struct rdma_cm_id  *cmid  = NULL;
3002         struct kib_hca_dev *hdev  = NULL;
3003         struct kib_hca_dev *old;
3004         struct ib_pd       *pd;
3005         struct kib_net *net;
3006         struct sockaddr_in  addr;
3007         struct net_device *netdev;
3008         unsigned long       flags;
3009         int                 rc = 0;
3010         int                 i;
3011         bool                set_fatal = true;
3012
3013         LASSERT(*kiblnd_tunables.kib_dev_failover > 1 ||
3014                 dev->ibd_can_failover ||
3015                 dev->ibd_hdev == NULL);
3016
3017         rc = kiblnd_dev_need_failover(dev, ns);
3018         if (rc <= 0)
3019                 goto out;
3020
3021         if (dev->ibd_hdev != NULL &&
3022             dev->ibd_hdev->ibh_cmid != NULL) {
3023                 /* XXX it's not good to close old listener at here,
3024                  * because we can fail to create new listener.
3025                  * But we have to close it now, otherwise rdma_bind_addr
3026                  * will return EADDRINUSE... How crap! */
3027                 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
3028
3029                 cmid = dev->ibd_hdev->ibh_cmid;
3030                 /* make next schedule of kiblnd_dev_need_failover()
3031                  * return 1 for me */
3032                 dev->ibd_hdev->ibh_cmid  = NULL;
3033                 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
3034
3035                 rdma_destroy_id(cmid);
3036         }
3037
3038         cmid = kiblnd_rdma_create_id(ns, kiblnd_cm_callback, dev, RDMA_PS_TCP,
3039                                      IB_QPT_RC);
3040         if (IS_ERR(cmid)) {
3041                 rc = PTR_ERR(cmid);
3042                 CERROR("Failed to create cmid for failover: %d\n", rc);
3043                 goto out;
3044         }
3045
3046         memset(&addr, 0, sizeof(addr));
3047         addr.sin_family      = AF_INET;
3048         addr.sin_addr.s_addr = (__force u32)htonl(dev->ibd_ifip);
3049         addr.sin_port        = htons(*kiblnd_tunables.kib_service);
3050
3051         /* Bind to failover device or port */
3052         rc = rdma_bind_addr(cmid, (struct sockaddr *)&addr);
3053         if (rc != 0 || cmid->device == NULL) {
3054                 CERROR("Failed to bind %s:%pI4h to device(%p): %d\n",
3055                        dev->ibd_ifname, &dev->ibd_ifip,
3056                        cmid->device, rc);
3057                 if (!rc && !cmid->device)
3058                         set_fatal = false;
3059                 rdma_destroy_id(cmid);
3060                 goto out;
3061         }
3062
3063         LIBCFS_ALLOC(hdev, sizeof(*hdev));
3064         if (hdev == NULL) {
3065                 CERROR("Failed to allocate kib_hca_dev\n");
3066                 rdma_destroy_id(cmid);
3067                 rc = -ENOMEM;
3068                 goto out;
3069         }
3070
3071         atomic_set(&hdev->ibh_ref, 1);
3072         hdev->ibh_dev   = dev;
3073         hdev->ibh_cmid  = cmid;
3074         hdev->ibh_ibdev = cmid->device;
3075         hdev->ibh_port  = cmid->port_num;
3076
3077 #ifdef HAVE_OFED_IB_ALLOC_PD_2ARGS
3078         pd = ib_alloc_pd(cmid->device, 0);
3079 #else
3080         pd = ib_alloc_pd(cmid->device);
3081 #endif
3082         if (IS_ERR(pd)) {
3083                 rc = PTR_ERR(pd);
3084                 CERROR("Can't allocate PD: %d\n", rc);
3085                 goto out;
3086         }
3087
3088         hdev->ibh_pd = pd;
3089
3090         rc = rdma_listen(cmid, 0);
3091         if (rc != 0) {
3092                 CERROR("Can't start new listener: %d\n", rc);
3093                 goto out;
3094         }
3095
3096         rc = kiblnd_hdev_get_attr(hdev);
3097         if (rc != 0) {
3098                 CERROR("Can't get device attributes: %d\n", rc);
3099                 goto out;
3100         }
3101
3102 #ifdef HAVE_OFED_IB_GET_DMA_MR
3103         rc = kiblnd_hdev_setup_mrs(hdev);
3104         if (rc != 0) {
3105                 CERROR("Can't setup device: %d\n", rc);
3106                 goto out;
3107         }
3108 #endif
3109
3110         INIT_IB_EVENT_HANDLER(&hdev->ibh_event_handler,
3111                                 hdev->ibh_ibdev, kiblnd_event_handler);
3112         ib_register_event_handler(&hdev->ibh_event_handler);
3113
3114         write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
3115
3116         old = dev->ibd_hdev;
3117         dev->ibd_hdev = hdev;   /* take over the refcount */
3118         hdev = old;
3119
3120         list_for_each_entry(net, &dev->ibd_nets, ibn_list) {
3121                 cfs_cpt_for_each(i, lnet_cpt_table()) {
3122                         kiblnd_fail_poolset(&net->ibn_tx_ps[i]->tps_poolset,
3123                                             &zombie_tpo);
3124
3125                         if (net->ibn_fmr_ps != NULL)
3126                                 kiblnd_fail_fmr_poolset(net->ibn_fmr_ps[i],
3127                                                         &zombie_fpo);
3128                 }
3129         }
3130
3131         write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
3132  out:
3133         if (!list_empty(&zombie_tpo))
3134                 kiblnd_destroy_pool_list(&zombie_tpo);
3135         if (!list_empty(&zombie_ppo))
3136                 kiblnd_destroy_pool_list(&zombie_ppo);
3137         if (!list_empty(&zombie_fpo))
3138                 kiblnd_destroy_fmr_pool_list(&zombie_fpo);
3139         if (hdev != NULL)
3140                 kiblnd_hdev_decref(hdev);
3141
3142         if (rc != 0) {
3143                 dev->ibd_failed_failover++;
3144         } else {
3145                 dev->ibd_failed_failover = 0;
3146
3147                 if (set_fatal) {
3148                         rcu_read_lock();
3149                         netdev = dev_get_by_name_rcu(ns, dev->ibd_ifname);
3150                         if (netdev && (lnet_get_link_status(netdev) == 1))
3151                                 kiblnd_set_ni_fatal_on(dev->ibd_hdev, 0);
3152                         rcu_read_unlock();
3153                 }
3154         }
3155
3156         return rc;
3157 }
3158
3159 void
3160 kiblnd_destroy_dev(struct kib_dev *dev)
3161 {
3162         LASSERT(dev->ibd_nnets == 0);
3163         LASSERT(list_empty(&dev->ibd_nets));
3164
3165         list_del(&dev->ibd_fail_list);
3166         list_del(&dev->ibd_list);
3167
3168         if (dev->ibd_hdev != NULL)
3169                 kiblnd_hdev_decref(dev->ibd_hdev);
3170
3171         LIBCFS_FREE(dev, sizeof(*dev));
3172 }
3173
3174 static struct kib_dev *
3175 kiblnd_dev_search(char *ifname)
3176 {
3177         struct kib_dev *alias = NULL;
3178         struct kib_dev *dev;
3179         char *colon;
3180         char *colon2;
3181
3182         colon = strchr(ifname, ':');
3183         list_for_each_entry(dev, &kiblnd_data.kib_devs, ibd_list) {
3184                 if (strcmp(&dev->ibd_ifname[0], ifname) == 0)
3185                         return dev;
3186
3187                 if (alias != NULL)
3188                         continue;
3189
3190                 colon2 = strchr(dev->ibd_ifname, ':');
3191                 if (colon != NULL)
3192                         *colon = 0;
3193                 if (colon2 != NULL)
3194                         *colon2 = 0;
3195
3196                 if (strcmp(&dev->ibd_ifname[0], ifname) == 0)
3197                         alias = dev;
3198
3199                 if (colon != NULL)
3200                         *colon = ':';
3201                 if (colon2 != NULL)
3202                         *colon2 = ':';
3203         }
3204         return alias;
3205 }
3206
3207 static int
3208 kiblnd_handle_link_state_change(struct net_device *dev,
3209                                 unsigned char operstate)
3210 {
3211         struct lnet_ni *ni = NULL;
3212         struct kib_dev *event_kibdev;
3213         struct kib_net *net;
3214         struct kib_net *cnxt;
3215         bool link_down = !(operstate == IF_OPER_UP);
3216         struct in_device *in_dev;
3217         bool found_ip = false;
3218         __u32 ni_state_before;
3219         bool update_ping_buf = false;
3220         int state;
3221         DECLARE_CONST_IN_IFADDR(ifa);
3222
3223         event_kibdev = kiblnd_dev_search(dev->name);
3224
3225         if (!event_kibdev)
3226                 goto out;
3227
3228         list_for_each_entry_safe(net, cnxt, &event_kibdev->ibd_nets, ibn_list) {
3229                 found_ip = false;
3230                 ni = net->ibn_ni;
3231
3232                 in_dev = __in_dev_get_rtnl(dev);
3233                 if (!in_dev) {
3234                         CDEBUG(D_NET, "Interface %s has no IPv4 status.\n",
3235                                dev->name);
3236                         ni_state_before = lnet_set_link_fatal_state(ni, 1);
3237                         goto ni_done;
3238                 }
3239                 in_dev_for_each_ifa_rtnl(ifa, in_dev) {
3240                         if (htonl(event_kibdev->ibd_ifip) == ifa->ifa_local)
3241                                 found_ip = true;
3242                 }
3243                 endfor_ifa(in_dev);
3244
3245                 if (!found_ip) {
3246                         CDEBUG(D_NET, "Interface %s has no matching ip\n",
3247                                dev->name);
3248                         ni_state_before = lnet_set_link_fatal_state(ni, 1);
3249                         goto ni_done;
3250                 }
3251
3252                 if (link_down) {
3253                         ni_state_before = lnet_set_link_fatal_state(ni, 1);
3254                 } else {
3255                         state = (lnet_get_link_status(dev) == 0);
3256                         ni_state_before = lnet_set_link_fatal_state(ni,
3257                                                                     state);
3258                 }
3259 ni_done:
3260                 if (!update_ping_buf &&
3261                     (ni->ni_state == LNET_NI_STATE_ACTIVE) &&
3262                     (atomic_read(&ni->ni_fatal_error_on) != ni_state_before) &&
3263                     (net->ibn_init == IBLND_INIT_ALL))
3264                         update_ping_buf = true;
3265         }
3266
3267         if (update_ping_buf)
3268                 lnet_mark_ping_buffer_for_update();
3269 out:
3270         return 0;
3271 }
3272
3273 static int
3274 kiblnd_handle_inetaddr_change(struct in_ifaddr *ifa, unsigned long event)
3275 {
3276         struct kib_dev *event_kibdev;
3277         struct kib_net *net;
3278         struct kib_net *cnxt;
3279         struct net_device *event_netdev = ifa->ifa_dev->dev;
3280         __u32 ni_state_before;
3281         bool update_ping_buf = false;
3282         struct lnet_ni *ni = NULL;
3283         bool link_down;
3284
3285         event_kibdev = kiblnd_dev_search(event_netdev->name);
3286
3287         if (!event_kibdev)
3288                 goto out;
3289
3290         if (htonl(event_kibdev->ibd_ifip) != ifa->ifa_local)
3291                 goto out;
3292
3293         list_for_each_entry_safe(net, cnxt, &event_kibdev->ibd_nets,
3294                                  ibn_list) {
3295                 ni = net->ibn_ni;
3296                 link_down = (event == NETDEV_DOWN);
3297                 ni_state_before = lnet_set_link_fatal_state(ni, link_down);
3298                 if (!update_ping_buf &&
3299                     (ni->ni_state == LNET_NI_STATE_ACTIVE) &&
3300                     ((event == NETDEV_DOWN) != ni_state_before) &&
3301                     (net->ibn_init == IBLND_INIT_ALL))
3302                         update_ping_buf = true;
3303         }
3304
3305         if (update_ping_buf)
3306                 lnet_mark_ping_buffer_for_update();
3307 out:
3308         return 0;
3309 }
3310
3311
3312 /************************************
3313  * Net device notifier event handler
3314  ************************************/
3315 static int kiblnd_device_event(struct notifier_block *unused,
3316                                  unsigned long event, void *ptr)
3317 {
3318         struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3319         unsigned char operstate;
3320
3321         operstate = dev->operstate;
3322
3323         CDEBUG(D_NET, "devevent: status=%ld, iface=%s ifindex %d state %u\n",
3324                event, dev->name, dev->ifindex, operstate);
3325
3326         switch (event) {
3327         case NETDEV_UP:
3328         case NETDEV_DOWN:
3329         case NETDEV_CHANGE:
3330                 kiblnd_handle_link_state_change(dev, operstate);
3331                 break;
3332         }
3333
3334         return NOTIFY_OK;
3335 }
3336
3337 /************************************
3338  * Inetaddr notifier event handler
3339  ************************************/
3340 static int kiblnd_inetaddr_event(struct notifier_block *unused,
3341                                  unsigned long event, void *ptr)
3342 {
3343         struct in_ifaddr *ifa = ptr;
3344
3345         CDEBUG(D_NET, "addrevent: status %ld ip addr %pI4, netmask %pI4.\n",
3346                event, &ifa->ifa_address, &ifa->ifa_mask);
3347
3348         switch (event) {
3349         case NETDEV_UP:
3350         case NETDEV_DOWN:
3351         case NETDEV_CHANGE:
3352                 kiblnd_handle_inetaddr_change(ifa, event);
3353                 break;
3354
3355         }
3356         return NOTIFY_OK;
3357 }
3358
3359 static struct notifier_block kiblnd_dev_notifier_block = {
3360         .notifier_call = kiblnd_device_event,
3361 };
3362
3363 static struct notifier_block kiblnd_inetaddr_notifier_block = {
3364         .notifier_call = kiblnd_inetaddr_event,
3365 };
3366
3367 static void
3368 kiblnd_base_shutdown(void)
3369 {
3370         struct kib_sched_info *sched;
3371         struct kib_peer_ni *peer_ni;
3372         int i;
3373
3374         LASSERT(list_empty(&kiblnd_data.kib_devs));
3375
3376         CDEBUG(D_MALLOC, "before LND base cleanup: kmem %lld\n",
3377                libcfs_kmem_read());
3378
3379         if (kiblnd_data.kib_init == IBLND_INIT_ALL) {
3380                 unregister_netdevice_notifier(&kiblnd_dev_notifier_block);
3381                 unregister_inetaddr_notifier(&kiblnd_inetaddr_notifier_block);
3382         }
3383
3384         switch (kiblnd_data.kib_init) {
3385         default:
3386                 LBUG();
3387
3388         case IBLND_INIT_ALL:
3389         case IBLND_INIT_DATA:
3390                 hash_for_each(kiblnd_data.kib_peers, i, peer_ni, ibp_list)
3391                         LASSERT(0);
3392                 LASSERT(list_empty(&kiblnd_data.kib_connd_zombies));
3393                 LASSERT(list_empty(&kiblnd_data.kib_connd_conns));
3394                 LASSERT(list_empty(&kiblnd_data.kib_reconn_list));
3395                 LASSERT(list_empty(&kiblnd_data.kib_reconn_wait));
3396
3397                 /* flag threads to terminate; wake and wait for them to die */
3398                 kiblnd_data.kib_shutdown = 1;
3399
3400                 /* NB: we really want to stop scheduler threads net by net
3401                  * instead of the whole module, this should be improved
3402                  * with dynamic configuration LNet.
3403                  */
3404                 cfs_percpt_for_each(sched, i, kiblnd_data.kib_scheds)
3405                         wake_up_all(&sched->ibs_waitq);
3406
3407                 wake_up(&kiblnd_data.kib_connd_waitq);
3408                 wake_up(&kiblnd_data.kib_failover_waitq);
3409
3410                 wait_var_event_warning(&kiblnd_data.kib_nthreads,
3411                                        !atomic_read(&kiblnd_data.kib_nthreads),
3412                                        "Waiting for %d threads to terminate\n",
3413                                        atomic_read(&kiblnd_data.kib_nthreads));
3414                 fallthrough;
3415
3416         case IBLND_INIT_NOTHING:
3417                 break;
3418         }
3419
3420         if (kiblnd_data.kib_scheds != NULL)
3421                 cfs_percpt_free(kiblnd_data.kib_scheds);
3422
3423         CDEBUG(D_MALLOC, "after LND base cleanup: kmem %lld\n",
3424                libcfs_kmem_read());
3425
3426         kiblnd_data.kib_init = IBLND_INIT_NOTHING;
3427         module_put(THIS_MODULE);
3428 }
3429
3430 static void
3431 kiblnd_shutdown(struct lnet_ni *ni)
3432 {
3433         struct kib_net *net = ni->ni_data;
3434         rwlock_t     *g_lock = &kiblnd_data.kib_global_lock;
3435         unsigned long     flags;
3436
3437         LASSERT(kiblnd_data.kib_init == IBLND_INIT_ALL);
3438
3439         if (net == NULL)
3440                 goto out;
3441
3442         CDEBUG(D_MALLOC, "before LND net cleanup: kmem %lld\n",
3443                libcfs_kmem_read());
3444
3445         write_lock_irqsave(g_lock, flags);
3446         net->ibn_shutdown = 1;
3447         write_unlock_irqrestore(g_lock, flags);
3448
3449         switch (net->ibn_init) {
3450         default:
3451                 LBUG();
3452
3453         case IBLND_INIT_ALL:
3454                 /* nuke all existing peers within this net */
3455                 kiblnd_del_peer(ni, LNET_NID_ANY);
3456
3457                 /* Wait for all peer_ni state to clean up */
3458                 wait_var_event_warning(&net->ibn_npeers,
3459                                        atomic_read(&net->ibn_npeers) == 0,
3460                                        "%s: waiting for %d peers to disconnect\n",
3461                                        libcfs_nidstr(&ni->ni_nid),
3462                                        atomic_read(&net->ibn_npeers));
3463
3464                 kiblnd_net_fini_pools(net);
3465
3466                 write_lock_irqsave(g_lock, flags);
3467                 LASSERT(net->ibn_dev->ibd_nnets > 0);
3468                 net->ibn_dev->ibd_nnets--;
3469                 list_del(&net->ibn_list);
3470                 write_unlock_irqrestore(g_lock, flags);
3471
3472                 wake_up_all(&kiblnd_data.kib_connd_waitq);
3473                 wait_var_event_warning(&net->ibn_nconns,
3474                                        atomic_read(&net->ibn_nconns) == 0,
3475                                        "%s: waiting for %d conns to clean\n",
3476                                        libcfs_nidstr(&ni->ni_nid),
3477                                        atomic_read(&net->ibn_nconns));
3478                 fallthrough;
3479
3480         case IBLND_INIT_NOTHING:
3481                 LASSERT (atomic_read(&net->ibn_nconns) == 0);
3482
3483                 if (net->ibn_dev != NULL &&
3484                     net->ibn_dev->ibd_nnets == 0)
3485                         kiblnd_destroy_dev(net->ibn_dev);
3486
3487                 break;
3488         }
3489
3490         CDEBUG(D_MALLOC, "after LND net cleanup: kmem %lld\n",
3491                libcfs_kmem_read());
3492
3493         net->ibn_init = IBLND_INIT_NOTHING;
3494         ni->ni_data = NULL;
3495
3496         LIBCFS_FREE(net, sizeof(*net));
3497
3498 out:
3499         if (list_empty(&kiblnd_data.kib_devs))
3500                 kiblnd_base_shutdown();
3501 }
3502
3503 static int
3504 kiblnd_base_startup(struct net *ns)
3505 {
3506         struct kib_sched_info *sched;
3507         int rc;
3508         int i;
3509
3510         LASSERT(kiblnd_data.kib_init == IBLND_INIT_NOTHING);
3511
3512         if (!try_module_get(THIS_MODULE))
3513                 goto failed;
3514
3515         memset(&kiblnd_data, 0, sizeof(kiblnd_data)); /* zero pointers, flags etc */
3516
3517         rwlock_init(&kiblnd_data.kib_global_lock);
3518
3519         INIT_LIST_HEAD(&kiblnd_data.kib_devs);
3520         INIT_LIST_HEAD(&kiblnd_data.kib_failed_devs);
3521
3522         hash_init(kiblnd_data.kib_peers);
3523
3524         spin_lock_init(&kiblnd_data.kib_connd_lock);
3525         INIT_LIST_HEAD(&kiblnd_data.kib_connd_conns);
3526         INIT_LIST_HEAD(&kiblnd_data.kib_connd_waits);
3527         INIT_LIST_HEAD(&kiblnd_data.kib_connd_zombies);
3528         INIT_LIST_HEAD(&kiblnd_data.kib_reconn_list);
3529         INIT_LIST_HEAD(&kiblnd_data.kib_reconn_wait);
3530
3531         init_waitqueue_head(&kiblnd_data.kib_connd_waitq);
3532         init_waitqueue_head(&kiblnd_data.kib_failover_waitq);
3533
3534         kiblnd_data.kib_scheds = cfs_percpt_alloc(lnet_cpt_table(),
3535                                                   sizeof(*sched));
3536         if (kiblnd_data.kib_scheds == NULL)
3537                 goto failed;
3538
3539         cfs_percpt_for_each(sched, i, kiblnd_data.kib_scheds) {
3540                 int     nthrs;
3541
3542                 spin_lock_init(&sched->ibs_lock);
3543                 INIT_LIST_HEAD(&sched->ibs_conns);
3544                 init_waitqueue_head(&sched->ibs_waitq);
3545
3546                 nthrs = cfs_cpt_weight(lnet_cpt_table(), i);
3547                 if (*kiblnd_tunables.kib_nscheds > 0) {
3548                         nthrs = min(nthrs, *kiblnd_tunables.kib_nscheds);
3549                 } else {
3550                         /* max to half of CPUs, another half is reserved for
3551                          * upper layer modules */
3552                         nthrs = min(max(IBLND_N_SCHED, nthrs >> 1), nthrs);
3553                 }
3554
3555                 sched->ibs_nthreads_max = nthrs;
3556                 sched->ibs_cpt = i;
3557         }
3558
3559         kiblnd_data.kib_error_qpa.qp_state = IB_QPS_ERR;
3560
3561         /* lists/ptrs/locks initialised */
3562         kiblnd_data.kib_init = IBLND_INIT_DATA;
3563         /*****************************************************/
3564
3565         rc = kiblnd_thread_start(kiblnd_connd, NULL, "kiblnd_connd");
3566         if (rc != 0) {
3567                 CERROR("Can't spawn o2iblnd connd: %d\n", rc);
3568                 goto failed;
3569         }
3570
3571         if (*kiblnd_tunables.kib_dev_failover != 0)
3572                 rc = kiblnd_thread_start(kiblnd_failover_thread, ns,
3573                                          "kiblnd_failover");
3574
3575         if (rc != 0) {
3576                 CERROR("Can't spawn o2iblnd failover thread: %d\n", rc);
3577                 goto failed;
3578         }
3579
3580         register_netdevice_notifier(&kiblnd_dev_notifier_block);
3581         register_inetaddr_notifier(&kiblnd_inetaddr_notifier_block);
3582
3583         /* flag everything initialised */
3584         kiblnd_data.kib_init = IBLND_INIT_ALL;
3585         /*****************************************************/
3586
3587         return 0;
3588
3589  failed:
3590         kiblnd_base_shutdown();
3591         return -ENETDOWN;
3592 }
3593
3594 static int
3595 kiblnd_start_schedulers(struct kib_sched_info *sched)
3596 {
3597         int     rc = 0;
3598         int     nthrs;
3599         int     i;
3600
3601         if (sched->ibs_nthreads == 0) {
3602                 if (*kiblnd_tunables.kib_nscheds > 0) {
3603                         nthrs = sched->ibs_nthreads_max;
3604                 } else {
3605                         nthrs = cfs_cpt_weight(lnet_cpt_table(),
3606                                                sched->ibs_cpt);
3607                         nthrs = min(max(IBLND_N_SCHED, nthrs >> 1), nthrs);
3608                         nthrs = min(IBLND_N_SCHED_HIGH, nthrs);
3609                 }
3610         } else {
3611                 LASSERT(sched->ibs_nthreads <= sched->ibs_nthreads_max);
3612                 /* increase one thread if there is new interface */
3613                 nthrs = (sched->ibs_nthreads < sched->ibs_nthreads_max);
3614         }
3615
3616         for (i = 0; i < nthrs; i++) {
3617                 long    id = KIB_THREAD_ID(sched->ibs_cpt, sched->ibs_nthreads + i);
3618
3619                 rc = kiblnd_thread_start(kiblnd_scheduler, (void *)id,
3620                                          "kiblnd_sd_%02ld_%02ld",
3621                                          KIB_THREAD_CPT(id), KIB_THREAD_TID(id));
3622                 if (rc == 0)
3623                         continue;
3624
3625                 CERROR("Can't spawn thread %d for scheduler[%d]: %d\n",
3626                        sched->ibs_cpt, sched->ibs_nthreads + i, rc);
3627                 break;
3628         }
3629
3630         sched->ibs_nthreads += i;
3631         return rc;
3632 }
3633
3634 static int kiblnd_dev_start_threads(struct kib_dev *dev, bool newdev, u32 *cpts,
3635                                     int ncpts)
3636 {
3637         int     cpt;
3638         int     rc;
3639         int     i;
3640
3641         for (i = 0; i < ncpts; i++) {
3642                 struct kib_sched_info *sched;
3643
3644                 cpt = (cpts == NULL) ? i : cpts[i];
3645                 sched = kiblnd_data.kib_scheds[cpt];
3646
3647                 if (!newdev && sched->ibs_nthreads > 0)
3648                         continue;
3649
3650                 rc = kiblnd_start_schedulers(kiblnd_data.kib_scheds[cpt]);
3651                 if (rc != 0) {
3652                         CERROR("Failed to start scheduler threads for %s\n",
3653                                dev->ibd_ifname);
3654                         return rc;
3655                 }
3656         }
3657         return 0;
3658 }
3659
3660 static int
3661 kiblnd_startup(struct lnet_ni *ni)
3662 {
3663         char *ifname = NULL;
3664         struct lnet_inetdev *ifaces = NULL;
3665         struct kib_dev *ibdev = NULL;
3666         struct kib_net *net = NULL;
3667         unsigned long flags;
3668         int rc;
3669         int i;
3670         bool newdev;
3671         struct net_device *netdev;
3672
3673         LASSERT(ni->ni_net->net_lnd == &the_o2iblnd);
3674
3675         if (kiblnd_data.kib_init == IBLND_INIT_NOTHING) {
3676                 rc = kiblnd_base_startup(ni->ni_net_ns);
3677                 if (rc != 0)
3678                         return rc;
3679         }
3680
3681         LIBCFS_ALLOC(net, sizeof(*net));
3682         ni->ni_data = net;
3683         if (net == NULL) {
3684                 rc = -ENOMEM;
3685                 goto failed;
3686         }
3687
3688         net->ibn_ni = ni;
3689         net->ibn_incarnation = ktime_get_real_ns() / NSEC_PER_USEC;
3690
3691         kiblnd_tunables_setup(ni);
3692
3693         /* Multi-Rail wants each secondary
3694          * IP to be treated as an unique 'struct ni' interface.
3695          */
3696         if (ni->ni_interface != NULL) {
3697                 /* Use the IPoIB interface specified in 'networks=' */
3698                 ifname = ni->ni_interface;
3699         } else {
3700                 ifname = *kiblnd_tunables.kib_default_ipif;
3701                 rc = libcfs_strnid(&ni->ni_nid, ifname);
3702                 if (rc < 0 || ni->ni_nid.nid_type != O2IBLND)
3703                         memset(&ni->ni_nid, 0, sizeof(ni->ni_nid));
3704         }
3705
3706         if (strlen(ifname) >= sizeof(ibdev->ibd_ifname)) {
3707                 CERROR("IPoIB interface name too long: %s\n", ifname);
3708                 rc = -E2BIG;
3709                 goto failed;
3710         }
3711
3712         rc = lnet_inet_enumerate(&ifaces, ni->ni_net_ns, false);
3713         if (rc < 0)
3714                 goto failed;
3715
3716         i = lnet_inet_select(ni, ifaces, rc);
3717         if (i < 0)
3718                 goto failed;
3719
3720         if (nid_addr_is_set(&ni->ni_nid)) {
3721                 strscpy(ifname, ifaces[i].li_name, sizeof(ifname));
3722         } else if (strcmp(ifname, ifaces[i].li_name) != 0) {
3723                 CERROR("ko2iblnd: No matching interfaces\n");
3724                 rc = -ENOENT;
3725                 goto failed;
3726         }
3727
3728         ibdev = kiblnd_dev_search(ifname);
3729         newdev = ibdev == NULL;
3730         /* hmm...create kib_dev even for alias */
3731         if (ibdev == NULL || strcmp(&ibdev->ibd_ifname[0], ifname) != 0) {
3732                 LIBCFS_ALLOC(ibdev, sizeof(*ibdev));
3733                 if (!ibdev) {
3734                         rc = -ENOMEM;
3735                         goto failed;
3736                 }
3737
3738                 ibdev->ibd_ifip = ntohl(ifaces[i].li_ipaddr);
3739                 strscpy(ibdev->ibd_ifname, ifaces[i].li_name,
3740                         sizeof(ibdev->ibd_ifname));
3741                 ibdev->ibd_can_failover = ifaces[i].li_iff_master;
3742
3743                 INIT_LIST_HEAD(&ibdev->ibd_nets);
3744                 INIT_LIST_HEAD(&ibdev->ibd_list); /* not yet in kib_devs */
3745                 INIT_LIST_HEAD(&ibdev->ibd_fail_list);
3746
3747                 /* initialize the device */
3748                 rc = kiblnd_dev_failover(ibdev, ni->ni_net_ns);
3749                 if (rc) {
3750                         CERROR("ko2iblnd: Can't initialize device: rc = %d\n",
3751                                rc);
3752                         goto failed;
3753                 }
3754
3755                 list_add_tail(&ibdev->ibd_list, &kiblnd_data.kib_devs);
3756         }
3757
3758         net->ibn_dev = ibdev;
3759         ni->ni_nid.nid_addr[0] = cpu_to_be32(ibdev->ibd_ifip);
3760         if (!ni->ni_interface) {
3761                 rc = lnet_ni_add_interface(ni, ifaces[i].li_name);
3762                 if (rc < 0)
3763                         CWARN("ko2iblnd failed to allocate ni_interface\n");
3764         }
3765         ni->ni_dev_cpt = ifaces[i].li_cpt;
3766
3767         rc = kiblnd_dev_start_threads(ibdev, newdev, ni->ni_cpts, ni->ni_ncpts);
3768         if (rc != 0)
3769                 goto failed;
3770
3771         rc = kiblnd_net_init_pools(net, ni, ni->ni_cpts, ni->ni_ncpts);
3772         if (rc != 0) {
3773                 CERROR("Failed to initialize NI pools: %d\n", rc);
3774                 goto failed;
3775         }
3776
3777         write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
3778         ibdev->ibd_nnets++;
3779         list_add_tail(&net->ibn_list, &ibdev->ibd_nets);
3780         /* for health check */
3781         if (ibdev->ibd_hdev->ibh_state == IBLND_DEV_PORT_DOWN)
3782                 kiblnd_set_ni_fatal_on(ibdev->ibd_hdev, 1);
3783
3784         rcu_read_lock();
3785         netdev = dev_get_by_name_rcu(ni->ni_net_ns, net->ibn_dev->ibd_ifname);
3786         if (netdev &&
3787             ((netdev->reg_state == NETREG_UNREGISTERING) ||
3788              (netdev->operstate != IF_OPER_UP) ||
3789             (lnet_get_link_status(netdev) == 0))) {
3790                 kiblnd_set_ni_fatal_on(ibdev->ibd_hdev, 1);
3791         }
3792         rcu_read_unlock();
3793
3794         write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
3795
3796         net->ibn_init = IBLND_INIT_ALL;
3797         kfree(ifaces);
3798
3799         return 0;
3800
3801 failed:
3802         if (net != NULL && net->ibn_dev == NULL && ibdev != NULL)
3803                 kiblnd_destroy_dev(ibdev);
3804
3805         kfree(ifaces);
3806         kiblnd_shutdown(ni);
3807
3808         CDEBUG(D_NET, "Configuration of device %s failed: rc = %d\n",
3809                ifname ? ifname : "", rc);
3810
3811         return -ENETDOWN;
3812 }
3813
3814 static const struct lnet_lnd the_o2iblnd = {
3815         .lnd_type       = O2IBLND,
3816         .lnd_startup    = kiblnd_startup,
3817         .lnd_shutdown   = kiblnd_shutdown,
3818         .lnd_ctl        = kiblnd_ctl,
3819         .lnd_send       = kiblnd_send,
3820         .lnd_recv       = kiblnd_recv,
3821         .lnd_get_dev_prio = kiblnd_get_dev_prio,
3822         .lnd_nl_get     = kiblnd_nl_get,
3823         .lnd_nl_set     = kiblnd_nl_set,
3824         .lnd_keys       = &kiblnd_tunables_keys,
3825 };
3826
3827 static void ko2inlnd_assert_wire_constants(void)
3828 {
3829         BUILD_BUG_ON(IBLND_MSG_MAGIC != 0x0be91b91);
3830         BUILD_BUG_ON(IBLND_MSG_VERSION_1 != 0x11);
3831         BUILD_BUG_ON(IBLND_MSG_VERSION_2 != 0x12);
3832         BUILD_BUG_ON(IBLND_MSG_VERSION != IBLND_MSG_VERSION_2);
3833
3834         BUILD_BUG_ON(IBLND_MSG_CONNREQ != 0xc0);
3835         BUILD_BUG_ON(IBLND_MSG_CONNACK != 0xc1);
3836         BUILD_BUG_ON(IBLND_MSG_NOOP != 0xd0);
3837         BUILD_BUG_ON(IBLND_MSG_IMMEDIATE != 0xd1);
3838         BUILD_BUG_ON(IBLND_MSG_PUT_REQ != 0xd2);
3839         BUILD_BUG_ON(IBLND_MSG_PUT_NAK != 0xd3);
3840         BUILD_BUG_ON(IBLND_MSG_PUT_ACK != 0xd4);
3841         BUILD_BUG_ON(IBLND_MSG_PUT_DONE != 0xd5);
3842         BUILD_BUG_ON(IBLND_MSG_GET_REQ != 0xd6);
3843         BUILD_BUG_ON(IBLND_MSG_GET_DONE != 0xd7);
3844
3845         BUILD_BUG_ON(IBLND_REJECT_CONN_RACE != 1);
3846         BUILD_BUG_ON(IBLND_REJECT_NO_RESOURCES != 2);
3847         BUILD_BUG_ON(IBLND_REJECT_FATAL != 3);
3848         BUILD_BUG_ON(IBLND_REJECT_CONN_UNCOMPAT != 4);
3849         BUILD_BUG_ON(IBLND_REJECT_CONN_STALE != 5);
3850         BUILD_BUG_ON(IBLND_REJECT_RDMA_FRAGS != 6);
3851         BUILD_BUG_ON(IBLND_REJECT_MSG_QUEUE_SIZE != 7);
3852         BUILD_BUG_ON(IBLND_REJECT_INVALID_SRV_ID != 8);
3853
3854         BUILD_BUG_ON((int)sizeof(struct kib_connparams) != 8);
3855         BUILD_BUG_ON((int)offsetof(struct kib_connparams, ibcp_queue_depth) != 0);
3856         BUILD_BUG_ON((int)sizeof(((struct kib_connparams *)0)->ibcp_queue_depth) != 2);
3857         BUILD_BUG_ON((int)offsetof(struct kib_connparams, ibcp_max_frags) != 2);
3858         BUILD_BUG_ON((int)sizeof(((struct kib_connparams *)0)->ibcp_max_frags) != 2);
3859         BUILD_BUG_ON((int)offsetof(struct kib_connparams, ibcp_max_msg_size) != 4);
3860         BUILD_BUG_ON((int)sizeof(((struct kib_connparams *)0)->ibcp_max_msg_size) != 4);
3861
3862         BUILD_BUG_ON((int)sizeof(struct kib_immediate_msg) != 72);
3863         BUILD_BUG_ON((int)offsetof(struct kib_immediate_msg, ibim_hdr) != 0);
3864         BUILD_BUG_ON((int)sizeof(((struct kib_immediate_msg *)0)->ibim_hdr) != 72);
3865         BUILD_BUG_ON((int)offsetof(struct kib_immediate_msg, ibim_payload) != 72);
3866         BUILD_BUG_ON((int)sizeof(((struct kib_immediate_msg *)0)->ibim_payload) != 0);
3867
3868         BUILD_BUG_ON((int)sizeof(struct kib_rdma_frag) != 12);
3869         BUILD_BUG_ON((int)offsetof(struct kib_rdma_frag, rf_nob) != 0);
3870         BUILD_BUG_ON((int)sizeof(((struct kib_rdma_frag *)0)->rf_nob) != 4);
3871         BUILD_BUG_ON((int)offsetof(struct kib_rdma_frag, rf_addr) != 4);
3872         BUILD_BUG_ON((int)sizeof(((struct kib_rdma_frag *)0)->rf_addr) != 8);
3873
3874         BUILD_BUG_ON((int)sizeof(struct kib_rdma_desc) != 8);
3875         BUILD_BUG_ON((int)offsetof(struct kib_rdma_desc, rd_key) != 0);
3876         BUILD_BUG_ON((int)sizeof(((struct kib_rdma_desc *)0)->rd_key) != 4);
3877         BUILD_BUG_ON((int)offsetof(struct kib_rdma_desc, rd_nfrags) != 4);
3878         BUILD_BUG_ON((int)sizeof(((struct kib_rdma_desc *)0)->rd_nfrags) != 4);
3879         BUILD_BUG_ON((int)offsetof(struct kib_rdma_desc, rd_frags) != 8);
3880         BUILD_BUG_ON((int)sizeof(((struct kib_rdma_desc *)0)->rd_frags) != 0);
3881
3882         BUILD_BUG_ON((int)sizeof(struct kib_putreq_msg) != 80);
3883         BUILD_BUG_ON((int)offsetof(struct kib_putreq_msg, ibprm_hdr) != 0);
3884         BUILD_BUG_ON((int)sizeof(((struct kib_putreq_msg *)0)->ibprm_hdr) != 72);
3885         BUILD_BUG_ON((int)offsetof(struct kib_putreq_msg, ibprm_cookie) != 72);
3886         BUILD_BUG_ON((int)sizeof(((struct kib_putreq_msg *)0)->ibprm_cookie) != 8);
3887
3888         BUILD_BUG_ON((int)sizeof(struct kib_putack_msg) != 24);
3889         BUILD_BUG_ON((int)offsetof(struct kib_putack_msg, ibpam_src_cookie) != 0);
3890         BUILD_BUG_ON((int)sizeof(((struct kib_putack_msg *)0)->ibpam_src_cookie) != 8);
3891         BUILD_BUG_ON((int)offsetof(struct kib_putack_msg, ibpam_dst_cookie) != 8);
3892         BUILD_BUG_ON((int)sizeof(((struct kib_putack_msg *)0)->ibpam_dst_cookie) != 8);
3893         BUILD_BUG_ON((int)offsetof(struct kib_putack_msg, ibpam_rd) != 16);
3894         BUILD_BUG_ON((int)sizeof(((struct kib_putack_msg *)0)->ibpam_rd) != 8);
3895
3896         BUILD_BUG_ON((int)sizeof(struct kib_get_msg) != 88);
3897         BUILD_BUG_ON((int)offsetof(struct kib_get_msg, ibgm_hdr) != 0);
3898         BUILD_BUG_ON((int)sizeof(((struct kib_get_msg *)0)->ibgm_hdr) != 72);
3899         BUILD_BUG_ON((int)offsetof(struct kib_get_msg, ibgm_cookie) != 72);
3900         BUILD_BUG_ON((int)sizeof(((struct kib_get_msg *)0)->ibgm_cookie) != 8);
3901         BUILD_BUG_ON((int)offsetof(struct kib_get_msg, ibgm_rd) != 80);
3902         BUILD_BUG_ON((int)sizeof(((struct kib_get_msg *)0)->ibgm_rd) != 8);
3903
3904         BUILD_BUG_ON((int)sizeof(struct kib_completion_msg) != 12);
3905         BUILD_BUG_ON((int)offsetof(struct kib_completion_msg, ibcm_cookie) != 0);
3906         BUILD_BUG_ON((int)sizeof(((struct kib_completion_msg *)0)->ibcm_cookie) != 8);
3907         BUILD_BUG_ON((int)offsetof(struct kib_completion_msg, ibcm_status) != 8);
3908         BUILD_BUG_ON((int)sizeof(((struct kib_completion_msg *)0)->ibcm_status) != 4);
3909
3910         /* Checks for struct kib_msg */
3911         //BUILD_BUG_ON((int)sizeof(struct kib_msg) != 12);
3912         BUILD_BUG_ON((int)offsetof(struct kib_msg, ibm_magic) != 0);
3913         BUILD_BUG_ON((int)sizeof(((struct kib_msg *)0)->ibm_magic) != 4);
3914         BUILD_BUG_ON((int)offsetof(struct kib_msg, ibm_version) != 4);
3915         BUILD_BUG_ON((int)sizeof(((struct kib_msg *)0)->ibm_version) != 2);
3916         BUILD_BUG_ON((int)offsetof(struct kib_msg, ibm_type) != 6);
3917         BUILD_BUG_ON((int)sizeof(((struct kib_msg *)0)->ibm_type) != 1);
3918         BUILD_BUG_ON((int)offsetof(struct kib_msg, ibm_credits) != 7);
3919         BUILD_BUG_ON((int)sizeof(((struct kib_msg *)0)->ibm_credits) != 1);
3920         BUILD_BUG_ON((int)offsetof(struct kib_msg, ibm_nob) != 8);
3921         BUILD_BUG_ON((int)sizeof(((struct kib_msg *)0)->ibm_nob) != 4);
3922         BUILD_BUG_ON((int)offsetof(struct kib_msg, ibm_cksum) != 12);
3923         BUILD_BUG_ON((int)sizeof(((struct kib_msg *)0)->ibm_cksum) != 4);
3924         BUILD_BUG_ON((int)offsetof(struct kib_msg, ibm_srcnid) != 16);
3925         BUILD_BUG_ON((int)sizeof(((struct kib_msg *)0)->ibm_srcnid) != 8);
3926         BUILD_BUG_ON((int)offsetof(struct kib_msg, ibm_srcstamp) != 24);
3927         BUILD_BUG_ON((int)sizeof(((struct kib_msg *)0)->ibm_srcstamp) != 8);
3928         BUILD_BUG_ON((int)offsetof(struct kib_msg, ibm_dstnid) != 32);
3929         BUILD_BUG_ON((int)sizeof(((struct kib_msg *)0)->ibm_dstnid) != 8);
3930         BUILD_BUG_ON((int)offsetof(struct kib_msg, ibm_dststamp) != 40);
3931         BUILD_BUG_ON((int)sizeof(((struct kib_msg *)0)->ibm_dststamp) != 8);
3932
3933         /* Connparams */
3934         BUILD_BUG_ON((int)offsetof(struct kib_msg, ibm_u.connparams.ibcp_queue_depth) != 48);
3935         BUILD_BUG_ON((int)sizeof(((struct kib_msg *)0)->ibm_u.connparams.ibcp_queue_depth) != 2);
3936         BUILD_BUG_ON((int)offsetof(struct kib_msg, ibm_u.connparams.ibcp_max_frags) != 50);
3937         BUILD_BUG_ON((int)sizeof(((struct kib_msg *)0)->ibm_u.connparams.ibcp_max_frags) != 2);
3938         BUILD_BUG_ON((int)offsetof(struct kib_msg, ibm_u.connparams.ibcp_max_msg_size) != 52);
3939         BUILD_BUG_ON((int)sizeof(((struct kib_msg *)0)->ibm_u.connparams.ibcp_max_msg_size) != 4);
3940
3941         /* Immediate message */
3942         BUILD_BUG_ON((int)offsetof(struct kib_msg, ibm_u.immediate.ibim_hdr) != 48);
3943         BUILD_BUG_ON((int)sizeof(((struct kib_msg *)0)->ibm_u.immediate.ibim_hdr) != 72);
3944         BUILD_BUG_ON((int)offsetof(struct kib_msg, ibm_u.immediate.ibim_payload) != 120);
3945         BUILD_BUG_ON((int)sizeof(((struct kib_msg *)0)->ibm_u.immediate.ibim_payload) != 0);
3946
3947         /* PUT req message */
3948         BUILD_BUG_ON((int)offsetof(struct kib_msg, ibm_u.putreq.ibprm_hdr) != 48);
3949         BUILD_BUG_ON((int)sizeof(((struct kib_msg *)0)->ibm_u.putreq.ibprm_hdr) != 72);
3950         BUILD_BUG_ON((int)offsetof(struct kib_msg, ibm_u.putreq.ibprm_cookie) != 120);
3951         BUILD_BUG_ON((int)sizeof(((struct kib_msg *)0)->ibm_u.putreq.ibprm_cookie) != 8);
3952
3953         /* Put ACK */
3954         BUILD_BUG_ON((int)offsetof(struct kib_msg, ibm_u.putack.ibpam_src_cookie) != 48);
3955         BUILD_BUG_ON((int)sizeof(((struct kib_msg *)0)->ibm_u.putack.ibpam_src_cookie) != 8);
3956         BUILD_BUG_ON((int)offsetof(struct kib_msg, ibm_u.putack.ibpam_dst_cookie) != 56);
3957         BUILD_BUG_ON((int)sizeof(((struct kib_msg *)0)->ibm_u.putack.ibpam_dst_cookie) != 8);
3958         BUILD_BUG_ON((int)offsetof(struct kib_msg, ibm_u.putack.ibpam_rd) != 64);
3959         BUILD_BUG_ON((int)sizeof(((struct kib_msg *)0)->ibm_u.putack.ibpam_rd) != 8);
3960
3961         /* GET message */
3962         BUILD_BUG_ON((int)offsetof(struct kib_msg, ibm_u.get.ibgm_hdr) != 48);
3963         BUILD_BUG_ON((int)sizeof(((struct kib_msg *)0)->ibm_u.get.ibgm_hdr) != 72);
3964         BUILD_BUG_ON((int)offsetof(struct kib_msg, ibm_u.get.ibgm_cookie) != 120);
3965         BUILD_BUG_ON((int)sizeof(((struct kib_msg *)0)->ibm_u.get.ibgm_cookie) != 8);
3966         BUILD_BUG_ON((int)offsetof(struct kib_msg, ibm_u.get.ibgm_rd) != 128);
3967         BUILD_BUG_ON((int)sizeof(((struct kib_msg *)0)->ibm_u.get.ibgm_rd) != 8);
3968
3969         /* Completion message */
3970         BUILD_BUG_ON((int)offsetof(struct kib_msg, ibm_u.completion.ibcm_cookie) != 48);
3971         BUILD_BUG_ON((int)sizeof(((struct kib_msg *)0)->ibm_u.completion.ibcm_cookie) != 8);
3972         BUILD_BUG_ON((int)offsetof(struct kib_msg, ibm_u.completion.ibcm_status) != 56);
3973         BUILD_BUG_ON((int)sizeof(((struct kib_msg *)0)->ibm_u.completion.ibcm_status) != 4);
3974
3975         /* Sanity checks */
3976         BUILD_BUG_ON(sizeof(struct kib_msg) > IBLND_MSG_SIZE);
3977         BUILD_BUG_ON(offsetof(struct kib_msg,
3978                      ibm_u.get.ibgm_rd.rd_frags[IBLND_MAX_RDMA_FRAGS]) >
3979                      IBLND_MSG_SIZE);
3980         BUILD_BUG_ON(offsetof(struct kib_msg,
3981                      ibm_u.putack.ibpam_rd.rd_frags[IBLND_MAX_RDMA_FRAGS]) >
3982                      IBLND_MSG_SIZE);
3983 }
3984
3985 static void __exit ko2iblnd_exit(void)
3986 {
3987         lnet_unregister_lnd(&the_o2iblnd);
3988 }
3989
3990 static int __init ko2iblnd_init(void)
3991 {
3992         int rc;
3993
3994         ko2inlnd_assert_wire_constants();
3995
3996         rc = kiblnd_tunables_init();
3997         if (rc != 0)
3998                 return rc;
3999
4000         rc = libcfs_setup();
4001         if (rc)
4002                 return rc;
4003
4004         lnet_register_lnd(&the_o2iblnd);
4005
4006         return 0;
4007 }
4008
4009 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
4010 MODULE_DESCRIPTION("OpenIB gen2 LNet Network Driver");
4011 MODULE_VERSION("2.8.0");
4012 MODULE_LICENSE("GPL");
4013
4014 module_init(ko2iblnd_init);
4015 module_exit(ko2iblnd_exit);