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