block: Use blk_queue_flag_*() in drivers instead of queue_flag_*()
[linux-2.6-block.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
7a2765f6
DW
54static const struct dax_operations dcssblk_dax_ops = {
55 .direct_access = dcssblk_dax_direct_access,
5d61e43b 56 .copy_from_iter = dcssblk_dax_copy_from_iter,
1da177e4
LT
57};
58
b2300b9e
HY
59struct dcssblk_dev_info {
60 struct list_head lh;
61 struct device dev;
98df67b3 62 char segment_name[DCSS_BUS_ID_SIZE];
b2300b9e
HY
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;
7a2765f6 73 struct dax_device *dax_dev;
b2300b9e
HY
74};
75
76struct segment_info {
77 struct list_head lh;
98df67b3 78 char segment_name[DCSS_BUS_ID_SIZE];
b2300b9e
HY
79 unsigned long start;
80 unsigned long end;
81 int segment_type;
82};
83
e404e274 84static ssize_t dcssblk_add_store(struct device * dev, struct device_attribute *attr, const char * buf,
1da177e4 85 size_t count);
e404e274 86static ssize_t dcssblk_remove_store(struct device * dev, struct device_attribute *attr, const char * buf,
1da177e4 87 size_t count);
1da177e4
LT
88
89static DEVICE_ATTR(add, S_IWUSR, NULL, dcssblk_add_store);
90static DEVICE_ATTR(remove, S_IWUSR, NULL, dcssblk_remove_store);
1da177e4
LT
91
92static struct device *dcssblk_root_dev;
93
c11ca97e 94static LIST_HEAD(dcssblk_devices);
1da177e4
LT
95static struct rw_semaphore dcssblk_devices_sem;
96
97/*
98 * release function for segment device.
99 */
100static void
101dcssblk_release_segment(struct device *dev)
102{
b2300b9e
HY
103 struct dcssblk_dev_info *dev_info;
104 struct segment_info *entry, *temp;
105
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);
110 }
111 kfree(dev_info);
1da177e4
LT
112 module_put(THIS_MODULE);
113}
114
115/*
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.
120 */
4d284cac 121static int
1da177e4
LT
122dcssblk_assign_free_minor(struct dcssblk_dev_info *dev_info)
123{
124 int minor, found;
125 struct dcssblk_dev_info *entry;
126
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)
d0591485 133 if (minor == entry->gd->first_minor)
1da177e4
LT
134 found++;
135 if (!found) break; // got unused minor
136 }
137 if (found)
138 return -EBUSY;
139 dev_info->gd->first_minor = minor;
140 return 0;
141}
142
143/*
144 * get the struct dcssblk_dev_info from dcssblk_devices
145 * for the given name.
146 * down_read(&dcssblk_devices_sem) must be held.
147 */
148static struct dcssblk_dev_info *
149dcssblk_get_device_by_name(char *name)
150{
151 struct dcssblk_dev_info *entry;
152
153 list_for_each_entry(entry, &dcssblk_devices, lh) {
154 if (!strcmp(name, entry->segment_name)) {
155 return entry;
156 }
157 }
158 return NULL;
159}
160
b2300b9e
HY
161/*
162 * get the struct segment_info from seg_list
163 * for the given name.
164 * down_read(&dcssblk_devices_sem) must be held.
165 */
166static struct segment_info *
167dcssblk_get_segment_by_name(char *name)
168{
169 struct dcssblk_dev_info *dev_info;
170 struct segment_info *entry;
171
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;
176 }
177 }
178 return NULL;
179}
180
181/*
182 * get the highest address of the multi-segment block.
183 */
184static unsigned long
185dcssblk_find_highest_addr(struct dcssblk_dev_info *dev_info)
186{
187 unsigned long highest_addr;
188 struct segment_info *entry;
189
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;
194 }
195 return highest_addr;
196}
197
198/*
199 * get the lowest address of the multi-segment block.
200 */
201static unsigned long
202dcssblk_find_lowest_addr(struct dcssblk_dev_info *dev_info)
203{
204 int set_first;
205 unsigned long lowest_addr;
206 struct segment_info *entry;
207
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;
217 }
218 }
219 return lowest_addr;
220}
221
222/*
223 * Check continuity of segments.
224 */
225static int
226dcssblk_is_continuous(struct dcssblk_dev_info *dev_info)
227{
228 int i, j, rc;
229 struct segment_info *sort_list, *entry, temp;
230
231 if (dev_info->num_of_segments <= 1)
232 return 0;
233
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++;
243 }
244
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));
255 }
256
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) {
93098bf0
HY
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);
b2300b9e
HY
263 rc = -EINVAL;
264 goto out;
265 }
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)) {
93098bf0
HY
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);
b2300b9e
HY
277 rc = -EINVAL;
278 goto out;
279 }
280 }
281 }
282 rc = 0;
283out:
284 kfree(sort_list);
285 return rc;
286}
287
288/*
289 * Load a segment
290 */
291static int
292dcssblk_load_segment(char *name, struct segment_info **seg_info)
293{
294 int rc;
295
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;
302
303 /* get a struct segment_info */
304 *seg_info = kzalloc(sizeof(struct segment_info), GFP_KERNEL);
305 if (*seg_info == NULL)
306 return -ENOMEM;
307
308 strcpy((*seg_info)->segment_name, name);
309
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;
319 }
320 return rc;
321}
322
1da177e4
LT
323/*
324 * device attribute for switching shared/nonshared (exclusive)
325 * operation (show + store)
326 */
327static ssize_t
e404e274 328dcssblk_shared_show(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
329{
330 struct dcssblk_dev_info *dev_info;
331
332 dev_info = container_of(dev, struct dcssblk_dev_info, dev);
333 return sprintf(buf, dev_info->is_shared ? "1\n" : "0\n");
334}
335
336static ssize_t
e404e274 337dcssblk_shared_store(struct device *dev, struct device_attribute *attr, const char *inbuf, size_t count)
1da177e4
LT
338{
339 struct dcssblk_dev_info *dev_info;
b2300b9e 340 struct segment_info *entry, *temp;
1da177e4
LT
341 int rc;
342
ded77fb4 343 if ((count > 1) && (inbuf[1] != '\n') && (inbuf[1] != '\0'))
1da177e4 344 return -EINVAL;
1da177e4
LT
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)) {
1da177e4
LT
348 rc = -EBUSY;
349 goto out;
350 }
351 if (inbuf[0] == '1') {
b2300b9e
HY
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;
1da177e4
LT
360 }
361 }
b2300b9e
HY
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);
368 }
1da177e4 369 } else if (inbuf[0] == '0') {
b2300b9e 370 /* reload segments in exclusive mode */
1da177e4 371 if (dev_info->segment_type == SEG_TYPE_SC) {
93098bf0
HY
372 pr_err("DCSS %s is of type SC and cannot be "
373 "loaded as exclusive-writable\n",
374 dev_info->segment_name);
1da177e4
LT
375 rc = -EINVAL;
376 goto out;
377 }
b2300b9e
HY
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;
385 }
1da177e4 386 }
b2300b9e
HY
387 dev_info->is_shared = 0;
388 set_disk_ro(dev_info->gd, 0);
1da177e4 389 } else {
1da177e4
LT
390 rc = -EINVAL;
391 goto out;
392 }
393 rc = count;
394 goto out;
395
396removeseg:
93098bf0
HY
397 pr_err("DCSS device %s is removed after a failed access mode "
398 "change\n", dev_info->segment_name);
b2300b9e
HY
399 temp = entry;
400 list_for_each_entry(entry, &dev_info->seg_list, lh) {
401 if (entry != temp)
402 segment_unload(entry->segment_name);
403 }
1da177e4
LT
404 list_del(&dev_info->lh);
405
7a2765f6
DW
406 kill_dax(dev_info->dax_dev);
407 put_dax(dev_info->dax_dev);
1da177e4 408 del_gendisk(dev_info->gd);
1312f40e 409 blk_cleanup_queue(dev_info->dcssblk_queue);
1da177e4
LT
410 dev_info->gd->queue = NULL;
411 put_disk(dev_info->gd);
0b60f9ea
TH
412 up_write(&dcssblk_devices_sem);
413
414 if (device_remove_file_self(dev, attr)) {
415 device_unregister(dev);
416 put_device(dev);
417 }
418 return rc;
1da177e4
LT
419out:
420 up_write(&dcssblk_devices_sem);
421 return rc;
422}
521b3d79
SO
423static DEVICE_ATTR(shared, S_IWUSR | S_IRUSR, dcssblk_shared_show,
424 dcssblk_shared_store);
1da177e4
LT
425
426/*
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)
432 */
433static ssize_t
e404e274 434dcssblk_save_show(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
435{
436 struct dcssblk_dev_info *dev_info;
437
438 dev_info = container_of(dev, struct dcssblk_dev_info, dev);
439 return sprintf(buf, dev_info->save_pending ? "1\n" : "0\n");
440}
441
442static ssize_t
e404e274 443dcssblk_save_store(struct device *dev, struct device_attribute *attr, const char *inbuf, size_t count)
1da177e4
LT
444{
445 struct dcssblk_dev_info *dev_info;
b2300b9e 446 struct segment_info *entry;
1da177e4 447
ded77fb4 448 if ((count > 1) && (inbuf[1] != '\n') && (inbuf[1] != '\0'))
1da177e4 449 return -EINVAL;
1da177e4
LT
450 dev_info = container_of(dev, struct dcssblk_dev_info, dev);
451
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
93098bf0
HY
456 pr_info("All DCSSs that map to device %s are "
457 "saved\n", dev_info->segment_name);
b2300b9e 458 list_for_each_entry(entry, &dev_info->seg_list, lh) {
5be6fdc0
GS
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);
b2300b9e 466 }
1da177e4
LT
467 } else {
468 // device is busy => we save it when it becomes
469 // idle in dcssblk_release
93098bf0
HY
470 pr_info("Device %s is in use, its DCSSs will be "
471 "saved when it becomes idle\n",
472 dev_info->segment_name);
1da177e4
LT
473 dev_info->save_pending = 1;
474 }
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;
93098bf0
HY
480 pr_info("A pending save request for device %s "
481 "has been canceled\n",
482 dev_info->segment_name);
1da177e4
LT
483 }
484 } else {
485 up_write(&dcssblk_devices_sem);
1da177e4
LT
486 return -EINVAL;
487 }
488 up_write(&dcssblk_devices_sem);
489 return count;
490}
521b3d79
SO
491static DEVICE_ATTR(save, S_IWUSR | S_IRUSR, dcssblk_save_show,
492 dcssblk_save_store);
1da177e4 493
b2300b9e
HY
494/*
495 * device attribute for showing all segments in a device
496 */
497static ssize_t
498dcssblk_seglist_show(struct device *dev, struct device_attribute *attr,
499 char *buf)
500{
501 int i;
502
503 struct dcssblk_dev_info *dev_info;
504 struct segment_info *entry;
505
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++;
515 }
516 up_read(&dcssblk_devices_sem);
517 return i;
518}
521b3d79
SO
519static DEVICE_ATTR(seglist, S_IRUSR, dcssblk_seglist_show, NULL);
520
521static struct attribute *dcssblk_dev_attrs[] = {
522 &dev_attr_shared.attr,
523 &dev_attr_save.attr,
524 &dev_attr_seglist.attr,
525 NULL,
526};
527static struct attribute_group dcssblk_dev_attr_group = {
528 .attrs = dcssblk_dev_attrs,
529};
530static const struct attribute_group *dcssblk_dev_attr_groups[] = {
531 &dcssblk_dev_attr_group,
532 NULL,
533};
b2300b9e 534
1da177e4
LT
535/*
536 * device attribute for adding devices
537 */
538static ssize_t
e404e274 539dcssblk_add_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1da177e4 540{
b2300b9e 541 int rc, i, j, num_of_segments;
1da177e4 542 struct dcssblk_dev_info *dev_info;
b2300b9e 543 struct segment_info *seg_info, *temp;
1da177e4
LT
544 char *local_buf;
545 unsigned long seg_byte_size;
546
547 dev_info = NULL;
b2300b9e 548 seg_info = NULL;
1da177e4
LT
549 if (dev != dcssblk_root_dev) {
550 rc = -EINVAL;
551 goto out_nobuf;
552 }
b2300b9e
HY
553 if ((count < 1) || (buf[0] == '\0') || (buf[0] == '\n')) {
554 rc = -ENAMETOOLONG;
555 goto out_nobuf;
556 }
557
1da177e4
LT
558 local_buf = kmalloc(count + 1, GFP_KERNEL);
559 if (local_buf == NULL) {
560 rc = -ENOMEM;
561 goto out_nobuf;
562 }
b2300b9e 563
1da177e4
LT
564 /*
565 * parse input
566 */
b2300b9e 567 num_of_segments = 0;
3a9f9183 568 for (i = 0; (i < count && (buf[i] != '\0') && (buf[i] != '\n')); i++) {
42cfc6b5
MS
569 for (j = i; j < count &&
570 (buf[j] != ':') &&
b2300b9e 571 (buf[j] != '\0') &&
42cfc6b5 572 (buf[j] != '\n'); j++) {
b2300b9e
HY
573 local_buf[j-i] = toupper(buf[j]);
574 }
575 local_buf[j-i] = '\0';
576 if (((j - i) == 0) || ((j - i) > 8)) {
577 rc = -ENAMETOOLONG;
578 goto seg_list_del;
579 }
580
581 rc = dcssblk_load_segment(local_buf, &seg_info);
582 if (rc < 0)
583 goto seg_list_del;
584 /*
585 * get a struct dcssblk_dev_info
586 */
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;
593 }
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);
597 }
598 list_add_tail(&seg_info->lh, &dev_info->seg_list);
599 num_of_segments++;
600 i = j;
601
602 if ((buf[j] == '\0') || (buf[j] == '\n'))
603 break;
1da177e4 604 }
b2300b9e
HY
605
606 /* no trailing colon at the end of the input */
607 if ((i > 0) && (buf[i-1] == ':')) {
1da177e4 608 rc = -ENAMETOOLONG;
b2300b9e 609 goto seg_list_del;
1da177e4 610 }
b2300b9e
HY
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;
616
617 dev_info->start = dcssblk_find_lowest_addr(dev_info);
618 dev_info->end = dcssblk_find_highest_addr(dev_info);
1da177e4 619
ef283688 620 dev_set_name(&dev_info->dev, "%s", dev_info->segment_name);
1da177e4 621 dev_info->dev.release = dcssblk_release_segment;
521b3d79 622 dev_info->dev.groups = dcssblk_dev_attr_groups;
1da177e4 623 INIT_LIST_HEAD(&dev_info->lh);
1da177e4
LT
624 dev_info->gd = alloc_disk(DCSSBLK_MINORS_PER_DISK);
625 if (dev_info->gd == NULL) {
626 rc = -ENOMEM;
b2300b9e 627 goto seg_list_del;
1da177e4
LT
628 }
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;
c5411dba 634 blk_queue_make_request(dev_info->dcssblk_queue, dcssblk_make_request);
e1defc4f 635 blk_queue_logical_block_size(dev_info->dcssblk_queue, 4096);
8b904b5b 636 blk_queue_flag_set(QUEUE_FLAG_DAX, dev_info->dcssblk_queue);
b2300b9e 637
1da177e4
LT
638 seg_byte_size = (dev_info->end - dev_info->start + 1);
639 set_capacity(dev_info->gd, seg_byte_size >> 9); // size in sectors
93098bf0
HY
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);
1da177e4 642
1da177e4
LT
643 dev_info->save_pending = 0;
644 dev_info->is_shared = 1;
645 dev_info->dev.parent = dcssblk_root_dev;
646
647 /*
b2300b9e 648 *get minor, add to list
1da177e4
LT
649 */
650 down_write(&dcssblk_devices_sem);
b2300b9e 651 if (dcssblk_get_segment_by_name(local_buf)) {
04f64b57 652 rc = -EEXIST;
b2300b9e 653 goto release_gd;
04f64b57 654 }
1da177e4 655 rc = dcssblk_assign_free_minor(dev_info);
b2300b9e
HY
656 if (rc)
657 goto release_gd;
1da177e4 658 sprintf(dev_info->gd->disk_name, "dcssblk%d",
d0591485 659 dev_info->gd->first_minor);
1da177e4
LT
660 list_add_tail(&dev_info->lh, &dcssblk_devices);
661
662 if (!try_module_get(THIS_MODULE)) {
663 rc = -ENODEV;
b2300b9e 664 goto dev_list_del;
1da177e4
LT
665 }
666 /*
667 * register the device
668 */
669 rc = device_register(&dev_info->dev);
b2300b9e 670 if (rc)
521b3d79 671 goto put_dev;
1da177e4 672
7a2765f6
DW
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;
678 }
679
521b3d79 680 get_device(&dev_info->dev);
0d52c756 681 device_add_disk(&dev_info->dev, dev_info->gd);
436d1bc7 682
1da177e4
LT
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;
692 }
1da177e4
LT
693 up_write(&dcssblk_devices_sem);
694 rc = count;
695 goto out;
696
521b3d79 697put_dev:
1da177e4 698 list_del(&dev_info->lh);
1312f40e 699 blk_cleanup_queue(dev_info->dcssblk_queue);
1da177e4
LT
700 dev_info->gd->queue = NULL;
701 put_disk(dev_info->gd);
b2300b9e
HY
702 list_for_each_entry(seg_info, &dev_info->seg_list, lh) {
703 segment_unload(seg_info->segment_name);
704 }
1da177e4
LT
705 put_device(&dev_info->dev);
706 up_write(&dcssblk_devices_sem);
707 goto out;
b2300b9e 708dev_list_del:
1da177e4 709 list_del(&dev_info->lh);
b2300b9e 710release_gd:
1312f40e 711 blk_cleanup_queue(dev_info->dcssblk_queue);
1da177e4
LT
712 dev_info->gd->queue = NULL;
713 put_disk(dev_info->gd);
b2300b9e
HY
714 up_write(&dcssblk_devices_sem);
715seg_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);
722 }
1da177e4
LT
723 kfree(dev_info);
724out:
725 kfree(local_buf);
726out_nobuf:
727 return rc;
728}
729
730/*
731 * device attribute for removing devices
732 */
733static ssize_t
e404e274 734dcssblk_remove_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1da177e4
LT
735{
736 struct dcssblk_dev_info *dev_info;
b2300b9e 737 struct segment_info *entry;
1da177e4
LT
738 int rc, i;
739 char *local_buf;
740
741 if (dev != dcssblk_root_dev) {
742 return -EINVAL;
743 }
744 local_buf = kmalloc(count + 1, GFP_KERNEL);
745 if (local_buf == NULL) {
746 return -ENOMEM;
747 }
748 /*
749 * parse input
750 */
42cfc6b5 751 for (i = 0; (i < count && (*(buf+i)!='\0') && (*(buf+i)!='\n')); i++) {
1da177e4
LT
752 local_buf[i] = toupper(buf[i]);
753 }
754 local_buf[i] = '\0';
755 if ((i == 0) || (i > 8)) {
756 rc = -ENAMETOOLONG;
757 goto out_buf;
758 }
759
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);
baebc70a
JP
764 pr_warn("Device %s cannot be removed because it is not a known device\n",
765 local_buf);
1da177e4
LT
766 rc = -ENODEV;
767 goto out_buf;
768 }
769 if (atomic_read(&dev_info->use_count) != 0) {
770 up_write(&dcssblk_devices_sem);
baebc70a
JP
771 pr_warn("Device %s cannot be removed while it is in use\n",
772 local_buf);
1da177e4
LT
773 rc = -EBUSY;
774 goto out_buf;
775 }
1da177e4 776
b2300b9e 777 list_del(&dev_info->lh);
7a2765f6
DW
778 kill_dax(dev_info->dax_dev);
779 put_dax(dev_info->dax_dev);
1da177e4 780 del_gendisk(dev_info->gd);
1312f40e 781 blk_cleanup_queue(dev_info->dcssblk_queue);
1da177e4
LT
782 dev_info->gd->queue = NULL;
783 put_disk(dev_info->gd);
b2300b9e
HY
784
785 /* unload all related segments */
786 list_for_each_entry(entry, &dev_info->seg_list, lh)
787 segment_unload(entry->segment_name);
788
1da177e4
LT
789 up_write(&dcssblk_devices_sem);
790
1378a683
GS
791 device_unregister(&dev_info->dev);
792 put_device(&dev_info->dev);
793
1da177e4
LT
794 rc = count;
795out_buf:
796 kfree(local_buf);
797 return rc;
798}
799
800static int
46d74326 801dcssblk_open(struct block_device *bdev, fmode_t mode)
1da177e4
LT
802{
803 struct dcssblk_dev_info *dev_info;
804 int rc;
805
46d74326 806 dev_info = bdev->bd_disk->private_data;
1da177e4
LT
807 if (NULL == dev_info) {
808 rc = -ENODEV;
809 goto out;
810 }
811 atomic_inc(&dev_info->use_count);
46d74326 812 bdev->bd_block_size = 4096;
1da177e4
LT
813 rc = 0;
814out:
815 return rc;
816}
817
db2a144b 818static void
46d74326 819dcssblk_release(struct gendisk *disk, fmode_t mode)
1da177e4 820{
46d74326 821 struct dcssblk_dev_info *dev_info = disk->private_data;
b2300b9e 822 struct segment_info *entry;
1da177e4 823
46d74326 824 if (!dev_info) {
db2a144b
AV
825 WARN_ON(1);
826 return;
1da177e4
LT
827 }
828 down_write(&dcssblk_devices_sem);
829 if (atomic_dec_and_test(&dev_info->use_count)
830 && (dev_info->save_pending)) {
93098bf0
HY
831 pr_info("Device %s has become idle and is being saved "
832 "now\n", dev_info->segment_name);
b2300b9e 833 list_for_each_entry(entry, &dev_info->seg_list, lh) {
5be6fdc0
GS
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);
b2300b9e 840 }
1da177e4
LT
841 dev_info->save_pending = 0;
842 }
843 up_write(&dcssblk_devices_sem);
1da177e4
LT
844}
845
dece1635 846static blk_qc_t
165125e1 847dcssblk_make_request(struct request_queue *q, struct bio *bio)
1da177e4
LT
848{
849 struct dcssblk_dev_info *dev_info;
7988613b
KO
850 struct bio_vec bvec;
851 struct bvec_iter iter;
1da177e4
LT
852 unsigned long index;
853 unsigned long page_addr;
854 unsigned long source_addr;
855 unsigned long bytes_done;
1da177e4 856
af67c31f 857 blk_queue_split(q, &bio);
54efd50b 858
1da177e4 859 bytes_done = 0;
74d46992 860 dev_info = bio->bi_disk->private_data;
1da177e4
LT
861 if (dev_info == NULL)
862 goto fail;
4f024f37
KO
863 if ((bio->bi_iter.bi_sector & 7) != 0 ||
864 (bio->bi_iter.bi_size & 4095) != 0)
1da177e4
LT
865 /* Request is not page-aligned. */
866 goto fail;
74d46992 867 if (bio_end_sector(bio) > get_capacity(bio->bi_disk)) {
1da177e4
LT
868 /* Request beyond end of DCSS segment. */
869 goto fail;
870 }
420edbcc
CO
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) {
baebc70a
JP
879 pr_warn("Writing to %s failed because it is a read-only device\n",
880 dev_name(&dev_info->dev));
420edbcc
CO
881 goto fail;
882 }
883 }
884 }
885
4f024f37 886 index = (bio->bi_iter.bi_sector >> 3);
7988613b 887 bio_for_each_segment(bvec, bio, iter) {
1da177e4 888 page_addr = (unsigned long)
7988613b 889 page_address(bvec.bv_page) + bvec.bv_offset;
1da177e4 890 source_addr = dev_info->start + (index<<12) + bytes_done;
7988613b 891 if (unlikely((page_addr & 4095) != 0) || (bvec.bv_len & 4095) != 0)
1da177e4
LT
892 // More paranoia.
893 goto fail;
894 if (bio_data_dir(bio) == READ) {
895 memcpy((void*)page_addr, (void*)source_addr,
7988613b 896 bvec.bv_len);
1da177e4
LT
897 } else {
898 memcpy((void*)source_addr, (void*)page_addr,
7988613b 899 bvec.bv_len);
1da177e4 900 }
7988613b 901 bytes_done += bvec.bv_len;
1da177e4 902 }
4246a0b6 903 bio_endio(bio);
dece1635 904 return BLK_QC_T_NONE;
1da177e4 905fail:
6712ecf8 906 bio_io_error(bio);
dece1635 907 return BLK_QC_T_NONE;
420edbcc
CO
908}
909
dd22f551 910static long
7a2765f6
DW
911__dcssblk_direct_access(struct dcssblk_dev_info *dev_info, pgoff_t pgoff,
912 long nr_pages, void **kaddr, pfn_t *pfn)
913{
914 resource_size_t offset = pgoff * PAGE_SIZE;
915 unsigned long dev_sz;
916
917 dev_sz = dev_info->end - dev_info->start + 1;
918 *kaddr = (void *) dev_info->start + offset;
785a3fab
DW
919 *pfn = __pfn_to_pfn_t(PFN_DOWN(dev_info->start + offset),
920 PFN_DEV|PFN_SPECIAL);
7a2765f6
DW
921
922 return (dev_sz - offset) / PAGE_SIZE;
923}
924
7a2765f6
DW
925static long
926dcssblk_dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff,
927 long nr_pages, void **kaddr, pfn_t *pfn)
928{
929 struct dcssblk_dev_info *dev_info = dax_get_private(dax_dev);
30afcb4b 930
7a2765f6 931 return __dcssblk_direct_access(dev_info, pgoff, nr_pages, kaddr, pfn);
1da177e4
LT
932}
933
934static void
935dcssblk_check_params(void)
936{
937 int rc, i, j, k;
b2300b9e 938 char buf[DCSSBLK_PARM_LEN + 1];
1da177e4
LT
939 struct dcssblk_dev_info *dev_info;
940
941 for (i = 0; (i < DCSSBLK_PARM_LEN) && (dcssblk_segments[i] != '\0');
942 i++) {
42cfc6b5
MS
943 for (j = i; (j < DCSSBLK_PARM_LEN) &&
944 (dcssblk_segments[j] != ',') &&
1da177e4 945 (dcssblk_segments[j] != '\0') &&
42cfc6b5 946 (dcssblk_segments[j] != '('); j++)
1da177e4
LT
947 {
948 buf[j-i] = dcssblk_segments[j];
949 }
950 buf[j-i] = '\0';
f901e5d1 951 rc = dcssblk_add_store(dcssblk_root_dev, NULL, buf, j-i);
1da177e4 952 if ((rc >= 0) && (dcssblk_segments[j] == '(')) {
b2300b9e 953 for (k = 0; (buf[k] != ':') && (buf[k] != '\0'); k++)
1da177e4 954 buf[k] = toupper(buf[k]);
b2300b9e 955 buf[k] = '\0';
1da177e4
LT
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,
f901e5d1 962 NULL, "0\n", 2);
1da177e4
LT
963 }
964 }
965 while ((dcssblk_segments[j] != ',') &&
966 (dcssblk_segments[j] != '\0'))
967 {
968 j++;
969 }
970 if (dcssblk_segments[j] == '\0')
971 break;
972 i = j;
973 }
974}
975
c369527f
GS
976/*
977 * Suspend / Resume
978 */
979static int dcssblk_freeze(struct device *dev)
980{
981 struct dcssblk_dev_info *dev_info;
982 int rc = 0;
983
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;
995 }
996 if (rc)
997 break;
998 }
999 if (rc)
2c48c4d6
CB
1000 pr_err("Suspending the system failed because DCSS device %s "
1001 "is writable\n",
c369527f
GS
1002 dev_info->segment_name);
1003 return rc;
1004}
1005
1006static int dcssblk_restore(struct device *dev)
1007{
1008 struct dcssblk_dev_info *dev_info;
1009 struct segment_info *entry;
1010 unsigned long start, end;
1011 int rc = 0;
1012
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;
1022 }
1023 if (start != entry->start || end != entry->end) {
2c48c4d6
CB
1024 pr_err("The address range of DCSS %s changed "
1025 "while the system was suspended\n",
c369527f
GS
1026 entry->segment_name);
1027 goto out_panic;
1028 }
1029 }
1030 }
1031 return 0;
1032out_panic:
1033 panic("fatal dcssblk resume error\n");
1034}
1035
1036static int dcssblk_thaw(struct device *dev)
1037{
1038 return 0;
1039}
1040
47145210 1041static const struct dev_pm_ops dcssblk_pm_ops = {
c369527f
GS
1042 .freeze = dcssblk_freeze,
1043 .thaw = dcssblk_thaw,
1044 .restore = dcssblk_restore,
1045};
1046
1047static struct platform_driver dcssblk_pdrv = {
1048 .driver = {
1049 .name = "dcssblk",
c369527f
GS
1050 .pm = &dcssblk_pm_ops,
1051 },
1052};
1053
1054static struct platform_device *dcssblk_pdev;
1055
1056
1da177e4
LT
1057/*
1058 * The init/exit functions.
1059 */
1060static void __exit
1061dcssblk_exit(void)
1062{
c369527f
GS
1063 platform_device_unregister(dcssblk_pdev);
1064 platform_driver_unregister(&dcssblk_pdrv);
035da16f 1065 root_device_unregister(dcssblk_root_dev);
00d59405 1066 unregister_blkdev(dcssblk_major, DCSSBLK_NAME);
1da177e4
LT
1067}
1068
1069static int __init
1070dcssblk_init(void)
1071{
1072 int rc;
1073
c369527f
GS
1074 rc = platform_driver_register(&dcssblk_pdrv);
1075 if (rc)
1da177e4 1076 return rc;
c369527f
GS
1077
1078 dcssblk_pdev = platform_device_register_simple("dcssblk", -1, NULL,
1079 0);
1080 if (IS_ERR(dcssblk_pdev)) {
1081 rc = PTR_ERR(dcssblk_pdev);
1082 goto out_pdrv;
1da177e4 1083 }
c369527f
GS
1084
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;
1da177e4 1089 }
c369527f
GS
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;
1da177e4 1096 rc = register_blkdev(0, DCSSBLK_NAME);
c369527f
GS
1097 if (rc < 0)
1098 goto out_root;
1da177e4
LT
1099 dcssblk_major = rc;
1100 init_rwsem(&dcssblk_devices_sem);
1101
1102 dcssblk_check_params();
1da177e4 1103 return 0;
c369527f
GS
1104
1105out_root:
1106 root_device_unregister(dcssblk_root_dev);
1107out_pdev:
1108 platform_device_unregister(dcssblk_pdev);
1109out_pdrv:
1110 platform_driver_unregister(&dcssblk_pdrv);
1111 return rc;
1da177e4
LT
1112}
1113
1114module_init(dcssblk_init);
1115module_exit(dcssblk_exit);
1116
1117module_param_string(segments, dcssblk_segments, DCSSBLK_PARM_LEN, 0444);
1118MODULE_PARM_DESC(segments, "Name of DCSS segment(s) to be loaded, "
b2300b9e
HY
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)\"");
1da177e4
LT
1126
1127MODULE_LICENSE("GPL");