xref: /illumos-gate/usr/src/uts/common/io/comstar/port/srpt/srpt_ch.c (revision 2dea4eed7ad1c66ae4770263aa2911815a8b86eb)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * RDMA channel interface for Solaris SCSI RDMA Protocol Target (SRP)
29  * transport port provider module for the COMSTAR framework.
30  */
31 
32 #include <sys/cpuvar.h>
33 #include <sys/types.h>
34 #include <sys/conf.h>
35 #include <sys/stat.h>
36 #include <sys/file.h>
37 #include <sys/ddi.h>
38 #include <sys/sunddi.h>
39 #include <sys/modctl.h>
40 #include <sys/sysmacros.h>
41 #include <sys/sdt.h>
42 #include <sys/taskq.h>
43 #include <sys/scsi/scsi.h>
44 #include <sys/ib/ibtl/ibti.h>
45 
46 #include <stmf.h>
47 #include <stmf_ioctl.h>
48 #include <portif.h>
49 
50 #include "srp.h"
51 #include "srpt_impl.h"
52 #include "srpt_ioc.h"
53 #include "srpt_stp.h"
54 #include "srpt_ch.h"
55 
56 extern srpt_ctxt_t *srpt_ctxt;
57 extern uint16_t srpt_send_msg_depth;
58 
59 /*
60  * Prototypes.
61  */
62 static void srpt_ch_scq_hdlr(ibt_cq_hdl_t cq_dhl, void *arg);
63 static void srpt_ch_rcq_hdlr(ibt_cq_hdl_t cq_dhl, void *arg);
64 static void srpt_ch_process_iu(srpt_channel_t *ch, srpt_iu_t *iu);
65 
66 /*
67  * srpt_ch_alloc()
68  */
69 srpt_channel_t *
70 srpt_ch_alloc(srpt_target_port_t *tgt, uint8_t port)
71 {
72 	ibt_status_t			status;
73 	srpt_channel_t			*ch;
74 	ibt_cq_attr_t			cq_attr;
75 	ibt_rc_chan_alloc_args_t	ch_args;
76 	uint32_t			cq_real_size;
77 	srpt_ioc_t			*ioc;
78 
79 	ASSERT(tgt != NULL);
80 	ioc = tgt->tp_ioc;
81 	ASSERT(ioc != NULL);
82 
83 	ch = kmem_zalloc(sizeof (*ch), KM_SLEEP);
84 	rw_init(&ch->ch_rwlock, NULL, RW_DRIVER, NULL);
85 	mutex_init(&ch->ch_reflock, NULL, MUTEX_DRIVER, NULL);
86 	cv_init(&ch->ch_cv_complete, NULL, CV_DRIVER, NULL);
87 	ch->ch_refcnt	= 1;
88 	ch->ch_cv_waiters = 0;
89 
90 	ch->ch_state  = SRPT_CHANNEL_CONNECTING;
91 	ch->ch_tgt    = tgt;
92 	ch->ch_req_lim_delta = 0;
93 	ch->ch_ti_iu_len = 0;
94 
95 	cq_attr.cq_size	 = srpt_send_msg_depth * 2;
96 	cq_attr.cq_sched = 0;
97 	cq_attr.cq_flags = IBT_CQ_NO_FLAGS;
98 
99 	status = ibt_alloc_cq(ioc->ioc_ibt_hdl, &cq_attr, &ch->ch_scq_hdl,
100 	    &cq_real_size);
101 	if (status != IBT_SUCCESS) {
102 		SRPT_DPRINTF_L1("ch_alloc, send CQ alloc error (%d)",
103 		    status);
104 		goto scq_alloc_err;
105 	}
106 
107 	cq_attr.cq_size	 = srpt_send_msg_depth + 1;
108 	cq_attr.cq_sched = 0;
109 	cq_attr.cq_flags = IBT_CQ_NO_FLAGS;
110 
111 	status = ibt_alloc_cq(ioc->ioc_ibt_hdl, &cq_attr, &ch->ch_rcq_hdl,
112 	    &cq_real_size);
113 	if (status != IBT_SUCCESS) {
114 		SRPT_DPRINTF_L2("ch_alloc, receive CQ alloc error (%d)",
115 		    status);
116 		goto rcq_alloc_err;
117 	}
118 
119 	ibt_set_cq_handler(ch->ch_scq_hdl, srpt_ch_scq_hdlr, ch);
120 	ibt_set_cq_handler(ch->ch_rcq_hdl, srpt_ch_rcq_hdlr, ch);
121 	(void) ibt_enable_cq_notify(ch->ch_scq_hdl, IBT_NEXT_COMPLETION);
122 	(void) ibt_enable_cq_notify(ch->ch_rcq_hdl, IBT_NEXT_COMPLETION);
123 
124 	ch_args.rc_flags   = IBT_WR_SIGNALED;
125 
126 	/* Maker certain initiator can not read/write our memory */
127 	ch_args.rc_control = 0;
128 
129 	ch_args.rc_hca_port_num = port;
130 
131 	/*
132 	 * Any SRP IU can result in a number of STMF data buffer transfers
133 	 * and those transfers themselves could span multiple initiator
134 	 * buffers.  Therefore, the number of send WQE's actually required
135 	 * can vary.  Here we assume that on average an I/O will require
136 	 * no more than SRPT_MAX_OUT_IO_PER_CMD send WQE's.  In practice
137 	 * this will prevent send work queue overrun, but we will also
138 	 * inform STMF to throttle I/O should the work queue become full.
139 	 *
140 	 * If the HCA tells us the max outstanding WRs for a channel is
141 	 * lower than our default, use the HCA value.
142 	 */
143 	ch_args.rc_sizes.cs_sq = min(ioc->ioc_attr.hca_max_chan_sz,
144 	    (srpt_send_msg_depth * SRPT_MAX_OUT_IO_PER_CMD));
145 	ch_args.rc_sizes.cs_rq =  0;
146 	ch_args.rc_sizes.cs_sq_sgl = 2;
147 	ch_args.rc_sizes.cs_rq_sgl = 0;
148 
149 	ch_args.rc_scq = ch->ch_scq_hdl;
150 	ch_args.rc_rcq = ch->ch_rcq_hdl;
151 	ch_args.rc_pd  = ioc->ioc_pd_hdl;
152 	ch_args.rc_clone_chan = NULL;
153 	ch_args.rc_srq = ioc->ioc_srq_hdl;
154 
155 	status = ibt_alloc_rc_channel(ioc->ioc_ibt_hdl, IBT_ACHAN_USES_SRQ,
156 	    &ch_args, &ch->ch_chan_hdl, &ch->ch_sizes);
157 	if (status != IBT_SUCCESS) {
158 		SRPT_DPRINTF_L2("ch_alloc, IBT channel alloc error (%d)",
159 		    status);
160 		goto qp_alloc_err;
161 	}
162 
163 	/*
164 	 * Create pool of send WQE entries to map send wqe work IDs
165 	 * to various types (specifically in error cases where OP
166 	 * is not known).
167 	 */
168 	ch->ch_num_swqe = ch->ch_sizes.cs_sq;
169 	SRPT_DPRINTF_L2("ch_alloc, number of SWQEs = %u", ch->ch_num_swqe);
170 	ch->ch_swqe = kmem_zalloc(sizeof (srpt_swqe_t) * ch->ch_num_swqe,
171 	    KM_SLEEP);
172 	if (ch->ch_swqe == NULL) {
173 		SRPT_DPRINTF_L2("ch_alloc, SWQE alloc error");
174 		(void) ibt_free_channel(ch->ch_chan_hdl);
175 		goto qp_alloc_err;
176 	}
177 	mutex_init(&ch->ch_swqe_lock, NULL, MUTEX_DRIVER, NULL);
178 	ch->ch_head = 1;
179 	for (ch->ch_tail = 1; ch->ch_tail < ch->ch_num_swqe -1; ch->ch_tail++) {
180 		ch->ch_swqe[ch->ch_tail].sw_next = ch->ch_tail + 1;
181 	}
182 	ch->ch_swqe[ch->ch_tail].sw_next = 0;
183 
184 	ibt_set_chan_private(ch->ch_chan_hdl, ch);
185 	return (ch);
186 
187 qp_alloc_err:
188 	(void) ibt_free_cq(ch->ch_rcq_hdl);
189 
190 rcq_alloc_err:
191 	(void) ibt_free_cq(ch->ch_scq_hdl);
192 
193 scq_alloc_err:
194 	cv_destroy(&ch->ch_cv_complete);
195 	mutex_destroy(&ch->ch_reflock);
196 	rw_destroy(&ch->ch_rwlock);
197 	kmem_free(ch, sizeof (*ch));
198 
199 	return (NULL);
200 }
201 
202 /*
203  * srpt_ch_add_ref()
204  */
205 void
206 srpt_ch_add_ref(srpt_channel_t *ch)
207 {
208 	mutex_enter(&ch->ch_reflock);
209 	ch->ch_refcnt++;
210 	SRPT_DPRINTF_L4("ch_add_ref, ch (%p), refcnt (%d)",
211 	    (void *)ch, ch->ch_refcnt);
212 	ASSERT(ch->ch_refcnt != 0);
213 	mutex_exit(&ch->ch_reflock);
214 }
215 
216 /*
217  * srpt_ch_release_ref()
218  *
219  * A non-zero value for wait causes thread to block until all references
220  * to channel are released.
221  */
222 void
223 srpt_ch_release_ref(srpt_channel_t *ch, uint_t wait)
224 {
225 	mutex_enter(&ch->ch_reflock);
226 
227 	SRPT_DPRINTF_L4("ch_release_ref, ch (%p), refcnt (%d), wait (%d)",
228 	    (void *)ch, ch->ch_refcnt, wait);
229 
230 	ASSERT(ch->ch_refcnt != 0);
231 
232 	ch->ch_refcnt--;
233 
234 	if (ch->ch_refcnt != 0) {
235 		if (wait) {
236 			ch->ch_cv_waiters++;
237 			while (ch->ch_refcnt != 0) {
238 				cv_wait(&ch->ch_cv_complete, &ch->ch_reflock);
239 			}
240 			ch->ch_cv_waiters--;
241 		} else {
242 			mutex_exit(&ch->ch_reflock);
243 			return;
244 		}
245 	}
246 
247 	/*
248 	 * Last thread out frees the IB resources, locks/conditions and memory
249 	 */
250 	if (ch->ch_cv_waiters > 0) {
251 		/* we're not last, wake someone else up */
252 		cv_signal(&ch->ch_cv_complete);
253 		mutex_exit(&ch->ch_reflock);
254 		return;
255 	}
256 
257 	SRPT_DPRINTF_L3("ch_release_ref - release resources");
258 	if (ch->ch_chan_hdl) {
259 		SRPT_DPRINTF_L3("ch_release_ref - free channel");
260 		(void) ibt_free_channel(ch->ch_chan_hdl);
261 	}
262 
263 	if (ch->ch_scq_hdl) {
264 		(void) ibt_free_cq(ch->ch_scq_hdl);
265 	}
266 
267 	if (ch->ch_rcq_hdl) {
268 		(void) ibt_free_cq(ch->ch_rcq_hdl);
269 	}
270 
271 	/*
272 	 * There should be no IU's associated with this
273 	 * channel on the SCSI session.
274 	 */
275 	if (ch->ch_session != NULL) {
276 		ASSERT(list_is_empty(&ch->ch_session->ss_task_list));
277 
278 		/*
279 		 * Currently only have one channel per session, we will
280 		 * need to release a reference when support is added
281 		 * for multi-channel target login.
282 		 */
283 		srpt_stp_free_session(ch->ch_session);
284 		ch->ch_session = NULL;
285 	}
286 
287 	kmem_free(ch->ch_swqe, sizeof (srpt_swqe_t) * ch->ch_num_swqe);
288 	mutex_destroy(&ch->ch_swqe_lock);
289 	mutex_exit(&ch->ch_reflock);
290 	mutex_destroy(&ch->ch_reflock);
291 	rw_destroy(&ch->ch_rwlock);
292 	kmem_free(ch, sizeof (srpt_channel_t));
293 }
294 
295 /*
296  * srpt_ch_disconnect()
297  */
298 void
299 srpt_ch_disconnect(srpt_channel_t *ch)
300 {
301 	ibt_status_t		status;
302 
303 	SRPT_DPRINTF_L3("ch_disconnect, invoked for ch (%p)",
304 	    (void *)ch);
305 
306 	rw_enter(&ch->ch_rwlock, RW_WRITER);
307 
308 	/*
309 	 * If we are already in the process of disconnecting then
310 	 * nothing need be done, CM will call-back into us when done.
311 	 */
312 	if (ch->ch_state == SRPT_CHANNEL_DISCONNECTING) {
313 		SRPT_DPRINTF_L2("ch_disconnect, called when"
314 		    " disconnect in progress");
315 		rw_exit(&ch->ch_rwlock);
316 		return;
317 	}
318 	ch->ch_state = SRPT_CHANNEL_DISCONNECTING;
319 	rw_exit(&ch->ch_rwlock);
320 
321 	/*
322 	 * Initiate the sending of the CM DREQ message, the private data
323 	 * should be the SRP Target logout IU.  We don't really care about
324 	 * the remote CM DREP message returned.  We issue this in an
325 	 * asynchronous manner and will cleanup when called back by CM.
326 	 */
327 	status = ibt_close_rc_channel(ch->ch_chan_hdl, IBT_NONBLOCKING,
328 	    NULL, 0, NULL, NULL, 0);
329 
330 	if (status != IBT_SUCCESS) {
331 		SRPT_DPRINTF_L2("ch_disconnect, close RC channel"
332 		    " err(%d)", status);
333 	}
334 }
335 
336 /*
337  * srpt_ch_cleanup()
338  */
339 void
340 srpt_ch_cleanup(srpt_channel_t *ch)
341 {
342 	srpt_iu_t		*iu;
343 	srpt_iu_t		*next;
344 	ibt_wc_t		wc;
345 	srpt_target_port_t	*tgt;
346 	srpt_channel_t		*tgt_ch;
347 	scsi_task_t		*iutask;
348 
349 	SRPT_DPRINTF_L3("ch_cleanup, invoked for ch(%p), state(%d)",
350 	    (void *)ch, ch->ch_state);
351 
352 	/* add a ref for the channel until we're done */
353 	srpt_ch_add_ref(ch);
354 
355 	tgt = ch->ch_tgt;
356 	ASSERT(tgt != NULL);
357 
358 	/*
359 	 * Make certain the channel is in the target ports list of
360 	 * known channels and remove it (releasing the target
361 	 * ports reference to the channel).
362 	 */
363 	mutex_enter(&tgt->tp_ch_list_lock);
364 	tgt_ch = list_head(&tgt->tp_ch_list);
365 	while (tgt_ch != NULL) {
366 		if (tgt_ch == ch) {
367 			list_remove(&tgt->tp_ch_list, tgt_ch);
368 			srpt_ch_release_ref(tgt_ch, 0);
369 			break;
370 		}
371 		tgt_ch = list_next(&tgt->tp_ch_list, tgt_ch);
372 	}
373 	mutex_exit(&tgt->tp_ch_list_lock);
374 
375 	if (tgt_ch == NULL) {
376 		SRPT_DPRINTF_L2("ch_cleanup, target channel no"
377 		    "longer known to target");
378 		srpt_ch_release_ref(ch, 0);
379 		return;
380 	}
381 
382 	rw_enter(&ch->ch_rwlock, RW_WRITER);
383 	ch->ch_state = SRPT_CHANNEL_DISCONNECTING;
384 	rw_exit(&ch->ch_rwlock);
385 
386 
387 	/*
388 	 * Generally the IB CQ's will have been drained prior to
389 	 * getting to this call; but we check here to make certain.
390 	 */
391 	if (ch->ch_scq_hdl) {
392 		SRPT_DPRINTF_L4("ch_cleanup, start drain (%d)",
393 		    ch->ch_swqe_posted);
394 		while ((int)ch->ch_swqe_posted > 0) {
395 			delay(drv_usectohz(1000));
396 		}
397 		ibt_set_cq_handler(ch->ch_scq_hdl, NULL, NULL);
398 	}
399 
400 	if (ch->ch_rcq_hdl) {
401 		ibt_set_cq_handler(ch->ch_rcq_hdl, NULL, NULL);
402 
403 		while (ibt_poll_cq(ch->ch_rcq_hdl, &wc, 1, NULL) ==
404 		    IBT_SUCCESS) {
405 			iu = (srpt_iu_t *)(uintptr_t)wc.wc_id;
406 			SRPT_DPRINTF_L4("ch_cleanup, recovering"
407 			    " outstanding RX iu(%p)", (void *)iu);
408 			mutex_enter(&iu->iu_lock);
409 			srpt_ioc_repost_recv_iu(iu->iu_ioc, iu);
410 			/*
411 			 * Channel reference has not yet been added for this
412 			 * IU, so do not decrement.
413 			 */
414 			mutex_exit(&iu->iu_lock);
415 		}
416 	}
417 
418 	/*
419 	 * Go through the list of outstanding IU for the channel's SCSI
420 	 * session and for each either abort or complete an abort.
421 	 */
422 	rw_enter(&ch->ch_rwlock, RW_READER);
423 	if (ch->ch_session != NULL) {
424 		rw_enter(&ch->ch_session->ss_rwlock, RW_READER);
425 		iu = list_head(&ch->ch_session->ss_task_list);
426 		while (iu != NULL) {
427 			next = list_next(&ch->ch_session->ss_task_list, iu);
428 
429 			mutex_enter(&iu->iu_lock);
430 			if (ch == iu->iu_ch) {
431 				if (iu->iu_stmf_task == NULL) {
432 					cmn_err(CE_NOTE,
433 					    "ch_cleanup, NULL stmf task");
434 					ASSERT(0);
435 				}
436 				iutask = iu->iu_stmf_task;
437 			} else {
438 				iutask = NULL;
439 			}
440 			mutex_exit(&iu->iu_lock);
441 
442 			if (iutask != NULL) {
443 				SRPT_DPRINTF_L4("ch_cleanup, aborting "
444 				    "task(%p)", (void *)iutask);
445 				stmf_abort(STMF_QUEUE_TASK_ABORT, iutask,
446 				    STMF_ABORTED, NULL);
447 			}
448 			iu = next;
449 		}
450 		rw_exit(&ch->ch_session->ss_rwlock);
451 	}
452 	rw_exit(&ch->ch_rwlock);
453 
454 	srpt_ch_release_ref(ch, 0);
455 }
456 
457 /*
458  * srpt_ch_rsp_comp()
459  *
460  * Process a completion for an IB SEND message.  A SEND completion
461  * is for a SRP response packet sent back to the initiator.  It
462  * will not have a STMF SCSI task associated with it if it was
463  * sent for a rejected IU, or was a task management abort response.
464  */
465 static void
466 srpt_ch_rsp_comp(srpt_channel_t *ch, srpt_iu_t *iu,
467 	ibt_wc_status_t wc_status)
468 {
469 	stmf_status_t	st = STMF_SUCCESS;
470 
471 	ASSERT(iu->iu_ch == ch);
472 
473 	/*
474 	 * Process the completion regardless whether it's a failure or
475 	 * success.  At this point, we've processed as far as we can and
476 	 * just need to complete the associated task.
477 	 */
478 
479 	if (wc_status != IBT_SUCCESS) {
480 		SRPT_DPRINTF_L2("ch_rsp_comp, WC status err(%d)",
481 		    wc_status);
482 
483 		st = STMF_FAILURE;
484 
485 		if (wc_status != IBT_WC_WR_FLUSHED_ERR) {
486 			srpt_ch_disconnect(ch);
487 		}
488 	}
489 
490 	/*
491 	 * If the IU response completion is not associated with
492 	 * with a SCSI task, release the IU to return the resource
493 	 * and the reference to the channel it holds.
494 	 */
495 	mutex_enter(&iu->iu_lock);
496 	atomic_dec_32(&iu->iu_sq_posted_cnt);
497 
498 	if (iu->iu_stmf_task == NULL) {
499 		srpt_ioc_repost_recv_iu(iu->iu_ioc, iu);
500 		mutex_exit(&iu->iu_lock);
501 		srpt_ch_release_ref(ch, 0);
502 		return;
503 	}
504 
505 	/*
506 	 * We should not get a SEND completion where the task has already
507 	 * completed aborting and STMF has been informed.
508 	 */
509 	ASSERT((iu->iu_flags & SRPT_IU_ABORTED) == 0);
510 
511 	/*
512 	 * Let STMF know we are done.
513 	 */
514 	mutex_exit(&iu->iu_lock);
515 
516 	stmf_send_status_done(iu->iu_stmf_task, st, STMF_IOF_LPORT_DONE);
517 }
518 
519 /*
520  * srpt_ch_data_comp()
521  *
522  * Process an IB completion for a RDMA operation.  This completion
523  * should be associated with the last RDMA operation for any
524  * data buffer transfer.
525  */
526 static void
527 srpt_ch_data_comp(srpt_channel_t *ch, stmf_data_buf_t *stmf_dbuf,
528 	ibt_wc_status_t wc_status)
529 {
530 	srpt_ds_dbuf_t		*dbuf;
531 	srpt_iu_t		*iu;
532 	stmf_status_t		status;
533 
534 	ASSERT(stmf_dbuf != NULL);
535 
536 	dbuf = (srpt_ds_dbuf_t *)stmf_dbuf->db_port_private;
537 
538 	ASSERT(dbuf != NULL);
539 
540 	iu = dbuf->db_iu;
541 
542 	ASSERT(iu != NULL);
543 	ASSERT(iu->iu_ch == ch);
544 
545 	/*
546 	 * If work completion indicates non-flush failure, then
547 	 * start a channel disconnect (asynchronous) and release
548 	 * the reference to the IU.  The task will be cleaned
549 	 * up with STMF during channel shutdown processing.
550 	 */
551 	if (wc_status != IBT_SUCCESS) {
552 		SRPT_DPRINTF_L2("ch_data_comp, WC status err(%d)",
553 		    wc_status);
554 		if (wc_status != IBT_WC_WR_FLUSHED_ERR) {
555 			srpt_ch_disconnect(ch);
556 		}
557 		atomic_dec_32(&iu->iu_sq_posted_cnt);
558 		return;
559 	}
560 
561 	/*
562 	 * If STMF has requested this task be aborted, then if this is the
563 	 * last I/O operation outstanding, notify STMF the task has been
564 	 *  aborted and ignore the completion.
565 	 */
566 	mutex_enter(&iu->iu_lock);
567 	atomic_dec_32(&iu->iu_sq_posted_cnt);
568 
569 	if ((iu->iu_flags & SRPT_IU_STMF_ABORTING) != 0) {
570 		scsi_task_t	*abort_task = iu->iu_stmf_task;
571 
572 		mutex_exit(&iu->iu_lock);
573 		stmf_abort(STMF_REQUEUE_TASK_ABORT_LPORT, abort_task,
574 		    STMF_ABORTED, NULL);
575 		return;
576 	}
577 
578 	/*
579 	 * We should not get an RDMA completion where the task has already
580 	 * completed aborting and STMF has been informed.
581 	 */
582 	ASSERT((iu->iu_flags & SRPT_IU_ABORTED) == 0);
583 
584 	/*
585 	 * Good completion for last RDMA op associated with a data buffer
586 	 * I/O, if specified initiate status otherwise let STMF know we are
587 	 * done.
588 	 */
589 	stmf_dbuf->db_xfer_status = STMF_SUCCESS;
590 	mutex_exit(&iu->iu_lock);
591 
592 	DTRACE_SRP_8(xfer__done, srpt_channel_t, ch,
593 	    ibt_wr_ds_t, &(dbuf->db_sge), srpt_iu_t, iu,
594 	    ibt_send_wr_t, 0, uint32_t, stmf_dbuf->db_data_size,
595 	    uint32_t, 0, uint32_t, 0,
596 	    uint32_t, (stmf_dbuf->db_flags & DB_DIRECTION_TO_RPORT) ? 1 : 0);
597 
598 	if ((stmf_dbuf->db_flags & DB_SEND_STATUS_GOOD) != 0) {
599 		status = srpt_stp_send_status(dbuf->db_iu->iu_stmf_task, 0);
600 		if (status == STMF_SUCCESS) {
601 			return;
602 		}
603 		stmf_dbuf->db_xfer_status = STMF_FAILURE;
604 	}
605 	stmf_data_xfer_done(dbuf->db_iu->iu_stmf_task, stmf_dbuf, 0);
606 }
607 
608 /*
609  * srpt_ch_scq_hdlr()
610  */
611 static void
612 srpt_ch_scq_hdlr(ibt_cq_hdl_t cq_hdl, void *arg)
613 {
614 	ibt_status_t		status;
615 	srpt_channel_t		*ch = arg;
616 	ibt_wc_t		wc[SRPT_SEND_WC_POLL_SIZE];
617 	ibt_wc_t		*wcp;
618 	int			i;
619 	uint32_t		cq_rearmed = 0;
620 	uint32_t		entries;
621 	srpt_swqe_t		*swqe;
622 
623 	ASSERT(ch != NULL);
624 
625 	/* Reference channel for the duration of this call */
626 	srpt_ch_add_ref(ch);
627 
628 	for (;;) {
629 		status = ibt_poll_cq(cq_hdl, &wc[0], SRPT_SEND_WC_POLL_SIZE,
630 		    &entries);
631 		if (status == IBT_CQ_EMPTY) {
632 			/*
633 			 * CQ drained, if we have not rearmed the CQ
634 			 * do so and poll to eliminate race; otherwise
635 			 * we are done.
636 			 */
637 			if (cq_rearmed == 0) {
638 				(void) ibt_enable_cq_notify(ch->ch_scq_hdl,
639 				    IBT_NEXT_COMPLETION);
640 				cq_rearmed = 1;
641 				continue;
642 			} else {
643 				break;
644 			}
645 		} else if (status != IBT_SUCCESS) {
646 			/*
647 			 * This error should not happen, it indicates something
648 			 * abnormal has gone wrong and represents either a
649 			 * hardware or programming logic coding error.
650 			 */
651 			SRPT_DPRINTF_L2("ch_scq_hdlr, unexpected CQ err(%d)",
652 			    status);
653 			srpt_ch_disconnect(ch);
654 			break;
655 		}
656 
657 		for (wcp = wc, i = 0; i < entries; i++, wcp++) {
658 
659 			/*
660 			 * A zero work ID indicates this CQE is associated
661 			 * with an intermediate post of a RDMA data transfer
662 			 * operation.  Since intermediate data requests are
663 			 * unsignaled, we should only get these if there was
664 			 * an error.  No action is required.
665 			 */
666 			if (wcp->wc_id == 0) {
667 				continue;
668 			}
669 			swqe = ch->ch_swqe + wcp->wc_id;
670 
671 			switch (swqe->sw_type) {
672 			case SRPT_SWQE_TYPE_RESP:
673 				srpt_ch_rsp_comp(ch, (srpt_iu_t *)
674 				    swqe->sw_addr, wcp->wc_status);
675 				break;
676 
677 			case SRPT_SWQE_TYPE_DATA:
678 				srpt_ch_data_comp(ch, (stmf_data_buf_t *)
679 				    swqe->sw_addr, wcp->wc_status);
680 				break;
681 
682 			default:
683 				SRPT_DPRINTF_L2("ch_scq_hdlr, bad type(%d)",
684 				    swqe->sw_type);
685 				ASSERT(0);
686 			}
687 
688 			srpt_ch_free_swqe_wrid(ch, wcp->wc_id);
689 		}
690 	}
691 
692 	srpt_ch_release_ref(ch, 0);
693 }
694 
695 /*
696  * srpt_ch_rcq_hdlr()
697  */
698 static void
699 srpt_ch_rcq_hdlr(ibt_cq_hdl_t cq_hdl, void *arg)
700 {
701 	ibt_status_t		status;
702 	srpt_channel_t		*ch = arg;
703 	ibt_wc_t		wc[SRPT_RECV_WC_POLL_SIZE];
704 	ibt_wc_t		*wcp;
705 	int			i;
706 	uint32_t		entries;
707 	srpt_iu_t		*iu;
708 	uint_t			cq_rearmed = 0;
709 
710 	/*
711 	 * The channel object will exists while the CQ handler call-back
712 	 * is installed.
713 	 */
714 	ASSERT(ch != NULL);
715 	srpt_ch_add_ref(ch);
716 
717 	/*
718 	 * If we know a channel disconnect has started do nothing
719 	 * and let channel cleanup code recover resources from the CQ.
720 	 * We are not concerned about races with the state transition
721 	 * since the code will do the correct thing either way. This
722 	 * is simply to circumvent rearming the CQ, and it will
723 	 * catch the state next time.
724 	 */
725 	rw_enter(&ch->ch_rwlock, RW_READER);
726 	if (ch->ch_state == SRPT_CHANNEL_DISCONNECTING) {
727 		SRPT_DPRINTF_L2("ch_rcq_hdlr, channel disconnecting");
728 		rw_exit(&ch->ch_rwlock);
729 		srpt_ch_release_ref(ch, 0);
730 		return;
731 	}
732 	rw_exit(&ch->ch_rwlock);
733 
734 	for (;;) {
735 		status = ibt_poll_cq(cq_hdl, &wc[0], SRPT_RECV_WC_POLL_SIZE,
736 		    &entries);
737 		if (status == IBT_CQ_EMPTY) {
738 			/*
739 			 * OK, empty, if we have not rearmed the CQ
740 			 * do so, and poll to eliminate race; otherwise
741 			 * we are done.
742 			 */
743 			if (cq_rearmed == 0) {
744 				(void) ibt_enable_cq_notify(ch->ch_rcq_hdl,
745 				    IBT_NEXT_COMPLETION);
746 				cq_rearmed = 1;
747 				continue;
748 			} else {
749 				break;
750 			}
751 		} else if (status != IBT_SUCCESS) {
752 			/*
753 			 * This error should not happen, it indicates something
754 			 * abnormal has gone wrong and represents either a
755 			 * hardware or programming logic coding error.
756 			 */
757 			SRPT_DPRINTF_L2("ch_rcq_hdlr, unexpected CQ err(%d)",
758 			    status);
759 			srpt_ch_disconnect(ch);
760 			break;
761 		}
762 
763 		for (wcp = wc, i = 0; i < entries; i++, wcp++) {
764 
765 			/*
766 			 *  Check wc_status before proceeding.  If the
767 			 *  status indicates a channel problem, stop processing.
768 			 */
769 			if (wcp->wc_status != IBT_WC_SUCCESS) {
770 				if (wcp->wc_status == IBT_WC_WR_FLUSHED_ERR) {
771 					SRPT_DPRINTF_L2(
772 					    "ch_rcq, unexpected"
773 					    " wc_status err(%d)",
774 					    wcp->wc_status);
775 					srpt_ch_disconnect(ch);
776 					/* XXX - verify not leaking IUs */
777 					goto done;
778 				} else {
779 					/* skip IUs with errors */
780 					SRPT_DPRINTF_L2(
781 					    "ch_rcq, ERROR comp(%d)",
782 					    wcp->wc_status);
783 					/* XXX - verify not leaking IUs */
784 					continue;
785 				}
786 			}
787 
788 			iu = (srpt_iu_t *)(uintptr_t)wcp->wc_id;
789 			ASSERT(iu != NULL);
790 
791 			/*
792 			 * Process the IU.
793 			 */
794 			ASSERT(wcp->wc_type == IBT_WRC_RECV);
795 			srpt_ch_process_iu(ch, iu);
796 		}
797 	}
798 
799 done:
800 	srpt_ch_release_ref(ch, 0);
801 }
802 
803 /*
804  * srpt_ch_srp_cmd()
805  */
806 static int
807 srpt_ch_srp_cmd(srpt_channel_t *ch, srpt_iu_t *iu)
808 {
809 	srp_cmd_req_t		*cmd = (srp_cmd_req_t *)iu->iu_buf;
810 	srp_indirect_desc_t	*i_desc;
811 	uint_t			i_di_cnt;
812 	uint_t			i_do_cnt;
813 	uint8_t			do_fmt;
814 	uint8_t			di_fmt;
815 	uint32_t		*cur_desc_off;
816 	int			i;
817 	ibt_status_t		status;
818 	uint8_t			addlen;
819 
820 
821 	DTRACE_SRP_2(task__command, srpt_channel_t, ch, srp_cmd_req_t, cmd);
822 	iu->iu_ch  = ch;
823 	iu->iu_tag = cmd->cr_tag;
824 
825 	/*
826 	 * The SRP specification and SAM require support for bi-directional
827 	 * data transfer, so we create a single buffer descriptor list that
828 	 * in the IU buffer that covers the data-in and data-out buffers.
829 	 * In practice we will just see unidirectional transfers with either
830 	 * data-in or data out descriptors.  If we were to take that as fact,
831 	 * we could reduce overhead slightly.
832 	 */
833 
834 	/*
835 	 * additional length is a 6-bit number in 4-byte words, so multiply by 4
836 	 * to get bytes.
837 	 */
838 	addlen = cmd->cr_add_cdb_len & 0x3f;	/* mask off 6 bits */
839 
840 	cur_desc_off = (uint32_t *)(void *)&cmd->cr_add_data;
841 	cur_desc_off  += addlen;		/* 32-bit arithmetic */
842 	iu->iu_num_rdescs = 0;
843 	iu->iu_rdescs = (srp_direct_desc_t *)(void *)cur_desc_off;
844 
845 	/*
846 	 * Examine buffer description for Data In (i.e. data flows
847 	 * to the initiator).
848 	 */
849 	i_do_cnt = i_di_cnt = 0;
850 	di_fmt = cmd->cr_buf_fmt >> 4;
851 	if (di_fmt == SRP_DATA_DESC_DIRECT) {
852 		iu->iu_num_rdescs = 1;
853 		cur_desc_off = (uint32_t *)(void *)&iu->iu_rdescs[1];
854 	} else if (di_fmt == SRP_DATA_DESC_INDIRECT) {
855 		i_desc = (srp_indirect_desc_t *)iu->iu_rdescs;
856 		i_di_cnt  = b2h32(i_desc->id_table.dd_len) /
857 		    sizeof (srp_direct_desc_t);
858 
859 		/*
860 		 * Some initiators like OFED occasionally use the wrong counts,
861 		 * so check total to allow for this.  NOTE: we do not support
862 		 * reading of the descriptor table from the initiator, so if
863 		 * not all descriptors are in the IU we drop the task.
864 		 */
865 		if (i_di_cnt > (cmd->cr_dicnt + cmd->cr_docnt)) {
866 			SRPT_DPRINTF_L2("ch_srp_cmd, remote RDMA of"
867 			    " descriptors not supported");
868 			SRPT_DPRINTF_L2("ch_srp_cmd, sizeof entry (%d),"
869 			    " i_di_cnt(%d), cr_dicnt(%d)",
870 			    (uint_t)sizeof (srp_direct_desc_t),
871 			    i_di_cnt, cmd->cr_dicnt);
872 			iu->iu_rdescs = NULL;
873 			return (1);
874 		}
875 		bcopy(&i_desc->id_desc[0], iu->iu_rdescs,
876 		    sizeof (srp_direct_desc_t) * i_di_cnt);
877 		iu->iu_num_rdescs += i_di_cnt;
878 		cur_desc_off = (uint32_t *)(void *)&i_desc->id_desc[i_di_cnt];
879 	}
880 
881 	/*
882 	 * Examine buffer description for Data Out (i.e. data flows
883 	 * from the initiator).
884 	 */
885 	do_fmt = cmd->cr_buf_fmt & 0x0F;
886 	if (do_fmt == SRP_DATA_DESC_DIRECT) {
887 		if (di_fmt == SRP_DATA_DESC_DIRECT) {
888 			bcopy(cur_desc_off, &iu->iu_rdescs[iu->iu_num_rdescs],
889 			    sizeof (srp_direct_desc_t));
890 		}
891 		iu->iu_num_rdescs++;
892 	} else if (do_fmt == SRP_DATA_DESC_INDIRECT) {
893 		i_desc = (srp_indirect_desc_t *)cur_desc_off;
894 		i_do_cnt  = b2h32(i_desc->id_table.dd_len) /
895 		    sizeof (srp_direct_desc_t);
896 
897 		/*
898 		 * Some initiators like OFED occasionally use the wrong counts,
899 		 * so check total to allow for this.  NOTE: we do not support
900 		 * reading of the descriptor table from the initiator, so if
901 		 * not all descriptors are in the IU we drop the task.
902 		 */
903 		if ((i_di_cnt + i_do_cnt) > (cmd->cr_dicnt + cmd->cr_docnt)) {
904 			SRPT_DPRINTF_L2("ch_srp_cmd, remote RDMA of"
905 			    " descriptors not supported");
906 			SRPT_DPRINTF_L2("ch_srp_cmd, sizeof entry (%d),"
907 			    " i_do_cnt(%d), cr_docnt(%d)",
908 			    (uint_t)sizeof (srp_direct_desc_t),
909 			    i_do_cnt, cmd->cr_docnt);
910 			iu->iu_rdescs = 0;
911 			return (1);
912 		}
913 		bcopy(&i_desc->id_desc[0], &iu->iu_rdescs[iu->iu_num_rdescs],
914 		    sizeof (srp_direct_desc_t) * i_do_cnt);
915 		iu->iu_num_rdescs += i_do_cnt;
916 	}
917 
918 	iu->iu_tot_xfer_len = 0;
919 	for (i = 0; i < iu->iu_num_rdescs; i++) {
920 		iu->iu_rdescs[i].dd_vaddr = b2h64(iu->iu_rdescs[i].dd_vaddr);
921 		iu->iu_rdescs[i].dd_hdl   = b2h32(iu->iu_rdescs[i].dd_hdl);
922 		iu->iu_rdescs[i].dd_len   = b2h32(iu->iu_rdescs[i].dd_len);
923 		iu->iu_tot_xfer_len += iu->iu_rdescs[i].dd_len;
924 	}
925 
926 #ifdef DEBUG
927 	if (srpt_errlevel >= SRPT_LOG_L4) {
928 		SRPT_DPRINTF_L4("ch_srp_cmd, iu->iu_tot_xfer_len (%d)",
929 		    iu->iu_tot_xfer_len);
930 		for (i = 0; i < iu->iu_num_rdescs; i++) {
931 			SRPT_DPRINTF_L4("ch_srp_cmd, rdescs[%d].dd_vaddr"
932 			    " (0x%08llx)",
933 			    i, (u_longlong_t)iu->iu_rdescs[i].dd_vaddr);
934 			SRPT_DPRINTF_L4("ch_srp_cmd, rdescs[%d].dd_hdl"
935 			    " (0x%08x)", i, iu->iu_rdescs[i].dd_hdl);
936 			SRPT_DPRINTF_L4("ch_srp_cmd, rdescs[%d].dd_len (%d)",
937 			    i, iu->iu_rdescs[i].dd_len);
938 		}
939 		SRPT_DPRINTF_L4("ch_srp_cmd, LUN (0x%08lx)",
940 		    (unsigned long int) *((uint64_t *)(void *) cmd->cr_lun));
941 	}
942 #endif
943 	rw_enter(&ch->ch_rwlock, RW_READER);
944 
945 	if (ch->ch_state == SRPT_CHANNEL_DISCONNECTING) {
946 		/*
947 		 * The channel has begun disconnecting, so ignore the
948 		 * the command returning the IU resources.
949 		 */
950 		rw_exit(&ch->ch_rwlock);
951 		return (1);
952 	}
953 
954 	/*
955 	 * Once a SCSI task is allocated and assigned to the IU, it
956 	 * owns those IU resources, which will be held until STMF
957 	 * is notified the task is done (from a lport perspective).
958 	 */
959 	iu->iu_stmf_task = stmf_task_alloc(ch->ch_tgt->tp_lport,
960 	    ch->ch_session->ss_ss, cmd->cr_lun,
961 	    SRP_CDB_SIZE + (addlen * 4), 0);
962 	if (iu->iu_stmf_task == NULL) {
963 		/*
964 		 * Could not allocate, return status to the initiator
965 		 * indicating that we are temporarily unable to process
966 		 * commands.  If unable to send, immediately return IU
967 		 * resource.
968 		 */
969 		SRPT_DPRINTF_L2("ch_srp_cmd, SCSI task allocation failure");
970 		rw_exit(&ch->ch_rwlock);
971 		mutex_enter(&iu->iu_lock);
972 		status = srpt_stp_send_response(iu, STATUS_BUSY, 0, 0, 0,
973 		    NULL, SRPT_NO_FENCE_SEND);
974 		mutex_exit(&iu->iu_lock);
975 		if (status != IBT_SUCCESS) {
976 			SRPT_DPRINTF_L2("ch_srp_cmd, error(%d) posting error"
977 			    " response", status);
978 			return (1);
979 		} else {
980 			return (0);
981 		}
982 	}
983 
984 	iu->iu_stmf_task->task_port_private = iu;
985 	iu->iu_stmf_task->task_flags = 0;
986 
987 	if (di_fmt != 0) {
988 		iu->iu_stmf_task->task_flags |= TF_WRITE_DATA;
989 	}
990 	if (do_fmt != 0) {
991 		iu->iu_stmf_task->task_flags |= TF_READ_DATA;
992 	}
993 
994 	switch (cmd->cr_task_attr) {
995 	case SRP_TSK_ATTR_QTYPE_SIMPLE:
996 		iu->iu_stmf_task->task_flags |=	TF_ATTR_SIMPLE_QUEUE;
997 		break;
998 
999 	case SRP_TSK_ATTR_QTYPE_HEAD_OF_Q:
1000 		iu->iu_stmf_task->task_flags |=	TF_ATTR_HEAD_OF_QUEUE;
1001 		break;
1002 
1003 	case SRP_TSK_ATTR_QTYPE_ORDERED:
1004 		iu->iu_stmf_task->task_flags |=	TF_ATTR_ORDERED_QUEUE;
1005 		break;
1006 
1007 	case SRP_TSK_ATTR_QTYPE_ACA_Q_TAG:
1008 		iu->iu_stmf_task->task_flags |=	TF_ATTR_ACA;
1009 		break;
1010 
1011 	default:
1012 		SRPT_DPRINTF_L2("ch_srp_cmd, reserved task attr (%d)",
1013 		    cmd->cr_task_attr);
1014 		iu->iu_stmf_task->task_flags |=	TF_ATTR_ORDERED_QUEUE;
1015 		break;
1016 	}
1017 	iu->iu_stmf_task->task_additional_flags = 0;
1018 	iu->iu_stmf_task->task_priority		= 0;
1019 	iu->iu_stmf_task->task_mgmt_function    = TM_NONE;
1020 	iu->iu_stmf_task->task_max_nbufs	= STMF_BUFS_MAX;
1021 	iu->iu_stmf_task->task_expected_xfer_length = iu->iu_tot_xfer_len;
1022 	iu->iu_stmf_task->task_csn_size		= 0;
1023 
1024 	bcopy(cmd->cr_cdb, iu->iu_stmf_task->task_cdb,
1025 	    SRP_CDB_SIZE);
1026 	if (addlen != 0) {
1027 		bcopy(&cmd->cr_add_data,
1028 		    iu->iu_stmf_task->task_cdb + SRP_CDB_SIZE,
1029 		    addlen * 4);
1030 	}
1031 
1032 	/*
1033 	 * Add the IU/task to the session and post to STMF.  The task will
1034 	 * remain in the session's list until STMF is informed by SRP that
1035 	 * it is done with the task.
1036 	 */
1037 	DTRACE_SRP_3(scsi__command, srpt_channel_t, iu->iu_ch,
1038 	    scsi_task_t, iu->iu_stmf_task, srp_cmd_req_t, cmd);
1039 	srpt_stp_add_task(ch->ch_session, iu);
1040 
1041 	SRPT_DPRINTF_L3("ch_srp_cmd, new task (%p) posted",
1042 	    (void *)iu->iu_stmf_task);
1043 	stmf_post_task(iu->iu_stmf_task, NULL);
1044 	rw_exit(&ch->ch_rwlock);
1045 
1046 	return (0);
1047 }
1048 
1049 /*
1050  * srpt_ch_task_mgmt_abort()
1051  *
1052  * Returns 0 on success, indicating we've sent a management response.
1053  * Returns !0 to indicate failure; the IU should be reposted.
1054  */
1055 static ibt_status_t
1056 srpt_ch_task_mgmt_abort(srpt_channel_t *ch, srpt_iu_t *iu,
1057 	uint64_t tag_to_abort)
1058 {
1059 	srpt_session_t	*session = ch->ch_session;
1060 	srpt_iu_t	*ss_iu;
1061 	ibt_status_t	status;
1062 
1063 	/*
1064 	 * Locate the associated task (tag_to_abort) in the
1065 	 * session's active task list.
1066 	 */
1067 	rw_enter(&session->ss_rwlock, RW_READER);
1068 	ss_iu = list_head(&session->ss_task_list);
1069 	while (ss_iu != NULL) {
1070 		mutex_enter(&ss_iu->iu_lock);
1071 		if ((tag_to_abort == ss_iu->iu_tag)) {
1072 			mutex_exit(&ss_iu->iu_lock);
1073 			break;
1074 		}
1075 		mutex_exit(&ss_iu->iu_lock);
1076 		ss_iu = list_next(&session->ss_task_list, ss_iu);
1077 	}
1078 	rw_exit(&session->ss_rwlock);
1079 
1080 	/*
1081 	 * Take appropriate action based on state of task
1082 	 * to be aborted:
1083 	 * 1) No longer exists - do nothing.
1084 	 * 2) Previously aborted or status queued - do nothing.
1085 	 * 3) Otherwise - initiate abort.
1086 	 */
1087 	if (ss_iu == NULL)  {
1088 		goto send_mgmt_resp;
1089 	}
1090 
1091 	mutex_enter(&ss_iu->iu_lock);
1092 	if ((ss_iu->iu_flags & (SRPT_IU_STMF_ABORTING |
1093 	    SRPT_IU_ABORTED | SRPT_IU_RESP_SENT)) != 0) {
1094 		mutex_exit(&ss_iu->iu_lock);
1095 		goto send_mgmt_resp;
1096 	}
1097 
1098 	/*
1099 	 * Set aborting flag and notify STMF of abort request.  No
1100 	 * additional I/O will be queued for this IU.
1101 	 */
1102 	SRPT_DPRINTF_L3("ch_task_mgmt_abort, task found");
1103 	ss_iu->iu_flags |= SRPT_IU_SRP_ABORTING;
1104 	mutex_exit(&ss_iu->iu_lock);
1105 	stmf_abort(STMF_QUEUE_TASK_ABORT,
1106 	    ss_iu->iu_stmf_task, STMF_ABORTED, NULL);
1107 
1108 send_mgmt_resp:
1109 	mutex_enter(&iu->iu_lock);
1110 	status = srpt_stp_send_mgmt_response(iu, SRP_TM_SUCCESS,
1111 	    SRPT_FENCE_SEND);
1112 	mutex_exit(&iu->iu_lock);
1113 
1114 	if (status != IBT_SUCCESS) {
1115 		SRPT_DPRINTF_L2("ch_task_mgmt_abort, err(%d)"
1116 		    " posting abort response", status);
1117 	}
1118 
1119 	return (status);
1120 }
1121 
1122 /*
1123  * srpt_ch_srp_task_mgmt()
1124  */
1125 static int
1126 srpt_ch_srp_task_mgmt(srpt_channel_t *ch, srpt_iu_t *iu)
1127 {
1128 	srp_tsk_mgmt_t		*tsk = (srp_tsk_mgmt_t *)iu->iu_buf;
1129 	uint8_t			tm_fn;
1130 	ibt_status_t		status;
1131 
1132 	SRPT_DPRINTF_L3("ch_srp_task_mgmt, SRP TASK MGMT func(%d)",
1133 	    tsk->tm_function);
1134 
1135 	/*
1136 	 * Both tag and lun fileds have the same corresponding offsets
1137 	 * in both srp_tsk_mgmt_t and srp_cmd_req_t structures.  The
1138 	 * casting will allow us to use the same dtrace translator.
1139 	 */
1140 	DTRACE_SRP_2(task__command, srpt_channel_t, ch,
1141 	    srp_cmd_req_t, (srp_cmd_req_t *)tsk);
1142 
1143 	iu->iu_ch  = ch;
1144 	iu->iu_tag = tsk->tm_tag;
1145 
1146 	/*
1147 	 * Task management aborts are processed directly by the SRP driver;
1148 	 * all other task management requests are handed off to STMF.
1149 	 */
1150 	switch (tsk->tm_function) {
1151 	case SRP_TSK_MGMT_ABORT_TASK:
1152 		/*
1153 		 * Initiate SCSI transport protocol specific task abort
1154 		 * logic.
1155 		 */
1156 		status = srpt_ch_task_mgmt_abort(ch, iu, tsk->tm_task_tag);
1157 		if (status != IBT_SUCCESS) {
1158 			/* repost this IU */
1159 			return (1);
1160 		} else {
1161 			return (0);
1162 		}
1163 
1164 	case SRP_TSK_MGMT_ABORT_TASK_SET:
1165 		tm_fn = TM_ABORT_TASK_SET;
1166 		break;
1167 
1168 	case SRP_TSK_MGMT_CLEAR_TASK_SET:
1169 		tm_fn = TM_CLEAR_TASK_SET;
1170 		break;
1171 
1172 	case SRP_TSK_MGMT_LUN_RESET:
1173 		tm_fn = TM_LUN_RESET;
1174 		break;
1175 
1176 	case SRP_TSK_MGMT_CLEAR_ACA:
1177 		tm_fn = TM_CLEAR_ACA;
1178 		break;
1179 
1180 	default:
1181 		/*
1182 		 * SRP does not support the requested task management
1183 		 * function; return a not supported status in the response.
1184 		 */
1185 		SRPT_DPRINTF_L2("ch_srp_task_mgmt, SRP task mgmt fn(%d)"
1186 		    " not supported", tsk->tm_function);
1187 		mutex_enter(&iu->iu_lock);
1188 		status = srpt_stp_send_mgmt_response(iu,
1189 		    SRP_TM_NOT_SUPPORTED, SRPT_NO_FENCE_SEND);
1190 		mutex_exit(&iu->iu_lock);
1191 		if (status != IBT_SUCCESS) {
1192 			SRPT_DPRINTF_L2("ch_srp_task_mgmt, err(%d) posting"
1193 			    " response", status);
1194 			return (1);
1195 		}
1196 		return (0);
1197 	}
1198 
1199 	rw_enter(&ch->ch_rwlock, RW_READER);
1200 	if (ch->ch_state == SRPT_CHANNEL_DISCONNECTING) {
1201 		/*
1202 		 * The channel has begun disconnecting, so ignore the
1203 		 * the command returning the IU resources.
1204 		 */
1205 		rw_exit(&ch->ch_rwlock);
1206 		return (1);
1207 	}
1208 
1209 	/*
1210 	 * Once a SCSI mgmt task is allocated and assigned to the IU, it
1211 	 * owns those IU resources, which will be held until we inform
1212 	 * STMF that we are done with the task (from an lports perspective).
1213 	 */
1214 	iu->iu_stmf_task = stmf_task_alloc(ch->ch_tgt->tp_lport,
1215 	    ch->ch_session->ss_ss, tsk->tm_lun, 0, STMF_TASK_EXT_NONE);
1216 	if (iu->iu_stmf_task == NULL) {
1217 		/*
1218 		 * Could not allocate, return status to the initiator
1219 		 * indicating that we are temporarily unable to process
1220 		 * commands.  If unable to send, immediately return IU
1221 		 * resource.
1222 		 */
1223 		SRPT_DPRINTF_L2("ch_srp_task_mgmt, SCSI task allocation"
1224 		    " failure");
1225 		rw_exit(&ch->ch_rwlock);
1226 		mutex_enter(&iu->iu_lock);
1227 		status = srpt_stp_send_response(iu, STATUS_BUSY, 0, 0, 0,
1228 		    NULL, SRPT_NO_FENCE_SEND);
1229 		mutex_exit(&iu->iu_lock);
1230 		if (status != IBT_SUCCESS) {
1231 			SRPT_DPRINTF_L2("ch_srp_task_mgmt, err(%d) posting"
1232 			    "busy response", status);
1233 			/* repost the IU */
1234 			return (1);
1235 		}
1236 		return (0);
1237 	}
1238 
1239 	iu->iu_stmf_task->task_port_private = iu;
1240 	iu->iu_stmf_task->task_flags = 0;
1241 	iu->iu_stmf_task->task_additional_flags =
1242 	    TASK_AF_NO_EXPECTED_XFER_LENGTH;
1243 	iu->iu_stmf_task->task_priority = 0;
1244 	iu->iu_stmf_task->task_mgmt_function = tm_fn;
1245 	iu->iu_stmf_task->task_max_nbufs = STMF_BUFS_MAX;
1246 	iu->iu_stmf_task->task_expected_xfer_length = 0;
1247 	iu->iu_stmf_task->task_csn_size = 0;
1248 
1249 	/*
1250 	 * Add the IU/task to the session and post to STMF.  The task will
1251 	 * remain in the session's list until STMF is informed by SRP that
1252 	 * it is done with the task.
1253 	 */
1254 	srpt_stp_add_task(ch->ch_session, iu);
1255 
1256 	SRPT_DPRINTF_L3("ch_srp_task_mgmt, new mgmt task(%p) posted",
1257 	    (void *)iu->iu_stmf_task);
1258 	stmf_post_task(iu->iu_stmf_task, NULL);
1259 	rw_exit(&ch->ch_rwlock);
1260 
1261 	return (0);
1262 }
1263 
1264 /*
1265  * srpt_ch_process_iu()
1266  */
1267 static void
1268 srpt_ch_process_iu(srpt_channel_t *ch, srpt_iu_t *iu)
1269 {
1270 	srpt_iu_data_t	*iud;
1271 	int		status = 1;
1272 
1273 	/*
1274 	 * IU adds reference to channel which will represent a
1275 	 * a reference by STMF.  If for whatever reason the IU
1276 	 * is not handed off to STMF, then this reference will be
1277 	 * released.  Otherwise, the reference will be released when
1278 	 * SRP informs STMF that the associated SCSI task is done.
1279 	 */
1280 	srpt_ch_add_ref(ch);
1281 
1282 	/*
1283 	 * Validate login RC channel state. Normally active, if
1284 	 * not active then we need to handle a possible race between the
1285 	 * receipt of a implied RTU and CM calling back to notify of the
1286 	 * state transition.
1287 	 */
1288 	rw_enter(&ch->ch_rwlock, RW_READER);
1289 	if (ch->ch_state == SRPT_CHANNEL_DISCONNECTING) {
1290 		rw_exit(&ch->ch_rwlock);
1291 		goto repost_iu;
1292 	}
1293 	rw_exit(&ch->ch_rwlock);
1294 
1295 	iud = iu->iu_buf;
1296 
1297 	switch (iud->rx_iu.srp_op) {
1298 	case SRP_IU_CMD:
1299 		status = srpt_ch_srp_cmd(ch, iu);
1300 		break;
1301 
1302 	case SRP_IU_TASK_MGMT:
1303 		status = srpt_ch_srp_task_mgmt(ch, iu);
1304 		return;
1305 
1306 	case SRP_IU_I_LOGOUT:
1307 		SRPT_DPRINTF_L3("ch_process_iu, SRP INITIATOR LOGOUT");
1308 		/*
1309 		 * Initiators should logout by issuing a CM disconnect
1310 		 * request (DREQ) with the logout IU in the private data;
1311 		 * however some initiators have been known to send the
1312 		 * IU in-band, if this happens just initiate the logout.
1313 		 * Note that we do not return a response as per the
1314 		 * specification.
1315 		 */
1316 		srpt_stp_logout(ch);
1317 		break;
1318 
1319 	case SRP_IU_AER_RSP:
1320 	case SRP_IU_CRED_RSP:
1321 	default:
1322 		/*
1323 		 * We don't send asynchronous events or ask for credit
1324 		 * adjustments, so nothing need be done.  Log we got an
1325 		 * unexpected IU but then just repost the IU to the SRQ.
1326 		 */
1327 		SRPT_DPRINTF_L2("ch_process_iu, invalid IU from initiator,"
1328 		    " IU opcode(%d)", iud->rx_iu.srp_op);
1329 		break;
1330 	}
1331 
1332 	if (status == 0) {
1333 		return;
1334 	}
1335 
1336 repost_iu:
1337 	SRPT_DPRINTF_L4("process_iu:  reposting iu %p", (void *)iu);
1338 	mutex_enter(&iu->iu_lock);
1339 	srpt_ioc_repost_recv_iu(iu->iu_ioc, iu);
1340 	mutex_exit(&iu->iu_lock);
1341 	srpt_ch_release_ref(ch, 0);
1342 }
1343 
1344 /*
1345  * srpt_ch_post_send
1346  */
1347 ibt_status_t
1348 srpt_ch_post_send(srpt_channel_t *ch, srpt_iu_t *iu, uint32_t len,
1349 	uint_t fence)
1350 {
1351 	ibt_status_t		status;
1352 	ibt_send_wr_t		wr;
1353 	ibt_wr_ds_t		ds;
1354 	uint_t			posted;
1355 
1356 	ASSERT(ch != NULL);
1357 	ASSERT(iu != NULL);
1358 	ASSERT(mutex_owned(&iu->iu_lock));
1359 
1360 	rw_enter(&ch->ch_rwlock, RW_READER);
1361 	if (ch->ch_state == SRPT_CHANNEL_DISCONNECTING) {
1362 		rw_exit(&ch->ch_rwlock);
1363 		SRPT_DPRINTF_L2("ch_post_send, bad ch state (%d)",
1364 		    ch->ch_state);
1365 		return (IBT_FAILURE);
1366 	}
1367 	rw_exit(&ch->ch_rwlock);
1368 
1369 	wr.wr_id = srpt_ch_alloc_swqe_wrid(ch, SRPT_SWQE_TYPE_RESP,
1370 	    (void *)iu);
1371 	if (wr.wr_id == 0) {
1372 		SRPT_DPRINTF_L2("ch_post_send, queue full");
1373 		return (IBT_FAILURE);
1374 	}
1375 
1376 	atomic_inc_32(&iu->iu_sq_posted_cnt);
1377 
1378 	wr.wr_flags = IBT_WR_SEND_SIGNAL;
1379 	if (fence == SRPT_FENCE_SEND) {
1380 		wr.wr_flags |= IBT_WR_SEND_FENCE;
1381 	}
1382 	wr.wr_opcode = IBT_WRC_SEND;
1383 	wr.wr_trans  = IBT_RC_SRV;
1384 	wr.wr_nds = 1;
1385 	wr.wr_sgl = &ds;
1386 
1387 	ds.ds_va = iu->iu_sge.ds_va;
1388 	ds.ds_key = iu->iu_sge.ds_key;
1389 	ds.ds_len = len;
1390 
1391 	SRPT_DPRINTF_L4("ch_post_send, posting SRP response to channel"
1392 	    " ds.ds_va (0x%16llx), ds.ds_key (0x%08x), "
1393 	    " ds.ds_len (%d)",
1394 	    (u_longlong_t)ds.ds_va, ds.ds_key, ds.ds_len);
1395 
1396 	status = ibt_post_send(ch->ch_chan_hdl, &wr, 1, &posted);
1397 	if (status != IBT_SUCCESS) {
1398 		SRPT_DPRINTF_L2("ch_post_send, post_send failed (%d)",
1399 		    status);
1400 		atomic_dec_32(&iu->iu_sq_posted_cnt);
1401 		srpt_ch_free_swqe_wrid(ch, wr.wr_id);
1402 		return (status);
1403 	}
1404 
1405 	return (IBT_SUCCESS);
1406 }
1407 
1408 /*
1409  * srpt_ch_alloc_swqe_wrid()
1410  */
1411 ibt_wrid_t
1412 srpt_ch_alloc_swqe_wrid(srpt_channel_t *ch,
1413 	srpt_swqe_type_t wqe_type, void *addr)
1414 {
1415 	ibt_wrid_t	wrid;
1416 
1417 	mutex_enter(&ch->ch_swqe_lock);
1418 	if (ch->ch_head == ch->ch_tail) {
1419 		mutex_exit(&ch->ch_swqe_lock);
1420 		return ((ibt_wrid_t)0);
1421 	}
1422 	wrid = (ibt_wrid_t)ch->ch_head;
1423 	ch->ch_swqe[ch->ch_head].sw_type = wqe_type;
1424 	ch->ch_swqe[ch->ch_head].sw_addr = addr;
1425 	ch->ch_head = ch->ch_swqe[ch->ch_head].sw_next;
1426 	ch->ch_swqe_posted++;
1427 	mutex_exit(&ch->ch_swqe_lock);
1428 	return (wrid);
1429 }
1430 
1431 /*
1432  * srpt_ch_free_swqe_wrid()
1433  */
1434 void
1435 srpt_ch_free_swqe_wrid(srpt_channel_t *ch, ibt_wrid_t id)
1436 {
1437 	mutex_enter(&ch->ch_swqe_lock);
1438 	ch->ch_swqe[ch->ch_tail].sw_next = id;
1439 	ch->ch_tail = (uint32_t)id;
1440 	ch->ch_swqe_posted--;
1441 	mutex_exit(&ch->ch_swqe_lock);
1442 }
1443