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/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lnet/klnds/o2iblnd/o2iblnd.h
34 * Author: Eric Barton <eric@bartonsoftware.com>
37 #include <linux/module.h>
38 #include <linux/kernel.h>
40 #if defined(NEED_LOCKDEP_IS_HELD_DISCARD_CONST) \
41 && defined(CONFIG_LOCKDEP) \
42 && defined(lockdep_is_held)
43 #undef lockdep_is_held
44 #define lockdep_is_held(lock) \
45 lock_is_held((struct lockdep_map *)&(lock)->dep_map)
48 #ifdef HAVE_COMPAT_RDMA
49 #include <linux/compat-2.6.h>
51 #ifdef LINUX_3_17_COMPAT_H
52 #undef NEED_KTIME_GET_REAL_NS
55 /* MOFED has its own bitmap_alloc backport */
56 #define HAVE_BITMAP_ALLOC 1
60 #include <linux/kthread.h>
62 #include <linux/string.h>
63 #include <linux/stat.h>
64 #include <linux/errno.h>
65 #include <linux/unistd.h>
66 #include <linux/uio.h>
68 #include <asm/uaccess.h>
71 #include <linux/init.h>
73 #include <linux/file.h>
74 #include <linux/stat.h>
75 #include <linux/list.h>
76 #include <linux/kmod.h>
77 #include <linux/sysctl.h>
78 #include <linux/pci.h>
83 #include <rdma/rdma_cm.h>
84 #include <rdma/ib_cm.h>
85 #include <rdma/ib_verbs.h>
86 #ifdef HAVE_FMR_POOL_API
87 #include <rdma/ib_fmr_pool.h>
90 #define DEBUG_SUBSYSTEM S_LND
92 #include <libcfs/libcfs.h>
93 #include <lnet/lib-lnet.h>
94 #include "o2iblnd-idl.h"
96 #define IBLND_PEER_HASH_SIZE 101 /* # peer_ni lists */
98 #define IBLND_N_SCHED 2
99 #define IBLND_N_SCHED_HIGH 4
101 struct kib_tunables {
102 int *kib_dev_failover; /* HCA failover */
103 unsigned int *kib_service; /* IB service number */
104 int *kib_cksum; /* checksum struct kib_msg? */
105 int *kib_timeout; /* comms timeout (seconds) */
106 int *kib_keepalive; /* keepalive timeout (seconds) */
107 char **kib_default_ipif; /* default IPoIB interface */
108 int *kib_retry_count;
109 int *kib_rnr_retry_count;
110 int *kib_ib_mtu; /* IB MTU */
111 int *kib_require_priv_port;/* accept only privileged ports */
112 int *kib_use_priv_port; /* use privileged port for active connect */
113 /* # threads on each CPT */
115 int *kib_wrq_sge; /* # sg elements per wrq */
116 int *kib_use_fastreg_gaps; /* enable discontiguous fastreg fragment support */
119 extern struct kib_tunables kiblnd_tunables;
121 #define IBLND_MSG_QUEUE_SIZE_V1 8 /* V1 only : # messages/RDMAs in-flight */
122 #define IBLND_CREDIT_HIGHWATER_V1 7 /* V1 only : when eagerly to return credits */
124 #define IBLND_CREDITS_DEFAULT 8 /* default # of peer_ni credits */
125 #define IBLND_CREDITS_MAX ((typeof(((struct kib_msg *) 0)->ibm_credits)) - 1) /* Max # of peer_ni credits */
127 /* when eagerly to return credits */
128 #define IBLND_CREDITS_HIGHWATER(t, conn) ((conn->ibc_version) == IBLND_MSG_VERSION_1 ? \
129 IBLND_CREDIT_HIGHWATER_V1 : \
130 min(t->lnd_peercredits_hiw, (__u32)conn->ibc_queue_depth - 1))
132 #ifdef HAVE_RDMA_CREATE_ID_5ARG
133 # define kiblnd_rdma_create_id(ns, cb, dev, ps, qpt) \
134 rdma_create_id((ns) ? (ns) : &init_net, cb, dev, ps, qpt)
136 # ifdef HAVE_RDMA_CREATE_ID_4ARG
137 # define kiblnd_rdma_create_id(ns, cb, dev, ps, qpt) \
138 rdma_create_id(cb, dev, ps, qpt)
140 # define kiblnd_rdma_create_id(ns, cb, dev, ps, qpt) \
141 rdma_create_id(cb, dev, ps)
145 /* 2 OOB shall suffice for 1 keepalive and 1 returning credits */
146 #define IBLND_OOB_CAPABLE(v) ((v) != IBLND_MSG_VERSION_1)
147 #define IBLND_OOB_MSGS(v) (IBLND_OOB_CAPABLE(v) ? 2 : 0)
149 #define IBLND_MSG_SIZE (4<<10) /* max size of queued messages (inc hdr) */
150 #define IBLND_MAX_RDMA_FRAGS LNET_MAX_IOV /* max # of fragments supported */
152 /************************/
153 /* derived constants... */
154 /* Pools (shared by connections on each CPT) */
155 /* These pools can grow at runtime, so don't need give a very large value */
156 #define IBLND_TX_POOL 256
157 #define IBLND_FMR_POOL 256
158 #define IBLND_FMR_POOL_FLUSH 192
160 /* RX messages (per connection) */
161 #define IBLND_RX_MSGS(c) \
162 ((c->ibc_queue_depth) * 2 + IBLND_OOB_MSGS(c->ibc_version))
163 #define IBLND_RX_MSG_BYTES(c) (IBLND_RX_MSGS(c) * IBLND_MSG_SIZE)
164 #define IBLND_RX_MSG_PAGES(c) \
165 ((IBLND_RX_MSG_BYTES(c) + PAGE_SIZE - 1) / PAGE_SIZE)
167 /* WRs and CQEs (per connection) */
168 #define IBLND_RECV_WRS(c) IBLND_RX_MSGS(c)
170 /* 2 = LNet msg + Transfer chain */
171 #define IBLND_CQ_ENTRIES(c) (IBLND_RECV_WRS(c) + kiblnd_send_wrs(c))
175 /* o2iblnd can run over aliased interface */
177 #define KIB_IFNAME_SIZE IFALIASZ
179 #define KIB_IFNAME_SIZE 256
183 IBLND_DEV_CAPS_FASTREG_ENABLED = BIT(0),
184 IBLND_DEV_CAPS_FASTREG_GAPS_SUPPORT = BIT(1),
185 #ifdef HAVE_FMR_POOL_API
186 IBLND_DEV_CAPS_FMR_ENABLED = BIT(2),
191 struct list_head ibd_list; /* chain on kib_devs */
192 struct list_head ibd_fail_list; /* chain on kib_failed_devs */
193 __u32 ibd_ifip; /* IPoIB interface IP */
194 /** IPoIB interface name */
195 char ibd_ifname[KIB_IFNAME_SIZE];
196 int ibd_nnets; /* # nets extant */
198 time64_t ibd_next_failover;
199 /* # failover failures */
200 int ibd_failed_failover;
201 /* failover in progress */
202 unsigned int ibd_failover;
203 /* IPoIB interface is a bonding master */
204 unsigned int ibd_can_failover;
205 struct list_head ibd_nets;
206 struct kib_hca_dev *ibd_hdev;
207 enum kib_dev_caps ibd_dev_caps;
211 struct rdma_cm_id *ibh_cmid; /* listener cmid */
212 struct ib_device *ibh_ibdev; /* IB device */
213 int ibh_page_shift; /* page shift of current HCA */
214 int ibh_page_size; /* page size of current HCA */
215 __u64 ibh_page_mask; /* page mask of current HCA */
216 __u64 ibh_mr_size; /* size of MR */
217 int ibh_max_qp_wr; /* maximum work requests size */
218 #ifdef HAVE_IB_GET_DMA_MR
219 struct ib_mr *ibh_mrs; /* global MR */
221 struct ib_pd *ibh_pd; /* PD */
222 u8 ibh_port; /* port number */
223 struct ib_event_handler
224 ibh_event_handler; /* IB event handler */
225 int ibh_state; /* device status */
226 #define IBLND_DEV_PORT_DOWN 0
227 #define IBLND_DEV_PORT_ACTIVE 1
228 #define IBLND_DEV_FATAL 2
229 struct kib_dev *ibh_dev; /* owner */
230 atomic_t ibh_ref; /* refcount */
233 /** # of seconds to keep pool alive */
234 #define IBLND_POOL_DEADLINE 300
235 /** # of seconds to retry if allocation failed */
236 #define IBLND_POOL_RETRY 1
239 int ibp_npages; /* # pages */
240 struct page *ibp_pages[0]; /* page array */
246 typedef int (*kib_ps_pool_create_t)(struct kib_poolset *ps,
247 int inc, struct kib_pool **pp_po);
248 typedef void (*kib_ps_pool_destroy_t)(struct kib_pool *po);
249 typedef void (*kib_ps_node_init_t)(struct kib_pool *po, struct list_head *node);
250 typedef void (*kib_ps_node_fini_t)(struct kib_pool *po, struct list_head *node);
254 #define IBLND_POOL_NAME_LEN 32
259 /* network it belongs to */
260 struct kib_net *ps_net;
262 char ps_name[IBLND_POOL_NAME_LEN];
264 struct list_head ps_pool_list;
265 /* failed pool list */
266 struct list_head ps_failed_pool_list;
267 /* time stamp for retry if failed to allocate */
268 time64_t ps_next_retry;
269 /* is allocating new pool */
276 /* create a new pool */
277 kib_ps_pool_create_t ps_pool_create;
279 kib_ps_pool_destroy_t ps_pool_destroy;
280 /* initialize new allocated node */
281 kib_ps_node_init_t ps_node_init;
283 kib_ps_node_fini_t ps_node_fini;
287 /* chain on pool list */
288 struct list_head po_list;
289 /* pre-allocated node */
290 struct list_head po_free_list;
291 /* pool_set of this pool */
292 struct kib_poolset *po_owner;
293 /* deadline of this pool */
294 time64_t po_deadline;
295 /* # of elements in use */
297 /* pool is created on failed HCA */
299 /* # of pre-allocated elements */
303 struct kib_tx_poolset {
304 struct kib_poolset tps_poolset; /* pool-set */
305 __u64 tps_next_tx_cookie; /* cookie of TX */
309 struct kib_pool tpo_pool; /* pool */
310 struct kib_hca_dev *tpo_hdev; /* device for this pool */
311 struct kib_tx *tpo_tx_descs; /* all the tx descriptors */
312 struct kib_pages *tpo_tx_pages; /* premapped tx msg pages */
315 struct kib_fmr_poolset {
316 spinlock_t fps_lock; /* serialize */
317 struct kib_net *fps_net; /* IB network */
318 struct list_head fps_pool_list; /* FMR pool list */
319 struct list_head fps_failed_pool_list; /* FMR pool list */
320 __u64 fps_version; /* validity stamp */
321 int fps_cpt; /* CPT id */
323 int fps_flush_trigger;
325 /* is allocating new pool */
327 /* time stamp for retry if failed to allocate */
328 time64_t fps_next_retry;
331 #ifndef HAVE_IB_RDMA_WR
333 struct ib_send_wr wr;
337 struct kib_fast_reg_descriptor { /* For fast registration */
338 struct list_head frd_list;
339 struct ib_rdma_wr frd_inv_wr;
340 #ifdef HAVE_IB_MAP_MR_SG
341 struct ib_reg_wr frd_fastreg_wr;
343 struct ib_rdma_wr frd_fastreg_wr;
344 struct ib_fast_reg_page_list *frd_frpl;
346 struct ib_mr *frd_mr;
350 struct kib_fmr_pool {
351 struct list_head fpo_list; /* chain on pool list */
352 struct kib_hca_dev *fpo_hdev; /* device for this pool */
353 struct kib_fmr_poolset *fpo_owner; /* owner of this pool */
354 #ifdef HAVE_FMR_POOL_API
357 struct ib_fmr_pool *fpo_fmr_pool; /* IB FMR pool */
360 struct { /* For fast registration */
361 struct list_head fpo_pool_list;
364 #ifdef HAVE_FMR_POOL_API
366 bool fpo_is_fmr; /* True if FMR pools allocated */
368 time64_t fpo_deadline; /* deadline of this pool */
369 int fpo_failed; /* fmr pool is failed */
370 int fpo_map_count; /* # of mapped FMR */
374 struct kib_fmr_pool *fmr_pool; /* pool of FMR */
375 #ifdef HAVE_FMR_POOL_API
376 struct ib_pool_fmr *fmr_pfmr; /* IB pool fmr */
377 #endif /* HAVE_FMR_POOL_API */
378 struct kib_fast_reg_descriptor *fmr_frd;
382 #ifdef HAVE_FMR_POOL_API
384 #ifdef HAVE_ORACLE_OFED_EXTENSIONS
385 #define kib_fmr_pool_map(pool, pgs, n, iov) \
386 ib_fmr_pool_map_phys((pool), (pgs), (n), (iov), NULL)
388 #define kib_fmr_pool_map(pool, pgs, n, iov) \
389 ib_fmr_pool_map_phys((pool), (pgs), (n), (iov))
392 #endif /* HAVE_FMR_POOL_API */
395 /* chain on struct kib_dev::ibd_nets */
396 struct list_head ibn_list;
397 __u64 ibn_incarnation;/* my epoch */
398 int ibn_init; /* initialisation state */
399 int ibn_shutdown; /* shutting down? */
401 atomic_t ibn_npeers; /* # peers extant */
402 atomic_t ibn_nconns; /* # connections extant */
404 struct kib_tx_poolset **ibn_tx_ps; /* tx pool-set */
405 struct kib_fmr_poolset **ibn_fmr_ps; /* fmr pool-set */
407 struct kib_dev *ibn_dev; /* underlying IB device */
408 struct lnet_ni *ibn_ni; /* LNet interface */
411 #define KIB_THREAD_SHIFT 16
412 #define KIB_THREAD_ID(cpt, tid) ((cpt) << KIB_THREAD_SHIFT | (tid))
413 #define KIB_THREAD_CPT(id) ((id) >> KIB_THREAD_SHIFT)
414 #define KIB_THREAD_TID(id) ((id) & ((1UL << KIB_THREAD_SHIFT) - 1))
416 struct kib_sched_info {
419 /* schedulers sleep here */
420 wait_queue_head_t ibs_waitq;
421 /* conns to check for rx completions */
422 struct list_head ibs_conns;
423 /* number of scheduler threads */
425 /* max allowed scheduler threads */
426 int ibs_nthreads_max;
427 int ibs_cpt; /* CPT id */
431 int kib_init; /* initialisation state */
432 int kib_shutdown; /* shut down? */
433 struct list_head kib_devs; /* IB devices extant */
434 /* list head of failed devices */
435 struct list_head kib_failed_devs;
436 /* schedulers sleep here */
437 wait_queue_head_t kib_failover_waitq;
438 atomic_t kib_nthreads; /* # live threads */
439 /* stabilize net/dev/peer_ni/conn ops */
440 rwlock_t kib_global_lock;
441 /* hash table of all my known peers */
442 struct list_head *kib_peers;
443 /* size of kib_peers */
444 int kib_peer_hash_size;
445 /* the connd task (serialisation assertions) */
447 /* connections to setup/teardown */
448 struct list_head kib_connd_conns;
449 /* connections with zero refcount */
450 struct list_head kib_connd_zombies;
451 /* connections to reconnect */
452 struct list_head kib_reconn_list;
453 /* peers wait for reconnection */
454 struct list_head kib_reconn_wait;
455 /* connections wait for completion */
456 struct list_head kib_connd_waits;
458 * The second that peers are pulled out from \a kib_reconn_wait
461 time64_t kib_reconn_sec;
462 /* connection daemon sleeps here */
463 wait_queue_head_t kib_connd_waitq;
464 spinlock_t kib_connd_lock; /* serialise */
465 struct ib_qp_attr kib_error_qpa; /* QP->ERROR */
466 /* percpt data for schedulers */
467 struct kib_sched_info **kib_scheds;
470 #define IBLND_INIT_NOTHING 0
471 #define IBLND_INIT_DATA 1
472 #define IBLND_INIT_ALL 2
474 struct kib_rx { /* receive message */
475 /* queue for attention */
476 struct list_head rx_list;
478 struct kib_conn *rx_conn;
479 /* # bytes received (-1 while posted) */
481 /* message buffer (host vaddr) */
482 struct kib_msg *rx_msg;
483 /* message buffer (I/O addr) */
485 /* for dma_unmap_single() */
486 DEFINE_DMA_UNMAP_ADDR(rx_msgunmap);
487 /* receive work item... */
488 struct ib_recv_wr rx_wrq;
489 /* ...and its memory */
490 struct ib_sge rx_sge;
493 #define IBLND_POSTRX_DONT_POST 0 /* don't post */
494 #define IBLND_POSTRX_NO_CREDIT 1 /* post: no credits */
495 #define IBLND_POSTRX_PEER_CREDIT 2 /* post: give peer_ni back 1 credit */
496 #define IBLND_POSTRX_RSRVD_CREDIT 3 /* post: give myself back 1 reserved credit */
498 struct kib_tx { /* transmit message */
499 /* queue on idle_txs ibc_tx_queue etc. */
500 struct list_head tx_list;
502 struct kib_tx_pool *tx_pool;
504 struct kib_conn *tx_conn;
505 /* # tx callbacks outstanding */
507 /* queued for sending */
509 /* waiting for peer_ni */
511 /* LNET completion status */
513 /* health status of the transmit */
514 enum lnet_msg_hstatus tx_hstatus;
515 /* completion deadline */
517 /* completion cookie */
519 /* lnet msgs to finalize on completion */
520 struct lnet_msg *tx_lntmsg[2];
521 /* message buffer (host vaddr) */
522 struct kib_msg *tx_msg;
523 /* message buffer (I/O addr) */
525 /* for dma_unmap_single() */
526 DEFINE_DMA_UNMAP_ADDR(tx_msgunmap);
527 /** sge for tx_msgaddr */
528 struct ib_sge tx_msgsge;
529 /* # send work items */
531 /* # used scatter/gather elements */
533 /* send work items... */
534 struct ib_rdma_wr *tx_wrq;
535 /* ...and their memory */
536 struct ib_sge *tx_sge;
537 /* rdma descriptor */
538 struct kib_rdma_desc *tx_rd;
539 /* # entries in... */
541 /* dma_map_sg descriptor */
542 struct scatterlist *tx_frags;
543 /* rdma phys page addrs */
545 /* gaps in fragments */
548 struct kib_fmr tx_fmr;
553 struct kib_connvars {
554 /* connection-in-progress variables */
555 struct kib_msg cv_msg;
559 /* scheduler information */
560 struct kib_sched_info *ibc_sched;
562 struct kib_peer_ni *ibc_peer;
564 struct kib_hca_dev *ibc_hdev;
565 /* stash on peer_ni's conn list */
566 struct list_head ibc_list;
567 /* schedule for attention */
568 struct list_head ibc_sched_list;
569 /* version of connection */
571 /* reconnect later */
572 __u16 ibc_reconnect:1;
573 /* which instance of the peer */
574 __u64 ibc_incarnation;
576 atomic_t ibc_refcount;
577 /* what's happening */
579 /* # uncompleted sends */
580 int ibc_nsends_posted;
581 /* # uncompleted NOOPs */
582 int ibc_noops_posted;
583 /* # credits I have */
585 /* # credits to return */
586 int ibc_outstanding_credits;
587 /* # ACK/DONE msg credits */
588 int ibc_reserved_credits;
589 /* set on comms error */
591 /* connections queue depth */
592 __u16 ibc_queue_depth;
593 /* connections max frags */
595 /* count of timeout txs waiting on cq */
597 /* receive buffers owned */
598 unsigned int ibc_nrx:16;
599 /* scheduled for attention */
600 unsigned int ibc_scheduled:1;
601 /* CQ callback fired */
602 unsigned int ibc_ready:1;
603 /* time of last send */
604 ktime_t ibc_last_send;
605 /** link chain for kiblnd_check_conns only */
606 struct list_head ibc_connd_list;
607 /** rxs completed before ESTABLISHED */
608 struct list_head ibc_early_rxs;
609 /** IBLND_MSG_NOOPs for IBLND_MSG_VERSION_1 */
610 struct list_head ibc_tx_noops;
611 /* sends that need a credit */
612 struct list_head ibc_tx_queue;
613 /* sends that don't need a credit */
614 struct list_head ibc_tx_queue_nocred;
615 /* sends that need to reserve an ACK/DONE msg */
616 struct list_head ibc_tx_queue_rsrvd;
617 /* active tx awaiting completion */
618 struct list_head ibc_active_txs;
619 /* zombie tx awaiting done */
620 struct list_head ibc_zombie_txs;
624 struct kib_rx *ibc_rxs;
625 /* premapped rx msg pages */
626 struct kib_pages *ibc_rx_pages;
629 struct rdma_cm_id *ibc_cmid;
630 /* completion queue */
631 struct ib_cq *ibc_cq;
633 /* in-progress connection state */
634 struct kib_connvars *ibc_connvars;
637 #define IBLND_CONN_INIT 0 /* being initialised */
638 #define IBLND_CONN_ACTIVE_CONNECT 1 /* active sending req */
639 #define IBLND_CONN_PASSIVE_WAIT 2 /* passive waiting for rtu */
640 #define IBLND_CONN_ESTABLISHED 3 /* connection established */
641 #define IBLND_CONN_CLOSING 4 /* being closed */
642 #define IBLND_CONN_DISCONNECTED 5 /* disconnected */
645 /* stash on global peer_ni list */
646 struct list_head ibp_list;
647 /* who's on the other end(s) */
650 struct lnet_ni *ibp_ni;
651 /* all active connections */
652 struct list_head ibp_conns;
653 /* next connection to send on for round robin */
654 struct kib_conn *ibp_next_conn;
655 /* msgs waiting for a conn */
656 struct list_head ibp_tx_queue;
657 /* incarnation of peer_ni */
658 __u64 ibp_incarnation;
659 /* when (in seconds) I was last alive */
660 time64_t ibp_last_alive;
662 atomic_t ibp_refcount;
663 /* version of peer_ni */
665 /* current passive connection attempts */
666 unsigned short ibp_accepting;
667 /* current active connection attempts */
668 unsigned short ibp_connecting;
669 /* reconnect this peer_ni later */
670 unsigned char ibp_reconnecting;
671 /* counter of how many times we triggered a conn race */
672 unsigned char ibp_races;
673 /* # consecutive reconnection attempts to this peer */
674 unsigned int ibp_reconnected;
675 /* number of total active retries */
676 unsigned int ibp_retries;
677 /* errno on closing this peer_ni */
679 /* max map_on_demand */
681 /* max_peer_credits */
682 __u16 ibp_queue_depth;
683 /* reduced value which allows conn to be created if max fails */
684 __u16 ibp_queue_depth_mod;
687 #ifndef HAVE_IB_INC_RKEY
689 * ib_inc_rkey - increments the key portion of the given rkey. Can be used
690 * for calculating a new rkey for type 2 memory windows.
691 * @rkey - the rkey to increment.
693 static inline u32 ib_inc_rkey(u32 rkey)
695 const u32 mask = 0x000000ff;
696 return ((rkey + 1) & mask) | (rkey & ~mask);
700 extern struct kib_data kiblnd_data;
702 extern void kiblnd_hdev_destroy(struct kib_hca_dev *hdev);
704 int kiblnd_msg_queue_size(int version, struct lnet_ni *ni);
706 static inline int kiblnd_timeout(void)
708 return *kiblnd_tunables.kib_timeout ? *kiblnd_tunables.kib_timeout :
709 lnet_get_lnd_timeout();
713 kiblnd_concurrent_sends(int version, struct lnet_ni *ni)
715 struct lnet_ioctl_config_o2iblnd_tunables *tunables;
716 int concurrent_sends;
718 tunables = &ni->ni_lnd_tunables.lnd_tun_u.lnd_o2ib;
719 concurrent_sends = tunables->lnd_concurrent_sends;
721 if (version == IBLND_MSG_VERSION_1) {
722 if (concurrent_sends > IBLND_MSG_QUEUE_SIZE_V1 * 2)
723 return IBLND_MSG_QUEUE_SIZE_V1 * 2;
725 if (concurrent_sends < IBLND_MSG_QUEUE_SIZE_V1 / 2)
726 return IBLND_MSG_QUEUE_SIZE_V1 / 2;
729 return concurrent_sends;
733 kiblnd_hdev_addref_locked(struct kib_hca_dev *hdev)
735 LASSERT(atomic_read(&hdev->ibh_ref) > 0);
736 atomic_inc(&hdev->ibh_ref);
740 kiblnd_hdev_decref(struct kib_hca_dev *hdev)
742 LASSERT(atomic_read(&hdev->ibh_ref) > 0);
743 if (atomic_dec_and_test(&hdev->ibh_ref))
744 kiblnd_hdev_destroy(hdev);
748 kiblnd_dev_can_failover(struct kib_dev *dev)
750 if (!list_empty(&dev->ibd_fail_list)) /* already scheduled */
753 if (*kiblnd_tunables.kib_dev_failover == 0) /* disabled */
756 if (*kiblnd_tunables.kib_dev_failover > 1) /* force failover */
759 return dev->ibd_can_failover;
762 #define kiblnd_conn_addref(conn) \
764 CDEBUG(D_NET, "conn[%p] (%d)++\n", \
765 (conn), atomic_read(&(conn)->ibc_refcount)); \
766 atomic_inc(&(conn)->ibc_refcount); \
769 #define kiblnd_conn_decref(conn) \
771 unsigned long flags; \
773 CDEBUG(D_NET, "conn[%p] (%d)--\n", \
774 (conn), atomic_read(&(conn)->ibc_refcount)); \
775 LASSERT_ATOMIC_POS(&(conn)->ibc_refcount); \
776 if (atomic_dec_and_test(&(conn)->ibc_refcount)) { \
777 spin_lock_irqsave(&kiblnd_data.kib_connd_lock, flags); \
778 list_add_tail(&(conn)->ibc_list, \
779 &kiblnd_data.kib_connd_zombies); \
780 wake_up(&kiblnd_data.kib_connd_waitq); \
781 spin_unlock_irqrestore(&kiblnd_data.kib_connd_lock, flags);\
785 #define kiblnd_peer_addref(peer_ni) \
787 CDEBUG(D_NET, "peer_ni[%p] -> %s (%d)++\n", \
788 (peer_ni), libcfs_nid2str((peer_ni)->ibp_nid), \
789 atomic_read (&(peer_ni)->ibp_refcount)); \
790 atomic_inc(&(peer_ni)->ibp_refcount); \
793 #define kiblnd_peer_decref(peer_ni) \
795 CDEBUG(D_NET, "peer_ni[%p] -> %s (%d)--\n", \
796 (peer_ni), libcfs_nid2str((peer_ni)->ibp_nid), \
797 atomic_read (&(peer_ni)->ibp_refcount)); \
798 LASSERT_ATOMIC_POS(&(peer_ni)->ibp_refcount); \
799 if (atomic_dec_and_test(&(peer_ni)->ibp_refcount)) \
800 kiblnd_destroy_peer(peer_ni); \
804 kiblnd_peer_connecting(struct kib_peer_ni *peer_ni)
806 return peer_ni->ibp_connecting != 0 ||
807 peer_ni->ibp_reconnecting != 0 ||
808 peer_ni->ibp_accepting != 0;
812 kiblnd_peer_idle(struct kib_peer_ni *peer_ni)
814 return !kiblnd_peer_connecting(peer_ni) && list_empty(&peer_ni->ibp_conns);
817 static inline struct list_head *
818 kiblnd_nid2peerlist (lnet_nid_t nid)
821 ((unsigned int)nid) % kiblnd_data.kib_peer_hash_size;
823 return &kiblnd_data.kib_peers[hash];
827 kiblnd_peer_active(struct kib_peer_ni *peer_ni)
829 /* Am I in the peer_ni hash table? */
830 return !list_empty(&peer_ni->ibp_list);
833 static inline struct kib_conn *
834 kiblnd_get_conn_locked(struct kib_peer_ni *peer_ni)
836 struct list_head *next;
838 LASSERT(!list_empty(&peer_ni->ibp_conns));
840 /* Advance to next connection, be sure to skip the head node */
841 if (!peer_ni->ibp_next_conn ||
842 peer_ni->ibp_next_conn->ibc_list.next == &peer_ni->ibp_conns)
843 next = peer_ni->ibp_conns.next;
845 next = peer_ni->ibp_next_conn->ibc_list.next;
846 peer_ni->ibp_next_conn = list_entry(next, struct kib_conn, ibc_list);
848 return peer_ni->ibp_next_conn;
852 kiblnd_send_keepalive(struct kib_conn *conn)
854 s64 keepalive_ns = *kiblnd_tunables.kib_keepalive * NSEC_PER_SEC;
856 return (*kiblnd_tunables.kib_keepalive > 0) &&
857 ktime_after(ktime_get(),
858 ktime_add_ns(conn->ibc_last_send, keepalive_ns));
862 kiblnd_need_noop(struct kib_conn *conn)
864 struct lnet_ni *ni = conn->ibc_peer->ibp_ni;
865 struct lnet_ioctl_config_o2iblnd_tunables *tunables;
867 LASSERT(conn->ibc_state >= IBLND_CONN_ESTABLISHED);
868 tunables = &ni->ni_lnd_tunables.lnd_tun_u.lnd_o2ib;
870 if (conn->ibc_outstanding_credits <
871 IBLND_CREDITS_HIGHWATER(tunables, conn) &&
872 !kiblnd_send_keepalive(conn))
873 return 0; /* No need to send NOOP */
875 if (IBLND_OOB_CAPABLE(conn->ibc_version)) {
876 if (!list_empty(&conn->ibc_tx_queue_nocred))
877 return 0; /* NOOP can be piggybacked */
879 /* No tx to piggyback NOOP onto or no credit to send a tx */
880 return (list_empty(&conn->ibc_tx_queue) ||
881 conn->ibc_credits == 0);
884 if (!list_empty(&conn->ibc_tx_noops) || /* NOOP already queued */
885 !list_empty(&conn->ibc_tx_queue_nocred) || /* piggyback NOOP */
886 conn->ibc_credits == 0) /* no credit */
889 if (conn->ibc_credits == 1 && /* last credit reserved for */
890 conn->ibc_outstanding_credits == 0) /* giving back credits */
893 /* No tx to piggyback NOOP onto or no credit to send a tx */
894 return (list_empty(&conn->ibc_tx_queue) || conn->ibc_credits == 1);
898 kiblnd_abort_receives(struct kib_conn *conn)
900 ib_modify_qp(conn->ibc_cmid->qp,
901 &kiblnd_data.kib_error_qpa, IB_QP_STATE);
904 static inline const char *
905 kiblnd_queue2str(struct kib_conn *conn, struct list_head *q)
907 if (q == &conn->ibc_tx_queue)
910 if (q == &conn->ibc_tx_queue_rsrvd)
911 return "tx_queue_rsrvd";
913 if (q == &conn->ibc_tx_queue_nocred)
914 return "tx_queue_nocred";
916 if (q == &conn->ibc_active_txs)
923 /* CAVEAT EMPTOR: We rely on descriptor alignment to allow us to use the
924 * lowest bits of the work request id to stash the work item type. */
926 #define IBLND_WID_INVAL 0
927 #define IBLND_WID_TX 1
928 #define IBLND_WID_RX 2
929 #define IBLND_WID_RDMA 3
930 #define IBLND_WID_MR 4
931 #define IBLND_WID_MASK 7UL
934 kiblnd_ptr2wreqid (void *ptr, int type)
936 unsigned long lptr = (unsigned long)ptr;
938 LASSERT ((lptr & IBLND_WID_MASK) == 0);
939 LASSERT ((type & ~IBLND_WID_MASK) == 0);
940 return (__u64)(lptr | type);
944 kiblnd_wreqid2ptr (__u64 wreqid)
946 return (void *)(((unsigned long)wreqid) & ~IBLND_WID_MASK);
950 kiblnd_wreqid2type (__u64 wreqid)
952 return (wreqid & IBLND_WID_MASK);
956 kiblnd_set_conn_state(struct kib_conn *conn, int state)
958 conn->ibc_state = state;
963 kiblnd_init_msg(struct kib_msg *msg, int type, int body_nob)
965 msg->ibm_type = type;
966 msg->ibm_nob = offsetof(struct kib_msg, ibm_u) + body_nob;
970 kiblnd_rd_size(struct kib_rdma_desc *rd)
975 for (i = size = 0; i < rd->rd_nfrags; i++)
976 size += rd->rd_frags[i].rf_nob;
982 kiblnd_rd_frag_addr(struct kib_rdma_desc *rd, int index)
984 return rd->rd_frags[index].rf_addr;
988 kiblnd_rd_frag_size(struct kib_rdma_desc *rd, int index)
990 return rd->rd_frags[index].rf_nob;
994 kiblnd_rd_frag_key(struct kib_rdma_desc *rd, int index)
1000 kiblnd_rd_consume_frag(struct kib_rdma_desc *rd, int index, __u32 nob)
1002 if (nob < rd->rd_frags[index].rf_nob) {
1003 rd->rd_frags[index].rf_addr += nob;
1004 rd->rd_frags[index].rf_nob -= nob;
1013 kiblnd_rd_msg_size(struct kib_rdma_desc *rd, int msgtype, int n)
1015 LASSERT (msgtype == IBLND_MSG_GET_REQ ||
1016 msgtype == IBLND_MSG_PUT_ACK);
1018 return msgtype == IBLND_MSG_GET_REQ ?
1019 offsetof(struct kib_get_msg, ibgm_rd.rd_frags[n]) :
1020 offsetof(struct kib_putack_msg, ibpam_rd.rd_frags[n]);
1024 kiblnd_dma_mapping_error(struct ib_device *dev, u64 dma_addr)
1026 return ib_dma_mapping_error(dev, dma_addr);
1029 static inline __u64 kiblnd_dma_map_single(struct ib_device *dev,
1030 void *msg, size_t size,
1031 enum dma_data_direction direction)
1033 return ib_dma_map_single(dev, msg, size, direction);
1036 static inline void kiblnd_dma_unmap_single(struct ib_device *dev,
1037 __u64 addr, size_t size,
1038 enum dma_data_direction direction)
1040 ib_dma_unmap_single(dev, addr, size, direction);
1043 #define KIBLND_UNMAP_ADDR_SET(p, m, a) do {} while (0)
1044 #define KIBLND_UNMAP_ADDR(p, m, a) (a)
1046 static inline int kiblnd_dma_map_sg(struct ib_device *dev,
1047 struct scatterlist *sg, int nents,
1048 enum dma_data_direction direction)
1050 return ib_dma_map_sg(dev, sg, nents, direction);
1053 static inline void kiblnd_dma_unmap_sg(struct ib_device *dev,
1054 struct scatterlist *sg, int nents,
1055 enum dma_data_direction direction)
1057 ib_dma_unmap_sg(dev, sg, nents, direction);
1060 #ifndef HAVE_IB_SG_DMA_ADDRESS
1061 #include <linux/scatterlist.h>
1062 #define ib_sg_dma_address(dev, sg) sg_dma_address(sg)
1063 #define ib_sg_dma_len(dev, sg) sg_dma_len(sg)
1066 static inline __u64 kiblnd_sg_dma_address(struct ib_device *dev,
1067 struct scatterlist *sg)
1069 return ib_sg_dma_address(dev, sg);
1072 static inline unsigned int kiblnd_sg_dma_len(struct ib_device *dev,
1073 struct scatterlist *sg)
1075 return ib_sg_dma_len(dev, sg);
1078 /* XXX We use KIBLND_CONN_PARAM(e) as writable buffer, it's not strictly
1079 * right because OFED1.2 defines it as const, to use it we have to add
1080 * (void *) cast to overcome "const" */
1082 #define KIBLND_CONN_PARAM(e) ((e)->param.conn.private_data)
1083 #define KIBLND_CONN_PARAM_LEN(e) ((e)->param.conn.private_data_len)
1085 void kiblnd_abort_txs(struct kib_conn *conn, struct list_head *txs);
1086 void kiblnd_map_rx_descs(struct kib_conn *conn);
1087 void kiblnd_unmap_rx_descs(struct kib_conn *conn);
1088 void kiblnd_pool_free_node(struct kib_pool *pool, struct list_head *node);
1089 struct list_head *kiblnd_pool_alloc_node(struct kib_poolset *ps);
1091 int kiblnd_fmr_pool_map(struct kib_fmr_poolset *fps, struct kib_tx *tx,
1092 struct kib_rdma_desc *rd, u32 nob, u64 iov,
1093 struct kib_fmr *fmr);
1094 void kiblnd_fmr_pool_unmap(struct kib_fmr *fmr, int status);
1096 int kiblnd_tunables_setup(struct lnet_ni *ni);
1097 int kiblnd_tunables_init(void);
1099 int kiblnd_connd (void *arg);
1100 int kiblnd_scheduler(void *arg);
1101 int kiblnd_thread_start(int (*fn)(void *arg), void *arg, char *name);
1102 int kiblnd_failover_thread (void *arg);
1104 int kiblnd_alloc_pages(struct kib_pages **pp, int cpt, int npages);
1106 int kiblnd_cm_callback(struct rdma_cm_id *cmid,
1107 struct rdma_cm_event *event);
1108 int kiblnd_translate_mtu(int value);
1110 int kiblnd_dev_failover(struct kib_dev *dev, struct net *ns);
1111 int kiblnd_create_peer(struct lnet_ni *ni, struct kib_peer_ni **peerp,
1113 void kiblnd_destroy_peer(struct kib_peer_ni *peer);
1114 bool kiblnd_reconnect_peer(struct kib_peer_ni *peer);
1115 void kiblnd_destroy_dev(struct kib_dev *dev);
1116 void kiblnd_unlink_peer_locked(struct kib_peer_ni *peer_ni);
1117 struct kib_peer_ni *kiblnd_find_peer_locked(struct lnet_ni *ni, lnet_nid_t nid);
1118 int kiblnd_close_stale_conns_locked(struct kib_peer_ni *peer_ni,
1119 int version, u64 incarnation);
1120 int kiblnd_close_peer_conns_locked(struct kib_peer_ni *peer_ni, int why);
1122 struct kib_conn *kiblnd_create_conn(struct kib_peer_ni *peer_ni,
1123 struct rdma_cm_id *cmid,
1124 int state, int version);
1125 void kiblnd_destroy_conn(struct kib_conn *conn);
1126 void kiblnd_close_conn(struct kib_conn *conn, int error);
1127 void kiblnd_close_conn_locked(struct kib_conn *conn, int error);
1129 void kiblnd_launch_tx(struct lnet_ni *ni, struct kib_tx *tx, lnet_nid_t nid);
1130 void kiblnd_txlist_done(struct list_head *txlist, int status,
1131 enum lnet_msg_hstatus hstatus);
1133 void kiblnd_qp_event(struct ib_event *event, void *arg);
1134 void kiblnd_cq_event(struct ib_event *event, void *arg);
1135 void kiblnd_cq_completion(struct ib_cq *cq, void *arg);
1137 void kiblnd_pack_msg(struct lnet_ni *ni, struct kib_msg *msg, int version,
1138 int credits, lnet_nid_t dstnid, __u64 dststamp);
1139 int kiblnd_unpack_msg(struct kib_msg *msg, int nob);
1140 int kiblnd_post_rx(struct kib_rx *rx, int credit);
1142 int kiblnd_send(struct lnet_ni *ni, void *private, struct lnet_msg *lntmsg);
1143 int kiblnd_recv(struct lnet_ni *ni, void *private, struct lnet_msg *lntmsg,
1144 int delayed, unsigned int niov,
1145 struct bio_vec *kiov, unsigned int offset, unsigned int mlen,