dcssblk.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  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. rc = dcssblk_assign_free_minor(dev_info);
  353. if (rc) {
  354. up_write(&dcssblk_devices_sem);
  355. PRINT_ERR("No free minor number available! "
  356. "Unloading segment...\n");
  357. goto unload_seg;
  358. }
  359. sprintf(dev_info->gd->disk_name, "dcssblk%d",
  360. dev_info->gd->first_minor);
  361. list_add_tail(&dev_info->lh, &dcssblk_devices);
  362. if (!try_module_get(THIS_MODULE)) {
  363. rc = -ENODEV;
  364. goto list_del;
  365. }
  366. /*
  367. * register the device
  368. */
  369. rc = device_register(&dev_info->dev);
  370. if (rc) {
  371. PRINT_ERR("Segment %s could not be registered RC=%d\n",
  372. local_buf, rc);
  373. module_put(THIS_MODULE);
  374. goto list_del;
  375. }
  376. get_device(&dev_info->dev);
  377. rc = device_create_file(&dev_info->dev, &dev_attr_shared);
  378. if (rc)
  379. goto unregister_dev;
  380. rc = device_create_file(&dev_info->dev, &dev_attr_save);
  381. if (rc)
  382. goto unregister_dev;
  383. add_disk(dev_info->gd);
  384. switch (dev_info->segment_type) {
  385. case SEG_TYPE_SR:
  386. case SEG_TYPE_ER:
  387. case SEG_TYPE_SC:
  388. set_disk_ro(dev_info->gd,1);
  389. break;
  390. default:
  391. set_disk_ro(dev_info->gd,0);
  392. break;
  393. }
  394. PRINT_DEBUG("Segment %s loaded successfully\n", local_buf);
  395. up_write(&dcssblk_devices_sem);
  396. rc = count;
  397. goto out;
  398. unregister_dev:
  399. list_del(&dev_info->lh);
  400. blk_cleanup_queue(dev_info->dcssblk_queue);
  401. dev_info->gd->queue = NULL;
  402. put_disk(dev_info->gd);
  403. device_unregister(&dev_info->dev);
  404. segment_unload(dev_info->segment_name);
  405. put_device(&dev_info->dev);
  406. up_write(&dcssblk_devices_sem);
  407. goto out;
  408. list_del:
  409. list_del(&dev_info->lh);
  410. up_write(&dcssblk_devices_sem);
  411. unload_seg:
  412. segment_unload(local_buf);
  413. dealloc_gendisk:
  414. blk_cleanup_queue(dev_info->dcssblk_queue);
  415. dev_info->gd->queue = NULL;
  416. put_disk(dev_info->gd);
  417. free_dev_info:
  418. kfree(dev_info);
  419. out:
  420. kfree(local_buf);
  421. out_nobuf:
  422. return rc;
  423. }
  424. /*
  425. * device attribute for removing devices
  426. */
  427. static ssize_t
  428. dcssblk_remove_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  429. {
  430. struct dcssblk_dev_info *dev_info;
  431. int rc, i;
  432. char *local_buf;
  433. if (dev != dcssblk_root_dev) {
  434. return -EINVAL;
  435. }
  436. local_buf = kmalloc(count + 1, GFP_KERNEL);
  437. if (local_buf == NULL) {
  438. return -ENOMEM;
  439. }
  440. /*
  441. * parse input
  442. */
  443. for (i = 0; ((*(buf+i)!='\0') && (*(buf+i)!='\n') && i < count); i++) {
  444. local_buf[i] = toupper(buf[i]);
  445. }
  446. local_buf[i] = '\0';
  447. if ((i == 0) || (i > 8)) {
  448. rc = -ENAMETOOLONG;
  449. goto out_buf;
  450. }
  451. down_write(&dcssblk_devices_sem);
  452. dev_info = dcssblk_get_device_by_name(local_buf);
  453. if (dev_info == NULL) {
  454. up_write(&dcssblk_devices_sem);
  455. PRINT_WARN("Segment %s is not loaded!\n", local_buf);
  456. rc = -ENODEV;
  457. goto out_buf;
  458. }
  459. if (atomic_read(&dev_info->use_count) != 0) {
  460. up_write(&dcssblk_devices_sem);
  461. PRINT_WARN("Segment %s is in use!\n", local_buf);
  462. rc = -EBUSY;
  463. goto out_buf;
  464. }
  465. list_del(&dev_info->lh);
  466. del_gendisk(dev_info->gd);
  467. blk_cleanup_queue(dev_info->dcssblk_queue);
  468. dev_info->gd->queue = NULL;
  469. put_disk(dev_info->gd);
  470. device_unregister(&dev_info->dev);
  471. segment_unload(dev_info->segment_name);
  472. PRINT_DEBUG("Segment %s unloaded successfully\n",
  473. dev_info->segment_name);
  474. put_device(&dev_info->dev);
  475. up_write(&dcssblk_devices_sem);
  476. rc = count;
  477. out_buf:
  478. kfree(local_buf);
  479. return rc;
  480. }
  481. static int
  482. dcssblk_open(struct inode *inode, struct file *filp)
  483. {
  484. struct dcssblk_dev_info *dev_info;
  485. int rc;
  486. dev_info = inode->i_bdev->bd_disk->private_data;
  487. if (NULL == dev_info) {
  488. rc = -ENODEV;
  489. goto out;
  490. }
  491. atomic_inc(&dev_info->use_count);
  492. inode->i_bdev->bd_block_size = 4096;
  493. rc = 0;
  494. out:
  495. return rc;
  496. }
  497. static int
  498. dcssblk_release(struct inode *inode, struct file *filp)
  499. {
  500. struct dcssblk_dev_info *dev_info;
  501. int rc;
  502. dev_info = inode->i_bdev->bd_disk->private_data;
  503. if (NULL == dev_info) {
  504. rc = -ENODEV;
  505. goto out;
  506. }
  507. down_write(&dcssblk_devices_sem);
  508. if (atomic_dec_and_test(&dev_info->use_count)
  509. && (dev_info->save_pending)) {
  510. PRINT_INFO("Segment %s became idle and is being saved now\n",
  511. dev_info->segment_name);
  512. segment_save(dev_info->segment_name);
  513. dev_info->save_pending = 0;
  514. }
  515. up_write(&dcssblk_devices_sem);
  516. rc = 0;
  517. out:
  518. return rc;
  519. }
  520. static int
  521. dcssblk_make_request(struct request_queue *q, struct bio *bio)
  522. {
  523. struct dcssblk_dev_info *dev_info;
  524. struct bio_vec *bvec;
  525. unsigned long index;
  526. unsigned long page_addr;
  527. unsigned long source_addr;
  528. unsigned long bytes_done;
  529. int i;
  530. bytes_done = 0;
  531. dev_info = bio->bi_bdev->bd_disk->private_data;
  532. if (dev_info == NULL)
  533. goto fail;
  534. if ((bio->bi_sector & 7) != 0 || (bio->bi_size & 4095) != 0)
  535. /* Request is not page-aligned. */
  536. goto fail;
  537. if (((bio->bi_size >> 9) + bio->bi_sector)
  538. > get_capacity(bio->bi_bdev->bd_disk)) {
  539. /* Request beyond end of DCSS segment. */
  540. goto fail;
  541. }
  542. /* verify data transfer direction */
  543. if (dev_info->is_shared) {
  544. switch (dev_info->segment_type) {
  545. case SEG_TYPE_SR:
  546. case SEG_TYPE_ER:
  547. case SEG_TYPE_SC:
  548. /* cannot write to these segments */
  549. if (bio_data_dir(bio) == WRITE) {
  550. PRINT_WARN("rejecting write to ro segment %s\n", dev_info->dev.bus_id);
  551. goto fail;
  552. }
  553. }
  554. }
  555. index = (bio->bi_sector >> 3);
  556. bio_for_each_segment(bvec, bio, i) {
  557. page_addr = (unsigned long)
  558. page_address(bvec->bv_page) + bvec->bv_offset;
  559. source_addr = dev_info->start + (index<<12) + bytes_done;
  560. if (unlikely((page_addr & 4095) != 0) || (bvec->bv_len & 4095) != 0)
  561. // More paranoia.
  562. goto fail;
  563. if (bio_data_dir(bio) == READ) {
  564. memcpy((void*)page_addr, (void*)source_addr,
  565. bvec->bv_len);
  566. } else {
  567. memcpy((void*)source_addr, (void*)page_addr,
  568. bvec->bv_len);
  569. }
  570. bytes_done += bvec->bv_len;
  571. }
  572. bio_endio(bio, 0);
  573. return 0;
  574. fail:
  575. bio_io_error(bio);
  576. return 0;
  577. }
  578. static int
  579. dcssblk_direct_access (struct block_device *bdev, sector_t secnum,
  580. void **kaddr, unsigned long *pfn)
  581. {
  582. struct dcssblk_dev_info *dev_info;
  583. unsigned long pgoff;
  584. dev_info = bdev->bd_disk->private_data;
  585. if (!dev_info)
  586. return -ENODEV;
  587. if (secnum % (PAGE_SIZE/512))
  588. return -EINVAL;
  589. pgoff = secnum / (PAGE_SIZE / 512);
  590. if ((pgoff+1)*PAGE_SIZE-1 > dev_info->end - dev_info->start)
  591. return -ERANGE;
  592. *kaddr = (void *) (dev_info->start+pgoff*PAGE_SIZE);
  593. *pfn = virt_to_phys(*kaddr) >> PAGE_SHIFT;
  594. return 0;
  595. }
  596. static void
  597. dcssblk_check_params(void)
  598. {
  599. int rc, i, j, k;
  600. char buf[9];
  601. struct dcssblk_dev_info *dev_info;
  602. for (i = 0; (i < DCSSBLK_PARM_LEN) && (dcssblk_segments[i] != '\0');
  603. i++) {
  604. for (j = i; (dcssblk_segments[j] != ',') &&
  605. (dcssblk_segments[j] != '\0') &&
  606. (dcssblk_segments[j] != '(') &&
  607. (j - i) < 8; j++)
  608. {
  609. buf[j-i] = dcssblk_segments[j];
  610. }
  611. buf[j-i] = '\0';
  612. rc = dcssblk_add_store(dcssblk_root_dev, NULL, buf, j-i);
  613. if ((rc >= 0) && (dcssblk_segments[j] == '(')) {
  614. for (k = 0; buf[k] != '\0'; k++)
  615. buf[k] = toupper(buf[k]);
  616. if (!strncmp(&dcssblk_segments[j], "(local)", 7)) {
  617. down_read(&dcssblk_devices_sem);
  618. dev_info = dcssblk_get_device_by_name(buf);
  619. up_read(&dcssblk_devices_sem);
  620. if (dev_info)
  621. dcssblk_shared_store(&dev_info->dev,
  622. NULL, "0\n", 2);
  623. }
  624. }
  625. while ((dcssblk_segments[j] != ',') &&
  626. (dcssblk_segments[j] != '\0'))
  627. {
  628. j++;
  629. }
  630. if (dcssblk_segments[j] == '\0')
  631. break;
  632. i = j;
  633. }
  634. }
  635. /*
  636. * The init/exit functions.
  637. */
  638. static void __exit
  639. dcssblk_exit(void)
  640. {
  641. s390_root_dev_unregister(dcssblk_root_dev);
  642. unregister_blkdev(dcssblk_major, DCSSBLK_NAME);
  643. }
  644. static int __init
  645. dcssblk_init(void)
  646. {
  647. int rc;
  648. dcssblk_root_dev = s390_root_dev_register("dcssblk");
  649. if (IS_ERR(dcssblk_root_dev))
  650. return PTR_ERR(dcssblk_root_dev);
  651. rc = device_create_file(dcssblk_root_dev, &dev_attr_add);
  652. if (rc) {
  653. s390_root_dev_unregister(dcssblk_root_dev);
  654. return rc;
  655. }
  656. rc = device_create_file(dcssblk_root_dev, &dev_attr_remove);
  657. if (rc) {
  658. s390_root_dev_unregister(dcssblk_root_dev);
  659. return rc;
  660. }
  661. rc = register_blkdev(0, DCSSBLK_NAME);
  662. if (rc < 0) {
  663. s390_root_dev_unregister(dcssblk_root_dev);
  664. return rc;
  665. }
  666. dcssblk_major = rc;
  667. init_rwsem(&dcssblk_devices_sem);
  668. dcssblk_check_params();
  669. return 0;
  670. }
  671. module_init(dcssblk_init);
  672. module_exit(dcssblk_exit);
  673. module_param_string(segments, dcssblk_segments, DCSSBLK_PARM_LEN, 0444);
  674. MODULE_PARM_DESC(segments, "Name of DCSS segment(s) to be loaded, "
  675. "comma-separated list, each name max. 8 chars.\n"
  676. "Adding \"(local)\" to segment name equals echoing 0 to "
  677. "/sys/devices/dcssblk/<segment name>/shared after loading "
  678. "the segment - \n"
  679. "e.g. segments=\"mydcss1,mydcss2,mydcss3(local)\"");
  680. MODULE_LICENSE("GPL");