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_rmpp_mad *ack,
115 struct ib_rmpp_mad *data,
116 struct mad_rmpp_recv *rmpp_recv)
120 memcpy(&ack->mad_hdr, &data->mad_hdr,
121 data_offset(data->mad_hdr.mgmt_class));
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 msg = ib_create_send_mad(&rmpp_recv->agent->agent, recv_wc->wc->src_qp,
141 recv_wc->wc->pkey_index, 1, IB_MGMT_RMPP_HDR,
142 IB_MGMT_RMPP_DATA, GFP_KERNEL);
146 format_ack(msg->mad, (struct ib_rmpp_mad *) recv_wc->recv_buf.mad,
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;
160 ah = ib_create_ah_from_wc(agent->qp->pd, recv_wc->wc,
161 recv_wc->recv_buf.grh, agent->port_num);
165 msg = ib_create_send_mad(agent, recv_wc->wc->src_qp,
166 recv_wc->wc->pkey_index, 1,
167 IB_MGMT_RMPP_HDR, IB_MGMT_RMPP_DATA,
177 void ib_rmpp_send_handler(struct ib_mad_send_wc *mad_send_wc)
179 struct ib_rmpp_mad *rmpp_mad = mad_send_wc->send_buf->mad;
181 if (rmpp_mad->rmpp_hdr.rmpp_type != IB_MGMT_RMPP_TYPE_ACK)
182 ib_destroy_ah(mad_send_wc->send_buf->ah);
183 ib_free_send_mad(mad_send_wc->send_buf);
186 static void nack_recv(struct ib_mad_agent_private *agent,
187 struct ib_mad_recv_wc *recv_wc, u8 rmpp_status)
189 struct ib_mad_send_buf *msg;
190 struct ib_rmpp_mad *rmpp_mad;
193 msg = alloc_response_msg(&agent->agent, recv_wc);
198 memcpy(rmpp_mad, recv_wc->recv_buf.mad,
199 data_offset(recv_wc->recv_buf.mad->mad_hdr.mgmt_class));
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 void ib_coalesce_recv_mad(struct ib_mad_recv_wc *mad_recv_wc, void *buf)
438 struct ib_mad_recv_buf *seg_buf;
439 struct ib_rmpp_mad *rmpp_mad;
441 int size, len, offset;
444 len = mad_recv_wc->mad_len;
445 if (len <= sizeof(struct ib_mad)) {
446 memcpy(buf, mad_recv_wc->recv_buf.mad, len);
450 offset = data_offset(mad_recv_wc->recv_buf.mad->mad_hdr.mgmt_class);
452 list_for_each_entry(seg_buf, &mad_recv_wc->rmpp_list, list) {
453 rmpp_mad = (struct ib_rmpp_mad *)seg_buf->mad;
454 flags = ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr);
456 if (flags & IB_MGMT_RMPP_FLAG_FIRST) {
458 size = sizeof(*rmpp_mad);
460 data = (void *) rmpp_mad + offset;
461 if (flags & IB_MGMT_RMPP_FLAG_LAST)
464 size = sizeof(*rmpp_mad) - offset;
467 memcpy(buf, data, size);
472 EXPORT_SYMBOL(ib_coalesce_recv_mad);
474 static struct ib_mad_recv_wc *
475 continue_rmpp(struct ib_mad_agent_private *agent,
476 struct ib_mad_recv_wc *mad_recv_wc)
478 struct mad_rmpp_recv *rmpp_recv;
479 struct ib_mad_recv_buf *prev_buf;
480 struct ib_mad_recv_wc *done_wc;
484 rmpp_recv = acquire_rmpp_recv(agent, mad_recv_wc);
488 seg_num = get_seg_num(&mad_recv_wc->recv_buf);
490 spin_lock_irqsave(&rmpp_recv->lock, flags);
491 if ((rmpp_recv->state == RMPP_STATE_TIMEOUT) ||
492 (seg_num > rmpp_recv->newwin))
495 if ((seg_num <= rmpp_recv->last_ack) ||
496 (rmpp_recv->state == RMPP_STATE_COMPLETE)) {
497 spin_unlock_irqrestore(&rmpp_recv->lock, flags);
498 ack_recv(rmpp_recv, mad_recv_wc);
502 prev_buf = find_seg_location(&rmpp_recv->rmpp_wc->rmpp_list, seg_num);
507 list_add(&mad_recv_wc->recv_buf.list, &prev_buf->list);
508 if (rmpp_recv->cur_seg_buf == prev_buf) {
509 update_seg_num(rmpp_recv, &mad_recv_wc->recv_buf);
510 if (get_last_flag(rmpp_recv->cur_seg_buf)) {
511 rmpp_recv->state = RMPP_STATE_COMPLETE;
512 spin_unlock_irqrestore(&rmpp_recv->lock, flags);
513 done_wc = complete_rmpp(rmpp_recv);
515 } else if (rmpp_recv->seg_num == rmpp_recv->newwin) {
516 rmpp_recv->newwin += window_size(agent);
517 spin_unlock_irqrestore(&rmpp_recv->lock, flags);
518 ack_recv(rmpp_recv, mad_recv_wc);
522 spin_unlock_irqrestore(&rmpp_recv->lock, flags);
524 deref_rmpp_recv(rmpp_recv);
527 drop3: spin_unlock_irqrestore(&rmpp_recv->lock, flags);
528 drop2: deref_rmpp_recv(rmpp_recv);
529 drop1: ib_free_recv_mad(mad_recv_wc);
533 static struct ib_mad_recv_wc *
534 start_rmpp(struct ib_mad_agent_private *agent,
535 struct ib_mad_recv_wc *mad_recv_wc)
537 struct mad_rmpp_recv *rmpp_recv;
540 rmpp_recv = create_rmpp_recv(agent, mad_recv_wc);
542 ib_free_recv_mad(mad_recv_wc);
546 spin_lock_irqsave(&agent->lock, flags);
547 if (insert_rmpp_recv(agent, rmpp_recv)) {
548 spin_unlock_irqrestore(&agent->lock, flags);
549 /* duplicate first MAD */
550 destroy_rmpp_recv(rmpp_recv);
551 return continue_rmpp(agent, mad_recv_wc);
553 atomic_inc(&rmpp_recv->refcount);
555 if (get_last_flag(&mad_recv_wc->recv_buf)) {
556 rmpp_recv->state = RMPP_STATE_COMPLETE;
557 spin_unlock_irqrestore(&agent->lock, flags);
558 complete_rmpp(rmpp_recv);
560 spin_unlock_irqrestore(&agent->lock, flags);
561 /* 40 seconds until we can find the packet lifetimes */
562 queue_delayed_work(agent->qp_info->port_priv->wq,
563 &rmpp_recv->timeout_work,
564 msecs_to_jiffies(40000));
565 rmpp_recv->newwin += window_size(agent);
566 ack_recv(rmpp_recv, mad_recv_wc);
569 deref_rmpp_recv(rmpp_recv);
573 static inline u64 get_seg_addr(struct ib_mad_send_wr_private *mad_send_wr)
575 return mad_send_wr->sg_list[0].addr + mad_send_wr->data_offset +
576 (sizeof(struct ib_rmpp_mad) - mad_send_wr->data_offset) *
577 (mad_send_wr->seg_num - 1);
580 static int send_next_seg(struct ib_mad_send_wr_private *mad_send_wr)
582 struct ib_rmpp_mad *rmpp_mad;
586 rmpp_mad = mad_send_wr->send_buf.mad;
587 ib_set_rmpp_flags(&rmpp_mad->rmpp_hdr, IB_MGMT_RMPP_FLAG_ACTIVE);
588 rmpp_mad->rmpp_hdr.seg_num = cpu_to_be32(mad_send_wr->seg_num);
590 if (mad_send_wr->seg_num == 1) {
591 rmpp_mad->rmpp_hdr.rmpp_rtime_flags |= IB_MGMT_RMPP_FLAG_FIRST;
592 paylen = mad_send_wr->total_seg * IB_MGMT_RMPP_DATA -
594 rmpp_mad->rmpp_hdr.paylen_newwin = cpu_to_be32(paylen);
595 mad_send_wr->sg_list[0].length = sizeof(struct ib_rmpp_mad);
597 mad_send_wr->send_wr.num_sge = 2;
598 mad_send_wr->sg_list[0].length = mad_send_wr->data_offset;
599 mad_send_wr->sg_list[1].addr = get_seg_addr(mad_send_wr);
600 mad_send_wr->sg_list[1].length = sizeof(struct ib_rmpp_mad) -
601 mad_send_wr->data_offset;
602 mad_send_wr->sg_list[1].lkey = mad_send_wr->sg_list[0].lkey;
603 rmpp_mad->rmpp_hdr.paylen_newwin = 0;
606 if (mad_send_wr->seg_num == mad_send_wr->total_seg) {
607 rmpp_mad->rmpp_hdr.rmpp_rtime_flags |= IB_MGMT_RMPP_FLAG_LAST;
608 paylen = IB_MGMT_RMPP_DATA - mad_send_wr->pad;
609 rmpp_mad->rmpp_hdr.paylen_newwin = cpu_to_be32(paylen);
612 /* 2 seconds for an ACK until we can find the packet lifetime */
613 timeout = mad_send_wr->send_buf.timeout_ms;
614 if (!timeout || timeout > 2000)
615 mad_send_wr->timeout = msecs_to_jiffies(2000);
616 mad_send_wr->seg_num++;
617 return ib_send_mad(mad_send_wr);
620 static void abort_send(struct ib_mad_agent_private *agent, __be64 tid,
623 struct ib_mad_send_wr_private *mad_send_wr;
624 struct ib_mad_send_wc wc;
627 spin_lock_irqsave(&agent->lock, flags);
628 mad_send_wr = ib_find_send_mad(agent, tid);
630 goto out; /* Unmatched send */
632 if ((mad_send_wr->last_ack == mad_send_wr->total_seg) ||
633 (!mad_send_wr->timeout) || (mad_send_wr->status != IB_WC_SUCCESS))
634 goto out; /* Send is already done */
636 ib_mark_mad_done(mad_send_wr);
637 spin_unlock_irqrestore(&agent->lock, flags);
639 wc.status = IB_WC_REM_ABORT_ERR;
640 wc.vendor_err = rmpp_status;
641 wc.send_buf = &mad_send_wr->send_buf;
642 ib_mad_complete_send_wr(mad_send_wr, &wc);
645 spin_unlock_irqrestore(&agent->lock, flags);
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, rmpp_mad->mad_hdr.tid,
659 IB_MGMT_RMPP_STATUS_BAD_STATUS);
660 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BAD_STATUS);
664 seg_num = be32_to_cpu(rmpp_mad->rmpp_hdr.seg_num);
665 newwin = be32_to_cpu(rmpp_mad->rmpp_hdr.paylen_newwin);
666 if (newwin < seg_num) {
667 abort_send(agent, rmpp_mad->mad_hdr.tid,
668 IB_MGMT_RMPP_STATUS_W2S);
669 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_W2S);
673 spin_lock_irqsave(&agent->lock, flags);
674 mad_send_wr = ib_find_send_mad(agent, rmpp_mad->mad_hdr.tid);
676 goto out; /* Unmatched ACK */
678 if ((mad_send_wr->last_ack == mad_send_wr->total_seg) ||
679 (!mad_send_wr->timeout) || (mad_send_wr->status != IB_WC_SUCCESS))
680 goto out; /* Send is already done */
682 if (seg_num > mad_send_wr->total_seg || seg_num > mad_send_wr->newwin) {
683 spin_unlock_irqrestore(&agent->lock, flags);
684 abort_send(agent, rmpp_mad->mad_hdr.tid,
685 IB_MGMT_RMPP_STATUS_S2B);
686 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_S2B);
690 if (newwin < mad_send_wr->newwin || seg_num < mad_send_wr->last_ack)
691 goto out; /* Old ACK */
693 if (seg_num > mad_send_wr->last_ack) {
694 mad_send_wr->last_ack = seg_num;
695 mad_send_wr->retries = mad_send_wr->send_buf.retries;
697 mad_send_wr->newwin = newwin;
698 if (mad_send_wr->last_ack == mad_send_wr->total_seg) {
699 /* If no response is expected, the ACK completes the send */
700 if (!mad_send_wr->send_buf.timeout_ms) {
701 struct ib_mad_send_wc wc;
703 ib_mark_mad_done(mad_send_wr);
704 spin_unlock_irqrestore(&agent->lock, flags);
706 wc.status = IB_WC_SUCCESS;
708 wc.send_buf = &mad_send_wr->send_buf;
709 ib_mad_complete_send_wr(mad_send_wr, &wc);
712 if (mad_send_wr->refcount == 1)
713 ib_reset_mad_timeout(mad_send_wr,
714 mad_send_wr->send_buf.timeout_ms);
715 } else if (mad_send_wr->refcount == 1 &&
716 mad_send_wr->seg_num < mad_send_wr->newwin &&
717 mad_send_wr->seg_num <= mad_send_wr->total_seg) {
718 /* Send failure will just result in a timeout/retry */
719 ret = send_next_seg(mad_send_wr);
723 mad_send_wr->refcount++;
724 list_del(&mad_send_wr->agent_list);
725 list_add_tail(&mad_send_wr->agent_list,
726 &mad_send_wr->mad_agent_priv->send_list);
729 spin_unlock_irqrestore(&agent->lock, flags);
732 static struct ib_mad_recv_wc *
733 process_rmpp_data(struct ib_mad_agent_private *agent,
734 struct ib_mad_recv_wc *mad_recv_wc)
736 struct ib_rmpp_hdr *rmpp_hdr;
739 rmpp_hdr = &((struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad)->rmpp_hdr;
741 if (rmpp_hdr->rmpp_status) {
742 rmpp_status = IB_MGMT_RMPP_STATUS_BAD_STATUS;
746 if (rmpp_hdr->seg_num == __constant_htonl(1)) {
747 if (!(ib_get_rmpp_flags(rmpp_hdr) & IB_MGMT_RMPP_FLAG_FIRST)) {
748 rmpp_status = IB_MGMT_RMPP_STATUS_BAD_SEG;
751 return start_rmpp(agent, mad_recv_wc);
753 if (ib_get_rmpp_flags(rmpp_hdr) & IB_MGMT_RMPP_FLAG_FIRST) {
754 rmpp_status = IB_MGMT_RMPP_STATUS_BAD_SEG;
757 return continue_rmpp(agent, mad_recv_wc);
760 nack_recv(agent, mad_recv_wc, rmpp_status);
761 ib_free_recv_mad(mad_recv_wc);
765 static void process_rmpp_stop(struct ib_mad_agent_private *agent,
766 struct ib_mad_recv_wc *mad_recv_wc)
768 struct ib_rmpp_mad *rmpp_mad;
770 rmpp_mad = (struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad;
772 if (rmpp_mad->rmpp_hdr.rmpp_status != IB_MGMT_RMPP_STATUS_RESX) {
773 abort_send(agent, rmpp_mad->mad_hdr.tid,
774 IB_MGMT_RMPP_STATUS_BAD_STATUS);
775 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BAD_STATUS);
777 abort_send(agent, rmpp_mad->mad_hdr.tid,
778 rmpp_mad->rmpp_hdr.rmpp_status);
781 static void process_rmpp_abort(struct ib_mad_agent_private *agent,
782 struct ib_mad_recv_wc *mad_recv_wc)
784 struct ib_rmpp_mad *rmpp_mad;
786 rmpp_mad = (struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad;
788 if (rmpp_mad->rmpp_hdr.rmpp_status < IB_MGMT_RMPP_STATUS_ABORT_MIN ||
789 rmpp_mad->rmpp_hdr.rmpp_status > IB_MGMT_RMPP_STATUS_ABORT_MAX) {
790 abort_send(agent, rmpp_mad->mad_hdr.tid,
791 IB_MGMT_RMPP_STATUS_BAD_STATUS);
792 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BAD_STATUS);
794 abort_send(agent, rmpp_mad->mad_hdr.tid,
795 rmpp_mad->rmpp_hdr.rmpp_status);
798 struct ib_mad_recv_wc *
799 ib_process_rmpp_recv_wc(struct ib_mad_agent_private *agent,
800 struct ib_mad_recv_wc *mad_recv_wc)
802 struct ib_rmpp_mad *rmpp_mad;
804 rmpp_mad = (struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad;
805 if (!(rmpp_mad->rmpp_hdr.rmpp_rtime_flags & IB_MGMT_RMPP_FLAG_ACTIVE))
808 if (rmpp_mad->rmpp_hdr.rmpp_version != IB_MGMT_RMPP_VERSION) {
809 abort_send(agent, rmpp_mad->mad_hdr.tid,
810 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, rmpp_mad->mad_hdr.tid,
829 IB_MGMT_RMPP_STATUS_BADT);
830 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BADT);
834 ib_free_recv_mad(mad_recv_wc);
838 int ib_send_rmpp_mad(struct ib_mad_send_wr_private *mad_send_wr)
840 struct ib_rmpp_mad *rmpp_mad;
841 int i, total_len, ret;
843 rmpp_mad = mad_send_wr->send_buf.mad;
844 if (!(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
845 IB_MGMT_RMPP_FLAG_ACTIVE))
846 return IB_RMPP_RESULT_UNHANDLED;
848 if (rmpp_mad->rmpp_hdr.rmpp_type != IB_MGMT_RMPP_TYPE_DATA)
849 return IB_RMPP_RESULT_INTERNAL;
851 if (mad_send_wr->send_wr.num_sge > 1)
852 return -EINVAL; /* TODO: support num_sge > 1 */
854 mad_send_wr->seg_num = 1;
855 mad_send_wr->newwin = 1;
856 mad_send_wr->data_offset = data_offset(rmpp_mad->mad_hdr.mgmt_class);
859 for (i = 0; i < mad_send_wr->send_wr.num_sge; i++)
860 total_len += mad_send_wr->send_wr.sg_list[i].length;
862 mad_send_wr->total_seg = (total_len - mad_send_wr->data_offset) /
863 (sizeof(struct ib_rmpp_mad) - mad_send_wr->data_offset);
864 mad_send_wr->pad = total_len - IB_MGMT_RMPP_HDR -
865 be32_to_cpu(rmpp_mad->rmpp_hdr.paylen_newwin);
867 /* We need to wait for the final ACK even if there isn't a response */
868 mad_send_wr->refcount += (mad_send_wr->timeout == 0);
869 ret = send_next_seg(mad_send_wr);
871 return IB_RMPP_RESULT_CONSUMED;
875 int ib_process_rmpp_send_wc(struct ib_mad_send_wr_private *mad_send_wr,
876 struct ib_mad_send_wc *mad_send_wc)
878 struct ib_rmpp_mad *rmpp_mad;
881 rmpp_mad = mad_send_wr->send_buf.mad;
882 if (!(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
883 IB_MGMT_RMPP_FLAG_ACTIVE))
884 return IB_RMPP_RESULT_UNHANDLED; /* RMPP not active */
886 if (rmpp_mad->rmpp_hdr.rmpp_type != IB_MGMT_RMPP_TYPE_DATA)
887 return IB_RMPP_RESULT_INTERNAL; /* ACK, STOP, or ABORT */
889 if (mad_send_wc->status != IB_WC_SUCCESS ||
890 mad_send_wr->status != IB_WC_SUCCESS)
891 return IB_RMPP_RESULT_PROCESSED; /* Canceled or send error */
893 if (!mad_send_wr->timeout)
894 return IB_RMPP_RESULT_PROCESSED; /* Response received */
896 if (mad_send_wr->last_ack == mad_send_wr->total_seg) {
897 mad_send_wr->timeout =
898 msecs_to_jiffies(mad_send_wr->send_buf.timeout_ms);
899 return IB_RMPP_RESULT_PROCESSED; /* Send done */
902 if (mad_send_wr->seg_num > mad_send_wr->newwin ||
903 mad_send_wr->seg_num > mad_send_wr->total_seg)
904 return IB_RMPP_RESULT_PROCESSED; /* Wait for ACK */
906 ret = send_next_seg(mad_send_wr);
908 mad_send_wc->status = IB_WC_GENERAL_ERR;
909 return IB_RMPP_RESULT_PROCESSED;
911 return IB_RMPP_RESULT_CONSUMED;
914 int ib_retry_rmpp(struct ib_mad_send_wr_private *mad_send_wr)
916 struct ib_rmpp_mad *rmpp_mad;
919 rmpp_mad = mad_send_wr->send_buf.mad;
920 if (!(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
921 IB_MGMT_RMPP_FLAG_ACTIVE))
922 return IB_RMPP_RESULT_UNHANDLED; /* RMPP not active */
924 if (mad_send_wr->last_ack == mad_send_wr->total_seg)
925 return IB_RMPP_RESULT_PROCESSED;
927 mad_send_wr->seg_num = mad_send_wr->last_ack + 1;
928 ret = send_next_seg(mad_send_wr);
930 return IB_RMPP_RESULT_PROCESSED;
932 return IB_RMPP_RESULT_CONSUMED;