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