xref: /illumos-gate/usr/src/uts/common/io/comstar/port/srpt/srpt_stp.c (revision 828d47c166ce67972b1f1929669b9af5be769423)
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 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * SCSI Target Port I/F for Solaris SCSI RDMA Protocol Target (SRP)
29  * 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/atomic.h>
44 
45 #include <stmf.h>
46 #include <stmf_ioctl.h>
47 #include <portif.h>
48 
49 #include <sys/ib/mgt/ibdma/ibdma.h>
50 
51 #include "srp.h"
52 #include "srpt_impl.h"
53 #include "srpt_cm.h"
54 #include "srpt_ioc.h"
55 #include "srpt_ch.h"
56 #include "srpt_stp.h"
57 
58 extern srpt_ctxt_t	*srpt_ctxt;
59 
60 /*
61  * STMF LPort Interface Prototypes
62  */
63 static stmf_status_t srpt_stp_xfer_data(struct scsi_task *task,
64 	struct stmf_data_buf *dbuf, uint32_t ioflags);
65 stmf_status_t srpt_stp_send_status(struct scsi_task *task,
66 	uint32_t ioflags);
67 static void srpt_stp_task_free(struct scsi_task *task);
68 static stmf_status_t srpt_stp_abort(struct stmf_local_port *lport,
69 	int abort_cmd, void *arg, uint32_t flags);
70 static void srpt_stp_task_poll(struct scsi_task *task);
71 static void srpt_stp_ctl(struct stmf_local_port *lport,
72 	int cmd, void *arg);
73 static stmf_status_t srpt_stp_info(uint32_t cmd,
74 	struct stmf_local_port *lport, void *arg, uint8_t *buf,
75 	uint32_t *bufsizep);
76 static void srpt_stp_event_handler(struct stmf_local_port *lport,
77 	int eventid, void *arg, uint32_t flags);
78 
79 static void srpt_format_login_rsp(srp_login_req_t *req,
80 	srp_login_rsp_t *rsp, uint8_t flags);
81 static void srpt_format_login_rej(srp_login_req_t *req,
82 	srp_login_rej_t *rej, uint32_t reason);
83 
84 static scsi_devid_desc_t *srpt_stp_alloc_scsi_devid_desc(uint64_t guid);
85 static void srpt_stp_free_scsi_devid_desc(scsi_devid_desc_t *sdd);
86 
87 extern uint16_t srpt_send_msg_depth;
88 
89 /*
90  * srpt_stp_start_srp() - Start SRP service
91  *
92  * Enable the SRP service for the specified SCSI Target Port.
93  */
94 int
95 srpt_stp_start_srp(srpt_target_port_t *tgt)
96 {
97 	ibt_status_t		status;
98 	ibdma_status_t		dma_status;
99 	int			port;
100 	srpt_ioc_t		*ioc;
101 
102 	if (tgt == NULL) {
103 		SRPT_DPRINTF_L1("stp_start_srp, NULL SCSI target port");
104 		return (IBT_FAILURE);
105 	}
106 
107 	if (tgt->tp_ioc == NULL) {
108 		SRPT_DPRINTF_L1("stp_start_srp, SCSI target port NULL"
109 		    " IOC pointer");
110 		return (IBT_FAILURE);
111 	}
112 	ioc = tgt->tp_ioc;
113 
114 	SRPT_DPRINTF_L2("stp_start_srp, register SRP service for"
115 	    " svc_id (%016llx)", (u_longlong_t)tgt->tp_ibt_svc_id);
116 	status = ibt_register_service(srpt_ctxt->sc_ibt_hdl,
117 	    &tgt->tp_ibt_svc_desc, tgt->tp_ibt_svc_id, 1,
118 	    &tgt->tp_ibt_svc_hdl, NULL);
119 	if (status != IBT_SUCCESS) {
120 		tgt->tp_ibt_svc_hdl = NULL;
121 		SRPT_DPRINTF_L1("stp_start_srp, SRP service creation err (%d)",
122 		    status);
123 		return (status);
124 	}
125 
126 	/*
127 	 * Bind the service associated with the SCSI target port to
128 	 * each active port of the I/O Controller.
129 	 */
130 	for (port = 0; port < ioc->ioc_attr.hca_nports; port++) {
131 		status = srpt_ioc_svc_bind(tgt, port+1);
132 		if (status != IBT_SUCCESS &&
133 		    status != IBT_HCA_PORT_NOT_ACTIVE) {
134 			SRPT_DPRINTF_L1("start_srp, Unable to bind"
135 			    " service (%d)", status);
136 			goto srp_start_err;
137 		}
138 	}
139 	tgt->tp_srp_enabled = 1;
140 
141 	/*
142 	 * Calculate the new I/O Controller profile and either update the
143 	 * profile if previously registered or register it with the IB
144 	 * Device Management Agent.
145 	 */
146 	SRPT_DPRINTF_L3("start_srp, update I/O Controller profile (%016llx)",
147 	    (u_longlong_t)ioc->ioc_guid);
148 
149 	srpt_ioc_init_profile(ioc);
150 	if (ioc->ioc_ibdma_hdl == NULL) {
151 		ioc->ioc_ibdma_hdl =
152 		    srpt_ctxt->sc_ibdma_ops.ibdma_register(ioc->ioc_guid,
153 		    &ioc->ioc_profile, &ioc->ioc_svc);
154 		if (ioc->ioc_ibdma_hdl == NULL) {
155 			SRPT_DPRINTF_L1("start_srp, Unable to register"
156 			    " I/O Profile (%d)", status);
157 			goto srp_start_err;
158 		}
159 	} else {
160 		dma_status =
161 		    srpt_ctxt->sc_ibdma_ops.ibdma_update(ioc->ioc_ibdma_hdl,
162 		    &ioc->ioc_profile, &ioc->ioc_svc);
163 		if (dma_status != IBDMA_SUCCESS) {
164 			SRPT_DPRINTF_L1("start_srp, Unable to update I/O"
165 			    " Profile (%d)", dma_status);
166 			goto srp_start_err;
167 		}
168 	}
169 
170 	return (IBT_SUCCESS);
171 
172 srp_start_err:
173 	tgt->tp_srp_enabled = 0;
174 	srpt_ioc_svc_unbind_all(tgt);
175 	if (tgt->tp_ibt_svc_hdl != NULL) {
176 		ibt_deregister_service(srpt_ctxt->sc_ibt_hdl,
177 		    tgt->tp_ibt_svc_hdl);
178 		tgt->tp_ibt_svc_hdl = NULL;
179 	}
180 	return (status);
181 }
182 
183 /*
184  * srpt_stp_stop_srp() - Stop SRP service.
185  *
186  * Disable the SRP service on the specified SCSI Target Port.
187  */
188 void
189 srpt_stp_stop_srp(srpt_target_port_t *tgt)
190 {
191 	ibt_status_t		status;
192 	ibdma_status_t		dma_status;
193 	srpt_ioc_t		*ioc;
194 	srpt_channel_t		*ch;
195 
196 	if (tgt == NULL) {
197 		SRPT_DPRINTF_L2("stp_stop_srp, NULL SCSI Target Port"
198 		    " specified");
199 		return;
200 	}
201 
202 	if (tgt->tp_ioc == NULL) {
203 		SRPT_DPRINTF_L2("stp_stop_srp, bad Target, IOC NULL");
204 		return;
205 	}
206 	ioc = tgt->tp_ioc;
207 
208 	/*
209 	 * Update the I/O Controller profile to remove the SRP service
210 	 * for this SCSI target port.
211 	 */
212 	tgt->tp_srp_enabled = 0;
213 
214 	if (ioc->ioc_ibdma_hdl != NULL) {
215 		SRPT_DPRINTF_L3("stp_stop_srp, update I/O Controller"
216 		    " profile (%016llx)", (u_longlong_t)ioc->ioc_guid);
217 		srpt_ioc_init_profile(ioc);
218 
219 		if (ioc->ioc_profile.ioc_service_entries == 0) {
220 			SRPT_DPRINTF_L3("stp_stop_srp, no services active"
221 			    " unregister IOC profile");
222 			srpt_ctxt->sc_ibdma_ops.ibdma_unregister(
223 			    ioc->ioc_ibdma_hdl);
224 			ioc->ioc_ibdma_hdl = NULL;
225 		} else {
226 			dma_status = srpt_ctxt->sc_ibdma_ops.ibdma_update(
227 			    ioc->ioc_ibdma_hdl, &ioc->ioc_profile,
228 			    &ioc->ioc_svc);
229 			if (dma_status != IBDMA_SUCCESS) {
230 				SRPT_DPRINTF_L1("stp_stop_srp, Unable to"
231 				    " update I/O Profile (%d)", dma_status);
232 				return;
233 			}
234 		}
235 	}
236 
237 	/*
238 	 * Unbind the SRP service associated with the SCSI target port
239 	 * from all of the I/O Controller physical ports.
240 	 */
241 	SRPT_DPRINTF_L2("stp_stop_srp, unbind and de-register service"
242 	    "(%016llx)", (u_longlong_t)tgt->tp_ibt_svc_id);
243 	if (tgt->tp_ibt_svc_hdl != NULL) {
244 		srpt_ioc_svc_unbind_all(tgt);
245 	}
246 
247 	if (tgt->tp_ibt_svc_hdl != NULL) {
248 		status = ibt_deregister_service(srpt_ctxt->sc_ibt_hdl,
249 		    tgt->tp_ibt_svc_hdl);
250 		if (status != IBT_SUCCESS) {
251 			SRPT_DPRINTF_L1("stp_stop_srp, de-register service"
252 			    " error(%d)", status);
253 		}
254 		tgt->tp_ibt_svc_hdl = NULL;
255 	}
256 
257 	/*
258 	 * SRP service is now off-line for this SCSI Target Port.
259 	 * We force a disconnect (i.e. SRP Target Logout) for any
260 	 * active SRP logins.
261 	 */
262 	mutex_enter(&tgt->tp_ch_list_lock);
263 	ch = list_head(&tgt->tp_ch_list);
264 	while (ch != NULL) {
265 		SRPT_DPRINTF_L3("stp_stop_srp, disconnect ch(%p)",
266 		    (void *)ch);
267 		srpt_ch_disconnect(ch);
268 		ch = list_next(&tgt->tp_ch_list, ch);
269 	}
270 	mutex_exit(&tgt->tp_ch_list_lock);
271 
272 	/*
273 	 * wait for all sessions to terminate before returning
274 	 */
275 	mutex_enter(&tgt->tp_sess_list_lock);
276 	while (!list_is_empty(&tgt->tp_sess_list)) {
277 		cv_wait(&tgt->tp_sess_complete, &tgt->tp_sess_list_lock);
278 	}
279 	mutex_exit(&tgt->tp_sess_list_lock);
280 }
281 
282 /*
283  * srpt_stp_alloc_port() - Allocate SCSI Target Port
284  */
285 srpt_target_port_t *
286 srpt_stp_alloc_port(srpt_ioc_t *ioc, ib_guid_t guid)
287 {
288 	stmf_status_t		status;
289 	srpt_target_port_t	*tgt;
290 	stmf_local_port_t	*lport;
291 	uint64_t		temp;
292 
293 	if (ioc == NULL) {
294 		SRPT_DPRINTF_L1("stp_alloc_port, NULL I/O Controller");
295 		return (NULL);
296 	}
297 
298 	SRPT_DPRINTF_L3("stp_alloc_port, allocate STMF local port");
299 	lport = stmf_alloc(STMF_STRUCT_STMF_LOCAL_PORT, sizeof (*tgt), 0);
300 	if (lport == NULL) {
301 		SRPT_DPRINTF_L1("tgt_alloc_port, stmf_alloc failed");
302 		return (NULL);
303 	}
304 
305 	tgt = lport->lport_port_private;
306 	ASSERT(tgt != NULL);
307 
308 	mutex_init(&tgt->tp_lock, NULL, MUTEX_DRIVER, NULL);
309 
310 	mutex_init(&tgt->tp_ch_list_lock, NULL, MUTEX_DRIVER, NULL);
311 	cv_init(&tgt->tp_offline_complete, NULL, CV_DRIVER, NULL);
312 	list_create(&tgt->tp_ch_list, sizeof (srpt_channel_t),
313 	    offsetof(srpt_channel_t, ch_stp_node));
314 
315 	mutex_init(&tgt->tp_sess_list_lock, NULL, MUTEX_DRIVER, NULL);
316 	cv_init(&tgt->tp_sess_complete, NULL, CV_DRIVER, NULL);
317 	list_create(&tgt->tp_sess_list, sizeof (srpt_session_t),
318 	    offsetof(srpt_session_t, ss_node));
319 
320 	tgt->tp_state	 = SRPT_TGT_STATE_OFFLINE;
321 	tgt->tp_drv_disabled  = 0;
322 	tgt->tp_srp_enabled   = 0;
323 	tgt->tp_lport	 = lport;
324 	tgt->tp_ioc	   = ioc;
325 	tgt->tp_ibt_svc_id = guid;
326 	tgt->tp_ibt_svc_desc.sd_handler = srpt_cm_hdlr;
327 	tgt->tp_ibt_svc_desc.sd_flags   = IBT_SRV_NO_FLAGS;
328 	temp = h2b64(tgt->tp_ibt_svc_id);
329 	bcopy(&temp, &tgt->tp_srp_port_id[0], 8);
330 	temp = h2b64(tgt->tp_ioc->ioc_guid);
331 	bcopy(&temp, &tgt->tp_srp_port_id[8], 8);
332 
333 	tgt->tp_nports  = ioc->ioc_attr.hca_nports;
334 	tgt->tp_hw_port =
335 	    kmem_zalloc(sizeof (srpt_hw_port_t) * tgt->tp_nports, KM_SLEEP);
336 
337 	tgt->tp_scsi_devid = srpt_stp_alloc_scsi_devid_desc(tgt->tp_ibt_svc_id);
338 	lport->lport_id = tgt->tp_scsi_devid;
339 	lport->lport_pp = srpt_ctxt->sc_pp;
340 	lport->lport_ds	= ioc->ioc_stmf_ds;
341 	lport->lport_xfer_data	= &srpt_stp_xfer_data;
342 	lport->lport_send_status = &srpt_stp_send_status;
343 	lport->lport_task_free	= &srpt_stp_task_free;
344 	lport->lport_abort	= &srpt_stp_abort;
345 	lport->lport_task_poll	= &srpt_stp_task_poll;
346 	lport->lport_ctl	= &srpt_stp_ctl;
347 	lport->lport_info	= &srpt_stp_info;
348 	lport->lport_event_handler = &srpt_stp_event_handler;
349 
350 	SRPT_DPRINTF_L3("stp_alloc_port, register STMF LPORT");
351 
352 retry_registration:
353 	status = stmf_register_local_port(lport);
354 	if (status == STMF_SUCCESS) {
355 		SRPT_DPRINTF_L3("stp_alloc_port, LPORT successfully"
356 		    " registered");
357 		return (tgt);
358 	}
359 
360 	if (status == STMF_BUSY) {
361 		/*
362 		 * This is only done on an administrative thread of
363 		 * execution so it is ok to take a while.
364 		 */
365 		SRPT_DPRINTF_L3("stp_alloc_port, delaying");
366 		delay(2 * drv_usectohz(1000000));
367 		goto retry_registration;
368 	}
369 	SRPT_DPRINTF_L1("stp_alloc_port, STMF register local port err(0x%llx)",
370 	    (u_longlong_t)status);
371 
372 	SRPT_DPRINTF_L3("stp_alloc_port, free STMF local port");
373 	cv_destroy(&tgt->tp_offline_complete);
374 	mutex_destroy(&tgt->tp_ch_list_lock);
375 	mutex_destroy(&tgt->tp_lock);
376 	if (tgt->tp_hw_port) {
377 		kmem_free(tgt->tp_hw_port,
378 		    sizeof (srpt_hw_port_t) * tgt->tp_nports);
379 	}
380 	if (tgt->tp_scsi_devid) {
381 		srpt_stp_free_scsi_devid_desc(tgt->tp_scsi_devid);
382 	}
383 
384 	stmf_free(lport);
385 
386 	return (NULL);
387 }
388 
389 /*
390  * srpt_stp_free_port() - Free SCSI Target Port
391  */
392 stmf_status_t
393 srpt_stp_free_port(srpt_target_port_t *tgt)
394 {
395 	ASSERT(tgt != NULL);
396 	ASSERT(list_is_empty(&tgt->tp_sess_list));
397 	ASSERT(list_is_empty(&tgt->tp_ch_list));
398 
399 	list_destroy(&tgt->tp_ch_list);
400 	list_destroy(&tgt->tp_sess_list);
401 
402 	cv_destroy(&tgt->tp_sess_complete);
403 	cv_destroy(&tgt->tp_offline_complete);
404 
405 	mutex_destroy(&tgt->tp_sess_list_lock);
406 	mutex_destroy(&tgt->tp_ch_list_lock);
407 	mutex_destroy(&tgt->tp_lock);
408 
409 
410 	SRPT_DPRINTF_L3("stp_free_port, free STMF local port");
411 	if (tgt->tp_hw_port) {
412 		kmem_free(tgt->tp_hw_port,
413 		    sizeof (srpt_hw_port_t) * tgt->tp_nports);
414 	}
415 
416 	if (tgt->tp_scsi_devid) {
417 		srpt_stp_free_scsi_devid_desc(tgt->tp_scsi_devid);
418 	}
419 
420 	stmf_free(tgt->tp_lport);
421 
422 	return (STMF_SUCCESS);
423 }
424 
425 /*
426  * srpt_stp_deregister_port()
427  */
428 stmf_status_t
429 srpt_stp_deregister_port(srpt_target_port_t *tgt)
430 {
431 	stmf_status_t		status;
432 
433 	ASSERT(tgt != NULL);
434 	ASSERT(tgt->tp_lport != NULL);
435 
436 	SRPT_DPRINTF_L3("stp_deregister_port, de-register STMF LPORT");
437 
438 retry_deregistration:
439 	status = stmf_deregister_local_port(tgt->tp_lport);
440 	if (status == STMF_SUCCESS) {
441 		SRPT_DPRINTF_L3("stp_deregister_port, LPORT de-register"
442 		    " complete");
443 		return (status);
444 	}
445 	/*
446 	 * This is only done on an administrative thread of
447 	 * execution so it is ok to take a while.
448 	 */
449 	if (status == STMF_BUSY) {
450 		delay(drv_usectohz(1000000));
451 		goto retry_deregistration;
452 	}
453 
454 	/*
455 	 * Something other than a BUSY error, this should not happen.
456 	 */
457 	SRPT_DPRINTF_L1("stp_deregister_port, de-register STMF error(0x%llx)",
458 	    (u_longlong_t)status);
459 	return (status);
460 }
461 
462 /*
463  * srpt_stp_xfer_data()
464  */
465 /* ARGSUSED */
466 static stmf_status_t
467 srpt_stp_xfer_data(struct scsi_task *task, struct stmf_data_buf *dbuf,
468 	uint32_t ioflags)
469 {
470 	srpt_iu_t		*iu;
471 	srpt_channel_t		*ch;
472 	srpt_ds_dbuf_t		*db;
473 	ibt_send_wr_t		wr;
474 	ibt_wr_ds_t		ds;
475 	ibt_status_t		status;
476 	uint32_t		xfer_len;
477 	uint32_t		xferred_len;
478 	uint32_t		rdma_len;
479 	uint32_t		base_offset;
480 	uint32_t		desc_offset;
481 	srp_direct_desc_t	*desc;
482 
483 	SRPT_DPRINTF_L3("stp_xfer_data, invoked task (%p), dbuf (%p)",
484 	    (void *)task, (void *)dbuf);
485 	iu = task->task_port_private;
486 	ASSERT(iu != NULL);
487 	ASSERT(iu->iu_ch != NULL);
488 
489 	/*
490 	 * We should use iu->iu_ch->ch_swqe_posted to throttle
491 	 * send wqe posting. This is very unlikely because we limit
492 	 * the maximum number of initiator descriptors per IU (impact
493 	 * of fragmentation of intiator buffer space) but it could occur
494 	 * if the back-end (STMF) were to use too many small buffers. In
495 	 * that case we would want to return STMF_BUSY.
496 	 */
497 
498 	SRPT_DPRINTF_L4("stp_xfer_data, dbuf->db_flags (0x%x)",
499 	    dbuf->db_flags);
500 	SRPT_DPRINTF_L4("stp_xfer_data, dbuf->db_data_size (%d)",
501 	    dbuf->db_data_size);
502 	SRPT_DPRINTF_L4("stp_xfer_data, dbuf->db_relative_offset (%d)",
503 	    dbuf->db_relative_offset);
504 
505 	ASSERT((dbuf->db_flags & (DB_DIRECTION_TO_RPORT |
506 	    DB_DIRECTION_FROM_RPORT)) != (DB_DIRECTION_TO_RPORT |
507 	    DB_DIRECTION_FROM_RPORT));
508 
509 	db = dbuf->db_port_private;
510 
511 	/*
512 	 * Check to see if request will overflow the remote buffer; if so
513 	 * return a bad status and let STMF abort the task.
514 	 */
515 	if ((dbuf->db_relative_offset + dbuf->db_data_size) >
516 	    iu->iu_tot_xfer_len) {
517 		SRPT_DPRINTF_L2("stp_xfer_data, overflow of remote buffer");
518 		return (STMF_FAILURE);
519 	}
520 
521 	db->db_iu	= iu;
522 	wr.wr_trans  = IBT_RC_SRV;
523 	wr.wr_opcode = (dbuf->db_flags & DB_DIRECTION_TO_RPORT) ?
524 	    IBT_WRC_RDMAW : IBT_WRC_RDMAR;
525 	wr.wr_nds    = 1;
526 	wr.wr_sgl    = &ds;
527 
528 	/*
529 	 * We know that the data transfer is within the bounds described
530 	 * by our list of remote buffer descriptors.  Find the starting
531 	 * point based on the offset for the transfer, then perform the
532 	 * RDMA operations required of this transfer.
533 	 */
534 	base_offset = 0;
535 	desc = iu->iu_rdescs;
536 
537 	while ((base_offset + desc->dd_len) < dbuf->db_relative_offset) {
538 		base_offset += desc->dd_len;
539 		desc++;
540 	}
541 
542 	xfer_len    = dbuf->db_data_size;
543 	xferred_len = 0;
544 	desc_offset = dbuf->db_relative_offset - base_offset;
545 
546 	ch = iu->iu_ch;
547 
548 	/*
549 	 * If the channel is no longer connected then return an
550 	 * error and do not initiate I/O.  STMF should abort the
551 	 * task.
552 	 */
553 	rw_enter(&ch->ch_rwlock, RW_READER);
554 
555 	if (iu->iu_ch->ch_state == SRPT_CHANNEL_DISCONNECTING) {
556 		rw_exit(&iu->iu_ch->ch_rwlock);
557 		return (STMF_FAILURE);
558 	}
559 
560 	while (xfer_len > 0) {
561 		rdma_len = desc->dd_len - desc_offset;
562 
563 		/*
564 		 * We only generate completion entries on the last IB
565 		 * operation associated with any STMF buffer.
566 		 */
567 		if (rdma_len >= xfer_len) {
568 			rdma_len = xfer_len;
569 			wr.wr_flags  = IBT_WR_SEND_SIGNAL;
570 		} else {
571 			wr.wr_flags  = IBT_WR_NO_FLAGS;
572 		}
573 
574 		wr.wr.rc.rcwr.rdma.rdma_raddr = desc->dd_vaddr + desc_offset;
575 		wr.wr.rc.rcwr.rdma.rdma_rkey  = desc->dd_hdl;
576 		ds.ds_va  = db->db_sge.ds_va + xferred_len;
577 		ds.ds_key = db->db_sge.ds_key;
578 		ds.ds_len = rdma_len;
579 
580 		SRPT_DPRINTF_L4("stp_xfer_data, post RDMA operation");
581 
582 		/*
583 		 * If this task is being aborted or has been aborted,
584 		 * do not post additional I/O.
585 		 */
586 		mutex_enter(&iu->iu_lock);
587 		if ((iu->iu_flags & (SRPT_IU_SRP_ABORTING |
588 		    SRPT_IU_STMF_ABORTING | SRPT_IU_ABORTED)) != 0) {
589 			mutex_exit(&iu->iu_lock);
590 			rw_exit(&iu->iu_ch->ch_rwlock);
591 			return (STMF_SUCCESS);
592 		}
593 
594 		/*
595 		 * If a non-error CQE will be requested, add a reference to
596 		 * the IU and initialize the work request appropriately.
597 		 */
598 		if ((wr.wr_flags & IBT_WR_SEND_SIGNAL) != 0) {
599 			wr.wr_id = srpt_ch_alloc_swqe_wrid(ch,
600 			    SRPT_SWQE_TYPE_DATA, (void *)dbuf);
601 			if (wr.wr_id == 0) {
602 				rw_exit(&iu->iu_ch->ch_rwlock);
603 				mutex_exit(&iu->iu_lock);
604 				return (STMF_BUSY);
605 			}
606 			atomic_inc_32(&iu->iu_sq_posted_cnt);
607 		} else {
608 			wr.wr_id = 0;
609 		}
610 
611 		status = ibt_post_send(iu->iu_ch->ch_chan_hdl, &wr, 1, NULL);
612 		mutex_exit(&iu->iu_lock);
613 
614 		if (status != IBT_SUCCESS) {
615 			/*
616 			 * Could not post to IB transport, report to STMF and
617 			 * and let it initiate an abort of the task.
618 			 */
619 			SRPT_DPRINTF_L2("stp_xfer_data, post RDMA"
620 			    " error (%d)", status);
621 
622 			if ((wr.wr_flags & IBT_WR_SEND_SIGNAL) != 0) {
623 				srpt_ch_free_swqe_wrid(ch, wr.wr_id);
624 				atomic_dec_32(&iu->iu_sq_posted_cnt);
625 			}
626 			rw_exit(&iu->iu_ch->ch_rwlock);
627 			return (STMF_FAILURE);
628 		}
629 
630 		xferred_len += rdma_len;
631 		xfer_len    -= rdma_len;
632 		desc_offset  = 0;
633 		desc++;
634 	}
635 
636 	rw_exit(&ch->ch_rwlock);
637 	return (STMF_SUCCESS);
638 }
639 
640 /*
641  * srpt_stp_send_mgmt_response() - Return SRP task managment response IU
642  */
643 ibt_status_t
644 srpt_stp_send_mgmt_response(srpt_iu_t *iu, uint8_t srp_rsp,
645 	uint_t fence)
646 {
647 	srp_rsp_t	*rsp;
648 	srp_rsp_data_t	*data;
649 	uint32_t	rsp_length;
650 	ibt_status_t	status;
651 	uint8_t		*bufp;
652 
653 	ASSERT(mutex_owned(&iu->iu_lock));
654 	rsp = iu->iu_buf;
655 	bufp = (uint8_t *)iu->iu_buf + SRP_RSP_SIZE;
656 	bzero(rsp, SRP_RSP_SIZE + sizeof (srp_rsp_data_t));
657 	rsp->rsp_type = SRP_IU_RSP;
658 
659 	/*
660 	 * Report ULP credits we have added since last response sent
661 	 * over this channel.
662 	 */
663 	rsp->rsp_req_limit_delta =
664 	    h2b32(atomic_swap_32(&iu->iu_ch->ch_req_lim_delta, 0));
665 	rsp->rsp_tag = iu->iu_tag;
666 
667 	/* srp_rsp_t is padded out, so use explicit size here */
668 	rsp_length = SRP_RSP_SIZE;
669 	if (srp_rsp != SRP_TM_SUCCESS) {
670 		rsp->rsp_flags |= SRP_RSP_VALID;
671 		data = (srp_rsp_data_t *)bufp;
672 		data->rd_rsp_status = srp_rsp;
673 		rsp->rsp_data_len = h2b32(sizeof (srp_rsp_data_t));
674 		rsp_length += sizeof (srp_rsp_data_t);
675 	}
676 
677 	SRPT_DPRINTF_L4("stp_send_mgmt_response, sending on ch(%p),"
678 	    " iu(%p), mgmt status(%d)", (void *)iu->iu_ch,
679 	    (void *)iu, srp_rsp);
680 
681 	status = srpt_ch_post_send(iu->iu_ch, iu, rsp_length, fence);
682 	if (status != IBT_SUCCESS) {
683 		SRPT_DPRINTF_L2("stp_send_mgmt_response, post "
684 		    "response err(%d)", status);
685 	}
686 	return (status);
687 }
688 
689 /*
690  * srpt_stp_send_response() - Send SRP command response IU
691  */
692 ibt_status_t
693 srpt_stp_send_response(srpt_iu_t *iu, uint8_t scsi_status,
694 	uint8_t flags, uint32_t resid, uint16_t sense_length,
695 	uint8_t *sense_data, uint_t fence)
696 {
697 	srp_rsp_t	*rsp;
698 	uint32_t	rsp_length;
699 	uint8_t		*bufp;
700 	ibt_status_t	status;
701 
702 	ASSERT(mutex_owned(&iu->iu_lock));
703 	rsp = iu->iu_buf;
704 	bufp = (uint8_t *)iu->iu_buf + SRP_RSP_SIZE;
705 	bzero(rsp, SRP_RSP_SIZE);
706 	rsp->rsp_type = SRP_IU_RSP;
707 
708 	/*
709 	 * Report ULP credits we have added since last response sent
710 	 * over this channel.
711 	 */
712 	rsp->rsp_req_limit_delta =
713 	    h2b32(atomic_swap_32(&iu->iu_ch->ch_req_lim_delta, 0));
714 	rsp->rsp_tag = iu->iu_tag;
715 	rsp->rsp_status = scsi_status;
716 
717 	rsp_length = SRP_RSP_SIZE;
718 
719 	if (resid != 0) {
720 		rsp->rsp_flags |= flags;
721 
722 		if ((flags & SRP_RSP_DO_OVER) ||
723 		    (flags & SRP_RSP_DO_UNDER)) {
724 			rsp->rsp_do_resid_cnt = h2b32(resid);
725 		} else if ((flags & SRP_RSP_DI_OVER) ||
726 		    (flags & SRP_RSP_DI_UNDER)) {
727 			rsp->rsp_di_resid_cnt = h2b32(resid);
728 		}
729 	}
730 
731 	if (sense_length != 0) {
732 		rsp->rsp_flags |= SRP_RSP_SNS_VALID;
733 		if (SRP_RSP_SIZE + sense_length >
734 		    iu->iu_ch->ch_ti_iu_len) {
735 			sense_length = iu->iu_ch->ch_ti_iu_len -
736 			    SRP_RSP_SIZE;
737 		}
738 		bcopy(sense_data, bufp, sense_length);
739 		rsp->rsp_sense_data_len = h2b32(sense_length);
740 		rsp_length += sense_length;
741 	}
742 
743 	SRPT_DPRINTF_L4("stp_send_reponse, sending on ch(%p),"
744 	    " iu(%p), length(%d)", (void *)iu->iu_ch,
745 	    (void *)iu, rsp_length);
746 
747 	status = srpt_ch_post_send(iu->iu_ch, iu, rsp_length, fence);
748 	if (status != IBT_SUCCESS) {
749 		SRPT_DPRINTF_L2("stp_send_response, post response err(%d)",
750 		    status);
751 	}
752 	return (status);
753 }
754 
755 /*
756  * srpt_stp_send_status()
757  */
758 /* ARGSUSED */
759 stmf_status_t
760 srpt_stp_send_status(struct scsi_task *task, uint32_t ioflags)
761 {
762 	srpt_iu_t	*iu;
763 	ibt_status_t	status;
764 
765 	ASSERT(task != NULL);
766 	iu = task->task_port_private;
767 
768 	ASSERT(iu != NULL);
769 	ASSERT(iu->iu_ch != NULL);
770 
771 	SRPT_DPRINTF_L3("stp_send_status, invoked task (%p)"
772 	    ", task_completion_status (%d)"
773 	    ", task_resid (%d)"
774 	    ", task_status_ctrl (%d)"
775 	    ", task_scsi_status (%d)"
776 	    ", task_sense_length (%d)"
777 	    ", task_sense_data (%p)",
778 	    (void *)task,
779 	    (int)task->task_completion_status,
780 	    task->task_resid,
781 	    task->task_status_ctrl,
782 	    task->task_scsi_status,
783 	    task->task_sense_length,
784 	    (void *)task->task_sense_data);
785 
786 	/*
787 	 * Indicate future aborts can not be initiated (although
788 	 * we will handle any that have been requested since the
789 	 * last I/O completed and before we are sending status).
790 	 */
791 	mutex_enter(&iu->iu_lock);
792 	iu->iu_flags |= SRPT_IU_RESP_SENT;
793 
794 	if ((iu->iu_flags & (SRPT_IU_STMF_ABORTING |
795 	    SRPT_IU_SRP_ABORTING | SRPT_IU_ABORTED)) != 0) {
796 		mutex_exit(&iu->iu_lock);
797 		return (STMF_FAILURE);
798 	}
799 
800 	/*
801 	 * Send SRP command response or SRP task mgmt response.
802 	 */
803 	if (task->task_mgmt_function == 0) {
804 		uint8_t		rsp_flags = 0;
805 		uint32_t	resbytes = 0;
806 
807 		if (task->task_status_ctrl == TASK_SCTRL_OVER) {
808 			resbytes = task->task_resid;
809 
810 			if (task->task_flags & TF_READ_DATA) {
811 				SRPT_DPRINTF_L3(
812 				    "stp_send_status, data out overrun");
813 				rsp_flags |= SRP_RSP_DO_OVER;
814 			} else if (task->task_flags & TF_WRITE_DATA) {
815 				SRPT_DPRINTF_L3(
816 				    "stp_send_status, data in overrun");
817 				rsp_flags |= SRP_RSP_DI_OVER;
818 			}
819 		} else if (task->task_status_ctrl == TASK_SCTRL_UNDER) {
820 			resbytes = task->task_resid;
821 
822 			if (task->task_flags & TF_READ_DATA) {
823 				SRPT_DPRINTF_L3(
824 				    "stp_send_status, data out underrun");
825 				rsp_flags |= SRP_RSP_DO_UNDER;
826 			} else if (task->task_flags & TF_WRITE_DATA) {
827 				SRPT_DPRINTF_L3(
828 				    "stp_send_status, data in underrun");
829 				rsp_flags |= SRP_RSP_DI_UNDER;
830 			}
831 		}
832 
833 		status = srpt_stp_send_response(iu,
834 		    task->task_scsi_status, rsp_flags, resbytes,
835 		    task->task_sense_length, task->task_sense_data, 0);
836 	} else {
837 		status = srpt_stp_send_mgmt_response(iu,
838 		    (task->task_scsi_status ?
839 		    SRP_TM_FAILED : SRP_TM_SUCCESS),
840 		    SRPT_FENCE_SEND);
841 	}
842 
843 	/*
844 	 * If we have an error posting the response return bad status
845 	 * to STMF and let it initiate an abort for the task.
846 	 */
847 	if (status != IBT_SUCCESS) {
848 		SRPT_DPRINTF_L2("stp_send_status, post response err(%d)",
849 		    status);
850 		mutex_exit(&iu->iu_lock);
851 		return (STMF_FAILURE);
852 	}
853 	mutex_exit(&iu->iu_lock);
854 	return (STMF_SUCCESS);
855 }
856 
857 /*
858  * srpt_stp_task_free() - STMF call-back.
859  */
860 static void
861 srpt_stp_task_free(struct scsi_task *task)
862 {
863 	srpt_iu_t	*iu;
864 	srpt_channel_t	*ch;
865 
866 	SRPT_DPRINTF_L3("stp_task_free, invoked task (%p)",
867 	    (void *)task);
868 
869 	iu = task->task_port_private;
870 	ASSERT(iu != NULL);
871 
872 	mutex_enter(&iu->iu_lock);
873 	ch = iu->iu_ch;
874 	mutex_exit(&iu->iu_lock);
875 
876 	ASSERT(ch != NULL);
877 	ASSERT(ch->ch_session != NULL);
878 
879 	/*
880 	 * Do not hold IU lock while task is being removed from
881 	 * the session list - possible deadlock if cleaning up
882 	 * channel when this is called.
883 	 */
884 	srpt_stp_remove_task(ch->ch_session, iu);
885 
886 	mutex_enter(&iu->iu_lock);
887 	iu->iu_stmf_task = NULL;
888 
889 	srpt_ioc_repost_recv_iu(iu->iu_ioc, iu);
890 
891 	mutex_exit(&iu->iu_lock);
892 
893 	srpt_ch_release_ref(ch, 0);
894 }
895 
896 /*
897  * srpt_stp_abort() - STMF call-back.
898  */
899 /* ARGSUSED */
900 static stmf_status_t
901 srpt_stp_abort(struct stmf_local_port *lport, int abort_cmd,
902 	void *arg, uint32_t flags)
903 {
904 	struct scsi_task	*task;
905 	srpt_iu_t		*iu;
906 	stmf_status_t		status;
907 
908 	SRPT_DPRINTF_L3("stp_abort, invoked lport (%p), arg (%p)",
909 	    (void *)lport, (void *)arg);
910 
911 	task = (struct scsi_task *)arg;
912 	ASSERT(task != NULL);
913 
914 	iu = (srpt_iu_t *)task->task_port_private;
915 	ASSERT(iu != NULL);
916 
917 	mutex_enter(&iu->iu_lock);
918 
919 	/*
920 	 * If no I/O is outstanding then immediately transition to
921 	 * aborted state.  If I/O are in progress, then indicate that an
922 	 * STMF abort has been requested and tell STMF we will complete
923 	 * it asynchronously.
924 	 */
925 	if (iu->iu_sq_posted_cnt == 0) {
926 		SRPT_DPRINTF_L3("stp_abort, no outstanding I/O for %p",
927 		    (void *)iu);
928 		iu->iu_flags |= SRPT_IU_ABORTED;
929 		mutex_exit(&iu->iu_lock);
930 		/* Synchronous abort - STMF will call task_free */
931 		status = STMF_ABORT_SUCCESS;
932 	} else {
933 		SRPT_DPRINTF_L3("stp_abort, %d outstanding I/O for %p",
934 		    iu->iu_sq_posted_cnt, (void *)iu);
935 		iu->iu_flags |= SRPT_IU_STMF_ABORTING;
936 		mutex_exit(&iu->iu_lock);
937 		status = STMF_SUCCESS;
938 	}
939 
940 	return (status);
941 }
942 
943 /*
944  * srpt_stp_task_poll() - STMF call-back
945  */
946 static void
947 srpt_stp_task_poll(struct scsi_task *task)
948 {
949 	SRPT_DPRINTF_L3("stp_task_poll, invoked, task (%p)",
950 	    (void *)task);
951 }
952 
953 /*
954  * srpt_stp_ctl() - STMF call-back
955  */
956 static void
957 srpt_stp_ctl(struct stmf_local_port *lport, int cmd, void *arg)
958 {
959 	stmf_state_change_info_t	*sc_info = arg;
960 	stmf_change_status_t		cstatus;
961 	stmf_status_t			status;
962 	srpt_target_port_t		*tgt;
963 
964 	ASSERT(sc_info != NULL);
965 	ASSERT(lport != NULL);
966 
967 	tgt = lport->lport_port_private;
968 	ASSERT(tgt->tp_ioc != NULL);
969 
970 	SRPT_DPRINTF_L2("stp_ctl, invoked for LPORT (0x%016llx), cmd (%d)",
971 	    (u_longlong_t)tgt->tp_ibt_svc_id, cmd);
972 
973 	cstatus.st_completion_status = STMF_SUCCESS;
974 	cstatus.st_additional_info = NULL;
975 
976 	switch (cmd) {
977 	case STMF_CMD_LPORT_ONLINE:
978 		SRPT_DPRINTF_L2("stp_ctl, LPORT_ONLINE command,"
979 		    " st_rflags(0x%llx)", (u_longlong_t)sc_info->st_rflags);
980 		/*
981 		 * If the SCSI Target Port is not enabled by the driver,
982 		 * don't start and instead return busy.  This is a
983 		 * creation/destruction transitional state and the will
984 		 * either go away or become enabled.
985 		 */
986 		mutex_enter(&tgt->tp_lock);
987 		if (tgt->tp_drv_disabled != 0) {
988 			SRPT_DPRINTF_L1("stp_ctl, set LPORT_ONLINE failed - "
989 			    "LPORT (0x%016llx) BUSY",
990 			    (u_longlong_t)tgt->tp_ibt_svc_id);
991 			cstatus.st_completion_status = STMF_BUSY;
992 		} else if (tgt->tp_state == SRPT_TGT_STATE_ONLINE) {
993 			cstatus.st_completion_status = STMF_ALREADY;
994 		} else if (tgt->tp_state != SRPT_TGT_STATE_OFFLINE) {
995 			cstatus.st_completion_status = STMF_INVALID_ARG;
996 		} else {
997 			tgt->tp_state = SRPT_TGT_STATE_ONLINING;
998 			status = srpt_stp_start_srp(tgt);
999 			if (status != STMF_SUCCESS) {
1000 				tgt->tp_state = SRPT_TGT_STATE_OFFLINE;
1001 				cstatus.st_completion_status = STMF_INVALID_ARG;
1002 			}
1003 		}
1004 		mutex_exit(&tgt->tp_lock);
1005 		SRPT_DPRINTF_L3("stp_ctl, (0x%016llx) LPORT_ONLINE command"
1006 		    " status (0x%llx)", (u_longlong_t)tgt->tp_ibt_svc_id,
1007 		    (u_longlong_t)cstatus.st_completion_status);
1008 		status = stmf_ctl(STMF_CMD_LPORT_ONLINE_COMPLETE, lport,
1009 		    &cstatus);
1010 		if (status != STMF_SUCCESS) {
1011 			SRPT_DPRINTF_L1("stp_ctl, ONLINE_COMPLETE returned"
1012 			    " error(0x%llx)", (u_longlong_t)status);
1013 		}
1014 		break;
1015 
1016 	case STMF_CMD_LPORT_OFFLINE:
1017 		SRPT_DPRINTF_L2("stp_ctl, LPORT_OFFLINE");
1018 		mutex_enter(&tgt->tp_lock);
1019 		if (tgt->tp_state == SRPT_TGT_STATE_OFFLINE) {
1020 			cstatus.st_completion_status = STMF_ALREADY;
1021 		} else if (tgt->tp_state != SRPT_TGT_STATE_ONLINE) {
1022 			cstatus.st_completion_status = STMF_INVALID_ARG;
1023 		} else {
1024 			tgt->tp_state = SRPT_TGT_STATE_OFFLINING;
1025 			srpt_stp_stop_srp(tgt);
1026 		}
1027 		mutex_exit(&tgt->tp_lock);
1028 		SRPT_DPRINTF_L3("stp_ctl, notify STMF OFFLINE complete"
1029 		    " (0x%016llx)", (u_longlong_t)tgt->tp_ibt_svc_id);
1030 		status = stmf_ctl(STMF_CMD_LPORT_OFFLINE_COMPLETE,
1031 		    lport, &cstatus);
1032 		if (status != STMF_SUCCESS) {
1033 			SRPT_DPRINTF_L1("stp_ctl, OFFLINE_COMPLETE returned"
1034 			    " error(0x%llx)", (u_longlong_t)status);
1035 		}
1036 		break;
1037 
1038 	case STMF_ACK_LPORT_ONLINE_COMPLETE:
1039 		SRPT_DPRINTF_L2("stp_ctl, LPORT_ONLINE_COMPLETE ACK from"
1040 		    " STMF");
1041 		mutex_enter(&tgt->tp_lock);
1042 		if (tgt->tp_state == SRPT_TGT_STATE_ONLINING) {
1043 			SRPT_DPRINTF_L2("stp_ctl, LPORT is ONLINE");
1044 			tgt->tp_state = SRPT_TGT_STATE_ONLINE;
1045 		} else {
1046 			SRPT_DPRINTF_L2("stp_ctl, LPORT not on-lining");
1047 		}
1048 		mutex_exit(&tgt->tp_lock);
1049 		break;
1050 
1051 	case STMF_ACK_LPORT_OFFLINE_COMPLETE:
1052 		SRPT_DPRINTF_L2("stp_ctl, LPORT_OFFLINE_COMPLETE ACK from"
1053 		    " STMF");
1054 		mutex_enter(&tgt->tp_lock);
1055 		if (tgt->tp_state == SRPT_TGT_STATE_OFFLINING) {
1056 			SRPT_DPRINTF_L2("stp_ctl, LPORT is OFFLINE");
1057 			tgt->tp_state = SRPT_TGT_STATE_OFFLINE;
1058 			cv_broadcast(&tgt->tp_offline_complete);
1059 		} else {
1060 			SRPT_DPRINTF_L2("stp_ctl, LPORT not off-lining");
1061 		}
1062 		mutex_exit(&tgt->tp_lock);
1063 		break;
1064 
1065 	default:
1066 		SRPT_DPRINTF_L2("stp_ctl, cmd (%d) not handled",
1067 		    cmd);
1068 		break;
1069 	}
1070 }
1071 
1072 /*
1073  * srpt_stp_info() - STMF call-back
1074  */
1075 /* ARGSUSED */
1076 static stmf_status_t
1077 srpt_stp_info(uint32_t cmd, struct stmf_local_port *lport,
1078 	void *arg, uint8_t *buf, uint32_t *bufsizep)
1079 {
1080 	SRPT_DPRINTF_L3("stp_info, invoked");
1081 	return (STMF_SUCCESS);
1082 }
1083 
1084 /*
1085  * srpt_stp_event_handler() - STMF call-back
1086  */
1087 /* ARGSUSED */
1088 static void
1089 srpt_stp_event_handler(struct stmf_local_port *lport, int eventid,
1090 	void *arg, uint32_t flags)
1091 {
1092 	SRPT_DPRINTF_L3("stp_event_handler, invoked");
1093 }
1094 
1095 /*
1096  * srpt_stp_alloc_scsi_devid_desc()
1097  *
1098  * Allocate and initialize a SCSI device ID descriptor for
1099  * the SRP protocol.  Names are eui.GUID format.
1100  *
1101  * Both extension and guid are passed in host order.
1102  */
1103 static scsi_devid_desc_t *
1104 srpt_stp_alloc_scsi_devid_desc(uint64_t guid)
1105 {
1106 	scsi_devid_desc_t	*sdd;
1107 
1108 	sdd = kmem_zalloc(sizeof (*sdd) + SRPT_EUI_ID_LEN + 1, KM_SLEEP);
1109 	sdd->protocol_id = PROTOCOL_SRP;
1110 	sdd->piv = 1;
1111 	sdd->code_set = CODE_SET_ASCII;
1112 	sdd->association = ID_IS_TARGET_PORT;
1113 	sdd->ident_length = SRPT_EUI_ID_LEN;
1114 	(void) sprintf((char *)sdd->ident, "eui.%016llX", (u_longlong_t)guid);
1115 	return (sdd);
1116 }
1117 
1118 /*
1119  * srpt_stp_free_scsi_devid_desc()
1120  *
1121  * Free a SRPT SCSI device ID descriptor previously allocated via
1122  * srpt_stp_alloc_scsi_devid_desc().
1123  */
1124 static void
1125 srpt_stp_free_scsi_devid_desc(scsi_devid_desc_t *sdd)
1126 {
1127 	kmem_free(sdd, sizeof (*sdd) + SRPT_EUI_ID_LEN + 1);
1128 }
1129 
1130 /*
1131  * srpt_stp_alloc_session()
1132  */
1133 srpt_session_t *
1134 srpt_stp_alloc_session(srpt_target_port_t *tgt,
1135 	uint8_t *i_id, uint8_t *t_id, uint8_t port)
1136 {
1137 	stmf_status_t		status;
1138 	srpt_session_t		*ss;
1139 	stmf_scsi_session_t	*stmf_ss;
1140 	uint64_t		i_guid;
1141 
1142 	ASSERT(tgt != NULL);
1143 	SRPT_DPRINTF_L3("stp_alloc_session, invoked");
1144 
1145 	mutex_enter(&tgt->tp_sess_list_lock);
1146 
1147 	i_guid = BE_IN64(&i_id[8]);
1148 
1149 	stmf_ss = stmf_alloc(STMF_STRUCT_SCSI_SESSION,
1150 	    sizeof (srpt_session_t), 0);
1151 	if (stmf_ss == NULL) {
1152 		SRPT_DPRINTF_L2("stp_alloc_session, stmf_alloc"
1153 		    " returned NULL");
1154 		mutex_exit(&tgt->tp_sess_list_lock);
1155 		return (NULL);
1156 	}
1157 	ss = stmf_ss->ss_port_private;
1158 	ASSERT(ss != NULL);
1159 
1160 
1161 	rw_init(&ss->ss_rwlock, NULL, RW_DRIVER, NULL);
1162 	list_create(&ss->ss_task_list, sizeof (srpt_iu_t),
1163 	    offsetof(srpt_iu_t, iu_ss_task_node));
1164 
1165 	stmf_ss->ss_rport_id = srpt_stp_alloc_scsi_devid_desc(i_guid);
1166 	stmf_ss->ss_lport    = tgt->tp_lport;
1167 
1168 	ss->ss_ss	= stmf_ss;
1169 	ss->ss_hw_port	= port;
1170 	ss->ss_tgt	= tgt;
1171 	bcopy(i_id, ss->ss_i_id, SRP_PORT_ID_LEN);
1172 	bcopy(t_id, ss->ss_t_id, SRP_PORT_ID_LEN);
1173 
1174 	/*
1175 	 * Set the alias to include the initiator extension, this will enable
1176 	 * the administrator to identify multiple unique sessions originating
1177 	 * from the same initiator.
1178 	 */
1179 	(void) sprintf(ss->ss_alias, "%016llx:%016llx",
1180 	    (u_longlong_t)BE_IN64(&ss->ss_i_id[0]),
1181 	    (u_longlong_t)BE_IN64(&ss->ss_i_id[8]));
1182 
1183 	stmf_ss->ss_rport_alias = ss->ss_alias;
1184 
1185 	status = stmf_register_scsi_session(tgt->tp_lport, stmf_ss);
1186 	if (status != STMF_SUCCESS) {
1187 		SRPT_DPRINTF_L1("stp_alloc_session, STMF register session"
1188 		    " err(0x%llx)", (u_longlong_t)status);
1189 		list_destroy(&ss->ss_task_list);
1190 		rw_destroy(&ss->ss_rwlock);
1191 		stmf_free(stmf_ss);
1192 		mutex_exit(&tgt->tp_sess_list_lock);
1193 		return (NULL);
1194 	}
1195 
1196 	list_insert_tail(&tgt->tp_sess_list, ss);
1197 	mutex_exit(&tgt->tp_sess_list_lock);
1198 	return (ss);
1199 }
1200 
1201 /*
1202  * srpt_stp_free_session()
1203  */
1204 void
1205 srpt_stp_free_session(srpt_session_t *session)
1206 {
1207 	stmf_scsi_session_t	*stmf_ss;
1208 	srpt_target_port_t	*tgt;
1209 
1210 	ASSERT(session != NULL);
1211 
1212 	tgt = session->ss_tgt;
1213 
1214 	ASSERT(tgt != NULL);
1215 
1216 	SRPT_DPRINTF_L3("stp_free_session, invoked");
1217 
1218 	mutex_enter(&tgt->tp_sess_list_lock);
1219 
1220 	stmf_ss = session->ss_ss;
1221 
1222 	list_destroy(&session->ss_task_list);
1223 	rw_destroy(&session->ss_rwlock);
1224 
1225 	stmf_deregister_scsi_session(tgt->tp_lport, stmf_ss);
1226 	srpt_stp_free_scsi_devid_desc(stmf_ss->ss_rport_id);
1227 
1228 	list_remove(&tgt->tp_sess_list, session);
1229 	cv_signal(&tgt->tp_sess_complete);
1230 	mutex_exit(&tgt->tp_sess_list_lock);
1231 	stmf_free(stmf_ss);
1232 }
1233 
1234 /*
1235  * srpt_stp_login() - SRP SCSI Target port login
1236  */
1237 srpt_channel_t *
1238 srpt_stp_login(srpt_target_port_t *tgt, srp_login_req_t *login,
1239 	srp_login_rsp_t *login_rsp, srp_login_rej_t *login_rej,
1240 	uint8_t login_port)
1241 {
1242 	uint32_t	reason;
1243 	uint32_t	req_it_ui_len;
1244 	uint8_t		rsp_flags;
1245 	srpt_ioc_t	*ioc;
1246 	srpt_channel_t	*ch = NULL;
1247 	srpt_channel_t	*next_ch = NULL;
1248 	srpt_session_t	*session = NULL;
1249 
1250 	ASSERT(tgt != NULL);
1251 	ASSERT(login != NULL);
1252 	ASSERT(login_rsp != NULL);
1253 	ASSERT(login_rej != NULL);
1254 
1255 	/*
1256 	 * The target lock taken here serializes logins to this target
1257 	 * and prevents an STMF target port from starting a control
1258 	 * operation to transition the target state while a login is
1259 	 * being processed.
1260 	 */
1261 	mutex_enter(&tgt->tp_lock);
1262 	ioc = tgt->tp_ioc;
1263 	if (ioc == NULL) {
1264 		SRPT_DPRINTF_L1("stp_login, NULL I/O Controller");
1265 		reason = SRP_LOGIN_REJ_UNABLE_TO_ASSOCIATE_I_T_NEXUS;
1266 		goto reject_login;
1267 	}
1268 
1269 	/*
1270 	 * Validate that the SRP Target ID in the login request specifies
1271 	 * this I/O Controller SCSI Target Port.
1272 	 */
1273 	if (memcmp(login->lreq_target_port_id, tgt->tp_srp_port_id,
1274 	    SRP_PORT_ID_LEN) != 0) {
1275 		SRPT_DPRINTF_L2("stp_login, SRP CM SVC target ID mismatch."
1276 		    " Incoming TgtID 0x%016llx:0x%016llx",
1277 		    (u_longlong_t)BE_IN64(&login->lreq_target_port_id[0]),
1278 		    (u_longlong_t)BE_IN64(&login->lreq_target_port_id[8]));
1279 
1280 		reason = SRP_LOGIN_REJ_UNABLE_TO_ASSOCIATE_I_T_NEXUS;
1281 		goto reject_login;
1282 	}
1283 
1284 	if (tgt->tp_state != SRPT_TGT_STATE_ONLINE) {
1285 		SRPT_DPRINTF_L2("stp_login, SRP Login target not on-line");
1286 		reason = SRP_LOGIN_REJ_UNABLE_TO_ASSOCIATE_I_T_NEXUS;
1287 		goto reject_login;
1288 	}
1289 
1290 	/*
1291 	 * Initiator requested IU size must be as large as the specification
1292 	 * minimum and no greater than what we chose to support.
1293 	 */
1294 	req_it_ui_len = b2h32(login->lreq_req_it_iu_len);
1295 	SRPT_DPRINTF_L2("stp_login, requested iu size = %d", req_it_ui_len);
1296 	if (req_it_ui_len > SRPT_DEFAULT_SEND_MSG_SIZE) {
1297 		SRPT_DPRINTF_L2("stp_login, SRP Login IU size (%d) too large",
1298 		    req_it_ui_len);
1299 		reason = SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE;
1300 		goto reject_login;
1301 	}
1302 	if (req_it_ui_len < SRP_MIN_IU_SIZE) {
1303 		SRPT_DPRINTF_L2("stp_login, SRP Login IU size (%d) too small",
1304 		    req_it_ui_len);
1305 		reason = SRP_LOGIN_REJ_NO_REASON;
1306 		goto reject_login;
1307 	}
1308 
1309 	SRPT_DPRINTF_L2("stp_login, login req InitID 0x%016llx:0x%016llx",
1310 	    (u_longlong_t)BE_IN64(&login->lreq_initiator_port_id[0]),
1311 	    (u_longlong_t)BE_IN64(&login->lreq_initiator_port_id[8]));
1312 	SRPT_DPRINTF_L2("stp_login, login req TgtID 0x%016llx:0x%016llx",
1313 	    (u_longlong_t)BE_IN64(&login->lreq_target_port_id[0]),
1314 	    (u_longlong_t)BE_IN64(&login->lreq_target_port_id[8]));
1315 
1316 	/*
1317 	 * Processing is based on either single channel or multi-channel
1318 	 * operation.  In single channel, all current logins for this
1319 	 * same I_T_Nexus should be logged out.  In multi-channel
1320 	 * mode we would add an additional channel to an existing
1321 	 * I_T_Nexus if one currently exists (i.e. reference the
1322 	 * same SCSI session).
1323 	 */
1324 	rsp_flags = SRP_MULTI_CH_RESULT_NO_EXISTING;
1325 
1326 	switch (login->lreq_req_flags & SRP_LOGIN_MULTI_CH_MASK) {
1327 
1328 	case SRP_LOGIN_MULTI_CH_SINGLE:
1329 		/*
1330 		 * Only a single channel may be associated with a I_T_Nexus.
1331 		 * Disconnect any channel with the same SRP Initiator and
1332 		 * SRP target IDs.
1333 		 */
1334 		mutex_enter(&tgt->tp_ch_list_lock);
1335 		ch = list_head(&tgt->tp_ch_list);
1336 		while (ch != NULL) {
1337 			SRPT_DPRINTF_L3("stp_login, compare session,"
1338 			    " ch_state(%d)", ch->ch_state);
1339 			next_ch = list_next(&tgt->tp_ch_list, ch);
1340 
1341 			if (ch->ch_state != SRPT_CHANNEL_CONNECTING &&
1342 			    ch->ch_state != SRPT_CHANNEL_CONNECTED) {
1343 				SRPT_DPRINTF_L3("stp_login, compare session,"
1344 				    " channel not active");
1345 				ch = next_ch;
1346 				continue;
1347 			}
1348 
1349 			ASSERT(ch->ch_session != NULL);
1350 			SRPT_DPRINTF_L3("stp_login, compare session"
1351 			    " I_ID 0x%016llx:0x%016llx",
1352 			    (u_longlong_t)b2h64(*((uint64_t *)(void *)
1353 			    &ch->ch_session->ss_i_id[0])),
1354 			    (u_longlong_t)b2h64(*((uint64_t *)(void *)
1355 			    &ch->ch_session->ss_i_id[8])));
1356 			SRPT_DPRINTF_L3("stp_login, compare session"
1357 			    " T_ID 0x%016llx:0x%016llx",
1358 			    (u_longlong_t)b2h64(*((uint64_t *)(void *)
1359 			    &ch->ch_session->ss_t_id[0])),
1360 			    (u_longlong_t)b2h64(*((uint64_t *)(void *)
1361 			    &ch->ch_session->ss_t_id[8])));
1362 			if ((bcmp(login->lreq_initiator_port_id,
1363 			    ch->ch_session->ss_i_id,
1364 			    SRP_PORT_ID_LEN) == 0) &&
1365 			    (bcmp(login->lreq_target_port_id,
1366 			    ch->ch_session->ss_t_id,
1367 			    SRP_PORT_ID_LEN) == 0)) {
1368 				/*
1369 				 * if a session is in the process of connecting,
1370 				 * reject subsequent equivalent requests.
1371 				 */
1372 				if (ch->ch_state == SRPT_CHANNEL_CONNECTING) {
1373 					reason = SRP_LOGIN_REJ_INIT_CH_LIMIT;
1374 					mutex_exit(&tgt->tp_ch_list_lock);
1375 					goto reject_login;
1376 				}
1377 
1378 				SRPT_DPRINTF_L2("stp_login, terminate"
1379 				    " existing login");
1380 				rsp_flags =
1381 				    SRP_MULTI_CH_RESULT_TERM_EXISTING;
1382 				srpt_ch_disconnect(ch);
1383 			}
1384 
1385 			ch = next_ch;
1386 		}
1387 		mutex_exit(&tgt->tp_ch_list_lock);
1388 
1389 		/* Create the new session for this SRP login */
1390 		session = srpt_stp_alloc_session(tgt,
1391 		    login->lreq_initiator_port_id,
1392 		    login->lreq_target_port_id, login_port);
1393 		if (session == NULL) {
1394 			SRPT_DPRINTF_L2("stp_login, session allocation"
1395 			    " failed");
1396 			reason = SRP_LOGIN_REJ_UNABLE_TO_ASSOCIATE_I_T_NEXUS;
1397 			goto reject_login;
1398 		}
1399 		break;
1400 
1401 	case SRP_LOGIN_MULTI_CH_MULTIPLE:
1402 		SRPT_DPRINTF_L2("stp_login, multichannel not supported yet");
1403 		reason = SRP_LOGIN_REJ_MULTI_CH_NOT_SUPPORTED;
1404 		goto reject_login;
1405 		/* break via goto */
1406 
1407 	default:
1408 		SRPT_DPRINTF_L2("stp_login, invalid multichannel field (%d)",
1409 		    login->lreq_req_flags & SRP_LOGIN_MULTI_CH_MASK);
1410 		reason = SRP_LOGIN_REJ_NO_REASON;
1411 		goto reject_login;
1412 		/* break via goto */
1413 	}
1414 
1415 	/*
1416 	 * Create new RDMA channel for this SRP login request.
1417 	 * The channel is returned with a single reference which
1418 	 * represents the reference held by the CM.
1419 	 */
1420 	ch = srpt_ch_alloc(tgt, login_port);
1421 	if (ch == NULL) {
1422 		SRPT_DPRINTF_L2("stp_login, unable to alloc RDMA channel");
1423 		reason = SRP_LOGIN_REJ_INSUFFICIENT_CH_RESOURCES;
1424 		srpt_stp_free_session(session);
1425 		goto reject_login;
1426 	}
1427 	ch->ch_session = session;
1428 	ch->ch_ti_iu_len = b2h32(login->lreq_req_it_iu_len);
1429 
1430 	/*
1431 	 * Add another reference to the channel which represents
1432 	 * a reference placed by the target port and add it to
1433 	 * the store of channels logged in for this target port.
1434 	 */
1435 	srpt_ch_add_ref(ch);
1436 	mutex_enter(&tgt->tp_ch_list_lock);
1437 	list_insert_tail(&tgt->tp_ch_list, ch);
1438 	mutex_exit(&tgt->tp_ch_list_lock);
1439 
1440 	srpt_format_login_rsp(login, login_rsp, rsp_flags);
1441 	mutex_exit(&tgt->tp_lock);
1442 	SRPT_DPRINTF_L2("stp_login, login successful");
1443 
1444 	return (ch);
1445 
1446 reject_login:
1447 	srpt_format_login_rej(login, login_rej, reason);
1448 	mutex_exit(&tgt->tp_lock);
1449 	return (NULL);
1450 }
1451 
1452 /*
1453  * srpt_stp_logout() - SRP logout
1454  *
1455  * Logout is not normally initiated in-band, but is so, just
1456  * initiate a disconnect.
1457  */
1458 void
1459 srpt_stp_logout(srpt_channel_t *ch)
1460 {
1461 	SRPT_DPRINTF_L2("stp_logout, invoked for ch (%p)",
1462 	    (void *)ch);
1463 	srpt_ch_disconnect(ch);
1464 }
1465 
1466 /*
1467  * srpt_format_login_rej() - Format login reject IU
1468  */
1469 static void
1470 srpt_format_login_rej(srp_login_req_t *req, srp_login_rej_t *rej,
1471 	uint32_t reason)
1472 {
1473 	bzero(rej, sizeof (srp_login_rej_t));
1474 
1475 	rej->lrej_type   = SRP_IU_LOGIN_REJ;
1476 	rej->lrej_reason = h2b32(reason);
1477 	rej->lrej_tag    = req->lreq_tag;
1478 	rej->lrej_sup_buf_format =
1479 	    h2b16(SRP_DIRECT_BUFR_DESC | SRP_INDIRECT_BUFR_DESC);
1480 }
1481 
1482 /*
1483  * srpt_format_login_rsp() - Format login response IU
1484  */
1485 static void
1486 srpt_format_login_rsp(srp_login_req_t *req, srp_login_rsp_t *rsp,
1487 	uint8_t flags)
1488 {
1489 	bzero(rsp, sizeof (srp_login_rsp_t));
1490 
1491 	rsp->lrsp_type   = SRP_IU_LOGIN_RSP;
1492 	rsp->lrsp_req_limit_delta = h2b32((uint32_t)srpt_send_msg_depth);
1493 	rsp->lrsp_tag    = req->lreq_tag;
1494 
1495 	rsp->lrsp_max_it_iu_len = req->lreq_req_it_iu_len;
1496 	/* by def. > min T_IU_LEN */
1497 	rsp->lrsp_max_ti_iu_len = req->lreq_req_it_iu_len;
1498 
1499 	rsp->lrsp_sup_buf_format =
1500 	    h2b16(SRP_DIRECT_BUFR_DESC | SRP_INDIRECT_BUFR_DESC);
1501 	rsp->lrsp_rsp_flags = flags;
1502 }
1503 
1504 /*
1505  * srpt_stp_add_task()
1506  */
1507 void
1508 srpt_stp_add_task(srpt_session_t *session, srpt_iu_t *iu)
1509 {
1510 	rw_enter(&session->ss_rwlock, RW_WRITER);
1511 	list_insert_tail(&session->ss_task_list, iu);
1512 	rw_exit(&session->ss_rwlock);
1513 }
1514 
1515 /*
1516  * srpt_stp_remove_task()
1517  */
1518 void
1519 srpt_stp_remove_task(srpt_session_t *session, srpt_iu_t *iu)
1520 {
1521 	rw_enter(&session->ss_rwlock, RW_WRITER);
1522 
1523 	ASSERT(!list_is_empty(&session->ss_task_list));
1524 
1525 	list_remove(&session->ss_task_list, iu);
1526 	rw_exit(&session->ss_rwlock);
1527 }
1528