dcssblk.c 19 KB

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