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