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