xref: /onnv-gate/usr/src/uts/common/io/scsi/adapters/iscsi/iscsi_stats.c (revision 9162:b011b0287065)
17836SJohn.Forte@Sun.COM /*
27836SJohn.Forte@Sun.COM  * CDDL HEADER START
37836SJohn.Forte@Sun.COM  *
47836SJohn.Forte@Sun.COM  * The contents of this file are subject to the terms of the
57836SJohn.Forte@Sun.COM  * Common Development and Distribution License (the "License").
67836SJohn.Forte@Sun.COM  * You may not use this file except in compliance with the License.
77836SJohn.Forte@Sun.COM  *
87836SJohn.Forte@Sun.COM  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97836SJohn.Forte@Sun.COM  * or http://www.opensolaris.org/os/licensing.
107836SJohn.Forte@Sun.COM  * See the License for the specific language governing permissions
117836SJohn.Forte@Sun.COM  * and limitations under the License.
127836SJohn.Forte@Sun.COM  *
137836SJohn.Forte@Sun.COM  * When distributing Covered Code, include this CDDL HEADER in each
147836SJohn.Forte@Sun.COM  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157836SJohn.Forte@Sun.COM  * If applicable, add the following below this CDDL HEADER, with the
167836SJohn.Forte@Sun.COM  * fields enclosed by brackets "[]" replaced with your own identifying
177836SJohn.Forte@Sun.COM  * information: Portions Copyright [yyyy] [name of copyright owner]
187836SJohn.Forte@Sun.COM  *
197836SJohn.Forte@Sun.COM  * CDDL HEADER END
207836SJohn.Forte@Sun.COM  */
217836SJohn.Forte@Sun.COM /*
22*9162SPeter.Dunlap@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237836SJohn.Forte@Sun.COM  * Use is subject to license terms.
247836SJohn.Forte@Sun.COM  *
257836SJohn.Forte@Sun.COM  * iSCSI Software Initiator
267836SJohn.Forte@Sun.COM  */
277836SJohn.Forte@Sun.COM 
28*9162SPeter.Dunlap@Sun.COM #define	ISCSI_ICS_NAMES
297836SJohn.Forte@Sun.COM #include "iscsi.h"	/* main header */
307836SJohn.Forte@Sun.COM 
317836SJohn.Forte@Sun.COM kstat_item_t	kstat_items_hba[KN_HBA_IDX_MAX] = {
327836SJohn.Forte@Sun.COM 	{"_name", KSTAT_DATA_STRING},
337836SJohn.Forte@Sun.COM 	{"_alias", KSTAT_DATA_STRING},
347836SJohn.Forte@Sun.COM 	{"_cntr_sess", KSTAT_DATA_ULONG}
357836SJohn.Forte@Sun.COM };
367836SJohn.Forte@Sun.COM 
377836SJohn.Forte@Sun.COM kstat_item_t	kstat_items_sess[KN_SESS_IDX_MAX] = {
387836SJohn.Forte@Sun.COM 	{"_state", KSTAT_DATA_STRING},
397836SJohn.Forte@Sun.COM 	{"_oid", KSTAT_DATA_ULONG},
407836SJohn.Forte@Sun.COM 	{"_hba", KSTAT_DATA_STRING},
417836SJohn.Forte@Sun.COM 	{"_cntr_conn", KSTAT_DATA_ULONG},
427836SJohn.Forte@Sun.COM 	{"_cntr_reset", KSTAT_DATA_ULONG},
437836SJohn.Forte@Sun.COM 	{"_cntr_pkt_pending", KSTAT_DATA_ULONG},
447836SJohn.Forte@Sun.COM 	{"_cmd_sn", KSTAT_DATA_ULONG},
457836SJohn.Forte@Sun.COM 	{"_cmd_sn_exp", KSTAT_DATA_ULONG},
467836SJohn.Forte@Sun.COM 	{"_cmd_sn_max", KSTAT_DATA_ULONG},
477836SJohn.Forte@Sun.COM 	{"_target_name", KSTAT_DATA_STRING},
487836SJohn.Forte@Sun.COM 	{"_target_alias", KSTAT_DATA_STRING},
497836SJohn.Forte@Sun.COM 	{"_tpgt", KSTAT_DATA_ULONG}
507836SJohn.Forte@Sun.COM };
517836SJohn.Forte@Sun.COM 
527836SJohn.Forte@Sun.COM kstat_item_t	kstat_items_conn[KN_CONN_IDX_MAX] = {
537836SJohn.Forte@Sun.COM 	{"_state", KSTAT_DATA_STRING},
547836SJohn.Forte@Sun.COM 	{"_cid", KSTAT_DATA_ULONG},
557836SJohn.Forte@Sun.COM 	{"_oid", KSTAT_DATA_ULONG},
567836SJohn.Forte@Sun.COM 	{"_session", KSTAT_DATA_STRING},
577836SJohn.Forte@Sun.COM 	{"_err_header_digest", KSTAT_DATA_ULONG},
587836SJohn.Forte@Sun.COM 	{"_err_data_digest", KSTAT_DATA_ULONG},
597836SJohn.Forte@Sun.COM 	{"_err_connection_reset", KSTAT_DATA_ULONG},
607836SJohn.Forte@Sun.COM 	{"_err_protocol_error", KSTAT_DATA_ULONG},
617836SJohn.Forte@Sun.COM 	{"_cntr_tx_bytes", KSTAT_DATA_ULONGLONG},
627836SJohn.Forte@Sun.COM 	{"_cntr_rx_bytes", KSTAT_DATA_ULONGLONG},
637836SJohn.Forte@Sun.COM 	{"_cntr_qactive", KSTAT_DATA_ULONG},
647836SJohn.Forte@Sun.COM 	{"_stat_sn_exp", KSTAT_DATA_ULONG},
657836SJohn.Forte@Sun.COM 	{"_stat_sn_last", KSTAT_DATA_ULONG}
667836SJohn.Forte@Sun.COM };
677836SJohn.Forte@Sun.COM 
687836SJohn.Forte@Sun.COM int iscsi_hba_kstat_update(kstat_t *ks, int rw);
697836SJohn.Forte@Sun.COM int iscsi_sess_kstat_update(kstat_t *ks, int rw);
707836SJohn.Forte@Sun.COM int iscsi_conn_kstat_update(kstat_t *ks, int rw);
717836SJohn.Forte@Sun.COM 
727836SJohn.Forte@Sun.COM /*
737836SJohn.Forte@Sun.COM  * HBA
747836SJohn.Forte@Sun.COM  */
757836SJohn.Forte@Sun.COM 
767836SJohn.Forte@Sun.COM /*
777836SJohn.Forte@Sun.COM  * iscsi_hba_kstat_init - This function registers with the kstat service.
787836SJohn.Forte@Sun.COM  */
797836SJohn.Forte@Sun.COM boolean_t
iscsi_hba_kstat_init(iscsi_hba_t * ihp)807836SJohn.Forte@Sun.COM iscsi_hba_kstat_init(iscsi_hba_t *ihp)
817836SJohn.Forte@Sun.COM {
827836SJohn.Forte@Sun.COM 	char			ks_name[KSTAT_STRLEN];
837836SJohn.Forte@Sun.COM 	iscsi_hba_stats_t	*ihs;
847836SJohn.Forte@Sun.COM 	int			i;
857836SJohn.Forte@Sun.COM 
867836SJohn.Forte@Sun.COM 	/*
877836SJohn.Forte@Sun.COM 	 * The name of the KSTAT structure is built.
887836SJohn.Forte@Sun.COM 	 */
897836SJohn.Forte@Sun.COM 	bzero(ks_name, sizeof (ks_name));
907836SJohn.Forte@Sun.COM 
917836SJohn.Forte@Sun.COM 	if (snprintf(ks_name, sizeof (ks_name) - 1, iSCSI_HBA_BASE_NAME,
927836SJohn.Forte@Sun.COM 	    ihp->hba_oid) >= sizeof (ks_name)) {
937836SJohn.Forte@Sun.COM 		return (TRUE);
947836SJohn.Forte@Sun.COM 	}
957836SJohn.Forte@Sun.COM 
967836SJohn.Forte@Sun.COM 	ihp->stats.ks = kstat_create(iSCSI_MODULE_NAME,
977836SJohn.Forte@Sun.COM 	    ddi_get_instance(ihp->hba_dip), ks_name, iSCSI_CLASS_HBA,
987836SJohn.Forte@Sun.COM 	    KSTAT_TYPE_NAMED, 0, KSTAT_FLAG_VIRTUAL);
997836SJohn.Forte@Sun.COM 
1007836SJohn.Forte@Sun.COM 	if (ihp->stats.ks == NULL) {
1017836SJohn.Forte@Sun.COM 		cmn_err(CE_NOTE, "iscsi kstat creation failed for hba(%d)",
1027836SJohn.Forte@Sun.COM 		    ihp->hba_oid);
1037836SJohn.Forte@Sun.COM 		return (TRUE);
1047836SJohn.Forte@Sun.COM 	}
1057836SJohn.Forte@Sun.COM 
1067836SJohn.Forte@Sun.COM 	ihs = &ihp->stats.ks_data;
1077836SJohn.Forte@Sun.COM 	ihp->stats.ks->ks_data = &ihp->stats.ks_data;
1087836SJohn.Forte@Sun.COM 	ihp->stats.ks->ks_data_size = sizeof (ihp->stats.ks_data);
1097836SJohn.Forte@Sun.COM 	ihp->stats.ks->ks_ndata = KN_HBA_IDX_MAX;
1107836SJohn.Forte@Sun.COM 
1117836SJohn.Forte@Sun.COM 	for (i = 0; i < KN_HBA_IDX_MAX; i++) {
1127836SJohn.Forte@Sun.COM 		kstat_named_init(&ihs->kn[i], kstat_items_hba[i]._name,
1137836SJohn.Forte@Sun.COM 		    kstat_items_hba[i]._data_type);
1147836SJohn.Forte@Sun.COM 	}
1157836SJohn.Forte@Sun.COM 
1167836SJohn.Forte@Sun.COM 	ihp->stats.ks->ks_update = iscsi_hba_kstat_update;
1177836SJohn.Forte@Sun.COM 	ihp->stats.ks->ks_private = (void *)ihp;
1187836SJohn.Forte@Sun.COM 
1197836SJohn.Forte@Sun.COM 	kstat_install(ihp->stats.ks);
1207836SJohn.Forte@Sun.COM 
1217836SJohn.Forte@Sun.COM 	return (FALSE);
1227836SJohn.Forte@Sun.COM }
1237836SJohn.Forte@Sun.COM 
1247836SJohn.Forte@Sun.COM /*
1257836SJohn.Forte@Sun.COM  * iscsi_hba_kstat_term - This function deregisters from the kstat service.
1267836SJohn.Forte@Sun.COM  */
1277836SJohn.Forte@Sun.COM boolean_t
iscsi_hba_kstat_term(iscsi_hba_t * ihp)1287836SJohn.Forte@Sun.COM iscsi_hba_kstat_term(iscsi_hba_t *ihp)
1297836SJohn.Forte@Sun.COM {
1307836SJohn.Forte@Sun.COM 	kstat_delete(ihp->stats.ks);
1317836SJohn.Forte@Sun.COM 	return (FALSE);
1327836SJohn.Forte@Sun.COM }
1337836SJohn.Forte@Sun.COM 
1347836SJohn.Forte@Sun.COM /*
1357836SJohn.Forte@Sun.COM  * iscsi_hba_kstat_update - This function update the kstat structure of the HBA.
1367836SJohn.Forte@Sun.COM  */
1377836SJohn.Forte@Sun.COM int
iscsi_hba_kstat_update(kstat_t * ks,int rw)1387836SJohn.Forte@Sun.COM iscsi_hba_kstat_update(kstat_t *ks, int rw)
1397836SJohn.Forte@Sun.COM {
1407836SJohn.Forte@Sun.COM 	iscsi_hba_t		*ihp = (iscsi_hba_t *)ks->ks_private;
1417836SJohn.Forte@Sun.COM 	iscsi_hba_stats_t	*ihs = &ihp->stats.ks_data;
1427836SJohn.Forte@Sun.COM 
1437836SJohn.Forte@Sun.COM 	if (rw == KSTAT_READ) {
1447836SJohn.Forte@Sun.COM 		rw_enter(&ihp->hba_sess_list_rwlock, RW_READER);
1457836SJohn.Forte@Sun.COM 		bcopy(ihp->hba_name, ihs->name, ihp->hba_name_length);
1467836SJohn.Forte@Sun.COM 
1477836SJohn.Forte@Sun.COM 		bcopy(ihp->hba_alias, ihs->alias, ihp->hba_alias_length);
1487836SJohn.Forte@Sun.COM 
1497836SJohn.Forte@Sun.COM 		ihs->name[ihp->hba_name_length] = 0;
1507836SJohn.Forte@Sun.COM 		ihs->alias[ihp->hba_alias_length] = 0;
1517836SJohn.Forte@Sun.COM 
1527836SJohn.Forte@Sun.COM 		kstat_named_setstr(&ihs->kn[KN_HBA_IDX_NAME],
1537836SJohn.Forte@Sun.COM 		    (const char *)ihs->name);
1547836SJohn.Forte@Sun.COM 		kstat_named_setstr(&ihs->kn[KN_HBA_IDX_ALIAS],
1557836SJohn.Forte@Sun.COM 		    (const char *)ihs->alias);
1567836SJohn.Forte@Sun.COM 		rw_exit(&ihp->hba_sess_list_rwlock);
1577836SJohn.Forte@Sun.COM 	}
1587836SJohn.Forte@Sun.COM 	return (0);
1597836SJohn.Forte@Sun.COM }
1607836SJohn.Forte@Sun.COM 
1617836SJohn.Forte@Sun.COM /*
1627836SJohn.Forte@Sun.COM  * Session
1637836SJohn.Forte@Sun.COM  */
1647836SJohn.Forte@Sun.COM 
1657836SJohn.Forte@Sun.COM /*
1667836SJohn.Forte@Sun.COM  * iscsi_sess_kstat_init - This function registers with the kstat service.
1677836SJohn.Forte@Sun.COM  */
1687836SJohn.Forte@Sun.COM boolean_t
iscsi_sess_kstat_init(iscsi_sess_t * isp)1697836SJohn.Forte@Sun.COM iscsi_sess_kstat_init(iscsi_sess_t *isp)
1707836SJohn.Forte@Sun.COM {
1717836SJohn.Forte@Sun.COM 	iscsi_hba_t		*ihp;
1727836SJohn.Forte@Sun.COM 	char			ks_name[KSTAT_STRLEN];
1737836SJohn.Forte@Sun.COM 	iscsi_sess_stats_t	*iss;
1747836SJohn.Forte@Sun.COM 	int			i;
1757836SJohn.Forte@Sun.COM 
1767836SJohn.Forte@Sun.COM 	ASSERT(isp != NULL);
1777836SJohn.Forte@Sun.COM 	ihp = isp->sess_hba;
1787836SJohn.Forte@Sun.COM 	ASSERT(ihp != NULL);
1797836SJohn.Forte@Sun.COM 
1807836SJohn.Forte@Sun.COM 	/*
1817836SJohn.Forte@Sun.COM 	 * The name of the KSTAT structure is built.
1827836SJohn.Forte@Sun.COM 	 */
1837836SJohn.Forte@Sun.COM 	bzero(ks_name, sizeof (ks_name));
1847836SJohn.Forte@Sun.COM 
1857836SJohn.Forte@Sun.COM 	if (snprintf(ks_name, sizeof (ks_name) - 1, iSCSI_SESS_BASE_NAME,
1867836SJohn.Forte@Sun.COM 	    isp->sess_hba->hba_oid, isp->sess_oid) >= sizeof (ks_name)) {
1877836SJohn.Forte@Sun.COM 		cmn_err(CE_NOTE, "iscsi kstat creation failed for "
1887836SJohn.Forte@Sun.COM 		    "session(%u)", isp->sess_oid);
1897836SJohn.Forte@Sun.COM 		return (TRUE);
1907836SJohn.Forte@Sun.COM 	}
1917836SJohn.Forte@Sun.COM 
1927836SJohn.Forte@Sun.COM 	isp->stats.ks = kstat_create(iSCSI_MODULE_NAME,
1937836SJohn.Forte@Sun.COM 	    ddi_get_instance(ihp->hba_dip), ks_name, iSCSI_CLASS_SESS,
1947836SJohn.Forte@Sun.COM 	    KSTAT_TYPE_NAMED, 0, KSTAT_FLAG_VIRTUAL);
1957836SJohn.Forte@Sun.COM 
1967836SJohn.Forte@Sun.COM 	if (isp->stats.ks == NULL) {
1977836SJohn.Forte@Sun.COM 		cmn_err(CE_NOTE, "iscsi kstat creation failed "
1987836SJohn.Forte@Sun.COM 		    "for session(%u)", isp->sess_oid);
1997836SJohn.Forte@Sun.COM 		return (TRUE);
2007836SJohn.Forte@Sun.COM 	}
2017836SJohn.Forte@Sun.COM 
2027836SJohn.Forte@Sun.COM 	iss = &isp->stats.ks_data;
2037836SJohn.Forte@Sun.COM 	isp->stats.ks->ks_data = (void *)&isp->stats.ks_data;
2047836SJohn.Forte@Sun.COM 	isp->stats.ks->ks_data_size = sizeof (isp->stats.ks_data);
2057836SJohn.Forte@Sun.COM 	isp->stats.ks->ks_ndata = KN_SESS_IDX_MAX;
2067836SJohn.Forte@Sun.COM 
2077836SJohn.Forte@Sun.COM 	for (i = 0; i < KN_SESS_IDX_MAX; i++) {
2087836SJohn.Forte@Sun.COM 		kstat_named_init(&iss->kn[i], kstat_items_sess[i]._name,
2097836SJohn.Forte@Sun.COM 		    kstat_items_sess[i]._data_type);
2107836SJohn.Forte@Sun.COM 	}
2117836SJohn.Forte@Sun.COM 
2127836SJohn.Forte@Sun.COM 	/* The static information is updated immediately */
2137836SJohn.Forte@Sun.COM 	bzero(iss->hba_str, sizeof (iss->hba_str));
2147836SJohn.Forte@Sun.COM 	bcopy(ihp->stats.ks->ks_name, iss->hba_str, sizeof (iss->hba_str));
2157836SJohn.Forte@Sun.COM 	kstat_named_setstr(&iss->kn[KN_SESS_IDX_HBA],
2167836SJohn.Forte@Sun.COM 	    (const char *)iss->hba_str);
2177836SJohn.Forte@Sun.COM 
2187836SJohn.Forte@Sun.COM 	iss->kn[KN_SESS_IDX_OID].value.ul = isp->sess_oid;
2197836SJohn.Forte@Sun.COM 
2207836SJohn.Forte@Sun.COM 	isp->stats.ks->ks_update = iscsi_sess_kstat_update;
2217836SJohn.Forte@Sun.COM 	isp->stats.ks->ks_private = (void *)isp;
2227836SJohn.Forte@Sun.COM 
2237836SJohn.Forte@Sun.COM 	/* The IO KSTAT structure is created */
2247836SJohn.Forte@Sun.COM 	bzero(ks_name, sizeof (ks_name));
2257836SJohn.Forte@Sun.COM 
2267836SJohn.Forte@Sun.COM 	if (snprintf(ks_name, sizeof (ks_name) - 1, iSCSI_SESS_IO_BASE_NAME,
2277836SJohn.Forte@Sun.COM 	    isp->sess_hba->hba_oid, isp->sess_oid) >= sizeof (ks_name)) {
2287836SJohn.Forte@Sun.COM 		cmn_err(CE_NOTE, "iscsi kstat createion failed "
2297836SJohn.Forte@Sun.COM 		    "for session(%u)", isp->sess_oid);
2307836SJohn.Forte@Sun.COM 		kstat_delete(isp->stats.ks);
2317836SJohn.Forte@Sun.COM 		return (TRUE);
2327836SJohn.Forte@Sun.COM 	}
2337836SJohn.Forte@Sun.COM 
2347836SJohn.Forte@Sun.COM 	isp->stats.ks_io = kstat_create(iSCSI_MODULE_NAME,
2357836SJohn.Forte@Sun.COM 	    ddi_get_instance(ihp->hba_dip), ks_name, iSCSI_CLASS_SESS,
2367836SJohn.Forte@Sun.COM 	    KSTAT_TYPE_IO, 1, KSTAT_FLAG_VIRTUAL);
2377836SJohn.Forte@Sun.COM 
2387836SJohn.Forte@Sun.COM 	if (isp->stats.ks_io == NULL) {
2397836SJohn.Forte@Sun.COM 		kstat_delete(isp->stats.ks);
2407836SJohn.Forte@Sun.COM 		cmn_err(CE_NOTE, "iscsi kstat creation failed "
2417836SJohn.Forte@Sun.COM 		    "for session(%u)", isp->sess_oid);
2427836SJohn.Forte@Sun.COM 		return (TRUE);
2437836SJohn.Forte@Sun.COM 	}
2447836SJohn.Forte@Sun.COM 	mutex_init(&isp->stats.ks_io_lock, NULL, MUTEX_DRIVER, NULL);
2457836SJohn.Forte@Sun.COM 	isp->stats.ks_io->ks_data = &isp->stats.ks_io_data;
2467836SJohn.Forte@Sun.COM 	isp->stats.ks_io->ks_lock = &isp->stats.ks_io_lock;
2477836SJohn.Forte@Sun.COM 
2487836SJohn.Forte@Sun.COM 	kstat_install(isp->stats.ks);
2497836SJohn.Forte@Sun.COM 	kstat_install(isp->stats.ks_io);
2507836SJohn.Forte@Sun.COM 
2517836SJohn.Forte@Sun.COM 	return (FALSE);
2527836SJohn.Forte@Sun.COM }
2537836SJohn.Forte@Sun.COM 
2547836SJohn.Forte@Sun.COM /*
2557836SJohn.Forte@Sun.COM  * iscsi_sess_kstat_term - This function deregisters with the kstat service.
2567836SJohn.Forte@Sun.COM  */
2577836SJohn.Forte@Sun.COM boolean_t
iscsi_sess_kstat_term(iscsi_sess_t * isp)2587836SJohn.Forte@Sun.COM iscsi_sess_kstat_term(iscsi_sess_t *isp)
2597836SJohn.Forte@Sun.COM {
2607836SJohn.Forte@Sun.COM 	kstat_delete(isp->stats.ks_io);
2617836SJohn.Forte@Sun.COM 	mutex_destroy(&isp->stats.ks_io_lock);
2627836SJohn.Forte@Sun.COM 	kstat_delete(isp->stats.ks);
2637836SJohn.Forte@Sun.COM 	return (FALSE);
2647836SJohn.Forte@Sun.COM }
2657836SJohn.Forte@Sun.COM 
2667836SJohn.Forte@Sun.COM /*
2677836SJohn.Forte@Sun.COM  * iscsi_sess_kstat_update - This function update the kstat
2687836SJohn.Forte@Sun.COM  *	structure of the HBA.
2697836SJohn.Forte@Sun.COM  */
2707836SJohn.Forte@Sun.COM int
iscsi_sess_kstat_update(kstat_t * ks,int rw)2717836SJohn.Forte@Sun.COM iscsi_sess_kstat_update(kstat_t *ks, int rw)
2727836SJohn.Forte@Sun.COM {
2737836SJohn.Forte@Sun.COM 	iscsi_sess_t		*isp = (iscsi_sess_t *)ks->ks_private;
2747836SJohn.Forte@Sun.COM 	iscsi_sess_stats_t	*iss = &isp->stats.ks_data;
2757836SJohn.Forte@Sun.COM 	char			*ptr;
2767836SJohn.Forte@Sun.COM 	int			len;
2777836SJohn.Forte@Sun.COM 
2787836SJohn.Forte@Sun.COM 	if (rw == KSTAT_READ) {
2797836SJohn.Forte@Sun.COM 
2807836SJohn.Forte@Sun.COM 		/* String indicating the state of the session */
2817836SJohn.Forte@Sun.COM 		ptr = iscsi_sess_state_str(isp->sess_state);
2827836SJohn.Forte@Sun.COM 		len =  strlen(ptr);
283*9162SPeter.Dunlap@Sun.COM 		if (len > (sizeof (iss->state_str) - 1)) {
284*9162SPeter.Dunlap@Sun.COM 			len = sizeof (iss->state_str) - 1;
2857836SJohn.Forte@Sun.COM 		}
2867836SJohn.Forte@Sun.COM 		bzero(iss->state_str, sizeof (iss->state_str));
2877836SJohn.Forte@Sun.COM 		bcopy(ptr, iss->state_str, len);
2887836SJohn.Forte@Sun.COM 		kstat_named_setstr(
2897836SJohn.Forte@Sun.COM 		    &iss->kn[KN_SESS_IDX_STATE],
2907836SJohn.Forte@Sun.COM 		    (const char *)iss->state_str);
2917836SJohn.Forte@Sun.COM 
2927836SJohn.Forte@Sun.COM 		/* Target name string */
2937836SJohn.Forte@Sun.COM 		if (isp->sess_name_length > sizeof (iss->target_name)) {
2947836SJohn.Forte@Sun.COM 			len = sizeof (iss->target_name);
2957836SJohn.Forte@Sun.COM 		} else {
2967836SJohn.Forte@Sun.COM 			len =  isp->sess_name_length;
2977836SJohn.Forte@Sun.COM 		}
2987836SJohn.Forte@Sun.COM 		bzero(iss->target_name, sizeof (iss->target_name));
2997836SJohn.Forte@Sun.COM 		bcopy(isp->sess_name, iss->target_name, len);
3007836SJohn.Forte@Sun.COM 		kstat_named_setstr(&iss->kn[KN_SESS_IDX_TARGET_NAME],
3017836SJohn.Forte@Sun.COM 		    (const char *)iss->target_name);
3027836SJohn.Forte@Sun.COM 
3037836SJohn.Forte@Sun.COM 		/* Target alias string */
3047836SJohn.Forte@Sun.COM 		if (isp->sess_alias_length > sizeof (iss->target_alias)) {
3057836SJohn.Forte@Sun.COM 			len = sizeof (iss->target_alias);
3067836SJohn.Forte@Sun.COM 		} else {
3077836SJohn.Forte@Sun.COM 			len =  isp->sess_alias_length;
3087836SJohn.Forte@Sun.COM 		}
3097836SJohn.Forte@Sun.COM 		bzero(iss->target_alias, sizeof (iss->target_alias));
3107836SJohn.Forte@Sun.COM 		bcopy(isp->sess_alias, iss->target_alias, len);
3117836SJohn.Forte@Sun.COM 		kstat_named_setstr(
3127836SJohn.Forte@Sun.COM 		    &iss->kn[KN_SESS_IDX_TARGET_ALIAS],
3137836SJohn.Forte@Sun.COM 		    (const char *)iss->target_alias);
3147836SJohn.Forte@Sun.COM 
3157836SJohn.Forte@Sun.COM 		iss->kn[KN_SESS_IDX_CNTR_PKT_PENDING].value.ul =
3167836SJohn.Forte@Sun.COM 		    isp->sess_queue_pending.count;
3177836SJohn.Forte@Sun.COM 		iss->kn[KN_SESS_IDX_CMDSN].value.ul =
3187836SJohn.Forte@Sun.COM 		    isp->sess_cmdsn;
3197836SJohn.Forte@Sun.COM 		iss->kn[KN_SESS_IDX_EXPCMDSN].value.ul =
3207836SJohn.Forte@Sun.COM 		    isp->sess_expcmdsn;
3217836SJohn.Forte@Sun.COM 		iss->kn[KN_SESS_IDX_MAXCMDSN].value.ul =
3227836SJohn.Forte@Sun.COM 		    isp->sess_maxcmdsn;
3237836SJohn.Forte@Sun.COM 		iss->kn[KN_SESS_IDX_TPGT].value.ul =
3247836SJohn.Forte@Sun.COM 		    isp->sess_tpgt_conf;
3257836SJohn.Forte@Sun.COM 
3267836SJohn.Forte@Sun.COM 	}
3277836SJohn.Forte@Sun.COM 	return (0);
3287836SJohn.Forte@Sun.COM }
3297836SJohn.Forte@Sun.COM 
3307836SJohn.Forte@Sun.COM /*
3317836SJohn.Forte@Sun.COM  * Connection
3327836SJohn.Forte@Sun.COM  */
3337836SJohn.Forte@Sun.COM 
3347836SJohn.Forte@Sun.COM /*
3357836SJohn.Forte@Sun.COM  * iscsi_conn_kstat_init - This function registers with the kstat service.
3367836SJohn.Forte@Sun.COM  */
3377836SJohn.Forte@Sun.COM boolean_t
iscsi_conn_kstat_init(iscsi_conn_t * icp)3387836SJohn.Forte@Sun.COM iscsi_conn_kstat_init(iscsi_conn_t *icp)
3397836SJohn.Forte@Sun.COM {
3407836SJohn.Forte@Sun.COM 	iscsi_sess_t		*isp = icp->conn_sess;
3417836SJohn.Forte@Sun.COM 	iscsi_hba_t		*ihp = isp->sess_hba;
3427836SJohn.Forte@Sun.COM 	iscsi_conn_stats_t	*ics;
3437836SJohn.Forte@Sun.COM 	int			i;
3447836SJohn.Forte@Sun.COM 	char			ks_name[KSTAT_STRLEN];
3457836SJohn.Forte@Sun.COM 
3467836SJohn.Forte@Sun.COM 	/*
3477836SJohn.Forte@Sun.COM 	 * The name of the KSTAT structure is built.
3487836SJohn.Forte@Sun.COM 	 */
3497836SJohn.Forte@Sun.COM 	bzero(ks_name, sizeof (ks_name));
3507836SJohn.Forte@Sun.COM 
3517836SJohn.Forte@Sun.COM 	if (snprintf(ks_name, sizeof (ks_name) - 1, iSCSI_CONN_BASE_NAME,
3527836SJohn.Forte@Sun.COM 	    icp->conn_sess->sess_hba->hba_oid, icp->conn_sess->sess_oid,
3537836SJohn.Forte@Sun.COM 	    icp->conn_oid) >= sizeof (ks_name)) {
3547836SJohn.Forte@Sun.COM 		return (TRUE);
3557836SJohn.Forte@Sun.COM 	}
3567836SJohn.Forte@Sun.COM 
3577836SJohn.Forte@Sun.COM 	icp->stats.ks = kstat_create(iSCSI_MODULE_NAME,
3587836SJohn.Forte@Sun.COM 	    ddi_get_instance(ihp->hba_dip), ks_name, iSCSI_CLASS_CONN,
3597836SJohn.Forte@Sun.COM 	    KSTAT_TYPE_NAMED, 0, KSTAT_FLAG_VIRTUAL);
3607836SJohn.Forte@Sun.COM 
3617836SJohn.Forte@Sun.COM 	if (icp->stats.ks == NULL) {
3627836SJohn.Forte@Sun.COM 		cmn_err(CE_NOTE, "iscsi kstat creation failed "
3637836SJohn.Forte@Sun.COM 		    "for connection(%d)", icp->conn_oid);
3647836SJohn.Forte@Sun.COM 		return (TRUE);
3657836SJohn.Forte@Sun.COM 	}
3667836SJohn.Forte@Sun.COM 
3677836SJohn.Forte@Sun.COM 	ics = &icp->stats.ks_data;
3687836SJohn.Forte@Sun.COM 	icp->stats.ks->ks_data = (void *)ics;
3697836SJohn.Forte@Sun.COM 	icp->stats.ks->ks_data_size = sizeof (*ics);
3707836SJohn.Forte@Sun.COM 	icp->stats.ks->ks_ndata = KN_CONN_IDX_MAX;
3717836SJohn.Forte@Sun.COM 
3727836SJohn.Forte@Sun.COM 	for (i = 0; i < KN_CONN_IDX_MAX; i++) {
3737836SJohn.Forte@Sun.COM 		kstat_named_init(&ics->kn[i], kstat_items_conn[i]._name,
3747836SJohn.Forte@Sun.COM 		    kstat_items_conn[i]._data_type);
3757836SJohn.Forte@Sun.COM 	}
3767836SJohn.Forte@Sun.COM 
3777836SJohn.Forte@Sun.COM 	/* The static information is updated immediately */
3787836SJohn.Forte@Sun.COM 	bzero(ics->sess_str, sizeof (ics->sess_str));
3797836SJohn.Forte@Sun.COM 	bcopy(isp->stats.ks->ks_name,
3807836SJohn.Forte@Sun.COM 	    ics->sess_str,
3817836SJohn.Forte@Sun.COM 	    sizeof (ics->sess_str));
3827836SJohn.Forte@Sun.COM 
3837836SJohn.Forte@Sun.COM 	kstat_named_setstr(&ics->kn[KN_CONN_IDX_SESS],
3847836SJohn.Forte@Sun.COM 	    (const char *)ics->sess_str);
3857836SJohn.Forte@Sun.COM 
3867836SJohn.Forte@Sun.COM 	ics->kn[KN_CONN_IDX_OID].value.ul = isp->sess_oid;
3877836SJohn.Forte@Sun.COM 	ics->kn[KN_CONN_IDX_CID].value.ul = icp->conn_cid;
3887836SJohn.Forte@Sun.COM 	icp->stats.ks->ks_update = iscsi_conn_kstat_update;
3897836SJohn.Forte@Sun.COM 	icp->stats.ks->ks_private = (void *)icp;
3907836SJohn.Forte@Sun.COM 
3917836SJohn.Forte@Sun.COM 	kstat_install(icp->stats.ks);
3927836SJohn.Forte@Sun.COM 
3937836SJohn.Forte@Sun.COM 	return (FALSE);
3947836SJohn.Forte@Sun.COM }
3957836SJohn.Forte@Sun.COM 
3967836SJohn.Forte@Sun.COM /*
3977836SJohn.Forte@Sun.COM  * iscsi_conn_kstat_term - This function deregisters with the kstat service.
3987836SJohn.Forte@Sun.COM  */
3997836SJohn.Forte@Sun.COM void
iscsi_conn_kstat_term(iscsi_conn_t * icp)4007836SJohn.Forte@Sun.COM iscsi_conn_kstat_term(iscsi_conn_t *icp)
4017836SJohn.Forte@Sun.COM {
4027836SJohn.Forte@Sun.COM 	kstat_delete(icp->stats.ks);
4037836SJohn.Forte@Sun.COM }
4047836SJohn.Forte@Sun.COM 
4057836SJohn.Forte@Sun.COM /*
4067836SJohn.Forte@Sun.COM  * iscsi_conn_kstat_update - This function update the kstat
4077836SJohn.Forte@Sun.COM  *	structure of the HBA.
4087836SJohn.Forte@Sun.COM  */
4097836SJohn.Forte@Sun.COM int
iscsi_conn_kstat_update(kstat_t * ks,int rw)4107836SJohn.Forte@Sun.COM iscsi_conn_kstat_update(kstat_t *ks, int rw)
4117836SJohn.Forte@Sun.COM {
4127836SJohn.Forte@Sun.COM 	iscsi_conn_t	*icp = (iscsi_conn_t *)ks->ks_private;
4137836SJohn.Forte@Sun.COM 	iscsi_conn_stats_t	*ics = &icp->stats.ks_data;
4147836SJohn.Forte@Sun.COM 	int			len;
4157836SJohn.Forte@Sun.COM 
4167836SJohn.Forte@Sun.COM 	if (rw == KSTAT_READ) {
417*9162SPeter.Dunlap@Sun.COM 		len =  strlen(iscsi_ics_name[icp->conn_state]);
418*9162SPeter.Dunlap@Sun.COM 		if (len > (sizeof (ics->state_str) - 1)) {
419*9162SPeter.Dunlap@Sun.COM 			len = sizeof (ics->state_str) - 1;
4207836SJohn.Forte@Sun.COM 		}
4217836SJohn.Forte@Sun.COM 		bzero(ics->state_str, sizeof (ics->state_str));
422*9162SPeter.Dunlap@Sun.COM 		bcopy(iscsi_ics_name[icp->conn_state], ics->state_str, len);
4237836SJohn.Forte@Sun.COM 		kstat_named_setstr(&ics->kn[KN_CONN_IDX_STATE],
4247836SJohn.Forte@Sun.COM 		    (const char *)ics->state_str);
4257836SJohn.Forte@Sun.COM 
4267836SJohn.Forte@Sun.COM 		ics->kn[KN_CONN_IDX_CNTR_QACTIVE].value.ul =
4277836SJohn.Forte@Sun.COM 		    icp->conn_queue_active.count;
4287836SJohn.Forte@Sun.COM 		ics->kn[KN_CONN_IDX_EXPSTATSN].value.ul =
4297836SJohn.Forte@Sun.COM 		    icp->conn_expstatsn;
4307836SJohn.Forte@Sun.COM 		ics->kn[KN_CONN_IDX_LASTSTATSN].value.ul =
4317836SJohn.Forte@Sun.COM 		    icp->conn_laststatsn;
4327836SJohn.Forte@Sun.COM 	}
4337836SJohn.Forte@Sun.COM 	return (0);
4347836SJohn.Forte@Sun.COM }
435