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