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