dcssblk.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  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. #define DCSS_BUS_ID_SIZE 20
  23. #ifdef DCSSBLK_DEBUG
  24. #define PRINT_DEBUG(x...) printk(KERN_DEBUG DCSSBLK_NAME " debug: " x)
  25. #else
  26. #define PRINT_DEBUG(x...) do {} while (0)
  27. #endif
  28. #define PRINT_INFO(x...) printk(KERN_INFO DCSSBLK_NAME " info: " x)
  29. #define PRINT_WARN(x...) printk(KERN_WARNING DCSSBLK_NAME " warning: " x)
  30. #define PRINT_ERR(x...) printk(KERN_ERR DCSSBLK_NAME " error: " x)
  31. static int dcssblk_open(struct block_device *bdev, fmode_t mode);
  32. static int dcssblk_release(struct gendisk *disk, fmode_t mode);
  33. static int dcssblk_make_request(struct request_queue *q, struct bio *bio);
  34. static int dcssblk_direct_access(struct block_device *bdev, sector_t secnum,
  35. void **kaddr, unsigned long *pfn);
  36. static char dcssblk_segments[DCSSBLK_PARM_LEN] = "\0";
  37. static int dcssblk_major;
  38. static struct block_device_operations dcssblk_devops = {
  39. .owner = THIS_MODULE,
  40. .open = dcssblk_open,
  41. .release = dcssblk_release,
  42. .direct_access = dcssblk_direct_access,
  43. };
  44. struct dcssblk_dev_info {
  45. struct list_head lh;
  46. struct device dev;
  47. char segment_name[DCSS_BUS_ID_SIZE];
  48. atomic_t use_count;
  49. struct gendisk *gd;
  50. unsigned long start;
  51. unsigned long end;
  52. int segment_type;
  53. unsigned char save_pending;
  54. unsigned char is_shared;
  55. struct request_queue *dcssblk_queue;
  56. int num_of_segments;
  57. struct list_head seg_list;
  58. };
  59. struct segment_info {
  60. struct list_head lh;
  61. char segment_name[DCSS_BUS_ID_SIZE];
  62. unsigned long start;
  63. unsigned long end;
  64. int segment_type;
  65. };
  66. static ssize_t dcssblk_add_store(struct device * dev, struct device_attribute *attr, const char * buf,
  67. size_t count);
  68. static ssize_t dcssblk_remove_store(struct device * dev, struct device_attribute *attr, const char * buf,
  69. size_t count);
  70. static ssize_t dcssblk_save_store(struct device * dev, struct device_attribute *attr, const char * buf,
  71. size_t count);
  72. static ssize_t dcssblk_save_show(struct device *dev, struct device_attribute *attr, char *buf);
  73. static ssize_t dcssblk_shared_store(struct device * dev, struct device_attribute *attr, const char * buf,
  74. size_t count);
  75. static ssize_t dcssblk_shared_show(struct device *dev, struct device_attribute *attr, char *buf);
  76. static ssize_t dcssblk_seglist_show(struct device *dev,
  77. struct device_attribute *attr,
  78. char *buf);
  79. static DEVICE_ATTR(add, S_IWUSR, NULL, dcssblk_add_store);
  80. static DEVICE_ATTR(remove, S_IWUSR, NULL, dcssblk_remove_store);
  81. static DEVICE_ATTR(save, S_IWUSR | S_IRUSR, dcssblk_save_show,
  82. dcssblk_save_store);
  83. static DEVICE_ATTR(shared, S_IWUSR | S_IRUSR, dcssblk_shared_show,
  84. dcssblk_shared_store);
  85. static DEVICE_ATTR(seglist, S_IRUSR, dcssblk_seglist_show, NULL);
  86. static struct device *dcssblk_root_dev;
  87. static LIST_HEAD(dcssblk_devices);
  88. static struct rw_semaphore dcssblk_devices_sem;
  89. /*
  90. * release function for segment device.
  91. */
  92. static void
  93. dcssblk_release_segment(struct device *dev)
  94. {
  95. struct dcssblk_dev_info *dev_info;
  96. struct segment_info *entry, *temp;
  97. dev_info = container_of(dev, struct dcssblk_dev_info, dev);
  98. list_for_each_entry_safe(entry, temp, &dev_info->seg_list, lh) {
  99. list_del(&entry->lh);
  100. kfree(entry);
  101. }
  102. kfree(dev_info);
  103. module_put(THIS_MODULE);
  104. }
  105. /*
  106. * get a minor number. needs to be called with
  107. * down_write(&dcssblk_devices_sem) and the
  108. * device needs to be enqueued before the semaphore is
  109. * freed.
  110. */
  111. static int
  112. dcssblk_assign_free_minor(struct dcssblk_dev_info *dev_info)
  113. {
  114. int minor, found;
  115. struct dcssblk_dev_info *entry;
  116. if (dev_info == NULL)
  117. return -EINVAL;
  118. for (minor = 0; minor < (1<<MINORBITS); minor++) {
  119. found = 0;
  120. // test if minor available
  121. list_for_each_entry(entry, &dcssblk_devices, lh)
  122. if (minor == MINOR(disk_devt(entry->gd)))
  123. found++;
  124. if (!found) break; // got unused minor
  125. }
  126. if (found)
  127. return -EBUSY;
  128. dev_info->gd->first_minor = minor;
  129. return 0;
  130. }
  131. /*
  132. * get the struct dcssblk_dev_info from dcssblk_devices
  133. * for the given name.
  134. * down_read(&dcssblk_devices_sem) must be held.
  135. */
  136. static struct dcssblk_dev_info *
  137. dcssblk_get_device_by_name(char *name)
  138. {
  139. struct dcssblk_dev_info *entry;
  140. list_for_each_entry(entry, &dcssblk_devices, lh) {
  141. if (!strcmp(name, entry->segment_name)) {
  142. return entry;
  143. }
  144. }
  145. return NULL;
  146. }
  147. /*
  148. * get the struct segment_info from seg_list
  149. * for the given name.
  150. * down_read(&dcssblk_devices_sem) must be held.
  151. */
  152. static struct segment_info *
  153. dcssblk_get_segment_by_name(char *name)
  154. {
  155. struct dcssblk_dev_info *dev_info;
  156. struct segment_info *entry;
  157. list_for_each_entry(dev_info, &dcssblk_devices, lh) {
  158. list_for_each_entry(entry, &dev_info->seg_list, lh) {
  159. if (!strcmp(name, entry->segment_name))
  160. return entry;
  161. }
  162. }
  163. return NULL;
  164. }
  165. /*
  166. * get the highest address of the multi-segment block.
  167. */
  168. static unsigned long
  169. dcssblk_find_highest_addr(struct dcssblk_dev_info *dev_info)
  170. {
  171. unsigned long highest_addr;
  172. struct segment_info *entry;
  173. highest_addr = 0;
  174. list_for_each_entry(entry, &dev_info->seg_list, lh) {
  175. if (highest_addr < entry->end)
  176. highest_addr = entry->end;
  177. }
  178. return highest_addr;
  179. }
  180. /*
  181. * get the lowest address of the multi-segment block.
  182. */
  183. static unsigned long
  184. dcssblk_find_lowest_addr(struct dcssblk_dev_info *dev_info)
  185. {
  186. int set_first;
  187. unsigned long lowest_addr;
  188. struct segment_info *entry;
  189. set_first = 0;
  190. lowest_addr = 0;
  191. list_for_each_entry(entry, &dev_info->seg_list, lh) {
  192. if (set_first == 0) {
  193. lowest_addr = entry->start;
  194. set_first = 1;
  195. } else {
  196. if (lowest_addr > entry->start)
  197. lowest_addr = entry->start;
  198. }
  199. }
  200. return lowest_addr;
  201. }
  202. /*
  203. * Check continuity of segments.
  204. */
  205. static int
  206. dcssblk_is_continuous(struct dcssblk_dev_info *dev_info)
  207. {
  208. int i, j, rc;
  209. struct segment_info *sort_list, *entry, temp;
  210. if (dev_info->num_of_segments <= 1)
  211. return 0;
  212. sort_list = kzalloc(
  213. sizeof(struct segment_info) * dev_info->num_of_segments,
  214. GFP_KERNEL);
  215. if (sort_list == NULL)
  216. return -ENOMEM;
  217. i = 0;
  218. list_for_each_entry(entry, &dev_info->seg_list, lh) {
  219. memcpy(&sort_list[i], entry, sizeof(struct segment_info));
  220. i++;
  221. }
  222. /* sort segments */
  223. for (i = 0; i < dev_info->num_of_segments; i++)
  224. for (j = 0; j < dev_info->num_of_segments; j++)
  225. if (sort_list[j].start > sort_list[i].start) {
  226. memcpy(&temp, &sort_list[i],
  227. sizeof(struct segment_info));
  228. memcpy(&sort_list[i], &sort_list[j],
  229. sizeof(struct segment_info));
  230. memcpy(&sort_list[j], &temp,
  231. sizeof(struct segment_info));
  232. }
  233. /* check continuity */
  234. for (i = 0; i < dev_info->num_of_segments - 1; i++) {
  235. if ((sort_list[i].end + 1) != sort_list[i+1].start) {
  236. PRINT_ERR("Segment %s is not contiguous with "
  237. "segment %s\n",
  238. sort_list[i].segment_name,
  239. sort_list[i+1].segment_name);
  240. rc = -EINVAL;
  241. goto out;
  242. }
  243. /* EN and EW are allowed in a block device */
  244. if (sort_list[i].segment_type != sort_list[i+1].segment_type) {
  245. if (!(sort_list[i].segment_type & SEGMENT_EXCLUSIVE) ||
  246. (sort_list[i].segment_type == SEG_TYPE_ER) ||
  247. !(sort_list[i+1].segment_type &
  248. SEGMENT_EXCLUSIVE) ||
  249. (sort_list[i+1].segment_type == SEG_TYPE_ER)) {
  250. PRINT_ERR("Segment %s has different type from "
  251. "segment %s\n",
  252. sort_list[i].segment_name,
  253. sort_list[i+1].segment_name);
  254. rc = -EINVAL;
  255. goto out;
  256. }
  257. }
  258. }
  259. rc = 0;
  260. out:
  261. kfree(sort_list);
  262. return rc;
  263. }
  264. /*
  265. * Load a segment
  266. */
  267. static int
  268. dcssblk_load_segment(char *name, struct segment_info **seg_info)
  269. {
  270. int rc;
  271. /* already loaded? */
  272. down_read(&dcssblk_devices_sem);
  273. *seg_info = dcssblk_get_segment_by_name(name);
  274. up_read(&dcssblk_devices_sem);
  275. if (*seg_info != NULL)
  276. return -EEXIST;
  277. /* get a struct segment_info */
  278. *seg_info = kzalloc(sizeof(struct segment_info), GFP_KERNEL);
  279. if (*seg_info == NULL)
  280. return -ENOMEM;
  281. strcpy((*seg_info)->segment_name, name);
  282. /* load the segment */
  283. rc = segment_load(name, SEGMENT_SHARED,
  284. &(*seg_info)->start, &(*seg_info)->end);
  285. if (rc < 0) {
  286. segment_warning(rc, (*seg_info)->segment_name);
  287. kfree(*seg_info);
  288. } else {
  289. INIT_LIST_HEAD(&(*seg_info)->lh);
  290. (*seg_info)->segment_type = rc;
  291. }
  292. return rc;
  293. }
  294. static void dcssblk_unregister_callback(struct device *dev)
  295. {
  296. device_unregister(dev);
  297. put_device(dev);
  298. }
  299. /*
  300. * device attribute for switching shared/nonshared (exclusive)
  301. * operation (show + store)
  302. */
  303. static ssize_t
  304. dcssblk_shared_show(struct device *dev, struct device_attribute *attr, char *buf)
  305. {
  306. struct dcssblk_dev_info *dev_info;
  307. dev_info = container_of(dev, struct dcssblk_dev_info, dev);
  308. return sprintf(buf, dev_info->is_shared ? "1\n" : "0\n");
  309. }
  310. static ssize_t
  311. dcssblk_shared_store(struct device *dev, struct device_attribute *attr, const char *inbuf, size_t count)
  312. {
  313. struct dcssblk_dev_info *dev_info;
  314. struct segment_info *entry, *temp;
  315. int rc;
  316. if ((count > 1) && (inbuf[1] != '\n') && (inbuf[1] != '\0'))
  317. return -EINVAL;
  318. down_write(&dcssblk_devices_sem);
  319. dev_info = container_of(dev, struct dcssblk_dev_info, dev);
  320. if (atomic_read(&dev_info->use_count)) {
  321. rc = -EBUSY;
  322. goto out;
  323. }
  324. if (inbuf[0] == '1') {
  325. /* reload segments in shared mode */
  326. list_for_each_entry(entry, &dev_info->seg_list, lh) {
  327. rc = segment_modify_shared(entry->segment_name,
  328. SEGMENT_SHARED);
  329. if (rc < 0) {
  330. BUG_ON(rc == -EINVAL);
  331. if (rc != -EAGAIN)
  332. goto removeseg;
  333. }
  334. }
  335. dev_info->is_shared = 1;
  336. switch (dev_info->segment_type) {
  337. case SEG_TYPE_SR:
  338. case SEG_TYPE_ER:
  339. case SEG_TYPE_SC:
  340. set_disk_ro(dev_info->gd, 1);
  341. }
  342. } else if (inbuf[0] == '0') {
  343. /* reload segments in exclusive mode */
  344. if (dev_info->segment_type == SEG_TYPE_SC) {
  345. PRINT_ERR("Segment type SC (%s) cannot be loaded in "
  346. "non-shared mode\n", dev_info->segment_name);
  347. rc = -EINVAL;
  348. goto out;
  349. }
  350. list_for_each_entry(entry, &dev_info->seg_list, lh) {
  351. rc = segment_modify_shared(entry->segment_name,
  352. SEGMENT_EXCLUSIVE);
  353. if (rc < 0) {
  354. BUG_ON(rc == -EINVAL);
  355. if (rc != -EAGAIN)
  356. goto removeseg;
  357. }
  358. }
  359. dev_info->is_shared = 0;
  360. set_disk_ro(dev_info->gd, 0);
  361. } else {
  362. rc = -EINVAL;
  363. goto out;
  364. }
  365. rc = count;
  366. goto out;
  367. removeseg:
  368. PRINT_ERR("Could not reload segment(s) of the device %s, removing "
  369. "segment(s) now!\n",
  370. dev_info->segment_name);
  371. temp = entry;
  372. list_for_each_entry(entry, &dev_info->seg_list, lh) {
  373. if (entry != temp)
  374. segment_unload(entry->segment_name);
  375. }
  376. list_del(&dev_info->lh);
  377. del_gendisk(dev_info->gd);
  378. blk_cleanup_queue(dev_info->dcssblk_queue);
  379. dev_info->gd->queue = NULL;
  380. put_disk(dev_info->gd);
  381. rc = device_schedule_callback(dev, dcssblk_unregister_callback);
  382. out:
  383. up_write(&dcssblk_devices_sem);
  384. return rc;
  385. }
  386. /*
  387. * device attribute for save operation on current copy
  388. * of the segment. If the segment is busy, saving will
  389. * become pending until it gets released, which can be
  390. * undone by storing a non-true value to this entry.
  391. * (show + store)
  392. */
  393. static ssize_t
  394. dcssblk_save_show(struct device *dev, struct device_attribute *attr, char *buf)
  395. {
  396. struct dcssblk_dev_info *dev_info;
  397. dev_info = container_of(dev, struct dcssblk_dev_info, dev);
  398. return sprintf(buf, dev_info->save_pending ? "1\n" : "0\n");
  399. }
  400. static ssize_t
  401. dcssblk_save_store(struct device *dev, struct device_attribute *attr, const char *inbuf, size_t count)
  402. {
  403. struct dcssblk_dev_info *dev_info;
  404. struct segment_info *entry;
  405. if ((count > 1) && (inbuf[1] != '\n') && (inbuf[1] != '\0'))
  406. return -EINVAL;
  407. dev_info = container_of(dev, struct dcssblk_dev_info, dev);
  408. down_write(&dcssblk_devices_sem);
  409. if (inbuf[0] == '1') {
  410. if (atomic_read(&dev_info->use_count) == 0) {
  411. // device is idle => we save immediately
  412. PRINT_INFO("Saving segment(s) of the device %s\n",
  413. dev_info->segment_name);
  414. list_for_each_entry(entry, &dev_info->seg_list, lh) {
  415. segment_save(entry->segment_name);
  416. }
  417. } else {
  418. // device is busy => we save it when it becomes
  419. // idle in dcssblk_release
  420. PRINT_INFO("Device %s is currently busy, segment(s) "
  421. "will be saved when it becomes idle...\n",
  422. dev_info->segment_name);
  423. dev_info->save_pending = 1;
  424. }
  425. } else if (inbuf[0] == '0') {
  426. if (dev_info->save_pending) {
  427. // device is busy & the user wants to undo his save
  428. // request
  429. dev_info->save_pending = 0;
  430. PRINT_INFO("Pending save for segment(s) of the device "
  431. "%s deactivated\n",
  432. dev_info->segment_name);
  433. }
  434. } else {
  435. up_write(&dcssblk_devices_sem);
  436. return -EINVAL;
  437. }
  438. up_write(&dcssblk_devices_sem);
  439. return count;
  440. }
  441. /*
  442. * device attribute for showing all segments in a device
  443. */
  444. static ssize_t
  445. dcssblk_seglist_show(struct device *dev, struct device_attribute *attr,
  446. char *buf)
  447. {
  448. int i;
  449. struct dcssblk_dev_info *dev_info;
  450. struct segment_info *entry;
  451. down_read(&dcssblk_devices_sem);
  452. dev_info = container_of(dev, struct dcssblk_dev_info, dev);
  453. i = 0;
  454. buf[0] = '\0';
  455. list_for_each_entry(entry, &dev_info->seg_list, lh) {
  456. strcpy(&buf[i], entry->segment_name);
  457. i += strlen(entry->segment_name);
  458. buf[i] = '\n';
  459. i++;
  460. }
  461. up_read(&dcssblk_devices_sem);
  462. return i;
  463. }
  464. /*
  465. * device attribute for adding devices
  466. */
  467. static ssize_t
  468. dcssblk_add_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  469. {
  470. int rc, i, j, num_of_segments;
  471. struct dcssblk_dev_info *dev_info;
  472. struct segment_info *seg_info, *temp;
  473. char *local_buf;
  474. unsigned long seg_byte_size;
  475. dev_info = NULL;
  476. seg_info = NULL;
  477. if (dev != dcssblk_root_dev) {
  478. rc = -EINVAL;
  479. goto out_nobuf;
  480. }
  481. if ((count < 1) || (buf[0] == '\0') || (buf[0] == '\n')) {
  482. rc = -ENAMETOOLONG;
  483. goto out_nobuf;
  484. }
  485. local_buf = kmalloc(count + 1, GFP_KERNEL);
  486. if (local_buf == NULL) {
  487. rc = -ENOMEM;
  488. goto out_nobuf;
  489. }
  490. /*
  491. * parse input
  492. */
  493. num_of_segments = 0;
  494. for (i = 0; ((buf[i] != '\0') && (buf[i] != '\n') && i < count); i++) {
  495. for (j = i; (buf[j] != ':') &&
  496. (buf[j] != '\0') &&
  497. (buf[j] != '\n') &&
  498. j < count; j++) {
  499. local_buf[j-i] = toupper(buf[j]);
  500. }
  501. local_buf[j-i] = '\0';
  502. if (((j - i) == 0) || ((j - i) > 8)) {
  503. rc = -ENAMETOOLONG;
  504. goto seg_list_del;
  505. }
  506. rc = dcssblk_load_segment(local_buf, &seg_info);
  507. if (rc < 0)
  508. goto seg_list_del;
  509. /*
  510. * get a struct dcssblk_dev_info
  511. */
  512. if (num_of_segments == 0) {
  513. dev_info = kzalloc(sizeof(struct dcssblk_dev_info),
  514. GFP_KERNEL);
  515. if (dev_info == NULL) {
  516. rc = -ENOMEM;
  517. goto out;
  518. }
  519. strcpy(dev_info->segment_name, local_buf);
  520. dev_info->segment_type = seg_info->segment_type;
  521. INIT_LIST_HEAD(&dev_info->seg_list);
  522. }
  523. list_add_tail(&seg_info->lh, &dev_info->seg_list);
  524. num_of_segments++;
  525. i = j;
  526. if ((buf[j] == '\0') || (buf[j] == '\n'))
  527. break;
  528. }
  529. /* no trailing colon at the end of the input */
  530. if ((i > 0) && (buf[i-1] == ':')) {
  531. rc = -ENAMETOOLONG;
  532. goto seg_list_del;
  533. }
  534. strlcpy(local_buf, buf, i + 1);
  535. dev_info->num_of_segments = num_of_segments;
  536. rc = dcssblk_is_continuous(dev_info);
  537. if (rc < 0)
  538. goto seg_list_del;
  539. dev_info->start = dcssblk_find_lowest_addr(dev_info);
  540. dev_info->end = dcssblk_find_highest_addr(dev_info);
  541. dev_set_name(&dev_info->dev, dev_info->segment_name);
  542. dev_info->dev.release = dcssblk_release_segment;
  543. INIT_LIST_HEAD(&dev_info->lh);
  544. dev_info->gd = alloc_disk(DCSSBLK_MINORS_PER_DISK);
  545. if (dev_info->gd == NULL) {
  546. rc = -ENOMEM;
  547. goto seg_list_del;
  548. }
  549. dev_info->gd->major = dcssblk_major;
  550. dev_info->gd->fops = &dcssblk_devops;
  551. dev_info->dcssblk_queue = blk_alloc_queue(GFP_KERNEL);
  552. dev_info->gd->queue = dev_info->dcssblk_queue;
  553. dev_info->gd->private_data = dev_info;
  554. dev_info->gd->driverfs_dev = &dev_info->dev;
  555. blk_queue_make_request(dev_info->dcssblk_queue, dcssblk_make_request);
  556. blk_queue_hardsect_size(dev_info->dcssblk_queue, 4096);
  557. seg_byte_size = (dev_info->end - dev_info->start + 1);
  558. set_capacity(dev_info->gd, seg_byte_size >> 9); // size in sectors
  559. PRINT_INFO("Loaded segment(s) %s, size = %lu Byte, "
  560. "capacity = %lu (512 Byte) sectors\n", local_buf,
  561. seg_byte_size, seg_byte_size >> 9);
  562. dev_info->save_pending = 0;
  563. dev_info->is_shared = 1;
  564. dev_info->dev.parent = dcssblk_root_dev;
  565. /*
  566. *get minor, add to list
  567. */
  568. down_write(&dcssblk_devices_sem);
  569. if (dcssblk_get_segment_by_name(local_buf)) {
  570. rc = -EEXIST;
  571. goto release_gd;
  572. }
  573. rc = dcssblk_assign_free_minor(dev_info);
  574. if (rc)
  575. goto release_gd;
  576. sprintf(dev_info->gd->disk_name, "dcssblk%d",
  577. MINOR(disk_devt(dev_info->gd)));
  578. list_add_tail(&dev_info->lh, &dcssblk_devices);
  579. if (!try_module_get(THIS_MODULE)) {
  580. rc = -ENODEV;
  581. goto dev_list_del;
  582. }
  583. /*
  584. * register the device
  585. */
  586. rc = device_register(&dev_info->dev);
  587. if (rc) {
  588. module_put(THIS_MODULE);
  589. goto dev_list_del;
  590. }
  591. get_device(&dev_info->dev);
  592. rc = device_create_file(&dev_info->dev, &dev_attr_shared);
  593. if (rc)
  594. goto unregister_dev;
  595. rc = device_create_file(&dev_info->dev, &dev_attr_save);
  596. if (rc)
  597. goto unregister_dev;
  598. rc = device_create_file(&dev_info->dev, &dev_attr_seglist);
  599. if (rc)
  600. goto unregister_dev;
  601. add_disk(dev_info->gd);
  602. switch (dev_info->segment_type) {
  603. case SEG_TYPE_SR:
  604. case SEG_TYPE_ER:
  605. case SEG_TYPE_SC:
  606. set_disk_ro(dev_info->gd,1);
  607. break;
  608. default:
  609. set_disk_ro(dev_info->gd,0);
  610. break;
  611. }
  612. up_write(&dcssblk_devices_sem);
  613. rc = count;
  614. goto out;
  615. unregister_dev:
  616. list_del(&dev_info->lh);
  617. blk_cleanup_queue(dev_info->dcssblk_queue);
  618. dev_info->gd->queue = NULL;
  619. put_disk(dev_info->gd);
  620. device_unregister(&dev_info->dev);
  621. list_for_each_entry(seg_info, &dev_info->seg_list, lh) {
  622. segment_unload(seg_info->segment_name);
  623. }
  624. put_device(&dev_info->dev);
  625. up_write(&dcssblk_devices_sem);
  626. goto out;
  627. dev_list_del:
  628. list_del(&dev_info->lh);
  629. release_gd:
  630. blk_cleanup_queue(dev_info->dcssblk_queue);
  631. dev_info->gd->queue = NULL;
  632. put_disk(dev_info->gd);
  633. up_write(&dcssblk_devices_sem);
  634. seg_list_del:
  635. if (dev_info == NULL)
  636. goto out;
  637. list_for_each_entry_safe(seg_info, temp, &dev_info->seg_list, lh) {
  638. list_del(&seg_info->lh);
  639. segment_unload(seg_info->segment_name);
  640. kfree(seg_info);
  641. }
  642. kfree(dev_info);
  643. out:
  644. kfree(local_buf);
  645. out_nobuf:
  646. return rc;
  647. }
  648. /*
  649. * device attribute for removing devices
  650. */
  651. static ssize_t
  652. dcssblk_remove_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  653. {
  654. struct dcssblk_dev_info *dev_info;
  655. struct segment_info *entry;
  656. int rc, i;
  657. char *local_buf;
  658. if (dev != dcssblk_root_dev) {
  659. return -EINVAL;
  660. }
  661. local_buf = kmalloc(count + 1, GFP_KERNEL);
  662. if (local_buf == NULL) {
  663. return -ENOMEM;
  664. }
  665. /*
  666. * parse input
  667. */
  668. for (i = 0; ((*(buf+i)!='\0') && (*(buf+i)!='\n') && i < count); i++) {
  669. local_buf[i] = toupper(buf[i]);
  670. }
  671. local_buf[i] = '\0';
  672. if ((i == 0) || (i > 8)) {
  673. rc = -ENAMETOOLONG;
  674. goto out_buf;
  675. }
  676. down_write(&dcssblk_devices_sem);
  677. dev_info = dcssblk_get_device_by_name(local_buf);
  678. if (dev_info == NULL) {
  679. up_write(&dcssblk_devices_sem);
  680. PRINT_WARN("Device %s is not loaded!\n", local_buf);
  681. rc = -ENODEV;
  682. goto out_buf;
  683. }
  684. if (atomic_read(&dev_info->use_count) != 0) {
  685. up_write(&dcssblk_devices_sem);
  686. PRINT_WARN("Device %s is in use!\n", local_buf);
  687. rc = -EBUSY;
  688. goto out_buf;
  689. }
  690. list_del(&dev_info->lh);
  691. del_gendisk(dev_info->gd);
  692. blk_cleanup_queue(dev_info->dcssblk_queue);
  693. dev_info->gd->queue = NULL;
  694. put_disk(dev_info->gd);
  695. device_unregister(&dev_info->dev);
  696. /* unload all related segments */
  697. list_for_each_entry(entry, &dev_info->seg_list, lh)
  698. segment_unload(entry->segment_name);
  699. put_device(&dev_info->dev);
  700. up_write(&dcssblk_devices_sem);
  701. rc = count;
  702. out_buf:
  703. kfree(local_buf);
  704. return rc;
  705. }
  706. static int
  707. dcssblk_open(struct block_device *bdev, fmode_t mode)
  708. {
  709. struct dcssblk_dev_info *dev_info;
  710. int rc;
  711. dev_info = bdev->bd_disk->private_data;
  712. if (NULL == dev_info) {
  713. rc = -ENODEV;
  714. goto out;
  715. }
  716. atomic_inc(&dev_info->use_count);
  717. bdev->bd_block_size = 4096;
  718. rc = 0;
  719. out:
  720. return rc;
  721. }
  722. static int
  723. dcssblk_release(struct gendisk *disk, fmode_t mode)
  724. {
  725. struct dcssblk_dev_info *dev_info = disk->private_data;
  726. struct segment_info *entry;
  727. int rc;
  728. if (!dev_info) {
  729. rc = -ENODEV;
  730. goto out;
  731. }
  732. down_write(&dcssblk_devices_sem);
  733. if (atomic_dec_and_test(&dev_info->use_count)
  734. && (dev_info->save_pending)) {
  735. PRINT_INFO("Device %s became idle and is being saved now\n",
  736. dev_info->segment_name);
  737. list_for_each_entry(entry, &dev_info->seg_list, lh) {
  738. segment_save(entry->segment_name);
  739. }
  740. dev_info->save_pending = 0;
  741. }
  742. up_write(&dcssblk_devices_sem);
  743. rc = 0;
  744. out:
  745. return rc;
  746. }
  747. static int
  748. dcssblk_make_request(struct request_queue *q, struct bio *bio)
  749. {
  750. struct dcssblk_dev_info *dev_info;
  751. struct bio_vec *bvec;
  752. unsigned long index;
  753. unsigned long page_addr;
  754. unsigned long source_addr;
  755. unsigned long bytes_done;
  756. int i;
  757. bytes_done = 0;
  758. dev_info = bio->bi_bdev->bd_disk->private_data;
  759. if (dev_info == NULL)
  760. goto fail;
  761. if ((bio->bi_sector & 7) != 0 || (bio->bi_size & 4095) != 0)
  762. /* Request is not page-aligned. */
  763. goto fail;
  764. if (((bio->bi_size >> 9) + bio->bi_sector)
  765. > get_capacity(bio->bi_bdev->bd_disk)) {
  766. /* Request beyond end of DCSS segment. */
  767. goto fail;
  768. }
  769. /* verify data transfer direction */
  770. if (dev_info->is_shared) {
  771. switch (dev_info->segment_type) {
  772. case SEG_TYPE_SR:
  773. case SEG_TYPE_ER:
  774. case SEG_TYPE_SC:
  775. /* cannot write to these segments */
  776. if (bio_data_dir(bio) == WRITE) {
  777. PRINT_WARN("rejecting write to ro device %s\n",
  778. dev_name(&dev_info->dev));
  779. goto fail;
  780. }
  781. }
  782. }
  783. index = (bio->bi_sector >> 3);
  784. bio_for_each_segment(bvec, bio, i) {
  785. page_addr = (unsigned long)
  786. page_address(bvec->bv_page) + bvec->bv_offset;
  787. source_addr = dev_info->start + (index<<12) + bytes_done;
  788. if (unlikely((page_addr & 4095) != 0) || (bvec->bv_len & 4095) != 0)
  789. // More paranoia.
  790. goto fail;
  791. if (bio_data_dir(bio) == READ) {
  792. memcpy((void*)page_addr, (void*)source_addr,
  793. bvec->bv_len);
  794. } else {
  795. memcpy((void*)source_addr, (void*)page_addr,
  796. bvec->bv_len);
  797. }
  798. bytes_done += bvec->bv_len;
  799. }
  800. bio_endio(bio, 0);
  801. return 0;
  802. fail:
  803. bio_io_error(bio);
  804. return 0;
  805. }
  806. static int
  807. dcssblk_direct_access (struct block_device *bdev, sector_t secnum,
  808. void **kaddr, unsigned long *pfn)
  809. {
  810. struct dcssblk_dev_info *dev_info;
  811. unsigned long pgoff;
  812. dev_info = bdev->bd_disk->private_data;
  813. if (!dev_info)
  814. return -ENODEV;
  815. if (secnum % (PAGE_SIZE/512))
  816. return -EINVAL;
  817. pgoff = secnum / (PAGE_SIZE / 512);
  818. if ((pgoff+1)*PAGE_SIZE-1 > dev_info->end - dev_info->start)
  819. return -ERANGE;
  820. *kaddr = (void *) (dev_info->start+pgoff*PAGE_SIZE);
  821. *pfn = virt_to_phys(*kaddr) >> PAGE_SHIFT;
  822. return 0;
  823. }
  824. static void
  825. dcssblk_check_params(void)
  826. {
  827. int rc, i, j, k;
  828. char buf[DCSSBLK_PARM_LEN + 1];
  829. struct dcssblk_dev_info *dev_info;
  830. for (i = 0; (i < DCSSBLK_PARM_LEN) && (dcssblk_segments[i] != '\0');
  831. i++) {
  832. for (j = i; (dcssblk_segments[j] != ',') &&
  833. (dcssblk_segments[j] != '\0') &&
  834. (dcssblk_segments[j] != '(') &&
  835. (j < DCSSBLK_PARM_LEN); j++)
  836. {
  837. buf[j-i] = dcssblk_segments[j];
  838. }
  839. buf[j-i] = '\0';
  840. rc = dcssblk_add_store(dcssblk_root_dev, NULL, buf, j-i);
  841. if ((rc >= 0) && (dcssblk_segments[j] == '(')) {
  842. for (k = 0; (buf[k] != ':') && (buf[k] != '\0'); k++)
  843. buf[k] = toupper(buf[k]);
  844. buf[k] = '\0';
  845. if (!strncmp(&dcssblk_segments[j], "(local)", 7)) {
  846. down_read(&dcssblk_devices_sem);
  847. dev_info = dcssblk_get_device_by_name(buf);
  848. up_read(&dcssblk_devices_sem);
  849. if (dev_info)
  850. dcssblk_shared_store(&dev_info->dev,
  851. NULL, "0\n", 2);
  852. }
  853. }
  854. while ((dcssblk_segments[j] != ',') &&
  855. (dcssblk_segments[j] != '\0'))
  856. {
  857. j++;
  858. }
  859. if (dcssblk_segments[j] == '\0')
  860. break;
  861. i = j;
  862. }
  863. }
  864. /*
  865. * The init/exit functions.
  866. */
  867. static void __exit
  868. dcssblk_exit(void)
  869. {
  870. s390_root_dev_unregister(dcssblk_root_dev);
  871. unregister_blkdev(dcssblk_major, DCSSBLK_NAME);
  872. }
  873. static int __init
  874. dcssblk_init(void)
  875. {
  876. int rc;
  877. dcssblk_root_dev = s390_root_dev_register("dcssblk");
  878. if (IS_ERR(dcssblk_root_dev))
  879. return PTR_ERR(dcssblk_root_dev);
  880. rc = device_create_file(dcssblk_root_dev, &dev_attr_add);
  881. if (rc) {
  882. s390_root_dev_unregister(dcssblk_root_dev);
  883. return rc;
  884. }
  885. rc = device_create_file(dcssblk_root_dev, &dev_attr_remove);
  886. if (rc) {
  887. s390_root_dev_unregister(dcssblk_root_dev);
  888. return rc;
  889. }
  890. rc = register_blkdev(0, DCSSBLK_NAME);
  891. if (rc < 0) {
  892. s390_root_dev_unregister(dcssblk_root_dev);
  893. return rc;
  894. }
  895. dcssblk_major = rc;
  896. init_rwsem(&dcssblk_devices_sem);
  897. dcssblk_check_params();
  898. return 0;
  899. }
  900. module_init(dcssblk_init);
  901. module_exit(dcssblk_exit);
  902. module_param_string(segments, dcssblk_segments, DCSSBLK_PARM_LEN, 0444);
  903. MODULE_PARM_DESC(segments, "Name of DCSS segment(s) to be loaded, "
  904. "comma-separated list, names in each set separated "
  905. "by commas are separated by colons, each set contains "
  906. "names of contiguous segments and each name max. 8 chars.\n"
  907. "Adding \"(local)\" to the end of each set equals echoing 0 "
  908. "to /sys/devices/dcssblk/<device name>/shared after loading "
  909. "the contiguous segments - \n"
  910. "e.g. segments=\"mydcss1,mydcss2:mydcss3,mydcss4(local)\"");
  911. MODULE_LICENSE("GPL");