xref: /openbmc/linux/drivers/s390/scsi/zfcp_sysfs.c (revision 165f2d28)
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 
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 ssize_t zfcp_sysfs_adapter_fc_security_show(
374 	struct device *dev, struct device_attribute *attr, char *buf)
375 {
376 	struct ccw_device *cdev = to_ccwdev(dev);
377 	struct zfcp_adapter *adapter = zfcp_ccw_adapter_by_cdev(cdev);
378 	unsigned int status;
379 	int i;
380 
381 	if (!adapter)
382 		return -ENODEV;
383 
384 	/*
385 	 * Adapter status COMMON_OPEN implies xconf data and xport data
386 	 * was done. Adapter FC Endpoint Security capability remains
387 	 * unchanged in case of COMMON_ERP_FAILED (e.g. due to local link
388 	 * down).
389 	 */
390 	status = atomic_read(&adapter->status);
391 	if (0 == (status & ZFCP_STATUS_COMMON_OPEN))
392 		i = sprintf(buf, "unknown\n");
393 	else if (!(adapter->adapter_features & FSF_FEATURE_FC_SECURITY))
394 		i = sprintf(buf, "unsupported\n");
395 	else {
396 		i = zfcp_fsf_scnprint_fc_security(
397 			buf, PAGE_SIZE - 1, adapter->fc_security_algorithms,
398 			ZFCP_FSF_PRINT_FMT_LIST);
399 		i += scnprintf(buf + i, PAGE_SIZE - i, "\n");
400 	}
401 
402 	zfcp_ccw_adapter_put(adapter);
403 	return i;
404 }
405 static ZFCP_DEV_ATTR(adapter, fc_security, S_IRUGO,
406 		     zfcp_sysfs_adapter_fc_security_show,
407 		     NULL);
408 
409 static struct attribute *zfcp_adapter_attrs[] = {
410 	&dev_attr_adapter_failed.attr,
411 	&dev_attr_adapter_in_recovery.attr,
412 	&dev_attr_adapter_port_remove.attr,
413 	&dev_attr_adapter_port_rescan.attr,
414 	&dev_attr_adapter_peer_wwnn.attr,
415 	&dev_attr_adapter_peer_wwpn.attr,
416 	&dev_attr_adapter_peer_d_id.attr,
417 	&dev_attr_adapter_card_version.attr,
418 	&dev_attr_adapter_lic_version.attr,
419 	&dev_attr_adapter_status.attr,
420 	&dev_attr_adapter_hardware_version.attr,
421 	&dev_attr_adapter_diag_max_age.attr,
422 	&dev_attr_adapter_fc_security.attr,
423 	NULL
424 };
425 
426 struct attribute_group zfcp_sysfs_adapter_attrs = {
427 	.attrs = zfcp_adapter_attrs,
428 };
429 
430 static ssize_t zfcp_sysfs_unit_add_store(struct device *dev,
431 					 struct device_attribute *attr,
432 					 const char *buf, size_t count)
433 {
434 	struct zfcp_port *port = container_of(dev, struct zfcp_port, dev);
435 	u64 fcp_lun;
436 	int retval;
437 
438 	if (kstrtoull(buf, 0, (unsigned long long *) &fcp_lun))
439 		return -EINVAL;
440 
441 	retval = zfcp_unit_add(port, fcp_lun);
442 	if (retval)
443 		return retval;
444 
445 	return count;
446 }
447 static DEVICE_ATTR(unit_add, S_IWUSR, NULL, zfcp_sysfs_unit_add_store);
448 
449 static ssize_t zfcp_sysfs_unit_remove_store(struct device *dev,
450 					    struct device_attribute *attr,
451 					    const char *buf, size_t count)
452 {
453 	struct zfcp_port *port = container_of(dev, struct zfcp_port, dev);
454 	u64 fcp_lun;
455 
456 	if (kstrtoull(buf, 0, (unsigned long long *) &fcp_lun))
457 		return -EINVAL;
458 
459 	if (zfcp_unit_remove(port, fcp_lun))
460 		return -EINVAL;
461 
462 	return count;
463 }
464 static DEVICE_ATTR(unit_remove, S_IWUSR, NULL, zfcp_sysfs_unit_remove_store);
465 
466 static ssize_t zfcp_sysfs_port_fc_security_show(struct device *dev,
467 						struct device_attribute *attr,
468 						char *buf)
469 {
470 	struct zfcp_port *port = container_of(dev, struct zfcp_port, dev);
471 	struct zfcp_adapter *adapter = port->adapter;
472 	unsigned int status = atomic_read(&port->status);
473 	int i;
474 
475 	if (0 == (status & ZFCP_STATUS_COMMON_OPEN) ||
476 	    0 == (status & ZFCP_STATUS_COMMON_UNBLOCKED) ||
477 	    0 == (status & ZFCP_STATUS_PORT_PHYS_OPEN) ||
478 	    0 != (status & ZFCP_STATUS_COMMON_ERP_FAILED) ||
479 	    0 != (status & ZFCP_STATUS_COMMON_ACCESS_BOXED))
480 		i = sprintf(buf, "unknown\n");
481 	else if (!(adapter->adapter_features & FSF_FEATURE_FC_SECURITY))
482 		i = sprintf(buf, "unsupported\n");
483 	else {
484 		i = zfcp_fsf_scnprint_fc_security(
485 			buf, PAGE_SIZE - 1, port->connection_info,
486 			ZFCP_FSF_PRINT_FMT_SINGLEITEM);
487 		i += scnprintf(buf + i, PAGE_SIZE - i, "\n");
488 	}
489 
490 	return i;
491 }
492 static ZFCP_DEV_ATTR(port, fc_security, S_IRUGO,
493 		     zfcp_sysfs_port_fc_security_show,
494 		     NULL);
495 
496 static struct attribute *zfcp_port_attrs[] = {
497 	&dev_attr_unit_add.attr,
498 	&dev_attr_unit_remove.attr,
499 	&dev_attr_port_failed.attr,
500 	&dev_attr_port_in_recovery.attr,
501 	&dev_attr_port_status.attr,
502 	&dev_attr_port_access_denied.attr,
503 	&dev_attr_port_fc_security.attr,
504 	NULL
505 };
506 static struct attribute_group zfcp_port_attr_group = {
507 	.attrs = zfcp_port_attrs,
508 };
509 const struct attribute_group *zfcp_port_attr_groups[] = {
510 	&zfcp_port_attr_group,
511 	NULL,
512 };
513 
514 static struct attribute *zfcp_unit_attrs[] = {
515 	&dev_attr_unit_failed.attr,
516 	&dev_attr_unit_in_recovery.attr,
517 	&dev_attr_unit_status.attr,
518 	&dev_attr_unit_access_denied.attr,
519 	&dev_attr_unit_access_shared.attr,
520 	&dev_attr_unit_access_readonly.attr,
521 	NULL
522 };
523 static struct attribute_group zfcp_unit_attr_group = {
524 	.attrs = zfcp_unit_attrs,
525 };
526 const struct attribute_group *zfcp_unit_attr_groups[] = {
527 	&zfcp_unit_attr_group,
528 	NULL,
529 };
530 
531 #define ZFCP_DEFINE_LATENCY_ATTR(_name) 				\
532 static ssize_t								\
533 zfcp_sysfs_unit_##_name##_latency_show(struct device *dev,		\
534 				       struct device_attribute *attr,	\
535 				       char *buf) {			\
536 	struct scsi_device *sdev = to_scsi_device(dev);			\
537 	struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);		\
538 	struct zfcp_latencies *lat = &zfcp_sdev->latencies;		\
539 	struct zfcp_adapter *adapter = zfcp_sdev->port->adapter;	\
540 	unsigned long long fsum, fmin, fmax, csum, cmin, cmax, cc;	\
541 									\
542 	spin_lock_bh(&lat->lock);					\
543 	fsum = lat->_name.fabric.sum * adapter->timer_ticks;		\
544 	fmin = lat->_name.fabric.min * adapter->timer_ticks;		\
545 	fmax = lat->_name.fabric.max * adapter->timer_ticks;		\
546 	csum = lat->_name.channel.sum * adapter->timer_ticks;		\
547 	cmin = lat->_name.channel.min * adapter->timer_ticks;		\
548 	cmax = lat->_name.channel.max * adapter->timer_ticks;		\
549 	cc  = lat->_name.counter;					\
550 	spin_unlock_bh(&lat->lock);					\
551 									\
552 	do_div(fsum, 1000);						\
553 	do_div(fmin, 1000);						\
554 	do_div(fmax, 1000);						\
555 	do_div(csum, 1000);						\
556 	do_div(cmin, 1000);						\
557 	do_div(cmax, 1000);						\
558 									\
559 	return sprintf(buf, "%llu %llu %llu %llu %llu %llu %llu\n",	\
560 		       fmin, fmax, fsum, cmin, cmax, csum, cc); 	\
561 }									\
562 static ssize_t								\
563 zfcp_sysfs_unit_##_name##_latency_store(struct device *dev,		\
564 					struct device_attribute *attr,	\
565 					const char *buf, size_t count)	\
566 {									\
567 	struct scsi_device *sdev = to_scsi_device(dev);			\
568 	struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);		\
569 	struct zfcp_latencies *lat = &zfcp_sdev->latencies;		\
570 	unsigned long flags;						\
571 									\
572 	spin_lock_irqsave(&lat->lock, flags);				\
573 	lat->_name.fabric.sum = 0;					\
574 	lat->_name.fabric.min = 0xFFFFFFFF;				\
575 	lat->_name.fabric.max = 0;					\
576 	lat->_name.channel.sum = 0;					\
577 	lat->_name.channel.min = 0xFFFFFFFF;				\
578 	lat->_name.channel.max = 0;					\
579 	lat->_name.counter = 0;						\
580 	spin_unlock_irqrestore(&lat->lock, flags);			\
581 									\
582 	return (ssize_t) count;						\
583 }									\
584 static DEVICE_ATTR(_name##_latency, S_IWUSR | S_IRUGO,			\
585 		   zfcp_sysfs_unit_##_name##_latency_show,		\
586 		   zfcp_sysfs_unit_##_name##_latency_store);
587 
588 ZFCP_DEFINE_LATENCY_ATTR(read);
589 ZFCP_DEFINE_LATENCY_ATTR(write);
590 ZFCP_DEFINE_LATENCY_ATTR(cmd);
591 
592 #define ZFCP_DEFINE_SCSI_ATTR(_name, _format, _value)			\
593 static ssize_t zfcp_sysfs_scsi_##_name##_show(struct device *dev,	\
594 					      struct device_attribute *attr,\
595 					      char *buf)                 \
596 {                                                                        \
597 	struct scsi_device *sdev = to_scsi_device(dev);			 \
598 	struct zfcp_scsi_dev *zfcp_sdev = sdev_to_zfcp(sdev);		 \
599 									 \
600 	return sprintf(buf, _format, _value);                            \
601 }                                                                        \
602 static DEVICE_ATTR(_name, S_IRUGO, zfcp_sysfs_scsi_##_name##_show, NULL);
603 
604 ZFCP_DEFINE_SCSI_ATTR(hba_id, "%s\n",
605 		      dev_name(&zfcp_sdev->port->adapter->ccw_device->dev));
606 ZFCP_DEFINE_SCSI_ATTR(wwpn, "0x%016llx\n",
607 		      (unsigned long long) zfcp_sdev->port->wwpn);
608 
609 static ssize_t zfcp_sysfs_scsi_fcp_lun_show(struct device *dev,
610 					    struct device_attribute *attr,
611 					    char *buf)
612 {
613 	struct scsi_device *sdev = to_scsi_device(dev);
614 
615 	return sprintf(buf, "0x%016llx\n", zfcp_scsi_dev_lun(sdev));
616 }
617 static DEVICE_ATTR(fcp_lun, S_IRUGO, zfcp_sysfs_scsi_fcp_lun_show, NULL);
618 
619 ZFCP_DEFINE_SCSI_ATTR(zfcp_access_denied, "%d\n",
620 		      (atomic_read(&zfcp_sdev->status) &
621 		       ZFCP_STATUS_COMMON_ACCESS_DENIED) != 0);
622 
623 static ssize_t zfcp_sysfs_scsi_zfcp_failed_show(struct device *dev,
624 					   struct device_attribute *attr,
625 					   char *buf)
626 {
627 	struct scsi_device *sdev = to_scsi_device(dev);
628 	unsigned int status = atomic_read(&sdev_to_zfcp(sdev)->status);
629 	unsigned int failed = status & ZFCP_STATUS_COMMON_ERP_FAILED ? 1 : 0;
630 
631 	return sprintf(buf, "%d\n", failed);
632 }
633 
634 static ssize_t zfcp_sysfs_scsi_zfcp_failed_store(struct device *dev,
635 					    struct device_attribute *attr,
636 					    const char *buf, size_t count)
637 {
638 	struct scsi_device *sdev = to_scsi_device(dev);
639 	unsigned long val;
640 
641 	if (kstrtoul(buf, 0, &val) || val != 0)
642 		return -EINVAL;
643 
644 	zfcp_erp_set_lun_status(sdev, ZFCP_STATUS_COMMON_RUNNING);
645 	zfcp_erp_lun_reopen(sdev, ZFCP_STATUS_COMMON_ERP_FAILED,
646 			    "syufai3");
647 	zfcp_erp_wait(sdev_to_zfcp(sdev)->port->adapter);
648 
649 	return count;
650 }
651 static DEVICE_ATTR(zfcp_failed, S_IWUSR | S_IRUGO,
652 		   zfcp_sysfs_scsi_zfcp_failed_show,
653 		   zfcp_sysfs_scsi_zfcp_failed_store);
654 
655 ZFCP_DEFINE_SCSI_ATTR(zfcp_in_recovery, "%d\n",
656 		      (atomic_read(&zfcp_sdev->status) &
657 		       ZFCP_STATUS_COMMON_ERP_INUSE) != 0);
658 
659 ZFCP_DEFINE_SCSI_ATTR(zfcp_status, "0x%08x\n",
660 		      atomic_read(&zfcp_sdev->status));
661 
662 struct device_attribute *zfcp_sysfs_sdev_attrs[] = {
663 	&dev_attr_fcp_lun,
664 	&dev_attr_wwpn,
665 	&dev_attr_hba_id,
666 	&dev_attr_read_latency,
667 	&dev_attr_write_latency,
668 	&dev_attr_cmd_latency,
669 	&dev_attr_zfcp_access_denied,
670 	&dev_attr_zfcp_failed,
671 	&dev_attr_zfcp_in_recovery,
672 	&dev_attr_zfcp_status,
673 	NULL
674 };
675 
676 static ssize_t zfcp_sysfs_adapter_util_show(struct device *dev,
677 					    struct device_attribute *attr,
678 					    char *buf)
679 {
680 	struct Scsi_Host *scsi_host = dev_to_shost(dev);
681 	struct fsf_qtcb_bottom_port *qtcb_port;
682 	struct zfcp_adapter *adapter;
683 	int retval;
684 
685 	adapter = (struct zfcp_adapter *) scsi_host->hostdata[0];
686 	if (!(adapter->adapter_features & FSF_FEATURE_MEASUREMENT_DATA))
687 		return -EOPNOTSUPP;
688 
689 	qtcb_port = kzalloc(sizeof(struct fsf_qtcb_bottom_port), GFP_KERNEL);
690 	if (!qtcb_port)
691 		return -ENOMEM;
692 
693 	retval = zfcp_fsf_exchange_port_data_sync(adapter->qdio, qtcb_port);
694 	if (retval == 0 || retval == -EAGAIN)
695 		retval = sprintf(buf, "%u %u %u\n", qtcb_port->cp_util,
696 				 qtcb_port->cb_util, qtcb_port->a_util);
697 	kfree(qtcb_port);
698 	return retval;
699 }
700 static DEVICE_ATTR(utilization, S_IRUGO, zfcp_sysfs_adapter_util_show, NULL);
701 
702 static int zfcp_sysfs_adapter_ex_config(struct device *dev,
703 					struct fsf_statistics_info *stat_inf)
704 {
705 	struct Scsi_Host *scsi_host = dev_to_shost(dev);
706 	struct fsf_qtcb_bottom_config *qtcb_config;
707 	struct zfcp_adapter *adapter;
708 	int retval;
709 
710 	adapter = (struct zfcp_adapter *) scsi_host->hostdata[0];
711 	if (!(adapter->adapter_features & FSF_FEATURE_MEASUREMENT_DATA))
712 		return -EOPNOTSUPP;
713 
714 	qtcb_config = kzalloc(sizeof(struct fsf_qtcb_bottom_config),
715 			      GFP_KERNEL);
716 	if (!qtcb_config)
717 		return -ENOMEM;
718 
719 	retval = zfcp_fsf_exchange_config_data_sync(adapter->qdio, qtcb_config);
720 	if (retval == 0 || retval == -EAGAIN)
721 		*stat_inf = qtcb_config->stat_info;
722 
723 	kfree(qtcb_config);
724 	return retval;
725 }
726 
727 #define ZFCP_SHOST_ATTR(_name, _format, _arg...)			\
728 static ssize_t zfcp_sysfs_adapter_##_name##_show(struct device *dev,	\
729 						 struct device_attribute *attr,\
730 						 char *buf)		\
731 {									\
732 	struct fsf_statistics_info stat_info;				\
733 	int retval;							\
734 									\
735 	retval = zfcp_sysfs_adapter_ex_config(dev, &stat_info);		\
736 	if (retval)							\
737 		return retval;						\
738 									\
739 	return sprintf(buf, _format, ## _arg);				\
740 }									\
741 static DEVICE_ATTR(_name, S_IRUGO, zfcp_sysfs_adapter_##_name##_show, NULL);
742 
743 ZFCP_SHOST_ATTR(requests, "%llu %llu %llu\n",
744 		(unsigned long long) stat_info.input_req,
745 		(unsigned long long) stat_info.output_req,
746 		(unsigned long long) stat_info.control_req);
747 
748 ZFCP_SHOST_ATTR(megabytes, "%llu %llu\n",
749 		(unsigned long long) stat_info.input_mb,
750 		(unsigned long long) stat_info.output_mb);
751 
752 ZFCP_SHOST_ATTR(seconds_active, "%llu\n",
753 		(unsigned long long) stat_info.seconds_act);
754 
755 static ssize_t zfcp_sysfs_adapter_q_full_show(struct device *dev,
756 					      struct device_attribute *attr,
757 					      char *buf)
758 {
759 	struct Scsi_Host *scsi_host = class_to_shost(dev);
760 	struct zfcp_qdio *qdio =
761 		((struct zfcp_adapter *) scsi_host->hostdata[0])->qdio;
762 	u64 util;
763 
764 	spin_lock_bh(&qdio->stat_lock);
765 	util = qdio->req_q_util;
766 	spin_unlock_bh(&qdio->stat_lock);
767 
768 	return sprintf(buf, "%d %llu\n", atomic_read(&qdio->req_q_full),
769 		       (unsigned long long)util);
770 }
771 static DEVICE_ATTR(queue_full, S_IRUGO, zfcp_sysfs_adapter_q_full_show, NULL);
772 
773 struct device_attribute *zfcp_sysfs_shost_attrs[] = {
774 	&dev_attr_utilization,
775 	&dev_attr_requests,
776 	&dev_attr_megabytes,
777 	&dev_attr_seconds_active,
778 	&dev_attr_queue_full,
779 	NULL
780 };
781 
782 static ssize_t zfcp_sysfs_adapter_diag_b2b_credit_show(
783 	struct device *dev, struct device_attribute *attr, char *buf)
784 {
785 	struct zfcp_adapter *adapter = zfcp_ccw_adapter_by_cdev(to_ccwdev(dev));
786 	struct zfcp_diag_header *diag_hdr;
787 	struct fc_els_flogi *nsp;
788 	ssize_t rc = -ENOLINK;
789 	unsigned long flags;
790 	unsigned int status;
791 
792 	if (!adapter)
793 		return -ENODEV;
794 
795 	status = atomic_read(&adapter->status);
796 	if (0 == (status & ZFCP_STATUS_COMMON_OPEN) ||
797 	    0 == (status & ZFCP_STATUS_COMMON_UNBLOCKED) ||
798 	    0 != (status & ZFCP_STATUS_COMMON_ERP_FAILED))
799 		goto out;
800 
801 	diag_hdr = &adapter->diagnostics->config_data.header;
802 
803 	rc = zfcp_diag_update_buffer_limited(
804 		adapter, diag_hdr, zfcp_diag_update_config_data_buffer);
805 	if (rc != 0)
806 		goto out;
807 
808 	spin_lock_irqsave(&diag_hdr->access_lock, flags);
809 	/* nport_serv_param doesn't contain the ELS_Command code */
810 	nsp = (struct fc_els_flogi *)((unsigned long)
811 					      adapter->diagnostics->config_data
812 						      .data.nport_serv_param -
813 				      sizeof(u32));
814 
815 	rc = scnprintf(buf, 5 + 2, "%hu\n",
816 		       be16_to_cpu(nsp->fl_csp.sp_bb_cred));
817 	spin_unlock_irqrestore(&diag_hdr->access_lock, flags);
818 
819 out:
820 	zfcp_ccw_adapter_put(adapter);
821 	return rc;
822 }
823 static ZFCP_DEV_ATTR(adapter_diag, b2b_credit, 0400,
824 		     zfcp_sysfs_adapter_diag_b2b_credit_show, NULL);
825 
826 #define ZFCP_DEFINE_DIAG_SFP_ATTR(_name, _qtcb_member, _prtsize, _prtfmt)      \
827 	static ssize_t zfcp_sysfs_adapter_diag_sfp_##_name##_show(	       \
828 		struct device *dev, struct device_attribute *attr, char *buf)  \
829 	{								       \
830 		struct zfcp_adapter *const adapter =			       \
831 			zfcp_ccw_adapter_by_cdev(to_ccwdev(dev));	       \
832 		struct zfcp_diag_header *diag_hdr;			       \
833 		ssize_t rc = -ENOLINK;					       \
834 		unsigned long flags;					       \
835 		unsigned int status;					       \
836 									       \
837 		if (!adapter)						       \
838 			return -ENODEV;					       \
839 									       \
840 		status = atomic_read(&adapter->status);			       \
841 		if (0 == (status & ZFCP_STATUS_COMMON_OPEN) ||		       \
842 		    0 == (status & ZFCP_STATUS_COMMON_UNBLOCKED) ||	       \
843 		    0 != (status & ZFCP_STATUS_COMMON_ERP_FAILED))	       \
844 			goto out;					       \
845 									       \
846 		if (!zfcp_diag_support_sfp(adapter)) {			       \
847 			rc = -EOPNOTSUPP;				       \
848 			goto out;					       \
849 		}							       \
850 									       \
851 		diag_hdr = &adapter->diagnostics->port_data.header;	       \
852 									       \
853 		rc = zfcp_diag_update_buffer_limited(			       \
854 			adapter, diag_hdr, zfcp_diag_update_port_data_buffer); \
855 		if (rc != 0)						       \
856 			goto out;					       \
857 									       \
858 		spin_lock_irqsave(&diag_hdr->access_lock, flags);	       \
859 		rc = scnprintf(						       \
860 			buf, (_prtsize) + 2, _prtfmt "\n",		       \
861 			adapter->diagnostics->port_data.data._qtcb_member);    \
862 		spin_unlock_irqrestore(&diag_hdr->access_lock, flags);	       \
863 									       \
864 	out:								       \
865 		zfcp_ccw_adapter_put(adapter);				       \
866 		return rc;						       \
867 	}								       \
868 	static ZFCP_DEV_ATTR(adapter_diag_sfp, _name, 0400,		       \
869 			     zfcp_sysfs_adapter_diag_sfp_##_name##_show, NULL)
870 
871 ZFCP_DEFINE_DIAG_SFP_ATTR(temperature, temperature, 6, "%hd");
872 ZFCP_DEFINE_DIAG_SFP_ATTR(vcc, vcc, 5, "%hu");
873 ZFCP_DEFINE_DIAG_SFP_ATTR(tx_bias, tx_bias, 5, "%hu");
874 ZFCP_DEFINE_DIAG_SFP_ATTR(tx_power, tx_power, 5, "%hu");
875 ZFCP_DEFINE_DIAG_SFP_ATTR(rx_power, rx_power, 5, "%hu");
876 ZFCP_DEFINE_DIAG_SFP_ATTR(port_tx_type, sfp_flags.port_tx_type, 2, "%hu");
877 ZFCP_DEFINE_DIAG_SFP_ATTR(optical_port, sfp_flags.optical_port, 1, "%hu");
878 ZFCP_DEFINE_DIAG_SFP_ATTR(sfp_invalid, sfp_flags.sfp_invalid, 1, "%hu");
879 ZFCP_DEFINE_DIAG_SFP_ATTR(connector_type, sfp_flags.connector_type, 1, "%hu");
880 ZFCP_DEFINE_DIAG_SFP_ATTR(fec_active, sfp_flags.fec_active, 1, "%hu");
881 
882 static struct attribute *zfcp_sysfs_diag_attrs[] = {
883 	&dev_attr_adapter_diag_sfp_temperature.attr,
884 	&dev_attr_adapter_diag_sfp_vcc.attr,
885 	&dev_attr_adapter_diag_sfp_tx_bias.attr,
886 	&dev_attr_adapter_diag_sfp_tx_power.attr,
887 	&dev_attr_adapter_diag_sfp_rx_power.attr,
888 	&dev_attr_adapter_diag_sfp_port_tx_type.attr,
889 	&dev_attr_adapter_diag_sfp_optical_port.attr,
890 	&dev_attr_adapter_diag_sfp_sfp_invalid.attr,
891 	&dev_attr_adapter_diag_sfp_connector_type.attr,
892 	&dev_attr_adapter_diag_sfp_fec_active.attr,
893 	&dev_attr_adapter_diag_b2b_credit.attr,
894 	NULL,
895 };
896 
897 const struct attribute_group zfcp_sysfs_diag_attr_group = {
898 	.name = "diagnostics",
899 	.attrs = zfcp_sysfs_diag_attrs,
900 };
901