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