xref: /illumos-gate/usr/src/uts/common/io/scsi/adapters/iscsi/iscsi_stats.c (revision 30e7468f8f41aa30ada067b2c1d5d284046514da)
1fcf3ce44SJohn Forte /*
2fcf3ce44SJohn Forte  * CDDL HEADER START
3fcf3ce44SJohn Forte  *
4fcf3ce44SJohn Forte  * The contents of this file are subject to the terms of the
5fcf3ce44SJohn Forte  * Common Development and Distribution License (the "License").
6fcf3ce44SJohn Forte  * You may not use this file except in compliance with the License.
7fcf3ce44SJohn Forte  *
8fcf3ce44SJohn Forte  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fcf3ce44SJohn Forte  * or http://www.opensolaris.org/os/licensing.
10fcf3ce44SJohn Forte  * See the License for the specific language governing permissions
11fcf3ce44SJohn Forte  * and limitations under the License.
12fcf3ce44SJohn Forte  *
13fcf3ce44SJohn Forte  * When distributing Covered Code, include this CDDL HEADER in each
14fcf3ce44SJohn Forte  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fcf3ce44SJohn Forte  * If applicable, add the following below this CDDL HEADER, with the
16fcf3ce44SJohn Forte  * fields enclosed by brackets "[]" replaced with your own identifying
17fcf3ce44SJohn Forte  * information: Portions Copyright [yyyy] [name of copyright owner]
18fcf3ce44SJohn Forte  *
19fcf3ce44SJohn Forte  * CDDL HEADER END
20fcf3ce44SJohn Forte  */
21fcf3ce44SJohn Forte /*
22*30e7468fSPeter Dunlap  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23fcf3ce44SJohn Forte  * Use is subject to license terms.
24fcf3ce44SJohn Forte  *
25fcf3ce44SJohn Forte  * iSCSI Software Initiator
26fcf3ce44SJohn Forte  */
27fcf3ce44SJohn Forte 
28*30e7468fSPeter Dunlap #define	ISCSI_ICS_NAMES
29fcf3ce44SJohn Forte #include "iscsi.h"	/* main header */
30fcf3ce44SJohn Forte 
31fcf3ce44SJohn Forte kstat_item_t	kstat_items_hba[KN_HBA_IDX_MAX] = {
32fcf3ce44SJohn Forte 	{"_name", KSTAT_DATA_STRING},
33fcf3ce44SJohn Forte 	{"_alias", KSTAT_DATA_STRING},
34fcf3ce44SJohn Forte 	{"_cntr_sess", KSTAT_DATA_ULONG}
35fcf3ce44SJohn Forte };
36fcf3ce44SJohn Forte 
37fcf3ce44SJohn Forte kstat_item_t	kstat_items_sess[KN_SESS_IDX_MAX] = {
38fcf3ce44SJohn Forte 	{"_state", KSTAT_DATA_STRING},
39fcf3ce44SJohn Forte 	{"_oid", KSTAT_DATA_ULONG},
40fcf3ce44SJohn Forte 	{"_hba", KSTAT_DATA_STRING},
41fcf3ce44SJohn Forte 	{"_cntr_conn", KSTAT_DATA_ULONG},
42fcf3ce44SJohn Forte 	{"_cntr_reset", KSTAT_DATA_ULONG},
43fcf3ce44SJohn Forte 	{"_cntr_pkt_pending", KSTAT_DATA_ULONG},
44fcf3ce44SJohn Forte 	{"_cmd_sn", KSTAT_DATA_ULONG},
45fcf3ce44SJohn Forte 	{"_cmd_sn_exp", KSTAT_DATA_ULONG},
46fcf3ce44SJohn Forte 	{"_cmd_sn_max", KSTAT_DATA_ULONG},
47fcf3ce44SJohn Forte 	{"_target_name", KSTAT_DATA_STRING},
48fcf3ce44SJohn Forte 	{"_target_alias", KSTAT_DATA_STRING},
49fcf3ce44SJohn Forte 	{"_tpgt", KSTAT_DATA_ULONG}
50fcf3ce44SJohn Forte };
51fcf3ce44SJohn Forte 
52fcf3ce44SJohn Forte kstat_item_t	kstat_items_conn[KN_CONN_IDX_MAX] = {
53fcf3ce44SJohn Forte 	{"_state", KSTAT_DATA_STRING},
54fcf3ce44SJohn Forte 	{"_cid", KSTAT_DATA_ULONG},
55fcf3ce44SJohn Forte 	{"_oid", KSTAT_DATA_ULONG},
56fcf3ce44SJohn Forte 	{"_session", KSTAT_DATA_STRING},
57fcf3ce44SJohn Forte 	{"_err_header_digest", KSTAT_DATA_ULONG},
58fcf3ce44SJohn Forte 	{"_err_data_digest", KSTAT_DATA_ULONG},
59fcf3ce44SJohn Forte 	{"_err_connection_reset", KSTAT_DATA_ULONG},
60fcf3ce44SJohn Forte 	{"_err_protocol_error", KSTAT_DATA_ULONG},
61fcf3ce44SJohn Forte 	{"_cntr_tx_bytes", KSTAT_DATA_ULONGLONG},
62fcf3ce44SJohn Forte 	{"_cntr_rx_bytes", KSTAT_DATA_ULONGLONG},
63fcf3ce44SJohn Forte 	{"_cntr_qactive", KSTAT_DATA_ULONG},
64fcf3ce44SJohn Forte 	{"_stat_sn_exp", KSTAT_DATA_ULONG},
65fcf3ce44SJohn Forte 	{"_stat_sn_last", KSTAT_DATA_ULONG}
66fcf3ce44SJohn Forte };
67fcf3ce44SJohn Forte 
68fcf3ce44SJohn Forte int iscsi_hba_kstat_update(kstat_t *ks, int rw);
69fcf3ce44SJohn Forte int iscsi_sess_kstat_update(kstat_t *ks, int rw);
70fcf3ce44SJohn Forte int iscsi_conn_kstat_update(kstat_t *ks, int rw);
71fcf3ce44SJohn Forte 
72fcf3ce44SJohn Forte /*
73fcf3ce44SJohn Forte  * HBA
74fcf3ce44SJohn Forte  */
75fcf3ce44SJohn Forte 
76fcf3ce44SJohn Forte /*
77fcf3ce44SJohn Forte  * iscsi_hba_kstat_init - This function registers with the kstat service.
78fcf3ce44SJohn Forte  */
79fcf3ce44SJohn Forte boolean_t
iscsi_hba_kstat_init(iscsi_hba_t * ihp)80fcf3ce44SJohn Forte iscsi_hba_kstat_init(iscsi_hba_t *ihp)
81fcf3ce44SJohn Forte {
82fcf3ce44SJohn Forte 	char			ks_name[KSTAT_STRLEN];
83fcf3ce44SJohn Forte 	iscsi_hba_stats_t	*ihs;
84fcf3ce44SJohn Forte 	int			i;
85fcf3ce44SJohn Forte 
86fcf3ce44SJohn Forte 	/*
87fcf3ce44SJohn Forte 	 * The name of the KSTAT structure is built.
88fcf3ce44SJohn Forte 	 */
89fcf3ce44SJohn Forte 	bzero(ks_name, sizeof (ks_name));
90fcf3ce44SJohn Forte 
91fcf3ce44SJohn Forte 	if (snprintf(ks_name, sizeof (ks_name) - 1, iSCSI_HBA_BASE_NAME,
92fcf3ce44SJohn Forte 	    ihp->hba_oid) >= sizeof (ks_name)) {
93fcf3ce44SJohn Forte 		return (TRUE);
94fcf3ce44SJohn Forte 	}
95fcf3ce44SJohn Forte 
96fcf3ce44SJohn Forte 	ihp->stats.ks = kstat_create(iSCSI_MODULE_NAME,
97fcf3ce44SJohn Forte 	    ddi_get_instance(ihp->hba_dip), ks_name, iSCSI_CLASS_HBA,
98fcf3ce44SJohn Forte 	    KSTAT_TYPE_NAMED, 0, KSTAT_FLAG_VIRTUAL);
99fcf3ce44SJohn Forte 
100fcf3ce44SJohn Forte 	if (ihp->stats.ks == NULL) {
101fcf3ce44SJohn Forte 		cmn_err(CE_NOTE, "iscsi kstat creation failed for hba(%d)",
102fcf3ce44SJohn Forte 		    ihp->hba_oid);
103fcf3ce44SJohn Forte 		return (TRUE);
104fcf3ce44SJohn Forte 	}
105fcf3ce44SJohn Forte 
106fcf3ce44SJohn Forte 	ihs = &ihp->stats.ks_data;
107fcf3ce44SJohn Forte 	ihp->stats.ks->ks_data = &ihp->stats.ks_data;
108fcf3ce44SJohn Forte 	ihp->stats.ks->ks_data_size = sizeof (ihp->stats.ks_data);
109fcf3ce44SJohn Forte 	ihp->stats.ks->ks_ndata = KN_HBA_IDX_MAX;
110fcf3ce44SJohn Forte 
111fcf3ce44SJohn Forte 	for (i = 0; i < KN_HBA_IDX_MAX; i++) {
112fcf3ce44SJohn Forte 		kstat_named_init(&ihs->kn[i], kstat_items_hba[i]._name,
113fcf3ce44SJohn Forte 		    kstat_items_hba[i]._data_type);
114fcf3ce44SJohn Forte 	}
115fcf3ce44SJohn Forte 
116fcf3ce44SJohn Forte 	ihp->stats.ks->ks_update = iscsi_hba_kstat_update;
117fcf3ce44SJohn Forte 	ihp->stats.ks->ks_private = (void *)ihp;
118fcf3ce44SJohn Forte 
119fcf3ce44SJohn Forte 	kstat_install(ihp->stats.ks);
120fcf3ce44SJohn Forte 
121fcf3ce44SJohn Forte 	return (FALSE);
122fcf3ce44SJohn Forte }
123fcf3ce44SJohn Forte 
124fcf3ce44SJohn Forte /*
125fcf3ce44SJohn Forte  * iscsi_hba_kstat_term - This function deregisters from the kstat service.
126fcf3ce44SJohn Forte  */
127fcf3ce44SJohn Forte boolean_t
iscsi_hba_kstat_term(iscsi_hba_t * ihp)128fcf3ce44SJohn Forte iscsi_hba_kstat_term(iscsi_hba_t *ihp)
129fcf3ce44SJohn Forte {
130fcf3ce44SJohn Forte 	kstat_delete(ihp->stats.ks);
131fcf3ce44SJohn Forte 	return (FALSE);
132fcf3ce44SJohn Forte }
133fcf3ce44SJohn Forte 
134fcf3ce44SJohn Forte /*
135fcf3ce44SJohn Forte  * iscsi_hba_kstat_update - This function update the kstat structure of the HBA.
136fcf3ce44SJohn Forte  */
137fcf3ce44SJohn Forte int
iscsi_hba_kstat_update(kstat_t * ks,int rw)138fcf3ce44SJohn Forte iscsi_hba_kstat_update(kstat_t *ks, int rw)
139fcf3ce44SJohn Forte {
140fcf3ce44SJohn Forte 	iscsi_hba_t		*ihp = (iscsi_hba_t *)ks->ks_private;
141fcf3ce44SJohn Forte 	iscsi_hba_stats_t	*ihs = &ihp->stats.ks_data;
142fcf3ce44SJohn Forte 
143fcf3ce44SJohn Forte 	if (rw == KSTAT_READ) {
144fcf3ce44SJohn Forte 		rw_enter(&ihp->hba_sess_list_rwlock, RW_READER);
145fcf3ce44SJohn Forte 		bcopy(ihp->hba_name, ihs->name, ihp->hba_name_length);
146fcf3ce44SJohn Forte 
147fcf3ce44SJohn Forte 		bcopy(ihp->hba_alias, ihs->alias, ihp->hba_alias_length);
148fcf3ce44SJohn Forte 
149fcf3ce44SJohn Forte 		ihs->name[ihp->hba_name_length] = 0;
150fcf3ce44SJohn Forte 		ihs->alias[ihp->hba_alias_length] = 0;
151fcf3ce44SJohn Forte 
152fcf3ce44SJohn Forte 		kstat_named_setstr(&ihs->kn[KN_HBA_IDX_NAME],
153fcf3ce44SJohn Forte 		    (const char *)ihs->name);
154fcf3ce44SJohn Forte 		kstat_named_setstr(&ihs->kn[KN_HBA_IDX_ALIAS],
155fcf3ce44SJohn Forte 		    (const char *)ihs->alias);
156fcf3ce44SJohn Forte 		rw_exit(&ihp->hba_sess_list_rwlock);
157fcf3ce44SJohn Forte 	}
158fcf3ce44SJohn Forte 	return (0);
159fcf3ce44SJohn Forte }
160fcf3ce44SJohn Forte 
161fcf3ce44SJohn Forte /*
162fcf3ce44SJohn Forte  * Session
163fcf3ce44SJohn Forte  */
164fcf3ce44SJohn Forte 
165fcf3ce44SJohn Forte /*
166fcf3ce44SJohn Forte  * iscsi_sess_kstat_init - This function registers with the kstat service.
167fcf3ce44SJohn Forte  */
168fcf3ce44SJohn Forte boolean_t
iscsi_sess_kstat_init(iscsi_sess_t * isp)169fcf3ce44SJohn Forte iscsi_sess_kstat_init(iscsi_sess_t *isp)
170fcf3ce44SJohn Forte {
171fcf3ce44SJohn Forte 	iscsi_hba_t		*ihp;
172fcf3ce44SJohn Forte 	char			ks_name[KSTAT_STRLEN];
173fcf3ce44SJohn Forte 	iscsi_sess_stats_t	*iss;
174fcf3ce44SJohn Forte 	int			i;
175fcf3ce44SJohn Forte 
176fcf3ce44SJohn Forte 	ASSERT(isp != NULL);
177fcf3ce44SJohn Forte 	ihp = isp->sess_hba;
178fcf3ce44SJohn Forte 	ASSERT(ihp != NULL);
179fcf3ce44SJohn Forte 
180fcf3ce44SJohn Forte 	/*
181fcf3ce44SJohn Forte 	 * The name of the KSTAT structure is built.
182fcf3ce44SJohn Forte 	 */
183fcf3ce44SJohn Forte 	bzero(ks_name, sizeof (ks_name));
184fcf3ce44SJohn Forte 
185fcf3ce44SJohn Forte 	if (snprintf(ks_name, sizeof (ks_name) - 1, iSCSI_SESS_BASE_NAME,
186fcf3ce44SJohn Forte 	    isp->sess_hba->hba_oid, isp->sess_oid) >= sizeof (ks_name)) {
187fcf3ce44SJohn Forte 		cmn_err(CE_NOTE, "iscsi kstat creation failed for "
188fcf3ce44SJohn Forte 		    "session(%u)", isp->sess_oid);
189fcf3ce44SJohn Forte 		return (TRUE);
190fcf3ce44SJohn Forte 	}
191fcf3ce44SJohn Forte 
192fcf3ce44SJohn Forte 	isp->stats.ks = kstat_create(iSCSI_MODULE_NAME,
193fcf3ce44SJohn Forte 	    ddi_get_instance(ihp->hba_dip), ks_name, iSCSI_CLASS_SESS,
194fcf3ce44SJohn Forte 	    KSTAT_TYPE_NAMED, 0, KSTAT_FLAG_VIRTUAL);
195fcf3ce44SJohn Forte 
196fcf3ce44SJohn Forte 	if (isp->stats.ks == NULL) {
197fcf3ce44SJohn Forte 		cmn_err(CE_NOTE, "iscsi kstat creation failed "
198fcf3ce44SJohn Forte 		    "for session(%u)", isp->sess_oid);
199fcf3ce44SJohn Forte 		return (TRUE);
200fcf3ce44SJohn Forte 	}
201fcf3ce44SJohn Forte 
202fcf3ce44SJohn Forte 	iss = &isp->stats.ks_data;
203fcf3ce44SJohn Forte 	isp->stats.ks->ks_data = (void *)&isp->stats.ks_data;
204fcf3ce44SJohn Forte 	isp->stats.ks->ks_data_size = sizeof (isp->stats.ks_data);
205fcf3ce44SJohn Forte 	isp->stats.ks->ks_ndata = KN_SESS_IDX_MAX;
206fcf3ce44SJohn Forte 
207fcf3ce44SJohn Forte 	for (i = 0; i < KN_SESS_IDX_MAX; i++) {
208fcf3ce44SJohn Forte 		kstat_named_init(&iss->kn[i], kstat_items_sess[i]._name,
209fcf3ce44SJohn Forte 		    kstat_items_sess[i]._data_type);
210fcf3ce44SJohn Forte 	}
211fcf3ce44SJohn Forte 
212fcf3ce44SJohn Forte 	/* The static information is updated immediately */
213fcf3ce44SJohn Forte 	bzero(iss->hba_str, sizeof (iss->hba_str));
214fcf3ce44SJohn Forte 	bcopy(ihp->stats.ks->ks_name, iss->hba_str, sizeof (iss->hba_str));
215fcf3ce44SJohn Forte 	kstat_named_setstr(&iss->kn[KN_SESS_IDX_HBA],
216fcf3ce44SJohn Forte 	    (const char *)iss->hba_str);
217fcf3ce44SJohn Forte 
218fcf3ce44SJohn Forte 	iss->kn[KN_SESS_IDX_OID].value.ul = isp->sess_oid;
219fcf3ce44SJohn Forte 
220fcf3ce44SJohn Forte 	isp->stats.ks->ks_update = iscsi_sess_kstat_update;
221fcf3ce44SJohn Forte 	isp->stats.ks->ks_private = (void *)isp;
222fcf3ce44SJohn Forte 
223fcf3ce44SJohn Forte 	/* The IO KSTAT structure is created */
224fcf3ce44SJohn Forte 	bzero(ks_name, sizeof (ks_name));
225fcf3ce44SJohn Forte 
226fcf3ce44SJohn Forte 	if (snprintf(ks_name, sizeof (ks_name) - 1, iSCSI_SESS_IO_BASE_NAME,
227fcf3ce44SJohn Forte 	    isp->sess_hba->hba_oid, isp->sess_oid) >= sizeof (ks_name)) {
228fcf3ce44SJohn Forte 		cmn_err(CE_NOTE, "iscsi kstat createion failed "
229fcf3ce44SJohn Forte 		    "for session(%u)", isp->sess_oid);
230fcf3ce44SJohn Forte 		kstat_delete(isp->stats.ks);
231fcf3ce44SJohn Forte 		return (TRUE);
232fcf3ce44SJohn Forte 	}
233fcf3ce44SJohn Forte 
234fcf3ce44SJohn Forte 	isp->stats.ks_io = kstat_create(iSCSI_MODULE_NAME,
235fcf3ce44SJohn Forte 	    ddi_get_instance(ihp->hba_dip), ks_name, iSCSI_CLASS_SESS,
236fcf3ce44SJohn Forte 	    KSTAT_TYPE_IO, 1, KSTAT_FLAG_VIRTUAL);
237fcf3ce44SJohn Forte 
238fcf3ce44SJohn Forte 	if (isp->stats.ks_io == NULL) {
239fcf3ce44SJohn Forte 		kstat_delete(isp->stats.ks);
240fcf3ce44SJohn Forte 		cmn_err(CE_NOTE, "iscsi kstat creation failed "
241fcf3ce44SJohn Forte 		    "for session(%u)", isp->sess_oid);
242fcf3ce44SJohn Forte 		return (TRUE);
243fcf3ce44SJohn Forte 	}
244fcf3ce44SJohn Forte 	mutex_init(&isp->stats.ks_io_lock, NULL, MUTEX_DRIVER, NULL);
245fcf3ce44SJohn Forte 	isp->stats.ks_io->ks_data = &isp->stats.ks_io_data;
246fcf3ce44SJohn Forte 	isp->stats.ks_io->ks_lock = &isp->stats.ks_io_lock;
247fcf3ce44SJohn Forte 
248fcf3ce44SJohn Forte 	kstat_install(isp->stats.ks);
249fcf3ce44SJohn Forte 	kstat_install(isp->stats.ks_io);
250fcf3ce44SJohn Forte 
251fcf3ce44SJohn Forte 	return (FALSE);
252fcf3ce44SJohn Forte }
253fcf3ce44SJohn Forte 
254fcf3ce44SJohn Forte /*
255fcf3ce44SJohn Forte  * iscsi_sess_kstat_term - This function deregisters with the kstat service.
256fcf3ce44SJohn Forte  */
257fcf3ce44SJohn Forte boolean_t
iscsi_sess_kstat_term(iscsi_sess_t * isp)258fcf3ce44SJohn Forte iscsi_sess_kstat_term(iscsi_sess_t *isp)
259fcf3ce44SJohn Forte {
260fcf3ce44SJohn Forte 	kstat_delete(isp->stats.ks_io);
261fcf3ce44SJohn Forte 	mutex_destroy(&isp->stats.ks_io_lock);
262fcf3ce44SJohn Forte 	kstat_delete(isp->stats.ks);
263fcf3ce44SJohn Forte 	return (FALSE);
264fcf3ce44SJohn Forte }
265fcf3ce44SJohn Forte 
266fcf3ce44SJohn Forte /*
267fcf3ce44SJohn Forte  * iscsi_sess_kstat_update - This function update the kstat
268fcf3ce44SJohn Forte  *	structure of the HBA.
269fcf3ce44SJohn Forte  */
270fcf3ce44SJohn Forte int
iscsi_sess_kstat_update(kstat_t * ks,int rw)271fcf3ce44SJohn Forte iscsi_sess_kstat_update(kstat_t *ks, int rw)
272fcf3ce44SJohn Forte {
273fcf3ce44SJohn Forte 	iscsi_sess_t		*isp = (iscsi_sess_t *)ks->ks_private;
274fcf3ce44SJohn Forte 	iscsi_sess_stats_t	*iss = &isp->stats.ks_data;
275fcf3ce44SJohn Forte 	char			*ptr;
276fcf3ce44SJohn Forte 	int			len;
277fcf3ce44SJohn Forte 
278fcf3ce44SJohn Forte 	if (rw == KSTAT_READ) {
279fcf3ce44SJohn Forte 
280fcf3ce44SJohn Forte 		/* String indicating the state of the session */
281fcf3ce44SJohn Forte 		ptr = iscsi_sess_state_str(isp->sess_state);
282fcf3ce44SJohn Forte 		len =  strlen(ptr);
283*30e7468fSPeter Dunlap 		if (len > (sizeof (iss->state_str) - 1)) {
284*30e7468fSPeter Dunlap 			len = sizeof (iss->state_str) - 1;
285fcf3ce44SJohn Forte 		}
286fcf3ce44SJohn Forte 		bzero(iss->state_str, sizeof (iss->state_str));
287fcf3ce44SJohn Forte 		bcopy(ptr, iss->state_str, len);
288fcf3ce44SJohn Forte 		kstat_named_setstr(
289fcf3ce44SJohn Forte 		    &iss->kn[KN_SESS_IDX_STATE],
290fcf3ce44SJohn Forte 		    (const char *)iss->state_str);
291fcf3ce44SJohn Forte 
292fcf3ce44SJohn Forte 		/* Target name string */
293fcf3ce44SJohn Forte 		if (isp->sess_name_length > sizeof (iss->target_name)) {
294fcf3ce44SJohn Forte 			len = sizeof (iss->target_name);
295fcf3ce44SJohn Forte 		} else {
296fcf3ce44SJohn Forte 			len =  isp->sess_name_length;
297fcf3ce44SJohn Forte 		}
298fcf3ce44SJohn Forte 		bzero(iss->target_name, sizeof (iss->target_name));
299fcf3ce44SJohn Forte 		bcopy(isp->sess_name, iss->target_name, len);
300fcf3ce44SJohn Forte 		kstat_named_setstr(&iss->kn[KN_SESS_IDX_TARGET_NAME],
301fcf3ce44SJohn Forte 		    (const char *)iss->target_name);
302fcf3ce44SJohn Forte 
303fcf3ce44SJohn Forte 		/* Target alias string */
304fcf3ce44SJohn Forte 		if (isp->sess_alias_length > sizeof (iss->target_alias)) {
305fcf3ce44SJohn Forte 			len = sizeof (iss->target_alias);
306fcf3ce44SJohn Forte 		} else {
307fcf3ce44SJohn Forte 			len =  isp->sess_alias_length;
308fcf3ce44SJohn Forte 		}
309fcf3ce44SJohn Forte 		bzero(iss->target_alias, sizeof (iss->target_alias));
310fcf3ce44SJohn Forte 		bcopy(isp->sess_alias, iss->target_alias, len);
311fcf3ce44SJohn Forte 		kstat_named_setstr(
312fcf3ce44SJohn Forte 		    &iss->kn[KN_SESS_IDX_TARGET_ALIAS],
313fcf3ce44SJohn Forte 		    (const char *)iss->target_alias);
314fcf3ce44SJohn Forte 
315fcf3ce44SJohn Forte 		iss->kn[KN_SESS_IDX_CNTR_PKT_PENDING].value.ul =
316fcf3ce44SJohn Forte 		    isp->sess_queue_pending.count;
317fcf3ce44SJohn Forte 		iss->kn[KN_SESS_IDX_CMDSN].value.ul =
318fcf3ce44SJohn Forte 		    isp->sess_cmdsn;
319fcf3ce44SJohn Forte 		iss->kn[KN_SESS_IDX_EXPCMDSN].value.ul =
320fcf3ce44SJohn Forte 		    isp->sess_expcmdsn;
321fcf3ce44SJohn Forte 		iss->kn[KN_SESS_IDX_MAXCMDSN].value.ul =
322fcf3ce44SJohn Forte 		    isp->sess_maxcmdsn;
323fcf3ce44SJohn Forte 		iss->kn[KN_SESS_IDX_TPGT].value.ul =
324fcf3ce44SJohn Forte 		    isp->sess_tpgt_conf;
325fcf3ce44SJohn Forte 
326fcf3ce44SJohn Forte 	}
327fcf3ce44SJohn Forte 	return (0);
328fcf3ce44SJohn Forte }
329fcf3ce44SJohn Forte 
330fcf3ce44SJohn Forte /*
331fcf3ce44SJohn Forte  * Connection
332fcf3ce44SJohn Forte  */
333fcf3ce44SJohn Forte 
334fcf3ce44SJohn Forte /*
335fcf3ce44SJohn Forte  * iscsi_conn_kstat_init - This function registers with the kstat service.
336fcf3ce44SJohn Forte  */
337fcf3ce44SJohn Forte boolean_t
iscsi_conn_kstat_init(iscsi_conn_t * icp)338fcf3ce44SJohn Forte iscsi_conn_kstat_init(iscsi_conn_t *icp)
339fcf3ce44SJohn Forte {
340fcf3ce44SJohn Forte 	iscsi_sess_t		*isp = icp->conn_sess;
341fcf3ce44SJohn Forte 	iscsi_hba_t		*ihp = isp->sess_hba;
342fcf3ce44SJohn Forte 	iscsi_conn_stats_t	*ics;
343fcf3ce44SJohn Forte 	int			i;
344fcf3ce44SJohn Forte 	char			ks_name[KSTAT_STRLEN];
345fcf3ce44SJohn Forte 
346fcf3ce44SJohn Forte 	/*
347fcf3ce44SJohn Forte 	 * The name of the KSTAT structure is built.
348fcf3ce44SJohn Forte 	 */
349fcf3ce44SJohn Forte 	bzero(ks_name, sizeof (ks_name));
350fcf3ce44SJohn Forte 
351fcf3ce44SJohn Forte 	if (snprintf(ks_name, sizeof (ks_name) - 1, iSCSI_CONN_BASE_NAME,
352fcf3ce44SJohn Forte 	    icp->conn_sess->sess_hba->hba_oid, icp->conn_sess->sess_oid,
353fcf3ce44SJohn Forte 	    icp->conn_oid) >= sizeof (ks_name)) {
354fcf3ce44SJohn Forte 		return (TRUE);
355fcf3ce44SJohn Forte 	}
356fcf3ce44SJohn Forte 
357fcf3ce44SJohn Forte 	icp->stats.ks = kstat_create(iSCSI_MODULE_NAME,
358fcf3ce44SJohn Forte 	    ddi_get_instance(ihp->hba_dip), ks_name, iSCSI_CLASS_CONN,
359fcf3ce44SJohn Forte 	    KSTAT_TYPE_NAMED, 0, KSTAT_FLAG_VIRTUAL);
360fcf3ce44SJohn Forte 
361fcf3ce44SJohn Forte 	if (icp->stats.ks == NULL) {
362fcf3ce44SJohn Forte 		cmn_err(CE_NOTE, "iscsi kstat creation failed "
363fcf3ce44SJohn Forte 		    "for connection(%d)", icp->conn_oid);
364fcf3ce44SJohn Forte 		return (TRUE);
365fcf3ce44SJohn Forte 	}
366fcf3ce44SJohn Forte 
367fcf3ce44SJohn Forte 	ics = &icp->stats.ks_data;
368fcf3ce44SJohn Forte 	icp->stats.ks->ks_data = (void *)ics;
369fcf3ce44SJohn Forte 	icp->stats.ks->ks_data_size = sizeof (*ics);
370fcf3ce44SJohn Forte 	icp->stats.ks->ks_ndata = KN_CONN_IDX_MAX;
371fcf3ce44SJohn Forte 
372fcf3ce44SJohn Forte 	for (i = 0; i < KN_CONN_IDX_MAX; i++) {
373fcf3ce44SJohn Forte 		kstat_named_init(&ics->kn[i], kstat_items_conn[i]._name,
374fcf3ce44SJohn Forte 		    kstat_items_conn[i]._data_type);
375fcf3ce44SJohn Forte 	}
376fcf3ce44SJohn Forte 
377fcf3ce44SJohn Forte 	/* The static information is updated immediately */
378fcf3ce44SJohn Forte 	bzero(ics->sess_str, sizeof (ics->sess_str));
379fcf3ce44SJohn Forte 	bcopy(isp->stats.ks->ks_name,
380fcf3ce44SJohn Forte 	    ics->sess_str,
381fcf3ce44SJohn Forte 	    sizeof (ics->sess_str));
382fcf3ce44SJohn Forte 
383fcf3ce44SJohn Forte 	kstat_named_setstr(&ics->kn[KN_CONN_IDX_SESS],
384fcf3ce44SJohn Forte 	    (const char *)ics->sess_str);
385fcf3ce44SJohn Forte 
386fcf3ce44SJohn Forte 	ics->kn[KN_CONN_IDX_OID].value.ul = isp->sess_oid;
387fcf3ce44SJohn Forte 	ics->kn[KN_CONN_IDX_CID].value.ul = icp->conn_cid;
388fcf3ce44SJohn Forte 	icp->stats.ks->ks_update = iscsi_conn_kstat_update;
389fcf3ce44SJohn Forte 	icp->stats.ks->ks_private = (void *)icp;
390fcf3ce44SJohn Forte 
391fcf3ce44SJohn Forte 	kstat_install(icp->stats.ks);
392fcf3ce44SJohn Forte 
393fcf3ce44SJohn Forte 	return (FALSE);
394fcf3ce44SJohn Forte }
395fcf3ce44SJohn Forte 
396fcf3ce44SJohn Forte /*
397fcf3ce44SJohn Forte  * iscsi_conn_kstat_term - This function deregisters with the kstat service.
398fcf3ce44SJohn Forte  */
399fcf3ce44SJohn Forte void
iscsi_conn_kstat_term(iscsi_conn_t * icp)400fcf3ce44SJohn Forte iscsi_conn_kstat_term(iscsi_conn_t *icp)
401fcf3ce44SJohn Forte {
402fcf3ce44SJohn Forte 	kstat_delete(icp->stats.ks);
403fcf3ce44SJohn Forte }
404fcf3ce44SJohn Forte 
405fcf3ce44SJohn Forte /*
406fcf3ce44SJohn Forte  * iscsi_conn_kstat_update - This function update the kstat
407fcf3ce44SJohn Forte  *	structure of the HBA.
408fcf3ce44SJohn Forte  */
409fcf3ce44SJohn Forte int
iscsi_conn_kstat_update(kstat_t * ks,int rw)410fcf3ce44SJohn Forte iscsi_conn_kstat_update(kstat_t *ks, int rw)
411fcf3ce44SJohn Forte {
412fcf3ce44SJohn Forte 	iscsi_conn_t	*icp = (iscsi_conn_t *)ks->ks_private;
413fcf3ce44SJohn Forte 	iscsi_conn_stats_t	*ics = &icp->stats.ks_data;
414fcf3ce44SJohn Forte 	int			len;
415fcf3ce44SJohn Forte 
416fcf3ce44SJohn Forte 	if (rw == KSTAT_READ) {
417*30e7468fSPeter Dunlap 		len =  strlen(iscsi_ics_name[icp->conn_state]);
418*30e7468fSPeter Dunlap 		if (len > (sizeof (ics->state_str) - 1)) {
419*30e7468fSPeter Dunlap 			len = sizeof (ics->state_str) - 1;
420fcf3ce44SJohn Forte 		}
421fcf3ce44SJohn Forte 		bzero(ics->state_str, sizeof (ics->state_str));
422*30e7468fSPeter Dunlap 		bcopy(iscsi_ics_name[icp->conn_state], ics->state_str, len);
423fcf3ce44SJohn Forte 		kstat_named_setstr(&ics->kn[KN_CONN_IDX_STATE],
424fcf3ce44SJohn Forte 		    (const char *)ics->state_str);
425fcf3ce44SJohn Forte 
426fcf3ce44SJohn Forte 		ics->kn[KN_CONN_IDX_CNTR_QACTIVE].value.ul =
427fcf3ce44SJohn Forte 		    icp->conn_queue_active.count;
428fcf3ce44SJohn Forte 		ics->kn[KN_CONN_IDX_EXPSTATSN].value.ul =
429fcf3ce44SJohn Forte 		    icp->conn_expstatsn;
430fcf3ce44SJohn Forte 		ics->kn[KN_CONN_IDX_LASTSTATSN].value.ul =
431fcf3ce44SJohn Forte 		    icp->conn_laststatsn;
432fcf3ce44SJohn Forte 	}
433fcf3ce44SJohn Forte 	return (0);
434fcf3ce44SJohn Forte }
435