blob: 2f056b2662f663d21515b72eb25b41e3f32a8e05 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * dcssblk.c -- the S/390 block driver for dcss memory
3 *
4 * Authors: Carsten Otte, Stefan Weinhuber, Gerald Schaefer
5 */
6
Hongjie Yang93098bf2008-12-25 13:39:45 +01007#define KMSG_COMPONENT "dcssblk"
8#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
9
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/module.h>
11#include <linux/moduleparam.h>
12#include <linux/ctype.h>
13#include <linux/errno.h>
14#include <linux/init.h>
15#include <linux/slab.h>
16#include <linux/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/completion.h>
18#include <linux/interrupt.h>
Gerald Schaeferc3695272009-06-16 10:30:51 +020019#include <linux/platform_device.h>
Dan Williams34c0fd52016-01-15 16:56:14 -080020#include <linux/pfn_t.h>
Gerald Schaeferc3695272009-06-16 10:30:51 +020021#include <asm/extmem.h>
22#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#define DCSSBLK_NAME "dcssblk"
25#define DCSSBLK_MINORS_PER_DISK 1
26#define DCSSBLK_PARM_LEN 400
Kay Sievers98df67b2008-12-25 13:38:55 +010027#define DCSS_BUS_ID_SIZE 20
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Al Viro46d74322008-03-02 10:37:41 -050029static int dcssblk_open(struct block_device *bdev, fmode_t mode);
Al Virodb2a1442013-05-05 21:52:57 -040030static void dcssblk_release(struct gendisk *disk, fmode_t mode);
Jens Axboedece1632015-11-05 10:41:16 -070031static blk_qc_t dcssblk_make_request(struct request_queue *q,
32 struct bio *bio);
Matthew Wilcoxdd22f552015-01-07 18:05:34 +020033static long dcssblk_direct_access(struct block_device *bdev, sector_t secnum,
Dan Williams0a70bd42016-02-24 14:02:11 -080034 void __pmem **kaddr, pfn_t *pfn, long size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36static char dcssblk_segments[DCSSBLK_PARM_LEN] = "\0";
37
38static int dcssblk_major;
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -070039static const struct block_device_operations dcssblk_devops = {
Carsten Otte420edbc2005-06-23 22:05:23 -070040 .owner = THIS_MODULE,
Al Viro46d74322008-03-02 10:37:41 -050041 .open = dcssblk_open,
42 .release = dcssblk_release,
Carsten Otte420edbc2005-06-23 22:05:23 -070043 .direct_access = dcssblk_direct_access,
Linus Torvalds1da177e2005-04-16 15:20:36 -070044};
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046struct dcssblk_dev_info {
47 struct list_head lh;
48 struct device dev;
Kay Sievers98df67b2008-12-25 13:38:55 +010049 char segment_name[DCSS_BUS_ID_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 atomic_t use_count;
51 struct gendisk *gd;
52 unsigned long start;
53 unsigned long end;
54 int segment_type;
55 unsigned char save_pending;
56 unsigned char is_shared;
57 struct request_queue *dcssblk_queue;
Hongjie Yangb2300b92008-10-10 21:33:21 +020058 int num_of_segments;
59 struct list_head seg_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060};
61
Hongjie Yangb2300b92008-10-10 21:33:21 +020062struct segment_info {
63 struct list_head lh;
Kay Sievers98df67b2008-12-25 13:38:55 +010064 char segment_name[DCSS_BUS_ID_SIZE];
Hongjie Yangb2300b92008-10-10 21:33:21 +020065 unsigned long start;
66 unsigned long end;
67 int segment_type;
68};
69
70static ssize_t dcssblk_add_store(struct device * dev, struct device_attribute *attr, const char * buf,
71 size_t count);
72static ssize_t dcssblk_remove_store(struct device * dev, struct device_attribute *attr, const char * buf,
73 size_t count);
Hongjie Yangb2300b92008-10-10 21:33:21 +020074
75static DEVICE_ATTR(add, S_IWUSR, NULL, dcssblk_add_store);
76static DEVICE_ATTR(remove, S_IWUSR, NULL, dcssblk_remove_store);
Hongjie Yangb2300b92008-10-10 21:33:21 +020077
78static struct device *dcssblk_root_dev;
79
Denis Chengc11ca972008-01-26 14:11:13 +010080static LIST_HEAD(dcssblk_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081static struct rw_semaphore dcssblk_devices_sem;
82
83/*
84 * release function for segment device.
85 */
86static void
87dcssblk_release_segment(struct device *dev)
88{
Hongjie Yangb2300b92008-10-10 21:33:21 +020089 struct dcssblk_dev_info *dev_info;
90 struct segment_info *entry, *temp;
91
92 dev_info = container_of(dev, struct dcssblk_dev_info, dev);
93 list_for_each_entry_safe(entry, temp, &dev_info->seg_list, lh) {
94 list_del(&entry->lh);
95 kfree(entry);
96 }
97 kfree(dev_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 module_put(THIS_MODULE);
99}
100
101/*
102 * get a minor number. needs to be called with
103 * down_write(&dcssblk_devices_sem) and the
104 * device needs to be enqueued before the semaphore is
105 * freed.
106 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100107static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108dcssblk_assign_free_minor(struct dcssblk_dev_info *dev_info)
109{
110 int minor, found;
111 struct dcssblk_dev_info *entry;
112
113 if (dev_info == NULL)
114 return -EINVAL;
115 for (minor = 0; minor < (1<<MINORBITS); minor++) {
116 found = 0;
117 // test if minor available
118 list_for_each_entry(entry, &dcssblk_devices, lh)
Gerald Schaeferd0591482009-06-12 10:26:35 +0200119 if (minor == entry->gd->first_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 found++;
121 if (!found) break; // got unused minor
122 }
123 if (found)
124 return -EBUSY;
125 dev_info->gd->first_minor = minor;
126 return 0;
127}
128
129/*
130 * get the struct dcssblk_dev_info from dcssblk_devices
131 * for the given name.
132 * down_read(&dcssblk_devices_sem) must be held.
133 */
134static struct dcssblk_dev_info *
135dcssblk_get_device_by_name(char *name)
136{
137 struct dcssblk_dev_info *entry;
138
139 list_for_each_entry(entry, &dcssblk_devices, lh) {
140 if (!strcmp(name, entry->segment_name)) {
141 return entry;
142 }
143 }
144 return NULL;
145}
146
Hongjie Yangb2300b92008-10-10 21:33:21 +0200147/*
148 * get the struct segment_info from seg_list
149 * for the given name.
150 * down_read(&dcssblk_devices_sem) must be held.
151 */
152static struct segment_info *
153dcssblk_get_segment_by_name(char *name)
154{
155 struct dcssblk_dev_info *dev_info;
156 struct segment_info *entry;
157
158 list_for_each_entry(dev_info, &dcssblk_devices, lh) {
159 list_for_each_entry(entry, &dev_info->seg_list, lh) {
160 if (!strcmp(name, entry->segment_name))
161 return entry;
162 }
163 }
164 return NULL;
165}
166
167/*
168 * get the highest address of the multi-segment block.
169 */
170static unsigned long
171dcssblk_find_highest_addr(struct dcssblk_dev_info *dev_info)
172{
173 unsigned long highest_addr;
174 struct segment_info *entry;
175
176 highest_addr = 0;
177 list_for_each_entry(entry, &dev_info->seg_list, lh) {
178 if (highest_addr < entry->end)
179 highest_addr = entry->end;
180 }
181 return highest_addr;
182}
183
184/*
185 * get the lowest address of the multi-segment block.
186 */
187static unsigned long
188dcssblk_find_lowest_addr(struct dcssblk_dev_info *dev_info)
189{
190 int set_first;
191 unsigned long lowest_addr;
192 struct segment_info *entry;
193
194 set_first = 0;
195 lowest_addr = 0;
196 list_for_each_entry(entry, &dev_info->seg_list, lh) {
197 if (set_first == 0) {
198 lowest_addr = entry->start;
199 set_first = 1;
200 } else {
201 if (lowest_addr > entry->start)
202 lowest_addr = entry->start;
203 }
204 }
205 return lowest_addr;
206}
207
208/*
209 * Check continuity of segments.
210 */
211static int
212dcssblk_is_continuous(struct dcssblk_dev_info *dev_info)
213{
214 int i, j, rc;
215 struct segment_info *sort_list, *entry, temp;
216
217 if (dev_info->num_of_segments <= 1)
218 return 0;
219
220 sort_list = kzalloc(
221 sizeof(struct segment_info) * dev_info->num_of_segments,
222 GFP_KERNEL);
223 if (sort_list == NULL)
224 return -ENOMEM;
225 i = 0;
226 list_for_each_entry(entry, &dev_info->seg_list, lh) {
227 memcpy(&sort_list[i], entry, sizeof(struct segment_info));
228 i++;
229 }
230
231 /* sort segments */
232 for (i = 0; i < dev_info->num_of_segments; i++)
233 for (j = 0; j < dev_info->num_of_segments; j++)
234 if (sort_list[j].start > sort_list[i].start) {
235 memcpy(&temp, &sort_list[i],
236 sizeof(struct segment_info));
237 memcpy(&sort_list[i], &sort_list[j],
238 sizeof(struct segment_info));
239 memcpy(&sort_list[j], &temp,
240 sizeof(struct segment_info));
241 }
242
243 /* check continuity */
244 for (i = 0; i < dev_info->num_of_segments - 1; i++) {
245 if ((sort_list[i].end + 1) != sort_list[i+1].start) {
Hongjie Yang93098bf2008-12-25 13:39:45 +0100246 pr_err("Adjacent DCSSs %s and %s are not "
247 "contiguous\n", sort_list[i].segment_name,
248 sort_list[i+1].segment_name);
Hongjie Yangb2300b92008-10-10 21:33:21 +0200249 rc = -EINVAL;
250 goto out;
251 }
252 /* EN and EW are allowed in a block device */
253 if (sort_list[i].segment_type != sort_list[i+1].segment_type) {
254 if (!(sort_list[i].segment_type & SEGMENT_EXCLUSIVE) ||
255 (sort_list[i].segment_type == SEG_TYPE_ER) ||
256 !(sort_list[i+1].segment_type &
257 SEGMENT_EXCLUSIVE) ||
258 (sort_list[i+1].segment_type == SEG_TYPE_ER)) {
Hongjie Yang93098bf2008-12-25 13:39:45 +0100259 pr_err("DCSS %s and DCSS %s have "
260 "incompatible types\n",
261 sort_list[i].segment_name,
262 sort_list[i+1].segment_name);
Hongjie Yangb2300b92008-10-10 21:33:21 +0200263 rc = -EINVAL;
264 goto out;
265 }
266 }
267 }
268 rc = 0;
269out:
270 kfree(sort_list);
271 return rc;
272}
273
274/*
275 * Load a segment
276 */
277static int
278dcssblk_load_segment(char *name, struct segment_info **seg_info)
279{
280 int rc;
281
282 /* already loaded? */
283 down_read(&dcssblk_devices_sem);
284 *seg_info = dcssblk_get_segment_by_name(name);
285 up_read(&dcssblk_devices_sem);
286 if (*seg_info != NULL)
287 return -EEXIST;
288
289 /* get a struct segment_info */
290 *seg_info = kzalloc(sizeof(struct segment_info), GFP_KERNEL);
291 if (*seg_info == NULL)
292 return -ENOMEM;
293
294 strcpy((*seg_info)->segment_name, name);
295
296 /* load the segment */
297 rc = segment_load(name, SEGMENT_SHARED,
298 &(*seg_info)->start, &(*seg_info)->end);
299 if (rc < 0) {
300 segment_warning(rc, (*seg_info)->segment_name);
301 kfree(*seg_info);
302 } else {
303 INIT_LIST_HEAD(&(*seg_info)->lh);
304 (*seg_info)->segment_type = rc;
305 }
306 return rc;
307}
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309/*
310 * device attribute for switching shared/nonshared (exclusive)
311 * operation (show + store)
312 */
313static ssize_t
Yani Ioannoue404e272005-05-17 06:42:58 -0400314dcssblk_shared_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
316 struct dcssblk_dev_info *dev_info;
317
318 dev_info = container_of(dev, struct dcssblk_dev_info, dev);
319 return sprintf(buf, dev_info->is_shared ? "1\n" : "0\n");
320}
321
322static ssize_t
Yani Ioannoue404e272005-05-17 06:42:58 -0400323dcssblk_shared_store(struct device *dev, struct device_attribute *attr, const char *inbuf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
325 struct dcssblk_dev_info *dev_info;
Hongjie Yangb2300b92008-10-10 21:33:21 +0200326 struct segment_info *entry, *temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 int rc;
328
Hongjie Yangded77fb2008-07-14 09:59:39 +0200329 if ((count > 1) && (inbuf[1] != '\n') && (inbuf[1] != '\0'))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 down_write(&dcssblk_devices_sem);
332 dev_info = container_of(dev, struct dcssblk_dev_info, dev);
333 if (atomic_read(&dev_info->use_count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 rc = -EBUSY;
335 goto out;
336 }
337 if (inbuf[0] == '1') {
Hongjie Yangb2300b92008-10-10 21:33:21 +0200338 /* reload segments in shared mode */
339 list_for_each_entry(entry, &dev_info->seg_list, lh) {
340 rc = segment_modify_shared(entry->segment_name,
341 SEGMENT_SHARED);
342 if (rc < 0) {
343 BUG_ON(rc == -EINVAL);
344 if (rc != -EAGAIN)
345 goto removeseg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
347 }
Hongjie Yangb2300b92008-10-10 21:33:21 +0200348 dev_info->is_shared = 1;
349 switch (dev_info->segment_type) {
350 case SEG_TYPE_SR:
351 case SEG_TYPE_ER:
352 case SEG_TYPE_SC:
353 set_disk_ro(dev_info->gd, 1);
354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 } else if (inbuf[0] == '0') {
Hongjie Yangb2300b92008-10-10 21:33:21 +0200356 /* reload segments in exclusive mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 if (dev_info->segment_type == SEG_TYPE_SC) {
Hongjie Yang93098bf2008-12-25 13:39:45 +0100358 pr_err("DCSS %s is of type SC and cannot be "
359 "loaded as exclusive-writable\n",
360 dev_info->segment_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 rc = -EINVAL;
362 goto out;
363 }
Hongjie Yangb2300b92008-10-10 21:33:21 +0200364 list_for_each_entry(entry, &dev_info->seg_list, lh) {
365 rc = segment_modify_shared(entry->segment_name,
366 SEGMENT_EXCLUSIVE);
367 if (rc < 0) {
368 BUG_ON(rc == -EINVAL);
369 if (rc != -EAGAIN)
370 goto removeseg;
371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 }
Hongjie Yangb2300b92008-10-10 21:33:21 +0200373 dev_info->is_shared = 0;
374 set_disk_ro(dev_info->gd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 rc = -EINVAL;
377 goto out;
378 }
379 rc = count;
380 goto out;
381
382removeseg:
Hongjie Yang93098bf2008-12-25 13:39:45 +0100383 pr_err("DCSS device %s is removed after a failed access mode "
384 "change\n", dev_info->segment_name);
Hongjie Yangb2300b92008-10-10 21:33:21 +0200385 temp = entry;
386 list_for_each_entry(entry, &dev_info->seg_list, lh) {
387 if (entry != temp)
388 segment_unload(entry->segment_name);
389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 list_del(&dev_info->lh);
391
392 del_gendisk(dev_info->gd);
Al Viro1312f402006-03-12 11:02:03 -0500393 blk_cleanup_queue(dev_info->dcssblk_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 dev_info->gd->queue = NULL;
395 put_disk(dev_info->gd);
Tejun Heo0b60f9e2014-02-03 14:03:04 -0500396 up_write(&dcssblk_devices_sem);
397
398 if (device_remove_file_self(dev, attr)) {
399 device_unregister(dev);
400 put_device(dev);
401 }
402 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403out:
404 up_write(&dcssblk_devices_sem);
405 return rc;
406}
Sebastian Ott521b3d72012-10-01 09:12:01 +0200407static DEVICE_ATTR(shared, S_IWUSR | S_IRUSR, dcssblk_shared_show,
408 dcssblk_shared_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410/*
411 * device attribute for save operation on current copy
412 * of the segment. If the segment is busy, saving will
413 * become pending until it gets released, which can be
414 * undone by storing a non-true value to this entry.
415 * (show + store)
416 */
417static ssize_t
Yani Ioannoue404e272005-05-17 06:42:58 -0400418dcssblk_save_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
420 struct dcssblk_dev_info *dev_info;
421
422 dev_info = container_of(dev, struct dcssblk_dev_info, dev);
423 return sprintf(buf, dev_info->save_pending ? "1\n" : "0\n");
424}
425
426static ssize_t
Yani Ioannoue404e272005-05-17 06:42:58 -0400427dcssblk_save_store(struct device *dev, struct device_attribute *attr, const char *inbuf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
429 struct dcssblk_dev_info *dev_info;
Hongjie Yangb2300b92008-10-10 21:33:21 +0200430 struct segment_info *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Hongjie Yangded77fb2008-07-14 09:59:39 +0200432 if ((count > 1) && (inbuf[1] != '\n') && (inbuf[1] != '\0'))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 dev_info = container_of(dev, struct dcssblk_dev_info, dev);
435
436 down_write(&dcssblk_devices_sem);
437 if (inbuf[0] == '1') {
438 if (atomic_read(&dev_info->use_count) == 0) {
439 // device is idle => we save immediately
Hongjie Yang93098bf2008-12-25 13:39:45 +0100440 pr_info("All DCSSs that map to device %s are "
441 "saved\n", dev_info->segment_name);
Hongjie Yangb2300b92008-10-10 21:33:21 +0200442 list_for_each_entry(entry, &dev_info->seg_list, lh) {
Gerald Schaefer5be6fdc2015-01-16 18:29:04 +0100443 if (entry->segment_type == SEG_TYPE_EN ||
444 entry->segment_type == SEG_TYPE_SN)
445 pr_warn("DCSS %s is of type SN or EN"
446 " and cannot be saved\n",
447 entry->segment_name);
448 else
449 segment_save(entry->segment_name);
Hongjie Yangb2300b92008-10-10 21:33:21 +0200450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 } else {
452 // device is busy => we save it when it becomes
453 // idle in dcssblk_release
Hongjie Yang93098bf2008-12-25 13:39:45 +0100454 pr_info("Device %s is in use, its DCSSs will be "
455 "saved when it becomes idle\n",
456 dev_info->segment_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 dev_info->save_pending = 1;
458 }
459 } else if (inbuf[0] == '0') {
460 if (dev_info->save_pending) {
461 // device is busy & the user wants to undo his save
462 // request
463 dev_info->save_pending = 0;
Hongjie Yang93098bf2008-12-25 13:39:45 +0100464 pr_info("A pending save request for device %s "
465 "has been canceled\n",
466 dev_info->segment_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 }
468 } else {
469 up_write(&dcssblk_devices_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 return -EINVAL;
471 }
472 up_write(&dcssblk_devices_sem);
473 return count;
474}
Sebastian Ott521b3d72012-10-01 09:12:01 +0200475static DEVICE_ATTR(save, S_IWUSR | S_IRUSR, dcssblk_save_show,
476 dcssblk_save_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478/*
Hongjie Yangb2300b92008-10-10 21:33:21 +0200479 * device attribute for showing all segments in a device
480 */
481static ssize_t
482dcssblk_seglist_show(struct device *dev, struct device_attribute *attr,
483 char *buf)
484{
485 int i;
486
487 struct dcssblk_dev_info *dev_info;
488 struct segment_info *entry;
489
490 down_read(&dcssblk_devices_sem);
491 dev_info = container_of(dev, struct dcssblk_dev_info, dev);
492 i = 0;
493 buf[0] = '\0';
494 list_for_each_entry(entry, &dev_info->seg_list, lh) {
495 strcpy(&buf[i], entry->segment_name);
496 i += strlen(entry->segment_name);
497 buf[i] = '\n';
498 i++;
499 }
500 up_read(&dcssblk_devices_sem);
501 return i;
502}
Sebastian Ott521b3d72012-10-01 09:12:01 +0200503static DEVICE_ATTR(seglist, S_IRUSR, dcssblk_seglist_show, NULL);
504
505static struct attribute *dcssblk_dev_attrs[] = {
506 &dev_attr_shared.attr,
507 &dev_attr_save.attr,
508 &dev_attr_seglist.attr,
509 NULL,
510};
511static struct attribute_group dcssblk_dev_attr_group = {
512 .attrs = dcssblk_dev_attrs,
513};
514static const struct attribute_group *dcssblk_dev_attr_groups[] = {
515 &dcssblk_dev_attr_group,
516 NULL,
517};
Hongjie Yangb2300b92008-10-10 21:33:21 +0200518
519/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 * device attribute for adding devices
521 */
522static ssize_t
Yani Ioannoue404e272005-05-17 06:42:58 -0400523dcssblk_add_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
Hongjie Yangb2300b92008-10-10 21:33:21 +0200525 int rc, i, j, num_of_segments;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 struct dcssblk_dev_info *dev_info;
Hongjie Yangb2300b92008-10-10 21:33:21 +0200527 struct segment_info *seg_info, *temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 char *local_buf;
529 unsigned long seg_byte_size;
530
531 dev_info = NULL;
Hongjie Yangb2300b92008-10-10 21:33:21 +0200532 seg_info = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 if (dev != dcssblk_root_dev) {
534 rc = -EINVAL;
535 goto out_nobuf;
536 }
Hongjie Yangb2300b92008-10-10 21:33:21 +0200537 if ((count < 1) || (buf[0] == '\0') || (buf[0] == '\n')) {
538 rc = -ENAMETOOLONG;
539 goto out_nobuf;
540 }
541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 local_buf = kmalloc(count + 1, GFP_KERNEL);
543 if (local_buf == NULL) {
544 rc = -ENOMEM;
545 goto out_nobuf;
546 }
Hongjie Yangb2300b92008-10-10 21:33:21 +0200547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 /*
549 * parse input
550 */
Hongjie Yangb2300b92008-10-10 21:33:21 +0200551 num_of_segments = 0;
Ameen Ali3a9f9182015-02-24 18:41:50 +0200552 for (i = 0; (i < count && (buf[i] != '\0') && (buf[i] != '\n')); i++) {
Martin Schwidefsky42cfc6b2015-08-19 10:42:23 +0200553 for (j = i; j < count &&
554 (buf[j] != ':') &&
Hongjie Yangb2300b92008-10-10 21:33:21 +0200555 (buf[j] != '\0') &&
Martin Schwidefsky42cfc6b2015-08-19 10:42:23 +0200556 (buf[j] != '\n'); j++) {
Hongjie Yangb2300b92008-10-10 21:33:21 +0200557 local_buf[j-i] = toupper(buf[j]);
558 }
559 local_buf[j-i] = '\0';
560 if (((j - i) == 0) || ((j - i) > 8)) {
561 rc = -ENAMETOOLONG;
562 goto seg_list_del;
563 }
564
565 rc = dcssblk_load_segment(local_buf, &seg_info);
566 if (rc < 0)
567 goto seg_list_del;
568 /*
569 * get a struct dcssblk_dev_info
570 */
571 if (num_of_segments == 0) {
572 dev_info = kzalloc(sizeof(struct dcssblk_dev_info),
573 GFP_KERNEL);
574 if (dev_info == NULL) {
575 rc = -ENOMEM;
576 goto out;
577 }
578 strcpy(dev_info->segment_name, local_buf);
579 dev_info->segment_type = seg_info->segment_type;
580 INIT_LIST_HEAD(&dev_info->seg_list);
581 }
582 list_add_tail(&seg_info->lh, &dev_info->seg_list);
583 num_of_segments++;
584 i = j;
585
586 if ((buf[j] == '\0') || (buf[j] == '\n'))
587 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Hongjie Yangb2300b92008-10-10 21:33:21 +0200590 /* no trailing colon at the end of the input */
591 if ((i > 0) && (buf[i-1] == ':')) {
592 rc = -ENAMETOOLONG;
593 goto seg_list_del;
594 }
595 strlcpy(local_buf, buf, i + 1);
596 dev_info->num_of_segments = num_of_segments;
597 rc = dcssblk_is_continuous(dev_info);
598 if (rc < 0)
599 goto seg_list_del;
600
601 dev_info->start = dcssblk_find_lowest_addr(dev_info);
602 dev_info->end = dcssblk_find_highest_addr(dev_info);
603
Kees Cookef283688f2014-06-10 10:46:20 -0700604 dev_set_name(&dev_info->dev, "%s", dev_info->segment_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 dev_info->dev.release = dcssblk_release_segment;
Sebastian Ott521b3d72012-10-01 09:12:01 +0200606 dev_info->dev.groups = dcssblk_dev_attr_groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 INIT_LIST_HEAD(&dev_info->lh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 dev_info->gd = alloc_disk(DCSSBLK_MINORS_PER_DISK);
609 if (dev_info->gd == NULL) {
610 rc = -ENOMEM;
Hongjie Yangb2300b92008-10-10 21:33:21 +0200611 goto seg_list_del;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 }
613 dev_info->gd->major = dcssblk_major;
614 dev_info->gd->fops = &dcssblk_devops;
615 dev_info->dcssblk_queue = blk_alloc_queue(GFP_KERNEL);
616 dev_info->gd->queue = dev_info->dcssblk_queue;
617 dev_info->gd->private_data = dev_info;
Heiko Carstensc5411db2008-02-05 16:50:50 +0100618 blk_queue_make_request(dev_info->dcssblk_queue, dcssblk_make_request);
Martin K. Petersene1defc42009-05-22 17:17:49 -0400619 blk_queue_logical_block_size(dev_info->dcssblk_queue, 4096);
Hongjie Yangb2300b92008-10-10 21:33:21 +0200620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 seg_byte_size = (dev_info->end - dev_info->start + 1);
622 set_capacity(dev_info->gd, seg_byte_size >> 9); // size in sectors
Hongjie Yang93098bf2008-12-25 13:39:45 +0100623 pr_info("Loaded %s with total size %lu bytes and capacity %lu "
624 "sectors\n", local_buf, seg_byte_size, seg_byte_size >> 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 dev_info->save_pending = 0;
627 dev_info->is_shared = 1;
628 dev_info->dev.parent = dcssblk_root_dev;
629
630 /*
Hongjie Yangb2300b92008-10-10 21:33:21 +0200631 *get minor, add to list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 */
633 down_write(&dcssblk_devices_sem);
Hongjie Yangb2300b92008-10-10 21:33:21 +0200634 if (dcssblk_get_segment_by_name(local_buf)) {
Gerald Schaefer04f64b52008-08-21 19:46:40 +0200635 rc = -EEXIST;
Hongjie Yangb2300b92008-10-10 21:33:21 +0200636 goto release_gd;
Gerald Schaefer04f64b52008-08-21 19:46:40 +0200637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 rc = dcssblk_assign_free_minor(dev_info);
Hongjie Yangb2300b92008-10-10 21:33:21 +0200639 if (rc)
640 goto release_gd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 sprintf(dev_info->gd->disk_name, "dcssblk%d",
Gerald Schaeferd0591482009-06-12 10:26:35 +0200642 dev_info->gd->first_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 list_add_tail(&dev_info->lh, &dcssblk_devices);
644
645 if (!try_module_get(THIS_MODULE)) {
646 rc = -ENODEV;
Hongjie Yangb2300b92008-10-10 21:33:21 +0200647 goto dev_list_del;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 }
649 /*
650 * register the device
651 */
652 rc = device_register(&dev_info->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 if (rc)
Sebastian Ott521b3d72012-10-01 09:12:01 +0200654 goto put_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Sebastian Ott521b3d72012-10-01 09:12:01 +0200656 get_device(&dev_info->dev);
Dan Williams0d52c7562016-06-15 19:44:20 -0700657 device_add_disk(&dev_info->dev, dev_info->gd);
Christian Borntraeger436d1bc2007-12-04 16:09:03 +0100658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 switch (dev_info->segment_type) {
660 case SEG_TYPE_SR:
661 case SEG_TYPE_ER:
662 case SEG_TYPE_SC:
663 set_disk_ro(dev_info->gd,1);
664 break;
665 default:
666 set_disk_ro(dev_info->gd,0);
667 break;
668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 up_write(&dcssblk_devices_sem);
670 rc = count;
671 goto out;
672
Sebastian Ott521b3d72012-10-01 09:12:01 +0200673put_dev:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 list_del(&dev_info->lh);
Al Viro1312f402006-03-12 11:02:03 -0500675 blk_cleanup_queue(dev_info->dcssblk_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 dev_info->gd->queue = NULL;
677 put_disk(dev_info->gd);
Hongjie Yangb2300b92008-10-10 21:33:21 +0200678 list_for_each_entry(seg_info, &dev_info->seg_list, lh) {
679 segment_unload(seg_info->segment_name);
680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 put_device(&dev_info->dev);
682 up_write(&dcssblk_devices_sem);
683 goto out;
Hongjie Yangb2300b92008-10-10 21:33:21 +0200684dev_list_del:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 list_del(&dev_info->lh);
Hongjie Yangb2300b92008-10-10 21:33:21 +0200686release_gd:
Al Viro1312f402006-03-12 11:02:03 -0500687 blk_cleanup_queue(dev_info->dcssblk_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 dev_info->gd->queue = NULL;
689 put_disk(dev_info->gd);
Hongjie Yangb2300b92008-10-10 21:33:21 +0200690 up_write(&dcssblk_devices_sem);
691seg_list_del:
692 if (dev_info == NULL)
693 goto out;
694 list_for_each_entry_safe(seg_info, temp, &dev_info->seg_list, lh) {
695 list_del(&seg_info->lh);
696 segment_unload(seg_info->segment_name);
697 kfree(seg_info);
698 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 kfree(dev_info);
700out:
701 kfree(local_buf);
702out_nobuf:
703 return rc;
704}
705
706/*
707 * device attribute for removing devices
708 */
709static ssize_t
Yani Ioannoue404e272005-05-17 06:42:58 -0400710dcssblk_remove_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
712 struct dcssblk_dev_info *dev_info;
Hongjie Yangb2300b92008-10-10 21:33:21 +0200713 struct segment_info *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 int rc, i;
715 char *local_buf;
716
717 if (dev != dcssblk_root_dev) {
718 return -EINVAL;
719 }
720 local_buf = kmalloc(count + 1, GFP_KERNEL);
721 if (local_buf == NULL) {
722 return -ENOMEM;
723 }
724 /*
725 * parse input
726 */
Martin Schwidefsky42cfc6b2015-08-19 10:42:23 +0200727 for (i = 0; (i < count && (*(buf+i)!='\0') && (*(buf+i)!='\n')); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 local_buf[i] = toupper(buf[i]);
729 }
730 local_buf[i] = '\0';
731 if ((i == 0) || (i > 8)) {
732 rc = -ENAMETOOLONG;
733 goto out_buf;
734 }
735
736 down_write(&dcssblk_devices_sem);
737 dev_info = dcssblk_get_device_by_name(local_buf);
738 if (dev_info == NULL) {
739 up_write(&dcssblk_devices_sem);
Joe Perchesbaebc702016-03-03 20:49:57 -0800740 pr_warn("Device %s cannot be removed because it is not a known device\n",
741 local_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 rc = -ENODEV;
743 goto out_buf;
744 }
745 if (atomic_read(&dev_info->use_count) != 0) {
746 up_write(&dcssblk_devices_sem);
Joe Perchesbaebc702016-03-03 20:49:57 -0800747 pr_warn("Device %s cannot be removed while it is in use\n",
748 local_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 rc = -EBUSY;
750 goto out_buf;
751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Hongjie Yangb2300b92008-10-10 21:33:21 +0200753 list_del(&dev_info->lh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 del_gendisk(dev_info->gd);
Al Viro1312f402006-03-12 11:02:03 -0500755 blk_cleanup_queue(dev_info->dcssblk_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 dev_info->gd->queue = NULL;
757 put_disk(dev_info->gd);
Hongjie Yangb2300b92008-10-10 21:33:21 +0200758
759 /* unload all related segments */
760 list_for_each_entry(entry, &dev_info->seg_list, lh)
761 segment_unload(entry->segment_name);
762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 up_write(&dcssblk_devices_sem);
764
Gerald Schaefer1378a682016-04-08 13:23:52 +0200765 device_unregister(&dev_info->dev);
766 put_device(&dev_info->dev);
767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 rc = count;
769out_buf:
770 kfree(local_buf);
771 return rc;
772}
773
774static int
Al Viro46d74322008-03-02 10:37:41 -0500775dcssblk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
777 struct dcssblk_dev_info *dev_info;
778 int rc;
779
Al Viro46d74322008-03-02 10:37:41 -0500780 dev_info = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 if (NULL == dev_info) {
782 rc = -ENODEV;
783 goto out;
784 }
785 atomic_inc(&dev_info->use_count);
Al Viro46d74322008-03-02 10:37:41 -0500786 bdev->bd_block_size = 4096;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 rc = 0;
788out:
789 return rc;
790}
791
Al Virodb2a1442013-05-05 21:52:57 -0400792static void
Al Viro46d74322008-03-02 10:37:41 -0500793dcssblk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
Al Viro46d74322008-03-02 10:37:41 -0500795 struct dcssblk_dev_info *dev_info = disk->private_data;
Hongjie Yangb2300b92008-10-10 21:33:21 +0200796 struct segment_info *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Al Viro46d74322008-03-02 10:37:41 -0500798 if (!dev_info) {
Al Virodb2a1442013-05-05 21:52:57 -0400799 WARN_ON(1);
800 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 }
802 down_write(&dcssblk_devices_sem);
803 if (atomic_dec_and_test(&dev_info->use_count)
804 && (dev_info->save_pending)) {
Hongjie Yang93098bf2008-12-25 13:39:45 +0100805 pr_info("Device %s has become idle and is being saved "
806 "now\n", dev_info->segment_name);
Hongjie Yangb2300b92008-10-10 21:33:21 +0200807 list_for_each_entry(entry, &dev_info->seg_list, lh) {
Gerald Schaefer5be6fdc2015-01-16 18:29:04 +0100808 if (entry->segment_type == SEG_TYPE_EN ||
809 entry->segment_type == SEG_TYPE_SN)
810 pr_warn("DCSS %s is of type SN or EN and cannot"
811 " be saved\n", entry->segment_name);
812 else
813 segment_save(entry->segment_name);
Hongjie Yangb2300b92008-10-10 21:33:21 +0200814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 dev_info->save_pending = 0;
816 }
817 up_write(&dcssblk_devices_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818}
819
Jens Axboedece1632015-11-05 10:41:16 -0700820static blk_qc_t
Jens Axboe165125e2007-07-24 09:28:11 +0200821dcssblk_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
823 struct dcssblk_dev_info *dev_info;
Kent Overstreet79886132013-11-23 17:19:00 -0800824 struct bio_vec bvec;
825 struct bvec_iter iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 unsigned long index;
827 unsigned long page_addr;
828 unsigned long source_addr;
829 unsigned long bytes_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Kent Overstreet54efd502015-04-23 22:37:18 -0700831 blk_queue_split(q, &bio, q->bio_split);
832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 bytes_done = 0;
834 dev_info = bio->bi_bdev->bd_disk->private_data;
835 if (dev_info == NULL)
836 goto fail;
Kent Overstreet4f024f32013-10-11 15:44:27 -0700837 if ((bio->bi_iter.bi_sector & 7) != 0 ||
838 (bio->bi_iter.bi_size & 4095) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 /* Request is not page-aligned. */
840 goto fail;
Kent Overstreetf73a1c72012-09-25 15:05:12 -0700841 if (bio_end_sector(bio) > get_capacity(bio->bi_bdev->bd_disk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 /* Request beyond end of DCSS segment. */
843 goto fail;
844 }
Carsten Otte420edbc2005-06-23 22:05:23 -0700845 /* verify data transfer direction */
846 if (dev_info->is_shared) {
847 switch (dev_info->segment_type) {
848 case SEG_TYPE_SR:
849 case SEG_TYPE_ER:
850 case SEG_TYPE_SC:
851 /* cannot write to these segments */
852 if (bio_data_dir(bio) == WRITE) {
Joe Perchesbaebc702016-03-03 20:49:57 -0800853 pr_warn("Writing to %s failed because it is a read-only device\n",
854 dev_name(&dev_info->dev));
Carsten Otte420edbc2005-06-23 22:05:23 -0700855 goto fail;
856 }
857 }
858 }
859
Kent Overstreet4f024f32013-10-11 15:44:27 -0700860 index = (bio->bi_iter.bi_sector >> 3);
Kent Overstreet79886132013-11-23 17:19:00 -0800861 bio_for_each_segment(bvec, bio, iter) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 page_addr = (unsigned long)
Kent Overstreet79886132013-11-23 17:19:00 -0800863 page_address(bvec.bv_page) + bvec.bv_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 source_addr = dev_info->start + (index<<12) + bytes_done;
Kent Overstreet79886132013-11-23 17:19:00 -0800865 if (unlikely((page_addr & 4095) != 0) || (bvec.bv_len & 4095) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 // More paranoia.
867 goto fail;
868 if (bio_data_dir(bio) == READ) {
869 memcpy((void*)page_addr, (void*)source_addr,
Kent Overstreet79886132013-11-23 17:19:00 -0800870 bvec.bv_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 } else {
872 memcpy((void*)source_addr, (void*)page_addr,
Kent Overstreet79886132013-11-23 17:19:00 -0800873 bvec.bv_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 }
Kent Overstreet79886132013-11-23 17:19:00 -0800875 bytes_done += bvec.bv_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 }
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200877 bio_endio(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700878 return BLK_QC_T_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879fail:
NeilBrown6712ecf2007-09-27 12:47:43 +0200880 bio_io_error(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700881 return BLK_QC_T_NONE;
Carsten Otte420edbc2005-06-23 22:05:23 -0700882}
883
Matthew Wilcoxdd22f552015-01-07 18:05:34 +0200884static long
Carsten Otte420edbc2005-06-23 22:05:23 -0700885dcssblk_direct_access (struct block_device *bdev, sector_t secnum,
Dan Williams0a70bd42016-02-24 14:02:11 -0800886 void __pmem **kaddr, pfn_t *pfn, long size)
Carsten Otte420edbc2005-06-23 22:05:23 -0700887{
888 struct dcssblk_dev_info *dev_info;
Matthew Wilcoxdd22f552015-01-07 18:05:34 +0200889 unsigned long offset, dev_sz;
Carsten Otte420edbc2005-06-23 22:05:23 -0700890
891 dev_info = bdev->bd_disk->private_data;
892 if (!dev_info)
893 return -ENODEV;
Matthew Wilcoxdd22f552015-01-07 18:05:34 +0200894 dev_sz = dev_info->end - dev_info->start;
895 offset = secnum * 512;
Dan Williams34c0fd52016-01-15 16:56:14 -0800896 *kaddr = (void __pmem *) (dev_info->start + offset);
897 *pfn = __pfn_to_pfn_t(PFN_DOWN(dev_info->start + offset), PFN_DEV);
Jared Hulbert30afcb42008-04-28 02:13:02 -0700898
Matthew Wilcoxdd22f552015-01-07 18:05:34 +0200899 return dev_sz - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900}
901
902static void
903dcssblk_check_params(void)
904{
905 int rc, i, j, k;
Hongjie Yangb2300b92008-10-10 21:33:21 +0200906 char buf[DCSSBLK_PARM_LEN + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 struct dcssblk_dev_info *dev_info;
908
909 for (i = 0; (i < DCSSBLK_PARM_LEN) && (dcssblk_segments[i] != '\0');
910 i++) {
Martin Schwidefsky42cfc6b2015-08-19 10:42:23 +0200911 for (j = i; (j < DCSSBLK_PARM_LEN) &&
912 (dcssblk_segments[j] != ',') &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 (dcssblk_segments[j] != '\0') &&
Martin Schwidefsky42cfc6b2015-08-19 10:42:23 +0200914 (dcssblk_segments[j] != '('); j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 {
916 buf[j-i] = dcssblk_segments[j];
917 }
918 buf[j-i] = '\0';
Cornelia Huckf901e5d2005-06-25 14:55:29 -0700919 rc = dcssblk_add_store(dcssblk_root_dev, NULL, buf, j-i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 if ((rc >= 0) && (dcssblk_segments[j] == '(')) {
Hongjie Yangb2300b92008-10-10 21:33:21 +0200921 for (k = 0; (buf[k] != ':') && (buf[k] != '\0'); k++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 buf[k] = toupper(buf[k]);
Hongjie Yangb2300b92008-10-10 21:33:21 +0200923 buf[k] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 if (!strncmp(&dcssblk_segments[j], "(local)", 7)) {
925 down_read(&dcssblk_devices_sem);
926 dev_info = dcssblk_get_device_by_name(buf);
927 up_read(&dcssblk_devices_sem);
928 if (dev_info)
929 dcssblk_shared_store(&dev_info->dev,
Cornelia Huckf901e5d2005-06-25 14:55:29 -0700930 NULL, "0\n", 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 }
932 }
933 while ((dcssblk_segments[j] != ',') &&
934 (dcssblk_segments[j] != '\0'))
935 {
936 j++;
937 }
938 if (dcssblk_segments[j] == '\0')
939 break;
940 i = j;
941 }
942}
943
944/*
Gerald Schaeferc3695272009-06-16 10:30:51 +0200945 * Suspend / Resume
946 */
947static int dcssblk_freeze(struct device *dev)
948{
949 struct dcssblk_dev_info *dev_info;
950 int rc = 0;
951
952 list_for_each_entry(dev_info, &dcssblk_devices, lh) {
953 switch (dev_info->segment_type) {
954 case SEG_TYPE_SR:
955 case SEG_TYPE_ER:
956 case SEG_TYPE_SC:
957 if (!dev_info->is_shared)
958 rc = -EINVAL;
959 break;
960 default:
961 rc = -EINVAL;
962 break;
963 }
964 if (rc)
965 break;
966 }
967 if (rc)
Christian Borntraeger2c48c4d2009-07-07 16:37:11 +0200968 pr_err("Suspending the system failed because DCSS device %s "
969 "is writable\n",
Gerald Schaeferc3695272009-06-16 10:30:51 +0200970 dev_info->segment_name);
971 return rc;
972}
973
974static int dcssblk_restore(struct device *dev)
975{
976 struct dcssblk_dev_info *dev_info;
977 struct segment_info *entry;
978 unsigned long start, end;
979 int rc = 0;
980
981 list_for_each_entry(dev_info, &dcssblk_devices, lh) {
982 list_for_each_entry(entry, &dev_info->seg_list, lh) {
983 segment_unload(entry->segment_name);
984 rc = segment_load(entry->segment_name, SEGMENT_SHARED,
985 &start, &end);
986 if (rc < 0) {
987// TODO in_use check ?
988 segment_warning(rc, entry->segment_name);
989 goto out_panic;
990 }
991 if (start != entry->start || end != entry->end) {
Christian Borntraeger2c48c4d2009-07-07 16:37:11 +0200992 pr_err("The address range of DCSS %s changed "
993 "while the system was suspended\n",
Gerald Schaeferc3695272009-06-16 10:30:51 +0200994 entry->segment_name);
995 goto out_panic;
996 }
997 }
998 }
999 return 0;
1000out_panic:
1001 panic("fatal dcssblk resume error\n");
1002}
1003
1004static int dcssblk_thaw(struct device *dev)
1005{
1006 return 0;
1007}
1008
Alexey Dobriyan47145212009-12-14 18:00:08 -08001009static const struct dev_pm_ops dcssblk_pm_ops = {
Gerald Schaeferc3695272009-06-16 10:30:51 +02001010 .freeze = dcssblk_freeze,
1011 .thaw = dcssblk_thaw,
1012 .restore = dcssblk_restore,
1013};
1014
1015static struct platform_driver dcssblk_pdrv = {
1016 .driver = {
1017 .name = "dcssblk",
Gerald Schaeferc3695272009-06-16 10:30:51 +02001018 .pm = &dcssblk_pm_ops,
1019 },
1020};
1021
1022static struct platform_device *dcssblk_pdev;
1023
1024
1025/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 * The init/exit functions.
1027 */
1028static void __exit
1029dcssblk_exit(void)
1030{
Gerald Schaeferc3695272009-06-16 10:30:51 +02001031 platform_device_unregister(dcssblk_pdev);
1032 platform_driver_unregister(&dcssblk_pdrv);
Mark McLoughlin035da162008-12-15 12:58:29 +00001033 root_device_unregister(dcssblk_root_dev);
Akinobu Mita00d59402007-07-17 04:03:46 -07001034 unregister_blkdev(dcssblk_major, DCSSBLK_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035}
1036
1037static int __init
1038dcssblk_init(void)
1039{
1040 int rc;
1041
Gerald Schaeferc3695272009-06-16 10:30:51 +02001042 rc = platform_driver_register(&dcssblk_pdrv);
1043 if (rc)
1044 return rc;
1045
1046 dcssblk_pdev = platform_device_register_simple("dcssblk", -1, NULL,
1047 0);
1048 if (IS_ERR(dcssblk_pdev)) {
1049 rc = PTR_ERR(dcssblk_pdev);
1050 goto out_pdrv;
1051 }
1052
Mark McLoughlin035da162008-12-15 12:58:29 +00001053 dcssblk_root_dev = root_device_register("dcssblk");
Gerald Schaeferc3695272009-06-16 10:30:51 +02001054 if (IS_ERR(dcssblk_root_dev)) {
1055 rc = PTR_ERR(dcssblk_root_dev);
1056 goto out_pdev;
1057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 rc = device_create_file(dcssblk_root_dev, &dev_attr_add);
Gerald Schaeferc3695272009-06-16 10:30:51 +02001059 if (rc)
1060 goto out_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 rc = device_create_file(dcssblk_root_dev, &dev_attr_remove);
Gerald Schaeferc3695272009-06-16 10:30:51 +02001062 if (rc)
1063 goto out_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 rc = register_blkdev(0, DCSSBLK_NAME);
Gerald Schaeferc3695272009-06-16 10:30:51 +02001065 if (rc < 0)
1066 goto out_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 dcssblk_major = rc;
1068 init_rwsem(&dcssblk_devices_sem);
1069
1070 dcssblk_check_params();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 return 0;
Gerald Schaeferc3695272009-06-16 10:30:51 +02001072
1073out_root:
1074 root_device_unregister(dcssblk_root_dev);
1075out_pdev:
1076 platform_device_unregister(dcssblk_pdev);
1077out_pdrv:
1078 platform_driver_unregister(&dcssblk_pdrv);
1079 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080}
1081
1082module_init(dcssblk_init);
1083module_exit(dcssblk_exit);
1084
1085module_param_string(segments, dcssblk_segments, DCSSBLK_PARM_LEN, 0444);
1086MODULE_PARM_DESC(segments, "Name of DCSS segment(s) to be loaded, "
Hongjie Yangb2300b92008-10-10 21:33:21 +02001087 "comma-separated list, names in each set separated "
1088 "by commas are separated by colons, each set contains "
1089 "names of contiguous segments and each name max. 8 chars.\n"
1090 "Adding \"(local)\" to the end of each set equals echoing 0 "
1091 "to /sys/devices/dcssblk/<device name>/shared after loading "
1092 "the contiguous segments - \n"
1093 "e.g. segments=\"mydcss1,mydcss2:mydcss3,mydcss4(local)\"");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
1095MODULE_LICENSE("GPL");