2 * Copyright (c) 2005 Intel Inc. All rights reserved.
3 * Copyright (c) 2005-2006 Voltaire, Inc. All rights reserved.
4 * Copyright (c) 2014 Intel Corporation. All rights reserved.
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35 #include <linux/slab.h>
46 struct mad_rmpp_recv
{
47 struct ib_mad_agent_private
*agent
;
48 struct list_head list
;
49 struct delayed_work timeout_work
;
50 struct delayed_work cleanup_work
;
51 struct completion comp
;
52 enum rmpp_state state
;
57 struct ib_mad_recv_wc
*rmpp_wc
;
58 struct ib_mad_recv_buf
*cur_seg_buf
;
73 static inline void deref_rmpp_recv(struct mad_rmpp_recv
*rmpp_recv
)
75 if (refcount_dec_and_test(&rmpp_recv
->refcount
))
76 complete(&rmpp_recv
->comp
);
79 static void destroy_rmpp_recv(struct mad_rmpp_recv
*rmpp_recv
)
81 deref_rmpp_recv(rmpp_recv
);
82 wait_for_completion(&rmpp_recv
->comp
);
83 rdma_destroy_ah(rmpp_recv
->ah
, RDMA_DESTROY_AH_SLEEPABLE
);
87 void ib_cancel_rmpp_recvs(struct ib_mad_agent_private
*agent
)
89 struct mad_rmpp_recv
*rmpp_recv
, *temp_rmpp_recv
;
92 spin_lock_irqsave(&agent
->lock
, flags
);
93 list_for_each_entry(rmpp_recv
, &agent
->rmpp_list
, list
) {
94 cancel_delayed_work(&rmpp_recv
->timeout_work
);
95 cancel_delayed_work(&rmpp_recv
->cleanup_work
);
97 spin_unlock_irqrestore(&agent
->lock
, flags
);
99 flush_workqueue(agent
->qp_info
->port_priv
->wq
);
101 list_for_each_entry_safe(rmpp_recv
, temp_rmpp_recv
,
102 &agent
->rmpp_list
, list
) {
103 list_del(&rmpp_recv
->list
);
104 if (rmpp_recv
->state
!= RMPP_STATE_COMPLETE
)
105 ib_free_recv_mad(rmpp_recv
->rmpp_wc
);
106 destroy_rmpp_recv(rmpp_recv
);
110 static void format_ack(struct ib_mad_send_buf
*msg
,
111 struct ib_rmpp_mad
*data
,
112 struct mad_rmpp_recv
*rmpp_recv
)
114 struct ib_rmpp_mad
*ack
= msg
->mad
;
117 memcpy(ack
, &data
->mad_hdr
, msg
->hdr_len
);
119 ack
->mad_hdr
.method
^= IB_MGMT_METHOD_RESP
;
120 ack
->rmpp_hdr
.rmpp_type
= IB_MGMT_RMPP_TYPE_ACK
;
121 ib_set_rmpp_flags(&ack
->rmpp_hdr
, IB_MGMT_RMPP_FLAG_ACTIVE
);
123 spin_lock_irqsave(&rmpp_recv
->lock
, flags
);
124 rmpp_recv
->last_ack
= rmpp_recv
->seg_num
;
125 ack
->rmpp_hdr
.seg_num
= cpu_to_be32(rmpp_recv
->seg_num
);
126 ack
->rmpp_hdr
.paylen_newwin
= cpu_to_be32(rmpp_recv
->newwin
);
127 spin_unlock_irqrestore(&rmpp_recv
->lock
, flags
);
130 static void ack_recv(struct mad_rmpp_recv
*rmpp_recv
,
131 struct ib_mad_recv_wc
*recv_wc
)
133 struct ib_mad_send_buf
*msg
;
136 hdr_len
= ib_get_mad_data_offset(recv_wc
->recv_buf
.mad
->mad_hdr
.mgmt_class
);
137 msg
= ib_create_send_mad(&rmpp_recv
->agent
->agent
, recv_wc
->wc
->src_qp
,
138 recv_wc
->wc
->pkey_index
, 1, hdr_len
,
140 IB_MGMT_BASE_VERSION
);
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
,
167 IB_MGMT_BASE_VERSION
);
169 rdma_destroy_ah(ah
, RDMA_DESTROY_AH_SLEEPABLE
);
172 msg
->context
[0] = ah
;
178 static void ack_ds_ack(struct ib_mad_agent_private
*agent
,
179 struct ib_mad_recv_wc
*recv_wc
)
181 struct ib_mad_send_buf
*msg
;
182 struct ib_rmpp_mad
*rmpp_mad
;
185 msg
= alloc_response_msg(&agent
->agent
, recv_wc
);
190 memcpy(rmpp_mad
, recv_wc
->recv_buf
.mad
, msg
->hdr_len
);
192 rmpp_mad
->mad_hdr
.method
^= IB_MGMT_METHOD_RESP
;
193 ib_set_rmpp_flags(&rmpp_mad
->rmpp_hdr
, IB_MGMT_RMPP_FLAG_ACTIVE
);
194 rmpp_mad
->rmpp_hdr
.seg_num
= 0;
195 rmpp_mad
->rmpp_hdr
.paylen_newwin
= cpu_to_be32(1);
197 ret
= ib_post_send_mad(msg
, NULL
);
199 rdma_destroy_ah(msg
->ah
, RDMA_DESTROY_AH_SLEEPABLE
);
200 ib_free_send_mad(msg
);
204 void ib_rmpp_send_handler(struct ib_mad_send_wc
*mad_send_wc
)
206 if (mad_send_wc
->send_buf
->context
[0] == mad_send_wc
->send_buf
->ah
)
207 rdma_destroy_ah(mad_send_wc
->send_buf
->ah
,
208 RDMA_DESTROY_AH_SLEEPABLE
);
209 ib_free_send_mad(mad_send_wc
->send_buf
);
212 static void nack_recv(struct ib_mad_agent_private
*agent
,
213 struct ib_mad_recv_wc
*recv_wc
, u8 rmpp_status
)
215 struct ib_mad_send_buf
*msg
;
216 struct ib_rmpp_mad
*rmpp_mad
;
219 msg
= alloc_response_msg(&agent
->agent
, recv_wc
);
224 memcpy(rmpp_mad
, recv_wc
->recv_buf
.mad
, msg
->hdr_len
);
226 rmpp_mad
->mad_hdr
.method
^= IB_MGMT_METHOD_RESP
;
227 rmpp_mad
->rmpp_hdr
.rmpp_version
= IB_MGMT_RMPP_VERSION
;
228 rmpp_mad
->rmpp_hdr
.rmpp_type
= IB_MGMT_RMPP_TYPE_ABORT
;
229 ib_set_rmpp_flags(&rmpp_mad
->rmpp_hdr
, IB_MGMT_RMPP_FLAG_ACTIVE
);
230 rmpp_mad
->rmpp_hdr
.rmpp_status
= rmpp_status
;
231 rmpp_mad
->rmpp_hdr
.seg_num
= 0;
232 rmpp_mad
->rmpp_hdr
.paylen_newwin
= 0;
234 ret
= ib_post_send_mad(msg
, NULL
);
236 rdma_destroy_ah(msg
->ah
, RDMA_DESTROY_AH_SLEEPABLE
);
237 ib_free_send_mad(msg
);
241 static void recv_timeout_handler(struct work_struct
*work
)
243 struct mad_rmpp_recv
*rmpp_recv
=
244 container_of(work
, struct mad_rmpp_recv
, timeout_work
.work
);
245 struct ib_mad_recv_wc
*rmpp_wc
;
248 spin_lock_irqsave(&rmpp_recv
->agent
->lock
, flags
);
249 if (rmpp_recv
->state
!= RMPP_STATE_ACTIVE
) {
250 spin_unlock_irqrestore(&rmpp_recv
->agent
->lock
, flags
);
253 rmpp_recv
->state
= RMPP_STATE_TIMEOUT
;
254 list_del(&rmpp_recv
->list
);
255 spin_unlock_irqrestore(&rmpp_recv
->agent
->lock
, flags
);
257 rmpp_wc
= rmpp_recv
->rmpp_wc
;
258 nack_recv(rmpp_recv
->agent
, rmpp_wc
, IB_MGMT_RMPP_STATUS_T2L
);
259 destroy_rmpp_recv(rmpp_recv
);
260 ib_free_recv_mad(rmpp_wc
);
263 static void recv_cleanup_handler(struct work_struct
*work
)
265 struct mad_rmpp_recv
*rmpp_recv
=
266 container_of(work
, struct mad_rmpp_recv
, cleanup_work
.work
);
269 spin_lock_irqsave(&rmpp_recv
->agent
->lock
, flags
);
270 list_del(&rmpp_recv
->list
);
271 spin_unlock_irqrestore(&rmpp_recv
->agent
->lock
, flags
);
272 destroy_rmpp_recv(rmpp_recv
);
275 static struct mad_rmpp_recv
*
276 create_rmpp_recv(struct ib_mad_agent_private
*agent
,
277 struct ib_mad_recv_wc
*mad_recv_wc
)
279 struct mad_rmpp_recv
*rmpp_recv
;
280 struct ib_mad_hdr
*mad_hdr
;
282 rmpp_recv
= kmalloc(sizeof *rmpp_recv
, GFP_KERNEL
);
286 rmpp_recv
->ah
= ib_create_ah_from_wc(agent
->agent
.qp
->pd
,
288 mad_recv_wc
->recv_buf
.grh
,
289 agent
->agent
.port_num
);
290 if (IS_ERR(rmpp_recv
->ah
))
293 rmpp_recv
->agent
= agent
;
294 init_completion(&rmpp_recv
->comp
);
295 INIT_DELAYED_WORK(&rmpp_recv
->timeout_work
, recv_timeout_handler
);
296 INIT_DELAYED_WORK(&rmpp_recv
->cleanup_work
, recv_cleanup_handler
);
297 spin_lock_init(&rmpp_recv
->lock
);
298 rmpp_recv
->state
= RMPP_STATE_ACTIVE
;
299 refcount_set(&rmpp_recv
->refcount
, 1);
301 rmpp_recv
->rmpp_wc
= mad_recv_wc
;
302 rmpp_recv
->cur_seg_buf
= &mad_recv_wc
->recv_buf
;
303 rmpp_recv
->newwin
= 1;
304 rmpp_recv
->seg_num
= 1;
305 rmpp_recv
->last_ack
= 0;
306 rmpp_recv
->repwin
= 1;
308 mad_hdr
= &mad_recv_wc
->recv_buf
.mad
->mad_hdr
;
309 rmpp_recv
->tid
= mad_hdr
->tid
;
310 rmpp_recv
->src_qp
= mad_recv_wc
->wc
->src_qp
;
311 rmpp_recv
->slid
= mad_recv_wc
->wc
->slid
;
312 rmpp_recv
->mgmt_class
= mad_hdr
->mgmt_class
;
313 rmpp_recv
->class_version
= mad_hdr
->class_version
;
314 rmpp_recv
->method
= mad_hdr
->method
;
315 rmpp_recv
->base_version
= mad_hdr
->base_version
;
318 error
: kfree(rmpp_recv
);
322 static struct mad_rmpp_recv
*
323 find_rmpp_recv(struct ib_mad_agent_private
*agent
,
324 struct ib_mad_recv_wc
*mad_recv_wc
)
326 struct mad_rmpp_recv
*rmpp_recv
;
327 struct ib_mad_hdr
*mad_hdr
= &mad_recv_wc
->recv_buf
.mad
->mad_hdr
;
329 list_for_each_entry(rmpp_recv
, &agent
->rmpp_list
, list
) {
330 if (rmpp_recv
->tid
== mad_hdr
->tid
&&
331 rmpp_recv
->src_qp
== mad_recv_wc
->wc
->src_qp
&&
332 rmpp_recv
->slid
== mad_recv_wc
->wc
->slid
&&
333 rmpp_recv
->mgmt_class
== mad_hdr
->mgmt_class
&&
334 rmpp_recv
->class_version
== mad_hdr
->class_version
&&
335 rmpp_recv
->method
== mad_hdr
->method
)
341 static struct mad_rmpp_recv
*
342 acquire_rmpp_recv(struct ib_mad_agent_private
*agent
,
343 struct ib_mad_recv_wc
*mad_recv_wc
)
345 struct mad_rmpp_recv
*rmpp_recv
;
348 spin_lock_irqsave(&agent
->lock
, flags
);
349 rmpp_recv
= find_rmpp_recv(agent
, mad_recv_wc
);
351 refcount_inc(&rmpp_recv
->refcount
);
352 spin_unlock_irqrestore(&agent
->lock
, flags
);
356 static struct mad_rmpp_recv
*
357 insert_rmpp_recv(struct ib_mad_agent_private
*agent
,
358 struct mad_rmpp_recv
*rmpp_recv
)
360 struct mad_rmpp_recv
*cur_rmpp_recv
;
362 cur_rmpp_recv
= find_rmpp_recv(agent
, rmpp_recv
->rmpp_wc
);
364 list_add_tail(&rmpp_recv
->list
, &agent
->rmpp_list
);
366 return cur_rmpp_recv
;
369 static inline int get_last_flag(struct ib_mad_recv_buf
*seg
)
371 struct ib_rmpp_mad
*rmpp_mad
;
373 rmpp_mad
= (struct ib_rmpp_mad
*) seg
->mad
;
374 return ib_get_rmpp_flags(&rmpp_mad
->rmpp_hdr
) & IB_MGMT_RMPP_FLAG_LAST
;
377 static inline int get_seg_num(struct ib_mad_recv_buf
*seg
)
379 struct ib_rmpp_mad
*rmpp_mad
;
381 rmpp_mad
= (struct ib_rmpp_mad
*) seg
->mad
;
382 return be32_to_cpu(rmpp_mad
->rmpp_hdr
.seg_num
);
385 static inline struct ib_mad_recv_buf
*get_next_seg(struct list_head
*rmpp_list
,
386 struct ib_mad_recv_buf
*seg
)
388 if (seg
->list
.next
== rmpp_list
)
391 return container_of(seg
->list
.next
, struct ib_mad_recv_buf
, list
);
394 static inline int window_size(struct ib_mad_agent_private
*agent
)
396 return max(agent
->qp_info
->recv_queue
.max_active
>> 3, 1);
399 static struct ib_mad_recv_buf
*find_seg_location(struct list_head
*rmpp_list
,
402 struct ib_mad_recv_buf
*seg_buf
;
405 list_for_each_entry_reverse(seg_buf
, rmpp_list
, list
) {
406 cur_seg_num
= get_seg_num(seg_buf
);
407 if (seg_num
> cur_seg_num
)
409 if (seg_num
== cur_seg_num
)
415 static void update_seg_num(struct mad_rmpp_recv
*rmpp_recv
,
416 struct ib_mad_recv_buf
*new_buf
)
418 struct list_head
*rmpp_list
= &rmpp_recv
->rmpp_wc
->rmpp_list
;
420 while (new_buf
&& (get_seg_num(new_buf
) == rmpp_recv
->seg_num
+ 1)) {
421 rmpp_recv
->cur_seg_buf
= new_buf
;
422 rmpp_recv
->seg_num
++;
423 new_buf
= get_next_seg(rmpp_list
, new_buf
);
427 static inline int get_mad_len(struct mad_rmpp_recv
*rmpp_recv
)
429 struct ib_rmpp_mad
*rmpp_mad
;
430 int hdr_size
, data_size
, pad
;
431 bool opa
= rdma_cap_opa_mad(rmpp_recv
->agent
->qp_info
->port_priv
->device
,
432 rmpp_recv
->agent
->qp_info
->port_priv
->port_num
);
434 rmpp_mad
= (struct ib_rmpp_mad
*)rmpp_recv
->cur_seg_buf
->mad
;
436 hdr_size
= ib_get_mad_data_offset(rmpp_mad
->mad_hdr
.mgmt_class
);
437 if (opa
&& rmpp_recv
->base_version
== OPA_MGMT_BASE_VERSION
) {
438 data_size
= sizeof(struct opa_rmpp_mad
) - hdr_size
;
439 pad
= OPA_MGMT_RMPP_DATA
- be32_to_cpu(rmpp_mad
->rmpp_hdr
.paylen_newwin
);
440 if (pad
> OPA_MGMT_RMPP_DATA
|| pad
< 0)
443 data_size
= sizeof(struct ib_rmpp_mad
) - hdr_size
;
444 pad
= IB_MGMT_RMPP_DATA
- be32_to_cpu(rmpp_mad
->rmpp_hdr
.paylen_newwin
);
445 if (pad
> IB_MGMT_RMPP_DATA
|| pad
< 0)
449 return hdr_size
+ rmpp_recv
->seg_num
* data_size
- pad
;
452 static struct ib_mad_recv_wc
*complete_rmpp(struct mad_rmpp_recv
*rmpp_recv
)
454 struct ib_mad_recv_wc
*rmpp_wc
;
456 ack_recv(rmpp_recv
, rmpp_recv
->rmpp_wc
);
457 if (rmpp_recv
->seg_num
> 1)
458 cancel_delayed_work(&rmpp_recv
->timeout_work
);
460 rmpp_wc
= rmpp_recv
->rmpp_wc
;
461 rmpp_wc
->mad_len
= get_mad_len(rmpp_recv
);
462 /* 10 seconds until we can find the packet lifetime */
463 queue_delayed_work(rmpp_recv
->agent
->qp_info
->port_priv
->wq
,
464 &rmpp_recv
->cleanup_work
, msecs_to_jiffies(10000));
468 static struct ib_mad_recv_wc
*
469 continue_rmpp(struct ib_mad_agent_private
*agent
,
470 struct ib_mad_recv_wc
*mad_recv_wc
)
472 struct mad_rmpp_recv
*rmpp_recv
;
473 struct ib_mad_recv_buf
*prev_buf
;
474 struct ib_mad_recv_wc
*done_wc
;
478 rmpp_recv
= acquire_rmpp_recv(agent
, mad_recv_wc
);
482 seg_num
= get_seg_num(&mad_recv_wc
->recv_buf
);
484 spin_lock_irqsave(&rmpp_recv
->lock
, flags
);
485 if ((rmpp_recv
->state
== RMPP_STATE_TIMEOUT
) ||
486 (seg_num
> rmpp_recv
->newwin
))
489 if ((seg_num
<= rmpp_recv
->last_ack
) ||
490 (rmpp_recv
->state
== RMPP_STATE_COMPLETE
)) {
491 spin_unlock_irqrestore(&rmpp_recv
->lock
, flags
);
492 ack_recv(rmpp_recv
, mad_recv_wc
);
496 prev_buf
= find_seg_location(&rmpp_recv
->rmpp_wc
->rmpp_list
, seg_num
);
501 list_add(&mad_recv_wc
->recv_buf
.list
, &prev_buf
->list
);
502 if (rmpp_recv
->cur_seg_buf
== prev_buf
) {
503 update_seg_num(rmpp_recv
, &mad_recv_wc
->recv_buf
);
504 if (get_last_flag(rmpp_recv
->cur_seg_buf
)) {
505 rmpp_recv
->state
= RMPP_STATE_COMPLETE
;
506 spin_unlock_irqrestore(&rmpp_recv
->lock
, flags
);
507 done_wc
= complete_rmpp(rmpp_recv
);
509 } else if (rmpp_recv
->seg_num
== rmpp_recv
->newwin
) {
510 rmpp_recv
->newwin
+= window_size(agent
);
511 spin_unlock_irqrestore(&rmpp_recv
->lock
, flags
);
512 ack_recv(rmpp_recv
, mad_recv_wc
);
516 spin_unlock_irqrestore(&rmpp_recv
->lock
, flags
);
518 deref_rmpp_recv(rmpp_recv
);
521 drop3
: spin_unlock_irqrestore(&rmpp_recv
->lock
, flags
);
522 drop2
: deref_rmpp_recv(rmpp_recv
);
523 drop1
: ib_free_recv_mad(mad_recv_wc
);
527 static struct ib_mad_recv_wc
*
528 start_rmpp(struct ib_mad_agent_private
*agent
,
529 struct ib_mad_recv_wc
*mad_recv_wc
)
531 struct mad_rmpp_recv
*rmpp_recv
;
534 rmpp_recv
= create_rmpp_recv(agent
, mad_recv_wc
);
536 ib_free_recv_mad(mad_recv_wc
);
540 spin_lock_irqsave(&agent
->lock
, flags
);
541 if (insert_rmpp_recv(agent
, rmpp_recv
)) {
542 spin_unlock_irqrestore(&agent
->lock
, flags
);
543 /* duplicate first MAD */
544 destroy_rmpp_recv(rmpp_recv
);
545 return continue_rmpp(agent
, mad_recv_wc
);
547 refcount_inc(&rmpp_recv
->refcount
);
549 if (get_last_flag(&mad_recv_wc
->recv_buf
)) {
550 rmpp_recv
->state
= RMPP_STATE_COMPLETE
;
551 spin_unlock_irqrestore(&agent
->lock
, flags
);
552 complete_rmpp(rmpp_recv
);
554 spin_unlock_irqrestore(&agent
->lock
, flags
);
555 /* 40 seconds until we can find the packet lifetimes */
556 queue_delayed_work(agent
->qp_info
->port_priv
->wq
,
557 &rmpp_recv
->timeout_work
,
558 msecs_to_jiffies(40000));
559 rmpp_recv
->newwin
+= window_size(agent
);
560 ack_recv(rmpp_recv
, mad_recv_wc
);
563 deref_rmpp_recv(rmpp_recv
);
567 static int send_next_seg(struct ib_mad_send_wr_private
*mad_send_wr
)
569 struct ib_rmpp_mad
*rmpp_mad
;
573 rmpp_mad
= mad_send_wr
->send_buf
.mad
;
574 ib_set_rmpp_flags(&rmpp_mad
->rmpp_hdr
, IB_MGMT_RMPP_FLAG_ACTIVE
);
575 rmpp_mad
->rmpp_hdr
.seg_num
= cpu_to_be32(++mad_send_wr
->seg_num
);
577 if (mad_send_wr
->seg_num
== 1) {
578 rmpp_mad
->rmpp_hdr
.rmpp_rtime_flags
|= IB_MGMT_RMPP_FLAG_FIRST
;
579 paylen
= (mad_send_wr
->send_buf
.seg_count
*
580 mad_send_wr
->send_buf
.seg_rmpp_size
) -
584 if (mad_send_wr
->seg_num
== mad_send_wr
->send_buf
.seg_count
) {
585 rmpp_mad
->rmpp_hdr
.rmpp_rtime_flags
|= IB_MGMT_RMPP_FLAG_LAST
;
586 paylen
= mad_send_wr
->send_buf
.seg_rmpp_size
- mad_send_wr
->pad
;
588 rmpp_mad
->rmpp_hdr
.paylen_newwin
= cpu_to_be32(paylen
);
590 /* 2 seconds for an ACK until we can find the packet lifetime */
591 timeout
= mad_send_wr
->send_buf
.timeout_ms
;
592 if (!timeout
|| timeout
> 2000)
593 mad_send_wr
->timeout
= msecs_to_jiffies(2000);
595 return ib_send_mad(mad_send_wr
);
598 static void abort_send(struct ib_mad_agent_private
*agent
,
599 struct ib_mad_recv_wc
*mad_recv_wc
, u8 rmpp_status
)
601 struct ib_mad_send_wr_private
*mad_send_wr
;
602 struct ib_mad_send_wc wc
;
605 spin_lock_irqsave(&agent
->lock
, flags
);
606 mad_send_wr
= ib_find_send_mad(agent
, mad_recv_wc
);
608 goto out
; /* Unmatched send */
610 if ((mad_send_wr
->last_ack
== mad_send_wr
->send_buf
.seg_count
) ||
611 (!mad_send_wr
->timeout
) || (mad_send_wr
->status
!= IB_WC_SUCCESS
))
612 goto out
; /* Send is already done */
614 ib_mark_mad_done(mad_send_wr
);
615 spin_unlock_irqrestore(&agent
->lock
, flags
);
617 wc
.status
= IB_WC_REM_ABORT_ERR
;
618 wc
.vendor_err
= rmpp_status
;
619 wc
.send_buf
= &mad_send_wr
->send_buf
;
620 ib_mad_complete_send_wr(mad_send_wr
, &wc
);
623 spin_unlock_irqrestore(&agent
->lock
, flags
);
626 static inline void adjust_last_ack(struct ib_mad_send_wr_private
*wr
,
629 struct list_head
*list
;
631 wr
->last_ack
= seg_num
;
632 list
= &wr
->last_ack_seg
->list
;
633 list_for_each_entry(wr
->last_ack_seg
, list
, list
)
634 if (wr
->last_ack_seg
->num
== seg_num
)
638 static void process_ds_ack(struct ib_mad_agent_private
*agent
,
639 struct ib_mad_recv_wc
*mad_recv_wc
, int newwin
)
641 struct mad_rmpp_recv
*rmpp_recv
;
643 rmpp_recv
= find_rmpp_recv(agent
, mad_recv_wc
);
644 if (rmpp_recv
&& rmpp_recv
->state
== RMPP_STATE_COMPLETE
)
645 rmpp_recv
->repwin
= newwin
;
648 static void process_rmpp_ack(struct ib_mad_agent_private
*agent
,
649 struct ib_mad_recv_wc
*mad_recv_wc
)
651 struct ib_mad_send_wr_private
*mad_send_wr
;
652 struct ib_rmpp_mad
*rmpp_mad
;
654 int seg_num
, newwin
, ret
;
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
, mad_recv_wc
, IB_MGMT_RMPP_STATUS_BAD_STATUS
);
659 nack_recv(agent
, mad_recv_wc
, IB_MGMT_RMPP_STATUS_BAD_STATUS
);
663 seg_num
= be32_to_cpu(rmpp_mad
->rmpp_hdr
.seg_num
);
664 newwin
= be32_to_cpu(rmpp_mad
->rmpp_hdr
.paylen_newwin
);
665 if (newwin
< seg_num
) {
666 abort_send(agent
, mad_recv_wc
, IB_MGMT_RMPP_STATUS_W2S
);
667 nack_recv(agent
, mad_recv_wc
, IB_MGMT_RMPP_STATUS_W2S
);
671 spin_lock_irqsave(&agent
->lock
, flags
);
672 mad_send_wr
= ib_find_send_mad(agent
, mad_recv_wc
);
675 process_ds_ack(agent
, mad_recv_wc
, newwin
);
676 goto out
; /* Unmatched or DS RMPP ACK */
679 if ((mad_send_wr
->last_ack
== mad_send_wr
->send_buf
.seg_count
) &&
680 (mad_send_wr
->timeout
)) {
681 spin_unlock_irqrestore(&agent
->lock
, flags
);
682 ack_ds_ack(agent
, mad_recv_wc
);
683 return; /* Repeated ACK for DS RMPP transaction */
686 if ((mad_send_wr
->last_ack
== mad_send_wr
->send_buf
.seg_count
) ||
687 (!mad_send_wr
->timeout
) || (mad_send_wr
->status
!= IB_WC_SUCCESS
))
688 goto out
; /* Send is already done */
690 if (seg_num
> mad_send_wr
->send_buf
.seg_count
||
691 seg_num
> mad_send_wr
->newwin
) {
692 spin_unlock_irqrestore(&agent
->lock
, flags
);
693 abort_send(agent
, mad_recv_wc
, IB_MGMT_RMPP_STATUS_S2B
);
694 nack_recv(agent
, mad_recv_wc
, IB_MGMT_RMPP_STATUS_S2B
);
698 if (newwin
< mad_send_wr
->newwin
|| seg_num
< mad_send_wr
->last_ack
)
699 goto out
; /* Old ACK */
701 if (seg_num
> mad_send_wr
->last_ack
) {
702 adjust_last_ack(mad_send_wr
, seg_num
);
703 mad_send_wr
->retries_left
= mad_send_wr
->max_retries
;
705 mad_send_wr
->newwin
= newwin
;
706 if (mad_send_wr
->last_ack
== mad_send_wr
->send_buf
.seg_count
) {
707 /* If no response is expected, the ACK completes the send */
708 if (!mad_send_wr
->send_buf
.timeout_ms
) {
709 struct ib_mad_send_wc wc
;
711 ib_mark_mad_done(mad_send_wr
);
712 spin_unlock_irqrestore(&agent
->lock
, flags
);
714 wc
.status
= IB_WC_SUCCESS
;
716 wc
.send_buf
= &mad_send_wr
->send_buf
;
717 ib_mad_complete_send_wr(mad_send_wr
, &wc
);
720 if (mad_send_wr
->refcount
== 1)
721 ib_reset_mad_timeout(mad_send_wr
,
722 mad_send_wr
->send_buf
.timeout_ms
);
723 spin_unlock_irqrestore(&agent
->lock
, flags
);
724 ack_ds_ack(agent
, mad_recv_wc
);
726 } else if (mad_send_wr
->refcount
== 1 &&
727 mad_send_wr
->seg_num
< mad_send_wr
->newwin
&&
728 mad_send_wr
->seg_num
< mad_send_wr
->send_buf
.seg_count
) {
729 /* Send failure will just result in a timeout/retry */
730 ret
= send_next_seg(mad_send_wr
);
734 mad_send_wr
->refcount
++;
735 list_move_tail(&mad_send_wr
->agent_list
,
736 &mad_send_wr
->mad_agent_priv
->send_list
);
739 spin_unlock_irqrestore(&agent
->lock
, flags
);
742 static struct ib_mad_recv_wc
*
743 process_rmpp_data(struct ib_mad_agent_private
*agent
,
744 struct ib_mad_recv_wc
*mad_recv_wc
)
746 struct ib_rmpp_hdr
*rmpp_hdr
;
749 rmpp_hdr
= &((struct ib_rmpp_mad
*)mad_recv_wc
->recv_buf
.mad
)->rmpp_hdr
;
751 if (rmpp_hdr
->rmpp_status
) {
752 rmpp_status
= IB_MGMT_RMPP_STATUS_BAD_STATUS
;
756 if (rmpp_hdr
->seg_num
== cpu_to_be32(1)) {
757 if (!(ib_get_rmpp_flags(rmpp_hdr
) & IB_MGMT_RMPP_FLAG_FIRST
)) {
758 rmpp_status
= IB_MGMT_RMPP_STATUS_BAD_SEG
;
761 return start_rmpp(agent
, mad_recv_wc
);
763 if (ib_get_rmpp_flags(rmpp_hdr
) & IB_MGMT_RMPP_FLAG_FIRST
) {
764 rmpp_status
= IB_MGMT_RMPP_STATUS_BAD_SEG
;
767 return continue_rmpp(agent
, mad_recv_wc
);
770 nack_recv(agent
, mad_recv_wc
, rmpp_status
);
771 ib_free_recv_mad(mad_recv_wc
);
775 static void process_rmpp_stop(struct ib_mad_agent_private
*agent
,
776 struct ib_mad_recv_wc
*mad_recv_wc
)
778 struct ib_rmpp_mad
*rmpp_mad
;
780 rmpp_mad
= (struct ib_rmpp_mad
*)mad_recv_wc
->recv_buf
.mad
;
782 if (rmpp_mad
->rmpp_hdr
.rmpp_status
!= IB_MGMT_RMPP_STATUS_RESX
) {
783 abort_send(agent
, mad_recv_wc
, IB_MGMT_RMPP_STATUS_BAD_STATUS
);
784 nack_recv(agent
, mad_recv_wc
, IB_MGMT_RMPP_STATUS_BAD_STATUS
);
786 abort_send(agent
, mad_recv_wc
, rmpp_mad
->rmpp_hdr
.rmpp_status
);
789 static void process_rmpp_abort(struct ib_mad_agent_private
*agent
,
790 struct ib_mad_recv_wc
*mad_recv_wc
)
792 struct ib_rmpp_mad
*rmpp_mad
;
794 rmpp_mad
= (struct ib_rmpp_mad
*)mad_recv_wc
->recv_buf
.mad
;
796 if (rmpp_mad
->rmpp_hdr
.rmpp_status
< IB_MGMT_RMPP_STATUS_ABORT_MIN
||
797 rmpp_mad
->rmpp_hdr
.rmpp_status
> IB_MGMT_RMPP_STATUS_ABORT_MAX
) {
798 abort_send(agent
, mad_recv_wc
, IB_MGMT_RMPP_STATUS_BAD_STATUS
);
799 nack_recv(agent
, mad_recv_wc
, IB_MGMT_RMPP_STATUS_BAD_STATUS
);
801 abort_send(agent
, mad_recv_wc
, rmpp_mad
->rmpp_hdr
.rmpp_status
);
804 struct ib_mad_recv_wc
*
805 ib_process_rmpp_recv_wc(struct ib_mad_agent_private
*agent
,
806 struct ib_mad_recv_wc
*mad_recv_wc
)
808 struct ib_rmpp_mad
*rmpp_mad
;
810 rmpp_mad
= (struct ib_rmpp_mad
*)mad_recv_wc
->recv_buf
.mad
;
811 if (!(rmpp_mad
->rmpp_hdr
.rmpp_rtime_flags
& IB_MGMT_RMPP_FLAG_ACTIVE
))
814 if (rmpp_mad
->rmpp_hdr
.rmpp_version
!= IB_MGMT_RMPP_VERSION
) {
815 abort_send(agent
, mad_recv_wc
, IB_MGMT_RMPP_STATUS_UNV
);
816 nack_recv(agent
, mad_recv_wc
, IB_MGMT_RMPP_STATUS_UNV
);
820 switch (rmpp_mad
->rmpp_hdr
.rmpp_type
) {
821 case IB_MGMT_RMPP_TYPE_DATA
:
822 return process_rmpp_data(agent
, mad_recv_wc
);
823 case IB_MGMT_RMPP_TYPE_ACK
:
824 process_rmpp_ack(agent
, mad_recv_wc
);
826 case IB_MGMT_RMPP_TYPE_STOP
:
827 process_rmpp_stop(agent
, mad_recv_wc
);
829 case IB_MGMT_RMPP_TYPE_ABORT
:
830 process_rmpp_abort(agent
, mad_recv_wc
);
833 abort_send(agent
, mad_recv_wc
, IB_MGMT_RMPP_STATUS_BADT
);
834 nack_recv(agent
, mad_recv_wc
, IB_MGMT_RMPP_STATUS_BADT
);
838 ib_free_recv_mad(mad_recv_wc
);
842 static int init_newwin(struct ib_mad_send_wr_private
*mad_send_wr
)
844 struct ib_mad_agent_private
*agent
= mad_send_wr
->mad_agent_priv
;
845 struct ib_mad_hdr
*mad_hdr
= mad_send_wr
->send_buf
.mad
;
846 struct mad_rmpp_recv
*rmpp_recv
;
847 struct rdma_ah_attr ah_attr
;
851 if (!(mad_hdr
->method
& IB_MGMT_METHOD_RESP
))
854 spin_lock_irqsave(&agent
->lock
, flags
);
855 list_for_each_entry(rmpp_recv
, &agent
->rmpp_list
, list
) {
856 if (rmpp_recv
->tid
!= mad_hdr
->tid
||
857 rmpp_recv
->mgmt_class
!= mad_hdr
->mgmt_class
||
858 rmpp_recv
->class_version
!= mad_hdr
->class_version
||
859 (rmpp_recv
->method
& IB_MGMT_METHOD_RESP
))
862 if (rdma_query_ah(mad_send_wr
->send_buf
.ah
, &ah_attr
))
865 if (rmpp_recv
->slid
== rdma_ah_get_dlid(&ah_attr
)) {
866 newwin
= rmpp_recv
->repwin
;
870 spin_unlock_irqrestore(&agent
->lock
, flags
);
875 int ib_send_rmpp_mad(struct ib_mad_send_wr_private
*mad_send_wr
)
877 struct ib_rmpp_mad
*rmpp_mad
;
880 rmpp_mad
= mad_send_wr
->send_buf
.mad
;
881 if (!(ib_get_rmpp_flags(&rmpp_mad
->rmpp_hdr
) &
882 IB_MGMT_RMPP_FLAG_ACTIVE
))
883 return IB_RMPP_RESULT_UNHANDLED
;
885 if (rmpp_mad
->rmpp_hdr
.rmpp_type
!= IB_MGMT_RMPP_TYPE_DATA
) {
886 mad_send_wr
->seg_num
= 1;
887 return IB_RMPP_RESULT_INTERNAL
;
890 mad_send_wr
->newwin
= init_newwin(mad_send_wr
);
892 /* We need to wait for the final ACK even if there isn't a response */
893 mad_send_wr
->refcount
+= (mad_send_wr
->timeout
== 0);
894 ret
= send_next_seg(mad_send_wr
);
896 return IB_RMPP_RESULT_CONSUMED
;
900 int ib_process_rmpp_send_wc(struct ib_mad_send_wr_private
*mad_send_wr
,
901 struct ib_mad_send_wc
*mad_send_wc
)
903 struct ib_rmpp_mad
*rmpp_mad
;
906 rmpp_mad
= mad_send_wr
->send_buf
.mad
;
907 if (!(ib_get_rmpp_flags(&rmpp_mad
->rmpp_hdr
) &
908 IB_MGMT_RMPP_FLAG_ACTIVE
))
909 return IB_RMPP_RESULT_UNHANDLED
; /* RMPP not active */
911 if (rmpp_mad
->rmpp_hdr
.rmpp_type
!= IB_MGMT_RMPP_TYPE_DATA
)
912 return IB_RMPP_RESULT_INTERNAL
; /* ACK, STOP, or ABORT */
914 if (mad_send_wc
->status
!= IB_WC_SUCCESS
||
915 mad_send_wr
->status
!= IB_WC_SUCCESS
)
916 return IB_RMPP_RESULT_PROCESSED
; /* Canceled or send error */
918 if (!mad_send_wr
->timeout
)
919 return IB_RMPP_RESULT_PROCESSED
; /* Response received */
921 if (mad_send_wr
->last_ack
== mad_send_wr
->send_buf
.seg_count
) {
922 mad_send_wr
->timeout
=
923 msecs_to_jiffies(mad_send_wr
->send_buf
.timeout_ms
);
924 return IB_RMPP_RESULT_PROCESSED
; /* Send done */
927 if (mad_send_wr
->seg_num
== mad_send_wr
->newwin
||
928 mad_send_wr
->seg_num
== mad_send_wr
->send_buf
.seg_count
)
929 return IB_RMPP_RESULT_PROCESSED
; /* Wait for ACK */
931 ret
= send_next_seg(mad_send_wr
);
933 mad_send_wc
->status
= IB_WC_GENERAL_ERR
;
934 return IB_RMPP_RESULT_PROCESSED
;
936 return IB_RMPP_RESULT_CONSUMED
;
939 int ib_retry_rmpp(struct ib_mad_send_wr_private
*mad_send_wr
)
941 struct ib_rmpp_mad
*rmpp_mad
;
944 rmpp_mad
= mad_send_wr
->send_buf
.mad
;
945 if (!(ib_get_rmpp_flags(&rmpp_mad
->rmpp_hdr
) &
946 IB_MGMT_RMPP_FLAG_ACTIVE
))
947 return IB_RMPP_RESULT_UNHANDLED
; /* RMPP not active */
949 if (mad_send_wr
->last_ack
== mad_send_wr
->send_buf
.seg_count
)
950 return IB_RMPP_RESULT_PROCESSED
;
952 mad_send_wr
->seg_num
= mad_send_wr
->last_ack
;
953 mad_send_wr
->cur_seg
= mad_send_wr
->last_ack_seg
;
955 ret
= send_next_seg(mad_send_wr
);
957 return IB_RMPP_RESULT_PROCESSED
;
959 return IB_RMPP_RESULT_CONSUMED
;