]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/infiniband/core/sa_query.c
78ea8157d62298f28553f399f891b6f3759a7887
[linux-2.6-omap-h63xx.git] / drivers / infiniband / core / sa_query.c
1 /*
2  * Copyright (c) 2004 Topspin Communications.  All rights reserved.
3  * Copyright (c) 2005 Voltaire, Inc.  All rights reserved.
4  * Copyright (c) 2006 Intel Corporation.  All rights reserved.
5  *
6  * This software is available to you under a choice of one of two
7  * licenses.  You may choose to be licensed under the terms of the GNU
8  * General Public License (GPL) Version 2, available from the file
9  * COPYING in the main directory of this source tree, or the
10  * OpenIB.org BSD license below:
11  *
12  *     Redistribution and use in source and binary forms, with or
13  *     without modification, are permitted provided that the following
14  *     conditions are met:
15  *
16  *      - Redistributions of source code must retain the above
17  *        copyright notice, this list of conditions and the following
18  *        disclaimer.
19  *
20  *      - Redistributions in binary form must reproduce the above
21  *        copyright notice, this list of conditions and the following
22  *        disclaimer in the documentation and/or other materials
23  *        provided with the distribution.
24  *
25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32  * SOFTWARE.
33  *
34  * $Id: sa_query.c 2811 2005-07-06 18:11:43Z halr $
35  */
36
37 #include <linux/module.h>
38 #include <linux/init.h>
39 #include <linux/err.h>
40 #include <linux/random.h>
41 #include <linux/spinlock.h>
42 #include <linux/slab.h>
43 #include <linux/dma-mapping.h>
44 #include <linux/kref.h>
45 #include <linux/idr.h>
46 #include <linux/workqueue.h>
47
48 #include <rdma/ib_pack.h>
49 #include <rdma/ib_cache.h>
50 #include "sa.h"
51
52 MODULE_AUTHOR("Roland Dreier");
53 MODULE_DESCRIPTION("InfiniBand subnet administration query support");
54 MODULE_LICENSE("Dual BSD/GPL");
55
56 struct ib_sa_sm_ah {
57         struct ib_ah        *ah;
58         struct kref          ref;
59         u16                  pkey_index;
60         u8                   src_path_mask;
61 };
62
63 struct ib_sa_port {
64         struct ib_mad_agent *agent;
65         struct ib_sa_sm_ah  *sm_ah;
66         struct work_struct   update_task;
67         spinlock_t           ah_lock;
68         u8                   port_num;
69 };
70
71 struct ib_sa_device {
72         int                     start_port, end_port;
73         struct ib_event_handler event_handler;
74         struct ib_sa_port port[0];
75 };
76
77 struct ib_sa_query {
78         void (*callback)(struct ib_sa_query *, int, struct ib_sa_mad *);
79         void (*release)(struct ib_sa_query *);
80         struct ib_sa_client    *client;
81         struct ib_sa_port      *port;
82         struct ib_mad_send_buf *mad_buf;
83         struct ib_sa_sm_ah     *sm_ah;
84         int                     id;
85 };
86
87 struct ib_sa_service_query {
88         void (*callback)(int, struct ib_sa_service_rec *, void *);
89         void *context;
90         struct ib_sa_query sa_query;
91 };
92
93 struct ib_sa_path_query {
94         void (*callback)(int, struct ib_sa_path_rec *, void *);
95         void *context;
96         struct ib_sa_query sa_query;
97 };
98
99 struct ib_sa_mcmember_query {
100         void (*callback)(int, struct ib_sa_mcmember_rec *, void *);
101         void *context;
102         struct ib_sa_query sa_query;
103 };
104
105 static void ib_sa_add_one(struct ib_device *device);
106 static void ib_sa_remove_one(struct ib_device *device);
107
108 static struct ib_client sa_client = {
109         .name   = "sa",
110         .add    = ib_sa_add_one,
111         .remove = ib_sa_remove_one
112 };
113
114 static spinlock_t idr_lock;
115 static DEFINE_IDR(query_idr);
116
117 static spinlock_t tid_lock;
118 static u32 tid;
119
120 #define PATH_REC_FIELD(field) \
121         .struct_offset_bytes = offsetof(struct ib_sa_path_rec, field),          \
122         .struct_size_bytes   = sizeof ((struct ib_sa_path_rec *) 0)->field,     \
123         .field_name          = "sa_path_rec:" #field
124
125 static const struct ib_field path_rec_table[] = {
126         { PATH_REC_FIELD(service_id),
127           .offset_words = 0,
128           .offset_bits  = 0,
129           .size_bits    = 64 },
130         { PATH_REC_FIELD(dgid),
131           .offset_words = 2,
132           .offset_bits  = 0,
133           .size_bits    = 128 },
134         { PATH_REC_FIELD(sgid),
135           .offset_words = 6,
136           .offset_bits  = 0,
137           .size_bits    = 128 },
138         { PATH_REC_FIELD(dlid),
139           .offset_words = 10,
140           .offset_bits  = 0,
141           .size_bits    = 16 },
142         { PATH_REC_FIELD(slid),
143           .offset_words = 10,
144           .offset_bits  = 16,
145           .size_bits    = 16 },
146         { PATH_REC_FIELD(raw_traffic),
147           .offset_words = 11,
148           .offset_bits  = 0,
149           .size_bits    = 1 },
150         { RESERVED,
151           .offset_words = 11,
152           .offset_bits  = 1,
153           .size_bits    = 3 },
154         { PATH_REC_FIELD(flow_label),
155           .offset_words = 11,
156           .offset_bits  = 4,
157           .size_bits    = 20 },
158         { PATH_REC_FIELD(hop_limit),
159           .offset_words = 11,
160           .offset_bits  = 24,
161           .size_bits    = 8 },
162         { PATH_REC_FIELD(traffic_class),
163           .offset_words = 12,
164           .offset_bits  = 0,
165           .size_bits    = 8 },
166         { PATH_REC_FIELD(reversible),
167           .offset_words = 12,
168           .offset_bits  = 8,
169           .size_bits    = 1 },
170         { PATH_REC_FIELD(numb_path),
171           .offset_words = 12,
172           .offset_bits  = 9,
173           .size_bits    = 7 },
174         { PATH_REC_FIELD(pkey),
175           .offset_words = 12,
176           .offset_bits  = 16,
177           .size_bits    = 16 },
178         { PATH_REC_FIELD(qos_class),
179           .offset_words = 13,
180           .offset_bits  = 0,
181           .size_bits    = 12 },
182         { PATH_REC_FIELD(sl),
183           .offset_words = 13,
184           .offset_bits  = 12,
185           .size_bits    = 4 },
186         { PATH_REC_FIELD(mtu_selector),
187           .offset_words = 13,
188           .offset_bits  = 16,
189           .size_bits    = 2 },
190         { PATH_REC_FIELD(mtu),
191           .offset_words = 13,
192           .offset_bits  = 18,
193           .size_bits    = 6 },
194         { PATH_REC_FIELD(rate_selector),
195           .offset_words = 13,
196           .offset_bits  = 24,
197           .size_bits    = 2 },
198         { PATH_REC_FIELD(rate),
199           .offset_words = 13,
200           .offset_bits  = 26,
201           .size_bits    = 6 },
202         { PATH_REC_FIELD(packet_life_time_selector),
203           .offset_words = 14,
204           .offset_bits  = 0,
205           .size_bits    = 2 },
206         { PATH_REC_FIELD(packet_life_time),
207           .offset_words = 14,
208           .offset_bits  = 2,
209           .size_bits    = 6 },
210         { PATH_REC_FIELD(preference),
211           .offset_words = 14,
212           .offset_bits  = 8,
213           .size_bits    = 8 },
214         { RESERVED,
215           .offset_words = 14,
216           .offset_bits  = 16,
217           .size_bits    = 48 },
218 };
219
220 #define MCMEMBER_REC_FIELD(field) \
221         .struct_offset_bytes = offsetof(struct ib_sa_mcmember_rec, field),      \
222         .struct_size_bytes   = sizeof ((struct ib_sa_mcmember_rec *) 0)->field, \
223         .field_name          = "sa_mcmember_rec:" #field
224
225 static const struct ib_field mcmember_rec_table[] = {
226         { MCMEMBER_REC_FIELD(mgid),
227           .offset_words = 0,
228           .offset_bits  = 0,
229           .size_bits    = 128 },
230         { MCMEMBER_REC_FIELD(port_gid),
231           .offset_words = 4,
232           .offset_bits  = 0,
233           .size_bits    = 128 },
234         { MCMEMBER_REC_FIELD(qkey),
235           .offset_words = 8,
236           .offset_bits  = 0,
237           .size_bits    = 32 },
238         { MCMEMBER_REC_FIELD(mlid),
239           .offset_words = 9,
240           .offset_bits  = 0,
241           .size_bits    = 16 },
242         { MCMEMBER_REC_FIELD(mtu_selector),
243           .offset_words = 9,
244           .offset_bits  = 16,
245           .size_bits    = 2 },
246         { MCMEMBER_REC_FIELD(mtu),
247           .offset_words = 9,
248           .offset_bits  = 18,
249           .size_bits    = 6 },
250         { MCMEMBER_REC_FIELD(traffic_class),
251           .offset_words = 9,
252           .offset_bits  = 24,
253           .size_bits    = 8 },
254         { MCMEMBER_REC_FIELD(pkey),
255           .offset_words = 10,
256           .offset_bits  = 0,
257           .size_bits    = 16 },
258         { MCMEMBER_REC_FIELD(rate_selector),
259           .offset_words = 10,
260           .offset_bits  = 16,
261           .size_bits    = 2 },
262         { MCMEMBER_REC_FIELD(rate),
263           .offset_words = 10,
264           .offset_bits  = 18,
265           .size_bits    = 6 },
266         { MCMEMBER_REC_FIELD(packet_life_time_selector),
267           .offset_words = 10,
268           .offset_bits  = 24,
269           .size_bits    = 2 },
270         { MCMEMBER_REC_FIELD(packet_life_time),
271           .offset_words = 10,
272           .offset_bits  = 26,
273           .size_bits    = 6 },
274         { MCMEMBER_REC_FIELD(sl),
275           .offset_words = 11,
276           .offset_bits  = 0,
277           .size_bits    = 4 },
278         { MCMEMBER_REC_FIELD(flow_label),
279           .offset_words = 11,
280           .offset_bits  = 4,
281           .size_bits    = 20 },
282         { MCMEMBER_REC_FIELD(hop_limit),
283           .offset_words = 11,
284           .offset_bits  = 24,
285           .size_bits    = 8 },
286         { MCMEMBER_REC_FIELD(scope),
287           .offset_words = 12,
288           .offset_bits  = 0,
289           .size_bits    = 4 },
290         { MCMEMBER_REC_FIELD(join_state),
291           .offset_words = 12,
292           .offset_bits  = 4,
293           .size_bits    = 4 },
294         { MCMEMBER_REC_FIELD(proxy_join),
295           .offset_words = 12,
296           .offset_bits  = 8,
297           .size_bits    = 1 },
298         { RESERVED,
299           .offset_words = 12,
300           .offset_bits  = 9,
301           .size_bits    = 23 },
302 };
303
304 #define SERVICE_REC_FIELD(field) \
305         .struct_offset_bytes = offsetof(struct ib_sa_service_rec, field),       \
306         .struct_size_bytes   = sizeof ((struct ib_sa_service_rec *) 0)->field,  \
307         .field_name          = "sa_service_rec:" #field
308
309 static const struct ib_field service_rec_table[] = {
310         { SERVICE_REC_FIELD(id),
311           .offset_words = 0,
312           .offset_bits  = 0,
313           .size_bits    = 64 },
314         { SERVICE_REC_FIELD(gid),
315           .offset_words = 2,
316           .offset_bits  = 0,
317           .size_bits    = 128 },
318         { SERVICE_REC_FIELD(pkey),
319           .offset_words = 6,
320           .offset_bits  = 0,
321           .size_bits    = 16 },
322         { SERVICE_REC_FIELD(lease),
323           .offset_words = 7,
324           .offset_bits  = 0,
325           .size_bits    = 32 },
326         { SERVICE_REC_FIELD(key),
327           .offset_words = 8,
328           .offset_bits  = 0,
329           .size_bits    = 128 },
330         { SERVICE_REC_FIELD(name),
331           .offset_words = 12,
332           .offset_bits  = 0,
333           .size_bits    = 64*8 },
334         { SERVICE_REC_FIELD(data8),
335           .offset_words = 28,
336           .offset_bits  = 0,
337           .size_bits    = 16*8 },
338         { SERVICE_REC_FIELD(data16),
339           .offset_words = 32,
340           .offset_bits  = 0,
341           .size_bits    = 8*16 },
342         { SERVICE_REC_FIELD(data32),
343           .offset_words = 36,
344           .offset_bits  = 0,
345           .size_bits    = 4*32 },
346         { SERVICE_REC_FIELD(data64),
347           .offset_words = 40,
348           .offset_bits  = 0,
349           .size_bits    = 2*64 },
350 };
351
352 static void free_sm_ah(struct kref *kref)
353 {
354         struct ib_sa_sm_ah *sm_ah = container_of(kref, struct ib_sa_sm_ah, ref);
355
356         ib_destroy_ah(sm_ah->ah);
357         kfree(sm_ah);
358 }
359
360 static void update_sm_ah(struct work_struct *work)
361 {
362         struct ib_sa_port *port =
363                 container_of(work, struct ib_sa_port, update_task);
364         struct ib_sa_sm_ah *new_ah;
365         struct ib_port_attr port_attr;
366         struct ib_ah_attr   ah_attr;
367
368         if (ib_query_port(port->agent->device, port->port_num, &port_attr)) {
369                 printk(KERN_WARNING "Couldn't query port\n");
370                 return;
371         }
372
373         new_ah = kmalloc(sizeof *new_ah, GFP_KERNEL);
374         if (!new_ah) {
375                 printk(KERN_WARNING "Couldn't allocate new SM AH\n");
376                 return;
377         }
378
379         kref_init(&new_ah->ref);
380         new_ah->src_path_mask = (1 << port_attr.lmc) - 1;
381
382         new_ah->pkey_index = 0;
383         if (ib_find_pkey(port->agent->device, port->port_num,
384                          IB_DEFAULT_PKEY_FULL, &new_ah->pkey_index))
385                 printk(KERN_ERR "Couldn't find index for default PKey\n");
386
387         memset(&ah_attr, 0, sizeof ah_attr);
388         ah_attr.dlid     = port_attr.sm_lid;
389         ah_attr.sl       = port_attr.sm_sl;
390         ah_attr.port_num = port->port_num;
391
392         new_ah->ah = ib_create_ah(port->agent->qp->pd, &ah_attr);
393         if (IS_ERR(new_ah->ah)) {
394                 printk(KERN_WARNING "Couldn't create new SM AH\n");
395                 kfree(new_ah);
396                 return;
397         }
398
399         spin_lock_irq(&port->ah_lock);
400         port->sm_ah = new_ah;
401         spin_unlock_irq(&port->ah_lock);
402
403 }
404
405 static void ib_sa_event(struct ib_event_handler *handler, struct ib_event *event)
406 {
407         if (event->event == IB_EVENT_PORT_ERR    ||
408             event->event == IB_EVENT_PORT_ACTIVE ||
409             event->event == IB_EVENT_LID_CHANGE  ||
410             event->event == IB_EVENT_PKEY_CHANGE ||
411             event->event == IB_EVENT_SM_CHANGE   ||
412             event->event == IB_EVENT_CLIENT_REREGISTER) {
413                 unsigned long flags;
414                 struct ib_sa_device *sa_dev =
415                         container_of(handler, typeof(*sa_dev), event_handler);
416                 struct ib_sa_port *port =
417                         &sa_dev->port[event->element.port_num - sa_dev->start_port];
418
419                 spin_lock_irqsave(&port->ah_lock, flags);
420                 if (port->sm_ah)
421                         kref_put(&port->sm_ah->ref, free_sm_ah);
422                 port->sm_ah = NULL;
423                 spin_unlock_irqrestore(&port->ah_lock, flags);
424
425                 schedule_work(&sa_dev->port[event->element.port_num -
426                                             sa_dev->start_port].update_task);
427         }
428 }
429
430 void ib_sa_register_client(struct ib_sa_client *client)
431 {
432         atomic_set(&client->users, 1);
433         init_completion(&client->comp);
434 }
435 EXPORT_SYMBOL(ib_sa_register_client);
436
437 void ib_sa_unregister_client(struct ib_sa_client *client)
438 {
439         ib_sa_client_put(client);
440         wait_for_completion(&client->comp);
441 }
442 EXPORT_SYMBOL(ib_sa_unregister_client);
443
444 /**
445  * ib_sa_cancel_query - try to cancel an SA query
446  * @id:ID of query to cancel
447  * @query:query pointer to cancel
448  *
449  * Try to cancel an SA query.  If the id and query don't match up or
450  * the query has already completed, nothing is done.  Otherwise the
451  * query is canceled and will complete with a status of -EINTR.
452  */
453 void ib_sa_cancel_query(int id, struct ib_sa_query *query)
454 {
455         unsigned long flags;
456         struct ib_mad_agent *agent;
457         struct ib_mad_send_buf *mad_buf;
458
459         spin_lock_irqsave(&idr_lock, flags);
460         if (idr_find(&query_idr, id) != query) {
461                 spin_unlock_irqrestore(&idr_lock, flags);
462                 return;
463         }
464         agent = query->port->agent;
465         mad_buf = query->mad_buf;
466         spin_unlock_irqrestore(&idr_lock, flags);
467
468         ib_cancel_mad(agent, mad_buf);
469 }
470 EXPORT_SYMBOL(ib_sa_cancel_query);
471
472 static u8 get_src_path_mask(struct ib_device *device, u8 port_num)
473 {
474         struct ib_sa_device *sa_dev;
475         struct ib_sa_port   *port;
476         unsigned long flags;
477         u8 src_path_mask;
478
479         sa_dev = ib_get_client_data(device, &sa_client);
480         if (!sa_dev)
481                 return 0x7f;
482
483         port  = &sa_dev->port[port_num - sa_dev->start_port];
484         spin_lock_irqsave(&port->ah_lock, flags);
485         src_path_mask = port->sm_ah ? port->sm_ah->src_path_mask : 0x7f;
486         spin_unlock_irqrestore(&port->ah_lock, flags);
487
488         return src_path_mask;
489 }
490
491 int ib_init_ah_from_path(struct ib_device *device, u8 port_num,
492                          struct ib_sa_path_rec *rec, struct ib_ah_attr *ah_attr)
493 {
494         int ret;
495         u16 gid_index;
496
497         memset(ah_attr, 0, sizeof *ah_attr);
498         ah_attr->dlid = be16_to_cpu(rec->dlid);
499         ah_attr->sl = rec->sl;
500         ah_attr->src_path_bits = be16_to_cpu(rec->slid) &
501                                  get_src_path_mask(device, port_num);
502         ah_attr->port_num = port_num;
503         ah_attr->static_rate = rec->rate;
504
505         if (rec->hop_limit > 1) {
506                 ah_attr->ah_flags = IB_AH_GRH;
507                 ah_attr->grh.dgid = rec->dgid;
508
509                 ret = ib_find_cached_gid(device, &rec->sgid, &port_num,
510                                          &gid_index);
511                 if (ret)
512                         return ret;
513
514                 ah_attr->grh.sgid_index    = gid_index;
515                 ah_attr->grh.flow_label    = be32_to_cpu(rec->flow_label);
516                 ah_attr->grh.hop_limit     = rec->hop_limit;
517                 ah_attr->grh.traffic_class = rec->traffic_class;
518         }
519         return 0;
520 }
521 EXPORT_SYMBOL(ib_init_ah_from_path);
522
523 static int alloc_mad(struct ib_sa_query *query, gfp_t gfp_mask)
524 {
525         unsigned long flags;
526
527         spin_lock_irqsave(&query->port->ah_lock, flags);
528         if (!query->port->sm_ah) {
529                 spin_unlock_irqrestore(&query->port->ah_lock, flags);
530                 return -EAGAIN;
531         }
532         kref_get(&query->port->sm_ah->ref);
533         query->sm_ah = query->port->sm_ah;
534         spin_unlock_irqrestore(&query->port->ah_lock, flags);
535
536         query->mad_buf = ib_create_send_mad(query->port->agent, 1,
537                                             query->sm_ah->pkey_index,
538                                             0, IB_MGMT_SA_HDR, IB_MGMT_SA_DATA,
539                                             gfp_mask);
540         if (IS_ERR(query->mad_buf)) {
541                 kref_put(&query->sm_ah->ref, free_sm_ah);
542                 return -ENOMEM;
543         }
544
545         query->mad_buf->ah = query->sm_ah->ah;
546
547         return 0;
548 }
549
550 static void free_mad(struct ib_sa_query *query)
551 {
552         ib_free_send_mad(query->mad_buf);
553         kref_put(&query->sm_ah->ref, free_sm_ah);
554 }
555
556 static void init_mad(struct ib_sa_mad *mad, struct ib_mad_agent *agent)
557 {
558         unsigned long flags;
559
560         memset(mad, 0, sizeof *mad);
561
562         mad->mad_hdr.base_version  = IB_MGMT_BASE_VERSION;
563         mad->mad_hdr.mgmt_class    = IB_MGMT_CLASS_SUBN_ADM;
564         mad->mad_hdr.class_version = IB_SA_CLASS_VERSION;
565
566         spin_lock_irqsave(&tid_lock, flags);
567         mad->mad_hdr.tid           =
568                 cpu_to_be64(((u64) agent->hi_tid) << 32 | tid++);
569         spin_unlock_irqrestore(&tid_lock, flags);
570 }
571
572 static int send_mad(struct ib_sa_query *query, int timeout_ms, gfp_t gfp_mask)
573 {
574         unsigned long flags;
575         int ret, id;
576
577 retry:
578         if (!idr_pre_get(&query_idr, gfp_mask))
579                 return -ENOMEM;
580         spin_lock_irqsave(&idr_lock, flags);
581         ret = idr_get_new(&query_idr, query, &id);
582         spin_unlock_irqrestore(&idr_lock, flags);
583         if (ret == -EAGAIN)
584                 goto retry;
585         if (ret)
586                 return ret;
587
588         query->mad_buf->timeout_ms  = timeout_ms;
589         query->mad_buf->context[0] = query;
590         query->id = id;
591
592         ret = ib_post_send_mad(query->mad_buf, NULL);
593         if (ret) {
594                 spin_lock_irqsave(&idr_lock, flags);
595                 idr_remove(&query_idr, id);
596                 spin_unlock_irqrestore(&idr_lock, flags);
597         }
598
599         /*
600          * It's not safe to dereference query any more, because the
601          * send may already have completed and freed the query in
602          * another context.
603          */
604         return ret ? ret : id;
605 }
606
607 static void ib_sa_path_rec_callback(struct ib_sa_query *sa_query,
608                                     int status,
609                                     struct ib_sa_mad *mad)
610 {
611         struct ib_sa_path_query *query =
612                 container_of(sa_query, struct ib_sa_path_query, sa_query);
613
614         if (mad) {
615                 struct ib_sa_path_rec rec;
616
617                 ib_unpack(path_rec_table, ARRAY_SIZE(path_rec_table),
618                           mad->data, &rec);
619                 query->callback(status, &rec, query->context);
620         } else
621                 query->callback(status, NULL, query->context);
622 }
623
624 static void ib_sa_path_rec_release(struct ib_sa_query *sa_query)
625 {
626         kfree(container_of(sa_query, struct ib_sa_path_query, sa_query));
627 }
628
629 /**
630  * ib_sa_path_rec_get - Start a Path get query
631  * @client:SA client
632  * @device:device to send query on
633  * @port_num: port number to send query on
634  * @rec:Path Record to send in query
635  * @comp_mask:component mask to send in query
636  * @timeout_ms:time to wait for response
637  * @gfp_mask:GFP mask to use for internal allocations
638  * @callback:function called when query completes, times out or is
639  * canceled
640  * @context:opaque user context passed to callback
641  * @sa_query:query context, used to cancel query
642  *
643  * Send a Path Record Get query to the SA to look up a path.  The
644  * callback function will be called when the query completes (or
645  * fails); status is 0 for a successful response, -EINTR if the query
646  * is canceled, -ETIMEDOUT is the query timed out, or -EIO if an error
647  * occurred sending the query.  The resp parameter of the callback is
648  * only valid if status is 0.
649  *
650  * If the return value of ib_sa_path_rec_get() is negative, it is an
651  * error code.  Otherwise it is a query ID that can be used to cancel
652  * the query.
653  */
654 int ib_sa_path_rec_get(struct ib_sa_client *client,
655                        struct ib_device *device, u8 port_num,
656                        struct ib_sa_path_rec *rec,
657                        ib_sa_comp_mask comp_mask,
658                        int timeout_ms, gfp_t gfp_mask,
659                        void (*callback)(int status,
660                                         struct ib_sa_path_rec *resp,
661                                         void *context),
662                        void *context,
663                        struct ib_sa_query **sa_query)
664 {
665         struct ib_sa_path_query *query;
666         struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
667         struct ib_sa_port   *port;
668         struct ib_mad_agent *agent;
669         struct ib_sa_mad *mad;
670         int ret;
671
672         if (!sa_dev)
673                 return -ENODEV;
674
675         port  = &sa_dev->port[port_num - sa_dev->start_port];
676         agent = port->agent;
677
678         query = kmalloc(sizeof *query, gfp_mask);
679         if (!query)
680                 return -ENOMEM;
681
682         query->sa_query.port     = port;
683         ret = alloc_mad(&query->sa_query, gfp_mask);
684         if (ret)
685                 goto err1;
686
687         ib_sa_client_get(client);
688         query->sa_query.client = client;
689         query->callback        = callback;
690         query->context         = context;
691
692         mad = query->sa_query.mad_buf->mad;
693         init_mad(mad, agent);
694
695         query->sa_query.callback = callback ? ib_sa_path_rec_callback : NULL;
696         query->sa_query.release  = ib_sa_path_rec_release;
697         mad->mad_hdr.method      = IB_MGMT_METHOD_GET;
698         mad->mad_hdr.attr_id     = cpu_to_be16(IB_SA_ATTR_PATH_REC);
699         mad->sa_hdr.comp_mask    = comp_mask;
700
701         ib_pack(path_rec_table, ARRAY_SIZE(path_rec_table), rec, mad->data);
702
703         *sa_query = &query->sa_query;
704
705         ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
706         if (ret < 0)
707                 goto err2;
708
709         return ret;
710
711 err2:
712         *sa_query = NULL;
713         ib_sa_client_put(query->sa_query.client);
714         free_mad(&query->sa_query);
715
716 err1:
717         kfree(query);
718         return ret;
719 }
720 EXPORT_SYMBOL(ib_sa_path_rec_get);
721
722 static void ib_sa_service_rec_callback(struct ib_sa_query *sa_query,
723                                     int status,
724                                     struct ib_sa_mad *mad)
725 {
726         struct ib_sa_service_query *query =
727                 container_of(sa_query, struct ib_sa_service_query, sa_query);
728
729         if (mad) {
730                 struct ib_sa_service_rec rec;
731
732                 ib_unpack(service_rec_table, ARRAY_SIZE(service_rec_table),
733                           mad->data, &rec);
734                 query->callback(status, &rec, query->context);
735         } else
736                 query->callback(status, NULL, query->context);
737 }
738
739 static void ib_sa_service_rec_release(struct ib_sa_query *sa_query)
740 {
741         kfree(container_of(sa_query, struct ib_sa_service_query, sa_query));
742 }
743
744 /**
745  * ib_sa_service_rec_query - Start Service Record operation
746  * @client:SA client
747  * @device:device to send request on
748  * @port_num: port number to send request on
749  * @method:SA method - should be get, set, or delete
750  * @rec:Service Record to send in request
751  * @comp_mask:component mask to send in request
752  * @timeout_ms:time to wait for response
753  * @gfp_mask:GFP mask to use for internal allocations
754  * @callback:function called when request completes, times out or is
755  * canceled
756  * @context:opaque user context passed to callback
757  * @sa_query:request context, used to cancel request
758  *
759  * Send a Service Record set/get/delete to the SA to register,
760  * unregister or query a service record.
761  * The callback function will be called when the request completes (or
762  * fails); status is 0 for a successful response, -EINTR if the query
763  * is canceled, -ETIMEDOUT is the query timed out, or -EIO if an error
764  * occurred sending the query.  The resp parameter of the callback is
765  * only valid if status is 0.
766  *
767  * If the return value of ib_sa_service_rec_query() is negative, it is an
768  * error code.  Otherwise it is a request ID that can be used to cancel
769  * the query.
770  */
771 int ib_sa_service_rec_query(struct ib_sa_client *client,
772                             struct ib_device *device, u8 port_num, u8 method,
773                             struct ib_sa_service_rec *rec,
774                             ib_sa_comp_mask comp_mask,
775                             int timeout_ms, gfp_t gfp_mask,
776                             void (*callback)(int status,
777                                              struct ib_sa_service_rec *resp,
778                                              void *context),
779                             void *context,
780                             struct ib_sa_query **sa_query)
781 {
782         struct ib_sa_service_query *query;
783         struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
784         struct ib_sa_port   *port;
785         struct ib_mad_agent *agent;
786         struct ib_sa_mad *mad;
787         int ret;
788
789         if (!sa_dev)
790                 return -ENODEV;
791
792         port  = &sa_dev->port[port_num - sa_dev->start_port];
793         agent = port->agent;
794
795         if (method != IB_MGMT_METHOD_GET &&
796             method != IB_MGMT_METHOD_SET &&
797             method != IB_SA_METHOD_DELETE)
798                 return -EINVAL;
799
800         query = kmalloc(sizeof *query, gfp_mask);
801         if (!query)
802                 return -ENOMEM;
803
804         query->sa_query.port     = port;
805         ret = alloc_mad(&query->sa_query, gfp_mask);
806         if (ret)
807                 goto err1;
808
809         ib_sa_client_get(client);
810         query->sa_query.client = client;
811         query->callback        = callback;
812         query->context         = context;
813
814         mad = query->sa_query.mad_buf->mad;
815         init_mad(mad, agent);
816
817         query->sa_query.callback = callback ? ib_sa_service_rec_callback : NULL;
818         query->sa_query.release  = ib_sa_service_rec_release;
819         mad->mad_hdr.method      = method;
820         mad->mad_hdr.attr_id     = cpu_to_be16(IB_SA_ATTR_SERVICE_REC);
821         mad->sa_hdr.comp_mask    = comp_mask;
822
823         ib_pack(service_rec_table, ARRAY_SIZE(service_rec_table),
824                 rec, mad->data);
825
826         *sa_query = &query->sa_query;
827
828         ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
829         if (ret < 0)
830                 goto err2;
831
832         return ret;
833
834 err2:
835         *sa_query = NULL;
836         ib_sa_client_put(query->sa_query.client);
837         free_mad(&query->sa_query);
838
839 err1:
840         kfree(query);
841         return ret;
842 }
843 EXPORT_SYMBOL(ib_sa_service_rec_query);
844
845 static void ib_sa_mcmember_rec_callback(struct ib_sa_query *sa_query,
846                                         int status,
847                                         struct ib_sa_mad *mad)
848 {
849         struct ib_sa_mcmember_query *query =
850                 container_of(sa_query, struct ib_sa_mcmember_query, sa_query);
851
852         if (mad) {
853                 struct ib_sa_mcmember_rec rec;
854
855                 ib_unpack(mcmember_rec_table, ARRAY_SIZE(mcmember_rec_table),
856                           mad->data, &rec);
857                 query->callback(status, &rec, query->context);
858         } else
859                 query->callback(status, NULL, query->context);
860 }
861
862 static void ib_sa_mcmember_rec_release(struct ib_sa_query *sa_query)
863 {
864         kfree(container_of(sa_query, struct ib_sa_mcmember_query, sa_query));
865 }
866
867 int ib_sa_mcmember_rec_query(struct ib_sa_client *client,
868                              struct ib_device *device, u8 port_num,
869                              u8 method,
870                              struct ib_sa_mcmember_rec *rec,
871                              ib_sa_comp_mask comp_mask,
872                              int timeout_ms, gfp_t gfp_mask,
873                              void (*callback)(int status,
874                                               struct ib_sa_mcmember_rec *resp,
875                                               void *context),
876                              void *context,
877                              struct ib_sa_query **sa_query)
878 {
879         struct ib_sa_mcmember_query *query;
880         struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
881         struct ib_sa_port   *port;
882         struct ib_mad_agent *agent;
883         struct ib_sa_mad *mad;
884         int ret;
885
886         if (!sa_dev)
887                 return -ENODEV;
888
889         port  = &sa_dev->port[port_num - sa_dev->start_port];
890         agent = port->agent;
891
892         query = kmalloc(sizeof *query, gfp_mask);
893         if (!query)
894                 return -ENOMEM;
895
896         query->sa_query.port     = port;
897         ret = alloc_mad(&query->sa_query, gfp_mask);
898         if (ret)
899                 goto err1;
900
901         ib_sa_client_get(client);
902         query->sa_query.client = client;
903         query->callback        = callback;
904         query->context         = context;
905
906         mad = query->sa_query.mad_buf->mad;
907         init_mad(mad, agent);
908
909         query->sa_query.callback = callback ? ib_sa_mcmember_rec_callback : NULL;
910         query->sa_query.release  = ib_sa_mcmember_rec_release;
911         mad->mad_hdr.method      = method;
912         mad->mad_hdr.attr_id     = cpu_to_be16(IB_SA_ATTR_MC_MEMBER_REC);
913         mad->sa_hdr.comp_mask    = comp_mask;
914
915         ib_pack(mcmember_rec_table, ARRAY_SIZE(mcmember_rec_table),
916                 rec, mad->data);
917
918         *sa_query = &query->sa_query;
919
920         ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
921         if (ret < 0)
922                 goto err2;
923
924         return ret;
925
926 err2:
927         *sa_query = NULL;
928         ib_sa_client_put(query->sa_query.client);
929         free_mad(&query->sa_query);
930
931 err1:
932         kfree(query);
933         return ret;
934 }
935
936 static void send_handler(struct ib_mad_agent *agent,
937                          struct ib_mad_send_wc *mad_send_wc)
938 {
939         struct ib_sa_query *query = mad_send_wc->send_buf->context[0];
940         unsigned long flags;
941
942         if (query->callback)
943                 switch (mad_send_wc->status) {
944                 case IB_WC_SUCCESS:
945                         /* No callback -- already got recv */
946                         break;
947                 case IB_WC_RESP_TIMEOUT_ERR:
948                         query->callback(query, -ETIMEDOUT, NULL);
949                         break;
950                 case IB_WC_WR_FLUSH_ERR:
951                         query->callback(query, -EINTR, NULL);
952                         break;
953                 default:
954                         query->callback(query, -EIO, NULL);
955                         break;
956                 }
957
958         spin_lock_irqsave(&idr_lock, flags);
959         idr_remove(&query_idr, query->id);
960         spin_unlock_irqrestore(&idr_lock, flags);
961
962         free_mad(query);
963         ib_sa_client_put(query->client);
964         query->release(query);
965 }
966
967 static void recv_handler(struct ib_mad_agent *mad_agent,
968                          struct ib_mad_recv_wc *mad_recv_wc)
969 {
970         struct ib_sa_query *query;
971         struct ib_mad_send_buf *mad_buf;
972
973         mad_buf = (void *) (unsigned long) mad_recv_wc->wc->wr_id;
974         query = mad_buf->context[0];
975
976         if (query->callback) {
977                 if (mad_recv_wc->wc->status == IB_WC_SUCCESS)
978                         query->callback(query,
979                                         mad_recv_wc->recv_buf.mad->mad_hdr.status ?
980                                         -EINVAL : 0,
981                                         (struct ib_sa_mad *) mad_recv_wc->recv_buf.mad);
982                 else
983                         query->callback(query, -EIO, NULL);
984         }
985
986         ib_free_recv_mad(mad_recv_wc);
987 }
988
989 static void ib_sa_add_one(struct ib_device *device)
990 {
991         struct ib_sa_device *sa_dev;
992         int s, e, i;
993
994         if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
995                 return;
996
997         if (device->node_type == RDMA_NODE_IB_SWITCH)
998                 s = e = 0;
999         else {
1000                 s = 1;
1001                 e = device->phys_port_cnt;
1002         }
1003
1004         sa_dev = kmalloc(sizeof *sa_dev +
1005                          (e - s + 1) * sizeof (struct ib_sa_port),
1006                          GFP_KERNEL);
1007         if (!sa_dev)
1008                 return;
1009
1010         sa_dev->start_port = s;
1011         sa_dev->end_port   = e;
1012
1013         for (i = 0; i <= e - s; ++i) {
1014                 sa_dev->port[i].sm_ah    = NULL;
1015                 sa_dev->port[i].port_num = i + s;
1016                 spin_lock_init(&sa_dev->port[i].ah_lock);
1017
1018                 sa_dev->port[i].agent =
1019                         ib_register_mad_agent(device, i + s, IB_QPT_GSI,
1020                                               NULL, 0, send_handler,
1021                                               recv_handler, sa_dev);
1022                 if (IS_ERR(sa_dev->port[i].agent))
1023                         goto err;
1024
1025                 INIT_WORK(&sa_dev->port[i].update_task, update_sm_ah);
1026         }
1027
1028         ib_set_client_data(device, &sa_client, sa_dev);
1029
1030         /*
1031          * We register our event handler after everything is set up,
1032          * and then update our cached info after the event handler is
1033          * registered to avoid any problems if a port changes state
1034          * during our initialization.
1035          */
1036
1037         INIT_IB_EVENT_HANDLER(&sa_dev->event_handler, device, ib_sa_event);
1038         if (ib_register_event_handler(&sa_dev->event_handler))
1039                 goto err;
1040
1041         for (i = 0; i <= e - s; ++i)
1042                 update_sm_ah(&sa_dev->port[i].update_task);
1043
1044         return;
1045
1046 err:
1047         while (--i >= 0)
1048                 ib_unregister_mad_agent(sa_dev->port[i].agent);
1049
1050         kfree(sa_dev);
1051
1052         return;
1053 }
1054
1055 static void ib_sa_remove_one(struct ib_device *device)
1056 {
1057         struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
1058         int i;
1059
1060         if (!sa_dev)
1061                 return;
1062
1063         ib_unregister_event_handler(&sa_dev->event_handler);
1064
1065         flush_scheduled_work();
1066
1067         for (i = 0; i <= sa_dev->end_port - sa_dev->start_port; ++i) {
1068                 ib_unregister_mad_agent(sa_dev->port[i].agent);
1069                 kref_put(&sa_dev->port[i].sm_ah->ref, free_sm_ah);
1070         }
1071
1072         kfree(sa_dev);
1073 }
1074
1075 static int __init ib_sa_init(void)
1076 {
1077         int ret;
1078
1079         spin_lock_init(&idr_lock);
1080         spin_lock_init(&tid_lock);
1081
1082         get_random_bytes(&tid, sizeof tid);
1083
1084         ret = ib_register_client(&sa_client);
1085         if (ret) {
1086                 printk(KERN_ERR "Couldn't register ib_sa client\n");
1087                 goto err1;
1088         }
1089
1090         ret = mcast_init();
1091         if (ret) {
1092                 printk(KERN_ERR "Couldn't initialize multicast handling\n");
1093                 goto err2;
1094         }
1095
1096         return 0;
1097 err2:
1098         ib_unregister_client(&sa_client);
1099 err1:
1100         return ret;
1101 }
1102
1103 static void __exit ib_sa_cleanup(void)
1104 {
1105         mcast_cleanup();
1106         ib_unregister_client(&sa_client);
1107         idr_destroy(&query_idr);
1108 }
1109
1110 module_init(ib_sa_init);
1111 module_exit(ib_sa_cleanup);