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, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
31 * lnet/klnds/o2iblnd/o2iblnd.h
33 * Author: Eric Barton <eric@bartonsoftware.com>
36 #include <linux/module.h>
37 #include <linux/kernel.h>
39 #if defined(NEED_LOCKDEP_IS_HELD_DISCARD_CONST) \
40 && defined(CONFIG_LOCKDEP) \
41 && defined(lockdep_is_held)
42 #undef lockdep_is_held
43 #define lockdep_is_held(lock) \
44 lock_is_held((struct lockdep_map *)&(lock)->dep_map)
47 #ifdef HAVE_COMPAT_RDMA
48 #include <linux/compat-2.6.h>
50 #ifdef LINUX_3_17_COMPAT_H
51 #undef NEED_KTIME_GET_REAL_NS
54 #define HAVE_NLA_PUT_U64_64BIT 1
55 #define HAVE_NLA_PARSE_6_PARAMS 1
56 #define HAVE_NETLINK_EXTACK 1
59 /* MOFED has its own bitmap_alloc backport */
60 #define HAVE_BITMAP_ALLOC 1
64 #include <linux/kthread.h>
66 #include <linux/string.h>
67 #include <linux/stat.h>
68 #include <linux/errno.h>
69 #include <linux/unistd.h>
70 #include <linux/uio.h>
72 #include <asm/uaccess.h>
75 #include <linux/init.h>
77 #include <linux/file.h>
78 #include <linux/stat.h>
79 #include <linux/list.h>
80 #include <linux/kmod.h>
81 #include <linux/sysctl.h>
82 #include <linux/pci.h>
87 #include <rdma/rdma_cm.h>
88 #include <rdma/ib_cm.h>
89 #include <rdma/ib_verbs.h>
90 #ifdef HAVE_FMR_POOL_API
91 #include <rdma/ib_fmr_pool.h>
94 #define DEBUG_SUBSYSTEM S_LND
96 #include <lnet/lib-lnet.h>
97 #include <lnet/lnet_rdma.h>
98 #include "o2iblnd-idl.h"
100 #define IBLND_PEER_HASH_BITS 7 /* log2 of # peer_ni lists */
101 #define IBLND_N_SCHED 2
102 #define IBLND_N_SCHED_HIGH 4
104 struct kib_tunables {
105 int *kib_dev_failover; /* HCA failover */
106 unsigned int *kib_service; /* IB service number */
107 int *kib_cksum; /* checksum struct kib_msg? */
108 int *kib_timeout; /* comms timeout (seconds) */
109 int *kib_keepalive; /* keepalive timeout (seconds) */
110 char **kib_default_ipif; /* default IPoIB interface */
111 int *kib_retry_count;
112 int *kib_rnr_retry_count;
113 int *kib_ib_mtu; /* IB MTU */
114 int *kib_require_priv_port;/* accept only privileged ports */
115 int *kib_use_priv_port; /* use privileged port for active connect */
116 /* # threads on each CPT */
118 int *kib_wrq_sge; /* # sg elements per wrq */
119 int *kib_use_fastreg_gaps; /* enable discontiguous fastreg fragment support */
122 extern struct kib_tunables kiblnd_tunables;
124 #define IBLND_MSG_QUEUE_SIZE_V1 8 /* V1 only : # messages/RDMAs in-flight */
125 #define IBLND_CREDIT_HIGHWATER_V1 7 /* V1 only : when eagerly to return credits */
127 #define IBLND_CREDITS_DEFAULT 8 /* default # of peer_ni credits */
128 #define IBLND_CREDITS_MAX ((typeof(((struct kib_msg *) 0)->ibm_credits)) - 1) /* Max # of peer_ni credits */
130 /* when eagerly to return credits */
131 #define IBLND_CREDITS_HIGHWATER(t, conn) ((conn->ibc_version) == IBLND_MSG_VERSION_1 ? \
132 IBLND_CREDIT_HIGHWATER_V1 : \
133 min(t->lnd_peercredits_hiw, (__u32)conn->ibc_queue_depth - 1))
135 #ifdef HAVE_RDMA_CREATE_ID_5ARG
136 # define kiblnd_rdma_create_id(ns, cb, dev, ps, qpt) \
137 rdma_create_id((ns) ? (ns) : &init_net, cb, dev, ps, qpt)
139 # ifdef HAVE_RDMA_CREATE_ID_4ARG
140 # define kiblnd_rdma_create_id(ns, cb, dev, ps, qpt) \
141 rdma_create_id(cb, dev, ps, qpt)
143 # define kiblnd_rdma_create_id(ns, cb, dev, ps, qpt) \
144 rdma_create_id(cb, dev, ps)
148 /* 2 OOB shall suffice for 1 keepalive and 1 returning credits */
149 #define IBLND_OOB_CAPABLE(v) ((v) != IBLND_MSG_VERSION_1)
150 #define IBLND_OOB_MSGS(v) (IBLND_OOB_CAPABLE(v) ? 2 : 0)
152 /* max size of queued messages (inc hdr) */
153 #define IBLND_MSG_SIZE (4<<10)
154 /* max # of fragments supported. + 1 for unaligned case */
155 #define IBLND_MAX_RDMA_FRAGS (LNET_MAX_IOV + 1)
157 /************************/
158 /* derived constants... */
159 /* Pools (shared by connections on each CPT) */
160 /* These pools can grow at runtime, so don't need give a very large value */
161 #define IBLND_TX_POOL 256
162 #define IBLND_FMR_POOL 256
163 #define IBLND_FMR_POOL_FLUSH 192
165 /* RX messages (per connection) */
166 #define IBLND_RX_MSGS(c) \
167 ((c->ibc_queue_depth) * 2 + IBLND_OOB_MSGS(c->ibc_version))
168 #define IBLND_RX_MSG_BYTES(c) (IBLND_RX_MSGS(c) * IBLND_MSG_SIZE)
169 #define IBLND_RX_MSG_PAGES(c) \
170 ((IBLND_RX_MSG_BYTES(c) + PAGE_SIZE - 1) / PAGE_SIZE)
172 /* WRs and CQEs (per connection) */
173 #define IBLND_RECV_WRS(c) IBLND_RX_MSGS(c)
175 /* 2 = LNet msg + Transfer chain */
176 #define IBLND_CQ_ENTRIES(c) (IBLND_RECV_WRS(c) + kiblnd_send_wrs(c))
180 /* o2iblnd can run over aliased interface */
182 #define KIB_IFNAME_SIZE IFALIASZ
184 #define KIB_IFNAME_SIZE 256
188 IBLND_DEV_CAPS_FASTREG_ENABLED = BIT(0),
189 IBLND_DEV_CAPS_FASTREG_GAPS_SUPPORT = BIT(1),
190 #ifdef HAVE_FMR_POOL_API
191 IBLND_DEV_CAPS_FMR_ENABLED = BIT(2),
195 #define IS_FAST_REG_DEV(dev) \
196 ((dev)->ibd_dev_caps & IBLND_DEV_CAPS_FASTREG_ENABLED)
200 struct list_head ibd_list; /* chain on kib_devs */
201 struct list_head ibd_fail_list; /* chain on kib_failed_devs */
202 __u32 ibd_ifip; /* IPoIB interface IP */
203 /** IPoIB interface name */
204 char ibd_ifname[KIB_IFNAME_SIZE];
205 int ibd_nnets; /* # nets extant */
207 time64_t ibd_next_failover;
208 /* # failover failures */
209 int ibd_failed_failover;
210 /* failover in progress */
211 unsigned int ibd_failover;
212 /* IPoIB interface is a bonding master */
213 unsigned int ibd_can_failover;
214 struct list_head ibd_nets;
215 struct kib_hca_dev *ibd_hdev;
216 enum kib_dev_caps ibd_dev_caps;
220 struct rdma_cm_id *ibh_cmid; /* listener cmid */
221 struct ib_device *ibh_ibdev; /* IB device */
222 int ibh_page_shift; /* page shift of current HCA */
223 int ibh_page_size; /* page size of current HCA */
224 __u64 ibh_page_mask; /* page mask of current HCA */
225 __u64 ibh_mr_size; /* size of MR */
226 int ibh_max_qp_wr; /* maximum work requests size */
227 #ifdef HAVE_IB_GET_DMA_MR
228 struct ib_mr *ibh_mrs; /* global MR */
230 struct ib_pd *ibh_pd; /* PD */
231 u8 ibh_port; /* port number */
232 struct ib_event_handler
233 ibh_event_handler; /* IB event handler */
234 int ibh_state; /* device status */
235 #define IBLND_DEV_PORT_DOWN 0
236 #define IBLND_DEV_PORT_ACTIVE 1
237 #define IBLND_DEV_FATAL 2
238 struct kib_dev *ibh_dev; /* owner */
239 atomic_t ibh_ref; /* refcount */
242 /** # of seconds to keep pool alive */
243 #define IBLND_POOL_DEADLINE 300
244 /** # of seconds to retry if allocation failed */
245 #define IBLND_POOL_RETRY 1
248 int ibp_npages; /* # pages */
249 struct page *ibp_pages[0]; /* page array */
255 typedef int (*kib_ps_pool_create_t)(struct kib_poolset *ps,
256 int inc, struct kib_pool **pp_po);
257 typedef void (*kib_ps_pool_destroy_t)(struct kib_pool *po);
258 typedef void (*kib_ps_node_init_t)(struct kib_pool *po, struct list_head *node);
259 typedef void (*kib_ps_node_fini_t)(struct kib_pool *po, struct list_head *node);
263 #define IBLND_POOL_NAME_LEN 32
268 /* network it belongs to */
269 struct kib_net *ps_net;
271 char ps_name[IBLND_POOL_NAME_LEN];
273 struct list_head ps_pool_list;
274 /* failed pool list */
275 struct list_head ps_failed_pool_list;
276 /* time stamp for retry if failed to allocate */
277 time64_t ps_next_retry;
278 /* is allocating new pool */
285 /* create a new pool */
286 kib_ps_pool_create_t ps_pool_create;
288 kib_ps_pool_destroy_t ps_pool_destroy;
289 /* initialize new allocated node */
290 kib_ps_node_init_t ps_node_init;
292 kib_ps_node_fini_t ps_node_fini;
296 /* chain on pool list */
297 struct list_head po_list;
298 /* pre-allocated node */
299 struct list_head po_free_list;
300 /* pool_set of this pool */
301 struct kib_poolset *po_owner;
302 /* deadline of this pool */
303 time64_t po_deadline;
304 /* # of elements in use */
306 /* pool is created on failed HCA */
308 /* # of pre-allocated elements */
312 struct kib_tx_poolset {
313 struct kib_poolset tps_poolset; /* pool-set */
314 __u64 tps_next_tx_cookie; /* cookie of TX */
318 struct kib_pool tpo_pool; /* pool */
319 struct kib_hca_dev *tpo_hdev; /* device for this pool */
320 struct kib_tx *tpo_tx_descs; /* all the tx descriptors */
321 struct kib_pages *tpo_tx_pages; /* premapped tx msg pages */
324 struct kib_fmr_poolset {
325 spinlock_t fps_lock; /* serialize */
326 struct kib_net *fps_net; /* IB network */
327 struct list_head fps_pool_list; /* FMR pool list */
328 struct list_head fps_failed_pool_list; /* FMR pool list */
329 __u64 fps_version; /* validity stamp */
330 int fps_cpt; /* CPT id */
332 int fps_flush_trigger;
334 /* is allocating new pool */
336 /* time stamp for retry if failed to allocate */
337 time64_t fps_next_retry;
340 #ifndef HAVE_IB_RDMA_WR
342 struct ib_send_wr wr;
346 struct kib_fast_reg_descriptor { /* For fast registration */
347 struct list_head frd_list;
348 struct ib_rdma_wr frd_inv_wr;
349 #ifdef HAVE_IB_MAP_MR_SG
350 struct ib_reg_wr frd_fastreg_wr;
352 struct ib_rdma_wr frd_fastreg_wr;
353 struct ib_fast_reg_page_list *frd_frpl;
355 struct ib_mr *frd_mr;
360 struct kib_fmr_pool {
361 struct list_head fpo_list; /* chain on pool list */
362 struct kib_hca_dev *fpo_hdev; /* device for this pool */
363 struct kib_fmr_poolset *fpo_owner; /* owner of this pool */
364 #ifdef HAVE_FMR_POOL_API
367 struct ib_fmr_pool *fpo_fmr_pool; /* IB FMR pool */
370 struct { /* For fast registration */
371 struct list_head fpo_pool_list;
374 #ifdef HAVE_FMR_POOL_API
376 bool fpo_is_fmr; /* True if FMR pools allocated */
378 time64_t fpo_deadline; /* deadline of this pool */
379 int fpo_failed; /* fmr pool is failed */
380 int fpo_map_count; /* # of mapped FMR */
384 struct kib_fmr_pool *fmr_pool; /* pool of FMR */
385 #ifdef HAVE_FMR_POOL_API
386 struct ib_pool_fmr *fmr_pfmr; /* IB pool fmr */
387 #endif /* HAVE_FMR_POOL_API */
388 struct kib_fast_reg_descriptor *fmr_frd;
392 #ifdef HAVE_FMR_POOL_API
394 #ifdef HAVE_ORACLE_OFED_EXTENSIONS
395 #define kib_fmr_pool_map(pool, pgs, n, iov) \
396 ib_fmr_pool_map_phys((pool), (pgs), (n), (iov), NULL)
398 #define kib_fmr_pool_map(pool, pgs, n, iov) \
399 ib_fmr_pool_map_phys((pool), (pgs), (n), (iov))
402 #endif /* HAVE_FMR_POOL_API */
405 /* chain on struct kib_dev::ibd_nets */
406 struct list_head ibn_list;
407 __u64 ibn_incarnation;/* my epoch */
408 int ibn_init; /* initialisation state */
409 int ibn_shutdown; /* shutting down? */
411 atomic_t ibn_npeers; /* # peers extant */
412 atomic_t ibn_nconns; /* # connections extant */
414 struct kib_tx_poolset **ibn_tx_ps; /* tx pool-set */
415 struct kib_fmr_poolset **ibn_fmr_ps; /* fmr pool-set */
417 struct kib_dev *ibn_dev; /* underlying IB device */
418 struct lnet_ni *ibn_ni; /* LNet interface */
421 #define KIB_THREAD_SHIFT 16
422 #define KIB_THREAD_ID(cpt, tid) ((cpt) << KIB_THREAD_SHIFT | (tid))
423 #define KIB_THREAD_CPT(id) ((id) >> KIB_THREAD_SHIFT)
424 #define KIB_THREAD_TID(id) ((id) & ((1UL << KIB_THREAD_SHIFT) - 1))
426 struct kib_sched_info {
429 /* schedulers sleep here */
430 wait_queue_head_t ibs_waitq;
431 /* conns to check for rx completions */
432 struct list_head ibs_conns;
433 /* number of scheduler threads */
435 /* max allowed scheduler threads */
436 int ibs_nthreads_max;
437 int ibs_cpt; /* CPT id */
441 int kib_init; /* initialisation state */
442 int kib_shutdown; /* shut down? */
443 struct list_head kib_devs; /* IB devices extant */
444 /* list head of failed devices */
445 struct list_head kib_failed_devs;
446 /* schedulers sleep here */
447 wait_queue_head_t kib_failover_waitq;
448 atomic_t kib_nthreads; /* # live threads */
449 /* stabilize net/dev/peer_ni/conn ops */
450 rwlock_t kib_global_lock;
451 /* hash table of all my known peers */
452 DECLARE_HASHTABLE(kib_peers, IBLND_PEER_HASH_BITS);
453 /* the connd task (serialisation assertions) */
455 /* connections to setup/teardown */
456 struct list_head kib_connd_conns;
457 /* connections with zero refcount */
458 struct list_head kib_connd_zombies;
459 /* connections to reconnect */
460 struct list_head kib_reconn_list;
461 /* peers wait for reconnection */
462 struct list_head kib_reconn_wait;
463 /* connections wait for completion */
464 struct list_head kib_connd_waits;
466 * The second that peers are pulled out from \a kib_reconn_wait
469 time64_t kib_reconn_sec;
470 /* connection daemon sleeps here */
471 wait_queue_head_t kib_connd_waitq;
472 spinlock_t kib_connd_lock; /* serialise */
473 struct ib_qp_attr kib_error_qpa; /* QP->ERROR */
474 /* percpt data for schedulers */
475 struct kib_sched_info **kib_scheds;
478 #define IBLND_INIT_NOTHING 0
479 #define IBLND_INIT_DATA 1
480 #define IBLND_INIT_ALL 2
482 struct kib_rx { /* receive message */
483 /* queue for attention */
484 struct list_head rx_list;
486 struct kib_conn *rx_conn;
487 /* # bytes received (-1 while posted) */
489 /* message buffer (host vaddr) */
490 struct kib_msg *rx_msg;
491 /* message buffer (I/O addr) */
493 /* for dma_unmap_single() */
494 DEFINE_DMA_UNMAP_ADDR(rx_msgunmap);
495 /* receive work item... */
496 struct ib_recv_wr rx_wrq;
497 /* ...and its memory */
498 struct ib_sge rx_sge;
501 #define IBLND_POSTRX_DONT_POST 0 /* don't post */
502 #define IBLND_POSTRX_NO_CREDIT 1 /* post: no credits */
503 #define IBLND_POSTRX_PEER_CREDIT 2 /* post: give peer_ni back 1 credit */
504 #define IBLND_POSTRX_RSRVD_CREDIT 3 /* post: give myself back 1 reserved credit */
506 struct kib_tx { /* transmit message */
507 /* queue on idle_txs ibc_tx_queue etc. */
508 struct list_head tx_list;
510 struct kib_tx_pool *tx_pool;
512 struct kib_conn *tx_conn;
513 /* # tx callbacks outstanding */
515 /* queued for sending */
516 unsigned long tx_queued:1,
517 /* waiting for peer_ni */
521 /* LNET completion status */
523 /* health status of the transmit */
524 enum lnet_msg_hstatus tx_hstatus;
525 /* completion deadline */
527 /* completion cookie */
529 /* lnet msgs to finalize on completion */
530 struct lnet_msg *tx_lntmsg[2];
531 /* message buffer (host vaddr) */
532 struct kib_msg *tx_msg;
533 /* message buffer (I/O addr) */
535 /* for dma_unmap_single() */
536 DEFINE_DMA_UNMAP_ADDR(tx_msgunmap);
537 /* # send work items */
539 /* # used scatter/gather elements */
541 /* send work items... */
542 struct ib_rdma_wr *tx_wrq;
543 /* ...and their memory */
544 struct ib_sge *tx_sge;
545 /* rdma descriptor */
546 struct kib_rdma_desc *tx_rd;
547 /* # entries in... */
549 /* dma_map_sg descriptor */
550 struct scatterlist *tx_frags;
551 /* rdma phys page addrs */
553 /* gaps in fragments */
556 struct kib_fmr tx_fmr;
561 struct kib_connvars {
562 /* connection-in-progress variables */
563 struct kib_msg cv_msg;
567 /* scheduler information */
568 struct kib_sched_info *ibc_sched;
570 struct kib_peer_ni *ibc_peer;
572 struct kib_hca_dev *ibc_hdev;
573 /* stash on peer_ni's conn list */
574 struct list_head ibc_list;
575 /* schedule for attention */
576 struct list_head ibc_sched_list;
577 /* version of connection */
579 /* reconnect later */
580 __u16 ibc_reconnect:1;
581 /* which instance of the peer */
582 __u64 ibc_incarnation;
584 atomic_t ibc_refcount;
585 /* what's happening */
587 /* # uncompleted sends */
588 int ibc_nsends_posted;
589 /* # uncompleted NOOPs */
590 int ibc_noops_posted;
591 /* # credits I have */
593 /* # credits to return */
594 int ibc_outstanding_credits;
595 /* # ACK/DONE msg credits */
596 int ibc_reserved_credits;
597 /* set on comms error */
599 /* connections queue depth */
600 __u16 ibc_queue_depth;
601 /* connections max frags */
603 /* count of timeout txs waiting on cq */
605 /* receive buffers owned */
606 unsigned int ibc_nrx:16;
607 /* scheduled for attention */
608 unsigned int ibc_scheduled:1;
609 /* CQ callback fired */
610 unsigned int ibc_ready:1;
611 /* time of last send */
612 ktime_t ibc_last_send;
613 /** link chain for kiblnd_check_conns only */
614 struct list_head ibc_connd_list;
615 /** rxs completed before ESTABLISHED */
616 struct list_head ibc_early_rxs;
617 /** IBLND_MSG_NOOPs for IBLND_MSG_VERSION_1 */
618 struct list_head ibc_tx_noops;
619 /* sends that need a credit */
620 struct list_head ibc_tx_queue;
621 /* sends that don't need a credit */
622 struct list_head ibc_tx_queue_nocred;
623 /* sends that need to reserve an ACK/DONE msg */
624 struct list_head ibc_tx_queue_rsrvd;
625 /* active tx awaiting completion */
626 struct list_head ibc_active_txs;
627 /* zombie tx awaiting done */
628 struct list_head ibc_zombie_txs;
632 struct kib_rx *ibc_rxs;
633 /* premapped rx msg pages */
634 struct kib_pages *ibc_rx_pages;
637 struct rdma_cm_id *ibc_cmid;
638 /* completion queue */
639 struct ib_cq *ibc_cq;
641 /* in-progress connection state */
642 struct kib_connvars *ibc_connvars;
645 #define IBLND_CONN_INIT 0 /* being initialised */
646 #define IBLND_CONN_ACTIVE_CONNECT 1 /* active sending req */
647 #define IBLND_CONN_PASSIVE_WAIT 2 /* passive waiting for rtu */
648 #define IBLND_CONN_ESTABLISHED 3 /* connection established */
649 #define IBLND_CONN_CLOSING 4 /* being closed */
650 #define IBLND_CONN_DISCONNECTED 5 /* disconnected */
653 /* on peer_ni hash chain */
654 struct hlist_node ibp_list;
655 /* who's on the other end(s) */
658 struct lnet_ni *ibp_ni;
659 /* all active connections */
660 struct list_head ibp_conns;
661 /* next connection to send on for round robin */
662 struct kib_conn *ibp_next_conn;
663 /* msgs waiting for a conn */
664 struct list_head ibp_tx_queue;
665 /* incarnation of peer_ni */
666 __u64 ibp_incarnation;
667 /* when (in seconds) I was last alive */
668 time64_t ibp_last_alive;
670 struct kref ibp_kref;
671 /* version of peer_ni */
673 /* current passive connection attempts */
674 unsigned short ibp_accepting;
675 /* current active connection attempts */
676 unsigned short ibp_connecting;
677 /* reconnect this peer_ni later */
678 unsigned char ibp_reconnecting;
679 /* counter of how many times we triggered a conn race */
680 unsigned char ibp_races;
681 /* # consecutive reconnection attempts to this peer */
682 unsigned int ibp_reconnected;
683 /* errno on closing this peer_ni */
685 /* max map_on_demand */
687 /* max_peer_credits */
688 __u16 ibp_queue_depth;
689 /* reduced value which allows conn to be created if max fails */
690 __u16 ibp_queue_depth_mod;
691 /* Number of connections allocated. */
695 #ifndef HAVE_IB_INC_RKEY
697 * ib_inc_rkey - increments the key portion of the given rkey. Can be used
698 * for calculating a new rkey for type 2 memory windows.
699 * @rkey - the rkey to increment.
701 static inline u32 ib_inc_rkey(u32 rkey)
703 const u32 mask = 0x000000ff;
704 return ((rkey + 1) & mask) | (rkey & ~mask);
708 extern struct kib_data kiblnd_data;
710 extern void kiblnd_hdev_destroy(struct kib_hca_dev *hdev);
712 int kiblnd_msg_queue_size(int version, struct lnet_ni *ni);
714 static inline int kiblnd_timeout(void)
716 return *kiblnd_tunables.kib_timeout ? *kiblnd_tunables.kib_timeout :
717 lnet_get_lnd_timeout();
721 kiblnd_concurrent_sends(int version, struct lnet_ni *ni)
723 struct lnet_ioctl_config_o2iblnd_tunables *tunables;
724 int concurrent_sends;
726 tunables = &ni->ni_lnd_tunables.lnd_tun_u.lnd_o2ib;
727 concurrent_sends = tunables->lnd_concurrent_sends;
729 if (version == IBLND_MSG_VERSION_1) {
730 if (concurrent_sends > IBLND_MSG_QUEUE_SIZE_V1 * 2)
731 return IBLND_MSG_QUEUE_SIZE_V1 * 2;
733 if (concurrent_sends < IBLND_MSG_QUEUE_SIZE_V1 / 2)
734 return IBLND_MSG_QUEUE_SIZE_V1 / 2;
737 return concurrent_sends;
741 kiblnd_hdev_addref_locked(struct kib_hca_dev *hdev)
743 LASSERT(atomic_read(&hdev->ibh_ref) > 0);
744 atomic_inc(&hdev->ibh_ref);
748 kiblnd_hdev_decref(struct kib_hca_dev *hdev)
750 LASSERT(atomic_read(&hdev->ibh_ref) > 0);
751 if (atomic_dec_and_test(&hdev->ibh_ref))
752 kiblnd_hdev_destroy(hdev);
756 kiblnd_dev_can_failover(struct kib_dev *dev)
758 if (!list_empty(&dev->ibd_fail_list)) /* already scheduled */
761 if (*kiblnd_tunables.kib_dev_failover == 0) /* disabled */
764 if (*kiblnd_tunables.kib_dev_failover > 1) /* force failover */
767 return dev->ibd_can_failover;
770 #define kiblnd_conn_addref(conn) \
772 CDEBUG(D_NET, "conn[%p] (%d)++\n", \
773 (conn), atomic_read(&(conn)->ibc_refcount)); \
774 atomic_inc(&(conn)->ibc_refcount); \
777 #define kiblnd_conn_decref(conn) \
779 unsigned long flags; \
781 CDEBUG(D_NET, "conn[%p] (%d)--\n", \
782 (conn), atomic_read(&(conn)->ibc_refcount)); \
783 LASSERT_ATOMIC_POS(&(conn)->ibc_refcount); \
784 if (atomic_dec_and_test(&(conn)->ibc_refcount)) { \
785 spin_lock_irqsave(&kiblnd_data.kib_connd_lock, flags); \
786 list_add_tail(&(conn)->ibc_list, \
787 &kiblnd_data.kib_connd_zombies); \
788 wake_up(&kiblnd_data.kib_connd_waitq); \
789 spin_unlock_irqrestore(&kiblnd_data.kib_connd_lock, flags);\
793 void kiblnd_destroy_peer(struct kref *kref);
795 static inline void kiblnd_peer_addref(struct kib_peer_ni *peer_ni)
797 CDEBUG(D_NET, "peer_ni[%p] -> %s (%d)++\n",
798 peer_ni, libcfs_nid2str(peer_ni->ibp_nid),
799 kref_read(&peer_ni->ibp_kref));
800 kref_get(&(peer_ni)->ibp_kref);
803 static inline void kiblnd_peer_decref(struct kib_peer_ni *peer_ni)
805 CDEBUG(D_NET, "peer_ni[%p] -> %s (%d)--\n",
806 peer_ni, libcfs_nid2str(peer_ni->ibp_nid),
807 kref_read(&peer_ni->ibp_kref));
808 kref_put(&peer_ni->ibp_kref, kiblnd_destroy_peer);
812 kiblnd_peer_connecting(struct kib_peer_ni *peer_ni)
814 return peer_ni->ibp_connecting != 0 ||
815 peer_ni->ibp_reconnecting != 0 ||
816 peer_ni->ibp_accepting != 0;
820 kiblnd_peer_idle(struct kib_peer_ni *peer_ni)
822 return !kiblnd_peer_connecting(peer_ni) && list_empty(&peer_ni->ibp_conns);
826 kiblnd_peer_active(struct kib_peer_ni *peer_ni)
828 /* Am I in the peer_ni hash table? */
829 return !hlist_unhashed(&peer_ni->ibp_list);
832 static inline struct kib_conn *
833 kiblnd_get_conn_locked(struct kib_peer_ni *peer_ni)
835 struct list_head *next;
837 LASSERT(!list_empty(&peer_ni->ibp_conns));
839 /* Advance to next connection, be sure to skip the head node */
840 if (!peer_ni->ibp_next_conn ||
841 peer_ni->ibp_next_conn->ibc_list.next == &peer_ni->ibp_conns)
842 next = peer_ni->ibp_conns.next;
844 next = peer_ni->ibp_next_conn->ibc_list.next;
845 peer_ni->ibp_next_conn = list_entry(next, struct kib_conn, ibc_list);
847 return peer_ni->ibp_next_conn;
851 kiblnd_send_keepalive(struct kib_conn *conn)
853 s64 keepalive_ns = *kiblnd_tunables.kib_keepalive * NSEC_PER_SEC;
855 return (*kiblnd_tunables.kib_keepalive > 0) &&
856 ktime_after(ktime_get(),
857 ktime_add_ns(conn->ibc_last_send, keepalive_ns));
861 kiblnd_need_noop(struct kib_conn *conn)
863 struct lnet_ni *ni = conn->ibc_peer->ibp_ni;
864 struct lnet_ioctl_config_o2iblnd_tunables *tunables;
866 LASSERT(conn->ibc_state >= IBLND_CONN_ESTABLISHED);
867 tunables = &ni->ni_lnd_tunables.lnd_tun_u.lnd_o2ib;
869 if (conn->ibc_outstanding_credits <
870 IBLND_CREDITS_HIGHWATER(tunables, conn) &&
871 !kiblnd_send_keepalive(conn))
872 return 0; /* No need to send NOOP */
874 if (IBLND_OOB_CAPABLE(conn->ibc_version)) {
875 if (!list_empty(&conn->ibc_tx_queue_nocred))
876 return 0; /* NOOP can be piggybacked */
878 /* No tx to piggyback NOOP onto or no credit to send a tx */
879 return (list_empty(&conn->ibc_tx_queue) ||
880 conn->ibc_credits == 0);
883 if (!list_empty(&conn->ibc_tx_noops) || /* NOOP already queued */
884 !list_empty(&conn->ibc_tx_queue_nocred) || /* piggyback NOOP */
885 conn->ibc_credits == 0) /* no credit */
888 if (conn->ibc_credits == 1 && /* last credit reserved for */
889 conn->ibc_outstanding_credits == 0) /* giving back credits */
892 /* No tx to piggyback NOOP onto or no credit to send a tx */
893 return (list_empty(&conn->ibc_tx_queue) || conn->ibc_credits == 1);
897 kiblnd_abort_receives(struct kib_conn *conn)
899 ib_modify_qp(conn->ibc_cmid->qp,
900 &kiblnd_data.kib_error_qpa, IB_QP_STATE);
903 static inline const char *
904 kiblnd_queue2str(struct kib_conn *conn, struct list_head *q)
906 if (q == &conn->ibc_tx_queue)
909 if (q == &conn->ibc_tx_queue_rsrvd)
910 return "tx_queue_rsrvd";
912 if (q == &conn->ibc_tx_queue_nocred)
913 return "tx_queue_nocred";
915 if (q == &conn->ibc_active_txs)
922 /* CAVEAT EMPTOR: We rely on descriptor alignment to allow us to use the
923 * lowest bits of the work request id to stash the work item type. */
925 #define IBLND_WID_INVAL 0
926 #define IBLND_WID_TX 1
927 #define IBLND_WID_RX 2
928 #define IBLND_WID_RDMA 3
929 #define IBLND_WID_MR 4
930 #define IBLND_WID_MASK 7UL
933 kiblnd_ptr2wreqid (void *ptr, int type)
935 unsigned long lptr = (unsigned long)ptr;
937 LASSERT ((lptr & IBLND_WID_MASK) == 0);
938 LASSERT ((type & ~IBLND_WID_MASK) == 0);
939 return (__u64)(lptr | type);
943 kiblnd_wreqid2ptr (__u64 wreqid)
945 return (void *)(((unsigned long)wreqid) & ~IBLND_WID_MASK);
949 kiblnd_wreqid2type (__u64 wreqid)
951 return (wreqid & IBLND_WID_MASK);
955 kiblnd_set_conn_state(struct kib_conn *conn, int state)
957 conn->ibc_state = state;
962 kiblnd_init_msg(struct kib_msg *msg, int type, int body_nob)
964 msg->ibm_type = type;
965 msg->ibm_nob = offsetof(struct kib_msg, ibm_u) + body_nob;
969 kiblnd_rd_size(struct kib_rdma_desc *rd)
974 for (i = size = 0; i < rd->rd_nfrags; i++)
975 size += rd->rd_frags[i].rf_nob;
981 kiblnd_rd_frag_addr(struct kib_rdma_desc *rd, int index)
983 return rd->rd_frags[index].rf_addr;
987 kiblnd_rd_frag_size(struct kib_rdma_desc *rd, int index)
989 return rd->rd_frags[index].rf_nob;
993 kiblnd_rd_frag_key(struct kib_rdma_desc *rd, int index)
999 kiblnd_rd_consume_frag(struct kib_rdma_desc *rd, int index, __u32 nob)
1001 if (nob < rd->rd_frags[index].rf_nob) {
1002 rd->rd_frags[index].rf_addr += nob;
1003 rd->rd_frags[index].rf_nob -= nob;
1012 kiblnd_rd_msg_size(struct kib_rdma_desc *rd, int msgtype, int n)
1014 LASSERT (msgtype == IBLND_MSG_GET_REQ ||
1015 msgtype == IBLND_MSG_PUT_ACK);
1017 return msgtype == IBLND_MSG_GET_REQ ?
1018 offsetof(struct kib_get_msg, ibgm_rd.rd_frags[n]) :
1019 offsetof(struct kib_putack_msg, ibpam_rd.rd_frags[n]);
1023 kiblnd_dma_mapping_error(struct ib_device *dev, u64 dma_addr)
1025 return ib_dma_mapping_error(dev, dma_addr);
1028 static inline __u64 kiblnd_dma_map_single(struct ib_device *dev,
1029 void *msg, size_t size,
1030 enum dma_data_direction direction)
1032 return ib_dma_map_single(dev, msg, size, direction);
1035 static inline void kiblnd_dma_unmap_single(struct ib_device *dev,
1036 __u64 addr, size_t size,
1037 enum dma_data_direction direction)
1039 ib_dma_unmap_single(dev, addr, size, direction);
1042 #define KIBLND_UNMAP_ADDR_SET(p, m, a) do {} while (0)
1043 #define KIBLND_UNMAP_ADDR(p, m, a) (a)
1046 int kiblnd_dma_map_sg(struct kib_hca_dev *hdev, struct kib_tx *tx)
1048 struct scatterlist *sg = tx->tx_frags;
1049 int nents = tx->tx_nfrags;
1050 enum dma_data_direction direction = tx->tx_dmadir;
1053 return lnet_rdma_map_sg_attrs(hdev->ibh_ibdev->dma_device,
1054 sg, nents, direction);
1056 return ib_dma_map_sg(hdev->ibh_ibdev, sg, nents, direction);
1060 void kiblnd_dma_unmap_sg(struct kib_hca_dev *hdev, struct kib_tx *tx)
1062 struct scatterlist *sg = tx->tx_frags;
1063 int nents = tx->tx_nfrags;
1064 enum dma_data_direction direction = tx->tx_dmadir;
1067 lnet_rdma_unmap_sg(hdev->ibh_ibdev->dma_device,
1068 sg, nents, direction);
1070 ib_dma_unmap_sg(hdev->ibh_ibdev, sg, nents, direction);
1073 #ifndef HAVE_IB_SG_DMA_ADDRESS
1074 #include <linux/scatterlist.h>
1075 #define ib_sg_dma_address(dev, sg) sg_dma_address(sg)
1076 #define ib_sg_dma_len(dev, sg) sg_dma_len(sg)
1079 static inline __u64 kiblnd_sg_dma_address(struct ib_device *dev,
1080 struct scatterlist *sg)
1082 return ib_sg_dma_address(dev, sg);
1085 static inline unsigned int kiblnd_sg_dma_len(struct ib_device *dev,
1086 struct scatterlist *sg)
1088 return ib_sg_dma_len(dev, sg);
1091 #ifndef HAVE_RDMA_CONNECT_LOCKED
1092 #define rdma_connect_locked(cmid, cpp) rdma_connect(cmid, cpp)
1095 /* XXX We use KIBLND_CONN_PARAM(e) as writable buffer, it's not strictly
1096 * right because OFED1.2 defines it as const, to use it we have to add
1097 * (void *) cast to overcome "const" */
1099 #define KIBLND_CONN_PARAM(e) ((e)->param.conn.private_data)
1100 #define KIBLND_CONN_PARAM_LEN(e) ((e)->param.conn.private_data_len)
1102 void kiblnd_abort_txs(struct kib_conn *conn, struct list_head *txs);
1103 void kiblnd_map_rx_descs(struct kib_conn *conn);
1104 void kiblnd_unmap_rx_descs(struct kib_conn *conn);
1105 void kiblnd_pool_free_node(struct kib_pool *pool, struct list_head *node);
1106 struct list_head *kiblnd_pool_alloc_node(struct kib_poolset *ps);
1108 int kiblnd_fmr_pool_map(struct kib_fmr_poolset *fps, struct kib_tx *tx,
1109 struct kib_rdma_desc *rd, u32 nob, u64 iov,
1110 struct kib_fmr *fmr);
1111 void kiblnd_fmr_pool_unmap(struct kib_fmr *fmr, int status);
1113 int kiblnd_tunables_setup(struct lnet_ni *ni);
1114 int kiblnd_tunables_init(void);
1116 int kiblnd_connd (void *arg);
1117 int kiblnd_scheduler(void *arg);
1118 #define kiblnd_thread_start(fn, data, namefmt, arg...) \
1120 struct task_struct *__task = kthread_run(fn, data, \
1122 if (!IS_ERR(__task)) \
1123 atomic_inc(&kiblnd_data.kib_nthreads); \
1124 PTR_ERR_OR_ZERO(__task); \
1127 int kiblnd_failover_thread (void *arg);
1129 int kiblnd_alloc_pages(struct kib_pages **pp, int cpt, int npages);
1131 int kiblnd_cm_callback(struct rdma_cm_id *cmid,
1132 struct rdma_cm_event *event);
1133 int kiblnd_translate_mtu(int value);
1135 int kiblnd_dev_failover(struct kib_dev *dev, struct net *ns);
1136 int kiblnd_create_peer(struct lnet_ni *ni, struct kib_peer_ni **peerp,
1138 bool kiblnd_reconnect_peer(struct kib_peer_ni *peer);
1139 void kiblnd_destroy_dev(struct kib_dev *dev);
1140 void kiblnd_unlink_peer_locked(struct kib_peer_ni *peer_ni);
1141 struct kib_peer_ni *kiblnd_find_peer_locked(struct lnet_ni *ni, lnet_nid_t nid);
1142 int kiblnd_close_stale_conns_locked(struct kib_peer_ni *peer_ni,
1143 int version, u64 incarnation);
1144 int kiblnd_close_peer_conns_locked(struct kib_peer_ni *peer_ni, int why);
1146 struct kib_conn *kiblnd_create_conn(struct kib_peer_ni *peer_ni,
1147 struct rdma_cm_id *cmid,
1148 int state, int version);
1149 void kiblnd_destroy_conn(struct kib_conn *conn);
1150 void kiblnd_close_conn(struct kib_conn *conn, int error);
1151 void kiblnd_close_conn_locked(struct kib_conn *conn, int error);
1153 void kiblnd_launch_tx(struct lnet_ni *ni, struct kib_tx *tx, lnet_nid_t nid);
1154 void kiblnd_txlist_done(struct list_head *txlist, int status,
1155 enum lnet_msg_hstatus hstatus);
1157 void kiblnd_qp_event(struct ib_event *event, void *arg);
1158 void kiblnd_cq_event(struct ib_event *event, void *arg);
1159 void kiblnd_cq_completion(struct ib_cq *cq, void *arg);
1161 void kiblnd_pack_msg(struct lnet_ni *ni, struct kib_msg *msg, int version,
1162 int credits, lnet_nid_t dstnid, __u64 dststamp);
1163 int kiblnd_unpack_msg(struct kib_msg *msg, int nob);
1164 int kiblnd_post_rx(struct kib_rx *rx, int credit);
1166 int kiblnd_send(struct lnet_ni *ni, void *private, struct lnet_msg *lntmsg);
1167 int kiblnd_recv(struct lnet_ni *ni, void *private, struct lnet_msg *lntmsg,
1168 int delayed, unsigned int niov,
1169 struct bio_vec *kiov, unsigned int offset, unsigned int mlen,
1171 unsigned int kiblnd_get_dev_prio(struct lnet_ni *ni, unsigned int dev_idx);