blob: a518f7c8fa83a9372fc9a5327e646825876261bf [file] [log] [blame]
Bryan O'Sullivan74ed6b52006-03-29 15:23:34 -08001/*
Bryan O'Sullivan759d5762006-07-01 04:35:49 -07002 * Copyright (c) 2006 QLogic, Inc. All rights reserved.
Bryan O'Sullivan74ed6b52006-03-29 15:23:34 -08003 * 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"
Ralph Campbell373d9912006-09-22 15:22:26 -070037#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{
Ralph Campbell373d9912006-09-22 15:22:26 -070042 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) ||
Bryan O'Sullivan6a553af2006-09-28 09:00:07 -070052 !ipath_lkey_ok(qp, j ? &ss->sg_list[j - 1] : &ss->sge,
Ralph Campbell373d9912006-09-22 15:22:26 -070053 &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;
Michael S. Tsirkin062dbb62006-12-31 21:09:42 +020069 wc.qp = &qp->ibqp;
Ralph Campbell373d9912006-09-22 15:22:26 -070070 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}
Bryan O'Sullivan74ed6b52006-03-29 15:23:34 -080083
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.
Ralph Campbell373d9912006-09-22 15:22:26 -070094 * Note that the receive interrupt handler may be calling ipath_ud_rcv()
95 * while this is being called.
Bryan O'Sullivan74ed6b52006-03-29 15:23:34 -080096 */
Bryan O'Sullivanf6f04132006-04-24 14:23:09 -070097static 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)
Bryan O'Sullivan74ed6b52006-03-29 15:23:34 -0800101{
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;
Ralph Campbell373d9912006-09-22 15:22:26 -0700110 struct ipath_rwq *wq;
Bryan O'Sullivan74ed6b52006-03-29 15:23:34 -0800111 struct ipath_rwqe *wqe;
Ralph Campbell373d9912006-09-22 15:22:26 -0700112 void (*handler)(struct ib_event *, void *);
113 u32 tail;
114 u32 rlen;
Bryan O'Sullivan74ed6b52006-03-29 15:23:34 -0800115
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
Ralph Campbell373d9912006-09-22 15:22:26 -0700148 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
Bryan O'Sullivan74ed6b52006-03-29 15:23:34 -0800155 /*
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);
Ralph Campbell373d9912006-09-22 15:22:26 -0700162 handler = srq->ibsrq.event_handler;
Bryan O'Sullivan74ed6b52006-03-29 15:23:34 -0800163 rq = &srq->rq;
164 } else {
165 srq = NULL;
Ralph Campbell373d9912006-09-22 15:22:26 -0700166 handler = NULL;
Bryan O'Sullivan74ed6b52006-03-29 15:23:34 -0800167 rq = &qp->r_rq;
168 }
Ralph Campbell373d9912006-09-22 15:22:26 -0700169
Bryan O'Sullivan74ed6b52006-03-29 15:23:34 -0800170 spin_lock_irqsave(&rq->lock, flags);
Ralph Campbell373d9912006-09-22 15:22:26 -0700171 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;
Bryan O'Sullivan74ed6b52006-03-29 15:23:34 -0800185 }
186 /* Silently drop packets which are too big. */
Ralph Campbell373d9912006-09-22 15:22:26 -0700187 if (wc->byte_len > rlen) {
Bryan O'Sullivan74ed6b52006-03-29 15:23:34 -0800188 spin_unlock_irqrestore(&rq->lock, flags);
189 dev->n_pkt_drops++;
Ralph Campbell373d9912006-09-22 15:22:26 -0700190 goto bail_sge;
Bryan O'Sullivan74ed6b52006-03-29 15:23:34 -0800191 }
Ralph Campbell373d9912006-09-22 15:22:26 -0700192 wq->tail = tail;
Bryan O'Sullivan74ed6b52006-03-29 15:23:34 -0800193 wc->wr_id = wqe->wr_id;
Ralph Campbell373d9912006-09-22 15:22:26 -0700194 if (handler) {
Bryan O'Sullivan74ed6b52006-03-29 15:23:34 -0800195 u32 n;
196
Ralph Campbell373d9912006-09-22 15:22:26 -0700197 /*
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;
Bryan O'Sullivan74ed6b52006-03-29 15:23:34 -0800206 else
Ralph Campbell373d9912006-09-22 15:22:26 -0700207 n -= tail;
Bryan O'Sullivan74ed6b52006-03-29 15:23:34 -0800208 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;
Ralph Campbell373d9912006-09-22 15:22:26 -0700216 handler(&ev, srq->ibsrq.srq_context);
Bryan O'Sullivan74ed6b52006-03-29 15:23:34 -0800217 } else
218 spin_unlock_irqrestore(&rq->lock, flags);
219 } else
220 spin_unlock_irqrestore(&rq->lock, flags);
Ralph Campbell373d9912006-09-22 15:22:26 -0700221
Bryan O'Sullivan74ed6b52006-03-29 15:23:34 -0800222 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;
Michael S. Tsirkin062dbb62006-12-31 21:09:42 +0200258 wc->qp = &qp->ibqp;
Bryan O'Sullivan74ed6b52006-03-29 15:23:34 -0800259 wc->src_qp = sqp->ibqp.qp_num;
260 /* XXX do we know which pkey matched? Only needed for GSI. */
261 wc->pkey_index = 0;
Ralph Campbell373d9912006-09-22 15:22:26 -0700262 wc->slid = dev->dd->ipath_lid |
Bryan O'Sullivan74ed6b52006-03-29 15:23:34 -0800263 (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
Ralph Campbell373d9912006-09-22 15:22:26 -0700272bail_sge:
273 kfree(rsge.sg_list);
Bryan O'Sullivan74ed6b52006-03-29 15:23:34 -0800274done:
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
Robert Walsh6ce73b02007-03-15 14:45:16 -0700311 if (wr->wr.ud.ah->pd != qp->ibqp.pd) {
312 ret = -EPERM;
313 goto bail;
314 }
315
Bryan O'Sullivan74ed6b52006-03-29 15:23:34 -0800316 /* IB spec says that num_sge == 0 is OK. */
317 if (wr->num_sge > qp->s_max_sge) {
318 ret = -EINVAL;
319 goto bail;
320 }
321
322 if (wr->num_sge > 1) {
323 sg_list = kmalloc((qp->s_max_sge - 1) * sizeof(*sg_list),
324 GFP_ATOMIC);
325 if (!sg_list) {
326 ret = -ENOMEM;
327 goto bail;
328 }
329 } else
330 sg_list = NULL;
331
332 /* Check the buffer to send. */
333 ss.sg_list = sg_list;
334 ss.sge.mr = NULL;
335 ss.sge.vaddr = NULL;
336 ss.sge.length = 0;
337 ss.sge.sge_length = 0;
338 ss.num_sge = 0;
339 len = 0;
340 for (i = 0; i < wr->num_sge; i++) {
341 /* Check LKEY */
342 if (to_ipd(qp->ibqp.pd)->user && wr->sg_list[i].lkey == 0) {
343 ret = -EINVAL;
344 goto bail;
345 }
346
347 if (wr->sg_list[i].length == 0)
348 continue;
Bryan O'Sullivan6a553af2006-09-28 09:00:07 -0700349 if (!ipath_lkey_ok(qp, ss.num_sge ?
Bryan O'Sullivan74ed6b52006-03-29 15:23:34 -0800350 sg_list + ss.num_sge - 1 : &ss.sge,
351 &wr->sg_list[i], 0)) {
352 ret = -EINVAL;
353 goto bail;
354 }
355 len += wr->sg_list[i].length;
356 ss.num_sge++;
357 }
Bryan O'Sullivan4faba982006-07-01 04:36:06 -0700358 /* Check for invalid packet size. */
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -0700359 if (len > dev->dd->ipath_ibmtu) {
Bryan O'Sullivan4faba982006-07-01 04:36:06 -0700360 ret = -EINVAL;
361 goto bail;
362 }
Bryan O'Sullivan74ed6b52006-03-29 15:23:34 -0800363 extra_bytes = (4 - len) & 3;
364 nwords = (len + extra_bytes) >> 2;
365
366 /* Construct the header. */
367 ah_attr = &to_iah(wr->wr.ud.ah)->attr;
368 if (ah_attr->dlid == 0) {
369 ret = -EINVAL;
370 goto bail;
371 }
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700372 if (ah_attr->dlid >= IPATH_MULTICAST_LID_BASE) {
373 if (ah_attr->dlid != IPATH_PERMISSIVE_LID)
Bryan O'Sullivan74ed6b52006-03-29 15:23:34 -0800374 dev->n_multicast_xmit++;
375 else
376 dev->n_unicast_xmit++;
377 } else {
378 dev->n_unicast_xmit++;
379 lid = ah_attr->dlid &
380 ~((1 << (dev->mkeyprot_resv_lmc & 7)) - 1);
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -0700381 if (unlikely(lid == dev->dd->ipath_lid)) {
Bryan O'Sullivan74ed6b52006-03-29 15:23:34 -0800382 /*
383 * Pass in an uninitialized ib_wc to save stack
384 * space.
385 */
386 ipath_ud_loopback(qp, &ss, len, wr, &wc);
387 goto done;
388 }
389 }
390 if (ah_attr->ah_flags & IB_AH_GRH) {
391 /* Header size in 32-bit words. */
392 hwords = 17;
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700393 lrh0 = IPATH_LRH_GRH;
Bryan O'Sullivan74ed6b52006-03-29 15:23:34 -0800394 ohdr = &qp->s_hdr.u.l.oth;
395 qp->s_hdr.u.l.grh.version_tclass_flow =
396 cpu_to_be32((6 << 28) |
397 (ah_attr->grh.traffic_class << 20) |
398 ah_attr->grh.flow_label);
399 qp->s_hdr.u.l.grh.paylen =
400 cpu_to_be16(((wr->opcode ==
401 IB_WR_SEND_WITH_IMM ? 6 : 5) +
402 nwords + SIZE_OF_CRC) << 2);
403 /* next_hdr is defined by C8-7 in ch. 8.4.1 */
404 qp->s_hdr.u.l.grh.next_hdr = 0x1B;
405 qp->s_hdr.u.l.grh.hop_limit = ah_attr->grh.hop_limit;
406 /* The SGID is 32-bit aligned. */
407 qp->s_hdr.u.l.grh.sgid.global.subnet_prefix =
408 dev->gid_prefix;
409 qp->s_hdr.u.l.grh.sgid.global.interface_id =
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -0700410 dev->dd->ipath_guid;
Bryan O'Sullivan74ed6b52006-03-29 15:23:34 -0800411 qp->s_hdr.u.l.grh.dgid = ah_attr->grh.dgid;
412 /*
413 * Don't worry about sending to locally attached multicast
414 * QPs. It is unspecified by the spec. what happens.
415 */
416 } else {
417 /* Header size in 32-bit words. */
418 hwords = 7;
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700419 lrh0 = IPATH_LRH_BTH;
Bryan O'Sullivan74ed6b52006-03-29 15:23:34 -0800420 ohdr = &qp->s_hdr.u.oth;
421 }
422 if (wr->opcode == IB_WR_SEND_WITH_IMM) {
423 ohdr->u.ud.imm_data = wr->imm_data;
424 wc.imm_data = wr->imm_data;
425 hwords += 1;
426 bth0 = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE << 24;
427 } else if (wr->opcode == IB_WR_SEND) {
428 wc.imm_data = 0;
429 bth0 = IB_OPCODE_UD_SEND_ONLY << 24;
430 } else {
431 ret = -EINVAL;
432 goto bail;
433 }
434 lrh0 |= ah_attr->sl << 4;
435 if (qp->ibqp.qp_type == IB_QPT_SMI)
436 lrh0 |= 0xF000; /* Set VL (see ch. 13.5.3.1) */
437 qp->s_hdr.lrh[0] = cpu_to_be16(lrh0);
438 qp->s_hdr.lrh[1] = cpu_to_be16(ah_attr->dlid); /* DEST LID */
439 qp->s_hdr.lrh[2] = cpu_to_be16(hwords + nwords + SIZE_OF_CRC);
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -0700440 lid = dev->dd->ipath_lid;
Bryan O'Sullivan74ed6b52006-03-29 15:23:34 -0800441 if (lid) {
442 lid |= ah_attr->src_path_bits &
443 ((1 << (dev->mkeyprot_resv_lmc & 7)) - 1);
444 qp->s_hdr.lrh[3] = cpu_to_be16(lid);
445 } else
446 qp->s_hdr.lrh[3] = IB_LID_PERMISSIVE;
447 if (wr->send_flags & IB_SEND_SOLICITED)
448 bth0 |= 1 << 23;
449 bth0 |= extra_bytes << 20;
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700450 bth0 |= qp->ibqp.qp_type == IB_QPT_SMI ? IPATH_DEFAULT_P_KEY :
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -0700451 ipath_get_pkey(dev->dd, qp->s_pkey_index);
Bryan O'Sullivan74ed6b52006-03-29 15:23:34 -0800452 ohdr->bth[0] = cpu_to_be32(bth0);
453 /*
454 * Use the multicast QP if the destination LID is a multicast LID.
455 */
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700456 ohdr->bth[1] = ah_attr->dlid >= IPATH_MULTICAST_LID_BASE &&
457 ah_attr->dlid != IPATH_PERMISSIVE_LID ?
458 __constant_cpu_to_be32(IPATH_MULTICAST_QPN) :
Bryan O'Sullivan74ed6b52006-03-29 15:23:34 -0800459 cpu_to_be32(wr->wr.ud.remote_qpn);
460 /* XXX Could lose a PSN count but not worth locking */
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700461 ohdr->bth[2] = cpu_to_be32(qp->s_next_psn++ & IPATH_PSN_MASK);
Bryan O'Sullivan74ed6b52006-03-29 15:23:34 -0800462 /*
463 * Qkeys with the high order bit set mean use the
464 * qkey from the QP context instead of the WR (see 10.2.5).
465 */
466 ohdr->u.ud.deth[0] = cpu_to_be32((int)wr->wr.ud.remote_qkey < 0 ?
467 qp->qkey : wr->wr.ud.remote_qkey);
468 ohdr->u.ud.deth[1] = cpu_to_be32(qp->ibqp.qp_num);
469 if (ipath_verbs_send(dev->dd, hwords, (u32 *) &qp->s_hdr,
470 len, &ss))
471 dev->n_no_piobuf++;
472
473done:
474 /* Queue the completion status entry. */
Ralph Campbell3859e392007-03-15 14:44:51 -0700475 if (!(qp->s_flags & IPATH_S_SIGNAL_REQ_WR) ||
Bryan O'Sullivan74ed6b52006-03-29 15:23:34 -0800476 (wr->send_flags & IB_SEND_SIGNALED)) {
477 wc.wr_id = wr->wr_id;
478 wc.status = IB_WC_SUCCESS;
479 wc.vendor_err = 0;
480 wc.opcode = IB_WC_SEND;
481 wc.byte_len = len;
Michael S. Tsirkin062dbb62006-12-31 21:09:42 +0200482 wc.qp = &qp->ibqp;
Bryan O'Sullivan74ed6b52006-03-29 15:23:34 -0800483 wc.src_qp = 0;
484 wc.wc_flags = 0;
485 /* XXX initialize other fields? */
486 ipath_cq_enter(to_icq(qp->ibqp.send_cq), &wc, 0);
487 }
488 kfree(sg_list);
489
490 ret = 0;
491
492bail:
493 return ret;
494}
495
496/**
497 * ipath_ud_rcv - receive an incoming UD packet
498 * @dev: the device the packet came in on
499 * @hdr: the packet header
500 * @has_grh: true if the packet has a GRH
501 * @data: the packet data
502 * @tlen: the packet length
503 * @qp: the QP the packet came on
504 *
505 * This is called from ipath_qp_rcv() to process an incoming UD packet
506 * for the given QP.
507 * Called at interrupt level.
508 */
509void ipath_ud_rcv(struct ipath_ibdev *dev, struct ipath_ib_header *hdr,
510 int has_grh, void *data, u32 tlen, struct ipath_qp *qp)
511{
512 struct ipath_other_headers *ohdr;
513 int opcode;
514 u32 hdrsize;
515 u32 pad;
Bryan O'Sullivan74ed6b52006-03-29 15:23:34 -0800516 struct ib_wc wc;
517 u32 qkey;
518 u32 src_qp;
Bryan O'Sullivan74ed6b52006-03-29 15:23:34 -0800519 u16 dlid;
520 int header_in_data;
521
522 /* Check for GRH */
523 if (!has_grh) {
524 ohdr = &hdr->u.oth;
525 hdrsize = 8 + 12 + 8; /* LRH + BTH + DETH */
526 qkey = be32_to_cpu(ohdr->u.ud.deth[0]);
527 src_qp = be32_to_cpu(ohdr->u.ud.deth[1]);
528 header_in_data = 0;
529 } else {
530 ohdr = &hdr->u.l.oth;
531 hdrsize = 8 + 40 + 12 + 8; /* LRH + GRH + BTH + DETH */
532 /*
533 * The header with GRH is 68 bytes and the core driver sets
534 * the eager header buffer size to 56 bytes so the last 12
535 * bytes of the IB header is in the data buffer.
536 */
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -0700537 header_in_data = dev->dd->ipath_rcvhdrentsize == 16;
Bryan O'Sullivan74ed6b52006-03-29 15:23:34 -0800538 if (header_in_data) {
539 qkey = be32_to_cpu(((__be32 *) data)[1]);
540 src_qp = be32_to_cpu(((__be32 *) data)[2]);
541 data += 12;
542 } else {
543 qkey = be32_to_cpu(ohdr->u.ud.deth[0]);
544 src_qp = be32_to_cpu(ohdr->u.ud.deth[1]);
545 }
546 }
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700547 src_qp &= IPATH_QPN_MASK;
Bryan O'Sullivan74ed6b52006-03-29 15:23:34 -0800548
549 /*
550 * Check that the permissive LID is only used on QP0
551 * and the QKEY matches (see 9.6.1.4.1 and 9.6.1.5.1).
552 */
553 if (qp->ibqp.qp_num) {
554 if (unlikely(hdr->lrh[1] == IB_LID_PERMISSIVE ||
555 hdr->lrh[3] == IB_LID_PERMISSIVE)) {
556 dev->n_pkt_drops++;
557 goto bail;
558 }
559 if (unlikely(qkey != qp->qkey)) {
560 /* XXX OK to lose a count once in a while. */
561 dev->qkey_violations++;
562 dev->n_pkt_drops++;
563 goto bail;
564 }
565 } else if (hdr->lrh[1] == IB_LID_PERMISSIVE ||
566 hdr->lrh[3] == IB_LID_PERMISSIVE) {
567 struct ib_smp *smp = (struct ib_smp *) data;
568
569 if (smp->mgmt_class != IB_MGMT_CLASS_SUBN_DIRECTED_ROUTE) {
570 dev->n_pkt_drops++;
571 goto bail;
572 }
573 }
574
575 /* Get the number of bytes the message was padded by. */
576 pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
577 if (unlikely(tlen < (hdrsize + pad + 4))) {
578 /* Drop incomplete packets. */
579 dev->n_pkt_drops++;
580 goto bail;
581 }
582 tlen -= hdrsize + pad + 4;
583
584 /* Drop invalid MAD packets (see 13.5.3.1). */
585 if (unlikely((qp->ibqp.qp_num == 0 &&
586 (tlen != 256 ||
587 (be16_to_cpu(hdr->lrh[0]) >> 12) != 15)) ||
588 (qp->ibqp.qp_num == 1 &&
589 (tlen != 256 ||
590 (be16_to_cpu(hdr->lrh[0]) >> 12) == 15)))) {
591 dev->n_pkt_drops++;
592 goto bail;
593 }
594
595 /*
596 * A GRH is expected to preceed the data even if not
597 * present on the wire.
598 */
599 wc.byte_len = tlen + sizeof(struct ib_grh);
600
601 /*
602 * The opcode is in the low byte when its in network order
603 * (top byte when in host order).
604 */
605 opcode = be32_to_cpu(ohdr->bth[0]) >> 24;
606 if (qp->ibqp.qp_num > 1 &&
607 opcode == IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE) {
608 if (header_in_data) {
609 wc.imm_data = *(__be32 *) data;
610 data += sizeof(__be32);
611 } else
612 wc.imm_data = ohdr->u.ud.imm_data;
613 wc.wc_flags = IB_WC_WITH_IMM;
614 hdrsize += sizeof(u32);
615 } else if (opcode == IB_OPCODE_UD_SEND_ONLY) {
616 wc.imm_data = 0;
617 wc.wc_flags = 0;
618 } else {
619 dev->n_pkt_drops++;
620 goto bail;
621 }
622
623 /*
624 * Get the next work request entry to find where to put the data.
Bryan O'Sullivan74ed6b52006-03-29 15:23:34 -0800625 */
Ralph Campbell373d9912006-09-22 15:22:26 -0700626 if (qp->r_reuse_sge)
627 qp->r_reuse_sge = 0;
628 else if (!ipath_get_rwqe(qp, 0)) {
Bryan O'Sullivanfba75202006-07-01 04:36:09 -0700629 /*
630 * Count VL15 packets dropped due to no receive buffer.
631 * Otherwise, count them as buffer overruns since usually,
632 * the HW will be able to receive packets even if there are
633 * no QPs with posted receive buffers.
634 */
635 if (qp->ibqp.qp_num == 0)
636 dev->n_vl15_dropped++;
637 else
638 dev->rcv_errors++;
Bryan O'Sullivan74ed6b52006-03-29 15:23:34 -0800639 goto bail;
640 }
641 /* Silently drop packets which are too big. */
Ralph Campbell373d9912006-09-22 15:22:26 -0700642 if (wc.byte_len > qp->r_len) {
643 qp->r_reuse_sge = 1;
Bryan O'Sullivan74ed6b52006-03-29 15:23:34 -0800644 dev->n_pkt_drops++;
645 goto bail;
646 }
Bryan O'Sullivan74ed6b52006-03-29 15:23:34 -0800647 if (has_grh) {
648 ipath_copy_sge(&qp->r_sge, &hdr->u.l.grh,
649 sizeof(struct ib_grh));
650 wc.wc_flags |= IB_WC_GRH;
651 } else
652 ipath_skip_sge(&qp->r_sge, sizeof(struct ib_grh));
653 ipath_copy_sge(&qp->r_sge, data,
654 wc.byte_len - sizeof(struct ib_grh));
Bryan O'Sullivan041eab92007-03-15 14:44:57 -0700655 qp->r_wrid_valid = 0;
Ralph Campbell373d9912006-09-22 15:22:26 -0700656 wc.wr_id = qp->r_wr_id;
Bryan O'Sullivan74ed6b52006-03-29 15:23:34 -0800657 wc.status = IB_WC_SUCCESS;
658 wc.opcode = IB_WC_RECV;
659 wc.vendor_err = 0;
Michael S. Tsirkin062dbb62006-12-31 21:09:42 +0200660 wc.qp = &qp->ibqp;
Bryan O'Sullivan74ed6b52006-03-29 15:23:34 -0800661 wc.src_qp = src_qp;
662 /* XXX do we know which pkey matched? Only needed for GSI. */
663 wc.pkey_index = 0;
664 wc.slid = be16_to_cpu(hdr->lrh[3]);
665 wc.sl = (be16_to_cpu(hdr->lrh[0]) >> 4) & 0xF;
666 dlid = be16_to_cpu(hdr->lrh[1]);
667 /*
668 * Save the LMC lower bits if the destination LID is a unicast LID.
669 */
Bryan O'Sullivan27b678d2006-07-01 04:36:17 -0700670 wc.dlid_path_bits = dlid >= IPATH_MULTICAST_LID_BASE ? 0 :
Bryan O'Sullivan74ed6b52006-03-29 15:23:34 -0800671 dlid & ((1 << (dev->mkeyprot_resv_lmc & 7)) - 1);
672 /* Signal completion event if the solicited bit is set. */
673 ipath_cq_enter(to_icq(qp->ibqp.recv_cq), &wc,
674 (ohdr->bth[0] &
675 __constant_cpu_to_be32(1 << 23)) != 0);
676
677bail:;
678}