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
34 #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;
72 static inline void deref_rmpp_recv(struct mad_rmpp_recv *rmpp_recv)
74 if (atomic_dec_and_test(&rmpp_recv->refcount))
75 complete(&rmpp_recv->comp);
78 static void destroy_rmpp_recv(struct mad_rmpp_recv *rmpp_recv)
80 deref_rmpp_recv(rmpp_recv);
81 wait_for_completion(&rmpp_recv->comp);
82 ib_destroy_ah(rmpp_recv->ah);
86 void ib_cancel_rmpp_recvs(struct ib_mad_agent_private *agent)
88 struct mad_rmpp_recv *rmpp_recv, *temp_rmpp_recv;
91 spin_lock_irqsave(&agent->lock, flags);
92 list_for_each_entry(rmpp_recv, &agent->rmpp_list, list) {
93 if (rmpp_recv->state != RMPP_STATE_COMPLETE)
94 ib_free_recv_mad(rmpp_recv->rmpp_wc);
95 rmpp_recv->state = RMPP_STATE_CANCELING;
97 spin_unlock_irqrestore(&agent->lock, flags);
99 list_for_each_entry(rmpp_recv, &agent->rmpp_list, list) {
100 cancel_delayed_work(&rmpp_recv->timeout_work);
101 cancel_delayed_work(&rmpp_recv->cleanup_work);
104 flush_workqueue(agent->qp_info->port_priv->wq);
106 list_for_each_entry_safe(rmpp_recv, temp_rmpp_recv,
107 &agent->rmpp_list, list) {
108 list_del(&rmpp_recv->list);
109 destroy_rmpp_recv(rmpp_recv);
113 static void format_ack(struct ib_mad_send_buf *msg,
114 struct ib_rmpp_mad *data,
115 struct mad_rmpp_recv *rmpp_recv)
117 struct ib_rmpp_mad *ack = msg->mad;
120 memcpy(ack, &data->mad_hdr, msg->hdr_len);
122 ack->mad_hdr.method ^= IB_MGMT_METHOD_RESP;
123 ack->rmpp_hdr.rmpp_type = IB_MGMT_RMPP_TYPE_ACK;
124 ib_set_rmpp_flags(&ack->rmpp_hdr, IB_MGMT_RMPP_FLAG_ACTIVE);
126 spin_lock_irqsave(&rmpp_recv->lock, flags);
127 rmpp_recv->last_ack = rmpp_recv->seg_num;
128 ack->rmpp_hdr.seg_num = cpu_to_be32(rmpp_recv->seg_num);
129 ack->rmpp_hdr.paylen_newwin = cpu_to_be32(rmpp_recv->newwin);
130 spin_unlock_irqrestore(&rmpp_recv->lock, flags);
133 static void ack_recv(struct mad_rmpp_recv *rmpp_recv,
134 struct ib_mad_recv_wc *recv_wc)
136 struct ib_mad_send_buf *msg;
139 hdr_len = ib_get_mad_data_offset(recv_wc->recv_buf.mad->mad_hdr.mgmt_class);
140 msg = ib_create_send_mad(&rmpp_recv->agent->agent, recv_wc->wc->src_qp,
141 recv_wc->wc->pkey_index, 1, hdr_len,
143 IB_MGMT_BASE_VERSION);
147 format_ack(msg, (struct ib_rmpp_mad *) recv_wc->recv_buf.mad, rmpp_recv);
148 msg->ah = rmpp_recv->ah;
149 ret = ib_post_send_mad(msg, NULL);
151 ib_free_send_mad(msg);
154 static struct ib_mad_send_buf *alloc_response_msg(struct ib_mad_agent *agent,
155 struct ib_mad_recv_wc *recv_wc)
157 struct ib_mad_send_buf *msg;
161 ah = ib_create_ah_from_wc(agent->qp->pd, recv_wc->wc,
162 recv_wc->recv_buf.grh, agent->port_num);
166 hdr_len = ib_get_mad_data_offset(recv_wc->recv_buf.mad->mad_hdr.mgmt_class);
167 msg = ib_create_send_mad(agent, recv_wc->wc->src_qp,
168 recv_wc->wc->pkey_index, 1,
169 hdr_len, 0, GFP_KERNEL,
170 IB_MGMT_BASE_VERSION);
175 msg->context[0] = ah;
181 static void ack_ds_ack(struct ib_mad_agent_private *agent,
182 struct ib_mad_recv_wc *recv_wc)
184 struct ib_mad_send_buf *msg;
185 struct ib_rmpp_mad *rmpp_mad;
188 msg = alloc_response_msg(&agent->agent, recv_wc);
193 memcpy(rmpp_mad, recv_wc->recv_buf.mad, msg->hdr_len);
195 rmpp_mad->mad_hdr.method ^= IB_MGMT_METHOD_RESP;
196 ib_set_rmpp_flags(&rmpp_mad->rmpp_hdr, IB_MGMT_RMPP_FLAG_ACTIVE);
197 rmpp_mad->rmpp_hdr.seg_num = 0;
198 rmpp_mad->rmpp_hdr.paylen_newwin = cpu_to_be32(1);
200 ret = ib_post_send_mad(msg, NULL);
202 ib_destroy_ah(msg->ah);
203 ib_free_send_mad(msg);
207 void ib_rmpp_send_handler(struct ib_mad_send_wc *mad_send_wc)
209 if (mad_send_wc->send_buf->context[0] == mad_send_wc->send_buf->ah)
210 ib_destroy_ah(mad_send_wc->send_buf->ah);
211 ib_free_send_mad(mad_send_wc->send_buf);
214 static void nack_recv(struct ib_mad_agent_private *agent,
215 struct ib_mad_recv_wc *recv_wc, u8 rmpp_status)
217 struct ib_mad_send_buf *msg;
218 struct ib_rmpp_mad *rmpp_mad;
221 msg = alloc_response_msg(&agent->agent, recv_wc);
226 memcpy(rmpp_mad, recv_wc->recv_buf.mad, msg->hdr_len);
228 rmpp_mad->mad_hdr.method ^= IB_MGMT_METHOD_RESP;
229 rmpp_mad->rmpp_hdr.rmpp_version = IB_MGMT_RMPP_VERSION;
230 rmpp_mad->rmpp_hdr.rmpp_type = IB_MGMT_RMPP_TYPE_ABORT;
231 ib_set_rmpp_flags(&rmpp_mad->rmpp_hdr, IB_MGMT_RMPP_FLAG_ACTIVE);
232 rmpp_mad->rmpp_hdr.rmpp_status = rmpp_status;
233 rmpp_mad->rmpp_hdr.seg_num = 0;
234 rmpp_mad->rmpp_hdr.paylen_newwin = 0;
236 ret = ib_post_send_mad(msg, NULL);
238 ib_destroy_ah(msg->ah);
239 ib_free_send_mad(msg);
243 static void recv_timeout_handler(struct work_struct *work)
245 struct mad_rmpp_recv *rmpp_recv =
246 container_of(work, struct mad_rmpp_recv, timeout_work.work);
247 struct ib_mad_recv_wc *rmpp_wc;
250 spin_lock_irqsave(&rmpp_recv->agent->lock, flags);
251 if (rmpp_recv->state != RMPP_STATE_ACTIVE) {
252 spin_unlock_irqrestore(&rmpp_recv->agent->lock, flags);
255 rmpp_recv->state = RMPP_STATE_TIMEOUT;
256 list_del(&rmpp_recv->list);
257 spin_unlock_irqrestore(&rmpp_recv->agent->lock, flags);
259 rmpp_wc = rmpp_recv->rmpp_wc;
260 nack_recv(rmpp_recv->agent, rmpp_wc, IB_MGMT_RMPP_STATUS_T2L);
261 destroy_rmpp_recv(rmpp_recv);
262 ib_free_recv_mad(rmpp_wc);
265 static void recv_cleanup_handler(struct work_struct *work)
267 struct mad_rmpp_recv *rmpp_recv =
268 container_of(work, struct mad_rmpp_recv, cleanup_work.work);
271 spin_lock_irqsave(&rmpp_recv->agent->lock, flags);
272 if (rmpp_recv->state == RMPP_STATE_CANCELING) {
273 spin_unlock_irqrestore(&rmpp_recv->agent->lock, flags);
276 list_del(&rmpp_recv->list);
277 spin_unlock_irqrestore(&rmpp_recv->agent->lock, flags);
278 destroy_rmpp_recv(rmpp_recv);
281 static struct mad_rmpp_recv *
282 create_rmpp_recv(struct ib_mad_agent_private *agent,
283 struct ib_mad_recv_wc *mad_recv_wc)
285 struct mad_rmpp_recv *rmpp_recv;
286 struct ib_mad_hdr *mad_hdr;
288 rmpp_recv = kmalloc(sizeof *rmpp_recv, GFP_KERNEL);
292 rmpp_recv->ah = ib_create_ah_from_wc(agent->agent.qp->pd,
294 mad_recv_wc->recv_buf.grh,
295 agent->agent.port_num);
296 if (IS_ERR(rmpp_recv->ah))
299 rmpp_recv->agent = agent;
300 init_completion(&rmpp_recv->comp);
301 INIT_DELAYED_WORK(&rmpp_recv->timeout_work, recv_timeout_handler);
302 INIT_DELAYED_WORK(&rmpp_recv->cleanup_work, recv_cleanup_handler);
303 spin_lock_init(&rmpp_recv->lock);
304 rmpp_recv->state = RMPP_STATE_ACTIVE;
305 atomic_set(&rmpp_recv->refcount, 1);
307 rmpp_recv->rmpp_wc = mad_recv_wc;
308 rmpp_recv->cur_seg_buf = &mad_recv_wc->recv_buf;
309 rmpp_recv->newwin = 1;
310 rmpp_recv->seg_num = 1;
311 rmpp_recv->last_ack = 0;
312 rmpp_recv->repwin = 1;
314 mad_hdr = &mad_recv_wc->recv_buf.mad->mad_hdr;
315 rmpp_recv->tid = mad_hdr->tid;
316 rmpp_recv->src_qp = mad_recv_wc->wc->src_qp;
317 rmpp_recv->slid = mad_recv_wc->wc->slid;
318 rmpp_recv->mgmt_class = mad_hdr->mgmt_class;
319 rmpp_recv->class_version = mad_hdr->class_version;
320 rmpp_recv->method = mad_hdr->method;
323 error: kfree(rmpp_recv);
327 static struct mad_rmpp_recv *
328 find_rmpp_recv(struct ib_mad_agent_private *agent,
329 struct ib_mad_recv_wc *mad_recv_wc)
331 struct mad_rmpp_recv *rmpp_recv;
332 struct ib_mad_hdr *mad_hdr = &mad_recv_wc->recv_buf.mad->mad_hdr;
334 list_for_each_entry(rmpp_recv, &agent->rmpp_list, list) {
335 if (rmpp_recv->tid == mad_hdr->tid &&
336 rmpp_recv->src_qp == mad_recv_wc->wc->src_qp &&
337 rmpp_recv->slid == mad_recv_wc->wc->slid &&
338 rmpp_recv->mgmt_class == mad_hdr->mgmt_class &&
339 rmpp_recv->class_version == mad_hdr->class_version &&
340 rmpp_recv->method == mad_hdr->method)
346 static struct mad_rmpp_recv *
347 acquire_rmpp_recv(struct ib_mad_agent_private *agent,
348 struct ib_mad_recv_wc *mad_recv_wc)
350 struct mad_rmpp_recv *rmpp_recv;
353 spin_lock_irqsave(&agent->lock, flags);
354 rmpp_recv = find_rmpp_recv(agent, mad_recv_wc);
356 atomic_inc(&rmpp_recv->refcount);
357 spin_unlock_irqrestore(&agent->lock, flags);
361 static struct mad_rmpp_recv *
362 insert_rmpp_recv(struct ib_mad_agent_private *agent,
363 struct mad_rmpp_recv *rmpp_recv)
365 struct mad_rmpp_recv *cur_rmpp_recv;
367 cur_rmpp_recv = find_rmpp_recv(agent, rmpp_recv->rmpp_wc);
369 list_add_tail(&rmpp_recv->list, &agent->rmpp_list);
371 return cur_rmpp_recv;
374 static inline int get_last_flag(struct ib_mad_recv_buf *seg)
376 struct ib_rmpp_mad *rmpp_mad;
378 rmpp_mad = (struct ib_rmpp_mad *) seg->mad;
379 return ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) & IB_MGMT_RMPP_FLAG_LAST;
382 static inline int get_seg_num(struct ib_mad_recv_buf *seg)
384 struct ib_rmpp_mad *rmpp_mad;
386 rmpp_mad = (struct ib_rmpp_mad *) seg->mad;
387 return be32_to_cpu(rmpp_mad->rmpp_hdr.seg_num);
390 static inline struct ib_mad_recv_buf * get_next_seg(struct list_head *rmpp_list,
391 struct ib_mad_recv_buf *seg)
393 if (seg->list.next == rmpp_list)
396 return container_of(seg->list.next, struct ib_mad_recv_buf, list);
399 static inline int window_size(struct ib_mad_agent_private *agent)
401 return max(agent->qp_info->recv_queue.max_active >> 3, 1);
404 static struct ib_mad_recv_buf * find_seg_location(struct list_head *rmpp_list,
407 struct ib_mad_recv_buf *seg_buf;
410 list_for_each_entry_reverse(seg_buf, rmpp_list, list) {
411 cur_seg_num = get_seg_num(seg_buf);
412 if (seg_num > cur_seg_num)
414 if (seg_num == cur_seg_num)
420 static void update_seg_num(struct mad_rmpp_recv *rmpp_recv,
421 struct ib_mad_recv_buf *new_buf)
423 struct list_head *rmpp_list = &rmpp_recv->rmpp_wc->rmpp_list;
425 while (new_buf && (get_seg_num(new_buf) == rmpp_recv->seg_num + 1)) {
426 rmpp_recv->cur_seg_buf = new_buf;
427 rmpp_recv->seg_num++;
428 new_buf = get_next_seg(rmpp_list, new_buf);
432 static inline int get_mad_len(struct mad_rmpp_recv *rmpp_recv)
434 struct ib_rmpp_mad *rmpp_mad;
435 int hdr_size, data_size, pad;
437 rmpp_mad = (struct ib_rmpp_mad *)rmpp_recv->cur_seg_buf->mad;
439 hdr_size = ib_get_mad_data_offset(rmpp_mad->mad_hdr.mgmt_class);
440 data_size = sizeof(struct ib_rmpp_mad) - hdr_size;
441 pad = IB_MGMT_RMPP_DATA - be32_to_cpu(rmpp_mad->rmpp_hdr.paylen_newwin);
442 if (pad > IB_MGMT_RMPP_DATA || pad < 0)
445 return hdr_size + rmpp_recv->seg_num * data_size - pad;
448 static struct ib_mad_recv_wc * complete_rmpp(struct mad_rmpp_recv *rmpp_recv)
450 struct ib_mad_recv_wc *rmpp_wc;
452 ack_recv(rmpp_recv, rmpp_recv->rmpp_wc);
453 if (rmpp_recv->seg_num > 1)
454 cancel_delayed_work(&rmpp_recv->timeout_work);
456 rmpp_wc = rmpp_recv->rmpp_wc;
457 rmpp_wc->mad_len = get_mad_len(rmpp_recv);
458 /* 10 seconds until we can find the packet lifetime */
459 queue_delayed_work(rmpp_recv->agent->qp_info->port_priv->wq,
460 &rmpp_recv->cleanup_work, msecs_to_jiffies(10000));
464 static struct ib_mad_recv_wc *
465 continue_rmpp(struct ib_mad_agent_private *agent,
466 struct ib_mad_recv_wc *mad_recv_wc)
468 struct mad_rmpp_recv *rmpp_recv;
469 struct ib_mad_recv_buf *prev_buf;
470 struct ib_mad_recv_wc *done_wc;
474 rmpp_recv = acquire_rmpp_recv(agent, mad_recv_wc);
478 seg_num = get_seg_num(&mad_recv_wc->recv_buf);
480 spin_lock_irqsave(&rmpp_recv->lock, flags);
481 if ((rmpp_recv->state == RMPP_STATE_TIMEOUT) ||
482 (seg_num > rmpp_recv->newwin))
485 if ((seg_num <= rmpp_recv->last_ack) ||
486 (rmpp_recv->state == RMPP_STATE_COMPLETE)) {
487 spin_unlock_irqrestore(&rmpp_recv->lock, flags);
488 ack_recv(rmpp_recv, mad_recv_wc);
492 prev_buf = find_seg_location(&rmpp_recv->rmpp_wc->rmpp_list, seg_num);
497 list_add(&mad_recv_wc->recv_buf.list, &prev_buf->list);
498 if (rmpp_recv->cur_seg_buf == prev_buf) {
499 update_seg_num(rmpp_recv, &mad_recv_wc->recv_buf);
500 if (get_last_flag(rmpp_recv->cur_seg_buf)) {
501 rmpp_recv->state = RMPP_STATE_COMPLETE;
502 spin_unlock_irqrestore(&rmpp_recv->lock, flags);
503 done_wc = complete_rmpp(rmpp_recv);
505 } else if (rmpp_recv->seg_num == rmpp_recv->newwin) {
506 rmpp_recv->newwin += window_size(agent);
507 spin_unlock_irqrestore(&rmpp_recv->lock, flags);
508 ack_recv(rmpp_recv, mad_recv_wc);
512 spin_unlock_irqrestore(&rmpp_recv->lock, flags);
514 deref_rmpp_recv(rmpp_recv);
517 drop3: spin_unlock_irqrestore(&rmpp_recv->lock, flags);
518 drop2: deref_rmpp_recv(rmpp_recv);
519 drop1: ib_free_recv_mad(mad_recv_wc);
523 static struct ib_mad_recv_wc *
524 start_rmpp(struct ib_mad_agent_private *agent,
525 struct ib_mad_recv_wc *mad_recv_wc)
527 struct mad_rmpp_recv *rmpp_recv;
530 rmpp_recv = create_rmpp_recv(agent, mad_recv_wc);
532 ib_free_recv_mad(mad_recv_wc);
536 spin_lock_irqsave(&agent->lock, flags);
537 if (insert_rmpp_recv(agent, rmpp_recv)) {
538 spin_unlock_irqrestore(&agent->lock, flags);
539 /* duplicate first MAD */
540 destroy_rmpp_recv(rmpp_recv);
541 return continue_rmpp(agent, mad_recv_wc);
543 atomic_inc(&rmpp_recv->refcount);
545 if (get_last_flag(&mad_recv_wc->recv_buf)) {
546 rmpp_recv->state = RMPP_STATE_COMPLETE;
547 spin_unlock_irqrestore(&agent->lock, flags);
548 complete_rmpp(rmpp_recv);
550 spin_unlock_irqrestore(&agent->lock, flags);
551 /* 40 seconds until we can find the packet lifetimes */
552 queue_delayed_work(agent->qp_info->port_priv->wq,
553 &rmpp_recv->timeout_work,
554 msecs_to_jiffies(40000));
555 rmpp_recv->newwin += window_size(agent);
556 ack_recv(rmpp_recv, mad_recv_wc);
559 deref_rmpp_recv(rmpp_recv);
563 static int send_next_seg(struct ib_mad_send_wr_private *mad_send_wr)
565 struct ib_rmpp_mad *rmpp_mad;
569 rmpp_mad = mad_send_wr->send_buf.mad;
570 ib_set_rmpp_flags(&rmpp_mad->rmpp_hdr, IB_MGMT_RMPP_FLAG_ACTIVE);
571 rmpp_mad->rmpp_hdr.seg_num = cpu_to_be32(++mad_send_wr->seg_num);
573 if (mad_send_wr->seg_num == 1) {
574 rmpp_mad->rmpp_hdr.rmpp_rtime_flags |= IB_MGMT_RMPP_FLAG_FIRST;
575 paylen = mad_send_wr->send_buf.seg_count * IB_MGMT_RMPP_DATA -
579 if (mad_send_wr->seg_num == mad_send_wr->send_buf.seg_count) {
580 rmpp_mad->rmpp_hdr.rmpp_rtime_flags |= IB_MGMT_RMPP_FLAG_LAST;
581 paylen = IB_MGMT_RMPP_DATA - mad_send_wr->pad;
583 rmpp_mad->rmpp_hdr.paylen_newwin = cpu_to_be32(paylen);
585 /* 2 seconds for an ACK until we can find the packet lifetime */
586 timeout = mad_send_wr->send_buf.timeout_ms;
587 if (!timeout || timeout > 2000)
588 mad_send_wr->timeout = msecs_to_jiffies(2000);
590 return ib_send_mad(mad_send_wr);
593 static void abort_send(struct ib_mad_agent_private *agent,
594 struct ib_mad_recv_wc *mad_recv_wc, u8 rmpp_status)
596 struct ib_mad_send_wr_private *mad_send_wr;
597 struct ib_mad_send_wc wc;
600 spin_lock_irqsave(&agent->lock, flags);
601 mad_send_wr = ib_find_send_mad(agent, mad_recv_wc);
603 goto out; /* Unmatched send */
605 if ((mad_send_wr->last_ack == mad_send_wr->send_buf.seg_count) ||
606 (!mad_send_wr->timeout) || (mad_send_wr->status != IB_WC_SUCCESS))
607 goto out; /* Send is already done */
609 ib_mark_mad_done(mad_send_wr);
610 spin_unlock_irqrestore(&agent->lock, flags);
612 wc.status = IB_WC_REM_ABORT_ERR;
613 wc.vendor_err = rmpp_status;
614 wc.send_buf = &mad_send_wr->send_buf;
615 ib_mad_complete_send_wr(mad_send_wr, &wc);
618 spin_unlock_irqrestore(&agent->lock, flags);
621 static inline void adjust_last_ack(struct ib_mad_send_wr_private *wr,
624 struct list_head *list;
626 wr->last_ack = seg_num;
627 list = &wr->last_ack_seg->list;
628 list_for_each_entry(wr->last_ack_seg, list, list)
629 if (wr->last_ack_seg->num == seg_num)
633 static void process_ds_ack(struct ib_mad_agent_private *agent,
634 struct ib_mad_recv_wc *mad_recv_wc, int newwin)
636 struct mad_rmpp_recv *rmpp_recv;
638 rmpp_recv = find_rmpp_recv(agent, mad_recv_wc);
639 if (rmpp_recv && rmpp_recv->state == RMPP_STATE_COMPLETE)
640 rmpp_recv->repwin = newwin;
643 static void process_rmpp_ack(struct ib_mad_agent_private *agent,
644 struct ib_mad_recv_wc *mad_recv_wc)
646 struct ib_mad_send_wr_private *mad_send_wr;
647 struct ib_rmpp_mad *rmpp_mad;
649 int seg_num, newwin, ret;
651 rmpp_mad = (struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad;
652 if (rmpp_mad->rmpp_hdr.rmpp_status) {
653 abort_send(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BAD_STATUS);
654 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BAD_STATUS);
658 seg_num = be32_to_cpu(rmpp_mad->rmpp_hdr.seg_num);
659 newwin = be32_to_cpu(rmpp_mad->rmpp_hdr.paylen_newwin);
660 if (newwin < seg_num) {
661 abort_send(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_W2S);
662 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_W2S);
666 spin_lock_irqsave(&agent->lock, flags);
667 mad_send_wr = ib_find_send_mad(agent, mad_recv_wc);
670 process_ds_ack(agent, mad_recv_wc, newwin);
671 goto out; /* Unmatched or DS RMPP ACK */
674 if ((mad_send_wr->last_ack == mad_send_wr->send_buf.seg_count) &&
675 (mad_send_wr->timeout)) {
676 spin_unlock_irqrestore(&agent->lock, flags);
677 ack_ds_ack(agent, mad_recv_wc);
678 return; /* Repeated ACK for DS RMPP transaction */
681 if ((mad_send_wr->last_ack == mad_send_wr->send_buf.seg_count) ||
682 (!mad_send_wr->timeout) || (mad_send_wr->status != IB_WC_SUCCESS))
683 goto out; /* Send is already done */
685 if (seg_num > mad_send_wr->send_buf.seg_count ||
686 seg_num > mad_send_wr->newwin) {
687 spin_unlock_irqrestore(&agent->lock, flags);
688 abort_send(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_S2B);
689 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_S2B);
693 if (newwin < mad_send_wr->newwin || seg_num < mad_send_wr->last_ack)
694 goto out; /* Old ACK */
696 if (seg_num > mad_send_wr->last_ack) {
697 adjust_last_ack(mad_send_wr, seg_num);
698 mad_send_wr->retries_left = mad_send_wr->max_retries;
700 mad_send_wr->newwin = newwin;
701 if (mad_send_wr->last_ack == mad_send_wr->send_buf.seg_count) {
702 /* If no response is expected, the ACK completes the send */
703 if (!mad_send_wr->send_buf.timeout_ms) {
704 struct ib_mad_send_wc wc;
706 ib_mark_mad_done(mad_send_wr);
707 spin_unlock_irqrestore(&agent->lock, flags);
709 wc.status = IB_WC_SUCCESS;
711 wc.send_buf = &mad_send_wr->send_buf;
712 ib_mad_complete_send_wr(mad_send_wr, &wc);
715 if (mad_send_wr->refcount == 1)
716 ib_reset_mad_timeout(mad_send_wr,
717 mad_send_wr->send_buf.timeout_ms);
718 spin_unlock_irqrestore(&agent->lock, flags);
719 ack_ds_ack(agent, mad_recv_wc);
721 } else if (mad_send_wr->refcount == 1 &&
722 mad_send_wr->seg_num < mad_send_wr->newwin &&
723 mad_send_wr->seg_num < mad_send_wr->send_buf.seg_count) {
724 /* Send failure will just result in a timeout/retry */
725 ret = send_next_seg(mad_send_wr);
729 mad_send_wr->refcount++;
730 list_move_tail(&mad_send_wr->agent_list,
731 &mad_send_wr->mad_agent_priv->send_list);
734 spin_unlock_irqrestore(&agent->lock, flags);
737 static struct ib_mad_recv_wc *
738 process_rmpp_data(struct ib_mad_agent_private *agent,
739 struct ib_mad_recv_wc *mad_recv_wc)
741 struct ib_rmpp_hdr *rmpp_hdr;
744 rmpp_hdr = &((struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad)->rmpp_hdr;
746 if (rmpp_hdr->rmpp_status) {
747 rmpp_status = IB_MGMT_RMPP_STATUS_BAD_STATUS;
751 if (rmpp_hdr->seg_num == cpu_to_be32(1)) {
752 if (!(ib_get_rmpp_flags(rmpp_hdr) & IB_MGMT_RMPP_FLAG_FIRST)) {
753 rmpp_status = IB_MGMT_RMPP_STATUS_BAD_SEG;
756 return start_rmpp(agent, mad_recv_wc);
758 if (ib_get_rmpp_flags(rmpp_hdr) & IB_MGMT_RMPP_FLAG_FIRST) {
759 rmpp_status = IB_MGMT_RMPP_STATUS_BAD_SEG;
762 return continue_rmpp(agent, mad_recv_wc);
765 nack_recv(agent, mad_recv_wc, rmpp_status);
766 ib_free_recv_mad(mad_recv_wc);
770 static void process_rmpp_stop(struct ib_mad_agent_private *agent,
771 struct ib_mad_recv_wc *mad_recv_wc)
773 struct ib_rmpp_mad *rmpp_mad;
775 rmpp_mad = (struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad;
777 if (rmpp_mad->rmpp_hdr.rmpp_status != IB_MGMT_RMPP_STATUS_RESX) {
778 abort_send(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BAD_STATUS);
779 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BAD_STATUS);
781 abort_send(agent, mad_recv_wc, rmpp_mad->rmpp_hdr.rmpp_status);
784 static void process_rmpp_abort(struct ib_mad_agent_private *agent,
785 struct ib_mad_recv_wc *mad_recv_wc)
787 struct ib_rmpp_mad *rmpp_mad;
789 rmpp_mad = (struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad;
791 if (rmpp_mad->rmpp_hdr.rmpp_status < IB_MGMT_RMPP_STATUS_ABORT_MIN ||
792 rmpp_mad->rmpp_hdr.rmpp_status > IB_MGMT_RMPP_STATUS_ABORT_MAX) {
793 abort_send(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BAD_STATUS);
794 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BAD_STATUS);
796 abort_send(agent, mad_recv_wc, rmpp_mad->rmpp_hdr.rmpp_status);
799 struct ib_mad_recv_wc *
800 ib_process_rmpp_recv_wc(struct ib_mad_agent_private *agent,
801 struct ib_mad_recv_wc *mad_recv_wc)
803 struct ib_rmpp_mad *rmpp_mad;
805 rmpp_mad = (struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad;
806 if (!(rmpp_mad->rmpp_hdr.rmpp_rtime_flags & IB_MGMT_RMPP_FLAG_ACTIVE))
809 if (rmpp_mad->rmpp_hdr.rmpp_version != IB_MGMT_RMPP_VERSION) {
810 abort_send(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_UNV);
811 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_UNV);
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);
821 case IB_MGMT_RMPP_TYPE_STOP:
822 process_rmpp_stop(agent, mad_recv_wc);
824 case IB_MGMT_RMPP_TYPE_ABORT:
825 process_rmpp_abort(agent, mad_recv_wc);
828 abort_send(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BADT);
829 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BADT);
833 ib_free_recv_mad(mad_recv_wc);
837 static int init_newwin(struct ib_mad_send_wr_private *mad_send_wr)
839 struct ib_mad_agent_private *agent = mad_send_wr->mad_agent_priv;
840 struct ib_mad_hdr *mad_hdr = mad_send_wr->send_buf.mad;
841 struct mad_rmpp_recv *rmpp_recv;
842 struct ib_ah_attr ah_attr;
846 if (!(mad_hdr->method & IB_MGMT_METHOD_RESP))
849 spin_lock_irqsave(&agent->lock, flags);
850 list_for_each_entry(rmpp_recv, &agent->rmpp_list, list) {
851 if (rmpp_recv->tid != mad_hdr->tid ||
852 rmpp_recv->mgmt_class != mad_hdr->mgmt_class ||
853 rmpp_recv->class_version != mad_hdr->class_version ||
854 (rmpp_recv->method & IB_MGMT_METHOD_RESP))
857 if (ib_query_ah(mad_send_wr->send_buf.ah, &ah_attr))
860 if (rmpp_recv->slid == ah_attr.dlid) {
861 newwin = rmpp_recv->repwin;
865 spin_unlock_irqrestore(&agent->lock, flags);
870 int ib_send_rmpp_mad(struct ib_mad_send_wr_private *mad_send_wr)
872 struct ib_rmpp_mad *rmpp_mad;
875 rmpp_mad = mad_send_wr->send_buf.mad;
876 if (!(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
877 IB_MGMT_RMPP_FLAG_ACTIVE))
878 return IB_RMPP_RESULT_UNHANDLED;
880 if (rmpp_mad->rmpp_hdr.rmpp_type != IB_MGMT_RMPP_TYPE_DATA) {
881 mad_send_wr->seg_num = 1;
882 return IB_RMPP_RESULT_INTERNAL;
885 mad_send_wr->newwin = init_newwin(mad_send_wr);
887 /* We need to wait for the final ACK even if there isn't a response */
888 mad_send_wr->refcount += (mad_send_wr->timeout == 0);
889 ret = send_next_seg(mad_send_wr);
891 return IB_RMPP_RESULT_CONSUMED;
895 int ib_process_rmpp_send_wc(struct ib_mad_send_wr_private *mad_send_wr,
896 struct ib_mad_send_wc *mad_send_wc)
898 struct ib_rmpp_mad *rmpp_mad;
901 rmpp_mad = mad_send_wr->send_buf.mad;
902 if (!(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
903 IB_MGMT_RMPP_FLAG_ACTIVE))
904 return IB_RMPP_RESULT_UNHANDLED; /* RMPP not active */
906 if (rmpp_mad->rmpp_hdr.rmpp_type != IB_MGMT_RMPP_TYPE_DATA)
907 return IB_RMPP_RESULT_INTERNAL; /* ACK, STOP, or ABORT */
909 if (mad_send_wc->status != IB_WC_SUCCESS ||
910 mad_send_wr->status != IB_WC_SUCCESS)
911 return IB_RMPP_RESULT_PROCESSED; /* Canceled or send error */
913 if (!mad_send_wr->timeout)
914 return IB_RMPP_RESULT_PROCESSED; /* Response received */
916 if (mad_send_wr->last_ack == mad_send_wr->send_buf.seg_count) {
917 mad_send_wr->timeout =
918 msecs_to_jiffies(mad_send_wr->send_buf.timeout_ms);
919 return IB_RMPP_RESULT_PROCESSED; /* Send done */
922 if (mad_send_wr->seg_num == mad_send_wr->newwin ||
923 mad_send_wr->seg_num == mad_send_wr->send_buf.seg_count)
924 return IB_RMPP_RESULT_PROCESSED; /* Wait for ACK */
926 ret = send_next_seg(mad_send_wr);
928 mad_send_wc->status = IB_WC_GENERAL_ERR;
929 return IB_RMPP_RESULT_PROCESSED;
931 return IB_RMPP_RESULT_CONSUMED;
934 int ib_retry_rmpp(struct ib_mad_send_wr_private *mad_send_wr)
936 struct ib_rmpp_mad *rmpp_mad;
939 rmpp_mad = mad_send_wr->send_buf.mad;
940 if (!(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
941 IB_MGMT_RMPP_FLAG_ACTIVE))
942 return IB_RMPP_RESULT_UNHANDLED; /* RMPP not active */
944 if (mad_send_wr->last_ack == mad_send_wr->send_buf.seg_count)
945 return IB_RMPP_RESULT_PROCESSED;
947 mad_send_wr->seg_num = mad_send_wr->last_ack;
948 mad_send_wr->cur_seg = mad_send_wr->last_ack_seg;
950 ret = send_next_seg(mad_send_wr);
952 return IB_RMPP_RESULT_PROCESSED;
954 return IB_RMPP_RESULT_CONSUMED;