scm_blk.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /*
  2. * Block driver for s390 storage class memory.
  3. *
  4. * Copyright IBM Corp. 2012
  5. * Author(s): Sebastian Ott <sebott@linux.vnet.ibm.com>
  6. */
  7. #define KMSG_COMPONENT "scm_block"
  8. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  9. #include <linux/interrupt.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/module.h>
  12. #include <linux/blkdev.h>
  13. #include <linux/genhd.h>
  14. #include <linux/slab.h>
  15. #include <linux/list.h>
  16. #include <asm/eadm.h>
  17. #include "scm_blk.h"
  18. debug_info_t *scm_debug;
  19. static int scm_major;
  20. static DEFINE_SPINLOCK(list_lock);
  21. static LIST_HEAD(inactive_requests);
  22. static unsigned int nr_requests = 64;
  23. static atomic_t nr_devices = ATOMIC_INIT(0);
  24. module_param(nr_requests, uint, S_IRUGO);
  25. MODULE_PARM_DESC(nr_requests, "Number of parallel requests.");
  26. MODULE_DESCRIPTION("Block driver for s390 storage class memory.");
  27. MODULE_LICENSE("GPL");
  28. MODULE_ALIAS("scm:scmdev*");
  29. static void __scm_free_rq(struct scm_request *scmrq)
  30. {
  31. struct aob_rq_header *aobrq = to_aobrq(scmrq);
  32. free_page((unsigned long) scmrq->aob);
  33. free_page((unsigned long) scmrq->aidaw);
  34. __scm_free_rq_cluster(scmrq);
  35. kfree(aobrq);
  36. }
  37. static void scm_free_rqs(void)
  38. {
  39. struct list_head *iter, *safe;
  40. struct scm_request *scmrq;
  41. spin_lock_irq(&list_lock);
  42. list_for_each_safe(iter, safe, &inactive_requests) {
  43. scmrq = list_entry(iter, struct scm_request, list);
  44. list_del(&scmrq->list);
  45. __scm_free_rq(scmrq);
  46. }
  47. spin_unlock_irq(&list_lock);
  48. }
  49. static int __scm_alloc_rq(void)
  50. {
  51. struct aob_rq_header *aobrq;
  52. struct scm_request *scmrq;
  53. aobrq = kzalloc(sizeof(*aobrq) + sizeof(*scmrq), GFP_KERNEL);
  54. if (!aobrq)
  55. return -ENOMEM;
  56. scmrq = (void *) aobrq->data;
  57. scmrq->aidaw = (void *) get_zeroed_page(GFP_DMA);
  58. scmrq->aob = (void *) get_zeroed_page(GFP_DMA);
  59. if (!scmrq->aob || !scmrq->aidaw) {
  60. __scm_free_rq(scmrq);
  61. return -ENOMEM;
  62. }
  63. if (__scm_alloc_rq_cluster(scmrq)) {
  64. __scm_free_rq(scmrq);
  65. return -ENOMEM;
  66. }
  67. INIT_LIST_HEAD(&scmrq->list);
  68. spin_lock_irq(&list_lock);
  69. list_add(&scmrq->list, &inactive_requests);
  70. spin_unlock_irq(&list_lock);
  71. return 0;
  72. }
  73. static int scm_alloc_rqs(unsigned int nrqs)
  74. {
  75. int ret = 0;
  76. while (nrqs-- && !ret)
  77. ret = __scm_alloc_rq();
  78. return ret;
  79. }
  80. static struct scm_request *scm_request_fetch(void)
  81. {
  82. struct scm_request *scmrq = NULL;
  83. spin_lock(&list_lock);
  84. if (list_empty(&inactive_requests))
  85. goto out;
  86. scmrq = list_first_entry(&inactive_requests, struct scm_request, list);
  87. list_del(&scmrq->list);
  88. out:
  89. spin_unlock(&list_lock);
  90. return scmrq;
  91. }
  92. static void scm_request_done(struct scm_request *scmrq)
  93. {
  94. unsigned long flags;
  95. spin_lock_irqsave(&list_lock, flags);
  96. list_add(&scmrq->list, &inactive_requests);
  97. spin_unlock_irqrestore(&list_lock, flags);
  98. }
  99. static bool scm_permit_request(struct scm_blk_dev *bdev, struct request *req)
  100. {
  101. return rq_data_dir(req) != WRITE || bdev->state != SCM_WR_PROHIBIT;
  102. }
  103. static void scm_request_prepare(struct scm_request *scmrq)
  104. {
  105. struct scm_blk_dev *bdev = scmrq->bdev;
  106. struct scm_device *scmdev = bdev->gendisk->private_data;
  107. struct aidaw *aidaw = scmrq->aidaw;
  108. struct msb *msb = &scmrq->aob->msb[0];
  109. struct req_iterator iter;
  110. struct bio_vec *bv;
  111. msb->bs = MSB_BS_4K;
  112. scmrq->aob->request.msb_count = 1;
  113. msb->scm_addr = scmdev->address +
  114. ((u64) blk_rq_pos(scmrq->request) << 9);
  115. msb->oc = (rq_data_dir(scmrq->request) == READ) ?
  116. MSB_OC_READ : MSB_OC_WRITE;
  117. msb->flags |= MSB_FLAG_IDA;
  118. msb->data_addr = (u64) aidaw;
  119. rq_for_each_segment(bv, scmrq->request, iter) {
  120. WARN_ON(bv->bv_offset);
  121. msb->blk_count += bv->bv_len >> 12;
  122. aidaw->data_addr = (u64) page_address(bv->bv_page);
  123. aidaw++;
  124. }
  125. }
  126. static inline void scm_request_init(struct scm_blk_dev *bdev,
  127. struct scm_request *scmrq,
  128. struct request *req)
  129. {
  130. struct aob_rq_header *aobrq = to_aobrq(scmrq);
  131. struct aob *aob = scmrq->aob;
  132. memset(aob, 0, sizeof(*aob));
  133. memset(scmrq->aidaw, 0, PAGE_SIZE);
  134. aobrq->scmdev = bdev->scmdev;
  135. aob->request.cmd_code = ARQB_CMD_MOVE;
  136. aob->request.data = (u64) aobrq;
  137. scmrq->request = req;
  138. scmrq->bdev = bdev;
  139. scmrq->retries = 4;
  140. scmrq->error = 0;
  141. scm_request_cluster_init(scmrq);
  142. }
  143. static void scm_ensure_queue_restart(struct scm_blk_dev *bdev)
  144. {
  145. if (atomic_read(&bdev->queued_reqs)) {
  146. /* Queue restart is triggered by the next interrupt. */
  147. return;
  148. }
  149. blk_delay_queue(bdev->rq, SCM_QUEUE_DELAY);
  150. }
  151. void scm_request_requeue(struct scm_request *scmrq)
  152. {
  153. struct scm_blk_dev *bdev = scmrq->bdev;
  154. scm_release_cluster(scmrq);
  155. blk_requeue_request(bdev->rq, scmrq->request);
  156. atomic_dec(&bdev->queued_reqs);
  157. scm_request_done(scmrq);
  158. scm_ensure_queue_restart(bdev);
  159. }
  160. void scm_request_finish(struct scm_request *scmrq)
  161. {
  162. struct scm_blk_dev *bdev = scmrq->bdev;
  163. scm_release_cluster(scmrq);
  164. blk_end_request_all(scmrq->request, scmrq->error);
  165. atomic_dec(&bdev->queued_reqs);
  166. scm_request_done(scmrq);
  167. }
  168. static void scm_blk_request(struct request_queue *rq)
  169. {
  170. struct scm_device *scmdev = rq->queuedata;
  171. struct scm_blk_dev *bdev = dev_get_drvdata(&scmdev->dev);
  172. struct scm_request *scmrq;
  173. struct request *req;
  174. int ret;
  175. while ((req = blk_peek_request(rq))) {
  176. if (req->cmd_type != REQ_TYPE_FS) {
  177. blk_start_request(req);
  178. blk_dump_rq_flags(req, KMSG_COMPONENT " bad request");
  179. blk_end_request_all(req, -EIO);
  180. continue;
  181. }
  182. if (!scm_permit_request(bdev, req)) {
  183. scm_ensure_queue_restart(bdev);
  184. return;
  185. }
  186. scmrq = scm_request_fetch();
  187. if (!scmrq) {
  188. SCM_LOG(5, "no request");
  189. scm_ensure_queue_restart(bdev);
  190. return;
  191. }
  192. scm_request_init(bdev, scmrq, req);
  193. if (!scm_reserve_cluster(scmrq)) {
  194. SCM_LOG(5, "cluster busy");
  195. scm_request_done(scmrq);
  196. return;
  197. }
  198. if (scm_need_cluster_request(scmrq)) {
  199. atomic_inc(&bdev->queued_reqs);
  200. blk_start_request(req);
  201. scm_initiate_cluster_request(scmrq);
  202. return;
  203. }
  204. scm_request_prepare(scmrq);
  205. atomic_inc(&bdev->queued_reqs);
  206. blk_start_request(req);
  207. ret = eadm_start_aob(scmrq->aob);
  208. if (ret) {
  209. SCM_LOG(5, "no subchannel");
  210. scm_request_requeue(scmrq);
  211. return;
  212. }
  213. }
  214. }
  215. static void __scmrq_log_error(struct scm_request *scmrq)
  216. {
  217. struct aob *aob = scmrq->aob;
  218. if (scmrq->error == -ETIMEDOUT)
  219. SCM_LOG(1, "Request timeout");
  220. else {
  221. SCM_LOG(1, "Request error");
  222. SCM_LOG_HEX(1, &aob->response, sizeof(aob->response));
  223. }
  224. if (scmrq->retries)
  225. SCM_LOG(1, "Retry request");
  226. else
  227. pr_err("An I/O operation to SCM failed with rc=%d\n",
  228. scmrq->error);
  229. }
  230. void scm_blk_irq(struct scm_device *scmdev, void *data, int error)
  231. {
  232. struct scm_request *scmrq = data;
  233. struct scm_blk_dev *bdev = scmrq->bdev;
  234. scmrq->error = error;
  235. if (error)
  236. __scmrq_log_error(scmrq);
  237. spin_lock(&bdev->lock);
  238. list_add_tail(&scmrq->list, &bdev->finished_requests);
  239. spin_unlock(&bdev->lock);
  240. tasklet_hi_schedule(&bdev->tasklet);
  241. }
  242. static void scm_blk_handle_error(struct scm_request *scmrq)
  243. {
  244. struct scm_blk_dev *bdev = scmrq->bdev;
  245. unsigned long flags;
  246. if (scmrq->error != -EIO)
  247. goto restart;
  248. /* For -EIO the response block is valid. */
  249. switch (scmrq->aob->response.eqc) {
  250. case EQC_WR_PROHIBIT:
  251. spin_lock_irqsave(&bdev->lock, flags);
  252. if (bdev->state != SCM_WR_PROHIBIT)
  253. pr_info("%lx: Write access to the SCM increment is suspended\n",
  254. (unsigned long) bdev->scmdev->address);
  255. bdev->state = SCM_WR_PROHIBIT;
  256. spin_unlock_irqrestore(&bdev->lock, flags);
  257. goto requeue;
  258. default:
  259. break;
  260. }
  261. restart:
  262. if (!eadm_start_aob(scmrq->aob))
  263. return;
  264. requeue:
  265. spin_lock_irqsave(&bdev->rq_lock, flags);
  266. scm_request_requeue(scmrq);
  267. spin_unlock_irqrestore(&bdev->rq_lock, flags);
  268. }
  269. static void scm_blk_tasklet(struct scm_blk_dev *bdev)
  270. {
  271. struct scm_request *scmrq;
  272. unsigned long flags;
  273. spin_lock_irqsave(&bdev->lock, flags);
  274. while (!list_empty(&bdev->finished_requests)) {
  275. scmrq = list_first_entry(&bdev->finished_requests,
  276. struct scm_request, list);
  277. list_del(&scmrq->list);
  278. spin_unlock_irqrestore(&bdev->lock, flags);
  279. if (scmrq->error && scmrq->retries-- > 0) {
  280. scm_blk_handle_error(scmrq);
  281. /* Request restarted or requeued, handle next. */
  282. spin_lock_irqsave(&bdev->lock, flags);
  283. continue;
  284. }
  285. if (scm_test_cluster_request(scmrq)) {
  286. scm_cluster_request_irq(scmrq);
  287. spin_lock_irqsave(&bdev->lock, flags);
  288. continue;
  289. }
  290. scm_request_finish(scmrq);
  291. spin_lock_irqsave(&bdev->lock, flags);
  292. }
  293. spin_unlock_irqrestore(&bdev->lock, flags);
  294. /* Look out for more requests. */
  295. blk_run_queue(bdev->rq);
  296. }
  297. static const struct block_device_operations scm_blk_devops = {
  298. .owner = THIS_MODULE,
  299. };
  300. int scm_blk_dev_setup(struct scm_blk_dev *bdev, struct scm_device *scmdev)
  301. {
  302. struct request_queue *rq;
  303. int len, ret = -ENOMEM;
  304. unsigned int devindex, nr_max_blk;
  305. devindex = atomic_inc_return(&nr_devices) - 1;
  306. /* scma..scmz + scmaa..scmzz */
  307. if (devindex > 701) {
  308. ret = -ENODEV;
  309. goto out;
  310. }
  311. bdev->scmdev = scmdev;
  312. bdev->state = SCM_OPER;
  313. spin_lock_init(&bdev->rq_lock);
  314. spin_lock_init(&bdev->lock);
  315. INIT_LIST_HEAD(&bdev->finished_requests);
  316. atomic_set(&bdev->queued_reqs, 0);
  317. tasklet_init(&bdev->tasklet,
  318. (void (*)(unsigned long)) scm_blk_tasklet,
  319. (unsigned long) bdev);
  320. rq = blk_init_queue(scm_blk_request, &bdev->rq_lock);
  321. if (!rq)
  322. goto out;
  323. bdev->rq = rq;
  324. nr_max_blk = min(scmdev->nr_max_block,
  325. (unsigned int) (PAGE_SIZE / sizeof(struct aidaw)));
  326. blk_queue_logical_block_size(rq, 1 << 12);
  327. blk_queue_max_hw_sectors(rq, nr_max_blk << 3); /* 8 * 512 = blk_size */
  328. blk_queue_max_segments(rq, nr_max_blk);
  329. queue_flag_set_unlocked(QUEUE_FLAG_NONROT, rq);
  330. scm_blk_dev_cluster_setup(bdev);
  331. bdev->gendisk = alloc_disk(SCM_NR_PARTS);
  332. if (!bdev->gendisk)
  333. goto out_queue;
  334. rq->queuedata = scmdev;
  335. bdev->gendisk->driverfs_dev = &scmdev->dev;
  336. bdev->gendisk->private_data = scmdev;
  337. bdev->gendisk->fops = &scm_blk_devops;
  338. bdev->gendisk->queue = rq;
  339. bdev->gendisk->major = scm_major;
  340. bdev->gendisk->first_minor = devindex * SCM_NR_PARTS;
  341. len = snprintf(bdev->gendisk->disk_name, DISK_NAME_LEN, "scm");
  342. if (devindex > 25) {
  343. len += snprintf(bdev->gendisk->disk_name + len,
  344. DISK_NAME_LEN - len, "%c",
  345. 'a' + (devindex / 26) - 1);
  346. devindex = devindex % 26;
  347. }
  348. snprintf(bdev->gendisk->disk_name + len, DISK_NAME_LEN - len, "%c",
  349. 'a' + devindex);
  350. /* 512 byte sectors */
  351. set_capacity(bdev->gendisk, scmdev->size >> 9);
  352. add_disk(bdev->gendisk);
  353. return 0;
  354. out_queue:
  355. blk_cleanup_queue(rq);
  356. out:
  357. atomic_dec(&nr_devices);
  358. return ret;
  359. }
  360. void scm_blk_dev_cleanup(struct scm_blk_dev *bdev)
  361. {
  362. tasklet_kill(&bdev->tasklet);
  363. del_gendisk(bdev->gendisk);
  364. blk_cleanup_queue(bdev->gendisk->queue);
  365. put_disk(bdev->gendisk);
  366. }
  367. void scm_blk_set_available(struct scm_blk_dev *bdev)
  368. {
  369. unsigned long flags;
  370. spin_lock_irqsave(&bdev->lock, flags);
  371. if (bdev->state == SCM_WR_PROHIBIT)
  372. pr_info("%lx: Write access to the SCM increment is restored\n",
  373. (unsigned long) bdev->scmdev->address);
  374. bdev->state = SCM_OPER;
  375. spin_unlock_irqrestore(&bdev->lock, flags);
  376. }
  377. static int __init scm_blk_init(void)
  378. {
  379. int ret = -EINVAL;
  380. if (!scm_cluster_size_valid())
  381. goto out;
  382. ret = register_blkdev(0, "scm");
  383. if (ret < 0)
  384. goto out;
  385. scm_major = ret;
  386. ret = scm_alloc_rqs(nr_requests);
  387. if (ret)
  388. goto out_free;
  389. scm_debug = debug_register("scm_log", 16, 1, 16);
  390. if (!scm_debug) {
  391. ret = -ENOMEM;
  392. goto out_free;
  393. }
  394. debug_register_view(scm_debug, &debug_hex_ascii_view);
  395. debug_set_level(scm_debug, 2);
  396. ret = scm_drv_init();
  397. if (ret)
  398. goto out_dbf;
  399. return ret;
  400. out_dbf:
  401. debug_unregister(scm_debug);
  402. out_free:
  403. scm_free_rqs();
  404. unregister_blkdev(scm_major, "scm");
  405. out:
  406. return ret;
  407. }
  408. module_init(scm_blk_init);
  409. static void __exit scm_blk_cleanup(void)
  410. {
  411. scm_drv_cleanup();
  412. debug_unregister(scm_debug);
  413. scm_free_rqs();
  414. unregister_blkdev(scm_major, "scm");
  415. }
  416. module_exit(scm_blk_cleanup);