32fc80c904cb3b2e2f357ac35d504f4733260e3b
[linux-2.6-microblaze.git] / drivers / infiniband / hw / qib / qib_ud.c
1 /*
2  * Copyright (c) 2006, 2007, 2008, 2009 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 #include <rdma/ib_verbs.h>
36
37 #include "qib.h"
38 #include "qib_mad.h"
39
40 /**
41  * qib_ud_loopback - handle send on loopback QPs
42  * @sqp: the sending QP
43  * @swqe: the send work request
44  *
45  * This is called from qib_make_ud_req() to forward a WQE addressed
46  * to the same HCA.
47  * Note that the receive interrupt handler may be calling qib_ud_rcv()
48  * while this is being called.
49  */
50 static void qib_ud_loopback(struct qib_qp *sqp, struct qib_swqe *swqe)
51 {
52         struct qib_ibport *ibp = to_iport(sqp->ibqp.device, sqp->port_num);
53         struct qib_pportdata *ppd;
54         struct qib_qp *qp;
55         struct ib_ah_attr *ah_attr;
56         unsigned long flags;
57         struct qib_sge_state ssge;
58         struct qib_sge *sge;
59         struct ib_wc wc;
60         u32 length;
61         enum ib_qp_type sqptype, dqptype;
62
63         qp = qib_lookup_qpn(ibp, swqe->ud_wr.remote_qpn);
64         if (!qp) {
65                 ibp->n_pkt_drops++;
66                 return;
67         }
68
69         sqptype = sqp->ibqp.qp_type == IB_QPT_GSI ?
70                         IB_QPT_UD : sqp->ibqp.qp_type;
71         dqptype = qp->ibqp.qp_type == IB_QPT_GSI ?
72                         IB_QPT_UD : qp->ibqp.qp_type;
73
74         if (dqptype != sqptype ||
75             !(ib_qib_state_ops[qp->state] & QIB_PROCESS_RECV_OK)) {
76                 ibp->n_pkt_drops++;
77                 goto drop;
78         }
79
80         ah_attr = &to_iah(swqe->ud_wr.ah)->attr;
81         ppd = ppd_from_ibp(ibp);
82
83         if (qp->ibqp.qp_num > 1) {
84                 u16 pkey1;
85                 u16 pkey2;
86                 u16 lid;
87
88                 pkey1 = qib_get_pkey(ibp, sqp->s_pkey_index);
89                 pkey2 = qib_get_pkey(ibp, qp->s_pkey_index);
90                 if (unlikely(!qib_pkey_ok(pkey1, pkey2))) {
91                         lid = ppd->lid | (ah_attr->src_path_bits &
92                                           ((1 << ppd->lmc) - 1));
93                         qib_bad_pqkey(ibp, IB_NOTICE_TRAP_BAD_PKEY, pkey1,
94                                       ah_attr->sl,
95                                       sqp->ibqp.qp_num, qp->ibqp.qp_num,
96                                       cpu_to_be16(lid),
97                                       cpu_to_be16(ah_attr->dlid));
98                         goto drop;
99                 }
100         }
101
102         /*
103          * Check that the qkey matches (except for QP0, see 9.6.1.4.1).
104          * Qkeys with the high order bit set mean use the
105          * qkey from the QP context instead of the WR (see 10.2.5).
106          */
107         if (qp->ibqp.qp_num) {
108                 u32 qkey;
109
110                 qkey = (int)swqe->ud_wr.remote_qkey < 0 ?
111                         sqp->qkey : swqe->ud_wr.remote_qkey;
112                 if (unlikely(qkey != qp->qkey)) {
113                         u16 lid;
114
115                         lid = ppd->lid | (ah_attr->src_path_bits &
116                                           ((1 << ppd->lmc) - 1));
117                         qib_bad_pqkey(ibp, IB_NOTICE_TRAP_BAD_QKEY, qkey,
118                                       ah_attr->sl,
119                                       sqp->ibqp.qp_num, qp->ibqp.qp_num,
120                                       cpu_to_be16(lid),
121                                       cpu_to_be16(ah_attr->dlid));
122                         goto drop;
123                 }
124         }
125
126         /*
127          * A GRH is expected to precede the data even if not
128          * present on the wire.
129          */
130         length = swqe->length;
131         memset(&wc, 0, sizeof(wc));
132         wc.byte_len = length + sizeof(struct ib_grh);
133
134         if (swqe->wr.opcode == IB_WR_SEND_WITH_IMM) {
135                 wc.wc_flags = IB_WC_WITH_IMM;
136                 wc.ex.imm_data = swqe->wr.ex.imm_data;
137         }
138
139         spin_lock_irqsave(&qp->r_lock, flags);
140
141         /*
142          * Get the next work request entry to find where to put the data.
143          */
144         if (qp->r_flags & QIB_R_REUSE_SGE)
145                 qp->r_flags &= ~QIB_R_REUSE_SGE;
146         else {
147                 int ret;
148
149                 ret = qib_get_rwqe(qp, 0);
150                 if (ret < 0) {
151                         qib_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
152                         goto bail_unlock;
153                 }
154                 if (!ret) {
155                         if (qp->ibqp.qp_num == 0)
156                                 ibp->n_vl15_dropped++;
157                         goto bail_unlock;
158                 }
159         }
160         /* Silently drop packets which are too big. */
161         if (unlikely(wc.byte_len > qp->r_len)) {
162                 qp->r_flags |= QIB_R_REUSE_SGE;
163                 ibp->n_pkt_drops++;
164                 goto bail_unlock;
165         }
166
167         if (ah_attr->ah_flags & IB_AH_GRH) {
168                 qib_copy_sge(&qp->r_sge, &ah_attr->grh,
169                              sizeof(struct ib_grh), 1);
170                 wc.wc_flags |= IB_WC_GRH;
171         } else
172                 qib_skip_sge(&qp->r_sge, sizeof(struct ib_grh), 1);
173         ssge.sg_list = swqe->sg_list + 1;
174         ssge.sge = *swqe->sg_list;
175         ssge.num_sge = swqe->wr.num_sge;
176         sge = &ssge.sge;
177         while (length) {
178                 u32 len = sge->length;
179
180                 if (len > length)
181                         len = length;
182                 if (len > sge->sge_length)
183                         len = sge->sge_length;
184                 BUG_ON(len == 0);
185                 qib_copy_sge(&qp->r_sge, sge->vaddr, len, 1);
186                 sge->vaddr += len;
187                 sge->length -= len;
188                 sge->sge_length -= len;
189                 if (sge->sge_length == 0) {
190                         if (--ssge.num_sge)
191                                 *sge = *ssge.sg_list++;
192                 } else if (sge->length == 0 && sge->mr->lkey) {
193                         if (++sge->n >= QIB_SEGSZ) {
194                                 if (++sge->m >= sge->mr->mapsz)
195                                         break;
196                                 sge->n = 0;
197                         }
198                         sge->vaddr =
199                                 sge->mr->map[sge->m]->segs[sge->n].vaddr;
200                         sge->length =
201                                 sge->mr->map[sge->m]->segs[sge->n].length;
202                 }
203                 length -= len;
204         }
205         qib_put_ss(&qp->r_sge);
206         if (!test_and_clear_bit(QIB_R_WRID_VALID, &qp->r_aflags))
207                 goto bail_unlock;
208         wc.wr_id = qp->r_wr_id;
209         wc.status = IB_WC_SUCCESS;
210         wc.opcode = IB_WC_RECV;
211         wc.qp = &qp->ibqp;
212         wc.src_qp = sqp->ibqp.qp_num;
213         wc.pkey_index = qp->ibqp.qp_type == IB_QPT_GSI ?
214                 swqe->ud_wr.pkey_index : 0;
215         wc.slid = ppd->lid | (ah_attr->src_path_bits & ((1 << ppd->lmc) - 1));
216         wc.sl = ah_attr->sl;
217         wc.dlid_path_bits = ah_attr->dlid & ((1 << ppd->lmc) - 1);
218         wc.port_num = qp->port_num;
219         /* Signal completion event if the solicited bit is set. */
220         qib_cq_enter(to_icq(qp->ibqp.recv_cq), &wc,
221                      swqe->wr.send_flags & IB_SEND_SOLICITED);
222         ibp->n_loop_pkts++;
223 bail_unlock:
224         spin_unlock_irqrestore(&qp->r_lock, flags);
225 drop:
226         if (atomic_dec_and_test(&qp->refcount))
227                 wake_up(&qp->wait);
228 }
229
230 /**
231  * qib_make_ud_req - construct a UD request packet
232  * @qp: the QP
233  *
234  * Return 1 if constructed; otherwise, return 0.
235  */
236 int qib_make_ud_req(struct qib_qp *qp)
237 {
238         struct qib_other_headers *ohdr;
239         struct ib_ah_attr *ah_attr;
240         struct qib_pportdata *ppd;
241         struct qib_ibport *ibp;
242         struct qib_swqe *wqe;
243         unsigned long flags;
244         u32 nwords;
245         u32 extra_bytes;
246         u32 bth0;
247         u16 lrh0;
248         u16 lid;
249         int ret = 0;
250         int next_cur;
251
252         spin_lock_irqsave(&qp->s_lock, flags);
253
254         if (!(ib_qib_state_ops[qp->state] & QIB_PROCESS_NEXT_SEND_OK)) {
255                 if (!(ib_qib_state_ops[qp->state] & QIB_FLUSH_SEND))
256                         goto bail;
257                 /* We are in the error state, flush the work request. */
258                 if (qp->s_last == qp->s_head)
259                         goto bail;
260                 /* If DMAs are in progress, we can't flush immediately. */
261                 if (atomic_read(&qp->s_dma_busy)) {
262                         qp->s_flags |= QIB_S_WAIT_DMA;
263                         goto bail;
264                 }
265                 wqe = get_swqe_ptr(qp, qp->s_last);
266                 qib_send_complete(qp, wqe, IB_WC_WR_FLUSH_ERR);
267                 goto done;
268         }
269
270         if (qp->s_cur == qp->s_head)
271                 goto bail;
272
273         wqe = get_swqe_ptr(qp, qp->s_cur);
274         next_cur = qp->s_cur + 1;
275         if (next_cur >= qp->s_size)
276                 next_cur = 0;
277
278         /* Construct the header. */
279         ibp = to_iport(qp->ibqp.device, qp->port_num);
280         ppd = ppd_from_ibp(ibp);
281         ah_attr = &to_iah(wqe->ud_wr.ah)->attr;
282         if (ah_attr->dlid >= be16_to_cpu(IB_MULTICAST_LID_BASE)) {
283                 if (ah_attr->dlid != be16_to_cpu(IB_LID_PERMISSIVE))
284                         this_cpu_inc(ibp->pmastats->n_multicast_xmit);
285                 else
286                         this_cpu_inc(ibp->pmastats->n_unicast_xmit);
287         } else {
288                 this_cpu_inc(ibp->pmastats->n_unicast_xmit);
289                 lid = ah_attr->dlid & ~((1 << ppd->lmc) - 1);
290                 if (unlikely(lid == ppd->lid)) {
291                         /*
292                          * If DMAs are in progress, we can't generate
293                          * a completion for the loopback packet since
294                          * it would be out of order.
295                          * XXX Instead of waiting, we could queue a
296                          * zero length descriptor so we get a callback.
297                          */
298                         if (atomic_read(&qp->s_dma_busy)) {
299                                 qp->s_flags |= QIB_S_WAIT_DMA;
300                                 goto bail;
301                         }
302                         qp->s_cur = next_cur;
303                         spin_unlock_irqrestore(&qp->s_lock, flags);
304                         qib_ud_loopback(qp, wqe);
305                         spin_lock_irqsave(&qp->s_lock, flags);
306                         qib_send_complete(qp, wqe, IB_WC_SUCCESS);
307                         goto done;
308                 }
309         }
310
311         qp->s_cur = next_cur;
312         extra_bytes = -wqe->length & 3;
313         nwords = (wqe->length + extra_bytes) >> 2;
314
315         /* header size in 32-bit words LRH+BTH+DETH = (8+12+8)/4. */
316         qp->s_hdrwords = 7;
317         qp->s_cur_size = wqe->length;
318         qp->s_cur_sge = &qp->s_sge;
319         qp->s_srate = ah_attr->static_rate;
320         qp->s_wqe = wqe;
321         qp->s_sge.sge = wqe->sg_list[0];
322         qp->s_sge.sg_list = wqe->sg_list + 1;
323         qp->s_sge.num_sge = wqe->wr.num_sge;
324         qp->s_sge.total_len = wqe->length;
325
326         if (ah_attr->ah_flags & IB_AH_GRH) {
327                 /* Header size in 32-bit words. */
328                 qp->s_hdrwords += qib_make_grh(ibp, &qp->s_hdr->u.l.grh,
329                                                &ah_attr->grh,
330                                                qp->s_hdrwords, nwords);
331                 lrh0 = QIB_LRH_GRH;
332                 ohdr = &qp->s_hdr->u.l.oth;
333                 /*
334                  * Don't worry about sending to locally attached multicast
335                  * QPs.  It is unspecified by the spec. what happens.
336                  */
337         } else {
338                 /* Header size in 32-bit words. */
339                 lrh0 = QIB_LRH_BTH;
340                 ohdr = &qp->s_hdr->u.oth;
341         }
342         if (wqe->wr.opcode == IB_WR_SEND_WITH_IMM) {
343                 qp->s_hdrwords++;
344                 ohdr->u.ud.imm_data = wqe->wr.ex.imm_data;
345                 bth0 = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE << 24;
346         } else
347                 bth0 = IB_OPCODE_UD_SEND_ONLY << 24;
348         lrh0 |= ah_attr->sl << 4;
349         if (qp->ibqp.qp_type == IB_QPT_SMI)
350                 lrh0 |= 0xF000; /* Set VL (see ch. 13.5.3.1) */
351         else
352                 lrh0 |= ibp->sl_to_vl[ah_attr->sl] << 12;
353         qp->s_hdr->lrh[0] = cpu_to_be16(lrh0);
354         qp->s_hdr->lrh[1] = cpu_to_be16(ah_attr->dlid);  /* DEST LID */
355         qp->s_hdr->lrh[2] = cpu_to_be16(qp->s_hdrwords + nwords + SIZE_OF_CRC);
356         lid = ppd->lid;
357         if (lid) {
358                 lid |= ah_attr->src_path_bits & ((1 << ppd->lmc) - 1);
359                 qp->s_hdr->lrh[3] = cpu_to_be16(lid);
360         } else
361                 qp->s_hdr->lrh[3] = IB_LID_PERMISSIVE;
362         if (wqe->wr.send_flags & IB_SEND_SOLICITED)
363                 bth0 |= IB_BTH_SOLICITED;
364         bth0 |= extra_bytes << 20;
365         bth0 |= qp->ibqp.qp_type == IB_QPT_SMI ? QIB_DEFAULT_P_KEY :
366                 qib_get_pkey(ibp, qp->ibqp.qp_type == IB_QPT_GSI ?
367                              wqe->ud_wr.pkey_index : qp->s_pkey_index);
368         ohdr->bth[0] = cpu_to_be32(bth0);
369         /*
370          * Use the multicast QP if the destination LID is a multicast LID.
371          */
372         ohdr->bth[1] = ah_attr->dlid >= be16_to_cpu(IB_MULTICAST_LID_BASE) &&
373                 ah_attr->dlid != be16_to_cpu(IB_LID_PERMISSIVE) ?
374                 cpu_to_be32(QIB_MULTICAST_QPN) :
375                 cpu_to_be32(wqe->ud_wr.remote_qpn);
376         ohdr->bth[2] = cpu_to_be32(qp->s_next_psn++ & QIB_PSN_MASK);
377         /*
378          * Qkeys with the high order bit set mean use the
379          * qkey from the QP context instead of the WR (see 10.2.5).
380          */
381         ohdr->u.ud.deth[0] = cpu_to_be32((int)wqe->ud_wr.remote_qkey < 0 ?
382                                          qp->qkey : wqe->ud_wr.remote_qkey);
383         ohdr->u.ud.deth[1] = cpu_to_be32(qp->ibqp.qp_num);
384
385 done:
386         ret = 1;
387         goto unlock;
388
389 bail:
390         qp->s_flags &= ~QIB_S_BUSY;
391 unlock:
392         spin_unlock_irqrestore(&qp->s_lock, flags);
393         return ret;
394 }
395
396 static unsigned qib_lookup_pkey(struct qib_ibport *ibp, u16 pkey)
397 {
398         struct qib_pportdata *ppd = ppd_from_ibp(ibp);
399         struct qib_devdata *dd = ppd->dd;
400         unsigned ctxt = ppd->hw_pidx;
401         unsigned i;
402
403         pkey &= 0x7fff; /* remove limited/full membership bit */
404
405         for (i = 0; i < ARRAY_SIZE(dd->rcd[ctxt]->pkeys); ++i)
406                 if ((dd->rcd[ctxt]->pkeys[i] & 0x7fff) == pkey)
407                         return i;
408
409         /*
410          * Should not get here, this means hardware failed to validate pkeys.
411          * Punt and return index 0.
412          */
413         return 0;
414 }
415
416 /**
417  * qib_ud_rcv - receive an incoming UD packet
418  * @ibp: the port the packet came in on
419  * @hdr: the packet header
420  * @has_grh: true if the packet has a GRH
421  * @data: the packet data
422  * @tlen: the packet length
423  * @qp: the QP the packet came on
424  *
425  * This is called from qib_qp_rcv() to process an incoming UD packet
426  * for the given QP.
427  * Called at interrupt level.
428  */
429 void qib_ud_rcv(struct qib_ibport *ibp, struct qib_ib_header *hdr,
430                 int has_grh, void *data, u32 tlen, struct qib_qp *qp)
431 {
432         struct qib_other_headers *ohdr;
433         int opcode;
434         u32 hdrsize;
435         u32 pad;
436         struct ib_wc wc;
437         u32 qkey;
438         u32 src_qp;
439         u16 dlid;
440
441         /* Check for GRH */
442         if (!has_grh) {
443                 ohdr = &hdr->u.oth;
444                 hdrsize = 8 + 12 + 8;   /* LRH + BTH + DETH */
445         } else {
446                 ohdr = &hdr->u.l.oth;
447                 hdrsize = 8 + 40 + 12 + 8; /* LRH + GRH + BTH + DETH */
448         }
449         qkey = be32_to_cpu(ohdr->u.ud.deth[0]);
450         src_qp = be32_to_cpu(ohdr->u.ud.deth[1]) & QIB_QPN_MASK;
451
452         /*
453          * Get the number of bytes the message was padded by
454          * and drop incomplete packets.
455          */
456         pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
457         if (unlikely(tlen < (hdrsize + pad + 4)))
458                 goto drop;
459
460         tlen -= hdrsize + pad + 4;
461
462         /*
463          * Check that the permissive LID is only used on QP0
464          * and the QKEY matches (see 9.6.1.4.1 and 9.6.1.5.1).
465          */
466         if (qp->ibqp.qp_num) {
467                 if (unlikely(hdr->lrh[1] == IB_LID_PERMISSIVE ||
468                              hdr->lrh[3] == IB_LID_PERMISSIVE))
469                         goto drop;
470                 if (qp->ibqp.qp_num > 1) {
471                         u16 pkey1, pkey2;
472
473                         pkey1 = be32_to_cpu(ohdr->bth[0]);
474                         pkey2 = qib_get_pkey(ibp, qp->s_pkey_index);
475                         if (unlikely(!qib_pkey_ok(pkey1, pkey2))) {
476                                 qib_bad_pqkey(ibp, IB_NOTICE_TRAP_BAD_PKEY,
477                                               pkey1,
478                                               (be16_to_cpu(hdr->lrh[0]) >> 4) &
479                                                 0xF,
480                                               src_qp, qp->ibqp.qp_num,
481                                               hdr->lrh[3], hdr->lrh[1]);
482                                 return;
483                         }
484                 }
485                 if (unlikely(qkey != qp->qkey)) {
486                         qib_bad_pqkey(ibp, IB_NOTICE_TRAP_BAD_QKEY, qkey,
487                                       (be16_to_cpu(hdr->lrh[0]) >> 4) & 0xF,
488                                       src_qp, qp->ibqp.qp_num,
489                                       hdr->lrh[3], hdr->lrh[1]);
490                         return;
491                 }
492                 /* Drop invalid MAD packets (see 13.5.3.1). */
493                 if (unlikely(qp->ibqp.qp_num == 1 &&
494                              (tlen != 256 ||
495                               (be16_to_cpu(hdr->lrh[0]) >> 12) == 15)))
496                         goto drop;
497         } else {
498                 struct ib_smp *smp;
499
500                 /* Drop invalid MAD packets (see 13.5.3.1). */
501                 if (tlen != 256 || (be16_to_cpu(hdr->lrh[0]) >> 12) != 15)
502                         goto drop;
503                 smp = (struct ib_smp *) data;
504                 if ((hdr->lrh[1] == IB_LID_PERMISSIVE ||
505                      hdr->lrh[3] == IB_LID_PERMISSIVE) &&
506                     smp->mgmt_class != IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE)
507                         goto drop;
508         }
509
510         /*
511          * The opcode is in the low byte when its in network order
512          * (top byte when in host order).
513          */
514         opcode = be32_to_cpu(ohdr->bth[0]) >> 24;
515         if (qp->ibqp.qp_num > 1 &&
516             opcode == IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE) {
517                 wc.ex.imm_data = ohdr->u.ud.imm_data;
518                 wc.wc_flags = IB_WC_WITH_IMM;
519                 tlen -= sizeof(u32);
520         } else if (opcode == IB_OPCODE_UD_SEND_ONLY) {
521                 wc.ex.imm_data = 0;
522                 wc.wc_flags = 0;
523         } else
524                 goto drop;
525
526         /*
527          * A GRH is expected to precede the data even if not
528          * present on the wire.
529          */
530         wc.byte_len = tlen + sizeof(struct ib_grh);
531
532         /*
533          * Get the next work request entry to find where to put the data.
534          */
535         if (qp->r_flags & QIB_R_REUSE_SGE)
536                 qp->r_flags &= ~QIB_R_REUSE_SGE;
537         else {
538                 int ret;
539
540                 ret = qib_get_rwqe(qp, 0);
541                 if (ret < 0) {
542                         qib_rc_error(qp, IB_WC_LOC_QP_OP_ERR);
543                         return;
544                 }
545                 if (!ret) {
546                         if (qp->ibqp.qp_num == 0)
547                                 ibp->n_vl15_dropped++;
548                         return;
549                 }
550         }
551         /* Silently drop packets which are too big. */
552         if (unlikely(wc.byte_len > qp->r_len)) {
553                 qp->r_flags |= QIB_R_REUSE_SGE;
554                 goto drop;
555         }
556         if (has_grh) {
557                 qib_copy_sge(&qp->r_sge, &hdr->u.l.grh,
558                              sizeof(struct ib_grh), 1);
559                 wc.wc_flags |= IB_WC_GRH;
560         } else
561                 qib_skip_sge(&qp->r_sge, sizeof(struct ib_grh), 1);
562         qib_copy_sge(&qp->r_sge, data, wc.byte_len - sizeof(struct ib_grh), 1);
563         qib_put_ss(&qp->r_sge);
564         if (!test_and_clear_bit(QIB_R_WRID_VALID, &qp->r_aflags))
565                 return;
566         wc.wr_id = qp->r_wr_id;
567         wc.status = IB_WC_SUCCESS;
568         wc.opcode = IB_WC_RECV;
569         wc.vendor_err = 0;
570         wc.qp = &qp->ibqp;
571         wc.src_qp = src_qp;
572         wc.pkey_index = qp->ibqp.qp_type == IB_QPT_GSI ?
573                 qib_lookup_pkey(ibp, be32_to_cpu(ohdr->bth[0])) : 0;
574         wc.slid = be16_to_cpu(hdr->lrh[3]);
575         wc.sl = (be16_to_cpu(hdr->lrh[0]) >> 4) & 0xF;
576         dlid = be16_to_cpu(hdr->lrh[1]);
577         /*
578          * Save the LMC lower bits if the destination LID is a unicast LID.
579          */
580         wc.dlid_path_bits = dlid >= be16_to_cpu(IB_MULTICAST_LID_BASE) ? 0 :
581                 dlid & ((1 << ppd_from_ibp(ibp)->lmc) - 1);
582         wc.port_num = qp->port_num;
583         /* Signal completion event if the solicited bit is set. */
584         qib_cq_enter(to_icq(qp->ibqp.recv_cq), &wc,
585                      (ohdr->bth[0] &
586                         cpu_to_be32(IB_BTH_SOLICITED)) != 0);
587         return;
588
589 drop:
590         ibp->n_pkt_drops++;
591 }