1 /*
2  * Copyright (c) 2005 Intel Inc. All rights reserved.
3  * Copyright (c) 2005 Voltaire, Inc. All rights reserved.
4  *
5  * This software is available to you under a choice of one of two
6  * licenses.  You may choose to be licensed under the terms of the GNU
7  * General Public License (GPL) Version 2, available from the file
8  * COPYING in the main directory of this source tree, or the
9  * OpenIB.org BSD license below:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *      - Redistributions of source code must retain the above
16  *        copyright notice, this list of conditions and the following
17  *        disclaimer.
18  *
19  *      - Redistributions in binary form must reproduce the above
20  *        copyright notice, this list of conditions and the following
21  *        disclaimer in the documentation and/or other materials
22  *        provided with the distribution.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31  * SOFTWARE.
32  *
33  * $Id: mad_rmpp.c 1921 2005-03-02 22:58:44Z sean.hefty $
34  */
35 
36 #include <linux/dma-mapping.h>
37 
38 #include "mad_priv.h"
39 #include "mad_rmpp.h"
40 
41 enum rmpp_state {
42 	RMPP_STATE_ACTIVE,
43 	RMPP_STATE_TIMEOUT,
44 	RMPP_STATE_COMPLETE
45 };
46 
47 struct mad_rmpp_recv {
48 	struct ib_mad_agent_private *agent;
49 	struct list_head list;
50 	struct work_struct timeout_work;
51 	struct work_struct cleanup_work;
52 	wait_queue_head_t wait;
53 	enum rmpp_state state;
54 	spinlock_t lock;
55 	atomic_t refcount;
56 
57 	struct ib_ah *ah;
58 	struct ib_mad_recv_wc *rmpp_wc;
59 	struct ib_mad_recv_buf *cur_seg_buf;
60 	int last_ack;
61 	int seg_num;
62 	int newwin;
63 
64 	__be64 tid;
65 	u32 src_qp;
66 	u16 slid;
67 	u8 mgmt_class;
68 	u8 class_version;
69 	u8 method;
70 };
71 
72 static void destroy_rmpp_recv(struct mad_rmpp_recv *rmpp_recv)
73 {
74 	atomic_dec(&rmpp_recv->refcount);
75 	wait_event(rmpp_recv->wait, !atomic_read(&rmpp_recv->refcount));
76 	ib_destroy_ah(rmpp_recv->ah);
77 	kfree(rmpp_recv);
78 }
79 
80 void ib_cancel_rmpp_recvs(struct ib_mad_agent_private *agent)
81 {
82 	struct mad_rmpp_recv *rmpp_recv, *temp_rmpp_recv;
83 	unsigned long flags;
84 
85 	spin_lock_irqsave(&agent->lock, flags);
86 	list_for_each_entry(rmpp_recv, &agent->rmpp_list, list) {
87 		cancel_delayed_work(&rmpp_recv->timeout_work);
88 		cancel_delayed_work(&rmpp_recv->cleanup_work);
89 	}
90 	spin_unlock_irqrestore(&agent->lock, flags);
91 
92 	flush_workqueue(agent->qp_info->port_priv->wq);
93 
94 	list_for_each_entry_safe(rmpp_recv, temp_rmpp_recv,
95 				 &agent->rmpp_list, list) {
96 		list_del(&rmpp_recv->list);
97 		if (rmpp_recv->state != RMPP_STATE_COMPLETE)
98 			ib_free_recv_mad(rmpp_recv->rmpp_wc);
99 		destroy_rmpp_recv(rmpp_recv);
100 	}
101 }
102 
103 static int data_offset(u8 mgmt_class)
104 {
105 	if (mgmt_class == IB_MGMT_CLASS_SUBN_ADM)
106 		return IB_MGMT_SA_HDR;
107 	else if ((mgmt_class >= IB_MGMT_CLASS_VENDOR_RANGE2_START) &&
108 		 (mgmt_class <= IB_MGMT_CLASS_VENDOR_RANGE2_END))
109 		return IB_MGMT_VENDOR_HDR;
110 	else
111 		return IB_MGMT_RMPP_HDR;
112 }
113 
114 static void format_ack(struct ib_rmpp_mad *ack,
115 		       struct ib_rmpp_mad *data,
116 		       struct mad_rmpp_recv *rmpp_recv)
117 {
118 	unsigned long flags;
119 
120 	memcpy(&ack->mad_hdr, &data->mad_hdr,
121 	       data_offset(data->mad_hdr.mgmt_class));
122 
123 	ack->mad_hdr.method ^= IB_MGMT_METHOD_RESP;
124 	ack->rmpp_hdr.rmpp_type = IB_MGMT_RMPP_TYPE_ACK;
125 	ib_set_rmpp_flags(&ack->rmpp_hdr, IB_MGMT_RMPP_FLAG_ACTIVE);
126 
127 	spin_lock_irqsave(&rmpp_recv->lock, flags);
128 	rmpp_recv->last_ack = rmpp_recv->seg_num;
129 	ack->rmpp_hdr.seg_num = cpu_to_be32(rmpp_recv->seg_num);
130 	ack->rmpp_hdr.paylen_newwin = cpu_to_be32(rmpp_recv->newwin);
131 	spin_unlock_irqrestore(&rmpp_recv->lock, flags);
132 }
133 
134 static void ack_recv(struct mad_rmpp_recv *rmpp_recv,
135 		     struct ib_mad_recv_wc *recv_wc)
136 {
137 	struct ib_mad_send_buf *msg;
138 	int ret;
139 
140 	msg = ib_create_send_mad(&rmpp_recv->agent->agent, recv_wc->wc->src_qp,
141 				 recv_wc->wc->pkey_index, 1, IB_MGMT_RMPP_HDR,
142 				 IB_MGMT_RMPP_DATA, GFP_KERNEL);
143 	if (!msg)
144 		return;
145 
146 	format_ack(msg->mad, (struct ib_rmpp_mad *) recv_wc->recv_buf.mad,
147 		   rmpp_recv);
148 	msg->ah = rmpp_recv->ah;
149 	ret = ib_post_send_mad(msg, NULL);
150 	if (ret)
151 		ib_free_send_mad(msg);
152 }
153 
154 static struct ib_mad_send_buf *alloc_response_msg(struct ib_mad_agent *agent,
155 						  struct ib_mad_recv_wc *recv_wc)
156 {
157 	struct ib_mad_send_buf *msg;
158 	struct ib_ah *ah;
159 
160 	ah = ib_create_ah_from_wc(agent->qp->pd, recv_wc->wc,
161 				  recv_wc->recv_buf.grh, agent->port_num);
162 	if (IS_ERR(ah))
163 		return (void *) ah;
164 
165 	msg = ib_create_send_mad(agent, recv_wc->wc->src_qp,
166 				 recv_wc->wc->pkey_index, 1,
167 				 IB_MGMT_RMPP_HDR, IB_MGMT_RMPP_DATA,
168 				 GFP_KERNEL);
169 	if (IS_ERR(msg))
170 		ib_destroy_ah(ah);
171 	else
172 		msg->ah = ah;
173 
174 	return msg;
175 }
176 
177 void ib_rmpp_send_handler(struct ib_mad_send_wc *mad_send_wc)
178 {
179 	struct ib_rmpp_mad *rmpp_mad = mad_send_wc->send_buf->mad;
180 
181 	if (rmpp_mad->rmpp_hdr.rmpp_type != IB_MGMT_RMPP_TYPE_ACK)
182 		ib_destroy_ah(mad_send_wc->send_buf->ah);
183 	ib_free_send_mad(mad_send_wc->send_buf);
184 }
185 
186 static void nack_recv(struct ib_mad_agent_private *agent,
187 		      struct ib_mad_recv_wc *recv_wc, u8 rmpp_status)
188 {
189 	struct ib_mad_send_buf *msg;
190 	struct ib_rmpp_mad *rmpp_mad;
191 	int ret;
192 
193 	msg = alloc_response_msg(&agent->agent, recv_wc);
194 	if (IS_ERR(msg))
195 		return;
196 
197 	rmpp_mad = msg->mad;
198 	memcpy(rmpp_mad, recv_wc->recv_buf.mad,
199 	       data_offset(recv_wc->recv_buf.mad->mad_hdr.mgmt_class));
200 
201 	rmpp_mad->mad_hdr.method ^= IB_MGMT_METHOD_RESP;
202 	rmpp_mad->rmpp_hdr.rmpp_version = IB_MGMT_RMPP_VERSION;
203 	rmpp_mad->rmpp_hdr.rmpp_type = IB_MGMT_RMPP_TYPE_ABORT;
204 	ib_set_rmpp_flags(&rmpp_mad->rmpp_hdr, IB_MGMT_RMPP_FLAG_ACTIVE);
205 	rmpp_mad->rmpp_hdr.rmpp_status = rmpp_status;
206 	rmpp_mad->rmpp_hdr.seg_num = 0;
207 	rmpp_mad->rmpp_hdr.paylen_newwin = 0;
208 
209 	ret = ib_post_send_mad(msg, NULL);
210 	if (ret) {
211 		ib_destroy_ah(msg->ah);
212 		ib_free_send_mad(msg);
213 	}
214 }
215 
216 static void recv_timeout_handler(void *data)
217 {
218 	struct mad_rmpp_recv *rmpp_recv = data;
219 	struct ib_mad_recv_wc *rmpp_wc;
220 	unsigned long flags;
221 
222 	spin_lock_irqsave(&rmpp_recv->agent->lock, flags);
223 	if (rmpp_recv->state != RMPP_STATE_ACTIVE) {
224 		spin_unlock_irqrestore(&rmpp_recv->agent->lock, flags);
225 		return;
226 	}
227 	rmpp_recv->state = RMPP_STATE_TIMEOUT;
228 	list_del(&rmpp_recv->list);
229 	spin_unlock_irqrestore(&rmpp_recv->agent->lock, flags);
230 
231 	rmpp_wc = rmpp_recv->rmpp_wc;
232 	nack_recv(rmpp_recv->agent, rmpp_wc, IB_MGMT_RMPP_STATUS_T2L);
233 	destroy_rmpp_recv(rmpp_recv);
234 	ib_free_recv_mad(rmpp_wc);
235 }
236 
237 static void recv_cleanup_handler(void *data)
238 {
239 	struct mad_rmpp_recv *rmpp_recv = data;
240 	unsigned long flags;
241 
242 	spin_lock_irqsave(&rmpp_recv->agent->lock, flags);
243 	list_del(&rmpp_recv->list);
244 	spin_unlock_irqrestore(&rmpp_recv->agent->lock, flags);
245 	destroy_rmpp_recv(rmpp_recv);
246 }
247 
248 static struct mad_rmpp_recv *
249 create_rmpp_recv(struct ib_mad_agent_private *agent,
250 		 struct ib_mad_recv_wc *mad_recv_wc)
251 {
252 	struct mad_rmpp_recv *rmpp_recv;
253 	struct ib_mad_hdr *mad_hdr;
254 
255 	rmpp_recv = kmalloc(sizeof *rmpp_recv, GFP_KERNEL);
256 	if (!rmpp_recv)
257 		return NULL;
258 
259 	rmpp_recv->ah = ib_create_ah_from_wc(agent->agent.qp->pd,
260 					     mad_recv_wc->wc,
261 					     mad_recv_wc->recv_buf.grh,
262 					     agent->agent.port_num);
263 	if (IS_ERR(rmpp_recv->ah))
264 		goto error;
265 
266 	rmpp_recv->agent = agent;
267 	init_waitqueue_head(&rmpp_recv->wait);
268 	INIT_WORK(&rmpp_recv->timeout_work, recv_timeout_handler, rmpp_recv);
269 	INIT_WORK(&rmpp_recv->cleanup_work, recv_cleanup_handler, rmpp_recv);
270 	spin_lock_init(&rmpp_recv->lock);
271 	rmpp_recv->state = RMPP_STATE_ACTIVE;
272 	atomic_set(&rmpp_recv->refcount, 1);
273 
274 	rmpp_recv->rmpp_wc = mad_recv_wc;
275 	rmpp_recv->cur_seg_buf = &mad_recv_wc->recv_buf;
276 	rmpp_recv->newwin = 1;
277 	rmpp_recv->seg_num = 1;
278 	rmpp_recv->last_ack = 0;
279 
280 	mad_hdr = &mad_recv_wc->recv_buf.mad->mad_hdr;
281 	rmpp_recv->tid = mad_hdr->tid;
282 	rmpp_recv->src_qp = mad_recv_wc->wc->src_qp;
283 	rmpp_recv->slid = mad_recv_wc->wc->slid;
284 	rmpp_recv->mgmt_class = mad_hdr->mgmt_class;
285 	rmpp_recv->class_version = mad_hdr->class_version;
286 	rmpp_recv->method  = mad_hdr->method;
287 	return rmpp_recv;
288 
289 error:	kfree(rmpp_recv);
290 	return NULL;
291 }
292 
293 static inline void deref_rmpp_recv(struct mad_rmpp_recv *rmpp_recv)
294 {
295 	if (atomic_dec_and_test(&rmpp_recv->refcount))
296 		wake_up(&rmpp_recv->wait);
297 }
298 
299 static struct mad_rmpp_recv *
300 find_rmpp_recv(struct ib_mad_agent_private *agent,
301 	       struct ib_mad_recv_wc *mad_recv_wc)
302 {
303 	struct mad_rmpp_recv *rmpp_recv;
304 	struct ib_mad_hdr *mad_hdr = &mad_recv_wc->recv_buf.mad->mad_hdr;
305 
306 	list_for_each_entry(rmpp_recv, &agent->rmpp_list, list) {
307 		if (rmpp_recv->tid == mad_hdr->tid &&
308 		    rmpp_recv->src_qp == mad_recv_wc->wc->src_qp &&
309 		    rmpp_recv->slid == mad_recv_wc->wc->slid &&
310 		    rmpp_recv->mgmt_class == mad_hdr->mgmt_class &&
311 		    rmpp_recv->class_version == mad_hdr->class_version &&
312 		    rmpp_recv->method == mad_hdr->method)
313 			return rmpp_recv;
314 	}
315 	return NULL;
316 }
317 
318 static struct mad_rmpp_recv *
319 acquire_rmpp_recv(struct ib_mad_agent_private *agent,
320 		  struct ib_mad_recv_wc *mad_recv_wc)
321 {
322 	struct mad_rmpp_recv *rmpp_recv;
323 	unsigned long flags;
324 
325 	spin_lock_irqsave(&agent->lock, flags);
326 	rmpp_recv = find_rmpp_recv(agent, mad_recv_wc);
327 	if (rmpp_recv)
328 		atomic_inc(&rmpp_recv->refcount);
329 	spin_unlock_irqrestore(&agent->lock, flags);
330 	return rmpp_recv;
331 }
332 
333 static struct mad_rmpp_recv *
334 insert_rmpp_recv(struct ib_mad_agent_private *agent,
335 		 struct mad_rmpp_recv *rmpp_recv)
336 {
337 	struct mad_rmpp_recv *cur_rmpp_recv;
338 
339 	cur_rmpp_recv = find_rmpp_recv(agent, rmpp_recv->rmpp_wc);
340 	if (!cur_rmpp_recv)
341 		list_add_tail(&rmpp_recv->list, &agent->rmpp_list);
342 
343 	return cur_rmpp_recv;
344 }
345 
346 static inline int get_last_flag(struct ib_mad_recv_buf *seg)
347 {
348 	struct ib_rmpp_mad *rmpp_mad;
349 
350 	rmpp_mad = (struct ib_rmpp_mad *) seg->mad;
351 	return ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) & IB_MGMT_RMPP_FLAG_LAST;
352 }
353 
354 static inline int get_seg_num(struct ib_mad_recv_buf *seg)
355 {
356 	struct ib_rmpp_mad *rmpp_mad;
357 
358 	rmpp_mad = (struct ib_rmpp_mad *) seg->mad;
359 	return be32_to_cpu(rmpp_mad->rmpp_hdr.seg_num);
360 }
361 
362 static inline struct ib_mad_recv_buf * get_next_seg(struct list_head *rmpp_list,
363 						    struct ib_mad_recv_buf *seg)
364 {
365 	if (seg->list.next == rmpp_list)
366 		return NULL;
367 
368 	return container_of(seg->list.next, struct ib_mad_recv_buf, list);
369 }
370 
371 static inline int window_size(struct ib_mad_agent_private *agent)
372 {
373 	return max(agent->qp_info->recv_queue.max_active >> 3, 1);
374 }
375 
376 static struct ib_mad_recv_buf * find_seg_location(struct list_head *rmpp_list,
377 						  int seg_num)
378 {
379         struct ib_mad_recv_buf *seg_buf;
380 	int cur_seg_num;
381 
382 	list_for_each_entry_reverse(seg_buf, rmpp_list, list) {
383 		cur_seg_num = get_seg_num(seg_buf);
384 		if (seg_num > cur_seg_num)
385 			return seg_buf;
386 		if (seg_num == cur_seg_num)
387 			break;
388 	}
389 	return NULL;
390 }
391 
392 static void update_seg_num(struct mad_rmpp_recv *rmpp_recv,
393 			   struct ib_mad_recv_buf *new_buf)
394 {
395 	struct list_head *rmpp_list = &rmpp_recv->rmpp_wc->rmpp_list;
396 
397 	while (new_buf && (get_seg_num(new_buf) == rmpp_recv->seg_num + 1)) {
398 		rmpp_recv->cur_seg_buf = new_buf;
399 		rmpp_recv->seg_num++;
400 		new_buf = get_next_seg(rmpp_list, new_buf);
401 	}
402 }
403 
404 static inline int get_mad_len(struct mad_rmpp_recv *rmpp_recv)
405 {
406 	struct ib_rmpp_mad *rmpp_mad;
407 	int hdr_size, data_size, pad;
408 
409 	rmpp_mad = (struct ib_rmpp_mad *)rmpp_recv->cur_seg_buf->mad;
410 
411 	hdr_size = data_offset(rmpp_mad->mad_hdr.mgmt_class);
412 	data_size = sizeof(struct ib_rmpp_mad) - hdr_size;
413 	pad = IB_MGMT_RMPP_DATA - be32_to_cpu(rmpp_mad->rmpp_hdr.paylen_newwin);
414 	if (pad > IB_MGMT_RMPP_DATA || pad < 0)
415 		pad = 0;
416 
417 	return hdr_size + rmpp_recv->seg_num * data_size - pad;
418 }
419 
420 static struct ib_mad_recv_wc * complete_rmpp(struct mad_rmpp_recv *rmpp_recv)
421 {
422 	struct ib_mad_recv_wc *rmpp_wc;
423 
424 	ack_recv(rmpp_recv, rmpp_recv->rmpp_wc);
425 	if (rmpp_recv->seg_num > 1)
426 		cancel_delayed_work(&rmpp_recv->timeout_work);
427 
428 	rmpp_wc = rmpp_recv->rmpp_wc;
429 	rmpp_wc->mad_len = get_mad_len(rmpp_recv);
430 	/* 10 seconds until we can find the packet lifetime */
431 	queue_delayed_work(rmpp_recv->agent->qp_info->port_priv->wq,
432 			   &rmpp_recv->cleanup_work, msecs_to_jiffies(10000));
433 	return rmpp_wc;
434 }
435 
436 void ib_coalesce_recv_mad(struct ib_mad_recv_wc *mad_recv_wc, void *buf)
437 {
438 	struct ib_mad_recv_buf *seg_buf;
439 	struct ib_rmpp_mad *rmpp_mad;
440 	void *data;
441 	int size, len, offset;
442 	u8 flags;
443 
444 	len = mad_recv_wc->mad_len;
445 	if (len <= sizeof(struct ib_mad)) {
446 		memcpy(buf, mad_recv_wc->recv_buf.mad, len);
447 		return;
448 	}
449 
450 	offset = data_offset(mad_recv_wc->recv_buf.mad->mad_hdr.mgmt_class);
451 
452 	list_for_each_entry(seg_buf, &mad_recv_wc->rmpp_list, list) {
453 		rmpp_mad = (struct ib_rmpp_mad *)seg_buf->mad;
454 		flags = ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr);
455 
456 		if (flags & IB_MGMT_RMPP_FLAG_FIRST) {
457 			data = rmpp_mad;
458 			size = sizeof(*rmpp_mad);
459 		} else {
460 			data = (void *) rmpp_mad + offset;
461 			if (flags & IB_MGMT_RMPP_FLAG_LAST)
462 				size = len;
463 			else
464 				size = sizeof(*rmpp_mad) - offset;
465 		}
466 
467 		memcpy(buf, data, size);
468 		len -= size;
469 		buf += size;
470 	}
471 }
472 EXPORT_SYMBOL(ib_coalesce_recv_mad);
473 
474 static struct ib_mad_recv_wc *
475 continue_rmpp(struct ib_mad_agent_private *agent,
476 	      struct ib_mad_recv_wc *mad_recv_wc)
477 {
478 	struct mad_rmpp_recv *rmpp_recv;
479 	struct ib_mad_recv_buf *prev_buf;
480 	struct ib_mad_recv_wc *done_wc;
481 	int seg_num;
482 	unsigned long flags;
483 
484 	rmpp_recv = acquire_rmpp_recv(agent, mad_recv_wc);
485 	if (!rmpp_recv)
486 		goto drop1;
487 
488 	seg_num = get_seg_num(&mad_recv_wc->recv_buf);
489 
490 	spin_lock_irqsave(&rmpp_recv->lock, flags);
491 	if ((rmpp_recv->state == RMPP_STATE_TIMEOUT) ||
492 	    (seg_num > rmpp_recv->newwin))
493 		goto drop3;
494 
495 	if ((seg_num <= rmpp_recv->last_ack) ||
496 	    (rmpp_recv->state == RMPP_STATE_COMPLETE)) {
497 		spin_unlock_irqrestore(&rmpp_recv->lock, flags);
498 		ack_recv(rmpp_recv, mad_recv_wc);
499 		goto drop2;
500 	}
501 
502 	prev_buf = find_seg_location(&rmpp_recv->rmpp_wc->rmpp_list, seg_num);
503 	if (!prev_buf)
504 		goto drop3;
505 
506 	done_wc = NULL;
507 	list_add(&mad_recv_wc->recv_buf.list, &prev_buf->list);
508 	if (rmpp_recv->cur_seg_buf == prev_buf) {
509 		update_seg_num(rmpp_recv, &mad_recv_wc->recv_buf);
510 		if (get_last_flag(rmpp_recv->cur_seg_buf)) {
511 			rmpp_recv->state = RMPP_STATE_COMPLETE;
512 			spin_unlock_irqrestore(&rmpp_recv->lock, flags);
513 			done_wc = complete_rmpp(rmpp_recv);
514 			goto out;
515 		} else if (rmpp_recv->seg_num == rmpp_recv->newwin) {
516 			rmpp_recv->newwin += window_size(agent);
517 			spin_unlock_irqrestore(&rmpp_recv->lock, flags);
518 			ack_recv(rmpp_recv, mad_recv_wc);
519 			goto out;
520 		}
521 	}
522 	spin_unlock_irqrestore(&rmpp_recv->lock, flags);
523 out:
524 	deref_rmpp_recv(rmpp_recv);
525 	return done_wc;
526 
527 drop3:	spin_unlock_irqrestore(&rmpp_recv->lock, flags);
528 drop2:	deref_rmpp_recv(rmpp_recv);
529 drop1:	ib_free_recv_mad(mad_recv_wc);
530 	return NULL;
531 }
532 
533 static struct ib_mad_recv_wc *
534 start_rmpp(struct ib_mad_agent_private *agent,
535 	   struct ib_mad_recv_wc *mad_recv_wc)
536 {
537 	struct mad_rmpp_recv *rmpp_recv;
538 	unsigned long flags;
539 
540 	rmpp_recv = create_rmpp_recv(agent, mad_recv_wc);
541 	if (!rmpp_recv) {
542 		ib_free_recv_mad(mad_recv_wc);
543 		return NULL;
544 	}
545 
546 	spin_lock_irqsave(&agent->lock, flags);
547 	if (insert_rmpp_recv(agent, rmpp_recv)) {
548 		spin_unlock_irqrestore(&agent->lock, flags);
549 		/* duplicate first MAD */
550 		destroy_rmpp_recv(rmpp_recv);
551 		return continue_rmpp(agent, mad_recv_wc);
552 	}
553 	atomic_inc(&rmpp_recv->refcount);
554 
555 	if (get_last_flag(&mad_recv_wc->recv_buf)) {
556 		rmpp_recv->state = RMPP_STATE_COMPLETE;
557 		spin_unlock_irqrestore(&agent->lock, flags);
558 		complete_rmpp(rmpp_recv);
559 	} else {
560 		spin_unlock_irqrestore(&agent->lock, flags);
561 		/* 40 seconds until we can find the packet lifetimes */
562 		queue_delayed_work(agent->qp_info->port_priv->wq,
563 				   &rmpp_recv->timeout_work,
564 				   msecs_to_jiffies(40000));
565 		rmpp_recv->newwin += window_size(agent);
566 		ack_recv(rmpp_recv, mad_recv_wc);
567 		mad_recv_wc = NULL;
568 	}
569 	deref_rmpp_recv(rmpp_recv);
570 	return mad_recv_wc;
571 }
572 
573 static inline u64 get_seg_addr(struct ib_mad_send_wr_private *mad_send_wr)
574 {
575 	return mad_send_wr->sg_list[0].addr + mad_send_wr->data_offset +
576 	       (sizeof(struct ib_rmpp_mad) - mad_send_wr->data_offset) *
577 	       (mad_send_wr->seg_num - 1);
578 }
579 
580 static int send_next_seg(struct ib_mad_send_wr_private *mad_send_wr)
581 {
582 	struct ib_rmpp_mad *rmpp_mad;
583 	int timeout;
584 	u32 paylen;
585 
586 	rmpp_mad = mad_send_wr->send_buf.mad;
587 	ib_set_rmpp_flags(&rmpp_mad->rmpp_hdr, IB_MGMT_RMPP_FLAG_ACTIVE);
588 	rmpp_mad->rmpp_hdr.seg_num = cpu_to_be32(mad_send_wr->seg_num);
589 
590 	if (mad_send_wr->seg_num == 1) {
591 		rmpp_mad->rmpp_hdr.rmpp_rtime_flags |= IB_MGMT_RMPP_FLAG_FIRST;
592 		paylen = mad_send_wr->total_seg * IB_MGMT_RMPP_DATA -
593 			 mad_send_wr->pad;
594 		rmpp_mad->rmpp_hdr.paylen_newwin = cpu_to_be32(paylen);
595 		mad_send_wr->sg_list[0].length = sizeof(struct ib_rmpp_mad);
596 	} else {
597 		mad_send_wr->send_wr.num_sge = 2;
598 		mad_send_wr->sg_list[0].length = mad_send_wr->data_offset;
599 		mad_send_wr->sg_list[1].addr = get_seg_addr(mad_send_wr);
600 		mad_send_wr->sg_list[1].length = sizeof(struct ib_rmpp_mad) -
601 						 mad_send_wr->data_offset;
602 		mad_send_wr->sg_list[1].lkey = mad_send_wr->sg_list[0].lkey;
603 		rmpp_mad->rmpp_hdr.paylen_newwin = 0;
604 	}
605 
606 	if (mad_send_wr->seg_num == mad_send_wr->total_seg) {
607 		rmpp_mad->rmpp_hdr.rmpp_rtime_flags |= IB_MGMT_RMPP_FLAG_LAST;
608 		paylen = IB_MGMT_RMPP_DATA - mad_send_wr->pad;
609 		rmpp_mad->rmpp_hdr.paylen_newwin = cpu_to_be32(paylen);
610 	}
611 
612 	/* 2 seconds for an ACK until we can find the packet lifetime */
613 	timeout = mad_send_wr->send_buf.timeout_ms;
614 	if (!timeout || timeout > 2000)
615 		mad_send_wr->timeout = msecs_to_jiffies(2000);
616 	mad_send_wr->seg_num++;
617 	return ib_send_mad(mad_send_wr);
618 }
619 
620 static void abort_send(struct ib_mad_agent_private *agent, __be64 tid,
621 		       u8 rmpp_status)
622 {
623 	struct ib_mad_send_wr_private *mad_send_wr;
624 	struct ib_mad_send_wc wc;
625 	unsigned long flags;
626 
627 	spin_lock_irqsave(&agent->lock, flags);
628 	mad_send_wr = ib_find_send_mad(agent, tid);
629 	if (!mad_send_wr)
630 		goto out;	/* Unmatched send */
631 
632 	if ((mad_send_wr->last_ack == mad_send_wr->total_seg) ||
633 	    (!mad_send_wr->timeout) || (mad_send_wr->status != IB_WC_SUCCESS))
634 		goto out;	/* Send is already done */
635 
636 	ib_mark_mad_done(mad_send_wr);
637 	spin_unlock_irqrestore(&agent->lock, flags);
638 
639 	wc.status = IB_WC_REM_ABORT_ERR;
640 	wc.vendor_err = rmpp_status;
641 	wc.send_buf = &mad_send_wr->send_buf;
642 	ib_mad_complete_send_wr(mad_send_wr, &wc);
643 	return;
644 out:
645 	spin_unlock_irqrestore(&agent->lock, flags);
646 }
647 
648 static void process_rmpp_ack(struct ib_mad_agent_private *agent,
649 			     struct ib_mad_recv_wc *mad_recv_wc)
650 {
651 	struct ib_mad_send_wr_private *mad_send_wr;
652 	struct ib_rmpp_mad *rmpp_mad;
653 	unsigned long flags;
654 	int seg_num, newwin, ret;
655 
656 	rmpp_mad = (struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad;
657 	if (rmpp_mad->rmpp_hdr.rmpp_status) {
658 		abort_send(agent, rmpp_mad->mad_hdr.tid,
659 			   IB_MGMT_RMPP_STATUS_BAD_STATUS);
660 		nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BAD_STATUS);
661 		return;
662 	}
663 
664 	seg_num = be32_to_cpu(rmpp_mad->rmpp_hdr.seg_num);
665 	newwin = be32_to_cpu(rmpp_mad->rmpp_hdr.paylen_newwin);
666 	if (newwin < seg_num) {
667 		abort_send(agent, rmpp_mad->mad_hdr.tid,
668 			   IB_MGMT_RMPP_STATUS_W2S);
669 		nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_W2S);
670 		return;
671 	}
672 
673 	spin_lock_irqsave(&agent->lock, flags);
674 	mad_send_wr = ib_find_send_mad(agent, rmpp_mad->mad_hdr.tid);
675 	if (!mad_send_wr)
676 		goto out;	/* Unmatched ACK */
677 
678 	if ((mad_send_wr->last_ack == mad_send_wr->total_seg) ||
679 	    (!mad_send_wr->timeout) || (mad_send_wr->status != IB_WC_SUCCESS))
680 		goto out;	/* Send is already done */
681 
682 	if (seg_num > mad_send_wr->total_seg || seg_num > mad_send_wr->newwin) {
683 		spin_unlock_irqrestore(&agent->lock, flags);
684 		abort_send(agent, rmpp_mad->mad_hdr.tid,
685 			   IB_MGMT_RMPP_STATUS_S2B);
686 		nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_S2B);
687 		return;
688 	}
689 
690 	if (newwin < mad_send_wr->newwin || seg_num < mad_send_wr->last_ack)
691 		goto out;	/* Old ACK */
692 
693 	if (seg_num > mad_send_wr->last_ack) {
694 		mad_send_wr->last_ack = seg_num;
695 		mad_send_wr->retries = mad_send_wr->send_buf.retries;
696 	}
697 	mad_send_wr->newwin = newwin;
698 	if (mad_send_wr->last_ack == mad_send_wr->total_seg) {
699 		/* If no response is expected, the ACK completes the send */
700 		if (!mad_send_wr->send_buf.timeout_ms) {
701 			struct ib_mad_send_wc wc;
702 
703 			ib_mark_mad_done(mad_send_wr);
704 			spin_unlock_irqrestore(&agent->lock, flags);
705 
706 			wc.status = IB_WC_SUCCESS;
707 			wc.vendor_err = 0;
708 			wc.send_buf = &mad_send_wr->send_buf;
709 			ib_mad_complete_send_wr(mad_send_wr, &wc);
710 			return;
711 		}
712 		if (mad_send_wr->refcount == 1)
713 			ib_reset_mad_timeout(mad_send_wr,
714 					     mad_send_wr->send_buf.timeout_ms);
715 	} else if (mad_send_wr->refcount == 1 &&
716 		   mad_send_wr->seg_num < mad_send_wr->newwin &&
717 		   mad_send_wr->seg_num <= mad_send_wr->total_seg) {
718 		/* Send failure will just result in a timeout/retry */
719 		ret = send_next_seg(mad_send_wr);
720 		if (ret)
721 			goto out;
722 
723 		mad_send_wr->refcount++;
724 		list_del(&mad_send_wr->agent_list);
725 		list_add_tail(&mad_send_wr->agent_list,
726 			      &mad_send_wr->mad_agent_priv->send_list);
727 	}
728 out:
729 	spin_unlock_irqrestore(&agent->lock, flags);
730 }
731 
732 static struct ib_mad_recv_wc *
733 process_rmpp_data(struct ib_mad_agent_private *agent,
734 		  struct ib_mad_recv_wc *mad_recv_wc)
735 {
736 	struct ib_rmpp_hdr *rmpp_hdr;
737 	u8 rmpp_status;
738 
739 	rmpp_hdr = &((struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad)->rmpp_hdr;
740 
741 	if (rmpp_hdr->rmpp_status) {
742 		rmpp_status = IB_MGMT_RMPP_STATUS_BAD_STATUS;
743 		goto bad;
744 	}
745 
746 	if (rmpp_hdr->seg_num == __constant_htonl(1)) {
747 		if (!(ib_get_rmpp_flags(rmpp_hdr) & IB_MGMT_RMPP_FLAG_FIRST)) {
748 			rmpp_status = IB_MGMT_RMPP_STATUS_BAD_SEG;
749 			goto bad;
750 		}
751 		return start_rmpp(agent, mad_recv_wc);
752 	} else {
753 		if (ib_get_rmpp_flags(rmpp_hdr) & IB_MGMT_RMPP_FLAG_FIRST) {
754 			rmpp_status = IB_MGMT_RMPP_STATUS_BAD_SEG;
755 			goto bad;
756 		}
757 		return continue_rmpp(agent, mad_recv_wc);
758 	}
759 bad:
760 	nack_recv(agent, mad_recv_wc, rmpp_status);
761 	ib_free_recv_mad(mad_recv_wc);
762 	return NULL;
763 }
764 
765 static void process_rmpp_stop(struct ib_mad_agent_private *agent,
766 			      struct ib_mad_recv_wc *mad_recv_wc)
767 {
768 	struct ib_rmpp_mad *rmpp_mad;
769 
770 	rmpp_mad = (struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad;
771 
772 	if (rmpp_mad->rmpp_hdr.rmpp_status != IB_MGMT_RMPP_STATUS_RESX) {
773 		abort_send(agent, rmpp_mad->mad_hdr.tid,
774 			   IB_MGMT_RMPP_STATUS_BAD_STATUS);
775 		nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BAD_STATUS);
776 	} else
777 		abort_send(agent, rmpp_mad->mad_hdr.tid,
778 			   rmpp_mad->rmpp_hdr.rmpp_status);
779 }
780 
781 static void process_rmpp_abort(struct ib_mad_agent_private *agent,
782 			       struct ib_mad_recv_wc *mad_recv_wc)
783 {
784 	struct ib_rmpp_mad *rmpp_mad;
785 
786 	rmpp_mad = (struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad;
787 
788 	if (rmpp_mad->rmpp_hdr.rmpp_status < IB_MGMT_RMPP_STATUS_ABORT_MIN ||
789 	    rmpp_mad->rmpp_hdr.rmpp_status > IB_MGMT_RMPP_STATUS_ABORT_MAX) {
790 		abort_send(agent, rmpp_mad->mad_hdr.tid,
791 			   IB_MGMT_RMPP_STATUS_BAD_STATUS);
792 		nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BAD_STATUS);
793 	} else
794 		abort_send(agent, rmpp_mad->mad_hdr.tid,
795 			   rmpp_mad->rmpp_hdr.rmpp_status);
796 }
797 
798 struct ib_mad_recv_wc *
799 ib_process_rmpp_recv_wc(struct ib_mad_agent_private *agent,
800 			struct ib_mad_recv_wc *mad_recv_wc)
801 {
802 	struct ib_rmpp_mad *rmpp_mad;
803 
804 	rmpp_mad = (struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad;
805 	if (!(rmpp_mad->rmpp_hdr.rmpp_rtime_flags & IB_MGMT_RMPP_FLAG_ACTIVE))
806 		return mad_recv_wc;
807 
808 	if (rmpp_mad->rmpp_hdr.rmpp_version != IB_MGMT_RMPP_VERSION) {
809 		abort_send(agent, rmpp_mad->mad_hdr.tid,
810 			   IB_MGMT_RMPP_STATUS_UNV);
811 		nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_UNV);
812 		goto out;
813 	}
814 
815 	switch (rmpp_mad->rmpp_hdr.rmpp_type) {
816 	case IB_MGMT_RMPP_TYPE_DATA:
817 		return process_rmpp_data(agent, mad_recv_wc);
818 	case IB_MGMT_RMPP_TYPE_ACK:
819 		process_rmpp_ack(agent, mad_recv_wc);
820 		break;
821 	case IB_MGMT_RMPP_TYPE_STOP:
822 		process_rmpp_stop(agent, mad_recv_wc);
823 		break;
824 	case IB_MGMT_RMPP_TYPE_ABORT:
825 		process_rmpp_abort(agent, mad_recv_wc);
826 		break;
827 	default:
828 		abort_send(agent, rmpp_mad->mad_hdr.tid,
829 			   IB_MGMT_RMPP_STATUS_BADT);
830 		nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BADT);
831 		break;
832 	}
833 out:
834 	ib_free_recv_mad(mad_recv_wc);
835 	return NULL;
836 }
837 
838 int ib_send_rmpp_mad(struct ib_mad_send_wr_private *mad_send_wr)
839 {
840 	struct ib_rmpp_mad *rmpp_mad;
841 	int i, total_len, ret;
842 
843 	rmpp_mad = mad_send_wr->send_buf.mad;
844 	if (!(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
845 	      IB_MGMT_RMPP_FLAG_ACTIVE))
846 		return IB_RMPP_RESULT_UNHANDLED;
847 
848 	if (rmpp_mad->rmpp_hdr.rmpp_type != IB_MGMT_RMPP_TYPE_DATA)
849 		return IB_RMPP_RESULT_INTERNAL;
850 
851 	if (mad_send_wr->send_wr.num_sge > 1)
852 		return -EINVAL;		/* TODO: support num_sge > 1 */
853 
854 	mad_send_wr->seg_num = 1;
855 	mad_send_wr->newwin = 1;
856 	mad_send_wr->data_offset = data_offset(rmpp_mad->mad_hdr.mgmt_class);
857 
858 	total_len = 0;
859 	for (i = 0; i < mad_send_wr->send_wr.num_sge; i++)
860 		total_len += mad_send_wr->send_wr.sg_list[i].length;
861 
862         mad_send_wr->total_seg = (total_len - mad_send_wr->data_offset) /
863 			(sizeof(struct ib_rmpp_mad) - mad_send_wr->data_offset);
864 	mad_send_wr->pad = total_len - IB_MGMT_RMPP_HDR -
865 			   be32_to_cpu(rmpp_mad->rmpp_hdr.paylen_newwin);
866 
867 	/* We need to wait for the final ACK even if there isn't a response */
868 	mad_send_wr->refcount += (mad_send_wr->timeout == 0);
869 	ret = send_next_seg(mad_send_wr);
870 	if (!ret)
871 		return IB_RMPP_RESULT_CONSUMED;
872 	return ret;
873 }
874 
875 int ib_process_rmpp_send_wc(struct ib_mad_send_wr_private *mad_send_wr,
876 			    struct ib_mad_send_wc *mad_send_wc)
877 {
878 	struct ib_rmpp_mad *rmpp_mad;
879 	int ret;
880 
881 	rmpp_mad = mad_send_wr->send_buf.mad;
882 	if (!(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
883 	      IB_MGMT_RMPP_FLAG_ACTIVE))
884 		return IB_RMPP_RESULT_UNHANDLED; /* RMPP not active */
885 
886 	if (rmpp_mad->rmpp_hdr.rmpp_type != IB_MGMT_RMPP_TYPE_DATA)
887 		return IB_RMPP_RESULT_INTERNAL;	 /* ACK, STOP, or ABORT */
888 
889 	if (mad_send_wc->status != IB_WC_SUCCESS ||
890 	    mad_send_wr->status != IB_WC_SUCCESS)
891 		return IB_RMPP_RESULT_PROCESSED; /* Canceled or send error */
892 
893 	if (!mad_send_wr->timeout)
894 		return IB_RMPP_RESULT_PROCESSED; /* Response received */
895 
896 	if (mad_send_wr->last_ack == mad_send_wr->total_seg) {
897 		mad_send_wr->timeout =
898 			msecs_to_jiffies(mad_send_wr->send_buf.timeout_ms);
899 		return IB_RMPP_RESULT_PROCESSED; /* Send done */
900 	}
901 
902 	if (mad_send_wr->seg_num > mad_send_wr->newwin ||
903 	    mad_send_wr->seg_num > mad_send_wr->total_seg)
904 		return IB_RMPP_RESULT_PROCESSED; /* Wait for ACK */
905 
906 	ret = send_next_seg(mad_send_wr);
907 	if (ret) {
908 		mad_send_wc->status = IB_WC_GENERAL_ERR;
909 		return IB_RMPP_RESULT_PROCESSED;
910 	}
911 	return IB_RMPP_RESULT_CONSUMED;
912 }
913 
914 int ib_retry_rmpp(struct ib_mad_send_wr_private *mad_send_wr)
915 {
916 	struct ib_rmpp_mad *rmpp_mad;
917 	int ret;
918 
919 	rmpp_mad = mad_send_wr->send_buf.mad;
920 	if (!(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
921 	      IB_MGMT_RMPP_FLAG_ACTIVE))
922 		return IB_RMPP_RESULT_UNHANDLED; /* RMPP not active */
923 
924 	if (mad_send_wr->last_ack == mad_send_wr->total_seg)
925 		return IB_RMPP_RESULT_PROCESSED;
926 
927 	mad_send_wr->seg_num = mad_send_wr->last_ack + 1;
928 	ret = send_next_seg(mad_send_wr);
929 	if (ret)
930 		return IB_RMPP_RESULT_PROCESSED;
931 
932 	return IB_RMPP_RESULT_CONSUMED;
933 }
934