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