dcssblk.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  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->bi_size >> 9) + bio->bi_sector)
  751. > get_capacity(bio->bi_bdev->bd_disk)) {
  752. /* Request beyond end of DCSS segment. */
  753. goto fail;
  754. }
  755. /* verify data transfer direction */
  756. if (dev_info->is_shared) {
  757. switch (dev_info->segment_type) {
  758. case SEG_TYPE_SR:
  759. case SEG_TYPE_ER:
  760. case SEG_TYPE_SC:
  761. /* cannot write to these segments */
  762. if (bio_data_dir(bio) == WRITE) {
  763. pr_warning("Writing to %s failed because it "
  764. "is a read-only device\n",
  765. dev_name(&dev_info->dev));
  766. goto fail;
  767. }
  768. }
  769. }
  770. index = (bio->bi_sector >> 3);
  771. bio_for_each_segment(bvec, bio, i) {
  772. page_addr = (unsigned long)
  773. page_address(bvec->bv_page) + bvec->bv_offset;
  774. source_addr = dev_info->start + (index<<12) + bytes_done;
  775. if (unlikely((page_addr & 4095) != 0) || (bvec->bv_len & 4095) != 0)
  776. // More paranoia.
  777. goto fail;
  778. if (bio_data_dir(bio) == READ) {
  779. memcpy((void*)page_addr, (void*)source_addr,
  780. bvec->bv_len);
  781. } else {
  782. memcpy((void*)source_addr, (void*)page_addr,
  783. bvec->bv_len);
  784. }
  785. bytes_done += bvec->bv_len;
  786. }
  787. bio_endio(bio, 0);
  788. return;
  789. fail:
  790. bio_io_error(bio);
  791. }
  792. static int
  793. dcssblk_direct_access (struct block_device *bdev, sector_t secnum,
  794. void **kaddr, unsigned long *pfn)
  795. {
  796. struct dcssblk_dev_info *dev_info;
  797. unsigned long pgoff;
  798. dev_info = bdev->bd_disk->private_data;
  799. if (!dev_info)
  800. return -ENODEV;
  801. if (secnum % (PAGE_SIZE/512))
  802. return -EINVAL;
  803. pgoff = secnum / (PAGE_SIZE / 512);
  804. if ((pgoff+1)*PAGE_SIZE-1 > dev_info->end - dev_info->start)
  805. return -ERANGE;
  806. *kaddr = (void *) (dev_info->start+pgoff*PAGE_SIZE);
  807. *pfn = virt_to_phys(*kaddr) >> PAGE_SHIFT;
  808. return 0;
  809. }
  810. static void
  811. dcssblk_check_params(void)
  812. {
  813. int rc, i, j, k;
  814. char buf[DCSSBLK_PARM_LEN + 1];
  815. struct dcssblk_dev_info *dev_info;
  816. for (i = 0; (i < DCSSBLK_PARM_LEN) && (dcssblk_segments[i] != '\0');
  817. i++) {
  818. for (j = i; (dcssblk_segments[j] != ',') &&
  819. (dcssblk_segments[j] != '\0') &&
  820. (dcssblk_segments[j] != '(') &&
  821. (j < DCSSBLK_PARM_LEN); j++)
  822. {
  823. buf[j-i] = dcssblk_segments[j];
  824. }
  825. buf[j-i] = '\0';
  826. rc = dcssblk_add_store(dcssblk_root_dev, NULL, buf, j-i);
  827. if ((rc >= 0) && (dcssblk_segments[j] == '(')) {
  828. for (k = 0; (buf[k] != ':') && (buf[k] != '\0'); k++)
  829. buf[k] = toupper(buf[k]);
  830. buf[k] = '\0';
  831. if (!strncmp(&dcssblk_segments[j], "(local)", 7)) {
  832. down_read(&dcssblk_devices_sem);
  833. dev_info = dcssblk_get_device_by_name(buf);
  834. up_read(&dcssblk_devices_sem);
  835. if (dev_info)
  836. dcssblk_shared_store(&dev_info->dev,
  837. NULL, "0\n", 2);
  838. }
  839. }
  840. while ((dcssblk_segments[j] != ',') &&
  841. (dcssblk_segments[j] != '\0'))
  842. {
  843. j++;
  844. }
  845. if (dcssblk_segments[j] == '\0')
  846. break;
  847. i = j;
  848. }
  849. }
  850. /*
  851. * Suspend / Resume
  852. */
  853. static int dcssblk_freeze(struct device *dev)
  854. {
  855. struct dcssblk_dev_info *dev_info;
  856. int rc = 0;
  857. list_for_each_entry(dev_info, &dcssblk_devices, lh) {
  858. switch (dev_info->segment_type) {
  859. case SEG_TYPE_SR:
  860. case SEG_TYPE_ER:
  861. case SEG_TYPE_SC:
  862. if (!dev_info->is_shared)
  863. rc = -EINVAL;
  864. break;
  865. default:
  866. rc = -EINVAL;
  867. break;
  868. }
  869. if (rc)
  870. break;
  871. }
  872. if (rc)
  873. pr_err("Suspending the system failed because DCSS device %s "
  874. "is writable\n",
  875. dev_info->segment_name);
  876. return rc;
  877. }
  878. static int dcssblk_restore(struct device *dev)
  879. {
  880. struct dcssblk_dev_info *dev_info;
  881. struct segment_info *entry;
  882. unsigned long start, end;
  883. int rc = 0;
  884. list_for_each_entry(dev_info, &dcssblk_devices, lh) {
  885. list_for_each_entry(entry, &dev_info->seg_list, lh) {
  886. segment_unload(entry->segment_name);
  887. rc = segment_load(entry->segment_name, SEGMENT_SHARED,
  888. &start, &end);
  889. if (rc < 0) {
  890. // TODO in_use check ?
  891. segment_warning(rc, entry->segment_name);
  892. goto out_panic;
  893. }
  894. if (start != entry->start || end != entry->end) {
  895. pr_err("The address range of DCSS %s changed "
  896. "while the system was suspended\n",
  897. entry->segment_name);
  898. goto out_panic;
  899. }
  900. }
  901. }
  902. return 0;
  903. out_panic:
  904. panic("fatal dcssblk resume error\n");
  905. }
  906. static int dcssblk_thaw(struct device *dev)
  907. {
  908. return 0;
  909. }
  910. static const struct dev_pm_ops dcssblk_pm_ops = {
  911. .freeze = dcssblk_freeze,
  912. .thaw = dcssblk_thaw,
  913. .restore = dcssblk_restore,
  914. };
  915. static struct platform_driver dcssblk_pdrv = {
  916. .driver = {
  917. .name = "dcssblk",
  918. .owner = THIS_MODULE,
  919. .pm = &dcssblk_pm_ops,
  920. },
  921. };
  922. static struct platform_device *dcssblk_pdev;
  923. /*
  924. * The init/exit functions.
  925. */
  926. static void __exit
  927. dcssblk_exit(void)
  928. {
  929. platform_device_unregister(dcssblk_pdev);
  930. platform_driver_unregister(&dcssblk_pdrv);
  931. root_device_unregister(dcssblk_root_dev);
  932. unregister_blkdev(dcssblk_major, DCSSBLK_NAME);
  933. }
  934. static int __init
  935. dcssblk_init(void)
  936. {
  937. int rc;
  938. rc = platform_driver_register(&dcssblk_pdrv);
  939. if (rc)
  940. return rc;
  941. dcssblk_pdev = platform_device_register_simple("dcssblk", -1, NULL,
  942. 0);
  943. if (IS_ERR(dcssblk_pdev)) {
  944. rc = PTR_ERR(dcssblk_pdev);
  945. goto out_pdrv;
  946. }
  947. dcssblk_root_dev = root_device_register("dcssblk");
  948. if (IS_ERR(dcssblk_root_dev)) {
  949. rc = PTR_ERR(dcssblk_root_dev);
  950. goto out_pdev;
  951. }
  952. rc = device_create_file(dcssblk_root_dev, &dev_attr_add);
  953. if (rc)
  954. goto out_root;
  955. rc = device_create_file(dcssblk_root_dev, &dev_attr_remove);
  956. if (rc)
  957. goto out_root;
  958. rc = register_blkdev(0, DCSSBLK_NAME);
  959. if (rc < 0)
  960. goto out_root;
  961. dcssblk_major = rc;
  962. init_rwsem(&dcssblk_devices_sem);
  963. dcssblk_check_params();
  964. return 0;
  965. out_root:
  966. root_device_unregister(dcssblk_root_dev);
  967. out_pdev:
  968. platform_device_unregister(dcssblk_pdev);
  969. out_pdrv:
  970. platform_driver_unregister(&dcssblk_pdrv);
  971. return rc;
  972. }
  973. module_init(dcssblk_init);
  974. module_exit(dcssblk_exit);
  975. module_param_string(segments, dcssblk_segments, DCSSBLK_PARM_LEN, 0444);
  976. MODULE_PARM_DESC(segments, "Name of DCSS segment(s) to be loaded, "
  977. "comma-separated list, names in each set separated "
  978. "by commas are separated by colons, each set contains "
  979. "names of contiguous segments and each name max. 8 chars.\n"
  980. "Adding \"(local)\" to the end of each set equals echoing 0 "
  981. "to /sys/devices/dcssblk/<device name>/shared after loading "
  982. "the contiguous segments - \n"
  983. "e.g. segments=\"mydcss1,mydcss2:mydcss3,mydcss4(local)\"");
  984. MODULE_LICENSE("GPL");