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