[PATCH] cyclades: return closing_wait
[linux-2.6] / drivers / infiniband / core / mad_rmpp.c
1 /*
2  * Copyright (c) 2005 Intel Inc. All rights reserved.
3  * Copyright (c) 2005-2006 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 "mad_priv.h"
37 #include "mad_rmpp.h"
38
39 enum rmpp_state {
40         RMPP_STATE_ACTIVE,
41         RMPP_STATE_TIMEOUT,
42         RMPP_STATE_COMPLETE
43 };
44
45 struct mad_rmpp_recv {
46         struct ib_mad_agent_private *agent;
47         struct list_head list;
48         struct delayed_work timeout_work;
49         struct delayed_work cleanup_work;
50         struct completion comp;
51         enum rmpp_state state;
52         spinlock_t lock;
53         atomic_t refcount;
54
55         struct ib_ah *ah;
56         struct ib_mad_recv_wc *rmpp_wc;
57         struct ib_mad_recv_buf *cur_seg_buf;
58         int last_ack;
59         int seg_num;
60         int newwin;
61         int repwin;
62
63         __be64 tid;
64         u32 src_qp;
65         u16 slid;
66         u8 mgmt_class;
67         u8 class_version;
68         u8 method;
69 };
70
71 static inline void deref_rmpp_recv(struct mad_rmpp_recv *rmpp_recv)
72 {
73         if (atomic_dec_and_test(&rmpp_recv->refcount))
74                 complete(&rmpp_recv->comp);
75 }
76
77 static void destroy_rmpp_recv(struct mad_rmpp_recv *rmpp_recv)
78 {
79         deref_rmpp_recv(rmpp_recv);
80         wait_for_completion(&rmpp_recv->comp);
81         ib_destroy_ah(rmpp_recv->ah);
82         kfree(rmpp_recv);
83 }
84
85 void ib_cancel_rmpp_recvs(struct ib_mad_agent_private *agent)
86 {
87         struct mad_rmpp_recv *rmpp_recv, *temp_rmpp_recv;
88         unsigned long flags;
89
90         spin_lock_irqsave(&agent->lock, flags);
91         list_for_each_entry(rmpp_recv, &agent->rmpp_list, list) {
92                 cancel_delayed_work(&rmpp_recv->timeout_work);
93                 cancel_delayed_work(&rmpp_recv->cleanup_work);
94         }
95         spin_unlock_irqrestore(&agent->lock, flags);
96
97         flush_workqueue(agent->qp_info->port_priv->wq);
98
99         list_for_each_entry_safe(rmpp_recv, temp_rmpp_recv,
100                                  &agent->rmpp_list, list) {
101                 list_del(&rmpp_recv->list);
102                 if (rmpp_recv->state != RMPP_STATE_COMPLETE)
103                         ib_free_recv_mad(rmpp_recv->rmpp_wc);
104                 destroy_rmpp_recv(rmpp_recv);
105         }
106 }
107
108 static void format_ack(struct ib_mad_send_buf *msg,
109                        struct ib_rmpp_mad *data,
110                        struct mad_rmpp_recv *rmpp_recv)
111 {
112         struct ib_rmpp_mad *ack = msg->mad;
113         unsigned long flags;
114
115         memcpy(ack, &data->mad_hdr, msg->hdr_len);
116
117         ack->mad_hdr.method ^= IB_MGMT_METHOD_RESP;
118         ack->rmpp_hdr.rmpp_type = IB_MGMT_RMPP_TYPE_ACK;
119         ib_set_rmpp_flags(&ack->rmpp_hdr, IB_MGMT_RMPP_FLAG_ACTIVE);
120
121         spin_lock_irqsave(&rmpp_recv->lock, flags);
122         rmpp_recv->last_ack = rmpp_recv->seg_num;
123         ack->rmpp_hdr.seg_num = cpu_to_be32(rmpp_recv->seg_num);
124         ack->rmpp_hdr.paylen_newwin = cpu_to_be32(rmpp_recv->newwin);
125         spin_unlock_irqrestore(&rmpp_recv->lock, flags);
126 }
127
128 static void ack_recv(struct mad_rmpp_recv *rmpp_recv,
129                      struct ib_mad_recv_wc *recv_wc)
130 {
131         struct ib_mad_send_buf *msg;
132         int ret, hdr_len;
133
134         hdr_len = ib_get_mad_data_offset(recv_wc->recv_buf.mad->mad_hdr.mgmt_class);
135         msg = ib_create_send_mad(&rmpp_recv->agent->agent, recv_wc->wc->src_qp,
136                                  recv_wc->wc->pkey_index, 1, hdr_len,
137                                  0, GFP_KERNEL);
138         if (!msg)
139                 return;
140
141         format_ack(msg, (struct ib_rmpp_mad *) recv_wc->recv_buf.mad, rmpp_recv);
142         msg->ah = rmpp_recv->ah;
143         ret = ib_post_send_mad(msg, NULL);
144         if (ret)
145                 ib_free_send_mad(msg);
146 }
147
148 static struct ib_mad_send_buf *alloc_response_msg(struct ib_mad_agent *agent,
149                                                   struct ib_mad_recv_wc *recv_wc)
150 {
151         struct ib_mad_send_buf *msg;
152         struct ib_ah *ah;
153         int hdr_len;
154
155         ah = ib_create_ah_from_wc(agent->qp->pd, recv_wc->wc,
156                                   recv_wc->recv_buf.grh, agent->port_num);
157         if (IS_ERR(ah))
158                 return (void *) ah;
159
160         hdr_len = ib_get_mad_data_offset(recv_wc->recv_buf.mad->mad_hdr.mgmt_class);
161         msg = ib_create_send_mad(agent, recv_wc->wc->src_qp,
162                                  recv_wc->wc->pkey_index, 1,
163                                  hdr_len, 0, GFP_KERNEL);
164         if (IS_ERR(msg))
165                 ib_destroy_ah(ah);
166         else
167                 msg->ah = ah;
168
169         return msg;
170 }
171
172 static void ack_ds_ack(struct ib_mad_agent_private *agent,
173                        struct ib_mad_recv_wc *recv_wc)
174 {
175         struct ib_mad_send_buf *msg;
176         struct ib_rmpp_mad *rmpp_mad;
177         int ret;
178
179         msg = alloc_response_msg(&agent->agent, recv_wc);
180         if (IS_ERR(msg))
181                 return;
182
183         rmpp_mad = msg->mad;
184         memcpy(rmpp_mad, recv_wc->recv_buf.mad, msg->hdr_len);
185
186         rmpp_mad->mad_hdr.method ^= IB_MGMT_METHOD_RESP;
187         ib_set_rmpp_flags(&rmpp_mad->rmpp_hdr, IB_MGMT_RMPP_FLAG_ACTIVE);
188         rmpp_mad->rmpp_hdr.seg_num = 0;
189         rmpp_mad->rmpp_hdr.paylen_newwin = cpu_to_be32(1);
190
191         ret = ib_post_send_mad(msg, NULL);
192         if (ret) {
193                 ib_destroy_ah(msg->ah);
194                 ib_free_send_mad(msg);
195         }
196 }
197
198 void ib_rmpp_send_handler(struct ib_mad_send_wc *mad_send_wc)
199 {
200         struct ib_rmpp_mad *rmpp_mad = mad_send_wc->send_buf->mad;
201
202         if (rmpp_mad->rmpp_hdr.rmpp_type != IB_MGMT_RMPP_TYPE_ACK)
203                 ib_destroy_ah(mad_send_wc->send_buf->ah);
204         ib_free_send_mad(mad_send_wc->send_buf);
205 }
206
207 static void nack_recv(struct ib_mad_agent_private *agent,
208                       struct ib_mad_recv_wc *recv_wc, u8 rmpp_status)
209 {
210         struct ib_mad_send_buf *msg;
211         struct ib_rmpp_mad *rmpp_mad;
212         int ret;
213
214         msg = alloc_response_msg(&agent->agent, recv_wc);
215         if (IS_ERR(msg))
216                 return;
217
218         rmpp_mad = msg->mad;
219         memcpy(rmpp_mad, recv_wc->recv_buf.mad, msg->hdr_len);
220
221         rmpp_mad->mad_hdr.method ^= IB_MGMT_METHOD_RESP;
222         rmpp_mad->rmpp_hdr.rmpp_version = IB_MGMT_RMPP_VERSION;
223         rmpp_mad->rmpp_hdr.rmpp_type = IB_MGMT_RMPP_TYPE_ABORT;
224         ib_set_rmpp_flags(&rmpp_mad->rmpp_hdr, IB_MGMT_RMPP_FLAG_ACTIVE);
225         rmpp_mad->rmpp_hdr.rmpp_status = rmpp_status;
226         rmpp_mad->rmpp_hdr.seg_num = 0;
227         rmpp_mad->rmpp_hdr.paylen_newwin = 0;
228
229         ret = ib_post_send_mad(msg, NULL);
230         if (ret) {
231                 ib_destroy_ah(msg->ah);
232                 ib_free_send_mad(msg);
233         }
234 }
235
236 static void recv_timeout_handler(struct work_struct *work)
237 {
238         struct mad_rmpp_recv *rmpp_recv =
239                 container_of(work, struct mad_rmpp_recv, timeout_work.work);
240         struct ib_mad_recv_wc *rmpp_wc;
241         unsigned long flags;
242
243         spin_lock_irqsave(&rmpp_recv->agent->lock, flags);
244         if (rmpp_recv->state != RMPP_STATE_ACTIVE) {
245                 spin_unlock_irqrestore(&rmpp_recv->agent->lock, flags);
246                 return;
247         }
248         rmpp_recv->state = RMPP_STATE_TIMEOUT;
249         list_del(&rmpp_recv->list);
250         spin_unlock_irqrestore(&rmpp_recv->agent->lock, flags);
251
252         rmpp_wc = rmpp_recv->rmpp_wc;
253         nack_recv(rmpp_recv->agent, rmpp_wc, IB_MGMT_RMPP_STATUS_T2L);
254         destroy_rmpp_recv(rmpp_recv);
255         ib_free_recv_mad(rmpp_wc);
256 }
257
258 static void recv_cleanup_handler(struct work_struct *work)
259 {
260         struct mad_rmpp_recv *rmpp_recv =
261                 container_of(work, struct mad_rmpp_recv, cleanup_work.work);
262         unsigned long flags;
263
264         spin_lock_irqsave(&rmpp_recv->agent->lock, flags);
265         list_del(&rmpp_recv->list);
266         spin_unlock_irqrestore(&rmpp_recv->agent->lock, flags);
267         destroy_rmpp_recv(rmpp_recv);
268 }
269
270 static struct mad_rmpp_recv *
271 create_rmpp_recv(struct ib_mad_agent_private *agent,
272                  struct ib_mad_recv_wc *mad_recv_wc)
273 {
274         struct mad_rmpp_recv *rmpp_recv;
275         struct ib_mad_hdr *mad_hdr;
276
277         rmpp_recv = kmalloc(sizeof *rmpp_recv, GFP_KERNEL);
278         if (!rmpp_recv)
279                 return NULL;
280
281         rmpp_recv->ah = ib_create_ah_from_wc(agent->agent.qp->pd,
282                                              mad_recv_wc->wc,
283                                              mad_recv_wc->recv_buf.grh,
284                                              agent->agent.port_num);
285         if (IS_ERR(rmpp_recv->ah))
286                 goto error;
287
288         rmpp_recv->agent = agent;
289         init_completion(&rmpp_recv->comp);
290         INIT_DELAYED_WORK(&rmpp_recv->timeout_work, recv_timeout_handler);
291         INIT_DELAYED_WORK(&rmpp_recv->cleanup_work, recv_cleanup_handler);
292         spin_lock_init(&rmpp_recv->lock);
293         rmpp_recv->state = RMPP_STATE_ACTIVE;
294         atomic_set(&rmpp_recv->refcount, 1);
295
296         rmpp_recv->rmpp_wc = mad_recv_wc;
297         rmpp_recv->cur_seg_buf = &mad_recv_wc->recv_buf;
298         rmpp_recv->newwin = 1;
299         rmpp_recv->seg_num = 1;
300         rmpp_recv->last_ack = 0;
301         rmpp_recv->repwin = 1;
302
303         mad_hdr = &mad_recv_wc->recv_buf.mad->mad_hdr;
304         rmpp_recv->tid = mad_hdr->tid;
305         rmpp_recv->src_qp = mad_recv_wc->wc->src_qp;
306         rmpp_recv->slid = mad_recv_wc->wc->slid;
307         rmpp_recv->mgmt_class = mad_hdr->mgmt_class;
308         rmpp_recv->class_version = mad_hdr->class_version;
309         rmpp_recv->method  = mad_hdr->method;
310         return rmpp_recv;
311
312 error:  kfree(rmpp_recv);
313         return NULL;
314 }
315
316 static struct mad_rmpp_recv *
317 find_rmpp_recv(struct ib_mad_agent_private *agent,
318                struct ib_mad_recv_wc *mad_recv_wc)
319 {
320         struct mad_rmpp_recv *rmpp_recv;
321         struct ib_mad_hdr *mad_hdr = &mad_recv_wc->recv_buf.mad->mad_hdr;
322
323         list_for_each_entry(rmpp_recv, &agent->rmpp_list, list) {
324                 if (rmpp_recv->tid == mad_hdr->tid &&
325                     rmpp_recv->src_qp == mad_recv_wc->wc->src_qp &&
326                     rmpp_recv->slid == mad_recv_wc->wc->slid &&
327                     rmpp_recv->mgmt_class == mad_hdr->mgmt_class &&
328                     rmpp_recv->class_version == mad_hdr->class_version &&
329                     rmpp_recv->method == mad_hdr->method)
330                         return rmpp_recv;
331         }
332         return NULL;
333 }
334
335 static struct mad_rmpp_recv *
336 acquire_rmpp_recv(struct ib_mad_agent_private *agent,
337                   struct ib_mad_recv_wc *mad_recv_wc)
338 {
339         struct mad_rmpp_recv *rmpp_recv;
340         unsigned long flags;
341
342         spin_lock_irqsave(&agent->lock, flags);
343         rmpp_recv = find_rmpp_recv(agent, mad_recv_wc);
344         if (rmpp_recv)
345                 atomic_inc(&rmpp_recv->refcount);
346         spin_unlock_irqrestore(&agent->lock, flags);
347         return rmpp_recv;
348 }
349
350 static struct mad_rmpp_recv *
351 insert_rmpp_recv(struct ib_mad_agent_private *agent,
352                  struct mad_rmpp_recv *rmpp_recv)
353 {
354         struct mad_rmpp_recv *cur_rmpp_recv;
355
356         cur_rmpp_recv = find_rmpp_recv(agent, rmpp_recv->rmpp_wc);
357         if (!cur_rmpp_recv)
358                 list_add_tail(&rmpp_recv->list, &agent->rmpp_list);
359
360         return cur_rmpp_recv;
361 }
362
363 static inline int get_last_flag(struct ib_mad_recv_buf *seg)
364 {
365         struct ib_rmpp_mad *rmpp_mad;
366
367         rmpp_mad = (struct ib_rmpp_mad *) seg->mad;
368         return ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) & IB_MGMT_RMPP_FLAG_LAST;
369 }
370
371 static inline int get_seg_num(struct ib_mad_recv_buf *seg)
372 {
373         struct ib_rmpp_mad *rmpp_mad;
374
375         rmpp_mad = (struct ib_rmpp_mad *) seg->mad;
376         return be32_to_cpu(rmpp_mad->rmpp_hdr.seg_num);
377 }
378
379 static inline struct ib_mad_recv_buf * get_next_seg(struct list_head *rmpp_list,
380                                                     struct ib_mad_recv_buf *seg)
381 {
382         if (seg->list.next == rmpp_list)
383                 return NULL;
384
385         return container_of(seg->list.next, struct ib_mad_recv_buf, list);
386 }
387
388 static inline int window_size(struct ib_mad_agent_private *agent)
389 {
390         return max(agent->qp_info->recv_queue.max_active >> 3, 1);
391 }
392
393 static struct ib_mad_recv_buf * find_seg_location(struct list_head *rmpp_list,
394                                                   int seg_num)
395 {
396         struct ib_mad_recv_buf *seg_buf;
397         int cur_seg_num;
398
399         list_for_each_entry_reverse(seg_buf, rmpp_list, list) {
400                 cur_seg_num = get_seg_num(seg_buf);
401                 if (seg_num > cur_seg_num)
402                         return seg_buf;
403                 if (seg_num == cur_seg_num)
404                         break;
405         }
406         return NULL;
407 }
408
409 static void update_seg_num(struct mad_rmpp_recv *rmpp_recv,
410                            struct ib_mad_recv_buf *new_buf)
411 {
412         struct list_head *rmpp_list = &rmpp_recv->rmpp_wc->rmpp_list;
413
414         while (new_buf && (get_seg_num(new_buf) == rmpp_recv->seg_num + 1)) {
415                 rmpp_recv->cur_seg_buf = new_buf;
416                 rmpp_recv->seg_num++;
417                 new_buf = get_next_seg(rmpp_list, new_buf);
418         }
419 }
420
421 static inline int get_mad_len(struct mad_rmpp_recv *rmpp_recv)
422 {
423         struct ib_rmpp_mad *rmpp_mad;
424         int hdr_size, data_size, pad;
425
426         rmpp_mad = (struct ib_rmpp_mad *)rmpp_recv->cur_seg_buf->mad;
427
428         hdr_size = ib_get_mad_data_offset(rmpp_mad->mad_hdr.mgmt_class);
429         data_size = sizeof(struct ib_rmpp_mad) - hdr_size;
430         pad = IB_MGMT_RMPP_DATA - be32_to_cpu(rmpp_mad->rmpp_hdr.paylen_newwin);
431         if (pad > IB_MGMT_RMPP_DATA || pad < 0)
432                 pad = 0;
433
434         return hdr_size + rmpp_recv->seg_num * data_size - pad;
435 }
436
437 static struct ib_mad_recv_wc * complete_rmpp(struct mad_rmpp_recv *rmpp_recv)
438 {
439         struct ib_mad_recv_wc *rmpp_wc;
440
441         ack_recv(rmpp_recv, rmpp_recv->rmpp_wc);
442         if (rmpp_recv->seg_num > 1)
443                 cancel_delayed_work(&rmpp_recv->timeout_work);
444
445         rmpp_wc = rmpp_recv->rmpp_wc;
446         rmpp_wc->mad_len = get_mad_len(rmpp_recv);
447         /* 10 seconds until we can find the packet lifetime */
448         queue_delayed_work(rmpp_recv->agent->qp_info->port_priv->wq,
449                            &rmpp_recv->cleanup_work, msecs_to_jiffies(10000));
450         return rmpp_wc;
451 }
452
453 static struct ib_mad_recv_wc *
454 continue_rmpp(struct ib_mad_agent_private *agent,
455               struct ib_mad_recv_wc *mad_recv_wc)
456 {
457         struct mad_rmpp_recv *rmpp_recv;
458         struct ib_mad_recv_buf *prev_buf;
459         struct ib_mad_recv_wc *done_wc;
460         int seg_num;
461         unsigned long flags;
462
463         rmpp_recv = acquire_rmpp_recv(agent, mad_recv_wc);
464         if (!rmpp_recv)
465                 goto drop1;
466
467         seg_num = get_seg_num(&mad_recv_wc->recv_buf);
468
469         spin_lock_irqsave(&rmpp_recv->lock, flags);
470         if ((rmpp_recv->state == RMPP_STATE_TIMEOUT) ||
471             (seg_num > rmpp_recv->newwin))
472                 goto drop3;
473
474         if ((seg_num <= rmpp_recv->last_ack) ||
475             (rmpp_recv->state == RMPP_STATE_COMPLETE)) {
476                 spin_unlock_irqrestore(&rmpp_recv->lock, flags);
477                 ack_recv(rmpp_recv, mad_recv_wc);
478                 goto drop2;
479         }
480
481         prev_buf = find_seg_location(&rmpp_recv->rmpp_wc->rmpp_list, seg_num);
482         if (!prev_buf)
483                 goto drop3;
484
485         done_wc = NULL;
486         list_add(&mad_recv_wc->recv_buf.list, &prev_buf->list);
487         if (rmpp_recv->cur_seg_buf == prev_buf) {
488                 update_seg_num(rmpp_recv, &mad_recv_wc->recv_buf);
489                 if (get_last_flag(rmpp_recv->cur_seg_buf)) {
490                         rmpp_recv->state = RMPP_STATE_COMPLETE;
491                         spin_unlock_irqrestore(&rmpp_recv->lock, flags);
492                         done_wc = complete_rmpp(rmpp_recv);
493                         goto out;
494                 } else if (rmpp_recv->seg_num == rmpp_recv->newwin) {
495                         rmpp_recv->newwin += window_size(agent);
496                         spin_unlock_irqrestore(&rmpp_recv->lock, flags);
497                         ack_recv(rmpp_recv, mad_recv_wc);
498                         goto out;
499                 }
500         }
501         spin_unlock_irqrestore(&rmpp_recv->lock, flags);
502 out:
503         deref_rmpp_recv(rmpp_recv);
504         return done_wc;
505
506 drop3:  spin_unlock_irqrestore(&rmpp_recv->lock, flags);
507 drop2:  deref_rmpp_recv(rmpp_recv);
508 drop1:  ib_free_recv_mad(mad_recv_wc);
509         return NULL;
510 }
511
512 static struct ib_mad_recv_wc *
513 start_rmpp(struct ib_mad_agent_private *agent,
514            struct ib_mad_recv_wc *mad_recv_wc)
515 {
516         struct mad_rmpp_recv *rmpp_recv;
517         unsigned long flags;
518
519         rmpp_recv = create_rmpp_recv(agent, mad_recv_wc);
520         if (!rmpp_recv) {
521                 ib_free_recv_mad(mad_recv_wc);
522                 return NULL;
523         }
524
525         spin_lock_irqsave(&agent->lock, flags);
526         if (insert_rmpp_recv(agent, rmpp_recv)) {
527                 spin_unlock_irqrestore(&agent->lock, flags);
528                 /* duplicate first MAD */
529                 destroy_rmpp_recv(rmpp_recv);
530                 return continue_rmpp(agent, mad_recv_wc);
531         }
532         atomic_inc(&rmpp_recv->refcount);
533
534         if (get_last_flag(&mad_recv_wc->recv_buf)) {
535                 rmpp_recv->state = RMPP_STATE_COMPLETE;
536                 spin_unlock_irqrestore(&agent->lock, flags);
537                 complete_rmpp(rmpp_recv);
538         } else {
539                 spin_unlock_irqrestore(&agent->lock, flags);
540                 /* 40 seconds until we can find the packet lifetimes */
541                 queue_delayed_work(agent->qp_info->port_priv->wq,
542                                    &rmpp_recv->timeout_work,
543                                    msecs_to_jiffies(40000));
544                 rmpp_recv->newwin += window_size(agent);
545                 ack_recv(rmpp_recv, mad_recv_wc);
546                 mad_recv_wc = NULL;
547         }
548         deref_rmpp_recv(rmpp_recv);
549         return mad_recv_wc;
550 }
551
552 static int send_next_seg(struct ib_mad_send_wr_private *mad_send_wr)
553 {
554         struct ib_rmpp_mad *rmpp_mad;
555         int timeout;
556         u32 paylen = 0;
557
558         rmpp_mad = mad_send_wr->send_buf.mad;
559         ib_set_rmpp_flags(&rmpp_mad->rmpp_hdr, IB_MGMT_RMPP_FLAG_ACTIVE);
560         rmpp_mad->rmpp_hdr.seg_num = cpu_to_be32(++mad_send_wr->seg_num);
561
562         if (mad_send_wr->seg_num == 1) {
563                 rmpp_mad->rmpp_hdr.rmpp_rtime_flags |= IB_MGMT_RMPP_FLAG_FIRST;
564                 paylen = mad_send_wr->send_buf.seg_count * IB_MGMT_RMPP_DATA -
565                          mad_send_wr->pad;
566         }
567
568         if (mad_send_wr->seg_num == mad_send_wr->send_buf.seg_count) {
569                 rmpp_mad->rmpp_hdr.rmpp_rtime_flags |= IB_MGMT_RMPP_FLAG_LAST;
570                 paylen = IB_MGMT_RMPP_DATA - mad_send_wr->pad;
571         }
572         rmpp_mad->rmpp_hdr.paylen_newwin = cpu_to_be32(paylen);
573
574         /* 2 seconds for an ACK until we can find the packet lifetime */
575         timeout = mad_send_wr->send_buf.timeout_ms;
576         if (!timeout || timeout > 2000)
577                 mad_send_wr->timeout = msecs_to_jiffies(2000);
578
579         return ib_send_mad(mad_send_wr);
580 }
581
582 static void abort_send(struct ib_mad_agent_private *agent,
583                        struct ib_mad_recv_wc *mad_recv_wc, u8 rmpp_status)
584 {
585         struct ib_mad_send_wr_private *mad_send_wr;
586         struct ib_mad_send_wc wc;
587         unsigned long flags;
588
589         spin_lock_irqsave(&agent->lock, flags);
590         mad_send_wr = ib_find_send_mad(agent, mad_recv_wc);
591         if (!mad_send_wr)
592                 goto out;       /* Unmatched send */
593
594         if ((mad_send_wr->last_ack == mad_send_wr->send_buf.seg_count) ||
595             (!mad_send_wr->timeout) || (mad_send_wr->status != IB_WC_SUCCESS))
596                 goto out;       /* Send is already done */
597
598         ib_mark_mad_done(mad_send_wr);
599         spin_unlock_irqrestore(&agent->lock, flags);
600
601         wc.status = IB_WC_REM_ABORT_ERR;
602         wc.vendor_err = rmpp_status;
603         wc.send_buf = &mad_send_wr->send_buf;
604         ib_mad_complete_send_wr(mad_send_wr, &wc);
605         return;
606 out:
607         spin_unlock_irqrestore(&agent->lock, flags);
608 }
609
610 static inline void adjust_last_ack(struct ib_mad_send_wr_private *wr,
611                                    int seg_num)
612 {
613         struct list_head *list;
614
615         wr->last_ack = seg_num;
616         list = &wr->last_ack_seg->list;
617         list_for_each_entry(wr->last_ack_seg, list, list)
618                 if (wr->last_ack_seg->num == seg_num)
619                         break;
620 }
621
622 static void process_ds_ack(struct ib_mad_agent_private *agent,
623                            struct ib_mad_recv_wc *mad_recv_wc, int newwin)
624 {
625         struct mad_rmpp_recv *rmpp_recv;
626
627         rmpp_recv = find_rmpp_recv(agent, mad_recv_wc);
628         if (rmpp_recv && rmpp_recv->state == RMPP_STATE_COMPLETE)
629                 rmpp_recv->repwin = newwin;
630 }
631
632 static void process_rmpp_ack(struct ib_mad_agent_private *agent,
633                              struct ib_mad_recv_wc *mad_recv_wc)
634 {
635         struct ib_mad_send_wr_private *mad_send_wr;
636         struct ib_rmpp_mad *rmpp_mad;
637         unsigned long flags;
638         int seg_num, newwin, ret;
639
640         rmpp_mad = (struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad;
641         if (rmpp_mad->rmpp_hdr.rmpp_status) {
642                 abort_send(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BAD_STATUS);
643                 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BAD_STATUS);
644                 return;
645         }
646
647         seg_num = be32_to_cpu(rmpp_mad->rmpp_hdr.seg_num);
648         newwin = be32_to_cpu(rmpp_mad->rmpp_hdr.paylen_newwin);
649         if (newwin < seg_num) {
650                 abort_send(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_W2S);
651                 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_W2S);
652                 return;
653         }
654
655         spin_lock_irqsave(&agent->lock, flags);
656         mad_send_wr = ib_find_send_mad(agent, mad_recv_wc);
657         if (!mad_send_wr) {
658                 if (!seg_num)
659                         process_ds_ack(agent, mad_recv_wc, newwin);
660                 goto out;       /* Unmatched or DS RMPP ACK */
661         }
662
663         if ((mad_send_wr->last_ack == mad_send_wr->send_buf.seg_count) &&
664             (mad_send_wr->timeout)) {
665                 spin_unlock_irqrestore(&agent->lock, flags);
666                 ack_ds_ack(agent, mad_recv_wc);
667                 return;         /* Repeated ACK for DS RMPP transaction */
668         }
669
670         if ((mad_send_wr->last_ack == mad_send_wr->send_buf.seg_count) ||
671             (!mad_send_wr->timeout) || (mad_send_wr->status != IB_WC_SUCCESS))
672                 goto out;       /* Send is already done */
673
674         if (seg_num > mad_send_wr->send_buf.seg_count ||
675             seg_num > mad_send_wr->newwin) {
676                 spin_unlock_irqrestore(&agent->lock, flags);
677                 abort_send(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_S2B);
678                 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_S2B);
679                 return;
680         }
681
682         if (newwin < mad_send_wr->newwin || seg_num < mad_send_wr->last_ack)
683                 goto out;       /* Old ACK */
684
685         if (seg_num > mad_send_wr->last_ack) {
686                 adjust_last_ack(mad_send_wr, seg_num);
687                 mad_send_wr->retries = mad_send_wr->send_buf.retries;
688         }
689         mad_send_wr->newwin = newwin;
690         if (mad_send_wr->last_ack == mad_send_wr->send_buf.seg_count) {
691                 /* If no response is expected, the ACK completes the send */
692                 if (!mad_send_wr->send_buf.timeout_ms) {
693                         struct ib_mad_send_wc wc;
694
695                         ib_mark_mad_done(mad_send_wr);
696                         spin_unlock_irqrestore(&agent->lock, flags);
697
698                         wc.status = IB_WC_SUCCESS;
699                         wc.vendor_err = 0;
700                         wc.send_buf = &mad_send_wr->send_buf;
701                         ib_mad_complete_send_wr(mad_send_wr, &wc);
702                         return;
703                 }
704                 if (mad_send_wr->refcount == 1)
705                         ib_reset_mad_timeout(mad_send_wr,
706                                              mad_send_wr->send_buf.timeout_ms);
707                 spin_unlock_irqrestore(&agent->lock, flags);
708                 ack_ds_ack(agent, mad_recv_wc);
709                 return;
710         } else if (mad_send_wr->refcount == 1 &&
711                    mad_send_wr->seg_num < mad_send_wr->newwin &&
712                    mad_send_wr->seg_num < mad_send_wr->send_buf.seg_count) {
713                 /* Send failure will just result in a timeout/retry */
714                 ret = send_next_seg(mad_send_wr);
715                 if (ret)
716                         goto out;
717
718                 mad_send_wr->refcount++;
719                 list_move_tail(&mad_send_wr->agent_list,
720                               &mad_send_wr->mad_agent_priv->send_list);
721         }
722 out:
723         spin_unlock_irqrestore(&agent->lock, flags);
724 }
725
726 static struct ib_mad_recv_wc *
727 process_rmpp_data(struct ib_mad_agent_private *agent,
728                   struct ib_mad_recv_wc *mad_recv_wc)
729 {
730         struct ib_rmpp_hdr *rmpp_hdr;
731         u8 rmpp_status;
732
733         rmpp_hdr = &((struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad)->rmpp_hdr;
734
735         if (rmpp_hdr->rmpp_status) {
736                 rmpp_status = IB_MGMT_RMPP_STATUS_BAD_STATUS;
737                 goto bad;
738         }
739
740         if (rmpp_hdr->seg_num == __constant_htonl(1)) {
741                 if (!(ib_get_rmpp_flags(rmpp_hdr) & IB_MGMT_RMPP_FLAG_FIRST)) {
742                         rmpp_status = IB_MGMT_RMPP_STATUS_BAD_SEG;
743                         goto bad;
744                 }
745                 return start_rmpp(agent, mad_recv_wc);
746         } else {
747                 if (ib_get_rmpp_flags(rmpp_hdr) & IB_MGMT_RMPP_FLAG_FIRST) {
748                         rmpp_status = IB_MGMT_RMPP_STATUS_BAD_SEG;
749                         goto bad;
750                 }
751                 return continue_rmpp(agent, mad_recv_wc);
752         }
753 bad:
754         nack_recv(agent, mad_recv_wc, rmpp_status);
755         ib_free_recv_mad(mad_recv_wc);
756         return NULL;
757 }
758
759 static void process_rmpp_stop(struct ib_mad_agent_private *agent,
760                               struct ib_mad_recv_wc *mad_recv_wc)
761 {
762         struct ib_rmpp_mad *rmpp_mad;
763
764         rmpp_mad = (struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad;
765
766         if (rmpp_mad->rmpp_hdr.rmpp_status != IB_MGMT_RMPP_STATUS_RESX) {
767                 abort_send(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BAD_STATUS);
768                 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BAD_STATUS);
769         } else
770                 abort_send(agent, mad_recv_wc, rmpp_mad->rmpp_hdr.rmpp_status);
771 }
772
773 static void process_rmpp_abort(struct ib_mad_agent_private *agent,
774                                struct ib_mad_recv_wc *mad_recv_wc)
775 {
776         struct ib_rmpp_mad *rmpp_mad;
777
778         rmpp_mad = (struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad;
779
780         if (rmpp_mad->rmpp_hdr.rmpp_status < IB_MGMT_RMPP_STATUS_ABORT_MIN ||
781             rmpp_mad->rmpp_hdr.rmpp_status > IB_MGMT_RMPP_STATUS_ABORT_MAX) {
782                 abort_send(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BAD_STATUS);
783                 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BAD_STATUS);
784         } else
785                 abort_send(agent, mad_recv_wc, rmpp_mad->rmpp_hdr.rmpp_status);
786 }
787
788 struct ib_mad_recv_wc *
789 ib_process_rmpp_recv_wc(struct ib_mad_agent_private *agent,
790                         struct ib_mad_recv_wc *mad_recv_wc)
791 {
792         struct ib_rmpp_mad *rmpp_mad;
793
794         rmpp_mad = (struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad;
795         if (!(rmpp_mad->rmpp_hdr.rmpp_rtime_flags & IB_MGMT_RMPP_FLAG_ACTIVE))
796                 return mad_recv_wc;
797
798         if (rmpp_mad->rmpp_hdr.rmpp_version != IB_MGMT_RMPP_VERSION) {
799                 abort_send(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_UNV);
800                 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_UNV);
801                 goto out;
802         }
803
804         switch (rmpp_mad->rmpp_hdr.rmpp_type) {
805         case IB_MGMT_RMPP_TYPE_DATA:
806                 return process_rmpp_data(agent, mad_recv_wc);
807         case IB_MGMT_RMPP_TYPE_ACK:
808                 process_rmpp_ack(agent, mad_recv_wc);
809                 break;
810         case IB_MGMT_RMPP_TYPE_STOP:
811                 process_rmpp_stop(agent, mad_recv_wc);
812                 break;
813         case IB_MGMT_RMPP_TYPE_ABORT:
814                 process_rmpp_abort(agent, mad_recv_wc);
815                 break;
816         default:
817                 abort_send(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BADT);
818                 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BADT);
819                 break;
820         }
821 out:
822         ib_free_recv_mad(mad_recv_wc);
823         return NULL;
824 }
825
826 static int init_newwin(struct ib_mad_send_wr_private *mad_send_wr)
827 {
828         struct ib_mad_agent_private *agent = mad_send_wr->mad_agent_priv;
829         struct ib_mad_hdr *mad_hdr = mad_send_wr->send_buf.mad;
830         struct mad_rmpp_recv *rmpp_recv;
831         struct ib_ah_attr ah_attr;
832         unsigned long flags;
833         int newwin = 1;
834
835         if (!(mad_hdr->method & IB_MGMT_METHOD_RESP))
836                 goto out;
837
838         spin_lock_irqsave(&agent->lock, flags);
839         list_for_each_entry(rmpp_recv, &agent->rmpp_list, list) {
840                 if (rmpp_recv->tid != mad_hdr->tid ||
841                     rmpp_recv->mgmt_class != mad_hdr->mgmt_class ||
842                     rmpp_recv->class_version != mad_hdr->class_version ||
843                     (rmpp_recv->method & IB_MGMT_METHOD_RESP))
844                         continue;
845
846                 if (ib_query_ah(mad_send_wr->send_buf.ah, &ah_attr))
847                         continue;
848
849                 if (rmpp_recv->slid == ah_attr.dlid) {
850                         newwin = rmpp_recv->repwin;
851                         break;
852                 }
853         }
854         spin_unlock_irqrestore(&agent->lock, flags);
855 out:
856         return newwin;
857 }
858
859 int ib_send_rmpp_mad(struct ib_mad_send_wr_private *mad_send_wr)
860 {
861         struct ib_rmpp_mad *rmpp_mad;
862         int ret;
863
864         rmpp_mad = mad_send_wr->send_buf.mad;
865         if (!(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
866               IB_MGMT_RMPP_FLAG_ACTIVE))
867                 return IB_RMPP_RESULT_UNHANDLED;
868
869         if (rmpp_mad->rmpp_hdr.rmpp_type != IB_MGMT_RMPP_TYPE_DATA) {
870                 mad_send_wr->seg_num = 1;
871                 return IB_RMPP_RESULT_INTERNAL;
872         }
873
874         mad_send_wr->newwin = init_newwin(mad_send_wr);
875
876         /* We need to wait for the final ACK even if there isn't a response */
877         mad_send_wr->refcount += (mad_send_wr->timeout == 0);
878         ret = send_next_seg(mad_send_wr);
879         if (!ret)
880                 return IB_RMPP_RESULT_CONSUMED;
881         return ret;
882 }
883
884 int ib_process_rmpp_send_wc(struct ib_mad_send_wr_private *mad_send_wr,
885                             struct ib_mad_send_wc *mad_send_wc)
886 {
887         struct ib_rmpp_mad *rmpp_mad;
888         int ret;
889
890         rmpp_mad = mad_send_wr->send_buf.mad;
891         if (!(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
892               IB_MGMT_RMPP_FLAG_ACTIVE))
893                 return IB_RMPP_RESULT_UNHANDLED; /* RMPP not active */
894
895         if (rmpp_mad->rmpp_hdr.rmpp_type != IB_MGMT_RMPP_TYPE_DATA)
896                 return IB_RMPP_RESULT_INTERNAL;  /* ACK, STOP, or ABORT */
897
898         if (mad_send_wc->status != IB_WC_SUCCESS ||
899             mad_send_wr->status != IB_WC_SUCCESS)
900                 return IB_RMPP_RESULT_PROCESSED; /* Canceled or send error */
901
902         if (!mad_send_wr->timeout)
903                 return IB_RMPP_RESULT_PROCESSED; /* Response received */
904
905         if (mad_send_wr->last_ack == mad_send_wr->send_buf.seg_count) {
906                 mad_send_wr->timeout =
907                         msecs_to_jiffies(mad_send_wr->send_buf.timeout_ms);
908                 return IB_RMPP_RESULT_PROCESSED; /* Send done */
909         }
910
911         if (mad_send_wr->seg_num == mad_send_wr->newwin ||
912             mad_send_wr->seg_num == mad_send_wr->send_buf.seg_count)
913                 return IB_RMPP_RESULT_PROCESSED; /* Wait for ACK */
914
915         ret = send_next_seg(mad_send_wr);
916         if (ret) {
917                 mad_send_wc->status = IB_WC_GENERAL_ERR;
918                 return IB_RMPP_RESULT_PROCESSED;
919         }
920         return IB_RMPP_RESULT_CONSUMED;
921 }
922
923 int ib_retry_rmpp(struct ib_mad_send_wr_private *mad_send_wr)
924 {
925         struct ib_rmpp_mad *rmpp_mad;
926         int ret;
927
928         rmpp_mad = mad_send_wr->send_buf.mad;
929         if (!(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
930               IB_MGMT_RMPP_FLAG_ACTIVE))
931                 return IB_RMPP_RESULT_UNHANDLED; /* RMPP not active */
932
933         if (mad_send_wr->last_ack == mad_send_wr->send_buf.seg_count)
934                 return IB_RMPP_RESULT_PROCESSED;
935
936         mad_send_wr->seg_num = mad_send_wr->last_ack;
937         mad_send_wr->cur_seg = mad_send_wr->last_ack_seg;
938
939         ret = send_next_seg(mad_send_wr);
940         if (ret)
941                 return IB_RMPP_RESULT_PROCESSED;
942
943         return IB_RMPP_RESULT_CONSUMED;
944 }