2 * Copyright (c) 2006, 2007, 2008, 2009, 2010 QLogic Corporation.
4 * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
37 /* cut down ridiculously long IB macro names */
38 #define OP(x) IB_OPCODE_UC_##x
41 * qib_make_uc_req - construct a request packet (SEND, RDMA write)
42 * @qp: a pointer to the QP
45 * Assumes the s_lock is held.
47 * Return 1 if constructed; otherwise, return 0.
49 int qib_make_uc_req(struct rvt_qp *qp, unsigned long *flags)
51 struct qib_qp_priv *priv = qp->priv;
52 struct ib_other_headers *ohdr;
60 if (!(ib_rvt_state_ops[qp->state] & RVT_PROCESS_SEND_OK)) {
61 if (!(ib_rvt_state_ops[qp->state] & RVT_FLUSH_SEND))
63 /* We are in the error state, flush the work request. */
64 if (qp->s_last == READ_ONCE(qp->s_head))
66 /* If DMAs are in progress, we can't flush immediately. */
67 if (atomic_read(&priv->s_dma_busy)) {
68 qp->s_flags |= RVT_S_WAIT_DMA;
71 wqe = rvt_get_swqe_ptr(qp, qp->s_last);
72 rvt_send_complete(qp, wqe, IB_WC_WR_FLUSH_ERR);
76 ohdr = &priv->s_hdr->u.oth;
77 if (rdma_ah_get_ah_flags(&qp->remote_ah_attr) & IB_AH_GRH)
78 ohdr = &priv->s_hdr->u.l.oth;
80 /* header size in 32-bit words LRH+BTH = (8+12)/4. */
84 /* Get the next send request. */
85 wqe = rvt_get_swqe_ptr(qp, qp->s_cur);
87 switch (qp->s_state) {
89 if (!(ib_rvt_state_ops[qp->state] &
90 RVT_PROCESS_NEXT_SEND_OK))
92 /* Check if send work queue is empty. */
93 if (qp->s_cur == READ_ONCE(qp->s_head))
96 * Start a new request.
99 qp->s_sge.sge = wqe->sg_list[0];
100 qp->s_sge.sg_list = wqe->sg_list + 1;
101 qp->s_sge.num_sge = wqe->wr.num_sge;
102 qp->s_sge.total_len = wqe->length;
105 switch (wqe->wr.opcode) {
107 case IB_WR_SEND_WITH_IMM:
109 qp->s_state = OP(SEND_FIRST);
113 if (wqe->wr.opcode == IB_WR_SEND)
114 qp->s_state = OP(SEND_ONLY);
117 OP(SEND_ONLY_WITH_IMMEDIATE);
118 /* Immediate data comes after the BTH */
119 ohdr->u.imm_data = wqe->wr.ex.imm_data;
122 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
123 bth0 |= IB_BTH_SOLICITED;
125 if (++qp->s_cur >= qp->s_size)
129 case IB_WR_RDMA_WRITE:
130 case IB_WR_RDMA_WRITE_WITH_IMM:
131 ohdr->u.rc.reth.vaddr =
132 cpu_to_be64(wqe->rdma_wr.remote_addr);
133 ohdr->u.rc.reth.rkey =
134 cpu_to_be32(wqe->rdma_wr.rkey);
135 ohdr->u.rc.reth.length = cpu_to_be32(len);
136 hwords += sizeof(struct ib_reth) / 4;
138 qp->s_state = OP(RDMA_WRITE_FIRST);
142 if (wqe->wr.opcode == IB_WR_RDMA_WRITE)
143 qp->s_state = OP(RDMA_WRITE_ONLY);
146 OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE);
147 /* Immediate data comes after the RETH */
148 ohdr->u.rc.imm_data = wqe->wr.ex.imm_data;
150 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
151 bth0 |= IB_BTH_SOLICITED;
154 if (++qp->s_cur >= qp->s_size)
164 qp->s_state = OP(SEND_MIDDLE);
166 case OP(SEND_MIDDLE):
172 if (wqe->wr.opcode == IB_WR_SEND)
173 qp->s_state = OP(SEND_LAST);
175 qp->s_state = OP(SEND_LAST_WITH_IMMEDIATE);
176 /* Immediate data comes after the BTH */
177 ohdr->u.imm_data = wqe->wr.ex.imm_data;
180 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
181 bth0 |= IB_BTH_SOLICITED;
183 if (++qp->s_cur >= qp->s_size)
187 case OP(RDMA_WRITE_FIRST):
188 qp->s_state = OP(RDMA_WRITE_MIDDLE);
190 case OP(RDMA_WRITE_MIDDLE):
196 if (wqe->wr.opcode == IB_WR_RDMA_WRITE)
197 qp->s_state = OP(RDMA_WRITE_LAST);
200 OP(RDMA_WRITE_LAST_WITH_IMMEDIATE);
201 /* Immediate data comes after the BTH */
202 ohdr->u.imm_data = wqe->wr.ex.imm_data;
204 if (wqe->wr.send_flags & IB_SEND_SOLICITED)
205 bth0 |= IB_BTH_SOLICITED;
208 if (++qp->s_cur >= qp->s_size)
213 qp->s_hdrwords = hwords;
214 qp->s_cur_sge = &qp->s_sge;
215 qp->s_cur_size = len;
216 qib_make_ruc_header(qp, ohdr, bth0 | (qp->s_state << 24),
217 qp->s_psn++ & QIB_PSN_MASK);
221 qp->s_flags &= ~RVT_S_BUSY;
226 * qib_uc_rcv - handle an incoming UC packet
227 * @ibp: the port the packet came in on
228 * @hdr: the header of the packet
229 * @has_grh: true if the packet has a GRH
230 * @data: the packet data
231 * @tlen: the length of the packet
232 * @qp: the QP for this packet.
234 * This is called from qib_qp_rcv() to process an incoming UC packet
236 * Called at interrupt level.
238 void qib_uc_rcv(struct qib_ibport *ibp, struct ib_header *hdr,
239 int has_grh, void *data, u32 tlen, struct rvt_qp *qp)
241 struct ib_other_headers *ohdr;
248 struct ib_reth *reth;
254 hdrsize = 8 + 12; /* LRH + BTH */
256 ohdr = &hdr->u.l.oth;
257 hdrsize = 8 + 40 + 12; /* LRH + GRH + BTH */
260 opcode = be32_to_cpu(ohdr->bth[0]);
261 if (qib_ruc_check_hdr(ibp, hdr, has_grh, qp, opcode))
264 psn = be32_to_cpu(ohdr->bth[2]);
267 /* Compare the PSN verses the expected PSN. */
268 if (unlikely(qib_cmp24(psn, qp->r_psn) != 0)) {
270 * Handle a sequence error.
271 * Silently drop any current message.
275 if (qp->r_state == OP(SEND_FIRST) ||
276 qp->r_state == OP(SEND_MIDDLE)) {
277 set_bit(RVT_R_REWIND_SGE, &qp->r_aflags);
278 qp->r_sge.num_sge = 0;
280 rvt_put_ss(&qp->r_sge);
281 qp->r_state = OP(SEND_LAST);
285 case OP(SEND_ONLY_WITH_IMMEDIATE):
288 case OP(RDMA_WRITE_FIRST):
289 case OP(RDMA_WRITE_ONLY):
290 case OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE):
298 /* Check for opcode sequence errors. */
299 switch (qp->r_state) {
301 case OP(SEND_MIDDLE):
302 if (opcode == OP(SEND_MIDDLE) ||
303 opcode == OP(SEND_LAST) ||
304 opcode == OP(SEND_LAST_WITH_IMMEDIATE))
308 case OP(RDMA_WRITE_FIRST):
309 case OP(RDMA_WRITE_MIDDLE):
310 if (opcode == OP(RDMA_WRITE_MIDDLE) ||
311 opcode == OP(RDMA_WRITE_LAST) ||
312 opcode == OP(RDMA_WRITE_LAST_WITH_IMMEDIATE))
317 if (opcode == OP(SEND_FIRST) ||
318 opcode == OP(SEND_ONLY) ||
319 opcode == OP(SEND_ONLY_WITH_IMMEDIATE) ||
320 opcode == OP(RDMA_WRITE_FIRST) ||
321 opcode == OP(RDMA_WRITE_ONLY) ||
322 opcode == OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE))
327 if (qp->state == IB_QPS_RTR && !(qp->r_flags & RVT_R_COMM_EST))
330 /* OK, process the packet. */
334 case OP(SEND_ONLY_WITH_IMMEDIATE):
336 if (test_and_clear_bit(RVT_R_REWIND_SGE, &qp->r_aflags))
337 qp->r_sge = qp->s_rdma_read_sge;
339 ret = rvt_get_rwqe(qp, false);
345 * qp->s_rdma_read_sge will be the owner
346 * of the mr references.
348 qp->s_rdma_read_sge = qp->r_sge;
351 if (opcode == OP(SEND_ONLY))
352 goto no_immediate_data;
353 else if (opcode == OP(SEND_ONLY_WITH_IMMEDIATE))
356 case OP(SEND_MIDDLE):
357 /* Check for invalid length PMTU or posted rwqe len. */
358 if (unlikely(tlen != (hdrsize + pmtu + 4)))
360 qp->r_rcv_len += pmtu;
361 if (unlikely(qp->r_rcv_len > qp->r_len))
363 rvt_copy_sge(qp, &qp->r_sge, data, pmtu, false, false);
366 case OP(SEND_LAST_WITH_IMMEDIATE):
368 wc.ex.imm_data = ohdr->u.imm_data;
370 wc.wc_flags = IB_WC_WITH_IMM;
377 /* Get the number of bytes the message was padded by. */
378 pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
379 /* Check for invalid length. */
380 /* XXX LAST len should be >= 1 */
381 if (unlikely(tlen < (hdrsize + pad + 4)))
383 /* Don't count the CRC. */
384 tlen -= (hdrsize + pad + 4);
385 wc.byte_len = tlen + qp->r_rcv_len;
386 if (unlikely(wc.byte_len > qp->r_len))
388 wc.opcode = IB_WC_RECV;
389 rvt_copy_sge(qp, &qp->r_sge, data, tlen, false, false);
390 rvt_put_ss(&qp->s_rdma_read_sge);
392 wc.wr_id = qp->r_wr_id;
393 wc.status = IB_WC_SUCCESS;
395 wc.src_qp = qp->remote_qpn;
396 wc.slid = rdma_ah_get_dlid(&qp->remote_ah_attr);
397 wc.sl = rdma_ah_get_sl(&qp->remote_ah_attr);
398 /* zero fields that are N/A */
401 wc.dlid_path_bits = 0;
403 /* Signal completion event if the solicited bit is set. */
404 rvt_recv_cq(qp, &wc, ib_bth_is_solicited(ohdr));
407 case OP(RDMA_WRITE_FIRST):
408 case OP(RDMA_WRITE_ONLY):
409 case OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE): /* consume RWQE */
411 if (unlikely(!(qp->qp_access_flags &
412 IB_ACCESS_REMOTE_WRITE))) {
415 reth = &ohdr->u.rc.reth;
416 hdrsize += sizeof(*reth);
417 qp->r_len = be32_to_cpu(reth->length);
419 qp->r_sge.sg_list = NULL;
420 if (qp->r_len != 0) {
421 u32 rkey = be32_to_cpu(reth->rkey);
422 u64 vaddr = be64_to_cpu(reth->vaddr);
426 ok = rvt_rkey_ok(qp, &qp->r_sge.sge, qp->r_len,
427 vaddr, rkey, IB_ACCESS_REMOTE_WRITE);
430 qp->r_sge.num_sge = 1;
432 qp->r_sge.num_sge = 0;
433 qp->r_sge.sge.mr = NULL;
434 qp->r_sge.sge.vaddr = NULL;
435 qp->r_sge.sge.length = 0;
436 qp->r_sge.sge.sge_length = 0;
438 if (opcode == OP(RDMA_WRITE_ONLY))
440 else if (opcode == OP(RDMA_WRITE_ONLY_WITH_IMMEDIATE)) {
441 wc.ex.imm_data = ohdr->u.rc.imm_data;
445 case OP(RDMA_WRITE_MIDDLE):
446 /* Check for invalid length PMTU or posted rwqe len. */
447 if (unlikely(tlen != (hdrsize + pmtu + 4)))
449 qp->r_rcv_len += pmtu;
450 if (unlikely(qp->r_rcv_len > qp->r_len))
452 rvt_copy_sge(qp, &qp->r_sge, data, pmtu, true, false);
455 case OP(RDMA_WRITE_LAST_WITH_IMMEDIATE):
456 wc.ex.imm_data = ohdr->u.imm_data;
459 wc.wc_flags = IB_WC_WITH_IMM;
461 /* Get the number of bytes the message was padded by. */
462 pad = (be32_to_cpu(ohdr->bth[0]) >> 20) & 3;
463 /* Check for invalid length. */
464 /* XXX LAST len should be >= 1 */
465 if (unlikely(tlen < (hdrsize + pad + 4)))
467 /* Don't count the CRC. */
468 tlen -= (hdrsize + pad + 4);
469 if (unlikely(tlen + qp->r_rcv_len != qp->r_len))
471 if (test_and_clear_bit(RVT_R_REWIND_SGE, &qp->r_aflags))
472 rvt_put_ss(&qp->s_rdma_read_sge);
474 ret = rvt_get_rwqe(qp, true);
480 wc.byte_len = qp->r_len;
481 wc.opcode = IB_WC_RECV_RDMA_WITH_IMM;
482 rvt_copy_sge(qp, &qp->r_sge, data, tlen, true, false);
483 rvt_put_ss(&qp->r_sge);
486 case OP(RDMA_WRITE_LAST):
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)))
494 /* Don't count the CRC. */
495 tlen -= (hdrsize + pad + 4);
496 if (unlikely(tlen + qp->r_rcv_len != qp->r_len))
498 rvt_copy_sge(qp, &qp->r_sge, data, tlen, true, false);
499 rvt_put_ss(&qp->r_sge);
503 /* Drop packet for unknown opcodes. */
507 qp->r_state = opcode;
511 set_bit(RVT_R_REWIND_SGE, &qp->r_aflags);
512 qp->r_sge.num_sge = 0;
514 ibp->rvp.n_pkt_drops++;
518 rvt_rc_error(qp, IB_WC_LOC_QP_OP_ERR);