blob: 1706d3c7e95eaf2f8f8315cfab1897fb940eeaa8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (c) 2004 Topspin Communications. All rights reserved.
Roland Dreier2a1d9b72005-08-10 23:03:10 -07003 * Copyright (c) 2005 Voltaire, Inc.  All rights reserved.
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07004 * Copyright (c) 2006 Intel Corporation. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
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 *
Hal Rosenstockcbae32c2005-07-27 11:45:38 -070034 * $Id: sa_query.c 2811 2005-07-06 18:11:43Z halr $
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 */
36
37#include <linux/module.h>
38#include <linux/init.h>
39#include <linux/err.h>
40#include <linux/random.h>
41#include <linux/spinlock.h>
42#include <linux/slab.h>
43#include <linux/pci.h>
44#include <linux/dma-mapping.h>
45#include <linux/kref.h>
46#include <linux/idr.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080047#include <linux/workqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Roland Dreiera4d61e82005-08-25 13:40:04 -070049#include <rdma/ib_pack.h>
50#include <rdma/ib_sa.h>
Sean Hefty6d969a42006-06-17 20:37:39 -070051#include <rdma/ib_cache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53MODULE_AUTHOR("Roland Dreier");
54MODULE_DESCRIPTION("InfiniBand subnet administration query support");
55MODULE_LICENSE("Dual BSD/GPL");
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057struct ib_sa_sm_ah {
58 struct ib_ah *ah;
59 struct kref ref;
60};
61
62struct ib_sa_port {
63 struct ib_mad_agent *agent;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 struct ib_sa_sm_ah *sm_ah;
65 struct work_struct update_task;
66 spinlock_t ah_lock;
67 u8 port_num;
68};
69
70struct ib_sa_device {
71 int start_port, end_port;
72 struct ib_event_handler event_handler;
73 struct ib_sa_port port[0];
74};
75
76struct ib_sa_query {
77 void (*callback)(struct ib_sa_query *, int, struct ib_sa_mad *);
78 void (*release)(struct ib_sa_query *);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -070079 struct ib_sa_client *client;
Sean Hefty34816ad2005-10-25 10:51:39 -070080 struct ib_sa_port *port;
81 struct ib_mad_send_buf *mad_buf;
82 struct ib_sa_sm_ah *sm_ah;
83 int id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084};
85
Hal Rosenstockcbae32c2005-07-27 11:45:38 -070086struct ib_sa_service_query {
87 void (*callback)(int, struct ib_sa_service_rec *, void *);
88 void *context;
89 struct ib_sa_query sa_query;
90};
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092struct ib_sa_path_query {
93 void (*callback)(int, struct ib_sa_path_rec *, void *);
94 void *context;
95 struct ib_sa_query sa_query;
96};
97
98struct ib_sa_mcmember_query {
99 void (*callback)(int, struct ib_sa_mcmember_rec *, void *);
100 void *context;
101 struct ib_sa_query sa_query;
102};
103
104static void ib_sa_add_one(struct ib_device *device);
105static void ib_sa_remove_one(struct ib_device *device);
106
107static struct ib_client sa_client = {
108 .name = "sa",
109 .add = ib_sa_add_one,
110 .remove = ib_sa_remove_one
111};
112
113static spinlock_t idr_lock;
114static DEFINE_IDR(query_idr);
115
116static spinlock_t tid_lock;
117static u32 tid;
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119#define PATH_REC_FIELD(field) \
120 .struct_offset_bytes = offsetof(struct ib_sa_path_rec, field), \
121 .struct_size_bytes = sizeof ((struct ib_sa_path_rec *) 0)->field, \
122 .field_name = "sa_path_rec:" #field
123
124static const struct ib_field path_rec_table[] = {
125 { RESERVED,
126 .offset_words = 0,
127 .offset_bits = 0,
128 .size_bits = 32 },
129 { RESERVED,
130 .offset_words = 1,
131 .offset_bits = 0,
132 .size_bits = 32 },
133 { PATH_REC_FIELD(dgid),
134 .offset_words = 2,
135 .offset_bits = 0,
136 .size_bits = 128 },
137 { PATH_REC_FIELD(sgid),
138 .offset_words = 6,
139 .offset_bits = 0,
140 .size_bits = 128 },
141 { PATH_REC_FIELD(dlid),
142 .offset_words = 10,
143 .offset_bits = 0,
144 .size_bits = 16 },
145 { PATH_REC_FIELD(slid),
146 .offset_words = 10,
147 .offset_bits = 16,
148 .size_bits = 16 },
149 { PATH_REC_FIELD(raw_traffic),
150 .offset_words = 11,
151 .offset_bits = 0,
152 .size_bits = 1 },
153 { RESERVED,
154 .offset_words = 11,
155 .offset_bits = 1,
156 .size_bits = 3 },
157 { PATH_REC_FIELD(flow_label),
158 .offset_words = 11,
159 .offset_bits = 4,
160 .size_bits = 20 },
161 { PATH_REC_FIELD(hop_limit),
162 .offset_words = 11,
163 .offset_bits = 24,
164 .size_bits = 8 },
165 { PATH_REC_FIELD(traffic_class),
166 .offset_words = 12,
167 .offset_bits = 0,
168 .size_bits = 8 },
169 { PATH_REC_FIELD(reversible),
170 .offset_words = 12,
171 .offset_bits = 8,
172 .size_bits = 1 },
173 { PATH_REC_FIELD(numb_path),
174 .offset_words = 12,
175 .offset_bits = 9,
176 .size_bits = 7 },
177 { PATH_REC_FIELD(pkey),
178 .offset_words = 12,
179 .offset_bits = 16,
180 .size_bits = 16 },
181 { RESERVED,
182 .offset_words = 13,
183 .offset_bits = 0,
184 .size_bits = 12 },
185 { PATH_REC_FIELD(sl),
186 .offset_words = 13,
187 .offset_bits = 12,
188 .size_bits = 4 },
189 { PATH_REC_FIELD(mtu_selector),
190 .offset_words = 13,
191 .offset_bits = 16,
192 .size_bits = 2 },
193 { PATH_REC_FIELD(mtu),
194 .offset_words = 13,
195 .offset_bits = 18,
196 .size_bits = 6 },
197 { PATH_REC_FIELD(rate_selector),
198 .offset_words = 13,
199 .offset_bits = 24,
200 .size_bits = 2 },
201 { PATH_REC_FIELD(rate),
202 .offset_words = 13,
203 .offset_bits = 26,
204 .size_bits = 6 },
205 { PATH_REC_FIELD(packet_life_time_selector),
206 .offset_words = 14,
207 .offset_bits = 0,
208 .size_bits = 2 },
209 { PATH_REC_FIELD(packet_life_time),
210 .offset_words = 14,
211 .offset_bits = 2,
212 .size_bits = 6 },
213 { PATH_REC_FIELD(preference),
214 .offset_words = 14,
215 .offset_bits = 8,
216 .size_bits = 8 },
217 { RESERVED,
218 .offset_words = 14,
219 .offset_bits = 16,
220 .size_bits = 48 },
221};
222
223#define MCMEMBER_REC_FIELD(field) \
224 .struct_offset_bytes = offsetof(struct ib_sa_mcmember_rec, field), \
225 .struct_size_bytes = sizeof ((struct ib_sa_mcmember_rec *) 0)->field, \
226 .field_name = "sa_mcmember_rec:" #field
227
228static const struct ib_field mcmember_rec_table[] = {
229 { MCMEMBER_REC_FIELD(mgid),
230 .offset_words = 0,
231 .offset_bits = 0,
232 .size_bits = 128 },
233 { MCMEMBER_REC_FIELD(port_gid),
234 .offset_words = 4,
235 .offset_bits = 0,
236 .size_bits = 128 },
237 { MCMEMBER_REC_FIELD(qkey),
238 .offset_words = 8,
239 .offset_bits = 0,
240 .size_bits = 32 },
241 { MCMEMBER_REC_FIELD(mlid),
242 .offset_words = 9,
243 .offset_bits = 0,
244 .size_bits = 16 },
245 { MCMEMBER_REC_FIELD(mtu_selector),
246 .offset_words = 9,
247 .offset_bits = 16,
248 .size_bits = 2 },
249 { MCMEMBER_REC_FIELD(mtu),
250 .offset_words = 9,
251 .offset_bits = 18,
252 .size_bits = 6 },
253 { MCMEMBER_REC_FIELD(traffic_class),
254 .offset_words = 9,
255 .offset_bits = 24,
256 .size_bits = 8 },
257 { MCMEMBER_REC_FIELD(pkey),
258 .offset_words = 10,
259 .offset_bits = 0,
260 .size_bits = 16 },
261 { MCMEMBER_REC_FIELD(rate_selector),
262 .offset_words = 10,
263 .offset_bits = 16,
264 .size_bits = 2 },
265 { MCMEMBER_REC_FIELD(rate),
266 .offset_words = 10,
267 .offset_bits = 18,
268 .size_bits = 6 },
269 { MCMEMBER_REC_FIELD(packet_life_time_selector),
270 .offset_words = 10,
271 .offset_bits = 24,
272 .size_bits = 2 },
273 { MCMEMBER_REC_FIELD(packet_life_time),
274 .offset_words = 10,
275 .offset_bits = 26,
276 .size_bits = 6 },
277 { MCMEMBER_REC_FIELD(sl),
278 .offset_words = 11,
279 .offset_bits = 0,
280 .size_bits = 4 },
281 { MCMEMBER_REC_FIELD(flow_label),
282 .offset_words = 11,
283 .offset_bits = 4,
284 .size_bits = 20 },
285 { MCMEMBER_REC_FIELD(hop_limit),
286 .offset_words = 11,
287 .offset_bits = 24,
288 .size_bits = 8 },
289 { MCMEMBER_REC_FIELD(scope),
290 .offset_words = 12,
291 .offset_bits = 0,
292 .size_bits = 4 },
293 { MCMEMBER_REC_FIELD(join_state),
294 .offset_words = 12,
295 .offset_bits = 4,
296 .size_bits = 4 },
297 { MCMEMBER_REC_FIELD(proxy_join),
298 .offset_words = 12,
299 .offset_bits = 8,
300 .size_bits = 1 },
301 { RESERVED,
302 .offset_words = 12,
303 .offset_bits = 9,
304 .size_bits = 23 },
305};
306
Hal Rosenstockcbae32c2005-07-27 11:45:38 -0700307#define SERVICE_REC_FIELD(field) \
308 .struct_offset_bytes = offsetof(struct ib_sa_service_rec, field), \
309 .struct_size_bytes = sizeof ((struct ib_sa_service_rec *) 0)->field, \
310 .field_name = "sa_service_rec:" #field
311
312static const struct ib_field service_rec_table[] = {
313 { SERVICE_REC_FIELD(id),
314 .offset_words = 0,
315 .offset_bits = 0,
316 .size_bits = 64 },
317 { SERVICE_REC_FIELD(gid),
318 .offset_words = 2,
319 .offset_bits = 0,
320 .size_bits = 128 },
321 { SERVICE_REC_FIELD(pkey),
322 .offset_words = 6,
323 .offset_bits = 0,
324 .size_bits = 16 },
325 { SERVICE_REC_FIELD(lease),
326 .offset_words = 7,
327 .offset_bits = 0,
328 .size_bits = 32 },
329 { SERVICE_REC_FIELD(key),
330 .offset_words = 8,
331 .offset_bits = 0,
332 .size_bits = 128 },
333 { SERVICE_REC_FIELD(name),
334 .offset_words = 12,
335 .offset_bits = 0,
336 .size_bits = 64*8 },
337 { SERVICE_REC_FIELD(data8),
338 .offset_words = 28,
339 .offset_bits = 0,
340 .size_bits = 16*8 },
341 { SERVICE_REC_FIELD(data16),
342 .offset_words = 32,
343 .offset_bits = 0,
344 .size_bits = 8*16 },
345 { SERVICE_REC_FIELD(data32),
346 .offset_words = 36,
347 .offset_bits = 0,
348 .size_bits = 4*32 },
349 { SERVICE_REC_FIELD(data64),
350 .offset_words = 40,
351 .offset_bits = 0,
352 .size_bits = 2*64 },
353};
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355static void free_sm_ah(struct kref *kref)
356{
357 struct ib_sa_sm_ah *sm_ah = container_of(kref, struct ib_sa_sm_ah, ref);
358
359 ib_destroy_ah(sm_ah->ah);
360 kfree(sm_ah);
361}
362
363static void update_sm_ah(void *port_ptr)
364{
365 struct ib_sa_port *port = port_ptr;
366 struct ib_sa_sm_ah *new_ah, *old_ah;
367 struct ib_port_attr port_attr;
368 struct ib_ah_attr ah_attr;
369
370 if (ib_query_port(port->agent->device, port->port_num, &port_attr)) {
371 printk(KERN_WARNING "Couldn't query port\n");
372 return;
373 }
374
375 new_ah = kmalloc(sizeof *new_ah, GFP_KERNEL);
376 if (!new_ah) {
377 printk(KERN_WARNING "Couldn't allocate new SM AH\n");
378 return;
379 }
380
381 kref_init(&new_ah->ref);
382
383 memset(&ah_attr, 0, sizeof ah_attr);
384 ah_attr.dlid = port_attr.sm_lid;
385 ah_attr.sl = port_attr.sm_sl;
386 ah_attr.port_num = port->port_num;
387
388 new_ah->ah = ib_create_ah(port->agent->qp->pd, &ah_attr);
389 if (IS_ERR(new_ah->ah)) {
390 printk(KERN_WARNING "Couldn't create new SM AH\n");
391 kfree(new_ah);
392 return;
393 }
394
395 spin_lock_irq(&port->ah_lock);
396 old_ah = port->sm_ah;
397 port->sm_ah = new_ah;
398 spin_unlock_irq(&port->ah_lock);
399
400 if (old_ah)
401 kref_put(&old_ah->ref, free_sm_ah);
402}
403
404static void ib_sa_event(struct ib_event_handler *handler, struct ib_event *event)
405{
406 if (event->event == IB_EVENT_PORT_ERR ||
407 event->event == IB_EVENT_PORT_ACTIVE ||
408 event->event == IB_EVENT_LID_CHANGE ||
409 event->event == IB_EVENT_PKEY_CHANGE ||
Jack Morgensteinacaea9e2006-08-15 17:20:50 +0300410 event->event == IB_EVENT_SM_CHANGE ||
411 event->event == IB_EVENT_CLIENT_REREGISTER) {
Michael S. Tsirkin1d6801f92005-09-01 09:19:44 -0700412 struct ib_sa_device *sa_dev;
413 sa_dev = container_of(handler, typeof(*sa_dev), event_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 schedule_work(&sa_dev->port[event->element.port_num -
416 sa_dev->start_port].update_task);
417 }
418}
419
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -0700420void ib_sa_register_client(struct ib_sa_client *client)
421{
422 atomic_set(&client->users, 1);
423 init_completion(&client->comp);
424}
425EXPORT_SYMBOL(ib_sa_register_client);
426
427static inline void ib_sa_client_get(struct ib_sa_client *client)
428{
429 atomic_inc(&client->users);
430}
431
432static inline void ib_sa_client_put(struct ib_sa_client *client)
433{
434 if (atomic_dec_and_test(&client->users))
435 complete(&client->comp);
436}
437
438void ib_sa_unregister_client(struct ib_sa_client *client)
439{
440 ib_sa_client_put(client);
441 wait_for_completion(&client->comp);
442}
443EXPORT_SYMBOL(ib_sa_unregister_client);
444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445/**
446 * ib_sa_cancel_query - try to cancel an SA query
447 * @id:ID of query to cancel
448 * @query:query pointer to cancel
449 *
450 * Try to cancel an SA query. If the id and query don't match up or
451 * the query has already completed, nothing is done. Otherwise the
452 * query is canceled and will complete with a status of -EINTR.
453 */
454void ib_sa_cancel_query(int id, struct ib_sa_query *query)
455{
456 unsigned long flags;
457 struct ib_mad_agent *agent;
Sean Hefty34816ad2005-10-25 10:51:39 -0700458 struct ib_mad_send_buf *mad_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 spin_lock_irqsave(&idr_lock, flags);
461 if (idr_find(&query_idr, id) != query) {
462 spin_unlock_irqrestore(&idr_lock, flags);
463 return;
464 }
465 agent = query->port->agent;
Sean Hefty34816ad2005-10-25 10:51:39 -0700466 mad_buf = query->mad_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 spin_unlock_irqrestore(&idr_lock, flags);
468
Sean Hefty34816ad2005-10-25 10:51:39 -0700469 ib_cancel_mad(agent, mad_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470}
471EXPORT_SYMBOL(ib_sa_cancel_query);
472
Sean Hefty6d969a42006-06-17 20:37:39 -0700473int ib_init_ah_from_path(struct ib_device *device, u8 port_num,
474 struct ib_sa_path_rec *rec, struct ib_ah_attr *ah_attr)
475{
476 int ret;
477 u16 gid_index;
478
479 memset(ah_attr, 0, sizeof *ah_attr);
480 ah_attr->dlid = be16_to_cpu(rec->dlid);
481 ah_attr->sl = rec->sl;
482 ah_attr->src_path_bits = be16_to_cpu(rec->slid) & 0x7f;
483 ah_attr->port_num = port_num;
484
485 if (rec->hop_limit > 1) {
486 ah_attr->ah_flags = IB_AH_GRH;
487 ah_attr->grh.dgid = rec->dgid;
488
489 ret = ib_find_cached_gid(device, &rec->sgid, &port_num,
490 &gid_index);
491 if (ret)
492 return ret;
493
Sean Heftyca222c62006-06-17 20:37:40 -0700494 ah_attr->grh.sgid_index = gid_index;
495 ah_attr->grh.flow_label = be32_to_cpu(rec->flow_label);
496 ah_attr->grh.hop_limit = rec->hop_limit;
Sean Hefty6d969a42006-06-17 20:37:39 -0700497 ah_attr->grh.traffic_class = rec->traffic_class;
498 }
499 return 0;
500}
501EXPORT_SYMBOL(ib_init_ah_from_path);
502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503static void init_mad(struct ib_sa_mad *mad, struct ib_mad_agent *agent)
504{
505 unsigned long flags;
506
507 memset(mad, 0, sizeof *mad);
508
509 mad->mad_hdr.base_version = IB_MGMT_BASE_VERSION;
510 mad->mad_hdr.mgmt_class = IB_MGMT_CLASS_SUBN_ADM;
511 mad->mad_hdr.class_version = IB_SA_CLASS_VERSION;
512
513 spin_lock_irqsave(&tid_lock, flags);
514 mad->mad_hdr.tid =
515 cpu_to_be64(((u64) agent->hi_tid) << 32 | tid++);
516 spin_unlock_irqrestore(&tid_lock, flags);
517}
518
Michael S. Tsirkine322fed2006-07-14 00:23:56 -0700519static int send_mad(struct ib_sa_query *query, int timeout_ms, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 unsigned long flags;
Sean Hefty34816ad2005-10-25 10:51:39 -0700522 int ret, id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524retry:
Michael S. Tsirkine322fed2006-07-14 00:23:56 -0700525 if (!idr_pre_get(&query_idr, gfp_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 return -ENOMEM;
527 spin_lock_irqsave(&idr_lock, flags);
Sean Hefty34816ad2005-10-25 10:51:39 -0700528 ret = idr_get_new(&query_idr, query, &id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 spin_unlock_irqrestore(&idr_lock, flags);
530 if (ret == -EAGAIN)
531 goto retry;
532 if (ret)
533 return ret;
534
Sean Hefty34816ad2005-10-25 10:51:39 -0700535 query->mad_buf->timeout_ms = timeout_ms;
536 query->mad_buf->context[0] = query;
537 query->id = id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Sean Hefty34816ad2005-10-25 10:51:39 -0700539 spin_lock_irqsave(&query->port->ah_lock, flags);
540 kref_get(&query->port->sm_ah->ref);
541 query->sm_ah = query->port->sm_ah;
542 spin_unlock_irqrestore(&query->port->ah_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Sean Hefty34816ad2005-10-25 10:51:39 -0700544 query->mad_buf->ah = query->sm_ah->ah;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Sean Hefty34816ad2005-10-25 10:51:39 -0700546 ret = ib_post_send_mad(query->mad_buf, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 spin_lock_irqsave(&idr_lock, flags);
Sean Hefty34816ad2005-10-25 10:51:39 -0700549 idr_remove(&query_idr, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 spin_unlock_irqrestore(&idr_lock, flags);
Sean Hefty34816ad2005-10-25 10:51:39 -0700551
552 kref_put(&query->sm_ah->ref, free_sm_ah);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
554
Roland Dreierdae4c1d2005-06-27 14:36:46 -0700555 /*
556 * It's not safe to dereference query any more, because the
557 * send may already have completed and freed the query in
Sean Hefty34816ad2005-10-25 10:51:39 -0700558 * another context.
Roland Dreierdae4c1d2005-06-27 14:36:46 -0700559 */
Sean Hefty34816ad2005-10-25 10:51:39 -0700560 return ret ? ret : id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561}
562
563static void ib_sa_path_rec_callback(struct ib_sa_query *sa_query,
564 int status,
565 struct ib_sa_mad *mad)
566{
567 struct ib_sa_path_query *query =
568 container_of(sa_query, struct ib_sa_path_query, sa_query);
569
570 if (mad) {
571 struct ib_sa_path_rec rec;
572
573 ib_unpack(path_rec_table, ARRAY_SIZE(path_rec_table),
574 mad->data, &rec);
575 query->callback(status, &rec, query->context);
576 } else
577 query->callback(status, NULL, query->context);
578}
579
580static void ib_sa_path_rec_release(struct ib_sa_query *sa_query)
581{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 kfree(container_of(sa_query, struct ib_sa_path_query, sa_query));
583}
584
585/**
586 * ib_sa_path_rec_get - Start a Path get query
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -0700587 * @client:SA client
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 * @device:device to send query on
589 * @port_num: port number to send query on
590 * @rec:Path Record to send in query
591 * @comp_mask:component mask to send in query
592 * @timeout_ms:time to wait for response
593 * @gfp_mask:GFP mask to use for internal allocations
594 * @callback:function called when query completes, times out or is
595 * canceled
596 * @context:opaque user context passed to callback
597 * @sa_query:query context, used to cancel query
598 *
599 * Send a Path Record Get query to the SA to look up a path. The
600 * callback function will be called when the query completes (or
601 * fails); status is 0 for a successful response, -EINTR if the query
602 * is canceled, -ETIMEDOUT is the query timed out, or -EIO if an error
603 * occurred sending the query. The resp parameter of the callback is
604 * only valid if status is 0.
605 *
606 * If the return value of ib_sa_path_rec_get() is negative, it is an
607 * error code. Otherwise it is a query ID that can be used to cancel
608 * the query.
609 */
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -0700610int ib_sa_path_rec_get(struct ib_sa_client *client,
611 struct ib_device *device, u8 port_num,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 struct ib_sa_path_rec *rec,
613 ib_sa_comp_mask comp_mask,
Al Virodd0fc662005-10-07 07:46:04 +0100614 int timeout_ms, gfp_t gfp_mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 void (*callback)(int status,
616 struct ib_sa_path_rec *resp,
617 void *context),
618 void *context,
619 struct ib_sa_query **sa_query)
620{
621 struct ib_sa_path_query *query;
622 struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
Roland Dreier56c202d2005-10-13 10:45:02 -0700623 struct ib_sa_port *port;
624 struct ib_mad_agent *agent;
Sean Hefty34816ad2005-10-25 10:51:39 -0700625 struct ib_sa_mad *mad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 int ret;
627
Roland Dreier56c202d2005-10-13 10:45:02 -0700628 if (!sa_dev)
629 return -ENODEV;
630
631 port = &sa_dev->port[port_num - sa_dev->start_port];
632 agent = port->agent;
633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 query = kmalloc(sizeof *query, gfp_mask);
635 if (!query)
636 return -ENOMEM;
Sean Hefty34816ad2005-10-25 10:51:39 -0700637
638 query->sa_query.mad_buf = ib_create_send_mad(agent, 1, 0,
639 0, IB_MGMT_SA_HDR,
640 IB_MGMT_SA_DATA, gfp_mask);
641 if (!query->sa_query.mad_buf) {
642 ret = -ENOMEM;
643 goto err1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 }
645
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -0700646 ib_sa_client_get(client);
647 query->sa_query.client = client;
648 query->callback = callback;
649 query->context = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Sean Hefty34816ad2005-10-25 10:51:39 -0700651 mad = query->sa_query.mad_buf->mad;
652 init_mad(mad, agent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Sean Hefty34816ad2005-10-25 10:51:39 -0700654 query->sa_query.callback = callback ? ib_sa_path_rec_callback : NULL;
655 query->sa_query.release = ib_sa_path_rec_release;
656 query->sa_query.port = port;
657 mad->mad_hdr.method = IB_MGMT_METHOD_GET;
658 mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_PATH_REC);
659 mad->sa_hdr.comp_mask = comp_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Sean Hefty34816ad2005-10-25 10:51:39 -0700661 ib_pack(path_rec_table, ARRAY_SIZE(path_rec_table), rec, mad->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
663 *sa_query = &query->sa_query;
Roland Dreierdae4c1d2005-06-27 14:36:46 -0700664
Michael S. Tsirkine322fed2006-07-14 00:23:56 -0700665 ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
Sean Hefty34816ad2005-10-25 10:51:39 -0700666 if (ret < 0)
667 goto err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Roland Dreierdae4c1d2005-06-27 14:36:46 -0700669 return ret;
Sean Hefty34816ad2005-10-25 10:51:39 -0700670
671err2:
672 *sa_query = NULL;
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -0700673 ib_sa_client_put(query->sa_query.client);
Sean Hefty34816ad2005-10-25 10:51:39 -0700674 ib_free_send_mad(query->sa_query.mad_buf);
675
676err1:
677 kfree(query);
678 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679}
680EXPORT_SYMBOL(ib_sa_path_rec_get);
681
Hal Rosenstockcbae32c2005-07-27 11:45:38 -0700682static void ib_sa_service_rec_callback(struct ib_sa_query *sa_query,
683 int status,
684 struct ib_sa_mad *mad)
685{
686 struct ib_sa_service_query *query =
687 container_of(sa_query, struct ib_sa_service_query, sa_query);
688
689 if (mad) {
690 struct ib_sa_service_rec rec;
691
692 ib_unpack(service_rec_table, ARRAY_SIZE(service_rec_table),
693 mad->data, &rec);
694 query->callback(status, &rec, query->context);
695 } else
696 query->callback(status, NULL, query->context);
697}
698
699static void ib_sa_service_rec_release(struct ib_sa_query *sa_query)
700{
Hal Rosenstockcbae32c2005-07-27 11:45:38 -0700701 kfree(container_of(sa_query, struct ib_sa_service_query, sa_query));
702}
703
704/**
705 * ib_sa_service_rec_query - Start Service Record operation
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -0700706 * @client:SA client
Hal Rosenstockcbae32c2005-07-27 11:45:38 -0700707 * @device:device to send request on
708 * @port_num: port number to send request on
709 * @method:SA method - should be get, set, or delete
710 * @rec:Service Record to send in request
711 * @comp_mask:component mask to send in request
712 * @timeout_ms:time to wait for response
713 * @gfp_mask:GFP mask to use for internal allocations
714 * @callback:function called when request completes, times out or is
715 * canceled
716 * @context:opaque user context passed to callback
717 * @sa_query:request context, used to cancel request
718 *
719 * Send a Service Record set/get/delete to the SA to register,
720 * unregister or query a service record.
721 * The callback function will be called when the request completes (or
722 * fails); status is 0 for a successful response, -EINTR if the query
723 * is canceled, -ETIMEDOUT is the query timed out, or -EIO if an error
724 * occurred sending the query. The resp parameter of the callback is
725 * only valid if status is 0.
726 *
727 * If the return value of ib_sa_service_rec_query() is negative, it is an
728 * error code. Otherwise it is a request ID that can be used to cancel
729 * the query.
730 */
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -0700731int ib_sa_service_rec_query(struct ib_sa_client *client,
732 struct ib_device *device, u8 port_num, u8 method,
Hal Rosenstockcbae32c2005-07-27 11:45:38 -0700733 struct ib_sa_service_rec *rec,
734 ib_sa_comp_mask comp_mask,
Al Virodd0fc662005-10-07 07:46:04 +0100735 int timeout_ms, gfp_t gfp_mask,
Hal Rosenstockcbae32c2005-07-27 11:45:38 -0700736 void (*callback)(int status,
737 struct ib_sa_service_rec *resp,
738 void *context),
739 void *context,
740 struct ib_sa_query **sa_query)
741{
742 struct ib_sa_service_query *query;
743 struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
Roland Dreier56c202d2005-10-13 10:45:02 -0700744 struct ib_sa_port *port;
745 struct ib_mad_agent *agent;
Sean Hefty34816ad2005-10-25 10:51:39 -0700746 struct ib_sa_mad *mad;
Hal Rosenstockcbae32c2005-07-27 11:45:38 -0700747 int ret;
748
Roland Dreier56c202d2005-10-13 10:45:02 -0700749 if (!sa_dev)
750 return -ENODEV;
751
752 port = &sa_dev->port[port_num - sa_dev->start_port];
753 agent = port->agent;
754
Hal Rosenstockcbae32c2005-07-27 11:45:38 -0700755 if (method != IB_MGMT_METHOD_GET &&
756 method != IB_MGMT_METHOD_SET &&
757 method != IB_SA_METHOD_DELETE)
758 return -EINVAL;
759
760 query = kmalloc(sizeof *query, gfp_mask);
761 if (!query)
762 return -ENOMEM;
Sean Hefty34816ad2005-10-25 10:51:39 -0700763
764 query->sa_query.mad_buf = ib_create_send_mad(agent, 1, 0,
765 0, IB_MGMT_SA_HDR,
766 IB_MGMT_SA_DATA, gfp_mask);
767 if (!query->sa_query.mad_buf) {
768 ret = -ENOMEM;
769 goto err1;
Hal Rosenstockcbae32c2005-07-27 11:45:38 -0700770 }
771
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -0700772 ib_sa_client_get(client);
773 query->sa_query.client = client;
774 query->callback = callback;
775 query->context = context;
Hal Rosenstockcbae32c2005-07-27 11:45:38 -0700776
Sean Hefty34816ad2005-10-25 10:51:39 -0700777 mad = query->sa_query.mad_buf->mad;
778 init_mad(mad, agent);
Hal Rosenstockcbae32c2005-07-27 11:45:38 -0700779
Sean Hefty34816ad2005-10-25 10:51:39 -0700780 query->sa_query.callback = callback ? ib_sa_service_rec_callback : NULL;
781 query->sa_query.release = ib_sa_service_rec_release;
782 query->sa_query.port = port;
783 mad->mad_hdr.method = method;
784 mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_SERVICE_REC);
785 mad->sa_hdr.comp_mask = comp_mask;
Hal Rosenstockcbae32c2005-07-27 11:45:38 -0700786
787 ib_pack(service_rec_table, ARRAY_SIZE(service_rec_table),
Sean Hefty34816ad2005-10-25 10:51:39 -0700788 rec, mad->data);
Hal Rosenstockcbae32c2005-07-27 11:45:38 -0700789
790 *sa_query = &query->sa_query;
791
Michael S. Tsirkine322fed2006-07-14 00:23:56 -0700792 ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
Sean Hefty34816ad2005-10-25 10:51:39 -0700793 if (ret < 0)
794 goto err2;
Hal Rosenstockcbae32c2005-07-27 11:45:38 -0700795
796 return ret;
Sean Hefty34816ad2005-10-25 10:51:39 -0700797
798err2:
799 *sa_query = NULL;
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -0700800 ib_sa_client_put(query->sa_query.client);
Sean Hefty34816ad2005-10-25 10:51:39 -0700801 ib_free_send_mad(query->sa_query.mad_buf);
802
803err1:
804 kfree(query);
805 return ret;
Hal Rosenstockcbae32c2005-07-27 11:45:38 -0700806}
807EXPORT_SYMBOL(ib_sa_service_rec_query);
808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809static void ib_sa_mcmember_rec_callback(struct ib_sa_query *sa_query,
810 int status,
811 struct ib_sa_mad *mad)
812{
813 struct ib_sa_mcmember_query *query =
814 container_of(sa_query, struct ib_sa_mcmember_query, sa_query);
815
816 if (mad) {
817 struct ib_sa_mcmember_rec rec;
818
819 ib_unpack(mcmember_rec_table, ARRAY_SIZE(mcmember_rec_table),
820 mad->data, &rec);
821 query->callback(status, &rec, query->context);
822 } else
823 query->callback(status, NULL, query->context);
824}
825
826static void ib_sa_mcmember_rec_release(struct ib_sa_query *sa_query)
827{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 kfree(container_of(sa_query, struct ib_sa_mcmember_query, sa_query));
829}
830
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -0700831int ib_sa_mcmember_rec_query(struct ib_sa_client *client,
832 struct ib_device *device, u8 port_num,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 u8 method,
834 struct ib_sa_mcmember_rec *rec,
835 ib_sa_comp_mask comp_mask,
Al Virodd0fc662005-10-07 07:46:04 +0100836 int timeout_ms, gfp_t gfp_mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 void (*callback)(int status,
838 struct ib_sa_mcmember_rec *resp,
839 void *context),
840 void *context,
841 struct ib_sa_query **sa_query)
842{
843 struct ib_sa_mcmember_query *query;
844 struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
Roland Dreier56c202d2005-10-13 10:45:02 -0700845 struct ib_sa_port *port;
846 struct ib_mad_agent *agent;
Sean Hefty34816ad2005-10-25 10:51:39 -0700847 struct ib_sa_mad *mad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 int ret;
849
Roland Dreier56c202d2005-10-13 10:45:02 -0700850 if (!sa_dev)
851 return -ENODEV;
852
853 port = &sa_dev->port[port_num - sa_dev->start_port];
854 agent = port->agent;
855
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 query = kmalloc(sizeof *query, gfp_mask);
857 if (!query)
858 return -ENOMEM;
Sean Hefty34816ad2005-10-25 10:51:39 -0700859
860 query->sa_query.mad_buf = ib_create_send_mad(agent, 1, 0,
861 0, IB_MGMT_SA_HDR,
862 IB_MGMT_SA_DATA, gfp_mask);
863 if (!query->sa_query.mad_buf) {
864 ret = -ENOMEM;
865 goto err1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 }
867
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -0700868 ib_sa_client_get(client);
869 query->sa_query.client = client;
870 query->callback = callback;
871 query->context = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Sean Hefty34816ad2005-10-25 10:51:39 -0700873 mad = query->sa_query.mad_buf->mad;
874 init_mad(mad, agent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Sean Hefty34816ad2005-10-25 10:51:39 -0700876 query->sa_query.callback = callback ? ib_sa_mcmember_rec_callback : NULL;
877 query->sa_query.release = ib_sa_mcmember_rec_release;
878 query->sa_query.port = port;
879 mad->mad_hdr.method = method;
880 mad->mad_hdr.attr_id = cpu_to_be16(IB_SA_ATTR_MC_MEMBER_REC);
881 mad->sa_hdr.comp_mask = comp_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
883 ib_pack(mcmember_rec_table, ARRAY_SIZE(mcmember_rec_table),
Sean Hefty34816ad2005-10-25 10:51:39 -0700884 rec, mad->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
886 *sa_query = &query->sa_query;
Roland Dreierdae4c1d2005-06-27 14:36:46 -0700887
Michael S. Tsirkine322fed2006-07-14 00:23:56 -0700888 ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
Sean Hefty34816ad2005-10-25 10:51:39 -0700889 if (ret < 0)
890 goto err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Roland Dreierdae4c1d2005-06-27 14:36:46 -0700892 return ret;
Sean Hefty34816ad2005-10-25 10:51:39 -0700893
894err2:
895 *sa_query = NULL;
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -0700896 ib_sa_client_put(query->sa_query.client);
Sean Hefty34816ad2005-10-25 10:51:39 -0700897 ib_free_send_mad(query->sa_query.mad_buf);
898
899err1:
900 kfree(query);
901 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902}
903EXPORT_SYMBOL(ib_sa_mcmember_rec_query);
904
905static void send_handler(struct ib_mad_agent *agent,
906 struct ib_mad_send_wc *mad_send_wc)
907{
Sean Hefty34816ad2005-10-25 10:51:39 -0700908 struct ib_sa_query *query = mad_send_wc->send_buf->context[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 unsigned long flags;
910
Roland Dreiere4f50f02005-05-25 12:31:29 -0700911 if (query->callback)
912 switch (mad_send_wc->status) {
913 case IB_WC_SUCCESS:
914 /* No callback -- already got recv */
915 break;
916 case IB_WC_RESP_TIMEOUT_ERR:
917 query->callback(query, -ETIMEDOUT, NULL);
918 break;
919 case IB_WC_WR_FLUSH_ERR:
920 query->callback(query, -EINTR, NULL);
921 break;
922 default:
923 query->callback(query, -EIO, NULL);
924 break;
925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 spin_lock_irqsave(&idr_lock, flags);
Sean Hefty34816ad2005-10-25 10:51:39 -0700928 idr_remove(&query_idr, query->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 spin_unlock_irqrestore(&idr_lock, flags);
Sean Hefty34816ad2005-10-25 10:51:39 -0700930
Roland Dreier3cd96562006-09-22 15:22:46 -0700931 ib_free_send_mad(mad_send_wc->send_buf);
Sean Hefty34816ad2005-10-25 10:51:39 -0700932 kref_put(&query->sm_ah->ref, free_sm_ah);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -0700933 ib_sa_client_put(query->client);
Sean Hefty34816ad2005-10-25 10:51:39 -0700934 query->release(query);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935}
936
937static void recv_handler(struct ib_mad_agent *mad_agent,
938 struct ib_mad_recv_wc *mad_recv_wc)
939{
940 struct ib_sa_query *query;
Sean Hefty34816ad2005-10-25 10:51:39 -0700941 struct ib_mad_send_buf *mad_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Sean Hefty34816ad2005-10-25 10:51:39 -0700943 mad_buf = (void *) (unsigned long) mad_recv_wc->wc->wr_id;
944 query = mad_buf->context[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Sean Hefty34816ad2005-10-25 10:51:39 -0700946 if (query->callback) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 if (mad_recv_wc->wc->status == IB_WC_SUCCESS)
948 query->callback(query,
949 mad_recv_wc->recv_buf.mad->mad_hdr.status ?
950 -EINVAL : 0,
951 (struct ib_sa_mad *) mad_recv_wc->recv_buf.mad);
952 else
953 query->callback(query, -EIO, NULL);
954 }
955
956 ib_free_recv_mad(mad_recv_wc);
957}
958
959static void ib_sa_add_one(struct ib_device *device)
960{
961 struct ib_sa_device *sa_dev;
962 int s, e, i;
963
Tom Tucker07ebafb2006-08-03 16:02:42 -0500964 if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
965 return;
966
967 if (device->node_type == RDMA_NODE_IB_SWITCH)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 s = e = 0;
969 else {
970 s = 1;
971 e = device->phys_port_cnt;
972 }
973
974 sa_dev = kmalloc(sizeof *sa_dev +
975 (e - s + 1) * sizeof (struct ib_sa_port),
976 GFP_KERNEL);
977 if (!sa_dev)
978 return;
979
980 sa_dev->start_port = s;
981 sa_dev->end_port = e;
982
983 for (i = 0; i <= e - s; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 sa_dev->port[i].sm_ah = NULL;
985 sa_dev->port[i].port_num = i + s;
986 spin_lock_init(&sa_dev->port[i].ah_lock);
987
988 sa_dev->port[i].agent =
989 ib_register_mad_agent(device, i + s, IB_QPT_GSI,
990 NULL, 0, send_handler,
991 recv_handler, sa_dev);
992 if (IS_ERR(sa_dev->port[i].agent))
993 goto err;
994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 INIT_WORK(&sa_dev->port[i].update_task,
996 update_sm_ah, &sa_dev->port[i]);
997 }
998
999 ib_set_client_data(device, &sa_client, sa_dev);
1000
1001 /*
1002 * We register our event handler after everything is set up,
1003 * and then update our cached info after the event handler is
1004 * registered to avoid any problems if a port changes state
1005 * during our initialization.
1006 */
1007
1008 INIT_IB_EVENT_HANDLER(&sa_dev->event_handler, device, ib_sa_event);
1009 if (ib_register_event_handler(&sa_dev->event_handler))
1010 goto err;
1011
1012 for (i = 0; i <= e - s; ++i)
1013 update_sm_ah(&sa_dev->port[i]);
1014
1015 return;
1016
1017err:
Hal Rosenstockb82cab62005-07-27 11:45:22 -07001018 while (--i >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 ib_unregister_mad_agent(sa_dev->port[i].agent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
1021 kfree(sa_dev);
1022
1023 return;
1024}
1025
1026static void ib_sa_remove_one(struct ib_device *device)
1027{
1028 struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
1029 int i;
1030
1031 if (!sa_dev)
1032 return;
1033
1034 ib_unregister_event_handler(&sa_dev->event_handler);
1035
Michael S. Tsirkin0f47ae02006-01-17 09:53:51 -08001036 flush_scheduled_work();
1037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 for (i = 0; i <= sa_dev->end_port - sa_dev->start_port; ++i) {
1039 ib_unregister_mad_agent(sa_dev->port[i].agent);
1040 kref_put(&sa_dev->port[i].sm_ah->ref, free_sm_ah);
1041 }
1042
1043 kfree(sa_dev);
1044}
1045
1046static int __init ib_sa_init(void)
1047{
1048 int ret;
1049
1050 spin_lock_init(&idr_lock);
1051 spin_lock_init(&tid_lock);
1052
1053 get_random_bytes(&tid, sizeof tid);
1054
1055 ret = ib_register_client(&sa_client);
1056 if (ret)
1057 printk(KERN_ERR "Couldn't register ib_sa client\n");
1058
1059 return ret;
1060}
1061
1062static void __exit ib_sa_cleanup(void)
1063{
1064 ib_unregister_client(&sa_client);
Roland Dreier5d7edb32005-10-24 10:53:25 -07001065 idr_destroy(&query_idr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066}
1067
1068module_init(ib_sa_init);
1069module_exit(ib_sa_cleanup);