]> err.no Git - linux-2.6/blob - drivers/infiniband/core/sa_query.c
[PATCH] IB: Update MAD client API
[linux-2.6] / drivers / infiniband / core / sa_query.c
1 /*
2  * Copyright (c) 2004 Topspin Communications.  All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  *
32  * $Id: sa_query.c 1389 2004-12-27 22:56:47Z roland $
33  */
34
35 #include <linux/module.h>
36 #include <linux/init.h>
37 #include <linux/err.h>
38 #include <linux/random.h>
39 #include <linux/spinlock.h>
40 #include <linux/slab.h>
41 #include <linux/pci.h>
42 #include <linux/dma-mapping.h>
43 #include <linux/kref.h>
44 #include <linux/idr.h>
45
46 #include <ib_pack.h>
47 #include <ib_sa.h>
48
49 MODULE_AUTHOR("Roland Dreier");
50 MODULE_DESCRIPTION("InfiniBand subnet administration query support");
51 MODULE_LICENSE("Dual BSD/GPL");
52
53 /*
54  * These two structures must be packed because they have 64-bit fields
55  * that are only 32-bit aligned.  64-bit architectures will lay them
56  * out wrong otherwise.  (And unfortunately they are sent on the wire
57  * so we can't change the layout)
58  */
59 struct ib_sa_hdr {
60         u64                     sm_key;
61         u16                     attr_offset;
62         u16                     reserved;
63         ib_sa_comp_mask         comp_mask;
64 } __attribute__ ((packed));
65
66 struct ib_sa_mad {
67         struct ib_mad_hdr       mad_hdr;
68         struct ib_rmpp_hdr      rmpp_hdr;
69         struct ib_sa_hdr        sa_hdr;
70         u8                      data[200];
71 } __attribute__ ((packed));
72
73 struct ib_sa_sm_ah {
74         struct ib_ah        *ah;
75         struct kref          ref;
76 };
77
78 struct ib_sa_port {
79         struct ib_mad_agent *agent;
80         struct ib_sa_sm_ah  *sm_ah;
81         struct work_struct   update_task;
82         spinlock_t           ah_lock;
83         u8                   port_num;
84 };
85
86 struct ib_sa_device {
87         int                     start_port, end_port;
88         struct ib_event_handler event_handler;
89         struct ib_sa_port port[0];
90 };
91
92 struct ib_sa_query {
93         void (*callback)(struct ib_sa_query *, int, struct ib_sa_mad *);
94         void (*release)(struct ib_sa_query *);
95         struct ib_sa_port  *port;
96         struct ib_sa_mad   *mad;
97         struct ib_sa_sm_ah *sm_ah;
98         DECLARE_PCI_UNMAP_ADDR(mapping)
99         int                 id;
100 };
101
102 struct ib_sa_path_query {
103         void (*callback)(int, struct ib_sa_path_rec *, void *);
104         void *context;
105         struct ib_sa_query sa_query;
106 };
107
108 struct ib_sa_mcmember_query {
109         void (*callback)(int, struct ib_sa_mcmember_rec *, void *);
110         void *context;
111         struct ib_sa_query sa_query;
112 };
113
114 static void ib_sa_add_one(struct ib_device *device);
115 static void ib_sa_remove_one(struct ib_device *device);
116
117 static struct ib_client sa_client = {
118         .name   = "sa",
119         .add    = ib_sa_add_one,
120         .remove = ib_sa_remove_one
121 };
122
123 static spinlock_t idr_lock;
124 static DEFINE_IDR(query_idr);
125
126 static spinlock_t tid_lock;
127 static u32 tid;
128
129 enum {
130         IB_SA_ATTR_CLASS_PORTINFO    = 0x01,
131         IB_SA_ATTR_NOTICE            = 0x02,
132         IB_SA_ATTR_INFORM_INFO       = 0x03,
133         IB_SA_ATTR_NODE_REC          = 0x11,
134         IB_SA_ATTR_PORT_INFO_REC     = 0x12,
135         IB_SA_ATTR_SL2VL_REC         = 0x13,
136         IB_SA_ATTR_SWITCH_REC        = 0x14,
137         IB_SA_ATTR_LINEAR_FDB_REC    = 0x15,
138         IB_SA_ATTR_RANDOM_FDB_REC    = 0x16,
139         IB_SA_ATTR_MCAST_FDB_REC     = 0x17,
140         IB_SA_ATTR_SM_INFO_REC       = 0x18,
141         IB_SA_ATTR_LINK_REC          = 0x20,
142         IB_SA_ATTR_GUID_INFO_REC     = 0x30,
143         IB_SA_ATTR_SERVICE_REC       = 0x31,
144         IB_SA_ATTR_PARTITION_REC     = 0x33,
145         IB_SA_ATTR_RANGE_REC         = 0x34,
146         IB_SA_ATTR_PATH_REC          = 0x35,
147         IB_SA_ATTR_VL_ARB_REC        = 0x36,
148         IB_SA_ATTR_MC_GROUP_REC      = 0x37,
149         IB_SA_ATTR_MC_MEMBER_REC     = 0x38,
150         IB_SA_ATTR_TRACE_REC         = 0x39,
151         IB_SA_ATTR_MULTI_PATH_REC    = 0x3a,
152         IB_SA_ATTR_SERVICE_ASSOC_REC = 0x3b
153 };
154
155 #define PATH_REC_FIELD(field) \
156         .struct_offset_bytes = offsetof(struct ib_sa_path_rec, field),          \
157         .struct_size_bytes   = sizeof ((struct ib_sa_path_rec *) 0)->field,     \
158         .field_name          = "sa_path_rec:" #field
159
160 static const struct ib_field path_rec_table[] = {
161         { RESERVED,
162           .offset_words = 0,
163           .offset_bits  = 0,
164           .size_bits    = 32 },
165         { RESERVED,
166           .offset_words = 1,
167           .offset_bits  = 0,
168           .size_bits    = 32 },
169         { PATH_REC_FIELD(dgid),
170           .offset_words = 2,
171           .offset_bits  = 0,
172           .size_bits    = 128 },
173         { PATH_REC_FIELD(sgid),
174           .offset_words = 6,
175           .offset_bits  = 0,
176           .size_bits    = 128 },
177         { PATH_REC_FIELD(dlid),
178           .offset_words = 10,
179           .offset_bits  = 0,
180           .size_bits    = 16 },
181         { PATH_REC_FIELD(slid),
182           .offset_words = 10,
183           .offset_bits  = 16,
184           .size_bits    = 16 },
185         { PATH_REC_FIELD(raw_traffic),
186           .offset_words = 11,
187           .offset_bits  = 0,
188           .size_bits    = 1 },
189         { RESERVED,
190           .offset_words = 11,
191           .offset_bits  = 1,
192           .size_bits    = 3 },
193         { PATH_REC_FIELD(flow_label),
194           .offset_words = 11,
195           .offset_bits  = 4,
196           .size_bits    = 20 },
197         { PATH_REC_FIELD(hop_limit),
198           .offset_words = 11,
199           .offset_bits  = 24,
200           .size_bits    = 8 },
201         { PATH_REC_FIELD(traffic_class),
202           .offset_words = 12,
203           .offset_bits  = 0,
204           .size_bits    = 8 },
205         { PATH_REC_FIELD(reversible),
206           .offset_words = 12,
207           .offset_bits  = 8,
208           .size_bits    = 1 },
209         { PATH_REC_FIELD(numb_path),
210           .offset_words = 12,
211           .offset_bits  = 9,
212           .size_bits    = 7 },
213         { PATH_REC_FIELD(pkey),
214           .offset_words = 12,
215           .offset_bits  = 16,
216           .size_bits    = 16 },
217         { RESERVED,
218           .offset_words = 13,
219           .offset_bits  = 0,
220           .size_bits    = 12 },
221         { PATH_REC_FIELD(sl),
222           .offset_words = 13,
223           .offset_bits  = 12,
224           .size_bits    = 4 },
225         { PATH_REC_FIELD(mtu_selector),
226           .offset_words = 13,
227           .offset_bits  = 16,
228           .size_bits    = 2 },
229         { PATH_REC_FIELD(mtu),
230           .offset_words = 13,
231           .offset_bits  = 18,
232           .size_bits    = 6 },
233         { PATH_REC_FIELD(rate_selector),
234           .offset_words = 13,
235           .offset_bits  = 24,
236           .size_bits    = 2 },
237         { PATH_REC_FIELD(rate),
238           .offset_words = 13,
239           .offset_bits  = 26,
240           .size_bits    = 6 },
241         { PATH_REC_FIELD(packet_life_time_selector),
242           .offset_words = 14,
243           .offset_bits  = 0,
244           .size_bits    = 2 },
245         { PATH_REC_FIELD(packet_life_time),
246           .offset_words = 14,
247           .offset_bits  = 2,
248           .size_bits    = 6 },
249         { PATH_REC_FIELD(preference),
250           .offset_words = 14,
251           .offset_bits  = 8,
252           .size_bits    = 8 },
253         { RESERVED,
254           .offset_words = 14,
255           .offset_bits  = 16,
256           .size_bits    = 48 },
257 };
258
259 #define MCMEMBER_REC_FIELD(field) \
260         .struct_offset_bytes = offsetof(struct ib_sa_mcmember_rec, field),      \
261         .struct_size_bytes   = sizeof ((struct ib_sa_mcmember_rec *) 0)->field, \
262         .field_name          = "sa_mcmember_rec:" #field
263
264 static const struct ib_field mcmember_rec_table[] = {
265         { MCMEMBER_REC_FIELD(mgid),
266           .offset_words = 0,
267           .offset_bits  = 0,
268           .size_bits    = 128 },
269         { MCMEMBER_REC_FIELD(port_gid),
270           .offset_words = 4,
271           .offset_bits  = 0,
272           .size_bits    = 128 },
273         { MCMEMBER_REC_FIELD(qkey),
274           .offset_words = 8,
275           .offset_bits  = 0,
276           .size_bits    = 32 },
277         { MCMEMBER_REC_FIELD(mlid),
278           .offset_words = 9,
279           .offset_bits  = 0,
280           .size_bits    = 16 },
281         { MCMEMBER_REC_FIELD(mtu_selector),
282           .offset_words = 9,
283           .offset_bits  = 16,
284           .size_bits    = 2 },
285         { MCMEMBER_REC_FIELD(mtu),
286           .offset_words = 9,
287           .offset_bits  = 18,
288           .size_bits    = 6 },
289         { MCMEMBER_REC_FIELD(traffic_class),
290           .offset_words = 9,
291           .offset_bits  = 24,
292           .size_bits    = 8 },
293         { MCMEMBER_REC_FIELD(pkey),
294           .offset_words = 10,
295           .offset_bits  = 0,
296           .size_bits    = 16 },
297         { MCMEMBER_REC_FIELD(rate_selector),
298           .offset_words = 10,
299           .offset_bits  = 16,
300           .size_bits    = 2 },
301         { MCMEMBER_REC_FIELD(rate),
302           .offset_words = 10,
303           .offset_bits  = 18,
304           .size_bits    = 6 },
305         { MCMEMBER_REC_FIELD(packet_life_time_selector),
306           .offset_words = 10,
307           .offset_bits  = 24,
308           .size_bits    = 2 },
309         { MCMEMBER_REC_FIELD(packet_life_time),
310           .offset_words = 10,
311           .offset_bits  = 26,
312           .size_bits    = 6 },
313         { MCMEMBER_REC_FIELD(sl),
314           .offset_words = 11,
315           .offset_bits  = 0,
316           .size_bits    = 4 },
317         { MCMEMBER_REC_FIELD(flow_label),
318           .offset_words = 11,
319           .offset_bits  = 4,
320           .size_bits    = 20 },
321         { MCMEMBER_REC_FIELD(hop_limit),
322           .offset_words = 11,
323           .offset_bits  = 24,
324           .size_bits    = 8 },
325         { MCMEMBER_REC_FIELD(scope),
326           .offset_words = 12,
327           .offset_bits  = 0,
328           .size_bits    = 4 },
329         { MCMEMBER_REC_FIELD(join_state),
330           .offset_words = 12,
331           .offset_bits  = 4,
332           .size_bits    = 4 },
333         { MCMEMBER_REC_FIELD(proxy_join),
334           .offset_words = 12,
335           .offset_bits  = 8,
336           .size_bits    = 1 },
337         { RESERVED,
338           .offset_words = 12,
339           .offset_bits  = 9,
340           .size_bits    = 23 },
341 };
342
343 static void free_sm_ah(struct kref *kref)
344 {
345         struct ib_sa_sm_ah *sm_ah = container_of(kref, struct ib_sa_sm_ah, ref);
346
347         ib_destroy_ah(sm_ah->ah);
348         kfree(sm_ah);
349 }
350
351 static void update_sm_ah(void *port_ptr)
352 {
353         struct ib_sa_port *port = port_ptr;
354         struct ib_sa_sm_ah *new_ah, *old_ah;
355         struct ib_port_attr port_attr;
356         struct ib_ah_attr   ah_attr;
357
358         if (ib_query_port(port->agent->device, port->port_num, &port_attr)) {
359                 printk(KERN_WARNING "Couldn't query port\n");
360                 return;
361         }
362
363         new_ah = kmalloc(sizeof *new_ah, GFP_KERNEL);
364         if (!new_ah) {
365                 printk(KERN_WARNING "Couldn't allocate new SM AH\n");
366                 return;
367         }
368
369         kref_init(&new_ah->ref);
370
371         memset(&ah_attr, 0, sizeof ah_attr);
372         ah_attr.dlid     = port_attr.sm_lid;
373         ah_attr.sl       = port_attr.sm_sl;
374         ah_attr.port_num = port->port_num;
375
376         new_ah->ah = ib_create_ah(port->agent->qp->pd, &ah_attr);
377         if (IS_ERR(new_ah->ah)) {
378                 printk(KERN_WARNING "Couldn't create new SM AH\n");
379                 kfree(new_ah);
380                 return;
381         }
382
383         spin_lock_irq(&port->ah_lock);
384         old_ah = port->sm_ah;
385         port->sm_ah = new_ah;
386         spin_unlock_irq(&port->ah_lock);
387
388         if (old_ah)
389                 kref_put(&old_ah->ref, free_sm_ah);
390 }
391
392 static void ib_sa_event(struct ib_event_handler *handler, struct ib_event *event)
393 {
394         if (event->event == IB_EVENT_PORT_ERR    ||
395             event->event == IB_EVENT_PORT_ACTIVE ||
396             event->event == IB_EVENT_LID_CHANGE  ||
397             event->event == IB_EVENT_PKEY_CHANGE ||
398             event->event == IB_EVENT_SM_CHANGE) {
399                 struct ib_sa_device *sa_dev =
400                         ib_get_client_data(event->device, &sa_client);
401
402                 schedule_work(&sa_dev->port[event->element.port_num -
403                                             sa_dev->start_port].update_task);
404         }
405 }
406
407 /**
408  * ib_sa_cancel_query - try to cancel an SA query
409  * @id:ID of query to cancel
410  * @query:query pointer to cancel
411  *
412  * Try to cancel an SA query.  If the id and query don't match up or
413  * the query has already completed, nothing is done.  Otherwise the
414  * query is canceled and will complete with a status of -EINTR.
415  */
416 void ib_sa_cancel_query(int id, struct ib_sa_query *query)
417 {
418         unsigned long flags;
419         struct ib_mad_agent *agent;
420
421         spin_lock_irqsave(&idr_lock, flags);
422         if (idr_find(&query_idr, id) != query) {
423                 spin_unlock_irqrestore(&idr_lock, flags);
424                 return;
425         }
426         agent = query->port->agent;
427         spin_unlock_irqrestore(&idr_lock, flags);
428
429         ib_cancel_mad(agent, id);
430 }
431 EXPORT_SYMBOL(ib_sa_cancel_query);
432
433 static void init_mad(struct ib_sa_mad *mad, struct ib_mad_agent *agent)
434 {
435         unsigned long flags;
436
437         memset(mad, 0, sizeof *mad);
438
439         mad->mad_hdr.base_version  = IB_MGMT_BASE_VERSION;
440         mad->mad_hdr.mgmt_class    = IB_MGMT_CLASS_SUBN_ADM;
441         mad->mad_hdr.class_version = IB_SA_CLASS_VERSION;
442
443         spin_lock_irqsave(&tid_lock, flags);
444         mad->mad_hdr.tid           =
445                 cpu_to_be64(((u64) agent->hi_tid) << 32 | tid++);
446         spin_unlock_irqrestore(&tid_lock, flags);
447 }
448
449 static int send_mad(struct ib_sa_query *query, int timeout_ms)
450 {
451         struct ib_sa_port *port = query->port;
452         unsigned long flags;
453         int ret;
454         struct ib_sge      gather_list;
455         struct ib_send_wr *bad_wr, wr = {
456                 .opcode      = IB_WR_SEND,
457                 .sg_list     = &gather_list,
458                 .num_sge     = 1,
459                 .send_flags  = IB_SEND_SIGNALED,
460                 .wr          = {
461                          .ud = {
462                                  .mad_hdr     = &query->mad->mad_hdr,
463                                  .remote_qpn  = 1,
464                                  .remote_qkey = IB_QP1_QKEY,
465                                  .timeout_ms  = timeout_ms
466                          }
467                  }
468         };
469
470 retry:
471         if (!idr_pre_get(&query_idr, GFP_ATOMIC))
472                 return -ENOMEM;
473         spin_lock_irqsave(&idr_lock, flags);
474         ret = idr_get_new(&query_idr, query, &query->id);
475         spin_unlock_irqrestore(&idr_lock, flags);
476         if (ret == -EAGAIN)
477                 goto retry;
478         if (ret)
479                 return ret;
480
481         wr.wr_id = query->id;
482
483         spin_lock_irqsave(&port->ah_lock, flags);
484         kref_get(&port->sm_ah->ref);
485         query->sm_ah = port->sm_ah;
486         wr.wr.ud.ah  = port->sm_ah->ah;
487         spin_unlock_irqrestore(&port->ah_lock, flags);
488
489         gather_list.addr   = dma_map_single(port->agent->device->dma_device,
490                                             query->mad,
491                                             sizeof (struct ib_sa_mad),
492                                             DMA_TO_DEVICE);
493         gather_list.length = sizeof (struct ib_sa_mad);
494         gather_list.lkey   = port->agent->mr->lkey;
495         pci_unmap_addr_set(query, mapping, gather_list.addr);
496
497         ret = ib_post_send_mad(port->agent, &wr, &bad_wr);
498         if (ret) {
499                 dma_unmap_single(port->agent->device->dma_device,
500                                  pci_unmap_addr(query, mapping),
501                                  sizeof (struct ib_sa_mad),
502                                  DMA_TO_DEVICE);
503                 kref_put(&query->sm_ah->ref, free_sm_ah);
504                 spin_lock_irqsave(&idr_lock, flags);
505                 idr_remove(&query_idr, query->id);
506                 spin_unlock_irqrestore(&idr_lock, flags);
507         }
508
509         /*
510          * It's not safe to dereference query any more, because the
511          * send may already have completed and freed the query in
512          * another context.  So use wr.wr_id, which has a copy of the
513          * query's id.
514          */
515         return ret ? ret : wr.wr_id;
516 }
517
518 static void ib_sa_path_rec_callback(struct ib_sa_query *sa_query,
519                                     int status,
520                                     struct ib_sa_mad *mad)
521 {
522         struct ib_sa_path_query *query =
523                 container_of(sa_query, struct ib_sa_path_query, sa_query);
524
525         if (mad) {
526                 struct ib_sa_path_rec rec;
527
528                 ib_unpack(path_rec_table, ARRAY_SIZE(path_rec_table),
529                           mad->data, &rec);
530                 query->callback(status, &rec, query->context);
531         } else
532                 query->callback(status, NULL, query->context);
533 }
534
535 static void ib_sa_path_rec_release(struct ib_sa_query *sa_query)
536 {
537         kfree(sa_query->mad);
538         kfree(container_of(sa_query, struct ib_sa_path_query, sa_query));
539 }
540
541 /**
542  * ib_sa_path_rec_get - Start a Path get query
543  * @device:device to send query on
544  * @port_num: port number to send query on
545  * @rec:Path Record to send in query
546  * @comp_mask:component mask to send in query
547  * @timeout_ms:time to wait for response
548  * @gfp_mask:GFP mask to use for internal allocations
549  * @callback:function called when query completes, times out or is
550  * canceled
551  * @context:opaque user context passed to callback
552  * @sa_query:query context, used to cancel query
553  *
554  * Send a Path Record Get query to the SA to look up a path.  The
555  * callback function will be called when the query completes (or
556  * fails); status is 0 for a successful response, -EINTR if the query
557  * is canceled, -ETIMEDOUT is the query timed out, or -EIO if an error
558  * occurred sending the query.  The resp parameter of the callback is
559  * only valid if status is 0.
560  *
561  * If the return value of ib_sa_path_rec_get() is negative, it is an
562  * error code.  Otherwise it is a query ID that can be used to cancel
563  * the query.
564  */
565 int ib_sa_path_rec_get(struct ib_device *device, u8 port_num,
566                        struct ib_sa_path_rec *rec,
567                        ib_sa_comp_mask comp_mask,
568                        int timeout_ms, int gfp_mask,
569                        void (*callback)(int status,
570                                         struct ib_sa_path_rec *resp,
571                                         void *context),
572                        void *context,
573                        struct ib_sa_query **sa_query)
574 {
575         struct ib_sa_path_query *query;
576         struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
577         struct ib_sa_port   *port   = &sa_dev->port[port_num - sa_dev->start_port];
578         struct ib_mad_agent *agent  = port->agent;
579         int ret;
580
581         query = kmalloc(sizeof *query, gfp_mask);
582         if (!query)
583                 return -ENOMEM;
584         query->sa_query.mad = kmalloc(sizeof *query->sa_query.mad, gfp_mask);
585         if (!query->sa_query.mad) {
586                 kfree(query);
587                 return -ENOMEM;
588         }
589
590         query->callback = callback;
591         query->context  = context;
592
593         init_mad(query->sa_query.mad, agent);
594
595         query->sa_query.callback              = callback ? ib_sa_path_rec_callback : NULL;
596         query->sa_query.release               = ib_sa_path_rec_release;
597         query->sa_query.port                  = port;
598         query->sa_query.mad->mad_hdr.method   = IB_MGMT_METHOD_GET;
599         query->sa_query.mad->mad_hdr.attr_id  = cpu_to_be16(IB_SA_ATTR_PATH_REC);
600         query->sa_query.mad->sa_hdr.comp_mask = comp_mask;
601
602         ib_pack(path_rec_table, ARRAY_SIZE(path_rec_table),
603                 rec, query->sa_query.mad->data);
604
605         *sa_query = &query->sa_query;
606
607         ret = send_mad(&query->sa_query, timeout_ms);
608         if (ret < 0) {
609                 *sa_query = NULL;
610                 kfree(query->sa_query.mad);
611                 kfree(query);
612         }
613
614         return ret;
615 }
616 EXPORT_SYMBOL(ib_sa_path_rec_get);
617
618 static void ib_sa_mcmember_rec_callback(struct ib_sa_query *sa_query,
619                                         int status,
620                                         struct ib_sa_mad *mad)
621 {
622         struct ib_sa_mcmember_query *query =
623                 container_of(sa_query, struct ib_sa_mcmember_query, sa_query);
624
625         if (mad) {
626                 struct ib_sa_mcmember_rec rec;
627
628                 ib_unpack(mcmember_rec_table, ARRAY_SIZE(mcmember_rec_table),
629                           mad->data, &rec);
630                 query->callback(status, &rec, query->context);
631         } else
632                 query->callback(status, NULL, query->context);
633 }
634
635 static void ib_sa_mcmember_rec_release(struct ib_sa_query *sa_query)
636 {
637         kfree(sa_query->mad);
638         kfree(container_of(sa_query, struct ib_sa_mcmember_query, sa_query));
639 }
640
641 int ib_sa_mcmember_rec_query(struct ib_device *device, u8 port_num,
642                              u8 method,
643                              struct ib_sa_mcmember_rec *rec,
644                              ib_sa_comp_mask comp_mask,
645                              int timeout_ms, int gfp_mask,
646                              void (*callback)(int status,
647                                               struct ib_sa_mcmember_rec *resp,
648                                               void *context),
649                              void *context,
650                              struct ib_sa_query **sa_query)
651 {
652         struct ib_sa_mcmember_query *query;
653         struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
654         struct ib_sa_port   *port   = &sa_dev->port[port_num - sa_dev->start_port];
655         struct ib_mad_agent *agent  = port->agent;
656         int ret;
657
658         query = kmalloc(sizeof *query, gfp_mask);
659         if (!query)
660                 return -ENOMEM;
661         query->sa_query.mad = kmalloc(sizeof *query->sa_query.mad, gfp_mask);
662         if (!query->sa_query.mad) {
663                 kfree(query);
664                 return -ENOMEM;
665         }
666
667         query->callback = callback;
668         query->context  = context;
669
670         init_mad(query->sa_query.mad, agent);
671
672         query->sa_query.callback              = callback ? ib_sa_mcmember_rec_callback : NULL;
673         query->sa_query.release               = ib_sa_mcmember_rec_release;
674         query->sa_query.port                  = port;
675         query->sa_query.mad->mad_hdr.method   = method;
676         query->sa_query.mad->mad_hdr.attr_id  = cpu_to_be16(IB_SA_ATTR_MC_MEMBER_REC);
677         query->sa_query.mad->sa_hdr.comp_mask = comp_mask;
678
679         ib_pack(mcmember_rec_table, ARRAY_SIZE(mcmember_rec_table),
680                 rec, query->sa_query.mad->data);
681
682         *sa_query = &query->sa_query;
683
684         ret = send_mad(&query->sa_query, timeout_ms);
685         if (ret < 0) {
686                 *sa_query = NULL;
687                 kfree(query->sa_query.mad);
688                 kfree(query);
689         }
690
691         return ret;
692 }
693 EXPORT_SYMBOL(ib_sa_mcmember_rec_query);
694
695 static void send_handler(struct ib_mad_agent *agent,
696                          struct ib_mad_send_wc *mad_send_wc)
697 {
698         struct ib_sa_query *query;
699         unsigned long flags;
700
701         spin_lock_irqsave(&idr_lock, flags);
702         query = idr_find(&query_idr, mad_send_wc->wr_id);
703         spin_unlock_irqrestore(&idr_lock, flags);
704
705         if (!query)
706                 return;
707
708         if (query->callback)
709                 switch (mad_send_wc->status) {
710                 case IB_WC_SUCCESS:
711                         /* No callback -- already got recv */
712                         break;
713                 case IB_WC_RESP_TIMEOUT_ERR:
714                         query->callback(query, -ETIMEDOUT, NULL);
715                         break;
716                 case IB_WC_WR_FLUSH_ERR:
717                         query->callback(query, -EINTR, NULL);
718                         break;
719                 default:
720                         query->callback(query, -EIO, NULL);
721                         break;
722                 }
723
724         dma_unmap_single(agent->device->dma_device,
725                          pci_unmap_addr(query, mapping),
726                          sizeof (struct ib_sa_mad),
727                          DMA_TO_DEVICE);
728         kref_put(&query->sm_ah->ref, free_sm_ah);
729
730         query->release(query);
731
732         spin_lock_irqsave(&idr_lock, flags);
733         idr_remove(&query_idr, mad_send_wc->wr_id);
734         spin_unlock_irqrestore(&idr_lock, flags);
735 }
736
737 static void recv_handler(struct ib_mad_agent *mad_agent,
738                          struct ib_mad_recv_wc *mad_recv_wc)
739 {
740         struct ib_sa_query *query;
741         unsigned long flags;
742
743         spin_lock_irqsave(&idr_lock, flags);
744         query = idr_find(&query_idr, mad_recv_wc->wc->wr_id);
745         spin_unlock_irqrestore(&idr_lock, flags);
746
747         if (query && query->callback) {
748                 if (mad_recv_wc->wc->status == IB_WC_SUCCESS)
749                         query->callback(query,
750                                         mad_recv_wc->recv_buf.mad->mad_hdr.status ?
751                                         -EINVAL : 0,
752                                         (struct ib_sa_mad *) mad_recv_wc->recv_buf.mad);
753                 else
754                         query->callback(query, -EIO, NULL);
755         }
756
757         ib_free_recv_mad(mad_recv_wc);
758 }
759
760 static void ib_sa_add_one(struct ib_device *device)
761 {
762         struct ib_sa_device *sa_dev;
763         int s, e, i;
764
765         if (device->node_type == IB_NODE_SWITCH)
766                 s = e = 0;
767         else {
768                 s = 1;
769                 e = device->phys_port_cnt;
770         }
771
772         sa_dev = kmalloc(sizeof *sa_dev +
773                          (e - s + 1) * sizeof (struct ib_sa_port),
774                          GFP_KERNEL);
775         if (!sa_dev)
776                 return;
777
778         sa_dev->start_port = s;
779         sa_dev->end_port   = e;
780
781         for (i = 0; i <= e - s; ++i) {
782                 sa_dev->port[i].sm_ah    = NULL;
783                 sa_dev->port[i].port_num = i + s;
784                 spin_lock_init(&sa_dev->port[i].ah_lock);
785
786                 sa_dev->port[i].agent =
787                         ib_register_mad_agent(device, i + s, IB_QPT_GSI,
788                                               NULL, 0, send_handler,
789                                               recv_handler, sa_dev);
790                 if (IS_ERR(sa_dev->port[i].agent))
791                         goto err;
792
793                 INIT_WORK(&sa_dev->port[i].update_task,
794                           update_sm_ah, &sa_dev->port[i]);
795         }
796
797         ib_set_client_data(device, &sa_client, sa_dev);
798
799         /*
800          * We register our event handler after everything is set up,
801          * and then update our cached info after the event handler is
802          * registered to avoid any problems if a port changes state
803          * during our initialization.
804          */
805
806         INIT_IB_EVENT_HANDLER(&sa_dev->event_handler, device, ib_sa_event);
807         if (ib_register_event_handler(&sa_dev->event_handler))
808                 goto err;
809
810         for (i = 0; i <= e - s; ++i)
811                 update_sm_ah(&sa_dev->port[i]);
812
813         return;
814
815 err:
816         while (--i >= 0)
817                 ib_unregister_mad_agent(sa_dev->port[i].agent);
818
819         kfree(sa_dev);
820
821         return;
822 }
823
824 static void ib_sa_remove_one(struct ib_device *device)
825 {
826         struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
827         int i;
828
829         if (!sa_dev)
830                 return;
831
832         ib_unregister_event_handler(&sa_dev->event_handler);
833
834         for (i = 0; i <= sa_dev->end_port - sa_dev->start_port; ++i) {
835                 ib_unregister_mad_agent(sa_dev->port[i].agent);
836                 kref_put(&sa_dev->port[i].sm_ah->ref, free_sm_ah);
837         }
838
839         kfree(sa_dev);
840 }
841
842 static int __init ib_sa_init(void)
843 {
844         int ret;
845
846         spin_lock_init(&idr_lock);
847         spin_lock_init(&tid_lock);
848
849         get_random_bytes(&tid, sizeof tid);
850
851         ret = ib_register_client(&sa_client);
852         if (ret)
853                 printk(KERN_ERR "Couldn't register ib_sa client\n");
854
855         return ret;
856 }
857
858 static void __exit ib_sa_cleanup(void)
859 {
860         ib_unregister_client(&sa_client);
861 }
862
863 module_init(ib_sa_init);
864 module_exit(ib_sa_cleanup);