4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2016, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lnet/klnds/o2iblnd/o2iblnd.h
34 * Author: Eric Barton <eric@bartonsoftware.com>
37 #ifdef HAVE_COMPAT_RDMA
38 #include <linux/compat-2.6.h>
40 #ifdef LINUX_3_17_COMPAT_H
41 #undef NEED_KTIME_GET_REAL_NS
46 #include <linux/version.h>
47 #include <linux/module.h>
48 #include <linux/kernel.h>
49 #include <linux/kthread.h>
51 #include <linux/string.h>
52 #include <linux/stat.h>
53 #include <linux/errno.h>
54 #include <linux/unistd.h>
55 #include <linux/uio.h>
57 #include <asm/uaccess.h>
60 #include <linux/init.h>
62 #include <linux/file.h>
63 #include <linux/stat.h>
64 #include <linux/list.h>
65 #include <linux/kmod.h>
66 #include <linux/sysctl.h>
67 #include <linux/pci.h>
68 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,32)
69 #include <linux/pci-dma.h>
75 #include <rdma/rdma_cm.h>
76 #include <rdma/ib_cm.h>
77 #include <rdma/ib_verbs.h>
78 #include <rdma/ib_fmr_pool.h>
80 #define DEBUG_SUBSYSTEM S_LND
82 #include <libcfs/libcfs.h>
83 #include <lnet/lnet.h>
84 #include <lnet/lib-lnet.h>
86 #define IBLND_PEER_HASH_SIZE 101 /* # peer_ni lists */
87 /* # scheduler loops before reschedule */
88 #define IBLND_RESCHED 100
90 #define IBLND_N_SCHED 2
91 #define IBLND_N_SCHED_HIGH 4
95 int *kib_dev_failover; /* HCA failover */
96 unsigned int *kib_service; /* IB service number */
97 int *kib_min_reconnect_interval; /* first failed connection retry... */
98 int *kib_max_reconnect_interval; /* ...exponentially increasing to this */
99 int *kib_cksum; /* checksum kib_msg_t? */
100 int *kib_timeout; /* comms timeout (seconds) */
101 int *kib_keepalive; /* keepalive timeout (seconds) */
102 int *kib_ntx; /* # tx descs */
103 char **kib_default_ipif; /* default IPoIB interface */
104 int *kib_retry_count;
105 int *kib_rnr_retry_count;
106 int *kib_ib_mtu; /* IB MTU */
107 int *kib_require_priv_port;/* accept only privileged ports */
108 int *kib_use_priv_port; /* use privileged port for active connect */
109 /* # threads on each CPT */
111 int *kib_wrq_sge; /* # sg elements per wrq */
114 extern kib_tunables_t kiblnd_tunables;
116 #define IBLND_MSG_QUEUE_SIZE_V1 8 /* V1 only : # messages/RDMAs in-flight */
117 #define IBLND_CREDIT_HIGHWATER_V1 7 /* V1 only : when eagerly to return credits */
119 #define IBLND_CREDITS_DEFAULT 8 /* default # of peer_ni credits */
120 #define IBLND_CREDITS_MAX ((typeof(((kib_msg_t*) 0)->ibm_credits)) - 1) /* Max # of peer_ni credits */
122 /* when eagerly to return credits */
123 #define IBLND_CREDITS_HIGHWATER(t, v) ((v) == IBLND_MSG_VERSION_1 ? \
124 IBLND_CREDIT_HIGHWATER_V1 : \
125 t->lnd_peercredits_hiw)
127 #ifdef HAVE_RDMA_CREATE_ID_5ARG
128 # define kiblnd_rdma_create_id(cb, dev, ps, qpt) rdma_create_id(current->nsproxy->net_ns, \
132 # ifdef HAVE_RDMA_CREATE_ID_4ARG
133 # define kiblnd_rdma_create_id(cb, dev, ps, qpt) rdma_create_id(cb, dev, \
136 # define kiblnd_rdma_create_id(cb, dev, ps, qpt) rdma_create_id(cb, dev, ps)
140 /* 2 OOB shall suffice for 1 keepalive and 1 returning credits */
141 #define IBLND_OOB_CAPABLE(v) ((v) != IBLND_MSG_VERSION_1)
142 #define IBLND_OOB_MSGS(v) (IBLND_OOB_CAPABLE(v) ? 2 : 0)
144 #define IBLND_MSG_SIZE (4<<10) /* max size of queued messages (inc hdr) */
145 #define IBLND_MAX_RDMA_FRAGS LNET_MAX_IOV /* max # of fragments supported */
147 /************************/
148 /* derived constants... */
149 /* Pools (shared by connections on each CPT) */
150 /* These pools can grow at runtime, so don't need give a very large value */
151 #define IBLND_TX_POOL 256
152 #define IBLND_FMR_POOL 256
153 #define IBLND_FMR_POOL_FLUSH 192
155 /* RX messages (per connection) */
156 #define IBLND_RX_MSGS(c) \
157 ((c->ibc_queue_depth) * 2 + IBLND_OOB_MSGS(c->ibc_version))
158 #define IBLND_RX_MSG_BYTES(c) (IBLND_RX_MSGS(c) * IBLND_MSG_SIZE)
159 #define IBLND_RX_MSG_PAGES(c) \
160 ((IBLND_RX_MSG_BYTES(c) + PAGE_SIZE - 1) / PAGE_SIZE)
162 /* WRs and CQEs (per connection) */
163 #define IBLND_RECV_WRS(c) IBLND_RX_MSGS(c)
164 #define IBLND_SEND_WRS(c) \
165 ((c->ibc_max_frags + 1) * kiblnd_concurrent_sends(c->ibc_version, \
166 c->ibc_peer->ibp_ni))
167 #define IBLND_CQ_ENTRIES(c) (IBLND_RECV_WRS(c) + IBLND_SEND_WRS(c))
171 /* o2iblnd can run over aliased interface */
173 #define KIB_IFNAME_SIZE IFALIASZ
175 #define KIB_IFNAME_SIZE 256
180 struct list_head ibd_list; /* chain on kib_devs */
181 struct list_head ibd_fail_list; /* chain on kib_failed_devs */
182 __u32 ibd_ifip; /* IPoIB interface IP */
183 /** IPoIB interface name */
184 char ibd_ifname[KIB_IFNAME_SIZE];
185 int ibd_nnets; /* # nets extant */
187 cfs_time_t ibd_next_failover;
188 /* # failover failures */
189 int ibd_failed_failover;
190 /* failover in progress */
191 unsigned int ibd_failover;
192 /* IPoIB interface is a bonding master */
193 unsigned int ibd_can_failover;
194 struct list_head ibd_nets;
195 struct kib_hca_dev *ibd_hdev;
198 typedef struct kib_hca_dev
200 struct rdma_cm_id *ibh_cmid; /* listener cmid */
201 struct ib_device *ibh_ibdev; /* IB device */
202 int ibh_page_shift; /* page shift of current HCA */
203 int ibh_page_size; /* page size of current HCA */
204 __u64 ibh_page_mask; /* page mask of current HCA */
205 int ibh_mr_shift; /* bits shift of max MR size */
206 __u64 ibh_mr_size; /* size of MR */
207 #ifdef HAVE_IB_GET_DMA_MR
208 struct ib_mr *ibh_mrs; /* global MR */
210 struct ib_pd *ibh_pd; /* PD */
211 kib_dev_t *ibh_dev; /* owner */
212 atomic_t ibh_ref; /* refcount */
215 /** # of seconds to keep pool alive */
216 #define IBLND_POOL_DEADLINE 300
217 /** # of seconds to retry if allocation failed */
218 #define IBLND_POOL_RETRY 1
222 int ibp_npages; /* # pages */
223 struct page *ibp_pages[0]; /* page array */
229 typedef int (*kib_ps_pool_create_t)(struct kib_poolset *ps,
230 int inc, struct kib_pool **pp_po);
231 typedef void (*kib_ps_pool_destroy_t)(struct kib_pool *po);
232 typedef void (*kib_ps_node_init_t)(struct kib_pool *po, struct list_head *node);
233 typedef void (*kib_ps_node_fini_t)(struct kib_pool *po, struct list_head *node);
237 #define IBLND_POOL_NAME_LEN 32
239 typedef struct kib_poolset
243 /* network it belongs to */
244 struct kib_net *ps_net;
246 char ps_name[IBLND_POOL_NAME_LEN];
248 struct list_head ps_pool_list;
249 /* failed pool list */
250 struct list_head ps_failed_pool_list;
251 /* time stamp for retry if failed to allocate */
252 cfs_time_t ps_next_retry;
253 /* is allocating new pool */
260 /* create a new pool */
261 kib_ps_pool_create_t ps_pool_create;
263 kib_ps_pool_destroy_t ps_pool_destroy;
264 /* initialize new allocated node */
265 kib_ps_node_init_t ps_node_init;
267 kib_ps_node_fini_t ps_node_fini;
270 typedef struct kib_pool
272 /* chain on pool list */
273 struct list_head po_list;
274 /* pre-allocated node */
275 struct list_head po_free_list;
276 /* pool_set of this pool */
277 kib_poolset_t *po_owner;
278 /* deadline of this pool */
279 cfs_time_t po_deadline;
280 /* # of elements in use */
282 /* pool is created on failed HCA */
284 /* # of pre-allocated elements */
289 kib_poolset_t tps_poolset; /* pool-set */
290 __u64 tps_next_tx_cookie; /* cookie of TX */
294 kib_pool_t tpo_pool; /* pool */
295 struct kib_hca_dev *tpo_hdev; /* device for this pool */
296 struct kib_tx *tpo_tx_descs; /* all the tx descriptors */
297 kib_pages_t *tpo_tx_pages; /* premapped tx msg pages */
302 spinlock_t fps_lock; /* serialize */
303 struct kib_net *fps_net; /* IB network */
304 struct list_head fps_pool_list; /* FMR pool list */
305 struct list_head fps_failed_pool_list; /* FMR pool list */
306 __u64 fps_version; /* validity stamp */
307 int fps_cpt; /* CPT id */
309 int fps_flush_trigger;
311 /* is allocating new pool */
313 /* time stamp for retry if failed to allocate */
314 cfs_time_t fps_next_retry;
317 #ifndef HAVE_IB_RDMA_WR
319 struct ib_send_wr wr;
323 struct kib_fast_reg_descriptor { /* For fast registration */
324 struct list_head frd_list;
325 struct ib_rdma_wr frd_inv_wr;
326 #ifdef HAVE_IB_MAP_MR_SG
327 struct ib_reg_wr frd_fastreg_wr;
329 struct ib_rdma_wr frd_fastreg_wr;
330 struct ib_fast_reg_page_list *frd_frpl;
332 struct ib_mr *frd_mr;
338 struct list_head fpo_list; /* chain on pool list */
339 struct kib_hca_dev *fpo_hdev; /* device for this pool */
340 kib_fmr_poolset_t *fpo_owner; /* owner of this pool */
343 struct ib_fmr_pool *fpo_fmr_pool; /* IB FMR pool */
345 struct { /* For fast registration */
346 struct list_head fpo_pool_list;
350 cfs_time_t fpo_deadline; /* deadline of this pool */
351 int fpo_failed; /* fmr pool is failed */
352 int fpo_map_count; /* # of mapped FMR */
357 kib_fmr_pool_t *fmr_pool; /* pool of FMR */
358 struct ib_pool_fmr *fmr_pfmr; /* IB pool fmr */
359 struct kib_fast_reg_descriptor *fmr_frd;
363 typedef struct kib_net
365 /* chain on kib_dev_t::ibd_nets */
366 struct list_head ibn_list;
367 __u64 ibn_incarnation;/* my epoch */
368 int ibn_init; /* initialisation state */
369 int ibn_shutdown; /* shutting down? */
371 atomic_t ibn_npeers; /* # peers extant */
372 atomic_t ibn_nconns; /* # connections extant */
374 kib_tx_poolset_t **ibn_tx_ps; /* tx pool-set */
375 kib_fmr_poolset_t **ibn_fmr_ps; /* fmr pool-set */
377 kib_dev_t *ibn_dev; /* underlying IB device */
380 #define KIB_THREAD_SHIFT 16
381 #define KIB_THREAD_ID(cpt, tid) ((cpt) << KIB_THREAD_SHIFT | (tid))
382 #define KIB_THREAD_CPT(id) ((id) >> KIB_THREAD_SHIFT)
383 #define KIB_THREAD_TID(id) ((id) & ((1UL << KIB_THREAD_SHIFT) - 1))
385 struct kib_sched_info {
388 /* schedulers sleep here */
389 wait_queue_head_t ibs_waitq;
390 /* conns to check for rx completions */
391 struct list_head ibs_conns;
392 /* number of scheduler threads */
394 /* max allowed scheduler threads */
395 int ibs_nthreads_max;
396 int ibs_cpt; /* CPT id */
401 int kib_init; /* initialisation state */
402 int kib_shutdown; /* shut down? */
403 struct list_head kib_devs; /* IB devices extant */
404 /* list head of failed devices */
405 struct list_head kib_failed_devs;
406 /* schedulers sleep here */
407 wait_queue_head_t kib_failover_waitq;
408 atomic_t kib_nthreads; /* # live threads */
409 /* stabilize net/dev/peer_ni/conn ops */
410 rwlock_t kib_global_lock;
411 /* hash table of all my known peers */
412 struct list_head *kib_peers;
413 /* size of kib_peers */
414 int kib_peer_hash_size;
415 /* the connd task (serialisation assertions) */
417 /* connections to setup/teardown */
418 struct list_head kib_connd_conns;
419 /* connections with zero refcount */
420 struct list_head kib_connd_zombies;
421 /* connections to reconnect */
422 struct list_head kib_reconn_list;
423 /* peers wait for reconnection */
424 struct list_head kib_reconn_wait;
426 * The second that peers are pulled out from \a kib_reconn_wait
429 time64_t kib_reconn_sec;
430 /* connection daemon sleeps here */
431 wait_queue_head_t kib_connd_waitq;
432 spinlock_t kib_connd_lock; /* serialise */
433 struct ib_qp_attr kib_error_qpa; /* QP->ERROR */
434 /* percpt data for schedulers */
435 struct kib_sched_info **kib_scheds;
438 #define IBLND_INIT_NOTHING 0
439 #define IBLND_INIT_DATA 1
440 #define IBLND_INIT_ALL 2
442 /************************************************************************
443 * IB Wire message format.
444 * These are sent in sender's byte order (i.e. receiver flips).
447 typedef struct kib_connparams
449 __u16 ibcp_queue_depth;
450 __u16 ibcp_max_frags;
451 __u32 ibcp_max_msg_size;
452 } WIRE_ATTR kib_connparams_t;
456 struct lnet_hdr ibim_hdr; /* portals header */
457 char ibim_payload[0];/* piggy-backed payload */
458 } WIRE_ATTR kib_immediate_msg_t;
462 __u32 rf_nob; /* # bytes this frag */
463 __u64 rf_addr; /* CAVEAT EMPTOR: misaligned!! */
464 } WIRE_ATTR kib_rdma_frag_t;
468 __u32 rd_key; /* local/remote key */
469 __u32 rd_nfrags; /* # fragments */
470 kib_rdma_frag_t rd_frags[0]; /* buffer frags */
471 } WIRE_ATTR kib_rdma_desc_t;
475 struct lnet_hdr ibprm_hdr; /* portals header */
476 __u64 ibprm_cookie; /* opaque completion cookie */
477 } WIRE_ATTR kib_putreq_msg_t;
481 __u64 ibpam_src_cookie; /* reflected completion cookie */
482 __u64 ibpam_dst_cookie; /* opaque completion cookie */
483 kib_rdma_desc_t ibpam_rd; /* sender's sink buffer */
484 } WIRE_ATTR kib_putack_msg_t;
488 struct lnet_hdr ibgm_hdr; /* portals header */
489 __u64 ibgm_cookie; /* opaque completion cookie */
490 kib_rdma_desc_t ibgm_rd; /* rdma descriptor */
491 } WIRE_ATTR kib_get_msg_t;
495 __u64 ibcm_cookie; /* opaque completion cookie */
496 __s32 ibcm_status; /* < 0 failure: >= 0 length */
497 } WIRE_ATTR kib_completion_msg_t;
501 /* First 2 fields fixed FOR ALL TIME */
502 __u32 ibm_magic; /* I'm an ibnal message */
503 __u16 ibm_version; /* this is my version number */
505 __u8 ibm_type; /* msg type */
506 __u8 ibm_credits; /* returned credits */
507 __u32 ibm_nob; /* # bytes in whole message */
508 __u32 ibm_cksum; /* checksum (0 == no checksum) */
509 __u64 ibm_srcnid; /* sender's NID */
510 __u64 ibm_srcstamp; /* sender's incarnation */
511 __u64 ibm_dstnid; /* destination's NID */
512 __u64 ibm_dststamp; /* destination's incarnation */
515 kib_connparams_t connparams;
516 kib_immediate_msg_t immediate;
517 kib_putreq_msg_t putreq;
518 kib_putack_msg_t putack;
520 kib_completion_msg_t completion;
522 } WIRE_ATTR kib_msg_t;
524 #define IBLND_MSG_MAGIC LNET_PROTO_IB_MAGIC /* unique magic */
526 #define IBLND_MSG_VERSION_1 0x11
527 #define IBLND_MSG_VERSION_2 0x12
528 #define IBLND_MSG_VERSION IBLND_MSG_VERSION_2
530 #define IBLND_MSG_CONNREQ 0xc0 /* connection request */
531 #define IBLND_MSG_CONNACK 0xc1 /* connection acknowledge */
532 #define IBLND_MSG_NOOP 0xd0 /* nothing (just credits) */
533 #define IBLND_MSG_IMMEDIATE 0xd1 /* immediate */
534 #define IBLND_MSG_PUT_REQ 0xd2 /* putreq (src->sink) */
535 #define IBLND_MSG_PUT_NAK 0xd3 /* completion (sink->src) */
536 #define IBLND_MSG_PUT_ACK 0xd4 /* putack (sink->src) */
537 #define IBLND_MSG_PUT_DONE 0xd5 /* completion (src->sink) */
538 #define IBLND_MSG_GET_REQ 0xd6 /* getreq (sink->src) */
539 #define IBLND_MSG_GET_DONE 0xd7 /* completion (src->sink: all OK) */
542 __u32 ibr_magic; /* sender's magic */
543 __u16 ibr_version; /* sender's version */
544 __u8 ibr_why; /* reject reason */
545 __u8 ibr_padding; /* padding */
546 __u64 ibr_incarnation; /* incarnation of peer_ni */
547 kib_connparams_t ibr_cp; /* connection parameters */
548 } WIRE_ATTR kib_rej_t;
550 /* connection rejection reasons */
551 #define IBLND_REJECT_CONN_RACE 1 /* You lost connection race */
552 #define IBLND_REJECT_NO_RESOURCES 2 /* Out of memory/conns etc */
553 #define IBLND_REJECT_FATAL 3 /* Anything else */
555 #define IBLND_REJECT_CONN_UNCOMPAT 4 /* incompatible version peer_ni */
556 #define IBLND_REJECT_CONN_STALE 5 /* stale peer_ni */
558 /* peer_ni's rdma frags doesn't match mine */
559 #define IBLND_REJECT_RDMA_FRAGS 6
560 /* peer_ni's msg queue size doesn't match mine */
561 #define IBLND_REJECT_MSG_QUEUE_SIZE 7
562 #define IBLND_REJECT_INVALID_SRV_ID 8
564 /***********************************************************************/
566 typedef struct kib_rx /* receive message */
568 /* queue for attention */
569 struct list_head rx_list;
571 struct kib_conn *rx_conn;
572 /* # bytes received (-1 while posted) */
574 /* completion status */
575 enum ib_wc_status rx_status;
576 /* message buffer (host vaddr) */
578 /* message buffer (I/O addr) */
580 /* for dma_unmap_single() */
581 DECLARE_PCI_UNMAP_ADDR(rx_msgunmap);
582 /* receive work item... */
583 struct ib_recv_wr rx_wrq;
584 /* ...and its memory */
585 struct ib_sge rx_sge;
588 #define IBLND_POSTRX_DONT_POST 0 /* don't post */
589 #define IBLND_POSTRX_NO_CREDIT 1 /* post: no credits */
590 #define IBLND_POSTRX_PEER_CREDIT 2 /* post: give peer_ni back 1 credit */
591 #define IBLND_POSTRX_RSRVD_CREDIT 3 /* post: give myself back 1 reserved credit */
593 typedef struct kib_tx /* transmit message */
595 /* queue on idle_txs ibc_tx_queue etc. */
596 struct list_head tx_list;
598 kib_tx_pool_t *tx_pool;
600 struct kib_conn *tx_conn;
601 /* # tx callbacks outstanding */
603 /* queued for sending */
605 /* waiting for peer_ni */
607 /* LNET completion status */
609 /* completion deadline */
610 unsigned long tx_deadline;
611 /* completion cookie */
613 /* lnet msgs to finalize on completion */
614 struct lnet_msg *tx_lntmsg[2];
615 /* message buffer (host vaddr) */
617 /* message buffer (I/O addr) */
619 /* for dma_unmap_single() */
620 DECLARE_PCI_UNMAP_ADDR(tx_msgunmap);
621 /** sge for tx_msgaddr */
622 struct ib_sge tx_msgsge;
623 /* # send work items */
625 /* # used scatter/gather elements */
627 /* send work items... */
628 struct ib_rdma_wr *tx_wrq;
629 /* ...and their memory */
630 struct ib_sge *tx_sge;
631 /* rdma descriptor */
632 kib_rdma_desc_t *tx_rd;
633 /* # entries in... */
635 /* dma_map_sg descriptor */
636 struct scatterlist *tx_frags;
637 /* rdma phys page addrs */
645 typedef struct kib_connvars
647 /* connection-in-progress variables */
651 typedef struct kib_conn
653 /* scheduler information */
654 struct kib_sched_info *ibc_sched;
656 struct kib_peer *ibc_peer;
658 kib_hca_dev_t *ibc_hdev;
659 /* stash on peer_ni's conn list */
660 struct list_head ibc_list;
661 /* schedule for attention */
662 struct list_head ibc_sched_list;
663 /* version of connection */
665 /* reconnect later */
666 __u16 ibc_reconnect:1;
667 /* which instance of the peer */
668 __u64 ibc_incarnation;
670 atomic_t ibc_refcount;
671 /* what's happening */
673 /* # uncompleted sends */
674 int ibc_nsends_posted;
675 /* # uncompleted NOOPs */
676 int ibc_noops_posted;
677 /* # credits I have */
679 /* # credits to return */
680 int ibc_outstanding_credits;
681 /* # ACK/DONE msg credits */
682 int ibc_reserved_credits;
683 /* set on comms error */
685 /* connections queue depth */
686 __u16 ibc_queue_depth;
687 /* connections max frags */
689 /* receive buffers owned */
690 unsigned int ibc_nrx:16;
691 /* scheduled for attention */
692 unsigned int ibc_scheduled:1;
693 /* CQ callback fired */
694 unsigned int ibc_ready:1;
695 /* time of last send */
696 unsigned long ibc_last_send;
697 /** link chain for kiblnd_check_conns only */
698 struct list_head ibc_connd_list;
699 /** rxs completed before ESTABLISHED */
700 struct list_head ibc_early_rxs;
701 /** IBLND_MSG_NOOPs for IBLND_MSG_VERSION_1 */
702 struct list_head ibc_tx_noops;
703 /* sends that need a credit */
704 struct list_head ibc_tx_queue;
705 /* sends that don't need a credit */
706 struct list_head ibc_tx_queue_nocred;
707 /* sends that need to reserve an ACK/DONE msg */
708 struct list_head ibc_tx_queue_rsrvd;
709 /* active tx awaiting completion */
710 struct list_head ibc_active_txs;
715 /* premapped rx msg pages */
716 kib_pages_t *ibc_rx_pages;
719 struct rdma_cm_id *ibc_cmid;
720 /* completion queue */
721 struct ib_cq *ibc_cq;
723 /* in-progress connection state */
724 kib_connvars_t *ibc_connvars;
727 #define IBLND_CONN_INIT 0 /* being initialised */
728 #define IBLND_CONN_ACTIVE_CONNECT 1 /* active sending req */
729 #define IBLND_CONN_PASSIVE_WAIT 2 /* passive waiting for rtu */
730 #define IBLND_CONN_ESTABLISHED 3 /* connection established */
731 #define IBLND_CONN_CLOSING 4 /* being closed */
732 #define IBLND_CONN_DISCONNECTED 5 /* disconnected */
734 typedef struct kib_peer
736 /* stash on global peer_ni list */
737 struct list_head ibp_list;
738 /* who's on the other end(s) */
741 struct lnet_ni *ibp_ni;
742 /* all active connections */
743 struct list_head ibp_conns;
744 /* msgs waiting for a conn */
745 struct list_head ibp_tx_queue;
746 /* incarnation of peer_ni */
747 __u64 ibp_incarnation;
748 /* when (in jiffies) I was last alive */
749 cfs_time_t ibp_last_alive;
751 atomic_t ibp_refcount;
752 /* version of peer_ni */
754 /* current passive connection attempts */
755 unsigned short ibp_accepting;
756 /* current active connection attempts */
757 unsigned short ibp_connecting;
758 /* reconnect this peer_ni later */
759 unsigned short ibp_reconnecting:1;
760 /* counter of how many times we triggered a conn race */
761 unsigned char ibp_races;
762 /* # consecutive reconnection attempts to this peer */
763 unsigned int ibp_reconnected;
764 /* errno on closing this peer_ni */
766 /* max map_on_demand */
768 /* max_peer_credits */
769 __u16 ibp_queue_depth;
772 #ifndef HAVE_IB_INC_RKEY
774 * ib_inc_rkey - increments the key portion of the given rkey. Can be used
775 * for calculating a new rkey for type 2 memory windows.
776 * @rkey - the rkey to increment.
778 static inline u32 ib_inc_rkey(u32 rkey)
780 const u32 mask = 0x000000ff;
781 return ((rkey + 1) & mask) | (rkey & ~mask);
785 extern kib_data_t kiblnd_data;
787 extern void kiblnd_hdev_destroy(kib_hca_dev_t *hdev);
789 int kiblnd_msg_queue_size(int version, struct lnet_ni *ni);
791 /* max # of fragments configured by user */
793 kiblnd_cfg_rdma_frags(struct lnet_ni *ni)
795 struct lnet_ioctl_config_o2iblnd_tunables *tunables;
798 tunables = &ni->ni_lnd_tunables.lnd_tun_u.lnd_o2ib;
799 mod = tunables->lnd_map_on_demand;
800 return mod != 0 ? mod : IBLND_MAX_RDMA_FRAGS;
804 kiblnd_rdma_frags(int version, struct lnet_ni *ni)
806 return version == IBLND_MSG_VERSION_1 ?
807 IBLND_MAX_RDMA_FRAGS :
808 kiblnd_cfg_rdma_frags(ni);
812 kiblnd_concurrent_sends(int version, struct lnet_ni *ni)
814 struct lnet_ioctl_config_o2iblnd_tunables *tunables;
815 int concurrent_sends;
817 tunables = &ni->ni_lnd_tunables.lnd_tun_u.lnd_o2ib;
818 concurrent_sends = tunables->lnd_concurrent_sends;
820 if (version == IBLND_MSG_VERSION_1) {
821 if (concurrent_sends > IBLND_MSG_QUEUE_SIZE_V1 * 2)
822 return IBLND_MSG_QUEUE_SIZE_V1 * 2;
824 if (concurrent_sends < IBLND_MSG_QUEUE_SIZE_V1 / 2)
825 return IBLND_MSG_QUEUE_SIZE_V1 / 2;
828 return concurrent_sends;
832 kiblnd_hdev_addref_locked(kib_hca_dev_t *hdev)
834 LASSERT(atomic_read(&hdev->ibh_ref) > 0);
835 atomic_inc(&hdev->ibh_ref);
839 kiblnd_hdev_decref(kib_hca_dev_t *hdev)
841 LASSERT(atomic_read(&hdev->ibh_ref) > 0);
842 if (atomic_dec_and_test(&hdev->ibh_ref))
843 kiblnd_hdev_destroy(hdev);
847 kiblnd_dev_can_failover(kib_dev_t *dev)
849 if (!list_empty(&dev->ibd_fail_list)) /* already scheduled */
852 if (*kiblnd_tunables.kib_dev_failover == 0) /* disabled */
855 if (*kiblnd_tunables.kib_dev_failover > 1) /* force failover */
858 return dev->ibd_can_failover;
861 #define kiblnd_conn_addref(conn) \
863 CDEBUG(D_NET, "conn[%p] (%d)++\n", \
864 (conn), atomic_read(&(conn)->ibc_refcount)); \
865 atomic_inc(&(conn)->ibc_refcount); \
868 #define kiblnd_conn_decref(conn) \
870 unsigned long flags; \
872 CDEBUG(D_NET, "conn[%p] (%d)--\n", \
873 (conn), atomic_read(&(conn)->ibc_refcount)); \
874 LASSERT_ATOMIC_POS(&(conn)->ibc_refcount); \
875 if (atomic_dec_and_test(&(conn)->ibc_refcount)) { \
876 spin_lock_irqsave(&kiblnd_data.kib_connd_lock, flags); \
877 list_add_tail(&(conn)->ibc_list, \
878 &kiblnd_data.kib_connd_zombies); \
879 wake_up(&kiblnd_data.kib_connd_waitq); \
880 spin_unlock_irqrestore(&kiblnd_data.kib_connd_lock, flags);\
884 #define kiblnd_peer_addref(peer_ni) \
886 CDEBUG(D_NET, "peer_ni[%p] -> %s (%d)++\n", \
887 (peer_ni), libcfs_nid2str((peer_ni)->ibp_nid), \
888 atomic_read (&(peer_ni)->ibp_refcount)); \
889 atomic_inc(&(peer_ni)->ibp_refcount); \
892 #define kiblnd_peer_decref(peer_ni) \
894 CDEBUG(D_NET, "peer_ni[%p] -> %s (%d)--\n", \
895 (peer_ni), libcfs_nid2str((peer_ni)->ibp_nid), \
896 atomic_read (&(peer_ni)->ibp_refcount)); \
897 LASSERT_ATOMIC_POS(&(peer_ni)->ibp_refcount); \
898 if (atomic_dec_and_test(&(peer_ni)->ibp_refcount)) \
899 kiblnd_destroy_peer(peer_ni); \
903 kiblnd_peer_connecting(kib_peer_ni_t *peer_ni)
905 return peer_ni->ibp_connecting != 0 ||
906 peer_ni->ibp_reconnecting != 0 ||
907 peer_ni->ibp_accepting != 0;
911 kiblnd_peer_idle(kib_peer_ni_t *peer_ni)
913 return !kiblnd_peer_connecting(peer_ni) && list_empty(&peer_ni->ibp_conns);
916 static inline struct list_head *
917 kiblnd_nid2peerlist (lnet_nid_t nid)
920 ((unsigned int)nid) % kiblnd_data.kib_peer_hash_size;
922 return &kiblnd_data.kib_peers[hash];
926 kiblnd_peer_active (kib_peer_ni_t *peer_ni)
928 /* Am I in the peer_ni hash table? */
929 return !list_empty(&peer_ni->ibp_list);
932 static inline kib_conn_t *
933 kiblnd_get_conn_locked (kib_peer_ni_t *peer_ni)
935 LASSERT(!list_empty(&peer_ni->ibp_conns));
937 /* just return the first connection */
938 return list_entry(peer_ni->ibp_conns.next, kib_conn_t, ibc_list);
942 kiblnd_send_keepalive(kib_conn_t *conn)
944 return (*kiblnd_tunables.kib_keepalive > 0) &&
945 cfs_time_after(jiffies, conn->ibc_last_send +
946 msecs_to_jiffies(*kiblnd_tunables.kib_keepalive *
951 kiblnd_need_noop(kib_conn_t *conn)
953 struct lnet_ni *ni = conn->ibc_peer->ibp_ni;
954 struct lnet_ioctl_config_o2iblnd_tunables *tunables;
956 LASSERT(conn->ibc_state >= IBLND_CONN_ESTABLISHED);
957 tunables = &ni->ni_lnd_tunables.lnd_tun_u.lnd_o2ib;
959 if (conn->ibc_outstanding_credits <
960 IBLND_CREDITS_HIGHWATER(tunables, conn->ibc_version) &&
961 !kiblnd_send_keepalive(conn))
962 return 0; /* No need to send NOOP */
964 if (IBLND_OOB_CAPABLE(conn->ibc_version)) {
965 if (!list_empty(&conn->ibc_tx_queue_nocred))
966 return 0; /* NOOP can be piggybacked */
968 /* No tx to piggyback NOOP onto or no credit to send a tx */
969 return (list_empty(&conn->ibc_tx_queue) ||
970 conn->ibc_credits == 0);
973 if (!list_empty(&conn->ibc_tx_noops) || /* NOOP already queued */
974 !list_empty(&conn->ibc_tx_queue_nocred) || /* piggyback NOOP */
975 conn->ibc_credits == 0) /* no credit */
978 if (conn->ibc_credits == 1 && /* last credit reserved for */
979 conn->ibc_outstanding_credits == 0) /* giving back credits */
982 /* No tx to piggyback NOOP onto or no credit to send a tx */
983 return (list_empty(&conn->ibc_tx_queue) || conn->ibc_credits == 1);
987 kiblnd_abort_receives(kib_conn_t *conn)
989 ib_modify_qp(conn->ibc_cmid->qp,
990 &kiblnd_data.kib_error_qpa, IB_QP_STATE);
993 static inline const char *
994 kiblnd_queue2str(kib_conn_t *conn, struct list_head *q)
996 if (q == &conn->ibc_tx_queue)
999 if (q == &conn->ibc_tx_queue_rsrvd)
1000 return "tx_queue_rsrvd";
1002 if (q == &conn->ibc_tx_queue_nocred)
1003 return "tx_queue_nocred";
1005 if (q == &conn->ibc_active_txs)
1006 return "active_txs";
1012 /* CAVEAT EMPTOR: We rely on descriptor alignment to allow us to use the
1013 * lowest bits of the work request id to stash the work item type. */
1015 #define IBLND_WID_INVAL 0
1016 #define IBLND_WID_TX 1
1017 #define IBLND_WID_RX 2
1018 #define IBLND_WID_RDMA 3
1019 #define IBLND_WID_MR 4
1020 #define IBLND_WID_MASK 7UL
1023 kiblnd_ptr2wreqid (void *ptr, int type)
1025 unsigned long lptr = (unsigned long)ptr;
1027 LASSERT ((lptr & IBLND_WID_MASK) == 0);
1028 LASSERT ((type & ~IBLND_WID_MASK) == 0);
1029 return (__u64)(lptr | type);
1032 static inline void *
1033 kiblnd_wreqid2ptr (__u64 wreqid)
1035 return (void *)(((unsigned long)wreqid) & ~IBLND_WID_MASK);
1039 kiblnd_wreqid2type (__u64 wreqid)
1041 return (wreqid & IBLND_WID_MASK);
1045 kiblnd_set_conn_state (kib_conn_t *conn, int state)
1047 conn->ibc_state = state;
1052 kiblnd_init_msg (kib_msg_t *msg, int type, int body_nob)
1054 msg->ibm_type = type;
1055 msg->ibm_nob = offsetof(kib_msg_t, ibm_u) + body_nob;
1059 kiblnd_rd_size (kib_rdma_desc_t *rd)
1064 for (i = size = 0; i < rd->rd_nfrags; i++)
1065 size += rd->rd_frags[i].rf_nob;
1071 kiblnd_rd_frag_addr(kib_rdma_desc_t *rd, int index)
1073 return rd->rd_frags[index].rf_addr;
1077 kiblnd_rd_frag_size(kib_rdma_desc_t *rd, int index)
1079 return rd->rd_frags[index].rf_nob;
1083 kiblnd_rd_frag_key(kib_rdma_desc_t *rd, int index)
1089 kiblnd_rd_consume_frag(kib_rdma_desc_t *rd, int index, __u32 nob)
1091 if (nob < rd->rd_frags[index].rf_nob) {
1092 rd->rd_frags[index].rf_addr += nob;
1093 rd->rd_frags[index].rf_nob -= nob;
1102 kiblnd_rd_msg_size(kib_rdma_desc_t *rd, int msgtype, int n)
1104 LASSERT (msgtype == IBLND_MSG_GET_REQ ||
1105 msgtype == IBLND_MSG_PUT_ACK);
1107 return msgtype == IBLND_MSG_GET_REQ ?
1108 offsetof(kib_get_msg_t, ibgm_rd.rd_frags[n]) :
1109 offsetof(kib_putack_msg_t, ibpam_rd.rd_frags[n]);
1113 kiblnd_dma_mapping_error(struct ib_device *dev, u64 dma_addr)
1115 return ib_dma_mapping_error(dev, dma_addr);
1118 static inline __u64 kiblnd_dma_map_single(struct ib_device *dev,
1119 void *msg, size_t size,
1120 enum dma_data_direction direction)
1122 return ib_dma_map_single(dev, msg, size, direction);
1125 static inline void kiblnd_dma_unmap_single(struct ib_device *dev,
1126 __u64 addr, size_t size,
1127 enum dma_data_direction direction)
1129 ib_dma_unmap_single(dev, addr, size, direction);
1132 #define KIBLND_UNMAP_ADDR_SET(p, m, a) do {} while (0)
1133 #define KIBLND_UNMAP_ADDR(p, m, a) (a)
1135 static inline int kiblnd_dma_map_sg(struct ib_device *dev,
1136 struct scatterlist *sg, int nents,
1137 enum dma_data_direction direction)
1139 return ib_dma_map_sg(dev, sg, nents, direction);
1142 static inline void kiblnd_dma_unmap_sg(struct ib_device *dev,
1143 struct scatterlist *sg, int nents,
1144 enum dma_data_direction direction)
1146 ib_dma_unmap_sg(dev, sg, nents, direction);
1149 static inline __u64 kiblnd_sg_dma_address(struct ib_device *dev,
1150 struct scatterlist *sg)
1152 return ib_sg_dma_address(dev, sg);
1155 static inline unsigned int kiblnd_sg_dma_len(struct ib_device *dev,
1156 struct scatterlist *sg)
1158 return ib_sg_dma_len(dev, sg);
1161 /* XXX We use KIBLND_CONN_PARAM(e) as writable buffer, it's not strictly
1162 * right because OFED1.2 defines it as const, to use it we have to add
1163 * (void *) cast to overcome "const" */
1165 #define KIBLND_CONN_PARAM(e) ((e)->param.conn.private_data)
1166 #define KIBLND_CONN_PARAM_LEN(e) ((e)->param.conn.private_data_len)
1168 #ifdef HAVE_IB_GET_DMA_MR
1169 struct ib_mr *kiblnd_find_rd_dma_mr(struct lnet_ni *ni, kib_rdma_desc_t *rd,
1170 int negotiated_nfrags);
1172 void kiblnd_map_rx_descs(kib_conn_t *conn);
1173 void kiblnd_unmap_rx_descs(kib_conn_t *conn);
1174 void kiblnd_pool_free_node(kib_pool_t *pool, struct list_head *node);
1175 struct list_head *kiblnd_pool_alloc_node(kib_poolset_t *ps);
1177 int kiblnd_fmr_pool_map(kib_fmr_poolset_t *fps, kib_tx_t *tx,
1178 kib_rdma_desc_t *rd, __u32 nob, __u64 iov,
1180 void kiblnd_fmr_pool_unmap(kib_fmr_t *fmr, int status);
1182 int kiblnd_tunables_setup(struct lnet_ni *ni);
1183 int kiblnd_tunables_init(void);
1185 int kiblnd_connd (void *arg);
1186 int kiblnd_scheduler(void *arg);
1187 int kiblnd_thread_start(int (*fn)(void *arg), void *arg, char *name);
1188 int kiblnd_failover_thread (void *arg);
1190 int kiblnd_alloc_pages(kib_pages_t **pp, int cpt, int npages);
1192 int kiblnd_cm_callback(struct rdma_cm_id *cmid,
1193 struct rdma_cm_event *event);
1194 int kiblnd_translate_mtu(int value);
1196 int kiblnd_dev_failover(kib_dev_t *dev);
1197 int kiblnd_create_peer(struct lnet_ni *ni, kib_peer_ni_t **peerp,
1199 void kiblnd_destroy_peer (kib_peer_ni_t *peer);
1200 bool kiblnd_reconnect_peer(kib_peer_ni_t *peer);
1201 void kiblnd_destroy_dev (kib_dev_t *dev);
1202 void kiblnd_unlink_peer_locked (kib_peer_ni_t *peer_ni);
1203 kib_peer_ni_t *kiblnd_find_peer_locked(struct lnet_ni *ni, lnet_nid_t nid);
1204 int kiblnd_close_stale_conns_locked (kib_peer_ni_t *peer_ni,
1205 int version, __u64 incarnation);
1206 int kiblnd_close_peer_conns_locked (kib_peer_ni_t *peer_ni, int why);
1208 kib_conn_t *kiblnd_create_conn(kib_peer_ni_t *peer_ni, struct rdma_cm_id *cmid,
1209 int state, int version);
1210 void kiblnd_destroy_conn(kib_conn_t *conn, bool free_conn);
1211 void kiblnd_close_conn (kib_conn_t *conn, int error);
1212 void kiblnd_close_conn_locked (kib_conn_t *conn, int error);
1214 void kiblnd_launch_tx(struct lnet_ni *ni, kib_tx_t *tx, lnet_nid_t nid);
1215 void kiblnd_txlist_done(struct list_head *txlist, int status);
1217 void kiblnd_qp_event(struct ib_event *event, void *arg);
1218 void kiblnd_cq_event(struct ib_event *event, void *arg);
1219 void kiblnd_cq_completion(struct ib_cq *cq, void *arg);
1221 void kiblnd_pack_msg(struct lnet_ni *ni, kib_msg_t *msg, int version,
1222 int credits, lnet_nid_t dstnid, __u64 dststamp);
1223 int kiblnd_unpack_msg(kib_msg_t *msg, int nob);
1224 int kiblnd_post_rx (kib_rx_t *rx, int credit);
1226 int kiblnd_send(struct lnet_ni *ni, void *private, struct lnet_msg *lntmsg);
1227 int kiblnd_recv(struct lnet_ni *ni, void *private, struct lnet_msg *lntmsg,
1228 int delayed, unsigned int niov, struct kvec *iov,
1229 lnet_kiov_t *kiov, unsigned int offset, unsigned int mlen,