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