2 * Copyright (c) 2005 Intel Inc. All rights reserved.
3 * Copyright (c) 2005 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
33 * $Id: mad_rmpp.c 1921 2005-03-02 22:58:44Z sean.hefty $
36 #include <linux/dma-mapping.h>
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;
58 struct ib_mad_recv_wc *rmpp_wc;
59 struct ib_mad_recv_buf *cur_seg_buf;
72 static void destroy_rmpp_recv(struct mad_rmpp_recv *rmpp_recv)
74 atomic_dec(&rmpp_recv->refcount);
75 wait_event(rmpp_recv->wait, !atomic_read(&rmpp_recv->refcount));
76 ib_destroy_ah(rmpp_recv->ah);
80 void ib_cancel_rmpp_recvs(struct ib_mad_agent_private *agent)
82 struct mad_rmpp_recv *rmpp_recv, *temp_rmpp_recv;
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);
90 spin_unlock_irqrestore(&agent->lock, flags);
92 flush_workqueue(agent->qp_info->port_priv->wq);
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);
103 static int data_offset(u8 mgmt_class)
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;
111 return IB_MGMT_RMPP_HDR;
114 static void format_ack(struct ib_mad_send_buf *msg,
115 struct ib_rmpp_mad *data,
116 struct mad_rmpp_recv *rmpp_recv)
118 struct ib_rmpp_mad *ack = msg->mad;
121 memcpy(ack, &data->mad_hdr, msg->hdr_len);
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);
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);
134 static void ack_recv(struct mad_rmpp_recv *rmpp_recv,
135 struct ib_mad_recv_wc *recv_wc)
137 struct ib_mad_send_buf *msg;
140 hdr_len = data_offset(recv_wc->recv_buf.mad->mad_hdr.mgmt_class);
141 msg = ib_create_send_mad(&rmpp_recv->agent->agent, recv_wc->wc->src_qp,
142 recv_wc->wc->pkey_index, 1, hdr_len,
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 = 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);
178 void ib_rmpp_send_handler(struct ib_mad_send_wc *mad_send_wc)
180 struct ib_rmpp_mad *rmpp_mad = mad_send_wc->send_buf->mad;
182 if (rmpp_mad->rmpp_hdr.rmpp_type != IB_MGMT_RMPP_TYPE_ACK)
183 ib_destroy_ah(mad_send_wc->send_buf->ah);
184 ib_free_send_mad(mad_send_wc->send_buf);
187 static void nack_recv(struct ib_mad_agent_private *agent,
188 struct ib_mad_recv_wc *recv_wc, u8 rmpp_status)
190 struct ib_mad_send_buf *msg;
191 struct ib_rmpp_mad *rmpp_mad;
194 msg = alloc_response_msg(&agent->agent, recv_wc);
199 memcpy(rmpp_mad, recv_wc->recv_buf.mad, msg->hdr_len);
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;
209 ret = ib_post_send_mad(msg, NULL);
211 ib_destroy_ah(msg->ah);
212 ib_free_send_mad(msg);
216 static void recv_timeout_handler(void *data)
218 struct mad_rmpp_recv *rmpp_recv = data;
219 struct ib_mad_recv_wc *rmpp_wc;
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);
227 rmpp_recv->state = RMPP_STATE_TIMEOUT;
228 list_del(&rmpp_recv->list);
229 spin_unlock_irqrestore(&rmpp_recv->agent->lock, flags);
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);
237 static void recv_cleanup_handler(void *data)
239 struct mad_rmpp_recv *rmpp_recv = data;
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);
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)
252 struct mad_rmpp_recv *rmpp_recv;
253 struct ib_mad_hdr *mad_hdr;
255 rmpp_recv = kmalloc(sizeof *rmpp_recv, GFP_KERNEL);
259 rmpp_recv->ah = ib_create_ah_from_wc(agent->agent.qp->pd,
261 mad_recv_wc->recv_buf.grh,
262 agent->agent.port_num);
263 if (IS_ERR(rmpp_recv->ah))
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);
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;
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;
289 error: kfree(rmpp_recv);
293 static inline void deref_rmpp_recv(struct mad_rmpp_recv *rmpp_recv)
295 if (atomic_dec_and_test(&rmpp_recv->refcount))
296 wake_up(&rmpp_recv->wait);
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)
303 struct mad_rmpp_recv *rmpp_recv;
304 struct ib_mad_hdr *mad_hdr = &mad_recv_wc->recv_buf.mad->mad_hdr;
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)
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)
322 struct mad_rmpp_recv *rmpp_recv;
325 spin_lock_irqsave(&agent->lock, flags);
326 rmpp_recv = find_rmpp_recv(agent, mad_recv_wc);
328 atomic_inc(&rmpp_recv->refcount);
329 spin_unlock_irqrestore(&agent->lock, flags);
333 static struct mad_rmpp_recv *
334 insert_rmpp_recv(struct ib_mad_agent_private *agent,
335 struct mad_rmpp_recv *rmpp_recv)
337 struct mad_rmpp_recv *cur_rmpp_recv;
339 cur_rmpp_recv = find_rmpp_recv(agent, rmpp_recv->rmpp_wc);
341 list_add_tail(&rmpp_recv->list, &agent->rmpp_list);
343 return cur_rmpp_recv;
346 static inline int get_last_flag(struct ib_mad_recv_buf *seg)
348 struct ib_rmpp_mad *rmpp_mad;
350 rmpp_mad = (struct ib_rmpp_mad *) seg->mad;
351 return ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) & IB_MGMT_RMPP_FLAG_LAST;
354 static inline int get_seg_num(struct ib_mad_recv_buf *seg)
356 struct ib_rmpp_mad *rmpp_mad;
358 rmpp_mad = (struct ib_rmpp_mad *) seg->mad;
359 return be32_to_cpu(rmpp_mad->rmpp_hdr.seg_num);
362 static inline struct ib_mad_recv_buf * get_next_seg(struct list_head *rmpp_list,
363 struct ib_mad_recv_buf *seg)
365 if (seg->list.next == rmpp_list)
368 return container_of(seg->list.next, struct ib_mad_recv_buf, list);
371 static inline int window_size(struct ib_mad_agent_private *agent)
373 return max(agent->qp_info->recv_queue.max_active >> 3, 1);
376 static struct ib_mad_recv_buf * find_seg_location(struct list_head *rmpp_list,
379 struct ib_mad_recv_buf *seg_buf;
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)
386 if (seg_num == cur_seg_num)
392 static void update_seg_num(struct mad_rmpp_recv *rmpp_recv,
393 struct ib_mad_recv_buf *new_buf)
395 struct list_head *rmpp_list = &rmpp_recv->rmpp_wc->rmpp_list;
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);
404 static inline int get_mad_len(struct mad_rmpp_recv *rmpp_recv)
406 struct ib_rmpp_mad *rmpp_mad;
407 int hdr_size, data_size, pad;
409 rmpp_mad = (struct ib_rmpp_mad *)rmpp_recv->cur_seg_buf->mad;
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)
417 return hdr_size + rmpp_recv->seg_num * data_size - pad;
420 static struct ib_mad_recv_wc * complete_rmpp(struct mad_rmpp_recv *rmpp_recv)
422 struct ib_mad_recv_wc *rmpp_wc;
424 ack_recv(rmpp_recv, rmpp_recv->rmpp_wc);
425 if (rmpp_recv->seg_num > 1)
426 cancel_delayed_work(&rmpp_recv->timeout_work);
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));
436 static struct ib_mad_recv_wc *
437 continue_rmpp(struct ib_mad_agent_private *agent,
438 struct ib_mad_recv_wc *mad_recv_wc)
440 struct mad_rmpp_recv *rmpp_recv;
441 struct ib_mad_recv_buf *prev_buf;
442 struct ib_mad_recv_wc *done_wc;
446 rmpp_recv = acquire_rmpp_recv(agent, mad_recv_wc);
450 seg_num = get_seg_num(&mad_recv_wc->recv_buf);
452 spin_lock_irqsave(&rmpp_recv->lock, flags);
453 if ((rmpp_recv->state == RMPP_STATE_TIMEOUT) ||
454 (seg_num > rmpp_recv->newwin))
457 if ((seg_num <= rmpp_recv->last_ack) ||
458 (rmpp_recv->state == RMPP_STATE_COMPLETE)) {
459 spin_unlock_irqrestore(&rmpp_recv->lock, flags);
460 ack_recv(rmpp_recv, mad_recv_wc);
464 prev_buf = find_seg_location(&rmpp_recv->rmpp_wc->rmpp_list, seg_num);
469 list_add(&mad_recv_wc->recv_buf.list, &prev_buf->list);
470 if (rmpp_recv->cur_seg_buf == prev_buf) {
471 update_seg_num(rmpp_recv, &mad_recv_wc->recv_buf);
472 if (get_last_flag(rmpp_recv->cur_seg_buf)) {
473 rmpp_recv->state = RMPP_STATE_COMPLETE;
474 spin_unlock_irqrestore(&rmpp_recv->lock, flags);
475 done_wc = complete_rmpp(rmpp_recv);
477 } else if (rmpp_recv->seg_num == rmpp_recv->newwin) {
478 rmpp_recv->newwin += window_size(agent);
479 spin_unlock_irqrestore(&rmpp_recv->lock, flags);
480 ack_recv(rmpp_recv, mad_recv_wc);
484 spin_unlock_irqrestore(&rmpp_recv->lock, flags);
486 deref_rmpp_recv(rmpp_recv);
489 drop3: spin_unlock_irqrestore(&rmpp_recv->lock, flags);
490 drop2: deref_rmpp_recv(rmpp_recv);
491 drop1: ib_free_recv_mad(mad_recv_wc);
495 static struct ib_mad_recv_wc *
496 start_rmpp(struct ib_mad_agent_private *agent,
497 struct ib_mad_recv_wc *mad_recv_wc)
499 struct mad_rmpp_recv *rmpp_recv;
502 rmpp_recv = create_rmpp_recv(agent, mad_recv_wc);
504 ib_free_recv_mad(mad_recv_wc);
508 spin_lock_irqsave(&agent->lock, flags);
509 if (insert_rmpp_recv(agent, rmpp_recv)) {
510 spin_unlock_irqrestore(&agent->lock, flags);
511 /* duplicate first MAD */
512 destroy_rmpp_recv(rmpp_recv);
513 return continue_rmpp(agent, mad_recv_wc);
515 atomic_inc(&rmpp_recv->refcount);
517 if (get_last_flag(&mad_recv_wc->recv_buf)) {
518 rmpp_recv->state = RMPP_STATE_COMPLETE;
519 spin_unlock_irqrestore(&agent->lock, flags);
520 complete_rmpp(rmpp_recv);
522 spin_unlock_irqrestore(&agent->lock, flags);
523 /* 40 seconds until we can find the packet lifetimes */
524 queue_delayed_work(agent->qp_info->port_priv->wq,
525 &rmpp_recv->timeout_work,
526 msecs_to_jiffies(40000));
527 rmpp_recv->newwin += window_size(agent);
528 ack_recv(rmpp_recv, mad_recv_wc);
531 deref_rmpp_recv(rmpp_recv);
535 static int send_next_seg(struct ib_mad_send_wr_private *mad_send_wr)
537 struct ib_rmpp_mad *rmpp_mad;
541 rmpp_mad = mad_send_wr->send_buf.mad;
542 ib_set_rmpp_flags(&rmpp_mad->rmpp_hdr, IB_MGMT_RMPP_FLAG_ACTIVE);
543 rmpp_mad->rmpp_hdr.seg_num = cpu_to_be32(++mad_send_wr->seg_num);
545 if (mad_send_wr->seg_num == 1) {
546 rmpp_mad->rmpp_hdr.rmpp_rtime_flags |= IB_MGMT_RMPP_FLAG_FIRST;
547 paylen = mad_send_wr->send_buf.seg_count * IB_MGMT_RMPP_DATA -
551 if (mad_send_wr->seg_num == mad_send_wr->send_buf.seg_count) {
552 rmpp_mad->rmpp_hdr.rmpp_rtime_flags |= IB_MGMT_RMPP_FLAG_LAST;
553 paylen = IB_MGMT_RMPP_DATA - mad_send_wr->pad;
555 rmpp_mad->rmpp_hdr.paylen_newwin = cpu_to_be32(paylen);
557 /* 2 seconds for an ACK until we can find the packet lifetime */
558 timeout = mad_send_wr->send_buf.timeout_ms;
559 if (!timeout || timeout > 2000)
560 mad_send_wr->timeout = msecs_to_jiffies(2000);
562 return ib_send_mad(mad_send_wr);
565 static void abort_send(struct ib_mad_agent_private *agent, __be64 tid,
568 struct ib_mad_send_wr_private *mad_send_wr;
569 struct ib_mad_send_wc wc;
572 spin_lock_irqsave(&agent->lock, flags);
573 mad_send_wr = ib_find_send_mad(agent, tid);
575 goto out; /* Unmatched send */
577 if ((mad_send_wr->last_ack == mad_send_wr->send_buf.seg_count) ||
578 (!mad_send_wr->timeout) || (mad_send_wr->status != IB_WC_SUCCESS))
579 goto out; /* Send is already done */
581 ib_mark_mad_done(mad_send_wr);
582 spin_unlock_irqrestore(&agent->lock, flags);
584 wc.status = IB_WC_REM_ABORT_ERR;
585 wc.vendor_err = rmpp_status;
586 wc.send_buf = &mad_send_wr->send_buf;
587 ib_mad_complete_send_wr(mad_send_wr, &wc);
590 spin_unlock_irqrestore(&agent->lock, flags);
593 static inline void adjust_last_ack(struct ib_mad_send_wr_private *wr,
596 struct list_head *list;
598 wr->last_ack = seg_num;
599 list = &wr->last_ack_seg->list;
600 list_for_each_entry(wr->last_ack_seg, list, list)
601 if (wr->last_ack_seg->num == seg_num)
605 static void process_rmpp_ack(struct ib_mad_agent_private *agent,
606 struct ib_mad_recv_wc *mad_recv_wc)
608 struct ib_mad_send_wr_private *mad_send_wr;
609 struct ib_rmpp_mad *rmpp_mad;
611 int seg_num, newwin, ret;
613 rmpp_mad = (struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad;
614 if (rmpp_mad->rmpp_hdr.rmpp_status) {
615 abort_send(agent, rmpp_mad->mad_hdr.tid,
616 IB_MGMT_RMPP_STATUS_BAD_STATUS);
617 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BAD_STATUS);
621 seg_num = be32_to_cpu(rmpp_mad->rmpp_hdr.seg_num);
622 newwin = be32_to_cpu(rmpp_mad->rmpp_hdr.paylen_newwin);
623 if (newwin < seg_num) {
624 abort_send(agent, rmpp_mad->mad_hdr.tid,
625 IB_MGMT_RMPP_STATUS_W2S);
626 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_W2S);
630 spin_lock_irqsave(&agent->lock, flags);
631 mad_send_wr = ib_find_send_mad(agent, rmpp_mad->mad_hdr.tid);
633 goto out; /* Unmatched ACK */
635 if ((mad_send_wr->last_ack == mad_send_wr->send_buf.seg_count) ||
636 (!mad_send_wr->timeout) || (mad_send_wr->status != IB_WC_SUCCESS))
637 goto out; /* Send is already done */
639 if (seg_num > mad_send_wr->send_buf.seg_count ||
640 seg_num > mad_send_wr->newwin) {
641 spin_unlock_irqrestore(&agent->lock, flags);
642 abort_send(agent, rmpp_mad->mad_hdr.tid,
643 IB_MGMT_RMPP_STATUS_S2B);
644 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_S2B);
648 if (newwin < mad_send_wr->newwin || seg_num < mad_send_wr->last_ack)
649 goto out; /* Old ACK */
651 if (seg_num > mad_send_wr->last_ack) {
652 adjust_last_ack(mad_send_wr, seg_num);
653 mad_send_wr->retries = mad_send_wr->send_buf.retries;
655 mad_send_wr->newwin = newwin;
656 if (mad_send_wr->last_ack == mad_send_wr->send_buf.seg_count) {
657 /* If no response is expected, the ACK completes the send */
658 if (!mad_send_wr->send_buf.timeout_ms) {
659 struct ib_mad_send_wc wc;
661 ib_mark_mad_done(mad_send_wr);
662 spin_unlock_irqrestore(&agent->lock, flags);
664 wc.status = IB_WC_SUCCESS;
666 wc.send_buf = &mad_send_wr->send_buf;
667 ib_mad_complete_send_wr(mad_send_wr, &wc);
670 if (mad_send_wr->refcount == 1)
671 ib_reset_mad_timeout(mad_send_wr,
672 mad_send_wr->send_buf.timeout_ms);
673 } else if (mad_send_wr->refcount == 1 &&
674 mad_send_wr->seg_num < mad_send_wr->newwin &&
675 mad_send_wr->seg_num < mad_send_wr->send_buf.seg_count) {
676 /* Send failure will just result in a timeout/retry */
677 ret = send_next_seg(mad_send_wr);
681 mad_send_wr->refcount++;
682 list_del(&mad_send_wr->agent_list);
683 list_add_tail(&mad_send_wr->agent_list,
684 &mad_send_wr->mad_agent_priv->send_list);
687 spin_unlock_irqrestore(&agent->lock, flags);
690 static struct ib_mad_recv_wc *
691 process_rmpp_data(struct ib_mad_agent_private *agent,
692 struct ib_mad_recv_wc *mad_recv_wc)
694 struct ib_rmpp_hdr *rmpp_hdr;
697 rmpp_hdr = &((struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad)->rmpp_hdr;
699 if (rmpp_hdr->rmpp_status) {
700 rmpp_status = IB_MGMT_RMPP_STATUS_BAD_STATUS;
704 if (rmpp_hdr->seg_num == __constant_htonl(1)) {
705 if (!(ib_get_rmpp_flags(rmpp_hdr) & IB_MGMT_RMPP_FLAG_FIRST)) {
706 rmpp_status = IB_MGMT_RMPP_STATUS_BAD_SEG;
709 return start_rmpp(agent, mad_recv_wc);
711 if (ib_get_rmpp_flags(rmpp_hdr) & IB_MGMT_RMPP_FLAG_FIRST) {
712 rmpp_status = IB_MGMT_RMPP_STATUS_BAD_SEG;
715 return continue_rmpp(agent, mad_recv_wc);
718 nack_recv(agent, mad_recv_wc, rmpp_status);
719 ib_free_recv_mad(mad_recv_wc);
723 static void process_rmpp_stop(struct ib_mad_agent_private *agent,
724 struct ib_mad_recv_wc *mad_recv_wc)
726 struct ib_rmpp_mad *rmpp_mad;
728 rmpp_mad = (struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad;
730 if (rmpp_mad->rmpp_hdr.rmpp_status != IB_MGMT_RMPP_STATUS_RESX) {
731 abort_send(agent, rmpp_mad->mad_hdr.tid,
732 IB_MGMT_RMPP_STATUS_BAD_STATUS);
733 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BAD_STATUS);
735 abort_send(agent, rmpp_mad->mad_hdr.tid,
736 rmpp_mad->rmpp_hdr.rmpp_status);
739 static void process_rmpp_abort(struct ib_mad_agent_private *agent,
740 struct ib_mad_recv_wc *mad_recv_wc)
742 struct ib_rmpp_mad *rmpp_mad;
744 rmpp_mad = (struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad;
746 if (rmpp_mad->rmpp_hdr.rmpp_status < IB_MGMT_RMPP_STATUS_ABORT_MIN ||
747 rmpp_mad->rmpp_hdr.rmpp_status > IB_MGMT_RMPP_STATUS_ABORT_MAX) {
748 abort_send(agent, rmpp_mad->mad_hdr.tid,
749 IB_MGMT_RMPP_STATUS_BAD_STATUS);
750 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BAD_STATUS);
752 abort_send(agent, rmpp_mad->mad_hdr.tid,
753 rmpp_mad->rmpp_hdr.rmpp_status);
756 struct ib_mad_recv_wc *
757 ib_process_rmpp_recv_wc(struct ib_mad_agent_private *agent,
758 struct ib_mad_recv_wc *mad_recv_wc)
760 struct ib_rmpp_mad *rmpp_mad;
762 rmpp_mad = (struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad;
763 if (!(rmpp_mad->rmpp_hdr.rmpp_rtime_flags & IB_MGMT_RMPP_FLAG_ACTIVE))
766 if (rmpp_mad->rmpp_hdr.rmpp_version != IB_MGMT_RMPP_VERSION) {
767 abort_send(agent, rmpp_mad->mad_hdr.tid,
768 IB_MGMT_RMPP_STATUS_UNV);
769 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_UNV);
773 switch (rmpp_mad->rmpp_hdr.rmpp_type) {
774 case IB_MGMT_RMPP_TYPE_DATA:
775 return process_rmpp_data(agent, mad_recv_wc);
776 case IB_MGMT_RMPP_TYPE_ACK:
777 process_rmpp_ack(agent, mad_recv_wc);
779 case IB_MGMT_RMPP_TYPE_STOP:
780 process_rmpp_stop(agent, mad_recv_wc);
782 case IB_MGMT_RMPP_TYPE_ABORT:
783 process_rmpp_abort(agent, mad_recv_wc);
786 abort_send(agent, rmpp_mad->mad_hdr.tid,
787 IB_MGMT_RMPP_STATUS_BADT);
788 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BADT);
792 ib_free_recv_mad(mad_recv_wc);
796 int ib_send_rmpp_mad(struct ib_mad_send_wr_private *mad_send_wr)
798 struct ib_rmpp_mad *rmpp_mad;
801 rmpp_mad = mad_send_wr->send_buf.mad;
802 if (!(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
803 IB_MGMT_RMPP_FLAG_ACTIVE))
804 return IB_RMPP_RESULT_UNHANDLED;
806 if (rmpp_mad->rmpp_hdr.rmpp_type != IB_MGMT_RMPP_TYPE_DATA) {
807 mad_send_wr->seg_num = 1;
808 return IB_RMPP_RESULT_INTERNAL;
811 mad_send_wr->newwin = 1;
813 /* We need to wait for the final ACK even if there isn't a response */
814 mad_send_wr->refcount += (mad_send_wr->timeout == 0);
815 ret = send_next_seg(mad_send_wr);
817 return IB_RMPP_RESULT_CONSUMED;
821 int ib_process_rmpp_send_wc(struct ib_mad_send_wr_private *mad_send_wr,
822 struct ib_mad_send_wc *mad_send_wc)
824 struct ib_rmpp_mad *rmpp_mad;
827 rmpp_mad = mad_send_wr->send_buf.mad;
828 if (!(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
829 IB_MGMT_RMPP_FLAG_ACTIVE))
830 return IB_RMPP_RESULT_UNHANDLED; /* RMPP not active */
832 if (rmpp_mad->rmpp_hdr.rmpp_type != IB_MGMT_RMPP_TYPE_DATA)
833 return IB_RMPP_RESULT_INTERNAL; /* ACK, STOP, or ABORT */
835 if (mad_send_wc->status != IB_WC_SUCCESS ||
836 mad_send_wr->status != IB_WC_SUCCESS)
837 return IB_RMPP_RESULT_PROCESSED; /* Canceled or send error */
839 if (!mad_send_wr->timeout)
840 return IB_RMPP_RESULT_PROCESSED; /* Response received */
842 if (mad_send_wr->last_ack == mad_send_wr->send_buf.seg_count) {
843 mad_send_wr->timeout =
844 msecs_to_jiffies(mad_send_wr->send_buf.timeout_ms);
845 return IB_RMPP_RESULT_PROCESSED; /* Send done */
848 if (mad_send_wr->seg_num == mad_send_wr->newwin ||
849 mad_send_wr->seg_num == mad_send_wr->send_buf.seg_count)
850 return IB_RMPP_RESULT_PROCESSED; /* Wait for ACK */
852 ret = send_next_seg(mad_send_wr);
854 mad_send_wc->status = IB_WC_GENERAL_ERR;
855 return IB_RMPP_RESULT_PROCESSED;
857 return IB_RMPP_RESULT_CONSUMED;
860 int ib_retry_rmpp(struct ib_mad_send_wr_private *mad_send_wr)
862 struct ib_rmpp_mad *rmpp_mad;
865 rmpp_mad = mad_send_wr->send_buf.mad;
866 if (!(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
867 IB_MGMT_RMPP_FLAG_ACTIVE))
868 return IB_RMPP_RESULT_UNHANDLED; /* RMPP not active */
870 if (mad_send_wr->last_ack == mad_send_wr->send_buf.seg_count)
871 return IB_RMPP_RESULT_PROCESSED;
873 mad_send_wr->seg_num = mad_send_wr->last_ack;
874 mad_send_wr->cur_seg = mad_send_wr->last_ack_seg;
876 ret = send_next_seg(mad_send_wr);
878 return IB_RMPP_RESULT_PROCESSED;
880 return IB_RMPP_RESULT_CONSUMED;