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