1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2004 Cluster File Systems, Inc.
5 * Author: Eric Barton <eric@bartonsoftware.com>
7 * This file is part of Lustre, http://www.lustre.org.
9 * Lustre is free software; you can redistribute it and/or
10 * modify it under the terms of version 2 of the GNU General Public
11 * License as published by the Free Software Foundation.
13 * Lustre is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Lustre; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 # define EXPORT_SYMTAB
28 #include <linux/config.h>
29 #include <linux/module.h>
30 #include <linux/kernel.h>
32 #include <linux/string.h>
33 #include <linux/stat.h>
34 #include <linux/errno.h>
35 #include <linux/smp_lock.h>
36 #include <linux/unistd.h>
37 #include <linux/uio.h>
39 #include <asm/system.h>
40 #include <asm/uaccess.h>
43 #include <linux/init.h>
45 #include <linux/file.h>
46 #include <linux/stat.h>
47 #include <linux/list.h>
48 #include <linux/kmod.h>
49 #include <linux/sysctl.h>
51 #define DEBUG_SUBSYSTEM S_OPENIBNAL
53 #include <linux/kp30.h>
54 #include <portals/p30.h>
55 #include <portals/lib-p30.h>
56 #include <portals/nal.h>
58 #include <ts_ib_core.h>
60 #include <ts_ib_sa_client.h>
62 #define OPENIBNAL_SERVICE_NAME "openibnal"
65 # define OPENIBNAL_N_SCHED num_online_cpus() /* # schedulers */
67 # define OPENIBNAL_N_SCHED 1 /* # schedulers */
70 #define OPENIBNAL_MIN_RECONNECT_INTERVAL HZ /* first failed connection retry... */
71 #define OPENIBNAL_MAX_RECONNECT_INTERVAL (60*HZ) /* ...exponentially increasing to this */
73 #define OPENIBNAL_MSG_SIZE (4<<10) /* max size of queued messages (inc hdr) */
75 #define OPENIBNAL_MSG_QUEUE_SIZE 8 /* # messages in-flight */
76 #define OPENIBNAL_CREDIT_HIGHWATER 6 /* when to eagerly return credits */
77 #define OPENIBNAL_RETRY 7 /* # times to retry */
78 #define OPENIBNAL_RNR_RETRY 7 /* */
79 #define OPENIBNAL_CM_RETRY 7 /* # times to retry connection */
80 #define OPENIBNAL_FLOW_CONTROL 1
81 #define OPENIBNAL_RESPONDER_RESOURCES 8
83 #define OPENIBNAL_NTX 64 /* # tx descs */
84 #define OPENIBNAL_NTX_NBLK 256 /* # reserved tx descs */
86 #define OPENIBNAL_PEER_HASH_SIZE 101 /* # peer lists */
88 #define OPENIBNAL_RESCHED 100 /* # scheduler loops before reschedule */
90 #define OPENIBNAL_CONCURRENT_PEERS 1000 /* # nodes all talking at once to me */
92 /* default vals for runtime tunables */
93 #define OPENIBNAL_IO_TIMEOUT 50 /* default comms timeout (seconds) */
95 /************************/
96 /* derived constants... */
98 /* TX messages (shared by all connections) */
99 #define OPENIBNAL_TX_MSGS (OPENIBNAL_NTX + OPENIBNAL_NTX_NBLK)
100 #define OPENIBNAL_TX_MSG_BYTES (OPENIBNAL_TX_MSGS * OPENIBNAL_MSG_SIZE)
101 #define OPENIBNAL_TX_MSG_PAGES ((OPENIBNAL_TX_MSG_BYTES + PAGE_SIZE - 1)/PAGE_SIZE)
103 /* we may have up to 2 completions per transmit */
104 #define OPENIBNAL_TX_CQ_ENTRIES (2*OPENIBNAL_TX_MSGS)
106 /* RX messages (per connection) */
107 #define OPENIBNAL_RX_MSGS OPENIBNAL_MSG_QUEUE_SIZE
108 #define OPENIBNAL_RX_MSG_BYTES (OPENIBNAL_RX_MSGS * OPENIBNAL_MSG_SIZE)
109 #define OPENIBNAL_RX_MSG_PAGES ((OPENIBNAL_RX_MSG_BYTES + PAGE_SIZE - 1)/PAGE_SIZE)
111 /* 1 completion per receive, per connection */
112 #define OPENIBNAL_RX_CQ_ENTRIES (OPENIBNAL_RX_MSGS * OPENIBNAL_CONCURRENT_PEERS)
114 #define OPENIBNAL_RDMA_BASE 0x0eeb0000
115 #define OPENIBNAL_FMR 1
116 #define OPENIBNAL_CKSUM 0
117 //#define OPENIBNAL_CALLBACK_CTXT IB_CQ_CALLBACK_PROCESS
118 #define OPENIBNAL_CALLBACK_CTXT IB_CQ_CALLBACK_INTERRUPT
122 int koib_io_timeout; /* comms timeout (seconds) */
123 struct ctl_table_header *koib_sysctl; /* sysctl interface */
128 int oibp_npages; /* # pages */
129 int oibp_mapped; /* mapped? */
130 __u64 oibp_vaddr; /* mapped region vaddr */
131 __u32 oibp_lkey; /* mapped region lkey */
132 __u32 oibp_rkey; /* mapped region rkey */
133 struct ib_mr *oibp_handle; /* mapped region handle */
134 struct page *oibp_pages[0];
139 int koib_init; /* initialisation state */
140 __u64 koib_incarnation; /* which one am I */
141 int koib_shutdown; /* shut down? */
142 atomic_t koib_nthreads; /* # live threads */
144 __u64 koib_cm_service_id; /* service number I listen on */
145 ptl_nid_t koib_nid; /* my NID */
146 struct semaphore koib_nid_mutex; /* serialise NID ops */
147 struct semaphore koib_nid_signal; /* signal completion */
149 rwlock_t koib_global_lock; /* stabilize peer/conn ops */
151 struct list_head *koib_peers; /* hash table of all my known peers */
152 int koib_peer_hash_size; /* size of koib_peers */
153 atomic_t koib_npeers; /* # peers extant */
154 atomic_t koib_nconns; /* # connections extant */
156 struct list_head koib_connd_conns; /* connections to progress */
157 struct list_head koib_connd_peers; /* peers waiting for a connection */
158 wait_queue_head_t koib_connd_waitq; /* connection daemons sleep here */
159 unsigned long koib_connd_waketime; /* when connd will wake */
160 spinlock_t koib_connd_lock; /* serialise */
162 wait_queue_head_t koib_sched_waitq; /* schedulers sleep here */
163 struct list_head koib_sched_txq; /* tx requiring attention */
164 struct list_head koib_sched_rxq; /* rx requiring attention */
165 spinlock_t koib_sched_lock; /* serialise */
167 struct koib_tx *koib_tx_descs; /* all the tx descriptors */
168 koib_pages_t *koib_tx_pages; /* premapped tx msg pages */
170 struct list_head koib_idle_txs; /* idle tx descriptors */
171 struct list_head koib_idle_nblk_txs; /* idle reserved tx descriptors */
172 wait_queue_head_t koib_idle_tx_waitq; /* block here for tx descriptor */
173 __u64 koib_next_tx_cookie; /* RDMA completion cookie */
174 spinlock_t koib_tx_lock; /* serialise */
176 struct ib_device *koib_device; /* "the" device */
177 struct ib_device_properties koib_device_props; /* its properties */
178 int koib_port; /* port on the device */
179 struct ib_port_properties koib_port_props; /* its properties */
180 struct ib_pd *koib_pd; /* protection domain */
182 struct ib_fmr_pool *koib_fmr_pool; /* fast memory region pool */
184 struct ib_cq *koib_rx_cq; /* receive completion queue */
185 struct ib_cq *koib_tx_cq; /* transmit completion queue */
186 void *koib_listen_handle; /* where I listen for connections */
187 struct ib_common_attrib_service koib_service; /* SM service */
191 #define OPENIBNAL_INIT_NOTHING 0
192 #define OPENIBNAL_INIT_DATA 1
193 #define OPENIBNAL_INIT_LIB 2
194 #define OPENIBNAL_INIT_PD 3
195 #define OPENIBNAL_INIT_FMR 4
196 #define OPENIBNAL_INIT_TXD 5
197 #define OPENIBNAL_INIT_RX_CQ 6
198 #define OPENIBNAL_INIT_TX_CQ 7
199 #define OPENIBNAL_INIT_ALL 8
201 /************************************************************************
202 * Wire message structs.
203 * These are sent in sender's byte order (i.e. receiver flips).
204 * CAVEAT EMPTOR: other structs communicated between nodes (e.g. MAD
205 * private data and SM service info), is LE on the wire.
221 __u32 rd_key; /* remote key */
222 __u32 rd_nob; /* # of bytes */
223 __u64 rd_addr; /* remote io vaddr */
229 ptl_hdr_t oibim_hdr; /* portals header */
230 char oibim_payload[0]; /* piggy-backed payload */
231 } koib_immediate_msg_t;
235 ptl_hdr_t oibrm_hdr; /* portals header */
236 __u64 oibrm_cookie; /* opaque completion cookie */
237 koib_rdma_desc_t oibrm_desc; /* where to suck/blow */
242 __u64 oibcm_cookie; /* opaque completion cookie */
243 __u32 oibcm_status; /* completion status */
244 } koib_completion_msg_t;
248 __u32 oibm_magic; /* I'm an openibnal message */
249 __u16 oibm_version; /* this is my version number */
250 __u8 oibm_type; /* msg type */
251 __u8 oibm_credits; /* returned credits */
257 koib_immediate_msg_t immediate;
258 koib_rdma_msg_t rdma;
259 koib_completion_msg_t completion;
263 #define OPENIBNAL_MSG_MAGIC 0x0be91b91 /* unique magic */
264 #define OPENIBNAL_MSG_VERSION 1 /* current protocol version */
266 #define OPENIBNAL_MSG_NOOP 0xd0 /* nothing (just credits) */
267 #define OPENIBNAL_MSG_IMMEDIATE 0xd1 /* portals hdr + payload */
268 #define OPENIBNAL_MSG_PUT_RDMA 0xd2 /* portals PUT hdr + source rdma desc */
269 #define OPENIBNAL_MSG_PUT_DONE 0xd3 /* signal PUT rdma completion */
270 #define OPENIBNAL_MSG_GET_RDMA 0xd4 /* portals GET hdr + sink rdma desc */
271 #define OPENIBNAL_MSG_GET_DONE 0xd5 /* signal GET rdma completion */
273 /***********************************************************************/
275 typedef struct koib_rx /* receive message */
277 struct list_head rx_list; /* queue for attention */
278 struct koib_conn *rx_conn; /* owning conn */
279 int rx_rdma; /* RDMA completion posted? */
280 int rx_posted; /* posted? */
281 __u64 rx_vaddr; /* pre-mapped buffer (hca vaddr) */
282 koib_msg_t *rx_msg; /* pre-mapped buffer (host vaddr) */
283 struct ib_receive_param rx_sp; /* receive work item */
284 struct ib_gather_scatter rx_gl; /* and it's memory */
287 typedef struct koib_tx /* transmit message */
289 struct list_head tx_list; /* queue on idle_txs ibc_tx_queue etc. */
290 int tx_isnblk; /* I'm reserved for non-blocking sends */
291 struct koib_conn *tx_conn; /* owning conn */
292 int tx_mapped; /* mapped for RDMA? */
293 int tx_sending; /* # tx callbacks outstanding */
294 int tx_status; /* completion status */
295 int tx_passive_rdma; /* waiting for peer to RDMA? */
296 int tx_passive_rdma_wait; /* on ibc_rdma_queue */
297 unsigned long tx_passive_rdma_deadline; /* completion deadline */
298 __u64 tx_passive_rdma_cookie; /* completion cookie */
299 lib_msg_t *tx_libmsg[2]; /* lib msgs to finalize on completion */
300 koib_md_t tx_md; /* RDMA mapping (active/passive) */
301 __u64 tx_vaddr; /* pre-mapped buffer (hca vaddr) */
302 koib_msg_t *tx_msg; /* pre-mapped buffer (host vaddr) */
303 int tx_nsp; /* # send work items */
304 struct ib_send_param tx_sp[2]; /* send work items... */
305 struct ib_gather_scatter tx_gl[2]; /* ...and their memory */
308 #define KOIB_TX_UNMAPPED 0
309 #define KOIB_TX_MAPPED 1
310 #define KOIB_TX_MAPPED_FMR 2
312 typedef struct koib_wire_connreq
314 __u32 wcr_magic; /* I'm an openibnal connreq */
315 __u16 wcr_version; /* this is my version number */
316 __u16 wcr_queue_depth; /* this is my receive queue size */
317 __u64 wcr_nid; /* peer's NID */
318 __u64 wcr_incarnation; /* peer's incarnation */
319 } koib_wire_connreq_t;
321 typedef struct koib_connreq
323 /* connection-in-progress */
324 struct koib_conn *cr_conn;
325 koib_wire_connreq_t cr_wcr;
327 struct ib_common_attrib_service cr_service;
329 struct ib_path_record cr_path;
330 struct ib_cm_active_param cr_connparam;
333 typedef struct koib_conn
335 struct koib_peer *ibc_peer; /* owning peer */
336 struct list_head ibc_list; /* stash on peer's conn list */
337 __u64 ibc_incarnation; /* which instance of the peer */
338 atomic_t ibc_refcount; /* # users */
339 int ibc_state; /* what's happening */
340 atomic_t ibc_nob; /* # bytes buffered */
341 int ibc_nsends_posted; /* # uncompleted sends */
342 int ibc_credits; /* # credits I have */
343 int ibc_outstanding_credits; /* # credits to return */
344 struct list_head ibc_tx_queue; /* send queue */
345 struct list_head ibc_rdma_queue; /* tx awaiting RDMA completion */
346 spinlock_t ibc_lock; /* serialise */
347 koib_rx_t *ibc_rxs; /* the rx descs */
348 koib_pages_t *ibc_rx_pages; /* premapped rx msg pages */
349 struct ib_qp *ibc_qp; /* queue pair */
350 __u32 ibc_qpn; /* queue pair number */
351 tTS_IB_CM_COMM_ID ibc_comm_id; /* connection ID? */
352 koib_connreq_t *ibc_connreq; /* connection request state */
355 #define OPENIBNAL_CONN_INIT_NOTHING 0 /* initial state */
356 #define OPENIBNAL_CONN_INIT_QP 1 /* ibc_qp set up */
357 #define OPENIBNAL_CONN_CONNECTING 2 /* started to connect */
358 #define OPENIBNAL_CONN_ESTABLISHED 3 /* connection established */
359 #define OPENIBNAL_CONN_DEATHROW 4 /* waiting to be closed */
360 #define OPENIBNAL_CONN_ZOMBIE 5 /* waiting to be freed */
362 typedef struct koib_peer
364 struct list_head ibp_list; /* stash on global peer list */
365 struct list_head ibp_connd_list; /* schedule on koib_connd_peers */
366 ptl_nid_t ibp_nid; /* who's on the other end(s) */
367 atomic_t ibp_refcount; /* # users */
368 int ibp_persistence; /* "known" peer refs */
369 struct list_head ibp_conns; /* all active connections */
370 struct list_head ibp_tx_queue; /* msgs waiting for a conn */
371 int ibp_connecting; /* connecting+accepting */
372 unsigned long ibp_reconnect_time; /* when reconnect may be attempted */
373 unsigned long ibp_reconnect_interval; /* exponential backoff */
377 extern lib_nal_t koibnal_lib;
378 extern koib_data_t koibnal_data;
379 extern koib_tunables_t koibnal_tunables;
381 static inline struct list_head *
382 koibnal_nid2peerlist (ptl_nid_t nid)
384 unsigned int hash = ((unsigned int)nid) % koibnal_data.koib_peer_hash_size;
386 return (&koibnal_data.koib_peers [hash]);
390 koibnal_peer_active(koib_peer_t *peer)
392 /* Am I in the peer hash table? */
393 return (!list_empty(&peer->ibp_list));
397 koibnal_queue_tx_locked (koib_tx_t *tx, koib_conn_t *conn)
399 /* CAVEAT EMPTOR: tx takes caller's ref on conn */
401 LASSERT (tx->tx_nsp > 0); /* work items set up */
402 LASSERT (tx->tx_conn == NULL); /* only set here */
405 list_add_tail(&tx->tx_list, &conn->ibc_tx_queue);
408 #define KOIBNAL_SERVICE_KEY_MASK (IB_SA_SERVICE_COMP_MASK_NAME | \
409 IB_SA_SERVICE_COMP_MASK_DATA8_1 | \
410 IB_SA_SERVICE_COMP_MASK_DATA8_2 | \
411 IB_SA_SERVICE_COMP_MASK_DATA8_3 | \
412 IB_SA_SERVICE_COMP_MASK_DATA8_4 | \
413 IB_SA_SERVICE_COMP_MASK_DATA8_5 | \
414 IB_SA_SERVICE_COMP_MASK_DATA8_6 | \
415 IB_SA_SERVICE_COMP_MASK_DATA8_7 | \
416 IB_SA_SERVICE_COMP_MASK_DATA8_8)
419 koibnal_service_nid_field(struct ib_common_attrib_service *srv)
421 /* must be consistent with KOIBNAL_SERVICE_KEY_MASK */
422 return (__u64 *)srv->service_data8;
427 koibnal_set_service_keys(struct ib_common_attrib_service *srv, ptl_nid_t nid)
429 LASSERT (strlen (OPENIBNAL_SERVICE_NAME) < sizeof(srv->service_name));
430 memset (srv->service_name, 0, sizeof(srv->service_name));
431 strcpy (srv->service_name, OPENIBNAL_SERVICE_NAME);
433 *koibnal_service_nid_field(srv) = cpu_to_le64(nid);
438 koibnal_show_rdma_attr (koib_conn_t *conn)
440 struct ib_qp_attribute qp_attr;
443 memset (&qp_attr, 0, sizeof(qp_attr));
444 rc = ib_qp_query(conn->ibc_qp, &qp_attr);
446 CERROR ("Can't get qp attrs: %d\n", rc);
450 CWARN ("RDMA CAPABILITY: write %s read %s\n",
451 (qp_attr.valid_fields & TS_IB_QP_ATTRIBUTE_RDMA_ATOMIC_ENABLE) ?
452 (qp_attr.enable_rdma_write ? "enabled" : "disabled") : "invalid",
453 (qp_attr.valid_fields & TS_IB_QP_ATTRIBUTE_RDMA_ATOMIC_ENABLE) ?
454 (qp_attr.enable_rdma_read ? "enabled" : "disabled") : "invalid");
460 koibnal_page2phys (struct page *p)
462 __u64 page_number = p - mem_map;
464 return (page_number << PAGE_SHIFT);
467 # error "no page->phys"
470 extern koib_peer_t *koibnal_create_peer (ptl_nid_t nid);
471 extern void koibnal_put_peer (koib_peer_t *peer);
472 extern int koibnal_del_peer (ptl_nid_t nid, int single_share);
473 extern koib_peer_t *koibnal_find_peer_locked (ptl_nid_t nid);
474 extern void koibnal_unlink_peer_locked (koib_peer_t *peer);
475 extern int koibnal_close_stale_conns_locked (koib_peer_t *peer,
477 extern koib_conn_t *koibnal_create_conn (void);
478 extern void koibnal_put_conn (koib_conn_t *conn);
479 extern void koibnal_destroy_conn (koib_conn_t *conn);
480 extern int koibnal_alloc_pages (koib_pages_t **pp, int npages, int access);
481 extern void koibnal_free_pages (koib_pages_t *p);
483 extern void koibnal_check_sends (koib_conn_t *conn);
485 extern tTS_IB_CM_CALLBACK_RETURN
486 koibnal_conn_callback (tTS_IB_CM_EVENT event, tTS_IB_CM_COMM_ID cid,
487 void *param, void *arg);
488 extern tTS_IB_CM_CALLBACK_RETURN
489 koibnal_passive_conn_callback (tTS_IB_CM_EVENT event, tTS_IB_CM_COMM_ID cid,
490 void *param, void *arg);
492 extern void koibnal_close_conn_locked (koib_conn_t *conn, int error);
493 extern void koibnal_destroy_conn (koib_conn_t *conn);
494 extern int koibnal_thread_start (int (*fn)(void *arg), void *arg);
495 extern int koibnal_scheduler(void *arg);
496 extern int koibnal_connd (void *arg);
497 extern void koibnal_rx_callback (struct ib_cq *cq, struct ib_cq_entry *e, void *arg);
498 extern void koibnal_tx_callback (struct ib_cq *cq, struct ib_cq_entry *e, void *arg);
499 extern void koibnal_init_tx_msg (koib_tx_t *tx, int type, int body_nob);
500 extern int koibnal_close_conn (koib_conn_t *conn, int why);
501 extern void koibnal_start_active_rdma (int type, int status,
502 koib_rx_t *rx, lib_msg_t *libmsg,
504 struct iovec *iov, ptl_kiov_t *kiov,
505 size_t offset, size_t nob);