ACPI / EC: Work around method reentrancy limit in ACPICA for _Qxx
[cascardo/linux.git] / drivers / staging / lustre / lnet / lnet / api-ni.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2015, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #define DEBUG_SUBSYSTEM S_LNET
38 #include <linux/log2.h>
39 #include <linux/ktime.h>
40
41 #include "../../include/linux/lnet/lib-lnet.h"
42 #include "../../include/linux/lnet/lib-dlc.h"
43
44 #define D_LNI D_CONSOLE
45
46 lnet_t the_lnet;                           /* THE state of the network */
47 EXPORT_SYMBOL(the_lnet);
48
49 static char *ip2nets = "";
50 module_param(ip2nets, charp, 0444);
51 MODULE_PARM_DESC(ip2nets, "LNET network <- IP table");
52
53 static char *networks = "";
54 module_param(networks, charp, 0444);
55 MODULE_PARM_DESC(networks, "local networks");
56
57 static char *routes = "";
58 module_param(routes, charp, 0444);
59 MODULE_PARM_DESC(routes, "routes to non-local networks");
60
61 static int rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
62 module_param(rnet_htable_size, int, 0444);
63 MODULE_PARM_DESC(rnet_htable_size, "size of remote network hash table");
64
65 static int lnet_ping(lnet_process_id_t id, int timeout_ms,
66                      lnet_process_id_t __user *ids, int n_ids);
67
68 static char *
69 lnet_get_routes(void)
70 {
71         return routes;
72 }
73
74 static char *
75 lnet_get_networks(void)
76 {
77         char *nets;
78         int rc;
79
80         if (*networks && *ip2nets) {
81                 LCONSOLE_ERROR_MSG(0x101, "Please specify EITHER 'networks' or 'ip2nets' but not both at once\n");
82                 return NULL;
83         }
84
85         if (*ip2nets) {
86                 rc = lnet_parse_ip2nets(&nets, ip2nets);
87                 return !rc ? nets : NULL;
88         }
89
90         if (*networks)
91                 return networks;
92
93         return "tcp";
94 }
95
96 static void
97 lnet_init_locks(void)
98 {
99         spin_lock_init(&the_lnet.ln_eq_wait_lock);
100         init_waitqueue_head(&the_lnet.ln_eq_waitq);
101         init_waitqueue_head(&the_lnet.ln_rc_waitq);
102         mutex_init(&the_lnet.ln_lnd_mutex);
103         mutex_init(&the_lnet.ln_api_mutex);
104 }
105
106 static int
107 lnet_create_remote_nets_table(void)
108 {
109         int i;
110         struct list_head *hash;
111
112         LASSERT(!the_lnet.ln_remote_nets_hash);
113         LASSERT(the_lnet.ln_remote_nets_hbits > 0);
114         LIBCFS_ALLOC(hash, LNET_REMOTE_NETS_HASH_SIZE * sizeof(*hash));
115         if (!hash) {
116                 CERROR("Failed to create remote nets hash table\n");
117                 return -ENOMEM;
118         }
119
120         for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
121                 INIT_LIST_HEAD(&hash[i]);
122         the_lnet.ln_remote_nets_hash = hash;
123         return 0;
124 }
125
126 static void
127 lnet_destroy_remote_nets_table(void)
128 {
129         int i;
130
131         if (!the_lnet.ln_remote_nets_hash)
132                 return;
133
134         for (i = 0; i < LNET_REMOTE_NETS_HASH_SIZE; i++)
135                 LASSERT(list_empty(&the_lnet.ln_remote_nets_hash[i]));
136
137         LIBCFS_FREE(the_lnet.ln_remote_nets_hash,
138                     LNET_REMOTE_NETS_HASH_SIZE *
139                     sizeof(the_lnet.ln_remote_nets_hash[0]));
140         the_lnet.ln_remote_nets_hash = NULL;
141 }
142
143 static void
144 lnet_destroy_locks(void)
145 {
146         if (the_lnet.ln_res_lock) {
147                 cfs_percpt_lock_free(the_lnet.ln_res_lock);
148                 the_lnet.ln_res_lock = NULL;
149         }
150
151         if (the_lnet.ln_net_lock) {
152                 cfs_percpt_lock_free(the_lnet.ln_net_lock);
153                 the_lnet.ln_net_lock = NULL;
154         }
155 }
156
157 static int
158 lnet_create_locks(void)
159 {
160         lnet_init_locks();
161
162         the_lnet.ln_res_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
163         if (!the_lnet.ln_res_lock)
164                 goto failed;
165
166         the_lnet.ln_net_lock = cfs_percpt_lock_alloc(lnet_cpt_table());
167         if (!the_lnet.ln_net_lock)
168                 goto failed;
169
170         return 0;
171
172  failed:
173         lnet_destroy_locks();
174         return -ENOMEM;
175 }
176
177 static void lnet_assert_wire_constants(void)
178 {
179         /*
180          * Wire protocol assertions generated by 'wirecheck'
181          * running on Linux robert.bartonsoftware.com 2.6.8-1.521
182          * #1 Mon Aug 16 09:01:18 EDT 2004 i686 athlon i386 GNU/Linux
183          * with gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7)
184          */
185
186         /* Constants... */
187         CLASSERT(LNET_PROTO_TCP_MAGIC == 0xeebc0ded);
188         CLASSERT(LNET_PROTO_TCP_VERSION_MAJOR == 1);
189         CLASSERT(LNET_PROTO_TCP_VERSION_MINOR == 0);
190         CLASSERT(LNET_MSG_ACK == 0);
191         CLASSERT(LNET_MSG_PUT == 1);
192         CLASSERT(LNET_MSG_GET == 2);
193         CLASSERT(LNET_MSG_REPLY == 3);
194         CLASSERT(LNET_MSG_HELLO == 4);
195
196         /* Checks for struct ptl_handle_wire_t */
197         CLASSERT((int)sizeof(lnet_handle_wire_t) == 16);
198         CLASSERT((int)offsetof(lnet_handle_wire_t, wh_interface_cookie) == 0);
199         CLASSERT((int)sizeof(((lnet_handle_wire_t *)0)->wh_interface_cookie) == 8);
200         CLASSERT((int)offsetof(lnet_handle_wire_t, wh_object_cookie) == 8);
201         CLASSERT((int)sizeof(((lnet_handle_wire_t *)0)->wh_object_cookie) == 8);
202
203         /* Checks for struct lnet_magicversion_t */
204         CLASSERT((int)sizeof(lnet_magicversion_t) == 8);
205         CLASSERT((int)offsetof(lnet_magicversion_t, magic) == 0);
206         CLASSERT((int)sizeof(((lnet_magicversion_t *)0)->magic) == 4);
207         CLASSERT((int)offsetof(lnet_magicversion_t, version_major) == 4);
208         CLASSERT((int)sizeof(((lnet_magicversion_t *)0)->version_major) == 2);
209         CLASSERT((int)offsetof(lnet_magicversion_t, version_minor) == 6);
210         CLASSERT((int)sizeof(((lnet_magicversion_t *)0)->version_minor) == 2);
211
212         /* Checks for struct lnet_hdr_t */
213         CLASSERT((int)sizeof(lnet_hdr_t) == 72);
214         CLASSERT((int)offsetof(lnet_hdr_t, dest_nid) == 0);
215         CLASSERT((int)sizeof(((lnet_hdr_t *)0)->dest_nid) == 8);
216         CLASSERT((int)offsetof(lnet_hdr_t, src_nid) == 8);
217         CLASSERT((int)sizeof(((lnet_hdr_t *)0)->src_nid) == 8);
218         CLASSERT((int)offsetof(lnet_hdr_t, dest_pid) == 16);
219         CLASSERT((int)sizeof(((lnet_hdr_t *)0)->dest_pid) == 4);
220         CLASSERT((int)offsetof(lnet_hdr_t, src_pid) == 20);
221         CLASSERT((int)sizeof(((lnet_hdr_t *)0)->src_pid) == 4);
222         CLASSERT((int)offsetof(lnet_hdr_t, type) == 24);
223         CLASSERT((int)sizeof(((lnet_hdr_t *)0)->type) == 4);
224         CLASSERT((int)offsetof(lnet_hdr_t, payload_length) == 28);
225         CLASSERT((int)sizeof(((lnet_hdr_t *)0)->payload_length) == 4);
226         CLASSERT((int)offsetof(lnet_hdr_t, msg) == 32);
227         CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg) == 40);
228
229         /* Ack */
230         CLASSERT((int)offsetof(lnet_hdr_t, msg.ack.dst_wmd) == 32);
231         CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.ack.dst_wmd) == 16);
232         CLASSERT((int)offsetof(lnet_hdr_t, msg.ack.match_bits) == 48);
233         CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.ack.match_bits) == 8);
234         CLASSERT((int)offsetof(lnet_hdr_t, msg.ack.mlength) == 56);
235         CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.ack.mlength) == 4);
236
237         /* Put */
238         CLASSERT((int)offsetof(lnet_hdr_t, msg.put.ack_wmd) == 32);
239         CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.put.ack_wmd) == 16);
240         CLASSERT((int)offsetof(lnet_hdr_t, msg.put.match_bits) == 48);
241         CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.put.match_bits) == 8);
242         CLASSERT((int)offsetof(lnet_hdr_t, msg.put.hdr_data) == 56);
243         CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.put.hdr_data) == 8);
244         CLASSERT((int)offsetof(lnet_hdr_t, msg.put.ptl_index) == 64);
245         CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.put.ptl_index) == 4);
246         CLASSERT((int)offsetof(lnet_hdr_t, msg.put.offset) == 68);
247         CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.put.offset) == 4);
248
249         /* Get */
250         CLASSERT((int)offsetof(lnet_hdr_t, msg.get.return_wmd) == 32);
251         CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.get.return_wmd) == 16);
252         CLASSERT((int)offsetof(lnet_hdr_t, msg.get.match_bits) == 48);
253         CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.get.match_bits) == 8);
254         CLASSERT((int)offsetof(lnet_hdr_t, msg.get.ptl_index) == 56);
255         CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.get.ptl_index) == 4);
256         CLASSERT((int)offsetof(lnet_hdr_t, msg.get.src_offset) == 60);
257         CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.get.src_offset) == 4);
258         CLASSERT((int)offsetof(lnet_hdr_t, msg.get.sink_length) == 64);
259         CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.get.sink_length) == 4);
260
261         /* Reply */
262         CLASSERT((int)offsetof(lnet_hdr_t, msg.reply.dst_wmd) == 32);
263         CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.reply.dst_wmd) == 16);
264
265         /* Hello */
266         CLASSERT((int)offsetof(lnet_hdr_t, msg.hello.incarnation) == 32);
267         CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.hello.incarnation) == 8);
268         CLASSERT((int)offsetof(lnet_hdr_t, msg.hello.type) == 40);
269         CLASSERT((int)sizeof(((lnet_hdr_t *)0)->msg.hello.type) == 4);
270 }
271
272 static lnd_t *
273 lnet_find_lnd_by_type(__u32 type)
274 {
275         lnd_t *lnd;
276         struct list_head *tmp;
277
278         /* holding lnd mutex */
279         list_for_each(tmp, &the_lnet.ln_lnds) {
280                 lnd = list_entry(tmp, lnd_t, lnd_list);
281
282                 if (lnd->lnd_type == type)
283                         return lnd;
284         }
285
286         return NULL;
287 }
288
289 void
290 lnet_register_lnd(lnd_t *lnd)
291 {
292         mutex_lock(&the_lnet.ln_lnd_mutex);
293
294         LASSERT(libcfs_isknown_lnd(lnd->lnd_type));
295         LASSERT(!lnet_find_lnd_by_type(lnd->lnd_type));
296
297         list_add_tail(&lnd->lnd_list, &the_lnet.ln_lnds);
298         lnd->lnd_refcount = 0;
299
300         CDEBUG(D_NET, "%s LND registered\n", libcfs_lnd2str(lnd->lnd_type));
301
302         mutex_unlock(&the_lnet.ln_lnd_mutex);
303 }
304 EXPORT_SYMBOL(lnet_register_lnd);
305
306 void
307 lnet_unregister_lnd(lnd_t *lnd)
308 {
309         mutex_lock(&the_lnet.ln_lnd_mutex);
310
311         LASSERT(lnet_find_lnd_by_type(lnd->lnd_type) == lnd);
312         LASSERT(!lnd->lnd_refcount);
313
314         list_del(&lnd->lnd_list);
315         CDEBUG(D_NET, "%s LND unregistered\n", libcfs_lnd2str(lnd->lnd_type));
316
317         mutex_unlock(&the_lnet.ln_lnd_mutex);
318 }
319 EXPORT_SYMBOL(lnet_unregister_lnd);
320
321 void
322 lnet_counters_get(lnet_counters_t *counters)
323 {
324         lnet_counters_t *ctr;
325         int i;
326
327         memset(counters, 0, sizeof(*counters));
328
329         lnet_net_lock(LNET_LOCK_EX);
330
331         cfs_percpt_for_each(ctr, i, the_lnet.ln_counters) {
332                 counters->msgs_max     += ctr->msgs_max;
333                 counters->msgs_alloc   += ctr->msgs_alloc;
334                 counters->errors       += ctr->errors;
335                 counters->send_count   += ctr->send_count;
336                 counters->recv_count   += ctr->recv_count;
337                 counters->route_count  += ctr->route_count;
338                 counters->drop_count   += ctr->drop_count;
339                 counters->send_length  += ctr->send_length;
340                 counters->recv_length  += ctr->recv_length;
341                 counters->route_length += ctr->route_length;
342                 counters->drop_length  += ctr->drop_length;
343         }
344         lnet_net_unlock(LNET_LOCK_EX);
345 }
346 EXPORT_SYMBOL(lnet_counters_get);
347
348 void
349 lnet_counters_reset(void)
350 {
351         lnet_counters_t *counters;
352         int i;
353
354         lnet_net_lock(LNET_LOCK_EX);
355
356         cfs_percpt_for_each(counters, i, the_lnet.ln_counters)
357                 memset(counters, 0, sizeof(lnet_counters_t));
358
359         lnet_net_unlock(LNET_LOCK_EX);
360 }
361
362 static char *
363 lnet_res_type2str(int type)
364 {
365         switch (type) {
366         default:
367                 LBUG();
368         case LNET_COOKIE_TYPE_MD:
369                 return "MD";
370         case LNET_COOKIE_TYPE_ME:
371                 return "ME";
372         case LNET_COOKIE_TYPE_EQ:
373                 return "EQ";
374         }
375 }
376
377 static void
378 lnet_res_container_cleanup(struct lnet_res_container *rec)
379 {
380         int count = 0;
381
382         if (!rec->rec_type) /* not set yet, it's uninitialized */
383                 return;
384
385         while (!list_empty(&rec->rec_active)) {
386                 struct list_head *e = rec->rec_active.next;
387
388                 list_del_init(e);
389                 if (rec->rec_type == LNET_COOKIE_TYPE_EQ) {
390                         lnet_eq_free(list_entry(e, lnet_eq_t, eq_list));
391
392                 } else if (rec->rec_type == LNET_COOKIE_TYPE_MD) {
393                         lnet_md_free(list_entry(e, lnet_libmd_t, md_list));
394
395                 } else { /* NB: Active MEs should be attached on portals */
396                         LBUG();
397                 }
398                 count++;
399         }
400
401         if (count > 0) {
402                 /*
403                  * Found alive MD/ME/EQ, user really should unlink/free
404                  * all of them before finalize LNet, but if someone didn't,
405                  * we have to recycle garbage for him
406                  */
407                 CERROR("%d active elements on exit of %s container\n",
408                        count, lnet_res_type2str(rec->rec_type));
409         }
410
411         if (rec->rec_lh_hash) {
412                 LIBCFS_FREE(rec->rec_lh_hash,
413                             LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
414                 rec->rec_lh_hash = NULL;
415         }
416
417         rec->rec_type = 0; /* mark it as finalized */
418 }
419
420 static int
421 lnet_res_container_setup(struct lnet_res_container *rec, int cpt, int type)
422 {
423         int rc = 0;
424         int i;
425
426         LASSERT(!rec->rec_type);
427
428         rec->rec_type = type;
429         INIT_LIST_HEAD(&rec->rec_active);
430         rec->rec_lh_cookie = (cpt << LNET_COOKIE_TYPE_BITS) | type;
431
432         /* Arbitrary choice of hash table size */
433         LIBCFS_CPT_ALLOC(rec->rec_lh_hash, lnet_cpt_table(), cpt,
434                          LNET_LH_HASH_SIZE * sizeof(rec->rec_lh_hash[0]));
435         if (!rec->rec_lh_hash) {
436                 rc = -ENOMEM;
437                 goto out;
438         }
439
440         for (i = 0; i < LNET_LH_HASH_SIZE; i++)
441                 INIT_LIST_HEAD(&rec->rec_lh_hash[i]);
442
443         return 0;
444
445 out:
446         CERROR("Failed to setup %s resource container\n",
447                lnet_res_type2str(type));
448         lnet_res_container_cleanup(rec);
449         return rc;
450 }
451
452 static void
453 lnet_res_containers_destroy(struct lnet_res_container **recs)
454 {
455         struct lnet_res_container *rec;
456         int i;
457
458         cfs_percpt_for_each(rec, i, recs)
459                 lnet_res_container_cleanup(rec);
460
461         cfs_percpt_free(recs);
462 }
463
464 static struct lnet_res_container **
465 lnet_res_containers_create(int type)
466 {
467         struct lnet_res_container **recs;
468         struct lnet_res_container *rec;
469         int rc;
470         int i;
471
472         recs = cfs_percpt_alloc(lnet_cpt_table(), sizeof(*rec));
473         if (!recs) {
474                 CERROR("Failed to allocate %s resource containers\n",
475                        lnet_res_type2str(type));
476                 return NULL;
477         }
478
479         cfs_percpt_for_each(rec, i, recs) {
480                 rc = lnet_res_container_setup(rec, i, type);
481                 if (rc) {
482                         lnet_res_containers_destroy(recs);
483                         return NULL;
484                 }
485         }
486
487         return recs;
488 }
489
490 lnet_libhandle_t *
491 lnet_res_lh_lookup(struct lnet_res_container *rec, __u64 cookie)
492 {
493         /* ALWAYS called with lnet_res_lock held */
494         struct list_head *head;
495         lnet_libhandle_t *lh;
496         unsigned int hash;
497
498         if ((cookie & LNET_COOKIE_MASK) != rec->rec_type)
499                 return NULL;
500
501         hash = cookie >> (LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS);
502         head = &rec->rec_lh_hash[hash & LNET_LH_HASH_MASK];
503
504         list_for_each_entry(lh, head, lh_hash_chain) {
505                 if (lh->lh_cookie == cookie)
506                         return lh;
507         }
508
509         return NULL;
510 }
511
512 void
513 lnet_res_lh_initialize(struct lnet_res_container *rec, lnet_libhandle_t *lh)
514 {
515         /* ALWAYS called with lnet_res_lock held */
516         unsigned int ibits = LNET_COOKIE_TYPE_BITS + LNET_CPT_BITS;
517         unsigned int hash;
518
519         lh->lh_cookie = rec->rec_lh_cookie;
520         rec->rec_lh_cookie += 1 << ibits;
521
522         hash = (lh->lh_cookie >> ibits) & LNET_LH_HASH_MASK;
523
524         list_add(&lh->lh_hash_chain, &rec->rec_lh_hash[hash]);
525 }
526
527 static int lnet_unprepare(void);
528
529 static int
530 lnet_prepare(lnet_pid_t requested_pid)
531 {
532         /* Prepare to bring up the network */
533         struct lnet_res_container **recs;
534         int rc = 0;
535
536         if (requested_pid == LNET_PID_ANY) {
537                 /* Don't instantiate LNET just for me */
538                 return -ENETDOWN;
539         }
540
541         LASSERT(!the_lnet.ln_refcount);
542
543         the_lnet.ln_routing = 0;
544
545         LASSERT(!(requested_pid & LNET_PID_USERFLAG));
546         the_lnet.ln_pid = requested_pid;
547
548         INIT_LIST_HEAD(&the_lnet.ln_test_peers);
549         INIT_LIST_HEAD(&the_lnet.ln_nis);
550         INIT_LIST_HEAD(&the_lnet.ln_nis_cpt);
551         INIT_LIST_HEAD(&the_lnet.ln_nis_zombie);
552         INIT_LIST_HEAD(&the_lnet.ln_routers);
553         INIT_LIST_HEAD(&the_lnet.ln_drop_rules);
554         INIT_LIST_HEAD(&the_lnet.ln_delay_rules);
555
556         rc = lnet_create_remote_nets_table();
557         if (rc)
558                 goto failed;
559         /*
560          * NB the interface cookie in wire handles guards against delayed
561          * replies and ACKs appearing valid after reboot.
562          */
563         the_lnet.ln_interface_cookie = ktime_get_ns();
564
565         the_lnet.ln_counters = cfs_percpt_alloc(lnet_cpt_table(),
566                                                 sizeof(lnet_counters_t));
567         if (!the_lnet.ln_counters) {
568                 CERROR("Failed to allocate counters for LNet\n");
569                 rc = -ENOMEM;
570                 goto failed;
571         }
572
573         rc = lnet_peer_tables_create();
574         if (rc)
575                 goto failed;
576
577         rc = lnet_msg_containers_create();
578         if (rc)
579                 goto failed;
580
581         rc = lnet_res_container_setup(&the_lnet.ln_eq_container, 0,
582                                       LNET_COOKIE_TYPE_EQ);
583         if (rc)
584                 goto failed;
585
586         recs = lnet_res_containers_create(LNET_COOKIE_TYPE_ME);
587         if (!recs) {
588                 rc = -ENOMEM;
589                 goto failed;
590         }
591
592         the_lnet.ln_me_containers = recs;
593
594         recs = lnet_res_containers_create(LNET_COOKIE_TYPE_MD);
595         if (!recs) {
596                 rc = -ENOMEM;
597                 goto failed;
598         }
599
600         the_lnet.ln_md_containers = recs;
601
602         rc = lnet_portals_create();
603         if (rc) {
604                 CERROR("Failed to create portals for LNet: %d\n", rc);
605                 goto failed;
606         }
607
608         return 0;
609
610  failed:
611         lnet_unprepare();
612         return rc;
613 }
614
615 static int
616 lnet_unprepare(void)
617 {
618         /*
619          * NB no LNET_LOCK since this is the last reference.  All LND instances
620          * have shut down already, so it is safe to unlink and free all
621          * descriptors, even those that appear committed to a network op (eg MD
622          * with non-zero pending count)
623          */
624         lnet_fail_nid(LNET_NID_ANY, 0);
625
626         LASSERT(!the_lnet.ln_refcount);
627         LASSERT(list_empty(&the_lnet.ln_test_peers));
628         LASSERT(list_empty(&the_lnet.ln_nis));
629         LASSERT(list_empty(&the_lnet.ln_nis_cpt));
630         LASSERT(list_empty(&the_lnet.ln_nis_zombie));
631
632         lnet_portals_destroy();
633
634         if (the_lnet.ln_md_containers) {
635                 lnet_res_containers_destroy(the_lnet.ln_md_containers);
636                 the_lnet.ln_md_containers = NULL;
637         }
638
639         if (the_lnet.ln_me_containers) {
640                 lnet_res_containers_destroy(the_lnet.ln_me_containers);
641                 the_lnet.ln_me_containers = NULL;
642         }
643
644         lnet_res_container_cleanup(&the_lnet.ln_eq_container);
645
646         lnet_msg_containers_destroy();
647         lnet_peer_tables_destroy();
648         lnet_rtrpools_free(0);
649
650         if (the_lnet.ln_counters) {
651                 cfs_percpt_free(the_lnet.ln_counters);
652                 the_lnet.ln_counters = NULL;
653         }
654         lnet_destroy_remote_nets_table();
655
656         return 0;
657 }
658
659 lnet_ni_t  *
660 lnet_net2ni_locked(__u32 net, int cpt)
661 {
662         struct list_head *tmp;
663         lnet_ni_t *ni;
664
665         LASSERT(cpt != LNET_LOCK_EX);
666
667         list_for_each(tmp, &the_lnet.ln_nis) {
668                 ni = list_entry(tmp, lnet_ni_t, ni_list);
669
670                 if (LNET_NIDNET(ni->ni_nid) == net) {
671                         lnet_ni_addref_locked(ni, cpt);
672                         return ni;
673                 }
674         }
675
676         return NULL;
677 }
678
679 lnet_ni_t *
680 lnet_net2ni(__u32 net)
681 {
682         lnet_ni_t *ni;
683
684         lnet_net_lock(0);
685         ni = lnet_net2ni_locked(net, 0);
686         lnet_net_unlock(0);
687
688         return ni;
689 }
690 EXPORT_SYMBOL(lnet_net2ni);
691
692 static unsigned int
693 lnet_nid_cpt_hash(lnet_nid_t nid, unsigned int number)
694 {
695         __u64 key = nid;
696         unsigned int val;
697
698         LASSERT(number >= 1 && number <= LNET_CPT_NUMBER);
699
700         if (number == 1)
701                 return 0;
702
703         val = hash_long(key, LNET_CPT_BITS);
704         /* NB: LNET_CP_NUMBER doesn't have to be PO2 */
705         if (val < number)
706                 return val;
707
708         return (unsigned int)(key + val + (val >> 1)) % number;
709 }
710
711 int
712 lnet_cpt_of_nid_locked(lnet_nid_t nid)
713 {
714         struct lnet_ni *ni;
715
716         /* must called with hold of lnet_net_lock */
717         if (LNET_CPT_NUMBER == 1)
718                 return 0; /* the only one */
719
720         /* take lnet_net_lock(any) would be OK */
721         if (!list_empty(&the_lnet.ln_nis_cpt)) {
722                 list_for_each_entry(ni, &the_lnet.ln_nis_cpt, ni_cptlist) {
723                         if (LNET_NIDNET(ni->ni_nid) != LNET_NIDNET(nid))
724                                 continue;
725
726                         LASSERT(ni->ni_cpts);
727                         return ni->ni_cpts[lnet_nid_cpt_hash
728                                            (nid, ni->ni_ncpts)];
729                 }
730         }
731
732         return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
733 }
734
735 int
736 lnet_cpt_of_nid(lnet_nid_t nid)
737 {
738         int cpt;
739         int cpt2;
740
741         if (LNET_CPT_NUMBER == 1)
742                 return 0; /* the only one */
743
744         if (list_empty(&the_lnet.ln_nis_cpt))
745                 return lnet_nid_cpt_hash(nid, LNET_CPT_NUMBER);
746
747         cpt = lnet_net_lock_current();
748         cpt2 = lnet_cpt_of_nid_locked(nid);
749         lnet_net_unlock(cpt);
750
751         return cpt2;
752 }
753 EXPORT_SYMBOL(lnet_cpt_of_nid);
754
755 int
756 lnet_islocalnet(__u32 net)
757 {
758         struct lnet_ni *ni;
759         int cpt;
760
761         cpt = lnet_net_lock_current();
762
763         ni = lnet_net2ni_locked(net, cpt);
764         if (ni)
765                 lnet_ni_decref_locked(ni, cpt);
766
767         lnet_net_unlock(cpt);
768
769         return !!ni;
770 }
771
772 lnet_ni_t  *
773 lnet_nid2ni_locked(lnet_nid_t nid, int cpt)
774 {
775         struct lnet_ni *ni;
776         struct list_head *tmp;
777
778         LASSERT(cpt != LNET_LOCK_EX);
779
780         list_for_each(tmp, &the_lnet.ln_nis) {
781                 ni = list_entry(tmp, lnet_ni_t, ni_list);
782
783                 if (ni->ni_nid == nid) {
784                         lnet_ni_addref_locked(ni, cpt);
785                         return ni;
786                 }
787         }
788
789         return NULL;
790 }
791
792 int
793 lnet_islocalnid(lnet_nid_t nid)
794 {
795         struct lnet_ni *ni;
796         int cpt;
797
798         cpt = lnet_net_lock_current();
799         ni = lnet_nid2ni_locked(nid, cpt);
800         if (ni)
801                 lnet_ni_decref_locked(ni, cpt);
802         lnet_net_unlock(cpt);
803
804         return !!ni;
805 }
806
807 int
808 lnet_count_acceptor_nis(void)
809 {
810         /* Return the # of NIs that need the acceptor. */
811         int count = 0;
812         struct list_head *tmp;
813         struct lnet_ni *ni;
814         int cpt;
815
816         cpt = lnet_net_lock_current();
817         list_for_each(tmp, &the_lnet.ln_nis) {
818                 ni = list_entry(tmp, lnet_ni_t, ni_list);
819
820                 if (ni->ni_lnd->lnd_accept)
821                         count++;
822         }
823
824         lnet_net_unlock(cpt);
825
826         return count;
827 }
828
829 static lnet_ping_info_t *
830 lnet_ping_info_create(int num_ni)
831 {
832         lnet_ping_info_t *ping_info;
833         unsigned int infosz;
834
835         infosz = offsetof(lnet_ping_info_t, pi_ni[num_ni]);
836         LIBCFS_ALLOC(ping_info, infosz);
837         if (!ping_info) {
838                 CERROR("Can't allocate ping info[%d]\n", num_ni);
839                 return NULL;
840         }
841
842         ping_info->pi_nnis = num_ni;
843         ping_info->pi_pid = the_lnet.ln_pid;
844         ping_info->pi_magic = LNET_PROTO_PING_MAGIC;
845         ping_info->pi_features = LNET_PING_FEAT_NI_STATUS;
846
847         return ping_info;
848 }
849
850 static inline int
851 lnet_get_ni_count(void)
852 {
853         struct lnet_ni *ni;
854         int count = 0;
855
856         lnet_net_lock(0);
857
858         list_for_each_entry(ni, &the_lnet.ln_nis, ni_list)
859                 count++;
860
861         lnet_net_unlock(0);
862
863         return count;
864 }
865
866 static inline void
867 lnet_ping_info_free(lnet_ping_info_t *pinfo)
868 {
869         LIBCFS_FREE(pinfo,
870                     offsetof(lnet_ping_info_t,
871                              pi_ni[pinfo->pi_nnis]));
872 }
873
874 static void
875 lnet_ping_info_destroy(void)
876 {
877         struct lnet_ni *ni;
878
879         lnet_net_lock(LNET_LOCK_EX);
880
881         list_for_each_entry(ni, &the_lnet.ln_nis, ni_list) {
882                 lnet_ni_lock(ni);
883                 ni->ni_status = NULL;
884                 lnet_ni_unlock(ni);
885         }
886
887         lnet_ping_info_free(the_lnet.ln_ping_info);
888         the_lnet.ln_ping_info = NULL;
889
890         lnet_net_unlock(LNET_LOCK_EX);
891 }
892
893 static void
894 lnet_ping_event_handler(lnet_event_t *event)
895 {
896         lnet_ping_info_t *pinfo = event->md.user_ptr;
897
898         if (event->unlinked)
899                 pinfo->pi_features = LNET_PING_FEAT_INVAL;
900 }
901
902 static int
903 lnet_ping_info_setup(lnet_ping_info_t **ppinfo, lnet_handle_md_t *md_handle,
904                      int ni_count, bool set_eq)
905 {
906         lnet_process_id_t id = {LNET_NID_ANY, LNET_PID_ANY};
907         lnet_handle_me_t me_handle;
908         lnet_md_t md = { NULL };
909         int rc, rc2;
910
911         if (set_eq) {
912                 rc = LNetEQAlloc(0, lnet_ping_event_handler,
913                                  &the_lnet.ln_ping_target_eq);
914                 if (rc) {
915                         CERROR("Can't allocate ping EQ: %d\n", rc);
916                         return rc;
917                 }
918         }
919
920         *ppinfo = lnet_ping_info_create(ni_count);
921         if (!*ppinfo) {
922                 rc = -ENOMEM;
923                 goto failed_0;
924         }
925
926         rc = LNetMEAttach(LNET_RESERVED_PORTAL, id,
927                           LNET_PROTO_PING_MATCHBITS, 0,
928                           LNET_UNLINK, LNET_INS_AFTER,
929                           &me_handle);
930         if (rc) {
931                 CERROR("Can't create ping ME: %d\n", rc);
932                 goto failed_1;
933         }
934
935         /* initialize md content */
936         md.start = *ppinfo;
937         md.length = offsetof(lnet_ping_info_t,
938                              pi_ni[(*ppinfo)->pi_nnis]);
939         md.threshold = LNET_MD_THRESH_INF;
940         md.max_size = 0;
941         md.options = LNET_MD_OP_GET | LNET_MD_TRUNCATE |
942                      LNET_MD_MANAGE_REMOTE;
943         md.user_ptr  = NULL;
944         md.eq_handle = the_lnet.ln_ping_target_eq;
945         md.user_ptr = *ppinfo;
946
947         rc = LNetMDAttach(me_handle, md, LNET_RETAIN, md_handle);
948         if (rc) {
949                 CERROR("Can't attach ping MD: %d\n", rc);
950                 goto failed_2;
951         }
952
953         return 0;
954
955 failed_2:
956         rc2 = LNetMEUnlink(me_handle);
957         LASSERT(!rc2);
958 failed_1:
959         lnet_ping_info_free(*ppinfo);
960         *ppinfo = NULL;
961 failed_0:
962         if (set_eq)
963                 LNetEQFree(the_lnet.ln_ping_target_eq);
964         return rc;
965 }
966
967 static void
968 lnet_ping_md_unlink(lnet_ping_info_t *pinfo, lnet_handle_md_t *md_handle)
969 {
970         sigset_t blocked = cfs_block_allsigs();
971
972         LNetMDUnlink(*md_handle);
973         LNetInvalidateHandle(md_handle);
974
975         /* NB md could be busy; this just starts the unlink */
976         while (pinfo->pi_features != LNET_PING_FEAT_INVAL) {
977                 CDEBUG(D_NET, "Still waiting for ping MD to unlink\n");
978                 set_current_state(TASK_UNINTERRUPTIBLE);
979                 schedule_timeout(cfs_time_seconds(1));
980         }
981
982         cfs_restore_sigs(blocked);
983 }
984
985 static void
986 lnet_ping_info_install_locked(lnet_ping_info_t *ping_info)
987 {
988         lnet_ni_status_t *ns;
989         lnet_ni_t *ni;
990         int i = 0;
991
992         list_for_each_entry(ni, &the_lnet.ln_nis, ni_list) {
993                 LASSERT(i < ping_info->pi_nnis);
994
995                 ns = &ping_info->pi_ni[i];
996
997                 ns->ns_nid = ni->ni_nid;
998
999                 lnet_ni_lock(ni);
1000                 ns->ns_status = (ni->ni_status) ?
1001                                  ni->ni_status->ns_status : LNET_NI_STATUS_UP;
1002                 ni->ni_status = ns;
1003                 lnet_ni_unlock(ni);
1004
1005                 i++;
1006         }
1007 }
1008
1009 static void
1010 lnet_ping_target_update(lnet_ping_info_t *pinfo, lnet_handle_md_t md_handle)
1011 {
1012         lnet_ping_info_t *old_pinfo = NULL;
1013         lnet_handle_md_t old_md;
1014
1015         /* switch the NIs to point to the new ping info created */
1016         lnet_net_lock(LNET_LOCK_EX);
1017
1018         if (!the_lnet.ln_routing)
1019                 pinfo->pi_features |= LNET_PING_FEAT_RTE_DISABLED;
1020         lnet_ping_info_install_locked(pinfo);
1021
1022         if (the_lnet.ln_ping_info) {
1023                 old_pinfo = the_lnet.ln_ping_info;
1024                 old_md = the_lnet.ln_ping_target_md;
1025         }
1026         the_lnet.ln_ping_target_md = md_handle;
1027         the_lnet.ln_ping_info = pinfo;
1028
1029         lnet_net_unlock(LNET_LOCK_EX);
1030
1031         if (old_pinfo) {
1032                 /* unlink the old ping info */
1033                 lnet_ping_md_unlink(old_pinfo, &old_md);
1034                 lnet_ping_info_free(old_pinfo);
1035         }
1036 }
1037
1038 static void
1039 lnet_ping_target_fini(void)
1040 {
1041         int rc;
1042
1043         lnet_ping_md_unlink(the_lnet.ln_ping_info,
1044                             &the_lnet.ln_ping_target_md);
1045
1046         rc = LNetEQFree(the_lnet.ln_ping_target_eq);
1047         LASSERT(!rc);
1048
1049         lnet_ping_info_destroy();
1050 }
1051
1052 static int
1053 lnet_ni_tq_credits(lnet_ni_t *ni)
1054 {
1055         int credits;
1056
1057         LASSERT(ni->ni_ncpts >= 1);
1058
1059         if (ni->ni_ncpts == 1)
1060                 return ni->ni_maxtxcredits;
1061
1062         credits = ni->ni_maxtxcredits / ni->ni_ncpts;
1063         credits = max(credits, 8 * ni->ni_peertxcredits);
1064         credits = min(credits, ni->ni_maxtxcredits);
1065
1066         return credits;
1067 }
1068
1069 static void
1070 lnet_ni_unlink_locked(lnet_ni_t *ni)
1071 {
1072         if (!list_empty(&ni->ni_cptlist)) {
1073                 list_del_init(&ni->ni_cptlist);
1074                 lnet_ni_decref_locked(ni, 0);
1075         }
1076
1077         /* move it to zombie list and nobody can find it anymore */
1078         LASSERT(!list_empty(&ni->ni_list));
1079         list_move(&ni->ni_list, &the_lnet.ln_nis_zombie);
1080         lnet_ni_decref_locked(ni, 0);   /* drop ln_nis' ref */
1081 }
1082
1083 static void
1084 lnet_clear_zombies_nis_locked(void)
1085 {
1086         int i;
1087         int islo;
1088         lnet_ni_t *ni;
1089         lnet_ni_t *temp;
1090
1091         /*
1092          * Now wait for the NI's I just nuked to show up on ln_zombie_nis
1093          * and shut them down in guaranteed thread context
1094          */
1095         i = 2;
1096         list_for_each_entry_safe(ni, temp, &the_lnet.ln_nis_zombie, ni_list) {
1097                 int *ref;
1098                 int j;
1099
1100                 list_del_init(&ni->ni_list);
1101                 cfs_percpt_for_each(ref, j, ni->ni_refs) {
1102                         if (!*ref)
1103                                 continue;
1104                         /* still busy, add it back to zombie list */
1105                         list_add(&ni->ni_list, &the_lnet.ln_nis_zombie);
1106                         break;
1107                 }
1108
1109                 if (!list_empty(&ni->ni_list)) {
1110                         lnet_net_unlock(LNET_LOCK_EX);
1111                         ++i;
1112                         if ((i & (-i)) == i) {
1113                                 CDEBUG(D_WARNING, "Waiting for zombie LNI %s\n",
1114                                        libcfs_nid2str(ni->ni_nid));
1115                         }
1116                         set_current_state(TASK_UNINTERRUPTIBLE);
1117                         schedule_timeout(cfs_time_seconds(1));
1118                         lnet_net_lock(LNET_LOCK_EX);
1119                         continue;
1120                 }
1121
1122                 ni->ni_lnd->lnd_refcount--;
1123                 lnet_net_unlock(LNET_LOCK_EX);
1124
1125                 islo = ni->ni_lnd->lnd_type == LOLND;
1126
1127                 LASSERT(!in_interrupt());
1128                 ni->ni_lnd->lnd_shutdown(ni);
1129
1130                 /*
1131                  * can't deref lnd anymore now; it might have unregistered
1132                  * itself...
1133                  */
1134                 if (!islo)
1135                         CDEBUG(D_LNI, "Removed LNI %s\n",
1136                                libcfs_nid2str(ni->ni_nid));
1137
1138                 lnet_ni_free(ni);
1139                 i = 2;
1140
1141                 lnet_net_lock(LNET_LOCK_EX);
1142         }
1143 }
1144
1145 static void
1146 lnet_shutdown_lndnis(void)
1147 {
1148         lnet_ni_t *ni;
1149         lnet_ni_t *temp;
1150         int i;
1151
1152         /* NB called holding the global mutex */
1153
1154         /* All quiet on the API front */
1155         LASSERT(!the_lnet.ln_shutdown);
1156         LASSERT(!the_lnet.ln_refcount);
1157         LASSERT(list_empty(&the_lnet.ln_nis_zombie));
1158
1159         lnet_net_lock(LNET_LOCK_EX);
1160         the_lnet.ln_shutdown = 1;       /* flag shutdown */
1161
1162         /* Unlink NIs from the global table */
1163         list_for_each_entry_safe(ni, temp, &the_lnet.ln_nis, ni_list) {
1164                 lnet_ni_unlink_locked(ni);
1165         }
1166
1167         /* Drop the cached loopback NI. */
1168         if (the_lnet.ln_loni) {
1169                 lnet_ni_decref_locked(the_lnet.ln_loni, 0);
1170                 the_lnet.ln_loni = NULL;
1171         }
1172
1173         lnet_net_unlock(LNET_LOCK_EX);
1174
1175         /*
1176          * Clear lazy portals and drop delayed messages which hold refs
1177          * on their lnet_msg_t::msg_rxpeer
1178          */
1179         for (i = 0; i < the_lnet.ln_nportals; i++)
1180                 LNetClearLazyPortal(i);
1181
1182         /*
1183          * Clear the peer table and wait for all peers to go (they hold refs on
1184          * their NIs)
1185          */
1186         lnet_peer_tables_cleanup(NULL);
1187
1188         lnet_net_lock(LNET_LOCK_EX);
1189
1190         lnet_clear_zombies_nis_locked();
1191         the_lnet.ln_shutdown = 0;
1192         lnet_net_unlock(LNET_LOCK_EX);
1193 }
1194
1195 /* shutdown down the NI and release refcount */
1196 static void
1197 lnet_shutdown_lndni(struct lnet_ni *ni)
1198 {
1199         int i;
1200
1201         lnet_net_lock(LNET_LOCK_EX);
1202         lnet_ni_unlink_locked(ni);
1203         lnet_net_unlock(LNET_LOCK_EX);
1204
1205         /* clear messages for this NI on the lazy portal */
1206         for (i = 0; i < the_lnet.ln_nportals; i++)
1207                 lnet_clear_lazy_portal(ni, i, "Shutting down NI");
1208
1209         /* Do peer table cleanup for this ni */
1210         lnet_peer_tables_cleanup(ni);
1211
1212         lnet_net_lock(LNET_LOCK_EX);
1213         lnet_clear_zombies_nis_locked();
1214         lnet_net_unlock(LNET_LOCK_EX);
1215 }
1216
1217 static int
1218 lnet_startup_lndni(struct lnet_ni *ni, struct lnet_ioctl_config_data *conf)
1219 {
1220         struct lnet_ioctl_config_lnd_tunables *lnd_tunables = NULL;
1221         int rc = -EINVAL;
1222         int lnd_type;
1223         lnd_t *lnd;
1224         struct lnet_tx_queue *tq;
1225         int i;
1226
1227         lnd_type = LNET_NETTYP(LNET_NIDNET(ni->ni_nid));
1228
1229         LASSERT(libcfs_isknown_lnd(lnd_type));
1230
1231         if (lnd_type == CIBLND || lnd_type == OPENIBLND ||
1232             lnd_type == IIBLND || lnd_type == VIBLND) {
1233                 CERROR("LND %s obsoleted\n", libcfs_lnd2str(lnd_type));
1234                 goto failed0;
1235         }
1236
1237         /* Make sure this new NI is unique. */
1238         lnet_net_lock(LNET_LOCK_EX);
1239         rc = lnet_net_unique(LNET_NIDNET(ni->ni_nid), &the_lnet.ln_nis);
1240         lnet_net_unlock(LNET_LOCK_EX);
1241         if (!rc) {
1242                 if (lnd_type == LOLND) {
1243                         lnet_ni_free(ni);
1244                         return 0;
1245                 }
1246
1247                 CERROR("Net %s is not unique\n",
1248                        libcfs_net2str(LNET_NIDNET(ni->ni_nid)));
1249                 rc = -EEXIST;
1250                 goto failed0;
1251         }
1252
1253         mutex_lock(&the_lnet.ln_lnd_mutex);
1254         lnd = lnet_find_lnd_by_type(lnd_type);
1255
1256         if (!lnd) {
1257                 mutex_unlock(&the_lnet.ln_lnd_mutex);
1258                 rc = request_module("%s", libcfs_lnd2modname(lnd_type));
1259                 mutex_lock(&the_lnet.ln_lnd_mutex);
1260
1261                 lnd = lnet_find_lnd_by_type(lnd_type);
1262                 if (!lnd) {
1263                         mutex_unlock(&the_lnet.ln_lnd_mutex);
1264                         CERROR("Can't load LND %s, module %s, rc=%d\n",
1265                                libcfs_lnd2str(lnd_type),
1266                                libcfs_lnd2modname(lnd_type), rc);
1267                         rc = -EINVAL;
1268                         goto failed0;
1269                 }
1270         }
1271
1272         lnet_net_lock(LNET_LOCK_EX);
1273         lnd->lnd_refcount++;
1274         lnet_net_unlock(LNET_LOCK_EX);
1275
1276         ni->ni_lnd = lnd;
1277
1278         if (conf && conf->cfg_hdr.ioc_len > sizeof(*conf))
1279                 lnd_tunables = (struct lnet_ioctl_config_lnd_tunables *)conf->cfg_bulk;
1280
1281         if (lnd_tunables) {
1282                 LIBCFS_ALLOC(ni->ni_lnd_tunables,
1283                              sizeof(*ni->ni_lnd_tunables));
1284                 if (!ni->ni_lnd_tunables) {
1285                         mutex_unlock(&the_lnet.ln_lnd_mutex);
1286                         rc = -ENOMEM;
1287                         goto failed0;
1288                 }
1289                 memcpy(ni->ni_lnd_tunables, lnd_tunables,
1290                        sizeof(*ni->ni_lnd_tunables));
1291         }
1292
1293         rc = lnd->lnd_startup(ni);
1294
1295         mutex_unlock(&the_lnet.ln_lnd_mutex);
1296
1297         if (rc) {
1298                 LCONSOLE_ERROR_MSG(0x105, "Error %d starting up LNI %s\n",
1299                                    rc, libcfs_lnd2str(lnd->lnd_type));
1300                 lnet_net_lock(LNET_LOCK_EX);
1301                 lnd->lnd_refcount--;
1302                 lnet_net_unlock(LNET_LOCK_EX);
1303                 goto failed0;
1304         }
1305
1306         /*
1307          * If given some LND tunable parameters, parse those now to
1308          * override the values in the NI structure.
1309          */
1310         if (conf && conf->cfg_config_u.cfg_net.net_peer_rtr_credits >= 0) {
1311                 ni->ni_peerrtrcredits =
1312                         conf->cfg_config_u.cfg_net.net_peer_rtr_credits;
1313         }
1314         if (conf && conf->cfg_config_u.cfg_net.net_peer_timeout >= 0) {
1315                 ni->ni_peertimeout =
1316                         conf->cfg_config_u.cfg_net.net_peer_timeout;
1317         }
1318         /*
1319          * TODO
1320          * Note: For now, don't allow the user to change
1321          * peertxcredits as this number is used in the
1322          * IB LND to control queue depth.
1323          *
1324          * if (conf && conf->cfg_config_u.cfg_net.net_peer_tx_credits != -1)
1325          *      ni->ni_peertxcredits =
1326          *              conf->cfg_config_u.cfg_net.net_peer_tx_credits;
1327          */
1328         if (conf && conf->cfg_config_u.cfg_net.net_max_tx_credits >= 0) {
1329                 ni->ni_maxtxcredits =
1330                         conf->cfg_config_u.cfg_net.net_max_tx_credits;
1331         }
1332
1333         LASSERT(ni->ni_peertimeout <= 0 || lnd->lnd_query);
1334
1335         lnet_net_lock(LNET_LOCK_EX);
1336         /* refcount for ln_nis */
1337         lnet_ni_addref_locked(ni, 0);
1338         list_add_tail(&ni->ni_list, &the_lnet.ln_nis);
1339         if (ni->ni_cpts) {
1340                 lnet_ni_addref_locked(ni, 0);
1341                 list_add_tail(&ni->ni_cptlist, &the_lnet.ln_nis_cpt);
1342         }
1343
1344         lnet_net_unlock(LNET_LOCK_EX);
1345
1346         if (lnd->lnd_type == LOLND) {
1347                 lnet_ni_addref(ni);
1348                 LASSERT(!the_lnet.ln_loni);
1349                 the_lnet.ln_loni = ni;
1350                 return 0;
1351         }
1352
1353         if (!ni->ni_peertxcredits || !ni->ni_maxtxcredits) {
1354                 LCONSOLE_ERROR_MSG(0x107, "LNI %s has no %scredits\n",
1355                                    libcfs_lnd2str(lnd->lnd_type),
1356                                    !ni->ni_peertxcredits ?
1357                                    "" : "per-peer ");
1358                 /*
1359                  * shutdown the NI since if we get here then it must've already
1360                  * been started
1361                  */
1362                 lnet_shutdown_lndni(ni);
1363                 return -EINVAL;
1364         }
1365
1366         cfs_percpt_for_each(tq, i, ni->ni_tx_queues) {
1367                 tq->tq_credits_min =
1368                 tq->tq_credits_max =
1369                 tq->tq_credits = lnet_ni_tq_credits(ni);
1370         }
1371
1372         CDEBUG(D_LNI, "Added LNI %s [%d/%d/%d/%d]\n",
1373                libcfs_nid2str(ni->ni_nid), ni->ni_peertxcredits,
1374                lnet_ni_tq_credits(ni) * LNET_CPT_NUMBER,
1375                ni->ni_peerrtrcredits, ni->ni_peertimeout);
1376
1377         return 0;
1378 failed0:
1379         lnet_ni_free(ni);
1380         return rc;
1381 }
1382
1383 static int
1384 lnet_startup_lndnis(struct list_head *nilist)
1385 {
1386         struct lnet_ni *ni;
1387         int rc;
1388         int ni_count = 0;
1389
1390         while (!list_empty(nilist)) {
1391                 ni = list_entry(nilist->next, lnet_ni_t, ni_list);
1392                 list_del(&ni->ni_list);
1393                 rc = lnet_startup_lndni(ni, NULL);
1394
1395                 if (rc < 0)
1396                         goto failed;
1397
1398                 ni_count++;
1399         }
1400
1401         return ni_count;
1402 failed:
1403         lnet_shutdown_lndnis();
1404
1405         return rc;
1406 }
1407
1408 /**
1409  * Initialize LNet library.
1410  *
1411  * Automatically called at module loading time. Caller has to call
1412  * lnet_lib_exit() after a call to lnet_lib_init(), if and only if the
1413  * latter returned 0. It must be called exactly once.
1414  *
1415  * \retval 0 on success
1416  * \retval -ve on failures.
1417  */
1418 int lnet_lib_init(void)
1419 {
1420         int rc;
1421
1422         lnet_assert_wire_constants();
1423
1424         memset(&the_lnet, 0, sizeof(the_lnet));
1425
1426         /* refer to global cfs_cpt_table for now */
1427         the_lnet.ln_cpt_table   = cfs_cpt_table;
1428         the_lnet.ln_cpt_number  = cfs_cpt_number(cfs_cpt_table);
1429
1430         LASSERT(the_lnet.ln_cpt_number > 0);
1431         if (the_lnet.ln_cpt_number > LNET_CPT_MAX) {
1432                 /* we are under risk of consuming all lh_cookie */
1433                 CERROR("Can't have %d CPTs for LNet (max allowed is %d), please change setting of CPT-table and retry\n",
1434                        the_lnet.ln_cpt_number, LNET_CPT_MAX);
1435                 return -E2BIG;
1436         }
1437
1438         while ((1 << the_lnet.ln_cpt_bits) < the_lnet.ln_cpt_number)
1439                 the_lnet.ln_cpt_bits++;
1440
1441         rc = lnet_create_locks();
1442         if (rc) {
1443                 CERROR("Can't create LNet global locks: %d\n", rc);
1444                 return rc;
1445         }
1446
1447         the_lnet.ln_refcount = 0;
1448         LNetInvalidateHandle(&the_lnet.ln_rc_eqh);
1449         INIT_LIST_HEAD(&the_lnet.ln_lnds);
1450         INIT_LIST_HEAD(&the_lnet.ln_rcd_zombie);
1451         INIT_LIST_HEAD(&the_lnet.ln_rcd_deathrow);
1452
1453         /*
1454          * The hash table size is the number of bits it takes to express the set
1455          * ln_num_routes, minus 1 (better to under estimate than over so we
1456          * don't waste memory).
1457          */
1458         if (rnet_htable_size <= 0)
1459                 rnet_htable_size = LNET_REMOTE_NETS_HASH_DEFAULT;
1460         else if (rnet_htable_size > LNET_REMOTE_NETS_HASH_MAX)
1461                 rnet_htable_size = LNET_REMOTE_NETS_HASH_MAX;
1462         the_lnet.ln_remote_nets_hbits = max_t(int, 1,
1463                                            order_base_2(rnet_htable_size) - 1);
1464
1465         /*
1466          * All LNDs apart from the LOLND are in separate modules.  They
1467          * register themselves when their module loads, and unregister
1468          * themselves when their module is unloaded.
1469          */
1470         lnet_register_lnd(&the_lolnd);
1471         return 0;
1472 }
1473
1474 /**
1475  * Finalize LNet library.
1476  *
1477  * \pre lnet_lib_init() called with success.
1478  * \pre All LNet users called LNetNIFini() for matching LNetNIInit() calls.
1479  */
1480 void lnet_lib_exit(void)
1481 {
1482         LASSERT(!the_lnet.ln_refcount);
1483
1484         while (!list_empty(&the_lnet.ln_lnds))
1485                 lnet_unregister_lnd(list_entry(the_lnet.ln_lnds.next,
1486                                                lnd_t, lnd_list));
1487         lnet_destroy_locks();
1488 }
1489
1490 /**
1491  * Set LNet PID and start LNet interfaces, routing, and forwarding.
1492  *
1493  * Users must call this function at least once before any other functions.
1494  * For each successful call there must be a corresponding call to
1495  * LNetNIFini(). For subsequent calls to LNetNIInit(), \a requested_pid is
1496  * ignored.
1497  *
1498  * The PID used by LNet may be different from the one requested.
1499  * See LNetGetId().
1500  *
1501  * \param requested_pid PID requested by the caller.
1502  *
1503  * \return >= 0 on success, and < 0 error code on failures.
1504  */
1505 int
1506 LNetNIInit(lnet_pid_t requested_pid)
1507 {
1508         int im_a_router = 0;
1509         int rc;
1510         int ni_count;
1511         lnet_ping_info_t *pinfo;
1512         lnet_handle_md_t md_handle;
1513         struct list_head net_head;
1514
1515         INIT_LIST_HEAD(&net_head);
1516
1517         mutex_lock(&the_lnet.ln_api_mutex);
1518
1519         CDEBUG(D_OTHER, "refs %d\n", the_lnet.ln_refcount);
1520
1521         if (the_lnet.ln_refcount > 0) {
1522                 rc = the_lnet.ln_refcount++;
1523                 mutex_unlock(&the_lnet.ln_api_mutex);
1524                 return rc;
1525         }
1526
1527         rc = lnet_prepare(requested_pid);
1528         if (rc) {
1529                 mutex_unlock(&the_lnet.ln_api_mutex);
1530                 return rc;
1531         }
1532
1533         /* Add in the loopback network */
1534         if (!lnet_ni_alloc(LNET_MKNET(LOLND, 0), NULL, &net_head)) {
1535                 rc = -ENOMEM;
1536                 goto err_empty_list;
1537         }
1538
1539         /*
1540          * If LNet is being initialized via DLC it is possible
1541          * that the user requests not to load module parameters (ones which
1542          * are supported by DLC) on initialization.  Therefore, make sure not
1543          * to load networks, routes and forwarding from module parameters
1544          * in this case. On cleanup in case of failure only clean up
1545          * routes if it has been loaded
1546          */
1547         if (!the_lnet.ln_nis_from_mod_params) {
1548                 rc = lnet_parse_networks(&net_head, lnet_get_networks());
1549                 if (rc < 0)
1550                         goto err_empty_list;
1551         }
1552
1553         ni_count = lnet_startup_lndnis(&net_head);
1554         if (ni_count < 0) {
1555                 rc = ni_count;
1556                 goto err_empty_list;
1557         }
1558
1559         if (!the_lnet.ln_nis_from_mod_params) {
1560                 rc = lnet_parse_routes(lnet_get_routes(), &im_a_router);
1561                 if (rc)
1562                         goto err_shutdown_lndnis;
1563
1564                 rc = lnet_check_routes();
1565                 if (rc)
1566                         goto err_destory_routes;
1567
1568                 rc = lnet_rtrpools_alloc(im_a_router);
1569                 if (rc)
1570                         goto err_destory_routes;
1571         }
1572
1573         rc = lnet_acceptor_start();
1574         if (rc)
1575                 goto err_destory_routes;
1576
1577         the_lnet.ln_refcount = 1;
1578         /* Now I may use my own API functions... */
1579
1580         rc = lnet_ping_info_setup(&pinfo, &md_handle, ni_count, true);
1581         if (rc)
1582                 goto err_acceptor_stop;
1583
1584         lnet_ping_target_update(pinfo, md_handle);
1585
1586         rc = lnet_router_checker_start();
1587         if (rc)
1588                 goto err_stop_ping;
1589
1590         lnet_fault_init();
1591         lnet_router_debugfs_init();
1592
1593         mutex_unlock(&the_lnet.ln_api_mutex);
1594
1595         return 0;
1596
1597 err_stop_ping:
1598         lnet_ping_target_fini();
1599 err_acceptor_stop:
1600         the_lnet.ln_refcount = 0;
1601         lnet_acceptor_stop();
1602 err_destory_routes:
1603         if (!the_lnet.ln_nis_from_mod_params)
1604                 lnet_destroy_routes();
1605 err_shutdown_lndnis:
1606         lnet_shutdown_lndnis();
1607 err_empty_list:
1608         lnet_unprepare();
1609         LASSERT(rc < 0);
1610         mutex_unlock(&the_lnet.ln_api_mutex);
1611         while (!list_empty(&net_head)) {
1612                 struct lnet_ni *ni;
1613
1614                 ni = list_entry(net_head.next, struct lnet_ni, ni_list);
1615                 list_del_init(&ni->ni_list);
1616                 lnet_ni_free(ni);
1617         }
1618         return rc;
1619 }
1620 EXPORT_SYMBOL(LNetNIInit);
1621
1622 /**
1623  * Stop LNet interfaces, routing, and forwarding.
1624  *
1625  * Users must call this function once for each successful call to LNetNIInit().
1626  * Once the LNetNIFini() operation has been started, the results of pending
1627  * API operations are undefined.
1628  *
1629  * \return always 0 for current implementation.
1630  */
1631 int
1632 LNetNIFini(void)
1633 {
1634         mutex_lock(&the_lnet.ln_api_mutex);
1635
1636         LASSERT(the_lnet.ln_refcount > 0);
1637
1638         if (the_lnet.ln_refcount != 1) {
1639                 the_lnet.ln_refcount--;
1640         } else {
1641                 LASSERT(!the_lnet.ln_niinit_self);
1642
1643                 lnet_fault_fini();
1644                 lnet_router_debugfs_fini();
1645                 lnet_router_checker_stop();
1646                 lnet_ping_target_fini();
1647
1648                 /* Teardown fns that use my own API functions BEFORE here */
1649                 the_lnet.ln_refcount = 0;
1650
1651                 lnet_acceptor_stop();
1652                 lnet_destroy_routes();
1653                 lnet_shutdown_lndnis();
1654                 lnet_unprepare();
1655         }
1656
1657         mutex_unlock(&the_lnet.ln_api_mutex);
1658         return 0;
1659 }
1660 EXPORT_SYMBOL(LNetNIFini);
1661
1662 /**
1663  * Grabs the ni data from the ni structure and fills the out
1664  * parameters
1665  *
1666  * \param[in] ni network       interface structure
1667  * \param[out] config          NI configuration
1668  */
1669 static void
1670 lnet_fill_ni_info(struct lnet_ni *ni, struct lnet_ioctl_config_data *config)
1671 {
1672         struct lnet_ioctl_config_lnd_tunables *lnd_cfg = NULL;
1673         struct lnet_ioctl_net_config *net_config;
1674         size_t min_size, tunable_size = 0;
1675         int i;
1676
1677         if (!ni || !config)
1678                 return;
1679
1680         net_config = (struct lnet_ioctl_net_config *) config->cfg_bulk;
1681         if (!net_config)
1682                 return;
1683
1684         BUILD_BUG_ON(ARRAY_SIZE(ni->ni_interfaces) !=
1685                      ARRAY_SIZE(net_config->ni_interfaces));
1686
1687         for (i = 0; i < ARRAY_SIZE(ni->ni_interfaces); i++) {
1688                 if (!ni->ni_interfaces[i])
1689                         break;
1690
1691                 strncpy(net_config->ni_interfaces[i],
1692                         ni->ni_interfaces[i],
1693                         sizeof(net_config->ni_interfaces[i]));
1694         }
1695
1696         config->cfg_nid = ni->ni_nid;
1697         config->cfg_config_u.cfg_net.net_peer_timeout = ni->ni_peertimeout;
1698         config->cfg_config_u.cfg_net.net_max_tx_credits = ni->ni_maxtxcredits;
1699         config->cfg_config_u.cfg_net.net_peer_tx_credits = ni->ni_peertxcredits;
1700         config->cfg_config_u.cfg_net.net_peer_rtr_credits = ni->ni_peerrtrcredits;
1701
1702         net_config->ni_status = ni->ni_status->ns_status;
1703
1704         if (ni->ni_cpts) {
1705                 int num_cpts = min(ni->ni_ncpts, LNET_MAX_SHOW_NUM_CPT);
1706
1707                 for (i = 0; i < num_cpts; i++)
1708                         net_config->ni_cpts[i] = ni->ni_cpts[i];
1709
1710                 config->cfg_ncpts = num_cpts;
1711         }
1712
1713         /*
1714          * See if user land tools sent in a newer and larger version
1715          * of struct lnet_tunables than what the kernel uses.
1716          */
1717         min_size = sizeof(*config) + sizeof(*net_config);
1718
1719         if (config->cfg_hdr.ioc_len > min_size)
1720                 tunable_size = config->cfg_hdr.ioc_len - min_size;
1721
1722         /* Don't copy to much data to user space */
1723         min_size = min(tunable_size, sizeof(*ni->ni_lnd_tunables));
1724         lnd_cfg = (struct lnet_ioctl_config_lnd_tunables *)net_config->cfg_bulk;
1725
1726         if (ni->ni_lnd_tunables && lnd_cfg && min_size) {
1727                 memcpy(lnd_cfg, ni->ni_lnd_tunables, min_size);
1728                 config->cfg_config_u.cfg_net.net_interface_count = 1;
1729
1730                 /* Tell user land that kernel side has less data */
1731                 if (tunable_size > sizeof(*ni->ni_lnd_tunables)) {
1732                         min_size = tunable_size - sizeof(ni->ni_lnd_tunables);
1733                         config->cfg_hdr.ioc_len -= min_size;
1734                 }
1735         }
1736 }
1737
1738 static int
1739 lnet_get_net_config(struct lnet_ioctl_config_data *config)
1740 {
1741         struct lnet_ni *ni;
1742         struct list_head *tmp;
1743         int idx = config->cfg_count;
1744         int cpt, i = 0;
1745         int rc = -ENOENT;
1746
1747         cpt = lnet_net_lock_current();
1748
1749         list_for_each(tmp, &the_lnet.ln_nis) {
1750                 if (i++ != idx)
1751                         continue;
1752
1753                 ni = list_entry(tmp, lnet_ni_t, ni_list);
1754                 lnet_ni_lock(ni);
1755                 lnet_fill_ni_info(ni, config);
1756                 lnet_ni_unlock(ni);
1757                 rc = 0;
1758                 break;
1759         }
1760
1761         lnet_net_unlock(cpt);
1762         return rc;
1763 }
1764
1765 int
1766 lnet_dyn_add_ni(lnet_pid_t requested_pid, struct lnet_ioctl_config_data *conf)
1767 {
1768         char *nets = conf->cfg_config_u.cfg_net.net_intf;
1769         lnet_ping_info_t *pinfo;
1770         lnet_handle_md_t md_handle;
1771         struct lnet_ni *ni;
1772         struct list_head net_head;
1773         lnet_remotenet_t *rnet;
1774         int rc;
1775
1776         INIT_LIST_HEAD(&net_head);
1777
1778         /* Create a ni structure for the network string */
1779         rc = lnet_parse_networks(&net_head, nets);
1780         if (rc <= 0)
1781                 return !rc ? -EINVAL : rc;
1782
1783         mutex_lock(&the_lnet.ln_api_mutex);
1784
1785         if (rc > 1) {
1786                 rc = -EINVAL; /* only add one interface per call */
1787                 goto failed0;
1788         }
1789
1790         ni = list_entry(net_head.next, struct lnet_ni, ni_list);
1791
1792         lnet_net_lock(LNET_LOCK_EX);
1793         rnet = lnet_find_net_locked(LNET_NIDNET(ni->ni_nid));
1794         lnet_net_unlock(LNET_LOCK_EX);
1795         /*
1796          * make sure that the net added doesn't invalidate the current
1797          * configuration LNet is keeping
1798          */
1799         if (rnet) {
1800                 CERROR("Adding net %s will invalidate routing configuration\n",
1801                        nets);
1802                 rc = -EUSERS;
1803                 goto failed0;
1804         }
1805
1806         rc = lnet_ping_info_setup(&pinfo, &md_handle, 1 + lnet_get_ni_count(),
1807                                   false);
1808         if (rc)
1809                 goto failed0;
1810
1811         list_del_init(&ni->ni_list);
1812
1813         rc = lnet_startup_lndni(ni, conf);
1814         if (rc)
1815                 goto failed1;
1816
1817         if (ni->ni_lnd->lnd_accept) {
1818                 rc = lnet_acceptor_start();
1819                 if (rc < 0) {
1820                         /* shutdown the ni that we just started */
1821                         CERROR("Failed to start up acceptor thread\n");
1822                         lnet_shutdown_lndni(ni);
1823                         goto failed1;
1824                 }
1825         }
1826
1827         lnet_ping_target_update(pinfo, md_handle);
1828         mutex_unlock(&the_lnet.ln_api_mutex);
1829
1830         return 0;
1831
1832 failed1:
1833         lnet_ping_md_unlink(pinfo, &md_handle);
1834         lnet_ping_info_free(pinfo);
1835 failed0:
1836         mutex_unlock(&the_lnet.ln_api_mutex);
1837         while (!list_empty(&net_head)) {
1838                 ni = list_entry(net_head.next, struct lnet_ni, ni_list);
1839                 list_del_init(&ni->ni_list);
1840                 lnet_ni_free(ni);
1841         }
1842         return rc;
1843 }
1844
1845 int
1846 lnet_dyn_del_ni(__u32 net)
1847 {
1848         lnet_ni_t *ni;
1849         lnet_ping_info_t *pinfo;
1850         lnet_handle_md_t md_handle;
1851         int rc;
1852
1853         /* don't allow userspace to shutdown the LOLND */
1854         if (LNET_NETTYP(net) == LOLND)
1855                 return -EINVAL;
1856
1857         mutex_lock(&the_lnet.ln_api_mutex);
1858         /* create and link a new ping info, before removing the old one */
1859         rc = lnet_ping_info_setup(&pinfo, &md_handle,
1860                                   lnet_get_ni_count() - 1, false);
1861         if (rc)
1862                 goto out;
1863
1864         ni = lnet_net2ni(net);
1865         if (!ni) {
1866                 rc = -EINVAL;
1867                 goto failed;
1868         }
1869
1870         /* decrement the reference counter taken by lnet_net2ni() */
1871         lnet_ni_decref_locked(ni, 0);
1872
1873         lnet_shutdown_lndni(ni);
1874
1875         if (!lnet_count_acceptor_nis())
1876                 lnet_acceptor_stop();
1877
1878         lnet_ping_target_update(pinfo, md_handle);
1879         goto out;
1880 failed:
1881         lnet_ping_md_unlink(pinfo, &md_handle);
1882         lnet_ping_info_free(pinfo);
1883 out:
1884         mutex_unlock(&the_lnet.ln_api_mutex);
1885
1886         return rc;
1887 }
1888
1889 /**
1890  * LNet ioctl handler.
1891  *
1892  */
1893 int
1894 LNetCtl(unsigned int cmd, void *arg)
1895 {
1896         struct libcfs_ioctl_data *data = arg;
1897         struct lnet_ioctl_config_data *config;
1898         lnet_process_id_t id = {0};
1899         lnet_ni_t *ni;
1900         int rc;
1901         unsigned long secs_passed;
1902
1903         BUILD_BUG_ON(LIBCFS_IOC_DATA_MAX <
1904                      sizeof(struct lnet_ioctl_net_config) +
1905                      sizeof(struct lnet_ioctl_config_data));
1906
1907         switch (cmd) {
1908         case IOC_LIBCFS_GET_NI:
1909                 rc = LNetGetId(data->ioc_count, &id);
1910                 data->ioc_nid = id.nid;
1911                 return rc;
1912
1913         case IOC_LIBCFS_FAIL_NID:
1914                 return lnet_fail_nid(data->ioc_nid, data->ioc_count);
1915
1916         case IOC_LIBCFS_ADD_ROUTE:
1917                 config = arg;
1918
1919                 if (config->cfg_hdr.ioc_len < sizeof(*config))
1920                         return -EINVAL;
1921
1922                 mutex_lock(&the_lnet.ln_api_mutex);
1923                 rc = lnet_add_route(config->cfg_net,
1924                                     config->cfg_config_u.cfg_route.rtr_hop,
1925                                     config->cfg_nid,
1926                                     config->cfg_config_u.cfg_route.rtr_priority);
1927                 if (!rc) {
1928                         rc = lnet_check_routes();
1929                         if (rc)
1930                                 lnet_del_route(config->cfg_net,
1931                                                config->cfg_nid);
1932                 }
1933                 mutex_unlock(&the_lnet.ln_api_mutex);
1934                 return rc;
1935
1936         case IOC_LIBCFS_DEL_ROUTE:
1937                 config = arg;
1938
1939                 if (config->cfg_hdr.ioc_len < sizeof(*config))
1940                         return -EINVAL;
1941
1942                 mutex_lock(&the_lnet.ln_api_mutex);
1943                 rc = lnet_del_route(config->cfg_net, config->cfg_nid);
1944                 mutex_unlock(&the_lnet.ln_api_mutex);
1945                 return rc;
1946
1947         case IOC_LIBCFS_GET_ROUTE:
1948                 config = arg;
1949
1950                 if (config->cfg_hdr.ioc_len < sizeof(*config))
1951                         return -EINVAL;
1952
1953                 return lnet_get_route(config->cfg_count,
1954                                       &config->cfg_net,
1955                                       &config->cfg_config_u.cfg_route.rtr_hop,
1956                                       &config->cfg_nid,
1957                                       &config->cfg_config_u.cfg_route.rtr_flags,
1958                                       &config->cfg_config_u.cfg_route.rtr_priority);
1959
1960         case IOC_LIBCFS_GET_NET: {
1961                 size_t total = sizeof(*config) +
1962                                sizeof(struct lnet_ioctl_net_config);
1963                 config = arg;
1964
1965                 if (config->cfg_hdr.ioc_len < total)
1966                         return -EINVAL;
1967
1968                 return lnet_get_net_config(config);
1969         }
1970
1971         case IOC_LIBCFS_GET_LNET_STATS: {
1972                 struct lnet_ioctl_lnet_stats *lnet_stats = arg;
1973
1974                 if (lnet_stats->st_hdr.ioc_len < sizeof(*lnet_stats))
1975                         return -EINVAL;
1976
1977                 lnet_counters_get(&lnet_stats->st_cntrs);
1978                 return 0;
1979         }
1980
1981         case IOC_LIBCFS_CONFIG_RTR:
1982                 config = arg;
1983
1984                 if (config->cfg_hdr.ioc_len < sizeof(*config))
1985                         return -EINVAL;
1986
1987                 mutex_lock(&the_lnet.ln_api_mutex);
1988                 if (config->cfg_config_u.cfg_buffers.buf_enable) {
1989                         rc = lnet_rtrpools_enable();
1990                         mutex_unlock(&the_lnet.ln_api_mutex);
1991                         return rc;
1992                 }
1993                 lnet_rtrpools_disable();
1994                 mutex_unlock(&the_lnet.ln_api_mutex);
1995                 return 0;
1996
1997         case IOC_LIBCFS_ADD_BUF:
1998                 config = arg;
1999
2000                 if (config->cfg_hdr.ioc_len < sizeof(*config))
2001                         return -EINVAL;
2002
2003                 mutex_lock(&the_lnet.ln_api_mutex);
2004                 rc = lnet_rtrpools_adjust(config->cfg_config_u.cfg_buffers.buf_tiny,
2005                                           config->cfg_config_u.cfg_buffers.buf_small,
2006                                           config->cfg_config_u.cfg_buffers.buf_large);
2007                 mutex_unlock(&the_lnet.ln_api_mutex);
2008                 return rc;
2009
2010         case IOC_LIBCFS_GET_BUF: {
2011                 struct lnet_ioctl_pool_cfg *pool_cfg;
2012                 size_t total = sizeof(*config) + sizeof(*pool_cfg);
2013
2014                 config = arg;
2015
2016                 if (config->cfg_hdr.ioc_len < total)
2017                         return -EINVAL;
2018
2019                 pool_cfg = (struct lnet_ioctl_pool_cfg *)config->cfg_bulk;
2020                 return lnet_get_rtr_pool_cfg(config->cfg_count, pool_cfg);
2021         }
2022
2023         case IOC_LIBCFS_GET_PEER_INFO: {
2024                 struct lnet_ioctl_peer *peer_info = arg;
2025
2026                 if (peer_info->pr_hdr.ioc_len < sizeof(*peer_info))
2027                         return -EINVAL;
2028
2029                 return lnet_get_peer_info(peer_info->pr_count,
2030                         &peer_info->pr_nid,
2031                         peer_info->pr_lnd_u.pr_peer_credits.cr_aliveness,
2032                         &peer_info->pr_lnd_u.pr_peer_credits.cr_ncpt,
2033                         &peer_info->pr_lnd_u.pr_peer_credits.cr_refcount,
2034                         &peer_info->pr_lnd_u.pr_peer_credits.cr_ni_peer_tx_credits,
2035                         &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_tx_credits,
2036                         &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_rtr_credits,
2037                         &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_min_rtr_credits,
2038                         &peer_info->pr_lnd_u.pr_peer_credits.cr_peer_tx_qnob);
2039         }
2040
2041         case IOC_LIBCFS_NOTIFY_ROUTER:
2042                 secs_passed = (ktime_get_real_seconds() - data->ioc_u64[0]);
2043                 secs_passed *= msecs_to_jiffies(MSEC_PER_SEC);
2044
2045                 return lnet_notify(NULL, data->ioc_nid, data->ioc_flags,
2046                                    jiffies - secs_passed);
2047
2048         case IOC_LIBCFS_LNET_DIST:
2049                 rc = LNetDist(data->ioc_nid, &data->ioc_nid, &data->ioc_u32[1]);
2050                 if (rc < 0 && rc != -EHOSTUNREACH)
2051                         return rc;
2052
2053                 data->ioc_u32[0] = rc;
2054                 return 0;
2055
2056         case IOC_LIBCFS_TESTPROTOCOMPAT:
2057                 lnet_net_lock(LNET_LOCK_EX);
2058                 the_lnet.ln_testprotocompat = data->ioc_flags;
2059                 lnet_net_unlock(LNET_LOCK_EX);
2060                 return 0;
2061
2062         case IOC_LIBCFS_LNET_FAULT:
2063                 return lnet_fault_ctl(data->ioc_flags, data);
2064
2065         case IOC_LIBCFS_PING:
2066                 id.nid = data->ioc_nid;
2067                 id.pid = data->ioc_u32[0];
2068                 rc = lnet_ping(id, data->ioc_u32[1], /* timeout */
2069                                data->ioc_pbuf1,
2070                                data->ioc_plen1 / sizeof(lnet_process_id_t));
2071                 if (rc < 0)
2072                         return rc;
2073                 data->ioc_count = rc;
2074                 return 0;
2075
2076         default:
2077                 ni = lnet_net2ni(data->ioc_net);
2078                 if (!ni)
2079                         return -EINVAL;
2080
2081                 if (!ni->ni_lnd->lnd_ctl)
2082                         rc = -EINVAL;
2083                 else
2084                         rc = ni->ni_lnd->lnd_ctl(ni, cmd, arg);
2085
2086                 lnet_ni_decref(ni);
2087                 return rc;
2088         }
2089         /* not reached */
2090 }
2091 EXPORT_SYMBOL(LNetCtl);
2092
2093 void LNetDebugPeer(lnet_process_id_t id)
2094 {
2095         lnet_debug_peer(id.nid);
2096 }
2097 EXPORT_SYMBOL(LNetDebugPeer);
2098
2099 /**
2100  * Retrieve the lnet_process_id_t ID of LNet interface at \a index. Note that
2101  * all interfaces share a same PID, as requested by LNetNIInit().
2102  *
2103  * \param index Index of the interface to look up.
2104  * \param id On successful return, this location will hold the
2105  * lnet_process_id_t ID of the interface.
2106  *
2107  * \retval 0 If an interface exists at \a index.
2108  * \retval -ENOENT If no interface has been found.
2109  */
2110 int
2111 LNetGetId(unsigned int index, lnet_process_id_t *id)
2112 {
2113         struct lnet_ni *ni;
2114         struct list_head *tmp;
2115         int cpt;
2116         int rc = -ENOENT;
2117
2118         LASSERT(the_lnet.ln_refcount > 0);
2119
2120         cpt = lnet_net_lock_current();
2121
2122         list_for_each(tmp, &the_lnet.ln_nis) {
2123                 if (index--)
2124                         continue;
2125
2126                 ni = list_entry(tmp, lnet_ni_t, ni_list);
2127
2128                 id->nid = ni->ni_nid;
2129                 id->pid = the_lnet.ln_pid;
2130                 rc = 0;
2131                 break;
2132         }
2133
2134         lnet_net_unlock(cpt);
2135         return rc;
2136 }
2137 EXPORT_SYMBOL(LNetGetId);
2138
2139 /**
2140  * Print a string representation of handle \a h into buffer \a str of
2141  * \a len bytes.
2142  */
2143 void
2144 LNetSnprintHandle(char *str, int len, lnet_handle_any_t h)
2145 {
2146         snprintf(str, len, "%#llx", h.cookie);
2147 }
2148 EXPORT_SYMBOL(LNetSnprintHandle);
2149
2150 static int lnet_ping(lnet_process_id_t id, int timeout_ms,
2151                      lnet_process_id_t __user *ids, int n_ids)
2152 {
2153         lnet_handle_eq_t eqh;
2154         lnet_handle_md_t mdh;
2155         lnet_event_t event;
2156         lnet_md_t md = { NULL };
2157         int which;
2158         int unlinked = 0;
2159         int replied = 0;
2160         const int a_long_time = 60000; /* mS */
2161         int infosz;
2162         lnet_ping_info_t *info;
2163         lnet_process_id_t tmpid;
2164         int i;
2165         int nob;
2166         int rc;
2167         int rc2;
2168         sigset_t blocked;
2169
2170         infosz = offsetof(lnet_ping_info_t, pi_ni[n_ids]);
2171
2172         if (n_ids <= 0 ||
2173             id.nid == LNET_NID_ANY ||
2174             timeout_ms > 500000 ||            /* arbitrary limit! */
2175             n_ids > 20)                  /* arbitrary limit! */
2176                 return -EINVAL;
2177
2178         if (id.pid == LNET_PID_ANY)
2179                 id.pid = LNET_PID_LUSTRE;
2180
2181         LIBCFS_ALLOC(info, infosz);
2182         if (!info)
2183                 return -ENOMEM;
2184
2185         /* NB 2 events max (including any unlink event) */
2186         rc = LNetEQAlloc(2, LNET_EQ_HANDLER_NONE, &eqh);
2187         if (rc) {
2188                 CERROR("Can't allocate EQ: %d\n", rc);
2189                 goto out_0;
2190         }
2191
2192         /* initialize md content */
2193         md.start     = info;
2194         md.length    = infosz;
2195         md.threshold = 2; /*GET/REPLY*/
2196         md.max_size  = 0;
2197         md.options   = LNET_MD_TRUNCATE;
2198         md.user_ptr  = NULL;
2199         md.eq_handle = eqh;
2200
2201         rc = LNetMDBind(md, LNET_UNLINK, &mdh);
2202         if (rc) {
2203                 CERROR("Can't bind MD: %d\n", rc);
2204                 goto out_1;
2205         }
2206
2207         rc = LNetGet(LNET_NID_ANY, mdh, id,
2208                      LNET_RESERVED_PORTAL,
2209                      LNET_PROTO_PING_MATCHBITS, 0);
2210
2211         if (rc) {
2212                 /* Don't CERROR; this could be deliberate! */
2213
2214                 rc2 = LNetMDUnlink(mdh);
2215                 LASSERT(!rc2);
2216
2217                 /* NB must wait for the UNLINK event below... */
2218                 unlinked = 1;
2219                 timeout_ms = a_long_time;
2220         }
2221
2222         do {
2223                 /* MUST block for unlink to complete */
2224                 if (unlinked)
2225                         blocked = cfs_block_allsigs();
2226
2227                 rc2 = LNetEQPoll(&eqh, 1, timeout_ms, &event, &which);
2228
2229                 if (unlinked)
2230                         cfs_restore_sigs(blocked);
2231
2232                 CDEBUG(D_NET, "poll %d(%d %d)%s\n", rc2,
2233                        (rc2 <= 0) ? -1 : event.type,
2234                        (rc2 <= 0) ? -1 : event.status,
2235                        (rc2 > 0 && event.unlinked) ? " unlinked" : "");
2236
2237                 LASSERT(rc2 != -EOVERFLOW);     /* can't miss anything */
2238
2239                 if (rc2 <= 0 || event.status) {
2240                         /* timeout or error */
2241                         if (!replied && !rc)
2242                                 rc = (rc2 < 0) ? rc2 :
2243                                      !rc2 ? -ETIMEDOUT :
2244                                      event.status;
2245
2246                         if (!unlinked) {
2247                                 /* Ensure completion in finite time... */
2248                                 LNetMDUnlink(mdh);
2249                                 /* No assertion (racing with network) */
2250                                 unlinked = 1;
2251                                 timeout_ms = a_long_time;
2252                         } else if (!rc2) {
2253                                 /* timed out waiting for unlink */
2254                                 CWARN("ping %s: late network completion\n",
2255                                       libcfs_id2str(id));
2256                         }
2257                 } else if (event.type == LNET_EVENT_REPLY) {
2258                         replied = 1;
2259                         rc = event.mlength;
2260                 }
2261
2262         } while (rc2 <= 0 || !event.unlinked);
2263
2264         if (!replied) {
2265                 if (rc >= 0)
2266                         CWARN("%s: Unexpected rc >= 0 but no reply!\n",
2267                               libcfs_id2str(id));
2268                 rc = -EIO;
2269                 goto out_1;
2270         }
2271
2272         nob = rc;
2273         LASSERT(nob >= 0 && nob <= infosz);
2274
2275         rc = -EPROTO;                      /* if I can't parse... */
2276
2277         if (nob < 8) {
2278                 /* can't check magic/version */
2279                 CERROR("%s: ping info too short %d\n",
2280                        libcfs_id2str(id), nob);
2281                 goto out_1;
2282         }
2283
2284         if (info->pi_magic == __swab32(LNET_PROTO_PING_MAGIC)) {
2285                 lnet_swap_pinginfo(info);
2286         } else if (info->pi_magic != LNET_PROTO_PING_MAGIC) {
2287                 CERROR("%s: Unexpected magic %08x\n",
2288                        libcfs_id2str(id), info->pi_magic);
2289                 goto out_1;
2290         }
2291
2292         if (!(info->pi_features & LNET_PING_FEAT_NI_STATUS)) {
2293                 CERROR("%s: ping w/o NI status: 0x%x\n",
2294                        libcfs_id2str(id), info->pi_features);
2295                 goto out_1;
2296         }
2297
2298         if (nob < offsetof(lnet_ping_info_t, pi_ni[0])) {
2299                 CERROR("%s: Short reply %d(%d min)\n", libcfs_id2str(id),
2300                        nob, (int)offsetof(lnet_ping_info_t, pi_ni[0]));
2301                 goto out_1;
2302         }
2303
2304         if (info->pi_nnis < n_ids)
2305                 n_ids = info->pi_nnis;
2306
2307         if (nob < offsetof(lnet_ping_info_t, pi_ni[n_ids])) {
2308                 CERROR("%s: Short reply %d(%d expected)\n", libcfs_id2str(id),
2309                        nob, (int)offsetof(lnet_ping_info_t, pi_ni[n_ids]));
2310                 goto out_1;
2311         }
2312
2313         rc = -EFAULT;                      /* If I SEGV... */
2314
2315         memset(&tmpid, 0, sizeof(tmpid));
2316         for (i = 0; i < n_ids; i++) {
2317                 tmpid.pid = info->pi_pid;
2318                 tmpid.nid = info->pi_ni[i].ns_nid;
2319                 if (copy_to_user(&ids[i], &tmpid, sizeof(tmpid)))
2320                         goto out_1;
2321         }
2322         rc = info->pi_nnis;
2323
2324  out_1:
2325         rc2 = LNetEQFree(eqh);
2326         if (rc2)
2327                 CERROR("rc2 %d\n", rc2);
2328         LASSERT(!rc2);
2329
2330  out_0:
2331         LIBCFS_FREE(info, infosz);
2332         return rc;
2333 }