xref: /onnv-gate/usr/src/uts/common/io/ib/clients/rdsv3/ib_send.c (revision 12863:5783b6fe7ed5)
112198SEiji.Ota@Sun.COM /*
212198SEiji.Ota@Sun.COM  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
312198SEiji.Ota@Sun.COM  */
412198SEiji.Ota@Sun.COM 
512198SEiji.Ota@Sun.COM /*
612763SGiri.Adari@Sun.COM  * This file contains code imported from the OFED rds source file ib_send.c
712763SGiri.Adari@Sun.COM  * Oracle elects to have and use the contents of ib_send.c under and governed
812763SGiri.Adari@Sun.COM  * by the OpenIB.org BSD license (see below for full license text). However,
912763SGiri.Adari@Sun.COM  * the following notice accompanied the original version of this file:
1012763SGiri.Adari@Sun.COM  */
1112763SGiri.Adari@Sun.COM 
1212763SGiri.Adari@Sun.COM /*
1312198SEiji.Ota@Sun.COM  * Copyright (c) 2006 Oracle.  All rights reserved.
1412198SEiji.Ota@Sun.COM  *
1512198SEiji.Ota@Sun.COM  * This software is available to you under a choice of one of two
1612198SEiji.Ota@Sun.COM  * licenses.  You may choose to be licensed under the terms of the GNU
1712198SEiji.Ota@Sun.COM  * General Public License (GPL) Version 2, available from the file
1812198SEiji.Ota@Sun.COM  * COPYING in the main directory of this source tree, or the
1912198SEiji.Ota@Sun.COM  * OpenIB.org BSD license below:
2012198SEiji.Ota@Sun.COM  *
2112198SEiji.Ota@Sun.COM  *     Redistribution and use in source and binary forms, with or
2212198SEiji.Ota@Sun.COM  *     without modification, are permitted provided that the following
2312198SEiji.Ota@Sun.COM  *     conditions are met:
2412198SEiji.Ota@Sun.COM  *
2512198SEiji.Ota@Sun.COM  *      - Redistributions of source code must retain the above
2612198SEiji.Ota@Sun.COM  *        copyright notice, this list of conditions and the following
2712198SEiji.Ota@Sun.COM  *        disclaimer.
2812198SEiji.Ota@Sun.COM  *
2912198SEiji.Ota@Sun.COM  *      - Redistributions in binary form must reproduce the above
3012198SEiji.Ota@Sun.COM  *        copyright notice, this list of conditions and the following
3112198SEiji.Ota@Sun.COM  *        disclaimer in the documentation and/or other materials
3212198SEiji.Ota@Sun.COM  *        provided with the distribution.
3312198SEiji.Ota@Sun.COM  *
3412198SEiji.Ota@Sun.COM  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
3512198SEiji.Ota@Sun.COM  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
3612198SEiji.Ota@Sun.COM  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
3712198SEiji.Ota@Sun.COM  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
3812198SEiji.Ota@Sun.COM  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
3912198SEiji.Ota@Sun.COM  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
4012198SEiji.Ota@Sun.COM  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
4112198SEiji.Ota@Sun.COM  * SOFTWARE.
4212198SEiji.Ota@Sun.COM  *
4312198SEiji.Ota@Sun.COM  */
4412198SEiji.Ota@Sun.COM #include <sys/rds.h>
4512198SEiji.Ota@Sun.COM 
4612198SEiji.Ota@Sun.COM #include <sys/ib/clients/rdsv3/rdsv3.h>
4712198SEiji.Ota@Sun.COM #include <sys/ib/clients/rdsv3/rdma.h>
4812198SEiji.Ota@Sun.COM #include <sys/ib/clients/rdsv3/ib.h>
4912198SEiji.Ota@Sun.COM #include <sys/ib/clients/rdsv3/rdsv3_debug.h>
5012198SEiji.Ota@Sun.COM 
5112198SEiji.Ota@Sun.COM static void
rdsv3_ib_send_rdma_complete(struct rdsv3_message * rm,int wc_status)5212198SEiji.Ota@Sun.COM rdsv3_ib_send_rdma_complete(struct rdsv3_message *rm,
5312198SEiji.Ota@Sun.COM     int wc_status)
5412198SEiji.Ota@Sun.COM {
5512198SEiji.Ota@Sun.COM 	int notify_status;
5612198SEiji.Ota@Sun.COM 
5712198SEiji.Ota@Sun.COM 	RDSV3_DPRINTF4("rdsv3_ib_send_rdma_complete", "rm: %p, wc_status: %d",
5812198SEiji.Ota@Sun.COM 	    rm, wc_status);
5912198SEiji.Ota@Sun.COM 
6012198SEiji.Ota@Sun.COM 	switch (wc_status) {
6112198SEiji.Ota@Sun.COM 	case IBT_WC_WR_FLUSHED_ERR:
6212198SEiji.Ota@Sun.COM 		return;
6312198SEiji.Ota@Sun.COM 
6412198SEiji.Ota@Sun.COM 	case IBT_WC_SUCCESS:
65*12863SEiji.Ota@Sun.COM 		notify_status = RDS_RDMA_SUCCESS;
6612198SEiji.Ota@Sun.COM 		break;
6712198SEiji.Ota@Sun.COM 
6812198SEiji.Ota@Sun.COM 	case IBT_WC_REMOTE_ACCESS_ERR:
69*12863SEiji.Ota@Sun.COM 		notify_status = RDS_RDMA_REMOTE_ERROR;
7012198SEiji.Ota@Sun.COM 		break;
7112198SEiji.Ota@Sun.COM 
7212198SEiji.Ota@Sun.COM 	default:
73*12863SEiji.Ota@Sun.COM 		notify_status = RDS_RDMA_OTHER_ERROR;
7412198SEiji.Ota@Sun.COM 		break;
7512198SEiji.Ota@Sun.COM 	}
7612198SEiji.Ota@Sun.COM 	rdsv3_rdma_send_complete(rm, notify_status);
7712198SEiji.Ota@Sun.COM 
7812198SEiji.Ota@Sun.COM 	RDSV3_DPRINTF4("rdsv3_ib_send_rdma_complete", "rm: %p, wc_status: %d",
7912198SEiji.Ota@Sun.COM 	    rm, wc_status);
8012198SEiji.Ota@Sun.COM }
8112198SEiji.Ota@Sun.COM 
8212198SEiji.Ota@Sun.COM static void rdsv3_ib_dma_unmap_sg_rdma(struct ib_device *dev,
8312198SEiji.Ota@Sun.COM     uint_t num, struct rdsv3_rdma_sg scat[]);
8412198SEiji.Ota@Sun.COM 
8512198SEiji.Ota@Sun.COM void
rdsv3_ib_send_unmap_rdma(struct rdsv3_ib_connection * ic,struct rdsv3_rdma_op * op)8612198SEiji.Ota@Sun.COM rdsv3_ib_send_unmap_rdma(struct rdsv3_ib_connection *ic,
8712198SEiji.Ota@Sun.COM     struct rdsv3_rdma_op *op)
8812198SEiji.Ota@Sun.COM {
8912198SEiji.Ota@Sun.COM 	RDSV3_DPRINTF4("rdsv3_ib_send_unmap_rdma", "ic: %p, op: %p", ic, op);
9012198SEiji.Ota@Sun.COM 	if (op->r_mapped) {
9112198SEiji.Ota@Sun.COM 		op->r_mapped = 0;
9212198SEiji.Ota@Sun.COM 		if (ic->i_cm_id) {
9312198SEiji.Ota@Sun.COM 			rdsv3_ib_dma_unmap_sg_rdma(ic->i_cm_id->device,
9412198SEiji.Ota@Sun.COM 			    op->r_nents, op->r_rdma_sg);
9512198SEiji.Ota@Sun.COM 		} else {
9612198SEiji.Ota@Sun.COM 			rdsv3_ib_dma_unmap_sg_rdma((struct ib_device *)NULL,
9712198SEiji.Ota@Sun.COM 			    op->r_nents, op->r_rdma_sg);
9812198SEiji.Ota@Sun.COM 		}
9912198SEiji.Ota@Sun.COM 	}
10012198SEiji.Ota@Sun.COM }
10112198SEiji.Ota@Sun.COM 
10212198SEiji.Ota@Sun.COM static void
rdsv3_ib_send_unmap_rm(struct rdsv3_ib_connection * ic,struct rdsv3_ib_send_work * send,int wc_status)10312198SEiji.Ota@Sun.COM rdsv3_ib_send_unmap_rm(struct rdsv3_ib_connection *ic,
10412198SEiji.Ota@Sun.COM     struct rdsv3_ib_send_work *send,
10512198SEiji.Ota@Sun.COM     int wc_status)
10612198SEiji.Ota@Sun.COM {
10712198SEiji.Ota@Sun.COM 	struct rdsv3_message *rm = send->s_rm;
10812198SEiji.Ota@Sun.COM 
10912198SEiji.Ota@Sun.COM 	RDSV3_DPRINTF4("rdsv3_ib_send_unmap_rm", "ic %p send %p rm %p\n",
11012198SEiji.Ota@Sun.COM 	    ic, send, rm);
11112198SEiji.Ota@Sun.COM 
11212676SEiji.Ota@Sun.COM 	mutex_enter(&rm->m_rs_lock);
11312676SEiji.Ota@Sun.COM 	if (rm->m_count) {
11412676SEiji.Ota@Sun.COM 		rdsv3_ib_dma_unmap_sg(ic->i_cm_id->device,
11512676SEiji.Ota@Sun.COM 		    rm->m_sg, rm->m_count);
11612676SEiji.Ota@Sun.COM 		rm->m_count = 0;
11712676SEiji.Ota@Sun.COM 	}
11812676SEiji.Ota@Sun.COM 	mutex_exit(&rm->m_rs_lock);
11912198SEiji.Ota@Sun.COM 
12012198SEiji.Ota@Sun.COM 	if (rm->m_rdma_op != NULL) {
12112198SEiji.Ota@Sun.COM 		rdsv3_ib_send_unmap_rdma(ic, rm->m_rdma_op);
12212198SEiji.Ota@Sun.COM 
12312198SEiji.Ota@Sun.COM 		/*
12412198SEiji.Ota@Sun.COM 		 * If the user asked for a completion notification on this
12512198SEiji.Ota@Sun.COM 		 * message, we can implement three different semantics:
12612198SEiji.Ota@Sun.COM 		 *  1.	Notify when we received the ACK on the RDS message
12712198SEiji.Ota@Sun.COM 		 *	that was queued with the RDMA. This provides reliable
12812198SEiji.Ota@Sun.COM 		 *	notification of RDMA status at the expense of a one-way
12912198SEiji.Ota@Sun.COM 		 *	packet delay.
13012198SEiji.Ota@Sun.COM 		 *  2.	Notify when the IB stack gives us the completion
13112198SEiji.Ota@Sun.COM 		 *	event for the RDMA operation.
13212198SEiji.Ota@Sun.COM 		 *  3.	Notify when the IB stack gives us the completion
13312198SEiji.Ota@Sun.COM 		 *	event for the accompanying RDS messages.
13412198SEiji.Ota@Sun.COM 		 * Here, we implement approach #3. To implement approach #2,
13512198SEiji.Ota@Sun.COM 		 * call rdsv3_rdma_send_complete from the cq_handler.
13612198SEiji.Ota@Sun.COM 		 * To implement #1,
13712198SEiji.Ota@Sun.COM 		 * don't call rdsv3_rdma_send_complete at all, and fall back to
13812198SEiji.Ota@Sun.COM 		 * the notify
13912198SEiji.Ota@Sun.COM 		 * handling in the ACK processing code.
14012198SEiji.Ota@Sun.COM 		 *
14112198SEiji.Ota@Sun.COM 		 * Note: There's no need to explicitly sync any RDMA buffers
14212198SEiji.Ota@Sun.COM 		 * using
14312198SEiji.Ota@Sun.COM 		 * ib_dma_sync_sg_for_cpu - the completion for the RDMA
14412198SEiji.Ota@Sun.COM 		 * operation itself unmapped the RDMA buffers, which takes care
14512198SEiji.Ota@Sun.COM 		 * of synching.
14612198SEiji.Ota@Sun.COM 		 */
14712198SEiji.Ota@Sun.COM 		rdsv3_ib_send_rdma_complete(rm, wc_status);
14812198SEiji.Ota@Sun.COM 
14912198SEiji.Ota@Sun.COM 		if (rm->m_rdma_op->r_write)
15012198SEiji.Ota@Sun.COM 			rdsv3_stats_add(s_send_rdma_bytes,
15112198SEiji.Ota@Sun.COM 			    rm->m_rdma_op->r_bytes);
15212198SEiji.Ota@Sun.COM 		else
15312198SEiji.Ota@Sun.COM 			rdsv3_stats_add(s_recv_rdma_bytes,
15412198SEiji.Ota@Sun.COM 			    rm->m_rdma_op->r_bytes);
15512198SEiji.Ota@Sun.COM 	}
15612198SEiji.Ota@Sun.COM 
15712198SEiji.Ota@Sun.COM 	/*
15812198SEiji.Ota@Sun.COM 	 * If anyone waited for this message to get flushed out, wake
15912198SEiji.Ota@Sun.COM 	 * them up now
16012198SEiji.Ota@Sun.COM 	 */
16112198SEiji.Ota@Sun.COM 	rdsv3_message_unmapped(rm);
16212198SEiji.Ota@Sun.COM 
16312198SEiji.Ota@Sun.COM 	rdsv3_message_put(rm);
16412198SEiji.Ota@Sun.COM 	send->s_rm = NULL;
16512198SEiji.Ota@Sun.COM }
16612198SEiji.Ota@Sun.COM 
16712198SEiji.Ota@Sun.COM void
rdsv3_ib_send_init_ring(struct rdsv3_ib_connection * ic)16812198SEiji.Ota@Sun.COM rdsv3_ib_send_init_ring(struct rdsv3_ib_connection *ic)
16912198SEiji.Ota@Sun.COM {
17012198SEiji.Ota@Sun.COM 	struct rdsv3_ib_send_work *send;
17112198SEiji.Ota@Sun.COM 	uint32_t i;
17212198SEiji.Ota@Sun.COM 
17312198SEiji.Ota@Sun.COM 	RDSV3_DPRINTF4("rdsv3_ib_send_init_ring", "ic: %p", ic);
17412198SEiji.Ota@Sun.COM 
17512198SEiji.Ota@Sun.COM 	for (i = 0, send = ic->i_sends; i < ic->i_send_ring.w_nr; i++, send++) {
17612198SEiji.Ota@Sun.COM 		send->s_rm = NULL;
17712198SEiji.Ota@Sun.COM 		send->s_op = NULL;
17812198SEiji.Ota@Sun.COM 	}
17912198SEiji.Ota@Sun.COM }
18012198SEiji.Ota@Sun.COM 
18112198SEiji.Ota@Sun.COM void
rdsv3_ib_send_clear_ring(struct rdsv3_ib_connection * ic)18212198SEiji.Ota@Sun.COM rdsv3_ib_send_clear_ring(struct rdsv3_ib_connection *ic)
18312198SEiji.Ota@Sun.COM {
18412198SEiji.Ota@Sun.COM 	struct rdsv3_ib_send_work *send;
18512198SEiji.Ota@Sun.COM 	uint32_t i;
18612198SEiji.Ota@Sun.COM 
18712198SEiji.Ota@Sun.COM 	RDSV3_DPRINTF4("rdsv3_ib_send_clear_ring", "ic: %p", ic);
18812198SEiji.Ota@Sun.COM 
18912198SEiji.Ota@Sun.COM 	for (i = 0, send = ic->i_sends; i < ic->i_send_ring.w_nr; i++, send++) {
19012198SEiji.Ota@Sun.COM 		if (send->s_opcode == 0xdd)
19112198SEiji.Ota@Sun.COM 			continue;
19212198SEiji.Ota@Sun.COM 		if (send->s_rm)
19312198SEiji.Ota@Sun.COM 			rdsv3_ib_send_unmap_rm(ic, send, IBT_WC_WR_FLUSHED_ERR);
19412198SEiji.Ota@Sun.COM 		if (send->s_op)
19512198SEiji.Ota@Sun.COM 			rdsv3_ib_send_unmap_rdma(ic, send->s_op);
19612198SEiji.Ota@Sun.COM 	}
19712198SEiji.Ota@Sun.COM 
19812198SEiji.Ota@Sun.COM 	RDSV3_DPRINTF4("rdsv3_ib_send_clear_ring", "Return: ic: %p", ic);
19912198SEiji.Ota@Sun.COM }
20012198SEiji.Ota@Sun.COM 
20112198SEiji.Ota@Sun.COM /*
20212198SEiji.Ota@Sun.COM  * The _oldest/_free ring operations here race cleanly with the alloc/unalloc
20312198SEiji.Ota@Sun.COM  * operations performed in the send path.  As the sender allocs and potentially
20412198SEiji.Ota@Sun.COM  * unallocs the next free entry in the ring it doesn't alter which is
20512198SEiji.Ota@Sun.COM  * the next to be freed, which is what this is concerned with.
20612198SEiji.Ota@Sun.COM  */
20712198SEiji.Ota@Sun.COM void
rdsv3_ib_send_cqe_handler(struct rdsv3_ib_connection * ic,ibt_wc_t * wc)20812676SEiji.Ota@Sun.COM rdsv3_ib_send_cqe_handler(struct rdsv3_ib_connection *ic, ibt_wc_t *wc)
20912198SEiji.Ota@Sun.COM {
21012676SEiji.Ota@Sun.COM 	struct rdsv3_connection *conn = ic->conn;
21112198SEiji.Ota@Sun.COM 	struct rdsv3_ib_send_work *send;
21212198SEiji.Ota@Sun.COM 	uint32_t completed, polled;
21312198SEiji.Ota@Sun.COM 	uint32_t oldest;
21412198SEiji.Ota@Sun.COM 	uint32_t i = 0;
21512198SEiji.Ota@Sun.COM 	int ret;
21612198SEiji.Ota@Sun.COM 
21712676SEiji.Ota@Sun.COM 	RDSV3_DPRINTF4("rdsv3_ib_send_cqe_handler",
21812676SEiji.Ota@Sun.COM 	    "wc wc_id 0x%llx status %u byte_len %u imm_data %u\n",
21912676SEiji.Ota@Sun.COM 	    (unsigned long long)wc->wc_id, wc->wc_status,
22012676SEiji.Ota@Sun.COM 	    wc->wc_bytes_xfer, ntohl(wc->wc_immed_data));
22112676SEiji.Ota@Sun.COM 
22212676SEiji.Ota@Sun.COM 	rdsv3_ib_stats_inc(s_ib_tx_cq_event);
22312198SEiji.Ota@Sun.COM 
22412676SEiji.Ota@Sun.COM 	if (wc->wc_id == RDSV3_IB_ACK_WR_ID) {
22512676SEiji.Ota@Sun.COM 		if (ic->i_ack_queued + HZ/2 < jiffies)
22612676SEiji.Ota@Sun.COM 			rdsv3_ib_stats_inc(s_ib_tx_stalled);
22712676SEiji.Ota@Sun.COM 		rdsv3_ib_ack_send_complete(ic);
22812676SEiji.Ota@Sun.COM 		return;
22912676SEiji.Ota@Sun.COM 	}
23012676SEiji.Ota@Sun.COM 
23112676SEiji.Ota@Sun.COM 	oldest = rdsv3_ib_ring_oldest(&ic->i_send_ring);
23212676SEiji.Ota@Sun.COM 
23312676SEiji.Ota@Sun.COM 	completed = rdsv3_ib_ring_completed(&ic->i_send_ring,
23412676SEiji.Ota@Sun.COM 	    (wc->wc_id & ~RDSV3_IB_SEND_OP), oldest);
23512676SEiji.Ota@Sun.COM 
23612676SEiji.Ota@Sun.COM 	for (i = 0; i < completed; i++) {
23712676SEiji.Ota@Sun.COM 		send = &ic->i_sends[oldest];
23812198SEiji.Ota@Sun.COM 
23912676SEiji.Ota@Sun.COM 		/*
24012676SEiji.Ota@Sun.COM 		 * In the error case, wc->opcode sometimes contains
24112676SEiji.Ota@Sun.COM 		 * garbage
24212676SEiji.Ota@Sun.COM 		 */
24312676SEiji.Ota@Sun.COM 		switch (send->s_opcode) {
24412676SEiji.Ota@Sun.COM 		case IBT_WRC_SEND:
24512676SEiji.Ota@Sun.COM 			if (send->s_rm)
24612676SEiji.Ota@Sun.COM 				rdsv3_ib_send_unmap_rm(ic, send,
24712676SEiji.Ota@Sun.COM 				    wc->wc_status);
24812676SEiji.Ota@Sun.COM 			break;
24912676SEiji.Ota@Sun.COM 		case IBT_WRC_RDMAW:
25012676SEiji.Ota@Sun.COM 		case IBT_WRC_RDMAR:
25112676SEiji.Ota@Sun.COM 			/*
25212676SEiji.Ota@Sun.COM 			 * Nothing to be done - the SG list will
25312676SEiji.Ota@Sun.COM 			 * be unmapped
25412676SEiji.Ota@Sun.COM 			 * when the SEND completes.
25512676SEiji.Ota@Sun.COM 			 */
25612676SEiji.Ota@Sun.COM 			break;
25712676SEiji.Ota@Sun.COM 		default:
25812676SEiji.Ota@Sun.COM #ifndef __lock_lint
25912676SEiji.Ota@Sun.COM 			RDSV3_DPRINTF2("rdsv3_ib_send_cq_comp_handler",
26012676SEiji.Ota@Sun.COM 			    "RDS/IB: %s: unexpected opcode "
26112676SEiji.Ota@Sun.COM 			    "0x%x in WR!",
26212676SEiji.Ota@Sun.COM 			    __func__, send->s_opcode);
26312676SEiji.Ota@Sun.COM #endif
26412676SEiji.Ota@Sun.COM 			break;
26512198SEiji.Ota@Sun.COM 		}
26612198SEiji.Ota@Sun.COM 
26712676SEiji.Ota@Sun.COM 		send->s_opcode = 0xdd;
26812676SEiji.Ota@Sun.COM 		if (send->s_queued + HZ/2 < jiffies)
26912676SEiji.Ota@Sun.COM 			rdsv3_ib_stats_inc(s_ib_tx_stalled);
27012198SEiji.Ota@Sun.COM 
27112676SEiji.Ota@Sun.COM 		/*
27212676SEiji.Ota@Sun.COM 		 * If a RDMA operation produced an error, signal
27312676SEiji.Ota@Sun.COM 		 * this right
27412676SEiji.Ota@Sun.COM 		 * away. If we don't, the subsequent SEND that goes
27512676SEiji.Ota@Sun.COM 		 * with this
27612676SEiji.Ota@Sun.COM 		 * RDMA will be canceled with ERR_WFLUSH, and the
27712676SEiji.Ota@Sun.COM 		 * application
27812676SEiji.Ota@Sun.COM 		 * never learn that the RDMA failed.
27912676SEiji.Ota@Sun.COM 		 */
28012676SEiji.Ota@Sun.COM 		if (wc->wc_status ==
28112676SEiji.Ota@Sun.COM 		    IBT_WC_REMOTE_ACCESS_ERR && send->s_op) {
28212676SEiji.Ota@Sun.COM 			struct rdsv3_message *rm;
28312198SEiji.Ota@Sun.COM 
28412676SEiji.Ota@Sun.COM 			rm = rdsv3_send_get_message(conn, send->s_op);
28512676SEiji.Ota@Sun.COM 			if (rm) {
28612676SEiji.Ota@Sun.COM 				if (rm->m_rdma_op != NULL)
28712676SEiji.Ota@Sun.COM 					rdsv3_ib_send_unmap_rdma(ic,
28812676SEiji.Ota@Sun.COM 					    rm->m_rdma_op);
28912676SEiji.Ota@Sun.COM 				rdsv3_ib_send_rdma_complete(rm,
29012676SEiji.Ota@Sun.COM 				    wc->wc_status);
29112676SEiji.Ota@Sun.COM 				rdsv3_message_put(rm);
29212198SEiji.Ota@Sun.COM 			}
29312198SEiji.Ota@Sun.COM 		}
29412198SEiji.Ota@Sun.COM 
29512676SEiji.Ota@Sun.COM 		oldest = (oldest + 1) % ic->i_send_ring.w_nr;
29612198SEiji.Ota@Sun.COM 	}
29712198SEiji.Ota@Sun.COM 
29812676SEiji.Ota@Sun.COM 	rdsv3_ib_ring_free(&ic->i_send_ring, completed);
29912676SEiji.Ota@Sun.COM 
30012676SEiji.Ota@Sun.COM 	clear_bit(RDSV3_LL_SEND_FULL, &conn->c_flags);
30112676SEiji.Ota@Sun.COM 
30212676SEiji.Ota@Sun.COM 	/* We expect errors as the qp is drained during shutdown */
30312676SEiji.Ota@Sun.COM 	if (wc->wc_status != IBT_WC_SUCCESS && rdsv3_conn_up(conn)) {
30412676SEiji.Ota@Sun.COM 		RDSV3_DPRINTF2("rdsv3_ib_send_cqe_handler",
30512676SEiji.Ota@Sun.COM 		    "send completion on %u.%u.%u.%u "
30612676SEiji.Ota@Sun.COM 		    "had status %u, disconnecting and reconnecting\n",
30712676SEiji.Ota@Sun.COM 		    NIPQUAD(conn->c_faddr), wc->wc_status);
30812676SEiji.Ota@Sun.COM 		rdsv3_conn_drop(conn);
30912676SEiji.Ota@Sun.COM 	}
31012676SEiji.Ota@Sun.COM 
31112676SEiji.Ota@Sun.COM 	RDSV3_DPRINTF4("rdsv3_ib_send_cqe_handler", "Return: conn: %p", ic);
31212198SEiji.Ota@Sun.COM }
31312198SEiji.Ota@Sun.COM 
31412198SEiji.Ota@Sun.COM /*
31512198SEiji.Ota@Sun.COM  * This is the main function for allocating credits when sending
31612198SEiji.Ota@Sun.COM  * messages.
31712198SEiji.Ota@Sun.COM  *
31812198SEiji.Ota@Sun.COM  * Conceptually, we have two counters:
31912198SEiji.Ota@Sun.COM  *  -	send credits: this tells us how many WRs we're allowed
32012198SEiji.Ota@Sun.COM  *	to submit without overruning the reciever's queue. For
32112198SEiji.Ota@Sun.COM  *	each SEND WR we post, we decrement this by one.
32212198SEiji.Ota@Sun.COM  *
32312198SEiji.Ota@Sun.COM  *  -	posted credits: this tells us how many WRs we recently
32412198SEiji.Ota@Sun.COM  *	posted to the receive queue. This value is transferred
32512198SEiji.Ota@Sun.COM  *	to the peer as a "credit update" in a RDS header field.
32612198SEiji.Ota@Sun.COM  *	Every time we transmit credits to the peer, we subtract
32712198SEiji.Ota@Sun.COM  *	the amount of transferred credits from this counter.
32812198SEiji.Ota@Sun.COM  *
32912198SEiji.Ota@Sun.COM  * It is essential that we avoid situations where both sides have
33012198SEiji.Ota@Sun.COM  * exhausted their send credits, and are unable to send new credits
33112198SEiji.Ota@Sun.COM  * to the peer. We achieve this by requiring that we send at least
33212198SEiji.Ota@Sun.COM  * one credit update to the peer before exhausting our credits.
33312198SEiji.Ota@Sun.COM  * When new credits arrive, we subtract one credit that is withheld
33412198SEiji.Ota@Sun.COM  * until we've posted new buffers and are ready to transmit these
33512198SEiji.Ota@Sun.COM  * credits (see rdsv3_ib_send_add_credits below).
33612198SEiji.Ota@Sun.COM  *
33712198SEiji.Ota@Sun.COM  * The RDS send code is essentially single-threaded; rdsv3_send_xmit
33812198SEiji.Ota@Sun.COM  * grabs c_send_lock to ensure exclusive access to the send ring.
33912198SEiji.Ota@Sun.COM  * However, the ACK sending code is independent and can race with
34012198SEiji.Ota@Sun.COM  * message SENDs.
34112198SEiji.Ota@Sun.COM  *
34212198SEiji.Ota@Sun.COM  * In the send path, we need to update the counters for send credits
34312198SEiji.Ota@Sun.COM  * and the counter of posted buffers atomically - when we use the
34412198SEiji.Ota@Sun.COM  * last available credit, we cannot allow another thread to race us
34512198SEiji.Ota@Sun.COM  * and grab the posted credits counter.  Hence, we have to use a
34612198SEiji.Ota@Sun.COM  * spinlock to protect the credit counter, or use atomics.
34712198SEiji.Ota@Sun.COM  *
34812198SEiji.Ota@Sun.COM  * Spinlocks shared between the send and the receive path are bad,
34912198SEiji.Ota@Sun.COM  * because they create unnecessary delays. An early implementation
35012198SEiji.Ota@Sun.COM  * using a spinlock showed a 5% degradation in throughput at some
35112198SEiji.Ota@Sun.COM  * loads.
35212198SEiji.Ota@Sun.COM  *
35312198SEiji.Ota@Sun.COM  * This implementation avoids spinlocks completely, putting both
35412198SEiji.Ota@Sun.COM  * counters into a single atomic, and updating that atomic using
35512198SEiji.Ota@Sun.COM  * atomic_add (in the receive path, when receiving fresh credits),
35612198SEiji.Ota@Sun.COM  * and using atomic_cmpxchg when updating the two counters.
35712198SEiji.Ota@Sun.COM  */
35812198SEiji.Ota@Sun.COM int
rdsv3_ib_send_grab_credits(struct rdsv3_ib_connection * ic,uint32_t wanted,uint32_t * adv_credits,int need_posted)35912198SEiji.Ota@Sun.COM rdsv3_ib_send_grab_credits(struct rdsv3_ib_connection *ic,
36012414SEiji.Ota@Sun.COM     uint32_t wanted, uint32_t *adv_credits, int need_posted)
36112198SEiji.Ota@Sun.COM {
36212198SEiji.Ota@Sun.COM 	unsigned int avail, posted, got = 0, advertise;
36312198SEiji.Ota@Sun.COM 	long oldval, newval;
36412198SEiji.Ota@Sun.COM 
36512414SEiji.Ota@Sun.COM 	RDSV3_DPRINTF4("rdsv3_ib_send_grab_credits", "ic: %p, %d %d %d",
36612414SEiji.Ota@Sun.COM 	    ic, wanted, *adv_credits, need_posted);
36712198SEiji.Ota@Sun.COM 
36812198SEiji.Ota@Sun.COM 	*adv_credits = 0;
36912198SEiji.Ota@Sun.COM 	if (!ic->i_flowctl)
37012198SEiji.Ota@Sun.COM 		return (wanted);
37112198SEiji.Ota@Sun.COM 
37212198SEiji.Ota@Sun.COM try_again:
37312198SEiji.Ota@Sun.COM 	advertise = 0;
37412198SEiji.Ota@Sun.COM 	oldval = newval = atomic_get(&ic->i_credits);
37512198SEiji.Ota@Sun.COM 	posted = IB_GET_POST_CREDITS(oldval);
37612198SEiji.Ota@Sun.COM 	avail = IB_GET_SEND_CREDITS(oldval);
37712198SEiji.Ota@Sun.COM 
37812198SEiji.Ota@Sun.COM 	RDSV3_DPRINTF5("rdsv3_ib_send_grab_credits",
37912198SEiji.Ota@Sun.COM 	    "wanted (%u): credits=%u posted=%u\n", wanted, avail, posted);
38012198SEiji.Ota@Sun.COM 
38112198SEiji.Ota@Sun.COM 	/* The last credit must be used to send a credit update. */
38212198SEiji.Ota@Sun.COM 	if (avail && !posted)
38312198SEiji.Ota@Sun.COM 		avail--;
38412198SEiji.Ota@Sun.COM 
38512198SEiji.Ota@Sun.COM 	if (avail < wanted) {
38612198SEiji.Ota@Sun.COM 		struct rdsv3_connection *conn = ic->i_cm_id->context;
38712198SEiji.Ota@Sun.COM 
38812198SEiji.Ota@Sun.COM 		/* Oops, there aren't that many credits left! */
38912198SEiji.Ota@Sun.COM 		set_bit(RDSV3_LL_SEND_FULL, &conn->c_flags);
39012198SEiji.Ota@Sun.COM 		got = avail;
39112198SEiji.Ota@Sun.COM 	} else {
39212198SEiji.Ota@Sun.COM 		/* Sometimes you get what you want, lalala. */
39312198SEiji.Ota@Sun.COM 		got = wanted;
39412198SEiji.Ota@Sun.COM 	}
39512198SEiji.Ota@Sun.COM 	newval -= IB_SET_SEND_CREDITS(got);
39612198SEiji.Ota@Sun.COM 
39712198SEiji.Ota@Sun.COM 	/*
39812198SEiji.Ota@Sun.COM 	 * If need_posted is non-zero, then the caller wants
39912198SEiji.Ota@Sun.COM 	 * the posted regardless of whether any send credits are
40012198SEiji.Ota@Sun.COM 	 * available.
40112198SEiji.Ota@Sun.COM 	 */
40212198SEiji.Ota@Sun.COM 	if (posted && (got || need_posted)) {
40312414SEiji.Ota@Sun.COM 		advertise = min(posted, RDSV3_MAX_ADV_CREDIT);
40412198SEiji.Ota@Sun.COM 		newval -= IB_SET_POST_CREDITS(advertise);
40512198SEiji.Ota@Sun.COM 	}
40612198SEiji.Ota@Sun.COM 
40712198SEiji.Ota@Sun.COM 	/* Finally bill everything */
40812198SEiji.Ota@Sun.COM 	if (atomic_cmpxchg(&ic->i_credits, oldval, newval) != oldval)
40912198SEiji.Ota@Sun.COM 		goto try_again;
41012198SEiji.Ota@Sun.COM 
41112198SEiji.Ota@Sun.COM 	*adv_credits = advertise;
41212198SEiji.Ota@Sun.COM 
41312414SEiji.Ota@Sun.COM 	RDSV3_DPRINTF4("rdsv3_ib_send_grab_credits", "ic: %p, %d %d %d",
41412414SEiji.Ota@Sun.COM 	    ic, got, *adv_credits, need_posted);
41512414SEiji.Ota@Sun.COM 
41612198SEiji.Ota@Sun.COM 	return (got);
41712198SEiji.Ota@Sun.COM }
41812198SEiji.Ota@Sun.COM 
41912198SEiji.Ota@Sun.COM void
rdsv3_ib_send_add_credits(struct rdsv3_connection * conn,unsigned int credits)42012198SEiji.Ota@Sun.COM rdsv3_ib_send_add_credits(struct rdsv3_connection *conn, unsigned int credits)
42112198SEiji.Ota@Sun.COM {
42212198SEiji.Ota@Sun.COM 	struct rdsv3_ib_connection *ic = conn->c_transport_data;
42312198SEiji.Ota@Sun.COM 
42412198SEiji.Ota@Sun.COM 	if (credits == 0)
42512198SEiji.Ota@Sun.COM 		return;
42612198SEiji.Ota@Sun.COM 
42712198SEiji.Ota@Sun.COM 	RDSV3_DPRINTF5("rdsv3_ib_send_add_credits",
42812198SEiji.Ota@Sun.COM 	    "credits (%u): current=%u%s\n",
42912198SEiji.Ota@Sun.COM 	    credits,
43012198SEiji.Ota@Sun.COM 	    IB_GET_SEND_CREDITS(atomic_get(&ic->i_credits)),
43112198SEiji.Ota@Sun.COM 	    test_bit(RDSV3_LL_SEND_FULL, &conn->c_flags) ?
43212198SEiji.Ota@Sun.COM 	    ", ll_send_full" : "");
43312198SEiji.Ota@Sun.COM 
43412198SEiji.Ota@Sun.COM 	atomic_add_32(&ic->i_credits, IB_SET_SEND_CREDITS(credits));
43512198SEiji.Ota@Sun.COM 	if (test_and_clear_bit(RDSV3_LL_SEND_FULL, &conn->c_flags))
43612198SEiji.Ota@Sun.COM 		rdsv3_queue_delayed_work(rdsv3_wq, &conn->c_send_w, 0);
43712198SEiji.Ota@Sun.COM 
43812198SEiji.Ota@Sun.COM 	ASSERT(!(IB_GET_SEND_CREDITS(credits) >= 16384));
43912198SEiji.Ota@Sun.COM 
44012198SEiji.Ota@Sun.COM 	rdsv3_ib_stats_inc(s_ib_rx_credit_updates);
44112198SEiji.Ota@Sun.COM 
44212198SEiji.Ota@Sun.COM 	RDSV3_DPRINTF4("rdsv3_ib_send_add_credits",
44312198SEiji.Ota@Sun.COM 	    "Return: conn: %p, credits: %d",
44412198SEiji.Ota@Sun.COM 	    conn, credits);
44512198SEiji.Ota@Sun.COM }
44612198SEiji.Ota@Sun.COM 
44712198SEiji.Ota@Sun.COM void
rdsv3_ib_advertise_credits(struct rdsv3_connection * conn,unsigned int posted)44812198SEiji.Ota@Sun.COM rdsv3_ib_advertise_credits(struct rdsv3_connection *conn, unsigned int posted)
44912198SEiji.Ota@Sun.COM {
45012198SEiji.Ota@Sun.COM 	struct rdsv3_ib_connection *ic = conn->c_transport_data;
45112198SEiji.Ota@Sun.COM 
45212198SEiji.Ota@Sun.COM 	RDSV3_DPRINTF4("rdsv3_ib_advertise_credits", "conn: %p, posted: %d",
45312198SEiji.Ota@Sun.COM 	    conn, posted);
45412198SEiji.Ota@Sun.COM 
45512198SEiji.Ota@Sun.COM 	if (posted == 0)
45612198SEiji.Ota@Sun.COM 		return;
45712198SEiji.Ota@Sun.COM 
45812198SEiji.Ota@Sun.COM 	atomic_add_32(&ic->i_credits, IB_SET_POST_CREDITS(posted));
45912198SEiji.Ota@Sun.COM 
46012198SEiji.Ota@Sun.COM 	/*
46112198SEiji.Ota@Sun.COM 	 * Decide whether to send an update to the peer now.
46212198SEiji.Ota@Sun.COM 	 * If we would send a credit update for every single buffer we
46312198SEiji.Ota@Sun.COM 	 * post, we would end up with an ACK storm (ACK arrives,
46412198SEiji.Ota@Sun.COM 	 * consumes buffer, we refill the ring, send ACK to remote
46512198SEiji.Ota@Sun.COM 	 * advertising the newly posted buffer... ad inf)
46612198SEiji.Ota@Sun.COM 	 *
46712198SEiji.Ota@Sun.COM 	 * Performance pretty much depends on how often we send
46812198SEiji.Ota@Sun.COM 	 * credit updates - too frequent updates mean lots of ACKs.
46912198SEiji.Ota@Sun.COM 	 * Too infrequent updates, and the peer will run out of
47012198SEiji.Ota@Sun.COM 	 * credits and has to throttle.
47112198SEiji.Ota@Sun.COM 	 * For the time being, 16 seems to be a good compromise.
47212198SEiji.Ota@Sun.COM 	 */
47312198SEiji.Ota@Sun.COM 	if (IB_GET_POST_CREDITS(atomic_get(&ic->i_credits)) >= 16)
47412198SEiji.Ota@Sun.COM 		set_bit(IB_ACK_REQUESTED, &ic->i_ack_flags);
47512198SEiji.Ota@Sun.COM }
47612198SEiji.Ota@Sun.COM 
47712198SEiji.Ota@Sun.COM static inline void
rdsv3_ib_xmit_populate_wr(struct rdsv3_ib_connection * ic,ibt_send_wr_t * wr,unsigned int pos,struct rdsv3_scatterlist * scat,unsigned int off,unsigned int length,int send_flags)47812198SEiji.Ota@Sun.COM rdsv3_ib_xmit_populate_wr(struct rdsv3_ib_connection *ic,
47912198SEiji.Ota@Sun.COM     ibt_send_wr_t *wr, unsigned int pos,
48012198SEiji.Ota@Sun.COM     struct rdsv3_scatterlist *scat, unsigned int off, unsigned int length,
48112198SEiji.Ota@Sun.COM     int send_flags)
48212198SEiji.Ota@Sun.COM {
48312198SEiji.Ota@Sun.COM 	ibt_wr_ds_t *sge;
48412198SEiji.Ota@Sun.COM 
48512198SEiji.Ota@Sun.COM 	RDSV3_DPRINTF4("rdsv3_ib_xmit_populate_wr",
48612198SEiji.Ota@Sun.COM 	    "ic: %p, wr: %p scat: %p %d %d %d %d",
48712198SEiji.Ota@Sun.COM 	    ic, wr, scat, pos, off, length, send_flags);
48812198SEiji.Ota@Sun.COM 
48912676SEiji.Ota@Sun.COM 	wr->wr_id = pos | RDSV3_IB_SEND_OP;
49012198SEiji.Ota@Sun.COM 	wr->wr_trans = IBT_RC_SRV;
49112198SEiji.Ota@Sun.COM 	wr->wr_flags = send_flags;
49212198SEiji.Ota@Sun.COM 	wr->wr_opcode = IBT_WRC_SEND;
49312198SEiji.Ota@Sun.COM 
49412198SEiji.Ota@Sun.COM 	if (length != 0) {
49512198SEiji.Ota@Sun.COM 		int	ix, len, assigned;
49612198SEiji.Ota@Sun.COM 		ibt_wr_ds_t *sgl;
49712198SEiji.Ota@Sun.COM 
49812198SEiji.Ota@Sun.COM 		ASSERT(length <= scat->length - off);
49912198SEiji.Ota@Sun.COM 
50012198SEiji.Ota@Sun.COM 		sgl = scat->sgl;
50112198SEiji.Ota@Sun.COM 		if (off != 0) {
50212198SEiji.Ota@Sun.COM 			/* find the right sgl to begin with */
50312198SEiji.Ota@Sun.COM 			while (sgl->ds_len <= off) {
50412198SEiji.Ota@Sun.COM 				off -= sgl->ds_len;
50512198SEiji.Ota@Sun.COM 				sgl++;
50612198SEiji.Ota@Sun.COM 			}
50712198SEiji.Ota@Sun.COM 		}
50812198SEiji.Ota@Sun.COM 
50912198SEiji.Ota@Sun.COM 		ix = 1; /* first data sgl is at 1 */
51012198SEiji.Ota@Sun.COM 		assigned = 0;
51112198SEiji.Ota@Sun.COM 		len = length;
51212198SEiji.Ota@Sun.COM 		do {
51312198SEiji.Ota@Sun.COM 			sge = &wr->wr_sgl[ix++];
51412198SEiji.Ota@Sun.COM 			sge->ds_va = sgl->ds_va + off;
51512198SEiji.Ota@Sun.COM 			assigned = min(len, sgl->ds_len - off);
51612198SEiji.Ota@Sun.COM 			sge->ds_len = assigned;
51712198SEiji.Ota@Sun.COM 			sge->ds_key = sgl->ds_key;
51812198SEiji.Ota@Sun.COM 			len -= assigned;
51912198SEiji.Ota@Sun.COM 			if (len != 0) {
52012198SEiji.Ota@Sun.COM 				sgl++;
52112198SEiji.Ota@Sun.COM 				off = 0;
52212198SEiji.Ota@Sun.COM 			}
52312198SEiji.Ota@Sun.COM 		} while (len > 0);
52412198SEiji.Ota@Sun.COM 
52512198SEiji.Ota@Sun.COM 		wr->wr_nds = ix;
52612198SEiji.Ota@Sun.COM 	} else {
52712198SEiji.Ota@Sun.COM 		/*
52812198SEiji.Ota@Sun.COM 		 * We're sending a packet with no payload. There is only
52912198SEiji.Ota@Sun.COM 		 * one SGE
53012198SEiji.Ota@Sun.COM 		 */
53112198SEiji.Ota@Sun.COM 		wr->wr_nds = 1;
53212198SEiji.Ota@Sun.COM 	}
53312198SEiji.Ota@Sun.COM 
53412198SEiji.Ota@Sun.COM 	sge = &wr->wr_sgl[0];
53512198SEiji.Ota@Sun.COM 	sge->ds_va = ic->i_send_hdrs_dma + (pos * sizeof (struct rdsv3_header));
53612198SEiji.Ota@Sun.COM 	sge->ds_len = sizeof (struct rdsv3_header);
53712198SEiji.Ota@Sun.COM 	sge->ds_key = ic->i_mr->lkey;
53812198SEiji.Ota@Sun.COM 
53912198SEiji.Ota@Sun.COM 	RDSV3_DPRINTF4("rdsv3_ib_xmit_populate_wr",
54012198SEiji.Ota@Sun.COM 	    "Return: ic: %p, wr: %p scat: %p", ic, wr, scat);
54112198SEiji.Ota@Sun.COM }
54212198SEiji.Ota@Sun.COM 
54312198SEiji.Ota@Sun.COM /*
54412198SEiji.Ota@Sun.COM  * This can be called multiple times for a given message.  The first time
54512198SEiji.Ota@Sun.COM  * we see a message we map its scatterlist into the IB device so that
54612198SEiji.Ota@Sun.COM  * we can provide that mapped address to the IB scatter gather entries
54712198SEiji.Ota@Sun.COM  * in the IB work requests.  We translate the scatterlist into a series
54812198SEiji.Ota@Sun.COM  * of work requests that fragment the message.  These work requests complete
54912198SEiji.Ota@Sun.COM  * in order so we pass ownership of the message to the completion handler
55012198SEiji.Ota@Sun.COM  * once we send the final fragment.
55112198SEiji.Ota@Sun.COM  *
55212198SEiji.Ota@Sun.COM  * The RDS core uses the c_send_lock to only enter this function once
55312198SEiji.Ota@Sun.COM  * per connection.  This makes sure that the tx ring alloc/unalloc pairs
55412198SEiji.Ota@Sun.COM  * don't get out of sync and confuse the ring.
55512198SEiji.Ota@Sun.COM  */
55612198SEiji.Ota@Sun.COM int
rdsv3_ib_xmit(struct rdsv3_connection * conn,struct rdsv3_message * rm,unsigned int hdr_off,unsigned int sg,unsigned int off)55712198SEiji.Ota@Sun.COM rdsv3_ib_xmit(struct rdsv3_connection *conn, struct rdsv3_message *rm,
55812198SEiji.Ota@Sun.COM     unsigned int hdr_off, unsigned int sg, unsigned int off)
55912198SEiji.Ota@Sun.COM {
56012198SEiji.Ota@Sun.COM 	struct rdsv3_ib_connection *ic = conn->c_transport_data;
56112198SEiji.Ota@Sun.COM 	struct ib_device *dev = ic->i_cm_id->device;
56212198SEiji.Ota@Sun.COM 	struct rdsv3_ib_send_work *send = NULL;
56312198SEiji.Ota@Sun.COM 	struct rdsv3_ib_send_work *first;
56412198SEiji.Ota@Sun.COM 	struct rdsv3_ib_send_work *prev;
56512198SEiji.Ota@Sun.COM 	ibt_send_wr_t *wr;
56612198SEiji.Ota@Sun.COM 	struct rdsv3_scatterlist *scat;
56712198SEiji.Ota@Sun.COM 	uint32_t pos;
56812198SEiji.Ota@Sun.COM 	uint32_t i;
56912198SEiji.Ota@Sun.COM 	uint32_t work_alloc;
57012198SEiji.Ota@Sun.COM 	uint32_t credit_alloc;
57112198SEiji.Ota@Sun.COM 	uint32_t posted;
57212198SEiji.Ota@Sun.COM 	uint32_t adv_credits = 0;
57312198SEiji.Ota@Sun.COM 	int send_flags = 0;
57412198SEiji.Ota@Sun.COM 	int sent;
57512198SEiji.Ota@Sun.COM 	int ret;
57612198SEiji.Ota@Sun.COM 	int flow_controlled = 0;
57712198SEiji.Ota@Sun.COM 
57812198SEiji.Ota@Sun.COM 	RDSV3_DPRINTF4("rdsv3_ib_xmit", "conn: %p, rm: %p", conn, rm);
57912198SEiji.Ota@Sun.COM 
58012198SEiji.Ota@Sun.COM 	ASSERT(!(off % RDSV3_FRAG_SIZE));
58112198SEiji.Ota@Sun.COM 	ASSERT(!(hdr_off != 0 && hdr_off != sizeof (struct rdsv3_header)));
58212198SEiji.Ota@Sun.COM 
58312198SEiji.Ota@Sun.COM 	/* Do not send cong updates to IB loopback */
58412198SEiji.Ota@Sun.COM 	if (conn->c_loopback &&
58512198SEiji.Ota@Sun.COM 	    rm->m_inc.i_hdr.h_flags & RDSV3_FLAG_CONG_BITMAP) {
58612198SEiji.Ota@Sun.COM 		rdsv3_cong_map_updated(conn->c_fcong, ~(uint64_t)0);
58712198SEiji.Ota@Sun.COM 		return (sizeof (struct rdsv3_header) + RDSV3_CONG_MAP_BYTES);
58812198SEiji.Ota@Sun.COM 	}
58912198SEiji.Ota@Sun.COM 
59012198SEiji.Ota@Sun.COM #ifndef __lock_lint
59112198SEiji.Ota@Sun.COM 	/* FIXME we may overallocate here */
59212198SEiji.Ota@Sun.COM 	if (ntohl(rm->m_inc.i_hdr.h_len) == 0)
59312198SEiji.Ota@Sun.COM 		i = 1;
59412198SEiji.Ota@Sun.COM 	else
59512198SEiji.Ota@Sun.COM 		i = ceil(ntohl(rm->m_inc.i_hdr.h_len), RDSV3_FRAG_SIZE);
59612198SEiji.Ota@Sun.COM #endif
59712198SEiji.Ota@Sun.COM 
59812198SEiji.Ota@Sun.COM 	work_alloc = rdsv3_ib_ring_alloc(&ic->i_send_ring, i, &pos);
59912676SEiji.Ota@Sun.COM 	if (work_alloc != i) {
60012676SEiji.Ota@Sun.COM 		rdsv3_ib_ring_unalloc(&ic->i_send_ring, work_alloc);
60112198SEiji.Ota@Sun.COM 		set_bit(RDSV3_LL_SEND_FULL, &conn->c_flags);
60212198SEiji.Ota@Sun.COM 		rdsv3_ib_stats_inc(s_ib_tx_ring_full);
60312198SEiji.Ota@Sun.COM 		ret = -ENOMEM;
60412198SEiji.Ota@Sun.COM 		goto out;
60512198SEiji.Ota@Sun.COM 	}
60612198SEiji.Ota@Sun.COM 
60712198SEiji.Ota@Sun.COM 	credit_alloc = work_alloc;
60812198SEiji.Ota@Sun.COM 	if (ic->i_flowctl) {
60912198SEiji.Ota@Sun.COM 		credit_alloc = rdsv3_ib_send_grab_credits(ic, work_alloc,
61012414SEiji.Ota@Sun.COM 		    &posted, 0);
61112198SEiji.Ota@Sun.COM 		adv_credits += posted;
61212198SEiji.Ota@Sun.COM 		if (credit_alloc < work_alloc) {
61312198SEiji.Ota@Sun.COM 			rdsv3_ib_ring_unalloc(&ic->i_send_ring,
61412198SEiji.Ota@Sun.COM 			    work_alloc - credit_alloc);
61512198SEiji.Ota@Sun.COM 			work_alloc = credit_alloc;
61612198SEiji.Ota@Sun.COM 			flow_controlled++;
61712198SEiji.Ota@Sun.COM 		}
61812198SEiji.Ota@Sun.COM 		if (work_alloc == 0) {
61912414SEiji.Ota@Sun.COM 			rdsv3_ib_ring_unalloc(&ic->i_send_ring, work_alloc);
62012198SEiji.Ota@Sun.COM 			rdsv3_ib_stats_inc(s_ib_tx_throttle);
62112198SEiji.Ota@Sun.COM 			ret = -ENOMEM;
62212198SEiji.Ota@Sun.COM 			goto out;
62312198SEiji.Ota@Sun.COM 		}
62412198SEiji.Ota@Sun.COM 	}
62512198SEiji.Ota@Sun.COM 
62612198SEiji.Ota@Sun.COM 	/* map the message the first time we see it */
62712198SEiji.Ota@Sun.COM 	if (ic->i_rm == NULL) {
62812198SEiji.Ota@Sun.COM 		/*
62912198SEiji.Ota@Sun.COM 		 * printk(KERN_NOTICE
63012198SEiji.Ota@Sun.COM 		 * "rdsv3_ib_xmit prep msg dport=%u flags=0x%x len=%d\n",
63112198SEiji.Ota@Sun.COM 		 * be16_to_cpu(rm->m_inc.i_hdr.h_dport),
63212198SEiji.Ota@Sun.COM 		 * rm->m_inc.i_hdr.h_flags,
63312198SEiji.Ota@Sun.COM 		 * be32_to_cpu(rm->m_inc.i_hdr.h_len));
63412198SEiji.Ota@Sun.COM 		 */
63512198SEiji.Ota@Sun.COM 		if (rm->m_nents) {
63612198SEiji.Ota@Sun.COM 			rm->m_count = rdsv3_ib_dma_map_sg(dev,
63712198SEiji.Ota@Sun.COM 			    rm->m_sg, rm->m_nents);
63812198SEiji.Ota@Sun.COM 			RDSV3_DPRINTF5("rdsv3_ib_xmit",
63912198SEiji.Ota@Sun.COM 			    "ic %p mapping rm %p: %d\n", ic, rm, rm->m_count);
64012198SEiji.Ota@Sun.COM 			if (rm->m_count == 0) {
64112198SEiji.Ota@Sun.COM 				rdsv3_ib_stats_inc(s_ib_tx_sg_mapping_failure);
64212198SEiji.Ota@Sun.COM 				rdsv3_ib_ring_unalloc(&ic->i_send_ring,
64312198SEiji.Ota@Sun.COM 				    work_alloc);
64412198SEiji.Ota@Sun.COM 				ret = -ENOMEM; /* XXX ? */
64512198SEiji.Ota@Sun.COM 				RDSV3_DPRINTF2("rdsv3_ib_xmit",
64612198SEiji.Ota@Sun.COM 				    "fail: ic %p mapping rm %p: %d\n",
64712198SEiji.Ota@Sun.COM 				    ic, rm, rm->m_count);
64812198SEiji.Ota@Sun.COM 				goto out;
64912198SEiji.Ota@Sun.COM 			}
65012198SEiji.Ota@Sun.COM 		} else {
65112198SEiji.Ota@Sun.COM 			rm->m_count = 0;
65212198SEiji.Ota@Sun.COM 		}
65312198SEiji.Ota@Sun.COM 
65412198SEiji.Ota@Sun.COM 		ic->i_unsignaled_wrs = rdsv3_ib_sysctl_max_unsig_wrs;
65512198SEiji.Ota@Sun.COM 		ic->i_unsignaled_bytes = rdsv3_ib_sysctl_max_unsig_bytes;
65612198SEiji.Ota@Sun.COM 		rdsv3_message_addref(rm);
65712198SEiji.Ota@Sun.COM 		ic->i_rm = rm;
65812198SEiji.Ota@Sun.COM 
65912198SEiji.Ota@Sun.COM 		/* Finalize the header */
66012198SEiji.Ota@Sun.COM 		if (test_bit(RDSV3_MSG_ACK_REQUIRED, &rm->m_flags))
66112198SEiji.Ota@Sun.COM 			rm->m_inc.i_hdr.h_flags |= RDSV3_FLAG_ACK_REQUIRED;
66212198SEiji.Ota@Sun.COM 		if (test_bit(RDSV3_MSG_RETRANSMITTED, &rm->m_flags))
66312198SEiji.Ota@Sun.COM 			rm->m_inc.i_hdr.h_flags |= RDSV3_FLAG_RETRANSMITTED;
66412198SEiji.Ota@Sun.COM 
66512198SEiji.Ota@Sun.COM 		/*
66612198SEiji.Ota@Sun.COM 		 * If it has a RDMA op, tell the peer we did it. This is
66712198SEiji.Ota@Sun.COM 		 * used by the peer to release use-once RDMA MRs.
66812198SEiji.Ota@Sun.COM 		 */
66912198SEiji.Ota@Sun.COM 		if (rm->m_rdma_op) {
67012198SEiji.Ota@Sun.COM 			struct rdsv3_ext_header_rdma ext_hdr;
67112198SEiji.Ota@Sun.COM 
67212198SEiji.Ota@Sun.COM 			ext_hdr.h_rdma_rkey = htonl(rm->m_rdma_op->r_key);
67312198SEiji.Ota@Sun.COM 			(void) rdsv3_message_add_extension(&rm->m_inc.i_hdr,
67412198SEiji.Ota@Sun.COM 			    RDSV3_EXTHDR_RDMA, &ext_hdr,
67512198SEiji.Ota@Sun.COM 			    sizeof (ext_hdr));
67612198SEiji.Ota@Sun.COM 		}
67712198SEiji.Ota@Sun.COM 		if (rm->m_rdma_cookie) {
67812198SEiji.Ota@Sun.COM 			(void) rdsv3_message_add_rdma_dest_extension(
67912198SEiji.Ota@Sun.COM 			    &rm->m_inc.i_hdr,
68012198SEiji.Ota@Sun.COM 			    rdsv3_rdma_cookie_key(rm->m_rdma_cookie),
68112198SEiji.Ota@Sun.COM 			    rdsv3_rdma_cookie_offset(rm->m_rdma_cookie));
68212198SEiji.Ota@Sun.COM 		}
68312198SEiji.Ota@Sun.COM 
68412198SEiji.Ota@Sun.COM 		/*
68512198SEiji.Ota@Sun.COM 		 * Note - rdsv3_ib_piggyb_ack clears the ACK_REQUIRED bit, so
68612198SEiji.Ota@Sun.COM 		 * we should not do this unless we have a chance of at least
68712198SEiji.Ota@Sun.COM 		 * sticking the header into the send ring. Which is why we
68812198SEiji.Ota@Sun.COM 		 * should call rdsv3_ib_ring_alloc first.
68912198SEiji.Ota@Sun.COM 		 */
69012198SEiji.Ota@Sun.COM 		rm->m_inc.i_hdr.h_ack = htonll(rdsv3_ib_piggyb_ack(ic));
69112198SEiji.Ota@Sun.COM 		rdsv3_message_make_checksum(&rm->m_inc.i_hdr);
69212198SEiji.Ota@Sun.COM 
69312198SEiji.Ota@Sun.COM 		/*
69412198SEiji.Ota@Sun.COM 		 * Update adv_credits since we reset the ACK_REQUIRED bit.
69512198SEiji.Ota@Sun.COM 		 */
69612414SEiji.Ota@Sun.COM 		(void) rdsv3_ib_send_grab_credits(ic, 0, &posted, 1);
69712198SEiji.Ota@Sun.COM 		adv_credits += posted;
69812198SEiji.Ota@Sun.COM 		ASSERT(adv_credits <= 255);
69912414SEiji.Ota@Sun.COM 	}
70012198SEiji.Ota@Sun.COM 
70112198SEiji.Ota@Sun.COM 	send = &ic->i_sends[pos];
70212198SEiji.Ota@Sun.COM 	first = send;
70312198SEiji.Ota@Sun.COM 	prev = NULL;
70412198SEiji.Ota@Sun.COM 	scat = &rm->m_sg[sg];
70512198SEiji.Ota@Sun.COM 	sent = 0;
70612198SEiji.Ota@Sun.COM 	i = 0;
70712198SEiji.Ota@Sun.COM 
70812198SEiji.Ota@Sun.COM 	/*
70912198SEiji.Ota@Sun.COM 	 * Sometimes you want to put a fence between an RDMA
71012198SEiji.Ota@Sun.COM 	 * READ and the following SEND.
71112198SEiji.Ota@Sun.COM 	 * We could either do this all the time
71212198SEiji.Ota@Sun.COM 	 * or when requested by the user. Right now, we let
71312198SEiji.Ota@Sun.COM 	 * the application choose.
71412198SEiji.Ota@Sun.COM 	 */
71512198SEiji.Ota@Sun.COM 	if (rm->m_rdma_op && rm->m_rdma_op->r_fence)
71612198SEiji.Ota@Sun.COM 		send_flags = IBT_WR_SEND_FENCE;
71712198SEiji.Ota@Sun.COM 
71812198SEiji.Ota@Sun.COM 	/*
71912198SEiji.Ota@Sun.COM 	 * We could be copying the header into the unused tail of the page.
72012198SEiji.Ota@Sun.COM 	 * That would need to be changed in the future when those pages might
72112198SEiji.Ota@Sun.COM 	 * be mapped userspace pages or page cache pages.  So instead we always
72212198SEiji.Ota@Sun.COM 	 * use a second sge and our long-lived ring of mapped headers.  We send
72312198SEiji.Ota@Sun.COM 	 * the header after the data so that the data payload can be aligned on
72412198SEiji.Ota@Sun.COM 	 * the receiver.
72512198SEiji.Ota@Sun.COM 	 */
72612198SEiji.Ota@Sun.COM 
72712198SEiji.Ota@Sun.COM 	/* handle a 0-len message */
72812198SEiji.Ota@Sun.COM 	if (ntohl(rm->m_inc.i_hdr.h_len) == 0) {
72912198SEiji.Ota@Sun.COM 		wr = &ic->i_send_wrs[0];
73012198SEiji.Ota@Sun.COM 		rdsv3_ib_xmit_populate_wr(ic, wr, pos, NULL, 0, 0, send_flags);
73112198SEiji.Ota@Sun.COM 		send->s_queued = jiffies;
73212198SEiji.Ota@Sun.COM 		send->s_op = NULL;
73312198SEiji.Ota@Sun.COM 		send->s_opcode = wr->wr_opcode;
73412198SEiji.Ota@Sun.COM 		goto add_header;
73512198SEiji.Ota@Sun.COM 	}
73612198SEiji.Ota@Sun.COM 
73712198SEiji.Ota@Sun.COM 	/* if there's data reference it with a chain of work reqs */
73812198SEiji.Ota@Sun.COM 	for (; i < work_alloc && scat != &rm->m_sg[rm->m_count]; i++) {
73912198SEiji.Ota@Sun.COM 		unsigned int len;
74012198SEiji.Ota@Sun.COM 
74112198SEiji.Ota@Sun.COM 		send = &ic->i_sends[pos];
74212198SEiji.Ota@Sun.COM 
74312198SEiji.Ota@Sun.COM 		wr = &ic->i_send_wrs[i];
74412198SEiji.Ota@Sun.COM 		len = min(RDSV3_FRAG_SIZE,
74512198SEiji.Ota@Sun.COM 		    rdsv3_ib_sg_dma_len(dev, scat) - off);
74612198SEiji.Ota@Sun.COM 		rdsv3_ib_xmit_populate_wr(ic, wr, pos, scat, off, len,
74712198SEiji.Ota@Sun.COM 		    send_flags);
74812198SEiji.Ota@Sun.COM 		send->s_queued = jiffies;
74912198SEiji.Ota@Sun.COM 		send->s_op = NULL;
75012198SEiji.Ota@Sun.COM 		send->s_opcode = wr->wr_opcode;
75112198SEiji.Ota@Sun.COM 
75212198SEiji.Ota@Sun.COM 		/*
75312198SEiji.Ota@Sun.COM 		 * We want to delay signaling completions just enough to get
75412198SEiji.Ota@Sun.COM 		 * the batching benefits but not so much that we create dead
75512198SEiji.Ota@Sun.COM 		 * time
75612198SEiji.Ota@Sun.COM 		 * on the wire.
75712198SEiji.Ota@Sun.COM 		 */
75812198SEiji.Ota@Sun.COM 		if (ic->i_unsignaled_wrs-- == 0) {
75912198SEiji.Ota@Sun.COM 			ic->i_unsignaled_wrs = rdsv3_ib_sysctl_max_unsig_wrs;
76012198SEiji.Ota@Sun.COM 			wr->wr_flags |=
76112198SEiji.Ota@Sun.COM 			    IBT_WR_SEND_SIGNAL | IBT_WR_SEND_SOLICIT;
76212198SEiji.Ota@Sun.COM 		}
76312198SEiji.Ota@Sun.COM 
76412198SEiji.Ota@Sun.COM 		ic->i_unsignaled_bytes -= len;
76512198SEiji.Ota@Sun.COM 		if (ic->i_unsignaled_bytes <= 0) {
76612198SEiji.Ota@Sun.COM 			ic->i_unsignaled_bytes =
76712198SEiji.Ota@Sun.COM 			    rdsv3_ib_sysctl_max_unsig_bytes;
76812198SEiji.Ota@Sun.COM 			wr->wr_flags |=
76912198SEiji.Ota@Sun.COM 			    IBT_WR_SEND_SIGNAL | IBT_WR_SEND_SOLICIT;
77012198SEiji.Ota@Sun.COM 		}
77112198SEiji.Ota@Sun.COM 
77212198SEiji.Ota@Sun.COM 		/*
77312198SEiji.Ota@Sun.COM 		 * Always signal the last one if we're stopping due to flow
77412198SEiji.Ota@Sun.COM 		 * control.
77512198SEiji.Ota@Sun.COM 		 */
77612198SEiji.Ota@Sun.COM 		if (flow_controlled && i == (work_alloc-1)) {
77712198SEiji.Ota@Sun.COM 			wr->wr_flags |=
77812198SEiji.Ota@Sun.COM 			    IBT_WR_SEND_SIGNAL | IBT_WR_SEND_SOLICIT;
77912198SEiji.Ota@Sun.COM 		}
78012198SEiji.Ota@Sun.COM 
78112198SEiji.Ota@Sun.COM 		RDSV3_DPRINTF5("rdsv3_ib_xmit", "send %p wr %p num_sge %u \n",
78212198SEiji.Ota@Sun.COM 		    send, wr, wr->wr_nds);
78312198SEiji.Ota@Sun.COM 
78412198SEiji.Ota@Sun.COM 		sent += len;
78512198SEiji.Ota@Sun.COM 		off += len;
78612198SEiji.Ota@Sun.COM 		if (off == rdsv3_ib_sg_dma_len(dev, scat)) {
78712198SEiji.Ota@Sun.COM 			scat++;
78812198SEiji.Ota@Sun.COM 			off = 0;
78912198SEiji.Ota@Sun.COM 		}
79012198SEiji.Ota@Sun.COM 
79112198SEiji.Ota@Sun.COM add_header:
79212198SEiji.Ota@Sun.COM 		/*
79312198SEiji.Ota@Sun.COM 		 * Tack on the header after the data. The header SGE
79412198SEiji.Ota@Sun.COM 		 * should already
79512198SEiji.Ota@Sun.COM 		 * have been set up to point to the right header buffer.
79612198SEiji.Ota@Sun.COM 		 */
79712198SEiji.Ota@Sun.COM 		(void) memcpy(&ic->i_send_hdrs[pos], &rm->m_inc.i_hdr,
79812198SEiji.Ota@Sun.COM 		    sizeof (struct rdsv3_header));
79912198SEiji.Ota@Sun.COM 
80012198SEiji.Ota@Sun.COM 		if (0) {
80112198SEiji.Ota@Sun.COM 			struct rdsv3_header *hdr = &ic->i_send_hdrs[pos];
80212198SEiji.Ota@Sun.COM 
80312320SGiri.Adari@Sun.COM 			RDSV3_DPRINTF2("rdsv3_ib_xmit",
80412198SEiji.Ota@Sun.COM 			    "send WR dport=%u flags=0x%x len=%d",
80512198SEiji.Ota@Sun.COM 			    ntohs(hdr->h_dport),
80612198SEiji.Ota@Sun.COM 			    hdr->h_flags,
80712198SEiji.Ota@Sun.COM 			    ntohl(hdr->h_len));
80812198SEiji.Ota@Sun.COM 		}
80912198SEiji.Ota@Sun.COM 		if (adv_credits) {
81012198SEiji.Ota@Sun.COM 			struct rdsv3_header *hdr = &ic->i_send_hdrs[pos];
81112198SEiji.Ota@Sun.COM 
81212198SEiji.Ota@Sun.COM 			/* add credit and redo the header checksum */
81312198SEiji.Ota@Sun.COM 			hdr->h_credit = adv_credits;
81412198SEiji.Ota@Sun.COM 			rdsv3_message_make_checksum(hdr);
81512198SEiji.Ota@Sun.COM 			adv_credits = 0;
81612198SEiji.Ota@Sun.COM 			rdsv3_ib_stats_inc(s_ib_tx_credit_updates);
81712198SEiji.Ota@Sun.COM 		}
81812198SEiji.Ota@Sun.COM 
81912198SEiji.Ota@Sun.COM 		prev = send;
82012198SEiji.Ota@Sun.COM 
82112198SEiji.Ota@Sun.COM 		pos = (pos + 1) % ic->i_send_ring.w_nr;
82212198SEiji.Ota@Sun.COM 	}
82312198SEiji.Ota@Sun.COM 
82412198SEiji.Ota@Sun.COM 	/*
82512198SEiji.Ota@Sun.COM 	 * Account the RDS header in the number of bytes we sent, but just once.
82612198SEiji.Ota@Sun.COM 	 * The caller has no concept of fragmentation.
82712198SEiji.Ota@Sun.COM 	 */
82812198SEiji.Ota@Sun.COM 	if (hdr_off == 0)
82912198SEiji.Ota@Sun.COM 		sent += sizeof (struct rdsv3_header);
83012198SEiji.Ota@Sun.COM 
83112198SEiji.Ota@Sun.COM 	/* if we finished the message then send completion owns it */
83212198SEiji.Ota@Sun.COM 	if (scat == &rm->m_sg[rm->m_count]) {
83312198SEiji.Ota@Sun.COM 		prev->s_rm = ic->i_rm;
83412198SEiji.Ota@Sun.COM 		wr->wr_flags |= IBT_WR_SEND_SIGNAL | IBT_WR_SEND_SOLICIT;
83512198SEiji.Ota@Sun.COM 		ic->i_rm = NULL;
83612198SEiji.Ota@Sun.COM 	}
83712198SEiji.Ota@Sun.COM 
83812198SEiji.Ota@Sun.COM 	if (i < work_alloc) {
83912198SEiji.Ota@Sun.COM 		rdsv3_ib_ring_unalloc(&ic->i_send_ring, work_alloc - i);
84012198SEiji.Ota@Sun.COM 		work_alloc = i;
84112198SEiji.Ota@Sun.COM 	}
84212198SEiji.Ota@Sun.COM 	if (ic->i_flowctl && i < credit_alloc)
84312198SEiji.Ota@Sun.COM 		rdsv3_ib_send_add_credits(conn, credit_alloc - i);
84412198SEiji.Ota@Sun.COM 
84512198SEiji.Ota@Sun.COM 	/* XXX need to worry about failed_wr and partial sends. */
84612198SEiji.Ota@Sun.COM 	ret = ibt_post_send(ib_get_ibt_channel_hdl(ic->i_cm_id),
84712198SEiji.Ota@Sun.COM 	    ic->i_send_wrs, i, &posted);
84812198SEiji.Ota@Sun.COM 	if (posted != i) {
84912320SGiri.Adari@Sun.COM 		RDSV3_DPRINTF2("rdsv3_ib_xmit",
85012198SEiji.Ota@Sun.COM 		    "ic %p first %p nwr: %d ret %d:%d",
85112198SEiji.Ota@Sun.COM 		    ic, first, i, ret, posted);
85212198SEiji.Ota@Sun.COM 	}
85312198SEiji.Ota@Sun.COM 	if (ret) {
85412320SGiri.Adari@Sun.COM 		RDSV3_DPRINTF2("rdsv3_ib_xmit",
85512198SEiji.Ota@Sun.COM 		    "RDS/IB: ib_post_send to %u.%u.%u.%u "
85612198SEiji.Ota@Sun.COM 		    "returned %d\n", NIPQUAD(conn->c_faddr), ret);
85712198SEiji.Ota@Sun.COM 		rdsv3_ib_ring_unalloc(&ic->i_send_ring, work_alloc);
85812198SEiji.Ota@Sun.COM 		if (prev->s_rm) {
85912198SEiji.Ota@Sun.COM 			ic->i_rm = prev->s_rm;
86012198SEiji.Ota@Sun.COM 			prev->s_rm = NULL;
86112198SEiji.Ota@Sun.COM 		}
86212414SEiji.Ota@Sun.COM 		RDSV3_DPRINTF2("rdsv3_ib_xmit", "ibt_post_send failed\n");
86312414SEiji.Ota@Sun.COM 		rdsv3_conn_drop(ic->conn);
86412676SEiji.Ota@Sun.COM 		ret = -EAGAIN;
86512198SEiji.Ota@Sun.COM 		goto out;
86612198SEiji.Ota@Sun.COM 	}
86712198SEiji.Ota@Sun.COM 
86812198SEiji.Ota@Sun.COM 	ret = sent;
86912198SEiji.Ota@Sun.COM 
87012198SEiji.Ota@Sun.COM 	RDSV3_DPRINTF4("rdsv3_ib_xmit", "Return: conn: %p, rm: %p", conn, rm);
87112198SEiji.Ota@Sun.COM out:
87212198SEiji.Ota@Sun.COM 	ASSERT(!adv_credits);
87312198SEiji.Ota@Sun.COM 	return (ret);
87412198SEiji.Ota@Sun.COM }
87512198SEiji.Ota@Sun.COM 
87612198SEiji.Ota@Sun.COM static void
rdsv3_ib_dma_unmap_sg_rdma(struct ib_device * dev,uint_t num,struct rdsv3_rdma_sg scat[])87712198SEiji.Ota@Sun.COM rdsv3_ib_dma_unmap_sg_rdma(struct ib_device *dev, uint_t num,
87812198SEiji.Ota@Sun.COM 	struct rdsv3_rdma_sg scat[])
87912198SEiji.Ota@Sun.COM {
88012198SEiji.Ota@Sun.COM 	ibt_hca_hdl_t hca_hdl;
88112198SEiji.Ota@Sun.COM 	int i;
88212198SEiji.Ota@Sun.COM 	int num_sgl;
88312198SEiji.Ota@Sun.COM 
88412198SEiji.Ota@Sun.COM 	RDSV3_DPRINTF4("rdsv3_ib_dma_unmap_sg", "rdma_sg: %p", scat);
88512198SEiji.Ota@Sun.COM 
88612198SEiji.Ota@Sun.COM 	if (dev) {
88712198SEiji.Ota@Sun.COM 		hca_hdl = ib_get_ibt_hca_hdl(dev);
88812198SEiji.Ota@Sun.COM 	} else {
88912198SEiji.Ota@Sun.COM 		hca_hdl = scat[0].hca_hdl;
89012198SEiji.Ota@Sun.COM 		RDSV3_DPRINTF2("rdsv3_ib_dma_unmap_sg_rdma",
89112198SEiji.Ota@Sun.COM 		    "NULL dev use cached hca_hdl %p", hca_hdl);
89212198SEiji.Ota@Sun.COM 	}
89312198SEiji.Ota@Sun.COM 
89412198SEiji.Ota@Sun.COM 	if (hca_hdl == NULL)
89512198SEiji.Ota@Sun.COM 		return;
89612198SEiji.Ota@Sun.COM 	scat[0].hca_hdl = NULL;
89712198SEiji.Ota@Sun.COM 
89812198SEiji.Ota@Sun.COM 	for (i = 0; i < num; i++) {
89912198SEiji.Ota@Sun.COM 		if (scat[i].mihdl != NULL) {
90012198SEiji.Ota@Sun.COM 			num_sgl = (scat[i].iovec.bytes / PAGESIZE) + 2;
90112198SEiji.Ota@Sun.COM 			kmem_free(scat[i].swr.wr_sgl,
90212198SEiji.Ota@Sun.COM 			    (num_sgl * sizeof (ibt_wr_ds_t)));
90312198SEiji.Ota@Sun.COM 			scat[i].swr.wr_sgl = NULL;
90412198SEiji.Ota@Sun.COM 			(void) ibt_unmap_mem_iov(hca_hdl, scat[i].mihdl);
90512198SEiji.Ota@Sun.COM 			scat[i].mihdl = NULL;
90612198SEiji.Ota@Sun.COM 		} else
90712198SEiji.Ota@Sun.COM 			break;
90812198SEiji.Ota@Sun.COM 	}
90912198SEiji.Ota@Sun.COM }
91012198SEiji.Ota@Sun.COM 
91112198SEiji.Ota@Sun.COM /* ARGSUSED */
91212198SEiji.Ota@Sun.COM uint_t
rdsv3_ib_dma_map_sg_rdma(struct ib_device * dev,struct rdsv3_rdma_sg scat[],uint_t num,struct rdsv3_scatterlist ** scatl)91312198SEiji.Ota@Sun.COM rdsv3_ib_dma_map_sg_rdma(struct ib_device *dev, struct rdsv3_rdma_sg scat[],
91412198SEiji.Ota@Sun.COM     uint_t num, struct rdsv3_scatterlist **scatl)
91512198SEiji.Ota@Sun.COM {
91612198SEiji.Ota@Sun.COM 	ibt_hca_hdl_t hca_hdl;
91712198SEiji.Ota@Sun.COM 	ibt_iov_attr_t iov_attr;
91812198SEiji.Ota@Sun.COM 	struct buf *bp;
91912198SEiji.Ota@Sun.COM 	uint_t i, j, k;
92012198SEiji.Ota@Sun.COM 	uint_t count;
92112198SEiji.Ota@Sun.COM 	struct rdsv3_scatterlist *sg;
92212198SEiji.Ota@Sun.COM 	int ret;
92312198SEiji.Ota@Sun.COM 
92412198SEiji.Ota@Sun.COM 	RDSV3_DPRINTF4("rdsv3_ib_dma_map_sg_rdma", "scat: %p, num: %d",
92512198SEiji.Ota@Sun.COM 	    scat, num);
92612198SEiji.Ota@Sun.COM 
92712198SEiji.Ota@Sun.COM 	hca_hdl = ib_get_ibt_hca_hdl(dev);
92812198SEiji.Ota@Sun.COM 	scat[0].hca_hdl = hca_hdl;
92912198SEiji.Ota@Sun.COM 	bzero(&iov_attr, sizeof (ibt_iov_attr_t));
93012198SEiji.Ota@Sun.COM 	iov_attr.iov_flags = IBT_IOV_BUF;
93112198SEiji.Ota@Sun.COM 	iov_attr.iov_lso_hdr_sz = 0;
93212198SEiji.Ota@Sun.COM 
93312198SEiji.Ota@Sun.COM 	for (i = 0, count = 0; i < num; i++) {
93412198SEiji.Ota@Sun.COM 		/* transpose umem_cookie  to buf structure */
93512198SEiji.Ota@Sun.COM 		bp = ddi_umem_iosetup(scat[i].umem_cookie,
93612198SEiji.Ota@Sun.COM 		    scat[i].iovec.addr & PAGEOFFSET, scat[i].iovec.bytes,
93712198SEiji.Ota@Sun.COM 		    B_WRITE, 0, 0, NULL, DDI_UMEM_SLEEP);
93812198SEiji.Ota@Sun.COM 		if (bp == NULL) {
93912198SEiji.Ota@Sun.COM 			/* free resources  and return error */
94012198SEiji.Ota@Sun.COM 			goto out;
94112198SEiji.Ota@Sun.COM 		}
94212198SEiji.Ota@Sun.COM 		/* setup ibt_map_mem_iov() attributes */
94312198SEiji.Ota@Sun.COM 		iov_attr.iov_buf = bp;
94412198SEiji.Ota@Sun.COM 		iov_attr.iov_wr_nds = (scat[i].iovec.bytes / PAGESIZE) + 2;
94512198SEiji.Ota@Sun.COM 		scat[i].swr.wr_sgl =
94612198SEiji.Ota@Sun.COM 		    kmem_zalloc(iov_attr.iov_wr_nds * sizeof (ibt_wr_ds_t),
94712198SEiji.Ota@Sun.COM 		    KM_SLEEP);
94812198SEiji.Ota@Sun.COM 
94912198SEiji.Ota@Sun.COM 		ret = ibt_map_mem_iov(hca_hdl, &iov_attr,
95012198SEiji.Ota@Sun.COM 		    (ibt_all_wr_t *)&scat[i].swr, &scat[i].mihdl);
95112198SEiji.Ota@Sun.COM 		freerbuf(bp);
95212198SEiji.Ota@Sun.COM 		if (ret != IBT_SUCCESS) {
95312198SEiji.Ota@Sun.COM 			RDSV3_DPRINTF2("rdsv3_ib_dma_map_sg_rdma",
95412198SEiji.Ota@Sun.COM 			    "ibt_map_mem_iov returned: %d", ret);
95512198SEiji.Ota@Sun.COM 			/* free resources and return error */
95612198SEiji.Ota@Sun.COM 			kmem_free(scat[i].swr.wr_sgl,
95712198SEiji.Ota@Sun.COM 			    iov_attr.iov_wr_nds * sizeof (ibt_wr_ds_t));
95812198SEiji.Ota@Sun.COM 			goto out;
95912198SEiji.Ota@Sun.COM 		}
96012198SEiji.Ota@Sun.COM 		count += scat[i].swr.wr_nds;
96112198SEiji.Ota@Sun.COM 
96212198SEiji.Ota@Sun.COM #ifdef  DEBUG
96312198SEiji.Ota@Sun.COM 		for (j = 0; j < scat[i].swr.wr_nds; j++) {
96412198SEiji.Ota@Sun.COM 			RDSV3_DPRINTF5("rdsv3_ib_dma_map_sg_rdma",
96512198SEiji.Ota@Sun.COM 			    "sgl[%d] va %llx len %x", j,
96612198SEiji.Ota@Sun.COM 			    scat[i].swr.wr_sgl[j].ds_va,
96712198SEiji.Ota@Sun.COM 			    scat[i].swr.wr_sgl[j].ds_len);
96812198SEiji.Ota@Sun.COM 		}
96912198SEiji.Ota@Sun.COM #endif
97012198SEiji.Ota@Sun.COM 		RDSV3_DPRINTF4("rdsv3_ib_dma_map_sg_rdma",
97112198SEiji.Ota@Sun.COM 		    "iovec.bytes: 0x%x scat[%d]swr.wr_nds: %d",
97212198SEiji.Ota@Sun.COM 		    scat[i].iovec.bytes, i, scat[i].swr.wr_nds);
97312198SEiji.Ota@Sun.COM 	}
97412198SEiji.Ota@Sun.COM 
97512198SEiji.Ota@Sun.COM 	count = ((count - 1) / RDSV3_IB_MAX_SGE) + 1;
97612198SEiji.Ota@Sun.COM 	RDSV3_DPRINTF4("rdsv3_ib_dma_map_sg_rdma", "Ret: num: %d", count);
97712198SEiji.Ota@Sun.COM 	return (count);
97812198SEiji.Ota@Sun.COM 
97912198SEiji.Ota@Sun.COM out:
98012198SEiji.Ota@Sun.COM 	rdsv3_ib_dma_unmap_sg_rdma(dev, num, scat);
98112198SEiji.Ota@Sun.COM 	return (0);
98212198SEiji.Ota@Sun.COM }
98312198SEiji.Ota@Sun.COM 
98412198SEiji.Ota@Sun.COM int
rdsv3_ib_xmit_rdma(struct rdsv3_connection * conn,struct rdsv3_rdma_op * op)98512198SEiji.Ota@Sun.COM rdsv3_ib_xmit_rdma(struct rdsv3_connection *conn, struct rdsv3_rdma_op *op)
98612198SEiji.Ota@Sun.COM {
98712198SEiji.Ota@Sun.COM 	struct rdsv3_ib_connection *ic = conn->c_transport_data;
98812198SEiji.Ota@Sun.COM 	struct rdsv3_ib_send_work *send = NULL;
98912198SEiji.Ota@Sun.COM 	struct rdsv3_rdma_sg *scat;
99012198SEiji.Ota@Sun.COM 	uint64_t remote_addr;
99112198SEiji.Ota@Sun.COM 	uint32_t pos;
99212198SEiji.Ota@Sun.COM 	uint32_t work_alloc;
99312198SEiji.Ota@Sun.COM 	uint32_t i, j, k, idx;
99412198SEiji.Ota@Sun.COM 	uint32_t left, count;
99512198SEiji.Ota@Sun.COM 	uint32_t posted;
99612198SEiji.Ota@Sun.COM 	int sent;
99712198SEiji.Ota@Sun.COM 	ibt_status_t status;
99812198SEiji.Ota@Sun.COM 	ibt_send_wr_t *wr;
99912198SEiji.Ota@Sun.COM 	ibt_wr_ds_t *sge;
100012198SEiji.Ota@Sun.COM 
100112198SEiji.Ota@Sun.COM 	RDSV3_DPRINTF4("rdsv3_ib_xmit_rdma", "rdsv3_ib_conn: %p", ic);
100212198SEiji.Ota@Sun.COM 
100312198SEiji.Ota@Sun.COM 	/* map the message the first time we see it */
100412198SEiji.Ota@Sun.COM 	if (!op->r_mapped) {
100512198SEiji.Ota@Sun.COM 		op->r_count = rdsv3_ib_dma_map_sg_rdma(ic->i_cm_id->device,
100612198SEiji.Ota@Sun.COM 		    op->r_rdma_sg, op->r_nents, &op->r_sg);
100712198SEiji.Ota@Sun.COM 		RDSV3_DPRINTF5("rdsv3_ib_xmit_rdma", "ic %p mapping op %p: %d",
100812198SEiji.Ota@Sun.COM 		    ic, op, op->r_count);
100912198SEiji.Ota@Sun.COM 		if (op->r_count == 0) {
101012198SEiji.Ota@Sun.COM 			rdsv3_ib_stats_inc(s_ib_tx_sg_mapping_failure);
101112198SEiji.Ota@Sun.COM 			RDSV3_DPRINTF2("rdsv3_ib_xmit_rdma",
101212198SEiji.Ota@Sun.COM 			    "fail: ic %p mapping op %p: %d",
101312198SEiji.Ota@Sun.COM 			    ic, op, op->r_count);
101412198SEiji.Ota@Sun.COM 			return (-ENOMEM); /* XXX ? */
101512198SEiji.Ota@Sun.COM 		}
101612198SEiji.Ota@Sun.COM 		op->r_mapped = 1;
101712198SEiji.Ota@Sun.COM 	}
101812198SEiji.Ota@Sun.COM 
101912198SEiji.Ota@Sun.COM 	/*
102012198SEiji.Ota@Sun.COM 	 * Instead of knowing how to return a partial rdma read/write
102112198SEiji.Ota@Sun.COM 	 * we insist that there
102212198SEiji.Ota@Sun.COM 	 * be enough work requests to send the entire message.
102312198SEiji.Ota@Sun.COM 	 */
102412198SEiji.Ota@Sun.COM 	work_alloc = rdsv3_ib_ring_alloc(&ic->i_send_ring, op->r_count, &pos);
102512198SEiji.Ota@Sun.COM 	if (work_alloc != op->r_count) {
102612198SEiji.Ota@Sun.COM 		rdsv3_ib_ring_unalloc(&ic->i_send_ring, work_alloc);
102712198SEiji.Ota@Sun.COM 		rdsv3_ib_stats_inc(s_ib_tx_ring_full);
102812198SEiji.Ota@Sun.COM 		return (-ENOMEM);
102912198SEiji.Ota@Sun.COM 	}
103012198SEiji.Ota@Sun.COM 
103112676SEiji.Ota@Sun.COM 	RDSV3_DPRINTF4("rdsv3_ib_xmit_rdma", "pos %u cnt %u", pos, op->r_count);
103212198SEiji.Ota@Sun.COM 	/*
103312198SEiji.Ota@Sun.COM 	 * take the scatter list and transpose into a list of
103412198SEiji.Ota@Sun.COM 	 * send wr's each with a scatter list of RDSV3_IB_MAX_SGE
103512198SEiji.Ota@Sun.COM 	 */
103612198SEiji.Ota@Sun.COM 	scat = &op->r_rdma_sg[0];
103712198SEiji.Ota@Sun.COM 	sent = 0;
103812198SEiji.Ota@Sun.COM 	remote_addr = op->r_remote_addr;
103912198SEiji.Ota@Sun.COM 
104012198SEiji.Ota@Sun.COM 	for (i = 0, k = 0; i < op->r_nents; i++) {
104112198SEiji.Ota@Sun.COM 		left = scat[i].swr.wr_nds;
104212198SEiji.Ota@Sun.COM 		for (idx = 0; left > 0; k++) {
104312198SEiji.Ota@Sun.COM 			send = &ic->i_sends[pos];
104412198SEiji.Ota@Sun.COM 			send->s_queued = jiffies;
104512198SEiji.Ota@Sun.COM 			send->s_opcode = op->r_write ? IBT_WRC_RDMAW :
104612198SEiji.Ota@Sun.COM 			    IBT_WRC_RDMAR;
104712198SEiji.Ota@Sun.COM 			send->s_op = op;
104812198SEiji.Ota@Sun.COM 
104912198SEiji.Ota@Sun.COM 			wr = &ic->i_send_wrs[k];
105012198SEiji.Ota@Sun.COM 			wr->wr_flags = 0;
105112676SEiji.Ota@Sun.COM 			wr->wr_id = pos | RDSV3_IB_SEND_OP;
105212198SEiji.Ota@Sun.COM 			wr->wr_trans = IBT_RC_SRV;
105312198SEiji.Ota@Sun.COM 			wr->wr_opcode = op->r_write ? IBT_WRC_RDMAW :
105412198SEiji.Ota@Sun.COM 			    IBT_WRC_RDMAR;
105512198SEiji.Ota@Sun.COM 			wr->wr.rc.rcwr.rdma.rdma_raddr = remote_addr;
105612198SEiji.Ota@Sun.COM 			wr->wr.rc.rcwr.rdma.rdma_rkey = op->r_key;
105712198SEiji.Ota@Sun.COM 
105812198SEiji.Ota@Sun.COM 			if (left > RDSV3_IB_MAX_SGE) {
105912198SEiji.Ota@Sun.COM 				count = RDSV3_IB_MAX_SGE;
106012198SEiji.Ota@Sun.COM 				left -= RDSV3_IB_MAX_SGE;
106112198SEiji.Ota@Sun.COM 			} else {
106212198SEiji.Ota@Sun.COM 				count = left;
106312198SEiji.Ota@Sun.COM 				left = 0;
106412198SEiji.Ota@Sun.COM 			}
106512198SEiji.Ota@Sun.COM 			wr->wr_nds = count;
106612198SEiji.Ota@Sun.COM 
106712198SEiji.Ota@Sun.COM 			for (j = 0; j < count; j++) {
106812198SEiji.Ota@Sun.COM 				sge = &wr->wr_sgl[j];
106912198SEiji.Ota@Sun.COM 				*sge = scat[i].swr.wr_sgl[idx];
107012198SEiji.Ota@Sun.COM 				remote_addr += scat[i].swr.wr_sgl[idx].ds_len;
107112198SEiji.Ota@Sun.COM 				sent += scat[i].swr.wr_sgl[idx].ds_len;
107212198SEiji.Ota@Sun.COM 				idx++;
107312676SEiji.Ota@Sun.COM 				RDSV3_DPRINTF5("xmit_rdma",
107412198SEiji.Ota@Sun.COM 				    "send_wrs[%d]sgl[%d] va %llx len %x",
107512198SEiji.Ota@Sun.COM 				    k, j, sge->ds_va, sge->ds_len);
107612198SEiji.Ota@Sun.COM 			}
107712676SEiji.Ota@Sun.COM 			RDSV3_DPRINTF5("rdsv3_ib_xmit_rdma",
107812198SEiji.Ota@Sun.COM 			    "wr[%d] %p key: %x code: %d tlen: %d",
107912198SEiji.Ota@Sun.COM 			    k, wr, wr->wr.rc.rcwr.rdma.rdma_rkey,
108012198SEiji.Ota@Sun.COM 			    wr->wr_opcode, sent);
108112198SEiji.Ota@Sun.COM 
108212198SEiji.Ota@Sun.COM 			/*
108312198SEiji.Ota@Sun.COM 			 * We want to delay signaling completions just enough
108412198SEiji.Ota@Sun.COM 			 * to get the batching benefits but not so much that
108512198SEiji.Ota@Sun.COM 			 * we create dead time on the wire.
108612198SEiji.Ota@Sun.COM 			 */
108712198SEiji.Ota@Sun.COM 			if (ic->i_unsignaled_wrs-- == 0) {
108812198SEiji.Ota@Sun.COM 				ic->i_unsignaled_wrs =
108912198SEiji.Ota@Sun.COM 				    rdsv3_ib_sysctl_max_unsig_wrs;
109012198SEiji.Ota@Sun.COM 				wr->wr_flags = IBT_WR_SEND_SIGNAL;
109112198SEiji.Ota@Sun.COM 			}
109212198SEiji.Ota@Sun.COM 
109312198SEiji.Ota@Sun.COM 			pos = (pos + 1) % ic->i_send_ring.w_nr;
109412198SEiji.Ota@Sun.COM 		}
109512198SEiji.Ota@Sun.COM 	}
109612198SEiji.Ota@Sun.COM 
109712198SEiji.Ota@Sun.COM 	status = ibt_post_send(ib_get_ibt_channel_hdl(ic->i_cm_id),
109812198SEiji.Ota@Sun.COM 	    ic->i_send_wrs, k, &posted);
109912198SEiji.Ota@Sun.COM 	if (status != IBT_SUCCESS) {
110012320SGiri.Adari@Sun.COM 		RDSV3_DPRINTF2("rdsv3_ib_xmit_rdma",
110112414SEiji.Ota@Sun.COM 		    "RDS/IB: rdma ib_post_send to %u.%u.%u.%u "
110212414SEiji.Ota@Sun.COM 		    "returned %d", NIPQUAD(conn->c_faddr), status);
110312198SEiji.Ota@Sun.COM 		rdsv3_ib_ring_unalloc(&ic->i_send_ring, work_alloc);
110412198SEiji.Ota@Sun.COM 	}
110512676SEiji.Ota@Sun.COM 	RDSV3_DPRINTF4("rdsv3_ib_xmit_rdma", "Ret: %p", ic);
110612198SEiji.Ota@Sun.COM 	return (status);
110712198SEiji.Ota@Sun.COM }
110812198SEiji.Ota@Sun.COM 
110912198SEiji.Ota@Sun.COM void
rdsv3_ib_xmit_complete(struct rdsv3_connection * conn)111012198SEiji.Ota@Sun.COM rdsv3_ib_xmit_complete(struct rdsv3_connection *conn)
111112198SEiji.Ota@Sun.COM {
111212198SEiji.Ota@Sun.COM 	struct rdsv3_ib_connection *ic = conn->c_transport_data;
111312198SEiji.Ota@Sun.COM 
111412198SEiji.Ota@Sun.COM 	RDSV3_DPRINTF4("rdsv3_ib_xmit_complete", "conn: %p", conn);
111512198SEiji.Ota@Sun.COM 
111612198SEiji.Ota@Sun.COM 	/*
111712198SEiji.Ota@Sun.COM 	 * We may have a pending ACK or window update we were unable
111812198SEiji.Ota@Sun.COM 	 * to send previously (due to flow control). Try again.
111912198SEiji.Ota@Sun.COM 	 */
112012198SEiji.Ota@Sun.COM 	rdsv3_ib_attempt_ack(ic);
112112198SEiji.Ota@Sun.COM }
1122