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/init.h>
37 #include <linux/err.h>
38 #include <linux/random.h>
39 #include <linux/spinlock.h>
40 #include <linux/slab.h>
41 #include <linux/dma-mapping.h>
42 #include <linux/kref.h>
43 #include <linux/idr.h>
44 #include <linux/workqueue.h>
45 #include <uapi/linux/if_ether.h>
46 #include <rdma/ib_pack.h>
47 #include <rdma/ib_cache.h>
48 #include <rdma/rdma_netlink.h>
49 #include <net/netlink.h>
50 #include <uapi/rdma/ib_user_sa.h>
51 #include <rdma/ib_marshall.h>
52 #include <rdma/ib_addr.h>
53 #include "sa.h"
54 #include "core_priv.h"
55 
56 #define IB_SA_LOCAL_SVC_TIMEOUT_MIN		100
57 #define IB_SA_LOCAL_SVC_TIMEOUT_DEFAULT		2000
58 #define IB_SA_LOCAL_SVC_TIMEOUT_MAX		200000
59 static int sa_local_svc_timeout_ms = IB_SA_LOCAL_SVC_TIMEOUT_DEFAULT;
60 
61 struct ib_sa_sm_ah {
62 	struct ib_ah        *ah;
63 	struct kref          ref;
64 	u16		     pkey_index;
65 	u8		     src_path_mask;
66 };
67 
68 struct ib_sa_classport_cache {
69 	bool valid;
70 	struct ib_class_port_info data;
71 };
72 
73 struct ib_sa_port {
74 	struct ib_mad_agent *agent;
75 	struct ib_sa_sm_ah  *sm_ah;
76 	struct work_struct   update_task;
77 	struct ib_sa_classport_cache classport_info;
78 	spinlock_t                   classport_lock; /* protects class port info set */
79 	spinlock_t           ah_lock;
80 	u8                   port_num;
81 };
82 
83 struct ib_sa_device {
84 	int                     start_port, end_port;
85 	struct ib_event_handler event_handler;
86 	struct ib_sa_port port[0];
87 };
88 
89 struct ib_sa_query {
90 	void (*callback)(struct ib_sa_query *, int, struct ib_sa_mad *);
91 	void (*release)(struct ib_sa_query *);
92 	struct ib_sa_client    *client;
93 	struct ib_sa_port      *port;
94 	struct ib_mad_send_buf *mad_buf;
95 	struct ib_sa_sm_ah     *sm_ah;
96 	int			id;
97 	u32			flags;
98 	struct list_head	list; /* Local svc request list */
99 	u32			seq; /* Local svc request sequence number */
100 	unsigned long		timeout; /* Local svc timeout */
101 	u8			path_use; /* How will the pathrecord be used */
102 };
103 
104 #define IB_SA_ENABLE_LOCAL_SERVICE	0x00000001
105 #define IB_SA_CANCEL			0x00000002
106 
107 struct ib_sa_service_query {
108 	void (*callback)(int, struct ib_sa_service_rec *, void *);
109 	void *context;
110 	struct ib_sa_query sa_query;
111 };
112 
113 struct ib_sa_path_query {
114 	void (*callback)(int, struct ib_sa_path_rec *, void *);
115 	void *context;
116 	struct ib_sa_query sa_query;
117 };
118 
119 struct ib_sa_guidinfo_query {
120 	void (*callback)(int, struct ib_sa_guidinfo_rec *, void *);
121 	void *context;
122 	struct ib_sa_query sa_query;
123 };
124 
125 struct ib_sa_classport_info_query {
126 	void (*callback)(int, struct ib_class_port_info *, void *);
127 	void *context;
128 	struct ib_sa_query sa_query;
129 };
130 
131 struct ib_sa_mcmember_query {
132 	void (*callback)(int, struct ib_sa_mcmember_rec *, void *);
133 	void *context;
134 	struct ib_sa_query sa_query;
135 };
136 
137 static LIST_HEAD(ib_nl_request_list);
138 static DEFINE_SPINLOCK(ib_nl_request_lock);
139 static atomic_t ib_nl_sa_request_seq;
140 static struct workqueue_struct *ib_nl_wq;
141 static struct delayed_work ib_nl_timed_work;
142 static const struct nla_policy ib_nl_policy[LS_NLA_TYPE_MAX] = {
143 	[LS_NLA_TYPE_PATH_RECORD]	= {.type = NLA_BINARY,
144 		.len = sizeof(struct ib_path_rec_data)},
145 	[LS_NLA_TYPE_TIMEOUT]		= {.type = NLA_U32},
146 	[LS_NLA_TYPE_SERVICE_ID]	= {.type = NLA_U64},
147 	[LS_NLA_TYPE_DGID]		= {.type = NLA_BINARY,
148 		.len = sizeof(struct rdma_nla_ls_gid)},
149 	[LS_NLA_TYPE_SGID]		= {.type = NLA_BINARY,
150 		.len = sizeof(struct rdma_nla_ls_gid)},
151 	[LS_NLA_TYPE_TCLASS]		= {.type = NLA_U8},
152 	[LS_NLA_TYPE_PKEY]		= {.type = NLA_U16},
153 	[LS_NLA_TYPE_QOS_CLASS]		= {.type = NLA_U16},
154 };
155 
156 
157 static void ib_sa_add_one(struct ib_device *device);
158 static void ib_sa_remove_one(struct ib_device *device, void *client_data);
159 
160 static struct ib_client sa_client = {
161 	.name   = "sa",
162 	.add    = ib_sa_add_one,
163 	.remove = ib_sa_remove_one
164 };
165 
166 static DEFINE_SPINLOCK(idr_lock);
167 static DEFINE_IDR(query_idr);
168 
169 static DEFINE_SPINLOCK(tid_lock);
170 static u32 tid;
171 
172 #define PATH_REC_FIELD(field) \
173 	.struct_offset_bytes = offsetof(struct ib_sa_path_rec, field),		\
174 	.struct_size_bytes   = sizeof ((struct ib_sa_path_rec *) 0)->field,	\
175 	.field_name          = "sa_path_rec:" #field
176 
177 static const struct ib_field path_rec_table[] = {
178 	{ PATH_REC_FIELD(service_id),
179 	  .offset_words = 0,
180 	  .offset_bits  = 0,
181 	  .size_bits    = 64 },
182 	{ PATH_REC_FIELD(dgid),
183 	  .offset_words = 2,
184 	  .offset_bits  = 0,
185 	  .size_bits    = 128 },
186 	{ PATH_REC_FIELD(sgid),
187 	  .offset_words = 6,
188 	  .offset_bits  = 0,
189 	  .size_bits    = 128 },
190 	{ PATH_REC_FIELD(dlid),
191 	  .offset_words = 10,
192 	  .offset_bits  = 0,
193 	  .size_bits    = 16 },
194 	{ PATH_REC_FIELD(slid),
195 	  .offset_words = 10,
196 	  .offset_bits  = 16,
197 	  .size_bits    = 16 },
198 	{ PATH_REC_FIELD(raw_traffic),
199 	  .offset_words = 11,
200 	  .offset_bits  = 0,
201 	  .size_bits    = 1 },
202 	{ RESERVED,
203 	  .offset_words = 11,
204 	  .offset_bits  = 1,
205 	  .size_bits    = 3 },
206 	{ PATH_REC_FIELD(flow_label),
207 	  .offset_words = 11,
208 	  .offset_bits  = 4,
209 	  .size_bits    = 20 },
210 	{ PATH_REC_FIELD(hop_limit),
211 	  .offset_words = 11,
212 	  .offset_bits  = 24,
213 	  .size_bits    = 8 },
214 	{ PATH_REC_FIELD(traffic_class),
215 	  .offset_words = 12,
216 	  .offset_bits  = 0,
217 	  .size_bits    = 8 },
218 	{ PATH_REC_FIELD(reversible),
219 	  .offset_words = 12,
220 	  .offset_bits  = 8,
221 	  .size_bits    = 1 },
222 	{ PATH_REC_FIELD(numb_path),
223 	  .offset_words = 12,
224 	  .offset_bits  = 9,
225 	  .size_bits    = 7 },
226 	{ PATH_REC_FIELD(pkey),
227 	  .offset_words = 12,
228 	  .offset_bits  = 16,
229 	  .size_bits    = 16 },
230 	{ PATH_REC_FIELD(qos_class),
231 	  .offset_words = 13,
232 	  .offset_bits  = 0,
233 	  .size_bits    = 12 },
234 	{ PATH_REC_FIELD(sl),
235 	  .offset_words = 13,
236 	  .offset_bits  = 12,
237 	  .size_bits    = 4 },
238 	{ PATH_REC_FIELD(mtu_selector),
239 	  .offset_words = 13,
240 	  .offset_bits  = 16,
241 	  .size_bits    = 2 },
242 	{ PATH_REC_FIELD(mtu),
243 	  .offset_words = 13,
244 	  .offset_bits  = 18,
245 	  .size_bits    = 6 },
246 	{ PATH_REC_FIELD(rate_selector),
247 	  .offset_words = 13,
248 	  .offset_bits  = 24,
249 	  .size_bits    = 2 },
250 	{ PATH_REC_FIELD(rate),
251 	  .offset_words = 13,
252 	  .offset_bits  = 26,
253 	  .size_bits    = 6 },
254 	{ PATH_REC_FIELD(packet_life_time_selector),
255 	  .offset_words = 14,
256 	  .offset_bits  = 0,
257 	  .size_bits    = 2 },
258 	{ PATH_REC_FIELD(packet_life_time),
259 	  .offset_words = 14,
260 	  .offset_bits  = 2,
261 	  .size_bits    = 6 },
262 	{ PATH_REC_FIELD(preference),
263 	  .offset_words = 14,
264 	  .offset_bits  = 8,
265 	  .size_bits    = 8 },
266 	{ RESERVED,
267 	  .offset_words = 14,
268 	  .offset_bits  = 16,
269 	  .size_bits    = 48 },
270 };
271 
272 #define MCMEMBER_REC_FIELD(field) \
273 	.struct_offset_bytes = offsetof(struct ib_sa_mcmember_rec, field),	\
274 	.struct_size_bytes   = sizeof ((struct ib_sa_mcmember_rec *) 0)->field,	\
275 	.field_name          = "sa_mcmember_rec:" #field
276 
277 static const struct ib_field mcmember_rec_table[] = {
278 	{ MCMEMBER_REC_FIELD(mgid),
279 	  .offset_words = 0,
280 	  .offset_bits  = 0,
281 	  .size_bits    = 128 },
282 	{ MCMEMBER_REC_FIELD(port_gid),
283 	  .offset_words = 4,
284 	  .offset_bits  = 0,
285 	  .size_bits    = 128 },
286 	{ MCMEMBER_REC_FIELD(qkey),
287 	  .offset_words = 8,
288 	  .offset_bits  = 0,
289 	  .size_bits    = 32 },
290 	{ MCMEMBER_REC_FIELD(mlid),
291 	  .offset_words = 9,
292 	  .offset_bits  = 0,
293 	  .size_bits    = 16 },
294 	{ MCMEMBER_REC_FIELD(mtu_selector),
295 	  .offset_words = 9,
296 	  .offset_bits  = 16,
297 	  .size_bits    = 2 },
298 	{ MCMEMBER_REC_FIELD(mtu),
299 	  .offset_words = 9,
300 	  .offset_bits  = 18,
301 	  .size_bits    = 6 },
302 	{ MCMEMBER_REC_FIELD(traffic_class),
303 	  .offset_words = 9,
304 	  .offset_bits  = 24,
305 	  .size_bits    = 8 },
306 	{ MCMEMBER_REC_FIELD(pkey),
307 	  .offset_words = 10,
308 	  .offset_bits  = 0,
309 	  .size_bits    = 16 },
310 	{ MCMEMBER_REC_FIELD(rate_selector),
311 	  .offset_words = 10,
312 	  .offset_bits  = 16,
313 	  .size_bits    = 2 },
314 	{ MCMEMBER_REC_FIELD(rate),
315 	  .offset_words = 10,
316 	  .offset_bits  = 18,
317 	  .size_bits    = 6 },
318 	{ MCMEMBER_REC_FIELD(packet_life_time_selector),
319 	  .offset_words = 10,
320 	  .offset_bits  = 24,
321 	  .size_bits    = 2 },
322 	{ MCMEMBER_REC_FIELD(packet_life_time),
323 	  .offset_words = 10,
324 	  .offset_bits  = 26,
325 	  .size_bits    = 6 },
326 	{ MCMEMBER_REC_FIELD(sl),
327 	  .offset_words = 11,
328 	  .offset_bits  = 0,
329 	  .size_bits    = 4 },
330 	{ MCMEMBER_REC_FIELD(flow_label),
331 	  .offset_words = 11,
332 	  .offset_bits  = 4,
333 	  .size_bits    = 20 },
334 	{ MCMEMBER_REC_FIELD(hop_limit),
335 	  .offset_words = 11,
336 	  .offset_bits  = 24,
337 	  .size_bits    = 8 },
338 	{ MCMEMBER_REC_FIELD(scope),
339 	  .offset_words = 12,
340 	  .offset_bits  = 0,
341 	  .size_bits    = 4 },
342 	{ MCMEMBER_REC_FIELD(join_state),
343 	  .offset_words = 12,
344 	  .offset_bits  = 4,
345 	  .size_bits    = 4 },
346 	{ MCMEMBER_REC_FIELD(proxy_join),
347 	  .offset_words = 12,
348 	  .offset_bits  = 8,
349 	  .size_bits    = 1 },
350 	{ RESERVED,
351 	  .offset_words = 12,
352 	  .offset_bits  = 9,
353 	  .size_bits    = 23 },
354 };
355 
356 #define SERVICE_REC_FIELD(field) \
357 	.struct_offset_bytes = offsetof(struct ib_sa_service_rec, field),	\
358 	.struct_size_bytes   = sizeof ((struct ib_sa_service_rec *) 0)->field,	\
359 	.field_name          = "sa_service_rec:" #field
360 
361 static const struct ib_field service_rec_table[] = {
362 	{ SERVICE_REC_FIELD(id),
363 	  .offset_words = 0,
364 	  .offset_bits  = 0,
365 	  .size_bits    = 64 },
366 	{ SERVICE_REC_FIELD(gid),
367 	  .offset_words = 2,
368 	  .offset_bits  = 0,
369 	  .size_bits    = 128 },
370 	{ SERVICE_REC_FIELD(pkey),
371 	  .offset_words = 6,
372 	  .offset_bits  = 0,
373 	  .size_bits    = 16 },
374 	{ SERVICE_REC_FIELD(lease),
375 	  .offset_words = 7,
376 	  .offset_bits  = 0,
377 	  .size_bits    = 32 },
378 	{ SERVICE_REC_FIELD(key),
379 	  .offset_words = 8,
380 	  .offset_bits  = 0,
381 	  .size_bits    = 128 },
382 	{ SERVICE_REC_FIELD(name),
383 	  .offset_words = 12,
384 	  .offset_bits  = 0,
385 	  .size_bits    = 64*8 },
386 	{ SERVICE_REC_FIELD(data8),
387 	  .offset_words = 28,
388 	  .offset_bits  = 0,
389 	  .size_bits    = 16*8 },
390 	{ SERVICE_REC_FIELD(data16),
391 	  .offset_words = 32,
392 	  .offset_bits  = 0,
393 	  .size_bits    = 8*16 },
394 	{ SERVICE_REC_FIELD(data32),
395 	  .offset_words = 36,
396 	  .offset_bits  = 0,
397 	  .size_bits    = 4*32 },
398 	{ SERVICE_REC_FIELD(data64),
399 	  .offset_words = 40,
400 	  .offset_bits  = 0,
401 	  .size_bits    = 2*64 },
402 };
403 
404 #define CLASSPORTINFO_REC_FIELD(field) \
405 	.struct_offset_bytes = offsetof(struct ib_class_port_info, field),	\
406 	.struct_size_bytes   = sizeof((struct ib_class_port_info *)0)->field,	\
407 	.field_name          = "ib_class_port_info:" #field
408 
409 static const struct ib_field classport_info_rec_table[] = {
410 	{ CLASSPORTINFO_REC_FIELD(base_version),
411 	  .offset_words = 0,
412 	  .offset_bits  = 0,
413 	  .size_bits    = 8 },
414 	{ CLASSPORTINFO_REC_FIELD(class_version),
415 	  .offset_words = 0,
416 	  .offset_bits  = 8,
417 	  .size_bits    = 8 },
418 	{ CLASSPORTINFO_REC_FIELD(capability_mask),
419 	  .offset_words = 0,
420 	  .offset_bits  = 16,
421 	  .size_bits    = 16 },
422 	{ CLASSPORTINFO_REC_FIELD(cap_mask2_resp_time),
423 	  .offset_words = 1,
424 	  .offset_bits  = 0,
425 	  .size_bits    = 32 },
426 	{ CLASSPORTINFO_REC_FIELD(redirect_gid),
427 	  .offset_words = 2,
428 	  .offset_bits  = 0,
429 	  .size_bits    = 128 },
430 	{ CLASSPORTINFO_REC_FIELD(redirect_tcslfl),
431 	  .offset_words = 6,
432 	  .offset_bits  = 0,
433 	  .size_bits    = 32 },
434 	{ CLASSPORTINFO_REC_FIELD(redirect_lid),
435 	  .offset_words = 7,
436 	  .offset_bits  = 0,
437 	  .size_bits    = 16 },
438 	{ CLASSPORTINFO_REC_FIELD(redirect_pkey),
439 	  .offset_words = 7,
440 	  .offset_bits  = 16,
441 	  .size_bits    = 16 },
442 
443 	{ CLASSPORTINFO_REC_FIELD(redirect_qp),
444 	  .offset_words = 8,
445 	  .offset_bits  = 0,
446 	  .size_bits    = 32 },
447 	{ CLASSPORTINFO_REC_FIELD(redirect_qkey),
448 	  .offset_words = 9,
449 	  .offset_bits  = 0,
450 	  .size_bits    = 32 },
451 
452 	{ CLASSPORTINFO_REC_FIELD(trap_gid),
453 	  .offset_words = 10,
454 	  .offset_bits  = 0,
455 	  .size_bits    = 128 },
456 	{ CLASSPORTINFO_REC_FIELD(trap_tcslfl),
457 	  .offset_words = 14,
458 	  .offset_bits  = 0,
459 	  .size_bits    = 32 },
460 
461 	{ CLASSPORTINFO_REC_FIELD(trap_lid),
462 	  .offset_words = 15,
463 	  .offset_bits  = 0,
464 	  .size_bits    = 16 },
465 	{ CLASSPORTINFO_REC_FIELD(trap_pkey),
466 	  .offset_words = 15,
467 	  .offset_bits  = 16,
468 	  .size_bits    = 16 },
469 
470 	{ CLASSPORTINFO_REC_FIELD(trap_hlqp),
471 	  .offset_words = 16,
472 	  .offset_bits  = 0,
473 	  .size_bits    = 32 },
474 	{ CLASSPORTINFO_REC_FIELD(trap_qkey),
475 	  .offset_words = 17,
476 	  .offset_bits  = 0,
477 	  .size_bits    = 32 },
478 };
479 
480 #define GUIDINFO_REC_FIELD(field) \
481 	.struct_offset_bytes = offsetof(struct ib_sa_guidinfo_rec, field),	\
482 	.struct_size_bytes   = sizeof((struct ib_sa_guidinfo_rec *) 0)->field,	\
483 	.field_name          = "sa_guidinfo_rec:" #field
484 
485 static const struct ib_field guidinfo_rec_table[] = {
486 	{ GUIDINFO_REC_FIELD(lid),
487 	  .offset_words = 0,
488 	  .offset_bits  = 0,
489 	  .size_bits    = 16 },
490 	{ GUIDINFO_REC_FIELD(block_num),
491 	  .offset_words = 0,
492 	  .offset_bits  = 16,
493 	  .size_bits    = 8 },
494 	{ GUIDINFO_REC_FIELD(res1),
495 	  .offset_words = 0,
496 	  .offset_bits  = 24,
497 	  .size_bits    = 8 },
498 	{ GUIDINFO_REC_FIELD(res2),
499 	  .offset_words = 1,
500 	  .offset_bits  = 0,
501 	  .size_bits    = 32 },
502 	{ GUIDINFO_REC_FIELD(guid_info_list),
503 	  .offset_words = 2,
504 	  .offset_bits  = 0,
505 	  .size_bits    = 512 },
506 };
507 
508 static inline void ib_sa_disable_local_svc(struct ib_sa_query *query)
509 {
510 	query->flags &= ~IB_SA_ENABLE_LOCAL_SERVICE;
511 }
512 
513 static inline int ib_sa_query_cancelled(struct ib_sa_query *query)
514 {
515 	return (query->flags & IB_SA_CANCEL);
516 }
517 
518 static void ib_nl_set_path_rec_attrs(struct sk_buff *skb,
519 				     struct ib_sa_query *query)
520 {
521 	struct ib_sa_path_rec *sa_rec = query->mad_buf->context[1];
522 	struct ib_sa_mad *mad = query->mad_buf->mad;
523 	ib_sa_comp_mask comp_mask = mad->sa_hdr.comp_mask;
524 	u16 val16;
525 	u64 val64;
526 	struct rdma_ls_resolve_header *header;
527 
528 	query->mad_buf->context[1] = NULL;
529 
530 	/* Construct the family header first */
531 	header = (struct rdma_ls_resolve_header *)
532 		skb_put(skb, NLMSG_ALIGN(sizeof(*header)));
533 	memcpy(header->device_name, query->port->agent->device->name,
534 	       LS_DEVICE_NAME_MAX);
535 	header->port_num = query->port->port_num;
536 
537 	if ((comp_mask & IB_SA_PATH_REC_REVERSIBLE) &&
538 	    sa_rec->reversible != 0)
539 		query->path_use = LS_RESOLVE_PATH_USE_GMP;
540 	else
541 		query->path_use = LS_RESOLVE_PATH_USE_UNIDIRECTIONAL;
542 	header->path_use = query->path_use;
543 
544 	/* Now build the attributes */
545 	if (comp_mask & IB_SA_PATH_REC_SERVICE_ID) {
546 		val64 = be64_to_cpu(sa_rec->service_id);
547 		nla_put(skb, RDMA_NLA_F_MANDATORY | LS_NLA_TYPE_SERVICE_ID,
548 			sizeof(val64), &val64);
549 	}
550 	if (comp_mask & IB_SA_PATH_REC_DGID)
551 		nla_put(skb, RDMA_NLA_F_MANDATORY | LS_NLA_TYPE_DGID,
552 			sizeof(sa_rec->dgid), &sa_rec->dgid);
553 	if (comp_mask & IB_SA_PATH_REC_SGID)
554 		nla_put(skb, RDMA_NLA_F_MANDATORY | LS_NLA_TYPE_SGID,
555 			sizeof(sa_rec->sgid), &sa_rec->sgid);
556 	if (comp_mask & IB_SA_PATH_REC_TRAFFIC_CLASS)
557 		nla_put(skb, RDMA_NLA_F_MANDATORY | LS_NLA_TYPE_TCLASS,
558 			sizeof(sa_rec->traffic_class), &sa_rec->traffic_class);
559 
560 	if (comp_mask & IB_SA_PATH_REC_PKEY) {
561 		val16 = be16_to_cpu(sa_rec->pkey);
562 		nla_put(skb, RDMA_NLA_F_MANDATORY | LS_NLA_TYPE_PKEY,
563 			sizeof(val16), &val16);
564 	}
565 	if (comp_mask & IB_SA_PATH_REC_QOS_CLASS) {
566 		val16 = be16_to_cpu(sa_rec->qos_class);
567 		nla_put(skb, RDMA_NLA_F_MANDATORY | LS_NLA_TYPE_QOS_CLASS,
568 			sizeof(val16), &val16);
569 	}
570 }
571 
572 static int ib_nl_get_path_rec_attrs_len(ib_sa_comp_mask comp_mask)
573 {
574 	int len = 0;
575 
576 	if (comp_mask & IB_SA_PATH_REC_SERVICE_ID)
577 		len += nla_total_size(sizeof(u64));
578 	if (comp_mask & IB_SA_PATH_REC_DGID)
579 		len += nla_total_size(sizeof(struct rdma_nla_ls_gid));
580 	if (comp_mask & IB_SA_PATH_REC_SGID)
581 		len += nla_total_size(sizeof(struct rdma_nla_ls_gid));
582 	if (comp_mask & IB_SA_PATH_REC_TRAFFIC_CLASS)
583 		len += nla_total_size(sizeof(u8));
584 	if (comp_mask & IB_SA_PATH_REC_PKEY)
585 		len += nla_total_size(sizeof(u16));
586 	if (comp_mask & IB_SA_PATH_REC_QOS_CLASS)
587 		len += nla_total_size(sizeof(u16));
588 
589 	/*
590 	 * Make sure that at least some of the required comp_mask bits are
591 	 * set.
592 	 */
593 	if (WARN_ON(len == 0))
594 		return len;
595 
596 	/* Add the family header */
597 	len += NLMSG_ALIGN(sizeof(struct rdma_ls_resolve_header));
598 
599 	return len;
600 }
601 
602 static int ib_nl_send_msg(struct ib_sa_query *query, gfp_t gfp_mask)
603 {
604 	struct sk_buff *skb = NULL;
605 	struct nlmsghdr *nlh;
606 	void *data;
607 	int ret = 0;
608 	struct ib_sa_mad *mad;
609 	int len;
610 
611 	mad = query->mad_buf->mad;
612 	len = ib_nl_get_path_rec_attrs_len(mad->sa_hdr.comp_mask);
613 	if (len <= 0)
614 		return -EMSGSIZE;
615 
616 	skb = nlmsg_new(len, gfp_mask);
617 	if (!skb)
618 		return -ENOMEM;
619 
620 	/* Put nlmsg header only for now */
621 	data = ibnl_put_msg(skb, &nlh, query->seq, 0, RDMA_NL_LS,
622 			    RDMA_NL_LS_OP_RESOLVE, NLM_F_REQUEST);
623 	if (!data) {
624 		nlmsg_free(skb);
625 		return -EMSGSIZE;
626 	}
627 
628 	/* Add attributes */
629 	ib_nl_set_path_rec_attrs(skb, query);
630 
631 	/* Repair the nlmsg header length */
632 	nlmsg_end(skb, nlh);
633 
634 	ret = ibnl_multicast(skb, nlh, RDMA_NL_GROUP_LS, gfp_mask);
635 	if (!ret)
636 		ret = len;
637 	else
638 		ret = 0;
639 
640 	return ret;
641 }
642 
643 static int ib_nl_make_request(struct ib_sa_query *query, gfp_t gfp_mask)
644 {
645 	unsigned long flags;
646 	unsigned long delay;
647 	int ret;
648 
649 	INIT_LIST_HEAD(&query->list);
650 	query->seq = (u32)atomic_inc_return(&ib_nl_sa_request_seq);
651 
652 	/* Put the request on the list first.*/
653 	spin_lock_irqsave(&ib_nl_request_lock, flags);
654 	delay = msecs_to_jiffies(sa_local_svc_timeout_ms);
655 	query->timeout = delay + jiffies;
656 	list_add_tail(&query->list, &ib_nl_request_list);
657 	/* Start the timeout if this is the only request */
658 	if (ib_nl_request_list.next == &query->list)
659 		queue_delayed_work(ib_nl_wq, &ib_nl_timed_work, delay);
660 	spin_unlock_irqrestore(&ib_nl_request_lock, flags);
661 
662 	ret = ib_nl_send_msg(query, gfp_mask);
663 	if (ret <= 0) {
664 		ret = -EIO;
665 		/* Remove the request */
666 		spin_lock_irqsave(&ib_nl_request_lock, flags);
667 		list_del(&query->list);
668 		spin_unlock_irqrestore(&ib_nl_request_lock, flags);
669 	} else {
670 		ret = 0;
671 	}
672 
673 	return ret;
674 }
675 
676 static int ib_nl_cancel_request(struct ib_sa_query *query)
677 {
678 	unsigned long flags;
679 	struct ib_sa_query *wait_query;
680 	int found = 0;
681 
682 	spin_lock_irqsave(&ib_nl_request_lock, flags);
683 	list_for_each_entry(wait_query, &ib_nl_request_list, list) {
684 		/* Let the timeout to take care of the callback */
685 		if (query == wait_query) {
686 			query->flags |= IB_SA_CANCEL;
687 			query->timeout = jiffies;
688 			list_move(&query->list, &ib_nl_request_list);
689 			found = 1;
690 			mod_delayed_work(ib_nl_wq, &ib_nl_timed_work, 1);
691 			break;
692 		}
693 	}
694 	spin_unlock_irqrestore(&ib_nl_request_lock, flags);
695 
696 	return found;
697 }
698 
699 static void send_handler(struct ib_mad_agent *agent,
700 			 struct ib_mad_send_wc *mad_send_wc);
701 
702 static void ib_nl_process_good_resolve_rsp(struct ib_sa_query *query,
703 					   const struct nlmsghdr *nlh)
704 {
705 	struct ib_mad_send_wc mad_send_wc;
706 	struct ib_sa_mad *mad = NULL;
707 	const struct nlattr *head, *curr;
708 	struct ib_path_rec_data  *rec;
709 	int len, rem;
710 	u32 mask = 0;
711 	int status = -EIO;
712 
713 	if (query->callback) {
714 		head = (const struct nlattr *) nlmsg_data(nlh);
715 		len = nlmsg_len(nlh);
716 		switch (query->path_use) {
717 		case LS_RESOLVE_PATH_USE_UNIDIRECTIONAL:
718 			mask = IB_PATH_PRIMARY | IB_PATH_OUTBOUND;
719 			break;
720 
721 		case LS_RESOLVE_PATH_USE_ALL:
722 		case LS_RESOLVE_PATH_USE_GMP:
723 		default:
724 			mask = IB_PATH_PRIMARY | IB_PATH_GMP |
725 				IB_PATH_BIDIRECTIONAL;
726 			break;
727 		}
728 		nla_for_each_attr(curr, head, len, rem) {
729 			if (curr->nla_type == LS_NLA_TYPE_PATH_RECORD) {
730 				rec = nla_data(curr);
731 				/*
732 				 * Get the first one. In the future, we may
733 				 * need to get up to 6 pathrecords.
734 				 */
735 				if ((rec->flags & mask) == mask) {
736 					mad = query->mad_buf->mad;
737 					mad->mad_hdr.method |=
738 						IB_MGMT_METHOD_RESP;
739 					memcpy(mad->data, rec->path_rec,
740 					       sizeof(rec->path_rec));
741 					status = 0;
742 					break;
743 				}
744 			}
745 		}
746 		query->callback(query, status, mad);
747 	}
748 
749 	mad_send_wc.send_buf = query->mad_buf;
750 	mad_send_wc.status = IB_WC_SUCCESS;
751 	send_handler(query->mad_buf->mad_agent, &mad_send_wc);
752 }
753 
754 static void ib_nl_request_timeout(struct work_struct *work)
755 {
756 	unsigned long flags;
757 	struct ib_sa_query *query;
758 	unsigned long delay;
759 	struct ib_mad_send_wc mad_send_wc;
760 	int ret;
761 
762 	spin_lock_irqsave(&ib_nl_request_lock, flags);
763 	while (!list_empty(&ib_nl_request_list)) {
764 		query = list_entry(ib_nl_request_list.next,
765 				   struct ib_sa_query, list);
766 
767 		if (time_after(query->timeout, jiffies)) {
768 			delay = query->timeout - jiffies;
769 			if ((long)delay <= 0)
770 				delay = 1;
771 			queue_delayed_work(ib_nl_wq, &ib_nl_timed_work, delay);
772 			break;
773 		}
774 
775 		list_del(&query->list);
776 		ib_sa_disable_local_svc(query);
777 		/* Hold the lock to protect against query cancellation */
778 		if (ib_sa_query_cancelled(query))
779 			ret = -1;
780 		else
781 			ret = ib_post_send_mad(query->mad_buf, NULL);
782 		if (ret) {
783 			mad_send_wc.send_buf = query->mad_buf;
784 			mad_send_wc.status = IB_WC_WR_FLUSH_ERR;
785 			spin_unlock_irqrestore(&ib_nl_request_lock, flags);
786 			send_handler(query->port->agent, &mad_send_wc);
787 			spin_lock_irqsave(&ib_nl_request_lock, flags);
788 		}
789 	}
790 	spin_unlock_irqrestore(&ib_nl_request_lock, flags);
791 }
792 
793 int ib_nl_handle_set_timeout(struct sk_buff *skb,
794 			     struct netlink_callback *cb)
795 {
796 	const struct nlmsghdr *nlh = (struct nlmsghdr *)cb->nlh;
797 	int timeout, delta, abs_delta;
798 	const struct nlattr *attr;
799 	unsigned long flags;
800 	struct ib_sa_query *query;
801 	long delay = 0;
802 	struct nlattr *tb[LS_NLA_TYPE_MAX];
803 	int ret;
804 
805 	if (!(nlh->nlmsg_flags & NLM_F_REQUEST) ||
806 	    !(NETLINK_CB(skb).sk) ||
807 	    !netlink_capable(skb, CAP_NET_ADMIN))
808 		return -EPERM;
809 
810 	ret = nla_parse(tb, LS_NLA_TYPE_MAX - 1, nlmsg_data(nlh),
811 			nlmsg_len(nlh), ib_nl_policy);
812 	attr = (const struct nlattr *)tb[LS_NLA_TYPE_TIMEOUT];
813 	if (ret || !attr)
814 		goto settimeout_out;
815 
816 	timeout = *(int *) nla_data(attr);
817 	if (timeout < IB_SA_LOCAL_SVC_TIMEOUT_MIN)
818 		timeout = IB_SA_LOCAL_SVC_TIMEOUT_MIN;
819 	if (timeout > IB_SA_LOCAL_SVC_TIMEOUT_MAX)
820 		timeout = IB_SA_LOCAL_SVC_TIMEOUT_MAX;
821 
822 	delta = timeout - sa_local_svc_timeout_ms;
823 	if (delta < 0)
824 		abs_delta = -delta;
825 	else
826 		abs_delta = delta;
827 
828 	if (delta != 0) {
829 		spin_lock_irqsave(&ib_nl_request_lock, flags);
830 		sa_local_svc_timeout_ms = timeout;
831 		list_for_each_entry(query, &ib_nl_request_list, list) {
832 			if (delta < 0 && abs_delta > query->timeout)
833 				query->timeout = 0;
834 			else
835 				query->timeout += delta;
836 
837 			/* Get the new delay from the first entry */
838 			if (!delay) {
839 				delay = query->timeout - jiffies;
840 				if (delay <= 0)
841 					delay = 1;
842 			}
843 		}
844 		if (delay)
845 			mod_delayed_work(ib_nl_wq, &ib_nl_timed_work,
846 					 (unsigned long)delay);
847 		spin_unlock_irqrestore(&ib_nl_request_lock, flags);
848 	}
849 
850 settimeout_out:
851 	return skb->len;
852 }
853 
854 static inline int ib_nl_is_good_resolve_resp(const struct nlmsghdr *nlh)
855 {
856 	struct nlattr *tb[LS_NLA_TYPE_MAX];
857 	int ret;
858 
859 	if (nlh->nlmsg_flags & RDMA_NL_LS_F_ERR)
860 		return 0;
861 
862 	ret = nla_parse(tb, LS_NLA_TYPE_MAX - 1, nlmsg_data(nlh),
863 			nlmsg_len(nlh), ib_nl_policy);
864 	if (ret)
865 		return 0;
866 
867 	return 1;
868 }
869 
870 int ib_nl_handle_resolve_resp(struct sk_buff *skb,
871 			      struct netlink_callback *cb)
872 {
873 	const struct nlmsghdr *nlh = (struct nlmsghdr *)cb->nlh;
874 	unsigned long flags;
875 	struct ib_sa_query *query;
876 	struct ib_mad_send_buf *send_buf;
877 	struct ib_mad_send_wc mad_send_wc;
878 	int found = 0;
879 	int ret;
880 
881 	if ((nlh->nlmsg_flags & NLM_F_REQUEST) ||
882 	    !(NETLINK_CB(skb).sk) ||
883 	    !netlink_capable(skb, CAP_NET_ADMIN))
884 		return -EPERM;
885 
886 	spin_lock_irqsave(&ib_nl_request_lock, flags);
887 	list_for_each_entry(query, &ib_nl_request_list, list) {
888 		/*
889 		 * If the query is cancelled, let the timeout routine
890 		 * take care of it.
891 		 */
892 		if (nlh->nlmsg_seq == query->seq) {
893 			found = !ib_sa_query_cancelled(query);
894 			if (found)
895 				list_del(&query->list);
896 			break;
897 		}
898 	}
899 
900 	if (!found) {
901 		spin_unlock_irqrestore(&ib_nl_request_lock, flags);
902 		goto resp_out;
903 	}
904 
905 	send_buf = query->mad_buf;
906 
907 	if (!ib_nl_is_good_resolve_resp(nlh)) {
908 		/* if the result is a failure, send out the packet via IB */
909 		ib_sa_disable_local_svc(query);
910 		ret = ib_post_send_mad(query->mad_buf, NULL);
911 		spin_unlock_irqrestore(&ib_nl_request_lock, flags);
912 		if (ret) {
913 			mad_send_wc.send_buf = send_buf;
914 			mad_send_wc.status = IB_WC_GENERAL_ERR;
915 			send_handler(query->port->agent, &mad_send_wc);
916 		}
917 	} else {
918 		spin_unlock_irqrestore(&ib_nl_request_lock, flags);
919 		ib_nl_process_good_resolve_rsp(query, nlh);
920 	}
921 
922 resp_out:
923 	return skb->len;
924 }
925 
926 static void free_sm_ah(struct kref *kref)
927 {
928 	struct ib_sa_sm_ah *sm_ah = container_of(kref, struct ib_sa_sm_ah, ref);
929 
930 	ib_destroy_ah(sm_ah->ah);
931 	kfree(sm_ah);
932 }
933 
934 static void update_sm_ah(struct work_struct *work)
935 {
936 	struct ib_sa_port *port =
937 		container_of(work, struct ib_sa_port, update_task);
938 	struct ib_sa_sm_ah *new_ah;
939 	struct ib_port_attr port_attr;
940 	struct ib_ah_attr   ah_attr;
941 
942 	if (ib_query_port(port->agent->device, port->port_num, &port_attr)) {
943 		pr_warn("Couldn't query port\n");
944 		return;
945 	}
946 
947 	new_ah = kmalloc(sizeof *new_ah, GFP_KERNEL);
948 	if (!new_ah) {
949 		return;
950 	}
951 
952 	kref_init(&new_ah->ref);
953 	new_ah->src_path_mask = (1 << port_attr.lmc) - 1;
954 
955 	new_ah->pkey_index = 0;
956 	if (ib_find_pkey(port->agent->device, port->port_num,
957 			 IB_DEFAULT_PKEY_FULL, &new_ah->pkey_index))
958 		pr_err("Couldn't find index for default PKey\n");
959 
960 	memset(&ah_attr, 0, sizeof ah_attr);
961 	ah_attr.dlid     = port_attr.sm_lid;
962 	ah_attr.sl       = port_attr.sm_sl;
963 	ah_attr.port_num = port->port_num;
964 	if (port_attr.grh_required) {
965 		ah_attr.ah_flags = IB_AH_GRH;
966 		ah_attr.grh.dgid.global.subnet_prefix = cpu_to_be64(port_attr.subnet_prefix);
967 		ah_attr.grh.dgid.global.interface_id = cpu_to_be64(IB_SA_WELL_KNOWN_GUID);
968 	}
969 
970 	new_ah->ah = ib_create_ah(port->agent->qp->pd, &ah_attr);
971 	if (IS_ERR(new_ah->ah)) {
972 		pr_warn("Couldn't create new SM AH\n");
973 		kfree(new_ah);
974 		return;
975 	}
976 
977 	spin_lock_irq(&port->ah_lock);
978 	if (port->sm_ah)
979 		kref_put(&port->sm_ah->ref, free_sm_ah);
980 	port->sm_ah = new_ah;
981 	spin_unlock_irq(&port->ah_lock);
982 
983 }
984 
985 static void ib_sa_event(struct ib_event_handler *handler, struct ib_event *event)
986 {
987 	if (event->event == IB_EVENT_PORT_ERR    ||
988 	    event->event == IB_EVENT_PORT_ACTIVE ||
989 	    event->event == IB_EVENT_LID_CHANGE  ||
990 	    event->event == IB_EVENT_PKEY_CHANGE ||
991 	    event->event == IB_EVENT_SM_CHANGE   ||
992 	    event->event == IB_EVENT_CLIENT_REREGISTER) {
993 		unsigned long flags;
994 		struct ib_sa_device *sa_dev =
995 			container_of(handler, typeof(*sa_dev), event_handler);
996 		struct ib_sa_port *port =
997 			&sa_dev->port[event->element.port_num - sa_dev->start_port];
998 
999 		if (!rdma_cap_ib_sa(handler->device, port->port_num))
1000 			return;
1001 
1002 		spin_lock_irqsave(&port->ah_lock, flags);
1003 		if (port->sm_ah)
1004 			kref_put(&port->sm_ah->ref, free_sm_ah);
1005 		port->sm_ah = NULL;
1006 		spin_unlock_irqrestore(&port->ah_lock, flags);
1007 
1008 		if (event->event == IB_EVENT_SM_CHANGE ||
1009 		    event->event == IB_EVENT_CLIENT_REREGISTER ||
1010 		    event->event == IB_EVENT_LID_CHANGE) {
1011 			spin_lock_irqsave(&port->classport_lock, flags);
1012 			port->classport_info.valid = false;
1013 			spin_unlock_irqrestore(&port->classport_lock, flags);
1014 		}
1015 		queue_work(ib_wq, &sa_dev->port[event->element.port_num -
1016 					    sa_dev->start_port].update_task);
1017 	}
1018 }
1019 
1020 void ib_sa_register_client(struct ib_sa_client *client)
1021 {
1022 	atomic_set(&client->users, 1);
1023 	init_completion(&client->comp);
1024 }
1025 EXPORT_SYMBOL(ib_sa_register_client);
1026 
1027 void ib_sa_unregister_client(struct ib_sa_client *client)
1028 {
1029 	ib_sa_client_put(client);
1030 	wait_for_completion(&client->comp);
1031 }
1032 EXPORT_SYMBOL(ib_sa_unregister_client);
1033 
1034 /**
1035  * ib_sa_cancel_query - try to cancel an SA query
1036  * @id:ID of query to cancel
1037  * @query:query pointer to cancel
1038  *
1039  * Try to cancel an SA query.  If the id and query don't match up or
1040  * the query has already completed, nothing is done.  Otherwise the
1041  * query is canceled and will complete with a status of -EINTR.
1042  */
1043 void ib_sa_cancel_query(int id, struct ib_sa_query *query)
1044 {
1045 	unsigned long flags;
1046 	struct ib_mad_agent *agent;
1047 	struct ib_mad_send_buf *mad_buf;
1048 
1049 	spin_lock_irqsave(&idr_lock, flags);
1050 	if (idr_find(&query_idr, id) != query) {
1051 		spin_unlock_irqrestore(&idr_lock, flags);
1052 		return;
1053 	}
1054 	agent = query->port->agent;
1055 	mad_buf = query->mad_buf;
1056 	spin_unlock_irqrestore(&idr_lock, flags);
1057 
1058 	/*
1059 	 * If the query is still on the netlink request list, schedule
1060 	 * it to be cancelled by the timeout routine. Otherwise, it has been
1061 	 * sent to the MAD layer and has to be cancelled from there.
1062 	 */
1063 	if (!ib_nl_cancel_request(query))
1064 		ib_cancel_mad(agent, mad_buf);
1065 }
1066 EXPORT_SYMBOL(ib_sa_cancel_query);
1067 
1068 static u8 get_src_path_mask(struct ib_device *device, u8 port_num)
1069 {
1070 	struct ib_sa_device *sa_dev;
1071 	struct ib_sa_port   *port;
1072 	unsigned long flags;
1073 	u8 src_path_mask;
1074 
1075 	sa_dev = ib_get_client_data(device, &sa_client);
1076 	if (!sa_dev)
1077 		return 0x7f;
1078 
1079 	port  = &sa_dev->port[port_num - sa_dev->start_port];
1080 	spin_lock_irqsave(&port->ah_lock, flags);
1081 	src_path_mask = port->sm_ah ? port->sm_ah->src_path_mask : 0x7f;
1082 	spin_unlock_irqrestore(&port->ah_lock, flags);
1083 
1084 	return src_path_mask;
1085 }
1086 
1087 int ib_init_ah_from_path(struct ib_device *device, u8 port_num,
1088 			 struct ib_sa_path_rec *rec, struct ib_ah_attr *ah_attr)
1089 {
1090 	int ret;
1091 	u16 gid_index;
1092 	int use_roce;
1093 	struct net_device *ndev = NULL;
1094 
1095 	memset(ah_attr, 0, sizeof *ah_attr);
1096 	ah_attr->dlid = be16_to_cpu(rec->dlid);
1097 	ah_attr->sl = rec->sl;
1098 	ah_attr->src_path_bits = be16_to_cpu(rec->slid) &
1099 				 get_src_path_mask(device, port_num);
1100 	ah_attr->port_num = port_num;
1101 	ah_attr->static_rate = rec->rate;
1102 
1103 	use_roce = rdma_cap_eth_ah(device, port_num);
1104 
1105 	if (use_roce) {
1106 		struct net_device *idev;
1107 		struct net_device *resolved_dev;
1108 		struct rdma_dev_addr dev_addr = {.bound_dev_if = rec->ifindex,
1109 						 .net = rec->net ? rec->net :
1110 							 &init_net};
1111 		union {
1112 			struct sockaddr     _sockaddr;
1113 			struct sockaddr_in  _sockaddr_in;
1114 			struct sockaddr_in6 _sockaddr_in6;
1115 		} sgid_addr, dgid_addr;
1116 
1117 		if (!device->get_netdev)
1118 			return -EOPNOTSUPP;
1119 
1120 		rdma_gid2ip(&sgid_addr._sockaddr, &rec->sgid);
1121 		rdma_gid2ip(&dgid_addr._sockaddr, &rec->dgid);
1122 
1123 		/* validate the route */
1124 		ret = rdma_resolve_ip_route(&sgid_addr._sockaddr,
1125 					    &dgid_addr._sockaddr, &dev_addr);
1126 		if (ret)
1127 			return ret;
1128 
1129 		if ((dev_addr.network == RDMA_NETWORK_IPV4 ||
1130 		     dev_addr.network == RDMA_NETWORK_IPV6) &&
1131 		    rec->gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP)
1132 			return -EINVAL;
1133 
1134 		idev = device->get_netdev(device, port_num);
1135 		if (!idev)
1136 			return -ENODEV;
1137 
1138 		resolved_dev = dev_get_by_index(dev_addr.net,
1139 						dev_addr.bound_dev_if);
1140 		if (resolved_dev->flags & IFF_LOOPBACK) {
1141 			dev_put(resolved_dev);
1142 			resolved_dev = idev;
1143 			dev_hold(resolved_dev);
1144 		}
1145 		ndev = ib_get_ndev_from_path(rec);
1146 		rcu_read_lock();
1147 		if ((ndev && ndev != resolved_dev) ||
1148 		    (resolved_dev != idev &&
1149 		     !rdma_is_upper_dev_rcu(idev, resolved_dev)))
1150 			ret = -EHOSTUNREACH;
1151 		rcu_read_unlock();
1152 		dev_put(idev);
1153 		dev_put(resolved_dev);
1154 		if (ret) {
1155 			if (ndev)
1156 				dev_put(ndev);
1157 			return ret;
1158 		}
1159 	}
1160 
1161 	if (rec->hop_limit > 0 || use_roce) {
1162 		ah_attr->ah_flags = IB_AH_GRH;
1163 		ah_attr->grh.dgid = rec->dgid;
1164 
1165 		ret = ib_find_cached_gid_by_port(device, &rec->sgid,
1166 						 rec->gid_type, port_num, ndev,
1167 						 &gid_index);
1168 		if (ret) {
1169 			if (ndev)
1170 				dev_put(ndev);
1171 			return ret;
1172 		}
1173 
1174 		ah_attr->grh.sgid_index    = gid_index;
1175 		ah_attr->grh.flow_label    = be32_to_cpu(rec->flow_label);
1176 		ah_attr->grh.hop_limit     = rec->hop_limit;
1177 		ah_attr->grh.traffic_class = rec->traffic_class;
1178 		if (ndev)
1179 			dev_put(ndev);
1180 	}
1181 
1182 	if (use_roce)
1183 		memcpy(ah_attr->dmac, rec->dmac, ETH_ALEN);
1184 
1185 	return 0;
1186 }
1187 EXPORT_SYMBOL(ib_init_ah_from_path);
1188 
1189 static int alloc_mad(struct ib_sa_query *query, gfp_t gfp_mask)
1190 {
1191 	unsigned long flags;
1192 
1193 	spin_lock_irqsave(&query->port->ah_lock, flags);
1194 	if (!query->port->sm_ah) {
1195 		spin_unlock_irqrestore(&query->port->ah_lock, flags);
1196 		return -EAGAIN;
1197 	}
1198 	kref_get(&query->port->sm_ah->ref);
1199 	query->sm_ah = query->port->sm_ah;
1200 	spin_unlock_irqrestore(&query->port->ah_lock, flags);
1201 
1202 	query->mad_buf = ib_create_send_mad(query->port->agent, 1,
1203 					    query->sm_ah->pkey_index,
1204 					    0, IB_MGMT_SA_HDR, IB_MGMT_SA_DATA,
1205 					    gfp_mask,
1206 					    IB_MGMT_BASE_VERSION);
1207 	if (IS_ERR(query->mad_buf)) {
1208 		kref_put(&query->sm_ah->ref, free_sm_ah);
1209 		return -ENOMEM;
1210 	}
1211 
1212 	query->mad_buf->ah = query->sm_ah->ah;
1213 
1214 	return 0;
1215 }
1216 
1217 static void free_mad(struct ib_sa_query *query)
1218 {
1219 	ib_free_send_mad(query->mad_buf);
1220 	kref_put(&query->sm_ah->ref, free_sm_ah);
1221 }
1222 
1223 static void init_mad(struct ib_sa_mad *mad, struct ib_mad_agent *agent)
1224 {
1225 	unsigned long flags;
1226 
1227 	memset(mad, 0, sizeof *mad);
1228 
1229 	mad->mad_hdr.base_version  = IB_MGMT_BASE_VERSION;
1230 	mad->mad_hdr.mgmt_class    = IB_MGMT_CLASS_SUBN_ADM;
1231 	mad->mad_hdr.class_version = IB_SA_CLASS_VERSION;
1232 
1233 	spin_lock_irqsave(&tid_lock, flags);
1234 	mad->mad_hdr.tid           =
1235 		cpu_to_be64(((u64) agent->hi_tid) << 32 | tid++);
1236 	spin_unlock_irqrestore(&tid_lock, flags);
1237 }
1238 
1239 static int send_mad(struct ib_sa_query *query, int timeout_ms, gfp_t gfp_mask)
1240 {
1241 	bool preload = gfpflags_allow_blocking(gfp_mask);
1242 	unsigned long flags;
1243 	int ret, id;
1244 
1245 	if (preload)
1246 		idr_preload(gfp_mask);
1247 	spin_lock_irqsave(&idr_lock, flags);
1248 
1249 	id = idr_alloc(&query_idr, query, 0, 0, GFP_NOWAIT);
1250 
1251 	spin_unlock_irqrestore(&idr_lock, flags);
1252 	if (preload)
1253 		idr_preload_end();
1254 	if (id < 0)
1255 		return id;
1256 
1257 	query->mad_buf->timeout_ms  = timeout_ms;
1258 	query->mad_buf->context[0] = query;
1259 	query->id = id;
1260 
1261 	if (query->flags & IB_SA_ENABLE_LOCAL_SERVICE) {
1262 		if (!ibnl_chk_listeners(RDMA_NL_GROUP_LS)) {
1263 			if (!ib_nl_make_request(query, gfp_mask))
1264 				return id;
1265 		}
1266 		ib_sa_disable_local_svc(query);
1267 	}
1268 
1269 	ret = ib_post_send_mad(query->mad_buf, NULL);
1270 	if (ret) {
1271 		spin_lock_irqsave(&idr_lock, flags);
1272 		idr_remove(&query_idr, id);
1273 		spin_unlock_irqrestore(&idr_lock, flags);
1274 	}
1275 
1276 	/*
1277 	 * It's not safe to dereference query any more, because the
1278 	 * send may already have completed and freed the query in
1279 	 * another context.
1280 	 */
1281 	return ret ? ret : id;
1282 }
1283 
1284 void ib_sa_unpack_path(void *attribute, struct ib_sa_path_rec *rec)
1285 {
1286 	ib_unpack(path_rec_table, ARRAY_SIZE(path_rec_table), attribute, rec);
1287 }
1288 EXPORT_SYMBOL(ib_sa_unpack_path);
1289 
1290 void ib_sa_pack_path(struct ib_sa_path_rec *rec, void *attribute)
1291 {
1292 	ib_pack(path_rec_table, ARRAY_SIZE(path_rec_table), rec, attribute);
1293 }
1294 EXPORT_SYMBOL(ib_sa_pack_path);
1295 
1296 static void ib_sa_path_rec_callback(struct ib_sa_query *sa_query,
1297 				    int status,
1298 				    struct ib_sa_mad *mad)
1299 {
1300 	struct ib_sa_path_query *query =
1301 		container_of(sa_query, struct ib_sa_path_query, sa_query);
1302 
1303 	if (mad) {
1304 		struct ib_sa_path_rec rec;
1305 
1306 		ib_unpack(path_rec_table, ARRAY_SIZE(path_rec_table),
1307 			  mad->data, &rec);
1308 		rec.net = NULL;
1309 		rec.ifindex = 0;
1310 		rec.gid_type = IB_GID_TYPE_IB;
1311 		eth_zero_addr(rec.dmac);
1312 		query->callback(status, &rec, query->context);
1313 	} else
1314 		query->callback(status, NULL, query->context);
1315 }
1316 
1317 static void ib_sa_path_rec_release(struct ib_sa_query *sa_query)
1318 {
1319 	kfree(container_of(sa_query, struct ib_sa_path_query, sa_query));
1320 }
1321 
1322 /**
1323  * ib_sa_path_rec_get - Start a Path get query
1324  * @client:SA client
1325  * @device:device to send query on
1326  * @port_num: port number to send query on
1327  * @rec:Path Record to send in query
1328  * @comp_mask:component mask to send in query
1329  * @timeout_ms:time to wait for response
1330  * @gfp_mask:GFP mask to use for internal allocations
1331  * @callback:function called when query completes, times out or is
1332  * canceled
1333  * @context:opaque user context passed to callback
1334  * @sa_query:query context, used to cancel query
1335  *
1336  * Send a Path Record Get query to the SA to look up a path.  The
1337  * callback function will be called when the query completes (or
1338  * fails); status is 0 for a successful response, -EINTR if the query
1339  * is canceled, -ETIMEDOUT is the query timed out, or -EIO if an error
1340  * occurred sending the query.  The resp parameter of the callback is
1341  * only valid if status is 0.
1342  *
1343  * If the return value of ib_sa_path_rec_get() is negative, it is an
1344  * error code.  Otherwise it is a query ID that can be used to cancel
1345  * the query.
1346  */
1347 int ib_sa_path_rec_get(struct ib_sa_client *client,
1348 		       struct ib_device *device, u8 port_num,
1349 		       struct ib_sa_path_rec *rec,
1350 		       ib_sa_comp_mask comp_mask,
1351 		       int timeout_ms, gfp_t gfp_mask,
1352 		       void (*callback)(int status,
1353 					struct ib_sa_path_rec *resp,
1354 					void *context),
1355 		       void *context,
1356 		       struct ib_sa_query **sa_query)
1357 {
1358 	struct ib_sa_path_query *query;
1359 	struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
1360 	struct ib_sa_port   *port;
1361 	struct ib_mad_agent *agent;
1362 	struct ib_sa_mad *mad;
1363 	int ret;
1364 
1365 	if (!sa_dev)
1366 		return -ENODEV;
1367 
1368 	port  = &sa_dev->port[port_num - sa_dev->start_port];
1369 	agent = port->agent;
1370 
1371 	query = kzalloc(sizeof(*query), gfp_mask);
1372 	if (!query)
1373 		return -ENOMEM;
1374 
1375 	query->sa_query.port     = port;
1376 	ret = alloc_mad(&query->sa_query, gfp_mask);
1377 	if (ret)
1378 		goto err1;
1379 
1380 	ib_sa_client_get(client);
1381 	query->sa_query.client = client;
1382 	query->callback        = callback;
1383 	query->context         = context;
1384 
1385 	mad = query->sa_query.mad_buf->mad;
1386 	init_mad(mad, agent);
1387 
1388 	query->sa_query.callback = callback ? ib_sa_path_rec_callback : NULL;
1389 	query->sa_query.release  = ib_sa_path_rec_release;
1390 	mad->mad_hdr.method	 = IB_MGMT_METHOD_GET;
1391 	mad->mad_hdr.attr_id	 = cpu_to_be16(IB_SA_ATTR_PATH_REC);
1392 	mad->sa_hdr.comp_mask	 = comp_mask;
1393 
1394 	ib_pack(path_rec_table, ARRAY_SIZE(path_rec_table), rec, mad->data);
1395 
1396 	*sa_query = &query->sa_query;
1397 
1398 	query->sa_query.flags |= IB_SA_ENABLE_LOCAL_SERVICE;
1399 	query->sa_query.mad_buf->context[1] = rec;
1400 
1401 	ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
1402 	if (ret < 0)
1403 		goto err2;
1404 
1405 	return ret;
1406 
1407 err2:
1408 	*sa_query = NULL;
1409 	ib_sa_client_put(query->sa_query.client);
1410 	free_mad(&query->sa_query);
1411 
1412 err1:
1413 	kfree(query);
1414 	return ret;
1415 }
1416 EXPORT_SYMBOL(ib_sa_path_rec_get);
1417 
1418 static void ib_sa_service_rec_callback(struct ib_sa_query *sa_query,
1419 				    int status,
1420 				    struct ib_sa_mad *mad)
1421 {
1422 	struct ib_sa_service_query *query =
1423 		container_of(sa_query, struct ib_sa_service_query, sa_query);
1424 
1425 	if (mad) {
1426 		struct ib_sa_service_rec rec;
1427 
1428 		ib_unpack(service_rec_table, ARRAY_SIZE(service_rec_table),
1429 			  mad->data, &rec);
1430 		query->callback(status, &rec, query->context);
1431 	} else
1432 		query->callback(status, NULL, query->context);
1433 }
1434 
1435 static void ib_sa_service_rec_release(struct ib_sa_query *sa_query)
1436 {
1437 	kfree(container_of(sa_query, struct ib_sa_service_query, sa_query));
1438 }
1439 
1440 /**
1441  * ib_sa_service_rec_query - Start Service Record operation
1442  * @client:SA client
1443  * @device:device to send request on
1444  * @port_num: port number to send request on
1445  * @method:SA method - should be get, set, or delete
1446  * @rec:Service Record to send in request
1447  * @comp_mask:component mask to send in request
1448  * @timeout_ms:time to wait for response
1449  * @gfp_mask:GFP mask to use for internal allocations
1450  * @callback:function called when request completes, times out or is
1451  * canceled
1452  * @context:opaque user context passed to callback
1453  * @sa_query:request context, used to cancel request
1454  *
1455  * Send a Service Record set/get/delete to the SA to register,
1456  * unregister or query a service record.
1457  * The callback function will be called when the request completes (or
1458  * fails); status is 0 for a successful response, -EINTR if the query
1459  * is canceled, -ETIMEDOUT is the query timed out, or -EIO if an error
1460  * occurred sending the query.  The resp parameter of the callback is
1461  * only valid if status is 0.
1462  *
1463  * If the return value of ib_sa_service_rec_query() is negative, it is an
1464  * error code.  Otherwise it is a request ID that can be used to cancel
1465  * the query.
1466  */
1467 int ib_sa_service_rec_query(struct ib_sa_client *client,
1468 			    struct ib_device *device, u8 port_num, u8 method,
1469 			    struct ib_sa_service_rec *rec,
1470 			    ib_sa_comp_mask comp_mask,
1471 			    int timeout_ms, gfp_t gfp_mask,
1472 			    void (*callback)(int status,
1473 					     struct ib_sa_service_rec *resp,
1474 					     void *context),
1475 			    void *context,
1476 			    struct ib_sa_query **sa_query)
1477 {
1478 	struct ib_sa_service_query *query;
1479 	struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
1480 	struct ib_sa_port   *port;
1481 	struct ib_mad_agent *agent;
1482 	struct ib_sa_mad *mad;
1483 	int ret;
1484 
1485 	if (!sa_dev)
1486 		return -ENODEV;
1487 
1488 	port  = &sa_dev->port[port_num - sa_dev->start_port];
1489 	agent = port->agent;
1490 
1491 	if (method != IB_MGMT_METHOD_GET &&
1492 	    method != IB_MGMT_METHOD_SET &&
1493 	    method != IB_SA_METHOD_DELETE)
1494 		return -EINVAL;
1495 
1496 	query = kzalloc(sizeof(*query), gfp_mask);
1497 	if (!query)
1498 		return -ENOMEM;
1499 
1500 	query->sa_query.port     = port;
1501 	ret = alloc_mad(&query->sa_query, gfp_mask);
1502 	if (ret)
1503 		goto err1;
1504 
1505 	ib_sa_client_get(client);
1506 	query->sa_query.client = client;
1507 	query->callback        = callback;
1508 	query->context         = context;
1509 
1510 	mad = query->sa_query.mad_buf->mad;
1511 	init_mad(mad, agent);
1512 
1513 	query->sa_query.callback = callback ? ib_sa_service_rec_callback : NULL;
1514 	query->sa_query.release  = ib_sa_service_rec_release;
1515 	mad->mad_hdr.method	 = method;
1516 	mad->mad_hdr.attr_id	 = cpu_to_be16(IB_SA_ATTR_SERVICE_REC);
1517 	mad->sa_hdr.comp_mask	 = comp_mask;
1518 
1519 	ib_pack(service_rec_table, ARRAY_SIZE(service_rec_table),
1520 		rec, mad->data);
1521 
1522 	*sa_query = &query->sa_query;
1523 
1524 	ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
1525 	if (ret < 0)
1526 		goto err2;
1527 
1528 	return ret;
1529 
1530 err2:
1531 	*sa_query = NULL;
1532 	ib_sa_client_put(query->sa_query.client);
1533 	free_mad(&query->sa_query);
1534 
1535 err1:
1536 	kfree(query);
1537 	return ret;
1538 }
1539 EXPORT_SYMBOL(ib_sa_service_rec_query);
1540 
1541 static void ib_sa_mcmember_rec_callback(struct ib_sa_query *sa_query,
1542 					int status,
1543 					struct ib_sa_mad *mad)
1544 {
1545 	struct ib_sa_mcmember_query *query =
1546 		container_of(sa_query, struct ib_sa_mcmember_query, sa_query);
1547 
1548 	if (mad) {
1549 		struct ib_sa_mcmember_rec rec;
1550 
1551 		ib_unpack(mcmember_rec_table, ARRAY_SIZE(mcmember_rec_table),
1552 			  mad->data, &rec);
1553 		query->callback(status, &rec, query->context);
1554 	} else
1555 		query->callback(status, NULL, query->context);
1556 }
1557 
1558 static void ib_sa_mcmember_rec_release(struct ib_sa_query *sa_query)
1559 {
1560 	kfree(container_of(sa_query, struct ib_sa_mcmember_query, sa_query));
1561 }
1562 
1563 int ib_sa_mcmember_rec_query(struct ib_sa_client *client,
1564 			     struct ib_device *device, u8 port_num,
1565 			     u8 method,
1566 			     struct ib_sa_mcmember_rec *rec,
1567 			     ib_sa_comp_mask comp_mask,
1568 			     int timeout_ms, gfp_t gfp_mask,
1569 			     void (*callback)(int status,
1570 					      struct ib_sa_mcmember_rec *resp,
1571 					      void *context),
1572 			     void *context,
1573 			     struct ib_sa_query **sa_query)
1574 {
1575 	struct ib_sa_mcmember_query *query;
1576 	struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
1577 	struct ib_sa_port   *port;
1578 	struct ib_mad_agent *agent;
1579 	struct ib_sa_mad *mad;
1580 	int ret;
1581 
1582 	if (!sa_dev)
1583 		return -ENODEV;
1584 
1585 	port  = &sa_dev->port[port_num - sa_dev->start_port];
1586 	agent = port->agent;
1587 
1588 	query = kzalloc(sizeof(*query), gfp_mask);
1589 	if (!query)
1590 		return -ENOMEM;
1591 
1592 	query->sa_query.port     = port;
1593 	ret = alloc_mad(&query->sa_query, gfp_mask);
1594 	if (ret)
1595 		goto err1;
1596 
1597 	ib_sa_client_get(client);
1598 	query->sa_query.client = client;
1599 	query->callback        = callback;
1600 	query->context         = context;
1601 
1602 	mad = query->sa_query.mad_buf->mad;
1603 	init_mad(mad, agent);
1604 
1605 	query->sa_query.callback = callback ? ib_sa_mcmember_rec_callback : NULL;
1606 	query->sa_query.release  = ib_sa_mcmember_rec_release;
1607 	mad->mad_hdr.method	 = method;
1608 	mad->mad_hdr.attr_id	 = cpu_to_be16(IB_SA_ATTR_MC_MEMBER_REC);
1609 	mad->sa_hdr.comp_mask	 = comp_mask;
1610 
1611 	ib_pack(mcmember_rec_table, ARRAY_SIZE(mcmember_rec_table),
1612 		rec, mad->data);
1613 
1614 	*sa_query = &query->sa_query;
1615 
1616 	ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
1617 	if (ret < 0)
1618 		goto err2;
1619 
1620 	return ret;
1621 
1622 err2:
1623 	*sa_query = NULL;
1624 	ib_sa_client_put(query->sa_query.client);
1625 	free_mad(&query->sa_query);
1626 
1627 err1:
1628 	kfree(query);
1629 	return ret;
1630 }
1631 
1632 /* Support GuidInfoRecord */
1633 static void ib_sa_guidinfo_rec_callback(struct ib_sa_query *sa_query,
1634 					int status,
1635 					struct ib_sa_mad *mad)
1636 {
1637 	struct ib_sa_guidinfo_query *query =
1638 		container_of(sa_query, struct ib_sa_guidinfo_query, sa_query);
1639 
1640 	if (mad) {
1641 		struct ib_sa_guidinfo_rec rec;
1642 
1643 		ib_unpack(guidinfo_rec_table, ARRAY_SIZE(guidinfo_rec_table),
1644 			  mad->data, &rec);
1645 		query->callback(status, &rec, query->context);
1646 	} else
1647 		query->callback(status, NULL, query->context);
1648 }
1649 
1650 static void ib_sa_guidinfo_rec_release(struct ib_sa_query *sa_query)
1651 {
1652 	kfree(container_of(sa_query, struct ib_sa_guidinfo_query, sa_query));
1653 }
1654 
1655 int ib_sa_guid_info_rec_query(struct ib_sa_client *client,
1656 			      struct ib_device *device, u8 port_num,
1657 			      struct ib_sa_guidinfo_rec *rec,
1658 			      ib_sa_comp_mask comp_mask, u8 method,
1659 			      int timeout_ms, gfp_t gfp_mask,
1660 			      void (*callback)(int status,
1661 					       struct ib_sa_guidinfo_rec *resp,
1662 					       void *context),
1663 			      void *context,
1664 			      struct ib_sa_query **sa_query)
1665 {
1666 	struct ib_sa_guidinfo_query *query;
1667 	struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
1668 	struct ib_sa_port *port;
1669 	struct ib_mad_agent *agent;
1670 	struct ib_sa_mad *mad;
1671 	int ret;
1672 
1673 	if (!sa_dev)
1674 		return -ENODEV;
1675 
1676 	if (method != IB_MGMT_METHOD_GET &&
1677 	    method != IB_MGMT_METHOD_SET &&
1678 	    method != IB_SA_METHOD_DELETE) {
1679 		return -EINVAL;
1680 	}
1681 
1682 	port  = &sa_dev->port[port_num - sa_dev->start_port];
1683 	agent = port->agent;
1684 
1685 	query = kzalloc(sizeof(*query), gfp_mask);
1686 	if (!query)
1687 		return -ENOMEM;
1688 
1689 	query->sa_query.port = port;
1690 	ret = alloc_mad(&query->sa_query, gfp_mask);
1691 	if (ret)
1692 		goto err1;
1693 
1694 	ib_sa_client_get(client);
1695 	query->sa_query.client = client;
1696 	query->callback        = callback;
1697 	query->context         = context;
1698 
1699 	mad = query->sa_query.mad_buf->mad;
1700 	init_mad(mad, agent);
1701 
1702 	query->sa_query.callback = callback ? ib_sa_guidinfo_rec_callback : NULL;
1703 	query->sa_query.release  = ib_sa_guidinfo_rec_release;
1704 
1705 	mad->mad_hdr.method	 = method;
1706 	mad->mad_hdr.attr_id	 = cpu_to_be16(IB_SA_ATTR_GUID_INFO_REC);
1707 	mad->sa_hdr.comp_mask	 = comp_mask;
1708 
1709 	ib_pack(guidinfo_rec_table, ARRAY_SIZE(guidinfo_rec_table), rec,
1710 		mad->data);
1711 
1712 	*sa_query = &query->sa_query;
1713 
1714 	ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
1715 	if (ret < 0)
1716 		goto err2;
1717 
1718 	return ret;
1719 
1720 err2:
1721 	*sa_query = NULL;
1722 	ib_sa_client_put(query->sa_query.client);
1723 	free_mad(&query->sa_query);
1724 
1725 err1:
1726 	kfree(query);
1727 	return ret;
1728 }
1729 EXPORT_SYMBOL(ib_sa_guid_info_rec_query);
1730 
1731 /* Support get SA ClassPortInfo */
1732 static void ib_sa_classport_info_rec_callback(struct ib_sa_query *sa_query,
1733 					      int status,
1734 					      struct ib_sa_mad *mad)
1735 {
1736 	unsigned long flags;
1737 	struct ib_sa_classport_info_query *query =
1738 		container_of(sa_query, struct ib_sa_classport_info_query, sa_query);
1739 
1740 	if (mad) {
1741 		struct ib_class_port_info rec;
1742 
1743 		ib_unpack(classport_info_rec_table,
1744 			  ARRAY_SIZE(classport_info_rec_table),
1745 			  mad->data, &rec);
1746 
1747 		spin_lock_irqsave(&sa_query->port->classport_lock, flags);
1748 		if (!status && !sa_query->port->classport_info.valid) {
1749 			memcpy(&sa_query->port->classport_info.data, &rec,
1750 			       sizeof(sa_query->port->classport_info.data));
1751 
1752 			sa_query->port->classport_info.valid = true;
1753 		}
1754 		spin_unlock_irqrestore(&sa_query->port->classport_lock, flags);
1755 
1756 		query->callback(status, &rec, query->context);
1757 	} else {
1758 		query->callback(status, NULL, query->context);
1759 	}
1760 }
1761 
1762 static void ib_sa_portclass_info_rec_release(struct ib_sa_query *sa_query)
1763 {
1764 	kfree(container_of(sa_query, struct ib_sa_classport_info_query,
1765 			   sa_query));
1766 }
1767 
1768 int ib_sa_classport_info_rec_query(struct ib_sa_client *client,
1769 				   struct ib_device *device, u8 port_num,
1770 				   int timeout_ms, gfp_t gfp_mask,
1771 				   void (*callback)(int status,
1772 						    struct ib_class_port_info *resp,
1773 						    void *context),
1774 				   void *context,
1775 				   struct ib_sa_query **sa_query)
1776 {
1777 	struct ib_sa_classport_info_query *query;
1778 	struct ib_sa_device *sa_dev = ib_get_client_data(device, &sa_client);
1779 	struct ib_sa_port *port;
1780 	struct ib_mad_agent *agent;
1781 	struct ib_sa_mad *mad;
1782 	struct ib_class_port_info cached_class_port_info;
1783 	int ret;
1784 	unsigned long flags;
1785 
1786 	if (!sa_dev)
1787 		return -ENODEV;
1788 
1789 	port  = &sa_dev->port[port_num - sa_dev->start_port];
1790 	agent = port->agent;
1791 
1792 	/* Use cached ClassPortInfo attribute if valid instead of sending mad */
1793 	spin_lock_irqsave(&port->classport_lock, flags);
1794 	if (port->classport_info.valid && callback) {
1795 		memcpy(&cached_class_port_info, &port->classport_info.data,
1796 		       sizeof(cached_class_port_info));
1797 		spin_unlock_irqrestore(&port->classport_lock, flags);
1798 		callback(0, &cached_class_port_info, context);
1799 		return 0;
1800 	}
1801 	spin_unlock_irqrestore(&port->classport_lock, flags);
1802 
1803 	query = kzalloc(sizeof(*query), gfp_mask);
1804 	if (!query)
1805 		return -ENOMEM;
1806 
1807 	query->sa_query.port = port;
1808 	ret = alloc_mad(&query->sa_query, gfp_mask);
1809 	if (ret)
1810 		goto err1;
1811 
1812 	ib_sa_client_get(client);
1813 	query->sa_query.client = client;
1814 	query->callback        = callback;
1815 	query->context         = context;
1816 
1817 	mad = query->sa_query.mad_buf->mad;
1818 	init_mad(mad, agent);
1819 
1820 	query->sa_query.callback = callback ? ib_sa_classport_info_rec_callback : NULL;
1821 
1822 	query->sa_query.release  = ib_sa_portclass_info_rec_release;
1823 	/* support GET only */
1824 	mad->mad_hdr.method	 = IB_MGMT_METHOD_GET;
1825 	mad->mad_hdr.attr_id	 = cpu_to_be16(IB_SA_ATTR_CLASS_PORTINFO);
1826 	mad->sa_hdr.comp_mask	 = 0;
1827 	*sa_query = &query->sa_query;
1828 
1829 	ret = send_mad(&query->sa_query, timeout_ms, gfp_mask);
1830 	if (ret < 0)
1831 		goto err2;
1832 
1833 	return ret;
1834 
1835 err2:
1836 	*sa_query = NULL;
1837 	ib_sa_client_put(query->sa_query.client);
1838 	free_mad(&query->sa_query);
1839 
1840 err1:
1841 	kfree(query);
1842 	return ret;
1843 }
1844 EXPORT_SYMBOL(ib_sa_classport_info_rec_query);
1845 
1846 static void send_handler(struct ib_mad_agent *agent,
1847 			 struct ib_mad_send_wc *mad_send_wc)
1848 {
1849 	struct ib_sa_query *query = mad_send_wc->send_buf->context[0];
1850 	unsigned long flags;
1851 
1852 	if (query->callback)
1853 		switch (mad_send_wc->status) {
1854 		case IB_WC_SUCCESS:
1855 			/* No callback -- already got recv */
1856 			break;
1857 		case IB_WC_RESP_TIMEOUT_ERR:
1858 			query->callback(query, -ETIMEDOUT, NULL);
1859 			break;
1860 		case IB_WC_WR_FLUSH_ERR:
1861 			query->callback(query, -EINTR, NULL);
1862 			break;
1863 		default:
1864 			query->callback(query, -EIO, NULL);
1865 			break;
1866 		}
1867 
1868 	spin_lock_irqsave(&idr_lock, flags);
1869 	idr_remove(&query_idr, query->id);
1870 	spin_unlock_irqrestore(&idr_lock, flags);
1871 
1872 	free_mad(query);
1873 	ib_sa_client_put(query->client);
1874 	query->release(query);
1875 }
1876 
1877 static void recv_handler(struct ib_mad_agent *mad_agent,
1878 			 struct ib_mad_send_buf *send_buf,
1879 			 struct ib_mad_recv_wc *mad_recv_wc)
1880 {
1881 	struct ib_sa_query *query;
1882 
1883 	if (!send_buf)
1884 		return;
1885 
1886 	query = send_buf->context[0];
1887 	if (query->callback) {
1888 		if (mad_recv_wc->wc->status == IB_WC_SUCCESS)
1889 			query->callback(query,
1890 					mad_recv_wc->recv_buf.mad->mad_hdr.status ?
1891 					-EINVAL : 0,
1892 					(struct ib_sa_mad *) mad_recv_wc->recv_buf.mad);
1893 		else
1894 			query->callback(query, -EIO, NULL);
1895 	}
1896 
1897 	ib_free_recv_mad(mad_recv_wc);
1898 }
1899 
1900 static void ib_sa_add_one(struct ib_device *device)
1901 {
1902 	struct ib_sa_device *sa_dev;
1903 	int s, e, i;
1904 	int count = 0;
1905 
1906 	s = rdma_start_port(device);
1907 	e = rdma_end_port(device);
1908 
1909 	sa_dev = kzalloc(sizeof *sa_dev +
1910 			 (e - s + 1) * sizeof (struct ib_sa_port),
1911 			 GFP_KERNEL);
1912 	if (!sa_dev)
1913 		return;
1914 
1915 	sa_dev->start_port = s;
1916 	sa_dev->end_port   = e;
1917 
1918 	for (i = 0; i <= e - s; ++i) {
1919 		spin_lock_init(&sa_dev->port[i].ah_lock);
1920 		if (!rdma_cap_ib_sa(device, i + 1))
1921 			continue;
1922 
1923 		sa_dev->port[i].sm_ah    = NULL;
1924 		sa_dev->port[i].port_num = i + s;
1925 
1926 		spin_lock_init(&sa_dev->port[i].classport_lock);
1927 		sa_dev->port[i].classport_info.valid = false;
1928 
1929 		sa_dev->port[i].agent =
1930 			ib_register_mad_agent(device, i + s, IB_QPT_GSI,
1931 					      NULL, 0, send_handler,
1932 					      recv_handler, sa_dev, 0);
1933 		if (IS_ERR(sa_dev->port[i].agent))
1934 			goto err;
1935 
1936 		INIT_WORK(&sa_dev->port[i].update_task, update_sm_ah);
1937 
1938 		count++;
1939 	}
1940 
1941 	if (!count)
1942 		goto free;
1943 
1944 	ib_set_client_data(device, &sa_client, sa_dev);
1945 
1946 	/*
1947 	 * We register our event handler after everything is set up,
1948 	 * and then update our cached info after the event handler is
1949 	 * registered to avoid any problems if a port changes state
1950 	 * during our initialization.
1951 	 */
1952 
1953 	INIT_IB_EVENT_HANDLER(&sa_dev->event_handler, device, ib_sa_event);
1954 	if (ib_register_event_handler(&sa_dev->event_handler))
1955 		goto err;
1956 
1957 	for (i = 0; i <= e - s; ++i) {
1958 		if (rdma_cap_ib_sa(device, i + 1))
1959 			update_sm_ah(&sa_dev->port[i].update_task);
1960 	}
1961 
1962 	return;
1963 
1964 err:
1965 	while (--i >= 0) {
1966 		if (rdma_cap_ib_sa(device, i + 1))
1967 			ib_unregister_mad_agent(sa_dev->port[i].agent);
1968 	}
1969 free:
1970 	kfree(sa_dev);
1971 	return;
1972 }
1973 
1974 static void ib_sa_remove_one(struct ib_device *device, void *client_data)
1975 {
1976 	struct ib_sa_device *sa_dev = client_data;
1977 	int i;
1978 
1979 	if (!sa_dev)
1980 		return;
1981 
1982 	ib_unregister_event_handler(&sa_dev->event_handler);
1983 
1984 	flush_workqueue(ib_wq);
1985 
1986 	for (i = 0; i <= sa_dev->end_port - sa_dev->start_port; ++i) {
1987 		if (rdma_cap_ib_sa(device, i + 1)) {
1988 			ib_unregister_mad_agent(sa_dev->port[i].agent);
1989 			if (sa_dev->port[i].sm_ah)
1990 				kref_put(&sa_dev->port[i].sm_ah->ref, free_sm_ah);
1991 		}
1992 
1993 	}
1994 
1995 	kfree(sa_dev);
1996 }
1997 
1998 int ib_sa_init(void)
1999 {
2000 	int ret;
2001 
2002 	get_random_bytes(&tid, sizeof tid);
2003 
2004 	atomic_set(&ib_nl_sa_request_seq, 0);
2005 
2006 	ret = ib_register_client(&sa_client);
2007 	if (ret) {
2008 		pr_err("Couldn't register ib_sa client\n");
2009 		goto err1;
2010 	}
2011 
2012 	ret = mcast_init();
2013 	if (ret) {
2014 		pr_err("Couldn't initialize multicast handling\n");
2015 		goto err2;
2016 	}
2017 
2018 	ib_nl_wq = alloc_ordered_workqueue("ib_nl_sa_wq", WQ_MEM_RECLAIM);
2019 	if (!ib_nl_wq) {
2020 		ret = -ENOMEM;
2021 		goto err3;
2022 	}
2023 
2024 	INIT_DELAYED_WORK(&ib_nl_timed_work, ib_nl_request_timeout);
2025 
2026 	return 0;
2027 
2028 err3:
2029 	mcast_cleanup();
2030 err2:
2031 	ib_unregister_client(&sa_client);
2032 err1:
2033 	return ret;
2034 }
2035 
2036 void ib_sa_cleanup(void)
2037 {
2038 	cancel_delayed_work(&ib_nl_timed_work);
2039 	flush_workqueue(ib_nl_wq);
2040 	destroy_workqueue(ib_nl_wq);
2041 	mcast_cleanup();
2042 	ib_unregister_client(&sa_client);
2043 	idr_destroy(&query_idr);
2044 }
2045