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_NAL
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 IBNAL_SERVICE_NAME "openibnal"
65 # define IBNAL_N_SCHED num_online_cpus() /* # schedulers */
67 # define IBNAL_N_SCHED 1 /* # schedulers */
70 #define IBNAL_MIN_RECONNECT_INTERVAL HZ /* first failed connection retry... */
71 #define IBNAL_MAX_RECONNECT_INTERVAL (60*HZ) /* ...exponentially increasing to this */
73 #define IBNAL_MSG_SIZE (4<<10) /* max size of queued messages (inc hdr) */
75 #define IBNAL_MSG_QUEUE_SIZE 8 /* # messages/RDMAs in-flight */
76 #define IBNAL_CREDIT_HIGHWATER 6 /* when to eagerly return credits */
77 #define IBNAL_RETRY 7 /* # times to retry */
78 #define IBNAL_RNR_RETRY 7 /* */
79 #define IBNAL_CM_RETRY 7 /* # times to retry connection */
80 #define IBNAL_FLOW_CONTROL 1
81 #define IBNAL_RESPONDER_RESOURCES 8
83 #define IBNAL_NTX 64 /* # tx descs */
84 #define IBNAL_NTX_NBLK 256 /* # reserved tx descs */
86 #define IBNAL_PEER_HASH_SIZE 101 /* # peer lists */
88 #define IBNAL_RESCHED 100 /* # scheduler loops before reschedule */
90 #define IBNAL_CONCURRENT_PEERS 1000 /* # nodes all talking at once to me */
92 /* default vals for runtime tunables */
93 #define IBNAL_IO_TIMEOUT 50 /* default comms timeout (seconds) */
95 /************************/
96 /* derived constants... */
98 /* TX messages (shared by all connections) */
99 #define IBNAL_TX_MSGS (IBNAL_NTX + IBNAL_NTX_NBLK)
100 #define IBNAL_TX_MSG_BYTES (IBNAL_TX_MSGS * IBNAL_MSG_SIZE)
101 #define IBNAL_TX_MSG_PAGES ((IBNAL_TX_MSG_BYTES + PAGE_SIZE - 1)/PAGE_SIZE)
103 /* RX messages (per connection) */
104 #define IBNAL_RX_MSGS IBNAL_MSG_QUEUE_SIZE
105 #define IBNAL_RX_MSG_BYTES (IBNAL_RX_MSGS * IBNAL_MSG_SIZE)
106 #define IBNAL_RX_MSG_PAGES ((IBNAL_RX_MSG_BYTES + PAGE_SIZE - 1)/PAGE_SIZE)
108 /* we may have up to 2 completions per transmit +
109 1 completion per receive, per connection */
110 #define IBNAL_CQ_ENTRIES ((2*IBNAL_TX_MSGS) + \
111 (IBNAL_RX_MSGS * IBNAL_CONCURRENT_PEERS))
113 #define IBNAL_RDMA_BASE 0x0eeb0000
115 #define IBNAL_CKSUM 0
116 //#define IBNAL_CALLBACK_CTXT IB_CQ_CALLBACK_PROCESS
117 #define IBNAL_CALLBACK_CTXT IB_CQ_CALLBACK_INTERRUPT
121 int kib_io_timeout; /* comms timeout (seconds) */
122 struct ctl_table_header *kib_sysctl; /* sysctl interface */
127 int ibp_npages; /* # pages */
128 int ibp_mapped; /* mapped? */
129 __u64 ibp_vaddr; /* mapped region vaddr */
130 __u32 ibp_lkey; /* mapped region lkey */
131 __u32 ibp_rkey; /* mapped region rkey */
132 struct ib_mr *ibp_handle; /* mapped region handle */
133 struct page *ibp_pages[0];
138 int kib_init; /* initialisation state */
139 __u64 kib_incarnation; /* which one am I */
140 int kib_shutdown; /* shut down? */
141 atomic_t kib_nthreads; /* # live threads */
143 __u64 kib_service_id; /* service number I listen on */
144 ptl_nid_t kib_nid; /* my NID */
145 struct semaphore kib_nid_mutex; /* serialise NID ops */
146 struct semaphore kib_nid_signal; /* signal completion */
148 rwlock_t kib_global_lock; /* stabilize peer/conn ops */
150 struct list_head *kib_peers; /* hash table of all my known peers */
151 int kib_peer_hash_size; /* size of kib_peers */
152 atomic_t kib_npeers; /* # peers extant */
153 atomic_t kib_nconns; /* # connections extant */
155 struct list_head kib_connd_conns; /* connections to progress */
156 struct list_head kib_connd_peers; /* peers waiting for a connection */
157 wait_queue_head_t kib_connd_waitq; /* connection daemons sleep here */
158 unsigned long kib_connd_waketime; /* when connd will wake */
159 spinlock_t kib_connd_lock; /* serialise */
161 wait_queue_head_t kib_sched_waitq; /* schedulers sleep here */
162 struct list_head kib_sched_txq; /* tx requiring attention */
163 struct list_head kib_sched_rxq; /* rx requiring attention */
164 spinlock_t kib_sched_lock; /* serialise */
166 struct kib_tx *kib_tx_descs; /* all the tx descriptors */
167 kib_pages_t *kib_tx_pages; /* premapped tx msg pages */
169 struct list_head kib_idle_txs; /* idle tx descriptors */
170 struct list_head kib_idle_nblk_txs; /* idle reserved tx descriptors */
171 wait_queue_head_t kib_idle_tx_waitq; /* block here for tx descriptor */
172 __u64 kib_next_tx_cookie; /* RDMA completion cookie */
173 spinlock_t kib_tx_lock; /* serialise */
175 struct ib_device *kib_device; /* "the" device */
176 struct ib_device_properties kib_device_props; /* its properties */
177 int kib_port; /* port on the device */
178 struct ib_port_properties kib_port_props; /* its properties */
179 struct ib_pd *kib_pd; /* protection domain */
181 struct ib_fmr_pool *kib_fmr_pool; /* fast memory region pool */
183 struct ib_cq *kib_cq; /* completion queue */
184 void *kib_listen_handle; /* where I listen for connections */
188 #define IBNAL_INIT_NOTHING 0
189 #define IBNAL_INIT_DATA 1
190 #define IBNAL_INIT_LIB 2
191 #define IBNAL_INIT_PD 3
192 #define IBNAL_INIT_FMR 4
193 #define IBNAL_INIT_TXD 5
194 #define IBNAL_INIT_CQ 6
195 #define IBNAL_INIT_ALL 7
197 /************************************************************************
198 * Wire message structs.
199 * These are sent in sender's byte order (i.e. receiver flips).
200 * CAVEAT EMPTOR: other structs communicated between nodes (e.g. MAD
201 * private data and SM service info), is LE on the wire.
217 __u32 rd_key; /* remote key */
218 __u32 rd_nob; /* # of bytes */
219 __u64 rd_addr; /* remote io vaddr */
225 ptl_hdr_t ibim_hdr; /* portals header */
226 char ibim_payload[0]; /* piggy-backed payload */
227 } kib_immediate_msg_t;
231 ptl_hdr_t ibrm_hdr; /* portals header */
232 __u64 ibrm_cookie; /* opaque completion cookie */
233 kib_rdma_desc_t ibrm_desc; /* where to suck/blow */
238 __u64 ibcm_cookie; /* opaque completion cookie */
239 __u32 ibcm_status; /* completion status */
240 } kib_completion_msg_t;
244 __u32 ibm_magic; /* I'm an openibnal message */
245 __u16 ibm_version; /* this is my version number */
246 __u8 ibm_type; /* msg type */
247 __u8 ibm_credits; /* returned credits */
253 kib_immediate_msg_t immediate;
255 kib_completion_msg_t completion;
259 #define IBNAL_MSG_MAGIC 0x0be91b91 /* unique magic */
260 #define IBNAL_MSG_VERSION 1 /* current protocol version */
262 #define IBNAL_MSG_NOOP 0xd0 /* nothing (just credits) */
263 #define IBNAL_MSG_IMMEDIATE 0xd1 /* portals hdr + payload */
264 #define IBNAL_MSG_PUT_RDMA 0xd2 /* portals PUT hdr + source rdma desc */
265 #define IBNAL_MSG_PUT_DONE 0xd3 /* signal PUT rdma completion */
266 #define IBNAL_MSG_GET_RDMA 0xd4 /* portals GET hdr + sink rdma desc */
267 #define IBNAL_MSG_GET_DONE 0xd5 /* signal GET rdma completion */
269 /***********************************************************************/
271 typedef struct kib_rx /* receive message */
273 struct list_head rx_list; /* queue for attention */
274 struct kib_conn *rx_conn; /* owning conn */
275 int rx_rdma; /* RDMA completion posted? */
276 int rx_posted; /* posted? */
277 __u64 rx_vaddr; /* pre-mapped buffer (hca vaddr) */
278 kib_msg_t *rx_msg; /* pre-mapped buffer (host vaddr) */
279 struct ib_receive_param rx_sp; /* receive work item */
280 struct ib_gather_scatter rx_gl; /* and it's memory */
283 typedef struct kib_tx /* transmit message */
285 struct list_head tx_list; /* queue on idle_txs ibc_tx_queue etc. */
286 int tx_isnblk; /* I'm reserved for non-blocking sends */
287 struct kib_conn *tx_conn; /* owning conn */
288 int tx_mapped; /* mapped for RDMA? */
289 int tx_sending; /* # tx callbacks outstanding */
290 int tx_status; /* completion status */
291 unsigned long tx_deadline; /* completion deadline */
292 int tx_passive_rdma; /* peer sucks/blows */
293 int tx_passive_rdma_wait; /* waiting for peer to complete */
294 __u64 tx_passive_rdma_cookie; /* completion cookie */
295 lib_msg_t *tx_libmsg[2]; /* lib msgs to finalize on completion */
296 kib_md_t tx_md; /* RDMA mapping (active/passive) */
297 __u64 tx_vaddr; /* pre-mapped buffer (hca vaddr) */
298 kib_msg_t *tx_msg; /* pre-mapped buffer (host vaddr) */
299 int tx_nsp; /* # send work items */
300 struct ib_send_param tx_sp[2]; /* send work items... */
301 struct ib_gather_scatter tx_gl[2]; /* ...and their memory */
304 #define KIB_TX_UNMAPPED 0
305 #define KIB_TX_MAPPED 1
306 #define KIB_TX_MAPPED_FMR 2
308 typedef struct kib_wire_connreq
310 __u32 wcr_magic; /* I'm an openibnal connreq */
311 __u16 wcr_version; /* this is my version number */
312 __u16 wcr_queue_depth; /* this is my receive queue size */
313 __u64 wcr_nid; /* peer's NID */
314 __u64 wcr_incarnation; /* peer's incarnation */
315 } kib_wire_connreq_t;
317 typedef struct kib_connreq
319 /* connection-in-progress */
320 struct kib_conn *cr_conn;
321 kib_wire_connreq_t cr_wcr;
323 struct ib_common_attrib_service cr_service;
325 struct ib_path_record cr_path;
326 struct ib_cm_active_param cr_connparam;
329 typedef struct kib_conn
331 struct kib_peer *ibc_peer; /* owning peer */
332 struct list_head ibc_list; /* stash on peer's conn list */
333 __u64 ibc_incarnation; /* which instance of the peer */
334 atomic_t ibc_refcount; /* # users */
335 int ibc_state; /* what's happening */
336 atomic_t ibc_nob; /* # bytes buffered */
337 int ibc_nsends_posted; /* # uncompleted sends */
338 int ibc_credits; /* # credits I have */
339 int ibc_outstanding_credits; /* # credits to return */
340 struct list_head ibc_tx_queue; /* send queue */
341 struct list_head ibc_active_txs; /* active tx awaiting completion */
342 spinlock_t ibc_lock; /* serialise */
343 kib_rx_t *ibc_rxs; /* the rx descs */
344 kib_pages_t *ibc_rx_pages; /* premapped rx msg pages */
345 struct ib_qp *ibc_qp; /* queue pair */
346 __u32 ibc_qpn; /* queue pair number */
347 tTS_IB_CM_COMM_ID ibc_comm_id; /* connection ID? */
348 kib_connreq_t *ibc_connreq; /* connection request state */
351 #define IBNAL_CONN_INIT_NOTHING 0 /* initial state */
352 #define IBNAL_CONN_INIT_QP 1 /* ibc_qp set up */
353 #define IBNAL_CONN_CONNECTING 2 /* started to connect */
354 #define IBNAL_CONN_ESTABLISHED 3 /* connection established */
355 #define IBNAL_CONN_DEATHROW 4 /* waiting to be closed */
356 #define IBNAL_CONN_ZOMBIE 5 /* waiting to be freed */
358 typedef struct kib_peer
360 struct list_head ibp_list; /* stash on global peer list */
361 struct list_head ibp_connd_list; /* schedule on kib_connd_peers */
362 ptl_nid_t ibp_nid; /* who's on the other end(s) */
363 atomic_t ibp_refcount; /* # users */
364 int ibp_persistence; /* "known" peer refs */
365 struct list_head ibp_conns; /* all active connections */
366 struct list_head ibp_tx_queue; /* msgs waiting for a conn */
367 int ibp_connecting; /* connecting+accepting */
368 unsigned long ibp_reconnect_time; /* when reconnect may be attempted */
369 unsigned long ibp_reconnect_interval; /* exponential backoff */
373 extern lib_nal_t kibnal_lib;
374 extern kib_data_t kibnal_data;
375 extern kib_tunables_t kibnal_tunables;
377 static inline struct list_head *
378 kibnal_nid2peerlist (ptl_nid_t nid)
380 unsigned int hash = ((unsigned int)nid) % kibnal_data.kib_peer_hash_size;
382 return (&kibnal_data.kib_peers [hash]);
386 kibnal_peer_active(kib_peer_t *peer)
388 /* Am I in the peer hash table? */
389 return (!list_empty(&peer->ibp_list));
393 kibnal_queue_tx_locked (kib_tx_t *tx, kib_conn_t *conn)
395 /* CAVEAT EMPTOR: tx takes caller's ref on conn */
397 LASSERT (tx->tx_nsp > 0); /* work items set up */
398 LASSERT (tx->tx_conn == NULL); /* only set here */
401 tx->tx_deadline = jiffies + kibnal_tunables.kib_io_timeout * HZ;
402 list_add_tail(&tx->tx_list, &conn->ibc_tx_queue);
405 #define KIBNAL_SERVICE_KEY_MASK (IB_SA_SERVICE_COMP_MASK_NAME | \
406 IB_SA_SERVICE_COMP_MASK_DATA8_1 | \
407 IB_SA_SERVICE_COMP_MASK_DATA8_2 | \
408 IB_SA_SERVICE_COMP_MASK_DATA8_3 | \
409 IB_SA_SERVICE_COMP_MASK_DATA8_4 | \
410 IB_SA_SERVICE_COMP_MASK_DATA8_5 | \
411 IB_SA_SERVICE_COMP_MASK_DATA8_6 | \
412 IB_SA_SERVICE_COMP_MASK_DATA8_7 | \
413 IB_SA_SERVICE_COMP_MASK_DATA8_8)
416 kibnal_service_nid_field(struct ib_common_attrib_service *srv)
418 /* must be consistent with KIBNAL_SERVICE_KEY_MASK */
419 return (__u64 *)srv->service_data8;
424 kibnal_set_service_keys(struct ib_common_attrib_service *srv, ptl_nid_t nid)
426 LASSERT (strlen (IBNAL_SERVICE_NAME) < sizeof(srv->service_name));
427 memset (srv->service_name, 0, sizeof(srv->service_name));
428 strcpy (srv->service_name, IBNAL_SERVICE_NAME);
430 *kibnal_service_nid_field(srv) = cpu_to_le64(nid);
435 kibnal_show_rdma_attr (kib_conn_t *conn)
437 struct ib_qp_attribute qp_attr;
440 memset (&qp_attr, 0, sizeof(qp_attr));
441 rc = ib_qp_query(conn->ibc_qp, &qp_attr);
443 CERROR ("Can't get qp attrs: %d\n", rc);
447 CWARN ("RDMA CAPABILITY: write %s read %s\n",
448 (qp_attr.valid_fields & TS_IB_QP_ATTRIBUTE_RDMA_ATOMIC_ENABLE) ?
449 (qp_attr.enable_rdma_write ? "enabled" : "disabled") : "invalid",
450 (qp_attr.valid_fields & TS_IB_QP_ATTRIBUTE_RDMA_ATOMIC_ENABLE) ?
451 (qp_attr.enable_rdma_read ? "enabled" : "disabled") : "invalid");
457 kibnal_page2phys (struct page *p)
459 __u64 page_number = p - mem_map;
461 return (page_number << PAGE_SHIFT);
464 # error "no page->phys"
468 * We rely on tx/rx descriptor alignment to allow us to use the lowest bit
469 * of the work request id as a flag to determine if the completion is for a
470 * transmit or a receive. It seems that that the CQ entry's 'op' field
471 * isn't always set correctly on completions that occur after QP teardown. */
474 kibnal_ptr2wreqid (void *ptr, int isrx)
476 unsigned long lptr = (unsigned long)ptr;
478 LASSERT ((lptr & 1) == 0);
479 return (__u64)(lptr | (isrx ? 1 : 0));
483 kibnal_wreqid2ptr (__u64 wreqid)
485 return (void *)(((unsigned long)wreqid) & ~1UL);
489 kibnal_wreqid_is_rx (__u64 wreqid)
491 return (wreqid & 1) != 0;
494 extern kib_peer_t *kibnal_create_peer (ptl_nid_t nid);
495 extern void kibnal_put_peer (kib_peer_t *peer);
496 extern int kibnal_del_peer (ptl_nid_t nid, int single_share);
497 extern kib_peer_t *kibnal_find_peer_locked (ptl_nid_t nid);
498 extern void kibnal_unlink_peer_locked (kib_peer_t *peer);
499 extern int kibnal_close_stale_conns_locked (kib_peer_t *peer,
501 extern kib_conn_t *kibnal_create_conn (void);
502 extern void kibnal_put_conn (kib_conn_t *conn);
503 extern void kibnal_destroy_conn (kib_conn_t *conn);
504 extern int kibnal_alloc_pages (kib_pages_t **pp, int npages, int access);
505 extern void kibnal_free_pages (kib_pages_t *p);
507 extern void kibnal_check_sends (kib_conn_t *conn);
509 extern tTS_IB_CM_CALLBACK_RETURN
510 kibnal_conn_callback (tTS_IB_CM_EVENT event, tTS_IB_CM_COMM_ID cid,
511 void *param, void *arg);
512 extern tTS_IB_CM_CALLBACK_RETURN
513 kibnal_passive_conn_callback (tTS_IB_CM_EVENT event, tTS_IB_CM_COMM_ID cid,
514 void *param, void *arg);
516 extern void kibnal_close_conn_locked (kib_conn_t *conn, int error);
517 extern void kibnal_destroy_conn (kib_conn_t *conn);
518 extern int kibnal_thread_start (int (*fn)(void *arg), void *arg);
519 extern int kibnal_scheduler(void *arg);
520 extern int kibnal_connd (void *arg);
521 extern void kibnal_callback (struct ib_cq *cq, struct ib_cq_entry *e, void *arg);
522 extern void kibnal_init_tx_msg (kib_tx_t *tx, int type, int body_nob);
523 extern int kibnal_close_conn (kib_conn_t *conn, int why);
524 extern void kibnal_start_active_rdma (int type, int status,
525 kib_rx_t *rx, lib_msg_t *libmsg,
527 struct iovec *iov, ptl_kiov_t *kiov,
528 size_t offset, size_t nob);