Merge branch 'mv-merge'
[linux-2.6] / drivers / infiniband / core / mad_rmpp.c
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
41 enum rmpp_state {
42         RMPP_STATE_ACTIVE,
43         RMPP_STATE_TIMEOUT,
44         RMPP_STATE_COMPLETE
45 };
46
47 struct mad_rmpp_recv {
48         struct ib_mad_agent_private *agent;
49         struct list_head list;
50         struct work_struct timeout_work;
51         struct work_struct cleanup_work;
52         wait_queue_head_t wait;
53         enum rmpp_state state;
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
64         __be64 tid;
65         u32 src_qp;
66         u16 slid;
67         u8 mgmt_class;
68         u8 class_version;
69         u8 method;
70 };
71
72 static 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
80 void 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
103 static int data_offset(u8 mgmt_class)
104 {
105         if (mgmt_class == IB_MGMT_CLASS_SUBN_ADM)
106                 return IB_MGMT_SA_HDR;
107         else if ((mgmt_class >= IB_MGMT_CLASS_VENDOR_RANGE2_START) &&
108                  (mgmt_class <= IB_MGMT_CLASS_VENDOR_RANGE2_END))
109                 return IB_MGMT_VENDOR_HDR;
110         else
111                 return IB_MGMT_RMPP_HDR;
112 }
113
114 static void format_ack(struct ib_mad_send_buf *msg,
115                        struct ib_rmpp_mad *data,
116                        struct mad_rmpp_recv *rmpp_recv)
117 {
118         struct ib_rmpp_mad *ack = msg->mad;
119         unsigned long flags;
120
121         memcpy(ack, &data->mad_hdr, msg->hdr_len);
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
134 static 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;
138         int ret, hdr_len;
139
140         hdr_len = data_offset(recv_wc->recv_buf.mad->mad_hdr.mgmt_class);
141         msg = ib_create_send_mad(&rmpp_recv->agent->agent, recv_wc->wc->src_qp,
142                                  recv_wc->wc->pkey_index, 1, hdr_len,
143                                  0, GFP_KERNEL);
144         if (!msg)
145                 return;
146
147         format_ack(msg, (struct ib_rmpp_mad *) recv_wc->recv_buf.mad, rmpp_recv);
148         msg->ah = rmpp_recv->ah;
149         ret = ib_post_send_mad(msg, NULL);
150         if (ret)
151                 ib_free_send_mad(msg);
152 }
153
154 static struct ib_mad_send_buf *alloc_response_msg(struct ib_mad_agent *agent,
155                                                   struct ib_mad_recv_wc *recv_wc)
156 {
157         struct ib_mad_send_buf *msg;
158         struct ib_ah *ah;
159         int hdr_len;
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))
164                 return (void *) ah;
165
166         hdr_len = data_offset(recv_wc->recv_buf.mad->mad_hdr.mgmt_class);
167         msg = ib_create_send_mad(agent, recv_wc->wc->src_qp,
168                                  recv_wc->wc->pkey_index, 1,
169                                  hdr_len, 0, GFP_KERNEL);
170         if (IS_ERR(msg))
171                 ib_destroy_ah(ah);
172         else
173                 msg->ah = ah;
174
175         return msg;
176 }
177
178 void ib_rmpp_send_handler(struct ib_mad_send_wc *mad_send_wc)
179 {
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);
185 }
186
187 static 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;
192         int ret;
193
194         msg = alloc_response_msg(&agent->agent, recv_wc);
195         if (IS_ERR(msg))
196                 return;
197
198         rmpp_mad = msg->mad;
199         memcpy(rmpp_mad, recv_wc->recv_buf.mad, msg->hdr_len);
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
209         ret = ib_post_send_mad(msg, NULL);
210         if (ret) {
211                 ib_destroy_ah(msg->ah);
212                 ib_free_send_mad(msg);
213         }
214 }
215
216 static 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
231         rmpp_wc = rmpp_recv->rmpp_wc;
232         nack_recv(rmpp_recv->agent, rmpp_wc, IB_MGMT_RMPP_STATUS_T2L);
233         destroy_rmpp_recv(rmpp_recv);
234         ib_free_recv_mad(rmpp_wc);
235 }
236
237 static 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
248 static struct mad_rmpp_recv *
249 create_rmpp_recv(struct ib_mad_agent_private *agent,
250                  struct ib_mad_recv_wc *mad_recv_wc)
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
289 error:  kfree(rmpp_recv);
290         return NULL;
291 }
292
293 static 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
299 static struct mad_rmpp_recv *
300 find_rmpp_recv(struct ib_mad_agent_private *agent,
301                struct ib_mad_recv_wc *mad_recv_wc)
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
318 static struct mad_rmpp_recv *
319 acquire_rmpp_recv(struct ib_mad_agent_private *agent,
320                   struct ib_mad_recv_wc *mad_recv_wc)
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
333 static struct mad_rmpp_recv *
334 insert_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
346 static 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
354 static 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
362 static 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
371 static 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
376 static 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
392 static 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
404 static 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;
413         pad = IB_MGMT_RMPP_DATA - be32_to_cpu(rmpp_mad->rmpp_hdr.paylen_newwin);
414         if (pad > IB_MGMT_RMPP_DATA || pad < 0)
415                 pad = 0;
416
417         return hdr_size + rmpp_recv->seg_num * data_size - pad;
418 }
419
420 static 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
436 static struct ib_mad_recv_wc *
437 continue_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);
485 out:
486         deref_rmpp_recv(rmpp_recv);
487         return done_wc;
488
489 drop3:  spin_unlock_irqrestore(&rmpp_recv->lock, flags);
490 drop2:  deref_rmpp_recv(rmpp_recv);
491 drop1:  ib_free_recv_mad(mad_recv_wc);
492         return NULL;
493 }
494
495 static struct ib_mad_recv_wc *
496 start_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
535 static int send_next_seg(struct ib_mad_send_wr_private *mad_send_wr)
536 {
537         struct ib_rmpp_mad *rmpp_mad;
538         int timeout;
539         u32 paylen = 0;
540
541         rmpp_mad = mad_send_wr->send_buf.mad;
542         ib_set_rmpp_flags(&rmpp_mad->rmpp_hdr, IB_MGMT_RMPP_FLAG_ACTIVE);
543         rmpp_mad->rmpp_hdr.seg_num = cpu_to_be32(++mad_send_wr->seg_num);
544
545         if (mad_send_wr->seg_num == 1) {
546                 rmpp_mad->rmpp_hdr.rmpp_rtime_flags |= IB_MGMT_RMPP_FLAG_FIRST;
547                 paylen = mad_send_wr->send_buf.seg_count * IB_MGMT_RMPP_DATA -
548                          mad_send_wr->pad;
549         }
550
551         if (mad_send_wr->seg_num == mad_send_wr->send_buf.seg_count) {
552                 rmpp_mad->rmpp_hdr.rmpp_rtime_flags |= IB_MGMT_RMPP_FLAG_LAST;
553                 paylen = IB_MGMT_RMPP_DATA - mad_send_wr->pad;
554         }
555         rmpp_mad->rmpp_hdr.paylen_newwin = cpu_to_be32(paylen);
556
557         /* 2 seconds for an ACK until we can find the packet lifetime */
558         timeout = mad_send_wr->send_buf.timeout_ms;
559         if (!timeout || timeout > 2000)
560                 mad_send_wr->timeout = msecs_to_jiffies(2000);
561
562         return ib_send_mad(mad_send_wr);
563 }
564
565 static void abort_send(struct ib_mad_agent_private *agent, __be64 tid,
566                        u8 rmpp_status)
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);
573         mad_send_wr = ib_find_send_mad(agent, tid);
574         if (!mad_send_wr)
575                 goto out;       /* Unmatched send */
576
577         if ((mad_send_wr->last_ack == mad_send_wr->send_buf.seg_count) ||
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;
586         wc.send_buf = &mad_send_wr->send_buf;
587         ib_mad_complete_send_wr(mad_send_wr, &wc);
588         return;
589 out:
590         spin_unlock_irqrestore(&agent->lock, flags);
591 }
592
593 static 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
605 static 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;
614         if (rmpp_mad->rmpp_hdr.rmpp_status) {
615                 abort_send(agent, rmpp_mad->mad_hdr.tid,
616                            IB_MGMT_RMPP_STATUS_BAD_STATUS);
617                 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BAD_STATUS);
618                 return;
619         }
620
621         seg_num = be32_to_cpu(rmpp_mad->rmpp_hdr.seg_num);
622         newwin = be32_to_cpu(rmpp_mad->rmpp_hdr.paylen_newwin);
623         if (newwin < seg_num) {
624                 abort_send(agent, rmpp_mad->mad_hdr.tid,
625                            IB_MGMT_RMPP_STATUS_W2S);
626                 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_W2S);
627                 return;
628         }
629
630         spin_lock_irqsave(&agent->lock, flags);
631         mad_send_wr = ib_find_send_mad(agent, rmpp_mad->mad_hdr.tid);
632         if (!mad_send_wr)
633                 goto out;       /* Unmatched ACK */
634
635         if ((mad_send_wr->last_ack == mad_send_wr->send_buf.seg_count) ||
636             (!mad_send_wr->timeout) || (mad_send_wr->status != IB_WC_SUCCESS))
637                 goto out;       /* Send is already done */
638
639         if (seg_num > mad_send_wr->send_buf.seg_count ||
640             seg_num > mad_send_wr->newwin) {
641                 spin_unlock_irqrestore(&agent->lock, flags);
642                 abort_send(agent, rmpp_mad->mad_hdr.tid,
643                            IB_MGMT_RMPP_STATUS_S2B);
644                 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_S2B);
645                 return;
646         }
647
648         if (newwin < mad_send_wr->newwin || seg_num < mad_send_wr->last_ack)
649                 goto out;       /* Old ACK */
650
651         if (seg_num > mad_send_wr->last_ack) {
652                 adjust_last_ack(mad_send_wr, seg_num);
653                 mad_send_wr->retries = mad_send_wr->send_buf.retries;
654         }
655         mad_send_wr->newwin = newwin;
656         if (mad_send_wr->last_ack == mad_send_wr->send_buf.seg_count) {
657                 /* If no response is expected, the ACK completes the send */
658                 if (!mad_send_wr->send_buf.timeout_ms) {
659                         struct ib_mad_send_wc wc;
660
661                         ib_mark_mad_done(mad_send_wr);
662                         spin_unlock_irqrestore(&agent->lock, flags);
663
664                         wc.status = IB_WC_SUCCESS;
665                         wc.vendor_err = 0;
666                         wc.send_buf = &mad_send_wr->send_buf;
667                         ib_mad_complete_send_wr(mad_send_wr, &wc);
668                         return;
669                 }
670                 if (mad_send_wr->refcount == 1)
671                         ib_reset_mad_timeout(mad_send_wr,
672                                              mad_send_wr->send_buf.timeout_ms);
673         } else if (mad_send_wr->refcount == 1 &&
674                    mad_send_wr->seg_num < mad_send_wr->newwin &&
675                    mad_send_wr->seg_num < mad_send_wr->send_buf.seg_count) {
676                 /* Send failure will just result in a timeout/retry */
677                 ret = send_next_seg(mad_send_wr);
678                 if (ret)
679                         goto out;
680
681                 mad_send_wr->refcount++;
682                 list_del(&mad_send_wr->agent_list);
683                 list_add_tail(&mad_send_wr->agent_list,
684                               &mad_send_wr->mad_agent_priv->send_list);
685         }
686 out:
687         spin_unlock_irqrestore(&agent->lock, flags);
688 }
689
690 static struct ib_mad_recv_wc *
691 process_rmpp_data(struct ib_mad_agent_private *agent,
692                   struct ib_mad_recv_wc *mad_recv_wc)
693 {
694         struct ib_rmpp_hdr *rmpp_hdr;
695         u8 rmpp_status;
696
697         rmpp_hdr = &((struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad)->rmpp_hdr;
698
699         if (rmpp_hdr->rmpp_status) {
700                 rmpp_status = IB_MGMT_RMPP_STATUS_BAD_STATUS;
701                 goto bad;
702         }
703
704         if (rmpp_hdr->seg_num == __constant_htonl(1)) {
705                 if (!(ib_get_rmpp_flags(rmpp_hdr) & IB_MGMT_RMPP_FLAG_FIRST)) {
706                         rmpp_status = IB_MGMT_RMPP_STATUS_BAD_SEG;
707                         goto bad;
708                 }
709                 return start_rmpp(agent, mad_recv_wc);
710         } else {
711                 if (ib_get_rmpp_flags(rmpp_hdr) & IB_MGMT_RMPP_FLAG_FIRST) {
712                         rmpp_status = IB_MGMT_RMPP_STATUS_BAD_SEG;
713                         goto bad;
714                 }
715                 return continue_rmpp(agent, mad_recv_wc);
716         }
717 bad:
718         nack_recv(agent, mad_recv_wc, rmpp_status);
719         ib_free_recv_mad(mad_recv_wc);
720         return NULL;
721 }
722
723 static void process_rmpp_stop(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_RESX) {
731                 abort_send(agent, rmpp_mad->mad_hdr.tid,
732                            IB_MGMT_RMPP_STATUS_BAD_STATUS);
733                 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BAD_STATUS);
734         } else
735                 abort_send(agent, rmpp_mad->mad_hdr.tid,
736                            rmpp_mad->rmpp_hdr.rmpp_status);
737 }
738
739 static void process_rmpp_abort(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
746         if (rmpp_mad->rmpp_hdr.rmpp_status < IB_MGMT_RMPP_STATUS_ABORT_MIN ||
747             rmpp_mad->rmpp_hdr.rmpp_status > IB_MGMT_RMPP_STATUS_ABORT_MAX) {
748                 abort_send(agent, rmpp_mad->mad_hdr.tid,
749                            IB_MGMT_RMPP_STATUS_BAD_STATUS);
750                 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BAD_STATUS);
751         } else
752                 abort_send(agent, rmpp_mad->mad_hdr.tid,
753                            rmpp_mad->rmpp_hdr.rmpp_status);
754 }
755
756 struct ib_mad_recv_wc *
757 ib_process_rmpp_recv_wc(struct ib_mad_agent_private *agent,
758                         struct ib_mad_recv_wc *mad_recv_wc)
759 {
760         struct ib_rmpp_mad *rmpp_mad;
761
762         rmpp_mad = (struct ib_rmpp_mad *)mad_recv_wc->recv_buf.mad;
763         if (!(rmpp_mad->rmpp_hdr.rmpp_rtime_flags & IB_MGMT_RMPP_FLAG_ACTIVE))
764                 return mad_recv_wc;
765
766         if (rmpp_mad->rmpp_hdr.rmpp_version != IB_MGMT_RMPP_VERSION) {
767                 abort_send(agent, rmpp_mad->mad_hdr.tid,
768                            IB_MGMT_RMPP_STATUS_UNV);
769                 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_UNV);
770                 goto out;
771         }
772
773         switch (rmpp_mad->rmpp_hdr.rmpp_type) {
774         case IB_MGMT_RMPP_TYPE_DATA:
775                 return process_rmpp_data(agent, mad_recv_wc);
776         case IB_MGMT_RMPP_TYPE_ACK:
777                 process_rmpp_ack(agent, mad_recv_wc);
778                 break;
779         case IB_MGMT_RMPP_TYPE_STOP:
780                 process_rmpp_stop(agent, mad_recv_wc);
781                 break;
782         case IB_MGMT_RMPP_TYPE_ABORT:
783                 process_rmpp_abort(agent, mad_recv_wc);
784                 break;
785         default:
786                 abort_send(agent, rmpp_mad->mad_hdr.tid,
787                            IB_MGMT_RMPP_STATUS_BADT);
788                 nack_recv(agent, mad_recv_wc, IB_MGMT_RMPP_STATUS_BADT);
789                 break;
790         }
791 out:
792         ib_free_recv_mad(mad_recv_wc);
793         return NULL;
794 }
795
796 int ib_send_rmpp_mad(struct ib_mad_send_wr_private *mad_send_wr)
797 {
798         struct ib_rmpp_mad *rmpp_mad;
799         int ret;
800
801         rmpp_mad = mad_send_wr->send_buf.mad;
802         if (!(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
803               IB_MGMT_RMPP_FLAG_ACTIVE))
804                 return IB_RMPP_RESULT_UNHANDLED;
805
806         if (rmpp_mad->rmpp_hdr.rmpp_type != IB_MGMT_RMPP_TYPE_DATA) {
807                 mad_send_wr->seg_num = 1;
808                 return IB_RMPP_RESULT_INTERNAL;
809         }
810
811         mad_send_wr->newwin = 1;
812
813         /* We need to wait for the final ACK even if there isn't a response */
814         mad_send_wr->refcount += (mad_send_wr->timeout == 0);
815         ret = send_next_seg(mad_send_wr);
816         if (!ret)
817                 return IB_RMPP_RESULT_CONSUMED;
818         return ret;
819 }
820
821 int ib_process_rmpp_send_wc(struct ib_mad_send_wr_private *mad_send_wr,
822                             struct ib_mad_send_wc *mad_send_wc)
823 {
824         struct ib_rmpp_mad *rmpp_mad;
825         int ret;
826
827         rmpp_mad = mad_send_wr->send_buf.mad;
828         if (!(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
829               IB_MGMT_RMPP_FLAG_ACTIVE))
830                 return IB_RMPP_RESULT_UNHANDLED; /* RMPP not active */
831
832         if (rmpp_mad->rmpp_hdr.rmpp_type != IB_MGMT_RMPP_TYPE_DATA)
833                 return IB_RMPP_RESULT_INTERNAL;  /* ACK, STOP, or ABORT */
834
835         if (mad_send_wc->status != IB_WC_SUCCESS ||
836             mad_send_wr->status != IB_WC_SUCCESS)
837                 return IB_RMPP_RESULT_PROCESSED; /* Canceled or send error */
838
839         if (!mad_send_wr->timeout)
840                 return IB_RMPP_RESULT_PROCESSED; /* Response received */
841
842         if (mad_send_wr->last_ack == mad_send_wr->send_buf.seg_count) {
843                 mad_send_wr->timeout =
844                         msecs_to_jiffies(mad_send_wr->send_buf.timeout_ms);
845                 return IB_RMPP_RESULT_PROCESSED; /* Send done */
846         }
847
848         if (mad_send_wr->seg_num == mad_send_wr->newwin ||
849             mad_send_wr->seg_num == mad_send_wr->send_buf.seg_count)
850                 return IB_RMPP_RESULT_PROCESSED; /* Wait for ACK */
851
852         ret = send_next_seg(mad_send_wr);
853         if (ret) {
854                 mad_send_wc->status = IB_WC_GENERAL_ERR;
855                 return IB_RMPP_RESULT_PROCESSED;
856         }
857         return IB_RMPP_RESULT_CONSUMED;
858 }
859
860 int ib_retry_rmpp(struct ib_mad_send_wr_private *mad_send_wr)
861 {
862         struct ib_rmpp_mad *rmpp_mad;
863         int ret;
864
865         rmpp_mad = mad_send_wr->send_buf.mad;
866         if (!(ib_get_rmpp_flags(&rmpp_mad->rmpp_hdr) &
867               IB_MGMT_RMPP_FLAG_ACTIVE))
868                 return IB_RMPP_RESULT_UNHANDLED; /* RMPP not active */
869
870         if (mad_send_wr->last_ack == mad_send_wr->send_buf.seg_count)
871                 return IB_RMPP_RESULT_PROCESSED;
872
873         mad_send_wr->seg_num = mad_send_wr->last_ack;
874         mad_send_wr->cur_seg = mad_send_wr->last_ack_seg;
875
876         ret = send_next_seg(mad_send_wr);
877         if (ret)
878                 return IB_RMPP_RESULT_PROCESSED;
879
880         return IB_RMPP_RESULT_CONSUMED;
881 }