dcssblk.c 27 KB

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