xref: /openbmc/linux/drivers/s390/scsi/zfcp_sysfs.c (revision f97769fd)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * zfcp device driver
4  *
5  * sysfs attributes.
6  *
7  * Copyright IBM Corp. 2008, 2020
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 	int retval = 0;
220 
221 	if (!adapter)
222 		return -ENODEV;
223 
224 	/*
225 	 * If `scsi_host` is missing, we can't schedule `scan_work`, as it
226 	 * makes use of the corresponding fc_host object. But this state is
227 	 * only possible if xconfig/xport data has never completed yet,
228 	 * and we couldn't successfully scan for ports anyway.
229 	 */
230 	if (adapter->scsi_host == NULL) {
231 		retval = -ENODEV;
232 		goto out;
233 	}
234 
235 	/*
236 	 * Users wish is our command: immediately schedule and flush a
237 	 * worker to conduct a synchronous port scan, that is, neither
238 	 * a random delay nor a rate limit is applied here.
239 	 */
240 	queue_delayed_work(adapter->work_queue, &adapter->scan_work, 0);
241 	flush_delayed_work(&adapter->scan_work);
242 out:
243 	zfcp_ccw_adapter_put(adapter);
244 	return retval ? retval : (ssize_t) count;
245 }
246 static ZFCP_DEV_ATTR(adapter, port_rescan, S_IWUSR, NULL,
247 		     zfcp_sysfs_port_rescan_store);
248 
249 DEFINE_MUTEX(zfcp_sysfs_port_units_mutex);
250 
251 static void zfcp_sysfs_port_set_removing(struct zfcp_port *const port)
252 {
253 	lockdep_assert_held(&zfcp_sysfs_port_units_mutex);
254 	atomic_set(&port->units, -1);
255 }
256 
257 bool zfcp_sysfs_port_is_removing(const struct zfcp_port *const port)
258 {
259 	lockdep_assert_held(&zfcp_sysfs_port_units_mutex);
260 	return atomic_read(&port->units) == -1;
261 }
262 
263 static bool zfcp_sysfs_port_in_use(struct zfcp_port *const port)
264 {
265 	struct zfcp_adapter *const adapter = port->adapter;
266 	unsigned long flags;
267 	struct scsi_device *sdev;
268 	bool in_use = true;
269 
270 	mutex_lock(&zfcp_sysfs_port_units_mutex);
271 	if (atomic_read(&port->units) > 0)
272 		goto unlock_port_units_mutex; /* zfcp_unit(s) under port */
273 
274 	spin_lock_irqsave(adapter->scsi_host->host_lock, flags);
275 	__shost_for_each_device(sdev, adapter->scsi_host) {
276 		const struct zfcp_scsi_dev *zsdev = sdev_to_zfcp(sdev);
277 
278 		if (sdev->sdev_state == SDEV_DEL ||
279 		    sdev->sdev_state == SDEV_CANCEL)
280 			continue;
281 		if (zsdev->port != port)
282 			continue;
283 		/* alive scsi_device under port of interest */
284 		goto unlock_host_lock;
285 	}
286 
287 	/* port is about to be removed, so no more unit_add or slave_alloc */
288 	zfcp_sysfs_port_set_removing(port);
289 	in_use = false;
290 
291 unlock_host_lock:
292 	spin_unlock_irqrestore(adapter->scsi_host->host_lock, flags);
293 unlock_port_units_mutex:
294 	mutex_unlock(&zfcp_sysfs_port_units_mutex);
295 	return in_use;
296 }
297 
298 static ssize_t zfcp_sysfs_port_remove_store(struct device *dev,
299 					    struct device_attribute *attr,
300 					    const char *buf, size_t count)
301 {
302 	struct ccw_device *cdev = to_ccwdev(dev);
303 	struct zfcp_adapter *adapter = zfcp_ccw_adapter_by_cdev(cdev);
304 	struct zfcp_port *port;
305 	u64 wwpn;
306 	int retval = -EINVAL;
307 
308 	if (!adapter)
309 		return -ENODEV;
310 
311 	if (kstrtoull(buf, 0, (unsigned long long *) &wwpn))
312 		goto out;
313 
314 	port = zfcp_get_port_by_wwpn(adapter, wwpn);
315 	if (!port)
316 		goto out;
317 	else
318 		retval = 0;
319 
320 	if (zfcp_sysfs_port_in_use(port)) {
321 		retval = -EBUSY;
322 		put_device(&port->dev); /* undo zfcp_get_port_by_wwpn() */
323 		goto out;
324 	}
325 
326 	write_lock_irq(&adapter->port_list_lock);
327 	list_del(&port->list);
328 	write_unlock_irq(&adapter->port_list_lock);
329 
330 	put_device(&port->dev);
331 
332 	zfcp_erp_port_shutdown(port, 0, "syprs_1");
333 	device_unregister(&port->dev);
334  out:
335 	zfcp_ccw_adapter_put(adapter);
336 	return retval ? retval : (ssize_t) count;
337 }
338 static ZFCP_DEV_ATTR(adapter, port_remove, S_IWUSR, NULL,
339 		     zfcp_sysfs_port_remove_store);
340 
341 static ssize_t
342 zfcp_sysfs_adapter_diag_max_age_show(struct device *dev,
343 				     struct device_attribute *attr, char *buf)
344 {
345 	struct zfcp_adapter *adapter = zfcp_ccw_adapter_by_cdev(to_ccwdev(dev));
346 	ssize_t rc;
347 
348 	if (!adapter)
349 		return -ENODEV;
350 
351 	/* ceil(log(2^64 - 1) / log(10)) = 20 */
352 	rc = scnprintf(buf, 20 + 2, "%lu\n", adapter->diagnostics->max_age);
353 
354 	zfcp_ccw_adapter_put(adapter);
355 	return rc;
356 }
357 
358 static ssize_t
359 zfcp_sysfs_adapter_diag_max_age_store(struct device *dev,
360 				      struct device_attribute *attr,
361 				      const char *buf, size_t count)
362 {
363 	struct zfcp_adapter *adapter = zfcp_ccw_adapter_by_cdev(to_ccwdev(dev));
364 	unsigned long max_age;
365 	ssize_t rc;
366 
367 	if (!adapter)
368 		return -ENODEV;
369 
370 	rc = kstrtoul(buf, 10, &max_age);
371 	if (rc != 0)
372 		goto out;
373 
374 	adapter->diagnostics->max_age = max_age;
375 
376 	rc = count;
377 out:
378 	zfcp_ccw_adapter_put(adapter);
379 	return rc;
380 }
381 static ZFCP_DEV_ATTR(adapter, diag_max_age, 0644,
382 		     zfcp_sysfs_adapter_diag_max_age_show,
383 		     zfcp_sysfs_adapter_diag_max_age_store);
384 
385 static ssize_t zfcp_sysfs_adapter_fc_security_show(
386 	struct device *dev, struct device_attribute *attr, char *buf)
387 {
388 	struct ccw_device *cdev = to_ccwdev(dev);
389 	struct zfcp_adapter *adapter = zfcp_ccw_adapter_by_cdev(cdev);
390 	unsigned int status;
391 	int i;
392 
393 	if (!adapter)
394 		return -ENODEV;
395 
396 	/*
397 	 * Adapter status COMMON_OPEN implies xconf data and xport data
398 	 * was done. Adapter FC Endpoint Security capability remains
399 	 * unchanged in case of COMMON_ERP_FAILED (e.g. due to local link
400 	 * down).
401 	 */
402 	status = atomic_read(&adapter->status);
403 	if (0 == (status & ZFCP_STATUS_COMMON_OPEN))
404 		i = sprintf(buf, "unknown\n");
405 	else if (!(adapter->adapter_features & FSF_FEATURE_FC_SECURITY))
406 		i = sprintf(buf, "unsupported\n");
407 	else {
408 		i = zfcp_fsf_scnprint_fc_security(
409 			buf, PAGE_SIZE - 1, adapter->fc_security_algorithms,
410 			ZFCP_FSF_PRINT_FMT_LIST);
411 		i += scnprintf(buf + i, PAGE_SIZE - i, "\n");
412 	}
413 
414 	zfcp_ccw_adapter_put(adapter);
415 	return i;
416 }
417 static ZFCP_DEV_ATTR(adapter, fc_security, S_IRUGO,
418 		     zfcp_sysfs_adapter_fc_security_show,
419 		     NULL);
420 
421 static struct attribute *zfcp_adapter_attrs[] = {
422 	&dev_attr_adapter_failed.attr,
423 	&dev_attr_adapter_in_recovery.attr,
424 	&dev_attr_adapter_port_remove.attr,
425 	&dev_attr_adapter_port_rescan.attr,
426 	&dev_attr_adapter_peer_wwnn.attr,
427 	&dev_attr_adapter_peer_wwpn.attr,
428 	&dev_attr_adapter_peer_d_id.attr,
429 	&dev_attr_adapter_card_version.attr,
430 	&dev_attr_adapter_lic_version.attr,
431 	&dev_attr_adapter_status.attr,
432 	&dev_attr_adapter_hardware_version.attr,
433 	&dev_attr_adapter_diag_max_age.attr,
434 	&dev_attr_adapter_fc_security.attr,
435 	NULL
436 };
437 
438 struct attribute_group zfcp_sysfs_adapter_attrs = {
439 	.attrs = zfcp_adapter_attrs,
440 };
441 
442 static ssize_t zfcp_sysfs_unit_add_store(struct device *dev,
443 					 struct device_attribute *attr,
444 					 const char *buf, size_t count)
445 {
446 	struct zfcp_port *port = container_of(dev, struct zfcp_port, dev);
447 	u64 fcp_lun;
448 	int retval;
449 
450 	if (kstrtoull(buf, 0, (unsigned long long *) &fcp_lun))
451 		return -EINVAL;
452 
453 	retval = zfcp_unit_add(port, fcp_lun);
454 	if (retval)
455 		return retval;
456 
457 	return count;
458 }
459 static DEVICE_ATTR(unit_add, S_IWUSR, NULL, zfcp_sysfs_unit_add_store);
460 
461 static ssize_t zfcp_sysfs_unit_remove_store(struct device *dev,
462 					    struct device_attribute *attr,
463 					    const char *buf, size_t count)
464 {
465 	struct zfcp_port *port = container_of(dev, struct zfcp_port, dev);
466 	u64 fcp_lun;
467 
468 	if (kstrtoull(buf, 0, (unsigned long long *) &fcp_lun))
469 		return -EINVAL;
470 
471 	if (zfcp_unit_remove(port, fcp_lun))
472 		return -EINVAL;
473 
474 	return count;
475 }
476 static DEVICE_ATTR(unit_remove, S_IWUSR, NULL, zfcp_sysfs_unit_remove_store);
477 
478 static ssize_t zfcp_sysfs_port_fc_security_show(struct device *dev,
479 						struct device_attribute *attr,
480 						char *buf)
481 {
482 	struct zfcp_port *port = container_of(dev, struct zfcp_port, dev);
483 	struct zfcp_adapter *adapter = port->adapter;
484 	unsigned int status = atomic_read(&port->status);
485 	int i;
486 
487 	if (0 == (status & ZFCP_STATUS_COMMON_OPEN) ||
488 	    0 == (status & ZFCP_STATUS_COMMON_UNBLOCKED) ||
489 	    0 == (status & ZFCP_STATUS_PORT_PHYS_OPEN) ||
490 	    0 != (status & ZFCP_STATUS_COMMON_ERP_FAILED) ||
491 	    0 != (status & ZFCP_STATUS_COMMON_ACCESS_BOXED))
492 		i = sprintf(buf, "unknown\n");
493 	else if (!(adapter->adapter_features & FSF_FEATURE_FC_SECURITY))
494 		i = sprintf(buf, "unsupported\n");
495 	else {
496 		i = zfcp_fsf_scnprint_fc_security(
497 			buf, PAGE_SIZE - 1, port->connection_info,
498 			ZFCP_FSF_PRINT_FMT_SINGLEITEM);
499 		i += scnprintf(buf + i, PAGE_SIZE - i, "\n");
500 	}
501 
502 	return i;
503 }
504 static ZFCP_DEV_ATTR(port, fc_security, S_IRUGO,
505 		     zfcp_sysfs_port_fc_security_show,
506 		     NULL);
507 
508 static struct attribute *zfcp_port_attrs[] = {
509 	&dev_attr_unit_add.attr,
510 	&dev_attr_unit_remove.attr,
511 	&dev_attr_port_failed.attr,
512 	&dev_attr_port_in_recovery.attr,
513 	&dev_attr_port_status.attr,
514 	&dev_attr_port_access_denied.attr,
515 	&dev_attr_port_fc_security.attr,
516 	NULL
517 };
518 static struct attribute_group zfcp_port_attr_group = {
519 	.attrs = zfcp_port_attrs,
520 };
521 const struct attribute_group *zfcp_port_attr_groups[] = {
522 	&zfcp_port_attr_group,
523 	NULL,
524 };
525 
526 static struct attribute *zfcp_unit_attrs[] = {
527 	&dev_attr_unit_failed.attr,
528 	&dev_attr_unit_in_recovery.attr,
529 	&dev_attr_unit_status.attr,
530 	&dev_attr_unit_access_denied.attr,
531 	&dev_attr_unit_access_shared.attr,
532 	&dev_attr_unit_access_readonly.attr,
533 	NULL
534 };
535 static struct attribute_group zfcp_unit_attr_group = {
536 	.attrs = zfcp_unit_attrs,
537 };
538 const struct attribute_group *zfcp_unit_attr_groups[] = {
539 	&zfcp_unit_attr_group,
540 	NULL,
541 };
542 
543 #define ZFCP_DEFINE_LATENCY_ATTR(_name) 				\
544 static ssize_t								\
545 zfcp_sysfs_unit_##_name##_latency_show(struct device *dev,		\
546 				       struct device_attribute *attr,	\
547 				       char *buf) {			\
548 	struct scsi_device *sdev = to_scsi_device(dev);			\
549 	struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);		\
550 	struct zfcp_latencies *lat = &zfcp_sdev->latencies;		\
551 	struct zfcp_adapter *adapter = zfcp_sdev->port->adapter;	\
552 	unsigned long long fsum, fmin, fmax, csum, cmin, cmax, cc;	\
553 									\
554 	spin_lock_bh(&lat->lock);					\
555 	fsum = lat->_name.fabric.sum * adapter->timer_ticks;		\
556 	fmin = lat->_name.fabric.min * adapter->timer_ticks;		\
557 	fmax = lat->_name.fabric.max * adapter->timer_ticks;		\
558 	csum = lat->_name.channel.sum * adapter->timer_ticks;		\
559 	cmin = lat->_name.channel.min * adapter->timer_ticks;		\
560 	cmax = lat->_name.channel.max * adapter->timer_ticks;		\
561 	cc  = lat->_name.counter;					\
562 	spin_unlock_bh(&lat->lock);					\
563 									\
564 	do_div(fsum, 1000);						\
565 	do_div(fmin, 1000);						\
566 	do_div(fmax, 1000);						\
567 	do_div(csum, 1000);						\
568 	do_div(cmin, 1000);						\
569 	do_div(cmax, 1000);						\
570 									\
571 	return sprintf(buf, "%llu %llu %llu %llu %llu %llu %llu\n",	\
572 		       fmin, fmax, fsum, cmin, cmax, csum, cc); 	\
573 }									\
574 static ssize_t								\
575 zfcp_sysfs_unit_##_name##_latency_store(struct device *dev,		\
576 					struct device_attribute *attr,	\
577 					const char *buf, size_t count)	\
578 {									\
579 	struct scsi_device *sdev = to_scsi_device(dev);			\
580 	struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);		\
581 	struct zfcp_latencies *lat = &zfcp_sdev->latencies;		\
582 	unsigned long flags;						\
583 									\
584 	spin_lock_irqsave(&lat->lock, flags);				\
585 	lat->_name.fabric.sum = 0;					\
586 	lat->_name.fabric.min = 0xFFFFFFFF;				\
587 	lat->_name.fabric.max = 0;					\
588 	lat->_name.channel.sum = 0;					\
589 	lat->_name.channel.min = 0xFFFFFFFF;				\
590 	lat->_name.channel.max = 0;					\
591 	lat->_name.counter = 0;						\
592 	spin_unlock_irqrestore(&lat->lock, flags);			\
593 									\
594 	return (ssize_t) count;						\
595 }									\
596 static DEVICE_ATTR(_name##_latency, S_IWUSR | S_IRUGO,			\
597 		   zfcp_sysfs_unit_##_name##_latency_show,		\
598 		   zfcp_sysfs_unit_##_name##_latency_store);
599 
600 ZFCP_DEFINE_LATENCY_ATTR(read);
601 ZFCP_DEFINE_LATENCY_ATTR(write);
602 ZFCP_DEFINE_LATENCY_ATTR(cmd);
603 
604 #define ZFCP_DEFINE_SCSI_ATTR(_name, _format, _value)			\
605 static ssize_t zfcp_sysfs_scsi_##_name##_show(struct device *dev,	\
606 					      struct device_attribute *attr,\
607 					      char *buf)                 \
608 {                                                                        \
609 	struct scsi_device *sdev = to_scsi_device(dev);			 \
610 	struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);		 \
611 									 \
612 	return sprintf(buf, _format, _value);                            \
613 }                                                                        \
614 static DEVICE_ATTR(_name, S_IRUGO, zfcp_sysfs_scsi_##_name##_show, NULL);
615 
616 ZFCP_DEFINE_SCSI_ATTR(hba_id, "%s\n",
617 		      dev_name(&zfcp_sdev->port->adapter->ccw_device->dev));
618 ZFCP_DEFINE_SCSI_ATTR(wwpn, "0x%016llx\n",
619 		      (unsigned long long) zfcp_sdev->port->wwpn);
620 
621 static ssize_t zfcp_sysfs_scsi_fcp_lun_show(struct device *dev,
622 					    struct device_attribute *attr,
623 					    char *buf)
624 {
625 	struct scsi_device *sdev = to_scsi_device(dev);
626 
627 	return sprintf(buf, "0x%016llx\n", zfcp_scsi_dev_lun(sdev));
628 }
629 static DEVICE_ATTR(fcp_lun, S_IRUGO, zfcp_sysfs_scsi_fcp_lun_show, NULL);
630 
631 ZFCP_DEFINE_SCSI_ATTR(zfcp_access_denied, "%d\n",
632 		      (atomic_read(&zfcp_sdev->status) &
633 		       ZFCP_STATUS_COMMON_ACCESS_DENIED) != 0);
634 
635 static ssize_t zfcp_sysfs_scsi_zfcp_failed_show(struct device *dev,
636 					   struct device_attribute *attr,
637 					   char *buf)
638 {
639 	struct scsi_device *sdev = to_scsi_device(dev);
640 	unsigned int status = atomic_read(&sdev_to_zfcp(sdev)->status);
641 	unsigned int failed = status & ZFCP_STATUS_COMMON_ERP_FAILED ? 1 : 0;
642 
643 	return sprintf(buf, "%d\n", failed);
644 }
645 
646 static ssize_t zfcp_sysfs_scsi_zfcp_failed_store(struct device *dev,
647 					    struct device_attribute *attr,
648 					    const char *buf, size_t count)
649 {
650 	struct scsi_device *sdev = to_scsi_device(dev);
651 	unsigned long val;
652 
653 	if (kstrtoul(buf, 0, &val) || val != 0)
654 		return -EINVAL;
655 
656 	zfcp_erp_set_lun_status(sdev, ZFCP_STATUS_COMMON_RUNNING);
657 	zfcp_erp_lun_reopen(sdev, ZFCP_STATUS_COMMON_ERP_FAILED,
658 			    "syufai3");
659 	zfcp_erp_wait(sdev_to_zfcp(sdev)->port->adapter);
660 
661 	return count;
662 }
663 static DEVICE_ATTR(zfcp_failed, S_IWUSR | S_IRUGO,
664 		   zfcp_sysfs_scsi_zfcp_failed_show,
665 		   zfcp_sysfs_scsi_zfcp_failed_store);
666 
667 ZFCP_DEFINE_SCSI_ATTR(zfcp_in_recovery, "%d\n",
668 		      (atomic_read(&zfcp_sdev->status) &
669 		       ZFCP_STATUS_COMMON_ERP_INUSE) != 0);
670 
671 ZFCP_DEFINE_SCSI_ATTR(zfcp_status, "0x%08x\n",
672 		      atomic_read(&zfcp_sdev->status));
673 
674 struct device_attribute *zfcp_sysfs_sdev_attrs[] = {
675 	&dev_attr_fcp_lun,
676 	&dev_attr_wwpn,
677 	&dev_attr_hba_id,
678 	&dev_attr_read_latency,
679 	&dev_attr_write_latency,
680 	&dev_attr_cmd_latency,
681 	&dev_attr_zfcp_access_denied,
682 	&dev_attr_zfcp_failed,
683 	&dev_attr_zfcp_in_recovery,
684 	&dev_attr_zfcp_status,
685 	NULL
686 };
687 
688 static ssize_t zfcp_sysfs_adapter_util_show(struct device *dev,
689 					    struct device_attribute *attr,
690 					    char *buf)
691 {
692 	struct Scsi_Host *scsi_host = dev_to_shost(dev);
693 	struct fsf_qtcb_bottom_port *qtcb_port;
694 	struct zfcp_adapter *adapter;
695 	int retval;
696 
697 	adapter = (struct zfcp_adapter *) scsi_host->hostdata[0];
698 	if (!(adapter->adapter_features & FSF_FEATURE_MEASUREMENT_DATA))
699 		return -EOPNOTSUPP;
700 
701 	qtcb_port = kzalloc(sizeof(struct fsf_qtcb_bottom_port), GFP_KERNEL);
702 	if (!qtcb_port)
703 		return -ENOMEM;
704 
705 	retval = zfcp_fsf_exchange_port_data_sync(adapter->qdio, qtcb_port);
706 	if (retval == 0 || retval == -EAGAIN)
707 		retval = sprintf(buf, "%u %u %u\n", qtcb_port->cp_util,
708 				 qtcb_port->cb_util, qtcb_port->a_util);
709 	kfree(qtcb_port);
710 	return retval;
711 }
712 static DEVICE_ATTR(utilization, S_IRUGO, zfcp_sysfs_adapter_util_show, NULL);
713 
714 static int zfcp_sysfs_adapter_ex_config(struct device *dev,
715 					struct fsf_statistics_info *stat_inf)
716 {
717 	struct Scsi_Host *scsi_host = dev_to_shost(dev);
718 	struct fsf_qtcb_bottom_config *qtcb_config;
719 	struct zfcp_adapter *adapter;
720 	int retval;
721 
722 	adapter = (struct zfcp_adapter *) scsi_host->hostdata[0];
723 	if (!(adapter->adapter_features & FSF_FEATURE_MEASUREMENT_DATA))
724 		return -EOPNOTSUPP;
725 
726 	qtcb_config = kzalloc(sizeof(struct fsf_qtcb_bottom_config),
727 			      GFP_KERNEL);
728 	if (!qtcb_config)
729 		return -ENOMEM;
730 
731 	retval = zfcp_fsf_exchange_config_data_sync(adapter->qdio, qtcb_config);
732 	if (retval == 0 || retval == -EAGAIN)
733 		*stat_inf = qtcb_config->stat_info;
734 
735 	kfree(qtcb_config);
736 	return retval;
737 }
738 
739 #define ZFCP_SHOST_ATTR(_name, _format, _arg...)			\
740 static ssize_t zfcp_sysfs_adapter_##_name##_show(struct device *dev,	\
741 						 struct device_attribute *attr,\
742 						 char *buf)		\
743 {									\
744 	struct fsf_statistics_info stat_info;				\
745 	int retval;							\
746 									\
747 	retval = zfcp_sysfs_adapter_ex_config(dev, &stat_info);		\
748 	if (retval)							\
749 		return retval;						\
750 									\
751 	return sprintf(buf, _format, ## _arg);				\
752 }									\
753 static DEVICE_ATTR(_name, S_IRUGO, zfcp_sysfs_adapter_##_name##_show, NULL);
754 
755 ZFCP_SHOST_ATTR(requests, "%llu %llu %llu\n",
756 		(unsigned long long) stat_info.input_req,
757 		(unsigned long long) stat_info.output_req,
758 		(unsigned long long) stat_info.control_req);
759 
760 ZFCP_SHOST_ATTR(megabytes, "%llu %llu\n",
761 		(unsigned long long) stat_info.input_mb,
762 		(unsigned long long) stat_info.output_mb);
763 
764 ZFCP_SHOST_ATTR(seconds_active, "%llu\n",
765 		(unsigned long long) stat_info.seconds_act);
766 
767 static ssize_t zfcp_sysfs_adapter_q_full_show(struct device *dev,
768 					      struct device_attribute *attr,
769 					      char *buf)
770 {
771 	struct Scsi_Host *scsi_host = class_to_shost(dev);
772 	struct zfcp_qdio *qdio =
773 		((struct zfcp_adapter *) scsi_host->hostdata[0])->qdio;
774 	u64 util;
775 
776 	spin_lock_bh(&qdio->stat_lock);
777 	util = qdio->req_q_util;
778 	spin_unlock_bh(&qdio->stat_lock);
779 
780 	return sprintf(buf, "%d %llu\n", atomic_read(&qdio->req_q_full),
781 		       (unsigned long long)util);
782 }
783 static DEVICE_ATTR(queue_full, S_IRUGO, zfcp_sysfs_adapter_q_full_show, NULL);
784 
785 struct device_attribute *zfcp_sysfs_shost_attrs[] = {
786 	&dev_attr_utilization,
787 	&dev_attr_requests,
788 	&dev_attr_megabytes,
789 	&dev_attr_seconds_active,
790 	&dev_attr_queue_full,
791 	NULL
792 };
793 
794 static ssize_t zfcp_sysfs_adapter_diag_b2b_credit_show(
795 	struct device *dev, struct device_attribute *attr, char *buf)
796 {
797 	struct zfcp_adapter *adapter = zfcp_ccw_adapter_by_cdev(to_ccwdev(dev));
798 	struct zfcp_diag_header *diag_hdr;
799 	struct fc_els_flogi *nsp;
800 	ssize_t rc = -ENOLINK;
801 	unsigned long flags;
802 	unsigned int status;
803 
804 	if (!adapter)
805 		return -ENODEV;
806 
807 	status = atomic_read(&adapter->status);
808 	if (0 == (status & ZFCP_STATUS_COMMON_OPEN) ||
809 	    0 == (status & ZFCP_STATUS_COMMON_UNBLOCKED) ||
810 	    0 != (status & ZFCP_STATUS_COMMON_ERP_FAILED))
811 		goto out;
812 
813 	diag_hdr = &adapter->diagnostics->config_data.header;
814 
815 	rc = zfcp_diag_update_buffer_limited(
816 		adapter, diag_hdr, zfcp_diag_update_config_data_buffer);
817 	if (rc != 0)
818 		goto out;
819 
820 	spin_lock_irqsave(&diag_hdr->access_lock, flags);
821 	/* nport_serv_param doesn't contain the ELS_Command code */
822 	nsp = (struct fc_els_flogi *)((unsigned long)
823 					      adapter->diagnostics->config_data
824 						      .data.nport_serv_param -
825 				      sizeof(u32));
826 
827 	rc = scnprintf(buf, 5 + 2, "%hu\n",
828 		       be16_to_cpu(nsp->fl_csp.sp_bb_cred));
829 	spin_unlock_irqrestore(&diag_hdr->access_lock, flags);
830 
831 out:
832 	zfcp_ccw_adapter_put(adapter);
833 	return rc;
834 }
835 static ZFCP_DEV_ATTR(adapter_diag, b2b_credit, 0400,
836 		     zfcp_sysfs_adapter_diag_b2b_credit_show, NULL);
837 
838 #define ZFCP_DEFINE_DIAG_SFP_ATTR(_name, _qtcb_member, _prtsize, _prtfmt)      \
839 	static ssize_t zfcp_sysfs_adapter_diag_sfp_##_name##_show(	       \
840 		struct device *dev, struct device_attribute *attr, char *buf)  \
841 	{								       \
842 		struct zfcp_adapter *const adapter =			       \
843 			zfcp_ccw_adapter_by_cdev(to_ccwdev(dev));	       \
844 		struct zfcp_diag_header *diag_hdr;			       \
845 		ssize_t rc = -ENOLINK;					       \
846 		unsigned long flags;					       \
847 		unsigned int status;					       \
848 									       \
849 		if (!adapter)						       \
850 			return -ENODEV;					       \
851 									       \
852 		status = atomic_read(&adapter->status);			       \
853 		if (0 == (status & ZFCP_STATUS_COMMON_OPEN) ||		       \
854 		    0 == (status & ZFCP_STATUS_COMMON_UNBLOCKED) ||	       \
855 		    0 != (status & ZFCP_STATUS_COMMON_ERP_FAILED))	       \
856 			goto out;					       \
857 									       \
858 		if (!zfcp_diag_support_sfp(adapter)) {			       \
859 			rc = -EOPNOTSUPP;				       \
860 			goto out;					       \
861 		}							       \
862 									       \
863 		diag_hdr = &adapter->diagnostics->port_data.header;	       \
864 									       \
865 		rc = zfcp_diag_update_buffer_limited(			       \
866 			adapter, diag_hdr, zfcp_diag_update_port_data_buffer); \
867 		if (rc != 0)						       \
868 			goto out;					       \
869 									       \
870 		spin_lock_irqsave(&diag_hdr->access_lock, flags);	       \
871 		rc = scnprintf(						       \
872 			buf, (_prtsize) + 2, _prtfmt "\n",		       \
873 			adapter->diagnostics->port_data.data._qtcb_member);    \
874 		spin_unlock_irqrestore(&diag_hdr->access_lock, flags);	       \
875 									       \
876 	out:								       \
877 		zfcp_ccw_adapter_put(adapter);				       \
878 		return rc;						       \
879 	}								       \
880 	static ZFCP_DEV_ATTR(adapter_diag_sfp, _name, 0400,		       \
881 			     zfcp_sysfs_adapter_diag_sfp_##_name##_show, NULL)
882 
883 ZFCP_DEFINE_DIAG_SFP_ATTR(temperature, temperature, 6, "%hd");
884 ZFCP_DEFINE_DIAG_SFP_ATTR(vcc, vcc, 5, "%hu");
885 ZFCP_DEFINE_DIAG_SFP_ATTR(tx_bias, tx_bias, 5, "%hu");
886 ZFCP_DEFINE_DIAG_SFP_ATTR(tx_power, tx_power, 5, "%hu");
887 ZFCP_DEFINE_DIAG_SFP_ATTR(rx_power, rx_power, 5, "%hu");
888 ZFCP_DEFINE_DIAG_SFP_ATTR(port_tx_type, sfp_flags.port_tx_type, 2, "%hu");
889 ZFCP_DEFINE_DIAG_SFP_ATTR(optical_port, sfp_flags.optical_port, 1, "%hu");
890 ZFCP_DEFINE_DIAG_SFP_ATTR(sfp_invalid, sfp_flags.sfp_invalid, 1, "%hu");
891 ZFCP_DEFINE_DIAG_SFP_ATTR(connector_type, sfp_flags.connector_type, 1, "%hu");
892 ZFCP_DEFINE_DIAG_SFP_ATTR(fec_active, sfp_flags.fec_active, 1, "%hu");
893 
894 static struct attribute *zfcp_sysfs_diag_attrs[] = {
895 	&dev_attr_adapter_diag_sfp_temperature.attr,
896 	&dev_attr_adapter_diag_sfp_vcc.attr,
897 	&dev_attr_adapter_diag_sfp_tx_bias.attr,
898 	&dev_attr_adapter_diag_sfp_tx_power.attr,
899 	&dev_attr_adapter_diag_sfp_rx_power.attr,
900 	&dev_attr_adapter_diag_sfp_port_tx_type.attr,
901 	&dev_attr_adapter_diag_sfp_optical_port.attr,
902 	&dev_attr_adapter_diag_sfp_sfp_invalid.attr,
903 	&dev_attr_adapter_diag_sfp_connector_type.attr,
904 	&dev_attr_adapter_diag_sfp_fec_active.attr,
905 	&dev_attr_adapter_diag_b2b_credit.attr,
906 	NULL,
907 };
908 
909 const struct attribute_group zfcp_sysfs_diag_attr_group = {
910 	.name = "diagnostics",
911 	.attrs = zfcp_sysfs_diag_attrs,
912 };
913