xref: /openbmc/linux/drivers/s390/scsi/zfcp_sysfs.c (revision 0ad53fe3)
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 	zfcp_erp_port_shutdown(port, 0, "syprs_1");
331 	device_unregister(&port->dev);
332 
333 	put_device(&port->dev); /* undo zfcp_get_port_by_wwpn() */
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 static const struct attribute_group zfcp_sysfs_adapter_attr_group = {
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_PORT_LINK_TEST) ||
491 	    0 != (status & ZFCP_STATUS_COMMON_ERP_FAILED) ||
492 	    0 != (status & ZFCP_STATUS_COMMON_ACCESS_BOXED))
493 		i = sprintf(buf, "unknown\n");
494 	else if (!(adapter->adapter_features & FSF_FEATURE_FC_SECURITY))
495 		i = sprintf(buf, "unsupported\n");
496 	else {
497 		i = zfcp_fsf_scnprint_fc_security(
498 			buf, PAGE_SIZE - 1, port->connection_info,
499 			ZFCP_FSF_PRINT_FMT_SINGLEITEM);
500 		i += scnprintf(buf + i, PAGE_SIZE - i, "\n");
501 	}
502 
503 	return i;
504 }
505 static ZFCP_DEV_ATTR(port, fc_security, S_IRUGO,
506 		     zfcp_sysfs_port_fc_security_show,
507 		     NULL);
508 
509 static struct attribute *zfcp_port_attrs[] = {
510 	&dev_attr_unit_add.attr,
511 	&dev_attr_unit_remove.attr,
512 	&dev_attr_port_failed.attr,
513 	&dev_attr_port_in_recovery.attr,
514 	&dev_attr_port_status.attr,
515 	&dev_attr_port_access_denied.attr,
516 	&dev_attr_port_fc_security.attr,
517 	NULL
518 };
519 static struct attribute_group zfcp_port_attr_group = {
520 	.attrs = zfcp_port_attrs,
521 };
522 const struct attribute_group *zfcp_port_attr_groups[] = {
523 	&zfcp_port_attr_group,
524 	NULL,
525 };
526 
527 static struct attribute *zfcp_unit_attrs[] = {
528 	&dev_attr_unit_failed.attr,
529 	&dev_attr_unit_in_recovery.attr,
530 	&dev_attr_unit_status.attr,
531 	&dev_attr_unit_access_denied.attr,
532 	&dev_attr_unit_access_shared.attr,
533 	&dev_attr_unit_access_readonly.attr,
534 	NULL
535 };
536 static struct attribute_group zfcp_unit_attr_group = {
537 	.attrs = zfcp_unit_attrs,
538 };
539 const struct attribute_group *zfcp_unit_attr_groups[] = {
540 	&zfcp_unit_attr_group,
541 	NULL,
542 };
543 
544 #define ZFCP_DEFINE_LATENCY_ATTR(_name) 				\
545 static ssize_t								\
546 zfcp_sysfs_unit_##_name##_latency_show(struct device *dev,		\
547 				       struct device_attribute *attr,	\
548 				       char *buf) {			\
549 	struct scsi_device *sdev = to_scsi_device(dev);			\
550 	struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);		\
551 	struct zfcp_latencies *lat = &zfcp_sdev->latencies;		\
552 	struct zfcp_adapter *adapter = zfcp_sdev->port->adapter;	\
553 	unsigned long long fsum, fmin, fmax, csum, cmin, cmax, cc;	\
554 									\
555 	spin_lock_bh(&lat->lock);					\
556 	fsum = lat->_name.fabric.sum * adapter->timer_ticks;		\
557 	fmin = lat->_name.fabric.min * adapter->timer_ticks;		\
558 	fmax = lat->_name.fabric.max * adapter->timer_ticks;		\
559 	csum = lat->_name.channel.sum * adapter->timer_ticks;		\
560 	cmin = lat->_name.channel.min * adapter->timer_ticks;		\
561 	cmax = lat->_name.channel.max * adapter->timer_ticks;		\
562 	cc  = lat->_name.counter;					\
563 	spin_unlock_bh(&lat->lock);					\
564 									\
565 	do_div(fsum, 1000);						\
566 	do_div(fmin, 1000);						\
567 	do_div(fmax, 1000);						\
568 	do_div(csum, 1000);						\
569 	do_div(cmin, 1000);						\
570 	do_div(cmax, 1000);						\
571 									\
572 	return sprintf(buf, "%llu %llu %llu %llu %llu %llu %llu\n",	\
573 		       fmin, fmax, fsum, cmin, cmax, csum, cc); 	\
574 }									\
575 static ssize_t								\
576 zfcp_sysfs_unit_##_name##_latency_store(struct device *dev,		\
577 					struct device_attribute *attr,	\
578 					const char *buf, size_t count)	\
579 {									\
580 	struct scsi_device *sdev = to_scsi_device(dev);			\
581 	struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);		\
582 	struct zfcp_latencies *lat = &zfcp_sdev->latencies;		\
583 	unsigned long flags;						\
584 									\
585 	spin_lock_irqsave(&lat->lock, flags);				\
586 	lat->_name.fabric.sum = 0;					\
587 	lat->_name.fabric.min = 0xFFFFFFFF;				\
588 	lat->_name.fabric.max = 0;					\
589 	lat->_name.channel.sum = 0;					\
590 	lat->_name.channel.min = 0xFFFFFFFF;				\
591 	lat->_name.channel.max = 0;					\
592 	lat->_name.counter = 0;						\
593 	spin_unlock_irqrestore(&lat->lock, flags);			\
594 									\
595 	return (ssize_t) count;						\
596 }									\
597 static DEVICE_ATTR(_name##_latency, S_IWUSR | S_IRUGO,			\
598 		   zfcp_sysfs_unit_##_name##_latency_show,		\
599 		   zfcp_sysfs_unit_##_name##_latency_store);
600 
601 ZFCP_DEFINE_LATENCY_ATTR(read);
602 ZFCP_DEFINE_LATENCY_ATTR(write);
603 ZFCP_DEFINE_LATENCY_ATTR(cmd);
604 
605 #define ZFCP_DEFINE_SCSI_ATTR(_name, _format, _value)			\
606 static ssize_t zfcp_sysfs_scsi_##_name##_show(struct device *dev,	\
607 					      struct device_attribute *attr,\
608 					      char *buf)                 \
609 {                                                                        \
610 	struct scsi_device *sdev = to_scsi_device(dev);			 \
611 	struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);		 \
612 									 \
613 	return sprintf(buf, _format, _value);                            \
614 }                                                                        \
615 static DEVICE_ATTR(_name, S_IRUGO, zfcp_sysfs_scsi_##_name##_show, NULL);
616 
617 ZFCP_DEFINE_SCSI_ATTR(hba_id, "%s\n",
618 		      dev_name(&zfcp_sdev->port->adapter->ccw_device->dev));
619 ZFCP_DEFINE_SCSI_ATTR(wwpn, "0x%016llx\n",
620 		      (unsigned long long) zfcp_sdev->port->wwpn);
621 
622 static ssize_t zfcp_sysfs_scsi_fcp_lun_show(struct device *dev,
623 					    struct device_attribute *attr,
624 					    char *buf)
625 {
626 	struct scsi_device *sdev = to_scsi_device(dev);
627 
628 	return sprintf(buf, "0x%016llx\n", zfcp_scsi_dev_lun(sdev));
629 }
630 static DEVICE_ATTR(fcp_lun, S_IRUGO, zfcp_sysfs_scsi_fcp_lun_show, NULL);
631 
632 ZFCP_DEFINE_SCSI_ATTR(zfcp_access_denied, "%d\n",
633 		      (atomic_read(&zfcp_sdev->status) &
634 		       ZFCP_STATUS_COMMON_ACCESS_DENIED) != 0);
635 
636 static ssize_t zfcp_sysfs_scsi_zfcp_failed_show(struct device *dev,
637 					   struct device_attribute *attr,
638 					   char *buf)
639 {
640 	struct scsi_device *sdev = to_scsi_device(dev);
641 	unsigned int status = atomic_read(&sdev_to_zfcp(sdev)->status);
642 	unsigned int failed = status & ZFCP_STATUS_COMMON_ERP_FAILED ? 1 : 0;
643 
644 	return sprintf(buf, "%d\n", failed);
645 }
646 
647 static ssize_t zfcp_sysfs_scsi_zfcp_failed_store(struct device *dev,
648 					    struct device_attribute *attr,
649 					    const char *buf, size_t count)
650 {
651 	struct scsi_device *sdev = to_scsi_device(dev);
652 	unsigned long val;
653 
654 	if (kstrtoul(buf, 0, &val) || val != 0)
655 		return -EINVAL;
656 
657 	zfcp_erp_set_lun_status(sdev, ZFCP_STATUS_COMMON_RUNNING);
658 	zfcp_erp_lun_reopen(sdev, ZFCP_STATUS_COMMON_ERP_FAILED,
659 			    "syufai3");
660 	zfcp_erp_wait(sdev_to_zfcp(sdev)->port->adapter);
661 
662 	return count;
663 }
664 static DEVICE_ATTR(zfcp_failed, S_IWUSR | S_IRUGO,
665 		   zfcp_sysfs_scsi_zfcp_failed_show,
666 		   zfcp_sysfs_scsi_zfcp_failed_store);
667 
668 ZFCP_DEFINE_SCSI_ATTR(zfcp_in_recovery, "%d\n",
669 		      (atomic_read(&zfcp_sdev->status) &
670 		       ZFCP_STATUS_COMMON_ERP_INUSE) != 0);
671 
672 ZFCP_DEFINE_SCSI_ATTR(zfcp_status, "0x%08x\n",
673 		      atomic_read(&zfcp_sdev->status));
674 
675 struct device_attribute *zfcp_sysfs_sdev_attrs[] = {
676 	&dev_attr_fcp_lun,
677 	&dev_attr_wwpn,
678 	&dev_attr_hba_id,
679 	&dev_attr_read_latency,
680 	&dev_attr_write_latency,
681 	&dev_attr_cmd_latency,
682 	&dev_attr_zfcp_access_denied,
683 	&dev_attr_zfcp_failed,
684 	&dev_attr_zfcp_in_recovery,
685 	&dev_attr_zfcp_status,
686 	NULL
687 };
688 
689 static ssize_t zfcp_sysfs_adapter_util_show(struct device *dev,
690 					    struct device_attribute *attr,
691 					    char *buf)
692 {
693 	struct Scsi_Host *scsi_host = dev_to_shost(dev);
694 	struct fsf_qtcb_bottom_port *qtcb_port;
695 	struct zfcp_adapter *adapter;
696 	int retval;
697 
698 	adapter = (struct zfcp_adapter *) scsi_host->hostdata[0];
699 	if (!(adapter->adapter_features & FSF_FEATURE_MEASUREMENT_DATA))
700 		return -EOPNOTSUPP;
701 
702 	qtcb_port = kzalloc(sizeof(struct fsf_qtcb_bottom_port), GFP_KERNEL);
703 	if (!qtcb_port)
704 		return -ENOMEM;
705 
706 	retval = zfcp_fsf_exchange_port_data_sync(adapter->qdio, qtcb_port);
707 	if (retval == 0 || retval == -EAGAIN)
708 		retval = sprintf(buf, "%u %u %u\n", qtcb_port->cp_util,
709 				 qtcb_port->cb_util, qtcb_port->a_util);
710 	kfree(qtcb_port);
711 	return retval;
712 }
713 static DEVICE_ATTR(utilization, S_IRUGO, zfcp_sysfs_adapter_util_show, NULL);
714 
715 static int zfcp_sysfs_adapter_ex_config(struct device *dev,
716 					struct fsf_statistics_info *stat_inf)
717 {
718 	struct Scsi_Host *scsi_host = dev_to_shost(dev);
719 	struct fsf_qtcb_bottom_config *qtcb_config;
720 	struct zfcp_adapter *adapter;
721 	int retval;
722 
723 	adapter = (struct zfcp_adapter *) scsi_host->hostdata[0];
724 	if (!(adapter->adapter_features & FSF_FEATURE_MEASUREMENT_DATA))
725 		return -EOPNOTSUPP;
726 
727 	qtcb_config = kzalloc(sizeof(struct fsf_qtcb_bottom_config),
728 			      GFP_KERNEL);
729 	if (!qtcb_config)
730 		return -ENOMEM;
731 
732 	retval = zfcp_fsf_exchange_config_data_sync(adapter->qdio, qtcb_config);
733 	if (retval == 0 || retval == -EAGAIN)
734 		*stat_inf = qtcb_config->stat_info;
735 
736 	kfree(qtcb_config);
737 	return retval;
738 }
739 
740 #define ZFCP_SHOST_ATTR(_name, _format, _arg...)			\
741 static ssize_t zfcp_sysfs_adapter_##_name##_show(struct device *dev,	\
742 						 struct device_attribute *attr,\
743 						 char *buf)		\
744 {									\
745 	struct fsf_statistics_info stat_info;				\
746 	int retval;							\
747 									\
748 	retval = zfcp_sysfs_adapter_ex_config(dev, &stat_info);		\
749 	if (retval)							\
750 		return retval;						\
751 									\
752 	return sprintf(buf, _format, ## _arg);				\
753 }									\
754 static DEVICE_ATTR(_name, S_IRUGO, zfcp_sysfs_adapter_##_name##_show, NULL);
755 
756 ZFCP_SHOST_ATTR(requests, "%llu %llu %llu\n",
757 		(unsigned long long) stat_info.input_req,
758 		(unsigned long long) stat_info.output_req,
759 		(unsigned long long) stat_info.control_req);
760 
761 ZFCP_SHOST_ATTR(megabytes, "%llu %llu\n",
762 		(unsigned long long) stat_info.input_mb,
763 		(unsigned long long) stat_info.output_mb);
764 
765 ZFCP_SHOST_ATTR(seconds_active, "%llu\n",
766 		(unsigned long long) stat_info.seconds_act);
767 
768 static ssize_t zfcp_sysfs_adapter_q_full_show(struct device *dev,
769 					      struct device_attribute *attr,
770 					      char *buf)
771 {
772 	struct Scsi_Host *scsi_host = class_to_shost(dev);
773 	struct zfcp_qdio *qdio =
774 		((struct zfcp_adapter *) scsi_host->hostdata[0])->qdio;
775 	u64 util;
776 
777 	spin_lock_bh(&qdio->stat_lock);
778 	util = qdio->req_q_util;
779 	spin_unlock_bh(&qdio->stat_lock);
780 
781 	return sprintf(buf, "%d %llu\n", atomic_read(&qdio->req_q_full),
782 		       (unsigned long long)util);
783 }
784 static DEVICE_ATTR(queue_full, S_IRUGO, zfcp_sysfs_adapter_q_full_show, NULL);
785 
786 struct device_attribute *zfcp_sysfs_shost_attrs[] = {
787 	&dev_attr_utilization,
788 	&dev_attr_requests,
789 	&dev_attr_megabytes,
790 	&dev_attr_seconds_active,
791 	&dev_attr_queue_full,
792 	NULL
793 };
794 
795 static ssize_t zfcp_sysfs_adapter_diag_b2b_credit_show(
796 	struct device *dev, struct device_attribute *attr, char *buf)
797 {
798 	struct zfcp_adapter *adapter = zfcp_ccw_adapter_by_cdev(to_ccwdev(dev));
799 	struct zfcp_diag_header *diag_hdr;
800 	struct fc_els_flogi *nsp;
801 	ssize_t rc = -ENOLINK;
802 	unsigned long flags;
803 	unsigned int status;
804 
805 	if (!adapter)
806 		return -ENODEV;
807 
808 	status = atomic_read(&adapter->status);
809 	if (0 == (status & ZFCP_STATUS_COMMON_OPEN) ||
810 	    0 == (status & ZFCP_STATUS_COMMON_UNBLOCKED) ||
811 	    0 != (status & ZFCP_STATUS_COMMON_ERP_FAILED))
812 		goto out;
813 
814 	diag_hdr = &adapter->diagnostics->config_data.header;
815 
816 	rc = zfcp_diag_update_buffer_limited(
817 		adapter, diag_hdr, zfcp_diag_update_config_data_buffer);
818 	if (rc != 0)
819 		goto out;
820 
821 	spin_lock_irqsave(&diag_hdr->access_lock, flags);
822 	/* nport_serv_param doesn't contain the ELS_Command code */
823 	nsp = (struct fc_els_flogi *)((unsigned long)
824 					      adapter->diagnostics->config_data
825 						      .data.nport_serv_param -
826 				      sizeof(u32));
827 
828 	rc = scnprintf(buf, 5 + 2, "%hu\n",
829 		       be16_to_cpu(nsp->fl_csp.sp_bb_cred));
830 	spin_unlock_irqrestore(&diag_hdr->access_lock, flags);
831 
832 out:
833 	zfcp_ccw_adapter_put(adapter);
834 	return rc;
835 }
836 static ZFCP_DEV_ATTR(adapter_diag, b2b_credit, 0400,
837 		     zfcp_sysfs_adapter_diag_b2b_credit_show, NULL);
838 
839 #define ZFCP_DEFINE_DIAG_SFP_ATTR(_name, _qtcb_member, _prtsize, _prtfmt)      \
840 	static ssize_t zfcp_sysfs_adapter_diag_sfp_##_name##_show(	       \
841 		struct device *dev, struct device_attribute *attr, char *buf)  \
842 	{								       \
843 		struct zfcp_adapter *const adapter =			       \
844 			zfcp_ccw_adapter_by_cdev(to_ccwdev(dev));	       \
845 		struct zfcp_diag_header *diag_hdr;			       \
846 		ssize_t rc = -ENOLINK;					       \
847 		unsigned long flags;					       \
848 		unsigned int status;					       \
849 									       \
850 		if (!adapter)						       \
851 			return -ENODEV;					       \
852 									       \
853 		status = atomic_read(&adapter->status);			       \
854 		if (0 == (status & ZFCP_STATUS_COMMON_OPEN) ||		       \
855 		    0 == (status & ZFCP_STATUS_COMMON_UNBLOCKED) ||	       \
856 		    0 != (status & ZFCP_STATUS_COMMON_ERP_FAILED))	       \
857 			goto out;					       \
858 									       \
859 		if (!zfcp_diag_support_sfp(adapter)) {			       \
860 			rc = -EOPNOTSUPP;				       \
861 			goto out;					       \
862 		}							       \
863 									       \
864 		diag_hdr = &adapter->diagnostics->port_data.header;	       \
865 									       \
866 		rc = zfcp_diag_update_buffer_limited(			       \
867 			adapter, diag_hdr, zfcp_diag_update_port_data_buffer); \
868 		if (rc != 0)						       \
869 			goto out;					       \
870 									       \
871 		spin_lock_irqsave(&diag_hdr->access_lock, flags);	       \
872 		rc = scnprintf(						       \
873 			buf, (_prtsize) + 2, _prtfmt "\n",		       \
874 			adapter->diagnostics->port_data.data._qtcb_member);    \
875 		spin_unlock_irqrestore(&diag_hdr->access_lock, flags);	       \
876 									       \
877 	out:								       \
878 		zfcp_ccw_adapter_put(adapter);				       \
879 		return rc;						       \
880 	}								       \
881 	static ZFCP_DEV_ATTR(adapter_diag_sfp, _name, 0400,		       \
882 			     zfcp_sysfs_adapter_diag_sfp_##_name##_show, NULL)
883 
884 ZFCP_DEFINE_DIAG_SFP_ATTR(temperature, temperature, 6, "%hd");
885 ZFCP_DEFINE_DIAG_SFP_ATTR(vcc, vcc, 5, "%hu");
886 ZFCP_DEFINE_DIAG_SFP_ATTR(tx_bias, tx_bias, 5, "%hu");
887 ZFCP_DEFINE_DIAG_SFP_ATTR(tx_power, tx_power, 5, "%hu");
888 ZFCP_DEFINE_DIAG_SFP_ATTR(rx_power, rx_power, 5, "%hu");
889 ZFCP_DEFINE_DIAG_SFP_ATTR(port_tx_type, sfp_flags.port_tx_type, 2, "%hu");
890 ZFCP_DEFINE_DIAG_SFP_ATTR(optical_port, sfp_flags.optical_port, 1, "%hu");
891 ZFCP_DEFINE_DIAG_SFP_ATTR(sfp_invalid, sfp_flags.sfp_invalid, 1, "%hu");
892 ZFCP_DEFINE_DIAG_SFP_ATTR(connector_type, sfp_flags.connector_type, 1, "%hu");
893 ZFCP_DEFINE_DIAG_SFP_ATTR(fec_active, sfp_flags.fec_active, 1, "%hu");
894 
895 static struct attribute *zfcp_sysfs_diag_attrs[] = {
896 	&dev_attr_adapter_diag_sfp_temperature.attr,
897 	&dev_attr_adapter_diag_sfp_vcc.attr,
898 	&dev_attr_adapter_diag_sfp_tx_bias.attr,
899 	&dev_attr_adapter_diag_sfp_tx_power.attr,
900 	&dev_attr_adapter_diag_sfp_rx_power.attr,
901 	&dev_attr_adapter_diag_sfp_port_tx_type.attr,
902 	&dev_attr_adapter_diag_sfp_optical_port.attr,
903 	&dev_attr_adapter_diag_sfp_sfp_invalid.attr,
904 	&dev_attr_adapter_diag_sfp_connector_type.attr,
905 	&dev_attr_adapter_diag_sfp_fec_active.attr,
906 	&dev_attr_adapter_diag_b2b_credit.attr,
907 	NULL,
908 };
909 
910 static const struct attribute_group zfcp_sysfs_diag_attr_group = {
911 	.name = "diagnostics",
912 	.attrs = zfcp_sysfs_diag_attrs,
913 };
914 
915 const struct attribute_group *zfcp_sysfs_adapter_attr_groups[] = {
916 	&zfcp_sysfs_adapter_attr_group,
917 	&zfcp_sysfs_diag_attr_group,
918 	NULL,
919 };
920