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 offsetof(struct ib_sa_mad, data);
107 else if ((mgmt_class >= IB_MGMT_CLASS_VENDOR_RANGE2_START) &&
108 (mgmt_class <= IB_MGMT_CLASS_VENDOR_RANGE2_END))
109 return offsetof(struct ib_vendor_mad, data);
111 return offsetof(struct ib_rmpp_mad, data);
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;
138 struct ib_send_wr *bad_send_wr;
141 hdr_len = sizeof(struct ib_mad_hdr) + sizeof(struct ib_rmpp_hdr);
142 msg = ib_create_send_mad(&rmpp_recv->agent->agent, recv_wc->wc->src_qp,
143 recv_wc->wc->pkey_index, rmpp_recv->ah, 1,
144 hdr_len, sizeof(struct ib_rmpp_mad) - hdr_len,
149 format_ack((struct ib_rmpp_mad *) msg->mad,
150 (struct ib_rmpp_mad *) recv_wc->recv_buf.mad, rmpp_recv);
151 ret = ib_post_send_mad(&rmpp_recv->agent->agent, &msg->send_wr,
154 ib_free_send_mad(msg);
157 static int alloc_response_msg(struct ib_mad_agent *agent,
158 struct ib_mad_recv_wc *recv_wc,
159 struct ib_mad_send_buf **msg)
161 struct ib_mad_send_buf *m;
165 ah = ib_create_ah_from_wc(agent->qp->pd, recv_wc->wc,
166 recv_wc->recv_buf.grh, agent->port_num);
170 hdr_len = sizeof(struct ib_mad_hdr) + sizeof(struct ib_rmpp_hdr);
171 m = ib_create_send_mad(agent, recv_wc->wc->src_qp,
172 recv_wc->wc->pkey_index, ah, 1, hdr_len,
173 sizeof(struct ib_rmpp_mad) - hdr_len,
183 static void free_msg(struct ib_mad_send_buf *msg)
185 ib_destroy_ah(msg->send_wr.wr.ud.ah);
186 ib_free_send_mad(msg);
189 static void nack_recv(struct ib_mad_agent_private *agent,
190 struct ib_mad_recv_wc *recv_wc, u8 rmpp_status)
192 struct ib_mad_send_buf *msg;
193 struct ib_rmpp_mad *rmpp_mad;
194 struct ib_send_wr *bad_send_wr;
197 ret = alloc_response_msg(&agent->agent, recv_wc, &msg);
201 rmpp_mad = (struct ib_rmpp_mad *) msg->mad;
202 memcpy(rmpp_mad, recv_wc->recv_buf.mad,
203 data_offset(recv_wc->recv_buf.mad->mad_hdr.mgmt_class));
205 rmpp_mad->mad_hdr.method ^= IB_MGMT_METHOD_RESP;
206 rmpp_mad->rmpp_hdr.rmpp_version = IB_MGMT_RMPP_VERSION;
207 rmpp_mad->rmpp_hdr.rmpp_type = IB_MGMT_RMPP_TYPE_ABORT;
208 ib_set_rmpp_flags(&rmpp_mad->rmpp_hdr, IB_MGMT_RMPP_FLAG_ACTIVE);
209 rmpp_mad->rmpp_hdr.rmpp_status = rmpp_status;
210 rmpp_mad->rmpp_hdr.seg_num = 0;
211 rmpp_mad->rmpp_hdr.paylen_newwin = 0;
213 ret = ib_post_send_mad(&agent->agent, &msg->send_wr, &bad_send_wr);
218 static void recv_timeout_handler(void *data)
220 struct mad_rmpp_recv *rmpp_recv = data;
221 struct ib_mad_recv_wc *rmpp_wc;
224 spin_lock_irqsave(&rmpp_recv->agent->lock, flags);
225 if (rmpp_recv->state != RMPP_STATE_ACTIVE) {
226 spin_unlock_irqrestore(&rmpp_recv->agent->lock, flags);
229 rmpp_recv->state = RMPP_STATE_TIMEOUT;
230 list_del(&rmpp_recv->list);
231 spin_unlock_irqrestore(&rmpp_recv->agent->lock, flags);
233 rmpp_wc = rmpp_recv->rmpp_wc;
234 nack_recv(rmpp_recv->agent, rmpp_wc, IB_MGMT_RMPP_STATUS_T2L);
235 destroy_rmpp_recv(rmpp_recv);
236 ib_free_recv_mad(rmpp_wc);
239 static void recv_cleanup_handler(void *data)
241 struct mad_rmpp_recv *rmpp_recv = data;
244 spin_lock_irqsave(&rmpp_recv->agent->lock, flags);
245 list_del(&rmpp_recv->list);
246 spin_unlock_irqrestore(&rmpp_recv->agent->lock, flags);
247 destroy_rmpp_recv(rmpp_recv);
250 static struct mad_rmpp_recv *
251 create_rmpp_recv(struct ib_mad_agent_private *agent,
252 struct ib_mad_recv_wc *mad_recv_wc)
254 struct mad_rmpp_recv *rmpp_recv;
255 struct ib_mad_hdr *mad_hdr;
257 rmpp_recv = kmalloc(sizeof *rmpp_recv, GFP_KERNEL);
261 rmpp_recv->ah = ib_create_ah_from_wc(agent->agent.qp->pd,
263 mad_recv_wc->recv_buf.grh,
264 agent->agent.port_num);
265 if (IS_ERR(rmpp_recv->ah))
268 rmpp_recv->agent = agent;
269 init_waitqueue_head(&rmpp_recv->wait);
270 INIT_WORK(&rmpp_recv->timeout_work, recv_timeout_handler, rmpp_recv);
271 INIT_WORK(&rmpp_recv->cleanup_work, recv_cleanup_handler, rmpp_recv);
272 spin_lock_init(&rmpp_recv->lock);
273 rmpp_recv->state = RMPP_STATE_ACTIVE;
274 atomic_set(&rmpp_recv->refcount, 1);
276 rmpp_recv->rmpp_wc = mad_recv_wc;
277 rmpp_recv->cur_seg_buf = &mad_recv_wc->recv_buf;
278 rmpp_recv->newwin = 1;
279 rmpp_recv->seg_num = 1;
280 rmpp_recv->last_ack = 0;
282 mad_hdr = &mad_recv_wc->recv_buf.mad->mad_hdr;
283 rmpp_recv->tid = mad_hdr->tid;
284 rmpp_recv->src_qp = mad_recv_wc->wc->src_qp;
285 rmpp_recv->slid = mad_recv_wc->wc->slid;
286 rmpp_recv->mgmt_class = mad_hdr->mgmt_class;
287 rmpp_recv->class_version = mad_hdr->class_version;
288 rmpp_recv->method = mad_hdr->method;
291 error: kfree(rmpp_recv);
295 static inline void deref_rmpp_recv(struct mad_rmpp_recv *rmpp_recv)
297 if (atomic_dec_and_test(&rmpp_recv->refcount))
298 wake_up(&rmpp_recv->wait);
301 static struct mad_rmpp_recv *
302 find_rmpp_recv(struct ib_mad_agent_private *agent,
303 struct ib_mad_recv_wc *mad_recv_wc)
305 struct mad_rmpp_recv *rmpp_recv;
306 struct ib_mad_hdr *mad_hdr = &mad_recv_wc->recv_buf.mad->mad_hdr;
308 list_for_each_entry(rmpp_recv, &agent->rmpp_list, list) {
309 if (rmpp_recv->tid == mad_hdr->tid &&
310 rmpp_recv->src_qp == mad_recv_wc->wc->src_qp &&
311 rmpp_recv->slid == mad_recv_wc->wc->slid &&
312 rmpp_recv->mgmt_class == mad_hdr->mgmt_class &&
313 rmpp_recv->class_version == mad_hdr->class_version &&
314 rmpp_recv->method == mad_hdr->method)
320 static struct mad_rmpp_recv *
321 acquire_rmpp_recv(struct ib_mad_agent_private *agent,
322 struct ib_mad_recv_wc *mad_recv_wc)
324 struct mad_rmpp_recv *rmpp_recv;
327 spin_lock_irqsave(&agent->lock, flags);
328 rmpp_recv = find_rmpp_recv(agent, mad_recv_wc);
330 atomic_inc(&rmpp_recv->refcount);
331 spin_unlock_irqrestore(&agent->lock, flags);
335 static struct mad_rmpp_recv *
336 insert_rmpp_recv(struct ib_mad_agent_private *agent,
337 struct mad_rmpp_recv *rmpp_recv)
339 struct mad_rmpp_recv *cur_rmpp_recv;
341 cur_rmpp_recv = find_rmpp_recv(agent, rmpp_recv->rmpp_wc);
343 list_add_tail(&rmpp_recv->list, &agent->rmpp_list);
345 return cur_rmpp_recv;
348 static inline int get_last_flag(struct ib_mad_recv_buf *seg)
350 struct ib_rmpp_mad *rmpp_mad;
352 rmpp_mad = (struct ib_rmpp_mad *) seg->mad;
353 return ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) & IB_MGMT_RMPP_FLAG_LAST;
356 static inline int get_seg_num(struct ib_mad_recv_buf *seg)
358 struct ib_rmpp_mad *rmpp_mad;
360 rmpp_mad = (struct ib_rmpp_mad *) seg->mad;
361 return be32_to_cpu(rmpp_mad->rmpp_hdr.seg_num);
364 static inline struct ib_mad_recv_buf * get_next_seg(struct list_head *rmpp_list,
365 struct ib_mad_recv_buf *seg)
367 if (seg->list.next == rmpp_list)
370 return container_of(seg->list.next, struct ib_mad_recv_buf, list);
373 static inline int window_size(struct ib_mad_agent_private *agent)
375 return max(agent->qp_info->recv_queue.max_active >> 3, 1);
378 static struct ib_mad_recv_buf * find_seg_location(struct list_head *rmpp_list,
381 struct ib_mad_recv_buf *seg_buf;
384 list_for_each_entry_reverse(seg_buf, rmpp_list, list) {
385 cur_seg_num = get_seg_num(seg_buf);
386 if (seg_num > cur_seg_num)
388 if (seg_num == cur_seg_num)
394 static void update_seg_num(struct mad_rmpp_recv *rmpp_recv,
395 struct ib_mad_recv_buf *new_buf)
397 struct list_head *rmpp_list = &rmpp_recv->rmpp_wc->rmpp_list;
399 while (new_buf && (get_seg_num(new_buf) == rmpp_recv->seg_num + 1)) {
400 rmpp_recv->cur_seg_buf = new_buf;
401 rmpp_recv->seg_num++;
402 new_buf = get_next_seg(rmpp_list, new_buf);
406 static inline int get_mad_len(struct mad_rmpp_recv *rmpp_recv)
408 struct ib_rmpp_mad *rmpp_mad;
409 int hdr_size, data_size, pad;
411 rmpp_mad = (struct ib_rmpp_mad *)rmpp_recv->cur_seg_buf->mad;
413 hdr_size = data_offset(rmpp_mad->mad_hdr.mgmt_class);
414 data_size = sizeof(struct ib_rmpp_mad) - hdr_size;
415 pad = IB_MGMT_RMPP_DATA - be32_to_cpu(rmpp_mad->rmpp_hdr.paylen_newwin);
416 if (pad > IB_MGMT_RMPP_DATA || pad < 0)
419 return hdr_size + rmpp_recv->seg_num * data_size - pad;
422 static struct ib_mad_recv_wc * complete_rmpp(struct mad_rmpp_recv *rmpp_recv)
424 struct ib_mad_recv_wc *rmpp_wc;
426 ack_recv(rmpp_recv, rmpp_recv->rmpp_wc);
427 if (rmpp_recv->seg_num > 1)
428 cancel_delayed_work(&rmpp_recv->timeout_work);
430 rmpp_wc = rmpp_recv->rmpp_wc;
431 rmpp_wc->mad_len = get_mad_len(rmpp_recv);
432 /* 10 seconds until we can find the packet lifetime */
433 queue_delayed_work(rmpp_recv->agent->qp_info->port_priv->wq,
434 &rmpp_recv->cleanup_work, msecs_to_jiffies(10000));
438 void ib_coalesce_recv_mad(struct ib_mad_recv_wc *mad_recv_wc, void *buf)
440 struct ib_mad_recv_buf *seg_buf;
441 struct ib_rmpp_mad *rmpp_mad;
443 int size, len, offset;
446 len = mad_recv_wc->mad_len;
447 if (len <= sizeof(struct ib_mad)) {
448 memcpy(buf, mad_recv_wc->recv_buf.mad, len);
452 offset = data_offset(mad_recv_wc->recv_buf.mad->mad_hdr.mgmt_class);
454 list_for_each_entry(seg_buf, &mad_recv_wc->rmpp_list, list) {
455 rmpp_mad = (struct ib_rmpp_mad *)seg_buf->mad;
456 flags = ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr);
458 if (flags & IB_MGMT_RMPP_FLAG_FIRST) {
460 size = sizeof(*rmpp_mad);
462 data = (void *) rmpp_mad + offset;
463 if (flags & IB_MGMT_RMPP_FLAG_LAST)
466 size = sizeof(*rmpp_mad) - offset;
469 memcpy(buf, data, size);
474 EXPORT_SYMBOL(ib_coalesce_recv_mad);
476 static struct ib_mad_recv_wc *
477 continue_rmpp(struct ib_mad_agent_private *agent,
478 struct ib_mad_recv_wc *mad_recv_wc)
480 struct mad_rmpp_recv *rmpp_recv;
481 struct ib_mad_recv_buf *prev_buf;
482 struct ib_mad_recv_wc *done_wc;
486 rmpp_recv = acquire_rmpp_recv(agent, mad_recv_wc);
490 seg_num = get_seg_num(&mad_recv_wc->recv_buf);
492 spin_lock_irqsave(&rmpp_recv->lock, flags);
493 if ((rmpp_recv->state == RMPP_STATE_TIMEOUT) ||
494 (seg_num > rmpp_recv->newwin))
497 if ((seg_num <= rmpp_recv->last_ack) ||
498 (rmpp_recv->state == RMPP_STATE_COMPLETE)) {
499 spin_unlock_irqrestore(&rmpp_recv->lock, flags);
500 ack_recv(rmpp_recv, mad_recv_wc);
504 prev_buf = find_seg_location(&rmpp_recv->rmpp_wc->rmpp_list, seg_num);
509 list_add(&mad_recv_wc->recv_buf.list, &prev_buf->list);
510 if (rmpp_recv->cur_seg_buf == prev_buf) {
511 update_seg_num(rmpp_recv, &mad_recv_wc->recv_buf);
512 if (get_last_flag(rmpp_recv->cur_seg_buf)) {
513 rmpp_recv->state = RMPP_STATE_COMPLETE;
514 spin_unlock_irqrestore(&rmpp_recv->lock, flags);
515 done_wc = complete_rmpp(rmpp_recv);
517 } else if (rmpp_recv->seg_num == rmpp_recv->newwin) {
518 rmpp_recv->newwin += window_size(agent);
519 spin_unlock_irqrestore(&rmpp_recv->lock, flags);
520 ack_recv(rmpp_recv, mad_recv_wc);
524 spin_unlock_irqrestore(&rmpp_recv->lock, flags);
526 deref_rmpp_recv(rmpp_recv);
529 drop3: spin_unlock_irqrestore(&rmpp_recv->lock, flags);
530 drop2: deref_rmpp_recv(rmpp_recv);
531 drop1: ib_free_recv_mad(mad_recv_wc);
535 static struct ib_mad_recv_wc *
536 start_rmpp(struct ib_mad_agent_private *agent,
537 struct ib_mad_recv_wc *mad_recv_wc)
539 struct mad_rmpp_recv *rmpp_recv;
542 rmpp_recv = create_rmpp_recv(agent, mad_recv_wc);
544 ib_free_recv_mad(mad_recv_wc);
548 spin_lock_irqsave(&agent->lock, flags);
549 if (insert_rmpp_recv(agent, rmpp_recv)) {
550 spin_unlock_irqrestore(&agent->lock, flags);
551 /* duplicate first MAD */
552 destroy_rmpp_recv(rmpp_recv);
553 return continue_rmpp(agent, mad_recv_wc);
555 atomic_inc(&rmpp_recv->refcount);
557 if (get_last_flag(&mad_recv_wc->recv_buf)) {
558 rmpp_recv->state = RMPP_STATE_COMPLETE;
559 spin_unlock_irqrestore(&agent->lock, flags);
560 complete_rmpp(rmpp_recv);
562 spin_unlock_irqrestore(&agent->lock, flags);
563 /* 40 seconds until we can find the packet lifetimes */
564 queue_delayed_work(agent->qp_info->port_priv->wq,
565 &rmpp_recv->timeout_work,
566 msecs_to_jiffies(40000));
567 rmpp_recv->newwin += window_size(agent);
568 ack_recv(rmpp_recv, mad_recv_wc);
571 deref_rmpp_recv(rmpp_recv);
575 static inline u64 get_seg_addr(struct ib_mad_send_wr_private *mad_send_wr)
577 return mad_send_wr->sg_list[0].addr + mad_send_wr->data_offset +
578 (sizeof(struct ib_rmpp_mad) - mad_send_wr->data_offset) *
579 (mad_send_wr->seg_num - 1);
582 static int send_next_seg(struct ib_mad_send_wr_private *mad_send_wr)
584 struct ib_rmpp_mad *rmpp_mad;
588 rmpp_mad = (struct ib_rmpp_mad *)mad_send_wr->send_wr.wr.ud.mad_hdr;
589 ib_set_rmpp_flags(&rmpp_mad->rmpp_hdr, IB_MGMT_RMPP_FLAG_ACTIVE);
590 rmpp_mad->rmpp_hdr.seg_num = cpu_to_be32(mad_send_wr->seg_num);
592 if (mad_send_wr->seg_num == 1) {
593 rmpp_mad->rmpp_hdr.rmpp_rtime_flags |= IB_MGMT_RMPP_FLAG_FIRST;
594 paylen = mad_send_wr->total_seg * IB_MGMT_RMPP_DATA -
596 rmpp_mad->rmpp_hdr.paylen_newwin = cpu_to_be32(paylen);
597 mad_send_wr->sg_list[0].length = sizeof(struct ib_rmpp_mad);
599 mad_send_wr->send_wr.num_sge = 2;
600 mad_send_wr->sg_list[0].length = mad_send_wr->data_offset;
601 mad_send_wr->sg_list[1].addr = get_seg_addr(mad_send_wr);
602 mad_send_wr->sg_list[1].length = sizeof(struct ib_rmpp_mad) -
603 mad_send_wr->data_offset;
604 mad_send_wr->sg_list[1].lkey = mad_send_wr->sg_list[0].lkey;
605 rmpp_mad->rmpp_hdr.paylen_newwin = 0;
608 if (mad_send_wr->seg_num == mad_send_wr->total_seg) {
609 rmpp_mad->rmpp_hdr.rmpp_rtime_flags |= IB_MGMT_RMPP_FLAG_LAST;
610 paylen = IB_MGMT_RMPP_DATA - mad_send_wr->pad;
611 rmpp_mad->rmpp_hdr.paylen_newwin = cpu_to_be32(paylen);
614 /* 2 seconds for an ACK until we can find the packet lifetime */
615 timeout = mad_send_wr->send_wr.wr.ud.timeout_ms;
616 if (!timeout || timeout > 2000)
617 mad_send_wr->timeout = msecs_to_jiffies(2000);
618 mad_send_wr->seg_num++;
619 return ib_send_mad(mad_send_wr);
622 static void abort_send(struct ib_mad_agent_private *agent, __be64 tid,
625 struct ib_mad_send_wr_private *mad_send_wr;
626 struct ib_mad_send_wc wc;
629 spin_lock_irqsave(&agent->lock, flags);
630 mad_send_wr = ib_find_send_mad(agent, tid);
632 goto out; /* Unmatched send */
634 if ((mad_send_wr->last_ack == mad_send_wr->total_seg) ||
635 (!mad_send_wr->timeout) || (mad_send_wr->status != IB_WC_SUCCESS))
636 goto out; /* Send is already done */
638 ib_mark_mad_done(mad_send_wr);
639 spin_unlock_irqrestore(&agent->lock, flags);
641 wc.status = IB_WC_REM_ABORT_ERR;
642 wc.vendor_err = rmpp_status;
643 wc.wr_id = mad_send_wr->wr_id;
644 ib_mad_complete_send_wr(mad_send_wr, &wc);
647 spin_unlock_irqrestore(&agent->lock, flags);
650 static void process_rmpp_ack(struct ib_mad_agent_private *agent,
651 struct ib_mad_recv_wc *mad_recv_wc)
653 struct ib_mad_send_wr_private *mad_send_wr;
654 struct ib_rmpp_mad *rmpp_mad;
656 int seg_num, newwin, ret;
658 rmpp_mad = (struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad;
659 if (rmpp_mad->rmpp_hdr.rmpp_status) {
660 abort_send(agent, rmpp_mad->mad_hdr.tid,
661 IB_MGMT_RMPP_STATUS_BAD_STATUS);
662 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BAD_STATUS);
666 seg_num = be32_to_cpu(rmpp_mad->rmpp_hdr.seg_num);
667 newwin = be32_to_cpu(rmpp_mad->rmpp_hdr.paylen_newwin);
668 if (newwin < seg_num) {
669 abort_send(agent, rmpp_mad->mad_hdr.tid,
670 IB_MGMT_RMPP_STATUS_W2S);
671 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_W2S);
675 spin_lock_irqsave(&agent->lock, flags);
676 mad_send_wr = ib_find_send_mad(agent, rmpp_mad->mad_hdr.tid);
678 goto out; /* Unmatched ACK */
680 if ((mad_send_wr->last_ack == mad_send_wr->total_seg) ||
681 (!mad_send_wr->timeout) || (mad_send_wr->status != IB_WC_SUCCESS))
682 goto out; /* Send is already done */
684 if (seg_num > mad_send_wr->total_seg || seg_num > mad_send_wr->newwin) {
685 spin_unlock_irqrestore(&agent->lock, flags);
686 abort_send(agent, rmpp_mad->mad_hdr.tid,
687 IB_MGMT_RMPP_STATUS_S2B);
688 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_S2B);
692 if (newwin < mad_send_wr->newwin || seg_num < mad_send_wr->last_ack)
693 goto out; /* Old ACK */
695 if (seg_num > mad_send_wr->last_ack) {
696 mad_send_wr->last_ack = seg_num;
697 mad_send_wr->retries = mad_send_wr->send_wr.wr.ud.retries;
699 mad_send_wr->newwin = newwin;
700 if (mad_send_wr->last_ack == mad_send_wr->total_seg) {
701 /* If no response is expected, the ACK completes the send */
702 if (!mad_send_wr->send_wr.wr.ud.timeout_ms) {
703 struct ib_mad_send_wc wc;
705 ib_mark_mad_done(mad_send_wr);
706 spin_unlock_irqrestore(&agent->lock, flags);
708 wc.status = IB_WC_SUCCESS;
710 wc.wr_id = mad_send_wr->wr_id;
711 ib_mad_complete_send_wr(mad_send_wr, &wc);
714 if (mad_send_wr->refcount == 1)
715 ib_reset_mad_timeout(mad_send_wr, mad_send_wr->
716 send_wr.wr.ud.timeout_ms);
717 } else if (mad_send_wr->refcount == 1 &&
718 mad_send_wr->seg_num < mad_send_wr->newwin &&
719 mad_send_wr->seg_num <= mad_send_wr->total_seg) {
720 /* Send failure will just result in a timeout/retry */
721 ret = send_next_seg(mad_send_wr);
725 mad_send_wr->refcount++;
726 list_del(&mad_send_wr->agent_list);
727 list_add_tail(&mad_send_wr->agent_list,
728 &mad_send_wr->mad_agent_priv->send_list);
731 spin_unlock_irqrestore(&agent->lock, flags);
734 static struct ib_mad_recv_wc *
735 process_rmpp_data(struct ib_mad_agent_private *agent,
736 struct ib_mad_recv_wc *mad_recv_wc)
738 struct ib_rmpp_hdr *rmpp_hdr;
741 rmpp_hdr = &((struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad)->rmpp_hdr;
743 if (rmpp_hdr->rmpp_status) {
744 rmpp_status = IB_MGMT_RMPP_STATUS_BAD_STATUS;
748 if (rmpp_hdr->seg_num == __constant_htonl(1)) {
749 if (!(ib_get_rmpp_flags(rmpp_hdr) & IB_MGMT_RMPP_FLAG_FIRST)) {
750 rmpp_status = IB_MGMT_RMPP_STATUS_BAD_SEG;
753 return start_rmpp(agent, mad_recv_wc);
755 if (ib_get_rmpp_flags(rmpp_hdr) & IB_MGMT_RMPP_FLAG_FIRST) {
756 rmpp_status = IB_MGMT_RMPP_STATUS_BAD_SEG;
759 return continue_rmpp(agent, mad_recv_wc);
762 nack_recv(agent, mad_recv_wc, rmpp_status);
763 ib_free_recv_mad(mad_recv_wc);
767 static void process_rmpp_stop(struct ib_mad_agent_private *agent,
768 struct ib_mad_recv_wc *mad_recv_wc)
770 struct ib_rmpp_mad *rmpp_mad;
772 rmpp_mad = (struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad;
774 if (rmpp_mad->rmpp_hdr.rmpp_status != IB_MGMT_RMPP_STATUS_RESX) {
775 abort_send(agent, rmpp_mad->mad_hdr.tid,
776 IB_MGMT_RMPP_STATUS_BAD_STATUS);
777 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BAD_STATUS);
779 abort_send(agent, rmpp_mad->mad_hdr.tid,
780 rmpp_mad->rmpp_hdr.rmpp_status);
783 static void process_rmpp_abort(struct ib_mad_agent_private *agent,
784 struct ib_mad_recv_wc *mad_recv_wc)
786 struct ib_rmpp_mad *rmpp_mad;
788 rmpp_mad = (struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad;
790 if (rmpp_mad->rmpp_hdr.rmpp_status < IB_MGMT_RMPP_STATUS_ABORT_MIN ||
791 rmpp_mad->rmpp_hdr.rmpp_status > IB_MGMT_RMPP_STATUS_ABORT_MAX) {
792 abort_send(agent, rmpp_mad->mad_hdr.tid,
793 IB_MGMT_RMPP_STATUS_BAD_STATUS);
794 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BAD_STATUS);
796 abort_send(agent, rmpp_mad->mad_hdr.tid,
797 rmpp_mad->rmpp_hdr.rmpp_status);
800 struct ib_mad_recv_wc *
801 ib_process_rmpp_recv_wc(struct ib_mad_agent_private *agent,
802 struct ib_mad_recv_wc *mad_recv_wc)
804 struct ib_rmpp_mad *rmpp_mad;
806 rmpp_mad = (struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad;
807 if (!(rmpp_mad->rmpp_hdr.rmpp_rtime_flags & IB_MGMT_RMPP_FLAG_ACTIVE))
810 if (rmpp_mad->rmpp_hdr.rmpp_version != IB_MGMT_RMPP_VERSION) {
811 abort_send(agent, rmpp_mad->mad_hdr.tid,
812 IB_MGMT_RMPP_STATUS_UNV);
813 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_UNV);
817 switch (rmpp_mad->rmpp_hdr.rmpp_type) {
818 case IB_MGMT_RMPP_TYPE_DATA:
819 return process_rmpp_data(agent, mad_recv_wc);
820 case IB_MGMT_RMPP_TYPE_ACK:
821 process_rmpp_ack(agent, mad_recv_wc);
823 case IB_MGMT_RMPP_TYPE_STOP:
824 process_rmpp_stop(agent, mad_recv_wc);
826 case IB_MGMT_RMPP_TYPE_ABORT:
827 process_rmpp_abort(agent, mad_recv_wc);
830 abort_send(agent, rmpp_mad->mad_hdr.tid,
831 IB_MGMT_RMPP_STATUS_BADT);
832 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BADT);
836 ib_free_recv_mad(mad_recv_wc);
840 int ib_send_rmpp_mad(struct ib_mad_send_wr_private *mad_send_wr)
842 struct ib_rmpp_mad *rmpp_mad;
843 int i, total_len, ret;
845 rmpp_mad = (struct ib_rmpp_mad *)mad_send_wr->send_wr.wr.ud.mad_hdr;
846 if (!(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
847 IB_MGMT_RMPP_FLAG_ACTIVE))
848 return IB_RMPP_RESULT_UNHANDLED;
850 if (rmpp_mad->rmpp_hdr.rmpp_type != IB_MGMT_RMPP_TYPE_DATA)
851 return IB_RMPP_RESULT_INTERNAL;
853 if (mad_send_wr->send_wr.num_sge > 1)
854 return -EINVAL; /* TODO: support num_sge > 1 */
856 mad_send_wr->seg_num = 1;
857 mad_send_wr->newwin = 1;
858 mad_send_wr->data_offset = data_offset(rmpp_mad->mad_hdr.mgmt_class);
861 for (i = 0; i < mad_send_wr->send_wr.num_sge; i++)
862 total_len += mad_send_wr->send_wr.sg_list[i].length;
864 mad_send_wr->total_seg = (total_len - mad_send_wr->data_offset) /
865 (sizeof(struct ib_rmpp_mad) - mad_send_wr->data_offset);
866 mad_send_wr->pad = total_len - offsetof(struct ib_rmpp_mad, data) -
867 be32_to_cpu(rmpp_mad->rmpp_hdr.paylen_newwin);
869 /* We need to wait for the final ACK even if there isn't a response */
870 mad_send_wr->refcount += (mad_send_wr->timeout == 0);
871 ret = send_next_seg(mad_send_wr);
873 return IB_RMPP_RESULT_CONSUMED;
877 int ib_process_rmpp_send_wc(struct ib_mad_send_wr_private *mad_send_wr,
878 struct ib_mad_send_wc *mad_send_wc)
880 struct ib_rmpp_mad *rmpp_mad;
881 struct ib_mad_send_buf *msg;
884 rmpp_mad = (struct ib_rmpp_mad *)mad_send_wr->send_wr.wr.ud.mad_hdr;
885 if (!(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
886 IB_MGMT_RMPP_FLAG_ACTIVE))
887 return IB_RMPP_RESULT_UNHANDLED; /* RMPP not active */
889 if (rmpp_mad->rmpp_hdr.rmpp_type != IB_MGMT_RMPP_TYPE_DATA) {
890 msg = (struct ib_mad_send_buf *) (unsigned long)
892 if (rmpp_mad->rmpp_hdr.rmpp_type == IB_MGMT_RMPP_TYPE_ACK)
893 ib_free_send_mad(msg);
896 return IB_RMPP_RESULT_INTERNAL; /* ACK, STOP, or ABORT */
899 if (mad_send_wc->status != IB_WC_SUCCESS ||
900 mad_send_wr->status != IB_WC_SUCCESS)
901 return IB_RMPP_RESULT_PROCESSED; /* Canceled or send error */
903 if (!mad_send_wr->timeout)
904 return IB_RMPP_RESULT_PROCESSED; /* Response received */
906 if (mad_send_wr->last_ack == mad_send_wr->total_seg) {
907 mad_send_wr->timeout =
908 msecs_to_jiffies(mad_send_wr->send_wr.wr.ud.timeout_ms);
909 return IB_RMPP_RESULT_PROCESSED; /* Send done */
912 if (mad_send_wr->seg_num > mad_send_wr->newwin ||
913 mad_send_wr->seg_num > mad_send_wr->total_seg)
914 return IB_RMPP_RESULT_PROCESSED; /* Wait for ACK */
916 ret = send_next_seg(mad_send_wr);
918 mad_send_wc->status = IB_WC_GENERAL_ERR;
919 return IB_RMPP_RESULT_PROCESSED;
921 return IB_RMPP_RESULT_CONSUMED;
924 int ib_retry_rmpp(struct ib_mad_send_wr_private *mad_send_wr)
926 struct ib_rmpp_mad *rmpp_mad;
929 rmpp_mad = (struct ib_rmpp_mad *)mad_send_wr->send_wr.wr.ud.mad_hdr;
930 if (!(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
931 IB_MGMT_RMPP_FLAG_ACTIVE))
932 return IB_RMPP_RESULT_UNHANDLED; /* RMPP not active */
934 if (mad_send_wr->last_ack == mad_send_wr->total_seg)
935 return IB_RMPP_RESULT_PROCESSED;
937 mad_send_wr->seg_num = mad_send_wr->last_ack + 1;
938 ret = send_next_seg(mad_send_wr);
940 return IB_RMPP_RESULT_PROCESSED;
942 return IB_RMPP_RESULT_CONSUMED;