dcssblk.c 27 KB

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