Linux 4.16.11
[linux/fpc-iii.git] / drivers / s390 / block / dcssblk.c
blob9cae08b36b8022b590bdb550113212d102d94043
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * dcssblk.c -- the S/390 block driver for dcss memory
5 * Authors: Carsten Otte, Stefan Weinhuber, Gerald Schaefer
6 */
8 #define KMSG_COMPONENT "dcssblk"
9 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
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>
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
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);
39 static char dcssblk_segments[DCSSBLK_PARM_LEN] = "\0";
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,
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)
51 return copy_from_iter(addr, bytes, i);
54 static const struct dax_operations dcssblk_dax_ops = {
55 .direct_access = dcssblk_dax_direct_access,
56 .copy_from_iter = dcssblk_dax_copy_from_iter,
59 struct dcssblk_dev_info {
60 struct list_head lh;
61 struct device dev;
62 char segment_name[DCSS_BUS_ID_SIZE];
63 atomic_t use_count;
64 struct gendisk *gd;
65 unsigned long start;
66 unsigned long end;
67 int segment_type;
68 unsigned char save_pending;
69 unsigned char is_shared;
70 struct request_queue *dcssblk_queue;
71 int num_of_segments;
72 struct list_head seg_list;
73 struct dax_device *dax_dev;
76 struct segment_info {
77 struct list_head lh;
78 char segment_name[DCSS_BUS_ID_SIZE];
79 unsigned long start;
80 unsigned long end;
81 int segment_type;
84 static ssize_t dcssblk_add_store(struct device * dev, struct device_attribute *attr, const char * buf,
85 size_t count);
86 static ssize_t dcssblk_remove_store(struct device * dev, struct device_attribute *attr, const char * buf,
87 size_t count);
89 static DEVICE_ATTR(add, S_IWUSR, NULL, dcssblk_add_store);
90 static DEVICE_ATTR(remove, S_IWUSR, NULL, dcssblk_remove_store);
92 static struct device *dcssblk_root_dev;
94 static LIST_HEAD(dcssblk_devices);
95 static struct rw_semaphore dcssblk_devices_sem;
98 * release function for segment device.
100 static void
101 dcssblk_release_segment(struct device *dev)
103 struct dcssblk_dev_info *dev_info;
104 struct segment_info *entry, *temp;
106 dev_info = container_of(dev, struct dcssblk_dev_info, dev);
107 list_for_each_entry_safe(entry, temp, &dev_info->seg_list, lh) {
108 list_del(&entry->lh);
109 kfree(entry);
111 kfree(dev_info);
112 module_put(THIS_MODULE);
116 * get a minor number. needs to be called with
117 * down_write(&dcssblk_devices_sem) and the
118 * device needs to be enqueued before the semaphore is
119 * freed.
121 static int
122 dcssblk_assign_free_minor(struct dcssblk_dev_info *dev_info)
124 int minor, found;
125 struct dcssblk_dev_info *entry;
127 if (dev_info == NULL)
128 return -EINVAL;
129 for (minor = 0; minor < (1<<MINORBITS); minor++) {
130 found = 0;
131 // test if minor available
132 list_for_each_entry(entry, &dcssblk_devices, lh)
133 if (minor == entry->gd->first_minor)
134 found++;
135 if (!found) break; // got unused minor
137 if (found)
138 return -EBUSY;
139 dev_info->gd->first_minor = minor;
140 return 0;
144 * get the struct dcssblk_dev_info from dcssblk_devices
145 * for the given name.
146 * down_read(&dcssblk_devices_sem) must be held.
148 static struct dcssblk_dev_info *
149 dcssblk_get_device_by_name(char *name)
151 struct dcssblk_dev_info *entry;
153 list_for_each_entry(entry, &dcssblk_devices, lh) {
154 if (!strcmp(name, entry->segment_name)) {
155 return entry;
158 return NULL;
162 * get the struct segment_info from seg_list
163 * for the given name.
164 * down_read(&dcssblk_devices_sem) must be held.
166 static struct segment_info *
167 dcssblk_get_segment_by_name(char *name)
169 struct dcssblk_dev_info *dev_info;
170 struct segment_info *entry;
172 list_for_each_entry(dev_info, &dcssblk_devices, lh) {
173 list_for_each_entry(entry, &dev_info->seg_list, lh) {
174 if (!strcmp(name, entry->segment_name))
175 return entry;
178 return NULL;
182 * get the highest address of the multi-segment block.
184 static unsigned long
185 dcssblk_find_highest_addr(struct dcssblk_dev_info *dev_info)
187 unsigned long highest_addr;
188 struct segment_info *entry;
190 highest_addr = 0;
191 list_for_each_entry(entry, &dev_info->seg_list, lh) {
192 if (highest_addr < entry->end)
193 highest_addr = entry->end;
195 return highest_addr;
199 * get the lowest address of the multi-segment block.
201 static unsigned long
202 dcssblk_find_lowest_addr(struct dcssblk_dev_info *dev_info)
204 int set_first;
205 unsigned long lowest_addr;
206 struct segment_info *entry;
208 set_first = 0;
209 lowest_addr = 0;
210 list_for_each_entry(entry, &dev_info->seg_list, lh) {
211 if (set_first == 0) {
212 lowest_addr = entry->start;
213 set_first = 1;
214 } else {
215 if (lowest_addr > entry->start)
216 lowest_addr = entry->start;
219 return lowest_addr;
223 * Check continuity of segments.
225 static int
226 dcssblk_is_continuous(struct dcssblk_dev_info *dev_info)
228 int i, j, rc;
229 struct segment_info *sort_list, *entry, temp;
231 if (dev_info->num_of_segments <= 1)
232 return 0;
234 sort_list = kzalloc(
235 sizeof(struct segment_info) * dev_info->num_of_segments,
236 GFP_KERNEL);
237 if (sort_list == NULL)
238 return -ENOMEM;
239 i = 0;
240 list_for_each_entry(entry, &dev_info->seg_list, lh) {
241 memcpy(&sort_list[i], entry, sizeof(struct segment_info));
242 i++;
245 /* sort segments */
246 for (i = 0; i < dev_info->num_of_segments; i++)
247 for (j = 0; j < dev_info->num_of_segments; j++)
248 if (sort_list[j].start > sort_list[i].start) {
249 memcpy(&temp, &sort_list[i],
250 sizeof(struct segment_info));
251 memcpy(&sort_list[i], &sort_list[j],
252 sizeof(struct segment_info));
253 memcpy(&sort_list[j], &temp,
254 sizeof(struct segment_info));
257 /* check continuity */
258 for (i = 0; i < dev_info->num_of_segments - 1; i++) {
259 if ((sort_list[i].end + 1) != sort_list[i+1].start) {
260 pr_err("Adjacent DCSSs %s and %s are not "
261 "contiguous\n", sort_list[i].segment_name,
262 sort_list[i+1].segment_name);
263 rc = -EINVAL;
264 goto out;
266 /* EN and EW are allowed in a block device */
267 if (sort_list[i].segment_type != sort_list[i+1].segment_type) {
268 if (!(sort_list[i].segment_type & SEGMENT_EXCLUSIVE) ||
269 (sort_list[i].segment_type == SEG_TYPE_ER) ||
270 !(sort_list[i+1].segment_type &
271 SEGMENT_EXCLUSIVE) ||
272 (sort_list[i+1].segment_type == SEG_TYPE_ER)) {
273 pr_err("DCSS %s and DCSS %s have "
274 "incompatible types\n",
275 sort_list[i].segment_name,
276 sort_list[i+1].segment_name);
277 rc = -EINVAL;
278 goto out;
282 rc = 0;
283 out:
284 kfree(sort_list);
285 return rc;
289 * Load a segment
291 static int
292 dcssblk_load_segment(char *name, struct segment_info **seg_info)
294 int rc;
296 /* already loaded? */
297 down_read(&dcssblk_devices_sem);
298 *seg_info = dcssblk_get_segment_by_name(name);
299 up_read(&dcssblk_devices_sem);
300 if (*seg_info != NULL)
301 return -EEXIST;
303 /* get a struct segment_info */
304 *seg_info = kzalloc(sizeof(struct segment_info), GFP_KERNEL);
305 if (*seg_info == NULL)
306 return -ENOMEM;
308 strcpy((*seg_info)->segment_name, name);
310 /* load the segment */
311 rc = segment_load(name, SEGMENT_SHARED,
312 &(*seg_info)->start, &(*seg_info)->end);
313 if (rc < 0) {
314 segment_warning(rc, (*seg_info)->segment_name);
315 kfree(*seg_info);
316 } else {
317 INIT_LIST_HEAD(&(*seg_info)->lh);
318 (*seg_info)->segment_type = rc;
320 return rc;
324 * device attribute for switching shared/nonshared (exclusive)
325 * operation (show + store)
327 static ssize_t
328 dcssblk_shared_show(struct device *dev, struct device_attribute *attr, char *buf)
330 struct dcssblk_dev_info *dev_info;
332 dev_info = container_of(dev, struct dcssblk_dev_info, dev);
333 return sprintf(buf, dev_info->is_shared ? "1\n" : "0\n");
336 static ssize_t
337 dcssblk_shared_store(struct device *dev, struct device_attribute *attr, const char *inbuf, size_t count)
339 struct dcssblk_dev_info *dev_info;
340 struct segment_info *entry, *temp;
341 int rc;
343 if ((count > 1) && (inbuf[1] != '\n') && (inbuf[1] != '\0'))
344 return -EINVAL;
345 down_write(&dcssblk_devices_sem);
346 dev_info = container_of(dev, struct dcssblk_dev_info, dev);
347 if (atomic_read(&dev_info->use_count)) {
348 rc = -EBUSY;
349 goto out;
351 if (inbuf[0] == '1') {
352 /* reload segments in shared mode */
353 list_for_each_entry(entry, &dev_info->seg_list, lh) {
354 rc = segment_modify_shared(entry->segment_name,
355 SEGMENT_SHARED);
356 if (rc < 0) {
357 BUG_ON(rc == -EINVAL);
358 if (rc != -EAGAIN)
359 goto removeseg;
362 dev_info->is_shared = 1;
363 switch (dev_info->segment_type) {
364 case SEG_TYPE_SR:
365 case SEG_TYPE_ER:
366 case SEG_TYPE_SC:
367 set_disk_ro(dev_info->gd, 1);
369 } else if (inbuf[0] == '0') {
370 /* reload segments in exclusive mode */
371 if (dev_info->segment_type == SEG_TYPE_SC) {
372 pr_err("DCSS %s is of type SC and cannot be "
373 "loaded as exclusive-writable\n",
374 dev_info->segment_name);
375 rc = -EINVAL;
376 goto out;
378 list_for_each_entry(entry, &dev_info->seg_list, lh) {
379 rc = segment_modify_shared(entry->segment_name,
380 SEGMENT_EXCLUSIVE);
381 if (rc < 0) {
382 BUG_ON(rc == -EINVAL);
383 if (rc != -EAGAIN)
384 goto removeseg;
387 dev_info->is_shared = 0;
388 set_disk_ro(dev_info->gd, 0);
389 } else {
390 rc = -EINVAL;
391 goto out;
393 rc = count;
394 goto out;
396 removeseg:
397 pr_err("DCSS device %s is removed after a failed access mode "
398 "change\n", dev_info->segment_name);
399 temp = entry;
400 list_for_each_entry(entry, &dev_info->seg_list, lh) {
401 if (entry != temp)
402 segment_unload(entry->segment_name);
404 list_del(&dev_info->lh);
406 kill_dax(dev_info->dax_dev);
407 put_dax(dev_info->dax_dev);
408 del_gendisk(dev_info->gd);
409 blk_cleanup_queue(dev_info->dcssblk_queue);
410 dev_info->gd->queue = NULL;
411 put_disk(dev_info->gd);
412 up_write(&dcssblk_devices_sem);
414 if (device_remove_file_self(dev, attr)) {
415 device_unregister(dev);
416 put_device(dev);
418 return rc;
419 out:
420 up_write(&dcssblk_devices_sem);
421 return rc;
423 static DEVICE_ATTR(shared, S_IWUSR | S_IRUSR, dcssblk_shared_show,
424 dcssblk_shared_store);
427 * device attribute for save operation on current copy
428 * of the segment. If the segment is busy, saving will
429 * become pending until it gets released, which can be
430 * undone by storing a non-true value to this entry.
431 * (show + store)
433 static ssize_t
434 dcssblk_save_show(struct device *dev, struct device_attribute *attr, char *buf)
436 struct dcssblk_dev_info *dev_info;
438 dev_info = container_of(dev, struct dcssblk_dev_info, dev);
439 return sprintf(buf, dev_info->save_pending ? "1\n" : "0\n");
442 static ssize_t
443 dcssblk_save_store(struct device *dev, struct device_attribute *attr, const char *inbuf, size_t count)
445 struct dcssblk_dev_info *dev_info;
446 struct segment_info *entry;
448 if ((count > 1) && (inbuf[1] != '\n') && (inbuf[1] != '\0'))
449 return -EINVAL;
450 dev_info = container_of(dev, struct dcssblk_dev_info, dev);
452 down_write(&dcssblk_devices_sem);
453 if (inbuf[0] == '1') {
454 if (atomic_read(&dev_info->use_count) == 0) {
455 // device is idle => we save immediately
456 pr_info("All DCSSs that map to device %s are "
457 "saved\n", dev_info->segment_name);
458 list_for_each_entry(entry, &dev_info->seg_list, lh) {
459 if (entry->segment_type == SEG_TYPE_EN ||
460 entry->segment_type == SEG_TYPE_SN)
461 pr_warn("DCSS %s is of type SN or EN"
462 " and cannot be saved\n",
463 entry->segment_name);
464 else
465 segment_save(entry->segment_name);
467 } else {
468 // device is busy => we save it when it becomes
469 // idle in dcssblk_release
470 pr_info("Device %s is in use, its DCSSs will be "
471 "saved when it becomes idle\n",
472 dev_info->segment_name);
473 dev_info->save_pending = 1;
475 } else if (inbuf[0] == '0') {
476 if (dev_info->save_pending) {
477 // device is busy & the user wants to undo his save
478 // request
479 dev_info->save_pending = 0;
480 pr_info("A pending save request for device %s "
481 "has been canceled\n",
482 dev_info->segment_name);
484 } else {
485 up_write(&dcssblk_devices_sem);
486 return -EINVAL;
488 up_write(&dcssblk_devices_sem);
489 return count;
491 static DEVICE_ATTR(save, S_IWUSR | S_IRUSR, dcssblk_save_show,
492 dcssblk_save_store);
495 * device attribute for showing all segments in a device
497 static ssize_t
498 dcssblk_seglist_show(struct device *dev, struct device_attribute *attr,
499 char *buf)
501 int i;
503 struct dcssblk_dev_info *dev_info;
504 struct segment_info *entry;
506 down_read(&dcssblk_devices_sem);
507 dev_info = container_of(dev, struct dcssblk_dev_info, dev);
508 i = 0;
509 buf[0] = '\0';
510 list_for_each_entry(entry, &dev_info->seg_list, lh) {
511 strcpy(&buf[i], entry->segment_name);
512 i += strlen(entry->segment_name);
513 buf[i] = '\n';
514 i++;
516 up_read(&dcssblk_devices_sem);
517 return i;
519 static DEVICE_ATTR(seglist, S_IRUSR, dcssblk_seglist_show, NULL);
521 static struct attribute *dcssblk_dev_attrs[] = {
522 &dev_attr_shared.attr,
523 &dev_attr_save.attr,
524 &dev_attr_seglist.attr,
525 NULL,
527 static struct attribute_group dcssblk_dev_attr_group = {
528 .attrs = dcssblk_dev_attrs,
530 static const struct attribute_group *dcssblk_dev_attr_groups[] = {
531 &dcssblk_dev_attr_group,
532 NULL,
536 * device attribute for adding devices
538 static ssize_t
539 dcssblk_add_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
541 int rc, i, j, num_of_segments;
542 struct dcssblk_dev_info *dev_info;
543 struct segment_info *seg_info, *temp;
544 char *local_buf;
545 unsigned long seg_byte_size;
547 dev_info = NULL;
548 seg_info = NULL;
549 if (dev != dcssblk_root_dev) {
550 rc = -EINVAL;
551 goto out_nobuf;
553 if ((count < 1) || (buf[0] == '\0') || (buf[0] == '\n')) {
554 rc = -ENAMETOOLONG;
555 goto out_nobuf;
558 local_buf = kmalloc(count + 1, GFP_KERNEL);
559 if (local_buf == NULL) {
560 rc = -ENOMEM;
561 goto out_nobuf;
565 * parse input
567 num_of_segments = 0;
568 for (i = 0; (i < count && (buf[i] != '\0') && (buf[i] != '\n')); i++) {
569 for (j = i; j < count &&
570 (buf[j] != ':') &&
571 (buf[j] != '\0') &&
572 (buf[j] != '\n'); j++) {
573 local_buf[j-i] = toupper(buf[j]);
575 local_buf[j-i] = '\0';
576 if (((j - i) == 0) || ((j - i) > 8)) {
577 rc = -ENAMETOOLONG;
578 goto seg_list_del;
581 rc = dcssblk_load_segment(local_buf, &seg_info);
582 if (rc < 0)
583 goto seg_list_del;
585 * get a struct dcssblk_dev_info
587 if (num_of_segments == 0) {
588 dev_info = kzalloc(sizeof(struct dcssblk_dev_info),
589 GFP_KERNEL);
590 if (dev_info == NULL) {
591 rc = -ENOMEM;
592 goto out;
594 strcpy(dev_info->segment_name, local_buf);
595 dev_info->segment_type = seg_info->segment_type;
596 INIT_LIST_HEAD(&dev_info->seg_list);
598 list_add_tail(&seg_info->lh, &dev_info->seg_list);
599 num_of_segments++;
600 i = j;
602 if ((buf[j] == '\0') || (buf[j] == '\n'))
603 break;
606 /* no trailing colon at the end of the input */
607 if ((i > 0) && (buf[i-1] == ':')) {
608 rc = -ENAMETOOLONG;
609 goto seg_list_del;
611 strlcpy(local_buf, buf, i + 1);
612 dev_info->num_of_segments = num_of_segments;
613 rc = dcssblk_is_continuous(dev_info);
614 if (rc < 0)
615 goto seg_list_del;
617 dev_info->start = dcssblk_find_lowest_addr(dev_info);
618 dev_info->end = dcssblk_find_highest_addr(dev_info);
620 dev_set_name(&dev_info->dev, "%s", dev_info->segment_name);
621 dev_info->dev.release = dcssblk_release_segment;
622 dev_info->dev.groups = dcssblk_dev_attr_groups;
623 INIT_LIST_HEAD(&dev_info->lh);
624 dev_info->gd = alloc_disk(DCSSBLK_MINORS_PER_DISK);
625 if (dev_info->gd == NULL) {
626 rc = -ENOMEM;
627 goto seg_list_del;
629 dev_info->gd->major = dcssblk_major;
630 dev_info->gd->fops = &dcssblk_devops;
631 dev_info->dcssblk_queue = blk_alloc_queue(GFP_KERNEL);
632 dev_info->gd->queue = dev_info->dcssblk_queue;
633 dev_info->gd->private_data = dev_info;
634 blk_queue_make_request(dev_info->dcssblk_queue, dcssblk_make_request);
635 blk_queue_logical_block_size(dev_info->dcssblk_queue, 4096);
636 queue_flag_set_unlocked(QUEUE_FLAG_DAX, dev_info->dcssblk_queue);
638 seg_byte_size = (dev_info->end - dev_info->start + 1);
639 set_capacity(dev_info->gd, seg_byte_size >> 9); // size in sectors
640 pr_info("Loaded %s with total size %lu bytes and capacity %lu "
641 "sectors\n", local_buf, seg_byte_size, seg_byte_size >> 9);
643 dev_info->save_pending = 0;
644 dev_info->is_shared = 1;
645 dev_info->dev.parent = dcssblk_root_dev;
648 *get minor, add to list
650 down_write(&dcssblk_devices_sem);
651 if (dcssblk_get_segment_by_name(local_buf)) {
652 rc = -EEXIST;
653 goto release_gd;
655 rc = dcssblk_assign_free_minor(dev_info);
656 if (rc)
657 goto release_gd;
658 sprintf(dev_info->gd->disk_name, "dcssblk%d",
659 dev_info->gd->first_minor);
660 list_add_tail(&dev_info->lh, &dcssblk_devices);
662 if (!try_module_get(THIS_MODULE)) {
663 rc = -ENODEV;
664 goto dev_list_del;
667 * register the device
669 rc = device_register(&dev_info->dev);
670 if (rc)
671 goto put_dev;
673 dev_info->dax_dev = alloc_dax(dev_info, dev_info->gd->disk_name,
674 &dcssblk_dax_ops);
675 if (!dev_info->dax_dev) {
676 rc = -ENOMEM;
677 goto put_dev;
680 get_device(&dev_info->dev);
681 device_add_disk(&dev_info->dev, dev_info->gd);
683 switch (dev_info->segment_type) {
684 case SEG_TYPE_SR:
685 case SEG_TYPE_ER:
686 case SEG_TYPE_SC:
687 set_disk_ro(dev_info->gd,1);
688 break;
689 default:
690 set_disk_ro(dev_info->gd,0);
691 break;
693 up_write(&dcssblk_devices_sem);
694 rc = count;
695 goto out;
697 put_dev:
698 list_del(&dev_info->lh);
699 blk_cleanup_queue(dev_info->dcssblk_queue);
700 dev_info->gd->queue = NULL;
701 put_disk(dev_info->gd);
702 list_for_each_entry(seg_info, &dev_info->seg_list, lh) {
703 segment_unload(seg_info->segment_name);
705 put_device(&dev_info->dev);
706 up_write(&dcssblk_devices_sem);
707 goto out;
708 dev_list_del:
709 list_del(&dev_info->lh);
710 release_gd:
711 blk_cleanup_queue(dev_info->dcssblk_queue);
712 dev_info->gd->queue = NULL;
713 put_disk(dev_info->gd);
714 up_write(&dcssblk_devices_sem);
715 seg_list_del:
716 if (dev_info == NULL)
717 goto out;
718 list_for_each_entry_safe(seg_info, temp, &dev_info->seg_list, lh) {
719 list_del(&seg_info->lh);
720 segment_unload(seg_info->segment_name);
721 kfree(seg_info);
723 kfree(dev_info);
724 out:
725 kfree(local_buf);
726 out_nobuf:
727 return rc;
731 * device attribute for removing devices
733 static ssize_t
734 dcssblk_remove_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
736 struct dcssblk_dev_info *dev_info;
737 struct segment_info *entry;
738 int rc, i;
739 char *local_buf;
741 if (dev != dcssblk_root_dev) {
742 return -EINVAL;
744 local_buf = kmalloc(count + 1, GFP_KERNEL);
745 if (local_buf == NULL) {
746 return -ENOMEM;
749 * parse input
751 for (i = 0; (i < count && (*(buf+i)!='\0') && (*(buf+i)!='\n')); i++) {
752 local_buf[i] = toupper(buf[i]);
754 local_buf[i] = '\0';
755 if ((i == 0) || (i > 8)) {
756 rc = -ENAMETOOLONG;
757 goto out_buf;
760 down_write(&dcssblk_devices_sem);
761 dev_info = dcssblk_get_device_by_name(local_buf);
762 if (dev_info == NULL) {
763 up_write(&dcssblk_devices_sem);
764 pr_warn("Device %s cannot be removed because it is not a known device\n",
765 local_buf);
766 rc = -ENODEV;
767 goto out_buf;
769 if (atomic_read(&dev_info->use_count) != 0) {
770 up_write(&dcssblk_devices_sem);
771 pr_warn("Device %s cannot be removed while it is in use\n",
772 local_buf);
773 rc = -EBUSY;
774 goto out_buf;
777 list_del(&dev_info->lh);
778 kill_dax(dev_info->dax_dev);
779 put_dax(dev_info->dax_dev);
780 del_gendisk(dev_info->gd);
781 blk_cleanup_queue(dev_info->dcssblk_queue);
782 dev_info->gd->queue = NULL;
783 put_disk(dev_info->gd);
785 /* unload all related segments */
786 list_for_each_entry(entry, &dev_info->seg_list, lh)
787 segment_unload(entry->segment_name);
789 up_write(&dcssblk_devices_sem);
791 device_unregister(&dev_info->dev);
792 put_device(&dev_info->dev);
794 rc = count;
795 out_buf:
796 kfree(local_buf);
797 return rc;
800 static int
801 dcssblk_open(struct block_device *bdev, fmode_t mode)
803 struct dcssblk_dev_info *dev_info;
804 int rc;
806 dev_info = bdev->bd_disk->private_data;
807 if (NULL == dev_info) {
808 rc = -ENODEV;
809 goto out;
811 atomic_inc(&dev_info->use_count);
812 bdev->bd_block_size = 4096;
813 rc = 0;
814 out:
815 return rc;
818 static void
819 dcssblk_release(struct gendisk *disk, fmode_t mode)
821 struct dcssblk_dev_info *dev_info = disk->private_data;
822 struct segment_info *entry;
824 if (!dev_info) {
825 WARN_ON(1);
826 return;
828 down_write(&dcssblk_devices_sem);
829 if (atomic_dec_and_test(&dev_info->use_count)
830 && (dev_info->save_pending)) {
831 pr_info("Device %s has become idle and is being saved "
832 "now\n", dev_info->segment_name);
833 list_for_each_entry(entry, &dev_info->seg_list, lh) {
834 if (entry->segment_type == SEG_TYPE_EN ||
835 entry->segment_type == SEG_TYPE_SN)
836 pr_warn("DCSS %s is of type SN or EN and cannot"
837 " be saved\n", entry->segment_name);
838 else
839 segment_save(entry->segment_name);
841 dev_info->save_pending = 0;
843 up_write(&dcssblk_devices_sem);
846 static blk_qc_t
847 dcssblk_make_request(struct request_queue *q, struct bio *bio)
849 struct dcssblk_dev_info *dev_info;
850 struct bio_vec bvec;
851 struct bvec_iter iter;
852 unsigned long index;
853 unsigned long page_addr;
854 unsigned long source_addr;
855 unsigned long bytes_done;
857 blk_queue_split(q, &bio);
859 bytes_done = 0;
860 dev_info = bio->bi_disk->private_data;
861 if (dev_info == NULL)
862 goto fail;
863 if ((bio->bi_iter.bi_sector & 7) != 0 ||
864 (bio->bi_iter.bi_size & 4095) != 0)
865 /* Request is not page-aligned. */
866 goto fail;
867 if (bio_end_sector(bio) > get_capacity(bio->bi_disk)) {
868 /* Request beyond end of DCSS segment. */
869 goto fail;
871 /* verify data transfer direction */
872 if (dev_info->is_shared) {
873 switch (dev_info->segment_type) {
874 case SEG_TYPE_SR:
875 case SEG_TYPE_ER:
876 case SEG_TYPE_SC:
877 /* cannot write to these segments */
878 if (bio_data_dir(bio) == WRITE) {
879 pr_warn("Writing to %s failed because it is a read-only device\n",
880 dev_name(&dev_info->dev));
881 goto fail;
886 index = (bio->bi_iter.bi_sector >> 3);
887 bio_for_each_segment(bvec, bio, iter) {
888 page_addr = (unsigned long)
889 page_address(bvec.bv_page) + bvec.bv_offset;
890 source_addr = dev_info->start + (index<<12) + bytes_done;
891 if (unlikely((page_addr & 4095) != 0) || (bvec.bv_len & 4095) != 0)
892 // More paranoia.
893 goto fail;
894 if (bio_data_dir(bio) == READ) {
895 memcpy((void*)page_addr, (void*)source_addr,
896 bvec.bv_len);
897 } else {
898 memcpy((void*)source_addr, (void*)page_addr,
899 bvec.bv_len);
901 bytes_done += bvec.bv_len;
903 bio_endio(bio);
904 return BLK_QC_T_NONE;
905 fail:
906 bio_io_error(bio);
907 return BLK_QC_T_NONE;
910 static long
911 __dcssblk_direct_access(struct dcssblk_dev_info *dev_info, pgoff_t pgoff,
912 long nr_pages, void **kaddr, pfn_t *pfn)
914 resource_size_t offset = pgoff * PAGE_SIZE;
915 unsigned long dev_sz;
917 dev_sz = dev_info->end - dev_info->start + 1;
918 *kaddr = (void *) dev_info->start + offset;
919 *pfn = __pfn_to_pfn_t(PFN_DOWN(dev_info->start + offset),
920 PFN_DEV|PFN_SPECIAL);
922 return (dev_sz - offset) / PAGE_SIZE;
925 static long
926 dcssblk_dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff,
927 long nr_pages, void **kaddr, pfn_t *pfn)
929 struct dcssblk_dev_info *dev_info = dax_get_private(dax_dev);
931 return __dcssblk_direct_access(dev_info, pgoff, nr_pages, kaddr, pfn);
934 static void
935 dcssblk_check_params(void)
937 int rc, i, j, k;
938 char buf[DCSSBLK_PARM_LEN + 1];
939 struct dcssblk_dev_info *dev_info;
941 for (i = 0; (i < DCSSBLK_PARM_LEN) && (dcssblk_segments[i] != '\0');
942 i++) {
943 for (j = i; (j < DCSSBLK_PARM_LEN) &&
944 (dcssblk_segments[j] != ',') &&
945 (dcssblk_segments[j] != '\0') &&
946 (dcssblk_segments[j] != '('); j++)
948 buf[j-i] = dcssblk_segments[j];
950 buf[j-i] = '\0';
951 rc = dcssblk_add_store(dcssblk_root_dev, NULL, buf, j-i);
952 if ((rc >= 0) && (dcssblk_segments[j] == '(')) {
953 for (k = 0; (buf[k] != ':') && (buf[k] != '\0'); k++)
954 buf[k] = toupper(buf[k]);
955 buf[k] = '\0';
956 if (!strncmp(&dcssblk_segments[j], "(local)", 7)) {
957 down_read(&dcssblk_devices_sem);
958 dev_info = dcssblk_get_device_by_name(buf);
959 up_read(&dcssblk_devices_sem);
960 if (dev_info)
961 dcssblk_shared_store(&dev_info->dev,
962 NULL, "0\n", 2);
965 while ((dcssblk_segments[j] != ',') &&
966 (dcssblk_segments[j] != '\0'))
968 j++;
970 if (dcssblk_segments[j] == '\0')
971 break;
972 i = j;
977 * Suspend / Resume
979 static int dcssblk_freeze(struct device *dev)
981 struct dcssblk_dev_info *dev_info;
982 int rc = 0;
984 list_for_each_entry(dev_info, &dcssblk_devices, lh) {
985 switch (dev_info->segment_type) {
986 case SEG_TYPE_SR:
987 case SEG_TYPE_ER:
988 case SEG_TYPE_SC:
989 if (!dev_info->is_shared)
990 rc = -EINVAL;
991 break;
992 default:
993 rc = -EINVAL;
994 break;
996 if (rc)
997 break;
999 if (rc)
1000 pr_err("Suspending the system failed because DCSS device %s "
1001 "is writable\n",
1002 dev_info->segment_name);
1003 return rc;
1006 static int dcssblk_restore(struct device *dev)
1008 struct dcssblk_dev_info *dev_info;
1009 struct segment_info *entry;
1010 unsigned long start, end;
1011 int rc = 0;
1013 list_for_each_entry(dev_info, &dcssblk_devices, lh) {
1014 list_for_each_entry(entry, &dev_info->seg_list, lh) {
1015 segment_unload(entry->segment_name);
1016 rc = segment_load(entry->segment_name, SEGMENT_SHARED,
1017 &start, &end);
1018 if (rc < 0) {
1019 // TODO in_use check ?
1020 segment_warning(rc, entry->segment_name);
1021 goto out_panic;
1023 if (start != entry->start || end != entry->end) {
1024 pr_err("The address range of DCSS %s changed "
1025 "while the system was suspended\n",
1026 entry->segment_name);
1027 goto out_panic;
1031 return 0;
1032 out_panic:
1033 panic("fatal dcssblk resume error\n");
1036 static int dcssblk_thaw(struct device *dev)
1038 return 0;
1041 static const struct dev_pm_ops dcssblk_pm_ops = {
1042 .freeze = dcssblk_freeze,
1043 .thaw = dcssblk_thaw,
1044 .restore = dcssblk_restore,
1047 static struct platform_driver dcssblk_pdrv = {
1048 .driver = {
1049 .name = "dcssblk",
1050 .pm = &dcssblk_pm_ops,
1054 static struct platform_device *dcssblk_pdev;
1058 * The init/exit functions.
1060 static void __exit
1061 dcssblk_exit(void)
1063 platform_device_unregister(dcssblk_pdev);
1064 platform_driver_unregister(&dcssblk_pdrv);
1065 root_device_unregister(dcssblk_root_dev);
1066 unregister_blkdev(dcssblk_major, DCSSBLK_NAME);
1069 static int __init
1070 dcssblk_init(void)
1072 int rc;
1074 rc = platform_driver_register(&dcssblk_pdrv);
1075 if (rc)
1076 return rc;
1078 dcssblk_pdev = platform_device_register_simple("dcssblk", -1, NULL,
1080 if (IS_ERR(dcssblk_pdev)) {
1081 rc = PTR_ERR(dcssblk_pdev);
1082 goto out_pdrv;
1085 dcssblk_root_dev = root_device_register("dcssblk");
1086 if (IS_ERR(dcssblk_root_dev)) {
1087 rc = PTR_ERR(dcssblk_root_dev);
1088 goto out_pdev;
1090 rc = device_create_file(dcssblk_root_dev, &dev_attr_add);
1091 if (rc)
1092 goto out_root;
1093 rc = device_create_file(dcssblk_root_dev, &dev_attr_remove);
1094 if (rc)
1095 goto out_root;
1096 rc = register_blkdev(0, DCSSBLK_NAME);
1097 if (rc < 0)
1098 goto out_root;
1099 dcssblk_major = rc;
1100 init_rwsem(&dcssblk_devices_sem);
1102 dcssblk_check_params();
1103 return 0;
1105 out_root:
1106 root_device_unregister(dcssblk_root_dev);
1107 out_pdev:
1108 platform_device_unregister(dcssblk_pdev);
1109 out_pdrv:
1110 platform_driver_unregister(&dcssblk_pdrv);
1111 return rc;
1114 module_init(dcssblk_init);
1115 module_exit(dcssblk_exit);
1117 module_param_string(segments, dcssblk_segments, DCSSBLK_PARM_LEN, 0444);
1118 MODULE_PARM_DESC(segments, "Name of DCSS segment(s) to be loaded, "
1119 "comma-separated list, names in each set separated "
1120 "by commas are separated by colons, each set contains "
1121 "names of contiguous segments and each name max. 8 chars.\n"
1122 "Adding \"(local)\" to the end of each set equals echoing 0 "
1123 "to /sys/devices/dcssblk/<device name>/shared after loading "
1124 "the contiguous segments - \n"
1125 "e.g. segments=\"mydcss1,mydcss2:mydcss3,mydcss4(local)\"");
1127 MODULE_LICENSE("GPL");