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