dcssblk.c 27 KB

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