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