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
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 struct completion comp;
53 enum rmpp_state state;
58 struct ib_mad_recv_wc *rmpp_wc;
59 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 cancel_delayed_work(&rmpp_recv->timeout_work);
94 cancel_delayed_work(&rmpp_recv->cleanup_work);
96 spin_unlock_irqrestore(&agent->lock, flags);
98 flush_workqueue(agent->qp_info->port_priv->wq);
100 list_for_each_entry_safe(rmpp_recv, temp_rmpp_recv,
101 &agent->rmpp_list, list) {
102 list_del(&rmpp_recv->list);
103 if (rmpp_recv->state != RMPP_STATE_COMPLETE)
104 ib_free_recv_mad(rmpp_recv->rmpp_wc);
105 destroy_rmpp_recv(rmpp_recv);
109 static void format_ack(struct ib_mad_send_buf *msg,
110 struct ib_rmpp_mad *data,
111 struct mad_rmpp_recv *rmpp_recv)
113 struct ib_rmpp_mad *ack = msg->mad;
116 memcpy(ack, &data->mad_hdr, msg->hdr_len);
118 ack->mad_hdr.method ^= IB_MGMT_METHOD_RESP;
119 ack->rmpp_hdr.rmpp_type = IB_MGMT_RMPP_TYPE_ACK;
120 ib_set_rmpp_flags(&ack->rmpp_hdr, IB_MGMT_RMPP_FLAG_ACTIVE);
122 spin_lock_irqsave(&rmpp_recv->lock, flags);
123 rmpp_recv->last_ack = rmpp_recv->seg_num;
124 ack->rmpp_hdr.seg_num = cpu_to_be32(rmpp_recv->seg_num);
125 ack->rmpp_hdr.paylen_newwin = cpu_to_be32(rmpp_recv->newwin);
126 spin_unlock_irqrestore(&rmpp_recv->lock, flags);
129 static void ack_recv(struct mad_rmpp_recv *rmpp_recv,
130 struct ib_mad_recv_wc *recv_wc)
132 struct ib_mad_send_buf *msg;
135 hdr_len = ib_get_mad_data_offset(recv_wc->recv_buf.mad->mad_hdr.mgmt_class);
136 msg = ib_create_send_mad(&rmpp_recv->agent->agent, recv_wc->wc->src_qp,
137 recv_wc->wc->pkey_index, 1, hdr_len,
142 format_ack(msg, (struct ib_rmpp_mad *) recv_wc->recv_buf.mad, rmpp_recv);
143 msg->ah = rmpp_recv->ah;
144 ret = ib_post_send_mad(msg, NULL);
146 ib_free_send_mad(msg);
149 static struct ib_mad_send_buf *alloc_response_msg(struct ib_mad_agent *agent,
150 struct ib_mad_recv_wc *recv_wc)
152 struct ib_mad_send_buf *msg;
156 ah = ib_create_ah_from_wc(agent->qp->pd, recv_wc->wc,
157 recv_wc->recv_buf.grh, agent->port_num);
161 hdr_len = ib_get_mad_data_offset(recv_wc->recv_buf.mad->mad_hdr.mgmt_class);
162 msg = ib_create_send_mad(agent, recv_wc->wc->src_qp,
163 recv_wc->wc->pkey_index, 1,
164 hdr_len, 0, GFP_KERNEL);
173 void ib_rmpp_send_handler(struct ib_mad_send_wc *mad_send_wc)
175 struct ib_rmpp_mad *rmpp_mad = mad_send_wc->send_buf->mad;
177 if (rmpp_mad->rmpp_hdr.rmpp_type != IB_MGMT_RMPP_TYPE_ACK)
178 ib_destroy_ah(mad_send_wc->send_buf->ah);
179 ib_free_send_mad(mad_send_wc->send_buf);
182 static void nack_recv(struct ib_mad_agent_private *agent,
183 struct ib_mad_recv_wc *recv_wc, u8 rmpp_status)
185 struct ib_mad_send_buf *msg;
186 struct ib_rmpp_mad *rmpp_mad;
189 msg = alloc_response_msg(&agent->agent, recv_wc);
194 memcpy(rmpp_mad, recv_wc->recv_buf.mad, msg->hdr_len);
196 rmpp_mad->mad_hdr.method ^= IB_MGMT_METHOD_RESP;
197 rmpp_mad->rmpp_hdr.rmpp_version = IB_MGMT_RMPP_VERSION;
198 rmpp_mad->rmpp_hdr.rmpp_type = IB_MGMT_RMPP_TYPE_ABORT;
199 ib_set_rmpp_flags(&rmpp_mad->rmpp_hdr, IB_MGMT_RMPP_FLAG_ACTIVE);
200 rmpp_mad->rmpp_hdr.rmpp_status = rmpp_status;
201 rmpp_mad->rmpp_hdr.seg_num = 0;
202 rmpp_mad->rmpp_hdr.paylen_newwin = 0;
204 ret = ib_post_send_mad(msg, NULL);
206 ib_destroy_ah(msg->ah);
207 ib_free_send_mad(msg);
211 static void recv_timeout_handler(void *data)
213 struct mad_rmpp_recv *rmpp_recv = data;
214 struct ib_mad_recv_wc *rmpp_wc;
217 spin_lock_irqsave(&rmpp_recv->agent->lock, flags);
218 if (rmpp_recv->state != RMPP_STATE_ACTIVE) {
219 spin_unlock_irqrestore(&rmpp_recv->agent->lock, flags);
222 rmpp_recv->state = RMPP_STATE_TIMEOUT;
223 list_del(&rmpp_recv->list);
224 spin_unlock_irqrestore(&rmpp_recv->agent->lock, flags);
226 rmpp_wc = rmpp_recv->rmpp_wc;
227 nack_recv(rmpp_recv->agent, rmpp_wc, IB_MGMT_RMPP_STATUS_T2L);
228 destroy_rmpp_recv(rmpp_recv);
229 ib_free_recv_mad(rmpp_wc);
232 static void recv_cleanup_handler(void *data)
234 struct mad_rmpp_recv *rmpp_recv = data;
237 spin_lock_irqsave(&rmpp_recv->agent->lock, flags);
238 list_del(&rmpp_recv->list);
239 spin_unlock_irqrestore(&rmpp_recv->agent->lock, flags);
240 destroy_rmpp_recv(rmpp_recv);
243 static struct mad_rmpp_recv *
244 create_rmpp_recv(struct ib_mad_agent_private *agent,
245 struct ib_mad_recv_wc *mad_recv_wc)
247 struct mad_rmpp_recv *rmpp_recv;
248 struct ib_mad_hdr *mad_hdr;
250 rmpp_recv = kmalloc(sizeof *rmpp_recv, GFP_KERNEL);
254 rmpp_recv->ah = ib_create_ah_from_wc(agent->agent.qp->pd,
256 mad_recv_wc->recv_buf.grh,
257 agent->agent.port_num);
258 if (IS_ERR(rmpp_recv->ah))
261 rmpp_recv->agent = agent;
262 init_completion(&rmpp_recv->comp);
263 INIT_WORK(&rmpp_recv->timeout_work, recv_timeout_handler, rmpp_recv);
264 INIT_WORK(&rmpp_recv->cleanup_work, recv_cleanup_handler, rmpp_recv);
265 spin_lock_init(&rmpp_recv->lock);
266 rmpp_recv->state = RMPP_STATE_ACTIVE;
267 atomic_set(&rmpp_recv->refcount, 1);
269 rmpp_recv->rmpp_wc = mad_recv_wc;
270 rmpp_recv->cur_seg_buf = &mad_recv_wc->recv_buf;
271 rmpp_recv->newwin = 1;
272 rmpp_recv->seg_num = 1;
273 rmpp_recv->last_ack = 0;
275 mad_hdr = &mad_recv_wc->recv_buf.mad->mad_hdr;
276 rmpp_recv->tid = mad_hdr->tid;
277 rmpp_recv->src_qp = mad_recv_wc->wc->src_qp;
278 rmpp_recv->slid = mad_recv_wc->wc->slid;
279 rmpp_recv->mgmt_class = mad_hdr->mgmt_class;
280 rmpp_recv->class_version = mad_hdr->class_version;
281 rmpp_recv->method = mad_hdr->method;
284 error: kfree(rmpp_recv);
288 static struct mad_rmpp_recv *
289 find_rmpp_recv(struct ib_mad_agent_private *agent,
290 struct ib_mad_recv_wc *mad_recv_wc)
292 struct mad_rmpp_recv *rmpp_recv;
293 struct ib_mad_hdr *mad_hdr = &mad_recv_wc->recv_buf.mad->mad_hdr;
295 list_for_each_entry(rmpp_recv, &agent->rmpp_list, list) {
296 if (rmpp_recv->tid == mad_hdr->tid &&
297 rmpp_recv->src_qp == mad_recv_wc->wc->src_qp &&
298 rmpp_recv->slid == mad_recv_wc->wc->slid &&
299 rmpp_recv->mgmt_class == mad_hdr->mgmt_class &&
300 rmpp_recv->class_version == mad_hdr->class_version &&
301 rmpp_recv->method == mad_hdr->method)
307 static struct mad_rmpp_recv *
308 acquire_rmpp_recv(struct ib_mad_agent_private *agent,
309 struct ib_mad_recv_wc *mad_recv_wc)
311 struct mad_rmpp_recv *rmpp_recv;
314 spin_lock_irqsave(&agent->lock, flags);
315 rmpp_recv = find_rmpp_recv(agent, mad_recv_wc);
317 atomic_inc(&rmpp_recv->refcount);
318 spin_unlock_irqrestore(&agent->lock, flags);
322 static struct mad_rmpp_recv *
323 insert_rmpp_recv(struct ib_mad_agent_private *agent,
324 struct mad_rmpp_recv *rmpp_recv)
326 struct mad_rmpp_recv *cur_rmpp_recv;
328 cur_rmpp_recv = find_rmpp_recv(agent, rmpp_recv->rmpp_wc);
330 list_add_tail(&rmpp_recv->list, &agent->rmpp_list);
332 return cur_rmpp_recv;
335 static inline int get_last_flag(struct ib_mad_recv_buf *seg)
337 struct ib_rmpp_mad *rmpp_mad;
339 rmpp_mad = (struct ib_rmpp_mad *) seg->mad;
340 return ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) & IB_MGMT_RMPP_FLAG_LAST;
343 static inline int get_seg_num(struct ib_mad_recv_buf *seg)
345 struct ib_rmpp_mad *rmpp_mad;
347 rmpp_mad = (struct ib_rmpp_mad *) seg->mad;
348 return be32_to_cpu(rmpp_mad->rmpp_hdr.seg_num);
351 static inline struct ib_mad_recv_buf * get_next_seg(struct list_head *rmpp_list,
352 struct ib_mad_recv_buf *seg)
354 if (seg->list.next == rmpp_list)
357 return container_of(seg->list.next, struct ib_mad_recv_buf, list);
360 static inline int window_size(struct ib_mad_agent_private *agent)
362 return max(agent->qp_info->recv_queue.max_active >> 3, 1);
365 static struct ib_mad_recv_buf * find_seg_location(struct list_head *rmpp_list,
368 struct ib_mad_recv_buf *seg_buf;
371 list_for_each_entry_reverse(seg_buf, rmpp_list, list) {
372 cur_seg_num = get_seg_num(seg_buf);
373 if (seg_num > cur_seg_num)
375 if (seg_num == cur_seg_num)
381 static void update_seg_num(struct mad_rmpp_recv *rmpp_recv,
382 struct ib_mad_recv_buf *new_buf)
384 struct list_head *rmpp_list = &rmpp_recv->rmpp_wc->rmpp_list;
386 while (new_buf && (get_seg_num(new_buf) == rmpp_recv->seg_num + 1)) {
387 rmpp_recv->cur_seg_buf = new_buf;
388 rmpp_recv->seg_num++;
389 new_buf = get_next_seg(rmpp_list, new_buf);
393 static inline int get_mad_len(struct mad_rmpp_recv *rmpp_recv)
395 struct ib_rmpp_mad *rmpp_mad;
396 int hdr_size, data_size, pad;
398 rmpp_mad = (struct ib_rmpp_mad *)rmpp_recv->cur_seg_buf->mad;
400 hdr_size = ib_get_mad_data_offset(rmpp_mad->mad_hdr.mgmt_class);
401 data_size = sizeof(struct ib_rmpp_mad) - hdr_size;
402 pad = IB_MGMT_RMPP_DATA - be32_to_cpu(rmpp_mad->rmpp_hdr.paylen_newwin);
403 if (pad > IB_MGMT_RMPP_DATA || pad < 0)
406 return hdr_size + rmpp_recv->seg_num * data_size - pad;
409 static struct ib_mad_recv_wc * complete_rmpp(struct mad_rmpp_recv *rmpp_recv)
411 struct ib_mad_recv_wc *rmpp_wc;
413 ack_recv(rmpp_recv, rmpp_recv->rmpp_wc);
414 if (rmpp_recv->seg_num > 1)
415 cancel_delayed_work(&rmpp_recv->timeout_work);
417 rmpp_wc = rmpp_recv->rmpp_wc;
418 rmpp_wc->mad_len = get_mad_len(rmpp_recv);
419 /* 10 seconds until we can find the packet lifetime */
420 queue_delayed_work(rmpp_recv->agent->qp_info->port_priv->wq,
421 &rmpp_recv->cleanup_work, msecs_to_jiffies(10000));
425 static struct ib_mad_recv_wc *
426 continue_rmpp(struct ib_mad_agent_private *agent,
427 struct ib_mad_recv_wc *mad_recv_wc)
429 struct mad_rmpp_recv *rmpp_recv;
430 struct ib_mad_recv_buf *prev_buf;
431 struct ib_mad_recv_wc *done_wc;
435 rmpp_recv = acquire_rmpp_recv(agent, mad_recv_wc);
439 seg_num = get_seg_num(&mad_recv_wc->recv_buf);
441 spin_lock_irqsave(&rmpp_recv->lock, flags);
442 if ((rmpp_recv->state == RMPP_STATE_TIMEOUT) ||
443 (seg_num > rmpp_recv->newwin))
446 if ((seg_num <= rmpp_recv->last_ack) ||
447 (rmpp_recv->state == RMPP_STATE_COMPLETE)) {
448 spin_unlock_irqrestore(&rmpp_recv->lock, flags);
449 ack_recv(rmpp_recv, mad_recv_wc);
453 prev_buf = find_seg_location(&rmpp_recv->rmpp_wc->rmpp_list, seg_num);
458 list_add(&mad_recv_wc->recv_buf.list, &prev_buf->list);
459 if (rmpp_recv->cur_seg_buf == prev_buf) {
460 update_seg_num(rmpp_recv, &mad_recv_wc->recv_buf);
461 if (get_last_flag(rmpp_recv->cur_seg_buf)) {
462 rmpp_recv->state = RMPP_STATE_COMPLETE;
463 spin_unlock_irqrestore(&rmpp_recv->lock, flags);
464 done_wc = complete_rmpp(rmpp_recv);
466 } else if (rmpp_recv->seg_num == rmpp_recv->newwin) {
467 rmpp_recv->newwin += window_size(agent);
468 spin_unlock_irqrestore(&rmpp_recv->lock, flags);
469 ack_recv(rmpp_recv, mad_recv_wc);
473 spin_unlock_irqrestore(&rmpp_recv->lock, flags);
475 deref_rmpp_recv(rmpp_recv);
478 drop3: spin_unlock_irqrestore(&rmpp_recv->lock, flags);
479 drop2: deref_rmpp_recv(rmpp_recv);
480 drop1: ib_free_recv_mad(mad_recv_wc);
484 static struct ib_mad_recv_wc *
485 start_rmpp(struct ib_mad_agent_private *agent,
486 struct ib_mad_recv_wc *mad_recv_wc)
488 struct mad_rmpp_recv *rmpp_recv;
491 rmpp_recv = create_rmpp_recv(agent, mad_recv_wc);
493 ib_free_recv_mad(mad_recv_wc);
497 spin_lock_irqsave(&agent->lock, flags);
498 if (insert_rmpp_recv(agent, rmpp_recv)) {
499 spin_unlock_irqrestore(&agent->lock, flags);
500 /* duplicate first MAD */
501 destroy_rmpp_recv(rmpp_recv);
502 return continue_rmpp(agent, mad_recv_wc);
504 atomic_inc(&rmpp_recv->refcount);
506 if (get_last_flag(&mad_recv_wc->recv_buf)) {
507 rmpp_recv->state = RMPP_STATE_COMPLETE;
508 spin_unlock_irqrestore(&agent->lock, flags);
509 complete_rmpp(rmpp_recv);
511 spin_unlock_irqrestore(&agent->lock, flags);
512 /* 40 seconds until we can find the packet lifetimes */
513 queue_delayed_work(agent->qp_info->port_priv->wq,
514 &rmpp_recv->timeout_work,
515 msecs_to_jiffies(40000));
516 rmpp_recv->newwin += window_size(agent);
517 ack_recv(rmpp_recv, mad_recv_wc);
520 deref_rmpp_recv(rmpp_recv);
524 static int send_next_seg(struct ib_mad_send_wr_private *mad_send_wr)
526 struct ib_rmpp_mad *rmpp_mad;
530 rmpp_mad = mad_send_wr->send_buf.mad;
531 ib_set_rmpp_flags(&rmpp_mad->rmpp_hdr, IB_MGMT_RMPP_FLAG_ACTIVE);
532 rmpp_mad->rmpp_hdr.seg_num = cpu_to_be32(++mad_send_wr->seg_num);
534 if (mad_send_wr->seg_num == 1) {
535 rmpp_mad->rmpp_hdr.rmpp_rtime_flags |= IB_MGMT_RMPP_FLAG_FIRST;
536 paylen = mad_send_wr->send_buf.seg_count * IB_MGMT_RMPP_DATA -
540 if (mad_send_wr->seg_num == mad_send_wr->send_buf.seg_count) {
541 rmpp_mad->rmpp_hdr.rmpp_rtime_flags |= IB_MGMT_RMPP_FLAG_LAST;
542 paylen = IB_MGMT_RMPP_DATA - mad_send_wr->pad;
544 rmpp_mad->rmpp_hdr.paylen_newwin = cpu_to_be32(paylen);
546 /* 2 seconds for an ACK until we can find the packet lifetime */
547 timeout = mad_send_wr->send_buf.timeout_ms;
548 if (!timeout || timeout > 2000)
549 mad_send_wr->timeout = msecs_to_jiffies(2000);
551 return ib_send_mad(mad_send_wr);
554 static void abort_send(struct ib_mad_agent_private *agent,
555 struct ib_mad_recv_wc *mad_recv_wc, u8 rmpp_status)
557 struct ib_mad_send_wr_private *mad_send_wr;
558 struct ib_mad_send_wc wc;
561 spin_lock_irqsave(&agent->lock, flags);
562 mad_send_wr = ib_find_send_mad(agent, mad_recv_wc);
564 goto out; /* Unmatched send */
566 if ((mad_send_wr->last_ack == mad_send_wr->send_buf.seg_count) ||
567 (!mad_send_wr->timeout) || (mad_send_wr->status != IB_WC_SUCCESS))
568 goto out; /* Send is already done */
570 ib_mark_mad_done(mad_send_wr);
571 spin_unlock_irqrestore(&agent->lock, flags);
573 wc.status = IB_WC_REM_ABORT_ERR;
574 wc.vendor_err = rmpp_status;
575 wc.send_buf = &mad_send_wr->send_buf;
576 ib_mad_complete_send_wr(mad_send_wr, &wc);
579 spin_unlock_irqrestore(&agent->lock, flags);
582 static inline void adjust_last_ack(struct ib_mad_send_wr_private *wr,
585 struct list_head *list;
587 wr->last_ack = seg_num;
588 list = &wr->last_ack_seg->list;
589 list_for_each_entry(wr->last_ack_seg, list, list)
590 if (wr->last_ack_seg->num == seg_num)
594 static void process_rmpp_ack(struct ib_mad_agent_private *agent,
595 struct ib_mad_recv_wc *mad_recv_wc)
597 struct ib_mad_send_wr_private *mad_send_wr;
598 struct ib_rmpp_mad *rmpp_mad;
600 int seg_num, newwin, ret;
602 rmpp_mad = (struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad;
603 if (rmpp_mad->rmpp_hdr.rmpp_status) {
604 abort_send(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BAD_STATUS);
605 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BAD_STATUS);
609 seg_num = be32_to_cpu(rmpp_mad->rmpp_hdr.seg_num);
610 newwin = be32_to_cpu(rmpp_mad->rmpp_hdr.paylen_newwin);
611 if (newwin < seg_num) {
612 abort_send(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_W2S);
613 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_W2S);
617 spin_lock_irqsave(&agent->lock, flags);
618 mad_send_wr = ib_find_send_mad(agent, mad_recv_wc);
620 goto out; /* Unmatched ACK */
622 if ((mad_send_wr->last_ack == mad_send_wr->send_buf.seg_count) ||
623 (!mad_send_wr->timeout) || (mad_send_wr->status != IB_WC_SUCCESS))
624 goto out; /* Send is already done */
626 if (seg_num > mad_send_wr->send_buf.seg_count ||
627 seg_num > mad_send_wr->newwin) {
628 spin_unlock_irqrestore(&agent->lock, flags);
629 abort_send(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_S2B);
630 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_S2B);
634 if (newwin < mad_send_wr->newwin || seg_num < mad_send_wr->last_ack)
635 goto out; /* Old ACK */
637 if (seg_num > mad_send_wr->last_ack) {
638 adjust_last_ack(mad_send_wr, seg_num);
639 mad_send_wr->retries = mad_send_wr->send_buf.retries;
641 mad_send_wr->newwin = newwin;
642 if (mad_send_wr->last_ack == mad_send_wr->send_buf.seg_count) {
643 /* If no response is expected, the ACK completes the send */
644 if (!mad_send_wr->send_buf.timeout_ms) {
645 struct ib_mad_send_wc wc;
647 ib_mark_mad_done(mad_send_wr);
648 spin_unlock_irqrestore(&agent->lock, flags);
650 wc.status = IB_WC_SUCCESS;
652 wc.send_buf = &mad_send_wr->send_buf;
653 ib_mad_complete_send_wr(mad_send_wr, &wc);
656 if (mad_send_wr->refcount == 1)
657 ib_reset_mad_timeout(mad_send_wr,
658 mad_send_wr->send_buf.timeout_ms);
659 } else if (mad_send_wr->refcount == 1 &&
660 mad_send_wr->seg_num < mad_send_wr->newwin &&
661 mad_send_wr->seg_num < mad_send_wr->send_buf.seg_count) {
662 /* Send failure will just result in a timeout/retry */
663 ret = send_next_seg(mad_send_wr);
667 mad_send_wr->refcount++;
668 list_move_tail(&mad_send_wr->agent_list,
669 &mad_send_wr->mad_agent_priv->send_list);
672 spin_unlock_irqrestore(&agent->lock, flags);
675 static struct ib_mad_recv_wc *
676 process_rmpp_data(struct ib_mad_agent_private *agent,
677 struct ib_mad_recv_wc *mad_recv_wc)
679 struct ib_rmpp_hdr *rmpp_hdr;
682 rmpp_hdr = &((struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad)->rmpp_hdr;
684 if (rmpp_hdr->rmpp_status) {
685 rmpp_status = IB_MGMT_RMPP_STATUS_BAD_STATUS;
689 if (rmpp_hdr->seg_num == __constant_htonl(1)) {
690 if (!(ib_get_rmpp_flags(rmpp_hdr) & IB_MGMT_RMPP_FLAG_FIRST)) {
691 rmpp_status = IB_MGMT_RMPP_STATUS_BAD_SEG;
694 return start_rmpp(agent, mad_recv_wc);
696 if (ib_get_rmpp_flags(rmpp_hdr) & IB_MGMT_RMPP_FLAG_FIRST) {
697 rmpp_status = IB_MGMT_RMPP_STATUS_BAD_SEG;
700 return continue_rmpp(agent, mad_recv_wc);
703 nack_recv(agent, mad_recv_wc, rmpp_status);
704 ib_free_recv_mad(mad_recv_wc);
708 static void process_rmpp_stop(struct ib_mad_agent_private *agent,
709 struct ib_mad_recv_wc *mad_recv_wc)
711 struct ib_rmpp_mad *rmpp_mad;
713 rmpp_mad = (struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad;
715 if (rmpp_mad->rmpp_hdr.rmpp_status != IB_MGMT_RMPP_STATUS_RESX) {
716 abort_send(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BAD_STATUS);
717 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BAD_STATUS);
719 abort_send(agent, mad_recv_wc, rmpp_mad->rmpp_hdr.rmpp_status);
722 static void process_rmpp_abort(struct ib_mad_agent_private *agent,
723 struct ib_mad_recv_wc *mad_recv_wc)
725 struct ib_rmpp_mad *rmpp_mad;
727 rmpp_mad = (struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad;
729 if (rmpp_mad->rmpp_hdr.rmpp_status < IB_MGMT_RMPP_STATUS_ABORT_MIN ||
730 rmpp_mad->rmpp_hdr.rmpp_status > IB_MGMT_RMPP_STATUS_ABORT_MAX) {
731 abort_send(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BAD_STATUS);
732 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BAD_STATUS);
734 abort_send(agent, mad_recv_wc, rmpp_mad->rmpp_hdr.rmpp_status);
737 struct ib_mad_recv_wc *
738 ib_process_rmpp_recv_wc(struct ib_mad_agent_private *agent,
739 struct ib_mad_recv_wc *mad_recv_wc)
741 struct ib_rmpp_mad *rmpp_mad;
743 rmpp_mad = (struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad;
744 if (!(rmpp_mad->rmpp_hdr.rmpp_rtime_flags & IB_MGMT_RMPP_FLAG_ACTIVE))
747 if (rmpp_mad->rmpp_hdr.rmpp_version != IB_MGMT_RMPP_VERSION) {
748 abort_send(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_UNV);
749 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_UNV);
753 switch (rmpp_mad->rmpp_hdr.rmpp_type) {
754 case IB_MGMT_RMPP_TYPE_DATA:
755 return process_rmpp_data(agent, mad_recv_wc);
756 case IB_MGMT_RMPP_TYPE_ACK:
757 process_rmpp_ack(agent, mad_recv_wc);
759 case IB_MGMT_RMPP_TYPE_STOP:
760 process_rmpp_stop(agent, mad_recv_wc);
762 case IB_MGMT_RMPP_TYPE_ABORT:
763 process_rmpp_abort(agent, mad_recv_wc);
766 abort_send(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BADT);
767 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BADT);
771 ib_free_recv_mad(mad_recv_wc);
775 int ib_send_rmpp_mad(struct ib_mad_send_wr_private *mad_send_wr)
777 struct ib_rmpp_mad *rmpp_mad;
780 rmpp_mad = mad_send_wr->send_buf.mad;
781 if (!(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
782 IB_MGMT_RMPP_FLAG_ACTIVE))
783 return IB_RMPP_RESULT_UNHANDLED;
785 if (rmpp_mad->rmpp_hdr.rmpp_type != IB_MGMT_RMPP_TYPE_DATA) {
786 mad_send_wr->seg_num = 1;
787 return IB_RMPP_RESULT_INTERNAL;
790 mad_send_wr->newwin = 1;
792 /* We need to wait for the final ACK even if there isn't a response */
793 mad_send_wr->refcount += (mad_send_wr->timeout == 0);
794 ret = send_next_seg(mad_send_wr);
796 return IB_RMPP_RESULT_CONSUMED;
800 int ib_process_rmpp_send_wc(struct ib_mad_send_wr_private *mad_send_wr,
801 struct ib_mad_send_wc *mad_send_wc)
803 struct ib_rmpp_mad *rmpp_mad;
806 rmpp_mad = mad_send_wr->send_buf.mad;
807 if (!(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
808 IB_MGMT_RMPP_FLAG_ACTIVE))
809 return IB_RMPP_RESULT_UNHANDLED; /* RMPP not active */
811 if (rmpp_mad->rmpp_hdr.rmpp_type != IB_MGMT_RMPP_TYPE_DATA)
812 return IB_RMPP_RESULT_INTERNAL; /* ACK, STOP, or ABORT */
814 if (mad_send_wc->status != IB_WC_SUCCESS ||
815 mad_send_wr->status != IB_WC_SUCCESS)
816 return IB_RMPP_RESULT_PROCESSED; /* Canceled or send error */
818 if (!mad_send_wr->timeout)
819 return IB_RMPP_RESULT_PROCESSED; /* Response received */
821 if (mad_send_wr->last_ack == mad_send_wr->send_buf.seg_count) {
822 mad_send_wr->timeout =
823 msecs_to_jiffies(mad_send_wr->send_buf.timeout_ms);
824 return IB_RMPP_RESULT_PROCESSED; /* Send done */
827 if (mad_send_wr->seg_num == mad_send_wr->newwin ||
828 mad_send_wr->seg_num == mad_send_wr->send_buf.seg_count)
829 return IB_RMPP_RESULT_PROCESSED; /* Wait for ACK */
831 ret = send_next_seg(mad_send_wr);
833 mad_send_wc->status = IB_WC_GENERAL_ERR;
834 return IB_RMPP_RESULT_PROCESSED;
836 return IB_RMPP_RESULT_CONSUMED;
839 int ib_retry_rmpp(struct ib_mad_send_wr_private *mad_send_wr)
841 struct ib_rmpp_mad *rmpp_mad;
844 rmpp_mad = mad_send_wr->send_buf.mad;
845 if (!(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
846 IB_MGMT_RMPP_FLAG_ACTIVE))
847 return IB_RMPP_RESULT_UNHANDLED; /* RMPP not active */
849 if (mad_send_wr->last_ack == mad_send_wr->send_buf.seg_count)
850 return IB_RMPP_RESULT_PROCESSED;
852 mad_send_wr->seg_num = mad_send_wr->last_ack;
853 mad_send_wr->cur_seg = mad_send_wr->last_ack_seg;
855 ret = send_next_seg(mad_send_wr);
857 return IB_RMPP_RESULT_PROCESSED;
859 return IB_RMPP_RESULT_CONSUMED;