2 * Copyright (c) 2006, 2007 QLogic Corporation. All rights reserved.
3 * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.
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:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
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.
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
34 #include "ipath_verbs.h"
35 #include "ipath_kernel.h"
37 /* cut down ridiculously long IB macro names */
38 #define OP(x) IB_OPCODE_UC_##x
40 static void complete_last_send(struct ipath_qp *qp, struct ipath_swqe *wqe,
43 if (++qp->s_last == qp->s_size)
45 if (!(qp->s_flags & IPATH_S_SIGNAL_REQ_WR) ||
46 (wqe->wr.send_flags & IB_SEND_SIGNALED)) {
47 wc->wr_id = wqe->wr.wr_id;
48 wc->status = IB_WC_SUCCESS;
49 wc->opcode = ib_ipath_wc_opcode[wqe->wr.opcode];
51 wc->byte_len = wqe->length;
53 wc->src_qp = qp->remote_qpn;
55 wc->slid = qp->remote_ah_attr.dlid;
56 wc->sl = qp->remote_ah_attr.sl;
57 wc->dlid_path_bits = 0;
59 ipath_cq_enter(to_icq(qp->ibqp.send_cq), wc, 0);
64 * ipath_make_uc_req - construct a request packet (SEND, RDMA write)
65 * @qp: a pointer to the QP
66 * @ohdr: a pointer to the IB header being constructed
68 * @bth0p: pointer to the BTH opcode word
69 * @bth2p: pointer to the BTH PSN word
71 * Return 1 if constructed; otherwise, return 0.
72 * Note the QP s_lock must be held and interrupts disabled.
74 int ipath_make_uc_req(struct ipath_qp *qp,
75 struct ipath_other_headers *ohdr,
76 u32 pmtu, u32 *bth0p, u32 *bth2p)
78 struct ipath_swqe *wqe;
84 if (!(ib_ipath_state_ops[qp->state] & IPATH_PROCESS_SEND_OK))
87 /* header size in 32-bit words LRH+BTH = (8+12)/4. */
89 bth0 = 1 << 22; /* Set M bit */
91 /* Get the next send request. */
92 wqe = get_swqe_ptr(qp, qp->s_last);
93 switch (qp->s_state) {
96 * Signal the completion of the last send
99 if (qp->s_last != qp->s_tail) {
100 complete_last_send(qp, wqe, &wc);
101 wqe = get_swqe_ptr(qp, qp->s_last);
104 /* Check if send work queue is empty. */
105 if (qp->s_tail == qp->s_head)
108 * Start a new request.
110 qp->s_psn = wqe->psn = qp->s_next_psn;
111 qp->s_sge.sge = wqe->sg_list[0];
112 qp->s_sge.sg_list = wqe->sg_list + 1;
113 qp->s_sge.num_sge = wqe->wr.num_sge;
114 qp->s_len = len = wqe->length;
115 switch (wqe->wr.opcode) {
117 case IB_WR_SEND_WITH_IMM:
119 qp->s_state = OP(SEND_FIRST);
123 if (wqe->wr.opcode == IB_WR_SEND)
124 qp->s_state = OP(SEND_ONLY);
127 OP(SEND_ONLY_WITH_IMMEDIATE);
128 /* Immediate data comes after the BTH */
129 ohdr->u.imm_data = wqe->wr.imm_data;
132 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
136 case IB_WR_RDMA_WRITE:
137 case IB_WR_RDMA_WRITE_WITH_IMM:
138 ohdr->u.rc.reth.vaddr =
139 cpu_to_be64(wqe->wr.wr.rdma.remote_addr);
140 ohdr->u.rc.reth.rkey =
141 cpu_to_be32(wqe->wr.wr.rdma.rkey);
142 ohdr->u.rc.reth.length = cpu_to_be32(len);
143 hwords += sizeof(struct ib_reth) / 4;
145 qp->s_state = OP(RDMA_WRITE_FIRST);
149 if (wqe->wr.opcode == IB_WR_RDMA_WRITE)
150 qp->s_state = OP(RDMA_WRITE_ONLY);
153 OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE);
154 /* Immediate data comes after the RETH */
155 ohdr->u.rc.imm_data = wqe->wr.imm_data;
157 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
165 if (++qp->s_tail >= qp->s_size)
170 qp->s_state = OP(SEND_MIDDLE);
172 case OP(SEND_MIDDLE):
178 if (wqe->wr.opcode == IB_WR_SEND)
179 qp->s_state = OP(SEND_LAST);
181 qp->s_state = OP(SEND_LAST_WITH_IMMEDIATE);
182 /* Immediate data comes after the BTH */
183 ohdr->u.imm_data = wqe->wr.imm_data;
186 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
190 case OP(RDMA_WRITE_FIRST):
191 qp->s_state = OP(RDMA_WRITE_MIDDLE);
193 case OP(RDMA_WRITE_MIDDLE):
199 if (wqe->wr.opcode == IB_WR_RDMA_WRITE)
200 qp->s_state = OP(RDMA_WRITE_LAST);
203 OP(RDMA_WRITE_LAST_WITH_IMMEDIATE);
204 /* Immediate data comes after the BTH */
205 ohdr->u.imm_data = wqe->wr.imm_data;
207 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
213 qp->s_hdrwords = hwords;
214 qp->s_cur_sge = &qp->s_sge;
215 qp->s_cur_size = len;
216 *bth0p = bth0 | (qp->s_state << 24);
217 *bth2p = qp->s_next_psn++ & IPATH_PSN_MASK;
225 * ipath_uc_rcv - handle an incoming UC packet
226 * @dev: the device the packet came in on
227 * @hdr: the header of the packet
228 * @has_grh: true if the packet has a GRH
229 * @data: the packet data
230 * @tlen: the length of the packet
231 * @qp: the QP for this packet.
233 * This is called from ipath_qp_rcv() to process an incoming UC packet
235 * Called at interrupt level.
237 void ipath_uc_rcv(struct ipath_ibdev *dev, struct ipath_ib_header *hdr,
238 int has_grh, void *data, u32 tlen, struct ipath_qp *qp)
240 struct ipath_other_headers *ohdr;
246 u32 pmtu = ib_mtu_enum_to_int(qp->path_mtu);
247 struct ib_reth *reth;
250 /* Validate the SLID. See Ch. 9.6.1.5 */
251 if (unlikely(be16_to_cpu(hdr->lrh[3]) != qp->remote_ah_attr.dlid))
257 hdrsize = 8 + 12; /* LRH + BTH */
258 psn = be32_to_cpu(ohdr->bth[2]);
261 ohdr = &hdr->u.l.oth;
262 hdrsize = 8 + 40 + 12; /* LRH + GRH + BTH */
264 * The header with GRH is 60 bytes and the
265 * core driver sets the eager header buffer
266 * size to 56 bytes so the last 4 bytes of
267 * the BTH header (PSN) is in the data buffer.
269 header_in_data = dev->dd->ipath_rcvhdrentsize == 16;
270 if (header_in_data) {
271 psn = be32_to_cpu(((__be32 *) data)[0]);
272 data += sizeof(__be32);
274 psn = be32_to_cpu(ohdr->bth[2]);
277 * The opcode is in the low byte when its in network order
278 * (top byte when in host order).
280 opcode = be32_to_cpu(ohdr->bth[0]) >> 24;
285 /* Compare the PSN verses the expected PSN. */
286 if (unlikely(ipath_cmp24(psn, qp->r_psn) != 0)) {
288 * Handle a sequence error.
289 * Silently drop any current message.
293 qp->r_state = OP(SEND_LAST);
297 case OP(SEND_ONLY_WITH_IMMEDIATE):
300 case OP(RDMA_WRITE_FIRST):
301 case OP(RDMA_WRITE_ONLY):
302 case OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE):
311 /* Check for opcode sequence errors. */
312 switch (qp->r_state) {
314 case OP(SEND_MIDDLE):
315 if (opcode == OP(SEND_MIDDLE) ||
316 opcode == OP(SEND_LAST) ||
317 opcode == OP(SEND_LAST_WITH_IMMEDIATE))
321 case OP(RDMA_WRITE_FIRST):
322 case OP(RDMA_WRITE_MIDDLE):
323 if (opcode == OP(RDMA_WRITE_MIDDLE) ||
324 opcode == OP(RDMA_WRITE_LAST) ||
325 opcode == OP(RDMA_WRITE_LAST_WITH_IMMEDIATE))
330 if (opcode == OP(SEND_FIRST) ||
331 opcode == OP(SEND_ONLY) ||
332 opcode == OP(SEND_ONLY_WITH_IMMEDIATE) ||
333 opcode == OP(RDMA_WRITE_FIRST) ||
334 opcode == OP(RDMA_WRITE_ONLY) ||
335 opcode == OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE))
340 /* OK, process the packet. */
344 case OP(SEND_ONLY_WITH_IMMEDIATE):
346 if (qp->r_reuse_sge) {
348 qp->r_sge = qp->s_rdma_read_sge;
349 } else if (!ipath_get_rwqe(qp, 0)) {
353 /* Save the WQE so we can reuse it in case of an error. */
354 qp->s_rdma_read_sge = qp->r_sge;
356 if (opcode == OP(SEND_ONLY))
358 else if (opcode == OP(SEND_ONLY_WITH_IMMEDIATE))
361 case OP(SEND_MIDDLE):
362 /* Check for invalid length PMTU or posted rwqe len. */
363 if (unlikely(tlen != (hdrsize + pmtu + 4))) {
368 qp->r_rcv_len += pmtu;
369 if (unlikely(qp->r_rcv_len > qp->r_len)) {
374 ipath_copy_sge(&qp->r_sge, data, pmtu);
377 case OP(SEND_LAST_WITH_IMMEDIATE):
379 if (header_in_data) {
380 wc.imm_data = *(__be32 *) data;
381 data += sizeof(__be32);
383 /* Immediate data comes after BTH */
384 wc.imm_data = ohdr->u.imm_data;
387 wc.wc_flags = IB_WC_WITH_IMM;
391 /* Get the number of bytes the message was padded by. */
392 pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
393 /* Check for invalid length. */
394 /* XXX LAST len should be >= 1 */
395 if (unlikely(tlen < (hdrsize + pad + 4))) {
400 /* Don't count the CRC. */
401 tlen -= (hdrsize + pad + 4);
402 wc.byte_len = tlen + qp->r_rcv_len;
403 if (unlikely(wc.byte_len > qp->r_len)) {
408 /* XXX Need to free SGEs */
410 ipath_copy_sge(&qp->r_sge, data, tlen);
411 wc.wr_id = qp->r_wr_id;
412 wc.status = IB_WC_SUCCESS;
413 wc.opcode = IB_WC_RECV;
416 wc.src_qp = qp->remote_qpn;
418 wc.slid = qp->remote_ah_attr.dlid;
419 wc.sl = qp->remote_ah_attr.sl;
420 wc.dlid_path_bits = 0;
422 /* Signal completion event if the solicited bit is set. */
423 ipath_cq_enter(to_icq(qp->ibqp.recv_cq), &wc,
425 __constant_cpu_to_be32(1 << 23)) != 0);
428 case OP(RDMA_WRITE_FIRST):
429 case OP(RDMA_WRITE_ONLY):
430 case OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE): /* consume RWQE */
432 /* RETH comes after BTH */
434 reth = &ohdr->u.rc.reth;
436 reth = (struct ib_reth *)data;
437 data += sizeof(*reth);
439 hdrsize += sizeof(*reth);
440 qp->r_len = be32_to_cpu(reth->length);
442 if (qp->r_len != 0) {
443 u32 rkey = be32_to_cpu(reth->rkey);
444 u64 vaddr = be64_to_cpu(reth->vaddr);
448 ok = ipath_rkey_ok(qp, &qp->r_sge, qp->r_len,
450 IB_ACCESS_REMOTE_WRITE);
456 qp->r_sge.sg_list = NULL;
457 qp->r_sge.sge.mr = NULL;
458 qp->r_sge.sge.vaddr = NULL;
459 qp->r_sge.sge.length = 0;
460 qp->r_sge.sge.sge_length = 0;
462 if (unlikely(!(qp->qp_access_flags &
463 IB_ACCESS_REMOTE_WRITE))) {
467 if (opcode == OP(RDMA_WRITE_ONLY))
469 else if (opcode == OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE))
472 case OP(RDMA_WRITE_MIDDLE):
473 /* Check for invalid length PMTU or posted rwqe len. */
474 if (unlikely(tlen != (hdrsize + pmtu + 4))) {
478 qp->r_rcv_len += pmtu;
479 if (unlikely(qp->r_rcv_len > qp->r_len)) {
483 ipath_copy_sge(&qp->r_sge, data, pmtu);
486 case OP(RDMA_WRITE_LAST_WITH_IMMEDIATE):
488 /* Get the number of bytes the message was padded by. */
489 pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
490 /* Check for invalid length. */
491 /* XXX LAST len should be >= 1 */
492 if (unlikely(tlen < (hdrsize + pad + 4))) {
496 /* Don't count the CRC. */
497 tlen -= (hdrsize + pad + 4);
498 if (unlikely(tlen + qp->r_rcv_len != qp->r_len)) {
504 else if (!ipath_get_rwqe(qp, 1)) {
508 if (header_in_data) {
509 wc.imm_data = *(__be32 *) data;
510 data += sizeof(__be32);
512 /* Immediate data comes after BTH */
513 wc.imm_data = ohdr->u.imm_data;
516 wc.wc_flags = IB_WC_WITH_IMM;
520 case OP(RDMA_WRITE_LAST):
522 /* Get the number of bytes the message was padded by. */
523 pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
524 /* Check for invalid length. */
525 /* XXX LAST len should be >= 1 */
526 if (unlikely(tlen < (hdrsize + pad + 4))) {
530 /* Don't count the CRC. */
531 tlen -= (hdrsize + pad + 4);
532 if (unlikely(tlen + qp->r_rcv_len != qp->r_len)) {
536 ipath_copy_sge(&qp->r_sge, data, tlen);
540 /* Drop packet for unknown opcodes. */
545 qp->r_state = opcode;