IB/ipath: Fix possible data corruption if multiple SGEs used for receive
[safe/jmp/linux-2.6] / drivers / infiniband / hw / ipath / ipath_ud.c
1 /*
2  * Copyright (c) 2006 QLogic, Inc. All rights reserved.
3  * Copyright (c) 2005, 2006 PathScale, 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
34 #include <rdma/ib_smi.h>
35
36 #include "ipath_verbs.h"
37 #include "ipath_kernel.h"
38
39 static int init_sge(struct ipath_qp *qp, struct ipath_rwqe *wqe,
40                     u32 *lengthp, struct ipath_sge_state *ss)
41 {
42         int user = to_ipd(qp->ibqp.pd)->user;
43         int i, j, ret;
44         struct ib_wc wc;
45
46         *lengthp = 0;
47         for (i = j = 0; i < wqe->num_sge; i++) {
48                 if (wqe->sg_list[i].length == 0)
49                         continue;
50                 /* Check LKEY */
51                 if ((user && wqe->sg_list[i].lkey == 0) ||
52                     !ipath_lkey_ok(qp, j ? &ss->sg_list[j - 1] : &ss->sge,
53                                    &wqe->sg_list[i], IB_ACCESS_LOCAL_WRITE))
54                         goto bad_lkey;
55                 *lengthp += wqe->sg_list[i].length;
56                 j++;
57         }
58         ss->num_sge = j;
59         ret = 1;
60         goto bail;
61
62 bad_lkey:
63         wc.wr_id = wqe->wr_id;
64         wc.status = IB_WC_LOC_PROT_ERR;
65         wc.opcode = IB_WC_RECV;
66         wc.vendor_err = 0;
67         wc.byte_len = 0;
68         wc.imm_data = 0;
69         wc.qp = &qp->ibqp;
70         wc.src_qp = 0;
71         wc.wc_flags = 0;
72         wc.pkey_index = 0;
73         wc.slid = 0;
74         wc.sl = 0;
75         wc.dlid_path_bits = 0;
76         wc.port_num = 0;
77         /* Signal solicited completion event. */
78         ipath_cq_enter(to_icq(qp->ibqp.recv_cq), &wc, 1);
79         ret = 0;
80 bail:
81         return ret;
82 }
83
84 /**
85  * ipath_ud_loopback - handle send on loopback QPs
86  * @sqp: the QP
87  * @ss: the SGE state
88  * @length: the length of the data to send
89  * @wr: the work request
90  * @wc: the work completion entry
91  *
92  * This is called from ipath_post_ud_send() to forward a WQE addressed
93  * to the same HCA.
94  * Note that the receive interrupt handler may be calling ipath_ud_rcv()
95  * while this is being called.
96  */
97 static void ipath_ud_loopback(struct ipath_qp *sqp,
98                               struct ipath_sge_state *ss,
99                               u32 length, struct ib_send_wr *wr,
100                               struct ib_wc *wc)
101 {
102         struct ipath_ibdev *dev = to_idev(sqp->ibqp.device);
103         struct ipath_qp *qp;
104         struct ib_ah_attr *ah_attr;
105         unsigned long flags;
106         struct ipath_rq *rq;
107         struct ipath_srq *srq;
108         struct ipath_sge_state rsge;
109         struct ipath_sge *sge;
110         struct ipath_rwq *wq;
111         struct ipath_rwqe *wqe;
112         void (*handler)(struct ib_event *, void *);
113         u32 tail;
114         u32 rlen;
115
116         qp = ipath_lookup_qpn(&dev->qp_table, wr->wr.ud.remote_qpn);
117         if (!qp)
118                 return;
119
120         /*
121          * Check that the qkey matches (except for QP0, see 9.6.1.4.1).
122          * Qkeys with the high order bit set mean use the
123          * qkey from the QP context instead of the WR (see 10.2.5).
124          */
125         if (unlikely(qp->ibqp.qp_num &&
126                      ((int) wr->wr.ud.remote_qkey < 0
127                       ? qp->qkey : wr->wr.ud.remote_qkey) != qp->qkey)) {
128                 /* XXX OK to lose a count once in a while. */
129                 dev->qkey_violations++;
130                 dev->n_pkt_drops++;
131                 goto done;
132         }
133
134         /*
135          * A GRH is expected to preceed the data even if not
136          * present on the wire.
137          */
138         wc->byte_len = length + sizeof(struct ib_grh);
139
140         if (wr->opcode == IB_WR_SEND_WITH_IMM) {
141                 wc->wc_flags = IB_WC_WITH_IMM;
142                 wc->imm_data = wr->imm_data;
143         } else {
144                 wc->wc_flags = 0;
145                 wc->imm_data = 0;
146         }
147
148         if (wr->num_sge > 1) {
149                 rsge.sg_list = kmalloc((wr->num_sge - 1) *
150                                         sizeof(struct ipath_sge),
151                                        GFP_ATOMIC);
152         } else
153                 rsge.sg_list = NULL;
154
155         /*
156          * Get the next work request entry to find where to put the data.
157          * Note that it is safe to drop the lock after changing rq->tail
158          * since ipath_post_receive() won't fill the empty slot.
159          */
160         if (qp->ibqp.srq) {
161                 srq = to_isrq(qp->ibqp.srq);
162                 handler = srq->ibsrq.event_handler;
163                 rq = &srq->rq;
164         } else {
165                 srq = NULL;
166                 handler = NULL;
167                 rq = &qp->r_rq;
168         }
169
170         spin_lock_irqsave(&rq->lock, flags);
171         wq = rq->wq;
172         tail = wq->tail;
173         while (1) {
174                 if (unlikely(tail == wq->head)) {
175                         spin_unlock_irqrestore(&rq->lock, flags);
176                         dev->n_pkt_drops++;
177                         goto bail_sge;
178                 }
179                 wqe = get_rwqe_ptr(rq, tail);
180                 if (++tail >= rq->size)
181                         tail = 0;
182                 if (init_sge(qp, wqe, &rlen, &rsge))
183                         break;
184                 wq->tail = tail;
185         }
186         /* Silently drop packets which are too big. */
187         if (wc->byte_len > rlen) {
188                 spin_unlock_irqrestore(&rq->lock, flags);
189                 dev->n_pkt_drops++;
190                 goto bail_sge;
191         }
192         wq->tail = tail;
193         wc->wr_id = wqe->wr_id;
194         if (handler) {
195                 u32 n;
196
197                 /*
198                  * validate head pointer value and compute
199                  * the number of remaining WQEs.
200                  */
201                 n = wq->head;
202                 if (n >= rq->size)
203                         n = 0;
204                 if (n < tail)
205                         n += rq->size - tail;
206                 else
207                         n -= tail;
208                 if (n < srq->limit) {
209                         struct ib_event ev;
210
211                         srq->limit = 0;
212                         spin_unlock_irqrestore(&rq->lock, flags);
213                         ev.device = qp->ibqp.device;
214                         ev.element.srq = qp->ibqp.srq;
215                         ev.event = IB_EVENT_SRQ_LIMIT_REACHED;
216                         handler(&ev, srq->ibsrq.srq_context);
217                 } else
218                         spin_unlock_irqrestore(&rq->lock, flags);
219         } else
220                 spin_unlock_irqrestore(&rq->lock, flags);
221
222         ah_attr = &to_iah(wr->wr.ud.ah)->attr;
223         if (ah_attr->ah_flags & IB_AH_GRH) {
224                 ipath_copy_sge(&rsge, &ah_attr->grh, sizeof(struct ib_grh));
225                 wc->wc_flags |= IB_WC_GRH;
226         } else
227                 ipath_skip_sge(&rsge, sizeof(struct ib_grh));
228         sge = &ss->sge;
229         while (length) {
230                 u32 len = sge->length;
231
232                 if (len > length)
233                         len = length;
234                 if (len > sge->sge_length)
235                         len = sge->sge_length;
236                 BUG_ON(len == 0);
237                 ipath_copy_sge(&rsge, sge->vaddr, len);
238                 sge->vaddr += len;
239                 sge->length -= len;
240                 sge->sge_length -= len;
241                 if (sge->sge_length == 0) {
242                         if (--ss->num_sge)
243                                 *sge = *ss->sg_list++;
244                 } else if (sge->length == 0 && sge->mr != NULL) {
245                         if (++sge->n >= IPATH_SEGSZ) {
246                                 if (++sge->m >= sge->mr->mapsz)
247                                         break;
248                                 sge->n = 0;
249                         }
250                         sge->vaddr =
251                                 sge->mr->map[sge->m]->segs[sge->n].vaddr;
252                         sge->length =
253                                 sge->mr->map[sge->m]->segs[sge->n].length;
254                 }
255                 length -= len;
256         }
257         wc->status = IB_WC_SUCCESS;
258         wc->opcode = IB_WC_RECV;
259         wc->vendor_err = 0;
260         wc->qp = &qp->ibqp;
261         wc->src_qp = sqp->ibqp.qp_num;
262         /* XXX do we know which pkey matched? Only needed for GSI. */
263         wc->pkey_index = 0;
264         wc->slid = dev->dd->ipath_lid |
265                 (ah_attr->src_path_bits &
266                  ((1 << (dev->mkeyprot_resv_lmc & 7)) - 1));
267         wc->sl = ah_attr->sl;
268         wc->dlid_path_bits =
269                 ah_attr->dlid & ((1 << (dev->mkeyprot_resv_lmc & 7)) - 1);
270         /* Signal completion event if the solicited bit is set. */
271         ipath_cq_enter(to_icq(qp->ibqp.recv_cq), wc,
272                        wr->send_flags & IB_SEND_SOLICITED);
273
274 bail_sge:
275         kfree(rsge.sg_list);
276 done:
277         if (atomic_dec_and_test(&qp->refcount))
278                 wake_up(&qp->wait);
279 }
280
281 /**
282  * ipath_post_ud_send - post a UD send on QP
283  * @qp: the QP
284  * @wr: the work request
285  *
286  * Note that we actually send the data as it is posted instead of putting
287  * the request into a ring buffer.  If we wanted to use a ring buffer,
288  * we would need to save a reference to the destination address in the SWQE.
289  */
290 int ipath_post_ud_send(struct ipath_qp *qp, struct ib_send_wr *wr)
291 {
292         struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
293         struct ipath_other_headers *ohdr;
294         struct ib_ah_attr *ah_attr;
295         struct ipath_sge_state ss;
296         struct ipath_sge *sg_list;
297         struct ib_wc wc;
298         u32 hwords;
299         u32 nwords;
300         u32 len;
301         u32 extra_bytes;
302         u32 bth0;
303         u16 lrh0;
304         u16 lid;
305         int i;
306         int ret;
307
308         if (!(ib_ipath_state_ops[qp->state] & IPATH_PROCESS_SEND_OK)) {
309                 ret = 0;
310                 goto bail;
311         }
312
313         if (wr->wr.ud.ah->pd != qp->ibqp.pd) {
314                 ret = -EPERM;
315                 goto bail;
316         }
317
318         /* IB spec says that num_sge == 0 is OK. */
319         if (wr->num_sge > qp->s_max_sge) {
320                 ret = -EINVAL;
321                 goto bail;
322         }
323
324         if (wr->num_sge > 1) {
325                 sg_list = kmalloc((qp->s_max_sge - 1) * sizeof(*sg_list),
326                                   GFP_ATOMIC);
327                 if (!sg_list) {
328                         ret = -ENOMEM;
329                         goto bail;
330                 }
331         } else
332                 sg_list = NULL;
333
334         /* Check the buffer to send. */
335         ss.sg_list = sg_list;
336         ss.sge.mr = NULL;
337         ss.sge.vaddr = NULL;
338         ss.sge.length = 0;
339         ss.sge.sge_length = 0;
340         ss.num_sge = 0;
341         len = 0;
342         for (i = 0; i < wr->num_sge; i++) {
343                 /* Check LKEY */
344                 if (to_ipd(qp->ibqp.pd)->user && wr->sg_list[i].lkey == 0) {
345                         ret = -EINVAL;
346                         goto bail;
347                 }
348
349                 if (wr->sg_list[i].length == 0)
350                         continue;
351                 if (!ipath_lkey_ok(qp, ss.num_sge ?
352                                    sg_list + ss.num_sge - 1 : &ss.sge,
353                                    &wr->sg_list[i], 0)) {
354                         ret = -EINVAL;
355                         goto bail;
356                 }
357                 len += wr->sg_list[i].length;
358                 ss.num_sge++;
359         }
360         /* Check for invalid packet size. */
361         if (len > dev->dd->ipath_ibmtu) {
362                 ret = -EINVAL;
363                 goto bail;
364         }
365         extra_bytes = (4 - len) & 3;
366         nwords = (len + extra_bytes) >> 2;
367
368         /* Construct the header. */
369         ah_attr = &to_iah(wr->wr.ud.ah)->attr;
370         if (ah_attr->dlid == 0) {
371                 ret = -EINVAL;
372                 goto bail;
373         }
374         if (ah_attr->dlid >= IPATH_MULTICAST_LID_BASE) {
375                 if (ah_attr->dlid != IPATH_PERMISSIVE_LID)
376                         dev->n_multicast_xmit++;
377                 else
378                         dev->n_unicast_xmit++;
379         } else {
380                 dev->n_unicast_xmit++;
381                 lid = ah_attr->dlid &
382                         ~((1 << (dev->mkeyprot_resv_lmc & 7)) - 1);
383                 if (unlikely(lid == dev->dd->ipath_lid)) {
384                         /*
385                          * Pass in an uninitialized ib_wc to save stack
386                          * space.
387                          */
388                         ipath_ud_loopback(qp, &ss, len, wr, &wc);
389                         goto done;
390                 }
391         }
392         if (ah_attr->ah_flags & IB_AH_GRH) {
393                 /* Header size in 32-bit words. */
394                 hwords = 17;
395                 lrh0 = IPATH_LRH_GRH;
396                 ohdr = &qp->s_hdr.u.l.oth;
397                 qp->s_hdr.u.l.grh.version_tclass_flow =
398                         cpu_to_be32((6 << 28) |
399                                     (ah_attr->grh.traffic_class << 20) |
400                                     ah_attr->grh.flow_label);
401                 qp->s_hdr.u.l.grh.paylen =
402                         cpu_to_be16(((wr->opcode ==
403                                       IB_WR_SEND_WITH_IMM ? 6 : 5) +
404                                      nwords + SIZE_OF_CRC) << 2);
405                 /* next_hdr is defined by C8-7 in ch. 8.4.1 */
406                 qp->s_hdr.u.l.grh.next_hdr = 0x1B;
407                 qp->s_hdr.u.l.grh.hop_limit = ah_attr->grh.hop_limit;
408                 /* The SGID is 32-bit aligned. */
409                 qp->s_hdr.u.l.grh.sgid.global.subnet_prefix =
410                         dev->gid_prefix;
411                 qp->s_hdr.u.l.grh.sgid.global.interface_id =
412                         dev->dd->ipath_guid;
413                 qp->s_hdr.u.l.grh.dgid = ah_attr->grh.dgid;
414                 /*
415                  * Don't worry about sending to locally attached multicast
416                  * QPs.  It is unspecified by the spec. what happens.
417                  */
418         } else {
419                 /* Header size in 32-bit words. */
420                 hwords = 7;
421                 lrh0 = IPATH_LRH_BTH;
422                 ohdr = &qp->s_hdr.u.oth;
423         }
424         if (wr->opcode == IB_WR_SEND_WITH_IMM) {
425                 ohdr->u.ud.imm_data = wr->imm_data;
426                 wc.imm_data = wr->imm_data;
427                 hwords += 1;
428                 bth0 = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE << 24;
429         } else if (wr->opcode == IB_WR_SEND) {
430                 wc.imm_data = 0;
431                 bth0 = IB_OPCODE_UD_SEND_ONLY << 24;
432         } else {
433                 ret = -EINVAL;
434                 goto bail;
435         }
436         lrh0 |= ah_attr->sl << 4;
437         if (qp->ibqp.qp_type == IB_QPT_SMI)
438                 lrh0 |= 0xF000; /* Set VL (see ch. 13.5.3.1) */
439         qp->s_hdr.lrh[0] = cpu_to_be16(lrh0);
440         qp->s_hdr.lrh[1] = cpu_to_be16(ah_attr->dlid);  /* DEST LID */
441         qp->s_hdr.lrh[2] = cpu_to_be16(hwords + nwords + SIZE_OF_CRC);
442         lid = dev->dd->ipath_lid;
443         if (lid) {
444                 lid |= ah_attr->src_path_bits &
445                         ((1 << (dev->mkeyprot_resv_lmc & 7)) - 1);
446                 qp->s_hdr.lrh[3] = cpu_to_be16(lid);
447         } else
448                 qp->s_hdr.lrh[3] = IB_LID_PERMISSIVE;
449         if (wr->send_flags & IB_SEND_SOLICITED)
450                 bth0 |= 1 << 23;
451         bth0 |= extra_bytes << 20;
452         bth0 |= qp->ibqp.qp_type == IB_QPT_SMI ? IPATH_DEFAULT_P_KEY :
453                 ipath_get_pkey(dev->dd, qp->s_pkey_index);
454         ohdr->bth[0] = cpu_to_be32(bth0);
455         /*
456          * Use the multicast QP if the destination LID is a multicast LID.
457          */
458         ohdr->bth[1] = ah_attr->dlid >= IPATH_MULTICAST_LID_BASE &&
459                 ah_attr->dlid != IPATH_PERMISSIVE_LID ?
460                 __constant_cpu_to_be32(IPATH_MULTICAST_QPN) :
461                 cpu_to_be32(wr->wr.ud.remote_qpn);
462         /* XXX Could lose a PSN count but not worth locking */
463         ohdr->bth[2] = cpu_to_be32(qp->s_next_psn++ & IPATH_PSN_MASK);
464         /*
465          * Qkeys with the high order bit set mean use the
466          * qkey from the QP context instead of the WR (see 10.2.5).
467          */
468         ohdr->u.ud.deth[0] = cpu_to_be32((int)wr->wr.ud.remote_qkey < 0 ?
469                                          qp->qkey : wr->wr.ud.remote_qkey);
470         ohdr->u.ud.deth[1] = cpu_to_be32(qp->ibqp.qp_num);
471         if (ipath_verbs_send(dev->dd, hwords, (u32 *) &qp->s_hdr,
472                              len, &ss))
473                 dev->n_no_piobuf++;
474
475 done:
476         /* Queue the completion status entry. */
477         if (!(qp->s_flags & IPATH_S_SIGNAL_REQ_WR) ||
478             (wr->send_flags & IB_SEND_SIGNALED)) {
479                 wc.wr_id = wr->wr_id;
480                 wc.status = IB_WC_SUCCESS;
481                 wc.vendor_err = 0;
482                 wc.opcode = IB_WC_SEND;
483                 wc.byte_len = len;
484                 wc.qp = &qp->ibqp;
485                 wc.src_qp = 0;
486                 wc.wc_flags = 0;
487                 /* XXX initialize other fields? */
488                 ipath_cq_enter(to_icq(qp->ibqp.send_cq), &wc, 0);
489         }
490         kfree(sg_list);
491
492         ret = 0;
493
494 bail:
495         return ret;
496 }
497
498 /**
499  * ipath_ud_rcv - receive an incoming UD packet
500  * @dev: the device the packet came in on
501  * @hdr: the packet header
502  * @has_grh: true if the packet has a GRH
503  * @data: the packet data
504  * @tlen: the packet length
505  * @qp: the QP the packet came on
506  *
507  * This is called from ipath_qp_rcv() to process an incoming UD packet
508  * for the given QP.
509  * Called at interrupt level.
510  */
511 void ipath_ud_rcv(struct ipath_ibdev *dev, struct ipath_ib_header *hdr,
512                   int has_grh, void *data, u32 tlen, struct ipath_qp *qp)
513 {
514         struct ipath_other_headers *ohdr;
515         int opcode;
516         u32 hdrsize;
517         u32 pad;
518         struct ib_wc wc;
519         u32 qkey;
520         u32 src_qp;
521         u16 dlid;
522         int header_in_data;
523
524         /* Check for GRH */
525         if (!has_grh) {
526                 ohdr = &hdr->u.oth;
527                 hdrsize = 8 + 12 + 8;   /* LRH + BTH + DETH */
528                 qkey = be32_to_cpu(ohdr->u.ud.deth[0]);
529                 src_qp = be32_to_cpu(ohdr->u.ud.deth[1]);
530                 header_in_data = 0;
531         } else {
532                 ohdr = &hdr->u.l.oth;
533                 hdrsize = 8 + 40 + 12 + 8; /* LRH + GRH + BTH + DETH */
534                 /*
535                  * The header with GRH is 68 bytes and the core driver sets
536                  * the eager header buffer size to 56 bytes so the last 12
537                  * bytes of the IB header is in the data buffer.
538                  */
539                 header_in_data = dev->dd->ipath_rcvhdrentsize == 16;
540                 if (header_in_data) {
541                         qkey = be32_to_cpu(((__be32 *) data)[1]);
542                         src_qp = be32_to_cpu(((__be32 *) data)[2]);
543                         data += 12;
544                 } else {
545                         qkey = be32_to_cpu(ohdr->u.ud.deth[0]);
546                         src_qp = be32_to_cpu(ohdr->u.ud.deth[1]);
547                 }
548         }
549         src_qp &= IPATH_QPN_MASK;
550
551         /*
552          * Check that the permissive LID is only used on QP0
553          * and the QKEY matches (see 9.6.1.4.1 and 9.6.1.5.1).
554          */
555         if (qp->ibqp.qp_num) {
556                 if (unlikely(hdr->lrh[1] == IB_LID_PERMISSIVE ||
557                              hdr->lrh[3] == IB_LID_PERMISSIVE)) {
558                         dev->n_pkt_drops++;
559                         goto bail;
560                 }
561                 if (unlikely(qkey != qp->qkey)) {
562                         /* XXX OK to lose a count once in a while. */
563                         dev->qkey_violations++;
564                         dev->n_pkt_drops++;
565                         goto bail;
566                 }
567         } else if (hdr->lrh[1] == IB_LID_PERMISSIVE ||
568                    hdr->lrh[3] == IB_LID_PERMISSIVE) {
569                 struct ib_smp *smp = (struct ib_smp *) data;
570
571                 if (smp->mgmt_class != IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
572                         dev->n_pkt_drops++;
573                         goto bail;
574                 }
575         }
576
577         /* Get the number of bytes the message was padded by. */
578         pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
579         if (unlikely(tlen < (hdrsize + pad + 4))) {
580                 /* Drop incomplete packets. */
581                 dev->n_pkt_drops++;
582                 goto bail;
583         }
584         tlen -= hdrsize + pad + 4;
585
586         /* Drop invalid MAD packets (see 13.5.3.1). */
587         if (unlikely((qp->ibqp.qp_num == 0 &&
588                       (tlen != 256 ||
589                        (be16_to_cpu(hdr->lrh[0]) >> 12) != 15)) ||
590                      (qp->ibqp.qp_num == 1 &&
591                       (tlen != 256 ||
592                        (be16_to_cpu(hdr->lrh[0]) >> 12) == 15)))) {
593                 dev->n_pkt_drops++;
594                 goto bail;
595         }
596
597         /*
598          * A GRH is expected to preceed the data even if not
599          * present on the wire.
600          */
601         wc.byte_len = tlen + sizeof(struct ib_grh);
602
603         /*
604          * The opcode is in the low byte when its in network order
605          * (top byte when in host order).
606          */
607         opcode = be32_to_cpu(ohdr->bth[0]) >> 24;
608         if (qp->ibqp.qp_num > 1 &&
609             opcode == IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE) {
610                 if (header_in_data) {
611                         wc.imm_data = *(__be32 *) data;
612                         data += sizeof(__be32);
613                 } else
614                         wc.imm_data = ohdr->u.ud.imm_data;
615                 wc.wc_flags = IB_WC_WITH_IMM;
616                 hdrsize += sizeof(u32);
617         } else if (opcode == IB_OPCODE_UD_SEND_ONLY) {
618                 wc.imm_data = 0;
619                 wc.wc_flags = 0;
620         } else {
621                 dev->n_pkt_drops++;
622                 goto bail;
623         }
624
625         /*
626          * Get the next work request entry to find where to put the data.
627          */
628         if (qp->r_reuse_sge)
629                 qp->r_reuse_sge = 0;
630         else if (!ipath_get_rwqe(qp, 0)) {
631                 /*
632                  * Count VL15 packets dropped due to no receive buffer.
633                  * Otherwise, count them as buffer overruns since usually,
634                  * the HW will be able to receive packets even if there are
635                  * no QPs with posted receive buffers.
636                  */
637                 if (qp->ibqp.qp_num == 0)
638                         dev->n_vl15_dropped++;
639                 else
640                         dev->rcv_errors++;
641                 goto bail;
642         }
643         /* Silently drop packets which are too big. */
644         if (wc.byte_len > qp->r_len) {
645                 qp->r_reuse_sge = 1;
646                 dev->n_pkt_drops++;
647                 goto bail;
648         }
649         if (has_grh) {
650                 ipath_copy_sge(&qp->r_sge, &hdr->u.l.grh,
651                                sizeof(struct ib_grh));
652                 wc.wc_flags |= IB_WC_GRH;
653         } else
654                 ipath_skip_sge(&qp->r_sge, sizeof(struct ib_grh));
655         ipath_copy_sge(&qp->r_sge, data,
656                        wc.byte_len - sizeof(struct ib_grh));
657         qp->r_wrid_valid = 0;
658         wc.wr_id = qp->r_wr_id;
659         wc.status = IB_WC_SUCCESS;
660         wc.opcode = IB_WC_RECV;
661         wc.vendor_err = 0;
662         wc.qp = &qp->ibqp;
663         wc.src_qp = src_qp;
664         /* XXX do we know which pkey matched? Only needed for GSI. */
665         wc.pkey_index = 0;
666         wc.slid = be16_to_cpu(hdr->lrh[3]);
667         wc.sl = (be16_to_cpu(hdr->lrh[0]) >> 4) & 0xF;
668         dlid = be16_to_cpu(hdr->lrh[1]);
669         /*
670          * Save the LMC lower bits if the destination LID is a unicast LID.
671          */
672         wc.dlid_path_bits = dlid >= IPATH_MULTICAST_LID_BASE ? 0 :
673                 dlid & ((1 << (dev->mkeyprot_resv_lmc & 7)) - 1);
674         /* Signal completion event if the solicited bit is set. */
675         ipath_cq_enter(to_icq(qp->ibqp.recv_cq), &wc,
676                        (ohdr->bth[0] &
677                         __constant_cpu_to_be32(1 << 23)) != 0);
678
679 bail:;
680 }