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