]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/infiniband/core/mad_rmpp.c
[PATCH] net/rxrpc: use list_move()
[net-next-2.6.git] / drivers / infiniband / core / mad_rmpp.c
CommitLineData
fa619a77
HR
1/*
2 * Copyright (c) 2005 Intel Inc. All rights reserved.
618a3c03 3 * Copyright (c) 2005-2006 Voltaire, Inc. All rights reserved.
fa619a77
HR
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;
1b52fa98 52 struct completion comp;
fa619a77
HR
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
1b52fa98
SH
72static inline void deref_rmpp_recv(struct mad_rmpp_recv *rmpp_recv)
73{
74 if (atomic_dec_and_test(&rmpp_recv->refcount))
75 complete(&rmpp_recv->comp);
76}
77
fa619a77
HR
78static void destroy_rmpp_recv(struct mad_rmpp_recv *rmpp_recv)
79{
1b52fa98
SH
80 deref_rmpp_recv(rmpp_recv);
81 wait_for_completion(&rmpp_recv->comp);
fa619a77
HR
82 ib_destroy_ah(rmpp_recv->ah);
83 kfree(rmpp_recv);
84}
85
86void ib_cancel_rmpp_recvs(struct ib_mad_agent_private *agent)
87{
88 struct mad_rmpp_recv *rmpp_recv, *temp_rmpp_recv;
89 unsigned long flags;
90
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);
95 }
96 spin_unlock_irqrestore(&agent->lock, flags);
97
98 flush_workqueue(agent->qp_info->port_priv->wq);
99
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);
106 }
107}
108
f36e1793 109static void format_ack(struct ib_mad_send_buf *msg,
fe9e08e1
SH
110 struct ib_rmpp_mad *data,
111 struct mad_rmpp_recv *rmpp_recv)
112{
f36e1793 113 struct ib_rmpp_mad *ack = msg->mad;
fe9e08e1
SH
114 unsigned long flags;
115
f36e1793 116 memcpy(ack, &data->mad_hdr, msg->hdr_len);
fe9e08e1
SH
117
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);
121
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);
127}
128
129static void ack_recv(struct mad_rmpp_recv *rmpp_recv,
130 struct ib_mad_recv_wc *recv_wc)
131{
132 struct ib_mad_send_buf *msg;
f36e1793 133 int ret, hdr_len;
fe9e08e1 134
618a3c03 135 hdr_len = ib_get_mad_data_offset(recv_wc->recv_buf.mad->mad_hdr.mgmt_class);
fe9e08e1 136 msg = ib_create_send_mad(&rmpp_recv->agent->agent, recv_wc->wc->src_qp,
f36e1793
JM
137 recv_wc->wc->pkey_index, 1, hdr_len,
138 0, GFP_KERNEL);
fe9e08e1
SH
139 if (!msg)
140 return;
141
f36e1793 142 format_ack(msg, (struct ib_rmpp_mad *) recv_wc->recv_buf.mad, rmpp_recv);
34816ad9
SH
143 msg->ah = rmpp_recv->ah;
144 ret = ib_post_send_mad(msg, NULL);
fe9e08e1
SH
145 if (ret)
146 ib_free_send_mad(msg);
147}
148
7cc656ef
RD
149static struct ib_mad_send_buf *alloc_response_msg(struct ib_mad_agent *agent,
150 struct ib_mad_recv_wc *recv_wc)
fe9e08e1 151{
7cc656ef 152 struct ib_mad_send_buf *msg;
fe9e08e1 153 struct ib_ah *ah;
f36e1793 154 int hdr_len;
fe9e08e1
SH
155
156 ah = ib_create_ah_from_wc(agent->qp->pd, recv_wc->wc,
157 recv_wc->recv_buf.grh, agent->port_num);
158 if (IS_ERR(ah))
7cc656ef 159 return (void *) ah;
fe9e08e1 160
618a3c03 161 hdr_len = ib_get_mad_data_offset(recv_wc->recv_buf.mad->mad_hdr.mgmt_class);
7cc656ef
RD
162 msg = ib_create_send_mad(agent, recv_wc->wc->src_qp,
163 recv_wc->wc->pkey_index, 1,
f36e1793 164 hdr_len, 0, GFP_KERNEL);
7cc656ef 165 if (IS_ERR(msg))
fe9e08e1 166 ib_destroy_ah(ah);
7cc656ef
RD
167 else
168 msg->ah = ah;
169
170 return msg;
fe9e08e1
SH
171}
172
34816ad9 173void ib_rmpp_send_handler(struct ib_mad_send_wc *mad_send_wc)
fe9e08e1 174{
34816ad9
SH
175 struct ib_rmpp_mad *rmpp_mad = mad_send_wc->send_buf->mad;
176
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);
fe9e08e1
SH
180}
181
182static void nack_recv(struct ib_mad_agent_private *agent,
183 struct ib_mad_recv_wc *recv_wc, u8 rmpp_status)
184{
185 struct ib_mad_send_buf *msg;
186 struct ib_rmpp_mad *rmpp_mad;
fe9e08e1
SH
187 int ret;
188
7cc656ef
RD
189 msg = alloc_response_msg(&agent->agent, recv_wc);
190 if (IS_ERR(msg))
fe9e08e1
SH
191 return;
192
34816ad9 193 rmpp_mad = msg->mad;
f36e1793 194 memcpy(rmpp_mad, recv_wc->recv_buf.mad, msg->hdr_len);
fe9e08e1
SH
195
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;
203
34816ad9
SH
204 ret = ib_post_send_mad(msg, NULL);
205 if (ret) {
206 ib_destroy_ah(msg->ah);
207 ib_free_send_mad(msg);
208 }
fe9e08e1
SH
209}
210
fa619a77
HR
211static void recv_timeout_handler(void *data)
212{
213 struct mad_rmpp_recv *rmpp_recv = data;
214 struct ib_mad_recv_wc *rmpp_wc;
215 unsigned long flags;
216
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);
220 return;
221 }
222 rmpp_recv->state = RMPP_STATE_TIMEOUT;
223 list_del(&rmpp_recv->list);
224 spin_unlock_irqrestore(&rmpp_recv->agent->lock, flags);
225
fa619a77 226 rmpp_wc = rmpp_recv->rmpp_wc;
fe9e08e1 227 nack_recv(rmpp_recv->agent, rmpp_wc, IB_MGMT_RMPP_STATUS_T2L);
fa619a77
HR
228 destroy_rmpp_recv(rmpp_recv);
229 ib_free_recv_mad(rmpp_wc);
230}
231
232static void recv_cleanup_handler(void *data)
233{
234 struct mad_rmpp_recv *rmpp_recv = data;
235 unsigned long flags;
236
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);
241}
242
243static struct mad_rmpp_recv *
244create_rmpp_recv(struct ib_mad_agent_private *agent,
245 struct ib_mad_recv_wc *mad_recv_wc)
246{
247 struct mad_rmpp_recv *rmpp_recv;
248 struct ib_mad_hdr *mad_hdr;
249
250 rmpp_recv = kmalloc(sizeof *rmpp_recv, GFP_KERNEL);
251 if (!rmpp_recv)
252 return NULL;
253
254 rmpp_recv->ah = ib_create_ah_from_wc(agent->agent.qp->pd,
255 mad_recv_wc->wc,
256 mad_recv_wc->recv_buf.grh,
257 agent->agent.port_num);
258 if (IS_ERR(rmpp_recv->ah))
259 goto error;
260
261 rmpp_recv->agent = agent;
1b52fa98 262 init_completion(&rmpp_recv->comp);
fa619a77
HR
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);
268
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;
274
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;
282 return rmpp_recv;
283
284error: kfree(rmpp_recv);
285 return NULL;
286}
287
fa619a77
HR
288static struct mad_rmpp_recv *
289find_rmpp_recv(struct ib_mad_agent_private *agent,
290 struct ib_mad_recv_wc *mad_recv_wc)
291{
292 struct mad_rmpp_recv *rmpp_recv;
293 struct ib_mad_hdr *mad_hdr = &mad_recv_wc->recv_buf.mad->mad_hdr;
294
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)
302 return rmpp_recv;
303 }
304 return NULL;
305}
306
307static struct mad_rmpp_recv *
308acquire_rmpp_recv(struct ib_mad_agent_private *agent,
309 struct ib_mad_recv_wc *mad_recv_wc)
310{
311 struct mad_rmpp_recv *rmpp_recv;
312 unsigned long flags;
313
314 spin_lock_irqsave(&agent->lock, flags);
315 rmpp_recv = find_rmpp_recv(agent, mad_recv_wc);
316 if (rmpp_recv)
317 atomic_inc(&rmpp_recv->refcount);
318 spin_unlock_irqrestore(&agent->lock, flags);
319 return rmpp_recv;
320}
321
322static struct mad_rmpp_recv *
323insert_rmpp_recv(struct ib_mad_agent_private *agent,
324 struct mad_rmpp_recv *rmpp_recv)
325{
326 struct mad_rmpp_recv *cur_rmpp_recv;
327
328 cur_rmpp_recv = find_rmpp_recv(agent, rmpp_recv->rmpp_wc);
329 if (!cur_rmpp_recv)
330 list_add_tail(&rmpp_recv->list, &agent->rmpp_list);
331
332 return cur_rmpp_recv;
333}
334
fa619a77
HR
335static inline int get_last_flag(struct ib_mad_recv_buf *seg)
336{
337 struct ib_rmpp_mad *rmpp_mad;
338
339 rmpp_mad = (struct ib_rmpp_mad *) seg->mad;
340 return ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) & IB_MGMT_RMPP_FLAG_LAST;
341}
342
343static inline int get_seg_num(struct ib_mad_recv_buf *seg)
344{
345 struct ib_rmpp_mad *rmpp_mad;
346
347 rmpp_mad = (struct ib_rmpp_mad *) seg->mad;
348 return be32_to_cpu(rmpp_mad->rmpp_hdr.seg_num);
349}
350
351static inline struct ib_mad_recv_buf * get_next_seg(struct list_head *rmpp_list,
352 struct ib_mad_recv_buf *seg)
353{
354 if (seg->list.next == rmpp_list)
355 return NULL;
356
357 return container_of(seg->list.next, struct ib_mad_recv_buf, list);
358}
359
360static inline int window_size(struct ib_mad_agent_private *agent)
361{
362 return max(agent->qp_info->recv_queue.max_active >> 3, 1);
363}
364
365static struct ib_mad_recv_buf * find_seg_location(struct list_head *rmpp_list,
366 int seg_num)
367{
368 struct ib_mad_recv_buf *seg_buf;
369 int cur_seg_num;
370
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)
374 return seg_buf;
375 if (seg_num == cur_seg_num)
376 break;
377 }
378 return NULL;
379}
380
381static void update_seg_num(struct mad_rmpp_recv *rmpp_recv,
382 struct ib_mad_recv_buf *new_buf)
383{
384 struct list_head *rmpp_list = &rmpp_recv->rmpp_wc->rmpp_list;
385
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);
390 }
391}
392
393static inline int get_mad_len(struct mad_rmpp_recv *rmpp_recv)
394{
395 struct ib_rmpp_mad *rmpp_mad;
396 int hdr_size, data_size, pad;
397
398 rmpp_mad = (struct ib_rmpp_mad *)rmpp_recv->cur_seg_buf->mad;
399
618a3c03 400 hdr_size = ib_get_mad_data_offset(rmpp_mad->mad_hdr.mgmt_class);
fa619a77 401 data_size = sizeof(struct ib_rmpp_mad) - hdr_size;
f2065e42
HR
402 pad = IB_MGMT_RMPP_DATA - be32_to_cpu(rmpp_mad->rmpp_hdr.paylen_newwin);
403 if (pad > IB_MGMT_RMPP_DATA || pad < 0)
fa619a77
HR
404 pad = 0;
405
406 return hdr_size + rmpp_recv->seg_num * data_size - pad;
407}
408
409static struct ib_mad_recv_wc * complete_rmpp(struct mad_rmpp_recv *rmpp_recv)
410{
411 struct ib_mad_recv_wc *rmpp_wc;
412
413 ack_recv(rmpp_recv, rmpp_recv->rmpp_wc);
414 if (rmpp_recv->seg_num > 1)
415 cancel_delayed_work(&rmpp_recv->timeout_work);
416
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));
422 return rmpp_wc;
423}
424
fa619a77
HR
425static struct ib_mad_recv_wc *
426continue_rmpp(struct ib_mad_agent_private *agent,
427 struct ib_mad_recv_wc *mad_recv_wc)
428{
429 struct mad_rmpp_recv *rmpp_recv;
430 struct ib_mad_recv_buf *prev_buf;
431 struct ib_mad_recv_wc *done_wc;
432 int seg_num;
433 unsigned long flags;
434
435 rmpp_recv = acquire_rmpp_recv(agent, mad_recv_wc);
436 if (!rmpp_recv)
437 goto drop1;
438
439 seg_num = get_seg_num(&mad_recv_wc->recv_buf);
440
441 spin_lock_irqsave(&rmpp_recv->lock, flags);
442 if ((rmpp_recv->state == RMPP_STATE_TIMEOUT) ||
443 (seg_num > rmpp_recv->newwin))
444 goto drop3;
445
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);
450 goto drop2;
451 }
452
453 prev_buf = find_seg_location(&rmpp_recv->rmpp_wc->rmpp_list, seg_num);
454 if (!prev_buf)
455 goto drop3;
456
457 done_wc = NULL;
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);
465 goto out;
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);
470 goto out;
471 }
472 }
473 spin_unlock_irqrestore(&rmpp_recv->lock, flags);
474out:
475 deref_rmpp_recv(rmpp_recv);
476 return done_wc;
477
478drop3: spin_unlock_irqrestore(&rmpp_recv->lock, flags);
479drop2: deref_rmpp_recv(rmpp_recv);
480drop1: ib_free_recv_mad(mad_recv_wc);
481 return NULL;
482}
483
484static struct ib_mad_recv_wc *
485start_rmpp(struct ib_mad_agent_private *agent,
486 struct ib_mad_recv_wc *mad_recv_wc)
487{
488 struct mad_rmpp_recv *rmpp_recv;
489 unsigned long flags;
490
491 rmpp_recv = create_rmpp_recv(agent, mad_recv_wc);
492 if (!rmpp_recv) {
493 ib_free_recv_mad(mad_recv_wc);
494 return NULL;
495 }
496
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);
503 }
504 atomic_inc(&rmpp_recv->refcount);
505
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);
510 } else {
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);
518 mad_recv_wc = NULL;
519 }
520 deref_rmpp_recv(rmpp_recv);
521 return mad_recv_wc;
522}
523
fa619a77
HR
524static int send_next_seg(struct ib_mad_send_wr_private *mad_send_wr)
525{
526 struct ib_rmpp_mad *rmpp_mad;
527 int timeout;
f36e1793 528 u32 paylen = 0;
fa619a77 529
34816ad9 530 rmpp_mad = mad_send_wr->send_buf.mad;
fa619a77 531 ib_set_rmpp_flags(&rmpp_mad->rmpp_hdr, IB_MGMT_RMPP_FLAG_ACTIVE);
f36e1793 532 rmpp_mad->rmpp_hdr.seg_num = cpu_to_be32(++mad_send_wr->seg_num);
fa619a77
HR
533
534 if (mad_send_wr->seg_num == 1) {
535 rmpp_mad->rmpp_hdr.rmpp_rtime_flags |= IB_MGMT_RMPP_FLAG_FIRST;
f36e1793 536 paylen = mad_send_wr->send_buf.seg_count * IB_MGMT_RMPP_DATA -
972d512a 537 mad_send_wr->pad;
fa619a77
HR
538 }
539
f36e1793 540 if (mad_send_wr->seg_num == mad_send_wr->send_buf.seg_count) {
fa619a77 541 rmpp_mad->rmpp_hdr.rmpp_rtime_flags |= IB_MGMT_RMPP_FLAG_LAST;
972d512a 542 paylen = IB_MGMT_RMPP_DATA - mad_send_wr->pad;
fa619a77 543 }
f36e1793 544 rmpp_mad->rmpp_hdr.paylen_newwin = cpu_to_be32(paylen);
fa619a77
HR
545
546 /* 2 seconds for an ACK until we can find the packet lifetime */
34816ad9 547 timeout = mad_send_wr->send_buf.timeout_ms;
fa619a77
HR
548 if (!timeout || timeout > 2000)
549 mad_send_wr->timeout = msecs_to_jiffies(2000);
f36e1793 550
fa619a77
HR
551 return ib_send_mad(mad_send_wr);
552}
553
fa9656bb
JM
554static void abort_send(struct ib_mad_agent_private *agent,
555 struct ib_mad_recv_wc *mad_recv_wc, u8 rmpp_status)
fe9e08e1
SH
556{
557 struct ib_mad_send_wr_private *mad_send_wr;
558 struct ib_mad_send_wc wc;
559 unsigned long flags;
560
561 spin_lock_irqsave(&agent->lock, flags);
fa9656bb 562 mad_send_wr = ib_find_send_mad(agent, mad_recv_wc);
fe9e08e1
SH
563 if (!mad_send_wr)
564 goto out; /* Unmatched send */
565
f36e1793 566 if ((mad_send_wr->last_ack == mad_send_wr->send_buf.seg_count) ||
fe9e08e1
SH
567 (!mad_send_wr->timeout) || (mad_send_wr->status != IB_WC_SUCCESS))
568 goto out; /* Send is already done */
569
570 ib_mark_mad_done(mad_send_wr);
571 spin_unlock_irqrestore(&agent->lock, flags);
572
573 wc.status = IB_WC_REM_ABORT_ERR;
574 wc.vendor_err = rmpp_status;
34816ad9 575 wc.send_buf = &mad_send_wr->send_buf;
fe9e08e1
SH
576 ib_mad_complete_send_wr(mad_send_wr, &wc);
577 return;
578out:
579 spin_unlock_irqrestore(&agent->lock, flags);
580}
581
f36e1793
JM
582static inline void adjust_last_ack(struct ib_mad_send_wr_private *wr,
583 int seg_num)
584{
585 struct list_head *list;
586
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)
591 break;
592}
593
fa619a77
HR
594static void process_rmpp_ack(struct ib_mad_agent_private *agent,
595 struct ib_mad_recv_wc *mad_recv_wc)
596{
597 struct ib_mad_send_wr_private *mad_send_wr;
598 struct ib_rmpp_mad *rmpp_mad;
599 unsigned long flags;
600 int seg_num, newwin, ret;
601
602 rmpp_mad = (struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad;
fe9e08e1 603 if (rmpp_mad->rmpp_hdr.rmpp_status) {
fa9656bb 604 abort_send(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BAD_STATUS);
fe9e08e1 605 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BAD_STATUS);
fa619a77 606 return;
fe9e08e1 607 }
fa619a77
HR
608
609 seg_num = be32_to_cpu(rmpp_mad->rmpp_hdr.seg_num);
610 newwin = be32_to_cpu(rmpp_mad->rmpp_hdr.paylen_newwin);
fe9e08e1 611 if (newwin < seg_num) {
fa9656bb 612 abort_send(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_W2S);
fe9e08e1
SH
613 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_W2S);
614 return;
615 }
fa619a77
HR
616
617 spin_lock_irqsave(&agent->lock, flags);
fa9656bb 618 mad_send_wr = ib_find_send_mad(agent, mad_recv_wc);
fa619a77
HR
619 if (!mad_send_wr)
620 goto out; /* Unmatched ACK */
621
f36e1793 622 if ((mad_send_wr->last_ack == mad_send_wr->send_buf.seg_count) ||
fa619a77
HR
623 (!mad_send_wr->timeout) || (mad_send_wr->status != IB_WC_SUCCESS))
624 goto out; /* Send is already done */
625
f36e1793
JM
626 if (seg_num > mad_send_wr->send_buf.seg_count ||
627 seg_num > mad_send_wr->newwin) {
fe9e08e1 628 spin_unlock_irqrestore(&agent->lock, flags);
fa9656bb 629 abort_send(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_S2B);
fe9e08e1
SH
630 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_S2B);
631 return;
632 }
fa619a77
HR
633
634 if (newwin < mad_send_wr->newwin || seg_num < mad_send_wr->last_ack)
635 goto out; /* Old ACK */
636
637 if (seg_num > mad_send_wr->last_ack) {
f36e1793 638 adjust_last_ack(mad_send_wr, seg_num);
34816ad9 639 mad_send_wr->retries = mad_send_wr->send_buf.retries;
fa619a77
HR
640 }
641 mad_send_wr->newwin = newwin;
f36e1793 642 if (mad_send_wr->last_ack == mad_send_wr->send_buf.seg_count) {
fa619a77 643 /* If no response is expected, the ACK completes the send */
34816ad9 644 if (!mad_send_wr->send_buf.timeout_ms) {
fa619a77
HR
645 struct ib_mad_send_wc wc;
646
647 ib_mark_mad_done(mad_send_wr);
648 spin_unlock_irqrestore(&agent->lock, flags);
649
650 wc.status = IB_WC_SUCCESS;
651 wc.vendor_err = 0;
34816ad9 652 wc.send_buf = &mad_send_wr->send_buf;
fa619a77
HR
653 ib_mad_complete_send_wr(mad_send_wr, &wc);
654 return;
655 }
656 if (mad_send_wr->refcount == 1)
34816ad9
SH
657 ib_reset_mad_timeout(mad_send_wr,
658 mad_send_wr->send_buf.timeout_ms);
fa619a77
HR
659 } else if (mad_send_wr->refcount == 1 &&
660 mad_send_wr->seg_num < mad_send_wr->newwin &&
f36e1793 661 mad_send_wr->seg_num < mad_send_wr->send_buf.seg_count) {
fa619a77
HR
662 /* Send failure will just result in a timeout/retry */
663 ret = send_next_seg(mad_send_wr);
664 if (ret)
665 goto out;
666
667 mad_send_wr->refcount++;
668 list_del(&mad_send_wr->agent_list);
669 list_add_tail(&mad_send_wr->agent_list,
670 &mad_send_wr->mad_agent_priv->send_list);
671 }
672out:
673 spin_unlock_irqrestore(&agent->lock, flags);
674}
675
fe9e08e1
SH
676static struct ib_mad_recv_wc *
677process_rmpp_data(struct ib_mad_agent_private *agent,
678 struct ib_mad_recv_wc *mad_recv_wc)
679{
680 struct ib_rmpp_hdr *rmpp_hdr;
681 u8 rmpp_status;
682
683 rmpp_hdr = &((struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad)->rmpp_hdr;
684
685 if (rmpp_hdr->rmpp_status) {
686 rmpp_status = IB_MGMT_RMPP_STATUS_BAD_STATUS;
687 goto bad;
688 }
689
690 if (rmpp_hdr->seg_num == __constant_htonl(1)) {
691 if (!(ib_get_rmpp_flags(rmpp_hdr) & IB_MGMT_RMPP_FLAG_FIRST)) {
692 rmpp_status = IB_MGMT_RMPP_STATUS_BAD_SEG;
693 goto bad;
694 }
695 return start_rmpp(agent, mad_recv_wc);
696 } else {
697 if (ib_get_rmpp_flags(rmpp_hdr) & IB_MGMT_RMPP_FLAG_FIRST) {
698 rmpp_status = IB_MGMT_RMPP_STATUS_BAD_SEG;
699 goto bad;
700 }
701 return continue_rmpp(agent, mad_recv_wc);
702 }
703bad:
704 nack_recv(agent, mad_recv_wc, rmpp_status);
705 ib_free_recv_mad(mad_recv_wc);
706 return NULL;
707}
708
709static void process_rmpp_stop(struct ib_mad_agent_private *agent,
710 struct ib_mad_recv_wc *mad_recv_wc)
711{
712 struct ib_rmpp_mad *rmpp_mad;
713
714 rmpp_mad = (struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad;
715
716 if (rmpp_mad->rmpp_hdr.rmpp_status != IB_MGMT_RMPP_STATUS_RESX) {
fa9656bb 717 abort_send(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BAD_STATUS);
fe9e08e1
SH
718 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BAD_STATUS);
719 } else
fa9656bb 720 abort_send(agent, mad_recv_wc, rmpp_mad->rmpp_hdr.rmpp_status);
fe9e08e1
SH
721}
722
723static void process_rmpp_abort(struct ib_mad_agent_private *agent,
724 struct ib_mad_recv_wc *mad_recv_wc)
725{
726 struct ib_rmpp_mad *rmpp_mad;
727
728 rmpp_mad = (struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad;
729
730 if (rmpp_mad->rmpp_hdr.rmpp_status < IB_MGMT_RMPP_STATUS_ABORT_MIN ||
731 rmpp_mad->rmpp_hdr.rmpp_status > IB_MGMT_RMPP_STATUS_ABORT_MAX) {
fa9656bb 732 abort_send(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BAD_STATUS);
fe9e08e1
SH
733 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BAD_STATUS);
734 } else
fa9656bb 735 abort_send(agent, mad_recv_wc, rmpp_mad->rmpp_hdr.rmpp_status);
fe9e08e1
SH
736}
737
fa619a77
HR
738struct ib_mad_recv_wc *
739ib_process_rmpp_recv_wc(struct ib_mad_agent_private *agent,
740 struct ib_mad_recv_wc *mad_recv_wc)
741{
742 struct ib_rmpp_mad *rmpp_mad;
743
744 rmpp_mad = (struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad;
745 if (!(rmpp_mad->rmpp_hdr.rmpp_rtime_flags & IB_MGMT_RMPP_FLAG_ACTIVE))
746 return mad_recv_wc;
747
fe9e08e1 748 if (rmpp_mad->rmpp_hdr.rmpp_version != IB_MGMT_RMPP_VERSION) {
fa9656bb 749 abort_send(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_UNV);
fe9e08e1 750 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_UNV);
fa619a77 751 goto out;
fe9e08e1 752 }
fa619a77
HR
753
754 switch (rmpp_mad->rmpp_hdr.rmpp_type) {
755 case IB_MGMT_RMPP_TYPE_DATA:
fe9e08e1 756 return process_rmpp_data(agent, mad_recv_wc);
fa619a77
HR
757 case IB_MGMT_RMPP_TYPE_ACK:
758 process_rmpp_ack(agent, mad_recv_wc);
759 break;
760 case IB_MGMT_RMPP_TYPE_STOP:
fe9e08e1
SH
761 process_rmpp_stop(agent, mad_recv_wc);
762 break;
fa619a77 763 case IB_MGMT_RMPP_TYPE_ABORT:
fe9e08e1 764 process_rmpp_abort(agent, mad_recv_wc);
fa619a77
HR
765 break;
766 default:
fa9656bb 767 abort_send(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BADT);
fe9e08e1 768 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BADT);
fa619a77
HR
769 break;
770 }
771out:
772 ib_free_recv_mad(mad_recv_wc);
773 return NULL;
774}
775
776int ib_send_rmpp_mad(struct ib_mad_send_wr_private *mad_send_wr)
777{
778 struct ib_rmpp_mad *rmpp_mad;
f36e1793 779 int ret;
fa619a77 780
34816ad9 781 rmpp_mad = mad_send_wr->send_buf.mad;
fa619a77
HR
782 if (!(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
783 IB_MGMT_RMPP_FLAG_ACTIVE))
784 return IB_RMPP_RESULT_UNHANDLED;
785
f36e1793
JM
786 if (rmpp_mad->rmpp_hdr.rmpp_type != IB_MGMT_RMPP_TYPE_DATA) {
787 mad_send_wr->seg_num = 1;
fa619a77 788 return IB_RMPP_RESULT_INTERNAL;
f36e1793 789 }
fa619a77 790
fa619a77 791 mad_send_wr->newwin = 1;
fa619a77
HR
792
793 /* We need to wait for the final ACK even if there isn't a response */
794 mad_send_wr->refcount += (mad_send_wr->timeout == 0);
795 ret = send_next_seg(mad_send_wr);
796 if (!ret)
797 return IB_RMPP_RESULT_CONSUMED;
798 return ret;
799}
800
801int ib_process_rmpp_send_wc(struct ib_mad_send_wr_private *mad_send_wr,
802 struct ib_mad_send_wc *mad_send_wc)
803{
804 struct ib_rmpp_mad *rmpp_mad;
fa619a77
HR
805 int ret;
806
34816ad9 807 rmpp_mad = mad_send_wr->send_buf.mad;
fa619a77
HR
808 if (!(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
809 IB_MGMT_RMPP_FLAG_ACTIVE))
810 return IB_RMPP_RESULT_UNHANDLED; /* RMPP not active */
811
34816ad9 812 if (rmpp_mad->rmpp_hdr.rmpp_type != IB_MGMT_RMPP_TYPE_DATA)
fa619a77 813 return IB_RMPP_RESULT_INTERNAL; /* ACK, STOP, or ABORT */
fa619a77
HR
814
815 if (mad_send_wc->status != IB_WC_SUCCESS ||
816 mad_send_wr->status != IB_WC_SUCCESS)
817 return IB_RMPP_RESULT_PROCESSED; /* Canceled or send error */
818
819 if (!mad_send_wr->timeout)
820 return IB_RMPP_RESULT_PROCESSED; /* Response received */
821
f36e1793 822 if (mad_send_wr->last_ack == mad_send_wr->send_buf.seg_count) {
fa619a77 823 mad_send_wr->timeout =
34816ad9 824 msecs_to_jiffies(mad_send_wr->send_buf.timeout_ms);
fa619a77
HR
825 return IB_RMPP_RESULT_PROCESSED; /* Send done */
826 }
827
f36e1793
JM
828 if (mad_send_wr->seg_num == mad_send_wr->newwin ||
829 mad_send_wr->seg_num == mad_send_wr->send_buf.seg_count)
fa619a77
HR
830 return IB_RMPP_RESULT_PROCESSED; /* Wait for ACK */
831
832 ret = send_next_seg(mad_send_wr);
833 if (ret) {
834 mad_send_wc->status = IB_WC_GENERAL_ERR;
835 return IB_RMPP_RESULT_PROCESSED;
836 }
837 return IB_RMPP_RESULT_CONSUMED;
838}
839
840int ib_retry_rmpp(struct ib_mad_send_wr_private *mad_send_wr)
841{
842 struct ib_rmpp_mad *rmpp_mad;
843 int ret;
844
34816ad9 845 rmpp_mad = mad_send_wr->send_buf.mad;
fa619a77
HR
846 if (!(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
847 IB_MGMT_RMPP_FLAG_ACTIVE))
848 return IB_RMPP_RESULT_UNHANDLED; /* RMPP not active */
849
f36e1793 850 if (mad_send_wr->last_ack == mad_send_wr->send_buf.seg_count)
fa619a77
HR
851 return IB_RMPP_RESULT_PROCESSED;
852
f36e1793
JM
853 mad_send_wr->seg_num = mad_send_wr->last_ack;
854 mad_send_wr->cur_seg = mad_send_wr->last_ack_seg;
855
fa619a77
HR
856 ret = send_next_seg(mad_send_wr);
857 if (ret)
858 return IB_RMPP_RESULT_PROCESSED;
859
860 return IB_RMPP_RESULT_CONSUMED;
861}