dcssblk.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  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. void **kaddr, unsigned long *pfn);
  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. return -EINVAL;
  150. down_write(&dcssblk_devices_sem);
  151. dev_info = container_of(dev, struct dcssblk_dev_info, dev);
  152. if (atomic_read(&dev_info->use_count)) {
  153. PRINT_ERR("share: segment %s is busy!\n",
  154. dev_info->segment_name);
  155. rc = -EBUSY;
  156. goto out;
  157. }
  158. if (inbuf[0] == '1') {
  159. // reload segment in shared mode
  160. rc = segment_modify_shared(dev_info->segment_name,
  161. SEGMENT_SHARED);
  162. if (rc < 0) {
  163. BUG_ON(rc == -EINVAL);
  164. if (rc != -EAGAIN)
  165. goto removeseg;
  166. } else {
  167. dev_info->is_shared = 1;
  168. switch (dev_info->segment_type) {
  169. case SEG_TYPE_SR:
  170. case SEG_TYPE_ER:
  171. case SEG_TYPE_SC:
  172. set_disk_ro(dev_info->gd,1);
  173. }
  174. }
  175. } else if (inbuf[0] == '0') {
  176. // reload segment in exclusive mode
  177. if (dev_info->segment_type == SEG_TYPE_SC) {
  178. PRINT_ERR("Segment type SC (%s) cannot be loaded in "
  179. "non-shared mode\n", dev_info->segment_name);
  180. rc = -EINVAL;
  181. goto out;
  182. }
  183. rc = segment_modify_shared(dev_info->segment_name,
  184. SEGMENT_EXCLUSIVE);
  185. if (rc < 0) {
  186. BUG_ON(rc == -EINVAL);
  187. if (rc != -EAGAIN)
  188. goto removeseg;
  189. } else {
  190. dev_info->is_shared = 0;
  191. set_disk_ro(dev_info->gd, 0);
  192. }
  193. } else {
  194. rc = -EINVAL;
  195. goto out;
  196. }
  197. rc = count;
  198. goto out;
  199. removeseg:
  200. PRINT_ERR("Could not reload segment %s, removing it now!\n",
  201. dev_info->segment_name);
  202. list_del(&dev_info->lh);
  203. del_gendisk(dev_info->gd);
  204. blk_cleanup_queue(dev_info->dcssblk_queue);
  205. dev_info->gd->queue = NULL;
  206. put_disk(dev_info->gd);
  207. rc = device_schedule_callback(dev, dcssblk_unregister_callback);
  208. out:
  209. up_write(&dcssblk_devices_sem);
  210. return rc;
  211. }
  212. /*
  213. * device attribute for save operation on current copy
  214. * of the segment. If the segment is busy, saving will
  215. * become pending until it gets released, which can be
  216. * undone by storing a non-true value to this entry.
  217. * (show + store)
  218. */
  219. static ssize_t
  220. dcssblk_save_show(struct device *dev, struct device_attribute *attr, char *buf)
  221. {
  222. struct dcssblk_dev_info *dev_info;
  223. dev_info = container_of(dev, struct dcssblk_dev_info, dev);
  224. return sprintf(buf, dev_info->save_pending ? "1\n" : "0\n");
  225. }
  226. static ssize_t
  227. dcssblk_save_store(struct device *dev, struct device_attribute *attr, const char *inbuf, size_t count)
  228. {
  229. struct dcssblk_dev_info *dev_info;
  230. if ((count > 1) && (inbuf[1] != '\n') && (inbuf[1] != '\0'))
  231. return -EINVAL;
  232. dev_info = container_of(dev, struct dcssblk_dev_info, dev);
  233. down_write(&dcssblk_devices_sem);
  234. if (inbuf[0] == '1') {
  235. if (atomic_read(&dev_info->use_count) == 0) {
  236. // device is idle => we save immediately
  237. PRINT_INFO("Saving segment %s\n",
  238. dev_info->segment_name);
  239. segment_save(dev_info->segment_name);
  240. } else {
  241. // device is busy => we save it when it becomes
  242. // idle in dcssblk_release
  243. PRINT_INFO("Segment %s is currently busy, it will "
  244. "be saved when it becomes idle...\n",
  245. dev_info->segment_name);
  246. dev_info->save_pending = 1;
  247. }
  248. } else if (inbuf[0] == '0') {
  249. if (dev_info->save_pending) {
  250. // device is busy & the user wants to undo his save
  251. // request
  252. dev_info->save_pending = 0;
  253. PRINT_INFO("Pending save for segment %s deactivated\n",
  254. dev_info->segment_name);
  255. }
  256. } else {
  257. up_write(&dcssblk_devices_sem);
  258. return -EINVAL;
  259. }
  260. up_write(&dcssblk_devices_sem);
  261. return count;
  262. }
  263. /*
  264. * device attribute for adding devices
  265. */
  266. static ssize_t
  267. dcssblk_add_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  268. {
  269. int rc, i;
  270. struct dcssblk_dev_info *dev_info;
  271. char *local_buf;
  272. unsigned long seg_byte_size;
  273. dev_info = NULL;
  274. if (dev != dcssblk_root_dev) {
  275. rc = -EINVAL;
  276. goto out_nobuf;
  277. }
  278. local_buf = kmalloc(count + 1, GFP_KERNEL);
  279. if (local_buf == NULL) {
  280. rc = -ENOMEM;
  281. goto out_nobuf;
  282. }
  283. /*
  284. * parse input
  285. */
  286. for (i = 0; ((buf[i] != '\0') && (buf[i] != '\n') && i < count); i++) {
  287. local_buf[i] = toupper(buf[i]);
  288. }
  289. local_buf[i] = '\0';
  290. if ((i == 0) || (i > 8)) {
  291. rc = -ENAMETOOLONG;
  292. goto out;
  293. }
  294. /*
  295. * already loaded?
  296. */
  297. down_read(&dcssblk_devices_sem);
  298. dev_info = dcssblk_get_device_by_name(local_buf);
  299. up_read(&dcssblk_devices_sem);
  300. if (dev_info != NULL) {
  301. PRINT_WARN("Segment %s already loaded!\n", local_buf);
  302. rc = -EEXIST;
  303. goto out;
  304. }
  305. /*
  306. * get a struct dcssblk_dev_info
  307. */
  308. dev_info = kzalloc(sizeof(struct dcssblk_dev_info), GFP_KERNEL);
  309. if (dev_info == NULL) {
  310. rc = -ENOMEM;
  311. goto out;
  312. }
  313. strcpy(dev_info->segment_name, local_buf);
  314. strlcpy(dev_info->dev.bus_id, local_buf, BUS_ID_SIZE);
  315. dev_info->dev.release = dcssblk_release_segment;
  316. INIT_LIST_HEAD(&dev_info->lh);
  317. dev_info->gd = alloc_disk(DCSSBLK_MINORS_PER_DISK);
  318. if (dev_info->gd == NULL) {
  319. rc = -ENOMEM;
  320. goto free_dev_info;
  321. }
  322. dev_info->gd->major = dcssblk_major;
  323. dev_info->gd->fops = &dcssblk_devops;
  324. dev_info->dcssblk_queue = blk_alloc_queue(GFP_KERNEL);
  325. dev_info->gd->queue = dev_info->dcssblk_queue;
  326. dev_info->gd->private_data = dev_info;
  327. dev_info->gd->driverfs_dev = &dev_info->dev;
  328. blk_queue_make_request(dev_info->dcssblk_queue, dcssblk_make_request);
  329. blk_queue_hardsect_size(dev_info->dcssblk_queue, 4096);
  330. /*
  331. * load the segment
  332. */
  333. rc = segment_load(local_buf, SEGMENT_SHARED,
  334. &dev_info->start, &dev_info->end);
  335. if (rc < 0) {
  336. segment_warning(rc, dev_info->segment_name);
  337. goto dealloc_gendisk;
  338. }
  339. seg_byte_size = (dev_info->end - dev_info->start + 1);
  340. set_capacity(dev_info->gd, seg_byte_size >> 9); // size in sectors
  341. PRINT_INFO("Loaded segment %s, size = %lu Byte, "
  342. "capacity = %lu (512 Byte) sectors\n", local_buf,
  343. seg_byte_size, seg_byte_size >> 9);
  344. dev_info->segment_type = rc;
  345. dev_info->save_pending = 0;
  346. dev_info->is_shared = 1;
  347. dev_info->dev.parent = dcssblk_root_dev;
  348. /*
  349. * get minor, add to list
  350. */
  351. down_write(&dcssblk_devices_sem);
  352. if (dcssblk_get_segment_by_name(local_buf)) {
  353. rc = -EEXIST;
  354. goto release_gd;
  355. }
  356. rc = dcssblk_assign_free_minor(dev_info);
  357. if (rc) {
  358. up_write(&dcssblk_devices_sem);
  359. PRINT_ERR("No free minor number available! "
  360. "Unloading segment...\n");
  361. goto unload_seg;
  362. }
  363. sprintf(dev_info->gd->disk_name, "dcssblk%d",
  364. dev_info->gd->first_minor);
  365. list_add_tail(&dev_info->lh, &dcssblk_devices);
  366. if (!try_module_get(THIS_MODULE)) {
  367. rc = -ENODEV;
  368. goto list_del;
  369. }
  370. /*
  371. * register the device
  372. */
  373. rc = device_register(&dev_info->dev);
  374. if (rc) {
  375. PRINT_ERR("Segment %s could not be registered RC=%d\n",
  376. local_buf, rc);
  377. module_put(THIS_MODULE);
  378. goto list_del;
  379. }
  380. get_device(&dev_info->dev);
  381. rc = device_create_file(&dev_info->dev, &dev_attr_shared);
  382. if (rc)
  383. goto unregister_dev;
  384. rc = device_create_file(&dev_info->dev, &dev_attr_save);
  385. if (rc)
  386. goto unregister_dev;
  387. add_disk(dev_info->gd);
  388. switch (dev_info->segment_type) {
  389. case SEG_TYPE_SR:
  390. case SEG_TYPE_ER:
  391. case SEG_TYPE_SC:
  392. set_disk_ro(dev_info->gd,1);
  393. break;
  394. default:
  395. set_disk_ro(dev_info->gd,0);
  396. break;
  397. }
  398. PRINT_DEBUG("Segment %s loaded successfully\n", local_buf);
  399. up_write(&dcssblk_devices_sem);
  400. rc = count;
  401. goto out;
  402. unregister_dev:
  403. list_del(&dev_info->lh);
  404. blk_cleanup_queue(dev_info->dcssblk_queue);
  405. dev_info->gd->queue = NULL;
  406. put_disk(dev_info->gd);
  407. device_unregister(&dev_info->dev);
  408. segment_unload(dev_info->segment_name);
  409. put_device(&dev_info->dev);
  410. up_write(&dcssblk_devices_sem);
  411. goto out;
  412. list_del:
  413. list_del(&dev_info->lh);
  414. up_write(&dcssblk_devices_sem);
  415. unload_seg:
  416. segment_unload(local_buf);
  417. dealloc_gendisk:
  418. blk_cleanup_queue(dev_info->dcssblk_queue);
  419. dev_info->gd->queue = NULL;
  420. put_disk(dev_info->gd);
  421. free_dev_info:
  422. kfree(dev_info);
  423. out:
  424. kfree(local_buf);
  425. out_nobuf:
  426. return rc;
  427. }
  428. /*
  429. * device attribute for removing devices
  430. */
  431. static ssize_t
  432. dcssblk_remove_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  433. {
  434. struct dcssblk_dev_info *dev_info;
  435. int rc, i;
  436. char *local_buf;
  437. if (dev != dcssblk_root_dev) {
  438. return -EINVAL;
  439. }
  440. local_buf = kmalloc(count + 1, GFP_KERNEL);
  441. if (local_buf == NULL) {
  442. return -ENOMEM;
  443. }
  444. /*
  445. * parse input
  446. */
  447. for (i = 0; ((*(buf+i)!='\0') && (*(buf+i)!='\n') && i < count); i++) {
  448. local_buf[i] = toupper(buf[i]);
  449. }
  450. local_buf[i] = '\0';
  451. if ((i == 0) || (i > 8)) {
  452. rc = -ENAMETOOLONG;
  453. goto out_buf;
  454. }
  455. down_write(&dcssblk_devices_sem);
  456. dev_info = dcssblk_get_device_by_name(local_buf);
  457. if (dev_info == NULL) {
  458. up_write(&dcssblk_devices_sem);
  459. PRINT_WARN("Segment %s is not loaded!\n", local_buf);
  460. rc = -ENODEV;
  461. goto out_buf;
  462. }
  463. if (atomic_read(&dev_info->use_count) != 0) {
  464. up_write(&dcssblk_devices_sem);
  465. PRINT_WARN("Segment %s is in use!\n", local_buf);
  466. rc = -EBUSY;
  467. goto out_buf;
  468. }
  469. list_del(&dev_info->lh);
  470. del_gendisk(dev_info->gd);
  471. blk_cleanup_queue(dev_info->dcssblk_queue);
  472. dev_info->gd->queue = NULL;
  473. put_disk(dev_info->gd);
  474. device_unregister(&dev_info->dev);
  475. segment_unload(dev_info->segment_name);
  476. PRINT_DEBUG("Segment %s unloaded successfully\n",
  477. dev_info->segment_name);
  478. put_device(&dev_info->dev);
  479. up_write(&dcssblk_devices_sem);
  480. rc = count;
  481. out_buf:
  482. kfree(local_buf);
  483. return rc;
  484. }
  485. static int
  486. dcssblk_open(struct inode *inode, struct file *filp)
  487. {
  488. struct dcssblk_dev_info *dev_info;
  489. int rc;
  490. dev_info = inode->i_bdev->bd_disk->private_data;
  491. if (NULL == dev_info) {
  492. rc = -ENODEV;
  493. goto out;
  494. }
  495. atomic_inc(&dev_info->use_count);
  496. inode->i_bdev->bd_block_size = 4096;
  497. rc = 0;
  498. out:
  499. return rc;
  500. }
  501. static int
  502. dcssblk_release(struct inode *inode, struct file *filp)
  503. {
  504. struct dcssblk_dev_info *dev_info;
  505. int rc;
  506. dev_info = inode->i_bdev->bd_disk->private_data;
  507. if (NULL == dev_info) {
  508. rc = -ENODEV;
  509. goto out;
  510. }
  511. down_write(&dcssblk_devices_sem);
  512. if (atomic_dec_and_test(&dev_info->use_count)
  513. && (dev_info->save_pending)) {
  514. PRINT_INFO("Segment %s became idle and is being saved now\n",
  515. dev_info->segment_name);
  516. segment_save(dev_info->segment_name);
  517. dev_info->save_pending = 0;
  518. }
  519. up_write(&dcssblk_devices_sem);
  520. rc = 0;
  521. out:
  522. return rc;
  523. }
  524. static int
  525. dcssblk_make_request(struct request_queue *q, struct bio *bio)
  526. {
  527. struct dcssblk_dev_info *dev_info;
  528. struct bio_vec *bvec;
  529. unsigned long index;
  530. unsigned long page_addr;
  531. unsigned long source_addr;
  532. unsigned long bytes_done;
  533. int i;
  534. bytes_done = 0;
  535. dev_info = bio->bi_bdev->bd_disk->private_data;
  536. if (dev_info == NULL)
  537. goto fail;
  538. if ((bio->bi_sector & 7) != 0 || (bio->bi_size & 4095) != 0)
  539. /* Request is not page-aligned. */
  540. goto fail;
  541. if (((bio->bi_size >> 9) + bio->bi_sector)
  542. > get_capacity(bio->bi_bdev->bd_disk)) {
  543. /* Request beyond end of DCSS segment. */
  544. goto fail;
  545. }
  546. /* verify data transfer direction */
  547. if (dev_info->is_shared) {
  548. switch (dev_info->segment_type) {
  549. case SEG_TYPE_SR:
  550. case SEG_TYPE_ER:
  551. case SEG_TYPE_SC:
  552. /* cannot write to these segments */
  553. if (bio_data_dir(bio) == WRITE) {
  554. PRINT_WARN("rejecting write to ro segment %s\n", dev_info->dev.bus_id);
  555. goto fail;
  556. }
  557. }
  558. }
  559. index = (bio->bi_sector >> 3);
  560. bio_for_each_segment(bvec, bio, i) {
  561. page_addr = (unsigned long)
  562. page_address(bvec->bv_page) + bvec->bv_offset;
  563. source_addr = dev_info->start + (index<<12) + bytes_done;
  564. if (unlikely((page_addr & 4095) != 0) || (bvec->bv_len & 4095) != 0)
  565. // More paranoia.
  566. goto fail;
  567. if (bio_data_dir(bio) == READ) {
  568. memcpy((void*)page_addr, (void*)source_addr,
  569. bvec->bv_len);
  570. } else {
  571. memcpy((void*)source_addr, (void*)page_addr,
  572. bvec->bv_len);
  573. }
  574. bytes_done += bvec->bv_len;
  575. }
  576. bio_endio(bio, 0);
  577. return 0;
  578. fail:
  579. bio_io_error(bio);
  580. return 0;
  581. }
  582. static int
  583. dcssblk_direct_access (struct block_device *bdev, sector_t secnum,
  584. void **kaddr, unsigned long *pfn)
  585. {
  586. struct dcssblk_dev_info *dev_info;
  587. unsigned long pgoff;
  588. dev_info = bdev->bd_disk->private_data;
  589. if (!dev_info)
  590. return -ENODEV;
  591. if (secnum % (PAGE_SIZE/512))
  592. return -EINVAL;
  593. pgoff = secnum / (PAGE_SIZE / 512);
  594. if ((pgoff+1)*PAGE_SIZE-1 > dev_info->end - dev_info->start)
  595. return -ERANGE;
  596. *kaddr = (void *) (dev_info->start+pgoff*PAGE_SIZE);
  597. *pfn = virt_to_phys(*kaddr) >> PAGE_SHIFT;
  598. return 0;
  599. }
  600. static void
  601. dcssblk_check_params(void)
  602. {
  603. int rc, i, j, k;
  604. char buf[9];
  605. struct dcssblk_dev_info *dev_info;
  606. for (i = 0; (i < DCSSBLK_PARM_LEN) && (dcssblk_segments[i] != '\0');
  607. i++) {
  608. for (j = i; (dcssblk_segments[j] != ',') &&
  609. (dcssblk_segments[j] != '\0') &&
  610. (dcssblk_segments[j] != '(') &&
  611. (j - i) < 8; j++)
  612. {
  613. buf[j-i] = dcssblk_segments[j];
  614. }
  615. buf[j-i] = '\0';
  616. rc = dcssblk_add_store(dcssblk_root_dev, NULL, buf, j-i);
  617. if ((rc >= 0) && (dcssblk_segments[j] == '(')) {
  618. for (k = 0; buf[k] != '\0'; k++)
  619. buf[k] = toupper(buf[k]);
  620. if (!strncmp(&dcssblk_segments[j], "(local)", 7)) {
  621. down_read(&dcssblk_devices_sem);
  622. dev_info = dcssblk_get_device_by_name(buf);
  623. up_read(&dcssblk_devices_sem);
  624. if (dev_info)
  625. dcssblk_shared_store(&dev_info->dev,
  626. NULL, "0\n", 2);
  627. }
  628. }
  629. while ((dcssblk_segments[j] != ',') &&
  630. (dcssblk_segments[j] != '\0'))
  631. {
  632. j++;
  633. }
  634. if (dcssblk_segments[j] == '\0')
  635. break;
  636. i = j;
  637. }
  638. }
  639. /*
  640. * The init/exit functions.
  641. */
  642. static void __exit
  643. dcssblk_exit(void)
  644. {
  645. s390_root_dev_unregister(dcssblk_root_dev);
  646. unregister_blkdev(dcssblk_major, DCSSBLK_NAME);
  647. }
  648. static int __init
  649. dcssblk_init(void)
  650. {
  651. int rc;
  652. dcssblk_root_dev = s390_root_dev_register("dcssblk");
  653. if (IS_ERR(dcssblk_root_dev))
  654. return PTR_ERR(dcssblk_root_dev);
  655. rc = device_create_file(dcssblk_root_dev, &dev_attr_add);
  656. if (rc) {
  657. s390_root_dev_unregister(dcssblk_root_dev);
  658. return rc;
  659. }
  660. rc = device_create_file(dcssblk_root_dev, &dev_attr_remove);
  661. if (rc) {
  662. s390_root_dev_unregister(dcssblk_root_dev);
  663. return rc;
  664. }
  665. rc = register_blkdev(0, DCSSBLK_NAME);
  666. if (rc < 0) {
  667. s390_root_dev_unregister(dcssblk_root_dev);
  668. return rc;
  669. }
  670. dcssblk_major = rc;
  671. init_rwsem(&dcssblk_devices_sem);
  672. dcssblk_check_params();
  673. return 0;
  674. }
  675. module_init(dcssblk_init);
  676. module_exit(dcssblk_exit);
  677. module_param_string(segments, dcssblk_segments, DCSSBLK_PARM_LEN, 0444);
  678. MODULE_PARM_DESC(segments, "Name of DCSS segment(s) to be loaded, "
  679. "comma-separated list, each name max. 8 chars.\n"
  680. "Adding \"(local)\" to segment name equals echoing 0 to "
  681. "/sys/devices/dcssblk/<segment name>/shared after loading "
  682. "the segment - \n"
  683. "e.g. segments=\"mydcss1,mydcss2,mydcss3(local)\"");
  684. MODULE_LICENSE("GPL");