Whamcloud - gitweb
b=16098
[fs/lustre-release.git] / lnet / ulnds / ptllnd / ptllnd.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see [sun.com URL with a
20  * copy of GPLv2].
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lnet/ulnds/ptllnd/ptllnd.c
37  *
38  * Author: Eric Barton <eeb@bartonsoftware.com>
39  */
40
41 #include "ptllnd.h"
42
43 lnd_t               the_ptllnd = {
44         .lnd_type       = PTLLND,
45         .lnd_startup    = ptllnd_startup,
46         .lnd_shutdown   = ptllnd_shutdown,
47         .lnd_ctl        = ptllnd_ctl,
48         .lnd_send       = ptllnd_send,
49         .lnd_recv       = ptllnd_recv,
50         .lnd_eager_recv = ptllnd_eager_recv,
51         .lnd_notify     = ptllnd_notify,
52         .lnd_wait       = ptllnd_wait,
53         .lnd_setasync   = ptllnd_setasync,
54 };
55
56 static int ptllnd_ni_count = 0;
57
58 static struct list_head ptllnd_idle_history;
59 static struct list_head ptllnd_history_list;
60
61 void
62 ptllnd_history_fini(void)
63 {
64         ptllnd_he_t *he;
65
66         while (!list_empty(&ptllnd_idle_history)) {
67                 he = list_entry(ptllnd_idle_history.next,
68                                 ptllnd_he_t, he_list);
69                 
70                 list_del(&he->he_list);
71                 LIBCFS_FREE(he, sizeof(*he));
72         }
73         
74         while (!list_empty(&ptllnd_history_list)) {
75                 he = list_entry(ptllnd_history_list.next,
76                                 ptllnd_he_t, he_list);
77                 
78                 list_del(&he->he_list);
79                 LIBCFS_FREE(he, sizeof(*he));
80         }
81 }
82
83 int
84 ptllnd_history_init(void)
85 {
86         int          i;
87         ptllnd_he_t *he;
88         int          n;
89         int          rc;
90         
91         CFS_INIT_LIST_HEAD(&ptllnd_idle_history);
92         CFS_INIT_LIST_HEAD(&ptllnd_history_list);
93         
94         rc = ptllnd_parse_int_tunable(&n, "PTLLND_HISTORY", 0);
95         if (rc != 0)
96                 return rc;
97         
98         for (i = 0; i < n; i++) {
99                 LIBCFS_ALLOC(he, sizeof(*he));
100                 if (he == NULL) {
101                         ptllnd_history_fini();
102                         return -ENOMEM;
103                 }
104                 
105                 list_add(&he->he_list, &ptllnd_idle_history);
106         }
107
108         PTLLND_HISTORY("Init");
109
110         return 0;
111 }
112
113 void
114 ptllnd_history(const char *fn, const char *file, const int line,
115                const char *fmt, ...)
116 {
117         static int     seq;
118         
119         va_list        ap;
120         ptllnd_he_t   *he;
121         
122         if (!list_empty(&ptllnd_idle_history)) {
123                 he = list_entry(ptllnd_idle_history.next,
124                                 ptllnd_he_t, he_list);
125         } else if (!list_empty(&ptllnd_history_list)) {
126                 he = list_entry(ptllnd_history_list.next,
127                                 ptllnd_he_t, he_list);
128         } else {
129                 return;
130         }
131
132         list_del(&he->he_list);
133         list_add_tail(&he->he_list, &ptllnd_history_list);
134
135         he->he_seq = seq++;
136         he->he_fn = fn;
137         he->he_file = file;
138         he->he_line = line;
139         gettimeofday(&he->he_time, NULL);
140         
141         va_start(ap, fmt);
142         vsnprintf(he->he_msg, sizeof(he->he_msg), fmt, ap);
143         va_end(ap);
144 }
145
146 void
147 ptllnd_dump_history(void)
148 {
149         ptllnd_he_t    *he;
150
151         PTLLND_HISTORY("dumping...");
152         
153         while (!list_empty(&ptllnd_history_list)) {
154                 he = list_entry(ptllnd_history_list.next,
155                                 ptllnd_he_t, he_list);
156
157                 list_del(&he->he_list);
158                 
159                 CDEBUG(D_WARNING, "%d %d.%06d (%s:%d:%s()) %s\n", he->he_seq,
160                        (int)he->he_time.tv_sec, (int)he->he_time.tv_usec,
161                        he->he_file, he->he_line, he->he_fn, he->he_msg);
162
163                 list_add_tail(&he->he_list, &ptllnd_idle_history);
164         }
165
166         PTLLND_HISTORY("complete");
167 }
168
169 void 
170 ptllnd_assert_wire_constants (void)
171 {
172         /* Wire protocol assertions generated by 'wirecheck'
173          * running on Linux fedora 2.6.11-co-0.6.4 #1 Mon Jun 19 05:36:13 UTC 2006 i686 i686 i386 GNU
174          * with gcc version 4.1.1 20060525 (Red Hat 4.1.1-1) */
175
176
177         /* Constants... */
178         CLASSERT (PTL_RESERVED_MATCHBITS == 0x100);
179         CLASSERT (LNET_MSG_MATCHBITS == 0);
180         CLASSERT (PTLLND_MSG_MAGIC == 0x50746C4E);
181         CLASSERT (PTLLND_MSG_VERSION == 0x04);
182         CLASSERT (PTLLND_RDMA_OK == 0x00);
183         CLASSERT (PTLLND_RDMA_FAIL == 0x01);
184         CLASSERT (PTLLND_MSG_TYPE_INVALID == 0x00);
185         CLASSERT (PTLLND_MSG_TYPE_PUT == 0x01);
186         CLASSERT (PTLLND_MSG_TYPE_GET == 0x02);
187         CLASSERT (PTLLND_MSG_TYPE_IMMEDIATE == 0x03);
188         CLASSERT (PTLLND_MSG_TYPE_NOOP == 0x04);
189         CLASSERT (PTLLND_MSG_TYPE_HELLO == 0x05);
190         CLASSERT (PTLLND_MSG_TYPE_NAK == 0x06);
191
192         /* Checks for struct kptl_msg_t */
193         CLASSERT ((int)sizeof(kptl_msg_t) == 136);
194         CLASSERT ((int)offsetof(kptl_msg_t, ptlm_magic) == 0);
195         CLASSERT ((int)sizeof(((kptl_msg_t *)0)->ptlm_magic) == 4);
196         CLASSERT ((int)offsetof(kptl_msg_t, ptlm_version) == 4);
197         CLASSERT ((int)sizeof(((kptl_msg_t *)0)->ptlm_version) == 2);
198         CLASSERT ((int)offsetof(kptl_msg_t, ptlm_type) == 6);
199         CLASSERT ((int)sizeof(((kptl_msg_t *)0)->ptlm_type) == 1);
200         CLASSERT ((int)offsetof(kptl_msg_t, ptlm_credits) == 7);
201         CLASSERT ((int)sizeof(((kptl_msg_t *)0)->ptlm_credits) == 1);
202         CLASSERT ((int)offsetof(kptl_msg_t, ptlm_nob) == 8);
203         CLASSERT ((int)sizeof(((kptl_msg_t *)0)->ptlm_nob) == 4);
204         CLASSERT ((int)offsetof(kptl_msg_t, ptlm_cksum) == 12);
205         CLASSERT ((int)sizeof(((kptl_msg_t *)0)->ptlm_cksum) == 4);
206         CLASSERT ((int)offsetof(kptl_msg_t, ptlm_srcnid) == 16);
207         CLASSERT ((int)sizeof(((kptl_msg_t *)0)->ptlm_srcnid) == 8);
208         CLASSERT ((int)offsetof(kptl_msg_t, ptlm_srcstamp) == 24);
209         CLASSERT ((int)sizeof(((kptl_msg_t *)0)->ptlm_srcstamp) == 8);
210         CLASSERT ((int)offsetof(kptl_msg_t, ptlm_dstnid) == 32);
211         CLASSERT ((int)sizeof(((kptl_msg_t *)0)->ptlm_dstnid) == 8);
212         CLASSERT ((int)offsetof(kptl_msg_t, ptlm_dststamp) == 40);
213         CLASSERT ((int)sizeof(((kptl_msg_t *)0)->ptlm_dststamp) == 8);
214         CLASSERT ((int)offsetof(kptl_msg_t, ptlm_srcpid) == 48);
215         CLASSERT ((int)sizeof(((kptl_msg_t *)0)->ptlm_srcpid) == 4);
216         CLASSERT ((int)offsetof(kptl_msg_t, ptlm_dstpid) == 52);
217         CLASSERT ((int)sizeof(((kptl_msg_t *)0)->ptlm_dstpid) == 4);
218         CLASSERT ((int)offsetof(kptl_msg_t, ptlm_u.immediate) == 56);
219         CLASSERT ((int)sizeof(((kptl_msg_t *)0)->ptlm_u.immediate) == 72);
220         CLASSERT ((int)offsetof(kptl_msg_t, ptlm_u.rdma) == 56);
221         CLASSERT ((int)sizeof(((kptl_msg_t *)0)->ptlm_u.rdma) == 80);
222         CLASSERT ((int)offsetof(kptl_msg_t, ptlm_u.hello) == 56);
223         CLASSERT ((int)sizeof(((kptl_msg_t *)0)->ptlm_u.hello) == 12);
224
225         /* Checks for struct kptl_immediate_msg_t */
226         CLASSERT ((int)sizeof(kptl_immediate_msg_t) == 72);
227         CLASSERT ((int)offsetof(kptl_immediate_msg_t, kptlim_hdr) == 0);
228         CLASSERT ((int)sizeof(((kptl_immediate_msg_t *)0)->kptlim_hdr) == 72);
229         CLASSERT ((int)offsetof(kptl_immediate_msg_t, kptlim_payload[13]) == 85);
230         CLASSERT ((int)sizeof(((kptl_immediate_msg_t *)0)->kptlim_payload[13]) == 1);
231
232         /* Checks for struct kptl_rdma_msg_t */
233         CLASSERT ((int)sizeof(kptl_rdma_msg_t) == 80);
234         CLASSERT ((int)offsetof(kptl_rdma_msg_t, kptlrm_hdr) == 0);
235         CLASSERT ((int)sizeof(((kptl_rdma_msg_t *)0)->kptlrm_hdr) == 72);
236         CLASSERT ((int)offsetof(kptl_rdma_msg_t, kptlrm_matchbits) == 72);
237         CLASSERT ((int)sizeof(((kptl_rdma_msg_t *)0)->kptlrm_matchbits) == 8);
238
239         /* Checks for struct kptl_hello_msg_t */
240         CLASSERT ((int)sizeof(kptl_hello_msg_t) == 12);
241         CLASSERT ((int)offsetof(kptl_hello_msg_t, kptlhm_matchbits) == 0);
242         CLASSERT ((int)sizeof(((kptl_hello_msg_t *)0)->kptlhm_matchbits) == 8);
243         CLASSERT ((int)offsetof(kptl_hello_msg_t, kptlhm_max_msg_size) == 8);
244         CLASSERT ((int)sizeof(((kptl_hello_msg_t *)0)->kptlhm_max_msg_size) == 4);
245 }
246
247 int
248 ptllnd_parse_int_tunable(int *value, char *name, int dflt)
249 {
250         char    *env = getenv(name);
251         char    *end;
252
253         if (env == NULL) {
254                 *value = dflt;
255                 return 0;
256         }
257
258         *value = strtoull(env, &end, 0);
259         if (*end == 0)
260                 return 0;
261
262         CERROR("Can't parse tunable %s=%s\n", name, env);
263         return -EINVAL;
264 }
265
266 int
267 ptllnd_get_tunables(lnet_ni_t *ni)
268 {
269         ptllnd_ni_t *plni = ni->ni_data;
270         int          max_msg_size;
271         int          msgs_per_buffer;
272         int          rc;
273         int          temp;
274
275         /*  Other tunable defaults depend on this */
276         rc = ptllnd_parse_int_tunable(&plni->plni_debug, "PTLLND_DEBUG", 0);
277         if (rc != 0)
278                 return rc;
279
280         rc = ptllnd_parse_int_tunable(&plni->plni_portal,
281                                       "PTLLND_PORTAL", PTLLND_PORTAL);
282         if (rc != 0)
283                 return rc;
284
285         rc = ptllnd_parse_int_tunable(&temp,
286                                       "PTLLND_PID", PTLLND_PID);
287         if (rc != 0)
288                 return rc;
289         plni->plni_ptllnd_pid = (ptl_pid_t)temp;
290
291         rc = ptllnd_parse_int_tunable(&plni->plni_peer_credits,
292                                       "PTLLND_PEERCREDITS", PTLLND_PEERCREDITS);
293         if (rc != 0)
294                 return rc;
295
296         rc = ptllnd_parse_int_tunable(&max_msg_size,
297                                       "PTLLND_MAX_MSG_SIZE",
298                                       PTLLND_MAX_ULND_MSG_SIZE);
299         if (rc != 0)
300                 return rc;
301
302         rc = ptllnd_parse_int_tunable(&msgs_per_buffer,
303                                       "PTLLND_MSGS_PER_BUFFER", 64);
304         if (rc != 0)
305                 return rc;
306
307         rc = ptllnd_parse_int_tunable(&plni->plni_msgs_spare,
308                                       "PTLLND_MSGS_SPARE", 256);
309         if (rc != 0)
310                 return rc;
311
312         rc = ptllnd_parse_int_tunable(&plni->plni_peer_hash_size,
313                                       "PTLLND_PEER_HASH_SIZE", 101);
314         if (rc != 0)
315                 return rc;
316
317
318         rc = ptllnd_parse_int_tunable(&plni->plni_eq_size,
319                                       "PTLLND_EQ_SIZE", 1024);
320         if (rc != 0)
321                 return rc;
322
323         rc = ptllnd_parse_int_tunable(&plni->plni_checksum,
324                                       "PTLLND_CHECKSUM", 0);
325         if (rc != 0)
326                 return rc;
327
328         rc = ptllnd_parse_int_tunable(&plni->plni_max_tx_history,
329                                       "PTLLND_TX_HISTORY",
330                                       plni->plni_debug ? 1024 : 0);
331         if (rc != 0)
332                 return rc;
333
334         rc = ptllnd_parse_int_tunable(&plni->plni_abort_on_protocol_mismatch,
335                                       "PTLLND_ABORT_ON_PROTOCOL_MISMATCH", 1);
336         if (rc != 0)
337                 return rc;
338
339         rc = ptllnd_parse_int_tunable(&plni->plni_abort_on_nak,
340                                       "PTLLND_ABORT_ON_NAK", 0);
341         if (rc != 0)
342                 return rc;
343
344         rc = ptllnd_parse_int_tunable(&plni->plni_dump_on_nak,
345                                       "PTLLND_DUMP_ON_NAK", plni->plni_debug);
346         if (rc != 0)
347                 return rc;
348
349         rc = ptllnd_parse_int_tunable(&plni->plni_watchdog_interval,
350                                       "PTLLND_WATCHDOG_INTERVAL", 1);
351         if (rc != 0)
352                 return rc;
353         if (plni->plni_watchdog_interval <= 0)
354                 plni->plni_watchdog_interval = 1;
355
356         rc = ptllnd_parse_int_tunable(&plni->plni_timeout,
357                                       "PTLLND_TIMEOUT", 50);
358         if (rc != 0)
359                 return rc;
360
361         rc = ptllnd_parse_int_tunable(&plni->plni_long_wait,
362                                       "PTLLND_LONG_WAIT",
363                                       plni->plni_debug ? 5 : plni->plni_timeout);
364         if (rc != 0)
365                 return rc;
366         plni->plni_long_wait *= 1000;           /* convert to mS */
367
368         plni->plni_max_msg_size = max_msg_size & ~7;
369         if (plni->plni_max_msg_size < PTLLND_MIN_BUFFER_SIZE)
370                 plni->plni_max_msg_size = PTLLND_MIN_BUFFER_SIZE;
371         CLASSERT ((PTLLND_MIN_BUFFER_SIZE & 7) == 0);
372         CLASSERT (sizeof(kptl_msg_t) <= PTLLND_MIN_BUFFER_SIZE);
373
374         plni->plni_buffer_size = plni->plni_max_msg_size * msgs_per_buffer;
375
376         CDEBUG(D_NET, "portal          = %d\n",plni->plni_portal);
377         CDEBUG(D_NET, "ptllnd_pid      = %d\n",plni->plni_ptllnd_pid);
378         CDEBUG(D_NET, "max_msg_size    = %d\n",max_msg_size);
379         CDEBUG(D_NET, "msgs_per_buffer = %d\n",msgs_per_buffer);
380         CDEBUG(D_NET, "msgs_spare      = %d\n",plni->plni_msgs_spare);
381         CDEBUG(D_NET, "peer_hash_size  = %d\n",plni->plni_peer_hash_size);
382         CDEBUG(D_NET, "eq_size         = %d\n",plni->plni_eq_size);
383         CDEBUG(D_NET, "max_msg_size    = %d\n",plni->plni_max_msg_size);
384         CDEBUG(D_NET, "buffer_size     = %d\n",plni->plni_buffer_size);
385
386         return 0;
387 }
388
389 ptllnd_buffer_t *
390 ptllnd_create_buffer (lnet_ni_t *ni)
391 {
392         ptllnd_ni_t     *plni = ni->ni_data;
393         ptllnd_buffer_t *buf;
394
395         LIBCFS_ALLOC(buf, sizeof(*buf));
396         if (buf == NULL) {
397                 CERROR("Can't allocate buffer descriptor\n");
398                 return NULL;
399         }
400
401         buf->plb_ni = ni;
402         buf->plb_posted = 0;
403         CFS_INIT_LIST_HEAD(&buf->plb_list);
404
405         LIBCFS_ALLOC(buf->plb_buffer, plni->plni_buffer_size);
406         if (buf->plb_buffer == NULL) {
407                 CERROR("Can't allocate buffer size %d\n",
408                        plni->plni_buffer_size);
409                 LIBCFS_FREE(buf, sizeof(*buf));
410                 return NULL;
411         }
412
413         list_add(&buf->plb_list, &plni->plni_buffers);
414         plni->plni_nbuffers++;
415
416         return buf;
417 }
418
419 void
420 ptllnd_destroy_buffer (ptllnd_buffer_t *buf)
421 {
422         ptllnd_ni_t     *plni = buf->plb_ni->ni_data;
423
424         LASSERT (!buf->plb_posted);
425
426         plni->plni_nbuffers--;
427         list_del(&buf->plb_list);
428         LIBCFS_FREE(buf->plb_buffer, plni->plni_buffer_size);
429         LIBCFS_FREE(buf, sizeof(*buf));
430 }
431
432 int
433 ptllnd_size_buffers (lnet_ni_t *ni, int delta)
434 {
435         ptllnd_ni_t     *plni = ni->ni_data;
436         ptllnd_buffer_t *buf;
437         int              nmsgs;
438         int              nbufs;
439         int              rc;
440
441         CDEBUG(D_NET, "nposted_buffers = %d (before)\n",plni->plni_nposted_buffers);
442         CDEBUG(D_NET, "nbuffers = %d (before)\n",plni->plni_nbuffers);
443
444         plni->plni_nmsgs += delta;
445         LASSERT(plni->plni_nmsgs >= 0);
446         
447         nmsgs = plni->plni_nmsgs + plni->plni_msgs_spare;
448
449         nbufs = (nmsgs * plni->plni_max_msg_size + plni->plni_buffer_size - 1) /
450                 plni->plni_buffer_size;
451
452         while (nbufs > plni->plni_nbuffers) {
453                 buf = ptllnd_create_buffer(ni);
454
455                 if (buf == NULL)
456                         return -ENOMEM;
457
458                 rc = ptllnd_post_buffer(buf);
459                 if (rc != 0) {
460                         /* TODO - this path seems to orpahn the buffer
461                          * in a state where its not posted and will never be
462                          * However it does not leak the buffer as it's
463                          * already been put onto the global buffer list
464                          * and will be cleaned up
465                          */
466                         return rc;
467                 }
468         }
469
470         CDEBUG(D_NET, "nposted_buffers = %d (after)\n",plni->plni_nposted_buffers);
471         CDEBUG(D_NET, "nbuffers = %d (after)\n",plni->plni_nbuffers);
472         return 0;
473 }
474
475 void
476 ptllnd_destroy_buffers (lnet_ni_t *ni)
477 {
478         ptllnd_ni_t       *plni = ni->ni_data;
479         ptllnd_buffer_t   *buf;
480         struct list_head  *tmp;
481         struct list_head  *nxt;
482
483         CDEBUG(D_NET, "nposted_buffers = %d (before)\n",plni->plni_nposted_buffers);
484         CDEBUG(D_NET, "nbuffers = %d (before)\n",plni->plni_nbuffers);
485
486         list_for_each_safe(tmp, nxt, &plni->plni_buffers) {
487                 buf = list_entry(tmp, ptllnd_buffer_t, plb_list);
488
489                 //CDEBUG(D_NET, "buf=%p posted=%d\n",buf,buf->plb_posted);
490
491                 LASSERT (plni->plni_nbuffers > 0);
492                 if (buf->plb_posted) {
493                         time_t   start = cfs_time_current_sec();
494                         int      w = plni->plni_long_wait;
495
496                         LASSERT (plni->plni_nposted_buffers > 0);
497
498 #ifdef LUSTRE_PORTALS_UNLINK_SEMANTICS
499                         (void) PtlMDUnlink(buf->plb_md);
500
501                         while (buf->plb_posted) {
502                                 if (w > 0 && cfs_time_current_sec() > start + w/1000) {
503                                         CWARN("Waited %ds to unlink buffer\n",
504                                               (int)(cfs_time_current_sec() - start));
505                                         w *= 2;
506                                 }
507                                 ptllnd_wait(ni, w);
508                         }
509 #else
510                         while (buf->plb_posted) {
511                                 rc = PtlMDUnlink(buf->plb_md);
512                                 if (rc == PTL_OK) {
513                                         buf->plb_posted = 0;
514                                         plni->plni_nposted_buffers--;
515                                         break;
516                                 }
517                                 LASSERT (rc == PTL_MD_IN_USE);
518                                 if (w > 0 && cfs_time_current_sec() > start + w/1000) {
519                                         CWARN("Waited %ds to unlink buffer\n",
520                                               cfs_time_current_sec() - start);
521                                         w *= 2;
522                                 }
523                                 ptllnd_wait(ni, w);
524                         }
525 #endif
526                 }
527                 ptllnd_destroy_buffer(buf);
528         }
529
530         CDEBUG(D_NET, "nposted_buffers = %d (after)\n",plni->plni_nposted_buffers);
531         CDEBUG(D_NET, "nbuffers = %d (after)\n",plni->plni_nbuffers);
532
533         LASSERT (plni->plni_nposted_buffers == 0);
534         LASSERT (plni->plni_nbuffers == 0);
535 }
536
537 int
538 ptllnd_create_peer_hash (lnet_ni_t *ni)
539 {
540         ptllnd_ni_t *plni = ni->ni_data;
541         int          i;
542
543         plni->plni_npeers = 0;
544
545         LIBCFS_ALLOC(plni->plni_peer_hash,
546                      plni->plni_peer_hash_size * sizeof(*plni->plni_peer_hash));
547         if (plni->plni_peer_hash == NULL) {
548                 CERROR("Can't allocate ptllnd peer hash (size %d)\n",
549                        plni->plni_peer_hash_size);
550                 return -ENOMEM;
551         }
552
553         for (i = 0; i < plni->plni_peer_hash_size; i++)
554                 CFS_INIT_LIST_HEAD(&plni->plni_peer_hash[i]);
555
556         return 0;
557 }
558
559 void
560 ptllnd_destroy_peer_hash (lnet_ni_t *ni)
561 {
562         ptllnd_ni_t    *plni = ni->ni_data;
563         int             i;
564
565         LASSERT( plni->plni_npeers == 0);
566
567         for (i = 0; i < plni->plni_peer_hash_size; i++)
568                 LASSERT (list_empty(&plni->plni_peer_hash[i]));
569
570         LIBCFS_FREE(plni->plni_peer_hash,
571                     plni->plni_peer_hash_size * sizeof(*plni->plni_peer_hash));
572 }
573
574 void
575 ptllnd_close_peers (lnet_ni_t *ni)
576 {
577         ptllnd_ni_t    *plni = ni->ni_data;
578         ptllnd_peer_t  *plp;
579         int             i;
580
581         for (i = 0; i < plni->plni_peer_hash_size; i++)
582                 while (!list_empty(&plni->plni_peer_hash[i])) {
583                         plp = list_entry(plni->plni_peer_hash[i].next,
584                                          ptllnd_peer_t, plp_list);
585
586                         ptllnd_close_peer(plp, 0);
587                 }
588 }
589
590 int
591 ptllnd_ctl(lnet_ni_t *ni, unsigned int cmd, void *arg)
592 {
593         switch (cmd) {
594         case IOC_LIBCFS_DEBUG_PEER:
595                 ptllnd_dump_debug(ni, *((lnet_process_id_t *)arg));
596                 return 0;
597                 
598         default:
599                 return -EINVAL;
600         }
601 }
602
603 __u64
604 ptllnd_get_timestamp(void)
605 {
606         struct timeval  tv;
607         int             rc = gettimeofday(&tv, NULL);
608
609         LASSERT (rc == 0);
610         return ((__u64)tv.tv_sec) * 1000000 + tv.tv_usec;
611 }
612
613 void
614 ptllnd_shutdown (lnet_ni_t *ni)
615 {
616         ptllnd_ni_t *plni = ni->ni_data;
617         int          rc;
618         time_t       start = cfs_time_current_sec();
619         int          w = plni->plni_long_wait;
620
621         LASSERT (ptllnd_ni_count == 1);
622         plni->plni_max_tx_history = 0;
623
624         ptllnd_cull_tx_history(plni);
625
626         ptllnd_close_peers(ni);
627         ptllnd_destroy_buffers(ni);
628
629         while (plni->plni_npeers > 0) {
630                 if (w > 0 && cfs_time_current_sec() > start + w/1000) {
631                         CWARN("Waited %ds for peers to shutdown\n",
632                               (int)(cfs_time_current_sec() - start));
633                         w *= 2;
634                 }
635                 ptllnd_wait(ni, w);
636         }
637
638         LASSERT (plni->plni_ntxs == 0);
639         LASSERT (plni->plni_nrxs == 0);
640
641         rc = PtlEQFree(plni->plni_eqh);
642         LASSERT (rc == PTL_OK);
643
644         rc = PtlNIFini(plni->plni_nih);
645         LASSERT (rc == PTL_OK);
646
647         ptllnd_destroy_peer_hash(ni);
648         LIBCFS_FREE(plni, sizeof(*plni));
649         ptllnd_ni_count--;
650 }
651
652 int
653 ptllnd_startup (lnet_ni_t *ni)
654 {
655         ptllnd_ni_t *plni;
656         int          rc;
657
658         /* could get limits from portals I guess... */
659         ni->ni_maxtxcredits =
660         ni->ni_peertxcredits = 1000;
661
662         if (ptllnd_ni_count != 0) {
663                 CERROR("Can't have > 1 instance of ptllnd\n");
664                 return -EPERM;
665         }
666
667         ptllnd_ni_count++;
668
669         rc = ptllnd_history_init();
670         if (rc != 0) {
671                 CERROR("Can't init history\n");
672                 goto failed0;
673         }
674         
675         LIBCFS_ALLOC(plni, sizeof(*plni));
676         if (plni == NULL) {
677                 CERROR("Can't allocate ptllnd state\n");
678                 rc = -ENOMEM;
679                 goto failed0;
680         }
681
682         ni->ni_data = plni;
683
684         plni->plni_stamp = ptllnd_get_timestamp();
685         plni->plni_nrxs = 0;
686         plni->plni_ntxs = 0;
687         plni->plni_ntx_history = 0;
688         plni->plni_watchdog_peeridx = 0;
689         plni->plni_watchdog_nextt = cfs_time_current_sec();
690         CFS_INIT_LIST_HEAD(&plni->plni_zombie_txs);
691         CFS_INIT_LIST_HEAD(&plni->plni_tx_history);
692
693         /*
694          *  Initilize buffer related data structures
695          */
696         CFS_INIT_LIST_HEAD(&plni->plni_buffers);
697         plni->plni_nbuffers = 0;
698         plni->plni_nposted_buffers = 0;
699
700         rc = ptllnd_get_tunables(ni);
701         if (rc != 0)
702                 goto failed1;
703
704         rc = ptllnd_create_peer_hash(ni);
705         if (rc != 0)
706                 goto failed1;
707
708         /* NB I most probably won't get the PID I requested here.  It doesn't
709          * matter because I don't need a fixed PID (only connection acceptors
710          * need a "well known" PID). */
711
712         rc = PtlNIInit(PTL_IFACE_DEFAULT, plni->plni_ptllnd_pid,
713                        NULL, NULL, &plni->plni_nih);
714         if (rc != PTL_OK && rc != PTL_IFACE_DUP) {
715                 CERROR("PtlNIInit failed: %s(%d)\n",
716                        ptllnd_errtype2str(rc), rc);
717                 rc = -ENODEV;
718                 goto failed2;
719         }
720
721         rc = PtlEQAlloc(plni->plni_nih, plni->plni_eq_size,
722                         PTL_EQ_HANDLER_NONE, &plni->plni_eqh);
723         if (rc != PTL_OK) {
724                 CERROR("PtlEQAlloc failed: %s(%d)\n",
725                        ptllnd_errtype2str(rc), rc);
726                 rc = -ENODEV;
727                 goto failed3;
728         }
729
730         /*
731          * Fetch the Portals NID
732          */
733         rc = PtlGetId(plni->plni_nih, &plni->plni_portals_id);
734         if (rc != PTL_OK) {
735                 CERROR ("PtlGetID failed : %s(%d)\n",
736                         ptllnd_errtype2str(rc), rc);
737                 rc = -EINVAL;
738                 goto failed4;
739         }
740
741         /*
742          * Create the new NID.  Based on the LND network type
743          * and the lower ni's address data.
744          */
745         ni->ni_nid = ptllnd_ptl2lnetnid(ni, plni->plni_portals_id.nid);
746
747         CDEBUG(D_NET, "ptl id  =%s\n", ptllnd_ptlid2str(plni->plni_portals_id));
748         CDEBUG(D_NET, "lnet id =%s (passed back)\n",
749                libcfs_id2str((lnet_process_id_t) {
750                        .nid = ni->ni_nid, .pid = the_lnet.ln_pid}));
751
752         rc = ptllnd_size_buffers(ni, 0);
753         if (rc != 0)
754                 goto failed4;
755
756         return 0;
757
758  failed4:
759         ptllnd_destroy_buffers(ni);
760         PtlEQFree(plni->plni_eqh);
761  failed3:
762         PtlNIFini(plni->plni_nih);
763  failed2:
764         ptllnd_destroy_peer_hash(ni);
765  failed1:
766         LIBCFS_FREE(plni, sizeof(*plni));
767  failed0:
768         ptllnd_history_fini();
769         ptllnd_ni_count--;
770         CDEBUG(D_NET, "<<< rc=%d\n",rc);
771         return rc;
772 }
773
774 const char *ptllnd_evtype2str(int type)
775 {
776 #define DO_TYPE(x) case x: return #x;
777         switch(type)
778         {
779                 DO_TYPE(PTL_EVENT_GET_START);
780                 DO_TYPE(PTL_EVENT_GET_END);
781                 DO_TYPE(PTL_EVENT_PUT_START);
782                 DO_TYPE(PTL_EVENT_PUT_END);
783                 DO_TYPE(PTL_EVENT_REPLY_START);
784                 DO_TYPE(PTL_EVENT_REPLY_END);
785                 DO_TYPE(PTL_EVENT_ACK);
786                 DO_TYPE(PTL_EVENT_SEND_START);
787                 DO_TYPE(PTL_EVENT_SEND_END);
788                 DO_TYPE(PTL_EVENT_UNLINK);
789         default:
790                 return "<unknown event type>";
791         }
792 #undef DO_TYPE
793 }
794
795 const char *ptllnd_msgtype2str(int type)
796 {
797 #define DO_TYPE(x) case x: return #x;
798         switch(type)
799         {
800                 DO_TYPE(PTLLND_MSG_TYPE_INVALID);
801                 DO_TYPE(PTLLND_MSG_TYPE_PUT);
802                 DO_TYPE(PTLLND_MSG_TYPE_GET);
803                 DO_TYPE(PTLLND_MSG_TYPE_IMMEDIATE);
804                 DO_TYPE(PTLLND_MSG_TYPE_HELLO);
805                 DO_TYPE(PTLLND_MSG_TYPE_NOOP);
806                 DO_TYPE(PTLLND_MSG_TYPE_NAK);
807         default:
808                 return "<unknown msg type>";
809         }
810 #undef DO_TYPE
811 }
812
813 const char *ptllnd_errtype2str(int type)
814 {
815 #define DO_TYPE(x) case x: return #x;
816         switch(type)
817         {
818                 DO_TYPE(PTL_OK);
819                 DO_TYPE(PTL_SEGV);
820                 DO_TYPE(PTL_NO_SPACE);
821                 DO_TYPE(PTL_ME_IN_USE);
822                 DO_TYPE(PTL_NAL_FAILED);
823                 DO_TYPE(PTL_NO_INIT);
824                 DO_TYPE(PTL_IFACE_DUP);
825                 DO_TYPE(PTL_IFACE_INVALID);
826                 DO_TYPE(PTL_HANDLE_INVALID);
827                 DO_TYPE(PTL_MD_INVALID);
828                 DO_TYPE(PTL_ME_INVALID);
829                 DO_TYPE(PTL_PROCESS_INVALID);
830                 DO_TYPE(PTL_PT_INDEX_INVALID);
831                 DO_TYPE(PTL_SR_INDEX_INVALID);
832                 DO_TYPE(PTL_EQ_INVALID);
833                 DO_TYPE(PTL_EQ_DROPPED);
834                 DO_TYPE(PTL_EQ_EMPTY);
835                 DO_TYPE(PTL_MD_NO_UPDATE);
836                 DO_TYPE(PTL_FAIL);
837                 DO_TYPE(PTL_AC_INDEX_INVALID);
838                 DO_TYPE(PTL_MD_ILLEGAL);
839                 DO_TYPE(PTL_ME_LIST_TOO_LONG);
840                 DO_TYPE(PTL_MD_IN_USE);
841                 DO_TYPE(PTL_NI_INVALID);
842                 DO_TYPE(PTL_PID_INVALID);
843                 DO_TYPE(PTL_PT_FULL);
844                 DO_TYPE(PTL_VAL_FAILED);
845                 DO_TYPE(PTL_NOT_IMPLEMENTED);
846                 DO_TYPE(PTL_NO_ACK);
847                 DO_TYPE(PTL_EQ_IN_USE);
848                 DO_TYPE(PTL_PID_IN_USE);
849                 DO_TYPE(PTL_INV_EQ_SIZE);
850                 DO_TYPE(PTL_AGAIN);
851         default:
852                 return "<unknown error type>";
853         }
854 #undef DO_TYPE
855 }