xref: /NextBSD/sys/ofed/drivers/infiniband/core/sa_query.c (revision 287e3b14e9552995def1802ec9c5034f4adf28ec)
1 /*
2  * Copyright (c) 2004 Topspin Communications.  All rights reserved.
3  * Copyright (c) 2005 Voltaire, Inc.  All rights reserved.
4  * Copyright (c) 2006 Intel Corporation.  All rights reserved.
5  *
6  * This software is available to you under a choice of one of two
7  * licenses.  You may choose to be licensed under the terms of the GNU
8  * General Public License (GPL) Version 2, available from the file
9  * COPYING in the main directory of this source tree, or the
10  * OpenIB.org BSD license below:
11  *
12  *     Redistribution and use in source and binary forms, with or
13  *     without modification, are permitted provided that the following
14  *     conditions are met:
15  *
16  *      - Redistributions of source code must retain the above
17  *        copyright notice, this list of conditions and the following
18  *        disclaimer.
19  *
20  *      - Redistributions in binary form must reproduce the above
21  *        copyright notice, this list of conditions and the following
22  *        disclaimer in the documentation and/or other materials
23  *        provided with the distribution.
24  *
25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32  * SOFTWARE.
33  */
34 
35 #include <linux/module.h>
36 #include <linux/err.h>
37 #include <linux/random.h>
38 #include <linux/spinlock.h>
39 #include <linux/slab.h>
40 #include <linux/dma-mapping.h>
41 #include <linux/kref.h>
42 #include <linux/idr.h>
43 #include <linux/workqueue.h>
44 
45 #include <rdma/ib_pack.h>
46 #include <rdma/ib_cache.h>
47 #include "sa.h"
48 
49 MODULE_AUTHOR("Roland Dreier");
50 MODULE_DESCRIPTION("InfiniBand subnet administration query support");
51 MODULE_LICENSE("Dual BSD/GPL");
52 
53 struct ib_sa_sm_ah {
54 	struct ib_ah        *ah;
55 	struct kref          ref;
56 	u16		     pkey_index;
57 	u8		     src_path_mask;
58 };
59 
60 struct ib_sa_port {
61 	struct ib_mad_agent *agent;
62 	struct ib_sa_sm_ah  *sm_ah;
63 	struct work_struct   update_task;
64 	spinlock_t           ah_lock;
65 	u8                   port_num;
66 };
67 
68 struct ib_sa_device {
69 	int                     start_port, end_port;
70 	struct ib_event_handler event_handler;
71 	struct ib_sa_port port[0];
72 };
73 
74 struct ib_sa_query {
75 	void (*callback)(struct ib_sa_query *, int, struct ib_sa_mad *);
76 	void (*release)(struct ib_sa_query *);
77 	struct ib_sa_client    *client;
78 	struct ib_sa_port      *port;
79 	struct ib_mad_send_buf *mad_buf;
80 	struct ib_sa_sm_ah     *sm_ah;
81 	int			id;
82 };
83 
84 struct ib_sa_service_query {
85 	void (*callback)(int, struct ib_sa_service_rec *, void *);
86 	void *context;
87 	struct ib_sa_query sa_query;
88 };
89 
90 struct ib_sa_path_query {
91 	void (*callback)(int, struct ib_sa_path_rec *, void *);
92 	void *context;
93 	struct ib_sa_query sa_query;
94 };
95 
96 struct ib_sa_guidinfo_query {
97 	void (*callback)(int, struct ib_sa_guidinfo_rec *, void *);
98 	void *context;
99 	struct ib_sa_query sa_query;
100 };
101 
102 struct ib_sa_mcmember_query {
103 	void (*callback)(int, struct ib_sa_mcmember_rec *, void *);
104 	void *context;
105 	struct ib_sa_query sa_query;
106 };
107 
108 static void ib_sa_add_one(struct ib_device *device);
109 static void ib_sa_remove_one(struct ib_device *device);
110 
111 static struct ib_client sa_client = {
112 	.name   = "sa",
113 	.add    = ib_sa_add_one,
114 	.remove = ib_sa_remove_one
115 };
116 
117 static DEFINE_SPINLOCK(idr_lock);
118 static DEFINE_IDR(query_idr);
119 
120 static DEFINE_SPINLOCK(tid_lock);
121 static u32 tid;
122 
123 #define PATH_REC_FIELD(field) \
124 	.struct_offset_bytes = offsetof(struct ib_sa_path_rec, field),		\
125 	.struct_size_bytes   = sizeof ((struct ib_sa_path_rec *) 0)->field,	\
126 	.field_name          = "sa_path_rec:" #field
127 
128 static const struct ib_field path_rec_table[] = {
129 	{ PATH_REC_FIELD(service_id),
130 	  .offset_words = 0,
131 	  .offset_bits  = 0,
132 	  .size_bits    = 64 },
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 	{ PATH_REC_FIELD(qos_class),
182 	  .offset_words = 13,
183 	  .offset_bits  = 0,
184 	  .size_bits    = 12 },
185 	{ PATH_REC_FIELD(sl),
186 	  .offset_words = 13,
187 	  .offset_bits  = 12,
188 	  .size_bits    = 4 },
189 	{ PATH_REC_FIELD(mtu_selector),
190 	  .offset_words = 13,
191 	  .offset_bits  = 16,
192 	  .size_bits    = 2 },
193 	{ PATH_REC_FIELD(mtu),
194 	  .offset_words = 13,
195 	  .offset_bits  = 18,
196 	  .size_bits    = 6 },
197 	{ PATH_REC_FIELD(rate_selector),
198 	  .offset_words = 13,
199 	  .offset_bits  = 24,
200 	  .size_bits    = 2 },
201 	{ PATH_REC_FIELD(rate),
202 	  .offset_words = 13,
203 	  .offset_bits  = 26,
204 	  .size_bits    = 6 },
205 	{ PATH_REC_FIELD(packet_life_time_selector),
206 	  .offset_words = 14,
207 	  .offset_bits  = 0,
208 	  .size_bits    = 2 },
209 	{ PATH_REC_FIELD(packet_life_time),
210 	  .offset_words = 14,
211 	  .offset_bits  = 2,
212 	  .size_bits    = 6 },
213 	{ PATH_REC_FIELD(preference),
214 	  .offset_words = 14,
215 	  .offset_bits  = 8,
216 	  .size_bits    = 8 },
217 	{ RESERVED,
218 	  .offset_words = 14,
219 	  .offset_bits  = 16,
220 	  .size_bits    = 48 },
221 };
222 
223 #define MCMEMBER_REC_FIELD(field) \
224 	.struct_offset_bytes = offsetof(struct ib_sa_mcmember_rec, field),	\
225 	.struct_size_bytes   = sizeof ((struct ib_sa_mcmember_rec *) 0)->field,	\
226 	.field_name          = "sa_mcmember_rec:" #field
227 
228 static const struct ib_field mcmember_rec_table[] = {
229 	{ MCMEMBER_REC_FIELD(mgid),
230 	  .offset_words = 0,
231 	  .offset_bits  = 0,
232 	  .size_bits    = 128 },
233 	{ MCMEMBER_REC_FIELD(port_gid),
234 	  .offset_words = 4,
235 	  .offset_bits  = 0,
236 	  .size_bits    = 128 },
237 	{ MCMEMBER_REC_FIELD(qkey),
238 	  .offset_words = 8,
239 	  .offset_bits  = 0,
240 	  .size_bits    = 32 },
241 	{ MCMEMBER_REC_FIELD(mlid),
242 	  .offset_words = 9,
243 	  .offset_bits  = 0,
244 	  .size_bits    = 16 },
245 	{ MCMEMBER_REC_FIELD(mtu_selector),
246 	  .offset_words = 9,
247 	  .offset_bits  = 16,
248 	  .size_bits    = 2 },
249 	{ MCMEMBER_REC_FIELD(mtu),
250 	  .offset_words = 9,
251 	  .offset_bits  = 18,
252 	  .size_bits    = 6 },
253 	{ MCMEMBER_REC_FIELD(traffic_class),
254 	  .offset_words = 9,
255 	  .offset_bits  = 24,
256 	  .size_bits    = 8 },
257 	{ MCMEMBER_REC_FIELD(pkey),
258 	  .offset_words = 10,
259 	  .offset_bits  = 0,
260 	  .size_bits    = 16 },
261 	{ MCMEMBER_REC_FIELD(rate_selector),
262 	  .offset_words = 10,
263 	  .offset_bits  = 16,
264 	  .size_bits    = 2 },
265 	{ MCMEMBER_REC_FIELD(rate),
266 	  .offset_words = 10,
267 	  .offset_bits  = 18,
268 	  .size_bits    = 6 },
269 	{ MCMEMBER_REC_FIELD(packet_life_time_selector),
270 	  .offset_words = 10,
271 	  .offset_bits  = 24,
272 	  .size_bits    = 2 },
273 	{ MCMEMBER_REC_FIELD(packet_life_time),
274 	  .offset_words = 10,
275 	  .offset_bits  = 26,
276 	  .size_bits    = 6 },
277 	{ MCMEMBER_REC_FIELD(sl),
278 	  .offset_words = 11,
279 	  .offset_bits  = 0,
280 	  .size_bits    = 4 },
281 	{ MCMEMBER_REC_FIELD(flow_label),
282 	  .offset_words = 11,
283 	  .offset_bits  = 4,
284 	  .size_bits    = 20 },
285 	{ MCMEMBER_REC_FIELD(hop_limit),
286 	  .offset_words = 11,
287 	  .offset_bits  = 24,
288 	  .size_bits    = 8 },
289 	{ MCMEMBER_REC_FIELD(scope),
290 	  .offset_words = 12,
291 	  .offset_bits  = 0,
292 	  .size_bits    = 4 },
293 	{ MCMEMBER_REC_FIELD(join_state),
294 	  .offset_words = 12,
295 	  .offset_bits  = 4,
296 	  .size_bits    = 4 },
297 	{ MCMEMBER_REC_FIELD(proxy_join),
298 	  .offset_words = 12,
299 	  .offset_bits  = 8,
300 	  .size_bits    = 1 },
301 	{ RESERVED,
302 	  .offset_words = 12,
303 	  .offset_bits  = 9,
304 	  .size_bits    = 23 },
305 };
306 
307 #define SERVICE_REC_FIELD(field) \
308 	.struct_offset_bytes = offsetof(struct ib_sa_service_rec, field),	\
309 	.struct_size_bytes   = sizeof ((struct ib_sa_service_rec *) 0)->field,	\
310 	.field_name          = "sa_service_rec:" #field
311 
312 static const struct ib_field service_rec_table[] = {
313 	{ SERVICE_REC_FIELD(id),
314 	  .offset_words = 0,
315 	  .offset_bits  = 0,
316 	  .size_bits    = 64 },
317 	{ SERVICE_REC_FIELD(gid),
318 	  .offset_words = 2,
319 	  .offset_bits  = 0,
320 	  .size_bits    = 128 },
321 	{ SERVICE_REC_FIELD(pkey),
322 	  .offset_words = 6,
323 	  .offset_bits  = 0,
324 	  .size_bits    = 16 },
325 	{ SERVICE_REC_FIELD(lease),
326 	  .offset_words = 7,
327 	  .offset_bits  = 0,
328 	  .size_bits    = 32 },
329 	{ SERVICE_REC_FIELD(key),
330 	  .offset_words = 8,
331 	  .offset_bits  = 0,
332 	  .size_bits    = 128 },
333 	{ SERVICE_REC_FIELD(name),
334 	  .offset_words = 12,
335 	  .offset_bits  = 0,
336 	  .size_bits    = 64*8 },
337 	{ SERVICE_REC_FIELD(data8),
338 	  .offset_words = 28,
339 	  .offset_bits  = 0,
340 	  .size_bits    = 16*8 },
341 	{ SERVICE_REC_FIELD(data16),
342 	  .offset_words = 32,
343 	  .offset_bits  = 0,
344 	  .size_bits    = 8*16 },
345 	{ SERVICE_REC_FIELD(data32),
346 	  .offset_words = 36,
347 	  .offset_bits  = 0,
348 	  .size_bits    = 4*32 },
349 	{ SERVICE_REC_FIELD(data64),
350 	  .offset_words = 40,
351 	  .offset_bits  = 0,
352 	  .size_bits    = 2*64 },
353 };
354 
355 #define GUIDINFO_REC_FIELD(field) \
356 	.struct_offset_bytes = offsetof(struct ib_sa_guidinfo_rec, field),	\
357 	.struct_size_bytes   = sizeof((struct ib_sa_guidinfo_rec *) 0)->field,	\
358 	.field_name          = "sa_guidinfo_rec:" #field
359 
360 static const struct ib_field guidinfo_rec_table[] = {
361 	{ GUIDINFO_REC_FIELD(lid),
362 	  .offset_words = 0,
363 	  .offset_bits  = 0,
364 	  .size_bits    = 16 },
365 	{ GUIDINFO_REC_FIELD(block_num),
366 	  .offset_words = 0,
367 	  .offset_bits  = 16,
368 	  .size_bits    = 8 },
369 	{ GUIDINFO_REC_FIELD(res1),
370 	  .offset_words = 0,
371 	  .offset_bits  = 24,
372 	  .size_bits    = 8 },
373 	{ GUIDINFO_REC_FIELD(res2),
374 	  .offset_words = 1,
375 	  .offset_bits  = 0,
376 	  .size_bits    = 32 },
377 	{ GUIDINFO_REC_FIELD(guid_info_list),
378 	  .offset_words = 2,
379 	  .offset_bits  = 0,
380 	  .size_bits    = 512 },
381 };
382 
free_sm_ah(struct kref * kref)383 static void free_sm_ah(struct kref *kref)
384 {
385 	struct ib_sa_sm_ah *sm_ah = container_of(kref, struct ib_sa_sm_ah, ref);
386 
387 	ib_destroy_ah(sm_ah->ah);
388 	kfree(sm_ah);
389 }
390 
update_sm_ah(struct work_struct * work)391 static void update_sm_ah(struct work_struct *work)
392 {
393 	struct ib_sa_port *port =
394 		container_of(work, struct ib_sa_port, update_task);
395 	struct ib_sa_sm_ah *new_ah;
396 	struct ib_port_attr port_attr;
397 	struct ib_ah_attr   ah_attr;
398 
399 	if (ib_query_port(port->agent->device, port->port_num, &port_attr)) {
400 		printk(KERN_WARNING "Couldn't query port\n");
401 		return;
402 	}
403 
404 	new_ah = kmalloc(sizeof *new_ah, GFP_KERNEL);
405 	if (!new_ah) {
406 		printk(KERN_WARNING "Couldn't allocate new SM AH\n");
407 		return;
408 	}
409 
410 	kref_init(&new_ah->ref);
411 	new_ah->src_path_mask = (1 << port_attr.lmc) - 1;
412 
413 	new_ah->pkey_index = 0;
414 	if (ib_find_pkey(port->agent->device, port->port_num,
415 			 IB_DEFAULT_PKEY_FULL, &new_ah->pkey_index))
416 		printk(KERN_ERR "Couldn't find index for default PKey\n");
417 
418 	memset(&ah_attr, 0, sizeof ah_attr);
419 	ah_attr.dlid     = port_attr.sm_lid;
420 	ah_attr.sl       = port_attr.sm_sl;
421 	ah_attr.port_num = port->port_num;
422 
423 	new_ah->ah = ib_create_ah(port->agent->qp->pd, &ah_attr);
424 	if (IS_ERR(new_ah->ah)) {
425 		printk(KERN_WARNING "Couldn't create new SM AH\n");
426 		kfree(new_ah);
427 		return;
428 	}
429 
430 	spin_lock_irq(&port->ah_lock);
431 	if (port->sm_ah)
432 		kref_put(&port->sm_ah->ref, free_sm_ah);
433 	port->sm_ah = new_ah;
434 	spin_unlock_irq(&port->ah_lock);
435 
436 }
437 
ib_sa_event(struct ib_event_handler * handler,struct ib_event * event)438 static void ib_sa_event(struct ib_event_handler *handler, struct ib_event *event)
439 {
440 	if (event->event == IB_EVENT_PORT_ERR    ||
441 	    event->event == IB_EVENT_PORT_ACTIVE ||
442 	    event->event == IB_EVENT_LID_CHANGE  ||
443 	    event->event == IB_EVENT_PKEY_CHANGE ||
444 	    event->event == IB_EVENT_SM_CHANGE   ||
445 	    event->event == IB_EVENT_CLIENT_REREGISTER) {
446 		unsigned long flags;
447 		struct ib_sa_device *sa_dev =
448 			container_of(handler, typeof(*sa_dev), event_handler);
449 		struct ib_sa_port *port =
450 			&sa_dev->port[event->element.port_num - sa_dev->start_port];
451 
452 		if (rdma_port_get_link_layer(handler->device, port->port_num) != IB_LINK_LAYER_INFINIBAND)
453 			return;
454 
455 		spin_lock_irqsave(&port->ah_lock, flags);
456 		if (port->sm_ah)
457 			kref_put(&port->sm_ah->ref, free_sm_ah);
458 		port->sm_ah = NULL;
459 		spin_unlock_irqrestore(&port->ah_lock, flags);
460 
461 		queue_work(ib_wq, &sa_dev->port[event->element.port_num -
462 					    sa_dev->start_port].update_task);
463 	}
464 }
465 
ib_sa_register_client(struct ib_sa_client * client)466 void ib_sa_register_client(struct ib_sa_client *client)
467 {
468 	atomic_set(&client->users, 1);
469 	init_completion(&client->comp);
470 }
471 EXPORT_SYMBOL(ib_sa_register_client);
472 
ib_sa_unregister_client(struct ib_sa_client * client)473 void ib_sa_unregister_client(struct ib_sa_client *client)
474 {
475 	ib_sa_client_put(client);
476 	wait_for_completion(&client->comp);
477 }
478 EXPORT_SYMBOL(ib_sa_unregister_client);
479 
480 /**
481  * ib_sa_cancel_query - try to cancel an SA query
482  * @id:ID of query to cancel
483  * @query:query pointer to cancel
484  *
485  * Try to cancel an SA query.  If the id and query don't match up or
486  * the query has already completed, nothing is done.  Otherwise the
487  * query is canceled and will complete with a status of -EINTR.
488  */
ib_sa_cancel_query(int id,struct ib_sa_query * query)489 void ib_sa_cancel_query(int id, struct ib_sa_query *query)
490 {
491 	unsigned long flags;
492 	struct ib_mad_agent *agent;
493 	struct ib_mad_send_buf *mad_buf;
494 
495 	spin_lock_irqsave(&idr_lock, flags);
496 	if (idr_find(&query_idr, id) != query) {
497 		spin_unlock_irqrestore(&idr_lock, flags);
498 		return;
499 	}
500 	agent = query->port->agent;
501 	mad_buf = query->mad_buf;
502 	spin_unlock_irqrestore(&idr_lock, flags);
503 
504 	ib_cancel_mad(agent, mad_buf);
505 }
506 EXPORT_SYMBOL(ib_sa_cancel_query);
507 
get_src_path_mask(struct ib_device * device,u8 port_num)508 static u8 get_src_path_mask(struct ib_device *device, u8 port_num)
509 {
510 	struct ib_sa_device *sa_dev;
511 	struct ib_sa_port   *port;
512 	unsigned long flags;
513 	u8 src_path_mask;
514 
515 	sa_dev = ib_get_client_data(device, &sa_client);
516 	if (!sa_dev)
517 		return 0x7f;
518 
519 	port  = &sa_dev->port[port_num - sa_dev->start_port];
520 	spin_lock_irqsave(&port->ah_lock, flags);
521 	src_path_mask = port->sm_ah ? port->sm_ah->src_path_mask : 0x7f;
522 	spin_unlock_irqrestore(&port->ah_lock, flags);
523 
524 	return src_path_mask;
525 }
526 
ib_init_ah_from_path(struct ib_device * device,u8 port_num,struct ib_sa_path_rec * rec,struct ib_ah_attr * ah_attr)527 int ib_init_ah_from_path(struct ib_device *device, u8 port_num,
528 			 struct ib_sa_path_rec *rec, struct ib_ah_attr *ah_attr)
529 {
530 	int ret;
531 	u16 gid_index;
532 	int force_grh;
533 
534 	memset(ah_attr, 0, sizeof *ah_attr);
535 	ah_attr->dlid = be16_to_cpu(rec->dlid);
536 	ah_attr->sl = rec->sl;
537 	ah_attr->src_path_bits = be16_to_cpu(rec->slid) &
538 				 get_src_path_mask(device, port_num);
539 	ah_attr->port_num = port_num;
540 	ah_attr->static_rate = rec->rate;
541 
542 	force_grh = rdma_port_get_link_layer(device, port_num) == IB_LINK_LAYER_ETHERNET;
543 
544 	if (rec->hop_limit > 1 || force_grh) {
545 		ah_attr->ah_flags = IB_AH_GRH;
546 		ah_attr->grh.dgid = rec->dgid;
547 
548 		ret = ib_find_cached_gid(device, &rec->sgid, &port_num,
549 					 &gid_index);
550 		if (ret)
551 			return ret;
552 
553 		ah_attr->grh.sgid_index    = gid_index;
554 		ah_attr->grh.flow_label    = be32_to_cpu(rec->flow_label);
555 		ah_attr->grh.hop_limit     = rec->hop_limit;
556 		ah_attr->grh.traffic_class = rec->traffic_class;
557 	}
558 	if (force_grh) {
559 		memcpy(ah_attr->dmac, rec->dmac, 6);
560 		ah_attr->vlan_id = rec->vlan_id;
561 	} else {
562 		memset(ah_attr->dmac, 0, 6);
563 		ah_attr->vlan_id = 0xffff;
564 	}
565 
566 	return 0;
567 }
568 EXPORT_SYMBOL(ib_init_ah_from_path);
569 
alloc_mad(struct ib_sa_query * query,gfp_t gfp_mask)570 static int alloc_mad(struct ib_sa_query *query, gfp_t gfp_mask)
571 {
572 	unsigned long flags;
573 
574 	spin_lock_irqsave(&query->port->ah_lock, flags);
575 	if (!query->port->sm_ah) {
576 		spin_unlock_irqrestore(&query->port->ah_lock, flags);
577 		return -EAGAIN;
578 	}
579 	kref_get(&query->port->sm_ah->ref);
580 	query->sm_ah = query->port->sm_ah;
581 	spin_unlock_irqrestore(&query->port->ah_lock, flags);
582 
583 	query->mad_buf = ib_create_send_mad(query->port->agent, 1,
584 					    query->sm_ah->pkey_index,
585 					    0, IB_MGMT_SA_HDR, IB_MGMT_SA_DATA,
586 					    gfp_mask);
587 	if (IS_ERR(query->mad_buf)) {
588 		kref_put(&query->sm_ah->ref, free_sm_ah);
589 		return -ENOMEM;
590 	}
591 
592 	query->mad_buf->ah = query->sm_ah->ah;
593 
594 	return 0;
595 }
596 
free_mad(struct ib_sa_query * query)597 static void free_mad(struct ib_sa_query *query)
598 {
599 	ib_free_send_mad(query->mad_buf);
600 	kref_put(&query->sm_ah->ref, free_sm_ah);
601 }
602 
init_mad(struct ib_sa_mad * mad,struct ib_mad_agent * agent)603 static void init_mad(struct ib_sa_mad *mad, struct ib_mad_agent *agent)
604 {
605 	unsigned long flags;
606 
607 	memset(mad, 0, sizeof *mad);
608 
609 	mad->mad_hdr.base_version  = IB_MGMT_BASE_VERSION;
610 	mad->mad_hdr.mgmt_class    = IB_MGMT_CLASS_SUBN_ADM;
611 	mad->mad_hdr.class_version = IB_SA_CLASS_VERSION;
612 
613 	spin_lock_irqsave(&tid_lock, flags);
614 	mad->mad_hdr.tid           =
615 		cpu_to_be64(((u64) agent->hi_tid) << 32 | tid++);
616 	spin_unlock_irqrestore(&tid_lock, flags);
617 }
618 
send_mad(struct ib_sa_query * query,int timeout_ms,gfp_t gfp_mask)619 static int send_mad(struct ib_sa_query *query, int timeout_ms, gfp_t gfp_mask)
620 {
621 	unsigned long flags;
622 	int ret, id;
623 
624 retry:
625 	if (!idr_pre_get(&query_idr, gfp_mask))
626 		return -ENOMEM;
627 	spin_lock_irqsave(&idr_lock, flags);
628 	ret = idr_get_new(&query_idr, query, &id);
629 	spin_unlock_irqrestore(&idr_lock, flags);
630 	if (ret == -EAGAIN)
631 		goto retry;
632 	if (ret)
633 		return ret;
634 
635 	query->mad_buf->timeout_ms  = timeout_ms;
636 	query->mad_buf->context[0] = query;
637 	query->id = id;
638 
639 	ret = ib_post_send_mad(query->mad_buf, NULL);
640 	if (ret) {
641 		spin_lock_irqsave(&idr_lock, flags);
642 		idr_remove(&query_idr, id);
643 		spin_unlock_irqrestore(&idr_lock, flags);
644 	}
645 
646 	/*
647 	 * It's not safe to dereference query any more, because the
648 	 * send may already have completed and freed the query in
649 	 * another context.
650 	 */
651 	return ret ? ret : id;
652 }
653 
ib_sa_unpack_path(void * attribute,struct ib_sa_path_rec * rec)654 void ib_sa_unpack_path(void *attribute, struct ib_sa_path_rec *rec)
655 {
656 	ib_unpack(path_rec_table, ARRAY_SIZE(path_rec_table), attribute, rec);
657 }
658 EXPORT_SYMBOL(ib_sa_unpack_path);
659 
ib_sa_path_rec_callback(struct ib_sa_query * sa_query,int status,struct ib_sa_mad * mad)660 static void ib_sa_path_rec_callback(struct ib_sa_query *sa_query,
661 				    int status,
662 				    struct ib_sa_mad *mad)
663 {
664 	struct ib_sa_path_query *query =
665 		container_of(sa_query, struct ib_sa_path_query, sa_query);
666 
667 	if (mad) {
668 		struct ib_sa_path_rec rec;
669 
670 		ib_unpack(path_rec_table, ARRAY_SIZE(path_rec_table),
671 			  mad->data, &rec);
672 		rec.vlan_id = 0xffff;
673 		memset(rec.dmac, 0, ETH_ALEN);
674 		memset(rec.smac, 0, ETH_ALEN);
675 
676 		query->callback(status, &rec, query->context);
677 	} else
678 		query->callback(status, NULL, query->context);
679 }
680 
ib_sa_path_rec_release(struct ib_sa_query * sa_query)681 static void ib_sa_path_rec_release(struct ib_sa_query *sa_query)
682 {
683 	kfree(container_of(sa_query, struct ib_sa_path_query, sa_query));
684 }
685 
686 
687 /**
688  * ib_sa_path_rec_get - Start a Path get query
689  * @client:SA client
690  * @device:device to send query on
691  * @port_num: port number to send query on
692  * @rec:Path Record to send in query
693  * @comp_mask:component mask to send in query
694  * @timeout_ms:time to wait for response
695  * @gfp_mask:GFP mask to use for internal allocations
696  * @callback:function called when query completes, times out or is
697  * canceled
698  * @context:opaque user context passed to callback
699  * @sa_query:query context, used to cancel query
700  *
701  * Send a Path Record Get query to the SA to look up a path.  The
702  * callback function will be called when the query completes (or
703  * fails); status is 0 for a successful response, -EINTR if the query
704  * is canceled, -ETIMEDOUT is the query timed out, or -EIO if an error
705  * occurred sending the query.  The resp parameter of the callback is
706  * only valid if status is 0.
707  *
708  * If the return value of ib_sa_path_rec_get() is negative, it is an
709  * error code.  Otherwise it is a query ID that can be used to cancel
710  * the query.
711  */
ib_sa_path_rec_get(struct ib_sa_client * client,struct ib_device * device,u8 port_num,struct ib_sa_path_rec * rec,ib_sa_comp_mask comp_mask,int timeout_ms,gfp_t gfp_mask,void (* callback)(int status,struct ib_sa_path_rec * resp,void * context),void * context,struct ib_sa_query ** sa_query)712 int ib_sa_path_rec_get(struct ib_sa_client *client,
713 			 struct ib_device *device, u8 port_num,
714 			 struct ib_sa_path_rec *rec,
715 			 ib_sa_comp_mask comp_mask,
716 			 int timeout_ms, gfp_t gfp_mask,
717 			 void (*callback)(int status,
718 					  struct ib_sa_path_rec *resp,
719 					  void *context),
720 			 void *context,
721 			 struct ib_sa_query **sa_query)
722 {
723 	struct ib_sa_path_query *query;
724 	struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
725 	struct ib_sa_port   *port;
726 	struct ib_mad_agent *agent;
727 	struct ib_sa_mad *mad;
728 	int ret;
729 
730 	if (!sa_dev)
731 		return -ENODEV;
732 
733 	port  = &sa_dev->port[port_num - sa_dev->start_port];
734 	agent = port->agent;
735 
736 	query = kmalloc(sizeof *query, gfp_mask);
737 	if (!query)
738 		return -ENOMEM;
739 
740 	query->sa_query.port     = port;
741 	ret = alloc_mad(&query->sa_query, gfp_mask);
742 	if (ret)
743 		goto err1;
744 
745 	ib_sa_client_get(client);
746 	query->sa_query.client = client;
747 	query->callback        = callback;
748 	query->context         = context;
749 
750 	mad = query->sa_query.mad_buf->mad;
751 	init_mad(mad, agent);
752 
753 	query->sa_query.callback = callback ? ib_sa_path_rec_callback : NULL;
754 	query->sa_query.release  = ib_sa_path_rec_release;
755 	mad->mad_hdr.method	 = IB_MGMT_METHOD_GET;
756 	mad->mad_hdr.attr_id	 = cpu_to_be16(IB_SA_ATTR_PATH_REC);
757 	mad->sa_hdr.comp_mask	 = comp_mask;
758 
759 	ib_pack(path_rec_table, ARRAY_SIZE(path_rec_table), rec, mad->data);
760 
761 	*sa_query = &query->sa_query;
762 
763 	ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
764 	if (ret < 0)
765 		goto err2;
766 
767 	return ret;
768 
769 err2:
770 	*sa_query = NULL;
771 	ib_sa_client_put(query->sa_query.client);
772 	free_mad(&query->sa_query);
773 
774 err1:
775 	kfree(query);
776 	return ret;
777 }
778 EXPORT_SYMBOL(ib_sa_path_rec_get);
779 
ib_sa_service_rec_callback(struct ib_sa_query * sa_query,int status,struct ib_sa_mad * mad)780 static void ib_sa_service_rec_callback(struct ib_sa_query *sa_query,
781 				    int status,
782 				    struct ib_sa_mad *mad)
783 {
784 	struct ib_sa_service_query *query =
785 		container_of(sa_query, struct ib_sa_service_query, sa_query);
786 
787 	if (mad) {
788 		struct ib_sa_service_rec rec;
789 
790 		ib_unpack(service_rec_table, ARRAY_SIZE(service_rec_table),
791 			  mad->data, &rec);
792 		query->callback(status, &rec, query->context);
793 	} else
794 		query->callback(status, NULL, query->context);
795 }
796 
ib_sa_service_rec_release(struct ib_sa_query * sa_query)797 static void ib_sa_service_rec_release(struct ib_sa_query *sa_query)
798 {
799 	kfree(container_of(sa_query, struct ib_sa_service_query, sa_query));
800 }
801 
802 /**
803  * ib_sa_service_rec_query - Start Service Record operation
804  * @client:SA client
805  * @device:device to send request on
806  * @port_num: port number to send request on
807  * @method:SA method - should be get, set, or delete
808  * @rec:Service Record to send in request
809  * @comp_mask:component mask to send in request
810  * @timeout_ms:time to wait for response
811  * @gfp_mask:GFP mask to use for internal allocations
812  * @callback:function called when request completes, times out or is
813  * canceled
814  * @context:opaque user context passed to callback
815  * @sa_query:request context, used to cancel request
816  *
817  * Send a Service Record set/get/delete to the SA to register,
818  * unregister or query a service record.
819  * The callback function will be called when the request completes (or
820  * fails); status is 0 for a successful response, -EINTR if the query
821  * is canceled, -ETIMEDOUT is the query timed out, or -EIO if an error
822  * occurred sending the query.  The resp parameter of the callback is
823  * only valid if status is 0.
824  *
825  * If the return value of ib_sa_service_rec_query() is negative, it is an
826  * error code.  Otherwise it is a request ID that can be used to cancel
827  * the query.
828  */
ib_sa_service_rec_query(struct ib_sa_client * client,struct ib_device * device,u8 port_num,u8 method,struct ib_sa_service_rec * rec,ib_sa_comp_mask comp_mask,int timeout_ms,gfp_t gfp_mask,void (* callback)(int status,struct ib_sa_service_rec * resp,void * context),void * context,struct ib_sa_query ** sa_query)829 int ib_sa_service_rec_query(struct ib_sa_client *client,
830 			    struct ib_device *device, u8 port_num, u8 method,
831 			    struct ib_sa_service_rec *rec,
832 			    ib_sa_comp_mask comp_mask,
833 			    int timeout_ms, gfp_t gfp_mask,
834 			    void (*callback)(int status,
835 					     struct ib_sa_service_rec *resp,
836 					     void *context),
837 			    void *context,
838 			    struct ib_sa_query **sa_query)
839 {
840 	struct ib_sa_service_query *query;
841 	struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
842 	struct ib_sa_port   *port;
843 	struct ib_mad_agent *agent;
844 	struct ib_sa_mad *mad;
845 	int ret;
846 
847 	if (!sa_dev)
848 		return -ENODEV;
849 
850 	port  = &sa_dev->port[port_num - sa_dev->start_port];
851 	agent = port->agent;
852 
853 	if (method != IB_MGMT_METHOD_GET &&
854 	    method != IB_MGMT_METHOD_SET &&
855 	    method != IB_SA_METHOD_DELETE)
856 		return -EINVAL;
857 
858 	query = kmalloc(sizeof *query, gfp_mask);
859 	if (!query)
860 		return -ENOMEM;
861 
862 	query->sa_query.port     = port;
863 	ret = alloc_mad(&query->sa_query, gfp_mask);
864 	if (ret)
865 		goto err1;
866 
867 	ib_sa_client_get(client);
868 	query->sa_query.client = client;
869 	query->callback        = callback;
870 	query->context         = context;
871 
872 	mad = query->sa_query.mad_buf->mad;
873 	init_mad(mad, agent);
874 
875 	query->sa_query.callback = callback ? ib_sa_service_rec_callback : NULL;
876 	query->sa_query.release  = ib_sa_service_rec_release;
877 	mad->mad_hdr.method	 = method;
878 	mad->mad_hdr.attr_id	 = cpu_to_be16(IB_SA_ATTR_SERVICE_REC);
879 	mad->sa_hdr.comp_mask	 = comp_mask;
880 
881 	ib_pack(service_rec_table, ARRAY_SIZE(service_rec_table),
882 		rec, mad->data);
883 
884 	*sa_query = &query->sa_query;
885 
886 	ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
887 	if (ret < 0)
888 		goto err2;
889 
890 	return ret;
891 
892 err2:
893 	*sa_query = NULL;
894 	ib_sa_client_put(query->sa_query.client);
895 	free_mad(&query->sa_query);
896 
897 err1:
898 	kfree(query);
899 	return ret;
900 }
901 EXPORT_SYMBOL(ib_sa_service_rec_query);
902 
ib_sa_mcmember_rec_callback(struct ib_sa_query * sa_query,int status,struct ib_sa_mad * mad)903 static void ib_sa_mcmember_rec_callback(struct ib_sa_query *sa_query,
904 					int status,
905 					struct ib_sa_mad *mad)
906 {
907 	struct ib_sa_mcmember_query *query =
908 		container_of(sa_query, struct ib_sa_mcmember_query, sa_query);
909 
910 	if (mad) {
911 		struct ib_sa_mcmember_rec rec;
912 
913 		ib_unpack(mcmember_rec_table, ARRAY_SIZE(mcmember_rec_table),
914 			  mad->data, &rec);
915 		query->callback(status, &rec, query->context);
916 	} else
917 		query->callback(status, NULL, query->context);
918 }
919 
ib_sa_mcmember_rec_release(struct ib_sa_query * sa_query)920 static void ib_sa_mcmember_rec_release(struct ib_sa_query *sa_query)
921 {
922 	kfree(container_of(sa_query, struct ib_sa_mcmember_query, sa_query));
923 }
924 
ib_sa_mcmember_rec_query(struct ib_sa_client * client,struct ib_device * device,u8 port_num,u8 method,struct ib_sa_mcmember_rec * rec,ib_sa_comp_mask comp_mask,int timeout_ms,gfp_t gfp_mask,void (* callback)(int status,struct ib_sa_mcmember_rec * resp,void * context),void * context,struct ib_sa_query ** sa_query)925 int ib_sa_mcmember_rec_query(struct ib_sa_client *client,
926 			     struct ib_device *device, u8 port_num,
927 			     u8 method,
928 			     struct ib_sa_mcmember_rec *rec,
929 			     ib_sa_comp_mask comp_mask,
930 			     int timeout_ms, gfp_t gfp_mask,
931 			     void (*callback)(int status,
932 					      struct ib_sa_mcmember_rec *resp,
933 					      void *context),
934 			     void *context,
935 			     struct ib_sa_query **sa_query)
936 {
937 	struct ib_sa_mcmember_query *query;
938 	struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
939 	struct ib_sa_port   *port;
940 	struct ib_mad_agent *agent;
941 	struct ib_sa_mad *mad;
942 	int ret;
943 
944 	if (!sa_dev)
945 		return -ENODEV;
946 
947 	port  = &sa_dev->port[port_num - sa_dev->start_port];
948 	agent = port->agent;
949 
950 	query = kmalloc(sizeof *query, gfp_mask);
951 	if (!query)
952 		return -ENOMEM;
953 
954 	query->sa_query.port     = port;
955 	ret = alloc_mad(&query->sa_query, gfp_mask);
956 	if (ret)
957 		goto err1;
958 
959 	ib_sa_client_get(client);
960 	query->sa_query.client = client;
961 	query->callback        = callback;
962 	query->context         = context;
963 
964 	mad = query->sa_query.mad_buf->mad;
965 	init_mad(mad, agent);
966 
967 	query->sa_query.callback = callback ? ib_sa_mcmember_rec_callback : NULL;
968 	query->sa_query.release  = ib_sa_mcmember_rec_release;
969 	mad->mad_hdr.method	 = method;
970 	mad->mad_hdr.attr_id	 = cpu_to_be16(IB_SA_ATTR_MC_MEMBER_REC);
971 	mad->sa_hdr.comp_mask	 = comp_mask;
972 
973 	ib_pack(mcmember_rec_table, ARRAY_SIZE(mcmember_rec_table),
974 		rec, mad->data);
975 
976 	*sa_query = &query->sa_query;
977 
978 	ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
979 	if (ret < 0)
980 		goto err2;
981 
982 	return ret;
983 
984 err2:
985 	*sa_query = NULL;
986 	ib_sa_client_put(query->sa_query.client);
987 	free_mad(&query->sa_query);
988 
989 err1:
990 	kfree(query);
991 	return ret;
992 }
993 
994 /* Support GuidInfoRecord */
ib_sa_guidinfo_rec_callback(struct ib_sa_query * sa_query,int status,struct ib_sa_mad * mad)995 static void ib_sa_guidinfo_rec_callback(struct ib_sa_query *sa_query,
996 				  int status,
997 				  struct ib_sa_mad *mad)
998 {
999 	struct ib_sa_guidinfo_query *query =
1000 		container_of(sa_query, struct ib_sa_guidinfo_query, sa_query);
1001 
1002 	if (mad) {
1003 		struct ib_sa_guidinfo_rec rec;
1004 
1005 		ib_unpack(guidinfo_rec_table, ARRAY_SIZE(guidinfo_rec_table),
1006 			  mad->data, &rec);
1007 		query->callback(status, &rec, query->context);
1008 	} else
1009 		query->callback(status, NULL, query->context);
1010 }
1011 
ib_sa_guidinfo_rec_release(struct ib_sa_query * sa_query)1012 static void ib_sa_guidinfo_rec_release(struct ib_sa_query *sa_query)
1013 {
1014 	kfree(container_of(sa_query, struct ib_sa_guidinfo_query, sa_query));
1015 }
1016 
ib_sa_guid_info_rec_query(struct ib_sa_client * client,struct ib_device * device,u8 port_num,struct ib_sa_guidinfo_rec * rec,ib_sa_comp_mask comp_mask,u8 method,int timeout_ms,gfp_t gfp_mask,void (* callback)(int status,struct ib_sa_guidinfo_rec * resp,void * context),void * context,struct ib_sa_query ** sa_query)1017 int ib_sa_guid_info_rec_query(struct ib_sa_client *client,
1018 			      struct ib_device *device, u8 port_num,
1019 			      struct ib_sa_guidinfo_rec *rec,
1020 			      ib_sa_comp_mask comp_mask, u8 method,
1021 			      int timeout_ms, gfp_t gfp_mask,
1022 			      void (*callback)(int status,
1023 					       struct ib_sa_guidinfo_rec *resp,
1024 					       void *context),
1025 			      void *context,
1026 			      struct ib_sa_query **sa_query)
1027 {
1028 	struct ib_sa_guidinfo_query *query;
1029 	struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
1030 	struct ib_sa_port   *port;
1031 	struct ib_mad_agent *agent;
1032 	struct ib_sa_mad *mad;
1033 	int ret;
1034 
1035 	if (!sa_dev)
1036 		return -ENODEV;
1037 
1038 	if (method != IB_MGMT_METHOD_GET &&
1039 	    method != IB_MGMT_METHOD_SET &&
1040 	    method != IB_SA_METHOD_DELETE) {
1041 		return -EINVAL;
1042 	}
1043 
1044 	port  = &sa_dev->port[port_num - sa_dev->start_port];
1045 	agent = port->agent;
1046 
1047 	query = kmalloc(sizeof *query, gfp_mask);
1048 	if (!query)
1049 		return -ENOMEM;
1050 
1051 	query->sa_query.port     = port;
1052 	ret = alloc_mad(&query->sa_query, gfp_mask);
1053 	if (ret)
1054 		goto err1;
1055 
1056 	ib_sa_client_get(client);
1057 	query->sa_query.client = client;
1058 	query->callback = callback;
1059 	query->context  = context;
1060 
1061 	mad = query->sa_query.mad_buf->mad;
1062 	init_mad(mad, agent);
1063 
1064 	query->sa_query.callback = callback ? ib_sa_guidinfo_rec_callback : NULL;
1065 	query->sa_query.release  = ib_sa_guidinfo_rec_release;
1066 
1067 	mad->mad_hdr.method	 = method;
1068 	mad->mad_hdr.attr_id	 = cpu_to_be16(IB_SA_ATTR_GUID_INFO_REC);
1069 	mad->sa_hdr.comp_mask	 = comp_mask;
1070 
1071 	ib_pack(guidinfo_rec_table, ARRAY_SIZE(guidinfo_rec_table), rec,
1072 		mad->data);
1073 
1074 	*sa_query = &query->sa_query;
1075 
1076 	ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
1077 	if (ret < 0)
1078 		goto err2;
1079 
1080 	return ret;
1081 
1082 err2:
1083 	*sa_query = NULL;
1084 	ib_sa_client_put(query->sa_query.client);
1085 	free_mad(&query->sa_query);
1086 
1087 err1:
1088 	kfree(query);
1089 	return ret;
1090 }
1091 EXPORT_SYMBOL(ib_sa_guid_info_rec_query);
1092 
send_handler(struct ib_mad_agent * agent,struct ib_mad_send_wc * mad_send_wc)1093 static void send_handler(struct ib_mad_agent *agent,
1094 			 struct ib_mad_send_wc *mad_send_wc)
1095 {
1096 	struct ib_sa_query *query = mad_send_wc->send_buf->context[0];
1097 	unsigned long flags;
1098 
1099 	if (query->callback)
1100 		switch (mad_send_wc->status) {
1101 		case IB_WC_SUCCESS:
1102 			/* No callback -- already got recv */
1103 			break;
1104 		case IB_WC_RESP_TIMEOUT_ERR:
1105 			query->callback(query, -ETIMEDOUT, NULL);
1106 			break;
1107 		case IB_WC_WR_FLUSH_ERR:
1108 			query->callback(query, -EINTR, NULL);
1109 			break;
1110 		default:
1111 			query->callback(query, -EIO, NULL);
1112 			break;
1113 		}
1114 
1115 	spin_lock_irqsave(&idr_lock, flags);
1116 	idr_remove(&query_idr, query->id);
1117 	spin_unlock_irqrestore(&idr_lock, flags);
1118 
1119 	free_mad(query);
1120 	ib_sa_client_put(query->client);
1121 	query->release(query);
1122 }
1123 
recv_handler(struct ib_mad_agent * mad_agent,struct ib_mad_recv_wc * mad_recv_wc)1124 static void recv_handler(struct ib_mad_agent *mad_agent,
1125 			 struct ib_mad_recv_wc *mad_recv_wc)
1126 {
1127 	struct ib_sa_query *query;
1128 	struct ib_mad_send_buf *mad_buf;
1129 
1130 	mad_buf = (void *) (unsigned long) mad_recv_wc->wc->wr_id;
1131 	query = mad_buf->context[0];
1132 
1133 	if (query->callback) {
1134 		if (mad_recv_wc->wc->status == IB_WC_SUCCESS)
1135 			query->callback(query,
1136 					mad_recv_wc->recv_buf.mad->mad_hdr.status ?
1137 					-EINVAL : 0,
1138 					(struct ib_sa_mad *) mad_recv_wc->recv_buf.mad);
1139 		else
1140 			query->callback(query, -EIO, NULL);
1141 	}
1142 
1143 	ib_free_recv_mad(mad_recv_wc);
1144 }
1145 
ib_sa_add_one(struct ib_device * device)1146 static void ib_sa_add_one(struct ib_device *device)
1147 {
1148 	struct ib_sa_device *sa_dev;
1149 	int s, e, i;
1150 
1151 	if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
1152 		return;
1153 
1154 	if (device->node_type == RDMA_NODE_IB_SWITCH)
1155 		s = e = 0;
1156 	else {
1157 		s = 1;
1158 		e = device->phys_port_cnt;
1159 	}
1160 
1161 	sa_dev = kzalloc(sizeof *sa_dev +
1162 			 (e - s + 1) * sizeof (struct ib_sa_port),
1163 			 GFP_KERNEL);
1164 	if (!sa_dev)
1165 		return;
1166 
1167 	sa_dev->start_port = s;
1168 	sa_dev->end_port   = e;
1169 
1170 	for (i = 0; i <= e - s; ++i) {
1171 		spin_lock_init(&sa_dev->port[i].ah_lock);
1172 		if (rdma_port_get_link_layer(device, i + 1) != IB_LINK_LAYER_INFINIBAND)
1173 			continue;
1174 
1175 		sa_dev->port[i].sm_ah    = NULL;
1176 		sa_dev->port[i].port_num = i + s;
1177 
1178 		sa_dev->port[i].agent =
1179 			ib_register_mad_agent(device, i + s, IB_QPT_GSI,
1180 					      NULL, 0, send_handler,
1181 					      recv_handler, sa_dev);
1182 		if (IS_ERR(sa_dev->port[i].agent))
1183 			goto err;
1184 
1185 		INIT_WORK(&sa_dev->port[i].update_task, update_sm_ah);
1186 	}
1187 
1188 	ib_set_client_data(device, &sa_client, sa_dev);
1189 
1190 	/*
1191 	 * We register our event handler after everything is set up,
1192 	 * and then update our cached info after the event handler is
1193 	 * registered to avoid any problems if a port changes state
1194 	 * during our initialization.
1195 	 */
1196 
1197 	INIT_IB_EVENT_HANDLER(&sa_dev->event_handler, device, ib_sa_event);
1198 	if (ib_register_event_handler(&sa_dev->event_handler))
1199 		goto reg_err;
1200 
1201 	for (i = 0; i <= e - s; ++i)
1202 		if (rdma_port_get_link_layer(device, i + 1) == IB_LINK_LAYER_INFINIBAND)
1203 			update_sm_ah(&sa_dev->port[i].update_task);
1204 
1205 	return;
1206 
1207 reg_err:
1208 	ib_set_client_data(device, &sa_client, NULL);
1209 	i = e - s;
1210 err:
1211 	for (; i >= 0; --i)
1212 		if (rdma_port_get_link_layer(device, i + 1) == IB_LINK_LAYER_INFINIBAND &&
1213 		    !IS_ERR(sa_dev->port[i].agent))
1214 				ib_unregister_mad_agent(sa_dev->port[i].agent);
1215 
1216 	kfree(sa_dev);
1217 
1218 	return;
1219 }
1220 
ib_sa_remove_one(struct ib_device * device)1221 static void ib_sa_remove_one(struct ib_device *device)
1222 {
1223 	struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
1224 	int i;
1225 
1226 	if (!sa_dev)
1227 		return;
1228 
1229 	ib_unregister_event_handler(&sa_dev->event_handler);
1230 
1231 	flush_workqueue(ib_wq);
1232 
1233 	for (i = 0; i <= sa_dev->end_port - sa_dev->start_port; ++i) {
1234 		if (rdma_port_get_link_layer(device, i + 1) == IB_LINK_LAYER_INFINIBAND) {
1235 			ib_unregister_mad_agent(sa_dev->port[i].agent);
1236 			if (sa_dev->port[i].sm_ah)
1237 				kref_put(&sa_dev->port[i].sm_ah->ref, free_sm_ah);
1238 		}
1239 
1240 	}
1241 
1242 	kfree(sa_dev);
1243 }
1244 
ib_sa_init(void)1245 static int __init ib_sa_init(void)
1246 {
1247 	int ret;
1248 
1249 	get_random_bytes(&tid, sizeof tid);
1250 
1251 	ret = ib_register_client(&sa_client);
1252 	if (ret) {
1253 		printk(KERN_ERR "Couldn't register ib_sa client\n");
1254 		goto err1;
1255 	}
1256 
1257 	ret = mcast_init();
1258 	if (ret) {
1259 		printk(KERN_ERR "Couldn't initialize multicast handling\n");
1260 		goto err2;
1261 	}
1262 
1263 	return 0;
1264 err2:
1265 	ib_unregister_client(&sa_client);
1266 err1:
1267 	return ret;
1268 }
1269 
ib_sa_cleanup(void)1270 static void __exit ib_sa_cleanup(void)
1271 {
1272 	mcast_cleanup();
1273 	ib_unregister_client(&sa_client);
1274 	idr_destroy(&query_idr);
1275 }
1276 
1277 module_init_order(ib_sa_init, SI_ORDER_SECOND);
1278 module_exit(ib_sa_cleanup);
1279