2 * Copyright (c) 2005 Intel Inc. All rights reserved.
3 * Copyright (c) 2005-2006 Voltaire, Inc. All rights reserved.
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:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
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.
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
44 struct mad_rmpp_recv
{
45 struct ib_mad_agent_private
*agent
;
46 struct list_head list
;
47 struct delayed_work timeout_work
;
48 struct delayed_work cleanup_work
;
49 struct completion comp
;
50 enum rmpp_state state
;
55 struct ib_mad_recv_wc
*rmpp_wc
;
56 struct ib_mad_recv_buf
*cur_seg_buf
;
70 static inline void deref_rmpp_recv(struct mad_rmpp_recv
*rmpp_recv
)
72 if (atomic_dec_and_test(&rmpp_recv
->refcount
))
73 complete(&rmpp_recv
->comp
);
76 static void destroy_rmpp_recv(struct mad_rmpp_recv
*rmpp_recv
)
78 deref_rmpp_recv(rmpp_recv
);
79 wait_for_completion(&rmpp_recv
->comp
);
80 ib_destroy_ah(rmpp_recv
->ah
);
84 void ib_cancel_rmpp_recvs(struct ib_mad_agent_private
*agent
)
86 struct mad_rmpp_recv
*rmpp_recv
, *temp_rmpp_recv
;
89 spin_lock_irqsave(&agent
->lock
, flags
);
90 list_for_each_entry(rmpp_recv
, &agent
->rmpp_list
, list
) {
91 if (rmpp_recv
->state
!= RMPP_STATE_COMPLETE
)
92 ib_free_recv_mad(rmpp_recv
->rmpp_wc
);
93 rmpp_recv
->state
= RMPP_STATE_CANCELING
;
95 spin_unlock_irqrestore(&agent
->lock
, flags
);
97 list_for_each_entry(rmpp_recv
, &agent
->rmpp_list
, list
) {
98 cancel_delayed_work(&rmpp_recv
->timeout_work
);
99 cancel_delayed_work(&rmpp_recv
->cleanup_work
);
102 flush_workqueue(agent
->qp_info
->port_priv
->wq
);
104 list_for_each_entry_safe(rmpp_recv
, temp_rmpp_recv
,
105 &agent
->rmpp_list
, list
) {
106 list_del(&rmpp_recv
->list
);
107 destroy_rmpp_recv(rmpp_recv
);
111 static void format_ack(struct ib_mad_send_buf
*msg
,
112 struct ib_rmpp_mad
*data
,
113 struct mad_rmpp_recv
*rmpp_recv
)
115 struct ib_rmpp_mad
*ack
= msg
->mad
;
118 memcpy(ack
, &data
->mad_hdr
, msg
->hdr_len
);
120 ack
->mad_hdr
.method
^= IB_MGMT_METHOD_RESP
;
121 ack
->rmpp_hdr
.rmpp_type
= IB_MGMT_RMPP_TYPE_ACK
;
122 ib_set_rmpp_flags(&ack
->rmpp_hdr
, IB_MGMT_RMPP_FLAG_ACTIVE
);
124 spin_lock_irqsave(&rmpp_recv
->lock
, flags
);
125 rmpp_recv
->last_ack
= rmpp_recv
->seg_num
;
126 ack
->rmpp_hdr
.seg_num
= cpu_to_be32(rmpp_recv
->seg_num
);
127 ack
->rmpp_hdr
.paylen_newwin
= cpu_to_be32(rmpp_recv
->newwin
);
128 spin_unlock_irqrestore(&rmpp_recv
->lock
, flags
);
131 static void ack_recv(struct mad_rmpp_recv
*rmpp_recv
,
132 struct ib_mad_recv_wc
*recv_wc
)
134 struct ib_mad_send_buf
*msg
;
137 hdr_len
= ib_get_mad_data_offset(recv_wc
->recv_buf
.mad
->mad_hdr
.mgmt_class
);
138 msg
= ib_create_send_mad(&rmpp_recv
->agent
->agent
, recv_wc
->wc
->src_qp
,
139 recv_wc
->wc
->pkey_index
, 1, hdr_len
,
144 format_ack(msg
, (struct ib_rmpp_mad
*) recv_wc
->recv_buf
.mad
, rmpp_recv
);
145 msg
->ah
= rmpp_recv
->ah
;
146 ret
= ib_post_send_mad(msg
, NULL
);
148 ib_free_send_mad(msg
);
151 static struct ib_mad_send_buf
*alloc_response_msg(struct ib_mad_agent
*agent
,
152 struct ib_mad_recv_wc
*recv_wc
)
154 struct ib_mad_send_buf
*msg
;
158 ah
= ib_create_ah_from_wc(agent
->qp
->pd
, recv_wc
->wc
,
159 recv_wc
->recv_buf
.grh
, agent
->port_num
);
163 hdr_len
= ib_get_mad_data_offset(recv_wc
->recv_buf
.mad
->mad_hdr
.mgmt_class
);
164 msg
= ib_create_send_mad(agent
, recv_wc
->wc
->src_qp
,
165 recv_wc
->wc
->pkey_index
, 1,
166 hdr_len
, 0, GFP_KERNEL
);
171 msg
->context
[0] = ah
;
177 static void ack_ds_ack(struct ib_mad_agent_private
*agent
,
178 struct ib_mad_recv_wc
*recv_wc
)
180 struct ib_mad_send_buf
*msg
;
181 struct ib_rmpp_mad
*rmpp_mad
;
184 msg
= alloc_response_msg(&agent
->agent
, recv_wc
);
189 memcpy(rmpp_mad
, recv_wc
->recv_buf
.mad
, msg
->hdr_len
);
191 rmpp_mad
->mad_hdr
.method
^= IB_MGMT_METHOD_RESP
;
192 ib_set_rmpp_flags(&rmpp_mad
->rmpp_hdr
, IB_MGMT_RMPP_FLAG_ACTIVE
);
193 rmpp_mad
->rmpp_hdr
.seg_num
= 0;
194 rmpp_mad
->rmpp_hdr
.paylen_newwin
= cpu_to_be32(1);
196 ret
= ib_post_send_mad(msg
, NULL
);
198 ib_destroy_ah(msg
->ah
);
199 ib_free_send_mad(msg
);
203 void ib_rmpp_send_handler(struct ib_mad_send_wc
*mad_send_wc
)
205 if (mad_send_wc
->send_buf
->context
[0] == mad_send_wc
->send_buf
->ah
)
206 ib_destroy_ah(mad_send_wc
->send_buf
->ah
);
207 ib_free_send_mad(mad_send_wc
->send_buf
);
210 static void nack_recv(struct ib_mad_agent_private
*agent
,
211 struct ib_mad_recv_wc
*recv_wc
, u8 rmpp_status
)
213 struct ib_mad_send_buf
*msg
;
214 struct ib_rmpp_mad
*rmpp_mad
;
217 msg
= alloc_response_msg(&agent
->agent
, recv_wc
);
222 memcpy(rmpp_mad
, recv_wc
->recv_buf
.mad
, msg
->hdr_len
);
224 rmpp_mad
->mad_hdr
.method
^= IB_MGMT_METHOD_RESP
;
225 rmpp_mad
->rmpp_hdr
.rmpp_version
= IB_MGMT_RMPP_VERSION
;
226 rmpp_mad
->rmpp_hdr
.rmpp_type
= IB_MGMT_RMPP_TYPE_ABORT
;
227 ib_set_rmpp_flags(&rmpp_mad
->rmpp_hdr
, IB_MGMT_RMPP_FLAG_ACTIVE
);
228 rmpp_mad
->rmpp_hdr
.rmpp_status
= rmpp_status
;
229 rmpp_mad
->rmpp_hdr
.seg_num
= 0;
230 rmpp_mad
->rmpp_hdr
.paylen_newwin
= 0;
232 ret
= ib_post_send_mad(msg
, NULL
);
234 ib_destroy_ah(msg
->ah
);
235 ib_free_send_mad(msg
);
239 static void recv_timeout_handler(struct work_struct
*work
)
241 struct mad_rmpp_recv
*rmpp_recv
=
242 container_of(work
, struct mad_rmpp_recv
, timeout_work
.work
);
243 struct ib_mad_recv_wc
*rmpp_wc
;
246 spin_lock_irqsave(&rmpp_recv
->agent
->lock
, flags
);
247 if (rmpp_recv
->state
!= RMPP_STATE_ACTIVE
) {
248 spin_unlock_irqrestore(&rmpp_recv
->agent
->lock
, flags
);
251 rmpp_recv
->state
= RMPP_STATE_TIMEOUT
;
252 list_del(&rmpp_recv
->list
);
253 spin_unlock_irqrestore(&rmpp_recv
->agent
->lock
, flags
);
255 rmpp_wc
= rmpp_recv
->rmpp_wc
;
256 nack_recv(rmpp_recv
->agent
, rmpp_wc
, IB_MGMT_RMPP_STATUS_T2L
);
257 destroy_rmpp_recv(rmpp_recv
);
258 ib_free_recv_mad(rmpp_wc
);
261 static void recv_cleanup_handler(struct work_struct
*work
)
263 struct mad_rmpp_recv
*rmpp_recv
=
264 container_of(work
, struct mad_rmpp_recv
, cleanup_work
.work
);
267 spin_lock_irqsave(&rmpp_recv
->agent
->lock
, flags
);
268 if (rmpp_recv
->state
== RMPP_STATE_CANCELING
) {
269 spin_unlock_irqrestore(&rmpp_recv
->agent
->lock
, flags
);
272 list_del(&rmpp_recv
->list
);
273 spin_unlock_irqrestore(&rmpp_recv
->agent
->lock
, flags
);
274 destroy_rmpp_recv(rmpp_recv
);
277 static struct mad_rmpp_recv
*
278 create_rmpp_recv(struct ib_mad_agent_private
*agent
,
279 struct ib_mad_recv_wc
*mad_recv_wc
)
281 struct mad_rmpp_recv
*rmpp_recv
;
282 struct ib_mad_hdr
*mad_hdr
;
284 rmpp_recv
= kmalloc(sizeof *rmpp_recv
, GFP_KERNEL
);
288 rmpp_recv
->ah
= ib_create_ah_from_wc(agent
->agent
.qp
->pd
,
290 mad_recv_wc
->recv_buf
.grh
,
291 agent
->agent
.port_num
);
292 if (IS_ERR(rmpp_recv
->ah
))
295 rmpp_recv
->agent
= agent
;
296 init_completion(&rmpp_recv
->comp
);
297 INIT_DELAYED_WORK(&rmpp_recv
->timeout_work
, recv_timeout_handler
);
298 INIT_DELAYED_WORK(&rmpp_recv
->cleanup_work
, recv_cleanup_handler
);
299 spin_lock_init(&rmpp_recv
->lock
);
300 rmpp_recv
->state
= RMPP_STATE_ACTIVE
;
301 atomic_set(&rmpp_recv
->refcount
, 1);
303 rmpp_recv
->rmpp_wc
= mad_recv_wc
;
304 rmpp_recv
->cur_seg_buf
= &mad_recv_wc
->recv_buf
;
305 rmpp_recv
->newwin
= 1;
306 rmpp_recv
->seg_num
= 1;
307 rmpp_recv
->last_ack
= 0;
308 rmpp_recv
->repwin
= 1;
310 mad_hdr
= &mad_recv_wc
->recv_buf
.mad
->mad_hdr
;
311 rmpp_recv
->tid
= mad_hdr
->tid
;
312 rmpp_recv
->src_qp
= mad_recv_wc
->wc
->src_qp
;
313 rmpp_recv
->slid
= mad_recv_wc
->wc
->slid
;
314 rmpp_recv
->mgmt_class
= mad_hdr
->mgmt_class
;
315 rmpp_recv
->class_version
= mad_hdr
->class_version
;
316 rmpp_recv
->method
= mad_hdr
->method
;
319 error
: kfree(rmpp_recv
);
323 static struct mad_rmpp_recv
*
324 find_rmpp_recv(struct ib_mad_agent_private
*agent
,
325 struct ib_mad_recv_wc
*mad_recv_wc
)
327 struct mad_rmpp_recv
*rmpp_recv
;
328 struct ib_mad_hdr
*mad_hdr
= &mad_recv_wc
->recv_buf
.mad
->mad_hdr
;
330 list_for_each_entry(rmpp_recv
, &agent
->rmpp_list
, list
) {
331 if (rmpp_recv
->tid
== mad_hdr
->tid
&&
332 rmpp_recv
->src_qp
== mad_recv_wc
->wc
->src_qp
&&
333 rmpp_recv
->slid
== mad_recv_wc
->wc
->slid
&&
334 rmpp_recv
->mgmt_class
== mad_hdr
->mgmt_class
&&
335 rmpp_recv
->class_version
== mad_hdr
->class_version
&&
336 rmpp_recv
->method
== mad_hdr
->method
)
342 static struct mad_rmpp_recv
*
343 acquire_rmpp_recv(struct ib_mad_agent_private
*agent
,
344 struct ib_mad_recv_wc
*mad_recv_wc
)
346 struct mad_rmpp_recv
*rmpp_recv
;
349 spin_lock_irqsave(&agent
->lock
, flags
);
350 rmpp_recv
= find_rmpp_recv(agent
, mad_recv_wc
);
352 atomic_inc(&rmpp_recv
->refcount
);
353 spin_unlock_irqrestore(&agent
->lock
, flags
);
357 static struct mad_rmpp_recv
*
358 insert_rmpp_recv(struct ib_mad_agent_private
*agent
,
359 struct mad_rmpp_recv
*rmpp_recv
)
361 struct mad_rmpp_recv
*cur_rmpp_recv
;
363 cur_rmpp_recv
= find_rmpp_recv(agent
, rmpp_recv
->rmpp_wc
);
365 list_add_tail(&rmpp_recv
->list
, &agent
->rmpp_list
);
367 return cur_rmpp_recv
;
370 static inline int get_last_flag(struct ib_mad_recv_buf
*seg
)
372 struct ib_rmpp_mad
*rmpp_mad
;
374 rmpp_mad
= (struct ib_rmpp_mad
*) seg
->mad
;
375 return ib_get_rmpp_flags(&rmpp_mad
->rmpp_hdr
) & IB_MGMT_RMPP_FLAG_LAST
;
378 static inline int get_seg_num(struct ib_mad_recv_buf
*seg
)
380 struct ib_rmpp_mad
*rmpp_mad
;
382 rmpp_mad
= (struct ib_rmpp_mad
*) seg
->mad
;
383 return be32_to_cpu(rmpp_mad
->rmpp_hdr
.seg_num
);
386 static inline struct ib_mad_recv_buf
* get_next_seg(struct list_head
*rmpp_list
,
387 struct ib_mad_recv_buf
*seg
)
389 if (seg
->list
.next
== rmpp_list
)
392 return container_of(seg
->list
.next
, struct ib_mad_recv_buf
, list
);
395 static inline int window_size(struct ib_mad_agent_private
*agent
)
397 return max(agent
->qp_info
->recv_queue
.max_active
>> 3, 1);
400 static struct ib_mad_recv_buf
* find_seg_location(struct list_head
*rmpp_list
,
403 struct ib_mad_recv_buf
*seg_buf
;
406 list_for_each_entry_reverse(seg_buf
, rmpp_list
, list
) {
407 cur_seg_num
= get_seg_num(seg_buf
);
408 if (seg_num
> cur_seg_num
)
410 if (seg_num
== cur_seg_num
)
416 static void update_seg_num(struct mad_rmpp_recv
*rmpp_recv
,
417 struct ib_mad_recv_buf
*new_buf
)
419 struct list_head
*rmpp_list
= &rmpp_recv
->rmpp_wc
->rmpp_list
;
421 while (new_buf
&& (get_seg_num(new_buf
) == rmpp_recv
->seg_num
+ 1)) {
422 rmpp_recv
->cur_seg_buf
= new_buf
;
423 rmpp_recv
->seg_num
++;
424 new_buf
= get_next_seg(rmpp_list
, new_buf
);
428 static inline int get_mad_len(struct mad_rmpp_recv
*rmpp_recv
)
430 struct ib_rmpp_mad
*rmpp_mad
;
431 int hdr_size
, data_size
, pad
;
433 rmpp_mad
= (struct ib_rmpp_mad
*)rmpp_recv
->cur_seg_buf
->mad
;
435 hdr_size
= ib_get_mad_data_offset(rmpp_mad
->mad_hdr
.mgmt_class
);
436 data_size
= sizeof(struct ib_rmpp_mad
) - hdr_size
;
437 pad
= IB_MGMT_RMPP_DATA
- be32_to_cpu(rmpp_mad
->rmpp_hdr
.paylen_newwin
);
438 if (pad
> IB_MGMT_RMPP_DATA
|| pad
< 0)
441 return hdr_size
+ rmpp_recv
->seg_num
* data_size
- pad
;
444 static struct ib_mad_recv_wc
* complete_rmpp(struct mad_rmpp_recv
*rmpp_recv
)
446 struct ib_mad_recv_wc
*rmpp_wc
;
448 ack_recv(rmpp_recv
, rmpp_recv
->rmpp_wc
);
449 if (rmpp_recv
->seg_num
> 1)
450 cancel_delayed_work(&rmpp_recv
->timeout_work
);
452 rmpp_wc
= rmpp_recv
->rmpp_wc
;
453 rmpp_wc
->mad_len
= get_mad_len(rmpp_recv
);
454 /* 10 seconds until we can find the packet lifetime */
455 queue_delayed_work(rmpp_recv
->agent
->qp_info
->port_priv
->wq
,
456 &rmpp_recv
->cleanup_work
, msecs_to_jiffies(10000));
460 static struct ib_mad_recv_wc
*
461 continue_rmpp(struct ib_mad_agent_private
*agent
,
462 struct ib_mad_recv_wc
*mad_recv_wc
)
464 struct mad_rmpp_recv
*rmpp_recv
;
465 struct ib_mad_recv_buf
*prev_buf
;
466 struct ib_mad_recv_wc
*done_wc
;
470 rmpp_recv
= acquire_rmpp_recv(agent
, mad_recv_wc
);
474 seg_num
= get_seg_num(&mad_recv_wc
->recv_buf
);
476 spin_lock_irqsave(&rmpp_recv
->lock
, flags
);
477 if ((rmpp_recv
->state
== RMPP_STATE_TIMEOUT
) ||
478 (seg_num
> rmpp_recv
->newwin
))
481 if ((seg_num
<= rmpp_recv
->last_ack
) ||
482 (rmpp_recv
->state
== RMPP_STATE_COMPLETE
)) {
483 spin_unlock_irqrestore(&rmpp_recv
->lock
, flags
);
484 ack_recv(rmpp_recv
, mad_recv_wc
);
488 prev_buf
= find_seg_location(&rmpp_recv
->rmpp_wc
->rmpp_list
, seg_num
);
493 list_add(&mad_recv_wc
->recv_buf
.list
, &prev_buf
->list
);
494 if (rmpp_recv
->cur_seg_buf
== prev_buf
) {
495 update_seg_num(rmpp_recv
, &mad_recv_wc
->recv_buf
);
496 if (get_last_flag(rmpp_recv
->cur_seg_buf
)) {
497 rmpp_recv
->state
= RMPP_STATE_COMPLETE
;
498 spin_unlock_irqrestore(&rmpp_recv
->lock
, flags
);
499 done_wc
= complete_rmpp(rmpp_recv
);
501 } else if (rmpp_recv
->seg_num
== rmpp_recv
->newwin
) {
502 rmpp_recv
->newwin
+= window_size(agent
);
503 spin_unlock_irqrestore(&rmpp_recv
->lock
, flags
);
504 ack_recv(rmpp_recv
, mad_recv_wc
);
508 spin_unlock_irqrestore(&rmpp_recv
->lock
, flags
);
510 deref_rmpp_recv(rmpp_recv
);
513 drop3
: spin_unlock_irqrestore(&rmpp_recv
->lock
, flags
);
514 drop2
: deref_rmpp_recv(rmpp_recv
);
515 drop1
: ib_free_recv_mad(mad_recv_wc
);
519 static struct ib_mad_recv_wc
*
520 start_rmpp(struct ib_mad_agent_private
*agent
,
521 struct ib_mad_recv_wc
*mad_recv_wc
)
523 struct mad_rmpp_recv
*rmpp_recv
;
526 rmpp_recv
= create_rmpp_recv(agent
, mad_recv_wc
);
528 ib_free_recv_mad(mad_recv_wc
);
532 spin_lock_irqsave(&agent
->lock
, flags
);
533 if (insert_rmpp_recv(agent
, rmpp_recv
)) {
534 spin_unlock_irqrestore(&agent
->lock
, flags
);
535 /* duplicate first MAD */
536 destroy_rmpp_recv(rmpp_recv
);
537 return continue_rmpp(agent
, mad_recv_wc
);
539 atomic_inc(&rmpp_recv
->refcount
);
541 if (get_last_flag(&mad_recv_wc
->recv_buf
)) {
542 rmpp_recv
->state
= RMPP_STATE_COMPLETE
;
543 spin_unlock_irqrestore(&agent
->lock
, flags
);
544 complete_rmpp(rmpp_recv
);
546 spin_unlock_irqrestore(&agent
->lock
, flags
);
547 /* 40 seconds until we can find the packet lifetimes */
548 queue_delayed_work(agent
->qp_info
->port_priv
->wq
,
549 &rmpp_recv
->timeout_work
,
550 msecs_to_jiffies(40000));
551 rmpp_recv
->newwin
+= window_size(agent
);
552 ack_recv(rmpp_recv
, mad_recv_wc
);
555 deref_rmpp_recv(rmpp_recv
);
559 static int send_next_seg(struct ib_mad_send_wr_private
*mad_send_wr
)
561 struct ib_rmpp_mad
*rmpp_mad
;
565 rmpp_mad
= mad_send_wr
->send_buf
.mad
;
566 ib_set_rmpp_flags(&rmpp_mad
->rmpp_hdr
, IB_MGMT_RMPP_FLAG_ACTIVE
);
567 rmpp_mad
->rmpp_hdr
.seg_num
= cpu_to_be32(++mad_send_wr
->seg_num
);
569 if (mad_send_wr
->seg_num
== 1) {
570 rmpp_mad
->rmpp_hdr
.rmpp_rtime_flags
|= IB_MGMT_RMPP_FLAG_FIRST
;
571 paylen
= mad_send_wr
->send_buf
.seg_count
* IB_MGMT_RMPP_DATA
-
575 if (mad_send_wr
->seg_num
== mad_send_wr
->send_buf
.seg_count
) {
576 rmpp_mad
->rmpp_hdr
.rmpp_rtime_flags
|= IB_MGMT_RMPP_FLAG_LAST
;
577 paylen
= IB_MGMT_RMPP_DATA
- mad_send_wr
->pad
;
579 rmpp_mad
->rmpp_hdr
.paylen_newwin
= cpu_to_be32(paylen
);
581 /* 2 seconds for an ACK until we can find the packet lifetime */
582 timeout
= mad_send_wr
->send_buf
.timeout_ms
;
583 if (!timeout
|| timeout
> 2000)
584 mad_send_wr
->timeout
= msecs_to_jiffies(2000);
586 return ib_send_mad(mad_send_wr
);
589 static void abort_send(struct ib_mad_agent_private
*agent
,
590 struct ib_mad_recv_wc
*mad_recv_wc
, u8 rmpp_status
)
592 struct ib_mad_send_wr_private
*mad_send_wr
;
593 struct ib_mad_send_wc wc
;
596 spin_lock_irqsave(&agent
->lock
, flags
);
597 mad_send_wr
= ib_find_send_mad(agent
, mad_recv_wc
);
599 goto out
; /* Unmatched send */
601 if ((mad_send_wr
->last_ack
== mad_send_wr
->send_buf
.seg_count
) ||
602 (!mad_send_wr
->timeout
) || (mad_send_wr
->status
!= IB_WC_SUCCESS
))
603 goto out
; /* Send is already done */
605 ib_mark_mad_done(mad_send_wr
);
606 spin_unlock_irqrestore(&agent
->lock
, flags
);
608 wc
.status
= IB_WC_REM_ABORT_ERR
;
609 wc
.vendor_err
= rmpp_status
;
610 wc
.send_buf
= &mad_send_wr
->send_buf
;
611 ib_mad_complete_send_wr(mad_send_wr
, &wc
);
614 spin_unlock_irqrestore(&agent
->lock
, flags
);
617 static inline void adjust_last_ack(struct ib_mad_send_wr_private
*wr
,
620 struct list_head
*list
;
622 wr
->last_ack
= seg_num
;
623 list
= &wr
->last_ack_seg
->list
;
624 list_for_each_entry(wr
->last_ack_seg
, list
, list
)
625 if (wr
->last_ack_seg
->num
== seg_num
)
629 static void process_ds_ack(struct ib_mad_agent_private
*agent
,
630 struct ib_mad_recv_wc
*mad_recv_wc
, int newwin
)
632 struct mad_rmpp_recv
*rmpp_recv
;
634 rmpp_recv
= find_rmpp_recv(agent
, mad_recv_wc
);
635 if (rmpp_recv
&& rmpp_recv
->state
== RMPP_STATE_COMPLETE
)
636 rmpp_recv
->repwin
= newwin
;
639 static void process_rmpp_ack(struct ib_mad_agent_private
*agent
,
640 struct ib_mad_recv_wc
*mad_recv_wc
)
642 struct ib_mad_send_wr_private
*mad_send_wr
;
643 struct ib_rmpp_mad
*rmpp_mad
;
645 int seg_num
, newwin
, ret
;
647 rmpp_mad
= (struct ib_rmpp_mad
*)mad_recv_wc
->recv_buf
.mad
;
648 if (rmpp_mad
->rmpp_hdr
.rmpp_status
) {
649 abort_send(agent
, mad_recv_wc
, IB_MGMT_RMPP_STATUS_BAD_STATUS
);
650 nack_recv(agent
, mad_recv_wc
, IB_MGMT_RMPP_STATUS_BAD_STATUS
);
654 seg_num
= be32_to_cpu(rmpp_mad
->rmpp_hdr
.seg_num
);
655 newwin
= be32_to_cpu(rmpp_mad
->rmpp_hdr
.paylen_newwin
);
656 if (newwin
< seg_num
) {
657 abort_send(agent
, mad_recv_wc
, IB_MGMT_RMPP_STATUS_W2S
);
658 nack_recv(agent
, mad_recv_wc
, IB_MGMT_RMPP_STATUS_W2S
);
662 spin_lock_irqsave(&agent
->lock
, flags
);
663 mad_send_wr
= ib_find_send_mad(agent
, mad_recv_wc
);
666 process_ds_ack(agent
, mad_recv_wc
, newwin
);
667 goto out
; /* Unmatched or DS RMPP ACK */
670 if ((mad_send_wr
->last_ack
== mad_send_wr
->send_buf
.seg_count
) &&
671 (mad_send_wr
->timeout
)) {
672 spin_unlock_irqrestore(&agent
->lock
, flags
);
673 ack_ds_ack(agent
, mad_recv_wc
);
674 return; /* Repeated ACK for DS RMPP transaction */
677 if ((mad_send_wr
->last_ack
== mad_send_wr
->send_buf
.seg_count
) ||
678 (!mad_send_wr
->timeout
) || (mad_send_wr
->status
!= IB_WC_SUCCESS
))
679 goto out
; /* Send is already done */
681 if (seg_num
> mad_send_wr
->send_buf
.seg_count
||
682 seg_num
> mad_send_wr
->newwin
) {
683 spin_unlock_irqrestore(&agent
->lock
, flags
);
684 abort_send(agent
, mad_recv_wc
, IB_MGMT_RMPP_STATUS_S2B
);
685 nack_recv(agent
, mad_recv_wc
, IB_MGMT_RMPP_STATUS_S2B
);
689 if (newwin
< mad_send_wr
->newwin
|| seg_num
< mad_send_wr
->last_ack
)
690 goto out
; /* Old ACK */
692 if (seg_num
> mad_send_wr
->last_ack
) {
693 adjust_last_ack(mad_send_wr
, seg_num
);
694 mad_send_wr
->retries_left
= mad_send_wr
->max_retries
;
696 mad_send_wr
->newwin
= newwin
;
697 if (mad_send_wr
->last_ack
== mad_send_wr
->send_buf
.seg_count
) {
698 /* If no response is expected, the ACK completes the send */
699 if (!mad_send_wr
->send_buf
.timeout_ms
) {
700 struct ib_mad_send_wc wc
;
702 ib_mark_mad_done(mad_send_wr
);
703 spin_unlock_irqrestore(&agent
->lock
, flags
);
705 wc
.status
= IB_WC_SUCCESS
;
707 wc
.send_buf
= &mad_send_wr
->send_buf
;
708 ib_mad_complete_send_wr(mad_send_wr
, &wc
);
711 if (mad_send_wr
->refcount
== 1)
712 ib_reset_mad_timeout(mad_send_wr
,
713 mad_send_wr
->send_buf
.timeout_ms
);
714 spin_unlock_irqrestore(&agent
->lock
, flags
);
715 ack_ds_ack(agent
, mad_recv_wc
);
717 } else if (mad_send_wr
->refcount
== 1 &&
718 mad_send_wr
->seg_num
< mad_send_wr
->newwin
&&
719 mad_send_wr
->seg_num
< mad_send_wr
->send_buf
.seg_count
) {
720 /* Send failure will just result in a timeout/retry */
721 ret
= send_next_seg(mad_send_wr
);
725 mad_send_wr
->refcount
++;
726 list_move_tail(&mad_send_wr
->agent_list
,
727 &mad_send_wr
->mad_agent_priv
->send_list
);
730 spin_unlock_irqrestore(&agent
->lock
, flags
);
733 static struct ib_mad_recv_wc
*
734 process_rmpp_data(struct ib_mad_agent_private
*agent
,
735 struct ib_mad_recv_wc
*mad_recv_wc
)
737 struct ib_rmpp_hdr
*rmpp_hdr
;
740 rmpp_hdr
= &((struct ib_rmpp_mad
*)mad_recv_wc
->recv_buf
.mad
)->rmpp_hdr
;
742 if (rmpp_hdr
->rmpp_status
) {
743 rmpp_status
= IB_MGMT_RMPP_STATUS_BAD_STATUS
;
747 if (rmpp_hdr
->seg_num
== cpu_to_be32(1)) {
748 if (!(ib_get_rmpp_flags(rmpp_hdr
) & IB_MGMT_RMPP_FLAG_FIRST
)) {
749 rmpp_status
= IB_MGMT_RMPP_STATUS_BAD_SEG
;
752 return start_rmpp(agent
, mad_recv_wc
);
754 if (ib_get_rmpp_flags(rmpp_hdr
) & IB_MGMT_RMPP_FLAG_FIRST
) {
755 rmpp_status
= IB_MGMT_RMPP_STATUS_BAD_SEG
;
758 return continue_rmpp(agent
, mad_recv_wc
);
761 nack_recv(agent
, mad_recv_wc
, rmpp_status
);
762 ib_free_recv_mad(mad_recv_wc
);
766 static void process_rmpp_stop(struct ib_mad_agent_private
*agent
,
767 struct ib_mad_recv_wc
*mad_recv_wc
)
769 struct ib_rmpp_mad
*rmpp_mad
;
771 rmpp_mad
= (struct ib_rmpp_mad
*)mad_recv_wc
->recv_buf
.mad
;
773 if (rmpp_mad
->rmpp_hdr
.rmpp_status
!= IB_MGMT_RMPP_STATUS_RESX
) {
774 abort_send(agent
, mad_recv_wc
, IB_MGMT_RMPP_STATUS_BAD_STATUS
);
775 nack_recv(agent
, mad_recv_wc
, IB_MGMT_RMPP_STATUS_BAD_STATUS
);
777 abort_send(agent
, mad_recv_wc
, rmpp_mad
->rmpp_hdr
.rmpp_status
);
780 static void process_rmpp_abort(struct ib_mad_agent_private
*agent
,
781 struct ib_mad_recv_wc
*mad_recv_wc
)
783 struct ib_rmpp_mad
*rmpp_mad
;
785 rmpp_mad
= (struct ib_rmpp_mad
*)mad_recv_wc
->recv_buf
.mad
;
787 if (rmpp_mad
->rmpp_hdr
.rmpp_status
< IB_MGMT_RMPP_STATUS_ABORT_MIN
||
788 rmpp_mad
->rmpp_hdr
.rmpp_status
> IB_MGMT_RMPP_STATUS_ABORT_MAX
) {
789 abort_send(agent
, mad_recv_wc
, IB_MGMT_RMPP_STATUS_BAD_STATUS
);
790 nack_recv(agent
, mad_recv_wc
, IB_MGMT_RMPP_STATUS_BAD_STATUS
);
792 abort_send(agent
, mad_recv_wc
, rmpp_mad
->rmpp_hdr
.rmpp_status
);
795 struct ib_mad_recv_wc
*
796 ib_process_rmpp_recv_wc(struct ib_mad_agent_private
*agent
,
797 struct ib_mad_recv_wc
*mad_recv_wc
)
799 struct ib_rmpp_mad
*rmpp_mad
;
801 rmpp_mad
= (struct ib_rmpp_mad
*)mad_recv_wc
->recv_buf
.mad
;
802 if (!(rmpp_mad
->rmpp_hdr
.rmpp_rtime_flags
& IB_MGMT_RMPP_FLAG_ACTIVE
))
805 if (rmpp_mad
->rmpp_hdr
.rmpp_version
!= IB_MGMT_RMPP_VERSION
) {
806 abort_send(agent
, mad_recv_wc
, IB_MGMT_RMPP_STATUS_UNV
);
807 nack_recv(agent
, mad_recv_wc
, IB_MGMT_RMPP_STATUS_UNV
);
811 switch (rmpp_mad
->rmpp_hdr
.rmpp_type
) {
812 case IB_MGMT_RMPP_TYPE_DATA
:
813 return process_rmpp_data(agent
, mad_recv_wc
);
814 case IB_MGMT_RMPP_TYPE_ACK
:
815 process_rmpp_ack(agent
, mad_recv_wc
);
817 case IB_MGMT_RMPP_TYPE_STOP
:
818 process_rmpp_stop(agent
, mad_recv_wc
);
820 case IB_MGMT_RMPP_TYPE_ABORT
:
821 process_rmpp_abort(agent
, mad_recv_wc
);
824 abort_send(agent
, mad_recv_wc
, IB_MGMT_RMPP_STATUS_BADT
);
825 nack_recv(agent
, mad_recv_wc
, IB_MGMT_RMPP_STATUS_BADT
);
829 ib_free_recv_mad(mad_recv_wc
);
833 static int init_newwin(struct ib_mad_send_wr_private
*mad_send_wr
)
835 struct ib_mad_agent_private
*agent
= mad_send_wr
->mad_agent_priv
;
836 struct ib_mad_hdr
*mad_hdr
= mad_send_wr
->send_buf
.mad
;
837 struct mad_rmpp_recv
*rmpp_recv
;
838 struct ib_ah_attr ah_attr
;
842 if (!(mad_hdr
->method
& IB_MGMT_METHOD_RESP
))
845 spin_lock_irqsave(&agent
->lock
, flags
);
846 list_for_each_entry(rmpp_recv
, &agent
->rmpp_list
, list
) {
847 if (rmpp_recv
->tid
!= mad_hdr
->tid
||
848 rmpp_recv
->mgmt_class
!= mad_hdr
->mgmt_class
||
849 rmpp_recv
->class_version
!= mad_hdr
->class_version
||
850 (rmpp_recv
->method
& IB_MGMT_METHOD_RESP
))
853 if (ib_query_ah(mad_send_wr
->send_buf
.ah
, &ah_attr
))
856 if (rmpp_recv
->slid
== ah_attr
.dlid
) {
857 newwin
= rmpp_recv
->repwin
;
861 spin_unlock_irqrestore(&agent
->lock
, flags
);
866 int ib_send_rmpp_mad(struct ib_mad_send_wr_private
*mad_send_wr
)
868 struct ib_rmpp_mad
*rmpp_mad
;
871 rmpp_mad
= mad_send_wr
->send_buf
.mad
;
872 if (!(ib_get_rmpp_flags(&rmpp_mad
->rmpp_hdr
) &
873 IB_MGMT_RMPP_FLAG_ACTIVE
))
874 return IB_RMPP_RESULT_UNHANDLED
;
876 if (rmpp_mad
->rmpp_hdr
.rmpp_type
!= IB_MGMT_RMPP_TYPE_DATA
) {
877 mad_send_wr
->seg_num
= 1;
878 return IB_RMPP_RESULT_INTERNAL
;
881 mad_send_wr
->newwin
= init_newwin(mad_send_wr
);
883 /* We need to wait for the final ACK even if there isn't a response */
884 mad_send_wr
->refcount
+= (mad_send_wr
->timeout
== 0);
885 ret
= send_next_seg(mad_send_wr
);
887 return IB_RMPP_RESULT_CONSUMED
;
891 int ib_process_rmpp_send_wc(struct ib_mad_send_wr_private
*mad_send_wr
,
892 struct ib_mad_send_wc
*mad_send_wc
)
894 struct ib_rmpp_mad
*rmpp_mad
;
897 rmpp_mad
= mad_send_wr
->send_buf
.mad
;
898 if (!(ib_get_rmpp_flags(&rmpp_mad
->rmpp_hdr
) &
899 IB_MGMT_RMPP_FLAG_ACTIVE
))
900 return IB_RMPP_RESULT_UNHANDLED
; /* RMPP not active */
902 if (rmpp_mad
->rmpp_hdr
.rmpp_type
!= IB_MGMT_RMPP_TYPE_DATA
)
903 return IB_RMPP_RESULT_INTERNAL
; /* ACK, STOP, or ABORT */
905 if (mad_send_wc
->status
!= IB_WC_SUCCESS
||
906 mad_send_wr
->status
!= IB_WC_SUCCESS
)
907 return IB_RMPP_RESULT_PROCESSED
; /* Canceled or send error */
909 if (!mad_send_wr
->timeout
)
910 return IB_RMPP_RESULT_PROCESSED
; /* Response received */
912 if (mad_send_wr
->last_ack
== mad_send_wr
->send_buf
.seg_count
) {
913 mad_send_wr
->timeout
=
914 msecs_to_jiffies(mad_send_wr
->send_buf
.timeout_ms
);
915 return IB_RMPP_RESULT_PROCESSED
; /* Send done */
918 if (mad_send_wr
->seg_num
== mad_send_wr
->newwin
||
919 mad_send_wr
->seg_num
== mad_send_wr
->send_buf
.seg_count
)
920 return IB_RMPP_RESULT_PROCESSED
; /* Wait for ACK */
922 ret
= send_next_seg(mad_send_wr
);
924 mad_send_wc
->status
= IB_WC_GENERAL_ERR
;
925 return IB_RMPP_RESULT_PROCESSED
;
927 return IB_RMPP_RESULT_CONSUMED
;
930 int ib_retry_rmpp(struct ib_mad_send_wr_private
*mad_send_wr
)
932 struct ib_rmpp_mad
*rmpp_mad
;
935 rmpp_mad
= mad_send_wr
->send_buf
.mad
;
936 if (!(ib_get_rmpp_flags(&rmpp_mad
->rmpp_hdr
) &
937 IB_MGMT_RMPP_FLAG_ACTIVE
))
938 return IB_RMPP_RESULT_UNHANDLED
; /* RMPP not active */
940 if (mad_send_wr
->last_ack
== mad_send_wr
->send_buf
.seg_count
)
941 return IB_RMPP_RESULT_PROCESSED
;
943 mad_send_wr
->seg_num
= mad_send_wr
->last_ack
;
944 mad_send_wr
->cur_seg
= mad_send_wr
->last_ack_seg
;
946 ret
= send_next_seg(mad_send_wr
);
948 return IB_RMPP_RESULT_PROCESSED
;
950 return IB_RMPP_RESULT_CONSUMED
;