xref: /linux/drivers/infiniband/core/mad.c (revision de2fe5e07d58424bc286fff3fd3c1b0bf933cd58)
1 /*
2  * Copyright (c) 2004, 2005 Voltaire, Inc. All rights reserved.
3  * Copyright (c) 2005 Intel Corporation.  All rights reserved.
4  * Copyright (c) 2005 Mellanox Technologies Ltd.  All rights reserved.
5  *
6  * This software is available to you under a choice of one of two
7  * licenses.  You may choose to be licensed under the terms of the GNU
8  * General Public License (GPL) Version 2, available from the file
9  * COPYING in the main directory of this source tree, or the
10  * OpenIB.org BSD license below:
11  *
12  *     Redistribution and use in source and binary forms, with or
13  *     without modification, are permitted provided that the following
14  *     conditions are met:
15  *
16  *      - Redistributions of source code must retain the above
17  *        copyright notice, this list of conditions and the following
18  *        disclaimer.
19  *
20  *      - Redistributions in binary form must reproduce the above
21  *        copyright notice, this list of conditions and the following
22  *        disclaimer in the documentation and/or other materials
23  *        provided with the distribution.
24  *
25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32  * SOFTWARE.
33  *
34  * $Id: mad.c 5596 2006-03-03 01:00:07Z sean.hefty $
35  */
36 #include <linux/dma-mapping.h>
37 
38 #include "mad_priv.h"
39 #include "mad_rmpp.h"
40 #include "smi.h"
41 #include "agent.h"
42 
43 MODULE_LICENSE("Dual BSD/GPL");
44 MODULE_DESCRIPTION("kernel IB MAD API");
45 MODULE_AUTHOR("Hal Rosenstock");
46 MODULE_AUTHOR("Sean Hefty");
47 
48 
49 kmem_cache_t *ib_mad_cache;
50 
51 static struct list_head ib_mad_port_list;
52 static u32 ib_mad_client_id = 0;
53 
54 /* Port list lock */
55 static spinlock_t ib_mad_port_list_lock;
56 
57 
58 /* Forward declarations */
59 static int method_in_use(struct ib_mad_mgmt_method_table **method,
60 			 struct ib_mad_reg_req *mad_reg_req);
61 static void remove_mad_reg_req(struct ib_mad_agent_private *priv);
62 static struct ib_mad_agent_private *find_mad_agent(
63 					struct ib_mad_port_private *port_priv,
64 					struct ib_mad *mad);
65 static int ib_mad_post_receive_mads(struct ib_mad_qp_info *qp_info,
66 				    struct ib_mad_private *mad);
67 static void cancel_mads(struct ib_mad_agent_private *mad_agent_priv);
68 static void timeout_sends(void *data);
69 static void local_completions(void *data);
70 static int add_nonoui_reg_req(struct ib_mad_reg_req *mad_reg_req,
71 			      struct ib_mad_agent_private *agent_priv,
72 			      u8 mgmt_class);
73 static int add_oui_reg_req(struct ib_mad_reg_req *mad_reg_req,
74 			   struct ib_mad_agent_private *agent_priv);
75 
76 /*
77  * Returns a ib_mad_port_private structure or NULL for a device/port
78  * Assumes ib_mad_port_list_lock is being held
79  */
80 static inline struct ib_mad_port_private *
81 __ib_get_mad_port(struct ib_device *device, int port_num)
82 {
83 	struct ib_mad_port_private *entry;
84 
85 	list_for_each_entry(entry, &ib_mad_port_list, port_list) {
86 		if (entry->device == device && entry->port_num == port_num)
87 			return entry;
88 	}
89 	return NULL;
90 }
91 
92 /*
93  * Wrapper function to return a ib_mad_port_private structure or NULL
94  * for a device/port
95  */
96 static inline struct ib_mad_port_private *
97 ib_get_mad_port(struct ib_device *device, int port_num)
98 {
99 	struct ib_mad_port_private *entry;
100 	unsigned long flags;
101 
102 	spin_lock_irqsave(&ib_mad_port_list_lock, flags);
103 	entry = __ib_get_mad_port(device, port_num);
104 	spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
105 
106 	return entry;
107 }
108 
109 static inline u8 convert_mgmt_class(u8 mgmt_class)
110 {
111 	/* Alias IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE to 0 */
112 	return mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE ?
113 		0 : mgmt_class;
114 }
115 
116 static int get_spl_qp_index(enum ib_qp_type qp_type)
117 {
118 	switch (qp_type)
119 	{
120 	case IB_QPT_SMI:
121 		return 0;
122 	case IB_QPT_GSI:
123 		return 1;
124 	default:
125 		return -1;
126 	}
127 }
128 
129 static int vendor_class_index(u8 mgmt_class)
130 {
131 	return mgmt_class - IB_MGMT_CLASS_VENDOR_RANGE2_START;
132 }
133 
134 static int is_vendor_class(u8 mgmt_class)
135 {
136 	if ((mgmt_class < IB_MGMT_CLASS_VENDOR_RANGE2_START) ||
137 	    (mgmt_class > IB_MGMT_CLASS_VENDOR_RANGE2_END))
138 		return 0;
139 	return 1;
140 }
141 
142 static int is_vendor_oui(char *oui)
143 {
144 	if (oui[0] || oui[1] || oui[2])
145 		return 1;
146 	return 0;
147 }
148 
149 static int is_vendor_method_in_use(
150 		struct ib_mad_mgmt_vendor_class *vendor_class,
151 		struct ib_mad_reg_req *mad_reg_req)
152 {
153 	struct ib_mad_mgmt_method_table *method;
154 	int i;
155 
156 	for (i = 0; i < MAX_MGMT_OUI; i++) {
157 		if (!memcmp(vendor_class->oui[i], mad_reg_req->oui, 3)) {
158 			method = vendor_class->method_table[i];
159 			if (method) {
160 				if (method_in_use(&method, mad_reg_req))
161 					return 1;
162 				else
163 					break;
164 			}
165 		}
166 	}
167 	return 0;
168 }
169 
170 /*
171  * ib_register_mad_agent - Register to send/receive MADs
172  */
173 struct ib_mad_agent *ib_register_mad_agent(struct ib_device *device,
174 					   u8 port_num,
175 					   enum ib_qp_type qp_type,
176 					   struct ib_mad_reg_req *mad_reg_req,
177 					   u8 rmpp_version,
178 					   ib_mad_send_handler send_handler,
179 					   ib_mad_recv_handler recv_handler,
180 					   void *context)
181 {
182 	struct ib_mad_port_private *port_priv;
183 	struct ib_mad_agent *ret = ERR_PTR(-EINVAL);
184 	struct ib_mad_agent_private *mad_agent_priv;
185 	struct ib_mad_reg_req *reg_req = NULL;
186 	struct ib_mad_mgmt_class_table *class;
187 	struct ib_mad_mgmt_vendor_class_table *vendor;
188 	struct ib_mad_mgmt_vendor_class *vendor_class;
189 	struct ib_mad_mgmt_method_table *method;
190 	int ret2, qpn;
191 	unsigned long flags;
192 	u8 mgmt_class, vclass;
193 
194 	/* Validate parameters */
195 	qpn = get_spl_qp_index(qp_type);
196 	if (qpn == -1)
197 		goto error1;
198 
199 	if (rmpp_version && rmpp_version != IB_MGMT_RMPP_VERSION)
200 		goto error1;
201 
202 	/* Validate MAD registration request if supplied */
203 	if (mad_reg_req) {
204 		if (mad_reg_req->mgmt_class_version >= MAX_MGMT_VERSION)
205 			goto error1;
206 		if (!recv_handler)
207 			goto error1;
208 		if (mad_reg_req->mgmt_class >= MAX_MGMT_CLASS) {
209 			/*
210 			 * IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE is the only
211 			 * one in this range currently allowed
212 			 */
213 			if (mad_reg_req->mgmt_class !=
214 			    IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
215 				goto error1;
216 		} else if (mad_reg_req->mgmt_class == 0) {
217 			/*
218 			 * Class 0 is reserved in IBA and is used for
219 			 * aliasing of IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE
220 			 */
221 			goto error1;
222 		} else if (is_vendor_class(mad_reg_req->mgmt_class)) {
223 			/*
224 			 * If class is in "new" vendor range,
225 			 * ensure supplied OUI is not zero
226 			 */
227 			if (!is_vendor_oui(mad_reg_req->oui))
228 				goto error1;
229 		}
230 		/* Make sure class supplied is consistent with RMPP */
231 		if (ib_is_mad_class_rmpp(mad_reg_req->mgmt_class)) {
232 			if (!rmpp_version)
233 				goto error1;
234 		} else {
235 			if (rmpp_version)
236 				goto error1;
237 		}
238 		/* Make sure class supplied is consistent with QP type */
239 		if (qp_type == IB_QPT_SMI) {
240 			if ((mad_reg_req->mgmt_class !=
241 					IB_MGMT_CLASS_SUBN_LID_ROUTED) &&
242 			    (mad_reg_req->mgmt_class !=
243 					IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE))
244 				goto error1;
245 		} else {
246 			if ((mad_reg_req->mgmt_class ==
247 					IB_MGMT_CLASS_SUBN_LID_ROUTED) ||
248 			    (mad_reg_req->mgmt_class ==
249 					IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE))
250 				goto error1;
251 		}
252 	} else {
253 		/* No registration request supplied */
254 		if (!send_handler)
255 			goto error1;
256 	}
257 
258 	/* Validate device and port */
259 	port_priv = ib_get_mad_port(device, port_num);
260 	if (!port_priv) {
261 		ret = ERR_PTR(-ENODEV);
262 		goto error1;
263 	}
264 
265 	/* Allocate structures */
266 	mad_agent_priv = kzalloc(sizeof *mad_agent_priv, GFP_KERNEL);
267 	if (!mad_agent_priv) {
268 		ret = ERR_PTR(-ENOMEM);
269 		goto error1;
270 	}
271 
272 	mad_agent_priv->agent.mr = ib_get_dma_mr(port_priv->qp_info[qpn].qp->pd,
273 						 IB_ACCESS_LOCAL_WRITE);
274 	if (IS_ERR(mad_agent_priv->agent.mr)) {
275 		ret = ERR_PTR(-ENOMEM);
276 		goto error2;
277 	}
278 
279 	if (mad_reg_req) {
280 		reg_req = kmalloc(sizeof *reg_req, GFP_KERNEL);
281 		if (!reg_req) {
282 			ret = ERR_PTR(-ENOMEM);
283 			goto error3;
284 		}
285 		/* Make a copy of the MAD registration request */
286 		memcpy(reg_req, mad_reg_req, sizeof *reg_req);
287 	}
288 
289 	/* Now, fill in the various structures */
290 	mad_agent_priv->qp_info = &port_priv->qp_info[qpn];
291 	mad_agent_priv->reg_req = reg_req;
292 	mad_agent_priv->agent.rmpp_version = rmpp_version;
293 	mad_agent_priv->agent.device = device;
294 	mad_agent_priv->agent.recv_handler = recv_handler;
295 	mad_agent_priv->agent.send_handler = send_handler;
296 	mad_agent_priv->agent.context = context;
297 	mad_agent_priv->agent.qp = port_priv->qp_info[qpn].qp;
298 	mad_agent_priv->agent.port_num = port_num;
299 
300 	spin_lock_irqsave(&port_priv->reg_lock, flags);
301 	mad_agent_priv->agent.hi_tid = ++ib_mad_client_id;
302 
303 	/*
304 	 * Make sure MAD registration (if supplied)
305 	 * is non overlapping with any existing ones
306 	 */
307 	if (mad_reg_req) {
308 		mgmt_class = convert_mgmt_class(mad_reg_req->mgmt_class);
309 		if (!is_vendor_class(mgmt_class)) {
310 			class = port_priv->version[mad_reg_req->
311 						   mgmt_class_version].class;
312 			if (class) {
313 				method = class->method_table[mgmt_class];
314 				if (method) {
315 					if (method_in_use(&method,
316 							   mad_reg_req))
317 						goto error4;
318 				}
319 			}
320 			ret2 = add_nonoui_reg_req(mad_reg_req, mad_agent_priv,
321 						  mgmt_class);
322 		} else {
323 			/* "New" vendor class range */
324 			vendor = port_priv->version[mad_reg_req->
325 						    mgmt_class_version].vendor;
326 			if (vendor) {
327 				vclass = vendor_class_index(mgmt_class);
328 				vendor_class = vendor->vendor_class[vclass];
329 				if (vendor_class) {
330 					if (is_vendor_method_in_use(
331 							vendor_class,
332 							mad_reg_req))
333 						goto error4;
334 				}
335 			}
336 			ret2 = add_oui_reg_req(mad_reg_req, mad_agent_priv);
337 		}
338 		if (ret2) {
339 			ret = ERR_PTR(ret2);
340 			goto error4;
341 		}
342 	}
343 
344 	/* Add mad agent into port's agent list */
345 	list_add_tail(&mad_agent_priv->agent_list, &port_priv->agent_list);
346 	spin_unlock_irqrestore(&port_priv->reg_lock, flags);
347 
348 	spin_lock_init(&mad_agent_priv->lock);
349 	INIT_LIST_HEAD(&mad_agent_priv->send_list);
350 	INIT_LIST_HEAD(&mad_agent_priv->wait_list);
351 	INIT_LIST_HEAD(&mad_agent_priv->done_list);
352 	INIT_LIST_HEAD(&mad_agent_priv->rmpp_list);
353 	INIT_WORK(&mad_agent_priv->timed_work, timeout_sends, mad_agent_priv);
354 	INIT_LIST_HEAD(&mad_agent_priv->local_list);
355 	INIT_WORK(&mad_agent_priv->local_work, local_completions,
356 		   mad_agent_priv);
357 	atomic_set(&mad_agent_priv->refcount, 1);
358 	init_waitqueue_head(&mad_agent_priv->wait);
359 
360 	return &mad_agent_priv->agent;
361 
362 error4:
363 	spin_unlock_irqrestore(&port_priv->reg_lock, flags);
364 	kfree(reg_req);
365 error3:
366 	ib_dereg_mr(mad_agent_priv->agent.mr);
367 error2:
368 	kfree(mad_agent_priv);
369 error1:
370 	return ret;
371 }
372 EXPORT_SYMBOL(ib_register_mad_agent);
373 
374 static inline int is_snooping_sends(int mad_snoop_flags)
375 {
376 	return (mad_snoop_flags &
377 		(/*IB_MAD_SNOOP_POSTED_SENDS |
378 		 IB_MAD_SNOOP_RMPP_SENDS |*/
379 		 IB_MAD_SNOOP_SEND_COMPLETIONS /*|
380 		 IB_MAD_SNOOP_RMPP_SEND_COMPLETIONS*/));
381 }
382 
383 static inline int is_snooping_recvs(int mad_snoop_flags)
384 {
385 	return (mad_snoop_flags &
386 		(IB_MAD_SNOOP_RECVS /*|
387 		 IB_MAD_SNOOP_RMPP_RECVS*/));
388 }
389 
390 static int register_snoop_agent(struct ib_mad_qp_info *qp_info,
391 				struct ib_mad_snoop_private *mad_snoop_priv)
392 {
393 	struct ib_mad_snoop_private **new_snoop_table;
394 	unsigned long flags;
395 	int i;
396 
397 	spin_lock_irqsave(&qp_info->snoop_lock, flags);
398 	/* Check for empty slot in array. */
399 	for (i = 0; i < qp_info->snoop_table_size; i++)
400 		if (!qp_info->snoop_table[i])
401 			break;
402 
403 	if (i == qp_info->snoop_table_size) {
404 		/* Grow table. */
405 		new_snoop_table = kmalloc(sizeof mad_snoop_priv *
406 					  qp_info->snoop_table_size + 1,
407 					  GFP_ATOMIC);
408 		if (!new_snoop_table) {
409 			i = -ENOMEM;
410 			goto out;
411 		}
412 		if (qp_info->snoop_table) {
413 			memcpy(new_snoop_table, qp_info->snoop_table,
414 			       sizeof mad_snoop_priv *
415 			       qp_info->snoop_table_size);
416 			kfree(qp_info->snoop_table);
417 		}
418 		qp_info->snoop_table = new_snoop_table;
419 		qp_info->snoop_table_size++;
420 	}
421 	qp_info->snoop_table[i] = mad_snoop_priv;
422 	atomic_inc(&qp_info->snoop_count);
423 out:
424 	spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
425 	return i;
426 }
427 
428 struct ib_mad_agent *ib_register_mad_snoop(struct ib_device *device,
429 					   u8 port_num,
430 					   enum ib_qp_type qp_type,
431 					   int mad_snoop_flags,
432 					   ib_mad_snoop_handler snoop_handler,
433 					   ib_mad_recv_handler recv_handler,
434 					   void *context)
435 {
436 	struct ib_mad_port_private *port_priv;
437 	struct ib_mad_agent *ret;
438 	struct ib_mad_snoop_private *mad_snoop_priv;
439 	int qpn;
440 
441 	/* Validate parameters */
442 	if ((is_snooping_sends(mad_snoop_flags) && !snoop_handler) ||
443 	    (is_snooping_recvs(mad_snoop_flags) && !recv_handler)) {
444 		ret = ERR_PTR(-EINVAL);
445 		goto error1;
446 	}
447 	qpn = get_spl_qp_index(qp_type);
448 	if (qpn == -1) {
449 		ret = ERR_PTR(-EINVAL);
450 		goto error1;
451 	}
452 	port_priv = ib_get_mad_port(device, port_num);
453 	if (!port_priv) {
454 		ret = ERR_PTR(-ENODEV);
455 		goto error1;
456 	}
457 	/* Allocate structures */
458 	mad_snoop_priv = kzalloc(sizeof *mad_snoop_priv, GFP_KERNEL);
459 	if (!mad_snoop_priv) {
460 		ret = ERR_PTR(-ENOMEM);
461 		goto error1;
462 	}
463 
464 	/* Now, fill in the various structures */
465 	mad_snoop_priv->qp_info = &port_priv->qp_info[qpn];
466 	mad_snoop_priv->agent.device = device;
467 	mad_snoop_priv->agent.recv_handler = recv_handler;
468 	mad_snoop_priv->agent.snoop_handler = snoop_handler;
469 	mad_snoop_priv->agent.context = context;
470 	mad_snoop_priv->agent.qp = port_priv->qp_info[qpn].qp;
471 	mad_snoop_priv->agent.port_num = port_num;
472 	mad_snoop_priv->mad_snoop_flags = mad_snoop_flags;
473 	init_waitqueue_head(&mad_snoop_priv->wait);
474 	mad_snoop_priv->snoop_index = register_snoop_agent(
475 						&port_priv->qp_info[qpn],
476 						mad_snoop_priv);
477 	if (mad_snoop_priv->snoop_index < 0) {
478 		ret = ERR_PTR(mad_snoop_priv->snoop_index);
479 		goto error2;
480 	}
481 
482 	atomic_set(&mad_snoop_priv->refcount, 1);
483 	return &mad_snoop_priv->agent;
484 
485 error2:
486 	kfree(mad_snoop_priv);
487 error1:
488 	return ret;
489 }
490 EXPORT_SYMBOL(ib_register_mad_snoop);
491 
492 static void unregister_mad_agent(struct ib_mad_agent_private *mad_agent_priv)
493 {
494 	struct ib_mad_port_private *port_priv;
495 	unsigned long flags;
496 
497 	/* Note that we could still be handling received MADs */
498 
499 	/*
500 	 * Canceling all sends results in dropping received response
501 	 * MADs, preventing us from queuing additional work
502 	 */
503 	cancel_mads(mad_agent_priv);
504 	port_priv = mad_agent_priv->qp_info->port_priv;
505 	cancel_delayed_work(&mad_agent_priv->timed_work);
506 
507 	spin_lock_irqsave(&port_priv->reg_lock, flags);
508 	remove_mad_reg_req(mad_agent_priv);
509 	list_del(&mad_agent_priv->agent_list);
510 	spin_unlock_irqrestore(&port_priv->reg_lock, flags);
511 
512 	flush_workqueue(port_priv->wq);
513 	ib_cancel_rmpp_recvs(mad_agent_priv);
514 
515 	atomic_dec(&mad_agent_priv->refcount);
516 	wait_event(mad_agent_priv->wait,
517 		   !atomic_read(&mad_agent_priv->refcount));
518 
519 	kfree(mad_agent_priv->reg_req);
520 	ib_dereg_mr(mad_agent_priv->agent.mr);
521 	kfree(mad_agent_priv);
522 }
523 
524 static void unregister_mad_snoop(struct ib_mad_snoop_private *mad_snoop_priv)
525 {
526 	struct ib_mad_qp_info *qp_info;
527 	unsigned long flags;
528 
529 	qp_info = mad_snoop_priv->qp_info;
530 	spin_lock_irqsave(&qp_info->snoop_lock, flags);
531 	qp_info->snoop_table[mad_snoop_priv->snoop_index] = NULL;
532 	atomic_dec(&qp_info->snoop_count);
533 	spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
534 
535 	atomic_dec(&mad_snoop_priv->refcount);
536 	wait_event(mad_snoop_priv->wait,
537 		   !atomic_read(&mad_snoop_priv->refcount));
538 
539 	kfree(mad_snoop_priv);
540 }
541 
542 /*
543  * ib_unregister_mad_agent - Unregisters a client from using MAD services
544  */
545 int ib_unregister_mad_agent(struct ib_mad_agent *mad_agent)
546 {
547 	struct ib_mad_agent_private *mad_agent_priv;
548 	struct ib_mad_snoop_private *mad_snoop_priv;
549 
550 	/* If the TID is zero, the agent can only snoop. */
551 	if (mad_agent->hi_tid) {
552 		mad_agent_priv = container_of(mad_agent,
553 					      struct ib_mad_agent_private,
554 					      agent);
555 		unregister_mad_agent(mad_agent_priv);
556 	} else {
557 		mad_snoop_priv = container_of(mad_agent,
558 					      struct ib_mad_snoop_private,
559 					      agent);
560 		unregister_mad_snoop(mad_snoop_priv);
561 	}
562 	return 0;
563 }
564 EXPORT_SYMBOL(ib_unregister_mad_agent);
565 
566 static inline int response_mad(struct ib_mad *mad)
567 {
568 	/* Trap represses are responses although response bit is reset */
569 	return ((mad->mad_hdr.method == IB_MGMT_METHOD_TRAP_REPRESS) ||
570 		(mad->mad_hdr.method & IB_MGMT_METHOD_RESP));
571 }
572 
573 static void dequeue_mad(struct ib_mad_list_head *mad_list)
574 {
575 	struct ib_mad_queue *mad_queue;
576 	unsigned long flags;
577 
578 	BUG_ON(!mad_list->mad_queue);
579 	mad_queue = mad_list->mad_queue;
580 	spin_lock_irqsave(&mad_queue->lock, flags);
581 	list_del(&mad_list->list);
582 	mad_queue->count--;
583 	spin_unlock_irqrestore(&mad_queue->lock, flags);
584 }
585 
586 static void snoop_send(struct ib_mad_qp_info *qp_info,
587 		       struct ib_mad_send_buf *send_buf,
588 		       struct ib_mad_send_wc *mad_send_wc,
589 		       int mad_snoop_flags)
590 {
591 	struct ib_mad_snoop_private *mad_snoop_priv;
592 	unsigned long flags;
593 	int i;
594 
595 	spin_lock_irqsave(&qp_info->snoop_lock, flags);
596 	for (i = 0; i < qp_info->snoop_table_size; i++) {
597 		mad_snoop_priv = qp_info->snoop_table[i];
598 		if (!mad_snoop_priv ||
599 		    !(mad_snoop_priv->mad_snoop_flags & mad_snoop_flags))
600 			continue;
601 
602 		atomic_inc(&mad_snoop_priv->refcount);
603 		spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
604 		mad_snoop_priv->agent.snoop_handler(&mad_snoop_priv->agent,
605 						    send_buf, mad_send_wc);
606 		if (atomic_dec_and_test(&mad_snoop_priv->refcount))
607 			wake_up(&mad_snoop_priv->wait);
608 		spin_lock_irqsave(&qp_info->snoop_lock, flags);
609 	}
610 	spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
611 }
612 
613 static void snoop_recv(struct ib_mad_qp_info *qp_info,
614 		       struct ib_mad_recv_wc *mad_recv_wc,
615 		       int mad_snoop_flags)
616 {
617 	struct ib_mad_snoop_private *mad_snoop_priv;
618 	unsigned long flags;
619 	int i;
620 
621 	spin_lock_irqsave(&qp_info->snoop_lock, flags);
622 	for (i = 0; i < qp_info->snoop_table_size; i++) {
623 		mad_snoop_priv = qp_info->snoop_table[i];
624 		if (!mad_snoop_priv ||
625 		    !(mad_snoop_priv->mad_snoop_flags & mad_snoop_flags))
626 			continue;
627 
628 		atomic_inc(&mad_snoop_priv->refcount);
629 		spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
630 		mad_snoop_priv->agent.recv_handler(&mad_snoop_priv->agent,
631 						   mad_recv_wc);
632 		if (atomic_dec_and_test(&mad_snoop_priv->refcount))
633 			wake_up(&mad_snoop_priv->wait);
634 		spin_lock_irqsave(&qp_info->snoop_lock, flags);
635 	}
636 	spin_unlock_irqrestore(&qp_info->snoop_lock, flags);
637 }
638 
639 static void build_smp_wc(u64 wr_id, u16 slid, u16 pkey_index, u8 port_num,
640 			 struct ib_wc *wc)
641 {
642 	memset(wc, 0, sizeof *wc);
643 	wc->wr_id = wr_id;
644 	wc->status = IB_WC_SUCCESS;
645 	wc->opcode = IB_WC_RECV;
646 	wc->pkey_index = pkey_index;
647 	wc->byte_len = sizeof(struct ib_mad) + sizeof(struct ib_grh);
648 	wc->src_qp = IB_QP0;
649 	wc->qp_num = IB_QP0;
650 	wc->slid = slid;
651 	wc->sl = 0;
652 	wc->dlid_path_bits = 0;
653 	wc->port_num = port_num;
654 }
655 
656 /*
657  * Return 0 if SMP is to be sent
658  * Return 1 if SMP was consumed locally (whether or not solicited)
659  * Return < 0 if error
660  */
661 static int handle_outgoing_dr_smp(struct ib_mad_agent_private *mad_agent_priv,
662 				  struct ib_mad_send_wr_private *mad_send_wr)
663 {
664 	int ret;
665 	struct ib_smp *smp = mad_send_wr->send_buf.mad;
666 	unsigned long flags;
667 	struct ib_mad_local_private *local;
668 	struct ib_mad_private *mad_priv;
669 	struct ib_mad_port_private *port_priv;
670 	struct ib_mad_agent_private *recv_mad_agent = NULL;
671 	struct ib_device *device = mad_agent_priv->agent.device;
672 	u8 port_num = mad_agent_priv->agent.port_num;
673 	struct ib_wc mad_wc;
674 	struct ib_send_wr *send_wr = &mad_send_wr->send_wr;
675 
676 	/*
677 	 * Directed route handling starts if the initial LID routed part of
678 	 * a request or the ending LID routed part of a response is empty.
679 	 * If we are at the start of the LID routed part, don't update the
680 	 * hop_ptr or hop_cnt.  See section 14.2.2, Vol 1 IB spec.
681 	 */
682 	if ((ib_get_smp_direction(smp) ? smp->dr_dlid : smp->dr_slid) ==
683 	     IB_LID_PERMISSIVE &&
684 	    !smi_handle_dr_smp_send(smp, device->node_type, port_num)) {
685 		ret = -EINVAL;
686 		printk(KERN_ERR PFX "Invalid directed route\n");
687 		goto out;
688 	}
689 	/* Check to post send on QP or process locally */
690 	ret = smi_check_local_smp(smp, device);
691 	if (!ret)
692 		goto out;
693 
694 	local = kmalloc(sizeof *local, GFP_ATOMIC);
695 	if (!local) {
696 		ret = -ENOMEM;
697 		printk(KERN_ERR PFX "No memory for ib_mad_local_private\n");
698 		goto out;
699 	}
700 	local->mad_priv = NULL;
701 	local->recv_mad_agent = NULL;
702 	mad_priv = kmem_cache_alloc(ib_mad_cache, GFP_ATOMIC);
703 	if (!mad_priv) {
704 		ret = -ENOMEM;
705 		printk(KERN_ERR PFX "No memory for local response MAD\n");
706 		kfree(local);
707 		goto out;
708 	}
709 
710 	build_smp_wc(send_wr->wr_id, be16_to_cpu(smp->dr_slid),
711 		     send_wr->wr.ud.pkey_index,
712 		     send_wr->wr.ud.port_num, &mad_wc);
713 
714 	/* No GRH for DR SMP */
715 	ret = device->process_mad(device, 0, port_num, &mad_wc, NULL,
716 				  (struct ib_mad *)smp,
717 				  (struct ib_mad *)&mad_priv->mad);
718 	switch (ret)
719 	{
720 	case IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY:
721 		if (response_mad(&mad_priv->mad.mad) &&
722 		    mad_agent_priv->agent.recv_handler) {
723 			local->mad_priv = mad_priv;
724 			local->recv_mad_agent = mad_agent_priv;
725 			/*
726 			 * Reference MAD agent until receive
727 			 * side of local completion handled
728 			 */
729 			atomic_inc(&mad_agent_priv->refcount);
730 		} else
731 			kmem_cache_free(ib_mad_cache, mad_priv);
732 		break;
733 	case IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_CONSUMED:
734 		kmem_cache_free(ib_mad_cache, mad_priv);
735 		break;
736 	case IB_MAD_RESULT_SUCCESS:
737 		/* Treat like an incoming receive MAD */
738 		port_priv = ib_get_mad_port(mad_agent_priv->agent.device,
739 					    mad_agent_priv->agent.port_num);
740 		if (port_priv) {
741 			mad_priv->mad.mad.mad_hdr.tid =
742 				((struct ib_mad *)smp)->mad_hdr.tid;
743 			recv_mad_agent = find_mad_agent(port_priv,
744 						        &mad_priv->mad.mad);
745 		}
746 		if (!port_priv || !recv_mad_agent) {
747 			kmem_cache_free(ib_mad_cache, mad_priv);
748 			kfree(local);
749 			ret = 0;
750 			goto out;
751 		}
752 		local->mad_priv = mad_priv;
753 		local->recv_mad_agent = recv_mad_agent;
754 		break;
755 	default:
756 		kmem_cache_free(ib_mad_cache, mad_priv);
757 		kfree(local);
758 		ret = -EINVAL;
759 		goto out;
760 	}
761 
762 	local->mad_send_wr = mad_send_wr;
763 	/* Reference MAD agent until send side of local completion handled */
764 	atomic_inc(&mad_agent_priv->refcount);
765 	/* Queue local completion to local list */
766 	spin_lock_irqsave(&mad_agent_priv->lock, flags);
767 	list_add_tail(&local->completion_list, &mad_agent_priv->local_list);
768 	spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
769 	queue_work(mad_agent_priv->qp_info->port_priv->wq,
770 		   &mad_agent_priv->local_work);
771 	ret = 1;
772 out:
773 	return ret;
774 }
775 
776 static int get_pad_size(int hdr_len, int data_len)
777 {
778 	int seg_size, pad;
779 
780 	seg_size = sizeof(struct ib_mad) - hdr_len;
781 	if (data_len && seg_size) {
782 		pad = seg_size - data_len % seg_size;
783 		return pad == seg_size ? 0 : pad;
784 	} else
785 		return seg_size;
786 }
787 
788 static void free_send_rmpp_list(struct ib_mad_send_wr_private *mad_send_wr)
789 {
790 	struct ib_rmpp_segment *s, *t;
791 
792 	list_for_each_entry_safe(s, t, &mad_send_wr->rmpp_list, list) {
793 		list_del(&s->list);
794 		kfree(s);
795 	}
796 }
797 
798 static int alloc_send_rmpp_list(struct ib_mad_send_wr_private *send_wr,
799 				gfp_t gfp_mask)
800 {
801 	struct ib_mad_send_buf *send_buf = &send_wr->send_buf;
802 	struct ib_rmpp_mad *rmpp_mad = send_buf->mad;
803 	struct ib_rmpp_segment *seg = NULL;
804 	int left, seg_size, pad;
805 
806 	send_buf->seg_size = sizeof (struct ib_mad) - send_buf->hdr_len;
807 	seg_size = send_buf->seg_size;
808 	pad = send_wr->pad;
809 
810 	/* Allocate data segments. */
811 	for (left = send_buf->data_len + pad; left > 0; left -= seg_size) {
812 		seg = kmalloc(sizeof (*seg) + seg_size, gfp_mask);
813 		if (!seg) {
814 			printk(KERN_ERR "alloc_send_rmpp_segs: RMPP mem "
815 			       "alloc failed for len %zd, gfp %#x\n",
816 			       sizeof (*seg) + seg_size, gfp_mask);
817 			free_send_rmpp_list(send_wr);
818 			return -ENOMEM;
819 		}
820 		seg->num = ++send_buf->seg_count;
821 		list_add_tail(&seg->list, &send_wr->rmpp_list);
822 	}
823 
824 	/* Zero any padding */
825 	if (pad)
826 		memset(seg->data + seg_size - pad, 0, pad);
827 
828 	rmpp_mad->rmpp_hdr.rmpp_version = send_wr->mad_agent_priv->
829 					  agent.rmpp_version;
830 	rmpp_mad->rmpp_hdr.rmpp_type = IB_MGMT_RMPP_TYPE_DATA;
831 	ib_set_rmpp_flags(&rmpp_mad->rmpp_hdr, IB_MGMT_RMPP_FLAG_ACTIVE);
832 
833 	send_wr->cur_seg = container_of(send_wr->rmpp_list.next,
834 					struct ib_rmpp_segment, list);
835 	send_wr->last_ack_seg = send_wr->cur_seg;
836 	return 0;
837 }
838 
839 struct ib_mad_send_buf * ib_create_send_mad(struct ib_mad_agent *mad_agent,
840 					    u32 remote_qpn, u16 pkey_index,
841 					    int rmpp_active,
842 					    int hdr_len, int data_len,
843 					    gfp_t gfp_mask)
844 {
845 	struct ib_mad_agent_private *mad_agent_priv;
846 	struct ib_mad_send_wr_private *mad_send_wr;
847 	int pad, message_size, ret, size;
848 	void *buf;
849 
850 	mad_agent_priv = container_of(mad_agent, struct ib_mad_agent_private,
851 				      agent);
852 	pad = get_pad_size(hdr_len, data_len);
853 	message_size = hdr_len + data_len + pad;
854 
855 	if ((!mad_agent->rmpp_version &&
856 	     (rmpp_active || message_size > sizeof(struct ib_mad))) ||
857 	    (!rmpp_active && message_size > sizeof(struct ib_mad)))
858 		return ERR_PTR(-EINVAL);
859 
860 	size = rmpp_active ? hdr_len : sizeof(struct ib_mad);
861 	buf = kzalloc(sizeof *mad_send_wr + size, gfp_mask);
862 	if (!buf)
863 		return ERR_PTR(-ENOMEM);
864 
865 	mad_send_wr = buf + size;
866 	INIT_LIST_HEAD(&mad_send_wr->rmpp_list);
867 	mad_send_wr->send_buf.mad = buf;
868 	mad_send_wr->send_buf.hdr_len = hdr_len;
869 	mad_send_wr->send_buf.data_len = data_len;
870 	mad_send_wr->pad = pad;
871 
872 	mad_send_wr->mad_agent_priv = mad_agent_priv;
873 	mad_send_wr->sg_list[0].length = hdr_len;
874 	mad_send_wr->sg_list[0].lkey = mad_agent->mr->lkey;
875 	mad_send_wr->sg_list[1].length = sizeof(struct ib_mad) - hdr_len;
876 	mad_send_wr->sg_list[1].lkey = mad_agent->mr->lkey;
877 
878 	mad_send_wr->send_wr.wr_id = (unsigned long) mad_send_wr;
879 	mad_send_wr->send_wr.sg_list = mad_send_wr->sg_list;
880 	mad_send_wr->send_wr.num_sge = 2;
881 	mad_send_wr->send_wr.opcode = IB_WR_SEND;
882 	mad_send_wr->send_wr.send_flags = IB_SEND_SIGNALED;
883 	mad_send_wr->send_wr.wr.ud.remote_qpn = remote_qpn;
884 	mad_send_wr->send_wr.wr.ud.remote_qkey = IB_QP_SET_QKEY;
885 	mad_send_wr->send_wr.wr.ud.pkey_index = pkey_index;
886 
887 	if (rmpp_active) {
888 		ret = alloc_send_rmpp_list(mad_send_wr, gfp_mask);
889 		if (ret) {
890 			kfree(buf);
891 			return ERR_PTR(ret);
892 		}
893 	}
894 
895 	mad_send_wr->send_buf.mad_agent = mad_agent;
896 	atomic_inc(&mad_agent_priv->refcount);
897 	return &mad_send_wr->send_buf;
898 }
899 EXPORT_SYMBOL(ib_create_send_mad);
900 
901 int ib_get_mad_data_offset(u8 mgmt_class)
902 {
903 	if (mgmt_class == IB_MGMT_CLASS_SUBN_ADM)
904 		return IB_MGMT_SA_HDR;
905 	else if ((mgmt_class == IB_MGMT_CLASS_DEVICE_MGMT) ||
906 		 (mgmt_class == IB_MGMT_CLASS_DEVICE_ADM) ||
907 		 (mgmt_class == IB_MGMT_CLASS_BIS))
908 		return IB_MGMT_DEVICE_HDR;
909 	else if ((mgmt_class >= IB_MGMT_CLASS_VENDOR_RANGE2_START) &&
910 		 (mgmt_class <= IB_MGMT_CLASS_VENDOR_RANGE2_END))
911 		return IB_MGMT_VENDOR_HDR;
912 	else
913 		return IB_MGMT_MAD_HDR;
914 }
915 EXPORT_SYMBOL(ib_get_mad_data_offset);
916 
917 int ib_is_mad_class_rmpp(u8 mgmt_class)
918 {
919 	if ((mgmt_class == IB_MGMT_CLASS_SUBN_ADM) ||
920 	    (mgmt_class == IB_MGMT_CLASS_DEVICE_MGMT) ||
921 	    (mgmt_class == IB_MGMT_CLASS_DEVICE_ADM) ||
922 	    (mgmt_class == IB_MGMT_CLASS_BIS) ||
923 	    ((mgmt_class >= IB_MGMT_CLASS_VENDOR_RANGE2_START) &&
924 	     (mgmt_class <= IB_MGMT_CLASS_VENDOR_RANGE2_END)))
925 		return 1;
926 	return 0;
927 }
928 EXPORT_SYMBOL(ib_is_mad_class_rmpp);
929 
930 void *ib_get_rmpp_segment(struct ib_mad_send_buf *send_buf, int seg_num)
931 {
932 	struct ib_mad_send_wr_private *mad_send_wr;
933 	struct list_head *list;
934 
935 	mad_send_wr = container_of(send_buf, struct ib_mad_send_wr_private,
936 				   send_buf);
937 	list = &mad_send_wr->cur_seg->list;
938 
939 	if (mad_send_wr->cur_seg->num < seg_num) {
940 		list_for_each_entry(mad_send_wr->cur_seg, list, list)
941 			if (mad_send_wr->cur_seg->num == seg_num)
942 				break;
943 	} else if (mad_send_wr->cur_seg->num > seg_num) {
944 		list_for_each_entry_reverse(mad_send_wr->cur_seg, list, list)
945 			if (mad_send_wr->cur_seg->num == seg_num)
946 				break;
947 	}
948 	return mad_send_wr->cur_seg->data;
949 }
950 EXPORT_SYMBOL(ib_get_rmpp_segment);
951 
952 static inline void *ib_get_payload(struct ib_mad_send_wr_private *mad_send_wr)
953 {
954 	if (mad_send_wr->send_buf.seg_count)
955 		return ib_get_rmpp_segment(&mad_send_wr->send_buf,
956 					   mad_send_wr->seg_num);
957 	else
958 		return mad_send_wr->send_buf.mad +
959 		       mad_send_wr->send_buf.hdr_len;
960 }
961 
962 void ib_free_send_mad(struct ib_mad_send_buf *send_buf)
963 {
964 	struct ib_mad_agent_private *mad_agent_priv;
965 	struct ib_mad_send_wr_private *mad_send_wr;
966 
967 	mad_agent_priv = container_of(send_buf->mad_agent,
968 				      struct ib_mad_agent_private, agent);
969 	mad_send_wr = container_of(send_buf, struct ib_mad_send_wr_private,
970 				   send_buf);
971 
972 	free_send_rmpp_list(mad_send_wr);
973 	kfree(send_buf->mad);
974 	if (atomic_dec_and_test(&mad_agent_priv->refcount))
975 		wake_up(&mad_agent_priv->wait);
976 }
977 EXPORT_SYMBOL(ib_free_send_mad);
978 
979 int ib_send_mad(struct ib_mad_send_wr_private *mad_send_wr)
980 {
981 	struct ib_mad_qp_info *qp_info;
982 	struct list_head *list;
983 	struct ib_send_wr *bad_send_wr;
984 	struct ib_mad_agent *mad_agent;
985 	struct ib_sge *sge;
986 	unsigned long flags;
987 	int ret;
988 
989 	/* Set WR ID to find mad_send_wr upon completion */
990 	qp_info = mad_send_wr->mad_agent_priv->qp_info;
991 	mad_send_wr->send_wr.wr_id = (unsigned long)&mad_send_wr->mad_list;
992 	mad_send_wr->mad_list.mad_queue = &qp_info->send_queue;
993 
994 	mad_agent = mad_send_wr->send_buf.mad_agent;
995 	sge = mad_send_wr->sg_list;
996 	sge[0].addr = dma_map_single(mad_agent->device->dma_device,
997 				     mad_send_wr->send_buf.mad,
998 				     sge[0].length,
999 				     DMA_TO_DEVICE);
1000 	pci_unmap_addr_set(mad_send_wr, header_mapping, sge[0].addr);
1001 
1002 	sge[1].addr = dma_map_single(mad_agent->device->dma_device,
1003 				     ib_get_payload(mad_send_wr),
1004 				     sge[1].length,
1005 				     DMA_TO_DEVICE);
1006 	pci_unmap_addr_set(mad_send_wr, payload_mapping, sge[1].addr);
1007 
1008 	spin_lock_irqsave(&qp_info->send_queue.lock, flags);
1009 	if (qp_info->send_queue.count < qp_info->send_queue.max_active) {
1010 		ret = ib_post_send(mad_agent->qp, &mad_send_wr->send_wr,
1011 				   &bad_send_wr);
1012 		list = &qp_info->send_queue.list;
1013 	} else {
1014 		ret = 0;
1015 		list = &qp_info->overflow_list;
1016 	}
1017 
1018 	if (!ret) {
1019 		qp_info->send_queue.count++;
1020 		list_add_tail(&mad_send_wr->mad_list.list, list);
1021 	}
1022 	spin_unlock_irqrestore(&qp_info->send_queue.lock, flags);
1023 	if (ret) {
1024 		dma_unmap_single(mad_agent->device->dma_device,
1025 				 pci_unmap_addr(mad_send_wr, header_mapping),
1026 				 sge[0].length, DMA_TO_DEVICE);
1027 		dma_unmap_single(mad_agent->device->dma_device,
1028 				 pci_unmap_addr(mad_send_wr, payload_mapping),
1029 				 sge[1].length, DMA_TO_DEVICE);
1030 	}
1031 	return ret;
1032 }
1033 
1034 /*
1035  * ib_post_send_mad - Posts MAD(s) to the send queue of the QP associated
1036  *  with the registered client
1037  */
1038 int ib_post_send_mad(struct ib_mad_send_buf *send_buf,
1039 		     struct ib_mad_send_buf **bad_send_buf)
1040 {
1041 	struct ib_mad_agent_private *mad_agent_priv;
1042 	struct ib_mad_send_buf *next_send_buf;
1043 	struct ib_mad_send_wr_private *mad_send_wr;
1044 	unsigned long flags;
1045 	int ret = -EINVAL;
1046 
1047 	/* Walk list of send WRs and post each on send list */
1048 	for (; send_buf; send_buf = next_send_buf) {
1049 
1050 		mad_send_wr = container_of(send_buf,
1051 					   struct ib_mad_send_wr_private,
1052 					   send_buf);
1053 		mad_agent_priv = mad_send_wr->mad_agent_priv;
1054 
1055 		if (!send_buf->mad_agent->send_handler ||
1056 		    (send_buf->timeout_ms &&
1057 		     !send_buf->mad_agent->recv_handler)) {
1058 			ret = -EINVAL;
1059 			goto error;
1060 		}
1061 
1062 		if (!ib_is_mad_class_rmpp(((struct ib_mad_hdr *) send_buf->mad)->mgmt_class)) {
1063 			if (mad_agent_priv->agent.rmpp_version) {
1064 				ret = -EINVAL;
1065 				goto error;
1066 			}
1067 		}
1068 
1069 		/*
1070 		 * Save pointer to next work request to post in case the
1071 		 * current one completes, and the user modifies the work
1072 		 * request associated with the completion
1073 		 */
1074 		next_send_buf = send_buf->next;
1075 		mad_send_wr->send_wr.wr.ud.ah = send_buf->ah;
1076 
1077 		if (((struct ib_mad_hdr *) send_buf->mad)->mgmt_class ==
1078 		    IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
1079 			ret = handle_outgoing_dr_smp(mad_agent_priv,
1080 						     mad_send_wr);
1081 			if (ret < 0)		/* error */
1082 				goto error;
1083 			else if (ret == 1)	/* locally consumed */
1084 				continue;
1085 		}
1086 
1087 		mad_send_wr->tid = ((struct ib_mad_hdr *) send_buf->mad)->tid;
1088 		/* Timeout will be updated after send completes */
1089 		mad_send_wr->timeout = msecs_to_jiffies(send_buf->timeout_ms);
1090 		mad_send_wr->retries = send_buf->retries;
1091 		/* Reference for work request to QP + response */
1092 		mad_send_wr->refcount = 1 + (mad_send_wr->timeout > 0);
1093 		mad_send_wr->status = IB_WC_SUCCESS;
1094 
1095 		/* Reference MAD agent until send completes */
1096 		atomic_inc(&mad_agent_priv->refcount);
1097 		spin_lock_irqsave(&mad_agent_priv->lock, flags);
1098 		list_add_tail(&mad_send_wr->agent_list,
1099 			      &mad_agent_priv->send_list);
1100 		spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1101 
1102 		if (mad_agent_priv->agent.rmpp_version) {
1103 			ret = ib_send_rmpp_mad(mad_send_wr);
1104 			if (ret >= 0 && ret != IB_RMPP_RESULT_CONSUMED)
1105 				ret = ib_send_mad(mad_send_wr);
1106 		} else
1107 			ret = ib_send_mad(mad_send_wr);
1108 		if (ret < 0) {
1109 			/* Fail send request */
1110 			spin_lock_irqsave(&mad_agent_priv->lock, flags);
1111 			list_del(&mad_send_wr->agent_list);
1112 			spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1113 			atomic_dec(&mad_agent_priv->refcount);
1114 			goto error;
1115 		}
1116 	}
1117 	return 0;
1118 error:
1119 	if (bad_send_buf)
1120 		*bad_send_buf = send_buf;
1121 	return ret;
1122 }
1123 EXPORT_SYMBOL(ib_post_send_mad);
1124 
1125 /*
1126  * ib_free_recv_mad - Returns data buffers used to receive
1127  *  a MAD to the access layer
1128  */
1129 void ib_free_recv_mad(struct ib_mad_recv_wc *mad_recv_wc)
1130 {
1131 	struct ib_mad_recv_buf *mad_recv_buf, *temp_recv_buf;
1132 	struct ib_mad_private_header *mad_priv_hdr;
1133 	struct ib_mad_private *priv;
1134 	struct list_head free_list;
1135 
1136 	INIT_LIST_HEAD(&free_list);
1137 	list_splice_init(&mad_recv_wc->rmpp_list, &free_list);
1138 
1139 	list_for_each_entry_safe(mad_recv_buf, temp_recv_buf,
1140 					&free_list, list) {
1141 		mad_recv_wc = container_of(mad_recv_buf, struct ib_mad_recv_wc,
1142 					   recv_buf);
1143 		mad_priv_hdr = container_of(mad_recv_wc,
1144 					    struct ib_mad_private_header,
1145 					    recv_wc);
1146 		priv = container_of(mad_priv_hdr, struct ib_mad_private,
1147 				    header);
1148 		kmem_cache_free(ib_mad_cache, priv);
1149 	}
1150 }
1151 EXPORT_SYMBOL(ib_free_recv_mad);
1152 
1153 struct ib_mad_agent *ib_redirect_mad_qp(struct ib_qp *qp,
1154 					u8 rmpp_version,
1155 					ib_mad_send_handler send_handler,
1156 					ib_mad_recv_handler recv_handler,
1157 					void *context)
1158 {
1159 	return ERR_PTR(-EINVAL);	/* XXX: for now */
1160 }
1161 EXPORT_SYMBOL(ib_redirect_mad_qp);
1162 
1163 int ib_process_mad_wc(struct ib_mad_agent *mad_agent,
1164 		      struct ib_wc *wc)
1165 {
1166 	printk(KERN_ERR PFX "ib_process_mad_wc() not implemented yet\n");
1167 	return 0;
1168 }
1169 EXPORT_SYMBOL(ib_process_mad_wc);
1170 
1171 static int method_in_use(struct ib_mad_mgmt_method_table **method,
1172 			 struct ib_mad_reg_req *mad_reg_req)
1173 {
1174 	int i;
1175 
1176 	for (i = find_first_bit(mad_reg_req->method_mask, IB_MGMT_MAX_METHODS);
1177 	     i < IB_MGMT_MAX_METHODS;
1178 	     i = find_next_bit(mad_reg_req->method_mask, IB_MGMT_MAX_METHODS,
1179 			       1+i)) {
1180 		if ((*method)->agent[i]) {
1181 			printk(KERN_ERR PFX "Method %d already in use\n", i);
1182 			return -EINVAL;
1183 		}
1184 	}
1185 	return 0;
1186 }
1187 
1188 static int allocate_method_table(struct ib_mad_mgmt_method_table **method)
1189 {
1190 	/* Allocate management method table */
1191 	*method = kzalloc(sizeof **method, GFP_ATOMIC);
1192 	if (!*method) {
1193 		printk(KERN_ERR PFX "No memory for "
1194 		       "ib_mad_mgmt_method_table\n");
1195 		return -ENOMEM;
1196 	}
1197 
1198 	return 0;
1199 }
1200 
1201 /*
1202  * Check to see if there are any methods still in use
1203  */
1204 static int check_method_table(struct ib_mad_mgmt_method_table *method)
1205 {
1206 	int i;
1207 
1208 	for (i = 0; i < IB_MGMT_MAX_METHODS; i++)
1209 		if (method->agent[i])
1210 			return 1;
1211 	return 0;
1212 }
1213 
1214 /*
1215  * Check to see if there are any method tables for this class still in use
1216  */
1217 static int check_class_table(struct ib_mad_mgmt_class_table *class)
1218 {
1219 	int i;
1220 
1221 	for (i = 0; i < MAX_MGMT_CLASS; i++)
1222 		if (class->method_table[i])
1223 			return 1;
1224 	return 0;
1225 }
1226 
1227 static int check_vendor_class(struct ib_mad_mgmt_vendor_class *vendor_class)
1228 {
1229 	int i;
1230 
1231 	for (i = 0; i < MAX_MGMT_OUI; i++)
1232 		if (vendor_class->method_table[i])
1233 			return 1;
1234 	return 0;
1235 }
1236 
1237 static int find_vendor_oui(struct ib_mad_mgmt_vendor_class *vendor_class,
1238 			   char *oui)
1239 {
1240 	int i;
1241 
1242 	for (i = 0; i < MAX_MGMT_OUI; i++)
1243                 /* Is there matching OUI for this vendor class ? */
1244                 if (!memcmp(vendor_class->oui[i], oui, 3))
1245 			return i;
1246 
1247 	return -1;
1248 }
1249 
1250 static int check_vendor_table(struct ib_mad_mgmt_vendor_class_table *vendor)
1251 {
1252 	int i;
1253 
1254 	for (i = 0; i < MAX_MGMT_VENDOR_RANGE2; i++)
1255 		if (vendor->vendor_class[i])
1256 			return 1;
1257 
1258 	return 0;
1259 }
1260 
1261 static void remove_methods_mad_agent(struct ib_mad_mgmt_method_table *method,
1262 				     struct ib_mad_agent_private *agent)
1263 {
1264 	int i;
1265 
1266 	/* Remove any methods for this mad agent */
1267 	for (i = 0; i < IB_MGMT_MAX_METHODS; i++) {
1268 		if (method->agent[i] == agent) {
1269 			method->agent[i] = NULL;
1270 		}
1271 	}
1272 }
1273 
1274 static int add_nonoui_reg_req(struct ib_mad_reg_req *mad_reg_req,
1275 			      struct ib_mad_agent_private *agent_priv,
1276 			      u8 mgmt_class)
1277 {
1278 	struct ib_mad_port_private *port_priv;
1279 	struct ib_mad_mgmt_class_table **class;
1280 	struct ib_mad_mgmt_method_table **method;
1281 	int i, ret;
1282 
1283 	port_priv = agent_priv->qp_info->port_priv;
1284 	class = &port_priv->version[mad_reg_req->mgmt_class_version].class;
1285 	if (!*class) {
1286 		/* Allocate management class table for "new" class version */
1287 		*class = kzalloc(sizeof **class, GFP_ATOMIC);
1288 		if (!*class) {
1289 			printk(KERN_ERR PFX "No memory for "
1290 			       "ib_mad_mgmt_class_table\n");
1291 			ret = -ENOMEM;
1292 			goto error1;
1293 		}
1294 
1295 		/* Allocate method table for this management class */
1296 		method = &(*class)->method_table[mgmt_class];
1297 		if ((ret = allocate_method_table(method)))
1298 			goto error2;
1299 	} else {
1300 		method = &(*class)->method_table[mgmt_class];
1301 		if (!*method) {
1302 			/* Allocate method table for this management class */
1303 			if ((ret = allocate_method_table(method)))
1304 				goto error1;
1305 		}
1306 	}
1307 
1308 	/* Now, make sure methods are not already in use */
1309 	if (method_in_use(method, mad_reg_req))
1310 		goto error3;
1311 
1312 	/* Finally, add in methods being registered */
1313 	for (i = find_first_bit(mad_reg_req->method_mask,
1314 				IB_MGMT_MAX_METHODS);
1315 	     i < IB_MGMT_MAX_METHODS;
1316 	     i = find_next_bit(mad_reg_req->method_mask, IB_MGMT_MAX_METHODS,
1317 			       1+i)) {
1318 		(*method)->agent[i] = agent_priv;
1319 	}
1320 	return 0;
1321 
1322 error3:
1323 	/* Remove any methods for this mad agent */
1324 	remove_methods_mad_agent(*method, agent_priv);
1325 	/* Now, check to see if there are any methods in use */
1326 	if (!check_method_table(*method)) {
1327 		/* If not, release management method table */
1328 		kfree(*method);
1329 		*method = NULL;
1330 	}
1331 	ret = -EINVAL;
1332 	goto error1;
1333 error2:
1334 	kfree(*class);
1335 	*class = NULL;
1336 error1:
1337 	return ret;
1338 }
1339 
1340 static int add_oui_reg_req(struct ib_mad_reg_req *mad_reg_req,
1341 			   struct ib_mad_agent_private *agent_priv)
1342 {
1343 	struct ib_mad_port_private *port_priv;
1344 	struct ib_mad_mgmt_vendor_class_table **vendor_table;
1345 	struct ib_mad_mgmt_vendor_class_table *vendor = NULL;
1346 	struct ib_mad_mgmt_vendor_class *vendor_class = NULL;
1347 	struct ib_mad_mgmt_method_table **method;
1348 	int i, ret = -ENOMEM;
1349 	u8 vclass;
1350 
1351 	/* "New" vendor (with OUI) class */
1352 	vclass = vendor_class_index(mad_reg_req->mgmt_class);
1353 	port_priv = agent_priv->qp_info->port_priv;
1354 	vendor_table = &port_priv->version[
1355 				mad_reg_req->mgmt_class_version].vendor;
1356 	if (!*vendor_table) {
1357 		/* Allocate mgmt vendor class table for "new" class version */
1358 		vendor = kzalloc(sizeof *vendor, GFP_ATOMIC);
1359 		if (!vendor) {
1360 			printk(KERN_ERR PFX "No memory for "
1361 			       "ib_mad_mgmt_vendor_class_table\n");
1362 			goto error1;
1363 		}
1364 
1365 		*vendor_table = vendor;
1366 	}
1367 	if (!(*vendor_table)->vendor_class[vclass]) {
1368 		/* Allocate table for this management vendor class */
1369 		vendor_class = kzalloc(sizeof *vendor_class, GFP_ATOMIC);
1370 		if (!vendor_class) {
1371 			printk(KERN_ERR PFX "No memory for "
1372 			       "ib_mad_mgmt_vendor_class\n");
1373 			goto error2;
1374 		}
1375 
1376 		(*vendor_table)->vendor_class[vclass] = vendor_class;
1377 	}
1378 	for (i = 0; i < MAX_MGMT_OUI; i++) {
1379 		/* Is there matching OUI for this vendor class ? */
1380 		if (!memcmp((*vendor_table)->vendor_class[vclass]->oui[i],
1381 			    mad_reg_req->oui, 3)) {
1382 			method = &(*vendor_table)->vendor_class[
1383 						vclass]->method_table[i];
1384 			BUG_ON(!*method);
1385 			goto check_in_use;
1386 		}
1387 	}
1388 	for (i = 0; i < MAX_MGMT_OUI; i++) {
1389 		/* OUI slot available ? */
1390 		if (!is_vendor_oui((*vendor_table)->vendor_class[
1391 				vclass]->oui[i])) {
1392 			method = &(*vendor_table)->vendor_class[
1393 				vclass]->method_table[i];
1394 			BUG_ON(*method);
1395 			/* Allocate method table for this OUI */
1396 			if ((ret = allocate_method_table(method)))
1397 				goto error3;
1398 			memcpy((*vendor_table)->vendor_class[vclass]->oui[i],
1399 			       mad_reg_req->oui, 3);
1400 			goto check_in_use;
1401 		}
1402 	}
1403 	printk(KERN_ERR PFX "All OUI slots in use\n");
1404 	goto error3;
1405 
1406 check_in_use:
1407 	/* Now, make sure methods are not already in use */
1408 	if (method_in_use(method, mad_reg_req))
1409 		goto error4;
1410 
1411 	/* Finally, add in methods being registered */
1412 	for (i = find_first_bit(mad_reg_req->method_mask,
1413 				IB_MGMT_MAX_METHODS);
1414 	     i < IB_MGMT_MAX_METHODS;
1415 	     i = find_next_bit(mad_reg_req->method_mask, IB_MGMT_MAX_METHODS,
1416 			       1+i)) {
1417 		(*method)->agent[i] = agent_priv;
1418 	}
1419 	return 0;
1420 
1421 error4:
1422 	/* Remove any methods for this mad agent */
1423 	remove_methods_mad_agent(*method, agent_priv);
1424 	/* Now, check to see if there are any methods in use */
1425 	if (!check_method_table(*method)) {
1426 		/* If not, release management method table */
1427 		kfree(*method);
1428 		*method = NULL;
1429 	}
1430 	ret = -EINVAL;
1431 error3:
1432 	if (vendor_class) {
1433 		(*vendor_table)->vendor_class[vclass] = NULL;
1434 		kfree(vendor_class);
1435 	}
1436 error2:
1437 	if (vendor) {
1438 		*vendor_table = NULL;
1439 		kfree(vendor);
1440 	}
1441 error1:
1442 	return ret;
1443 }
1444 
1445 static void remove_mad_reg_req(struct ib_mad_agent_private *agent_priv)
1446 {
1447 	struct ib_mad_port_private *port_priv;
1448 	struct ib_mad_mgmt_class_table *class;
1449 	struct ib_mad_mgmt_method_table *method;
1450 	struct ib_mad_mgmt_vendor_class_table *vendor;
1451 	struct ib_mad_mgmt_vendor_class *vendor_class;
1452 	int index;
1453 	u8 mgmt_class;
1454 
1455 	/*
1456 	 * Was MAD registration request supplied
1457 	 * with original registration ?
1458 	 */
1459 	if (!agent_priv->reg_req) {
1460 		goto out;
1461 	}
1462 
1463 	port_priv = agent_priv->qp_info->port_priv;
1464 	mgmt_class = convert_mgmt_class(agent_priv->reg_req->mgmt_class);
1465 	class = port_priv->version[
1466 			agent_priv->reg_req->mgmt_class_version].class;
1467 	if (!class)
1468 		goto vendor_check;
1469 
1470 	method = class->method_table[mgmt_class];
1471 	if (method) {
1472 		/* Remove any methods for this mad agent */
1473 		remove_methods_mad_agent(method, agent_priv);
1474 		/* Now, check to see if there are any methods still in use */
1475 		if (!check_method_table(method)) {
1476 			/* If not, release management method table */
1477 			 kfree(method);
1478 			 class->method_table[mgmt_class] = NULL;
1479 			 /* Any management classes left ? */
1480 			if (!check_class_table(class)) {
1481 				/* If not, release management class table */
1482 				kfree(class);
1483 				port_priv->version[
1484 					agent_priv->reg_req->
1485 					mgmt_class_version].class = NULL;
1486 			}
1487 		}
1488 	}
1489 
1490 vendor_check:
1491 	if (!is_vendor_class(mgmt_class))
1492 		goto out;
1493 
1494 	/* normalize mgmt_class to vendor range 2 */
1495 	mgmt_class = vendor_class_index(agent_priv->reg_req->mgmt_class);
1496 	vendor = port_priv->version[
1497 			agent_priv->reg_req->mgmt_class_version].vendor;
1498 
1499 	if (!vendor)
1500 		goto out;
1501 
1502 	vendor_class = vendor->vendor_class[mgmt_class];
1503 	if (vendor_class) {
1504 		index = find_vendor_oui(vendor_class, agent_priv->reg_req->oui);
1505 		if (index < 0)
1506 			goto out;
1507 		method = vendor_class->method_table[index];
1508 		if (method) {
1509 			/* Remove any methods for this mad agent */
1510 			remove_methods_mad_agent(method, agent_priv);
1511 			/*
1512 			 * Now, check to see if there are
1513 			 * any methods still in use
1514 			 */
1515 			if (!check_method_table(method)) {
1516 				/* If not, release management method table */
1517 				kfree(method);
1518 				vendor_class->method_table[index] = NULL;
1519 				memset(vendor_class->oui[index], 0, 3);
1520 				/* Any OUIs left ? */
1521 				if (!check_vendor_class(vendor_class)) {
1522 					/* If not, release vendor class table */
1523 					kfree(vendor_class);
1524 					vendor->vendor_class[mgmt_class] = NULL;
1525 					/* Any other vendor classes left ? */
1526 					if (!check_vendor_table(vendor)) {
1527 						kfree(vendor);
1528 						port_priv->version[
1529 							agent_priv->reg_req->
1530 							mgmt_class_version].
1531 							vendor = NULL;
1532 					}
1533 				}
1534 			}
1535 		}
1536 	}
1537 
1538 out:
1539 	return;
1540 }
1541 
1542 static struct ib_mad_agent_private *
1543 find_mad_agent(struct ib_mad_port_private *port_priv,
1544 	       struct ib_mad *mad)
1545 {
1546 	struct ib_mad_agent_private *mad_agent = NULL;
1547 	unsigned long flags;
1548 
1549 	spin_lock_irqsave(&port_priv->reg_lock, flags);
1550 	if (response_mad(mad)) {
1551 		u32 hi_tid;
1552 		struct ib_mad_agent_private *entry;
1553 
1554 		/*
1555 		 * Routing is based on high 32 bits of transaction ID
1556 		 * of MAD.
1557 		 */
1558 		hi_tid = be64_to_cpu(mad->mad_hdr.tid) >> 32;
1559 		list_for_each_entry(entry, &port_priv->agent_list, agent_list) {
1560 			if (entry->agent.hi_tid == hi_tid) {
1561 				mad_agent = entry;
1562 				break;
1563 			}
1564 		}
1565 	} else {
1566 		struct ib_mad_mgmt_class_table *class;
1567 		struct ib_mad_mgmt_method_table *method;
1568 		struct ib_mad_mgmt_vendor_class_table *vendor;
1569 		struct ib_mad_mgmt_vendor_class *vendor_class;
1570 		struct ib_vendor_mad *vendor_mad;
1571 		int index;
1572 
1573 		/*
1574 		 * Routing is based on version, class, and method
1575 		 * For "newer" vendor MADs, also based on OUI
1576 		 */
1577 		if (mad->mad_hdr.class_version >= MAX_MGMT_VERSION)
1578 			goto out;
1579 		if (!is_vendor_class(mad->mad_hdr.mgmt_class)) {
1580 			class = port_priv->version[
1581 					mad->mad_hdr.class_version].class;
1582 			if (!class)
1583 				goto out;
1584 			method = class->method_table[convert_mgmt_class(
1585 							mad->mad_hdr.mgmt_class)];
1586 			if (method)
1587 				mad_agent = method->agent[mad->mad_hdr.method &
1588 							  ~IB_MGMT_METHOD_RESP];
1589 		} else {
1590 			vendor = port_priv->version[
1591 					mad->mad_hdr.class_version].vendor;
1592 			if (!vendor)
1593 				goto out;
1594 			vendor_class = vendor->vendor_class[vendor_class_index(
1595 						mad->mad_hdr.mgmt_class)];
1596 			if (!vendor_class)
1597 				goto out;
1598 			/* Find matching OUI */
1599 			vendor_mad = (struct ib_vendor_mad *)mad;
1600 			index = find_vendor_oui(vendor_class, vendor_mad->oui);
1601 			if (index == -1)
1602 				goto out;
1603 			method = vendor_class->method_table[index];
1604 			if (method) {
1605 				mad_agent = method->agent[mad->mad_hdr.method &
1606 							  ~IB_MGMT_METHOD_RESP];
1607 			}
1608 		}
1609 	}
1610 
1611 	if (mad_agent) {
1612 		if (mad_agent->agent.recv_handler)
1613 			atomic_inc(&mad_agent->refcount);
1614 		else {
1615 			printk(KERN_NOTICE PFX "No receive handler for client "
1616 			       "%p on port %d\n",
1617 			       &mad_agent->agent, port_priv->port_num);
1618 			mad_agent = NULL;
1619 		}
1620 	}
1621 out:
1622 	spin_unlock_irqrestore(&port_priv->reg_lock, flags);
1623 
1624 	return mad_agent;
1625 }
1626 
1627 static int validate_mad(struct ib_mad *mad, u32 qp_num)
1628 {
1629 	int valid = 0;
1630 
1631 	/* Make sure MAD base version is understood */
1632 	if (mad->mad_hdr.base_version != IB_MGMT_BASE_VERSION) {
1633 		printk(KERN_ERR PFX "MAD received with unsupported base "
1634 		       "version %d\n", mad->mad_hdr.base_version);
1635 		goto out;
1636 	}
1637 
1638 	/* Filter SMI packets sent to other than QP0 */
1639 	if ((mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_LID_ROUTED) ||
1640 	    (mad->mad_hdr.mgmt_class == IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)) {
1641 		if (qp_num == 0)
1642 			valid = 1;
1643 	} else {
1644 		/* Filter GSI packets sent to QP0 */
1645 		if (qp_num != 0)
1646 			valid = 1;
1647 	}
1648 
1649 out:
1650 	return valid;
1651 }
1652 
1653 static int is_data_mad(struct ib_mad_agent_private *mad_agent_priv,
1654 		       struct ib_mad_hdr *mad_hdr)
1655 {
1656 	struct ib_rmpp_mad *rmpp_mad;
1657 
1658 	rmpp_mad = (struct ib_rmpp_mad *)mad_hdr;
1659 	return !mad_agent_priv->agent.rmpp_version ||
1660 		!(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
1661 				    IB_MGMT_RMPP_FLAG_ACTIVE) ||
1662 		(rmpp_mad->rmpp_hdr.rmpp_type == IB_MGMT_RMPP_TYPE_DATA);
1663 }
1664 
1665 static inline int rcv_has_same_class(struct ib_mad_send_wr_private *wr,
1666 				     struct ib_mad_recv_wc *rwc)
1667 {
1668 	return ((struct ib_mad *)(wr->send_buf.mad))->mad_hdr.mgmt_class ==
1669 		rwc->recv_buf.mad->mad_hdr.mgmt_class;
1670 }
1671 
1672 static inline int rcv_has_same_gid(struct ib_mad_send_wr_private *wr,
1673 				   struct ib_mad_recv_wc *rwc )
1674 {
1675 	struct ib_ah_attr attr;
1676 	u8 send_resp, rcv_resp;
1677 
1678 	send_resp = ((struct ib_mad *)(wr->send_buf.mad))->
1679 		     mad_hdr.method & IB_MGMT_METHOD_RESP;
1680 	rcv_resp = rwc->recv_buf.mad->mad_hdr.method & IB_MGMT_METHOD_RESP;
1681 
1682 	if (!send_resp && rcv_resp)
1683 		/* is request/response. GID/LIDs are both local (same). */
1684 		return 1;
1685 
1686 	if (send_resp == rcv_resp)
1687 		/* both requests, or both responses. GIDs different */
1688 		return 0;
1689 
1690 	if (ib_query_ah(wr->send_buf.ah, &attr))
1691 		/* Assume not equal, to avoid false positives. */
1692 		return 0;
1693 
1694 	if (!(attr.ah_flags & IB_AH_GRH) && !(rwc->wc->wc_flags & IB_WC_GRH))
1695 		return attr.dlid == rwc->wc->slid;
1696 	else if ((attr.ah_flags & IB_AH_GRH) &&
1697 		 (rwc->wc->wc_flags & IB_WC_GRH))
1698 		return memcmp(attr.grh.dgid.raw,
1699 			      rwc->recv_buf.grh->sgid.raw, 16) == 0;
1700 	else
1701 		/* one has GID, other does not.  Assume different */
1702 		return 0;
1703 }
1704 struct ib_mad_send_wr_private*
1705 ib_find_send_mad(struct ib_mad_agent_private *mad_agent_priv,
1706 		 struct ib_mad_recv_wc *mad_recv_wc)
1707 {
1708 	struct ib_mad_send_wr_private *mad_send_wr;
1709 	struct ib_mad *mad;
1710 
1711 	mad = (struct ib_mad *)mad_recv_wc->recv_buf.mad;
1712 
1713 	list_for_each_entry(mad_send_wr, &mad_agent_priv->wait_list,
1714 			    agent_list) {
1715 		if ((mad_send_wr->tid == mad->mad_hdr.tid) &&
1716 		    rcv_has_same_class(mad_send_wr, mad_recv_wc) &&
1717 		    rcv_has_same_gid(mad_send_wr, mad_recv_wc))
1718 			return mad_send_wr;
1719 	}
1720 
1721 	/*
1722 	 * It's possible to receive the response before we've
1723 	 * been notified that the send has completed
1724 	 */
1725 	list_for_each_entry(mad_send_wr, &mad_agent_priv->send_list,
1726 			    agent_list) {
1727 		if (is_data_mad(mad_agent_priv, mad_send_wr->send_buf.mad) &&
1728 		    mad_send_wr->tid == mad->mad_hdr.tid &&
1729 		    mad_send_wr->timeout &&
1730 		    rcv_has_same_class(mad_send_wr, mad_recv_wc) &&
1731 		    rcv_has_same_gid(mad_send_wr, mad_recv_wc)) {
1732 			/* Verify request has not been canceled */
1733 			return (mad_send_wr->status == IB_WC_SUCCESS) ?
1734 				mad_send_wr : NULL;
1735 		}
1736 	}
1737 	return NULL;
1738 }
1739 
1740 void ib_mark_mad_done(struct ib_mad_send_wr_private *mad_send_wr)
1741 {
1742 	mad_send_wr->timeout = 0;
1743 	if (mad_send_wr->refcount == 1) {
1744 		list_del(&mad_send_wr->agent_list);
1745 		list_add_tail(&mad_send_wr->agent_list,
1746 			      &mad_send_wr->mad_agent_priv->done_list);
1747 	}
1748 }
1749 
1750 static void ib_mad_complete_recv(struct ib_mad_agent_private *mad_agent_priv,
1751 				 struct ib_mad_recv_wc *mad_recv_wc)
1752 {
1753 	struct ib_mad_send_wr_private *mad_send_wr;
1754 	struct ib_mad_send_wc mad_send_wc;
1755 	unsigned long flags;
1756 
1757 	INIT_LIST_HEAD(&mad_recv_wc->rmpp_list);
1758 	list_add(&mad_recv_wc->recv_buf.list, &mad_recv_wc->rmpp_list);
1759 	if (mad_agent_priv->agent.rmpp_version) {
1760 		mad_recv_wc = ib_process_rmpp_recv_wc(mad_agent_priv,
1761 						      mad_recv_wc);
1762 		if (!mad_recv_wc) {
1763 			if (atomic_dec_and_test(&mad_agent_priv->refcount))
1764 				wake_up(&mad_agent_priv->wait);
1765 			return;
1766 		}
1767 	}
1768 
1769 	/* Complete corresponding request */
1770 	if (response_mad(mad_recv_wc->recv_buf.mad)) {
1771 		spin_lock_irqsave(&mad_agent_priv->lock, flags);
1772 		mad_send_wr = ib_find_send_mad(mad_agent_priv, mad_recv_wc);
1773 		if (!mad_send_wr) {
1774 			spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1775 			ib_free_recv_mad(mad_recv_wc);
1776 			if (atomic_dec_and_test(&mad_agent_priv->refcount))
1777 				wake_up(&mad_agent_priv->wait);
1778 			return;
1779 		}
1780 		ib_mark_mad_done(mad_send_wr);
1781 		spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
1782 
1783 		/* Defined behavior is to complete response before request */
1784 		mad_recv_wc->wc->wr_id = (unsigned long) &mad_send_wr->send_buf;
1785 		mad_agent_priv->agent.recv_handler(&mad_agent_priv->agent,
1786 						   mad_recv_wc);
1787 		atomic_dec(&mad_agent_priv->refcount);
1788 
1789 		mad_send_wc.status = IB_WC_SUCCESS;
1790 		mad_send_wc.vendor_err = 0;
1791 		mad_send_wc.send_buf = &mad_send_wr->send_buf;
1792 		ib_mad_complete_send_wr(mad_send_wr, &mad_send_wc);
1793 	} else {
1794 		mad_agent_priv->agent.recv_handler(&mad_agent_priv->agent,
1795 						   mad_recv_wc);
1796 		if (atomic_dec_and_test(&mad_agent_priv->refcount))
1797 			wake_up(&mad_agent_priv->wait);
1798 	}
1799 }
1800 
1801 static void ib_mad_recv_done_handler(struct ib_mad_port_private *port_priv,
1802 				     struct ib_wc *wc)
1803 {
1804 	struct ib_mad_qp_info *qp_info;
1805 	struct ib_mad_private_header *mad_priv_hdr;
1806 	struct ib_mad_private *recv, *response;
1807 	struct ib_mad_list_head *mad_list;
1808 	struct ib_mad_agent_private *mad_agent;
1809 
1810 	response = kmem_cache_alloc(ib_mad_cache, GFP_KERNEL);
1811 	if (!response)
1812 		printk(KERN_ERR PFX "ib_mad_recv_done_handler no memory "
1813 		       "for response buffer\n");
1814 
1815 	mad_list = (struct ib_mad_list_head *)(unsigned long)wc->wr_id;
1816 	qp_info = mad_list->mad_queue->qp_info;
1817 	dequeue_mad(mad_list);
1818 
1819 	mad_priv_hdr = container_of(mad_list, struct ib_mad_private_header,
1820 				    mad_list);
1821 	recv = container_of(mad_priv_hdr, struct ib_mad_private, header);
1822 	dma_unmap_single(port_priv->device->dma_device,
1823 			 pci_unmap_addr(&recv->header, mapping),
1824 			 sizeof(struct ib_mad_private) -
1825 			 sizeof(struct ib_mad_private_header),
1826 			 DMA_FROM_DEVICE);
1827 
1828 	/* Setup MAD receive work completion from "normal" work completion */
1829 	recv->header.wc = *wc;
1830 	recv->header.recv_wc.wc = &recv->header.wc;
1831 	recv->header.recv_wc.mad_len = sizeof(struct ib_mad);
1832 	recv->header.recv_wc.recv_buf.mad = &recv->mad.mad;
1833 	recv->header.recv_wc.recv_buf.grh = &recv->grh;
1834 
1835 	if (atomic_read(&qp_info->snoop_count))
1836 		snoop_recv(qp_info, &recv->header.recv_wc, IB_MAD_SNOOP_RECVS);
1837 
1838 	/* Validate MAD */
1839 	if (!validate_mad(&recv->mad.mad, qp_info->qp->qp_num))
1840 		goto out;
1841 
1842 	if (recv->mad.mad.mad_hdr.mgmt_class ==
1843 	    IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
1844 		if (!smi_handle_dr_smp_recv(&recv->mad.smp,
1845 					    port_priv->device->node_type,
1846 					    port_priv->port_num,
1847 					    port_priv->device->phys_port_cnt))
1848 			goto out;
1849 		if (!smi_check_forward_dr_smp(&recv->mad.smp))
1850 			goto local;
1851 		if (!smi_handle_dr_smp_send(&recv->mad.smp,
1852 					    port_priv->device->node_type,
1853 					    port_priv->port_num))
1854 			goto out;
1855 		if (!smi_check_local_smp(&recv->mad.smp, port_priv->device))
1856 			goto out;
1857 	}
1858 
1859 local:
1860 	/* Give driver "right of first refusal" on incoming MAD */
1861 	if (port_priv->device->process_mad) {
1862 		int ret;
1863 
1864 		if (!response) {
1865 			printk(KERN_ERR PFX "No memory for response MAD\n");
1866 			/*
1867 			 * Is it better to assume that
1868 			 * it wouldn't be processed ?
1869 			 */
1870 			goto out;
1871 		}
1872 
1873 		ret = port_priv->device->process_mad(port_priv->device, 0,
1874 						     port_priv->port_num,
1875 						     wc, &recv->grh,
1876 						     &recv->mad.mad,
1877 						     &response->mad.mad);
1878 		if (ret & IB_MAD_RESULT_SUCCESS) {
1879 			if (ret & IB_MAD_RESULT_CONSUMED)
1880 				goto out;
1881 			if (ret & IB_MAD_RESULT_REPLY) {
1882 				agent_send_response(&response->mad.mad,
1883 						    &recv->grh, wc,
1884 						    port_priv->device,
1885 						    port_priv->port_num,
1886 						    qp_info->qp->qp_num);
1887 				goto out;
1888 			}
1889 		}
1890 	}
1891 
1892 	mad_agent = find_mad_agent(port_priv, &recv->mad.mad);
1893 	if (mad_agent) {
1894 		ib_mad_complete_recv(mad_agent, &recv->header.recv_wc);
1895 		/*
1896 		 * recv is freed up in error cases in ib_mad_complete_recv
1897 		 * or via recv_handler in ib_mad_complete_recv()
1898 		 */
1899 		recv = NULL;
1900 	}
1901 
1902 out:
1903 	/* Post another receive request for this QP */
1904 	if (response) {
1905 		ib_mad_post_receive_mads(qp_info, response);
1906 		if (recv)
1907 			kmem_cache_free(ib_mad_cache, recv);
1908 	} else
1909 		ib_mad_post_receive_mads(qp_info, recv);
1910 }
1911 
1912 static void adjust_timeout(struct ib_mad_agent_private *mad_agent_priv)
1913 {
1914 	struct ib_mad_send_wr_private *mad_send_wr;
1915 	unsigned long delay;
1916 
1917 	if (list_empty(&mad_agent_priv->wait_list)) {
1918 		cancel_delayed_work(&mad_agent_priv->timed_work);
1919 	} else {
1920 		mad_send_wr = list_entry(mad_agent_priv->wait_list.next,
1921 					 struct ib_mad_send_wr_private,
1922 					 agent_list);
1923 
1924 		if (time_after(mad_agent_priv->timeout,
1925 			       mad_send_wr->timeout)) {
1926 			mad_agent_priv->timeout = mad_send_wr->timeout;
1927 			cancel_delayed_work(&mad_agent_priv->timed_work);
1928 			delay = mad_send_wr->timeout - jiffies;
1929 			if ((long)delay <= 0)
1930 				delay = 1;
1931 			queue_delayed_work(mad_agent_priv->qp_info->
1932 					   port_priv->wq,
1933 					   &mad_agent_priv->timed_work, delay);
1934 		}
1935 	}
1936 }
1937 
1938 static void wait_for_response(struct ib_mad_send_wr_private *mad_send_wr)
1939 {
1940 	struct ib_mad_agent_private *mad_agent_priv;
1941 	struct ib_mad_send_wr_private *temp_mad_send_wr;
1942 	struct list_head *list_item;
1943 	unsigned long delay;
1944 
1945 	mad_agent_priv = mad_send_wr->mad_agent_priv;
1946 	list_del(&mad_send_wr->agent_list);
1947 
1948 	delay = mad_send_wr->timeout;
1949 	mad_send_wr->timeout += jiffies;
1950 
1951 	if (delay) {
1952 		list_for_each_prev(list_item, &mad_agent_priv->wait_list) {
1953 			temp_mad_send_wr = list_entry(list_item,
1954 						struct ib_mad_send_wr_private,
1955 						agent_list);
1956 			if (time_after(mad_send_wr->timeout,
1957 				       temp_mad_send_wr->timeout))
1958 				break;
1959 		}
1960 	}
1961 	else
1962 		list_item = &mad_agent_priv->wait_list;
1963 	list_add(&mad_send_wr->agent_list, list_item);
1964 
1965 	/* Reschedule a work item if we have a shorter timeout */
1966 	if (mad_agent_priv->wait_list.next == &mad_send_wr->agent_list) {
1967 		cancel_delayed_work(&mad_agent_priv->timed_work);
1968 		queue_delayed_work(mad_agent_priv->qp_info->port_priv->wq,
1969 				   &mad_agent_priv->timed_work, delay);
1970 	}
1971 }
1972 
1973 void ib_reset_mad_timeout(struct ib_mad_send_wr_private *mad_send_wr,
1974 			  int timeout_ms)
1975 {
1976 	mad_send_wr->timeout = msecs_to_jiffies(timeout_ms);
1977 	wait_for_response(mad_send_wr);
1978 }
1979 
1980 /*
1981  * Process a send work completion
1982  */
1983 void ib_mad_complete_send_wr(struct ib_mad_send_wr_private *mad_send_wr,
1984 			     struct ib_mad_send_wc *mad_send_wc)
1985 {
1986 	struct ib_mad_agent_private	*mad_agent_priv;
1987 	unsigned long			flags;
1988 	int				ret;
1989 
1990 	mad_agent_priv = mad_send_wr->mad_agent_priv;
1991 	spin_lock_irqsave(&mad_agent_priv->lock, flags);
1992 	if (mad_agent_priv->agent.rmpp_version) {
1993 		ret = ib_process_rmpp_send_wc(mad_send_wr, mad_send_wc);
1994 		if (ret == IB_RMPP_RESULT_CONSUMED)
1995 			goto done;
1996 	} else
1997 		ret = IB_RMPP_RESULT_UNHANDLED;
1998 
1999 	if (mad_send_wc->status != IB_WC_SUCCESS &&
2000 	    mad_send_wr->status == IB_WC_SUCCESS) {
2001 		mad_send_wr->status = mad_send_wc->status;
2002 		mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
2003 	}
2004 
2005 	if (--mad_send_wr->refcount > 0) {
2006 		if (mad_send_wr->refcount == 1 && mad_send_wr->timeout &&
2007 		    mad_send_wr->status == IB_WC_SUCCESS) {
2008 			wait_for_response(mad_send_wr);
2009 		}
2010 		goto done;
2011 	}
2012 
2013 	/* Remove send from MAD agent and notify client of completion */
2014 	list_del(&mad_send_wr->agent_list);
2015 	adjust_timeout(mad_agent_priv);
2016 	spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2017 
2018 	if (mad_send_wr->status != IB_WC_SUCCESS )
2019 		mad_send_wc->status = mad_send_wr->status;
2020 	if (ret == IB_RMPP_RESULT_INTERNAL)
2021 		ib_rmpp_send_handler(mad_send_wc);
2022 	else
2023 		mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2024 						   mad_send_wc);
2025 
2026 	/* Release reference on agent taken when sending */
2027 	if (atomic_dec_and_test(&mad_agent_priv->refcount))
2028 		wake_up(&mad_agent_priv->wait);
2029 	return;
2030 done:
2031 	spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2032 }
2033 
2034 static void ib_mad_send_done_handler(struct ib_mad_port_private *port_priv,
2035 				     struct ib_wc *wc)
2036 {
2037 	struct ib_mad_send_wr_private	*mad_send_wr, *queued_send_wr;
2038 	struct ib_mad_list_head		*mad_list;
2039 	struct ib_mad_qp_info		*qp_info;
2040 	struct ib_mad_queue		*send_queue;
2041 	struct ib_send_wr		*bad_send_wr;
2042 	struct ib_mad_send_wc		mad_send_wc;
2043 	unsigned long flags;
2044 	int ret;
2045 
2046 	mad_list = (struct ib_mad_list_head *)(unsigned long)wc->wr_id;
2047 	mad_send_wr = container_of(mad_list, struct ib_mad_send_wr_private,
2048 				   mad_list);
2049 	send_queue = mad_list->mad_queue;
2050 	qp_info = send_queue->qp_info;
2051 
2052 retry:
2053 	dma_unmap_single(mad_send_wr->send_buf.mad_agent->device->dma_device,
2054 			 pci_unmap_addr(mad_send_wr, header_mapping),
2055 			 mad_send_wr->sg_list[0].length, DMA_TO_DEVICE);
2056 	dma_unmap_single(mad_send_wr->send_buf.mad_agent->device->dma_device,
2057 			 pci_unmap_addr(mad_send_wr, payload_mapping),
2058 			 mad_send_wr->sg_list[1].length, DMA_TO_DEVICE);
2059 	queued_send_wr = NULL;
2060 	spin_lock_irqsave(&send_queue->lock, flags);
2061 	list_del(&mad_list->list);
2062 
2063 	/* Move queued send to the send queue */
2064 	if (send_queue->count-- > send_queue->max_active) {
2065 		mad_list = container_of(qp_info->overflow_list.next,
2066 					struct ib_mad_list_head, list);
2067 		queued_send_wr = container_of(mad_list,
2068 					struct ib_mad_send_wr_private,
2069 					mad_list);
2070 		list_del(&mad_list->list);
2071 		list_add_tail(&mad_list->list, &send_queue->list);
2072 	}
2073 	spin_unlock_irqrestore(&send_queue->lock, flags);
2074 
2075 	mad_send_wc.send_buf = &mad_send_wr->send_buf;
2076 	mad_send_wc.status = wc->status;
2077 	mad_send_wc.vendor_err = wc->vendor_err;
2078 	if (atomic_read(&qp_info->snoop_count))
2079 		snoop_send(qp_info, &mad_send_wr->send_buf, &mad_send_wc,
2080 			   IB_MAD_SNOOP_SEND_COMPLETIONS);
2081 	ib_mad_complete_send_wr(mad_send_wr, &mad_send_wc);
2082 
2083 	if (queued_send_wr) {
2084 		ret = ib_post_send(qp_info->qp, &queued_send_wr->send_wr,
2085 				   &bad_send_wr);
2086 		if (ret) {
2087 			printk(KERN_ERR PFX "ib_post_send failed: %d\n", ret);
2088 			mad_send_wr = queued_send_wr;
2089 			wc->status = IB_WC_LOC_QP_OP_ERR;
2090 			goto retry;
2091 		}
2092 	}
2093 }
2094 
2095 static void mark_sends_for_retry(struct ib_mad_qp_info *qp_info)
2096 {
2097 	struct ib_mad_send_wr_private *mad_send_wr;
2098 	struct ib_mad_list_head *mad_list;
2099 	unsigned long flags;
2100 
2101 	spin_lock_irqsave(&qp_info->send_queue.lock, flags);
2102 	list_for_each_entry(mad_list, &qp_info->send_queue.list, list) {
2103 		mad_send_wr = container_of(mad_list,
2104 					   struct ib_mad_send_wr_private,
2105 					   mad_list);
2106 		mad_send_wr->retry = 1;
2107 	}
2108 	spin_unlock_irqrestore(&qp_info->send_queue.lock, flags);
2109 }
2110 
2111 static void mad_error_handler(struct ib_mad_port_private *port_priv,
2112 			      struct ib_wc *wc)
2113 {
2114 	struct ib_mad_list_head *mad_list;
2115 	struct ib_mad_qp_info *qp_info;
2116 	struct ib_mad_send_wr_private *mad_send_wr;
2117 	int ret;
2118 
2119 	/* Determine if failure was a send or receive */
2120 	mad_list = (struct ib_mad_list_head *)(unsigned long)wc->wr_id;
2121 	qp_info = mad_list->mad_queue->qp_info;
2122 	if (mad_list->mad_queue == &qp_info->recv_queue)
2123 		/*
2124 		 * Receive errors indicate that the QP has entered the error
2125 		 * state - error handling/shutdown code will cleanup
2126 		 */
2127 		return;
2128 
2129 	/*
2130 	 * Send errors will transition the QP to SQE - move
2131 	 * QP to RTS and repost flushed work requests
2132 	 */
2133 	mad_send_wr = container_of(mad_list, struct ib_mad_send_wr_private,
2134 				   mad_list);
2135 	if (wc->status == IB_WC_WR_FLUSH_ERR) {
2136 		if (mad_send_wr->retry) {
2137 			/* Repost send */
2138 			struct ib_send_wr *bad_send_wr;
2139 
2140 			mad_send_wr->retry = 0;
2141 			ret = ib_post_send(qp_info->qp, &mad_send_wr->send_wr,
2142 					&bad_send_wr);
2143 			if (ret)
2144 				ib_mad_send_done_handler(port_priv, wc);
2145 		} else
2146 			ib_mad_send_done_handler(port_priv, wc);
2147 	} else {
2148 		struct ib_qp_attr *attr;
2149 
2150 		/* Transition QP to RTS and fail offending send */
2151 		attr = kmalloc(sizeof *attr, GFP_KERNEL);
2152 		if (attr) {
2153 			attr->qp_state = IB_QPS_RTS;
2154 			attr->cur_qp_state = IB_QPS_SQE;
2155 			ret = ib_modify_qp(qp_info->qp, attr,
2156 					   IB_QP_STATE | IB_QP_CUR_STATE);
2157 			kfree(attr);
2158 			if (ret)
2159 				printk(KERN_ERR PFX "mad_error_handler - "
2160 				       "ib_modify_qp to RTS : %d\n", ret);
2161 			else
2162 				mark_sends_for_retry(qp_info);
2163 		}
2164 		ib_mad_send_done_handler(port_priv, wc);
2165 	}
2166 }
2167 
2168 /*
2169  * IB MAD completion callback
2170  */
2171 static void ib_mad_completion_handler(void *data)
2172 {
2173 	struct ib_mad_port_private *port_priv;
2174 	struct ib_wc wc;
2175 
2176 	port_priv = (struct ib_mad_port_private *)data;
2177 	ib_req_notify_cq(port_priv->cq, IB_CQ_NEXT_COMP);
2178 
2179 	while (ib_poll_cq(port_priv->cq, 1, &wc) == 1) {
2180 		if (wc.status == IB_WC_SUCCESS) {
2181 			switch (wc.opcode) {
2182 			case IB_WC_SEND:
2183 				ib_mad_send_done_handler(port_priv, &wc);
2184 				break;
2185 			case IB_WC_RECV:
2186 				ib_mad_recv_done_handler(port_priv, &wc);
2187 				break;
2188 			default:
2189 				BUG_ON(1);
2190 				break;
2191 			}
2192 		} else
2193 			mad_error_handler(port_priv, &wc);
2194 	}
2195 }
2196 
2197 static void cancel_mads(struct ib_mad_agent_private *mad_agent_priv)
2198 {
2199 	unsigned long flags;
2200 	struct ib_mad_send_wr_private *mad_send_wr, *temp_mad_send_wr;
2201 	struct ib_mad_send_wc mad_send_wc;
2202 	struct list_head cancel_list;
2203 
2204 	INIT_LIST_HEAD(&cancel_list);
2205 
2206 	spin_lock_irqsave(&mad_agent_priv->lock, flags);
2207 	list_for_each_entry_safe(mad_send_wr, temp_mad_send_wr,
2208 				 &mad_agent_priv->send_list, agent_list) {
2209 		if (mad_send_wr->status == IB_WC_SUCCESS) {
2210  			mad_send_wr->status = IB_WC_WR_FLUSH_ERR;
2211 			mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
2212 		}
2213 	}
2214 
2215 	/* Empty wait list to prevent receives from finding a request */
2216 	list_splice_init(&mad_agent_priv->wait_list, &cancel_list);
2217 	/* Empty local completion list as well */
2218 	list_splice_init(&mad_agent_priv->local_list, &cancel_list);
2219 	spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2220 
2221 	/* Report all cancelled requests */
2222 	mad_send_wc.status = IB_WC_WR_FLUSH_ERR;
2223 	mad_send_wc.vendor_err = 0;
2224 
2225 	list_for_each_entry_safe(mad_send_wr, temp_mad_send_wr,
2226 				 &cancel_list, agent_list) {
2227 		mad_send_wc.send_buf = &mad_send_wr->send_buf;
2228 		list_del(&mad_send_wr->agent_list);
2229 		mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2230 						   &mad_send_wc);
2231 		atomic_dec(&mad_agent_priv->refcount);
2232 	}
2233 }
2234 
2235 static struct ib_mad_send_wr_private*
2236 find_send_wr(struct ib_mad_agent_private *mad_agent_priv,
2237 	     struct ib_mad_send_buf *send_buf)
2238 {
2239 	struct ib_mad_send_wr_private *mad_send_wr;
2240 
2241 	list_for_each_entry(mad_send_wr, &mad_agent_priv->wait_list,
2242 			    agent_list) {
2243 		if (&mad_send_wr->send_buf == send_buf)
2244 			return mad_send_wr;
2245 	}
2246 
2247 	list_for_each_entry(mad_send_wr, &mad_agent_priv->send_list,
2248 			    agent_list) {
2249 		if (is_data_mad(mad_agent_priv, mad_send_wr->send_buf.mad) &&
2250 		    &mad_send_wr->send_buf == send_buf)
2251 			return mad_send_wr;
2252 	}
2253 	return NULL;
2254 }
2255 
2256 int ib_modify_mad(struct ib_mad_agent *mad_agent,
2257 		  struct ib_mad_send_buf *send_buf, u32 timeout_ms)
2258 {
2259 	struct ib_mad_agent_private *mad_agent_priv;
2260 	struct ib_mad_send_wr_private *mad_send_wr;
2261 	unsigned long flags;
2262 	int active;
2263 
2264 	mad_agent_priv = container_of(mad_agent, struct ib_mad_agent_private,
2265 				      agent);
2266 	spin_lock_irqsave(&mad_agent_priv->lock, flags);
2267 	mad_send_wr = find_send_wr(mad_agent_priv, send_buf);
2268 	if (!mad_send_wr || mad_send_wr->status != IB_WC_SUCCESS) {
2269 		spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2270 		return -EINVAL;
2271 	}
2272 
2273 	active = (!mad_send_wr->timeout || mad_send_wr->refcount > 1);
2274 	if (!timeout_ms) {
2275 		mad_send_wr->status = IB_WC_WR_FLUSH_ERR;
2276 		mad_send_wr->refcount -= (mad_send_wr->timeout > 0);
2277 	}
2278 
2279 	mad_send_wr->send_buf.timeout_ms = timeout_ms;
2280 	if (active)
2281 		mad_send_wr->timeout = msecs_to_jiffies(timeout_ms);
2282 	else
2283 		ib_reset_mad_timeout(mad_send_wr, timeout_ms);
2284 
2285 	spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2286 	return 0;
2287 }
2288 EXPORT_SYMBOL(ib_modify_mad);
2289 
2290 void ib_cancel_mad(struct ib_mad_agent *mad_agent,
2291 		   struct ib_mad_send_buf *send_buf)
2292 {
2293 	ib_modify_mad(mad_agent, send_buf, 0);
2294 }
2295 EXPORT_SYMBOL(ib_cancel_mad);
2296 
2297 static void local_completions(void *data)
2298 {
2299 	struct ib_mad_agent_private *mad_agent_priv;
2300 	struct ib_mad_local_private *local;
2301 	struct ib_mad_agent_private *recv_mad_agent;
2302 	unsigned long flags;
2303 	int recv = 0;
2304 	struct ib_wc wc;
2305 	struct ib_mad_send_wc mad_send_wc;
2306 
2307 	mad_agent_priv = (struct ib_mad_agent_private *)data;
2308 
2309 	spin_lock_irqsave(&mad_agent_priv->lock, flags);
2310 	while (!list_empty(&mad_agent_priv->local_list)) {
2311 		local = list_entry(mad_agent_priv->local_list.next,
2312 				   struct ib_mad_local_private,
2313 				   completion_list);
2314 		spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2315 		if (local->mad_priv) {
2316 			recv_mad_agent = local->recv_mad_agent;
2317 			if (!recv_mad_agent) {
2318 				printk(KERN_ERR PFX "No receive MAD agent for local completion\n");
2319 				goto local_send_completion;
2320 			}
2321 
2322 			recv = 1;
2323 			/*
2324 			 * Defined behavior is to complete response
2325 			 * before request
2326 			 */
2327 			build_smp_wc((unsigned long) local->mad_send_wr,
2328 				     be16_to_cpu(IB_LID_PERMISSIVE),
2329 				     0, recv_mad_agent->agent.port_num, &wc);
2330 
2331 			local->mad_priv->header.recv_wc.wc = &wc;
2332 			local->mad_priv->header.recv_wc.mad_len =
2333 						sizeof(struct ib_mad);
2334 			INIT_LIST_HEAD(&local->mad_priv->header.recv_wc.rmpp_list);
2335 			list_add(&local->mad_priv->header.recv_wc.recv_buf.list,
2336 				 &local->mad_priv->header.recv_wc.rmpp_list);
2337 			local->mad_priv->header.recv_wc.recv_buf.grh = NULL;
2338 			local->mad_priv->header.recv_wc.recv_buf.mad =
2339 						&local->mad_priv->mad.mad;
2340 			if (atomic_read(&recv_mad_agent->qp_info->snoop_count))
2341 				snoop_recv(recv_mad_agent->qp_info,
2342 					  &local->mad_priv->header.recv_wc,
2343 					   IB_MAD_SNOOP_RECVS);
2344 			recv_mad_agent->agent.recv_handler(
2345 						&recv_mad_agent->agent,
2346 						&local->mad_priv->header.recv_wc);
2347 			spin_lock_irqsave(&recv_mad_agent->lock, flags);
2348 			atomic_dec(&recv_mad_agent->refcount);
2349 			spin_unlock_irqrestore(&recv_mad_agent->lock, flags);
2350 		}
2351 
2352 local_send_completion:
2353 		/* Complete send */
2354 		mad_send_wc.status = IB_WC_SUCCESS;
2355 		mad_send_wc.vendor_err = 0;
2356 		mad_send_wc.send_buf = &local->mad_send_wr->send_buf;
2357 		if (atomic_read(&mad_agent_priv->qp_info->snoop_count))
2358 			snoop_send(mad_agent_priv->qp_info,
2359 				   &local->mad_send_wr->send_buf,
2360 				   &mad_send_wc, IB_MAD_SNOOP_SEND_COMPLETIONS);
2361 		mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2362 						   &mad_send_wc);
2363 
2364 		spin_lock_irqsave(&mad_agent_priv->lock, flags);
2365 		list_del(&local->completion_list);
2366 		atomic_dec(&mad_agent_priv->refcount);
2367 		if (!recv)
2368 			kmem_cache_free(ib_mad_cache, local->mad_priv);
2369 		kfree(local);
2370 	}
2371 	spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2372 }
2373 
2374 static int retry_send(struct ib_mad_send_wr_private *mad_send_wr)
2375 {
2376 	int ret;
2377 
2378 	if (!mad_send_wr->retries--)
2379 		return -ETIMEDOUT;
2380 
2381 	mad_send_wr->timeout = msecs_to_jiffies(mad_send_wr->send_buf.timeout_ms);
2382 
2383 	if (mad_send_wr->mad_agent_priv->agent.rmpp_version) {
2384 		ret = ib_retry_rmpp(mad_send_wr);
2385 		switch (ret) {
2386 		case IB_RMPP_RESULT_UNHANDLED:
2387 			ret = ib_send_mad(mad_send_wr);
2388 			break;
2389 		case IB_RMPP_RESULT_CONSUMED:
2390 			ret = 0;
2391 			break;
2392 		default:
2393 			ret = -ECOMM;
2394 			break;
2395 		}
2396 	} else
2397 		ret = ib_send_mad(mad_send_wr);
2398 
2399 	if (!ret) {
2400 		mad_send_wr->refcount++;
2401 		list_add_tail(&mad_send_wr->agent_list,
2402 			      &mad_send_wr->mad_agent_priv->send_list);
2403 	}
2404 	return ret;
2405 }
2406 
2407 static void timeout_sends(void *data)
2408 {
2409 	struct ib_mad_agent_private *mad_agent_priv;
2410 	struct ib_mad_send_wr_private *mad_send_wr;
2411 	struct ib_mad_send_wc mad_send_wc;
2412 	unsigned long flags, delay;
2413 
2414 	mad_agent_priv = (struct ib_mad_agent_private *)data;
2415 	mad_send_wc.vendor_err = 0;
2416 
2417 	spin_lock_irqsave(&mad_agent_priv->lock, flags);
2418 	while (!list_empty(&mad_agent_priv->wait_list)) {
2419 		mad_send_wr = list_entry(mad_agent_priv->wait_list.next,
2420 					 struct ib_mad_send_wr_private,
2421 					 agent_list);
2422 
2423 		if (time_after(mad_send_wr->timeout, jiffies)) {
2424 			delay = mad_send_wr->timeout - jiffies;
2425 			if ((long)delay <= 0)
2426 				delay = 1;
2427 			queue_delayed_work(mad_agent_priv->qp_info->
2428 					   port_priv->wq,
2429 					   &mad_agent_priv->timed_work, delay);
2430 			break;
2431 		}
2432 
2433 		list_del(&mad_send_wr->agent_list);
2434 		if (mad_send_wr->status == IB_WC_SUCCESS &&
2435 		    !retry_send(mad_send_wr))
2436 			continue;
2437 
2438 		spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2439 
2440 		if (mad_send_wr->status == IB_WC_SUCCESS)
2441 			mad_send_wc.status = IB_WC_RESP_TIMEOUT_ERR;
2442 		else
2443 			mad_send_wc.status = mad_send_wr->status;
2444 		mad_send_wc.send_buf = &mad_send_wr->send_buf;
2445 		mad_agent_priv->agent.send_handler(&mad_agent_priv->agent,
2446 						   &mad_send_wc);
2447 
2448 		atomic_dec(&mad_agent_priv->refcount);
2449 		spin_lock_irqsave(&mad_agent_priv->lock, flags);
2450 	}
2451 	spin_unlock_irqrestore(&mad_agent_priv->lock, flags);
2452 }
2453 
2454 static void ib_mad_thread_completion_handler(struct ib_cq *cq, void *arg)
2455 {
2456 	struct ib_mad_port_private *port_priv = cq->cq_context;
2457 	unsigned long flags;
2458 
2459 	spin_lock_irqsave(&ib_mad_port_list_lock, flags);
2460 	if (!list_empty(&port_priv->port_list))
2461 		queue_work(port_priv->wq, &port_priv->work);
2462 	spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
2463 }
2464 
2465 /*
2466  * Allocate receive MADs and post receive WRs for them
2467  */
2468 static int ib_mad_post_receive_mads(struct ib_mad_qp_info *qp_info,
2469 				    struct ib_mad_private *mad)
2470 {
2471 	unsigned long flags;
2472 	int post, ret;
2473 	struct ib_mad_private *mad_priv;
2474 	struct ib_sge sg_list;
2475 	struct ib_recv_wr recv_wr, *bad_recv_wr;
2476 	struct ib_mad_queue *recv_queue = &qp_info->recv_queue;
2477 
2478 	/* Initialize common scatter list fields */
2479 	sg_list.length = sizeof *mad_priv - sizeof mad_priv->header;
2480 	sg_list.lkey = (*qp_info->port_priv->mr).lkey;
2481 
2482 	/* Initialize common receive WR fields */
2483 	recv_wr.next = NULL;
2484 	recv_wr.sg_list = &sg_list;
2485 	recv_wr.num_sge = 1;
2486 
2487 	do {
2488 		/* Allocate and map receive buffer */
2489 		if (mad) {
2490 			mad_priv = mad;
2491 			mad = NULL;
2492 		} else {
2493 			mad_priv = kmem_cache_alloc(ib_mad_cache, GFP_KERNEL);
2494 			if (!mad_priv) {
2495 				printk(KERN_ERR PFX "No memory for receive buffer\n");
2496 				ret = -ENOMEM;
2497 				break;
2498 			}
2499 		}
2500 		sg_list.addr = dma_map_single(qp_info->port_priv->
2501 					      	device->dma_device,
2502 					      &mad_priv->grh,
2503 					      sizeof *mad_priv -
2504 					      	sizeof mad_priv->header,
2505 					      DMA_FROM_DEVICE);
2506 		pci_unmap_addr_set(&mad_priv->header, mapping, sg_list.addr);
2507 		recv_wr.wr_id = (unsigned long)&mad_priv->header.mad_list;
2508 		mad_priv->header.mad_list.mad_queue = recv_queue;
2509 
2510 		/* Post receive WR */
2511 		spin_lock_irqsave(&recv_queue->lock, flags);
2512 		post = (++recv_queue->count < recv_queue->max_active);
2513 		list_add_tail(&mad_priv->header.mad_list.list, &recv_queue->list);
2514 		spin_unlock_irqrestore(&recv_queue->lock, flags);
2515 		ret = ib_post_recv(qp_info->qp, &recv_wr, &bad_recv_wr);
2516 		if (ret) {
2517 			spin_lock_irqsave(&recv_queue->lock, flags);
2518 			list_del(&mad_priv->header.mad_list.list);
2519 			recv_queue->count--;
2520 			spin_unlock_irqrestore(&recv_queue->lock, flags);
2521 			dma_unmap_single(qp_info->port_priv->device->dma_device,
2522 					 pci_unmap_addr(&mad_priv->header,
2523 							mapping),
2524 					 sizeof *mad_priv -
2525 					   sizeof mad_priv->header,
2526 					 DMA_FROM_DEVICE);
2527 			kmem_cache_free(ib_mad_cache, mad_priv);
2528 			printk(KERN_ERR PFX "ib_post_recv failed: %d\n", ret);
2529 			break;
2530 		}
2531 	} while (post);
2532 
2533 	return ret;
2534 }
2535 
2536 /*
2537  * Return all the posted receive MADs
2538  */
2539 static void cleanup_recv_queue(struct ib_mad_qp_info *qp_info)
2540 {
2541 	struct ib_mad_private_header *mad_priv_hdr;
2542 	struct ib_mad_private *recv;
2543 	struct ib_mad_list_head *mad_list;
2544 
2545 	while (!list_empty(&qp_info->recv_queue.list)) {
2546 
2547 		mad_list = list_entry(qp_info->recv_queue.list.next,
2548 				      struct ib_mad_list_head, list);
2549 		mad_priv_hdr = container_of(mad_list,
2550 					    struct ib_mad_private_header,
2551 					    mad_list);
2552 		recv = container_of(mad_priv_hdr, struct ib_mad_private,
2553 				    header);
2554 
2555 		/* Remove from posted receive MAD list */
2556 		list_del(&mad_list->list);
2557 
2558 		dma_unmap_single(qp_info->port_priv->device->dma_device,
2559 				 pci_unmap_addr(&recv->header, mapping),
2560 				 sizeof(struct ib_mad_private) -
2561 				 sizeof(struct ib_mad_private_header),
2562 				 DMA_FROM_DEVICE);
2563 		kmem_cache_free(ib_mad_cache, recv);
2564 	}
2565 
2566 	qp_info->recv_queue.count = 0;
2567 }
2568 
2569 /*
2570  * Start the port
2571  */
2572 static int ib_mad_port_start(struct ib_mad_port_private *port_priv)
2573 {
2574 	int ret, i;
2575 	struct ib_qp_attr *attr;
2576 	struct ib_qp *qp;
2577 
2578 	attr = kmalloc(sizeof *attr, GFP_KERNEL);
2579  	if (!attr) {
2580 		printk(KERN_ERR PFX "Couldn't kmalloc ib_qp_attr\n");
2581 		return -ENOMEM;
2582 	}
2583 
2584 	for (i = 0; i < IB_MAD_QPS_CORE; i++) {
2585 		qp = port_priv->qp_info[i].qp;
2586 		/*
2587 		 * PKey index for QP1 is irrelevant but
2588 		 * one is needed for the Reset to Init transition
2589 		 */
2590 		attr->qp_state = IB_QPS_INIT;
2591 		attr->pkey_index = 0;
2592 		attr->qkey = (qp->qp_num == 0) ? 0 : IB_QP1_QKEY;
2593 		ret = ib_modify_qp(qp, attr, IB_QP_STATE |
2594 					     IB_QP_PKEY_INDEX | IB_QP_QKEY);
2595 		if (ret) {
2596 			printk(KERN_ERR PFX "Couldn't change QP%d state to "
2597 			       "INIT: %d\n", i, ret);
2598 			goto out;
2599 		}
2600 
2601 		attr->qp_state = IB_QPS_RTR;
2602 		ret = ib_modify_qp(qp, attr, IB_QP_STATE);
2603 		if (ret) {
2604 			printk(KERN_ERR PFX "Couldn't change QP%d state to "
2605 			       "RTR: %d\n", i, ret);
2606 			goto out;
2607 		}
2608 
2609 		attr->qp_state = IB_QPS_RTS;
2610 		attr->sq_psn = IB_MAD_SEND_Q_PSN;
2611 		ret = ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_SQ_PSN);
2612 		if (ret) {
2613 			printk(KERN_ERR PFX "Couldn't change QP%d state to "
2614 			       "RTS: %d\n", i, ret);
2615 			goto out;
2616 		}
2617 	}
2618 
2619 	ret = ib_req_notify_cq(port_priv->cq, IB_CQ_NEXT_COMP);
2620 	if (ret) {
2621 		printk(KERN_ERR PFX "Failed to request completion "
2622 		       "notification: %d\n", ret);
2623 		goto out;
2624 	}
2625 
2626 	for (i = 0; i < IB_MAD_QPS_CORE; i++) {
2627 		ret = ib_mad_post_receive_mads(&port_priv->qp_info[i], NULL);
2628 		if (ret) {
2629 			printk(KERN_ERR PFX "Couldn't post receive WRs\n");
2630 			goto out;
2631 		}
2632 	}
2633 out:
2634 	kfree(attr);
2635 	return ret;
2636 }
2637 
2638 static void qp_event_handler(struct ib_event *event, void *qp_context)
2639 {
2640 	struct ib_mad_qp_info	*qp_info = qp_context;
2641 
2642 	/* It's worse than that! He's dead, Jim! */
2643 	printk(KERN_ERR PFX "Fatal error (%d) on MAD QP (%d)\n",
2644 		event->event, qp_info->qp->qp_num);
2645 }
2646 
2647 static void init_mad_queue(struct ib_mad_qp_info *qp_info,
2648 			   struct ib_mad_queue *mad_queue)
2649 {
2650 	mad_queue->qp_info = qp_info;
2651 	mad_queue->count = 0;
2652 	spin_lock_init(&mad_queue->lock);
2653 	INIT_LIST_HEAD(&mad_queue->list);
2654 }
2655 
2656 static void init_mad_qp(struct ib_mad_port_private *port_priv,
2657 			struct ib_mad_qp_info *qp_info)
2658 {
2659 	qp_info->port_priv = port_priv;
2660 	init_mad_queue(qp_info, &qp_info->send_queue);
2661 	init_mad_queue(qp_info, &qp_info->recv_queue);
2662 	INIT_LIST_HEAD(&qp_info->overflow_list);
2663 	spin_lock_init(&qp_info->snoop_lock);
2664 	qp_info->snoop_table = NULL;
2665 	qp_info->snoop_table_size = 0;
2666 	atomic_set(&qp_info->snoop_count, 0);
2667 }
2668 
2669 static int create_mad_qp(struct ib_mad_qp_info *qp_info,
2670 			 enum ib_qp_type qp_type)
2671 {
2672 	struct ib_qp_init_attr	qp_init_attr;
2673 	int ret;
2674 
2675 	memset(&qp_init_attr, 0, sizeof qp_init_attr);
2676 	qp_init_attr.send_cq = qp_info->port_priv->cq;
2677 	qp_init_attr.recv_cq = qp_info->port_priv->cq;
2678 	qp_init_attr.sq_sig_type = IB_SIGNAL_ALL_WR;
2679 	qp_init_attr.cap.max_send_wr = IB_MAD_QP_SEND_SIZE;
2680 	qp_init_attr.cap.max_recv_wr = IB_MAD_QP_RECV_SIZE;
2681 	qp_init_attr.cap.max_send_sge = IB_MAD_SEND_REQ_MAX_SG;
2682 	qp_init_attr.cap.max_recv_sge = IB_MAD_RECV_REQ_MAX_SG;
2683 	qp_init_attr.qp_type = qp_type;
2684 	qp_init_attr.port_num = qp_info->port_priv->port_num;
2685 	qp_init_attr.qp_context = qp_info;
2686 	qp_init_attr.event_handler = qp_event_handler;
2687 	qp_info->qp = ib_create_qp(qp_info->port_priv->pd, &qp_init_attr);
2688 	if (IS_ERR(qp_info->qp)) {
2689 		printk(KERN_ERR PFX "Couldn't create ib_mad QP%d\n",
2690 		       get_spl_qp_index(qp_type));
2691 		ret = PTR_ERR(qp_info->qp);
2692 		goto error;
2693 	}
2694 	/* Use minimum queue sizes unless the CQ is resized */
2695 	qp_info->send_queue.max_active = IB_MAD_QP_SEND_SIZE;
2696 	qp_info->recv_queue.max_active = IB_MAD_QP_RECV_SIZE;
2697 	return 0;
2698 
2699 error:
2700 	return ret;
2701 }
2702 
2703 static void destroy_mad_qp(struct ib_mad_qp_info *qp_info)
2704 {
2705 	ib_destroy_qp(qp_info->qp);
2706 	kfree(qp_info->snoop_table);
2707 }
2708 
2709 /*
2710  * Open the port
2711  * Create the QP, PD, MR, and CQ if needed
2712  */
2713 static int ib_mad_port_open(struct ib_device *device,
2714 			    int port_num)
2715 {
2716 	int ret, cq_size;
2717 	struct ib_mad_port_private *port_priv;
2718 	unsigned long flags;
2719 	char name[sizeof "ib_mad123"];
2720 
2721 	/* Create new device info */
2722 	port_priv = kzalloc(sizeof *port_priv, GFP_KERNEL);
2723 	if (!port_priv) {
2724 		printk(KERN_ERR PFX "No memory for ib_mad_port_private\n");
2725 		return -ENOMEM;
2726 	}
2727 
2728 	port_priv->device = device;
2729 	port_priv->port_num = port_num;
2730 	spin_lock_init(&port_priv->reg_lock);
2731 	INIT_LIST_HEAD(&port_priv->agent_list);
2732 	init_mad_qp(port_priv, &port_priv->qp_info[0]);
2733 	init_mad_qp(port_priv, &port_priv->qp_info[1]);
2734 
2735 	cq_size = (IB_MAD_QP_SEND_SIZE + IB_MAD_QP_RECV_SIZE) * 2;
2736 	port_priv->cq = ib_create_cq(port_priv->device,
2737 				     ib_mad_thread_completion_handler,
2738 				     NULL, port_priv, cq_size);
2739 	if (IS_ERR(port_priv->cq)) {
2740 		printk(KERN_ERR PFX "Couldn't create ib_mad CQ\n");
2741 		ret = PTR_ERR(port_priv->cq);
2742 		goto error3;
2743 	}
2744 
2745 	port_priv->pd = ib_alloc_pd(device);
2746 	if (IS_ERR(port_priv->pd)) {
2747 		printk(KERN_ERR PFX "Couldn't create ib_mad PD\n");
2748 		ret = PTR_ERR(port_priv->pd);
2749 		goto error4;
2750 	}
2751 
2752 	port_priv->mr = ib_get_dma_mr(port_priv->pd, IB_ACCESS_LOCAL_WRITE);
2753 	if (IS_ERR(port_priv->mr)) {
2754 		printk(KERN_ERR PFX "Couldn't get ib_mad DMA MR\n");
2755 		ret = PTR_ERR(port_priv->mr);
2756 		goto error5;
2757 	}
2758 
2759 	ret = create_mad_qp(&port_priv->qp_info[0], IB_QPT_SMI);
2760 	if (ret)
2761 		goto error6;
2762 	ret = create_mad_qp(&port_priv->qp_info[1], IB_QPT_GSI);
2763 	if (ret)
2764 		goto error7;
2765 
2766 	snprintf(name, sizeof name, "ib_mad%d", port_num);
2767 	port_priv->wq = create_singlethread_workqueue(name);
2768 	if (!port_priv->wq) {
2769 		ret = -ENOMEM;
2770 		goto error8;
2771 	}
2772 	INIT_WORK(&port_priv->work, ib_mad_completion_handler, port_priv);
2773 
2774 	spin_lock_irqsave(&ib_mad_port_list_lock, flags);
2775 	list_add_tail(&port_priv->port_list, &ib_mad_port_list);
2776 	spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
2777 
2778 	ret = ib_mad_port_start(port_priv);
2779 	if (ret) {
2780 		printk(KERN_ERR PFX "Couldn't start port\n");
2781 		goto error9;
2782 	}
2783 
2784 	return 0;
2785 
2786 error9:
2787 	spin_lock_irqsave(&ib_mad_port_list_lock, flags);
2788 	list_del_init(&port_priv->port_list);
2789 	spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
2790 
2791 	destroy_workqueue(port_priv->wq);
2792 error8:
2793 	destroy_mad_qp(&port_priv->qp_info[1]);
2794 error7:
2795 	destroy_mad_qp(&port_priv->qp_info[0]);
2796 error6:
2797 	ib_dereg_mr(port_priv->mr);
2798 error5:
2799 	ib_dealloc_pd(port_priv->pd);
2800 error4:
2801 	ib_destroy_cq(port_priv->cq);
2802 	cleanup_recv_queue(&port_priv->qp_info[1]);
2803 	cleanup_recv_queue(&port_priv->qp_info[0]);
2804 error3:
2805 	kfree(port_priv);
2806 
2807 	return ret;
2808 }
2809 
2810 /*
2811  * Close the port
2812  * If there are no classes using the port, free the port
2813  * resources (CQ, MR, PD, QP) and remove the port's info structure
2814  */
2815 static int ib_mad_port_close(struct ib_device *device, int port_num)
2816 {
2817 	struct ib_mad_port_private *port_priv;
2818 	unsigned long flags;
2819 
2820 	spin_lock_irqsave(&ib_mad_port_list_lock, flags);
2821 	port_priv = __ib_get_mad_port(device, port_num);
2822 	if (port_priv == NULL) {
2823 		spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
2824 		printk(KERN_ERR PFX "Port %d not found\n", port_num);
2825 		return -ENODEV;
2826 	}
2827 	list_del_init(&port_priv->port_list);
2828 	spin_unlock_irqrestore(&ib_mad_port_list_lock, flags);
2829 
2830 	destroy_workqueue(port_priv->wq);
2831 	destroy_mad_qp(&port_priv->qp_info[1]);
2832 	destroy_mad_qp(&port_priv->qp_info[0]);
2833 	ib_dereg_mr(port_priv->mr);
2834 	ib_dealloc_pd(port_priv->pd);
2835 	ib_destroy_cq(port_priv->cq);
2836 	cleanup_recv_queue(&port_priv->qp_info[1]);
2837 	cleanup_recv_queue(&port_priv->qp_info[0]);
2838 	/* XXX: Handle deallocation of MAD registration tables */
2839 
2840 	kfree(port_priv);
2841 
2842 	return 0;
2843 }
2844 
2845 static void ib_mad_init_device(struct ib_device *device)
2846 {
2847 	int start, end, i;
2848 
2849 	if (device->node_type == IB_NODE_SWITCH) {
2850 		start = 0;
2851 		end   = 0;
2852 	} else {
2853 		start = 1;
2854 		end   = device->phys_port_cnt;
2855 	}
2856 
2857 	for (i = start; i <= end; i++) {
2858 		if (ib_mad_port_open(device, i)) {
2859 			printk(KERN_ERR PFX "Couldn't open %s port %d\n",
2860 			       device->name, i);
2861 			goto error;
2862 		}
2863 		if (ib_agent_port_open(device, i)) {
2864 			printk(KERN_ERR PFX "Couldn't open %s port %d "
2865 			       "for agents\n",
2866 			       device->name, i);
2867 			goto error_agent;
2868 		}
2869 	}
2870 	return;
2871 
2872 error_agent:
2873 	if (ib_mad_port_close(device, i))
2874 		printk(KERN_ERR PFX "Couldn't close %s port %d\n",
2875 		       device->name, i);
2876 
2877 error:
2878 	i--;
2879 
2880 	while (i >= start) {
2881 		if (ib_agent_port_close(device, i))
2882 			printk(KERN_ERR PFX "Couldn't close %s port %d "
2883 			       "for agents\n",
2884 			       device->name, i);
2885 		if (ib_mad_port_close(device, i))
2886 			printk(KERN_ERR PFX "Couldn't close %s port %d\n",
2887 			       device->name, i);
2888 		i--;
2889 	}
2890 }
2891 
2892 static void ib_mad_remove_device(struct ib_device *device)
2893 {
2894 	int i, num_ports, cur_port;
2895 
2896 	if (device->node_type == IB_NODE_SWITCH) {
2897 		num_ports = 1;
2898 		cur_port = 0;
2899 	} else {
2900 		num_ports = device->phys_port_cnt;
2901 		cur_port = 1;
2902 	}
2903 	for (i = 0; i < num_ports; i++, cur_port++) {
2904 		if (ib_agent_port_close(device, cur_port))
2905 			printk(KERN_ERR PFX "Couldn't close %s port %d "
2906 			       "for agents\n",
2907 			       device->name, cur_port);
2908 		if (ib_mad_port_close(device, cur_port))
2909 			printk(KERN_ERR PFX "Couldn't close %s port %d\n",
2910 			       device->name, cur_port);
2911 	}
2912 }
2913 
2914 static struct ib_client mad_client = {
2915 	.name   = "mad",
2916 	.add = ib_mad_init_device,
2917 	.remove = ib_mad_remove_device
2918 };
2919 
2920 static int __init ib_mad_init_module(void)
2921 {
2922 	int ret;
2923 
2924 	spin_lock_init(&ib_mad_port_list_lock);
2925 
2926 	ib_mad_cache = kmem_cache_create("ib_mad",
2927 					 sizeof(struct ib_mad_private),
2928 					 0,
2929 					 SLAB_HWCACHE_ALIGN,
2930 					 NULL,
2931 					 NULL);
2932 	if (!ib_mad_cache) {
2933 		printk(KERN_ERR PFX "Couldn't create ib_mad cache\n");
2934 		ret = -ENOMEM;
2935 		goto error1;
2936 	}
2937 
2938 	INIT_LIST_HEAD(&ib_mad_port_list);
2939 
2940 	if (ib_register_client(&mad_client)) {
2941 		printk(KERN_ERR PFX "Couldn't register ib_mad client\n");
2942 		ret = -EINVAL;
2943 		goto error2;
2944 	}
2945 
2946 	return 0;
2947 
2948 error2:
2949 	kmem_cache_destroy(ib_mad_cache);
2950 error1:
2951 	return ret;
2952 }
2953 
2954 static void __exit ib_mad_cleanup_module(void)
2955 {
2956 	ib_unregister_client(&mad_client);
2957 
2958 	if (kmem_cache_destroy(ib_mad_cache)) {
2959 		printk(KERN_DEBUG PFX "Failed to destroy ib_mad cache\n");
2960 	}
2961 }
2962 
2963 module_init(ib_mad_init_module);
2964 module_exit(ib_mad_cleanup_module);
2965 
2966