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 2010 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_fca_version != FCT_FCA_MODREV_1) {
1122 		cmn_err(CE_WARN,
1123 		    "fct: %s driver version mismatch",
1124 		    port->port_default_alias);
1125 		return (FCT_FAILURE);
1126 	}
1127 	if (port->port_default_alias) {
1128 		int l = strlen(port->port_default_alias);
1129 
1130 		if (l < 16) {
1131 			iport->iport_alias = iport->iport_alias_mem;
1132 		} else {
1133 			iport->iport_alias =
1134 			    (char *)kmem_zalloc(l+1, KM_SLEEP);
1135 		}
1136 		(void) strcpy(iport->iport_alias, port->port_default_alias);
1137 	} else {
1138 		iport->iport_alias = NULL;
1139 	}
1140 	stmf_wwn_to_devid_desc((scsi_devid_desc_t *)iport->iport_id,
1141 	    port->port_pwwn, PROTOCOL_FIBRE_CHANNEL);
1142 	(void) snprintf(taskq_name, 24, "stmf_fct_taskq_%d",
1143 	    atomic_add_32_nv(&taskq_cntr, 1));
1144 	taskq_name[23] = 0;
1145 	if ((iport->iport_worker_taskq = ddi_taskq_create(NULL,
1146 	    taskq_name, 1, TASKQ_DEFAULTPRI, 0)) == NULL) {
1147 		return (FCT_FAILURE);
1148 	}
1149 	mutex_init(&iport->iport_worker_lock, NULL, MUTEX_DRIVER, NULL);
1150 	cv_init(&iport->iport_worker_cv, NULL, CV_DRIVER, NULL);
1151 	rw_init(&iport->iport_lock, NULL, RW_DRIVER, NULL);
1152 	sema_init(&iport->iport_rls_sema, 0, NULL, SEMA_DRIVER, NULL);
1153 
1154 	/* Remote port mgmt */
1155 	iport->iport_rp_slots = (fct_i_remote_port_t **)kmem_zalloc(
1156 	    port->port_max_logins * sizeof (fct_i_remote_port_t *), KM_SLEEP);
1157 	iport->iport_rp_tb = kmem_zalloc(rportid_table_size *
1158 	    sizeof (fct_i_remote_port_t *), KM_SLEEP);
1159 
1160 	/* fct_cmds for SCSI traffic */
1161 	iport->iport_total_alloced_ncmds = 0;
1162 	iport->iport_cached_ncmds = 0;
1163 	port->port_fca_fcp_cmd_size =
1164 	    (port->port_fca_fcp_cmd_size + 7) & ~7;
1165 	iport->iport_cached_cmdlist = NULL;
1166 	mutex_init(&iport->iport_cached_cmd_lock, NULL, MUTEX_DRIVER, NULL);
1167 
1168 	/* Initialize cmd slots */
1169 	iport->iport_cmd_slots = (fct_cmd_slot_t *)kmem_zalloc(
1170 	    port->port_max_xchges * sizeof (fct_cmd_slot_t), KM_SLEEP);
1171 	iport->iport_next_free_slot = 0;
1172 	for (i = 0; i < port->port_max_xchges; ) {
1173 		slot = &iport->iport_cmd_slots[i];
1174 		slot->slot_no = (uint16_t)i;
1175 		slot->slot_next = (uint16_t)(++i);
1176 	}
1177 	slot->slot_next = FCT_SLOT_EOL;
1178 	iport->iport_nslots_free = port->port_max_xchges;
1179 
1180 	iport->iport_task_green_limit =
1181 	    (port->port_max_xchges * FCT_TASK_GREEN_LIMIT) / 100;
1182 	iport->iport_task_yellow_limit =
1183 	    (port->port_max_xchges * FCT_TASK_YELLOW_LIMIT) / 100;
1184 	iport->iport_task_red_limit =
1185 	    (port->port_max_xchges * FCT_TASK_RED_LIMIT) / 100;
1186 
1187 	/* Start worker thread */
1188 	atomic_and_32(&iport->iport_flags, ~IPORT_TERMINATE_WORKER);
1189 	(void) ddi_taskq_dispatch(iport->iport_worker_taskq,
1190 	    fct_port_worker, port, DDI_SLEEP);
1191 	/* Wait for taskq to start */
1192 	while ((iport->iport_flags & IPORT_WORKER_RUNNING) == 0) {
1193 		delay(1);
1194 	}
1195 
1196 	lport = port->port_lport;
1197 	lport->lport_id = (scsi_devid_desc_t *)iport->iport_id;
1198 	lport->lport_alias = iport->iport_alias;
1199 	lport->lport_pp = port->port_pp;
1200 	port->port_fds->fds_ds->ds_alloc_data_buf = fct_alloc_dbuf;
1201 	port->port_fds->fds_ds->ds_free_data_buf = fct_free_dbuf;
1202 	lport->lport_ds = port->port_fds->fds_ds;
1203 	lport->lport_xfer_data = fct_xfer_scsi_data;
1204 	lport->lport_send_status = fct_send_scsi_status;
1205 	lport->lport_task_free = fct_scsi_task_free;
1206 	lport->lport_abort = fct_scsi_abort;
1207 	lport->lport_ctl = fct_ctl;
1208 	lport->lport_info = fct_info;
1209 	lport->lport_event_handler = fct_event_handler;
1210 	/* set up as alua participating port */
1211 	stmf_set_port_alua(lport);
1212 	if (stmf_register_local_port(port->port_lport) != FCT_SUCCESS) {
1213 		goto fct_regport_fail1;
1214 	}
1215 	(void) stmf_lport_add_event(lport, LPORT_EVENT_INITIAL_LUN_MAPPED);
1216 
1217 	mutex_enter(&fct_global_mutex);
1218 	iport->iport_next = fct_iport_list;
1219 	iport->iport_prev = NULL;
1220 	if (iport->iport_next)
1221 		iport->iport_next->iport_prev = iport;
1222 	fct_iport_list = iport;
1223 	mutex_exit(&fct_global_mutex);
1224 
1225 	fct_init_kstats(iport);
1226 
1227 	fct_log_local_port_event(port, ESC_SUNFC_PORT_ATTACH);
1228 
1229 	return (FCT_SUCCESS);
1230 
1231 fct_regport_fail1:;
1232 	/* Stop the taskq 1st */
1233 	if (iport->iport_flags & IPORT_WORKER_RUNNING) {
1234 		atomic_or_32(&iport->iport_flags, IPORT_TERMINATE_WORKER);
1235 		cv_broadcast(&iport->iport_worker_cv);
1236 		while (iport->iport_flags & IPORT_WORKER_RUNNING) {
1237 			delay(1);
1238 		}
1239 	}
1240 	ddi_taskq_destroy(iport->iport_worker_taskq);
1241 	if (iport->iport_rp_tb) {
1242 		kmem_free(iport->iport_rp_tb, rportid_table_size *
1243 		    sizeof (fct_i_remote_port_t *));
1244 	}
1245 	return (FCT_FAILURE);
1246 }
1247 
1248 fct_status_t
1249 fct_deregister_local_port(fct_local_port_t *port)
1250 {
1251 	fct_i_local_port_t	*iport;
1252 	fct_i_cmd_t		*icmd, *next_icmd;
1253 	int			ndx;
1254 
1255 	iport = (fct_i_local_port_t *)port->port_fct_private;
1256 
1257 	if ((iport->iport_state != FCT_STATE_OFFLINE) ||
1258 	    iport->iport_state_not_acked) {
1259 		return (FCT_FAILURE);
1260 	}
1261 
1262 	/* Stop the taskq 1st */
1263 	if (iport->iport_flags & IPORT_WORKER_RUNNING) {
1264 		atomic_or_32(&iport->iport_flags, IPORT_TERMINATE_WORKER);
1265 		cv_broadcast(&iport->iport_worker_cv);
1266 		for (ndx = 0; ndx < 100; ndx++) {
1267 			if ((iport->iport_flags & IPORT_WORKER_RUNNING)
1268 			    == 0) {
1269 				break;
1270 			}
1271 			delay(drv_usectohz(10000));
1272 		}
1273 		if (ndx == 100) {
1274 			atomic_and_32(&iport->iport_flags,
1275 			    ~IPORT_TERMINATE_WORKER);
1276 			return (FCT_WORKER_STUCK);
1277 		}
1278 	}
1279 
1280 	if (stmf_deregister_local_port(port->port_lport) != FCT_SUCCESS) {
1281 		goto fct_deregport_fail1;
1282 	}
1283 
1284 	mutex_enter(&fct_global_mutex);
1285 	if (iport->iport_next)
1286 		iport->iport_next->iport_prev = iport->iport_prev;
1287 	if (iport->iport_prev)
1288 		iport->iport_prev->iport_next = iport->iport_next;
1289 	else
1290 		fct_iport_list = iport->iport_next;
1291 	mutex_exit(&fct_global_mutex);
1292 	/*
1293 	 * At this time, there should be no outstanding and pending
1294 	 * I/Os, so we can just release resources.
1295 	 */
1296 	ASSERT(iport->iport_total_alloced_ncmds == iport->iport_cached_ncmds);
1297 	for (icmd = iport->iport_cached_cmdlist; icmd; icmd = next_icmd) {
1298 		next_icmd = icmd->icmd_next;
1299 		fct_free(icmd->icmd_cmd);
1300 	}
1301 	mutex_destroy(&iport->iport_cached_cmd_lock);
1302 	kmem_free(iport->iport_cmd_slots, port->port_max_xchges *
1303 	    sizeof (fct_cmd_slot_t));
1304 	kmem_free(iport->iport_rp_slots, port->port_max_logins *
1305 	    sizeof (fct_i_remote_port_t *));
1306 	rw_destroy(&iport->iport_lock);
1307 	cv_destroy(&iport->iport_worker_cv);
1308 	sema_destroy(&iport->iport_rls_sema);
1309 	mutex_destroy(&iport->iport_worker_lock);
1310 	ddi_taskq_destroy(iport->iport_worker_taskq);
1311 	if (iport->iport_rp_tb) {
1312 		kmem_free(iport->iport_rp_tb, rportid_table_size *
1313 		    sizeof (fct_i_remote_port_t *));
1314 	}
1315 
1316 	if (iport->iport_kstat_portstat) {
1317 		kstat_delete(iport->iport_kstat_portstat);
1318 	}
1319 
1320 	fct_log_local_port_event(port, ESC_SUNFC_PORT_DETACH);
1321 	return (FCT_SUCCESS);
1322 
1323 fct_deregport_fail1:;
1324 	/* Restart the worker */
1325 	atomic_and_32(&iport->iport_flags, ~IPORT_TERMINATE_WORKER);
1326 	(void) ddi_taskq_dispatch(iport->iport_worker_taskq,
1327 	    fct_port_worker, port, DDI_SLEEP);
1328 	/* Wait for taskq to start */
1329 	while ((iport->iport_flags & IPORT_WORKER_RUNNING) == 0) {
1330 		delay(1);
1331 	}
1332 	return (FCT_FAILURE);
1333 }
1334 
1335 /* ARGSUSED */
1336 void
1337 fct_handle_event(fct_local_port_t *port, int event_id, uint32_t event_flags,
1338 		caddr_t arg)
1339 {
1340 	char			info[80];
1341 	fct_i_event_t		*e;
1342 	fct_i_local_port_t	*iport = (fct_i_local_port_t *)
1343 	    port->port_fct_private;
1344 
1345 	e = kmem_zalloc(sizeof (fct_i_event_t), KM_NOSLEEP);
1346 
1347 	if (e == NULL) {
1348 		/*
1349 		 * XXX Throw HBA fatal error event
1350 		 */
1351 		(void) snprintf(info, 80,
1352 		    "fct_handle_event: iport-%p, allocation "
1353 		    "of fct_i_event failed", (void *)iport);
1354 		info[79] = 0;
1355 		(void) fct_port_shutdown(iport->iport_port,
1356 		    STMF_RFLAG_FATAL_ERROR | STMF_RFLAG_RESET, info);
1357 		return;
1358 	}
1359 	/* Just queue the event */
1360 	e->event_type = event_id;
1361 	mutex_enter(&iport->iport_worker_lock);
1362 	if (iport->iport_event_head == NULL) {
1363 		iport->iport_event_head = iport->iport_event_tail = e;
1364 	} else {
1365 		iport->iport_event_tail->event_next = e;
1366 		iport->iport_event_tail = e;
1367 	}
1368 	if (IS_WORKER_SLEEPING(iport))
1369 		cv_signal(&iport->iport_worker_cv);
1370 	mutex_exit(&iport->iport_worker_lock);
1371 }
1372 
1373 /*
1374  * Called with iport_lock held as reader.
1375  */
1376 fct_i_remote_port_t *
1377 fct_portid_to_portptr(fct_i_local_port_t *iport, uint32_t portid)
1378 {
1379 	fct_i_remote_port_t	*irp;
1380 
1381 	irp = iport->iport_rp_tb[FCT_PORTID_HASH_FUNC(portid)];
1382 	for (; irp != NULL; irp = irp->irp_next) {
1383 		if (irp->irp_portid == portid)
1384 			return (irp);
1385 	}
1386 
1387 	return (NULL);
1388 
1389 }
1390 
1391 /*
1392  * Called with irp_lock held as writer.
1393  */
1394 void
1395 fct_queue_rp(fct_i_local_port_t *iport, fct_i_remote_port_t *irp)
1396 {
1397 	int hash_key =
1398 	    FCT_PORTID_HASH_FUNC(irp->irp_portid);
1399 
1400 	irp->irp_next = iport->iport_rp_tb[hash_key];
1401 	iport->iport_rp_tb[hash_key] = irp;
1402 	iport->iport_nrps++;
1403 }
1404 
1405 /*
1406  * Called with irp_lock and iport_lock held as writer.
1407  */
1408 void
1409 fct_deque_rp(fct_i_local_port_t *iport, fct_i_remote_port_t *irp)
1410 {
1411 	fct_i_remote_port_t	*irp_next = NULL;
1412 	fct_i_remote_port_t	*irp_last = NULL;
1413 	int hash_key			  =
1414 	    FCT_PORTID_HASH_FUNC(irp->irp_portid);
1415 
1416 	irp_next = iport->iport_rp_tb[hash_key];
1417 	irp_last = NULL;
1418 	while (irp_next != NULL) {
1419 		if (irp == irp_next) {
1420 			if (irp->irp_flags & IRP_PLOGI_DONE) {
1421 				atomic_add_32(&iport->iport_nrps_login, -1);
1422 			}
1423 			atomic_and_32(&irp->irp_flags,
1424 			    ~(IRP_PLOGI_DONE | IRP_PRLI_DONE));
1425 			break;
1426 		}
1427 		irp_last = irp_next;
1428 		irp_next = irp_next->irp_next;
1429 	}
1430 
1431 	if (irp_next) {
1432 		if (irp_last == NULL) {
1433 			iport->iport_rp_tb[hash_key] =
1434 			    irp->irp_next;
1435 		} else {
1436 			irp_last->irp_next = irp->irp_next;
1437 		}
1438 		irp->irp_next = NULL;
1439 		iport->iport_nrps--;
1440 	}
1441 }
1442 
1443 int
1444 fct_is_irp_logging_out(fct_i_remote_port_t *irp, int force_implicit)
1445 {
1446 	int logging_out = 0;
1447 
1448 	rw_enter(&irp->irp_lock, RW_WRITER);
1449 	if ((irp->irp_flags & IRP_IN_DISCOVERY_QUEUE) == 0) {
1450 		logging_out = 0;
1451 		goto ilo_done;
1452 	}
1453 	if ((irp->irp_els_list == NULL) && (irp->irp_deregister_timer)) {
1454 		if (force_implicit && irp->irp_nonfcp_xchg_count) {
1455 			logging_out = 0;
1456 		} else {
1457 			logging_out = 1;
1458 		}
1459 		goto ilo_done;
1460 	}
1461 	if (irp->irp_els_list) {
1462 		fct_i_cmd_t *icmd;
1463 		/* Last session affecting ELS should be a LOGO */
1464 		for (icmd = irp->irp_els_list; icmd; icmd = icmd->icmd_next) {
1465 			uint8_t op = (ICMD_TO_ELS(icmd))->els_req_payload[0];
1466 			if (op == ELS_OP_LOGO) {
1467 				if (force_implicit) {
1468 					if (icmd->icmd_flags & ICMD_IMPLICIT)
1469 						logging_out = 1;
1470 					else
1471 						logging_out = 0;
1472 				} else {
1473 					logging_out = 1;
1474 				}
1475 			} else if ((op == ELS_OP_PLOGI) ||
1476 			    (op == ELS_OP_PRLI) ||
1477 			    (op == ELS_OP_PRLO) || (op == ELS_OP_TPRLO)) {
1478 				logging_out = 0;
1479 			}
1480 		}
1481 	}
1482 ilo_done:;
1483 	rw_exit(&irp->irp_lock);
1484 
1485 	return (logging_out);
1486 }
1487 
1488 /*
1489  * The force_implicit flag enforces the implicit semantics which may be
1490  * needed if a received logout got stuck e.g. a response to a received
1491  * LOGO never came back from the FCA.
1492  */
1493 int
1494 fct_implicitly_logo_all(fct_i_local_port_t *iport, int force_implicit)
1495 {
1496 	fct_i_remote_port_t	*irp = NULL;
1497 	fct_cmd_t		*cmd = NULL;
1498 	int			 i   = 0;
1499 	int			nports = 0;
1500 
1501 	if (!iport->iport_nrps) {
1502 		return (nports);
1503 	}
1504 
1505 	rw_enter(&iport->iport_lock, RW_WRITER);
1506 	for (i = 0; i < rportid_table_size; i++) {
1507 		irp = iport->iport_rp_tb[i];
1508 		while (irp) {
1509 			if ((!(irp->irp_flags & IRP_PLOGI_DONE)) &&
1510 			    (fct_is_irp_logging_out(irp, force_implicit))) {
1511 				irp = irp->irp_next;
1512 				continue;
1513 			}
1514 
1515 			cmd = fct_create_solels(iport->iport_port, irp->irp_rp,
1516 			    1, ELS_OP_LOGO, 0, fct_logo_cb);
1517 			if (cmd == NULL) {
1518 				stmf_trace(iport->iport_alias,
1519 				    "fct_implictly_logo_all: cmd null");
1520 				rw_exit(&iport->iport_lock);
1521 
1522 				return (nports);
1523 			}
1524 
1525 			fct_post_implicit_logo(cmd);
1526 			nports++;
1527 			irp = irp->irp_next;
1528 		}
1529 	}
1530 	rw_exit(&iport->iport_lock);
1531 
1532 	return (nports);
1533 }
1534 
1535 void
1536 fct_rehash(fct_i_local_port_t *iport)
1537 {
1538 	fct_i_remote_port_t **iport_rp_tb_tmp;
1539 	fct_i_remote_port_t **iport_rp_tb_new;
1540 	fct_i_remote_port_t *irp;
1541 	fct_i_remote_port_t *irp_next;
1542 	int i;
1543 
1544 	iport_rp_tb_new = kmem_zalloc(rportid_table_size *
1545 	    sizeof (fct_i_remote_port_t *), KM_SLEEP);
1546 	rw_enter(&iport->iport_lock, RW_WRITER);
1547 	/* reconstruct the hash table */
1548 	iport_rp_tb_tmp = iport->iport_rp_tb;
1549 	iport->iport_rp_tb = iport_rp_tb_new;
1550 	iport->iport_nrps = 0;
1551 	for (i = 0; i < rportid_table_size; i++) {
1552 		irp = iport_rp_tb_tmp[i];
1553 		while (irp) {
1554 			irp_next = irp->irp_next;
1555 			fct_queue_rp(iport, irp);
1556 			irp = irp_next;
1557 		}
1558 	}
1559 	rw_exit(&iport->iport_lock);
1560 	kmem_free(iport_rp_tb_tmp, rportid_table_size *
1561 	    sizeof (fct_i_remote_port_t *));
1562 
1563 }
1564 
1565 uint8_t
1566 fct_local_port_cleanup_done(fct_i_local_port_t *iport)
1567 {
1568 	fct_i_remote_port_t *irp;
1569 	int i;
1570 
1571 	if (iport->iport_nrps_login)
1572 		return (0);
1573 	/* loop all rps to check if the cmd have already been drained */
1574 	for (i = 0; i < rportid_table_size; i++) {
1575 		irp = iport->iport_rp_tb[i];
1576 		while (irp) {
1577 			if (irp->irp_fcp_xchg_count ||
1578 			    irp->irp_nonfcp_xchg_count)
1579 				return (0);
1580 			irp = irp->irp_next;
1581 		}
1582 	}
1583 	return (1);
1584 }
1585 
1586 fct_cmd_t *
1587 fct_scsi_task_alloc(fct_local_port_t *port, uint16_t rp_handle,
1588 		uint32_t rportid, uint8_t *lun, uint16_t cdb_length,
1589 		uint16_t task_ext)
1590 {
1591 	fct_cmd_t *cmd;
1592 	fct_i_cmd_t *icmd;
1593 	fct_i_local_port_t *iport =
1594 	    (fct_i_local_port_t *)port->port_fct_private;
1595 	fct_i_remote_port_t *irp;
1596 	scsi_task_t *task;
1597 	fct_remote_port_t *rp;
1598 	uint16_t cmd_slot;
1599 
1600 	rw_enter(&iport->iport_lock, RW_READER);
1601 	if ((iport->iport_link_state & S_LINK_ONLINE) == 0) {
1602 		rw_exit(&iport->iport_lock);
1603 		stmf_trace(iport->iport_alias, "cmd alloc called while the port"
1604 		    " was offline");
1605 		return (NULL);
1606 	}
1607 
1608 	if (rp_handle == FCT_HANDLE_NONE) {
1609 		irp = fct_portid_to_portptr(iport, rportid);
1610 		if (irp == NULL) {
1611 			rw_exit(&iport->iport_lock);
1612 			stmf_trace(iport->iport_alias, "cmd received from "
1613 			    "non existent port %x", rportid);
1614 			return (NULL);
1615 		}
1616 	} else {
1617 		if ((rp_handle >= port->port_max_logins) ||
1618 		    ((irp = iport->iport_rp_slots[rp_handle]) == NULL)) {
1619 			rw_exit(&iport->iport_lock);
1620 			stmf_trace(iport->iport_alias, "cmd received from "
1621 			    "invalid port handle %x", rp_handle);
1622 			return (NULL);
1623 		}
1624 	}
1625 	rp = irp->irp_rp;
1626 
1627 	rw_enter(&irp->irp_lock, RW_READER);
1628 	if ((irp->irp_flags & IRP_PRLI_DONE) == 0) {
1629 		rw_exit(&irp->irp_lock);
1630 		rw_exit(&iport->iport_lock);
1631 		stmf_trace(iport->iport_alias, "cmd alloc called while fcp "
1632 		    "login was not done. portid=%x, rp=%p", rp->rp_id, rp);
1633 		return (NULL);
1634 	}
1635 
1636 	mutex_enter(&iport->iport_cached_cmd_lock);
1637 	if ((icmd = iport->iport_cached_cmdlist) != NULL) {
1638 		iport->iport_cached_cmdlist = icmd->icmd_next;
1639 		iport->iport_cached_ncmds--;
1640 		cmd = icmd->icmd_cmd;
1641 	} else {
1642 		icmd = NULL;
1643 	}
1644 	mutex_exit(&iport->iport_cached_cmd_lock);
1645 	if (icmd == NULL) {
1646 		cmd = (fct_cmd_t *)fct_alloc(FCT_STRUCT_CMD_FCP_XCHG,
1647 		    port->port_fca_fcp_cmd_size, 0);
1648 		if (cmd == NULL) {
1649 			rw_exit(&irp->irp_lock);
1650 			rw_exit(&iport->iport_lock);
1651 			stmf_trace(iport->iport_alias, "Ran out of "
1652 			    "memory, port=%p", port);
1653 			return (NULL);
1654 		}
1655 
1656 		icmd = (fct_i_cmd_t *)cmd->cmd_fct_private;
1657 		icmd->icmd_next = NULL;
1658 		cmd->cmd_port = port;
1659 		atomic_add_32(&iport->iport_total_alloced_ncmds, 1);
1660 	}
1661 
1662 	/*
1663 	 * The accuracy of iport_max_active_ncmds is not important
1664 	 */
1665 	if ((iport->iport_total_alloced_ncmds - iport->iport_cached_ncmds) >
1666 	    iport->iport_max_active_ncmds) {
1667 		iport->iport_max_active_ncmds =
1668 		    iport->iport_total_alloced_ncmds -
1669 		    iport->iport_cached_ncmds;
1670 	}
1671 
1672 	/* Lets get a slot */
1673 	cmd_slot = fct_alloc_cmd_slot(iport, cmd);
1674 	if (cmd_slot == FCT_SLOT_EOL) {
1675 		rw_exit(&irp->irp_lock);
1676 		rw_exit(&iport->iport_lock);
1677 		stmf_trace(iport->iport_alias, "Ran out of xchg resources");
1678 		cmd->cmd_handle = 0;
1679 		fct_cmd_free(cmd);
1680 		return (NULL);
1681 	}
1682 	atomic_add_16(&irp->irp_fcp_xchg_count, 1);
1683 	cmd->cmd_rp = rp;
1684 	icmd->icmd_flags |= ICMD_IN_TRANSITION | ICMD_KNOWN_TO_FCA;
1685 	rw_exit(&irp->irp_lock);
1686 	rw_exit(&iport->iport_lock);
1687 
1688 	icmd->icmd_start_time = ddi_get_lbolt();
1689 
1690 	cmd->cmd_specific = stmf_task_alloc(port->port_lport, irp->irp_session,
1691 	    lun, cdb_length, task_ext);
1692 	if ((task = (scsi_task_t *)cmd->cmd_specific) != NULL) {
1693 		task->task_port_private = cmd;
1694 		return (cmd);
1695 	}
1696 
1697 	fct_cmd_free(cmd);
1698 
1699 	return (NULL);
1700 }
1701 
1702 void
1703 fct_scsi_task_free(scsi_task_t *task)
1704 {
1705 	fct_cmd_t *cmd = (fct_cmd_t *)task->task_port_private;
1706 
1707 	cmd->cmd_comp_status = task->task_completion_status;
1708 	fct_cmd_free(cmd);
1709 }
1710 
1711 void
1712 fct_post_rcvd_cmd(fct_cmd_t *cmd, stmf_data_buf_t *dbuf)
1713 {
1714 	if (cmd->cmd_type == FCT_CMD_FCP_XCHG) {
1715 		fct_i_cmd_t *icmd = (fct_i_cmd_t *)cmd->cmd_fct_private;
1716 		fct_i_local_port_t *iport =
1717 		    (fct_i_local_port_t *)cmd->cmd_port->port_fct_private;
1718 		fct_i_remote_port_t *irp =
1719 		    (fct_i_remote_port_t *)cmd->cmd_rp->rp_fct_private;
1720 		scsi_task_t *task = (scsi_task_t *)cmd->cmd_specific;
1721 
1722 		uint16_t irp_task = irp->irp_fcp_xchg_count;
1723 		uint32_t load = iport->iport_total_alloced_ncmds -
1724 		    iport->iport_cached_ncmds;
1725 
1726 		DTRACE_FC_4(scsi__command,
1727 		    fct_cmd_t, cmd,
1728 		    fct_i_local_port_t, iport,
1729 		    scsi_task_t, task,
1730 		    fct_i_remote_port_t, irp);
1731 
1732 		if (load >= iport->iport_task_green_limit) {
1733 			if ((load < iport->iport_task_yellow_limit &&
1734 			    irp_task >= 4) ||
1735 			    (load >= iport->iport_task_yellow_limit &&
1736 			    load < iport->iport_task_red_limit &&
1737 			    irp_task >= 1) ||
1738 			    (load >= iport->iport_task_red_limit))
1739 				task->task_additional_flags |=
1740 				    TASK_AF_PORT_LOAD_HIGH;
1741 		}
1742 		stmf_post_task((scsi_task_t *)cmd->cmd_specific, dbuf);
1743 		atomic_and_32(&icmd->icmd_flags, ~ICMD_IN_TRANSITION);
1744 		return;
1745 	}
1746 	/* We dont need dbuf for other cmds */
1747 	if (dbuf) {
1748 		cmd->cmd_port->port_fds->fds_free_data_buf(
1749 		    cmd->cmd_port->port_fds, dbuf);
1750 		dbuf = NULL;
1751 	}
1752 	if (cmd->cmd_type == FCT_CMD_RCVD_ELS) {
1753 		fct_handle_els(cmd);
1754 		return;
1755 	}
1756 	if (cmd->cmd_type == FCT_CMD_RCVD_ABTS) {
1757 		fct_handle_rcvd_abts(cmd);
1758 		return;
1759 	}
1760 
1761 	ASSERT(0);
1762 }
1763 
1764 /*
1765  * This function bypasses fct_handle_els()
1766  */
1767 void
1768 fct_post_implicit_logo(fct_cmd_t *cmd)
1769 {
1770 	fct_local_port_t *port = cmd->cmd_port;
1771 	fct_i_local_port_t *iport =
1772 	    (fct_i_local_port_t *)port->port_fct_private;
1773 	fct_i_cmd_t *icmd = (fct_i_cmd_t *)cmd->cmd_fct_private;
1774 	fct_remote_port_t *rp = cmd->cmd_rp;
1775 	fct_i_remote_port_t *irp = (fct_i_remote_port_t *)rp->rp_fct_private;
1776 
1777 	icmd->icmd_start_time = ddi_get_lbolt();
1778 
1779 	rw_enter(&irp->irp_lock, RW_WRITER);
1780 	atomic_or_32(&icmd->icmd_flags, ICMD_IMPLICIT_CMD_HAS_RESOURCE);
1781 	atomic_add_16(&irp->irp_nonfcp_xchg_count, 1);
1782 	atomic_add_16(&irp->irp_sa_elses_count, 1);
1783 	/*
1784 	 * An implicit LOGO can also be posted to a irp where a PLOGI might
1785 	 * be in process. That PLOGI will reset this flag and decrement the
1786 	 * iport_nrps_login counter.
1787 	 */
1788 	if (irp->irp_flags & IRP_PLOGI_DONE) {
1789 		atomic_add_32(&iport->iport_nrps_login, -1);
1790 	}
1791 	atomic_and_32(&irp->irp_flags, ~(IRP_PLOGI_DONE | IRP_PRLI_DONE));
1792 	atomic_or_32(&icmd->icmd_flags, ICMD_SESSION_AFFECTING);
1793 	fct_post_to_discovery_queue(iport, irp, icmd);
1794 	rw_exit(&irp->irp_lock);
1795 }
1796 
1797 /*
1798  * called with iport_lock held, return the slot number
1799  */
1800 uint16_t
1801 fct_alloc_cmd_slot(fct_i_local_port_t *iport, fct_cmd_t *cmd)
1802 {
1803 	uint16_t cmd_slot;
1804 	uint32_t old, new;
1805 	fct_i_cmd_t *icmd = (fct_i_cmd_t *)cmd->cmd_fct_private;
1806 
1807 	do {
1808 		old = iport->iport_next_free_slot;
1809 		cmd_slot = old & 0xFFFF;
1810 		if (cmd_slot == FCT_SLOT_EOL)
1811 			return (cmd_slot);
1812 		/*
1813 		 * We use high order 16 bits as a counter which keeps on
1814 		 * incrementing to avoid ABA issues with atomic lists.
1815 		 */
1816 		new = ((old + (0x10000)) & 0xFFFF0000);
1817 		new |= iport->iport_cmd_slots[cmd_slot].slot_next;
1818 	} while (atomic_cas_32(&iport->iport_next_free_slot, old, new) != old);
1819 
1820 	atomic_add_16(&iport->iport_nslots_free, -1);
1821 	iport->iport_cmd_slots[cmd_slot].slot_cmd = icmd;
1822 	cmd->cmd_handle = (uint32_t)cmd_slot | 0x80000000 |
1823 	    (((uint32_t)(iport->iport_cmd_slots[cmd_slot].slot_uniq_cntr))
1824 	    << 24);
1825 	return (cmd_slot);
1826 }
1827 
1828 /*
1829  * If icmd is not NULL, irp_lock must be held
1830  */
1831 void
1832 fct_post_to_discovery_queue(fct_i_local_port_t *iport,
1833     fct_i_remote_port_t *irp, fct_i_cmd_t *icmd)
1834 {
1835 	fct_i_cmd_t	**p;
1836 
1837 	ASSERT(!MUTEX_HELD(&iport->iport_worker_lock));
1838 	if (icmd) {
1839 		icmd->icmd_next = NULL;
1840 		for (p = &irp->irp_els_list; *p != NULL;
1841 		    p = &((*p)->icmd_next))
1842 			;
1843 
1844 		*p = icmd;
1845 		atomic_or_32(&icmd->icmd_flags, ICMD_IN_IRP_QUEUE);
1846 	}
1847 
1848 	mutex_enter(&iport->iport_worker_lock);
1849 	if ((irp->irp_flags & IRP_IN_DISCOVERY_QUEUE) == 0) {
1850 
1851 		/*
1852 		 * CAUTION: do not grab local_port/remote_port locks after
1853 		 * grabbing the worker lock.
1854 		 */
1855 		irp->irp_discovery_next = NULL;
1856 		if (iport->iport_rpwe_tail) {
1857 			iport->iport_rpwe_tail->irp_discovery_next = irp;
1858 			iport->iport_rpwe_tail = irp;
1859 		} else {
1860 			iport->iport_rpwe_head = iport->iport_rpwe_tail = irp;
1861 		}
1862 
1863 		atomic_or_32(&irp->irp_flags, IRP_IN_DISCOVERY_QUEUE);
1864 	}
1865 
1866 	/*
1867 	 * We need always signal the port worker irrespective of the fact that
1868 	 * irp is already in discovery queue or not.
1869 	 */
1870 	if (IS_WORKER_SLEEPING(iport)) {
1871 		cv_signal(&iport->iport_worker_cv);
1872 	}
1873 	mutex_exit(&iport->iport_worker_lock);
1874 }
1875 
1876 stmf_status_t
1877 fct_xfer_scsi_data(scsi_task_t *task, stmf_data_buf_t *dbuf, uint32_t ioflags)
1878 {
1879 	fct_cmd_t *cmd = (fct_cmd_t *)task->task_port_private;
1880 
1881 	DTRACE_FC_5(xfer__start,
1882 	    fct_cmd_t, cmd,
1883 	    fct_i_local_port_t, cmd->cmd_port->port_fct_private,
1884 	    scsi_task_t, task,
1885 	    fct_i_remote_port_t, cmd->cmd_rp->rp_fct_private,
1886 	    stmf_data_buf_t, dbuf);
1887 
1888 	return (cmd->cmd_port->port_xfer_scsi_data(cmd, dbuf, ioflags));
1889 }
1890 
1891 void
1892 fct_scsi_data_xfer_done(fct_cmd_t *cmd, stmf_data_buf_t *dbuf, uint32_t ioflags)
1893 {
1894 	fct_i_cmd_t	*icmd = (fct_i_cmd_t *)cmd->cmd_fct_private;
1895 	uint32_t	old, new;
1896 	uint32_t	iof = 0;
1897 
1898 	DTRACE_FC_5(xfer__done,
1899 	    fct_cmd_t, cmd,
1900 	    fct_i_local_port_t, cmd->cmd_port->port_fct_private,
1901 	    scsi_task_t, ((scsi_task_t *)cmd->cmd_specific),
1902 	    fct_i_remote_port_t, cmd->cmd_rp->rp_fct_private,
1903 	    stmf_data_buf_t, dbuf);
1904 
1905 	if (ioflags & FCT_IOF_FCA_DONE) {
1906 		do {
1907 			old = new = icmd->icmd_flags;
1908 			if (old & ICMD_BEING_ABORTED) {
1909 				return;
1910 			}
1911 			new &= ~ICMD_KNOWN_TO_FCA;
1912 		} while (atomic_cas_32(&icmd->icmd_flags, old, new) != old);
1913 		iof = STMF_IOF_LPORT_DONE;
1914 		cmd->cmd_comp_status = dbuf->db_xfer_status;
1915 	}
1916 
1917 	if (icmd->icmd_flags & ICMD_BEING_ABORTED)
1918 		return;
1919 	stmf_data_xfer_done((scsi_task_t *)cmd->cmd_specific, dbuf, iof);
1920 }
1921 
1922 stmf_status_t
1923 fct_send_scsi_status(scsi_task_t *task, uint32_t ioflags)
1924 {
1925 	fct_cmd_t *cmd = (fct_cmd_t *)task->task_port_private;
1926 
1927 	DTRACE_FC_4(scsi__response,
1928 	    fct_cmd_t, cmd,
1929 	    fct_i_local_port_t,
1930 	    (fct_i_local_port_t *)cmd->cmd_port->port_fct_private,
1931 	    scsi_task_t, task,
1932 	    fct_i_remote_port_t,
1933 	    (fct_i_remote_port_t *)cmd->cmd_rp->rp_fct_private);
1934 
1935 	return (cmd->cmd_port->port_send_cmd_response(cmd, ioflags));
1936 }
1937 
1938 void
1939 fct_send_response_done(fct_cmd_t *cmd, fct_status_t s, uint32_t ioflags)
1940 {
1941 	fct_i_cmd_t	*icmd = (fct_i_cmd_t *)cmd->cmd_fct_private;
1942 	fct_local_port_t *port = cmd->cmd_port;
1943 	fct_i_local_port_t *iport = (fct_i_local_port_t *)
1944 	    port->port_fct_private;
1945 	uint32_t old, new;
1946 
1947 	if ((ioflags & FCT_IOF_FCA_DONE) == 0) {
1948 		/* Until we support confirmed completions, this is an error */
1949 		fct_queue_cmd_for_termination(cmd, s);
1950 		return;
1951 	}
1952 	do {
1953 		old = new = icmd->icmd_flags;
1954 		if (old & ICMD_BEING_ABORTED) {
1955 			return;
1956 		}
1957 		new &= ~ICMD_KNOWN_TO_FCA;
1958 	} while (atomic_cas_32(&icmd->icmd_flags, old, new) != old);
1959 
1960 	cmd->cmd_comp_status = s;
1961 	if (cmd->cmd_type == FCT_CMD_FCP_XCHG) {
1962 		stmf_send_status_done((scsi_task_t *)cmd->cmd_specific, s,
1963 		    STMF_IOF_LPORT_DONE);
1964 		return;
1965 	}
1966 
1967 	if (cmd->cmd_type == FCT_CMD_RCVD_ELS) {
1968 		fct_cmd_free(cmd);
1969 		return;
1970 	} else if (cmd->cmd_type == FCT_CMD_SOL_ELS) {
1971 		fct_handle_sol_els_completion(iport, icmd);
1972 	} else if (cmd->cmd_type == FCT_CMD_SOL_CT) {
1973 		/* Tell the caller that we are done */
1974 		atomic_or_32(&icmd->icmd_flags, ICMD_CMD_COMPLETE);
1975 	} else {
1976 		ASSERT(0);
1977 	}
1978 }
1979 
1980 void
1981 fct_cmd_free(fct_cmd_t *cmd)
1982 {
1983 	char			info[80];
1984 	fct_i_cmd_t		*icmd = (fct_i_cmd_t *)cmd->cmd_fct_private;
1985 	fct_local_port_t	*port = cmd->cmd_port;
1986 	fct_i_local_port_t	*iport = (fct_i_local_port_t *)
1987 	    port->port_fct_private;
1988 	fct_i_remote_port_t	*irp = NULL;
1989 	int			do_abts_acc = 0;
1990 	uint32_t		old, new;
1991 
1992 	ASSERT(!mutex_owned(&iport->iport_worker_lock));
1993 	/* Give the slot back */
1994 	if (CMD_HANDLE_VALID(cmd->cmd_handle)) {
1995 		uint16_t n = CMD_HANDLE_SLOT_INDEX(cmd->cmd_handle);
1996 		fct_cmd_slot_t *slot;
1997 
1998 		/*
1999 		 * If anything went wrong, grab the lock as writer. This is
2000 		 * probably unnecessary.
2001 		 */
2002 		if ((cmd->cmd_comp_status != FCT_SUCCESS) ||
2003 		    (icmd->icmd_flags & ICMD_ABTS_RECEIVED)) {
2004 			rw_enter(&iport->iport_lock, RW_WRITER);
2005 		} else {
2006 			rw_enter(&iport->iport_lock, RW_READER);
2007 		}
2008 
2009 		if ((icmd->icmd_flags & ICMD_ABTS_RECEIVED) &&
2010 		    (cmd->cmd_link != NULL)) {
2011 			do_abts_acc = 1;
2012 		}
2013 
2014 		/* XXX Validate slot before freeing */
2015 
2016 		slot = &iport->iport_cmd_slots[n];
2017 		slot->slot_uniq_cntr++;
2018 		slot->slot_cmd = NULL;
2019 		do {
2020 			old = iport->iport_next_free_slot;
2021 			slot->slot_next = old & 0xFFFF;
2022 			new = (old + 0x10000) & 0xFFFF0000;
2023 			new |= slot->slot_no;
2024 		} while (atomic_cas_32(&iport->iport_next_free_slot,
2025 		    old, new) != old);
2026 		cmd->cmd_handle = 0;
2027 		atomic_add_16(&iport->iport_nslots_free, 1);
2028 		if (cmd->cmd_rp) {
2029 			irp = (fct_i_remote_port_t *)
2030 			    cmd->cmd_rp->rp_fct_private;
2031 			if (cmd->cmd_type == FCT_CMD_FCP_XCHG)
2032 				atomic_add_16(&irp->irp_fcp_xchg_count, -1);
2033 			else
2034 				atomic_add_16(&irp->irp_nonfcp_xchg_count, -1);
2035 		}
2036 		rw_exit(&iport->iport_lock);
2037 	} else if ((icmd->icmd_flags & ICMD_IMPLICIT) &&
2038 	    (icmd->icmd_flags & ICMD_IMPLICIT_CMD_HAS_RESOURCE)) {
2039 		/* for implicit cmd, no cmd slot is used */
2040 		if (cmd->cmd_rp) {
2041 			irp = (fct_i_remote_port_t *)
2042 			    cmd->cmd_rp->rp_fct_private;
2043 			if (cmd->cmd_type == FCT_CMD_FCP_XCHG)
2044 				atomic_add_16(&irp->irp_fcp_xchg_count, -1);
2045 			else
2046 				atomic_add_16(&irp->irp_nonfcp_xchg_count, -1);
2047 		}
2048 	}
2049 
2050 	if (do_abts_acc) {
2051 		fct_cmd_t *lcmd = cmd->cmd_link;
2052 		fct_fill_abts_acc(lcmd);
2053 		if (port->port_send_cmd_response(lcmd,
2054 		    FCT_IOF_FORCE_FCA_DONE) != FCT_SUCCESS) {
2055 			/*
2056 			 * XXX Throw HBA fatal error event
2057 			 * Later shutdown svc will terminate the ABTS in the end
2058 			 */
2059 			(void) snprintf(info, 80,
2060 			    "fct_cmd_free: iport-%p, ABTS_ACC"
2061 			    " port_send_cmd_response failed", (void *)iport);
2062 			info[79] = 0;
2063 			(void) fct_port_shutdown(iport->iport_port,
2064 			    STMF_RFLAG_FATAL_ERROR | STMF_RFLAG_RESET, info);
2065 			return;
2066 		} else {
2067 			fct_cmd_free(lcmd);
2068 			cmd->cmd_link = NULL;
2069 		}
2070 	}
2071 
2072 	/* Free the cmd */
2073 	if (cmd->cmd_type == FCT_CMD_FCP_XCHG) {
2074 		if (iport->iport_cached_ncmds < max_cached_ncmds) {
2075 			icmd->icmd_flags = 0;
2076 			mutex_enter(&iport->iport_cached_cmd_lock);
2077 			icmd->icmd_next = iport->iport_cached_cmdlist;
2078 			iport->iport_cached_cmdlist = icmd;
2079 			iport->iport_cached_ncmds++;
2080 			mutex_exit(&iport->iport_cached_cmd_lock);
2081 		} else {
2082 			atomic_add_32(&iport->iport_total_alloced_ncmds, -1);
2083 			fct_free(cmd);
2084 		}
2085 	} else {
2086 		fct_free(cmd);
2087 	}
2088 }
2089 
2090 /* ARGSUSED */
2091 stmf_status_t
2092 fct_scsi_abort(stmf_local_port_t *lport, int abort_cmd, void *arg,
2093 							uint32_t flags)
2094 {
2095 	stmf_status_t ret = STMF_SUCCESS;
2096 	scsi_task_t *task;
2097 	fct_cmd_t *cmd;
2098 	fct_i_cmd_t *icmd;
2099 	fct_local_port_t *port;
2100 	uint32_t old, new;
2101 
2102 	ASSERT(abort_cmd == STMF_LPORT_ABORT_TASK);
2103 
2104 	task = (scsi_task_t *)arg;
2105 	cmd = (fct_cmd_t *)task->task_port_private;
2106 	icmd = (fct_i_cmd_t *)cmd->cmd_fct_private;
2107 	port = (fct_local_port_t *)lport->lport_port_private;
2108 
2109 	do {
2110 		old = new = icmd->icmd_flags;
2111 		if ((old & ICMD_KNOWN_TO_FCA) == 0)
2112 			return (STMF_NOT_FOUND);
2113 		ASSERT((old & ICMD_FCA_ABORT_CALLED) == 0);
2114 		new |= ICMD_BEING_ABORTED | ICMD_FCA_ABORT_CALLED;
2115 	} while (atomic_cas_32(&icmd->icmd_flags, old, new) != old);
2116 	ret = port->port_abort_cmd(port, cmd, 0);
2117 	if ((ret == FCT_NOT_FOUND) || (ret == FCT_ABORT_SUCCESS)) {
2118 		atomic_and_32(&icmd->icmd_flags, ~ICMD_KNOWN_TO_FCA);
2119 	} else if (ret == FCT_BUSY) {
2120 		atomic_and_32(&icmd->icmd_flags, ~ICMD_FCA_ABORT_CALLED);
2121 	}
2122 
2123 	return (ret);
2124 }
2125 
2126 void
2127 fct_ctl(struct stmf_local_port *lport, int cmd, void *arg)
2128 {
2129 	fct_local_port_t *port;
2130 	fct_i_local_port_t *iport;
2131 	stmf_change_status_t st;
2132 	stmf_change_status_t *pst;
2133 
2134 	ASSERT((cmd == STMF_CMD_LPORT_ONLINE) ||
2135 	    (cmd == STMF_ACK_LPORT_ONLINE_COMPLETE) ||
2136 	    (cmd == STMF_CMD_LPORT_OFFLINE) ||
2137 	    (cmd == STMF_ACK_LPORT_OFFLINE_COMPLETE) ||
2138 	    (cmd == FCT_CMD_PORT_ONLINE_COMPLETE) ||
2139 	    (cmd == FCT_CMD_PORT_OFFLINE_COMPLETE));
2140 
2141 	port = (fct_local_port_t *)lport->lport_port_private;
2142 	pst = (stmf_change_status_t *)arg;
2143 	st.st_completion_status = STMF_SUCCESS;
2144 	st.st_additional_info = NULL;
2145 
2146 	iport = (fct_i_local_port_t *)port->port_fct_private;
2147 	/*
2148 	 * We are mostly a passthrough, except during offline.
2149 	 */
2150 	switch (cmd) {
2151 	case STMF_CMD_LPORT_ONLINE:
2152 		if (iport->iport_state == FCT_STATE_ONLINE)
2153 			st.st_completion_status = STMF_ALREADY;
2154 		else if (iport->iport_state != FCT_STATE_OFFLINE)
2155 			st.st_completion_status = STMF_INVALID_ARG;
2156 		if (st.st_completion_status != STMF_SUCCESS) {
2157 			(void) stmf_ctl(STMF_CMD_LPORT_ONLINE_COMPLETE, lport,
2158 			    &st);
2159 			break;
2160 		}
2161 		iport->iport_state_not_acked = 1;
2162 		iport->iport_state = FCT_STATE_ONLINING;
2163 		port->port_ctl(port, FCT_CMD_PORT_ONLINE, arg);
2164 		break;
2165 	case FCT_CMD_PORT_ONLINE_COMPLETE:
2166 		ASSERT(iport->iport_state == FCT_STATE_ONLINING);
2167 		if (pst->st_completion_status != FCT_SUCCESS) {
2168 			iport->iport_state = FCT_STATE_OFFLINE;
2169 			iport->iport_state_not_acked = 0;
2170 		} else {
2171 			iport->iport_state = FCT_STATE_ONLINE;
2172 		}
2173 		(void) stmf_ctl(STMF_CMD_LPORT_ONLINE_COMPLETE, lport, arg);
2174 		break;
2175 	case STMF_ACK_LPORT_ONLINE_COMPLETE:
2176 		ASSERT(iport->iport_state == FCT_STATE_ONLINE);
2177 		iport->iport_state_not_acked = 0;
2178 		port->port_ctl(port, FCT_ACK_PORT_ONLINE_COMPLETE, arg);
2179 		break;
2180 
2181 	case STMF_CMD_LPORT_OFFLINE:
2182 		if (iport->iport_state == FCT_STATE_OFFLINE)
2183 			st.st_completion_status = STMF_ALREADY;
2184 		else if (iport->iport_state != FCT_STATE_ONLINE)
2185 			st.st_completion_status = STMF_INVALID_ARG;
2186 		if (st.st_completion_status != STMF_SUCCESS) {
2187 			(void) stmf_ctl(STMF_CMD_LPORT_OFFLINE_COMPLETE, lport,
2188 			    &st);
2189 			break;
2190 		}
2191 		iport->iport_state_not_acked = 1;
2192 		iport->iport_state = FCT_STATE_OFFLINING;
2193 		port->port_ctl(port, FCT_CMD_PORT_OFFLINE, arg);
2194 		break;
2195 	case FCT_CMD_PORT_OFFLINE_COMPLETE:
2196 		ASSERT(iport->iport_state == FCT_STATE_OFFLINING);
2197 		if (pst->st_completion_status != FCT_SUCCESS) {
2198 			iport->iport_state = FCT_STATE_ONLINE;
2199 			iport->iport_state_not_acked = 0;
2200 			(void) stmf_ctl(STMF_CMD_LPORT_OFFLINE_COMPLETE, lport,
2201 			    pst);
2202 			break;
2203 		}
2204 
2205 		/*
2206 		 * If FCA's offline was successful, we dont tell stmf yet.
2207 		 * Becasue now we have to do the cleanup before we go upto
2208 		 * stmf. That cleanup is done by the worker thread.
2209 		 */
2210 
2211 		/* FCA is offline, post a link down, its harmless anyway */
2212 		fct_handle_event(port, FCT_EVENT_LINK_DOWN, 0, 0);
2213 
2214 		/* Trigger port offline processing by the worker */
2215 		iport->iport_offline_prstate = FCT_OPR_START;
2216 		break;
2217 	case STMF_ACK_LPORT_OFFLINE_COMPLETE:
2218 		ASSERT(iport->iport_state == FCT_STATE_OFFLINE);
2219 		iport->iport_state_not_acked = 0;
2220 		port->port_ctl(port, FCT_ACK_PORT_OFFLINE_COMPLETE, arg);
2221 		break;
2222 	}
2223 }
2224 
2225 /* ARGSUSED */
2226 stmf_status_t
2227 fct_info(uint32_t cmd, stmf_local_port_t *lport, void *arg, uint8_t *buf,
2228 						uint32_t *bufsizep)
2229 {
2230 	return (STMF_NOT_SUPPORTED);
2231 }
2232 
2233 /*
2234  * implicit: if it's true, it means it will only be used in fct module, or else
2235  * it will be sent to the link.
2236  */
2237 fct_cmd_t *
2238 fct_create_solels(fct_local_port_t *port, fct_remote_port_t *rp, int implicit,
2239     uchar_t elsop, uint32_t wkdid, fct_icmd_cb_t icmdcb)
2240 {
2241 	fct_cmd_t		*cmd	= NULL;
2242 	fct_i_cmd_t		*icmd	= NULL;
2243 	fct_els_t		*els	= NULL;
2244 	fct_i_remote_port_t	*irp	= NULL;
2245 	uint8_t			*p	= NULL;
2246 	uint32_t		 ptid	= 0;
2247 
2248 	cmd = (fct_cmd_t *)fct_alloc(FCT_STRUCT_CMD_SOL_ELS,
2249 	    port->port_fca_sol_els_private_size, 0);
2250 	if (!cmd) {
2251 		return (NULL);
2252 	}
2253 
2254 	if (rp) {
2255 		irp = RP_TO_IRP(rp);
2256 	} else if (((irp = fct_portid_to_portptr(PORT_TO_IPORT(port),
2257 	    wkdid)) == NULL) && (elsop != ELS_OP_PLOGI)) {
2258 		stmf_trace(PORT_TO_IPORT(port)->iport_alias,
2259 		    "fct_create_solels: Must PLOGI to %x first", wkdid);
2260 		fct_free(cmd);
2261 		return (NULL);
2262 	}
2263 
2264 	cmd->cmd_port	= port;
2265 	cmd->cmd_oxid	= PTR2INT(cmd, uint16_t);
2266 	cmd->cmd_rxid	= 0xFFFF;
2267 	cmd->cmd_handle = 0;
2268 	icmd		= CMD_TO_ICMD(cmd);
2269 	els		= ICMD_TO_ELS(icmd);
2270 	icmd->icmd_cb	= icmdcb;
2271 	if (irp) {
2272 		cmd->cmd_rp	   = irp->irp_rp;
2273 		cmd->cmd_rp_handle = irp->irp_rp->rp_handle;
2274 		cmd->cmd_rportid   = irp->irp_rp->rp_id;
2275 	} else {
2276 		cmd->cmd_rp_handle = FCT_HANDLE_NONE;
2277 		cmd->cmd_rportid   = wkdid;
2278 	}
2279 	cmd->cmd_lportid = (PORT_TO_IPORT(port))->iport_link_info.portid;
2280 
2281 	if (implicit) {
2282 		/*
2283 		 * Since we will not send it to FCA, so we only allocate space
2284 		 */
2285 		ASSERT(elsop & (ELS_OP_LOGO | ELS_OP_PLOGI));
2286 		icmd->icmd_flags |= ICMD_IMPLICIT;
2287 		if (elsop == ELS_OP_LOGO) {
2288 			/*
2289 			 * Handling implicit LOGO should dependent on as less
2290 			 * as resources. So a trick here.
2291 			 */
2292 			els->els_req_size = 1;
2293 			els->els_req_payload = cmd->cmd_fca_private;
2294 		} else {
2295 			els->els_req_alloc_size = els->els_req_size = 116;
2296 			els->els_resp_alloc_size = els->els_resp_size = 116;
2297 			els->els_req_payload = (uint8_t *)
2298 			    kmem_zalloc(els->els_req_size, KM_SLEEP);
2299 			els->els_resp_payload = (uint8_t *)
2300 			    kmem_zalloc(els->els_resp_size, KM_SLEEP);
2301 		}
2302 	} else {
2303 		/*
2304 		 * Allocate space for its request and response
2305 		 * Fill the request payload according to spec.
2306 		 */
2307 		switch (elsop) {
2308 		case ELS_OP_LOGO:
2309 			els->els_resp_alloc_size = els->els_resp_size = 4;
2310 			els->els_resp_payload = (uint8_t *)kmem_zalloc(
2311 			    els->els_resp_size, KM_SLEEP);
2312 			els->els_req_alloc_size = els->els_req_size = 16;
2313 			els->els_req_payload = (uint8_t *)kmem_zalloc(
2314 			    els->els_req_size, KM_SLEEP);
2315 			ptid = PORT_TO_IPORT(port)->iport_link_info.portid;
2316 			fct_value_to_netbuf(ptid, els->els_req_payload + 5, 3);
2317 			bcopy(port->port_pwwn, els->els_req_payload + 8, 8);
2318 			break;
2319 
2320 		case ELS_OP_RSCN:
2321 			els->els_resp_alloc_size = els->els_resp_size = 4;
2322 			els->els_resp_payload = (uint8_t *)kmem_zalloc(
2323 			    els->els_resp_size, KM_SLEEP);
2324 			els->els_req_size = els->els_req_alloc_size = 8;
2325 			els->els_req_payload = (uint8_t *)kmem_zalloc(
2326 			    els->els_req_size, KM_SLEEP);
2327 			els->els_req_payload[1] = 0x04;
2328 			els->els_req_payload[3] = 0x08;
2329 			els->els_req_payload[4] |= 0x80;
2330 			ptid = PORT_TO_IPORT(port)->iport_link_info.portid;
2331 			fct_value_to_netbuf(ptid, els->els_req_payload + 5, 3);
2332 			break;
2333 
2334 		case ELS_OP_PLOGI:
2335 			els->els_resp_alloc_size = els->els_resp_size = 116;
2336 			els->els_resp_payload = (uint8_t *)
2337 			    kmem_zalloc(els->els_resp_size, KM_SLEEP);
2338 			els->els_req_alloc_size = els->els_req_size = 116;
2339 			p = els->els_req_payload = (uint8_t *)
2340 			    kmem_zalloc(els->els_req_size, KM_SLEEP);
2341 			bcopy(port->port_pwwn, p + 20, 8);
2342 			bcopy(port->port_nwwn, p + 28, 8);
2343 
2344 			/*
2345 			 * Common service parameters
2346 			 */
2347 			p[0x04] = 0x09;		/* high version */
2348 			p[0x05] = 0x08;		/* low version */
2349 			p[0x06] = 0x00;		/* BB credit: 0x0065 */
2350 			p[0x07] = 0x65;
2351 
2352 			/* CI0: Continuously Increasing Offset - 1 */
2353 			/* RRO: Randomly Relative Offset - 0 */
2354 			/* VVV: Vendor Version Level - 0 */
2355 			/* N-F: N or F Port Payload Sender - 0 (N) */
2356 			/* BBM: BB Credit Management - 0 (Normal) */
2357 			p[0x08] = 0x80;
2358 			p[0x09] = 0x00;
2359 
2360 			/* Max RX size */
2361 			p[0x0A] = 0x08;
2362 			p[0x0B] = 0x00;
2363 
2364 			/* NPTCS: N Port Total Concurrent Sequences - 0x0000 */
2365 			p[0x0C] = 0x00;
2366 			p[0x0D] = 0x00;
2367 
2368 			/* ROIC: Relative Offset By Info - 0xFFFF */
2369 			p[0x0E] = 0xFF;
2370 			p[0x0F] = 0xFF;
2371 
2372 			/* EDTOV: Error Detect Timeout - 0x000007D0 */
2373 			p[0x10] = 0x00;
2374 			p[0x11] = 0x00;
2375 			p[0x12] = 0x07;
2376 			p[0x13] = 0xD0;
2377 
2378 			/*
2379 			 * Class-3 Parameters
2380 			 */
2381 			/* C3-VAL: Class 3 Value - 1 */
2382 			/* C3-XID: X_ID Reassignment - 0 */
2383 			/* C3-IPA: Initial Process Assignment */
2384 			/* C3-AI-DCC: Data compression capable */
2385 			/* C3-AI-DC-HB: Data compression history buffer size */
2386 			/* C3-AI-DCE: Data encrytion capable */
2387 			/* C3-AI-CSC: Clock synchronization capable */
2388 			/* C3-ErrPol: Error pliciy */
2389 			/* C3-CatSeq: Information Cat. Per Sequence */
2390 			/* C3-AR-DCC: */
2391 			/* C3-AR-DC-HB: */
2392 			/* C3-AR-DCE: */
2393 			/* C3-AR-CSC */
2394 			p[0x44] = 0x80;
2395 			p[0x45] = 0x00;
2396 			p[0x46] = 0x00;
2397 			p[0x47] = 0x00;
2398 			p[0x48] = 0x00;
2399 			p[0x49] = 0x00;
2400 
2401 			/* C3-RxSize: Class 3 receive data size */
2402 			p[0x4A] = 0x08;
2403 			p[0x4B] = 0x00;
2404 
2405 			/* C3-ConSeq: Class 3 Concourrent sequences */
2406 			p[0x4C] = 0x00;
2407 			p[0x4D] = 0xFF;
2408 
2409 			/* C3-OSPE: Class 3 open sequence per exchange */
2410 			p[0x50] = 0x00;
2411 			p[0x51] = 0x01;
2412 
2413 			break;
2414 
2415 		case ELS_OP_SCR:
2416 			els->els_resp_alloc_size = els->els_resp_size = 4;
2417 			els->els_resp_payload = (uint8_t *)
2418 			    kmem_zalloc(els->els_resp_size, KM_SLEEP);
2419 			els->els_req_alloc_size = els->els_req_size = 8;
2420 			p = els->els_req_payload = (uint8_t *)
2421 			    kmem_zalloc(els->els_req_size, KM_SLEEP);
2422 			p[7] = FC_SCR_FULL_REGISTRATION;
2423 			break;
2424 		case ELS_OP_RLS:
2425 			els->els_resp_alloc_size = els->els_resp_size = 28;
2426 			els->els_resp_payload = (uint8_t *)
2427 			    kmem_zalloc(els->els_resp_size, KM_SLEEP);
2428 			els->els_req_alloc_size = els->els_req_size = 8;
2429 			p = els->els_req_payload = (uint8_t *)
2430 			    kmem_zalloc(els->els_req_size, KM_SLEEP);
2431 			ptid = PORT_TO_IPORT(port)->iport_link_info.portid;
2432 			fct_value_to_netbuf(ptid, els->els_req_payload + 5, 3);
2433 			break;
2434 
2435 		default:
2436 			ASSERT(0);
2437 		}
2438 	}
2439 
2440 	els->els_req_payload[0] = elsop;
2441 	return (cmd);
2442 }
2443 
2444 fct_cmd_t *
2445 fct_create_solct(fct_local_port_t *port, fct_remote_port_t *query_rp,
2446     uint16_t ctop, fct_icmd_cb_t icmdcb)
2447 {
2448 	fct_cmd_t		*cmd	 = NULL;
2449 	fct_i_cmd_t		*icmd	 = NULL;
2450 	fct_sol_ct_t		*ct	 = NULL;
2451 	uint8_t			*p	 = NULL;
2452 	fct_i_remote_port_t	*irp	 = NULL;
2453 	fct_i_local_port_t	*iport	 = NULL;
2454 	char			*nname	 = NULL;
2455 	int			 namelen = 0;
2456 
2457 	/*
2458 	 * Allocate space
2459 	 */
2460 	cmd = fct_alloc(FCT_STRUCT_CMD_SOL_CT,
2461 	    port->port_fca_sol_ct_private_size, 0);
2462 	if (!cmd) {
2463 		return (NULL);
2464 	}
2465 
2466 	/*
2467 	 * We should have PLOGIed to the name server (0xFFFFFC)
2468 	 * Caution: this irp is not query_rp->rp_fct_private.
2469 	 */
2470 	irp = fct_portid_to_portptr((fct_i_local_port_t *)
2471 	    port->port_fct_private, FS_NAME_SERVER);
2472 	if (irp == NULL) {
2473 		stmf_trace(PORT_TO_IPORT(port)->iport_alias,
2474 		    "fct_create_solct: Must PLOGI name server first");
2475 		fct_free(cmd);
2476 		return (NULL);
2477 	}
2478 
2479 	cmd->cmd_port	   = port;
2480 	cmd->cmd_rp	   = irp->irp_rp;
2481 	cmd->cmd_rp_handle = irp->irp_rp->rp_handle;
2482 	cmd->cmd_rportid   = irp->irp_rp->rp_id;
2483 	cmd->cmd_lportid   = (PORT_TO_IPORT(port))->iport_link_info.portid;
2484 	cmd->cmd_oxid	   = PTR2INT(cmd, uint16_t);
2485 	cmd->cmd_rxid	   = 0xFFFF;
2486 	cmd->cmd_handle	   = 0;
2487 	icmd		   = CMD_TO_ICMD(cmd);
2488 	ct		   = ICMD_TO_CT(icmd);
2489 	icmd->icmd_cb	   = icmdcb;
2490 	iport		   = ICMD_TO_IPORT(icmd);
2491 
2492 	switch (ctop) {
2493 	case NS_GSNN_NN:
2494 		/*
2495 		 * Allocate max space for its sybolic name
2496 		 */
2497 		ct->ct_resp_alloc_size = ct->ct_resp_size = 272;
2498 		ct->ct_resp_payload = (uint8_t *)kmem_zalloc(ct->ct_resp_size,
2499 		    KM_SLEEP);
2500 
2501 		ct->ct_req_size = ct->ct_req_alloc_size = 24;
2502 		p = ct->ct_req_payload = (uint8_t *)kmem_zalloc(ct->ct_req_size,
2503 		    KM_SLEEP);
2504 
2505 		bcopy(query_rp->rp_nwwn, p + 16, 8);
2506 		break;
2507 
2508 	case NS_RNN_ID:
2509 		ct->ct_resp_alloc_size = ct->ct_resp_size = 16;
2510 		ct->ct_resp_payload = (uint8_t *)kmem_zalloc(ct->ct_resp_size,
2511 		    KM_SLEEP);
2512 		ct->ct_req_size = ct->ct_req_alloc_size = 28;
2513 		p = ct->ct_req_payload = (uint8_t *)kmem_zalloc(ct->ct_req_size,
2514 		    KM_SLEEP);
2515 
2516 		/*
2517 		 * Port Identifier
2518 		 */
2519 		p[17] = (iport->iport_link_info.portid >> 16) & 0xFF;
2520 		p[18] = (iport->iport_link_info.portid >>  8) & 0xFF;
2521 		p[19] = (iport->iport_link_info.portid >>  0) & 0xFF;
2522 
2523 		/*
2524 		 * Node Name
2525 		 */
2526 		bcopy(port->port_nwwn, p + 20, 8);
2527 		break;
2528 
2529 	case NS_RCS_ID:
2530 		ct->ct_resp_alloc_size = ct->ct_resp_size = 16;
2531 		ct->ct_resp_payload = (uint8_t *)kmem_zalloc(ct->ct_resp_size,
2532 		    KM_SLEEP);
2533 		ct->ct_req_size = ct->ct_req_alloc_size = 24;
2534 		p = ct->ct_req_payload = (uint8_t *)kmem_zalloc(ct->ct_req_size,
2535 		    KM_SLEEP);
2536 
2537 		/*
2538 		 * Port Identifier
2539 		 */
2540 		p[17] = (iport->iport_link_info.portid >> 16) & 0xFF;
2541 		p[18] = (iport->iport_link_info.portid >>  8) & 0xFF;
2542 		p[19] = (iport->iport_link_info.portid >>  0) & 0xFF;
2543 
2544 		/*
2545 		 * Class of Service
2546 		 */
2547 		*(p + 23) = FC_NS_CLASS3;
2548 		break;
2549 
2550 	case NS_RFT_ID:
2551 		ct->ct_resp_alloc_size = ct->ct_resp_size = 16;
2552 		ct->ct_resp_payload = (uint8_t *)kmem_zalloc(ct->ct_resp_size,
2553 		    KM_SLEEP);
2554 		ct->ct_req_size = ct->ct_req_alloc_size = 52;
2555 		p = ct->ct_req_payload = (uint8_t *)kmem_zalloc(ct->ct_req_size,
2556 		    KM_SLEEP);
2557 
2558 		/*
2559 		 * Port Identifier
2560 		 */
2561 		p[17] = (iport->iport_link_info.portid >> 16) & 0xFF;
2562 		p[18] = (iport->iport_link_info.portid >>  8) & 0xFF;
2563 		p[19] = (iport->iport_link_info.portid >>  0) & 0xFF;
2564 
2565 		/*
2566 		 * FC-4 Protocol Types
2567 		 */
2568 		*(p + 22) = 0x1;	/* 0x100 */
2569 		break;
2570 
2571 	case NS_RSPN_ID:
2572 		/*
2573 		 * If we get here, port->port_sym_port_name is always not NULL.
2574 		 */
2575 		ASSERT(port->port_sym_port_name);
2576 		namelen = strlen(port->port_sym_port_name);
2577 		ct->ct_resp_alloc_size = ct->ct_resp_size = 16;
2578 		ct->ct_resp_payload = (uint8_t *)kmem_zalloc(ct->ct_resp_size,
2579 		    KM_SLEEP);
2580 		ct->ct_req_size = ct->ct_req_alloc_size =
2581 		    (21 + namelen + 3) & ~3;
2582 		p = ct->ct_req_payload = (uint8_t *)kmem_zalloc(ct->ct_req_size,
2583 		    KM_SLEEP);
2584 
2585 		/*
2586 		 * Port Identifier
2587 		 */
2588 		p[17] = (iport->iport_link_info.portid >> 16) & 0xFF;
2589 		p[18] = (iport->iport_link_info.portid >>  8) & 0xFF;
2590 		p[19] = (iport->iport_link_info.portid >>  0) & 0xFF;
2591 
2592 		/*
2593 		 * String length
2594 		 */
2595 		p[20] = namelen;
2596 
2597 		/*
2598 		 * Symbolic port name
2599 		 */
2600 		bcopy(port->port_sym_port_name, p + 21, ct->ct_req_size - 21);
2601 		break;
2602 
2603 	case NS_RSNN_NN:
2604 		namelen = port->port_sym_node_name == NULL ?
2605 		    strlen(utsname.nodename) :
2606 		    strlen(port->port_sym_node_name);
2607 		nname = port->port_sym_node_name == NULL ?
2608 		    utsname.nodename : port->port_sym_node_name;
2609 
2610 		ct->ct_resp_alloc_size = ct->ct_resp_size = 16;
2611 		ct->ct_resp_payload = (uint8_t *)kmem_zalloc(ct->ct_resp_size,
2612 		    KM_SLEEP);
2613 		ct->ct_req_size = ct->ct_req_alloc_size =
2614 		    (25 + namelen + 3) & ~3;
2615 		p = ct->ct_req_payload = (uint8_t *)kmem_zalloc(ct->ct_req_size,
2616 		    KM_SLEEP);
2617 
2618 		/*
2619 		 * Node name
2620 		 */
2621 		bcopy(port->port_nwwn, p + 16, 8);
2622 
2623 		/*
2624 		 * String length
2625 		 */
2626 		p[24] = namelen;
2627 
2628 		/*
2629 		 * Symbolic node name
2630 		 */
2631 		bcopy(nname, p + 25, ct->ct_req_size - 25);
2632 		break;
2633 
2634 	case NS_GSPN_ID:
2635 		ct->ct_resp_alloc_size = ct->ct_resp_size = 272;
2636 		ct->ct_resp_payload = (uint8_t *)kmem_zalloc(ct->ct_resp_size,
2637 		    KM_SLEEP);
2638 		ct->ct_req_size = ct->ct_req_alloc_size = 20;
2639 		p = ct->ct_req_payload = (uint8_t *)kmem_zalloc(ct->ct_req_size,
2640 		    KM_SLEEP);
2641 		/*
2642 		 * Port Identifier
2643 		 */
2644 		p[17] = (query_rp->rp_id >> 16) & 0xFF;
2645 		p[18] = (query_rp->rp_id >>  8) & 0xFF;
2646 		p[19] = (query_rp->rp_id >>  0) & 0xFF;
2647 		break;
2648 
2649 	case NS_GCS_ID:
2650 		ct->ct_resp_alloc_size = ct->ct_resp_size = 20;
2651 		ct->ct_resp_payload = (uint8_t *)kmem_zalloc(ct->ct_resp_size,
2652 		    KM_SLEEP);
2653 		ct->ct_req_size = ct->ct_req_alloc_size = 20;
2654 		p = ct->ct_req_payload = (uint8_t *)kmem_zalloc(ct->ct_req_size,
2655 		    KM_SLEEP);
2656 		/*
2657 		 * Port Identifier
2658 		 */
2659 		p[17] = (query_rp->rp_id >> 16) & 0xFF;
2660 		p[18] = (query_rp->rp_id >>  8) & 0xFF;
2661 		p[19] = (query_rp->rp_id >>  0) & 0xFF;
2662 		break;
2663 
2664 	case NS_GFT_ID:
2665 		ct->ct_resp_alloc_size = ct->ct_resp_size = 48;
2666 		ct->ct_resp_payload = (uint8_t *)kmem_zalloc(ct->ct_resp_size,
2667 		    KM_SLEEP);
2668 		ct->ct_req_size = ct->ct_req_alloc_size = 20;
2669 		p = ct->ct_req_payload = (uint8_t *)kmem_zalloc(ct->ct_req_size,
2670 		    KM_SLEEP);
2671 		/*
2672 		 * Port Identifier
2673 		 */
2674 		p[17] = (query_rp->rp_id >> 16) & 0xFF;
2675 		p[18] = (query_rp->rp_id >>  8) & 0xFF;
2676 		p[19] = (query_rp->rp_id >>  0) & 0xFF;
2677 		break;
2678 
2679 	case NS_GID_PN:
2680 		ct->ct_resp_alloc_size = ct->ct_resp_size = 20;
2681 		ct->ct_resp_payload = (uint8_t *)kmem_zalloc(ct->ct_resp_size,
2682 		    KM_SLEEP);
2683 
2684 		ct->ct_req_size = ct->ct_req_alloc_size = 24;
2685 		p = ct->ct_req_payload = (uint8_t *)kmem_zalloc(ct->ct_req_size,
2686 		    KM_SLEEP);
2687 
2688 		bcopy(query_rp->rp_pwwn, p + 16, 8);
2689 		break;
2690 
2691 	default:
2692 		/* CONSTCOND */
2693 		ASSERT(0);
2694 	}
2695 
2696 	FCT_FILL_CTIU_PREAMPLE(p, ctop);
2697 	return (cmd);
2698 }
2699 
2700 /*
2701  * Cmd can only be solicited CT/ELS. They will be dispatched to the discovery
2702  * queue eventually too.
2703  * We queue solicited cmds here to track solicited cmds and to take full use
2704  * of single thread mechanism.
2705  * But in current implmentation, we don't use  this mechanism on SOL_CT, PLOGI.
2706  * To avoid to interrupt current flow, ICMD_IN_SOLCMD_QUEUE is used here.
2707  */
2708 void
2709 fct_post_to_solcmd_queue(fct_local_port_t *port, fct_cmd_t *cmd)
2710 {
2711 	fct_i_local_port_t	*iport	= (fct_i_local_port_t *)
2712 	    port->port_fct_private;
2713 	fct_i_cmd_t *icmd		= (fct_i_cmd_t *)cmd->cmd_fct_private;
2714 
2715 	mutex_enter(&iport->iport_worker_lock);
2716 	icmd->icmd_solcmd_next = iport->iport_solcmd_queue;
2717 	iport->iport_solcmd_queue = icmd;
2718 	atomic_or_32(&icmd->icmd_flags, ICMD_IN_SOLCMD_QUEUE | ICMD_SOLCMD_NEW);
2719 	if (IS_WORKER_SLEEPING(iport)) {
2720 		cv_signal(&iport->iport_worker_cv);
2721 	}
2722 	mutex_exit(&iport->iport_worker_lock);
2723 }
2724 
2725 /* ARGSUSED */
2726 void
2727 fct_event_handler(stmf_local_port_t *lport, int eventid, void *arg,
2728     uint32_t flags)
2729 {
2730 	fct_local_port_t	*port  = (fct_local_port_t *)
2731 	    lport->lport_port_private;
2732 	fct_i_local_port_t	*iport = (fct_i_local_port_t *)
2733 	    port->port_fct_private;
2734 	stmf_scsi_session_t	*ss;
2735 	fct_i_remote_port_t	*irp;
2736 
2737 	switch (eventid) {
2738 	case LPORT_EVENT_INITIAL_LUN_MAPPED:
2739 		ss = (stmf_scsi_session_t *)arg;
2740 		irp = (fct_i_remote_port_t *)ss->ss_port_private;
2741 		stmf_trace(iport->iport_alias,
2742 		    "Initial LUN mapped to session ss-%p, irp-%p", ss, irp);
2743 		break;
2744 
2745 	default:
2746 		stmf_trace(iport->iport_alias,
2747 		    "Unknown event received, %d", eventid);
2748 	}
2749 }
2750 
2751 void
2752 fct_send_cmd_done(fct_cmd_t *cmd, fct_status_t s, uint32_t ioflags)
2753 {
2754 	/* XXX For now just call send_resp_done() */
2755 	fct_send_response_done(cmd, s, ioflags);
2756 }
2757 
2758 void
2759 fct_cmd_fca_aborted(fct_cmd_t *cmd, fct_status_t s, uint32_t ioflags)
2760 {
2761 	fct_i_cmd_t		*icmd = (fct_i_cmd_t *)cmd->cmd_fct_private;
2762 	char			info[160];
2763 	unsigned long long	st;
2764 
2765 	st = s;	/* To make gcc happy */
2766 	ASSERT(icmd->icmd_flags & ICMD_BEING_ABORTED);
2767 	if ((((s != FCT_ABORT_SUCCESS) && (s != FCT_NOT_FOUND))) ||
2768 	    ((ioflags & FCT_IOF_FCA_DONE) == 0)) {
2769 		(void) snprintf(info, 160, "fct_cmd_fca_aborted: cmd-%p, "
2770 		    "s-%llx, iofalgs-%x", (void *)cmd, st, ioflags);
2771 		info[159] = 0;
2772 		(void) fct_port_shutdown(cmd->cmd_port,
2773 		    STMF_RFLAG_FATAL_ERROR | STMF_RFLAG_RESET, info);
2774 		return;
2775 	}
2776 
2777 	atomic_and_32(&icmd->icmd_flags, ~ICMD_KNOWN_TO_FCA);
2778 	/* For non FCP Rest of the work is done by the terminator */
2779 	/* For FCP stuff just call stmf */
2780 	if (cmd->cmd_type == FCT_CMD_FCP_XCHG) {
2781 		stmf_task_lport_aborted((scsi_task_t *)cmd->cmd_specific,
2782 		    s, STMF_IOF_LPORT_DONE);
2783 	}
2784 }
2785 
2786 /*
2787  * FCA drivers will use it, when they want to abort some FC transactions
2788  * due to lack of resource.
2789  */
2790 uint16_t
2791 fct_get_rp_handle(fct_local_port_t *port, uint32_t rportid)
2792 {
2793 	fct_i_remote_port_t	*irp;
2794 
2795 	irp = fct_portid_to_portptr(
2796 	    (fct_i_local_port_t *)(port->port_fct_private), rportid);
2797 	if (irp == NULL) {
2798 		return (0xFFFF);
2799 	} else {
2800 		return (irp->irp_rp->rp_handle);
2801 	}
2802 }
2803 
2804 fct_cmd_t *
2805 fct_handle_to_cmd(fct_local_port_t *port, uint32_t fct_handle)
2806 {
2807 	fct_cmd_slot_t *slot;
2808 	uint16_t ndx;
2809 
2810 	if (!CMD_HANDLE_VALID(fct_handle))
2811 		return (NULL);
2812 	if ((ndx = CMD_HANDLE_SLOT_INDEX(fct_handle)) >= port->port_max_xchges)
2813 		return (NULL);
2814 
2815 	slot = &((fct_i_local_port_t *)port->port_fct_private)->iport_cmd_slots[
2816 	    ndx];
2817 
2818 	if ((slot->slot_uniq_cntr | 0x80) != (fct_handle >> 24))
2819 		return (NULL);
2820 	return (slot->slot_cmd->icmd_cmd);
2821 }
2822 
2823 void
2824 fct_queue_scsi_task_for_termination(fct_cmd_t *cmd, fct_status_t s)
2825 {
2826 	fct_i_cmd_t *icmd = (fct_i_cmd_t *)cmd->cmd_fct_private;
2827 
2828 	uint32_t old, new;
2829 
2830 	do {
2831 		old = icmd->icmd_flags;
2832 		if ((old & (ICMD_BEING_ABORTED | ICMD_KNOWN_TO_FCA)) !=
2833 		    ICMD_KNOWN_TO_FCA)
2834 			return;
2835 		new = old | ICMD_BEING_ABORTED;
2836 	} while (atomic_cas_32(&icmd->icmd_flags, old, new) != old);
2837 	stmf_abort(STMF_QUEUE_TASK_ABORT, (scsi_task_t *)cmd->cmd_specific,
2838 	    s, NULL);
2839 }
2840 
2841 void
2842 fct_fill_abts_acc(fct_cmd_t *cmd)
2843 {
2844 	fct_rcvd_abts_t *abts = (fct_rcvd_abts_t *)cmd->cmd_specific;
2845 	uint8_t *p;
2846 
2847 	abts->abts_resp_rctl = BLS_OP_BA_ACC;
2848 	p = abts->abts_resp_payload;
2849 	bzero(p, 12);
2850 	*((uint16_t *)(p+4)) = BE_16(cmd->cmd_oxid);
2851 	*((uint16_t *)(p+6)) = BE_16(cmd->cmd_rxid);
2852 	p[10] = p[11] = 0xff;
2853 }
2854 
2855 void
2856 fct_handle_rcvd_abts(fct_cmd_t *cmd)
2857 {
2858 	char			info[80];
2859 	fct_local_port_t	*port = cmd->cmd_port;
2860 	fct_i_local_port_t	*iport =
2861 	    (fct_i_local_port_t *)port->port_fct_private;
2862 	fct_i_cmd_t		*icmd = (fct_i_cmd_t *)cmd->cmd_fct_private;
2863 	fct_i_remote_port_t	*irp;
2864 	fct_cmd_t		*c = NULL;
2865 	fct_i_cmd_t		*ic = NULL;
2866 	int			found = 0;
2867 	int			i;
2868 
2869 	icmd->icmd_start_time = ddi_get_lbolt();
2870 	icmd->icmd_flags |= ICMD_KNOWN_TO_FCA;
2871 
2872 	rw_enter(&iport->iport_lock, RW_WRITER);
2873 	/* Make sure local port is sane */
2874 	if ((iport->iport_link_state & S_LINK_ONLINE) == 0) {
2875 		rw_exit(&iport->iport_lock);
2876 		stmf_trace(iport->iport_alias, "ABTS not posted becasue"
2877 		    "port state was %x", iport->iport_link_state);
2878 		fct_queue_cmd_for_termination(cmd, FCT_LOCAL_PORT_OFFLINE);
2879 		return;
2880 	}
2881 
2882 	if (cmd->cmd_rp_handle == FCT_HANDLE_NONE)
2883 		irp = fct_portid_to_portptr(iport, cmd->cmd_rportid);
2884 	else if (cmd->cmd_rp_handle < port->port_max_logins)
2885 		irp = iport->iport_rp_slots[cmd->cmd_rp_handle];
2886 	else
2887 		irp = NULL;
2888 	if (irp == NULL) {
2889 		/* XXX Throw a logout to the initiator */
2890 		rw_exit(&iport->iport_lock);
2891 		stmf_trace(iport->iport_alias, "ABTS received from"
2892 		    " %x without a session", cmd->cmd_rportid);
2893 		fct_queue_cmd_for_termination(cmd, FCT_NOT_LOGGED_IN);
2894 		return;
2895 	}
2896 
2897 	DTRACE_FC_3(abts__receive,
2898 	    fct_cmd_t, cmd,
2899 	    fct_local_port_t, port,
2900 	    fct_i_remote_port_t, irp);
2901 
2902 	cmd->cmd_rp = irp->irp_rp;
2903 
2904 	/*
2905 	 * No need to allocate an xchg resource. ABTSes use the same
2906 	 * xchg resource as the cmd they are aborting.
2907 	 */
2908 	rw_enter(&irp->irp_lock, RW_WRITER);
2909 	mutex_enter(&iport->iport_worker_lock);
2910 	/* Lets find the command first */
2911 	for (i = 0; i < port->port_max_xchges; i++) {
2912 		if ((ic = iport->iport_cmd_slots[i].slot_cmd) == NULL)
2913 			continue;
2914 		if ((ic->icmd_flags & ICMD_KNOWN_TO_FCA) == 0)
2915 			continue;
2916 		c = ic->icmd_cmd;
2917 		if (!CMD_HANDLE_VALID(c->cmd_handle))
2918 			continue;
2919 		if ((c->cmd_rportid != cmd->cmd_rportid) ||
2920 		    (c->cmd_oxid != cmd->cmd_oxid))
2921 			continue;
2922 		/* Found the command */
2923 		found = 1;
2924 		break;
2925 	}
2926 	if (!found) {
2927 		mutex_exit(&iport->iport_worker_lock);
2928 		rw_exit(&irp->irp_lock);
2929 		rw_exit(&iport->iport_lock);
2930 		/* Dont even bother queueing it. Just respond */
2931 		fct_fill_abts_acc(cmd);
2932 		if (port->port_send_cmd_response(cmd,
2933 		    FCT_IOF_FORCE_FCA_DONE) != FCT_SUCCESS) {
2934 			/*
2935 			 * XXX Throw HBA fatal error event
2936 			 * Later shutdown svc will terminate the ABTS in the end
2937 			 */
2938 			(void) snprintf(info, 80,
2939 			    "fct_handle_rcvd_abts: iport-%p, "
2940 			    "ABTS_ACC port_send_cmd_response failed",
2941 			    (void *)iport);
2942 			info[79] = 0;
2943 			(void) fct_port_shutdown(iport->iport_port,
2944 			    STMF_RFLAG_FATAL_ERROR | STMF_RFLAG_RESET, info);
2945 		} else {
2946 			fct_cmd_free(cmd);
2947 		}
2948 		return;
2949 	}
2950 
2951 	/* Check if this an abts retry */
2952 	if (c->cmd_link && (ic->icmd_flags & ICMD_ABTS_RECEIVED)) {
2953 		/* Kill this abts. */
2954 		fct_q_for_termination_lock_held(iport, icmd, FCT_ABORTED);
2955 		if (IS_WORKER_SLEEPING(iport))
2956 			cv_signal(&iport->iport_worker_cv);
2957 		mutex_exit(&iport->iport_worker_lock);
2958 		rw_exit(&irp->irp_lock);
2959 		rw_exit(&iport->iport_lock);
2960 		return;
2961 	}
2962 	c->cmd_link = cmd;
2963 	atomic_or_32(&ic->icmd_flags, ICMD_ABTS_RECEIVED);
2964 	cmd->cmd_link = c;
2965 	mutex_exit(&iport->iport_worker_lock);
2966 	rw_exit(&irp->irp_lock);
2967 	fct_queue_cmd_for_termination(c, FCT_ABTS_RECEIVED);
2968 	rw_exit(&iport->iport_lock);
2969 }
2970 
2971 void
2972 fct_queue_cmd_for_termination(fct_cmd_t *cmd, fct_status_t s)
2973 {
2974 	fct_local_port_t *port = cmd->cmd_port;
2975 	fct_i_local_port_t *iport = (fct_i_local_port_t *)
2976 	    port->port_fct_private;
2977 	fct_i_cmd_t *icmd = (fct_i_cmd_t *)cmd->cmd_fct_private;
2978 
2979 	if (cmd->cmd_type == FCT_CMD_FCP_XCHG) {
2980 		fct_queue_scsi_task_for_termination(cmd, s);
2981 		return;
2982 	}
2983 	mutex_enter(&iport->iport_worker_lock);
2984 	fct_q_for_termination_lock_held(iport, icmd, s);
2985 	if (IS_WORKER_SLEEPING(iport))
2986 		cv_signal(&iport->iport_worker_cv);
2987 	mutex_exit(&iport->iport_worker_lock);
2988 }
2989 
2990 /*
2991  * This function will not be called for SCSI CMDS
2992  */
2993 void
2994 fct_q_for_termination_lock_held(fct_i_local_port_t *iport, fct_i_cmd_t *icmd,
2995 		fct_status_t s)
2996 {
2997 	uint32_t old, new;
2998 	fct_i_cmd_t **ppicmd;
2999 
3000 	do {
3001 		old = icmd->icmd_flags;
3002 		if (old & ICMD_BEING_ABORTED)
3003 			return;
3004 		new = old | ICMD_BEING_ABORTED;
3005 	} while (atomic_cas_32(&icmd->icmd_flags, old, new) != old);
3006 
3007 	icmd->icmd_start_time = ddi_get_lbolt();
3008 	icmd->icmd_cmd->cmd_comp_status = s;
3009 
3010 	icmd->icmd_next = NULL;
3011 	for (ppicmd = &(iport->iport_abort_queue); *ppicmd != NULL;
3012 	    ppicmd = &((*ppicmd)->icmd_next))
3013 		;
3014 
3015 	*ppicmd = icmd;
3016 }
3017 
3018 /*
3019  * For those cmds, for which we called fca_abort but it has not yet completed,
3020  * reset the FCA_ABORT_CALLED flag, so that abort can be called again.
3021  * This is done after a FCA offline. The reason is that after offline, the
3022  * firmware is not running so abort will never complete. But if we call it
3023  * again, the FCA will detect that it is not offline and it will
3024  * not call the firmware at all. Most likely it will abort in a synchronous
3025  * manner i.e. return FCT_ABORT_SUCCESS or FCT_NOT_FOUND.
3026  */
3027 void
3028 fct_reset_flag_abort_called(fct_i_local_port_t *iport)
3029 {
3030 	fct_i_cmd_t *icmd;
3031 	uint32_t old, new;
3032 	int i, do_clear;
3033 
3034 	ASSERT(mutex_owned(&iport->iport_worker_lock));
3035 	mutex_exit(&iport->iport_worker_lock);
3036 	rw_enter(&iport->iport_lock, RW_WRITER);
3037 	mutex_enter(&iport->iport_worker_lock);
3038 
3039 	for (i = 0; i < iport->iport_port->port_max_xchges; i++) {
3040 		if (iport->iport_cmd_slots[i].slot_cmd == NULL)
3041 			continue;
3042 
3043 		icmd = iport->iport_cmd_slots[i].slot_cmd;
3044 
3045 		do {
3046 			old = new = icmd->icmd_flags;
3047 			if ((old & (ICMD_KNOWN_TO_FCA |
3048 			    ICMD_FCA_ABORT_CALLED)) == (ICMD_KNOWN_TO_FCA |
3049 			    ICMD_FCA_ABORT_CALLED)) {
3050 				new &= ~ICMD_FCA_ABORT_CALLED;
3051 				do_clear = 1;
3052 			} else {
3053 				do_clear = 0;
3054 				break;
3055 			}
3056 		} while (atomic_cas_32(&icmd->icmd_flags, old, new) != old);
3057 		if (do_clear &&
3058 		    (icmd->icmd_cmd->cmd_type == FCT_CMD_FCP_XCHG)) {
3059 			stmf_abort(STMF_REQUEUE_TASK_ABORT_LPORT,
3060 			    icmd->icmd_cmd->cmd_specific, 0, NULL);
3061 		}
3062 	}
3063 
3064 	rw_exit(&iport->iport_lock);
3065 }
3066 
3067 /*
3068  * Modify the irp_deregister_timer such that the ports start deregistering
3069  * quickly.
3070  */
3071 void
3072 fct_irp_deregister_speedup(fct_i_local_port_t *iport)
3073 {
3074 	fct_i_remote_port_t *irp;
3075 	int i;
3076 
3077 	if (!iport->iport_nrps)
3078 		return;
3079 
3080 	for (i = 0; i < rportid_table_size; i++) {
3081 		irp = iport->iport_rp_tb[i];
3082 		while (irp) {
3083 			irp->irp_deregister_timer = ddi_get_lbolt() - 1;
3084 			irp = irp->irp_next;
3085 		}
3086 	}
3087 }
3088 
3089 disc_action_t
3090 fct_handle_port_offline(fct_i_local_port_t *iport)
3091 {
3092 	if (iport->iport_offline_prstate == FCT_OPR_START) {
3093 		fct_reset_flag_abort_called(iport);
3094 		iport->iport_offline_prstate = FCT_OPR_CMD_CLEANUP_WAIT;
3095 		/* fct_ctl has already submitted a link offline event */
3096 		return (DISC_ACTION_DELAY_RESCAN);
3097 	}
3098 	if (iport->iport_offline_prstate == FCT_OPR_CMD_CLEANUP_WAIT) {
3099 		if (iport->iport_link_state != PORT_STATE_LINK_DOWN)
3100 			return (DISC_ACTION_DELAY_RESCAN);
3101 		/*
3102 		 * All I/Os have been killed at this time. Lets speedup
3103 		 * the port deregister process.
3104 		 */
3105 		mutex_exit(&iport->iport_worker_lock);
3106 		rw_enter(&iport->iport_lock, RW_WRITER);
3107 		fct_irp_deregister_speedup(iport);
3108 		rw_exit(&iport->iport_lock);
3109 		mutex_enter(&iport->iport_worker_lock);
3110 		iport->iport_offline_prstate = FCT_OPR_INT_CLEANUP_WAIT;
3111 		return (DISC_ACTION_RESCAN);
3112 	}
3113 	if (iport->iport_offline_prstate == FCT_OPR_INT_CLEANUP_WAIT) {
3114 		stmf_change_status_t st;
3115 
3116 		if (iport->iport_solcmd_queue) {
3117 			return (DISC_ACTION_DELAY_RESCAN);
3118 		}
3119 
3120 		if (iport->iport_nrps) {
3121 			/*
3122 			 * A port logout may have gone when implicit logo all
3123 			 * was retried. So do the port speedup again here.
3124 			 */
3125 			mutex_exit(&iport->iport_worker_lock);
3126 			rw_enter(&iport->iport_lock, RW_WRITER);
3127 			fct_irp_deregister_speedup(iport);
3128 			rw_exit(&iport->iport_lock);
3129 			mutex_enter(&iport->iport_worker_lock);
3130 			return (DISC_ACTION_DELAY_RESCAN);
3131 		}
3132 
3133 		if (iport->iport_event_head != NULL) {
3134 			return (DISC_ACTION_DELAY_RESCAN);
3135 		}
3136 
3137 		st.st_completion_status = STMF_SUCCESS;
3138 		st.st_additional_info = NULL;
3139 		iport->iport_offline_prstate = FCT_OPR_DONE;
3140 		iport->iport_state = FCT_STATE_OFFLINE;
3141 		mutex_exit(&iport->iport_worker_lock);
3142 		(void) stmf_ctl(STMF_CMD_LPORT_OFFLINE_COMPLETE,
3143 		    iport->iport_port->port_lport, &st);
3144 		mutex_enter(&iport->iport_worker_lock);
3145 		return (DISC_ACTION_DELAY_RESCAN);
3146 	}
3147 
3148 	/* NOTREACHED */
3149 	return (0);
3150 }
3151 
3152 /*
3153  * See stmf.h for information on rflags. Additional info is just a text
3154  * description of the reason for this call. Additional_info can be NULL.
3155  * Also the caller can declare additional info on the stack. stmf_ctl
3156  * makes a copy of it before returning.
3157  */
3158 fct_status_t
3159 fct_port_initialize(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_initialize: port-%p, %s", port,
3167 	    additional_info? additional_info : "no more information");
3168 	return (stmf_ctl(STMF_CMD_LPORT_ONLINE, port->port_lport, &st));
3169 }
3170 
3171 fct_status_t
3172 fct_port_shutdown(fct_local_port_t *port, uint32_t rflags,
3173 				char *additional_info)
3174 {
3175 	stmf_state_change_info_t st;
3176 
3177 	st.st_rflags = rflags;
3178 	st.st_additional_info = additional_info;
3179 	stmf_trace(NULL, "fct_port_shutdown: port-%p, %s", port,
3180 	    additional_info? additional_info : "no more information");
3181 	return (stmf_ctl(STMF_CMD_LPORT_OFFLINE, port->port_lport, &st));
3182 }
3183 
3184 /*
3185  * Called by worker thread. The aim is to terminate the command
3186  * using whatever means it takes.
3187  * Called with worker lock held.
3188  */
3189 disc_action_t
3190 fct_cmd_terminator(fct_i_local_port_t *iport)
3191 {
3192 	char			info[80];
3193 	clock_t			endtime;
3194 	fct_i_cmd_t		**ppicmd;
3195 	fct_i_cmd_t		*icmd;
3196 	fct_cmd_t		*cmd;
3197 	fct_local_port_t	*port = iport->iport_port;
3198 	disc_action_t		ret = DISC_ACTION_NO_WORK;
3199 	fct_status_t		abort_ret;
3200 	int			fca_done, fct_done, cmd_implicit = 0;
3201 	int			flags;
3202 	unsigned long long	st;
3203 
3204 	/* Lets Limit each run to 20ms max. */
3205 	endtime = ddi_get_lbolt() + drv_usectohz(20000);
3206 
3207 	/* Start from where we left off last time */
3208 	if (iport->iport_ppicmd_term) {
3209 		ppicmd = iport->iport_ppicmd_term;
3210 		iport->iport_ppicmd_term = NULL;
3211 	} else {
3212 		ppicmd = &iport->iport_abort_queue;
3213 	}
3214 
3215 	/*
3216 	 * Once a command gets on discovery queue, this is the only thread
3217 	 * which can access it. So no need for the lock here.
3218 	 */
3219 	mutex_exit(&iport->iport_worker_lock);
3220 
3221 	while ((icmd = *ppicmd) != NULL) {
3222 		cmd = icmd->icmd_cmd;
3223 
3224 		/* Always remember that cmd->cmd_rp can be NULL */
3225 		if ((icmd->icmd_flags & (ICMD_KNOWN_TO_FCA |
3226 		    ICMD_FCA_ABORT_CALLED)) == ICMD_KNOWN_TO_FCA) {
3227 			atomic_or_32(&icmd->icmd_flags, ICMD_FCA_ABORT_CALLED);
3228 			if (CMD_HANDLE_VALID(cmd->cmd_handle))
3229 				flags = 0;
3230 			else
3231 				flags = FCT_IOF_FORCE_FCA_DONE;
3232 			abort_ret = port->port_abort_cmd(port, cmd, flags);
3233 			if ((abort_ret != FCT_SUCCESS) &&
3234 			    (abort_ret != FCT_ABORT_SUCCESS) &&
3235 			    (abort_ret != FCT_NOT_FOUND)) {
3236 				if (flags & FCT_IOF_FORCE_FCA_DONE) {
3237 					/*
3238 					 * XXX trigger port fatal,
3239 					 * Abort the termination, and shutdown
3240 					 * svc will trigger fct_cmd_termination
3241 					 * again.
3242 					 */
3243 					(void) snprintf(info, 80,
3244 					    "fct_cmd_terminator:"
3245 					    " iport-%p, port_abort_cmd with "
3246 					    "FORCE_FCA_DONE failed",
3247 					    (void *)iport);
3248 					info[79] = 0;
3249 					(void) fct_port_shutdown(
3250 					    iport->iport_port,
3251 					    STMF_RFLAG_FATAL_ERROR |
3252 					    STMF_RFLAG_RESET, info);
3253 
3254 					mutex_enter(&iport->iport_worker_lock);
3255 					iport->iport_ppicmd_term = ppicmd;
3256 					return (DISC_ACTION_DELAY_RESCAN);
3257 				}
3258 				atomic_and_32(&icmd->icmd_flags,
3259 				    ~ICMD_FCA_ABORT_CALLED);
3260 			} else if ((flags & FCT_IOF_FORCE_FCA_DONE) ||
3261 			    (abort_ret == FCT_ABORT_SUCCESS) ||
3262 			    (abort_ret == FCT_NOT_FOUND)) {
3263 				atomic_and_32(&icmd->icmd_flags,
3264 				    ~ICMD_KNOWN_TO_FCA);
3265 			}
3266 			ret |= DISC_ACTION_DELAY_RESCAN;
3267 		} else if (icmd->icmd_flags & ICMD_IMPLICIT) {
3268 			if (cmd->cmd_type == FCT_CMD_SOL_ELS)
3269 				cmd->cmd_comp_status = FCT_ABORTED;
3270 			atomic_or_32(&icmd->icmd_flags, ICMD_FCA_ABORT_CALLED);
3271 			cmd_implicit = 1;
3272 		}
3273 		if ((icmd->icmd_flags & ICMD_KNOWN_TO_FCA) == 0)
3274 			fca_done = 1;
3275 		else
3276 			fca_done = 0;
3277 		if ((icmd->icmd_flags & ICMD_IN_IRP_QUEUE) == 0)
3278 			fct_done = 1;
3279 		else
3280 			fct_done = 0;
3281 		if ((fca_done || cmd_implicit) && fct_done) {
3282 			mutex_enter(&iport->iport_worker_lock);
3283 			ASSERT(*ppicmd == icmd);
3284 			*ppicmd = (*ppicmd)->icmd_next;
3285 			mutex_exit(&iport->iport_worker_lock);
3286 			if ((cmd->cmd_type == FCT_CMD_RCVD_ELS) ||
3287 			    (cmd->cmd_type == FCT_CMD_RCVD_ABTS)) {
3288 				/* Free the cmd */
3289 				fct_cmd_free(cmd);
3290 			} else if (cmd->cmd_type == FCT_CMD_SOL_ELS) {
3291 				fct_handle_sol_els_completion(iport, icmd);
3292 				if (icmd->icmd_flags & ICMD_IMPLICIT) {
3293 					if (IS_LOGO_ELS(icmd)) {
3294 						/* IMPLICIT LOGO is special */
3295 						fct_cmd_free(cmd);
3296 					}
3297 				}
3298 			} else if (cmd->cmd_type == FCT_CMD_SOL_CT) {
3299 				fct_sol_ct_t *ct = ICMD_TO_CT(icmd);
3300 
3301 				/* Tell the caller that we are done */
3302 				atomic_or_32(&icmd->icmd_flags,
3303 				    ICMD_CMD_COMPLETE);
3304 				if (fct_netbuf_to_value(
3305 				    ct->ct_req_payload + 8, 2) == NS_GID_PN) {
3306 					fct_i_remote_port_t *irp;
3307 
3308 					rw_enter(&iport->iport_lock, RW_READER);
3309 					irp = fct_lookup_irp_by_portwwn(iport,
3310 					    ct->ct_req_payload + 16);
3311 
3312 					if (irp) {
3313 						atomic_and_32(&irp->irp_flags,
3314 						    ~IRP_RSCN_QUEUED);
3315 					}
3316 					rw_exit(&iport->iport_lock);
3317 				}
3318 			} else {
3319 				ASSERT(0);
3320 			}
3321 		} else {
3322 			clock_t	timeout_ticks;
3323 			if (port->port_fca_abort_timeout)
3324 				timeout_ticks = drv_usectohz(
3325 				    port->port_fca_abort_timeout*1000);
3326 			else
3327 				/* 10 seconds by default */
3328 				timeout_ticks = drv_usectohz(10 * 1000000);
3329 			if ((ddi_get_lbolt() >
3330 			    (icmd->icmd_start_time+timeout_ticks)) &&
3331 			    iport->iport_state == FCT_STATE_ONLINE) {
3332 				/* timeout, reset the port */
3333 				char cmd_type[10];
3334 				if (cmd->cmd_type == FCT_CMD_RCVD_ELS ||
3335 				    cmd->cmd_type == FCT_CMD_SOL_ELS) {
3336 					fct_els_t *els = cmd->cmd_specific;
3337 					(void) snprintf(cmd_type,
3338 					    sizeof (cmd_type), "%x.%x",
3339 					    cmd->cmd_type,
3340 					    els->els_req_payload[0]);
3341 				} else if (cmd->cmd_type == FCT_CMD_SOL_CT) {
3342 					fct_sol_ct_t *ct = cmd->cmd_specific;
3343 					(void) snprintf(cmd_type,
3344 					    sizeof (cmd_type), "%x.%02x%02x",
3345 					    cmd->cmd_type,
3346 					    ct->ct_req_payload[8],
3347 					    ct->ct_req_payload[9]);
3348 				} else {
3349 					cmd_type[0] = 0;
3350 				}
3351 				st = cmd->cmd_comp_status;	/* gcc fix */
3352 				(void) snprintf(info, 80, "fct_cmd_terminator:"
3353 				    " iport-%p, cmd_type(0x%s),"
3354 				    " reason(%llx)", (void *)iport, cmd_type,
3355 				    st);
3356 				info[79] = 0;
3357 				(void) fct_port_shutdown(port,
3358 				    STMF_RFLAG_FATAL_ERROR | STMF_RFLAG_RESET,
3359 				    info);
3360 			}
3361 			ppicmd = &((*ppicmd)->icmd_next);
3362 		}
3363 
3364 		if (ddi_get_lbolt() > endtime) {
3365 			mutex_enter(&iport->iport_worker_lock);
3366 			iport->iport_ppicmd_term = ppicmd;
3367 			return (DISC_ACTION_DELAY_RESCAN);
3368 		}
3369 	}
3370 	mutex_enter(&iport->iport_worker_lock);
3371 	if (iport->iport_abort_queue)
3372 		return (DISC_ACTION_DELAY_RESCAN);
3373 	if (ret == DISC_ACTION_NO_WORK)
3374 		return (DISC_ACTION_RESCAN);
3375 	return (ret);
3376 }
3377 
3378 /*
3379  * Send a syslog event for adapter port level events.
3380  */
3381 void
3382 fct_log_local_port_event(fct_local_port_t *port, char *subclass)
3383 {
3384 	nvlist_t *attr_list;
3385 	int port_instance;
3386 
3387 	if (!fct_dip)
3388 		return;
3389 	port_instance = ddi_get_instance(fct_dip);
3390 
3391 	if (nvlist_alloc(&attr_list, NV_UNIQUE_NAME_TYPE,
3392 	    KM_SLEEP) != DDI_SUCCESS) {
3393 		goto alloc_failed;
3394 	}
3395 
3396 	if (nvlist_add_uint32(attr_list, "instance", port_instance)
3397 	    != DDI_SUCCESS) {
3398 		goto error;
3399 	}
3400 
3401 	if (nvlist_add_byte_array(attr_list, "port-wwn",
3402 	    port->port_pwwn, 8) != DDI_SUCCESS) {
3403 		goto error;
3404 	}
3405 
3406 	(void) ddi_log_sysevent(fct_dip, DDI_VENDOR_SUNW, EC_SUNFC,
3407 	    subclass, attr_list, NULL, DDI_SLEEP);
3408 
3409 	nvlist_free(attr_list);
3410 	return;
3411 
3412 error:
3413 	nvlist_free(attr_list);
3414 alloc_failed:
3415 	stmf_trace(((fct_i_local_port_t *)port->port_fct_private)->iport_alias,
3416 	    "Unable to send %s event", subclass);
3417 }
3418 
3419 void
3420 fct_log_remote_port_event(fct_local_port_t *port, char *subclass,
3421     uint8_t *rp_pwwn, uint32_t rp_id)
3422 {
3423 	nvlist_t *attr_list;
3424 	int port_instance;
3425 
3426 	if (!fct_dip)
3427 		return;
3428 	port_instance = ddi_get_instance(fct_dip);
3429 
3430 	if (nvlist_alloc(&attr_list, NV_UNIQUE_NAME_TYPE,
3431 	    KM_SLEEP) != DDI_SUCCESS) {
3432 		goto alloc_failed;
3433 	}
3434 
3435 	if (nvlist_add_uint32(attr_list, "instance", port_instance)
3436 	    != DDI_SUCCESS) {
3437 		goto error;
3438 	}
3439 
3440 	if (nvlist_add_byte_array(attr_list, "port-wwn",
3441 	    port->port_pwwn, 8) != DDI_SUCCESS) {
3442 		goto error;
3443 	}
3444 
3445 	if (nvlist_add_byte_array(attr_list, "target-port-wwn",
3446 	    rp_pwwn, 8) != DDI_SUCCESS) {
3447 		goto error;
3448 	}
3449 
3450 	if (nvlist_add_uint32(attr_list, "target-port-id",
3451 	    rp_id) != DDI_SUCCESS) {
3452 		goto error;
3453 	}
3454 
3455 	(void) ddi_log_sysevent(fct_dip, DDI_VENDOR_SUNW, EC_SUNFC,
3456 	    subclass, attr_list, NULL, DDI_SLEEP);
3457 
3458 	nvlist_free(attr_list);
3459 	return;
3460 
3461 error:
3462 	nvlist_free(attr_list);
3463 alloc_failed:
3464 	stmf_trace(((fct_i_local_port_t *)port->port_fct_private)->iport_alias,
3465 	    "Unable to send %s event", subclass);
3466 }
3467 
3468 uint64_t
3469 fct_netbuf_to_value(uint8_t *buf, uint8_t nbytes)
3470 {
3471 	uint64_t	ret = 0;
3472 	uint8_t		idx = 0;
3473 
3474 	do {
3475 		ret |= (buf[idx] << (8 * (nbytes -idx - 1)));
3476 	} while (++idx < nbytes);
3477 
3478 	return (ret);
3479 }
3480 
3481 void
3482 fct_value_to_netbuf(uint64_t value, uint8_t *buf, uint8_t nbytes)
3483 {
3484 	uint8_t		idx = 0;
3485 
3486 	for (idx = 0; idx < nbytes; idx++) {
3487 		buf[idx] = 0xFF & (value >> (8 * (nbytes - idx - 1)));
3488 	}
3489 }
3490 
3491 /*
3492  * from_ptr: ptr to uchar_t array of size WWN_SIZE
3493  * to_ptr: char ptr to string of size WWN_SIZE*2+1
3494  */
3495 void
3496 fct_wwn_to_str(char *to_ptr, const uint8_t *from_ptr)
3497 {
3498 	ASSERT(to_ptr != NULL && from_ptr != NULL);
3499 
3500 	(void) sprintf(to_ptr, "%02x%02x%02x%02x%02x%02x%02x%02x",
3501 	    from_ptr[0], from_ptr[1], from_ptr[2], from_ptr[3],
3502 	    from_ptr[4], from_ptr[5], from_ptr[6], from_ptr[7]);
3503 }
3504 
3505 static int
3506 fct_update_stats(kstat_t *ks, int rw)
3507 {
3508 	fct_i_local_port_t *iport;
3509 	fct_port_stat_t *port_kstat;
3510 	fct_port_link_status_t stat;
3511 	uint32_t	buf_size = sizeof (stat);
3512 	int		ret;
3513 
3514 	if (rw == KSTAT_WRITE)
3515 		return (EACCES);
3516 
3517 	iport = (fct_i_local_port_t *)ks->ks_private;
3518 	port_kstat = (fct_port_stat_t *)ks->ks_data;
3519 
3520 	if (iport->iport_port->port_info == NULL) {
3521 		return (EIO);
3522 	}
3523 	ret = iport->iport_port->port_info(FC_TGT_PORT_RLS,
3524 	    iport->iport_port, NULL, (uint8_t *)&stat, &buf_size);
3525 	if (ret != STMF_SUCCESS) {
3526 		return (EIO);
3527 	}
3528 
3529 	port_kstat->link_failure_cnt.value.ui32 =
3530 	    stat.LinkFailureCount;
3531 	port_kstat->loss_of_sync_cnt.value.ui32 =
3532 	    stat.LossOfSyncCount;
3533 	port_kstat->loss_of_signals_cnt.value.ui32 =
3534 	    stat.LossOfSignalsCount;
3535 	port_kstat->prim_seq_protocol_err_cnt.value.ui32 =
3536 	    stat.PrimitiveSeqProtocolErrorCount;
3537 	port_kstat->invalid_tx_word_cnt.value.ui32 =
3538 	    stat.InvalidTransmissionWordCount;
3539 	port_kstat->invalid_crc_cnt.value.ui32 =
3540 	    stat.InvalidCRCCount;
3541 
3542 	return (0);
3543 }
3544 
3545 void
3546 fct_init_kstats(fct_i_local_port_t *iport)
3547 {
3548 	kstat_t *ks;
3549 	fct_port_stat_t *port_kstat;
3550 	char	name[256];
3551 
3552 	if (iport->iport_alias)
3553 		(void) sprintf(name, "iport_%s", iport->iport_alias);
3554 	else
3555 		(void) sprintf(name, "iport_%"PRIxPTR"", (uintptr_t)iport);
3556 	ks = kstat_create(FCT_MODULE_NAME, 0, name, "rawdata",
3557 	    KSTAT_TYPE_NAMED, sizeof (fct_port_stat_t) / sizeof (kstat_named_t),
3558 	    0);
3559 
3560 	if (ks == NULL) {
3561 		return;
3562 	}
3563 	port_kstat = (fct_port_stat_t *)ks->ks_data;
3564 
3565 	iport->iport_kstat_portstat = ks;
3566 	kstat_named_init(&port_kstat->link_failure_cnt,
3567 	    "Link_failure_cnt", KSTAT_DATA_UINT32);
3568 	kstat_named_init(&port_kstat->loss_of_sync_cnt,
3569 	    "Loss_of_sync_cnt", KSTAT_DATA_UINT32);
3570 	kstat_named_init(&port_kstat->loss_of_signals_cnt,
3571 	    "Loss_of_signals_cnt", KSTAT_DATA_UINT32);
3572 	kstat_named_init(&port_kstat->prim_seq_protocol_err_cnt,
3573 	    "Prim_seq_protocol_err_cnt", KSTAT_DATA_UINT32);
3574 	kstat_named_init(&port_kstat->invalid_tx_word_cnt,
3575 	    "Invalid_tx_word_cnt", KSTAT_DATA_UINT32);
3576 	kstat_named_init(&port_kstat->invalid_crc_cnt,
3577 	    "Invalid_crc_cnt", KSTAT_DATA_UINT32);
3578 	ks->ks_update = fct_update_stats;
3579 	ks->ks_private = (void *)iport;
3580 	kstat_install(ks);
3581 
3582 }
3583