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