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