IB/mad: include GID/class when matching receives
[linux-block.git] / drivers / infiniband / core / mad_rmpp.c
CommitLineData
fa619a77
HR
1/*
2 * Copyright (c) 2005 Intel Inc. All rights reserved.
3 * Copyright (c) 2005 Voltaire, Inc. All rights reserved.
4 *
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:
10 *
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
14 *
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
18 *
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.
23 *
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
31 * SOFTWARE.
32 *
33 * $Id: mad_rmpp.c 1921 2005-03-02 22:58:44Z sean.hefty $
34 */
35
36#include <linux/dma-mapping.h>
37
38#include "mad_priv.h"
39#include "mad_rmpp.h"
40
41enum rmpp_state {
42 RMPP_STATE_ACTIVE,
43 RMPP_STATE_TIMEOUT,
44 RMPP_STATE_COMPLETE
45};
46
47struct 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;
54 spinlock_t lock;
55 atomic_t refcount;
56
57 struct ib_ah *ah;
58 struct ib_mad_recv_wc *rmpp_wc;
59 struct ib_mad_recv_buf *cur_seg_buf;
60 int last_ack;
61 int seg_num;
62 int newwin;
63
97f52eb4 64 __be64 tid;
fa619a77
HR
65 u32 src_qp;
66 u16 slid;
67 u8 mgmt_class;
68 u8 class_version;
69 u8 method;
70};
71
72static void destroy_rmpp_recv(struct mad_rmpp_recv *rmpp_recv)
73{
74 atomic_dec(&rmpp_recv->refcount);
75 wait_event(rmpp_recv->wait, !atomic_read(&rmpp_recv->refcount));
76 ib_destroy_ah(rmpp_recv->ah);
77 kfree(rmpp_recv);
78}
79
80void ib_cancel_rmpp_recvs(struct ib_mad_agent_private *agent)
81{
82 struct mad_rmpp_recv *rmpp_recv, *temp_rmpp_recv;
83 unsigned long flags;
84
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);
89 }
90 spin_unlock_irqrestore(&agent->lock, flags);
91
92 flush_workqueue(agent->qp_info->port_priv->wq);
93
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);
100 }
101}
102
fe9e08e1
SH
103static int data_offset(u8 mgmt_class)
104{
105 if (mgmt_class == IB_MGMT_CLASS_SUBN_ADM)
34816ad9 106 return IB_MGMT_SA_HDR;
fe9e08e1
SH
107 else if ((mgmt_class >= IB_MGMT_CLASS_VENDOR_RANGE2_START) &&
108 (mgmt_class <= IB_MGMT_CLASS_VENDOR_RANGE2_END))
34816ad9 109 return IB_MGMT_VENDOR_HDR;
fe9e08e1 110 else
34816ad9 111 return IB_MGMT_RMPP_HDR;
fe9e08e1
SH
112}
113
f36e1793 114static void format_ack(struct ib_mad_send_buf *msg,
fe9e08e1
SH
115 struct ib_rmpp_mad *data,
116 struct mad_rmpp_recv *rmpp_recv)
117{
f36e1793 118 struct ib_rmpp_mad *ack = msg->mad;
fe9e08e1
SH
119 unsigned long flags;
120
f36e1793 121 memcpy(ack, &data->mad_hdr, msg->hdr_len);
fe9e08e1
SH
122
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);
126
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);
132}
133
134static void ack_recv(struct mad_rmpp_recv *rmpp_recv,
135 struct ib_mad_recv_wc *recv_wc)
136{
137 struct ib_mad_send_buf *msg;
f36e1793 138 int ret, hdr_len;
fe9e08e1 139
f36e1793 140 hdr_len = data_offset(recv_wc->recv_buf.mad->mad_hdr.mgmt_class);
fe9e08e1 141 msg = ib_create_send_mad(&rmpp_recv->agent->agent, recv_wc->wc->src_qp,
f36e1793
JM
142 recv_wc->wc->pkey_index, 1, hdr_len,
143 0, GFP_KERNEL);
fe9e08e1
SH
144 if (!msg)
145 return;
146
f36e1793 147 format_ack(msg, (struct ib_rmpp_mad *) recv_wc->recv_buf.mad, rmpp_recv);
34816ad9
SH
148 msg->ah = rmpp_recv->ah;
149 ret = ib_post_send_mad(msg, NULL);
fe9e08e1
SH
150 if (ret)
151 ib_free_send_mad(msg);
152}
153
7cc656ef
RD
154static struct ib_mad_send_buf *alloc_response_msg(struct ib_mad_agent *agent,
155 struct ib_mad_recv_wc *recv_wc)
fe9e08e1 156{
7cc656ef 157 struct ib_mad_send_buf *msg;
fe9e08e1 158 struct ib_ah *ah;
f36e1793 159 int hdr_len;
fe9e08e1
SH
160
161 ah = ib_create_ah_from_wc(agent->qp->pd, recv_wc->wc,
162 recv_wc->recv_buf.grh, agent->port_num);
163 if (IS_ERR(ah))
7cc656ef 164 return (void *) ah;
fe9e08e1 165
f36e1793 166 hdr_len = data_offset(recv_wc->recv_buf.mad->mad_hdr.mgmt_class);
7cc656ef
RD
167 msg = ib_create_send_mad(agent, recv_wc->wc->src_qp,
168 recv_wc->wc->pkey_index, 1,
f36e1793 169 hdr_len, 0, GFP_KERNEL);
7cc656ef 170 if (IS_ERR(msg))
fe9e08e1 171 ib_destroy_ah(ah);
7cc656ef
RD
172 else
173 msg->ah = ah;
174
175 return msg;
fe9e08e1
SH
176}
177
34816ad9 178void ib_rmpp_send_handler(struct ib_mad_send_wc *mad_send_wc)
fe9e08e1 179{
34816ad9
SH
180 struct ib_rmpp_mad *rmpp_mad = mad_send_wc->send_buf->mad;
181
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);
fe9e08e1
SH
185}
186
187static void nack_recv(struct ib_mad_agent_private *agent,
188 struct ib_mad_recv_wc *recv_wc, u8 rmpp_status)
189{
190 struct ib_mad_send_buf *msg;
191 struct ib_rmpp_mad *rmpp_mad;
fe9e08e1
SH
192 int ret;
193
7cc656ef
RD
194 msg = alloc_response_msg(&agent->agent, recv_wc);
195 if (IS_ERR(msg))
fe9e08e1
SH
196 return;
197
34816ad9 198 rmpp_mad = msg->mad;
f36e1793 199 memcpy(rmpp_mad, recv_wc->recv_buf.mad, msg->hdr_len);
fe9e08e1
SH
200
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;
208
34816ad9
SH
209 ret = ib_post_send_mad(msg, NULL);
210 if (ret) {
211 ib_destroy_ah(msg->ah);
212 ib_free_send_mad(msg);
213 }
fe9e08e1
SH
214}
215
fa619a77
HR
216static void recv_timeout_handler(void *data)
217{
218 struct mad_rmpp_recv *rmpp_recv = data;
219 struct ib_mad_recv_wc *rmpp_wc;
220 unsigned long flags;
221
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);
225 return;
226 }
227 rmpp_recv->state = RMPP_STATE_TIMEOUT;
228 list_del(&rmpp_recv->list);
229 spin_unlock_irqrestore(&rmpp_recv->agent->lock, flags);
230
fa619a77 231 rmpp_wc = rmpp_recv->rmpp_wc;
fe9e08e1 232 nack_recv(rmpp_recv->agent, rmpp_wc, IB_MGMT_RMPP_STATUS_T2L);
fa619a77
HR
233 destroy_rmpp_recv(rmpp_recv);
234 ib_free_recv_mad(rmpp_wc);
235}
236
237static void recv_cleanup_handler(void *data)
238{
239 struct mad_rmpp_recv *rmpp_recv = data;
240 unsigned long flags;
241
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);
246}
247
248static struct mad_rmpp_recv *
249create_rmpp_recv(struct ib_mad_agent_private *agent,
250 struct ib_mad_recv_wc *mad_recv_wc)
251{
252 struct mad_rmpp_recv *rmpp_recv;
253 struct ib_mad_hdr *mad_hdr;
254
255 rmpp_recv = kmalloc(sizeof *rmpp_recv, GFP_KERNEL);
256 if (!rmpp_recv)
257 return NULL;
258
259 rmpp_recv->ah = ib_create_ah_from_wc(agent->agent.qp->pd,
260 mad_recv_wc->wc,
261 mad_recv_wc->recv_buf.grh,
262 agent->agent.port_num);
263 if (IS_ERR(rmpp_recv->ah))
264 goto error;
265
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);
273
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;
279
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;
287 return rmpp_recv;
288
289error: kfree(rmpp_recv);
290 return NULL;
291}
292
293static inline void deref_rmpp_recv(struct mad_rmpp_recv *rmpp_recv)
294{
295 if (atomic_dec_and_test(&rmpp_recv->refcount))
296 wake_up(&rmpp_recv->wait);
297}
298
299static struct mad_rmpp_recv *
300find_rmpp_recv(struct ib_mad_agent_private *agent,
301 struct ib_mad_recv_wc *mad_recv_wc)
302{
303 struct mad_rmpp_recv *rmpp_recv;
304 struct ib_mad_hdr *mad_hdr = &mad_recv_wc->recv_buf.mad->mad_hdr;
305
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)
313 return rmpp_recv;
314 }
315 return NULL;
316}
317
318static struct mad_rmpp_recv *
319acquire_rmpp_recv(struct ib_mad_agent_private *agent,
320 struct ib_mad_recv_wc *mad_recv_wc)
321{
322 struct mad_rmpp_recv *rmpp_recv;
323 unsigned long flags;
324
325 spin_lock_irqsave(&agent->lock, flags);
326 rmpp_recv = find_rmpp_recv(agent, mad_recv_wc);
327 if (rmpp_recv)
328 atomic_inc(&rmpp_recv->refcount);
329 spin_unlock_irqrestore(&agent->lock, flags);
330 return rmpp_recv;
331}
332
333static struct mad_rmpp_recv *
334insert_rmpp_recv(struct ib_mad_agent_private *agent,
335 struct mad_rmpp_recv *rmpp_recv)
336{
337 struct mad_rmpp_recv *cur_rmpp_recv;
338
339 cur_rmpp_recv = find_rmpp_recv(agent, rmpp_recv->rmpp_wc);
340 if (!cur_rmpp_recv)
341 list_add_tail(&rmpp_recv->list, &agent->rmpp_list);
342
343 return cur_rmpp_recv;
344}
345
fa619a77
HR
346static inline int get_last_flag(struct ib_mad_recv_buf *seg)
347{
348 struct ib_rmpp_mad *rmpp_mad;
349
350 rmpp_mad = (struct ib_rmpp_mad *) seg->mad;
351 return ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) & IB_MGMT_RMPP_FLAG_LAST;
352}
353
354static inline int get_seg_num(struct ib_mad_recv_buf *seg)
355{
356 struct ib_rmpp_mad *rmpp_mad;
357
358 rmpp_mad = (struct ib_rmpp_mad *) seg->mad;
359 return be32_to_cpu(rmpp_mad->rmpp_hdr.seg_num);
360}
361
362static inline struct ib_mad_recv_buf * get_next_seg(struct list_head *rmpp_list,
363 struct ib_mad_recv_buf *seg)
364{
365 if (seg->list.next == rmpp_list)
366 return NULL;
367
368 return container_of(seg->list.next, struct ib_mad_recv_buf, list);
369}
370
371static inline int window_size(struct ib_mad_agent_private *agent)
372{
373 return max(agent->qp_info->recv_queue.max_active >> 3, 1);
374}
375
376static struct ib_mad_recv_buf * find_seg_location(struct list_head *rmpp_list,
377 int seg_num)
378{
379 struct ib_mad_recv_buf *seg_buf;
380 int cur_seg_num;
381
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)
385 return seg_buf;
386 if (seg_num == cur_seg_num)
387 break;
388 }
389 return NULL;
390}
391
392static void update_seg_num(struct mad_rmpp_recv *rmpp_recv,
393 struct ib_mad_recv_buf *new_buf)
394{
395 struct list_head *rmpp_list = &rmpp_recv->rmpp_wc->rmpp_list;
396
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);
401 }
402}
403
404static inline int get_mad_len(struct mad_rmpp_recv *rmpp_recv)
405{
406 struct ib_rmpp_mad *rmpp_mad;
407 int hdr_size, data_size, pad;
408
409 rmpp_mad = (struct ib_rmpp_mad *)rmpp_recv->cur_seg_buf->mad;
410
411 hdr_size = data_offset(rmpp_mad->mad_hdr.mgmt_class);
412 data_size = sizeof(struct ib_rmpp_mad) - hdr_size;
f2065e42
HR
413 pad = IB_MGMT_RMPP_DATA - be32_to_cpu(rmpp_mad->rmpp_hdr.paylen_newwin);
414 if (pad > IB_MGMT_RMPP_DATA || pad < 0)
fa619a77
HR
415 pad = 0;
416
417 return hdr_size + rmpp_recv->seg_num * data_size - pad;
418}
419
420static struct ib_mad_recv_wc * complete_rmpp(struct mad_rmpp_recv *rmpp_recv)
421{
422 struct ib_mad_recv_wc *rmpp_wc;
423
424 ack_recv(rmpp_recv, rmpp_recv->rmpp_wc);
425 if (rmpp_recv->seg_num > 1)
426 cancel_delayed_work(&rmpp_recv->timeout_work);
427
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));
433 return rmpp_wc;
434}
435
fa619a77
HR
436static struct ib_mad_recv_wc *
437continue_rmpp(struct ib_mad_agent_private *agent,
438 struct ib_mad_recv_wc *mad_recv_wc)
439{
440 struct mad_rmpp_recv *rmpp_recv;
441 struct ib_mad_recv_buf *prev_buf;
442 struct ib_mad_recv_wc *done_wc;
443 int seg_num;
444 unsigned long flags;
445
446 rmpp_recv = acquire_rmpp_recv(agent, mad_recv_wc);
447 if (!rmpp_recv)
448 goto drop1;
449
450 seg_num = get_seg_num(&mad_recv_wc->recv_buf);
451
452 spin_lock_irqsave(&rmpp_recv->lock, flags);
453 if ((rmpp_recv->state == RMPP_STATE_TIMEOUT) ||
454 (seg_num > rmpp_recv->newwin))
455 goto drop3;
456
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);
461 goto drop2;
462 }
463
464 prev_buf = find_seg_location(&rmpp_recv->rmpp_wc->rmpp_list, seg_num);
465 if (!prev_buf)
466 goto drop3;
467
468 done_wc = NULL;
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);
476 goto out;
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);
481 goto out;
482 }
483 }
484 spin_unlock_irqrestore(&rmpp_recv->lock, flags);
485out:
486 deref_rmpp_recv(rmpp_recv);
487 return done_wc;
488
489drop3: spin_unlock_irqrestore(&rmpp_recv->lock, flags);
490drop2: deref_rmpp_recv(rmpp_recv);
491drop1: ib_free_recv_mad(mad_recv_wc);
492 return NULL;
493}
494
495static struct ib_mad_recv_wc *
496start_rmpp(struct ib_mad_agent_private *agent,
497 struct ib_mad_recv_wc *mad_recv_wc)
498{
499 struct mad_rmpp_recv *rmpp_recv;
500 unsigned long flags;
501
502 rmpp_recv = create_rmpp_recv(agent, mad_recv_wc);
503 if (!rmpp_recv) {
504 ib_free_recv_mad(mad_recv_wc);
505 return NULL;
506 }
507
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);
514 }
515 atomic_inc(&rmpp_recv->refcount);
516
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);
521 } else {
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);
529 mad_recv_wc = NULL;
530 }
531 deref_rmpp_recv(rmpp_recv);
532 return mad_recv_wc;
533}
534
fa619a77
HR
535static int send_next_seg(struct ib_mad_send_wr_private *mad_send_wr)
536{
537 struct ib_rmpp_mad *rmpp_mad;
538 int timeout;
f36e1793 539 u32 paylen = 0;
fa619a77 540
34816ad9 541 rmpp_mad = mad_send_wr->send_buf.mad;
fa619a77 542 ib_set_rmpp_flags(&rmpp_mad->rmpp_hdr, IB_MGMT_RMPP_FLAG_ACTIVE);
f36e1793 543 rmpp_mad->rmpp_hdr.seg_num = cpu_to_be32(++mad_send_wr->seg_num);
fa619a77
HR
544
545 if (mad_send_wr->seg_num == 1) {
546 rmpp_mad->rmpp_hdr.rmpp_rtime_flags |= IB_MGMT_RMPP_FLAG_FIRST;
f36e1793 547 paylen = mad_send_wr->send_buf.seg_count * IB_MGMT_RMPP_DATA -
972d512a 548 mad_send_wr->pad;
fa619a77
HR
549 }
550
f36e1793 551 if (mad_send_wr->seg_num == mad_send_wr->send_buf.seg_count) {
fa619a77 552 rmpp_mad->rmpp_hdr.rmpp_rtime_flags |= IB_MGMT_RMPP_FLAG_LAST;
972d512a 553 paylen = IB_MGMT_RMPP_DATA - mad_send_wr->pad;
fa619a77 554 }
f36e1793 555 rmpp_mad->rmpp_hdr.paylen_newwin = cpu_to_be32(paylen);
fa619a77
HR
556
557 /* 2 seconds for an ACK until we can find the packet lifetime */
34816ad9 558 timeout = mad_send_wr->send_buf.timeout_ms;
fa619a77
HR
559 if (!timeout || timeout > 2000)
560 mad_send_wr->timeout = msecs_to_jiffies(2000);
f36e1793 561
fa619a77
HR
562 return ib_send_mad(mad_send_wr);
563}
564
fa9656bb
JM
565static void abort_send(struct ib_mad_agent_private *agent,
566 struct ib_mad_recv_wc *mad_recv_wc, u8 rmpp_status)
fe9e08e1
SH
567{
568 struct ib_mad_send_wr_private *mad_send_wr;
569 struct ib_mad_send_wc wc;
570 unsigned long flags;
571
572 spin_lock_irqsave(&agent->lock, flags);
fa9656bb 573 mad_send_wr = ib_find_send_mad(agent, mad_recv_wc);
fe9e08e1
SH
574 if (!mad_send_wr)
575 goto out; /* Unmatched send */
576
f36e1793 577 if ((mad_send_wr->last_ack == mad_send_wr->send_buf.seg_count) ||
fe9e08e1
SH
578 (!mad_send_wr->timeout) || (mad_send_wr->status != IB_WC_SUCCESS))
579 goto out; /* Send is already done */
580
581 ib_mark_mad_done(mad_send_wr);
582 spin_unlock_irqrestore(&agent->lock, flags);
583
584 wc.status = IB_WC_REM_ABORT_ERR;
585 wc.vendor_err = rmpp_status;
34816ad9 586 wc.send_buf = &mad_send_wr->send_buf;
fe9e08e1
SH
587 ib_mad_complete_send_wr(mad_send_wr, &wc);
588 return;
589out:
590 spin_unlock_irqrestore(&agent->lock, flags);
591}
592
f36e1793
JM
593static inline void adjust_last_ack(struct ib_mad_send_wr_private *wr,
594 int seg_num)
595{
596 struct list_head *list;
597
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)
602 break;
603}
604
fa619a77
HR
605static void process_rmpp_ack(struct ib_mad_agent_private *agent,
606 struct ib_mad_recv_wc *mad_recv_wc)
607{
608 struct ib_mad_send_wr_private *mad_send_wr;
609 struct ib_rmpp_mad *rmpp_mad;
610 unsigned long flags;
611 int seg_num, newwin, ret;
612
613 rmpp_mad = (struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad;
fe9e08e1 614 if (rmpp_mad->rmpp_hdr.rmpp_status) {
fa9656bb 615 abort_send(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BAD_STATUS);
fe9e08e1 616 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BAD_STATUS);
fa619a77 617 return;
fe9e08e1 618 }
fa619a77
HR
619
620 seg_num = be32_to_cpu(rmpp_mad->rmpp_hdr.seg_num);
621 newwin = be32_to_cpu(rmpp_mad->rmpp_hdr.paylen_newwin);
fe9e08e1 622 if (newwin < seg_num) {
fa9656bb 623 abort_send(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_W2S);
fe9e08e1
SH
624 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_W2S);
625 return;
626 }
fa619a77
HR
627
628 spin_lock_irqsave(&agent->lock, flags);
fa9656bb 629 mad_send_wr = ib_find_send_mad(agent, mad_recv_wc);
fa619a77
HR
630 if (!mad_send_wr)
631 goto out; /* Unmatched ACK */
632
f36e1793 633 if ((mad_send_wr->last_ack == mad_send_wr->send_buf.seg_count) ||
fa619a77
HR
634 (!mad_send_wr->timeout) || (mad_send_wr->status != IB_WC_SUCCESS))
635 goto out; /* Send is already done */
636
f36e1793
JM
637 if (seg_num > mad_send_wr->send_buf.seg_count ||
638 seg_num > mad_send_wr->newwin) {
fe9e08e1 639 spin_unlock_irqrestore(&agent->lock, flags);
fa9656bb 640 abort_send(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_S2B);
fe9e08e1
SH
641 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_S2B);
642 return;
643 }
fa619a77
HR
644
645 if (newwin < mad_send_wr->newwin || seg_num < mad_send_wr->last_ack)
646 goto out; /* Old ACK */
647
648 if (seg_num > mad_send_wr->last_ack) {
f36e1793 649 adjust_last_ack(mad_send_wr, seg_num);
34816ad9 650 mad_send_wr->retries = mad_send_wr->send_buf.retries;
fa619a77
HR
651 }
652 mad_send_wr->newwin = newwin;
f36e1793 653 if (mad_send_wr->last_ack == mad_send_wr->send_buf.seg_count) {
fa619a77 654 /* If no response is expected, the ACK completes the send */
34816ad9 655 if (!mad_send_wr->send_buf.timeout_ms) {
fa619a77
HR
656 struct ib_mad_send_wc wc;
657
658 ib_mark_mad_done(mad_send_wr);
659 spin_unlock_irqrestore(&agent->lock, flags);
660
661 wc.status = IB_WC_SUCCESS;
662 wc.vendor_err = 0;
34816ad9 663 wc.send_buf = &mad_send_wr->send_buf;
fa619a77
HR
664 ib_mad_complete_send_wr(mad_send_wr, &wc);
665 return;
666 }
667 if (mad_send_wr->refcount == 1)
34816ad9
SH
668 ib_reset_mad_timeout(mad_send_wr,
669 mad_send_wr->send_buf.timeout_ms);
fa619a77
HR
670 } else if (mad_send_wr->refcount == 1 &&
671 mad_send_wr->seg_num < mad_send_wr->newwin &&
f36e1793 672 mad_send_wr->seg_num < mad_send_wr->send_buf.seg_count) {
fa619a77
HR
673 /* Send failure will just result in a timeout/retry */
674 ret = send_next_seg(mad_send_wr);
675 if (ret)
676 goto out;
677
678 mad_send_wr->refcount++;
679 list_del(&mad_send_wr->agent_list);
680 list_add_tail(&mad_send_wr->agent_list,
681 &mad_send_wr->mad_agent_priv->send_list);
682 }
683out:
684 spin_unlock_irqrestore(&agent->lock, flags);
685}
686
fe9e08e1
SH
687static struct ib_mad_recv_wc *
688process_rmpp_data(struct ib_mad_agent_private *agent,
689 struct ib_mad_recv_wc *mad_recv_wc)
690{
691 struct ib_rmpp_hdr *rmpp_hdr;
692 u8 rmpp_status;
693
694 rmpp_hdr = &((struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad)->rmpp_hdr;
695
696 if (rmpp_hdr->rmpp_status) {
697 rmpp_status = IB_MGMT_RMPP_STATUS_BAD_STATUS;
698 goto bad;
699 }
700
701 if (rmpp_hdr->seg_num == __constant_htonl(1)) {
702 if (!(ib_get_rmpp_flags(rmpp_hdr) & IB_MGMT_RMPP_FLAG_FIRST)) {
703 rmpp_status = IB_MGMT_RMPP_STATUS_BAD_SEG;
704 goto bad;
705 }
706 return start_rmpp(agent, mad_recv_wc);
707 } else {
708 if (ib_get_rmpp_flags(rmpp_hdr) & IB_MGMT_RMPP_FLAG_FIRST) {
709 rmpp_status = IB_MGMT_RMPP_STATUS_BAD_SEG;
710 goto bad;
711 }
712 return continue_rmpp(agent, mad_recv_wc);
713 }
714bad:
715 nack_recv(agent, mad_recv_wc, rmpp_status);
716 ib_free_recv_mad(mad_recv_wc);
717 return NULL;
718}
719
720static void process_rmpp_stop(struct ib_mad_agent_private *agent,
721 struct ib_mad_recv_wc *mad_recv_wc)
722{
723 struct ib_rmpp_mad *rmpp_mad;
724
725 rmpp_mad = (struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad;
726
727 if (rmpp_mad->rmpp_hdr.rmpp_status != IB_MGMT_RMPP_STATUS_RESX) {
fa9656bb 728 abort_send(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BAD_STATUS);
fe9e08e1
SH
729 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BAD_STATUS);
730 } else
fa9656bb 731 abort_send(agent, mad_recv_wc, rmpp_mad->rmpp_hdr.rmpp_status);
fe9e08e1
SH
732}
733
734static void process_rmpp_abort(struct ib_mad_agent_private *agent,
735 struct ib_mad_recv_wc *mad_recv_wc)
736{
737 struct ib_rmpp_mad *rmpp_mad;
738
739 rmpp_mad = (struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad;
740
741 if (rmpp_mad->rmpp_hdr.rmpp_status < IB_MGMT_RMPP_STATUS_ABORT_MIN ||
742 rmpp_mad->rmpp_hdr.rmpp_status > IB_MGMT_RMPP_STATUS_ABORT_MAX) {
fa9656bb 743 abort_send(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BAD_STATUS);
fe9e08e1
SH
744 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BAD_STATUS);
745 } else
fa9656bb 746 abort_send(agent, mad_recv_wc, rmpp_mad->rmpp_hdr.rmpp_status);
fe9e08e1
SH
747}
748
fa619a77
HR
749struct ib_mad_recv_wc *
750ib_process_rmpp_recv_wc(struct ib_mad_agent_private *agent,
751 struct ib_mad_recv_wc *mad_recv_wc)
752{
753 struct ib_rmpp_mad *rmpp_mad;
754
755 rmpp_mad = (struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad;
756 if (!(rmpp_mad->rmpp_hdr.rmpp_rtime_flags & IB_MGMT_RMPP_FLAG_ACTIVE))
757 return mad_recv_wc;
758
fe9e08e1 759 if (rmpp_mad->rmpp_hdr.rmpp_version != IB_MGMT_RMPP_VERSION) {
fa9656bb 760 abort_send(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_UNV);
fe9e08e1 761 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_UNV);
fa619a77 762 goto out;
fe9e08e1 763 }
fa619a77
HR
764
765 switch (rmpp_mad->rmpp_hdr.rmpp_type) {
766 case IB_MGMT_RMPP_TYPE_DATA:
fe9e08e1 767 return process_rmpp_data(agent, mad_recv_wc);
fa619a77
HR
768 case IB_MGMT_RMPP_TYPE_ACK:
769 process_rmpp_ack(agent, mad_recv_wc);
770 break;
771 case IB_MGMT_RMPP_TYPE_STOP:
fe9e08e1
SH
772 process_rmpp_stop(agent, mad_recv_wc);
773 break;
fa619a77 774 case IB_MGMT_RMPP_TYPE_ABORT:
fe9e08e1 775 process_rmpp_abort(agent, mad_recv_wc);
fa619a77
HR
776 break;
777 default:
fa9656bb 778 abort_send(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BADT);
fe9e08e1 779 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BADT);
fa619a77
HR
780 break;
781 }
782out:
783 ib_free_recv_mad(mad_recv_wc);
784 return NULL;
785}
786
787int ib_send_rmpp_mad(struct ib_mad_send_wr_private *mad_send_wr)
788{
789 struct ib_rmpp_mad *rmpp_mad;
f36e1793 790 int ret;
fa619a77 791
34816ad9 792 rmpp_mad = mad_send_wr->send_buf.mad;
fa619a77
HR
793 if (!(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
794 IB_MGMT_RMPP_FLAG_ACTIVE))
795 return IB_RMPP_RESULT_UNHANDLED;
796
f36e1793
JM
797 if (rmpp_mad->rmpp_hdr.rmpp_type != IB_MGMT_RMPP_TYPE_DATA) {
798 mad_send_wr->seg_num = 1;
fa619a77 799 return IB_RMPP_RESULT_INTERNAL;
f36e1793 800 }
fa619a77 801
fa619a77 802 mad_send_wr->newwin = 1;
fa619a77
HR
803
804 /* We need to wait for the final ACK even if there isn't a response */
805 mad_send_wr->refcount += (mad_send_wr->timeout == 0);
806 ret = send_next_seg(mad_send_wr);
807 if (!ret)
808 return IB_RMPP_RESULT_CONSUMED;
809 return ret;
810}
811
812int ib_process_rmpp_send_wc(struct ib_mad_send_wr_private *mad_send_wr,
813 struct ib_mad_send_wc *mad_send_wc)
814{
815 struct ib_rmpp_mad *rmpp_mad;
fa619a77
HR
816 int ret;
817
34816ad9 818 rmpp_mad = mad_send_wr->send_buf.mad;
fa619a77
HR
819 if (!(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
820 IB_MGMT_RMPP_FLAG_ACTIVE))
821 return IB_RMPP_RESULT_UNHANDLED; /* RMPP not active */
822
34816ad9 823 if (rmpp_mad->rmpp_hdr.rmpp_type != IB_MGMT_RMPP_TYPE_DATA)
fa619a77 824 return IB_RMPP_RESULT_INTERNAL; /* ACK, STOP, or ABORT */
fa619a77
HR
825
826 if (mad_send_wc->status != IB_WC_SUCCESS ||
827 mad_send_wr->status != IB_WC_SUCCESS)
828 return IB_RMPP_RESULT_PROCESSED; /* Canceled or send error */
829
830 if (!mad_send_wr->timeout)
831 return IB_RMPP_RESULT_PROCESSED; /* Response received */
832
f36e1793 833 if (mad_send_wr->last_ack == mad_send_wr->send_buf.seg_count) {
fa619a77 834 mad_send_wr->timeout =
34816ad9 835 msecs_to_jiffies(mad_send_wr->send_buf.timeout_ms);
fa619a77
HR
836 return IB_RMPP_RESULT_PROCESSED; /* Send done */
837 }
838
f36e1793
JM
839 if (mad_send_wr->seg_num == mad_send_wr->newwin ||
840 mad_send_wr->seg_num == mad_send_wr->send_buf.seg_count)
fa619a77
HR
841 return IB_RMPP_RESULT_PROCESSED; /* Wait for ACK */
842
843 ret = send_next_seg(mad_send_wr);
844 if (ret) {
845 mad_send_wc->status = IB_WC_GENERAL_ERR;
846 return IB_RMPP_RESULT_PROCESSED;
847 }
848 return IB_RMPP_RESULT_CONSUMED;
849}
850
851int ib_retry_rmpp(struct ib_mad_send_wr_private *mad_send_wr)
852{
853 struct ib_rmpp_mad *rmpp_mad;
854 int ret;
855
34816ad9 856 rmpp_mad = mad_send_wr->send_buf.mad;
fa619a77
HR
857 if (!(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
858 IB_MGMT_RMPP_FLAG_ACTIVE))
859 return IB_RMPP_RESULT_UNHANDLED; /* RMPP not active */
860
f36e1793 861 if (mad_send_wr->last_ack == mad_send_wr->send_buf.seg_count)
fa619a77
HR
862 return IB_RMPP_RESULT_PROCESSED;
863
f36e1793
JM
864 mad_send_wr->seg_num = mad_send_wr->last_ack;
865 mad_send_wr->cur_seg = mad_send_wr->last_ack_seg;
866
fa619a77
HR
867 ret = send_next_seg(mad_send_wr);
868 if (ret)
869 return IB_RMPP_RESULT_PROCESSED;
870
871 return IB_RMPP_RESULT_CONSUMED;
872}