dcssblk.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  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 KMSG_COMPONENT "dcssblk"
  8. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  9. #include <linux/module.h>
  10. #include <linux/moduleparam.h>
  11. #include <linux/ctype.h>
  12. #include <linux/errno.h>
  13. #include <linux/init.h>
  14. #include <linux/slab.h>
  15. #include <linux/blkdev.h>
  16. #include <linux/completion.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/pfn_t.h>
  19. #include <linux/uio.h>
  20. #include <linux/dax.h>
  21. #include <linux/io.h>
  22. #include <asm/extmem.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 gendisk *disk, blk_mode_t mode);
  28. static void dcssblk_release(struct gendisk *disk);
  29. static void dcssblk_submit_bio(struct bio *bio);
  30. static long dcssblk_dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff,
  31. long nr_pages, enum dax_access_mode mode, void **kaddr,
  32. 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. .submit_bio = dcssblk_submit_bio,
  38. .open = dcssblk_open,
  39. .release = dcssblk_release,
  40. };
  41. static int dcssblk_dax_zero_page_range(struct dax_device *dax_dev,
  42. pgoff_t pgoff, size_t nr_pages)
  43. {
  44. long rc;
  45. void *kaddr;
  46. rc = dax_direct_access(dax_dev, pgoff, nr_pages, DAX_ACCESS,
  47. &kaddr, NULL);
  48. if (rc < 0)
  49. return dax_mem2blk_err(rc);
  50. memset(kaddr, 0, nr_pages << PAGE_SHIFT);
  51. dax_flush(dax_dev, kaddr, nr_pages << PAGE_SHIFT);
  52. return 0;
  53. }
  54. static const struct dax_operations dcssblk_dax_ops = {
  55. .direct_access = dcssblk_dax_direct_access,
  56. .zero_page_range = dcssblk_dax_zero_page_range,
  57. };
  58. struct dcssblk_dev_info {
  59. struct list_head lh;
  60. struct device dev;
  61. char segment_name[DCSS_BUS_ID_SIZE];
  62. atomic_t use_count;
  63. struct gendisk *gd;
  64. unsigned long start;
  65. unsigned long end;
  66. int segment_type;
  67. unsigned char save_pending;
  68. unsigned char is_shared;
  69. int num_of_segments;
  70. struct list_head seg_list;
  71. struct dax_device *dax_dev;
  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 = kcalloc(dev_info->num_of_segments,
  213. sizeof(struct segment_info),
  214. GFP_KERNEL);
  215. if (sort_list == NULL)
  216. return -ENOMEM;
  217. i = 0;
  218. list_for_each_entry(entry, &dev_info->seg_list, lh) {
  219. memcpy(&sort_list[i], entry, sizeof(struct segment_info));
  220. i++;
  221. }
  222. /* sort segments */
  223. for (i = 0; i < dev_info->num_of_segments; i++)
  224. for (j = 0; j < dev_info->num_of_segments; j++)
  225. if (sort_list[j].start > sort_list[i].start) {
  226. memcpy(&temp, &sort_list[i],
  227. sizeof(struct segment_info));
  228. memcpy(&sort_list[i], &sort_list[j],
  229. sizeof(struct segment_info));
  230. memcpy(&sort_list[j], &temp,
  231. sizeof(struct segment_info));
  232. }
  233. /* check continuity */
  234. for (i = 0; i < dev_info->num_of_segments - 1; i++) {
  235. if ((sort_list[i].end + 1) != sort_list[i+1].start) {
  236. pr_err("Adjacent DCSSs %s and %s are not "
  237. "contiguous\n", sort_list[i].segment_name,
  238. sort_list[i+1].segment_name);
  239. rc = -EINVAL;
  240. goto out;
  241. }
  242. /* EN and EW are allowed in a block device */
  243. if (sort_list[i].segment_type != sort_list[i+1].segment_type) {
  244. if (!(sort_list[i].segment_type & SEGMENT_EXCLUSIVE) ||
  245. (sort_list[i].segment_type == SEG_TYPE_ER) ||
  246. !(sort_list[i+1].segment_type &
  247. SEGMENT_EXCLUSIVE) ||
  248. (sort_list[i+1].segment_type == SEG_TYPE_ER)) {
  249. pr_err("DCSS %s and DCSS %s have "
  250. "incompatible types\n",
  251. sort_list[i].segment_name,
  252. sort_list[i+1].segment_name);
  253. rc = -EINVAL;
  254. goto out;
  255. }
  256. }
  257. }
  258. rc = 0;
  259. out:
  260. kfree(sort_list);
  261. return rc;
  262. }
  263. /*
  264. * Load a segment
  265. */
  266. static int
  267. dcssblk_load_segment(char *name, struct segment_info **seg_info)
  268. {
  269. int rc;
  270. /* already loaded? */
  271. down_read(&dcssblk_devices_sem);
  272. *seg_info = dcssblk_get_segment_by_name(name);
  273. up_read(&dcssblk_devices_sem);
  274. if (*seg_info != NULL)
  275. return -EEXIST;
  276. /* get a struct segment_info */
  277. *seg_info = kzalloc(sizeof(struct segment_info), GFP_KERNEL);
  278. if (*seg_info == NULL)
  279. return -ENOMEM;
  280. strcpy((*seg_info)->segment_name, name);
  281. /* load the segment */
  282. rc = segment_load(name, SEGMENT_SHARED,
  283. &(*seg_info)->start, &(*seg_info)->end);
  284. if (rc < 0) {
  285. segment_warning(rc, (*seg_info)->segment_name);
  286. kfree(*seg_info);
  287. } else {
  288. INIT_LIST_HEAD(&(*seg_info)->lh);
  289. (*seg_info)->segment_type = rc;
  290. }
  291. return rc;
  292. }
  293. /*
  294. * device attribute for switching shared/nonshared (exclusive)
  295. * operation (show + store)
  296. */
  297. static ssize_t
  298. dcssblk_shared_show(struct device *dev, struct device_attribute *attr, char *buf)
  299. {
  300. struct dcssblk_dev_info *dev_info;
  301. dev_info = container_of(dev, struct dcssblk_dev_info, dev);
  302. return sprintf(buf, dev_info->is_shared ? "1\n" : "0\n");
  303. }
  304. static ssize_t
  305. dcssblk_shared_store(struct device *dev, struct device_attribute *attr, const char *inbuf, size_t count)
  306. {
  307. struct dcssblk_dev_info *dev_info;
  308. struct segment_info *entry, *temp;
  309. int rc;
  310. if ((count > 1) && (inbuf[1] != '\n') && (inbuf[1] != '\0'))
  311. return -EINVAL;
  312. down_write(&dcssblk_devices_sem);
  313. dev_info = container_of(dev, struct dcssblk_dev_info, dev);
  314. if (atomic_read(&dev_info->use_count)) {
  315. rc = -EBUSY;
  316. goto out;
  317. }
  318. if (inbuf[0] == '1') {
  319. /* reload segments in shared mode */
  320. list_for_each_entry(entry, &dev_info->seg_list, lh) {
  321. rc = segment_modify_shared(entry->segment_name,
  322. SEGMENT_SHARED);
  323. if (rc < 0) {
  324. BUG_ON(rc == -EINVAL);
  325. if (rc != -EAGAIN)
  326. goto removeseg;
  327. }
  328. }
  329. dev_info->is_shared = 1;
  330. switch (dev_info->segment_type) {
  331. case SEG_TYPE_SR:
  332. case SEG_TYPE_ER:
  333. case SEG_TYPE_SC:
  334. set_disk_ro(dev_info->gd, 1);
  335. }
  336. } else if (inbuf[0] == '0') {
  337. /* reload segments in exclusive mode */
  338. if (dev_info->segment_type == SEG_TYPE_SC) {
  339. pr_err("DCSS %s is of type SC and cannot be "
  340. "loaded as exclusive-writable\n",
  341. dev_info->segment_name);
  342. rc = -EINVAL;
  343. goto out;
  344. }
  345. list_for_each_entry(entry, &dev_info->seg_list, lh) {
  346. rc = segment_modify_shared(entry->segment_name,
  347. SEGMENT_EXCLUSIVE);
  348. if (rc < 0) {
  349. BUG_ON(rc == -EINVAL);
  350. if (rc != -EAGAIN)
  351. goto removeseg;
  352. }
  353. }
  354. dev_info->is_shared = 0;
  355. set_disk_ro(dev_info->gd, 0);
  356. } else {
  357. rc = -EINVAL;
  358. goto out;
  359. }
  360. rc = count;
  361. goto out;
  362. removeseg:
  363. pr_err("DCSS device %s is removed after a failed access mode "
  364. "change\n", dev_info->segment_name);
  365. temp = entry;
  366. list_for_each_entry(entry, &dev_info->seg_list, lh) {
  367. if (entry != temp)
  368. segment_unload(entry->segment_name);
  369. }
  370. list_del(&dev_info->lh);
  371. up_write(&dcssblk_devices_sem);
  372. dax_remove_host(dev_info->gd);
  373. kill_dax(dev_info->dax_dev);
  374. put_dax(dev_info->dax_dev);
  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 sprintf(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. int i;
  459. struct dcssblk_dev_info *dev_info;
  460. struct segment_info *entry;
  461. down_read(&dcssblk_devices_sem);
  462. dev_info = container_of(dev, struct dcssblk_dev_info, dev);
  463. i = 0;
  464. buf[0] = '\0';
  465. list_for_each_entry(entry, &dev_info->seg_list, lh) {
  466. strcpy(&buf[i], entry->segment_name);
  467. i += strlen(entry->segment_name);
  468. buf[i] = '\n';
  469. i++;
  470. }
  471. up_read(&dcssblk_devices_sem);
  472. return i;
  473. }
  474. static DEVICE_ATTR(seglist, S_IRUSR, dcssblk_seglist_show, NULL);
  475. static struct attribute *dcssblk_dev_attrs[] = {
  476. &dev_attr_shared.attr,
  477. &dev_attr_save.attr,
  478. &dev_attr_seglist.attr,
  479. NULL,
  480. };
  481. static struct attribute_group dcssblk_dev_attr_group = {
  482. .attrs = dcssblk_dev_attrs,
  483. };
  484. static const struct attribute_group *dcssblk_dev_attr_groups[] = {
  485. &dcssblk_dev_attr_group,
  486. NULL,
  487. };
  488. /*
  489. * device attribute for adding devices
  490. */
  491. static ssize_t
  492. dcssblk_add_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  493. {
  494. struct queue_limits lim = {
  495. .logical_block_size = 4096,
  496. .features = BLK_FEAT_DAX,
  497. };
  498. int rc, i, j, num_of_segments;
  499. struct dcssblk_dev_info *dev_info;
  500. struct segment_info *seg_info, *temp;
  501. struct dax_device *dax_dev;
  502. char *local_buf;
  503. unsigned long seg_byte_size;
  504. dev_info = NULL;
  505. seg_info = NULL;
  506. if (dev != dcssblk_root_dev) {
  507. rc = -EINVAL;
  508. goto out_nobuf;
  509. }
  510. if ((count < 1) || (buf[0] == '\0') || (buf[0] == '\n')) {
  511. rc = -ENAMETOOLONG;
  512. goto out_nobuf;
  513. }
  514. local_buf = kmalloc(count + 1, GFP_KERNEL);
  515. if (local_buf == NULL) {
  516. rc = -ENOMEM;
  517. goto out_nobuf;
  518. }
  519. /*
  520. * parse input
  521. */
  522. num_of_segments = 0;
  523. for (i = 0; (i < count && (buf[i] != '\0') && (buf[i] != '\n')); i++) {
  524. for (j = i; j < count &&
  525. (buf[j] != ':') &&
  526. (buf[j] != '\0') &&
  527. (buf[j] != '\n'); j++) {
  528. local_buf[j-i] = toupper(buf[j]);
  529. }
  530. local_buf[j-i] = '\0';
  531. if (((j - i) == 0) || ((j - i) > 8)) {
  532. rc = -ENAMETOOLONG;
  533. goto seg_list_del;
  534. }
  535. rc = dcssblk_load_segment(local_buf, &seg_info);
  536. if (rc < 0)
  537. goto seg_list_del;
  538. /*
  539. * get a struct dcssblk_dev_info
  540. */
  541. if (num_of_segments == 0) {
  542. dev_info = kzalloc(sizeof(struct dcssblk_dev_info),
  543. GFP_KERNEL);
  544. if (dev_info == NULL) {
  545. rc = -ENOMEM;
  546. goto out;
  547. }
  548. strcpy(dev_info->segment_name, local_buf);
  549. dev_info->segment_type = seg_info->segment_type;
  550. INIT_LIST_HEAD(&dev_info->seg_list);
  551. }
  552. list_add_tail(&seg_info->lh, &dev_info->seg_list);
  553. num_of_segments++;
  554. i = j;
  555. if ((buf[j] == '\0') || (buf[j] == '\n'))
  556. break;
  557. }
  558. /* no trailing colon at the end of the input */
  559. if ((i > 0) && (buf[i-1] == ':')) {
  560. rc = -ENAMETOOLONG;
  561. goto seg_list_del;
  562. }
  563. strscpy(local_buf, buf, i + 1);
  564. dev_info->num_of_segments = num_of_segments;
  565. rc = dcssblk_is_continuous(dev_info);
  566. if (rc < 0)
  567. goto seg_list_del;
  568. dev_info->start = dcssblk_find_lowest_addr(dev_info);
  569. dev_info->end = dcssblk_find_highest_addr(dev_info);
  570. dev_set_name(&dev_info->dev, "%s", dev_info->segment_name);
  571. dev_info->dev.release = dcssblk_release_segment;
  572. dev_info->dev.groups = dcssblk_dev_attr_groups;
  573. INIT_LIST_HEAD(&dev_info->lh);
  574. dev_info->gd = blk_alloc_disk(&lim, NUMA_NO_NODE);
  575. if (IS_ERR(dev_info->gd)) {
  576. rc = PTR_ERR(dev_info->gd);
  577. goto seg_list_del;
  578. }
  579. dev_info->gd->major = dcssblk_major;
  580. dev_info->gd->minors = DCSSBLK_MINORS_PER_DISK;
  581. dev_info->gd->fops = &dcssblk_devops;
  582. dev_info->gd->private_data = dev_info;
  583. dev_info->gd->flags |= GENHD_FL_NO_PART;
  584. seg_byte_size = (dev_info->end - dev_info->start + 1);
  585. set_capacity(dev_info->gd, seg_byte_size >> 9); // size in sectors
  586. pr_info("Loaded %s with total size %lu bytes and capacity %lu "
  587. "sectors\n", local_buf, seg_byte_size, seg_byte_size >> 9);
  588. dev_info->save_pending = 0;
  589. dev_info->is_shared = 1;
  590. dev_info->dev.parent = dcssblk_root_dev;
  591. /*
  592. *get minor, add to list
  593. */
  594. down_write(&dcssblk_devices_sem);
  595. if (dcssblk_get_segment_by_name(local_buf)) {
  596. rc = -EEXIST;
  597. goto release_gd;
  598. }
  599. rc = dcssblk_assign_free_minor(dev_info);
  600. if (rc)
  601. goto release_gd;
  602. sprintf(dev_info->gd->disk_name, "dcssblk%d",
  603. dev_info->gd->first_minor);
  604. list_add_tail(&dev_info->lh, &dcssblk_devices);
  605. if (!try_module_get(THIS_MODULE)) {
  606. rc = -ENODEV;
  607. goto dev_list_del;
  608. }
  609. /*
  610. * register the device
  611. */
  612. rc = device_register(&dev_info->dev);
  613. if (rc)
  614. goto put_dev;
  615. dax_dev = alloc_dax(dev_info, &dcssblk_dax_ops);
  616. if (IS_ERR(dax_dev)) {
  617. rc = PTR_ERR(dax_dev);
  618. goto put_dev;
  619. }
  620. set_dax_synchronous(dax_dev);
  621. dev_info->dax_dev = dax_dev;
  622. rc = dax_add_host(dev_info->dax_dev, dev_info->gd);
  623. if (rc)
  624. goto out_dax;
  625. get_device(&dev_info->dev);
  626. rc = device_add_disk(&dev_info->dev, dev_info->gd, NULL);
  627. if (rc)
  628. goto out_dax_host;
  629. switch (dev_info->segment_type) {
  630. case SEG_TYPE_SR:
  631. case SEG_TYPE_ER:
  632. case SEG_TYPE_SC:
  633. set_disk_ro(dev_info->gd,1);
  634. break;
  635. default:
  636. set_disk_ro(dev_info->gd,0);
  637. break;
  638. }
  639. up_write(&dcssblk_devices_sem);
  640. rc = count;
  641. goto out;
  642. out_dax_host:
  643. put_device(&dev_info->dev);
  644. dax_remove_host(dev_info->gd);
  645. out_dax:
  646. kill_dax(dev_info->dax_dev);
  647. put_dax(dev_info->dax_dev);
  648. put_dev:
  649. list_del(&dev_info->lh);
  650. put_disk(dev_info->gd);
  651. list_for_each_entry(seg_info, &dev_info->seg_list, lh) {
  652. segment_unload(seg_info->segment_name);
  653. }
  654. put_device(&dev_info->dev);
  655. up_write(&dcssblk_devices_sem);
  656. goto out;
  657. dev_list_del:
  658. list_del(&dev_info->lh);
  659. release_gd:
  660. put_disk(dev_info->gd);
  661. up_write(&dcssblk_devices_sem);
  662. seg_list_del:
  663. if (dev_info == NULL)
  664. goto out;
  665. list_for_each_entry_safe(seg_info, temp, &dev_info->seg_list, lh) {
  666. list_del(&seg_info->lh);
  667. segment_unload(seg_info->segment_name);
  668. kfree(seg_info);
  669. }
  670. kfree(dev_info);
  671. out:
  672. kfree(local_buf);
  673. out_nobuf:
  674. return rc;
  675. }
  676. /*
  677. * device attribute for removing devices
  678. */
  679. static ssize_t
  680. dcssblk_remove_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  681. {
  682. struct dcssblk_dev_info *dev_info;
  683. struct segment_info *entry;
  684. int rc, i;
  685. char *local_buf;
  686. if (dev != dcssblk_root_dev) {
  687. return -EINVAL;
  688. }
  689. local_buf = kmalloc(count + 1, GFP_KERNEL);
  690. if (local_buf == NULL) {
  691. return -ENOMEM;
  692. }
  693. /*
  694. * parse input
  695. */
  696. for (i = 0; (i < count && (*(buf+i)!='\0') && (*(buf+i)!='\n')); i++) {
  697. local_buf[i] = toupper(buf[i]);
  698. }
  699. local_buf[i] = '\0';
  700. if ((i == 0) || (i > 8)) {
  701. rc = -ENAMETOOLONG;
  702. goto out_buf;
  703. }
  704. down_write(&dcssblk_devices_sem);
  705. dev_info = dcssblk_get_device_by_name(local_buf);
  706. if (dev_info == NULL) {
  707. up_write(&dcssblk_devices_sem);
  708. pr_warn("Device %s cannot be removed because it is not a known device\n",
  709. local_buf);
  710. rc = -ENODEV;
  711. goto out_buf;
  712. }
  713. if (atomic_read(&dev_info->use_count) != 0) {
  714. up_write(&dcssblk_devices_sem);
  715. pr_warn("Device %s cannot be removed while it is in use\n",
  716. local_buf);
  717. rc = -EBUSY;
  718. goto out_buf;
  719. }
  720. list_del(&dev_info->lh);
  721. /* unload all related segments */
  722. list_for_each_entry(entry, &dev_info->seg_list, lh)
  723. segment_unload(entry->segment_name);
  724. up_write(&dcssblk_devices_sem);
  725. dax_remove_host(dev_info->gd);
  726. kill_dax(dev_info->dax_dev);
  727. put_dax(dev_info->dax_dev);
  728. del_gendisk(dev_info->gd);
  729. put_disk(dev_info->gd);
  730. device_unregister(&dev_info->dev);
  731. put_device(&dev_info->dev);
  732. rc = count;
  733. out_buf:
  734. kfree(local_buf);
  735. return rc;
  736. }
  737. static int
  738. dcssblk_open(struct gendisk *disk, blk_mode_t mode)
  739. {
  740. struct dcssblk_dev_info *dev_info = disk->private_data;
  741. int rc;
  742. if (NULL == dev_info) {
  743. rc = -ENODEV;
  744. goto out;
  745. }
  746. atomic_inc(&dev_info->use_count);
  747. rc = 0;
  748. out:
  749. return rc;
  750. }
  751. static void
  752. dcssblk_release(struct gendisk *disk)
  753. {
  754. struct dcssblk_dev_info *dev_info = disk->private_data;
  755. struct segment_info *entry;
  756. if (!dev_info) {
  757. WARN_ON(1);
  758. return;
  759. }
  760. down_write(&dcssblk_devices_sem);
  761. if (atomic_dec_and_test(&dev_info->use_count)
  762. && (dev_info->save_pending)) {
  763. pr_info("Device %s has become idle and is being saved "
  764. "now\n", dev_info->segment_name);
  765. list_for_each_entry(entry, &dev_info->seg_list, lh) {
  766. if (entry->segment_type == SEG_TYPE_EN ||
  767. entry->segment_type == SEG_TYPE_SN)
  768. pr_warn("DCSS %s is of type SN or EN and cannot"
  769. " be saved\n", entry->segment_name);
  770. else
  771. segment_save(entry->segment_name);
  772. }
  773. dev_info->save_pending = 0;
  774. }
  775. up_write(&dcssblk_devices_sem);
  776. }
  777. static void
  778. dcssblk_submit_bio(struct bio *bio)
  779. {
  780. struct dcssblk_dev_info *dev_info;
  781. struct bio_vec bvec;
  782. struct bvec_iter iter;
  783. unsigned long index;
  784. void *page_addr;
  785. unsigned long source_addr;
  786. unsigned long bytes_done;
  787. bytes_done = 0;
  788. dev_info = bio->bi_bdev->bd_disk->private_data;
  789. if (dev_info == NULL)
  790. goto fail;
  791. if (!IS_ALIGNED(bio->bi_iter.bi_sector, 8) ||
  792. !IS_ALIGNED(bio->bi_iter.bi_size, PAGE_SIZE))
  793. /* Request is not page-aligned. */
  794. goto fail;
  795. /* verify data transfer direction */
  796. if (dev_info->is_shared) {
  797. switch (dev_info->segment_type) {
  798. case SEG_TYPE_SR:
  799. case SEG_TYPE_ER:
  800. case SEG_TYPE_SC:
  801. /* cannot write to these segments */
  802. if (bio_data_dir(bio) == WRITE) {
  803. pr_warn("Writing to %s failed because it is a read-only device\n",
  804. dev_name(&dev_info->dev));
  805. goto fail;
  806. }
  807. }
  808. }
  809. index = (bio->bi_iter.bi_sector >> 3);
  810. bio_for_each_segment(bvec, bio, iter) {
  811. page_addr = bvec_virt(&bvec);
  812. source_addr = dev_info->start + (index<<12) + bytes_done;
  813. if (unlikely(!IS_ALIGNED((unsigned long)page_addr, PAGE_SIZE) ||
  814. !IS_ALIGNED(bvec.bv_len, PAGE_SIZE)))
  815. // More paranoia.
  816. goto fail;
  817. if (bio_data_dir(bio) == READ)
  818. memcpy(page_addr, __va(source_addr), bvec.bv_len);
  819. else
  820. memcpy(__va(source_addr), page_addr, bvec.bv_len);
  821. bytes_done += bvec.bv_len;
  822. }
  823. bio_endio(bio);
  824. return;
  825. fail:
  826. bio_io_error(bio);
  827. }
  828. static long
  829. __dcssblk_direct_access(struct dcssblk_dev_info *dev_info, pgoff_t pgoff,
  830. long nr_pages, void **kaddr, pfn_t *pfn)
  831. {
  832. resource_size_t offset = pgoff * PAGE_SIZE;
  833. unsigned long dev_sz;
  834. dev_sz = dev_info->end - dev_info->start + 1;
  835. if (kaddr)
  836. *kaddr = __va(dev_info->start + offset);
  837. if (pfn)
  838. *pfn = __pfn_to_pfn_t(PFN_DOWN(dev_info->start + offset),
  839. PFN_DEV|PFN_SPECIAL);
  840. return (dev_sz - offset) / PAGE_SIZE;
  841. }
  842. static long
  843. dcssblk_dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff,
  844. long nr_pages, enum dax_access_mode mode, void **kaddr,
  845. pfn_t *pfn)
  846. {
  847. struct dcssblk_dev_info *dev_info = dax_get_private(dax_dev);
  848. return __dcssblk_direct_access(dev_info, pgoff, nr_pages, kaddr, pfn);
  849. }
  850. static void
  851. dcssblk_check_params(void)
  852. {
  853. int rc, i, j, k;
  854. char buf[DCSSBLK_PARM_LEN + 1];
  855. struct dcssblk_dev_info *dev_info;
  856. for (i = 0; (i < DCSSBLK_PARM_LEN) && (dcssblk_segments[i] != '\0');
  857. i++) {
  858. for (j = i; (j < DCSSBLK_PARM_LEN) &&
  859. (dcssblk_segments[j] != ',') &&
  860. (dcssblk_segments[j] != '\0') &&
  861. (dcssblk_segments[j] != '('); j++)
  862. {
  863. buf[j-i] = dcssblk_segments[j];
  864. }
  865. buf[j-i] = '\0';
  866. rc = dcssblk_add_store(dcssblk_root_dev, NULL, buf, j-i);
  867. if ((rc >= 0) && (dcssblk_segments[j] == '(')) {
  868. for (k = 0; (buf[k] != ':') && (buf[k] != '\0'); k++)
  869. buf[k] = toupper(buf[k]);
  870. buf[k] = '\0';
  871. if (!strncmp(&dcssblk_segments[j], "(local)", 7)) {
  872. down_read(&dcssblk_devices_sem);
  873. dev_info = dcssblk_get_device_by_name(buf);
  874. up_read(&dcssblk_devices_sem);
  875. if (dev_info)
  876. dcssblk_shared_store(&dev_info->dev,
  877. NULL, "0\n", 2);
  878. }
  879. }
  880. while ((dcssblk_segments[j] != ',') &&
  881. (dcssblk_segments[j] != '\0'))
  882. {
  883. j++;
  884. }
  885. if (dcssblk_segments[j] == '\0')
  886. break;
  887. i = j;
  888. }
  889. }
  890. /*
  891. * The init/exit functions.
  892. */
  893. static void __exit
  894. dcssblk_exit(void)
  895. {
  896. root_device_unregister(dcssblk_root_dev);
  897. unregister_blkdev(dcssblk_major, DCSSBLK_NAME);
  898. }
  899. static int __init
  900. dcssblk_init(void)
  901. {
  902. int rc;
  903. dcssblk_root_dev = root_device_register("dcssblk");
  904. if (IS_ERR(dcssblk_root_dev))
  905. return PTR_ERR(dcssblk_root_dev);
  906. rc = device_create_file(dcssblk_root_dev, &dev_attr_add);
  907. if (rc)
  908. goto out_root;
  909. rc = device_create_file(dcssblk_root_dev, &dev_attr_remove);
  910. if (rc)
  911. goto out_root;
  912. rc = register_blkdev(0, DCSSBLK_NAME);
  913. if (rc < 0)
  914. goto out_root;
  915. dcssblk_major = rc;
  916. init_rwsem(&dcssblk_devices_sem);
  917. dcssblk_check_params();
  918. return 0;
  919. out_root:
  920. root_device_unregister(dcssblk_root_dev);
  921. return rc;
  922. }
  923. module_init(dcssblk_init);
  924. module_exit(dcssblk_exit);
  925. module_param_string(segments, dcssblk_segments, DCSSBLK_PARM_LEN, 0444);
  926. MODULE_PARM_DESC(segments, "Name of DCSS segment(s) to be loaded, "
  927. "comma-separated list, names in each set separated "
  928. "by commas are separated by colons, each set contains "
  929. "names of contiguous segments and each name max. 8 chars.\n"
  930. "Adding \"(local)\" to the end of each set equals echoing 0 "
  931. "to /sys/devices/dcssblk/<device name>/shared after loading "
  932. "the contiguous segments - \n"
  933. "e.g. segments=\"mydcss1,mydcss2:mydcss3,mydcss4(local)\"");
  934. MODULE_DESCRIPTION("S/390 block driver for DCSS memory");
  935. MODULE_LICENSE("GPL");