xref: /linux/drivers/s390/block/dcssblk.c (revision 7fc2cd2e4b398c57c9cf961cfea05eadbf34c05c)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * dcssblk.c -- the S/390 block driver for dcss memory
4  *
5  * Authors: Carsten Otte, Stefan Weinhuber, Gerald Schaefer
6  */
7 
8 #define pr_fmt(fmt) "dcssblk: " fmt
9 
10 #include <linux/module.h>
11 #include <linux/moduleparam.h>
12 #include <linux/ctype.h>
13 #include <linux/errno.h>
14 #include <linux/init.h>
15 #include <linux/slab.h>
16 #include <linux/blkdev.h>
17 #include <linux/completion.h>
18 #include <linux/interrupt.h>
19 #include <linux/uio.h>
20 #include <linux/dax.h>
21 #include <linux/io.h>
22 #include <asm/extmem.h>
23 
24 #define DCSSBLK_NAME "dcssblk"
25 #define DCSSBLK_MINORS_PER_DISK 1
26 #define DCSSBLK_PARM_LEN 400
27 #define DCSS_BUS_ID_SIZE 20
28 
29 static int dcssblk_open(struct gendisk *disk, blk_mode_t mode);
30 static void dcssblk_release(struct gendisk *disk);
31 static void dcssblk_submit_bio(struct bio *bio);
32 static long dcssblk_dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff,
33 		long nr_pages, enum dax_access_mode mode, void **kaddr,
34 		unsigned long *pfn);
35 
36 static char dcssblk_segments[DCSSBLK_PARM_LEN] = "\0";
37 
38 static int dcssblk_major;
39 static const struct block_device_operations dcssblk_devops = {
40 	.owner   	= THIS_MODULE,
41 	.submit_bio	= dcssblk_submit_bio,
42 	.open    	= dcssblk_open,
43 	.release 	= dcssblk_release,
44 };
45 
46 static int dcssblk_dax_zero_page_range(struct dax_device *dax_dev,
47 				       pgoff_t pgoff, size_t nr_pages)
48 {
49 	long rc;
50 	void *kaddr;
51 
52 	rc = dax_direct_access(dax_dev, pgoff, nr_pages, DAX_ACCESS,
53 			&kaddr, NULL);
54 	if (rc < 0)
55 		return dax_mem2blk_err(rc);
56 
57 	memset(kaddr, 0, nr_pages << PAGE_SHIFT);
58 	dax_flush(dax_dev, kaddr, nr_pages << PAGE_SHIFT);
59 	return 0;
60 }
61 
62 static const struct dax_operations dcssblk_dax_ops = {
63 	.direct_access = dcssblk_dax_direct_access,
64 	.zero_page_range = dcssblk_dax_zero_page_range,
65 };
66 
67 struct dcssblk_dev_info {
68 	struct list_head lh;
69 	struct device dev;
70 	char segment_name[DCSS_BUS_ID_SIZE];
71 	atomic_t use_count;
72 	struct gendisk *gd;
73 	unsigned long start;
74 	unsigned long end;
75 	int segment_type;
76 	unsigned char save_pending;
77 	unsigned char is_shared;
78 	int num_of_segments;
79 	struct list_head seg_list;
80 	struct dax_device *dax_dev;
81 	struct dev_pagemap pgmap;
82 	void *pgmap_addr;
83 };
84 
85 struct segment_info {
86 	struct list_head lh;
87 	char segment_name[DCSS_BUS_ID_SIZE];
88 	unsigned long start;
89 	unsigned long end;
90 	int segment_type;
91 };
92 
93 static ssize_t dcssblk_add_store(struct device * dev, struct device_attribute *attr, const char * buf,
94 				  size_t count);
95 static ssize_t dcssblk_remove_store(struct device * dev, struct device_attribute *attr, const char * buf,
96 				  size_t count);
97 
98 static DEVICE_ATTR(add, S_IWUSR, NULL, dcssblk_add_store);
99 static DEVICE_ATTR(remove, S_IWUSR, NULL, dcssblk_remove_store);
100 
101 static struct device *dcssblk_root_dev;
102 
103 static LIST_HEAD(dcssblk_devices);
104 static struct rw_semaphore dcssblk_devices_sem;
105 
106 /*
107  * release function for segment device.
108  */
109 static void
110 dcssblk_release_segment(struct device *dev)
111 {
112 	struct dcssblk_dev_info *dev_info;
113 	struct segment_info *entry, *temp;
114 
115 	dev_info = container_of(dev, struct dcssblk_dev_info, dev);
116 	list_for_each_entry_safe(entry, temp, &dev_info->seg_list, lh) {
117 		list_del(&entry->lh);
118 		kfree(entry);
119 	}
120 	kfree(dev_info);
121 	module_put(THIS_MODULE);
122 }
123 
124 /*
125  * get a minor number. needs to be called with
126  * down_write(&dcssblk_devices_sem) and the
127  * device needs to be enqueued before the semaphore is
128  * freed.
129  */
130 static int
131 dcssblk_assign_free_minor(struct dcssblk_dev_info *dev_info)
132 {
133 	int minor, found;
134 	struct dcssblk_dev_info *entry;
135 
136 	if (dev_info == NULL)
137 		return -EINVAL;
138 	for (minor = 0; minor < (1<<MINORBITS); minor++) {
139 		found = 0;
140 		// test if minor available
141 		list_for_each_entry(entry, &dcssblk_devices, lh)
142 			if (minor == entry->gd->first_minor)
143 				found++;
144 		if (!found) break; // got unused minor
145 	}
146 	if (found)
147 		return -EBUSY;
148 	dev_info->gd->first_minor = minor;
149 	return 0;
150 }
151 
152 /*
153  * get the struct dcssblk_dev_info from dcssblk_devices
154  * for the given name.
155  * down_read(&dcssblk_devices_sem) must be held.
156  */
157 static struct dcssblk_dev_info *
158 dcssblk_get_device_by_name(char *name)
159 {
160 	struct dcssblk_dev_info *entry;
161 
162 	list_for_each_entry(entry, &dcssblk_devices, lh) {
163 		if (!strcmp(name, entry->segment_name)) {
164 			return entry;
165 		}
166 	}
167 	return NULL;
168 }
169 
170 /*
171  * get the struct segment_info from seg_list
172  * for the given name.
173  * down_read(&dcssblk_devices_sem) must be held.
174  */
175 static struct segment_info *
176 dcssblk_get_segment_by_name(char *name)
177 {
178 	struct dcssblk_dev_info *dev_info;
179 	struct segment_info *entry;
180 
181 	list_for_each_entry(dev_info, &dcssblk_devices, lh) {
182 		list_for_each_entry(entry, &dev_info->seg_list, lh) {
183 			if (!strcmp(name, entry->segment_name))
184 				return entry;
185 		}
186 	}
187 	return NULL;
188 }
189 
190 /*
191  * get the highest address of the multi-segment block.
192  */
193 static unsigned long
194 dcssblk_find_highest_addr(struct dcssblk_dev_info *dev_info)
195 {
196 	unsigned long highest_addr;
197 	struct segment_info *entry;
198 
199 	highest_addr = 0;
200 	list_for_each_entry(entry, &dev_info->seg_list, lh) {
201 		if (highest_addr < entry->end)
202 			highest_addr = entry->end;
203 	}
204 	return highest_addr;
205 }
206 
207 /*
208  * get the lowest address of the multi-segment block.
209  */
210 static unsigned long
211 dcssblk_find_lowest_addr(struct dcssblk_dev_info *dev_info)
212 {
213 	int set_first;
214 	unsigned long lowest_addr;
215 	struct segment_info *entry;
216 
217 	set_first = 0;
218 	lowest_addr = 0;
219 	list_for_each_entry(entry, &dev_info->seg_list, lh) {
220 		if (set_first == 0) {
221 			lowest_addr = entry->start;
222 			set_first = 1;
223 		} else {
224 			if (lowest_addr > entry->start)
225 				lowest_addr = entry->start;
226 		}
227 	}
228 	return lowest_addr;
229 }
230 
231 /*
232  * Check continuity of segments.
233  */
234 static int
235 dcssblk_is_continuous(struct dcssblk_dev_info *dev_info)
236 {
237 	int i, j, rc;
238 	struct segment_info *sort_list, *entry, temp;
239 
240 	if (dev_info->num_of_segments <= 1)
241 		return 0;
242 
243 	sort_list = kcalloc(dev_info->num_of_segments,
244 			    sizeof(struct segment_info),
245 			    GFP_KERNEL);
246 	if (sort_list == NULL)
247 		return -ENOMEM;
248 	i = 0;
249 	list_for_each_entry(entry, &dev_info->seg_list, lh) {
250 		memcpy(&sort_list[i], entry, sizeof(struct segment_info));
251 		i++;
252 	}
253 
254 	/* sort segments */
255 	for (i = 0; i < dev_info->num_of_segments; i++)
256 		for (j = 0; j < dev_info->num_of_segments; j++)
257 			if (sort_list[j].start > sort_list[i].start) {
258 				memcpy(&temp, &sort_list[i],
259 					sizeof(struct segment_info));
260 				memcpy(&sort_list[i], &sort_list[j],
261 					sizeof(struct segment_info));
262 				memcpy(&sort_list[j], &temp,
263 					sizeof(struct segment_info));
264 			}
265 
266 	/* check continuity */
267 	for (i = 0; i < dev_info->num_of_segments - 1; i++) {
268 		if ((sort_list[i].end + 1) != sort_list[i+1].start) {
269 			pr_err("Adjacent DCSSs %s and %s are not "
270 			       "contiguous\n", sort_list[i].segment_name,
271 			       sort_list[i+1].segment_name);
272 			rc = -EINVAL;
273 			goto out;
274 		}
275 		/* EN and EW are allowed in a block device */
276 		if (sort_list[i].segment_type != sort_list[i+1].segment_type) {
277 			if (!(sort_list[i].segment_type & SEGMENT_EXCLUSIVE) ||
278 				(sort_list[i].segment_type == SEG_TYPE_ER) ||
279 				!(sort_list[i+1].segment_type &
280 				SEGMENT_EXCLUSIVE) ||
281 				(sort_list[i+1].segment_type == SEG_TYPE_ER)) {
282 				pr_err("DCSS %s and DCSS %s have "
283 				       "incompatible types\n",
284 				       sort_list[i].segment_name,
285 				       sort_list[i+1].segment_name);
286 				rc = -EINVAL;
287 				goto out;
288 			}
289 		}
290 	}
291 	rc = 0;
292 out:
293 	kfree(sort_list);
294 	return rc;
295 }
296 
297 /*
298  * Load a segment
299  */
300 static int
301 dcssblk_load_segment(char *name, struct segment_info **seg_info)
302 {
303 	int rc;
304 
305 	/* already loaded? */
306 	down_read(&dcssblk_devices_sem);
307 	*seg_info = dcssblk_get_segment_by_name(name);
308 	up_read(&dcssblk_devices_sem);
309 	if (*seg_info != NULL)
310 		return -EEXIST;
311 
312 	/* get a struct segment_info */
313 	*seg_info = kzalloc(sizeof(struct segment_info), GFP_KERNEL);
314 	if (*seg_info == NULL)
315 		return -ENOMEM;
316 
317 	strscpy((*seg_info)->segment_name, name);
318 
319 	/* load the segment */
320 	rc = segment_load(name, SEGMENT_SHARED,
321 			&(*seg_info)->start, &(*seg_info)->end);
322 	if (rc < 0) {
323 		segment_warning(rc, (*seg_info)->segment_name);
324 		kfree(*seg_info);
325 	} else {
326 		INIT_LIST_HEAD(&(*seg_info)->lh);
327 		(*seg_info)->segment_type = rc;
328 	}
329 	return rc;
330 }
331 
332 /*
333  * device attribute for switching shared/nonshared (exclusive)
334  * operation (show + store)
335  */
336 static ssize_t
337 dcssblk_shared_show(struct device *dev, struct device_attribute *attr, char *buf)
338 {
339 	struct dcssblk_dev_info *dev_info;
340 
341 	dev_info = container_of(dev, struct dcssblk_dev_info, dev);
342 	return sysfs_emit(buf, dev_info->is_shared ? "1\n" : "0\n");
343 }
344 
345 static ssize_t
346 dcssblk_shared_store(struct device *dev, struct device_attribute *attr, const char *inbuf, size_t count)
347 {
348 	struct dcssblk_dev_info *dev_info;
349 	struct segment_info *entry, *temp;
350 	int rc;
351 
352 	if ((count > 1) && (inbuf[1] != '\n') && (inbuf[1] != '\0'))
353 		return -EINVAL;
354 	down_write(&dcssblk_devices_sem);
355 	dev_info = container_of(dev, struct dcssblk_dev_info, dev);
356 	if (atomic_read(&dev_info->use_count)) {
357 		rc = -EBUSY;
358 		goto out;
359 	}
360 	if (inbuf[0] == '1') {
361 		/* reload segments in shared mode */
362 		list_for_each_entry(entry, &dev_info->seg_list, lh) {
363 			rc = segment_modify_shared(entry->segment_name,
364 						SEGMENT_SHARED);
365 			if (rc < 0) {
366 				BUG_ON(rc == -EINVAL);
367 				if (rc != -EAGAIN)
368 					goto removeseg;
369 			}
370 		}
371 		dev_info->is_shared = 1;
372 		switch (dev_info->segment_type) {
373 		case SEG_TYPE_SR:
374 		case SEG_TYPE_ER:
375 		case SEG_TYPE_SC:
376 			set_disk_ro(dev_info->gd, 1);
377 		}
378 	} else if (inbuf[0] == '0') {
379 		/* reload segments in exclusive mode */
380 		if (dev_info->segment_type == SEG_TYPE_SC) {
381 			pr_err("DCSS %s is of type SC and cannot be "
382 			       "loaded as exclusive-writable\n",
383 			       dev_info->segment_name);
384 			rc = -EINVAL;
385 			goto out;
386 		}
387 		list_for_each_entry(entry, &dev_info->seg_list, lh) {
388 			rc = segment_modify_shared(entry->segment_name,
389 						   SEGMENT_EXCLUSIVE);
390 			if (rc < 0) {
391 				BUG_ON(rc == -EINVAL);
392 				if (rc != -EAGAIN)
393 					goto removeseg;
394 			}
395 		}
396 		dev_info->is_shared = 0;
397 		set_disk_ro(dev_info->gd, 0);
398 	} else {
399 		rc = -EINVAL;
400 		goto out;
401 	}
402 	rc = count;
403 	goto out;
404 
405 removeseg:
406 	pr_err("DCSS device %s is removed after a failed access mode "
407 	       "change\n", dev_info->segment_name);
408 	temp = entry;
409 	list_for_each_entry(entry, &dev_info->seg_list, lh) {
410 		if (entry != temp)
411 			segment_unload(entry->segment_name);
412 	}
413 	list_del(&dev_info->lh);
414 	up_write(&dcssblk_devices_sem);
415 
416 	dax_remove_host(dev_info->gd);
417 	kill_dax(dev_info->dax_dev);
418 	put_dax(dev_info->dax_dev);
419 	if (dev_info->pgmap_addr)
420 		devm_memunmap_pages(&dev_info->dev, &dev_info->pgmap);
421 	del_gendisk(dev_info->gd);
422 	put_disk(dev_info->gd);
423 
424 	if (device_remove_file_self(dev, attr)) {
425 		device_unregister(dev);
426 		put_device(dev);
427 	}
428 	return rc;
429 out:
430 	up_write(&dcssblk_devices_sem);
431 	return rc;
432 }
433 static DEVICE_ATTR(shared, S_IWUSR | S_IRUSR, dcssblk_shared_show,
434 		   dcssblk_shared_store);
435 
436 /*
437  * device attribute for save operation on current copy
438  * of the segment. If the segment is busy, saving will
439  * become pending until it gets released, which can be
440  * undone by storing a non-true value to this entry.
441  * (show + store)
442  */
443 static ssize_t
444 dcssblk_save_show(struct device *dev, struct device_attribute *attr, char *buf)
445 {
446 	struct dcssblk_dev_info *dev_info;
447 
448 	dev_info = container_of(dev, struct dcssblk_dev_info, dev);
449 	return sysfs_emit(buf, dev_info->save_pending ? "1\n" : "0\n");
450 }
451 
452 static ssize_t
453 dcssblk_save_store(struct device *dev, struct device_attribute *attr, const char *inbuf, size_t count)
454 {
455 	struct dcssblk_dev_info *dev_info;
456 	struct segment_info *entry;
457 
458 	if ((count > 1) && (inbuf[1] != '\n') && (inbuf[1] != '\0'))
459 		return -EINVAL;
460 	dev_info = container_of(dev, struct dcssblk_dev_info, dev);
461 
462 	down_write(&dcssblk_devices_sem);
463 	if (inbuf[0] == '1') {
464 		if (atomic_read(&dev_info->use_count) == 0) {
465 			// device is idle => we save immediately
466 			pr_info("All DCSSs that map to device %s are "
467 				"saved\n", dev_info->segment_name);
468 			list_for_each_entry(entry, &dev_info->seg_list, lh) {
469 				if (entry->segment_type == SEG_TYPE_EN ||
470 				    entry->segment_type == SEG_TYPE_SN)
471 					pr_warn("DCSS %s is of type SN or EN"
472 						" and cannot be saved\n",
473 						entry->segment_name);
474 				else
475 					segment_save(entry->segment_name);
476 			}
477 		}  else {
478 			// device is busy => we save it when it becomes
479 			// idle in dcssblk_release
480 			pr_info("Device %s is in use, its DCSSs will be "
481 				"saved when it becomes idle\n",
482 				dev_info->segment_name);
483 			dev_info->save_pending = 1;
484 		}
485 	} else if (inbuf[0] == '0') {
486 		if (dev_info->save_pending) {
487 			// device is busy & the user wants to undo his save
488 			// request
489 			dev_info->save_pending = 0;
490 			pr_info("A pending save request for device %s "
491 				"has been canceled\n",
492 				dev_info->segment_name);
493 		}
494 	} else {
495 		up_write(&dcssblk_devices_sem);
496 		return -EINVAL;
497 	}
498 	up_write(&dcssblk_devices_sem);
499 	return count;
500 }
501 static DEVICE_ATTR(save, S_IWUSR | S_IRUSR, dcssblk_save_show,
502 		   dcssblk_save_store);
503 
504 /*
505  * device attribute for showing all segments in a device
506  */
507 static ssize_t
508 dcssblk_seglist_show(struct device *dev, struct device_attribute *attr,
509 		char *buf)
510 {
511 	struct dcssblk_dev_info *dev_info;
512 	struct segment_info *entry;
513 	int i;
514 
515 	i = 0;
516 	down_read(&dcssblk_devices_sem);
517 	dev_info = container_of(dev, struct dcssblk_dev_info, dev);
518 	list_for_each_entry(entry, &dev_info->seg_list, lh)
519 		i += sysfs_emit_at(buf, i, "%s\n", entry->segment_name);
520 	up_read(&dcssblk_devices_sem);
521 	return i;
522 }
523 static DEVICE_ATTR(seglist, S_IRUSR, dcssblk_seglist_show, NULL);
524 
525 static struct attribute *dcssblk_dev_attrs[] = {
526 	&dev_attr_shared.attr,
527 	&dev_attr_save.attr,
528 	&dev_attr_seglist.attr,
529 	NULL,
530 };
531 static struct attribute_group dcssblk_dev_attr_group = {
532 	.attrs = dcssblk_dev_attrs,
533 };
534 static const struct attribute_group *dcssblk_dev_attr_groups[] = {
535 	&dcssblk_dev_attr_group,
536 	NULL,
537 };
538 
539 static int dcssblk_setup_dax(struct dcssblk_dev_info *dev_info)
540 {
541 	struct dax_device *dax_dev;
542 
543 	dax_dev = alloc_dax(dev_info, &dcssblk_dax_ops);
544 	if (IS_ERR(dax_dev))
545 		return PTR_ERR(dax_dev);
546 	set_dax_synchronous(dax_dev);
547 	dev_info->dax_dev = dax_dev;
548 	return dax_add_host(dev_info->dax_dev, dev_info->gd);
549 }
550 
551 /*
552  * device attribute for adding devices
553  */
554 static ssize_t
555 dcssblk_add_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
556 {
557 	struct queue_limits lim = {
558 		.logical_block_size	= 4096,
559 		.features		= BLK_FEAT_DAX,
560 	};
561 	int rc, i, j, num_of_segments;
562 	struct dcssblk_dev_info *dev_info;
563 	struct segment_info *seg_info, *temp;
564 	char *local_buf;
565 	void *addr;
566 	unsigned long seg_byte_size;
567 
568 	dev_info = NULL;
569 	seg_info = NULL;
570 	if (dev != dcssblk_root_dev) {
571 		rc = -EINVAL;
572 		goto out_nobuf;
573 	}
574 	if ((count < 1) || (buf[0] == '\0') || (buf[0] == '\n')) {
575 		rc = -ENAMETOOLONG;
576 		goto out_nobuf;
577 	}
578 
579 	local_buf = kmalloc(count + 1, GFP_KERNEL);
580 	if (local_buf == NULL) {
581 		rc = -ENOMEM;
582 		goto out_nobuf;
583 	}
584 
585 	/*
586 	 * parse input
587 	 */
588 	num_of_segments = 0;
589 	for (i = 0; (i < count && (buf[i] != '\0') && (buf[i] != '\n')); i++) {
590 		for (j = i; j < count &&
591 			(buf[j] != ':') &&
592 			(buf[j] != '\0') &&
593 			(buf[j] != '\n'); j++) {
594 			local_buf[j-i] = toupper(buf[j]);
595 		}
596 		local_buf[j-i] = '\0';
597 		if (((j - i) == 0) || ((j - i) > 8)) {
598 			rc = -ENAMETOOLONG;
599 			goto seg_list_del;
600 		}
601 
602 		rc = dcssblk_load_segment(local_buf, &seg_info);
603 		if (rc < 0)
604 			goto seg_list_del;
605 		/*
606 		 * get a struct dcssblk_dev_info
607 		 */
608 		if (num_of_segments == 0) {
609 			dev_info = kzalloc(sizeof(struct dcssblk_dev_info),
610 					GFP_KERNEL);
611 			if (dev_info == NULL) {
612 				rc = -ENOMEM;
613 				goto out;
614 			}
615 			strscpy(dev_info->segment_name, local_buf);
616 			dev_info->segment_type = seg_info->segment_type;
617 			INIT_LIST_HEAD(&dev_info->seg_list);
618 		}
619 		list_add_tail(&seg_info->lh, &dev_info->seg_list);
620 		num_of_segments++;
621 		i = j;
622 
623 		if ((buf[j] == '\0') || (buf[j] == '\n'))
624 			break;
625 	}
626 
627 	/* no trailing colon at the end of the input */
628 	if ((i > 0) && (buf[i-1] == ':')) {
629 		rc = -ENAMETOOLONG;
630 		goto seg_list_del;
631 	}
632 	strscpy(local_buf, buf, i + 1);
633 	dev_info->num_of_segments = num_of_segments;
634 	rc = dcssblk_is_continuous(dev_info);
635 	if (rc < 0)
636 		goto seg_list_del;
637 
638 	dev_info->start = dcssblk_find_lowest_addr(dev_info);
639 	dev_info->end = dcssblk_find_highest_addr(dev_info);
640 
641 	dev_set_name(&dev_info->dev, "%s", dev_info->segment_name);
642 	dev_info->dev.release = dcssblk_release_segment;
643 	dev_info->dev.groups = dcssblk_dev_attr_groups;
644 	INIT_LIST_HEAD(&dev_info->lh);
645 	dev_info->gd = blk_alloc_disk(&lim, NUMA_NO_NODE);
646 	if (IS_ERR(dev_info->gd)) {
647 		rc = PTR_ERR(dev_info->gd);
648 		goto seg_list_del;
649 	}
650 	dev_info->gd->major = dcssblk_major;
651 	dev_info->gd->minors = DCSSBLK_MINORS_PER_DISK;
652 	dev_info->gd->fops = &dcssblk_devops;
653 	dev_info->gd->private_data = dev_info;
654 	dev_info->gd->flags |= GENHD_FL_NO_PART;
655 
656 	seg_byte_size = (dev_info->end - dev_info->start + 1);
657 	set_capacity(dev_info->gd, seg_byte_size >> 9); // size in sectors
658 	pr_info("Loaded %s with total size %lu bytes and capacity %lu "
659 		"sectors\n", local_buf, seg_byte_size, seg_byte_size >> 9);
660 
661 	dev_info->save_pending = 0;
662 	dev_info->is_shared = 1;
663 	dev_info->dev.parent = dcssblk_root_dev;
664 
665 	/*
666 	 *get minor, add to list
667 	 */
668 	down_write(&dcssblk_devices_sem);
669 	if (dcssblk_get_segment_by_name(local_buf)) {
670 		rc = -EEXIST;
671 		goto release_gd;
672 	}
673 	rc = dcssblk_assign_free_minor(dev_info);
674 	if (rc)
675 		goto release_gd;
676 	scnprintf(dev_info->gd->disk_name, sizeof(dev_info->gd->disk_name),
677 		  "dcssblk%d", dev_info->gd->first_minor);
678 	list_add_tail(&dev_info->lh, &dcssblk_devices);
679 
680 	if (!try_module_get(THIS_MODULE)) {
681 		rc = -ENODEV;
682 		goto dev_list_del;
683 	}
684 	/*
685 	 * register the device
686 	 */
687 	rc = device_register(&dev_info->dev);
688 	if (rc)
689 		goto put_dev;
690 
691 	if (!IS_ALIGNED(dev_info->start, SUBSECTION_SIZE) ||
692 	    !IS_ALIGNED(dev_info->end + 1, SUBSECTION_SIZE)) {
693 		pr_info("DCSS %s is not aligned to %lu bytes, DAX support disabled\n",
694 			local_buf, SUBSECTION_SIZE);
695 	} else {
696 		dev_info->pgmap.type		= MEMORY_DEVICE_FS_DAX;
697 		dev_info->pgmap.range.start	= dev_info->start;
698 		dev_info->pgmap.range.end	= dev_info->end;
699 		dev_info->pgmap.nr_range	= 1;
700 		addr = devm_memremap_pages(&dev_info->dev, &dev_info->pgmap);
701 		if (IS_ERR(addr)) {
702 			rc = PTR_ERR(addr);
703 			goto put_dev;
704 		}
705 		dev_info->pgmap_addr = addr;
706 		rc = dcssblk_setup_dax(dev_info);
707 		if (rc)
708 			goto out_dax;
709 		pr_info("DAX support enabled for DCSS %s\n", local_buf);
710 	}
711 
712 	get_device(&dev_info->dev);
713 	rc = device_add_disk(&dev_info->dev, dev_info->gd, NULL);
714 	if (rc)
715 		goto out_dax_host;
716 
717 	switch (dev_info->segment_type) {
718 		case SEG_TYPE_SR:
719 		case SEG_TYPE_ER:
720 		case SEG_TYPE_SC:
721 			set_disk_ro(dev_info->gd,1);
722 			break;
723 		default:
724 			set_disk_ro(dev_info->gd,0);
725 			break;
726 	}
727 	up_write(&dcssblk_devices_sem);
728 	rc = count;
729 	goto out;
730 
731 out_dax_host:
732 	put_device(&dev_info->dev);
733 	dax_remove_host(dev_info->gd);
734 out_dax:
735 	kill_dax(dev_info->dax_dev);
736 	put_dax(dev_info->dax_dev);
737 	if (dev_info->pgmap_addr)
738 		devm_memunmap_pages(&dev_info->dev, &dev_info->pgmap);
739 put_dev:
740 	list_del(&dev_info->lh);
741 	put_disk(dev_info->gd);
742 	list_for_each_entry(seg_info, &dev_info->seg_list, lh) {
743 		segment_unload(seg_info->segment_name);
744 	}
745 	put_device(&dev_info->dev);
746 	up_write(&dcssblk_devices_sem);
747 	goto out;
748 dev_list_del:
749 	list_del(&dev_info->lh);
750 release_gd:
751 	put_disk(dev_info->gd);
752 	up_write(&dcssblk_devices_sem);
753 seg_list_del:
754 	if (dev_info == NULL)
755 		goto out;
756 	list_for_each_entry_safe(seg_info, temp, &dev_info->seg_list, lh) {
757 		list_del(&seg_info->lh);
758 		segment_unload(seg_info->segment_name);
759 		kfree(seg_info);
760 	}
761 	kfree(dev_info);
762 out:
763 	kfree(local_buf);
764 out_nobuf:
765 	return rc;
766 }
767 
768 /*
769  * device attribute for removing devices
770  */
771 static ssize_t
772 dcssblk_remove_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
773 {
774 	struct dcssblk_dev_info *dev_info;
775 	struct segment_info *entry;
776 	int rc, i;
777 	char *local_buf;
778 
779 	if (dev != dcssblk_root_dev) {
780 		return -EINVAL;
781 	}
782 	local_buf = kmalloc(count + 1, GFP_KERNEL);
783 	if (local_buf == NULL) {
784 		return -ENOMEM;
785 	}
786 	/*
787 	 * parse input
788 	 */
789 	for (i = 0; (i < count && (*(buf+i)!='\0') && (*(buf+i)!='\n')); i++) {
790 		local_buf[i] = toupper(buf[i]);
791 	}
792 	local_buf[i] = '\0';
793 	if ((i == 0) || (i > 8)) {
794 		rc = -ENAMETOOLONG;
795 		goto out_buf;
796 	}
797 
798 	down_write(&dcssblk_devices_sem);
799 	dev_info = dcssblk_get_device_by_name(local_buf);
800 	if (dev_info == NULL) {
801 		up_write(&dcssblk_devices_sem);
802 		pr_warn("Device %s cannot be removed because it is not a known device\n",
803 			local_buf);
804 		rc = -ENODEV;
805 		goto out_buf;
806 	}
807 	if (atomic_read(&dev_info->use_count) != 0) {
808 		up_write(&dcssblk_devices_sem);
809 		pr_warn("Device %s cannot be removed while it is in use\n",
810 			local_buf);
811 		rc = -EBUSY;
812 		goto out_buf;
813 	}
814 
815 	list_del(&dev_info->lh);
816 	/* unload all related segments */
817 	list_for_each_entry(entry, &dev_info->seg_list, lh)
818 		segment_unload(entry->segment_name);
819 	up_write(&dcssblk_devices_sem);
820 
821 	dax_remove_host(dev_info->gd);
822 	kill_dax(dev_info->dax_dev);
823 	put_dax(dev_info->dax_dev);
824 	if (dev_info->pgmap_addr)
825 		devm_memunmap_pages(&dev_info->dev, &dev_info->pgmap);
826 	del_gendisk(dev_info->gd);
827 	put_disk(dev_info->gd);
828 
829 	device_unregister(&dev_info->dev);
830 	put_device(&dev_info->dev);
831 
832 	rc = count;
833 out_buf:
834 	kfree(local_buf);
835 	return rc;
836 }
837 
838 static int
839 dcssblk_open(struct gendisk *disk, blk_mode_t mode)
840 {
841 	struct dcssblk_dev_info *dev_info = disk->private_data;
842 	int rc;
843 
844 	if (NULL == dev_info) {
845 		rc = -ENODEV;
846 		goto out;
847 	}
848 	atomic_inc(&dev_info->use_count);
849 	rc = 0;
850 out:
851 	return rc;
852 }
853 
854 static void
855 dcssblk_release(struct gendisk *disk)
856 {
857 	struct dcssblk_dev_info *dev_info = disk->private_data;
858 	struct segment_info *entry;
859 
860 	if (!dev_info) {
861 		WARN_ON(1);
862 		return;
863 	}
864 	down_write(&dcssblk_devices_sem);
865 	if (atomic_dec_and_test(&dev_info->use_count)
866 	    && (dev_info->save_pending)) {
867 		pr_info("Device %s has become idle and is being saved "
868 			"now\n", dev_info->segment_name);
869 		list_for_each_entry(entry, &dev_info->seg_list, lh) {
870 			if (entry->segment_type == SEG_TYPE_EN ||
871 			    entry->segment_type == SEG_TYPE_SN)
872 				pr_warn("DCSS %s is of type SN or EN and cannot"
873 					" be saved\n", entry->segment_name);
874 			else
875 				segment_save(entry->segment_name);
876 		}
877 		dev_info->save_pending = 0;
878 	}
879 	up_write(&dcssblk_devices_sem);
880 }
881 
882 static void
883 dcssblk_submit_bio(struct bio *bio)
884 {
885 	struct dcssblk_dev_info *dev_info;
886 	struct bio_vec bvec;
887 	struct bvec_iter iter;
888 	unsigned long index;
889 	void *page_addr;
890 	unsigned long source_addr;
891 	unsigned long bytes_done;
892 
893 	bytes_done = 0;
894 	dev_info = bio->bi_bdev->bd_disk->private_data;
895 	if (dev_info == NULL)
896 		goto fail;
897 	if (!IS_ALIGNED(bio->bi_iter.bi_sector, 8) ||
898 	    !IS_ALIGNED(bio->bi_iter.bi_size, PAGE_SIZE))
899 		/* Request is not page-aligned. */
900 		goto fail;
901 	/* verify data transfer direction */
902 	if (dev_info->is_shared) {
903 		switch (dev_info->segment_type) {
904 		case SEG_TYPE_SR:
905 		case SEG_TYPE_ER:
906 		case SEG_TYPE_SC:
907 			/* cannot write to these segments */
908 			if (bio_data_dir(bio) == WRITE) {
909 				pr_warn("Writing to %s failed because it is a read-only device\n",
910 					dev_name(&dev_info->dev));
911 				goto fail;
912 			}
913 		}
914 	}
915 
916 	index = (bio->bi_iter.bi_sector >> 3);
917 	bio_for_each_segment(bvec, bio, iter) {
918 		page_addr = bvec_virt(&bvec);
919 		source_addr = dev_info->start + (index<<12) + bytes_done;
920 		if (unlikely(!IS_ALIGNED((unsigned long)page_addr, PAGE_SIZE) ||
921 			     !IS_ALIGNED(bvec.bv_len, PAGE_SIZE)))
922 			// More paranoia.
923 			goto fail;
924 		if (bio_data_dir(bio) == READ)
925 			memcpy(page_addr, __va(source_addr), bvec.bv_len);
926 		else
927 			memcpy(__va(source_addr), page_addr, bvec.bv_len);
928 		bytes_done += bvec.bv_len;
929 	}
930 	bio_endio(bio);
931 	return;
932 fail:
933 	bio_io_error(bio);
934 }
935 
936 static long
937 __dcssblk_direct_access(struct dcssblk_dev_info *dev_info, pgoff_t pgoff,
938 		long nr_pages, void **kaddr, unsigned long *pfn)
939 {
940 	resource_size_t offset = pgoff * PAGE_SIZE;
941 	unsigned long dev_sz;
942 
943 	dev_sz = dev_info->end - dev_info->start + 1;
944 	if (kaddr)
945 		*kaddr = __va(dev_info->start + offset);
946 	if (pfn)
947 		*pfn = PFN_DOWN(dev_info->start + offset);
948 
949 	return (dev_sz - offset) / PAGE_SIZE;
950 }
951 
952 static long
953 dcssblk_dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff,
954 		long nr_pages, enum dax_access_mode mode, void **kaddr,
955 		unsigned long *pfn)
956 {
957 	struct dcssblk_dev_info *dev_info = dax_get_private(dax_dev);
958 
959 	return __dcssblk_direct_access(dev_info, pgoff, nr_pages, kaddr, pfn);
960 }
961 
962 static void
963 dcssblk_check_params(void)
964 {
965 	int rc, i, j, k;
966 	char buf[DCSSBLK_PARM_LEN + 1];
967 	struct dcssblk_dev_info *dev_info;
968 
969 	for (i = 0; (i < DCSSBLK_PARM_LEN) && (dcssblk_segments[i] != '\0');
970 	     i++) {
971 		for (j = i; (j < DCSSBLK_PARM_LEN) &&
972 			    (dcssblk_segments[j] != ',')  &&
973 			    (dcssblk_segments[j] != '\0') &&
974 			    (dcssblk_segments[j] != '('); j++)
975 		{
976 			buf[j-i] = dcssblk_segments[j];
977 		}
978 		buf[j-i] = '\0';
979 		rc = dcssblk_add_store(dcssblk_root_dev, NULL, buf, j-i);
980 		if ((rc >= 0) && (dcssblk_segments[j] == '(')) {
981 			for (k = 0; (buf[k] != ':') && (buf[k] != '\0'); k++)
982 				buf[k] = toupper(buf[k]);
983 			buf[k] = '\0';
984 			if (!strncmp(&dcssblk_segments[j], "(local)", 7)) {
985 				down_read(&dcssblk_devices_sem);
986 				dev_info = dcssblk_get_device_by_name(buf);
987 				up_read(&dcssblk_devices_sem);
988 				if (dev_info)
989 					dcssblk_shared_store(&dev_info->dev,
990 							     NULL, "0\n", 2);
991 			}
992 		}
993 		while ((dcssblk_segments[j] != ',') &&
994 		       (dcssblk_segments[j] != '\0'))
995 		{
996 			j++;
997 		}
998 		if (dcssblk_segments[j] == '\0')
999 			break;
1000 		i = j;
1001 	}
1002 }
1003 
1004 /*
1005  * The init/exit functions.
1006  */
1007 static void __exit
1008 dcssblk_exit(void)
1009 {
1010 	root_device_unregister(dcssblk_root_dev);
1011 	unregister_blkdev(dcssblk_major, DCSSBLK_NAME);
1012 }
1013 
1014 static int __init
1015 dcssblk_init(void)
1016 {
1017 	int rc;
1018 
1019 	dcssblk_root_dev = root_device_register("dcssblk");
1020 	if (IS_ERR(dcssblk_root_dev))
1021 		return PTR_ERR(dcssblk_root_dev);
1022 	rc = device_create_file(dcssblk_root_dev, &dev_attr_add);
1023 	if (rc)
1024 		goto out_root;
1025 	rc = device_create_file(dcssblk_root_dev, &dev_attr_remove);
1026 	if (rc)
1027 		goto out_root;
1028 	rc = register_blkdev(0, DCSSBLK_NAME);
1029 	if (rc < 0)
1030 		goto out_root;
1031 	dcssblk_major = rc;
1032 	init_rwsem(&dcssblk_devices_sem);
1033 
1034 	dcssblk_check_params();
1035 	return 0;
1036 
1037 out_root:
1038 	root_device_unregister(dcssblk_root_dev);
1039 
1040 	return rc;
1041 }
1042 
1043 module_init(dcssblk_init);
1044 module_exit(dcssblk_exit);
1045 
1046 module_param_string(segments, dcssblk_segments, DCSSBLK_PARM_LEN, 0444);
1047 MODULE_PARM_DESC(segments, "Name of DCSS segment(s) to be loaded, "
1048 		 "comma-separated list, names in each set separated "
1049 		 "by commas are separated by colons, each set contains "
1050 		 "names of contiguous segments and each name max. 8 chars.\n"
1051 		 "Adding \"(local)\" to the end of each set equals echoing 0 "
1052 		 "to /sys/devices/dcssblk/<device name>/shared after loading "
1053 		 "the contiguous segments - \n"
1054 		 "e.g. segments=\"mydcss1,mydcss2:mydcss3,mydcss4(local)\"");
1055 
1056 MODULE_DESCRIPTION("S/390 block driver for DCSS memory");
1057 MODULE_LICENSE("GPL");
1058