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