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>
54 #define DEBUG_SUBSYSTEM S_NAL
56 #include <libcfs/kp30.h>
57 #include <portals/p30.h>
58 #include <portals/lib-p30.h>
59 #include <portals/nal.h>
61 #include <ts_ib_core.h>
63 #include <ts_ib_sa_client.h>
66 # define IBNAL_N_SCHED num_online_cpus() /* # schedulers */
68 # define IBNAL_N_SCHED 1 /* # schedulers */
70 #define IBNAL_N_CONND 4 /* # connection daemons */
72 #define IBNAL_MIN_RECONNECT_INTERVAL HZ /* first failed connection retry... */
73 #define IBNAL_MAX_RECONNECT_INTERVAL (60*HZ) /* ...exponentially increasing to this */
75 #define IBNAL_MSG_SIZE (4<<10) /* max size of queued messages (inc hdr) */
77 #define IBNAL_MSG_QUEUE_SIZE 8 /* # messages/RDMAs in-flight */
78 #define IBNAL_CREDIT_HIGHWATER 6 /* when to eagerly return credits */
79 #define IBNAL_RETRY 7 /* # times to retry */
80 #define IBNAL_RNR_RETRY 7 /* */
81 #define IBNAL_CM_RETRY 7 /* # times to retry connection */
82 #define IBNAL_FLOW_CONTROL 1
83 #define IBNAL_RESPONDER_RESOURCES 8
85 #define IBNAL_NTX 64 /* # tx descs */
86 #define IBNAL_NTX_NBLK 256 /* # reserved tx descs */
88 #define IBNAL_PEER_HASH_SIZE 101 /* # peer lists */
90 #define IBNAL_RESCHED 100 /* # scheduler loops before reschedule */
92 #define IBNAL_CONCURRENT_PEERS 1000 /* # nodes all talking at once to me */
94 /* default vals for runtime tunables */
95 #define IBNAL_IO_TIMEOUT 50 /* default comms timeout (seconds) */
96 #define IBNAL_LISTENER_TIMEOUT 5 /* default listener timeout (seconds) */
97 #define IBNAL_BACKLOG 127 /* default listener backlog */
98 #define IBNAL_PORT 988 /* default listener port */
100 /************************/
101 /* derived constants... */
103 /* TX messages (shared by all connections) */
104 #define IBNAL_TX_MSGS (IBNAL_NTX + IBNAL_NTX_NBLK)
105 #define IBNAL_TX_MSG_BYTES (IBNAL_TX_MSGS * IBNAL_MSG_SIZE)
106 #define IBNAL_TX_MSG_PAGES ((IBNAL_TX_MSG_BYTES + PAGE_SIZE - 1)/PAGE_SIZE)
108 /* RX messages (per connection) */
109 #define IBNAL_RX_MSGS IBNAL_MSG_QUEUE_SIZE
110 #define IBNAL_RX_MSG_BYTES (IBNAL_RX_MSGS * IBNAL_MSG_SIZE)
111 #define IBNAL_RX_MSG_PAGES ((IBNAL_RX_MSG_BYTES + PAGE_SIZE - 1)/PAGE_SIZE)
113 /* we may have up to 2 completions per transmit +
114 1 completion per receive, per connection */
115 #define IBNAL_CQ_ENTRIES ((2*IBNAL_TX_MSGS) + \
116 (IBNAL_RX_MSGS * IBNAL_CONCURRENT_PEERS))
118 #define IBNAL_RDMA_BASE 0x0eeb0000
120 #define IBNAL_CKSUM 1
121 //#define IBNAL_CALLBACK_CTXT IB_CQ_CALLBACK_PROCESS
122 #define IBNAL_CALLBACK_CTXT IB_CQ_CALLBACK_INTERRUPT
126 int kib_io_timeout; /* comms timeout (seconds) */
127 int kib_listener_timeout; /* listener's timeout */
128 int kib_backlog; /* listenter's accept backlog */
129 int kib_port; /* where the listener listens */
130 struct ctl_table_header *kib_sysctl; /* sysctl interface */
135 int ibp_npages; /* # pages */
136 int ibp_mapped; /* mapped? */
137 __u64 ibp_vaddr; /* mapped region vaddr */
138 __u32 ibp_lkey; /* mapped region lkey */
139 __u32 ibp_rkey; /* mapped region rkey */
140 struct ib_mr *ibp_handle; /* mapped region handle */
141 struct page *ibp_pages[0];
146 int kib_init; /* initialisation state */
147 __u64 kib_incarnation; /* which one am I */
148 int kib_shutdown; /* shut down? */
149 atomic_t kib_nthreads; /* # live threads */
151 __u64 kib_svc_id; /* service number I listen on */
152 tTS_IB_GID kib_svc_gid; /* device/port GID */
153 __u16 kib_svc_pkey; /* device/port pkey */
155 ptl_nid_t kib_nid; /* my NID */
156 struct semaphore kib_nid_mutex; /* serialise NID ops */
157 struct semaphore kib_listener_signal; /* signal IP listener completion */
158 struct socket *kib_listener_sock; /* IP listener's socket */
159 int kib_listener_shutdown; /* ask IP listener to close */
160 void *kib_listen_handle; /* IB listen handle */
162 rwlock_t kib_global_lock; /* stabilize peer/conn ops */
164 struct list_head *kib_peers; /* hash table of all my known peers */
165 int kib_peer_hash_size; /* size of kib_peers */
166 atomic_t kib_npeers; /* # peers extant */
167 atomic_t kib_nconns; /* # connections extant */
169 struct list_head kib_reaper_conns; /* connections to reap */
170 wait_queue_head_t kib_reaper_waitq; /* reaper sleeps here */
171 unsigned long kib_reaper_waketime; /* when reaper will wake */
172 spinlock_t kib_reaper_lock; /* serialise */
174 struct list_head kib_connd_peers; /* peers waiting for a connection */
175 struct list_head kib_connd_acceptq; /* accepted sockets to handle */
176 wait_queue_head_t kib_connd_waitq; /* connection daemons sleep here */
177 spinlock_t kib_connd_lock; /* serialise */
179 wait_queue_head_t kib_sched_waitq; /* schedulers sleep here */
180 struct list_head kib_sched_txq; /* tx requiring attention */
181 struct list_head kib_sched_rxq; /* rx requiring attention */
182 spinlock_t kib_sched_lock; /* serialise */
184 struct kib_tx *kib_tx_descs; /* all the tx descriptors */
185 kib_pages_t *kib_tx_pages; /* premapped tx msg pages */
187 struct list_head kib_idle_txs; /* idle tx descriptors */
188 struct list_head kib_idle_nblk_txs; /* idle reserved tx descriptors */
189 wait_queue_head_t kib_idle_tx_waitq; /* block here for tx descriptor */
190 __u64 kib_next_tx_cookie; /* RDMA completion cookie */
191 spinlock_t kib_tx_lock; /* serialise */
193 struct ib_device *kib_device; /* "the" device */
194 struct ib_device_properties kib_device_props; /* its properties */
195 int kib_port; /* port on the device */
196 struct ib_port_properties kib_port_props; /* its properties */
197 struct ib_pd *kib_pd; /* protection domain */
199 struct ib_fmr_pool *kib_fmr_pool; /* fast memory region pool */
201 struct ib_cq *kib_cq; /* completion queue */
205 #define IBNAL_INIT_NOTHING 0
206 #define IBNAL_INIT_DATA 1
207 #define IBNAL_INIT_LIB 2
208 #define IBNAL_INIT_PD 3
209 #define IBNAL_INIT_FMR 4
210 #define IBNAL_INIT_TXD 5
211 #define IBNAL_INIT_CQ 6
212 #define IBNAL_INIT_ALL 7
214 typedef struct kib_acceptsock /* accepted socket queued for connd */
216 struct list_head ibas_list; /* queue for attention */
217 struct socket *ibas_sock; /* the accepted socket */
220 /************************************************************************
221 * IB Wire message format.
222 * These are sent in sender's byte order (i.e. receiver flips).
223 * They may be sent via TCP/IP (service ID,GID,PKEY query/response),
224 * as private data in the connection request/response, or "normally".
227 typedef struct kib_svcrsp /* service response */
229 __u64 ibsr_svc_id; /* service's id */
230 __u8 ibsr_svc_gid[16]; /* service's gid */
231 __u16 ibsr_svc_pkey; /* service's pkey */
234 typedef struct kib_connparams
236 __u32 ibcp_queue_depth;
252 __u32 rd_key; /* remote key */
253 __u32 rd_nob; /* # of bytes */
254 __u64 rd_addr; /* remote io vaddr */
259 ptl_hdr_t ibim_hdr; /* portals header */
260 char ibim_payload[0]; /* piggy-backed payload */
261 } kib_immediate_msg_t;
265 ptl_hdr_t ibrm_hdr; /* portals header */
266 __u64 ibrm_cookie; /* opaque completion cookie */
267 kib_rdma_desc_t ibrm_desc; /* where to suck/blow */
272 __u64 ibcm_cookie; /* opaque completion cookie */
273 __u32 ibcm_status; /* completion status */
274 } kib_completion_msg_t;
278 /* First 2 fields fixed FOR ALL TIME */
279 __u32 ibm_magic; /* I'm an openibnal message */
280 __u16 ibm_version; /* this is my version number */
282 __u8 ibm_type; /* msg type */
283 __u8 ibm_credits; /* returned credits */
284 __u32 ibm_nob; /* # bytes in whole message */
285 __u32 ibm_cksum; /* checksum (0 == no checksum) */
286 __u64 ibm_srcnid; /* sender's NID */
287 __u64 ibm_srcstamp; /* sender's incarnation */
288 __u64 ibm_dstnid; /* destination's NID */
289 __u64 ibm_dststamp; /* destination's incarnation */
292 kib_connparams_t connparams;
293 kib_immediate_msg_t immediate;
295 kib_completion_msg_t completion;
299 #define IBNAL_MSG_MAGIC 0x0be91b91 /* unique magic */
300 #define IBNAL_MSG_VERSION 2 /* current protocol version */
302 #define IBNAL_MSG_SVCQRY 0xb0 /* service query */
303 #define IBNAL_MSG_SVCRSP 0xb1 /* service response */
304 #define IBNAL_MSG_CONNREQ 0xc0 /* connection request */
305 #define IBNAL_MSG_CONNACK 0xc1 /* connection acknowledge */
306 #define IBNAL_MSG_NOOP 0xd0 /* nothing (just credits) */
307 #define IBNAL_MSG_IMMEDIATE 0xd1 /* portals hdr + payload */
308 #define IBNAL_MSG_PUT_RDMA 0xd2 /* portals PUT hdr + source rdma desc */
309 #define IBNAL_MSG_PUT_DONE 0xd3 /* signal PUT rdma completion */
310 #define IBNAL_MSG_GET_RDMA 0xd4 /* portals GET hdr + sink rdma desc */
311 #define IBNAL_MSG_GET_DONE 0xd5 /* signal GET rdma completion */
313 /***********************************************************************/
315 typedef struct kib_rx /* receive message */
317 struct list_head rx_list; /* queue for attention */
318 struct kib_conn *rx_conn; /* owning conn */
319 int rx_rdma; /* RDMA completion posted? */
320 int rx_posted; /* posted? */
321 __u64 rx_vaddr; /* pre-mapped buffer (hca vaddr) */
322 kib_msg_t *rx_msg; /* pre-mapped buffer (host vaddr) */
323 struct ib_receive_param rx_sp; /* receive work item */
324 struct ib_gather_scatter rx_gl; /* and it's memory */
327 typedef struct kib_tx /* transmit message */
329 struct list_head tx_list; /* queue on idle_txs ibc_tx_queue etc. */
330 int tx_isnblk; /* I'm reserved for non-blocking sends */
331 struct kib_conn *tx_conn; /* owning conn */
332 int tx_mapped; /* mapped for RDMA? */
333 int tx_sending; /* # tx callbacks outstanding */
334 int tx_status; /* completion status */
335 unsigned long tx_deadline; /* completion deadline */
336 int tx_passive_rdma; /* peer sucks/blows */
337 int tx_passive_rdma_wait; /* waiting for peer to complete */
338 __u64 tx_passive_rdma_cookie; /* completion cookie */
339 lib_msg_t *tx_libmsg[2]; /* lib msgs to finalize on completion */
340 kib_md_t tx_md; /* RDMA mapping (active/passive) */
341 __u64 tx_vaddr; /* pre-mapped buffer (hca vaddr) */
342 kib_msg_t *tx_msg; /* pre-mapped buffer (host vaddr) */
343 int tx_nsp; /* # send work items */
344 struct ib_send_param tx_sp[2]; /* send work items... */
345 struct ib_gather_scatter tx_gl[2]; /* ...and their memory */
348 #define KIB_TX_UNMAPPED 0
349 #define KIB_TX_MAPPED 1
350 #define KIB_TX_MAPPED_FMR 2
352 typedef struct kib_connreq
354 /* active connection-in-progress state */
355 struct kib_conn *cr_conn;
359 kib_svcrsp_t cr_svcrsp;
360 struct ib_path_record cr_path;
361 struct ib_cm_active_param cr_connparam;
364 typedef struct kib_conn
366 struct kib_peer *ibc_peer; /* owning peer */
367 struct list_head ibc_list; /* stash on peer's conn list */
368 __u64 ibc_incarnation; /* which instance of the peer */
369 atomic_t ibc_refcount; /* # users */
370 int ibc_state; /* what's happening */
371 atomic_t ibc_nob; /* # bytes buffered */
372 int ibc_nsends_posted; /* # uncompleted sends */
373 int ibc_credits; /* # credits I have */
374 int ibc_outstanding_credits; /* # credits to return */
375 struct list_head ibc_tx_queue; /* send queue */
376 struct list_head ibc_active_txs; /* active tx awaiting completion */
377 spinlock_t ibc_lock; /* serialise */
378 kib_rx_t *ibc_rxs; /* the rx descs */
379 kib_pages_t *ibc_rx_pages; /* premapped rx msg pages */
380 struct ib_qp *ibc_qp; /* queue pair */
381 __u32 ibc_qpn; /* queue pair number */
382 tTS_IB_CM_COMM_ID ibc_comm_id; /* connection ID? */
383 kib_connreq_t *ibc_connreq; /* connection request state */
386 #define IBNAL_CONN_INIT_NOTHING 0 /* initial state */
387 #define IBNAL_CONN_INIT_QP 1 /* ibc_qp set up */
388 #define IBNAL_CONN_CONNECTING 2 /* started to connect */
389 #define IBNAL_CONN_ESTABLISHED 3 /* connection established */
390 #define IBNAL_CONN_DEATHROW 4 /* waiting to be closed */
391 #define IBNAL_CONN_ZOMBIE 5 /* waiting to be freed */
393 typedef struct kib_peer
395 struct list_head ibp_list; /* stash on global peer list */
396 struct list_head ibp_connd_list; /* schedule on kib_connd_peers */
397 ptl_nid_t ibp_nid; /* who's on the other end(s) */
398 __u32 ibp_ip; /* IP to query for peer conn params */
399 int ibp_port; /* port to qery for peer conn params */
400 __u64 ibp_incarnation; /* peer's incarnation */
401 atomic_t ibp_refcount; /* # users */
402 int ibp_persistence; /* "known" peer refs */
403 struct list_head ibp_conns; /* all active connections */
404 struct list_head ibp_tx_queue; /* msgs waiting for a conn */
405 int ibp_connecting; /* connecting+accepting */
406 unsigned long ibp_reconnect_time; /* when reconnect may be attempted */
407 unsigned long ibp_reconnect_interval; /* exponential backoff */
410 extern lib_nal_t kibnal_lib;
411 extern kib_data_t kibnal_data;
412 extern kib_tunables_t kibnal_tunables;
414 static inline struct list_head *
415 kibnal_nid2peerlist (ptl_nid_t nid)
417 unsigned int hash = ((unsigned int)nid) % kibnal_data.kib_peer_hash_size;
419 return (&kibnal_data.kib_peers [hash]);
423 kibnal_peer_active(kib_peer_t *peer)
425 /* Am I in the peer hash table? */
426 return (!list_empty(&peer->ibp_list));
430 kibnal_queue_tx_locked (kib_tx_t *tx, kib_conn_t *conn)
432 /* CAVEAT EMPTOR: tx takes caller's ref on conn */
434 LASSERT (tx->tx_nsp > 0); /* work items set up */
435 LASSERT (tx->tx_conn == NULL); /* only set here */
438 tx->tx_deadline = jiffies + kibnal_tunables.kib_io_timeout * HZ;
439 list_add_tail(&tx->tx_list, &conn->ibc_tx_queue);
444 kibnal_show_rdma_attr (kib_conn_t *conn)
446 struct ib_qp_attribute qp_attr;
449 memset (&qp_attr, 0, sizeof(qp_attr));
450 rc = ib_qp_query(conn->ibc_qp, &qp_attr);
452 CERROR ("Can't get qp attrs: %d\n", rc);
456 CWARN ("RDMA CAPABILITY: write %s read %s\n",
457 (qp_attr.valid_fields & TS_IB_QP_ATTRIBUTE_RDMA_ATOMIC_ENABLE) ?
458 (qp_attr.enable_rdma_write ? "enabled" : "disabled") : "invalid",
459 (qp_attr.valid_fields & TS_IB_QP_ATTRIBUTE_RDMA_ATOMIC_ENABLE) ?
460 (qp_attr.enable_rdma_read ? "enabled" : "disabled") : "invalid");
465 kibnal_page2phys (struct page *p)
467 return page_to_phys(p);
471 * We rely on tx/rx descriptor alignment to allow us to use the lowest bit
472 * of the work request id as a flag to determine if the completion is for a
473 * transmit or a receive. It seems that that the CQ entry's 'op' field
474 * isn't always set correctly on completions that occur after QP teardown. */
477 kibnal_ptr2wreqid (void *ptr, int isrx)
479 unsigned long lptr = (unsigned long)ptr;
481 LASSERT ((lptr & 1) == 0);
482 return (__u64)(lptr | (isrx ? 1 : 0));
486 kibnal_wreqid2ptr (__u64 wreqid)
488 return (void *)(((unsigned long)wreqid) & ~1UL);
492 kibnal_wreqid_is_rx (__u64 wreqid)
494 return (wreqid & 1) != 0;
497 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0))
498 # define sk_allocation allocation
499 # define sk_data_ready data_ready
500 # define sk_write_space write_space
501 # define sk_user_data user_data
502 # define sk_prot prot
503 # define sk_sndbuf sndbuf
504 # define sk_socket socket
505 # define sk_wmem_queued wmem_queued
507 # define sk_sleep sleep
510 extern void kibnal_init_msg(kib_msg_t *msg, int type, int body_nob);
511 extern void kibnal_pack_msg(kib_msg_t *msg, int credits,
512 ptl_nid_t dstnid, __u64 dststamp);
513 extern int kibnal_unpack_msg(kib_msg_t *msg, int nob);
514 extern void kibnal_handle_svcqry (struct socket *sock);
515 extern int kibnal_make_svcqry (kib_conn_t *conn);
516 extern void kibnal_free_acceptsock (kib_acceptsock_t *as);
517 extern int kibnal_listener_procint(ctl_table *table, int write,
518 struct file *filp, void *buffer,
520 extern kib_peer_t *kibnal_create_peer (ptl_nid_t nid);
521 extern void kibnal_put_peer (kib_peer_t *peer);
522 extern int kibnal_del_peer (ptl_nid_t nid, int single_share);
523 extern kib_peer_t *kibnal_find_peer_locked (ptl_nid_t nid);
524 extern void kibnal_unlink_peer_locked (kib_peer_t *peer);
525 extern int kibnal_close_stale_conns_locked (kib_peer_t *peer,
527 extern kib_conn_t *kibnal_create_conn (void);
528 extern void kibnal_put_conn (kib_conn_t *conn);
529 extern void kibnal_destroy_conn (kib_conn_t *conn);
530 extern int kibnal_alloc_pages (kib_pages_t **pp, int npages, int access);
531 extern void kibnal_free_pages (kib_pages_t *p);
533 extern void kibnal_check_sends (kib_conn_t *conn);
535 extern tTS_IB_CM_CALLBACK_RETURN
536 kibnal_bad_conn_callback (tTS_IB_CM_EVENT event, tTS_IB_CM_COMM_ID cid,
537 void *param, void *arg);
538 extern tTS_IB_CM_CALLBACK_RETURN
539 kibnal_conn_callback (tTS_IB_CM_EVENT event, tTS_IB_CM_COMM_ID cid,
540 void *param, void *arg);
541 extern tTS_IB_CM_CALLBACK_RETURN
542 kibnal_passive_conn_callback (tTS_IB_CM_EVENT event, tTS_IB_CM_COMM_ID cid,
543 void *param, void *arg);
545 extern void kibnal_close_conn_locked (kib_conn_t *conn, int error);
546 extern void kibnal_destroy_conn (kib_conn_t *conn);
547 extern int kibnal_thread_start (int (*fn)(void *arg), void *arg);
548 extern int kibnal_scheduler(void *arg);
549 extern int kibnal_connd (void *arg);
550 extern int kibnal_reaper (void *arg);
551 extern void kibnal_callback (struct ib_cq *cq, struct ib_cq_entry *e, void *arg);
552 extern void kibnal_init_tx_msg (kib_tx_t *tx, int type, int body_nob);
553 extern int kibnal_close_conn (kib_conn_t *conn, int why);
554 extern void kibnal_start_active_rdma (int type, int status,
555 kib_rx_t *rx, lib_msg_t *libmsg,
557 struct iovec *iov, ptl_kiov_t *kiov,
558 int offset, int nob);