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  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <sys/conf.h>
27 #include <sys/file.h>
28 #include <sys/ddi.h>
29 #include <sys/sunddi.h>
30 #include <sys/modctl.h>
31 #include <sys/scsi/scsi.h>
32 #include <sys/scsi/impl/scsi_reset_notify.h>
33 #include <sys/disp.h>
34 #include <sys/byteorder.h>
35 #include <sys/varargs.h>
36 #include <sys/atomic.h>
37 #include <sys/sdt.h>
38 
39 #include <stmf.h>
40 #include <stmf_ioctl.h>
41 #include <portif.h>
42 #include <fct.h>
43 #include <fctio.h>
44 #include <fct_impl.h>
45 #include <discovery.h>
46 
47 static int fct_attach(dev_info_t *dip, ddi_attach_cmd_t cmd);
48 static int fct_detach(dev_info_t *dip, ddi_detach_cmd_t cmd);
49 static int fct_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg,
50     void **result);
51 static int fct_open(dev_t *devp, int flag, int otype, cred_t *credp);
52 static int fct_close(dev_t dev, int flag, int otype, cred_t *credp);
53 static int fct_ioctl(dev_t dev, int cmd, intptr_t data, int mode,
54     cred_t *credp, int *rval);
55 static int fct_fctiocmd(intptr_t data, int mode);
56 void fct_init_kstats(fct_i_local_port_t *iport);
57 
58 static dev_info_t *fct_dip;
59 static struct cb_ops fct_cb_ops = {
60 	fct_open,			/* open */
61 	fct_close,			/* close */
62 	nodev,				/* strategy */
63 	nodev,				/* print */
64 	nodev,				/* dump */
65 	nodev,				/* read */
66 	nodev,				/* write */
67 	fct_ioctl,			/* ioctl */
68 	nodev,				/* devmap */
69 	nodev,				/* mmap */
70 	nodev,				/* segmap */
71 	nochpoll,			/* chpoll */
72 	ddi_prop_op,			/* cb_prop_op */
73 	0,				/* streamtab */
74 	D_NEW | D_MP,			/* cb_flag */
75 	CB_REV,				/* rev */
76 	nodev,				/* aread */
77 	nodev				/* awrite */
78 };
79 
80 static struct dev_ops fct_ops = {
81 	DEVO_REV,
82 	0,
83 	fct_getinfo,
84 	nulldev,		/* identify */
85 	nulldev,		/* probe */
86 	fct_attach,
87 	fct_detach,
88 	nodev,			/* reset */
89 	&fct_cb_ops,
90 	NULL,			/* bus_ops */
91 	NULL			/* power */
92 };
93 
94 #define	FCT_NAME	"COMSTAR FCT"
95 #define	FCT_MODULE_NAME	"fct"
96 
97 extern struct mod_ops mod_driverops;
98 static struct modldrv modldrv = {
99 	&mod_driverops,
100 	FCT_NAME,
101 	&fct_ops
102 };
103 
104 static struct modlinkage modlinkage = {
105 	MODREV_1,
106 	&modldrv,
107 	NULL
108 };
109 
110 static uint32_t	rportid_table_size = FCT_HASH_TABLE_SIZE;
111 static int max_cached_ncmds = FCT_MAX_CACHED_CMDS;
112 static fct_i_local_port_t *fct_iport_list = NULL;
113 static kmutex_t fct_global_mutex;
114 uint32_t fct_rscn_options = RSCN_OPTION_VERIFY;
115 
116 int
117 _init(void)
118 {
119 	int ret;
120 
121 	ret = mod_install(&modlinkage);
122 	if (ret)
123 		return (ret);
124 	/* XXX */
125 	mutex_init(&fct_global_mutex, NULL, MUTEX_DRIVER, NULL);
126 	return (ret);
127 }
128 
129 int
130 _fini(void)
131 {
132 	int ret;
133 
134 	ret = mod_remove(&modlinkage);
135 	if (ret)
136 		return (ret);
137 	/* XXX */
138 	mutex_destroy(&fct_global_mutex);
139 	return (ret);
140 }
141 
142 int
143 _info(struct modinfo *modinfop)
144 {
145 	return (mod_info(&modlinkage, modinfop));
146 }
147 
148 /* ARGSUSED */
149 static int
150 fct_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg, void **result)
151 {
152 	switch (cmd) {
153 	case DDI_INFO_DEVT2DEVINFO:
154 		*result = fct_dip;
155 		break;
156 	case DDI_INFO_DEVT2INSTANCE:
157 		*result = (void *)(uintptr_t)ddi_get_instance(fct_dip);
158 		break;
159 	default:
160 		return (DDI_FAILURE);
161 	}
162 
163 	return (DDI_SUCCESS);
164 }
165 
166 static int
167 fct_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
168 {
169 	switch (cmd) {
170 	case DDI_ATTACH:
171 		fct_dip = dip;
172 
173 		if (ddi_create_minor_node(dip, "admin", S_IFCHR, 0,
174 		    DDI_NT_STMF_PP, 0) != DDI_SUCCESS) {
175 			break;
176 		}
177 		ddi_report_dev(dip);
178 		return (DDI_SUCCESS);
179 	}
180 
181 	return (DDI_FAILURE);
182 }
183 
184 static int
185 fct_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
186 {
187 	switch (cmd) {
188 	case DDI_DETACH:
189 		ddi_remove_minor_node(dip, 0);
190 		return (DDI_SUCCESS);
191 	}
192 
193 	return (DDI_FAILURE);
194 }
195 
196 /* ARGSUSED */
197 static int
198 fct_open(dev_t *devp, int flag, int otype, cred_t *credp)
199 {
200 	if (otype != OTYP_CHR)
201 		return (EINVAL);
202 	return (0);
203 }
204 
205 /* ARGSUSED */
206 static int
207 fct_close(dev_t dev, int flag, int otype, cred_t *credp)
208 {
209 	return (0);
210 }
211 
212 /* ARGSUSED */
213 static int
214 fct_ioctl(dev_t dev, int cmd, intptr_t data, int mode,
215     cred_t *credp, int *rval)
216 {
217 	int		ret = 0;
218 
219 	if ((cmd & 0xff000000) != FCT_IOCTL) {
220 		return (ENOTTY);
221 	}
222 
223 	if (drv_priv(credp) != 0) {
224 		return (EPERM);
225 	}
226 
227 	switch (cmd) {
228 	case FCTIO_CMD:
229 		ret = fct_fctiocmd(data, mode);
230 		break;
231 	default:
232 		ret = ENOTTY;
233 		break;
234 	}
235 
236 	return (ret);
237 }
238 
239 int
240 fct_copyin_iocdata(intptr_t data, int mode, fctio_t **fctio,
241     void **ibuf, void **abuf, void **obuf)
242 {
243 	int ret = 0;
244 
245 	*ibuf = NULL;
246 	*abuf = NULL;
247 	*obuf = NULL;
248 	*fctio = kmem_zalloc(sizeof (fctio_t), KM_SLEEP);
249 	if (ddi_copyin((void *)data, *fctio, sizeof (fctio_t), mode)) {
250 		ret = EFAULT;
251 		goto copyin_iocdata_done;
252 	}
253 
254 	if ((*fctio)->fctio_ilen) {
255 		*ibuf = kmem_zalloc((*fctio)->fctio_ilen, KM_SLEEP);
256 		if (ddi_copyin((void *)(unsigned long)(*fctio)->fctio_ibuf,
257 		    *ibuf, (*fctio)->fctio_ilen, mode)) {
258 			ret = EFAULT;
259 			goto copyin_iocdata_done;
260 		}
261 	}
262 	if ((*fctio)->fctio_alen) {
263 		*abuf = kmem_zalloc((*fctio)->fctio_alen, KM_SLEEP);
264 		if (ddi_copyin((void *)(unsigned long)(*fctio)->fctio_abuf,
265 		    *abuf, (*fctio)->fctio_alen, mode)) {
266 			ret = EFAULT;
267 			goto copyin_iocdata_done;
268 		}
269 	}
270 	if ((*fctio)->fctio_olen)
271 		*obuf = kmem_zalloc((*fctio)->fctio_olen, KM_SLEEP);
272 	if (ret == 0)
273 		return (0);
274 	ret = EFAULT;
275 copyin_iocdata_done:
276 	if (*obuf) {
277 		kmem_free(*obuf, (*fctio)->fctio_olen);
278 		*obuf = NULL;
279 	}
280 	if (*abuf) {
281 		kmem_free(*abuf, (*fctio)->fctio_alen);
282 		*abuf = NULL;
283 	}
284 	if (*ibuf) {
285 		kmem_free(*ibuf, (*fctio)->fctio_ilen);
286 		*ibuf = NULL;
287 	}
288 	kmem_free(*fctio, sizeof (fctio_t));
289 	return (ret);
290 }
291 
292 int
293 fct_copyout_iocdata(intptr_t data, int mode, fctio_t *fctio, void *obuf)
294 {
295 	int ret = 0;
296 
297 	if (fctio->fctio_olen) {
298 		ret = ddi_copyout(obuf,
299 		    (void *)(unsigned long)fctio->fctio_obuf, fctio->fctio_olen,
300 		    mode);
301 		if (ret) {
302 			return (EFAULT);
303 		}
304 	}
305 	ret = ddi_copyout(fctio, (void *)data, sizeof (fctio_t), mode);
306 	if (ret) {
307 		return (EFAULT);
308 	}
309 	return (0);
310 }
311 
312 int
313 fct_get_port_list(char *pathList, int count)
314 {
315 	fct_i_local_port_t *iport;
316 	int	i = 0, maxPorts = 0;
317 
318 	ASSERT(pathList != NULL);
319 
320 	mutex_enter(&fct_global_mutex);
321 	for (iport = fct_iport_list; iport; iport = iport->iport_next) {
322 		if (i < count)
323 			bcopy(iport->iport_port->port_pwwn,
324 			    pathList + 8 * i, 8);
325 		maxPorts ++;
326 		i++;
327 	}
328 	mutex_exit(&fct_global_mutex);
329 	return (maxPorts);
330 }
331 
332 /* invoked with fct_global_mutex locked */
333 fct_i_local_port_t *
334 fct_get_iport_per_wwn(uint8_t *pwwn)
335 {
336 	fct_i_local_port_t *iport;
337 
338 	ASSERT(mutex_owned(&fct_global_mutex));
339 	for (iport = fct_iport_list; iport; iport = iport->iport_next) {
340 		if (bcmp(iport->iport_port->port_pwwn, pwwn, 8) == 0)
341 			return (iport);
342 	}
343 	return (NULL);
344 }
345 
346 int
347 fct_get_adapter_attr(uint8_t *pwwn, fc_tgt_hba_adapter_attributes_t *hba_attr,
348     uint32_t *err_detail)
349 {
350 	fct_i_local_port_t *iport;
351 	fct_port_attrs_t *attr;
352 
353 	hba_attr->version = FCT_HBA_ADAPTER_ATTRIBUTES_VERSION;
354 	iport = fct_get_iport_per_wwn(pwwn);
355 	if (!iport) {
356 		*err_detail = FCTIO_BADWWN;
357 		return (ENXIO);
358 	}
359 
360 	attr = (fct_port_attrs_t *)kmem_zalloc(sizeof (fct_port_attrs_t),
361 	    KM_SLEEP);
362 	mutex_exit(&fct_global_mutex);
363 	iport->iport_port->port_populate_hba_details(iport->iport_port, attr);
364 	mutex_enter(&fct_global_mutex);
365 
366 	bcopy(attr->manufacturer, hba_attr->Manufacturer,
367 	    sizeof (hba_attr->Manufacturer));
368 	bcopy(attr->serial_number, hba_attr->SerialNumber,
369 	    sizeof (hba_attr->SerialNumber));
370 	bcopy(attr->model, hba_attr->Model, sizeof (hba_attr->Model));
371 	bcopy(attr->model_description, hba_attr->ModelDescription,
372 	    sizeof (hba_attr->ModelDescription));
373 	if (iport->iport_port->port_sym_node_name)
374 		bcopy(iport->iport_port->port_sym_node_name,
375 		    hba_attr->NodeSymbolicName,
376 		    strlen(iport->iport_port->port_sym_node_name));
377 	else
378 		bcopy(utsname.nodename, hba_attr->NodeSymbolicName,
379 		    strlen(utsname.nodename));
380 	bcopy(attr->hardware_version, hba_attr->HardwareVersion,
381 	    sizeof (hba_attr->HardwareVersion));
382 	bcopy(attr->option_rom_version, hba_attr->OptionROMVersion,
383 	    sizeof (hba_attr->OptionROMVersion));
384 	bcopy(attr->firmware_version, hba_attr->FirmwareVersion,
385 	    sizeof (hba_attr->FirmwareVersion));
386 	hba_attr->VendorSpecificID = attr->vendor_specific_id;
387 	bcopy(iport->iport_port->port_nwwn, hba_attr->NodeWWN,
388 	    sizeof (hba_attr->NodeWWN));
389 
390 	bcopy(attr->driver_name, hba_attr->DriverName,
391 	    sizeof (hba_attr->DriverName));
392 	bcopy(attr->driver_version, hba_attr->DriverVersion,
393 	    sizeof (hba_attr->DriverVersion));
394 
395 
396 	/* hba_attr->NumberOfPorts = fct_count_fru_ports(iport); */
397 	hba_attr->NumberOfPorts = 1;
398 
399 	kmem_free(attr, sizeof (fct_port_attrs_t));
400 	return (0);
401 }
402 
403 int
404 fct_get_adapter_port_attr(fct_i_local_port_t *ilport, uint8_t *pwwn,
405     fc_tgt_hba_port_attributes_t *port_attr, uint32_t *err_detail)
406 {
407 	fct_i_local_port_t *iport = ilport;
408 	fct_i_remote_port_t *irp = NULL;
409 	fct_port_attrs_t *attr;
410 	int i = 0;
411 
412 	port_attr->version = FCT_HBA_PORT_ATTRIBUTES_VERSION;
413 
414 	if (!ilport) {
415 		iport = fct_get_iport_per_wwn(pwwn);
416 		if (!iport) {
417 			*err_detail = FCTIO_BADWWN;
418 			return (ENXIO);
419 		}
420 	}
421 
422 	attr = (fct_port_attrs_t *)kmem_zalloc(sizeof (fct_port_attrs_t),
423 	    KM_SLEEP);
424 	mutex_exit(&fct_global_mutex);
425 	iport->iport_port->port_populate_hba_details(iport->iport_port, attr);
426 	mutex_enter(&fct_global_mutex);
427 
428 	port_attr->lastChange = iport->iport_last_change;
429 	bcopy(iport->iport_port->port_nwwn, port_attr->NodeWWN,
430 	    sizeof (port_attr->NodeWWN));
431 	bcopy(iport->iport_port->port_pwwn, port_attr->PortWWN,
432 	    sizeof (port_attr->PortWWN));
433 	bzero(port_attr->FabricName, sizeof (port_attr->FabricName));
434 	port_attr->PortFcId = iport->iport_link_info.portid;
435 	if ((iport->iport_link_state & S_LINK_ONLINE) ||
436 	    (iport->iport_link_state & S_RCVD_LINK_UP)) {
437 		port_attr->PortState = FC_HBA_PORTSTATE_ONLINE;
438 	} else {
439 		port_attr->PortState = FC_HBA_PORTSTATE_OFFLINE;
440 	}
441 	switch (iport->iport_link_info.port_topology) {
442 		case PORT_TOPOLOGY_PT_TO_PT:
443 			port_attr->PortType = FC_HBA_PORTTYPE_PTP;
444 			break;
445 		case PORT_TOPOLOGY_PRIVATE_LOOP:
446 			port_attr->PortType = FC_HBA_PORTTYPE_LPORT;
447 			break;
448 		case PORT_TOPOLOGY_PUBLIC_LOOP:
449 			port_attr->PortType = FC_HBA_PORTTYPE_NLPORT;
450 			break;
451 		case PORT_TOPOLOGY_FABRIC_PT_TO_PT:
452 			port_attr->PortType = FC_HBA_PORTTYPE_FPORT;
453 			break;
454 		default:
455 			port_attr->PortType = FC_HBA_PORTTYPE_UNKNOWN;
456 			break;
457 	}
458 	port_attr->PortSupportedClassofService = attr->supported_cos;
459 	port_attr->PortSupportedFc4Types[0] = 0;
460 	port_attr->PortActiveFc4Types[2] = 1;
461 	if (iport->iport_port->port_sym_port_name)
462 		bcopy(iport->iport_port->port_sym_port_name,
463 		    port_attr->PortSymbolicName,
464 		    strlen(iport->iport_port->port_sym_port_name));
465 	else if (iport->iport_port->port_default_alias)
466 		bcopy(iport->iport_port->port_default_alias,
467 		    port_attr->PortSymbolicName,
468 		    strlen(iport->iport_port->port_default_alias));
469 	else
470 		port_attr->PortSymbolicName[0] = 0;
471 	/* the definition is different so need to translate */
472 	if (attr->supported_speed & PORT_SPEED_1G)
473 		port_attr->PortSupportedSpeed |= FC_HBA_PORTSPEED_1GBIT;
474 	if (attr->supported_speed & PORT_SPEED_2G)
475 		port_attr->PortSupportedSpeed |= FC_HBA_PORTSPEED_2GBIT;
476 	if (attr->supported_speed & PORT_SPEED_4G)
477 		port_attr->PortSupportedSpeed |= FC_HBA_PORTSPEED_4GBIT;
478 	if (attr->supported_speed & PORT_SPEED_8G)
479 		port_attr->PortSupportedSpeed |= FC_HBA_PORTSPEED_8GBIT;
480 	if (attr->supported_speed & PORT_SPEED_10G)
481 		port_attr->PortSupportedSpeed |= FC_HBA_PORTSPEED_10GBIT;
482 	switch (iport->iport_link_info.port_speed) {
483 		case PORT_SPEED_1G:
484 			port_attr->PortSpeed = FC_HBA_PORTSPEED_1GBIT;
485 			break;
486 		case PORT_SPEED_2G:
487 			port_attr->PortSpeed = FC_HBA_PORTSPEED_2GBIT;
488 			break;
489 		case PORT_SPEED_4G:
490 			port_attr->PortSpeed = FC_HBA_PORTSPEED_4GBIT;
491 			break;
492 		case PORT_SPEED_8G:
493 			port_attr->PortSpeed = FC_HBA_PORTSPEED_8GBIT;
494 			break;
495 		case PORT_SPEED_10G:
496 			port_attr->PortSpeed = FC_HBA_PORTSPEED_10GBIT;
497 			break;
498 		default:
499 			port_attr->PortSpeed = FC_HBA_PORTSPEED_UNKNOWN;
500 			break;
501 	}
502 	port_attr->PortMaxFrameSize = attr->max_frame_size;
503 	rw_enter(&iport->iport_lock, RW_READER);
504 	port_attr->NumberofDiscoveredPorts = iport->iport_nrps_login;
505 	for (; i < iport->iport_port->port_max_logins; i++) {
506 		irp = iport->iport_rp_slots[i];
507 		if (irp && irp->irp_flags & IRP_PLOGI_DONE) {
508 			if (FC_WELL_KNOWN_ADDR(irp->irp_portid))
509 				port_attr->NumberofDiscoveredPorts --;
510 		}
511 	}
512 	rw_exit(&iport->iport_lock);
513 
514 	kmem_free(attr, sizeof (fct_port_attrs_t));
515 
516 	return (0);
517 }
518 
519 int
520 fct_get_discovered_port_attr(fct_i_remote_port_t *remote_port,
521     uint8_t *port_wwn, uint32_t index, fc_tgt_hba_port_attributes_t *port_attr,
522     uint32_t *error_detail)
523 {
524 	fct_i_local_port_t *iport;
525 	fct_i_remote_port_t *irp = remote_port;
526 	int	count = 0, i = 0;
527 
528 	port_attr->version = FCT_HBA_PORT_ATTRIBUTES_VERSION;
529 	if (!remote_port) {
530 		iport = fct_get_iport_per_wwn(port_wwn);
531 		if (!iport) {
532 			*error_detail = FCTIO_BADWWN;
533 			return (ENXIO);
534 		}
535 
536 		rw_enter(&iport->iport_lock, RW_READER);
537 
538 		if (index >= iport->iport_nrps_login) {
539 			rw_exit(&iport->iport_lock);
540 			*error_detail = FCTIO_OUTOFBOUNDS;
541 			return (EINVAL);
542 		}
543 		for (; i < iport->iport_port->port_max_logins; i++) {
544 			irp = iport->iport_rp_slots[i];
545 			if (irp && irp->irp_flags & IRP_PLOGI_DONE &&
546 			    !FC_WELL_KNOWN_ADDR(irp->irp_portid)) {
547 				count ++;
548 				if ((index + 1) <= count)
549 					break;
550 			}
551 		}
552 		if (i >= iport->iport_port->port_max_logins) {
553 			rw_exit(&iport->iport_lock);
554 			*error_detail = FCTIO_OUTOFBOUNDS;
555 			return (EINVAL);
556 		}
557 		ASSERT(irp);
558 	} else {
559 		iport = (fct_i_local_port_t *)
560 		    irp->irp_rp->rp_port->port_fct_private;
561 	}
562 	port_attr->lastChange = iport->iport_last_change;
563 	rw_enter(&irp->irp_lock, RW_READER);
564 	bcopy(irp->irp_rp->rp_pwwn, port_attr->PortWWN,
565 	    sizeof (port_attr->PortWWN));
566 	bcopy(irp->irp_rp->rp_nwwn, port_attr->NodeWWN,
567 	    sizeof (port_attr->NodeWWN));
568 	port_attr->PortFcId = irp->irp_portid;
569 	if (irp->irp_spn)
570 		(void) strncpy(port_attr->PortSymbolicName, irp->irp_spn,
571 		    strlen(irp->irp_spn));
572 	else
573 		port_attr->PortSymbolicName[0] = '\0';
574 	port_attr->PortSupportedClassofService = irp->irp_cos;
575 	bcopy((caddr_t)irp->irp_fc4types, port_attr->PortActiveFc4Types,
576 	    sizeof (irp->irp_fc4types));
577 	bcopy((caddr_t)irp->irp_fc4types, port_attr->PortSupportedFc4Types,
578 	    sizeof (irp->irp_fc4types));
579 	if (irp->irp_flags & IRP_PLOGI_DONE)
580 		port_attr->PortState = FC_HBA_PORTSTATE_ONLINE;
581 	else
582 		port_attr->PortState = FC_HBA_PORTSTATE_UNKNOWN;
583 
584 	port_attr->PortType = FC_HBA_PORTTYPE_UNKNOWN;
585 	port_attr->PortSupportedSpeed = FC_HBA_PORTSPEED_UNKNOWN;
586 	port_attr->PortSpeed = FC_HBA_PORTSPEED_UNKNOWN;
587 	port_attr->PortMaxFrameSize = 0;
588 	port_attr->NumberofDiscoveredPorts = 0;
589 	rw_exit(&irp->irp_lock);
590 	if (!remote_port) {
591 		rw_exit(&iport->iport_lock);
592 	}
593 	return (0);
594 }
595 
596 int
597 fct_get_port_attr(uint8_t *port_wwn,
598     fc_tgt_hba_port_attributes_t *port_attr, uint32_t *error_detail)
599 {
600 	fct_i_local_port_t *iport;
601 	fct_i_remote_port_t *irp;
602 	int i, ret;
603 
604 	iport = fct_get_iport_per_wwn(port_wwn);
605 	if (iport) {
606 		return (fct_get_adapter_port_attr(iport, port_wwn,
607 		    port_attr, error_detail));
608 	}
609 	/* else */
610 	for (iport = fct_iport_list; iport; iport = iport->iport_next) {
611 		rw_enter(&iport->iport_lock, RW_READER);
612 		for (i = 0; i < rportid_table_size; i++) {
613 			irp = iport->iport_rp_tb[i];
614 			while (irp) {
615 				if (bcmp(irp->irp_rp->rp_pwwn,
616 				    port_wwn, 8) == 0 &&
617 				    irp->irp_flags & IRP_PLOGI_DONE) {
618 					ret = fct_get_discovered_port_attr(
619 					    irp, NULL, 0, port_attr,
620 					    error_detail);
621 					rw_exit(&iport->iport_lock);
622 					return (ret);
623 				}
624 				irp = irp->irp_next;
625 			}
626 		}
627 		rw_exit(&iport->iport_lock);
628 	}
629 	*error_detail = FCTIO_BADWWN;
630 	return (ENXIO);
631 }
632 
633 /* ARGSUSED */
634 int
635 fct_get_port_stats(uint8_t *port_wwn,
636     fc_tgt_hba_adapter_port_stats_t *port_stats, uint32_t *error_detail)
637 {
638 	int ret;
639 	fct_i_local_port_t *iport = fct_get_iport_per_wwn(port_wwn);
640 	fct_port_link_status_t	stat;
641 	uint32_t buf_size = sizeof (fc_tgt_hba_adapter_port_stats_t);
642 
643 	if (!iport)
644 		return (ENXIO);
645 	port_stats->version = FCT_HBA_ADAPTER_PORT_STATS_VERSION;
646 
647 	if (iport->iport_port->port_info == NULL) {
648 		*error_detail = FCTIO_FAILURE;
649 		return (EIO);
650 	}
651 	ret = iport->iport_port->port_info(FC_TGT_PORT_RLS,
652 	    iport->iport_port, NULL, (uint8_t *)&stat, &buf_size);
653 	if (ret != STMF_SUCCESS) {
654 		*error_detail = FCTIO_FAILURE;
655 		return (EIO);
656 	}
657 
658 	port_stats->SecondsSinceLastReset = 0;
659 	port_stats->TxFrames = 0;
660 	port_stats->TxWords = 0;
661 	port_stats->RxFrames = 0;
662 	port_stats->RxWords = 0;
663 	port_stats->LIPCount = 0;
664 	port_stats->NOSCount = 0;
665 	port_stats->ErrorFrames = 0;
666 	port_stats->DumpedFrames = 0;
667 	port_stats->LinkFailureCount = stat.LinkFailureCount;
668 	port_stats->LossOfSyncCount = stat.LossOfSyncCount;
669 	port_stats->LossOfSignalCount = stat.LossOfSignalsCount;
670 	port_stats->PrimitiveSeqProtocolErrCount =
671 	    stat.PrimitiveSeqProtocolErrorCount;
672 	port_stats->InvalidTxWordCount =
673 	    stat.InvalidTransmissionWordCount;
674 	port_stats->InvalidCRCCount = stat.InvalidCRCCount;
675 
676 	return (ret);
677 }
678 
679 int
680 fct_get_link_status(uint8_t *port_wwn, uint64_t *dest_id,
681     fct_port_link_status_t *link_status, uint32_t *error_detail)
682 {
683 	fct_i_local_port_t *iport = fct_get_iport_per_wwn(port_wwn);
684 	fct_i_remote_port_t *irp = NULL;
685 	uint32_t buf_size = sizeof (fct_port_link_status_t);
686 	stmf_status_t ret = 0;
687 	int i;
688 	fct_cmd_t *cmd = NULL;
689 
690 	if (!iport) {
691 		*error_detail = FCTIO_BADWWN;
692 		return (ENXIO);
693 	}
694 
695 	/*
696 	 * If what we are requesting is zero or same as local port,
697 	 * then we use port_info()
698 	 */
699 	if (dest_id == NULL || *dest_id == iport->iport_link_info.portid) {
700 		if (iport->iport_port->port_info == NULL) {
701 			*error_detail = FCTIO_FAILURE;
702 			return (EIO);
703 		}
704 		ret = iport->iport_port->port_info(FC_TGT_PORT_RLS,
705 		    iport->iport_port, NULL,
706 		    (uint8_t *)link_status, &buf_size);
707 		if (ret == STMF_SUCCESS) {
708 			return (0);
709 		} else {
710 			*error_detail = FCTIO_FAILURE;
711 			return (EIO);
712 		}
713 	}
714 
715 	/*
716 	 * For remote port, we will send RLS
717 	 */
718 	for (i = 0; i < rportid_table_size; i++) {
719 		irp = iport->iport_rp_tb[i];
720 		while (irp) {
721 			if (irp->irp_rp->rp_id == *dest_id &&
722 			    irp->irp_flags & IRP_PLOGI_DONE) {
723 				goto SEND_RLS_ELS;
724 			}
725 			irp = irp->irp_next;
726 		}
727 	}
728 	return (ENXIO);
729 
730 SEND_RLS_ELS:
731 	cmd = fct_create_solels(iport->iport_port,
732 	    irp->irp_rp, 0, ELS_OP_RLS,
733 	    0, fct_rls_cb);
734 	if (!cmd)
735 		return (ENOMEM);
736 	iport->iport_rls_cb_data.fct_link_status = link_status;
737 	CMD_TO_ICMD(cmd)->icmd_cb_private = &iport->iport_rls_cb_data;
738 	fct_post_to_solcmd_queue(iport->iport_port, cmd);
739 	sema_p(&iport->iport_rls_sema);
740 	if (iport->iport_rls_cb_data.fct_els_res != FCT_SUCCESS)
741 		ret = EIO;
742 	return (ret);
743 }
744 
745 static int
746 fct_forcelip(uint8_t *port_wwn, uint32_t *fctio_errno)
747 {
748 	fct_status_t		 rval;
749 	fct_i_local_port_t	*iport;
750 
751 	mutex_enter(&fct_global_mutex);
752 	iport = fct_get_iport_per_wwn(port_wwn);
753 	mutex_exit(&fct_global_mutex);
754 	if (iport == NULL) {
755 		return (-1);
756 	}
757 
758 	iport->iport_port->port_ctl(iport->iport_port,
759 	    FCT_CMD_FORCE_LIP, &rval);
760 	if (rval != FCT_SUCCESS) {
761 		*fctio_errno = FCTIO_FAILURE;
762 	} else {
763 		*fctio_errno = 0;
764 	}
765 
766 	return (0);
767 }
768 
769 static int
770 fct_fctiocmd(intptr_t data, int mode)
771 {
772 	int ret	 = 0;
773 	void		*ibuf = NULL;
774 	void		*obuf = NULL;
775 	void		*abuf = NULL;
776 	fctio_t		*fctio;
777 	uint32_t	attr_length;
778 
779 	ret = fct_copyin_iocdata(data, mode, &fctio, &ibuf, &abuf, &obuf);
780 	if (ret) {
781 		return (ret);
782 	}
783 
784 	switch (fctio->fctio_cmd) {
785 	case FCTIO_ADAPTER_LIST: {
786 		fc_tgt_hba_list_t *list = (fc_tgt_hba_list_t *)obuf;
787 		int		count;
788 
789 		if (fctio->fctio_olen < sizeof (fc_tgt_hba_list_t)) {
790 			ret = EINVAL;
791 			break;
792 		}
793 		list->numPorts = (fctio->fctio_olen -
794 		    sizeof (fc_tgt_hba_list_t))/8 + 1;
795 
796 		list->version = FCT_HBA_LIST_VERSION;
797 		count = fct_get_port_list((char *)list->port_wwn,
798 		    list->numPorts);
799 		if (count < 0) {
800 			ret = ENXIO;
801 			break;
802 		}
803 		if (count > list->numPorts) {
804 			fctio->fctio_errno = FCTIO_MOREDATA;
805 			ret = ENOSPC;
806 		}
807 		list->numPorts = count;
808 		break;
809 		}
810 	case FCTIO_GET_ADAPTER_ATTRIBUTES: {
811 		fc_tgt_hba_adapter_attributes_t *hba_attr;
812 		uint8_t	*port_wwn = (uint8_t *)ibuf;
813 
814 		attr_length = sizeof (fc_tgt_hba_adapter_attributes_t);
815 		if (fctio->fctio_olen < attr_length ||
816 		    fctio->fctio_xfer != FCTIO_XFER_READ) {
817 			ret = EINVAL;
818 			break;
819 		}
820 		hba_attr = (fc_tgt_hba_adapter_attributes_t *)obuf;
821 
822 		mutex_enter(&fct_global_mutex);
823 		ret = fct_get_adapter_attr(port_wwn, hba_attr,
824 		    &fctio->fctio_errno);
825 		mutex_exit(&fct_global_mutex);
826 
827 		break;
828 		}
829 	case FCTIO_GET_ADAPTER_PORT_ATTRIBUTES: {
830 		fc_tgt_hba_port_attributes_t *port_attr;
831 
832 		uint8_t *port_wwn = (uint8_t *)ibuf;
833 
834 		attr_length = sizeof (fc_tgt_hba_port_attributes_t);
835 		if (fctio->fctio_olen < attr_length ||
836 		    fctio->fctio_xfer != FCTIO_XFER_READ) {
837 			ret = EINVAL;
838 			break;
839 		}
840 		port_attr = (fc_tgt_hba_port_attributes_t *)obuf;
841 
842 		mutex_enter(&fct_global_mutex);
843 		ret = fct_get_adapter_port_attr(NULL, port_wwn, port_attr,
844 		    &fctio->fctio_errno);
845 		mutex_exit(&fct_global_mutex);
846 
847 		break;
848 		}
849 	case FCTIO_GET_DISCOVERED_PORT_ATTRIBUTES: {
850 		uint8_t *port_wwn = (uint8_t *)ibuf;
851 		uint32_t *port_index = (uint32_t *)abuf;
852 		fc_tgt_hba_port_attributes_t *port_attr;
853 
854 		attr_length = sizeof (fc_tgt_hba_port_attributes_t);
855 		if (fctio->fctio_olen < attr_length ||
856 		    fctio->fctio_xfer != FCTIO_XFER_READ) {
857 			ret = EINVAL;
858 			break;
859 		}
860 		port_attr = (fc_tgt_hba_port_attributes_t *)obuf;
861 
862 		mutex_enter(&fct_global_mutex);
863 		ret = fct_get_discovered_port_attr(NULL, port_wwn,
864 		    *port_index, port_attr, &fctio->fctio_errno);
865 		mutex_exit(&fct_global_mutex);
866 
867 		break;
868 		}
869 	case FCTIO_GET_PORT_ATTRIBUTES: {
870 		uint8_t *port_wwn = (uint8_t *)ibuf;
871 		fc_tgt_hba_port_attributes_t *port_attr;
872 
873 		attr_length = sizeof (fc_tgt_hba_port_attributes_t);
874 		if (fctio->fctio_olen < attr_length ||
875 		    fctio->fctio_xfer != FCTIO_XFER_READ) {
876 			ret = EINVAL;
877 			break;
878 		}
879 
880 		port_attr = (fc_tgt_hba_port_attributes_t *)obuf;
881 
882 		mutex_enter(&fct_global_mutex);
883 		ret = fct_get_port_attr(port_wwn, port_attr,
884 		    &fctio->fctio_errno);
885 		mutex_exit(&fct_global_mutex);
886 
887 		break;
888 		}
889 	case FCTIO_GET_ADAPTER_PORT_STATS: {
890 		uint8_t *port_wwn = (uint8_t *)ibuf;
891 		fc_tgt_hba_adapter_port_stats_t *port_stats =
892 		    (fc_tgt_hba_adapter_port_stats_t *)obuf;
893 		mutex_enter(&fct_global_mutex);
894 		ret = fct_get_port_stats(port_wwn, port_stats,
895 		    &fctio->fctio_errno);
896 		mutex_exit(&fct_global_mutex);
897 		break;
898 		}
899 	case FCTIO_GET_LINK_STATUS: {
900 		uint8_t *port_wwn = (uint8_t *)ibuf;
901 		fct_port_link_status_t *link_status =
902 		    (fct_port_link_status_t *)obuf;
903 		uint64_t *dest_id = abuf;
904 
905 		mutex_enter(&fct_global_mutex);
906 		ret = fct_get_link_status(port_wwn, dest_id, link_status,
907 		    &fctio->fctio_errno);
908 		mutex_exit(&fct_global_mutex);
909 		break;
910 		}
911 
912 	case FCTIO_FORCE_LIP:
913 		ret = fct_forcelip((uint8_t *)ibuf, &fctio->fctio_errno);
914 		break;
915 
916 	default:
917 		break;
918 	}
919 	if (ret == 0) {
920 		ret = fct_copyout_iocdata(data, mode, fctio, obuf);
921 	} else if (fctio->fctio_errno) {
922 		(void) fct_copyout_iocdata(data, mode, fctio, obuf);
923 	}
924 
925 	if (obuf) {
926 		kmem_free(obuf, fctio->fctio_olen);
927 		obuf = NULL;
928 	}
929 	if (abuf) {
930 		kmem_free(abuf, fctio->fctio_alen);
931 		abuf = NULL;
932 	}
933 
934 	if (ibuf) {
935 		kmem_free(ibuf, fctio->fctio_ilen);
936 		ibuf = NULL;
937 	}
938 	kmem_free(fctio, sizeof (fctio_t));
939 	return (ret);
940 }
941 
942 typedef struct {
943 	void	*bp;	/* back pointer from internal struct to main struct */
944 	int	alloc_size;
945 	fct_struct_id_t struct_id;
946 } __ifct_t;
947 
948 typedef struct {
949 	__ifct_t	*fp;	/* Framework private */
950 	void		*cp;	/* Caller private */
951 	void		*ss;	/* struct specific */
952 } __fct_t;
953 
954 static struct {
955 	int shared;
956 	int fw_private;
957 	int struct_specific;
958 } fct_sizes[] = { { 0, 0, 0 },
959 	{ GET_STRUCT_SIZE(fct_local_port_t),
960 		GET_STRUCT_SIZE(fct_i_local_port_t), 0 },
961 	{ GET_STRUCT_SIZE(fct_remote_port_t),
962 		GET_STRUCT_SIZE(fct_i_remote_port_t), 0 },
963 	{ GET_STRUCT_SIZE(fct_cmd_t),
964 		GET_STRUCT_SIZE(fct_i_cmd_t), GET_STRUCT_SIZE(fct_els_t) },
965 	{ GET_STRUCT_SIZE(fct_cmd_t),
966 		GET_STRUCT_SIZE(fct_i_cmd_t), GET_STRUCT_SIZE(fct_els_t) },
967 	{ GET_STRUCT_SIZE(fct_cmd_t),
968 		GET_STRUCT_SIZE(fct_i_cmd_t), GET_STRUCT_SIZE(fct_sol_ct_t) },
969 	{ GET_STRUCT_SIZE(fct_cmd_t), GET_STRUCT_SIZE(fct_i_cmd_t),
970 		GET_STRUCT_SIZE(fct_rcvd_abts_t) },
971 	{ GET_STRUCT_SIZE(fct_cmd_t),	/* FCT_STRUCT_CMD_FCP_XCHG */
972 		GET_STRUCT_SIZE(fct_i_cmd_t), 0 },
973 	{ GET_STRUCT_SIZE(fct_dbuf_store_t),
974 		GET_STRUCT_SIZE(__ifct_t), 0 }
975 };
976 
977 void *
978 fct_alloc(fct_struct_id_t struct_id, int additional_size, int flags)
979 {
980 	int fct_size;
981 	int kmem_flag;
982 	__fct_t *sh;
983 
984 	if ((struct_id == 0) || (struct_id >= FCT_MAX_STRUCT_IDS))
985 		return (NULL);
986 
987 	if ((curthread->t_flag & T_INTR_THREAD) || (flags & AF_FORCE_NOSLEEP)) {
988 		kmem_flag = KM_NOSLEEP;
989 	} else {
990 		kmem_flag = KM_SLEEP;
991 	}
992 
993 	additional_size = (additional_size + 7) & (~7);
994 	fct_size = fct_sizes[struct_id].shared +
995 	    fct_sizes[struct_id].fw_private +
996 	    fct_sizes[struct_id].struct_specific + additional_size;
997 
998 	if (struct_id == FCT_STRUCT_LOCAL_PORT) {
999 		stmf_local_port_t *lport;
1000 
1001 		lport = (stmf_local_port_t *)stmf_alloc(
1002 		    STMF_STRUCT_STMF_LOCAL_PORT, fct_size, flags);
1003 		if (lport) {
1004 			sh = (__fct_t *)lport->lport_port_private;
1005 			sh->ss = lport;
1006 		} else {
1007 			return (NULL);
1008 		}
1009 	} else if (struct_id == FCT_STRUCT_DBUF_STORE) {
1010 		stmf_dbuf_store_t *ds;
1011 
1012 		ds = (stmf_dbuf_store_t *)stmf_alloc(STMF_STRUCT_DBUF_STORE,
1013 		    fct_size, flags);
1014 		if (ds) {
1015 			sh = (__fct_t *)ds->ds_port_private;
1016 			sh->ss = ds;
1017 		} else {
1018 			return (NULL);
1019 		}
1020 	} else {
1021 		sh = (__fct_t *)kmem_zalloc(fct_size, kmem_flag);
1022 	}
1023 
1024 	if (sh == NULL)
1025 		return (NULL);
1026 
1027 	sh->fp = (__ifct_t *)GET_BYTE_OFFSET(sh, fct_sizes[struct_id].shared);
1028 	sh->cp = GET_BYTE_OFFSET(sh->fp, fct_sizes[struct_id].fw_private);
1029 	if (fct_sizes[struct_id].struct_specific)
1030 		sh->ss = GET_BYTE_OFFSET(sh->cp, additional_size);
1031 
1032 	sh->fp->bp = sh;
1033 	sh->fp->alloc_size = fct_size;
1034 	sh->fp->struct_id = struct_id;
1035 
1036 	if (struct_id == FCT_STRUCT_CMD_FCP_XCHG) {
1037 		((fct_cmd_t *)sh)->cmd_type = FCT_CMD_FCP_XCHG;
1038 	} else if (struct_id == FCT_STRUCT_CMD_RCVD_ELS) {
1039 		((fct_cmd_t *)sh)->cmd_type = FCT_CMD_RCVD_ELS;
1040 	} else if (struct_id == FCT_STRUCT_CMD_SOL_ELS) {
1041 		((fct_cmd_t *)sh)->cmd_type = FCT_CMD_SOL_ELS;
1042 	} else if (struct_id == FCT_STRUCT_CMD_RCVD_ABTS) {
1043 		((fct_cmd_t *)sh)->cmd_type = FCT_CMD_RCVD_ABTS;
1044 	} else if (struct_id == FCT_STRUCT_CMD_SOL_CT) {
1045 		((fct_cmd_t *)sh)->cmd_type = FCT_CMD_SOL_CT;
1046 	}
1047 
1048 	return (sh);
1049 }
1050 
1051 void
1052 fct_free(void *ptr)
1053 {
1054 	__fct_t *sh = (__fct_t *)ptr;
1055 	fct_struct_id_t struct_id = sh->fp->struct_id;
1056 
1057 	if (struct_id == FCT_STRUCT_CMD_SOL_CT) {
1058 		fct_sol_ct_t *ct = (fct_sol_ct_t *)
1059 		    ((fct_cmd_t *)ptr)->cmd_specific;
1060 
1061 		if (ct->ct_req_alloc_size) {
1062 			kmem_free(ct->ct_req_payload, ct->ct_req_alloc_size);
1063 		}
1064 		if (ct->ct_resp_alloc_size) {
1065 			kmem_free(ct->ct_resp_payload, ct->ct_resp_alloc_size);
1066 		}
1067 	} else if ((struct_id == FCT_STRUCT_CMD_RCVD_ELS) ||
1068 	    (struct_id == FCT_STRUCT_CMD_SOL_ELS)) {
1069 		fct_els_t *els = (fct_els_t *)
1070 			((fct_cmd_t *)ptr)->cmd_specific;
1071 		if (els->els_req_alloc_size)
1072 			kmem_free(els->els_req_payload,
1073 				els->els_req_alloc_size);
1074 		if (els->els_resp_alloc_size)
1075 			kmem_free(els->els_resp_payload,
1076 				els->els_resp_alloc_size);
1077 	}
1078 
1079 	if (struct_id == FCT_STRUCT_LOCAL_PORT) {
1080 		stmf_free(((fct_local_port_t *)ptr)->port_lport);
1081 	} else if (struct_id == FCT_STRUCT_DBUF_STORE) {
1082 		stmf_free(((fct_dbuf_store_t *)ptr)->fds_ds);
1083 	} else {
1084 		kmem_free(ptr, sh->fp->alloc_size);
1085 	}
1086 }
1087 
1088 stmf_data_buf_t *
1089 fct_alloc_dbuf(scsi_task_t *task, uint32_t size, uint32_t *pminsize,
1090     uint32_t flags)
1091 {
1092 	fct_local_port_t *port = (fct_local_port_t *)
1093 	    task->task_lport->lport_port_private;
1094 
1095 	return (port->port_fds->fds_alloc_data_buf(port, size,
1096 	    pminsize, flags));
1097 }
1098 
1099 void
1100 fct_free_dbuf(stmf_dbuf_store_t *ds, stmf_data_buf_t *dbuf)
1101 {
1102 	fct_dbuf_store_t *fds;
1103 
1104 	fds = (fct_dbuf_store_t *)ds->ds_port_private;
1105 
1106 	fds->fds_free_data_buf(fds, dbuf);
1107 }
1108 
1109 static uint32_t taskq_cntr = 0;
1110 
1111 fct_status_t
1112 fct_register_local_port(fct_local_port_t *port)
1113 {
1114 	fct_i_local_port_t	*iport;
1115 	stmf_local_port_t	*lport;
1116 	fct_cmd_slot_t		*slot;
1117 	int			i;
1118 	char			taskq_name[24];
1119 
1120 	iport = (fct_i_local_port_t *)port->port_fct_private;
1121 	if (port->port_default_alias) {
1122 		int l = strlen(port->port_default_alias);
1123 
1124 		if (l < 16) {
1125 			iport->iport_alias = iport->iport_alias_mem;
1126 		} else {
1127 			iport->iport_alias =
1128 			    (char *)kmem_zalloc(l+1, KM_SLEEP);
1129 		}
1130 		(void) strcpy(iport->iport_alias, port->port_default_alias);
1131 	} else {
1132 		iport->iport_alias = NULL;
1133 	}
1134 	stmf_wwn_to_devid_desc((scsi_devid_desc_t *)iport->iport_id,
1135 	    port->port_pwwn, PROTOCOL_FIBRE_CHANNEL);
1136 	(void) snprintf(taskq_name, 24, "stmf_fct_taskq_%d",
1137 	    atomic_add_32_nv(&taskq_cntr, 1));
1138 	taskq_name[23] = 0;
1139 	if ((iport->iport_worker_taskq = ddi_taskq_create(NULL,
1140 	    taskq_name, 1, TASKQ_DEFAULTPRI, 0)) == NULL) {
1141 		return (FCT_FAILURE);
1142 	}
1143 	mutex_init(&iport->iport_worker_lock, NULL, MUTEX_DRIVER, NULL);
1144 	cv_init(&iport->iport_worker_cv, NULL, CV_DRIVER, NULL);
1145 	rw_init(&iport->iport_lock, NULL, RW_DRIVER, NULL);
1146 	sema_init(&iport->iport_rls_sema, 0, NULL, SEMA_DRIVER, NULL);
1147 
1148 	/* Remote port mgmt */
1149 	iport->iport_rp_slots = (fct_i_remote_port_t **)kmem_zalloc(
1150 	    port->port_max_logins * sizeof (fct_i_remote_port_t *), KM_SLEEP);
1151 	iport->iport_rp_tb = kmem_zalloc(rportid_table_size *
1152 	    sizeof (fct_i_remote_port_t *), KM_SLEEP);
1153 
1154 	/* fct_cmds for SCSI traffic */
1155 	iport->iport_total_alloced_ncmds = 0;
1156 	iport->iport_cached_ncmds = 0;
1157 	port->port_fca_fcp_cmd_size =
1158 	    (port->port_fca_fcp_cmd_size + 7) & ~7;
1159 	iport->iport_cached_cmdlist = NULL;
1160 	mutex_init(&iport->iport_cached_cmd_lock, NULL, MUTEX_DRIVER, NULL);
1161 
1162 	/* Initialize cmd slots */
1163 	iport->iport_cmd_slots = (fct_cmd_slot_t *)kmem_zalloc(
1164 	    port->port_max_xchges * sizeof (fct_cmd_slot_t), KM_SLEEP);
1165 	iport->iport_next_free_slot = 0;
1166 	for (i = 0; i < port->port_max_xchges; ) {
1167 		slot = &iport->iport_cmd_slots[i];
1168 		slot->slot_no = (uint16_t)i;
1169 		slot->slot_next = (uint16_t)(++i);
1170 	}
1171 	slot->slot_next = FCT_SLOT_EOL;
1172 	iport->iport_nslots_free = port->port_max_xchges;
1173 
1174 	iport->iport_task_green_limit =
1175 	    (port->port_max_xchges * FCT_TASK_GREEN_LIMIT) / 100;
1176 	iport->iport_task_yellow_limit =
1177 	    (port->port_max_xchges * FCT_TASK_YELLOW_LIMIT) / 100;
1178 	iport->iport_task_red_limit =
1179 	    (port->port_max_xchges * FCT_TASK_RED_LIMIT) / 100;
1180 
1181 	/* Start worker thread */
1182 	atomic_and_32(&iport->iport_flags, ~IPORT_TERMINATE_WORKER);
1183 	(void) ddi_taskq_dispatch(iport->iport_worker_taskq,
1184 	    fct_port_worker, port, DDI_SLEEP);
1185 	/* Wait for taskq to start */
1186 	while ((iport->iport_flags & IPORT_WORKER_RUNNING) == 0) {
1187 		delay(1);
1188 	}
1189 
1190 	lport = port->port_lport;
1191 	lport->lport_id = (scsi_devid_desc_t *)iport->iport_id;
1192 	lport->lport_alias = iport->iport_alias;
1193 	lport->lport_pp = port->port_pp;
1194 	port->port_fds->fds_ds->ds_alloc_data_buf = fct_alloc_dbuf;
1195 	port->port_fds->fds_ds->ds_free_data_buf = fct_free_dbuf;
1196 	lport->lport_ds = port->port_fds->fds_ds;
1197 	lport->lport_xfer_data = fct_xfer_scsi_data;
1198 	lport->lport_send_status = fct_send_scsi_status;
1199 	lport->lport_task_free = fct_scsi_task_free;
1200 	lport->lport_abort = fct_scsi_abort;
1201 	lport->lport_ctl = fct_ctl;
1202 	lport->lport_info = fct_info;
1203 	lport->lport_event_handler = fct_event_handler;
1204 	if (stmf_register_local_port(port->port_lport) != FCT_SUCCESS) {
1205 		goto fct_regport_fail1;
1206 	}
1207 	(void) stmf_lport_add_event(lport, LPORT_EVENT_INITIAL_LUN_MAPPED);
1208 
1209 	mutex_enter(&fct_global_mutex);
1210 	iport->iport_next = fct_iport_list;
1211 	iport->iport_prev = NULL;
1212 	if (iport->iport_next)
1213 		iport->iport_next->iport_prev = iport;
1214 	fct_iport_list = iport;
1215 	mutex_exit(&fct_global_mutex);
1216 
1217 	fct_init_kstats(iport);
1218 
1219 	fct_log_local_port_event(port, ESC_SUNFC_PORT_ATTACH);
1220 
1221 	return (FCT_SUCCESS);
1222 
1223 fct_regport_fail1:;
1224 	/* Stop the taskq 1st */
1225 	if (iport->iport_flags & IPORT_WORKER_RUNNING) {
1226 		atomic_or_32(&iport->iport_flags, IPORT_TERMINATE_WORKER);
1227 		cv_broadcast(&iport->iport_worker_cv);
1228 		while (iport->iport_flags & IPORT_WORKER_RUNNING) {
1229 			delay(1);
1230 		}
1231 	}
1232 	ddi_taskq_destroy(iport->iport_worker_taskq);
1233 	if (iport->iport_rp_tb) {
1234 		kmem_free(iport->iport_rp_tb, rportid_table_size *
1235 		    sizeof (fct_i_remote_port_t *));
1236 	}
1237 	return (FCT_FAILURE);
1238 }
1239 
1240 fct_status_t
1241 fct_deregister_local_port(fct_local_port_t *port)
1242 {
1243 	fct_i_local_port_t	*iport;
1244 	fct_i_cmd_t		*icmd, *next_icmd;
1245 	int			ndx;
1246 
1247 	iport = (fct_i_local_port_t *)port->port_fct_private;
1248 
1249 	if ((iport->iport_state != FCT_STATE_OFFLINE) ||
1250 	    iport->iport_state_not_acked) {
1251 		return (FCT_FAILURE);
1252 	}
1253 
1254 	/* Stop the taskq 1st */
1255 	if (iport->iport_flags & IPORT_WORKER_RUNNING) {
1256 		atomic_or_32(&iport->iport_flags, IPORT_TERMINATE_WORKER);
1257 		cv_broadcast(&iport->iport_worker_cv);
1258 		for (ndx = 0; ndx < 100; ndx++) {
1259 			if ((iport->iport_flags & IPORT_WORKER_RUNNING)
1260 			    == 0) {
1261 				break;
1262 			}
1263 			delay(drv_usectohz(10000));
1264 		}
1265 		if (ndx == 100) {
1266 			atomic_and_32(&iport->iport_flags,
1267 			    ~IPORT_TERMINATE_WORKER);
1268 			return (FCT_WORKER_STUCK);
1269 		}
1270 	}
1271 
1272 	if (stmf_deregister_local_port(port->port_lport) != FCT_SUCCESS) {
1273 		goto fct_deregport_fail1;
1274 	}
1275 
1276 	mutex_enter(&fct_global_mutex);
1277 	if (iport->iport_next)
1278 		iport->iport_next->iport_prev = iport->iport_prev;
1279 	if (iport->iport_prev)
1280 		iport->iport_prev->iport_next = iport->iport_next;
1281 	else
1282 		fct_iport_list = iport->iport_next;
1283 	mutex_exit(&fct_global_mutex);
1284 	/*
1285 	 * At this time, there should be no outstanding and pending
1286 	 * I/Os, so we can just release resources.
1287 	 */
1288 	ASSERT(iport->iport_total_alloced_ncmds == iport->iport_cached_ncmds);
1289 	for (icmd = iport->iport_cached_cmdlist; icmd; icmd = next_icmd) {
1290 		next_icmd = icmd->icmd_next;
1291 		fct_free(icmd->icmd_cmd);
1292 	}
1293 	mutex_destroy(&iport->iport_cached_cmd_lock);
1294 	kmem_free(iport->iport_cmd_slots, port->port_max_xchges *
1295 	    sizeof (fct_cmd_slot_t));
1296 	kmem_free(iport->iport_rp_slots, port->port_max_logins *
1297 	    sizeof (fct_i_remote_port_t *));
1298 	rw_destroy(&iport->iport_lock);
1299 	cv_destroy(&iport->iport_worker_cv);
1300 	sema_destroy(&iport->iport_rls_sema);
1301 	mutex_destroy(&iport->iport_worker_lock);
1302 	ddi_taskq_destroy(iport->iport_worker_taskq);
1303 	if (iport->iport_rp_tb) {
1304 		kmem_free(iport->iport_rp_tb, rportid_table_size *
1305 		    sizeof (fct_i_remote_port_t *));
1306 	}
1307 
1308 	if (iport->iport_kstat_portstat) {
1309 		kstat_delete(iport->iport_kstat_portstat);
1310 	}
1311 
1312 	fct_log_local_port_event(port, ESC_SUNFC_PORT_DETACH);
1313 	return (FCT_SUCCESS);
1314 
1315 fct_deregport_fail1:;
1316 	/* Restart the worker */
1317 	atomic_and_32(&iport->iport_flags, ~IPORT_TERMINATE_WORKER);
1318 	(void) ddi_taskq_dispatch(iport->iport_worker_taskq,
1319 	    fct_port_worker, port, DDI_SLEEP);
1320 	/* Wait for taskq to start */
1321 	while ((iport->iport_flags & IPORT_WORKER_RUNNING) == 0) {
1322 		delay(1);
1323 	}
1324 	return (FCT_FAILURE);
1325 }
1326 
1327 /* ARGSUSED */
1328 void
1329 fct_handle_event(fct_local_port_t *port, int event_id, uint32_t event_flags,
1330 		caddr_t arg)
1331 {
1332 	char			info[80];
1333 	fct_i_event_t		*e;
1334 	fct_i_local_port_t	*iport = (fct_i_local_port_t *)
1335 	    port->port_fct_private;
1336 
1337 	e = kmem_zalloc(sizeof (fct_i_event_t), KM_NOSLEEP);
1338 
1339 	if (e == NULL) {
1340 		/*
1341 		 * XXX Throw HBA fatal error event
1342 		 */
1343 		(void) snprintf(info, 80,
1344 		    "fct_handle_event: iport-%p, allocation "
1345 		    "of fct_i_event failed", (void *)iport);
1346 		info[79] = 0;
1347 		(void) fct_port_shutdown(iport->iport_port,
1348 		    STMF_RFLAG_FATAL_ERROR | STMF_RFLAG_RESET, info);
1349 		return;
1350 	}
1351 	/* Just queue the event */
1352 	e->event_type = event_id;
1353 	mutex_enter(&iport->iport_worker_lock);
1354 	if (iport->iport_event_head == NULL) {
1355 		iport->iport_event_head = iport->iport_event_tail = e;
1356 	} else {
1357 		iport->iport_event_tail->event_next = e;
1358 		iport->iport_event_tail = e;
1359 	}
1360 	if (IS_WORKER_SLEEPING(iport))
1361 		cv_signal(&iport->iport_worker_cv);
1362 	mutex_exit(&iport->iport_worker_lock);
1363 }
1364 
1365 /*
1366  * Called with iport_lock held as reader.
1367  */
1368 fct_i_remote_port_t *
1369 fct_portid_to_portptr(fct_i_local_port_t *iport, uint32_t portid)
1370 {
1371 	fct_i_remote_port_t	*irp;
1372 
1373 	irp = iport->iport_rp_tb[FCT_PORTID_HASH_FUNC(portid)];
1374 	for (; irp != NULL; irp = irp->irp_next) {
1375 		if (irp->irp_portid == portid)
1376 			return (irp);
1377 	}
1378 
1379 	return (NULL);
1380 
1381 }
1382 
1383 /*
1384  * Called with irp_lock held as writer.
1385  */
1386 void
1387 fct_queue_rp(fct_i_local_port_t *iport, fct_i_remote_port_t *irp)
1388 {
1389 	int hash_key =
1390 	    FCT_PORTID_HASH_FUNC(irp->irp_portid);
1391 
1392 	irp->irp_next = iport->iport_rp_tb[hash_key];
1393 	iport->iport_rp_tb[hash_key] = irp;
1394 	iport->iport_nrps++;
1395 }
1396 
1397 /*
1398  * Called with irp_lock and iport_lock held as writer.
1399  */
1400 void
1401 fct_deque_rp(fct_i_local_port_t *iport, fct_i_remote_port_t *irp)
1402 {
1403 	fct_i_remote_port_t	*irp_next = NULL;
1404 	fct_i_remote_port_t	*irp_last = NULL;
1405 	int hash_key			  =
1406 	    FCT_PORTID_HASH_FUNC(irp->irp_portid);
1407 
1408 	irp_next = iport->iport_rp_tb[hash_key];
1409 	irp_last = NULL;
1410 	while (irp_next != NULL) {
1411 		if (irp == irp_next) {
1412 			break;
1413 		}
1414 		irp_last = irp_next;
1415 		irp_next = irp_next->irp_next;
1416 	}
1417 
1418 	if (irp_next) {
1419 		if (irp_last == NULL) {
1420 			iport->iport_rp_tb[hash_key] =
1421 			    irp->irp_next;
1422 		} else {
1423 			irp_last->irp_next = irp->irp_next;
1424 		}
1425 		irp->irp_next = NULL;
1426 		iport->iport_nrps--;
1427 	}
1428 }
1429 
1430 int
1431 fct_is_irp_logging_out(fct_i_remote_port_t *irp, int force_implicit)
1432 {
1433 	int logging_out = 0;
1434 
1435 	rw_enter(&irp->irp_lock, RW_WRITER);
1436 	if ((irp->irp_flags & IRP_IN_DISCOVERY_QUEUE) == 0) {
1437 		logging_out = 0;
1438 		goto ilo_done;
1439 	}
1440 	if ((irp->irp_els_list == NULL) && (irp->irp_deregister_timer)) {
1441 		if (force_implicit && irp->irp_nonfcp_xchg_count) {
1442 			logging_out = 0;
1443 		} else {
1444 			logging_out = 1;
1445 		}
1446 		goto ilo_done;
1447 	}
1448 	if (irp->irp_els_list) {
1449 		fct_i_cmd_t *icmd;
1450 		/* Last session affecting ELS should be a LOGO */
1451 		for (icmd = irp->irp_els_list; icmd; icmd = icmd->icmd_next) {
1452 			uint8_t op = (ICMD_TO_ELS(icmd))->els_req_payload[0];
1453 			if (op == ELS_OP_LOGO) {
1454 				if (force_implicit) {
1455 					if (icmd->icmd_flags & ICMD_IMPLICIT)
1456 						logging_out = 1;
1457 					else
1458 						logging_out = 0;
1459 				} else {
1460 					logging_out = 1;
1461 				}
1462 			} else if ((op == ELS_OP_PLOGI) ||
1463 			    (op == ELS_OP_PRLI) ||
1464 			    (op == ELS_OP_PRLO) || (op == ELS_OP_TPRLO)) {
1465 				logging_out = 0;
1466 			}
1467 		}
1468 	}
1469 ilo_done:;
1470 	rw_exit(&irp->irp_lock);
1471 
1472 	return (logging_out);
1473 }
1474 
1475 /*
1476  * The force_implicit flag enforces the implicit semantics which may be
1477  * needed if a received logout got stuck e.g. a response to a received
1478  * LOGO never came back from the FCA.
1479  */
1480 int
1481 fct_implicitly_logo_all(fct_i_local_port_t *iport, int force_implicit)
1482 {
1483 	fct_i_remote_port_t	*irp = NULL;
1484 	fct_cmd_t		*cmd = NULL;
1485 	int			 i   = 0;
1486 	int			nports = 0;
1487 
1488 	if (!iport->iport_nrps) {
1489 		return (nports);
1490 	}
1491 
1492 	rw_enter(&iport->iport_lock, RW_WRITER);
1493 	for (i = 0; i < rportid_table_size; i++) {
1494 		irp = iport->iport_rp_tb[i];
1495 		while (irp) {
1496 			if ((!(irp->irp_flags & IRP_PLOGI_DONE)) &&
1497 			    (fct_is_irp_logging_out(irp, force_implicit))) {
1498 				irp = irp->irp_next;
1499 				continue;
1500 			}
1501 
1502 			cmd = fct_create_solels(iport->iport_port, irp->irp_rp,
1503 			    1, ELS_OP_LOGO, 0, fct_logo_cb);
1504 			if (cmd == NULL) {
1505 				stmf_trace(iport->iport_alias,
1506 				    "fct_implictly_logo_all: cmd null");
1507 				rw_exit(&iport->iport_lock);
1508 
1509 				return (nports);
1510 			}
1511 
1512 			fct_post_implicit_logo(cmd);
1513 			nports++;
1514 			irp = irp->irp_next;
1515 		}
1516 	}
1517 	rw_exit(&iport->iport_lock);
1518 
1519 	return (nports);
1520 }
1521 
1522 void
1523 fct_rehash(fct_i_local_port_t *iport)
1524 {
1525 	fct_i_remote_port_t **iport_rp_tb_tmp;
1526 	fct_i_remote_port_t **iport_rp_tb_new;
1527 	fct_i_remote_port_t *irp;
1528 	fct_i_remote_port_t *irp_next;
1529 	int i;
1530 
1531 	iport_rp_tb_new = kmem_zalloc(rportid_table_size *
1532 	    sizeof (fct_i_remote_port_t *), KM_SLEEP);
1533 	rw_enter(&iport->iport_lock, RW_WRITER);
1534 	/* reconstruct the hash table */
1535 	iport_rp_tb_tmp = iport->iport_rp_tb;
1536 	iport->iport_rp_tb = iport_rp_tb_new;
1537 	iport->iport_nrps = 0;
1538 	for (i = 0; i < rportid_table_size; i++) {
1539 		irp = iport_rp_tb_tmp[i];
1540 		while (irp) {
1541 			irp_next = irp->irp_next;
1542 			fct_queue_rp(iport, irp);
1543 			irp = irp_next;
1544 		}
1545 	}
1546 	rw_exit(&iport->iport_lock);
1547 	kmem_free(iport_rp_tb_tmp, rportid_table_size *
1548 	    sizeof (fct_i_remote_port_t *));
1549 
1550 }
1551 
1552 uint8_t
1553 fct_local_port_cleanup_done(fct_i_local_port_t *iport)
1554 {
1555 	fct_i_remote_port_t *irp;
1556 	int i;
1557 
1558 	if (iport->iport_nrps_login)
1559 		return (0);
1560 	/* loop all rps to check if the cmd have already been drained */
1561 	for (i = 0; i < rportid_table_size; i++) {
1562 		irp = iport->iport_rp_tb[i];
1563 		while (irp) {
1564 			if (irp->irp_fcp_xchg_count ||
1565 			    irp->irp_nonfcp_xchg_count)
1566 				return (0);
1567 			irp = irp->irp_next;
1568 		}
1569 	}
1570 	return (1);
1571 }
1572 
1573 fct_cmd_t *
1574 fct_scsi_task_alloc(fct_local_port_t *port, uint16_t rp_handle,
1575 		uint32_t rportid, uint8_t *lun, uint16_t cdb_length,
1576 		uint16_t task_ext)
1577 {
1578 	fct_cmd_t *cmd;
1579 	fct_i_cmd_t *icmd;
1580 	fct_i_local_port_t *iport =
1581 	    (fct_i_local_port_t *)port->port_fct_private;
1582 	fct_i_remote_port_t *irp;
1583 	scsi_task_t *task;
1584 	fct_remote_port_t *rp;
1585 	uint16_t cmd_slot;
1586 
1587 	rw_enter(&iport->iport_lock, RW_READER);
1588 	if ((iport->iport_link_state & S_LINK_ONLINE) == 0) {
1589 		rw_exit(&iport->iport_lock);
1590 		stmf_trace(iport->iport_alias, "cmd alloc called while the port"
1591 		    " was offline");
1592 		return (NULL);
1593 	}
1594 
1595 	if (rp_handle == FCT_HANDLE_NONE) {
1596 		irp = fct_portid_to_portptr(iport, rportid);
1597 		if (irp == NULL) {
1598 			rw_exit(&iport->iport_lock);
1599 			stmf_trace(iport->iport_alias, "cmd received from "
1600 			    "non existent port %x", rportid);
1601 			return (NULL);
1602 		}
1603 	} else {
1604 		if ((rp_handle >= port->port_max_logins) ||
1605 		    ((irp = iport->iport_rp_slots[rp_handle]) == NULL)) {
1606 			rw_exit(&iport->iport_lock);
1607 			stmf_trace(iport->iport_alias, "cmd received from "
1608 			    "invalid port handle %x", rp_handle);
1609 			return (NULL);
1610 		}
1611 	}
1612 	rp = irp->irp_rp;
1613 
1614 	rw_enter(&irp->irp_lock, RW_READER);
1615 	if ((irp->irp_flags & IRP_PRLI_DONE) == 0) {
1616 		rw_exit(&irp->irp_lock);
1617 		rw_exit(&iport->iport_lock);
1618 		stmf_trace(iport->iport_alias, "cmd alloc called while fcp "
1619 		    "login was not done. portid=%x, rp=%p", rp->rp_id, rp);
1620 		return (NULL);
1621 	}
1622 
1623 	mutex_enter(&iport->iport_cached_cmd_lock);
1624 	if ((icmd = iport->iport_cached_cmdlist) != NULL) {
1625 		iport->iport_cached_cmdlist = icmd->icmd_next;
1626 		iport->iport_cached_ncmds--;
1627 		cmd = icmd->icmd_cmd;
1628 	} else {
1629 		icmd = NULL;
1630 	}
1631 	mutex_exit(&iport->iport_cached_cmd_lock);
1632 	if (icmd == NULL) {
1633 		cmd = (fct_cmd_t *)fct_alloc(FCT_STRUCT_CMD_FCP_XCHG,
1634 		    port->port_fca_fcp_cmd_size, 0);
1635 		if (cmd == NULL) {
1636 			rw_exit(&irp->irp_lock);
1637 			rw_exit(&iport->iport_lock);
1638 			stmf_trace(iport->iport_alias, "Ran out of "
1639 			    "memory, port=%p", port);
1640 			return (NULL);
1641 		}
1642 
1643 		icmd = (fct_i_cmd_t *)cmd->cmd_fct_private;
1644 		icmd->icmd_next = NULL;
1645 		cmd->cmd_port = port;
1646 		atomic_add_32(&iport->iport_total_alloced_ncmds, 1);
1647 	}
1648 
1649 	/*
1650 	 * The accuracy of iport_max_active_ncmds is not important
1651 	 */
1652 	if ((iport->iport_total_alloced_ncmds - iport->iport_cached_ncmds) >
1653 	    iport->iport_max_active_ncmds) {
1654 		iport->iport_max_active_ncmds =
1655 		    iport->iport_total_alloced_ncmds -
1656 		    iport->iport_cached_ncmds;
1657 	}
1658 
1659 	/* Lets get a slot */
1660 	cmd_slot = fct_alloc_cmd_slot(iport, cmd);
1661 	if (cmd_slot == FCT_SLOT_EOL) {
1662 		rw_exit(&irp->irp_lock);
1663 		rw_exit(&iport->iport_lock);
1664 		stmf_trace(iport->iport_alias, "Ran out of xchg resources");
1665 		cmd->cmd_handle = 0;
1666 		fct_cmd_free(cmd);
1667 		return (NULL);
1668 	}
1669 	atomic_add_16(&irp->irp_fcp_xchg_count, 1);
1670 	cmd->cmd_rp = rp;
1671 	icmd->icmd_flags |= ICMD_IN_TRANSITION | ICMD_KNOWN_TO_FCA;
1672 	rw_exit(&irp->irp_lock);
1673 	rw_exit(&iport->iport_lock);
1674 
1675 	icmd->icmd_start_time = ddi_get_lbolt();
1676 
1677 	cmd->cmd_specific = stmf_task_alloc(port->port_lport, irp->irp_session,
1678 	    lun, cdb_length, task_ext);
1679 	if ((task = (scsi_task_t *)cmd->cmd_specific) != NULL) {
1680 		task->task_port_private = cmd;
1681 		return (cmd);
1682 	}
1683 
1684 	fct_cmd_free(cmd);
1685 
1686 	return (NULL);
1687 }
1688 
1689 void
1690 fct_scsi_task_free(scsi_task_t *task)
1691 {
1692 	fct_cmd_t *cmd = (fct_cmd_t *)task->task_port_private;
1693 
1694 	cmd->cmd_comp_status = task->task_completion_status;
1695 	fct_cmd_free(cmd);
1696 }
1697 
1698 void
1699 fct_post_rcvd_cmd(fct_cmd_t *cmd, stmf_data_buf_t *dbuf)
1700 {
1701 	if (cmd->cmd_type == FCT_CMD_FCP_XCHG) {
1702 		fct_i_cmd_t *icmd = (fct_i_cmd_t *)cmd->cmd_fct_private;
1703 		fct_i_local_port_t *iport =
1704 		    (fct_i_local_port_t *)cmd->cmd_port->port_fct_private;
1705 		fct_i_remote_port_t *irp =
1706 		    (fct_i_remote_port_t *)cmd->cmd_rp->rp_fct_private;
1707 		scsi_task_t *task = (scsi_task_t *)cmd->cmd_specific;
1708 
1709 		uint16_t irp_task = irp->irp_fcp_xchg_count;
1710 		uint32_t load = iport->iport_total_alloced_ncmds -
1711 		    iport->iport_cached_ncmds;
1712 
1713 		DTRACE_FC_4(scsi__command,
1714 		    fct_cmd_t, cmd,
1715 		    fct_i_local_port_t, iport,
1716 		    scsi_task_t, task,
1717 		    fct_i_remote_port_t, irp);
1718 
1719 		if (load >= iport->iport_task_green_limit) {
1720 			if ((load < iport->iport_task_yellow_limit &&
1721 			    irp_task >= 4) ||
1722 			    (load >= iport->iport_task_yellow_limit &&
1723 			    load < iport->iport_task_red_limit &&
1724 			    irp_task >= 1) ||
1725 			    (load >= iport->iport_task_red_limit))
1726 				task->task_additional_flags |=
1727 				    TASK_AF_PORT_LOAD_HIGH;
1728 		}
1729 		stmf_post_task((scsi_task_t *)cmd->cmd_specific, dbuf);
1730 		atomic_and_32(&icmd->icmd_flags, ~ICMD_IN_TRANSITION);
1731 		return;
1732 	}
1733 	/* We dont need dbuf for other cmds */
1734 	if (dbuf) {
1735 		cmd->cmd_port->port_fds->fds_free_data_buf(
1736 		    cmd->cmd_port->port_fds, dbuf);
1737 		dbuf = NULL;
1738 	}
1739 	if (cmd->cmd_type == FCT_CMD_RCVD_ELS) {
1740 		fct_handle_els(cmd);
1741 		return;
1742 	}
1743 	if (cmd->cmd_type == FCT_CMD_RCVD_ABTS) {
1744 		fct_handle_rcvd_abts(cmd);
1745 		return;
1746 	}
1747 
1748 	ASSERT(0);
1749 }
1750 
1751 /*
1752  * This function bypasses fct_handle_els()
1753  */
1754 void
1755 fct_post_implicit_logo(fct_cmd_t *cmd)
1756 {
1757 	fct_local_port_t *port = cmd->cmd_port;
1758 	fct_i_local_port_t *iport =
1759 	    (fct_i_local_port_t *)port->port_fct_private;
1760 	fct_i_cmd_t *icmd = (fct_i_cmd_t *)cmd->cmd_fct_private;
1761 	fct_remote_port_t *rp = cmd->cmd_rp;
1762 	fct_i_remote_port_t *irp = (fct_i_remote_port_t *)rp->rp_fct_private;
1763 
1764 	icmd->icmd_start_time = ddi_get_lbolt();
1765 
1766 	rw_enter(&irp->irp_lock, RW_WRITER);
1767 	atomic_or_32(&icmd->icmd_flags, ICMD_IMPLICIT_CMD_HAS_RESOURCE);
1768 	atomic_add_16(&irp->irp_nonfcp_xchg_count, 1);
1769 	atomic_add_16(&irp->irp_sa_elses_count, 1);
1770 	/*
1771 	 * An implicit LOGO can also be posted to a irp where a PLOGI might
1772 	 * be in process. That PLOGI will reset this flag and decrement the
1773 	 * iport_nrps_login counter.
1774 	 */
1775 	if (irp->irp_flags & IRP_PLOGI_DONE) {
1776 		atomic_add_32(&iport->iport_nrps_login, -1);
1777 	}
1778 	atomic_and_32(&irp->irp_flags, ~(IRP_PLOGI_DONE | IRP_PRLI_DONE));
1779 	atomic_or_32(&icmd->icmd_flags, ICMD_SESSION_AFFECTING);
1780 	fct_post_to_discovery_queue(iport, irp, icmd);
1781 	rw_exit(&irp->irp_lock);
1782 }
1783 
1784 /*
1785  * called with iport_lock held, return the slot number
1786  */
1787 uint16_t
1788 fct_alloc_cmd_slot(fct_i_local_port_t *iport, fct_cmd_t *cmd)
1789 {
1790 	uint16_t cmd_slot;
1791 	uint32_t old, new;
1792 	fct_i_cmd_t *icmd = (fct_i_cmd_t *)cmd->cmd_fct_private;
1793 
1794 	do {
1795 		old = iport->iport_next_free_slot;
1796 		cmd_slot = old & 0xFFFF;
1797 		if (cmd_slot == FCT_SLOT_EOL)
1798 			return (cmd_slot);
1799 		/*
1800 		 * We use high order 16 bits as a counter which keeps on
1801 		 * incrementing to avoid ABA issues with atomic lists.
1802 		 */
1803 		new = ((old + (0x10000)) & 0xFFFF0000);
1804 		new |= iport->iport_cmd_slots[cmd_slot].slot_next;
1805 	} while (atomic_cas_32(&iport->iport_next_free_slot, old, new) != old);
1806 
1807 	atomic_add_16(&iport->iport_nslots_free, -1);
1808 	iport->iport_cmd_slots[cmd_slot].slot_cmd = icmd;
1809 	cmd->cmd_handle = (uint32_t)cmd_slot | 0x80000000 |
1810 	    (((uint32_t)(iport->iport_cmd_slots[cmd_slot].slot_uniq_cntr))
1811 	    << 24);
1812 	return (cmd_slot);
1813 }
1814 
1815 /*
1816  * If icmd is not NULL, irp_lock must be held
1817  */
1818 void
1819 fct_post_to_discovery_queue(fct_i_local_port_t *iport,
1820     fct_i_remote_port_t *irp, fct_i_cmd_t *icmd)
1821 {
1822 	fct_i_cmd_t	**p;
1823 
1824 	ASSERT(!MUTEX_HELD(&iport->iport_worker_lock));
1825 	if (icmd) {
1826 		icmd->icmd_next = NULL;
1827 		for (p = &irp->irp_els_list; *p != NULL;
1828 		    p = &((*p)->icmd_next))
1829 			;
1830 
1831 		*p = icmd;
1832 		atomic_or_32(&icmd->icmd_flags, ICMD_IN_IRP_QUEUE);
1833 	}
1834 
1835 	mutex_enter(&iport->iport_worker_lock);
1836 	if ((irp->irp_flags & IRP_IN_DISCOVERY_QUEUE) == 0) {
1837 
1838 		/*
1839 		 * CAUTION: do not grab local_port/remote_port locks after
1840 		 * grabbing the worker lock.
1841 		 */
1842 		irp->irp_discovery_next = NULL;
1843 		if (iport->iport_rpwe_tail) {
1844 			iport->iport_rpwe_tail->irp_discovery_next = irp;
1845 			iport->iport_rpwe_tail = irp;
1846 		} else {
1847 			iport->iport_rpwe_head = iport->iport_rpwe_tail = irp;
1848 		}
1849 
1850 		atomic_or_32(&irp->irp_flags, IRP_IN_DISCOVERY_QUEUE);
1851 	}
1852 
1853 	/*
1854 	 * We need always signal the port worker irrespective of the fact that
1855 	 * irp is already in discovery queue or not.
1856 	 */
1857 	if (IS_WORKER_SLEEPING(iport)) {
1858 		cv_signal(&iport->iport_worker_cv);
1859 	}
1860 	mutex_exit(&iport->iport_worker_lock);
1861 }
1862 
1863 stmf_status_t
1864 fct_xfer_scsi_data(scsi_task_t *task, stmf_data_buf_t *dbuf, uint32_t ioflags)
1865 {
1866 	fct_cmd_t *cmd = (fct_cmd_t *)task->task_port_private;
1867 
1868 	DTRACE_FC_5(xfer__start,
1869 	    fct_cmd_t, cmd,
1870 	    fct_i_local_port_t, cmd->cmd_port->port_fct_private,
1871 	    scsi_task_t, task,
1872 	    fct_i_remote_port_t, cmd->cmd_rp->rp_fct_private,
1873 	    stmf_data_buf_t, dbuf);
1874 
1875 	return (cmd->cmd_port->port_xfer_scsi_data(cmd, dbuf, ioflags));
1876 }
1877 
1878 void
1879 fct_scsi_data_xfer_done(fct_cmd_t *cmd, stmf_data_buf_t *dbuf, uint32_t ioflags)
1880 {
1881 	fct_i_cmd_t	*icmd = (fct_i_cmd_t *)cmd->cmd_fct_private;
1882 	uint32_t	old, new;
1883 	uint32_t	iof = 0;
1884 
1885 	DTRACE_FC_5(xfer__done,
1886 	    fct_cmd_t, cmd,
1887 	    fct_i_local_port_t, cmd->cmd_port->port_fct_private,
1888 	    scsi_task_t, ((scsi_task_t *)cmd->cmd_specific),
1889 	    fct_i_remote_port_t, cmd->cmd_rp->rp_fct_private,
1890 	    stmf_data_buf_t, dbuf);
1891 
1892 	if (ioflags & FCT_IOF_FCA_DONE) {
1893 		do {
1894 			old = new = icmd->icmd_flags;
1895 			if (old & ICMD_BEING_ABORTED) {
1896 				return;
1897 			}
1898 			new &= ~ICMD_KNOWN_TO_FCA;
1899 		} while (atomic_cas_32(&icmd->icmd_flags, old, new) != old);
1900 		iof = STMF_IOF_LPORT_DONE;
1901 		cmd->cmd_comp_status = dbuf->db_xfer_status;
1902 	}
1903 
1904 	if (icmd->icmd_flags & ICMD_BEING_ABORTED)
1905 		return;
1906 	stmf_data_xfer_done((scsi_task_t *)cmd->cmd_specific, dbuf, iof);
1907 }
1908 
1909 stmf_status_t
1910 fct_send_scsi_status(scsi_task_t *task, uint32_t ioflags)
1911 {
1912 	fct_cmd_t *cmd = (fct_cmd_t *)task->task_port_private;
1913 
1914 	DTRACE_FC_4(scsi__response,
1915 	    fct_cmd_t, cmd,
1916 	    fct_i_local_port_t,
1917 	    (fct_i_local_port_t *)cmd->cmd_port->port_fct_private,
1918 	    scsi_task_t, task,
1919 	    fct_i_remote_port_t,
1920 	    (fct_i_remote_port_t *)cmd->cmd_rp->rp_fct_private);
1921 
1922 	return (cmd->cmd_port->port_send_cmd_response(cmd, ioflags));
1923 }
1924 
1925 void
1926 fct_send_response_done(fct_cmd_t *cmd, fct_status_t s, uint32_t ioflags)
1927 {
1928 	fct_i_cmd_t	*icmd = (fct_i_cmd_t *)cmd->cmd_fct_private;
1929 	fct_local_port_t *port = cmd->cmd_port;
1930 	fct_i_local_port_t *iport = (fct_i_local_port_t *)
1931 	    port->port_fct_private;
1932 	uint32_t old, new;
1933 
1934 	if ((ioflags & FCT_IOF_FCA_DONE) == 0) {
1935 		/* Until we support confirmed completions, this is an error */
1936 		fct_queue_cmd_for_termination(cmd, s);
1937 		return;
1938 	}
1939 	do {
1940 		old = new = icmd->icmd_flags;
1941 		if (old & ICMD_BEING_ABORTED) {
1942 			return;
1943 		}
1944 		new &= ~ICMD_KNOWN_TO_FCA;
1945 	} while (atomic_cas_32(&icmd->icmd_flags, old, new) != old);
1946 
1947 	cmd->cmd_comp_status = s;
1948 	if (cmd->cmd_type == FCT_CMD_FCP_XCHG) {
1949 		stmf_send_status_done((scsi_task_t *)cmd->cmd_specific, s,
1950 		    STMF_IOF_LPORT_DONE);
1951 		return;
1952 	}
1953 
1954 	if (cmd->cmd_type == FCT_CMD_RCVD_ELS) {
1955 		fct_cmd_free(cmd);
1956 		return;
1957 	} else if (cmd->cmd_type == FCT_CMD_SOL_ELS) {
1958 		fct_handle_sol_els_completion(iport, icmd);
1959 	} else if (cmd->cmd_type == FCT_CMD_SOL_CT) {
1960 		/* Tell the caller that we are done */
1961 		atomic_or_32(&icmd->icmd_flags, ICMD_CMD_COMPLETE);
1962 	} else {
1963 		ASSERT(0);
1964 	}
1965 }
1966 
1967 void
1968 fct_cmd_free(fct_cmd_t *cmd)
1969 {
1970 	char			info[80];
1971 	fct_i_cmd_t		*icmd = (fct_i_cmd_t *)cmd->cmd_fct_private;
1972 	fct_local_port_t	*port = cmd->cmd_port;
1973 	fct_i_local_port_t	*iport = (fct_i_local_port_t *)
1974 	    port->port_fct_private;
1975 	fct_i_remote_port_t	*irp = NULL;
1976 	int			do_abts_acc = 0;
1977 	uint32_t		old, new;
1978 
1979 	ASSERT(!mutex_owned(&iport->iport_worker_lock));
1980 	/* Give the slot back */
1981 	if (CMD_HANDLE_VALID(cmd->cmd_handle)) {
1982 		uint16_t n = CMD_HANDLE_SLOT_INDEX(cmd->cmd_handle);
1983 		fct_cmd_slot_t *slot;
1984 
1985 		/*
1986 		 * If anything went wrong, grab the lock as writer. This is
1987 		 * probably unnecessary.
1988 		 */
1989 		if ((cmd->cmd_comp_status != FCT_SUCCESS) ||
1990 		    (icmd->icmd_flags & ICMD_ABTS_RECEIVED)) {
1991 			rw_enter(&iport->iport_lock, RW_WRITER);
1992 		} else {
1993 			rw_enter(&iport->iport_lock, RW_READER);
1994 		}
1995 
1996 		if ((icmd->icmd_flags & ICMD_ABTS_RECEIVED) &&
1997 		    (cmd->cmd_link != NULL)) {
1998 			do_abts_acc = 1;
1999 		}
2000 
2001 		/* XXX Validate slot before freeing */
2002 
2003 		slot = &iport->iport_cmd_slots[n];
2004 		slot->slot_uniq_cntr++;
2005 		slot->slot_cmd = NULL;
2006 		do {
2007 			old = iport->iport_next_free_slot;
2008 			slot->slot_next = old & 0xFFFF;
2009 			new = (old + 0x10000) & 0xFFFF0000;
2010 			new |= slot->slot_no;
2011 		} while (atomic_cas_32(&iport->iport_next_free_slot,
2012 		    old, new) != old);
2013 		cmd->cmd_handle = 0;
2014 		atomic_add_16(&iport->iport_nslots_free, 1);
2015 		if (cmd->cmd_rp) {
2016 			irp = (fct_i_remote_port_t *)
2017 			    cmd->cmd_rp->rp_fct_private;
2018 			if (cmd->cmd_type == FCT_CMD_FCP_XCHG)
2019 				atomic_add_16(&irp->irp_fcp_xchg_count, -1);
2020 			else
2021 				atomic_add_16(&irp->irp_nonfcp_xchg_count, -1);
2022 		}
2023 		rw_exit(&iport->iport_lock);
2024 	} else if ((icmd->icmd_flags & ICMD_IMPLICIT) &&
2025 	    (icmd->icmd_flags & ICMD_IMPLICIT_CMD_HAS_RESOURCE)) {
2026 		/* for implicit cmd, no cmd slot is used */
2027 		if (cmd->cmd_rp) {
2028 			irp = (fct_i_remote_port_t *)
2029 			    cmd->cmd_rp->rp_fct_private;
2030 			if (cmd->cmd_type == FCT_CMD_FCP_XCHG)
2031 				atomic_add_16(&irp->irp_fcp_xchg_count, -1);
2032 			else
2033 				atomic_add_16(&irp->irp_nonfcp_xchg_count, -1);
2034 		}
2035 	}
2036 
2037 	if (do_abts_acc) {
2038 		fct_cmd_t *lcmd = cmd->cmd_link;
2039 		fct_fill_abts_acc(lcmd);
2040 		if (port->port_send_cmd_response(lcmd,
2041 		    FCT_IOF_FORCE_FCA_DONE) != FCT_SUCCESS) {
2042 			/*
2043 			 * XXX Throw HBA fatal error event
2044 			 * Later shutdown svc will terminate the ABTS in the end
2045 			 */
2046 			(void) snprintf(info, 80,
2047 			    "fct_cmd_free: iport-%p, ABTS_ACC"
2048 			    " port_send_cmd_response failed", (void *)iport);
2049 			info[79] = 0;
2050 			(void) fct_port_shutdown(iport->iport_port,
2051 			    STMF_RFLAG_FATAL_ERROR | STMF_RFLAG_RESET, info);
2052 			return;
2053 		} else {
2054 			fct_cmd_free(lcmd);
2055 			cmd->cmd_link = NULL;
2056 		}
2057 	}
2058 
2059 	/* Free the cmd */
2060 	if (cmd->cmd_type == FCT_CMD_FCP_XCHG) {
2061 		if (iport->iport_cached_ncmds < max_cached_ncmds) {
2062 			icmd->icmd_flags = 0;
2063 			mutex_enter(&iport->iport_cached_cmd_lock);
2064 			icmd->icmd_next = iport->iport_cached_cmdlist;
2065 			iport->iport_cached_cmdlist = icmd;
2066 			iport->iport_cached_ncmds++;
2067 			mutex_exit(&iport->iport_cached_cmd_lock);
2068 		} else {
2069 			atomic_add_32(&iport->iport_total_alloced_ncmds, -1);
2070 			fct_free(cmd);
2071 		}
2072 	} else {
2073 		fct_free(cmd);
2074 	}
2075 }
2076 
2077 /* ARGSUSED */
2078 stmf_status_t
2079 fct_scsi_abort(stmf_local_port_t *lport, int abort_cmd, void *arg,
2080 							uint32_t flags)
2081 {
2082 	stmf_status_t ret = STMF_SUCCESS;
2083 	scsi_task_t *task;
2084 	fct_cmd_t *cmd;
2085 	fct_i_cmd_t *icmd;
2086 	fct_local_port_t *port;
2087 	uint32_t old, new;
2088 
2089 	ASSERT(abort_cmd == STMF_LPORT_ABORT_TASK);
2090 
2091 	task = (scsi_task_t *)arg;
2092 	cmd = (fct_cmd_t *)task->task_port_private;
2093 	icmd = (fct_i_cmd_t *)cmd->cmd_fct_private;
2094 	port = (fct_local_port_t *)lport->lport_port_private;
2095 
2096 	do {
2097 		old = new = icmd->icmd_flags;
2098 		if ((old & ICMD_KNOWN_TO_FCA) == 0)
2099 			return (STMF_NOT_FOUND);
2100 		ASSERT((old & ICMD_FCA_ABORT_CALLED) == 0);
2101 		new |= ICMD_BEING_ABORTED | ICMD_FCA_ABORT_CALLED;
2102 	} while (atomic_cas_32(&icmd->icmd_flags, old, new) != old);
2103 	ret = port->port_abort_cmd(port, cmd, 0);
2104 	if ((ret == FCT_NOT_FOUND) || (ret == FCT_ABORT_SUCCESS)) {
2105 		atomic_and_32(&icmd->icmd_flags, ~ICMD_KNOWN_TO_FCA);
2106 	} else if (ret == FCT_BUSY) {
2107 		atomic_and_32(&icmd->icmd_flags, ~ICMD_FCA_ABORT_CALLED);
2108 	}
2109 
2110 	return (ret);
2111 }
2112 
2113 void
2114 fct_ctl(struct stmf_local_port *lport, int cmd, void *arg)
2115 {
2116 	fct_local_port_t *port;
2117 	fct_i_local_port_t *iport;
2118 	stmf_change_status_t st;
2119 	stmf_change_status_t *pst;
2120 
2121 	ASSERT((cmd == STMF_CMD_LPORT_ONLINE) ||
2122 	    (cmd == STMF_ACK_LPORT_ONLINE_COMPLETE) ||
2123 	    (cmd == STMF_CMD_LPORT_OFFLINE) ||
2124 	    (cmd == STMF_ACK_LPORT_OFFLINE_COMPLETE) ||
2125 	    (cmd == FCT_CMD_PORT_ONLINE_COMPLETE) ||
2126 	    (cmd == FCT_CMD_PORT_OFFLINE_COMPLETE));
2127 
2128 	port = (fct_local_port_t *)lport->lport_port_private;
2129 	pst = (stmf_change_status_t *)arg;
2130 	st.st_completion_status = STMF_SUCCESS;
2131 	st.st_additional_info = NULL;
2132 
2133 	iport = (fct_i_local_port_t *)port->port_fct_private;
2134 	/*
2135 	 * We are mostly a passthrough, except during offline.
2136 	 */
2137 	switch (cmd) {
2138 	case STMF_CMD_LPORT_ONLINE:
2139 		if (iport->iport_state == FCT_STATE_ONLINE)
2140 			st.st_completion_status = STMF_ALREADY;
2141 		else if (iport->iport_state != FCT_STATE_OFFLINE)
2142 			st.st_completion_status = STMF_INVALID_ARG;
2143 		if (st.st_completion_status != STMF_SUCCESS) {
2144 			(void) stmf_ctl(STMF_CMD_LPORT_ONLINE_COMPLETE, lport,
2145 			    &st);
2146 			break;
2147 		}
2148 		iport->iport_state_not_acked = 1;
2149 		iport->iport_state = FCT_STATE_ONLINING;
2150 		port->port_ctl(port, FCT_CMD_PORT_ONLINE, arg);
2151 		break;
2152 	case FCT_CMD_PORT_ONLINE_COMPLETE:
2153 		ASSERT(iport->iport_state == FCT_STATE_ONLINING);
2154 		if (pst->st_completion_status != FCT_SUCCESS) {
2155 			iport->iport_state = FCT_STATE_OFFLINE;
2156 			iport->iport_state_not_acked = 0;
2157 		} else {
2158 			iport->iport_state = FCT_STATE_ONLINE;
2159 		}
2160 		(void) stmf_ctl(STMF_CMD_LPORT_ONLINE_COMPLETE, lport, arg);
2161 		break;
2162 	case STMF_ACK_LPORT_ONLINE_COMPLETE:
2163 		ASSERT(iport->iport_state == FCT_STATE_ONLINE);
2164 		iport->iport_state_not_acked = 0;
2165 		port->port_ctl(port, FCT_ACK_PORT_ONLINE_COMPLETE, arg);
2166 		break;
2167 
2168 	case STMF_CMD_LPORT_OFFLINE:
2169 		if (iport->iport_state == FCT_STATE_OFFLINE)
2170 			st.st_completion_status = STMF_ALREADY;
2171 		else if (iport->iport_state != FCT_STATE_ONLINE)
2172 			st.st_completion_status = STMF_INVALID_ARG;
2173 		if (st.st_completion_status != STMF_SUCCESS) {
2174 			(void) stmf_ctl(STMF_CMD_LPORT_OFFLINE_COMPLETE, lport,
2175 			    &st);
2176 			break;
2177 		}
2178 		iport->iport_state_not_acked = 1;
2179 		iport->iport_state = FCT_STATE_OFFLINING;
2180 		port->port_ctl(port, FCT_CMD_PORT_OFFLINE, arg);
2181 		break;
2182 	case FCT_CMD_PORT_OFFLINE_COMPLETE:
2183 		ASSERT(iport->iport_state == FCT_STATE_OFFLINING);
2184 		if (pst->st_completion_status != FCT_SUCCESS) {
2185 			iport->iport_state = FCT_STATE_ONLINE;
2186 			iport->iport_state_not_acked = 0;
2187 			(void) stmf_ctl(STMF_CMD_LPORT_OFFLINE_COMPLETE, lport,
2188 			    pst);
2189 			break;
2190 		}
2191 
2192 		/*
2193 		 * If FCA's offline was successful, we dont tell stmf yet.
2194 		 * Becasue now we have to do the cleanup before we go upto
2195 		 * stmf. That cleanup is done by the worker thread.
2196 		 */
2197 
2198 		/* FCA is offline, post a link down, its harmless anyway */
2199 		fct_handle_event(port, FCT_EVENT_LINK_DOWN, 0, 0);
2200 
2201 		/* Trigger port offline processing by the worker */
2202 		iport->iport_offline_prstate = FCT_OPR_START;
2203 		break;
2204 	case STMF_ACK_LPORT_OFFLINE_COMPLETE:
2205 		ASSERT(iport->iport_state == FCT_STATE_OFFLINE);
2206 		iport->iport_state_not_acked = 0;
2207 		port->port_ctl(port, FCT_ACK_PORT_OFFLINE_COMPLETE, arg);
2208 		break;
2209 	}
2210 }
2211 
2212 /* ARGSUSED */
2213 stmf_status_t
2214 fct_info(uint32_t cmd, stmf_local_port_t *lport, void *arg, uint8_t *buf,
2215 						uint32_t *bufsizep)
2216 {
2217 	return (STMF_NOT_SUPPORTED);
2218 }
2219 
2220 /*
2221  * implicit: if it's true, it means it will only be used in fct module, or else
2222  * it will be sent to the link.
2223  */
2224 fct_cmd_t *
2225 fct_create_solels(fct_local_port_t *port, fct_remote_port_t *rp, int implicit,
2226     uchar_t elsop, uint32_t wkdid, fct_icmd_cb_t icmdcb)
2227 {
2228 	fct_cmd_t		*cmd	= NULL;
2229 	fct_i_cmd_t		*icmd	= NULL;
2230 	fct_els_t		*els	= NULL;
2231 	fct_i_remote_port_t	*irp	= NULL;
2232 	uint8_t			*p	= NULL;
2233 	uint32_t		 ptid	= 0;
2234 
2235 	cmd = (fct_cmd_t *)fct_alloc(FCT_STRUCT_CMD_SOL_ELS,
2236 	    port->port_fca_sol_els_private_size, 0);
2237 	if (!cmd) {
2238 		return (NULL);
2239 	}
2240 
2241 	if (rp) {
2242 		irp = RP_TO_IRP(rp);
2243 	} else if (((irp = fct_portid_to_portptr(PORT_TO_IPORT(port),
2244 	    wkdid)) == NULL) && (elsop != ELS_OP_PLOGI)) {
2245 		stmf_trace(PORT_TO_IPORT(port)->iport_alias,
2246 		    "fct_create_solels: Must PLOGI to %x first", wkdid);
2247 		fct_free(cmd);
2248 		return (NULL);
2249 	}
2250 
2251 	cmd->cmd_port	= port;
2252 	cmd->cmd_oxid	= PTR2INT(cmd, uint16_t);
2253 	cmd->cmd_rxid	= 0xFFFF;
2254 	cmd->cmd_handle = 0;
2255 	icmd		= CMD_TO_ICMD(cmd);
2256 	els		= ICMD_TO_ELS(icmd);
2257 	icmd->icmd_cb	= icmdcb;
2258 	if (irp) {
2259 		cmd->cmd_rp	   = irp->irp_rp;
2260 		cmd->cmd_rp_handle = irp->irp_rp->rp_handle;
2261 		cmd->cmd_rportid   = irp->irp_rp->rp_id;
2262 	} else {
2263 		cmd->cmd_rp_handle = FCT_HANDLE_NONE;
2264 		cmd->cmd_rportid   = wkdid;
2265 	}
2266 	cmd->cmd_lportid = (PORT_TO_IPORT(port))->iport_link_info.portid;
2267 
2268 	if (implicit) {
2269 		/*
2270 		 * Since we will not send it to FCA, so we only allocate space
2271 		 */
2272 		ASSERT(elsop & (ELS_OP_LOGO | ELS_OP_PLOGI));
2273 		icmd->icmd_flags |= ICMD_IMPLICIT;
2274 		if (elsop == ELS_OP_LOGO) {
2275 			/*
2276 			 * Handling implicit LOGO should dependent on as less
2277 			 * as resources. So a trick here.
2278 			 */
2279 			els->els_req_size = 1;
2280 			els->els_req_payload = cmd->cmd_fca_private;
2281 		} else {
2282 			els->els_req_alloc_size = els->els_req_size = 116;
2283 			els->els_resp_alloc_size = els->els_resp_size = 116;
2284 			els->els_req_payload = (uint8_t *)
2285 			    kmem_zalloc(els->els_req_size, KM_SLEEP);
2286 			els->els_resp_payload = (uint8_t *)
2287 			    kmem_zalloc(els->els_resp_size, KM_SLEEP);
2288 		}
2289 	} else {
2290 		/*
2291 		 * Allocate space for its request and response
2292 		 * Fill the request payload according to spec.
2293 		 */
2294 		switch (elsop) {
2295 		case ELS_OP_LOGO:
2296 			els->els_resp_alloc_size = els->els_resp_size = 4;
2297 			els->els_resp_payload = (uint8_t *)kmem_zalloc(
2298 			    els->els_resp_size, KM_SLEEP);
2299 			els->els_req_alloc_size = els->els_req_size = 16;
2300 			els->els_req_payload = (uint8_t *)kmem_zalloc(
2301 			    els->els_req_size, KM_SLEEP);
2302 			ptid = PORT_TO_IPORT(port)->iport_link_info.portid;
2303 			fct_value_to_netbuf(ptid, els->els_req_payload + 5, 3);
2304 			bcopy(port->port_pwwn, els->els_req_payload + 8, 8);
2305 			break;
2306 
2307 		case ELS_OP_RSCN:
2308 			els->els_resp_alloc_size = els->els_resp_size = 4;
2309 			els->els_resp_payload = (uint8_t *)kmem_zalloc(
2310 			    els->els_resp_size, KM_SLEEP);
2311 			els->els_req_size = els->els_req_alloc_size = 8;
2312 			els->els_req_payload = (uint8_t *)kmem_zalloc(
2313 			    els->els_req_size, KM_SLEEP);
2314 			els->els_req_payload[1] = 0x04;
2315 			els->els_req_payload[3] = 0x08;
2316 			els->els_req_payload[4] |= 0x80;
2317 			ptid = PORT_TO_IPORT(port)->iport_link_info.portid;
2318 			fct_value_to_netbuf(ptid, els->els_req_payload + 5, 3);
2319 			break;
2320 
2321 		case ELS_OP_PLOGI:
2322 			els->els_resp_alloc_size = els->els_resp_size = 116;
2323 			els->els_resp_payload = (uint8_t *)
2324 			    kmem_zalloc(els->els_resp_size, KM_SLEEP);
2325 			els->els_req_alloc_size = els->els_req_size = 116;
2326 			p = els->els_req_payload = (uint8_t *)
2327 			    kmem_zalloc(els->els_req_size, KM_SLEEP);
2328 			bcopy(port->port_pwwn, p + 20, 8);
2329 			bcopy(port->port_nwwn, p + 28, 8);
2330 
2331 			/*
2332 			 * Common service parameters
2333 			 */
2334 			p[0x04] = 0x09;		/* high version */
2335 			p[0x05] = 0x08;		/* low version */
2336 			p[0x06] = 0x00;		/* BB credit: 0x0065 */
2337 			p[0x07] = 0x65;
2338 
2339 			/* CI0: Continuously Increasing Offset - 1 */
2340 			/* RRO: Randomly Relative Offset - 0 */
2341 			/* VVV: Vendor Version Level - 0 */
2342 			/* N-F: N or F Port Payload Sender - 0 (N) */
2343 			/* BBM: BB Credit Management - 0 (Normal) */
2344 			p[0x08] = 0x80;
2345 			p[0x09] = 0x00;
2346 
2347 			/* Max RX size */
2348 			p[0x0A] = 0x08;
2349 			p[0x0B] = 0x00;
2350 
2351 			/* NPTCS: N Port Total Concurrent Sequences - 0x0000 */
2352 			p[0x0C] = 0x00;
2353 			p[0x0D] = 0x00;
2354 
2355 			/* ROIC: Relative Offset By Info - 0xFFFF */
2356 			p[0x0E] = 0xFF;
2357 			p[0x0F] = 0xFF;
2358 
2359 			/* EDTOV: Error Detect Timeout - 0x000007D0 */
2360 			p[0x10] = 0x00;
2361 			p[0x11] = 0x00;
2362 			p[0x12] = 0x07;
2363 			p[0x13] = 0xD0;
2364 
2365 			/*
2366 			 * Class-3 Parameters
2367 			 */
2368 			/* C3-VAL: Class 3 Value - 1 */
2369 			/* C3-XID: X_ID Reassignment - 0 */
2370 			/* C3-IPA: Initial Process Assignment */
2371 			/* C3-AI-DCC: Data compression capable */
2372 			/* C3-AI-DC-HB: Data compression history buffer size */
2373 			/* C3-AI-DCE: Data encrytion capable */
2374 			/* C3-AI-CSC: Clock synchronization capable */
2375 			/* C3-ErrPol: Error pliciy */
2376 			/* C3-CatSeq: Information Cat. Per Sequence */
2377 			/* C3-AR-DCC: */
2378 			/* C3-AR-DC-HB: */
2379 			/* C3-AR-DCE: */
2380 			/* C3-AR-CSC */
2381 			p[0x44] = 0x80;
2382 			p[0x45] = 0x00;
2383 			p[0x46] = 0x00;
2384 			p[0x47] = 0x00;
2385 			p[0x48] = 0x00;
2386 			p[0x49] = 0x00;
2387 
2388 			/* C3-RxSize: Class 3 receive data size */
2389 			p[0x4A] = 0x08;
2390 			p[0x4B] = 0x00;
2391 
2392 			/* C3-ConSeq: Class 3 Concourrent sequences */
2393 			p[0x4C] = 0x00;
2394 			p[0x4D] = 0xFF;
2395 
2396 			/* C3-OSPE: Class 3 open sequence per exchange */
2397 			p[0x50] = 0x00;
2398 			p[0x51] = 0x01;
2399 
2400 			break;
2401 
2402 		case ELS_OP_SCR:
2403 			els->els_resp_alloc_size = els->els_resp_size = 4;
2404 			els->els_resp_payload = (uint8_t *)
2405 			    kmem_zalloc(els->els_resp_size, KM_SLEEP);
2406 			els->els_req_alloc_size = els->els_req_size = 8;
2407 			p = els->els_req_payload = (uint8_t *)
2408 			    kmem_zalloc(els->els_req_size, KM_SLEEP);
2409 			p[7] = FC_SCR_FULL_REGISTRATION;
2410 			break;
2411 		case ELS_OP_RLS:
2412 			els->els_resp_alloc_size = els->els_resp_size = 28;
2413 			els->els_resp_payload = (uint8_t *)
2414 			    kmem_zalloc(els->els_resp_size, KM_SLEEP);
2415 			els->els_req_alloc_size = els->els_req_size = 8;
2416 			p = els->els_req_payload = (uint8_t *)
2417 			    kmem_zalloc(els->els_req_size, KM_SLEEP);
2418 			ptid = PORT_TO_IPORT(port)->iport_link_info.portid;
2419 			fct_value_to_netbuf(ptid, els->els_req_payload + 5, 3);
2420 			break;
2421 
2422 		default:
2423 			ASSERT(0);
2424 		}
2425 	}
2426 
2427 	els->els_req_payload[0] = elsop;
2428 	return (cmd);
2429 }
2430 
2431 fct_cmd_t *
2432 fct_create_solct(fct_local_port_t *port, fct_remote_port_t *query_rp,
2433     uint16_t ctop, fct_icmd_cb_t icmdcb)
2434 {
2435 	fct_cmd_t		*cmd	 = NULL;
2436 	fct_i_cmd_t		*icmd	 = NULL;
2437 	fct_sol_ct_t		*ct	 = NULL;
2438 	uint8_t			*p	 = NULL;
2439 	fct_i_remote_port_t	*irp	 = NULL;
2440 	fct_i_local_port_t	*iport	 = NULL;
2441 	char			*nname	 = NULL;
2442 	int			 namelen = 0;
2443 
2444 	/*
2445 	 * Allocate space
2446 	 */
2447 	cmd = fct_alloc(FCT_STRUCT_CMD_SOL_CT,
2448 	    port->port_fca_sol_ct_private_size, 0);
2449 	if (!cmd) {
2450 		return (NULL);
2451 	}
2452 
2453 	/*
2454 	 * We should have PLOGIed to the name server (0xFFFFFC)
2455 	 * Caution: this irp is not query_rp->rp_fct_private.
2456 	 */
2457 	irp = fct_portid_to_portptr((fct_i_local_port_t *)
2458 	    port->port_fct_private, FS_NAME_SERVER);
2459 	if (irp == NULL) {
2460 		stmf_trace(PORT_TO_IPORT(port)->iport_alias,
2461 		    "fct_create_solct: Must PLOGI name server first");
2462 		fct_free(cmd);
2463 		return (NULL);
2464 	}
2465 
2466 	cmd->cmd_port	   = port;
2467 	cmd->cmd_rp	   = irp->irp_rp;
2468 	cmd->cmd_rp_handle = irp->irp_rp->rp_handle;
2469 	cmd->cmd_rportid   = irp->irp_rp->rp_id;
2470 	cmd->cmd_lportid   = (PORT_TO_IPORT(port))->iport_link_info.portid;
2471 	cmd->cmd_oxid	   = PTR2INT(cmd, uint16_t);
2472 	cmd->cmd_rxid	   = 0xFFFF;
2473 	cmd->cmd_handle	   = 0;
2474 	icmd		   = CMD_TO_ICMD(cmd);
2475 	ct		   = ICMD_TO_CT(icmd);
2476 	icmd->icmd_cb	   = icmdcb;
2477 	iport		   = ICMD_TO_IPORT(icmd);
2478 
2479 	switch (ctop) {
2480 	case NS_GSNN_NN:
2481 		/*
2482 		 * Allocate max space for its sybolic name
2483 		 */
2484 		ct->ct_resp_alloc_size = ct->ct_resp_size = 272;
2485 		ct->ct_resp_payload = (uint8_t *)kmem_zalloc(ct->ct_resp_size,
2486 		    KM_SLEEP);
2487 
2488 		ct->ct_req_size = ct->ct_req_alloc_size = 24;
2489 		p = ct->ct_req_payload = (uint8_t *)kmem_zalloc(ct->ct_req_size,
2490 		    KM_SLEEP);
2491 
2492 		bcopy(query_rp->rp_nwwn, p + 16, 8);
2493 		break;
2494 
2495 	case NS_RNN_ID:
2496 		ct->ct_resp_alloc_size = ct->ct_resp_size = 16;
2497 		ct->ct_resp_payload = (uint8_t *)kmem_zalloc(ct->ct_resp_size,
2498 		    KM_SLEEP);
2499 		ct->ct_req_size = ct->ct_req_alloc_size = 28;
2500 		p = ct->ct_req_payload = (uint8_t *)kmem_zalloc(ct->ct_req_size,
2501 		    KM_SLEEP);
2502 
2503 		/*
2504 		 * Port Identifier
2505 		 */
2506 		p[17] = (iport->iport_link_info.portid >> 16) & 0xFF;
2507 		p[18] = (iport->iport_link_info.portid >>  8) & 0xFF;
2508 		p[19] = (iport->iport_link_info.portid >>  0) & 0xFF;
2509 
2510 		/*
2511 		 * Node Name
2512 		 */
2513 		bcopy(port->port_nwwn, p + 20, 8);
2514 		break;
2515 
2516 	case NS_RCS_ID:
2517 		ct->ct_resp_alloc_size = ct->ct_resp_size = 16;
2518 		ct->ct_resp_payload = (uint8_t *)kmem_zalloc(ct->ct_resp_size,
2519 		    KM_SLEEP);
2520 		ct->ct_req_size = ct->ct_req_alloc_size = 24;
2521 		p = ct->ct_req_payload = (uint8_t *)kmem_zalloc(ct->ct_req_size,
2522 		    KM_SLEEP);
2523 
2524 		/*
2525 		 * Port Identifier
2526 		 */
2527 		p[17] = (iport->iport_link_info.portid >> 16) & 0xFF;
2528 		p[18] = (iport->iport_link_info.portid >>  8) & 0xFF;
2529 		p[19] = (iport->iport_link_info.portid >>  0) & 0xFF;
2530 
2531 		/*
2532 		 * Class of Service
2533 		 */
2534 		*(p + 23) = FC_NS_CLASS3;
2535 		break;
2536 
2537 	case NS_RFT_ID:
2538 		ct->ct_resp_alloc_size = ct->ct_resp_size = 16;
2539 		ct->ct_resp_payload = (uint8_t *)kmem_zalloc(ct->ct_resp_size,
2540 		    KM_SLEEP);
2541 		ct->ct_req_size = ct->ct_req_alloc_size = 52;
2542 		p = ct->ct_req_payload = (uint8_t *)kmem_zalloc(ct->ct_req_size,
2543 		    KM_SLEEP);
2544 
2545 		/*
2546 		 * Port Identifier
2547 		 */
2548 		p[17] = (iport->iport_link_info.portid >> 16) & 0xFF;
2549 		p[18] = (iport->iport_link_info.portid >>  8) & 0xFF;
2550 		p[19] = (iport->iport_link_info.portid >>  0) & 0xFF;
2551 
2552 		/*
2553 		 * FC-4 Protocol Types
2554 		 */
2555 		*(p + 22) = 0x1;	/* 0x100 */
2556 		break;
2557 
2558 	case NS_RSPN_ID:
2559 		/*
2560 		 * If we get here, port->port_sym_port_name is always not NULL.
2561 		 */
2562 		ASSERT(port->port_sym_port_name);
2563 		namelen = strlen(port->port_sym_port_name);
2564 		ct->ct_resp_alloc_size = ct->ct_resp_size = 16;
2565 		ct->ct_resp_payload = (uint8_t *)kmem_zalloc(ct->ct_resp_size,
2566 		    KM_SLEEP);
2567 		ct->ct_req_size = ct->ct_req_alloc_size =
2568 		    (21 + namelen + 3) & ~3;
2569 		p = ct->ct_req_payload = (uint8_t *)kmem_zalloc(ct->ct_req_size,
2570 		    KM_SLEEP);
2571 
2572 		/*
2573 		 * Port Identifier
2574 		 */
2575 		p[17] = (iport->iport_link_info.portid >> 16) & 0xFF;
2576 		p[18] = (iport->iport_link_info.portid >>  8) & 0xFF;
2577 		p[19] = (iport->iport_link_info.portid >>  0) & 0xFF;
2578 
2579 		/*
2580 		 * String length
2581 		 */
2582 		p[20] = namelen;
2583 
2584 		/*
2585 		 * Symbolic port name
2586 		 */
2587 		bcopy(port->port_sym_port_name, p + 21, ct->ct_req_size - 21);
2588 		break;
2589 
2590 	case NS_RSNN_NN:
2591 		namelen = port->port_sym_node_name == NULL ?
2592 		    strlen(utsname.nodename) :
2593 		    strlen(port->port_sym_node_name);
2594 		nname = port->port_sym_node_name == NULL ?
2595 		    utsname.nodename : port->port_sym_node_name;
2596 
2597 		ct->ct_resp_alloc_size = ct->ct_resp_size = 16;
2598 		ct->ct_resp_payload = (uint8_t *)kmem_zalloc(ct->ct_resp_size,
2599 		    KM_SLEEP);
2600 		ct->ct_req_size = ct->ct_req_alloc_size =
2601 		    (25 + namelen + 3) & ~3;
2602 		p = ct->ct_req_payload = (uint8_t *)kmem_zalloc(ct->ct_req_size,
2603 		    KM_SLEEP);
2604 
2605 		/*
2606 		 * Node name
2607 		 */
2608 		bcopy(port->port_nwwn, p + 16, 8);
2609 
2610 		/*
2611 		 * String length
2612 		 */
2613 		p[24] = namelen;
2614 
2615 		/*
2616 		 * Symbolic node name
2617 		 */
2618 		bcopy(nname, p + 25, ct->ct_req_size - 25);
2619 		break;
2620 
2621 	case NS_GSPN_ID:
2622 		ct->ct_resp_alloc_size = ct->ct_resp_size = 272;
2623 		ct->ct_resp_payload = (uint8_t *)kmem_zalloc(ct->ct_resp_size,
2624 		    KM_SLEEP);
2625 		ct->ct_req_size = ct->ct_req_alloc_size = 20;
2626 		p = ct->ct_req_payload = (uint8_t *)kmem_zalloc(ct->ct_req_size,
2627 		    KM_SLEEP);
2628 		/*
2629 		 * Port Identifier
2630 		 */
2631 		p[17] = (query_rp->rp_id >> 16) & 0xFF;
2632 		p[18] = (query_rp->rp_id >>  8) & 0xFF;
2633 		p[19] = (query_rp->rp_id >>  0) & 0xFF;
2634 		break;
2635 
2636 	case NS_GCS_ID:
2637 		ct->ct_resp_alloc_size = ct->ct_resp_size = 20;
2638 		ct->ct_resp_payload = (uint8_t *)kmem_zalloc(ct->ct_resp_size,
2639 		    KM_SLEEP);
2640 		ct->ct_req_size = ct->ct_req_alloc_size = 20;
2641 		p = ct->ct_req_payload = (uint8_t *)kmem_zalloc(ct->ct_req_size,
2642 		    KM_SLEEP);
2643 		/*
2644 		 * Port Identifier
2645 		 */
2646 		p[17] = (query_rp->rp_id >> 16) & 0xFF;
2647 		p[18] = (query_rp->rp_id >>  8) & 0xFF;
2648 		p[19] = (query_rp->rp_id >>  0) & 0xFF;
2649 		break;
2650 
2651 	case NS_GFT_ID:
2652 		ct->ct_resp_alloc_size = ct->ct_resp_size = 48;
2653 		ct->ct_resp_payload = (uint8_t *)kmem_zalloc(ct->ct_resp_size,
2654 		    KM_SLEEP);
2655 		ct->ct_req_size = ct->ct_req_alloc_size = 20;
2656 		p = ct->ct_req_payload = (uint8_t *)kmem_zalloc(ct->ct_req_size,
2657 		    KM_SLEEP);
2658 		/*
2659 		 * Port Identifier
2660 		 */
2661 		p[17] = (query_rp->rp_id >> 16) & 0xFF;
2662 		p[18] = (query_rp->rp_id >>  8) & 0xFF;
2663 		p[19] = (query_rp->rp_id >>  0) & 0xFF;
2664 		break;
2665 
2666 	case NS_GID_PN:
2667 		ct->ct_resp_alloc_size = ct->ct_resp_size = 20;
2668 		ct->ct_resp_payload = (uint8_t *)kmem_zalloc(ct->ct_resp_size,
2669 		    KM_SLEEP);
2670 
2671 		ct->ct_req_size = ct->ct_req_alloc_size = 24;
2672 		p = ct->ct_req_payload = (uint8_t *)kmem_zalloc(ct->ct_req_size,
2673 		    KM_SLEEP);
2674 
2675 		bcopy(query_rp->rp_pwwn, p + 16, 8);
2676 		break;
2677 
2678 	default:
2679 		/* CONSTCOND */
2680 		ASSERT(0);
2681 	}
2682 
2683 	FCT_FILL_CTIU_PREAMPLE(p, ctop);
2684 	return (cmd);
2685 }
2686 
2687 /*
2688  * Cmd can only be solicited CT/ELS. They will be dispatched to the discovery
2689  * queue eventually too.
2690  * We queue solicited cmds here to track solicited cmds and to take full use
2691  * of single thread mechanism.
2692  * But in current implmentation, we don't use  this mechanism on SOL_CT, PLOGI.
2693  * To avoid to interrupt current flow, ICMD_IN_SOLCMD_QUEUE is used here.
2694  */
2695 void
2696 fct_post_to_solcmd_queue(fct_local_port_t *port, fct_cmd_t *cmd)
2697 {
2698 	fct_i_local_port_t	*iport	= (fct_i_local_port_t *)
2699 	    port->port_fct_private;
2700 	fct_i_cmd_t *icmd		= (fct_i_cmd_t *)cmd->cmd_fct_private;
2701 
2702 	mutex_enter(&iport->iport_worker_lock);
2703 	icmd->icmd_solcmd_next = iport->iport_solcmd_queue;
2704 	iport->iport_solcmd_queue = icmd;
2705 	atomic_or_32(&icmd->icmd_flags, ICMD_IN_SOLCMD_QUEUE | ICMD_SOLCMD_NEW);
2706 	if (IS_WORKER_SLEEPING(iport)) {
2707 		cv_signal(&iport->iport_worker_cv);
2708 	}
2709 	mutex_exit(&iport->iport_worker_lock);
2710 }
2711 
2712 /* ARGSUSED */
2713 void
2714 fct_event_handler(stmf_local_port_t *lport, int eventid, void *arg,
2715     uint32_t flags)
2716 {
2717 	fct_local_port_t	*port  = (fct_local_port_t *)
2718 	    lport->lport_port_private;
2719 	fct_i_local_port_t	*iport = (fct_i_local_port_t *)
2720 	    port->port_fct_private;
2721 	stmf_scsi_session_t	*ss;
2722 	fct_i_remote_port_t	*irp;
2723 
2724 	switch (eventid) {
2725 	case LPORT_EVENT_INITIAL_LUN_MAPPED:
2726 		ss = (stmf_scsi_session_t *)arg;
2727 		irp = (fct_i_remote_port_t *)ss->ss_port_private;
2728 		stmf_trace(iport->iport_alias,
2729 		    "Initial LUN mapped to session ss-%p, irp-%p", ss, irp);
2730 		break;
2731 
2732 	default:
2733 		stmf_trace(iport->iport_alias,
2734 		    "Unknown event received, %d", eventid);
2735 	}
2736 }
2737 
2738 void
2739 fct_send_cmd_done(fct_cmd_t *cmd, fct_status_t s, uint32_t ioflags)
2740 {
2741 	/* XXX For now just call send_resp_done() */
2742 	fct_send_response_done(cmd, s, ioflags);
2743 }
2744 
2745 void
2746 fct_cmd_fca_aborted(fct_cmd_t *cmd, fct_status_t s, uint32_t ioflags)
2747 {
2748 	fct_i_cmd_t		*icmd = (fct_i_cmd_t *)cmd->cmd_fct_private;
2749 	char			info[160];
2750 	unsigned long long	st;
2751 
2752 	st = s;	/* To make gcc happy */
2753 	ASSERT(icmd->icmd_flags & ICMD_BEING_ABORTED);
2754 	if ((((s != FCT_ABORT_SUCCESS) && (s != FCT_NOT_FOUND))) ||
2755 	    ((ioflags & FCT_IOF_FCA_DONE) == 0)) {
2756 		(void) snprintf(info, 160, "fct_cmd_fca_aborted: cmd-%p, "
2757 		    "s-%llx, iofalgs-%x", (void *)cmd, st, ioflags);
2758 		info[159] = 0;
2759 		(void) fct_port_shutdown(cmd->cmd_port,
2760 		    STMF_RFLAG_FATAL_ERROR | STMF_RFLAG_RESET, info);
2761 		return;
2762 	}
2763 
2764 	atomic_and_32(&icmd->icmd_flags, ~ICMD_KNOWN_TO_FCA);
2765 	/* For non FCP Rest of the work is done by the terminator */
2766 	/* For FCP stuff just call stmf */
2767 	if (cmd->cmd_type == FCT_CMD_FCP_XCHG) {
2768 		stmf_task_lport_aborted((scsi_task_t *)cmd->cmd_specific,
2769 		    s, STMF_IOF_LPORT_DONE);
2770 	}
2771 }
2772 
2773 /*
2774  * FCA drivers will use it, when they want to abort some FC transactions
2775  * due to lack of resource.
2776  */
2777 uint16_t
2778 fct_get_rp_handle(fct_local_port_t *port, uint32_t rportid)
2779 {
2780 	fct_i_remote_port_t	*irp;
2781 
2782 	irp = fct_portid_to_portptr(
2783 	    (fct_i_local_port_t *)(port->port_fct_private), rportid);
2784 	if (irp == NULL) {
2785 		return (0xFFFF);
2786 	} else {
2787 		return (irp->irp_rp->rp_handle);
2788 	}
2789 }
2790 
2791 fct_cmd_t *
2792 fct_handle_to_cmd(fct_local_port_t *port, uint32_t fct_handle)
2793 {
2794 	fct_cmd_slot_t *slot;
2795 	uint16_t ndx;
2796 
2797 	if (!CMD_HANDLE_VALID(fct_handle))
2798 		return (NULL);
2799 	if ((ndx = CMD_HANDLE_SLOT_INDEX(fct_handle)) >= port->port_max_xchges)
2800 		return (NULL);
2801 
2802 	slot = &((fct_i_local_port_t *)port->port_fct_private)->iport_cmd_slots[
2803 	    ndx];
2804 
2805 	if ((slot->slot_uniq_cntr | 0x80) != (fct_handle >> 24))
2806 		return (NULL);
2807 	return (slot->slot_cmd->icmd_cmd);
2808 }
2809 
2810 void
2811 fct_queue_scsi_task_for_termination(fct_cmd_t *cmd, fct_status_t s)
2812 {
2813 	fct_i_cmd_t *icmd = (fct_i_cmd_t *)cmd->cmd_fct_private;
2814 
2815 	uint32_t old, new;
2816 
2817 	do {
2818 		old = icmd->icmd_flags;
2819 		if ((old & (ICMD_BEING_ABORTED | ICMD_KNOWN_TO_FCA)) !=
2820 		    ICMD_KNOWN_TO_FCA)
2821 			return;
2822 		new = old | ICMD_BEING_ABORTED;
2823 	} while (atomic_cas_32(&icmd->icmd_flags, old, new) != old);
2824 	stmf_abort(STMF_QUEUE_TASK_ABORT, (scsi_task_t *)cmd->cmd_specific,
2825 	    s, NULL);
2826 }
2827 
2828 void
2829 fct_fill_abts_acc(fct_cmd_t *cmd)
2830 {
2831 	fct_rcvd_abts_t *abts = (fct_rcvd_abts_t *)cmd->cmd_specific;
2832 	uint8_t *p;
2833 
2834 	abts->abts_resp_rctl = BLS_OP_BA_ACC;
2835 	p = abts->abts_resp_payload;
2836 	bzero(p, 12);
2837 	*((uint16_t *)(p+4)) = BE_16(cmd->cmd_oxid);
2838 	*((uint16_t *)(p+6)) = BE_16(cmd->cmd_rxid);
2839 	p[10] = p[11] = 0xff;
2840 }
2841 
2842 void
2843 fct_handle_rcvd_abts(fct_cmd_t *cmd)
2844 {
2845 	char			info[80];
2846 	fct_local_port_t	*port = cmd->cmd_port;
2847 	fct_i_local_port_t	*iport =
2848 	    (fct_i_local_port_t *)port->port_fct_private;
2849 	fct_i_cmd_t		*icmd = (fct_i_cmd_t *)cmd->cmd_fct_private;
2850 	fct_i_remote_port_t	*irp;
2851 	fct_cmd_t		*c = NULL;
2852 	fct_i_cmd_t		*ic = NULL;
2853 	int			found = 0;
2854 	int			i;
2855 
2856 	icmd->icmd_start_time = ddi_get_lbolt();
2857 	icmd->icmd_flags |= ICMD_KNOWN_TO_FCA;
2858 
2859 	rw_enter(&iport->iport_lock, RW_WRITER);
2860 	/* Make sure local port is sane */
2861 	if ((iport->iport_link_state & S_LINK_ONLINE) == 0) {
2862 		rw_exit(&iport->iport_lock);
2863 		stmf_trace(iport->iport_alias, "ABTS not posted becasue"
2864 		    "port state was %x", iport->iport_link_state);
2865 		fct_queue_cmd_for_termination(cmd, FCT_LOCAL_PORT_OFFLINE);
2866 		return;
2867 	}
2868 
2869 	if (cmd->cmd_rp_handle == FCT_HANDLE_NONE)
2870 		irp = fct_portid_to_portptr(iport, cmd->cmd_rportid);
2871 	else if (cmd->cmd_rp_handle < port->port_max_logins)
2872 		irp = iport->iport_rp_slots[cmd->cmd_rp_handle];
2873 	else
2874 		irp = NULL;
2875 	if (irp == NULL) {
2876 		/* XXX Throw a logout to the initiator */
2877 		rw_exit(&iport->iport_lock);
2878 		stmf_trace(iport->iport_alias, "ABTS received from"
2879 		    " %x without a session", cmd->cmd_rportid);
2880 		fct_queue_cmd_for_termination(cmd, FCT_NOT_LOGGED_IN);
2881 		return;
2882 	}
2883 
2884 	DTRACE_FC_3(abts__receive,
2885 	    fct_cmd_t, cmd,
2886 	    fct_local_port_t, port,
2887 	    fct_i_remote_port_t, irp);
2888 
2889 	cmd->cmd_rp = irp->irp_rp;
2890 
2891 	/*
2892 	 * No need to allocate an xchg resource. ABTSes use the same
2893 	 * xchg resource as the cmd they are aborting.
2894 	 */
2895 	rw_enter(&irp->irp_lock, RW_WRITER);
2896 	mutex_enter(&iport->iport_worker_lock);
2897 	/* Lets find the command first */
2898 	for (i = 0; i < port->port_max_xchges; i++) {
2899 		if ((ic = iport->iport_cmd_slots[i].slot_cmd) == NULL)
2900 			continue;
2901 		if ((ic->icmd_flags & ICMD_KNOWN_TO_FCA) == 0)
2902 			continue;
2903 		c = ic->icmd_cmd;
2904 		if (!CMD_HANDLE_VALID(c->cmd_handle))
2905 			continue;
2906 		if ((c->cmd_rportid != cmd->cmd_rportid) ||
2907 		    (c->cmd_oxid != cmd->cmd_oxid))
2908 			continue;
2909 		/* Found the command */
2910 		found = 1;
2911 		break;
2912 	}
2913 	if (!found) {
2914 		mutex_exit(&iport->iport_worker_lock);
2915 		rw_exit(&irp->irp_lock);
2916 		rw_exit(&iport->iport_lock);
2917 		/* Dont even bother queueing it. Just respond */
2918 		fct_fill_abts_acc(cmd);
2919 		if (port->port_send_cmd_response(cmd,
2920 		    FCT_IOF_FORCE_FCA_DONE) != FCT_SUCCESS) {
2921 			/*
2922 			 * XXX Throw HBA fatal error event
2923 			 * Later shutdown svc will terminate the ABTS in the end
2924 			 */
2925 			(void) snprintf(info, 80,
2926 			    "fct_handle_rcvd_abts: iport-%p, "
2927 			    "ABTS_ACC port_send_cmd_response failed",
2928 			    (void *)iport);
2929 			info[79] = 0;
2930 			(void) fct_port_shutdown(iport->iport_port,
2931 			    STMF_RFLAG_FATAL_ERROR | STMF_RFLAG_RESET, info);
2932 		} else {
2933 			fct_cmd_free(cmd);
2934 		}
2935 		return;
2936 	}
2937 
2938 	/* Check if this an abts retry */
2939 	if (c->cmd_link && (ic->icmd_flags & ICMD_ABTS_RECEIVED)) {
2940 		/* Kill this abts. */
2941 		fct_q_for_termination_lock_held(iport, icmd, FCT_ABORTED);
2942 		if (IS_WORKER_SLEEPING(iport))
2943 			cv_signal(&iport->iport_worker_cv);
2944 		mutex_exit(&iport->iport_worker_lock);
2945 		rw_exit(&irp->irp_lock);
2946 		rw_exit(&iport->iport_lock);
2947 		return;
2948 	}
2949 	c->cmd_link = cmd;
2950 	atomic_or_32(&ic->icmd_flags, ICMD_ABTS_RECEIVED);
2951 	cmd->cmd_link = c;
2952 	mutex_exit(&iport->iport_worker_lock);
2953 	rw_exit(&irp->irp_lock);
2954 	fct_queue_cmd_for_termination(c, FCT_ABTS_RECEIVED);
2955 	rw_exit(&iport->iport_lock);
2956 }
2957 
2958 void
2959 fct_queue_cmd_for_termination(fct_cmd_t *cmd, fct_status_t s)
2960 {
2961 	fct_local_port_t *port = cmd->cmd_port;
2962 	fct_i_local_port_t *iport = (fct_i_local_port_t *)
2963 	    port->port_fct_private;
2964 	fct_i_cmd_t *icmd = (fct_i_cmd_t *)cmd->cmd_fct_private;
2965 
2966 	if (cmd->cmd_type == FCT_CMD_FCP_XCHG) {
2967 		fct_queue_scsi_task_for_termination(cmd, s);
2968 		return;
2969 	}
2970 	mutex_enter(&iport->iport_worker_lock);
2971 	fct_q_for_termination_lock_held(iport, icmd, s);
2972 	if (IS_WORKER_SLEEPING(iport))
2973 		cv_signal(&iport->iport_worker_cv);
2974 	mutex_exit(&iport->iport_worker_lock);
2975 }
2976 
2977 /*
2978  * This function will not be called for SCSI CMDS
2979  */
2980 void
2981 fct_q_for_termination_lock_held(fct_i_local_port_t *iport, fct_i_cmd_t *icmd,
2982 		fct_status_t s)
2983 {
2984 	uint32_t old, new;
2985 	fct_i_cmd_t **ppicmd;
2986 
2987 	do {
2988 		old = icmd->icmd_flags;
2989 		if (old & ICMD_BEING_ABORTED)
2990 			return;
2991 		new = old | ICMD_BEING_ABORTED;
2992 	} while (atomic_cas_32(&icmd->icmd_flags, old, new) != old);
2993 
2994 	icmd->icmd_start_time = ddi_get_lbolt();
2995 	icmd->icmd_cmd->cmd_comp_status = s;
2996 
2997 	icmd->icmd_next = NULL;
2998 	for (ppicmd = &(iport->iport_abort_queue); *ppicmd != NULL;
2999 	    ppicmd = &((*ppicmd)->icmd_next))
3000 		;
3001 
3002 	*ppicmd = icmd;
3003 }
3004 
3005 /*
3006  * For those cmds, for which we called fca_abort but it has not yet completed,
3007  * reset the FCA_ABORT_CALLED flag, so that abort can be called again.
3008  * This is done after a FCA offline. The reason is that after offline, the
3009  * firmware is not running so abort will never complete. But if we call it
3010  * again, the FCA will detect that it is not offline and it will
3011  * not call the firmware at all. Most likely it will abort in a synchronous
3012  * manner i.e. return FCT_ABORT_SUCCESS or FCT_NOT_FOUND.
3013  */
3014 void
3015 fct_reset_flag_abort_called(fct_i_local_port_t *iport)
3016 {
3017 	fct_i_cmd_t *icmd;
3018 	uint32_t old, new;
3019 	int i, do_clear;
3020 
3021 	ASSERT(mutex_owned(&iport->iport_worker_lock));
3022 	mutex_exit(&iport->iport_worker_lock);
3023 	rw_enter(&iport->iport_lock, RW_WRITER);
3024 	mutex_enter(&iport->iport_worker_lock);
3025 
3026 	for (i = 0; i < iport->iport_port->port_max_xchges; i++) {
3027 		if (iport->iport_cmd_slots[i].slot_cmd == NULL)
3028 			continue;
3029 
3030 		icmd = iport->iport_cmd_slots[i].slot_cmd;
3031 
3032 		do {
3033 			old = new = icmd->icmd_flags;
3034 			if ((old & (ICMD_KNOWN_TO_FCA |
3035 			    ICMD_FCA_ABORT_CALLED)) == (ICMD_KNOWN_TO_FCA |
3036 			    ICMD_FCA_ABORT_CALLED)) {
3037 				new &= ~ICMD_FCA_ABORT_CALLED;
3038 				do_clear = 1;
3039 			} else {
3040 				do_clear = 0;
3041 				break;
3042 			}
3043 		} while (atomic_cas_32(&icmd->icmd_flags, old, new) != old);
3044 		if (do_clear &&
3045 		    (icmd->icmd_cmd->cmd_type == FCT_CMD_FCP_XCHG)) {
3046 			stmf_abort(STMF_REQUEUE_TASK_ABORT_LPORT,
3047 			    icmd->icmd_cmd->cmd_specific, 0, NULL);
3048 		}
3049 	}
3050 
3051 	rw_exit(&iport->iport_lock);
3052 }
3053 
3054 /*
3055  * Modify the irp_deregister_timer such that the ports start deregistering
3056  * quickly.
3057  */
3058 void
3059 fct_irp_deregister_speedup(fct_i_local_port_t *iport)
3060 {
3061 	fct_i_remote_port_t *irp;
3062 	int i;
3063 
3064 	if (!iport->iport_nrps)
3065 		return;
3066 
3067 	for (i = 0; i < rportid_table_size; i++) {
3068 		irp = iport->iport_rp_tb[i];
3069 		while (irp) {
3070 			irp->irp_deregister_timer = ddi_get_lbolt() - 1;
3071 			irp = irp->irp_next;
3072 		}
3073 	}
3074 }
3075 
3076 disc_action_t
3077 fct_handle_port_offline(fct_i_local_port_t *iport)
3078 {
3079 	if (iport->iport_offline_prstate == FCT_OPR_START) {
3080 		fct_reset_flag_abort_called(iport);
3081 		iport->iport_offline_prstate = FCT_OPR_CMD_CLEANUP_WAIT;
3082 		/* fct_ctl has already submitted a link offline event */
3083 		return (DISC_ACTION_DELAY_RESCAN);
3084 	}
3085 	if (iport->iport_offline_prstate == FCT_OPR_CMD_CLEANUP_WAIT) {
3086 		if (iport->iport_link_state != PORT_STATE_LINK_DOWN)
3087 			return (DISC_ACTION_DELAY_RESCAN);
3088 		/*
3089 		 * All I/Os have been killed at this time. Lets speedup
3090 		 * the port deregister process.
3091 		 */
3092 		mutex_exit(&iport->iport_worker_lock);
3093 		rw_enter(&iport->iport_lock, RW_WRITER);
3094 		fct_irp_deregister_speedup(iport);
3095 		rw_exit(&iport->iport_lock);
3096 		mutex_enter(&iport->iport_worker_lock);
3097 		iport->iport_offline_prstate = FCT_OPR_INT_CLEANUP_WAIT;
3098 		return (DISC_ACTION_RESCAN);
3099 	}
3100 	if (iport->iport_offline_prstate == FCT_OPR_INT_CLEANUP_WAIT) {
3101 		stmf_change_status_t st;
3102 
3103 		if (iport->iport_solcmd_queue) {
3104 			return (DISC_ACTION_DELAY_RESCAN);
3105 		}
3106 
3107 		if (iport->iport_nrps) {
3108 			/*
3109 			 * A port logout may have gone when implicit logo all
3110 			 * was retried. So do the port speedup again here.
3111 			 */
3112 			mutex_exit(&iport->iport_worker_lock);
3113 			rw_enter(&iport->iport_lock, RW_WRITER);
3114 			fct_irp_deregister_speedup(iport);
3115 			rw_exit(&iport->iport_lock);
3116 			mutex_enter(&iport->iport_worker_lock);
3117 			return (DISC_ACTION_DELAY_RESCAN);
3118 		}
3119 
3120 		if (iport->iport_event_head != NULL) {
3121 			return (DISC_ACTION_DELAY_RESCAN);
3122 		}
3123 
3124 		st.st_completion_status = STMF_SUCCESS;
3125 		st.st_additional_info = NULL;
3126 		iport->iport_offline_prstate = FCT_OPR_DONE;
3127 		iport->iport_state = FCT_STATE_OFFLINE;
3128 		mutex_exit(&iport->iport_worker_lock);
3129 		(void) stmf_ctl(STMF_CMD_LPORT_OFFLINE_COMPLETE,
3130 		    iport->iport_port->port_lport, &st);
3131 		mutex_enter(&iport->iport_worker_lock);
3132 		return (DISC_ACTION_DELAY_RESCAN);
3133 	}
3134 
3135 	/* NOTREACHED */
3136 	return (0);
3137 }
3138 
3139 /*
3140  * See stmf.h for information on rflags. Additional info is just a text
3141  * description of the reason for this call. Additional_info can be NULL.
3142  * Also the caller can declare additional info on the stack. stmf_ctl
3143  * makes a copy of it before returning.
3144  */
3145 fct_status_t
3146 fct_port_initialize(fct_local_port_t *port, uint32_t rflags,
3147 				char *additional_info)
3148 {
3149 	stmf_state_change_info_t st;
3150 
3151 	st.st_rflags = rflags;
3152 	st.st_additional_info = additional_info;
3153 	stmf_trace(NULL, "fct_port_initialize: port-%p, %s", port,
3154 	    additional_info? additional_info : "no more information");
3155 	return (stmf_ctl(STMF_CMD_LPORT_ONLINE, port->port_lport, &st));
3156 }
3157 
3158 fct_status_t
3159 fct_port_shutdown(fct_local_port_t *port, uint32_t rflags,
3160 				char *additional_info)
3161 {
3162 	stmf_state_change_info_t st;
3163 
3164 	st.st_rflags = rflags;
3165 	st.st_additional_info = additional_info;
3166 	stmf_trace(NULL, "fct_port_shutdown: port-%p, %s", port,
3167 	    additional_info? additional_info : "no more information");
3168 	return (stmf_ctl(STMF_CMD_LPORT_OFFLINE, port->port_lport, &st));
3169 }
3170 
3171 /*
3172  * Called by worker thread. The aim is to terminate the command
3173  * using whatever means it takes.
3174  * Called with worker lock held.
3175  */
3176 disc_action_t
3177 fct_cmd_terminator(fct_i_local_port_t *iport)
3178 {
3179 	char			info[80];
3180 	clock_t			endtime;
3181 	fct_i_cmd_t		**ppicmd;
3182 	fct_i_cmd_t		*icmd;
3183 	fct_cmd_t		*cmd;
3184 	fct_local_port_t	*port = iport->iport_port;
3185 	disc_action_t		ret = DISC_ACTION_NO_WORK;
3186 	fct_status_t		abort_ret;
3187 	int			fca_done, fct_done, cmd_implicit = 0;
3188 	int			flags;
3189 	unsigned long long	st;
3190 
3191 	/* Lets Limit each run to 20ms max. */
3192 	endtime = ddi_get_lbolt() + drv_usectohz(20000);
3193 
3194 	/* Start from where we left off last time */
3195 	if (iport->iport_ppicmd_term) {
3196 		ppicmd = iport->iport_ppicmd_term;
3197 		iport->iport_ppicmd_term = NULL;
3198 	} else {
3199 		ppicmd = &iport->iport_abort_queue;
3200 	}
3201 
3202 	/*
3203 	 * Once a command gets on discovery queue, this is the only thread
3204 	 * which can access it. So no need for the lock here.
3205 	 */
3206 	mutex_exit(&iport->iport_worker_lock);
3207 
3208 	while ((icmd = *ppicmd) != NULL) {
3209 		cmd = icmd->icmd_cmd;
3210 
3211 		/* Always remember that cmd->cmd_rp can be NULL */
3212 		if ((icmd->icmd_flags & (ICMD_KNOWN_TO_FCA |
3213 		    ICMD_FCA_ABORT_CALLED)) == ICMD_KNOWN_TO_FCA) {
3214 			atomic_or_32(&icmd->icmd_flags, ICMD_FCA_ABORT_CALLED);
3215 			if (CMD_HANDLE_VALID(cmd->cmd_handle))
3216 				flags = 0;
3217 			else
3218 				flags = FCT_IOF_FORCE_FCA_DONE;
3219 			abort_ret = port->port_abort_cmd(port, cmd, flags);
3220 			if ((abort_ret != FCT_SUCCESS) &&
3221 			    (abort_ret != FCT_ABORT_SUCCESS) &&
3222 			    (abort_ret != FCT_NOT_FOUND)) {
3223 				if (flags & FCT_IOF_FORCE_FCA_DONE) {
3224 					/*
3225 					 * XXX trigger port fatal,
3226 					 * Abort the termination, and shutdown
3227 					 * svc will trigger fct_cmd_termination
3228 					 * again.
3229 					 */
3230 					(void) snprintf(info, 80,
3231 					    "fct_cmd_terminator:"
3232 					    " iport-%p, port_abort_cmd with "
3233 					    "FORCE_FCA_DONE failed",
3234 					    (void *)iport);
3235 					info[79] = 0;
3236 					(void) fct_port_shutdown(
3237 					    iport->iport_port,
3238 					    STMF_RFLAG_FATAL_ERROR |
3239 					    STMF_RFLAG_RESET, info);
3240 
3241 					mutex_enter(&iport->iport_worker_lock);
3242 					iport->iport_ppicmd_term = ppicmd;
3243 					return (DISC_ACTION_DELAY_RESCAN);
3244 				}
3245 				atomic_and_32(&icmd->icmd_flags,
3246 				    ~ICMD_FCA_ABORT_CALLED);
3247 			} else if ((flags & FCT_IOF_FORCE_FCA_DONE) ||
3248 			    (abort_ret == FCT_ABORT_SUCCESS) ||
3249 			    (abort_ret == FCT_NOT_FOUND)) {
3250 				atomic_and_32(&icmd->icmd_flags,
3251 				    ~ICMD_KNOWN_TO_FCA);
3252 			}
3253 			ret |= DISC_ACTION_DELAY_RESCAN;
3254 		} else if (icmd->icmd_flags & ICMD_IMPLICIT) {
3255 			if (cmd->cmd_type == FCT_CMD_SOL_ELS)
3256 				cmd->cmd_comp_status = FCT_ABORTED;
3257 			atomic_or_32(&icmd->icmd_flags, ICMD_FCA_ABORT_CALLED);
3258 			cmd_implicit = 1;
3259 		}
3260 		if ((icmd->icmd_flags & ICMD_KNOWN_TO_FCA) == 0)
3261 			fca_done = 1;
3262 		else
3263 			fca_done = 0;
3264 		if ((icmd->icmd_flags & ICMD_IN_IRP_QUEUE) == 0)
3265 			fct_done = 1;
3266 		else
3267 			fct_done = 0;
3268 		if ((fca_done || cmd_implicit) && fct_done) {
3269 			mutex_enter(&iport->iport_worker_lock);
3270 			ASSERT(*ppicmd == icmd);
3271 			*ppicmd = (*ppicmd)->icmd_next;
3272 			mutex_exit(&iport->iport_worker_lock);
3273 			if ((cmd->cmd_type == FCT_CMD_RCVD_ELS) ||
3274 			    (cmd->cmd_type == FCT_CMD_RCVD_ABTS)) {
3275 				/* Free the cmd */
3276 				fct_cmd_free(cmd);
3277 			} else if (cmd->cmd_type == FCT_CMD_SOL_ELS) {
3278 				fct_handle_sol_els_completion(iport, icmd);
3279 				if (icmd->icmd_flags & ICMD_IMPLICIT) {
3280 					if (IS_LOGO_ELS(icmd)) {
3281 						/* IMPLICIT LOGO is special */
3282 						fct_cmd_free(cmd);
3283 					}
3284 				}
3285 			} else if (cmd->cmd_type == FCT_CMD_SOL_CT) {
3286 				fct_sol_ct_t *ct = ICMD_TO_CT(icmd);
3287 
3288 				/* Tell the caller that we are done */
3289 				atomic_or_32(&icmd->icmd_flags,
3290 				    ICMD_CMD_COMPLETE);
3291 				if (fct_netbuf_to_value(
3292 				    ct->ct_req_payload + 8, 2) == NS_GID_PN) {
3293 					fct_i_remote_port_t *irp;
3294 
3295 					rw_enter(&iport->iport_lock, RW_READER);
3296 					irp = fct_lookup_irp_by_portwwn(iport,
3297 					    ct->ct_req_payload + 16);
3298 
3299 					if (irp) {
3300 						atomic_and_32(&irp->irp_flags,
3301 						    ~IRP_RSCN_QUEUED);
3302 					}
3303 					rw_exit(&iport->iport_lock);
3304 				}
3305 			} else {
3306 				ASSERT(0);
3307 			}
3308 		} else {
3309 			clock_t	timeout_ticks;
3310 			if (port->port_fca_abort_timeout)
3311 				timeout_ticks = drv_usectohz(
3312 				    port->port_fca_abort_timeout*1000);
3313 			else
3314 				/* 10 seconds by default */
3315 				timeout_ticks = drv_usectohz(10 * 1000000);
3316 			if ((ddi_get_lbolt() >
3317 			    (icmd->icmd_start_time+timeout_ticks)) &&
3318 			    iport->iport_state == FCT_STATE_ONLINE) {
3319 				/* timeout, reset the port */
3320 				char cmd_type[10];
3321 				if (cmd->cmd_type == FCT_CMD_RCVD_ELS ||
3322 				    cmd->cmd_type == FCT_CMD_SOL_ELS) {
3323 					fct_els_t *els = cmd->cmd_specific;
3324 					(void) snprintf(cmd_type,
3325 					    sizeof (cmd_type), "%x.%x",
3326 					    cmd->cmd_type,
3327 					    els->els_req_payload[0]);
3328 				} else if (cmd->cmd_type == FCT_CMD_SOL_CT) {
3329 					fct_sol_ct_t *ct = cmd->cmd_specific;
3330 					(void) snprintf(cmd_type,
3331 					    sizeof (cmd_type), "%x.%02x%02x",
3332 					    cmd->cmd_type,
3333 					    ct->ct_req_payload[8],
3334 					    ct->ct_req_payload[9]);
3335 				} else {
3336 					cmd_type[0] = 0;
3337 				}
3338 				st = cmd->cmd_comp_status;	/* gcc fix */
3339 				(void) snprintf(info, 80, "fct_cmd_terminator:"
3340 				    " iport-%p, cmd_type(0x%s),"
3341 				    " reason(%llx)", (void *)iport, cmd_type,
3342 				    st);
3343 				info[79] = 0;
3344 				(void) fct_port_shutdown(port,
3345 				    STMF_RFLAG_FATAL_ERROR | STMF_RFLAG_RESET,
3346 				    info);
3347 			}
3348 			ppicmd = &((*ppicmd)->icmd_next);
3349 		}
3350 
3351 		if (ddi_get_lbolt() > endtime) {
3352 			mutex_enter(&iport->iport_worker_lock);
3353 			iport->iport_ppicmd_term = ppicmd;
3354 			return (DISC_ACTION_DELAY_RESCAN);
3355 		}
3356 	}
3357 	mutex_enter(&iport->iport_worker_lock);
3358 	if (iport->iport_abort_queue)
3359 		return (DISC_ACTION_DELAY_RESCAN);
3360 	if (ret == DISC_ACTION_NO_WORK)
3361 		return (DISC_ACTION_RESCAN);
3362 	return (ret);
3363 }
3364 
3365 /*
3366  * Send a syslog event for adapter port level events.
3367  */
3368 void
3369 fct_log_local_port_event(fct_local_port_t *port, char *subclass)
3370 {
3371 	nvlist_t *attr_list;
3372 	int port_instance;
3373 
3374 	if (!fct_dip)
3375 		return;
3376 	port_instance = ddi_get_instance(fct_dip);
3377 
3378 	if (nvlist_alloc(&attr_list, NV_UNIQUE_NAME_TYPE,
3379 	    KM_SLEEP) != DDI_SUCCESS) {
3380 		goto alloc_failed;
3381 	}
3382 
3383 	if (nvlist_add_uint32(attr_list, "instance", port_instance)
3384 	    != DDI_SUCCESS) {
3385 		goto error;
3386 	}
3387 
3388 	if (nvlist_add_byte_array(attr_list, "port-wwn",
3389 	    port->port_pwwn, 8) != DDI_SUCCESS) {
3390 		goto error;
3391 	}
3392 
3393 	(void) ddi_log_sysevent(fct_dip, DDI_VENDOR_SUNW, EC_SUNFC,
3394 	    subclass, attr_list, NULL, DDI_SLEEP);
3395 
3396 	nvlist_free(attr_list);
3397 	return;
3398 
3399 error:
3400 	nvlist_free(attr_list);
3401 alloc_failed:
3402 	stmf_trace(((fct_i_local_port_t *)port->port_fct_private)->iport_alias,
3403 	    "Unable to send %s event", subclass);
3404 }
3405 
3406 void
3407 fct_log_remote_port_event(fct_local_port_t *port, char *subclass,
3408     uint8_t *rp_pwwn, uint32_t rp_id)
3409 {
3410 	nvlist_t *attr_list;
3411 	int port_instance;
3412 
3413 	if (!fct_dip)
3414 		return;
3415 	port_instance = ddi_get_instance(fct_dip);
3416 
3417 	if (nvlist_alloc(&attr_list, NV_UNIQUE_NAME_TYPE,
3418 	    KM_SLEEP) != DDI_SUCCESS) {
3419 		goto alloc_failed;
3420 	}
3421 
3422 	if (nvlist_add_uint32(attr_list, "instance", port_instance)
3423 	    != DDI_SUCCESS) {
3424 		goto error;
3425 	}
3426 
3427 	if (nvlist_add_byte_array(attr_list, "port-wwn",
3428 	    port->port_pwwn, 8) != DDI_SUCCESS) {
3429 		goto error;
3430 	}
3431 
3432 	if (nvlist_add_byte_array(attr_list, "target-port-wwn",
3433 	    rp_pwwn, 8) != DDI_SUCCESS) {
3434 		goto error;
3435 	}
3436 
3437 	if (nvlist_add_uint32(attr_list, "target-port-id",
3438 	    rp_id) != DDI_SUCCESS) {
3439 		goto error;
3440 	}
3441 
3442 	(void) ddi_log_sysevent(fct_dip, DDI_VENDOR_SUNW, EC_SUNFC,
3443 	    subclass, attr_list, NULL, DDI_SLEEP);
3444 
3445 	nvlist_free(attr_list);
3446 	return;
3447 
3448 error:
3449 	nvlist_free(attr_list);
3450 alloc_failed:
3451 	stmf_trace(((fct_i_local_port_t *)port->port_fct_private)->iport_alias,
3452 	    "Unable to send %s event", subclass);
3453 }
3454 
3455 uint64_t
3456 fct_netbuf_to_value(uint8_t *buf, uint8_t nbytes)
3457 {
3458 	uint64_t	ret = 0;
3459 	uint8_t		idx = 0;
3460 
3461 	do {
3462 		ret |= (buf[idx] << (8 * (nbytes -idx - 1)));
3463 	} while (++idx < nbytes);
3464 
3465 	return (ret);
3466 }
3467 
3468 void
3469 fct_value_to_netbuf(uint64_t value, uint8_t *buf, uint8_t nbytes)
3470 {
3471 	uint8_t		idx = 0;
3472 
3473 	for (idx = 0; idx < nbytes; idx++) {
3474 		buf[idx] = 0xFF & (value >> (8 * (nbytes - idx - 1)));
3475 	}
3476 }
3477 
3478 /*
3479  * from_ptr: ptr to uchar_t array of size WWN_SIZE
3480  * to_ptr: char ptr to string of size WWN_SIZE*2+1
3481  */
3482 void
3483 fct_wwn_to_str(char *to_ptr, const uint8_t *from_ptr)
3484 {
3485 	ASSERT(to_ptr != NULL && from_ptr != NULL);
3486 
3487 	(void) sprintf(to_ptr, "%02x%02x%02x%02x%02x%02x%02x%02x",
3488 	    from_ptr[0], from_ptr[1], from_ptr[2], from_ptr[3],
3489 	    from_ptr[4], from_ptr[5], from_ptr[6], from_ptr[7]);
3490 }
3491 
3492 static int
3493 fct_update_stats(kstat_t *ks, int rw)
3494 {
3495 	fct_i_local_port_t *iport;
3496 	fct_port_stat_t *port_kstat;
3497 	fct_port_link_status_t stat;
3498 	uint32_t	buf_size = sizeof (stat);
3499 	int		ret;
3500 
3501 	if (rw == KSTAT_WRITE)
3502 		return (EACCES);
3503 
3504 	iport = (fct_i_local_port_t *)ks->ks_private;
3505 	port_kstat = (fct_port_stat_t *)ks->ks_data;
3506 
3507 	if (iport->iport_port->port_info == NULL) {
3508 		return (EIO);
3509 	}
3510 	ret = iport->iport_port->port_info(FC_TGT_PORT_RLS,
3511 	    iport->iport_port, NULL, (uint8_t *)&stat, &buf_size);
3512 	if (ret != STMF_SUCCESS) {
3513 		return (EIO);
3514 	}
3515 
3516 	port_kstat->link_failure_cnt.value.ui32 =
3517 	    stat.LinkFailureCount;
3518 	port_kstat->loss_of_sync_cnt.value.ui32 =
3519 	    stat.LossOfSyncCount;
3520 	port_kstat->loss_of_signals_cnt.value.ui32 =
3521 	    stat.LossOfSignalsCount;
3522 	port_kstat->prim_seq_protocol_err_cnt.value.ui32 =
3523 	    stat.PrimitiveSeqProtocolErrorCount;
3524 	port_kstat->invalid_tx_word_cnt.value.ui32 =
3525 	    stat.InvalidTransmissionWordCount;
3526 	port_kstat->invalid_crc_cnt.value.ui32 =
3527 	    stat.InvalidCRCCount;
3528 
3529 	return (0);
3530 }
3531 
3532 void
3533 fct_init_kstats(fct_i_local_port_t *iport)
3534 {
3535 	kstat_t *ks;
3536 	fct_port_stat_t *port_kstat;
3537 	char	name[256];
3538 
3539 	if (iport->iport_alias)
3540 		(void) sprintf(name, "iport_%s", iport->iport_alias);
3541 	else
3542 		(void) sprintf(name, "iport_%"PRIxPTR"", (uintptr_t)iport);
3543 	ks = kstat_create(FCT_MODULE_NAME, 0, name, "rawdata",
3544 	    KSTAT_TYPE_NAMED, sizeof (fct_port_stat_t) / sizeof (kstat_named_t),
3545 	    0);
3546 
3547 	if (ks == NULL) {
3548 		return;
3549 	}
3550 	port_kstat = (fct_port_stat_t *)ks->ks_data;
3551 
3552 	iport->iport_kstat_portstat = ks;
3553 	kstat_named_init(&port_kstat->link_failure_cnt,
3554 	    "Link_failure_cnt", KSTAT_DATA_UINT32);
3555 	kstat_named_init(&port_kstat->loss_of_sync_cnt,
3556 	    "Loss_of_sync_cnt", KSTAT_DATA_UINT32);
3557 	kstat_named_init(&port_kstat->loss_of_signals_cnt,
3558 	    "Loss_of_signals_cnt", KSTAT_DATA_UINT32);
3559 	kstat_named_init(&port_kstat->prim_seq_protocol_err_cnt,
3560 	    "Prim_seq_protocol_err_cnt", KSTAT_DATA_UINT32);
3561 	kstat_named_init(&port_kstat->invalid_tx_word_cnt,
3562 	    "Invalid_tx_word_cnt", KSTAT_DATA_UINT32);
3563 	kstat_named_init(&port_kstat->invalid_crc_cnt,
3564 	    "Invalid_crc_cnt", KSTAT_DATA_UINT32);
3565 	ks->ks_update = fct_update_stats;
3566 	ks->ks_private = (void *)iport;
3567 	kstat_install(ks);
3568 
3569 }
3570