xref: /openbmc/linux/drivers/s390/scsi/zfcp_sysfs.c (revision dc6a81c3)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * zfcp device driver
4  *
5  * sysfs attributes.
6  *
7  * Copyright IBM Corp. 2008, 2010
8  */
9 
10 #define KMSG_COMPONENT "zfcp"
11 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12 
13 #include <linux/slab.h>
14 #include "zfcp_diag.h"
15 #include "zfcp_ext.h"
16 
17 #define ZFCP_DEV_ATTR(_feat, _name, _mode, _show, _store) \
18 struct device_attribute dev_attr_##_feat##_##_name = __ATTR(_name, _mode,\
19 							    _show, _store)
20 #define ZFCP_DEFINE_ATTR(_feat_def, _feat, _name, _format, _value)	       \
21 static ssize_t zfcp_sysfs_##_feat##_##_name##_show(struct device *dev,	       \
22 						   struct device_attribute *at,\
23 						   char *buf)		       \
24 {									       \
25 	struct _feat_def *_feat = container_of(dev, struct _feat_def, dev);    \
26 									       \
27 	return sprintf(buf, _format, _value);				       \
28 }									       \
29 static ZFCP_DEV_ATTR(_feat, _name, S_IRUGO,				       \
30 		     zfcp_sysfs_##_feat##_##_name##_show, NULL);
31 
32 #define ZFCP_DEFINE_ATTR_CONST(_feat, _name, _format, _value)		       \
33 static ssize_t zfcp_sysfs_##_feat##_##_name##_show(struct device *dev,	       \
34 						   struct device_attribute *at,\
35 						   char *buf)		       \
36 {									       \
37 	return sprintf(buf, _format, _value);				       \
38 }									       \
39 static ZFCP_DEV_ATTR(_feat, _name, S_IRUGO,				       \
40 		     zfcp_sysfs_##_feat##_##_name##_show, NULL);
41 
42 #define ZFCP_DEFINE_A_ATTR(_name, _format, _value)			     \
43 static ssize_t zfcp_sysfs_adapter_##_name##_show(struct device *dev,	     \
44 						 struct device_attribute *at,\
45 						 char *buf)		     \
46 {									     \
47 	struct ccw_device *cdev = to_ccwdev(dev);			     \
48 	struct zfcp_adapter *adapter = zfcp_ccw_adapter_by_cdev(cdev);	     \
49 	int i;								     \
50 									     \
51 	if (!adapter)							     \
52 		return -ENODEV;						     \
53 									     \
54 	i = sprintf(buf, _format, _value);				     \
55 	zfcp_ccw_adapter_put(adapter);					     \
56 	return i;							     \
57 }									     \
58 static ZFCP_DEV_ATTR(adapter, _name, S_IRUGO,				     \
59 		     zfcp_sysfs_adapter_##_name##_show, NULL);
60 
61 ZFCP_DEFINE_A_ATTR(status, "0x%08x\n", atomic_read(&adapter->status));
62 ZFCP_DEFINE_A_ATTR(peer_wwnn, "0x%016llx\n",
63 		   (unsigned long long) adapter->peer_wwnn);
64 ZFCP_DEFINE_A_ATTR(peer_wwpn, "0x%016llx\n",
65 		   (unsigned long long) adapter->peer_wwpn);
66 ZFCP_DEFINE_A_ATTR(peer_d_id, "0x%06x\n", adapter->peer_d_id);
67 ZFCP_DEFINE_A_ATTR(card_version, "0x%04x\n", adapter->hydra_version);
68 ZFCP_DEFINE_A_ATTR(lic_version, "0x%08x\n", adapter->fsf_lic_version);
69 ZFCP_DEFINE_A_ATTR(hardware_version, "0x%08x\n", adapter->hardware_version);
70 ZFCP_DEFINE_A_ATTR(in_recovery, "%d\n", (atomic_read(&adapter->status) &
71 					 ZFCP_STATUS_COMMON_ERP_INUSE) != 0);
72 
73 ZFCP_DEFINE_ATTR(zfcp_port, port, status, "0x%08x\n",
74 		 atomic_read(&port->status));
75 ZFCP_DEFINE_ATTR(zfcp_port, port, in_recovery, "%d\n",
76 		 (atomic_read(&port->status) &
77 		  ZFCP_STATUS_COMMON_ERP_INUSE) != 0);
78 ZFCP_DEFINE_ATTR_CONST(port, access_denied, "%d\n", 0);
79 
80 ZFCP_DEFINE_ATTR(zfcp_unit, unit, status, "0x%08x\n",
81 		 zfcp_unit_sdev_status(unit));
82 ZFCP_DEFINE_ATTR(zfcp_unit, unit, in_recovery, "%d\n",
83 		 (zfcp_unit_sdev_status(unit) &
84 		  ZFCP_STATUS_COMMON_ERP_INUSE) != 0);
85 ZFCP_DEFINE_ATTR(zfcp_unit, unit, access_denied, "%d\n",
86 		 (zfcp_unit_sdev_status(unit) &
87 		  ZFCP_STATUS_COMMON_ACCESS_DENIED) != 0);
88 ZFCP_DEFINE_ATTR_CONST(unit, access_shared, "%d\n", 0);
89 ZFCP_DEFINE_ATTR_CONST(unit, access_readonly, "%d\n", 0);
90 
91 static ssize_t zfcp_sysfs_port_failed_show(struct device *dev,
92 					   struct device_attribute *attr,
93 					   char *buf)
94 {
95 	struct zfcp_port *port = container_of(dev, struct zfcp_port, dev);
96 
97 	if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
98 		return sprintf(buf, "1\n");
99 
100 	return sprintf(buf, "0\n");
101 }
102 
103 static ssize_t zfcp_sysfs_port_failed_store(struct device *dev,
104 					    struct device_attribute *attr,
105 					    const char *buf, size_t count)
106 {
107 	struct zfcp_port *port = container_of(dev, struct zfcp_port, dev);
108 	unsigned long val;
109 
110 	if (kstrtoul(buf, 0, &val) || val != 0)
111 		return -EINVAL;
112 
113 	zfcp_erp_set_port_status(port, ZFCP_STATUS_COMMON_RUNNING);
114 	zfcp_erp_port_reopen(port, ZFCP_STATUS_COMMON_ERP_FAILED, "sypfai2");
115 	zfcp_erp_wait(port->adapter);
116 
117 	return count;
118 }
119 static ZFCP_DEV_ATTR(port, failed, S_IWUSR | S_IRUGO,
120 		     zfcp_sysfs_port_failed_show,
121 		     zfcp_sysfs_port_failed_store);
122 
123 static ssize_t zfcp_sysfs_unit_failed_show(struct device *dev,
124 					   struct device_attribute *attr,
125 					   char *buf)
126 {
127 	struct zfcp_unit *unit = container_of(dev, struct zfcp_unit, dev);
128 	struct scsi_device *sdev;
129 	unsigned int status, failed = 1;
130 
131 	sdev = zfcp_unit_sdev(unit);
132 	if (sdev) {
133 		status = atomic_read(&sdev_to_zfcp(sdev)->status);
134 		failed = status & ZFCP_STATUS_COMMON_ERP_FAILED ? 1 : 0;
135 		scsi_device_put(sdev);
136 	}
137 
138 	return sprintf(buf, "%d\n", failed);
139 }
140 
141 static ssize_t zfcp_sysfs_unit_failed_store(struct device *dev,
142 					    struct device_attribute *attr,
143 					    const char *buf, size_t count)
144 {
145 	struct zfcp_unit *unit = container_of(dev, struct zfcp_unit, dev);
146 	unsigned long val;
147 	struct scsi_device *sdev;
148 
149 	if (kstrtoul(buf, 0, &val) || val != 0)
150 		return -EINVAL;
151 
152 	sdev = zfcp_unit_sdev(unit);
153 	if (sdev) {
154 		zfcp_erp_set_lun_status(sdev, ZFCP_STATUS_COMMON_RUNNING);
155 		zfcp_erp_lun_reopen(sdev, ZFCP_STATUS_COMMON_ERP_FAILED,
156 				    "syufai2");
157 		zfcp_erp_wait(unit->port->adapter);
158 	} else
159 		zfcp_unit_scsi_scan(unit);
160 
161 	return count;
162 }
163 static ZFCP_DEV_ATTR(unit, failed, S_IWUSR | S_IRUGO,
164 		     zfcp_sysfs_unit_failed_show,
165 		     zfcp_sysfs_unit_failed_store);
166 
167 static ssize_t zfcp_sysfs_adapter_failed_show(struct device *dev,
168 					      struct device_attribute *attr,
169 					      char *buf)
170 {
171 	struct ccw_device *cdev = to_ccwdev(dev);
172 	struct zfcp_adapter *adapter = zfcp_ccw_adapter_by_cdev(cdev);
173 	int i;
174 
175 	if (!adapter)
176 		return -ENODEV;
177 
178 	if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_ERP_FAILED)
179 		i = sprintf(buf, "1\n");
180 	else
181 		i = sprintf(buf, "0\n");
182 
183 	zfcp_ccw_adapter_put(adapter);
184 	return i;
185 }
186 
187 static ssize_t zfcp_sysfs_adapter_failed_store(struct device *dev,
188 					       struct device_attribute *attr,
189 					       const char *buf, size_t count)
190 {
191 	struct ccw_device *cdev = to_ccwdev(dev);
192 	struct zfcp_adapter *adapter = zfcp_ccw_adapter_by_cdev(cdev);
193 	unsigned long val;
194 	int retval = 0;
195 
196 	if (!adapter)
197 		return -ENODEV;
198 
199 	if (kstrtoul(buf, 0, &val) || val != 0) {
200 		retval = -EINVAL;
201 		goto out;
202 	}
203 
204 	zfcp_erp_adapter_reset_sync(adapter, "syafai2");
205 out:
206 	zfcp_ccw_adapter_put(adapter);
207 	return retval ? retval : (ssize_t) count;
208 }
209 static ZFCP_DEV_ATTR(adapter, failed, S_IWUSR | S_IRUGO,
210 		     zfcp_sysfs_adapter_failed_show,
211 		     zfcp_sysfs_adapter_failed_store);
212 
213 static ssize_t zfcp_sysfs_port_rescan_store(struct device *dev,
214 					    struct device_attribute *attr,
215 					    const char *buf, size_t count)
216 {
217 	struct ccw_device *cdev = to_ccwdev(dev);
218 	struct zfcp_adapter *adapter = zfcp_ccw_adapter_by_cdev(cdev);
219 
220 	if (!adapter)
221 		return -ENODEV;
222 
223 	/*
224 	 * Users wish is our command: immediately schedule and flush a
225 	 * worker to conduct a synchronous port scan, that is, neither
226 	 * a random delay nor a rate limit is applied here.
227 	 */
228 	queue_delayed_work(adapter->work_queue, &adapter->scan_work, 0);
229 	flush_delayed_work(&adapter->scan_work);
230 	zfcp_ccw_adapter_put(adapter);
231 
232 	return (ssize_t) count;
233 }
234 static ZFCP_DEV_ATTR(adapter, port_rescan, S_IWUSR, NULL,
235 		     zfcp_sysfs_port_rescan_store);
236 
237 DEFINE_MUTEX(zfcp_sysfs_port_units_mutex);
238 
239 static void zfcp_sysfs_port_set_removing(struct zfcp_port *const port)
240 {
241 	lockdep_assert_held(&zfcp_sysfs_port_units_mutex);
242 	atomic_set(&port->units, -1);
243 }
244 
245 bool zfcp_sysfs_port_is_removing(const struct zfcp_port *const port)
246 {
247 	lockdep_assert_held(&zfcp_sysfs_port_units_mutex);
248 	return atomic_read(&port->units) == -1;
249 }
250 
251 static bool zfcp_sysfs_port_in_use(struct zfcp_port *const port)
252 {
253 	struct zfcp_adapter *const adapter = port->adapter;
254 	unsigned long flags;
255 	struct scsi_device *sdev;
256 	bool in_use = true;
257 
258 	mutex_lock(&zfcp_sysfs_port_units_mutex);
259 	if (atomic_read(&port->units) > 0)
260 		goto unlock_port_units_mutex; /* zfcp_unit(s) under port */
261 
262 	spin_lock_irqsave(adapter->scsi_host->host_lock, flags);
263 	__shost_for_each_device(sdev, adapter->scsi_host) {
264 		const struct zfcp_scsi_dev *zsdev = sdev_to_zfcp(sdev);
265 
266 		if (sdev->sdev_state == SDEV_DEL ||
267 		    sdev->sdev_state == SDEV_CANCEL)
268 			continue;
269 		if (zsdev->port != port)
270 			continue;
271 		/* alive scsi_device under port of interest */
272 		goto unlock_host_lock;
273 	}
274 
275 	/* port is about to be removed, so no more unit_add or slave_alloc */
276 	zfcp_sysfs_port_set_removing(port);
277 	in_use = false;
278 
279 unlock_host_lock:
280 	spin_unlock_irqrestore(adapter->scsi_host->host_lock, flags);
281 unlock_port_units_mutex:
282 	mutex_unlock(&zfcp_sysfs_port_units_mutex);
283 	return in_use;
284 }
285 
286 static ssize_t zfcp_sysfs_port_remove_store(struct device *dev,
287 					    struct device_attribute *attr,
288 					    const char *buf, size_t count)
289 {
290 	struct ccw_device *cdev = to_ccwdev(dev);
291 	struct zfcp_adapter *adapter = zfcp_ccw_adapter_by_cdev(cdev);
292 	struct zfcp_port *port;
293 	u64 wwpn;
294 	int retval = -EINVAL;
295 
296 	if (!adapter)
297 		return -ENODEV;
298 
299 	if (kstrtoull(buf, 0, (unsigned long long *) &wwpn))
300 		goto out;
301 
302 	port = zfcp_get_port_by_wwpn(adapter, wwpn);
303 	if (!port)
304 		goto out;
305 	else
306 		retval = 0;
307 
308 	if (zfcp_sysfs_port_in_use(port)) {
309 		retval = -EBUSY;
310 		put_device(&port->dev); /* undo zfcp_get_port_by_wwpn() */
311 		goto out;
312 	}
313 
314 	write_lock_irq(&adapter->port_list_lock);
315 	list_del(&port->list);
316 	write_unlock_irq(&adapter->port_list_lock);
317 
318 	put_device(&port->dev);
319 
320 	zfcp_erp_port_shutdown(port, 0, "syprs_1");
321 	device_unregister(&port->dev);
322  out:
323 	zfcp_ccw_adapter_put(adapter);
324 	return retval ? retval : (ssize_t) count;
325 }
326 static ZFCP_DEV_ATTR(adapter, port_remove, S_IWUSR, NULL,
327 		     zfcp_sysfs_port_remove_store);
328 
329 static ssize_t
330 zfcp_sysfs_adapter_diag_max_age_show(struct device *dev,
331 				     struct device_attribute *attr, char *buf)
332 {
333 	struct zfcp_adapter *adapter = zfcp_ccw_adapter_by_cdev(to_ccwdev(dev));
334 	ssize_t rc;
335 
336 	if (!adapter)
337 		return -ENODEV;
338 
339 	/* ceil(log(2^64 - 1) / log(10)) = 20 */
340 	rc = scnprintf(buf, 20 + 2, "%lu\n", adapter->diagnostics->max_age);
341 
342 	zfcp_ccw_adapter_put(adapter);
343 	return rc;
344 }
345 
346 static ssize_t
347 zfcp_sysfs_adapter_diag_max_age_store(struct device *dev,
348 				      struct device_attribute *attr,
349 				      const char *buf, size_t count)
350 {
351 	struct zfcp_adapter *adapter = zfcp_ccw_adapter_by_cdev(to_ccwdev(dev));
352 	unsigned long max_age;
353 	ssize_t rc;
354 
355 	if (!adapter)
356 		return -ENODEV;
357 
358 	rc = kstrtoul(buf, 10, &max_age);
359 	if (rc != 0)
360 		goto out;
361 
362 	adapter->diagnostics->max_age = max_age;
363 
364 	rc = count;
365 out:
366 	zfcp_ccw_adapter_put(adapter);
367 	return rc;
368 }
369 static ZFCP_DEV_ATTR(adapter, diag_max_age, 0644,
370 		     zfcp_sysfs_adapter_diag_max_age_show,
371 		     zfcp_sysfs_adapter_diag_max_age_store);
372 
373 static struct attribute *zfcp_adapter_attrs[] = {
374 	&dev_attr_adapter_failed.attr,
375 	&dev_attr_adapter_in_recovery.attr,
376 	&dev_attr_adapter_port_remove.attr,
377 	&dev_attr_adapter_port_rescan.attr,
378 	&dev_attr_adapter_peer_wwnn.attr,
379 	&dev_attr_adapter_peer_wwpn.attr,
380 	&dev_attr_adapter_peer_d_id.attr,
381 	&dev_attr_adapter_card_version.attr,
382 	&dev_attr_adapter_lic_version.attr,
383 	&dev_attr_adapter_status.attr,
384 	&dev_attr_adapter_hardware_version.attr,
385 	&dev_attr_adapter_diag_max_age.attr,
386 	NULL
387 };
388 
389 struct attribute_group zfcp_sysfs_adapter_attrs = {
390 	.attrs = zfcp_adapter_attrs,
391 };
392 
393 static ssize_t zfcp_sysfs_unit_add_store(struct device *dev,
394 					 struct device_attribute *attr,
395 					 const char *buf, size_t count)
396 {
397 	struct zfcp_port *port = container_of(dev, struct zfcp_port, dev);
398 	u64 fcp_lun;
399 	int retval;
400 
401 	if (kstrtoull(buf, 0, (unsigned long long *) &fcp_lun))
402 		return -EINVAL;
403 
404 	retval = zfcp_unit_add(port, fcp_lun);
405 	if (retval)
406 		return retval;
407 
408 	return count;
409 }
410 static DEVICE_ATTR(unit_add, S_IWUSR, NULL, zfcp_sysfs_unit_add_store);
411 
412 static ssize_t zfcp_sysfs_unit_remove_store(struct device *dev,
413 					    struct device_attribute *attr,
414 					    const char *buf, size_t count)
415 {
416 	struct zfcp_port *port = container_of(dev, struct zfcp_port, dev);
417 	u64 fcp_lun;
418 
419 	if (kstrtoull(buf, 0, (unsigned long long *) &fcp_lun))
420 		return -EINVAL;
421 
422 	if (zfcp_unit_remove(port, fcp_lun))
423 		return -EINVAL;
424 
425 	return count;
426 }
427 static DEVICE_ATTR(unit_remove, S_IWUSR, NULL, zfcp_sysfs_unit_remove_store);
428 
429 static struct attribute *zfcp_port_attrs[] = {
430 	&dev_attr_unit_add.attr,
431 	&dev_attr_unit_remove.attr,
432 	&dev_attr_port_failed.attr,
433 	&dev_attr_port_in_recovery.attr,
434 	&dev_attr_port_status.attr,
435 	&dev_attr_port_access_denied.attr,
436 	NULL
437 };
438 static struct attribute_group zfcp_port_attr_group = {
439 	.attrs = zfcp_port_attrs,
440 };
441 const struct attribute_group *zfcp_port_attr_groups[] = {
442 	&zfcp_port_attr_group,
443 	NULL,
444 };
445 
446 static struct attribute *zfcp_unit_attrs[] = {
447 	&dev_attr_unit_failed.attr,
448 	&dev_attr_unit_in_recovery.attr,
449 	&dev_attr_unit_status.attr,
450 	&dev_attr_unit_access_denied.attr,
451 	&dev_attr_unit_access_shared.attr,
452 	&dev_attr_unit_access_readonly.attr,
453 	NULL
454 };
455 static struct attribute_group zfcp_unit_attr_group = {
456 	.attrs = zfcp_unit_attrs,
457 };
458 const struct attribute_group *zfcp_unit_attr_groups[] = {
459 	&zfcp_unit_attr_group,
460 	NULL,
461 };
462 
463 #define ZFCP_DEFINE_LATENCY_ATTR(_name) 				\
464 static ssize_t								\
465 zfcp_sysfs_unit_##_name##_latency_show(struct device *dev,		\
466 				       struct device_attribute *attr,	\
467 				       char *buf) {			\
468 	struct scsi_device *sdev = to_scsi_device(dev);			\
469 	struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);		\
470 	struct zfcp_latencies *lat = &zfcp_sdev->latencies;		\
471 	struct zfcp_adapter *adapter = zfcp_sdev->port->adapter;	\
472 	unsigned long long fsum, fmin, fmax, csum, cmin, cmax, cc;	\
473 									\
474 	spin_lock_bh(&lat->lock);					\
475 	fsum = lat->_name.fabric.sum * adapter->timer_ticks;		\
476 	fmin = lat->_name.fabric.min * adapter->timer_ticks;		\
477 	fmax = lat->_name.fabric.max * adapter->timer_ticks;		\
478 	csum = lat->_name.channel.sum * adapter->timer_ticks;		\
479 	cmin = lat->_name.channel.min * adapter->timer_ticks;		\
480 	cmax = lat->_name.channel.max * adapter->timer_ticks;		\
481 	cc  = lat->_name.counter;					\
482 	spin_unlock_bh(&lat->lock);					\
483 									\
484 	do_div(fsum, 1000);						\
485 	do_div(fmin, 1000);						\
486 	do_div(fmax, 1000);						\
487 	do_div(csum, 1000);						\
488 	do_div(cmin, 1000);						\
489 	do_div(cmax, 1000);						\
490 									\
491 	return sprintf(buf, "%llu %llu %llu %llu %llu %llu %llu\n",	\
492 		       fmin, fmax, fsum, cmin, cmax, csum, cc); 	\
493 }									\
494 static ssize_t								\
495 zfcp_sysfs_unit_##_name##_latency_store(struct device *dev,		\
496 					struct device_attribute *attr,	\
497 					const char *buf, size_t count)	\
498 {									\
499 	struct scsi_device *sdev = to_scsi_device(dev);			\
500 	struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);		\
501 	struct zfcp_latencies *lat = &zfcp_sdev->latencies;		\
502 	unsigned long flags;						\
503 									\
504 	spin_lock_irqsave(&lat->lock, flags);				\
505 	lat->_name.fabric.sum = 0;					\
506 	lat->_name.fabric.min = 0xFFFFFFFF;				\
507 	lat->_name.fabric.max = 0;					\
508 	lat->_name.channel.sum = 0;					\
509 	lat->_name.channel.min = 0xFFFFFFFF;				\
510 	lat->_name.channel.max = 0;					\
511 	lat->_name.counter = 0;						\
512 	spin_unlock_irqrestore(&lat->lock, flags);			\
513 									\
514 	return (ssize_t) count;						\
515 }									\
516 static DEVICE_ATTR(_name##_latency, S_IWUSR | S_IRUGO,			\
517 		   zfcp_sysfs_unit_##_name##_latency_show,		\
518 		   zfcp_sysfs_unit_##_name##_latency_store);
519 
520 ZFCP_DEFINE_LATENCY_ATTR(read);
521 ZFCP_DEFINE_LATENCY_ATTR(write);
522 ZFCP_DEFINE_LATENCY_ATTR(cmd);
523 
524 #define ZFCP_DEFINE_SCSI_ATTR(_name, _format, _value)			\
525 static ssize_t zfcp_sysfs_scsi_##_name##_show(struct device *dev,	\
526 					      struct device_attribute *attr,\
527 					      char *buf)                 \
528 {                                                                        \
529 	struct scsi_device *sdev = to_scsi_device(dev);			 \
530 	struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);		 \
531 									 \
532 	return sprintf(buf, _format, _value);                            \
533 }                                                                        \
534 static DEVICE_ATTR(_name, S_IRUGO, zfcp_sysfs_scsi_##_name##_show, NULL);
535 
536 ZFCP_DEFINE_SCSI_ATTR(hba_id, "%s\n",
537 		      dev_name(&zfcp_sdev->port->adapter->ccw_device->dev));
538 ZFCP_DEFINE_SCSI_ATTR(wwpn, "0x%016llx\n",
539 		      (unsigned long long) zfcp_sdev->port->wwpn);
540 
541 static ssize_t zfcp_sysfs_scsi_fcp_lun_show(struct device *dev,
542 					    struct device_attribute *attr,
543 					    char *buf)
544 {
545 	struct scsi_device *sdev = to_scsi_device(dev);
546 
547 	return sprintf(buf, "0x%016llx\n", zfcp_scsi_dev_lun(sdev));
548 }
549 static DEVICE_ATTR(fcp_lun, S_IRUGO, zfcp_sysfs_scsi_fcp_lun_show, NULL);
550 
551 ZFCP_DEFINE_SCSI_ATTR(zfcp_access_denied, "%d\n",
552 		      (atomic_read(&zfcp_sdev->status) &
553 		       ZFCP_STATUS_COMMON_ACCESS_DENIED) != 0);
554 
555 static ssize_t zfcp_sysfs_scsi_zfcp_failed_show(struct device *dev,
556 					   struct device_attribute *attr,
557 					   char *buf)
558 {
559 	struct scsi_device *sdev = to_scsi_device(dev);
560 	unsigned int status = atomic_read(&sdev_to_zfcp(sdev)->status);
561 	unsigned int failed = status & ZFCP_STATUS_COMMON_ERP_FAILED ? 1 : 0;
562 
563 	return sprintf(buf, "%d\n", failed);
564 }
565 
566 static ssize_t zfcp_sysfs_scsi_zfcp_failed_store(struct device *dev,
567 					    struct device_attribute *attr,
568 					    const char *buf, size_t count)
569 {
570 	struct scsi_device *sdev = to_scsi_device(dev);
571 	unsigned long val;
572 
573 	if (kstrtoul(buf, 0, &val) || val != 0)
574 		return -EINVAL;
575 
576 	zfcp_erp_set_lun_status(sdev, ZFCP_STATUS_COMMON_RUNNING);
577 	zfcp_erp_lun_reopen(sdev, ZFCP_STATUS_COMMON_ERP_FAILED,
578 			    "syufai3");
579 	zfcp_erp_wait(sdev_to_zfcp(sdev)->port->adapter);
580 
581 	return count;
582 }
583 static DEVICE_ATTR(zfcp_failed, S_IWUSR | S_IRUGO,
584 		   zfcp_sysfs_scsi_zfcp_failed_show,
585 		   zfcp_sysfs_scsi_zfcp_failed_store);
586 
587 ZFCP_DEFINE_SCSI_ATTR(zfcp_in_recovery, "%d\n",
588 		      (atomic_read(&zfcp_sdev->status) &
589 		       ZFCP_STATUS_COMMON_ERP_INUSE) != 0);
590 
591 ZFCP_DEFINE_SCSI_ATTR(zfcp_status, "0x%08x\n",
592 		      atomic_read(&zfcp_sdev->status));
593 
594 struct device_attribute *zfcp_sysfs_sdev_attrs[] = {
595 	&dev_attr_fcp_lun,
596 	&dev_attr_wwpn,
597 	&dev_attr_hba_id,
598 	&dev_attr_read_latency,
599 	&dev_attr_write_latency,
600 	&dev_attr_cmd_latency,
601 	&dev_attr_zfcp_access_denied,
602 	&dev_attr_zfcp_failed,
603 	&dev_attr_zfcp_in_recovery,
604 	&dev_attr_zfcp_status,
605 	NULL
606 };
607 
608 static ssize_t zfcp_sysfs_adapter_util_show(struct device *dev,
609 					    struct device_attribute *attr,
610 					    char *buf)
611 {
612 	struct Scsi_Host *scsi_host = dev_to_shost(dev);
613 	struct fsf_qtcb_bottom_port *qtcb_port;
614 	struct zfcp_adapter *adapter;
615 	int retval;
616 
617 	adapter = (struct zfcp_adapter *) scsi_host->hostdata[0];
618 	if (!(adapter->adapter_features & FSF_FEATURE_MEASUREMENT_DATA))
619 		return -EOPNOTSUPP;
620 
621 	qtcb_port = kzalloc(sizeof(struct fsf_qtcb_bottom_port), GFP_KERNEL);
622 	if (!qtcb_port)
623 		return -ENOMEM;
624 
625 	retval = zfcp_fsf_exchange_port_data_sync(adapter->qdio, qtcb_port);
626 	if (retval == 0 || retval == -EAGAIN)
627 		retval = sprintf(buf, "%u %u %u\n", qtcb_port->cp_util,
628 				 qtcb_port->cb_util, qtcb_port->a_util);
629 	kfree(qtcb_port);
630 	return retval;
631 }
632 static DEVICE_ATTR(utilization, S_IRUGO, zfcp_sysfs_adapter_util_show, NULL);
633 
634 static int zfcp_sysfs_adapter_ex_config(struct device *dev,
635 					struct fsf_statistics_info *stat_inf)
636 {
637 	struct Scsi_Host *scsi_host = dev_to_shost(dev);
638 	struct fsf_qtcb_bottom_config *qtcb_config;
639 	struct zfcp_adapter *adapter;
640 	int retval;
641 
642 	adapter = (struct zfcp_adapter *) scsi_host->hostdata[0];
643 	if (!(adapter->adapter_features & FSF_FEATURE_MEASUREMENT_DATA))
644 		return -EOPNOTSUPP;
645 
646 	qtcb_config = kzalloc(sizeof(struct fsf_qtcb_bottom_config),
647 			      GFP_KERNEL);
648 	if (!qtcb_config)
649 		return -ENOMEM;
650 
651 	retval = zfcp_fsf_exchange_config_data_sync(adapter->qdio, qtcb_config);
652 	if (retval == 0 || retval == -EAGAIN)
653 		*stat_inf = qtcb_config->stat_info;
654 
655 	kfree(qtcb_config);
656 	return retval;
657 }
658 
659 #define ZFCP_SHOST_ATTR(_name, _format, _arg...)			\
660 static ssize_t zfcp_sysfs_adapter_##_name##_show(struct device *dev,	\
661 						 struct device_attribute *attr,\
662 						 char *buf)		\
663 {									\
664 	struct fsf_statistics_info stat_info;				\
665 	int retval;							\
666 									\
667 	retval = zfcp_sysfs_adapter_ex_config(dev, &stat_info);		\
668 	if (retval)							\
669 		return retval;						\
670 									\
671 	return sprintf(buf, _format, ## _arg);				\
672 }									\
673 static DEVICE_ATTR(_name, S_IRUGO, zfcp_sysfs_adapter_##_name##_show, NULL);
674 
675 ZFCP_SHOST_ATTR(requests, "%llu %llu %llu\n",
676 		(unsigned long long) stat_info.input_req,
677 		(unsigned long long) stat_info.output_req,
678 		(unsigned long long) stat_info.control_req);
679 
680 ZFCP_SHOST_ATTR(megabytes, "%llu %llu\n",
681 		(unsigned long long) stat_info.input_mb,
682 		(unsigned long long) stat_info.output_mb);
683 
684 ZFCP_SHOST_ATTR(seconds_active, "%llu\n",
685 		(unsigned long long) stat_info.seconds_act);
686 
687 static ssize_t zfcp_sysfs_adapter_q_full_show(struct device *dev,
688 					      struct device_attribute *attr,
689 					      char *buf)
690 {
691 	struct Scsi_Host *scsi_host = class_to_shost(dev);
692 	struct zfcp_qdio *qdio =
693 		((struct zfcp_adapter *) scsi_host->hostdata[0])->qdio;
694 	u64 util;
695 
696 	spin_lock_bh(&qdio->stat_lock);
697 	util = qdio->req_q_util;
698 	spin_unlock_bh(&qdio->stat_lock);
699 
700 	return sprintf(buf, "%d %llu\n", atomic_read(&qdio->req_q_full),
701 		       (unsigned long long)util);
702 }
703 static DEVICE_ATTR(queue_full, S_IRUGO, zfcp_sysfs_adapter_q_full_show, NULL);
704 
705 struct device_attribute *zfcp_sysfs_shost_attrs[] = {
706 	&dev_attr_utilization,
707 	&dev_attr_requests,
708 	&dev_attr_megabytes,
709 	&dev_attr_seconds_active,
710 	&dev_attr_queue_full,
711 	NULL
712 };
713 
714 static ssize_t zfcp_sysfs_adapter_diag_b2b_credit_show(
715 	struct device *dev, struct device_attribute *attr, char *buf)
716 {
717 	struct zfcp_adapter *adapter = zfcp_ccw_adapter_by_cdev(to_ccwdev(dev));
718 	struct zfcp_diag_header *diag_hdr;
719 	struct fc_els_flogi *nsp;
720 	ssize_t rc = -ENOLINK;
721 	unsigned long flags;
722 	unsigned int status;
723 
724 	if (!adapter)
725 		return -ENODEV;
726 
727 	status = atomic_read(&adapter->status);
728 	if (0 == (status & ZFCP_STATUS_COMMON_OPEN) ||
729 	    0 == (status & ZFCP_STATUS_COMMON_UNBLOCKED) ||
730 	    0 != (status & ZFCP_STATUS_COMMON_ERP_FAILED))
731 		goto out;
732 
733 	diag_hdr = &adapter->diagnostics->config_data.header;
734 
735 	rc = zfcp_diag_update_buffer_limited(
736 		adapter, diag_hdr, zfcp_diag_update_config_data_buffer);
737 	if (rc != 0)
738 		goto out;
739 
740 	spin_lock_irqsave(&diag_hdr->access_lock, flags);
741 	/* nport_serv_param doesn't contain the ELS_Command code */
742 	nsp = (struct fc_els_flogi *)((unsigned long)
743 					      adapter->diagnostics->config_data
744 						      .data.nport_serv_param -
745 				      sizeof(u32));
746 
747 	rc = scnprintf(buf, 5 + 2, "%hu\n",
748 		       be16_to_cpu(nsp->fl_csp.sp_bb_cred));
749 	spin_unlock_irqrestore(&diag_hdr->access_lock, flags);
750 
751 out:
752 	zfcp_ccw_adapter_put(adapter);
753 	return rc;
754 }
755 static ZFCP_DEV_ATTR(adapter_diag, b2b_credit, 0400,
756 		     zfcp_sysfs_adapter_diag_b2b_credit_show, NULL);
757 
758 #define ZFCP_DEFINE_DIAG_SFP_ATTR(_name, _qtcb_member, _prtsize, _prtfmt)      \
759 	static ssize_t zfcp_sysfs_adapter_diag_sfp_##_name##_show(	       \
760 		struct device *dev, struct device_attribute *attr, char *buf)  \
761 	{								       \
762 		struct zfcp_adapter *const adapter =			       \
763 			zfcp_ccw_adapter_by_cdev(to_ccwdev(dev));	       \
764 		struct zfcp_diag_header *diag_hdr;			       \
765 		ssize_t rc = -ENOLINK;					       \
766 		unsigned long flags;					       \
767 		unsigned int status;					       \
768 									       \
769 		if (!adapter)						       \
770 			return -ENODEV;					       \
771 									       \
772 		status = atomic_read(&adapter->status);			       \
773 		if (0 == (status & ZFCP_STATUS_COMMON_OPEN) ||		       \
774 		    0 == (status & ZFCP_STATUS_COMMON_UNBLOCKED) ||	       \
775 		    0 != (status & ZFCP_STATUS_COMMON_ERP_FAILED))	       \
776 			goto out;					       \
777 									       \
778 		if (!zfcp_diag_support_sfp(adapter)) {			       \
779 			rc = -EOPNOTSUPP;				       \
780 			goto out;					       \
781 		}							       \
782 									       \
783 		diag_hdr = &adapter->diagnostics->port_data.header;	       \
784 									       \
785 		rc = zfcp_diag_update_buffer_limited(			       \
786 			adapter, diag_hdr, zfcp_diag_update_port_data_buffer); \
787 		if (rc != 0)						       \
788 			goto out;					       \
789 									       \
790 		spin_lock_irqsave(&diag_hdr->access_lock, flags);	       \
791 		rc = scnprintf(						       \
792 			buf, (_prtsize) + 2, _prtfmt "\n",		       \
793 			adapter->diagnostics->port_data.data._qtcb_member);    \
794 		spin_unlock_irqrestore(&diag_hdr->access_lock, flags);	       \
795 									       \
796 	out:								       \
797 		zfcp_ccw_adapter_put(adapter);				       \
798 		return rc;						       \
799 	}								       \
800 	static ZFCP_DEV_ATTR(adapter_diag_sfp, _name, 0400,		       \
801 			     zfcp_sysfs_adapter_diag_sfp_##_name##_show, NULL)
802 
803 ZFCP_DEFINE_DIAG_SFP_ATTR(temperature, temperature, 5, "%hu");
804 ZFCP_DEFINE_DIAG_SFP_ATTR(vcc, vcc, 5, "%hu");
805 ZFCP_DEFINE_DIAG_SFP_ATTR(tx_bias, tx_bias, 5, "%hu");
806 ZFCP_DEFINE_DIAG_SFP_ATTR(tx_power, tx_power, 5, "%hu");
807 ZFCP_DEFINE_DIAG_SFP_ATTR(rx_power, rx_power, 5, "%hu");
808 ZFCP_DEFINE_DIAG_SFP_ATTR(port_tx_type, sfp_flags.port_tx_type, 2, "%hu");
809 ZFCP_DEFINE_DIAG_SFP_ATTR(optical_port, sfp_flags.optical_port, 1, "%hu");
810 ZFCP_DEFINE_DIAG_SFP_ATTR(sfp_invalid, sfp_flags.sfp_invalid, 1, "%hu");
811 ZFCP_DEFINE_DIAG_SFP_ATTR(connector_type, sfp_flags.connector_type, 1, "%hu");
812 ZFCP_DEFINE_DIAG_SFP_ATTR(fec_active, sfp_flags.fec_active, 1, "%hu");
813 
814 static struct attribute *zfcp_sysfs_diag_attrs[] = {
815 	&dev_attr_adapter_diag_sfp_temperature.attr,
816 	&dev_attr_adapter_diag_sfp_vcc.attr,
817 	&dev_attr_adapter_diag_sfp_tx_bias.attr,
818 	&dev_attr_adapter_diag_sfp_tx_power.attr,
819 	&dev_attr_adapter_diag_sfp_rx_power.attr,
820 	&dev_attr_adapter_diag_sfp_port_tx_type.attr,
821 	&dev_attr_adapter_diag_sfp_optical_port.attr,
822 	&dev_attr_adapter_diag_sfp_sfp_invalid.attr,
823 	&dev_attr_adapter_diag_sfp_connector_type.attr,
824 	&dev_attr_adapter_diag_sfp_fec_active.attr,
825 	&dev_attr_adapter_diag_b2b_credit.attr,
826 	NULL,
827 };
828 
829 const struct attribute_group zfcp_sysfs_diag_attr_group = {
830 	.name = "diagnostics",
831 	.attrs = zfcp_sysfs_diag_attrs,
832 };
833