Whamcloud - gitweb
LU-14536 obi2lnd: don't try to reconnect if there's no listener
[fs/lustre-release.git] / lnet / klnds / o2iblnd / o2iblnd_cb.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) 2012, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lnet/klnds/o2iblnd/o2iblnd_cb.c
33  *
34  * Author: Eric Barton <eric@bartonsoftware.com>
35  */
36
37 #include "o2iblnd.h"
38
39 #define MAX_CONN_RACES_BEFORE_ABORT 20
40
41 static void kiblnd_peer_alive(struct kib_peer_ni *peer_ni);
42 static void kiblnd_peer_connect_failed(struct kib_peer_ni *peer_ni, int active,
43                                        int error);
44 static void kiblnd_init_tx_msg(struct lnet_ni *ni, struct kib_tx *tx,
45                                int type, int body_nob);
46 static int kiblnd_init_rdma(struct kib_conn *conn, struct kib_tx *tx, int type,
47                             int resid, struct kib_rdma_desc *dstrd, u64 dstcookie);
48 static void kiblnd_queue_tx_locked(struct kib_tx *tx, struct kib_conn *conn);
49 static void kiblnd_queue_tx(struct kib_tx *tx, struct kib_conn *conn);
50
51 static void kiblnd_unmap_tx(struct kib_tx *tx);
52 static void kiblnd_check_sends_locked(struct kib_conn *conn);
53
54 void
55 kiblnd_tx_done(struct kib_tx *tx)
56 {
57         struct lnet_msg *lntmsg[2];
58         int         rc;
59         int         i;
60
61         LASSERT (!in_interrupt());
62         LASSERT (!tx->tx_queued);               /* mustn't be queued for sending */
63         LASSERT (tx->tx_sending == 0);          /* mustn't be awaiting sent callback */
64         LASSERT (!tx->tx_waiting);              /* mustn't be awaiting peer_ni response */
65         LASSERT (tx->tx_pool != NULL);
66
67         kiblnd_unmap_tx(tx);
68
69         /* tx may have up to 2 lnet msgs to finalise */
70         lntmsg[0] = tx->tx_lntmsg[0]; tx->tx_lntmsg[0] = NULL;
71         lntmsg[1] = tx->tx_lntmsg[1]; tx->tx_lntmsg[1] = NULL;
72         rc = tx->tx_status;
73
74         if (tx->tx_conn != NULL) {
75                 kiblnd_conn_decref(tx->tx_conn);
76                 tx->tx_conn = NULL;
77         }
78
79         tx->tx_nwrq = tx->tx_nsge = 0;
80         tx->tx_status = 0;
81
82         kiblnd_pool_free_node(&tx->tx_pool->tpo_pool, &tx->tx_list);
83
84         /* delay finalize until my descs have been freed */
85         for (i = 0; i < 2; i++) {
86                 if (lntmsg[i] == NULL)
87                         continue;
88
89                 /* propagate health status to LNet for requests */
90                 if (i == 0 && lntmsg[i])
91                         lntmsg[i]->msg_health_status = tx->tx_hstatus;
92
93                 lnet_finalize(lntmsg[i], rc);
94         }
95 }
96
97 void
98 kiblnd_txlist_done(struct list_head *txlist, int status,
99                    enum lnet_msg_hstatus hstatus)
100 {
101         struct kib_tx *tx;
102
103         while (!list_empty(txlist)) {
104                 tx = list_entry(txlist->next, struct kib_tx, tx_list);
105
106                 list_del(&tx->tx_list);
107                 /* complete now */
108                 tx->tx_waiting = 0;
109                 tx->tx_status = status;
110                 if (hstatus != LNET_MSG_STATUS_OK)
111                         tx->tx_hstatus = hstatus;
112                 kiblnd_tx_done(tx);
113         }
114 }
115
116 static struct kib_tx *
117 kiblnd_get_idle_tx(struct lnet_ni *ni, lnet_nid_t target)
118 {
119         struct kib_net *net = ni->ni_data;
120         struct list_head *node;
121         struct kib_tx *tx;
122         struct kib_tx_poolset *tps;
123
124         tps = net->ibn_tx_ps[lnet_cpt_of_nid(target, ni)];
125         node = kiblnd_pool_alloc_node(&tps->tps_poolset);
126         if (node == NULL)
127                 return NULL;
128         tx = container_of(node, struct kib_tx, tx_list);
129
130         LASSERT (tx->tx_nwrq == 0);
131         LASSERT (!tx->tx_queued);
132         LASSERT (tx->tx_sending == 0);
133         LASSERT (!tx->tx_waiting);
134         LASSERT (tx->tx_status == 0);
135         LASSERT (tx->tx_conn == NULL);
136         LASSERT (tx->tx_lntmsg[0] == NULL);
137         LASSERT (tx->tx_lntmsg[1] == NULL);
138         LASSERT (tx->tx_nfrags == 0);
139
140         tx->tx_gaps = false;
141         tx->tx_hstatus = LNET_MSG_STATUS_OK;
142
143         return tx;
144 }
145
146 static void
147 kiblnd_drop_rx(struct kib_rx *rx)
148 {
149         struct kib_conn *conn = rx->rx_conn;
150         struct kib_sched_info *sched = conn->ibc_sched;
151         unsigned long flags;
152
153         spin_lock_irqsave(&sched->ibs_lock, flags);
154         LASSERT(conn->ibc_nrx > 0);
155         conn->ibc_nrx--;
156         spin_unlock_irqrestore(&sched->ibs_lock, flags);
157
158         kiblnd_conn_decref(conn);
159 }
160
161 int
162 kiblnd_post_rx(struct kib_rx *rx, int credit)
163 {
164         struct kib_conn *conn = rx->rx_conn;
165         struct kib_net *net = conn->ibc_peer->ibp_ni->ni_data;
166         struct ib_recv_wr *bad_wrq = NULL;
167 #ifdef HAVE_IB_GET_DMA_MR
168         struct ib_mr *mr = conn->ibc_hdev->ibh_mrs;
169 #endif
170         int rc;
171
172         LASSERT (net != NULL);
173         LASSERT (!in_interrupt());
174         LASSERT (credit == IBLND_POSTRX_NO_CREDIT ||
175                  credit == IBLND_POSTRX_PEER_CREDIT ||
176                  credit == IBLND_POSTRX_RSRVD_CREDIT);
177 #ifdef HAVE_IB_GET_DMA_MR
178         LASSERT(mr != NULL);
179
180         rx->rx_sge.lkey   = mr->lkey;
181 #else
182         rx->rx_sge.lkey   = conn->ibc_hdev->ibh_pd->local_dma_lkey;
183 #endif
184         rx->rx_sge.addr   = rx->rx_msgaddr;
185         rx->rx_sge.length = IBLND_MSG_SIZE;
186
187         rx->rx_wrq.next = NULL;
188         rx->rx_wrq.sg_list = &rx->rx_sge;
189         rx->rx_wrq.num_sge = 1;
190         rx->rx_wrq.wr_id = kiblnd_ptr2wreqid(rx, IBLND_WID_RX);
191
192         LASSERT (conn->ibc_state >= IBLND_CONN_INIT);
193         LASSERT (rx->rx_nob >= 0);              /* not posted */
194
195         if (conn->ibc_state > IBLND_CONN_ESTABLISHED) {
196                 kiblnd_drop_rx(rx);             /* No more posts for this rx */
197                 return 0;
198         }
199
200         rx->rx_nob = -1;                        /* flag posted */
201
202         /* NB: need an extra reference after ib_post_recv because we don't
203          * own this rx (and rx::rx_conn) anymore, LU-5678.
204          */
205         kiblnd_conn_addref(conn);
206 #ifdef HAVE_IB_POST_SEND_RECV_CONST
207         rc = ib_post_recv(conn->ibc_cmid->qp, &rx->rx_wrq,
208                           (const struct ib_recv_wr **)&bad_wrq);
209 #else
210         rc = ib_post_recv(conn->ibc_cmid->qp, &rx->rx_wrq, &bad_wrq);
211 #endif
212         if (unlikely(rc != 0)) {
213                 CERROR("Can't post rx for %s: %d, bad_wrq: %p\n",
214                        libcfs_nid2str(conn->ibc_peer->ibp_nid), rc, bad_wrq);
215                 rx->rx_nob = 0;
216         }
217
218         if (conn->ibc_state < IBLND_CONN_ESTABLISHED) /* Initial post */
219                 goto out;
220
221         if (unlikely(rc != 0)) {
222                 kiblnd_close_conn(conn, rc);
223                 kiblnd_drop_rx(rx);     /* No more posts for this rx */
224                 goto out;
225         }
226
227         if (credit == IBLND_POSTRX_NO_CREDIT)
228                 goto out;
229
230         spin_lock(&conn->ibc_lock);
231         if (credit == IBLND_POSTRX_PEER_CREDIT)
232                 conn->ibc_outstanding_credits++;
233         else
234                 conn->ibc_reserved_credits++;
235         kiblnd_check_sends_locked(conn);
236         spin_unlock(&conn->ibc_lock);
237
238 out:
239         kiblnd_conn_decref(conn);
240         return rc;
241 }
242
243 static struct kib_tx *
244 kiblnd_find_waiting_tx_locked(struct kib_conn *conn, int txtype, u64 cookie)
245 {
246         struct list_head *tmp;
247
248         list_for_each(tmp, &conn->ibc_active_txs) {
249                 struct kib_tx *tx = list_entry(tmp, struct kib_tx, tx_list);
250
251                 LASSERT(!tx->tx_queued);
252                 LASSERT(tx->tx_sending != 0 || tx->tx_waiting);
253
254                 if (tx->tx_cookie != cookie)
255                         continue;
256
257                 if (tx->tx_waiting &&
258                     tx->tx_msg->ibm_type == txtype)
259                         return tx;
260
261                 CWARN("Bad completion: %swaiting, type %x (wanted %x)\n",
262                       tx->tx_waiting ? "" : "NOT ",
263                       tx->tx_msg->ibm_type, txtype);
264         }
265         return NULL;
266 }
267
268 static void
269 kiblnd_handle_completion(struct kib_conn *conn, int txtype, int status, u64 cookie)
270 {
271         struct kib_tx *tx;
272         struct lnet_ni *ni = conn->ibc_peer->ibp_ni;
273         int idle;
274
275         spin_lock(&conn->ibc_lock);
276
277         tx = kiblnd_find_waiting_tx_locked(conn, txtype, cookie);
278         if (tx == NULL) {
279                 spin_unlock(&conn->ibc_lock);
280
281                 CWARN("Unmatched completion type %x cookie %#llx from %s\n",
282                       txtype, cookie, libcfs_nid2str(conn->ibc_peer->ibp_nid));
283                 kiblnd_close_conn(conn, -EPROTO);
284                 return;
285         }
286
287         if (tx->tx_status == 0) {               /* success so far */
288                 if (status < 0) {               /* failed? */
289                         tx->tx_status = status;
290                         tx->tx_hstatus = LNET_MSG_STATUS_REMOTE_ERROR;
291                 } else if (txtype == IBLND_MSG_GET_REQ) {
292                         lnet_set_reply_msg_len(ni, tx->tx_lntmsg[1], status);
293                 }
294         }
295
296         tx->tx_waiting = 0;
297
298         idle = !tx->tx_queued && (tx->tx_sending == 0);
299         if (idle)
300                 list_del(&tx->tx_list);
301
302         spin_unlock(&conn->ibc_lock);
303
304         if (idle)
305                 kiblnd_tx_done(tx);
306 }
307
308 static void
309 kiblnd_send_completion(struct kib_conn *conn, int type, int status, u64 cookie)
310 {
311         struct lnet_ni *ni = conn->ibc_peer->ibp_ni;
312         struct kib_tx *tx = kiblnd_get_idle_tx(ni, conn->ibc_peer->ibp_nid);
313
314         if (tx == NULL) {
315                 CERROR("Can't get tx for completion %x for %s\n",
316                        type, libcfs_nid2str(conn->ibc_peer->ibp_nid));
317                 return;
318         }
319
320         tx->tx_msg->ibm_u.completion.ibcm_status = status;
321         tx->tx_msg->ibm_u.completion.ibcm_cookie = cookie;
322         kiblnd_init_tx_msg(ni, tx, type, sizeof(struct kib_completion_msg));
323
324         kiblnd_queue_tx(tx, conn);
325 }
326
327 static void
328 kiblnd_handle_rx(struct kib_rx *rx)
329 {
330         struct kib_msg *msg = rx->rx_msg;
331         struct kib_conn   *conn = rx->rx_conn;
332         struct lnet_ni *ni = conn->ibc_peer->ibp_ni;
333         int           credits = msg->ibm_credits;
334         struct kib_tx *tx;
335         int           rc = 0;
336         int           rc2;
337         int           post_credit;
338
339         LASSERT (conn->ibc_state >= IBLND_CONN_ESTABLISHED);
340
341         CDEBUG (D_NET, "Received %x[%d] from %s\n",
342                 msg->ibm_type, credits,
343                 libcfs_nid2str(conn->ibc_peer->ibp_nid));
344
345         if (credits != 0) {
346                 /* Have I received credits that will let me send? */
347                 spin_lock(&conn->ibc_lock);
348
349                 if (conn->ibc_credits + credits >
350                     conn->ibc_queue_depth) {
351                         rc2 = conn->ibc_credits;
352                         spin_unlock(&conn->ibc_lock);
353
354                         CERROR("Bad credits from %s: %d + %d > %d\n",
355                                libcfs_nid2str(conn->ibc_peer->ibp_nid),
356                                rc2, credits,
357                                conn->ibc_queue_depth);
358
359                         kiblnd_close_conn(conn, -EPROTO);
360                         kiblnd_post_rx(rx, IBLND_POSTRX_NO_CREDIT);
361                         return;
362                 }
363
364                 conn->ibc_credits += credits;
365
366                 /* This ensures the credit taken by NOOP can be returned */
367                 if (msg->ibm_type == IBLND_MSG_NOOP &&
368                     !IBLND_OOB_CAPABLE(conn->ibc_version)) /* v1 only */
369                         conn->ibc_outstanding_credits++;
370
371                 kiblnd_check_sends_locked(conn);
372                 spin_unlock(&conn->ibc_lock);
373         }
374
375         switch (msg->ibm_type) {
376         default:
377                 CERROR("Bad IBLND message type %x from %s\n",
378                        msg->ibm_type, libcfs_nid2str(conn->ibc_peer->ibp_nid));
379                 post_credit = IBLND_POSTRX_NO_CREDIT;
380                 rc = -EPROTO;
381                 break;
382
383         case IBLND_MSG_NOOP:
384                 if (IBLND_OOB_CAPABLE(conn->ibc_version)) {
385                         post_credit = IBLND_POSTRX_NO_CREDIT;
386                         break;
387                 }
388
389                 if (credits != 0) /* credit already posted */
390                         post_credit = IBLND_POSTRX_NO_CREDIT;
391                 else              /* a keepalive NOOP */
392                         post_credit = IBLND_POSTRX_PEER_CREDIT;
393                 break;
394
395         case IBLND_MSG_IMMEDIATE:
396                 post_credit = IBLND_POSTRX_DONT_POST;
397                 rc = lnet_parse(ni, &msg->ibm_u.immediate.ibim_hdr,
398                                 msg->ibm_srcnid, rx, 0);
399                 if (rc < 0)                     /* repost on error */
400                         post_credit = IBLND_POSTRX_PEER_CREDIT;
401                 break;
402
403         case IBLND_MSG_PUT_REQ:
404                 post_credit = IBLND_POSTRX_DONT_POST;
405                 rc = lnet_parse(ni, &msg->ibm_u.putreq.ibprm_hdr,
406                                 msg->ibm_srcnid, rx, 1);
407                 if (rc < 0)                     /* repost on error */
408                         post_credit = IBLND_POSTRX_PEER_CREDIT;
409                 break;
410
411         case IBLND_MSG_PUT_NAK:
412                 CWARN ("PUT_NACK from %s\n",
413                        libcfs_nid2str(conn->ibc_peer->ibp_nid));
414                 post_credit = IBLND_POSTRX_RSRVD_CREDIT;
415                 kiblnd_handle_completion(conn, IBLND_MSG_PUT_REQ,
416                                          msg->ibm_u.completion.ibcm_status,
417                                          msg->ibm_u.completion.ibcm_cookie);
418                 break;
419
420         case IBLND_MSG_PUT_ACK:
421                 post_credit = IBLND_POSTRX_RSRVD_CREDIT;
422
423                 spin_lock(&conn->ibc_lock);
424                 tx = kiblnd_find_waiting_tx_locked(conn, IBLND_MSG_PUT_REQ,
425                                         msg->ibm_u.putack.ibpam_src_cookie);
426                 if (tx != NULL)
427                         list_del(&tx->tx_list);
428                 spin_unlock(&conn->ibc_lock);
429
430                 if (tx == NULL) {
431                         CERROR("Unmatched PUT_ACK from %s\n",
432                                libcfs_nid2str(conn->ibc_peer->ibp_nid));
433                         rc = -EPROTO;
434                         break;
435                 }
436
437                 LASSERT (tx->tx_waiting);
438                 /* CAVEAT EMPTOR: I could be racing with tx_complete, but...
439                  * (a) I can overwrite tx_msg since my peer_ni has received it!
440                  * (b) tx_waiting set tells tx_complete() it's not done. */
441
442                 tx->tx_nwrq = tx->tx_nsge = 0;  /* overwrite PUT_REQ */
443
444                 rc2 = kiblnd_init_rdma(conn, tx, IBLND_MSG_PUT_DONE,
445                                        kiblnd_rd_size(&msg->ibm_u.putack.ibpam_rd),
446                                        &msg->ibm_u.putack.ibpam_rd,
447                                        msg->ibm_u.putack.ibpam_dst_cookie);
448                 if (rc2 < 0)
449                         CERROR("Can't setup rdma for PUT to %s: %d\n",
450                                libcfs_nid2str(conn->ibc_peer->ibp_nid), rc2);
451
452                 spin_lock(&conn->ibc_lock);
453                 tx->tx_waiting = 0;     /* clear waiting and queue atomically */
454                 kiblnd_queue_tx_locked(tx, conn);
455                 spin_unlock(&conn->ibc_lock);
456                 break;
457
458         case IBLND_MSG_PUT_DONE:
459                 post_credit = IBLND_POSTRX_PEER_CREDIT;
460                 kiblnd_handle_completion(conn, IBLND_MSG_PUT_ACK,
461                                          msg->ibm_u.completion.ibcm_status,
462                                          msg->ibm_u.completion.ibcm_cookie);
463                 break;
464
465         case IBLND_MSG_GET_REQ:
466                 post_credit = IBLND_POSTRX_DONT_POST;
467                 rc = lnet_parse(ni, &msg->ibm_u.get.ibgm_hdr,
468                                 msg->ibm_srcnid, rx, 1);
469                 if (rc < 0)                     /* repost on error */
470                         post_credit = IBLND_POSTRX_PEER_CREDIT;
471                 break;
472
473         case IBLND_MSG_GET_DONE:
474                 post_credit = IBLND_POSTRX_RSRVD_CREDIT;
475                 kiblnd_handle_completion(conn, IBLND_MSG_GET_REQ,
476                                          msg->ibm_u.completion.ibcm_status,
477                                          msg->ibm_u.completion.ibcm_cookie);
478                 break;
479         }
480
481         if (rc < 0)                             /* protocol error */
482                 kiblnd_close_conn(conn, rc);
483
484         if (post_credit != IBLND_POSTRX_DONT_POST)
485                 kiblnd_post_rx(rx, post_credit);
486 }
487
488 static void
489 kiblnd_rx_complete(struct kib_rx *rx, int status, int nob)
490 {
491         struct kib_msg *msg = rx->rx_msg;
492         struct kib_conn   *conn = rx->rx_conn;
493         struct lnet_ni *ni = conn->ibc_peer->ibp_ni;
494         struct kib_net *net = ni->ni_data;
495         int rc;
496         int err = -EIO;
497
498         LASSERT (net != NULL);
499         LASSERT (rx->rx_nob < 0);               /* was posted */
500         rx->rx_nob = 0;                         /* isn't now */
501
502         if (conn->ibc_state > IBLND_CONN_ESTABLISHED)
503                 goto ignore;
504
505         if (status != IB_WC_SUCCESS) {
506                 CNETERR("Rx from %s failed: %d\n",
507                         libcfs_nid2str(conn->ibc_peer->ibp_nid), status);
508                 goto failed;
509         }
510
511         LASSERT (nob >= 0);
512         rx->rx_nob = nob;
513
514         rc = kiblnd_unpack_msg(msg, rx->rx_nob);
515         if (rc != 0) {
516                 CERROR ("Error %d unpacking rx from %s\n",
517                         rc, libcfs_nid2str(conn->ibc_peer->ibp_nid));
518                 goto failed;
519         }
520
521         if (msg->ibm_srcnid != conn->ibc_peer->ibp_nid ||
522             msg->ibm_dstnid != ni->ni_nid ||
523             msg->ibm_srcstamp != conn->ibc_incarnation ||
524             msg->ibm_dststamp != net->ibn_incarnation) {
525                 CERROR ("Stale rx from %s\n",
526                         libcfs_nid2str(conn->ibc_peer->ibp_nid));
527                 err = -ESTALE;
528                 goto failed;
529         }
530
531         /* set time last known alive */
532         kiblnd_peer_alive(conn->ibc_peer);
533
534         /* racing with connection establishment/teardown! */
535
536         if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
537                 rwlock_t  *g_lock = &kiblnd_data.kib_global_lock;
538                 unsigned long  flags;
539
540                 write_lock_irqsave(g_lock, flags);
541                 /* must check holding global lock to eliminate race */
542                 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
543                         list_add_tail(&rx->rx_list, &conn->ibc_early_rxs);
544                         write_unlock_irqrestore(g_lock, flags);
545                         return;
546                 }
547                 write_unlock_irqrestore(g_lock, flags);
548         }
549         kiblnd_handle_rx(rx);
550         return;
551
552  failed:
553         CDEBUG(D_NET, "rx %p conn %p\n", rx, conn);
554         kiblnd_close_conn(conn, err);
555  ignore:
556         kiblnd_drop_rx(rx);                     /* Don't re-post rx. */
557 }
558
559 static int
560 kiblnd_fmr_map_tx(struct kib_net *net, struct kib_tx *tx,
561                   struct kib_rdma_desc *rd, u32 nob)
562 {
563         struct kib_hca_dev *hdev;
564         struct kib_dev *dev;
565         struct kib_fmr_poolset *fps;
566         int                     cpt;
567         int                     rc;
568         int i;
569
570         LASSERT(tx->tx_pool != NULL);
571         LASSERT(tx->tx_pool->tpo_pool.po_owner != NULL);
572
573         dev = net->ibn_dev;
574         hdev = tx->tx_pool->tpo_hdev;
575         cpt = tx->tx_pool->tpo_pool.po_owner->ps_cpt;
576
577         /*
578          * If we're dealing with FastReg, but the device doesn't
579          * support GAPS and the tx has GAPS, then there is no real point
580          * in trying to map the memory, because it'll just fail. So
581          * preemptively fail with an appropriate message
582          */
583         if ((dev->ibd_dev_caps & IBLND_DEV_CAPS_FASTREG_ENABLED) &&
584             !(dev->ibd_dev_caps & IBLND_DEV_CAPS_FASTREG_GAPS_SUPPORT) &&
585             tx->tx_gaps) {
586                 CERROR("Using FastReg with no GAPS support, but tx has gaps. "
587                        "Try setting use_fastreg_gaps to 1\n");
588                 return -EPROTONOSUPPORT;
589         }
590
591 #ifdef HAVE_FMR_POOL_API
592         /*
593          * FMR does not support gaps but the tx has gaps then
594          * we should make sure that the number of fragments we'll be sending
595          * over fits within the number of fragments negotiated on the
596          * connection, otherwise, we won't be able to RDMA the data.
597          * We need to maintain the number of fragments negotiation on the
598          * connection for backwards compatibility.
599          */
600         if (tx->tx_gaps && (dev->ibd_dev_caps & IBLND_DEV_CAPS_FMR_ENABLED)) {
601                 if (tx->tx_conn &&
602                     tx->tx_conn->ibc_max_frags <= rd->rd_nfrags) {
603                         CERROR("TX number of frags (%d) is <= than connection"
604                                " number of frags (%d). Consider setting peer's"
605                                " map_on_demand to 256\n", tx->tx_nfrags,
606                                tx->tx_conn->ibc_max_frags);
607                         return -EFBIG;
608                 }
609         }
610 #endif
611
612         fps = net->ibn_fmr_ps[cpt];
613         rc = kiblnd_fmr_pool_map(fps, tx, rd, nob, 0, &tx->tx_fmr);
614         if (rc != 0) {
615                 CERROR("Can't map %u bytes (%u/%u)s: %d\n", nob,
616                        tx->tx_nfrags, rd->rd_nfrags, rc);
617                 return rc;
618         }
619
620         /*
621          * If rd is not tx_rd, it's going to get sent to a peer_ni, who will
622          * need the rkey
623          */
624         rd->rd_key = tx->tx_fmr.fmr_key;
625         /*
626          * for FastReg or FMR with no gaps we can accumulate all
627          * the fragments in one FastReg or FMR fragment.
628          */
629         if (
630 #ifdef HAVE_FMR_POOL_API
631             ((dev->ibd_dev_caps & IBLND_DEV_CAPS_FMR_ENABLED)
632              && !tx->tx_gaps) ||
633 #endif
634             (dev->ibd_dev_caps & IBLND_DEV_CAPS_FASTREG_ENABLED)) {
635                 /* FMR requires zero based address */
636 #ifdef HAVE_FMR_POOL_API
637                 if (dev->ibd_dev_caps & IBLND_DEV_CAPS_FMR_ENABLED)
638                         rd->rd_frags[0].rf_addr &= ~hdev->ibh_page_mask;
639 #endif
640                 rd->rd_frags[0].rf_nob = nob;
641                 rd->rd_nfrags = 1;
642         } else {
643                 /*
644                  * We're transmitting with gaps using FMR.
645                  * We'll need to use multiple fragments and identify the
646                  * zero based address of each fragment.
647                  */
648                 for (i = 0; i < rd->rd_nfrags; i++) {
649                         rd->rd_frags[i].rf_addr &= ~hdev->ibh_page_mask;
650                         rd->rd_frags[i].rf_addr += i << hdev->ibh_page_shift;
651                 }
652         }
653
654         return 0;
655 }
656
657 static void
658 kiblnd_unmap_tx(struct kib_tx *tx)
659 {
660         if (
661 #ifdef HAVE_FMR_POOL_API
662                 tx->tx_fmr.fmr_pfmr ||
663 #endif
664                 tx->tx_fmr.fmr_frd)
665                 kiblnd_fmr_pool_unmap(&tx->tx_fmr, tx->tx_status);
666
667         if (tx->tx_nfrags != 0) {
668                 kiblnd_dma_unmap_sg(tx->tx_pool->tpo_hdev->ibh_ibdev,
669                                     tx->tx_frags, tx->tx_nfrags, tx->tx_dmadir);
670                 tx->tx_nfrags = 0;
671         }
672 }
673
674 #ifdef HAVE_IB_GET_DMA_MR
675 static struct ib_mr *
676 kiblnd_find_rd_dma_mr(struct lnet_ni *ni, struct kib_rdma_desc *rd)
677 {
678         struct kib_net *net = ni->ni_data;
679         struct kib_hca_dev *hdev = net->ibn_dev->ibd_hdev;
680         struct lnet_ioctl_config_o2iblnd_tunables *tunables;
681
682         tunables = &ni->ni_lnd_tunables.lnd_tun_u.lnd_o2ib;
683
684         /*
685          * if map-on-demand is turned on and the device supports
686          * either FMR or FastReg then use that. Otherwise use global
687          * memory regions. If that's not available either, then you're
688          * dead in the water and fail the operation.
689          */
690         if (tunables->lnd_map_on_demand &&
691             (net->ibn_dev->ibd_dev_caps & IBLND_DEV_CAPS_FASTREG_ENABLED
692 #ifdef HAVE_FMR_POOL_API
693              || net->ibn_dev->ibd_dev_caps & IBLND_DEV_CAPS_FMR_ENABLED
694 #endif
695         ))
696                 return NULL;
697
698         /*
699          * hdev->ibh_mrs can be NULL. This case is dealt with gracefully
700          * in the call chain. The mapping will fail with appropriate error
701          * message.
702          */
703         return hdev->ibh_mrs;
704 }
705 #endif
706
707 static int kiblnd_map_tx(struct lnet_ni *ni, struct kib_tx *tx,
708                          struct kib_rdma_desc *rd, int nfrags)
709 {
710         struct kib_net *net = ni->ni_data;
711         struct kib_hca_dev *hdev = net->ibn_dev->ibd_hdev;
712 #ifdef HAVE_IB_GET_DMA_MR
713         struct ib_mr *mr = NULL;
714 #endif
715         __u32 nob;
716         int i;
717
718         /* If rd is not tx_rd, it's going to get sent to a peer_ni and I'm the
719          * RDMA sink */
720         tx->tx_dmadir = (rd != tx->tx_rd) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
721         tx->tx_nfrags = nfrags;
722
723         rd->rd_nfrags = kiblnd_dma_map_sg(hdev->ibh_ibdev, tx->tx_frags,
724                                           tx->tx_nfrags, tx->tx_dmadir);
725
726         for (i = 0, nob = 0; i < rd->rd_nfrags; i++) {
727                 rd->rd_frags[i].rf_nob  = kiblnd_sg_dma_len(
728                         hdev->ibh_ibdev, &tx->tx_frags[i]);
729                 rd->rd_frags[i].rf_addr = kiblnd_sg_dma_address(
730                         hdev->ibh_ibdev, &tx->tx_frags[i]);
731                 nob += rd->rd_frags[i].rf_nob;
732         }
733
734 #ifdef HAVE_IB_GET_DMA_MR
735         mr = kiblnd_find_rd_dma_mr(ni, rd);
736         if (mr != NULL) {
737                 /* found pre-mapping MR */
738                 rd->rd_key = (rd != tx->tx_rd) ? mr->rkey : mr->lkey;
739                 return 0;
740         }
741 #endif
742
743         if (net->ibn_fmr_ps != NULL)
744                 return kiblnd_fmr_map_tx(net, tx, rd, nob);
745
746         return -EINVAL;
747 }
748
749 static int kiblnd_setup_rd_iov(struct lnet_ni *ni, struct kib_tx *tx,
750                                struct kib_rdma_desc *rd, unsigned int niov,
751                                struct kvec *iov, int offset, int nob)
752 {
753         struct kib_net *net = ni->ni_data;
754         struct page *page;
755         struct scatterlist *sg;
756         unsigned long       vaddr;
757         int                 fragnob;
758         int                 page_offset;
759         unsigned int        max_niov;
760
761         LASSERT (nob > 0);
762         LASSERT (niov > 0);
763         LASSERT (net != NULL);
764
765         while (offset >= iov->iov_len) {
766                 offset -= iov->iov_len;
767                 niov--;
768                 iov++;
769                 LASSERT (niov > 0);
770         }
771
772         max_niov = niov;
773
774         sg = tx->tx_frags;
775         do {
776                 LASSERT(niov > 0);
777
778                 vaddr = ((unsigned long)iov->iov_base) + offset;
779                 page_offset = vaddr & (PAGE_SIZE - 1);
780                 page = lnet_kvaddr_to_page(vaddr);
781                 if (page == NULL) {
782                         CERROR("Can't find page\n");
783                         return -EFAULT;
784                 }
785
786                 fragnob = min((int)(iov->iov_len - offset), nob);
787                 fragnob = min(fragnob, (int)PAGE_SIZE - page_offset);
788
789                 /*
790                  * We're allowed to start at a non-aligned page offset in
791                  * the first fragment and end at a non-aligned page offset
792                  * in the last fragment.
793                  */
794                 if ((fragnob < (int)PAGE_SIZE - page_offset) &&
795                     (niov < max_niov) && nob > fragnob) {
796                         CDEBUG(D_NET, "fragnob %d < available page %d: with"
797                                       " remaining %d iovs with %d nob left\n",
798                                fragnob, (int)PAGE_SIZE - page_offset, niov,
799                                nob);
800                         tx->tx_gaps = true;
801                 }
802
803                 sg_set_page(sg, page, fragnob, page_offset);
804                 sg = sg_next(sg);
805                 if (!sg) {
806                         CERROR("lacking enough sg entries to map tx\n");
807                         return -EFAULT;
808                 }
809
810                 if (offset + fragnob < iov->iov_len) {
811                         offset += fragnob;
812                 } else {
813                         offset = 0;
814                         iov++;
815                         niov--;
816                 }
817                 nob -= fragnob;
818         } while (nob > 0);
819
820         return kiblnd_map_tx(ni, tx, rd, sg - tx->tx_frags);
821 }
822
823 static int kiblnd_setup_rd_kiov(struct lnet_ni *ni, struct kib_tx *tx,
824                                 struct kib_rdma_desc *rd, int nkiov,
825                                 lnet_kiov_t *kiov, int offset, int nob)
826 {
827         struct kib_net *net = ni->ni_data;
828         struct scatterlist *sg;
829         int                 fragnob;
830         int                 max_nkiov;
831
832         CDEBUG(D_NET, "niov %d offset %d nob %d\n", nkiov, offset, nob);
833
834         LASSERT(nob > 0);
835         LASSERT(nkiov > 0);
836         LASSERT(net != NULL);
837
838         while (offset >= kiov->kiov_len) {
839                 offset -= kiov->kiov_len;
840                 nkiov--;
841                 kiov++;
842                 LASSERT(nkiov > 0);
843         }
844
845         max_nkiov = nkiov;
846
847         sg = tx->tx_frags;
848         do {
849                 LASSERT(nkiov > 0);
850
851                 fragnob = min((int)(kiov->kiov_len - offset), nob);
852
853                 /*
854                  * We're allowed to start at a non-aligned page offset in
855                  * the first fragment and end at a non-aligned page offset
856                  * in the last fragment.
857                  */
858                 if ((fragnob < (int)(kiov->kiov_len - offset)) &&
859                     nkiov < max_nkiov && nob > fragnob) {
860                         CDEBUG(D_NET, "fragnob %d < available page %d: with"
861                                       " remaining %d kiovs with %d nob left\n",
862                                fragnob, (int)(kiov->kiov_len - offset),
863                                nkiov, nob);
864                         tx->tx_gaps = true;
865                 }
866
867                 sg_set_page(sg, kiov->kiov_page, fragnob,
868                             kiov->kiov_offset + offset);
869                 sg = sg_next(sg);
870                 if (!sg) {
871                         CERROR("lacking enough sg entries to map tx\n");
872                         return -EFAULT;
873                 }
874
875                 offset = 0;
876                 kiov++;
877                 nkiov--;
878                 nob -= fragnob;
879         } while (nob > 0);
880
881         return kiblnd_map_tx(ni, tx, rd, sg - tx->tx_frags);
882 }
883
884 static int
885 kiblnd_post_tx_locked(struct kib_conn *conn, struct kib_tx *tx, int credit)
886 __must_hold(&conn->ibc_lock)
887 {
888         struct kib_msg *msg = tx->tx_msg;
889         struct kib_peer_ni *peer_ni = conn->ibc_peer;
890         struct lnet_ni *ni = peer_ni->ibp_ni;
891         struct kib_fast_reg_descriptor *frd = tx->tx_fmr.fmr_frd;
892         int ver = conn->ibc_version;
893         int rc;
894         int done;
895
896         LASSERT(tx->tx_queued);
897         /* We rely on this for QP sizing */
898         LASSERT(tx->tx_nwrq > 0 && tx->tx_nsge >= 0);
899         LASSERT(tx->tx_nwrq <= 1 + conn->ibc_max_frags);
900
901         LASSERT(credit == 0 || credit == 1);
902         LASSERT(conn->ibc_outstanding_credits >= 0);
903         LASSERT(conn->ibc_outstanding_credits <= conn->ibc_queue_depth);
904         LASSERT(conn->ibc_credits >= 0);
905         LASSERT(conn->ibc_credits <= conn->ibc_queue_depth);
906
907         if (conn->ibc_nsends_posted ==
908             kiblnd_concurrent_sends(ver, ni)) {
909                 /* tx completions outstanding... */
910                 CDEBUG(D_NET, "%s: posted enough\n",
911                        libcfs_nid2str(peer_ni->ibp_nid));
912                 return -EAGAIN;
913         }
914
915         if (credit != 0 && conn->ibc_credits == 0) {   /* no credits */
916                 CDEBUG(D_NET, "%s: no credits\n",
917                        libcfs_nid2str(peer_ni->ibp_nid));
918                 return -EAGAIN;
919         }
920
921         if (credit != 0 && !IBLND_OOB_CAPABLE(ver) &&
922             conn->ibc_credits == 1 &&   /* last credit reserved */
923             msg->ibm_type != IBLND_MSG_NOOP) {      /* for NOOP */
924                 CDEBUG(D_NET, "%s: not using last credit\n",
925                        libcfs_nid2str(peer_ni->ibp_nid));
926                 return -EAGAIN;
927         }
928
929         /* NB don't drop ibc_lock before bumping tx_sending */
930         list_del(&tx->tx_list);
931         tx->tx_queued = 0;
932
933         if (msg->ibm_type == IBLND_MSG_NOOP &&
934             (!kiblnd_need_noop(conn) ||     /* redundant NOOP */
935              (IBLND_OOB_CAPABLE(ver) && /* posted enough NOOP */
936               conn->ibc_noops_posted == IBLND_OOB_MSGS(ver)))) {
937                 /* OK to drop when posted enough NOOPs, since
938                  * kiblnd_check_sends_locked will queue NOOP again when
939                  * posted NOOPs complete */
940                 spin_unlock(&conn->ibc_lock);
941                 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
942                 kiblnd_tx_done(tx);
943                 spin_lock(&conn->ibc_lock);
944                 CDEBUG(D_NET, "%s(%d): redundant or enough NOOP\n",
945                        libcfs_nid2str(peer_ni->ibp_nid),
946                        conn->ibc_noops_posted);
947                 return 0;
948         }
949
950         kiblnd_pack_msg(peer_ni->ibp_ni, msg, ver, conn->ibc_outstanding_credits,
951                         peer_ni->ibp_nid, conn->ibc_incarnation);
952
953         conn->ibc_credits -= credit;
954         conn->ibc_outstanding_credits = 0;
955         conn->ibc_nsends_posted++;
956         if (msg->ibm_type == IBLND_MSG_NOOP)
957                 conn->ibc_noops_posted++;
958
959         /* CAVEAT EMPTOR!  This tx could be the PUT_DONE of an RDMA
960          * PUT.  If so, it was first queued here as a PUT_REQ, sent and
961          * stashed on ibc_active_txs, matched by an incoming PUT_ACK,
962          * and then re-queued here.  It's (just) possible that
963          * tx_sending is non-zero if we've not done the tx_complete()
964          * from the first send; hence the ++ rather than = below. */
965         tx->tx_sending++;
966         list_add(&tx->tx_list, &conn->ibc_active_txs);
967
968         /* I'm still holding ibc_lock! */
969         if (conn->ibc_state != IBLND_CONN_ESTABLISHED) {
970                 rc = -ECONNABORTED;
971         } else if (tx->tx_pool->tpo_pool.po_failed ||
972                  conn->ibc_hdev != tx->tx_pool->tpo_hdev) {
973                 /* close_conn will launch failover */
974                 rc = -ENETDOWN;
975         } else {
976                 struct ib_send_wr *bad = &tx->tx_wrq[tx->tx_nwrq - 1].wr;
977                 struct ib_send_wr *wr  = &tx->tx_wrq[0].wr;
978
979                 if (frd != NULL && !frd->frd_posted) {
980                         if (!frd->frd_valid) {
981                                 wr = &frd->frd_inv_wr.wr;
982                                 wr->next = &frd->frd_fastreg_wr.wr;
983                         } else {
984                                 wr = &frd->frd_fastreg_wr.wr;
985                         }
986                         frd->frd_fastreg_wr.wr.next = &tx->tx_wrq[0].wr;
987                 }
988
989                 LASSERTF(bad->wr_id == kiblnd_ptr2wreqid(tx, IBLND_WID_TX),
990                          "bad wr_id %#llx, opc %d, flags %d, peer_ni: %s\n",
991                          bad->wr_id, bad->opcode, bad->send_flags,
992                          libcfs_nid2str(conn->ibc_peer->ibp_nid));
993
994                 bad = NULL;
995                 if (lnet_send_error_simulation(tx->tx_lntmsg[0], &tx->tx_hstatus))
996                         rc = -EINVAL;
997                 else
998 #ifdef HAVE_IB_POST_SEND_RECV_CONST
999                         rc = ib_post_send(conn->ibc_cmid->qp, wr,
1000                                           (const struct ib_send_wr **)&bad);
1001 #else
1002                         rc = ib_post_send(conn->ibc_cmid->qp, wr, &bad);
1003 #endif
1004         }
1005
1006         conn->ibc_last_send = ktime_get();
1007
1008         if (rc == 0) {
1009                 if (frd != NULL)
1010                         frd->frd_posted = true;
1011                 return 0;
1012         }
1013
1014         /* NB credits are transferred in the actual
1015          * message, which can only be the last work item */
1016         conn->ibc_credits += credit;
1017         conn->ibc_outstanding_credits += msg->ibm_credits;
1018         conn->ibc_nsends_posted--;
1019         if (msg->ibm_type == IBLND_MSG_NOOP)
1020                 conn->ibc_noops_posted--;
1021
1022         tx->tx_status = rc;
1023         tx->tx_waiting = 0;
1024         tx->tx_sending--;
1025
1026         done = (tx->tx_sending == 0);
1027         if (done)
1028                 list_del(&tx->tx_list);
1029
1030         spin_unlock(&conn->ibc_lock);
1031
1032         if (conn->ibc_state == IBLND_CONN_ESTABLISHED)
1033                 CERROR("Error %d posting transmit to %s\n",
1034                        rc, libcfs_nid2str(peer_ni->ibp_nid));
1035         else
1036                 CDEBUG(D_NET, "Error %d posting transmit to %s\n",
1037                        rc, libcfs_nid2str(peer_ni->ibp_nid));
1038
1039         kiblnd_close_conn(conn, rc);
1040
1041         if (done)
1042                 kiblnd_tx_done(tx);
1043
1044         spin_lock(&conn->ibc_lock);
1045
1046         return -EIO;
1047 }
1048
1049 static void
1050 kiblnd_check_sends_locked(struct kib_conn *conn)
1051 {
1052         int ver = conn->ibc_version;
1053         struct lnet_ni *ni = conn->ibc_peer->ibp_ni;
1054         struct kib_tx *tx;
1055
1056         /* Don't send anything until after the connection is established */
1057         if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
1058                 CDEBUG(D_NET, "%s too soon\n",
1059                        libcfs_nid2str(conn->ibc_peer->ibp_nid));
1060                 return;
1061         }
1062
1063         LASSERT(conn->ibc_nsends_posted <=
1064                 kiblnd_concurrent_sends(ver, ni));
1065         LASSERT (!IBLND_OOB_CAPABLE(ver) ||
1066                  conn->ibc_noops_posted <= IBLND_OOB_MSGS(ver));
1067         LASSERT (conn->ibc_reserved_credits >= 0);
1068
1069         while (conn->ibc_reserved_credits > 0 &&
1070                !list_empty(&conn->ibc_tx_queue_rsrvd)) {
1071                 tx = list_entry(conn->ibc_tx_queue_rsrvd.next,
1072                                 struct kib_tx, tx_list);
1073                 list_del(&tx->tx_list);
1074                 list_add_tail(&tx->tx_list, &conn->ibc_tx_queue);
1075                 conn->ibc_reserved_credits--;
1076         }
1077
1078         if (kiblnd_need_noop(conn)) {
1079                 spin_unlock(&conn->ibc_lock);
1080
1081                 tx = kiblnd_get_idle_tx(ni, conn->ibc_peer->ibp_nid);
1082                 if (tx != NULL)
1083                         kiblnd_init_tx_msg(ni, tx, IBLND_MSG_NOOP, 0);
1084
1085                 spin_lock(&conn->ibc_lock);
1086                 if (tx != NULL)
1087                         kiblnd_queue_tx_locked(tx, conn);
1088         }
1089
1090         for (;;) {
1091                 int credit;
1092
1093                 if (!list_empty(&conn->ibc_tx_queue_nocred)) {
1094                         credit = 0;
1095                         tx = list_entry(conn->ibc_tx_queue_nocred.next,
1096                                         struct kib_tx, tx_list);
1097                 } else if (!list_empty(&conn->ibc_tx_noops)) {
1098                         LASSERT (!IBLND_OOB_CAPABLE(ver));
1099                         credit = 1;
1100                         tx = list_entry(conn->ibc_tx_noops.next,
1101                                         struct kib_tx, tx_list);
1102                 } else if (!list_empty(&conn->ibc_tx_queue)) {
1103                         credit = 1;
1104                         tx = list_entry(conn->ibc_tx_queue.next,
1105                                         struct kib_tx, tx_list);
1106                 } else
1107                         break;
1108
1109                 if (kiblnd_post_tx_locked(conn, tx, credit) != 0)
1110                         break;
1111         }
1112 }
1113
1114 static void
1115 kiblnd_tx_complete(struct kib_tx *tx, int status)
1116 {
1117         int           failed = (status != IB_WC_SUCCESS);
1118         struct kib_conn   *conn = tx->tx_conn;
1119         int           idle;
1120
1121         if (tx->tx_sending <= 0) {
1122                 CERROR("Received an event on a freed tx: %p status %d\n",
1123                        tx, tx->tx_status);
1124                 return;
1125         }
1126
1127         if (failed) {
1128                 if (conn->ibc_state == IBLND_CONN_ESTABLISHED)
1129                         CNETERR("Tx -> %s cookie %#llx"
1130                                 " sending %d waiting %d: failed %d\n",
1131                                 libcfs_nid2str(conn->ibc_peer->ibp_nid),
1132                                 tx->tx_cookie, tx->tx_sending, tx->tx_waiting,
1133                                 status);
1134
1135                 kiblnd_close_conn(conn, -EIO);
1136         } else {
1137                 kiblnd_peer_alive(conn->ibc_peer);
1138         }
1139
1140         spin_lock(&conn->ibc_lock);
1141
1142         /* I could be racing with rdma completion.  Whoever makes 'tx' idle
1143          * gets to free it, which also drops its ref on 'conn'. */
1144
1145         tx->tx_sending--;
1146         conn->ibc_nsends_posted--;
1147         if (tx->tx_msg->ibm_type == IBLND_MSG_NOOP)
1148                 conn->ibc_noops_posted--;
1149
1150         if (failed) {
1151                 tx->tx_hstatus = LNET_MSG_STATUS_REMOTE_DROPPED;
1152                 tx->tx_waiting = 0;             /* don't wait for peer_ni */
1153                 tx->tx_status = -EIO;
1154         }
1155
1156         idle = (tx->tx_sending == 0) &&         /* This is the final callback */
1157                !tx->tx_waiting &&               /* Not waiting for peer_ni */
1158                !tx->tx_queued;                  /* Not re-queued (PUT_DONE) */
1159         if (idle)
1160                 list_del(&tx->tx_list);
1161
1162         kiblnd_check_sends_locked(conn);
1163         spin_unlock(&conn->ibc_lock);
1164
1165         if (idle)
1166                 kiblnd_tx_done(tx);
1167 }
1168
1169 static void
1170 kiblnd_init_tx_msg(struct lnet_ni *ni, struct kib_tx *tx, int type,
1171                    int body_nob)
1172 {
1173         struct kib_hca_dev *hdev = tx->tx_pool->tpo_hdev;
1174         struct ib_sge *sge = &tx->tx_msgsge;
1175         struct ib_rdma_wr *wrq;
1176         int nob = offsetof(struct kib_msg, ibm_u) + body_nob;
1177 #ifdef HAVE_IB_GET_DMA_MR
1178         struct ib_mr *mr = hdev->ibh_mrs;
1179 #endif
1180
1181         LASSERT(tx->tx_nwrq >= 0);
1182         LASSERT(tx->tx_nwrq < IBLND_MAX_RDMA_FRAGS + 1);
1183         LASSERT(nob <= IBLND_MSG_SIZE);
1184 #ifdef HAVE_IB_GET_DMA_MR
1185         LASSERT(mr != NULL);
1186 #endif
1187
1188         kiblnd_init_msg(tx->tx_msg, type, body_nob);
1189
1190 #ifdef HAVE_IB_GET_DMA_MR
1191         sge->lkey   = mr->lkey;
1192 #else
1193         sge->lkey   = hdev->ibh_pd->local_dma_lkey;
1194 #endif
1195         sge->addr   = tx->tx_msgaddr;
1196         sge->length = nob;
1197
1198         wrq = &tx->tx_wrq[tx->tx_nwrq];
1199         memset(wrq, 0, sizeof(*wrq));
1200
1201         wrq->wr.next            = NULL;
1202         wrq->wr.wr_id           = kiblnd_ptr2wreqid(tx, IBLND_WID_TX);
1203         wrq->wr.sg_list         = sge;
1204         wrq->wr.num_sge         = 1;
1205         wrq->wr.opcode          = IB_WR_SEND;
1206         wrq->wr.send_flags      = IB_SEND_SIGNALED;
1207
1208         tx->tx_nwrq++;
1209 }
1210
1211 static int
1212 kiblnd_init_rdma(struct kib_conn *conn, struct kib_tx *tx, int type,
1213                  int resid, struct kib_rdma_desc *dstrd, u64 dstcookie)
1214 {
1215         struct kib_msg *ibmsg = tx->tx_msg;
1216         struct kib_rdma_desc *srcrd = tx->tx_rd;
1217         struct ib_rdma_wr *wrq = NULL;
1218         struct ib_sge     *sge;
1219         int                rc  = resid;
1220         int                srcidx;
1221         int                dstidx;
1222         int                sge_nob;
1223         int                wrq_sge;
1224
1225         LASSERT(!in_interrupt());
1226         LASSERT(tx->tx_nwrq == 0 && tx->tx_nsge == 0);
1227         LASSERT(type == IBLND_MSG_GET_DONE || type == IBLND_MSG_PUT_DONE);
1228
1229         for (srcidx = dstidx = wrq_sge = sge_nob = 0;
1230              resid > 0; resid -= sge_nob) {
1231                 int     prev = dstidx;
1232
1233                 if (srcidx >= srcrd->rd_nfrags) {
1234                         CERROR("Src buffer exhausted: %d frags\n", srcidx);
1235                         rc = -EPROTO;
1236                         break;
1237                 }
1238
1239                 if (dstidx >= dstrd->rd_nfrags) {
1240                         CERROR("Dst buffer exhausted: %d frags\n", dstidx);
1241                         rc = -EPROTO;
1242                         break;
1243                 }
1244
1245                 if (tx->tx_nwrq >= conn->ibc_max_frags) {
1246                         CERROR("RDMA has too many fragments for peer_ni %s (%d), "
1247                                "src idx/frags: %d/%d dst idx/frags: %d/%d\n",
1248                                libcfs_nid2str(conn->ibc_peer->ibp_nid),
1249                                conn->ibc_max_frags,
1250                                srcidx, srcrd->rd_nfrags,
1251                                dstidx, dstrd->rd_nfrags);
1252                         rc = -EMSGSIZE;
1253                         break;
1254                 }
1255
1256                 sge_nob = MIN(MIN(kiblnd_rd_frag_size(srcrd, srcidx),
1257                                   kiblnd_rd_frag_size(dstrd, dstidx)), resid);
1258
1259                 sge = &tx->tx_sge[tx->tx_nsge];
1260                 sge->addr   = kiblnd_rd_frag_addr(srcrd, srcidx);
1261                 sge->lkey   = kiblnd_rd_frag_key(srcrd, srcidx);
1262                 sge->length = sge_nob;
1263
1264                 if (wrq_sge == 0) {
1265                         wrq = &tx->tx_wrq[tx->tx_nwrq];
1266
1267                         wrq->wr.next    = &(wrq + 1)->wr;
1268                         wrq->wr.wr_id   = kiblnd_ptr2wreqid(tx, IBLND_WID_RDMA);
1269                         wrq->wr.sg_list = sge;
1270                         wrq->wr.opcode  = IB_WR_RDMA_WRITE;
1271                         wrq->wr.send_flags = 0;
1272
1273 #ifdef HAVE_IB_RDMA_WR
1274                         wrq->remote_addr        = kiblnd_rd_frag_addr(dstrd,
1275                                                                       dstidx);
1276                         wrq->rkey               = kiblnd_rd_frag_key(dstrd,
1277                                                                      dstidx);
1278 #else
1279                         wrq->wr.wr.rdma.remote_addr = kiblnd_rd_frag_addr(dstrd,
1280                                                                         dstidx);
1281                         wrq->wr.wr.rdma.rkey    = kiblnd_rd_frag_key(dstrd,
1282                                                                      dstidx);
1283 #endif
1284                 }
1285
1286                 srcidx = kiblnd_rd_consume_frag(srcrd, srcidx, sge_nob);
1287                 dstidx = kiblnd_rd_consume_frag(dstrd, dstidx, sge_nob);
1288
1289                 wrq_sge++;
1290                 if (wrq_sge == *kiblnd_tunables.kib_wrq_sge || dstidx != prev) {
1291                         tx->tx_nwrq++;
1292                         wrq->wr.num_sge = wrq_sge;
1293                         wrq_sge = 0;
1294                 }
1295                 tx->tx_nsge++;
1296         }
1297
1298         if (rc < 0)     /* no RDMA if completing with failure */
1299                 tx->tx_nwrq = tx->tx_nsge = 0;
1300
1301         ibmsg->ibm_u.completion.ibcm_status = rc;
1302         ibmsg->ibm_u.completion.ibcm_cookie = dstcookie;
1303         kiblnd_init_tx_msg(conn->ibc_peer->ibp_ni, tx,
1304                            type, sizeof(struct kib_completion_msg));
1305
1306         return rc;
1307 }
1308
1309 static void
1310 kiblnd_queue_tx_locked(struct kib_tx *tx, struct kib_conn *conn)
1311 {
1312         struct list_head *q;
1313         s64 timeout_ns;
1314
1315         LASSERT(tx->tx_nwrq > 0);       /* work items set up */
1316         LASSERT(!tx->tx_queued);        /* not queued for sending already */
1317         LASSERT(conn->ibc_state >= IBLND_CONN_ESTABLISHED);
1318
1319         if (conn->ibc_state >= IBLND_CONN_DISCONNECTED) {
1320                 tx->tx_status = -ECONNABORTED;
1321                 tx->tx_waiting = 0;
1322                 if (tx->tx_conn != NULL) {
1323                         /* PUT_DONE first attached to conn as a PUT_REQ */
1324                         LASSERT(tx->tx_conn == conn);
1325                         LASSERT(tx->tx_msg->ibm_type == IBLND_MSG_PUT_DONE);
1326                         tx->tx_conn = NULL;
1327                         kiblnd_conn_decref(conn);
1328                 }
1329                 list_add(&tx->tx_list, &conn->ibc_zombie_txs);
1330
1331                 return;
1332         }
1333
1334         timeout_ns = lnet_get_lnd_timeout() * NSEC_PER_SEC;
1335         tx->tx_queued = 1;
1336         tx->tx_deadline = ktime_add_ns(ktime_get(), timeout_ns);
1337
1338         if (tx->tx_conn == NULL) {
1339                 kiblnd_conn_addref(conn);
1340                 tx->tx_conn = conn;
1341                 LASSERT (tx->tx_msg->ibm_type != IBLND_MSG_PUT_DONE);
1342         } else {
1343                 /* PUT_DONE first attached to conn as a PUT_REQ */
1344                 LASSERT (tx->tx_conn == conn);
1345                 LASSERT (tx->tx_msg->ibm_type == IBLND_MSG_PUT_DONE);
1346         }
1347
1348         switch (tx->tx_msg->ibm_type) {
1349         default:
1350                 LBUG();
1351
1352         case IBLND_MSG_PUT_REQ:
1353         case IBLND_MSG_GET_REQ:
1354                 q = &conn->ibc_tx_queue_rsrvd;
1355                 break;
1356
1357         case IBLND_MSG_PUT_NAK:
1358         case IBLND_MSG_PUT_ACK:
1359         case IBLND_MSG_PUT_DONE:
1360         case IBLND_MSG_GET_DONE:
1361                 q = &conn->ibc_tx_queue_nocred;
1362                 break;
1363
1364         case IBLND_MSG_NOOP:
1365                 if (IBLND_OOB_CAPABLE(conn->ibc_version))
1366                         q = &conn->ibc_tx_queue_nocred;
1367                 else
1368                         q = &conn->ibc_tx_noops;
1369                 break;
1370
1371         case IBLND_MSG_IMMEDIATE:
1372                 q = &conn->ibc_tx_queue;
1373                 break;
1374         }
1375
1376         list_add_tail(&tx->tx_list, q);
1377 }
1378
1379 static void
1380 kiblnd_queue_tx(struct kib_tx *tx, struct kib_conn *conn)
1381 {
1382         spin_lock(&conn->ibc_lock);
1383         kiblnd_queue_tx_locked(tx, conn);
1384         kiblnd_check_sends_locked(conn);
1385         spin_unlock(&conn->ibc_lock);
1386 }
1387
1388 static int kiblnd_resolve_addr(struct rdma_cm_id *cmid,
1389                                struct sockaddr_in *srcaddr,
1390                                struct sockaddr_in *dstaddr,
1391                                int timeout_ms)
1392 {
1393         unsigned short port;
1394         int rc;
1395
1396         /* allow the port to be reused */
1397         rc = rdma_set_reuseaddr(cmid, 1);
1398         if (rc != 0) {
1399                 CERROR("Unable to set reuse on cmid: %d\n", rc);
1400                 return rc;
1401         }
1402
1403         /* look for a free privileged port */
1404         for (port = PROT_SOCK-1; port > 0; port--) {
1405                 srcaddr->sin_port = htons(port);
1406                 rc = rdma_resolve_addr(cmid,
1407                                        (struct sockaddr *)srcaddr,
1408                                        (struct sockaddr *)dstaddr,
1409                                        timeout_ms);
1410                 if (rc == 0) {
1411                         CDEBUG(D_NET, "bound to port %hu\n", port);
1412                         return 0;
1413                 } else if (rc == -EADDRINUSE || rc == -EADDRNOTAVAIL) {
1414                         CDEBUG(D_NET, "bind to port %hu failed: %d\n",
1415                                port, rc);
1416                 } else {
1417                         return rc;
1418                 }
1419         }
1420
1421         CERROR("Failed to bind to a free privileged port\n");
1422         return rc;
1423 }
1424
1425 static void
1426 kiblnd_connect_peer(struct kib_peer_ni *peer_ni)
1427 {
1428         struct rdma_cm_id *cmid;
1429         struct kib_dev *dev;
1430         struct kib_net *net = peer_ni->ibp_ni->ni_data;
1431         struct sockaddr_in srcaddr;
1432         struct sockaddr_in dstaddr;
1433         int rc;
1434
1435         LASSERT (net != NULL);
1436         LASSERT (peer_ni->ibp_connecting > 0);
1437
1438         cmid = kiblnd_rdma_create_id(peer_ni->ibp_ni->ni_net_ns,
1439                                      kiblnd_cm_callback, peer_ni,
1440                                      RDMA_PS_TCP, IB_QPT_RC);
1441
1442         if (IS_ERR(cmid)) {
1443                 CERROR("Can't create CMID for %s: %ld\n",
1444                        libcfs_nid2str(peer_ni->ibp_nid), PTR_ERR(cmid));
1445                 rc = PTR_ERR(cmid);
1446                 goto failed;
1447         }
1448
1449         dev = net->ibn_dev;
1450         memset(&srcaddr, 0, sizeof(srcaddr));
1451         srcaddr.sin_family = AF_INET;
1452         srcaddr.sin_addr.s_addr = htonl(dev->ibd_ifip);
1453
1454         memset(&dstaddr, 0, sizeof(dstaddr));
1455         dstaddr.sin_family = AF_INET;
1456         dstaddr.sin_port = htons(*kiblnd_tunables.kib_service);
1457         dstaddr.sin_addr.s_addr = htonl(LNET_NIDADDR(peer_ni->ibp_nid));
1458
1459         kiblnd_peer_addref(peer_ni);               /* cmid's ref */
1460
1461         if (*kiblnd_tunables.kib_use_priv_port) {
1462                 rc = kiblnd_resolve_addr(cmid, &srcaddr, &dstaddr,
1463                                          lnet_get_lnd_timeout() * 1000);
1464         } else {
1465                 rc = rdma_resolve_addr(cmid,
1466                                        (struct sockaddr *)&srcaddr,
1467                                        (struct sockaddr *)&dstaddr,
1468                                        lnet_get_lnd_timeout() * 1000);
1469         }
1470         if (rc != 0) {
1471                 /* Can't initiate address resolution:  */
1472                 CERROR("Can't resolve addr for %s: %d\n",
1473                        libcfs_nid2str(peer_ni->ibp_nid), rc);
1474                 goto failed2;
1475         }
1476
1477         return;
1478
1479  failed2:
1480         kiblnd_peer_connect_failed(peer_ni, 1, rc);
1481         kiblnd_peer_decref(peer_ni);               /* cmid's ref */
1482         rdma_destroy_id(cmid);
1483         return;
1484  failed:
1485         kiblnd_peer_connect_failed(peer_ni, 1, rc);
1486 }
1487
1488 bool
1489 kiblnd_reconnect_peer(struct kib_peer_ni *peer_ni)
1490 {
1491         rwlock_t         *glock = &kiblnd_data.kib_global_lock;
1492         char             *reason = NULL;
1493         struct list_head  txs;
1494         unsigned long     flags;
1495
1496         INIT_LIST_HEAD(&txs);
1497
1498         write_lock_irqsave(glock, flags);
1499         if (peer_ni->ibp_reconnecting == 0) {
1500                 if (peer_ni->ibp_accepting)
1501                         reason = "accepting";
1502                 else if (peer_ni->ibp_connecting)
1503                         reason = "connecting";
1504                 else if (!list_empty(&peer_ni->ibp_conns))
1505                         reason = "connected";
1506                 else /* connected then closed */
1507                         reason = "closed";
1508
1509                 goto no_reconnect;
1510         }
1511
1512         if (peer_ni->ibp_accepting)
1513                 CNETERR("Detecting race between accepting and reconnecting\n");
1514         peer_ni->ibp_reconnecting--;
1515
1516         if (!kiblnd_peer_active(peer_ni)) {
1517                 list_splice_init(&peer_ni->ibp_tx_queue, &txs);
1518                 reason = "unlinked";
1519                 goto no_reconnect;
1520         }
1521
1522         peer_ni->ibp_connecting++;
1523         peer_ni->ibp_reconnected++;
1524
1525         write_unlock_irqrestore(glock, flags);
1526
1527         kiblnd_connect_peer(peer_ni);
1528         return true;
1529
1530  no_reconnect:
1531         write_unlock_irqrestore(glock, flags);
1532
1533         CWARN("Abort reconnection of %s: %s\n",
1534               libcfs_nid2str(peer_ni->ibp_nid), reason);
1535         kiblnd_txlist_done(&txs, -ECONNABORTED,
1536                            LNET_MSG_STATUS_LOCAL_ABORTED);
1537         return false;
1538 }
1539
1540 void
1541 kiblnd_launch_tx(struct lnet_ni *ni, struct kib_tx *tx, lnet_nid_t nid)
1542 {
1543         struct kib_peer_ni *peer_ni;
1544         struct kib_peer_ni *peer2;
1545         struct kib_conn *conn;
1546         rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
1547         unsigned long      flags;
1548         int                rc;
1549         int                i;
1550         struct lnet_ioctl_config_o2iblnd_tunables *tunables;
1551
1552         /* If I get here, I've committed to send, so I complete the tx with
1553          * failure on any problems */
1554
1555         LASSERT (tx == NULL || tx->tx_conn == NULL); /* only set when assigned a conn */
1556         LASSERT (tx == NULL || tx->tx_nwrq > 0);     /* work items have been set up */
1557
1558         /* First time, just use a read lock since I expect to find my peer_ni
1559          * connected */
1560         read_lock_irqsave(g_lock, flags);
1561
1562         peer_ni = kiblnd_find_peer_locked(ni, nid);
1563         if (peer_ni != NULL && !list_empty(&peer_ni->ibp_conns)) {
1564                 /* Found a peer_ni with an established connection */
1565                 conn = kiblnd_get_conn_locked(peer_ni);
1566                 kiblnd_conn_addref(conn); /* 1 ref for me... */
1567
1568                 read_unlock_irqrestore(g_lock, flags);
1569
1570                 if (tx != NULL)
1571                         kiblnd_queue_tx(tx, conn);
1572                 kiblnd_conn_decref(conn); /* ...to here */
1573                 return;
1574         }
1575
1576         read_unlock(g_lock);
1577         /* Re-try with a write lock */
1578         write_lock(g_lock);
1579
1580         peer_ni = kiblnd_find_peer_locked(ni, nid);
1581         if (peer_ni != NULL) {
1582                 if (list_empty(&peer_ni->ibp_conns)) {
1583                         /* found a peer_ni, but it's still connecting... */
1584                         LASSERT(kiblnd_peer_connecting(peer_ni));
1585                         if (tx != NULL)
1586                                 list_add_tail(&tx->tx_list,
1587                                                   &peer_ni->ibp_tx_queue);
1588                         write_unlock_irqrestore(g_lock, flags);
1589                 } else {
1590                         conn = kiblnd_get_conn_locked(peer_ni);
1591                         kiblnd_conn_addref(conn); /* 1 ref for me... */
1592
1593                         write_unlock_irqrestore(g_lock, flags);
1594
1595                         if (tx != NULL)
1596                                 kiblnd_queue_tx(tx, conn);
1597                         kiblnd_conn_decref(conn); /* ...to here */
1598                 }
1599                 return;
1600         }
1601
1602         write_unlock_irqrestore(g_lock, flags);
1603
1604         /* Allocate a peer_ni ready to add to the peer_ni table and retry */
1605         rc = kiblnd_create_peer(ni, &peer_ni, nid);
1606         if (rc != 0) {
1607                 CERROR("Can't create peer_ni %s\n", libcfs_nid2str(nid));
1608                 if (tx != NULL) {
1609                         tx->tx_status = -EHOSTUNREACH;
1610                         tx->tx_waiting = 0;
1611                         tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
1612                         kiblnd_tx_done(tx);
1613                 }
1614                 return;
1615         }
1616
1617         write_lock_irqsave(g_lock, flags);
1618
1619         peer2 = kiblnd_find_peer_locked(ni, nid);
1620         if (peer2 != NULL) {
1621                 if (list_empty(&peer2->ibp_conns)) {
1622                         /* found a peer_ni, but it's still connecting... */
1623                         LASSERT(kiblnd_peer_connecting(peer2));
1624                         if (tx != NULL)
1625                                 list_add_tail(&tx->tx_list,
1626                                                   &peer2->ibp_tx_queue);
1627                         write_unlock_irqrestore(g_lock, flags);
1628                 } else {
1629                         conn = kiblnd_get_conn_locked(peer2);
1630                         kiblnd_conn_addref(conn); /* 1 ref for me... */
1631
1632                         write_unlock_irqrestore(g_lock, flags);
1633
1634                         if (tx != NULL)
1635                                 kiblnd_queue_tx(tx, conn);
1636                         kiblnd_conn_decref(conn); /* ...to here */
1637                 }
1638
1639                 kiblnd_peer_decref(peer_ni);
1640                 return;
1641         }
1642
1643         /* Brand new peer_ni */
1644         LASSERT(peer_ni->ibp_connecting == 0);
1645         tunables = &peer_ni->ibp_ni->ni_lnd_tunables.lnd_tun_u.lnd_o2ib;
1646         peer_ni->ibp_connecting = tunables->lnd_conns_per_peer;
1647
1648         /* always called with a ref on ni, which prevents ni being shutdown */
1649         LASSERT(((struct kib_net *)ni->ni_data)->ibn_shutdown == 0);
1650
1651         if (tx != NULL)
1652                 list_add_tail(&tx->tx_list, &peer_ni->ibp_tx_queue);
1653
1654         kiblnd_peer_addref(peer_ni);
1655         list_add_tail(&peer_ni->ibp_list, kiblnd_nid2peerlist(nid));
1656
1657         write_unlock_irqrestore(g_lock, flags);
1658
1659         for (i = 0; i < tunables->lnd_conns_per_peer; i++)
1660                 kiblnd_connect_peer(peer_ni);
1661         kiblnd_peer_decref(peer_ni);
1662 }
1663
1664 int
1665 kiblnd_send(struct lnet_ni *ni, void *private, struct lnet_msg *lntmsg)
1666 {
1667         struct lnet_hdr *hdr = &lntmsg->msg_hdr;
1668         int               type = lntmsg->msg_type;
1669         struct lnet_process_id target = lntmsg->msg_target;
1670         int               target_is_router = lntmsg->msg_target_is_router;
1671         int               routing = lntmsg->msg_routing;
1672         unsigned int      payload_niov = lntmsg->msg_niov;
1673         struct kvec      *payload_iov = lntmsg->msg_iov;
1674         lnet_kiov_t      *payload_kiov = lntmsg->msg_kiov;
1675         unsigned int      payload_offset = lntmsg->msg_offset;
1676         unsigned int      payload_nob = lntmsg->msg_len;
1677         struct kib_msg *ibmsg;
1678         struct kib_rdma_desc *rd;
1679         struct kib_tx *tx;
1680         int               nob;
1681         int               rc;
1682
1683         /* NB 'private' is different depending on what we're sending.... */
1684
1685         CDEBUG(D_NET, "sending %d bytes in %d frags to %s\n",
1686                payload_nob, payload_niov, libcfs_id2str(target));
1687
1688         LASSERT (payload_nob == 0 || payload_niov > 0);
1689         LASSERT (payload_niov <= LNET_MAX_IOV);
1690
1691         /* Thread context */
1692         LASSERT (!in_interrupt());
1693         /* payload is either all vaddrs or all pages */
1694         LASSERT (!(payload_kiov != NULL && payload_iov != NULL));
1695
1696         switch (type) {
1697         default:
1698                 LBUG();
1699                 return (-EIO);
1700
1701         case LNET_MSG_ACK:
1702                 LASSERT (payload_nob == 0);
1703                 break;
1704
1705         case LNET_MSG_GET:
1706                 if (routing || target_is_router)
1707                         break;                  /* send IMMEDIATE */
1708
1709                 /* is the REPLY message too small for RDMA? */
1710                 nob = offsetof(struct kib_msg, ibm_u.immediate.ibim_payload[lntmsg->msg_md->md_length]);
1711                 if (nob <= IBLND_MSG_SIZE)
1712                         break;                  /* send IMMEDIATE */
1713
1714                 tx = kiblnd_get_idle_tx(ni, target.nid);
1715                 if (tx == NULL) {
1716                         CERROR("Can't allocate txd for GET to %s\n",
1717                                libcfs_nid2str(target.nid));
1718                         return -ENOMEM;
1719                 }
1720
1721                 ibmsg = tx->tx_msg;
1722                 rd = &ibmsg->ibm_u.get.ibgm_rd;
1723                 if ((lntmsg->msg_md->md_options & LNET_MD_KIOV) == 0)
1724                         rc = kiblnd_setup_rd_iov(ni, tx, rd,
1725                                                  lntmsg->msg_md->md_niov,
1726                                                  lntmsg->msg_md->md_iov.iov,
1727                                                  0, lntmsg->msg_md->md_length);
1728                 else
1729                         rc = kiblnd_setup_rd_kiov(ni, tx, rd,
1730                                                   lntmsg->msg_md->md_niov,
1731                                                   lntmsg->msg_md->md_iov.kiov,
1732                                                   0, lntmsg->msg_md->md_length);
1733                 if (rc != 0) {
1734                         CERROR("Can't setup GET sink for %s: %d\n",
1735                                libcfs_nid2str(target.nid), rc);
1736                         tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
1737                         kiblnd_tx_done(tx);
1738                         return -EIO;
1739                 }
1740
1741                 nob = offsetof(struct kib_get_msg, ibgm_rd.rd_frags[rd->rd_nfrags]);
1742                 ibmsg->ibm_u.get.ibgm_cookie = tx->tx_cookie;
1743                 ibmsg->ibm_u.get.ibgm_hdr = *hdr;
1744
1745                 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_GET_REQ, nob);
1746
1747                 tx->tx_lntmsg[1] = lnet_create_reply_msg(ni, lntmsg);
1748                 if (tx->tx_lntmsg[1] == NULL) {
1749                         CERROR("Can't create reply for GET -> %s\n",
1750                                libcfs_nid2str(target.nid));
1751                         kiblnd_tx_done(tx);
1752                         return -EIO;
1753                 }
1754
1755                 tx->tx_lntmsg[0] = lntmsg;      /* finalise lntmsg[0,1] on completion */
1756                 tx->tx_waiting = 1;             /* waiting for GET_DONE */
1757                 kiblnd_launch_tx(ni, tx, target.nid);
1758                 return 0;
1759
1760         case LNET_MSG_REPLY:
1761         case LNET_MSG_PUT:
1762                 /* Is the payload small enough not to need RDMA? */
1763                 nob = offsetof(struct kib_msg, ibm_u.immediate.ibim_payload[payload_nob]);
1764                 if (nob <= IBLND_MSG_SIZE)
1765                         break;                  /* send IMMEDIATE */
1766
1767                 tx = kiblnd_get_idle_tx(ni, target.nid);
1768                 if (tx == NULL) {
1769                         CERROR("Can't allocate %s txd for %s\n",
1770                                type == LNET_MSG_PUT ? "PUT" : "REPLY",
1771                                libcfs_nid2str(target.nid));
1772                         return -ENOMEM;
1773                 }
1774
1775                 if (payload_kiov == NULL)
1776                         rc = kiblnd_setup_rd_iov(ni, tx, tx->tx_rd,
1777                                                  payload_niov, payload_iov,
1778                                                  payload_offset, payload_nob);
1779                 else
1780                         rc = kiblnd_setup_rd_kiov(ni, tx, tx->tx_rd,
1781                                                   payload_niov, payload_kiov,
1782                                                   payload_offset, payload_nob);
1783                 if (rc != 0) {
1784                         CERROR("Can't setup PUT src for %s: %d\n",
1785                                libcfs_nid2str(target.nid), rc);
1786                         kiblnd_tx_done(tx);
1787                         return -EIO;
1788                 }
1789
1790                 ibmsg = tx->tx_msg;
1791                 ibmsg->ibm_u.putreq.ibprm_hdr = *hdr;
1792                 ibmsg->ibm_u.putreq.ibprm_cookie = tx->tx_cookie;
1793                 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_PUT_REQ,
1794                                    sizeof(struct kib_putreq_msg));
1795
1796                 tx->tx_lntmsg[0] = lntmsg;      /* finalise lntmsg on completion */
1797                 tx->tx_waiting = 1;             /* waiting for PUT_{ACK,NAK} */
1798                 kiblnd_launch_tx(ni, tx, target.nid);
1799                 return 0;
1800         }
1801
1802         /* send IMMEDIATE */
1803         LASSERT(offsetof(struct kib_msg, ibm_u.immediate.ibim_payload[payload_nob])
1804                 <= IBLND_MSG_SIZE);
1805
1806         tx = kiblnd_get_idle_tx(ni, target.nid);
1807         if (tx == NULL) {
1808                 CERROR ("Can't send %d to %s: tx descs exhausted\n",
1809                         type, libcfs_nid2str(target.nid));
1810                 return -ENOMEM;
1811         }
1812
1813         ibmsg = tx->tx_msg;
1814         ibmsg->ibm_u.immediate.ibim_hdr = *hdr;
1815
1816         if (payload_kiov != NULL)
1817                 lnet_copy_kiov2flat(IBLND_MSG_SIZE, ibmsg,
1818                                     offsetof(struct kib_msg, ibm_u.immediate.ibim_payload),
1819                                     payload_niov, payload_kiov,
1820                                     payload_offset, payload_nob);
1821         else
1822                 lnet_copy_iov2flat(IBLND_MSG_SIZE, ibmsg,
1823                                    offsetof(struct kib_msg, ibm_u.immediate.ibim_payload),
1824                                    payload_niov, payload_iov,
1825                                    payload_offset, payload_nob);
1826
1827         nob = offsetof(struct kib_immediate_msg, ibim_payload[payload_nob]);
1828         kiblnd_init_tx_msg(ni, tx, IBLND_MSG_IMMEDIATE, nob);
1829
1830         tx->tx_lntmsg[0] = lntmsg;              /* finalise lntmsg on completion */
1831         kiblnd_launch_tx(ni, tx, target.nid);
1832         return 0;
1833 }
1834
1835 static void
1836 kiblnd_reply(struct lnet_ni *ni, struct kib_rx *rx, struct lnet_msg *lntmsg)
1837 {
1838         struct lnet_process_id target = lntmsg->msg_target;
1839         unsigned int      niov = lntmsg->msg_niov;
1840         struct kvec      *iov = lntmsg->msg_iov;
1841         lnet_kiov_t      *kiov = lntmsg->msg_kiov;
1842         unsigned int      offset = lntmsg->msg_offset;
1843         unsigned int      nob = lntmsg->msg_len;
1844         struct kib_tx *tx;
1845         int               rc;
1846
1847         tx = kiblnd_get_idle_tx(ni, rx->rx_conn->ibc_peer->ibp_nid);
1848         if (tx == NULL) {
1849                 CERROR("Can't get tx for REPLY to %s\n",
1850                        libcfs_nid2str(target.nid));
1851                 goto failed_0;
1852         }
1853
1854         if (nob == 0)
1855                 rc = 0;
1856         else if (kiov == NULL)
1857                 rc = kiblnd_setup_rd_iov(ni, tx, tx->tx_rd,
1858                                          niov, iov, offset, nob);
1859         else
1860                 rc = kiblnd_setup_rd_kiov(ni, tx, tx->tx_rd,
1861                                           niov, kiov, offset, nob);
1862
1863         if (rc != 0) {
1864                 CERROR("Can't setup GET src for %s: %d\n",
1865                        libcfs_nid2str(target.nid), rc);
1866                 goto failed_1;
1867         }
1868
1869         rc = kiblnd_init_rdma(rx->rx_conn, tx,
1870                               IBLND_MSG_GET_DONE, nob,
1871                               &rx->rx_msg->ibm_u.get.ibgm_rd,
1872                               rx->rx_msg->ibm_u.get.ibgm_cookie);
1873         if (rc < 0) {
1874                 CERROR("Can't setup rdma for GET from %s: %d\n",
1875                        libcfs_nid2str(target.nid), rc);
1876                 goto failed_1;
1877         }
1878
1879         if (nob == 0) {
1880                 /* No RDMA: local completion may happen now! */
1881                 lnet_finalize(lntmsg, 0);
1882         } else {
1883                 /* RDMA: lnet_finalize(lntmsg) when it
1884                  * completes */
1885                 tx->tx_lntmsg[0] = lntmsg;
1886         }
1887
1888         kiblnd_queue_tx(tx, rx->rx_conn);
1889         return;
1890
1891
1892 failed_1:
1893         tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
1894         kiblnd_tx_done(tx);
1895 failed_0:
1896         lnet_finalize(lntmsg, -EIO);
1897 }
1898
1899 int
1900 kiblnd_recv(struct lnet_ni *ni, void *private, struct lnet_msg *lntmsg,
1901             int delayed, unsigned int niov, struct kvec *iov, lnet_kiov_t *kiov,
1902             unsigned int offset, unsigned int mlen, unsigned int rlen)
1903 {
1904         struct kib_rx *rx = private;
1905         struct kib_msg *rxmsg = rx->rx_msg;
1906         struct kib_conn *conn = rx->rx_conn;
1907         struct kib_tx *tx;
1908         __u64        ibprm_cookie;
1909         int          nob;
1910         int          post_credit = IBLND_POSTRX_PEER_CREDIT;
1911         int          rc = 0;
1912
1913         LASSERT (mlen <= rlen);
1914         LASSERT (!in_interrupt());
1915         /* Either all pages or all vaddrs */
1916         LASSERT (!(kiov != NULL && iov != NULL));
1917
1918         switch (rxmsg->ibm_type) {
1919         default:
1920                 LBUG();
1921
1922         case IBLND_MSG_IMMEDIATE:
1923                 nob = offsetof(struct kib_msg, ibm_u.immediate.ibim_payload[rlen]);
1924                 if (nob > rx->rx_nob) {
1925                         CERROR ("Immediate message from %s too big: %d(%d)\n",
1926                                 libcfs_nid2str(rxmsg->ibm_u.immediate.ibim_hdr.src_nid),
1927                                 nob, rx->rx_nob);
1928                         rc = -EPROTO;
1929                         break;
1930                 }
1931
1932                 if (kiov != NULL)
1933                         lnet_copy_flat2kiov(niov, kiov, offset,
1934                                             IBLND_MSG_SIZE, rxmsg,
1935                                             offsetof(struct kib_msg, ibm_u.immediate.ibim_payload),
1936                                             mlen);
1937                 else
1938                         lnet_copy_flat2iov(niov, iov, offset,
1939                                            IBLND_MSG_SIZE, rxmsg,
1940                                            offsetof(struct kib_msg, ibm_u.immediate.ibim_payload),
1941                                            mlen);
1942                 lnet_finalize(lntmsg, 0);
1943                 break;
1944
1945         case IBLND_MSG_PUT_REQ: {
1946                 struct kib_msg  *txmsg;
1947                 struct kib_rdma_desc *rd;
1948                 ibprm_cookie = rxmsg->ibm_u.putreq.ibprm_cookie;
1949
1950                 if (mlen == 0) {
1951                         lnet_finalize(lntmsg, 0);
1952                         kiblnd_send_completion(rx->rx_conn, IBLND_MSG_PUT_NAK,
1953                                                0, ibprm_cookie);
1954                         break;
1955                 }
1956
1957                 tx = kiblnd_get_idle_tx(ni, conn->ibc_peer->ibp_nid);
1958                 if (tx == NULL) {
1959                         CERROR("Can't allocate tx for %s\n",
1960                                libcfs_nid2str(conn->ibc_peer->ibp_nid));
1961                         /* Not replying will break the connection */
1962                         rc = -ENOMEM;
1963                         break;
1964                 }
1965
1966                 txmsg = tx->tx_msg;
1967                 rd = &txmsg->ibm_u.putack.ibpam_rd;
1968                 if (kiov == NULL)
1969                         rc = kiblnd_setup_rd_iov(ni, tx, rd,
1970                                                  niov, iov, offset, mlen);
1971                 else
1972                         rc = kiblnd_setup_rd_kiov(ni, tx, rd,
1973                                                   niov, kiov, offset, mlen);
1974                 if (rc != 0) {
1975                         CERROR("Can't setup PUT sink for %s: %d\n",
1976                                libcfs_nid2str(conn->ibc_peer->ibp_nid), rc);
1977                         tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
1978                         kiblnd_tx_done(tx);
1979                         /* tell peer_ni it's over */
1980                         kiblnd_send_completion(rx->rx_conn, IBLND_MSG_PUT_NAK,
1981                                                rc, ibprm_cookie);
1982                         break;
1983                 }
1984
1985                 nob = offsetof(struct kib_putack_msg, ibpam_rd.rd_frags[rd->rd_nfrags]);
1986                 txmsg->ibm_u.putack.ibpam_src_cookie = ibprm_cookie;
1987                 txmsg->ibm_u.putack.ibpam_dst_cookie = tx->tx_cookie;
1988
1989                 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_PUT_ACK, nob);
1990
1991                 tx->tx_lntmsg[0] = lntmsg;      /* finalise lntmsg on completion */
1992                 tx->tx_waiting = 1;             /* waiting for PUT_DONE */
1993                 kiblnd_queue_tx(tx, conn);
1994
1995                 /* reposted buffer reserved for PUT_DONE */
1996                 post_credit = IBLND_POSTRX_NO_CREDIT;
1997                 break;
1998                 }
1999
2000         case IBLND_MSG_GET_REQ:
2001                 if (lntmsg != NULL) {
2002                         /* Optimized GET; RDMA lntmsg's payload */
2003                         kiblnd_reply(ni, rx, lntmsg);
2004                 } else {
2005                         /* GET didn't match anything */
2006                         kiblnd_send_completion(rx->rx_conn, IBLND_MSG_GET_DONE,
2007                                                -ENODATA,
2008                                                rxmsg->ibm_u.get.ibgm_cookie);
2009                 }
2010                 break;
2011         }
2012
2013         kiblnd_post_rx(rx, post_credit);
2014         return rc;
2015 }
2016
2017 int
2018 kiblnd_thread_start(int (*fn)(void *arg), void *arg, char *name)
2019 {
2020         struct task_struct *task = kthread_run(fn, arg, name);
2021
2022         if (IS_ERR(task))
2023                 return PTR_ERR(task);
2024
2025         atomic_inc(&kiblnd_data.kib_nthreads);
2026         return 0;
2027 }
2028
2029 static void
2030 kiblnd_thread_fini (void)
2031 {
2032         atomic_dec (&kiblnd_data.kib_nthreads);
2033 }
2034
2035 static void
2036 kiblnd_peer_alive(struct kib_peer_ni *peer_ni)
2037 {
2038         /* This is racy, but everyone's only writing ktime_get_seconds() */
2039         peer_ni->ibp_last_alive = ktime_get_seconds();
2040         smp_mb();
2041 }
2042
2043 static void
2044 kiblnd_peer_notify(struct kib_peer_ni *peer_ni)
2045 {
2046         int           error = 0;
2047         time64_t last_alive = 0;
2048         unsigned long flags;
2049
2050         read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2051
2052         if (kiblnd_peer_idle(peer_ni) && peer_ni->ibp_error != 0) {
2053                 error = peer_ni->ibp_error;
2054                 peer_ni->ibp_error = 0;
2055
2056                 last_alive = peer_ni->ibp_last_alive;
2057         }
2058
2059         read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2060
2061         if (error != 0)
2062                 lnet_notify(peer_ni->ibp_ni,
2063                             peer_ni->ibp_nid, 0, last_alive);
2064 }
2065
2066 void
2067 kiblnd_close_conn_locked(struct kib_conn *conn, int error)
2068 {
2069         /* This just does the immediate housekeeping.  'error' is zero for a
2070          * normal shutdown which can happen only after the connection has been
2071          * established.  If the connection is established, schedule the
2072          * connection to be finished off by the connd.  Otherwise the connd is
2073          * already dealing with it (either to set it up or tear it down).
2074          * Caller holds kib_global_lock exclusively in irq context */
2075         struct kib_peer_ni *peer_ni = conn->ibc_peer;
2076         struct kib_dev *dev;
2077         unsigned long flags;
2078
2079         LASSERT (error != 0 || conn->ibc_state >= IBLND_CONN_ESTABLISHED);
2080
2081         if (error != 0 && conn->ibc_comms_error == 0)
2082                 conn->ibc_comms_error = error;
2083
2084         if (conn->ibc_state != IBLND_CONN_ESTABLISHED)
2085                 return; /* already being handled  */
2086
2087         if (error == 0 &&
2088             list_empty(&conn->ibc_tx_noops) &&
2089             list_empty(&conn->ibc_tx_queue) &&
2090             list_empty(&conn->ibc_tx_queue_rsrvd) &&
2091             list_empty(&conn->ibc_tx_queue_nocred) &&
2092             list_empty(&conn->ibc_active_txs)) {
2093                 CDEBUG(D_NET, "closing conn to %s\n", 
2094                        libcfs_nid2str(peer_ni->ibp_nid));
2095         } else {
2096                 CNETERR("Closing conn to %s: error %d%s%s%s%s%s\n",
2097                        libcfs_nid2str(peer_ni->ibp_nid), error,
2098                        list_empty(&conn->ibc_tx_queue) ? "" : "(sending)",
2099                        list_empty(&conn->ibc_tx_noops) ? "" : "(sending_noops)",
2100                        list_empty(&conn->ibc_tx_queue_rsrvd) ?
2101                                                 "" : "(sending_rsrvd)",
2102                        list_empty(&conn->ibc_tx_queue_nocred) ?
2103                                                  "" : "(sending_nocred)",
2104                        list_empty(&conn->ibc_active_txs) ? "" : "(waiting)");
2105         }
2106
2107         dev = ((struct kib_net *)peer_ni->ibp_ni->ni_data)->ibn_dev;
2108         if (peer_ni->ibp_next_conn == conn)
2109                 /* clear next_conn so it won't be used */
2110                 peer_ni->ibp_next_conn = NULL;
2111         list_del(&conn->ibc_list);
2112         /* connd (see below) takes over ibc_list's ref */
2113
2114         if (list_empty(&peer_ni->ibp_conns) &&    /* no more conns */
2115             kiblnd_peer_active(peer_ni)) {         /* still in peer_ni table */
2116                 kiblnd_unlink_peer_locked(peer_ni);
2117
2118                 /* set/clear error on last conn */
2119                 peer_ni->ibp_error = conn->ibc_comms_error;
2120         }
2121
2122         kiblnd_set_conn_state(conn, IBLND_CONN_CLOSING);
2123
2124         if (error != 0 &&
2125             kiblnd_dev_can_failover(dev)) {
2126                 list_add_tail(&dev->ibd_fail_list,
2127                               &kiblnd_data.kib_failed_devs);
2128                 wake_up(&kiblnd_data.kib_failover_waitq);
2129         }
2130
2131         spin_lock_irqsave(&kiblnd_data.kib_connd_lock, flags);
2132
2133         list_add_tail(&conn->ibc_list, &kiblnd_data.kib_connd_conns);
2134         wake_up(&kiblnd_data.kib_connd_waitq);
2135
2136         spin_unlock_irqrestore(&kiblnd_data.kib_connd_lock, flags);
2137 }
2138
2139 void
2140 kiblnd_close_conn(struct kib_conn *conn, int error)
2141 {
2142         unsigned long flags;
2143
2144         write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2145
2146         kiblnd_close_conn_locked(conn, error);
2147
2148         write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2149 }
2150
2151 static void
2152 kiblnd_handle_early_rxs(struct kib_conn *conn)
2153 {
2154         unsigned long flags;
2155         struct kib_rx *rx;
2156
2157         LASSERT(!in_interrupt());
2158         LASSERT(conn->ibc_state >= IBLND_CONN_ESTABLISHED);
2159
2160         write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2161         while (!list_empty(&conn->ibc_early_rxs)) {
2162                 rx = list_entry(conn->ibc_early_rxs.next,
2163                                 struct kib_rx, rx_list);
2164                 list_del(&rx->rx_list);
2165                 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2166
2167                 kiblnd_handle_rx(rx);
2168
2169                 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2170         }
2171         write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2172 }
2173
2174 void
2175 kiblnd_abort_txs(struct kib_conn *conn, struct list_head *txs)
2176 {
2177         struct list_head         zombies = LIST_HEAD_INIT(zombies);
2178         struct list_head        *tmp;
2179         struct list_head        *nxt;
2180         struct kib_tx *tx;
2181
2182         spin_lock(&conn->ibc_lock);
2183
2184         list_for_each_safe(tmp, nxt, txs) {
2185                 tx = list_entry(tmp, struct kib_tx, tx_list);
2186
2187                 if (txs == &conn->ibc_active_txs) {
2188                         LASSERT(!tx->tx_queued);
2189                         LASSERT(tx->tx_waiting ||
2190                                 tx->tx_sending != 0);
2191                         if (conn->ibc_comms_error == -ETIMEDOUT) {
2192                                 if (tx->tx_waiting && !tx->tx_sending)
2193                                         tx->tx_hstatus =
2194                                           LNET_MSG_STATUS_REMOTE_TIMEOUT;
2195                                 else if (tx->tx_sending)
2196                                         tx->tx_hstatus =
2197                                           LNET_MSG_STATUS_NETWORK_TIMEOUT;
2198                         }
2199                 } else {
2200                         LASSERT(tx->tx_queued);
2201                         if (conn->ibc_comms_error == -ETIMEDOUT)
2202                                 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_TIMEOUT;
2203                         else
2204                                 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
2205                 }
2206
2207                 tx->tx_status = -ECONNABORTED;
2208                 tx->tx_waiting = 0;
2209
2210                 /*
2211                  * TODO: This makes an assumption that
2212                  * kiblnd_tx_complete() will be called for each tx. If
2213                  * that event is dropped we could end up with stale
2214                  * connections floating around. We'd like to deal with
2215                  * that in a better way.
2216                  *
2217                  * Also that means we can exceed the timeout by many
2218                  * seconds.
2219                  */
2220                 if (tx->tx_sending == 0) {
2221                         tx->tx_queued = 0;
2222                         list_del(&tx->tx_list);
2223                         list_add(&tx->tx_list, &zombies);
2224                 }
2225         }
2226
2227         spin_unlock(&conn->ibc_lock);
2228
2229         /*
2230          * aborting transmits occurs when finalizing the connection.
2231          * The connection is finalized on error.
2232          * Passing LNET_MSG_STATUS_OK to txlist_done() will not
2233          * override the value already set in tx->tx_hstatus above.
2234          */
2235         kiblnd_txlist_done(&zombies, -ECONNABORTED, LNET_MSG_STATUS_OK);
2236 }
2237
2238 static void
2239 kiblnd_finalise_conn(struct kib_conn *conn)
2240 {
2241         LASSERT (!in_interrupt());
2242         LASSERT (conn->ibc_state > IBLND_CONN_INIT);
2243
2244         /* abort_receives moves QP state to IB_QPS_ERR.  This is only required
2245          * for connections that didn't get as far as being connected, because
2246          * rdma_disconnect() does this for free. */
2247         kiblnd_abort_receives(conn);
2248
2249         kiblnd_set_conn_state(conn, IBLND_CONN_DISCONNECTED);
2250
2251         /* Complete all tx descs not waiting for sends to complete.
2252          * NB we should be safe from RDMA now that the QP has changed state */
2253
2254         kiblnd_abort_txs(conn, &conn->ibc_tx_noops);
2255         kiblnd_abort_txs(conn, &conn->ibc_tx_queue);
2256         kiblnd_abort_txs(conn, &conn->ibc_tx_queue_rsrvd);
2257         kiblnd_abort_txs(conn, &conn->ibc_tx_queue_nocred);
2258         kiblnd_abort_txs(conn, &conn->ibc_active_txs);
2259
2260         kiblnd_handle_early_rxs(conn);
2261 }
2262
2263 static void
2264 kiblnd_peer_connect_failed(struct kib_peer_ni *peer_ni, int active,
2265                            int error)
2266 {
2267         struct list_head zombies = LIST_HEAD_INIT(zombies);
2268         unsigned long   flags;
2269
2270         LASSERT (error != 0);
2271         LASSERT (!in_interrupt());
2272
2273         write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2274
2275         if (active) {
2276                 LASSERT(peer_ni->ibp_connecting > 0);
2277                 peer_ni->ibp_connecting--;
2278         } else {
2279                 LASSERT (peer_ni->ibp_accepting > 0);
2280                 peer_ni->ibp_accepting--;
2281         }
2282
2283         if (kiblnd_peer_connecting(peer_ni)) {
2284                 /* another connection attempt under way... */
2285                 write_unlock_irqrestore(&kiblnd_data.kib_global_lock,
2286                                         flags);
2287                 return;
2288         }
2289
2290         peer_ni->ibp_reconnected = 0;
2291         if (list_empty(&peer_ni->ibp_conns)) {
2292                 /* Take peer_ni's blocked transmits to complete with error */
2293                 list_splice_init(&peer_ni->ibp_tx_queue, &zombies);
2294
2295                 if (kiblnd_peer_active(peer_ni))
2296                         kiblnd_unlink_peer_locked(peer_ni);
2297
2298                 peer_ni->ibp_error = error;
2299         } else {
2300                 /* Can't have blocked transmits if there are connections */
2301                 LASSERT(list_empty(&peer_ni->ibp_tx_queue));
2302         }
2303
2304         write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2305
2306         kiblnd_peer_notify(peer_ni);
2307
2308         if (list_empty(&zombies))
2309                 return;
2310
2311         CNETERR("Deleting messages for %s: connection failed\n",
2312                 libcfs_nid2str(peer_ni->ibp_nid));
2313
2314         kiblnd_txlist_done(&zombies, error,
2315                            LNET_MSG_STATUS_LOCAL_DROPPED);
2316 }
2317
2318 static void
2319 kiblnd_connreq_done(struct kib_conn *conn, int status)
2320 {
2321         struct kib_peer_ni *peer_ni = conn->ibc_peer;
2322         struct kib_tx *tx;
2323         struct list_head txs;
2324         unsigned long    flags;
2325         int              active;
2326
2327         active = (conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2328
2329         CDEBUG(D_NET,"%s: active(%d), version(%x), status(%d)\n",
2330                libcfs_nid2str(peer_ni->ibp_nid), active,
2331                conn->ibc_version, status);
2332
2333         LASSERT (!in_interrupt());
2334         LASSERT ((conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT &&
2335                   peer_ni->ibp_connecting > 0) ||
2336                  (conn->ibc_state == IBLND_CONN_PASSIVE_WAIT &&
2337                   peer_ni->ibp_accepting > 0));
2338
2339         LIBCFS_FREE(conn->ibc_connvars, sizeof(*conn->ibc_connvars));
2340         conn->ibc_connvars = NULL;
2341
2342         if (status != 0) {
2343                 /* failed to establish connection */
2344                 kiblnd_peer_connect_failed(peer_ni, active, status);
2345                 kiblnd_finalise_conn(conn);
2346                 return;
2347         }
2348
2349         /* connection established */
2350         write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2351
2352         conn->ibc_last_send = ktime_get();
2353         kiblnd_set_conn_state(conn, IBLND_CONN_ESTABLISHED);
2354         kiblnd_peer_alive(peer_ni);
2355
2356         /* Add conn to peer_ni's list and nuke any dangling conns from a different
2357          * peer_ni instance... */
2358         kiblnd_conn_addref(conn);       /* +1 ref for ibc_list */
2359         list_add(&conn->ibc_list, &peer_ni->ibp_conns);
2360         peer_ni->ibp_reconnected = 0;
2361         if (active)
2362                 peer_ni->ibp_connecting--;
2363         else
2364                 peer_ni->ibp_accepting--;
2365
2366         if (peer_ni->ibp_version == 0) {
2367                 peer_ni->ibp_version     = conn->ibc_version;
2368                 peer_ni->ibp_incarnation = conn->ibc_incarnation;
2369         }
2370
2371         if (peer_ni->ibp_version     != conn->ibc_version ||
2372             peer_ni->ibp_incarnation != conn->ibc_incarnation) {
2373                 kiblnd_close_stale_conns_locked(peer_ni, conn->ibc_version,
2374                                                 conn->ibc_incarnation);
2375                 peer_ni->ibp_version     = conn->ibc_version;
2376                 peer_ni->ibp_incarnation = conn->ibc_incarnation;
2377         }
2378
2379         /* grab pending txs while I have the lock */
2380         INIT_LIST_HEAD(&txs);
2381         list_splice_init(&peer_ni->ibp_tx_queue, &txs);
2382
2383         if (!kiblnd_peer_active(peer_ni) ||        /* peer_ni has been deleted */
2384             conn->ibc_comms_error != 0) {       /* error has happened already */
2385
2386                 /* start to shut down connection */
2387                 kiblnd_close_conn_locked(conn, -ECONNABORTED);
2388                 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2389
2390                 kiblnd_txlist_done(&txs, -ECONNABORTED,
2391                                    LNET_MSG_STATUS_LOCAL_ERROR);
2392
2393                 return;
2394         }
2395
2396         /* +1 ref for myself, this connection is visible to other threads
2397          * now, refcount of peer:ibp_conns can be released by connection
2398          * close from either a different thread, or the calling of
2399          * kiblnd_check_sends_locked() below. See bz21911 for details.
2400          */
2401         kiblnd_conn_addref(conn);
2402         write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2403
2404         /* Schedule blocked txs
2405          * Note: if we are running with conns_per_peer > 1, these blocked
2406          * txs will all get scheduled to the first connection which gets
2407          * scheduled.  We won't be using round robin on this first batch.
2408          */
2409         spin_lock(&conn->ibc_lock);
2410         while (!list_empty(&txs)) {
2411                 tx = list_entry(txs.next, struct kib_tx, tx_list);
2412                 list_del(&tx->tx_list);
2413
2414                 kiblnd_queue_tx_locked(tx, conn);
2415         }
2416         kiblnd_check_sends_locked(conn);
2417         spin_unlock(&conn->ibc_lock);
2418
2419         /* schedule blocked rxs */
2420         kiblnd_handle_early_rxs(conn);
2421         kiblnd_conn_decref(conn);
2422 }
2423
2424 static void
2425 kiblnd_reject(struct rdma_cm_id *cmid, struct kib_rej *rej)
2426 {
2427         int          rc;
2428
2429 #ifdef HAVE_RDMA_REJECT_4ARGS
2430         rc = rdma_reject(cmid, rej, sizeof(*rej), IB_CM_REJ_CONSUMER_DEFINED);
2431 #else
2432         rc = rdma_reject(cmid, rej, sizeof(*rej));
2433 #endif
2434
2435         if (rc != 0)
2436                 CWARN("Error %d sending reject\n", rc);
2437 }
2438
2439 static int
2440 kiblnd_passive_connect(struct rdma_cm_id *cmid, void *priv, int priv_nob)
2441 {
2442         rwlock_t                *g_lock = &kiblnd_data.kib_global_lock;
2443         struct kib_msg *reqmsg = priv;
2444         struct kib_msg *ackmsg;
2445         struct kib_dev *ibdev;
2446         struct kib_peer_ni *peer_ni;
2447         struct kib_peer_ni *peer2;
2448         struct kib_conn *conn;
2449         struct lnet_ni *ni = NULL;
2450         struct kib_net *net = NULL;
2451         lnet_nid_t             nid;
2452         struct rdma_conn_param cp;
2453         struct kib_rej rej;
2454         int                    version = IBLND_MSG_VERSION;
2455         unsigned long          flags;
2456         int                    rc;
2457         struct sockaddr_in    *peer_addr;
2458         LASSERT (!in_interrupt());
2459
2460         /* cmid inherits 'context' from the corresponding listener id */
2461         ibdev = cmid->context;
2462         LASSERT(ibdev);
2463
2464         memset(&rej, 0, sizeof(rej));
2465         rej.ibr_magic                = IBLND_MSG_MAGIC;
2466         rej.ibr_why                  = IBLND_REJECT_FATAL;
2467         rej.ibr_cp.ibcp_max_msg_size = IBLND_MSG_SIZE;
2468
2469         peer_addr = (struct sockaddr_in *)&(cmid->route.addr.dst_addr);
2470         if (*kiblnd_tunables.kib_require_priv_port &&
2471             ntohs(peer_addr->sin_port) >= PROT_SOCK) {
2472                 __u32 ip = ntohl(peer_addr->sin_addr.s_addr);
2473                 CERROR("peer_ni's port (%pI4h:%hu) is not privileged\n",
2474                        &ip, ntohs(peer_addr->sin_port));
2475                 goto failed;
2476         }
2477
2478         if (priv_nob < offsetof(struct kib_msg, ibm_type)) {
2479                 CERROR("Short connection request\n");
2480                 goto failed;
2481         }
2482
2483         /* Future protocol version compatibility support!  If the
2484          * o2iblnd-specific protocol changes, or when LNET unifies
2485          * protocols over all LNDs, the initial connection will
2486          * negotiate a protocol version.  I trap this here to avoid
2487          * console errors; the reject tells the peer_ni which protocol I
2488          * speak. */
2489         if (reqmsg->ibm_magic == LNET_PROTO_MAGIC ||
2490             reqmsg->ibm_magic == __swab32(LNET_PROTO_MAGIC))
2491                 goto failed;
2492         if (reqmsg->ibm_magic == IBLND_MSG_MAGIC &&
2493             reqmsg->ibm_version != IBLND_MSG_VERSION &&
2494             reqmsg->ibm_version != IBLND_MSG_VERSION_1)
2495                 goto failed;
2496         if (reqmsg->ibm_magic == __swab32(IBLND_MSG_MAGIC) &&
2497             reqmsg->ibm_version != __swab16(IBLND_MSG_VERSION) &&
2498             reqmsg->ibm_version != __swab16(IBLND_MSG_VERSION_1))
2499                 goto failed;
2500
2501         rc = kiblnd_unpack_msg(reqmsg, priv_nob);
2502         if (rc != 0) {
2503                 CERROR("Can't parse connection request: %d\n", rc);
2504                 goto failed;
2505         }
2506
2507         nid = reqmsg->ibm_srcnid;
2508         ni  = lnet_nid2ni_addref(reqmsg->ibm_dstnid);
2509
2510         if (ni != NULL) {
2511                 net = (struct kib_net *)ni->ni_data;
2512                 rej.ibr_incarnation = net->ibn_incarnation;
2513         }
2514
2515         if (ni == NULL ||                         /* no matching net */
2516             ni->ni_nid != reqmsg->ibm_dstnid ||   /* right NET, wrong NID! */
2517             net->ibn_dev != ibdev) {              /* wrong device */
2518                 CERROR("Can't accept conn from %s on %s (%s:%d:%pI4h): "
2519                        "bad dst nid %s\n", libcfs_nid2str(nid),
2520                        ni == NULL ? "NA" : libcfs_nid2str(ni->ni_nid),
2521                        ibdev->ibd_ifname, ibdev->ibd_nnets,
2522                         &ibdev->ibd_ifip,
2523                        libcfs_nid2str(reqmsg->ibm_dstnid));
2524
2525                 goto failed;
2526         }
2527
2528        /* check time stamp as soon as possible */
2529         if (reqmsg->ibm_dststamp != 0 &&
2530             reqmsg->ibm_dststamp != net->ibn_incarnation) {
2531                 CWARN("Stale connection request\n");
2532                 rej.ibr_why = IBLND_REJECT_CONN_STALE;
2533                 goto failed;
2534         }
2535
2536         /* I can accept peer_ni's version */
2537         version = reqmsg->ibm_version;
2538
2539         if (reqmsg->ibm_type != IBLND_MSG_CONNREQ) {
2540                 CERROR("Unexpected connreq msg type: %x from %s\n",
2541                        reqmsg->ibm_type, libcfs_nid2str(nid));
2542                 goto failed;
2543         }
2544
2545         if (reqmsg->ibm_u.connparams.ibcp_queue_depth >
2546             kiblnd_msg_queue_size(version, ni)) {
2547                 CERROR("Can't accept conn from %s, queue depth too large: "
2548                        " %d (<=%d wanted)\n",
2549                        libcfs_nid2str(nid),
2550                        reqmsg->ibm_u.connparams.ibcp_queue_depth,
2551                        kiblnd_msg_queue_size(version, ni));
2552
2553                 if (version == IBLND_MSG_VERSION)
2554                         rej.ibr_why = IBLND_REJECT_MSG_QUEUE_SIZE;
2555
2556                 goto failed;
2557         }
2558
2559         if (reqmsg->ibm_u.connparams.ibcp_max_frags >
2560             IBLND_MAX_RDMA_FRAGS) {
2561                 CWARN("Can't accept conn from %s (version %x): "
2562                       "max_frags %d too large (%d wanted)\n",
2563                       libcfs_nid2str(nid), version,
2564                       reqmsg->ibm_u.connparams.ibcp_max_frags,
2565                       IBLND_MAX_RDMA_FRAGS);
2566
2567                 if (version >= IBLND_MSG_VERSION)
2568                         rej.ibr_why = IBLND_REJECT_RDMA_FRAGS;
2569
2570                 goto failed;
2571         } else if (reqmsg->ibm_u.connparams.ibcp_max_frags <
2572                    IBLND_MAX_RDMA_FRAGS &&
2573                    net->ibn_fmr_ps == NULL) {
2574                 CWARN("Can't accept conn from %s (version %x): "
2575                       "max_frags %d incompatible without FMR pool "
2576                       "(%d wanted)\n",
2577                       libcfs_nid2str(nid), version,
2578                       reqmsg->ibm_u.connparams.ibcp_max_frags,
2579                       IBLND_MAX_RDMA_FRAGS);
2580
2581                 if (version == IBLND_MSG_VERSION)
2582                         rej.ibr_why = IBLND_REJECT_RDMA_FRAGS;
2583
2584                 goto failed;
2585         }
2586
2587         if (reqmsg->ibm_u.connparams.ibcp_max_msg_size > IBLND_MSG_SIZE) {
2588                 CERROR("Can't accept %s: message size %d too big (%d max)\n",
2589                        libcfs_nid2str(nid),
2590                        reqmsg->ibm_u.connparams.ibcp_max_msg_size,
2591                        IBLND_MSG_SIZE);
2592                 goto failed;
2593         }
2594
2595         /* assume 'nid' is a new peer_ni; create  */
2596         rc = kiblnd_create_peer(ni, &peer_ni, nid);
2597         if (rc != 0) {
2598                 CERROR("Can't create peer_ni for %s\n", libcfs_nid2str(nid));
2599                 rej.ibr_why = IBLND_REJECT_NO_RESOURCES;
2600                 goto failed;
2601         }
2602
2603         /* We have validated the peer's parameters so use those */
2604         peer_ni->ibp_max_frags = reqmsg->ibm_u.connparams.ibcp_max_frags;
2605         peer_ni->ibp_queue_depth = reqmsg->ibm_u.connparams.ibcp_queue_depth;
2606
2607         write_lock_irqsave(g_lock, flags);
2608
2609         peer2 = kiblnd_find_peer_locked(ni, nid);
2610         if (peer2 != NULL) {
2611                 if (peer2->ibp_version == 0) {
2612                         peer2->ibp_version     = version;
2613                         peer2->ibp_incarnation = reqmsg->ibm_srcstamp;
2614                 }
2615
2616                 /* not the guy I've talked with */
2617                 if (peer2->ibp_incarnation != reqmsg->ibm_srcstamp ||
2618                     peer2->ibp_version     != version) {
2619                         kiblnd_close_peer_conns_locked(peer2, -ESTALE);
2620
2621                         if (kiblnd_peer_active(peer2)) {
2622                                 peer2->ibp_incarnation = reqmsg->ibm_srcstamp;
2623                                 peer2->ibp_version = version;
2624                         }
2625                         write_unlock_irqrestore(g_lock, flags);
2626
2627                         CWARN("Conn stale %s version %x/%x incarnation %llu/%llu\n",
2628                               libcfs_nid2str(nid), peer2->ibp_version, version,
2629                               peer2->ibp_incarnation, reqmsg->ibm_srcstamp);
2630
2631                         kiblnd_peer_decref(peer_ni);
2632                         rej.ibr_why = IBLND_REJECT_CONN_STALE;
2633                         goto failed;
2634                 }
2635
2636                 /* Tie-break connection race in favour of the higher NID.
2637                  * If we keep running into a race condition multiple times,
2638                  * we have to assume that the connection attempt with the
2639                  * higher NID is stuck in a connecting state and will never
2640                  * recover.  As such, we pass through this if-block and let
2641                  * the lower NID connection win so we can move forward.
2642                  */
2643                 if (peer2->ibp_connecting != 0 &&
2644                     nid < ni->ni_nid && peer2->ibp_races <
2645                     MAX_CONN_RACES_BEFORE_ABORT) {
2646                         peer2->ibp_races++;
2647                         write_unlock_irqrestore(g_lock, flags);
2648
2649                         CDEBUG(D_NET, "Conn race %s\n",
2650                                libcfs_nid2str(peer2->ibp_nid));
2651
2652                         kiblnd_peer_decref(peer_ni);
2653                         rej.ibr_why = IBLND_REJECT_CONN_RACE;
2654                         goto failed;
2655                 }
2656                 if (peer2->ibp_races >= MAX_CONN_RACES_BEFORE_ABORT)
2657                         CNETERR("Conn race %s: unresolved after %d attempts, letting lower NID win\n",
2658                                 libcfs_nid2str(peer2->ibp_nid),
2659                                 MAX_CONN_RACES_BEFORE_ABORT);
2660                 /*
2661                  * passive connection is allowed even this peer_ni is waiting for
2662                  * reconnection.
2663                  */
2664                 peer2->ibp_reconnecting = 0;
2665                 peer2->ibp_races = 0;
2666                 peer2->ibp_accepting++;
2667                 kiblnd_peer_addref(peer2);
2668
2669                 /* Race with kiblnd_launch_tx (active connect) to create peer_ni
2670                  * so copy validated parameters since we now know what the
2671                  * peer_ni's limits are */
2672                 peer2->ibp_max_frags = peer_ni->ibp_max_frags;
2673                 peer2->ibp_queue_depth = peer_ni->ibp_queue_depth;
2674
2675                 write_unlock_irqrestore(g_lock, flags);
2676                 kiblnd_peer_decref(peer_ni);
2677                 peer_ni = peer2;
2678         } else {
2679                 /* Brand new peer_ni */
2680                 LASSERT (peer_ni->ibp_accepting == 0);
2681                 LASSERT (peer_ni->ibp_version == 0 &&
2682                          peer_ni->ibp_incarnation == 0);
2683
2684                 peer_ni->ibp_accepting   = 1;
2685                 peer_ni->ibp_version     = version;
2686                 peer_ni->ibp_incarnation = reqmsg->ibm_srcstamp;
2687
2688                 /* I have a ref on ni that prevents it being shutdown */
2689                 LASSERT (net->ibn_shutdown == 0);
2690
2691                 kiblnd_peer_addref(peer_ni);
2692                 list_add_tail(&peer_ni->ibp_list, kiblnd_nid2peerlist(nid));
2693
2694                 write_unlock_irqrestore(g_lock, flags);
2695         }
2696
2697         conn = kiblnd_create_conn(peer_ni, cmid, IBLND_CONN_PASSIVE_WAIT, version);
2698         if (conn == NULL) {
2699                 kiblnd_peer_connect_failed(peer_ni, 0, -ENOMEM);
2700                 kiblnd_peer_decref(peer_ni);
2701                 rej.ibr_why = IBLND_REJECT_NO_RESOURCES;
2702                 goto failed;
2703         }
2704
2705         /* conn now "owns" cmid, so I return success from here on to ensure the
2706          * CM callback doesn't destroy cmid. */
2707         conn->ibc_incarnation      = reqmsg->ibm_srcstamp;
2708         conn->ibc_credits          = conn->ibc_queue_depth;
2709         conn->ibc_reserved_credits = conn->ibc_queue_depth;
2710         LASSERT(conn->ibc_credits + conn->ibc_reserved_credits +
2711                 IBLND_OOB_MSGS(version) <= IBLND_RX_MSGS(conn));
2712
2713         ackmsg = &conn->ibc_connvars->cv_msg;
2714         memset(ackmsg, 0, sizeof(*ackmsg));
2715
2716         kiblnd_init_msg(ackmsg, IBLND_MSG_CONNACK,
2717                         sizeof(ackmsg->ibm_u.connparams));
2718         ackmsg->ibm_u.connparams.ibcp_queue_depth  = conn->ibc_queue_depth;
2719         ackmsg->ibm_u.connparams.ibcp_max_frags    = conn->ibc_max_frags;
2720         ackmsg->ibm_u.connparams.ibcp_max_msg_size = IBLND_MSG_SIZE;
2721
2722         kiblnd_pack_msg(ni, ackmsg, version, 0, nid, reqmsg->ibm_srcstamp);
2723
2724         memset(&cp, 0, sizeof(cp));
2725         cp.private_data        = ackmsg;
2726         cp.private_data_len    = ackmsg->ibm_nob;
2727         cp.responder_resources = 0;             /* No atomic ops or RDMA reads */
2728         cp.initiator_depth     = 0;
2729         cp.flow_control        = 1;
2730         cp.retry_count         = *kiblnd_tunables.kib_retry_count;
2731         cp.rnr_retry_count     = *kiblnd_tunables.kib_rnr_retry_count;
2732
2733         CDEBUG(D_NET, "Accept %s\n", libcfs_nid2str(nid));
2734
2735         rc = rdma_accept(cmid, &cp);
2736         if (rc != 0) {
2737                 CERROR("Can't accept %s: %d\n", libcfs_nid2str(nid), rc);
2738                 rej.ibr_version = version;
2739                 rej.ibr_why     = IBLND_REJECT_FATAL;
2740
2741                 kiblnd_reject(cmid, &rej);
2742                 kiblnd_connreq_done(conn, rc);
2743                 kiblnd_conn_decref(conn);
2744         }
2745
2746         lnet_ni_decref(ni);
2747         return 0;
2748
2749  failed:
2750         if (ni != NULL) {
2751                 rej.ibr_cp.ibcp_queue_depth =
2752                         kiblnd_msg_queue_size(version, ni);
2753                 rej.ibr_cp.ibcp_max_frags   = IBLND_MAX_RDMA_FRAGS;
2754                 lnet_ni_decref(ni);
2755         }
2756
2757         rej.ibr_version = version;
2758         kiblnd_reject(cmid, &rej);
2759
2760         return -ECONNREFUSED;
2761 }
2762
2763 static void
2764 kiblnd_check_reconnect(struct kib_conn *conn, int version,
2765                        u64 incarnation, int why, struct kib_connparams *cp)
2766 {
2767         rwlock_t        *glock = &kiblnd_data.kib_global_lock;
2768         struct kib_peer_ni *peer_ni = conn->ibc_peer;
2769         char            *reason;
2770         int              msg_size = IBLND_MSG_SIZE;
2771         int              frag_num = -1;
2772         int              queue_dep = -1;
2773         bool             reconnect;
2774         unsigned long    flags;
2775
2776         LASSERT(conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2777         LASSERT(peer_ni->ibp_connecting > 0);   /* 'conn' at least */
2778
2779         if (cp) {
2780                 msg_size        = cp->ibcp_max_msg_size;
2781                 frag_num        = cp->ibcp_max_frags;
2782                 queue_dep       = cp->ibcp_queue_depth;
2783         }
2784
2785         write_lock_irqsave(glock, flags);
2786         /* retry connection if it's still needed and no other connection
2787          * attempts (active or passive) are in progress
2788          * NB: reconnect is still needed even when ibp_tx_queue is
2789          * empty if ibp_version != version because reconnect may be
2790          * initiated by kiblnd_query() */
2791         reconnect = (!list_empty(&peer_ni->ibp_tx_queue) ||
2792                      peer_ni->ibp_version != version) &&
2793                     peer_ni->ibp_connecting &&
2794                     peer_ni->ibp_accepting == 0;
2795         if (!reconnect) {
2796                 reason = "no need";
2797                 goto out;
2798         }
2799
2800         switch (why) {
2801         default:
2802                 reason = "Unknown";
2803                 break;
2804
2805         case IBLND_REJECT_RDMA_FRAGS: {
2806                 struct lnet_ioctl_config_o2iblnd_tunables *tunables;
2807
2808                 if (!cp) {
2809                         reason = "can't negotiate max frags";
2810                         goto out;
2811                 }
2812                 tunables = &peer_ni->ibp_ni->ni_lnd_tunables.lnd_tun_u.lnd_o2ib;
2813 #ifdef HAVE_IB_GET_DMA_MR
2814                 /*
2815                  * This check only makes sense if the kernel supports global
2816                  * memory registration. Otherwise, map_on_demand will never == 0
2817                  */
2818                 if (!tunables->lnd_map_on_demand) {
2819                         reason = "map_on_demand must be enabled";
2820                         goto out;
2821                 }
2822 #endif
2823                 if (conn->ibc_max_frags <= frag_num) {
2824                         reason = "unsupported max frags";
2825                         goto out;
2826                 }
2827
2828                 peer_ni->ibp_max_frags = frag_num;
2829                 reason = "rdma fragments";
2830                 break;
2831         }
2832         case IBLND_REJECT_MSG_QUEUE_SIZE:
2833                 if (!cp) {
2834                         reason = "can't negotiate queue depth";
2835                         goto out;
2836                 }
2837                 if (conn->ibc_queue_depth <= queue_dep) {
2838                         reason = "unsupported queue depth";
2839                         goto out;
2840                 }
2841
2842                 peer_ni->ibp_queue_depth = queue_dep;
2843                 reason = "queue depth";
2844                 break;
2845
2846         case IBLND_REJECT_CONN_STALE:
2847                 reason = "stale";
2848                 break;
2849
2850         case IBLND_REJECT_CONN_RACE:
2851                 reason = "conn race";
2852                 break;
2853
2854         case IBLND_REJECT_CONN_UNCOMPAT:
2855                 reason = "version negotiation";
2856                 break;
2857         }
2858
2859         conn->ibc_reconnect = 1;
2860         peer_ni->ibp_reconnecting++;
2861         peer_ni->ibp_version = version;
2862         if (incarnation != 0)
2863                 peer_ni->ibp_incarnation = incarnation;
2864  out:
2865         write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2866
2867         CNETERR("%s: %s (%s), %x, %x, msg_size: %d, queue_depth: %d/%d, max_frags: %d/%d\n",
2868                 libcfs_nid2str(peer_ni->ibp_nid),
2869                 reconnect ? "reconnect" : "don't reconnect",
2870                 reason, IBLND_MSG_VERSION, version, msg_size,
2871                 conn->ibc_queue_depth, queue_dep,
2872                 conn->ibc_max_frags, frag_num);
2873         /*
2874          * if conn::ibc_reconnect is TRUE, connd will reconnect to the peer_ni
2875          * while destroying the zombie
2876          */
2877 }
2878
2879 static void
2880 kiblnd_rejected(struct kib_conn *conn, int reason, void *priv, int priv_nob)
2881 {
2882         struct kib_peer_ni *peer_ni = conn->ibc_peer;
2883         int status = -ECONNREFUSED;
2884
2885         LASSERT (!in_interrupt());
2886         LASSERT (conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2887
2888         switch (reason) {
2889         case IB_CM_REJ_STALE_CONN:
2890                 kiblnd_check_reconnect(conn, IBLND_MSG_VERSION, 0,
2891                                        IBLND_REJECT_CONN_STALE, NULL);
2892                 break;
2893
2894         case IB_CM_REJ_INVALID_SERVICE_ID:
2895                 status = -EHOSTUNREACH;
2896                 CNETERR("%s rejected: no listener at %d\n",
2897                         libcfs_nid2str(peer_ni->ibp_nid),
2898                         *kiblnd_tunables.kib_service);
2899                 break;
2900
2901         case IB_CM_REJ_CONSUMER_DEFINED:
2902                 if (priv_nob >= offsetof(struct kib_rej, ibr_padding)) {
2903                         struct kib_rej *rej = priv;
2904                         struct kib_connparams *cp = NULL;
2905                         int               flip        = 0;
2906                         __u64             incarnation = -1;
2907
2908                         /* NB. default incarnation is -1 because:
2909                          * a) V1 will ignore dst incarnation in connreq.
2910                          * b) V2 will provide incarnation while rejecting me,
2911                          *    -1 will be overwrote.
2912                          *
2913                          * if I try to connect to a V1 peer_ni with V2 protocol,
2914                          * it rejected me then upgrade to V2, I have no idea
2915                          * about the upgrading and try to reconnect with V1,
2916                          * in this case upgraded V2 can find out I'm trying to
2917                          * talk to the old guy and reject me(incarnation is -1).
2918                          */
2919
2920                         if (rej->ibr_magic == __swab32(IBLND_MSG_MAGIC) ||
2921                             rej->ibr_magic == __swab32(LNET_PROTO_MAGIC)) {
2922                                 __swab32s(&rej->ibr_magic);
2923                                 __swab16s(&rej->ibr_version);
2924                                 flip = 1;
2925                         }
2926
2927                         if (priv_nob >= sizeof(struct kib_rej) &&
2928                             rej->ibr_version > IBLND_MSG_VERSION_1) {
2929                                 /* priv_nob is always 148 in current version
2930                                  * of OFED, so we still need to check version.
2931                                  * (define of IB_CM_REJ_PRIVATE_DATA_SIZE) */
2932                                 cp = &rej->ibr_cp;
2933
2934                                 if (flip) {
2935                                         __swab64s(&rej->ibr_incarnation);
2936                                         __swab16s(&cp->ibcp_queue_depth);
2937                                         __swab16s(&cp->ibcp_max_frags);
2938                                         __swab32s(&cp->ibcp_max_msg_size);
2939                                 }
2940
2941                                 incarnation = rej->ibr_incarnation;
2942                         }
2943
2944                         if (rej->ibr_magic != IBLND_MSG_MAGIC &&
2945                             rej->ibr_magic != LNET_PROTO_MAGIC) {
2946                                 CERROR("%s rejected: consumer defined fatal error\n",
2947                                        libcfs_nid2str(peer_ni->ibp_nid));
2948                                 break;
2949                         }
2950
2951                         if (rej->ibr_version != IBLND_MSG_VERSION &&
2952                             rej->ibr_version != IBLND_MSG_VERSION_1) {
2953                                 CERROR("%s rejected: o2iblnd version %x error\n",
2954                                        libcfs_nid2str(peer_ni->ibp_nid),
2955                                        rej->ibr_version);
2956                                 break;
2957                         }
2958
2959                         if (rej->ibr_why     == IBLND_REJECT_FATAL &&
2960                             rej->ibr_version == IBLND_MSG_VERSION_1) {
2961                                 CDEBUG(D_NET, "rejected by old version peer_ni %s: %x\n",
2962                                        libcfs_nid2str(peer_ni->ibp_nid), rej->ibr_version);
2963
2964                                 if (conn->ibc_version != IBLND_MSG_VERSION_1)
2965                                         rej->ibr_why = IBLND_REJECT_CONN_UNCOMPAT;
2966                         }
2967
2968                         switch (rej->ibr_why) {
2969                         case IBLND_REJECT_CONN_RACE:
2970                         case IBLND_REJECT_CONN_STALE:
2971                         case IBLND_REJECT_CONN_UNCOMPAT:
2972                         case IBLND_REJECT_MSG_QUEUE_SIZE:
2973                         case IBLND_REJECT_RDMA_FRAGS:
2974                                 kiblnd_check_reconnect(conn, rej->ibr_version,
2975                                                 incarnation, rej->ibr_why, cp);
2976                                 break;
2977
2978                         case IBLND_REJECT_NO_RESOURCES:
2979                                 CERROR("%s rejected: o2iblnd no resources\n",
2980                                        libcfs_nid2str(peer_ni->ibp_nid));
2981                                 break;
2982
2983                         case IBLND_REJECT_FATAL:
2984                                 CERROR("%s rejected: o2iblnd fatal error\n",
2985                                        libcfs_nid2str(peer_ni->ibp_nid));
2986                                 break;
2987
2988                         default:
2989                                 CERROR("%s rejected: o2iblnd reason %d\n",
2990                                        libcfs_nid2str(peer_ni->ibp_nid),
2991                                        rej->ibr_why);
2992                                 break;
2993                         }
2994                         break;
2995                 }
2996                 /* fall through */
2997         default:
2998                 CNETERR("%s rejected: reason %d, size %d\n",
2999                         libcfs_nid2str(peer_ni->ibp_nid), reason, priv_nob);
3000                 break;
3001         }
3002
3003         kiblnd_connreq_done(conn, status);
3004 }
3005
3006 static void
3007 kiblnd_check_connreply(struct kib_conn *conn, void *priv, int priv_nob)
3008 {
3009         struct kib_peer_ni *peer_ni = conn->ibc_peer;
3010         struct lnet_ni *ni = peer_ni->ibp_ni;
3011         struct kib_net *net = ni->ni_data;
3012         struct kib_msg *msg = priv;
3013         int            ver  = conn->ibc_version;
3014         int            rc   = kiblnd_unpack_msg(msg, priv_nob);
3015         unsigned long  flags;
3016
3017         LASSERT (net != NULL);
3018
3019         if (rc != 0) {
3020                 CERROR("Can't unpack connack from %s: %d\n",
3021                        libcfs_nid2str(peer_ni->ibp_nid), rc);
3022                 goto failed;
3023         }
3024
3025         if (msg->ibm_type != IBLND_MSG_CONNACK) {
3026                 CERROR("Unexpected message %d from %s\n",
3027                        msg->ibm_type, libcfs_nid2str(peer_ni->ibp_nid));
3028                 rc = -EPROTO;
3029                 goto failed;
3030         }
3031
3032         if (ver != msg->ibm_version) {
3033                 CERROR("%s replied version %x is different with "
3034                        "requested version %x\n",
3035                        libcfs_nid2str(peer_ni->ibp_nid), msg->ibm_version, ver);
3036                 rc = -EPROTO;
3037                 goto failed;
3038         }
3039
3040         if (msg->ibm_u.connparams.ibcp_queue_depth >
3041             conn->ibc_queue_depth) {
3042                 CERROR("%s has incompatible queue depth %d (<=%d wanted)\n",
3043                        libcfs_nid2str(peer_ni->ibp_nid),
3044                        msg->ibm_u.connparams.ibcp_queue_depth,
3045                        conn->ibc_queue_depth);
3046                 rc = -EPROTO;
3047                 goto failed;
3048         }
3049
3050         if (msg->ibm_u.connparams.ibcp_max_frags >
3051             conn->ibc_max_frags) {
3052                 CERROR("%s has incompatible max_frags %d (<=%d wanted)\n",
3053                        libcfs_nid2str(peer_ni->ibp_nid),
3054                        msg->ibm_u.connparams.ibcp_max_frags,
3055                        conn->ibc_max_frags);
3056                 rc = -EPROTO;
3057                 goto failed;
3058         }
3059
3060         if (msg->ibm_u.connparams.ibcp_max_msg_size > IBLND_MSG_SIZE) {
3061                 CERROR("%s max message size %d too big (%d max)\n",
3062                        libcfs_nid2str(peer_ni->ibp_nid),
3063                        msg->ibm_u.connparams.ibcp_max_msg_size,
3064                        IBLND_MSG_SIZE);
3065                 rc = -EPROTO;
3066                 goto failed;
3067         }
3068
3069         read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
3070         if (msg->ibm_dstnid == ni->ni_nid &&
3071             msg->ibm_dststamp == net->ibn_incarnation)
3072                 rc = 0;
3073         else
3074                 rc = -ESTALE;
3075         read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
3076
3077         if (rc != 0) {
3078                 CERROR("Bad connection reply from %s, rc = %d, "
3079                        "version: %x max_frags: %d\n",
3080                        libcfs_nid2str(peer_ni->ibp_nid), rc,
3081                        msg->ibm_version, msg->ibm_u.connparams.ibcp_max_frags);
3082                 goto failed;
3083         }
3084
3085         conn->ibc_incarnation      = msg->ibm_srcstamp;
3086         conn->ibc_credits          = msg->ibm_u.connparams.ibcp_queue_depth;
3087         conn->ibc_reserved_credits = msg->ibm_u.connparams.ibcp_queue_depth;
3088         conn->ibc_queue_depth      = msg->ibm_u.connparams.ibcp_queue_depth;
3089         conn->ibc_max_frags        = msg->ibm_u.connparams.ibcp_max_frags;
3090         LASSERT(conn->ibc_credits + conn->ibc_reserved_credits +
3091                 IBLND_OOB_MSGS(ver) <= IBLND_RX_MSGS(conn));
3092
3093         kiblnd_connreq_done(conn, 0);
3094         return;
3095
3096  failed:
3097         /* NB My QP has already established itself, so I handle anything going
3098          * wrong here by setting ibc_comms_error.
3099          * kiblnd_connreq_done(0) moves the conn state to ESTABLISHED, but then
3100          * immediately tears it down. */
3101
3102         LASSERT (rc != 0);
3103         conn->ibc_comms_error = rc;
3104         kiblnd_connreq_done(conn, 0);
3105 }
3106
3107 static int
3108 kiblnd_active_connect(struct rdma_cm_id *cmid)
3109 {
3110         struct kib_peer_ni *peer_ni = cmid->context;
3111         struct kib_conn *conn;
3112         struct kib_msg *msg;
3113         struct rdma_conn_param cp;
3114         int                      version;
3115         __u64                    incarnation;
3116         unsigned long            flags;
3117         int                      rc;
3118
3119         read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
3120
3121         incarnation = peer_ni->ibp_incarnation;
3122         version     = (peer_ni->ibp_version == 0) ? IBLND_MSG_VERSION :
3123                                                  peer_ni->ibp_version;
3124
3125         read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
3126
3127         conn = kiblnd_create_conn(peer_ni, cmid, IBLND_CONN_ACTIVE_CONNECT,
3128                                   version);
3129         if (conn == NULL) {
3130                 kiblnd_peer_connect_failed(peer_ni, 1, -ENOMEM);
3131                 kiblnd_peer_decref(peer_ni); /* lose cmid's ref */
3132                 return -ENOMEM;
3133         }
3134
3135         /* conn "owns" cmid now, so I return success from here on to ensure the
3136          * CM callback doesn't destroy cmid. conn also takes over cmid's ref
3137          * on peer_ni */
3138
3139         msg = &conn->ibc_connvars->cv_msg;
3140
3141         memset(msg, 0, sizeof(*msg));
3142         kiblnd_init_msg(msg, IBLND_MSG_CONNREQ, sizeof(msg->ibm_u.connparams));
3143         msg->ibm_u.connparams.ibcp_queue_depth  = conn->ibc_queue_depth;
3144         msg->ibm_u.connparams.ibcp_max_frags    = conn->ibc_max_frags;
3145         msg->ibm_u.connparams.ibcp_max_msg_size = IBLND_MSG_SIZE;
3146
3147         kiblnd_pack_msg(peer_ni->ibp_ni, msg, version,
3148                         0, peer_ni->ibp_nid, incarnation);
3149
3150         memset(&cp, 0, sizeof(cp));
3151         cp.private_data        = msg;
3152         cp.private_data_len    = msg->ibm_nob;
3153         cp.responder_resources = 0;             /* No atomic ops or RDMA reads */
3154         cp.initiator_depth     = 0;
3155         cp.flow_control        = 1;
3156         cp.retry_count         = *kiblnd_tunables.kib_retry_count;
3157         cp.rnr_retry_count     = *kiblnd_tunables.kib_rnr_retry_count;
3158
3159         LASSERT(cmid->context == (void *)conn);
3160         LASSERT(conn->ibc_cmid == cmid);
3161         rc = rdma_connect_locked(cmid, &cp);
3162         if (rc != 0) {
3163                 CERROR("Can't connect to %s: %d\n",
3164                        libcfs_nid2str(peer_ni->ibp_nid), rc);
3165                 kiblnd_connreq_done(conn, rc);
3166                 kiblnd_conn_decref(conn);
3167         }
3168
3169         return 0;
3170 }
3171
3172 int
3173 kiblnd_cm_callback(struct rdma_cm_id *cmid, struct rdma_cm_event *event)
3174 {
3175         struct kib_peer_ni *peer_ni;
3176         struct kib_conn *conn;
3177         int rc;
3178
3179         switch (event->event) {
3180         default:
3181                 CERROR("Unexpected event: %d, status: %d\n",
3182                        event->event, event->status);
3183                 LBUG();
3184
3185         case RDMA_CM_EVENT_CONNECT_REQUEST:
3186                 /* destroy cmid on failure */
3187                 rc = kiblnd_passive_connect(cmid,
3188                                             (void *)KIBLND_CONN_PARAM(event),
3189                                             KIBLND_CONN_PARAM_LEN(event));
3190                 CDEBUG(D_NET, "connreq: %d\n", rc);
3191                 return rc;
3192
3193         case RDMA_CM_EVENT_ADDR_ERROR:
3194                 peer_ni = cmid->context;
3195                 CNETERR("%s: ADDR ERROR %d\n",
3196                        libcfs_nid2str(peer_ni->ibp_nid), event->status);
3197                 kiblnd_peer_connect_failed(peer_ni, 1, -EHOSTUNREACH);
3198                 kiblnd_peer_decref(peer_ni);
3199                 return -EHOSTUNREACH;      /* rc != 0 destroys cmid */
3200
3201         case RDMA_CM_EVENT_ADDR_RESOLVED:
3202                 peer_ni = cmid->context;
3203
3204                 CDEBUG(D_NET,"%s Addr resolved: %d\n",
3205                        libcfs_nid2str(peer_ni->ibp_nid), event->status);
3206
3207                 if (event->status != 0) {
3208                         CNETERR("Can't resolve address for %s: %d\n",
3209                                 libcfs_nid2str(peer_ni->ibp_nid), event->status);
3210                         rc = event->status;
3211                 } else {
3212                         rc = rdma_resolve_route(
3213                                 cmid, lnet_get_lnd_timeout() * 1000);
3214                         if (rc == 0) {
3215                                 struct kib_net *net = peer_ni->ibp_ni->ni_data;
3216                                 struct kib_dev *dev = net->ibn_dev;
3217
3218                                 CDEBUG(D_NET, "%s: connection bound to "\
3219                                        "%s:%pI4h:%s\n",
3220                                        libcfs_nid2str(peer_ni->ibp_nid),
3221                                        dev->ibd_ifname,
3222                                        &dev->ibd_ifip, cmid->device->name);
3223
3224                                 return 0;
3225                         }
3226
3227                         /* Can't initiate route resolution */
3228                         CERROR("Can't resolve route for %s: %d\n",
3229                                libcfs_nid2str(peer_ni->ibp_nid), rc);
3230                 }
3231                 kiblnd_peer_connect_failed(peer_ni, 1, rc);
3232                 kiblnd_peer_decref(peer_ni);
3233                 return rc;                      /* rc != 0 destroys cmid */
3234
3235         case RDMA_CM_EVENT_ROUTE_ERROR:
3236                 peer_ni = cmid->context;
3237                 CNETERR("%s: ROUTE ERROR %d\n",
3238                         libcfs_nid2str(peer_ni->ibp_nid), event->status);
3239                 kiblnd_peer_connect_failed(peer_ni, 1, -EHOSTUNREACH);
3240                 kiblnd_peer_decref(peer_ni);
3241                 return -EHOSTUNREACH;           /* rc != 0 destroys cmid */
3242
3243         case RDMA_CM_EVENT_ROUTE_RESOLVED:
3244                 peer_ni = cmid->context;
3245                 CDEBUG(D_NET,"%s Route resolved: %d\n",
3246                        libcfs_nid2str(peer_ni->ibp_nid), event->status);
3247
3248                 if (event->status == 0)
3249                         return kiblnd_active_connect(cmid);
3250
3251                 CNETERR("Can't resolve route for %s: %d\n",
3252                        libcfs_nid2str(peer_ni->ibp_nid), event->status);
3253                 kiblnd_peer_connect_failed(peer_ni, 1, event->status);
3254                 kiblnd_peer_decref(peer_ni);
3255                 return event->status;           /* rc != 0 destroys cmid */
3256
3257         case RDMA_CM_EVENT_UNREACHABLE:
3258                 conn = cmid->context;
3259                 LASSERT(conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT ||
3260                         conn->ibc_state == IBLND_CONN_PASSIVE_WAIT);
3261                 CNETERR("%s: UNREACHABLE %d\n",
3262                        libcfs_nid2str(conn->ibc_peer->ibp_nid), event->status);
3263                 kiblnd_connreq_done(conn, -ENETDOWN);
3264                 kiblnd_conn_decref(conn);
3265                 return 0;
3266
3267         case RDMA_CM_EVENT_CONNECT_ERROR:
3268                 conn = cmid->context;
3269                 LASSERT(conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT ||
3270                         conn->ibc_state == IBLND_CONN_PASSIVE_WAIT);
3271                 CNETERR("%s: CONNECT ERROR %d\n",
3272                         libcfs_nid2str(conn->ibc_peer->ibp_nid), event->status);
3273                 kiblnd_connreq_done(conn, -ENOTCONN);
3274                 kiblnd_conn_decref(conn);
3275                 return 0;
3276
3277         case RDMA_CM_EVENT_REJECTED:
3278                 conn = cmid->context;
3279                 switch (conn->ibc_state) {
3280                 default:
3281                         LBUG();
3282
3283                 case IBLND_CONN_PASSIVE_WAIT:
3284                         CERROR ("%s: REJECTED %d\n",
3285                                 libcfs_nid2str(conn->ibc_peer->ibp_nid),
3286                                 event->status);
3287                         kiblnd_connreq_done(conn, -ECONNRESET);
3288                         break;
3289
3290                 case IBLND_CONN_ACTIVE_CONNECT:
3291                         kiblnd_rejected(conn, event->status,
3292                                         (void *)KIBLND_CONN_PARAM(event),
3293                                         KIBLND_CONN_PARAM_LEN(event));
3294                         break;
3295                 }
3296                 kiblnd_conn_decref(conn);
3297                 return 0;
3298
3299         case RDMA_CM_EVENT_ESTABLISHED:
3300                 conn = cmid->context;
3301                 switch (conn->ibc_state) {
3302                 default:
3303                         LBUG();
3304
3305                 case IBLND_CONN_PASSIVE_WAIT:
3306                         CDEBUG(D_NET, "ESTABLISHED (passive): %s\n",
3307                                libcfs_nid2str(conn->ibc_peer->ibp_nid));
3308                         kiblnd_connreq_done(conn, 0);
3309                         break;
3310
3311                 case IBLND_CONN_ACTIVE_CONNECT:
3312                         CDEBUG(D_NET, "ESTABLISHED(active): %s\n",
3313                                libcfs_nid2str(conn->ibc_peer->ibp_nid));
3314                         kiblnd_check_connreply(conn,
3315                                                (void *)KIBLND_CONN_PARAM(event),
3316                                                KIBLND_CONN_PARAM_LEN(event));
3317                         break;
3318                 }
3319                 /* net keeps its ref on conn! */
3320                 return 0;
3321
3322         case RDMA_CM_EVENT_TIMEWAIT_EXIT:
3323                 CDEBUG(D_NET, "Ignore TIMEWAIT_EXIT event\n");
3324                 return 0;
3325
3326         case RDMA_CM_EVENT_DISCONNECTED:
3327                 conn = cmid->context;
3328                 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
3329                         CERROR("%s DISCONNECTED\n",
3330                                libcfs_nid2str(conn->ibc_peer->ibp_nid));
3331                         kiblnd_connreq_done(conn, -ECONNRESET);
3332                 } else {
3333                         kiblnd_close_conn(conn, 0);
3334                 }
3335                 kiblnd_conn_decref(conn);
3336                 cmid->context = NULL;
3337                 return 0;
3338
3339         case RDMA_CM_EVENT_DEVICE_REMOVAL:
3340                 LCONSOLE_ERROR_MSG(0x131,
3341                                    "Received notification of device removal\n"
3342                                    "Please shutdown LNET to allow this to proceed\n");
3343                 /* Can't remove network from underneath LNET for now, so I have
3344                  * to ignore this */
3345                 return 0;
3346
3347         case RDMA_CM_EVENT_ADDR_CHANGE:
3348                 LCONSOLE_INFO("Physical link changed (eg hca/port)\n");
3349                 return 0;
3350         }
3351 }
3352
3353 static int
3354 kiblnd_check_txs_locked(struct kib_conn *conn, struct list_head *txs)
3355 {
3356         struct kib_tx *tx;
3357         struct list_head *ttmp;
3358
3359         list_for_each(ttmp, txs) {
3360                 tx = list_entry(ttmp, struct kib_tx, tx_list);
3361
3362                 if (txs != &conn->ibc_active_txs) {
3363                         LASSERT(tx->tx_queued);
3364                 } else {
3365                         LASSERT(!tx->tx_queued);
3366                         LASSERT(tx->tx_waiting || tx->tx_sending != 0);
3367                 }
3368
3369                 if (ktime_compare(ktime_get(), tx->tx_deadline) >= 0) {
3370                         CERROR("Timed out tx: %s, %lld seconds\n",
3371                                kiblnd_queue2str(conn, txs),
3372                                ktime_ms_delta(ktime_get(),
3373                                               tx->tx_deadline) / MSEC_PER_SEC);
3374                         return 1;
3375                 }
3376         }
3377
3378         return 0;
3379 }
3380
3381 static int
3382 kiblnd_conn_timed_out_locked(struct kib_conn *conn)
3383 {
3384         return  kiblnd_check_txs_locked(conn, &conn->ibc_tx_queue) ||
3385                 kiblnd_check_txs_locked(conn, &conn->ibc_tx_noops) ||
3386                 kiblnd_check_txs_locked(conn, &conn->ibc_tx_queue_rsrvd) ||
3387                 kiblnd_check_txs_locked(conn, &conn->ibc_tx_queue_nocred) ||
3388                 kiblnd_check_txs_locked(conn, &conn->ibc_active_txs);
3389 }
3390
3391 static void
3392 kiblnd_check_conns (int idx)
3393 {
3394         struct list_head  closes = LIST_HEAD_INIT(closes);
3395         struct list_head  checksends = LIST_HEAD_INIT(checksends);
3396         struct list_head  timedout_txs = LIST_HEAD_INIT(timedout_txs);
3397         struct list_head *peers = &kiblnd_data.kib_peers[idx];
3398         struct list_head *ptmp;
3399         struct kib_peer_ni *peer_ni;
3400         struct kib_conn *conn;
3401         struct kib_tx *tx, *tx_tmp;
3402         struct list_head *ctmp;
3403         unsigned long     flags;
3404
3405         /* NB. We expect to have a look at all the peers and not find any
3406          * RDMAs to time out, so we just use a shared lock while we
3407          * take a look... */
3408         write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
3409
3410         list_for_each(ptmp, peers) {
3411                 peer_ni = list_entry(ptmp, struct kib_peer_ni, ibp_list);
3412
3413                 /* Check tx_deadline */
3414                 list_for_each_entry_safe(tx, tx_tmp, &peer_ni->ibp_tx_queue, tx_list) {
3415                         if (ktime_compare(ktime_get(), tx->tx_deadline) >= 0) {
3416                                 CWARN("Timed out tx for %s: %lld seconds\n",
3417                                       libcfs_nid2str(peer_ni->ibp_nid),
3418                                       ktime_ms_delta(ktime_get(),
3419                                                      tx->tx_deadline) / MSEC_PER_SEC);
3420                                 list_move(&tx->tx_list, &timedout_txs);
3421                         }
3422                 }
3423
3424                 list_for_each(ctmp, &peer_ni->ibp_conns) {
3425                         int timedout;
3426                         int sendnoop;
3427
3428                         conn = list_entry(ctmp, struct kib_conn, ibc_list);
3429
3430                         LASSERT(conn->ibc_state == IBLND_CONN_ESTABLISHED);
3431
3432                         spin_lock(&conn->ibc_lock);
3433
3434                         sendnoop = kiblnd_need_noop(conn);
3435                         timedout = kiblnd_conn_timed_out_locked(conn);
3436                         if (!sendnoop && !timedout) {
3437                                 spin_unlock(&conn->ibc_lock);
3438                                 continue;
3439                         }
3440
3441                         if (timedout) {
3442                                 CERROR("Timed out RDMA with %s (%lld): "
3443                                        "c: %u, oc: %u, rc: %u\n",
3444                                        libcfs_nid2str(peer_ni->ibp_nid),
3445                                        ktime_get_seconds() - peer_ni->ibp_last_alive,
3446                                        conn->ibc_credits,
3447                                        conn->ibc_outstanding_credits,
3448                                        conn->ibc_reserved_credits);
3449                                 list_add(&conn->ibc_connd_list, &closes);
3450                         } else {
3451                                 list_add(&conn->ibc_connd_list, &checksends);
3452                         }
3453                         /* +ref for 'closes' or 'checksends' */
3454                         kiblnd_conn_addref(conn);
3455
3456                         spin_unlock(&conn->ibc_lock);
3457                 }
3458         }
3459
3460         write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
3461
3462         if (!list_empty(&timedout_txs))
3463                 kiblnd_txlist_done(&timedout_txs, -ETIMEDOUT,
3464                                    LNET_MSG_STATUS_LOCAL_TIMEOUT);
3465
3466         /* Handle timeout by closing the whole
3467          * connection. We can only be sure RDMA activity
3468          * has ceased once the QP has been modified. */
3469         while (!list_empty(&closes)) {
3470                 conn = list_entry(closes.next,
3471                                   struct kib_conn, ibc_connd_list);
3472                 list_del(&conn->ibc_connd_list);
3473                 kiblnd_close_conn(conn, -ETIMEDOUT);
3474                 kiblnd_conn_decref(conn);
3475         }
3476
3477         /* In case we have enough credits to return via a
3478          * NOOP, but there were no non-blocking tx descs
3479          * free to do it last time... */
3480         while (!list_empty(&checksends)) {
3481                 conn = list_entry(checksends.next,
3482                                   struct kib_conn, ibc_connd_list);
3483                 list_del(&conn->ibc_connd_list);
3484
3485                 spin_lock(&conn->ibc_lock);
3486                 kiblnd_check_sends_locked(conn);
3487                 spin_unlock(&conn->ibc_lock);
3488
3489                 kiblnd_conn_decref(conn);
3490         }
3491 }
3492
3493 static void
3494 kiblnd_disconnect_conn(struct kib_conn *conn)
3495 {
3496         LASSERT (!in_interrupt());
3497         LASSERT (current == kiblnd_data.kib_connd);
3498         LASSERT (conn->ibc_state == IBLND_CONN_CLOSING);
3499
3500         rdma_disconnect(conn->ibc_cmid);
3501         kiblnd_finalise_conn(conn);
3502
3503         kiblnd_peer_notify(conn->ibc_peer);
3504 }
3505
3506 /*
3507  * High-water for reconnection to the same peer_ni, reconnection attempt should
3508  * be delayed after trying more than KIB_RECONN_HIGH_RACE.
3509  */
3510 #define KIB_RECONN_HIGH_RACE    10
3511 /*
3512  * Allow connd to take a break and handle other things after consecutive
3513  * reconnection attemps.
3514  */
3515 #define KIB_RECONN_BREAK        100
3516
3517 int
3518 kiblnd_connd (void *arg)
3519 {
3520         spinlock_t        *lock= &kiblnd_data.kib_connd_lock;
3521         wait_queue_entry_t wait;
3522         unsigned long      flags;
3523         struct kib_conn *conn;
3524         int                timeout;
3525         int                i;
3526         int                dropped_lock;
3527         int                peer_index = 0;
3528         unsigned long      deadline = jiffies;
3529
3530         cfs_block_allsigs();
3531
3532         init_waitqueue_entry(&wait, current);
3533         kiblnd_data.kib_connd = current;
3534
3535         spin_lock_irqsave(lock, flags);
3536
3537         while (!kiblnd_data.kib_shutdown) {
3538                 int reconn = 0;
3539
3540                 dropped_lock = 0;
3541
3542                 if (!list_empty(&kiblnd_data.kib_connd_zombies)) {
3543                         struct kib_peer_ni *peer_ni = NULL;
3544
3545                         conn = list_entry(kiblnd_data.kib_connd_zombies.next,
3546                                           struct kib_conn, ibc_list);
3547                         list_del(&conn->ibc_list);
3548                         if (conn->ibc_reconnect) {
3549                                 peer_ni = conn->ibc_peer;
3550                                 kiblnd_peer_addref(peer_ni);
3551                         }
3552
3553                         spin_unlock_irqrestore(lock, flags);
3554                         dropped_lock = 1;
3555
3556                         kiblnd_destroy_conn(conn);
3557
3558                         spin_lock_irqsave(lock, flags);
3559                         if (!peer_ni) {
3560                                 LIBCFS_FREE(conn, sizeof(*conn));
3561                                 continue;
3562                         }
3563
3564                         conn->ibc_peer = peer_ni;
3565                         if (peer_ni->ibp_reconnected < KIB_RECONN_HIGH_RACE)
3566                                 list_add_tail(&conn->ibc_list,
3567                                               &kiblnd_data.kib_reconn_list);
3568                         else
3569                                 list_add_tail(&conn->ibc_list,
3570                                               &kiblnd_data.kib_reconn_wait);
3571                 }
3572
3573                 if (!list_empty(&kiblnd_data.kib_connd_conns)) {
3574                         conn = list_entry(kiblnd_data.kib_connd_conns.next,
3575                                           struct kib_conn, ibc_list);
3576                         list_del(&conn->ibc_list);
3577
3578                         spin_unlock_irqrestore(lock, flags);
3579                         dropped_lock = 1;
3580
3581                         kiblnd_disconnect_conn(conn);
3582                         kiblnd_conn_decref(conn);
3583
3584                         spin_lock_irqsave(lock, flags);
3585                 }
3586
3587                 while (reconn < KIB_RECONN_BREAK) {
3588                         if (kiblnd_data.kib_reconn_sec !=
3589                             ktime_get_real_seconds()) {
3590                                 kiblnd_data.kib_reconn_sec = ktime_get_real_seconds();
3591                                 list_splice_init(&kiblnd_data.kib_reconn_wait,
3592                                                  &kiblnd_data.kib_reconn_list);
3593                         }
3594
3595                         if (list_empty(&kiblnd_data.kib_reconn_list))
3596                                 break;
3597
3598                         conn = list_entry(kiblnd_data.kib_reconn_list.next,
3599                                           struct kib_conn, ibc_list);
3600                         list_del(&conn->ibc_list);
3601
3602                         spin_unlock_irqrestore(lock, flags);
3603                         dropped_lock = 1;
3604
3605                         reconn += kiblnd_reconnect_peer(conn->ibc_peer);
3606                         kiblnd_peer_decref(conn->ibc_peer);
3607                         LIBCFS_FREE(conn, sizeof(*conn));
3608
3609                         spin_lock_irqsave(lock, flags);
3610                 }
3611
3612                 /* careful with the jiffy wrap... */
3613                 timeout = (int)(deadline - jiffies);
3614                 if (timeout <= 0) {
3615                         const int n = 4;
3616                         const int p = 1;
3617                         int       chunk = kiblnd_data.kib_peer_hash_size;
3618                         unsigned int lnd_timeout;
3619
3620                         spin_unlock_irqrestore(lock, flags);
3621                         dropped_lock = 1;
3622
3623                         /* Time to check for RDMA timeouts on a few more
3624                          * peers: I do checks every 'p' seconds on a
3625                          * proportion of the peer_ni table and I need to check
3626                          * every connection 'n' times within a timeout
3627                          * interval, to ensure I detect a timeout on any
3628                          * connection within (n+1)/n times the timeout
3629                          * interval. */
3630
3631                         lnd_timeout = lnet_get_lnd_timeout();
3632                         if (lnd_timeout > n * p)
3633                                 chunk = (chunk * n * p) / lnd_timeout;
3634                         if (chunk == 0)
3635                                 chunk = 1;
3636
3637                         for (i = 0; i < chunk; i++) {
3638                                 kiblnd_check_conns(peer_index);
3639                                 peer_index = (peer_index + 1) %
3640                                              kiblnd_data.kib_peer_hash_size;
3641                         }
3642
3643                         deadline += msecs_to_jiffies(p * MSEC_PER_SEC);
3644                         spin_lock_irqsave(lock, flags);
3645                 }
3646
3647                 if (dropped_lock)
3648                         continue;
3649
3650                 /* Nothing to do for 'timeout'  */
3651                 set_current_state(TASK_INTERRUPTIBLE);
3652                 add_wait_queue(&kiblnd_data.kib_connd_waitq, &wait);
3653                 spin_unlock_irqrestore(lock, flags);
3654
3655                 schedule_timeout(timeout);
3656
3657                 set_current_state(TASK_RUNNING);
3658                 remove_wait_queue(&kiblnd_data.kib_connd_waitq, &wait);
3659                 spin_lock_irqsave(lock, flags);
3660         }
3661
3662         spin_unlock_irqrestore(lock, flags);
3663
3664         kiblnd_thread_fini();
3665         return 0;
3666 }
3667
3668 void
3669 kiblnd_qp_event(struct ib_event *event, void *arg)
3670 {
3671         struct kib_conn *conn = arg;
3672
3673         switch (event->event) {
3674         case IB_EVENT_COMM_EST:
3675                 CDEBUG(D_NET, "%s established\n",
3676                        libcfs_nid2str(conn->ibc_peer->ibp_nid));
3677                 /* We received a packet but connection isn't established
3678                  * probably handshake packet was lost, so free to
3679                  * force make connection established */
3680                 rdma_notify(conn->ibc_cmid, IB_EVENT_COMM_EST);
3681                 return;
3682
3683         case IB_EVENT_PORT_ERR:
3684         case IB_EVENT_DEVICE_FATAL:
3685                 CERROR("Fatal device error for NI %s\n",
3686                        libcfs_nid2str(conn->ibc_peer->ibp_ni->ni_nid));
3687                 atomic_set(&conn->ibc_peer->ibp_ni->ni_fatal_error_on, 1);
3688                 return;
3689
3690         case IB_EVENT_PORT_ACTIVE:
3691                 CERROR("Port reactivated for NI %s\n",
3692                        libcfs_nid2str(conn->ibc_peer->ibp_ni->ni_nid));
3693                 atomic_set(&conn->ibc_peer->ibp_ni->ni_fatal_error_on, 0);
3694                 return;
3695
3696         default:
3697                 CERROR("%s: Async QP event type %d\n",
3698                        libcfs_nid2str(conn->ibc_peer->ibp_nid), event->event);
3699                 return;
3700         }
3701 }
3702
3703 static void
3704 kiblnd_complete (struct ib_wc *wc)
3705 {
3706         switch (kiblnd_wreqid2type(wc->wr_id)) {
3707         default:
3708                 LBUG();
3709
3710         case IBLND_WID_MR:
3711                 if (wc->status != IB_WC_SUCCESS &&
3712                     wc->status != IB_WC_WR_FLUSH_ERR)
3713                         CNETERR("FastReg failed: %d\n", wc->status);
3714                 return;
3715
3716         case IBLND_WID_RDMA:
3717                 /* We only get RDMA completion notification if it fails.  All
3718                  * subsequent work items, including the final SEND will fail
3719                  * too.  However we can't print out any more info about the
3720                  * failing RDMA because 'tx' might be back on the idle list or
3721                  * even reused already if we didn't manage to post all our work
3722                  * items */
3723                 CNETERR("RDMA (tx: %p) failed: %d\n",
3724                         kiblnd_wreqid2ptr(wc->wr_id), wc->status);
3725                 return;
3726
3727         case IBLND_WID_TX:
3728                 kiblnd_tx_complete(kiblnd_wreqid2ptr(wc->wr_id), wc->status);
3729                 return;
3730
3731         case IBLND_WID_RX:
3732                 kiblnd_rx_complete(kiblnd_wreqid2ptr(wc->wr_id), wc->status,
3733                                    wc->byte_len);
3734                 return;
3735         }
3736 }
3737
3738 void
3739 kiblnd_cq_completion(struct ib_cq *cq, void *arg)
3740 {
3741         /* NB I'm not allowed to schedule this conn once its refcount has
3742          * reached 0.  Since fundamentally I'm racing with scheduler threads
3743          * consuming my CQ I could be called after all completions have
3744          * occurred.  But in this case, ibc_nrx == 0 && ibc_nsends_posted == 0
3745          * and this CQ is about to be destroyed so I NOOP. */
3746         struct kib_conn *conn = arg;
3747         struct kib_sched_info *sched = conn->ibc_sched;
3748         unsigned long flags;
3749
3750         LASSERT(cq == conn->ibc_cq);
3751
3752         spin_lock_irqsave(&sched->ibs_lock, flags);
3753
3754         conn->ibc_ready = 1;
3755
3756         if (!conn->ibc_scheduled &&
3757             (conn->ibc_nrx > 0 ||
3758              conn->ibc_nsends_posted > 0)) {
3759                 kiblnd_conn_addref(conn); /* +1 ref for sched_conns */
3760                 conn->ibc_scheduled = 1;
3761                 list_add_tail(&conn->ibc_sched_list, &sched->ibs_conns);
3762
3763                 if (waitqueue_active(&sched->ibs_waitq))
3764                         wake_up(&sched->ibs_waitq);
3765         }
3766
3767         spin_unlock_irqrestore(&sched->ibs_lock, flags);
3768 }
3769
3770 void
3771 kiblnd_cq_event(struct ib_event *event, void *arg)
3772 {
3773         struct kib_conn *conn = arg;
3774
3775         CERROR("%s: async CQ event type %d\n",
3776                libcfs_nid2str(conn->ibc_peer->ibp_nid), event->event);
3777 }
3778
3779 int
3780 kiblnd_scheduler(void *arg)
3781 {
3782         long                    id = (long)arg;
3783         struct kib_sched_info   *sched;
3784         struct kib_conn *conn;
3785         wait_queue_entry_t      wait;
3786         unsigned long           flags;
3787         struct ib_wc            wc;
3788         int                     did_something;
3789         int                     busy_loops = 0;
3790         int                     rc;
3791
3792         cfs_block_allsigs();
3793
3794         init_waitqueue_entry(&wait, current);
3795
3796         sched = kiblnd_data.kib_scheds[KIB_THREAD_CPT(id)];
3797
3798         rc = cfs_cpt_bind(lnet_cpt_table(), sched->ibs_cpt);
3799         if (rc != 0) {
3800                 CWARN("Unable to bind on CPU partition %d, please verify "
3801                       "whether all CPUs are healthy and reload modules if "
3802                       "necessary, otherwise your system might under risk of "
3803                       "low performance\n", sched->ibs_cpt);
3804         }
3805
3806         spin_lock_irqsave(&sched->ibs_lock, flags);
3807
3808         while (!kiblnd_data.kib_shutdown) {
3809                 if (busy_loops++ >= IBLND_RESCHED) {
3810                         spin_unlock_irqrestore(&sched->ibs_lock, flags);
3811
3812                         cond_resched();
3813                         busy_loops = 0;
3814
3815                         spin_lock_irqsave(&sched->ibs_lock, flags);
3816                 }
3817
3818                 did_something = 0;
3819
3820                 if (!list_empty(&sched->ibs_conns)) {
3821                         conn = list_entry(sched->ibs_conns.next,
3822                                           struct kib_conn, ibc_sched_list);
3823                         /* take over kib_sched_conns' ref on conn... */
3824                         LASSERT(conn->ibc_scheduled);
3825                         list_del(&conn->ibc_sched_list);
3826                         conn->ibc_ready = 0;
3827
3828                         spin_unlock_irqrestore(&sched->ibs_lock, flags);
3829
3830                         wc.wr_id = IBLND_WID_INVAL;
3831
3832                         rc = ib_poll_cq(conn->ibc_cq, 1, &wc);
3833                         if (rc == 0) {
3834                                 rc = ib_req_notify_cq(conn->ibc_cq,
3835                                                       IB_CQ_NEXT_COMP);
3836                                 if (rc < 0) {
3837                                         CWARN("%s: ib_req_notify_cq failed: %d, "
3838                                               "closing connection\n",
3839                                               libcfs_nid2str(conn->ibc_peer->ibp_nid), rc);
3840                                         kiblnd_close_conn(conn, -EIO);
3841                                         kiblnd_conn_decref(conn);
3842                                         spin_lock_irqsave(&sched->ibs_lock,
3843                                                               flags);
3844                                         continue;
3845                                 }
3846
3847                                 rc = ib_poll_cq(conn->ibc_cq, 1, &wc);
3848                         }
3849
3850                         if (unlikely(rc > 0 && wc.wr_id == IBLND_WID_INVAL)) {
3851                                 LCONSOLE_ERROR(
3852                                         "ib_poll_cq (rc: %d) returned invalid "
3853                                         "wr_id, opcode %d, status: %d, "
3854                                         "vendor_err: %d, conn: %s status: %d\n"
3855                                         "please upgrade firmware and OFED or "
3856                                         "contact vendor.\n", rc,
3857                                         wc.opcode, wc.status, wc.vendor_err,
3858                                         libcfs_nid2str(conn->ibc_peer->ibp_nid),
3859                                         conn->ibc_state);
3860                                 rc = -EINVAL;
3861                         }
3862
3863                         if (rc < 0) {
3864                                 CWARN("%s: ib_poll_cq failed: %d, "
3865                                       "closing connection\n",
3866                                       libcfs_nid2str(conn->ibc_peer->ibp_nid),
3867                                       rc);
3868                                 kiblnd_close_conn(conn, -EIO);
3869                                 kiblnd_conn_decref(conn);
3870                                 spin_lock_irqsave(&sched->ibs_lock, flags);
3871                                 continue;
3872                         }
3873
3874                         spin_lock_irqsave(&sched->ibs_lock, flags);
3875
3876                         if (rc != 0 || conn->ibc_ready) {
3877                                 /* There may be another completion waiting; get
3878                                  * another scheduler to check while I handle
3879                                  * this one... */
3880                                 /* +1 ref for sched_conns */
3881                                 kiblnd_conn_addref(conn);
3882                                 list_add_tail(&conn->ibc_sched_list,
3883                                                   &sched->ibs_conns);
3884                                 if (waitqueue_active(&sched->ibs_waitq))
3885                                         wake_up(&sched->ibs_waitq);
3886                         } else {
3887                                 conn->ibc_scheduled = 0;
3888                         }
3889
3890                         if (rc != 0) {
3891                                 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3892                                 kiblnd_complete(&wc);
3893
3894                                 spin_lock_irqsave(&sched->ibs_lock, flags);
3895                         }
3896
3897                         kiblnd_conn_decref(conn); /* ...drop my ref from above */
3898                         did_something = 1;
3899                 }
3900
3901                 if (did_something)
3902                         continue;
3903
3904                 set_current_state(TASK_INTERRUPTIBLE);
3905                 add_wait_queue_exclusive(&sched->ibs_waitq, &wait);
3906                 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3907
3908                 schedule();
3909                 busy_loops = 0;
3910
3911                 remove_wait_queue(&sched->ibs_waitq, &wait);
3912                 set_current_state(TASK_RUNNING);
3913                 spin_lock_irqsave(&sched->ibs_lock, flags);
3914         }
3915
3916         spin_unlock_irqrestore(&sched->ibs_lock, flags);
3917
3918         kiblnd_thread_fini();
3919         return 0;
3920 }
3921
3922 int
3923 kiblnd_failover_thread(void *arg)
3924 {
3925         rwlock_t        *glock = &kiblnd_data.kib_global_lock;
3926         struct kib_dev *dev;
3927         struct net *ns = arg;
3928         wait_queue_entry_t wait;
3929         unsigned long    flags;
3930         int              rc;
3931
3932         LASSERT(*kiblnd_tunables.kib_dev_failover != 0);
3933
3934         cfs_block_allsigs();
3935
3936         init_waitqueue_entry(&wait, current);
3937         write_lock_irqsave(glock, flags);
3938
3939         while (!kiblnd_data.kib_shutdown) {
3940                 int     do_failover = 0;
3941                 int     long_sleep;
3942
3943                 list_for_each_entry(dev, &kiblnd_data.kib_failed_devs,
3944                                     ibd_fail_list) {
3945                         if (ktime_get_seconds() < dev->ibd_next_failover)
3946                                 continue;
3947                         do_failover = 1;
3948                         break;
3949                 }
3950
3951                 if (do_failover) {
3952                         list_del_init(&dev->ibd_fail_list);
3953                         dev->ibd_failover = 1;
3954                         write_unlock_irqrestore(glock, flags);
3955
3956                         rc = kiblnd_dev_failover(dev, ns);
3957
3958                         write_lock_irqsave(glock, flags);
3959
3960                         LASSERT (dev->ibd_failover);
3961                         dev->ibd_failover = 0;
3962                         if (rc >= 0) { /* Device is OK or failover succeed */
3963                                 dev->ibd_next_failover = ktime_get_seconds() + 3;
3964                                 continue;
3965                         }
3966
3967                         /* failed to failover, retry later */
3968                         dev->ibd_next_failover = ktime_get_seconds() +
3969                                                  min(dev->ibd_failed_failover, 10);
3970                         if (kiblnd_dev_can_failover(dev)) {
3971                                 list_add_tail(&dev->ibd_fail_list,
3972                                               &kiblnd_data.kib_failed_devs);
3973                         }
3974
3975                         continue;
3976                 }
3977
3978                 /* long sleep if no more pending failover */
3979                 long_sleep = list_empty(&kiblnd_data.kib_failed_devs);
3980
3981                 set_current_state(TASK_INTERRUPTIBLE);
3982                 add_wait_queue(&kiblnd_data.kib_failover_waitq, &wait);
3983                 write_unlock_irqrestore(glock, flags);
3984
3985                 rc = schedule_timeout(long_sleep ? cfs_time_seconds(10) :
3986                                                    cfs_time_seconds(1));
3987                 set_current_state(TASK_RUNNING);
3988                 remove_wait_queue(&kiblnd_data.kib_failover_waitq, &wait);
3989                 write_lock_irqsave(glock, flags);
3990
3991                 if (!long_sleep || rc != 0)
3992                         continue;
3993
3994                 /* have a long sleep, routine check all active devices,
3995                  * we need checking like this because if there is not active
3996                  * connection on the dev and no SEND from local, we may listen
3997                  * on wrong HCA for ever while there is a bonding failover */
3998                 list_for_each_entry(dev, &kiblnd_data.kib_devs, ibd_list) {
3999                         if (kiblnd_dev_can_failover(dev)) {
4000                                 list_add_tail(&dev->ibd_fail_list,
4001                                               &kiblnd_data.kib_failed_devs);
4002                         }
4003                 }
4004         }
4005
4006         write_unlock_irqrestore(glock, flags);
4007
4008         kiblnd_thread_fini();
4009         return 0;
4010 }