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