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