xref: /linux/drivers/s390/scsi/zfcp_sysfs.c (revision 7f71507851fc7764b36a3221839607d3a45c2025)
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 sysfs_emit(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 sysfs_emit(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 = sysfs_emit(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 sysfs_emit(buf, "1\n");
99 
100 	return sysfs_emit(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 sysfs_emit(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 = sysfs_emit(buf, "1\n");
180 	else
181 		i = sysfs_emit(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 	rc = sysfs_emit(buf, "%lu\n", adapter->diagnostics->max_age);
352 
353 	zfcp_ccw_adapter_put(adapter);
354 	return rc;
355 }
356 
357 static ssize_t
358 zfcp_sysfs_adapter_diag_max_age_store(struct device *dev,
359 				      struct device_attribute *attr,
360 				      const char *buf, size_t count)
361 {
362 	struct zfcp_adapter *adapter = zfcp_ccw_adapter_by_cdev(to_ccwdev(dev));
363 	unsigned long max_age;
364 	ssize_t rc;
365 
366 	if (!adapter)
367 		return -ENODEV;
368 
369 	rc = kstrtoul(buf, 10, &max_age);
370 	if (rc != 0)
371 		goto out;
372 
373 	adapter->diagnostics->max_age = max_age;
374 
375 	rc = count;
376 out:
377 	zfcp_ccw_adapter_put(adapter);
378 	return rc;
379 }
380 static ZFCP_DEV_ATTR(adapter, diag_max_age, 0644,
381 		     zfcp_sysfs_adapter_diag_max_age_show,
382 		     zfcp_sysfs_adapter_diag_max_age_store);
383 
384 static ssize_t zfcp_sysfs_adapter_fc_security_show(
385 	struct device *dev, struct device_attribute *attr, char *buf)
386 {
387 	struct ccw_device *cdev = to_ccwdev(dev);
388 	struct zfcp_adapter *adapter = zfcp_ccw_adapter_by_cdev(cdev);
389 	unsigned int status;
390 	int i;
391 
392 	if (!adapter)
393 		return -ENODEV;
394 
395 	/*
396 	 * Adapter status COMMON_OPEN implies xconf data and xport data
397 	 * was done. Adapter FC Endpoint Security capability remains
398 	 * unchanged in case of COMMON_ERP_FAILED (e.g. due to local link
399 	 * down).
400 	 */
401 	status = atomic_read(&adapter->status);
402 	if (0 == (status & ZFCP_STATUS_COMMON_OPEN))
403 		i = sysfs_emit(buf, "unknown\n");
404 	else if (!(adapter->adapter_features & FSF_FEATURE_FC_SECURITY))
405 		i = sysfs_emit(buf, "unsupported\n");
406 	else {
407 		i = zfcp_fsf_scnprint_fc_security(
408 			buf, PAGE_SIZE - 1, adapter->fc_security_algorithms,
409 			ZFCP_FSF_PRINT_FMT_LIST);
410 		i += sysfs_emit_at(buf, i, "\n");
411 	}
412 
413 	zfcp_ccw_adapter_put(adapter);
414 	return i;
415 }
416 static ZFCP_DEV_ATTR(adapter, fc_security, S_IRUGO,
417 		     zfcp_sysfs_adapter_fc_security_show,
418 		     NULL);
419 
420 static struct attribute *zfcp_adapter_attrs[] = {
421 	&dev_attr_adapter_failed.attr,
422 	&dev_attr_adapter_in_recovery.attr,
423 	&dev_attr_adapter_port_remove.attr,
424 	&dev_attr_adapter_port_rescan.attr,
425 	&dev_attr_adapter_peer_wwnn.attr,
426 	&dev_attr_adapter_peer_wwpn.attr,
427 	&dev_attr_adapter_peer_d_id.attr,
428 	&dev_attr_adapter_card_version.attr,
429 	&dev_attr_adapter_lic_version.attr,
430 	&dev_attr_adapter_status.attr,
431 	&dev_attr_adapter_hardware_version.attr,
432 	&dev_attr_adapter_diag_max_age.attr,
433 	&dev_attr_adapter_fc_security.attr,
434 	NULL
435 };
436 
437 static const struct attribute_group zfcp_sysfs_adapter_attr_group = {
438 	.attrs = zfcp_adapter_attrs,
439 };
440 
441 static ssize_t zfcp_sysfs_unit_add_store(struct device *dev,
442 					 struct device_attribute *attr,
443 					 const char *buf, size_t count)
444 {
445 	struct zfcp_port *port = container_of(dev, struct zfcp_port, dev);
446 	u64 fcp_lun;
447 	int retval;
448 
449 	if (kstrtoull(buf, 0, (unsigned long long *) &fcp_lun))
450 		return -EINVAL;
451 
452 	retval = zfcp_unit_add(port, fcp_lun);
453 	if (retval)
454 		return retval;
455 
456 	return count;
457 }
458 static DEVICE_ATTR(unit_add, S_IWUSR, NULL, zfcp_sysfs_unit_add_store);
459 
460 static ssize_t zfcp_sysfs_unit_remove_store(struct device *dev,
461 					    struct device_attribute *attr,
462 					    const char *buf, size_t count)
463 {
464 	struct zfcp_port *port = container_of(dev, struct zfcp_port, dev);
465 	u64 fcp_lun;
466 
467 	if (kstrtoull(buf, 0, (unsigned long long *) &fcp_lun))
468 		return -EINVAL;
469 
470 	if (zfcp_unit_remove(port, fcp_lun))
471 		return -EINVAL;
472 
473 	return count;
474 }
475 static DEVICE_ATTR(unit_remove, S_IWUSR, NULL, zfcp_sysfs_unit_remove_store);
476 
477 static ssize_t zfcp_sysfs_port_fc_security_show(struct device *dev,
478 						struct device_attribute *attr,
479 						char *buf)
480 {
481 	struct zfcp_port *port = container_of(dev, struct zfcp_port, dev);
482 	struct zfcp_adapter *adapter = port->adapter;
483 	unsigned int status = atomic_read(&port->status);
484 	int i;
485 
486 	if (0 == (status & ZFCP_STATUS_COMMON_OPEN) ||
487 	    0 == (status & ZFCP_STATUS_COMMON_UNBLOCKED) ||
488 	    0 == (status & ZFCP_STATUS_PORT_PHYS_OPEN) ||
489 	    0 != (status & ZFCP_STATUS_PORT_LINK_TEST) ||
490 	    0 != (status & ZFCP_STATUS_COMMON_ERP_FAILED) ||
491 	    0 != (status & ZFCP_STATUS_COMMON_ACCESS_BOXED))
492 		i = sysfs_emit(buf, "unknown\n");
493 	else if (!(adapter->adapter_features & FSF_FEATURE_FC_SECURITY))
494 		i = sysfs_emit(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 += sysfs_emit_at(buf, 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 sysfs_emit(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 sysfs_emit(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 sysfs_emit(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 sysfs_emit(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 static struct attribute *zfcp_sdev_attrs[] = {
675 	&dev_attr_fcp_lun.attr,
676 	&dev_attr_wwpn.attr,
677 	&dev_attr_hba_id.attr,
678 	&dev_attr_read_latency.attr,
679 	&dev_attr_write_latency.attr,
680 	&dev_attr_cmd_latency.attr,
681 	&dev_attr_zfcp_access_denied.attr,
682 	&dev_attr_zfcp_failed.attr,
683 	&dev_attr_zfcp_in_recovery.attr,
684 	&dev_attr_zfcp_status.attr,
685 	NULL
686 };
687 
688 static const struct attribute_group zfcp_sysfs_sdev_attr_group = {
689 	.attrs = zfcp_sdev_attrs
690 };
691 
692 const struct attribute_group *zfcp_sysfs_sdev_attr_groups[] = {
693 	&zfcp_sysfs_sdev_attr_group,
694 	NULL
695 };
696 
697 static ssize_t zfcp_sysfs_adapter_util_show(struct device *dev,
698 					    struct device_attribute *attr,
699 					    char *buf)
700 {
701 	struct Scsi_Host *scsi_host = dev_to_shost(dev);
702 	struct fsf_qtcb_bottom_port *qtcb_port;
703 	struct zfcp_adapter *adapter;
704 	int retval;
705 
706 	adapter = (struct zfcp_adapter *) scsi_host->hostdata[0];
707 	if (!(adapter->adapter_features & FSF_FEATURE_MEASUREMENT_DATA))
708 		return -EOPNOTSUPP;
709 
710 	qtcb_port = kzalloc(sizeof(struct fsf_qtcb_bottom_port), GFP_KERNEL);
711 	if (!qtcb_port)
712 		return -ENOMEM;
713 
714 	retval = zfcp_fsf_exchange_port_data_sync(adapter->qdio, qtcb_port);
715 	if (retval == 0 || retval == -EAGAIN)
716 		retval = sysfs_emit(buf, "%u %u %u\n", qtcb_port->cp_util,
717 				    qtcb_port->cb_util, qtcb_port->a_util);
718 	kfree(qtcb_port);
719 	return retval;
720 }
721 static DEVICE_ATTR(utilization, S_IRUGO, zfcp_sysfs_adapter_util_show, NULL);
722 
723 static int zfcp_sysfs_adapter_ex_config(struct device *dev,
724 					struct fsf_statistics_info *stat_inf)
725 {
726 	struct Scsi_Host *scsi_host = dev_to_shost(dev);
727 	struct fsf_qtcb_bottom_config *qtcb_config;
728 	struct zfcp_adapter *adapter;
729 	int retval;
730 
731 	adapter = (struct zfcp_adapter *) scsi_host->hostdata[0];
732 	if (!(adapter->adapter_features & FSF_FEATURE_MEASUREMENT_DATA))
733 		return -EOPNOTSUPP;
734 
735 	qtcb_config = kzalloc(sizeof(struct fsf_qtcb_bottom_config),
736 			      GFP_KERNEL);
737 	if (!qtcb_config)
738 		return -ENOMEM;
739 
740 	retval = zfcp_fsf_exchange_config_data_sync(adapter->qdio, qtcb_config);
741 	if (retval == 0 || retval == -EAGAIN)
742 		*stat_inf = qtcb_config->stat_info;
743 
744 	kfree(qtcb_config);
745 	return retval;
746 }
747 
748 #define ZFCP_SHOST_ATTR(_name, _format, _arg...)			\
749 static ssize_t zfcp_sysfs_adapter_##_name##_show(struct device *dev,	\
750 						 struct device_attribute *attr,\
751 						 char *buf)		\
752 {									\
753 	struct fsf_statistics_info stat_info;				\
754 	int retval;							\
755 									\
756 	retval = zfcp_sysfs_adapter_ex_config(dev, &stat_info);		\
757 	if (retval)							\
758 		return retval;						\
759 									\
760 	return sysfs_emit(buf, _format, ## _arg);			\
761 }									\
762 static DEVICE_ATTR(_name, S_IRUGO, zfcp_sysfs_adapter_##_name##_show, NULL);
763 
764 ZFCP_SHOST_ATTR(requests, "%llu %llu %llu\n",
765 		(unsigned long long) stat_info.input_req,
766 		(unsigned long long) stat_info.output_req,
767 		(unsigned long long) stat_info.control_req);
768 
769 ZFCP_SHOST_ATTR(megabytes, "%llu %llu\n",
770 		(unsigned long long) stat_info.input_mb,
771 		(unsigned long long) stat_info.output_mb);
772 
773 ZFCP_SHOST_ATTR(seconds_active, "%llu\n",
774 		(unsigned long long) stat_info.seconds_act);
775 
776 static ssize_t zfcp_sysfs_adapter_q_full_show(struct device *dev,
777 					      struct device_attribute *attr,
778 					      char *buf)
779 {
780 	struct Scsi_Host *scsi_host = class_to_shost(dev);
781 	struct zfcp_qdio *qdio =
782 		((struct zfcp_adapter *) scsi_host->hostdata[0])->qdio;
783 	u64 util;
784 
785 	spin_lock_bh(&qdio->stat_lock);
786 	util = qdio->req_q_util;
787 	spin_unlock_bh(&qdio->stat_lock);
788 
789 	return sysfs_emit(buf, "%d %llu\n", atomic_read(&qdio->req_q_full),
790 			  (unsigned long long)util);
791 }
792 static DEVICE_ATTR(queue_full, S_IRUGO, zfcp_sysfs_adapter_q_full_show, NULL);
793 
794 static struct attribute *zfcp_sysfs_shost_attrs[] = {
795 	&dev_attr_utilization.attr,
796 	&dev_attr_requests.attr,
797 	&dev_attr_megabytes.attr,
798 	&dev_attr_seconds_active.attr,
799 	&dev_attr_queue_full.attr,
800 	NULL
801 };
802 
803 static const struct attribute_group zfcp_sysfs_shost_attr_group = {
804 	.attrs = zfcp_sysfs_shost_attrs
805 };
806 
807 const struct attribute_group *zfcp_sysfs_shost_attr_groups[] = {
808 	&zfcp_sysfs_shost_attr_group,
809 	NULL
810 };
811 
812 static ssize_t zfcp_sysfs_adapter_diag_b2b_credit_show(
813 	struct device *dev, struct device_attribute *attr, char *buf)
814 {
815 	struct zfcp_adapter *adapter = zfcp_ccw_adapter_by_cdev(to_ccwdev(dev));
816 	struct zfcp_diag_header *diag_hdr;
817 	struct fc_els_flogi *nsp;
818 	ssize_t rc = -ENOLINK;
819 	unsigned long flags;
820 	unsigned int status;
821 
822 	if (!adapter)
823 		return -ENODEV;
824 
825 	status = atomic_read(&adapter->status);
826 	if (0 == (status & ZFCP_STATUS_COMMON_OPEN) ||
827 	    0 == (status & ZFCP_STATUS_COMMON_UNBLOCKED) ||
828 	    0 != (status & ZFCP_STATUS_COMMON_ERP_FAILED))
829 		goto out;
830 
831 	diag_hdr = &adapter->diagnostics->config_data.header;
832 
833 	rc = zfcp_diag_update_buffer_limited(
834 		adapter, diag_hdr, zfcp_diag_update_config_data_buffer);
835 	if (rc != 0)
836 		goto out;
837 
838 	spin_lock_irqsave(&diag_hdr->access_lock, flags);
839 	/* nport_serv_param doesn't contain the ELS_Command code */
840 	nsp = (struct fc_els_flogi *)((unsigned long)
841 					      adapter->diagnostics->config_data
842 						      .data.nport_serv_param -
843 				      sizeof(u32));
844 
845 	rc = sysfs_emit(buf, "%hu\n", be16_to_cpu(nsp->fl_csp.sp_bb_cred));
846 	spin_unlock_irqrestore(&diag_hdr->access_lock, flags);
847 
848 out:
849 	zfcp_ccw_adapter_put(adapter);
850 	return rc;
851 }
852 static ZFCP_DEV_ATTR(adapter_diag, b2b_credit, 0400,
853 		     zfcp_sysfs_adapter_diag_b2b_credit_show, NULL);
854 
855 #define ZFCP_DEFINE_DIAG_SFP_ATTR(_name, _qtcb_member, _prtfmt)		       \
856 	static ssize_t zfcp_sysfs_adapter_diag_sfp_##_name##_show(	       \
857 		struct device *dev, struct device_attribute *attr, char *buf)  \
858 	{								       \
859 		struct zfcp_adapter *const adapter =			       \
860 			zfcp_ccw_adapter_by_cdev(to_ccwdev(dev));	       \
861 		struct zfcp_diag_header *diag_hdr;			       \
862 		ssize_t rc = -ENOLINK;					       \
863 		unsigned long flags;					       \
864 		unsigned int status;					       \
865 									       \
866 		if (!adapter)						       \
867 			return -ENODEV;					       \
868 									       \
869 		status = atomic_read(&adapter->status);			       \
870 		if (0 == (status & ZFCP_STATUS_COMMON_OPEN) ||		       \
871 		    0 == (status & ZFCP_STATUS_COMMON_UNBLOCKED) ||	       \
872 		    0 != (status & ZFCP_STATUS_COMMON_ERP_FAILED))	       \
873 			goto out;					       \
874 									       \
875 		if (!zfcp_diag_support_sfp(adapter)) {			       \
876 			rc = -EOPNOTSUPP;				       \
877 			goto out;					       \
878 		}							       \
879 									       \
880 		diag_hdr = &adapter->diagnostics->port_data.header;	       \
881 									       \
882 		rc = zfcp_diag_update_buffer_limited(			       \
883 			adapter, diag_hdr, zfcp_diag_update_port_data_buffer); \
884 		if (rc != 0)						       \
885 			goto out;					       \
886 									       \
887 		spin_lock_irqsave(&diag_hdr->access_lock, flags);	       \
888 		rc = sysfs_emit(					       \
889 			buf, _prtfmt "\n",				       \
890 			adapter->diagnostics->port_data.data._qtcb_member);    \
891 		spin_unlock_irqrestore(&diag_hdr->access_lock, flags);	       \
892 									       \
893 	out:								       \
894 		zfcp_ccw_adapter_put(adapter);				       \
895 		return rc;						       \
896 	}								       \
897 	static ZFCP_DEV_ATTR(adapter_diag_sfp, _name, 0400,		       \
898 			     zfcp_sysfs_adapter_diag_sfp_##_name##_show, NULL)
899 
900 ZFCP_DEFINE_DIAG_SFP_ATTR(temperature, temperature, "%hd");
901 ZFCP_DEFINE_DIAG_SFP_ATTR(vcc, vcc, "%hu");
902 ZFCP_DEFINE_DIAG_SFP_ATTR(tx_bias, tx_bias, "%hu");
903 ZFCP_DEFINE_DIAG_SFP_ATTR(tx_power, tx_power, "%hu");
904 ZFCP_DEFINE_DIAG_SFP_ATTR(rx_power, rx_power, "%hu");
905 ZFCP_DEFINE_DIAG_SFP_ATTR(port_tx_type, sfp_flags.port_tx_type, "%hu");
906 ZFCP_DEFINE_DIAG_SFP_ATTR(optical_port, sfp_flags.optical_port, "%hu");
907 ZFCP_DEFINE_DIAG_SFP_ATTR(sfp_invalid, sfp_flags.sfp_invalid, "%hu");
908 ZFCP_DEFINE_DIAG_SFP_ATTR(connector_type, sfp_flags.connector_type, "%hu");
909 ZFCP_DEFINE_DIAG_SFP_ATTR(fec_active, sfp_flags.fec_active, "%hu");
910 
911 static struct attribute *zfcp_sysfs_diag_attrs[] = {
912 	&dev_attr_adapter_diag_sfp_temperature.attr,
913 	&dev_attr_adapter_diag_sfp_vcc.attr,
914 	&dev_attr_adapter_diag_sfp_tx_bias.attr,
915 	&dev_attr_adapter_diag_sfp_tx_power.attr,
916 	&dev_attr_adapter_diag_sfp_rx_power.attr,
917 	&dev_attr_adapter_diag_sfp_port_tx_type.attr,
918 	&dev_attr_adapter_diag_sfp_optical_port.attr,
919 	&dev_attr_adapter_diag_sfp_sfp_invalid.attr,
920 	&dev_attr_adapter_diag_sfp_connector_type.attr,
921 	&dev_attr_adapter_diag_sfp_fec_active.attr,
922 	&dev_attr_adapter_diag_b2b_credit.attr,
923 	NULL,
924 };
925 
926 static const struct attribute_group zfcp_sysfs_diag_attr_group = {
927 	.name = "diagnostics",
928 	.attrs = zfcp_sysfs_diag_attrs,
929 };
930 
931 const struct attribute_group *zfcp_sysfs_adapter_attr_groups[] = {
932 	&zfcp_sysfs_adapter_attr_group,
933 	&zfcp_sysfs_diag_attr_group,
934 	NULL,
935 };
936