dcssblk.c 26 KB

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