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