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 #include <linux/etherdevice.h>
45 #include <rdma/ib_pack.h>
46 #include <rdma/ib_cache.h>
47 #include <rdma/ib_user_sa.h>
48 #include <rdma/ib_marshall.h>
49 #include <rdma/ib_addr.h>
50 #include "sa.h"
51 #include "core_priv.h"
52 
53 #define IB_SA_LOCAL_SVC_TIMEOUT_MIN		100
54 #define IB_SA_LOCAL_SVC_TIMEOUT_DEFAULT		2000
55 #define IB_SA_LOCAL_SVC_TIMEOUT_MAX		200000
56 
57 struct ib_sa_sm_ah {
58 	struct ib_ah        *ah;
59 	struct kref          ref;
60 	u16		     pkey_index;
61 	u8		     src_path_mask;
62 };
63 
64 struct ib_sa_classport_cache {
65 	bool valid;
66 	struct ib_class_port_info data;
67 };
68 
69 struct ib_sa_port {
70 	struct ib_mad_agent *agent;
71 	struct ib_sa_sm_ah  *sm_ah;
72 	struct work_struct   update_task;
73 	struct ib_sa_classport_cache classport_info;
74 	spinlock_t                   classport_lock; /* protects class port info set */
75 	spinlock_t           ah_lock;
76 	u8                   port_num;
77 };
78 
79 struct ib_sa_device {
80 	int                     start_port, end_port;
81 	struct ib_event_handler event_handler;
82 	struct ib_sa_port port[0];
83 };
84 
85 struct ib_sa_query {
86 	void (*callback)(struct ib_sa_query *, int, struct ib_sa_mad *);
87 	void (*release)(struct ib_sa_query *);
88 	struct ib_sa_client    *client;
89 	struct ib_sa_port      *port;
90 	struct ib_mad_send_buf *mad_buf;
91 	struct ib_sa_sm_ah     *sm_ah;
92 	int			id;
93 	u32			flags;
94 	struct list_head	list; /* Local svc request list */
95 	u32			seq; /* Local svc request sequence number */
96 	unsigned long		timeout; /* Local svc timeout */
97 	u8			path_use; /* How will the pathrecord be used */
98 };
99 
100 #define IB_SA_ENABLE_LOCAL_SERVICE	0x00000001
101 #define IB_SA_CANCEL			0x00000002
102 
103 struct ib_sa_service_query {
104 	void (*callback)(int, struct ib_sa_service_rec *, void *);
105 	void *context;
106 	struct ib_sa_query sa_query;
107 };
108 
109 struct ib_sa_path_query {
110 	void (*callback)(int, struct ib_sa_path_rec *, void *);
111 	void *context;
112 	struct ib_sa_query sa_query;
113 };
114 
115 struct ib_sa_guidinfo_query {
116 	void (*callback)(int, struct ib_sa_guidinfo_rec *, void *);
117 	void *context;
118 	struct ib_sa_query sa_query;
119 };
120 
121 struct ib_sa_classport_info_query {
122 	void (*callback)(int, struct ib_class_port_info *, void *);
123 	void *context;
124 	struct ib_sa_query sa_query;
125 };
126 
127 struct ib_sa_mcmember_query {
128 	void (*callback)(int, struct ib_sa_mcmember_rec *, void *);
129 	void *context;
130 	struct ib_sa_query sa_query;
131 };
132 
133 static void ib_sa_add_one(struct ib_device *device);
134 static void ib_sa_remove_one(struct ib_device *device, void *client_data);
135 
136 static struct ib_client sa_client = {
137 	.name   = "sa",
138 	.add    = ib_sa_add_one,
139 	.remove = ib_sa_remove_one
140 };
141 
142 static DEFINE_SPINLOCK(idr_lock);
143 static DEFINE_IDR(query_idr);
144 
145 static DEFINE_SPINLOCK(tid_lock);
146 static u32 tid;
147 
148 #define PATH_REC_FIELD(field) \
149 	.struct_offset_bytes = offsetof(struct ib_sa_path_rec, field),		\
150 	.struct_size_bytes   = sizeof ((struct ib_sa_path_rec *) 0)->field,	\
151 	.field_name          = "sa_path_rec:" #field
152 
153 static const struct ib_field path_rec_table[] = {
154 	{ PATH_REC_FIELD(service_id),
155 	  .offset_words = 0,
156 	  .offset_bits  = 0,
157 	  .size_bits    = 64 },
158 	{ PATH_REC_FIELD(dgid),
159 	  .offset_words = 2,
160 	  .offset_bits  = 0,
161 	  .size_bits    = 128 },
162 	{ PATH_REC_FIELD(sgid),
163 	  .offset_words = 6,
164 	  .offset_bits  = 0,
165 	  .size_bits    = 128 },
166 	{ PATH_REC_FIELD(dlid),
167 	  .offset_words = 10,
168 	  .offset_bits  = 0,
169 	  .size_bits    = 16 },
170 	{ PATH_REC_FIELD(slid),
171 	  .offset_words = 10,
172 	  .offset_bits  = 16,
173 	  .size_bits    = 16 },
174 	{ PATH_REC_FIELD(raw_traffic),
175 	  .offset_words = 11,
176 	  .offset_bits  = 0,
177 	  .size_bits    = 1 },
178 	{ RESERVED,
179 	  .offset_words = 11,
180 	  .offset_bits  = 1,
181 	  .size_bits    = 3 },
182 	{ PATH_REC_FIELD(flow_label),
183 	  .offset_words = 11,
184 	  .offset_bits  = 4,
185 	  .size_bits    = 20 },
186 	{ PATH_REC_FIELD(hop_limit),
187 	  .offset_words = 11,
188 	  .offset_bits  = 24,
189 	  .size_bits    = 8 },
190 	{ PATH_REC_FIELD(traffic_class),
191 	  .offset_words = 12,
192 	  .offset_bits  = 0,
193 	  .size_bits    = 8 },
194 	{ PATH_REC_FIELD(reversible),
195 	  .offset_words = 12,
196 	  .offset_bits  = 8,
197 	  .size_bits    = 1 },
198 	{ PATH_REC_FIELD(numb_path),
199 	  .offset_words = 12,
200 	  .offset_bits  = 9,
201 	  .size_bits    = 7 },
202 	{ PATH_REC_FIELD(pkey),
203 	  .offset_words = 12,
204 	  .offset_bits  = 16,
205 	  .size_bits    = 16 },
206 	{ PATH_REC_FIELD(qos_class),
207 	  .offset_words = 13,
208 	  .offset_bits  = 0,
209 	  .size_bits    = 12 },
210 	{ PATH_REC_FIELD(sl),
211 	  .offset_words = 13,
212 	  .offset_bits  = 12,
213 	  .size_bits    = 4 },
214 	{ PATH_REC_FIELD(mtu_selector),
215 	  .offset_words = 13,
216 	  .offset_bits  = 16,
217 	  .size_bits    = 2 },
218 	{ PATH_REC_FIELD(mtu),
219 	  .offset_words = 13,
220 	  .offset_bits  = 18,
221 	  .size_bits    = 6 },
222 	{ PATH_REC_FIELD(rate_selector),
223 	  .offset_words = 13,
224 	  .offset_bits  = 24,
225 	  .size_bits    = 2 },
226 	{ PATH_REC_FIELD(rate),
227 	  .offset_words = 13,
228 	  .offset_bits  = 26,
229 	  .size_bits    = 6 },
230 	{ PATH_REC_FIELD(packet_life_time_selector),
231 	  .offset_words = 14,
232 	  .offset_bits  = 0,
233 	  .size_bits    = 2 },
234 	{ PATH_REC_FIELD(packet_life_time),
235 	  .offset_words = 14,
236 	  .offset_bits  = 2,
237 	  .size_bits    = 6 },
238 	{ PATH_REC_FIELD(preference),
239 	  .offset_words = 14,
240 	  .offset_bits  = 8,
241 	  .size_bits    = 8 },
242 	{ RESERVED,
243 	  .offset_words = 14,
244 	  .offset_bits  = 16,
245 	  .size_bits    = 48 },
246 };
247 
248 #define MCMEMBER_REC_FIELD(field) \
249 	.struct_offset_bytes = offsetof(struct ib_sa_mcmember_rec, field),	\
250 	.struct_size_bytes   = sizeof ((struct ib_sa_mcmember_rec *) 0)->field,	\
251 	.field_name          = "sa_mcmember_rec:" #field
252 
253 static const struct ib_field mcmember_rec_table[] = {
254 	{ MCMEMBER_REC_FIELD(mgid),
255 	  .offset_words = 0,
256 	  .offset_bits  = 0,
257 	  .size_bits    = 128 },
258 	{ MCMEMBER_REC_FIELD(port_gid),
259 	  .offset_words = 4,
260 	  .offset_bits  = 0,
261 	  .size_bits    = 128 },
262 	{ MCMEMBER_REC_FIELD(qkey),
263 	  .offset_words = 8,
264 	  .offset_bits  = 0,
265 	  .size_bits    = 32 },
266 	{ MCMEMBER_REC_FIELD(mlid),
267 	  .offset_words = 9,
268 	  .offset_bits  = 0,
269 	  .size_bits    = 16 },
270 	{ MCMEMBER_REC_FIELD(mtu_selector),
271 	  .offset_words = 9,
272 	  .offset_bits  = 16,
273 	  .size_bits    = 2 },
274 	{ MCMEMBER_REC_FIELD(mtu),
275 	  .offset_words = 9,
276 	  .offset_bits  = 18,
277 	  .size_bits    = 6 },
278 	{ MCMEMBER_REC_FIELD(traffic_class),
279 	  .offset_words = 9,
280 	  .offset_bits  = 24,
281 	  .size_bits    = 8 },
282 	{ MCMEMBER_REC_FIELD(pkey),
283 	  .offset_words = 10,
284 	  .offset_bits  = 0,
285 	  .size_bits    = 16 },
286 	{ MCMEMBER_REC_FIELD(rate_selector),
287 	  .offset_words = 10,
288 	  .offset_bits  = 16,
289 	  .size_bits    = 2 },
290 	{ MCMEMBER_REC_FIELD(rate),
291 	  .offset_words = 10,
292 	  .offset_bits  = 18,
293 	  .size_bits    = 6 },
294 	{ MCMEMBER_REC_FIELD(packet_life_time_selector),
295 	  .offset_words = 10,
296 	  .offset_bits  = 24,
297 	  .size_bits    = 2 },
298 	{ MCMEMBER_REC_FIELD(packet_life_time),
299 	  .offset_words = 10,
300 	  .offset_bits  = 26,
301 	  .size_bits    = 6 },
302 	{ MCMEMBER_REC_FIELD(sl),
303 	  .offset_words = 11,
304 	  .offset_bits  = 0,
305 	  .size_bits    = 4 },
306 	{ MCMEMBER_REC_FIELD(flow_label),
307 	  .offset_words = 11,
308 	  .offset_bits  = 4,
309 	  .size_bits    = 20 },
310 	{ MCMEMBER_REC_FIELD(hop_limit),
311 	  .offset_words = 11,
312 	  .offset_bits  = 24,
313 	  .size_bits    = 8 },
314 	{ MCMEMBER_REC_FIELD(scope),
315 	  .offset_words = 12,
316 	  .offset_bits  = 0,
317 	  .size_bits    = 4 },
318 	{ MCMEMBER_REC_FIELD(join_state),
319 	  .offset_words = 12,
320 	  .offset_bits  = 4,
321 	  .size_bits    = 4 },
322 	{ MCMEMBER_REC_FIELD(proxy_join),
323 	  .offset_words = 12,
324 	  .offset_bits  = 8,
325 	  .size_bits    = 1 },
326 	{ RESERVED,
327 	  .offset_words = 12,
328 	  .offset_bits  = 9,
329 	  .size_bits    = 23 },
330 };
331 
332 #define SERVICE_REC_FIELD(field) \
333 	.struct_offset_bytes = offsetof(struct ib_sa_service_rec, field),	\
334 	.struct_size_bytes   = sizeof ((struct ib_sa_service_rec *) 0)->field,	\
335 	.field_name          = "sa_service_rec:" #field
336 
337 static const struct ib_field service_rec_table[] = {
338 	{ SERVICE_REC_FIELD(id),
339 	  .offset_words = 0,
340 	  .offset_bits  = 0,
341 	  .size_bits    = 64 },
342 	{ SERVICE_REC_FIELD(gid),
343 	  .offset_words = 2,
344 	  .offset_bits  = 0,
345 	  .size_bits    = 128 },
346 	{ SERVICE_REC_FIELD(pkey),
347 	  .offset_words = 6,
348 	  .offset_bits  = 0,
349 	  .size_bits    = 16 },
350 	{ SERVICE_REC_FIELD(lease),
351 	  .offset_words = 7,
352 	  .offset_bits  = 0,
353 	  .size_bits    = 32 },
354 	{ SERVICE_REC_FIELD(key),
355 	  .offset_words = 8,
356 	  .offset_bits  = 0,
357 	  .size_bits    = 128 },
358 	{ SERVICE_REC_FIELD(name),
359 	  .offset_words = 12,
360 	  .offset_bits  = 0,
361 	  .size_bits    = 64*8 },
362 	{ SERVICE_REC_FIELD(data8),
363 	  .offset_words = 28,
364 	  .offset_bits  = 0,
365 	  .size_bits    = 16*8 },
366 	{ SERVICE_REC_FIELD(data16),
367 	  .offset_words = 32,
368 	  .offset_bits  = 0,
369 	  .size_bits    = 8*16 },
370 	{ SERVICE_REC_FIELD(data32),
371 	  .offset_words = 36,
372 	  .offset_bits  = 0,
373 	  .size_bits    = 4*32 },
374 	{ SERVICE_REC_FIELD(data64),
375 	  .offset_words = 40,
376 	  .offset_bits  = 0,
377 	  .size_bits    = 2*64 },
378 };
379 
380 #define CLASSPORTINFO_REC_FIELD(field) \
381 	.struct_offset_bytes = offsetof(struct ib_class_port_info, field),	\
382 	.struct_size_bytes   = sizeof((struct ib_class_port_info *)0)->field,	\
383 	.field_name          = "ib_class_port_info:" #field
384 
385 static const struct ib_field classport_info_rec_table[] = {
386 	{ CLASSPORTINFO_REC_FIELD(base_version),
387 	  .offset_words = 0,
388 	  .offset_bits  = 0,
389 	  .size_bits    = 8 },
390 	{ CLASSPORTINFO_REC_FIELD(class_version),
391 	  .offset_words = 0,
392 	  .offset_bits  = 8,
393 	  .size_bits    = 8 },
394 	{ CLASSPORTINFO_REC_FIELD(capability_mask),
395 	  .offset_words = 0,
396 	  .offset_bits  = 16,
397 	  .size_bits    = 16 },
398 	{ CLASSPORTINFO_REC_FIELD(cap_mask2_resp_time),
399 	  .offset_words = 1,
400 	  .offset_bits  = 0,
401 	  .size_bits    = 32 },
402 	{ CLASSPORTINFO_REC_FIELD(redirect_gid),
403 	  .offset_words = 2,
404 	  .offset_bits  = 0,
405 	  .size_bits    = 128 },
406 	{ CLASSPORTINFO_REC_FIELD(redirect_tcslfl),
407 	  .offset_words = 6,
408 	  .offset_bits  = 0,
409 	  .size_bits    = 32 },
410 	{ CLASSPORTINFO_REC_FIELD(redirect_lid),
411 	  .offset_words = 7,
412 	  .offset_bits  = 0,
413 	  .size_bits    = 16 },
414 	{ CLASSPORTINFO_REC_FIELD(redirect_pkey),
415 	  .offset_words = 7,
416 	  .offset_bits  = 16,
417 	  .size_bits    = 16 },
418 
419 	{ CLASSPORTINFO_REC_FIELD(redirect_qp),
420 	  .offset_words = 8,
421 	  .offset_bits  = 0,
422 	  .size_bits    = 32 },
423 	{ CLASSPORTINFO_REC_FIELD(redirect_qkey),
424 	  .offset_words = 9,
425 	  .offset_bits  = 0,
426 	  .size_bits    = 32 },
427 
428 	{ CLASSPORTINFO_REC_FIELD(trap_gid),
429 	  .offset_words = 10,
430 	  .offset_bits  = 0,
431 	  .size_bits    = 128 },
432 	{ CLASSPORTINFO_REC_FIELD(trap_tcslfl),
433 	  .offset_words = 14,
434 	  .offset_bits  = 0,
435 	  .size_bits    = 32 },
436 
437 	{ CLASSPORTINFO_REC_FIELD(trap_lid),
438 	  .offset_words = 15,
439 	  .offset_bits  = 0,
440 	  .size_bits    = 16 },
441 	{ CLASSPORTINFO_REC_FIELD(trap_pkey),
442 	  .offset_words = 15,
443 	  .offset_bits  = 16,
444 	  .size_bits    = 16 },
445 
446 	{ CLASSPORTINFO_REC_FIELD(trap_hlqp),
447 	  .offset_words = 16,
448 	  .offset_bits  = 0,
449 	  .size_bits    = 32 },
450 	{ CLASSPORTINFO_REC_FIELD(trap_qkey),
451 	  .offset_words = 17,
452 	  .offset_bits  = 0,
453 	  .size_bits    = 32 },
454 };
455 
456 #define GUIDINFO_REC_FIELD(field) \
457 	.struct_offset_bytes = offsetof(struct ib_sa_guidinfo_rec, field),	\
458 	.struct_size_bytes   = sizeof((struct ib_sa_guidinfo_rec *) 0)->field,	\
459 	.field_name          = "sa_guidinfo_rec:" #field
460 
461 static const struct ib_field guidinfo_rec_table[] = {
462 	{ GUIDINFO_REC_FIELD(lid),
463 	  .offset_words = 0,
464 	  .offset_bits  = 0,
465 	  .size_bits    = 16 },
466 	{ GUIDINFO_REC_FIELD(block_num),
467 	  .offset_words = 0,
468 	  .offset_bits  = 16,
469 	  .size_bits    = 8 },
470 	{ GUIDINFO_REC_FIELD(res1),
471 	  .offset_words = 0,
472 	  .offset_bits  = 24,
473 	  .size_bits    = 8 },
474 	{ GUIDINFO_REC_FIELD(res2),
475 	  .offset_words = 1,
476 	  .offset_bits  = 0,
477 	  .size_bits    = 32 },
478 	{ GUIDINFO_REC_FIELD(guid_info_list),
479 	  .offset_words = 2,
480 	  .offset_bits  = 0,
481 	  .size_bits    = 512 },
482 };
483 
484 static inline void ib_sa_disable_local_svc(struct ib_sa_query *query)
485 {
486 	query->flags &= ~IB_SA_ENABLE_LOCAL_SERVICE;
487 }
488 
489 static void free_sm_ah(struct kref *kref)
490 {
491 	struct ib_sa_sm_ah *sm_ah = container_of(kref, struct ib_sa_sm_ah, ref);
492 
493 	ib_destroy_ah(sm_ah->ah);
494 	kfree(sm_ah);
495 }
496 
497 static void update_sm_ah(struct work_struct *work)
498 {
499 	struct ib_sa_port *port =
500 		container_of(work, struct ib_sa_port, update_task);
501 	struct ib_sa_sm_ah *new_ah;
502 	struct ib_port_attr port_attr;
503 	struct ib_ah_attr   ah_attr;
504 
505 	if (ib_query_port(port->agent->device, port->port_num, &port_attr)) {
506 		pr_warn("Couldn't query port\n");
507 		return;
508 	}
509 
510 	new_ah = kmalloc(sizeof *new_ah, GFP_KERNEL);
511 	if (!new_ah) {
512 		return;
513 	}
514 
515 	kref_init(&new_ah->ref);
516 	new_ah->src_path_mask = (1 << port_attr.lmc) - 1;
517 
518 	new_ah->pkey_index = 0;
519 	if (ib_find_pkey(port->agent->device, port->port_num,
520 			 IB_DEFAULT_PKEY_FULL, &new_ah->pkey_index))
521 		pr_err("Couldn't find index for default PKey\n");
522 
523 	memset(&ah_attr, 0, sizeof ah_attr);
524 	ah_attr.dlid     = port_attr.sm_lid;
525 	ah_attr.sl       = port_attr.sm_sl;
526 	ah_attr.port_num = port->port_num;
527 	if (port_attr.grh_required) {
528 		ah_attr.ah_flags = IB_AH_GRH;
529 		ah_attr.grh.dgid.global.subnet_prefix = cpu_to_be64(port_attr.subnet_prefix);
530 		ah_attr.grh.dgid.global.interface_id = cpu_to_be64(IB_SA_WELL_KNOWN_GUID);
531 	}
532 
533 	new_ah->ah = ib_create_ah(port->agent->qp->pd, &ah_attr);
534 	if (IS_ERR(new_ah->ah)) {
535 		pr_warn("Couldn't create new SM AH\n");
536 		kfree(new_ah);
537 		return;
538 	}
539 
540 	spin_lock_irq(&port->ah_lock);
541 	if (port->sm_ah)
542 		kref_put(&port->sm_ah->ref, free_sm_ah);
543 	port->sm_ah = new_ah;
544 	spin_unlock_irq(&port->ah_lock);
545 
546 }
547 
548 static void ib_sa_event(struct ib_event_handler *handler, struct ib_event *event)
549 {
550 	if (event->event == IB_EVENT_PORT_ERR    ||
551 	    event->event == IB_EVENT_PORT_ACTIVE ||
552 	    event->event == IB_EVENT_LID_CHANGE  ||
553 	    event->event == IB_EVENT_PKEY_CHANGE ||
554 	    event->event == IB_EVENT_SM_CHANGE   ||
555 	    event->event == IB_EVENT_CLIENT_REREGISTER) {
556 		unsigned long flags;
557 		struct ib_sa_device *sa_dev =
558 			container_of(handler, typeof(*sa_dev), event_handler);
559 		struct ib_sa_port *port =
560 			&sa_dev->port[event->element.port_num - sa_dev->start_port];
561 
562 		if (!rdma_cap_ib_sa(handler->device, port->port_num))
563 			return;
564 
565 		spin_lock_irqsave(&port->ah_lock, flags);
566 		if (port->sm_ah)
567 			kref_put(&port->sm_ah->ref, free_sm_ah);
568 		port->sm_ah = NULL;
569 		spin_unlock_irqrestore(&port->ah_lock, flags);
570 
571 		if (event->event == IB_EVENT_SM_CHANGE ||
572 		    event->event == IB_EVENT_CLIENT_REREGISTER ||
573 		    event->event == IB_EVENT_LID_CHANGE) {
574 			spin_lock_irqsave(&port->classport_lock, flags);
575 			port->classport_info.valid = false;
576 			spin_unlock_irqrestore(&port->classport_lock, flags);
577 		}
578 		queue_work(ib_wq, &sa_dev->port[event->element.port_num -
579 					    sa_dev->start_port].update_task);
580 	}
581 }
582 
583 void ib_sa_register_client(struct ib_sa_client *client)
584 {
585 	atomic_set(&client->users, 1);
586 	init_completion(&client->comp);
587 }
588 EXPORT_SYMBOL(ib_sa_register_client);
589 
590 void ib_sa_unregister_client(struct ib_sa_client *client)
591 {
592 	ib_sa_client_put(client);
593 	wait_for_completion(&client->comp);
594 }
595 EXPORT_SYMBOL(ib_sa_unregister_client);
596 
597 /**
598  * ib_sa_cancel_query - try to cancel an SA query
599  * @id:ID of query to cancel
600  * @query:query pointer to cancel
601  *
602  * Try to cancel an SA query.  If the id and query don't match up or
603  * the query has already completed, nothing is done.  Otherwise the
604  * query is canceled and will complete with a status of -EINTR.
605  */
606 void ib_sa_cancel_query(int id, struct ib_sa_query *query)
607 {
608 	unsigned long flags;
609 	struct ib_mad_agent *agent;
610 	struct ib_mad_send_buf *mad_buf;
611 
612 	spin_lock_irqsave(&idr_lock, flags);
613 	if (idr_find(&query_idr, id) != query) {
614 		spin_unlock_irqrestore(&idr_lock, flags);
615 		return;
616 	}
617 	agent = query->port->agent;
618 	mad_buf = query->mad_buf;
619 	spin_unlock_irqrestore(&idr_lock, flags);
620 }
621 EXPORT_SYMBOL(ib_sa_cancel_query);
622 
623 static u8 get_src_path_mask(struct ib_device *device, u8 port_num)
624 {
625 	struct ib_sa_device *sa_dev;
626 	struct ib_sa_port   *port;
627 	unsigned long flags;
628 	u8 src_path_mask;
629 
630 	sa_dev = ib_get_client_data(device, &sa_client);
631 	if (!sa_dev)
632 		return 0x7f;
633 
634 	port  = &sa_dev->port[port_num - sa_dev->start_port];
635 	spin_lock_irqsave(&port->ah_lock, flags);
636 	src_path_mask = port->sm_ah ? port->sm_ah->src_path_mask : 0x7f;
637 	spin_unlock_irqrestore(&port->ah_lock, flags);
638 
639 	return src_path_mask;
640 }
641 
642 int ib_init_ah_from_path(struct ib_device *device, u8 port_num,
643 			 struct ib_sa_path_rec *rec, struct ib_ah_attr *ah_attr)
644 {
645 	int ret;
646 	u16 gid_index;
647 	int use_roce;
648 	struct net_device *ndev = NULL;
649 
650 	memset(ah_attr, 0, sizeof *ah_attr);
651 	ah_attr->dlid = be16_to_cpu(rec->dlid);
652 	ah_attr->sl = rec->sl;
653 	ah_attr->src_path_bits = be16_to_cpu(rec->slid) &
654 				 get_src_path_mask(device, port_num);
655 	ah_attr->port_num = port_num;
656 	ah_attr->static_rate = rec->rate;
657 
658 	use_roce = rdma_cap_eth_ah(device, port_num);
659 
660 	if (use_roce) {
661 		struct net_device *idev;
662 		struct net_device *resolved_dev;
663 		struct rdma_dev_addr dev_addr = {.bound_dev_if = rec->ifindex,
664 						 .net = rec->net ? rec->net :
665 							 &init_net};
666 		union {
667 			struct sockaddr     _sockaddr;
668 			struct sockaddr_in  _sockaddr_in;
669 			struct sockaddr_in6 _sockaddr_in6;
670 		} sgid_addr, dgid_addr;
671 
672 		if (!device->get_netdev)
673 			return -EOPNOTSUPP;
674 
675 		rdma_gid2ip(&sgid_addr._sockaddr, &rec->sgid);
676 		rdma_gid2ip(&dgid_addr._sockaddr, &rec->dgid);
677 
678 		/* validate the route */
679 		ret = rdma_resolve_ip_route(&sgid_addr._sockaddr,
680 					    &dgid_addr._sockaddr, &dev_addr);
681 		if (ret)
682 			return ret;
683 
684 		if ((dev_addr.network == RDMA_NETWORK_IPV4 ||
685 		     dev_addr.network == RDMA_NETWORK_IPV6) &&
686 		    rec->gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP)
687 			return -EINVAL;
688 
689 		idev = device->get_netdev(device, port_num);
690 		if (!idev)
691 			return -ENODEV;
692 
693 		resolved_dev = dev_get_by_index(dev_addr.net,
694 						dev_addr.bound_dev_if);
695 		if (resolved_dev->if_flags & IFF_LOOPBACK) {
696 			dev_put(resolved_dev);
697 			resolved_dev = idev;
698 			dev_hold(resolved_dev);
699 		}
700 		ndev = ib_get_ndev_from_path(rec);
701 		rcu_read_lock();
702 		if ((ndev && ndev != resolved_dev) ||
703 		    (resolved_dev != idev &&
704 		     !rdma_is_upper_dev_rcu(idev, resolved_dev)))
705 			ret = -EHOSTUNREACH;
706 		rcu_read_unlock();
707 		dev_put(idev);
708 		dev_put(resolved_dev);
709 		if (ret) {
710 			if (ndev)
711 				dev_put(ndev);
712 			return ret;
713 		}
714 	}
715 
716 	if (rec->hop_limit > 0 || use_roce) {
717 		ah_attr->ah_flags = IB_AH_GRH;
718 		ah_attr->grh.dgid = rec->dgid;
719 
720 		ret = ib_find_cached_gid_by_port(device, &rec->sgid,
721 						 rec->gid_type, port_num, ndev,
722 						 &gid_index);
723 		if (ret) {
724 			if (ndev)
725 				dev_put(ndev);
726 			return ret;
727 		}
728 
729 		ah_attr->grh.sgid_index    = gid_index;
730 		ah_attr->grh.flow_label    = be32_to_cpu(rec->flow_label);
731 		ah_attr->grh.hop_limit     = rec->hop_limit;
732 		ah_attr->grh.traffic_class = rec->traffic_class;
733 		if (ndev)
734 			dev_put(ndev);
735 	}
736 
737 	if (use_roce)
738 		memcpy(ah_attr->dmac, rec->dmac, ETH_ALEN);
739 
740 	return 0;
741 }
742 EXPORT_SYMBOL(ib_init_ah_from_path);
743 
744 static int alloc_mad(struct ib_sa_query *query, gfp_t gfp_mask)
745 {
746 	unsigned long flags;
747 
748 	spin_lock_irqsave(&query->port->ah_lock, flags);
749 	if (!query->port->sm_ah) {
750 		spin_unlock_irqrestore(&query->port->ah_lock, flags);
751 		return -EAGAIN;
752 	}
753 	kref_get(&query->port->sm_ah->ref);
754 	query->sm_ah = query->port->sm_ah;
755 	spin_unlock_irqrestore(&query->port->ah_lock, flags);
756 
757 	query->mad_buf = ib_create_send_mad(query->port->agent, 1,
758 					    query->sm_ah->pkey_index,
759 					    0, IB_MGMT_SA_HDR, IB_MGMT_SA_DATA,
760 					    gfp_mask,
761 					    IB_MGMT_BASE_VERSION);
762 	if (IS_ERR(query->mad_buf)) {
763 		kref_put(&query->sm_ah->ref, free_sm_ah);
764 		return -ENOMEM;
765 	}
766 
767 	query->mad_buf->ah = query->sm_ah->ah;
768 
769 	return 0;
770 }
771 
772 static void free_mad(struct ib_sa_query *query)
773 {
774 	ib_free_send_mad(query->mad_buf);
775 	kref_put(&query->sm_ah->ref, free_sm_ah);
776 }
777 
778 static void init_mad(struct ib_sa_mad *mad, struct ib_mad_agent *agent)
779 {
780 	unsigned long flags;
781 
782 	memset(mad, 0, sizeof *mad);
783 
784 	mad->mad_hdr.base_version  = IB_MGMT_BASE_VERSION;
785 	mad->mad_hdr.mgmt_class    = IB_MGMT_CLASS_SUBN_ADM;
786 	mad->mad_hdr.class_version = IB_SA_CLASS_VERSION;
787 
788 	spin_lock_irqsave(&tid_lock, flags);
789 	mad->mad_hdr.tid           =
790 		cpu_to_be64(((u64) agent->hi_tid) << 32 | tid++);
791 	spin_unlock_irqrestore(&tid_lock, flags);
792 }
793 
794 static int send_mad(struct ib_sa_query *query, int timeout_ms, gfp_t gfp_mask)
795 {
796 	bool preload = gfpflags_allow_blocking(gfp_mask);
797 	unsigned long flags;
798 	int ret, id;
799 
800 	if (preload)
801 		idr_preload(gfp_mask);
802 	spin_lock_irqsave(&idr_lock, flags);
803 
804 	id = idr_alloc(&query_idr, query, 0, 0, GFP_NOWAIT);
805 
806 	spin_unlock_irqrestore(&idr_lock, flags);
807 	if (preload)
808 		idr_preload_end();
809 	if (id < 0)
810 		return id;
811 
812 	query->mad_buf->timeout_ms  = timeout_ms;
813 	query->mad_buf->context[0] = query;
814 	query->id = id;
815 
816 	if (query->flags & IB_SA_ENABLE_LOCAL_SERVICE) {
817 		ib_sa_disable_local_svc(query);
818 	}
819 
820 	ret = ib_post_send_mad(query->mad_buf, NULL);
821 	if (ret) {
822 		spin_lock_irqsave(&idr_lock, flags);
823 		idr_remove(&query_idr, id);
824 		spin_unlock_irqrestore(&idr_lock, flags);
825 	}
826 
827 	/*
828 	 * It's not safe to dereference query any more, because the
829 	 * send may already have completed and freed the query in
830 	 * another context.
831 	 */
832 	return ret ? ret : id;
833 }
834 
835 void ib_sa_unpack_path(void *attribute, struct ib_sa_path_rec *rec)
836 {
837 	ib_unpack(path_rec_table, ARRAY_SIZE(path_rec_table), attribute, rec);
838 }
839 EXPORT_SYMBOL(ib_sa_unpack_path);
840 
841 void ib_sa_pack_path(struct ib_sa_path_rec *rec, void *attribute)
842 {
843 	ib_pack(path_rec_table, ARRAY_SIZE(path_rec_table), rec, attribute);
844 }
845 EXPORT_SYMBOL(ib_sa_pack_path);
846 
847 static void ib_sa_path_rec_callback(struct ib_sa_query *sa_query,
848 				    int status,
849 				    struct ib_sa_mad *mad)
850 {
851 	struct ib_sa_path_query *query =
852 		container_of(sa_query, struct ib_sa_path_query, sa_query);
853 
854 	if (mad) {
855 		struct ib_sa_path_rec rec;
856 
857 		ib_unpack(path_rec_table, ARRAY_SIZE(path_rec_table),
858 			  mad->data, &rec);
859 		rec.net = NULL;
860 		rec.ifindex = 0;
861 		rec.gid_type = IB_GID_TYPE_IB;
862 		eth_zero_addr(rec.dmac);
863 		query->callback(status, &rec, query->context);
864 	} else
865 		query->callback(status, NULL, query->context);
866 }
867 
868 static void ib_sa_path_rec_release(struct ib_sa_query *sa_query)
869 {
870 	kfree(container_of(sa_query, struct ib_sa_path_query, sa_query));
871 }
872 
873 /**
874  * ib_sa_path_rec_get - Start a Path get query
875  * @client:SA client
876  * @device:device to send query on
877  * @port_num: port number to send query on
878  * @rec:Path Record to send in query
879  * @comp_mask:component mask to send in query
880  * @timeout_ms:time to wait for response
881  * @gfp_mask:GFP mask to use for internal allocations
882  * @callback:function called when query completes, times out or is
883  * canceled
884  * @context:opaque user context passed to callback
885  * @sa_query:query context, used to cancel query
886  *
887  * Send a Path Record Get query to the SA to look up a path.  The
888  * callback function will be called when the query completes (or
889  * fails); status is 0 for a successful response, -EINTR if the query
890  * is canceled, -ETIMEDOUT is the query timed out, or -EIO if an error
891  * occurred sending the query.  The resp parameter of the callback is
892  * only valid if status is 0.
893  *
894  * If the return value of ib_sa_path_rec_get() is negative, it is an
895  * error code.  Otherwise it is a query ID that can be used to cancel
896  * the query.
897  */
898 int ib_sa_path_rec_get(struct ib_sa_client *client,
899 		       struct ib_device *device, u8 port_num,
900 		       struct ib_sa_path_rec *rec,
901 		       ib_sa_comp_mask comp_mask,
902 		       int timeout_ms, gfp_t gfp_mask,
903 		       void (*callback)(int status,
904 					struct ib_sa_path_rec *resp,
905 					void *context),
906 		       void *context,
907 		       struct ib_sa_query **sa_query)
908 {
909 	struct ib_sa_path_query *query;
910 	struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
911 	struct ib_sa_port   *port;
912 	struct ib_mad_agent *agent;
913 	struct ib_sa_mad *mad;
914 	int ret;
915 
916 	if (!sa_dev)
917 		return -ENODEV;
918 
919 	port  = &sa_dev->port[port_num - sa_dev->start_port];
920 	agent = port->agent;
921 
922 	query = kzalloc(sizeof(*query), gfp_mask);
923 	if (!query)
924 		return -ENOMEM;
925 
926 	query->sa_query.port     = port;
927 	ret = alloc_mad(&query->sa_query, gfp_mask);
928 	if (ret)
929 		goto err1;
930 
931 	ib_sa_client_get(client);
932 	query->sa_query.client = client;
933 	query->callback        = callback;
934 	query->context         = context;
935 
936 	mad = query->sa_query.mad_buf->mad;
937 	init_mad(mad, agent);
938 
939 	query->sa_query.callback = callback ? ib_sa_path_rec_callback : NULL;
940 	query->sa_query.release  = ib_sa_path_rec_release;
941 	mad->mad_hdr.method	 = IB_MGMT_METHOD_GET;
942 	mad->mad_hdr.attr_id	 = cpu_to_be16(IB_SA_ATTR_PATH_REC);
943 	mad->sa_hdr.comp_mask	 = comp_mask;
944 
945 	ib_pack(path_rec_table, ARRAY_SIZE(path_rec_table), rec, mad->data);
946 
947 	*sa_query = &query->sa_query;
948 
949 	query->sa_query.flags |= IB_SA_ENABLE_LOCAL_SERVICE;
950 	query->sa_query.mad_buf->context[1] = rec;
951 
952 	ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
953 	if (ret < 0)
954 		goto err2;
955 
956 	return ret;
957 
958 err2:
959 	*sa_query = NULL;
960 	ib_sa_client_put(query->sa_query.client);
961 	free_mad(&query->sa_query);
962 
963 err1:
964 	kfree(query);
965 	return ret;
966 }
967 EXPORT_SYMBOL(ib_sa_path_rec_get);
968 
969 static void ib_sa_service_rec_callback(struct ib_sa_query *sa_query,
970 				    int status,
971 				    struct ib_sa_mad *mad)
972 {
973 	struct ib_sa_service_query *query =
974 		container_of(sa_query, struct ib_sa_service_query, sa_query);
975 
976 	if (mad) {
977 		struct ib_sa_service_rec rec;
978 
979 		ib_unpack(service_rec_table, ARRAY_SIZE(service_rec_table),
980 			  mad->data, &rec);
981 		query->callback(status, &rec, query->context);
982 	} else
983 		query->callback(status, NULL, query->context);
984 }
985 
986 static void ib_sa_service_rec_release(struct ib_sa_query *sa_query)
987 {
988 	kfree(container_of(sa_query, struct ib_sa_service_query, sa_query));
989 }
990 
991 /**
992  * ib_sa_service_rec_query - Start Service Record operation
993  * @client:SA client
994  * @device:device to send request on
995  * @port_num: port number to send request on
996  * @method:SA method - should be get, set, or delete
997  * @rec:Service Record to send in request
998  * @comp_mask:component mask to send in request
999  * @timeout_ms:time to wait for response
1000  * @gfp_mask:GFP mask to use for internal allocations
1001  * @callback:function called when request completes, times out or is
1002  * canceled
1003  * @context:opaque user context passed to callback
1004  * @sa_query:request context, used to cancel request
1005  *
1006  * Send a Service Record set/get/delete to the SA to register,
1007  * unregister or query a service record.
1008  * The callback function will be called when the request completes (or
1009  * fails); status is 0 for a successful response, -EINTR if the query
1010  * is canceled, -ETIMEDOUT is the query timed out, or -EIO if an error
1011  * occurred sending the query.  The resp parameter of the callback is
1012  * only valid if status is 0.
1013  *
1014  * If the return value of ib_sa_service_rec_query() is negative, it is an
1015  * error code.  Otherwise it is a request ID that can be used to cancel
1016  * the query.
1017  */
1018 int ib_sa_service_rec_query(struct ib_sa_client *client,
1019 			    struct ib_device *device, u8 port_num, u8 method,
1020 			    struct ib_sa_service_rec *rec,
1021 			    ib_sa_comp_mask comp_mask,
1022 			    int timeout_ms, gfp_t gfp_mask,
1023 			    void (*callback)(int status,
1024 					     struct ib_sa_service_rec *resp,
1025 					     void *context),
1026 			    void *context,
1027 			    struct ib_sa_query **sa_query)
1028 {
1029 	struct ib_sa_service_query *query;
1030 	struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
1031 	struct ib_sa_port   *port;
1032 	struct ib_mad_agent *agent;
1033 	struct ib_sa_mad *mad;
1034 	int ret;
1035 
1036 	if (!sa_dev)
1037 		return -ENODEV;
1038 
1039 	port  = &sa_dev->port[port_num - sa_dev->start_port];
1040 	agent = port->agent;
1041 
1042 	if (method != IB_MGMT_METHOD_GET &&
1043 	    method != IB_MGMT_METHOD_SET &&
1044 	    method != IB_SA_METHOD_DELETE)
1045 		return -EINVAL;
1046 
1047 	query = kzalloc(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_service_rec_callback : NULL;
1065 	query->sa_query.release  = ib_sa_service_rec_release;
1066 	mad->mad_hdr.method	 = method;
1067 	mad->mad_hdr.attr_id	 = cpu_to_be16(IB_SA_ATTR_SERVICE_REC);
1068 	mad->sa_hdr.comp_mask	 = comp_mask;
1069 
1070 	ib_pack(service_rec_table, ARRAY_SIZE(service_rec_table),
1071 		rec, mad->data);
1072 
1073 	*sa_query = &query->sa_query;
1074 
1075 	ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
1076 	if (ret < 0)
1077 		goto err2;
1078 
1079 	return ret;
1080 
1081 err2:
1082 	*sa_query = NULL;
1083 	ib_sa_client_put(query->sa_query.client);
1084 	free_mad(&query->sa_query);
1085 
1086 err1:
1087 	kfree(query);
1088 	return ret;
1089 }
1090 EXPORT_SYMBOL(ib_sa_service_rec_query);
1091 
1092 static void ib_sa_mcmember_rec_callback(struct ib_sa_query *sa_query,
1093 					int status,
1094 					struct ib_sa_mad *mad)
1095 {
1096 	struct ib_sa_mcmember_query *query =
1097 		container_of(sa_query, struct ib_sa_mcmember_query, sa_query);
1098 
1099 	if (mad) {
1100 		struct ib_sa_mcmember_rec rec;
1101 
1102 		ib_unpack(mcmember_rec_table, ARRAY_SIZE(mcmember_rec_table),
1103 			  mad->data, &rec);
1104 		query->callback(status, &rec, query->context);
1105 	} else
1106 		query->callback(status, NULL, query->context);
1107 }
1108 
1109 static void ib_sa_mcmember_rec_release(struct ib_sa_query *sa_query)
1110 {
1111 	kfree(container_of(sa_query, struct ib_sa_mcmember_query, sa_query));
1112 }
1113 
1114 int ib_sa_mcmember_rec_query(struct ib_sa_client *client,
1115 			     struct ib_device *device, u8 port_num,
1116 			     u8 method,
1117 			     struct ib_sa_mcmember_rec *rec,
1118 			     ib_sa_comp_mask comp_mask,
1119 			     int timeout_ms, gfp_t gfp_mask,
1120 			     void (*callback)(int status,
1121 					      struct ib_sa_mcmember_rec *resp,
1122 					      void *context),
1123 			     void *context,
1124 			     struct ib_sa_query **sa_query)
1125 {
1126 	struct ib_sa_mcmember_query *query;
1127 	struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
1128 	struct ib_sa_port   *port;
1129 	struct ib_mad_agent *agent;
1130 	struct ib_sa_mad *mad;
1131 	int ret;
1132 
1133 	if (!sa_dev)
1134 		return -ENODEV;
1135 
1136 	port  = &sa_dev->port[port_num - sa_dev->start_port];
1137 	agent = port->agent;
1138 
1139 	query = kzalloc(sizeof(*query), gfp_mask);
1140 	if (!query)
1141 		return -ENOMEM;
1142 
1143 	query->sa_query.port     = port;
1144 	ret = alloc_mad(&query->sa_query, gfp_mask);
1145 	if (ret)
1146 		goto err1;
1147 
1148 	ib_sa_client_get(client);
1149 	query->sa_query.client = client;
1150 	query->callback        = callback;
1151 	query->context         = context;
1152 
1153 	mad = query->sa_query.mad_buf->mad;
1154 	init_mad(mad, agent);
1155 
1156 	query->sa_query.callback = callback ? ib_sa_mcmember_rec_callback : NULL;
1157 	query->sa_query.release  = ib_sa_mcmember_rec_release;
1158 	mad->mad_hdr.method	 = method;
1159 	mad->mad_hdr.attr_id	 = cpu_to_be16(IB_SA_ATTR_MC_MEMBER_REC);
1160 	mad->sa_hdr.comp_mask	 = comp_mask;
1161 
1162 	ib_pack(mcmember_rec_table, ARRAY_SIZE(mcmember_rec_table),
1163 		rec, mad->data);
1164 
1165 	*sa_query = &query->sa_query;
1166 
1167 	ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
1168 	if (ret < 0)
1169 		goto err2;
1170 
1171 	return ret;
1172 
1173 err2:
1174 	*sa_query = NULL;
1175 	ib_sa_client_put(query->sa_query.client);
1176 	free_mad(&query->sa_query);
1177 
1178 err1:
1179 	kfree(query);
1180 	return ret;
1181 }
1182 
1183 /* Support GuidInfoRecord */
1184 static void ib_sa_guidinfo_rec_callback(struct ib_sa_query *sa_query,
1185 					int status,
1186 					struct ib_sa_mad *mad)
1187 {
1188 	struct ib_sa_guidinfo_query *query =
1189 		container_of(sa_query, struct ib_sa_guidinfo_query, sa_query);
1190 
1191 	if (mad) {
1192 		struct ib_sa_guidinfo_rec rec;
1193 
1194 		ib_unpack(guidinfo_rec_table, ARRAY_SIZE(guidinfo_rec_table),
1195 			  mad->data, &rec);
1196 		query->callback(status, &rec, query->context);
1197 	} else
1198 		query->callback(status, NULL, query->context);
1199 }
1200 
1201 static void ib_sa_guidinfo_rec_release(struct ib_sa_query *sa_query)
1202 {
1203 	kfree(container_of(sa_query, struct ib_sa_guidinfo_query, sa_query));
1204 }
1205 
1206 int ib_sa_guid_info_rec_query(struct ib_sa_client *client,
1207 			      struct ib_device *device, u8 port_num,
1208 			      struct ib_sa_guidinfo_rec *rec,
1209 			      ib_sa_comp_mask comp_mask, u8 method,
1210 			      int timeout_ms, gfp_t gfp_mask,
1211 			      void (*callback)(int status,
1212 					       struct ib_sa_guidinfo_rec *resp,
1213 					       void *context),
1214 			      void *context,
1215 			      struct ib_sa_query **sa_query)
1216 {
1217 	struct ib_sa_guidinfo_query *query;
1218 	struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
1219 	struct ib_sa_port *port;
1220 	struct ib_mad_agent *agent;
1221 	struct ib_sa_mad *mad;
1222 	int ret;
1223 
1224 	if (!sa_dev)
1225 		return -ENODEV;
1226 
1227 	if (method != IB_MGMT_METHOD_GET &&
1228 	    method != IB_MGMT_METHOD_SET &&
1229 	    method != IB_SA_METHOD_DELETE) {
1230 		return -EINVAL;
1231 	}
1232 
1233 	port  = &sa_dev->port[port_num - sa_dev->start_port];
1234 	agent = port->agent;
1235 
1236 	query = kzalloc(sizeof(*query), gfp_mask);
1237 	if (!query)
1238 		return -ENOMEM;
1239 
1240 	query->sa_query.port = port;
1241 	ret = alloc_mad(&query->sa_query, gfp_mask);
1242 	if (ret)
1243 		goto err1;
1244 
1245 	ib_sa_client_get(client);
1246 	query->sa_query.client = client;
1247 	query->callback        = callback;
1248 	query->context         = context;
1249 
1250 	mad = query->sa_query.mad_buf->mad;
1251 	init_mad(mad, agent);
1252 
1253 	query->sa_query.callback = callback ? ib_sa_guidinfo_rec_callback : NULL;
1254 	query->sa_query.release  = ib_sa_guidinfo_rec_release;
1255 
1256 	mad->mad_hdr.method	 = method;
1257 	mad->mad_hdr.attr_id	 = cpu_to_be16(IB_SA_ATTR_GUID_INFO_REC);
1258 	mad->sa_hdr.comp_mask	 = comp_mask;
1259 
1260 	ib_pack(guidinfo_rec_table, ARRAY_SIZE(guidinfo_rec_table), rec,
1261 		mad->data);
1262 
1263 	*sa_query = &query->sa_query;
1264 
1265 	ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
1266 	if (ret < 0)
1267 		goto err2;
1268 
1269 	return ret;
1270 
1271 err2:
1272 	*sa_query = NULL;
1273 	ib_sa_client_put(query->sa_query.client);
1274 	free_mad(&query->sa_query);
1275 
1276 err1:
1277 	kfree(query);
1278 	return ret;
1279 }
1280 EXPORT_SYMBOL(ib_sa_guid_info_rec_query);
1281 
1282 /* Support get SA ClassPortInfo */
1283 static void ib_sa_classport_info_rec_callback(struct ib_sa_query *sa_query,
1284 					      int status,
1285 					      struct ib_sa_mad *mad)
1286 {
1287 	unsigned long flags;
1288 	struct ib_sa_classport_info_query *query =
1289 		container_of(sa_query, struct ib_sa_classport_info_query, sa_query);
1290 
1291 	if (mad) {
1292 		struct ib_class_port_info rec;
1293 
1294 		ib_unpack(classport_info_rec_table,
1295 			  ARRAY_SIZE(classport_info_rec_table),
1296 			  mad->data, &rec);
1297 
1298 		spin_lock_irqsave(&sa_query->port->classport_lock, flags);
1299 		if (!status && !sa_query->port->classport_info.valid) {
1300 			memcpy(&sa_query->port->classport_info.data, &rec,
1301 			       sizeof(sa_query->port->classport_info.data));
1302 
1303 			sa_query->port->classport_info.valid = true;
1304 		}
1305 		spin_unlock_irqrestore(&sa_query->port->classport_lock, flags);
1306 
1307 		query->callback(status, &rec, query->context);
1308 	} else {
1309 		query->callback(status, NULL, query->context);
1310 	}
1311 }
1312 
1313 static void ib_sa_portclass_info_rec_release(struct ib_sa_query *sa_query)
1314 {
1315 	kfree(container_of(sa_query, struct ib_sa_classport_info_query,
1316 			   sa_query));
1317 }
1318 
1319 int ib_sa_classport_info_rec_query(struct ib_sa_client *client,
1320 				   struct ib_device *device, u8 port_num,
1321 				   int timeout_ms, gfp_t gfp_mask,
1322 				   void (*callback)(int status,
1323 						    struct ib_class_port_info *resp,
1324 						    void *context),
1325 				   void *context,
1326 				   struct ib_sa_query **sa_query)
1327 {
1328 	struct ib_sa_classport_info_query *query;
1329 	struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
1330 	struct ib_sa_port *port;
1331 	struct ib_mad_agent *agent;
1332 	struct ib_sa_mad *mad;
1333 	struct ib_class_port_info cached_class_port_info;
1334 	int ret;
1335 	unsigned long flags;
1336 
1337 	if (!sa_dev)
1338 		return -ENODEV;
1339 
1340 	port  = &sa_dev->port[port_num - sa_dev->start_port];
1341 	agent = port->agent;
1342 
1343 	/* Use cached ClassPortInfo attribute if valid instead of sending mad */
1344 	spin_lock_irqsave(&port->classport_lock, flags);
1345 	if (port->classport_info.valid && callback) {
1346 		memcpy(&cached_class_port_info, &port->classport_info.data,
1347 		       sizeof(cached_class_port_info));
1348 		spin_unlock_irqrestore(&port->classport_lock, flags);
1349 		callback(0, &cached_class_port_info, context);
1350 		return 0;
1351 	}
1352 	spin_unlock_irqrestore(&port->classport_lock, flags);
1353 
1354 	query = kzalloc(sizeof(*query), gfp_mask);
1355 	if (!query)
1356 		return -ENOMEM;
1357 
1358 	query->sa_query.port = port;
1359 	ret = alloc_mad(&query->sa_query, gfp_mask);
1360 	if (ret)
1361 		goto err1;
1362 
1363 	ib_sa_client_get(client);
1364 	query->sa_query.client = client;
1365 	query->callback        = callback;
1366 	query->context         = context;
1367 
1368 	mad = query->sa_query.mad_buf->mad;
1369 	init_mad(mad, agent);
1370 
1371 	query->sa_query.callback = callback ? ib_sa_classport_info_rec_callback : NULL;
1372 
1373 	query->sa_query.release  = ib_sa_portclass_info_rec_release;
1374 	/* support GET only */
1375 	mad->mad_hdr.method	 = IB_MGMT_METHOD_GET;
1376 	mad->mad_hdr.attr_id	 = cpu_to_be16(IB_SA_ATTR_CLASS_PORTINFO);
1377 	mad->sa_hdr.comp_mask	 = 0;
1378 	*sa_query = &query->sa_query;
1379 
1380 	ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
1381 	if (ret < 0)
1382 		goto err2;
1383 
1384 	return ret;
1385 
1386 err2:
1387 	*sa_query = NULL;
1388 	ib_sa_client_put(query->sa_query.client);
1389 	free_mad(&query->sa_query);
1390 
1391 err1:
1392 	kfree(query);
1393 	return ret;
1394 }
1395 EXPORT_SYMBOL(ib_sa_classport_info_rec_query);
1396 
1397 static void send_handler(struct ib_mad_agent *agent,
1398 			 struct ib_mad_send_wc *mad_send_wc)
1399 {
1400 	struct ib_sa_query *query = mad_send_wc->send_buf->context[0];
1401 	unsigned long flags;
1402 
1403 	if (query->callback)
1404 		switch (mad_send_wc->status) {
1405 		case IB_WC_SUCCESS:
1406 			/* No callback -- already got recv */
1407 			break;
1408 		case IB_WC_RESP_TIMEOUT_ERR:
1409 			query->callback(query, -ETIMEDOUT, NULL);
1410 			break;
1411 		case IB_WC_WR_FLUSH_ERR:
1412 			query->callback(query, -EINTR, NULL);
1413 			break;
1414 		default:
1415 			query->callback(query, -EIO, NULL);
1416 			break;
1417 		}
1418 
1419 	spin_lock_irqsave(&idr_lock, flags);
1420 	idr_remove(&query_idr, query->id);
1421 	spin_unlock_irqrestore(&idr_lock, flags);
1422 
1423 	free_mad(query);
1424 	ib_sa_client_put(query->client);
1425 	query->release(query);
1426 }
1427 
1428 static void recv_handler(struct ib_mad_agent *mad_agent,
1429 			 struct ib_mad_send_buf *send_buf,
1430 			 struct ib_mad_recv_wc *mad_recv_wc)
1431 {
1432 	struct ib_sa_query *query;
1433 
1434 	if (!send_buf)
1435 		return;
1436 
1437 	query = send_buf->context[0];
1438 	if (query->callback) {
1439 		if (mad_recv_wc->wc->status == IB_WC_SUCCESS)
1440 			query->callback(query,
1441 					mad_recv_wc->recv_buf.mad->mad_hdr.status ?
1442 					-EINVAL : 0,
1443 					(struct ib_sa_mad *) mad_recv_wc->recv_buf.mad);
1444 		else
1445 			query->callback(query, -EIO, NULL);
1446 	}
1447 
1448 	ib_free_recv_mad(mad_recv_wc);
1449 }
1450 
1451 static void ib_sa_add_one(struct ib_device *device)
1452 {
1453 	struct ib_sa_device *sa_dev;
1454 	int s, e, i;
1455 	int count = 0;
1456 
1457 	s = rdma_start_port(device);
1458 	e = rdma_end_port(device);
1459 
1460 	sa_dev = kzalloc(sizeof *sa_dev +
1461 			 (e - s + 1) * sizeof (struct ib_sa_port),
1462 			 GFP_KERNEL);
1463 	if (!sa_dev)
1464 		return;
1465 
1466 	sa_dev->start_port = s;
1467 	sa_dev->end_port   = e;
1468 
1469 	for (i = 0; i <= e - s; ++i) {
1470 		spin_lock_init(&sa_dev->port[i].ah_lock);
1471 		if (!rdma_cap_ib_sa(device, i + 1))
1472 			continue;
1473 
1474 		sa_dev->port[i].sm_ah    = NULL;
1475 		sa_dev->port[i].port_num = i + s;
1476 
1477 		spin_lock_init(&sa_dev->port[i].classport_lock);
1478 		sa_dev->port[i].classport_info.valid = false;
1479 
1480 		sa_dev->port[i].agent =
1481 			ib_register_mad_agent(device, i + s, IB_QPT_GSI,
1482 					      NULL, 0, send_handler,
1483 					      recv_handler, sa_dev, 0);
1484 		if (IS_ERR(sa_dev->port[i].agent))
1485 			goto err;
1486 
1487 		INIT_WORK(&sa_dev->port[i].update_task, update_sm_ah);
1488 
1489 		count++;
1490 	}
1491 
1492 	if (!count)
1493 		goto free;
1494 
1495 	ib_set_client_data(device, &sa_client, sa_dev);
1496 
1497 	/*
1498 	 * We register our event handler after everything is set up,
1499 	 * and then update our cached info after the event handler is
1500 	 * registered to avoid any problems if a port changes state
1501 	 * during our initialization.
1502 	 */
1503 
1504 	INIT_IB_EVENT_HANDLER(&sa_dev->event_handler, device, ib_sa_event);
1505 	if (ib_register_event_handler(&sa_dev->event_handler))
1506 		goto err;
1507 
1508 	for (i = 0; i <= e - s; ++i) {
1509 		if (rdma_cap_ib_sa(device, i + 1))
1510 			update_sm_ah(&sa_dev->port[i].update_task);
1511 	}
1512 
1513 	return;
1514 
1515 err:
1516 	while (--i >= 0) {
1517 		if (rdma_cap_ib_sa(device, i + 1))
1518 			ib_unregister_mad_agent(sa_dev->port[i].agent);
1519 	}
1520 free:
1521 	kfree(sa_dev);
1522 	return;
1523 }
1524 
1525 static void ib_sa_remove_one(struct ib_device *device, void *client_data)
1526 {
1527 	struct ib_sa_device *sa_dev = client_data;
1528 	int i;
1529 
1530 	if (!sa_dev)
1531 		return;
1532 
1533 	ib_unregister_event_handler(&sa_dev->event_handler);
1534 
1535 	flush_workqueue(ib_wq);
1536 
1537 	for (i = 0; i <= sa_dev->end_port - sa_dev->start_port; ++i) {
1538 		if (rdma_cap_ib_sa(device, i + 1)) {
1539 			ib_unregister_mad_agent(sa_dev->port[i].agent);
1540 			if (sa_dev->port[i].sm_ah)
1541 				kref_put(&sa_dev->port[i].sm_ah->ref, free_sm_ah);
1542 		}
1543 
1544 	}
1545 
1546 	kfree(sa_dev);
1547 }
1548 
1549 int ib_sa_init(void)
1550 {
1551 	int ret;
1552 
1553 	get_random_bytes(&tid, sizeof tid);
1554 
1555 	ret = ib_register_client(&sa_client);
1556 	if (ret) {
1557 		pr_err("Couldn't register ib_sa client\n");
1558 		goto err1;
1559 	}
1560 
1561 	ret = mcast_init();
1562 	if (ret) {
1563 		pr_err("Couldn't initialize multicast handling\n");
1564 		goto err2;
1565 	}
1566 
1567 	return 0;
1568 
1569 err2:
1570 	ib_unregister_client(&sa_client);
1571 err1:
1572 	return ret;
1573 }
1574 
1575 void ib_sa_cleanup(void)
1576 {
1577 	mcast_cleanup();
1578 	ib_unregister_client(&sa_client);
1579 	idr_destroy(&query_idr);
1580 }
1581