dcssblk.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * dcssblk.c -- the S/390 block driver for dcss memory
  4. *
  5. * Authors: Carsten Otte, Stefan Weinhuber, Gerald Schaefer
  6. */
  7. #define pr_fmt(fmt) "dcssblk: " 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/uio.h>
  18. #include <linux/dax.h>
  19. #include <linux/io.h>
  20. #include <asm/extmem.h>
  21. #define DCSSBLK_NAME "dcssblk"
  22. #define DCSSBLK_MINORS_PER_DISK 1
  23. #define DCSSBLK_PARM_LEN 400
  24. #define DCSS_BUS_ID_SIZE 20
  25. static int dcssblk_open(struct gendisk *disk, blk_mode_t mode);
  26. static void dcssblk_release(struct gendisk *disk);
  27. static void dcssblk_submit_bio(struct bio *bio);
  28. static long dcssblk_dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff,
  29. long nr_pages, enum dax_access_mode mode, void **kaddr,
  30. unsigned long *pfn);
  31. static char dcssblk_segments[DCSSBLK_PARM_LEN] = "\0";
  32. static int dcssblk_major;
  33. static const struct block_device_operations dcssblk_devops = {
  34. .owner = THIS_MODULE,
  35. .submit_bio = dcssblk_submit_bio,
  36. .open = dcssblk_open,
  37. .release = dcssblk_release,
  38. };
  39. static int dcssblk_dax_zero_page_range(struct dax_device *dax_dev,
  40. pgoff_t pgoff, size_t nr_pages)
  41. {
  42. long rc;
  43. void *kaddr;
  44. rc = dax_direct_access(dax_dev, pgoff, nr_pages, DAX_ACCESS,
  45. &kaddr, NULL);
  46. if (rc < 0)
  47. return dax_mem2blk_err(rc);
  48. memset(kaddr, 0, nr_pages << PAGE_SHIFT);
  49. dax_flush(dax_dev, kaddr, nr_pages << PAGE_SHIFT);
  50. return 0;
  51. }
  52. static const struct dax_operations dcssblk_dax_ops = {
  53. .direct_access = dcssblk_dax_direct_access,
  54. .zero_page_range = dcssblk_dax_zero_page_range,
  55. };
  56. struct dcssblk_dev_info {
  57. struct list_head lh;
  58. struct device dev;
  59. char segment_name[DCSS_BUS_ID_SIZE];
  60. atomic_t use_count;
  61. struct gendisk *gd;
  62. unsigned long start;
  63. unsigned long end;
  64. int segment_type;
  65. unsigned char save_pending;
  66. unsigned char is_shared;
  67. int num_of_segments;
  68. struct list_head seg_list;
  69. struct dax_device *dax_dev;
  70. struct dev_pagemap pgmap;
  71. void *pgmap_addr;
  72. };
  73. struct segment_info {
  74. struct list_head lh;
  75. char segment_name[DCSS_BUS_ID_SIZE];
  76. unsigned long start;
  77. unsigned long end;
  78. int segment_type;
  79. };
  80. static ssize_t dcssblk_add_store(struct device * dev, struct device_attribute *attr, const char * buf,
  81. size_t count);
  82. static ssize_t dcssblk_remove_store(struct device * dev, struct device_attribute *attr, const char * buf,
  83. size_t count);
  84. static DEVICE_ATTR(add, S_IWUSR, NULL, dcssblk_add_store);
  85. static DEVICE_ATTR(remove, S_IWUSR, NULL, dcssblk_remove_store);
  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 == entry->gd->first_minor)
  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_objs(struct segment_info, dev_info->num_of_segments);
  213. if (sort_list == NULL)
  214. return -ENOMEM;
  215. i = 0;
  216. list_for_each_entry(entry, &dev_info->seg_list, lh) {
  217. memcpy(&sort_list[i], entry, sizeof(struct segment_info));
  218. i++;
  219. }
  220. /* sort segments */
  221. for (i = 0; i < dev_info->num_of_segments; i++)
  222. for (j = 0; j < dev_info->num_of_segments; j++)
  223. if (sort_list[j].start > sort_list[i].start) {
  224. memcpy(&temp, &sort_list[i],
  225. sizeof(struct segment_info));
  226. memcpy(&sort_list[i], &sort_list[j],
  227. sizeof(struct segment_info));
  228. memcpy(&sort_list[j], &temp,
  229. sizeof(struct segment_info));
  230. }
  231. /* check continuity */
  232. for (i = 0; i < dev_info->num_of_segments - 1; i++) {
  233. if ((sort_list[i].end + 1) != sort_list[i+1].start) {
  234. pr_err("Adjacent DCSSs %s and %s are not "
  235. "contiguous\n", sort_list[i].segment_name,
  236. sort_list[i+1].segment_name);
  237. rc = -EINVAL;
  238. goto out;
  239. }
  240. /* EN and EW are allowed in a block device */
  241. if (sort_list[i].segment_type != sort_list[i+1].segment_type) {
  242. if (!(sort_list[i].segment_type & SEGMENT_EXCLUSIVE) ||
  243. (sort_list[i].segment_type == SEG_TYPE_ER) ||
  244. !(sort_list[i+1].segment_type &
  245. SEGMENT_EXCLUSIVE) ||
  246. (sort_list[i+1].segment_type == SEG_TYPE_ER)) {
  247. pr_err("DCSS %s and DCSS %s have "
  248. "incompatible types\n",
  249. sort_list[i].segment_name,
  250. sort_list[i+1].segment_name);
  251. rc = -EINVAL;
  252. goto out;
  253. }
  254. }
  255. }
  256. rc = 0;
  257. out:
  258. kfree(sort_list);
  259. return rc;
  260. }
  261. /*
  262. * Load a segment
  263. */
  264. static int
  265. dcssblk_load_segment(char *name, struct segment_info **seg_info)
  266. {
  267. int rc;
  268. /* already loaded? */
  269. down_read(&dcssblk_devices_sem);
  270. *seg_info = dcssblk_get_segment_by_name(name);
  271. up_read(&dcssblk_devices_sem);
  272. if (*seg_info != NULL)
  273. return -EEXIST;
  274. /* get a struct segment_info */
  275. *seg_info = kzalloc_obj(struct segment_info);
  276. if (*seg_info == NULL)
  277. return -ENOMEM;
  278. strscpy((*seg_info)->segment_name, name);
  279. /* load the segment */
  280. rc = segment_load(name, SEGMENT_SHARED,
  281. &(*seg_info)->start, &(*seg_info)->end);
  282. if (rc < 0) {
  283. segment_warning(rc, (*seg_info)->segment_name);
  284. kfree(*seg_info);
  285. } else {
  286. INIT_LIST_HEAD(&(*seg_info)->lh);
  287. (*seg_info)->segment_type = rc;
  288. }
  289. return rc;
  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 sysfs_emit(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. up_write(&dcssblk_devices_sem);
  370. dax_remove_host(dev_info->gd);
  371. kill_dax(dev_info->dax_dev);
  372. put_dax(dev_info->dax_dev);
  373. if (dev_info->pgmap_addr)
  374. devm_memunmap_pages(&dev_info->dev, &dev_info->pgmap);
  375. del_gendisk(dev_info->gd);
  376. put_disk(dev_info->gd);
  377. if (device_remove_file_self(dev, attr)) {
  378. device_unregister(dev);
  379. put_device(dev);
  380. }
  381. return rc;
  382. out:
  383. up_write(&dcssblk_devices_sem);
  384. return rc;
  385. }
  386. static DEVICE_ATTR(shared, S_IWUSR | S_IRUSR, dcssblk_shared_show,
  387. dcssblk_shared_store);
  388. /*
  389. * device attribute for save operation on current copy
  390. * of the segment. If the segment is busy, saving will
  391. * become pending until it gets released, which can be
  392. * undone by storing a non-true value to this entry.
  393. * (show + store)
  394. */
  395. static ssize_t
  396. dcssblk_save_show(struct device *dev, struct device_attribute *attr, char *buf)
  397. {
  398. struct dcssblk_dev_info *dev_info;
  399. dev_info = container_of(dev, struct dcssblk_dev_info, dev);
  400. return sysfs_emit(buf, dev_info->save_pending ? "1\n" : "0\n");
  401. }
  402. static ssize_t
  403. dcssblk_save_store(struct device *dev, struct device_attribute *attr, const char *inbuf, size_t count)
  404. {
  405. struct dcssblk_dev_info *dev_info;
  406. struct segment_info *entry;
  407. if ((count > 1) && (inbuf[1] != '\n') && (inbuf[1] != '\0'))
  408. return -EINVAL;
  409. dev_info = container_of(dev, struct dcssblk_dev_info, dev);
  410. down_write(&dcssblk_devices_sem);
  411. if (inbuf[0] == '1') {
  412. if (atomic_read(&dev_info->use_count) == 0) {
  413. // device is idle => we save immediately
  414. pr_info("All DCSSs that map to device %s are "
  415. "saved\n", dev_info->segment_name);
  416. list_for_each_entry(entry, &dev_info->seg_list, lh) {
  417. if (entry->segment_type == SEG_TYPE_EN ||
  418. entry->segment_type == SEG_TYPE_SN)
  419. pr_warn("DCSS %s is of type SN or EN"
  420. " and cannot be saved\n",
  421. entry->segment_name);
  422. else
  423. segment_save(entry->segment_name);
  424. }
  425. } else {
  426. // device is busy => we save it when it becomes
  427. // idle in dcssblk_release
  428. pr_info("Device %s is in use, its DCSSs will be "
  429. "saved when it becomes idle\n",
  430. dev_info->segment_name);
  431. dev_info->save_pending = 1;
  432. }
  433. } else if (inbuf[0] == '0') {
  434. if (dev_info->save_pending) {
  435. // device is busy & the user wants to undo his save
  436. // request
  437. dev_info->save_pending = 0;
  438. pr_info("A pending save request for device %s "
  439. "has been canceled\n",
  440. dev_info->segment_name);
  441. }
  442. } else {
  443. up_write(&dcssblk_devices_sem);
  444. return -EINVAL;
  445. }
  446. up_write(&dcssblk_devices_sem);
  447. return count;
  448. }
  449. static DEVICE_ATTR(save, S_IWUSR | S_IRUSR, dcssblk_save_show,
  450. dcssblk_save_store);
  451. /*
  452. * device attribute for showing all segments in a device
  453. */
  454. static ssize_t
  455. dcssblk_seglist_show(struct device *dev, struct device_attribute *attr,
  456. char *buf)
  457. {
  458. struct dcssblk_dev_info *dev_info;
  459. struct segment_info *entry;
  460. int i;
  461. i = 0;
  462. down_read(&dcssblk_devices_sem);
  463. dev_info = container_of(dev, struct dcssblk_dev_info, dev);
  464. list_for_each_entry(entry, &dev_info->seg_list, lh)
  465. i += sysfs_emit_at(buf, i, "%s\n", entry->segment_name);
  466. up_read(&dcssblk_devices_sem);
  467. return i;
  468. }
  469. static DEVICE_ATTR(seglist, S_IRUSR, dcssblk_seglist_show, NULL);
  470. static struct attribute *dcssblk_dev_attrs[] = {
  471. &dev_attr_shared.attr,
  472. &dev_attr_save.attr,
  473. &dev_attr_seglist.attr,
  474. NULL,
  475. };
  476. static struct attribute_group dcssblk_dev_attr_group = {
  477. .attrs = dcssblk_dev_attrs,
  478. };
  479. static const struct attribute_group *dcssblk_dev_attr_groups[] = {
  480. &dcssblk_dev_attr_group,
  481. NULL,
  482. };
  483. static int dcssblk_setup_dax(struct dcssblk_dev_info *dev_info)
  484. {
  485. struct dax_device *dax_dev;
  486. dax_dev = alloc_dax(dev_info, &dcssblk_dax_ops);
  487. if (IS_ERR(dax_dev))
  488. return PTR_ERR(dax_dev);
  489. set_dax_synchronous(dax_dev);
  490. dev_info->dax_dev = dax_dev;
  491. return dax_add_host(dev_info->dax_dev, dev_info->gd);
  492. }
  493. /*
  494. * device attribute for adding devices
  495. */
  496. static ssize_t
  497. dcssblk_add_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  498. {
  499. struct queue_limits lim = {
  500. .logical_block_size = 4096,
  501. .features = BLK_FEAT_DAX,
  502. };
  503. int rc, i, j, num_of_segments;
  504. struct dcssblk_dev_info *dev_info;
  505. struct segment_info *seg_info, *temp;
  506. char *local_buf;
  507. void *addr;
  508. unsigned long seg_byte_size;
  509. dev_info = NULL;
  510. seg_info = NULL;
  511. if (dev != dcssblk_root_dev) {
  512. rc = -EINVAL;
  513. goto out_nobuf;
  514. }
  515. if ((count < 1) || (buf[0] == '\0') || (buf[0] == '\n')) {
  516. rc = -ENAMETOOLONG;
  517. goto out_nobuf;
  518. }
  519. local_buf = kmalloc(count + 1, GFP_KERNEL);
  520. if (local_buf == NULL) {
  521. rc = -ENOMEM;
  522. goto out_nobuf;
  523. }
  524. /*
  525. * parse input
  526. */
  527. num_of_segments = 0;
  528. for (i = 0; (i < count && (buf[i] != '\0') && (buf[i] != '\n')); i++) {
  529. for (j = i; j < count &&
  530. (buf[j] != ':') &&
  531. (buf[j] != '\0') &&
  532. (buf[j] != '\n'); j++) {
  533. local_buf[j-i] = toupper(buf[j]);
  534. }
  535. local_buf[j-i] = '\0';
  536. if (((j - i) == 0) || ((j - i) > 8)) {
  537. rc = -ENAMETOOLONG;
  538. goto seg_list_del;
  539. }
  540. rc = dcssblk_load_segment(local_buf, &seg_info);
  541. if (rc < 0)
  542. goto seg_list_del;
  543. /*
  544. * get a struct dcssblk_dev_info
  545. */
  546. if (num_of_segments == 0) {
  547. dev_info = kzalloc_obj(struct dcssblk_dev_info);
  548. if (dev_info == NULL) {
  549. rc = -ENOMEM;
  550. goto out;
  551. }
  552. strscpy(dev_info->segment_name, local_buf);
  553. dev_info->segment_type = seg_info->segment_type;
  554. INIT_LIST_HEAD(&dev_info->seg_list);
  555. }
  556. list_add_tail(&seg_info->lh, &dev_info->seg_list);
  557. num_of_segments++;
  558. i = j;
  559. if ((buf[j] == '\0') || (buf[j] == '\n'))
  560. break;
  561. }
  562. /* no trailing colon at the end of the input */
  563. if ((i > 0) && (buf[i-1] == ':')) {
  564. rc = -ENAMETOOLONG;
  565. goto seg_list_del;
  566. }
  567. strscpy(local_buf, buf, i + 1);
  568. dev_info->num_of_segments = num_of_segments;
  569. rc = dcssblk_is_continuous(dev_info);
  570. if (rc < 0)
  571. goto seg_list_del;
  572. dev_info->start = dcssblk_find_lowest_addr(dev_info);
  573. dev_info->end = dcssblk_find_highest_addr(dev_info);
  574. dev_set_name(&dev_info->dev, "%s", dev_info->segment_name);
  575. dev_info->dev.release = dcssblk_release_segment;
  576. dev_info->dev.groups = dcssblk_dev_attr_groups;
  577. INIT_LIST_HEAD(&dev_info->lh);
  578. dev_info->gd = blk_alloc_disk(&lim, NUMA_NO_NODE);
  579. if (IS_ERR(dev_info->gd)) {
  580. rc = PTR_ERR(dev_info->gd);
  581. goto seg_list_del;
  582. }
  583. dev_info->gd->major = dcssblk_major;
  584. dev_info->gd->minors = DCSSBLK_MINORS_PER_DISK;
  585. dev_info->gd->fops = &dcssblk_devops;
  586. dev_info->gd->private_data = dev_info;
  587. dev_info->gd->flags |= GENHD_FL_NO_PART;
  588. seg_byte_size = (dev_info->end - dev_info->start + 1);
  589. set_capacity(dev_info->gd, seg_byte_size >> 9); // size in sectors
  590. pr_info("Loaded %s with total size %lu bytes and capacity %lu "
  591. "sectors\n", local_buf, seg_byte_size, seg_byte_size >> 9);
  592. dev_info->save_pending = 0;
  593. dev_info->is_shared = 1;
  594. dev_info->dev.parent = dcssblk_root_dev;
  595. /*
  596. *get minor, add to list
  597. */
  598. down_write(&dcssblk_devices_sem);
  599. if (dcssblk_get_segment_by_name(local_buf)) {
  600. rc = -EEXIST;
  601. goto release_gd;
  602. }
  603. rc = dcssblk_assign_free_minor(dev_info);
  604. if (rc)
  605. goto release_gd;
  606. scnprintf(dev_info->gd->disk_name, sizeof(dev_info->gd->disk_name),
  607. "dcssblk%d", dev_info->gd->first_minor);
  608. list_add_tail(&dev_info->lh, &dcssblk_devices);
  609. if (!try_module_get(THIS_MODULE)) {
  610. rc = -ENODEV;
  611. goto dev_list_del;
  612. }
  613. /*
  614. * register the device
  615. */
  616. rc = device_register(&dev_info->dev);
  617. if (rc)
  618. goto put_dev;
  619. if (!IS_ALIGNED(dev_info->start, SUBSECTION_SIZE) ||
  620. !IS_ALIGNED(dev_info->end + 1, SUBSECTION_SIZE)) {
  621. pr_info("DCSS %s is not aligned to %lu bytes, DAX support disabled\n",
  622. local_buf, SUBSECTION_SIZE);
  623. } else {
  624. dev_info->pgmap.type = MEMORY_DEVICE_FS_DAX;
  625. dev_info->pgmap.range.start = dev_info->start;
  626. dev_info->pgmap.range.end = dev_info->end;
  627. dev_info->pgmap.nr_range = 1;
  628. addr = devm_memremap_pages(&dev_info->dev, &dev_info->pgmap);
  629. if (IS_ERR(addr)) {
  630. rc = PTR_ERR(addr);
  631. goto put_dev;
  632. }
  633. dev_info->pgmap_addr = addr;
  634. rc = dcssblk_setup_dax(dev_info);
  635. if (rc)
  636. goto out_dax;
  637. pr_info("DAX support enabled for DCSS %s\n", local_buf);
  638. }
  639. get_device(&dev_info->dev);
  640. rc = device_add_disk(&dev_info->dev, dev_info->gd, NULL);
  641. if (rc)
  642. goto out_dax_host;
  643. switch (dev_info->segment_type) {
  644. case SEG_TYPE_SR:
  645. case SEG_TYPE_ER:
  646. case SEG_TYPE_SC:
  647. set_disk_ro(dev_info->gd,1);
  648. break;
  649. default:
  650. set_disk_ro(dev_info->gd,0);
  651. break;
  652. }
  653. up_write(&dcssblk_devices_sem);
  654. rc = count;
  655. goto out;
  656. out_dax_host:
  657. put_device(&dev_info->dev);
  658. dax_remove_host(dev_info->gd);
  659. out_dax:
  660. kill_dax(dev_info->dax_dev);
  661. put_dax(dev_info->dax_dev);
  662. if (dev_info->pgmap_addr)
  663. devm_memunmap_pages(&dev_info->dev, &dev_info->pgmap);
  664. put_dev:
  665. list_del(&dev_info->lh);
  666. put_disk(dev_info->gd);
  667. list_for_each_entry(seg_info, &dev_info->seg_list, lh) {
  668. segment_unload(seg_info->segment_name);
  669. }
  670. put_device(&dev_info->dev);
  671. up_write(&dcssblk_devices_sem);
  672. goto out;
  673. dev_list_del:
  674. list_del(&dev_info->lh);
  675. release_gd:
  676. put_disk(dev_info->gd);
  677. up_write(&dcssblk_devices_sem);
  678. seg_list_del:
  679. if (dev_info == NULL)
  680. goto out;
  681. list_for_each_entry_safe(seg_info, temp, &dev_info->seg_list, lh) {
  682. list_del(&seg_info->lh);
  683. segment_unload(seg_info->segment_name);
  684. kfree(seg_info);
  685. }
  686. kfree(dev_info);
  687. out:
  688. kfree(local_buf);
  689. out_nobuf:
  690. return rc;
  691. }
  692. /*
  693. * device attribute for removing devices
  694. */
  695. static ssize_t
  696. dcssblk_remove_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  697. {
  698. struct dcssblk_dev_info *dev_info;
  699. struct segment_info *entry;
  700. int rc, i;
  701. char *local_buf;
  702. if (dev != dcssblk_root_dev) {
  703. return -EINVAL;
  704. }
  705. local_buf = kmalloc(count + 1, GFP_KERNEL);
  706. if (local_buf == NULL) {
  707. return -ENOMEM;
  708. }
  709. /*
  710. * parse input
  711. */
  712. for (i = 0; (i < count && (*(buf+i)!='\0') && (*(buf+i)!='\n')); i++) {
  713. local_buf[i] = toupper(buf[i]);
  714. }
  715. local_buf[i] = '\0';
  716. if ((i == 0) || (i > 8)) {
  717. rc = -ENAMETOOLONG;
  718. goto out_buf;
  719. }
  720. down_write(&dcssblk_devices_sem);
  721. dev_info = dcssblk_get_device_by_name(local_buf);
  722. if (dev_info == NULL) {
  723. up_write(&dcssblk_devices_sem);
  724. pr_warn("Device %s cannot be removed because it is not a known device\n",
  725. local_buf);
  726. rc = -ENODEV;
  727. goto out_buf;
  728. }
  729. if (atomic_read(&dev_info->use_count) != 0) {
  730. up_write(&dcssblk_devices_sem);
  731. pr_warn("Device %s cannot be removed while it is in use\n",
  732. local_buf);
  733. rc = -EBUSY;
  734. goto out_buf;
  735. }
  736. list_del(&dev_info->lh);
  737. /* unload all related segments */
  738. list_for_each_entry(entry, &dev_info->seg_list, lh)
  739. segment_unload(entry->segment_name);
  740. up_write(&dcssblk_devices_sem);
  741. dax_remove_host(dev_info->gd);
  742. kill_dax(dev_info->dax_dev);
  743. put_dax(dev_info->dax_dev);
  744. if (dev_info->pgmap_addr)
  745. devm_memunmap_pages(&dev_info->dev, &dev_info->pgmap);
  746. del_gendisk(dev_info->gd);
  747. put_disk(dev_info->gd);
  748. device_unregister(&dev_info->dev);
  749. put_device(&dev_info->dev);
  750. rc = count;
  751. out_buf:
  752. kfree(local_buf);
  753. return rc;
  754. }
  755. static int
  756. dcssblk_open(struct gendisk *disk, blk_mode_t mode)
  757. {
  758. struct dcssblk_dev_info *dev_info = disk->private_data;
  759. int rc;
  760. if (NULL == dev_info) {
  761. rc = -ENODEV;
  762. goto out;
  763. }
  764. atomic_inc(&dev_info->use_count);
  765. rc = 0;
  766. out:
  767. return rc;
  768. }
  769. static void
  770. dcssblk_release(struct gendisk *disk)
  771. {
  772. struct dcssblk_dev_info *dev_info = disk->private_data;
  773. struct segment_info *entry;
  774. if (!dev_info) {
  775. WARN_ON(1);
  776. return;
  777. }
  778. down_write(&dcssblk_devices_sem);
  779. if (atomic_dec_and_test(&dev_info->use_count)
  780. && (dev_info->save_pending)) {
  781. pr_info("Device %s has become idle and is being saved "
  782. "now\n", dev_info->segment_name);
  783. list_for_each_entry(entry, &dev_info->seg_list, lh) {
  784. if (entry->segment_type == SEG_TYPE_EN ||
  785. entry->segment_type == SEG_TYPE_SN)
  786. pr_warn("DCSS %s is of type SN or EN and cannot"
  787. " be saved\n", entry->segment_name);
  788. else
  789. segment_save(entry->segment_name);
  790. }
  791. dev_info->save_pending = 0;
  792. }
  793. up_write(&dcssblk_devices_sem);
  794. }
  795. static void
  796. dcssblk_submit_bio(struct bio *bio)
  797. {
  798. struct dcssblk_dev_info *dev_info;
  799. struct bio_vec bvec;
  800. struct bvec_iter iter;
  801. unsigned long index;
  802. void *page_addr;
  803. unsigned long source_addr;
  804. unsigned long bytes_done;
  805. bytes_done = 0;
  806. dev_info = bio->bi_bdev->bd_disk->private_data;
  807. if (dev_info == NULL)
  808. goto fail;
  809. if (!IS_ALIGNED(bio->bi_iter.bi_sector, 8) ||
  810. !IS_ALIGNED(bio->bi_iter.bi_size, PAGE_SIZE))
  811. /* Request is not page-aligned. */
  812. goto fail;
  813. /* verify data transfer direction */
  814. if (dev_info->is_shared) {
  815. switch (dev_info->segment_type) {
  816. case SEG_TYPE_SR:
  817. case SEG_TYPE_ER:
  818. case SEG_TYPE_SC:
  819. /* cannot write to these segments */
  820. if (bio_data_dir(bio) == WRITE) {
  821. pr_warn("Writing to %s failed because it is a read-only device\n",
  822. dev_name(&dev_info->dev));
  823. goto fail;
  824. }
  825. }
  826. }
  827. index = (bio->bi_iter.bi_sector >> 3);
  828. bio_for_each_segment(bvec, bio, iter) {
  829. page_addr = bvec_virt(&bvec);
  830. source_addr = dev_info->start + (index<<12) + bytes_done;
  831. if (unlikely(!IS_ALIGNED((unsigned long)page_addr, PAGE_SIZE) ||
  832. !IS_ALIGNED(bvec.bv_len, PAGE_SIZE)))
  833. // More paranoia.
  834. goto fail;
  835. if (bio_data_dir(bio) == READ)
  836. memcpy(page_addr, __va(source_addr), bvec.bv_len);
  837. else
  838. memcpy(__va(source_addr), page_addr, bvec.bv_len);
  839. bytes_done += bvec.bv_len;
  840. }
  841. bio_endio(bio);
  842. return;
  843. fail:
  844. bio_io_error(bio);
  845. }
  846. static long
  847. __dcssblk_direct_access(struct dcssblk_dev_info *dev_info, pgoff_t pgoff,
  848. long nr_pages, void **kaddr, unsigned long *pfn)
  849. {
  850. resource_size_t offset = pgoff * PAGE_SIZE;
  851. unsigned long dev_sz;
  852. dev_sz = dev_info->end - dev_info->start + 1;
  853. if (kaddr)
  854. *kaddr = __va(dev_info->start + offset);
  855. if (pfn)
  856. *pfn = PFN_DOWN(dev_info->start + offset);
  857. return (dev_sz - offset) / PAGE_SIZE;
  858. }
  859. static long
  860. dcssblk_dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff,
  861. long nr_pages, enum dax_access_mode mode, void **kaddr,
  862. unsigned long *pfn)
  863. {
  864. struct dcssblk_dev_info *dev_info = dax_get_private(dax_dev);
  865. return __dcssblk_direct_access(dev_info, pgoff, nr_pages, kaddr, pfn);
  866. }
  867. static void
  868. dcssblk_check_params(void)
  869. {
  870. int rc, i, j, k;
  871. char buf[DCSSBLK_PARM_LEN + 1];
  872. struct dcssblk_dev_info *dev_info;
  873. for (i = 0; (i < DCSSBLK_PARM_LEN) && (dcssblk_segments[i] != '\0');
  874. i++) {
  875. for (j = i; (j < DCSSBLK_PARM_LEN) &&
  876. (dcssblk_segments[j] != ',') &&
  877. (dcssblk_segments[j] != '\0') &&
  878. (dcssblk_segments[j] != '('); j++)
  879. {
  880. buf[j-i] = dcssblk_segments[j];
  881. }
  882. buf[j-i] = '\0';
  883. rc = dcssblk_add_store(dcssblk_root_dev, NULL, buf, j-i);
  884. if ((rc >= 0) && (dcssblk_segments[j] == '(')) {
  885. for (k = 0; (buf[k] != ':') && (buf[k] != '\0'); k++)
  886. buf[k] = toupper(buf[k]);
  887. buf[k] = '\0';
  888. if (!strncmp(&dcssblk_segments[j], "(local)", 7)) {
  889. down_read(&dcssblk_devices_sem);
  890. dev_info = dcssblk_get_device_by_name(buf);
  891. up_read(&dcssblk_devices_sem);
  892. if (dev_info)
  893. dcssblk_shared_store(&dev_info->dev,
  894. NULL, "0\n", 2);
  895. }
  896. }
  897. while ((dcssblk_segments[j] != ',') &&
  898. (dcssblk_segments[j] != '\0'))
  899. {
  900. j++;
  901. }
  902. if (dcssblk_segments[j] == '\0')
  903. break;
  904. i = j;
  905. }
  906. }
  907. /*
  908. * The init/exit functions.
  909. */
  910. static void __exit
  911. dcssblk_exit(void)
  912. {
  913. root_device_unregister(dcssblk_root_dev);
  914. unregister_blkdev(dcssblk_major, DCSSBLK_NAME);
  915. }
  916. static int __init
  917. dcssblk_init(void)
  918. {
  919. int rc;
  920. dcssblk_root_dev = root_device_register("dcssblk");
  921. if (IS_ERR(dcssblk_root_dev))
  922. return PTR_ERR(dcssblk_root_dev);
  923. rc = device_create_file(dcssblk_root_dev, &dev_attr_add);
  924. if (rc)
  925. goto out_root;
  926. rc = device_create_file(dcssblk_root_dev, &dev_attr_remove);
  927. if (rc)
  928. goto out_root;
  929. rc = register_blkdev(0, DCSSBLK_NAME);
  930. if (rc < 0)
  931. goto out_root;
  932. dcssblk_major = rc;
  933. init_rwsem(&dcssblk_devices_sem);
  934. dcssblk_check_params();
  935. return 0;
  936. out_root:
  937. root_device_unregister(dcssblk_root_dev);
  938. return rc;
  939. }
  940. module_init(dcssblk_init);
  941. module_exit(dcssblk_exit);
  942. module_param_string(segments, dcssblk_segments, DCSSBLK_PARM_LEN, 0444);
  943. MODULE_PARM_DESC(segments, "Name of DCSS segment(s) to be loaded, "
  944. "comma-separated list, names in each set separated "
  945. "by commas are separated by colons, each set contains "
  946. "names of contiguous segments and each name max. 8 chars.\n"
  947. "Adding \"(local)\" to the end of each set equals echoing 0 "
  948. "to /sys/devices/dcssblk/<device name>/shared after loading "
  949. "the contiguous segments - \n"
  950. "e.g. segments=\"mydcss1,mydcss2:mydcss3,mydcss4(local)\"");
  951. MODULE_DESCRIPTION("S/390 block driver for DCSS memory");
  952. MODULE_LICENSE("GPL");