blob: db85f1fb131e0494dfb3e7a4838b23c269d9d36b [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
7#include <linux/module.h>
8#include <linux/moduleparam.h>
9#include <linux/ctype.h>
10#include <linux/errno.h>
11#include <linux/init.h>
12#include <linux/slab.h>
13#include <linux/blkdev.h>
14#include <asm/extmem.h>
15#include <asm/io.h>
16#include <linux/completion.h>
17#include <linux/interrupt.h>
Carsten Ottecfb1b552006-01-06 00:19:14 -080018#include <asm/s390_rdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20//#define DCSSBLK_DEBUG /* Debug messages on/off */
21#define DCSSBLK_NAME "dcssblk"
22#define DCSSBLK_MINORS_PER_DISK 1
23#define DCSSBLK_PARM_LEN 400
24
25#ifdef DCSSBLK_DEBUG
26#define PRINT_DEBUG(x...) printk(KERN_DEBUG DCSSBLK_NAME " debug: " x)
27#else
28#define PRINT_DEBUG(x...) do {} while (0)
29#endif
30#define PRINT_INFO(x...) printk(KERN_INFO DCSSBLK_NAME " info: " x)
31#define PRINT_WARN(x...) printk(KERN_WARNING DCSSBLK_NAME " warning: " x)
32#define PRINT_ERR(x...) printk(KERN_ERR DCSSBLK_NAME " error: " x)
33
34
35static int dcssblk_open(struct inode *inode, struct file *filp);
36static int dcssblk_release(struct inode *inode, struct file *filp);
37static int dcssblk_make_request(struct request_queue *q, struct bio *bio);
Carsten Otte420edbc2005-06-23 22:05:23 -070038static int dcssblk_direct_access(struct block_device *bdev, sector_t secnum,
Jared Hulbert30afcb42008-04-28 02:13:02 -070039 void **kaddr, unsigned long *pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41static char dcssblk_segments[DCSSBLK_PARM_LEN] = "\0";
42
43static int dcssblk_major;
44static struct block_device_operations dcssblk_devops = {
Carsten Otte420edbc2005-06-23 22:05:23 -070045 .owner = THIS_MODULE,
46 .open = dcssblk_open,
47 .release = dcssblk_release,
48 .direct_access = dcssblk_direct_access,
Linus Torvalds1da177e2005-04-16 15:20:36 -070049};
50
Yani Ioannoue404e272005-05-17 06:42:58 -040051static ssize_t dcssblk_add_store(struct device * dev, struct device_attribute *attr, const char * buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 size_t count);
Yani Ioannoue404e272005-05-17 06:42:58 -040053static ssize_t dcssblk_remove_store(struct device * dev, struct device_attribute *attr, const char * buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 size_t count);
Yani Ioannoue404e272005-05-17 06:42:58 -040055static ssize_t dcssblk_save_store(struct device * dev, struct device_attribute *attr, const char * buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 size_t count);
Yani Ioannoue404e272005-05-17 06:42:58 -040057static ssize_t dcssblk_save_show(struct device *dev, struct device_attribute *attr, char *buf);
58static ssize_t dcssblk_shared_store(struct device * dev, struct device_attribute *attr, const char * buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 size_t count);
Yani Ioannoue404e272005-05-17 06:42:58 -040060static ssize_t dcssblk_shared_show(struct device *dev, struct device_attribute *attr, char *buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62static DEVICE_ATTR(add, S_IWUSR, NULL, dcssblk_add_store);
63static DEVICE_ATTR(remove, S_IWUSR, NULL, dcssblk_remove_store);
64static DEVICE_ATTR(save, S_IWUSR | S_IRUGO, dcssblk_save_show,
65 dcssblk_save_store);
66static DEVICE_ATTR(shared, S_IWUSR | S_IRUGO, dcssblk_shared_show,
67 dcssblk_shared_store);
68
69static struct device *dcssblk_root_dev;
70
71struct dcssblk_dev_info {
72 struct list_head lh;
73 struct device dev;
74 char segment_name[BUS_ID_SIZE];
75 atomic_t use_count;
76 struct gendisk *gd;
77 unsigned long start;
78 unsigned long end;
79 int segment_type;
80 unsigned char save_pending;
81 unsigned char is_shared;
82 struct request_queue *dcssblk_queue;
83};
84
Denis Chengc11ca972008-01-26 14:11:13 +010085static LIST_HEAD(dcssblk_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086static struct rw_semaphore dcssblk_devices_sem;
87
88/*
89 * release function for segment device.
90 */
91static void
92dcssblk_release_segment(struct device *dev)
93{
94 PRINT_DEBUG("segment release fn called for %s\n", dev->bus_id);
95 kfree(container_of(dev, struct dcssblk_dev_info, dev));
96 module_put(THIS_MODULE);
97}
98
99/*
100 * get a minor number. needs to be called with
101 * down_write(&dcssblk_devices_sem) and the
102 * device needs to be enqueued before the semaphore is
103 * freed.
104 */
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100105static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106dcssblk_assign_free_minor(struct dcssblk_dev_info *dev_info)
107{
108 int minor, found;
109 struct dcssblk_dev_info *entry;
110
111 if (dev_info == NULL)
112 return -EINVAL;
113 for (minor = 0; minor < (1<<MINORBITS); minor++) {
114 found = 0;
115 // test if minor available
116 list_for_each_entry(entry, &dcssblk_devices, lh)
117 if (minor == entry->gd->first_minor)
118 found++;
119 if (!found) break; // got unused minor
120 }
121 if (found)
122 return -EBUSY;
123 dev_info->gd->first_minor = minor;
124 return 0;
125}
126
127/*
128 * get the struct dcssblk_dev_info from dcssblk_devices
129 * for the given name.
130 * down_read(&dcssblk_devices_sem) must be held.
131 */
132static struct dcssblk_dev_info *
133dcssblk_get_device_by_name(char *name)
134{
135 struct dcssblk_dev_info *entry;
136
137 list_for_each_entry(entry, &dcssblk_devices, lh) {
138 if (!strcmp(name, entry->segment_name)) {
139 return entry;
140 }
141 }
142 return NULL;
143}
144
Gerald Schaefer931bb682007-11-05 11:10:09 +0100145static void dcssblk_unregister_callback(struct device *dev)
146{
147 device_unregister(dev);
148 put_device(dev);
149}
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151/*
152 * device attribute for switching shared/nonshared (exclusive)
153 * operation (show + store)
154 */
155static ssize_t
Yani Ioannoue404e272005-05-17 06:42:58 -0400156dcssblk_shared_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
158 struct dcssblk_dev_info *dev_info;
159
160 dev_info = container_of(dev, struct dcssblk_dev_info, dev);
161 return sprintf(buf, dev_info->is_shared ? "1\n" : "0\n");
162}
163
164static ssize_t
Yani Ioannoue404e272005-05-17 06:42:58 -0400165dcssblk_shared_store(struct device *dev, struct device_attribute *attr, const char *inbuf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
167 struct dcssblk_dev_info *dev_info;
168 int rc;
169
Hongjie Yangded77fb2008-07-14 09:59:39 +0200170 if ((count > 1) && (inbuf[1] != '\n') && (inbuf[1] != '\0'))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 down_write(&dcssblk_devices_sem);
173 dev_info = container_of(dev, struct dcssblk_dev_info, dev);
174 if (atomic_read(&dev_info->use_count)) {
175 PRINT_ERR("share: segment %s is busy!\n",
176 dev_info->segment_name);
177 rc = -EBUSY;
178 goto out;
179 }
180 if (inbuf[0] == '1') {
181 // reload segment in shared mode
182 rc = segment_modify_shared(dev_info->segment_name,
183 SEGMENT_SHARED);
184 if (rc < 0) {
185 BUG_ON(rc == -EINVAL);
Gerald Schaefer444f0e52007-02-05 21:17:11 +0100186 if (rc != -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 goto removeseg;
188 } else {
189 dev_info->is_shared = 1;
190 switch (dev_info->segment_type) {
191 case SEG_TYPE_SR:
192 case SEG_TYPE_ER:
193 case SEG_TYPE_SC:
194 set_disk_ro(dev_info->gd,1);
195 }
196 }
197 } else if (inbuf[0] == '0') {
198 // reload segment in exclusive mode
199 if (dev_info->segment_type == SEG_TYPE_SC) {
200 PRINT_ERR("Segment type SC (%s) cannot be loaded in "
201 "non-shared mode\n", dev_info->segment_name);
202 rc = -EINVAL;
203 goto out;
204 }
205 rc = segment_modify_shared(dev_info->segment_name,
206 SEGMENT_EXCLUSIVE);
207 if (rc < 0) {
208 BUG_ON(rc == -EINVAL);
Gerald Schaefer444f0e52007-02-05 21:17:11 +0100209 if (rc != -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 goto removeseg;
211 } else {
212 dev_info->is_shared = 0;
213 set_disk_ro(dev_info->gd, 0);
214 }
215 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 rc = -EINVAL;
217 goto out;
218 }
219 rc = count;
220 goto out;
221
222removeseg:
223 PRINT_ERR("Could not reload segment %s, removing it now!\n",
224 dev_info->segment_name);
225 list_del(&dev_info->lh);
226
227 del_gendisk(dev_info->gd);
Al Viro1312f402006-03-12 11:02:03 -0500228 blk_cleanup_queue(dev_info->dcssblk_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 dev_info->gd->queue = NULL;
230 put_disk(dev_info->gd);
Gerald Schaefer931bb682007-11-05 11:10:09 +0100231 rc = device_schedule_callback(dev, dcssblk_unregister_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232out:
233 up_write(&dcssblk_devices_sem);
234 return rc;
235}
236
237/*
238 * device attribute for save operation on current copy
239 * of the segment. If the segment is busy, saving will
240 * become pending until it gets released, which can be
241 * undone by storing a non-true value to this entry.
242 * (show + store)
243 */
244static ssize_t
Yani Ioannoue404e272005-05-17 06:42:58 -0400245dcssblk_save_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
247 struct dcssblk_dev_info *dev_info;
248
249 dev_info = container_of(dev, struct dcssblk_dev_info, dev);
250 return sprintf(buf, dev_info->save_pending ? "1\n" : "0\n");
251}
252
253static ssize_t
Yani Ioannoue404e272005-05-17 06:42:58 -0400254dcssblk_save_store(struct device *dev, struct device_attribute *attr, const char *inbuf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
256 struct dcssblk_dev_info *dev_info;
257
Hongjie Yangded77fb2008-07-14 09:59:39 +0200258 if ((count > 1) && (inbuf[1] != '\n') && (inbuf[1] != '\0'))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 dev_info = container_of(dev, struct dcssblk_dev_info, dev);
261
262 down_write(&dcssblk_devices_sem);
263 if (inbuf[0] == '1') {
264 if (atomic_read(&dev_info->use_count) == 0) {
265 // device is idle => we save immediately
266 PRINT_INFO("Saving segment %s\n",
267 dev_info->segment_name);
268 segment_save(dev_info->segment_name);
269 } else {
270 // device is busy => we save it when it becomes
271 // idle in dcssblk_release
272 PRINT_INFO("Segment %s is currently busy, it will "
273 "be saved when it becomes idle...\n",
274 dev_info->segment_name);
275 dev_info->save_pending = 1;
276 }
277 } else if (inbuf[0] == '0') {
278 if (dev_info->save_pending) {
279 // device is busy & the user wants to undo his save
280 // request
281 dev_info->save_pending = 0;
282 PRINT_INFO("Pending save for segment %s deactivated\n",
283 dev_info->segment_name);
284 }
285 } else {
286 up_write(&dcssblk_devices_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 return -EINVAL;
288 }
289 up_write(&dcssblk_devices_sem);
290 return count;
291}
292
293/*
294 * device attribute for adding devices
295 */
296static ssize_t
Yani Ioannoue404e272005-05-17 06:42:58 -0400297dcssblk_add_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
299 int rc, i;
300 struct dcssblk_dev_info *dev_info;
301 char *local_buf;
302 unsigned long seg_byte_size;
303
304 dev_info = NULL;
305 if (dev != dcssblk_root_dev) {
306 rc = -EINVAL;
307 goto out_nobuf;
308 }
309 local_buf = kmalloc(count + 1, GFP_KERNEL);
310 if (local_buf == NULL) {
311 rc = -ENOMEM;
312 goto out_nobuf;
313 }
314 /*
315 * parse input
316 */
317 for (i = 0; ((buf[i] != '\0') && (buf[i] != '\n') && i < count); i++) {
318 local_buf[i] = toupper(buf[i]);
319 }
320 local_buf[i] = '\0';
321 if ((i == 0) || (i > 8)) {
322 rc = -ENAMETOOLONG;
323 goto out;
324 }
325 /*
326 * already loaded?
327 */
328 down_read(&dcssblk_devices_sem);
329 dev_info = dcssblk_get_device_by_name(local_buf);
330 up_read(&dcssblk_devices_sem);
331 if (dev_info != NULL) {
332 PRINT_WARN("Segment %s already loaded!\n", local_buf);
333 rc = -EEXIST;
334 goto out;
335 }
336 /*
337 * get a struct dcssblk_dev_info
338 */
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800339 dev_info = kzalloc(sizeof(struct dcssblk_dev_info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 if (dev_info == NULL) {
341 rc = -ENOMEM;
342 goto out;
343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345 strcpy(dev_info->segment_name, local_buf);
346 strlcpy(dev_info->dev.bus_id, local_buf, BUS_ID_SIZE);
347 dev_info->dev.release = dcssblk_release_segment;
348 INIT_LIST_HEAD(&dev_info->lh);
349
350 dev_info->gd = alloc_disk(DCSSBLK_MINORS_PER_DISK);
351 if (dev_info->gd == NULL) {
352 rc = -ENOMEM;
353 goto free_dev_info;
354 }
355 dev_info->gd->major = dcssblk_major;
356 dev_info->gd->fops = &dcssblk_devops;
357 dev_info->dcssblk_queue = blk_alloc_queue(GFP_KERNEL);
358 dev_info->gd->queue = dev_info->dcssblk_queue;
359 dev_info->gd->private_data = dev_info;
360 dev_info->gd->driverfs_dev = &dev_info->dev;
Heiko Carstensc5411db2008-02-05 16:50:50 +0100361 blk_queue_make_request(dev_info->dcssblk_queue, dcssblk_make_request);
362 blk_queue_hardsect_size(dev_info->dcssblk_queue, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 /*
364 * load the segment
365 */
366 rc = segment_load(local_buf, SEGMENT_SHARED,
367 &dev_info->start, &dev_info->end);
368 if (rc < 0) {
Martin Schwidefskyca683052008-04-17 07:46:31 +0200369 segment_warning(rc, dev_info->segment_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 goto dealloc_gendisk;
371 }
372 seg_byte_size = (dev_info->end - dev_info->start + 1);
373 set_capacity(dev_info->gd, seg_byte_size >> 9); // size in sectors
374 PRINT_INFO("Loaded segment %s, size = %lu Byte, "
375 "capacity = %lu (512 Byte) sectors\n", local_buf,
376 seg_byte_size, seg_byte_size >> 9);
377
378 dev_info->segment_type = rc;
379 dev_info->save_pending = 0;
380 dev_info->is_shared = 1;
381 dev_info->dev.parent = dcssblk_root_dev;
382
383 /*
384 * get minor, add to list
385 */
386 down_write(&dcssblk_devices_sem);
Gerald Schaefer04f64b52008-08-21 19:46:40 +0200387 if (dcssblk_get_segment_by_name(local_buf)) {
388 rc = -EEXIST;
389 goto release_gd;
390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 rc = dcssblk_assign_free_minor(dev_info);
392 if (rc) {
393 up_write(&dcssblk_devices_sem);
394 PRINT_ERR("No free minor number available! "
395 "Unloading segment...\n");
396 goto unload_seg;
397 }
398 sprintf(dev_info->gd->disk_name, "dcssblk%d",
399 dev_info->gd->first_minor);
400 list_add_tail(&dev_info->lh, &dcssblk_devices);
401
402 if (!try_module_get(THIS_MODULE)) {
403 rc = -ENODEV;
404 goto list_del;
405 }
406 /*
407 * register the device
408 */
409 rc = device_register(&dev_info->dev);
410 if (rc) {
411 PRINT_ERR("Segment %s could not be registered RC=%d\n",
412 local_buf, rc);
413 module_put(THIS_MODULE);
414 goto list_del;
415 }
416 get_device(&dev_info->dev);
417 rc = device_create_file(&dev_info->dev, &dev_attr_shared);
418 if (rc)
419 goto unregister_dev;
420 rc = device_create_file(&dev_info->dev, &dev_attr_save);
421 if (rc)
422 goto unregister_dev;
423
Christian Borntraeger436d1bc2007-12-04 16:09:03 +0100424 add_disk(dev_info->gd);
425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 switch (dev_info->segment_type) {
427 case SEG_TYPE_SR:
428 case SEG_TYPE_ER:
429 case SEG_TYPE_SC:
430 set_disk_ro(dev_info->gd,1);
431 break;
432 default:
433 set_disk_ro(dev_info->gd,0);
434 break;
435 }
436 PRINT_DEBUG("Segment %s loaded successfully\n", local_buf);
437 up_write(&dcssblk_devices_sem);
438 rc = count;
439 goto out;
440
441unregister_dev:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 list_del(&dev_info->lh);
Al Viro1312f402006-03-12 11:02:03 -0500443 blk_cleanup_queue(dev_info->dcssblk_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 dev_info->gd->queue = NULL;
445 put_disk(dev_info->gd);
446 device_unregister(&dev_info->dev);
447 segment_unload(dev_info->segment_name);
448 put_device(&dev_info->dev);
449 up_write(&dcssblk_devices_sem);
450 goto out;
451list_del:
452 list_del(&dev_info->lh);
453 up_write(&dcssblk_devices_sem);
454unload_seg:
455 segment_unload(local_buf);
456dealloc_gendisk:
Al Viro1312f402006-03-12 11:02:03 -0500457 blk_cleanup_queue(dev_info->dcssblk_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 dev_info->gd->queue = NULL;
459 put_disk(dev_info->gd);
460free_dev_info:
461 kfree(dev_info);
462out:
463 kfree(local_buf);
464out_nobuf:
465 return rc;
466}
467
468/*
469 * device attribute for removing devices
470 */
471static ssize_t
Yani Ioannoue404e272005-05-17 06:42:58 -0400472dcssblk_remove_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
474 struct dcssblk_dev_info *dev_info;
475 int rc, i;
476 char *local_buf;
477
478 if (dev != dcssblk_root_dev) {
479 return -EINVAL;
480 }
481 local_buf = kmalloc(count + 1, GFP_KERNEL);
482 if (local_buf == NULL) {
483 return -ENOMEM;
484 }
485 /*
486 * parse input
487 */
488 for (i = 0; ((*(buf+i)!='\0') && (*(buf+i)!='\n') && i < count); i++) {
489 local_buf[i] = toupper(buf[i]);
490 }
491 local_buf[i] = '\0';
492 if ((i == 0) || (i > 8)) {
493 rc = -ENAMETOOLONG;
494 goto out_buf;
495 }
496
497 down_write(&dcssblk_devices_sem);
498 dev_info = dcssblk_get_device_by_name(local_buf);
499 if (dev_info == NULL) {
500 up_write(&dcssblk_devices_sem);
501 PRINT_WARN("Segment %s is not loaded!\n", local_buf);
502 rc = -ENODEV;
503 goto out_buf;
504 }
505 if (atomic_read(&dev_info->use_count) != 0) {
506 up_write(&dcssblk_devices_sem);
507 PRINT_WARN("Segment %s is in use!\n", local_buf);
508 rc = -EBUSY;
509 goto out_buf;
510 }
511 list_del(&dev_info->lh);
512
513 del_gendisk(dev_info->gd);
Al Viro1312f402006-03-12 11:02:03 -0500514 blk_cleanup_queue(dev_info->dcssblk_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 dev_info->gd->queue = NULL;
516 put_disk(dev_info->gd);
517 device_unregister(&dev_info->dev);
518 segment_unload(dev_info->segment_name);
519 PRINT_DEBUG("Segment %s unloaded successfully\n",
520 dev_info->segment_name);
521 put_device(&dev_info->dev);
522 up_write(&dcssblk_devices_sem);
523
524 rc = count;
525out_buf:
526 kfree(local_buf);
527 return rc;
528}
529
530static int
531dcssblk_open(struct inode *inode, struct file *filp)
532{
533 struct dcssblk_dev_info *dev_info;
534 int rc;
535
536 dev_info = inode->i_bdev->bd_disk->private_data;
537 if (NULL == dev_info) {
538 rc = -ENODEV;
539 goto out;
540 }
541 atomic_inc(&dev_info->use_count);
542 inode->i_bdev->bd_block_size = 4096;
543 rc = 0;
544out:
545 return rc;
546}
547
548static int
549dcssblk_release(struct inode *inode, struct file *filp)
550{
551 struct dcssblk_dev_info *dev_info;
552 int rc;
553
554 dev_info = inode->i_bdev->bd_disk->private_data;
555 if (NULL == dev_info) {
556 rc = -ENODEV;
557 goto out;
558 }
559 down_write(&dcssblk_devices_sem);
560 if (atomic_dec_and_test(&dev_info->use_count)
561 && (dev_info->save_pending)) {
562 PRINT_INFO("Segment %s became idle and is being saved now\n",
563 dev_info->segment_name);
564 segment_save(dev_info->segment_name);
565 dev_info->save_pending = 0;
566 }
567 up_write(&dcssblk_devices_sem);
568 rc = 0;
569out:
570 return rc;
571}
572
573static int
Jens Axboe165125e2007-07-24 09:28:11 +0200574dcssblk_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
576 struct dcssblk_dev_info *dev_info;
577 struct bio_vec *bvec;
578 unsigned long index;
579 unsigned long page_addr;
580 unsigned long source_addr;
581 unsigned long bytes_done;
582 int i;
583
584 bytes_done = 0;
585 dev_info = bio->bi_bdev->bd_disk->private_data;
586 if (dev_info == NULL)
587 goto fail;
588 if ((bio->bi_sector & 7) != 0 || (bio->bi_size & 4095) != 0)
589 /* Request is not page-aligned. */
590 goto fail;
591 if (((bio->bi_size >> 9) + bio->bi_sector)
592 > get_capacity(bio->bi_bdev->bd_disk)) {
593 /* Request beyond end of DCSS segment. */
594 goto fail;
595 }
Carsten Otte420edbc2005-06-23 22:05:23 -0700596 /* verify data transfer direction */
597 if (dev_info->is_shared) {
598 switch (dev_info->segment_type) {
599 case SEG_TYPE_SR:
600 case SEG_TYPE_ER:
601 case SEG_TYPE_SC:
602 /* cannot write to these segments */
603 if (bio_data_dir(bio) == WRITE) {
604 PRINT_WARN("rejecting write to ro segment %s\n", dev_info->dev.bus_id);
605 goto fail;
606 }
607 }
608 }
609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 index = (bio->bi_sector >> 3);
611 bio_for_each_segment(bvec, bio, i) {
612 page_addr = (unsigned long)
613 page_address(bvec->bv_page) + bvec->bv_offset;
614 source_addr = dev_info->start + (index<<12) + bytes_done;
Roel Kluin39f73b22008-02-19 15:29:33 +0100615 if (unlikely((page_addr & 4095) != 0) || (bvec->bv_len & 4095) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 // More paranoia.
617 goto fail;
618 if (bio_data_dir(bio) == READ) {
619 memcpy((void*)page_addr, (void*)source_addr,
620 bvec->bv_len);
621 } else {
622 memcpy((void*)source_addr, (void*)page_addr,
623 bvec->bv_len);
624 }
625 bytes_done += bvec->bv_len;
626 }
NeilBrown6712ecf2007-09-27 12:47:43 +0200627 bio_endio(bio, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 return 0;
629fail:
NeilBrown6712ecf2007-09-27 12:47:43 +0200630 bio_io_error(bio);
Carsten Otte420edbc2005-06-23 22:05:23 -0700631 return 0;
632}
633
634static int
635dcssblk_direct_access (struct block_device *bdev, sector_t secnum,
Jared Hulbert30afcb42008-04-28 02:13:02 -0700636 void **kaddr, unsigned long *pfn)
Carsten Otte420edbc2005-06-23 22:05:23 -0700637{
638 struct dcssblk_dev_info *dev_info;
639 unsigned long pgoff;
640
641 dev_info = bdev->bd_disk->private_data;
642 if (!dev_info)
643 return -ENODEV;
644 if (secnum % (PAGE_SIZE/512))
645 return -EINVAL;
646 pgoff = secnum / (PAGE_SIZE / 512);
647 if ((pgoff+1)*PAGE_SIZE-1 > dev_info->end - dev_info->start)
648 return -ERANGE;
Jared Hulbert30afcb42008-04-28 02:13:02 -0700649 *kaddr = (void *) (dev_info->start+pgoff*PAGE_SIZE);
650 *pfn = virt_to_phys(*kaddr) >> PAGE_SHIFT;
651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 return 0;
653}
654
655static void
656dcssblk_check_params(void)
657{
658 int rc, i, j, k;
659 char buf[9];
660 struct dcssblk_dev_info *dev_info;
661
662 for (i = 0; (i < DCSSBLK_PARM_LEN) && (dcssblk_segments[i] != '\0');
663 i++) {
664 for (j = i; (dcssblk_segments[j] != ',') &&
665 (dcssblk_segments[j] != '\0') &&
666 (dcssblk_segments[j] != '(') &&
667 (j - i) < 8; j++)
668 {
669 buf[j-i] = dcssblk_segments[j];
670 }
671 buf[j-i] = '\0';
Cornelia Huckf901e5d2005-06-25 14:55:29 -0700672 rc = dcssblk_add_store(dcssblk_root_dev, NULL, buf, j-i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 if ((rc >= 0) && (dcssblk_segments[j] == '(')) {
674 for (k = 0; buf[k] != '\0'; k++)
675 buf[k] = toupper(buf[k]);
676 if (!strncmp(&dcssblk_segments[j], "(local)", 7)) {
677 down_read(&dcssblk_devices_sem);
678 dev_info = dcssblk_get_device_by_name(buf);
679 up_read(&dcssblk_devices_sem);
680 if (dev_info)
681 dcssblk_shared_store(&dev_info->dev,
Cornelia Huckf901e5d2005-06-25 14:55:29 -0700682 NULL, "0\n", 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
684 }
685 while ((dcssblk_segments[j] != ',') &&
686 (dcssblk_segments[j] != '\0'))
687 {
688 j++;
689 }
690 if (dcssblk_segments[j] == '\0')
691 break;
692 i = j;
693 }
694}
695
696/*
697 * The init/exit functions.
698 */
699static void __exit
700dcssblk_exit(void)
701{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 s390_root_dev_unregister(dcssblk_root_dev);
Akinobu Mita00d59402007-07-17 04:03:46 -0700703 unregister_blkdev(dcssblk_major, DCSSBLK_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704}
705
706static int __init
707dcssblk_init(void)
708{
709 int rc;
710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 dcssblk_root_dev = s390_root_dev_register("dcssblk");
Hongjie Yangded77fb2008-07-14 09:59:39 +0200712 if (IS_ERR(dcssblk_root_dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 return PTR_ERR(dcssblk_root_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 rc = device_create_file(dcssblk_root_dev, &dev_attr_add);
715 if (rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 s390_root_dev_unregister(dcssblk_root_dev);
717 return rc;
718 }
719 rc = device_create_file(dcssblk_root_dev, &dev_attr_remove);
720 if (rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 s390_root_dev_unregister(dcssblk_root_dev);
722 return rc;
723 }
724 rc = register_blkdev(0, DCSSBLK_NAME);
725 if (rc < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 s390_root_dev_unregister(dcssblk_root_dev);
727 return rc;
728 }
729 dcssblk_major = rc;
730 init_rwsem(&dcssblk_devices_sem);
731
732 dcssblk_check_params();
733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 return 0;
735}
736
737module_init(dcssblk_init);
738module_exit(dcssblk_exit);
739
740module_param_string(segments, dcssblk_segments, DCSSBLK_PARM_LEN, 0444);
741MODULE_PARM_DESC(segments, "Name of DCSS segment(s) to be loaded, "
742 "comma-separated list, each name max. 8 chars.\n"
743 "Adding \"(local)\" to segment name equals echoing 0 to "
744 "/sys/devices/dcssblk/<segment name>/shared after loading "
745 "the segment - \n"
746 "e.g. segments=\"mydcss1,mydcss2,mydcss3(local)\"");
747
748MODULE_LICENSE("GPL");