dcssblk.c 27 KB

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