1 /*******************************************************************************
2  * Filename:  target_core_stat.c
3  *
4  * Copyright (c) 2011 Rising Tide Systems
5  * Copyright (c) 2011 Linux-iSCSI.org
6  *
7  * Modern ConfigFS group context specific statistics based on original
8  * target_core_mib.c code
9  *
10  * Copyright (c) 2006-2007 SBE, Inc.  All Rights Reserved.
11  *
12  * Nicholas A. Bellinger <nab@linux-iscsi.org>
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27  *
28  ******************************************************************************/
29 
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/delay.h>
33 #include <linux/timer.h>
34 #include <linux/string.h>
35 #include <generated/utsrelease.h>
36 #include <linux/utsname.h>
37 #include <linux/proc_fs.h>
38 #include <linux/seq_file.h>
39 #include <linux/blkdev.h>
40 #include <linux/configfs.h>
41 #include <scsi/scsi.h>
42 #include <scsi/scsi_device.h>
43 #include <scsi/scsi_host.h>
44 
45 #include <target/target_core_base.h>
46 #include <target/target_core_backend.h>
47 #include <target/target_core_fabric.h>
48 #include <target/target_core_configfs.h>
49 #include <target/configfs_macros.h>
50 
51 #include "target_core_internal.h"
52 
53 #ifndef INITIAL_JIFFIES
54 #define INITIAL_JIFFIES ((unsigned long)(unsigned int) (-300*HZ))
55 #endif
56 
57 #define NONE		"None"
58 #define ISPRINT(a)   ((a >= ' ') && (a <= '~'))
59 
60 #define SCSI_LU_INDEX			1
61 #define LU_COUNT			1
62 
63 /*
64  * SCSI Device Table
65  */
66 
67 CONFIGFS_EATTR_STRUCT(target_stat_scsi_dev, se_dev_stat_grps);
68 #define DEV_STAT_SCSI_DEV_ATTR(_name, _mode)				\
69 static struct target_stat_scsi_dev_attribute				\
70 			target_stat_scsi_dev_##_name =			\
71 	__CONFIGFS_EATTR(_name, _mode,					\
72 	target_stat_scsi_dev_show_attr_##_name,				\
73 	target_stat_scsi_dev_store_attr_##_name);
74 
75 #define DEV_STAT_SCSI_DEV_ATTR_RO(_name)				\
76 static struct target_stat_scsi_dev_attribute				\
77 			target_stat_scsi_dev_##_name =			\
78 	__CONFIGFS_EATTR_RO(_name,					\
79 	target_stat_scsi_dev_show_attr_##_name);
80 
target_stat_scsi_dev_show_attr_inst(struct se_dev_stat_grps * sgrps,char * page)81 static ssize_t target_stat_scsi_dev_show_attr_inst(
82 	struct se_dev_stat_grps *sgrps, char *page)
83 {
84 	struct se_subsystem_dev *se_subdev = container_of(sgrps,
85 			struct se_subsystem_dev, dev_stat_grps);
86 	struct se_hba *hba = se_subdev->se_dev_hba;
87 	struct se_device *dev = se_subdev->se_dev_ptr;
88 
89 	if (!dev)
90 		return -ENODEV;
91 
92 	return snprintf(page, PAGE_SIZE, "%u\n", hba->hba_index);
93 }
94 DEV_STAT_SCSI_DEV_ATTR_RO(inst);
95 
target_stat_scsi_dev_show_attr_indx(struct se_dev_stat_grps * sgrps,char * page)96 static ssize_t target_stat_scsi_dev_show_attr_indx(
97 	struct se_dev_stat_grps *sgrps, char *page)
98 {
99 	struct se_subsystem_dev *se_subdev = container_of(sgrps,
100 			struct se_subsystem_dev, dev_stat_grps);
101 	struct se_device *dev = se_subdev->se_dev_ptr;
102 
103 	if (!dev)
104 		return -ENODEV;
105 
106 	return snprintf(page, PAGE_SIZE, "%u\n", dev->dev_index);
107 }
108 DEV_STAT_SCSI_DEV_ATTR_RO(indx);
109 
target_stat_scsi_dev_show_attr_role(struct se_dev_stat_grps * sgrps,char * page)110 static ssize_t target_stat_scsi_dev_show_attr_role(
111 	struct se_dev_stat_grps *sgrps, char *page)
112 {
113 	struct se_subsystem_dev *se_subdev = container_of(sgrps,
114 			struct se_subsystem_dev, dev_stat_grps);
115 	struct se_device *dev = se_subdev->se_dev_ptr;
116 
117 	if (!dev)
118 		return -ENODEV;
119 
120 	return snprintf(page, PAGE_SIZE, "Target\n");
121 }
122 DEV_STAT_SCSI_DEV_ATTR_RO(role);
123 
target_stat_scsi_dev_show_attr_ports(struct se_dev_stat_grps * sgrps,char * page)124 static ssize_t target_stat_scsi_dev_show_attr_ports(
125 	struct se_dev_stat_grps *sgrps, char *page)
126 {
127 	struct se_subsystem_dev *se_subdev = container_of(sgrps,
128 			struct se_subsystem_dev, dev_stat_grps);
129 	struct se_device *dev = se_subdev->se_dev_ptr;
130 
131 	if (!dev)
132 		return -ENODEV;
133 
134 	return snprintf(page, PAGE_SIZE, "%u\n", dev->dev_port_count);
135 }
136 DEV_STAT_SCSI_DEV_ATTR_RO(ports);
137 
138 CONFIGFS_EATTR_OPS(target_stat_scsi_dev, se_dev_stat_grps, scsi_dev_group);
139 
140 static struct configfs_attribute *target_stat_scsi_dev_attrs[] = {
141 	&target_stat_scsi_dev_inst.attr,
142 	&target_stat_scsi_dev_indx.attr,
143 	&target_stat_scsi_dev_role.attr,
144 	&target_stat_scsi_dev_ports.attr,
145 	NULL,
146 };
147 
148 static struct configfs_item_operations target_stat_scsi_dev_attrib_ops = {
149 	.show_attribute		= target_stat_scsi_dev_attr_show,
150 	.store_attribute	= target_stat_scsi_dev_attr_store,
151 };
152 
153 static struct config_item_type target_stat_scsi_dev_cit = {
154 	.ct_item_ops		= &target_stat_scsi_dev_attrib_ops,
155 	.ct_attrs		= target_stat_scsi_dev_attrs,
156 	.ct_owner		= THIS_MODULE,
157 };
158 
159 /*
160  * SCSI Target Device Table
161  */
162 
163 CONFIGFS_EATTR_STRUCT(target_stat_scsi_tgt_dev, se_dev_stat_grps);
164 #define DEV_STAT_SCSI_TGT_DEV_ATTR(_name, _mode)			\
165 static struct target_stat_scsi_tgt_dev_attribute			\
166 			target_stat_scsi_tgt_dev_##_name =		\
167 	__CONFIGFS_EATTR(_name, _mode,					\
168 	target_stat_scsi_tgt_dev_show_attr_##_name,			\
169 	target_stat_scsi_tgt_dev_store_attr_##_name);
170 
171 #define DEV_STAT_SCSI_TGT_DEV_ATTR_RO(_name)				\
172 static struct target_stat_scsi_tgt_dev_attribute			\
173 			target_stat_scsi_tgt_dev_##_name =		\
174 	__CONFIGFS_EATTR_RO(_name,					\
175 	target_stat_scsi_tgt_dev_show_attr_##_name);
176 
target_stat_scsi_tgt_dev_show_attr_inst(struct se_dev_stat_grps * sgrps,char * page)177 static ssize_t target_stat_scsi_tgt_dev_show_attr_inst(
178 	struct se_dev_stat_grps *sgrps, char *page)
179 {
180 	struct se_subsystem_dev *se_subdev = container_of(sgrps,
181 			struct se_subsystem_dev, dev_stat_grps);
182 	struct se_hba *hba = se_subdev->se_dev_hba;
183 	struct se_device *dev = se_subdev->se_dev_ptr;
184 
185 	if (!dev)
186 		return -ENODEV;
187 
188 	return snprintf(page, PAGE_SIZE, "%u\n", hba->hba_index);
189 }
190 DEV_STAT_SCSI_TGT_DEV_ATTR_RO(inst);
191 
target_stat_scsi_tgt_dev_show_attr_indx(struct se_dev_stat_grps * sgrps,char * page)192 static ssize_t target_stat_scsi_tgt_dev_show_attr_indx(
193 	struct se_dev_stat_grps *sgrps, char *page)
194 {
195 	struct se_subsystem_dev *se_subdev = container_of(sgrps,
196 			struct se_subsystem_dev, dev_stat_grps);
197 	struct se_device *dev = se_subdev->se_dev_ptr;
198 
199 	if (!dev)
200 		return -ENODEV;
201 
202 	return snprintf(page, PAGE_SIZE, "%u\n", dev->dev_index);
203 }
204 DEV_STAT_SCSI_TGT_DEV_ATTR_RO(indx);
205 
target_stat_scsi_tgt_dev_show_attr_num_lus(struct se_dev_stat_grps * sgrps,char * page)206 static ssize_t target_stat_scsi_tgt_dev_show_attr_num_lus(
207 	struct se_dev_stat_grps *sgrps, char *page)
208 {
209 	struct se_subsystem_dev *se_subdev = container_of(sgrps,
210 			struct se_subsystem_dev, dev_stat_grps);
211 	struct se_device *dev = se_subdev->se_dev_ptr;
212 
213 	if (!dev)
214 		return -ENODEV;
215 
216 	return snprintf(page, PAGE_SIZE, "%u\n", LU_COUNT);
217 }
218 DEV_STAT_SCSI_TGT_DEV_ATTR_RO(num_lus);
219 
target_stat_scsi_tgt_dev_show_attr_status(struct se_dev_stat_grps * sgrps,char * page)220 static ssize_t target_stat_scsi_tgt_dev_show_attr_status(
221 	struct se_dev_stat_grps *sgrps, char *page)
222 {
223 	struct se_subsystem_dev *se_subdev = container_of(sgrps,
224 			struct se_subsystem_dev, dev_stat_grps);
225 	struct se_device *dev = se_subdev->se_dev_ptr;
226 	char status[16];
227 
228 	if (!dev)
229 		return -ENODEV;
230 
231 	switch (dev->dev_status) {
232 	case TRANSPORT_DEVICE_ACTIVATED:
233 		strcpy(status, "activated");
234 		break;
235 	case TRANSPORT_DEVICE_DEACTIVATED:
236 		strcpy(status, "deactivated");
237 		break;
238 	case TRANSPORT_DEVICE_SHUTDOWN:
239 		strcpy(status, "shutdown");
240 		break;
241 	case TRANSPORT_DEVICE_OFFLINE_ACTIVATED:
242 	case TRANSPORT_DEVICE_OFFLINE_DEACTIVATED:
243 		strcpy(status, "offline");
244 		break;
245 	default:
246 		sprintf(status, "unknown(%d)", dev->dev_status);
247 		break;
248 	}
249 
250 	return snprintf(page, PAGE_SIZE, "%s\n", status);
251 }
252 DEV_STAT_SCSI_TGT_DEV_ATTR_RO(status);
253 
target_stat_scsi_tgt_dev_show_attr_non_access_lus(struct se_dev_stat_grps * sgrps,char * page)254 static ssize_t target_stat_scsi_tgt_dev_show_attr_non_access_lus(
255 	struct se_dev_stat_grps *sgrps, char *page)
256 {
257 	struct se_subsystem_dev *se_subdev = container_of(sgrps,
258 			struct se_subsystem_dev, dev_stat_grps);
259 	struct se_device *dev = se_subdev->se_dev_ptr;
260 	int non_accessible_lus;
261 
262 	if (!dev)
263 		return -ENODEV;
264 
265 	switch (dev->dev_status) {
266 	case TRANSPORT_DEVICE_ACTIVATED:
267 		non_accessible_lus = 0;
268 		break;
269 	case TRANSPORT_DEVICE_DEACTIVATED:
270 	case TRANSPORT_DEVICE_SHUTDOWN:
271 	case TRANSPORT_DEVICE_OFFLINE_ACTIVATED:
272 	case TRANSPORT_DEVICE_OFFLINE_DEACTIVATED:
273 	default:
274 		non_accessible_lus = 1;
275 		break;
276 	}
277 
278 	return snprintf(page, PAGE_SIZE, "%u\n", non_accessible_lus);
279 }
280 DEV_STAT_SCSI_TGT_DEV_ATTR_RO(non_access_lus);
281 
target_stat_scsi_tgt_dev_show_attr_resets(struct se_dev_stat_grps * sgrps,char * page)282 static ssize_t target_stat_scsi_tgt_dev_show_attr_resets(
283 	struct se_dev_stat_grps *sgrps, char *page)
284 {
285 	struct se_subsystem_dev *se_subdev = container_of(sgrps,
286 			struct se_subsystem_dev, dev_stat_grps);
287 	struct se_device *dev = se_subdev->se_dev_ptr;
288 
289 	if (!dev)
290 		return -ENODEV;
291 
292 	return snprintf(page, PAGE_SIZE, "%u\n", dev->num_resets);
293 }
294 DEV_STAT_SCSI_TGT_DEV_ATTR_RO(resets);
295 
296 
297 CONFIGFS_EATTR_OPS(target_stat_scsi_tgt_dev, se_dev_stat_grps, scsi_tgt_dev_group);
298 
299 static struct configfs_attribute *target_stat_scsi_tgt_dev_attrs[] = {
300 	&target_stat_scsi_tgt_dev_inst.attr,
301 	&target_stat_scsi_tgt_dev_indx.attr,
302 	&target_stat_scsi_tgt_dev_num_lus.attr,
303 	&target_stat_scsi_tgt_dev_status.attr,
304 	&target_stat_scsi_tgt_dev_non_access_lus.attr,
305 	&target_stat_scsi_tgt_dev_resets.attr,
306 	NULL,
307 };
308 
309 static struct configfs_item_operations target_stat_scsi_tgt_dev_attrib_ops = {
310 	.show_attribute		= target_stat_scsi_tgt_dev_attr_show,
311 	.store_attribute	= target_stat_scsi_tgt_dev_attr_store,
312 };
313 
314 static struct config_item_type target_stat_scsi_tgt_dev_cit = {
315 	.ct_item_ops		= &target_stat_scsi_tgt_dev_attrib_ops,
316 	.ct_attrs		= target_stat_scsi_tgt_dev_attrs,
317 	.ct_owner		= THIS_MODULE,
318 };
319 
320 /*
321  * SCSI Logical Unit Table
322  */
323 
324 CONFIGFS_EATTR_STRUCT(target_stat_scsi_lu, se_dev_stat_grps);
325 #define DEV_STAT_SCSI_LU_ATTR(_name, _mode)				\
326 static struct target_stat_scsi_lu_attribute target_stat_scsi_lu_##_name = \
327 	__CONFIGFS_EATTR(_name, _mode,					\
328 	target_stat_scsi_lu_show_attr_##_name,				\
329 	target_stat_scsi_lu_store_attr_##_name);
330 
331 #define DEV_STAT_SCSI_LU_ATTR_RO(_name)					\
332 static struct target_stat_scsi_lu_attribute target_stat_scsi_lu_##_name = \
333 	__CONFIGFS_EATTR_RO(_name,					\
334 	target_stat_scsi_lu_show_attr_##_name);
335 
target_stat_scsi_lu_show_attr_inst(struct se_dev_stat_grps * sgrps,char * page)336 static ssize_t target_stat_scsi_lu_show_attr_inst(
337 	struct se_dev_stat_grps *sgrps, char *page)
338 {
339 	struct se_subsystem_dev *se_subdev = container_of(sgrps,
340 			struct se_subsystem_dev, dev_stat_grps);
341 	struct se_hba *hba = se_subdev->se_dev_hba;
342 	struct se_device *dev = se_subdev->se_dev_ptr;
343 
344 	if (!dev)
345 		return -ENODEV;
346 
347 	return snprintf(page, PAGE_SIZE, "%u\n", hba->hba_index);
348 }
349 DEV_STAT_SCSI_LU_ATTR_RO(inst);
350 
target_stat_scsi_lu_show_attr_dev(struct se_dev_stat_grps * sgrps,char * page)351 static ssize_t target_stat_scsi_lu_show_attr_dev(
352 	struct se_dev_stat_grps *sgrps, char *page)
353 {
354 	struct se_subsystem_dev *se_subdev = container_of(sgrps,
355 			struct se_subsystem_dev, dev_stat_grps);
356 	struct se_device *dev = se_subdev->se_dev_ptr;
357 
358 	if (!dev)
359 		return -ENODEV;
360 
361 	return snprintf(page, PAGE_SIZE, "%u\n", dev->dev_index);
362 }
363 DEV_STAT_SCSI_LU_ATTR_RO(dev);
364 
target_stat_scsi_lu_show_attr_indx(struct se_dev_stat_grps * sgrps,char * page)365 static ssize_t target_stat_scsi_lu_show_attr_indx(
366 	struct se_dev_stat_grps *sgrps, char *page)
367 {
368 	struct se_subsystem_dev *se_subdev = container_of(sgrps,
369 			struct se_subsystem_dev, dev_stat_grps);
370 	struct se_device *dev = se_subdev->se_dev_ptr;
371 
372 	if (!dev)
373 		return -ENODEV;
374 
375 	return snprintf(page, PAGE_SIZE, "%u\n", SCSI_LU_INDEX);
376 }
377 DEV_STAT_SCSI_LU_ATTR_RO(indx);
378 
target_stat_scsi_lu_show_attr_lun(struct se_dev_stat_grps * sgrps,char * page)379 static ssize_t target_stat_scsi_lu_show_attr_lun(
380 	struct se_dev_stat_grps *sgrps, char *page)
381 {
382 	struct se_subsystem_dev *se_subdev = container_of(sgrps,
383 			struct se_subsystem_dev, dev_stat_grps);
384 	struct se_device *dev = se_subdev->se_dev_ptr;
385 
386 	if (!dev)
387 		return -ENODEV;
388 	/* FIXME: scsiLuDefaultLun */
389 	return snprintf(page, PAGE_SIZE, "%llu\n", (unsigned long long)0);
390 }
391 DEV_STAT_SCSI_LU_ATTR_RO(lun);
392 
target_stat_scsi_lu_show_attr_lu_name(struct se_dev_stat_grps * sgrps,char * page)393 static ssize_t target_stat_scsi_lu_show_attr_lu_name(
394 	struct se_dev_stat_grps *sgrps, char *page)
395 {
396 	struct se_subsystem_dev *se_subdev = container_of(sgrps,
397 			struct se_subsystem_dev, dev_stat_grps);
398 	struct se_device *dev = se_subdev->se_dev_ptr;
399 
400 	if (!dev)
401 		return -ENODEV;
402 	/* scsiLuWwnName */
403 	return snprintf(page, PAGE_SIZE, "%s\n",
404 			(strlen(dev->se_sub_dev->t10_wwn.unit_serial)) ?
405 			dev->se_sub_dev->t10_wwn.unit_serial : "None");
406 }
407 DEV_STAT_SCSI_LU_ATTR_RO(lu_name);
408 
target_stat_scsi_lu_show_attr_vend(struct se_dev_stat_grps * sgrps,char * page)409 static ssize_t target_stat_scsi_lu_show_attr_vend(
410 	struct se_dev_stat_grps *sgrps, char *page)
411 {
412 	struct se_subsystem_dev *se_subdev = container_of(sgrps,
413 			struct se_subsystem_dev, dev_stat_grps);
414 	struct se_device *dev = se_subdev->se_dev_ptr;
415 	int i;
416 	char str[sizeof(dev->se_sub_dev->t10_wwn.vendor)+1];
417 
418 	if (!dev)
419 		return -ENODEV;
420 
421 	/* scsiLuVendorId */
422 	for (i = 0; i < sizeof(dev->se_sub_dev->t10_wwn.vendor); i++)
423 		str[i] = ISPRINT(dev->se_sub_dev->t10_wwn.vendor[i]) ?
424 			dev->se_sub_dev->t10_wwn.vendor[i] : ' ';
425 	str[i] = '\0';
426 	return snprintf(page, PAGE_SIZE, "%s\n", str);
427 }
428 DEV_STAT_SCSI_LU_ATTR_RO(vend);
429 
target_stat_scsi_lu_show_attr_prod(struct se_dev_stat_grps * sgrps,char * page)430 static ssize_t target_stat_scsi_lu_show_attr_prod(
431 	struct se_dev_stat_grps *sgrps, char *page)
432 {
433 	struct se_subsystem_dev *se_subdev = container_of(sgrps,
434 			struct se_subsystem_dev, dev_stat_grps);
435 	struct se_device *dev = se_subdev->se_dev_ptr;
436 	int i;
437 	char str[sizeof(dev->se_sub_dev->t10_wwn.model)+1];
438 
439 	if (!dev)
440 		return -ENODEV;
441 
442 	/* scsiLuProductId */
443 	for (i = 0; i < sizeof(dev->se_sub_dev->t10_wwn.vendor); i++)
444 		str[i] = ISPRINT(dev->se_sub_dev->t10_wwn.model[i]) ?
445 			dev->se_sub_dev->t10_wwn.model[i] : ' ';
446 	str[i] = '\0';
447 	return snprintf(page, PAGE_SIZE, "%s\n", str);
448 }
449 DEV_STAT_SCSI_LU_ATTR_RO(prod);
450 
target_stat_scsi_lu_show_attr_rev(struct se_dev_stat_grps * sgrps,char * page)451 static ssize_t target_stat_scsi_lu_show_attr_rev(
452 	struct se_dev_stat_grps *sgrps, char *page)
453 {
454 	struct se_subsystem_dev *se_subdev = container_of(sgrps,
455 			struct se_subsystem_dev, dev_stat_grps);
456 	struct se_device *dev = se_subdev->se_dev_ptr;
457 	int i;
458 	char str[sizeof(dev->se_sub_dev->t10_wwn.revision)+1];
459 
460 	if (!dev)
461 		return -ENODEV;
462 
463 	/* scsiLuRevisionId */
464 	for (i = 0; i < sizeof(dev->se_sub_dev->t10_wwn.revision); i++)
465 		str[i] = ISPRINT(dev->se_sub_dev->t10_wwn.revision[i]) ?
466 			dev->se_sub_dev->t10_wwn.revision[i] : ' ';
467 	str[i] = '\0';
468 	return snprintf(page, PAGE_SIZE, "%s\n", str);
469 }
470 DEV_STAT_SCSI_LU_ATTR_RO(rev);
471 
target_stat_scsi_lu_show_attr_dev_type(struct se_dev_stat_grps * sgrps,char * page)472 static ssize_t target_stat_scsi_lu_show_attr_dev_type(
473 	struct se_dev_stat_grps *sgrps, char *page)
474 {
475 	struct se_subsystem_dev *se_subdev = container_of(sgrps,
476 			struct se_subsystem_dev, dev_stat_grps);
477 	struct se_device *dev = se_subdev->se_dev_ptr;
478 
479 	if (!dev)
480 		return -ENODEV;
481 
482 	/* scsiLuPeripheralType */
483 	return snprintf(page, PAGE_SIZE, "%u\n",
484 			dev->transport->get_device_type(dev));
485 }
486 DEV_STAT_SCSI_LU_ATTR_RO(dev_type);
487 
target_stat_scsi_lu_show_attr_status(struct se_dev_stat_grps * sgrps,char * page)488 static ssize_t target_stat_scsi_lu_show_attr_status(
489 	struct se_dev_stat_grps *sgrps, char *page)
490 {
491 	struct se_subsystem_dev *se_subdev = container_of(sgrps,
492 			struct se_subsystem_dev, dev_stat_grps);
493 	struct se_device *dev = se_subdev->se_dev_ptr;
494 
495 	if (!dev)
496 		return -ENODEV;
497 
498 	/* scsiLuStatus */
499 	return snprintf(page, PAGE_SIZE, "%s\n",
500 		(dev->dev_status == TRANSPORT_DEVICE_ACTIVATED) ?
501 		"available" : "notavailable");
502 }
503 DEV_STAT_SCSI_LU_ATTR_RO(status);
504 
target_stat_scsi_lu_show_attr_state_bit(struct se_dev_stat_grps * sgrps,char * page)505 static ssize_t target_stat_scsi_lu_show_attr_state_bit(
506 	struct se_dev_stat_grps *sgrps, char *page)
507 {
508 	struct se_subsystem_dev *se_subdev = container_of(sgrps,
509 			struct se_subsystem_dev, dev_stat_grps);
510 	struct se_device *dev = se_subdev->se_dev_ptr;
511 
512 	if (!dev)
513 		return -ENODEV;
514 
515 	/* scsiLuState */
516 	return snprintf(page, PAGE_SIZE, "exposed\n");
517 }
518 DEV_STAT_SCSI_LU_ATTR_RO(state_bit);
519 
target_stat_scsi_lu_show_attr_num_cmds(struct se_dev_stat_grps * sgrps,char * page)520 static ssize_t target_stat_scsi_lu_show_attr_num_cmds(
521 	struct se_dev_stat_grps *sgrps, char *page)
522 {
523 	struct se_subsystem_dev *se_subdev = container_of(sgrps,
524 			struct se_subsystem_dev, dev_stat_grps);
525 	struct se_device *dev = se_subdev->se_dev_ptr;
526 
527 	if (!dev)
528 		return -ENODEV;
529 
530 	/* scsiLuNumCommands */
531 	return snprintf(page, PAGE_SIZE, "%llu\n",
532 			(unsigned long long)dev->num_cmds);
533 }
534 DEV_STAT_SCSI_LU_ATTR_RO(num_cmds);
535 
target_stat_scsi_lu_show_attr_read_mbytes(struct se_dev_stat_grps * sgrps,char * page)536 static ssize_t target_stat_scsi_lu_show_attr_read_mbytes(
537 	struct se_dev_stat_grps *sgrps, char *page)
538 {
539 	struct se_subsystem_dev *se_subdev = container_of(sgrps,
540 			struct se_subsystem_dev, dev_stat_grps);
541 	struct se_device *dev = se_subdev->se_dev_ptr;
542 
543 	if (!dev)
544 		return -ENODEV;
545 
546 	/* scsiLuReadMegaBytes */
547 	return snprintf(page, PAGE_SIZE, "%u\n", (u32)(dev->read_bytes >> 20));
548 }
549 DEV_STAT_SCSI_LU_ATTR_RO(read_mbytes);
550 
target_stat_scsi_lu_show_attr_write_mbytes(struct se_dev_stat_grps * sgrps,char * page)551 static ssize_t target_stat_scsi_lu_show_attr_write_mbytes(
552 	struct se_dev_stat_grps *sgrps, char *page)
553 {
554 	struct se_subsystem_dev *se_subdev = container_of(sgrps,
555 			struct se_subsystem_dev, dev_stat_grps);
556 	struct se_device *dev = se_subdev->se_dev_ptr;
557 
558 	if (!dev)
559 		return -ENODEV;
560 
561 	/* scsiLuWrittenMegaBytes */
562 	return snprintf(page, PAGE_SIZE, "%u\n", (u32)(dev->write_bytes >> 20));
563 }
564 DEV_STAT_SCSI_LU_ATTR_RO(write_mbytes);
565 
target_stat_scsi_lu_show_attr_resets(struct se_dev_stat_grps * sgrps,char * page)566 static ssize_t target_stat_scsi_lu_show_attr_resets(
567 	struct se_dev_stat_grps *sgrps, char *page)
568 {
569 	struct se_subsystem_dev *se_subdev = container_of(sgrps,
570 			struct se_subsystem_dev, dev_stat_grps);
571 	struct se_device *dev = se_subdev->se_dev_ptr;
572 
573 	if (!dev)
574 		return -ENODEV;
575 
576 	/* scsiLuInResets */
577 	return snprintf(page, PAGE_SIZE, "%u\n", dev->num_resets);
578 }
579 DEV_STAT_SCSI_LU_ATTR_RO(resets);
580 
target_stat_scsi_lu_show_attr_full_stat(struct se_dev_stat_grps * sgrps,char * page)581 static ssize_t target_stat_scsi_lu_show_attr_full_stat(
582 	struct se_dev_stat_grps *sgrps, char *page)
583 {
584 	struct se_subsystem_dev *se_subdev = container_of(sgrps,
585 			struct se_subsystem_dev, dev_stat_grps);
586 	struct se_device *dev = se_subdev->se_dev_ptr;
587 
588 	if (!dev)
589 		return -ENODEV;
590 
591 	/* FIXME: scsiLuOutTaskSetFullStatus */
592 	return snprintf(page, PAGE_SIZE, "%u\n", 0);
593 }
594 DEV_STAT_SCSI_LU_ATTR_RO(full_stat);
595 
target_stat_scsi_lu_show_attr_hs_num_cmds(struct se_dev_stat_grps * sgrps,char * page)596 static ssize_t target_stat_scsi_lu_show_attr_hs_num_cmds(
597 	struct se_dev_stat_grps *sgrps, char *page)
598 {
599 	struct se_subsystem_dev *se_subdev = container_of(sgrps,
600 			struct se_subsystem_dev, dev_stat_grps);
601 	struct se_device *dev = se_subdev->se_dev_ptr;
602 
603 	if (!dev)
604 		return -ENODEV;
605 
606 	/* FIXME: scsiLuHSInCommands */
607 	return snprintf(page, PAGE_SIZE, "%u\n", 0);
608 }
609 DEV_STAT_SCSI_LU_ATTR_RO(hs_num_cmds);
610 
target_stat_scsi_lu_show_attr_creation_time(struct se_dev_stat_grps * sgrps,char * page)611 static ssize_t target_stat_scsi_lu_show_attr_creation_time(
612 	struct se_dev_stat_grps *sgrps, char *page)
613 {
614 	struct se_subsystem_dev *se_subdev = container_of(sgrps,
615 			struct se_subsystem_dev, dev_stat_grps);
616 	struct se_device *dev = se_subdev->se_dev_ptr;
617 
618 	if (!dev)
619 		return -ENODEV;
620 
621 	/* scsiLuCreationTime */
622 	return snprintf(page, PAGE_SIZE, "%u\n", (u32)(((u32)dev->creation_time -
623 				INITIAL_JIFFIES) * 100 / HZ));
624 }
625 DEV_STAT_SCSI_LU_ATTR_RO(creation_time);
626 
627 CONFIGFS_EATTR_OPS(target_stat_scsi_lu, se_dev_stat_grps, scsi_lu_group);
628 
629 static struct configfs_attribute *target_stat_scsi_lu_attrs[] = {
630 	&target_stat_scsi_lu_inst.attr,
631 	&target_stat_scsi_lu_dev.attr,
632 	&target_stat_scsi_lu_indx.attr,
633 	&target_stat_scsi_lu_lun.attr,
634 	&target_stat_scsi_lu_lu_name.attr,
635 	&target_stat_scsi_lu_vend.attr,
636 	&target_stat_scsi_lu_prod.attr,
637 	&target_stat_scsi_lu_rev.attr,
638 	&target_stat_scsi_lu_dev_type.attr,
639 	&target_stat_scsi_lu_status.attr,
640 	&target_stat_scsi_lu_state_bit.attr,
641 	&target_stat_scsi_lu_num_cmds.attr,
642 	&target_stat_scsi_lu_read_mbytes.attr,
643 	&target_stat_scsi_lu_write_mbytes.attr,
644 	&target_stat_scsi_lu_resets.attr,
645 	&target_stat_scsi_lu_full_stat.attr,
646 	&target_stat_scsi_lu_hs_num_cmds.attr,
647 	&target_stat_scsi_lu_creation_time.attr,
648 	NULL,
649 };
650 
651 static struct configfs_item_operations target_stat_scsi_lu_attrib_ops = {
652 	.show_attribute		= target_stat_scsi_lu_attr_show,
653 	.store_attribute	= target_stat_scsi_lu_attr_store,
654 };
655 
656 static struct config_item_type target_stat_scsi_lu_cit = {
657 	.ct_item_ops		= &target_stat_scsi_lu_attrib_ops,
658 	.ct_attrs		= target_stat_scsi_lu_attrs,
659 	.ct_owner		= THIS_MODULE,
660 };
661 
662 /*
663  * Called from target_core_configfs.c:target_core_make_subdev() to setup
664  * the target statistics groups + configfs CITs located in target_core_stat.c
665  */
target_stat_setup_dev_default_groups(struct se_subsystem_dev * se_subdev)666 void target_stat_setup_dev_default_groups(struct se_subsystem_dev *se_subdev)
667 {
668 	struct config_group *dev_stat_grp = &se_subdev->dev_stat_grps.stat_group;
669 
670 	config_group_init_type_name(&se_subdev->dev_stat_grps.scsi_dev_group,
671 			"scsi_dev", &target_stat_scsi_dev_cit);
672 	config_group_init_type_name(&se_subdev->dev_stat_grps.scsi_tgt_dev_group,
673 			"scsi_tgt_dev", &target_stat_scsi_tgt_dev_cit);
674 	config_group_init_type_name(&se_subdev->dev_stat_grps.scsi_lu_group,
675 			"scsi_lu", &target_stat_scsi_lu_cit);
676 
677 	dev_stat_grp->default_groups[0] = &se_subdev->dev_stat_grps.scsi_dev_group;
678 	dev_stat_grp->default_groups[1] = &se_subdev->dev_stat_grps.scsi_tgt_dev_group;
679 	dev_stat_grp->default_groups[2] = &se_subdev->dev_stat_grps.scsi_lu_group;
680 	dev_stat_grp->default_groups[3] = NULL;
681 }
682 
683 /*
684  * SCSI Port Table
685  */
686 
687 CONFIGFS_EATTR_STRUCT(target_stat_scsi_port, se_port_stat_grps);
688 #define DEV_STAT_SCSI_PORT_ATTR(_name, _mode)				\
689 static struct target_stat_scsi_port_attribute				\
690 			target_stat_scsi_port_##_name =			\
691 	__CONFIGFS_EATTR(_name, _mode,					\
692 	target_stat_scsi_port_show_attr_##_name,			\
693 	target_stat_scsi_port_store_attr_##_name);
694 
695 #define DEV_STAT_SCSI_PORT_ATTR_RO(_name)				\
696 static struct target_stat_scsi_port_attribute				\
697 			target_stat_scsi_port_##_name =			\
698 	__CONFIGFS_EATTR_RO(_name,					\
699 	target_stat_scsi_port_show_attr_##_name);
700 
target_stat_scsi_port_show_attr_inst(struct se_port_stat_grps * pgrps,char * page)701 static ssize_t target_stat_scsi_port_show_attr_inst(
702 	struct se_port_stat_grps *pgrps, char *page)
703 {
704 	struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps);
705 	struct se_port *sep;
706 	struct se_device *dev = lun->lun_se_dev;
707 	struct se_hba *hba;
708 	ssize_t ret;
709 
710 	spin_lock(&lun->lun_sep_lock);
711 	sep = lun->lun_sep;
712 	if (!sep) {
713 		spin_unlock(&lun->lun_sep_lock);
714 		return -ENODEV;
715 	}
716 	hba = dev->se_hba;
717 	ret = snprintf(page, PAGE_SIZE, "%u\n", hba->hba_index);
718 	spin_unlock(&lun->lun_sep_lock);
719 	return ret;
720 }
721 DEV_STAT_SCSI_PORT_ATTR_RO(inst);
722 
target_stat_scsi_port_show_attr_dev(struct se_port_stat_grps * pgrps,char * page)723 static ssize_t target_stat_scsi_port_show_attr_dev(
724 	struct se_port_stat_grps *pgrps, char *page)
725 {
726 	struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps);
727 	struct se_port *sep;
728 	struct se_device *dev = lun->lun_se_dev;
729 	ssize_t ret;
730 
731 	spin_lock(&lun->lun_sep_lock);
732 	sep = lun->lun_sep;
733 	if (!sep) {
734 		spin_unlock(&lun->lun_sep_lock);
735 		return -ENODEV;
736 	}
737 	ret = snprintf(page, PAGE_SIZE, "%u\n", dev->dev_index);
738 	spin_unlock(&lun->lun_sep_lock);
739 	return ret;
740 }
741 DEV_STAT_SCSI_PORT_ATTR_RO(dev);
742 
target_stat_scsi_port_show_attr_indx(struct se_port_stat_grps * pgrps,char * page)743 static ssize_t target_stat_scsi_port_show_attr_indx(
744 	struct se_port_stat_grps *pgrps, char *page)
745 {
746 	struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps);
747 	struct se_port *sep;
748 	ssize_t ret;
749 
750 	spin_lock(&lun->lun_sep_lock);
751 	sep = lun->lun_sep;
752 	if (!sep) {
753 		spin_unlock(&lun->lun_sep_lock);
754 		return -ENODEV;
755 	}
756 	ret = snprintf(page, PAGE_SIZE, "%u\n", sep->sep_index);
757 	spin_unlock(&lun->lun_sep_lock);
758 	return ret;
759 }
760 DEV_STAT_SCSI_PORT_ATTR_RO(indx);
761 
target_stat_scsi_port_show_attr_role(struct se_port_stat_grps * pgrps,char * page)762 static ssize_t target_stat_scsi_port_show_attr_role(
763 	struct se_port_stat_grps *pgrps, char *page)
764 {
765 	struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps);
766 	struct se_device *dev = lun->lun_se_dev;
767 	struct se_port *sep;
768 	ssize_t ret;
769 
770 	if (!dev)
771 		return -ENODEV;
772 
773 	spin_lock(&lun->lun_sep_lock);
774 	sep = lun->lun_sep;
775 	if (!sep) {
776 		spin_unlock(&lun->lun_sep_lock);
777 		return -ENODEV;
778 	}
779 	ret = snprintf(page, PAGE_SIZE, "%s%u\n", "Device", dev->dev_index);
780 	spin_unlock(&lun->lun_sep_lock);
781 	return ret;
782 }
783 DEV_STAT_SCSI_PORT_ATTR_RO(role);
784 
target_stat_scsi_port_show_attr_busy_count(struct se_port_stat_grps * pgrps,char * page)785 static ssize_t target_stat_scsi_port_show_attr_busy_count(
786 	struct se_port_stat_grps *pgrps, char *page)
787 {
788 	struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps);
789 	struct se_port *sep;
790 	ssize_t ret;
791 
792 	spin_lock(&lun->lun_sep_lock);
793 	sep = lun->lun_sep;
794 	if (!sep) {
795 		spin_unlock(&lun->lun_sep_lock);
796 		return -ENODEV;
797 	}
798 	/* FIXME: scsiPortBusyStatuses  */
799 	ret = snprintf(page, PAGE_SIZE, "%u\n", 0);
800 	spin_unlock(&lun->lun_sep_lock);
801 	return ret;
802 }
803 DEV_STAT_SCSI_PORT_ATTR_RO(busy_count);
804 
805 CONFIGFS_EATTR_OPS(target_stat_scsi_port, se_port_stat_grps, scsi_port_group);
806 
807 static struct configfs_attribute *target_stat_scsi_port_attrs[] = {
808 	&target_stat_scsi_port_inst.attr,
809 	&target_stat_scsi_port_dev.attr,
810 	&target_stat_scsi_port_indx.attr,
811 	&target_stat_scsi_port_role.attr,
812 	&target_stat_scsi_port_busy_count.attr,
813 	NULL,
814 };
815 
816 static struct configfs_item_operations target_stat_scsi_port_attrib_ops = {
817 	.show_attribute		= target_stat_scsi_port_attr_show,
818 	.store_attribute	= target_stat_scsi_port_attr_store,
819 };
820 
821 static struct config_item_type target_stat_scsi_port_cit = {
822 	.ct_item_ops		= &target_stat_scsi_port_attrib_ops,
823 	.ct_attrs		= target_stat_scsi_port_attrs,
824 	.ct_owner		= THIS_MODULE,
825 };
826 
827 /*
828  * SCSI Target Port Table
829  */
830 CONFIGFS_EATTR_STRUCT(target_stat_scsi_tgt_port, se_port_stat_grps);
831 #define DEV_STAT_SCSI_TGT_PORT_ATTR(_name, _mode)			\
832 static struct target_stat_scsi_tgt_port_attribute			\
833 			target_stat_scsi_tgt_port_##_name =		\
834 	__CONFIGFS_EATTR(_name, _mode,					\
835 	target_stat_scsi_tgt_port_show_attr_##_name,			\
836 	target_stat_scsi_tgt_port_store_attr_##_name);
837 
838 #define DEV_STAT_SCSI_TGT_PORT_ATTR_RO(_name)				\
839 static struct target_stat_scsi_tgt_port_attribute			\
840 			target_stat_scsi_tgt_port_##_name =		\
841 	__CONFIGFS_EATTR_RO(_name,					\
842 	target_stat_scsi_tgt_port_show_attr_##_name);
843 
target_stat_scsi_tgt_port_show_attr_inst(struct se_port_stat_grps * pgrps,char * page)844 static ssize_t target_stat_scsi_tgt_port_show_attr_inst(
845 	struct se_port_stat_grps *pgrps, char *page)
846 {
847 	struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps);
848 	struct se_device *dev = lun->lun_se_dev;
849 	struct se_port *sep;
850 	struct se_hba *hba;
851 	ssize_t ret;
852 
853 	spin_lock(&lun->lun_sep_lock);
854 	sep = lun->lun_sep;
855 	if (!sep) {
856 		spin_unlock(&lun->lun_sep_lock);
857 		return -ENODEV;
858 	}
859 	hba = dev->se_hba;
860 	ret = snprintf(page, PAGE_SIZE, "%u\n", hba->hba_index);
861 	spin_unlock(&lun->lun_sep_lock);
862 	return ret;
863 }
864 DEV_STAT_SCSI_TGT_PORT_ATTR_RO(inst);
865 
target_stat_scsi_tgt_port_show_attr_dev(struct se_port_stat_grps * pgrps,char * page)866 static ssize_t target_stat_scsi_tgt_port_show_attr_dev(
867 	struct se_port_stat_grps *pgrps, char *page)
868 {
869 	struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps);
870 	struct se_device *dev = lun->lun_se_dev;
871 	struct se_port *sep;
872 	ssize_t ret;
873 
874 	spin_lock(&lun->lun_sep_lock);
875 	sep = lun->lun_sep;
876 	if (!sep) {
877 		spin_unlock(&lun->lun_sep_lock);
878 		return -ENODEV;
879 	}
880 	ret = snprintf(page, PAGE_SIZE, "%u\n", dev->dev_index);
881 	spin_unlock(&lun->lun_sep_lock);
882 	return ret;
883 }
884 DEV_STAT_SCSI_TGT_PORT_ATTR_RO(dev);
885 
target_stat_scsi_tgt_port_show_attr_indx(struct se_port_stat_grps * pgrps,char * page)886 static ssize_t target_stat_scsi_tgt_port_show_attr_indx(
887 	struct se_port_stat_grps *pgrps, char *page)
888 {
889 	struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps);
890 	struct se_port *sep;
891 	ssize_t ret;
892 
893 	spin_lock(&lun->lun_sep_lock);
894 	sep = lun->lun_sep;
895 	if (!sep) {
896 		spin_unlock(&lun->lun_sep_lock);
897 		return -ENODEV;
898 	}
899 	ret = snprintf(page, PAGE_SIZE, "%u\n", sep->sep_index);
900 	spin_unlock(&lun->lun_sep_lock);
901 	return ret;
902 }
903 DEV_STAT_SCSI_TGT_PORT_ATTR_RO(indx);
904 
target_stat_scsi_tgt_port_show_attr_name(struct se_port_stat_grps * pgrps,char * page)905 static ssize_t target_stat_scsi_tgt_port_show_attr_name(
906 	struct se_port_stat_grps *pgrps, char *page)
907 {
908 	struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps);
909 	struct se_port *sep;
910 	struct se_portal_group *tpg;
911 	ssize_t ret;
912 
913 	spin_lock(&lun->lun_sep_lock);
914 	sep = lun->lun_sep;
915 	if (!sep) {
916 		spin_unlock(&lun->lun_sep_lock);
917 		return -ENODEV;
918 	}
919 	tpg = sep->sep_tpg;
920 
921 	ret = snprintf(page, PAGE_SIZE, "%sPort#%u\n",
922 		tpg->se_tpg_tfo->get_fabric_name(), sep->sep_index);
923 	spin_unlock(&lun->lun_sep_lock);
924 	return ret;
925 }
926 DEV_STAT_SCSI_TGT_PORT_ATTR_RO(name);
927 
target_stat_scsi_tgt_port_show_attr_port_index(struct se_port_stat_grps * pgrps,char * page)928 static ssize_t target_stat_scsi_tgt_port_show_attr_port_index(
929 	struct se_port_stat_grps *pgrps, char *page)
930 {
931 	struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps);
932 	struct se_port *sep;
933 	struct se_portal_group *tpg;
934 	ssize_t ret;
935 
936 	spin_lock(&lun->lun_sep_lock);
937 	sep = lun->lun_sep;
938 	if (!sep) {
939 		spin_unlock(&lun->lun_sep_lock);
940 		return -ENODEV;
941 	}
942 	tpg = sep->sep_tpg;
943 
944 	ret = snprintf(page, PAGE_SIZE, "%s%s%d\n",
945 		tpg->se_tpg_tfo->tpg_get_wwn(tpg), "+t+",
946 		tpg->se_tpg_tfo->tpg_get_tag(tpg));
947 	spin_unlock(&lun->lun_sep_lock);
948 	return ret;
949 }
950 DEV_STAT_SCSI_TGT_PORT_ATTR_RO(port_index);
951 
target_stat_scsi_tgt_port_show_attr_in_cmds(struct se_port_stat_grps * pgrps,char * page)952 static ssize_t target_stat_scsi_tgt_port_show_attr_in_cmds(
953 	struct se_port_stat_grps *pgrps, char *page)
954 {
955 	struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps);
956 	struct se_port *sep;
957 	ssize_t ret;
958 
959 	spin_lock(&lun->lun_sep_lock);
960 	sep = lun->lun_sep;
961 	if (!sep) {
962 		spin_unlock(&lun->lun_sep_lock);
963 		return -ENODEV;
964 	}
965 
966 	ret = snprintf(page, PAGE_SIZE, "%llu\n", sep->sep_stats.cmd_pdus);
967 	spin_unlock(&lun->lun_sep_lock);
968 	return ret;
969 }
970 DEV_STAT_SCSI_TGT_PORT_ATTR_RO(in_cmds);
971 
target_stat_scsi_tgt_port_show_attr_write_mbytes(struct se_port_stat_grps * pgrps,char * page)972 static ssize_t target_stat_scsi_tgt_port_show_attr_write_mbytes(
973 	struct se_port_stat_grps *pgrps, char *page)
974 {
975 	struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps);
976 	struct se_port *sep;
977 	ssize_t ret;
978 
979 	spin_lock(&lun->lun_sep_lock);
980 	sep = lun->lun_sep;
981 	if (!sep) {
982 		spin_unlock(&lun->lun_sep_lock);
983 		return -ENODEV;
984 	}
985 
986 	ret = snprintf(page, PAGE_SIZE, "%u\n",
987 			(u32)(sep->sep_stats.rx_data_octets >> 20));
988 	spin_unlock(&lun->lun_sep_lock);
989 	return ret;
990 }
991 DEV_STAT_SCSI_TGT_PORT_ATTR_RO(write_mbytes);
992 
target_stat_scsi_tgt_port_show_attr_read_mbytes(struct se_port_stat_grps * pgrps,char * page)993 static ssize_t target_stat_scsi_tgt_port_show_attr_read_mbytes(
994 	struct se_port_stat_grps *pgrps, char *page)
995 {
996 	struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps);
997 	struct se_port *sep;
998 	ssize_t ret;
999 
1000 	spin_lock(&lun->lun_sep_lock);
1001 	sep = lun->lun_sep;
1002 	if (!sep) {
1003 		spin_unlock(&lun->lun_sep_lock);
1004 		return -ENODEV;
1005 	}
1006 
1007 	ret = snprintf(page, PAGE_SIZE, "%u\n",
1008 			(u32)(sep->sep_stats.tx_data_octets >> 20));
1009 	spin_unlock(&lun->lun_sep_lock);
1010 	return ret;
1011 }
1012 DEV_STAT_SCSI_TGT_PORT_ATTR_RO(read_mbytes);
1013 
target_stat_scsi_tgt_port_show_attr_hs_in_cmds(struct se_port_stat_grps * pgrps,char * page)1014 static ssize_t target_stat_scsi_tgt_port_show_attr_hs_in_cmds(
1015 	struct se_port_stat_grps *pgrps, char *page)
1016 {
1017 	struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps);
1018 	struct se_port *sep;
1019 	ssize_t ret;
1020 
1021 	spin_lock(&lun->lun_sep_lock);
1022 	sep = lun->lun_sep;
1023 	if (!sep) {
1024 		spin_unlock(&lun->lun_sep_lock);
1025 		return -ENODEV;
1026 	}
1027 
1028 	/* FIXME: scsiTgtPortHsInCommands */
1029 	ret = snprintf(page, PAGE_SIZE, "%u\n", 0);
1030 	spin_unlock(&lun->lun_sep_lock);
1031 	return ret;
1032 }
1033 DEV_STAT_SCSI_TGT_PORT_ATTR_RO(hs_in_cmds);
1034 
1035 CONFIGFS_EATTR_OPS(target_stat_scsi_tgt_port, se_port_stat_grps,
1036 		scsi_tgt_port_group);
1037 
1038 static struct configfs_attribute *target_stat_scsi_tgt_port_attrs[] = {
1039 	&target_stat_scsi_tgt_port_inst.attr,
1040 	&target_stat_scsi_tgt_port_dev.attr,
1041 	&target_stat_scsi_tgt_port_indx.attr,
1042 	&target_stat_scsi_tgt_port_name.attr,
1043 	&target_stat_scsi_tgt_port_port_index.attr,
1044 	&target_stat_scsi_tgt_port_in_cmds.attr,
1045 	&target_stat_scsi_tgt_port_write_mbytes.attr,
1046 	&target_stat_scsi_tgt_port_read_mbytes.attr,
1047 	&target_stat_scsi_tgt_port_hs_in_cmds.attr,
1048 	NULL,
1049 };
1050 
1051 static struct configfs_item_operations target_stat_scsi_tgt_port_attrib_ops = {
1052 	.show_attribute		= target_stat_scsi_tgt_port_attr_show,
1053 	.store_attribute	= target_stat_scsi_tgt_port_attr_store,
1054 };
1055 
1056 static struct config_item_type target_stat_scsi_tgt_port_cit = {
1057 	.ct_item_ops		= &target_stat_scsi_tgt_port_attrib_ops,
1058 	.ct_attrs		= target_stat_scsi_tgt_port_attrs,
1059 	.ct_owner		= THIS_MODULE,
1060 };
1061 
1062 /*
1063  * SCSI Transport Table
1064 o */
1065 
1066 CONFIGFS_EATTR_STRUCT(target_stat_scsi_transport, se_port_stat_grps);
1067 #define DEV_STAT_SCSI_TRANSPORT_ATTR(_name, _mode)			\
1068 static struct target_stat_scsi_transport_attribute			\
1069 			target_stat_scsi_transport_##_name =		\
1070 	__CONFIGFS_EATTR(_name, _mode,					\
1071 	target_stat_scsi_transport_show_attr_##_name,			\
1072 	target_stat_scsi_transport_store_attr_##_name);
1073 
1074 #define DEV_STAT_SCSI_TRANSPORT_ATTR_RO(_name)				\
1075 static struct target_stat_scsi_transport_attribute			\
1076 			target_stat_scsi_transport_##_name =		\
1077 	__CONFIGFS_EATTR_RO(_name,					\
1078 	target_stat_scsi_transport_show_attr_##_name);
1079 
target_stat_scsi_transport_show_attr_inst(struct se_port_stat_grps * pgrps,char * page)1080 static ssize_t target_stat_scsi_transport_show_attr_inst(
1081 	struct se_port_stat_grps *pgrps, char *page)
1082 {
1083 	struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps);
1084 	struct se_device *dev = lun->lun_se_dev;
1085 	struct se_port *sep;
1086 	struct se_hba *hba;
1087 	ssize_t ret;
1088 
1089 	spin_lock(&lun->lun_sep_lock);
1090 	sep = lun->lun_sep;
1091 	if (!sep) {
1092 		spin_unlock(&lun->lun_sep_lock);
1093 		return -ENODEV;
1094 	}
1095 
1096 	hba = dev->se_hba;
1097 	ret = snprintf(page, PAGE_SIZE, "%u\n", hba->hba_index);
1098 	spin_unlock(&lun->lun_sep_lock);
1099 	return ret;
1100 }
1101 DEV_STAT_SCSI_TRANSPORT_ATTR_RO(inst);
1102 
target_stat_scsi_transport_show_attr_device(struct se_port_stat_grps * pgrps,char * page)1103 static ssize_t target_stat_scsi_transport_show_attr_device(
1104 	struct se_port_stat_grps *pgrps, char *page)
1105 {
1106 	struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps);
1107 	struct se_port *sep;
1108 	struct se_portal_group *tpg;
1109 	ssize_t ret;
1110 
1111 	spin_lock(&lun->lun_sep_lock);
1112 	sep = lun->lun_sep;
1113 	if (!sep) {
1114 		spin_unlock(&lun->lun_sep_lock);
1115 		return -ENODEV;
1116 	}
1117 	tpg = sep->sep_tpg;
1118 	/* scsiTransportType */
1119 	ret = snprintf(page, PAGE_SIZE, "scsiTransport%s\n",
1120 			tpg->se_tpg_tfo->get_fabric_name());
1121 	spin_unlock(&lun->lun_sep_lock);
1122 	return ret;
1123 }
1124 DEV_STAT_SCSI_TRANSPORT_ATTR_RO(device);
1125 
target_stat_scsi_transport_show_attr_indx(struct se_port_stat_grps * pgrps,char * page)1126 static ssize_t target_stat_scsi_transport_show_attr_indx(
1127 	struct se_port_stat_grps *pgrps, char *page)
1128 {
1129 	struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps);
1130 	struct se_port *sep;
1131 	struct se_portal_group *tpg;
1132 	ssize_t ret;
1133 
1134 	spin_lock(&lun->lun_sep_lock);
1135 	sep = lun->lun_sep;
1136 	if (!sep) {
1137 		spin_unlock(&lun->lun_sep_lock);
1138 		return -ENODEV;
1139 	}
1140 	tpg = sep->sep_tpg;
1141 	ret = snprintf(page, PAGE_SIZE, "%u\n",
1142 			tpg->se_tpg_tfo->tpg_get_inst_index(tpg));
1143 	spin_unlock(&lun->lun_sep_lock);
1144 	return ret;
1145 }
1146 DEV_STAT_SCSI_TRANSPORT_ATTR_RO(indx);
1147 
target_stat_scsi_transport_show_attr_dev_name(struct se_port_stat_grps * pgrps,char * page)1148 static ssize_t target_stat_scsi_transport_show_attr_dev_name(
1149 	struct se_port_stat_grps *pgrps, char *page)
1150 {
1151 	struct se_lun *lun = container_of(pgrps, struct se_lun, port_stat_grps);
1152 	struct se_device *dev = lun->lun_se_dev;
1153 	struct se_port *sep;
1154 	struct se_portal_group *tpg;
1155 	struct t10_wwn *wwn;
1156 	ssize_t ret;
1157 
1158 	spin_lock(&lun->lun_sep_lock);
1159 	sep = lun->lun_sep;
1160 	if (!sep) {
1161 		spin_unlock(&lun->lun_sep_lock);
1162 		return -ENODEV;
1163 	}
1164 	tpg = sep->sep_tpg;
1165 	wwn = &dev->se_sub_dev->t10_wwn;
1166 	/* scsiTransportDevName */
1167 	ret = snprintf(page, PAGE_SIZE, "%s+%s\n",
1168 			tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1169 			(strlen(wwn->unit_serial)) ? wwn->unit_serial :
1170 			wwn->vendor);
1171 	spin_unlock(&lun->lun_sep_lock);
1172 	return ret;
1173 }
1174 DEV_STAT_SCSI_TRANSPORT_ATTR_RO(dev_name);
1175 
1176 CONFIGFS_EATTR_OPS(target_stat_scsi_transport, se_port_stat_grps,
1177 		scsi_transport_group);
1178 
1179 static struct configfs_attribute *target_stat_scsi_transport_attrs[] = {
1180 	&target_stat_scsi_transport_inst.attr,
1181 	&target_stat_scsi_transport_device.attr,
1182 	&target_stat_scsi_transport_indx.attr,
1183 	&target_stat_scsi_transport_dev_name.attr,
1184 	NULL,
1185 };
1186 
1187 static struct configfs_item_operations target_stat_scsi_transport_attrib_ops = {
1188 	.show_attribute		= target_stat_scsi_transport_attr_show,
1189 	.store_attribute	= target_stat_scsi_transport_attr_store,
1190 };
1191 
1192 static struct config_item_type target_stat_scsi_transport_cit = {
1193 	.ct_item_ops		= &target_stat_scsi_transport_attrib_ops,
1194 	.ct_attrs		= target_stat_scsi_transport_attrs,
1195 	.ct_owner		= THIS_MODULE,
1196 };
1197 
1198 /*
1199  * Called from target_core_fabric_configfs.c:target_fabric_make_lun() to setup
1200  * the target port statistics groups + configfs CITs located in target_core_stat.c
1201  */
target_stat_setup_port_default_groups(struct se_lun * lun)1202 void target_stat_setup_port_default_groups(struct se_lun *lun)
1203 {
1204 	struct config_group *port_stat_grp = &lun->port_stat_grps.stat_group;
1205 
1206 	config_group_init_type_name(&lun->port_stat_grps.scsi_port_group,
1207 			"scsi_port", &target_stat_scsi_port_cit);
1208 	config_group_init_type_name(&lun->port_stat_grps.scsi_tgt_port_group,
1209 			"scsi_tgt_port", &target_stat_scsi_tgt_port_cit);
1210 	config_group_init_type_name(&lun->port_stat_grps.scsi_transport_group,
1211 			"scsi_transport", &target_stat_scsi_transport_cit);
1212 
1213 	port_stat_grp->default_groups[0] = &lun->port_stat_grps.scsi_port_group;
1214 	port_stat_grp->default_groups[1] = &lun->port_stat_grps.scsi_tgt_port_group;
1215 	port_stat_grp->default_groups[2] = &lun->port_stat_grps.scsi_transport_group;
1216 	port_stat_grp->default_groups[3] = NULL;
1217 }
1218 
1219 /*
1220  * SCSI Authorized Initiator Table
1221  */
1222 
1223 CONFIGFS_EATTR_STRUCT(target_stat_scsi_auth_intr, se_ml_stat_grps);
1224 #define DEV_STAT_SCSI_AUTH_INTR_ATTR(_name, _mode)			\
1225 static struct target_stat_scsi_auth_intr_attribute			\
1226 			target_stat_scsi_auth_intr_##_name =		\
1227 	__CONFIGFS_EATTR(_name, _mode,					\
1228 	target_stat_scsi_auth_intr_show_attr_##_name,			\
1229 	target_stat_scsi_auth_intr_store_attr_##_name);
1230 
1231 #define DEV_STAT_SCSI_AUTH_INTR_ATTR_RO(_name)				\
1232 static struct target_stat_scsi_auth_intr_attribute			\
1233 			target_stat_scsi_auth_intr_##_name =		\
1234 	__CONFIGFS_EATTR_RO(_name,					\
1235 	target_stat_scsi_auth_intr_show_attr_##_name);
1236 
target_stat_scsi_auth_intr_show_attr_inst(struct se_ml_stat_grps * lgrps,char * page)1237 static ssize_t target_stat_scsi_auth_intr_show_attr_inst(
1238 	struct se_ml_stat_grps *lgrps, char *page)
1239 {
1240 	struct se_lun_acl *lacl = container_of(lgrps,
1241 			struct se_lun_acl, ml_stat_grps);
1242 	struct se_node_acl *nacl = lacl->se_lun_nacl;
1243 	struct se_dev_entry *deve;
1244 	struct se_portal_group *tpg;
1245 	ssize_t ret;
1246 
1247 	spin_lock_irq(&nacl->device_list_lock);
1248 	deve = nacl->device_list[lacl->mapped_lun];
1249 	if (!deve->se_lun || !deve->se_lun_acl) {
1250 		spin_unlock_irq(&nacl->device_list_lock);
1251 		return -ENODEV;
1252 	}
1253 	tpg = nacl->se_tpg;
1254 	/* scsiInstIndex */
1255 	ret = snprintf(page, PAGE_SIZE, "%u\n",
1256 			tpg->se_tpg_tfo->tpg_get_inst_index(tpg));
1257 	spin_unlock_irq(&nacl->device_list_lock);
1258 	return ret;
1259 }
1260 DEV_STAT_SCSI_AUTH_INTR_ATTR_RO(inst);
1261 
target_stat_scsi_auth_intr_show_attr_dev(struct se_ml_stat_grps * lgrps,char * page)1262 static ssize_t target_stat_scsi_auth_intr_show_attr_dev(
1263 	struct se_ml_stat_grps *lgrps, char *page)
1264 {
1265 	struct se_lun_acl *lacl = container_of(lgrps,
1266 			struct se_lun_acl, ml_stat_grps);
1267 	struct se_node_acl *nacl = lacl->se_lun_nacl;
1268 	struct se_dev_entry *deve;
1269 	struct se_lun *lun;
1270 	ssize_t ret;
1271 
1272 	spin_lock_irq(&nacl->device_list_lock);
1273 	deve = nacl->device_list[lacl->mapped_lun];
1274 	if (!deve->se_lun || !deve->se_lun_acl) {
1275 		spin_unlock_irq(&nacl->device_list_lock);
1276 		return -ENODEV;
1277 	}
1278 	lun = deve->se_lun;
1279 	/* scsiDeviceIndex */
1280 	ret = snprintf(page, PAGE_SIZE, "%u\n", lun->lun_se_dev->dev_index);
1281 	spin_unlock_irq(&nacl->device_list_lock);
1282 	return ret;
1283 }
1284 DEV_STAT_SCSI_AUTH_INTR_ATTR_RO(dev);
1285 
target_stat_scsi_auth_intr_show_attr_port(struct se_ml_stat_grps * lgrps,char * page)1286 static ssize_t target_stat_scsi_auth_intr_show_attr_port(
1287 	struct se_ml_stat_grps *lgrps, char *page)
1288 {
1289 	struct se_lun_acl *lacl = container_of(lgrps,
1290 			struct se_lun_acl, ml_stat_grps);
1291 	struct se_node_acl *nacl = lacl->se_lun_nacl;
1292 	struct se_dev_entry *deve;
1293 	struct se_portal_group *tpg;
1294 	ssize_t ret;
1295 
1296 	spin_lock_irq(&nacl->device_list_lock);
1297 	deve = nacl->device_list[lacl->mapped_lun];
1298 	if (!deve->se_lun || !deve->se_lun_acl) {
1299 		spin_unlock_irq(&nacl->device_list_lock);
1300 		return -ENODEV;
1301 	}
1302 	tpg = nacl->se_tpg;
1303 	/* scsiAuthIntrTgtPortIndex */
1304 	ret = snprintf(page, PAGE_SIZE, "%u\n", tpg->se_tpg_tfo->tpg_get_tag(tpg));
1305 	spin_unlock_irq(&nacl->device_list_lock);
1306 	return ret;
1307 }
1308 DEV_STAT_SCSI_AUTH_INTR_ATTR_RO(port);
1309 
target_stat_scsi_auth_intr_show_attr_indx(struct se_ml_stat_grps * lgrps,char * page)1310 static ssize_t target_stat_scsi_auth_intr_show_attr_indx(
1311 	struct se_ml_stat_grps *lgrps, char *page)
1312 {
1313 	struct se_lun_acl *lacl = container_of(lgrps,
1314 			struct se_lun_acl, ml_stat_grps);
1315 	struct se_node_acl *nacl = lacl->se_lun_nacl;
1316 	struct se_dev_entry *deve;
1317 	ssize_t ret;
1318 
1319 	spin_lock_irq(&nacl->device_list_lock);
1320 	deve = nacl->device_list[lacl->mapped_lun];
1321 	if (!deve->se_lun || !deve->se_lun_acl) {
1322 		spin_unlock_irq(&nacl->device_list_lock);
1323 		return -ENODEV;
1324 	}
1325 	/* scsiAuthIntrIndex */
1326 	ret = snprintf(page, PAGE_SIZE, "%u\n", nacl->acl_index);
1327 	spin_unlock_irq(&nacl->device_list_lock);
1328 	return ret;
1329 }
1330 DEV_STAT_SCSI_AUTH_INTR_ATTR_RO(indx);
1331 
target_stat_scsi_auth_intr_show_attr_dev_or_port(struct se_ml_stat_grps * lgrps,char * page)1332 static ssize_t target_stat_scsi_auth_intr_show_attr_dev_or_port(
1333 	struct se_ml_stat_grps *lgrps, char *page)
1334 {
1335 	struct se_lun_acl *lacl = container_of(lgrps,
1336 			struct se_lun_acl, ml_stat_grps);
1337 	struct se_node_acl *nacl = lacl->se_lun_nacl;
1338 	struct se_dev_entry *deve;
1339 	ssize_t ret;
1340 
1341 	spin_lock_irq(&nacl->device_list_lock);
1342 	deve = nacl->device_list[lacl->mapped_lun];
1343 	if (!deve->se_lun || !deve->se_lun_acl) {
1344 		spin_unlock_irq(&nacl->device_list_lock);
1345 		return -ENODEV;
1346 	}
1347 	/* scsiAuthIntrDevOrPort */
1348 	ret = snprintf(page, PAGE_SIZE, "%u\n", 1);
1349 	spin_unlock_irq(&nacl->device_list_lock);
1350 	return ret;
1351 }
1352 DEV_STAT_SCSI_AUTH_INTR_ATTR_RO(dev_or_port);
1353 
target_stat_scsi_auth_intr_show_attr_intr_name(struct se_ml_stat_grps * lgrps,char * page)1354 static ssize_t target_stat_scsi_auth_intr_show_attr_intr_name(
1355 	struct se_ml_stat_grps *lgrps, char *page)
1356 {
1357 	struct se_lun_acl *lacl = container_of(lgrps,
1358 			struct se_lun_acl, ml_stat_grps);
1359 	struct se_node_acl *nacl = lacl->se_lun_nacl;
1360 	struct se_dev_entry *deve;
1361 	ssize_t ret;
1362 
1363 	spin_lock_irq(&nacl->device_list_lock);
1364 	deve = nacl->device_list[lacl->mapped_lun];
1365 	if (!deve->se_lun || !deve->se_lun_acl) {
1366 		spin_unlock_irq(&nacl->device_list_lock);
1367 		return -ENODEV;
1368 	}
1369 	/* scsiAuthIntrName */
1370 	ret = snprintf(page, PAGE_SIZE, "%s\n", nacl->initiatorname);
1371 	spin_unlock_irq(&nacl->device_list_lock);
1372 	return ret;
1373 }
1374 DEV_STAT_SCSI_AUTH_INTR_ATTR_RO(intr_name);
1375 
target_stat_scsi_auth_intr_show_attr_map_indx(struct se_ml_stat_grps * lgrps,char * page)1376 static ssize_t target_stat_scsi_auth_intr_show_attr_map_indx(
1377 	struct se_ml_stat_grps *lgrps, char *page)
1378 {
1379 	struct se_lun_acl *lacl = container_of(lgrps,
1380 			struct se_lun_acl, ml_stat_grps);
1381 	struct se_node_acl *nacl = lacl->se_lun_nacl;
1382 	struct se_dev_entry *deve;
1383 	ssize_t ret;
1384 
1385 	spin_lock_irq(&nacl->device_list_lock);
1386 	deve = nacl->device_list[lacl->mapped_lun];
1387 	if (!deve->se_lun || !deve->se_lun_acl) {
1388 		spin_unlock_irq(&nacl->device_list_lock);
1389 		return -ENODEV;
1390 	}
1391 	/* FIXME: scsiAuthIntrLunMapIndex */
1392 	ret = snprintf(page, PAGE_SIZE, "%u\n", 0);
1393 	spin_unlock_irq(&nacl->device_list_lock);
1394 	return ret;
1395 }
1396 DEV_STAT_SCSI_AUTH_INTR_ATTR_RO(map_indx);
1397 
target_stat_scsi_auth_intr_show_attr_att_count(struct se_ml_stat_grps * lgrps,char * page)1398 static ssize_t target_stat_scsi_auth_intr_show_attr_att_count(
1399 	struct se_ml_stat_grps *lgrps, char *page)
1400 {
1401 	struct se_lun_acl *lacl = container_of(lgrps,
1402 			struct se_lun_acl, ml_stat_grps);
1403 	struct se_node_acl *nacl = lacl->se_lun_nacl;
1404 	struct se_dev_entry *deve;
1405 	ssize_t ret;
1406 
1407 	spin_lock_irq(&nacl->device_list_lock);
1408 	deve = nacl->device_list[lacl->mapped_lun];
1409 	if (!deve->se_lun || !deve->se_lun_acl) {
1410 		spin_unlock_irq(&nacl->device_list_lock);
1411 		return -ENODEV;
1412 	}
1413 	/* scsiAuthIntrAttachedTimes */
1414 	ret = snprintf(page, PAGE_SIZE, "%u\n", deve->attach_count);
1415 	spin_unlock_irq(&nacl->device_list_lock);
1416 	return ret;
1417 }
1418 DEV_STAT_SCSI_AUTH_INTR_ATTR_RO(att_count);
1419 
target_stat_scsi_auth_intr_show_attr_num_cmds(struct se_ml_stat_grps * lgrps,char * page)1420 static ssize_t target_stat_scsi_auth_intr_show_attr_num_cmds(
1421 	struct se_ml_stat_grps *lgrps, char *page)
1422 {
1423 	struct se_lun_acl *lacl = container_of(lgrps,
1424 			struct se_lun_acl, ml_stat_grps);
1425 	struct se_node_acl *nacl = lacl->se_lun_nacl;
1426 	struct se_dev_entry *deve;
1427 	ssize_t ret;
1428 
1429 	spin_lock_irq(&nacl->device_list_lock);
1430 	deve = nacl->device_list[lacl->mapped_lun];
1431 	if (!deve->se_lun || !deve->se_lun_acl) {
1432 		spin_unlock_irq(&nacl->device_list_lock);
1433 		return -ENODEV;
1434 	}
1435 	/* scsiAuthIntrOutCommands */
1436 	ret = snprintf(page, PAGE_SIZE, "%u\n", deve->total_cmds);
1437 	spin_unlock_irq(&nacl->device_list_lock);
1438 	return ret;
1439 }
1440 DEV_STAT_SCSI_AUTH_INTR_ATTR_RO(num_cmds);
1441 
target_stat_scsi_auth_intr_show_attr_read_mbytes(struct se_ml_stat_grps * lgrps,char * page)1442 static ssize_t target_stat_scsi_auth_intr_show_attr_read_mbytes(
1443 	struct se_ml_stat_grps *lgrps, char *page)
1444 {
1445 	struct se_lun_acl *lacl = container_of(lgrps,
1446 			struct se_lun_acl, ml_stat_grps);
1447 	struct se_node_acl *nacl = lacl->se_lun_nacl;
1448 	struct se_dev_entry *deve;
1449 	ssize_t ret;
1450 
1451 	spin_lock_irq(&nacl->device_list_lock);
1452 	deve = nacl->device_list[lacl->mapped_lun];
1453 	if (!deve->se_lun || !deve->se_lun_acl) {
1454 		spin_unlock_irq(&nacl->device_list_lock);
1455 		return -ENODEV;
1456 	}
1457 	/* scsiAuthIntrReadMegaBytes */
1458 	ret = snprintf(page, PAGE_SIZE, "%u\n", (u32)(deve->read_bytes >> 20));
1459 	spin_unlock_irq(&nacl->device_list_lock);
1460 	return ret;
1461 }
1462 DEV_STAT_SCSI_AUTH_INTR_ATTR_RO(read_mbytes);
1463 
target_stat_scsi_auth_intr_show_attr_write_mbytes(struct se_ml_stat_grps * lgrps,char * page)1464 static ssize_t target_stat_scsi_auth_intr_show_attr_write_mbytes(
1465 	struct se_ml_stat_grps *lgrps, char *page)
1466 {
1467 	struct se_lun_acl *lacl = container_of(lgrps,
1468 			struct se_lun_acl, ml_stat_grps);
1469 	struct se_node_acl *nacl = lacl->se_lun_nacl;
1470 	struct se_dev_entry *deve;
1471 	ssize_t ret;
1472 
1473 	spin_lock_irq(&nacl->device_list_lock);
1474 	deve = nacl->device_list[lacl->mapped_lun];
1475 	if (!deve->se_lun || !deve->se_lun_acl) {
1476 		spin_unlock_irq(&nacl->device_list_lock);
1477 		return -ENODEV;
1478 	}
1479 	/* scsiAuthIntrWrittenMegaBytes */
1480 	ret = snprintf(page, PAGE_SIZE, "%u\n", (u32)(deve->write_bytes >> 20));
1481 	spin_unlock_irq(&nacl->device_list_lock);
1482 	return ret;
1483 }
1484 DEV_STAT_SCSI_AUTH_INTR_ATTR_RO(write_mbytes);
1485 
target_stat_scsi_auth_intr_show_attr_hs_num_cmds(struct se_ml_stat_grps * lgrps,char * page)1486 static ssize_t target_stat_scsi_auth_intr_show_attr_hs_num_cmds(
1487 	struct se_ml_stat_grps *lgrps, char *page)
1488 {
1489 	struct se_lun_acl *lacl = container_of(lgrps,
1490 			struct se_lun_acl, ml_stat_grps);
1491 	struct se_node_acl *nacl = lacl->se_lun_nacl;
1492 	struct se_dev_entry *deve;
1493 	ssize_t ret;
1494 
1495 	spin_lock_irq(&nacl->device_list_lock);
1496 	deve = nacl->device_list[lacl->mapped_lun];
1497 	if (!deve->se_lun || !deve->se_lun_acl) {
1498 		spin_unlock_irq(&nacl->device_list_lock);
1499 		return -ENODEV;
1500 	}
1501 	/* FIXME: scsiAuthIntrHSOutCommands */
1502 	ret = snprintf(page, PAGE_SIZE, "%u\n", 0);
1503 	spin_unlock_irq(&nacl->device_list_lock);
1504 	return ret;
1505 }
1506 DEV_STAT_SCSI_AUTH_INTR_ATTR_RO(hs_num_cmds);
1507 
target_stat_scsi_auth_intr_show_attr_creation_time(struct se_ml_stat_grps * lgrps,char * page)1508 static ssize_t target_stat_scsi_auth_intr_show_attr_creation_time(
1509 	struct se_ml_stat_grps *lgrps, char *page)
1510 {
1511 	struct se_lun_acl *lacl = container_of(lgrps,
1512 			struct se_lun_acl, ml_stat_grps);
1513 	struct se_node_acl *nacl = lacl->se_lun_nacl;
1514 	struct se_dev_entry *deve;
1515 	ssize_t ret;
1516 
1517 	spin_lock_irq(&nacl->device_list_lock);
1518 	deve = nacl->device_list[lacl->mapped_lun];
1519 	if (!deve->se_lun || !deve->se_lun_acl) {
1520 		spin_unlock_irq(&nacl->device_list_lock);
1521 		return -ENODEV;
1522 	}
1523 	/* scsiAuthIntrLastCreation */
1524 	ret = snprintf(page, PAGE_SIZE, "%u\n", (u32)(((u32)deve->creation_time -
1525 				INITIAL_JIFFIES) * 100 / HZ));
1526 	spin_unlock_irq(&nacl->device_list_lock);
1527 	return ret;
1528 }
1529 DEV_STAT_SCSI_AUTH_INTR_ATTR_RO(creation_time);
1530 
target_stat_scsi_auth_intr_show_attr_row_status(struct se_ml_stat_grps * lgrps,char * page)1531 static ssize_t target_stat_scsi_auth_intr_show_attr_row_status(
1532 	struct se_ml_stat_grps *lgrps, char *page)
1533 {
1534 	struct se_lun_acl *lacl = container_of(lgrps,
1535 			struct se_lun_acl, ml_stat_grps);
1536 	struct se_node_acl *nacl = lacl->se_lun_nacl;
1537 	struct se_dev_entry *deve;
1538 	ssize_t ret;
1539 
1540 	spin_lock_irq(&nacl->device_list_lock);
1541 	deve = nacl->device_list[lacl->mapped_lun];
1542 	if (!deve->se_lun || !deve->se_lun_acl) {
1543 		spin_unlock_irq(&nacl->device_list_lock);
1544 		return -ENODEV;
1545 	}
1546 	/* FIXME: scsiAuthIntrRowStatus */
1547 	ret = snprintf(page, PAGE_SIZE, "Ready\n");
1548 	spin_unlock_irq(&nacl->device_list_lock);
1549 	return ret;
1550 }
1551 DEV_STAT_SCSI_AUTH_INTR_ATTR_RO(row_status);
1552 
1553 CONFIGFS_EATTR_OPS(target_stat_scsi_auth_intr, se_ml_stat_grps,
1554 		scsi_auth_intr_group);
1555 
1556 static struct configfs_attribute *target_stat_scsi_auth_intr_attrs[] = {
1557 	&target_stat_scsi_auth_intr_inst.attr,
1558 	&target_stat_scsi_auth_intr_dev.attr,
1559 	&target_stat_scsi_auth_intr_port.attr,
1560 	&target_stat_scsi_auth_intr_indx.attr,
1561 	&target_stat_scsi_auth_intr_dev_or_port.attr,
1562 	&target_stat_scsi_auth_intr_intr_name.attr,
1563 	&target_stat_scsi_auth_intr_map_indx.attr,
1564 	&target_stat_scsi_auth_intr_att_count.attr,
1565 	&target_stat_scsi_auth_intr_num_cmds.attr,
1566 	&target_stat_scsi_auth_intr_read_mbytes.attr,
1567 	&target_stat_scsi_auth_intr_write_mbytes.attr,
1568 	&target_stat_scsi_auth_intr_hs_num_cmds.attr,
1569 	&target_stat_scsi_auth_intr_creation_time.attr,
1570 	&target_stat_scsi_auth_intr_row_status.attr,
1571 	NULL,
1572 };
1573 
1574 static struct configfs_item_operations target_stat_scsi_auth_intr_attrib_ops = {
1575 	.show_attribute		= target_stat_scsi_auth_intr_attr_show,
1576 	.store_attribute	= target_stat_scsi_auth_intr_attr_store,
1577 };
1578 
1579 static struct config_item_type target_stat_scsi_auth_intr_cit = {
1580 	.ct_item_ops		= &target_stat_scsi_auth_intr_attrib_ops,
1581 	.ct_attrs		= target_stat_scsi_auth_intr_attrs,
1582 	.ct_owner		= THIS_MODULE,
1583 };
1584 
1585 /*
1586  * SCSI Attached Initiator Port Table
1587  */
1588 
1589 CONFIGFS_EATTR_STRUCT(target_stat_scsi_att_intr_port, se_ml_stat_grps);
1590 #define DEV_STAT_SCSI_ATTR_INTR_PORT_ATTR(_name, _mode)			\
1591 static struct target_stat_scsi_att_intr_port_attribute			\
1592 		target_stat_scsi_att_intr_port_##_name =		\
1593 	__CONFIGFS_EATTR(_name, _mode,					\
1594 	target_stat_scsi_att_intr_port_show_attr_##_name,		\
1595 	target_stat_scsi_att_intr_port_store_attr_##_name);
1596 
1597 #define DEV_STAT_SCSI_ATTR_INTR_PORT_ATTR_RO(_name)			\
1598 static struct target_stat_scsi_att_intr_port_attribute			\
1599 		target_stat_scsi_att_intr_port_##_name =		\
1600 	__CONFIGFS_EATTR_RO(_name,					\
1601 	target_stat_scsi_att_intr_port_show_attr_##_name);
1602 
target_stat_scsi_att_intr_port_show_attr_inst(struct se_ml_stat_grps * lgrps,char * page)1603 static ssize_t target_stat_scsi_att_intr_port_show_attr_inst(
1604 	struct se_ml_stat_grps *lgrps, char *page)
1605 {
1606 	struct se_lun_acl *lacl = container_of(lgrps,
1607 			struct se_lun_acl, ml_stat_grps);
1608 	struct se_node_acl *nacl = lacl->se_lun_nacl;
1609 	struct se_dev_entry *deve;
1610 	struct se_portal_group *tpg;
1611 	ssize_t ret;
1612 
1613 	spin_lock_irq(&nacl->device_list_lock);
1614 	deve = nacl->device_list[lacl->mapped_lun];
1615 	if (!deve->se_lun || !deve->se_lun_acl) {
1616 		spin_unlock_irq(&nacl->device_list_lock);
1617 		return -ENODEV;
1618 	}
1619 	tpg = nacl->se_tpg;
1620 	/* scsiInstIndex */
1621 	ret = snprintf(page, PAGE_SIZE, "%u\n",
1622 			tpg->se_tpg_tfo->tpg_get_inst_index(tpg));
1623 	spin_unlock_irq(&nacl->device_list_lock);
1624 	return ret;
1625 }
1626 DEV_STAT_SCSI_ATTR_INTR_PORT_ATTR_RO(inst);
1627 
target_stat_scsi_att_intr_port_show_attr_dev(struct se_ml_stat_grps * lgrps,char * page)1628 static ssize_t target_stat_scsi_att_intr_port_show_attr_dev(
1629 	struct se_ml_stat_grps *lgrps, char *page)
1630 {
1631 	struct se_lun_acl *lacl = container_of(lgrps,
1632 			struct se_lun_acl, ml_stat_grps);
1633 	struct se_node_acl *nacl = lacl->se_lun_nacl;
1634 	struct se_dev_entry *deve;
1635 	struct se_lun *lun;
1636 	ssize_t ret;
1637 
1638 	spin_lock_irq(&nacl->device_list_lock);
1639 	deve = nacl->device_list[lacl->mapped_lun];
1640 	if (!deve->se_lun || !deve->se_lun_acl) {
1641 		spin_unlock_irq(&nacl->device_list_lock);
1642 		return -ENODEV;
1643 	}
1644 	lun = deve->se_lun;
1645 	/* scsiDeviceIndex */
1646 	ret = snprintf(page, PAGE_SIZE, "%u\n", lun->lun_se_dev->dev_index);
1647 	spin_unlock_irq(&nacl->device_list_lock);
1648 	return ret;
1649 }
1650 DEV_STAT_SCSI_ATTR_INTR_PORT_ATTR_RO(dev);
1651 
target_stat_scsi_att_intr_port_show_attr_port(struct se_ml_stat_grps * lgrps,char * page)1652 static ssize_t target_stat_scsi_att_intr_port_show_attr_port(
1653 	struct se_ml_stat_grps *lgrps, char *page)
1654 {
1655 	struct se_lun_acl *lacl = container_of(lgrps,
1656 			struct se_lun_acl, ml_stat_grps);
1657 	struct se_node_acl *nacl = lacl->se_lun_nacl;
1658 	struct se_dev_entry *deve;
1659 	struct se_portal_group *tpg;
1660 	ssize_t ret;
1661 
1662 	spin_lock_irq(&nacl->device_list_lock);
1663 	deve = nacl->device_list[lacl->mapped_lun];
1664 	if (!deve->se_lun || !deve->se_lun_acl) {
1665 		spin_unlock_irq(&nacl->device_list_lock);
1666 		return -ENODEV;
1667 	}
1668 	tpg = nacl->se_tpg;
1669 	/* scsiPortIndex */
1670 	ret = snprintf(page, PAGE_SIZE, "%u\n", tpg->se_tpg_tfo->tpg_get_tag(tpg));
1671 	spin_unlock_irq(&nacl->device_list_lock);
1672 	return ret;
1673 }
1674 DEV_STAT_SCSI_ATTR_INTR_PORT_ATTR_RO(port);
1675 
target_stat_scsi_att_intr_port_show_attr_indx(struct se_ml_stat_grps * lgrps,char * page)1676 static ssize_t target_stat_scsi_att_intr_port_show_attr_indx(
1677 	struct se_ml_stat_grps *lgrps, char *page)
1678 {
1679 	struct se_lun_acl *lacl = container_of(lgrps,
1680 			struct se_lun_acl, ml_stat_grps);
1681 	struct se_node_acl *nacl = lacl->se_lun_nacl;
1682 	struct se_session *se_sess;
1683 	struct se_portal_group *tpg;
1684 	ssize_t ret;
1685 
1686 	spin_lock_irq(&nacl->nacl_sess_lock);
1687 	se_sess = nacl->nacl_sess;
1688 	if (!se_sess) {
1689 		spin_unlock_irq(&nacl->nacl_sess_lock);
1690 		return -ENODEV;
1691 	}
1692 
1693 	tpg = nacl->se_tpg;
1694 	/* scsiAttIntrPortIndex */
1695 	ret = snprintf(page, PAGE_SIZE, "%u\n",
1696 			tpg->se_tpg_tfo->sess_get_index(se_sess));
1697 	spin_unlock_irq(&nacl->nacl_sess_lock);
1698 	return ret;
1699 }
1700 DEV_STAT_SCSI_ATTR_INTR_PORT_ATTR_RO(indx);
1701 
target_stat_scsi_att_intr_port_show_attr_port_auth_indx(struct se_ml_stat_grps * lgrps,char * page)1702 static ssize_t target_stat_scsi_att_intr_port_show_attr_port_auth_indx(
1703 	struct se_ml_stat_grps *lgrps, char *page)
1704 {
1705 	struct se_lun_acl *lacl = container_of(lgrps,
1706 			struct se_lun_acl, ml_stat_grps);
1707 	struct se_node_acl *nacl = lacl->se_lun_nacl;
1708 	struct se_dev_entry *deve;
1709 	ssize_t ret;
1710 
1711 	spin_lock_irq(&nacl->device_list_lock);
1712 	deve = nacl->device_list[lacl->mapped_lun];
1713 	if (!deve->se_lun || !deve->se_lun_acl) {
1714 		spin_unlock_irq(&nacl->device_list_lock);
1715 		return -ENODEV;
1716 	}
1717 	/* scsiAttIntrPortAuthIntrIdx */
1718 	ret = snprintf(page, PAGE_SIZE, "%u\n", nacl->acl_index);
1719 	spin_unlock_irq(&nacl->device_list_lock);
1720 	return ret;
1721 }
1722 DEV_STAT_SCSI_ATTR_INTR_PORT_ATTR_RO(port_auth_indx);
1723 
target_stat_scsi_att_intr_port_show_attr_port_ident(struct se_ml_stat_grps * lgrps,char * page)1724 static ssize_t target_stat_scsi_att_intr_port_show_attr_port_ident(
1725 	struct se_ml_stat_grps *lgrps, char *page)
1726 {
1727 	struct se_lun_acl *lacl = container_of(lgrps,
1728 			struct se_lun_acl, ml_stat_grps);
1729 	struct se_node_acl *nacl = lacl->se_lun_nacl;
1730 	struct se_session *se_sess;
1731 	struct se_portal_group *tpg;
1732 	ssize_t ret;
1733 	unsigned char buf[64];
1734 
1735 	spin_lock_irq(&nacl->nacl_sess_lock);
1736 	se_sess = nacl->nacl_sess;
1737 	if (!se_sess) {
1738 		spin_unlock_irq(&nacl->nacl_sess_lock);
1739 		return -ENODEV;
1740 	}
1741 
1742 	tpg = nacl->se_tpg;
1743 	/* scsiAttIntrPortName+scsiAttIntrPortIdentifier */
1744 	memset(buf, 0, 64);
1745 	if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL)
1746 		tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess, buf, 64);
1747 
1748 	ret = snprintf(page, PAGE_SIZE, "%s+i+%s\n", nacl->initiatorname, buf);
1749 	spin_unlock_irq(&nacl->nacl_sess_lock);
1750 	return ret;
1751 }
1752 DEV_STAT_SCSI_ATTR_INTR_PORT_ATTR_RO(port_ident);
1753 
1754 CONFIGFS_EATTR_OPS(target_stat_scsi_att_intr_port, se_ml_stat_grps,
1755 		scsi_att_intr_port_group);
1756 
1757 static struct configfs_attribute *target_stat_scsi_ath_intr_port_attrs[] = {
1758 	&target_stat_scsi_att_intr_port_inst.attr,
1759 	&target_stat_scsi_att_intr_port_dev.attr,
1760 	&target_stat_scsi_att_intr_port_port.attr,
1761 	&target_stat_scsi_att_intr_port_indx.attr,
1762 	&target_stat_scsi_att_intr_port_port_auth_indx.attr,
1763 	&target_stat_scsi_att_intr_port_port_ident.attr,
1764 	NULL,
1765 };
1766 
1767 static struct configfs_item_operations target_stat_scsi_att_intr_port_attrib_ops = {
1768 	.show_attribute		= target_stat_scsi_att_intr_port_attr_show,
1769 	.store_attribute	= target_stat_scsi_att_intr_port_attr_store,
1770 };
1771 
1772 static struct config_item_type target_stat_scsi_att_intr_port_cit = {
1773 	.ct_item_ops		= &target_stat_scsi_att_intr_port_attrib_ops,
1774 	.ct_attrs		= target_stat_scsi_ath_intr_port_attrs,
1775 	.ct_owner		= THIS_MODULE,
1776 };
1777 
1778 /*
1779  * Called from target_core_fabric_configfs.c:target_fabric_make_mappedlun() to setup
1780  * the target MappedLUN statistics groups + configfs CITs located in target_core_stat.c
1781  */
target_stat_setup_mappedlun_default_groups(struct se_lun_acl * lacl)1782 void target_stat_setup_mappedlun_default_groups(struct se_lun_acl *lacl)
1783 {
1784 	struct config_group *ml_stat_grp = &lacl->ml_stat_grps.stat_group;
1785 
1786 	config_group_init_type_name(&lacl->ml_stat_grps.scsi_auth_intr_group,
1787 			"scsi_auth_intr", &target_stat_scsi_auth_intr_cit);
1788 	config_group_init_type_name(&lacl->ml_stat_grps.scsi_att_intr_port_group,
1789 			"scsi_att_intr_port", &target_stat_scsi_att_intr_port_cit);
1790 
1791 	ml_stat_grp->default_groups[0] = &lacl->ml_stat_grps.scsi_auth_intr_group;
1792 	ml_stat_grp->default_groups[1] = &lacl->ml_stat_grps.scsi_att_intr_port_group;
1793 	ml_stat_grp->default_groups[2] = NULL;
1794 }
1795