1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*******************************************************************************
3  * Filename:  target_core_stat.c
4  *
5  * Modern ConfigFS group context specific statistics based on original
6  * target_core_mib.c code
7  *
8  * (c) Copyright 2006-2013 Datera, Inc.
9  *
10  * Nicholas A. Bellinger <nab@linux-iscsi.org>
11  *
12  ******************************************************************************/
13 
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/delay.h>
17 #include <linux/timer.h>
18 #include <linux/string.h>
19 #include <linux/utsname.h>
20 #include <linux/proc_fs.h>
21 #include <linux/seq_file.h>
22 #include <linux/configfs.h>
23 
24 #include <target/target_core_base.h>
25 #include <target/target_core_backend.h>
26 #include <target/target_core_fabric.h>
27 
28 #include "target_core_internal.h"
29 
30 #ifndef INITIAL_JIFFIES
31 #define INITIAL_JIFFIES ((unsigned long)(unsigned int) (-300*HZ))
32 #endif
33 
34 #define SCSI_LU_INDEX			1
35 #define LU_COUNT			1
36 
37 /*
38  * SCSI Device Table
39  */
40 
41 static struct se_device *to_stat_dev(struct config_item *item)
42 {
43 	struct se_dev_stat_grps *sgrps = container_of(to_config_group(item),
44 			struct se_dev_stat_grps, scsi_dev_group);
45 	return container_of(sgrps, struct se_device, dev_stat_grps);
46 }
47 
48 static ssize_t target_stat_inst_show(struct config_item *item, char *page)
49 {
50 	struct se_hba *hba = to_stat_dev(item)->se_hba;
51 
52 	return snprintf(page, PAGE_SIZE, "%u\n", hba->hba_index);
53 }
54 
55 static ssize_t target_stat_indx_show(struct config_item *item, char *page)
56 {
57 	return snprintf(page, PAGE_SIZE, "%u\n", to_stat_dev(item)->dev_index);
58 }
59 
60 static ssize_t target_stat_role_show(struct config_item *item, char *page)
61 {
62 	return snprintf(page, PAGE_SIZE, "Target\n");
63 }
64 
65 static ssize_t target_stat_ports_show(struct config_item *item, char *page)
66 {
67 	return snprintf(page, PAGE_SIZE, "%u\n", to_stat_dev(item)->export_count);
68 }
69 
70 CONFIGFS_ATTR_RO(target_stat_, inst);
71 CONFIGFS_ATTR_RO(target_stat_, indx);
72 CONFIGFS_ATTR_RO(target_stat_, role);
73 CONFIGFS_ATTR_RO(target_stat_, ports);
74 
75 static struct configfs_attribute *target_stat_scsi_dev_attrs[] = {
76 	&target_stat_attr_inst,
77 	&target_stat_attr_indx,
78 	&target_stat_attr_role,
79 	&target_stat_attr_ports,
80 	NULL,
81 };
82 
83 static const struct config_item_type target_stat_scsi_dev_cit = {
84 	.ct_attrs		= target_stat_scsi_dev_attrs,
85 	.ct_owner		= THIS_MODULE,
86 };
87 
88 /*
89  * SCSI Target Device Table
90  */
91 static struct se_device *to_stat_tgt_dev(struct config_item *item)
92 {
93 	struct se_dev_stat_grps *sgrps = container_of(to_config_group(item),
94 			struct se_dev_stat_grps, scsi_tgt_dev_group);
95 	return container_of(sgrps, struct se_device, dev_stat_grps);
96 }
97 
98 static ssize_t target_stat_tgt_inst_show(struct config_item *item, char *page)
99 {
100 	struct se_hba *hba = to_stat_tgt_dev(item)->se_hba;
101 
102 	return snprintf(page, PAGE_SIZE, "%u\n", hba->hba_index);
103 }
104 
105 static ssize_t target_stat_tgt_indx_show(struct config_item *item, char *page)
106 {
107 	return snprintf(page, PAGE_SIZE, "%u\n", to_stat_tgt_dev(item)->dev_index);
108 }
109 
110 static ssize_t target_stat_tgt_num_lus_show(struct config_item *item,
111 		char *page)
112 {
113 	return snprintf(page, PAGE_SIZE, "%u\n", LU_COUNT);
114 }
115 
116 static ssize_t target_stat_tgt_status_show(struct config_item *item,
117 		char *page)
118 {
119 	if (to_stat_tgt_dev(item)->export_count)
120 		return snprintf(page, PAGE_SIZE, "activated");
121 	else
122 		return snprintf(page, PAGE_SIZE, "deactivated");
123 }
124 
125 static ssize_t target_stat_tgt_non_access_lus_show(struct config_item *item,
126 		char *page)
127 {
128 	int non_accessible_lus;
129 
130 	if (to_stat_tgt_dev(item)->export_count)
131 		non_accessible_lus = 0;
132 	else
133 		non_accessible_lus = 1;
134 
135 	return snprintf(page, PAGE_SIZE, "%u\n", non_accessible_lus);
136 }
137 
138 static ssize_t target_stat_tgt_resets_show(struct config_item *item,
139 		char *page)
140 {
141 	return snprintf(page, PAGE_SIZE, "%lu\n",
142 			atomic_long_read(&to_stat_tgt_dev(item)->num_resets));
143 }
144 
145 static ssize_t target_stat_tgt_aborts_complete_show(struct config_item *item,
146 		char *page)
147 {
148 	return snprintf(page, PAGE_SIZE, "%lu\n",
149 			atomic_long_read(&to_stat_tgt_dev(item)->aborts_complete));
150 }
151 
152 static ssize_t target_stat_tgt_aborts_no_task_show(struct config_item *item,
153 		char *page)
154 {
155 	return snprintf(page, PAGE_SIZE, "%lu\n",
156 			atomic_long_read(&to_stat_tgt_dev(item)->aborts_no_task));
157 }
158 
159 CONFIGFS_ATTR_RO(target_stat_tgt_, inst);
160 CONFIGFS_ATTR_RO(target_stat_tgt_, indx);
161 CONFIGFS_ATTR_RO(target_stat_tgt_, num_lus);
162 CONFIGFS_ATTR_RO(target_stat_tgt_, status);
163 CONFIGFS_ATTR_RO(target_stat_tgt_, non_access_lus);
164 CONFIGFS_ATTR_RO(target_stat_tgt_, resets);
165 CONFIGFS_ATTR_RO(target_stat_tgt_, aborts_complete);
166 CONFIGFS_ATTR_RO(target_stat_tgt_, aborts_no_task);
167 
168 static struct configfs_attribute *target_stat_scsi_tgt_dev_attrs[] = {
169 	&target_stat_tgt_attr_inst,
170 	&target_stat_tgt_attr_indx,
171 	&target_stat_tgt_attr_num_lus,
172 	&target_stat_tgt_attr_status,
173 	&target_stat_tgt_attr_non_access_lus,
174 	&target_stat_tgt_attr_resets,
175 	&target_stat_tgt_attr_aborts_complete,
176 	&target_stat_tgt_attr_aborts_no_task,
177 	NULL,
178 };
179 
180 static const struct config_item_type target_stat_scsi_tgt_dev_cit = {
181 	.ct_attrs		= target_stat_scsi_tgt_dev_attrs,
182 	.ct_owner		= THIS_MODULE,
183 };
184 
185 /*
186  * SCSI Logical Unit Table
187  */
188 
189 static struct se_device *to_stat_lu_dev(struct config_item *item)
190 {
191 	struct se_dev_stat_grps *sgrps = container_of(to_config_group(item),
192 			struct se_dev_stat_grps, scsi_lu_group);
193 	return container_of(sgrps, struct se_device, dev_stat_grps);
194 }
195 
196 static ssize_t target_stat_lu_inst_show(struct config_item *item, char *page)
197 {
198 	struct se_hba *hba = to_stat_lu_dev(item)->se_hba;
199 
200 	return snprintf(page, PAGE_SIZE, "%u\n", hba->hba_index);
201 }
202 
203 static ssize_t target_stat_lu_dev_show(struct config_item *item, char *page)
204 {
205 	return snprintf(page, PAGE_SIZE, "%u\n",
206 			to_stat_lu_dev(item)->dev_index);
207 }
208 
209 static ssize_t target_stat_lu_indx_show(struct config_item *item, char *page)
210 {
211 	return snprintf(page, PAGE_SIZE, "%u\n", SCSI_LU_INDEX);
212 }
213 
214 static ssize_t target_stat_lu_lun_show(struct config_item *item, char *page)
215 {
216 	/* FIXME: scsiLuDefaultLun */
217 	return snprintf(page, PAGE_SIZE, "%llu\n", (unsigned long long)0);
218 }
219 
220 static ssize_t target_stat_lu_lu_name_show(struct config_item *item, char *page)
221 {
222 	struct se_device *dev = to_stat_lu_dev(item);
223 
224 	/* scsiLuWwnName */
225 	return snprintf(page, PAGE_SIZE, "%s\n",
226 			(strlen(dev->t10_wwn.unit_serial)) ?
227 			dev->t10_wwn.unit_serial : "None");
228 }
229 
230 static ssize_t target_stat_lu_vend_show(struct config_item *item, char *page)
231 {
232 	struct se_device *dev = to_stat_lu_dev(item);
233 
234 	return snprintf(page, PAGE_SIZE, "%-" __stringify(INQUIRY_VENDOR_LEN)
235 			"s\n", dev->t10_wwn.vendor);
236 }
237 
238 static ssize_t target_stat_lu_prod_show(struct config_item *item, char *page)
239 {
240 	struct se_device *dev = to_stat_lu_dev(item);
241 
242 	return snprintf(page, PAGE_SIZE, "%-" __stringify(INQUIRY_MODEL_LEN)
243 			"s\n", dev->t10_wwn.model);
244 }
245 
246 static ssize_t target_stat_lu_rev_show(struct config_item *item, char *page)
247 {
248 	struct se_device *dev = to_stat_lu_dev(item);
249 
250 	return snprintf(page, PAGE_SIZE, "%-" __stringify(INQUIRY_REVISION_LEN)
251 			"s\n", dev->t10_wwn.revision);
252 }
253 
254 static ssize_t target_stat_lu_dev_type_show(struct config_item *item, char *page)
255 {
256 	struct se_device *dev = to_stat_lu_dev(item);
257 
258 	/* scsiLuPeripheralType */
259 	return snprintf(page, PAGE_SIZE, "%u\n",
260 			dev->transport->get_device_type(dev));
261 }
262 
263 static ssize_t target_stat_lu_status_show(struct config_item *item, char *page)
264 {
265 	struct se_device *dev = to_stat_lu_dev(item);
266 
267 	/* scsiLuStatus */
268 	return snprintf(page, PAGE_SIZE, "%s\n",
269 		(dev->export_count) ? "available" : "notavailable");
270 }
271 
272 static ssize_t target_stat_lu_state_bit_show(struct config_item *item,
273 		char *page)
274 {
275 	/* scsiLuState */
276 	return snprintf(page, PAGE_SIZE, "exposed\n");
277 }
278 
279 static ssize_t target_stat_lu_num_cmds_show(struct config_item *item,
280 		char *page)
281 {
282 	struct se_device *dev = to_stat_lu_dev(item);
283 
284 	/* scsiLuNumCommands */
285 	return snprintf(page, PAGE_SIZE, "%lu\n",
286 			atomic_long_read(&dev->num_cmds));
287 }
288 
289 static ssize_t target_stat_lu_read_mbytes_show(struct config_item *item,
290 		char *page)
291 {
292 	struct se_device *dev = to_stat_lu_dev(item);
293 
294 	/* scsiLuReadMegaBytes */
295 	return snprintf(page, PAGE_SIZE, "%lu\n",
296 			atomic_long_read(&dev->read_bytes) >> 20);
297 }
298 
299 static ssize_t target_stat_lu_write_mbytes_show(struct config_item *item,
300 		char *page)
301 {
302 	struct se_device *dev = to_stat_lu_dev(item);
303 
304 	/* scsiLuWrittenMegaBytes */
305 	return snprintf(page, PAGE_SIZE, "%lu\n",
306 			atomic_long_read(&dev->write_bytes) >> 20);
307 }
308 
309 static ssize_t target_stat_lu_resets_show(struct config_item *item, char *page)
310 {
311 	struct se_device *dev = to_stat_lu_dev(item);
312 
313 	/* scsiLuInResets */
314 	return snprintf(page, PAGE_SIZE, "%lu\n",
315 		atomic_long_read(&dev->num_resets));
316 }
317 
318 static ssize_t target_stat_lu_full_stat_show(struct config_item *item,
319 		char *page)
320 {
321 	/* FIXME: scsiLuOutTaskSetFullStatus */
322 	return snprintf(page, PAGE_SIZE, "%u\n", 0);
323 }
324 
325 static ssize_t target_stat_lu_hs_num_cmds_show(struct config_item *item,
326 		char *page)
327 {
328 	/* FIXME: scsiLuHSInCommands */
329 	return snprintf(page, PAGE_SIZE, "%u\n", 0);
330 }
331 
332 static ssize_t target_stat_lu_creation_time_show(struct config_item *item,
333 		char *page)
334 {
335 	struct se_device *dev = to_stat_lu_dev(item);
336 
337 	/* scsiLuCreationTime */
338 	return snprintf(page, PAGE_SIZE, "%u\n", (u32)(((u32)dev->creation_time -
339 				INITIAL_JIFFIES) * 100 / HZ));
340 }
341 
342 CONFIGFS_ATTR_RO(target_stat_lu_, inst);
343 CONFIGFS_ATTR_RO(target_stat_lu_, dev);
344 CONFIGFS_ATTR_RO(target_stat_lu_, indx);
345 CONFIGFS_ATTR_RO(target_stat_lu_, lun);
346 CONFIGFS_ATTR_RO(target_stat_lu_, lu_name);
347 CONFIGFS_ATTR_RO(target_stat_lu_, vend);
348 CONFIGFS_ATTR_RO(target_stat_lu_, prod);
349 CONFIGFS_ATTR_RO(target_stat_lu_, rev);
350 CONFIGFS_ATTR_RO(target_stat_lu_, dev_type);
351 CONFIGFS_ATTR_RO(target_stat_lu_, status);
352 CONFIGFS_ATTR_RO(target_stat_lu_, state_bit);
353 CONFIGFS_ATTR_RO(target_stat_lu_, num_cmds);
354 CONFIGFS_ATTR_RO(target_stat_lu_, read_mbytes);
355 CONFIGFS_ATTR_RO(target_stat_lu_, write_mbytes);
356 CONFIGFS_ATTR_RO(target_stat_lu_, resets);
357 CONFIGFS_ATTR_RO(target_stat_lu_, full_stat);
358 CONFIGFS_ATTR_RO(target_stat_lu_, hs_num_cmds);
359 CONFIGFS_ATTR_RO(target_stat_lu_, creation_time);
360 
361 static struct configfs_attribute *target_stat_scsi_lu_attrs[] = {
362 	&target_stat_lu_attr_inst,
363 	&target_stat_lu_attr_dev,
364 	&target_stat_lu_attr_indx,
365 	&target_stat_lu_attr_lun,
366 	&target_stat_lu_attr_lu_name,
367 	&target_stat_lu_attr_vend,
368 	&target_stat_lu_attr_prod,
369 	&target_stat_lu_attr_rev,
370 	&target_stat_lu_attr_dev_type,
371 	&target_stat_lu_attr_status,
372 	&target_stat_lu_attr_state_bit,
373 	&target_stat_lu_attr_num_cmds,
374 	&target_stat_lu_attr_read_mbytes,
375 	&target_stat_lu_attr_write_mbytes,
376 	&target_stat_lu_attr_resets,
377 	&target_stat_lu_attr_full_stat,
378 	&target_stat_lu_attr_hs_num_cmds,
379 	&target_stat_lu_attr_creation_time,
380 	NULL,
381 };
382 
383 static const struct config_item_type target_stat_scsi_lu_cit = {
384 	.ct_attrs		= target_stat_scsi_lu_attrs,
385 	.ct_owner		= THIS_MODULE,
386 };
387 
388 /*
389  * Called from target_core_configfs.c:target_core_make_subdev() to setup
390  * the target statistics groups + configfs CITs located in target_core_stat.c
391  */
392 void target_stat_setup_dev_default_groups(struct se_device *dev)
393 {
394 	config_group_init_type_name(&dev->dev_stat_grps.scsi_dev_group,
395 			"scsi_dev", &target_stat_scsi_dev_cit);
396 	configfs_add_default_group(&dev->dev_stat_grps.scsi_dev_group,
397 			&dev->dev_stat_grps.stat_group);
398 
399 	config_group_init_type_name(&dev->dev_stat_grps.scsi_tgt_dev_group,
400 			"scsi_tgt_dev", &target_stat_scsi_tgt_dev_cit);
401 	configfs_add_default_group(&dev->dev_stat_grps.scsi_tgt_dev_group,
402 			&dev->dev_stat_grps.stat_group);
403 
404 	config_group_init_type_name(&dev->dev_stat_grps.scsi_lu_group,
405 			"scsi_lu", &target_stat_scsi_lu_cit);
406 	configfs_add_default_group(&dev->dev_stat_grps.scsi_lu_group,
407 			&dev->dev_stat_grps.stat_group);
408 }
409 
410 /*
411  * SCSI Port Table
412  */
413 
414 static struct se_lun *to_stat_port(struct config_item *item)
415 {
416 	struct se_port_stat_grps *pgrps = container_of(to_config_group(item),
417 			struct se_port_stat_grps, scsi_port_group);
418 	return container_of(pgrps, struct se_lun, port_stat_grps);
419 }
420 
421 static ssize_t target_stat_port_inst_show(struct config_item *item, char *page)
422 {
423 	struct se_lun *lun = to_stat_port(item);
424 	struct se_device *dev;
425 	ssize_t ret = -ENODEV;
426 
427 	rcu_read_lock();
428 	dev = rcu_dereference(lun->lun_se_dev);
429 	if (dev)
430 		ret = snprintf(page, PAGE_SIZE, "%u\n", dev->hba_index);
431 	rcu_read_unlock();
432 	return ret;
433 }
434 
435 static ssize_t target_stat_port_dev_show(struct config_item *item, char *page)
436 {
437 	struct se_lun *lun = to_stat_port(item);
438 	struct se_device *dev;
439 	ssize_t ret = -ENODEV;
440 
441 	rcu_read_lock();
442 	dev = rcu_dereference(lun->lun_se_dev);
443 	if (dev)
444 		ret = snprintf(page, PAGE_SIZE, "%u\n", dev->dev_index);
445 	rcu_read_unlock();
446 	return ret;
447 }
448 
449 static ssize_t target_stat_port_indx_show(struct config_item *item, char *page)
450 {
451 	struct se_lun *lun = to_stat_port(item);
452 	struct se_device *dev;
453 	ssize_t ret = -ENODEV;
454 
455 	rcu_read_lock();
456 	dev = rcu_dereference(lun->lun_se_dev);
457 	if (dev)
458 		ret = snprintf(page, PAGE_SIZE, "%u\n", lun->lun_rtpi);
459 	rcu_read_unlock();
460 	return ret;
461 }
462 
463 static ssize_t target_stat_port_role_show(struct config_item *item, char *page)
464 {
465 	struct se_lun *lun = to_stat_port(item);
466 	struct se_device *dev;
467 	ssize_t ret = -ENODEV;
468 
469 	rcu_read_lock();
470 	dev = rcu_dereference(lun->lun_se_dev);
471 	if (dev)
472 		ret = snprintf(page, PAGE_SIZE, "%s%u\n", "Device", dev->dev_index);
473 	rcu_read_unlock();
474 	return ret;
475 }
476 
477 static ssize_t target_stat_port_busy_count_show(struct config_item *item,
478 		char *page)
479 {
480 	struct se_lun *lun = to_stat_port(item);
481 	struct se_device *dev;
482 	ssize_t ret = -ENODEV;
483 
484 	rcu_read_lock();
485 	dev = rcu_dereference(lun->lun_se_dev);
486 	if (dev) {
487 		/* FIXME: scsiPortBusyStatuses  */
488 		ret = snprintf(page, PAGE_SIZE, "%u\n", 0);
489 	}
490 	rcu_read_unlock();
491 	return ret;
492 }
493 
494 CONFIGFS_ATTR_RO(target_stat_port_, inst);
495 CONFIGFS_ATTR_RO(target_stat_port_, dev);
496 CONFIGFS_ATTR_RO(target_stat_port_, indx);
497 CONFIGFS_ATTR_RO(target_stat_port_, role);
498 CONFIGFS_ATTR_RO(target_stat_port_, busy_count);
499 
500 static struct configfs_attribute *target_stat_scsi_port_attrs[] = {
501 	&target_stat_port_attr_inst,
502 	&target_stat_port_attr_dev,
503 	&target_stat_port_attr_indx,
504 	&target_stat_port_attr_role,
505 	&target_stat_port_attr_busy_count,
506 	NULL,
507 };
508 
509 static const struct config_item_type target_stat_scsi_port_cit = {
510 	.ct_attrs		= target_stat_scsi_port_attrs,
511 	.ct_owner		= THIS_MODULE,
512 };
513 
514 /*
515  * SCSI Target Port Table
516  */
517 static struct se_lun *to_stat_tgt_port(struct config_item *item)
518 {
519 	struct se_port_stat_grps *pgrps = container_of(to_config_group(item),
520 			struct se_port_stat_grps, scsi_tgt_port_group);
521 	return container_of(pgrps, struct se_lun, port_stat_grps);
522 }
523 
524 static ssize_t target_stat_tgt_port_inst_show(struct config_item *item,
525 		char *page)
526 {
527 	struct se_lun *lun = to_stat_tgt_port(item);
528 	struct se_device *dev;
529 	ssize_t ret = -ENODEV;
530 
531 	rcu_read_lock();
532 	dev = rcu_dereference(lun->lun_se_dev);
533 	if (dev)
534 		ret = snprintf(page, PAGE_SIZE, "%u\n", dev->hba_index);
535 	rcu_read_unlock();
536 	return ret;
537 }
538 
539 static ssize_t target_stat_tgt_port_dev_show(struct config_item *item,
540 		char *page)
541 {
542 	struct se_lun *lun = to_stat_tgt_port(item);
543 	struct se_device *dev;
544 	ssize_t ret = -ENODEV;
545 
546 	rcu_read_lock();
547 	dev = rcu_dereference(lun->lun_se_dev);
548 	if (dev)
549 		ret = snprintf(page, PAGE_SIZE, "%u\n", dev->dev_index);
550 	rcu_read_unlock();
551 	return ret;
552 }
553 
554 static ssize_t target_stat_tgt_port_indx_show(struct config_item *item,
555 		char *page)
556 {
557 	struct se_lun *lun = to_stat_tgt_port(item);
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", lun->lun_rtpi);
565 	rcu_read_unlock();
566 	return ret;
567 }
568 
569 static ssize_t target_stat_tgt_port_name_show(struct config_item *item,
570 		char *page)
571 {
572 	struct se_lun *lun = to_stat_tgt_port(item);
573 	struct se_portal_group *tpg = lun->lun_tpg;
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, "%sPort#%u\n",
581 			tpg->se_tpg_tfo->fabric_name,
582 			lun->lun_rtpi);
583 	rcu_read_unlock();
584 	return ret;
585 }
586 
587 static ssize_t target_stat_tgt_port_port_index_show(struct config_item *item,
588 		char *page)
589 {
590 	struct se_lun *lun = to_stat_tgt_port(item);
591 	struct se_portal_group *tpg = lun->lun_tpg;
592 	struct se_device *dev;
593 	ssize_t ret = -ENODEV;
594 
595 	rcu_read_lock();
596 	dev = rcu_dereference(lun->lun_se_dev);
597 	if (dev)
598 		ret = snprintf(page, PAGE_SIZE, "%s%s%d\n",
599 			tpg->se_tpg_tfo->tpg_get_wwn(tpg), "+t+",
600 			tpg->se_tpg_tfo->tpg_get_tag(tpg));
601 	rcu_read_unlock();
602 	return ret;
603 }
604 
605 static ssize_t target_stat_tgt_port_in_cmds_show(struct config_item *item,
606 		char *page)
607 {
608 	struct se_lun *lun = to_stat_tgt_port(item);
609 	struct se_device *dev;
610 	ssize_t ret = -ENODEV;
611 
612 	rcu_read_lock();
613 	dev = rcu_dereference(lun->lun_se_dev);
614 	if (dev)
615 		ret = snprintf(page, PAGE_SIZE, "%lu\n",
616 			       atomic_long_read(&lun->lun_stats.cmd_pdus));
617 	rcu_read_unlock();
618 	return ret;
619 }
620 
621 static ssize_t target_stat_tgt_port_write_mbytes_show(struct config_item *item,
622 		char *page)
623 {
624 	struct se_lun *lun = to_stat_tgt_port(item);
625 	struct se_device *dev;
626 	ssize_t ret = -ENODEV;
627 
628 	rcu_read_lock();
629 	dev = rcu_dereference(lun->lun_se_dev);
630 	if (dev)
631 		ret = snprintf(page, PAGE_SIZE, "%u\n",
632 			(u32)(atomic_long_read(&lun->lun_stats.rx_data_octets) >> 20));
633 	rcu_read_unlock();
634 	return ret;
635 }
636 
637 static ssize_t target_stat_tgt_port_read_mbytes_show(struct config_item *item,
638 		char *page)
639 {
640 	struct se_lun *lun = to_stat_tgt_port(item);
641 	struct se_device *dev;
642 	ssize_t ret = -ENODEV;
643 
644 	rcu_read_lock();
645 	dev = rcu_dereference(lun->lun_se_dev);
646 	if (dev)
647 		ret = snprintf(page, PAGE_SIZE, "%u\n",
648 				(u32)(atomic_long_read(&lun->lun_stats.tx_data_octets) >> 20));
649 	rcu_read_unlock();
650 	return ret;
651 }
652 
653 static ssize_t target_stat_tgt_port_hs_in_cmds_show(struct config_item *item,
654 		char *page)
655 {
656 	struct se_lun *lun = to_stat_tgt_port(item);
657 	struct se_device *dev;
658 	ssize_t ret = -ENODEV;
659 
660 	rcu_read_lock();
661 	dev = rcu_dereference(lun->lun_se_dev);
662 	if (dev) {
663 		/* FIXME: scsiTgtPortHsInCommands */
664 		ret = snprintf(page, PAGE_SIZE, "%u\n", 0);
665 	}
666 	rcu_read_unlock();
667 	return ret;
668 }
669 
670 CONFIGFS_ATTR_RO(target_stat_tgt_port_, inst);
671 CONFIGFS_ATTR_RO(target_stat_tgt_port_, dev);
672 CONFIGFS_ATTR_RO(target_stat_tgt_port_, indx);
673 CONFIGFS_ATTR_RO(target_stat_tgt_port_, name);
674 CONFIGFS_ATTR_RO(target_stat_tgt_port_, port_index);
675 CONFIGFS_ATTR_RO(target_stat_tgt_port_, in_cmds);
676 CONFIGFS_ATTR_RO(target_stat_tgt_port_, write_mbytes);
677 CONFIGFS_ATTR_RO(target_stat_tgt_port_, read_mbytes);
678 CONFIGFS_ATTR_RO(target_stat_tgt_port_, hs_in_cmds);
679 
680 static struct configfs_attribute *target_stat_scsi_tgt_port_attrs[] = {
681 	&target_stat_tgt_port_attr_inst,
682 	&target_stat_tgt_port_attr_dev,
683 	&target_stat_tgt_port_attr_indx,
684 	&target_stat_tgt_port_attr_name,
685 	&target_stat_tgt_port_attr_port_index,
686 	&target_stat_tgt_port_attr_in_cmds,
687 	&target_stat_tgt_port_attr_write_mbytes,
688 	&target_stat_tgt_port_attr_read_mbytes,
689 	&target_stat_tgt_port_attr_hs_in_cmds,
690 	NULL,
691 };
692 
693 static const struct config_item_type target_stat_scsi_tgt_port_cit = {
694 	.ct_attrs		= target_stat_scsi_tgt_port_attrs,
695 	.ct_owner		= THIS_MODULE,
696 };
697 
698 /*
699  * SCSI Transport Table
700  */
701 static struct se_lun *to_transport_stat(struct config_item *item)
702 {
703 	struct se_port_stat_grps *pgrps = container_of(to_config_group(item),
704 			struct se_port_stat_grps, scsi_transport_group);
705 	return container_of(pgrps, struct se_lun, port_stat_grps);
706 }
707 
708 static ssize_t target_stat_transport_inst_show(struct config_item *item,
709 		char *page)
710 {
711 	struct se_lun *lun = to_transport_stat(item);
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, "%u\n", dev->hba_index);
719 	rcu_read_unlock();
720 	return ret;
721 }
722 
723 static ssize_t target_stat_transport_device_show(struct config_item *item,
724 		char *page)
725 {
726 	struct se_lun *lun = to_transport_stat(item);
727 	struct se_device *dev;
728 	struct se_portal_group *tpg = lun->lun_tpg;
729 	ssize_t ret = -ENODEV;
730 
731 	rcu_read_lock();
732 	dev = rcu_dereference(lun->lun_se_dev);
733 	if (dev) {
734 		/* scsiTransportType */
735 		ret = snprintf(page, PAGE_SIZE, "scsiTransport%s\n",
736 			       tpg->se_tpg_tfo->fabric_name);
737 	}
738 	rcu_read_unlock();
739 	return ret;
740 }
741 
742 static ssize_t target_stat_transport_indx_show(struct config_item *item,
743 		char *page)
744 {
745 	struct se_lun *lun = to_transport_stat(item);
746 	struct se_device *dev;
747 	struct se_portal_group *tpg = lun->lun_tpg;
748 	ssize_t ret = -ENODEV;
749 
750 	rcu_read_lock();
751 	dev = rcu_dereference(lun->lun_se_dev);
752 	if (dev)
753 		ret = snprintf(page, PAGE_SIZE, "%u\n",
754 			       tpg->se_tpg_tfo->tpg_get_inst_index(tpg));
755 	rcu_read_unlock();
756 	return ret;
757 }
758 
759 static ssize_t target_stat_transport_dev_name_show(struct config_item *item,
760 		char *page)
761 {
762 	struct se_lun *lun = to_transport_stat(item);
763 	struct se_device *dev;
764 	struct se_portal_group *tpg = lun->lun_tpg;
765 	struct t10_wwn *wwn;
766 	ssize_t ret = -ENODEV;
767 
768 	rcu_read_lock();
769 	dev = rcu_dereference(lun->lun_se_dev);
770 	if (dev) {
771 		wwn = &dev->t10_wwn;
772 		/* scsiTransportDevName */
773 		ret = snprintf(page, PAGE_SIZE, "%s+%s\n",
774 				tpg->se_tpg_tfo->tpg_get_wwn(tpg),
775 				(strlen(wwn->unit_serial)) ? wwn->unit_serial :
776 				wwn->vendor);
777 	}
778 	rcu_read_unlock();
779 	return ret;
780 }
781 
782 static ssize_t target_stat_transport_proto_id_show(struct config_item *item,
783 		char *page)
784 {
785 	struct se_lun *lun = to_transport_stat(item);
786 	struct se_device *dev;
787 	struct se_portal_group *tpg = lun->lun_tpg;
788 	ssize_t ret = -ENODEV;
789 
790 	rcu_read_lock();
791 	dev = rcu_dereference(lun->lun_se_dev);
792 	if (dev)
793 		ret = snprintf(page, PAGE_SIZE, "%u\n", tpg->proto_id);
794 	rcu_read_unlock();
795 	return ret;
796 }
797 
798 CONFIGFS_ATTR_RO(target_stat_transport_, inst);
799 CONFIGFS_ATTR_RO(target_stat_transport_, device);
800 CONFIGFS_ATTR_RO(target_stat_transport_, indx);
801 CONFIGFS_ATTR_RO(target_stat_transport_, dev_name);
802 CONFIGFS_ATTR_RO(target_stat_transport_, proto_id);
803 
804 static struct configfs_attribute *target_stat_scsi_transport_attrs[] = {
805 	&target_stat_transport_attr_inst,
806 	&target_stat_transport_attr_device,
807 	&target_stat_transport_attr_indx,
808 	&target_stat_transport_attr_dev_name,
809 	&target_stat_transport_attr_proto_id,
810 	NULL,
811 };
812 
813 static const struct config_item_type target_stat_scsi_transport_cit = {
814 	.ct_attrs		= target_stat_scsi_transport_attrs,
815 	.ct_owner		= THIS_MODULE,
816 };
817 
818 /*
819  * Called from target_core_fabric_configfs.c:target_fabric_make_lun() to setup
820  * the target port statistics groups + configfs CITs located in target_core_stat.c
821  */
822 void target_stat_setup_port_default_groups(struct se_lun *lun)
823 {
824 	config_group_init_type_name(&lun->port_stat_grps.scsi_port_group,
825 			"scsi_port", &target_stat_scsi_port_cit);
826 	configfs_add_default_group(&lun->port_stat_grps.scsi_port_group,
827 			&lun->port_stat_grps.stat_group);
828 
829 	config_group_init_type_name(&lun->port_stat_grps.scsi_tgt_port_group,
830 			"scsi_tgt_port", &target_stat_scsi_tgt_port_cit);
831 	configfs_add_default_group(&lun->port_stat_grps.scsi_tgt_port_group,
832 			&lun->port_stat_grps.stat_group);
833 
834 	config_group_init_type_name(&lun->port_stat_grps.scsi_transport_group,
835 			"scsi_transport", &target_stat_scsi_transport_cit);
836 	configfs_add_default_group(&lun->port_stat_grps.scsi_transport_group,
837 			&lun->port_stat_grps.stat_group);
838 }
839 
840 /*
841  * SCSI Authorized Initiator Table
842  */
843 
844 static struct se_lun_acl *auth_to_lacl(struct config_item *item)
845 {
846 	struct se_ml_stat_grps *lgrps = container_of(to_config_group(item),
847 			struct se_ml_stat_grps, scsi_auth_intr_group);
848 	return container_of(lgrps, struct se_lun_acl, ml_stat_grps);
849 }
850 
851 static ssize_t target_stat_auth_inst_show(struct config_item *item,
852 		char *page)
853 {
854 	struct se_lun_acl *lacl = auth_to_lacl(item);
855 	struct se_node_acl *nacl = lacl->se_lun_nacl;
856 	struct se_dev_entry *deve;
857 	struct se_portal_group *tpg;
858 	ssize_t ret;
859 
860 	rcu_read_lock();
861 	deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
862 	if (!deve) {
863 		rcu_read_unlock();
864 		return -ENODEV;
865 	}
866 	tpg = nacl->se_tpg;
867 	/* scsiInstIndex */
868 	ret = snprintf(page, PAGE_SIZE, "%u\n",
869 			tpg->se_tpg_tfo->tpg_get_inst_index(tpg));
870 	rcu_read_unlock();
871 	return ret;
872 }
873 
874 static ssize_t target_stat_auth_dev_show(struct config_item *item,
875 		char *page)
876 {
877 	struct se_lun_acl *lacl = auth_to_lacl(item);
878 	struct se_node_acl *nacl = lacl->se_lun_nacl;
879 	struct se_dev_entry *deve;
880 	struct se_lun *lun;
881 	ssize_t ret;
882 
883 	rcu_read_lock();
884 	deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
885 	if (!deve) {
886 		rcu_read_unlock();
887 		return -ENODEV;
888 	}
889 	lun = rcu_dereference(deve->se_lun);
890 	/* scsiDeviceIndex */
891 	ret = snprintf(page, PAGE_SIZE, "%u\n", lun->lun_index);
892 	rcu_read_unlock();
893 	return ret;
894 }
895 
896 static ssize_t target_stat_auth_port_show(struct config_item *item,
897 		char *page)
898 {
899 	struct se_lun_acl *lacl = auth_to_lacl(item);
900 	struct se_node_acl *nacl = lacl->se_lun_nacl;
901 	struct se_dev_entry *deve;
902 	struct se_portal_group *tpg;
903 	ssize_t ret;
904 
905 	rcu_read_lock();
906 	deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
907 	if (!deve) {
908 		rcu_read_unlock();
909 		return -ENODEV;
910 	}
911 	tpg = nacl->se_tpg;
912 	/* scsiAuthIntrTgtPortIndex */
913 	ret = snprintf(page, PAGE_SIZE, "%u\n", tpg->se_tpg_tfo->tpg_get_tag(tpg));
914 	rcu_read_unlock();
915 	return ret;
916 }
917 
918 static ssize_t target_stat_auth_indx_show(struct config_item *item,
919 		char *page)
920 {
921 	struct se_lun_acl *lacl = auth_to_lacl(item);
922 	struct se_node_acl *nacl = lacl->se_lun_nacl;
923 	struct se_dev_entry *deve;
924 	ssize_t ret;
925 
926 	rcu_read_lock();
927 	deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
928 	if (!deve) {
929 		rcu_read_unlock();
930 		return -ENODEV;
931 	}
932 	/* scsiAuthIntrIndex */
933 	ret = snprintf(page, PAGE_SIZE, "%u\n", nacl->acl_index);
934 	rcu_read_unlock();
935 	return ret;
936 }
937 
938 static ssize_t target_stat_auth_dev_or_port_show(struct config_item *item,
939 		char *page)
940 {
941 	struct se_lun_acl *lacl = auth_to_lacl(item);
942 	struct se_node_acl *nacl = lacl->se_lun_nacl;
943 	struct se_dev_entry *deve;
944 	ssize_t ret;
945 
946 	rcu_read_lock();
947 	deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
948 	if (!deve) {
949 		rcu_read_unlock();
950 		return -ENODEV;
951 	}
952 	/* scsiAuthIntrDevOrPort */
953 	ret = snprintf(page, PAGE_SIZE, "%u\n", 1);
954 	rcu_read_unlock();
955 	return ret;
956 }
957 
958 static ssize_t target_stat_auth_intr_name_show(struct config_item *item,
959 		char *page)
960 {
961 	struct se_lun_acl *lacl = auth_to_lacl(item);
962 	struct se_node_acl *nacl = lacl->se_lun_nacl;
963 	struct se_dev_entry *deve;
964 	ssize_t ret;
965 
966 	rcu_read_lock();
967 	deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
968 	if (!deve) {
969 		rcu_read_unlock();
970 		return -ENODEV;
971 	}
972 	/* scsiAuthIntrName */
973 	ret = snprintf(page, PAGE_SIZE, "%s\n", nacl->initiatorname);
974 	rcu_read_unlock();
975 	return ret;
976 }
977 
978 static ssize_t target_stat_auth_map_indx_show(struct config_item *item,
979 		char *page)
980 {
981 	struct se_lun_acl *lacl = auth_to_lacl(item);
982 	struct se_node_acl *nacl = lacl->se_lun_nacl;
983 	struct se_dev_entry *deve;
984 	ssize_t ret;
985 
986 	rcu_read_lock();
987 	deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
988 	if (!deve) {
989 		rcu_read_unlock();
990 		return -ENODEV;
991 	}
992 	/* FIXME: scsiAuthIntrLunMapIndex */
993 	ret = snprintf(page, PAGE_SIZE, "%u\n", 0);
994 	rcu_read_unlock();
995 	return ret;
996 }
997 
998 static ssize_t target_stat_auth_att_count_show(struct config_item *item,
999 		char *page)
1000 {
1001 	struct se_lun_acl *lacl = auth_to_lacl(item);
1002 	struct se_node_acl *nacl = lacl->se_lun_nacl;
1003 	struct se_dev_entry *deve;
1004 	ssize_t ret;
1005 
1006 	rcu_read_lock();
1007 	deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
1008 	if (!deve) {
1009 		rcu_read_unlock();
1010 		return -ENODEV;
1011 	}
1012 	/* scsiAuthIntrAttachedTimes */
1013 	ret = snprintf(page, PAGE_SIZE, "%u\n", deve->attach_count);
1014 	rcu_read_unlock();
1015 	return ret;
1016 }
1017 
1018 static ssize_t target_stat_auth_num_cmds_show(struct config_item *item,
1019 		char *page)
1020 {
1021 	struct se_lun_acl *lacl = auth_to_lacl(item);
1022 	struct se_node_acl *nacl = lacl->se_lun_nacl;
1023 	struct se_dev_entry *deve;
1024 	ssize_t ret;
1025 
1026 	rcu_read_lock();
1027 	deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
1028 	if (!deve) {
1029 		rcu_read_unlock();
1030 		return -ENODEV;
1031 	}
1032 	/* scsiAuthIntrOutCommands */
1033 	ret = snprintf(page, PAGE_SIZE, "%lu\n",
1034 		       atomic_long_read(&deve->total_cmds));
1035 	rcu_read_unlock();
1036 	return ret;
1037 }
1038 
1039 static ssize_t target_stat_auth_read_mbytes_show(struct config_item *item,
1040 		char *page)
1041 {
1042 	struct se_lun_acl *lacl = auth_to_lacl(item);
1043 	struct se_node_acl *nacl = lacl->se_lun_nacl;
1044 	struct se_dev_entry *deve;
1045 	ssize_t ret;
1046 
1047 	rcu_read_lock();
1048 	deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
1049 	if (!deve) {
1050 		rcu_read_unlock();
1051 		return -ENODEV;
1052 	}
1053 	/* scsiAuthIntrReadMegaBytes */
1054 	ret = snprintf(page, PAGE_SIZE, "%u\n",
1055 		      (u32)(atomic_long_read(&deve->read_bytes) >> 20));
1056 	rcu_read_unlock();
1057 	return ret;
1058 }
1059 
1060 static ssize_t target_stat_auth_write_mbytes_show(struct config_item *item,
1061 		char *page)
1062 {
1063 	struct se_lun_acl *lacl = auth_to_lacl(item);
1064 	struct se_node_acl *nacl = lacl->se_lun_nacl;
1065 	struct se_dev_entry *deve;
1066 	ssize_t ret;
1067 
1068 	rcu_read_lock();
1069 	deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
1070 	if (!deve) {
1071 		rcu_read_unlock();
1072 		return -ENODEV;
1073 	}
1074 	/* scsiAuthIntrWrittenMegaBytes */
1075 	ret = snprintf(page, PAGE_SIZE, "%u\n",
1076 		      (u32)(atomic_long_read(&deve->write_bytes) >> 20));
1077 	rcu_read_unlock();
1078 	return ret;
1079 }
1080 
1081 static ssize_t target_stat_auth_hs_num_cmds_show(struct config_item *item,
1082 		char *page)
1083 {
1084 	struct se_lun_acl *lacl = auth_to_lacl(item);
1085 	struct se_node_acl *nacl = lacl->se_lun_nacl;
1086 	struct se_dev_entry *deve;
1087 	ssize_t ret;
1088 
1089 	rcu_read_lock();
1090 	deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
1091 	if (!deve) {
1092 		rcu_read_unlock();
1093 		return -ENODEV;
1094 	}
1095 	/* FIXME: scsiAuthIntrHSOutCommands */
1096 	ret = snprintf(page, PAGE_SIZE, "%u\n", 0);
1097 	rcu_read_unlock();
1098 	return ret;
1099 }
1100 
1101 static ssize_t target_stat_auth_creation_time_show(struct config_item *item,
1102 		char *page)
1103 {
1104 	struct se_lun_acl *lacl = auth_to_lacl(item);
1105 	struct se_node_acl *nacl = lacl->se_lun_nacl;
1106 	struct se_dev_entry *deve;
1107 	ssize_t ret;
1108 
1109 	rcu_read_lock();
1110 	deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
1111 	if (!deve) {
1112 		rcu_read_unlock();
1113 		return -ENODEV;
1114 	}
1115 	/* scsiAuthIntrLastCreation */
1116 	ret = snprintf(page, PAGE_SIZE, "%u\n", (u32)(((u32)deve->creation_time -
1117 				INITIAL_JIFFIES) * 100 / HZ));
1118 	rcu_read_unlock();
1119 	return ret;
1120 }
1121 
1122 static ssize_t target_stat_auth_row_status_show(struct config_item *item,
1123 		char *page)
1124 {
1125 	struct se_lun_acl *lacl = auth_to_lacl(item);
1126 	struct se_node_acl *nacl = lacl->se_lun_nacl;
1127 	struct se_dev_entry *deve;
1128 	ssize_t ret;
1129 
1130 	rcu_read_lock();
1131 	deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
1132 	if (!deve) {
1133 		rcu_read_unlock();
1134 		return -ENODEV;
1135 	}
1136 	/* FIXME: scsiAuthIntrRowStatus */
1137 	ret = snprintf(page, PAGE_SIZE, "Ready\n");
1138 	rcu_read_unlock();
1139 	return ret;
1140 }
1141 
1142 CONFIGFS_ATTR_RO(target_stat_auth_, inst);
1143 CONFIGFS_ATTR_RO(target_stat_auth_, dev);
1144 CONFIGFS_ATTR_RO(target_stat_auth_, port);
1145 CONFIGFS_ATTR_RO(target_stat_auth_, indx);
1146 CONFIGFS_ATTR_RO(target_stat_auth_, dev_or_port);
1147 CONFIGFS_ATTR_RO(target_stat_auth_, intr_name);
1148 CONFIGFS_ATTR_RO(target_stat_auth_, map_indx);
1149 CONFIGFS_ATTR_RO(target_stat_auth_, att_count);
1150 CONFIGFS_ATTR_RO(target_stat_auth_, num_cmds);
1151 CONFIGFS_ATTR_RO(target_stat_auth_, read_mbytes);
1152 CONFIGFS_ATTR_RO(target_stat_auth_, write_mbytes);
1153 CONFIGFS_ATTR_RO(target_stat_auth_, hs_num_cmds);
1154 CONFIGFS_ATTR_RO(target_stat_auth_, creation_time);
1155 CONFIGFS_ATTR_RO(target_stat_auth_, row_status);
1156 
1157 static struct configfs_attribute *target_stat_scsi_auth_intr_attrs[] = {
1158 	&target_stat_auth_attr_inst,
1159 	&target_stat_auth_attr_dev,
1160 	&target_stat_auth_attr_port,
1161 	&target_stat_auth_attr_indx,
1162 	&target_stat_auth_attr_dev_or_port,
1163 	&target_stat_auth_attr_intr_name,
1164 	&target_stat_auth_attr_map_indx,
1165 	&target_stat_auth_attr_att_count,
1166 	&target_stat_auth_attr_num_cmds,
1167 	&target_stat_auth_attr_read_mbytes,
1168 	&target_stat_auth_attr_write_mbytes,
1169 	&target_stat_auth_attr_hs_num_cmds,
1170 	&target_stat_auth_attr_creation_time,
1171 	&target_stat_auth_attr_row_status,
1172 	NULL,
1173 };
1174 
1175 static const struct config_item_type target_stat_scsi_auth_intr_cit = {
1176 	.ct_attrs		= target_stat_scsi_auth_intr_attrs,
1177 	.ct_owner		= THIS_MODULE,
1178 };
1179 
1180 /*
1181  * SCSI Attached Initiator Port Table
1182  */
1183 
1184 static struct se_lun_acl *iport_to_lacl(struct config_item *item)
1185 {
1186 	struct se_ml_stat_grps *lgrps = container_of(to_config_group(item),
1187 			struct se_ml_stat_grps, scsi_att_intr_port_group);
1188 	return container_of(lgrps, struct se_lun_acl, ml_stat_grps);
1189 }
1190 
1191 static ssize_t target_stat_iport_inst_show(struct config_item *item,
1192 		char *page)
1193 {
1194 	struct se_lun_acl *lacl = iport_to_lacl(item);
1195 	struct se_node_acl *nacl = lacl->se_lun_nacl;
1196 	struct se_dev_entry *deve;
1197 	struct se_portal_group *tpg;
1198 	ssize_t ret;
1199 
1200 	rcu_read_lock();
1201 	deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
1202 	if (!deve) {
1203 		rcu_read_unlock();
1204 		return -ENODEV;
1205 	}
1206 	tpg = nacl->se_tpg;
1207 	/* scsiInstIndex */
1208 	ret = snprintf(page, PAGE_SIZE, "%u\n",
1209 			tpg->se_tpg_tfo->tpg_get_inst_index(tpg));
1210 	rcu_read_unlock();
1211 	return ret;
1212 }
1213 
1214 static ssize_t target_stat_iport_dev_show(struct config_item *item,
1215 		char *page)
1216 {
1217 	struct se_lun_acl *lacl = iport_to_lacl(item);
1218 	struct se_node_acl *nacl = lacl->se_lun_nacl;
1219 	struct se_dev_entry *deve;
1220 	struct se_lun *lun;
1221 	ssize_t ret;
1222 
1223 	rcu_read_lock();
1224 	deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
1225 	if (!deve) {
1226 		rcu_read_unlock();
1227 		return -ENODEV;
1228 	}
1229 	lun = rcu_dereference(deve->se_lun);
1230 	/* scsiDeviceIndex */
1231 	ret = snprintf(page, PAGE_SIZE, "%u\n", lun->lun_index);
1232 	rcu_read_unlock();
1233 	return ret;
1234 }
1235 
1236 static ssize_t target_stat_iport_port_show(struct config_item *item,
1237 		char *page)
1238 {
1239 	struct se_lun_acl *lacl = iport_to_lacl(item);
1240 	struct se_node_acl *nacl = lacl->se_lun_nacl;
1241 	struct se_dev_entry *deve;
1242 	struct se_portal_group *tpg;
1243 	ssize_t ret;
1244 
1245 	rcu_read_lock();
1246 	deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
1247 	if (!deve) {
1248 		rcu_read_unlock();
1249 		return -ENODEV;
1250 	}
1251 	tpg = nacl->se_tpg;
1252 	/* scsiPortIndex */
1253 	ret = snprintf(page, PAGE_SIZE, "%u\n", tpg->se_tpg_tfo->tpg_get_tag(tpg));
1254 	rcu_read_unlock();
1255 	return ret;
1256 }
1257 
1258 static ssize_t target_stat_iport_indx_show(struct config_item *item,
1259 		char *page)
1260 {
1261 	struct se_lun_acl *lacl = iport_to_lacl(item);
1262 	struct se_node_acl *nacl = lacl->se_lun_nacl;
1263 	struct se_session *se_sess;
1264 	struct se_portal_group *tpg;
1265 	ssize_t ret;
1266 
1267 	spin_lock_irq(&nacl->nacl_sess_lock);
1268 	se_sess = nacl->nacl_sess;
1269 	if (!se_sess) {
1270 		spin_unlock_irq(&nacl->nacl_sess_lock);
1271 		return -ENODEV;
1272 	}
1273 
1274 	tpg = nacl->se_tpg;
1275 	/* scsiAttIntrPortIndex */
1276 	ret = snprintf(page, PAGE_SIZE, "%u\n",
1277 			tpg->se_tpg_tfo->sess_get_index(se_sess));
1278 	spin_unlock_irq(&nacl->nacl_sess_lock);
1279 	return ret;
1280 }
1281 
1282 static ssize_t target_stat_iport_port_auth_indx_show(struct config_item *item,
1283 		char *page)
1284 {
1285 	struct se_lun_acl *lacl = iport_to_lacl(item);
1286 	struct se_node_acl *nacl = lacl->se_lun_nacl;
1287 	struct se_dev_entry *deve;
1288 	ssize_t ret;
1289 
1290 	rcu_read_lock();
1291 	deve = target_nacl_find_deve(nacl, lacl->mapped_lun);
1292 	if (!deve) {
1293 		rcu_read_unlock();
1294 		return -ENODEV;
1295 	}
1296 	/* scsiAttIntrPortAuthIntrIdx */
1297 	ret = snprintf(page, PAGE_SIZE, "%u\n", nacl->acl_index);
1298 	rcu_read_unlock();
1299 	return ret;
1300 }
1301 
1302 static ssize_t target_stat_iport_port_ident_show(struct config_item *item,
1303 		char *page)
1304 {
1305 	struct se_lun_acl *lacl = iport_to_lacl(item);
1306 	struct se_node_acl *nacl = lacl->se_lun_nacl;
1307 	struct se_session *se_sess;
1308 	struct se_portal_group *tpg;
1309 	ssize_t ret;
1310 	unsigned char buf[64];
1311 
1312 	spin_lock_irq(&nacl->nacl_sess_lock);
1313 	se_sess = nacl->nacl_sess;
1314 	if (!se_sess) {
1315 		spin_unlock_irq(&nacl->nacl_sess_lock);
1316 		return -ENODEV;
1317 	}
1318 
1319 	tpg = nacl->se_tpg;
1320 	/* scsiAttIntrPortName+scsiAttIntrPortIdentifier */
1321 	memset(buf, 0, 64);
1322 	if (tpg->se_tpg_tfo->sess_get_initiator_sid != NULL)
1323 		tpg->se_tpg_tfo->sess_get_initiator_sid(se_sess, buf, 64);
1324 
1325 	ret = snprintf(page, PAGE_SIZE, "%s+i+%s\n", nacl->initiatorname, buf);
1326 	spin_unlock_irq(&nacl->nacl_sess_lock);
1327 	return ret;
1328 }
1329 
1330 CONFIGFS_ATTR_RO(target_stat_iport_, inst);
1331 CONFIGFS_ATTR_RO(target_stat_iport_, dev);
1332 CONFIGFS_ATTR_RO(target_stat_iport_, port);
1333 CONFIGFS_ATTR_RO(target_stat_iport_, indx);
1334 CONFIGFS_ATTR_RO(target_stat_iport_, port_auth_indx);
1335 CONFIGFS_ATTR_RO(target_stat_iport_, port_ident);
1336 
1337 static struct configfs_attribute *target_stat_scsi_ath_intr_port_attrs[] = {
1338 	&target_stat_iport_attr_inst,
1339 	&target_stat_iport_attr_dev,
1340 	&target_stat_iport_attr_port,
1341 	&target_stat_iport_attr_indx,
1342 	&target_stat_iport_attr_port_auth_indx,
1343 	&target_stat_iport_attr_port_ident,
1344 	NULL,
1345 };
1346 
1347 static const struct config_item_type target_stat_scsi_att_intr_port_cit = {
1348 	.ct_attrs		= target_stat_scsi_ath_intr_port_attrs,
1349 	.ct_owner		= THIS_MODULE,
1350 };
1351 
1352 /*
1353  * Called from target_core_fabric_configfs.c:target_fabric_make_mappedlun() to setup
1354  * the target MappedLUN statistics groups + configfs CITs located in target_core_stat.c
1355  */
1356 void target_stat_setup_mappedlun_default_groups(struct se_lun_acl *lacl)
1357 {
1358 	config_group_init_type_name(&lacl->ml_stat_grps.scsi_auth_intr_group,
1359 			"scsi_auth_intr", &target_stat_scsi_auth_intr_cit);
1360 	configfs_add_default_group(&lacl->ml_stat_grps.scsi_auth_intr_group,
1361 			&lacl->ml_stat_grps.stat_group);
1362 
1363 	config_group_init_type_name(&lacl->ml_stat_grps.scsi_att_intr_port_group,
1364 			"scsi_att_intr_port", &target_stat_scsi_att_intr_port_cit);
1365 	configfs_add_default_group(&lacl->ml_stat_grps.scsi_att_intr_port_group,
1366 			&lacl->ml_stat_grps.stat_group);
1367 }
1368