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