dcssblk.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. /*
  2. * dcssblk.c -- the S/390 block driver for dcss memory
  3. *
  4. * Authors: Carsten Otte, Stefan Weinhuber, Gerald Schaefer
  5. */
  6. #include <linux/module.h>
  7. #include <linux/moduleparam.h>
  8. #include <linux/ctype.h>
  9. #include <linux/errno.h>
  10. #include <linux/init.h>
  11. #include <linux/slab.h>
  12. #include <linux/blkdev.h>
  13. #include <asm/extmem.h>
  14. #include <asm/io.h>
  15. #include <linux/completion.h>
  16. #include <linux/interrupt.h>
  17. #include <asm/s390_rdev.h>
  18. //#define DCSSBLK_DEBUG /* Debug messages on/off */
  19. #define DCSSBLK_NAME "dcssblk"
  20. #define DCSSBLK_MINORS_PER_DISK 1
  21. #define DCSSBLK_PARM_LEN 400
  22. #ifdef DCSSBLK_DEBUG
  23. #define PRINT_DEBUG(x...) printk(KERN_DEBUG DCSSBLK_NAME " debug: " x)
  24. #else
  25. #define PRINT_DEBUG(x...) do {} while (0)
  26. #endif
  27. #define PRINT_INFO(x...) printk(KERN_INFO DCSSBLK_NAME " info: " x)
  28. #define PRINT_WARN(x...) printk(KERN_WARNING DCSSBLK_NAME " warning: " x)
  29. #define PRINT_ERR(x...) printk(KERN_ERR DCSSBLK_NAME " error: " x)
  30. static int dcssblk_open(struct inode *inode, struct file *filp);
  31. static int dcssblk_release(struct inode *inode, struct file *filp);
  32. static int dcssblk_make_request(struct request_queue *q, struct bio *bio);
  33. static int dcssblk_direct_access(struct block_device *bdev, sector_t secnum,
  34. unsigned long *data);
  35. static char dcssblk_segments[DCSSBLK_PARM_LEN] = "\0";
  36. static int dcssblk_major;
  37. static struct block_device_operations dcssblk_devops = {
  38. .owner = THIS_MODULE,
  39. .open = dcssblk_open,
  40. .release = dcssblk_release,
  41. .direct_access = dcssblk_direct_access,
  42. };
  43. static ssize_t dcssblk_add_store(struct device * dev, struct device_attribute *attr, const char * buf,
  44. size_t count);
  45. static ssize_t dcssblk_remove_store(struct device * dev, struct device_attribute *attr, const char * buf,
  46. size_t count);
  47. static ssize_t dcssblk_save_store(struct device * dev, struct device_attribute *attr, const char * buf,
  48. size_t count);
  49. static ssize_t dcssblk_save_show(struct device *dev, struct device_attribute *attr, char *buf);
  50. static ssize_t dcssblk_shared_store(struct device * dev, struct device_attribute *attr, const char * buf,
  51. size_t count);
  52. static ssize_t dcssblk_shared_show(struct device *dev, struct device_attribute *attr, char *buf);
  53. static DEVICE_ATTR(add, S_IWUSR, NULL, dcssblk_add_store);
  54. static DEVICE_ATTR(remove, S_IWUSR, NULL, dcssblk_remove_store);
  55. static DEVICE_ATTR(save, S_IWUSR | S_IRUGO, dcssblk_save_show,
  56. dcssblk_save_store);
  57. static DEVICE_ATTR(shared, S_IWUSR | S_IRUGO, dcssblk_shared_show,
  58. dcssblk_shared_store);
  59. static struct device *dcssblk_root_dev;
  60. struct dcssblk_dev_info {
  61. struct list_head lh;
  62. struct device dev;
  63. char segment_name[BUS_ID_SIZE];
  64. atomic_t use_count;
  65. struct gendisk *gd;
  66. unsigned long start;
  67. unsigned long end;
  68. int segment_type;
  69. unsigned char save_pending;
  70. unsigned char is_shared;
  71. struct request_queue *dcssblk_queue;
  72. };
  73. static LIST_HEAD(dcssblk_devices);
  74. static struct rw_semaphore dcssblk_devices_sem;
  75. /*
  76. * release function for segment device.
  77. */
  78. static void
  79. dcssblk_release_segment(struct device *dev)
  80. {
  81. PRINT_DEBUG("segment release fn called for %s\n", dev->bus_id);
  82. kfree(container_of(dev, struct dcssblk_dev_info, dev));
  83. module_put(THIS_MODULE);
  84. }
  85. /*
  86. * get a minor number. needs to be called with
  87. * down_write(&dcssblk_devices_sem) and the
  88. * device needs to be enqueued before the semaphore is
  89. * freed.
  90. */
  91. static int
  92. dcssblk_assign_free_minor(struct dcssblk_dev_info *dev_info)
  93. {
  94. int minor, found;
  95. struct dcssblk_dev_info *entry;
  96. if (dev_info == NULL)
  97. return -EINVAL;
  98. for (minor = 0; minor < (1<<MINORBITS); minor++) {
  99. found = 0;
  100. // test if minor available
  101. list_for_each_entry(entry, &dcssblk_devices, lh)
  102. if (minor == entry->gd->first_minor)
  103. found++;
  104. if (!found) break; // got unused minor
  105. }
  106. if (found)
  107. return -EBUSY;
  108. dev_info->gd->first_minor = minor;
  109. return 0;
  110. }
  111. /*
  112. * get the struct dcssblk_dev_info from dcssblk_devices
  113. * for the given name.
  114. * down_read(&dcssblk_devices_sem) must be held.
  115. */
  116. static struct dcssblk_dev_info *
  117. dcssblk_get_device_by_name(char *name)
  118. {
  119. struct dcssblk_dev_info *entry;
  120. list_for_each_entry(entry, &dcssblk_devices, lh) {
  121. if (!strcmp(name, entry->segment_name)) {
  122. return entry;
  123. }
  124. }
  125. return NULL;
  126. }
  127. static void dcssblk_unregister_callback(struct device *dev)
  128. {
  129. device_unregister(dev);
  130. put_device(dev);
  131. }
  132. /*
  133. * device attribute for switching shared/nonshared (exclusive)
  134. * operation (show + store)
  135. */
  136. static ssize_t
  137. dcssblk_shared_show(struct device *dev, struct device_attribute *attr, char *buf)
  138. {
  139. struct dcssblk_dev_info *dev_info;
  140. dev_info = container_of(dev, struct dcssblk_dev_info, dev);
  141. return sprintf(buf, dev_info->is_shared ? "1\n" : "0\n");
  142. }
  143. static ssize_t
  144. dcssblk_shared_store(struct device *dev, struct device_attribute *attr, const char *inbuf, size_t count)
  145. {
  146. struct dcssblk_dev_info *dev_info;
  147. int rc;
  148. if ((count > 1) && (inbuf[1] != '\n') && (inbuf[1] != '\0')) {
  149. PRINT_WARN("Invalid value, must be 0 or 1\n");
  150. return -EINVAL;
  151. }
  152. down_write(&dcssblk_devices_sem);
  153. dev_info = container_of(dev, struct dcssblk_dev_info, dev);
  154. if (atomic_read(&dev_info->use_count)) {
  155. PRINT_ERR("share: segment %s is busy!\n",
  156. dev_info->segment_name);
  157. rc = -EBUSY;
  158. goto out;
  159. }
  160. if (inbuf[0] == '1') {
  161. // reload segment in shared mode
  162. rc = segment_modify_shared(dev_info->segment_name,
  163. SEGMENT_SHARED);
  164. if (rc < 0) {
  165. BUG_ON(rc == -EINVAL);
  166. if (rc != -EAGAIN)
  167. goto removeseg;
  168. } else {
  169. dev_info->is_shared = 1;
  170. switch (dev_info->segment_type) {
  171. case SEG_TYPE_SR:
  172. case SEG_TYPE_ER:
  173. case SEG_TYPE_SC:
  174. set_disk_ro(dev_info->gd,1);
  175. }
  176. }
  177. } else if (inbuf[0] == '0') {
  178. // reload segment in exclusive mode
  179. if (dev_info->segment_type == SEG_TYPE_SC) {
  180. PRINT_ERR("Segment type SC (%s) cannot be loaded in "
  181. "non-shared mode\n", dev_info->segment_name);
  182. rc = -EINVAL;
  183. goto out;
  184. }
  185. rc = segment_modify_shared(dev_info->segment_name,
  186. SEGMENT_EXCLUSIVE);
  187. if (rc < 0) {
  188. BUG_ON(rc == -EINVAL);
  189. if (rc != -EAGAIN)
  190. goto removeseg;
  191. } else {
  192. dev_info->is_shared = 0;
  193. set_disk_ro(dev_info->gd, 0);
  194. }
  195. } else {
  196. PRINT_WARN("Invalid value, must be 0 or 1\n");
  197. rc = -EINVAL;
  198. goto out;
  199. }
  200. rc = count;
  201. goto out;
  202. removeseg:
  203. PRINT_ERR("Could not reload segment %s, removing it now!\n",
  204. dev_info->segment_name);
  205. list_del(&dev_info->lh);
  206. del_gendisk(dev_info->gd);
  207. blk_cleanup_queue(dev_info->dcssblk_queue);
  208. dev_info->gd->queue = NULL;
  209. put_disk(dev_info->gd);
  210. rc = device_schedule_callback(dev, dcssblk_unregister_callback);
  211. out:
  212. up_write(&dcssblk_devices_sem);
  213. return rc;
  214. }
  215. /*
  216. * device attribute for save operation on current copy
  217. * of the segment. If the segment is busy, saving will
  218. * become pending until it gets released, which can be
  219. * undone by storing a non-true value to this entry.
  220. * (show + store)
  221. */
  222. static ssize_t
  223. dcssblk_save_show(struct device *dev, struct device_attribute *attr, char *buf)
  224. {
  225. struct dcssblk_dev_info *dev_info;
  226. dev_info = container_of(dev, struct dcssblk_dev_info, dev);
  227. return sprintf(buf, dev_info->save_pending ? "1\n" : "0\n");
  228. }
  229. static ssize_t
  230. dcssblk_save_store(struct device *dev, struct device_attribute *attr, const char *inbuf, size_t count)
  231. {
  232. struct dcssblk_dev_info *dev_info;
  233. if ((count > 1) && (inbuf[1] != '\n') && (inbuf[1] != '\0')) {
  234. PRINT_WARN("Invalid value, must be 0 or 1\n");
  235. return -EINVAL;
  236. }
  237. dev_info = container_of(dev, struct dcssblk_dev_info, dev);
  238. down_write(&dcssblk_devices_sem);
  239. if (inbuf[0] == '1') {
  240. if (atomic_read(&dev_info->use_count) == 0) {
  241. // device is idle => we save immediately
  242. PRINT_INFO("Saving segment %s\n",
  243. dev_info->segment_name);
  244. segment_save(dev_info->segment_name);
  245. } else {
  246. // device is busy => we save it when it becomes
  247. // idle in dcssblk_release
  248. PRINT_INFO("Segment %s is currently busy, it will "
  249. "be saved when it becomes idle...\n",
  250. dev_info->segment_name);
  251. dev_info->save_pending = 1;
  252. }
  253. } else if (inbuf[0] == '0') {
  254. if (dev_info->save_pending) {
  255. // device is busy & the user wants to undo his save
  256. // request
  257. dev_info->save_pending = 0;
  258. PRINT_INFO("Pending save for segment %s deactivated\n",
  259. dev_info->segment_name);
  260. }
  261. } else {
  262. up_write(&dcssblk_devices_sem);
  263. PRINT_WARN("Invalid value, must be 0 or 1\n");
  264. return -EINVAL;
  265. }
  266. up_write(&dcssblk_devices_sem);
  267. return count;
  268. }
  269. /*
  270. * device attribute for adding devices
  271. */
  272. static ssize_t
  273. dcssblk_add_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  274. {
  275. int rc, i;
  276. struct dcssblk_dev_info *dev_info;
  277. char *local_buf;
  278. unsigned long seg_byte_size;
  279. dev_info = NULL;
  280. if (dev != dcssblk_root_dev) {
  281. rc = -EINVAL;
  282. goto out_nobuf;
  283. }
  284. local_buf = kmalloc(count + 1, GFP_KERNEL);
  285. if (local_buf == NULL) {
  286. rc = -ENOMEM;
  287. goto out_nobuf;
  288. }
  289. /*
  290. * parse input
  291. */
  292. for (i = 0; ((buf[i] != '\0') && (buf[i] != '\n') && i < count); i++) {
  293. local_buf[i] = toupper(buf[i]);
  294. }
  295. local_buf[i] = '\0';
  296. if ((i == 0) || (i > 8)) {
  297. rc = -ENAMETOOLONG;
  298. goto out;
  299. }
  300. /*
  301. * already loaded?
  302. */
  303. down_read(&dcssblk_devices_sem);
  304. dev_info = dcssblk_get_device_by_name(local_buf);
  305. up_read(&dcssblk_devices_sem);
  306. if (dev_info != NULL) {
  307. PRINT_WARN("Segment %s already loaded!\n", local_buf);
  308. rc = -EEXIST;
  309. goto out;
  310. }
  311. /*
  312. * get a struct dcssblk_dev_info
  313. */
  314. dev_info = kzalloc(sizeof(struct dcssblk_dev_info), GFP_KERNEL);
  315. if (dev_info == NULL) {
  316. rc = -ENOMEM;
  317. goto out;
  318. }
  319. strcpy(dev_info->segment_name, local_buf);
  320. strlcpy(dev_info->dev.bus_id, local_buf, BUS_ID_SIZE);
  321. dev_info->dev.release = dcssblk_release_segment;
  322. INIT_LIST_HEAD(&dev_info->lh);
  323. dev_info->gd = alloc_disk(DCSSBLK_MINORS_PER_DISK);
  324. if (dev_info->gd == NULL) {
  325. rc = -ENOMEM;
  326. goto free_dev_info;
  327. }
  328. dev_info->gd->major = dcssblk_major;
  329. dev_info->gd->fops = &dcssblk_devops;
  330. dev_info->dcssblk_queue = blk_alloc_queue(GFP_KERNEL);
  331. dev_info->gd->queue = dev_info->dcssblk_queue;
  332. dev_info->gd->private_data = dev_info;
  333. dev_info->gd->driverfs_dev = &dev_info->dev;
  334. blk_queue_make_request(dev_info->dcssblk_queue, dcssblk_make_request);
  335. blk_queue_hardsect_size(dev_info->dcssblk_queue, 4096);
  336. /*
  337. * load the segment
  338. */
  339. rc = segment_load(local_buf, SEGMENT_SHARED,
  340. &dev_info->start, &dev_info->end);
  341. if (rc < 0) {
  342. segment_warning(rc, dev_info->segment_name);
  343. goto dealloc_gendisk;
  344. }
  345. seg_byte_size = (dev_info->end - dev_info->start + 1);
  346. set_capacity(dev_info->gd, seg_byte_size >> 9); // size in sectors
  347. PRINT_INFO("Loaded segment %s, size = %lu Byte, "
  348. "capacity = %lu (512 Byte) sectors\n", local_buf,
  349. seg_byte_size, seg_byte_size >> 9);
  350. dev_info->segment_type = rc;
  351. dev_info->save_pending = 0;
  352. dev_info->is_shared = 1;
  353. dev_info->dev.parent = dcssblk_root_dev;
  354. /*
  355. * get minor, add to list
  356. */
  357. down_write(&dcssblk_devices_sem);
  358. rc = dcssblk_assign_free_minor(dev_info);
  359. if (rc) {
  360. up_write(&dcssblk_devices_sem);
  361. PRINT_ERR("No free minor number available! "
  362. "Unloading segment...\n");
  363. goto unload_seg;
  364. }
  365. sprintf(dev_info->gd->disk_name, "dcssblk%d",
  366. dev_info->gd->first_minor);
  367. list_add_tail(&dev_info->lh, &dcssblk_devices);
  368. if (!try_module_get(THIS_MODULE)) {
  369. rc = -ENODEV;
  370. goto list_del;
  371. }
  372. /*
  373. * register the device
  374. */
  375. rc = device_register(&dev_info->dev);
  376. if (rc) {
  377. PRINT_ERR("Segment %s could not be registered RC=%d\n",
  378. local_buf, rc);
  379. module_put(THIS_MODULE);
  380. goto list_del;
  381. }
  382. get_device(&dev_info->dev);
  383. rc = device_create_file(&dev_info->dev, &dev_attr_shared);
  384. if (rc)
  385. goto unregister_dev;
  386. rc = device_create_file(&dev_info->dev, &dev_attr_save);
  387. if (rc)
  388. goto unregister_dev;
  389. add_disk(dev_info->gd);
  390. switch (dev_info->segment_type) {
  391. case SEG_TYPE_SR:
  392. case SEG_TYPE_ER:
  393. case SEG_TYPE_SC:
  394. set_disk_ro(dev_info->gd,1);
  395. break;
  396. default:
  397. set_disk_ro(dev_info->gd,0);
  398. break;
  399. }
  400. PRINT_DEBUG("Segment %s loaded successfully\n", local_buf);
  401. up_write(&dcssblk_devices_sem);
  402. rc = count;
  403. goto out;
  404. unregister_dev:
  405. PRINT_ERR("device_create_file() failed!\n");
  406. list_del(&dev_info->lh);
  407. blk_cleanup_queue(dev_info->dcssblk_queue);
  408. dev_info->gd->queue = NULL;
  409. put_disk(dev_info->gd);
  410. device_unregister(&dev_info->dev);
  411. segment_unload(dev_info->segment_name);
  412. put_device(&dev_info->dev);
  413. up_write(&dcssblk_devices_sem);
  414. goto out;
  415. list_del:
  416. list_del(&dev_info->lh);
  417. up_write(&dcssblk_devices_sem);
  418. unload_seg:
  419. segment_unload(local_buf);
  420. dealloc_gendisk:
  421. blk_cleanup_queue(dev_info->dcssblk_queue);
  422. dev_info->gd->queue = NULL;
  423. put_disk(dev_info->gd);
  424. free_dev_info:
  425. kfree(dev_info);
  426. out:
  427. kfree(local_buf);
  428. out_nobuf:
  429. return rc;
  430. }
  431. /*
  432. * device attribute for removing devices
  433. */
  434. static ssize_t
  435. dcssblk_remove_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  436. {
  437. struct dcssblk_dev_info *dev_info;
  438. int rc, i;
  439. char *local_buf;
  440. if (dev != dcssblk_root_dev) {
  441. return -EINVAL;
  442. }
  443. local_buf = kmalloc(count + 1, GFP_KERNEL);
  444. if (local_buf == NULL) {
  445. return -ENOMEM;
  446. }
  447. /*
  448. * parse input
  449. */
  450. for (i = 0; ((*(buf+i)!='\0') && (*(buf+i)!='\n') && i < count); i++) {
  451. local_buf[i] = toupper(buf[i]);
  452. }
  453. local_buf[i] = '\0';
  454. if ((i == 0) || (i > 8)) {
  455. rc = -ENAMETOOLONG;
  456. goto out_buf;
  457. }
  458. down_write(&dcssblk_devices_sem);
  459. dev_info = dcssblk_get_device_by_name(local_buf);
  460. if (dev_info == NULL) {
  461. up_write(&dcssblk_devices_sem);
  462. PRINT_WARN("Segment %s is not loaded!\n", local_buf);
  463. rc = -ENODEV;
  464. goto out_buf;
  465. }
  466. if (atomic_read(&dev_info->use_count) != 0) {
  467. up_write(&dcssblk_devices_sem);
  468. PRINT_WARN("Segment %s is in use!\n", local_buf);
  469. rc = -EBUSY;
  470. goto out_buf;
  471. }
  472. list_del(&dev_info->lh);
  473. del_gendisk(dev_info->gd);
  474. blk_cleanup_queue(dev_info->dcssblk_queue);
  475. dev_info->gd->queue = NULL;
  476. put_disk(dev_info->gd);
  477. device_unregister(&dev_info->dev);
  478. segment_unload(dev_info->segment_name);
  479. PRINT_DEBUG("Segment %s unloaded successfully\n",
  480. dev_info->segment_name);
  481. put_device(&dev_info->dev);
  482. up_write(&dcssblk_devices_sem);
  483. rc = count;
  484. out_buf:
  485. kfree(local_buf);
  486. return rc;
  487. }
  488. static int
  489. dcssblk_open(struct inode *inode, struct file *filp)
  490. {
  491. struct dcssblk_dev_info *dev_info;
  492. int rc;
  493. dev_info = inode->i_bdev->bd_disk->private_data;
  494. if (NULL == dev_info) {
  495. rc = -ENODEV;
  496. goto out;
  497. }
  498. atomic_inc(&dev_info->use_count);
  499. inode->i_bdev->bd_block_size = 4096;
  500. rc = 0;
  501. out:
  502. return rc;
  503. }
  504. static int
  505. dcssblk_release(struct inode *inode, struct file *filp)
  506. {
  507. struct dcssblk_dev_info *dev_info;
  508. int rc;
  509. dev_info = inode->i_bdev->bd_disk->private_data;
  510. if (NULL == dev_info) {
  511. rc = -ENODEV;
  512. goto out;
  513. }
  514. down_write(&dcssblk_devices_sem);
  515. if (atomic_dec_and_test(&dev_info->use_count)
  516. && (dev_info->save_pending)) {
  517. PRINT_INFO("Segment %s became idle and is being saved now\n",
  518. dev_info->segment_name);
  519. segment_save(dev_info->segment_name);
  520. dev_info->save_pending = 0;
  521. }
  522. up_write(&dcssblk_devices_sem);
  523. rc = 0;
  524. out:
  525. return rc;
  526. }
  527. static int
  528. dcssblk_make_request(struct request_queue *q, struct bio *bio)
  529. {
  530. struct dcssblk_dev_info *dev_info;
  531. struct bio_vec *bvec;
  532. unsigned long index;
  533. unsigned long page_addr;
  534. unsigned long source_addr;
  535. unsigned long bytes_done;
  536. int i;
  537. bytes_done = 0;
  538. dev_info = bio->bi_bdev->bd_disk->private_data;
  539. if (dev_info == NULL)
  540. goto fail;
  541. if ((bio->bi_sector & 7) != 0 || (bio->bi_size & 4095) != 0)
  542. /* Request is not page-aligned. */
  543. goto fail;
  544. if (((bio->bi_size >> 9) + bio->bi_sector)
  545. > get_capacity(bio->bi_bdev->bd_disk)) {
  546. /* Request beyond end of DCSS segment. */
  547. goto fail;
  548. }
  549. /* verify data transfer direction */
  550. if (dev_info->is_shared) {
  551. switch (dev_info->segment_type) {
  552. case SEG_TYPE_SR:
  553. case SEG_TYPE_ER:
  554. case SEG_TYPE_SC:
  555. /* cannot write to these segments */
  556. if (bio_data_dir(bio) == WRITE) {
  557. PRINT_WARN("rejecting write to ro segment %s\n", dev_info->dev.bus_id);
  558. goto fail;
  559. }
  560. }
  561. }
  562. index = (bio->bi_sector >> 3);
  563. bio_for_each_segment(bvec, bio, i) {
  564. page_addr = (unsigned long)
  565. page_address(bvec->bv_page) + bvec->bv_offset;
  566. source_addr = dev_info->start + (index<<12) + bytes_done;
  567. if (unlikely((page_addr & 4095) != 0) || (bvec->bv_len & 4095) != 0)
  568. // More paranoia.
  569. goto fail;
  570. if (bio_data_dir(bio) == READ) {
  571. memcpy((void*)page_addr, (void*)source_addr,
  572. bvec->bv_len);
  573. } else {
  574. memcpy((void*)source_addr, (void*)page_addr,
  575. bvec->bv_len);
  576. }
  577. bytes_done += bvec->bv_len;
  578. }
  579. bio_endio(bio, 0);
  580. return 0;
  581. fail:
  582. bio_io_error(bio);
  583. return 0;
  584. }
  585. static int
  586. dcssblk_direct_access (struct block_device *bdev, sector_t secnum,
  587. unsigned long *data)
  588. {
  589. struct dcssblk_dev_info *dev_info;
  590. unsigned long pgoff;
  591. dev_info = bdev->bd_disk->private_data;
  592. if (!dev_info)
  593. return -ENODEV;
  594. if (secnum % (PAGE_SIZE/512))
  595. return -EINVAL;
  596. pgoff = secnum / (PAGE_SIZE / 512);
  597. if ((pgoff+1)*PAGE_SIZE-1 > dev_info->end - dev_info->start)
  598. return -ERANGE;
  599. *data = (unsigned long) (dev_info->start+pgoff*PAGE_SIZE);
  600. return 0;
  601. }
  602. static void
  603. dcssblk_check_params(void)
  604. {
  605. int rc, i, j, k;
  606. char buf[9];
  607. struct dcssblk_dev_info *dev_info;
  608. for (i = 0; (i < DCSSBLK_PARM_LEN) && (dcssblk_segments[i] != '\0');
  609. i++) {
  610. for (j = i; (dcssblk_segments[j] != ',') &&
  611. (dcssblk_segments[j] != '\0') &&
  612. (dcssblk_segments[j] != '(') &&
  613. (j - i) < 8; j++)
  614. {
  615. buf[j-i] = dcssblk_segments[j];
  616. }
  617. buf[j-i] = '\0';
  618. rc = dcssblk_add_store(dcssblk_root_dev, NULL, buf, j-i);
  619. if ((rc >= 0) && (dcssblk_segments[j] == '(')) {
  620. for (k = 0; buf[k] != '\0'; k++)
  621. buf[k] = toupper(buf[k]);
  622. if (!strncmp(&dcssblk_segments[j], "(local)", 7)) {
  623. down_read(&dcssblk_devices_sem);
  624. dev_info = dcssblk_get_device_by_name(buf);
  625. up_read(&dcssblk_devices_sem);
  626. if (dev_info)
  627. dcssblk_shared_store(&dev_info->dev,
  628. NULL, "0\n", 2);
  629. }
  630. }
  631. while ((dcssblk_segments[j] != ',') &&
  632. (dcssblk_segments[j] != '\0'))
  633. {
  634. j++;
  635. }
  636. if (dcssblk_segments[j] == '\0')
  637. break;
  638. i = j;
  639. }
  640. }
  641. /*
  642. * The init/exit functions.
  643. */
  644. static void __exit
  645. dcssblk_exit(void)
  646. {
  647. PRINT_DEBUG("DCSSBLOCK EXIT...\n");
  648. s390_root_dev_unregister(dcssblk_root_dev);
  649. unregister_blkdev(dcssblk_major, DCSSBLK_NAME);
  650. PRINT_DEBUG("...finished!\n");
  651. }
  652. static int __init
  653. dcssblk_init(void)
  654. {
  655. int rc;
  656. PRINT_DEBUG("DCSSBLOCK INIT...\n");
  657. dcssblk_root_dev = s390_root_dev_register("dcssblk");
  658. if (IS_ERR(dcssblk_root_dev)) {
  659. PRINT_ERR("device_register() failed!\n");
  660. return PTR_ERR(dcssblk_root_dev);
  661. }
  662. rc = device_create_file(dcssblk_root_dev, &dev_attr_add);
  663. if (rc) {
  664. PRINT_ERR("device_create_file(add) failed!\n");
  665. s390_root_dev_unregister(dcssblk_root_dev);
  666. return rc;
  667. }
  668. rc = device_create_file(dcssblk_root_dev, &dev_attr_remove);
  669. if (rc) {
  670. PRINT_ERR("device_create_file(remove) failed!\n");
  671. s390_root_dev_unregister(dcssblk_root_dev);
  672. return rc;
  673. }
  674. rc = register_blkdev(0, DCSSBLK_NAME);
  675. if (rc < 0) {
  676. PRINT_ERR("Can't get dynamic major!\n");
  677. s390_root_dev_unregister(dcssblk_root_dev);
  678. return rc;
  679. }
  680. dcssblk_major = rc;
  681. init_rwsem(&dcssblk_devices_sem);
  682. dcssblk_check_params();
  683. PRINT_DEBUG("...finished!\n");
  684. return 0;
  685. }
  686. module_init(dcssblk_init);
  687. module_exit(dcssblk_exit);
  688. module_param_string(segments, dcssblk_segments, DCSSBLK_PARM_LEN, 0444);
  689. MODULE_PARM_DESC(segments, "Name of DCSS segment(s) to be loaded, "
  690. "comma-separated list, each name max. 8 chars.\n"
  691. "Adding \"(local)\" to segment name equals echoing 0 to "
  692. "/sys/devices/dcssblk/<segment name>/shared after loading "
  693. "the segment - \n"
  694. "e.g. segments=\"mydcss1,mydcss2,mydcss3(local)\"");
  695. MODULE_LICENSE("GPL");