bnxt_en: Fix NULL ptr dereference crash in bnxt_fw_reset_task()
[linux/fpc-iii.git] / drivers / s390 / block / dcssblk.c
blob63502ca537eb801c650615a89273809fd69b3978
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 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)
57 return copy_to_iter(addr, bytes, i);
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,
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;
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;
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);
97 static DEVICE_ATTR(add, S_IWUSR, NULL, dcssblk_add_store);
98 static DEVICE_ATTR(remove, S_IWUSR, NULL, dcssblk_remove_store);
100 static struct device *dcssblk_root_dev;
102 static LIST_HEAD(dcssblk_devices);
103 static struct rw_semaphore dcssblk_devices_sem;
106 * release function for segment device.
108 static void
109 dcssblk_release_segment(struct device *dev)
111 struct dcssblk_dev_info *dev_info;
112 struct segment_info *entry, *temp;
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);
119 kfree(dev_info);
120 module_put(THIS_MODULE);
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.
129 static int
130 dcssblk_assign_free_minor(struct dcssblk_dev_info *dev_info)
132 int minor, found;
133 struct dcssblk_dev_info *entry;
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
145 if (found)
146 return -EBUSY;
147 dev_info->gd->first_minor = minor;
148 return 0;
152 * get the struct dcssblk_dev_info from dcssblk_devices
153 * for the given name.
154 * down_read(&dcssblk_devices_sem) must be held.
156 static struct dcssblk_dev_info *
157 dcssblk_get_device_by_name(char *name)
159 struct dcssblk_dev_info *entry;
161 list_for_each_entry(entry, &dcssblk_devices, lh) {
162 if (!strcmp(name, entry->segment_name)) {
163 return entry;
166 return NULL;
170 * get the struct segment_info from seg_list
171 * for the given name.
172 * down_read(&dcssblk_devices_sem) must be held.
174 static struct segment_info *
175 dcssblk_get_segment_by_name(char *name)
177 struct dcssblk_dev_info *dev_info;
178 struct segment_info *entry;
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;
186 return NULL;
190 * get the highest address of the multi-segment block.
192 static unsigned long
193 dcssblk_find_highest_addr(struct dcssblk_dev_info *dev_info)
195 unsigned long highest_addr;
196 struct segment_info *entry;
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;
203 return highest_addr;
207 * get the lowest address of the multi-segment block.
209 static unsigned long
210 dcssblk_find_lowest_addr(struct dcssblk_dev_info *dev_info)
212 int set_first;
213 unsigned long lowest_addr;
214 struct segment_info *entry;
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;
227 return lowest_addr;
231 * Check continuity of segments.
233 static int
234 dcssblk_is_continuous(struct dcssblk_dev_info *dev_info)
236 int i, j, rc;
237 struct segment_info *sort_list, *entry, temp;
239 if (dev_info->num_of_segments <= 1)
240 return 0;
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++;
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));
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;
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;
290 rc = 0;
291 out:
292 kfree(sort_list);
293 return rc;
297 * Load a segment
299 static int
300 dcssblk_load_segment(char *name, struct segment_info **seg_info)
302 int rc;
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;
311 /* get a struct segment_info */
312 *seg_info = kzalloc(sizeof(struct segment_info), GFP_KERNEL);
313 if (*seg_info == NULL)
314 return -ENOMEM;
316 strcpy((*seg_info)->segment_name, name);
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;
328 return rc;
332 * device attribute for switching shared/nonshared (exclusive)
333 * operation (show + store)
335 static ssize_t
336 dcssblk_shared_show(struct device *dev, struct device_attribute *attr, char *buf)
338 struct dcssblk_dev_info *dev_info;
340 dev_info = container_of(dev, struct dcssblk_dev_info, dev);
341 return sprintf(buf, dev_info->is_shared ? "1\n" : "0\n");
344 static ssize_t
345 dcssblk_shared_store(struct device *dev, struct device_attribute *attr, const char *inbuf, size_t count)
347 struct dcssblk_dev_info *dev_info;
348 struct segment_info *entry, *temp;
349 int rc;
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;
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;
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);
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;
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;
395 dev_info->is_shared = 0;
396 set_disk_ro(dev_info->gd, 0);
397 } else {
398 rc = -EINVAL;
399 goto out;
401 rc = count;
402 goto out;
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);
412 list_del(&dev_info->lh);
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);
422 if (device_remove_file_self(dev, attr)) {
423 device_unregister(dev);
424 put_device(dev);
426 return rc;
427 out:
428 up_write(&dcssblk_devices_sem);
429 return rc;
431 static DEVICE_ATTR(shared, S_IWUSR | S_IRUSR, dcssblk_shared_show,
432 dcssblk_shared_store);
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)
441 static ssize_t
442 dcssblk_save_show(struct device *dev, struct device_attribute *attr, char *buf)
444 struct dcssblk_dev_info *dev_info;
446 dev_info = container_of(dev, struct dcssblk_dev_info, dev);
447 return sprintf(buf, dev_info->save_pending ? "1\n" : "0\n");
450 static ssize_t
451 dcssblk_save_store(struct device *dev, struct device_attribute *attr, const char *inbuf, size_t count)
453 struct dcssblk_dev_info *dev_info;
454 struct segment_info *entry;
456 if ((count > 1) && (inbuf[1] != '\n') && (inbuf[1] != '\0'))
457 return -EINVAL;
458 dev_info = container_of(dev, struct dcssblk_dev_info, dev);
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);
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;
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);
492 } else {
493 up_write(&dcssblk_devices_sem);
494 return -EINVAL;
496 up_write(&dcssblk_devices_sem);
497 return count;
499 static DEVICE_ATTR(save, S_IWUSR | S_IRUSR, dcssblk_save_show,
500 dcssblk_save_store);
503 * device attribute for showing all segments in a device
505 static ssize_t
506 dcssblk_seglist_show(struct device *dev, struct device_attribute *attr,
507 char *buf)
509 int i;
511 struct dcssblk_dev_info *dev_info;
512 struct segment_info *entry;
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++;
524 up_read(&dcssblk_devices_sem);
525 return i;
527 static DEVICE_ATTR(seglist, S_IRUSR, dcssblk_seglist_show, NULL);
529 static struct attribute *dcssblk_dev_attrs[] = {
530 &dev_attr_shared.attr,
531 &dev_attr_save.attr,
532 &dev_attr_seglist.attr,
533 NULL,
535 static struct attribute_group dcssblk_dev_attr_group = {
536 .attrs = dcssblk_dev_attrs,
538 static const struct attribute_group *dcssblk_dev_attr_groups[] = {
539 &dcssblk_dev_attr_group,
540 NULL,
544 * device attribute for adding devices
546 static ssize_t
547 dcssblk_add_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
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;
555 dev_info = NULL;
556 seg_info = NULL;
557 if (dev != dcssblk_root_dev) {
558 rc = -EINVAL;
559 goto out_nobuf;
561 if ((count < 1) || (buf[0] == '\0') || (buf[0] == '\n')) {
562 rc = -ENAMETOOLONG;
563 goto out_nobuf;
566 local_buf = kmalloc(count + 1, GFP_KERNEL);
567 if (local_buf == NULL) {
568 rc = -ENOMEM;
569 goto out_nobuf;
573 * parse input
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]);
583 local_buf[j-i] = '\0';
584 if (((j - i) == 0) || ((j - i) > 8)) {
585 rc = -ENAMETOOLONG;
586 goto seg_list_del;
589 rc = dcssblk_load_segment(local_buf, &seg_info);
590 if (rc < 0)
591 goto seg_list_del;
593 * get a struct dcssblk_dev_info
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;
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);
606 list_add_tail(&seg_info->lh, &dev_info->seg_list);
607 num_of_segments++;
608 i = j;
610 if ((buf[j] == '\0') || (buf[j] == '\n'))
611 break;
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;
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;
625 dev_info->start = dcssblk_find_lowest_addr(dev_info);
626 dev_info->end = dcssblk_find_highest_addr(dev_info);
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;
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);
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);
651 dev_info->save_pending = 0;
652 dev_info->is_shared = 1;
653 dev_info->dev.parent = dcssblk_root_dev;
656 *get minor, add to list
658 down_write(&dcssblk_devices_sem);
659 if (dcssblk_get_segment_by_name(local_buf)) {
660 rc = -EEXIST;
661 goto release_gd;
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);
670 if (!try_module_get(THIS_MODULE)) {
671 rc = -ENODEV;
672 goto dev_list_del;
675 * register the device
677 rc = device_register(&dev_info->dev);
678 if (rc)
679 goto put_dev;
681 dev_info->dax_dev = alloc_dax(dev_info, dev_info->gd->disk_name,
682 &dcssblk_dax_ops, DAXDEV_F_SYNC);
683 if (!dev_info->dax_dev) {
684 rc = -ENOMEM;
685 goto put_dev;
688 get_device(&dev_info->dev);
689 device_add_disk(&dev_info->dev, dev_info->gd, NULL);
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;
701 up_write(&dcssblk_devices_sem);
702 rc = count;
703 goto out;
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);
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);
731 kfree(dev_info);
732 out:
733 kfree(local_buf);
734 out_nobuf:
735 return rc;
739 * device attribute for removing devices
741 static ssize_t
742 dcssblk_remove_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
744 struct dcssblk_dev_info *dev_info;
745 struct segment_info *entry;
746 int rc, i;
747 char *local_buf;
749 if (dev != dcssblk_root_dev) {
750 return -EINVAL;
752 local_buf = kmalloc(count + 1, GFP_KERNEL);
753 if (local_buf == NULL) {
754 return -ENOMEM;
757 * parse input
759 for (i = 0; (i < count && (*(buf+i)!='\0') && (*(buf+i)!='\n')); i++) {
760 local_buf[i] = toupper(buf[i]);
762 local_buf[i] = '\0';
763 if ((i == 0) || (i > 8)) {
764 rc = -ENAMETOOLONG;
765 goto out_buf;
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;
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;
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);
793 /* unload all related segments */
794 list_for_each_entry(entry, &dev_info->seg_list, lh)
795 segment_unload(entry->segment_name);
797 up_write(&dcssblk_devices_sem);
799 device_unregister(&dev_info->dev);
800 put_device(&dev_info->dev);
802 rc = count;
803 out_buf:
804 kfree(local_buf);
805 return rc;
808 static int
809 dcssblk_open(struct block_device *bdev, fmode_t mode)
811 struct dcssblk_dev_info *dev_info;
812 int rc;
814 dev_info = bdev->bd_disk->private_data;
815 if (NULL == dev_info) {
816 rc = -ENODEV;
817 goto out;
819 atomic_inc(&dev_info->use_count);
820 bdev->bd_block_size = 4096;
821 rc = 0;
822 out:
823 return rc;
826 static void
827 dcssblk_release(struct gendisk *disk, fmode_t mode)
829 struct dcssblk_dev_info *dev_info = disk->private_data;
830 struct segment_info *entry;
832 if (!dev_info) {
833 WARN_ON(1);
834 return;
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);
849 dev_info->save_pending = 0;
851 up_write(&dcssblk_devices_sem);
854 static blk_qc_t
855 dcssblk_make_request(struct request_queue *q, struct bio *bio)
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;
865 blk_queue_split(q, &bio);
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;
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;
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);
909 bytes_done += bvec.bv_len;
911 bio_endio(bio);
912 return BLK_QC_T_NONE;
913 fail:
914 bio_io_error(bio);
915 return BLK_QC_T_NONE;
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)
922 resource_size_t offset = pgoff * PAGE_SIZE;
923 unsigned long dev_sz;
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);
932 return (dev_sz - offset) / PAGE_SIZE;
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)
939 struct dcssblk_dev_info *dev_info = dax_get_private(dax_dev);
941 return __dcssblk_direct_access(dev_info, pgoff, nr_pages, kaddr, pfn);
944 static void
945 dcssblk_check_params(void)
947 int rc, i, j, k;
948 char buf[DCSSBLK_PARM_LEN + 1];
949 struct dcssblk_dev_info *dev_info;
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++)
958 buf[j-i] = dcssblk_segments[j];
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);
975 while ((dcssblk_segments[j] != ',') &&
976 (dcssblk_segments[j] != '\0'))
978 j++;
980 if (dcssblk_segments[j] == '\0')
981 break;
982 i = j;
987 * Suspend / Resume
989 static int dcssblk_freeze(struct device *dev)
991 struct dcssblk_dev_info *dev_info;
992 int rc = 0;
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;
1006 if (rc)
1007 break;
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;
1016 static int dcssblk_restore(struct device *dev)
1018 struct dcssblk_dev_info *dev_info;
1019 struct segment_info *entry;
1020 unsigned long start, end;
1021 int rc = 0;
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;
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;
1041 return 0;
1042 out_panic:
1043 panic("fatal dcssblk resume error\n");
1046 static int dcssblk_thaw(struct device *dev)
1048 return 0;
1051 static const struct dev_pm_ops dcssblk_pm_ops = {
1052 .freeze = dcssblk_freeze,
1053 .thaw = dcssblk_thaw,
1054 .restore = dcssblk_restore,
1057 static struct platform_driver dcssblk_pdrv = {
1058 .driver = {
1059 .name = "dcssblk",
1060 .pm = &dcssblk_pm_ops,
1064 static struct platform_device *dcssblk_pdev;
1068 * The init/exit functions.
1070 static void __exit
1071 dcssblk_exit(void)
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);
1079 static int __init
1080 dcssblk_init(void)
1082 int rc;
1084 rc = platform_driver_register(&dcssblk_pdrv);
1085 if (rc)
1086 return rc;
1088 dcssblk_pdev = platform_device_register_simple("dcssblk", -1, NULL,
1090 if (IS_ERR(dcssblk_pdev)) {
1091 rc = PTR_ERR(dcssblk_pdev);
1092 goto out_pdrv;
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;
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);
1112 dcssblk_check_params();
1113 return 0;
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;
1124 module_init(dcssblk_init);
1125 module_exit(dcssblk_exit);
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)\"");
1137 MODULE_LICENSE("GPL");